* src/xdisp.c (redisplay_internal): Don't call set_window_update_flags.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
304
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
308
309 #define INFINITY 10000000
310
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
323
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
326
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
330
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
333
334 static Lisp_Object Qfontification_functions;
335
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
339
340 /* Non-nil means don't actually do any redisplay. */
341
342 Lisp_Object Qinhibit_redisplay;
343
344 /* Names of text properties relevant for redisplay. */
345
346 Lisp_Object Qdisplay;
347
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
356
357 #ifdef HAVE_WINDOW_SYSTEM
358
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
361
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
369
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
373
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
377
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
389
390 /* Name of the face used to highlight trailing whitespace. */
391
392 static Lisp_Object Qtrailing_whitespace;
393
394 /* Name and number of the face used to highlight escape glyphs. */
395
396 static Lisp_Object Qescape_glyph;
397
398 /* Name and number of the face used to highlight non-breaking spaces. */
399
400 static Lisp_Object Qnobreak_space;
401
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
404
405 Lisp_Object Qimage;
406
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
411
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
414
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
417
418 bool noninteractive_need_newline;
419
420 /* Non-zero means print newline to message log before next message. */
421
422 static bool message_log_need_newline;
423
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
430 \f
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
435
436 static struct text_pos this_line_start_pos;
437
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
440
441 static struct text_pos this_line_end_pos;
442
443 /* The vertical positions and the height of this line. */
444
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
448
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
451
452 static int this_line_start_x;
453
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
457
458 static struct text_pos this_line_min_pos;
459
460 /* Buffer that this_line_.* variables are referring to. */
461
462 static struct buffer *this_line_buffer;
463
464
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
469
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
471
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
474
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
476
477 Lisp_Object Qmenu_bar_update_hook;
478
479 /* Nonzero if an overlay arrow has been displayed in this window. */
480
481 static bool overlay_arrow_seen;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
490
491 Lisp_Object echo_area_window;
492
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
497
498 static Lisp_Object Vmessage_stack;
499
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
502
503 static bool message_enable_multibyte;
504
505 /* Nonzero if we should redraw the mode lines on the next redisplay.
506 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
507 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
508 (the number used is then only used to track down the cause for this
509 full-redisplay). */
510
511 int update_mode_lines;
512
513 /* Nonzero if window sizes or contents other than selected-window have changed
514 since last redisplay that finished.
515 If it has value REDISPLAY_SOME, then only redisplay the windows where
516 the `redisplay' bit has been set. Otherwise, redisplay all windows
517 (the number used is then only used to track down the cause for this
518 full-redisplay). */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero after display_mode_line if %l was used and it displayed a
523 line number. */
524
525 static bool line_number_displayed;
526
527 /* The name of the *Messages* buffer, a string. */
528
529 static Lisp_Object Vmessages_buffer_name;
530
531 /* Current, index 0, and last displayed echo area message. Either
532 buffers from echo_buffers, or nil to indicate no message. */
533
534 Lisp_Object echo_area_buffer[2];
535
536 /* The buffers referenced from echo_area_buffer. */
537
538 static Lisp_Object echo_buffer[2];
539
540 /* A vector saved used in with_area_buffer to reduce consing. */
541
542 static Lisp_Object Vwith_echo_area_save_vector;
543
544 /* Non-zero means display_echo_area should display the last echo area
545 message again. Set by redisplay_preserve_echo_area. */
546
547 static bool display_last_displayed_message_p;
548
549 /* Nonzero if echo area is being used by print; zero if being used by
550 message. */
551
552 static bool message_buf_print;
553
554 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
555
556 static Lisp_Object Qinhibit_menubar_update;
557 static Lisp_Object Qmessage_truncate_lines;
558
559 /* Set to 1 in clear_message to make redisplay_internal aware
560 of an emptied echo area. */
561
562 static bool message_cleared_p;
563
564 /* A scratch glyph row with contents used for generating truncation
565 glyphs. Also used in direct_output_for_insert. */
566
567 #define MAX_SCRATCH_GLYPHS 100
568 static struct glyph_row scratch_glyph_row;
569 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
570
571 /* Ascent and height of the last line processed by move_it_to. */
572
573 static int last_height;
574
575 /* Non-zero if there's a help-echo in the echo area. */
576
577 bool help_echo_showing_p;
578
579 /* The maximum distance to look ahead for text properties. Values
580 that are too small let us call compute_char_face and similar
581 functions too often which is expensive. Values that are too large
582 let us call compute_char_face and alike too often because we
583 might not be interested in text properties that far away. */
584
585 #define TEXT_PROP_DISTANCE_LIMIT 100
586
587 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
588 iterator state and later restore it. This is needed because the
589 bidi iterator on bidi.c keeps a stacked cache of its states, which
590 is really a singleton. When we use scratch iterator objects to
591 move around the buffer, we can cause the bidi cache to be pushed or
592 popped, and therefore we need to restore the cache state when we
593 return to the original iterator. */
594 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
595 do { \
596 if (CACHE) \
597 bidi_unshelve_cache (CACHE, 1); \
598 ITCOPY = ITORIG; \
599 CACHE = bidi_shelve_cache (); \
600 } while (0)
601
602 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
603 do { \
604 if (pITORIG != pITCOPY) \
605 *(pITORIG) = *(pITCOPY); \
606 bidi_unshelve_cache (CACHE, 0); \
607 CACHE = NULL; \
608 } while (0)
609
610 /* Functions to mark elements as needing redisplay. */
611 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
612
613 void
614 redisplay_other_windows (void)
615 {
616 if (!windows_or_buffers_changed)
617 windows_or_buffers_changed = REDISPLAY_SOME;
618 }
619
620 void
621 wset_redisplay (struct window *w)
622 {
623 redisplay_other_windows ();
624 w->redisplay = true;
625 }
626
627 void
628 fset_redisplay (struct frame *f)
629 {
630 redisplay_other_windows ();
631 f->redisplay = true;
632 }
633
634 void
635 bset_redisplay (struct buffer *b)
636 {
637 int count = buffer_window_count (b);
638 if (count > 0)
639 {
640 /* ... it's visible in other window than selected, */
641 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
642 redisplay_other_windows ();
643 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
644 so that if we later set windows_or_buffers_changed, this buffer will
645 not be omitted. */
646 b->text->redisplay = true;
647 }
648 }
649
650 void
651 bset_update_mode_line (struct buffer *b)
652 {
653 if (!update_mode_lines)
654 update_mode_lines = REDISPLAY_SOME;
655 b->text->redisplay = true;
656 }
657
658 #ifdef GLYPH_DEBUG
659
660 /* Non-zero means print traces of redisplay if compiled with
661 GLYPH_DEBUG defined. */
662
663 int trace_redisplay_p;
664
665 #endif /* GLYPH_DEBUG */
666
667 #ifdef DEBUG_TRACE_MOVE
668 /* Non-zero means trace with TRACE_MOVE to stderr. */
669 int trace_move;
670
671 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
672 #else
673 #define TRACE_MOVE(x) (void) 0
674 #endif
675
676 static Lisp_Object Qauto_hscroll_mode;
677
678 /* Buffer being redisplayed -- for redisplay_window_error. */
679
680 static struct buffer *displayed_buffer;
681
682 /* Value returned from text property handlers (see below). */
683
684 enum prop_handled
685 {
686 HANDLED_NORMALLY,
687 HANDLED_RECOMPUTE_PROPS,
688 HANDLED_OVERLAY_STRING_CONSUMED,
689 HANDLED_RETURN
690 };
691
692 /* A description of text properties that redisplay is interested
693 in. */
694
695 struct props
696 {
697 /* The name of the property. */
698 Lisp_Object *name;
699
700 /* A unique index for the property. */
701 enum prop_idx idx;
702
703 /* A handler function called to set up iterator IT from the property
704 at IT's current position. Value is used to steer handle_stop. */
705 enum prop_handled (*handler) (struct it *it);
706 };
707
708 static enum prop_handled handle_face_prop (struct it *);
709 static enum prop_handled handle_invisible_prop (struct it *);
710 static enum prop_handled handle_display_prop (struct it *);
711 static enum prop_handled handle_composition_prop (struct it *);
712 static enum prop_handled handle_overlay_change (struct it *);
713 static enum prop_handled handle_fontified_prop (struct it *);
714
715 /* Properties handled by iterators. */
716
717 static struct props it_props[] =
718 {
719 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
720 /* Handle `face' before `display' because some sub-properties of
721 `display' need to know the face. */
722 {&Qface, FACE_PROP_IDX, handle_face_prop},
723 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
724 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
725 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
726 {NULL, 0, NULL}
727 };
728
729 /* Value is the position described by X. If X is a marker, value is
730 the marker_position of X. Otherwise, value is X. */
731
732 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
733
734 /* Enumeration returned by some move_it_.* functions internally. */
735
736 enum move_it_result
737 {
738 /* Not used. Undefined value. */
739 MOVE_UNDEFINED,
740
741 /* Move ended at the requested buffer position or ZV. */
742 MOVE_POS_MATCH_OR_ZV,
743
744 /* Move ended at the requested X pixel position. */
745 MOVE_X_REACHED,
746
747 /* Move within a line ended at the end of a line that must be
748 continued. */
749 MOVE_LINE_CONTINUED,
750
751 /* Move within a line ended at the end of a line that would
752 be displayed truncated. */
753 MOVE_LINE_TRUNCATED,
754
755 /* Move within a line ended at a line end. */
756 MOVE_NEWLINE_OR_CR
757 };
758
759 /* This counter is used to clear the face cache every once in a while
760 in redisplay_internal. It is incremented for each redisplay.
761 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
762 cleared. */
763
764 #define CLEAR_FACE_CACHE_COUNT 500
765 static int clear_face_cache_count;
766
767 /* Similarly for the image cache. */
768
769 #ifdef HAVE_WINDOW_SYSTEM
770 #define CLEAR_IMAGE_CACHE_COUNT 101
771 static int clear_image_cache_count;
772
773 /* Null glyph slice */
774 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
775 #endif
776
777 /* True while redisplay_internal is in progress. */
778
779 bool redisplaying_p;
780
781 static Lisp_Object Qinhibit_free_realized_faces;
782 static Lisp_Object Qmode_line_default_help_echo;
783
784 /* If a string, XTread_socket generates an event to display that string.
785 (The display is done in read_char.) */
786
787 Lisp_Object help_echo_string;
788 Lisp_Object help_echo_window;
789 Lisp_Object help_echo_object;
790 ptrdiff_t help_echo_pos;
791
792 /* Temporary variable for XTread_socket. */
793
794 Lisp_Object previous_help_echo_string;
795
796 /* Platform-independent portion of hourglass implementation. */
797
798 #ifdef HAVE_WINDOW_SYSTEM
799
800 /* Non-zero means an hourglass cursor is currently shown. */
801 bool hourglass_shown_p;
802
803 /* If non-null, an asynchronous timer that, when it expires, displays
804 an hourglass cursor on all frames. */
805 struct atimer *hourglass_atimer;
806
807 #endif /* HAVE_WINDOW_SYSTEM */
808
809 /* Name of the face used to display glyphless characters. */
810 static Lisp_Object Qglyphless_char;
811
812 /* Symbol for the purpose of Vglyphless_char_display. */
813 static Lisp_Object Qglyphless_char_display;
814
815 /* Method symbols for Vglyphless_char_display. */
816 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
817
818 /* Default number of seconds to wait before displaying an hourglass
819 cursor. */
820 #define DEFAULT_HOURGLASS_DELAY 1
821
822 #ifdef HAVE_WINDOW_SYSTEM
823
824 /* Default pixel width of `thin-space' display method. */
825 #define THIN_SPACE_WIDTH 1
826
827 #endif /* HAVE_WINDOW_SYSTEM */
828
829 /* Function prototypes. */
830
831 static void setup_for_ellipsis (struct it *, int);
832 static void set_iterator_to_next (struct it *, int);
833 static void mark_window_display_accurate_1 (struct window *, int);
834 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
835 static int display_prop_string_p (Lisp_Object, Lisp_Object);
836 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
837 static int cursor_row_p (struct glyph_row *);
838 static int redisplay_mode_lines (Lisp_Object, bool);
839 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
840
841 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
842
843 static void handle_line_prefix (struct it *);
844
845 static void pint2str (char *, int, ptrdiff_t);
846 static void pint2hrstr (char *, int, ptrdiff_t);
847 static struct text_pos run_window_scroll_functions (Lisp_Object,
848 struct text_pos);
849 static int text_outside_line_unchanged_p (struct window *,
850 ptrdiff_t, ptrdiff_t);
851 static void store_mode_line_noprop_char (char);
852 static int store_mode_line_noprop (const char *, int, int);
853 static void handle_stop (struct it *);
854 static void handle_stop_backwards (struct it *, ptrdiff_t);
855 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
856 static void ensure_echo_area_buffers (void);
857 static void unwind_with_echo_area_buffer (Lisp_Object);
858 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
859 static int with_echo_area_buffer (struct window *, int,
860 int (*) (ptrdiff_t, Lisp_Object),
861 ptrdiff_t, Lisp_Object);
862 static void clear_garbaged_frames (void);
863 static int current_message_1 (ptrdiff_t, Lisp_Object);
864 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
865 static void set_message (Lisp_Object);
866 static int set_message_1 (ptrdiff_t, Lisp_Object);
867 static int display_echo_area (struct window *);
868 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
869 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
870 static void unwind_redisplay (void);
871 static int string_char_and_length (const unsigned char *, int *);
872 static struct text_pos display_prop_end (struct it *, Lisp_Object,
873 struct text_pos);
874 static int compute_window_start_on_continuation_line (struct window *);
875 static void insert_left_trunc_glyphs (struct it *);
876 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
877 Lisp_Object);
878 static void extend_face_to_end_of_line (struct it *);
879 static int append_space_for_newline (struct it *, int);
880 static int cursor_row_fully_visible_p (struct window *, int, int);
881 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
882 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
883 static int trailing_whitespace_p (ptrdiff_t);
884 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
885 static void push_it (struct it *, struct text_pos *);
886 static void iterate_out_of_display_property (struct it *);
887 static void pop_it (struct it *);
888 static void sync_frame_with_window_matrix_rows (struct window *);
889 static void redisplay_internal (void);
890 static int echo_area_display (int);
891 static void redisplay_windows (Lisp_Object);
892 static void redisplay_window (Lisp_Object, bool);
893 static Lisp_Object redisplay_window_error (Lisp_Object);
894 static Lisp_Object redisplay_window_0 (Lisp_Object);
895 static Lisp_Object redisplay_window_1 (Lisp_Object);
896 static int set_cursor_from_row (struct window *, struct glyph_row *,
897 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
898 int, int);
899 static int update_menu_bar (struct frame *, int, int);
900 static int try_window_reusing_current_matrix (struct window *);
901 static int try_window_id (struct window *);
902 static int display_line (struct it *);
903 static int display_mode_lines (struct window *);
904 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
905 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
906 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
907 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
908 static void display_menu_bar (struct window *);
909 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
910 ptrdiff_t *);
911 static int display_string (const char *, Lisp_Object, Lisp_Object,
912 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
913 static void compute_line_metrics (struct it *);
914 static void run_redisplay_end_trigger_hook (struct it *);
915 static int get_overlay_strings (struct it *, ptrdiff_t);
916 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
917 static void next_overlay_string (struct it *);
918 static void reseat (struct it *, struct text_pos, int);
919 static void reseat_1 (struct it *, struct text_pos, int);
920 static void back_to_previous_visible_line_start (struct it *);
921 static void reseat_at_next_visible_line_start (struct it *, int);
922 static int next_element_from_ellipsis (struct it *);
923 static int next_element_from_display_vector (struct it *);
924 static int next_element_from_string (struct it *);
925 static int next_element_from_c_string (struct it *);
926 static int next_element_from_buffer (struct it *);
927 static int next_element_from_composition (struct it *);
928 static int next_element_from_image (struct it *);
929 static int next_element_from_stretch (struct it *);
930 static void load_overlay_strings (struct it *, ptrdiff_t);
931 static int init_from_display_pos (struct it *, struct window *,
932 struct display_pos *);
933 static void reseat_to_string (struct it *, const char *,
934 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
935 static int get_next_display_element (struct it *);
936 static enum move_it_result
937 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
938 enum move_operation_enum);
939 static void get_visually_first_element (struct it *);
940 static void init_to_row_start (struct it *, struct window *,
941 struct glyph_row *);
942 static int init_to_row_end (struct it *, struct window *,
943 struct glyph_row *);
944 static void back_to_previous_line_start (struct it *);
945 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
946 static struct text_pos string_pos_nchars_ahead (struct text_pos,
947 Lisp_Object, ptrdiff_t);
948 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
949 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
950 static ptrdiff_t number_of_chars (const char *, bool);
951 static void compute_stop_pos (struct it *);
952 static void compute_string_pos (struct text_pos *, struct text_pos,
953 Lisp_Object);
954 static int face_before_or_after_it_pos (struct it *, int);
955 static ptrdiff_t next_overlay_change (ptrdiff_t);
956 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
957 Lisp_Object, struct text_pos *, ptrdiff_t, int);
958 static int handle_single_display_spec (struct it *, Lisp_Object,
959 Lisp_Object, Lisp_Object,
960 struct text_pos *, ptrdiff_t, int, int);
961 static int underlying_face_id (struct it *);
962 static int in_ellipses_for_invisible_text_p (struct display_pos *,
963 struct window *);
964
965 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
966 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
967
968 #ifdef HAVE_WINDOW_SYSTEM
969
970 static void x_consider_frame_title (Lisp_Object);
971 static void update_tool_bar (struct frame *, int);
972 static int redisplay_tool_bar (struct frame *);
973 static void notice_overwritten_cursor (struct window *,
974 enum glyph_row_area,
975 int, int, int, int);
976 static void append_stretch_glyph (struct it *, Lisp_Object,
977 int, int, int);
978
979
980 #endif /* HAVE_WINDOW_SYSTEM */
981
982 static void produce_special_glyphs (struct it *, enum display_element_type);
983 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
984 static int coords_in_mouse_face_p (struct window *, int, int);
985
986
987 \f
988 /***********************************************************************
989 Window display dimensions
990 ***********************************************************************/
991
992 /* Return the bottom boundary y-position for text lines in window W.
993 This is the first y position at which a line cannot start.
994 It is relative to the top of the window.
995
996 This is the height of W minus the height of a mode line, if any. */
997
998 int
999 window_text_bottom_y (struct window *w)
1000 {
1001 int height = WINDOW_TOTAL_HEIGHT (w);
1002
1003 if (WINDOW_WANTS_MODELINE_P (w))
1004 height -= CURRENT_MODE_LINE_HEIGHT (w);
1005 return height;
1006 }
1007
1008 /* Return the pixel width of display area AREA of window W.
1009 ANY_AREA means return the total width of W, not including
1010 fringes to the left and right of the window. */
1011
1012 int
1013 window_box_width (struct window *w, enum glyph_row_area area)
1014 {
1015 int cols = w->total_cols;
1016 int pixels = 0;
1017
1018 if (!w->pseudo_window_p)
1019 {
1020 cols -= WINDOW_SCROLL_BAR_COLS (w);
1021
1022 if (area == TEXT_AREA)
1023 {
1024 cols -= max (0, w->left_margin_cols);
1025 cols -= max (0, w->right_margin_cols);
1026 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1027 }
1028 else if (area == LEFT_MARGIN_AREA)
1029 {
1030 cols = max (0, w->left_margin_cols);
1031 pixels = 0;
1032 }
1033 else if (area == RIGHT_MARGIN_AREA)
1034 {
1035 cols = max (0, w->right_margin_cols);
1036 pixels = 0;
1037 }
1038 }
1039
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1041 }
1042
1043
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1046
1047 int
1048 window_box_height (struct window *w)
1049 {
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_TOTAL_HEIGHT (w);
1052
1053 eassert (height >= 0);
1054
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1060
1061 if (WINDOW_WANTS_MODELINE_P (w))
1062 {
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1071 }
1072
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1074 {
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1083 }
1084
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1088 }
1089
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. ANY_AREA means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1093
1094 int
1095 window_box_left_offset (struct window *w, enum glyph_row_area area)
1096 {
1097 int x;
1098
1099 if (w->pseudo_window_p)
1100 return 0;
1101
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 ? 0
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. ANY_AREA means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 int
1127 window_box_right_offset (struct window *w, enum glyph_row_area area)
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. ANY_AREA means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 int
1137 window_box_left (struct window *w, enum glyph_row_area area)
1138 {
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1141
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1144
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1147
1148 return x;
1149 }
1150
1151
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. ANY_AREA means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1155
1156 int
1157 window_box_right (struct window *w, enum glyph_row_area area)
1158 {
1159 return window_box_left (w, area) + window_box_width (w, area);
1160 }
1161
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. ANY_AREA means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168
1169 void
1170 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1171 int *box_y, int *box_width, int *box_height)
1172 {
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1180 {
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1184 }
1185 }
1186
1187 #ifdef HAVE_WINDOW_SYSTEM
1188
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1195
1196 static void
1197 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1199 {
1200 window_box (w, ANY_AREA, top_left_x, top_left_y,
1201 bottom_right_x, bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1204 }
1205
1206 #endif /* HAVE_WINDOW_SYSTEM */
1207
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1211
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1214
1215 int
1216 line_bottom_y (struct it *it)
1217 {
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1220
1221 if (line_height == 0)
1222 {
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1226 {
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1231 }
1232 else
1233 {
1234 struct glyph_row *row = it->glyph_row;
1235
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1244 }
1245 }
1246
1247 return line_top_y + line_height;
1248 }
1249
1250 DEFUN ("line-pixel-height", Fline_pixel_height,
1251 Sline_pixel_height, 0, 0, 0,
1252 doc: /* Return height in pixels of text line in the selected window.
1253
1254 Value is the height in pixels of the line at point. */)
1255 (void)
1256 {
1257 struct it it;
1258 struct text_pos pt;
1259 struct window *w = XWINDOW (selected_window);
1260
1261 SET_TEXT_POS (pt, PT, PT_BYTE);
1262 start_display (&it, w, pt);
1263 it.vpos = it.current_y = 0;
1264 last_height = 0;
1265 return make_number (line_bottom_y (&it));
1266 }
1267
1268 /* Return the default pixel height of text lines in window W. The
1269 value is the canonical height of the W frame's default font, plus
1270 any extra space required by the line-spacing variable or frame
1271 parameter.
1272
1273 Implementation note: this ignores any line-spacing text properties
1274 put on the newline characters. This is because those properties
1275 only affect the _screen_ line ending in the newline (i.e., in a
1276 continued line, only the last screen line will be affected), which
1277 means only a small number of lines in a buffer can ever use this
1278 feature. Since this function is used to compute the default pixel
1279 equivalent of text lines in a window, we can safely ignore those
1280 few lines. For the same reasons, we ignore the line-height
1281 properties. */
1282 int
1283 default_line_pixel_height (struct window *w)
1284 {
1285 struct frame *f = WINDOW_XFRAME (w);
1286 int height = FRAME_LINE_HEIGHT (f);
1287
1288 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1289 {
1290 struct buffer *b = XBUFFER (w->contents);
1291 Lisp_Object val = BVAR (b, extra_line_spacing);
1292
1293 if (NILP (val))
1294 val = BVAR (&buffer_defaults, extra_line_spacing);
1295 if (!NILP (val))
1296 {
1297 if (RANGED_INTEGERP (0, val, INT_MAX))
1298 height += XFASTINT (val);
1299 else if (FLOATP (val))
1300 {
1301 int addon = XFLOAT_DATA (val) * height + 0.5;
1302
1303 if (addon >= 0)
1304 height += addon;
1305 }
1306 }
1307 else
1308 height += f->extra_line_spacing;
1309 }
1310
1311 return height;
1312 }
1313
1314 /* Subroutine of pos_visible_p below. Extracts a display string, if
1315 any, from the display spec given as its argument. */
1316 static Lisp_Object
1317 string_from_display_spec (Lisp_Object spec)
1318 {
1319 if (CONSP (spec))
1320 {
1321 while (CONSP (spec))
1322 {
1323 if (STRINGP (XCAR (spec)))
1324 return XCAR (spec);
1325 spec = XCDR (spec);
1326 }
1327 }
1328 else if (VECTORP (spec))
1329 {
1330 ptrdiff_t i;
1331
1332 for (i = 0; i < ASIZE (spec); i++)
1333 {
1334 if (STRINGP (AREF (spec, i)))
1335 return AREF (spec, i);
1336 }
1337 return Qnil;
1338 }
1339
1340 return spec;
1341 }
1342
1343
1344 /* Limit insanely large values of W->hscroll on frame F to the largest
1345 value that will still prevent first_visible_x and last_visible_x of
1346 'struct it' from overflowing an int. */
1347 static int
1348 window_hscroll_limited (struct window *w, struct frame *f)
1349 {
1350 ptrdiff_t window_hscroll = w->hscroll;
1351 int window_text_width = window_box_width (w, TEXT_AREA);
1352 int colwidth = FRAME_COLUMN_WIDTH (f);
1353
1354 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1355 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1356
1357 return window_hscroll;
1358 }
1359
1360 /* Return 1 if position CHARPOS is visible in window W.
1361 CHARPOS < 0 means return info about WINDOW_END position.
1362 If visible, set *X and *Y to pixel coordinates of top left corner.
1363 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1364 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1365
1366 int
1367 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1368 int *rtop, int *rbot, int *rowh, int *vpos)
1369 {
1370 struct it it;
1371 void *itdata = bidi_shelve_cache ();
1372 struct text_pos top;
1373 int visible_p = 0;
1374 struct buffer *old_buffer = NULL;
1375
1376 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1377 return visible_p;
1378
1379 if (XBUFFER (w->contents) != current_buffer)
1380 {
1381 old_buffer = current_buffer;
1382 set_buffer_internal_1 (XBUFFER (w->contents));
1383 }
1384
1385 SET_TEXT_POS_FROM_MARKER (top, w->start);
1386 /* Scrolling a minibuffer window via scroll bar when the echo area
1387 shows long text sometimes resets the minibuffer contents behind
1388 our backs. */
1389 if (CHARPOS (top) > ZV)
1390 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1391
1392 /* Compute exact mode line heights. */
1393 if (WINDOW_WANTS_MODELINE_P (w))
1394 w->mode_line_height
1395 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1396 BVAR (current_buffer, mode_line_format));
1397
1398 if (WINDOW_WANTS_HEADER_LINE_P (w))
1399 w->header_line_height
1400 = display_mode_line (w, HEADER_LINE_FACE_ID,
1401 BVAR (current_buffer, header_line_format));
1402
1403 start_display (&it, w, top);
1404 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1405 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1406
1407 if (charpos >= 0
1408 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1409 && IT_CHARPOS (it) >= charpos)
1410 /* When scanning backwards under bidi iteration, move_it_to
1411 stops at or _before_ CHARPOS, because it stops at or to
1412 the _right_ of the character at CHARPOS. */
1413 || (it.bidi_p && it.bidi_it.scan_dir == -1
1414 && IT_CHARPOS (it) <= charpos)))
1415 {
1416 /* We have reached CHARPOS, or passed it. How the call to
1417 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1418 or covered by a display property, move_it_to stops at the end
1419 of the invisible text, to the right of CHARPOS. (ii) If
1420 CHARPOS is in a display vector, move_it_to stops on its last
1421 glyph. */
1422 int top_x = it.current_x;
1423 int top_y = it.current_y;
1424 /* Calling line_bottom_y may change it.method, it.position, etc. */
1425 enum it_method it_method = it.method;
1426 int bottom_y = (last_height = 0, line_bottom_y (&it));
1427 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1428
1429 if (top_y < window_top_y)
1430 visible_p = bottom_y > window_top_y;
1431 else if (top_y < it.last_visible_y)
1432 visible_p = true;
1433 if (bottom_y >= it.last_visible_y
1434 && it.bidi_p && it.bidi_it.scan_dir == -1
1435 && IT_CHARPOS (it) < charpos)
1436 {
1437 /* When the last line of the window is scanned backwards
1438 under bidi iteration, we could be duped into thinking
1439 that we have passed CHARPOS, when in fact move_it_to
1440 simply stopped short of CHARPOS because it reached
1441 last_visible_y. To see if that's what happened, we call
1442 move_it_to again with a slightly larger vertical limit,
1443 and see if it actually moved vertically; if it did, we
1444 didn't really reach CHARPOS, which is beyond window end. */
1445 struct it save_it = it;
1446 /* Why 10? because we don't know how many canonical lines
1447 will the height of the next line(s) be. So we guess. */
1448 int ten_more_lines = 10 * default_line_pixel_height (w);
1449
1450 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1451 MOVE_TO_POS | MOVE_TO_Y);
1452 if (it.current_y > top_y)
1453 visible_p = 0;
1454
1455 it = save_it;
1456 }
1457 if (visible_p)
1458 {
1459 if (it_method == GET_FROM_DISPLAY_VECTOR)
1460 {
1461 /* We stopped on the last glyph of a display vector.
1462 Try and recompute. Hack alert! */
1463 if (charpos < 2 || top.charpos >= charpos)
1464 top_x = it.glyph_row->x;
1465 else
1466 {
1467 struct it it2, it2_prev;
1468 /* The idea is to get to the previous buffer
1469 position, consume the character there, and use
1470 the pixel coordinates we get after that. But if
1471 the previous buffer position is also displayed
1472 from a display vector, we need to consume all of
1473 the glyphs from that display vector. */
1474 start_display (&it2, w, top);
1475 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1476 /* If we didn't get to CHARPOS - 1, there's some
1477 replacing display property at that position, and
1478 we stopped after it. That is exactly the place
1479 whose coordinates we want. */
1480 if (IT_CHARPOS (it2) != charpos - 1)
1481 it2_prev = it2;
1482 else
1483 {
1484 /* Iterate until we get out of the display
1485 vector that displays the character at
1486 CHARPOS - 1. */
1487 do {
1488 get_next_display_element (&it2);
1489 PRODUCE_GLYPHS (&it2);
1490 it2_prev = it2;
1491 set_iterator_to_next (&it2, 1);
1492 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1493 && IT_CHARPOS (it2) < charpos);
1494 }
1495 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1496 || it2_prev.current_x > it2_prev.last_visible_x)
1497 top_x = it.glyph_row->x;
1498 else
1499 {
1500 top_x = it2_prev.current_x;
1501 top_y = it2_prev.current_y;
1502 }
1503 }
1504 }
1505 else if (IT_CHARPOS (it) != charpos)
1506 {
1507 Lisp_Object cpos = make_number (charpos);
1508 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1509 Lisp_Object string = string_from_display_spec (spec);
1510 struct text_pos tpos;
1511 int replacing_spec_p;
1512 bool newline_in_string
1513 = (STRINGP (string)
1514 && memchr (SDATA (string), '\n', SBYTES (string)));
1515
1516 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1517 replacing_spec_p
1518 = (!NILP (spec)
1519 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1520 charpos, FRAME_WINDOW_P (it.f)));
1521 /* The tricky code below is needed because there's a
1522 discrepancy between move_it_to and how we set cursor
1523 when PT is at the beginning of a portion of text
1524 covered by a display property or an overlay with a
1525 display property, or the display line ends in a
1526 newline from a display string. move_it_to will stop
1527 _after_ such display strings, whereas
1528 set_cursor_from_row conspires with cursor_row_p to
1529 place the cursor on the first glyph produced from the
1530 display string. */
1531
1532 /* We have overshoot PT because it is covered by a
1533 display property that replaces the text it covers.
1534 If the string includes embedded newlines, we are also
1535 in the wrong display line. Backtrack to the correct
1536 line, where the display property begins. */
1537 if (replacing_spec_p)
1538 {
1539 Lisp_Object startpos, endpos;
1540 EMACS_INT start, end;
1541 struct it it3;
1542 int it3_moved;
1543
1544 /* Find the first and the last buffer positions
1545 covered by the display string. */
1546 endpos =
1547 Fnext_single_char_property_change (cpos, Qdisplay,
1548 Qnil, Qnil);
1549 startpos =
1550 Fprevious_single_char_property_change (endpos, Qdisplay,
1551 Qnil, Qnil);
1552 start = XFASTINT (startpos);
1553 end = XFASTINT (endpos);
1554 /* Move to the last buffer position before the
1555 display property. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1558 /* Move forward one more line if the position before
1559 the display string is a newline or if it is the
1560 rightmost character on a line that is
1561 continued or word-wrapped. */
1562 if (it3.method == GET_FROM_BUFFER
1563 && (it3.c == '\n'
1564 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1565 move_it_by_lines (&it3, 1);
1566 else if (move_it_in_display_line_to (&it3, -1,
1567 it3.current_x
1568 + it3.pixel_width,
1569 MOVE_TO_X)
1570 == MOVE_LINE_CONTINUED)
1571 {
1572 move_it_by_lines (&it3, 1);
1573 /* When we are under word-wrap, the #$@%!
1574 move_it_by_lines moves 2 lines, so we need to
1575 fix that up. */
1576 if (it3.line_wrap == WORD_WRAP)
1577 move_it_by_lines (&it3, -1);
1578 }
1579
1580 /* Record the vertical coordinate of the display
1581 line where we wound up. */
1582 top_y = it3.current_y;
1583 if (it3.bidi_p)
1584 {
1585 /* When characters are reordered for display,
1586 the character displayed to the left of the
1587 display string could be _after_ the display
1588 property in the logical order. Use the
1589 smallest vertical position of these two. */
1590 start_display (&it3, w, top);
1591 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1592 if (it3.current_y < top_y)
1593 top_y = it3.current_y;
1594 }
1595 /* Move from the top of the window to the beginning
1596 of the display line where the display string
1597 begins. */
1598 start_display (&it3, w, top);
1599 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1600 /* If it3_moved stays zero after the 'while' loop
1601 below, that means we already were at a newline
1602 before the loop (e.g., the display string begins
1603 with a newline), so we don't need to (and cannot)
1604 inspect the glyphs of it3.glyph_row, because
1605 PRODUCE_GLYPHS will not produce anything for a
1606 newline, and thus it3.glyph_row stays at its
1607 stale content it got at top of the window. */
1608 it3_moved = 0;
1609 /* Finally, advance the iterator until we hit the
1610 first display element whose character position is
1611 CHARPOS, or until the first newline from the
1612 display string, which signals the end of the
1613 display line. */
1614 while (get_next_display_element (&it3))
1615 {
1616 PRODUCE_GLYPHS (&it3);
1617 if (IT_CHARPOS (it3) == charpos
1618 || ITERATOR_AT_END_OF_LINE_P (&it3))
1619 break;
1620 it3_moved = 1;
1621 set_iterator_to_next (&it3, 0);
1622 }
1623 top_x = it3.current_x - it3.pixel_width;
1624 /* Normally, we would exit the above loop because we
1625 found the display element whose character
1626 position is CHARPOS. For the contingency that we
1627 didn't, and stopped at the first newline from the
1628 display string, move back over the glyphs
1629 produced from the string, until we find the
1630 rightmost glyph not from the string. */
1631 if (it3_moved
1632 && newline_in_string
1633 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1634 {
1635 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1636 + it3.glyph_row->used[TEXT_AREA];
1637
1638 while (EQ ((g - 1)->object, string))
1639 {
1640 --g;
1641 top_x -= g->pixel_width;
1642 }
1643 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1644 + it3.glyph_row->used[TEXT_AREA]);
1645 }
1646 }
1647 }
1648
1649 *x = top_x;
1650 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1651 *rtop = max (0, window_top_y - top_y);
1652 *rbot = max (0, bottom_y - it.last_visible_y);
1653 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1654 - max (top_y, window_top_y)));
1655 *vpos = it.vpos;
1656 }
1657 }
1658 else
1659 {
1660 /* We were asked to provide info about WINDOW_END. */
1661 struct it it2;
1662 void *it2data = NULL;
1663
1664 SAVE_IT (it2, it, it2data);
1665 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1666 move_it_by_lines (&it, 1);
1667 if (charpos < IT_CHARPOS (it)
1668 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1669 {
1670 visible_p = true;
1671 RESTORE_IT (&it2, &it2, it2data);
1672 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1673 *x = it2.current_x;
1674 *y = it2.current_y + it2.max_ascent - it2.ascent;
1675 *rtop = max (0, -it2.current_y);
1676 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1677 - it.last_visible_y));
1678 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1679 it.last_visible_y)
1680 - max (it2.current_y,
1681 WINDOW_HEADER_LINE_HEIGHT (w))));
1682 *vpos = it2.vpos;
1683 }
1684 else
1685 bidi_unshelve_cache (it2data, 1);
1686 }
1687 bidi_unshelve_cache (itdata, 0);
1688
1689 if (old_buffer)
1690 set_buffer_internal_1 (old_buffer);
1691
1692 if (visible_p && w->hscroll > 0)
1693 *x -=
1694 window_hscroll_limited (w, WINDOW_XFRAME (w))
1695 * WINDOW_FRAME_COLUMN_WIDTH (w);
1696
1697 #if 0
1698 /* Debugging code. */
1699 if (visible_p)
1700 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1701 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1702 else
1703 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1704 #endif
1705
1706 return visible_p;
1707 }
1708
1709
1710 /* Return the next character from STR. Return in *LEN the length of
1711 the character. This is like STRING_CHAR_AND_LENGTH but never
1712 returns an invalid character. If we find one, we return a `?', but
1713 with the length of the invalid character. */
1714
1715 static int
1716 string_char_and_length (const unsigned char *str, int *len)
1717 {
1718 int c;
1719
1720 c = STRING_CHAR_AND_LENGTH (str, *len);
1721 if (!CHAR_VALID_P (c))
1722 /* We may not change the length here because other places in Emacs
1723 don't use this function, i.e. they silently accept invalid
1724 characters. */
1725 c = '?';
1726
1727 return c;
1728 }
1729
1730
1731
1732 /* Given a position POS containing a valid character and byte position
1733 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1734
1735 static struct text_pos
1736 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1737 {
1738 eassert (STRINGP (string) && nchars >= 0);
1739
1740 if (STRING_MULTIBYTE (string))
1741 {
1742 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1743 int len;
1744
1745 while (nchars--)
1746 {
1747 string_char_and_length (p, &len);
1748 p += len;
1749 CHARPOS (pos) += 1;
1750 BYTEPOS (pos) += len;
1751 }
1752 }
1753 else
1754 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1755
1756 return pos;
1757 }
1758
1759
1760 /* Value is the text position, i.e. character and byte position,
1761 for character position CHARPOS in STRING. */
1762
1763 static struct text_pos
1764 string_pos (ptrdiff_t charpos, Lisp_Object string)
1765 {
1766 struct text_pos pos;
1767 eassert (STRINGP (string));
1768 eassert (charpos >= 0);
1769 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1770 return pos;
1771 }
1772
1773
1774 /* Value is a text position, i.e. character and byte position, for
1775 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1776 means recognize multibyte characters. */
1777
1778 static struct text_pos
1779 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1780 {
1781 struct text_pos pos;
1782
1783 eassert (s != NULL);
1784 eassert (charpos >= 0);
1785
1786 if (multibyte_p)
1787 {
1788 int len;
1789
1790 SET_TEXT_POS (pos, 0, 0);
1791 while (charpos--)
1792 {
1793 string_char_and_length ((const unsigned char *) s, &len);
1794 s += len;
1795 CHARPOS (pos) += 1;
1796 BYTEPOS (pos) += len;
1797 }
1798 }
1799 else
1800 SET_TEXT_POS (pos, charpos, charpos);
1801
1802 return pos;
1803 }
1804
1805
1806 /* Value is the number of characters in C string S. MULTIBYTE_P
1807 non-zero means recognize multibyte characters. */
1808
1809 static ptrdiff_t
1810 number_of_chars (const char *s, bool multibyte_p)
1811 {
1812 ptrdiff_t nchars;
1813
1814 if (multibyte_p)
1815 {
1816 ptrdiff_t rest = strlen (s);
1817 int len;
1818 const unsigned char *p = (const unsigned char *) s;
1819
1820 for (nchars = 0; rest > 0; ++nchars)
1821 {
1822 string_char_and_length (p, &len);
1823 rest -= len, p += len;
1824 }
1825 }
1826 else
1827 nchars = strlen (s);
1828
1829 return nchars;
1830 }
1831
1832
1833 /* Compute byte position NEWPOS->bytepos corresponding to
1834 NEWPOS->charpos. POS is a known position in string STRING.
1835 NEWPOS->charpos must be >= POS.charpos. */
1836
1837 static void
1838 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1839 {
1840 eassert (STRINGP (string));
1841 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1842
1843 if (STRING_MULTIBYTE (string))
1844 *newpos = string_pos_nchars_ahead (pos, string,
1845 CHARPOS (*newpos) - CHARPOS (pos));
1846 else
1847 BYTEPOS (*newpos) = CHARPOS (*newpos);
1848 }
1849
1850 /* EXPORT:
1851 Return an estimation of the pixel height of mode or header lines on
1852 frame F. FACE_ID specifies what line's height to estimate. */
1853
1854 int
1855 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1856 {
1857 #ifdef HAVE_WINDOW_SYSTEM
1858 if (FRAME_WINDOW_P (f))
1859 {
1860 int height = FONT_HEIGHT (FRAME_FONT (f));
1861
1862 /* This function is called so early when Emacs starts that the face
1863 cache and mode line face are not yet initialized. */
1864 if (FRAME_FACE_CACHE (f))
1865 {
1866 struct face *face = FACE_FROM_ID (f, face_id);
1867 if (face)
1868 {
1869 if (face->font)
1870 height = FONT_HEIGHT (face->font);
1871 if (face->box_line_width > 0)
1872 height += 2 * face->box_line_width;
1873 }
1874 }
1875
1876 return height;
1877 }
1878 #endif
1879
1880 return 1;
1881 }
1882
1883 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1884 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1885 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1886 not force the value into range. */
1887
1888 void
1889 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1890 int *x, int *y, NativeRectangle *bounds, int noclip)
1891 {
1892
1893 #ifdef HAVE_WINDOW_SYSTEM
1894 if (FRAME_WINDOW_P (f))
1895 {
1896 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1897 even for negative values. */
1898 if (pix_x < 0)
1899 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1900 if (pix_y < 0)
1901 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1902
1903 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1904 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1905
1906 if (bounds)
1907 STORE_NATIVE_RECT (*bounds,
1908 FRAME_COL_TO_PIXEL_X (f, pix_x),
1909 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1910 FRAME_COLUMN_WIDTH (f) - 1,
1911 FRAME_LINE_HEIGHT (f) - 1);
1912
1913 if (!noclip)
1914 {
1915 if (pix_x < 0)
1916 pix_x = 0;
1917 else if (pix_x > FRAME_TOTAL_COLS (f))
1918 pix_x = FRAME_TOTAL_COLS (f);
1919
1920 if (pix_y < 0)
1921 pix_y = 0;
1922 else if (pix_y > FRAME_LINES (f))
1923 pix_y = FRAME_LINES (f);
1924 }
1925 }
1926 #endif
1927
1928 *x = pix_x;
1929 *y = pix_y;
1930 }
1931
1932
1933 /* Find the glyph under window-relative coordinates X/Y in window W.
1934 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1935 strings. Return in *HPOS and *VPOS the row and column number of
1936 the glyph found. Return in *AREA the glyph area containing X.
1937 Value is a pointer to the glyph found or null if X/Y is not on
1938 text, or we can't tell because W's current matrix is not up to
1939 date. */
1940
1941 static struct glyph *
1942 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1943 int *dx, int *dy, int *area)
1944 {
1945 struct glyph *glyph, *end;
1946 struct glyph_row *row = NULL;
1947 int x0, i;
1948
1949 /* Find row containing Y. Give up if some row is not enabled. */
1950 for (i = 0; i < w->current_matrix->nrows; ++i)
1951 {
1952 row = MATRIX_ROW (w->current_matrix, i);
1953 if (!row->enabled_p)
1954 return NULL;
1955 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1956 break;
1957 }
1958
1959 *vpos = i;
1960 *hpos = 0;
1961
1962 /* Give up if Y is not in the window. */
1963 if (i == w->current_matrix->nrows)
1964 return NULL;
1965
1966 /* Get the glyph area containing X. */
1967 if (w->pseudo_window_p)
1968 {
1969 *area = TEXT_AREA;
1970 x0 = 0;
1971 }
1972 else
1973 {
1974 if (x < window_box_left_offset (w, TEXT_AREA))
1975 {
1976 *area = LEFT_MARGIN_AREA;
1977 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1978 }
1979 else if (x < window_box_right_offset (w, TEXT_AREA))
1980 {
1981 *area = TEXT_AREA;
1982 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1983 }
1984 else
1985 {
1986 *area = RIGHT_MARGIN_AREA;
1987 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1988 }
1989 }
1990
1991 /* Find glyph containing X. */
1992 glyph = row->glyphs[*area];
1993 end = glyph + row->used[*area];
1994 x -= x0;
1995 while (glyph < end && x >= glyph->pixel_width)
1996 {
1997 x -= glyph->pixel_width;
1998 ++glyph;
1999 }
2000
2001 if (glyph == end)
2002 return NULL;
2003
2004 if (dx)
2005 {
2006 *dx = x;
2007 *dy = y - (row->y + row->ascent - glyph->ascent);
2008 }
2009
2010 *hpos = glyph - row->glyphs[*area];
2011 return glyph;
2012 }
2013
2014 /* Convert frame-relative x/y to coordinates relative to window W.
2015 Takes pseudo-windows into account. */
2016
2017 static void
2018 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2019 {
2020 if (w->pseudo_window_p)
2021 {
2022 /* A pseudo-window is always full-width, and starts at the
2023 left edge of the frame, plus a frame border. */
2024 struct frame *f = XFRAME (w->frame);
2025 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2026 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2027 }
2028 else
2029 {
2030 *x -= WINDOW_LEFT_EDGE_X (w);
2031 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2032 }
2033 }
2034
2035 #ifdef HAVE_WINDOW_SYSTEM
2036
2037 /* EXPORT:
2038 Return in RECTS[] at most N clipping rectangles for glyph string S.
2039 Return the number of stored rectangles. */
2040
2041 int
2042 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2043 {
2044 XRectangle r;
2045
2046 if (n <= 0)
2047 return 0;
2048
2049 if (s->row->full_width_p)
2050 {
2051 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2052 r.x = WINDOW_LEFT_EDGE_X (s->w);
2053 r.width = WINDOW_TOTAL_WIDTH (s->w);
2054
2055 /* Unless displaying a mode or menu bar line, which are always
2056 fully visible, clip to the visible part of the row. */
2057 if (s->w->pseudo_window_p)
2058 r.height = s->row->visible_height;
2059 else
2060 r.height = s->height;
2061 }
2062 else
2063 {
2064 /* This is a text line that may be partially visible. */
2065 r.x = window_box_left (s->w, s->area);
2066 r.width = window_box_width (s->w, s->area);
2067 r.height = s->row->visible_height;
2068 }
2069
2070 if (s->clip_head)
2071 if (r.x < s->clip_head->x)
2072 {
2073 if (r.width >= s->clip_head->x - r.x)
2074 r.width -= s->clip_head->x - r.x;
2075 else
2076 r.width = 0;
2077 r.x = s->clip_head->x;
2078 }
2079 if (s->clip_tail)
2080 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2081 {
2082 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2083 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2084 else
2085 r.width = 0;
2086 }
2087
2088 /* If S draws overlapping rows, it's sufficient to use the top and
2089 bottom of the window for clipping because this glyph string
2090 intentionally draws over other lines. */
2091 if (s->for_overlaps)
2092 {
2093 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2094 r.height = window_text_bottom_y (s->w) - r.y;
2095
2096 /* Alas, the above simple strategy does not work for the
2097 environments with anti-aliased text: if the same text is
2098 drawn onto the same place multiple times, it gets thicker.
2099 If the overlap we are processing is for the erased cursor, we
2100 take the intersection with the rectangle of the cursor. */
2101 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2102 {
2103 XRectangle rc, r_save = r;
2104
2105 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2106 rc.y = s->w->phys_cursor.y;
2107 rc.width = s->w->phys_cursor_width;
2108 rc.height = s->w->phys_cursor_height;
2109
2110 x_intersect_rectangles (&r_save, &rc, &r);
2111 }
2112 }
2113 else
2114 {
2115 /* Don't use S->y for clipping because it doesn't take partially
2116 visible lines into account. For example, it can be negative for
2117 partially visible lines at the top of a window. */
2118 if (!s->row->full_width_p
2119 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2120 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2121 else
2122 r.y = max (0, s->row->y);
2123 }
2124
2125 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2126
2127 /* If drawing the cursor, don't let glyph draw outside its
2128 advertised boundaries. Cleartype does this under some circumstances. */
2129 if (s->hl == DRAW_CURSOR)
2130 {
2131 struct glyph *glyph = s->first_glyph;
2132 int height, max_y;
2133
2134 if (s->x > r.x)
2135 {
2136 r.width -= s->x - r.x;
2137 r.x = s->x;
2138 }
2139 r.width = min (r.width, glyph->pixel_width);
2140
2141 /* If r.y is below window bottom, ensure that we still see a cursor. */
2142 height = min (glyph->ascent + glyph->descent,
2143 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2144 max_y = window_text_bottom_y (s->w) - height;
2145 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2146 if (s->ybase - glyph->ascent > max_y)
2147 {
2148 r.y = max_y;
2149 r.height = height;
2150 }
2151 else
2152 {
2153 /* Don't draw cursor glyph taller than our actual glyph. */
2154 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2155 if (height < r.height)
2156 {
2157 max_y = r.y + r.height;
2158 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2159 r.height = min (max_y - r.y, height);
2160 }
2161 }
2162 }
2163
2164 if (s->row->clip)
2165 {
2166 XRectangle r_save = r;
2167
2168 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2169 r.width = 0;
2170 }
2171
2172 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2173 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2174 {
2175 #ifdef CONVERT_FROM_XRECT
2176 CONVERT_FROM_XRECT (r, *rects);
2177 #else
2178 *rects = r;
2179 #endif
2180 return 1;
2181 }
2182 else
2183 {
2184 /* If we are processing overlapping and allowed to return
2185 multiple clipping rectangles, we exclude the row of the glyph
2186 string from the clipping rectangle. This is to avoid drawing
2187 the same text on the environment with anti-aliasing. */
2188 #ifdef CONVERT_FROM_XRECT
2189 XRectangle rs[2];
2190 #else
2191 XRectangle *rs = rects;
2192 #endif
2193 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2194
2195 if (s->for_overlaps & OVERLAPS_PRED)
2196 {
2197 rs[i] = r;
2198 if (r.y + r.height > row_y)
2199 {
2200 if (r.y < row_y)
2201 rs[i].height = row_y - r.y;
2202 else
2203 rs[i].height = 0;
2204 }
2205 i++;
2206 }
2207 if (s->for_overlaps & OVERLAPS_SUCC)
2208 {
2209 rs[i] = r;
2210 if (r.y < row_y + s->row->visible_height)
2211 {
2212 if (r.y + r.height > row_y + s->row->visible_height)
2213 {
2214 rs[i].y = row_y + s->row->visible_height;
2215 rs[i].height = r.y + r.height - rs[i].y;
2216 }
2217 else
2218 rs[i].height = 0;
2219 }
2220 i++;
2221 }
2222
2223 n = i;
2224 #ifdef CONVERT_FROM_XRECT
2225 for (i = 0; i < n; i++)
2226 CONVERT_FROM_XRECT (rs[i], rects[i]);
2227 #endif
2228 return n;
2229 }
2230 }
2231
2232 /* EXPORT:
2233 Return in *NR the clipping rectangle for glyph string S. */
2234
2235 void
2236 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2237 {
2238 get_glyph_string_clip_rects (s, nr, 1);
2239 }
2240
2241
2242 /* EXPORT:
2243 Return the position and height of the phys cursor in window W.
2244 Set w->phys_cursor_width to width of phys cursor.
2245 */
2246
2247 void
2248 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2249 struct glyph *glyph, int *xp, int *yp, int *heightp)
2250 {
2251 struct frame *f = XFRAME (WINDOW_FRAME (w));
2252 int x, y, wd, h, h0, y0;
2253
2254 /* Compute the width of the rectangle to draw. If on a stretch
2255 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2256 rectangle as wide as the glyph, but use a canonical character
2257 width instead. */
2258 wd = glyph->pixel_width - 1;
2259 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2260 wd++; /* Why? */
2261 #endif
2262
2263 x = w->phys_cursor.x;
2264 if (x < 0)
2265 {
2266 wd += x;
2267 x = 0;
2268 }
2269
2270 if (glyph->type == STRETCH_GLYPH
2271 && !x_stretch_cursor_p)
2272 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2273 w->phys_cursor_width = wd;
2274
2275 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2276
2277 /* If y is below window bottom, ensure that we still see a cursor. */
2278 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2279
2280 h = max (h0, glyph->ascent + glyph->descent);
2281 h0 = min (h0, glyph->ascent + glyph->descent);
2282
2283 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2284 if (y < y0)
2285 {
2286 h = max (h - (y0 - y) + 1, h0);
2287 y = y0 - 1;
2288 }
2289 else
2290 {
2291 y0 = window_text_bottom_y (w) - h0;
2292 if (y > y0)
2293 {
2294 h += y - y0;
2295 y = y0;
2296 }
2297 }
2298
2299 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2300 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2301 *heightp = h;
2302 }
2303
2304 /*
2305 * Remember which glyph the mouse is over.
2306 */
2307
2308 void
2309 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2310 {
2311 Lisp_Object window;
2312 struct window *w;
2313 struct glyph_row *r, *gr, *end_row;
2314 enum window_part part;
2315 enum glyph_row_area area;
2316 int x, y, width, height;
2317
2318 /* Try to determine frame pixel position and size of the glyph under
2319 frame pixel coordinates X/Y on frame F. */
2320
2321 if (!f->glyphs_initialized_p
2322 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2323 NILP (window)))
2324 {
2325 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2326 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2327 goto virtual_glyph;
2328 }
2329
2330 w = XWINDOW (window);
2331 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2332 height = WINDOW_FRAME_LINE_HEIGHT (w);
2333
2334 x = window_relative_x_coord (w, part, gx);
2335 y = gy - WINDOW_TOP_EDGE_Y (w);
2336
2337 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2338 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2339
2340 if (w->pseudo_window_p)
2341 {
2342 area = TEXT_AREA;
2343 part = ON_MODE_LINE; /* Don't adjust margin. */
2344 goto text_glyph;
2345 }
2346
2347 switch (part)
2348 {
2349 case ON_LEFT_MARGIN:
2350 area = LEFT_MARGIN_AREA;
2351 goto text_glyph;
2352
2353 case ON_RIGHT_MARGIN:
2354 area = RIGHT_MARGIN_AREA;
2355 goto text_glyph;
2356
2357 case ON_HEADER_LINE:
2358 case ON_MODE_LINE:
2359 gr = (part == ON_HEADER_LINE
2360 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2361 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2362 gy = gr->y;
2363 area = TEXT_AREA;
2364 goto text_glyph_row_found;
2365
2366 case ON_TEXT:
2367 area = TEXT_AREA;
2368
2369 text_glyph:
2370 gr = 0; gy = 0;
2371 for (; r <= end_row && r->enabled_p; ++r)
2372 if (r->y + r->height > y)
2373 {
2374 gr = r; gy = r->y;
2375 break;
2376 }
2377
2378 text_glyph_row_found:
2379 if (gr && gy <= y)
2380 {
2381 struct glyph *g = gr->glyphs[area];
2382 struct glyph *end = g + gr->used[area];
2383
2384 height = gr->height;
2385 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2386 if (gx + g->pixel_width > x)
2387 break;
2388
2389 if (g < end)
2390 {
2391 if (g->type == IMAGE_GLYPH)
2392 {
2393 /* Don't remember when mouse is over image, as
2394 image may have hot-spots. */
2395 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2396 return;
2397 }
2398 width = g->pixel_width;
2399 }
2400 else
2401 {
2402 /* Use nominal char spacing at end of line. */
2403 x -= gx;
2404 gx += (x / width) * width;
2405 }
2406
2407 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2408 gx += window_box_left_offset (w, area);
2409 }
2410 else
2411 {
2412 /* Use nominal line height at end of window. */
2413 gx = (x / width) * width;
2414 y -= gy;
2415 gy += (y / height) * height;
2416 }
2417 break;
2418
2419 case ON_LEFT_FRINGE:
2420 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2421 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2422 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2423 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2424 goto row_glyph;
2425
2426 case ON_RIGHT_FRINGE:
2427 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2428 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2429 : window_box_right_offset (w, TEXT_AREA));
2430 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2431 goto row_glyph;
2432
2433 case ON_SCROLL_BAR:
2434 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2435 ? 0
2436 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2437 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2438 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2439 : 0)));
2440 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2441
2442 row_glyph:
2443 gr = 0, gy = 0;
2444 for (; r <= end_row && r->enabled_p; ++r)
2445 if (r->y + r->height > y)
2446 {
2447 gr = r; gy = r->y;
2448 break;
2449 }
2450
2451 if (gr && gy <= y)
2452 height = gr->height;
2453 else
2454 {
2455 /* Use nominal line height at end of window. */
2456 y -= gy;
2457 gy += (y / height) * height;
2458 }
2459 break;
2460
2461 default:
2462 ;
2463 virtual_glyph:
2464 /* If there is no glyph under the mouse, then we divide the screen
2465 into a grid of the smallest glyph in the frame, and use that
2466 as our "glyph". */
2467
2468 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2469 round down even for negative values. */
2470 if (gx < 0)
2471 gx -= width - 1;
2472 if (gy < 0)
2473 gy -= height - 1;
2474
2475 gx = (gx / width) * width;
2476 gy = (gy / height) * height;
2477
2478 goto store_rect;
2479 }
2480
2481 gx += WINDOW_LEFT_EDGE_X (w);
2482 gy += WINDOW_TOP_EDGE_Y (w);
2483
2484 store_rect:
2485 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2486
2487 /* Visible feedback for debugging. */
2488 #if 0
2489 #if HAVE_X_WINDOWS
2490 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2491 f->output_data.x->normal_gc,
2492 gx, gy, width, height);
2493 #endif
2494 #endif
2495 }
2496
2497
2498 #endif /* HAVE_WINDOW_SYSTEM */
2499
2500 static void
2501 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2502 {
2503 eassert (w);
2504 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2505 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2506 w->window_end_vpos
2507 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2508 }
2509
2510 /***********************************************************************
2511 Lisp form evaluation
2512 ***********************************************************************/
2513
2514 /* Error handler for safe_eval and safe_call. */
2515
2516 static Lisp_Object
2517 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2518 {
2519 add_to_log ("Error during redisplay: %S signaled %S",
2520 Flist (nargs, args), arg);
2521 return Qnil;
2522 }
2523
2524 /* Call function FUNC with the rest of NARGS - 1 arguments
2525 following. Return the result, or nil if something went
2526 wrong. Prevent redisplay during the evaluation. */
2527
2528 Lisp_Object
2529 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2530 {
2531 Lisp_Object val;
2532
2533 if (inhibit_eval_during_redisplay)
2534 val = Qnil;
2535 else
2536 {
2537 va_list ap;
2538 ptrdiff_t i;
2539 ptrdiff_t count = SPECPDL_INDEX ();
2540 struct gcpro gcpro1;
2541 Lisp_Object *args = alloca (nargs * word_size);
2542
2543 args[0] = func;
2544 va_start (ap, func);
2545 for (i = 1; i < nargs; i++)
2546 args[i] = va_arg (ap, Lisp_Object);
2547 va_end (ap);
2548
2549 GCPRO1 (args[0]);
2550 gcpro1.nvars = nargs;
2551 specbind (Qinhibit_redisplay, Qt);
2552 /* Use Qt to ensure debugger does not run,
2553 so there is no possibility of wanting to redisplay. */
2554 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2555 safe_eval_handler);
2556 UNGCPRO;
2557 val = unbind_to (count, val);
2558 }
2559
2560 return val;
2561 }
2562
2563
2564 /* Call function FN with one argument ARG.
2565 Return the result, or nil if something went wrong. */
2566
2567 Lisp_Object
2568 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2569 {
2570 return safe_call (2, fn, arg);
2571 }
2572
2573 static Lisp_Object Qeval;
2574
2575 Lisp_Object
2576 safe_eval (Lisp_Object sexpr)
2577 {
2578 return safe_call1 (Qeval, sexpr);
2579 }
2580
2581 /* Call function FN with two arguments ARG1 and ARG2.
2582 Return the result, or nil if something went wrong. */
2583
2584 Lisp_Object
2585 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2586 {
2587 return safe_call (3, fn, arg1, arg2);
2588 }
2589
2590
2591 \f
2592 /***********************************************************************
2593 Debugging
2594 ***********************************************************************/
2595
2596 #if 0
2597
2598 /* Define CHECK_IT to perform sanity checks on iterators.
2599 This is for debugging. It is too slow to do unconditionally. */
2600
2601 static void
2602 check_it (struct it *it)
2603 {
2604 if (it->method == GET_FROM_STRING)
2605 {
2606 eassert (STRINGP (it->string));
2607 eassert (IT_STRING_CHARPOS (*it) >= 0);
2608 }
2609 else
2610 {
2611 eassert (IT_STRING_CHARPOS (*it) < 0);
2612 if (it->method == GET_FROM_BUFFER)
2613 {
2614 /* Check that character and byte positions agree. */
2615 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2616 }
2617 }
2618
2619 if (it->dpvec)
2620 eassert (it->current.dpvec_index >= 0);
2621 else
2622 eassert (it->current.dpvec_index < 0);
2623 }
2624
2625 #define CHECK_IT(IT) check_it ((IT))
2626
2627 #else /* not 0 */
2628
2629 #define CHECK_IT(IT) (void) 0
2630
2631 #endif /* not 0 */
2632
2633
2634 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2635
2636 /* Check that the window end of window W is what we expect it
2637 to be---the last row in the current matrix displaying text. */
2638
2639 static void
2640 check_window_end (struct window *w)
2641 {
2642 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2643 {
2644 struct glyph_row *row;
2645 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2646 !row->enabled_p
2647 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2648 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2649 }
2650 }
2651
2652 #define CHECK_WINDOW_END(W) check_window_end ((W))
2653
2654 #else
2655
2656 #define CHECK_WINDOW_END(W) (void) 0
2657
2658 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2659
2660 /***********************************************************************
2661 Iterator initialization
2662 ***********************************************************************/
2663
2664 /* Initialize IT for displaying current_buffer in window W, starting
2665 at character position CHARPOS. CHARPOS < 0 means that no buffer
2666 position is specified which is useful when the iterator is assigned
2667 a position later. BYTEPOS is the byte position corresponding to
2668 CHARPOS.
2669
2670 If ROW is not null, calls to produce_glyphs with IT as parameter
2671 will produce glyphs in that row.
2672
2673 BASE_FACE_ID is the id of a base face to use. It must be one of
2674 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2675 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2676 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2677
2678 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2679 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2680 will be initialized to use the corresponding mode line glyph row of
2681 the desired matrix of W. */
2682
2683 void
2684 init_iterator (struct it *it, struct window *w,
2685 ptrdiff_t charpos, ptrdiff_t bytepos,
2686 struct glyph_row *row, enum face_id base_face_id)
2687 {
2688 enum face_id remapped_base_face_id = base_face_id;
2689
2690 /* Some precondition checks. */
2691 eassert (w != NULL && it != NULL);
2692 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2693 && charpos <= ZV));
2694
2695 /* If face attributes have been changed since the last redisplay,
2696 free realized faces now because they depend on face definitions
2697 that might have changed. Don't free faces while there might be
2698 desired matrices pending which reference these faces. */
2699 if (face_change_count && !inhibit_free_realized_faces)
2700 {
2701 face_change_count = 0;
2702 free_all_realized_faces (Qnil);
2703 }
2704
2705 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2706 if (! NILP (Vface_remapping_alist))
2707 remapped_base_face_id
2708 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2709
2710 /* Use one of the mode line rows of W's desired matrix if
2711 appropriate. */
2712 if (row == NULL)
2713 {
2714 if (base_face_id == MODE_LINE_FACE_ID
2715 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2716 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2717 else if (base_face_id == HEADER_LINE_FACE_ID)
2718 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2719 }
2720
2721 /* Clear IT. */
2722 memset (it, 0, sizeof *it);
2723 it->current.overlay_string_index = -1;
2724 it->current.dpvec_index = -1;
2725 it->base_face_id = remapped_base_face_id;
2726 it->string = Qnil;
2727 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2728 it->paragraph_embedding = L2R;
2729 it->bidi_it.string.lstring = Qnil;
2730 it->bidi_it.string.s = NULL;
2731 it->bidi_it.string.bufpos = 0;
2732 it->bidi_it.w = w;
2733
2734 /* The window in which we iterate over current_buffer: */
2735 XSETWINDOW (it->window, w);
2736 it->w = w;
2737 it->f = XFRAME (w->frame);
2738
2739 it->cmp_it.id = -1;
2740
2741 /* Extra space between lines (on window systems only). */
2742 if (base_face_id == DEFAULT_FACE_ID
2743 && FRAME_WINDOW_P (it->f))
2744 {
2745 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2746 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2747 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2748 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2749 * FRAME_LINE_HEIGHT (it->f));
2750 else if (it->f->extra_line_spacing > 0)
2751 it->extra_line_spacing = it->f->extra_line_spacing;
2752 it->max_extra_line_spacing = 0;
2753 }
2754
2755 /* If realized faces have been removed, e.g. because of face
2756 attribute changes of named faces, recompute them. When running
2757 in batch mode, the face cache of the initial frame is null. If
2758 we happen to get called, make a dummy face cache. */
2759 if (FRAME_FACE_CACHE (it->f) == NULL)
2760 init_frame_faces (it->f);
2761 if (FRAME_FACE_CACHE (it->f)->used == 0)
2762 recompute_basic_faces (it->f);
2763
2764 /* Current value of the `slice', `space-width', and 'height' properties. */
2765 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2766 it->space_width = Qnil;
2767 it->font_height = Qnil;
2768 it->override_ascent = -1;
2769
2770 /* Are control characters displayed as `^C'? */
2771 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2772
2773 /* -1 means everything between a CR and the following line end
2774 is invisible. >0 means lines indented more than this value are
2775 invisible. */
2776 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2777 ? (clip_to_bounds
2778 (-1, XINT (BVAR (current_buffer, selective_display)),
2779 PTRDIFF_MAX))
2780 : (!NILP (BVAR (current_buffer, selective_display))
2781 ? -1 : 0));
2782 it->selective_display_ellipsis_p
2783 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2784
2785 /* Display table to use. */
2786 it->dp = window_display_table (w);
2787
2788 /* Are multibyte characters enabled in current_buffer? */
2789 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2790
2791 /* Get the position at which the redisplay_end_trigger hook should
2792 be run, if it is to be run at all. */
2793 if (MARKERP (w->redisplay_end_trigger)
2794 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2795 it->redisplay_end_trigger_charpos
2796 = marker_position (w->redisplay_end_trigger);
2797 else if (INTEGERP (w->redisplay_end_trigger))
2798 it->redisplay_end_trigger_charpos =
2799 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2800
2801 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2802
2803 /* Are lines in the display truncated? */
2804 if (base_face_id != DEFAULT_FACE_ID
2805 || it->w->hscroll
2806 || (! WINDOW_FULL_WIDTH_P (it->w)
2807 && ((!NILP (Vtruncate_partial_width_windows)
2808 && !INTEGERP (Vtruncate_partial_width_windows))
2809 || (INTEGERP (Vtruncate_partial_width_windows)
2810 && (WINDOW_TOTAL_COLS (it->w)
2811 < XINT (Vtruncate_partial_width_windows))))))
2812 it->line_wrap = TRUNCATE;
2813 else if (NILP (BVAR (current_buffer, truncate_lines)))
2814 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2815 ? WINDOW_WRAP : WORD_WRAP;
2816 else
2817 it->line_wrap = TRUNCATE;
2818
2819 /* Get dimensions of truncation and continuation glyphs. These are
2820 displayed as fringe bitmaps under X, but we need them for such
2821 frames when the fringes are turned off. But leave the dimensions
2822 zero for tooltip frames, as these glyphs look ugly there and also
2823 sabotage calculations of tooltip dimensions in x-show-tip. */
2824 #ifdef HAVE_WINDOW_SYSTEM
2825 if (!(FRAME_WINDOW_P (it->f)
2826 && FRAMEP (tip_frame)
2827 && it->f == XFRAME (tip_frame)))
2828 #endif
2829 {
2830 if (it->line_wrap == TRUNCATE)
2831 {
2832 /* We will need the truncation glyph. */
2833 eassert (it->glyph_row == NULL);
2834 produce_special_glyphs (it, IT_TRUNCATION);
2835 it->truncation_pixel_width = it->pixel_width;
2836 }
2837 else
2838 {
2839 /* We will need the continuation glyph. */
2840 eassert (it->glyph_row == NULL);
2841 produce_special_glyphs (it, IT_CONTINUATION);
2842 it->continuation_pixel_width = it->pixel_width;
2843 }
2844 }
2845
2846 /* Reset these values to zero because the produce_special_glyphs
2847 above has changed them. */
2848 it->pixel_width = it->ascent = it->descent = 0;
2849 it->phys_ascent = it->phys_descent = 0;
2850
2851 /* Set this after getting the dimensions of truncation and
2852 continuation glyphs, so that we don't produce glyphs when calling
2853 produce_special_glyphs, above. */
2854 it->glyph_row = row;
2855 it->area = TEXT_AREA;
2856
2857 /* Forget any previous info about this row being reversed. */
2858 if (it->glyph_row)
2859 it->glyph_row->reversed_p = 0;
2860
2861 /* Get the dimensions of the display area. The display area
2862 consists of the visible window area plus a horizontally scrolled
2863 part to the left of the window. All x-values are relative to the
2864 start of this total display area. */
2865 if (base_face_id != DEFAULT_FACE_ID)
2866 {
2867 /* Mode lines, menu bar in terminal frames. */
2868 it->first_visible_x = 0;
2869 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2870 }
2871 else
2872 {
2873 it->first_visible_x
2874 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2875 it->last_visible_x = (it->first_visible_x
2876 + window_box_width (w, TEXT_AREA));
2877
2878 /* If we truncate lines, leave room for the truncation glyph(s) at
2879 the right margin. Otherwise, leave room for the continuation
2880 glyph(s). Done only if the window has no fringes. Since we
2881 don't know at this point whether there will be any R2L lines in
2882 the window, we reserve space for truncation/continuation glyphs
2883 even if only one of the fringes is absent. */
2884 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2885 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2886 {
2887 if (it->line_wrap == TRUNCATE)
2888 it->last_visible_x -= it->truncation_pixel_width;
2889 else
2890 it->last_visible_x -= it->continuation_pixel_width;
2891 }
2892
2893 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2894 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2895 }
2896
2897 /* Leave room for a border glyph. */
2898 if (!FRAME_WINDOW_P (it->f)
2899 && !WINDOW_RIGHTMOST_P (it->w))
2900 it->last_visible_x -= 1;
2901
2902 it->last_visible_y = window_text_bottom_y (w);
2903
2904 /* For mode lines and alike, arrange for the first glyph having a
2905 left box line if the face specifies a box. */
2906 if (base_face_id != DEFAULT_FACE_ID)
2907 {
2908 struct face *face;
2909
2910 it->face_id = remapped_base_face_id;
2911
2912 /* If we have a boxed mode line, make the first character appear
2913 with a left box line. */
2914 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2915 if (face->box != FACE_NO_BOX)
2916 it->start_of_box_run_p = true;
2917 }
2918
2919 /* If a buffer position was specified, set the iterator there,
2920 getting overlays and face properties from that position. */
2921 if (charpos >= BUF_BEG (current_buffer))
2922 {
2923 it->end_charpos = ZV;
2924 eassert (charpos == BYTE_TO_CHAR (bytepos));
2925 IT_CHARPOS (*it) = charpos;
2926 IT_BYTEPOS (*it) = bytepos;
2927
2928 /* We will rely on `reseat' to set this up properly, via
2929 handle_face_prop. */
2930 it->face_id = it->base_face_id;
2931
2932 it->start = it->current;
2933 /* Do we need to reorder bidirectional text? Not if this is a
2934 unibyte buffer: by definition, none of the single-byte
2935 characters are strong R2L, so no reordering is needed. And
2936 bidi.c doesn't support unibyte buffers anyway. Also, don't
2937 reorder while we are loading loadup.el, since the tables of
2938 character properties needed for reordering are not yet
2939 available. */
2940 it->bidi_p =
2941 NILP (Vpurify_flag)
2942 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2943 && it->multibyte_p;
2944
2945 /* If we are to reorder bidirectional text, init the bidi
2946 iterator. */
2947 if (it->bidi_p)
2948 {
2949 /* Note the paragraph direction that this buffer wants to
2950 use. */
2951 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2952 Qleft_to_right))
2953 it->paragraph_embedding = L2R;
2954 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2955 Qright_to_left))
2956 it->paragraph_embedding = R2L;
2957 else
2958 it->paragraph_embedding = NEUTRAL_DIR;
2959 bidi_unshelve_cache (NULL, 0);
2960 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2961 &it->bidi_it);
2962 }
2963
2964 /* Compute faces etc. */
2965 reseat (it, it->current.pos, 1);
2966 }
2967
2968 CHECK_IT (it);
2969 }
2970
2971
2972 /* Initialize IT for the display of window W with window start POS. */
2973
2974 void
2975 start_display (struct it *it, struct window *w, struct text_pos pos)
2976 {
2977 struct glyph_row *row;
2978 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2979
2980 row = w->desired_matrix->rows + first_vpos;
2981 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2982 it->first_vpos = first_vpos;
2983
2984 /* Don't reseat to previous visible line start if current start
2985 position is in a string or image. */
2986 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2987 {
2988 int start_at_line_beg_p;
2989 int first_y = it->current_y;
2990
2991 /* If window start is not at a line start, skip forward to POS to
2992 get the correct continuation lines width. */
2993 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2994 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2995 if (!start_at_line_beg_p)
2996 {
2997 int new_x;
2998
2999 reseat_at_previous_visible_line_start (it);
3000 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3001
3002 new_x = it->current_x + it->pixel_width;
3003
3004 /* If lines are continued, this line may end in the middle
3005 of a multi-glyph character (e.g. a control character
3006 displayed as \003, or in the middle of an overlay
3007 string). In this case move_it_to above will not have
3008 taken us to the start of the continuation line but to the
3009 end of the continued line. */
3010 if (it->current_x > 0
3011 && it->line_wrap != TRUNCATE /* Lines are continued. */
3012 && (/* And glyph doesn't fit on the line. */
3013 new_x > it->last_visible_x
3014 /* Or it fits exactly and we're on a window
3015 system frame. */
3016 || (new_x == it->last_visible_x
3017 && FRAME_WINDOW_P (it->f)
3018 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3019 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3020 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3021 {
3022 if ((it->current.dpvec_index >= 0
3023 || it->current.overlay_string_index >= 0)
3024 /* If we are on a newline from a display vector or
3025 overlay string, then we are already at the end of
3026 a screen line; no need to go to the next line in
3027 that case, as this line is not really continued.
3028 (If we do go to the next line, C-e will not DTRT.) */
3029 && it->c != '\n')
3030 {
3031 set_iterator_to_next (it, 1);
3032 move_it_in_display_line_to (it, -1, -1, 0);
3033 }
3034
3035 it->continuation_lines_width += it->current_x;
3036 }
3037 /* If the character at POS is displayed via a display
3038 vector, move_it_to above stops at the final glyph of
3039 IT->dpvec. To make the caller redisplay that character
3040 again (a.k.a. start at POS), we need to reset the
3041 dpvec_index to the beginning of IT->dpvec. */
3042 else if (it->current.dpvec_index >= 0)
3043 it->current.dpvec_index = 0;
3044
3045 /* We're starting a new display line, not affected by the
3046 height of the continued line, so clear the appropriate
3047 fields in the iterator structure. */
3048 it->max_ascent = it->max_descent = 0;
3049 it->max_phys_ascent = it->max_phys_descent = 0;
3050
3051 it->current_y = first_y;
3052 it->vpos = 0;
3053 it->current_x = it->hpos = 0;
3054 }
3055 }
3056 }
3057
3058
3059 /* Return 1 if POS is a position in ellipses displayed for invisible
3060 text. W is the window we display, for text property lookup. */
3061
3062 static int
3063 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3064 {
3065 Lisp_Object prop, window;
3066 int ellipses_p = 0;
3067 ptrdiff_t charpos = CHARPOS (pos->pos);
3068
3069 /* If POS specifies a position in a display vector, this might
3070 be for an ellipsis displayed for invisible text. We won't
3071 get the iterator set up for delivering that ellipsis unless
3072 we make sure that it gets aware of the invisible text. */
3073 if (pos->dpvec_index >= 0
3074 && pos->overlay_string_index < 0
3075 && CHARPOS (pos->string_pos) < 0
3076 && charpos > BEGV
3077 && (XSETWINDOW (window, w),
3078 prop = Fget_char_property (make_number (charpos),
3079 Qinvisible, window),
3080 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3081 {
3082 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3083 window);
3084 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3085 }
3086
3087 return ellipses_p;
3088 }
3089
3090
3091 /* Initialize IT for stepping through current_buffer in window W,
3092 starting at position POS that includes overlay string and display
3093 vector/ control character translation position information. Value
3094 is zero if there are overlay strings with newlines at POS. */
3095
3096 static int
3097 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3098 {
3099 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3100 int i, overlay_strings_with_newlines = 0;
3101
3102 /* If POS specifies a position in a display vector, this might
3103 be for an ellipsis displayed for invisible text. We won't
3104 get the iterator set up for delivering that ellipsis unless
3105 we make sure that it gets aware of the invisible text. */
3106 if (in_ellipses_for_invisible_text_p (pos, w))
3107 {
3108 --charpos;
3109 bytepos = 0;
3110 }
3111
3112 /* Keep in mind: the call to reseat in init_iterator skips invisible
3113 text, so we might end up at a position different from POS. This
3114 is only a problem when POS is a row start after a newline and an
3115 overlay starts there with an after-string, and the overlay has an
3116 invisible property. Since we don't skip invisible text in
3117 display_line and elsewhere immediately after consuming the
3118 newline before the row start, such a POS will not be in a string,
3119 but the call to init_iterator below will move us to the
3120 after-string. */
3121 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3122
3123 /* This only scans the current chunk -- it should scan all chunks.
3124 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3125 to 16 in 22.1 to make this a lesser problem. */
3126 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3127 {
3128 const char *s = SSDATA (it->overlay_strings[i]);
3129 const char *e = s + SBYTES (it->overlay_strings[i]);
3130
3131 while (s < e && *s != '\n')
3132 ++s;
3133
3134 if (s < e)
3135 {
3136 overlay_strings_with_newlines = 1;
3137 break;
3138 }
3139 }
3140
3141 /* If position is within an overlay string, set up IT to the right
3142 overlay string. */
3143 if (pos->overlay_string_index >= 0)
3144 {
3145 int relative_index;
3146
3147 /* If the first overlay string happens to have a `display'
3148 property for an image, the iterator will be set up for that
3149 image, and we have to undo that setup first before we can
3150 correct the overlay string index. */
3151 if (it->method == GET_FROM_IMAGE)
3152 pop_it (it);
3153
3154 /* We already have the first chunk of overlay strings in
3155 IT->overlay_strings. Load more until the one for
3156 pos->overlay_string_index is in IT->overlay_strings. */
3157 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3158 {
3159 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3160 it->current.overlay_string_index = 0;
3161 while (n--)
3162 {
3163 load_overlay_strings (it, 0);
3164 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3165 }
3166 }
3167
3168 it->current.overlay_string_index = pos->overlay_string_index;
3169 relative_index = (it->current.overlay_string_index
3170 % OVERLAY_STRING_CHUNK_SIZE);
3171 it->string = it->overlay_strings[relative_index];
3172 eassert (STRINGP (it->string));
3173 it->current.string_pos = pos->string_pos;
3174 it->method = GET_FROM_STRING;
3175 it->end_charpos = SCHARS (it->string);
3176 /* Set up the bidi iterator for this overlay string. */
3177 if (it->bidi_p)
3178 {
3179 it->bidi_it.string.lstring = it->string;
3180 it->bidi_it.string.s = NULL;
3181 it->bidi_it.string.schars = SCHARS (it->string);
3182 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3183 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3184 it->bidi_it.string.unibyte = !it->multibyte_p;
3185 it->bidi_it.w = it->w;
3186 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3187 FRAME_WINDOW_P (it->f), &it->bidi_it);
3188
3189 /* Synchronize the state of the bidi iterator with
3190 pos->string_pos. For any string position other than
3191 zero, this will be done automagically when we resume
3192 iteration over the string and get_visually_first_element
3193 is called. But if string_pos is zero, and the string is
3194 to be reordered for display, we need to resync manually,
3195 since it could be that the iteration state recorded in
3196 pos ended at string_pos of 0 moving backwards in string. */
3197 if (CHARPOS (pos->string_pos) == 0)
3198 {
3199 get_visually_first_element (it);
3200 if (IT_STRING_CHARPOS (*it) != 0)
3201 do {
3202 /* Paranoia. */
3203 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3204 bidi_move_to_visually_next (&it->bidi_it);
3205 } while (it->bidi_it.charpos != 0);
3206 }
3207 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3208 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3209 }
3210 }
3211
3212 if (CHARPOS (pos->string_pos) >= 0)
3213 {
3214 /* Recorded position is not in an overlay string, but in another
3215 string. This can only be a string from a `display' property.
3216 IT should already be filled with that string. */
3217 it->current.string_pos = pos->string_pos;
3218 eassert (STRINGP (it->string));
3219 if (it->bidi_p)
3220 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3221 FRAME_WINDOW_P (it->f), &it->bidi_it);
3222 }
3223
3224 /* Restore position in display vector translations, control
3225 character translations or ellipses. */
3226 if (pos->dpvec_index >= 0)
3227 {
3228 if (it->dpvec == NULL)
3229 get_next_display_element (it);
3230 eassert (it->dpvec && it->current.dpvec_index == 0);
3231 it->current.dpvec_index = pos->dpvec_index;
3232 }
3233
3234 CHECK_IT (it);
3235 return !overlay_strings_with_newlines;
3236 }
3237
3238
3239 /* Initialize IT for stepping through current_buffer in window W
3240 starting at ROW->start. */
3241
3242 static void
3243 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3244 {
3245 init_from_display_pos (it, w, &row->start);
3246 it->start = row->start;
3247 it->continuation_lines_width = row->continuation_lines_width;
3248 CHECK_IT (it);
3249 }
3250
3251
3252 /* Initialize IT for stepping through current_buffer in window W
3253 starting in the line following ROW, i.e. starting at ROW->end.
3254 Value is zero if there are overlay strings with newlines at ROW's
3255 end position. */
3256
3257 static int
3258 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3259 {
3260 int success = 0;
3261
3262 if (init_from_display_pos (it, w, &row->end))
3263 {
3264 if (row->continued_p)
3265 it->continuation_lines_width
3266 = row->continuation_lines_width + row->pixel_width;
3267 CHECK_IT (it);
3268 success = 1;
3269 }
3270
3271 return success;
3272 }
3273
3274
3275
3276 \f
3277 /***********************************************************************
3278 Text properties
3279 ***********************************************************************/
3280
3281 /* Called when IT reaches IT->stop_charpos. Handle text property and
3282 overlay changes. Set IT->stop_charpos to the next position where
3283 to stop. */
3284
3285 static void
3286 handle_stop (struct it *it)
3287 {
3288 enum prop_handled handled;
3289 int handle_overlay_change_p;
3290 struct props *p;
3291
3292 it->dpvec = NULL;
3293 it->current.dpvec_index = -1;
3294 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3295 it->ignore_overlay_strings_at_pos_p = 0;
3296 it->ellipsis_p = 0;
3297
3298 /* Use face of preceding text for ellipsis (if invisible) */
3299 if (it->selective_display_ellipsis_p)
3300 it->saved_face_id = it->face_id;
3301
3302 do
3303 {
3304 handled = HANDLED_NORMALLY;
3305
3306 /* Call text property handlers. */
3307 for (p = it_props; p->handler; ++p)
3308 {
3309 handled = p->handler (it);
3310
3311 if (handled == HANDLED_RECOMPUTE_PROPS)
3312 break;
3313 else if (handled == HANDLED_RETURN)
3314 {
3315 /* We still want to show before and after strings from
3316 overlays even if the actual buffer text is replaced. */
3317 if (!handle_overlay_change_p
3318 || it->sp > 1
3319 /* Don't call get_overlay_strings_1 if we already
3320 have overlay strings loaded, because doing so
3321 will load them again and push the iterator state
3322 onto the stack one more time, which is not
3323 expected by the rest of the code that processes
3324 overlay strings. */
3325 || (it->current.overlay_string_index < 0
3326 ? !get_overlay_strings_1 (it, 0, 0)
3327 : 0))
3328 {
3329 if (it->ellipsis_p)
3330 setup_for_ellipsis (it, 0);
3331 /* When handling a display spec, we might load an
3332 empty string. In that case, discard it here. We
3333 used to discard it in handle_single_display_spec,
3334 but that causes get_overlay_strings_1, above, to
3335 ignore overlay strings that we must check. */
3336 if (STRINGP (it->string) && !SCHARS (it->string))
3337 pop_it (it);
3338 return;
3339 }
3340 else if (STRINGP (it->string) && !SCHARS (it->string))
3341 pop_it (it);
3342 else
3343 {
3344 it->ignore_overlay_strings_at_pos_p = true;
3345 it->string_from_display_prop_p = 0;
3346 it->from_disp_prop_p = 0;
3347 handle_overlay_change_p = 0;
3348 }
3349 handled = HANDLED_RECOMPUTE_PROPS;
3350 break;
3351 }
3352 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3353 handle_overlay_change_p = 0;
3354 }
3355
3356 if (handled != HANDLED_RECOMPUTE_PROPS)
3357 {
3358 /* Don't check for overlay strings below when set to deliver
3359 characters from a display vector. */
3360 if (it->method == GET_FROM_DISPLAY_VECTOR)
3361 handle_overlay_change_p = 0;
3362
3363 /* Handle overlay changes.
3364 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3365 if it finds overlays. */
3366 if (handle_overlay_change_p)
3367 handled = handle_overlay_change (it);
3368 }
3369
3370 if (it->ellipsis_p)
3371 {
3372 setup_for_ellipsis (it, 0);
3373 break;
3374 }
3375 }
3376 while (handled == HANDLED_RECOMPUTE_PROPS);
3377
3378 /* Determine where to stop next. */
3379 if (handled == HANDLED_NORMALLY)
3380 compute_stop_pos (it);
3381 }
3382
3383
3384 /* Compute IT->stop_charpos from text property and overlay change
3385 information for IT's current position. */
3386
3387 static void
3388 compute_stop_pos (struct it *it)
3389 {
3390 register INTERVAL iv, next_iv;
3391 Lisp_Object object, limit, position;
3392 ptrdiff_t charpos, bytepos;
3393
3394 if (STRINGP (it->string))
3395 {
3396 /* Strings are usually short, so don't limit the search for
3397 properties. */
3398 it->stop_charpos = it->end_charpos;
3399 object = it->string;
3400 limit = Qnil;
3401 charpos = IT_STRING_CHARPOS (*it);
3402 bytepos = IT_STRING_BYTEPOS (*it);
3403 }
3404 else
3405 {
3406 ptrdiff_t pos;
3407
3408 /* If end_charpos is out of range for some reason, such as a
3409 misbehaving display function, rationalize it (Bug#5984). */
3410 if (it->end_charpos > ZV)
3411 it->end_charpos = ZV;
3412 it->stop_charpos = it->end_charpos;
3413
3414 /* If next overlay change is in front of the current stop pos
3415 (which is IT->end_charpos), stop there. Note: value of
3416 next_overlay_change is point-max if no overlay change
3417 follows. */
3418 charpos = IT_CHARPOS (*it);
3419 bytepos = IT_BYTEPOS (*it);
3420 pos = next_overlay_change (charpos);
3421 if (pos < it->stop_charpos)
3422 it->stop_charpos = pos;
3423
3424 /* Set up variables for computing the stop position from text
3425 property changes. */
3426 XSETBUFFER (object, current_buffer);
3427 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3428 }
3429
3430 /* Get the interval containing IT's position. Value is a null
3431 interval if there isn't such an interval. */
3432 position = make_number (charpos);
3433 iv = validate_interval_range (object, &position, &position, 0);
3434 if (iv)
3435 {
3436 Lisp_Object values_here[LAST_PROP_IDX];
3437 struct props *p;
3438
3439 /* Get properties here. */
3440 for (p = it_props; p->handler; ++p)
3441 values_here[p->idx] = textget (iv->plist, *p->name);
3442
3443 /* Look for an interval following iv that has different
3444 properties. */
3445 for (next_iv = next_interval (iv);
3446 (next_iv
3447 && (NILP (limit)
3448 || XFASTINT (limit) > next_iv->position));
3449 next_iv = next_interval (next_iv))
3450 {
3451 for (p = it_props; p->handler; ++p)
3452 {
3453 Lisp_Object new_value;
3454
3455 new_value = textget (next_iv->plist, *p->name);
3456 if (!EQ (values_here[p->idx], new_value))
3457 break;
3458 }
3459
3460 if (p->handler)
3461 break;
3462 }
3463
3464 if (next_iv)
3465 {
3466 if (INTEGERP (limit)
3467 && next_iv->position >= XFASTINT (limit))
3468 /* No text property change up to limit. */
3469 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3470 else
3471 /* Text properties change in next_iv. */
3472 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3473 }
3474 }
3475
3476 if (it->cmp_it.id < 0)
3477 {
3478 ptrdiff_t stoppos = it->end_charpos;
3479
3480 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3481 stoppos = -1;
3482 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3483 stoppos, it->string);
3484 }
3485
3486 eassert (STRINGP (it->string)
3487 || (it->stop_charpos >= BEGV
3488 && it->stop_charpos >= IT_CHARPOS (*it)));
3489 }
3490
3491
3492 /* Return the position of the next overlay change after POS in
3493 current_buffer. Value is point-max if no overlay change
3494 follows. This is like `next-overlay-change' but doesn't use
3495 xmalloc. */
3496
3497 static ptrdiff_t
3498 next_overlay_change (ptrdiff_t pos)
3499 {
3500 ptrdiff_t i, noverlays;
3501 ptrdiff_t endpos;
3502 Lisp_Object *overlays;
3503
3504 /* Get all overlays at the given position. */
3505 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3506
3507 /* If any of these overlays ends before endpos,
3508 use its ending point instead. */
3509 for (i = 0; i < noverlays; ++i)
3510 {
3511 Lisp_Object oend;
3512 ptrdiff_t oendpos;
3513
3514 oend = OVERLAY_END (overlays[i]);
3515 oendpos = OVERLAY_POSITION (oend);
3516 endpos = min (endpos, oendpos);
3517 }
3518
3519 return endpos;
3520 }
3521
3522 /* How many characters forward to search for a display property or
3523 display string. Searching too far forward makes the bidi display
3524 sluggish, especially in small windows. */
3525 #define MAX_DISP_SCAN 250
3526
3527 /* Return the character position of a display string at or after
3528 position specified by POSITION. If no display string exists at or
3529 after POSITION, return ZV. A display string is either an overlay
3530 with `display' property whose value is a string, or a `display'
3531 text property whose value is a string. STRING is data about the
3532 string to iterate; if STRING->lstring is nil, we are iterating a
3533 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3534 on a GUI frame. DISP_PROP is set to zero if we searched
3535 MAX_DISP_SCAN characters forward without finding any display
3536 strings, non-zero otherwise. It is set to 2 if the display string
3537 uses any kind of `(space ...)' spec that will produce a stretch of
3538 white space in the text area. */
3539 ptrdiff_t
3540 compute_display_string_pos (struct text_pos *position,
3541 struct bidi_string_data *string,
3542 struct window *w,
3543 int frame_window_p, int *disp_prop)
3544 {
3545 /* OBJECT = nil means current buffer. */
3546 Lisp_Object object, object1;
3547 Lisp_Object pos, spec, limpos;
3548 int string_p = (string && (STRINGP (string->lstring) || string->s));
3549 ptrdiff_t eob = string_p ? string->schars : ZV;
3550 ptrdiff_t begb = string_p ? 0 : BEGV;
3551 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3552 ptrdiff_t lim =
3553 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3554 struct text_pos tpos;
3555 int rv = 0;
3556
3557 if (string && STRINGP (string->lstring))
3558 object1 = object = string->lstring;
3559 else if (w && !string_p)
3560 {
3561 XSETWINDOW (object, w);
3562 object1 = Qnil;
3563 }
3564 else
3565 object1 = object = Qnil;
3566
3567 *disp_prop = 1;
3568
3569 if (charpos >= eob
3570 /* We don't support display properties whose values are strings
3571 that have display string properties. */
3572 || string->from_disp_str
3573 /* C strings cannot have display properties. */
3574 || (string->s && !STRINGP (object)))
3575 {
3576 *disp_prop = 0;
3577 return eob;
3578 }
3579
3580 /* If the character at CHARPOS is where the display string begins,
3581 return CHARPOS. */
3582 pos = make_number (charpos);
3583 if (STRINGP (object))
3584 bufpos = string->bufpos;
3585 else
3586 bufpos = charpos;
3587 tpos = *position;
3588 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3589 && (charpos <= begb
3590 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3591 object),
3592 spec))
3593 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3594 frame_window_p)))
3595 {
3596 if (rv == 2)
3597 *disp_prop = 2;
3598 return charpos;
3599 }
3600
3601 /* Look forward for the first character with a `display' property
3602 that will replace the underlying text when displayed. */
3603 limpos = make_number (lim);
3604 do {
3605 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3606 CHARPOS (tpos) = XFASTINT (pos);
3607 if (CHARPOS (tpos) >= lim)
3608 {
3609 *disp_prop = 0;
3610 break;
3611 }
3612 if (STRINGP (object))
3613 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3614 else
3615 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3616 spec = Fget_char_property (pos, Qdisplay, object);
3617 if (!STRINGP (object))
3618 bufpos = CHARPOS (tpos);
3619 } while (NILP (spec)
3620 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3621 bufpos, frame_window_p)));
3622 if (rv == 2)
3623 *disp_prop = 2;
3624
3625 return CHARPOS (tpos);
3626 }
3627
3628 /* Return the character position of the end of the display string that
3629 started at CHARPOS. If there's no display string at CHARPOS,
3630 return -1. A display string is either an overlay with `display'
3631 property whose value is a string or a `display' text property whose
3632 value is a string. */
3633 ptrdiff_t
3634 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3635 {
3636 /* OBJECT = nil means current buffer. */
3637 Lisp_Object object =
3638 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3639 Lisp_Object pos = make_number (charpos);
3640 ptrdiff_t eob =
3641 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3642
3643 if (charpos >= eob || (string->s && !STRINGP (object)))
3644 return eob;
3645
3646 /* It could happen that the display property or overlay was removed
3647 since we found it in compute_display_string_pos above. One way
3648 this can happen is if JIT font-lock was called (through
3649 handle_fontified_prop), and jit-lock-functions remove text
3650 properties or overlays from the portion of buffer that includes
3651 CHARPOS. Muse mode is known to do that, for example. In this
3652 case, we return -1 to the caller, to signal that no display
3653 string is actually present at CHARPOS. See bidi_fetch_char for
3654 how this is handled.
3655
3656 An alternative would be to never look for display properties past
3657 it->stop_charpos. But neither compute_display_string_pos nor
3658 bidi_fetch_char that calls it know or care where the next
3659 stop_charpos is. */
3660 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3661 return -1;
3662
3663 /* Look forward for the first character where the `display' property
3664 changes. */
3665 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3666
3667 return XFASTINT (pos);
3668 }
3669
3670
3671 \f
3672 /***********************************************************************
3673 Fontification
3674 ***********************************************************************/
3675
3676 /* Handle changes in the `fontified' property of the current buffer by
3677 calling hook functions from Qfontification_functions to fontify
3678 regions of text. */
3679
3680 static enum prop_handled
3681 handle_fontified_prop (struct it *it)
3682 {
3683 Lisp_Object prop, pos;
3684 enum prop_handled handled = HANDLED_NORMALLY;
3685
3686 if (!NILP (Vmemory_full))
3687 return handled;
3688
3689 /* Get the value of the `fontified' property at IT's current buffer
3690 position. (The `fontified' property doesn't have a special
3691 meaning in strings.) If the value is nil, call functions from
3692 Qfontification_functions. */
3693 if (!STRINGP (it->string)
3694 && it->s == NULL
3695 && !NILP (Vfontification_functions)
3696 && !NILP (Vrun_hooks)
3697 && (pos = make_number (IT_CHARPOS (*it)),
3698 prop = Fget_char_property (pos, Qfontified, Qnil),
3699 /* Ignore the special cased nil value always present at EOB since
3700 no amount of fontifying will be able to change it. */
3701 NILP (prop) && IT_CHARPOS (*it) < Z))
3702 {
3703 ptrdiff_t count = SPECPDL_INDEX ();
3704 Lisp_Object val;
3705 struct buffer *obuf = current_buffer;
3706 ptrdiff_t begv = BEGV, zv = ZV;
3707 bool old_clip_changed = current_buffer->clip_changed;
3708
3709 val = Vfontification_functions;
3710 specbind (Qfontification_functions, Qnil);
3711
3712 eassert (it->end_charpos == ZV);
3713
3714 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3715 safe_call1 (val, pos);
3716 else
3717 {
3718 Lisp_Object fns, fn;
3719 struct gcpro gcpro1, gcpro2;
3720
3721 fns = Qnil;
3722 GCPRO2 (val, fns);
3723
3724 for (; CONSP (val); val = XCDR (val))
3725 {
3726 fn = XCAR (val);
3727
3728 if (EQ (fn, Qt))
3729 {
3730 /* A value of t indicates this hook has a local
3731 binding; it means to run the global binding too.
3732 In a global value, t should not occur. If it
3733 does, we must ignore it to avoid an endless
3734 loop. */
3735 for (fns = Fdefault_value (Qfontification_functions);
3736 CONSP (fns);
3737 fns = XCDR (fns))
3738 {
3739 fn = XCAR (fns);
3740 if (!EQ (fn, Qt))
3741 safe_call1 (fn, pos);
3742 }
3743 }
3744 else
3745 safe_call1 (fn, pos);
3746 }
3747
3748 UNGCPRO;
3749 }
3750
3751 unbind_to (count, Qnil);
3752
3753 /* Fontification functions routinely call `save-restriction'.
3754 Normally, this tags clip_changed, which can confuse redisplay
3755 (see discussion in Bug#6671). Since we don't perform any
3756 special handling of fontification changes in the case where
3757 `save-restriction' isn't called, there's no point doing so in
3758 this case either. So, if the buffer's restrictions are
3759 actually left unchanged, reset clip_changed. */
3760 if (obuf == current_buffer)
3761 {
3762 if (begv == BEGV && zv == ZV)
3763 current_buffer->clip_changed = old_clip_changed;
3764 }
3765 /* There isn't much we can reasonably do to protect against
3766 misbehaving fontification, but here's a fig leaf. */
3767 else if (BUFFER_LIVE_P (obuf))
3768 set_buffer_internal_1 (obuf);
3769
3770 /* The fontification code may have added/removed text.
3771 It could do even a lot worse, but let's at least protect against
3772 the most obvious case where only the text past `pos' gets changed',
3773 as is/was done in grep.el where some escapes sequences are turned
3774 into face properties (bug#7876). */
3775 it->end_charpos = ZV;
3776
3777 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3778 something. This avoids an endless loop if they failed to
3779 fontify the text for which reason ever. */
3780 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3781 handled = HANDLED_RECOMPUTE_PROPS;
3782 }
3783
3784 return handled;
3785 }
3786
3787
3788 \f
3789 /***********************************************************************
3790 Faces
3791 ***********************************************************************/
3792
3793 /* Set up iterator IT from face properties at its current position.
3794 Called from handle_stop. */
3795
3796 static enum prop_handled
3797 handle_face_prop (struct it *it)
3798 {
3799 int new_face_id;
3800 ptrdiff_t next_stop;
3801
3802 if (!STRINGP (it->string))
3803 {
3804 new_face_id
3805 = face_at_buffer_position (it->w,
3806 IT_CHARPOS (*it),
3807 &next_stop,
3808 (IT_CHARPOS (*it)
3809 + TEXT_PROP_DISTANCE_LIMIT),
3810 0, it->base_face_id);
3811
3812 /* Is this a start of a run of characters with box face?
3813 Caveat: this can be called for a freshly initialized
3814 iterator; face_id is -1 in this case. We know that the new
3815 face will not change until limit, i.e. if the new face has a
3816 box, all characters up to limit will have one. But, as
3817 usual, we don't know whether limit is really the end. */
3818 if (new_face_id != it->face_id)
3819 {
3820 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3821 /* If it->face_id is -1, old_face below will be NULL, see
3822 the definition of FACE_FROM_ID. This will happen if this
3823 is the initial call that gets the face. */
3824 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3825
3826 /* If the value of face_id of the iterator is -1, we have to
3827 look in front of IT's position and see whether there is a
3828 face there that's different from new_face_id. */
3829 if (!old_face && IT_CHARPOS (*it) > BEG)
3830 {
3831 int prev_face_id = face_before_it_pos (it);
3832
3833 old_face = FACE_FROM_ID (it->f, prev_face_id);
3834 }
3835
3836 /* If the new face has a box, but the old face does not,
3837 this is the start of a run of characters with box face,
3838 i.e. this character has a shadow on the left side. */
3839 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3840 && (old_face == NULL || !old_face->box));
3841 it->face_box_p = new_face->box != FACE_NO_BOX;
3842 }
3843 }
3844 else
3845 {
3846 int base_face_id;
3847 ptrdiff_t bufpos;
3848 int i;
3849 Lisp_Object from_overlay
3850 = (it->current.overlay_string_index >= 0
3851 ? it->string_overlays[it->current.overlay_string_index
3852 % OVERLAY_STRING_CHUNK_SIZE]
3853 : Qnil);
3854
3855 /* See if we got to this string directly or indirectly from
3856 an overlay property. That includes the before-string or
3857 after-string of an overlay, strings in display properties
3858 provided by an overlay, their text properties, etc.
3859
3860 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3861 if (! NILP (from_overlay))
3862 for (i = it->sp - 1; i >= 0; i--)
3863 {
3864 if (it->stack[i].current.overlay_string_index >= 0)
3865 from_overlay
3866 = it->string_overlays[it->stack[i].current.overlay_string_index
3867 % OVERLAY_STRING_CHUNK_SIZE];
3868 else if (! NILP (it->stack[i].from_overlay))
3869 from_overlay = it->stack[i].from_overlay;
3870
3871 if (!NILP (from_overlay))
3872 break;
3873 }
3874
3875 if (! NILP (from_overlay))
3876 {
3877 bufpos = IT_CHARPOS (*it);
3878 /* For a string from an overlay, the base face depends
3879 only on text properties and ignores overlays. */
3880 base_face_id
3881 = face_for_overlay_string (it->w,
3882 IT_CHARPOS (*it),
3883 &next_stop,
3884 (IT_CHARPOS (*it)
3885 + TEXT_PROP_DISTANCE_LIMIT),
3886 0,
3887 from_overlay);
3888 }
3889 else
3890 {
3891 bufpos = 0;
3892
3893 /* For strings from a `display' property, use the face at
3894 IT's current buffer position as the base face to merge
3895 with, so that overlay strings appear in the same face as
3896 surrounding text, unless they specify their own faces.
3897 For strings from wrap-prefix and line-prefix properties,
3898 use the default face, possibly remapped via
3899 Vface_remapping_alist. */
3900 base_face_id = it->string_from_prefix_prop_p
3901 ? (!NILP (Vface_remapping_alist)
3902 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3903 : DEFAULT_FACE_ID)
3904 : underlying_face_id (it);
3905 }
3906
3907 new_face_id = face_at_string_position (it->w,
3908 it->string,
3909 IT_STRING_CHARPOS (*it),
3910 bufpos,
3911 &next_stop,
3912 base_face_id, 0);
3913
3914 /* Is this a start of a run of characters with box? Caveat:
3915 this can be called for a freshly allocated iterator; face_id
3916 is -1 is this case. We know that the new face will not
3917 change until the next check pos, i.e. if the new face has a
3918 box, all characters up to that position will have a
3919 box. But, as usual, we don't know whether that position
3920 is really the end. */
3921 if (new_face_id != it->face_id)
3922 {
3923 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3924 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3925
3926 /* If new face has a box but old face hasn't, this is the
3927 start of a run of characters with box, i.e. it has a
3928 shadow on the left side. */
3929 it->start_of_box_run_p
3930 = new_face->box && (old_face == NULL || !old_face->box);
3931 it->face_box_p = new_face->box != FACE_NO_BOX;
3932 }
3933 }
3934
3935 it->face_id = new_face_id;
3936 return HANDLED_NORMALLY;
3937 }
3938
3939
3940 /* Return the ID of the face ``underlying'' IT's current position,
3941 which is in a string. If the iterator is associated with a
3942 buffer, return the face at IT's current buffer position.
3943 Otherwise, use the iterator's base_face_id. */
3944
3945 static int
3946 underlying_face_id (struct it *it)
3947 {
3948 int face_id = it->base_face_id, i;
3949
3950 eassert (STRINGP (it->string));
3951
3952 for (i = it->sp - 1; i >= 0; --i)
3953 if (NILP (it->stack[i].string))
3954 face_id = it->stack[i].face_id;
3955
3956 return face_id;
3957 }
3958
3959
3960 /* Compute the face one character before or after the current position
3961 of IT, in the visual order. BEFORE_P non-zero means get the face
3962 in front (to the left in L2R paragraphs, to the right in R2L
3963 paragraphs) of IT's screen position. Value is the ID of the face. */
3964
3965 static int
3966 face_before_or_after_it_pos (struct it *it, int before_p)
3967 {
3968 int face_id, limit;
3969 ptrdiff_t next_check_charpos;
3970 struct it it_copy;
3971 void *it_copy_data = NULL;
3972
3973 eassert (it->s == NULL);
3974
3975 if (STRINGP (it->string))
3976 {
3977 ptrdiff_t bufpos, charpos;
3978 int base_face_id;
3979
3980 /* No face change past the end of the string (for the case
3981 we are padding with spaces). No face change before the
3982 string start. */
3983 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3984 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3985 return it->face_id;
3986
3987 if (!it->bidi_p)
3988 {
3989 /* Set charpos to the position before or after IT's current
3990 position, in the logical order, which in the non-bidi
3991 case is the same as the visual order. */
3992 if (before_p)
3993 charpos = IT_STRING_CHARPOS (*it) - 1;
3994 else if (it->what == IT_COMPOSITION)
3995 /* For composition, we must check the character after the
3996 composition. */
3997 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3998 else
3999 charpos = IT_STRING_CHARPOS (*it) + 1;
4000 }
4001 else
4002 {
4003 if (before_p)
4004 {
4005 /* With bidi iteration, the character before the current
4006 in the visual order cannot be found by simple
4007 iteration, because "reverse" reordering is not
4008 supported. Instead, we need to use the move_it_*
4009 family of functions. */
4010 /* Ignore face changes before the first visible
4011 character on this display line. */
4012 if (it->current_x <= it->first_visible_x)
4013 return it->face_id;
4014 SAVE_IT (it_copy, *it, it_copy_data);
4015 /* Implementation note: Since move_it_in_display_line
4016 works in the iterator geometry, and thinks the first
4017 character is always the leftmost, even in R2L lines,
4018 we don't need to distinguish between the R2L and L2R
4019 cases here. */
4020 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4021 it_copy.current_x - 1, MOVE_TO_X);
4022 charpos = IT_STRING_CHARPOS (it_copy);
4023 RESTORE_IT (it, it, it_copy_data);
4024 }
4025 else
4026 {
4027 /* Set charpos to the string position of the character
4028 that comes after IT's current position in the visual
4029 order. */
4030 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4031
4032 it_copy = *it;
4033 while (n--)
4034 bidi_move_to_visually_next (&it_copy.bidi_it);
4035
4036 charpos = it_copy.bidi_it.charpos;
4037 }
4038 }
4039 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4040
4041 if (it->current.overlay_string_index >= 0)
4042 bufpos = IT_CHARPOS (*it);
4043 else
4044 bufpos = 0;
4045
4046 base_face_id = underlying_face_id (it);
4047
4048 /* Get the face for ASCII, or unibyte. */
4049 face_id = face_at_string_position (it->w,
4050 it->string,
4051 charpos,
4052 bufpos,
4053 &next_check_charpos,
4054 base_face_id, 0);
4055
4056 /* Correct the face for charsets different from ASCII. Do it
4057 for the multibyte case only. The face returned above is
4058 suitable for unibyte text if IT->string is unibyte. */
4059 if (STRING_MULTIBYTE (it->string))
4060 {
4061 struct text_pos pos1 = string_pos (charpos, it->string);
4062 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4063 int c, len;
4064 struct face *face = FACE_FROM_ID (it->f, face_id);
4065
4066 c = string_char_and_length (p, &len);
4067 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4068 }
4069 }
4070 else
4071 {
4072 struct text_pos pos;
4073
4074 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4075 || (IT_CHARPOS (*it) <= BEGV && before_p))
4076 return it->face_id;
4077
4078 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4079 pos = it->current.pos;
4080
4081 if (!it->bidi_p)
4082 {
4083 if (before_p)
4084 DEC_TEXT_POS (pos, it->multibyte_p);
4085 else
4086 {
4087 if (it->what == IT_COMPOSITION)
4088 {
4089 /* For composition, we must check the position after
4090 the composition. */
4091 pos.charpos += it->cmp_it.nchars;
4092 pos.bytepos += it->len;
4093 }
4094 else
4095 INC_TEXT_POS (pos, it->multibyte_p);
4096 }
4097 }
4098 else
4099 {
4100 if (before_p)
4101 {
4102 /* With bidi iteration, the character before the current
4103 in the visual order cannot be found by simple
4104 iteration, because "reverse" reordering is not
4105 supported. Instead, we need to use the move_it_*
4106 family of functions. */
4107 /* Ignore face changes before the first visible
4108 character on this display line. */
4109 if (it->current_x <= it->first_visible_x)
4110 return it->face_id;
4111 SAVE_IT (it_copy, *it, it_copy_data);
4112 /* Implementation note: Since move_it_in_display_line
4113 works in the iterator geometry, and thinks the first
4114 character is always the leftmost, even in R2L lines,
4115 we don't need to distinguish between the R2L and L2R
4116 cases here. */
4117 move_it_in_display_line (&it_copy, ZV,
4118 it_copy.current_x - 1, MOVE_TO_X);
4119 pos = it_copy.current.pos;
4120 RESTORE_IT (it, it, it_copy_data);
4121 }
4122 else
4123 {
4124 /* Set charpos to the buffer position of the character
4125 that comes after IT's current position in the visual
4126 order. */
4127 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4128
4129 it_copy = *it;
4130 while (n--)
4131 bidi_move_to_visually_next (&it_copy.bidi_it);
4132
4133 SET_TEXT_POS (pos,
4134 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4135 }
4136 }
4137 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4138
4139 /* Determine face for CHARSET_ASCII, or unibyte. */
4140 face_id = face_at_buffer_position (it->w,
4141 CHARPOS (pos),
4142 &next_check_charpos,
4143 limit, 0, -1);
4144
4145 /* Correct the face for charsets different from ASCII. Do it
4146 for the multibyte case only. The face returned above is
4147 suitable for unibyte text if current_buffer is unibyte. */
4148 if (it->multibyte_p)
4149 {
4150 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4151 struct face *face = FACE_FROM_ID (it->f, face_id);
4152 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4153 }
4154 }
4155
4156 return face_id;
4157 }
4158
4159
4160 \f
4161 /***********************************************************************
4162 Invisible text
4163 ***********************************************************************/
4164
4165 /* Set up iterator IT from invisible properties at its current
4166 position. Called from handle_stop. */
4167
4168 static enum prop_handled
4169 handle_invisible_prop (struct it *it)
4170 {
4171 enum prop_handled handled = HANDLED_NORMALLY;
4172 int invis_p;
4173 Lisp_Object prop;
4174
4175 if (STRINGP (it->string))
4176 {
4177 Lisp_Object end_charpos, limit, charpos;
4178
4179 /* Get the value of the invisible text property at the
4180 current position. Value will be nil if there is no such
4181 property. */
4182 charpos = make_number (IT_STRING_CHARPOS (*it));
4183 prop = Fget_text_property (charpos, Qinvisible, it->string);
4184 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4185
4186 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4187 {
4188 /* Record whether we have to display an ellipsis for the
4189 invisible text. */
4190 int display_ellipsis_p = (invis_p == 2);
4191 ptrdiff_t len, endpos;
4192
4193 handled = HANDLED_RECOMPUTE_PROPS;
4194
4195 /* Get the position at which the next visible text can be
4196 found in IT->string, if any. */
4197 endpos = len = SCHARS (it->string);
4198 XSETINT (limit, len);
4199 do
4200 {
4201 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4202 it->string, limit);
4203 if (INTEGERP (end_charpos))
4204 {
4205 endpos = XFASTINT (end_charpos);
4206 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4207 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4208 if (invis_p == 2)
4209 display_ellipsis_p = true;
4210 }
4211 }
4212 while (invis_p && endpos < len);
4213
4214 if (display_ellipsis_p)
4215 it->ellipsis_p = true;
4216
4217 if (endpos < len)
4218 {
4219 /* Text at END_CHARPOS is visible. Move IT there. */
4220 struct text_pos old;
4221 ptrdiff_t oldpos;
4222
4223 old = it->current.string_pos;
4224 oldpos = CHARPOS (old);
4225 if (it->bidi_p)
4226 {
4227 if (it->bidi_it.first_elt
4228 && it->bidi_it.charpos < SCHARS (it->string))
4229 bidi_paragraph_init (it->paragraph_embedding,
4230 &it->bidi_it, 1);
4231 /* Bidi-iterate out of the invisible text. */
4232 do
4233 {
4234 bidi_move_to_visually_next (&it->bidi_it);
4235 }
4236 while (oldpos <= it->bidi_it.charpos
4237 && it->bidi_it.charpos < endpos);
4238
4239 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4240 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4241 if (IT_CHARPOS (*it) >= endpos)
4242 it->prev_stop = endpos;
4243 }
4244 else
4245 {
4246 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4247 compute_string_pos (&it->current.string_pos, old, it->string);
4248 }
4249 }
4250 else
4251 {
4252 /* The rest of the string is invisible. If this is an
4253 overlay string, proceed with the next overlay string
4254 or whatever comes and return a character from there. */
4255 if (it->current.overlay_string_index >= 0
4256 && !display_ellipsis_p)
4257 {
4258 next_overlay_string (it);
4259 /* Don't check for overlay strings when we just
4260 finished processing them. */
4261 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4262 }
4263 else
4264 {
4265 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4266 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4267 }
4268 }
4269 }
4270 }
4271 else
4272 {
4273 ptrdiff_t newpos, next_stop, start_charpos, tem;
4274 Lisp_Object pos, overlay;
4275
4276 /* First of all, is there invisible text at this position? */
4277 tem = start_charpos = IT_CHARPOS (*it);
4278 pos = make_number (tem);
4279 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4280 &overlay);
4281 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4282
4283 /* If we are on invisible text, skip over it. */
4284 if (invis_p && start_charpos < it->end_charpos)
4285 {
4286 /* Record whether we have to display an ellipsis for the
4287 invisible text. */
4288 int display_ellipsis_p = invis_p == 2;
4289
4290 handled = HANDLED_RECOMPUTE_PROPS;
4291
4292 /* Loop skipping over invisible text. The loop is left at
4293 ZV or with IT on the first char being visible again. */
4294 do
4295 {
4296 /* Try to skip some invisible text. Return value is the
4297 position reached which can be equal to where we start
4298 if there is nothing invisible there. This skips both
4299 over invisible text properties and overlays with
4300 invisible property. */
4301 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4302
4303 /* If we skipped nothing at all we weren't at invisible
4304 text in the first place. If everything to the end of
4305 the buffer was skipped, end the loop. */
4306 if (newpos == tem || newpos >= ZV)
4307 invis_p = 0;
4308 else
4309 {
4310 /* We skipped some characters but not necessarily
4311 all there are. Check if we ended up on visible
4312 text. Fget_char_property returns the property of
4313 the char before the given position, i.e. if we
4314 get invis_p = 0, this means that the char at
4315 newpos is visible. */
4316 pos = make_number (newpos);
4317 prop = Fget_char_property (pos, Qinvisible, it->window);
4318 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4319 }
4320
4321 /* If we ended up on invisible text, proceed to
4322 skip starting with next_stop. */
4323 if (invis_p)
4324 tem = next_stop;
4325
4326 /* If there are adjacent invisible texts, don't lose the
4327 second one's ellipsis. */
4328 if (invis_p == 2)
4329 display_ellipsis_p = true;
4330 }
4331 while (invis_p);
4332
4333 /* The position newpos is now either ZV or on visible text. */
4334 if (it->bidi_p)
4335 {
4336 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4337 int on_newline
4338 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4339 int after_newline
4340 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4341
4342 /* If the invisible text ends on a newline or on a
4343 character after a newline, we can avoid the costly,
4344 character by character, bidi iteration to NEWPOS, and
4345 instead simply reseat the iterator there. That's
4346 because all bidi reordering information is tossed at
4347 the newline. This is a big win for modes that hide
4348 complete lines, like Outline, Org, etc. */
4349 if (on_newline || after_newline)
4350 {
4351 struct text_pos tpos;
4352 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4353
4354 SET_TEXT_POS (tpos, newpos, bpos);
4355 reseat_1 (it, tpos, 0);
4356 /* If we reseat on a newline/ZV, we need to prep the
4357 bidi iterator for advancing to the next character
4358 after the newline/EOB, keeping the current paragraph
4359 direction (so that PRODUCE_GLYPHS does TRT wrt
4360 prepending/appending glyphs to a glyph row). */
4361 if (on_newline)
4362 {
4363 it->bidi_it.first_elt = 0;
4364 it->bidi_it.paragraph_dir = pdir;
4365 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4366 it->bidi_it.nchars = 1;
4367 it->bidi_it.ch_len = 1;
4368 }
4369 }
4370 else /* Must use the slow method. */
4371 {
4372 /* With bidi iteration, the region of invisible text
4373 could start and/or end in the middle of a
4374 non-base embedding level. Therefore, we need to
4375 skip invisible text using the bidi iterator,
4376 starting at IT's current position, until we find
4377 ourselves outside of the invisible text.
4378 Skipping invisible text _after_ bidi iteration
4379 avoids affecting the visual order of the
4380 displayed text when invisible properties are
4381 added or removed. */
4382 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4383 {
4384 /* If we were `reseat'ed to a new paragraph,
4385 determine the paragraph base direction. We
4386 need to do it now because
4387 next_element_from_buffer may not have a
4388 chance to do it, if we are going to skip any
4389 text at the beginning, which resets the
4390 FIRST_ELT flag. */
4391 bidi_paragraph_init (it->paragraph_embedding,
4392 &it->bidi_it, 1);
4393 }
4394 do
4395 {
4396 bidi_move_to_visually_next (&it->bidi_it);
4397 }
4398 while (it->stop_charpos <= it->bidi_it.charpos
4399 && it->bidi_it.charpos < newpos);
4400 IT_CHARPOS (*it) = it->bidi_it.charpos;
4401 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4402 /* If we overstepped NEWPOS, record its position in
4403 the iterator, so that we skip invisible text if
4404 later the bidi iteration lands us in the
4405 invisible region again. */
4406 if (IT_CHARPOS (*it) >= newpos)
4407 it->prev_stop = newpos;
4408 }
4409 }
4410 else
4411 {
4412 IT_CHARPOS (*it) = newpos;
4413 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4414 }
4415
4416 /* If there are before-strings at the start of invisible
4417 text, and the text is invisible because of a text
4418 property, arrange to show before-strings because 20.x did
4419 it that way. (If the text is invisible because of an
4420 overlay property instead of a text property, this is
4421 already handled in the overlay code.) */
4422 if (NILP (overlay)
4423 && get_overlay_strings (it, it->stop_charpos))
4424 {
4425 handled = HANDLED_RECOMPUTE_PROPS;
4426 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4427 }
4428 else if (display_ellipsis_p)
4429 {
4430 /* Make sure that the glyphs of the ellipsis will get
4431 correct `charpos' values. If we would not update
4432 it->position here, the glyphs would belong to the
4433 last visible character _before_ the invisible
4434 text, which confuses `set_cursor_from_row'.
4435
4436 We use the last invisible position instead of the
4437 first because this way the cursor is always drawn on
4438 the first "." of the ellipsis, whenever PT is inside
4439 the invisible text. Otherwise the cursor would be
4440 placed _after_ the ellipsis when the point is after the
4441 first invisible character. */
4442 if (!STRINGP (it->object))
4443 {
4444 it->position.charpos = newpos - 1;
4445 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4446 }
4447 it->ellipsis_p = true;
4448 /* Let the ellipsis display before
4449 considering any properties of the following char.
4450 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4451 handled = HANDLED_RETURN;
4452 }
4453 }
4454 }
4455
4456 return handled;
4457 }
4458
4459
4460 /* Make iterator IT return `...' next.
4461 Replaces LEN characters from buffer. */
4462
4463 static void
4464 setup_for_ellipsis (struct it *it, int len)
4465 {
4466 /* Use the display table definition for `...'. Invalid glyphs
4467 will be handled by the method returning elements from dpvec. */
4468 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4469 {
4470 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4471 it->dpvec = v->contents;
4472 it->dpend = v->contents + v->header.size;
4473 }
4474 else
4475 {
4476 /* Default `...'. */
4477 it->dpvec = default_invis_vector;
4478 it->dpend = default_invis_vector + 3;
4479 }
4480
4481 it->dpvec_char_len = len;
4482 it->current.dpvec_index = 0;
4483 it->dpvec_face_id = -1;
4484
4485 /* Remember the current face id in case glyphs specify faces.
4486 IT's face is restored in set_iterator_to_next.
4487 saved_face_id was set to preceding char's face in handle_stop. */
4488 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4489 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4490
4491 it->method = GET_FROM_DISPLAY_VECTOR;
4492 it->ellipsis_p = true;
4493 }
4494
4495
4496 \f
4497 /***********************************************************************
4498 'display' property
4499 ***********************************************************************/
4500
4501 /* Set up iterator IT from `display' property at its current position.
4502 Called from handle_stop.
4503 We return HANDLED_RETURN if some part of the display property
4504 overrides the display of the buffer text itself.
4505 Otherwise we return HANDLED_NORMALLY. */
4506
4507 static enum prop_handled
4508 handle_display_prop (struct it *it)
4509 {
4510 Lisp_Object propval, object, overlay;
4511 struct text_pos *position;
4512 ptrdiff_t bufpos;
4513 /* Nonzero if some property replaces the display of the text itself. */
4514 int display_replaced_p = 0;
4515
4516 if (STRINGP (it->string))
4517 {
4518 object = it->string;
4519 position = &it->current.string_pos;
4520 bufpos = CHARPOS (it->current.pos);
4521 }
4522 else
4523 {
4524 XSETWINDOW (object, it->w);
4525 position = &it->current.pos;
4526 bufpos = CHARPOS (*position);
4527 }
4528
4529 /* Reset those iterator values set from display property values. */
4530 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4531 it->space_width = Qnil;
4532 it->font_height = Qnil;
4533 it->voffset = 0;
4534
4535 /* We don't support recursive `display' properties, i.e. string
4536 values that have a string `display' property, that have a string
4537 `display' property etc. */
4538 if (!it->string_from_display_prop_p)
4539 it->area = TEXT_AREA;
4540
4541 propval = get_char_property_and_overlay (make_number (position->charpos),
4542 Qdisplay, object, &overlay);
4543 if (NILP (propval))
4544 return HANDLED_NORMALLY;
4545 /* Now OVERLAY is the overlay that gave us this property, or nil
4546 if it was a text property. */
4547
4548 if (!STRINGP (it->string))
4549 object = it->w->contents;
4550
4551 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4552 position, bufpos,
4553 FRAME_WINDOW_P (it->f));
4554
4555 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4556 }
4557
4558 /* Subroutine of handle_display_prop. Returns non-zero if the display
4559 specification in SPEC is a replacing specification, i.e. it would
4560 replace the text covered by `display' property with something else,
4561 such as an image or a display string. If SPEC includes any kind or
4562 `(space ...) specification, the value is 2; this is used by
4563 compute_display_string_pos, which see.
4564
4565 See handle_single_display_spec for documentation of arguments.
4566 frame_window_p is non-zero if the window being redisplayed is on a
4567 GUI frame; this argument is used only if IT is NULL, see below.
4568
4569 IT can be NULL, if this is called by the bidi reordering code
4570 through compute_display_string_pos, which see. In that case, this
4571 function only examines SPEC, but does not otherwise "handle" it, in
4572 the sense that it doesn't set up members of IT from the display
4573 spec. */
4574 static int
4575 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4576 Lisp_Object overlay, struct text_pos *position,
4577 ptrdiff_t bufpos, int frame_window_p)
4578 {
4579 int replacing_p = 0;
4580 int rv;
4581
4582 if (CONSP (spec)
4583 /* Simple specifications. */
4584 && !EQ (XCAR (spec), Qimage)
4585 && !EQ (XCAR (spec), Qspace)
4586 && !EQ (XCAR (spec), Qwhen)
4587 && !EQ (XCAR (spec), Qslice)
4588 && !EQ (XCAR (spec), Qspace_width)
4589 && !EQ (XCAR (spec), Qheight)
4590 && !EQ (XCAR (spec), Qraise)
4591 /* Marginal area specifications. */
4592 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4593 && !EQ (XCAR (spec), Qleft_fringe)
4594 && !EQ (XCAR (spec), Qright_fringe)
4595 && !NILP (XCAR (spec)))
4596 {
4597 for (; CONSP (spec); spec = XCDR (spec))
4598 {
4599 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4600 overlay, position, bufpos,
4601 replacing_p, frame_window_p)))
4602 {
4603 replacing_p = rv;
4604 /* If some text in a string is replaced, `position' no
4605 longer points to the position of `object'. */
4606 if (!it || STRINGP (object))
4607 break;
4608 }
4609 }
4610 }
4611 else if (VECTORP (spec))
4612 {
4613 ptrdiff_t i;
4614 for (i = 0; i < ASIZE (spec); ++i)
4615 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4616 overlay, position, bufpos,
4617 replacing_p, frame_window_p)))
4618 {
4619 replacing_p = rv;
4620 /* If some text in a string is replaced, `position' no
4621 longer points to the position of `object'. */
4622 if (!it || STRINGP (object))
4623 break;
4624 }
4625 }
4626 else
4627 {
4628 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4629 position, bufpos, 0,
4630 frame_window_p)))
4631 replacing_p = rv;
4632 }
4633
4634 return replacing_p;
4635 }
4636
4637 /* Value is the position of the end of the `display' property starting
4638 at START_POS in OBJECT. */
4639
4640 static struct text_pos
4641 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4642 {
4643 Lisp_Object end;
4644 struct text_pos end_pos;
4645
4646 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4647 Qdisplay, object, Qnil);
4648 CHARPOS (end_pos) = XFASTINT (end);
4649 if (STRINGP (object))
4650 compute_string_pos (&end_pos, start_pos, it->string);
4651 else
4652 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4653
4654 return end_pos;
4655 }
4656
4657
4658 /* Set up IT from a single `display' property specification SPEC. OBJECT
4659 is the object in which the `display' property was found. *POSITION
4660 is the position in OBJECT at which the `display' property was found.
4661 BUFPOS is the buffer position of OBJECT (different from POSITION if
4662 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4663 previously saw a display specification which already replaced text
4664 display with something else, for example an image; we ignore such
4665 properties after the first one has been processed.
4666
4667 OVERLAY is the overlay this `display' property came from,
4668 or nil if it was a text property.
4669
4670 If SPEC is a `space' or `image' specification, and in some other
4671 cases too, set *POSITION to the position where the `display'
4672 property ends.
4673
4674 If IT is NULL, only examine the property specification in SPEC, but
4675 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4676 is intended to be displayed in a window on a GUI frame.
4677
4678 Value is non-zero if something was found which replaces the display
4679 of buffer or string text. */
4680
4681 static int
4682 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4683 Lisp_Object overlay, struct text_pos *position,
4684 ptrdiff_t bufpos, int display_replaced_p,
4685 int frame_window_p)
4686 {
4687 Lisp_Object form;
4688 Lisp_Object location, value;
4689 struct text_pos start_pos = *position;
4690 int valid_p;
4691
4692 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4693 If the result is non-nil, use VALUE instead of SPEC. */
4694 form = Qt;
4695 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4696 {
4697 spec = XCDR (spec);
4698 if (!CONSP (spec))
4699 return 0;
4700 form = XCAR (spec);
4701 spec = XCDR (spec);
4702 }
4703
4704 if (!NILP (form) && !EQ (form, Qt))
4705 {
4706 ptrdiff_t count = SPECPDL_INDEX ();
4707 struct gcpro gcpro1;
4708
4709 /* Bind `object' to the object having the `display' property, a
4710 buffer or string. Bind `position' to the position in the
4711 object where the property was found, and `buffer-position'
4712 to the current position in the buffer. */
4713
4714 if (NILP (object))
4715 XSETBUFFER (object, current_buffer);
4716 specbind (Qobject, object);
4717 specbind (Qposition, make_number (CHARPOS (*position)));
4718 specbind (Qbuffer_position, make_number (bufpos));
4719 GCPRO1 (form);
4720 form = safe_eval (form);
4721 UNGCPRO;
4722 unbind_to (count, Qnil);
4723 }
4724
4725 if (NILP (form))
4726 return 0;
4727
4728 /* Handle `(height HEIGHT)' specifications. */
4729 if (CONSP (spec)
4730 && EQ (XCAR (spec), Qheight)
4731 && CONSP (XCDR (spec)))
4732 {
4733 if (it)
4734 {
4735 if (!FRAME_WINDOW_P (it->f))
4736 return 0;
4737
4738 it->font_height = XCAR (XCDR (spec));
4739 if (!NILP (it->font_height))
4740 {
4741 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4742 int new_height = -1;
4743
4744 if (CONSP (it->font_height)
4745 && (EQ (XCAR (it->font_height), Qplus)
4746 || EQ (XCAR (it->font_height), Qminus))
4747 && CONSP (XCDR (it->font_height))
4748 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4749 {
4750 /* `(+ N)' or `(- N)' where N is an integer. */
4751 int steps = XINT (XCAR (XCDR (it->font_height)));
4752 if (EQ (XCAR (it->font_height), Qplus))
4753 steps = - steps;
4754 it->face_id = smaller_face (it->f, it->face_id, steps);
4755 }
4756 else if (FUNCTIONP (it->font_height))
4757 {
4758 /* Call function with current height as argument.
4759 Value is the new height. */
4760 Lisp_Object height;
4761 height = safe_call1 (it->font_height,
4762 face->lface[LFACE_HEIGHT_INDEX]);
4763 if (NUMBERP (height))
4764 new_height = XFLOATINT (height);
4765 }
4766 else if (NUMBERP (it->font_height))
4767 {
4768 /* Value is a multiple of the canonical char height. */
4769 struct face *f;
4770
4771 f = FACE_FROM_ID (it->f,
4772 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4773 new_height = (XFLOATINT (it->font_height)
4774 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4775 }
4776 else
4777 {
4778 /* Evaluate IT->font_height with `height' bound to the
4779 current specified height to get the new height. */
4780 ptrdiff_t count = SPECPDL_INDEX ();
4781
4782 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4783 value = safe_eval (it->font_height);
4784 unbind_to (count, Qnil);
4785
4786 if (NUMBERP (value))
4787 new_height = XFLOATINT (value);
4788 }
4789
4790 if (new_height > 0)
4791 it->face_id = face_with_height (it->f, it->face_id, new_height);
4792 }
4793 }
4794
4795 return 0;
4796 }
4797
4798 /* Handle `(space-width WIDTH)'. */
4799 if (CONSP (spec)
4800 && EQ (XCAR (spec), Qspace_width)
4801 && CONSP (XCDR (spec)))
4802 {
4803 if (it)
4804 {
4805 if (!FRAME_WINDOW_P (it->f))
4806 return 0;
4807
4808 value = XCAR (XCDR (spec));
4809 if (NUMBERP (value) && XFLOATINT (value) > 0)
4810 it->space_width = value;
4811 }
4812
4813 return 0;
4814 }
4815
4816 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4817 if (CONSP (spec)
4818 && EQ (XCAR (spec), Qslice))
4819 {
4820 Lisp_Object tem;
4821
4822 if (it)
4823 {
4824 if (!FRAME_WINDOW_P (it->f))
4825 return 0;
4826
4827 if (tem = XCDR (spec), CONSP (tem))
4828 {
4829 it->slice.x = XCAR (tem);
4830 if (tem = XCDR (tem), CONSP (tem))
4831 {
4832 it->slice.y = XCAR (tem);
4833 if (tem = XCDR (tem), CONSP (tem))
4834 {
4835 it->slice.width = XCAR (tem);
4836 if (tem = XCDR (tem), CONSP (tem))
4837 it->slice.height = XCAR (tem);
4838 }
4839 }
4840 }
4841 }
4842
4843 return 0;
4844 }
4845
4846 /* Handle `(raise FACTOR)'. */
4847 if (CONSP (spec)
4848 && EQ (XCAR (spec), Qraise)
4849 && CONSP (XCDR (spec)))
4850 {
4851 if (it)
4852 {
4853 if (!FRAME_WINDOW_P (it->f))
4854 return 0;
4855
4856 #ifdef HAVE_WINDOW_SYSTEM
4857 value = XCAR (XCDR (spec));
4858 if (NUMBERP (value))
4859 {
4860 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4861 it->voffset = - (XFLOATINT (value)
4862 * (FONT_HEIGHT (face->font)));
4863 }
4864 #endif /* HAVE_WINDOW_SYSTEM */
4865 }
4866
4867 return 0;
4868 }
4869
4870 /* Don't handle the other kinds of display specifications
4871 inside a string that we got from a `display' property. */
4872 if (it && it->string_from_display_prop_p)
4873 return 0;
4874
4875 /* Characters having this form of property are not displayed, so
4876 we have to find the end of the property. */
4877 if (it)
4878 {
4879 start_pos = *position;
4880 *position = display_prop_end (it, object, start_pos);
4881 }
4882 value = Qnil;
4883
4884 /* Stop the scan at that end position--we assume that all
4885 text properties change there. */
4886 if (it)
4887 it->stop_charpos = position->charpos;
4888
4889 /* Handle `(left-fringe BITMAP [FACE])'
4890 and `(right-fringe BITMAP [FACE])'. */
4891 if (CONSP (spec)
4892 && (EQ (XCAR (spec), Qleft_fringe)
4893 || EQ (XCAR (spec), Qright_fringe))
4894 && CONSP (XCDR (spec)))
4895 {
4896 int fringe_bitmap;
4897
4898 if (it)
4899 {
4900 if (!FRAME_WINDOW_P (it->f))
4901 /* If we return here, POSITION has been advanced
4902 across the text with this property. */
4903 {
4904 /* Synchronize the bidi iterator with POSITION. This is
4905 needed because we are not going to push the iterator
4906 on behalf of this display property, so there will be
4907 no pop_it call to do this synchronization for us. */
4908 if (it->bidi_p)
4909 {
4910 it->position = *position;
4911 iterate_out_of_display_property (it);
4912 *position = it->position;
4913 }
4914 return 1;
4915 }
4916 }
4917 else if (!frame_window_p)
4918 return 1;
4919
4920 #ifdef HAVE_WINDOW_SYSTEM
4921 value = XCAR (XCDR (spec));
4922 if (!SYMBOLP (value)
4923 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4924 /* If we return here, POSITION has been advanced
4925 across the text with this property. */
4926 {
4927 if (it && it->bidi_p)
4928 {
4929 it->position = *position;
4930 iterate_out_of_display_property (it);
4931 *position = it->position;
4932 }
4933 return 1;
4934 }
4935
4936 if (it)
4937 {
4938 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4939
4940 if (CONSP (XCDR (XCDR (spec))))
4941 {
4942 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4943 int face_id2 = lookup_derived_face (it->f, face_name,
4944 FRINGE_FACE_ID, 0);
4945 if (face_id2 >= 0)
4946 face_id = face_id2;
4947 }
4948
4949 /* Save current settings of IT so that we can restore them
4950 when we are finished with the glyph property value. */
4951 push_it (it, position);
4952
4953 it->area = TEXT_AREA;
4954 it->what = IT_IMAGE;
4955 it->image_id = -1; /* no image */
4956 it->position = start_pos;
4957 it->object = NILP (object) ? it->w->contents : object;
4958 it->method = GET_FROM_IMAGE;
4959 it->from_overlay = Qnil;
4960 it->face_id = face_id;
4961 it->from_disp_prop_p = true;
4962
4963 /* Say that we haven't consumed the characters with
4964 `display' property yet. The call to pop_it in
4965 set_iterator_to_next will clean this up. */
4966 *position = start_pos;
4967
4968 if (EQ (XCAR (spec), Qleft_fringe))
4969 {
4970 it->left_user_fringe_bitmap = fringe_bitmap;
4971 it->left_user_fringe_face_id = face_id;
4972 }
4973 else
4974 {
4975 it->right_user_fringe_bitmap = fringe_bitmap;
4976 it->right_user_fringe_face_id = face_id;
4977 }
4978 }
4979 #endif /* HAVE_WINDOW_SYSTEM */
4980 return 1;
4981 }
4982
4983 /* Prepare to handle `((margin left-margin) ...)',
4984 `((margin right-margin) ...)' and `((margin nil) ...)'
4985 prefixes for display specifications. */
4986 location = Qunbound;
4987 if (CONSP (spec) && CONSP (XCAR (spec)))
4988 {
4989 Lisp_Object tem;
4990
4991 value = XCDR (spec);
4992 if (CONSP (value))
4993 value = XCAR (value);
4994
4995 tem = XCAR (spec);
4996 if (EQ (XCAR (tem), Qmargin)
4997 && (tem = XCDR (tem),
4998 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4999 (NILP (tem)
5000 || EQ (tem, Qleft_margin)
5001 || EQ (tem, Qright_margin))))
5002 location = tem;
5003 }
5004
5005 if (EQ (location, Qunbound))
5006 {
5007 location = Qnil;
5008 value = spec;
5009 }
5010
5011 /* After this point, VALUE is the property after any
5012 margin prefix has been stripped. It must be a string,
5013 an image specification, or `(space ...)'.
5014
5015 LOCATION specifies where to display: `left-margin',
5016 `right-margin' or nil. */
5017
5018 valid_p = (STRINGP (value)
5019 #ifdef HAVE_WINDOW_SYSTEM
5020 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5021 && valid_image_p (value))
5022 #endif /* not HAVE_WINDOW_SYSTEM */
5023 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5024
5025 if (valid_p && !display_replaced_p)
5026 {
5027 int retval = 1;
5028
5029 if (!it)
5030 {
5031 /* Callers need to know whether the display spec is any kind
5032 of `(space ...)' spec that is about to affect text-area
5033 display. */
5034 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5035 retval = 2;
5036 return retval;
5037 }
5038
5039 /* Save current settings of IT so that we can restore them
5040 when we are finished with the glyph property value. */
5041 push_it (it, position);
5042 it->from_overlay = overlay;
5043 it->from_disp_prop_p = true;
5044
5045 if (NILP (location))
5046 it->area = TEXT_AREA;
5047 else if (EQ (location, Qleft_margin))
5048 it->area = LEFT_MARGIN_AREA;
5049 else
5050 it->area = RIGHT_MARGIN_AREA;
5051
5052 if (STRINGP (value))
5053 {
5054 it->string = value;
5055 it->multibyte_p = STRING_MULTIBYTE (it->string);
5056 it->current.overlay_string_index = -1;
5057 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5058 it->end_charpos = it->string_nchars = SCHARS (it->string);
5059 it->method = GET_FROM_STRING;
5060 it->stop_charpos = 0;
5061 it->prev_stop = 0;
5062 it->base_level_stop = 0;
5063 it->string_from_display_prop_p = true;
5064 /* Say that we haven't consumed the characters with
5065 `display' property yet. The call to pop_it in
5066 set_iterator_to_next will clean this up. */
5067 if (BUFFERP (object))
5068 *position = start_pos;
5069
5070 /* Force paragraph direction to be that of the parent
5071 object. If the parent object's paragraph direction is
5072 not yet determined, default to L2R. */
5073 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5074 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5075 else
5076 it->paragraph_embedding = L2R;
5077
5078 /* Set up the bidi iterator for this display string. */
5079 if (it->bidi_p)
5080 {
5081 it->bidi_it.string.lstring = it->string;
5082 it->bidi_it.string.s = NULL;
5083 it->bidi_it.string.schars = it->end_charpos;
5084 it->bidi_it.string.bufpos = bufpos;
5085 it->bidi_it.string.from_disp_str = 1;
5086 it->bidi_it.string.unibyte = !it->multibyte_p;
5087 it->bidi_it.w = it->w;
5088 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5089 }
5090 }
5091 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5092 {
5093 it->method = GET_FROM_STRETCH;
5094 it->object = value;
5095 *position = it->position = start_pos;
5096 retval = 1 + (it->area == TEXT_AREA);
5097 }
5098 #ifdef HAVE_WINDOW_SYSTEM
5099 else
5100 {
5101 it->what = IT_IMAGE;
5102 it->image_id = lookup_image (it->f, value);
5103 it->position = start_pos;
5104 it->object = NILP (object) ? it->w->contents : object;
5105 it->method = GET_FROM_IMAGE;
5106
5107 /* Say that we haven't consumed the characters with
5108 `display' property yet. The call to pop_it in
5109 set_iterator_to_next will clean this up. */
5110 *position = start_pos;
5111 }
5112 #endif /* HAVE_WINDOW_SYSTEM */
5113
5114 return retval;
5115 }
5116
5117 /* Invalid property or property not supported. Restore
5118 POSITION to what it was before. */
5119 *position = start_pos;
5120 return 0;
5121 }
5122
5123 /* Check if PROP is a display property value whose text should be
5124 treated as intangible. OVERLAY is the overlay from which PROP
5125 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5126 specify the buffer position covered by PROP. */
5127
5128 int
5129 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5130 ptrdiff_t charpos, ptrdiff_t bytepos)
5131 {
5132 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5133 struct text_pos position;
5134
5135 SET_TEXT_POS (position, charpos, bytepos);
5136 return handle_display_spec (NULL, prop, Qnil, overlay,
5137 &position, charpos, frame_window_p);
5138 }
5139
5140
5141 /* Return 1 if PROP is a display sub-property value containing STRING.
5142
5143 Implementation note: this and the following function are really
5144 special cases of handle_display_spec and
5145 handle_single_display_spec, and should ideally use the same code.
5146 Until they do, these two pairs must be consistent and must be
5147 modified in sync. */
5148
5149 static int
5150 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5151 {
5152 if (EQ (string, prop))
5153 return 1;
5154
5155 /* Skip over `when FORM'. */
5156 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5157 {
5158 prop = XCDR (prop);
5159 if (!CONSP (prop))
5160 return 0;
5161 /* Actually, the condition following `when' should be eval'ed,
5162 like handle_single_display_spec does, and we should return
5163 zero if it evaluates to nil. However, this function is
5164 called only when the buffer was already displayed and some
5165 glyph in the glyph matrix was found to come from a display
5166 string. Therefore, the condition was already evaluated, and
5167 the result was non-nil, otherwise the display string wouldn't
5168 have been displayed and we would have never been called for
5169 this property. Thus, we can skip the evaluation and assume
5170 its result is non-nil. */
5171 prop = XCDR (prop);
5172 }
5173
5174 if (CONSP (prop))
5175 /* Skip over `margin LOCATION'. */
5176 if (EQ (XCAR (prop), Qmargin))
5177 {
5178 prop = XCDR (prop);
5179 if (!CONSP (prop))
5180 return 0;
5181
5182 prop = XCDR (prop);
5183 if (!CONSP (prop))
5184 return 0;
5185 }
5186
5187 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5188 }
5189
5190
5191 /* Return 1 if STRING appears in the `display' property PROP. */
5192
5193 static int
5194 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5195 {
5196 if (CONSP (prop)
5197 && !EQ (XCAR (prop), Qwhen)
5198 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5199 {
5200 /* A list of sub-properties. */
5201 while (CONSP (prop))
5202 {
5203 if (single_display_spec_string_p (XCAR (prop), string))
5204 return 1;
5205 prop = XCDR (prop);
5206 }
5207 }
5208 else if (VECTORP (prop))
5209 {
5210 /* A vector of sub-properties. */
5211 ptrdiff_t i;
5212 for (i = 0; i < ASIZE (prop); ++i)
5213 if (single_display_spec_string_p (AREF (prop, i), string))
5214 return 1;
5215 }
5216 else
5217 return single_display_spec_string_p (prop, string);
5218
5219 return 0;
5220 }
5221
5222 /* Look for STRING in overlays and text properties in the current
5223 buffer, between character positions FROM and TO (excluding TO).
5224 BACK_P non-zero means look back (in this case, TO is supposed to be
5225 less than FROM).
5226 Value is the first character position where STRING was found, or
5227 zero if it wasn't found before hitting TO.
5228
5229 This function may only use code that doesn't eval because it is
5230 called asynchronously from note_mouse_highlight. */
5231
5232 static ptrdiff_t
5233 string_buffer_position_lim (Lisp_Object string,
5234 ptrdiff_t from, ptrdiff_t to, int back_p)
5235 {
5236 Lisp_Object limit, prop, pos;
5237 int found = 0;
5238
5239 pos = make_number (max (from, BEGV));
5240
5241 if (!back_p) /* looking forward */
5242 {
5243 limit = make_number (min (to, ZV));
5244 while (!found && !EQ (pos, limit))
5245 {
5246 prop = Fget_char_property (pos, Qdisplay, Qnil);
5247 if (!NILP (prop) && display_prop_string_p (prop, string))
5248 found = 1;
5249 else
5250 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5251 limit);
5252 }
5253 }
5254 else /* looking back */
5255 {
5256 limit = make_number (max (to, BEGV));
5257 while (!found && !EQ (pos, limit))
5258 {
5259 prop = Fget_char_property (pos, Qdisplay, Qnil);
5260 if (!NILP (prop) && display_prop_string_p (prop, string))
5261 found = 1;
5262 else
5263 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5264 limit);
5265 }
5266 }
5267
5268 return found ? XINT (pos) : 0;
5269 }
5270
5271 /* Determine which buffer position in current buffer STRING comes from.
5272 AROUND_CHARPOS is an approximate position where it could come from.
5273 Value is the buffer position or 0 if it couldn't be determined.
5274
5275 This function is necessary because we don't record buffer positions
5276 in glyphs generated from strings (to keep struct glyph small).
5277 This function may only use code that doesn't eval because it is
5278 called asynchronously from note_mouse_highlight. */
5279
5280 static ptrdiff_t
5281 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5282 {
5283 const int MAX_DISTANCE = 1000;
5284 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5285 around_charpos + MAX_DISTANCE,
5286 0);
5287
5288 if (!found)
5289 found = string_buffer_position_lim (string, around_charpos,
5290 around_charpos - MAX_DISTANCE, 1);
5291 return found;
5292 }
5293
5294
5295 \f
5296 /***********************************************************************
5297 `composition' property
5298 ***********************************************************************/
5299
5300 /* Set up iterator IT from `composition' property at its current
5301 position. Called from handle_stop. */
5302
5303 static enum prop_handled
5304 handle_composition_prop (struct it *it)
5305 {
5306 Lisp_Object prop, string;
5307 ptrdiff_t pos, pos_byte, start, end;
5308
5309 if (STRINGP (it->string))
5310 {
5311 unsigned char *s;
5312
5313 pos = IT_STRING_CHARPOS (*it);
5314 pos_byte = IT_STRING_BYTEPOS (*it);
5315 string = it->string;
5316 s = SDATA (string) + pos_byte;
5317 it->c = STRING_CHAR (s);
5318 }
5319 else
5320 {
5321 pos = IT_CHARPOS (*it);
5322 pos_byte = IT_BYTEPOS (*it);
5323 string = Qnil;
5324 it->c = FETCH_CHAR (pos_byte);
5325 }
5326
5327 /* If there's a valid composition and point is not inside of the
5328 composition (in the case that the composition is from the current
5329 buffer), draw a glyph composed from the composition components. */
5330 if (find_composition (pos, -1, &start, &end, &prop, string)
5331 && composition_valid_p (start, end, prop)
5332 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5333 {
5334 if (start < pos)
5335 /* As we can't handle this situation (perhaps font-lock added
5336 a new composition), we just return here hoping that next
5337 redisplay will detect this composition much earlier. */
5338 return HANDLED_NORMALLY;
5339 if (start != pos)
5340 {
5341 if (STRINGP (it->string))
5342 pos_byte = string_char_to_byte (it->string, start);
5343 else
5344 pos_byte = CHAR_TO_BYTE (start);
5345 }
5346 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5347 prop, string);
5348
5349 if (it->cmp_it.id >= 0)
5350 {
5351 it->cmp_it.ch = -1;
5352 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5353 it->cmp_it.nglyphs = -1;
5354 }
5355 }
5356
5357 return HANDLED_NORMALLY;
5358 }
5359
5360
5361 \f
5362 /***********************************************************************
5363 Overlay strings
5364 ***********************************************************************/
5365
5366 /* The following structure is used to record overlay strings for
5367 later sorting in load_overlay_strings. */
5368
5369 struct overlay_entry
5370 {
5371 Lisp_Object overlay;
5372 Lisp_Object string;
5373 EMACS_INT priority;
5374 int after_string_p;
5375 };
5376
5377
5378 /* Set up iterator IT from overlay strings at its current position.
5379 Called from handle_stop. */
5380
5381 static enum prop_handled
5382 handle_overlay_change (struct it *it)
5383 {
5384 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5385 return HANDLED_RECOMPUTE_PROPS;
5386 else
5387 return HANDLED_NORMALLY;
5388 }
5389
5390
5391 /* Set up the next overlay string for delivery by IT, if there is an
5392 overlay string to deliver. Called by set_iterator_to_next when the
5393 end of the current overlay string is reached. If there are more
5394 overlay strings to display, IT->string and
5395 IT->current.overlay_string_index are set appropriately here.
5396 Otherwise IT->string is set to nil. */
5397
5398 static void
5399 next_overlay_string (struct it *it)
5400 {
5401 ++it->current.overlay_string_index;
5402 if (it->current.overlay_string_index == it->n_overlay_strings)
5403 {
5404 /* No more overlay strings. Restore IT's settings to what
5405 they were before overlay strings were processed, and
5406 continue to deliver from current_buffer. */
5407
5408 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5409 pop_it (it);
5410 eassert (it->sp > 0
5411 || (NILP (it->string)
5412 && it->method == GET_FROM_BUFFER
5413 && it->stop_charpos >= BEGV
5414 && it->stop_charpos <= it->end_charpos));
5415 it->current.overlay_string_index = -1;
5416 it->n_overlay_strings = 0;
5417 it->overlay_strings_charpos = -1;
5418 /* If there's an empty display string on the stack, pop the
5419 stack, to resync the bidi iterator with IT's position. Such
5420 empty strings are pushed onto the stack in
5421 get_overlay_strings_1. */
5422 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5423 pop_it (it);
5424
5425 /* If we're at the end of the buffer, record that we have
5426 processed the overlay strings there already, so that
5427 next_element_from_buffer doesn't try it again. */
5428 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5429 it->overlay_strings_at_end_processed_p = true;
5430 }
5431 else
5432 {
5433 /* There are more overlay strings to process. If
5434 IT->current.overlay_string_index has advanced to a position
5435 where we must load IT->overlay_strings with more strings, do
5436 it. We must load at the IT->overlay_strings_charpos where
5437 IT->n_overlay_strings was originally computed; when invisible
5438 text is present, this might not be IT_CHARPOS (Bug#7016). */
5439 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5440
5441 if (it->current.overlay_string_index && i == 0)
5442 load_overlay_strings (it, it->overlay_strings_charpos);
5443
5444 /* Initialize IT to deliver display elements from the overlay
5445 string. */
5446 it->string = it->overlay_strings[i];
5447 it->multibyte_p = STRING_MULTIBYTE (it->string);
5448 SET_TEXT_POS (it->current.string_pos, 0, 0);
5449 it->method = GET_FROM_STRING;
5450 it->stop_charpos = 0;
5451 it->end_charpos = SCHARS (it->string);
5452 if (it->cmp_it.stop_pos >= 0)
5453 it->cmp_it.stop_pos = 0;
5454 it->prev_stop = 0;
5455 it->base_level_stop = 0;
5456
5457 /* Set up the bidi iterator for this overlay string. */
5458 if (it->bidi_p)
5459 {
5460 it->bidi_it.string.lstring = it->string;
5461 it->bidi_it.string.s = NULL;
5462 it->bidi_it.string.schars = SCHARS (it->string);
5463 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5464 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5465 it->bidi_it.string.unibyte = !it->multibyte_p;
5466 it->bidi_it.w = it->w;
5467 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5468 }
5469 }
5470
5471 CHECK_IT (it);
5472 }
5473
5474
5475 /* Compare two overlay_entry structures E1 and E2. Used as a
5476 comparison function for qsort in load_overlay_strings. Overlay
5477 strings for the same position are sorted so that
5478
5479 1. All after-strings come in front of before-strings, except
5480 when they come from the same overlay.
5481
5482 2. Within after-strings, strings are sorted so that overlay strings
5483 from overlays with higher priorities come first.
5484
5485 2. Within before-strings, strings are sorted so that overlay
5486 strings from overlays with higher priorities come last.
5487
5488 Value is analogous to strcmp. */
5489
5490
5491 static int
5492 compare_overlay_entries (const void *e1, const void *e2)
5493 {
5494 struct overlay_entry const *entry1 = e1;
5495 struct overlay_entry const *entry2 = e2;
5496 int result;
5497
5498 if (entry1->after_string_p != entry2->after_string_p)
5499 {
5500 /* Let after-strings appear in front of before-strings if
5501 they come from different overlays. */
5502 if (EQ (entry1->overlay, entry2->overlay))
5503 result = entry1->after_string_p ? 1 : -1;
5504 else
5505 result = entry1->after_string_p ? -1 : 1;
5506 }
5507 else if (entry1->priority != entry2->priority)
5508 {
5509 if (entry1->after_string_p)
5510 /* After-strings sorted in order of decreasing priority. */
5511 result = entry2->priority < entry1->priority ? -1 : 1;
5512 else
5513 /* Before-strings sorted in order of increasing priority. */
5514 result = entry1->priority < entry2->priority ? -1 : 1;
5515 }
5516 else
5517 result = 0;
5518
5519 return result;
5520 }
5521
5522
5523 /* Load the vector IT->overlay_strings with overlay strings from IT's
5524 current buffer position, or from CHARPOS if that is > 0. Set
5525 IT->n_overlays to the total number of overlay strings found.
5526
5527 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5528 a time. On entry into load_overlay_strings,
5529 IT->current.overlay_string_index gives the number of overlay
5530 strings that have already been loaded by previous calls to this
5531 function.
5532
5533 IT->add_overlay_start contains an additional overlay start
5534 position to consider for taking overlay strings from, if non-zero.
5535 This position comes into play when the overlay has an `invisible'
5536 property, and both before and after-strings. When we've skipped to
5537 the end of the overlay, because of its `invisible' property, we
5538 nevertheless want its before-string to appear.
5539 IT->add_overlay_start will contain the overlay start position
5540 in this case.
5541
5542 Overlay strings are sorted so that after-string strings come in
5543 front of before-string strings. Within before and after-strings,
5544 strings are sorted by overlay priority. See also function
5545 compare_overlay_entries. */
5546
5547 static void
5548 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5549 {
5550 Lisp_Object overlay, window, str, invisible;
5551 struct Lisp_Overlay *ov;
5552 ptrdiff_t start, end;
5553 ptrdiff_t size = 20;
5554 ptrdiff_t n = 0, i, j;
5555 int invis_p;
5556 struct overlay_entry *entries = alloca (size * sizeof *entries);
5557 USE_SAFE_ALLOCA;
5558
5559 if (charpos <= 0)
5560 charpos = IT_CHARPOS (*it);
5561
5562 /* Append the overlay string STRING of overlay OVERLAY to vector
5563 `entries' which has size `size' and currently contains `n'
5564 elements. AFTER_P non-zero means STRING is an after-string of
5565 OVERLAY. */
5566 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5567 do \
5568 { \
5569 Lisp_Object priority; \
5570 \
5571 if (n == size) \
5572 { \
5573 struct overlay_entry *old = entries; \
5574 SAFE_NALLOCA (entries, 2, size); \
5575 memcpy (entries, old, size * sizeof *entries); \
5576 size *= 2; \
5577 } \
5578 \
5579 entries[n].string = (STRING); \
5580 entries[n].overlay = (OVERLAY); \
5581 priority = Foverlay_get ((OVERLAY), Qpriority); \
5582 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5583 entries[n].after_string_p = (AFTER_P); \
5584 ++n; \
5585 } \
5586 while (0)
5587
5588 /* Process overlay before the overlay center. */
5589 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5590 {
5591 XSETMISC (overlay, ov);
5592 eassert (OVERLAYP (overlay));
5593 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5594 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5595
5596 if (end < charpos)
5597 break;
5598
5599 /* Skip this overlay if it doesn't start or end at IT's current
5600 position. */
5601 if (end != charpos && start != charpos)
5602 continue;
5603
5604 /* Skip this overlay if it doesn't apply to IT->w. */
5605 window = Foverlay_get (overlay, Qwindow);
5606 if (WINDOWP (window) && XWINDOW (window) != it->w)
5607 continue;
5608
5609 /* If the text ``under'' the overlay is invisible, both before-
5610 and after-strings from this overlay are visible; start and
5611 end position are indistinguishable. */
5612 invisible = Foverlay_get (overlay, Qinvisible);
5613 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5614
5615 /* If overlay has a non-empty before-string, record it. */
5616 if ((start == charpos || (end == charpos && invis_p))
5617 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5618 && SCHARS (str))
5619 RECORD_OVERLAY_STRING (overlay, str, 0);
5620
5621 /* If overlay has a non-empty after-string, record it. */
5622 if ((end == charpos || (start == charpos && invis_p))
5623 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5624 && SCHARS (str))
5625 RECORD_OVERLAY_STRING (overlay, str, 1);
5626 }
5627
5628 /* Process overlays after the overlay center. */
5629 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5630 {
5631 XSETMISC (overlay, ov);
5632 eassert (OVERLAYP (overlay));
5633 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5634 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5635
5636 if (start > charpos)
5637 break;
5638
5639 /* Skip this overlay if it doesn't start or end at IT's current
5640 position. */
5641 if (end != charpos && start != charpos)
5642 continue;
5643
5644 /* Skip this overlay if it doesn't apply to IT->w. */
5645 window = Foverlay_get (overlay, Qwindow);
5646 if (WINDOWP (window) && XWINDOW (window) != it->w)
5647 continue;
5648
5649 /* If the text ``under'' the overlay is invisible, it has a zero
5650 dimension, and both before- and after-strings apply. */
5651 invisible = Foverlay_get (overlay, Qinvisible);
5652 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5653
5654 /* If overlay has a non-empty before-string, record it. */
5655 if ((start == charpos || (end == charpos && invis_p))
5656 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5657 && SCHARS (str))
5658 RECORD_OVERLAY_STRING (overlay, str, 0);
5659
5660 /* If overlay has a non-empty after-string, record it. */
5661 if ((end == charpos || (start == charpos && invis_p))
5662 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5663 && SCHARS (str))
5664 RECORD_OVERLAY_STRING (overlay, str, 1);
5665 }
5666
5667 #undef RECORD_OVERLAY_STRING
5668
5669 /* Sort entries. */
5670 if (n > 1)
5671 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5672
5673 /* Record number of overlay strings, and where we computed it. */
5674 it->n_overlay_strings = n;
5675 it->overlay_strings_charpos = charpos;
5676
5677 /* IT->current.overlay_string_index is the number of overlay strings
5678 that have already been consumed by IT. Copy some of the
5679 remaining overlay strings to IT->overlay_strings. */
5680 i = 0;
5681 j = it->current.overlay_string_index;
5682 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5683 {
5684 it->overlay_strings[i] = entries[j].string;
5685 it->string_overlays[i++] = entries[j++].overlay;
5686 }
5687
5688 CHECK_IT (it);
5689 SAFE_FREE ();
5690 }
5691
5692
5693 /* Get the first chunk of overlay strings at IT's current buffer
5694 position, or at CHARPOS if that is > 0. Value is non-zero if at
5695 least one overlay string was found. */
5696
5697 static int
5698 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5699 {
5700 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5701 process. This fills IT->overlay_strings with strings, and sets
5702 IT->n_overlay_strings to the total number of strings to process.
5703 IT->pos.overlay_string_index has to be set temporarily to zero
5704 because load_overlay_strings needs this; it must be set to -1
5705 when no overlay strings are found because a zero value would
5706 indicate a position in the first overlay string. */
5707 it->current.overlay_string_index = 0;
5708 load_overlay_strings (it, charpos);
5709
5710 /* If we found overlay strings, set up IT to deliver display
5711 elements from the first one. Otherwise set up IT to deliver
5712 from current_buffer. */
5713 if (it->n_overlay_strings)
5714 {
5715 /* Make sure we know settings in current_buffer, so that we can
5716 restore meaningful values when we're done with the overlay
5717 strings. */
5718 if (compute_stop_p)
5719 compute_stop_pos (it);
5720 eassert (it->face_id >= 0);
5721
5722 /* Save IT's settings. They are restored after all overlay
5723 strings have been processed. */
5724 eassert (!compute_stop_p || it->sp == 0);
5725
5726 /* When called from handle_stop, there might be an empty display
5727 string loaded. In that case, don't bother saving it. But
5728 don't use this optimization with the bidi iterator, since we
5729 need the corresponding pop_it call to resync the bidi
5730 iterator's position with IT's position, after we are done
5731 with the overlay strings. (The corresponding call to pop_it
5732 in case of an empty display string is in
5733 next_overlay_string.) */
5734 if (!(!it->bidi_p
5735 && STRINGP (it->string) && !SCHARS (it->string)))
5736 push_it (it, NULL);
5737
5738 /* Set up IT to deliver display elements from the first overlay
5739 string. */
5740 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5741 it->string = it->overlay_strings[0];
5742 it->from_overlay = Qnil;
5743 it->stop_charpos = 0;
5744 eassert (STRINGP (it->string));
5745 it->end_charpos = SCHARS (it->string);
5746 it->prev_stop = 0;
5747 it->base_level_stop = 0;
5748 it->multibyte_p = STRING_MULTIBYTE (it->string);
5749 it->method = GET_FROM_STRING;
5750 it->from_disp_prop_p = 0;
5751
5752 /* Force paragraph direction to be that of the parent
5753 buffer. */
5754 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5755 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5756 else
5757 it->paragraph_embedding = L2R;
5758
5759 /* Set up the bidi iterator for this overlay string. */
5760 if (it->bidi_p)
5761 {
5762 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5763
5764 it->bidi_it.string.lstring = it->string;
5765 it->bidi_it.string.s = NULL;
5766 it->bidi_it.string.schars = SCHARS (it->string);
5767 it->bidi_it.string.bufpos = pos;
5768 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5769 it->bidi_it.string.unibyte = !it->multibyte_p;
5770 it->bidi_it.w = it->w;
5771 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5772 }
5773 return 1;
5774 }
5775
5776 it->current.overlay_string_index = -1;
5777 return 0;
5778 }
5779
5780 static int
5781 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5782 {
5783 it->string = Qnil;
5784 it->method = GET_FROM_BUFFER;
5785
5786 (void) get_overlay_strings_1 (it, charpos, 1);
5787
5788 CHECK_IT (it);
5789
5790 /* Value is non-zero if we found at least one overlay string. */
5791 return STRINGP (it->string);
5792 }
5793
5794
5795 \f
5796 /***********************************************************************
5797 Saving and restoring state
5798 ***********************************************************************/
5799
5800 /* Save current settings of IT on IT->stack. Called, for example,
5801 before setting up IT for an overlay string, to be able to restore
5802 IT's settings to what they were after the overlay string has been
5803 processed. If POSITION is non-NULL, it is the position to save on
5804 the stack instead of IT->position. */
5805
5806 static void
5807 push_it (struct it *it, struct text_pos *position)
5808 {
5809 struct iterator_stack_entry *p;
5810
5811 eassert (it->sp < IT_STACK_SIZE);
5812 p = it->stack + it->sp;
5813
5814 p->stop_charpos = it->stop_charpos;
5815 p->prev_stop = it->prev_stop;
5816 p->base_level_stop = it->base_level_stop;
5817 p->cmp_it = it->cmp_it;
5818 eassert (it->face_id >= 0);
5819 p->face_id = it->face_id;
5820 p->string = it->string;
5821 p->method = it->method;
5822 p->from_overlay = it->from_overlay;
5823 switch (p->method)
5824 {
5825 case GET_FROM_IMAGE:
5826 p->u.image.object = it->object;
5827 p->u.image.image_id = it->image_id;
5828 p->u.image.slice = it->slice;
5829 break;
5830 case GET_FROM_STRETCH:
5831 p->u.stretch.object = it->object;
5832 break;
5833 }
5834 p->position = position ? *position : it->position;
5835 p->current = it->current;
5836 p->end_charpos = it->end_charpos;
5837 p->string_nchars = it->string_nchars;
5838 p->area = it->area;
5839 p->multibyte_p = it->multibyte_p;
5840 p->avoid_cursor_p = it->avoid_cursor_p;
5841 p->space_width = it->space_width;
5842 p->font_height = it->font_height;
5843 p->voffset = it->voffset;
5844 p->string_from_display_prop_p = it->string_from_display_prop_p;
5845 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5846 p->display_ellipsis_p = 0;
5847 p->line_wrap = it->line_wrap;
5848 p->bidi_p = it->bidi_p;
5849 p->paragraph_embedding = it->paragraph_embedding;
5850 p->from_disp_prop_p = it->from_disp_prop_p;
5851 ++it->sp;
5852
5853 /* Save the state of the bidi iterator as well. */
5854 if (it->bidi_p)
5855 bidi_push_it (&it->bidi_it);
5856 }
5857
5858 static void
5859 iterate_out_of_display_property (struct it *it)
5860 {
5861 int buffer_p = !STRINGP (it->string);
5862 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5863 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5864
5865 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5866
5867 /* Maybe initialize paragraph direction. If we are at the beginning
5868 of a new paragraph, next_element_from_buffer may not have a
5869 chance to do that. */
5870 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5871 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5872 /* prev_stop can be zero, so check against BEGV as well. */
5873 while (it->bidi_it.charpos >= bob
5874 && it->prev_stop <= it->bidi_it.charpos
5875 && it->bidi_it.charpos < CHARPOS (it->position)
5876 && it->bidi_it.charpos < eob)
5877 bidi_move_to_visually_next (&it->bidi_it);
5878 /* Record the stop_pos we just crossed, for when we cross it
5879 back, maybe. */
5880 if (it->bidi_it.charpos > CHARPOS (it->position))
5881 it->prev_stop = CHARPOS (it->position);
5882 /* If we ended up not where pop_it put us, resync IT's
5883 positional members with the bidi iterator. */
5884 if (it->bidi_it.charpos != CHARPOS (it->position))
5885 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5886 if (buffer_p)
5887 it->current.pos = it->position;
5888 else
5889 it->current.string_pos = it->position;
5890 }
5891
5892 /* Restore IT's settings from IT->stack. Called, for example, when no
5893 more overlay strings must be processed, and we return to delivering
5894 display elements from a buffer, or when the end of a string from a
5895 `display' property is reached and we return to delivering display
5896 elements from an overlay string, or from a buffer. */
5897
5898 static void
5899 pop_it (struct it *it)
5900 {
5901 struct iterator_stack_entry *p;
5902 int from_display_prop = it->from_disp_prop_p;
5903
5904 eassert (it->sp > 0);
5905 --it->sp;
5906 p = it->stack + it->sp;
5907 it->stop_charpos = p->stop_charpos;
5908 it->prev_stop = p->prev_stop;
5909 it->base_level_stop = p->base_level_stop;
5910 it->cmp_it = p->cmp_it;
5911 it->face_id = p->face_id;
5912 it->current = p->current;
5913 it->position = p->position;
5914 it->string = p->string;
5915 it->from_overlay = p->from_overlay;
5916 if (NILP (it->string))
5917 SET_TEXT_POS (it->current.string_pos, -1, -1);
5918 it->method = p->method;
5919 switch (it->method)
5920 {
5921 case GET_FROM_IMAGE:
5922 it->image_id = p->u.image.image_id;
5923 it->object = p->u.image.object;
5924 it->slice = p->u.image.slice;
5925 break;
5926 case GET_FROM_STRETCH:
5927 it->object = p->u.stretch.object;
5928 break;
5929 case GET_FROM_BUFFER:
5930 it->object = it->w->contents;
5931 break;
5932 case GET_FROM_STRING:
5933 it->object = it->string;
5934 break;
5935 case GET_FROM_DISPLAY_VECTOR:
5936 if (it->s)
5937 it->method = GET_FROM_C_STRING;
5938 else if (STRINGP (it->string))
5939 it->method = GET_FROM_STRING;
5940 else
5941 {
5942 it->method = GET_FROM_BUFFER;
5943 it->object = it->w->contents;
5944 }
5945 }
5946 it->end_charpos = p->end_charpos;
5947 it->string_nchars = p->string_nchars;
5948 it->area = p->area;
5949 it->multibyte_p = p->multibyte_p;
5950 it->avoid_cursor_p = p->avoid_cursor_p;
5951 it->space_width = p->space_width;
5952 it->font_height = p->font_height;
5953 it->voffset = p->voffset;
5954 it->string_from_display_prop_p = p->string_from_display_prop_p;
5955 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5956 it->line_wrap = p->line_wrap;
5957 it->bidi_p = p->bidi_p;
5958 it->paragraph_embedding = p->paragraph_embedding;
5959 it->from_disp_prop_p = p->from_disp_prop_p;
5960 if (it->bidi_p)
5961 {
5962 bidi_pop_it (&it->bidi_it);
5963 /* Bidi-iterate until we get out of the portion of text, if any,
5964 covered by a `display' text property or by an overlay with
5965 `display' property. (We cannot just jump there, because the
5966 internal coherency of the bidi iterator state can not be
5967 preserved across such jumps.) We also must determine the
5968 paragraph base direction if the overlay we just processed is
5969 at the beginning of a new paragraph. */
5970 if (from_display_prop
5971 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5972 iterate_out_of_display_property (it);
5973
5974 eassert ((BUFFERP (it->object)
5975 && IT_CHARPOS (*it) == it->bidi_it.charpos
5976 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5977 || (STRINGP (it->object)
5978 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5979 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5980 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5981 }
5982 }
5983
5984
5985 \f
5986 /***********************************************************************
5987 Moving over lines
5988 ***********************************************************************/
5989
5990 /* Set IT's current position to the previous line start. */
5991
5992 static void
5993 back_to_previous_line_start (struct it *it)
5994 {
5995 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5996
5997 DEC_BOTH (cp, bp);
5998 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5999 }
6000
6001
6002 /* Move IT to the next line start.
6003
6004 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6005 we skipped over part of the text (as opposed to moving the iterator
6006 continuously over the text). Otherwise, don't change the value
6007 of *SKIPPED_P.
6008
6009 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6010 iterator on the newline, if it was found.
6011
6012 Newlines may come from buffer text, overlay strings, or strings
6013 displayed via the `display' property. That's the reason we can't
6014 simply use find_newline_no_quit.
6015
6016 Note that this function may not skip over invisible text that is so
6017 because of text properties and immediately follows a newline. If
6018 it would, function reseat_at_next_visible_line_start, when called
6019 from set_iterator_to_next, would effectively make invisible
6020 characters following a newline part of the wrong glyph row, which
6021 leads to wrong cursor motion. */
6022
6023 static int
6024 forward_to_next_line_start (struct it *it, int *skipped_p,
6025 struct bidi_it *bidi_it_prev)
6026 {
6027 ptrdiff_t old_selective;
6028 int newline_found_p, n;
6029 const int MAX_NEWLINE_DISTANCE = 500;
6030
6031 /* If already on a newline, just consume it to avoid unintended
6032 skipping over invisible text below. */
6033 if (it->what == IT_CHARACTER
6034 && it->c == '\n'
6035 && CHARPOS (it->position) == IT_CHARPOS (*it))
6036 {
6037 if (it->bidi_p && bidi_it_prev)
6038 *bidi_it_prev = it->bidi_it;
6039 set_iterator_to_next (it, 0);
6040 it->c = 0;
6041 return 1;
6042 }
6043
6044 /* Don't handle selective display in the following. It's (a)
6045 unnecessary because it's done by the caller, and (b) leads to an
6046 infinite recursion because next_element_from_ellipsis indirectly
6047 calls this function. */
6048 old_selective = it->selective;
6049 it->selective = 0;
6050
6051 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6052 from buffer text. */
6053 for (n = newline_found_p = 0;
6054 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6055 n += STRINGP (it->string) ? 0 : 1)
6056 {
6057 if (!get_next_display_element (it))
6058 return 0;
6059 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6060 if (newline_found_p && it->bidi_p && bidi_it_prev)
6061 *bidi_it_prev = it->bidi_it;
6062 set_iterator_to_next (it, 0);
6063 }
6064
6065 /* If we didn't find a newline near enough, see if we can use a
6066 short-cut. */
6067 if (!newline_found_p)
6068 {
6069 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6070 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6071 1, &bytepos);
6072 Lisp_Object pos;
6073
6074 eassert (!STRINGP (it->string));
6075
6076 /* If there isn't any `display' property in sight, and no
6077 overlays, we can just use the position of the newline in
6078 buffer text. */
6079 if (it->stop_charpos >= limit
6080 || ((pos = Fnext_single_property_change (make_number (start),
6081 Qdisplay, Qnil,
6082 make_number (limit)),
6083 NILP (pos))
6084 && next_overlay_change (start) == ZV))
6085 {
6086 if (!it->bidi_p)
6087 {
6088 IT_CHARPOS (*it) = limit;
6089 IT_BYTEPOS (*it) = bytepos;
6090 }
6091 else
6092 {
6093 struct bidi_it bprev;
6094
6095 /* Help bidi.c avoid expensive searches for display
6096 properties and overlays, by telling it that there are
6097 none up to `limit'. */
6098 if (it->bidi_it.disp_pos < limit)
6099 {
6100 it->bidi_it.disp_pos = limit;
6101 it->bidi_it.disp_prop = 0;
6102 }
6103 do {
6104 bprev = it->bidi_it;
6105 bidi_move_to_visually_next (&it->bidi_it);
6106 } while (it->bidi_it.charpos != limit);
6107 IT_CHARPOS (*it) = limit;
6108 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6109 if (bidi_it_prev)
6110 *bidi_it_prev = bprev;
6111 }
6112 *skipped_p = newline_found_p = true;
6113 }
6114 else
6115 {
6116 while (get_next_display_element (it)
6117 && !newline_found_p)
6118 {
6119 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6120 if (newline_found_p && it->bidi_p && bidi_it_prev)
6121 *bidi_it_prev = it->bidi_it;
6122 set_iterator_to_next (it, 0);
6123 }
6124 }
6125 }
6126
6127 it->selective = old_selective;
6128 return newline_found_p;
6129 }
6130
6131
6132 /* Set IT's current position to the previous visible line start. Skip
6133 invisible text that is so either due to text properties or due to
6134 selective display. Caution: this does not change IT->current_x and
6135 IT->hpos. */
6136
6137 static void
6138 back_to_previous_visible_line_start (struct it *it)
6139 {
6140 while (IT_CHARPOS (*it) > BEGV)
6141 {
6142 back_to_previous_line_start (it);
6143
6144 if (IT_CHARPOS (*it) <= BEGV)
6145 break;
6146
6147 /* If selective > 0, then lines indented more than its value are
6148 invisible. */
6149 if (it->selective > 0
6150 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6151 it->selective))
6152 continue;
6153
6154 /* Check the newline before point for invisibility. */
6155 {
6156 Lisp_Object prop;
6157 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6158 Qinvisible, it->window);
6159 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6160 continue;
6161 }
6162
6163 if (IT_CHARPOS (*it) <= BEGV)
6164 break;
6165
6166 {
6167 struct it it2;
6168 void *it2data = NULL;
6169 ptrdiff_t pos;
6170 ptrdiff_t beg, end;
6171 Lisp_Object val, overlay;
6172
6173 SAVE_IT (it2, *it, it2data);
6174
6175 /* If newline is part of a composition, continue from start of composition */
6176 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6177 && beg < IT_CHARPOS (*it))
6178 goto replaced;
6179
6180 /* If newline is replaced by a display property, find start of overlay
6181 or interval and continue search from that point. */
6182 pos = --IT_CHARPOS (it2);
6183 --IT_BYTEPOS (it2);
6184 it2.sp = 0;
6185 bidi_unshelve_cache (NULL, 0);
6186 it2.string_from_display_prop_p = 0;
6187 it2.from_disp_prop_p = 0;
6188 if (handle_display_prop (&it2) == HANDLED_RETURN
6189 && !NILP (val = get_char_property_and_overlay
6190 (make_number (pos), Qdisplay, Qnil, &overlay))
6191 && (OVERLAYP (overlay)
6192 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6193 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6194 {
6195 RESTORE_IT (it, it, it2data);
6196 goto replaced;
6197 }
6198
6199 /* Newline is not replaced by anything -- so we are done. */
6200 RESTORE_IT (it, it, it2data);
6201 break;
6202
6203 replaced:
6204 if (beg < BEGV)
6205 beg = BEGV;
6206 IT_CHARPOS (*it) = beg;
6207 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6208 }
6209 }
6210
6211 it->continuation_lines_width = 0;
6212
6213 eassert (IT_CHARPOS (*it) >= BEGV);
6214 eassert (IT_CHARPOS (*it) == BEGV
6215 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6216 CHECK_IT (it);
6217 }
6218
6219
6220 /* Reseat iterator IT at the previous visible line start. Skip
6221 invisible text that is so either due to text properties or due to
6222 selective display. At the end, update IT's overlay information,
6223 face information etc. */
6224
6225 void
6226 reseat_at_previous_visible_line_start (struct it *it)
6227 {
6228 back_to_previous_visible_line_start (it);
6229 reseat (it, it->current.pos, 1);
6230 CHECK_IT (it);
6231 }
6232
6233
6234 /* Reseat iterator IT on the next visible line start in the current
6235 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6236 preceding the line start. Skip over invisible text that is so
6237 because of selective display. Compute faces, overlays etc at the
6238 new position. Note that this function does not skip over text that
6239 is invisible because of text properties. */
6240
6241 static void
6242 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6243 {
6244 int newline_found_p, skipped_p = 0;
6245 struct bidi_it bidi_it_prev;
6246
6247 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6248
6249 /* Skip over lines that are invisible because they are indented
6250 more than the value of IT->selective. */
6251 if (it->selective > 0)
6252 while (IT_CHARPOS (*it) < ZV
6253 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6254 it->selective))
6255 {
6256 eassert (IT_BYTEPOS (*it) == BEGV
6257 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6258 newline_found_p =
6259 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6260 }
6261
6262 /* Position on the newline if that's what's requested. */
6263 if (on_newline_p && newline_found_p)
6264 {
6265 if (STRINGP (it->string))
6266 {
6267 if (IT_STRING_CHARPOS (*it) > 0)
6268 {
6269 if (!it->bidi_p)
6270 {
6271 --IT_STRING_CHARPOS (*it);
6272 --IT_STRING_BYTEPOS (*it);
6273 }
6274 else
6275 {
6276 /* We need to restore the bidi iterator to the state
6277 it had on the newline, and resync the IT's
6278 position with that. */
6279 it->bidi_it = bidi_it_prev;
6280 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6281 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6282 }
6283 }
6284 }
6285 else if (IT_CHARPOS (*it) > BEGV)
6286 {
6287 if (!it->bidi_p)
6288 {
6289 --IT_CHARPOS (*it);
6290 --IT_BYTEPOS (*it);
6291 }
6292 else
6293 {
6294 /* We need to restore the bidi iterator to the state it
6295 had on the newline and resync IT with that. */
6296 it->bidi_it = bidi_it_prev;
6297 IT_CHARPOS (*it) = it->bidi_it.charpos;
6298 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6299 }
6300 reseat (it, it->current.pos, 0);
6301 }
6302 }
6303 else if (skipped_p)
6304 reseat (it, it->current.pos, 0);
6305
6306 CHECK_IT (it);
6307 }
6308
6309
6310 \f
6311 /***********************************************************************
6312 Changing an iterator's position
6313 ***********************************************************************/
6314
6315 /* Change IT's current position to POS in current_buffer. If FORCE_P
6316 is non-zero, always check for text properties at the new position.
6317 Otherwise, text properties are only looked up if POS >=
6318 IT->check_charpos of a property. */
6319
6320 static void
6321 reseat (struct it *it, struct text_pos pos, int force_p)
6322 {
6323 ptrdiff_t original_pos = IT_CHARPOS (*it);
6324
6325 reseat_1 (it, pos, 0);
6326
6327 /* Determine where to check text properties. Avoid doing it
6328 where possible because text property lookup is very expensive. */
6329 if (force_p
6330 || CHARPOS (pos) > it->stop_charpos
6331 || CHARPOS (pos) < original_pos)
6332 {
6333 if (it->bidi_p)
6334 {
6335 /* For bidi iteration, we need to prime prev_stop and
6336 base_level_stop with our best estimations. */
6337 /* Implementation note: Of course, POS is not necessarily a
6338 stop position, so assigning prev_pos to it is a lie; we
6339 should have called compute_stop_backwards. However, if
6340 the current buffer does not include any R2L characters,
6341 that call would be a waste of cycles, because the
6342 iterator will never move back, and thus never cross this
6343 "fake" stop position. So we delay that backward search
6344 until the time we really need it, in next_element_from_buffer. */
6345 if (CHARPOS (pos) != it->prev_stop)
6346 it->prev_stop = CHARPOS (pos);
6347 if (CHARPOS (pos) < it->base_level_stop)
6348 it->base_level_stop = 0; /* meaning it's unknown */
6349 handle_stop (it);
6350 }
6351 else
6352 {
6353 handle_stop (it);
6354 it->prev_stop = it->base_level_stop = 0;
6355 }
6356
6357 }
6358
6359 CHECK_IT (it);
6360 }
6361
6362
6363 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6364 IT->stop_pos to POS, also. */
6365
6366 static void
6367 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6368 {
6369 /* Don't call this function when scanning a C string. */
6370 eassert (it->s == NULL);
6371
6372 /* POS must be a reasonable value. */
6373 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6374
6375 it->current.pos = it->position = pos;
6376 it->end_charpos = ZV;
6377 it->dpvec = NULL;
6378 it->current.dpvec_index = -1;
6379 it->current.overlay_string_index = -1;
6380 IT_STRING_CHARPOS (*it) = -1;
6381 IT_STRING_BYTEPOS (*it) = -1;
6382 it->string = Qnil;
6383 it->method = GET_FROM_BUFFER;
6384 it->object = it->w->contents;
6385 it->area = TEXT_AREA;
6386 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6387 it->sp = 0;
6388 it->string_from_display_prop_p = 0;
6389 it->string_from_prefix_prop_p = 0;
6390
6391 it->from_disp_prop_p = 0;
6392 it->face_before_selective_p = 0;
6393 if (it->bidi_p)
6394 {
6395 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6396 &it->bidi_it);
6397 bidi_unshelve_cache (NULL, 0);
6398 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6399 it->bidi_it.string.s = NULL;
6400 it->bidi_it.string.lstring = Qnil;
6401 it->bidi_it.string.bufpos = 0;
6402 it->bidi_it.string.unibyte = 0;
6403 it->bidi_it.w = it->w;
6404 }
6405
6406 if (set_stop_p)
6407 {
6408 it->stop_charpos = CHARPOS (pos);
6409 it->base_level_stop = CHARPOS (pos);
6410 }
6411 /* This make the information stored in it->cmp_it invalidate. */
6412 it->cmp_it.id = -1;
6413 }
6414
6415
6416 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6417 If S is non-null, it is a C string to iterate over. Otherwise,
6418 STRING gives a Lisp string to iterate over.
6419
6420 If PRECISION > 0, don't return more then PRECISION number of
6421 characters from the string.
6422
6423 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6424 characters have been returned. FIELD_WIDTH < 0 means an infinite
6425 field width.
6426
6427 MULTIBYTE = 0 means disable processing of multibyte characters,
6428 MULTIBYTE > 0 means enable it,
6429 MULTIBYTE < 0 means use IT->multibyte_p.
6430
6431 IT must be initialized via a prior call to init_iterator before
6432 calling this function. */
6433
6434 static void
6435 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6436 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6437 int multibyte)
6438 {
6439 /* No text property checks performed by default, but see below. */
6440 it->stop_charpos = -1;
6441
6442 /* Set iterator position and end position. */
6443 memset (&it->current, 0, sizeof it->current);
6444 it->current.overlay_string_index = -1;
6445 it->current.dpvec_index = -1;
6446 eassert (charpos >= 0);
6447
6448 /* If STRING is specified, use its multibyteness, otherwise use the
6449 setting of MULTIBYTE, if specified. */
6450 if (multibyte >= 0)
6451 it->multibyte_p = multibyte > 0;
6452
6453 /* Bidirectional reordering of strings is controlled by the default
6454 value of bidi-display-reordering. Don't try to reorder while
6455 loading loadup.el, as the necessary character property tables are
6456 not yet available. */
6457 it->bidi_p =
6458 NILP (Vpurify_flag)
6459 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6460
6461 if (s == NULL)
6462 {
6463 eassert (STRINGP (string));
6464 it->string = string;
6465 it->s = NULL;
6466 it->end_charpos = it->string_nchars = SCHARS (string);
6467 it->method = GET_FROM_STRING;
6468 it->current.string_pos = string_pos (charpos, string);
6469
6470 if (it->bidi_p)
6471 {
6472 it->bidi_it.string.lstring = string;
6473 it->bidi_it.string.s = NULL;
6474 it->bidi_it.string.schars = it->end_charpos;
6475 it->bidi_it.string.bufpos = 0;
6476 it->bidi_it.string.from_disp_str = 0;
6477 it->bidi_it.string.unibyte = !it->multibyte_p;
6478 it->bidi_it.w = it->w;
6479 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6480 FRAME_WINDOW_P (it->f), &it->bidi_it);
6481 }
6482 }
6483 else
6484 {
6485 it->s = (const unsigned char *) s;
6486 it->string = Qnil;
6487
6488 /* Note that we use IT->current.pos, not it->current.string_pos,
6489 for displaying C strings. */
6490 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6491 if (it->multibyte_p)
6492 {
6493 it->current.pos = c_string_pos (charpos, s, 1);
6494 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6495 }
6496 else
6497 {
6498 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6499 it->end_charpos = it->string_nchars = strlen (s);
6500 }
6501
6502 if (it->bidi_p)
6503 {
6504 it->bidi_it.string.lstring = Qnil;
6505 it->bidi_it.string.s = (const unsigned char *) s;
6506 it->bidi_it.string.schars = it->end_charpos;
6507 it->bidi_it.string.bufpos = 0;
6508 it->bidi_it.string.from_disp_str = 0;
6509 it->bidi_it.string.unibyte = !it->multibyte_p;
6510 it->bidi_it.w = it->w;
6511 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6512 &it->bidi_it);
6513 }
6514 it->method = GET_FROM_C_STRING;
6515 }
6516
6517 /* PRECISION > 0 means don't return more than PRECISION characters
6518 from the string. */
6519 if (precision > 0 && it->end_charpos - charpos > precision)
6520 {
6521 it->end_charpos = it->string_nchars = charpos + precision;
6522 if (it->bidi_p)
6523 it->bidi_it.string.schars = it->end_charpos;
6524 }
6525
6526 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6527 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6528 FIELD_WIDTH < 0 means infinite field width. This is useful for
6529 padding with `-' at the end of a mode line. */
6530 if (field_width < 0)
6531 field_width = INFINITY;
6532 /* Implementation note: We deliberately don't enlarge
6533 it->bidi_it.string.schars here to fit it->end_charpos, because
6534 the bidi iterator cannot produce characters out of thin air. */
6535 if (field_width > it->end_charpos - charpos)
6536 it->end_charpos = charpos + field_width;
6537
6538 /* Use the standard display table for displaying strings. */
6539 if (DISP_TABLE_P (Vstandard_display_table))
6540 it->dp = XCHAR_TABLE (Vstandard_display_table);
6541
6542 it->stop_charpos = charpos;
6543 it->prev_stop = charpos;
6544 it->base_level_stop = 0;
6545 if (it->bidi_p)
6546 {
6547 it->bidi_it.first_elt = 1;
6548 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6549 it->bidi_it.disp_pos = -1;
6550 }
6551 if (s == NULL && it->multibyte_p)
6552 {
6553 ptrdiff_t endpos = SCHARS (it->string);
6554 if (endpos > it->end_charpos)
6555 endpos = it->end_charpos;
6556 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6557 it->string);
6558 }
6559 CHECK_IT (it);
6560 }
6561
6562
6563 \f
6564 /***********************************************************************
6565 Iteration
6566 ***********************************************************************/
6567
6568 /* Map enum it_method value to corresponding next_element_from_* function. */
6569
6570 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6571 {
6572 next_element_from_buffer,
6573 next_element_from_display_vector,
6574 next_element_from_string,
6575 next_element_from_c_string,
6576 next_element_from_image,
6577 next_element_from_stretch
6578 };
6579
6580 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6581
6582
6583 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6584 (possibly with the following characters). */
6585
6586 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6587 ((IT)->cmp_it.id >= 0 \
6588 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6589 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6590 END_CHARPOS, (IT)->w, \
6591 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6592 (IT)->string)))
6593
6594
6595 /* Lookup the char-table Vglyphless_char_display for character C (-1
6596 if we want information for no-font case), and return the display
6597 method symbol. By side-effect, update it->what and
6598 it->glyphless_method. This function is called from
6599 get_next_display_element for each character element, and from
6600 x_produce_glyphs when no suitable font was found. */
6601
6602 Lisp_Object
6603 lookup_glyphless_char_display (int c, struct it *it)
6604 {
6605 Lisp_Object glyphless_method = Qnil;
6606
6607 if (CHAR_TABLE_P (Vglyphless_char_display)
6608 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6609 {
6610 if (c >= 0)
6611 {
6612 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6613 if (CONSP (glyphless_method))
6614 glyphless_method = FRAME_WINDOW_P (it->f)
6615 ? XCAR (glyphless_method)
6616 : XCDR (glyphless_method);
6617 }
6618 else
6619 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6620 }
6621
6622 retry:
6623 if (NILP (glyphless_method))
6624 {
6625 if (c >= 0)
6626 /* The default is to display the character by a proper font. */
6627 return Qnil;
6628 /* The default for the no-font case is to display an empty box. */
6629 glyphless_method = Qempty_box;
6630 }
6631 if (EQ (glyphless_method, Qzero_width))
6632 {
6633 if (c >= 0)
6634 return glyphless_method;
6635 /* This method can't be used for the no-font case. */
6636 glyphless_method = Qempty_box;
6637 }
6638 if (EQ (glyphless_method, Qthin_space))
6639 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6640 else if (EQ (glyphless_method, Qempty_box))
6641 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6642 else if (EQ (glyphless_method, Qhex_code))
6643 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6644 else if (STRINGP (glyphless_method))
6645 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6646 else
6647 {
6648 /* Invalid value. We use the default method. */
6649 glyphless_method = Qnil;
6650 goto retry;
6651 }
6652 it->what = IT_GLYPHLESS;
6653 return glyphless_method;
6654 }
6655
6656 /* Merge escape glyph face and cache the result. */
6657
6658 static struct frame *last_escape_glyph_frame = NULL;
6659 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6660 static int last_escape_glyph_merged_face_id = 0;
6661
6662 static int
6663 merge_escape_glyph_face (struct it *it)
6664 {
6665 int face_id;
6666
6667 if (it->f == last_escape_glyph_frame
6668 && it->face_id == last_escape_glyph_face_id)
6669 face_id = last_escape_glyph_merged_face_id;
6670 else
6671 {
6672 /* Merge the `escape-glyph' face into the current face. */
6673 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6674 last_escape_glyph_frame = it->f;
6675 last_escape_glyph_face_id = it->face_id;
6676 last_escape_glyph_merged_face_id = face_id;
6677 }
6678 return face_id;
6679 }
6680
6681 /* Likewise for glyphless glyph face. */
6682
6683 static struct frame *last_glyphless_glyph_frame = NULL;
6684 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6685 static int last_glyphless_glyph_merged_face_id = 0;
6686
6687 int
6688 merge_glyphless_glyph_face (struct it *it)
6689 {
6690 int face_id;
6691
6692 if (it->f == last_glyphless_glyph_frame
6693 && it->face_id == last_glyphless_glyph_face_id)
6694 face_id = last_glyphless_glyph_merged_face_id;
6695 else
6696 {
6697 /* Merge the `glyphless-char' face into the current face. */
6698 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6699 last_glyphless_glyph_frame = it->f;
6700 last_glyphless_glyph_face_id = it->face_id;
6701 last_glyphless_glyph_merged_face_id = face_id;
6702 }
6703 return face_id;
6704 }
6705
6706 /* Load IT's display element fields with information about the next
6707 display element from the current position of IT. Value is zero if
6708 end of buffer (or C string) is reached. */
6709
6710 static int
6711 get_next_display_element (struct it *it)
6712 {
6713 /* Non-zero means that we found a display element. Zero means that
6714 we hit the end of what we iterate over. Performance note: the
6715 function pointer `method' used here turns out to be faster than
6716 using a sequence of if-statements. */
6717 int success_p;
6718
6719 get_next:
6720 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6721
6722 if (it->what == IT_CHARACTER)
6723 {
6724 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6725 and only if (a) the resolved directionality of that character
6726 is R..." */
6727 /* FIXME: Do we need an exception for characters from display
6728 tables? */
6729 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6730 it->c = bidi_mirror_char (it->c);
6731 /* Map via display table or translate control characters.
6732 IT->c, IT->len etc. have been set to the next character by
6733 the function call above. If we have a display table, and it
6734 contains an entry for IT->c, translate it. Don't do this if
6735 IT->c itself comes from a display table, otherwise we could
6736 end up in an infinite recursion. (An alternative could be to
6737 count the recursion depth of this function and signal an
6738 error when a certain maximum depth is reached.) Is it worth
6739 it? */
6740 if (success_p && it->dpvec == NULL)
6741 {
6742 Lisp_Object dv;
6743 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6744 int nonascii_space_p = 0;
6745 int nonascii_hyphen_p = 0;
6746 int c = it->c; /* This is the character to display. */
6747
6748 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6749 {
6750 eassert (SINGLE_BYTE_CHAR_P (c));
6751 if (unibyte_display_via_language_environment)
6752 {
6753 c = DECODE_CHAR (unibyte, c);
6754 if (c < 0)
6755 c = BYTE8_TO_CHAR (it->c);
6756 }
6757 else
6758 c = BYTE8_TO_CHAR (it->c);
6759 }
6760
6761 if (it->dp
6762 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6763 VECTORP (dv)))
6764 {
6765 struct Lisp_Vector *v = XVECTOR (dv);
6766
6767 /* Return the first character from the display table
6768 entry, if not empty. If empty, don't display the
6769 current character. */
6770 if (v->header.size)
6771 {
6772 it->dpvec_char_len = it->len;
6773 it->dpvec = v->contents;
6774 it->dpend = v->contents + v->header.size;
6775 it->current.dpvec_index = 0;
6776 it->dpvec_face_id = -1;
6777 it->saved_face_id = it->face_id;
6778 it->method = GET_FROM_DISPLAY_VECTOR;
6779 it->ellipsis_p = 0;
6780 }
6781 else
6782 {
6783 set_iterator_to_next (it, 0);
6784 }
6785 goto get_next;
6786 }
6787
6788 if (! NILP (lookup_glyphless_char_display (c, it)))
6789 {
6790 if (it->what == IT_GLYPHLESS)
6791 goto done;
6792 /* Don't display this character. */
6793 set_iterator_to_next (it, 0);
6794 goto get_next;
6795 }
6796
6797 /* If `nobreak-char-display' is non-nil, we display
6798 non-ASCII spaces and hyphens specially. */
6799 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6800 {
6801 if (c == 0xA0)
6802 nonascii_space_p = true;
6803 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6804 nonascii_hyphen_p = true;
6805 }
6806
6807 /* Translate control characters into `\003' or `^C' form.
6808 Control characters coming from a display table entry are
6809 currently not translated because we use IT->dpvec to hold
6810 the translation. This could easily be changed but I
6811 don't believe that it is worth doing.
6812
6813 The characters handled by `nobreak-char-display' must be
6814 translated too.
6815
6816 Non-printable characters and raw-byte characters are also
6817 translated to octal form. */
6818 if (((c < ' ' || c == 127) /* ASCII control chars. */
6819 ? (it->area != TEXT_AREA
6820 /* In mode line, treat \n, \t like other crl chars. */
6821 || (c != '\t'
6822 && it->glyph_row
6823 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6824 || (c != '\n' && c != '\t'))
6825 : (nonascii_space_p
6826 || nonascii_hyphen_p
6827 || CHAR_BYTE8_P (c)
6828 || ! CHAR_PRINTABLE_P (c))))
6829 {
6830 /* C is a control character, non-ASCII space/hyphen,
6831 raw-byte, or a non-printable character which must be
6832 displayed either as '\003' or as `^C' where the '\\'
6833 and '^' can be defined in the display table. Fill
6834 IT->ctl_chars with glyphs for what we have to
6835 display. Then, set IT->dpvec to these glyphs. */
6836 Lisp_Object gc;
6837 int ctl_len;
6838 int face_id;
6839 int lface_id = 0;
6840 int escape_glyph;
6841
6842 /* Handle control characters with ^. */
6843
6844 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6845 {
6846 int g;
6847
6848 g = '^'; /* default glyph for Control */
6849 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6850 if (it->dp
6851 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6852 {
6853 g = GLYPH_CODE_CHAR (gc);
6854 lface_id = GLYPH_CODE_FACE (gc);
6855 }
6856
6857 face_id = (lface_id
6858 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6859 : merge_escape_glyph_face (it));
6860
6861 XSETINT (it->ctl_chars[0], g);
6862 XSETINT (it->ctl_chars[1], c ^ 0100);
6863 ctl_len = 2;
6864 goto display_control;
6865 }
6866
6867 /* Handle non-ascii space in the mode where it only gets
6868 highlighting. */
6869
6870 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6871 {
6872 /* Merge `nobreak-space' into the current face. */
6873 face_id = merge_faces (it->f, Qnobreak_space, 0,
6874 it->face_id);
6875 XSETINT (it->ctl_chars[0], ' ');
6876 ctl_len = 1;
6877 goto display_control;
6878 }
6879
6880 /* Handle sequences that start with the "escape glyph". */
6881
6882 /* the default escape glyph is \. */
6883 escape_glyph = '\\';
6884
6885 if (it->dp
6886 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6887 {
6888 escape_glyph = GLYPH_CODE_CHAR (gc);
6889 lface_id = GLYPH_CODE_FACE (gc);
6890 }
6891
6892 face_id = (lface_id
6893 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6894 : merge_escape_glyph_face (it));
6895
6896 /* Draw non-ASCII hyphen with just highlighting: */
6897
6898 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6899 {
6900 XSETINT (it->ctl_chars[0], '-');
6901 ctl_len = 1;
6902 goto display_control;
6903 }
6904
6905 /* Draw non-ASCII space/hyphen with escape glyph: */
6906
6907 if (nonascii_space_p || nonascii_hyphen_p)
6908 {
6909 XSETINT (it->ctl_chars[0], escape_glyph);
6910 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6911 ctl_len = 2;
6912 goto display_control;
6913 }
6914
6915 {
6916 char str[10];
6917 int len, i;
6918
6919 if (CHAR_BYTE8_P (c))
6920 /* Display \200 instead of \17777600. */
6921 c = CHAR_TO_BYTE8 (c);
6922 len = sprintf (str, "%03o", c);
6923
6924 XSETINT (it->ctl_chars[0], escape_glyph);
6925 for (i = 0; i < len; i++)
6926 XSETINT (it->ctl_chars[i + 1], str[i]);
6927 ctl_len = len + 1;
6928 }
6929
6930 display_control:
6931 /* Set up IT->dpvec and return first character from it. */
6932 it->dpvec_char_len = it->len;
6933 it->dpvec = it->ctl_chars;
6934 it->dpend = it->dpvec + ctl_len;
6935 it->current.dpvec_index = 0;
6936 it->dpvec_face_id = face_id;
6937 it->saved_face_id = it->face_id;
6938 it->method = GET_FROM_DISPLAY_VECTOR;
6939 it->ellipsis_p = 0;
6940 goto get_next;
6941 }
6942 it->char_to_display = c;
6943 }
6944 else if (success_p)
6945 {
6946 it->char_to_display = it->c;
6947 }
6948 }
6949
6950 #ifdef HAVE_WINDOW_SYSTEM
6951 /* Adjust face id for a multibyte character. There are no multibyte
6952 character in unibyte text. */
6953 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6954 && it->multibyte_p
6955 && success_p
6956 && FRAME_WINDOW_P (it->f))
6957 {
6958 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6959
6960 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6961 {
6962 /* Automatic composition with glyph-string. */
6963 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6964
6965 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6966 }
6967 else
6968 {
6969 ptrdiff_t pos = (it->s ? -1
6970 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6971 : IT_CHARPOS (*it));
6972 int c;
6973
6974 if (it->what == IT_CHARACTER)
6975 c = it->char_to_display;
6976 else
6977 {
6978 struct composition *cmp = composition_table[it->cmp_it.id];
6979 int i;
6980
6981 c = ' ';
6982 for (i = 0; i < cmp->glyph_len; i++)
6983 /* TAB in a composition means display glyphs with
6984 padding space on the left or right. */
6985 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6986 break;
6987 }
6988 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6989 }
6990 }
6991 #endif /* HAVE_WINDOW_SYSTEM */
6992
6993 done:
6994 /* Is this character the last one of a run of characters with
6995 box? If yes, set IT->end_of_box_run_p to 1. */
6996 if (it->face_box_p
6997 && it->s == NULL)
6998 {
6999 if (it->method == GET_FROM_STRING && it->sp)
7000 {
7001 int face_id = underlying_face_id (it);
7002 struct face *face = FACE_FROM_ID (it->f, face_id);
7003
7004 if (face)
7005 {
7006 if (face->box == FACE_NO_BOX)
7007 {
7008 /* If the box comes from face properties in a
7009 display string, check faces in that string. */
7010 int string_face_id = face_after_it_pos (it);
7011 it->end_of_box_run_p
7012 = (FACE_FROM_ID (it->f, string_face_id)->box
7013 == FACE_NO_BOX);
7014 }
7015 /* Otherwise, the box comes from the underlying face.
7016 If this is the last string character displayed, check
7017 the next buffer location. */
7018 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7019 && (it->current.overlay_string_index
7020 == it->n_overlay_strings - 1))
7021 {
7022 ptrdiff_t ignore;
7023 int next_face_id;
7024 struct text_pos pos = it->current.pos;
7025 INC_TEXT_POS (pos, it->multibyte_p);
7026
7027 next_face_id = face_at_buffer_position
7028 (it->w, CHARPOS (pos), &ignore,
7029 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7030 -1);
7031 it->end_of_box_run_p
7032 = (FACE_FROM_ID (it->f, next_face_id)->box
7033 == FACE_NO_BOX);
7034 }
7035 }
7036 }
7037 /* next_element_from_display_vector sets this flag according to
7038 faces of the display vector glyphs, see there. */
7039 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7040 {
7041 int face_id = face_after_it_pos (it);
7042 it->end_of_box_run_p
7043 = (face_id != it->face_id
7044 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7045 }
7046 }
7047 /* If we reached the end of the object we've been iterating (e.g., a
7048 display string or an overlay string), and there's something on
7049 IT->stack, proceed with what's on the stack. It doesn't make
7050 sense to return zero if there's unprocessed stuff on the stack,
7051 because otherwise that stuff will never be displayed. */
7052 if (!success_p && it->sp > 0)
7053 {
7054 set_iterator_to_next (it, 0);
7055 success_p = get_next_display_element (it);
7056 }
7057
7058 /* Value is 0 if end of buffer or string reached. */
7059 return success_p;
7060 }
7061
7062
7063 /* Move IT to the next display element.
7064
7065 RESEAT_P non-zero means if called on a newline in buffer text,
7066 skip to the next visible line start.
7067
7068 Functions get_next_display_element and set_iterator_to_next are
7069 separate because I find this arrangement easier to handle than a
7070 get_next_display_element function that also increments IT's
7071 position. The way it is we can first look at an iterator's current
7072 display element, decide whether it fits on a line, and if it does,
7073 increment the iterator position. The other way around we probably
7074 would either need a flag indicating whether the iterator has to be
7075 incremented the next time, or we would have to implement a
7076 decrement position function which would not be easy to write. */
7077
7078 void
7079 set_iterator_to_next (struct it *it, int reseat_p)
7080 {
7081 /* Reset flags indicating start and end of a sequence of characters
7082 with box. Reset them at the start of this function because
7083 moving the iterator to a new position might set them. */
7084 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7085
7086 switch (it->method)
7087 {
7088 case GET_FROM_BUFFER:
7089 /* The current display element of IT is a character from
7090 current_buffer. Advance in the buffer, and maybe skip over
7091 invisible lines that are so because of selective display. */
7092 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7093 reseat_at_next_visible_line_start (it, 0);
7094 else if (it->cmp_it.id >= 0)
7095 {
7096 /* We are currently getting glyphs from a composition. */
7097 int i;
7098
7099 if (! it->bidi_p)
7100 {
7101 IT_CHARPOS (*it) += it->cmp_it.nchars;
7102 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7103 if (it->cmp_it.to < it->cmp_it.nglyphs)
7104 {
7105 it->cmp_it.from = it->cmp_it.to;
7106 }
7107 else
7108 {
7109 it->cmp_it.id = -1;
7110 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7111 IT_BYTEPOS (*it),
7112 it->end_charpos, Qnil);
7113 }
7114 }
7115 else if (! it->cmp_it.reversed_p)
7116 {
7117 /* Composition created while scanning forward. */
7118 /* Update IT's char/byte positions to point to the first
7119 character of the next grapheme cluster, or to the
7120 character visually after the current composition. */
7121 for (i = 0; i < it->cmp_it.nchars; i++)
7122 bidi_move_to_visually_next (&it->bidi_it);
7123 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7124 IT_CHARPOS (*it) = it->bidi_it.charpos;
7125
7126 if (it->cmp_it.to < it->cmp_it.nglyphs)
7127 {
7128 /* Proceed to the next grapheme cluster. */
7129 it->cmp_it.from = it->cmp_it.to;
7130 }
7131 else
7132 {
7133 /* No more grapheme clusters in this composition.
7134 Find the next stop position. */
7135 ptrdiff_t stop = it->end_charpos;
7136 if (it->bidi_it.scan_dir < 0)
7137 /* Now we are scanning backward and don't know
7138 where to stop. */
7139 stop = -1;
7140 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7141 IT_BYTEPOS (*it), stop, Qnil);
7142 }
7143 }
7144 else
7145 {
7146 /* Composition created while scanning backward. */
7147 /* Update IT's char/byte positions to point to the last
7148 character of the previous grapheme cluster, or the
7149 character visually after the current composition. */
7150 for (i = 0; i < it->cmp_it.nchars; i++)
7151 bidi_move_to_visually_next (&it->bidi_it);
7152 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7153 IT_CHARPOS (*it) = it->bidi_it.charpos;
7154 if (it->cmp_it.from > 0)
7155 {
7156 /* Proceed to the previous grapheme cluster. */
7157 it->cmp_it.to = it->cmp_it.from;
7158 }
7159 else
7160 {
7161 /* No more grapheme clusters in this composition.
7162 Find the next stop position. */
7163 ptrdiff_t stop = it->end_charpos;
7164 if (it->bidi_it.scan_dir < 0)
7165 /* Now we are scanning backward and don't know
7166 where to stop. */
7167 stop = -1;
7168 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7169 IT_BYTEPOS (*it), stop, Qnil);
7170 }
7171 }
7172 }
7173 else
7174 {
7175 eassert (it->len != 0);
7176
7177 if (!it->bidi_p)
7178 {
7179 IT_BYTEPOS (*it) += it->len;
7180 IT_CHARPOS (*it) += 1;
7181 }
7182 else
7183 {
7184 int prev_scan_dir = it->bidi_it.scan_dir;
7185 /* If this is a new paragraph, determine its base
7186 direction (a.k.a. its base embedding level). */
7187 if (it->bidi_it.new_paragraph)
7188 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7189 bidi_move_to_visually_next (&it->bidi_it);
7190 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7191 IT_CHARPOS (*it) = it->bidi_it.charpos;
7192 if (prev_scan_dir != it->bidi_it.scan_dir)
7193 {
7194 /* As the scan direction was changed, we must
7195 re-compute the stop position for composition. */
7196 ptrdiff_t stop = it->end_charpos;
7197 if (it->bidi_it.scan_dir < 0)
7198 stop = -1;
7199 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7200 IT_BYTEPOS (*it), stop, Qnil);
7201 }
7202 }
7203 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7204 }
7205 break;
7206
7207 case GET_FROM_C_STRING:
7208 /* Current display element of IT is from a C string. */
7209 if (!it->bidi_p
7210 /* If the string position is beyond string's end, it means
7211 next_element_from_c_string is padding the string with
7212 blanks, in which case we bypass the bidi iterator,
7213 because it cannot deal with such virtual characters. */
7214 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7215 {
7216 IT_BYTEPOS (*it) += it->len;
7217 IT_CHARPOS (*it) += 1;
7218 }
7219 else
7220 {
7221 bidi_move_to_visually_next (&it->bidi_it);
7222 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7223 IT_CHARPOS (*it) = it->bidi_it.charpos;
7224 }
7225 break;
7226
7227 case GET_FROM_DISPLAY_VECTOR:
7228 /* Current display element of IT is from a display table entry.
7229 Advance in the display table definition. Reset it to null if
7230 end reached, and continue with characters from buffers/
7231 strings. */
7232 ++it->current.dpvec_index;
7233
7234 /* Restore face of the iterator to what they were before the
7235 display vector entry (these entries may contain faces). */
7236 it->face_id = it->saved_face_id;
7237
7238 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7239 {
7240 int recheck_faces = it->ellipsis_p;
7241
7242 if (it->s)
7243 it->method = GET_FROM_C_STRING;
7244 else if (STRINGP (it->string))
7245 it->method = GET_FROM_STRING;
7246 else
7247 {
7248 it->method = GET_FROM_BUFFER;
7249 it->object = it->w->contents;
7250 }
7251
7252 it->dpvec = NULL;
7253 it->current.dpvec_index = -1;
7254
7255 /* Skip over characters which were displayed via IT->dpvec. */
7256 if (it->dpvec_char_len < 0)
7257 reseat_at_next_visible_line_start (it, 1);
7258 else if (it->dpvec_char_len > 0)
7259 {
7260 if (it->method == GET_FROM_STRING
7261 && it->current.overlay_string_index >= 0
7262 && it->n_overlay_strings > 0)
7263 it->ignore_overlay_strings_at_pos_p = true;
7264 it->len = it->dpvec_char_len;
7265 set_iterator_to_next (it, reseat_p);
7266 }
7267
7268 /* Maybe recheck faces after display vector. */
7269 if (recheck_faces)
7270 it->stop_charpos = IT_CHARPOS (*it);
7271 }
7272 break;
7273
7274 case GET_FROM_STRING:
7275 /* Current display element is a character from a Lisp string. */
7276 eassert (it->s == NULL && STRINGP (it->string));
7277 /* Don't advance past string end. These conditions are true
7278 when set_iterator_to_next is called at the end of
7279 get_next_display_element, in which case the Lisp string is
7280 already exhausted, and all we want is pop the iterator
7281 stack. */
7282 if (it->current.overlay_string_index >= 0)
7283 {
7284 /* This is an overlay string, so there's no padding with
7285 spaces, and the number of characters in the string is
7286 where the string ends. */
7287 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7288 goto consider_string_end;
7289 }
7290 else
7291 {
7292 /* Not an overlay string. There could be padding, so test
7293 against it->end_charpos. */
7294 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7295 goto consider_string_end;
7296 }
7297 if (it->cmp_it.id >= 0)
7298 {
7299 int i;
7300
7301 if (! it->bidi_p)
7302 {
7303 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7304 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7305 if (it->cmp_it.to < it->cmp_it.nglyphs)
7306 it->cmp_it.from = it->cmp_it.to;
7307 else
7308 {
7309 it->cmp_it.id = -1;
7310 composition_compute_stop_pos (&it->cmp_it,
7311 IT_STRING_CHARPOS (*it),
7312 IT_STRING_BYTEPOS (*it),
7313 it->end_charpos, it->string);
7314 }
7315 }
7316 else if (! it->cmp_it.reversed_p)
7317 {
7318 for (i = 0; i < it->cmp_it.nchars; i++)
7319 bidi_move_to_visually_next (&it->bidi_it);
7320 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7321 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7322
7323 if (it->cmp_it.to < it->cmp_it.nglyphs)
7324 it->cmp_it.from = it->cmp_it.to;
7325 else
7326 {
7327 ptrdiff_t stop = it->end_charpos;
7328 if (it->bidi_it.scan_dir < 0)
7329 stop = -1;
7330 composition_compute_stop_pos (&it->cmp_it,
7331 IT_STRING_CHARPOS (*it),
7332 IT_STRING_BYTEPOS (*it), stop,
7333 it->string);
7334 }
7335 }
7336 else
7337 {
7338 for (i = 0; i < it->cmp_it.nchars; i++)
7339 bidi_move_to_visually_next (&it->bidi_it);
7340 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7341 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7342 if (it->cmp_it.from > 0)
7343 it->cmp_it.to = it->cmp_it.from;
7344 else
7345 {
7346 ptrdiff_t stop = it->end_charpos;
7347 if (it->bidi_it.scan_dir < 0)
7348 stop = -1;
7349 composition_compute_stop_pos (&it->cmp_it,
7350 IT_STRING_CHARPOS (*it),
7351 IT_STRING_BYTEPOS (*it), stop,
7352 it->string);
7353 }
7354 }
7355 }
7356 else
7357 {
7358 if (!it->bidi_p
7359 /* If the string position is beyond string's end, it
7360 means next_element_from_string is padding the string
7361 with blanks, in which case we bypass the bidi
7362 iterator, because it cannot deal with such virtual
7363 characters. */
7364 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7365 {
7366 IT_STRING_BYTEPOS (*it) += it->len;
7367 IT_STRING_CHARPOS (*it) += 1;
7368 }
7369 else
7370 {
7371 int prev_scan_dir = it->bidi_it.scan_dir;
7372
7373 bidi_move_to_visually_next (&it->bidi_it);
7374 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7375 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7376 if (prev_scan_dir != it->bidi_it.scan_dir)
7377 {
7378 ptrdiff_t stop = it->end_charpos;
7379
7380 if (it->bidi_it.scan_dir < 0)
7381 stop = -1;
7382 composition_compute_stop_pos (&it->cmp_it,
7383 IT_STRING_CHARPOS (*it),
7384 IT_STRING_BYTEPOS (*it), stop,
7385 it->string);
7386 }
7387 }
7388 }
7389
7390 consider_string_end:
7391
7392 if (it->current.overlay_string_index >= 0)
7393 {
7394 /* IT->string is an overlay string. Advance to the
7395 next, if there is one. */
7396 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7397 {
7398 it->ellipsis_p = 0;
7399 next_overlay_string (it);
7400 if (it->ellipsis_p)
7401 setup_for_ellipsis (it, 0);
7402 }
7403 }
7404 else
7405 {
7406 /* IT->string is not an overlay string. If we reached
7407 its end, and there is something on IT->stack, proceed
7408 with what is on the stack. This can be either another
7409 string, this time an overlay string, or a buffer. */
7410 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7411 && it->sp > 0)
7412 {
7413 pop_it (it);
7414 if (it->method == GET_FROM_STRING)
7415 goto consider_string_end;
7416 }
7417 }
7418 break;
7419
7420 case GET_FROM_IMAGE:
7421 case GET_FROM_STRETCH:
7422 /* The position etc with which we have to proceed are on
7423 the stack. The position may be at the end of a string,
7424 if the `display' property takes up the whole string. */
7425 eassert (it->sp > 0);
7426 pop_it (it);
7427 if (it->method == GET_FROM_STRING)
7428 goto consider_string_end;
7429 break;
7430
7431 default:
7432 /* There are no other methods defined, so this should be a bug. */
7433 emacs_abort ();
7434 }
7435
7436 eassert (it->method != GET_FROM_STRING
7437 || (STRINGP (it->string)
7438 && IT_STRING_CHARPOS (*it) >= 0));
7439 }
7440
7441 /* Load IT's display element fields with information about the next
7442 display element which comes from a display table entry or from the
7443 result of translating a control character to one of the forms `^C'
7444 or `\003'.
7445
7446 IT->dpvec holds the glyphs to return as characters.
7447 IT->saved_face_id holds the face id before the display vector--it
7448 is restored into IT->face_id in set_iterator_to_next. */
7449
7450 static int
7451 next_element_from_display_vector (struct it *it)
7452 {
7453 Lisp_Object gc;
7454 int prev_face_id = it->face_id;
7455 int next_face_id;
7456
7457 /* Precondition. */
7458 eassert (it->dpvec && it->current.dpvec_index >= 0);
7459
7460 it->face_id = it->saved_face_id;
7461
7462 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7463 That seemed totally bogus - so I changed it... */
7464 gc = it->dpvec[it->current.dpvec_index];
7465
7466 if (GLYPH_CODE_P (gc))
7467 {
7468 struct face *this_face, *prev_face, *next_face;
7469
7470 it->c = GLYPH_CODE_CHAR (gc);
7471 it->len = CHAR_BYTES (it->c);
7472
7473 /* The entry may contain a face id to use. Such a face id is
7474 the id of a Lisp face, not a realized face. A face id of
7475 zero means no face is specified. */
7476 if (it->dpvec_face_id >= 0)
7477 it->face_id = it->dpvec_face_id;
7478 else
7479 {
7480 int lface_id = GLYPH_CODE_FACE (gc);
7481 if (lface_id > 0)
7482 it->face_id = merge_faces (it->f, Qt, lface_id,
7483 it->saved_face_id);
7484 }
7485
7486 /* Glyphs in the display vector could have the box face, so we
7487 need to set the related flags in the iterator, as
7488 appropriate. */
7489 this_face = FACE_FROM_ID (it->f, it->face_id);
7490 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7491
7492 /* Is this character the first character of a box-face run? */
7493 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7494 && (!prev_face
7495 || prev_face->box == FACE_NO_BOX));
7496
7497 /* For the last character of the box-face run, we need to look
7498 either at the next glyph from the display vector, or at the
7499 face we saw before the display vector. */
7500 next_face_id = it->saved_face_id;
7501 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7502 {
7503 if (it->dpvec_face_id >= 0)
7504 next_face_id = it->dpvec_face_id;
7505 else
7506 {
7507 int lface_id =
7508 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7509
7510 if (lface_id > 0)
7511 next_face_id = merge_faces (it->f, Qt, lface_id,
7512 it->saved_face_id);
7513 }
7514 }
7515 next_face = FACE_FROM_ID (it->f, next_face_id);
7516 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7517 && (!next_face
7518 || next_face->box == FACE_NO_BOX));
7519 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7520 }
7521 else
7522 /* Display table entry is invalid. Return a space. */
7523 it->c = ' ', it->len = 1;
7524
7525 /* Don't change position and object of the iterator here. They are
7526 still the values of the character that had this display table
7527 entry or was translated, and that's what we want. */
7528 it->what = IT_CHARACTER;
7529 return 1;
7530 }
7531
7532 /* Get the first element of string/buffer in the visual order, after
7533 being reseated to a new position in a string or a buffer. */
7534 static void
7535 get_visually_first_element (struct it *it)
7536 {
7537 int string_p = STRINGP (it->string) || it->s;
7538 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7539 ptrdiff_t bob = (string_p ? 0 : BEGV);
7540
7541 if (STRINGP (it->string))
7542 {
7543 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7544 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7545 }
7546 else
7547 {
7548 it->bidi_it.charpos = IT_CHARPOS (*it);
7549 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7550 }
7551
7552 if (it->bidi_it.charpos == eob)
7553 {
7554 /* Nothing to do, but reset the FIRST_ELT flag, like
7555 bidi_paragraph_init does, because we are not going to
7556 call it. */
7557 it->bidi_it.first_elt = 0;
7558 }
7559 else if (it->bidi_it.charpos == bob
7560 || (!string_p
7561 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7562 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7563 {
7564 /* If we are at the beginning of a line/string, we can produce
7565 the next element right away. */
7566 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7567 bidi_move_to_visually_next (&it->bidi_it);
7568 }
7569 else
7570 {
7571 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7572
7573 /* We need to prime the bidi iterator starting at the line's or
7574 string's beginning, before we will be able to produce the
7575 next element. */
7576 if (string_p)
7577 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7578 else
7579 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7580 IT_BYTEPOS (*it), -1,
7581 &it->bidi_it.bytepos);
7582 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7583 do
7584 {
7585 /* Now return to buffer/string position where we were asked
7586 to get the next display element, and produce that. */
7587 bidi_move_to_visually_next (&it->bidi_it);
7588 }
7589 while (it->bidi_it.bytepos != orig_bytepos
7590 && it->bidi_it.charpos < eob);
7591 }
7592
7593 /* Adjust IT's position information to where we ended up. */
7594 if (STRINGP (it->string))
7595 {
7596 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7597 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7598 }
7599 else
7600 {
7601 IT_CHARPOS (*it) = it->bidi_it.charpos;
7602 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7603 }
7604
7605 if (STRINGP (it->string) || !it->s)
7606 {
7607 ptrdiff_t stop, charpos, bytepos;
7608
7609 if (STRINGP (it->string))
7610 {
7611 eassert (!it->s);
7612 stop = SCHARS (it->string);
7613 if (stop > it->end_charpos)
7614 stop = it->end_charpos;
7615 charpos = IT_STRING_CHARPOS (*it);
7616 bytepos = IT_STRING_BYTEPOS (*it);
7617 }
7618 else
7619 {
7620 stop = it->end_charpos;
7621 charpos = IT_CHARPOS (*it);
7622 bytepos = IT_BYTEPOS (*it);
7623 }
7624 if (it->bidi_it.scan_dir < 0)
7625 stop = -1;
7626 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7627 it->string);
7628 }
7629 }
7630
7631 /* Load IT with the next display element from Lisp string IT->string.
7632 IT->current.string_pos is the current position within the string.
7633 If IT->current.overlay_string_index >= 0, the Lisp string is an
7634 overlay string. */
7635
7636 static int
7637 next_element_from_string (struct it *it)
7638 {
7639 struct text_pos position;
7640
7641 eassert (STRINGP (it->string));
7642 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7643 eassert (IT_STRING_CHARPOS (*it) >= 0);
7644 position = it->current.string_pos;
7645
7646 /* With bidi reordering, the character to display might not be the
7647 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7648 that we were reseat()ed to a new string, whose paragraph
7649 direction is not known. */
7650 if (it->bidi_p && it->bidi_it.first_elt)
7651 {
7652 get_visually_first_element (it);
7653 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7654 }
7655
7656 /* Time to check for invisible text? */
7657 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7658 {
7659 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7660 {
7661 if (!(!it->bidi_p
7662 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7663 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7664 {
7665 /* With bidi non-linear iteration, we could find
7666 ourselves far beyond the last computed stop_charpos,
7667 with several other stop positions in between that we
7668 missed. Scan them all now, in buffer's logical
7669 order, until we find and handle the last stop_charpos
7670 that precedes our current position. */
7671 handle_stop_backwards (it, it->stop_charpos);
7672 return GET_NEXT_DISPLAY_ELEMENT (it);
7673 }
7674 else
7675 {
7676 if (it->bidi_p)
7677 {
7678 /* Take note of the stop position we just moved
7679 across, for when we will move back across it. */
7680 it->prev_stop = it->stop_charpos;
7681 /* If we are at base paragraph embedding level, take
7682 note of the last stop position seen at this
7683 level. */
7684 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7685 it->base_level_stop = it->stop_charpos;
7686 }
7687 handle_stop (it);
7688
7689 /* Since a handler may have changed IT->method, we must
7690 recurse here. */
7691 return GET_NEXT_DISPLAY_ELEMENT (it);
7692 }
7693 }
7694 else if (it->bidi_p
7695 /* If we are before prev_stop, we may have overstepped
7696 on our way backwards a stop_pos, and if so, we need
7697 to handle that stop_pos. */
7698 && IT_STRING_CHARPOS (*it) < it->prev_stop
7699 /* We can sometimes back up for reasons that have nothing
7700 to do with bidi reordering. E.g., compositions. The
7701 code below is only needed when we are above the base
7702 embedding level, so test for that explicitly. */
7703 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7704 {
7705 /* If we lost track of base_level_stop, we have no better
7706 place for handle_stop_backwards to start from than string
7707 beginning. This happens, e.g., when we were reseated to
7708 the previous screenful of text by vertical-motion. */
7709 if (it->base_level_stop <= 0
7710 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7711 it->base_level_stop = 0;
7712 handle_stop_backwards (it, it->base_level_stop);
7713 return GET_NEXT_DISPLAY_ELEMENT (it);
7714 }
7715 }
7716
7717 if (it->current.overlay_string_index >= 0)
7718 {
7719 /* Get the next character from an overlay string. In overlay
7720 strings, there is no field width or padding with spaces to
7721 do. */
7722 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7723 {
7724 it->what = IT_EOB;
7725 return 0;
7726 }
7727 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7728 IT_STRING_BYTEPOS (*it),
7729 it->bidi_it.scan_dir < 0
7730 ? -1
7731 : SCHARS (it->string))
7732 && next_element_from_composition (it))
7733 {
7734 return 1;
7735 }
7736 else if (STRING_MULTIBYTE (it->string))
7737 {
7738 const unsigned char *s = (SDATA (it->string)
7739 + IT_STRING_BYTEPOS (*it));
7740 it->c = string_char_and_length (s, &it->len);
7741 }
7742 else
7743 {
7744 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7745 it->len = 1;
7746 }
7747 }
7748 else
7749 {
7750 /* Get the next character from a Lisp string that is not an
7751 overlay string. Such strings come from the mode line, for
7752 example. We may have to pad with spaces, or truncate the
7753 string. See also next_element_from_c_string. */
7754 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7755 {
7756 it->what = IT_EOB;
7757 return 0;
7758 }
7759 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7760 {
7761 /* Pad with spaces. */
7762 it->c = ' ', it->len = 1;
7763 CHARPOS (position) = BYTEPOS (position) = -1;
7764 }
7765 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7766 IT_STRING_BYTEPOS (*it),
7767 it->bidi_it.scan_dir < 0
7768 ? -1
7769 : it->string_nchars)
7770 && next_element_from_composition (it))
7771 {
7772 return 1;
7773 }
7774 else if (STRING_MULTIBYTE (it->string))
7775 {
7776 const unsigned char *s = (SDATA (it->string)
7777 + IT_STRING_BYTEPOS (*it));
7778 it->c = string_char_and_length (s, &it->len);
7779 }
7780 else
7781 {
7782 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7783 it->len = 1;
7784 }
7785 }
7786
7787 /* Record what we have and where it came from. */
7788 it->what = IT_CHARACTER;
7789 it->object = it->string;
7790 it->position = position;
7791 return 1;
7792 }
7793
7794
7795 /* Load IT with next display element from C string IT->s.
7796 IT->string_nchars is the maximum number of characters to return
7797 from the string. IT->end_charpos may be greater than
7798 IT->string_nchars when this function is called, in which case we
7799 may have to return padding spaces. Value is zero if end of string
7800 reached, including padding spaces. */
7801
7802 static int
7803 next_element_from_c_string (struct it *it)
7804 {
7805 bool success_p = true;
7806
7807 eassert (it->s);
7808 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7809 it->what = IT_CHARACTER;
7810 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7811 it->object = Qnil;
7812
7813 /* With bidi reordering, the character to display might not be the
7814 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7815 we were reseated to a new string, whose paragraph direction is
7816 not known. */
7817 if (it->bidi_p && it->bidi_it.first_elt)
7818 get_visually_first_element (it);
7819
7820 /* IT's position can be greater than IT->string_nchars in case a
7821 field width or precision has been specified when the iterator was
7822 initialized. */
7823 if (IT_CHARPOS (*it) >= it->end_charpos)
7824 {
7825 /* End of the game. */
7826 it->what = IT_EOB;
7827 success_p = 0;
7828 }
7829 else if (IT_CHARPOS (*it) >= it->string_nchars)
7830 {
7831 /* Pad with spaces. */
7832 it->c = ' ', it->len = 1;
7833 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7834 }
7835 else if (it->multibyte_p)
7836 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7837 else
7838 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7839
7840 return success_p;
7841 }
7842
7843
7844 /* Set up IT to return characters from an ellipsis, if appropriate.
7845 The definition of the ellipsis glyphs may come from a display table
7846 entry. This function fills IT with the first glyph from the
7847 ellipsis if an ellipsis is to be displayed. */
7848
7849 static int
7850 next_element_from_ellipsis (struct it *it)
7851 {
7852 if (it->selective_display_ellipsis_p)
7853 setup_for_ellipsis (it, it->len);
7854 else
7855 {
7856 /* The face at the current position may be different from the
7857 face we find after the invisible text. Remember what it
7858 was in IT->saved_face_id, and signal that it's there by
7859 setting face_before_selective_p. */
7860 it->saved_face_id = it->face_id;
7861 it->method = GET_FROM_BUFFER;
7862 it->object = it->w->contents;
7863 reseat_at_next_visible_line_start (it, 1);
7864 it->face_before_selective_p = true;
7865 }
7866
7867 return GET_NEXT_DISPLAY_ELEMENT (it);
7868 }
7869
7870
7871 /* Deliver an image display element. The iterator IT is already
7872 filled with image information (done in handle_display_prop). Value
7873 is always 1. */
7874
7875
7876 static int
7877 next_element_from_image (struct it *it)
7878 {
7879 it->what = IT_IMAGE;
7880 it->ignore_overlay_strings_at_pos_p = 0;
7881 return 1;
7882 }
7883
7884
7885 /* Fill iterator IT with next display element from a stretch glyph
7886 property. IT->object is the value of the text property. Value is
7887 always 1. */
7888
7889 static int
7890 next_element_from_stretch (struct it *it)
7891 {
7892 it->what = IT_STRETCH;
7893 return 1;
7894 }
7895
7896 /* Scan backwards from IT's current position until we find a stop
7897 position, or until BEGV. This is called when we find ourself
7898 before both the last known prev_stop and base_level_stop while
7899 reordering bidirectional text. */
7900
7901 static void
7902 compute_stop_pos_backwards (struct it *it)
7903 {
7904 const int SCAN_BACK_LIMIT = 1000;
7905 struct text_pos pos;
7906 struct display_pos save_current = it->current;
7907 struct text_pos save_position = it->position;
7908 ptrdiff_t charpos = IT_CHARPOS (*it);
7909 ptrdiff_t where_we_are = charpos;
7910 ptrdiff_t save_stop_pos = it->stop_charpos;
7911 ptrdiff_t save_end_pos = it->end_charpos;
7912
7913 eassert (NILP (it->string) && !it->s);
7914 eassert (it->bidi_p);
7915 it->bidi_p = 0;
7916 do
7917 {
7918 it->end_charpos = min (charpos + 1, ZV);
7919 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7920 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7921 reseat_1 (it, pos, 0);
7922 compute_stop_pos (it);
7923 /* We must advance forward, right? */
7924 if (it->stop_charpos <= charpos)
7925 emacs_abort ();
7926 }
7927 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7928
7929 if (it->stop_charpos <= where_we_are)
7930 it->prev_stop = it->stop_charpos;
7931 else
7932 it->prev_stop = BEGV;
7933 it->bidi_p = true;
7934 it->current = save_current;
7935 it->position = save_position;
7936 it->stop_charpos = save_stop_pos;
7937 it->end_charpos = save_end_pos;
7938 }
7939
7940 /* Scan forward from CHARPOS in the current buffer/string, until we
7941 find a stop position > current IT's position. Then handle the stop
7942 position before that. This is called when we bump into a stop
7943 position while reordering bidirectional text. CHARPOS should be
7944 the last previously processed stop_pos (or BEGV/0, if none were
7945 processed yet) whose position is less that IT's current
7946 position. */
7947
7948 static void
7949 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7950 {
7951 int bufp = !STRINGP (it->string);
7952 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7953 struct display_pos save_current = it->current;
7954 struct text_pos save_position = it->position;
7955 struct text_pos pos1;
7956 ptrdiff_t next_stop;
7957
7958 /* Scan in strict logical order. */
7959 eassert (it->bidi_p);
7960 it->bidi_p = 0;
7961 do
7962 {
7963 it->prev_stop = charpos;
7964 if (bufp)
7965 {
7966 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7967 reseat_1 (it, pos1, 0);
7968 }
7969 else
7970 it->current.string_pos = string_pos (charpos, it->string);
7971 compute_stop_pos (it);
7972 /* We must advance forward, right? */
7973 if (it->stop_charpos <= it->prev_stop)
7974 emacs_abort ();
7975 charpos = it->stop_charpos;
7976 }
7977 while (charpos <= where_we_are);
7978
7979 it->bidi_p = true;
7980 it->current = save_current;
7981 it->position = save_position;
7982 next_stop = it->stop_charpos;
7983 it->stop_charpos = it->prev_stop;
7984 handle_stop (it);
7985 it->stop_charpos = next_stop;
7986 }
7987
7988 /* Load IT with the next display element from current_buffer. Value
7989 is zero if end of buffer reached. IT->stop_charpos is the next
7990 position at which to stop and check for text properties or buffer
7991 end. */
7992
7993 static int
7994 next_element_from_buffer (struct it *it)
7995 {
7996 bool success_p = true;
7997
7998 eassert (IT_CHARPOS (*it) >= BEGV);
7999 eassert (NILP (it->string) && !it->s);
8000 eassert (!it->bidi_p
8001 || (EQ (it->bidi_it.string.lstring, Qnil)
8002 && it->bidi_it.string.s == NULL));
8003
8004 /* With bidi reordering, the character to display might not be the
8005 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8006 we were reseat()ed to a new buffer position, which is potentially
8007 a different paragraph. */
8008 if (it->bidi_p && it->bidi_it.first_elt)
8009 {
8010 get_visually_first_element (it);
8011 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8012 }
8013
8014 if (IT_CHARPOS (*it) >= it->stop_charpos)
8015 {
8016 if (IT_CHARPOS (*it) >= it->end_charpos)
8017 {
8018 int overlay_strings_follow_p;
8019
8020 /* End of the game, except when overlay strings follow that
8021 haven't been returned yet. */
8022 if (it->overlay_strings_at_end_processed_p)
8023 overlay_strings_follow_p = 0;
8024 else
8025 {
8026 it->overlay_strings_at_end_processed_p = true;
8027 overlay_strings_follow_p = get_overlay_strings (it, 0);
8028 }
8029
8030 if (overlay_strings_follow_p)
8031 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8032 else
8033 {
8034 it->what = IT_EOB;
8035 it->position = it->current.pos;
8036 success_p = 0;
8037 }
8038 }
8039 else if (!(!it->bidi_p
8040 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8041 || IT_CHARPOS (*it) == it->stop_charpos))
8042 {
8043 /* With bidi non-linear iteration, we could find ourselves
8044 far beyond the last computed stop_charpos, with several
8045 other stop positions in between that we missed. Scan
8046 them all now, in buffer's logical order, until we find
8047 and handle the last stop_charpos that precedes our
8048 current position. */
8049 handle_stop_backwards (it, it->stop_charpos);
8050 return GET_NEXT_DISPLAY_ELEMENT (it);
8051 }
8052 else
8053 {
8054 if (it->bidi_p)
8055 {
8056 /* Take note of the stop position we just moved across,
8057 for when we will move back across it. */
8058 it->prev_stop = it->stop_charpos;
8059 /* If we are at base paragraph embedding level, take
8060 note of the last stop position seen at this
8061 level. */
8062 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8063 it->base_level_stop = it->stop_charpos;
8064 }
8065 handle_stop (it);
8066 return GET_NEXT_DISPLAY_ELEMENT (it);
8067 }
8068 }
8069 else if (it->bidi_p
8070 /* If we are before prev_stop, we may have overstepped on
8071 our way backwards a stop_pos, and if so, we need to
8072 handle that stop_pos. */
8073 && IT_CHARPOS (*it) < it->prev_stop
8074 /* We can sometimes back up for reasons that have nothing
8075 to do with bidi reordering. E.g., compositions. The
8076 code below is only needed when we are above the base
8077 embedding level, so test for that explicitly. */
8078 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8079 {
8080 if (it->base_level_stop <= 0
8081 || IT_CHARPOS (*it) < it->base_level_stop)
8082 {
8083 /* If we lost track of base_level_stop, we need to find
8084 prev_stop by looking backwards. This happens, e.g., when
8085 we were reseated to the previous screenful of text by
8086 vertical-motion. */
8087 it->base_level_stop = BEGV;
8088 compute_stop_pos_backwards (it);
8089 handle_stop_backwards (it, it->prev_stop);
8090 }
8091 else
8092 handle_stop_backwards (it, it->base_level_stop);
8093 return GET_NEXT_DISPLAY_ELEMENT (it);
8094 }
8095 else
8096 {
8097 /* No face changes, overlays etc. in sight, so just return a
8098 character from current_buffer. */
8099 unsigned char *p;
8100 ptrdiff_t stop;
8101
8102 /* Maybe run the redisplay end trigger hook. Performance note:
8103 This doesn't seem to cost measurable time. */
8104 if (it->redisplay_end_trigger_charpos
8105 && it->glyph_row
8106 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8107 run_redisplay_end_trigger_hook (it);
8108
8109 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8110 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8111 stop)
8112 && next_element_from_composition (it))
8113 {
8114 return 1;
8115 }
8116
8117 /* Get the next character, maybe multibyte. */
8118 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8119 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8120 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8121 else
8122 it->c = *p, it->len = 1;
8123
8124 /* Record what we have and where it came from. */
8125 it->what = IT_CHARACTER;
8126 it->object = it->w->contents;
8127 it->position = it->current.pos;
8128
8129 /* Normally we return the character found above, except when we
8130 really want to return an ellipsis for selective display. */
8131 if (it->selective)
8132 {
8133 if (it->c == '\n')
8134 {
8135 /* A value of selective > 0 means hide lines indented more
8136 than that number of columns. */
8137 if (it->selective > 0
8138 && IT_CHARPOS (*it) + 1 < ZV
8139 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8140 IT_BYTEPOS (*it) + 1,
8141 it->selective))
8142 {
8143 success_p = next_element_from_ellipsis (it);
8144 it->dpvec_char_len = -1;
8145 }
8146 }
8147 else if (it->c == '\r' && it->selective == -1)
8148 {
8149 /* A value of selective == -1 means that everything from the
8150 CR to the end of the line is invisible, with maybe an
8151 ellipsis displayed for it. */
8152 success_p = next_element_from_ellipsis (it);
8153 it->dpvec_char_len = -1;
8154 }
8155 }
8156 }
8157
8158 /* Value is zero if end of buffer reached. */
8159 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8160 return success_p;
8161 }
8162
8163
8164 /* Run the redisplay end trigger hook for IT. */
8165
8166 static void
8167 run_redisplay_end_trigger_hook (struct it *it)
8168 {
8169 Lisp_Object args[3];
8170
8171 /* IT->glyph_row should be non-null, i.e. we should be actually
8172 displaying something, or otherwise we should not run the hook. */
8173 eassert (it->glyph_row);
8174
8175 /* Set up hook arguments. */
8176 args[0] = Qredisplay_end_trigger_functions;
8177 args[1] = it->window;
8178 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8179 it->redisplay_end_trigger_charpos = 0;
8180
8181 /* Since we are *trying* to run these functions, don't try to run
8182 them again, even if they get an error. */
8183 wset_redisplay_end_trigger (it->w, Qnil);
8184 Frun_hook_with_args (3, args);
8185
8186 /* Notice if it changed the face of the character we are on. */
8187 handle_face_prop (it);
8188 }
8189
8190
8191 /* Deliver a composition display element. Unlike the other
8192 next_element_from_XXX, this function is not registered in the array
8193 get_next_element[]. It is called from next_element_from_buffer and
8194 next_element_from_string when necessary. */
8195
8196 static int
8197 next_element_from_composition (struct it *it)
8198 {
8199 it->what = IT_COMPOSITION;
8200 it->len = it->cmp_it.nbytes;
8201 if (STRINGP (it->string))
8202 {
8203 if (it->c < 0)
8204 {
8205 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8206 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8207 return 0;
8208 }
8209 it->position = it->current.string_pos;
8210 it->object = it->string;
8211 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8212 IT_STRING_BYTEPOS (*it), it->string);
8213 }
8214 else
8215 {
8216 if (it->c < 0)
8217 {
8218 IT_CHARPOS (*it) += it->cmp_it.nchars;
8219 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8220 if (it->bidi_p)
8221 {
8222 if (it->bidi_it.new_paragraph)
8223 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8224 /* Resync the bidi iterator with IT's new position.
8225 FIXME: this doesn't support bidirectional text. */
8226 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8227 bidi_move_to_visually_next (&it->bidi_it);
8228 }
8229 return 0;
8230 }
8231 it->position = it->current.pos;
8232 it->object = it->w->contents;
8233 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8234 IT_BYTEPOS (*it), Qnil);
8235 }
8236 return 1;
8237 }
8238
8239
8240 \f
8241 /***********************************************************************
8242 Moving an iterator without producing glyphs
8243 ***********************************************************************/
8244
8245 /* Check if iterator is at a position corresponding to a valid buffer
8246 position after some move_it_ call. */
8247
8248 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8249 ((it)->method == GET_FROM_STRING \
8250 ? IT_STRING_CHARPOS (*it) == 0 \
8251 : 1)
8252
8253
8254 /* Move iterator IT to a specified buffer or X position within one
8255 line on the display without producing glyphs.
8256
8257 OP should be a bit mask including some or all of these bits:
8258 MOVE_TO_X: Stop upon reaching x-position TO_X.
8259 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8260 Regardless of OP's value, stop upon reaching the end of the display line.
8261
8262 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8263 This means, in particular, that TO_X includes window's horizontal
8264 scroll amount.
8265
8266 The return value has several possible values that
8267 say what condition caused the scan to stop:
8268
8269 MOVE_POS_MATCH_OR_ZV
8270 - when TO_POS or ZV was reached.
8271
8272 MOVE_X_REACHED
8273 -when TO_X was reached before TO_POS or ZV were reached.
8274
8275 MOVE_LINE_CONTINUED
8276 - when we reached the end of the display area and the line must
8277 be continued.
8278
8279 MOVE_LINE_TRUNCATED
8280 - when we reached the end of the display area and the line is
8281 truncated.
8282
8283 MOVE_NEWLINE_OR_CR
8284 - when we stopped at a line end, i.e. a newline or a CR and selective
8285 display is on. */
8286
8287 static enum move_it_result
8288 move_it_in_display_line_to (struct it *it,
8289 ptrdiff_t to_charpos, int to_x,
8290 enum move_operation_enum op)
8291 {
8292 enum move_it_result result = MOVE_UNDEFINED;
8293 struct glyph_row *saved_glyph_row;
8294 struct it wrap_it, atpos_it, atx_it, ppos_it;
8295 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8296 void *ppos_data = NULL;
8297 int may_wrap = 0;
8298 enum it_method prev_method = it->method;
8299 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8300 int saw_smaller_pos = prev_pos < to_charpos;
8301
8302 /* Don't produce glyphs in produce_glyphs. */
8303 saved_glyph_row = it->glyph_row;
8304 it->glyph_row = NULL;
8305
8306 /* Use wrap_it to save a copy of IT wherever a word wrap could
8307 occur. Use atpos_it to save a copy of IT at the desired buffer
8308 position, if found, so that we can scan ahead and check if the
8309 word later overshoots the window edge. Use atx_it similarly, for
8310 pixel positions. */
8311 wrap_it.sp = -1;
8312 atpos_it.sp = -1;
8313 atx_it.sp = -1;
8314
8315 /* Use ppos_it under bidi reordering to save a copy of IT for the
8316 position > CHARPOS that is the closest to CHARPOS. We restore
8317 that position in IT when we have scanned the entire display line
8318 without finding a match for CHARPOS and all the character
8319 positions are greater than CHARPOS. */
8320 if (it->bidi_p)
8321 {
8322 SAVE_IT (ppos_it, *it, ppos_data);
8323 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8324 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8325 SAVE_IT (ppos_it, *it, ppos_data);
8326 }
8327
8328 #define BUFFER_POS_REACHED_P() \
8329 ((op & MOVE_TO_POS) != 0 \
8330 && BUFFERP (it->object) \
8331 && (IT_CHARPOS (*it) == to_charpos \
8332 || ((!it->bidi_p \
8333 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8334 && IT_CHARPOS (*it) > to_charpos) \
8335 || (it->what == IT_COMPOSITION \
8336 && ((IT_CHARPOS (*it) > to_charpos \
8337 && to_charpos >= it->cmp_it.charpos) \
8338 || (IT_CHARPOS (*it) < to_charpos \
8339 && to_charpos <= it->cmp_it.charpos)))) \
8340 && (it->method == GET_FROM_BUFFER \
8341 || (it->method == GET_FROM_DISPLAY_VECTOR \
8342 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8343
8344 /* If there's a line-/wrap-prefix, handle it. */
8345 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8346 && it->current_y < it->last_visible_y)
8347 handle_line_prefix (it);
8348
8349 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8350 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8351
8352 while (1)
8353 {
8354 int x, i, ascent = 0, descent = 0;
8355
8356 /* Utility macro to reset an iterator with x, ascent, and descent. */
8357 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8358 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8359 (IT)->max_descent = descent)
8360
8361 /* Stop if we move beyond TO_CHARPOS (after an image or a
8362 display string or stretch glyph). */
8363 if ((op & MOVE_TO_POS) != 0
8364 && BUFFERP (it->object)
8365 && it->method == GET_FROM_BUFFER
8366 && (((!it->bidi_p
8367 /* When the iterator is at base embedding level, we
8368 are guaranteed that characters are delivered for
8369 display in strictly increasing order of their
8370 buffer positions. */
8371 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8372 && IT_CHARPOS (*it) > to_charpos)
8373 || (it->bidi_p
8374 && (prev_method == GET_FROM_IMAGE
8375 || prev_method == GET_FROM_STRETCH
8376 || prev_method == GET_FROM_STRING)
8377 /* Passed TO_CHARPOS from left to right. */
8378 && ((prev_pos < to_charpos
8379 && IT_CHARPOS (*it) > to_charpos)
8380 /* Passed TO_CHARPOS from right to left. */
8381 || (prev_pos > to_charpos
8382 && IT_CHARPOS (*it) < to_charpos)))))
8383 {
8384 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8385 {
8386 result = MOVE_POS_MATCH_OR_ZV;
8387 break;
8388 }
8389 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8390 /* If wrap_it is valid, the current position might be in a
8391 word that is wrapped. So, save the iterator in
8392 atpos_it and continue to see if wrapping happens. */
8393 SAVE_IT (atpos_it, *it, atpos_data);
8394 }
8395
8396 /* Stop when ZV reached.
8397 We used to stop here when TO_CHARPOS reached as well, but that is
8398 too soon if this glyph does not fit on this line. So we handle it
8399 explicitly below. */
8400 if (!get_next_display_element (it))
8401 {
8402 result = MOVE_POS_MATCH_OR_ZV;
8403 break;
8404 }
8405
8406 if (it->line_wrap == TRUNCATE)
8407 {
8408 if (BUFFER_POS_REACHED_P ())
8409 {
8410 result = MOVE_POS_MATCH_OR_ZV;
8411 break;
8412 }
8413 }
8414 else
8415 {
8416 if (it->line_wrap == WORD_WRAP)
8417 {
8418 if (IT_DISPLAYING_WHITESPACE (it))
8419 may_wrap = 1;
8420 else if (may_wrap)
8421 {
8422 /* We have reached a glyph that follows one or more
8423 whitespace characters. If the position is
8424 already found, we are done. */
8425 if (atpos_it.sp >= 0)
8426 {
8427 RESTORE_IT (it, &atpos_it, atpos_data);
8428 result = MOVE_POS_MATCH_OR_ZV;
8429 goto done;
8430 }
8431 if (atx_it.sp >= 0)
8432 {
8433 RESTORE_IT (it, &atx_it, atx_data);
8434 result = MOVE_X_REACHED;
8435 goto done;
8436 }
8437 /* Otherwise, we can wrap here. */
8438 SAVE_IT (wrap_it, *it, wrap_data);
8439 may_wrap = 0;
8440 }
8441 }
8442 }
8443
8444 /* Remember the line height for the current line, in case
8445 the next element doesn't fit on the line. */
8446 ascent = it->max_ascent;
8447 descent = it->max_descent;
8448
8449 /* The call to produce_glyphs will get the metrics of the
8450 display element IT is loaded with. Record the x-position
8451 before this display element, in case it doesn't fit on the
8452 line. */
8453 x = it->current_x;
8454
8455 PRODUCE_GLYPHS (it);
8456
8457 if (it->area != TEXT_AREA)
8458 {
8459 prev_method = it->method;
8460 if (it->method == GET_FROM_BUFFER)
8461 prev_pos = IT_CHARPOS (*it);
8462 set_iterator_to_next (it, 1);
8463 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8464 SET_TEXT_POS (this_line_min_pos,
8465 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8466 if (it->bidi_p
8467 && (op & MOVE_TO_POS)
8468 && IT_CHARPOS (*it) > to_charpos
8469 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8470 SAVE_IT (ppos_it, *it, ppos_data);
8471 continue;
8472 }
8473
8474 /* The number of glyphs we get back in IT->nglyphs will normally
8475 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8476 character on a terminal frame, or (iii) a line end. For the
8477 second case, IT->nglyphs - 1 padding glyphs will be present.
8478 (On X frames, there is only one glyph produced for a
8479 composite character.)
8480
8481 The behavior implemented below means, for continuation lines,
8482 that as many spaces of a TAB as fit on the current line are
8483 displayed there. For terminal frames, as many glyphs of a
8484 multi-glyph character are displayed in the current line, too.
8485 This is what the old redisplay code did, and we keep it that
8486 way. Under X, the whole shape of a complex character must
8487 fit on the line or it will be completely displayed in the
8488 next line.
8489
8490 Note that both for tabs and padding glyphs, all glyphs have
8491 the same width. */
8492 if (it->nglyphs)
8493 {
8494 /* More than one glyph or glyph doesn't fit on line. All
8495 glyphs have the same width. */
8496 int single_glyph_width = it->pixel_width / it->nglyphs;
8497 int new_x;
8498 int x_before_this_char = x;
8499 int hpos_before_this_char = it->hpos;
8500
8501 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8502 {
8503 new_x = x + single_glyph_width;
8504
8505 /* We want to leave anything reaching TO_X to the caller. */
8506 if ((op & MOVE_TO_X) && new_x > to_x)
8507 {
8508 if (BUFFER_POS_REACHED_P ())
8509 {
8510 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8511 goto buffer_pos_reached;
8512 if (atpos_it.sp < 0)
8513 {
8514 SAVE_IT (atpos_it, *it, atpos_data);
8515 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8516 }
8517 }
8518 else
8519 {
8520 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8521 {
8522 it->current_x = x;
8523 result = MOVE_X_REACHED;
8524 break;
8525 }
8526 if (atx_it.sp < 0)
8527 {
8528 SAVE_IT (atx_it, *it, atx_data);
8529 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8530 }
8531 }
8532 }
8533
8534 if (/* Lines are continued. */
8535 it->line_wrap != TRUNCATE
8536 && (/* And glyph doesn't fit on the line. */
8537 new_x > it->last_visible_x
8538 /* Or it fits exactly and we're on a window
8539 system frame. */
8540 || (new_x == it->last_visible_x
8541 && FRAME_WINDOW_P (it->f)
8542 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8543 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8544 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8545 {
8546 if (/* IT->hpos == 0 means the very first glyph
8547 doesn't fit on the line, e.g. a wide image. */
8548 it->hpos == 0
8549 || (new_x == it->last_visible_x
8550 && FRAME_WINDOW_P (it->f)))
8551 {
8552 ++it->hpos;
8553 it->current_x = new_x;
8554
8555 /* The character's last glyph just barely fits
8556 in this row. */
8557 if (i == it->nglyphs - 1)
8558 {
8559 /* If this is the destination position,
8560 return a position *before* it in this row,
8561 now that we know it fits in this row. */
8562 if (BUFFER_POS_REACHED_P ())
8563 {
8564 if (it->line_wrap != WORD_WRAP
8565 || wrap_it.sp < 0)
8566 {
8567 it->hpos = hpos_before_this_char;
8568 it->current_x = x_before_this_char;
8569 result = MOVE_POS_MATCH_OR_ZV;
8570 break;
8571 }
8572 if (it->line_wrap == WORD_WRAP
8573 && atpos_it.sp < 0)
8574 {
8575 SAVE_IT (atpos_it, *it, atpos_data);
8576 atpos_it.current_x = x_before_this_char;
8577 atpos_it.hpos = hpos_before_this_char;
8578 }
8579 }
8580
8581 prev_method = it->method;
8582 if (it->method == GET_FROM_BUFFER)
8583 prev_pos = IT_CHARPOS (*it);
8584 set_iterator_to_next (it, 1);
8585 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8586 SET_TEXT_POS (this_line_min_pos,
8587 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8588 /* On graphical terminals, newlines may
8589 "overflow" into the fringe if
8590 overflow-newline-into-fringe is non-nil.
8591 On text terminals, and on graphical
8592 terminals with no right margin, newlines
8593 may overflow into the last glyph on the
8594 display line.*/
8595 if (!FRAME_WINDOW_P (it->f)
8596 || ((it->bidi_p
8597 && it->bidi_it.paragraph_dir == R2L)
8598 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8599 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8600 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8601 {
8602 if (!get_next_display_element (it))
8603 {
8604 result = MOVE_POS_MATCH_OR_ZV;
8605 break;
8606 }
8607 if (BUFFER_POS_REACHED_P ())
8608 {
8609 if (ITERATOR_AT_END_OF_LINE_P (it))
8610 result = MOVE_POS_MATCH_OR_ZV;
8611 else
8612 result = MOVE_LINE_CONTINUED;
8613 break;
8614 }
8615 if (ITERATOR_AT_END_OF_LINE_P (it)
8616 && (it->line_wrap != WORD_WRAP
8617 || wrap_it.sp < 0))
8618 {
8619 result = MOVE_NEWLINE_OR_CR;
8620 break;
8621 }
8622 }
8623 }
8624 }
8625 else
8626 IT_RESET_X_ASCENT_DESCENT (it);
8627
8628 if (wrap_it.sp >= 0)
8629 {
8630 RESTORE_IT (it, &wrap_it, wrap_data);
8631 atpos_it.sp = -1;
8632 atx_it.sp = -1;
8633 }
8634
8635 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8636 IT_CHARPOS (*it)));
8637 result = MOVE_LINE_CONTINUED;
8638 break;
8639 }
8640
8641 if (BUFFER_POS_REACHED_P ())
8642 {
8643 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8644 goto buffer_pos_reached;
8645 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8646 {
8647 SAVE_IT (atpos_it, *it, atpos_data);
8648 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8649 }
8650 }
8651
8652 if (new_x > it->first_visible_x)
8653 {
8654 /* Glyph is visible. Increment number of glyphs that
8655 would be displayed. */
8656 ++it->hpos;
8657 }
8658 }
8659
8660 if (result != MOVE_UNDEFINED)
8661 break;
8662 }
8663 else if (BUFFER_POS_REACHED_P ())
8664 {
8665 buffer_pos_reached:
8666 IT_RESET_X_ASCENT_DESCENT (it);
8667 result = MOVE_POS_MATCH_OR_ZV;
8668 break;
8669 }
8670 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8671 {
8672 /* Stop when TO_X specified and reached. This check is
8673 necessary here because of lines consisting of a line end,
8674 only. The line end will not produce any glyphs and we
8675 would never get MOVE_X_REACHED. */
8676 eassert (it->nglyphs == 0);
8677 result = MOVE_X_REACHED;
8678 break;
8679 }
8680
8681 /* Is this a line end? If yes, we're done. */
8682 if (ITERATOR_AT_END_OF_LINE_P (it))
8683 {
8684 /* If we are past TO_CHARPOS, but never saw any character
8685 positions smaller than TO_CHARPOS, return
8686 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8687 did. */
8688 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8689 {
8690 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8691 {
8692 if (IT_CHARPOS (ppos_it) < ZV)
8693 {
8694 RESTORE_IT (it, &ppos_it, ppos_data);
8695 result = MOVE_POS_MATCH_OR_ZV;
8696 }
8697 else
8698 goto buffer_pos_reached;
8699 }
8700 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8701 && IT_CHARPOS (*it) > to_charpos)
8702 goto buffer_pos_reached;
8703 else
8704 result = MOVE_NEWLINE_OR_CR;
8705 }
8706 else
8707 result = MOVE_NEWLINE_OR_CR;
8708 break;
8709 }
8710
8711 prev_method = it->method;
8712 if (it->method == GET_FROM_BUFFER)
8713 prev_pos = IT_CHARPOS (*it);
8714 /* The current display element has been consumed. Advance
8715 to the next. */
8716 set_iterator_to_next (it, 1);
8717 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8718 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8719 if (IT_CHARPOS (*it) < to_charpos)
8720 saw_smaller_pos = 1;
8721 if (it->bidi_p
8722 && (op & MOVE_TO_POS)
8723 && IT_CHARPOS (*it) >= to_charpos
8724 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8725 SAVE_IT (ppos_it, *it, ppos_data);
8726
8727 /* Stop if lines are truncated and IT's current x-position is
8728 past the right edge of the window now. */
8729 if (it->line_wrap == TRUNCATE
8730 && it->current_x >= it->last_visible_x)
8731 {
8732 if (!FRAME_WINDOW_P (it->f)
8733 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8734 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8735 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8736 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8737 {
8738 int at_eob_p = 0;
8739
8740 if ((at_eob_p = !get_next_display_element (it))
8741 || BUFFER_POS_REACHED_P ()
8742 /* If we are past TO_CHARPOS, but never saw any
8743 character positions smaller than TO_CHARPOS,
8744 return MOVE_POS_MATCH_OR_ZV, like the
8745 unidirectional display did. */
8746 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8747 && !saw_smaller_pos
8748 && IT_CHARPOS (*it) > to_charpos))
8749 {
8750 if (it->bidi_p
8751 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8752 RESTORE_IT (it, &ppos_it, ppos_data);
8753 result = MOVE_POS_MATCH_OR_ZV;
8754 break;
8755 }
8756 if (ITERATOR_AT_END_OF_LINE_P (it))
8757 {
8758 result = MOVE_NEWLINE_OR_CR;
8759 break;
8760 }
8761 }
8762 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8763 && !saw_smaller_pos
8764 && IT_CHARPOS (*it) > to_charpos)
8765 {
8766 if (IT_CHARPOS (ppos_it) < ZV)
8767 RESTORE_IT (it, &ppos_it, ppos_data);
8768 result = MOVE_POS_MATCH_OR_ZV;
8769 break;
8770 }
8771 result = MOVE_LINE_TRUNCATED;
8772 break;
8773 }
8774 #undef IT_RESET_X_ASCENT_DESCENT
8775 }
8776
8777 #undef BUFFER_POS_REACHED_P
8778
8779 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8780 restore the saved iterator. */
8781 if (atpos_it.sp >= 0)
8782 RESTORE_IT (it, &atpos_it, atpos_data);
8783 else if (atx_it.sp >= 0)
8784 RESTORE_IT (it, &atx_it, atx_data);
8785
8786 done:
8787
8788 if (atpos_data)
8789 bidi_unshelve_cache (atpos_data, 1);
8790 if (atx_data)
8791 bidi_unshelve_cache (atx_data, 1);
8792 if (wrap_data)
8793 bidi_unshelve_cache (wrap_data, 1);
8794 if (ppos_data)
8795 bidi_unshelve_cache (ppos_data, 1);
8796
8797 /* Restore the iterator settings altered at the beginning of this
8798 function. */
8799 it->glyph_row = saved_glyph_row;
8800 return result;
8801 }
8802
8803 /* For external use. */
8804 void
8805 move_it_in_display_line (struct it *it,
8806 ptrdiff_t to_charpos, int to_x,
8807 enum move_operation_enum op)
8808 {
8809 if (it->line_wrap == WORD_WRAP
8810 && (op & MOVE_TO_X))
8811 {
8812 struct it save_it;
8813 void *save_data = NULL;
8814 int skip;
8815
8816 SAVE_IT (save_it, *it, save_data);
8817 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8818 /* When word-wrap is on, TO_X may lie past the end
8819 of a wrapped line. Then it->current is the
8820 character on the next line, so backtrack to the
8821 space before the wrap point. */
8822 if (skip == MOVE_LINE_CONTINUED)
8823 {
8824 int prev_x = max (it->current_x - 1, 0);
8825 RESTORE_IT (it, &save_it, save_data);
8826 move_it_in_display_line_to
8827 (it, -1, prev_x, MOVE_TO_X);
8828 }
8829 else
8830 bidi_unshelve_cache (save_data, 1);
8831 }
8832 else
8833 move_it_in_display_line_to (it, to_charpos, to_x, op);
8834 }
8835
8836
8837 /* Move IT forward until it satisfies one or more of the criteria in
8838 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8839
8840 OP is a bit-mask that specifies where to stop, and in particular,
8841 which of those four position arguments makes a difference. See the
8842 description of enum move_operation_enum.
8843
8844 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8845 screen line, this function will set IT to the next position that is
8846 displayed to the right of TO_CHARPOS on the screen. */
8847
8848 void
8849 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8850 {
8851 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8852 int line_height, line_start_x = 0, reached = 0;
8853 void *backup_data = NULL;
8854
8855 for (;;)
8856 {
8857 if (op & MOVE_TO_VPOS)
8858 {
8859 /* If no TO_CHARPOS and no TO_X specified, stop at the
8860 start of the line TO_VPOS. */
8861 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8862 {
8863 if (it->vpos == to_vpos)
8864 {
8865 reached = 1;
8866 break;
8867 }
8868 else
8869 skip = move_it_in_display_line_to (it, -1, -1, 0);
8870 }
8871 else
8872 {
8873 /* TO_VPOS >= 0 means stop at TO_X in the line at
8874 TO_VPOS, or at TO_POS, whichever comes first. */
8875 if (it->vpos == to_vpos)
8876 {
8877 reached = 2;
8878 break;
8879 }
8880
8881 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8882
8883 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8884 {
8885 reached = 3;
8886 break;
8887 }
8888 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8889 {
8890 /* We have reached TO_X but not in the line we want. */
8891 skip = move_it_in_display_line_to (it, to_charpos,
8892 -1, MOVE_TO_POS);
8893 if (skip == MOVE_POS_MATCH_OR_ZV)
8894 {
8895 reached = 4;
8896 break;
8897 }
8898 }
8899 }
8900 }
8901 else if (op & MOVE_TO_Y)
8902 {
8903 struct it it_backup;
8904
8905 if (it->line_wrap == WORD_WRAP)
8906 SAVE_IT (it_backup, *it, backup_data);
8907
8908 /* TO_Y specified means stop at TO_X in the line containing
8909 TO_Y---or at TO_CHARPOS if this is reached first. The
8910 problem is that we can't really tell whether the line
8911 contains TO_Y before we have completely scanned it, and
8912 this may skip past TO_X. What we do is to first scan to
8913 TO_X.
8914
8915 If TO_X is not specified, use a TO_X of zero. The reason
8916 is to make the outcome of this function more predictable.
8917 If we didn't use TO_X == 0, we would stop at the end of
8918 the line which is probably not what a caller would expect
8919 to happen. */
8920 skip = move_it_in_display_line_to
8921 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8922 (MOVE_TO_X | (op & MOVE_TO_POS)));
8923
8924 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8925 if (skip == MOVE_POS_MATCH_OR_ZV)
8926 reached = 5;
8927 else if (skip == MOVE_X_REACHED)
8928 {
8929 /* If TO_X was reached, we want to know whether TO_Y is
8930 in the line. We know this is the case if the already
8931 scanned glyphs make the line tall enough. Otherwise,
8932 we must check by scanning the rest of the line. */
8933 line_height = it->max_ascent + it->max_descent;
8934 if (to_y >= it->current_y
8935 && to_y < it->current_y + line_height)
8936 {
8937 reached = 6;
8938 break;
8939 }
8940 SAVE_IT (it_backup, *it, backup_data);
8941 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8942 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8943 op & MOVE_TO_POS);
8944 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8945 line_height = it->max_ascent + it->max_descent;
8946 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8947
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8950 {
8951 /* If TO_Y is in this line and TO_X was reached
8952 above, we scanned too far. We have to restore
8953 IT's settings to the ones before skipping. But
8954 keep the more accurate values of max_ascent and
8955 max_descent we've found while skipping the rest
8956 of the line, for the sake of callers, such as
8957 pos_visible_p, that need to know the line
8958 height. */
8959 int max_ascent = it->max_ascent;
8960 int max_descent = it->max_descent;
8961
8962 RESTORE_IT (it, &it_backup, backup_data);
8963 it->max_ascent = max_ascent;
8964 it->max_descent = max_descent;
8965 reached = 6;
8966 }
8967 else
8968 {
8969 skip = skip2;
8970 if (skip == MOVE_POS_MATCH_OR_ZV)
8971 reached = 7;
8972 }
8973 }
8974 else
8975 {
8976 /* Check whether TO_Y is in this line. */
8977 line_height = it->max_ascent + it->max_descent;
8978 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8979
8980 if (to_y >= it->current_y
8981 && to_y < it->current_y + line_height)
8982 {
8983 /* When word-wrap is on, TO_X may lie past the end
8984 of a wrapped line. Then it->current is the
8985 character on the next line, so backtrack to the
8986 space before the wrap point. */
8987 if (skip == MOVE_LINE_CONTINUED
8988 && it->line_wrap == WORD_WRAP)
8989 {
8990 int prev_x = max (it->current_x - 1, 0);
8991 RESTORE_IT (it, &it_backup, backup_data);
8992 skip = move_it_in_display_line_to
8993 (it, -1, prev_x, MOVE_TO_X);
8994 }
8995 reached = 6;
8996 }
8997 }
8998
8999 if (reached)
9000 break;
9001 }
9002 else if (BUFFERP (it->object)
9003 && (it->method == GET_FROM_BUFFER
9004 || it->method == GET_FROM_STRETCH)
9005 && IT_CHARPOS (*it) >= to_charpos
9006 /* Under bidi iteration, a call to set_iterator_to_next
9007 can scan far beyond to_charpos if the initial
9008 portion of the next line needs to be reordered. In
9009 that case, give move_it_in_display_line_to another
9010 chance below. */
9011 && !(it->bidi_p
9012 && it->bidi_it.scan_dir == -1))
9013 skip = MOVE_POS_MATCH_OR_ZV;
9014 else
9015 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9016
9017 switch (skip)
9018 {
9019 case MOVE_POS_MATCH_OR_ZV:
9020 reached = 8;
9021 goto out;
9022
9023 case MOVE_NEWLINE_OR_CR:
9024 set_iterator_to_next (it, 1);
9025 it->continuation_lines_width = 0;
9026 break;
9027
9028 case MOVE_LINE_TRUNCATED:
9029 it->continuation_lines_width = 0;
9030 reseat_at_next_visible_line_start (it, 0);
9031 if ((op & MOVE_TO_POS) != 0
9032 && IT_CHARPOS (*it) > to_charpos)
9033 {
9034 reached = 9;
9035 goto out;
9036 }
9037 break;
9038
9039 case MOVE_LINE_CONTINUED:
9040 /* For continued lines ending in a tab, some of the glyphs
9041 associated with the tab are displayed on the current
9042 line. Since it->current_x does not include these glyphs,
9043 we use it->last_visible_x instead. */
9044 if (it->c == '\t')
9045 {
9046 it->continuation_lines_width += it->last_visible_x;
9047 /* When moving by vpos, ensure that the iterator really
9048 advances to the next line (bug#847, bug#969). Fixme:
9049 do we need to do this in other circumstances? */
9050 if (it->current_x != it->last_visible_x
9051 && (op & MOVE_TO_VPOS)
9052 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9053 {
9054 line_start_x = it->current_x + it->pixel_width
9055 - it->last_visible_x;
9056 set_iterator_to_next (it, 0);
9057 }
9058 }
9059 else
9060 it->continuation_lines_width += it->current_x;
9061 break;
9062
9063 default:
9064 emacs_abort ();
9065 }
9066
9067 /* Reset/increment for the next run. */
9068 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9069 it->current_x = line_start_x;
9070 line_start_x = 0;
9071 it->hpos = 0;
9072 it->current_y += it->max_ascent + it->max_descent;
9073 ++it->vpos;
9074 last_height = it->max_ascent + it->max_descent;
9075 it->max_ascent = it->max_descent = 0;
9076 }
9077
9078 out:
9079
9080 /* On text terminals, we may stop at the end of a line in the middle
9081 of a multi-character glyph. If the glyph itself is continued,
9082 i.e. it is actually displayed on the next line, don't treat this
9083 stopping point as valid; move to the next line instead (unless
9084 that brings us offscreen). */
9085 if (!FRAME_WINDOW_P (it->f)
9086 && op & MOVE_TO_POS
9087 && IT_CHARPOS (*it) == to_charpos
9088 && it->what == IT_CHARACTER
9089 && it->nglyphs > 1
9090 && it->line_wrap == WINDOW_WRAP
9091 && it->current_x == it->last_visible_x - 1
9092 && it->c != '\n'
9093 && it->c != '\t'
9094 && it->vpos < it->w->window_end_vpos)
9095 {
9096 it->continuation_lines_width += it->current_x;
9097 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9098 it->current_y += it->max_ascent + it->max_descent;
9099 ++it->vpos;
9100 last_height = it->max_ascent + it->max_descent;
9101 }
9102
9103 if (backup_data)
9104 bidi_unshelve_cache (backup_data, 1);
9105
9106 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9107 }
9108
9109
9110 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9111
9112 If DY > 0, move IT backward at least that many pixels. DY = 0
9113 means move IT backward to the preceding line start or BEGV. This
9114 function may move over more than DY pixels if IT->current_y - DY
9115 ends up in the middle of a line; in this case IT->current_y will be
9116 set to the top of the line moved to. */
9117
9118 void
9119 move_it_vertically_backward (struct it *it, int dy)
9120 {
9121 int nlines, h;
9122 struct it it2, it3;
9123 void *it2data = NULL, *it3data = NULL;
9124 ptrdiff_t start_pos;
9125 int nchars_per_row
9126 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9127 ptrdiff_t pos_limit;
9128
9129 move_further_back:
9130 eassert (dy >= 0);
9131
9132 start_pos = IT_CHARPOS (*it);
9133
9134 /* Estimate how many newlines we must move back. */
9135 nlines = max (1, dy / default_line_pixel_height (it->w));
9136 if (it->line_wrap == TRUNCATE)
9137 pos_limit = BEGV;
9138 else
9139 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9140
9141 /* Set the iterator's position that many lines back. But don't go
9142 back more than NLINES full screen lines -- this wins a day with
9143 buffers which have very long lines. */
9144 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9145 back_to_previous_visible_line_start (it);
9146
9147 /* Reseat the iterator here. When moving backward, we don't want
9148 reseat to skip forward over invisible text, set up the iterator
9149 to deliver from overlay strings at the new position etc. So,
9150 use reseat_1 here. */
9151 reseat_1 (it, it->current.pos, 1);
9152
9153 /* We are now surely at a line start. */
9154 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9155 reordering is in effect. */
9156 it->continuation_lines_width = 0;
9157
9158 /* Move forward and see what y-distance we moved. First move to the
9159 start of the next line so that we get its height. We need this
9160 height to be able to tell whether we reached the specified
9161 y-distance. */
9162 SAVE_IT (it2, *it, it2data);
9163 it2.max_ascent = it2.max_descent = 0;
9164 do
9165 {
9166 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9167 MOVE_TO_POS | MOVE_TO_VPOS);
9168 }
9169 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9170 /* If we are in a display string which starts at START_POS,
9171 and that display string includes a newline, and we are
9172 right after that newline (i.e. at the beginning of a
9173 display line), exit the loop, because otherwise we will
9174 infloop, since move_it_to will see that it is already at
9175 START_POS and will not move. */
9176 || (it2.method == GET_FROM_STRING
9177 && IT_CHARPOS (it2) == start_pos
9178 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9179 eassert (IT_CHARPOS (*it) >= BEGV);
9180 SAVE_IT (it3, it2, it3data);
9181
9182 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9183 eassert (IT_CHARPOS (*it) >= BEGV);
9184 /* H is the actual vertical distance from the position in *IT
9185 and the starting position. */
9186 h = it2.current_y - it->current_y;
9187 /* NLINES is the distance in number of lines. */
9188 nlines = it2.vpos - it->vpos;
9189
9190 /* Correct IT's y and vpos position
9191 so that they are relative to the starting point. */
9192 it->vpos -= nlines;
9193 it->current_y -= h;
9194
9195 if (dy == 0)
9196 {
9197 /* DY == 0 means move to the start of the screen line. The
9198 value of nlines is > 0 if continuation lines were involved,
9199 or if the original IT position was at start of a line. */
9200 RESTORE_IT (it, it, it2data);
9201 if (nlines > 0)
9202 move_it_by_lines (it, nlines);
9203 /* The above code moves us to some position NLINES down,
9204 usually to its first glyph (leftmost in an L2R line), but
9205 that's not necessarily the start of the line, under bidi
9206 reordering. We want to get to the character position
9207 that is immediately after the newline of the previous
9208 line. */
9209 if (it->bidi_p
9210 && !it->continuation_lines_width
9211 && !STRINGP (it->string)
9212 && IT_CHARPOS (*it) > BEGV
9213 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9214 {
9215 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9216
9217 DEC_BOTH (cp, bp);
9218 cp = find_newline_no_quit (cp, bp, -1, NULL);
9219 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9220 }
9221 bidi_unshelve_cache (it3data, 1);
9222 }
9223 else
9224 {
9225 /* The y-position we try to reach, relative to *IT.
9226 Note that H has been subtracted in front of the if-statement. */
9227 int target_y = it->current_y + h - dy;
9228 int y0 = it3.current_y;
9229 int y1;
9230 int line_height;
9231
9232 RESTORE_IT (&it3, &it3, it3data);
9233 y1 = line_bottom_y (&it3);
9234 line_height = y1 - y0;
9235 RESTORE_IT (it, it, it2data);
9236 /* If we did not reach target_y, try to move further backward if
9237 we can. If we moved too far backward, try to move forward. */
9238 if (target_y < it->current_y
9239 /* This is heuristic. In a window that's 3 lines high, with
9240 a line height of 13 pixels each, recentering with point
9241 on the bottom line will try to move -39/2 = 19 pixels
9242 backward. Try to avoid moving into the first line. */
9243 && (it->current_y - target_y
9244 > min (window_box_height (it->w), line_height * 2 / 3))
9245 && IT_CHARPOS (*it) > BEGV)
9246 {
9247 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9248 target_y - it->current_y));
9249 dy = it->current_y - target_y;
9250 goto move_further_back;
9251 }
9252 else if (target_y >= it->current_y + line_height
9253 && IT_CHARPOS (*it) < ZV)
9254 {
9255 /* Should move forward by at least one line, maybe more.
9256
9257 Note: Calling move_it_by_lines can be expensive on
9258 terminal frames, where compute_motion is used (via
9259 vmotion) to do the job, when there are very long lines
9260 and truncate-lines is nil. That's the reason for
9261 treating terminal frames specially here. */
9262
9263 if (!FRAME_WINDOW_P (it->f))
9264 move_it_vertically (it, target_y - (it->current_y + line_height));
9265 else
9266 {
9267 do
9268 {
9269 move_it_by_lines (it, 1);
9270 }
9271 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9272 }
9273 }
9274 }
9275 }
9276
9277
9278 /* Move IT by a specified amount of pixel lines DY. DY negative means
9279 move backwards. DY = 0 means move to start of screen line. At the
9280 end, IT will be on the start of a screen line. */
9281
9282 void
9283 move_it_vertically (struct it *it, int dy)
9284 {
9285 if (dy <= 0)
9286 move_it_vertically_backward (it, -dy);
9287 else
9288 {
9289 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9290 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9291 MOVE_TO_POS | MOVE_TO_Y);
9292 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9293
9294 /* If buffer ends in ZV without a newline, move to the start of
9295 the line to satisfy the post-condition. */
9296 if (IT_CHARPOS (*it) == ZV
9297 && ZV > BEGV
9298 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9299 move_it_by_lines (it, 0);
9300 }
9301 }
9302
9303
9304 /* Move iterator IT past the end of the text line it is in. */
9305
9306 void
9307 move_it_past_eol (struct it *it)
9308 {
9309 enum move_it_result rc;
9310
9311 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9312 if (rc == MOVE_NEWLINE_OR_CR)
9313 set_iterator_to_next (it, 0);
9314 }
9315
9316
9317 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9318 negative means move up. DVPOS == 0 means move to the start of the
9319 screen line.
9320
9321 Optimization idea: If we would know that IT->f doesn't use
9322 a face with proportional font, we could be faster for
9323 truncate-lines nil. */
9324
9325 void
9326 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9327 {
9328
9329 /* The commented-out optimization uses vmotion on terminals. This
9330 gives bad results, because elements like it->what, on which
9331 callers such as pos_visible_p rely, aren't updated. */
9332 /* struct position pos;
9333 if (!FRAME_WINDOW_P (it->f))
9334 {
9335 struct text_pos textpos;
9336
9337 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9338 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9339 reseat (it, textpos, 1);
9340 it->vpos += pos.vpos;
9341 it->current_y += pos.vpos;
9342 }
9343 else */
9344
9345 if (dvpos == 0)
9346 {
9347 /* DVPOS == 0 means move to the start of the screen line. */
9348 move_it_vertically_backward (it, 0);
9349 /* Let next call to line_bottom_y calculate real line height. */
9350 last_height = 0;
9351 }
9352 else if (dvpos > 0)
9353 {
9354 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9355 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9356 {
9357 /* Only move to the next buffer position if we ended up in a
9358 string from display property, not in an overlay string
9359 (before-string or after-string). That is because the
9360 latter don't conceal the underlying buffer position, so
9361 we can ask to move the iterator to the exact position we
9362 are interested in. Note that, even if we are already at
9363 IT_CHARPOS (*it), the call below is not a no-op, as it
9364 will detect that we are at the end of the string, pop the
9365 iterator, and compute it->current_x and it->hpos
9366 correctly. */
9367 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9368 -1, -1, -1, MOVE_TO_POS);
9369 }
9370 }
9371 else
9372 {
9373 struct it it2;
9374 void *it2data = NULL;
9375 ptrdiff_t start_charpos, i;
9376 int nchars_per_row
9377 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9378 ptrdiff_t pos_limit;
9379
9380 /* Start at the beginning of the screen line containing IT's
9381 position. This may actually move vertically backwards,
9382 in case of overlays, so adjust dvpos accordingly. */
9383 dvpos += it->vpos;
9384 move_it_vertically_backward (it, 0);
9385 dvpos -= it->vpos;
9386
9387 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9388 screen lines, and reseat the iterator there. */
9389 start_charpos = IT_CHARPOS (*it);
9390 if (it->line_wrap == TRUNCATE)
9391 pos_limit = BEGV;
9392 else
9393 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9394 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9395 back_to_previous_visible_line_start (it);
9396 reseat (it, it->current.pos, 1);
9397
9398 /* Move further back if we end up in a string or an image. */
9399 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9400 {
9401 /* First try to move to start of display line. */
9402 dvpos += it->vpos;
9403 move_it_vertically_backward (it, 0);
9404 dvpos -= it->vpos;
9405 if (IT_POS_VALID_AFTER_MOVE_P (it))
9406 break;
9407 /* If start of line is still in string or image,
9408 move further back. */
9409 back_to_previous_visible_line_start (it);
9410 reseat (it, it->current.pos, 1);
9411 dvpos--;
9412 }
9413
9414 it->current_x = it->hpos = 0;
9415
9416 /* Above call may have moved too far if continuation lines
9417 are involved. Scan forward and see if it did. */
9418 SAVE_IT (it2, *it, it2data);
9419 it2.vpos = it2.current_y = 0;
9420 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9421 it->vpos -= it2.vpos;
9422 it->current_y -= it2.current_y;
9423 it->current_x = it->hpos = 0;
9424
9425 /* If we moved too far back, move IT some lines forward. */
9426 if (it2.vpos > -dvpos)
9427 {
9428 int delta = it2.vpos + dvpos;
9429
9430 RESTORE_IT (&it2, &it2, it2data);
9431 SAVE_IT (it2, *it, it2data);
9432 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9433 /* Move back again if we got too far ahead. */
9434 if (IT_CHARPOS (*it) >= start_charpos)
9435 RESTORE_IT (it, &it2, it2data);
9436 else
9437 bidi_unshelve_cache (it2data, 1);
9438 }
9439 else
9440 RESTORE_IT (it, it, it2data);
9441 }
9442 }
9443
9444 /* Return 1 if IT points into the middle of a display vector. */
9445
9446 int
9447 in_display_vector_p (struct it *it)
9448 {
9449 return (it->method == GET_FROM_DISPLAY_VECTOR
9450 && it->current.dpvec_index > 0
9451 && it->dpvec + it->current.dpvec_index != it->dpend);
9452 }
9453
9454 \f
9455 /***********************************************************************
9456 Messages
9457 ***********************************************************************/
9458
9459
9460 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9461 to *Messages*. */
9462
9463 void
9464 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9465 {
9466 Lisp_Object args[3];
9467 Lisp_Object msg, fmt;
9468 char *buffer;
9469 ptrdiff_t len;
9470 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9471 USE_SAFE_ALLOCA;
9472
9473 fmt = msg = Qnil;
9474 GCPRO4 (fmt, msg, arg1, arg2);
9475
9476 args[0] = fmt = build_string (format);
9477 args[1] = arg1;
9478 args[2] = arg2;
9479 msg = Fformat (3, args);
9480
9481 len = SBYTES (msg) + 1;
9482 buffer = SAFE_ALLOCA (len);
9483 memcpy (buffer, SDATA (msg), len);
9484
9485 message_dolog (buffer, len - 1, 1, 0);
9486 SAFE_FREE ();
9487
9488 UNGCPRO;
9489 }
9490
9491
9492 /* Output a newline in the *Messages* buffer if "needs" one. */
9493
9494 void
9495 message_log_maybe_newline (void)
9496 {
9497 if (message_log_need_newline)
9498 message_dolog ("", 0, 1, 0);
9499 }
9500
9501
9502 /* Add a string M of length NBYTES to the message log, optionally
9503 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9504 true, means interpret the contents of M as multibyte. This
9505 function calls low-level routines in order to bypass text property
9506 hooks, etc. which might not be safe to run.
9507
9508 This may GC (insert may run before/after change hooks),
9509 so the buffer M must NOT point to a Lisp string. */
9510
9511 void
9512 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9513 {
9514 const unsigned char *msg = (const unsigned char *) m;
9515
9516 if (!NILP (Vmemory_full))
9517 return;
9518
9519 if (!NILP (Vmessage_log_max))
9520 {
9521 struct buffer *oldbuf;
9522 Lisp_Object oldpoint, oldbegv, oldzv;
9523 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9524 ptrdiff_t point_at_end = 0;
9525 ptrdiff_t zv_at_end = 0;
9526 Lisp_Object old_deactivate_mark;
9527 struct gcpro gcpro1;
9528
9529 old_deactivate_mark = Vdeactivate_mark;
9530 oldbuf = current_buffer;
9531
9532 /* Ensure the Messages buffer exists, and switch to it.
9533 If we created it, set the major-mode. */
9534 {
9535 int newbuffer = 0;
9536 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9537
9538 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9539
9540 if (newbuffer
9541 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9542 call0 (intern ("messages-buffer-mode"));
9543 }
9544
9545 bset_undo_list (current_buffer, Qt);
9546 bset_cache_long_scans (current_buffer, Qnil);
9547
9548 oldpoint = message_dolog_marker1;
9549 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9550 oldbegv = message_dolog_marker2;
9551 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9552 oldzv = message_dolog_marker3;
9553 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9554 GCPRO1 (old_deactivate_mark);
9555
9556 if (PT == Z)
9557 point_at_end = 1;
9558 if (ZV == Z)
9559 zv_at_end = 1;
9560
9561 BEGV = BEG;
9562 BEGV_BYTE = BEG_BYTE;
9563 ZV = Z;
9564 ZV_BYTE = Z_BYTE;
9565 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9566
9567 /* Insert the string--maybe converting multibyte to single byte
9568 or vice versa, so that all the text fits the buffer. */
9569 if (multibyte
9570 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9571 {
9572 ptrdiff_t i;
9573 int c, char_bytes;
9574 char work[1];
9575
9576 /* Convert a multibyte string to single-byte
9577 for the *Message* buffer. */
9578 for (i = 0; i < nbytes; i += char_bytes)
9579 {
9580 c = string_char_and_length (msg + i, &char_bytes);
9581 work[0] = (ASCII_CHAR_P (c)
9582 ? c
9583 : multibyte_char_to_unibyte (c));
9584 insert_1_both (work, 1, 1, 1, 0, 0);
9585 }
9586 }
9587 else if (! multibyte
9588 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9589 {
9590 ptrdiff_t i;
9591 int c, char_bytes;
9592 unsigned char str[MAX_MULTIBYTE_LENGTH];
9593 /* Convert a single-byte string to multibyte
9594 for the *Message* buffer. */
9595 for (i = 0; i < nbytes; i++)
9596 {
9597 c = msg[i];
9598 MAKE_CHAR_MULTIBYTE (c);
9599 char_bytes = CHAR_STRING (c, str);
9600 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9601 }
9602 }
9603 else if (nbytes)
9604 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9605
9606 if (nlflag)
9607 {
9608 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9609 printmax_t dups;
9610
9611 insert_1_both ("\n", 1, 1, 1, 0, 0);
9612
9613 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9614 this_bol = PT;
9615 this_bol_byte = PT_BYTE;
9616
9617 /* See if this line duplicates the previous one.
9618 If so, combine duplicates. */
9619 if (this_bol > BEG)
9620 {
9621 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9622 prev_bol = PT;
9623 prev_bol_byte = PT_BYTE;
9624
9625 dups = message_log_check_duplicate (prev_bol_byte,
9626 this_bol_byte);
9627 if (dups)
9628 {
9629 del_range_both (prev_bol, prev_bol_byte,
9630 this_bol, this_bol_byte, 0);
9631 if (dups > 1)
9632 {
9633 char dupstr[sizeof " [ times]"
9634 + INT_STRLEN_BOUND (printmax_t)];
9635
9636 /* If you change this format, don't forget to also
9637 change message_log_check_duplicate. */
9638 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9639 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9640 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9641 }
9642 }
9643 }
9644
9645 /* If we have more than the desired maximum number of lines
9646 in the *Messages* buffer now, delete the oldest ones.
9647 This is safe because we don't have undo in this buffer. */
9648
9649 if (NATNUMP (Vmessage_log_max))
9650 {
9651 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9652 -XFASTINT (Vmessage_log_max) - 1, 0);
9653 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9654 }
9655 }
9656 BEGV = marker_position (oldbegv);
9657 BEGV_BYTE = marker_byte_position (oldbegv);
9658
9659 if (zv_at_end)
9660 {
9661 ZV = Z;
9662 ZV_BYTE = Z_BYTE;
9663 }
9664 else
9665 {
9666 ZV = marker_position (oldzv);
9667 ZV_BYTE = marker_byte_position (oldzv);
9668 }
9669
9670 if (point_at_end)
9671 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9672 else
9673 /* We can't do Fgoto_char (oldpoint) because it will run some
9674 Lisp code. */
9675 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9676 marker_byte_position (oldpoint));
9677
9678 UNGCPRO;
9679 unchain_marker (XMARKER (oldpoint));
9680 unchain_marker (XMARKER (oldbegv));
9681 unchain_marker (XMARKER (oldzv));
9682
9683 /* We called insert_1_both above with its 5th argument (PREPARE)
9684 zero, which prevents insert_1_both from calling
9685 prepare_to_modify_buffer, which in turns prevents us from
9686 incrementing windows_or_buffers_changed even if *Messages* is
9687 shown in some window. So we must manually set
9688 windows_or_buffers_changed here to make up for that. */
9689 windows_or_buffers_changed = old_windows_or_buffers_changed;
9690 bset_redisplay (current_buffer);
9691
9692 set_buffer_internal (oldbuf);
9693
9694 message_log_need_newline = !nlflag;
9695 Vdeactivate_mark = old_deactivate_mark;
9696 }
9697 }
9698
9699
9700 /* We are at the end of the buffer after just having inserted a newline.
9701 (Note: We depend on the fact we won't be crossing the gap.)
9702 Check to see if the most recent message looks a lot like the previous one.
9703 Return 0 if different, 1 if the new one should just replace it, or a
9704 value N > 1 if we should also append " [N times]". */
9705
9706 static intmax_t
9707 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9708 {
9709 ptrdiff_t i;
9710 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9711 int seen_dots = 0;
9712 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9713 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9714
9715 for (i = 0; i < len; i++)
9716 {
9717 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9718 seen_dots = 1;
9719 if (p1[i] != p2[i])
9720 return seen_dots;
9721 }
9722 p1 += len;
9723 if (*p1 == '\n')
9724 return 2;
9725 if (*p1++ == ' ' && *p1++ == '[')
9726 {
9727 char *pend;
9728 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9729 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9730 return n + 1;
9731 }
9732 return 0;
9733 }
9734 \f
9735
9736 /* Display an echo area message M with a specified length of NBYTES
9737 bytes. The string may include null characters. If M is not a
9738 string, clear out any existing message, and let the mini-buffer
9739 text show through.
9740
9741 This function cancels echoing. */
9742
9743 void
9744 message3 (Lisp_Object m)
9745 {
9746 struct gcpro gcpro1;
9747
9748 GCPRO1 (m);
9749 clear_message (true, true);
9750 cancel_echoing ();
9751
9752 /* First flush out any partial line written with print. */
9753 message_log_maybe_newline ();
9754 if (STRINGP (m))
9755 {
9756 ptrdiff_t nbytes = SBYTES (m);
9757 bool multibyte = STRING_MULTIBYTE (m);
9758 USE_SAFE_ALLOCA;
9759 char *buffer = SAFE_ALLOCA (nbytes);
9760 memcpy (buffer, SDATA (m), nbytes);
9761 message_dolog (buffer, nbytes, 1, multibyte);
9762 SAFE_FREE ();
9763 }
9764 message3_nolog (m);
9765
9766 UNGCPRO;
9767 }
9768
9769
9770 /* The non-logging version of message3.
9771 This does not cancel echoing, because it is used for echoing.
9772 Perhaps we need to make a separate function for echoing
9773 and make this cancel echoing. */
9774
9775 void
9776 message3_nolog (Lisp_Object m)
9777 {
9778 struct frame *sf = SELECTED_FRAME ();
9779
9780 if (FRAME_INITIAL_P (sf))
9781 {
9782 if (noninteractive_need_newline)
9783 putc ('\n', stderr);
9784 noninteractive_need_newline = 0;
9785 if (STRINGP (m))
9786 {
9787 Lisp_Object s = ENCODE_SYSTEM (m);
9788
9789 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9790 }
9791 if (cursor_in_echo_area == 0)
9792 fprintf (stderr, "\n");
9793 fflush (stderr);
9794 }
9795 /* Error messages get reported properly by cmd_error, so this must be just an
9796 informative message; if the frame hasn't really been initialized yet, just
9797 toss it. */
9798 else if (INTERACTIVE && sf->glyphs_initialized_p)
9799 {
9800 /* Get the frame containing the mini-buffer
9801 that the selected frame is using. */
9802 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9803 Lisp_Object frame = XWINDOW (mini_window)->frame;
9804 struct frame *f = XFRAME (frame);
9805
9806 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9807 Fmake_frame_visible (frame);
9808
9809 if (STRINGP (m) && SCHARS (m) > 0)
9810 {
9811 set_message (m);
9812 if (minibuffer_auto_raise)
9813 Fraise_frame (frame);
9814 /* Assume we are not echoing.
9815 (If we are, echo_now will override this.) */
9816 echo_message_buffer = Qnil;
9817 }
9818 else
9819 clear_message (true, true);
9820
9821 do_pending_window_change (0);
9822 echo_area_display (1);
9823 do_pending_window_change (0);
9824 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9825 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9826 }
9827 }
9828
9829
9830 /* Display a null-terminated echo area message M. If M is 0, clear
9831 out any existing message, and let the mini-buffer text show through.
9832
9833 The buffer M must continue to exist until after the echo area gets
9834 cleared or some other message gets displayed there. Do not pass
9835 text that is stored in a Lisp string. Do not pass text in a buffer
9836 that was alloca'd. */
9837
9838 void
9839 message1 (const char *m)
9840 {
9841 message3 (m ? build_unibyte_string (m) : Qnil);
9842 }
9843
9844
9845 /* The non-logging counterpart of message1. */
9846
9847 void
9848 message1_nolog (const char *m)
9849 {
9850 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9851 }
9852
9853 /* Display a message M which contains a single %s
9854 which gets replaced with STRING. */
9855
9856 void
9857 message_with_string (const char *m, Lisp_Object string, int log)
9858 {
9859 CHECK_STRING (string);
9860
9861 if (noninteractive)
9862 {
9863 if (m)
9864 {
9865 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
9866 String whose data pointer might be passed to us in M. So
9867 we use a local copy. */
9868 char *fmt = xstrdup (m);
9869
9870 if (noninteractive_need_newline)
9871 putc ('\n', stderr);
9872 noninteractive_need_newline = 0;
9873 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
9874 if (!cursor_in_echo_area)
9875 fprintf (stderr, "\n");
9876 fflush (stderr);
9877 xfree (fmt);
9878 }
9879 }
9880 else if (INTERACTIVE)
9881 {
9882 /* The frame whose minibuffer we're going to display the message on.
9883 It may be larger than the selected frame, so we need
9884 to use its buffer, not the selected frame's buffer. */
9885 Lisp_Object mini_window;
9886 struct frame *f, *sf = SELECTED_FRAME ();
9887
9888 /* Get the frame containing the minibuffer
9889 that the selected frame is using. */
9890 mini_window = FRAME_MINIBUF_WINDOW (sf);
9891 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9892
9893 /* Error messages get reported properly by cmd_error, so this must be
9894 just an informative message; if the frame hasn't really been
9895 initialized yet, just toss it. */
9896 if (f->glyphs_initialized_p)
9897 {
9898 Lisp_Object args[2], msg;
9899 struct gcpro gcpro1, gcpro2;
9900
9901 args[0] = build_string (m);
9902 args[1] = msg = string;
9903 GCPRO2 (args[0], msg);
9904 gcpro1.nvars = 2;
9905
9906 msg = Fformat (2, args);
9907
9908 if (log)
9909 message3 (msg);
9910 else
9911 message3_nolog (msg);
9912
9913 UNGCPRO;
9914
9915 /* Print should start at the beginning of the message
9916 buffer next time. */
9917 message_buf_print = 0;
9918 }
9919 }
9920 }
9921
9922
9923 /* Dump an informative message to the minibuf. If M is 0, clear out
9924 any existing message, and let the mini-buffer text show through. */
9925
9926 static void
9927 vmessage (const char *m, va_list ap)
9928 {
9929 if (noninteractive)
9930 {
9931 if (m)
9932 {
9933 if (noninteractive_need_newline)
9934 putc ('\n', stderr);
9935 noninteractive_need_newline = 0;
9936 vfprintf (stderr, m, ap);
9937 if (cursor_in_echo_area == 0)
9938 fprintf (stderr, "\n");
9939 fflush (stderr);
9940 }
9941 }
9942 else if (INTERACTIVE)
9943 {
9944 /* The frame whose mini-buffer we're going to display the message
9945 on. It may be larger than the selected frame, so we need to
9946 use its buffer, not the selected frame's buffer. */
9947 Lisp_Object mini_window;
9948 struct frame *f, *sf = SELECTED_FRAME ();
9949
9950 /* Get the frame containing the mini-buffer
9951 that the selected frame is using. */
9952 mini_window = FRAME_MINIBUF_WINDOW (sf);
9953 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9954
9955 /* Error messages get reported properly by cmd_error, so this must be
9956 just an informative message; if the frame hasn't really been
9957 initialized yet, just toss it. */
9958 if (f->glyphs_initialized_p)
9959 {
9960 if (m)
9961 {
9962 ptrdiff_t len;
9963 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9964 char *message_buf = alloca (maxsize + 1);
9965
9966 len = doprnt (message_buf, maxsize, m, 0, ap);
9967
9968 message3 (make_string (message_buf, len));
9969 }
9970 else
9971 message1 (0);
9972
9973 /* Print should start at the beginning of the message
9974 buffer next time. */
9975 message_buf_print = 0;
9976 }
9977 }
9978 }
9979
9980 void
9981 message (const char *m, ...)
9982 {
9983 va_list ap;
9984 va_start (ap, m);
9985 vmessage (m, ap);
9986 va_end (ap);
9987 }
9988
9989
9990 #if 0
9991 /* The non-logging version of message. */
9992
9993 void
9994 message_nolog (const char *m, ...)
9995 {
9996 Lisp_Object old_log_max;
9997 va_list ap;
9998 va_start (ap, m);
9999 old_log_max = Vmessage_log_max;
10000 Vmessage_log_max = Qnil;
10001 vmessage (m, ap);
10002 Vmessage_log_max = old_log_max;
10003 va_end (ap);
10004 }
10005 #endif
10006
10007
10008 /* Display the current message in the current mini-buffer. This is
10009 only called from error handlers in process.c, and is not time
10010 critical. */
10011
10012 void
10013 update_echo_area (void)
10014 {
10015 if (!NILP (echo_area_buffer[0]))
10016 {
10017 Lisp_Object string;
10018 string = Fcurrent_message ();
10019 message3 (string);
10020 }
10021 }
10022
10023
10024 /* Make sure echo area buffers in `echo_buffers' are live.
10025 If they aren't, make new ones. */
10026
10027 static void
10028 ensure_echo_area_buffers (void)
10029 {
10030 int i;
10031
10032 for (i = 0; i < 2; ++i)
10033 if (!BUFFERP (echo_buffer[i])
10034 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10035 {
10036 char name[30];
10037 Lisp_Object old_buffer;
10038 int j;
10039
10040 old_buffer = echo_buffer[i];
10041 echo_buffer[i] = Fget_buffer_create
10042 (make_formatted_string (name, " *Echo Area %d*", i));
10043 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10044 /* to force word wrap in echo area -
10045 it was decided to postpone this*/
10046 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10047
10048 for (j = 0; j < 2; ++j)
10049 if (EQ (old_buffer, echo_area_buffer[j]))
10050 echo_area_buffer[j] = echo_buffer[i];
10051 }
10052 }
10053
10054
10055 /* Call FN with args A1..A2 with either the current or last displayed
10056 echo_area_buffer as current buffer.
10057
10058 WHICH zero means use the current message buffer
10059 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10060 from echo_buffer[] and clear it.
10061
10062 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10063 suitable buffer from echo_buffer[] and clear it.
10064
10065 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10066 that the current message becomes the last displayed one, make
10067 choose a suitable buffer for echo_area_buffer[0], and clear it.
10068
10069 Value is what FN returns. */
10070
10071 static int
10072 with_echo_area_buffer (struct window *w, int which,
10073 int (*fn) (ptrdiff_t, Lisp_Object),
10074 ptrdiff_t a1, Lisp_Object a2)
10075 {
10076 Lisp_Object buffer;
10077 int this_one, the_other, clear_buffer_p, rc;
10078 ptrdiff_t count = SPECPDL_INDEX ();
10079
10080 /* If buffers aren't live, make new ones. */
10081 ensure_echo_area_buffers ();
10082
10083 clear_buffer_p = 0;
10084
10085 if (which == 0)
10086 this_one = 0, the_other = 1;
10087 else if (which > 0)
10088 this_one = 1, the_other = 0;
10089 else
10090 {
10091 this_one = 0, the_other = 1;
10092 clear_buffer_p = true;
10093
10094 /* We need a fresh one in case the current echo buffer equals
10095 the one containing the last displayed echo area message. */
10096 if (!NILP (echo_area_buffer[this_one])
10097 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10098 echo_area_buffer[this_one] = Qnil;
10099 }
10100
10101 /* Choose a suitable buffer from echo_buffer[] is we don't
10102 have one. */
10103 if (NILP (echo_area_buffer[this_one]))
10104 {
10105 echo_area_buffer[this_one]
10106 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10107 ? echo_buffer[the_other]
10108 : echo_buffer[this_one]);
10109 clear_buffer_p = true;
10110 }
10111
10112 buffer = echo_area_buffer[this_one];
10113
10114 /* Don't get confused by reusing the buffer used for echoing
10115 for a different purpose. */
10116 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10117 cancel_echoing ();
10118
10119 record_unwind_protect (unwind_with_echo_area_buffer,
10120 with_echo_area_buffer_unwind_data (w));
10121
10122 /* Make the echo area buffer current. Note that for display
10123 purposes, it is not necessary that the displayed window's buffer
10124 == current_buffer, except for text property lookup. So, let's
10125 only set that buffer temporarily here without doing a full
10126 Fset_window_buffer. We must also change w->pointm, though,
10127 because otherwise an assertions in unshow_buffer fails, and Emacs
10128 aborts. */
10129 set_buffer_internal_1 (XBUFFER (buffer));
10130 if (w)
10131 {
10132 wset_buffer (w, buffer);
10133 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10134 }
10135
10136 bset_undo_list (current_buffer, Qt);
10137 bset_read_only (current_buffer, Qnil);
10138 specbind (Qinhibit_read_only, Qt);
10139 specbind (Qinhibit_modification_hooks, Qt);
10140
10141 if (clear_buffer_p && Z > BEG)
10142 del_range (BEG, Z);
10143
10144 eassert (BEGV >= BEG);
10145 eassert (ZV <= Z && ZV >= BEGV);
10146
10147 rc = fn (a1, a2);
10148
10149 eassert (BEGV >= BEG);
10150 eassert (ZV <= Z && ZV >= BEGV);
10151
10152 unbind_to (count, Qnil);
10153 return rc;
10154 }
10155
10156
10157 /* Save state that should be preserved around the call to the function
10158 FN called in with_echo_area_buffer. */
10159
10160 static Lisp_Object
10161 with_echo_area_buffer_unwind_data (struct window *w)
10162 {
10163 int i = 0;
10164 Lisp_Object vector, tmp;
10165
10166 /* Reduce consing by keeping one vector in
10167 Vwith_echo_area_save_vector. */
10168 vector = Vwith_echo_area_save_vector;
10169 Vwith_echo_area_save_vector = Qnil;
10170
10171 if (NILP (vector))
10172 vector = Fmake_vector (make_number (9), Qnil);
10173
10174 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10175 ASET (vector, i, Vdeactivate_mark); ++i;
10176 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10177
10178 if (w)
10179 {
10180 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10181 ASET (vector, i, w->contents); ++i;
10182 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10183 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10184 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10185 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10186 }
10187 else
10188 {
10189 int end = i + 6;
10190 for (; i < end; ++i)
10191 ASET (vector, i, Qnil);
10192 }
10193
10194 eassert (i == ASIZE (vector));
10195 return vector;
10196 }
10197
10198
10199 /* Restore global state from VECTOR which was created by
10200 with_echo_area_buffer_unwind_data. */
10201
10202 static void
10203 unwind_with_echo_area_buffer (Lisp_Object vector)
10204 {
10205 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10206 Vdeactivate_mark = AREF (vector, 1);
10207 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10208
10209 if (WINDOWP (AREF (vector, 3)))
10210 {
10211 struct window *w;
10212 Lisp_Object buffer;
10213
10214 w = XWINDOW (AREF (vector, 3));
10215 buffer = AREF (vector, 4);
10216
10217 wset_buffer (w, buffer);
10218 set_marker_both (w->pointm, buffer,
10219 XFASTINT (AREF (vector, 5)),
10220 XFASTINT (AREF (vector, 6)));
10221 set_marker_both (w->start, buffer,
10222 XFASTINT (AREF (vector, 7)),
10223 XFASTINT (AREF (vector, 8)));
10224 }
10225
10226 Vwith_echo_area_save_vector = vector;
10227 }
10228
10229
10230 /* Set up the echo area for use by print functions. MULTIBYTE_P
10231 non-zero means we will print multibyte. */
10232
10233 void
10234 setup_echo_area_for_printing (int multibyte_p)
10235 {
10236 /* If we can't find an echo area any more, exit. */
10237 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10238 Fkill_emacs (Qnil);
10239
10240 ensure_echo_area_buffers ();
10241
10242 if (!message_buf_print)
10243 {
10244 /* A message has been output since the last time we printed.
10245 Choose a fresh echo area buffer. */
10246 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10247 echo_area_buffer[0] = echo_buffer[1];
10248 else
10249 echo_area_buffer[0] = echo_buffer[0];
10250
10251 /* Switch to that buffer and clear it. */
10252 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10253 bset_truncate_lines (current_buffer, Qnil);
10254
10255 if (Z > BEG)
10256 {
10257 ptrdiff_t count = SPECPDL_INDEX ();
10258 specbind (Qinhibit_read_only, Qt);
10259 /* Note that undo recording is always disabled. */
10260 del_range (BEG, Z);
10261 unbind_to (count, Qnil);
10262 }
10263 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10264
10265 /* Set up the buffer for the multibyteness we need. */
10266 if (multibyte_p
10267 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10268 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10269
10270 /* Raise the frame containing the echo area. */
10271 if (minibuffer_auto_raise)
10272 {
10273 struct frame *sf = SELECTED_FRAME ();
10274 Lisp_Object mini_window;
10275 mini_window = FRAME_MINIBUF_WINDOW (sf);
10276 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10277 }
10278
10279 message_log_maybe_newline ();
10280 message_buf_print = 1;
10281 }
10282 else
10283 {
10284 if (NILP (echo_area_buffer[0]))
10285 {
10286 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10287 echo_area_buffer[0] = echo_buffer[1];
10288 else
10289 echo_area_buffer[0] = echo_buffer[0];
10290 }
10291
10292 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10293 {
10294 /* Someone switched buffers between print requests. */
10295 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10296 bset_truncate_lines (current_buffer, Qnil);
10297 }
10298 }
10299 }
10300
10301
10302 /* Display an echo area message in window W. Value is non-zero if W's
10303 height is changed. If display_last_displayed_message_p is
10304 non-zero, display the message that was last displayed, otherwise
10305 display the current message. */
10306
10307 static int
10308 display_echo_area (struct window *w)
10309 {
10310 int i, no_message_p, window_height_changed_p;
10311
10312 /* Temporarily disable garbage collections while displaying the echo
10313 area. This is done because a GC can print a message itself.
10314 That message would modify the echo area buffer's contents while a
10315 redisplay of the buffer is going on, and seriously confuse
10316 redisplay. */
10317 ptrdiff_t count = inhibit_garbage_collection ();
10318
10319 /* If there is no message, we must call display_echo_area_1
10320 nevertheless because it resizes the window. But we will have to
10321 reset the echo_area_buffer in question to nil at the end because
10322 with_echo_area_buffer will sets it to an empty buffer. */
10323 i = display_last_displayed_message_p ? 1 : 0;
10324 no_message_p = NILP (echo_area_buffer[i]);
10325
10326 window_height_changed_p
10327 = with_echo_area_buffer (w, display_last_displayed_message_p,
10328 display_echo_area_1,
10329 (intptr_t) w, Qnil);
10330
10331 if (no_message_p)
10332 echo_area_buffer[i] = Qnil;
10333
10334 unbind_to (count, Qnil);
10335 return window_height_changed_p;
10336 }
10337
10338
10339 /* Helper for display_echo_area. Display the current buffer which
10340 contains the current echo area message in window W, a mini-window,
10341 a pointer to which is passed in A1. A2..A4 are currently not used.
10342 Change the height of W so that all of the message is displayed.
10343 Value is non-zero if height of W was changed. */
10344
10345 static int
10346 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10347 {
10348 intptr_t i1 = a1;
10349 struct window *w = (struct window *) i1;
10350 Lisp_Object window;
10351 struct text_pos start;
10352 int window_height_changed_p = 0;
10353
10354 /* Do this before displaying, so that we have a large enough glyph
10355 matrix for the display. If we can't get enough space for the
10356 whole text, display the last N lines. That works by setting w->start. */
10357 window_height_changed_p = resize_mini_window (w, 0);
10358
10359 /* Use the starting position chosen by resize_mini_window. */
10360 SET_TEXT_POS_FROM_MARKER (start, w->start);
10361
10362 /* Display. */
10363 clear_glyph_matrix (w->desired_matrix);
10364 XSETWINDOW (window, w);
10365 try_window (window, start, 0);
10366
10367 return window_height_changed_p;
10368 }
10369
10370
10371 /* Resize the echo area window to exactly the size needed for the
10372 currently displayed message, if there is one. If a mini-buffer
10373 is active, don't shrink it. */
10374
10375 void
10376 resize_echo_area_exactly (void)
10377 {
10378 if (BUFFERP (echo_area_buffer[0])
10379 && WINDOWP (echo_area_window))
10380 {
10381 struct window *w = XWINDOW (echo_area_window);
10382 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10383 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10384 (intptr_t) w, resize_exactly);
10385 if (resized_p)
10386 {
10387 windows_or_buffers_changed = 42;
10388 update_mode_lines = 30;
10389 redisplay_internal ();
10390 }
10391 }
10392 }
10393
10394
10395 /* Callback function for with_echo_area_buffer, when used from
10396 resize_echo_area_exactly. A1 contains a pointer to the window to
10397 resize, EXACTLY non-nil means resize the mini-window exactly to the
10398 size of the text displayed. A3 and A4 are not used. Value is what
10399 resize_mini_window returns. */
10400
10401 static int
10402 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10403 {
10404 intptr_t i1 = a1;
10405 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10406 }
10407
10408
10409 /* Resize mini-window W to fit the size of its contents. EXACT_P
10410 means size the window exactly to the size needed. Otherwise, it's
10411 only enlarged until W's buffer is empty.
10412
10413 Set W->start to the right place to begin display. If the whole
10414 contents fit, start at the beginning. Otherwise, start so as
10415 to make the end of the contents appear. This is particularly
10416 important for y-or-n-p, but seems desirable generally.
10417
10418 Value is non-zero if the window height has been changed. */
10419
10420 int
10421 resize_mini_window (struct window *w, int exact_p)
10422 {
10423 struct frame *f = XFRAME (w->frame);
10424 int window_height_changed_p = 0;
10425
10426 eassert (MINI_WINDOW_P (w));
10427
10428 /* By default, start display at the beginning. */
10429 set_marker_both (w->start, w->contents,
10430 BUF_BEGV (XBUFFER (w->contents)),
10431 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10432
10433 /* Don't resize windows while redisplaying a window; it would
10434 confuse redisplay functions when the size of the window they are
10435 displaying changes from under them. Such a resizing can happen,
10436 for instance, when which-func prints a long message while
10437 we are running fontification-functions. We're running these
10438 functions with safe_call which binds inhibit-redisplay to t. */
10439 if (!NILP (Vinhibit_redisplay))
10440 return 0;
10441
10442 /* Nil means don't try to resize. */
10443 if (NILP (Vresize_mini_windows)
10444 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10445 return 0;
10446
10447 if (!FRAME_MINIBUF_ONLY_P (f))
10448 {
10449 struct it it;
10450 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10451 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10452 int height;
10453 EMACS_INT max_height;
10454 int unit = FRAME_LINE_HEIGHT (f);
10455 struct text_pos start;
10456 struct buffer *old_current_buffer = NULL;
10457
10458 if (current_buffer != XBUFFER (w->contents))
10459 {
10460 old_current_buffer = current_buffer;
10461 set_buffer_internal (XBUFFER (w->contents));
10462 }
10463
10464 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10465
10466 /* Compute the max. number of lines specified by the user. */
10467 if (FLOATP (Vmax_mini_window_height))
10468 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10469 else if (INTEGERP (Vmax_mini_window_height))
10470 max_height = XINT (Vmax_mini_window_height);
10471 else
10472 max_height = total_height / 4;
10473
10474 /* Correct that max. height if it's bogus. */
10475 max_height = clip_to_bounds (1, max_height, total_height);
10476
10477 /* Find out the height of the text in the window. */
10478 if (it.line_wrap == TRUNCATE)
10479 height = 1;
10480 else
10481 {
10482 last_height = 0;
10483 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10484 if (it.max_ascent == 0 && it.max_descent == 0)
10485 height = it.current_y + last_height;
10486 else
10487 height = it.current_y + it.max_ascent + it.max_descent;
10488 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10489 height = (height + unit - 1) / unit;
10490 }
10491
10492 /* Compute a suitable window start. */
10493 if (height > max_height)
10494 {
10495 height = max_height;
10496 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10497 move_it_vertically_backward (&it, (height - 1) * unit);
10498 start = it.current.pos;
10499 }
10500 else
10501 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10502 SET_MARKER_FROM_TEXT_POS (w->start, start);
10503
10504 if (EQ (Vresize_mini_windows, Qgrow_only))
10505 {
10506 /* Let it grow only, until we display an empty message, in which
10507 case the window shrinks again. */
10508 if (height > WINDOW_TOTAL_LINES (w))
10509 {
10510 int old_height = WINDOW_TOTAL_LINES (w);
10511
10512 FRAME_WINDOWS_FROZEN (f) = 1;
10513 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10514 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10515 }
10516 else if (height < WINDOW_TOTAL_LINES (w)
10517 && (exact_p || BEGV == ZV))
10518 {
10519 int old_height = WINDOW_TOTAL_LINES (w);
10520
10521 FRAME_WINDOWS_FROZEN (f) = 0;
10522 shrink_mini_window (w);
10523 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10524 }
10525 }
10526 else
10527 {
10528 /* Always resize to exact size needed. */
10529 if (height > WINDOW_TOTAL_LINES (w))
10530 {
10531 int old_height = WINDOW_TOTAL_LINES (w);
10532
10533 FRAME_WINDOWS_FROZEN (f) = 1;
10534 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10535 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10536 }
10537 else if (height < WINDOW_TOTAL_LINES (w))
10538 {
10539 int old_height = WINDOW_TOTAL_LINES (w);
10540
10541 FRAME_WINDOWS_FROZEN (f) = 0;
10542 shrink_mini_window (w);
10543
10544 if (height)
10545 {
10546 FRAME_WINDOWS_FROZEN (f) = 1;
10547 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10548 }
10549
10550 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10551 }
10552 }
10553
10554 if (old_current_buffer)
10555 set_buffer_internal (old_current_buffer);
10556 }
10557
10558 return window_height_changed_p;
10559 }
10560
10561
10562 /* Value is the current message, a string, or nil if there is no
10563 current message. */
10564
10565 Lisp_Object
10566 current_message (void)
10567 {
10568 Lisp_Object msg;
10569
10570 if (!BUFFERP (echo_area_buffer[0]))
10571 msg = Qnil;
10572 else
10573 {
10574 with_echo_area_buffer (0, 0, current_message_1,
10575 (intptr_t) &msg, Qnil);
10576 if (NILP (msg))
10577 echo_area_buffer[0] = Qnil;
10578 }
10579
10580 return msg;
10581 }
10582
10583
10584 static int
10585 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10586 {
10587 intptr_t i1 = a1;
10588 Lisp_Object *msg = (Lisp_Object *) i1;
10589
10590 if (Z > BEG)
10591 *msg = make_buffer_string (BEG, Z, 1);
10592 else
10593 *msg = Qnil;
10594 return 0;
10595 }
10596
10597
10598 /* Push the current message on Vmessage_stack for later restoration
10599 by restore_message. Value is non-zero if the current message isn't
10600 empty. This is a relatively infrequent operation, so it's not
10601 worth optimizing. */
10602
10603 bool
10604 push_message (void)
10605 {
10606 Lisp_Object msg = current_message ();
10607 Vmessage_stack = Fcons (msg, Vmessage_stack);
10608 return STRINGP (msg);
10609 }
10610
10611
10612 /* Restore message display from the top of Vmessage_stack. */
10613
10614 void
10615 restore_message (void)
10616 {
10617 eassert (CONSP (Vmessage_stack));
10618 message3_nolog (XCAR (Vmessage_stack));
10619 }
10620
10621
10622 /* Handler for unwind-protect calling pop_message. */
10623
10624 void
10625 pop_message_unwind (void)
10626 {
10627 /* Pop the top-most entry off Vmessage_stack. */
10628 eassert (CONSP (Vmessage_stack));
10629 Vmessage_stack = XCDR (Vmessage_stack);
10630 }
10631
10632
10633 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10634 exits. If the stack is not empty, we have a missing pop_message
10635 somewhere. */
10636
10637 void
10638 check_message_stack (void)
10639 {
10640 if (!NILP (Vmessage_stack))
10641 emacs_abort ();
10642 }
10643
10644
10645 /* Truncate to NCHARS what will be displayed in the echo area the next
10646 time we display it---but don't redisplay it now. */
10647
10648 void
10649 truncate_echo_area (ptrdiff_t nchars)
10650 {
10651 if (nchars == 0)
10652 echo_area_buffer[0] = Qnil;
10653 else if (!noninteractive
10654 && INTERACTIVE
10655 && !NILP (echo_area_buffer[0]))
10656 {
10657 struct frame *sf = SELECTED_FRAME ();
10658 /* Error messages get reported properly by cmd_error, so this must be
10659 just an informative message; if the frame hasn't really been
10660 initialized yet, just toss it. */
10661 if (sf->glyphs_initialized_p)
10662 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10663 }
10664 }
10665
10666
10667 /* Helper function for truncate_echo_area. Truncate the current
10668 message to at most NCHARS characters. */
10669
10670 static int
10671 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10672 {
10673 if (BEG + nchars < Z)
10674 del_range (BEG + nchars, Z);
10675 if (Z == BEG)
10676 echo_area_buffer[0] = Qnil;
10677 return 0;
10678 }
10679
10680 /* Set the current message to STRING. */
10681
10682 static void
10683 set_message (Lisp_Object string)
10684 {
10685 eassert (STRINGP (string));
10686
10687 message_enable_multibyte = STRING_MULTIBYTE (string);
10688
10689 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10690 message_buf_print = 0;
10691 help_echo_showing_p = 0;
10692
10693 if (STRINGP (Vdebug_on_message)
10694 && STRINGP (string)
10695 && fast_string_match (Vdebug_on_message, string) >= 0)
10696 call_debugger (list2 (Qerror, string));
10697 }
10698
10699
10700 /* Helper function for set_message. First argument is ignored and second
10701 argument has the same meaning as for set_message.
10702 This function is called with the echo area buffer being current. */
10703
10704 static int
10705 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10706 {
10707 eassert (STRINGP (string));
10708
10709 /* Change multibyteness of the echo buffer appropriately. */
10710 if (message_enable_multibyte
10711 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10712 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10713
10714 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10715 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10716 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10717
10718 /* Insert new message at BEG. */
10719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10720
10721 /* This function takes care of single/multibyte conversion.
10722 We just have to ensure that the echo area buffer has the right
10723 setting of enable_multibyte_characters. */
10724 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10725
10726 return 0;
10727 }
10728
10729
10730 /* Clear messages. CURRENT_P non-zero means clear the current
10731 message. LAST_DISPLAYED_P non-zero means clear the message
10732 last displayed. */
10733
10734 void
10735 clear_message (bool current_p, bool last_displayed_p)
10736 {
10737 if (current_p)
10738 {
10739 echo_area_buffer[0] = Qnil;
10740 message_cleared_p = true;
10741 }
10742
10743 if (last_displayed_p)
10744 echo_area_buffer[1] = Qnil;
10745
10746 message_buf_print = 0;
10747 }
10748
10749 /* Clear garbaged frames.
10750
10751 This function is used where the old redisplay called
10752 redraw_garbaged_frames which in turn called redraw_frame which in
10753 turn called clear_frame. The call to clear_frame was a source of
10754 flickering. I believe a clear_frame is not necessary. It should
10755 suffice in the new redisplay to invalidate all current matrices,
10756 and ensure a complete redisplay of all windows. */
10757
10758 static void
10759 clear_garbaged_frames (void)
10760 {
10761 if (frame_garbaged)
10762 {
10763 Lisp_Object tail, frame;
10764
10765 FOR_EACH_FRAME (tail, frame)
10766 {
10767 struct frame *f = XFRAME (frame);
10768
10769 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10770 {
10771 if (f->resized_p)
10772 redraw_frame (f);
10773 else
10774 clear_current_matrices (f);
10775 fset_redisplay (f);
10776 f->garbaged = false;
10777 f->resized_p = false;
10778 }
10779 }
10780
10781 frame_garbaged = false;
10782 }
10783 }
10784
10785
10786 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10787 is non-zero update selected_frame. Value is non-zero if the
10788 mini-windows height has been changed. */
10789
10790 static int
10791 echo_area_display (int update_frame_p)
10792 {
10793 Lisp_Object mini_window;
10794 struct window *w;
10795 struct frame *f;
10796 int window_height_changed_p = 0;
10797 struct frame *sf = SELECTED_FRAME ();
10798
10799 mini_window = FRAME_MINIBUF_WINDOW (sf);
10800 w = XWINDOW (mini_window);
10801 f = XFRAME (WINDOW_FRAME (w));
10802
10803 /* Don't display if frame is invisible or not yet initialized. */
10804 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10805 return 0;
10806
10807 #ifdef HAVE_WINDOW_SYSTEM
10808 /* When Emacs starts, selected_frame may be the initial terminal
10809 frame. If we let this through, a message would be displayed on
10810 the terminal. */
10811 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10812 return 0;
10813 #endif /* HAVE_WINDOW_SYSTEM */
10814
10815 /* Redraw garbaged frames. */
10816 clear_garbaged_frames ();
10817
10818 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10819 {
10820 echo_area_window = mini_window;
10821 window_height_changed_p = display_echo_area (w);
10822 w->must_be_updated_p = true;
10823
10824 /* Update the display, unless called from redisplay_internal.
10825 Also don't update the screen during redisplay itself. The
10826 update will happen at the end of redisplay, and an update
10827 here could cause confusion. */
10828 if (update_frame_p && !redisplaying_p)
10829 {
10830 int n = 0;
10831
10832 /* If the display update has been interrupted by pending
10833 input, update mode lines in the frame. Due to the
10834 pending input, it might have been that redisplay hasn't
10835 been called, so that mode lines above the echo area are
10836 garbaged. This looks odd, so we prevent it here. */
10837 if (!display_completed)
10838 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
10839
10840 if (window_height_changed_p
10841 /* Don't do this if Emacs is shutting down. Redisplay
10842 needs to run hooks. */
10843 && !NILP (Vrun_hooks))
10844 {
10845 /* Must update other windows. Likewise as in other
10846 cases, don't let this update be interrupted by
10847 pending input. */
10848 ptrdiff_t count = SPECPDL_INDEX ();
10849 specbind (Qredisplay_dont_pause, Qt);
10850 windows_or_buffers_changed = 44;
10851 redisplay_internal ();
10852 unbind_to (count, Qnil);
10853 }
10854 else if (FRAME_WINDOW_P (f) && n == 0)
10855 {
10856 /* Window configuration is the same as before.
10857 Can do with a display update of the echo area,
10858 unless we displayed some mode lines. */
10859 update_single_window (w, 1);
10860 flush_frame (f);
10861 }
10862 else
10863 update_frame (f, 1, 1);
10864
10865 /* If cursor is in the echo area, make sure that the next
10866 redisplay displays the minibuffer, so that the cursor will
10867 be replaced with what the minibuffer wants. */
10868 if (cursor_in_echo_area)
10869 wset_redisplay (XWINDOW (mini_window));
10870 }
10871 }
10872 else if (!EQ (mini_window, selected_window))
10873 wset_redisplay (XWINDOW (mini_window));
10874
10875 /* Last displayed message is now the current message. */
10876 echo_area_buffer[1] = echo_area_buffer[0];
10877 /* Inform read_char that we're not echoing. */
10878 echo_message_buffer = Qnil;
10879
10880 /* Prevent redisplay optimization in redisplay_internal by resetting
10881 this_line_start_pos. This is done because the mini-buffer now
10882 displays the message instead of its buffer text. */
10883 if (EQ (mini_window, selected_window))
10884 CHARPOS (this_line_start_pos) = 0;
10885
10886 return window_height_changed_p;
10887 }
10888
10889 /* Nonzero if W's buffer was changed but not saved. */
10890
10891 static int
10892 window_buffer_changed (struct window *w)
10893 {
10894 struct buffer *b = XBUFFER (w->contents);
10895
10896 eassert (BUFFER_LIVE_P (b));
10897
10898 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
10899 }
10900
10901 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10902
10903 static int
10904 mode_line_update_needed (struct window *w)
10905 {
10906 return (w->column_number_displayed != -1
10907 && !(PT == w->last_point && !window_outdated (w))
10908 && (w->column_number_displayed != current_column ()));
10909 }
10910
10911 /* Nonzero if window start of W is frozen and may not be changed during
10912 redisplay. */
10913
10914 static bool
10915 window_frozen_p (struct window *w)
10916 {
10917 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10918 {
10919 Lisp_Object window;
10920
10921 XSETWINDOW (window, w);
10922 if (MINI_WINDOW_P (w))
10923 return 0;
10924 else if (EQ (window, selected_window))
10925 return 0;
10926 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10927 && EQ (window, Vminibuf_scroll_window))
10928 /* This special window can't be frozen too. */
10929 return 0;
10930 else
10931 return 1;
10932 }
10933 return 0;
10934 }
10935
10936 /***********************************************************************
10937 Mode Lines and Frame Titles
10938 ***********************************************************************/
10939
10940 /* A buffer for constructing non-propertized mode-line strings and
10941 frame titles in it; allocated from the heap in init_xdisp and
10942 resized as needed in store_mode_line_noprop_char. */
10943
10944 static char *mode_line_noprop_buf;
10945
10946 /* The buffer's end, and a current output position in it. */
10947
10948 static char *mode_line_noprop_buf_end;
10949 static char *mode_line_noprop_ptr;
10950
10951 #define MODE_LINE_NOPROP_LEN(start) \
10952 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10953
10954 static enum {
10955 MODE_LINE_DISPLAY = 0,
10956 MODE_LINE_TITLE,
10957 MODE_LINE_NOPROP,
10958 MODE_LINE_STRING
10959 } mode_line_target;
10960
10961 /* Alist that caches the results of :propertize.
10962 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10963 static Lisp_Object mode_line_proptrans_alist;
10964
10965 /* List of strings making up the mode-line. */
10966 static Lisp_Object mode_line_string_list;
10967
10968 /* Base face property when building propertized mode line string. */
10969 static Lisp_Object mode_line_string_face;
10970 static Lisp_Object mode_line_string_face_prop;
10971
10972
10973 /* Unwind data for mode line strings */
10974
10975 static Lisp_Object Vmode_line_unwind_vector;
10976
10977 static Lisp_Object
10978 format_mode_line_unwind_data (struct frame *target_frame,
10979 struct buffer *obuf,
10980 Lisp_Object owin,
10981 int save_proptrans)
10982 {
10983 Lisp_Object vector, tmp;
10984
10985 /* Reduce consing by keeping one vector in
10986 Vwith_echo_area_save_vector. */
10987 vector = Vmode_line_unwind_vector;
10988 Vmode_line_unwind_vector = Qnil;
10989
10990 if (NILP (vector))
10991 vector = Fmake_vector (make_number (10), Qnil);
10992
10993 ASET (vector, 0, make_number (mode_line_target));
10994 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10995 ASET (vector, 2, mode_line_string_list);
10996 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10997 ASET (vector, 4, mode_line_string_face);
10998 ASET (vector, 5, mode_line_string_face_prop);
10999
11000 if (obuf)
11001 XSETBUFFER (tmp, obuf);
11002 else
11003 tmp = Qnil;
11004 ASET (vector, 6, tmp);
11005 ASET (vector, 7, owin);
11006 if (target_frame)
11007 {
11008 /* Similarly to `with-selected-window', if the operation selects
11009 a window on another frame, we must restore that frame's
11010 selected window, and (for a tty) the top-frame. */
11011 ASET (vector, 8, target_frame->selected_window);
11012 if (FRAME_TERMCAP_P (target_frame))
11013 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11014 }
11015
11016 return vector;
11017 }
11018
11019 static void
11020 unwind_format_mode_line (Lisp_Object vector)
11021 {
11022 Lisp_Object old_window = AREF (vector, 7);
11023 Lisp_Object target_frame_window = AREF (vector, 8);
11024 Lisp_Object old_top_frame = AREF (vector, 9);
11025
11026 mode_line_target = XINT (AREF (vector, 0));
11027 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11028 mode_line_string_list = AREF (vector, 2);
11029 if (! EQ (AREF (vector, 3), Qt))
11030 mode_line_proptrans_alist = AREF (vector, 3);
11031 mode_line_string_face = AREF (vector, 4);
11032 mode_line_string_face_prop = AREF (vector, 5);
11033
11034 /* Select window before buffer, since it may change the buffer. */
11035 if (!NILP (old_window))
11036 {
11037 /* If the operation that we are unwinding had selected a window
11038 on a different frame, reset its frame-selected-window. For a
11039 text terminal, reset its top-frame if necessary. */
11040 if (!NILP (target_frame_window))
11041 {
11042 Lisp_Object frame
11043 = WINDOW_FRAME (XWINDOW (target_frame_window));
11044
11045 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11046 Fselect_window (target_frame_window, Qt);
11047
11048 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11049 Fselect_frame (old_top_frame, Qt);
11050 }
11051
11052 Fselect_window (old_window, Qt);
11053 }
11054
11055 if (!NILP (AREF (vector, 6)))
11056 {
11057 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11058 ASET (vector, 6, Qnil);
11059 }
11060
11061 Vmode_line_unwind_vector = vector;
11062 }
11063
11064
11065 /* Store a single character C for the frame title in mode_line_noprop_buf.
11066 Re-allocate mode_line_noprop_buf if necessary. */
11067
11068 static void
11069 store_mode_line_noprop_char (char c)
11070 {
11071 /* If output position has reached the end of the allocated buffer,
11072 increase the buffer's size. */
11073 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11074 {
11075 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11076 ptrdiff_t size = len;
11077 mode_line_noprop_buf =
11078 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11079 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11080 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11081 }
11082
11083 *mode_line_noprop_ptr++ = c;
11084 }
11085
11086
11087 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11088 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11089 characters that yield more columns than PRECISION; PRECISION <= 0
11090 means copy the whole string. Pad with spaces until FIELD_WIDTH
11091 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11092 pad. Called from display_mode_element when it is used to build a
11093 frame title. */
11094
11095 static int
11096 store_mode_line_noprop (const char *string, int field_width, int precision)
11097 {
11098 const unsigned char *str = (const unsigned char *) string;
11099 int n = 0;
11100 ptrdiff_t dummy, nbytes;
11101
11102 /* Copy at most PRECISION chars from STR. */
11103 nbytes = strlen (string);
11104 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11105 while (nbytes--)
11106 store_mode_line_noprop_char (*str++);
11107
11108 /* Fill up with spaces until FIELD_WIDTH reached. */
11109 while (field_width > 0
11110 && n < field_width)
11111 {
11112 store_mode_line_noprop_char (' ');
11113 ++n;
11114 }
11115
11116 return n;
11117 }
11118
11119 /***********************************************************************
11120 Frame Titles
11121 ***********************************************************************/
11122
11123 #ifdef HAVE_WINDOW_SYSTEM
11124
11125 /* Set the title of FRAME, if it has changed. The title format is
11126 Vicon_title_format if FRAME is iconified, otherwise it is
11127 frame_title_format. */
11128
11129 static void
11130 x_consider_frame_title (Lisp_Object frame)
11131 {
11132 struct frame *f = XFRAME (frame);
11133
11134 if (FRAME_WINDOW_P (f)
11135 || FRAME_MINIBUF_ONLY_P (f)
11136 || f->explicit_name)
11137 {
11138 /* Do we have more than one visible frame on this X display? */
11139 Lisp_Object tail, other_frame, fmt;
11140 ptrdiff_t title_start;
11141 char *title;
11142 ptrdiff_t len;
11143 struct it it;
11144 ptrdiff_t count = SPECPDL_INDEX ();
11145
11146 FOR_EACH_FRAME (tail, other_frame)
11147 {
11148 struct frame *tf = XFRAME (other_frame);
11149
11150 if (tf != f
11151 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11152 && !FRAME_MINIBUF_ONLY_P (tf)
11153 && !EQ (other_frame, tip_frame)
11154 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11155 break;
11156 }
11157
11158 /* Set global variable indicating that multiple frames exist. */
11159 multiple_frames = CONSP (tail);
11160
11161 /* Switch to the buffer of selected window of the frame. Set up
11162 mode_line_target so that display_mode_element will output into
11163 mode_line_noprop_buf; then display the title. */
11164 record_unwind_protect (unwind_format_mode_line,
11165 format_mode_line_unwind_data
11166 (f, current_buffer, selected_window, 0));
11167
11168 Fselect_window (f->selected_window, Qt);
11169 set_buffer_internal_1
11170 (XBUFFER (XWINDOW (f->selected_window)->contents));
11171 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11172
11173 mode_line_target = MODE_LINE_TITLE;
11174 title_start = MODE_LINE_NOPROP_LEN (0);
11175 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11176 NULL, DEFAULT_FACE_ID);
11177 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11178 len = MODE_LINE_NOPROP_LEN (title_start);
11179 title = mode_line_noprop_buf + title_start;
11180 unbind_to (count, Qnil);
11181
11182 /* Set the title only if it's changed. This avoids consing in
11183 the common case where it hasn't. (If it turns out that we've
11184 already wasted too much time by walking through the list with
11185 display_mode_element, then we might need to optimize at a
11186 higher level than this.) */
11187 if (! STRINGP (f->name)
11188 || SBYTES (f->name) != len
11189 || memcmp (title, SDATA (f->name), len) != 0)
11190 x_implicitly_set_name (f, make_string (title, len), Qnil);
11191 }
11192 }
11193
11194 #endif /* not HAVE_WINDOW_SYSTEM */
11195
11196 \f
11197 /***********************************************************************
11198 Menu Bars
11199 ***********************************************************************/
11200
11201 /* Non-zero if we will not redisplay all visible windows. */
11202 #define REDISPLAY_SOME_P() \
11203 ((windows_or_buffers_changed == 0 \
11204 || windows_or_buffers_changed == REDISPLAY_SOME) \
11205 && (update_mode_lines == 0 \
11206 || update_mode_lines == REDISPLAY_SOME))
11207
11208 /* Prepare for redisplay by updating menu-bar item lists when
11209 appropriate. This can call eval. */
11210
11211 static void
11212 prepare_menu_bars (void)
11213 {
11214 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11215 bool some_windows = REDISPLAY_SOME_P ();
11216 struct gcpro gcpro1, gcpro2;
11217 Lisp_Object tooltip_frame;
11218
11219 #ifdef HAVE_WINDOW_SYSTEM
11220 tooltip_frame = tip_frame;
11221 #else
11222 tooltip_frame = Qnil;
11223 #endif
11224
11225 if (FUNCTIONP (Vpre_redisplay_function))
11226 {
11227 Lisp_Object windows = all_windows ? Qt : Qnil;
11228 if (all_windows && some_windows)
11229 {
11230 Lisp_Object ws = window_list ();
11231 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11232 {
11233 Lisp_Object this = XCAR (ws);
11234 struct window *w = XWINDOW (this);
11235 if (w->redisplay
11236 || XFRAME (w->frame)->redisplay
11237 || XBUFFER (w->contents)->text->redisplay)
11238 {
11239 windows = Fcons (this, windows);
11240 }
11241 }
11242 }
11243 safe_call1 (Vpre_redisplay_function, windows);
11244 }
11245
11246 /* Update all frame titles based on their buffer names, etc. We do
11247 this before the menu bars so that the buffer-menu will show the
11248 up-to-date frame titles. */
11249 #ifdef HAVE_WINDOW_SYSTEM
11250 if (all_windows)
11251 {
11252 Lisp_Object tail, frame;
11253
11254 FOR_EACH_FRAME (tail, frame)
11255 {
11256 struct frame *f = XFRAME (frame);
11257 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11258 if (some_windows
11259 && !f->redisplay
11260 && !w->redisplay
11261 && !XBUFFER (w->contents)->text->redisplay)
11262 continue;
11263
11264 if (!EQ (frame, tooltip_frame)
11265 && (FRAME_ICONIFIED_P (f)
11266 || FRAME_VISIBLE_P (f) == 1
11267 /* Exclude TTY frames that are obscured because they
11268 are not the top frame on their console. This is
11269 because x_consider_frame_title actually switches
11270 to the frame, which for TTY frames means it is
11271 marked as garbaged, and will be completely
11272 redrawn on the next redisplay cycle. This causes
11273 TTY frames to be completely redrawn, when there
11274 are more than one of them, even though nothing
11275 should be changed on display. */
11276 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11277 x_consider_frame_title (frame);
11278 }
11279 }
11280 #endif /* HAVE_WINDOW_SYSTEM */
11281
11282 /* Update the menu bar item lists, if appropriate. This has to be
11283 done before any actual redisplay or generation of display lines. */
11284
11285 if (all_windows)
11286 {
11287 Lisp_Object tail, frame;
11288 ptrdiff_t count = SPECPDL_INDEX ();
11289 /* 1 means that update_menu_bar has run its hooks
11290 so any further calls to update_menu_bar shouldn't do so again. */
11291 int menu_bar_hooks_run = 0;
11292
11293 record_unwind_save_match_data ();
11294
11295 FOR_EACH_FRAME (tail, frame)
11296 {
11297 struct frame *f = XFRAME (frame);
11298 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11299
11300 /* Ignore tooltip frame. */
11301 if (EQ (frame, tooltip_frame))
11302 continue;
11303
11304 if (some_windows
11305 && !f->redisplay
11306 && !w->redisplay
11307 && !XBUFFER (w->contents)->text->redisplay)
11308 continue;
11309
11310 /* If a window on this frame changed size, report that to
11311 the user and clear the size-change flag. */
11312 if (FRAME_WINDOW_SIZES_CHANGED (f))
11313 {
11314 Lisp_Object functions;
11315
11316 /* Clear flag first in case we get an error below. */
11317 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11318 functions = Vwindow_size_change_functions;
11319 GCPRO2 (tail, functions);
11320
11321 while (CONSP (functions))
11322 {
11323 if (!EQ (XCAR (functions), Qt))
11324 call1 (XCAR (functions), frame);
11325 functions = XCDR (functions);
11326 }
11327 UNGCPRO;
11328 }
11329
11330 GCPRO1 (tail);
11331 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11332 #ifdef HAVE_WINDOW_SYSTEM
11333 update_tool_bar (f, 0);
11334 #endif
11335 #ifdef HAVE_NS
11336 if (windows_or_buffers_changed
11337 && FRAME_NS_P (f))
11338 ns_set_doc_edited
11339 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11340 #endif
11341 UNGCPRO;
11342 }
11343
11344 unbind_to (count, Qnil);
11345 }
11346 else
11347 {
11348 struct frame *sf = SELECTED_FRAME ();
11349 update_menu_bar (sf, 1, 0);
11350 #ifdef HAVE_WINDOW_SYSTEM
11351 update_tool_bar (sf, 1);
11352 #endif
11353 }
11354 }
11355
11356
11357 /* Update the menu bar item list for frame F. This has to be done
11358 before we start to fill in any display lines, because it can call
11359 eval.
11360
11361 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11362
11363 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11364 already ran the menu bar hooks for this redisplay, so there
11365 is no need to run them again. The return value is the
11366 updated value of this flag, to pass to the next call. */
11367
11368 static int
11369 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11370 {
11371 Lisp_Object window;
11372 register struct window *w;
11373
11374 /* If called recursively during a menu update, do nothing. This can
11375 happen when, for instance, an activate-menubar-hook causes a
11376 redisplay. */
11377 if (inhibit_menubar_update)
11378 return hooks_run;
11379
11380 window = FRAME_SELECTED_WINDOW (f);
11381 w = XWINDOW (window);
11382
11383 if (FRAME_WINDOW_P (f)
11384 ?
11385 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11386 || defined (HAVE_NS) || defined (USE_GTK)
11387 FRAME_EXTERNAL_MENU_BAR (f)
11388 #else
11389 FRAME_MENU_BAR_LINES (f) > 0
11390 #endif
11391 : FRAME_MENU_BAR_LINES (f) > 0)
11392 {
11393 /* If the user has switched buffers or windows, we need to
11394 recompute to reflect the new bindings. But we'll
11395 recompute when update_mode_lines is set too; that means
11396 that people can use force-mode-line-update to request
11397 that the menu bar be recomputed. The adverse effect on
11398 the rest of the redisplay algorithm is about the same as
11399 windows_or_buffers_changed anyway. */
11400 if (windows_or_buffers_changed
11401 /* This used to test w->update_mode_line, but we believe
11402 there is no need to recompute the menu in that case. */
11403 || update_mode_lines
11404 || window_buffer_changed (w))
11405 {
11406 struct buffer *prev = current_buffer;
11407 ptrdiff_t count = SPECPDL_INDEX ();
11408
11409 specbind (Qinhibit_menubar_update, Qt);
11410
11411 set_buffer_internal_1 (XBUFFER (w->contents));
11412 if (save_match_data)
11413 record_unwind_save_match_data ();
11414 if (NILP (Voverriding_local_map_menu_flag))
11415 {
11416 specbind (Qoverriding_terminal_local_map, Qnil);
11417 specbind (Qoverriding_local_map, Qnil);
11418 }
11419
11420 if (!hooks_run)
11421 {
11422 /* Run the Lucid hook. */
11423 safe_run_hooks (Qactivate_menubar_hook);
11424
11425 /* If it has changed current-menubar from previous value,
11426 really recompute the menu-bar from the value. */
11427 if (! NILP (Vlucid_menu_bar_dirty_flag))
11428 call0 (Qrecompute_lucid_menubar);
11429
11430 safe_run_hooks (Qmenu_bar_update_hook);
11431
11432 hooks_run = 1;
11433 }
11434
11435 XSETFRAME (Vmenu_updating_frame, f);
11436 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11437
11438 /* Redisplay the menu bar in case we changed it. */
11439 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11440 || defined (HAVE_NS) || defined (USE_GTK)
11441 if (FRAME_WINDOW_P (f))
11442 {
11443 #if defined (HAVE_NS)
11444 /* All frames on Mac OS share the same menubar. So only
11445 the selected frame should be allowed to set it. */
11446 if (f == SELECTED_FRAME ())
11447 #endif
11448 set_frame_menubar (f, 0, 0);
11449 }
11450 else
11451 /* On a terminal screen, the menu bar is an ordinary screen
11452 line, and this makes it get updated. */
11453 w->update_mode_line = 1;
11454 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11455 /* In the non-toolkit version, the menu bar is an ordinary screen
11456 line, and this makes it get updated. */
11457 w->update_mode_line = 1;
11458 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11459
11460 unbind_to (count, Qnil);
11461 set_buffer_internal_1 (prev);
11462 }
11463 }
11464
11465 return hooks_run;
11466 }
11467
11468 /***********************************************************************
11469 Tool-bars
11470 ***********************************************************************/
11471
11472 #ifdef HAVE_WINDOW_SYSTEM
11473
11474 /* Tool-bar item index of the item on which a mouse button was pressed
11475 or -1. */
11476
11477 int last_tool_bar_item;
11478
11479 /* Select `frame' temporarily without running all the code in
11480 do_switch_frame.
11481 FIXME: Maybe do_switch_frame should be trimmed down similarly
11482 when `norecord' is set. */
11483 static void
11484 fast_set_selected_frame (Lisp_Object frame)
11485 {
11486 if (!EQ (selected_frame, frame))
11487 {
11488 selected_frame = frame;
11489 selected_window = XFRAME (frame)->selected_window;
11490 }
11491 }
11492
11493 /* Update the tool-bar item list for frame F. This has to be done
11494 before we start to fill in any display lines. Called from
11495 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11496 and restore it here. */
11497
11498 static void
11499 update_tool_bar (struct frame *f, int save_match_data)
11500 {
11501 #if defined (USE_GTK) || defined (HAVE_NS)
11502 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11503 #else
11504 int do_update = WINDOWP (f->tool_bar_window)
11505 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11506 #endif
11507
11508 if (do_update)
11509 {
11510 Lisp_Object window;
11511 struct window *w;
11512
11513 window = FRAME_SELECTED_WINDOW (f);
11514 w = XWINDOW (window);
11515
11516 /* If the user has switched buffers or windows, we need to
11517 recompute to reflect the new bindings. But we'll
11518 recompute when update_mode_lines is set too; that means
11519 that people can use force-mode-line-update to request
11520 that the menu bar be recomputed. The adverse effect on
11521 the rest of the redisplay algorithm is about the same as
11522 windows_or_buffers_changed anyway. */
11523 if (windows_or_buffers_changed
11524 || w->update_mode_line
11525 || update_mode_lines
11526 || window_buffer_changed (w))
11527 {
11528 struct buffer *prev = current_buffer;
11529 ptrdiff_t count = SPECPDL_INDEX ();
11530 Lisp_Object frame, new_tool_bar;
11531 int new_n_tool_bar;
11532 struct gcpro gcpro1;
11533
11534 /* Set current_buffer to the buffer of the selected
11535 window of the frame, so that we get the right local
11536 keymaps. */
11537 set_buffer_internal_1 (XBUFFER (w->contents));
11538
11539 /* Save match data, if we must. */
11540 if (save_match_data)
11541 record_unwind_save_match_data ();
11542
11543 /* Make sure that we don't accidentally use bogus keymaps. */
11544 if (NILP (Voverriding_local_map_menu_flag))
11545 {
11546 specbind (Qoverriding_terminal_local_map, Qnil);
11547 specbind (Qoverriding_local_map, Qnil);
11548 }
11549
11550 GCPRO1 (new_tool_bar);
11551
11552 /* We must temporarily set the selected frame to this frame
11553 before calling tool_bar_items, because the calculation of
11554 the tool-bar keymap uses the selected frame (see
11555 `tool-bar-make-keymap' in tool-bar.el). */
11556 eassert (EQ (selected_window,
11557 /* Since we only explicitly preserve selected_frame,
11558 check that selected_window would be redundant. */
11559 XFRAME (selected_frame)->selected_window));
11560 record_unwind_protect (fast_set_selected_frame, selected_frame);
11561 XSETFRAME (frame, f);
11562 fast_set_selected_frame (frame);
11563
11564 /* Build desired tool-bar items from keymaps. */
11565 new_tool_bar
11566 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11567 &new_n_tool_bar);
11568
11569 /* Redisplay the tool-bar if we changed it. */
11570 if (new_n_tool_bar != f->n_tool_bar_items
11571 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11572 {
11573 /* Redisplay that happens asynchronously due to an expose event
11574 may access f->tool_bar_items. Make sure we update both
11575 variables within BLOCK_INPUT so no such event interrupts. */
11576 block_input ();
11577 fset_tool_bar_items (f, new_tool_bar);
11578 f->n_tool_bar_items = new_n_tool_bar;
11579 w->update_mode_line = 1;
11580 unblock_input ();
11581 }
11582
11583 UNGCPRO;
11584
11585 unbind_to (count, Qnil);
11586 set_buffer_internal_1 (prev);
11587 }
11588 }
11589 }
11590
11591 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11592
11593 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11594 F's desired tool-bar contents. F->tool_bar_items must have
11595 been set up previously by calling prepare_menu_bars. */
11596
11597 static void
11598 build_desired_tool_bar_string (struct frame *f)
11599 {
11600 int i, size, size_needed;
11601 struct gcpro gcpro1, gcpro2, gcpro3;
11602 Lisp_Object image, plist, props;
11603
11604 image = plist = props = Qnil;
11605 GCPRO3 (image, plist, props);
11606
11607 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11608 Otherwise, make a new string. */
11609
11610 /* The size of the string we might be able to reuse. */
11611 size = (STRINGP (f->desired_tool_bar_string)
11612 ? SCHARS (f->desired_tool_bar_string)
11613 : 0);
11614
11615 /* We need one space in the string for each image. */
11616 size_needed = f->n_tool_bar_items;
11617
11618 /* Reuse f->desired_tool_bar_string, if possible. */
11619 if (size < size_needed || NILP (f->desired_tool_bar_string))
11620 fset_desired_tool_bar_string
11621 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11622 else
11623 {
11624 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11625 Fremove_text_properties (make_number (0), make_number (size),
11626 props, f->desired_tool_bar_string);
11627 }
11628
11629 /* Put a `display' property on the string for the images to display,
11630 put a `menu_item' property on tool-bar items with a value that
11631 is the index of the item in F's tool-bar item vector. */
11632 for (i = 0; i < f->n_tool_bar_items; ++i)
11633 {
11634 #define PROP(IDX) \
11635 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11636
11637 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11638 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11639 int hmargin, vmargin, relief, idx, end;
11640
11641 /* If image is a vector, choose the image according to the
11642 button state. */
11643 image = PROP (TOOL_BAR_ITEM_IMAGES);
11644 if (VECTORP (image))
11645 {
11646 if (enabled_p)
11647 idx = (selected_p
11648 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11649 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11650 else
11651 idx = (selected_p
11652 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11653 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11654
11655 eassert (ASIZE (image) >= idx);
11656 image = AREF (image, idx);
11657 }
11658 else
11659 idx = -1;
11660
11661 /* Ignore invalid image specifications. */
11662 if (!valid_image_p (image))
11663 continue;
11664
11665 /* Display the tool-bar button pressed, or depressed. */
11666 plist = Fcopy_sequence (XCDR (image));
11667
11668 /* Compute margin and relief to draw. */
11669 relief = (tool_bar_button_relief >= 0
11670 ? tool_bar_button_relief
11671 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11672 hmargin = vmargin = relief;
11673
11674 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11675 INT_MAX - max (hmargin, vmargin)))
11676 {
11677 hmargin += XFASTINT (Vtool_bar_button_margin);
11678 vmargin += XFASTINT (Vtool_bar_button_margin);
11679 }
11680 else if (CONSP (Vtool_bar_button_margin))
11681 {
11682 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11683 INT_MAX - hmargin))
11684 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11685
11686 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11687 INT_MAX - vmargin))
11688 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11689 }
11690
11691 if (auto_raise_tool_bar_buttons_p)
11692 {
11693 /* Add a `:relief' property to the image spec if the item is
11694 selected. */
11695 if (selected_p)
11696 {
11697 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11698 hmargin -= relief;
11699 vmargin -= relief;
11700 }
11701 }
11702 else
11703 {
11704 /* If image is selected, display it pressed, i.e. with a
11705 negative relief. If it's not selected, display it with a
11706 raised relief. */
11707 plist = Fplist_put (plist, QCrelief,
11708 (selected_p
11709 ? make_number (-relief)
11710 : make_number (relief)));
11711 hmargin -= relief;
11712 vmargin -= relief;
11713 }
11714
11715 /* Put a margin around the image. */
11716 if (hmargin || vmargin)
11717 {
11718 if (hmargin == vmargin)
11719 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11720 else
11721 plist = Fplist_put (plist, QCmargin,
11722 Fcons (make_number (hmargin),
11723 make_number (vmargin)));
11724 }
11725
11726 /* If button is not enabled, and we don't have special images
11727 for the disabled state, make the image appear disabled by
11728 applying an appropriate algorithm to it. */
11729 if (!enabled_p && idx < 0)
11730 plist = Fplist_put (plist, QCconversion, Qdisabled);
11731
11732 /* Put a `display' text property on the string for the image to
11733 display. Put a `menu-item' property on the string that gives
11734 the start of this item's properties in the tool-bar items
11735 vector. */
11736 image = Fcons (Qimage, plist);
11737 props = list4 (Qdisplay, image,
11738 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11739
11740 /* Let the last image hide all remaining spaces in the tool bar
11741 string. The string can be longer than needed when we reuse a
11742 previous string. */
11743 if (i + 1 == f->n_tool_bar_items)
11744 end = SCHARS (f->desired_tool_bar_string);
11745 else
11746 end = i + 1;
11747 Fadd_text_properties (make_number (i), make_number (end),
11748 props, f->desired_tool_bar_string);
11749 #undef PROP
11750 }
11751
11752 UNGCPRO;
11753 }
11754
11755
11756 /* Display one line of the tool-bar of frame IT->f.
11757
11758 HEIGHT specifies the desired height of the tool-bar line.
11759 If the actual height of the glyph row is less than HEIGHT, the
11760 row's height is increased to HEIGHT, and the icons are centered
11761 vertically in the new height.
11762
11763 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11764 count a final empty row in case the tool-bar width exactly matches
11765 the window width.
11766 */
11767
11768 static void
11769 display_tool_bar_line (struct it *it, int height)
11770 {
11771 struct glyph_row *row = it->glyph_row;
11772 int max_x = it->last_visible_x;
11773 struct glyph *last;
11774
11775 prepare_desired_row (row);
11776 row->y = it->current_y;
11777
11778 /* Note that this isn't made use of if the face hasn't a box,
11779 so there's no need to check the face here. */
11780 it->start_of_box_run_p = 1;
11781
11782 while (it->current_x < max_x)
11783 {
11784 int x, n_glyphs_before, i, nglyphs;
11785 struct it it_before;
11786
11787 /* Get the next display element. */
11788 if (!get_next_display_element (it))
11789 {
11790 /* Don't count empty row if we are counting needed tool-bar lines. */
11791 if (height < 0 && !it->hpos)
11792 return;
11793 break;
11794 }
11795
11796 /* Produce glyphs. */
11797 n_glyphs_before = row->used[TEXT_AREA];
11798 it_before = *it;
11799
11800 PRODUCE_GLYPHS (it);
11801
11802 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11803 i = 0;
11804 x = it_before.current_x;
11805 while (i < nglyphs)
11806 {
11807 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11808
11809 if (x + glyph->pixel_width > max_x)
11810 {
11811 /* Glyph doesn't fit on line. Backtrack. */
11812 row->used[TEXT_AREA] = n_glyphs_before;
11813 *it = it_before;
11814 /* If this is the only glyph on this line, it will never fit on the
11815 tool-bar, so skip it. But ensure there is at least one glyph,
11816 so we don't accidentally disable the tool-bar. */
11817 if (n_glyphs_before == 0
11818 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11819 break;
11820 goto out;
11821 }
11822
11823 ++it->hpos;
11824 x += glyph->pixel_width;
11825 ++i;
11826 }
11827
11828 /* Stop at line end. */
11829 if (ITERATOR_AT_END_OF_LINE_P (it))
11830 break;
11831
11832 set_iterator_to_next (it, 1);
11833 }
11834
11835 out:;
11836
11837 row->displays_text_p = row->used[TEXT_AREA] != 0;
11838
11839 /* Use default face for the border below the tool bar.
11840
11841 FIXME: When auto-resize-tool-bars is grow-only, there is
11842 no additional border below the possibly empty tool-bar lines.
11843 So to make the extra empty lines look "normal", we have to
11844 use the tool-bar face for the border too. */
11845 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11846 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11847 it->face_id = DEFAULT_FACE_ID;
11848
11849 extend_face_to_end_of_line (it);
11850 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11851 last->right_box_line_p = 1;
11852 if (last == row->glyphs[TEXT_AREA])
11853 last->left_box_line_p = 1;
11854
11855 /* Make line the desired height and center it vertically. */
11856 if ((height -= it->max_ascent + it->max_descent) > 0)
11857 {
11858 /* Don't add more than one line height. */
11859 height %= FRAME_LINE_HEIGHT (it->f);
11860 it->max_ascent += height / 2;
11861 it->max_descent += (height + 1) / 2;
11862 }
11863
11864 compute_line_metrics (it);
11865
11866 /* If line is empty, make it occupy the rest of the tool-bar. */
11867 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11868 {
11869 row->height = row->phys_height = it->last_visible_y - row->y;
11870 row->visible_height = row->height;
11871 row->ascent = row->phys_ascent = 0;
11872 row->extra_line_spacing = 0;
11873 }
11874
11875 row->full_width_p = 1;
11876 row->continued_p = 0;
11877 row->truncated_on_left_p = 0;
11878 row->truncated_on_right_p = 0;
11879
11880 it->current_x = it->hpos = 0;
11881 it->current_y += row->height;
11882 ++it->vpos;
11883 ++it->glyph_row;
11884 }
11885
11886
11887 /* Max tool-bar height. */
11888
11889 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11890 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11891
11892 /* Value is the number of screen lines needed to make all tool-bar
11893 items of frame F visible. The number of actual rows needed is
11894 returned in *N_ROWS if non-NULL. */
11895
11896 static int
11897 tool_bar_lines_needed (struct frame *f, int *n_rows)
11898 {
11899 struct window *w = XWINDOW (f->tool_bar_window);
11900 struct it it;
11901 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11902 the desired matrix, so use (unused) mode-line row as temporary row to
11903 avoid destroying the first tool-bar row. */
11904 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11905
11906 /* Initialize an iterator for iteration over
11907 F->desired_tool_bar_string in the tool-bar window of frame F. */
11908 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11909 it.first_visible_x = 0;
11910 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11911 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11912 it.paragraph_embedding = L2R;
11913
11914 while (!ITERATOR_AT_END_P (&it))
11915 {
11916 clear_glyph_row (temp_row);
11917 it.glyph_row = temp_row;
11918 display_tool_bar_line (&it, -1);
11919 }
11920 clear_glyph_row (temp_row);
11921
11922 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11923 if (n_rows)
11924 *n_rows = it.vpos > 0 ? it.vpos : -1;
11925
11926 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11927 }
11928
11929 #endif /* !USE_GTK && !HAVE_NS */
11930
11931 #if defined USE_GTK || defined HAVE_NS
11932 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11933 #endif
11934
11935 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11936 0, 1, 0,
11937 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11938 If FRAME is nil or omitted, use the selected frame. */)
11939 (Lisp_Object frame)
11940 {
11941 int nlines = 0;
11942 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11943 struct frame *f = decode_any_frame (frame);
11944 struct window *w;
11945
11946 if (WINDOWP (f->tool_bar_window)
11947 && (w = XWINDOW (f->tool_bar_window),
11948 WINDOW_TOTAL_LINES (w) > 0))
11949 {
11950 update_tool_bar (f, 1);
11951 if (f->n_tool_bar_items)
11952 {
11953 build_desired_tool_bar_string (f);
11954 nlines = tool_bar_lines_needed (f, NULL);
11955 }
11956 }
11957 #endif
11958 return make_number (nlines);
11959 }
11960
11961
11962 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11963 height should be changed. */
11964
11965 static int
11966 redisplay_tool_bar (struct frame *f)
11967 {
11968 #if defined (USE_GTK) || defined (HAVE_NS)
11969
11970 if (FRAME_EXTERNAL_TOOL_BAR (f))
11971 update_frame_tool_bar (f);
11972 return 0;
11973
11974 #else /* !USE_GTK && !HAVE_NS */
11975
11976 struct window *w;
11977 struct it it;
11978 struct glyph_row *row;
11979
11980 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11981 do anything. This means you must start with tool-bar-lines
11982 non-zero to get the auto-sizing effect. Or in other words, you
11983 can turn off tool-bars by specifying tool-bar-lines zero. */
11984 if (!WINDOWP (f->tool_bar_window)
11985 || (w = XWINDOW (f->tool_bar_window),
11986 WINDOW_TOTAL_LINES (w) == 0))
11987 return 0;
11988
11989 /* Set up an iterator for the tool-bar window. */
11990 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11991 it.first_visible_x = 0;
11992 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11993 row = it.glyph_row;
11994
11995 /* Build a string that represents the contents of the tool-bar. */
11996 build_desired_tool_bar_string (f);
11997 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11998 /* FIXME: This should be controlled by a user option. But it
11999 doesn't make sense to have an R2L tool bar if the menu bar cannot
12000 be drawn also R2L, and making the menu bar R2L is tricky due
12001 toolkit-specific code that implements it. If an R2L tool bar is
12002 ever supported, display_tool_bar_line should also be augmented to
12003 call unproduce_glyphs like display_line and display_string
12004 do. */
12005 it.paragraph_embedding = L2R;
12006
12007 if (f->n_tool_bar_rows == 0)
12008 {
12009 int nlines;
12010
12011 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12012 nlines != WINDOW_TOTAL_LINES (w)))
12013 {
12014 Lisp_Object frame;
12015 int old_height = WINDOW_TOTAL_LINES (w);
12016
12017 XSETFRAME (frame, f);
12018 Fmodify_frame_parameters (frame,
12019 list1 (Fcons (Qtool_bar_lines,
12020 make_number (nlines))));
12021 if (WINDOW_TOTAL_LINES (w) != old_height)
12022 {
12023 clear_glyph_matrix (w->desired_matrix);
12024 f->fonts_changed = 1;
12025 return 1;
12026 }
12027 }
12028 }
12029
12030 /* Display as many lines as needed to display all tool-bar items. */
12031
12032 if (f->n_tool_bar_rows > 0)
12033 {
12034 int border, rows, height, extra;
12035
12036 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12037 border = XINT (Vtool_bar_border);
12038 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12039 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12040 else if (EQ (Vtool_bar_border, Qborder_width))
12041 border = f->border_width;
12042 else
12043 border = 0;
12044 if (border < 0)
12045 border = 0;
12046
12047 rows = f->n_tool_bar_rows;
12048 height = max (1, (it.last_visible_y - border) / rows);
12049 extra = it.last_visible_y - border - height * rows;
12050
12051 while (it.current_y < it.last_visible_y)
12052 {
12053 int h = 0;
12054 if (extra > 0 && rows-- > 0)
12055 {
12056 h = (extra + rows - 1) / rows;
12057 extra -= h;
12058 }
12059 display_tool_bar_line (&it, height + h);
12060 }
12061 }
12062 else
12063 {
12064 while (it.current_y < it.last_visible_y)
12065 display_tool_bar_line (&it, 0);
12066 }
12067
12068 /* It doesn't make much sense to try scrolling in the tool-bar
12069 window, so don't do it. */
12070 w->desired_matrix->no_scrolling_p = 1;
12071 w->must_be_updated_p = 1;
12072
12073 if (!NILP (Vauto_resize_tool_bars))
12074 {
12075 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12076 int change_height_p = 0;
12077
12078 /* If we couldn't display everything, change the tool-bar's
12079 height if there is room for more. */
12080 if (IT_STRING_CHARPOS (it) < it.end_charpos
12081 && it.current_y < max_tool_bar_height)
12082 change_height_p = 1;
12083
12084 row = it.glyph_row - 1;
12085
12086 /* If there are blank lines at the end, except for a partially
12087 visible blank line at the end that is smaller than
12088 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12089 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12090 && row->height >= FRAME_LINE_HEIGHT (f))
12091 change_height_p = 1;
12092
12093 /* If row displays tool-bar items, but is partially visible,
12094 change the tool-bar's height. */
12095 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12096 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12097 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12098 change_height_p = 1;
12099
12100 /* Resize windows as needed by changing the `tool-bar-lines'
12101 frame parameter. */
12102 if (change_height_p)
12103 {
12104 Lisp_Object frame;
12105 int old_height = WINDOW_TOTAL_LINES (w);
12106 int nrows;
12107 int nlines = tool_bar_lines_needed (f, &nrows);
12108
12109 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12110 && !f->minimize_tool_bar_window_p)
12111 ? (nlines > old_height)
12112 : (nlines != old_height));
12113 f->minimize_tool_bar_window_p = 0;
12114
12115 if (change_height_p)
12116 {
12117 XSETFRAME (frame, f);
12118 Fmodify_frame_parameters (frame,
12119 list1 (Fcons (Qtool_bar_lines,
12120 make_number (nlines))));
12121 if (WINDOW_TOTAL_LINES (w) != old_height)
12122 {
12123 clear_glyph_matrix (w->desired_matrix);
12124 f->n_tool_bar_rows = nrows;
12125 f->fonts_changed = 1;
12126 return 1;
12127 }
12128 }
12129 }
12130 }
12131
12132 f->minimize_tool_bar_window_p = 0;
12133 return 0;
12134
12135 #endif /* USE_GTK || HAVE_NS */
12136 }
12137
12138 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12139
12140 /* Get information about the tool-bar item which is displayed in GLYPH
12141 on frame F. Return in *PROP_IDX the index where tool-bar item
12142 properties start in F->tool_bar_items. Value is zero if
12143 GLYPH doesn't display a tool-bar item. */
12144
12145 static int
12146 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12147 {
12148 Lisp_Object prop;
12149 int success_p;
12150 int charpos;
12151
12152 /* This function can be called asynchronously, which means we must
12153 exclude any possibility that Fget_text_property signals an
12154 error. */
12155 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12156 charpos = max (0, charpos);
12157
12158 /* Get the text property `menu-item' at pos. The value of that
12159 property is the start index of this item's properties in
12160 F->tool_bar_items. */
12161 prop = Fget_text_property (make_number (charpos),
12162 Qmenu_item, f->current_tool_bar_string);
12163 if (INTEGERP (prop))
12164 {
12165 *prop_idx = XINT (prop);
12166 success_p = 1;
12167 }
12168 else
12169 success_p = 0;
12170
12171 return success_p;
12172 }
12173
12174 \f
12175 /* Get information about the tool-bar item at position X/Y on frame F.
12176 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12177 the current matrix of the tool-bar window of F, or NULL if not
12178 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12179 item in F->tool_bar_items. Value is
12180
12181 -1 if X/Y is not on a tool-bar item
12182 0 if X/Y is on the same item that was highlighted before.
12183 1 otherwise. */
12184
12185 static int
12186 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12187 int *hpos, int *vpos, int *prop_idx)
12188 {
12189 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12190 struct window *w = XWINDOW (f->tool_bar_window);
12191 int area;
12192
12193 /* Find the glyph under X/Y. */
12194 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12195 if (*glyph == NULL)
12196 return -1;
12197
12198 /* Get the start of this tool-bar item's properties in
12199 f->tool_bar_items. */
12200 if (!tool_bar_item_info (f, *glyph, prop_idx))
12201 return -1;
12202
12203 /* Is mouse on the highlighted item? */
12204 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12205 && *vpos >= hlinfo->mouse_face_beg_row
12206 && *vpos <= hlinfo->mouse_face_end_row
12207 && (*vpos > hlinfo->mouse_face_beg_row
12208 || *hpos >= hlinfo->mouse_face_beg_col)
12209 && (*vpos < hlinfo->mouse_face_end_row
12210 || *hpos < hlinfo->mouse_face_end_col
12211 || hlinfo->mouse_face_past_end))
12212 return 0;
12213
12214 return 1;
12215 }
12216
12217
12218 /* EXPORT:
12219 Handle mouse button event on the tool-bar of frame F, at
12220 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12221 0 for button release. MODIFIERS is event modifiers for button
12222 release. */
12223
12224 void
12225 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12226 int modifiers)
12227 {
12228 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12229 struct window *w = XWINDOW (f->tool_bar_window);
12230 int hpos, vpos, prop_idx;
12231 struct glyph *glyph;
12232 Lisp_Object enabled_p;
12233 int ts;
12234
12235 /* If not on the highlighted tool-bar item, and mouse-highlight is
12236 non-nil, return. This is so we generate the tool-bar button
12237 click only when the mouse button is released on the same item as
12238 where it was pressed. However, when mouse-highlight is disabled,
12239 generate the click when the button is released regardless of the
12240 highlight, since tool-bar items are not highlighted in that
12241 case. */
12242 frame_to_window_pixel_xy (w, &x, &y);
12243 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12244 if (ts == -1
12245 || (ts != 0 && !NILP (Vmouse_highlight)))
12246 return;
12247
12248 /* When mouse-highlight is off, generate the click for the item
12249 where the button was pressed, disregarding where it was
12250 released. */
12251 if (NILP (Vmouse_highlight) && !down_p)
12252 prop_idx = last_tool_bar_item;
12253
12254 /* If item is disabled, do nothing. */
12255 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12256 if (NILP (enabled_p))
12257 return;
12258
12259 if (down_p)
12260 {
12261 /* Show item in pressed state. */
12262 if (!NILP (Vmouse_highlight))
12263 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12264 last_tool_bar_item = prop_idx;
12265 }
12266 else
12267 {
12268 Lisp_Object key, frame;
12269 struct input_event event;
12270 EVENT_INIT (event);
12271
12272 /* Show item in released state. */
12273 if (!NILP (Vmouse_highlight))
12274 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12275
12276 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12277
12278 XSETFRAME (frame, f);
12279 event.kind = TOOL_BAR_EVENT;
12280 event.frame_or_window = frame;
12281 event.arg = frame;
12282 kbd_buffer_store_event (&event);
12283
12284 event.kind = TOOL_BAR_EVENT;
12285 event.frame_or_window = frame;
12286 event.arg = key;
12287 event.modifiers = modifiers;
12288 kbd_buffer_store_event (&event);
12289 last_tool_bar_item = -1;
12290 }
12291 }
12292
12293
12294 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12295 tool-bar window-relative coordinates X/Y. Called from
12296 note_mouse_highlight. */
12297
12298 static void
12299 note_tool_bar_highlight (struct frame *f, int x, int y)
12300 {
12301 Lisp_Object window = f->tool_bar_window;
12302 struct window *w = XWINDOW (window);
12303 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12304 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12305 int hpos, vpos;
12306 struct glyph *glyph;
12307 struct glyph_row *row;
12308 int i;
12309 Lisp_Object enabled_p;
12310 int prop_idx;
12311 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12312 int mouse_down_p, rc;
12313
12314 /* Function note_mouse_highlight is called with negative X/Y
12315 values when mouse moves outside of the frame. */
12316 if (x <= 0 || y <= 0)
12317 {
12318 clear_mouse_face (hlinfo);
12319 return;
12320 }
12321
12322 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12323 if (rc < 0)
12324 {
12325 /* Not on tool-bar item. */
12326 clear_mouse_face (hlinfo);
12327 return;
12328 }
12329 else if (rc == 0)
12330 /* On same tool-bar item as before. */
12331 goto set_help_echo;
12332
12333 clear_mouse_face (hlinfo);
12334
12335 /* Mouse is down, but on different tool-bar item? */
12336 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12337 && f == dpyinfo->last_mouse_frame);
12338
12339 if (mouse_down_p
12340 && last_tool_bar_item != prop_idx)
12341 return;
12342
12343 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12344
12345 /* If tool-bar item is not enabled, don't highlight it. */
12346 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12347 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12348 {
12349 /* Compute the x-position of the glyph. In front and past the
12350 image is a space. We include this in the highlighted area. */
12351 row = MATRIX_ROW (w->current_matrix, vpos);
12352 for (i = x = 0; i < hpos; ++i)
12353 x += row->glyphs[TEXT_AREA][i].pixel_width;
12354
12355 /* Record this as the current active region. */
12356 hlinfo->mouse_face_beg_col = hpos;
12357 hlinfo->mouse_face_beg_row = vpos;
12358 hlinfo->mouse_face_beg_x = x;
12359 hlinfo->mouse_face_past_end = 0;
12360
12361 hlinfo->mouse_face_end_col = hpos + 1;
12362 hlinfo->mouse_face_end_row = vpos;
12363 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12364 hlinfo->mouse_face_window = window;
12365 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12366
12367 /* Display it as active. */
12368 show_mouse_face (hlinfo, draw);
12369 }
12370
12371 set_help_echo:
12372
12373 /* Set help_echo_string to a help string to display for this tool-bar item.
12374 XTread_socket does the rest. */
12375 help_echo_object = help_echo_window = Qnil;
12376 help_echo_pos = -1;
12377 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12378 if (NILP (help_echo_string))
12379 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12380 }
12381
12382 #endif /* !USE_GTK && !HAVE_NS */
12383
12384 #endif /* HAVE_WINDOW_SYSTEM */
12385
12386
12387 \f
12388 /************************************************************************
12389 Horizontal scrolling
12390 ************************************************************************/
12391
12392 static int hscroll_window_tree (Lisp_Object);
12393 static int hscroll_windows (Lisp_Object);
12394
12395 /* For all leaf windows in the window tree rooted at WINDOW, set their
12396 hscroll value so that PT is (i) visible in the window, and (ii) so
12397 that it is not within a certain margin at the window's left and
12398 right border. Value is non-zero if any window's hscroll has been
12399 changed. */
12400
12401 static int
12402 hscroll_window_tree (Lisp_Object window)
12403 {
12404 int hscrolled_p = 0;
12405 int hscroll_relative_p = FLOATP (Vhscroll_step);
12406 int hscroll_step_abs = 0;
12407 double hscroll_step_rel = 0;
12408
12409 if (hscroll_relative_p)
12410 {
12411 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12412 if (hscroll_step_rel < 0)
12413 {
12414 hscroll_relative_p = 0;
12415 hscroll_step_abs = 0;
12416 }
12417 }
12418 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12419 {
12420 hscroll_step_abs = XINT (Vhscroll_step);
12421 if (hscroll_step_abs < 0)
12422 hscroll_step_abs = 0;
12423 }
12424 else
12425 hscroll_step_abs = 0;
12426
12427 while (WINDOWP (window))
12428 {
12429 struct window *w = XWINDOW (window);
12430
12431 if (WINDOWP (w->contents))
12432 hscrolled_p |= hscroll_window_tree (w->contents);
12433 else if (w->cursor.vpos >= 0)
12434 {
12435 int h_margin;
12436 int text_area_width;
12437 struct glyph_row *current_cursor_row
12438 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12439 struct glyph_row *desired_cursor_row
12440 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12441 struct glyph_row *cursor_row
12442 = (desired_cursor_row->enabled_p
12443 ? desired_cursor_row
12444 : current_cursor_row);
12445 int row_r2l_p = cursor_row->reversed_p;
12446
12447 text_area_width = window_box_width (w, TEXT_AREA);
12448
12449 /* Scroll when cursor is inside this scroll margin. */
12450 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12451
12452 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12453 /* For left-to-right rows, hscroll when cursor is either
12454 (i) inside the right hscroll margin, or (ii) if it is
12455 inside the left margin and the window is already
12456 hscrolled. */
12457 && ((!row_r2l_p
12458 && ((w->hscroll
12459 && w->cursor.x <= h_margin)
12460 || (cursor_row->enabled_p
12461 && cursor_row->truncated_on_right_p
12462 && (w->cursor.x >= text_area_width - h_margin))))
12463 /* For right-to-left rows, the logic is similar,
12464 except that rules for scrolling to left and right
12465 are reversed. E.g., if cursor.x <= h_margin, we
12466 need to hscroll "to the right" unconditionally,
12467 and that will scroll the screen to the left so as
12468 to reveal the next portion of the row. */
12469 || (row_r2l_p
12470 && ((cursor_row->enabled_p
12471 /* FIXME: It is confusing to set the
12472 truncated_on_right_p flag when R2L rows
12473 are actually truncated on the left. */
12474 && cursor_row->truncated_on_right_p
12475 && w->cursor.x <= h_margin)
12476 || (w->hscroll
12477 && (w->cursor.x >= text_area_width - h_margin))))))
12478 {
12479 struct it it;
12480 ptrdiff_t hscroll;
12481 struct buffer *saved_current_buffer;
12482 ptrdiff_t pt;
12483 int wanted_x;
12484
12485 /* Find point in a display of infinite width. */
12486 saved_current_buffer = current_buffer;
12487 current_buffer = XBUFFER (w->contents);
12488
12489 if (w == XWINDOW (selected_window))
12490 pt = PT;
12491 else
12492 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12493
12494 /* Move iterator to pt starting at cursor_row->start in
12495 a line with infinite width. */
12496 init_to_row_start (&it, w, cursor_row);
12497 it.last_visible_x = INFINITY;
12498 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12499 current_buffer = saved_current_buffer;
12500
12501 /* Position cursor in window. */
12502 if (!hscroll_relative_p && hscroll_step_abs == 0)
12503 hscroll = max (0, (it.current_x
12504 - (ITERATOR_AT_END_OF_LINE_P (&it)
12505 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12506 : (text_area_width / 2))))
12507 / FRAME_COLUMN_WIDTH (it.f);
12508 else if ((!row_r2l_p
12509 && w->cursor.x >= text_area_width - h_margin)
12510 || (row_r2l_p && w->cursor.x <= h_margin))
12511 {
12512 if (hscroll_relative_p)
12513 wanted_x = text_area_width * (1 - hscroll_step_rel)
12514 - h_margin;
12515 else
12516 wanted_x = text_area_width
12517 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12518 - h_margin;
12519 hscroll
12520 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12521 }
12522 else
12523 {
12524 if (hscroll_relative_p)
12525 wanted_x = text_area_width * hscroll_step_rel
12526 + h_margin;
12527 else
12528 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12529 + h_margin;
12530 hscroll
12531 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12532 }
12533 hscroll = max (hscroll, w->min_hscroll);
12534
12535 /* Don't prevent redisplay optimizations if hscroll
12536 hasn't changed, as it will unnecessarily slow down
12537 redisplay. */
12538 if (w->hscroll != hscroll)
12539 {
12540 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12541 w->hscroll = hscroll;
12542 hscrolled_p = 1;
12543 }
12544 }
12545 }
12546
12547 window = w->next;
12548 }
12549
12550 /* Value is non-zero if hscroll of any leaf window has been changed. */
12551 return hscrolled_p;
12552 }
12553
12554
12555 /* Set hscroll so that cursor is visible and not inside horizontal
12556 scroll margins for all windows in the tree rooted at WINDOW. See
12557 also hscroll_window_tree above. Value is non-zero if any window's
12558 hscroll has been changed. If it has, desired matrices on the frame
12559 of WINDOW are cleared. */
12560
12561 static int
12562 hscroll_windows (Lisp_Object window)
12563 {
12564 int hscrolled_p = hscroll_window_tree (window);
12565 if (hscrolled_p)
12566 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12567 return hscrolled_p;
12568 }
12569
12570
12571 \f
12572 /************************************************************************
12573 Redisplay
12574 ************************************************************************/
12575
12576 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12577 to a non-zero value. This is sometimes handy to have in a debugger
12578 session. */
12579
12580 #ifdef GLYPH_DEBUG
12581
12582 /* First and last unchanged row for try_window_id. */
12583
12584 static int debug_first_unchanged_at_end_vpos;
12585 static int debug_last_unchanged_at_beg_vpos;
12586
12587 /* Delta vpos and y. */
12588
12589 static int debug_dvpos, debug_dy;
12590
12591 /* Delta in characters and bytes for try_window_id. */
12592
12593 static ptrdiff_t debug_delta, debug_delta_bytes;
12594
12595 /* Values of window_end_pos and window_end_vpos at the end of
12596 try_window_id. */
12597
12598 static ptrdiff_t debug_end_vpos;
12599
12600 /* Append a string to W->desired_matrix->method. FMT is a printf
12601 format string. If trace_redisplay_p is non-zero also printf the
12602 resulting string to stderr. */
12603
12604 static void debug_method_add (struct window *, char const *, ...)
12605 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12606
12607 static void
12608 debug_method_add (struct window *w, char const *fmt, ...)
12609 {
12610 void *ptr = w;
12611 char *method = w->desired_matrix->method;
12612 int len = strlen (method);
12613 int size = sizeof w->desired_matrix->method;
12614 int remaining = size - len - 1;
12615 va_list ap;
12616
12617 if (len && remaining)
12618 {
12619 method[len] = '|';
12620 --remaining, ++len;
12621 }
12622
12623 va_start (ap, fmt);
12624 vsnprintf (method + len, remaining + 1, fmt, ap);
12625 va_end (ap);
12626
12627 if (trace_redisplay_p)
12628 fprintf (stderr, "%p (%s): %s\n",
12629 ptr,
12630 ((BUFFERP (w->contents)
12631 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12632 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12633 : "no buffer"),
12634 method + len);
12635 }
12636
12637 #endif /* GLYPH_DEBUG */
12638
12639
12640 /* Value is non-zero if all changes in window W, which displays
12641 current_buffer, are in the text between START and END. START is a
12642 buffer position, END is given as a distance from Z. Used in
12643 redisplay_internal for display optimization. */
12644
12645 static int
12646 text_outside_line_unchanged_p (struct window *w,
12647 ptrdiff_t start, ptrdiff_t end)
12648 {
12649 int unchanged_p = 1;
12650
12651 /* If text or overlays have changed, see where. */
12652 if (window_outdated (w))
12653 {
12654 /* Gap in the line? */
12655 if (GPT < start || Z - GPT < end)
12656 unchanged_p = 0;
12657
12658 /* Changes start in front of the line, or end after it? */
12659 if (unchanged_p
12660 && (BEG_UNCHANGED < start - 1
12661 || END_UNCHANGED < end))
12662 unchanged_p = 0;
12663
12664 /* If selective display, can't optimize if changes start at the
12665 beginning of the line. */
12666 if (unchanged_p
12667 && INTEGERP (BVAR (current_buffer, selective_display))
12668 && XINT (BVAR (current_buffer, selective_display)) > 0
12669 && (BEG_UNCHANGED < start || GPT <= start))
12670 unchanged_p = 0;
12671
12672 /* If there are overlays at the start or end of the line, these
12673 may have overlay strings with newlines in them. A change at
12674 START, for instance, may actually concern the display of such
12675 overlay strings as well, and they are displayed on different
12676 lines. So, quickly rule out this case. (For the future, it
12677 might be desirable to implement something more telling than
12678 just BEG/END_UNCHANGED.) */
12679 if (unchanged_p)
12680 {
12681 if (BEG + BEG_UNCHANGED == start
12682 && overlay_touches_p (start))
12683 unchanged_p = 0;
12684 if (END_UNCHANGED == end
12685 && overlay_touches_p (Z - end))
12686 unchanged_p = 0;
12687 }
12688
12689 /* Under bidi reordering, adding or deleting a character in the
12690 beginning of a paragraph, before the first strong directional
12691 character, can change the base direction of the paragraph (unless
12692 the buffer specifies a fixed paragraph direction), which will
12693 require to redisplay the whole paragraph. It might be worthwhile
12694 to find the paragraph limits and widen the range of redisplayed
12695 lines to that, but for now just give up this optimization. */
12696 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12697 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12698 unchanged_p = 0;
12699 }
12700
12701 return unchanged_p;
12702 }
12703
12704
12705 /* Do a frame update, taking possible shortcuts into account. This is
12706 the main external entry point for redisplay.
12707
12708 If the last redisplay displayed an echo area message and that message
12709 is no longer requested, we clear the echo area or bring back the
12710 mini-buffer if that is in use. */
12711
12712 void
12713 redisplay (void)
12714 {
12715 redisplay_internal ();
12716 }
12717
12718
12719 static Lisp_Object
12720 overlay_arrow_string_or_property (Lisp_Object var)
12721 {
12722 Lisp_Object val;
12723
12724 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12725 return val;
12726
12727 return Voverlay_arrow_string;
12728 }
12729
12730 /* Return 1 if there are any overlay-arrows in current_buffer. */
12731 static int
12732 overlay_arrow_in_current_buffer_p (void)
12733 {
12734 Lisp_Object vlist;
12735
12736 for (vlist = Voverlay_arrow_variable_list;
12737 CONSP (vlist);
12738 vlist = XCDR (vlist))
12739 {
12740 Lisp_Object var = XCAR (vlist);
12741 Lisp_Object val;
12742
12743 if (!SYMBOLP (var))
12744 continue;
12745 val = find_symbol_value (var);
12746 if (MARKERP (val)
12747 && current_buffer == XMARKER (val)->buffer)
12748 return 1;
12749 }
12750 return 0;
12751 }
12752
12753
12754 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12755 has changed. */
12756
12757 static int
12758 overlay_arrows_changed_p (void)
12759 {
12760 Lisp_Object vlist;
12761
12762 for (vlist = Voverlay_arrow_variable_list;
12763 CONSP (vlist);
12764 vlist = XCDR (vlist))
12765 {
12766 Lisp_Object var = XCAR (vlist);
12767 Lisp_Object val, pstr;
12768
12769 if (!SYMBOLP (var))
12770 continue;
12771 val = find_symbol_value (var);
12772 if (!MARKERP (val))
12773 continue;
12774 if (! EQ (COERCE_MARKER (val),
12775 Fget (var, Qlast_arrow_position))
12776 || ! (pstr = overlay_arrow_string_or_property (var),
12777 EQ (pstr, Fget (var, Qlast_arrow_string))))
12778 return 1;
12779 }
12780 return 0;
12781 }
12782
12783 /* Mark overlay arrows to be updated on next redisplay. */
12784
12785 static void
12786 update_overlay_arrows (int up_to_date)
12787 {
12788 Lisp_Object vlist;
12789
12790 for (vlist = Voverlay_arrow_variable_list;
12791 CONSP (vlist);
12792 vlist = XCDR (vlist))
12793 {
12794 Lisp_Object var = XCAR (vlist);
12795
12796 if (!SYMBOLP (var))
12797 continue;
12798
12799 if (up_to_date > 0)
12800 {
12801 Lisp_Object val = find_symbol_value (var);
12802 Fput (var, Qlast_arrow_position,
12803 COERCE_MARKER (val));
12804 Fput (var, Qlast_arrow_string,
12805 overlay_arrow_string_or_property (var));
12806 }
12807 else if (up_to_date < 0
12808 || !NILP (Fget (var, Qlast_arrow_position)))
12809 {
12810 Fput (var, Qlast_arrow_position, Qt);
12811 Fput (var, Qlast_arrow_string, Qt);
12812 }
12813 }
12814 }
12815
12816
12817 /* Return overlay arrow string to display at row.
12818 Return integer (bitmap number) for arrow bitmap in left fringe.
12819 Return nil if no overlay arrow. */
12820
12821 static Lisp_Object
12822 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12823 {
12824 Lisp_Object vlist;
12825
12826 for (vlist = Voverlay_arrow_variable_list;
12827 CONSP (vlist);
12828 vlist = XCDR (vlist))
12829 {
12830 Lisp_Object var = XCAR (vlist);
12831 Lisp_Object val;
12832
12833 if (!SYMBOLP (var))
12834 continue;
12835
12836 val = find_symbol_value (var);
12837
12838 if (MARKERP (val)
12839 && current_buffer == XMARKER (val)->buffer
12840 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12841 {
12842 if (FRAME_WINDOW_P (it->f)
12843 /* FIXME: if ROW->reversed_p is set, this should test
12844 the right fringe, not the left one. */
12845 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12846 {
12847 #ifdef HAVE_WINDOW_SYSTEM
12848 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12849 {
12850 int fringe_bitmap;
12851 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12852 return make_number (fringe_bitmap);
12853 }
12854 #endif
12855 return make_number (-1); /* Use default arrow bitmap. */
12856 }
12857 return overlay_arrow_string_or_property (var);
12858 }
12859 }
12860
12861 return Qnil;
12862 }
12863
12864 /* Return 1 if point moved out of or into a composition. Otherwise
12865 return 0. PREV_BUF and PREV_PT are the last point buffer and
12866 position. BUF and PT are the current point buffer and position. */
12867
12868 static int
12869 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12870 struct buffer *buf, ptrdiff_t pt)
12871 {
12872 ptrdiff_t start, end;
12873 Lisp_Object prop;
12874 Lisp_Object buffer;
12875
12876 XSETBUFFER (buffer, buf);
12877 /* Check a composition at the last point if point moved within the
12878 same buffer. */
12879 if (prev_buf == buf)
12880 {
12881 if (prev_pt == pt)
12882 /* Point didn't move. */
12883 return 0;
12884
12885 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12886 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12887 && composition_valid_p (start, end, prop)
12888 && start < prev_pt && end > prev_pt)
12889 /* The last point was within the composition. Return 1 iff
12890 point moved out of the composition. */
12891 return (pt <= start || pt >= end);
12892 }
12893
12894 /* Check a composition at the current point. */
12895 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12896 && find_composition (pt, -1, &start, &end, &prop, buffer)
12897 && composition_valid_p (start, end, prop)
12898 && start < pt && end > pt);
12899 }
12900
12901 /* Reconsider the clip changes of buffer which is displayed in W. */
12902
12903 static void
12904 reconsider_clip_changes (struct window *w)
12905 {
12906 struct buffer *b = XBUFFER (w->contents);
12907
12908 if (b->clip_changed
12909 && w->window_end_valid
12910 && w->current_matrix->buffer == b
12911 && w->current_matrix->zv == BUF_ZV (b)
12912 && w->current_matrix->begv == BUF_BEGV (b))
12913 b->clip_changed = 0;
12914
12915 /* If display wasn't paused, and W is not a tool bar window, see if
12916 point has been moved into or out of a composition. In that case,
12917 we set b->clip_changed to 1 to force updating the screen. If
12918 b->clip_changed has already been set to 1, we can skip this
12919 check. */
12920 if (!b->clip_changed && w->window_end_valid)
12921 {
12922 ptrdiff_t pt = (w == XWINDOW (selected_window)
12923 ? PT : marker_position (w->pointm));
12924
12925 if ((w->current_matrix->buffer != b || pt != w->last_point)
12926 && check_point_in_composition (w->current_matrix->buffer,
12927 w->last_point, b, pt))
12928 b->clip_changed = 1;
12929 }
12930 }
12931
12932 static void
12933 propagate_buffer_redisplay (void)
12934 { /* Resetting b->text->redisplay is problematic!
12935 We can't just reset it in the case that some window that displays
12936 it has not been redisplayed; and such a window can stay
12937 unredisplayed for a long time if it's currently invisible.
12938 But we do want to reset it at the end of redisplay otherwise
12939 its displayed windows will keep being redisplayed over and over
12940 again.
12941 So we copy all b->text->redisplay flags up to their windows here,
12942 such that mark_window_display_accurate can safely reset
12943 b->text->redisplay. */
12944 Lisp_Object ws = window_list ();
12945 for (; CONSP (ws); ws = XCDR (ws))
12946 {
12947 struct window *thisw = XWINDOW (XCAR (ws));
12948 struct buffer *thisb = XBUFFER (thisw->contents);
12949 if (thisb->text->redisplay)
12950 thisw->redisplay = true;
12951 }
12952 }
12953
12954 #define STOP_POLLING \
12955 do { if (! polling_stopped_here) stop_polling (); \
12956 polling_stopped_here = 1; } while (0)
12957
12958 #define RESUME_POLLING \
12959 do { if (polling_stopped_here) start_polling (); \
12960 polling_stopped_here = 0; } while (0)
12961
12962
12963 /* Perhaps in the future avoid recentering windows if it
12964 is not necessary; currently that causes some problems. */
12965
12966 static void
12967 redisplay_internal (void)
12968 {
12969 struct window *w = XWINDOW (selected_window);
12970 struct window *sw;
12971 struct frame *fr;
12972 int pending;
12973 bool must_finish = 0, match_p;
12974 struct text_pos tlbufpos, tlendpos;
12975 int number_of_visible_frames;
12976 ptrdiff_t count;
12977 struct frame *sf;
12978 int polling_stopped_here = 0;
12979 Lisp_Object tail, frame;
12980
12981 /* True means redisplay has to consider all windows on all
12982 frames. False, only selected_window is considered. */
12983 bool consider_all_windows_p;
12984
12985 /* True means redisplay has to redisplay the miniwindow. */
12986 bool update_miniwindow_p = false;
12987
12988 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12989
12990 /* No redisplay if running in batch mode or frame is not yet fully
12991 initialized, or redisplay is explicitly turned off by setting
12992 Vinhibit_redisplay. */
12993 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12994 || !NILP (Vinhibit_redisplay))
12995 return;
12996
12997 /* Don't examine these until after testing Vinhibit_redisplay.
12998 When Emacs is shutting down, perhaps because its connection to
12999 X has dropped, we should not look at them at all. */
13000 fr = XFRAME (w->frame);
13001 sf = SELECTED_FRAME ();
13002
13003 if (!fr->glyphs_initialized_p)
13004 return;
13005
13006 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13007 if (popup_activated ())
13008 return;
13009 #endif
13010
13011 /* I don't think this happens but let's be paranoid. */
13012 if (redisplaying_p)
13013 return;
13014
13015 /* Record a function that clears redisplaying_p
13016 when we leave this function. */
13017 count = SPECPDL_INDEX ();
13018 record_unwind_protect_void (unwind_redisplay);
13019 redisplaying_p = 1;
13020 specbind (Qinhibit_free_realized_faces, Qnil);
13021
13022 /* Record this function, so it appears on the profiler's backtraces. */
13023 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13024
13025 FOR_EACH_FRAME (tail, frame)
13026 XFRAME (frame)->already_hscrolled_p = 0;
13027
13028 retry:
13029 /* Remember the currently selected window. */
13030 sw = w;
13031
13032 pending = 0;
13033 last_escape_glyph_frame = NULL;
13034 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13035 last_glyphless_glyph_frame = NULL;
13036 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13037
13038 /* If face_change_count is non-zero, init_iterator will free all
13039 realized faces, which includes the faces referenced from current
13040 matrices. So, we can't reuse current matrices in this case. */
13041 if (face_change_count)
13042 windows_or_buffers_changed = 47;
13043
13044 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13045 && FRAME_TTY (sf)->previous_frame != sf)
13046 {
13047 /* Since frames on a single ASCII terminal share the same
13048 display area, displaying a different frame means redisplay
13049 the whole thing. */
13050 SET_FRAME_GARBAGED (sf);
13051 #ifndef DOS_NT
13052 set_tty_color_mode (FRAME_TTY (sf), sf);
13053 #endif
13054 FRAME_TTY (sf)->previous_frame = sf;
13055 }
13056
13057 /* Set the visible flags for all frames. Do this before checking for
13058 resized or garbaged frames; they want to know if their frames are
13059 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13060 number_of_visible_frames = 0;
13061
13062 FOR_EACH_FRAME (tail, frame)
13063 {
13064 struct frame *f = XFRAME (frame);
13065
13066 if (FRAME_VISIBLE_P (f))
13067 {
13068 ++number_of_visible_frames;
13069 /* Adjust matrices for visible frames only. */
13070 if (f->fonts_changed)
13071 {
13072 adjust_frame_glyphs (f);
13073 f->fonts_changed = 0;
13074 }
13075 /* If cursor type has been changed on the frame
13076 other than selected, consider all frames. */
13077 if (f != sf && f->cursor_type_changed)
13078 update_mode_lines = 31;
13079 }
13080 clear_desired_matrices (f);
13081 }
13082
13083 /* Notice any pending interrupt request to change frame size. */
13084 do_pending_window_change (1);
13085
13086 /* do_pending_window_change could change the selected_window due to
13087 frame resizing which makes the selected window too small. */
13088 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13089 sw = w;
13090
13091 /* Clear frames marked as garbaged. */
13092 clear_garbaged_frames ();
13093
13094 /* Build menubar and tool-bar items. */
13095 if (NILP (Vmemory_full))
13096 prepare_menu_bars ();
13097
13098 reconsider_clip_changes (w);
13099
13100 /* In most cases selected window displays current buffer. */
13101 match_p = XBUFFER (w->contents) == current_buffer;
13102 if (match_p)
13103 {
13104 /* Detect case that we need to write or remove a star in the mode line. */
13105 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13106 w->update_mode_line = 1;
13107
13108 if (mode_line_update_needed (w))
13109 w->update_mode_line = 1;
13110 }
13111
13112 /* Normally the message* functions will have already displayed and
13113 updated the echo area, but the frame may have been trashed, or
13114 the update may have been preempted, so display the echo area
13115 again here. Checking message_cleared_p captures the case that
13116 the echo area should be cleared. */
13117 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13118 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13119 || (message_cleared_p
13120 && minibuf_level == 0
13121 /* If the mini-window is currently selected, this means the
13122 echo-area doesn't show through. */
13123 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13124 {
13125 int window_height_changed_p = echo_area_display (0);
13126
13127 if (message_cleared_p)
13128 update_miniwindow_p = true;
13129
13130 must_finish = 1;
13131
13132 /* If we don't display the current message, don't clear the
13133 message_cleared_p flag, because, if we did, we wouldn't clear
13134 the echo area in the next redisplay which doesn't preserve
13135 the echo area. */
13136 if (!display_last_displayed_message_p)
13137 message_cleared_p = 0;
13138
13139 if (window_height_changed_p)
13140 {
13141 windows_or_buffers_changed = 50;
13142
13143 /* If window configuration was changed, frames may have been
13144 marked garbaged. Clear them or we will experience
13145 surprises wrt scrolling. */
13146 clear_garbaged_frames ();
13147 }
13148 }
13149 else if (EQ (selected_window, minibuf_window)
13150 && (current_buffer->clip_changed || window_outdated (w))
13151 && resize_mini_window (w, 0))
13152 {
13153 /* Resized active mini-window to fit the size of what it is
13154 showing if its contents might have changed. */
13155 must_finish = 1;
13156
13157 /* If window configuration was changed, frames may have been
13158 marked garbaged. Clear them or we will experience
13159 surprises wrt scrolling. */
13160 clear_garbaged_frames ();
13161 }
13162
13163 if (windows_or_buffers_changed && !update_mode_lines)
13164 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13165 only the windows's contents needs to be refreshed, or whether the
13166 mode-lines also need a refresh. */
13167 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13168 ? REDISPLAY_SOME : 32);
13169
13170 /* If specs for an arrow have changed, do thorough redisplay
13171 to ensure we remove any arrow that should no longer exist. */
13172 if (overlay_arrows_changed_p ())
13173 /* Apparently, this is the only case where we update other windows,
13174 without updating other mode-lines. */
13175 windows_or_buffers_changed = 49;
13176
13177 consider_all_windows_p = (update_mode_lines
13178 || windows_or_buffers_changed);
13179
13180 #define AINC(a,i) \
13181 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13182 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13183
13184 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13185 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13186
13187 /* Optimize the case that only the line containing the cursor in the
13188 selected window has changed. Variables starting with this_ are
13189 set in display_line and record information about the line
13190 containing the cursor. */
13191 tlbufpos = this_line_start_pos;
13192 tlendpos = this_line_end_pos;
13193 if (!consider_all_windows_p
13194 && CHARPOS (tlbufpos) > 0
13195 && !w->update_mode_line
13196 && !current_buffer->clip_changed
13197 && !current_buffer->prevent_redisplay_optimizations_p
13198 && FRAME_VISIBLE_P (XFRAME (w->frame))
13199 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13200 && !XFRAME (w->frame)->cursor_type_changed
13201 /* Make sure recorded data applies to current buffer, etc. */
13202 && this_line_buffer == current_buffer
13203 && match_p
13204 && !w->force_start
13205 && !w->optional_new_start
13206 /* Point must be on the line that we have info recorded about. */
13207 && PT >= CHARPOS (tlbufpos)
13208 && PT <= Z - CHARPOS (tlendpos)
13209 /* All text outside that line, including its final newline,
13210 must be unchanged. */
13211 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13212 CHARPOS (tlendpos)))
13213 {
13214 if (CHARPOS (tlbufpos) > BEGV
13215 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13216 && (CHARPOS (tlbufpos) == ZV
13217 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13218 /* Former continuation line has disappeared by becoming empty. */
13219 goto cancel;
13220 else if (window_outdated (w) || MINI_WINDOW_P (w))
13221 {
13222 /* We have to handle the case of continuation around a
13223 wide-column character (see the comment in indent.c around
13224 line 1340).
13225
13226 For instance, in the following case:
13227
13228 -------- Insert --------
13229 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13230 J_I_ ==> J_I_ `^^' are cursors.
13231 ^^ ^^
13232 -------- --------
13233
13234 As we have to redraw the line above, we cannot use this
13235 optimization. */
13236
13237 struct it it;
13238 int line_height_before = this_line_pixel_height;
13239
13240 /* Note that start_display will handle the case that the
13241 line starting at tlbufpos is a continuation line. */
13242 start_display (&it, w, tlbufpos);
13243
13244 /* Implementation note: It this still necessary? */
13245 if (it.current_x != this_line_start_x)
13246 goto cancel;
13247
13248 TRACE ((stderr, "trying display optimization 1\n"));
13249 w->cursor.vpos = -1;
13250 overlay_arrow_seen = 0;
13251 it.vpos = this_line_vpos;
13252 it.current_y = this_line_y;
13253 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13254 display_line (&it);
13255
13256 /* If line contains point, is not continued,
13257 and ends at same distance from eob as before, we win. */
13258 if (w->cursor.vpos >= 0
13259 /* Line is not continued, otherwise this_line_start_pos
13260 would have been set to 0 in display_line. */
13261 && CHARPOS (this_line_start_pos)
13262 /* Line ends as before. */
13263 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13264 /* Line has same height as before. Otherwise other lines
13265 would have to be shifted up or down. */
13266 && this_line_pixel_height == line_height_before)
13267 {
13268 /* If this is not the window's last line, we must adjust
13269 the charstarts of the lines below. */
13270 if (it.current_y < it.last_visible_y)
13271 {
13272 struct glyph_row *row
13273 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13274 ptrdiff_t delta, delta_bytes;
13275
13276 /* We used to distinguish between two cases here,
13277 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13278 when the line ends in a newline or the end of the
13279 buffer's accessible portion. But both cases did
13280 the same, so they were collapsed. */
13281 delta = (Z
13282 - CHARPOS (tlendpos)
13283 - MATRIX_ROW_START_CHARPOS (row));
13284 delta_bytes = (Z_BYTE
13285 - BYTEPOS (tlendpos)
13286 - MATRIX_ROW_START_BYTEPOS (row));
13287
13288 increment_matrix_positions (w->current_matrix,
13289 this_line_vpos + 1,
13290 w->current_matrix->nrows,
13291 delta, delta_bytes);
13292 }
13293
13294 /* If this row displays text now but previously didn't,
13295 or vice versa, w->window_end_vpos may have to be
13296 adjusted. */
13297 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13298 {
13299 if (w->window_end_vpos < this_line_vpos)
13300 w->window_end_vpos = this_line_vpos;
13301 }
13302 else if (w->window_end_vpos == this_line_vpos
13303 && this_line_vpos > 0)
13304 w->window_end_vpos = this_line_vpos - 1;
13305 w->window_end_valid = 0;
13306
13307 /* Update hint: No need to try to scroll in update_window. */
13308 w->desired_matrix->no_scrolling_p = 1;
13309
13310 #ifdef GLYPH_DEBUG
13311 *w->desired_matrix->method = 0;
13312 debug_method_add (w, "optimization 1");
13313 #endif
13314 #ifdef HAVE_WINDOW_SYSTEM
13315 update_window_fringes (w, 0);
13316 #endif
13317 goto update;
13318 }
13319 else
13320 goto cancel;
13321 }
13322 else if (/* Cursor position hasn't changed. */
13323 PT == w->last_point
13324 /* Make sure the cursor was last displayed
13325 in this window. Otherwise we have to reposition it. */
13326 && 0 <= w->cursor.vpos
13327 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13328 {
13329 if (!must_finish)
13330 {
13331 do_pending_window_change (1);
13332 /* If selected_window changed, redisplay again. */
13333 if (WINDOWP (selected_window)
13334 && (w = XWINDOW (selected_window)) != sw)
13335 goto retry;
13336
13337 /* We used to always goto end_of_redisplay here, but this
13338 isn't enough if we have a blinking cursor. */
13339 if (w->cursor_off_p == w->last_cursor_off_p)
13340 goto end_of_redisplay;
13341 }
13342 goto update;
13343 }
13344 /* If highlighting the region, or if the cursor is in the echo area,
13345 then we can't just move the cursor. */
13346 else if (NILP (Vshow_trailing_whitespace)
13347 && !cursor_in_echo_area)
13348 {
13349 struct it it;
13350 struct glyph_row *row;
13351
13352 /* Skip from tlbufpos to PT and see where it is. Note that
13353 PT may be in invisible text. If so, we will end at the
13354 next visible position. */
13355 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13356 NULL, DEFAULT_FACE_ID);
13357 it.current_x = this_line_start_x;
13358 it.current_y = this_line_y;
13359 it.vpos = this_line_vpos;
13360
13361 /* The call to move_it_to stops in front of PT, but
13362 moves over before-strings. */
13363 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13364
13365 if (it.vpos == this_line_vpos
13366 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13367 row->enabled_p))
13368 {
13369 eassert (this_line_vpos == it.vpos);
13370 eassert (this_line_y == it.current_y);
13371 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13372 #ifdef GLYPH_DEBUG
13373 *w->desired_matrix->method = 0;
13374 debug_method_add (w, "optimization 3");
13375 #endif
13376 goto update;
13377 }
13378 else
13379 goto cancel;
13380 }
13381
13382 cancel:
13383 /* Text changed drastically or point moved off of line. */
13384 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13385 }
13386
13387 CHARPOS (this_line_start_pos) = 0;
13388 ++clear_face_cache_count;
13389 #ifdef HAVE_WINDOW_SYSTEM
13390 ++clear_image_cache_count;
13391 #endif
13392
13393 /* Build desired matrices, and update the display. If
13394 consider_all_windows_p is non-zero, do it for all windows on all
13395 frames. Otherwise do it for selected_window, only. */
13396
13397 if (consider_all_windows_p)
13398 {
13399 FOR_EACH_FRAME (tail, frame)
13400 XFRAME (frame)->updated_p = 0;
13401
13402 propagate_buffer_redisplay ();
13403
13404 FOR_EACH_FRAME (tail, frame)
13405 {
13406 struct frame *f = XFRAME (frame);
13407
13408 /* We don't have to do anything for unselected terminal
13409 frames. */
13410 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13411 && !EQ (FRAME_TTY (f)->top_frame, frame))
13412 continue;
13413
13414 retry_frame:
13415
13416 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13417 {
13418 bool gcscrollbars
13419 /* Only GC scollbars when we redisplay the whole frame. */
13420 = f->redisplay || !REDISPLAY_SOME_P ();
13421 /* Mark all the scroll bars to be removed; we'll redeem
13422 the ones we want when we redisplay their windows. */
13423 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13424 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13425
13426 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13427 redisplay_windows (FRAME_ROOT_WINDOW (f));
13428 /* Remember that the invisible frames need to be redisplayed next
13429 time they're visible. */
13430 else if (!REDISPLAY_SOME_P ())
13431 f->redisplay = true;
13432
13433 /* The X error handler may have deleted that frame. */
13434 if (!FRAME_LIVE_P (f))
13435 continue;
13436
13437 /* Any scroll bars which redisplay_windows should have
13438 nuked should now go away. */
13439 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13440 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13441
13442 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13443 {
13444 /* If fonts changed on visible frame, display again. */
13445 if (f->fonts_changed)
13446 {
13447 adjust_frame_glyphs (f);
13448 f->fonts_changed = 0;
13449 goto retry_frame;
13450 }
13451
13452 /* See if we have to hscroll. */
13453 if (!f->already_hscrolled_p)
13454 {
13455 f->already_hscrolled_p = 1;
13456 if (hscroll_windows (f->root_window))
13457 goto retry_frame;
13458 }
13459
13460 /* Prevent various kinds of signals during display
13461 update. stdio is not robust about handling
13462 signals, which can cause an apparent I/O error. */
13463 if (interrupt_input)
13464 unrequest_sigio ();
13465 STOP_POLLING;
13466
13467 pending |= update_frame (f, 0, 0);
13468 f->cursor_type_changed = 0;
13469 f->updated_p = 1;
13470 }
13471 }
13472 }
13473
13474 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13475
13476 if (!pending)
13477 {
13478 /* Do the mark_window_display_accurate after all windows have
13479 been redisplayed because this call resets flags in buffers
13480 which are needed for proper redisplay. */
13481 FOR_EACH_FRAME (tail, frame)
13482 {
13483 struct frame *f = XFRAME (frame);
13484 if (f->updated_p)
13485 {
13486 f->redisplay = false;
13487 mark_window_display_accurate (f->root_window, 1);
13488 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13489 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13490 }
13491 }
13492 }
13493 }
13494 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13495 {
13496 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13497 struct frame *mini_frame;
13498
13499 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13500 /* Use list_of_error, not Qerror, so that
13501 we catch only errors and don't run the debugger. */
13502 internal_condition_case_1 (redisplay_window_1, selected_window,
13503 list_of_error,
13504 redisplay_window_error);
13505 if (update_miniwindow_p)
13506 internal_condition_case_1 (redisplay_window_1, mini_window,
13507 list_of_error,
13508 redisplay_window_error);
13509
13510 /* Compare desired and current matrices, perform output. */
13511
13512 update:
13513 /* If fonts changed, display again. */
13514 if (sf->fonts_changed)
13515 goto retry;
13516
13517 /* Prevent various kinds of signals during display update.
13518 stdio is not robust about handling signals,
13519 which can cause an apparent I/O error. */
13520 if (interrupt_input)
13521 unrequest_sigio ();
13522 STOP_POLLING;
13523
13524 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13525 {
13526 if (hscroll_windows (selected_window))
13527 goto retry;
13528
13529 XWINDOW (selected_window)->must_be_updated_p = true;
13530 pending = update_frame (sf, 0, 0);
13531 sf->cursor_type_changed = 0;
13532 }
13533
13534 /* We may have called echo_area_display at the top of this
13535 function. If the echo area is on another frame, that may
13536 have put text on a frame other than the selected one, so the
13537 above call to update_frame would not have caught it. Catch
13538 it here. */
13539 mini_window = FRAME_MINIBUF_WINDOW (sf);
13540 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13541
13542 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13543 {
13544 XWINDOW (mini_window)->must_be_updated_p = true;
13545 pending |= update_frame (mini_frame, 0, 0);
13546 mini_frame->cursor_type_changed = 0;
13547 if (!pending && hscroll_windows (mini_window))
13548 goto retry;
13549 }
13550 }
13551
13552 /* If display was paused because of pending input, make sure we do a
13553 thorough update the next time. */
13554 if (pending)
13555 {
13556 /* Prevent the optimization at the beginning of
13557 redisplay_internal that tries a single-line update of the
13558 line containing the cursor in the selected window. */
13559 CHARPOS (this_line_start_pos) = 0;
13560
13561 /* Let the overlay arrow be updated the next time. */
13562 update_overlay_arrows (0);
13563
13564 /* If we pause after scrolling, some rows in the current
13565 matrices of some windows are not valid. */
13566 if (!WINDOW_FULL_WIDTH_P (w)
13567 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13568 update_mode_lines = 36;
13569 }
13570 else
13571 {
13572 if (!consider_all_windows_p)
13573 {
13574 /* This has already been done above if
13575 consider_all_windows_p is set. */
13576 if (XBUFFER (w->contents)->text->redisplay
13577 && buffer_window_count (XBUFFER (w->contents)) > 1)
13578 /* This can happen if b->text->redisplay was set during
13579 jit-lock. */
13580 propagate_buffer_redisplay ();
13581 mark_window_display_accurate_1 (w, 1);
13582
13583 /* Say overlay arrows are up to date. */
13584 update_overlay_arrows (1);
13585
13586 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13587 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13588 }
13589
13590 update_mode_lines = 0;
13591 windows_or_buffers_changed = 0;
13592 }
13593
13594 /* Start SIGIO interrupts coming again. Having them off during the
13595 code above makes it less likely one will discard output, but not
13596 impossible, since there might be stuff in the system buffer here.
13597 But it is much hairier to try to do anything about that. */
13598 if (interrupt_input)
13599 request_sigio ();
13600 RESUME_POLLING;
13601
13602 /* If a frame has become visible which was not before, redisplay
13603 again, so that we display it. Expose events for such a frame
13604 (which it gets when becoming visible) don't call the parts of
13605 redisplay constructing glyphs, so simply exposing a frame won't
13606 display anything in this case. So, we have to display these
13607 frames here explicitly. */
13608 if (!pending)
13609 {
13610 int new_count = 0;
13611
13612 FOR_EACH_FRAME (tail, frame)
13613 {
13614 if (XFRAME (frame)->visible)
13615 new_count++;
13616 }
13617
13618 if (new_count != number_of_visible_frames)
13619 windows_or_buffers_changed = 52;
13620 }
13621
13622 /* Change frame size now if a change is pending. */
13623 do_pending_window_change (1);
13624
13625 /* If we just did a pending size change, or have additional
13626 visible frames, or selected_window changed, redisplay again. */
13627 if ((windows_or_buffers_changed && !pending)
13628 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13629 goto retry;
13630
13631 /* Clear the face and image caches.
13632
13633 We used to do this only if consider_all_windows_p. But the cache
13634 needs to be cleared if a timer creates images in the current
13635 buffer (e.g. the test case in Bug#6230). */
13636
13637 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13638 {
13639 clear_face_cache (0);
13640 clear_face_cache_count = 0;
13641 }
13642
13643 #ifdef HAVE_WINDOW_SYSTEM
13644 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13645 {
13646 clear_image_caches (Qnil);
13647 clear_image_cache_count = 0;
13648 }
13649 #endif /* HAVE_WINDOW_SYSTEM */
13650
13651 end_of_redisplay:
13652 unbind_to (count, Qnil);
13653 RESUME_POLLING;
13654 }
13655
13656
13657 /* Redisplay, but leave alone any recent echo area message unless
13658 another message has been requested in its place.
13659
13660 This is useful in situations where you need to redisplay but no
13661 user action has occurred, making it inappropriate for the message
13662 area to be cleared. See tracking_off and
13663 wait_reading_process_output for examples of these situations.
13664
13665 FROM_WHERE is an integer saying from where this function was
13666 called. This is useful for debugging. */
13667
13668 void
13669 redisplay_preserve_echo_area (int from_where)
13670 {
13671 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13672
13673 if (!NILP (echo_area_buffer[1]))
13674 {
13675 /* We have a previously displayed message, but no current
13676 message. Redisplay the previous message. */
13677 display_last_displayed_message_p = 1;
13678 redisplay_internal ();
13679 display_last_displayed_message_p = 0;
13680 }
13681 else
13682 redisplay_internal ();
13683
13684 flush_frame (SELECTED_FRAME ());
13685 }
13686
13687
13688 /* Function registered with record_unwind_protect in redisplay_internal. */
13689
13690 static void
13691 unwind_redisplay (void)
13692 {
13693 redisplaying_p = 0;
13694 }
13695
13696
13697 /* Mark the display of leaf window W as accurate or inaccurate.
13698 If ACCURATE_P is non-zero mark display of W as accurate. If
13699 ACCURATE_P is zero, arrange for W to be redisplayed the next
13700 time redisplay_internal is called. */
13701
13702 static void
13703 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13704 {
13705 struct buffer *b = XBUFFER (w->contents);
13706
13707 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13708 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13709 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13710
13711 if (accurate_p)
13712 {
13713 b->clip_changed = false;
13714 b->prevent_redisplay_optimizations_p = false;
13715 eassert (buffer_window_count (b) > 0);
13716 /* Resetting b->text->redisplay is problematic!
13717 In order to make it safer to do it here, redisplay_internal must
13718 have copied all b->text->redisplay to their respective windows. */
13719 b->text->redisplay = false;
13720
13721 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13722 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13723 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13724 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13725
13726 w->current_matrix->buffer = b;
13727 w->current_matrix->begv = BUF_BEGV (b);
13728 w->current_matrix->zv = BUF_ZV (b);
13729
13730 w->last_cursor_vpos = w->cursor.vpos;
13731 w->last_cursor_off_p = w->cursor_off_p;
13732
13733 if (w == XWINDOW (selected_window))
13734 w->last_point = BUF_PT (b);
13735 else
13736 w->last_point = marker_position (w->pointm);
13737
13738 w->window_end_valid = true;
13739 w->update_mode_line = false;
13740 }
13741
13742 w->redisplay = !accurate_p;
13743 }
13744
13745
13746 /* Mark the display of windows in the window tree rooted at WINDOW as
13747 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13748 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13749 be redisplayed the next time redisplay_internal is called. */
13750
13751 void
13752 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13753 {
13754 struct window *w;
13755
13756 for (; !NILP (window); window = w->next)
13757 {
13758 w = XWINDOW (window);
13759 if (WINDOWP (w->contents))
13760 mark_window_display_accurate (w->contents, accurate_p);
13761 else
13762 mark_window_display_accurate_1 (w, accurate_p);
13763 }
13764
13765 if (accurate_p)
13766 update_overlay_arrows (1);
13767 else
13768 /* Force a thorough redisplay the next time by setting
13769 last_arrow_position and last_arrow_string to t, which is
13770 unequal to any useful value of Voverlay_arrow_... */
13771 update_overlay_arrows (-1);
13772 }
13773
13774
13775 /* Return value in display table DP (Lisp_Char_Table *) for character
13776 C. Since a display table doesn't have any parent, we don't have to
13777 follow parent. Do not call this function directly but use the
13778 macro DISP_CHAR_VECTOR. */
13779
13780 Lisp_Object
13781 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13782 {
13783 Lisp_Object val;
13784
13785 if (ASCII_CHAR_P (c))
13786 {
13787 val = dp->ascii;
13788 if (SUB_CHAR_TABLE_P (val))
13789 val = XSUB_CHAR_TABLE (val)->contents[c];
13790 }
13791 else
13792 {
13793 Lisp_Object table;
13794
13795 XSETCHAR_TABLE (table, dp);
13796 val = char_table_ref (table, c);
13797 }
13798 if (NILP (val))
13799 val = dp->defalt;
13800 return val;
13801 }
13802
13803
13804 \f
13805 /***********************************************************************
13806 Window Redisplay
13807 ***********************************************************************/
13808
13809 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13810
13811 static void
13812 redisplay_windows (Lisp_Object window)
13813 {
13814 while (!NILP (window))
13815 {
13816 struct window *w = XWINDOW (window);
13817
13818 if (WINDOWP (w->contents))
13819 redisplay_windows (w->contents);
13820 else if (BUFFERP (w->contents))
13821 {
13822 displayed_buffer = XBUFFER (w->contents);
13823 /* Use list_of_error, not Qerror, so that
13824 we catch only errors and don't run the debugger. */
13825 internal_condition_case_1 (redisplay_window_0, window,
13826 list_of_error,
13827 redisplay_window_error);
13828 }
13829
13830 window = w->next;
13831 }
13832 }
13833
13834 static Lisp_Object
13835 redisplay_window_error (Lisp_Object ignore)
13836 {
13837 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13838 return Qnil;
13839 }
13840
13841 static Lisp_Object
13842 redisplay_window_0 (Lisp_Object window)
13843 {
13844 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13845 redisplay_window (window, false);
13846 return Qnil;
13847 }
13848
13849 static Lisp_Object
13850 redisplay_window_1 (Lisp_Object window)
13851 {
13852 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13853 redisplay_window (window, true);
13854 return Qnil;
13855 }
13856 \f
13857
13858 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13859 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13860 which positions recorded in ROW differ from current buffer
13861 positions.
13862
13863 Return 0 if cursor is not on this row, 1 otherwise. */
13864
13865 static int
13866 set_cursor_from_row (struct window *w, struct glyph_row *row,
13867 struct glyph_matrix *matrix,
13868 ptrdiff_t delta, ptrdiff_t delta_bytes,
13869 int dy, int dvpos)
13870 {
13871 struct glyph *glyph = row->glyphs[TEXT_AREA];
13872 struct glyph *end = glyph + row->used[TEXT_AREA];
13873 struct glyph *cursor = NULL;
13874 /* The last known character position in row. */
13875 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13876 int x = row->x;
13877 ptrdiff_t pt_old = PT - delta;
13878 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13879 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13880 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13881 /* A glyph beyond the edge of TEXT_AREA which we should never
13882 touch. */
13883 struct glyph *glyphs_end = end;
13884 /* Non-zero means we've found a match for cursor position, but that
13885 glyph has the avoid_cursor_p flag set. */
13886 int match_with_avoid_cursor = 0;
13887 /* Non-zero means we've seen at least one glyph that came from a
13888 display string. */
13889 int string_seen = 0;
13890 /* Largest and smallest buffer positions seen so far during scan of
13891 glyph row. */
13892 ptrdiff_t bpos_max = pos_before;
13893 ptrdiff_t bpos_min = pos_after;
13894 /* Last buffer position covered by an overlay string with an integer
13895 `cursor' property. */
13896 ptrdiff_t bpos_covered = 0;
13897 /* Non-zero means the display string on which to display the cursor
13898 comes from a text property, not from an overlay. */
13899 int string_from_text_prop = 0;
13900
13901 /* Don't even try doing anything if called for a mode-line or
13902 header-line row, since the rest of the code isn't prepared to
13903 deal with such calamities. */
13904 eassert (!row->mode_line_p);
13905 if (row->mode_line_p)
13906 return 0;
13907
13908 /* Skip over glyphs not having an object at the start and the end of
13909 the row. These are special glyphs like truncation marks on
13910 terminal frames. */
13911 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13912 {
13913 if (!row->reversed_p)
13914 {
13915 while (glyph < end
13916 && INTEGERP (glyph->object)
13917 && glyph->charpos < 0)
13918 {
13919 x += glyph->pixel_width;
13920 ++glyph;
13921 }
13922 while (end > glyph
13923 && INTEGERP ((end - 1)->object)
13924 /* CHARPOS is zero for blanks and stretch glyphs
13925 inserted by extend_face_to_end_of_line. */
13926 && (end - 1)->charpos <= 0)
13927 --end;
13928 glyph_before = glyph - 1;
13929 glyph_after = end;
13930 }
13931 else
13932 {
13933 struct glyph *g;
13934
13935 /* If the glyph row is reversed, we need to process it from back
13936 to front, so swap the edge pointers. */
13937 glyphs_end = end = glyph - 1;
13938 glyph += row->used[TEXT_AREA] - 1;
13939
13940 while (glyph > end + 1
13941 && INTEGERP (glyph->object)
13942 && glyph->charpos < 0)
13943 {
13944 --glyph;
13945 x -= glyph->pixel_width;
13946 }
13947 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13948 --glyph;
13949 /* By default, in reversed rows we put the cursor on the
13950 rightmost (first in the reading order) glyph. */
13951 for (g = end + 1; g < glyph; g++)
13952 x += g->pixel_width;
13953 while (end < glyph
13954 && INTEGERP ((end + 1)->object)
13955 && (end + 1)->charpos <= 0)
13956 ++end;
13957 glyph_before = glyph + 1;
13958 glyph_after = end;
13959 }
13960 }
13961 else if (row->reversed_p)
13962 {
13963 /* In R2L rows that don't display text, put the cursor on the
13964 rightmost glyph. Case in point: an empty last line that is
13965 part of an R2L paragraph. */
13966 cursor = end - 1;
13967 /* Avoid placing the cursor on the last glyph of the row, where
13968 on terminal frames we hold the vertical border between
13969 adjacent windows. */
13970 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13971 && !WINDOW_RIGHTMOST_P (w)
13972 && cursor == row->glyphs[LAST_AREA] - 1)
13973 cursor--;
13974 x = -1; /* will be computed below, at label compute_x */
13975 }
13976
13977 /* Step 1: Try to find the glyph whose character position
13978 corresponds to point. If that's not possible, find 2 glyphs
13979 whose character positions are the closest to point, one before
13980 point, the other after it. */
13981 if (!row->reversed_p)
13982 while (/* not marched to end of glyph row */
13983 glyph < end
13984 /* glyph was not inserted by redisplay for internal purposes */
13985 && !INTEGERP (glyph->object))
13986 {
13987 if (BUFFERP (glyph->object))
13988 {
13989 ptrdiff_t dpos = glyph->charpos - pt_old;
13990
13991 if (glyph->charpos > bpos_max)
13992 bpos_max = glyph->charpos;
13993 if (glyph->charpos < bpos_min)
13994 bpos_min = glyph->charpos;
13995 if (!glyph->avoid_cursor_p)
13996 {
13997 /* If we hit point, we've found the glyph on which to
13998 display the cursor. */
13999 if (dpos == 0)
14000 {
14001 match_with_avoid_cursor = 0;
14002 break;
14003 }
14004 /* See if we've found a better approximation to
14005 POS_BEFORE or to POS_AFTER. */
14006 if (0 > dpos && dpos > pos_before - pt_old)
14007 {
14008 pos_before = glyph->charpos;
14009 glyph_before = glyph;
14010 }
14011 else if (0 < dpos && dpos < pos_after - pt_old)
14012 {
14013 pos_after = glyph->charpos;
14014 glyph_after = glyph;
14015 }
14016 }
14017 else if (dpos == 0)
14018 match_with_avoid_cursor = 1;
14019 }
14020 else if (STRINGP (glyph->object))
14021 {
14022 Lisp_Object chprop;
14023 ptrdiff_t glyph_pos = glyph->charpos;
14024
14025 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14026 glyph->object);
14027 if (!NILP (chprop))
14028 {
14029 /* If the string came from a `display' text property,
14030 look up the buffer position of that property and
14031 use that position to update bpos_max, as if we
14032 actually saw such a position in one of the row's
14033 glyphs. This helps with supporting integer values
14034 of `cursor' property on the display string in
14035 situations where most or all of the row's buffer
14036 text is completely covered by display properties,
14037 so that no glyph with valid buffer positions is
14038 ever seen in the row. */
14039 ptrdiff_t prop_pos =
14040 string_buffer_position_lim (glyph->object, pos_before,
14041 pos_after, 0);
14042
14043 if (prop_pos >= pos_before)
14044 bpos_max = prop_pos - 1;
14045 }
14046 if (INTEGERP (chprop))
14047 {
14048 bpos_covered = bpos_max + XINT (chprop);
14049 /* If the `cursor' property covers buffer positions up
14050 to and including point, we should display cursor on
14051 this glyph. Note that, if a `cursor' property on one
14052 of the string's characters has an integer value, we
14053 will break out of the loop below _before_ we get to
14054 the position match above. IOW, integer values of
14055 the `cursor' property override the "exact match for
14056 point" strategy of positioning the cursor. */
14057 /* Implementation note: bpos_max == pt_old when, e.g.,
14058 we are in an empty line, where bpos_max is set to
14059 MATRIX_ROW_START_CHARPOS, see above. */
14060 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14061 {
14062 cursor = glyph;
14063 break;
14064 }
14065 }
14066
14067 string_seen = 1;
14068 }
14069 x += glyph->pixel_width;
14070 ++glyph;
14071 }
14072 else if (glyph > end) /* row is reversed */
14073 while (!INTEGERP (glyph->object))
14074 {
14075 if (BUFFERP (glyph->object))
14076 {
14077 ptrdiff_t dpos = glyph->charpos - pt_old;
14078
14079 if (glyph->charpos > bpos_max)
14080 bpos_max = glyph->charpos;
14081 if (glyph->charpos < bpos_min)
14082 bpos_min = glyph->charpos;
14083 if (!glyph->avoid_cursor_p)
14084 {
14085 if (dpos == 0)
14086 {
14087 match_with_avoid_cursor = 0;
14088 break;
14089 }
14090 if (0 > dpos && dpos > pos_before - pt_old)
14091 {
14092 pos_before = glyph->charpos;
14093 glyph_before = glyph;
14094 }
14095 else if (0 < dpos && dpos < pos_after - pt_old)
14096 {
14097 pos_after = glyph->charpos;
14098 glyph_after = glyph;
14099 }
14100 }
14101 else if (dpos == 0)
14102 match_with_avoid_cursor = 1;
14103 }
14104 else if (STRINGP (glyph->object))
14105 {
14106 Lisp_Object chprop;
14107 ptrdiff_t glyph_pos = glyph->charpos;
14108
14109 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14110 glyph->object);
14111 if (!NILP (chprop))
14112 {
14113 ptrdiff_t prop_pos =
14114 string_buffer_position_lim (glyph->object, pos_before,
14115 pos_after, 0);
14116
14117 if (prop_pos >= pos_before)
14118 bpos_max = prop_pos - 1;
14119 }
14120 if (INTEGERP (chprop))
14121 {
14122 bpos_covered = bpos_max + XINT (chprop);
14123 /* If the `cursor' property covers buffer positions up
14124 to and including point, we should display cursor on
14125 this glyph. */
14126 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14127 {
14128 cursor = glyph;
14129 break;
14130 }
14131 }
14132 string_seen = 1;
14133 }
14134 --glyph;
14135 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14136 {
14137 x--; /* can't use any pixel_width */
14138 break;
14139 }
14140 x -= glyph->pixel_width;
14141 }
14142
14143 /* Step 2: If we didn't find an exact match for point, we need to
14144 look for a proper place to put the cursor among glyphs between
14145 GLYPH_BEFORE and GLYPH_AFTER. */
14146 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14147 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14148 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14149 {
14150 /* An empty line has a single glyph whose OBJECT is zero and
14151 whose CHARPOS is the position of a newline on that line.
14152 Note that on a TTY, there are more glyphs after that, which
14153 were produced by extend_face_to_end_of_line, but their
14154 CHARPOS is zero or negative. */
14155 int empty_line_p =
14156 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14157 && INTEGERP (glyph->object) && glyph->charpos > 0
14158 /* On a TTY, continued and truncated rows also have a glyph at
14159 their end whose OBJECT is zero and whose CHARPOS is
14160 positive (the continuation and truncation glyphs), but such
14161 rows are obviously not "empty". */
14162 && !(row->continued_p || row->truncated_on_right_p);
14163
14164 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14165 {
14166 ptrdiff_t ellipsis_pos;
14167
14168 /* Scan back over the ellipsis glyphs. */
14169 if (!row->reversed_p)
14170 {
14171 ellipsis_pos = (glyph - 1)->charpos;
14172 while (glyph > row->glyphs[TEXT_AREA]
14173 && (glyph - 1)->charpos == ellipsis_pos)
14174 glyph--, x -= glyph->pixel_width;
14175 /* That loop always goes one position too far, including
14176 the glyph before the ellipsis. So scan forward over
14177 that one. */
14178 x += glyph->pixel_width;
14179 glyph++;
14180 }
14181 else /* row is reversed */
14182 {
14183 ellipsis_pos = (glyph + 1)->charpos;
14184 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14185 && (glyph + 1)->charpos == ellipsis_pos)
14186 glyph++, x += glyph->pixel_width;
14187 x -= glyph->pixel_width;
14188 glyph--;
14189 }
14190 }
14191 else if (match_with_avoid_cursor)
14192 {
14193 cursor = glyph_after;
14194 x = -1;
14195 }
14196 else if (string_seen)
14197 {
14198 int incr = row->reversed_p ? -1 : +1;
14199
14200 /* Need to find the glyph that came out of a string which is
14201 present at point. That glyph is somewhere between
14202 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14203 positioned between POS_BEFORE and POS_AFTER in the
14204 buffer. */
14205 struct glyph *start, *stop;
14206 ptrdiff_t pos = pos_before;
14207
14208 x = -1;
14209
14210 /* If the row ends in a newline from a display string,
14211 reordering could have moved the glyphs belonging to the
14212 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14213 in this case we extend the search to the last glyph in
14214 the row that was not inserted by redisplay. */
14215 if (row->ends_in_newline_from_string_p)
14216 {
14217 glyph_after = end;
14218 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14219 }
14220
14221 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14222 correspond to POS_BEFORE and POS_AFTER, respectively. We
14223 need START and STOP in the order that corresponds to the
14224 row's direction as given by its reversed_p flag. If the
14225 directionality of characters between POS_BEFORE and
14226 POS_AFTER is the opposite of the row's base direction,
14227 these characters will have been reordered for display,
14228 and we need to reverse START and STOP. */
14229 if (!row->reversed_p)
14230 {
14231 start = min (glyph_before, glyph_after);
14232 stop = max (glyph_before, glyph_after);
14233 }
14234 else
14235 {
14236 start = max (glyph_before, glyph_after);
14237 stop = min (glyph_before, glyph_after);
14238 }
14239 for (glyph = start + incr;
14240 row->reversed_p ? glyph > stop : glyph < stop; )
14241 {
14242
14243 /* Any glyphs that come from the buffer are here because
14244 of bidi reordering. Skip them, and only pay
14245 attention to glyphs that came from some string. */
14246 if (STRINGP (glyph->object))
14247 {
14248 Lisp_Object str;
14249 ptrdiff_t tem;
14250 /* If the display property covers the newline, we
14251 need to search for it one position farther. */
14252 ptrdiff_t lim = pos_after
14253 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14254
14255 string_from_text_prop = 0;
14256 str = glyph->object;
14257 tem = string_buffer_position_lim (str, pos, lim, 0);
14258 if (tem == 0 /* from overlay */
14259 || pos <= tem)
14260 {
14261 /* If the string from which this glyph came is
14262 found in the buffer at point, or at position
14263 that is closer to point than pos_after, then
14264 we've found the glyph we've been looking for.
14265 If it comes from an overlay (tem == 0), and
14266 it has the `cursor' property on one of its
14267 glyphs, record that glyph as a candidate for
14268 displaying the cursor. (As in the
14269 unidirectional version, we will display the
14270 cursor on the last candidate we find.) */
14271 if (tem == 0
14272 || tem == pt_old
14273 || (tem - pt_old > 0 && tem < pos_after))
14274 {
14275 /* The glyphs from this string could have
14276 been reordered. Find the one with the
14277 smallest string position. Or there could
14278 be a character in the string with the
14279 `cursor' property, which means display
14280 cursor on that character's glyph. */
14281 ptrdiff_t strpos = glyph->charpos;
14282
14283 if (tem)
14284 {
14285 cursor = glyph;
14286 string_from_text_prop = 1;
14287 }
14288 for ( ;
14289 (row->reversed_p ? glyph > stop : glyph < stop)
14290 && EQ (glyph->object, str);
14291 glyph += incr)
14292 {
14293 Lisp_Object cprop;
14294 ptrdiff_t gpos = glyph->charpos;
14295
14296 cprop = Fget_char_property (make_number (gpos),
14297 Qcursor,
14298 glyph->object);
14299 if (!NILP (cprop))
14300 {
14301 cursor = glyph;
14302 break;
14303 }
14304 if (tem && glyph->charpos < strpos)
14305 {
14306 strpos = glyph->charpos;
14307 cursor = glyph;
14308 }
14309 }
14310
14311 if (tem == pt_old
14312 || (tem - pt_old > 0 && tem < pos_after))
14313 goto compute_x;
14314 }
14315 if (tem)
14316 pos = tem + 1; /* don't find previous instances */
14317 }
14318 /* This string is not what we want; skip all of the
14319 glyphs that came from it. */
14320 while ((row->reversed_p ? glyph > stop : glyph < stop)
14321 && EQ (glyph->object, str))
14322 glyph += incr;
14323 }
14324 else
14325 glyph += incr;
14326 }
14327
14328 /* If we reached the end of the line, and END was from a string,
14329 the cursor is not on this line. */
14330 if (cursor == NULL
14331 && (row->reversed_p ? glyph <= end : glyph >= end)
14332 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14333 && STRINGP (end->object)
14334 && row->continued_p)
14335 return 0;
14336 }
14337 /* A truncated row may not include PT among its character positions.
14338 Setting the cursor inside the scroll margin will trigger
14339 recalculation of hscroll in hscroll_window_tree. But if a
14340 display string covers point, defer to the string-handling
14341 code below to figure this out. */
14342 else if (row->truncated_on_left_p && pt_old < bpos_min)
14343 {
14344 cursor = glyph_before;
14345 x = -1;
14346 }
14347 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14348 /* Zero-width characters produce no glyphs. */
14349 || (!empty_line_p
14350 && (row->reversed_p
14351 ? glyph_after > glyphs_end
14352 : glyph_after < glyphs_end)))
14353 {
14354 cursor = glyph_after;
14355 x = -1;
14356 }
14357 }
14358
14359 compute_x:
14360 if (cursor != NULL)
14361 glyph = cursor;
14362 else if (glyph == glyphs_end
14363 && pos_before == pos_after
14364 && STRINGP ((row->reversed_p
14365 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14366 : row->glyphs[TEXT_AREA])->object))
14367 {
14368 /* If all the glyphs of this row came from strings, put the
14369 cursor on the first glyph of the row. This avoids having the
14370 cursor outside of the text area in this very rare and hard
14371 use case. */
14372 glyph =
14373 row->reversed_p
14374 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14375 : row->glyphs[TEXT_AREA];
14376 }
14377 if (x < 0)
14378 {
14379 struct glyph *g;
14380
14381 /* Need to compute x that corresponds to GLYPH. */
14382 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14383 {
14384 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14385 emacs_abort ();
14386 x += g->pixel_width;
14387 }
14388 }
14389
14390 /* ROW could be part of a continued line, which, under bidi
14391 reordering, might have other rows whose start and end charpos
14392 occlude point. Only set w->cursor if we found a better
14393 approximation to the cursor position than we have from previously
14394 examined candidate rows belonging to the same continued line. */
14395 if (/* We already have a candidate row. */
14396 w->cursor.vpos >= 0
14397 /* That candidate is not the row we are processing. */
14398 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14399 /* Make sure cursor.vpos specifies a row whose start and end
14400 charpos occlude point, and it is valid candidate for being a
14401 cursor-row. This is because some callers of this function
14402 leave cursor.vpos at the row where the cursor was displayed
14403 during the last redisplay cycle. */
14404 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14405 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14406 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14407 {
14408 struct glyph *g1
14409 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14410
14411 /* Don't consider glyphs that are outside TEXT_AREA. */
14412 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14413 return 0;
14414 /* Keep the candidate whose buffer position is the closest to
14415 point or has the `cursor' property. */
14416 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14417 w->cursor.hpos >= 0
14418 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14419 && ((BUFFERP (g1->object)
14420 && (g1->charpos == pt_old /* An exact match always wins. */
14421 || (BUFFERP (glyph->object)
14422 && eabs (g1->charpos - pt_old)
14423 < eabs (glyph->charpos - pt_old))))
14424 /* Previous candidate is a glyph from a string that has
14425 a non-nil `cursor' property. */
14426 || (STRINGP (g1->object)
14427 && (!NILP (Fget_char_property (make_number (g1->charpos),
14428 Qcursor, g1->object))
14429 /* Previous candidate is from the same display
14430 string as this one, and the display string
14431 came from a text property. */
14432 || (EQ (g1->object, glyph->object)
14433 && string_from_text_prop)
14434 /* this candidate is from newline and its
14435 position is not an exact match */
14436 || (INTEGERP (glyph->object)
14437 && glyph->charpos != pt_old)))))
14438 return 0;
14439 /* If this candidate gives an exact match, use that. */
14440 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14441 /* If this candidate is a glyph created for the
14442 terminating newline of a line, and point is on that
14443 newline, it wins because it's an exact match. */
14444 || (!row->continued_p
14445 && INTEGERP (glyph->object)
14446 && glyph->charpos == 0
14447 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14448 /* Otherwise, keep the candidate that comes from a row
14449 spanning less buffer positions. This may win when one or
14450 both candidate positions are on glyphs that came from
14451 display strings, for which we cannot compare buffer
14452 positions. */
14453 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14454 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14455 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14456 return 0;
14457 }
14458 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14459 w->cursor.x = x;
14460 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14461 w->cursor.y = row->y + dy;
14462
14463 if (w == XWINDOW (selected_window))
14464 {
14465 if (!row->continued_p
14466 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14467 && row->x == 0)
14468 {
14469 this_line_buffer = XBUFFER (w->contents);
14470
14471 CHARPOS (this_line_start_pos)
14472 = MATRIX_ROW_START_CHARPOS (row) + delta;
14473 BYTEPOS (this_line_start_pos)
14474 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14475
14476 CHARPOS (this_line_end_pos)
14477 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14478 BYTEPOS (this_line_end_pos)
14479 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14480
14481 this_line_y = w->cursor.y;
14482 this_line_pixel_height = row->height;
14483 this_line_vpos = w->cursor.vpos;
14484 this_line_start_x = row->x;
14485 }
14486 else
14487 CHARPOS (this_line_start_pos) = 0;
14488 }
14489
14490 return 1;
14491 }
14492
14493
14494 /* Run window scroll functions, if any, for WINDOW with new window
14495 start STARTP. Sets the window start of WINDOW to that position.
14496
14497 We assume that the window's buffer is really current. */
14498
14499 static struct text_pos
14500 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14501 {
14502 struct window *w = XWINDOW (window);
14503 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14504
14505 eassert (current_buffer == XBUFFER (w->contents));
14506
14507 if (!NILP (Vwindow_scroll_functions))
14508 {
14509 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14510 make_number (CHARPOS (startp)));
14511 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14512 /* In case the hook functions switch buffers. */
14513 set_buffer_internal (XBUFFER (w->contents));
14514 }
14515
14516 return startp;
14517 }
14518
14519
14520 /* Make sure the line containing the cursor is fully visible.
14521 A value of 1 means there is nothing to be done.
14522 (Either the line is fully visible, or it cannot be made so,
14523 or we cannot tell.)
14524
14525 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14526 is higher than window.
14527
14528 A value of 0 means the caller should do scrolling
14529 as if point had gone off the screen. */
14530
14531 static int
14532 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14533 {
14534 struct glyph_matrix *matrix;
14535 struct glyph_row *row;
14536 int window_height;
14537
14538 if (!make_cursor_line_fully_visible_p)
14539 return 1;
14540
14541 /* It's not always possible to find the cursor, e.g, when a window
14542 is full of overlay strings. Don't do anything in that case. */
14543 if (w->cursor.vpos < 0)
14544 return 1;
14545
14546 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14547 row = MATRIX_ROW (matrix, w->cursor.vpos);
14548
14549 /* If the cursor row is not partially visible, there's nothing to do. */
14550 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14551 return 1;
14552
14553 /* If the row the cursor is in is taller than the window's height,
14554 it's not clear what to do, so do nothing. */
14555 window_height = window_box_height (w);
14556 if (row->height >= window_height)
14557 {
14558 if (!force_p || MINI_WINDOW_P (w)
14559 || w->vscroll || w->cursor.vpos == 0)
14560 return 1;
14561 }
14562 return 0;
14563 }
14564
14565
14566 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14567 non-zero means only WINDOW is redisplayed in redisplay_internal.
14568 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14569 in redisplay_window to bring a partially visible line into view in
14570 the case that only the cursor has moved.
14571
14572 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14573 last screen line's vertical height extends past the end of the screen.
14574
14575 Value is
14576
14577 1 if scrolling succeeded
14578
14579 0 if scrolling didn't find point.
14580
14581 -1 if new fonts have been loaded so that we must interrupt
14582 redisplay, adjust glyph matrices, and try again. */
14583
14584 enum
14585 {
14586 SCROLLING_SUCCESS,
14587 SCROLLING_FAILED,
14588 SCROLLING_NEED_LARGER_MATRICES
14589 };
14590
14591 /* If scroll-conservatively is more than this, never recenter.
14592
14593 If you change this, don't forget to update the doc string of
14594 `scroll-conservatively' and the Emacs manual. */
14595 #define SCROLL_LIMIT 100
14596
14597 static int
14598 try_scrolling (Lisp_Object window, int just_this_one_p,
14599 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14600 int temp_scroll_step, int last_line_misfit)
14601 {
14602 struct window *w = XWINDOW (window);
14603 struct frame *f = XFRAME (w->frame);
14604 struct text_pos pos, startp;
14605 struct it it;
14606 int this_scroll_margin, scroll_max, rc, height;
14607 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14608 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14609 Lisp_Object aggressive;
14610 /* We will never try scrolling more than this number of lines. */
14611 int scroll_limit = SCROLL_LIMIT;
14612 int frame_line_height = default_line_pixel_height (w);
14613 int window_total_lines
14614 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14615
14616 #ifdef GLYPH_DEBUG
14617 debug_method_add (w, "try_scrolling");
14618 #endif
14619
14620 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14621
14622 /* Compute scroll margin height in pixels. We scroll when point is
14623 within this distance from the top or bottom of the window. */
14624 if (scroll_margin > 0)
14625 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14626 * frame_line_height;
14627 else
14628 this_scroll_margin = 0;
14629
14630 /* Force arg_scroll_conservatively to have a reasonable value, to
14631 avoid scrolling too far away with slow move_it_* functions. Note
14632 that the user can supply scroll-conservatively equal to
14633 `most-positive-fixnum', which can be larger than INT_MAX. */
14634 if (arg_scroll_conservatively > scroll_limit)
14635 {
14636 arg_scroll_conservatively = scroll_limit + 1;
14637 scroll_max = scroll_limit * frame_line_height;
14638 }
14639 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14640 /* Compute how much we should try to scroll maximally to bring
14641 point into view. */
14642 scroll_max = (max (scroll_step,
14643 max (arg_scroll_conservatively, temp_scroll_step))
14644 * frame_line_height);
14645 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14646 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14647 /* We're trying to scroll because of aggressive scrolling but no
14648 scroll_step is set. Choose an arbitrary one. */
14649 scroll_max = 10 * frame_line_height;
14650 else
14651 scroll_max = 0;
14652
14653 too_near_end:
14654
14655 /* Decide whether to scroll down. */
14656 if (PT > CHARPOS (startp))
14657 {
14658 int scroll_margin_y;
14659
14660 /* Compute the pixel ypos of the scroll margin, then move IT to
14661 either that ypos or PT, whichever comes first. */
14662 start_display (&it, w, startp);
14663 scroll_margin_y = it.last_visible_y - this_scroll_margin
14664 - frame_line_height * extra_scroll_margin_lines;
14665 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14666 (MOVE_TO_POS | MOVE_TO_Y));
14667
14668 if (PT > CHARPOS (it.current.pos))
14669 {
14670 int y0 = line_bottom_y (&it);
14671 /* Compute how many pixels below window bottom to stop searching
14672 for PT. This avoids costly search for PT that is far away if
14673 the user limited scrolling by a small number of lines, but
14674 always finds PT if scroll_conservatively is set to a large
14675 number, such as most-positive-fixnum. */
14676 int slack = max (scroll_max, 10 * frame_line_height);
14677 int y_to_move = it.last_visible_y + slack;
14678
14679 /* Compute the distance from the scroll margin to PT or to
14680 the scroll limit, whichever comes first. This should
14681 include the height of the cursor line, to make that line
14682 fully visible. */
14683 move_it_to (&it, PT, -1, y_to_move,
14684 -1, MOVE_TO_POS | MOVE_TO_Y);
14685 dy = line_bottom_y (&it) - y0;
14686
14687 if (dy > scroll_max)
14688 return SCROLLING_FAILED;
14689
14690 if (dy > 0)
14691 scroll_down_p = 1;
14692 }
14693 }
14694
14695 if (scroll_down_p)
14696 {
14697 /* Point is in or below the bottom scroll margin, so move the
14698 window start down. If scrolling conservatively, move it just
14699 enough down to make point visible. If scroll_step is set,
14700 move it down by scroll_step. */
14701 if (arg_scroll_conservatively)
14702 amount_to_scroll
14703 = min (max (dy, frame_line_height),
14704 frame_line_height * arg_scroll_conservatively);
14705 else if (scroll_step || temp_scroll_step)
14706 amount_to_scroll = scroll_max;
14707 else
14708 {
14709 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14710 height = WINDOW_BOX_TEXT_HEIGHT (w);
14711 if (NUMBERP (aggressive))
14712 {
14713 double float_amount = XFLOATINT (aggressive) * height;
14714 int aggressive_scroll = float_amount;
14715 if (aggressive_scroll == 0 && float_amount > 0)
14716 aggressive_scroll = 1;
14717 /* Don't let point enter the scroll margin near top of
14718 the window. This could happen if the value of
14719 scroll_up_aggressively is too large and there are
14720 non-zero margins, because scroll_up_aggressively
14721 means put point that fraction of window height
14722 _from_the_bottom_margin_. */
14723 if (aggressive_scroll + 2*this_scroll_margin > height)
14724 aggressive_scroll = height - 2*this_scroll_margin;
14725 amount_to_scroll = dy + aggressive_scroll;
14726 }
14727 }
14728
14729 if (amount_to_scroll <= 0)
14730 return SCROLLING_FAILED;
14731
14732 start_display (&it, w, startp);
14733 if (arg_scroll_conservatively <= scroll_limit)
14734 move_it_vertically (&it, amount_to_scroll);
14735 else
14736 {
14737 /* Extra precision for users who set scroll-conservatively
14738 to a large number: make sure the amount we scroll
14739 the window start is never less than amount_to_scroll,
14740 which was computed as distance from window bottom to
14741 point. This matters when lines at window top and lines
14742 below window bottom have different height. */
14743 struct it it1;
14744 void *it1data = NULL;
14745 /* We use a temporary it1 because line_bottom_y can modify
14746 its argument, if it moves one line down; see there. */
14747 int start_y;
14748
14749 SAVE_IT (it1, it, it1data);
14750 start_y = line_bottom_y (&it1);
14751 do {
14752 RESTORE_IT (&it, &it, it1data);
14753 move_it_by_lines (&it, 1);
14754 SAVE_IT (it1, it, it1data);
14755 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14756 }
14757
14758 /* If STARTP is unchanged, move it down another screen line. */
14759 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14760 move_it_by_lines (&it, 1);
14761 startp = it.current.pos;
14762 }
14763 else
14764 {
14765 struct text_pos scroll_margin_pos = startp;
14766 int y_offset = 0;
14767
14768 /* See if point is inside the scroll margin at the top of the
14769 window. */
14770 if (this_scroll_margin)
14771 {
14772 int y_start;
14773
14774 start_display (&it, w, startp);
14775 y_start = it.current_y;
14776 move_it_vertically (&it, this_scroll_margin);
14777 scroll_margin_pos = it.current.pos;
14778 /* If we didn't move enough before hitting ZV, request
14779 additional amount of scroll, to move point out of the
14780 scroll margin. */
14781 if (IT_CHARPOS (it) == ZV
14782 && it.current_y - y_start < this_scroll_margin)
14783 y_offset = this_scroll_margin - (it.current_y - y_start);
14784 }
14785
14786 if (PT < CHARPOS (scroll_margin_pos))
14787 {
14788 /* Point is in the scroll margin at the top of the window or
14789 above what is displayed in the window. */
14790 int y0, y_to_move;
14791
14792 /* Compute the vertical distance from PT to the scroll
14793 margin position. Move as far as scroll_max allows, or
14794 one screenful, or 10 screen lines, whichever is largest.
14795 Give up if distance is greater than scroll_max or if we
14796 didn't reach the scroll margin position. */
14797 SET_TEXT_POS (pos, PT, PT_BYTE);
14798 start_display (&it, w, pos);
14799 y0 = it.current_y;
14800 y_to_move = max (it.last_visible_y,
14801 max (scroll_max, 10 * frame_line_height));
14802 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14803 y_to_move, -1,
14804 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14805 dy = it.current_y - y0;
14806 if (dy > scroll_max
14807 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14808 return SCROLLING_FAILED;
14809
14810 /* Additional scroll for when ZV was too close to point. */
14811 dy += y_offset;
14812
14813 /* Compute new window start. */
14814 start_display (&it, w, startp);
14815
14816 if (arg_scroll_conservatively)
14817 amount_to_scroll = max (dy, frame_line_height *
14818 max (scroll_step, temp_scroll_step));
14819 else if (scroll_step || temp_scroll_step)
14820 amount_to_scroll = scroll_max;
14821 else
14822 {
14823 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14824 height = WINDOW_BOX_TEXT_HEIGHT (w);
14825 if (NUMBERP (aggressive))
14826 {
14827 double float_amount = XFLOATINT (aggressive) * height;
14828 int aggressive_scroll = float_amount;
14829 if (aggressive_scroll == 0 && float_amount > 0)
14830 aggressive_scroll = 1;
14831 /* Don't let point enter the scroll margin near
14832 bottom of the window, if the value of
14833 scroll_down_aggressively happens to be too
14834 large. */
14835 if (aggressive_scroll + 2*this_scroll_margin > height)
14836 aggressive_scroll = height - 2*this_scroll_margin;
14837 amount_to_scroll = dy + aggressive_scroll;
14838 }
14839 }
14840
14841 if (amount_to_scroll <= 0)
14842 return SCROLLING_FAILED;
14843
14844 move_it_vertically_backward (&it, amount_to_scroll);
14845 startp = it.current.pos;
14846 }
14847 }
14848
14849 /* Run window scroll functions. */
14850 startp = run_window_scroll_functions (window, startp);
14851
14852 /* Display the window. Give up if new fonts are loaded, or if point
14853 doesn't appear. */
14854 if (!try_window (window, startp, 0))
14855 rc = SCROLLING_NEED_LARGER_MATRICES;
14856 else if (w->cursor.vpos < 0)
14857 {
14858 clear_glyph_matrix (w->desired_matrix);
14859 rc = SCROLLING_FAILED;
14860 }
14861 else
14862 {
14863 /* Maybe forget recorded base line for line number display. */
14864 if (!just_this_one_p
14865 || current_buffer->clip_changed
14866 || BEG_UNCHANGED < CHARPOS (startp))
14867 w->base_line_number = 0;
14868
14869 /* If cursor ends up on a partially visible line,
14870 treat that as being off the bottom of the screen. */
14871 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14872 /* It's possible that the cursor is on the first line of the
14873 buffer, which is partially obscured due to a vscroll
14874 (Bug#7537). In that case, avoid looping forever . */
14875 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14876 {
14877 clear_glyph_matrix (w->desired_matrix);
14878 ++extra_scroll_margin_lines;
14879 goto too_near_end;
14880 }
14881 rc = SCROLLING_SUCCESS;
14882 }
14883
14884 return rc;
14885 }
14886
14887
14888 /* Compute a suitable window start for window W if display of W starts
14889 on a continuation line. Value is non-zero if a new window start
14890 was computed.
14891
14892 The new window start will be computed, based on W's width, starting
14893 from the start of the continued line. It is the start of the
14894 screen line with the minimum distance from the old start W->start. */
14895
14896 static int
14897 compute_window_start_on_continuation_line (struct window *w)
14898 {
14899 struct text_pos pos, start_pos;
14900 int window_start_changed_p = 0;
14901
14902 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14903
14904 /* If window start is on a continuation line... Window start may be
14905 < BEGV in case there's invisible text at the start of the
14906 buffer (M-x rmail, for example). */
14907 if (CHARPOS (start_pos) > BEGV
14908 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14909 {
14910 struct it it;
14911 struct glyph_row *row;
14912
14913 /* Handle the case that the window start is out of range. */
14914 if (CHARPOS (start_pos) < BEGV)
14915 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14916 else if (CHARPOS (start_pos) > ZV)
14917 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14918
14919 /* Find the start of the continued line. This should be fast
14920 because find_newline is fast (newline cache). */
14921 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14922 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14923 row, DEFAULT_FACE_ID);
14924 reseat_at_previous_visible_line_start (&it);
14925
14926 /* If the line start is "too far" away from the window start,
14927 say it takes too much time to compute a new window start. */
14928 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14929 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14930 {
14931 int min_distance, distance;
14932
14933 /* Move forward by display lines to find the new window
14934 start. If window width was enlarged, the new start can
14935 be expected to be > the old start. If window width was
14936 decreased, the new window start will be < the old start.
14937 So, we're looking for the display line start with the
14938 minimum distance from the old window start. */
14939 pos = it.current.pos;
14940 min_distance = INFINITY;
14941 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14942 distance < min_distance)
14943 {
14944 min_distance = distance;
14945 pos = it.current.pos;
14946 if (it.line_wrap == WORD_WRAP)
14947 {
14948 /* Under WORD_WRAP, move_it_by_lines is likely to
14949 overshoot and stop not at the first, but the
14950 second character from the left margin. So in
14951 that case, we need a more tight control on the X
14952 coordinate of the iterator than move_it_by_lines
14953 promises in its contract. The method is to first
14954 go to the last (rightmost) visible character of a
14955 line, then move to the leftmost character on the
14956 next line in a separate call. */
14957 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14958 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14959 move_it_to (&it, ZV, 0,
14960 it.current_y + it.max_ascent + it.max_descent, -1,
14961 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14962 }
14963 else
14964 move_it_by_lines (&it, 1);
14965 }
14966
14967 /* Set the window start there. */
14968 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14969 window_start_changed_p = 1;
14970 }
14971 }
14972
14973 return window_start_changed_p;
14974 }
14975
14976
14977 /* Try cursor movement in case text has not changed in window WINDOW,
14978 with window start STARTP. Value is
14979
14980 CURSOR_MOVEMENT_SUCCESS if successful
14981
14982 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14983
14984 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14985 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14986 we want to scroll as if scroll-step were set to 1. See the code.
14987
14988 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14989 which case we have to abort this redisplay, and adjust matrices
14990 first. */
14991
14992 enum
14993 {
14994 CURSOR_MOVEMENT_SUCCESS,
14995 CURSOR_MOVEMENT_CANNOT_BE_USED,
14996 CURSOR_MOVEMENT_MUST_SCROLL,
14997 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14998 };
14999
15000 static int
15001 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15002 {
15003 struct window *w = XWINDOW (window);
15004 struct frame *f = XFRAME (w->frame);
15005 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15006
15007 #ifdef GLYPH_DEBUG
15008 if (inhibit_try_cursor_movement)
15009 return rc;
15010 #endif
15011
15012 /* Previously, there was a check for Lisp integer in the
15013 if-statement below. Now, this field is converted to
15014 ptrdiff_t, thus zero means invalid position in a buffer. */
15015 eassert (w->last_point > 0);
15016 /* Likewise there was a check whether window_end_vpos is nil or larger
15017 than the window. Now window_end_vpos is int and so never nil, but
15018 let's leave eassert to check whether it fits in the window. */
15019 eassert (w->window_end_vpos < w->current_matrix->nrows);
15020
15021 /* Handle case where text has not changed, only point, and it has
15022 not moved off the frame. */
15023 if (/* Point may be in this window. */
15024 PT >= CHARPOS (startp)
15025 /* Selective display hasn't changed. */
15026 && !current_buffer->clip_changed
15027 /* Function force-mode-line-update is used to force a thorough
15028 redisplay. It sets either windows_or_buffers_changed or
15029 update_mode_lines. So don't take a shortcut here for these
15030 cases. */
15031 && !update_mode_lines
15032 && !windows_or_buffers_changed
15033 && !f->cursor_type_changed
15034 && NILP (Vshow_trailing_whitespace)
15035 /* This code is not used for mini-buffer for the sake of the case
15036 of redisplaying to replace an echo area message; since in
15037 that case the mini-buffer contents per se are usually
15038 unchanged. This code is of no real use in the mini-buffer
15039 since the handling of this_line_start_pos, etc., in redisplay
15040 handles the same cases. */
15041 && !EQ (window, minibuf_window)
15042 && (FRAME_WINDOW_P (f)
15043 || !overlay_arrow_in_current_buffer_p ()))
15044 {
15045 int this_scroll_margin, top_scroll_margin;
15046 struct glyph_row *row = NULL;
15047 int frame_line_height = default_line_pixel_height (w);
15048 int window_total_lines
15049 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15050
15051 #ifdef GLYPH_DEBUG
15052 debug_method_add (w, "cursor movement");
15053 #endif
15054
15055 /* Scroll if point within this distance from the top or bottom
15056 of the window. This is a pixel value. */
15057 if (scroll_margin > 0)
15058 {
15059 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15060 this_scroll_margin *= frame_line_height;
15061 }
15062 else
15063 this_scroll_margin = 0;
15064
15065 top_scroll_margin = this_scroll_margin;
15066 if (WINDOW_WANTS_HEADER_LINE_P (w))
15067 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15068
15069 /* Start with the row the cursor was displayed during the last
15070 not paused redisplay. Give up if that row is not valid. */
15071 if (w->last_cursor_vpos < 0
15072 || w->last_cursor_vpos >= w->current_matrix->nrows)
15073 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15074 else
15075 {
15076 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15077 if (row->mode_line_p)
15078 ++row;
15079 if (!row->enabled_p)
15080 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15081 }
15082
15083 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15084 {
15085 int scroll_p = 0, must_scroll = 0;
15086 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15087
15088 if (PT > w->last_point)
15089 {
15090 /* Point has moved forward. */
15091 while (MATRIX_ROW_END_CHARPOS (row) < PT
15092 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15093 {
15094 eassert (row->enabled_p);
15095 ++row;
15096 }
15097
15098 /* If the end position of a row equals the start
15099 position of the next row, and PT is at that position,
15100 we would rather display cursor in the next line. */
15101 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15102 && MATRIX_ROW_END_CHARPOS (row) == PT
15103 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15104 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15105 && !cursor_row_p (row))
15106 ++row;
15107
15108 /* If within the scroll margin, scroll. Note that
15109 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15110 the next line would be drawn, and that
15111 this_scroll_margin can be zero. */
15112 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15113 || PT > MATRIX_ROW_END_CHARPOS (row)
15114 /* Line is completely visible last line in window
15115 and PT is to be set in the next line. */
15116 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15117 && PT == MATRIX_ROW_END_CHARPOS (row)
15118 && !row->ends_at_zv_p
15119 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15120 scroll_p = 1;
15121 }
15122 else if (PT < w->last_point)
15123 {
15124 /* Cursor has to be moved backward. Note that PT >=
15125 CHARPOS (startp) because of the outer if-statement. */
15126 while (!row->mode_line_p
15127 && (MATRIX_ROW_START_CHARPOS (row) > PT
15128 || (MATRIX_ROW_START_CHARPOS (row) == PT
15129 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15130 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15131 row > w->current_matrix->rows
15132 && (row-1)->ends_in_newline_from_string_p))))
15133 && (row->y > top_scroll_margin
15134 || CHARPOS (startp) == BEGV))
15135 {
15136 eassert (row->enabled_p);
15137 --row;
15138 }
15139
15140 /* Consider the following case: Window starts at BEGV,
15141 there is invisible, intangible text at BEGV, so that
15142 display starts at some point START > BEGV. It can
15143 happen that we are called with PT somewhere between
15144 BEGV and START. Try to handle that case. */
15145 if (row < w->current_matrix->rows
15146 || row->mode_line_p)
15147 {
15148 row = w->current_matrix->rows;
15149 if (row->mode_line_p)
15150 ++row;
15151 }
15152
15153 /* Due to newlines in overlay strings, we may have to
15154 skip forward over overlay strings. */
15155 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15156 && MATRIX_ROW_END_CHARPOS (row) == PT
15157 && !cursor_row_p (row))
15158 ++row;
15159
15160 /* If within the scroll margin, scroll. */
15161 if (row->y < top_scroll_margin
15162 && CHARPOS (startp) != BEGV)
15163 scroll_p = 1;
15164 }
15165 else
15166 {
15167 /* Cursor did not move. So don't scroll even if cursor line
15168 is partially visible, as it was so before. */
15169 rc = CURSOR_MOVEMENT_SUCCESS;
15170 }
15171
15172 if (PT < MATRIX_ROW_START_CHARPOS (row)
15173 || PT > MATRIX_ROW_END_CHARPOS (row))
15174 {
15175 /* if PT is not in the glyph row, give up. */
15176 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15177 must_scroll = 1;
15178 }
15179 else if (rc != CURSOR_MOVEMENT_SUCCESS
15180 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15181 {
15182 struct glyph_row *row1;
15183
15184 /* If rows are bidi-reordered and point moved, back up
15185 until we find a row that does not belong to a
15186 continuation line. This is because we must consider
15187 all rows of a continued line as candidates for the
15188 new cursor positioning, since row start and end
15189 positions change non-linearly with vertical position
15190 in such rows. */
15191 /* FIXME: Revisit this when glyph ``spilling'' in
15192 continuation lines' rows is implemented for
15193 bidi-reordered rows. */
15194 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15195 MATRIX_ROW_CONTINUATION_LINE_P (row);
15196 --row)
15197 {
15198 /* If we hit the beginning of the displayed portion
15199 without finding the first row of a continued
15200 line, give up. */
15201 if (row <= row1)
15202 {
15203 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15204 break;
15205 }
15206 eassert (row->enabled_p);
15207 }
15208 }
15209 if (must_scroll)
15210 ;
15211 else if (rc != CURSOR_MOVEMENT_SUCCESS
15212 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15213 /* Make sure this isn't a header line by any chance, since
15214 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15215 && !row->mode_line_p
15216 && make_cursor_line_fully_visible_p)
15217 {
15218 if (PT == MATRIX_ROW_END_CHARPOS (row)
15219 && !row->ends_at_zv_p
15220 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15221 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15222 else if (row->height > window_box_height (w))
15223 {
15224 /* If we end up in a partially visible line, let's
15225 make it fully visible, except when it's taller
15226 than the window, in which case we can't do much
15227 about it. */
15228 *scroll_step = 1;
15229 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15230 }
15231 else
15232 {
15233 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15234 if (!cursor_row_fully_visible_p (w, 0, 1))
15235 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15236 else
15237 rc = CURSOR_MOVEMENT_SUCCESS;
15238 }
15239 }
15240 else if (scroll_p)
15241 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15242 else if (rc != CURSOR_MOVEMENT_SUCCESS
15243 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15244 {
15245 /* With bidi-reordered rows, there could be more than
15246 one candidate row whose start and end positions
15247 occlude point. We need to let set_cursor_from_row
15248 find the best candidate. */
15249 /* FIXME: Revisit this when glyph ``spilling'' in
15250 continuation lines' rows is implemented for
15251 bidi-reordered rows. */
15252 int rv = 0;
15253
15254 do
15255 {
15256 int at_zv_p = 0, exact_match_p = 0;
15257
15258 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15259 && PT <= MATRIX_ROW_END_CHARPOS (row)
15260 && cursor_row_p (row))
15261 rv |= set_cursor_from_row (w, row, w->current_matrix,
15262 0, 0, 0, 0);
15263 /* As soon as we've found the exact match for point,
15264 or the first suitable row whose ends_at_zv_p flag
15265 is set, we are done. */
15266 at_zv_p =
15267 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15268 if (rv && !at_zv_p
15269 && w->cursor.hpos >= 0
15270 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15271 w->cursor.vpos))
15272 {
15273 struct glyph_row *candidate =
15274 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15275 struct glyph *g =
15276 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15277 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15278
15279 exact_match_p =
15280 (BUFFERP (g->object) && g->charpos == PT)
15281 || (INTEGERP (g->object)
15282 && (g->charpos == PT
15283 || (g->charpos == 0 && endpos - 1 == PT)));
15284 }
15285 if (rv && (at_zv_p || exact_match_p))
15286 {
15287 rc = CURSOR_MOVEMENT_SUCCESS;
15288 break;
15289 }
15290 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15291 break;
15292 ++row;
15293 }
15294 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15295 || row->continued_p)
15296 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15297 || (MATRIX_ROW_START_CHARPOS (row) == PT
15298 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15299 /* If we didn't find any candidate rows, or exited the
15300 loop before all the candidates were examined, signal
15301 to the caller that this method failed. */
15302 if (rc != CURSOR_MOVEMENT_SUCCESS
15303 && !(rv
15304 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15305 && !row->continued_p))
15306 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15307 else if (rv)
15308 rc = CURSOR_MOVEMENT_SUCCESS;
15309 }
15310 else
15311 {
15312 do
15313 {
15314 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15315 {
15316 rc = CURSOR_MOVEMENT_SUCCESS;
15317 break;
15318 }
15319 ++row;
15320 }
15321 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15322 && MATRIX_ROW_START_CHARPOS (row) == PT
15323 && cursor_row_p (row));
15324 }
15325 }
15326 }
15327
15328 return rc;
15329 }
15330
15331 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15332 static
15333 #endif
15334 void
15335 set_vertical_scroll_bar (struct window *w)
15336 {
15337 ptrdiff_t start, end, whole;
15338
15339 /* Calculate the start and end positions for the current window.
15340 At some point, it would be nice to choose between scrollbars
15341 which reflect the whole buffer size, with special markers
15342 indicating narrowing, and scrollbars which reflect only the
15343 visible region.
15344
15345 Note that mini-buffers sometimes aren't displaying any text. */
15346 if (!MINI_WINDOW_P (w)
15347 || (w == XWINDOW (minibuf_window)
15348 && NILP (echo_area_buffer[0])))
15349 {
15350 struct buffer *buf = XBUFFER (w->contents);
15351 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15352 start = marker_position (w->start) - BUF_BEGV (buf);
15353 /* I don't think this is guaranteed to be right. For the
15354 moment, we'll pretend it is. */
15355 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15356
15357 if (end < start)
15358 end = start;
15359 if (whole < (end - start))
15360 whole = end - start;
15361 }
15362 else
15363 start = end = whole = 0;
15364
15365 /* Indicate what this scroll bar ought to be displaying now. */
15366 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15367 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15368 (w, end - start, whole, start);
15369 }
15370
15371
15372 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15373 selected_window is redisplayed.
15374
15375 We can return without actually redisplaying the window if fonts has been
15376 changed on window's frame. In that case, redisplay_internal will retry. */
15377
15378 static void
15379 redisplay_window (Lisp_Object window, bool just_this_one_p)
15380 {
15381 struct window *w = XWINDOW (window);
15382 struct frame *f = XFRAME (w->frame);
15383 struct buffer *buffer = XBUFFER (w->contents);
15384 struct buffer *old = current_buffer;
15385 struct text_pos lpoint, opoint, startp;
15386 int update_mode_line;
15387 int tem;
15388 struct it it;
15389 /* Record it now because it's overwritten. */
15390 bool current_matrix_up_to_date_p = false;
15391 bool used_current_matrix_p = false;
15392 /* This is less strict than current_matrix_up_to_date_p.
15393 It indicates that the buffer contents and narrowing are unchanged. */
15394 bool buffer_unchanged_p = false;
15395 int temp_scroll_step = 0;
15396 ptrdiff_t count = SPECPDL_INDEX ();
15397 int rc;
15398 int centering_position = -1;
15399 int last_line_misfit = 0;
15400 ptrdiff_t beg_unchanged, end_unchanged;
15401 int frame_line_height;
15402
15403 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15404 opoint = lpoint;
15405
15406 #ifdef GLYPH_DEBUG
15407 *w->desired_matrix->method = 0;
15408 #endif
15409
15410 if (!just_this_one_p
15411 && REDISPLAY_SOME_P ()
15412 && !w->redisplay
15413 && !f->redisplay
15414 && !buffer->text->redisplay)
15415 return;
15416
15417 /* Make sure that both W's markers are valid. */
15418 eassert (XMARKER (w->start)->buffer == buffer);
15419 eassert (XMARKER (w->pointm)->buffer == buffer);
15420
15421 restart:
15422 reconsider_clip_changes (w);
15423 frame_line_height = default_line_pixel_height (w);
15424
15425 /* Has the mode line to be updated? */
15426 update_mode_line = (w->update_mode_line
15427 || update_mode_lines
15428 || buffer->clip_changed
15429 || buffer->prevent_redisplay_optimizations_p);
15430
15431 if (!just_this_one_p)
15432 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15433 cleverly elsewhere. */
15434 w->must_be_updated_p = true;
15435
15436 if (MINI_WINDOW_P (w))
15437 {
15438 if (w == XWINDOW (echo_area_window)
15439 && !NILP (echo_area_buffer[0]))
15440 {
15441 if (update_mode_line)
15442 /* We may have to update a tty frame's menu bar or a
15443 tool-bar. Example `M-x C-h C-h C-g'. */
15444 goto finish_menu_bars;
15445 else
15446 /* We've already displayed the echo area glyphs in this window. */
15447 goto finish_scroll_bars;
15448 }
15449 else if ((w != XWINDOW (minibuf_window)
15450 || minibuf_level == 0)
15451 /* When buffer is nonempty, redisplay window normally. */
15452 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15453 /* Quail displays non-mini buffers in minibuffer window.
15454 In that case, redisplay the window normally. */
15455 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15456 {
15457 /* W is a mini-buffer window, but it's not active, so clear
15458 it. */
15459 int yb = window_text_bottom_y (w);
15460 struct glyph_row *row;
15461 int y;
15462
15463 for (y = 0, row = w->desired_matrix->rows;
15464 y < yb;
15465 y += row->height, ++row)
15466 blank_row (w, row, y);
15467 goto finish_scroll_bars;
15468 }
15469
15470 clear_glyph_matrix (w->desired_matrix);
15471 }
15472
15473 /* Otherwise set up data on this window; select its buffer and point
15474 value. */
15475 /* Really select the buffer, for the sake of buffer-local
15476 variables. */
15477 set_buffer_internal_1 (XBUFFER (w->contents));
15478
15479 current_matrix_up_to_date_p
15480 = (w->window_end_valid
15481 && !current_buffer->clip_changed
15482 && !current_buffer->prevent_redisplay_optimizations_p
15483 && !window_outdated (w));
15484
15485 /* Run the window-bottom-change-functions
15486 if it is possible that the text on the screen has changed
15487 (either due to modification of the text, or any other reason). */
15488 if (!current_matrix_up_to_date_p
15489 && !NILP (Vwindow_text_change_functions))
15490 {
15491 safe_run_hooks (Qwindow_text_change_functions);
15492 goto restart;
15493 }
15494
15495 beg_unchanged = BEG_UNCHANGED;
15496 end_unchanged = END_UNCHANGED;
15497
15498 SET_TEXT_POS (opoint, PT, PT_BYTE);
15499
15500 specbind (Qinhibit_point_motion_hooks, Qt);
15501
15502 buffer_unchanged_p
15503 = (w->window_end_valid
15504 && !current_buffer->clip_changed
15505 && !window_outdated (w));
15506
15507 /* When windows_or_buffers_changed is non-zero, we can't rely
15508 on the window end being valid, so set it to zero there. */
15509 if (windows_or_buffers_changed)
15510 {
15511 /* If window starts on a continuation line, maybe adjust the
15512 window start in case the window's width changed. */
15513 if (XMARKER (w->start)->buffer == current_buffer)
15514 compute_window_start_on_continuation_line (w);
15515
15516 w->window_end_valid = false;
15517 /* If so, we also can't rely on current matrix
15518 and should not fool try_cursor_movement below. */
15519 current_matrix_up_to_date_p = false;
15520 }
15521
15522 /* Some sanity checks. */
15523 CHECK_WINDOW_END (w);
15524 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15525 emacs_abort ();
15526 if (BYTEPOS (opoint) < CHARPOS (opoint))
15527 emacs_abort ();
15528
15529 if (mode_line_update_needed (w))
15530 update_mode_line = 1;
15531
15532 /* Point refers normally to the selected window. For any other
15533 window, set up appropriate value. */
15534 if (!EQ (window, selected_window))
15535 {
15536 ptrdiff_t new_pt = marker_position (w->pointm);
15537 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15538 if (new_pt < BEGV)
15539 {
15540 new_pt = BEGV;
15541 new_pt_byte = BEGV_BYTE;
15542 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15543 }
15544 else if (new_pt > (ZV - 1))
15545 {
15546 new_pt = ZV;
15547 new_pt_byte = ZV_BYTE;
15548 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15549 }
15550
15551 /* We don't use SET_PT so that the point-motion hooks don't run. */
15552 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15553 }
15554
15555 /* If any of the character widths specified in the display table
15556 have changed, invalidate the width run cache. It's true that
15557 this may be a bit late to catch such changes, but the rest of
15558 redisplay goes (non-fatally) haywire when the display table is
15559 changed, so why should we worry about doing any better? */
15560 if (current_buffer->width_run_cache)
15561 {
15562 struct Lisp_Char_Table *disptab = buffer_display_table ();
15563
15564 if (! disptab_matches_widthtab
15565 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15566 {
15567 invalidate_region_cache (current_buffer,
15568 current_buffer->width_run_cache,
15569 BEG, Z);
15570 recompute_width_table (current_buffer, disptab);
15571 }
15572 }
15573
15574 /* If window-start is screwed up, choose a new one. */
15575 if (XMARKER (w->start)->buffer != current_buffer)
15576 goto recenter;
15577
15578 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15579
15580 /* If someone specified a new starting point but did not insist,
15581 check whether it can be used. */
15582 if (w->optional_new_start
15583 && CHARPOS (startp) >= BEGV
15584 && CHARPOS (startp) <= ZV)
15585 {
15586 w->optional_new_start = 0;
15587 start_display (&it, w, startp);
15588 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15589 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15590 if (IT_CHARPOS (it) == PT)
15591 w->force_start = 1;
15592 /* IT may overshoot PT if text at PT is invisible. */
15593 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15594 w->force_start = 1;
15595 }
15596
15597 force_start:
15598
15599 /* Handle case where place to start displaying has been specified,
15600 unless the specified location is outside the accessible range. */
15601 if (w->force_start || window_frozen_p (w))
15602 {
15603 /* We set this later on if we have to adjust point. */
15604 int new_vpos = -1;
15605
15606 w->force_start = 0;
15607 w->vscroll = 0;
15608 w->window_end_valid = 0;
15609
15610 /* Forget any recorded base line for line number display. */
15611 if (!buffer_unchanged_p)
15612 w->base_line_number = 0;
15613
15614 /* Redisplay the mode line. Select the buffer properly for that.
15615 Also, run the hook window-scroll-functions
15616 because we have scrolled. */
15617 /* Note, we do this after clearing force_start because
15618 if there's an error, it is better to forget about force_start
15619 than to get into an infinite loop calling the hook functions
15620 and having them get more errors. */
15621 if (!update_mode_line
15622 || ! NILP (Vwindow_scroll_functions))
15623 {
15624 update_mode_line = 1;
15625 w->update_mode_line = 1;
15626 startp = run_window_scroll_functions (window, startp);
15627 }
15628
15629 if (CHARPOS (startp) < BEGV)
15630 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15631 else if (CHARPOS (startp) > ZV)
15632 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15633
15634 /* Redisplay, then check if cursor has been set during the
15635 redisplay. Give up if new fonts were loaded. */
15636 /* We used to issue a CHECK_MARGINS argument to try_window here,
15637 but this causes scrolling to fail when point begins inside
15638 the scroll margin (bug#148) -- cyd */
15639 if (!try_window (window, startp, 0))
15640 {
15641 w->force_start = 1;
15642 clear_glyph_matrix (w->desired_matrix);
15643 goto need_larger_matrices;
15644 }
15645
15646 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15647 {
15648 /* If point does not appear, try to move point so it does
15649 appear. The desired matrix has been built above, so we
15650 can use it here. */
15651 new_vpos = window_box_height (w) / 2;
15652 }
15653
15654 if (!cursor_row_fully_visible_p (w, 0, 0))
15655 {
15656 /* Point does appear, but on a line partly visible at end of window.
15657 Move it back to a fully-visible line. */
15658 new_vpos = window_box_height (w);
15659 }
15660 else if (w->cursor.vpos >= 0)
15661 {
15662 /* Some people insist on not letting point enter the scroll
15663 margin, even though this part handles windows that didn't
15664 scroll at all. */
15665 int window_total_lines
15666 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15667 int margin = min (scroll_margin, window_total_lines / 4);
15668 int pixel_margin = margin * frame_line_height;
15669 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15670
15671 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15672 below, which finds the row to move point to, advances by
15673 the Y coordinate of the _next_ row, see the definition of
15674 MATRIX_ROW_BOTTOM_Y. */
15675 if (w->cursor.vpos < margin + header_line)
15676 {
15677 w->cursor.vpos = -1;
15678 clear_glyph_matrix (w->desired_matrix);
15679 goto try_to_scroll;
15680 }
15681 else
15682 {
15683 int window_height = window_box_height (w);
15684
15685 if (header_line)
15686 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15687 if (w->cursor.y >= window_height - pixel_margin)
15688 {
15689 w->cursor.vpos = -1;
15690 clear_glyph_matrix (w->desired_matrix);
15691 goto try_to_scroll;
15692 }
15693 }
15694 }
15695
15696 /* If we need to move point for either of the above reasons,
15697 now actually do it. */
15698 if (new_vpos >= 0)
15699 {
15700 struct glyph_row *row;
15701
15702 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15703 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15704 ++row;
15705
15706 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15707 MATRIX_ROW_START_BYTEPOS (row));
15708
15709 if (w != XWINDOW (selected_window))
15710 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15711 else if (current_buffer == old)
15712 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15713
15714 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15715
15716 /* If we are highlighting the region, then we just changed
15717 the region, so redisplay to show it. */
15718 /* FIXME: We need to (re)run pre-redisplay-function! */
15719 /* if (markpos_of_region () >= 0)
15720 {
15721 clear_glyph_matrix (w->desired_matrix);
15722 if (!try_window (window, startp, 0))
15723 goto need_larger_matrices;
15724 }
15725 */
15726 }
15727
15728 #ifdef GLYPH_DEBUG
15729 debug_method_add (w, "forced window start");
15730 #endif
15731 goto done;
15732 }
15733
15734 /* Handle case where text has not changed, only point, and it has
15735 not moved off the frame, and we are not retrying after hscroll.
15736 (current_matrix_up_to_date_p is nonzero when retrying.) */
15737 if (current_matrix_up_to_date_p
15738 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15739 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15740 {
15741 switch (rc)
15742 {
15743 case CURSOR_MOVEMENT_SUCCESS:
15744 used_current_matrix_p = 1;
15745 goto done;
15746
15747 case CURSOR_MOVEMENT_MUST_SCROLL:
15748 goto try_to_scroll;
15749
15750 default:
15751 emacs_abort ();
15752 }
15753 }
15754 /* If current starting point was originally the beginning of a line
15755 but no longer is, find a new starting point. */
15756 else if (w->start_at_line_beg
15757 && !(CHARPOS (startp) <= BEGV
15758 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15759 {
15760 #ifdef GLYPH_DEBUG
15761 debug_method_add (w, "recenter 1");
15762 #endif
15763 goto recenter;
15764 }
15765
15766 /* Try scrolling with try_window_id. Value is > 0 if update has
15767 been done, it is -1 if we know that the same window start will
15768 not work. It is 0 if unsuccessful for some other reason. */
15769 else if ((tem = try_window_id (w)) != 0)
15770 {
15771 #ifdef GLYPH_DEBUG
15772 debug_method_add (w, "try_window_id %d", tem);
15773 #endif
15774
15775 if (f->fonts_changed)
15776 goto need_larger_matrices;
15777 if (tem > 0)
15778 goto done;
15779
15780 /* Otherwise try_window_id has returned -1 which means that we
15781 don't want the alternative below this comment to execute. */
15782 }
15783 else if (CHARPOS (startp) >= BEGV
15784 && CHARPOS (startp) <= ZV
15785 && PT >= CHARPOS (startp)
15786 && (CHARPOS (startp) < ZV
15787 /* Avoid starting at end of buffer. */
15788 || CHARPOS (startp) == BEGV
15789 || !window_outdated (w)))
15790 {
15791 int d1, d2, d3, d4, d5, d6;
15792
15793 /* If first window line is a continuation line, and window start
15794 is inside the modified region, but the first change is before
15795 current window start, we must select a new window start.
15796
15797 However, if this is the result of a down-mouse event (e.g. by
15798 extending the mouse-drag-overlay), we don't want to select a
15799 new window start, since that would change the position under
15800 the mouse, resulting in an unwanted mouse-movement rather
15801 than a simple mouse-click. */
15802 if (!w->start_at_line_beg
15803 && NILP (do_mouse_tracking)
15804 && CHARPOS (startp) > BEGV
15805 && CHARPOS (startp) > BEG + beg_unchanged
15806 && CHARPOS (startp) <= Z - end_unchanged
15807 /* Even if w->start_at_line_beg is nil, a new window may
15808 start at a line_beg, since that's how set_buffer_window
15809 sets it. So, we need to check the return value of
15810 compute_window_start_on_continuation_line. (See also
15811 bug#197). */
15812 && XMARKER (w->start)->buffer == current_buffer
15813 && compute_window_start_on_continuation_line (w)
15814 /* It doesn't make sense to force the window start like we
15815 do at label force_start if it is already known that point
15816 will not be visible in the resulting window, because
15817 doing so will move point from its correct position
15818 instead of scrolling the window to bring point into view.
15819 See bug#9324. */
15820 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15821 {
15822 w->force_start = 1;
15823 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15824 goto force_start;
15825 }
15826
15827 #ifdef GLYPH_DEBUG
15828 debug_method_add (w, "same window start");
15829 #endif
15830
15831 /* Try to redisplay starting at same place as before.
15832 If point has not moved off frame, accept the results. */
15833 if (!current_matrix_up_to_date_p
15834 /* Don't use try_window_reusing_current_matrix in this case
15835 because a window scroll function can have changed the
15836 buffer. */
15837 || !NILP (Vwindow_scroll_functions)
15838 || MINI_WINDOW_P (w)
15839 || !(used_current_matrix_p
15840 = try_window_reusing_current_matrix (w)))
15841 {
15842 IF_DEBUG (debug_method_add (w, "1"));
15843 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15844 /* -1 means we need to scroll.
15845 0 means we need new matrices, but fonts_changed
15846 is set in that case, so we will detect it below. */
15847 goto try_to_scroll;
15848 }
15849
15850 if (f->fonts_changed)
15851 goto need_larger_matrices;
15852
15853 if (w->cursor.vpos >= 0)
15854 {
15855 if (!just_this_one_p
15856 || current_buffer->clip_changed
15857 || BEG_UNCHANGED < CHARPOS (startp))
15858 /* Forget any recorded base line for line number display. */
15859 w->base_line_number = 0;
15860
15861 if (!cursor_row_fully_visible_p (w, 1, 0))
15862 {
15863 clear_glyph_matrix (w->desired_matrix);
15864 last_line_misfit = 1;
15865 }
15866 /* Drop through and scroll. */
15867 else
15868 goto done;
15869 }
15870 else
15871 clear_glyph_matrix (w->desired_matrix);
15872 }
15873
15874 try_to_scroll:
15875
15876 /* Redisplay the mode line. Select the buffer properly for that. */
15877 if (!update_mode_line)
15878 {
15879 update_mode_line = 1;
15880 w->update_mode_line = 1;
15881 }
15882
15883 /* Try to scroll by specified few lines. */
15884 if ((scroll_conservatively
15885 || emacs_scroll_step
15886 || temp_scroll_step
15887 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15888 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15889 && CHARPOS (startp) >= BEGV
15890 && CHARPOS (startp) <= ZV)
15891 {
15892 /* The function returns -1 if new fonts were loaded, 1 if
15893 successful, 0 if not successful. */
15894 int ss = try_scrolling (window, just_this_one_p,
15895 scroll_conservatively,
15896 emacs_scroll_step,
15897 temp_scroll_step, last_line_misfit);
15898 switch (ss)
15899 {
15900 case SCROLLING_SUCCESS:
15901 goto done;
15902
15903 case SCROLLING_NEED_LARGER_MATRICES:
15904 goto need_larger_matrices;
15905
15906 case SCROLLING_FAILED:
15907 break;
15908
15909 default:
15910 emacs_abort ();
15911 }
15912 }
15913
15914 /* Finally, just choose a place to start which positions point
15915 according to user preferences. */
15916
15917 recenter:
15918
15919 #ifdef GLYPH_DEBUG
15920 debug_method_add (w, "recenter");
15921 #endif
15922
15923 /* Forget any previously recorded base line for line number display. */
15924 if (!buffer_unchanged_p)
15925 w->base_line_number = 0;
15926
15927 /* Determine the window start relative to point. */
15928 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15929 it.current_y = it.last_visible_y;
15930 if (centering_position < 0)
15931 {
15932 int window_total_lines
15933 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15934 int margin =
15935 scroll_margin > 0
15936 ? min (scroll_margin, window_total_lines / 4)
15937 : 0;
15938 ptrdiff_t margin_pos = CHARPOS (startp);
15939 Lisp_Object aggressive;
15940 int scrolling_up;
15941
15942 /* If there is a scroll margin at the top of the window, find
15943 its character position. */
15944 if (margin
15945 /* Cannot call start_display if startp is not in the
15946 accessible region of the buffer. This can happen when we
15947 have just switched to a different buffer and/or changed
15948 its restriction. In that case, startp is initialized to
15949 the character position 1 (BEGV) because we did not yet
15950 have chance to display the buffer even once. */
15951 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15952 {
15953 struct it it1;
15954 void *it1data = NULL;
15955
15956 SAVE_IT (it1, it, it1data);
15957 start_display (&it1, w, startp);
15958 move_it_vertically (&it1, margin * frame_line_height);
15959 margin_pos = IT_CHARPOS (it1);
15960 RESTORE_IT (&it, &it, it1data);
15961 }
15962 scrolling_up = PT > margin_pos;
15963 aggressive =
15964 scrolling_up
15965 ? BVAR (current_buffer, scroll_up_aggressively)
15966 : BVAR (current_buffer, scroll_down_aggressively);
15967
15968 if (!MINI_WINDOW_P (w)
15969 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15970 {
15971 int pt_offset = 0;
15972
15973 /* Setting scroll-conservatively overrides
15974 scroll-*-aggressively. */
15975 if (!scroll_conservatively && NUMBERP (aggressive))
15976 {
15977 double float_amount = XFLOATINT (aggressive);
15978
15979 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15980 if (pt_offset == 0 && float_amount > 0)
15981 pt_offset = 1;
15982 if (pt_offset && margin > 0)
15983 margin -= 1;
15984 }
15985 /* Compute how much to move the window start backward from
15986 point so that point will be displayed where the user
15987 wants it. */
15988 if (scrolling_up)
15989 {
15990 centering_position = it.last_visible_y;
15991 if (pt_offset)
15992 centering_position -= pt_offset;
15993 centering_position -=
15994 frame_line_height * (1 + margin + (last_line_misfit != 0))
15995 + WINDOW_HEADER_LINE_HEIGHT (w);
15996 /* Don't let point enter the scroll margin near top of
15997 the window. */
15998 if (centering_position < margin * frame_line_height)
15999 centering_position = margin * frame_line_height;
16000 }
16001 else
16002 centering_position = margin * frame_line_height + pt_offset;
16003 }
16004 else
16005 /* Set the window start half the height of the window backward
16006 from point. */
16007 centering_position = window_box_height (w) / 2;
16008 }
16009 move_it_vertically_backward (&it, centering_position);
16010
16011 eassert (IT_CHARPOS (it) >= BEGV);
16012
16013 /* The function move_it_vertically_backward may move over more
16014 than the specified y-distance. If it->w is small, e.g. a
16015 mini-buffer window, we may end up in front of the window's
16016 display area. Start displaying at the start of the line
16017 containing PT in this case. */
16018 if (it.current_y <= 0)
16019 {
16020 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16021 move_it_vertically_backward (&it, 0);
16022 it.current_y = 0;
16023 }
16024
16025 it.current_x = it.hpos = 0;
16026
16027 /* Set the window start position here explicitly, to avoid an
16028 infinite loop in case the functions in window-scroll-functions
16029 get errors. */
16030 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16031
16032 /* Run scroll hooks. */
16033 startp = run_window_scroll_functions (window, it.current.pos);
16034
16035 /* Redisplay the window. */
16036 if (!current_matrix_up_to_date_p
16037 || windows_or_buffers_changed
16038 || f->cursor_type_changed
16039 /* Don't use try_window_reusing_current_matrix in this case
16040 because it can have changed the buffer. */
16041 || !NILP (Vwindow_scroll_functions)
16042 || !just_this_one_p
16043 || MINI_WINDOW_P (w)
16044 || !(used_current_matrix_p
16045 = try_window_reusing_current_matrix (w)))
16046 try_window (window, startp, 0);
16047
16048 /* If new fonts have been loaded (due to fontsets), give up. We
16049 have to start a new redisplay since we need to re-adjust glyph
16050 matrices. */
16051 if (f->fonts_changed)
16052 goto need_larger_matrices;
16053
16054 /* If cursor did not appear assume that the middle of the window is
16055 in the first line of the window. Do it again with the next line.
16056 (Imagine a window of height 100, displaying two lines of height
16057 60. Moving back 50 from it->last_visible_y will end in the first
16058 line.) */
16059 if (w->cursor.vpos < 0)
16060 {
16061 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16062 {
16063 clear_glyph_matrix (w->desired_matrix);
16064 move_it_by_lines (&it, 1);
16065 try_window (window, it.current.pos, 0);
16066 }
16067 else if (PT < IT_CHARPOS (it))
16068 {
16069 clear_glyph_matrix (w->desired_matrix);
16070 move_it_by_lines (&it, -1);
16071 try_window (window, it.current.pos, 0);
16072 }
16073 else
16074 {
16075 /* Not much we can do about it. */
16076 }
16077 }
16078
16079 /* Consider the following case: Window starts at BEGV, there is
16080 invisible, intangible text at BEGV, so that display starts at
16081 some point START > BEGV. It can happen that we are called with
16082 PT somewhere between BEGV and START. Try to handle that case. */
16083 if (w->cursor.vpos < 0)
16084 {
16085 struct glyph_row *row = w->current_matrix->rows;
16086 if (row->mode_line_p)
16087 ++row;
16088 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16089 }
16090
16091 if (!cursor_row_fully_visible_p (w, 0, 0))
16092 {
16093 /* If vscroll is enabled, disable it and try again. */
16094 if (w->vscroll)
16095 {
16096 w->vscroll = 0;
16097 clear_glyph_matrix (w->desired_matrix);
16098 goto recenter;
16099 }
16100
16101 /* Users who set scroll-conservatively to a large number want
16102 point just above/below the scroll margin. If we ended up
16103 with point's row partially visible, move the window start to
16104 make that row fully visible and out of the margin. */
16105 if (scroll_conservatively > SCROLL_LIMIT)
16106 {
16107 int window_total_lines
16108 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16109 int margin =
16110 scroll_margin > 0
16111 ? min (scroll_margin, window_total_lines / 4)
16112 : 0;
16113 int move_down = w->cursor.vpos >= window_total_lines / 2;
16114
16115 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16116 clear_glyph_matrix (w->desired_matrix);
16117 if (1 == try_window (window, it.current.pos,
16118 TRY_WINDOW_CHECK_MARGINS))
16119 goto done;
16120 }
16121
16122 /* If centering point failed to make the whole line visible,
16123 put point at the top instead. That has to make the whole line
16124 visible, if it can be done. */
16125 if (centering_position == 0)
16126 goto done;
16127
16128 clear_glyph_matrix (w->desired_matrix);
16129 centering_position = 0;
16130 goto recenter;
16131 }
16132
16133 done:
16134
16135 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16136 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16137 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16138
16139 /* Display the mode line, if we must. */
16140 if ((update_mode_line
16141 /* If window not full width, must redo its mode line
16142 if (a) the window to its side is being redone and
16143 (b) we do a frame-based redisplay. This is a consequence
16144 of how inverted lines are drawn in frame-based redisplay. */
16145 || (!just_this_one_p
16146 && !FRAME_WINDOW_P (f)
16147 && !WINDOW_FULL_WIDTH_P (w))
16148 /* Line number to display. */
16149 || w->base_line_pos > 0
16150 /* Column number is displayed and different from the one displayed. */
16151 || (w->column_number_displayed != -1
16152 && (w->column_number_displayed != current_column ())))
16153 /* This means that the window has a mode line. */
16154 && (WINDOW_WANTS_MODELINE_P (w)
16155 || WINDOW_WANTS_HEADER_LINE_P (w)))
16156 {
16157
16158 display_mode_lines (w);
16159
16160 /* If mode line height has changed, arrange for a thorough
16161 immediate redisplay using the correct mode line height. */
16162 if (WINDOW_WANTS_MODELINE_P (w)
16163 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16164 {
16165 f->fonts_changed = 1;
16166 w->mode_line_height = -1;
16167 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16168 = DESIRED_MODE_LINE_HEIGHT (w);
16169 }
16170
16171 /* If header line height has changed, arrange for a thorough
16172 immediate redisplay using the correct header line height. */
16173 if (WINDOW_WANTS_HEADER_LINE_P (w)
16174 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16175 {
16176 f->fonts_changed = 1;
16177 w->header_line_height = -1;
16178 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16179 = DESIRED_HEADER_LINE_HEIGHT (w);
16180 }
16181
16182 if (f->fonts_changed)
16183 goto need_larger_matrices;
16184 }
16185
16186 if (!line_number_displayed && w->base_line_pos != -1)
16187 {
16188 w->base_line_pos = 0;
16189 w->base_line_number = 0;
16190 }
16191
16192 finish_menu_bars:
16193
16194 /* When we reach a frame's selected window, redo the frame's menu bar. */
16195 if (update_mode_line
16196 && EQ (FRAME_SELECTED_WINDOW (f), window))
16197 {
16198 int redisplay_menu_p = 0;
16199
16200 if (FRAME_WINDOW_P (f))
16201 {
16202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16203 || defined (HAVE_NS) || defined (USE_GTK)
16204 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16205 #else
16206 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16207 #endif
16208 }
16209 else
16210 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16211
16212 if (redisplay_menu_p)
16213 display_menu_bar (w);
16214
16215 #ifdef HAVE_WINDOW_SYSTEM
16216 if (FRAME_WINDOW_P (f))
16217 {
16218 #if defined (USE_GTK) || defined (HAVE_NS)
16219 if (FRAME_EXTERNAL_TOOL_BAR (f))
16220 redisplay_tool_bar (f);
16221 #else
16222 if (WINDOWP (f->tool_bar_window)
16223 && (FRAME_TOOL_BAR_LINES (f) > 0
16224 || !NILP (Vauto_resize_tool_bars))
16225 && redisplay_tool_bar (f))
16226 ignore_mouse_drag_p = 1;
16227 #endif
16228 }
16229 #endif
16230 }
16231
16232 #ifdef HAVE_WINDOW_SYSTEM
16233 if (FRAME_WINDOW_P (f)
16234 && update_window_fringes (w, (just_this_one_p
16235 || (!used_current_matrix_p && !overlay_arrow_seen)
16236 || w->pseudo_window_p)))
16237 {
16238 update_begin (f);
16239 block_input ();
16240 if (draw_window_fringes (w, 1))
16241 x_draw_vertical_border (w);
16242 unblock_input ();
16243 update_end (f);
16244 }
16245 #endif /* HAVE_WINDOW_SYSTEM */
16246
16247 /* We go to this label, with fonts_changed set, if it is
16248 necessary to try again using larger glyph matrices.
16249 We have to redeem the scroll bar even in this case,
16250 because the loop in redisplay_internal expects that. */
16251 need_larger_matrices:
16252 ;
16253 finish_scroll_bars:
16254
16255 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16256 {
16257 /* Set the thumb's position and size. */
16258 set_vertical_scroll_bar (w);
16259
16260 /* Note that we actually used the scroll bar attached to this
16261 window, so it shouldn't be deleted at the end of redisplay. */
16262 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16263 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16264 }
16265
16266 /* Restore current_buffer and value of point in it. The window
16267 update may have changed the buffer, so first make sure `opoint'
16268 is still valid (Bug#6177). */
16269 if (CHARPOS (opoint) < BEGV)
16270 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16271 else if (CHARPOS (opoint) > ZV)
16272 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16273 else
16274 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16275
16276 set_buffer_internal_1 (old);
16277 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16278 shorter. This can be caused by log truncation in *Messages*. */
16279 if (CHARPOS (lpoint) <= ZV)
16280 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16281
16282 unbind_to (count, Qnil);
16283 }
16284
16285
16286 /* Build the complete desired matrix of WINDOW with a window start
16287 buffer position POS.
16288
16289 Value is 1 if successful. It is zero if fonts were loaded during
16290 redisplay which makes re-adjusting glyph matrices necessary, and -1
16291 if point would appear in the scroll margins.
16292 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16293 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16294 set in FLAGS.) */
16295
16296 int
16297 try_window (Lisp_Object window, struct text_pos pos, int flags)
16298 {
16299 struct window *w = XWINDOW (window);
16300 struct it it;
16301 struct glyph_row *last_text_row = NULL;
16302 struct frame *f = XFRAME (w->frame);
16303 int frame_line_height = default_line_pixel_height (w);
16304
16305 /* Make POS the new window start. */
16306 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16307
16308 /* Mark cursor position as unknown. No overlay arrow seen. */
16309 w->cursor.vpos = -1;
16310 overlay_arrow_seen = 0;
16311
16312 /* Initialize iterator and info to start at POS. */
16313 start_display (&it, w, pos);
16314
16315 /* Display all lines of W. */
16316 while (it.current_y < it.last_visible_y)
16317 {
16318 if (display_line (&it))
16319 last_text_row = it.glyph_row - 1;
16320 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16321 return 0;
16322 }
16323
16324 /* Don't let the cursor end in the scroll margins. */
16325 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16326 && !MINI_WINDOW_P (w))
16327 {
16328 int this_scroll_margin;
16329 int window_total_lines
16330 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16331
16332 if (scroll_margin > 0)
16333 {
16334 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16335 this_scroll_margin *= frame_line_height;
16336 }
16337 else
16338 this_scroll_margin = 0;
16339
16340 if ((w->cursor.y >= 0 /* not vscrolled */
16341 && w->cursor.y < this_scroll_margin
16342 && CHARPOS (pos) > BEGV
16343 && IT_CHARPOS (it) < ZV)
16344 /* rms: considering make_cursor_line_fully_visible_p here
16345 seems to give wrong results. We don't want to recenter
16346 when the last line is partly visible, we want to allow
16347 that case to be handled in the usual way. */
16348 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16349 {
16350 w->cursor.vpos = -1;
16351 clear_glyph_matrix (w->desired_matrix);
16352 return -1;
16353 }
16354 }
16355
16356 /* If bottom moved off end of frame, change mode line percentage. */
16357 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16358 w->update_mode_line = 1;
16359
16360 /* Set window_end_pos to the offset of the last character displayed
16361 on the window from the end of current_buffer. Set
16362 window_end_vpos to its row number. */
16363 if (last_text_row)
16364 {
16365 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16366 adjust_window_ends (w, last_text_row, 0);
16367 eassert
16368 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16369 w->window_end_vpos)));
16370 }
16371 else
16372 {
16373 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16374 w->window_end_pos = Z - ZV;
16375 w->window_end_vpos = 0;
16376 }
16377
16378 /* But that is not valid info until redisplay finishes. */
16379 w->window_end_valid = 0;
16380 return 1;
16381 }
16382
16383
16384 \f
16385 /************************************************************************
16386 Window redisplay reusing current matrix when buffer has not changed
16387 ************************************************************************/
16388
16389 /* Try redisplay of window W showing an unchanged buffer with a
16390 different window start than the last time it was displayed by
16391 reusing its current matrix. Value is non-zero if successful.
16392 W->start is the new window start. */
16393
16394 static int
16395 try_window_reusing_current_matrix (struct window *w)
16396 {
16397 struct frame *f = XFRAME (w->frame);
16398 struct glyph_row *bottom_row;
16399 struct it it;
16400 struct run run;
16401 struct text_pos start, new_start;
16402 int nrows_scrolled, i;
16403 struct glyph_row *last_text_row;
16404 struct glyph_row *last_reused_text_row;
16405 struct glyph_row *start_row;
16406 int start_vpos, min_y, max_y;
16407
16408 #ifdef GLYPH_DEBUG
16409 if (inhibit_try_window_reusing)
16410 return 0;
16411 #endif
16412
16413 if (/* This function doesn't handle terminal frames. */
16414 !FRAME_WINDOW_P (f)
16415 /* Don't try to reuse the display if windows have been split
16416 or such. */
16417 || windows_or_buffers_changed
16418 || f->cursor_type_changed)
16419 return 0;
16420
16421 /* Can't do this if showing trailing whitespace. */
16422 if (!NILP (Vshow_trailing_whitespace))
16423 return 0;
16424
16425 /* If top-line visibility has changed, give up. */
16426 if (WINDOW_WANTS_HEADER_LINE_P (w)
16427 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16428 return 0;
16429
16430 /* Give up if old or new display is scrolled vertically. We could
16431 make this function handle this, but right now it doesn't. */
16432 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16433 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16434 return 0;
16435
16436 /* The variable new_start now holds the new window start. The old
16437 start `start' can be determined from the current matrix. */
16438 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16439 start = start_row->minpos;
16440 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16441
16442 /* Clear the desired matrix for the display below. */
16443 clear_glyph_matrix (w->desired_matrix);
16444
16445 if (CHARPOS (new_start) <= CHARPOS (start))
16446 {
16447 /* Don't use this method if the display starts with an ellipsis
16448 displayed for invisible text. It's not easy to handle that case
16449 below, and it's certainly not worth the effort since this is
16450 not a frequent case. */
16451 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16452 return 0;
16453
16454 IF_DEBUG (debug_method_add (w, "twu1"));
16455
16456 /* Display up to a row that can be reused. The variable
16457 last_text_row is set to the last row displayed that displays
16458 text. Note that it.vpos == 0 if or if not there is a
16459 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16460 start_display (&it, w, new_start);
16461 w->cursor.vpos = -1;
16462 last_text_row = last_reused_text_row = NULL;
16463
16464 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16465 {
16466 /* If we have reached into the characters in the START row,
16467 that means the line boundaries have changed. So we
16468 can't start copying with the row START. Maybe it will
16469 work to start copying with the following row. */
16470 while (IT_CHARPOS (it) > CHARPOS (start))
16471 {
16472 /* Advance to the next row as the "start". */
16473 start_row++;
16474 start = start_row->minpos;
16475 /* If there are no more rows to try, or just one, give up. */
16476 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16477 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16478 || CHARPOS (start) == ZV)
16479 {
16480 clear_glyph_matrix (w->desired_matrix);
16481 return 0;
16482 }
16483
16484 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16485 }
16486 /* If we have reached alignment, we can copy the rest of the
16487 rows. */
16488 if (IT_CHARPOS (it) == CHARPOS (start)
16489 /* Don't accept "alignment" inside a display vector,
16490 since start_row could have started in the middle of
16491 that same display vector (thus their character
16492 positions match), and we have no way of telling if
16493 that is the case. */
16494 && it.current.dpvec_index < 0)
16495 break;
16496
16497 if (display_line (&it))
16498 last_text_row = it.glyph_row - 1;
16499
16500 }
16501
16502 /* A value of current_y < last_visible_y means that we stopped
16503 at the previous window start, which in turn means that we
16504 have at least one reusable row. */
16505 if (it.current_y < it.last_visible_y)
16506 {
16507 struct glyph_row *row;
16508
16509 /* IT.vpos always starts from 0; it counts text lines. */
16510 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16511
16512 /* Find PT if not already found in the lines displayed. */
16513 if (w->cursor.vpos < 0)
16514 {
16515 int dy = it.current_y - start_row->y;
16516
16517 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16518 row = row_containing_pos (w, PT, row, NULL, dy);
16519 if (row)
16520 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16521 dy, nrows_scrolled);
16522 else
16523 {
16524 clear_glyph_matrix (w->desired_matrix);
16525 return 0;
16526 }
16527 }
16528
16529 /* Scroll the display. Do it before the current matrix is
16530 changed. The problem here is that update has not yet
16531 run, i.e. part of the current matrix is not up to date.
16532 scroll_run_hook will clear the cursor, and use the
16533 current matrix to get the height of the row the cursor is
16534 in. */
16535 run.current_y = start_row->y;
16536 run.desired_y = it.current_y;
16537 run.height = it.last_visible_y - it.current_y;
16538
16539 if (run.height > 0 && run.current_y != run.desired_y)
16540 {
16541 update_begin (f);
16542 FRAME_RIF (f)->update_window_begin_hook (w);
16543 FRAME_RIF (f)->clear_window_mouse_face (w);
16544 FRAME_RIF (f)->scroll_run_hook (w, &run);
16545 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16546 update_end (f);
16547 }
16548
16549 /* Shift current matrix down by nrows_scrolled lines. */
16550 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16551 rotate_matrix (w->current_matrix,
16552 start_vpos,
16553 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16554 nrows_scrolled);
16555
16556 /* Disable lines that must be updated. */
16557 for (i = 0; i < nrows_scrolled; ++i)
16558 (start_row + i)->enabled_p = 0;
16559
16560 /* Re-compute Y positions. */
16561 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16562 max_y = it.last_visible_y;
16563 for (row = start_row + nrows_scrolled;
16564 row < bottom_row;
16565 ++row)
16566 {
16567 row->y = it.current_y;
16568 row->visible_height = row->height;
16569
16570 if (row->y < min_y)
16571 row->visible_height -= min_y - row->y;
16572 if (row->y + row->height > max_y)
16573 row->visible_height -= row->y + row->height - max_y;
16574 if (row->fringe_bitmap_periodic_p)
16575 row->redraw_fringe_bitmaps_p = 1;
16576
16577 it.current_y += row->height;
16578
16579 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16580 last_reused_text_row = row;
16581 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16582 break;
16583 }
16584
16585 /* Disable lines in the current matrix which are now
16586 below the window. */
16587 for (++row; row < bottom_row; ++row)
16588 row->enabled_p = row->mode_line_p = 0;
16589 }
16590
16591 /* Update window_end_pos etc.; last_reused_text_row is the last
16592 reused row from the current matrix containing text, if any.
16593 The value of last_text_row is the last displayed line
16594 containing text. */
16595 if (last_reused_text_row)
16596 adjust_window_ends (w, last_reused_text_row, 1);
16597 else if (last_text_row)
16598 adjust_window_ends (w, last_text_row, 0);
16599 else
16600 {
16601 /* This window must be completely empty. */
16602 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16603 w->window_end_pos = Z - ZV;
16604 w->window_end_vpos = 0;
16605 }
16606 w->window_end_valid = 0;
16607
16608 /* Update hint: don't try scrolling again in update_window. */
16609 w->desired_matrix->no_scrolling_p = 1;
16610
16611 #ifdef GLYPH_DEBUG
16612 debug_method_add (w, "try_window_reusing_current_matrix 1");
16613 #endif
16614 return 1;
16615 }
16616 else if (CHARPOS (new_start) > CHARPOS (start))
16617 {
16618 struct glyph_row *pt_row, *row;
16619 struct glyph_row *first_reusable_row;
16620 struct glyph_row *first_row_to_display;
16621 int dy;
16622 int yb = window_text_bottom_y (w);
16623
16624 /* Find the row starting at new_start, if there is one. Don't
16625 reuse a partially visible line at the end. */
16626 first_reusable_row = start_row;
16627 while (first_reusable_row->enabled_p
16628 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16629 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16630 < CHARPOS (new_start)))
16631 ++first_reusable_row;
16632
16633 /* Give up if there is no row to reuse. */
16634 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16635 || !first_reusable_row->enabled_p
16636 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16637 != CHARPOS (new_start)))
16638 return 0;
16639
16640 /* We can reuse fully visible rows beginning with
16641 first_reusable_row to the end of the window. Set
16642 first_row_to_display to the first row that cannot be reused.
16643 Set pt_row to the row containing point, if there is any. */
16644 pt_row = NULL;
16645 for (first_row_to_display = first_reusable_row;
16646 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16647 ++first_row_to_display)
16648 {
16649 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16650 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16651 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16652 && first_row_to_display->ends_at_zv_p
16653 && pt_row == NULL)))
16654 pt_row = first_row_to_display;
16655 }
16656
16657 /* Start displaying at the start of first_row_to_display. */
16658 eassert (first_row_to_display->y < yb);
16659 init_to_row_start (&it, w, first_row_to_display);
16660
16661 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16662 - start_vpos);
16663 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16664 - nrows_scrolled);
16665 it.current_y = (first_row_to_display->y - first_reusable_row->y
16666 + WINDOW_HEADER_LINE_HEIGHT (w));
16667
16668 /* Display lines beginning with first_row_to_display in the
16669 desired matrix. Set last_text_row to the last row displayed
16670 that displays text. */
16671 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16672 if (pt_row == NULL)
16673 w->cursor.vpos = -1;
16674 last_text_row = NULL;
16675 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16676 if (display_line (&it))
16677 last_text_row = it.glyph_row - 1;
16678
16679 /* If point is in a reused row, adjust y and vpos of the cursor
16680 position. */
16681 if (pt_row)
16682 {
16683 w->cursor.vpos -= nrows_scrolled;
16684 w->cursor.y -= first_reusable_row->y - start_row->y;
16685 }
16686
16687 /* Give up if point isn't in a row displayed or reused. (This
16688 also handles the case where w->cursor.vpos < nrows_scrolled
16689 after the calls to display_line, which can happen with scroll
16690 margins. See bug#1295.) */
16691 if (w->cursor.vpos < 0)
16692 {
16693 clear_glyph_matrix (w->desired_matrix);
16694 return 0;
16695 }
16696
16697 /* Scroll the display. */
16698 run.current_y = first_reusable_row->y;
16699 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16700 run.height = it.last_visible_y - run.current_y;
16701 dy = run.current_y - run.desired_y;
16702
16703 if (run.height)
16704 {
16705 update_begin (f);
16706 FRAME_RIF (f)->update_window_begin_hook (w);
16707 FRAME_RIF (f)->clear_window_mouse_face (w);
16708 FRAME_RIF (f)->scroll_run_hook (w, &run);
16709 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16710 update_end (f);
16711 }
16712
16713 /* Adjust Y positions of reused rows. */
16714 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16715 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16716 max_y = it.last_visible_y;
16717 for (row = first_reusable_row; row < first_row_to_display; ++row)
16718 {
16719 row->y -= dy;
16720 row->visible_height = row->height;
16721 if (row->y < min_y)
16722 row->visible_height -= min_y - row->y;
16723 if (row->y + row->height > max_y)
16724 row->visible_height -= row->y + row->height - max_y;
16725 if (row->fringe_bitmap_periodic_p)
16726 row->redraw_fringe_bitmaps_p = 1;
16727 }
16728
16729 /* Scroll the current matrix. */
16730 eassert (nrows_scrolled > 0);
16731 rotate_matrix (w->current_matrix,
16732 start_vpos,
16733 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16734 -nrows_scrolled);
16735
16736 /* Disable rows not reused. */
16737 for (row -= nrows_scrolled; row < bottom_row; ++row)
16738 row->enabled_p = 0;
16739
16740 /* Point may have moved to a different line, so we cannot assume that
16741 the previous cursor position is valid; locate the correct row. */
16742 if (pt_row)
16743 {
16744 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16745 row < bottom_row
16746 && PT >= MATRIX_ROW_END_CHARPOS (row)
16747 && !row->ends_at_zv_p;
16748 row++)
16749 {
16750 w->cursor.vpos++;
16751 w->cursor.y = row->y;
16752 }
16753 if (row < bottom_row)
16754 {
16755 /* Can't simply scan the row for point with
16756 bidi-reordered glyph rows. Let set_cursor_from_row
16757 figure out where to put the cursor, and if it fails,
16758 give up. */
16759 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16760 {
16761 if (!set_cursor_from_row (w, row, w->current_matrix,
16762 0, 0, 0, 0))
16763 {
16764 clear_glyph_matrix (w->desired_matrix);
16765 return 0;
16766 }
16767 }
16768 else
16769 {
16770 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16771 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16772
16773 for (; glyph < end
16774 && (!BUFFERP (glyph->object)
16775 || glyph->charpos < PT);
16776 glyph++)
16777 {
16778 w->cursor.hpos++;
16779 w->cursor.x += glyph->pixel_width;
16780 }
16781 }
16782 }
16783 }
16784
16785 /* Adjust window end. A null value of last_text_row means that
16786 the window end is in reused rows which in turn means that
16787 only its vpos can have changed. */
16788 if (last_text_row)
16789 adjust_window_ends (w, last_text_row, 0);
16790 else
16791 w->window_end_vpos -= nrows_scrolled;
16792
16793 w->window_end_valid = 0;
16794 w->desired_matrix->no_scrolling_p = 1;
16795
16796 #ifdef GLYPH_DEBUG
16797 debug_method_add (w, "try_window_reusing_current_matrix 2");
16798 #endif
16799 return 1;
16800 }
16801
16802 return 0;
16803 }
16804
16805
16806 \f
16807 /************************************************************************
16808 Window redisplay reusing current matrix when buffer has changed
16809 ************************************************************************/
16810
16811 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16812 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16813 ptrdiff_t *, ptrdiff_t *);
16814 static struct glyph_row *
16815 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16816 struct glyph_row *);
16817
16818
16819 /* Return the last row in MATRIX displaying text. If row START is
16820 non-null, start searching with that row. IT gives the dimensions
16821 of the display. Value is null if matrix is empty; otherwise it is
16822 a pointer to the row found. */
16823
16824 static struct glyph_row *
16825 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16826 struct glyph_row *start)
16827 {
16828 struct glyph_row *row, *row_found;
16829
16830 /* Set row_found to the last row in IT->w's current matrix
16831 displaying text. The loop looks funny but think of partially
16832 visible lines. */
16833 row_found = NULL;
16834 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16835 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16836 {
16837 eassert (row->enabled_p);
16838 row_found = row;
16839 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16840 break;
16841 ++row;
16842 }
16843
16844 return row_found;
16845 }
16846
16847
16848 /* Return the last row in the current matrix of W that is not affected
16849 by changes at the start of current_buffer that occurred since W's
16850 current matrix was built. Value is null if no such row exists.
16851
16852 BEG_UNCHANGED us the number of characters unchanged at the start of
16853 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16854 first changed character in current_buffer. Characters at positions <
16855 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16856 when the current matrix was built. */
16857
16858 static struct glyph_row *
16859 find_last_unchanged_at_beg_row (struct window *w)
16860 {
16861 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16862 struct glyph_row *row;
16863 struct glyph_row *row_found = NULL;
16864 int yb = window_text_bottom_y (w);
16865
16866 /* Find the last row displaying unchanged text. */
16867 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16868 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16869 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16870 ++row)
16871 {
16872 if (/* If row ends before first_changed_pos, it is unchanged,
16873 except in some case. */
16874 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16875 /* When row ends in ZV and we write at ZV it is not
16876 unchanged. */
16877 && !row->ends_at_zv_p
16878 /* When first_changed_pos is the end of a continued line,
16879 row is not unchanged because it may be no longer
16880 continued. */
16881 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16882 && (row->continued_p
16883 || row->exact_window_width_line_p))
16884 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16885 needs to be recomputed, so don't consider this row as
16886 unchanged. This happens when the last line was
16887 bidi-reordered and was killed immediately before this
16888 redisplay cycle. In that case, ROW->end stores the
16889 buffer position of the first visual-order character of
16890 the killed text, which is now beyond ZV. */
16891 && CHARPOS (row->end.pos) <= ZV)
16892 row_found = row;
16893
16894 /* Stop if last visible row. */
16895 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16896 break;
16897 }
16898
16899 return row_found;
16900 }
16901
16902
16903 /* Find the first glyph row in the current matrix of W that is not
16904 affected by changes at the end of current_buffer since the
16905 time W's current matrix was built.
16906
16907 Return in *DELTA the number of chars by which buffer positions in
16908 unchanged text at the end of current_buffer must be adjusted.
16909
16910 Return in *DELTA_BYTES the corresponding number of bytes.
16911
16912 Value is null if no such row exists, i.e. all rows are affected by
16913 changes. */
16914
16915 static struct glyph_row *
16916 find_first_unchanged_at_end_row (struct window *w,
16917 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16918 {
16919 struct glyph_row *row;
16920 struct glyph_row *row_found = NULL;
16921
16922 *delta = *delta_bytes = 0;
16923
16924 /* Display must not have been paused, otherwise the current matrix
16925 is not up to date. */
16926 eassert (w->window_end_valid);
16927
16928 /* A value of window_end_pos >= END_UNCHANGED means that the window
16929 end is in the range of changed text. If so, there is no
16930 unchanged row at the end of W's current matrix. */
16931 if (w->window_end_pos >= END_UNCHANGED)
16932 return NULL;
16933
16934 /* Set row to the last row in W's current matrix displaying text. */
16935 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16936
16937 /* If matrix is entirely empty, no unchanged row exists. */
16938 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16939 {
16940 /* The value of row is the last glyph row in the matrix having a
16941 meaningful buffer position in it. The end position of row
16942 corresponds to window_end_pos. This allows us to translate
16943 buffer positions in the current matrix to current buffer
16944 positions for characters not in changed text. */
16945 ptrdiff_t Z_old =
16946 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16947 ptrdiff_t Z_BYTE_old =
16948 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16949 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16950 struct glyph_row *first_text_row
16951 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16952
16953 *delta = Z - Z_old;
16954 *delta_bytes = Z_BYTE - Z_BYTE_old;
16955
16956 /* Set last_unchanged_pos to the buffer position of the last
16957 character in the buffer that has not been changed. Z is the
16958 index + 1 of the last character in current_buffer, i.e. by
16959 subtracting END_UNCHANGED we get the index of the last
16960 unchanged character, and we have to add BEG to get its buffer
16961 position. */
16962 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16963 last_unchanged_pos_old = last_unchanged_pos - *delta;
16964
16965 /* Search backward from ROW for a row displaying a line that
16966 starts at a minimum position >= last_unchanged_pos_old. */
16967 for (; row > first_text_row; --row)
16968 {
16969 /* This used to abort, but it can happen.
16970 It is ok to just stop the search instead here. KFS. */
16971 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16972 break;
16973
16974 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16975 row_found = row;
16976 }
16977 }
16978
16979 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16980
16981 return row_found;
16982 }
16983
16984
16985 /* Make sure that glyph rows in the current matrix of window W
16986 reference the same glyph memory as corresponding rows in the
16987 frame's frame matrix. This function is called after scrolling W's
16988 current matrix on a terminal frame in try_window_id and
16989 try_window_reusing_current_matrix. */
16990
16991 static void
16992 sync_frame_with_window_matrix_rows (struct window *w)
16993 {
16994 struct frame *f = XFRAME (w->frame);
16995 struct glyph_row *window_row, *window_row_end, *frame_row;
16996
16997 /* Preconditions: W must be a leaf window and full-width. Its frame
16998 must have a frame matrix. */
16999 eassert (BUFFERP (w->contents));
17000 eassert (WINDOW_FULL_WIDTH_P (w));
17001 eassert (!FRAME_WINDOW_P (f));
17002
17003 /* If W is a full-width window, glyph pointers in W's current matrix
17004 have, by definition, to be the same as glyph pointers in the
17005 corresponding frame matrix. Note that frame matrices have no
17006 marginal areas (see build_frame_matrix). */
17007 window_row = w->current_matrix->rows;
17008 window_row_end = window_row + w->current_matrix->nrows;
17009 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17010 while (window_row < window_row_end)
17011 {
17012 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17013 struct glyph *end = window_row->glyphs[LAST_AREA];
17014
17015 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17016 frame_row->glyphs[TEXT_AREA] = start;
17017 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17018 frame_row->glyphs[LAST_AREA] = end;
17019
17020 /* Disable frame rows whose corresponding window rows have
17021 been disabled in try_window_id. */
17022 if (!window_row->enabled_p)
17023 frame_row->enabled_p = 0;
17024
17025 ++window_row, ++frame_row;
17026 }
17027 }
17028
17029
17030 /* Find the glyph row in window W containing CHARPOS. Consider all
17031 rows between START and END (not inclusive). END null means search
17032 all rows to the end of the display area of W. Value is the row
17033 containing CHARPOS or null. */
17034
17035 struct glyph_row *
17036 row_containing_pos (struct window *w, ptrdiff_t charpos,
17037 struct glyph_row *start, struct glyph_row *end, int dy)
17038 {
17039 struct glyph_row *row = start;
17040 struct glyph_row *best_row = NULL;
17041 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17042 int last_y;
17043
17044 /* If we happen to start on a header-line, skip that. */
17045 if (row->mode_line_p)
17046 ++row;
17047
17048 if ((end && row >= end) || !row->enabled_p)
17049 return NULL;
17050
17051 last_y = window_text_bottom_y (w) - dy;
17052
17053 while (1)
17054 {
17055 /* Give up if we have gone too far. */
17056 if (end && row >= end)
17057 return NULL;
17058 /* This formerly returned if they were equal.
17059 I think that both quantities are of a "last plus one" type;
17060 if so, when they are equal, the row is within the screen. -- rms. */
17061 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17062 return NULL;
17063
17064 /* If it is in this row, return this row. */
17065 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17066 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17067 /* The end position of a row equals the start
17068 position of the next row. If CHARPOS is there, we
17069 would rather consider it displayed in the next
17070 line, except when this line ends in ZV. */
17071 && !row_for_charpos_p (row, charpos)))
17072 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17073 {
17074 struct glyph *g;
17075
17076 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17077 || (!best_row && !row->continued_p))
17078 return row;
17079 /* In bidi-reordered rows, there could be several rows whose
17080 edges surround CHARPOS, all of these rows belonging to
17081 the same continued line. We need to find the row which
17082 fits CHARPOS the best. */
17083 for (g = row->glyphs[TEXT_AREA];
17084 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17085 g++)
17086 {
17087 if (!STRINGP (g->object))
17088 {
17089 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17090 {
17091 mindif = eabs (g->charpos - charpos);
17092 best_row = row;
17093 /* Exact match always wins. */
17094 if (mindif == 0)
17095 return best_row;
17096 }
17097 }
17098 }
17099 }
17100 else if (best_row && !row->continued_p)
17101 return best_row;
17102 ++row;
17103 }
17104 }
17105
17106
17107 /* Try to redisplay window W by reusing its existing display. W's
17108 current matrix must be up to date when this function is called,
17109 i.e. window_end_valid must be nonzero.
17110
17111 Value is
17112
17113 1 if display has been updated
17114 0 if otherwise unsuccessful
17115 -1 if redisplay with same window start is known not to succeed
17116
17117 The following steps are performed:
17118
17119 1. Find the last row in the current matrix of W that is not
17120 affected by changes at the start of current_buffer. If no such row
17121 is found, give up.
17122
17123 2. Find the first row in W's current matrix that is not affected by
17124 changes at the end of current_buffer. Maybe there is no such row.
17125
17126 3. Display lines beginning with the row + 1 found in step 1 to the
17127 row found in step 2 or, if step 2 didn't find a row, to the end of
17128 the window.
17129
17130 4. If cursor is not known to appear on the window, give up.
17131
17132 5. If display stopped at the row found in step 2, scroll the
17133 display and current matrix as needed.
17134
17135 6. Maybe display some lines at the end of W, if we must. This can
17136 happen under various circumstances, like a partially visible line
17137 becoming fully visible, or because newly displayed lines are displayed
17138 in smaller font sizes.
17139
17140 7. Update W's window end information. */
17141
17142 static int
17143 try_window_id (struct window *w)
17144 {
17145 struct frame *f = XFRAME (w->frame);
17146 struct glyph_matrix *current_matrix = w->current_matrix;
17147 struct glyph_matrix *desired_matrix = w->desired_matrix;
17148 struct glyph_row *last_unchanged_at_beg_row;
17149 struct glyph_row *first_unchanged_at_end_row;
17150 struct glyph_row *row;
17151 struct glyph_row *bottom_row;
17152 int bottom_vpos;
17153 struct it it;
17154 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17155 int dvpos, dy;
17156 struct text_pos start_pos;
17157 struct run run;
17158 int first_unchanged_at_end_vpos = 0;
17159 struct glyph_row *last_text_row, *last_text_row_at_end;
17160 struct text_pos start;
17161 ptrdiff_t first_changed_charpos, last_changed_charpos;
17162
17163 #ifdef GLYPH_DEBUG
17164 if (inhibit_try_window_id)
17165 return 0;
17166 #endif
17167
17168 /* This is handy for debugging. */
17169 #if 0
17170 #define GIVE_UP(X) \
17171 do { \
17172 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17173 return 0; \
17174 } while (0)
17175 #else
17176 #define GIVE_UP(X) return 0
17177 #endif
17178
17179 SET_TEXT_POS_FROM_MARKER (start, w->start);
17180
17181 /* Don't use this for mini-windows because these can show
17182 messages and mini-buffers, and we don't handle that here. */
17183 if (MINI_WINDOW_P (w))
17184 GIVE_UP (1);
17185
17186 /* This flag is used to prevent redisplay optimizations. */
17187 if (windows_or_buffers_changed || f->cursor_type_changed)
17188 GIVE_UP (2);
17189
17190 /* Verify that narrowing has not changed.
17191 Also verify that we were not told to prevent redisplay optimizations.
17192 It would be nice to further
17193 reduce the number of cases where this prevents try_window_id. */
17194 if (current_buffer->clip_changed
17195 || current_buffer->prevent_redisplay_optimizations_p)
17196 GIVE_UP (3);
17197
17198 /* Window must either use window-based redisplay or be full width. */
17199 if (!FRAME_WINDOW_P (f)
17200 && (!FRAME_LINE_INS_DEL_OK (f)
17201 || !WINDOW_FULL_WIDTH_P (w)))
17202 GIVE_UP (4);
17203
17204 /* Give up if point is known NOT to appear in W. */
17205 if (PT < CHARPOS (start))
17206 GIVE_UP (5);
17207
17208 /* Another way to prevent redisplay optimizations. */
17209 if (w->last_modified == 0)
17210 GIVE_UP (6);
17211
17212 /* Verify that window is not hscrolled. */
17213 if (w->hscroll != 0)
17214 GIVE_UP (7);
17215
17216 /* Verify that display wasn't paused. */
17217 if (!w->window_end_valid)
17218 GIVE_UP (8);
17219
17220 /* Likewise if highlighting trailing whitespace. */
17221 if (!NILP (Vshow_trailing_whitespace))
17222 GIVE_UP (11);
17223
17224 /* Can't use this if overlay arrow position and/or string have
17225 changed. */
17226 if (overlay_arrows_changed_p ())
17227 GIVE_UP (12);
17228
17229 /* When word-wrap is on, adding a space to the first word of a
17230 wrapped line can change the wrap position, altering the line
17231 above it. It might be worthwhile to handle this more
17232 intelligently, but for now just redisplay from scratch. */
17233 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17234 GIVE_UP (21);
17235
17236 /* Under bidi reordering, adding or deleting a character in the
17237 beginning of a paragraph, before the first strong directional
17238 character, can change the base direction of the paragraph (unless
17239 the buffer specifies a fixed paragraph direction), which will
17240 require to redisplay the whole paragraph. It might be worthwhile
17241 to find the paragraph limits and widen the range of redisplayed
17242 lines to that, but for now just give up this optimization and
17243 redisplay from scratch. */
17244 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17245 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17246 GIVE_UP (22);
17247
17248 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17249 only if buffer has really changed. The reason is that the gap is
17250 initially at Z for freshly visited files. The code below would
17251 set end_unchanged to 0 in that case. */
17252 if (MODIFF > SAVE_MODIFF
17253 /* This seems to happen sometimes after saving a buffer. */
17254 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17255 {
17256 if (GPT - BEG < BEG_UNCHANGED)
17257 BEG_UNCHANGED = GPT - BEG;
17258 if (Z - GPT < END_UNCHANGED)
17259 END_UNCHANGED = Z - GPT;
17260 }
17261
17262 /* The position of the first and last character that has been changed. */
17263 first_changed_charpos = BEG + BEG_UNCHANGED;
17264 last_changed_charpos = Z - END_UNCHANGED;
17265
17266 /* If window starts after a line end, and the last change is in
17267 front of that newline, then changes don't affect the display.
17268 This case happens with stealth-fontification. Note that although
17269 the display is unchanged, glyph positions in the matrix have to
17270 be adjusted, of course. */
17271 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17272 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17273 && ((last_changed_charpos < CHARPOS (start)
17274 && CHARPOS (start) == BEGV)
17275 || (last_changed_charpos < CHARPOS (start) - 1
17276 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17277 {
17278 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17279 struct glyph_row *r0;
17280
17281 /* Compute how many chars/bytes have been added to or removed
17282 from the buffer. */
17283 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17284 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17285 Z_delta = Z - Z_old;
17286 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17287
17288 /* Give up if PT is not in the window. Note that it already has
17289 been checked at the start of try_window_id that PT is not in
17290 front of the window start. */
17291 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17292 GIVE_UP (13);
17293
17294 /* If window start is unchanged, we can reuse the whole matrix
17295 as is, after adjusting glyph positions. No need to compute
17296 the window end again, since its offset from Z hasn't changed. */
17297 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17298 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17299 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17300 /* PT must not be in a partially visible line. */
17301 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17302 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17303 {
17304 /* Adjust positions in the glyph matrix. */
17305 if (Z_delta || Z_delta_bytes)
17306 {
17307 struct glyph_row *r1
17308 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17309 increment_matrix_positions (w->current_matrix,
17310 MATRIX_ROW_VPOS (r0, current_matrix),
17311 MATRIX_ROW_VPOS (r1, current_matrix),
17312 Z_delta, Z_delta_bytes);
17313 }
17314
17315 /* Set the cursor. */
17316 row = row_containing_pos (w, PT, r0, NULL, 0);
17317 if (row)
17318 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17319 return 1;
17320 }
17321 }
17322
17323 /* Handle the case that changes are all below what is displayed in
17324 the window, and that PT is in the window. This shortcut cannot
17325 be taken if ZV is visible in the window, and text has been added
17326 there that is visible in the window. */
17327 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17328 /* ZV is not visible in the window, or there are no
17329 changes at ZV, actually. */
17330 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17331 || first_changed_charpos == last_changed_charpos))
17332 {
17333 struct glyph_row *r0;
17334
17335 /* Give up if PT is not in the window. Note that it already has
17336 been checked at the start of try_window_id that PT is not in
17337 front of the window start. */
17338 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17339 GIVE_UP (14);
17340
17341 /* If window start is unchanged, we can reuse the whole matrix
17342 as is, without changing glyph positions since no text has
17343 been added/removed in front of the window end. */
17344 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17345 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17346 /* PT must not be in a partially visible line. */
17347 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17348 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17349 {
17350 /* We have to compute the window end anew since text
17351 could have been added/removed after it. */
17352 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17353 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17354
17355 /* Set the cursor. */
17356 row = row_containing_pos (w, PT, r0, NULL, 0);
17357 if (row)
17358 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17359 return 2;
17360 }
17361 }
17362
17363 /* Give up if window start is in the changed area.
17364
17365 The condition used to read
17366
17367 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17368
17369 but why that was tested escapes me at the moment. */
17370 if (CHARPOS (start) >= first_changed_charpos
17371 && CHARPOS (start) <= last_changed_charpos)
17372 GIVE_UP (15);
17373
17374 /* Check that window start agrees with the start of the first glyph
17375 row in its current matrix. Check this after we know the window
17376 start is not in changed text, otherwise positions would not be
17377 comparable. */
17378 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17379 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17380 GIVE_UP (16);
17381
17382 /* Give up if the window ends in strings. Overlay strings
17383 at the end are difficult to handle, so don't try. */
17384 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17385 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17386 GIVE_UP (20);
17387
17388 /* Compute the position at which we have to start displaying new
17389 lines. Some of the lines at the top of the window might be
17390 reusable because they are not displaying changed text. Find the
17391 last row in W's current matrix not affected by changes at the
17392 start of current_buffer. Value is null if changes start in the
17393 first line of window. */
17394 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17395 if (last_unchanged_at_beg_row)
17396 {
17397 /* Avoid starting to display in the middle of a character, a TAB
17398 for instance. This is easier than to set up the iterator
17399 exactly, and it's not a frequent case, so the additional
17400 effort wouldn't really pay off. */
17401 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17402 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17403 && last_unchanged_at_beg_row > w->current_matrix->rows)
17404 --last_unchanged_at_beg_row;
17405
17406 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17407 GIVE_UP (17);
17408
17409 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17410 GIVE_UP (18);
17411 start_pos = it.current.pos;
17412
17413 /* Start displaying new lines in the desired matrix at the same
17414 vpos we would use in the current matrix, i.e. below
17415 last_unchanged_at_beg_row. */
17416 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17417 current_matrix);
17418 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17419 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17420
17421 eassert (it.hpos == 0 && it.current_x == 0);
17422 }
17423 else
17424 {
17425 /* There are no reusable lines at the start of the window.
17426 Start displaying in the first text line. */
17427 start_display (&it, w, start);
17428 it.vpos = it.first_vpos;
17429 start_pos = it.current.pos;
17430 }
17431
17432 /* Find the first row that is not affected by changes at the end of
17433 the buffer. Value will be null if there is no unchanged row, in
17434 which case we must redisplay to the end of the window. delta
17435 will be set to the value by which buffer positions beginning with
17436 first_unchanged_at_end_row have to be adjusted due to text
17437 changes. */
17438 first_unchanged_at_end_row
17439 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17440 IF_DEBUG (debug_delta = delta);
17441 IF_DEBUG (debug_delta_bytes = delta_bytes);
17442
17443 /* Set stop_pos to the buffer position up to which we will have to
17444 display new lines. If first_unchanged_at_end_row != NULL, this
17445 is the buffer position of the start of the line displayed in that
17446 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17447 that we don't stop at a buffer position. */
17448 stop_pos = 0;
17449 if (first_unchanged_at_end_row)
17450 {
17451 eassert (last_unchanged_at_beg_row == NULL
17452 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17453
17454 /* If this is a continuation line, move forward to the next one
17455 that isn't. Changes in lines above affect this line.
17456 Caution: this may move first_unchanged_at_end_row to a row
17457 not displaying text. */
17458 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17459 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17460 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17461 < it.last_visible_y))
17462 ++first_unchanged_at_end_row;
17463
17464 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17465 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17466 >= it.last_visible_y))
17467 first_unchanged_at_end_row = NULL;
17468 else
17469 {
17470 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17471 + delta);
17472 first_unchanged_at_end_vpos
17473 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17474 eassert (stop_pos >= Z - END_UNCHANGED);
17475 }
17476 }
17477 else if (last_unchanged_at_beg_row == NULL)
17478 GIVE_UP (19);
17479
17480
17481 #ifdef GLYPH_DEBUG
17482
17483 /* Either there is no unchanged row at the end, or the one we have
17484 now displays text. This is a necessary condition for the window
17485 end pos calculation at the end of this function. */
17486 eassert (first_unchanged_at_end_row == NULL
17487 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17488
17489 debug_last_unchanged_at_beg_vpos
17490 = (last_unchanged_at_beg_row
17491 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17492 : -1);
17493 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17494
17495 #endif /* GLYPH_DEBUG */
17496
17497
17498 /* Display new lines. Set last_text_row to the last new line
17499 displayed which has text on it, i.e. might end up as being the
17500 line where the window_end_vpos is. */
17501 w->cursor.vpos = -1;
17502 last_text_row = NULL;
17503 overlay_arrow_seen = 0;
17504 while (it.current_y < it.last_visible_y
17505 && !f->fonts_changed
17506 && (first_unchanged_at_end_row == NULL
17507 || IT_CHARPOS (it) < stop_pos))
17508 {
17509 if (display_line (&it))
17510 last_text_row = it.glyph_row - 1;
17511 }
17512
17513 if (f->fonts_changed)
17514 return -1;
17515
17516
17517 /* Compute differences in buffer positions, y-positions etc. for
17518 lines reused at the bottom of the window. Compute what we can
17519 scroll. */
17520 if (first_unchanged_at_end_row
17521 /* No lines reused because we displayed everything up to the
17522 bottom of the window. */
17523 && it.current_y < it.last_visible_y)
17524 {
17525 dvpos = (it.vpos
17526 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17527 current_matrix));
17528 dy = it.current_y - first_unchanged_at_end_row->y;
17529 run.current_y = first_unchanged_at_end_row->y;
17530 run.desired_y = run.current_y + dy;
17531 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17532 }
17533 else
17534 {
17535 delta = delta_bytes = dvpos = dy
17536 = run.current_y = run.desired_y = run.height = 0;
17537 first_unchanged_at_end_row = NULL;
17538 }
17539 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17540
17541
17542 /* Find the cursor if not already found. We have to decide whether
17543 PT will appear on this window (it sometimes doesn't, but this is
17544 not a very frequent case.) This decision has to be made before
17545 the current matrix is altered. A value of cursor.vpos < 0 means
17546 that PT is either in one of the lines beginning at
17547 first_unchanged_at_end_row or below the window. Don't care for
17548 lines that might be displayed later at the window end; as
17549 mentioned, this is not a frequent case. */
17550 if (w->cursor.vpos < 0)
17551 {
17552 /* Cursor in unchanged rows at the top? */
17553 if (PT < CHARPOS (start_pos)
17554 && last_unchanged_at_beg_row)
17555 {
17556 row = row_containing_pos (w, PT,
17557 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17558 last_unchanged_at_beg_row + 1, 0);
17559 if (row)
17560 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17561 }
17562
17563 /* Start from first_unchanged_at_end_row looking for PT. */
17564 else if (first_unchanged_at_end_row)
17565 {
17566 row = row_containing_pos (w, PT - delta,
17567 first_unchanged_at_end_row, NULL, 0);
17568 if (row)
17569 set_cursor_from_row (w, row, w->current_matrix, delta,
17570 delta_bytes, dy, dvpos);
17571 }
17572
17573 /* Give up if cursor was not found. */
17574 if (w->cursor.vpos < 0)
17575 {
17576 clear_glyph_matrix (w->desired_matrix);
17577 return -1;
17578 }
17579 }
17580
17581 /* Don't let the cursor end in the scroll margins. */
17582 {
17583 int this_scroll_margin, cursor_height;
17584 int frame_line_height = default_line_pixel_height (w);
17585 int window_total_lines
17586 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17587
17588 this_scroll_margin =
17589 max (0, min (scroll_margin, window_total_lines / 4));
17590 this_scroll_margin *= frame_line_height;
17591 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17592
17593 if ((w->cursor.y < this_scroll_margin
17594 && CHARPOS (start) > BEGV)
17595 /* Old redisplay didn't take scroll margin into account at the bottom,
17596 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17597 || (w->cursor.y + (make_cursor_line_fully_visible_p
17598 ? cursor_height + this_scroll_margin
17599 : 1)) > it.last_visible_y)
17600 {
17601 w->cursor.vpos = -1;
17602 clear_glyph_matrix (w->desired_matrix);
17603 return -1;
17604 }
17605 }
17606
17607 /* Scroll the display. Do it before changing the current matrix so
17608 that xterm.c doesn't get confused about where the cursor glyph is
17609 found. */
17610 if (dy && run.height)
17611 {
17612 update_begin (f);
17613
17614 if (FRAME_WINDOW_P (f))
17615 {
17616 FRAME_RIF (f)->update_window_begin_hook (w);
17617 FRAME_RIF (f)->clear_window_mouse_face (w);
17618 FRAME_RIF (f)->scroll_run_hook (w, &run);
17619 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17620 }
17621 else
17622 {
17623 /* Terminal frame. In this case, dvpos gives the number of
17624 lines to scroll by; dvpos < 0 means scroll up. */
17625 int from_vpos
17626 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17627 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17628 int end = (WINDOW_TOP_EDGE_LINE (w)
17629 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17630 + window_internal_height (w));
17631
17632 #if defined (HAVE_GPM) || defined (MSDOS)
17633 x_clear_window_mouse_face (w);
17634 #endif
17635 /* Perform the operation on the screen. */
17636 if (dvpos > 0)
17637 {
17638 /* Scroll last_unchanged_at_beg_row to the end of the
17639 window down dvpos lines. */
17640 set_terminal_window (f, end);
17641
17642 /* On dumb terminals delete dvpos lines at the end
17643 before inserting dvpos empty lines. */
17644 if (!FRAME_SCROLL_REGION_OK (f))
17645 ins_del_lines (f, end - dvpos, -dvpos);
17646
17647 /* Insert dvpos empty lines in front of
17648 last_unchanged_at_beg_row. */
17649 ins_del_lines (f, from, dvpos);
17650 }
17651 else if (dvpos < 0)
17652 {
17653 /* Scroll up last_unchanged_at_beg_vpos to the end of
17654 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17655 set_terminal_window (f, end);
17656
17657 /* Delete dvpos lines in front of
17658 last_unchanged_at_beg_vpos. ins_del_lines will set
17659 the cursor to the given vpos and emit |dvpos| delete
17660 line sequences. */
17661 ins_del_lines (f, from + dvpos, dvpos);
17662
17663 /* On a dumb terminal insert dvpos empty lines at the
17664 end. */
17665 if (!FRAME_SCROLL_REGION_OK (f))
17666 ins_del_lines (f, end + dvpos, -dvpos);
17667 }
17668
17669 set_terminal_window (f, 0);
17670 }
17671
17672 update_end (f);
17673 }
17674
17675 /* Shift reused rows of the current matrix to the right position.
17676 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17677 text. */
17678 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17679 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17680 if (dvpos < 0)
17681 {
17682 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17683 bottom_vpos, dvpos);
17684 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17685 bottom_vpos);
17686 }
17687 else if (dvpos > 0)
17688 {
17689 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17690 bottom_vpos, dvpos);
17691 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17692 first_unchanged_at_end_vpos + dvpos);
17693 }
17694
17695 /* For frame-based redisplay, make sure that current frame and window
17696 matrix are in sync with respect to glyph memory. */
17697 if (!FRAME_WINDOW_P (f))
17698 sync_frame_with_window_matrix_rows (w);
17699
17700 /* Adjust buffer positions in reused rows. */
17701 if (delta || delta_bytes)
17702 increment_matrix_positions (current_matrix,
17703 first_unchanged_at_end_vpos + dvpos,
17704 bottom_vpos, delta, delta_bytes);
17705
17706 /* Adjust Y positions. */
17707 if (dy)
17708 shift_glyph_matrix (w, current_matrix,
17709 first_unchanged_at_end_vpos + dvpos,
17710 bottom_vpos, dy);
17711
17712 if (first_unchanged_at_end_row)
17713 {
17714 first_unchanged_at_end_row += dvpos;
17715 if (first_unchanged_at_end_row->y >= it.last_visible_y
17716 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17717 first_unchanged_at_end_row = NULL;
17718 }
17719
17720 /* If scrolling up, there may be some lines to display at the end of
17721 the window. */
17722 last_text_row_at_end = NULL;
17723 if (dy < 0)
17724 {
17725 /* Scrolling up can leave for example a partially visible line
17726 at the end of the window to be redisplayed. */
17727 /* Set last_row to the glyph row in the current matrix where the
17728 window end line is found. It has been moved up or down in
17729 the matrix by dvpos. */
17730 int last_vpos = w->window_end_vpos + dvpos;
17731 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17732
17733 /* If last_row is the window end line, it should display text. */
17734 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17735
17736 /* If window end line was partially visible before, begin
17737 displaying at that line. Otherwise begin displaying with the
17738 line following it. */
17739 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17740 {
17741 init_to_row_start (&it, w, last_row);
17742 it.vpos = last_vpos;
17743 it.current_y = last_row->y;
17744 }
17745 else
17746 {
17747 init_to_row_end (&it, w, last_row);
17748 it.vpos = 1 + last_vpos;
17749 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17750 ++last_row;
17751 }
17752
17753 /* We may start in a continuation line. If so, we have to
17754 get the right continuation_lines_width and current_x. */
17755 it.continuation_lines_width = last_row->continuation_lines_width;
17756 it.hpos = it.current_x = 0;
17757
17758 /* Display the rest of the lines at the window end. */
17759 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17760 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17761 {
17762 /* Is it always sure that the display agrees with lines in
17763 the current matrix? I don't think so, so we mark rows
17764 displayed invalid in the current matrix by setting their
17765 enabled_p flag to zero. */
17766 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17767 if (display_line (&it))
17768 last_text_row_at_end = it.glyph_row - 1;
17769 }
17770 }
17771
17772 /* Update window_end_pos and window_end_vpos. */
17773 if (first_unchanged_at_end_row && !last_text_row_at_end)
17774 {
17775 /* Window end line if one of the preserved rows from the current
17776 matrix. Set row to the last row displaying text in current
17777 matrix starting at first_unchanged_at_end_row, after
17778 scrolling. */
17779 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17780 row = find_last_row_displaying_text (w->current_matrix, &it,
17781 first_unchanged_at_end_row);
17782 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17783 adjust_window_ends (w, row, 1);
17784 eassert (w->window_end_bytepos >= 0);
17785 IF_DEBUG (debug_method_add (w, "A"));
17786 }
17787 else if (last_text_row_at_end)
17788 {
17789 adjust_window_ends (w, last_text_row_at_end, 0);
17790 eassert (w->window_end_bytepos >= 0);
17791 IF_DEBUG (debug_method_add (w, "B"));
17792 }
17793 else if (last_text_row)
17794 {
17795 /* We have displayed either to the end of the window or at the
17796 end of the window, i.e. the last row with text is to be found
17797 in the desired matrix. */
17798 adjust_window_ends (w, last_text_row, 0);
17799 eassert (w->window_end_bytepos >= 0);
17800 }
17801 else if (first_unchanged_at_end_row == NULL
17802 && last_text_row == NULL
17803 && last_text_row_at_end == NULL)
17804 {
17805 /* Displayed to end of window, but no line containing text was
17806 displayed. Lines were deleted at the end of the window. */
17807 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17808 int vpos = w->window_end_vpos;
17809 struct glyph_row *current_row = current_matrix->rows + vpos;
17810 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17811
17812 for (row = NULL;
17813 row == NULL && vpos >= first_vpos;
17814 --vpos, --current_row, --desired_row)
17815 {
17816 if (desired_row->enabled_p)
17817 {
17818 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17819 row = desired_row;
17820 }
17821 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17822 row = current_row;
17823 }
17824
17825 eassert (row != NULL);
17826 w->window_end_vpos = vpos + 1;
17827 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17828 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17829 eassert (w->window_end_bytepos >= 0);
17830 IF_DEBUG (debug_method_add (w, "C"));
17831 }
17832 else
17833 emacs_abort ();
17834
17835 IF_DEBUG (debug_end_pos = w->window_end_pos;
17836 debug_end_vpos = w->window_end_vpos);
17837
17838 /* Record that display has not been completed. */
17839 w->window_end_valid = 0;
17840 w->desired_matrix->no_scrolling_p = 1;
17841 return 3;
17842
17843 #undef GIVE_UP
17844 }
17845
17846
17847 \f
17848 /***********************************************************************
17849 More debugging support
17850 ***********************************************************************/
17851
17852 #ifdef GLYPH_DEBUG
17853
17854 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17855 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17856 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17857
17858
17859 /* Dump the contents of glyph matrix MATRIX on stderr.
17860
17861 GLYPHS 0 means don't show glyph contents.
17862 GLYPHS 1 means show glyphs in short form
17863 GLYPHS > 1 means show glyphs in long form. */
17864
17865 void
17866 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17867 {
17868 int i;
17869 for (i = 0; i < matrix->nrows; ++i)
17870 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17871 }
17872
17873
17874 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17875 the glyph row and area where the glyph comes from. */
17876
17877 void
17878 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17879 {
17880 if (glyph->type == CHAR_GLYPH
17881 || glyph->type == GLYPHLESS_GLYPH)
17882 {
17883 fprintf (stderr,
17884 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17885 glyph - row->glyphs[TEXT_AREA],
17886 (glyph->type == CHAR_GLYPH
17887 ? 'C'
17888 : 'G'),
17889 glyph->charpos,
17890 (BUFFERP (glyph->object)
17891 ? 'B'
17892 : (STRINGP (glyph->object)
17893 ? 'S'
17894 : (INTEGERP (glyph->object)
17895 ? '0'
17896 : '-'))),
17897 glyph->pixel_width,
17898 glyph->u.ch,
17899 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17900 ? glyph->u.ch
17901 : '.'),
17902 glyph->face_id,
17903 glyph->left_box_line_p,
17904 glyph->right_box_line_p);
17905 }
17906 else if (glyph->type == STRETCH_GLYPH)
17907 {
17908 fprintf (stderr,
17909 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17910 glyph - row->glyphs[TEXT_AREA],
17911 'S',
17912 glyph->charpos,
17913 (BUFFERP (glyph->object)
17914 ? 'B'
17915 : (STRINGP (glyph->object)
17916 ? 'S'
17917 : (INTEGERP (glyph->object)
17918 ? '0'
17919 : '-'))),
17920 glyph->pixel_width,
17921 0,
17922 ' ',
17923 glyph->face_id,
17924 glyph->left_box_line_p,
17925 glyph->right_box_line_p);
17926 }
17927 else if (glyph->type == IMAGE_GLYPH)
17928 {
17929 fprintf (stderr,
17930 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17931 glyph - row->glyphs[TEXT_AREA],
17932 'I',
17933 glyph->charpos,
17934 (BUFFERP (glyph->object)
17935 ? 'B'
17936 : (STRINGP (glyph->object)
17937 ? 'S'
17938 : (INTEGERP (glyph->object)
17939 ? '0'
17940 : '-'))),
17941 glyph->pixel_width,
17942 glyph->u.img_id,
17943 '.',
17944 glyph->face_id,
17945 glyph->left_box_line_p,
17946 glyph->right_box_line_p);
17947 }
17948 else if (glyph->type == COMPOSITE_GLYPH)
17949 {
17950 fprintf (stderr,
17951 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17952 glyph - row->glyphs[TEXT_AREA],
17953 '+',
17954 glyph->charpos,
17955 (BUFFERP (glyph->object)
17956 ? 'B'
17957 : (STRINGP (glyph->object)
17958 ? 'S'
17959 : (INTEGERP (glyph->object)
17960 ? '0'
17961 : '-'))),
17962 glyph->pixel_width,
17963 glyph->u.cmp.id);
17964 if (glyph->u.cmp.automatic)
17965 fprintf (stderr,
17966 "[%d-%d]",
17967 glyph->slice.cmp.from, glyph->slice.cmp.to);
17968 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17969 glyph->face_id,
17970 glyph->left_box_line_p,
17971 glyph->right_box_line_p);
17972 }
17973 }
17974
17975
17976 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17977 GLYPHS 0 means don't show glyph contents.
17978 GLYPHS 1 means show glyphs in short form
17979 GLYPHS > 1 means show glyphs in long form. */
17980
17981 void
17982 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17983 {
17984 if (glyphs != 1)
17985 {
17986 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17987 fprintf (stderr, "==============================================================================\n");
17988
17989 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17990 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17991 vpos,
17992 MATRIX_ROW_START_CHARPOS (row),
17993 MATRIX_ROW_END_CHARPOS (row),
17994 row->used[TEXT_AREA],
17995 row->contains_overlapping_glyphs_p,
17996 row->enabled_p,
17997 row->truncated_on_left_p,
17998 row->truncated_on_right_p,
17999 row->continued_p,
18000 MATRIX_ROW_CONTINUATION_LINE_P (row),
18001 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18002 row->ends_at_zv_p,
18003 row->fill_line_p,
18004 row->ends_in_middle_of_char_p,
18005 row->starts_in_middle_of_char_p,
18006 row->mouse_face_p,
18007 row->x,
18008 row->y,
18009 row->pixel_width,
18010 row->height,
18011 row->visible_height,
18012 row->ascent,
18013 row->phys_ascent);
18014 /* The next 3 lines should align to "Start" in the header. */
18015 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18016 row->end.overlay_string_index,
18017 row->continuation_lines_width);
18018 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18019 CHARPOS (row->start.string_pos),
18020 CHARPOS (row->end.string_pos));
18021 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18022 row->end.dpvec_index);
18023 }
18024
18025 if (glyphs > 1)
18026 {
18027 int area;
18028
18029 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18030 {
18031 struct glyph *glyph = row->glyphs[area];
18032 struct glyph *glyph_end = glyph + row->used[area];
18033
18034 /* Glyph for a line end in text. */
18035 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18036 ++glyph_end;
18037
18038 if (glyph < glyph_end)
18039 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18040
18041 for (; glyph < glyph_end; ++glyph)
18042 dump_glyph (row, glyph, area);
18043 }
18044 }
18045 else if (glyphs == 1)
18046 {
18047 int area;
18048
18049 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18050 {
18051 char *s = alloca (row->used[area] + 4);
18052 int i;
18053
18054 for (i = 0; i < row->used[area]; ++i)
18055 {
18056 struct glyph *glyph = row->glyphs[area] + i;
18057 if (i == row->used[area] - 1
18058 && area == TEXT_AREA
18059 && INTEGERP (glyph->object)
18060 && glyph->type == CHAR_GLYPH
18061 && glyph->u.ch == ' ')
18062 {
18063 strcpy (&s[i], "[\\n]");
18064 i += 4;
18065 }
18066 else if (glyph->type == CHAR_GLYPH
18067 && glyph->u.ch < 0x80
18068 && glyph->u.ch >= ' ')
18069 s[i] = glyph->u.ch;
18070 else
18071 s[i] = '.';
18072 }
18073
18074 s[i] = '\0';
18075 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18076 }
18077 }
18078 }
18079
18080
18081 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18082 Sdump_glyph_matrix, 0, 1, "p",
18083 doc: /* Dump the current matrix of the selected window to stderr.
18084 Shows contents of glyph row structures. With non-nil
18085 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18086 glyphs in short form, otherwise show glyphs in long form. */)
18087 (Lisp_Object glyphs)
18088 {
18089 struct window *w = XWINDOW (selected_window);
18090 struct buffer *buffer = XBUFFER (w->contents);
18091
18092 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18093 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18094 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18095 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18096 fprintf (stderr, "=============================================\n");
18097 dump_glyph_matrix (w->current_matrix,
18098 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18099 return Qnil;
18100 }
18101
18102
18103 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18104 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18105 (void)
18106 {
18107 struct frame *f = XFRAME (selected_frame);
18108 dump_glyph_matrix (f->current_matrix, 1);
18109 return Qnil;
18110 }
18111
18112
18113 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18114 doc: /* Dump glyph row ROW to stderr.
18115 GLYPH 0 means don't dump glyphs.
18116 GLYPH 1 means dump glyphs in short form.
18117 GLYPH > 1 or omitted means dump glyphs in long form. */)
18118 (Lisp_Object row, Lisp_Object glyphs)
18119 {
18120 struct glyph_matrix *matrix;
18121 EMACS_INT vpos;
18122
18123 CHECK_NUMBER (row);
18124 matrix = XWINDOW (selected_window)->current_matrix;
18125 vpos = XINT (row);
18126 if (vpos >= 0 && vpos < matrix->nrows)
18127 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18128 vpos,
18129 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18130 return Qnil;
18131 }
18132
18133
18134 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18135 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18136 GLYPH 0 means don't dump glyphs.
18137 GLYPH 1 means dump glyphs in short form.
18138 GLYPH > 1 or omitted means dump glyphs in long form.
18139
18140 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18141 do nothing. */)
18142 (Lisp_Object row, Lisp_Object glyphs)
18143 {
18144 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18145 struct frame *sf = SELECTED_FRAME ();
18146 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18147 EMACS_INT vpos;
18148
18149 CHECK_NUMBER (row);
18150 vpos = XINT (row);
18151 if (vpos >= 0 && vpos < m->nrows)
18152 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18153 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18154 #endif
18155 return Qnil;
18156 }
18157
18158
18159 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18160 doc: /* Toggle tracing of redisplay.
18161 With ARG, turn tracing on if and only if ARG is positive. */)
18162 (Lisp_Object arg)
18163 {
18164 if (NILP (arg))
18165 trace_redisplay_p = !trace_redisplay_p;
18166 else
18167 {
18168 arg = Fprefix_numeric_value (arg);
18169 trace_redisplay_p = XINT (arg) > 0;
18170 }
18171
18172 return Qnil;
18173 }
18174
18175
18176 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18177 doc: /* Like `format', but print result to stderr.
18178 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18179 (ptrdiff_t nargs, Lisp_Object *args)
18180 {
18181 Lisp_Object s = Fformat (nargs, args);
18182 fprintf (stderr, "%s", SDATA (s));
18183 return Qnil;
18184 }
18185
18186 #endif /* GLYPH_DEBUG */
18187
18188
18189 \f
18190 /***********************************************************************
18191 Building Desired Matrix Rows
18192 ***********************************************************************/
18193
18194 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18195 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18196
18197 static struct glyph_row *
18198 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18199 {
18200 struct frame *f = XFRAME (WINDOW_FRAME (w));
18201 struct buffer *buffer = XBUFFER (w->contents);
18202 struct buffer *old = current_buffer;
18203 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18204 int arrow_len = SCHARS (overlay_arrow_string);
18205 const unsigned char *arrow_end = arrow_string + arrow_len;
18206 const unsigned char *p;
18207 struct it it;
18208 bool multibyte_p;
18209 int n_glyphs_before;
18210
18211 set_buffer_temp (buffer);
18212 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18213 it.glyph_row->used[TEXT_AREA] = 0;
18214 SET_TEXT_POS (it.position, 0, 0);
18215
18216 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18217 p = arrow_string;
18218 while (p < arrow_end)
18219 {
18220 Lisp_Object face, ilisp;
18221
18222 /* Get the next character. */
18223 if (multibyte_p)
18224 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18225 else
18226 {
18227 it.c = it.char_to_display = *p, it.len = 1;
18228 if (! ASCII_CHAR_P (it.c))
18229 it.char_to_display = BYTE8_TO_CHAR (it.c);
18230 }
18231 p += it.len;
18232
18233 /* Get its face. */
18234 ilisp = make_number (p - arrow_string);
18235 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18236 it.face_id = compute_char_face (f, it.char_to_display, face);
18237
18238 /* Compute its width, get its glyphs. */
18239 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18240 SET_TEXT_POS (it.position, -1, -1);
18241 PRODUCE_GLYPHS (&it);
18242
18243 /* If this character doesn't fit any more in the line, we have
18244 to remove some glyphs. */
18245 if (it.current_x > it.last_visible_x)
18246 {
18247 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18248 break;
18249 }
18250 }
18251
18252 set_buffer_temp (old);
18253 return it.glyph_row;
18254 }
18255
18256
18257 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18258 glyphs to insert is determined by produce_special_glyphs. */
18259
18260 static void
18261 insert_left_trunc_glyphs (struct it *it)
18262 {
18263 struct it truncate_it;
18264 struct glyph *from, *end, *to, *toend;
18265
18266 eassert (!FRAME_WINDOW_P (it->f)
18267 || (!it->glyph_row->reversed_p
18268 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18269 || (it->glyph_row->reversed_p
18270 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18271
18272 /* Get the truncation glyphs. */
18273 truncate_it = *it;
18274 truncate_it.current_x = 0;
18275 truncate_it.face_id = DEFAULT_FACE_ID;
18276 truncate_it.glyph_row = &scratch_glyph_row;
18277 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18278 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18279 truncate_it.object = make_number (0);
18280 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18281
18282 /* Overwrite glyphs from IT with truncation glyphs. */
18283 if (!it->glyph_row->reversed_p)
18284 {
18285 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18286
18287 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18288 end = from + tused;
18289 to = it->glyph_row->glyphs[TEXT_AREA];
18290 toend = to + it->glyph_row->used[TEXT_AREA];
18291 if (FRAME_WINDOW_P (it->f))
18292 {
18293 /* On GUI frames, when variable-size fonts are displayed,
18294 the truncation glyphs may need more pixels than the row's
18295 glyphs they overwrite. We overwrite more glyphs to free
18296 enough screen real estate, and enlarge the stretch glyph
18297 on the right (see display_line), if there is one, to
18298 preserve the screen position of the truncation glyphs on
18299 the right. */
18300 int w = 0;
18301 struct glyph *g = to;
18302 short used;
18303
18304 /* The first glyph could be partially visible, in which case
18305 it->glyph_row->x will be negative. But we want the left
18306 truncation glyphs to be aligned at the left margin of the
18307 window, so we override the x coordinate at which the row
18308 will begin. */
18309 it->glyph_row->x = 0;
18310 while (g < toend && w < it->truncation_pixel_width)
18311 {
18312 w += g->pixel_width;
18313 ++g;
18314 }
18315 if (g - to - tused > 0)
18316 {
18317 memmove (to + tused, g, (toend - g) * sizeof(*g));
18318 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18319 }
18320 used = it->glyph_row->used[TEXT_AREA];
18321 if (it->glyph_row->truncated_on_right_p
18322 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18323 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18324 == STRETCH_GLYPH)
18325 {
18326 int extra = w - it->truncation_pixel_width;
18327
18328 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18329 }
18330 }
18331
18332 while (from < end)
18333 *to++ = *from++;
18334
18335 /* There may be padding glyphs left over. Overwrite them too. */
18336 if (!FRAME_WINDOW_P (it->f))
18337 {
18338 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18339 {
18340 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18341 while (from < end)
18342 *to++ = *from++;
18343 }
18344 }
18345
18346 if (to > toend)
18347 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18348 }
18349 else
18350 {
18351 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18352
18353 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18354 that back to front. */
18355 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18356 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18357 toend = it->glyph_row->glyphs[TEXT_AREA];
18358 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18359 if (FRAME_WINDOW_P (it->f))
18360 {
18361 int w = 0;
18362 struct glyph *g = to;
18363
18364 while (g >= toend && w < it->truncation_pixel_width)
18365 {
18366 w += g->pixel_width;
18367 --g;
18368 }
18369 if (to - g - tused > 0)
18370 to = g + tused;
18371 if (it->glyph_row->truncated_on_right_p
18372 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18373 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18374 {
18375 int extra = w - it->truncation_pixel_width;
18376
18377 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18378 }
18379 }
18380
18381 while (from >= end && to >= toend)
18382 *to-- = *from--;
18383 if (!FRAME_WINDOW_P (it->f))
18384 {
18385 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18386 {
18387 from =
18388 truncate_it.glyph_row->glyphs[TEXT_AREA]
18389 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18390 while (from >= end && to >= toend)
18391 *to-- = *from--;
18392 }
18393 }
18394 if (from >= end)
18395 {
18396 /* Need to free some room before prepending additional
18397 glyphs. */
18398 int move_by = from - end + 1;
18399 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18400 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18401
18402 for ( ; g >= g0; g--)
18403 g[move_by] = *g;
18404 while (from >= end)
18405 *to-- = *from--;
18406 it->glyph_row->used[TEXT_AREA] += move_by;
18407 }
18408 }
18409 }
18410
18411 /* Compute the hash code for ROW. */
18412 unsigned
18413 row_hash (struct glyph_row *row)
18414 {
18415 int area, k;
18416 unsigned hashval = 0;
18417
18418 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18419 for (k = 0; k < row->used[area]; ++k)
18420 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18421 + row->glyphs[area][k].u.val
18422 + row->glyphs[area][k].face_id
18423 + row->glyphs[area][k].padding_p
18424 + (row->glyphs[area][k].type << 2));
18425
18426 return hashval;
18427 }
18428
18429 /* Compute the pixel height and width of IT->glyph_row.
18430
18431 Most of the time, ascent and height of a display line will be equal
18432 to the max_ascent and max_height values of the display iterator
18433 structure. This is not the case if
18434
18435 1. We hit ZV without displaying anything. In this case, max_ascent
18436 and max_height will be zero.
18437
18438 2. We have some glyphs that don't contribute to the line height.
18439 (The glyph row flag contributes_to_line_height_p is for future
18440 pixmap extensions).
18441
18442 The first case is easily covered by using default values because in
18443 these cases, the line height does not really matter, except that it
18444 must not be zero. */
18445
18446 static void
18447 compute_line_metrics (struct it *it)
18448 {
18449 struct glyph_row *row = it->glyph_row;
18450
18451 if (FRAME_WINDOW_P (it->f))
18452 {
18453 int i, min_y, max_y;
18454
18455 /* The line may consist of one space only, that was added to
18456 place the cursor on it. If so, the row's height hasn't been
18457 computed yet. */
18458 if (row->height == 0)
18459 {
18460 if (it->max_ascent + it->max_descent == 0)
18461 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18462 row->ascent = it->max_ascent;
18463 row->height = it->max_ascent + it->max_descent;
18464 row->phys_ascent = it->max_phys_ascent;
18465 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18466 row->extra_line_spacing = it->max_extra_line_spacing;
18467 }
18468
18469 /* Compute the width of this line. */
18470 row->pixel_width = row->x;
18471 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18472 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18473
18474 eassert (row->pixel_width >= 0);
18475 eassert (row->ascent >= 0 && row->height > 0);
18476
18477 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18478 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18479
18480 /* If first line's physical ascent is larger than its logical
18481 ascent, use the physical ascent, and make the row taller.
18482 This makes accented characters fully visible. */
18483 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18484 && row->phys_ascent > row->ascent)
18485 {
18486 row->height += row->phys_ascent - row->ascent;
18487 row->ascent = row->phys_ascent;
18488 }
18489
18490 /* Compute how much of the line is visible. */
18491 row->visible_height = row->height;
18492
18493 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18494 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18495
18496 if (row->y < min_y)
18497 row->visible_height -= min_y - row->y;
18498 if (row->y + row->height > max_y)
18499 row->visible_height -= row->y + row->height - max_y;
18500 }
18501 else
18502 {
18503 row->pixel_width = row->used[TEXT_AREA];
18504 if (row->continued_p)
18505 row->pixel_width -= it->continuation_pixel_width;
18506 else if (row->truncated_on_right_p)
18507 row->pixel_width -= it->truncation_pixel_width;
18508 row->ascent = row->phys_ascent = 0;
18509 row->height = row->phys_height = row->visible_height = 1;
18510 row->extra_line_spacing = 0;
18511 }
18512
18513 /* Compute a hash code for this row. */
18514 row->hash = row_hash (row);
18515
18516 it->max_ascent = it->max_descent = 0;
18517 it->max_phys_ascent = it->max_phys_descent = 0;
18518 }
18519
18520
18521 /* Append one space to the glyph row of iterator IT if doing a
18522 window-based redisplay. The space has the same face as
18523 IT->face_id. Value is non-zero if a space was added.
18524
18525 This function is called to make sure that there is always one glyph
18526 at the end of a glyph row that the cursor can be set on under
18527 window-systems. (If there weren't such a glyph we would not know
18528 how wide and tall a box cursor should be displayed).
18529
18530 At the same time this space let's a nicely handle clearing to the
18531 end of the line if the row ends in italic text. */
18532
18533 static int
18534 append_space_for_newline (struct it *it, int default_face_p)
18535 {
18536 if (FRAME_WINDOW_P (it->f))
18537 {
18538 int n = it->glyph_row->used[TEXT_AREA];
18539
18540 if (it->glyph_row->glyphs[TEXT_AREA] + n
18541 < it->glyph_row->glyphs[1 + TEXT_AREA])
18542 {
18543 /* Save some values that must not be changed.
18544 Must save IT->c and IT->len because otherwise
18545 ITERATOR_AT_END_P wouldn't work anymore after
18546 append_space_for_newline has been called. */
18547 enum display_element_type saved_what = it->what;
18548 int saved_c = it->c, saved_len = it->len;
18549 int saved_char_to_display = it->char_to_display;
18550 int saved_x = it->current_x;
18551 int saved_face_id = it->face_id;
18552 int saved_box_end = it->end_of_box_run_p;
18553 struct text_pos saved_pos;
18554 Lisp_Object saved_object;
18555 struct face *face;
18556
18557 saved_object = it->object;
18558 saved_pos = it->position;
18559
18560 it->what = IT_CHARACTER;
18561 memset (&it->position, 0, sizeof it->position);
18562 it->object = make_number (0);
18563 it->c = it->char_to_display = ' ';
18564 it->len = 1;
18565
18566 /* If the default face was remapped, be sure to use the
18567 remapped face for the appended newline. */
18568 if (default_face_p)
18569 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18570 else if (it->face_before_selective_p)
18571 it->face_id = it->saved_face_id;
18572 face = FACE_FROM_ID (it->f, it->face_id);
18573 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18574 /* In R2L rows, we will prepend a stretch glyph that will
18575 have the end_of_box_run_p flag set for it, so there's no
18576 need for the appended newline glyph to have that flag
18577 set. */
18578 if (it->glyph_row->reversed_p
18579 /* But if the appended newline glyph goes all the way to
18580 the end of the row, there will be no stretch glyph,
18581 so leave the box flag set. */
18582 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18583 it->end_of_box_run_p = 0;
18584
18585 PRODUCE_GLYPHS (it);
18586
18587 it->override_ascent = -1;
18588 it->constrain_row_ascent_descent_p = 0;
18589 it->current_x = saved_x;
18590 it->object = saved_object;
18591 it->position = saved_pos;
18592 it->what = saved_what;
18593 it->face_id = saved_face_id;
18594 it->len = saved_len;
18595 it->c = saved_c;
18596 it->char_to_display = saved_char_to_display;
18597 it->end_of_box_run_p = saved_box_end;
18598 return 1;
18599 }
18600 }
18601
18602 return 0;
18603 }
18604
18605
18606 /* Extend the face of the last glyph in the text area of IT->glyph_row
18607 to the end of the display line. Called from display_line. If the
18608 glyph row is empty, add a space glyph to it so that we know the
18609 face to draw. Set the glyph row flag fill_line_p. If the glyph
18610 row is R2L, prepend a stretch glyph to cover the empty space to the
18611 left of the leftmost glyph. */
18612
18613 static void
18614 extend_face_to_end_of_line (struct it *it)
18615 {
18616 struct face *face, *default_face;
18617 struct frame *f = it->f;
18618
18619 /* If line is already filled, do nothing. Non window-system frames
18620 get a grace of one more ``pixel'' because their characters are
18621 1-``pixel'' wide, so they hit the equality too early. This grace
18622 is needed only for R2L rows that are not continued, to produce
18623 one extra blank where we could display the cursor. */
18624 if (it->current_x >= it->last_visible_x
18625 + (!FRAME_WINDOW_P (f)
18626 && it->glyph_row->reversed_p
18627 && !it->glyph_row->continued_p))
18628 return;
18629
18630 /* The default face, possibly remapped. */
18631 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18632
18633 /* Face extension extends the background and box of IT->face_id
18634 to the end of the line. If the background equals the background
18635 of the frame, we don't have to do anything. */
18636 if (it->face_before_selective_p)
18637 face = FACE_FROM_ID (f, it->saved_face_id);
18638 else
18639 face = FACE_FROM_ID (f, it->face_id);
18640
18641 if (FRAME_WINDOW_P (f)
18642 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18643 && face->box == FACE_NO_BOX
18644 && face->background == FRAME_BACKGROUND_PIXEL (f)
18645 #ifdef HAVE_WINDOW_SYSTEM
18646 && !face->stipple
18647 #endif
18648 && !it->glyph_row->reversed_p)
18649 return;
18650
18651 /* Set the glyph row flag indicating that the face of the last glyph
18652 in the text area has to be drawn to the end of the text area. */
18653 it->glyph_row->fill_line_p = 1;
18654
18655 /* If current character of IT is not ASCII, make sure we have the
18656 ASCII face. This will be automatically undone the next time
18657 get_next_display_element returns a multibyte character. Note
18658 that the character will always be single byte in unibyte
18659 text. */
18660 if (!ASCII_CHAR_P (it->c))
18661 {
18662 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18663 }
18664
18665 if (FRAME_WINDOW_P (f))
18666 {
18667 /* If the row is empty, add a space with the current face of IT,
18668 so that we know which face to draw. */
18669 if (it->glyph_row->used[TEXT_AREA] == 0)
18670 {
18671 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18672 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18673 it->glyph_row->used[TEXT_AREA] = 1;
18674 }
18675 #ifdef HAVE_WINDOW_SYSTEM
18676 if (it->glyph_row->reversed_p)
18677 {
18678 /* Prepend a stretch glyph to the row, such that the
18679 rightmost glyph will be drawn flushed all the way to the
18680 right margin of the window. The stretch glyph that will
18681 occupy the empty space, if any, to the left of the
18682 glyphs. */
18683 struct font *font = face->font ? face->font : FRAME_FONT (f);
18684 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18685 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18686 struct glyph *g;
18687 int row_width, stretch_ascent, stretch_width;
18688 struct text_pos saved_pos;
18689 int saved_face_id, saved_avoid_cursor, saved_box_start;
18690
18691 for (row_width = 0, g = row_start; g < row_end; g++)
18692 row_width += g->pixel_width;
18693 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18694 if (stretch_width > 0)
18695 {
18696 stretch_ascent =
18697 (((it->ascent + it->descent)
18698 * FONT_BASE (font)) / FONT_HEIGHT (font));
18699 saved_pos = it->position;
18700 memset (&it->position, 0, sizeof it->position);
18701 saved_avoid_cursor = it->avoid_cursor_p;
18702 it->avoid_cursor_p = 1;
18703 saved_face_id = it->face_id;
18704 saved_box_start = it->start_of_box_run_p;
18705 /* The last row's stretch glyph should get the default
18706 face, to avoid painting the rest of the window with
18707 the region face, if the region ends at ZV. */
18708 if (it->glyph_row->ends_at_zv_p)
18709 it->face_id = default_face->id;
18710 else
18711 it->face_id = face->id;
18712 it->start_of_box_run_p = 0;
18713 append_stretch_glyph (it, make_number (0), stretch_width,
18714 it->ascent + it->descent, stretch_ascent);
18715 it->position = saved_pos;
18716 it->avoid_cursor_p = saved_avoid_cursor;
18717 it->face_id = saved_face_id;
18718 it->start_of_box_run_p = saved_box_start;
18719 }
18720 }
18721 #endif /* HAVE_WINDOW_SYSTEM */
18722 }
18723 else
18724 {
18725 /* Save some values that must not be changed. */
18726 int saved_x = it->current_x;
18727 struct text_pos saved_pos;
18728 Lisp_Object saved_object;
18729 enum display_element_type saved_what = it->what;
18730 int saved_face_id = it->face_id;
18731
18732 saved_object = it->object;
18733 saved_pos = it->position;
18734
18735 it->what = IT_CHARACTER;
18736 memset (&it->position, 0, sizeof it->position);
18737 it->object = make_number (0);
18738 it->c = it->char_to_display = ' ';
18739 it->len = 1;
18740 /* The last row's blank glyphs should get the default face, to
18741 avoid painting the rest of the window with the region face,
18742 if the region ends at ZV. */
18743 if (it->glyph_row->ends_at_zv_p)
18744 it->face_id = default_face->id;
18745 else
18746 it->face_id = face->id;
18747
18748 PRODUCE_GLYPHS (it);
18749
18750 while (it->current_x <= it->last_visible_x)
18751 PRODUCE_GLYPHS (it);
18752
18753 /* Don't count these blanks really. It would let us insert a left
18754 truncation glyph below and make us set the cursor on them, maybe. */
18755 it->current_x = saved_x;
18756 it->object = saved_object;
18757 it->position = saved_pos;
18758 it->what = saved_what;
18759 it->face_id = saved_face_id;
18760 }
18761 }
18762
18763
18764 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18765 trailing whitespace. */
18766
18767 static int
18768 trailing_whitespace_p (ptrdiff_t charpos)
18769 {
18770 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18771 int c = 0;
18772
18773 while (bytepos < ZV_BYTE
18774 && (c = FETCH_CHAR (bytepos),
18775 c == ' ' || c == '\t'))
18776 ++bytepos;
18777
18778 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18779 {
18780 if (bytepos != PT_BYTE)
18781 return 1;
18782 }
18783 return 0;
18784 }
18785
18786
18787 /* Highlight trailing whitespace, if any, in ROW. */
18788
18789 static void
18790 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18791 {
18792 int used = row->used[TEXT_AREA];
18793
18794 if (used)
18795 {
18796 struct glyph *start = row->glyphs[TEXT_AREA];
18797 struct glyph *glyph = start + used - 1;
18798
18799 if (row->reversed_p)
18800 {
18801 /* Right-to-left rows need to be processed in the opposite
18802 direction, so swap the edge pointers. */
18803 glyph = start;
18804 start = row->glyphs[TEXT_AREA] + used - 1;
18805 }
18806
18807 /* Skip over glyphs inserted to display the cursor at the
18808 end of a line, for extending the face of the last glyph
18809 to the end of the line on terminals, and for truncation
18810 and continuation glyphs. */
18811 if (!row->reversed_p)
18812 {
18813 while (glyph >= start
18814 && glyph->type == CHAR_GLYPH
18815 && INTEGERP (glyph->object))
18816 --glyph;
18817 }
18818 else
18819 {
18820 while (glyph <= start
18821 && glyph->type == CHAR_GLYPH
18822 && INTEGERP (glyph->object))
18823 ++glyph;
18824 }
18825
18826 /* If last glyph is a space or stretch, and it's trailing
18827 whitespace, set the face of all trailing whitespace glyphs in
18828 IT->glyph_row to `trailing-whitespace'. */
18829 if ((row->reversed_p ? glyph <= start : glyph >= start)
18830 && BUFFERP (glyph->object)
18831 && (glyph->type == STRETCH_GLYPH
18832 || (glyph->type == CHAR_GLYPH
18833 && glyph->u.ch == ' '))
18834 && trailing_whitespace_p (glyph->charpos))
18835 {
18836 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18837 if (face_id < 0)
18838 return;
18839
18840 if (!row->reversed_p)
18841 {
18842 while (glyph >= start
18843 && BUFFERP (glyph->object)
18844 && (glyph->type == STRETCH_GLYPH
18845 || (glyph->type == CHAR_GLYPH
18846 && glyph->u.ch == ' ')))
18847 (glyph--)->face_id = face_id;
18848 }
18849 else
18850 {
18851 while (glyph <= start
18852 && BUFFERP (glyph->object)
18853 && (glyph->type == STRETCH_GLYPH
18854 || (glyph->type == CHAR_GLYPH
18855 && glyph->u.ch == ' ')))
18856 (glyph++)->face_id = face_id;
18857 }
18858 }
18859 }
18860 }
18861
18862
18863 /* Value is non-zero if glyph row ROW should be
18864 considered to hold the buffer position CHARPOS. */
18865
18866 static int
18867 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18868 {
18869 int result = 1;
18870
18871 if (charpos == CHARPOS (row->end.pos)
18872 || charpos == MATRIX_ROW_END_CHARPOS (row))
18873 {
18874 /* Suppose the row ends on a string.
18875 Unless the row is continued, that means it ends on a newline
18876 in the string. If it's anything other than a display string
18877 (e.g., a before-string from an overlay), we don't want the
18878 cursor there. (This heuristic seems to give the optimal
18879 behavior for the various types of multi-line strings.)
18880 One exception: if the string has `cursor' property on one of
18881 its characters, we _do_ want the cursor there. */
18882 if (CHARPOS (row->end.string_pos) >= 0)
18883 {
18884 if (row->continued_p)
18885 result = 1;
18886 else
18887 {
18888 /* Check for `display' property. */
18889 struct glyph *beg = row->glyphs[TEXT_AREA];
18890 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18891 struct glyph *glyph;
18892
18893 result = 0;
18894 for (glyph = end; glyph >= beg; --glyph)
18895 if (STRINGP (glyph->object))
18896 {
18897 Lisp_Object prop
18898 = Fget_char_property (make_number (charpos),
18899 Qdisplay, Qnil);
18900 result =
18901 (!NILP (prop)
18902 && display_prop_string_p (prop, glyph->object));
18903 /* If there's a `cursor' property on one of the
18904 string's characters, this row is a cursor row,
18905 even though this is not a display string. */
18906 if (!result)
18907 {
18908 Lisp_Object s = glyph->object;
18909
18910 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18911 {
18912 ptrdiff_t gpos = glyph->charpos;
18913
18914 if (!NILP (Fget_char_property (make_number (gpos),
18915 Qcursor, s)))
18916 {
18917 result = 1;
18918 break;
18919 }
18920 }
18921 }
18922 break;
18923 }
18924 }
18925 }
18926 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18927 {
18928 /* If the row ends in middle of a real character,
18929 and the line is continued, we want the cursor here.
18930 That's because CHARPOS (ROW->end.pos) would equal
18931 PT if PT is before the character. */
18932 if (!row->ends_in_ellipsis_p)
18933 result = row->continued_p;
18934 else
18935 /* If the row ends in an ellipsis, then
18936 CHARPOS (ROW->end.pos) will equal point after the
18937 invisible text. We want that position to be displayed
18938 after the ellipsis. */
18939 result = 0;
18940 }
18941 /* If the row ends at ZV, display the cursor at the end of that
18942 row instead of at the start of the row below. */
18943 else if (row->ends_at_zv_p)
18944 result = 1;
18945 else
18946 result = 0;
18947 }
18948
18949 return result;
18950 }
18951
18952 /* Value is non-zero if glyph row ROW should be
18953 used to hold the cursor. */
18954
18955 static int
18956 cursor_row_p (struct glyph_row *row)
18957 {
18958 return row_for_charpos_p (row, PT);
18959 }
18960
18961 \f
18962
18963 /* Push the property PROP so that it will be rendered at the current
18964 position in IT. Return 1 if PROP was successfully pushed, 0
18965 otherwise. Called from handle_line_prefix to handle the
18966 `line-prefix' and `wrap-prefix' properties. */
18967
18968 static int
18969 push_prefix_prop (struct it *it, Lisp_Object prop)
18970 {
18971 struct text_pos pos =
18972 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18973
18974 eassert (it->method == GET_FROM_BUFFER
18975 || it->method == GET_FROM_DISPLAY_VECTOR
18976 || it->method == GET_FROM_STRING);
18977
18978 /* We need to save the current buffer/string position, so it will be
18979 restored by pop_it, because iterate_out_of_display_property
18980 depends on that being set correctly, but some situations leave
18981 it->position not yet set when this function is called. */
18982 push_it (it, &pos);
18983
18984 if (STRINGP (prop))
18985 {
18986 if (SCHARS (prop) == 0)
18987 {
18988 pop_it (it);
18989 return 0;
18990 }
18991
18992 it->string = prop;
18993 it->string_from_prefix_prop_p = 1;
18994 it->multibyte_p = STRING_MULTIBYTE (it->string);
18995 it->current.overlay_string_index = -1;
18996 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18997 it->end_charpos = it->string_nchars = SCHARS (it->string);
18998 it->method = GET_FROM_STRING;
18999 it->stop_charpos = 0;
19000 it->prev_stop = 0;
19001 it->base_level_stop = 0;
19002
19003 /* Force paragraph direction to be that of the parent
19004 buffer/string. */
19005 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19006 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19007 else
19008 it->paragraph_embedding = L2R;
19009
19010 /* Set up the bidi iterator for this display string. */
19011 if (it->bidi_p)
19012 {
19013 it->bidi_it.string.lstring = it->string;
19014 it->bidi_it.string.s = NULL;
19015 it->bidi_it.string.schars = it->end_charpos;
19016 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19017 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19018 it->bidi_it.string.unibyte = !it->multibyte_p;
19019 it->bidi_it.w = it->w;
19020 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19021 }
19022 }
19023 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19024 {
19025 it->method = GET_FROM_STRETCH;
19026 it->object = prop;
19027 }
19028 #ifdef HAVE_WINDOW_SYSTEM
19029 else if (IMAGEP (prop))
19030 {
19031 it->what = IT_IMAGE;
19032 it->image_id = lookup_image (it->f, prop);
19033 it->method = GET_FROM_IMAGE;
19034 }
19035 #endif /* HAVE_WINDOW_SYSTEM */
19036 else
19037 {
19038 pop_it (it); /* bogus display property, give up */
19039 return 0;
19040 }
19041
19042 return 1;
19043 }
19044
19045 /* Return the character-property PROP at the current position in IT. */
19046
19047 static Lisp_Object
19048 get_it_property (struct it *it, Lisp_Object prop)
19049 {
19050 Lisp_Object position, object = it->object;
19051
19052 if (STRINGP (object))
19053 position = make_number (IT_STRING_CHARPOS (*it));
19054 else if (BUFFERP (object))
19055 {
19056 position = make_number (IT_CHARPOS (*it));
19057 object = it->window;
19058 }
19059 else
19060 return Qnil;
19061
19062 return Fget_char_property (position, prop, object);
19063 }
19064
19065 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19066
19067 static void
19068 handle_line_prefix (struct it *it)
19069 {
19070 Lisp_Object prefix;
19071
19072 if (it->continuation_lines_width > 0)
19073 {
19074 prefix = get_it_property (it, Qwrap_prefix);
19075 if (NILP (prefix))
19076 prefix = Vwrap_prefix;
19077 }
19078 else
19079 {
19080 prefix = get_it_property (it, Qline_prefix);
19081 if (NILP (prefix))
19082 prefix = Vline_prefix;
19083 }
19084 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19085 {
19086 /* If the prefix is wider than the window, and we try to wrap
19087 it, it would acquire its own wrap prefix, and so on till the
19088 iterator stack overflows. So, don't wrap the prefix. */
19089 it->line_wrap = TRUNCATE;
19090 it->avoid_cursor_p = 1;
19091 }
19092 }
19093
19094 \f
19095
19096 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19097 only for R2L lines from display_line and display_string, when they
19098 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19099 the line/string needs to be continued on the next glyph row. */
19100 static void
19101 unproduce_glyphs (struct it *it, int n)
19102 {
19103 struct glyph *glyph, *end;
19104
19105 eassert (it->glyph_row);
19106 eassert (it->glyph_row->reversed_p);
19107 eassert (it->area == TEXT_AREA);
19108 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19109
19110 if (n > it->glyph_row->used[TEXT_AREA])
19111 n = it->glyph_row->used[TEXT_AREA];
19112 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19113 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19114 for ( ; glyph < end; glyph++)
19115 glyph[-n] = *glyph;
19116 }
19117
19118 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19119 and ROW->maxpos. */
19120 static void
19121 find_row_edges (struct it *it, struct glyph_row *row,
19122 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19123 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19124 {
19125 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19126 lines' rows is implemented for bidi-reordered rows. */
19127
19128 /* ROW->minpos is the value of min_pos, the minimal buffer position
19129 we have in ROW, or ROW->start.pos if that is smaller. */
19130 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19131 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19132 else
19133 /* We didn't find buffer positions smaller than ROW->start, or
19134 didn't find _any_ valid buffer positions in any of the glyphs,
19135 so we must trust the iterator's computed positions. */
19136 row->minpos = row->start.pos;
19137 if (max_pos <= 0)
19138 {
19139 max_pos = CHARPOS (it->current.pos);
19140 max_bpos = BYTEPOS (it->current.pos);
19141 }
19142
19143 /* Here are the various use-cases for ending the row, and the
19144 corresponding values for ROW->maxpos:
19145
19146 Line ends in a newline from buffer eol_pos + 1
19147 Line is continued from buffer max_pos + 1
19148 Line is truncated on right it->current.pos
19149 Line ends in a newline from string max_pos + 1(*)
19150 (*) + 1 only when line ends in a forward scan
19151 Line is continued from string max_pos
19152 Line is continued from display vector max_pos
19153 Line is entirely from a string min_pos == max_pos
19154 Line is entirely from a display vector min_pos == max_pos
19155 Line that ends at ZV ZV
19156
19157 If you discover other use-cases, please add them here as
19158 appropriate. */
19159 if (row->ends_at_zv_p)
19160 row->maxpos = it->current.pos;
19161 else if (row->used[TEXT_AREA])
19162 {
19163 int seen_this_string = 0;
19164 struct glyph_row *r1 = row - 1;
19165
19166 /* Did we see the same display string on the previous row? */
19167 if (STRINGP (it->object)
19168 /* this is not the first row */
19169 && row > it->w->desired_matrix->rows
19170 /* previous row is not the header line */
19171 && !r1->mode_line_p
19172 /* previous row also ends in a newline from a string */
19173 && r1->ends_in_newline_from_string_p)
19174 {
19175 struct glyph *start, *end;
19176
19177 /* Search for the last glyph of the previous row that came
19178 from buffer or string. Depending on whether the row is
19179 L2R or R2L, we need to process it front to back or the
19180 other way round. */
19181 if (!r1->reversed_p)
19182 {
19183 start = r1->glyphs[TEXT_AREA];
19184 end = start + r1->used[TEXT_AREA];
19185 /* Glyphs inserted by redisplay have an integer (zero)
19186 as their object. */
19187 while (end > start
19188 && INTEGERP ((end - 1)->object)
19189 && (end - 1)->charpos <= 0)
19190 --end;
19191 if (end > start)
19192 {
19193 if (EQ ((end - 1)->object, it->object))
19194 seen_this_string = 1;
19195 }
19196 else
19197 /* If all the glyphs of the previous row were inserted
19198 by redisplay, it means the previous row was
19199 produced from a single newline, which is only
19200 possible if that newline came from the same string
19201 as the one which produced this ROW. */
19202 seen_this_string = 1;
19203 }
19204 else
19205 {
19206 end = r1->glyphs[TEXT_AREA] - 1;
19207 start = end + r1->used[TEXT_AREA];
19208 while (end < start
19209 && INTEGERP ((end + 1)->object)
19210 && (end + 1)->charpos <= 0)
19211 ++end;
19212 if (end < start)
19213 {
19214 if (EQ ((end + 1)->object, it->object))
19215 seen_this_string = 1;
19216 }
19217 else
19218 seen_this_string = 1;
19219 }
19220 }
19221 /* Take note of each display string that covers a newline only
19222 once, the first time we see it. This is for when a display
19223 string includes more than one newline in it. */
19224 if (row->ends_in_newline_from_string_p && !seen_this_string)
19225 {
19226 /* If we were scanning the buffer forward when we displayed
19227 the string, we want to account for at least one buffer
19228 position that belongs to this row (position covered by
19229 the display string), so that cursor positioning will
19230 consider this row as a candidate when point is at the end
19231 of the visual line represented by this row. This is not
19232 required when scanning back, because max_pos will already
19233 have a much larger value. */
19234 if (CHARPOS (row->end.pos) > max_pos)
19235 INC_BOTH (max_pos, max_bpos);
19236 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19237 }
19238 else if (CHARPOS (it->eol_pos) > 0)
19239 SET_TEXT_POS (row->maxpos,
19240 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19241 else if (row->continued_p)
19242 {
19243 /* If max_pos is different from IT's current position, it
19244 means IT->method does not belong to the display element
19245 at max_pos. However, it also means that the display
19246 element at max_pos was displayed in its entirety on this
19247 line, which is equivalent to saying that the next line
19248 starts at the next buffer position. */
19249 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19250 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19251 else
19252 {
19253 INC_BOTH (max_pos, max_bpos);
19254 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19255 }
19256 }
19257 else if (row->truncated_on_right_p)
19258 /* display_line already called reseat_at_next_visible_line_start,
19259 which puts the iterator at the beginning of the next line, in
19260 the logical order. */
19261 row->maxpos = it->current.pos;
19262 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19263 /* A line that is entirely from a string/image/stretch... */
19264 row->maxpos = row->minpos;
19265 else
19266 emacs_abort ();
19267 }
19268 else
19269 row->maxpos = it->current.pos;
19270 }
19271
19272 /* Construct the glyph row IT->glyph_row in the desired matrix of
19273 IT->w from text at the current position of IT. See dispextern.h
19274 for an overview of struct it. Value is non-zero if
19275 IT->glyph_row displays text, as opposed to a line displaying ZV
19276 only. */
19277
19278 static int
19279 display_line (struct it *it)
19280 {
19281 struct glyph_row *row = it->glyph_row;
19282 Lisp_Object overlay_arrow_string;
19283 struct it wrap_it;
19284 void *wrap_data = NULL;
19285 int may_wrap = 0, wrap_x IF_LINT (= 0);
19286 int wrap_row_used = -1;
19287 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19288 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19289 int wrap_row_extra_line_spacing IF_LINT (= 0);
19290 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19291 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19292 int cvpos;
19293 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19294 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19295
19296 /* We always start displaying at hpos zero even if hscrolled. */
19297 eassert (it->hpos == 0 && it->current_x == 0);
19298
19299 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19300 >= it->w->desired_matrix->nrows)
19301 {
19302 it->w->nrows_scale_factor++;
19303 it->f->fonts_changed = 1;
19304 return 0;
19305 }
19306
19307 /* Clear the result glyph row and enable it. */
19308 prepare_desired_row (row);
19309
19310 row->y = it->current_y;
19311 row->start = it->start;
19312 row->continuation_lines_width = it->continuation_lines_width;
19313 row->displays_text_p = 1;
19314 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19315 it->starts_in_middle_of_char_p = 0;
19316
19317 /* Arrange the overlays nicely for our purposes. Usually, we call
19318 display_line on only one line at a time, in which case this
19319 can't really hurt too much, or we call it on lines which appear
19320 one after another in the buffer, in which case all calls to
19321 recenter_overlay_lists but the first will be pretty cheap. */
19322 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19323
19324 /* Move over display elements that are not visible because we are
19325 hscrolled. This may stop at an x-position < IT->first_visible_x
19326 if the first glyph is partially visible or if we hit a line end. */
19327 if (it->current_x < it->first_visible_x)
19328 {
19329 enum move_it_result move_result;
19330
19331 this_line_min_pos = row->start.pos;
19332 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19333 MOVE_TO_POS | MOVE_TO_X);
19334 /* If we are under a large hscroll, move_it_in_display_line_to
19335 could hit the end of the line without reaching
19336 it->first_visible_x. Pretend that we did reach it. This is
19337 especially important on a TTY, where we will call
19338 extend_face_to_end_of_line, which needs to know how many
19339 blank glyphs to produce. */
19340 if (it->current_x < it->first_visible_x
19341 && (move_result == MOVE_NEWLINE_OR_CR
19342 || move_result == MOVE_POS_MATCH_OR_ZV))
19343 it->current_x = it->first_visible_x;
19344
19345 /* Record the smallest positions seen while we moved over
19346 display elements that are not visible. This is needed by
19347 redisplay_internal for optimizing the case where the cursor
19348 stays inside the same line. The rest of this function only
19349 considers positions that are actually displayed, so
19350 RECORD_MAX_MIN_POS will not otherwise record positions that
19351 are hscrolled to the left of the left edge of the window. */
19352 min_pos = CHARPOS (this_line_min_pos);
19353 min_bpos = BYTEPOS (this_line_min_pos);
19354 }
19355 else
19356 {
19357 /* We only do this when not calling `move_it_in_display_line_to'
19358 above, because move_it_in_display_line_to calls
19359 handle_line_prefix itself. */
19360 handle_line_prefix (it);
19361 }
19362
19363 /* Get the initial row height. This is either the height of the
19364 text hscrolled, if there is any, or zero. */
19365 row->ascent = it->max_ascent;
19366 row->height = it->max_ascent + it->max_descent;
19367 row->phys_ascent = it->max_phys_ascent;
19368 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19369 row->extra_line_spacing = it->max_extra_line_spacing;
19370
19371 /* Utility macro to record max and min buffer positions seen until now. */
19372 #define RECORD_MAX_MIN_POS(IT) \
19373 do \
19374 { \
19375 int composition_p = !STRINGP ((IT)->string) \
19376 && ((IT)->what == IT_COMPOSITION); \
19377 ptrdiff_t current_pos = \
19378 composition_p ? (IT)->cmp_it.charpos \
19379 : IT_CHARPOS (*(IT)); \
19380 ptrdiff_t current_bpos = \
19381 composition_p ? CHAR_TO_BYTE (current_pos) \
19382 : IT_BYTEPOS (*(IT)); \
19383 if (current_pos < min_pos) \
19384 { \
19385 min_pos = current_pos; \
19386 min_bpos = current_bpos; \
19387 } \
19388 if (IT_CHARPOS (*it) > max_pos) \
19389 { \
19390 max_pos = IT_CHARPOS (*it); \
19391 max_bpos = IT_BYTEPOS (*it); \
19392 } \
19393 } \
19394 while (0)
19395
19396 /* Loop generating characters. The loop is left with IT on the next
19397 character to display. */
19398 while (1)
19399 {
19400 int n_glyphs_before, hpos_before, x_before;
19401 int x, nglyphs;
19402 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19403
19404 /* Retrieve the next thing to display. Value is zero if end of
19405 buffer reached. */
19406 if (!get_next_display_element (it))
19407 {
19408 /* Maybe add a space at the end of this line that is used to
19409 display the cursor there under X. Set the charpos of the
19410 first glyph of blank lines not corresponding to any text
19411 to -1. */
19412 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19413 row->exact_window_width_line_p = 1;
19414 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19415 || row->used[TEXT_AREA] == 0)
19416 {
19417 row->glyphs[TEXT_AREA]->charpos = -1;
19418 row->displays_text_p = 0;
19419
19420 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19421 && (!MINI_WINDOW_P (it->w)
19422 || (minibuf_level && EQ (it->window, minibuf_window))))
19423 row->indicate_empty_line_p = 1;
19424 }
19425
19426 it->continuation_lines_width = 0;
19427 row->ends_at_zv_p = 1;
19428 /* A row that displays right-to-left text must always have
19429 its last face extended all the way to the end of line,
19430 even if this row ends in ZV, because we still write to
19431 the screen left to right. We also need to extend the
19432 last face if the default face is remapped to some
19433 different face, otherwise the functions that clear
19434 portions of the screen will clear with the default face's
19435 background color. */
19436 if (row->reversed_p
19437 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19438 extend_face_to_end_of_line (it);
19439 break;
19440 }
19441
19442 /* Now, get the metrics of what we want to display. This also
19443 generates glyphs in `row' (which is IT->glyph_row). */
19444 n_glyphs_before = row->used[TEXT_AREA];
19445 x = it->current_x;
19446
19447 /* Remember the line height so far in case the next element doesn't
19448 fit on the line. */
19449 if (it->line_wrap != TRUNCATE)
19450 {
19451 ascent = it->max_ascent;
19452 descent = it->max_descent;
19453 phys_ascent = it->max_phys_ascent;
19454 phys_descent = it->max_phys_descent;
19455
19456 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19457 {
19458 if (IT_DISPLAYING_WHITESPACE (it))
19459 may_wrap = 1;
19460 else if (may_wrap)
19461 {
19462 SAVE_IT (wrap_it, *it, wrap_data);
19463 wrap_x = x;
19464 wrap_row_used = row->used[TEXT_AREA];
19465 wrap_row_ascent = row->ascent;
19466 wrap_row_height = row->height;
19467 wrap_row_phys_ascent = row->phys_ascent;
19468 wrap_row_phys_height = row->phys_height;
19469 wrap_row_extra_line_spacing = row->extra_line_spacing;
19470 wrap_row_min_pos = min_pos;
19471 wrap_row_min_bpos = min_bpos;
19472 wrap_row_max_pos = max_pos;
19473 wrap_row_max_bpos = max_bpos;
19474 may_wrap = 0;
19475 }
19476 }
19477 }
19478
19479 PRODUCE_GLYPHS (it);
19480
19481 /* If this display element was in marginal areas, continue with
19482 the next one. */
19483 if (it->area != TEXT_AREA)
19484 {
19485 row->ascent = max (row->ascent, it->max_ascent);
19486 row->height = max (row->height, it->max_ascent + it->max_descent);
19487 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19488 row->phys_height = max (row->phys_height,
19489 it->max_phys_ascent + it->max_phys_descent);
19490 row->extra_line_spacing = max (row->extra_line_spacing,
19491 it->max_extra_line_spacing);
19492 set_iterator_to_next (it, 1);
19493 continue;
19494 }
19495
19496 /* Does the display element fit on the line? If we truncate
19497 lines, we should draw past the right edge of the window. If
19498 we don't truncate, we want to stop so that we can display the
19499 continuation glyph before the right margin. If lines are
19500 continued, there are two possible strategies for characters
19501 resulting in more than 1 glyph (e.g. tabs): Display as many
19502 glyphs as possible in this line and leave the rest for the
19503 continuation line, or display the whole element in the next
19504 line. Original redisplay did the former, so we do it also. */
19505 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19506 hpos_before = it->hpos;
19507 x_before = x;
19508
19509 if (/* Not a newline. */
19510 nglyphs > 0
19511 /* Glyphs produced fit entirely in the line. */
19512 && it->current_x < it->last_visible_x)
19513 {
19514 it->hpos += nglyphs;
19515 row->ascent = max (row->ascent, it->max_ascent);
19516 row->height = max (row->height, it->max_ascent + it->max_descent);
19517 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19518 row->phys_height = max (row->phys_height,
19519 it->max_phys_ascent + it->max_phys_descent);
19520 row->extra_line_spacing = max (row->extra_line_spacing,
19521 it->max_extra_line_spacing);
19522 if (it->current_x - it->pixel_width < it->first_visible_x)
19523 row->x = x - it->first_visible_x;
19524 /* Record the maximum and minimum buffer positions seen so
19525 far in glyphs that will be displayed by this row. */
19526 if (it->bidi_p)
19527 RECORD_MAX_MIN_POS (it);
19528 }
19529 else
19530 {
19531 int i, new_x;
19532 struct glyph *glyph;
19533
19534 for (i = 0; i < nglyphs; ++i, x = new_x)
19535 {
19536 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19537 new_x = x + glyph->pixel_width;
19538
19539 if (/* Lines are continued. */
19540 it->line_wrap != TRUNCATE
19541 && (/* Glyph doesn't fit on the line. */
19542 new_x > it->last_visible_x
19543 /* Or it fits exactly on a window system frame. */
19544 || (new_x == it->last_visible_x
19545 && FRAME_WINDOW_P (it->f)
19546 && (row->reversed_p
19547 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19548 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19549 {
19550 /* End of a continued line. */
19551
19552 if (it->hpos == 0
19553 || (new_x == it->last_visible_x
19554 && FRAME_WINDOW_P (it->f)
19555 && (row->reversed_p
19556 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19557 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19558 {
19559 /* Current glyph is the only one on the line or
19560 fits exactly on the line. We must continue
19561 the line because we can't draw the cursor
19562 after the glyph. */
19563 row->continued_p = 1;
19564 it->current_x = new_x;
19565 it->continuation_lines_width += new_x;
19566 ++it->hpos;
19567 if (i == nglyphs - 1)
19568 {
19569 /* If line-wrap is on, check if a previous
19570 wrap point was found. */
19571 if (wrap_row_used > 0
19572 /* Even if there is a previous wrap
19573 point, continue the line here as
19574 usual, if (i) the previous character
19575 was a space or tab AND (ii) the
19576 current character is not. */
19577 && (!may_wrap
19578 || IT_DISPLAYING_WHITESPACE (it)))
19579 goto back_to_wrap;
19580
19581 /* Record the maximum and minimum buffer
19582 positions seen so far in glyphs that will be
19583 displayed by this row. */
19584 if (it->bidi_p)
19585 RECORD_MAX_MIN_POS (it);
19586 set_iterator_to_next (it, 1);
19587 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19588 {
19589 if (!get_next_display_element (it))
19590 {
19591 row->exact_window_width_line_p = 1;
19592 it->continuation_lines_width = 0;
19593 row->continued_p = 0;
19594 row->ends_at_zv_p = 1;
19595 }
19596 else if (ITERATOR_AT_END_OF_LINE_P (it))
19597 {
19598 row->continued_p = 0;
19599 row->exact_window_width_line_p = 1;
19600 }
19601 }
19602 }
19603 else if (it->bidi_p)
19604 RECORD_MAX_MIN_POS (it);
19605 }
19606 else if (CHAR_GLYPH_PADDING_P (*glyph)
19607 && !FRAME_WINDOW_P (it->f))
19608 {
19609 /* A padding glyph that doesn't fit on this line.
19610 This means the whole character doesn't fit
19611 on the line. */
19612 if (row->reversed_p)
19613 unproduce_glyphs (it, row->used[TEXT_AREA]
19614 - n_glyphs_before);
19615 row->used[TEXT_AREA] = n_glyphs_before;
19616
19617 /* Fill the rest of the row with continuation
19618 glyphs like in 20.x. */
19619 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19620 < row->glyphs[1 + TEXT_AREA])
19621 produce_special_glyphs (it, IT_CONTINUATION);
19622
19623 row->continued_p = 1;
19624 it->current_x = x_before;
19625 it->continuation_lines_width += x_before;
19626
19627 /* Restore the height to what it was before the
19628 element not fitting on the line. */
19629 it->max_ascent = ascent;
19630 it->max_descent = descent;
19631 it->max_phys_ascent = phys_ascent;
19632 it->max_phys_descent = phys_descent;
19633 }
19634 else if (wrap_row_used > 0)
19635 {
19636 back_to_wrap:
19637 if (row->reversed_p)
19638 unproduce_glyphs (it,
19639 row->used[TEXT_AREA] - wrap_row_used);
19640 RESTORE_IT (it, &wrap_it, wrap_data);
19641 it->continuation_lines_width += wrap_x;
19642 row->used[TEXT_AREA] = wrap_row_used;
19643 row->ascent = wrap_row_ascent;
19644 row->height = wrap_row_height;
19645 row->phys_ascent = wrap_row_phys_ascent;
19646 row->phys_height = wrap_row_phys_height;
19647 row->extra_line_spacing = wrap_row_extra_line_spacing;
19648 min_pos = wrap_row_min_pos;
19649 min_bpos = wrap_row_min_bpos;
19650 max_pos = wrap_row_max_pos;
19651 max_bpos = wrap_row_max_bpos;
19652 row->continued_p = 1;
19653 row->ends_at_zv_p = 0;
19654 row->exact_window_width_line_p = 0;
19655 it->continuation_lines_width += x;
19656
19657 /* Make sure that a non-default face is extended
19658 up to the right margin of the window. */
19659 extend_face_to_end_of_line (it);
19660 }
19661 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19662 {
19663 /* A TAB that extends past the right edge of the
19664 window. This produces a single glyph on
19665 window system frames. We leave the glyph in
19666 this row and let it fill the row, but don't
19667 consume the TAB. */
19668 if ((row->reversed_p
19669 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19670 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19671 produce_special_glyphs (it, IT_CONTINUATION);
19672 it->continuation_lines_width += it->last_visible_x;
19673 row->ends_in_middle_of_char_p = 1;
19674 row->continued_p = 1;
19675 glyph->pixel_width = it->last_visible_x - x;
19676 it->starts_in_middle_of_char_p = 1;
19677 }
19678 else
19679 {
19680 /* Something other than a TAB that draws past
19681 the right edge of the window. Restore
19682 positions to values before the element. */
19683 if (row->reversed_p)
19684 unproduce_glyphs (it, row->used[TEXT_AREA]
19685 - (n_glyphs_before + i));
19686 row->used[TEXT_AREA] = n_glyphs_before + i;
19687
19688 /* Display continuation glyphs. */
19689 it->current_x = x_before;
19690 it->continuation_lines_width += x;
19691 if (!FRAME_WINDOW_P (it->f)
19692 || (row->reversed_p
19693 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19694 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19695 produce_special_glyphs (it, IT_CONTINUATION);
19696 row->continued_p = 1;
19697
19698 extend_face_to_end_of_line (it);
19699
19700 if (nglyphs > 1 && i > 0)
19701 {
19702 row->ends_in_middle_of_char_p = 1;
19703 it->starts_in_middle_of_char_p = 1;
19704 }
19705
19706 /* Restore the height to what it was before the
19707 element not fitting on the line. */
19708 it->max_ascent = ascent;
19709 it->max_descent = descent;
19710 it->max_phys_ascent = phys_ascent;
19711 it->max_phys_descent = phys_descent;
19712 }
19713
19714 break;
19715 }
19716 else if (new_x > it->first_visible_x)
19717 {
19718 /* Increment number of glyphs actually displayed. */
19719 ++it->hpos;
19720
19721 /* Record the maximum and minimum buffer positions
19722 seen so far in glyphs that will be displayed by
19723 this row. */
19724 if (it->bidi_p)
19725 RECORD_MAX_MIN_POS (it);
19726
19727 if (x < it->first_visible_x)
19728 /* Glyph is partially visible, i.e. row starts at
19729 negative X position. */
19730 row->x = x - it->first_visible_x;
19731 }
19732 else
19733 {
19734 /* Glyph is completely off the left margin of the
19735 window. This should not happen because of the
19736 move_it_in_display_line at the start of this
19737 function, unless the text display area of the
19738 window is empty. */
19739 eassert (it->first_visible_x <= it->last_visible_x);
19740 }
19741 }
19742 /* Even if this display element produced no glyphs at all,
19743 we want to record its position. */
19744 if (it->bidi_p && nglyphs == 0)
19745 RECORD_MAX_MIN_POS (it);
19746
19747 row->ascent = max (row->ascent, it->max_ascent);
19748 row->height = max (row->height, it->max_ascent + it->max_descent);
19749 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19750 row->phys_height = max (row->phys_height,
19751 it->max_phys_ascent + it->max_phys_descent);
19752 row->extra_line_spacing = max (row->extra_line_spacing,
19753 it->max_extra_line_spacing);
19754
19755 /* End of this display line if row is continued. */
19756 if (row->continued_p || row->ends_at_zv_p)
19757 break;
19758 }
19759
19760 at_end_of_line:
19761 /* Is this a line end? If yes, we're also done, after making
19762 sure that a non-default face is extended up to the right
19763 margin of the window. */
19764 if (ITERATOR_AT_END_OF_LINE_P (it))
19765 {
19766 int used_before = row->used[TEXT_AREA];
19767
19768 row->ends_in_newline_from_string_p = STRINGP (it->object);
19769
19770 /* Add a space at the end of the line that is used to
19771 display the cursor there. */
19772 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19773 append_space_for_newline (it, 0);
19774
19775 /* Extend the face to the end of the line. */
19776 extend_face_to_end_of_line (it);
19777
19778 /* Make sure we have the position. */
19779 if (used_before == 0)
19780 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19781
19782 /* Record the position of the newline, for use in
19783 find_row_edges. */
19784 it->eol_pos = it->current.pos;
19785
19786 /* Consume the line end. This skips over invisible lines. */
19787 set_iterator_to_next (it, 1);
19788 it->continuation_lines_width = 0;
19789 break;
19790 }
19791
19792 /* Proceed with next display element. Note that this skips
19793 over lines invisible because of selective display. */
19794 set_iterator_to_next (it, 1);
19795
19796 /* If we truncate lines, we are done when the last displayed
19797 glyphs reach past the right margin of the window. */
19798 if (it->line_wrap == TRUNCATE
19799 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19800 ? (it->current_x >= it->last_visible_x)
19801 : (it->current_x > it->last_visible_x)))
19802 {
19803 /* Maybe add truncation glyphs. */
19804 if (!FRAME_WINDOW_P (it->f)
19805 || (row->reversed_p
19806 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19807 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19808 {
19809 int i, n;
19810
19811 if (!row->reversed_p)
19812 {
19813 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19814 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19815 break;
19816 }
19817 else
19818 {
19819 for (i = 0; i < row->used[TEXT_AREA]; i++)
19820 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19821 break;
19822 /* Remove any padding glyphs at the front of ROW, to
19823 make room for the truncation glyphs we will be
19824 adding below. The loop below always inserts at
19825 least one truncation glyph, so also remove the
19826 last glyph added to ROW. */
19827 unproduce_glyphs (it, i + 1);
19828 /* Adjust i for the loop below. */
19829 i = row->used[TEXT_AREA] - (i + 1);
19830 }
19831
19832 it->current_x = x_before;
19833 if (!FRAME_WINDOW_P (it->f))
19834 {
19835 for (n = row->used[TEXT_AREA]; i < n; ++i)
19836 {
19837 row->used[TEXT_AREA] = i;
19838 produce_special_glyphs (it, IT_TRUNCATION);
19839 }
19840 }
19841 else
19842 {
19843 row->used[TEXT_AREA] = i;
19844 produce_special_glyphs (it, IT_TRUNCATION);
19845 }
19846 }
19847 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19848 {
19849 /* Don't truncate if we can overflow newline into fringe. */
19850 if (!get_next_display_element (it))
19851 {
19852 it->continuation_lines_width = 0;
19853 row->ends_at_zv_p = 1;
19854 row->exact_window_width_line_p = 1;
19855 break;
19856 }
19857 if (ITERATOR_AT_END_OF_LINE_P (it))
19858 {
19859 row->exact_window_width_line_p = 1;
19860 goto at_end_of_line;
19861 }
19862 it->current_x = x_before;
19863 }
19864
19865 row->truncated_on_right_p = 1;
19866 it->continuation_lines_width = 0;
19867 reseat_at_next_visible_line_start (it, 0);
19868 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19869 it->hpos = hpos_before;
19870 break;
19871 }
19872 }
19873
19874 if (wrap_data)
19875 bidi_unshelve_cache (wrap_data, 1);
19876
19877 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19878 at the left window margin. */
19879 if (it->first_visible_x
19880 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19881 {
19882 if (!FRAME_WINDOW_P (it->f)
19883 || (row->reversed_p
19884 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19885 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19886 insert_left_trunc_glyphs (it);
19887 row->truncated_on_left_p = 1;
19888 }
19889
19890 /* Remember the position at which this line ends.
19891
19892 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19893 cannot be before the call to find_row_edges below, since that is
19894 where these positions are determined. */
19895 row->end = it->current;
19896 if (!it->bidi_p)
19897 {
19898 row->minpos = row->start.pos;
19899 row->maxpos = row->end.pos;
19900 }
19901 else
19902 {
19903 /* ROW->minpos and ROW->maxpos must be the smallest and
19904 `1 + the largest' buffer positions in ROW. But if ROW was
19905 bidi-reordered, these two positions can be anywhere in the
19906 row, so we must determine them now. */
19907 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19908 }
19909
19910 /* If the start of this line is the overlay arrow-position, then
19911 mark this glyph row as the one containing the overlay arrow.
19912 This is clearly a mess with variable size fonts. It would be
19913 better to let it be displayed like cursors under X. */
19914 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19915 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19916 !NILP (overlay_arrow_string)))
19917 {
19918 /* Overlay arrow in window redisplay is a fringe bitmap. */
19919 if (STRINGP (overlay_arrow_string))
19920 {
19921 struct glyph_row *arrow_row
19922 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19923 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19924 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19925 struct glyph *p = row->glyphs[TEXT_AREA];
19926 struct glyph *p2, *end;
19927
19928 /* Copy the arrow glyphs. */
19929 while (glyph < arrow_end)
19930 *p++ = *glyph++;
19931
19932 /* Throw away padding glyphs. */
19933 p2 = p;
19934 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19935 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19936 ++p2;
19937 if (p2 > p)
19938 {
19939 while (p2 < end)
19940 *p++ = *p2++;
19941 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19942 }
19943 }
19944 else
19945 {
19946 eassert (INTEGERP (overlay_arrow_string));
19947 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19948 }
19949 overlay_arrow_seen = 1;
19950 }
19951
19952 /* Highlight trailing whitespace. */
19953 if (!NILP (Vshow_trailing_whitespace))
19954 highlight_trailing_whitespace (it->f, it->glyph_row);
19955
19956 /* Compute pixel dimensions of this line. */
19957 compute_line_metrics (it);
19958
19959 /* Implementation note: No changes in the glyphs of ROW or in their
19960 faces can be done past this point, because compute_line_metrics
19961 computes ROW's hash value and stores it within the glyph_row
19962 structure. */
19963
19964 /* Record whether this row ends inside an ellipsis. */
19965 row->ends_in_ellipsis_p
19966 = (it->method == GET_FROM_DISPLAY_VECTOR
19967 && it->ellipsis_p);
19968
19969 /* Save fringe bitmaps in this row. */
19970 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19971 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19972 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19973 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19974
19975 it->left_user_fringe_bitmap = 0;
19976 it->left_user_fringe_face_id = 0;
19977 it->right_user_fringe_bitmap = 0;
19978 it->right_user_fringe_face_id = 0;
19979
19980 /* Maybe set the cursor. */
19981 cvpos = it->w->cursor.vpos;
19982 if ((cvpos < 0
19983 /* In bidi-reordered rows, keep checking for proper cursor
19984 position even if one has been found already, because buffer
19985 positions in such rows change non-linearly with ROW->VPOS,
19986 when a line is continued. One exception: when we are at ZV,
19987 display cursor on the first suitable glyph row, since all
19988 the empty rows after that also have their position set to ZV. */
19989 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19990 lines' rows is implemented for bidi-reordered rows. */
19991 || (it->bidi_p
19992 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19993 && PT >= MATRIX_ROW_START_CHARPOS (row)
19994 && PT <= MATRIX_ROW_END_CHARPOS (row)
19995 && cursor_row_p (row))
19996 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19997
19998 /* Prepare for the next line. This line starts horizontally at (X
19999 HPOS) = (0 0). Vertical positions are incremented. As a
20000 convenience for the caller, IT->glyph_row is set to the next
20001 row to be used. */
20002 it->current_x = it->hpos = 0;
20003 it->current_y += row->height;
20004 SET_TEXT_POS (it->eol_pos, 0, 0);
20005 ++it->vpos;
20006 ++it->glyph_row;
20007 /* The next row should by default use the same value of the
20008 reversed_p flag as this one. set_iterator_to_next decides when
20009 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20010 the flag accordingly. */
20011 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20012 it->glyph_row->reversed_p = row->reversed_p;
20013 it->start = row->end;
20014 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20015
20016 #undef RECORD_MAX_MIN_POS
20017 }
20018
20019 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20020 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20021 doc: /* Return paragraph direction at point in BUFFER.
20022 Value is either `left-to-right' or `right-to-left'.
20023 If BUFFER is omitted or nil, it defaults to the current buffer.
20024
20025 Paragraph direction determines how the text in the paragraph is displayed.
20026 In left-to-right paragraphs, text begins at the left margin of the window
20027 and the reading direction is generally left to right. In right-to-left
20028 paragraphs, text begins at the right margin and is read from right to left.
20029
20030 See also `bidi-paragraph-direction'. */)
20031 (Lisp_Object buffer)
20032 {
20033 struct buffer *buf = current_buffer;
20034 struct buffer *old = buf;
20035
20036 if (! NILP (buffer))
20037 {
20038 CHECK_BUFFER (buffer);
20039 buf = XBUFFER (buffer);
20040 }
20041
20042 if (NILP (BVAR (buf, bidi_display_reordering))
20043 || NILP (BVAR (buf, enable_multibyte_characters))
20044 /* When we are loading loadup.el, the character property tables
20045 needed for bidi iteration are not yet available. */
20046 || !NILP (Vpurify_flag))
20047 return Qleft_to_right;
20048 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20049 return BVAR (buf, bidi_paragraph_direction);
20050 else
20051 {
20052 /* Determine the direction from buffer text. We could try to
20053 use current_matrix if it is up to date, but this seems fast
20054 enough as it is. */
20055 struct bidi_it itb;
20056 ptrdiff_t pos = BUF_PT (buf);
20057 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20058 int c;
20059 void *itb_data = bidi_shelve_cache ();
20060
20061 set_buffer_temp (buf);
20062 /* bidi_paragraph_init finds the base direction of the paragraph
20063 by searching forward from paragraph start. We need the base
20064 direction of the current or _previous_ paragraph, so we need
20065 to make sure we are within that paragraph. To that end, find
20066 the previous non-empty line. */
20067 if (pos >= ZV && pos > BEGV)
20068 DEC_BOTH (pos, bytepos);
20069 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20070 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20071 {
20072 while ((c = FETCH_BYTE (bytepos)) == '\n'
20073 || c == ' ' || c == '\t' || c == '\f')
20074 {
20075 if (bytepos <= BEGV_BYTE)
20076 break;
20077 bytepos--;
20078 pos--;
20079 }
20080 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20081 bytepos--;
20082 }
20083 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20084 itb.paragraph_dir = NEUTRAL_DIR;
20085 itb.string.s = NULL;
20086 itb.string.lstring = Qnil;
20087 itb.string.bufpos = 0;
20088 itb.string.unibyte = 0;
20089 /* We have no window to use here for ignoring window-specific
20090 overlays. Using NULL for window pointer will cause
20091 compute_display_string_pos to use the current buffer. */
20092 itb.w = NULL;
20093 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20094 bidi_unshelve_cache (itb_data, 0);
20095 set_buffer_temp (old);
20096 switch (itb.paragraph_dir)
20097 {
20098 case L2R:
20099 return Qleft_to_right;
20100 break;
20101 case R2L:
20102 return Qright_to_left;
20103 break;
20104 default:
20105 emacs_abort ();
20106 }
20107 }
20108 }
20109
20110 DEFUN ("move-point-visually", Fmove_point_visually,
20111 Smove_point_visually, 1, 1, 0,
20112 doc: /* Move point in the visual order in the specified DIRECTION.
20113 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20114 left.
20115
20116 Value is the new character position of point. */)
20117 (Lisp_Object direction)
20118 {
20119 struct window *w = XWINDOW (selected_window);
20120 struct buffer *b = XBUFFER (w->contents);
20121 struct glyph_row *row;
20122 int dir;
20123 Lisp_Object paragraph_dir;
20124
20125 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20126 (!(ROW)->continued_p \
20127 && INTEGERP ((GLYPH)->object) \
20128 && (GLYPH)->type == CHAR_GLYPH \
20129 && (GLYPH)->u.ch == ' ' \
20130 && (GLYPH)->charpos >= 0 \
20131 && !(GLYPH)->avoid_cursor_p)
20132
20133 CHECK_NUMBER (direction);
20134 dir = XINT (direction);
20135 if (dir > 0)
20136 dir = 1;
20137 else
20138 dir = -1;
20139
20140 /* If current matrix is up-to-date, we can use the information
20141 recorded in the glyphs, at least as long as the goal is on the
20142 screen. */
20143 if (w->window_end_valid
20144 && !windows_or_buffers_changed
20145 && b
20146 && !b->clip_changed
20147 && !b->prevent_redisplay_optimizations_p
20148 && !window_outdated (w)
20149 && w->cursor.vpos >= 0
20150 && w->cursor.vpos < w->current_matrix->nrows
20151 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20152 {
20153 struct glyph *g = row->glyphs[TEXT_AREA];
20154 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20155 struct glyph *gpt = g + w->cursor.hpos;
20156
20157 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20158 {
20159 if (BUFFERP (g->object) && g->charpos != PT)
20160 {
20161 SET_PT (g->charpos);
20162 w->cursor.vpos = -1;
20163 return make_number (PT);
20164 }
20165 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20166 {
20167 ptrdiff_t new_pos;
20168
20169 if (BUFFERP (gpt->object))
20170 {
20171 new_pos = PT;
20172 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20173 new_pos += (row->reversed_p ? -dir : dir);
20174 else
20175 new_pos -= (row->reversed_p ? -dir : dir);;
20176 }
20177 else if (BUFFERP (g->object))
20178 new_pos = g->charpos;
20179 else
20180 break;
20181 SET_PT (new_pos);
20182 w->cursor.vpos = -1;
20183 return make_number (PT);
20184 }
20185 else if (ROW_GLYPH_NEWLINE_P (row, g))
20186 {
20187 /* Glyphs inserted at the end of a non-empty line for
20188 positioning the cursor have zero charpos, so we must
20189 deduce the value of point by other means. */
20190 if (g->charpos > 0)
20191 SET_PT (g->charpos);
20192 else if (row->ends_at_zv_p && PT != ZV)
20193 SET_PT (ZV);
20194 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20195 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20196 else
20197 break;
20198 w->cursor.vpos = -1;
20199 return make_number (PT);
20200 }
20201 }
20202 if (g == e || INTEGERP (g->object))
20203 {
20204 if (row->truncated_on_left_p || row->truncated_on_right_p)
20205 goto simulate_display;
20206 if (!row->reversed_p)
20207 row += dir;
20208 else
20209 row -= dir;
20210 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20211 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20212 goto simulate_display;
20213
20214 if (dir > 0)
20215 {
20216 if (row->reversed_p && !row->continued_p)
20217 {
20218 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20219 w->cursor.vpos = -1;
20220 return make_number (PT);
20221 }
20222 g = row->glyphs[TEXT_AREA];
20223 e = g + row->used[TEXT_AREA];
20224 for ( ; g < e; g++)
20225 {
20226 if (BUFFERP (g->object)
20227 /* Empty lines have only one glyph, which stands
20228 for the newline, and whose charpos is the
20229 buffer position of the newline. */
20230 || ROW_GLYPH_NEWLINE_P (row, g)
20231 /* When the buffer ends in a newline, the line at
20232 EOB also has one glyph, but its charpos is -1. */
20233 || (row->ends_at_zv_p
20234 && !row->reversed_p
20235 && INTEGERP (g->object)
20236 && g->type == CHAR_GLYPH
20237 && g->u.ch == ' '))
20238 {
20239 if (g->charpos > 0)
20240 SET_PT (g->charpos);
20241 else if (!row->reversed_p
20242 && row->ends_at_zv_p
20243 && PT != ZV)
20244 SET_PT (ZV);
20245 else
20246 continue;
20247 w->cursor.vpos = -1;
20248 return make_number (PT);
20249 }
20250 }
20251 }
20252 else
20253 {
20254 if (!row->reversed_p && !row->continued_p)
20255 {
20256 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20257 w->cursor.vpos = -1;
20258 return make_number (PT);
20259 }
20260 e = row->glyphs[TEXT_AREA];
20261 g = e + row->used[TEXT_AREA] - 1;
20262 for ( ; g >= e; g--)
20263 {
20264 if (BUFFERP (g->object)
20265 || (ROW_GLYPH_NEWLINE_P (row, g)
20266 && g->charpos > 0)
20267 /* Empty R2L lines on GUI frames have the buffer
20268 position of the newline stored in the stretch
20269 glyph. */
20270 || g->type == STRETCH_GLYPH
20271 || (row->ends_at_zv_p
20272 && row->reversed_p
20273 && INTEGERP (g->object)
20274 && g->type == CHAR_GLYPH
20275 && g->u.ch == ' '))
20276 {
20277 if (g->charpos > 0)
20278 SET_PT (g->charpos);
20279 else if (row->reversed_p
20280 && row->ends_at_zv_p
20281 && PT != ZV)
20282 SET_PT (ZV);
20283 else
20284 continue;
20285 w->cursor.vpos = -1;
20286 return make_number (PT);
20287 }
20288 }
20289 }
20290 }
20291 }
20292
20293 simulate_display:
20294
20295 /* If we wind up here, we failed to move by using the glyphs, so we
20296 need to simulate display instead. */
20297
20298 if (b)
20299 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20300 else
20301 paragraph_dir = Qleft_to_right;
20302 if (EQ (paragraph_dir, Qright_to_left))
20303 dir = -dir;
20304 if (PT <= BEGV && dir < 0)
20305 xsignal0 (Qbeginning_of_buffer);
20306 else if (PT >= ZV && dir > 0)
20307 xsignal0 (Qend_of_buffer);
20308 else
20309 {
20310 struct text_pos pt;
20311 struct it it;
20312 int pt_x, target_x, pixel_width, pt_vpos;
20313 bool at_eol_p;
20314 bool overshoot_expected = false;
20315 bool target_is_eol_p = false;
20316
20317 /* Setup the arena. */
20318 SET_TEXT_POS (pt, PT, PT_BYTE);
20319 start_display (&it, w, pt);
20320
20321 if (it.cmp_it.id < 0
20322 && it.method == GET_FROM_STRING
20323 && it.area == TEXT_AREA
20324 && it.string_from_display_prop_p
20325 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20326 overshoot_expected = true;
20327
20328 /* Find the X coordinate of point. We start from the beginning
20329 of this or previous line to make sure we are before point in
20330 the logical order (since the move_it_* functions can only
20331 move forward). */
20332 reseat_at_previous_visible_line_start (&it);
20333 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20334 if (IT_CHARPOS (it) != PT)
20335 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20336 -1, -1, -1, MOVE_TO_POS);
20337 pt_x = it.current_x;
20338 pt_vpos = it.vpos;
20339 if (dir > 0 || overshoot_expected)
20340 {
20341 struct glyph_row *row = it.glyph_row;
20342
20343 /* When point is at beginning of line, we don't have
20344 information about the glyph there loaded into struct
20345 it. Calling get_next_display_element fixes that. */
20346 if (pt_x == 0)
20347 get_next_display_element (&it);
20348 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20349 it.glyph_row = NULL;
20350 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20351 it.glyph_row = row;
20352 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20353 it, lest it will become out of sync with it's buffer
20354 position. */
20355 it.current_x = pt_x;
20356 }
20357 else
20358 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20359 pixel_width = it.pixel_width;
20360 if (overshoot_expected && at_eol_p)
20361 pixel_width = 0;
20362 else if (pixel_width <= 0)
20363 pixel_width = 1;
20364
20365 /* If there's a display string at point, we are actually at the
20366 glyph to the left of point, so we need to correct the X
20367 coordinate. */
20368 if (overshoot_expected)
20369 pt_x += pixel_width;
20370
20371 /* Compute target X coordinate, either to the left or to the
20372 right of point. On TTY frames, all characters have the same
20373 pixel width of 1, so we can use that. On GUI frames we don't
20374 have an easy way of getting at the pixel width of the
20375 character to the left of point, so we use a different method
20376 of getting to that place. */
20377 if (dir > 0)
20378 target_x = pt_x + pixel_width;
20379 else
20380 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20381
20382 /* Target X coordinate could be one line above or below the line
20383 of point, in which case we need to adjust the target X
20384 coordinate. Also, if moving to the left, we need to begin at
20385 the left edge of the point's screen line. */
20386 if (dir < 0)
20387 {
20388 if (pt_x > 0)
20389 {
20390 start_display (&it, w, pt);
20391 reseat_at_previous_visible_line_start (&it);
20392 it.current_x = it.current_y = it.hpos = 0;
20393 if (pt_vpos != 0)
20394 move_it_by_lines (&it, pt_vpos);
20395 }
20396 else
20397 {
20398 move_it_by_lines (&it, -1);
20399 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20400 target_is_eol_p = true;
20401 }
20402 }
20403 else
20404 {
20405 if (at_eol_p
20406 || (target_x >= it.last_visible_x
20407 && it.line_wrap != TRUNCATE))
20408 {
20409 if (pt_x > 0)
20410 move_it_by_lines (&it, 0);
20411 move_it_by_lines (&it, 1);
20412 target_x = 0;
20413 }
20414 }
20415
20416 /* Move to the target X coordinate. */
20417 #ifdef HAVE_WINDOW_SYSTEM
20418 /* On GUI frames, as we don't know the X coordinate of the
20419 character to the left of point, moving point to the left
20420 requires walking, one grapheme cluster at a time, until we
20421 find ourself at a place immediately to the left of the
20422 character at point. */
20423 if (FRAME_WINDOW_P (it.f) && dir < 0)
20424 {
20425 struct text_pos new_pos = it.current.pos;
20426 enum move_it_result rc = MOVE_X_REACHED;
20427
20428 while (it.current_x + it.pixel_width <= target_x
20429 && rc == MOVE_X_REACHED)
20430 {
20431 int new_x = it.current_x + it.pixel_width;
20432
20433 new_pos = it.current.pos;
20434 if (new_x == it.current_x)
20435 new_x++;
20436 rc = move_it_in_display_line_to (&it, ZV, new_x,
20437 MOVE_TO_POS | MOVE_TO_X);
20438 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20439 break;
20440 }
20441 /* If we ended up on a composed character inside
20442 bidi-reordered text (e.g., Hebrew text with diacritics),
20443 the iterator gives us the buffer position of the last (in
20444 logical order) character of the composed grapheme cluster,
20445 which is not what we want. So we cheat: we compute the
20446 character position of the character that follows (in the
20447 logical order) the one where the above loop stopped. That
20448 character will appear on display to the left of point. */
20449 if (it.bidi_p
20450 && it.bidi_it.scan_dir == -1
20451 && new_pos.charpos - IT_CHARPOS (it) > 1)
20452 {
20453 new_pos.charpos = IT_CHARPOS (it) + 1;
20454 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20455 }
20456 it.current.pos = new_pos;
20457 }
20458 else
20459 #endif
20460 if (it.current_x != target_x)
20461 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20462
20463 /* When lines are truncated, the above loop will stop at the
20464 window edge. But we want to get to the end of line, even if
20465 it is beyond the window edge; automatic hscroll will then
20466 scroll the window to show point as appropriate. */
20467 if (target_is_eol_p && it.line_wrap == TRUNCATE
20468 && get_next_display_element (&it))
20469 {
20470 struct text_pos new_pos = it.current.pos;
20471
20472 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20473 {
20474 set_iterator_to_next (&it, 0);
20475 if (it.method == GET_FROM_BUFFER)
20476 new_pos = it.current.pos;
20477 if (!get_next_display_element (&it))
20478 break;
20479 }
20480
20481 it.current.pos = new_pos;
20482 }
20483
20484 /* If we ended up in a display string that covers point, move to
20485 buffer position to the right in the visual order. */
20486 if (dir > 0)
20487 {
20488 while (IT_CHARPOS (it) == PT)
20489 {
20490 set_iterator_to_next (&it, 0);
20491 if (!get_next_display_element (&it))
20492 break;
20493 }
20494 }
20495
20496 /* Move point to that position. */
20497 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20498 }
20499
20500 return make_number (PT);
20501
20502 #undef ROW_GLYPH_NEWLINE_P
20503 }
20504
20505 \f
20506 /***********************************************************************
20507 Menu Bar
20508 ***********************************************************************/
20509
20510 /* Redisplay the menu bar in the frame for window W.
20511
20512 The menu bar of X frames that don't have X toolkit support is
20513 displayed in a special window W->frame->menu_bar_window.
20514
20515 The menu bar of terminal frames is treated specially as far as
20516 glyph matrices are concerned. Menu bar lines are not part of
20517 windows, so the update is done directly on the frame matrix rows
20518 for the menu bar. */
20519
20520 static void
20521 display_menu_bar (struct window *w)
20522 {
20523 struct frame *f = XFRAME (WINDOW_FRAME (w));
20524 struct it it;
20525 Lisp_Object items;
20526 int i;
20527
20528 /* Don't do all this for graphical frames. */
20529 #ifdef HAVE_NTGUI
20530 if (FRAME_W32_P (f))
20531 return;
20532 #endif
20533 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20534 if (FRAME_X_P (f))
20535 return;
20536 #endif
20537
20538 #ifdef HAVE_NS
20539 if (FRAME_NS_P (f))
20540 return;
20541 #endif /* HAVE_NS */
20542
20543 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20544 eassert (!FRAME_WINDOW_P (f));
20545 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20546 it.first_visible_x = 0;
20547 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20548 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20549 if (FRAME_WINDOW_P (f))
20550 {
20551 /* Menu bar lines are displayed in the desired matrix of the
20552 dummy window menu_bar_window. */
20553 struct window *menu_w;
20554 menu_w = XWINDOW (f->menu_bar_window);
20555 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20556 MENU_FACE_ID);
20557 it.first_visible_x = 0;
20558 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20559 }
20560 else
20561 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20562 {
20563 /* This is a TTY frame, i.e. character hpos/vpos are used as
20564 pixel x/y. */
20565 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20566 MENU_FACE_ID);
20567 it.first_visible_x = 0;
20568 it.last_visible_x = FRAME_COLS (f);
20569 }
20570
20571 /* FIXME: This should be controlled by a user option. See the
20572 comments in redisplay_tool_bar and display_mode_line about
20573 this. */
20574 it.paragraph_embedding = L2R;
20575
20576 /* Clear all rows of the menu bar. */
20577 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20578 {
20579 struct glyph_row *row = it.glyph_row + i;
20580 clear_glyph_row (row);
20581 row->enabled_p = 1;
20582 row->full_width_p = 1;
20583 }
20584
20585 /* Display all items of the menu bar. */
20586 items = FRAME_MENU_BAR_ITEMS (it.f);
20587 for (i = 0; i < ASIZE (items); i += 4)
20588 {
20589 Lisp_Object string;
20590
20591 /* Stop at nil string. */
20592 string = AREF (items, i + 1);
20593 if (NILP (string))
20594 break;
20595
20596 /* Remember where item was displayed. */
20597 ASET (items, i + 3, make_number (it.hpos));
20598
20599 /* Display the item, pad with one space. */
20600 if (it.current_x < it.last_visible_x)
20601 display_string (NULL, string, Qnil, 0, 0, &it,
20602 SCHARS (string) + 1, 0, 0, -1);
20603 }
20604
20605 /* Fill out the line with spaces. */
20606 if (it.current_x < it.last_visible_x)
20607 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20608
20609 /* Compute the total height of the lines. */
20610 compute_line_metrics (&it);
20611 }
20612
20613 /* Deep copy of a glyph row, including the glyphs. */
20614 static void
20615 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20616 {
20617 struct glyph *pointers[1 + LAST_AREA];
20618 int to_used = to->used[TEXT_AREA];
20619
20620 /* Save glyph pointers of TO. */
20621 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20622
20623 /* Do a structure assignment. */
20624 *to = *from;
20625
20626 /* Restore original glyph pointers of TO. */
20627 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20628
20629 /* Copy the glyphs. */
20630 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20631 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20632
20633 /* If we filled only part of the TO row, fill the rest with
20634 space_glyph (which will display as empty space). */
20635 if (to_used > from->used[TEXT_AREA])
20636 fill_up_frame_row_with_spaces (to, to_used);
20637 }
20638
20639 /* Display one menu item on a TTY, by overwriting the glyphs in the
20640 frame F's desired glyph matrix with glyphs produced from the menu
20641 item text. Called from term.c to display TTY drop-down menus one
20642 item at a time.
20643
20644 ITEM_TEXT is the menu item text as a C string.
20645
20646 FACE_ID is the face ID to be used for this menu item. FACE_ID
20647 could specify one of 3 faces: a face for an enabled item, a face
20648 for a disabled item, or a face for a selected item.
20649
20650 X and Y are coordinates of the first glyph in the frame's desired
20651 matrix to be overwritten by the menu item. Since this is a TTY, Y
20652 is the zero-based number of the glyph row and X is the zero-based
20653 glyph number in the row, starting from left, where to start
20654 displaying the item.
20655
20656 SUBMENU non-zero means this menu item drops down a submenu, which
20657 should be indicated by displaying a proper visual cue after the
20658 item text. */
20659
20660 void
20661 display_tty_menu_item (const char *item_text, int width, int face_id,
20662 int x, int y, int submenu)
20663 {
20664 struct it it;
20665 struct frame *f = SELECTED_FRAME ();
20666 struct window *w = XWINDOW (f->selected_window);
20667 int saved_used, saved_truncated, saved_width, saved_reversed;
20668 struct glyph_row *row;
20669 size_t item_len = strlen (item_text);
20670
20671 eassert (FRAME_TERMCAP_P (f));
20672
20673 /* Don't write beyond the matrix's last row. This can happen for
20674 TTY screens that are not high enough to show the entire menu.
20675 (This is actually a bit of defensive programming, as
20676 tty_menu_display already limits the number of menu items to one
20677 less than the number of screen lines.) */
20678 if (y >= f->desired_matrix->nrows)
20679 return;
20680
20681 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20682 it.first_visible_x = 0;
20683 it.last_visible_x = FRAME_COLS (f) - 1;
20684 row = it.glyph_row;
20685 /* Start with the row contents from the current matrix. */
20686 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20687 saved_width = row->full_width_p;
20688 row->full_width_p = 1;
20689 saved_reversed = row->reversed_p;
20690 row->reversed_p = 0;
20691 row->enabled_p = 1;
20692
20693 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20694 desired face. */
20695 eassert (x < f->desired_matrix->matrix_w);
20696 it.current_x = it.hpos = x;
20697 it.current_y = it.vpos = y;
20698 saved_used = row->used[TEXT_AREA];
20699 saved_truncated = row->truncated_on_right_p;
20700 row->used[TEXT_AREA] = x;
20701 it.face_id = face_id;
20702 it.line_wrap = TRUNCATE;
20703
20704 /* FIXME: This should be controlled by a user option. See the
20705 comments in redisplay_tool_bar and display_mode_line about this.
20706 Also, if paragraph_embedding could ever be R2L, changes will be
20707 needed to avoid shifting to the right the row characters in
20708 term.c:append_glyph. */
20709 it.paragraph_embedding = L2R;
20710
20711 /* Pad with a space on the left. */
20712 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20713 width--;
20714 /* Display the menu item, pad with spaces to WIDTH. */
20715 if (submenu)
20716 {
20717 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20718 item_len, 0, FRAME_COLS (f) - 1, -1);
20719 width -= item_len;
20720 /* Indicate with " >" that there's a submenu. */
20721 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20722 FRAME_COLS (f) - 1, -1);
20723 }
20724 else
20725 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20726 width, 0, FRAME_COLS (f) - 1, -1);
20727
20728 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20729 row->truncated_on_right_p = saved_truncated;
20730 row->hash = row_hash (row);
20731 row->full_width_p = saved_width;
20732 row->reversed_p = saved_reversed;
20733 }
20734 \f
20735 /***********************************************************************
20736 Mode Line
20737 ***********************************************************************/
20738
20739 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20740 FORCE is non-zero, redisplay mode lines unconditionally.
20741 Otherwise, redisplay only mode lines that are garbaged. Value is
20742 the number of windows whose mode lines were redisplayed. */
20743
20744 static int
20745 redisplay_mode_lines (Lisp_Object window, bool force)
20746 {
20747 int nwindows = 0;
20748
20749 while (!NILP (window))
20750 {
20751 struct window *w = XWINDOW (window);
20752
20753 if (WINDOWP (w->contents))
20754 nwindows += redisplay_mode_lines (w->contents, force);
20755 else if (force
20756 || FRAME_GARBAGED_P (XFRAME (w->frame))
20757 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20758 {
20759 struct text_pos lpoint;
20760 struct buffer *old = current_buffer;
20761
20762 /* Set the window's buffer for the mode line display. */
20763 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20764 set_buffer_internal_1 (XBUFFER (w->contents));
20765
20766 /* Point refers normally to the selected window. For any
20767 other window, set up appropriate value. */
20768 if (!EQ (window, selected_window))
20769 {
20770 struct text_pos pt;
20771
20772 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20773 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20774 }
20775
20776 /* Display mode lines. */
20777 clear_glyph_matrix (w->desired_matrix);
20778 if (display_mode_lines (w))
20779 ++nwindows;
20780
20781 /* Restore old settings. */
20782 set_buffer_internal_1 (old);
20783 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20784 }
20785
20786 window = w->next;
20787 }
20788
20789 return nwindows;
20790 }
20791
20792
20793 /* Display the mode and/or header line of window W. Value is the
20794 sum number of mode lines and header lines displayed. */
20795
20796 static int
20797 display_mode_lines (struct window *w)
20798 {
20799 Lisp_Object old_selected_window = selected_window;
20800 Lisp_Object old_selected_frame = selected_frame;
20801 Lisp_Object new_frame = w->frame;
20802 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20803 int n = 0;
20804
20805 selected_frame = new_frame;
20806 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20807 or window's point, then we'd need select_window_1 here as well. */
20808 XSETWINDOW (selected_window, w);
20809 XFRAME (new_frame)->selected_window = selected_window;
20810
20811 /* These will be set while the mode line specs are processed. */
20812 line_number_displayed = 0;
20813 w->column_number_displayed = -1;
20814
20815 if (WINDOW_WANTS_MODELINE_P (w))
20816 {
20817 struct window *sel_w = XWINDOW (old_selected_window);
20818
20819 /* Select mode line face based on the real selected window. */
20820 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20821 BVAR (current_buffer, mode_line_format));
20822 ++n;
20823 }
20824
20825 if (WINDOW_WANTS_HEADER_LINE_P (w))
20826 {
20827 display_mode_line (w, HEADER_LINE_FACE_ID,
20828 BVAR (current_buffer, header_line_format));
20829 ++n;
20830 }
20831
20832 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20833 selected_frame = old_selected_frame;
20834 selected_window = old_selected_window;
20835 if (n > 0)
20836 w->must_be_updated_p = true;
20837 return n;
20838 }
20839
20840
20841 /* Display mode or header line of window W. FACE_ID specifies which
20842 line to display; it is either MODE_LINE_FACE_ID or
20843 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20844 display. Value is the pixel height of the mode/header line
20845 displayed. */
20846
20847 static int
20848 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20849 {
20850 struct it it;
20851 struct face *face;
20852 ptrdiff_t count = SPECPDL_INDEX ();
20853
20854 init_iterator (&it, w, -1, -1, NULL, face_id);
20855 /* Don't extend on a previously drawn mode-line.
20856 This may happen if called from pos_visible_p. */
20857 it.glyph_row->enabled_p = 0;
20858 prepare_desired_row (it.glyph_row);
20859
20860 it.glyph_row->mode_line_p = 1;
20861
20862 /* FIXME: This should be controlled by a user option. But
20863 supporting such an option is not trivial, since the mode line is
20864 made up of many separate strings. */
20865 it.paragraph_embedding = L2R;
20866
20867 record_unwind_protect (unwind_format_mode_line,
20868 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20869
20870 mode_line_target = MODE_LINE_DISPLAY;
20871
20872 /* Temporarily make frame's keyboard the current kboard so that
20873 kboard-local variables in the mode_line_format will get the right
20874 values. */
20875 push_kboard (FRAME_KBOARD (it.f));
20876 record_unwind_save_match_data ();
20877 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20878 pop_kboard ();
20879
20880 unbind_to (count, Qnil);
20881
20882 /* Fill up with spaces. */
20883 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20884
20885 compute_line_metrics (&it);
20886 it.glyph_row->full_width_p = 1;
20887 it.glyph_row->continued_p = 0;
20888 it.glyph_row->truncated_on_left_p = 0;
20889 it.glyph_row->truncated_on_right_p = 0;
20890
20891 /* Make a 3D mode-line have a shadow at its right end. */
20892 face = FACE_FROM_ID (it.f, face_id);
20893 extend_face_to_end_of_line (&it);
20894 if (face->box != FACE_NO_BOX)
20895 {
20896 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20897 + it.glyph_row->used[TEXT_AREA] - 1);
20898 last->right_box_line_p = 1;
20899 }
20900
20901 return it.glyph_row->height;
20902 }
20903
20904 /* Move element ELT in LIST to the front of LIST.
20905 Return the updated list. */
20906
20907 static Lisp_Object
20908 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20909 {
20910 register Lisp_Object tail, prev;
20911 register Lisp_Object tem;
20912
20913 tail = list;
20914 prev = Qnil;
20915 while (CONSP (tail))
20916 {
20917 tem = XCAR (tail);
20918
20919 if (EQ (elt, tem))
20920 {
20921 /* Splice out the link TAIL. */
20922 if (NILP (prev))
20923 list = XCDR (tail);
20924 else
20925 Fsetcdr (prev, XCDR (tail));
20926
20927 /* Now make it the first. */
20928 Fsetcdr (tail, list);
20929 return tail;
20930 }
20931 else
20932 prev = tail;
20933 tail = XCDR (tail);
20934 QUIT;
20935 }
20936
20937 /* Not found--return unchanged LIST. */
20938 return list;
20939 }
20940
20941 /* Contribute ELT to the mode line for window IT->w. How it
20942 translates into text depends on its data type.
20943
20944 IT describes the display environment in which we display, as usual.
20945
20946 DEPTH is the depth in recursion. It is used to prevent
20947 infinite recursion here.
20948
20949 FIELD_WIDTH is the number of characters the display of ELT should
20950 occupy in the mode line, and PRECISION is the maximum number of
20951 characters to display from ELT's representation. See
20952 display_string for details.
20953
20954 Returns the hpos of the end of the text generated by ELT.
20955
20956 PROPS is a property list to add to any string we encounter.
20957
20958 If RISKY is nonzero, remove (disregard) any properties in any string
20959 we encounter, and ignore :eval and :propertize.
20960
20961 The global variable `mode_line_target' determines whether the
20962 output is passed to `store_mode_line_noprop',
20963 `store_mode_line_string', or `display_string'. */
20964
20965 static int
20966 display_mode_element (struct it *it, int depth, int field_width, int precision,
20967 Lisp_Object elt, Lisp_Object props, int risky)
20968 {
20969 int n = 0, field, prec;
20970 int literal = 0;
20971
20972 tail_recurse:
20973 if (depth > 100)
20974 elt = build_string ("*too-deep*");
20975
20976 depth++;
20977
20978 switch (XTYPE (elt))
20979 {
20980 case Lisp_String:
20981 {
20982 /* A string: output it and check for %-constructs within it. */
20983 unsigned char c;
20984 ptrdiff_t offset = 0;
20985
20986 if (SCHARS (elt) > 0
20987 && (!NILP (props) || risky))
20988 {
20989 Lisp_Object oprops, aelt;
20990 oprops = Ftext_properties_at (make_number (0), elt);
20991
20992 /* If the starting string's properties are not what
20993 we want, translate the string. Also, if the string
20994 is risky, do that anyway. */
20995
20996 if (NILP (Fequal (props, oprops)) || risky)
20997 {
20998 /* If the starting string has properties,
20999 merge the specified ones onto the existing ones. */
21000 if (! NILP (oprops) && !risky)
21001 {
21002 Lisp_Object tem;
21003
21004 oprops = Fcopy_sequence (oprops);
21005 tem = props;
21006 while (CONSP (tem))
21007 {
21008 oprops = Fplist_put (oprops, XCAR (tem),
21009 XCAR (XCDR (tem)));
21010 tem = XCDR (XCDR (tem));
21011 }
21012 props = oprops;
21013 }
21014
21015 aelt = Fassoc (elt, mode_line_proptrans_alist);
21016 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21017 {
21018 /* AELT is what we want. Move it to the front
21019 without consing. */
21020 elt = XCAR (aelt);
21021 mode_line_proptrans_alist
21022 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21023 }
21024 else
21025 {
21026 Lisp_Object tem;
21027
21028 /* If AELT has the wrong props, it is useless.
21029 so get rid of it. */
21030 if (! NILP (aelt))
21031 mode_line_proptrans_alist
21032 = Fdelq (aelt, mode_line_proptrans_alist);
21033
21034 elt = Fcopy_sequence (elt);
21035 Fset_text_properties (make_number (0), Flength (elt),
21036 props, elt);
21037 /* Add this item to mode_line_proptrans_alist. */
21038 mode_line_proptrans_alist
21039 = Fcons (Fcons (elt, props),
21040 mode_line_proptrans_alist);
21041 /* Truncate mode_line_proptrans_alist
21042 to at most 50 elements. */
21043 tem = Fnthcdr (make_number (50),
21044 mode_line_proptrans_alist);
21045 if (! NILP (tem))
21046 XSETCDR (tem, Qnil);
21047 }
21048 }
21049 }
21050
21051 offset = 0;
21052
21053 if (literal)
21054 {
21055 prec = precision - n;
21056 switch (mode_line_target)
21057 {
21058 case MODE_LINE_NOPROP:
21059 case MODE_LINE_TITLE:
21060 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21061 break;
21062 case MODE_LINE_STRING:
21063 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21064 break;
21065 case MODE_LINE_DISPLAY:
21066 n += display_string (NULL, elt, Qnil, 0, 0, it,
21067 0, prec, 0, STRING_MULTIBYTE (elt));
21068 break;
21069 }
21070
21071 break;
21072 }
21073
21074 /* Handle the non-literal case. */
21075
21076 while ((precision <= 0 || n < precision)
21077 && SREF (elt, offset) != 0
21078 && (mode_line_target != MODE_LINE_DISPLAY
21079 || it->current_x < it->last_visible_x))
21080 {
21081 ptrdiff_t last_offset = offset;
21082
21083 /* Advance to end of string or next format specifier. */
21084 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21085 ;
21086
21087 if (offset - 1 != last_offset)
21088 {
21089 ptrdiff_t nchars, nbytes;
21090
21091 /* Output to end of string or up to '%'. Field width
21092 is length of string. Don't output more than
21093 PRECISION allows us. */
21094 offset--;
21095
21096 prec = c_string_width (SDATA (elt) + last_offset,
21097 offset - last_offset, precision - n,
21098 &nchars, &nbytes);
21099
21100 switch (mode_line_target)
21101 {
21102 case MODE_LINE_NOPROP:
21103 case MODE_LINE_TITLE:
21104 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21105 break;
21106 case MODE_LINE_STRING:
21107 {
21108 ptrdiff_t bytepos = last_offset;
21109 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21110 ptrdiff_t endpos = (precision <= 0
21111 ? string_byte_to_char (elt, offset)
21112 : charpos + nchars);
21113
21114 n += store_mode_line_string (NULL,
21115 Fsubstring (elt, make_number (charpos),
21116 make_number (endpos)),
21117 0, 0, 0, Qnil);
21118 }
21119 break;
21120 case MODE_LINE_DISPLAY:
21121 {
21122 ptrdiff_t bytepos = last_offset;
21123 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21124
21125 if (precision <= 0)
21126 nchars = string_byte_to_char (elt, offset) - charpos;
21127 n += display_string (NULL, elt, Qnil, 0, charpos,
21128 it, 0, nchars, 0,
21129 STRING_MULTIBYTE (elt));
21130 }
21131 break;
21132 }
21133 }
21134 else /* c == '%' */
21135 {
21136 ptrdiff_t percent_position = offset;
21137
21138 /* Get the specified minimum width. Zero means
21139 don't pad. */
21140 field = 0;
21141 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21142 field = field * 10 + c - '0';
21143
21144 /* Don't pad beyond the total padding allowed. */
21145 if (field_width - n > 0 && field > field_width - n)
21146 field = field_width - n;
21147
21148 /* Note that either PRECISION <= 0 or N < PRECISION. */
21149 prec = precision - n;
21150
21151 if (c == 'M')
21152 n += display_mode_element (it, depth, field, prec,
21153 Vglobal_mode_string, props,
21154 risky);
21155 else if (c != 0)
21156 {
21157 bool multibyte;
21158 ptrdiff_t bytepos, charpos;
21159 const char *spec;
21160 Lisp_Object string;
21161
21162 bytepos = percent_position;
21163 charpos = (STRING_MULTIBYTE (elt)
21164 ? string_byte_to_char (elt, bytepos)
21165 : bytepos);
21166 spec = decode_mode_spec (it->w, c, field, &string);
21167 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21168
21169 switch (mode_line_target)
21170 {
21171 case MODE_LINE_NOPROP:
21172 case MODE_LINE_TITLE:
21173 n += store_mode_line_noprop (spec, field, prec);
21174 break;
21175 case MODE_LINE_STRING:
21176 {
21177 Lisp_Object tem = build_string (spec);
21178 props = Ftext_properties_at (make_number (charpos), elt);
21179 /* Should only keep face property in props */
21180 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21181 }
21182 break;
21183 case MODE_LINE_DISPLAY:
21184 {
21185 int nglyphs_before, nwritten;
21186
21187 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21188 nwritten = display_string (spec, string, elt,
21189 charpos, 0, it,
21190 field, prec, 0,
21191 multibyte);
21192
21193 /* Assign to the glyphs written above the
21194 string where the `%x' came from, position
21195 of the `%'. */
21196 if (nwritten > 0)
21197 {
21198 struct glyph *glyph
21199 = (it->glyph_row->glyphs[TEXT_AREA]
21200 + nglyphs_before);
21201 int i;
21202
21203 for (i = 0; i < nwritten; ++i)
21204 {
21205 glyph[i].object = elt;
21206 glyph[i].charpos = charpos;
21207 }
21208
21209 n += nwritten;
21210 }
21211 }
21212 break;
21213 }
21214 }
21215 else /* c == 0 */
21216 break;
21217 }
21218 }
21219 }
21220 break;
21221
21222 case Lisp_Symbol:
21223 /* A symbol: process the value of the symbol recursively
21224 as if it appeared here directly. Avoid error if symbol void.
21225 Special case: if value of symbol is a string, output the string
21226 literally. */
21227 {
21228 register Lisp_Object tem;
21229
21230 /* If the variable is not marked as risky to set
21231 then its contents are risky to use. */
21232 if (NILP (Fget (elt, Qrisky_local_variable)))
21233 risky = 1;
21234
21235 tem = Fboundp (elt);
21236 if (!NILP (tem))
21237 {
21238 tem = Fsymbol_value (elt);
21239 /* If value is a string, output that string literally:
21240 don't check for % within it. */
21241 if (STRINGP (tem))
21242 literal = 1;
21243
21244 if (!EQ (tem, elt))
21245 {
21246 /* Give up right away for nil or t. */
21247 elt = tem;
21248 goto tail_recurse;
21249 }
21250 }
21251 }
21252 break;
21253
21254 case Lisp_Cons:
21255 {
21256 register Lisp_Object car, tem;
21257
21258 /* A cons cell: five distinct cases.
21259 If first element is :eval or :propertize, do something special.
21260 If first element is a string or a cons, process all the elements
21261 and effectively concatenate them.
21262 If first element is a negative number, truncate displaying cdr to
21263 at most that many characters. If positive, pad (with spaces)
21264 to at least that many characters.
21265 If first element is a symbol, process the cadr or caddr recursively
21266 according to whether the symbol's value is non-nil or nil. */
21267 car = XCAR (elt);
21268 if (EQ (car, QCeval))
21269 {
21270 /* An element of the form (:eval FORM) means evaluate FORM
21271 and use the result as mode line elements. */
21272
21273 if (risky)
21274 break;
21275
21276 if (CONSP (XCDR (elt)))
21277 {
21278 Lisp_Object spec;
21279 spec = safe_eval (XCAR (XCDR (elt)));
21280 n += display_mode_element (it, depth, field_width - n,
21281 precision - n, spec, props,
21282 risky);
21283 }
21284 }
21285 else if (EQ (car, QCpropertize))
21286 {
21287 /* An element of the form (:propertize ELT PROPS...)
21288 means display ELT but applying properties PROPS. */
21289
21290 if (risky)
21291 break;
21292
21293 if (CONSP (XCDR (elt)))
21294 n += display_mode_element (it, depth, field_width - n,
21295 precision - n, XCAR (XCDR (elt)),
21296 XCDR (XCDR (elt)), risky);
21297 }
21298 else if (SYMBOLP (car))
21299 {
21300 tem = Fboundp (car);
21301 elt = XCDR (elt);
21302 if (!CONSP (elt))
21303 goto invalid;
21304 /* elt is now the cdr, and we know it is a cons cell.
21305 Use its car if CAR has a non-nil value. */
21306 if (!NILP (tem))
21307 {
21308 tem = Fsymbol_value (car);
21309 if (!NILP (tem))
21310 {
21311 elt = XCAR (elt);
21312 goto tail_recurse;
21313 }
21314 }
21315 /* Symbol's value is nil (or symbol is unbound)
21316 Get the cddr of the original list
21317 and if possible find the caddr and use that. */
21318 elt = XCDR (elt);
21319 if (NILP (elt))
21320 break;
21321 else if (!CONSP (elt))
21322 goto invalid;
21323 elt = XCAR (elt);
21324 goto tail_recurse;
21325 }
21326 else if (INTEGERP (car))
21327 {
21328 register int lim = XINT (car);
21329 elt = XCDR (elt);
21330 if (lim < 0)
21331 {
21332 /* Negative int means reduce maximum width. */
21333 if (precision <= 0)
21334 precision = -lim;
21335 else
21336 precision = min (precision, -lim);
21337 }
21338 else if (lim > 0)
21339 {
21340 /* Padding specified. Don't let it be more than
21341 current maximum. */
21342 if (precision > 0)
21343 lim = min (precision, lim);
21344
21345 /* If that's more padding than already wanted, queue it.
21346 But don't reduce padding already specified even if
21347 that is beyond the current truncation point. */
21348 field_width = max (lim, field_width);
21349 }
21350 goto tail_recurse;
21351 }
21352 else if (STRINGP (car) || CONSP (car))
21353 {
21354 Lisp_Object halftail = elt;
21355 int len = 0;
21356
21357 while (CONSP (elt)
21358 && (precision <= 0 || n < precision))
21359 {
21360 n += display_mode_element (it, depth,
21361 /* Do padding only after the last
21362 element in the list. */
21363 (! CONSP (XCDR (elt))
21364 ? field_width - n
21365 : 0),
21366 precision - n, XCAR (elt),
21367 props, risky);
21368 elt = XCDR (elt);
21369 len++;
21370 if ((len & 1) == 0)
21371 halftail = XCDR (halftail);
21372 /* Check for cycle. */
21373 if (EQ (halftail, elt))
21374 break;
21375 }
21376 }
21377 }
21378 break;
21379
21380 default:
21381 invalid:
21382 elt = build_string ("*invalid*");
21383 goto tail_recurse;
21384 }
21385
21386 /* Pad to FIELD_WIDTH. */
21387 if (field_width > 0 && n < field_width)
21388 {
21389 switch (mode_line_target)
21390 {
21391 case MODE_LINE_NOPROP:
21392 case MODE_LINE_TITLE:
21393 n += store_mode_line_noprop ("", field_width - n, 0);
21394 break;
21395 case MODE_LINE_STRING:
21396 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21397 break;
21398 case MODE_LINE_DISPLAY:
21399 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21400 0, 0, 0);
21401 break;
21402 }
21403 }
21404
21405 return n;
21406 }
21407
21408 /* Store a mode-line string element in mode_line_string_list.
21409
21410 If STRING is non-null, display that C string. Otherwise, the Lisp
21411 string LISP_STRING is displayed.
21412
21413 FIELD_WIDTH is the minimum number of output glyphs to produce.
21414 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21415 with spaces. FIELD_WIDTH <= 0 means don't pad.
21416
21417 PRECISION is the maximum number of characters to output from
21418 STRING. PRECISION <= 0 means don't truncate the string.
21419
21420 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21421 properties to the string.
21422
21423 PROPS are the properties to add to the string.
21424 The mode_line_string_face face property is always added to the string.
21425 */
21426
21427 static int
21428 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21429 int field_width, int precision, Lisp_Object props)
21430 {
21431 ptrdiff_t len;
21432 int n = 0;
21433
21434 if (string != NULL)
21435 {
21436 len = strlen (string);
21437 if (precision > 0 && len > precision)
21438 len = precision;
21439 lisp_string = make_string (string, len);
21440 if (NILP (props))
21441 props = mode_line_string_face_prop;
21442 else if (!NILP (mode_line_string_face))
21443 {
21444 Lisp_Object face = Fplist_get (props, Qface);
21445 props = Fcopy_sequence (props);
21446 if (NILP (face))
21447 face = mode_line_string_face;
21448 else
21449 face = list2 (face, mode_line_string_face);
21450 props = Fplist_put (props, Qface, face);
21451 }
21452 Fadd_text_properties (make_number (0), make_number (len),
21453 props, lisp_string);
21454 }
21455 else
21456 {
21457 len = XFASTINT (Flength (lisp_string));
21458 if (precision > 0 && len > precision)
21459 {
21460 len = precision;
21461 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21462 precision = -1;
21463 }
21464 if (!NILP (mode_line_string_face))
21465 {
21466 Lisp_Object face;
21467 if (NILP (props))
21468 props = Ftext_properties_at (make_number (0), lisp_string);
21469 face = Fplist_get (props, Qface);
21470 if (NILP (face))
21471 face = mode_line_string_face;
21472 else
21473 face = list2 (face, mode_line_string_face);
21474 props = list2 (Qface, face);
21475 if (copy_string)
21476 lisp_string = Fcopy_sequence (lisp_string);
21477 }
21478 if (!NILP (props))
21479 Fadd_text_properties (make_number (0), make_number (len),
21480 props, lisp_string);
21481 }
21482
21483 if (len > 0)
21484 {
21485 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21486 n += len;
21487 }
21488
21489 if (field_width > len)
21490 {
21491 field_width -= len;
21492 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21493 if (!NILP (props))
21494 Fadd_text_properties (make_number (0), make_number (field_width),
21495 props, lisp_string);
21496 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21497 n += field_width;
21498 }
21499
21500 return n;
21501 }
21502
21503
21504 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21505 1, 4, 0,
21506 doc: /* Format a string out of a mode line format specification.
21507 First arg FORMAT specifies the mode line format (see `mode-line-format'
21508 for details) to use.
21509
21510 By default, the format is evaluated for the currently selected window.
21511
21512 Optional second arg FACE specifies the face property to put on all
21513 characters for which no face is specified. The value nil means the
21514 default face. The value t means whatever face the window's mode line
21515 currently uses (either `mode-line' or `mode-line-inactive',
21516 depending on whether the window is the selected window or not).
21517 An integer value means the value string has no text
21518 properties.
21519
21520 Optional third and fourth args WINDOW and BUFFER specify the window
21521 and buffer to use as the context for the formatting (defaults
21522 are the selected window and the WINDOW's buffer). */)
21523 (Lisp_Object format, Lisp_Object face,
21524 Lisp_Object window, Lisp_Object buffer)
21525 {
21526 struct it it;
21527 int len;
21528 struct window *w;
21529 struct buffer *old_buffer = NULL;
21530 int face_id;
21531 int no_props = INTEGERP (face);
21532 ptrdiff_t count = SPECPDL_INDEX ();
21533 Lisp_Object str;
21534 int string_start = 0;
21535
21536 w = decode_any_window (window);
21537 XSETWINDOW (window, w);
21538
21539 if (NILP (buffer))
21540 buffer = w->contents;
21541 CHECK_BUFFER (buffer);
21542
21543 /* Make formatting the modeline a non-op when noninteractive, otherwise
21544 there will be problems later caused by a partially initialized frame. */
21545 if (NILP (format) || noninteractive)
21546 return empty_unibyte_string;
21547
21548 if (no_props)
21549 face = Qnil;
21550
21551 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21552 : EQ (face, Qt) ? (EQ (window, selected_window)
21553 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21554 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21555 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21556 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21557 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21558 : DEFAULT_FACE_ID;
21559
21560 old_buffer = current_buffer;
21561
21562 /* Save things including mode_line_proptrans_alist,
21563 and set that to nil so that we don't alter the outer value. */
21564 record_unwind_protect (unwind_format_mode_line,
21565 format_mode_line_unwind_data
21566 (XFRAME (WINDOW_FRAME (w)),
21567 old_buffer, selected_window, 1));
21568 mode_line_proptrans_alist = Qnil;
21569
21570 Fselect_window (window, Qt);
21571 set_buffer_internal_1 (XBUFFER (buffer));
21572
21573 init_iterator (&it, w, -1, -1, NULL, face_id);
21574
21575 if (no_props)
21576 {
21577 mode_line_target = MODE_LINE_NOPROP;
21578 mode_line_string_face_prop = Qnil;
21579 mode_line_string_list = Qnil;
21580 string_start = MODE_LINE_NOPROP_LEN (0);
21581 }
21582 else
21583 {
21584 mode_line_target = MODE_LINE_STRING;
21585 mode_line_string_list = Qnil;
21586 mode_line_string_face = face;
21587 mode_line_string_face_prop
21588 = NILP (face) ? Qnil : list2 (Qface, face);
21589 }
21590
21591 push_kboard (FRAME_KBOARD (it.f));
21592 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21593 pop_kboard ();
21594
21595 if (no_props)
21596 {
21597 len = MODE_LINE_NOPROP_LEN (string_start);
21598 str = make_string (mode_line_noprop_buf + string_start, len);
21599 }
21600 else
21601 {
21602 mode_line_string_list = Fnreverse (mode_line_string_list);
21603 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21604 empty_unibyte_string);
21605 }
21606
21607 unbind_to (count, Qnil);
21608 return str;
21609 }
21610
21611 /* Write a null-terminated, right justified decimal representation of
21612 the positive integer D to BUF using a minimal field width WIDTH. */
21613
21614 static void
21615 pint2str (register char *buf, register int width, register ptrdiff_t d)
21616 {
21617 register char *p = buf;
21618
21619 if (d <= 0)
21620 *p++ = '0';
21621 else
21622 {
21623 while (d > 0)
21624 {
21625 *p++ = d % 10 + '0';
21626 d /= 10;
21627 }
21628 }
21629
21630 for (width -= (int) (p - buf); width > 0; --width)
21631 *p++ = ' ';
21632 *p-- = '\0';
21633 while (p > buf)
21634 {
21635 d = *buf;
21636 *buf++ = *p;
21637 *p-- = d;
21638 }
21639 }
21640
21641 /* Write a null-terminated, right justified decimal and "human
21642 readable" representation of the nonnegative integer D to BUF using
21643 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21644
21645 static const char power_letter[] =
21646 {
21647 0, /* no letter */
21648 'k', /* kilo */
21649 'M', /* mega */
21650 'G', /* giga */
21651 'T', /* tera */
21652 'P', /* peta */
21653 'E', /* exa */
21654 'Z', /* zetta */
21655 'Y' /* yotta */
21656 };
21657
21658 static void
21659 pint2hrstr (char *buf, int width, ptrdiff_t d)
21660 {
21661 /* We aim to represent the nonnegative integer D as
21662 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21663 ptrdiff_t quotient = d;
21664 int remainder = 0;
21665 /* -1 means: do not use TENTHS. */
21666 int tenths = -1;
21667 int exponent = 0;
21668
21669 /* Length of QUOTIENT.TENTHS as a string. */
21670 int length;
21671
21672 char * psuffix;
21673 char * p;
21674
21675 if (quotient >= 1000)
21676 {
21677 /* Scale to the appropriate EXPONENT. */
21678 do
21679 {
21680 remainder = quotient % 1000;
21681 quotient /= 1000;
21682 exponent++;
21683 }
21684 while (quotient >= 1000);
21685
21686 /* Round to nearest and decide whether to use TENTHS or not. */
21687 if (quotient <= 9)
21688 {
21689 tenths = remainder / 100;
21690 if (remainder % 100 >= 50)
21691 {
21692 if (tenths < 9)
21693 tenths++;
21694 else
21695 {
21696 quotient++;
21697 if (quotient == 10)
21698 tenths = -1;
21699 else
21700 tenths = 0;
21701 }
21702 }
21703 }
21704 else
21705 if (remainder >= 500)
21706 {
21707 if (quotient < 999)
21708 quotient++;
21709 else
21710 {
21711 quotient = 1;
21712 exponent++;
21713 tenths = 0;
21714 }
21715 }
21716 }
21717
21718 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21719 if (tenths == -1 && quotient <= 99)
21720 if (quotient <= 9)
21721 length = 1;
21722 else
21723 length = 2;
21724 else
21725 length = 3;
21726 p = psuffix = buf + max (width, length);
21727
21728 /* Print EXPONENT. */
21729 *psuffix++ = power_letter[exponent];
21730 *psuffix = '\0';
21731
21732 /* Print TENTHS. */
21733 if (tenths >= 0)
21734 {
21735 *--p = '0' + tenths;
21736 *--p = '.';
21737 }
21738
21739 /* Print QUOTIENT. */
21740 do
21741 {
21742 int digit = quotient % 10;
21743 *--p = '0' + digit;
21744 }
21745 while ((quotient /= 10) != 0);
21746
21747 /* Print leading spaces. */
21748 while (buf < p)
21749 *--p = ' ';
21750 }
21751
21752 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21753 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21754 type of CODING_SYSTEM. Return updated pointer into BUF. */
21755
21756 static unsigned char invalid_eol_type[] = "(*invalid*)";
21757
21758 static char *
21759 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21760 {
21761 Lisp_Object val;
21762 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21763 const unsigned char *eol_str;
21764 int eol_str_len;
21765 /* The EOL conversion we are using. */
21766 Lisp_Object eoltype;
21767
21768 val = CODING_SYSTEM_SPEC (coding_system);
21769 eoltype = Qnil;
21770
21771 if (!VECTORP (val)) /* Not yet decided. */
21772 {
21773 *buf++ = multibyte ? '-' : ' ';
21774 if (eol_flag)
21775 eoltype = eol_mnemonic_undecided;
21776 /* Don't mention EOL conversion if it isn't decided. */
21777 }
21778 else
21779 {
21780 Lisp_Object attrs;
21781 Lisp_Object eolvalue;
21782
21783 attrs = AREF (val, 0);
21784 eolvalue = AREF (val, 2);
21785
21786 *buf++ = multibyte
21787 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21788 : ' ';
21789
21790 if (eol_flag)
21791 {
21792 /* The EOL conversion that is normal on this system. */
21793
21794 if (NILP (eolvalue)) /* Not yet decided. */
21795 eoltype = eol_mnemonic_undecided;
21796 else if (VECTORP (eolvalue)) /* Not yet decided. */
21797 eoltype = eol_mnemonic_undecided;
21798 else /* eolvalue is Qunix, Qdos, or Qmac. */
21799 eoltype = (EQ (eolvalue, Qunix)
21800 ? eol_mnemonic_unix
21801 : (EQ (eolvalue, Qdos) == 1
21802 ? eol_mnemonic_dos : eol_mnemonic_mac));
21803 }
21804 }
21805
21806 if (eol_flag)
21807 {
21808 /* Mention the EOL conversion if it is not the usual one. */
21809 if (STRINGP (eoltype))
21810 {
21811 eol_str = SDATA (eoltype);
21812 eol_str_len = SBYTES (eoltype);
21813 }
21814 else if (CHARACTERP (eoltype))
21815 {
21816 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21817 int c = XFASTINT (eoltype);
21818 eol_str_len = CHAR_STRING (c, tmp);
21819 eol_str = tmp;
21820 }
21821 else
21822 {
21823 eol_str = invalid_eol_type;
21824 eol_str_len = sizeof (invalid_eol_type) - 1;
21825 }
21826 memcpy (buf, eol_str, eol_str_len);
21827 buf += eol_str_len;
21828 }
21829
21830 return buf;
21831 }
21832
21833 /* Return a string for the output of a mode line %-spec for window W,
21834 generated by character C. FIELD_WIDTH > 0 means pad the string
21835 returned with spaces to that value. Return a Lisp string in
21836 *STRING if the resulting string is taken from that Lisp string.
21837
21838 Note we operate on the current buffer for most purposes. */
21839
21840 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21841
21842 static const char *
21843 decode_mode_spec (struct window *w, register int c, int field_width,
21844 Lisp_Object *string)
21845 {
21846 Lisp_Object obj;
21847 struct frame *f = XFRAME (WINDOW_FRAME (w));
21848 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21849 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21850 produce strings from numerical values, so limit preposterously
21851 large values of FIELD_WIDTH to avoid overrunning the buffer's
21852 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21853 bytes plus the terminating null. */
21854 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21855 struct buffer *b = current_buffer;
21856
21857 obj = Qnil;
21858 *string = Qnil;
21859
21860 switch (c)
21861 {
21862 case '*':
21863 if (!NILP (BVAR (b, read_only)))
21864 return "%";
21865 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21866 return "*";
21867 return "-";
21868
21869 case '+':
21870 /* This differs from %* only for a modified read-only buffer. */
21871 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21872 return "*";
21873 if (!NILP (BVAR (b, read_only)))
21874 return "%";
21875 return "-";
21876
21877 case '&':
21878 /* This differs from %* in ignoring read-only-ness. */
21879 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21880 return "*";
21881 return "-";
21882
21883 case '%':
21884 return "%";
21885
21886 case '[':
21887 {
21888 int i;
21889 char *p;
21890
21891 if (command_loop_level > 5)
21892 return "[[[... ";
21893 p = decode_mode_spec_buf;
21894 for (i = 0; i < command_loop_level; i++)
21895 *p++ = '[';
21896 *p = 0;
21897 return decode_mode_spec_buf;
21898 }
21899
21900 case ']':
21901 {
21902 int i;
21903 char *p;
21904
21905 if (command_loop_level > 5)
21906 return " ...]]]";
21907 p = decode_mode_spec_buf;
21908 for (i = 0; i < command_loop_level; i++)
21909 *p++ = ']';
21910 *p = 0;
21911 return decode_mode_spec_buf;
21912 }
21913
21914 case '-':
21915 {
21916 register int i;
21917
21918 /* Let lots_of_dashes be a string of infinite length. */
21919 if (mode_line_target == MODE_LINE_NOPROP
21920 || mode_line_target == MODE_LINE_STRING)
21921 return "--";
21922 if (field_width <= 0
21923 || field_width > sizeof (lots_of_dashes))
21924 {
21925 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21926 decode_mode_spec_buf[i] = '-';
21927 decode_mode_spec_buf[i] = '\0';
21928 return decode_mode_spec_buf;
21929 }
21930 else
21931 return lots_of_dashes;
21932 }
21933
21934 case 'b':
21935 obj = BVAR (b, name);
21936 break;
21937
21938 case 'c':
21939 /* %c and %l are ignored in `frame-title-format'.
21940 (In redisplay_internal, the frame title is drawn _before_ the
21941 windows are updated, so the stuff which depends on actual
21942 window contents (such as %l) may fail to render properly, or
21943 even crash emacs.) */
21944 if (mode_line_target == MODE_LINE_TITLE)
21945 return "";
21946 else
21947 {
21948 ptrdiff_t col = current_column ();
21949 w->column_number_displayed = col;
21950 pint2str (decode_mode_spec_buf, width, col);
21951 return decode_mode_spec_buf;
21952 }
21953
21954 case 'e':
21955 #ifndef SYSTEM_MALLOC
21956 {
21957 if (NILP (Vmemory_full))
21958 return "";
21959 else
21960 return "!MEM FULL! ";
21961 }
21962 #else
21963 return "";
21964 #endif
21965
21966 case 'F':
21967 /* %F displays the frame name. */
21968 if (!NILP (f->title))
21969 return SSDATA (f->title);
21970 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21971 return SSDATA (f->name);
21972 return "Emacs";
21973
21974 case 'f':
21975 obj = BVAR (b, filename);
21976 break;
21977
21978 case 'i':
21979 {
21980 ptrdiff_t size = ZV - BEGV;
21981 pint2str (decode_mode_spec_buf, width, size);
21982 return decode_mode_spec_buf;
21983 }
21984
21985 case 'I':
21986 {
21987 ptrdiff_t size = ZV - BEGV;
21988 pint2hrstr (decode_mode_spec_buf, width, size);
21989 return decode_mode_spec_buf;
21990 }
21991
21992 case 'l':
21993 {
21994 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21995 ptrdiff_t topline, nlines, height;
21996 ptrdiff_t junk;
21997
21998 /* %c and %l are ignored in `frame-title-format'. */
21999 if (mode_line_target == MODE_LINE_TITLE)
22000 return "";
22001
22002 startpos = marker_position (w->start);
22003 startpos_byte = marker_byte_position (w->start);
22004 height = WINDOW_TOTAL_LINES (w);
22005
22006 /* If we decided that this buffer isn't suitable for line numbers,
22007 don't forget that too fast. */
22008 if (w->base_line_pos == -1)
22009 goto no_value;
22010
22011 /* If the buffer is very big, don't waste time. */
22012 if (INTEGERP (Vline_number_display_limit)
22013 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22014 {
22015 w->base_line_pos = 0;
22016 w->base_line_number = 0;
22017 goto no_value;
22018 }
22019
22020 if (w->base_line_number > 0
22021 && w->base_line_pos > 0
22022 && w->base_line_pos <= startpos)
22023 {
22024 line = w->base_line_number;
22025 linepos = w->base_line_pos;
22026 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22027 }
22028 else
22029 {
22030 line = 1;
22031 linepos = BUF_BEGV (b);
22032 linepos_byte = BUF_BEGV_BYTE (b);
22033 }
22034
22035 /* Count lines from base line to window start position. */
22036 nlines = display_count_lines (linepos_byte,
22037 startpos_byte,
22038 startpos, &junk);
22039
22040 topline = nlines + line;
22041
22042 /* Determine a new base line, if the old one is too close
22043 or too far away, or if we did not have one.
22044 "Too close" means it's plausible a scroll-down would
22045 go back past it. */
22046 if (startpos == BUF_BEGV (b))
22047 {
22048 w->base_line_number = topline;
22049 w->base_line_pos = BUF_BEGV (b);
22050 }
22051 else if (nlines < height + 25 || nlines > height * 3 + 50
22052 || linepos == BUF_BEGV (b))
22053 {
22054 ptrdiff_t limit = BUF_BEGV (b);
22055 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22056 ptrdiff_t position;
22057 ptrdiff_t distance =
22058 (height * 2 + 30) * line_number_display_limit_width;
22059
22060 if (startpos - distance > limit)
22061 {
22062 limit = startpos - distance;
22063 limit_byte = CHAR_TO_BYTE (limit);
22064 }
22065
22066 nlines = display_count_lines (startpos_byte,
22067 limit_byte,
22068 - (height * 2 + 30),
22069 &position);
22070 /* If we couldn't find the lines we wanted within
22071 line_number_display_limit_width chars per line,
22072 give up on line numbers for this window. */
22073 if (position == limit_byte && limit == startpos - distance)
22074 {
22075 w->base_line_pos = -1;
22076 w->base_line_number = 0;
22077 goto no_value;
22078 }
22079
22080 w->base_line_number = topline - nlines;
22081 w->base_line_pos = BYTE_TO_CHAR (position);
22082 }
22083
22084 /* Now count lines from the start pos to point. */
22085 nlines = display_count_lines (startpos_byte,
22086 PT_BYTE, PT, &junk);
22087
22088 /* Record that we did display the line number. */
22089 line_number_displayed = 1;
22090
22091 /* Make the string to show. */
22092 pint2str (decode_mode_spec_buf, width, topline + nlines);
22093 return decode_mode_spec_buf;
22094 no_value:
22095 {
22096 char* p = decode_mode_spec_buf;
22097 int pad = width - 2;
22098 while (pad-- > 0)
22099 *p++ = ' ';
22100 *p++ = '?';
22101 *p++ = '?';
22102 *p = '\0';
22103 return decode_mode_spec_buf;
22104 }
22105 }
22106 break;
22107
22108 case 'm':
22109 obj = BVAR (b, mode_name);
22110 break;
22111
22112 case 'n':
22113 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22114 return " Narrow";
22115 break;
22116
22117 case 'p':
22118 {
22119 ptrdiff_t pos = marker_position (w->start);
22120 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22121
22122 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22123 {
22124 if (pos <= BUF_BEGV (b))
22125 return "All";
22126 else
22127 return "Bottom";
22128 }
22129 else if (pos <= BUF_BEGV (b))
22130 return "Top";
22131 else
22132 {
22133 if (total > 1000000)
22134 /* Do it differently for a large value, to avoid overflow. */
22135 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22136 else
22137 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22138 /* We can't normally display a 3-digit number,
22139 so get us a 2-digit number that is close. */
22140 if (total == 100)
22141 total = 99;
22142 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22143 return decode_mode_spec_buf;
22144 }
22145 }
22146
22147 /* Display percentage of size above the bottom of the screen. */
22148 case 'P':
22149 {
22150 ptrdiff_t toppos = marker_position (w->start);
22151 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22152 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22153
22154 if (botpos >= BUF_ZV (b))
22155 {
22156 if (toppos <= BUF_BEGV (b))
22157 return "All";
22158 else
22159 return "Bottom";
22160 }
22161 else
22162 {
22163 if (total > 1000000)
22164 /* Do it differently for a large value, to avoid overflow. */
22165 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22166 else
22167 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22168 /* We can't normally display a 3-digit number,
22169 so get us a 2-digit number that is close. */
22170 if (total == 100)
22171 total = 99;
22172 if (toppos <= BUF_BEGV (b))
22173 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22174 else
22175 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22176 return decode_mode_spec_buf;
22177 }
22178 }
22179
22180 case 's':
22181 /* status of process */
22182 obj = Fget_buffer_process (Fcurrent_buffer ());
22183 if (NILP (obj))
22184 return "no process";
22185 #ifndef MSDOS
22186 obj = Fsymbol_name (Fprocess_status (obj));
22187 #endif
22188 break;
22189
22190 case '@':
22191 {
22192 ptrdiff_t count = inhibit_garbage_collection ();
22193 Lisp_Object val = call1 (intern ("file-remote-p"),
22194 BVAR (current_buffer, directory));
22195 unbind_to (count, Qnil);
22196
22197 if (NILP (val))
22198 return "-";
22199 else
22200 return "@";
22201 }
22202
22203 case 'z':
22204 /* coding-system (not including end-of-line format) */
22205 case 'Z':
22206 /* coding-system (including end-of-line type) */
22207 {
22208 int eol_flag = (c == 'Z');
22209 char *p = decode_mode_spec_buf;
22210
22211 if (! FRAME_WINDOW_P (f))
22212 {
22213 /* No need to mention EOL here--the terminal never needs
22214 to do EOL conversion. */
22215 p = decode_mode_spec_coding (CODING_ID_NAME
22216 (FRAME_KEYBOARD_CODING (f)->id),
22217 p, 0);
22218 p = decode_mode_spec_coding (CODING_ID_NAME
22219 (FRAME_TERMINAL_CODING (f)->id),
22220 p, 0);
22221 }
22222 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22223 p, eol_flag);
22224
22225 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22226 #ifdef subprocesses
22227 obj = Fget_buffer_process (Fcurrent_buffer ());
22228 if (PROCESSP (obj))
22229 {
22230 p = decode_mode_spec_coding
22231 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22232 p = decode_mode_spec_coding
22233 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22234 }
22235 #endif /* subprocesses */
22236 #endif /* 0 */
22237 *p = 0;
22238 return decode_mode_spec_buf;
22239 }
22240 }
22241
22242 if (STRINGP (obj))
22243 {
22244 *string = obj;
22245 return SSDATA (obj);
22246 }
22247 else
22248 return "";
22249 }
22250
22251
22252 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22253 means count lines back from START_BYTE. But don't go beyond
22254 LIMIT_BYTE. Return the number of lines thus found (always
22255 nonnegative).
22256
22257 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22258 either the position COUNT lines after/before START_BYTE, if we
22259 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22260 COUNT lines. */
22261
22262 static ptrdiff_t
22263 display_count_lines (ptrdiff_t start_byte,
22264 ptrdiff_t limit_byte, ptrdiff_t count,
22265 ptrdiff_t *byte_pos_ptr)
22266 {
22267 register unsigned char *cursor;
22268 unsigned char *base;
22269
22270 register ptrdiff_t ceiling;
22271 register unsigned char *ceiling_addr;
22272 ptrdiff_t orig_count = count;
22273
22274 /* If we are not in selective display mode,
22275 check only for newlines. */
22276 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22277 && !INTEGERP (BVAR (current_buffer, selective_display)));
22278
22279 if (count > 0)
22280 {
22281 while (start_byte < limit_byte)
22282 {
22283 ceiling = BUFFER_CEILING_OF (start_byte);
22284 ceiling = min (limit_byte - 1, ceiling);
22285 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22286 base = (cursor = BYTE_POS_ADDR (start_byte));
22287
22288 do
22289 {
22290 if (selective_display)
22291 {
22292 while (*cursor != '\n' && *cursor != 015
22293 && ++cursor != ceiling_addr)
22294 continue;
22295 if (cursor == ceiling_addr)
22296 break;
22297 }
22298 else
22299 {
22300 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22301 if (! cursor)
22302 break;
22303 }
22304
22305 cursor++;
22306
22307 if (--count == 0)
22308 {
22309 start_byte += cursor - base;
22310 *byte_pos_ptr = start_byte;
22311 return orig_count;
22312 }
22313 }
22314 while (cursor < ceiling_addr);
22315
22316 start_byte += ceiling_addr - base;
22317 }
22318 }
22319 else
22320 {
22321 while (start_byte > limit_byte)
22322 {
22323 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22324 ceiling = max (limit_byte, ceiling);
22325 ceiling_addr = BYTE_POS_ADDR (ceiling);
22326 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22327 while (1)
22328 {
22329 if (selective_display)
22330 {
22331 while (--cursor >= ceiling_addr
22332 && *cursor != '\n' && *cursor != 015)
22333 continue;
22334 if (cursor < ceiling_addr)
22335 break;
22336 }
22337 else
22338 {
22339 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22340 if (! cursor)
22341 break;
22342 }
22343
22344 if (++count == 0)
22345 {
22346 start_byte += cursor - base + 1;
22347 *byte_pos_ptr = start_byte;
22348 /* When scanning backwards, we should
22349 not count the newline posterior to which we stop. */
22350 return - orig_count - 1;
22351 }
22352 }
22353 start_byte += ceiling_addr - base;
22354 }
22355 }
22356
22357 *byte_pos_ptr = limit_byte;
22358
22359 if (count < 0)
22360 return - orig_count + count;
22361 return orig_count - count;
22362
22363 }
22364
22365
22366 \f
22367 /***********************************************************************
22368 Displaying strings
22369 ***********************************************************************/
22370
22371 /* Display a NUL-terminated string, starting with index START.
22372
22373 If STRING is non-null, display that C string. Otherwise, the Lisp
22374 string LISP_STRING is displayed. There's a case that STRING is
22375 non-null and LISP_STRING is not nil. It means STRING is a string
22376 data of LISP_STRING. In that case, we display LISP_STRING while
22377 ignoring its text properties.
22378
22379 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22380 FACE_STRING. Display STRING or LISP_STRING with the face at
22381 FACE_STRING_POS in FACE_STRING:
22382
22383 Display the string in the environment given by IT, but use the
22384 standard display table, temporarily.
22385
22386 FIELD_WIDTH is the minimum number of output glyphs to produce.
22387 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22388 with spaces. If STRING has more characters, more than FIELD_WIDTH
22389 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22390
22391 PRECISION is the maximum number of characters to output from
22392 STRING. PRECISION < 0 means don't truncate the string.
22393
22394 This is roughly equivalent to printf format specifiers:
22395
22396 FIELD_WIDTH PRECISION PRINTF
22397 ----------------------------------------
22398 -1 -1 %s
22399 -1 10 %.10s
22400 10 -1 %10s
22401 20 10 %20.10s
22402
22403 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22404 display them, and < 0 means obey the current buffer's value of
22405 enable_multibyte_characters.
22406
22407 Value is the number of columns displayed. */
22408
22409 static int
22410 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22411 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22412 int field_width, int precision, int max_x, int multibyte)
22413 {
22414 int hpos_at_start = it->hpos;
22415 int saved_face_id = it->face_id;
22416 struct glyph_row *row = it->glyph_row;
22417 ptrdiff_t it_charpos;
22418
22419 /* Initialize the iterator IT for iteration over STRING beginning
22420 with index START. */
22421 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22422 precision, field_width, multibyte);
22423 if (string && STRINGP (lisp_string))
22424 /* LISP_STRING is the one returned by decode_mode_spec. We should
22425 ignore its text properties. */
22426 it->stop_charpos = it->end_charpos;
22427
22428 /* If displaying STRING, set up the face of the iterator from
22429 FACE_STRING, if that's given. */
22430 if (STRINGP (face_string))
22431 {
22432 ptrdiff_t endptr;
22433 struct face *face;
22434
22435 it->face_id
22436 = face_at_string_position (it->w, face_string, face_string_pos,
22437 0, &endptr, it->base_face_id, 0);
22438 face = FACE_FROM_ID (it->f, it->face_id);
22439 it->face_box_p = face->box != FACE_NO_BOX;
22440 }
22441
22442 /* Set max_x to the maximum allowed X position. Don't let it go
22443 beyond the right edge of the window. */
22444 if (max_x <= 0)
22445 max_x = it->last_visible_x;
22446 else
22447 max_x = min (max_x, it->last_visible_x);
22448
22449 /* Skip over display elements that are not visible. because IT->w is
22450 hscrolled. */
22451 if (it->current_x < it->first_visible_x)
22452 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22453 MOVE_TO_POS | MOVE_TO_X);
22454
22455 row->ascent = it->max_ascent;
22456 row->height = it->max_ascent + it->max_descent;
22457 row->phys_ascent = it->max_phys_ascent;
22458 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22459 row->extra_line_spacing = it->max_extra_line_spacing;
22460
22461 if (STRINGP (it->string))
22462 it_charpos = IT_STRING_CHARPOS (*it);
22463 else
22464 it_charpos = IT_CHARPOS (*it);
22465
22466 /* This condition is for the case that we are called with current_x
22467 past last_visible_x. */
22468 while (it->current_x < max_x)
22469 {
22470 int x_before, x, n_glyphs_before, i, nglyphs;
22471
22472 /* Get the next display element. */
22473 if (!get_next_display_element (it))
22474 break;
22475
22476 /* Produce glyphs. */
22477 x_before = it->current_x;
22478 n_glyphs_before = row->used[TEXT_AREA];
22479 PRODUCE_GLYPHS (it);
22480
22481 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22482 i = 0;
22483 x = x_before;
22484 while (i < nglyphs)
22485 {
22486 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22487
22488 if (it->line_wrap != TRUNCATE
22489 && x + glyph->pixel_width > max_x)
22490 {
22491 /* End of continued line or max_x reached. */
22492 if (CHAR_GLYPH_PADDING_P (*glyph))
22493 {
22494 /* A wide character is unbreakable. */
22495 if (row->reversed_p)
22496 unproduce_glyphs (it, row->used[TEXT_AREA]
22497 - n_glyphs_before);
22498 row->used[TEXT_AREA] = n_glyphs_before;
22499 it->current_x = x_before;
22500 }
22501 else
22502 {
22503 if (row->reversed_p)
22504 unproduce_glyphs (it, row->used[TEXT_AREA]
22505 - (n_glyphs_before + i));
22506 row->used[TEXT_AREA] = n_glyphs_before + i;
22507 it->current_x = x;
22508 }
22509 break;
22510 }
22511 else if (x + glyph->pixel_width >= it->first_visible_x)
22512 {
22513 /* Glyph is at least partially visible. */
22514 ++it->hpos;
22515 if (x < it->first_visible_x)
22516 row->x = x - it->first_visible_x;
22517 }
22518 else
22519 {
22520 /* Glyph is off the left margin of the display area.
22521 Should not happen. */
22522 emacs_abort ();
22523 }
22524
22525 row->ascent = max (row->ascent, it->max_ascent);
22526 row->height = max (row->height, it->max_ascent + it->max_descent);
22527 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22528 row->phys_height = max (row->phys_height,
22529 it->max_phys_ascent + it->max_phys_descent);
22530 row->extra_line_spacing = max (row->extra_line_spacing,
22531 it->max_extra_line_spacing);
22532 x += glyph->pixel_width;
22533 ++i;
22534 }
22535
22536 /* Stop if max_x reached. */
22537 if (i < nglyphs)
22538 break;
22539
22540 /* Stop at line ends. */
22541 if (ITERATOR_AT_END_OF_LINE_P (it))
22542 {
22543 it->continuation_lines_width = 0;
22544 break;
22545 }
22546
22547 set_iterator_to_next (it, 1);
22548 if (STRINGP (it->string))
22549 it_charpos = IT_STRING_CHARPOS (*it);
22550 else
22551 it_charpos = IT_CHARPOS (*it);
22552
22553 /* Stop if truncating at the right edge. */
22554 if (it->line_wrap == TRUNCATE
22555 && it->current_x >= it->last_visible_x)
22556 {
22557 /* Add truncation mark, but don't do it if the line is
22558 truncated at a padding space. */
22559 if (it_charpos < it->string_nchars)
22560 {
22561 if (!FRAME_WINDOW_P (it->f))
22562 {
22563 int ii, n;
22564
22565 if (it->current_x > it->last_visible_x)
22566 {
22567 if (!row->reversed_p)
22568 {
22569 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22570 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22571 break;
22572 }
22573 else
22574 {
22575 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22576 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22577 break;
22578 unproduce_glyphs (it, ii + 1);
22579 ii = row->used[TEXT_AREA] - (ii + 1);
22580 }
22581 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22582 {
22583 row->used[TEXT_AREA] = ii;
22584 produce_special_glyphs (it, IT_TRUNCATION);
22585 }
22586 }
22587 produce_special_glyphs (it, IT_TRUNCATION);
22588 }
22589 row->truncated_on_right_p = 1;
22590 }
22591 break;
22592 }
22593 }
22594
22595 /* Maybe insert a truncation at the left. */
22596 if (it->first_visible_x
22597 && it_charpos > 0)
22598 {
22599 if (!FRAME_WINDOW_P (it->f)
22600 || (row->reversed_p
22601 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22602 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22603 insert_left_trunc_glyphs (it);
22604 row->truncated_on_left_p = 1;
22605 }
22606
22607 it->face_id = saved_face_id;
22608
22609 /* Value is number of columns displayed. */
22610 return it->hpos - hpos_at_start;
22611 }
22612
22613
22614 \f
22615 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22616 appears as an element of LIST or as the car of an element of LIST.
22617 If PROPVAL is a list, compare each element against LIST in that
22618 way, and return 1/2 if any element of PROPVAL is found in LIST.
22619 Otherwise return 0. This function cannot quit.
22620 The return value is 2 if the text is invisible but with an ellipsis
22621 and 1 if it's invisible and without an ellipsis. */
22622
22623 int
22624 invisible_p (register Lisp_Object propval, Lisp_Object list)
22625 {
22626 register Lisp_Object tail, proptail;
22627
22628 for (tail = list; CONSP (tail); tail = XCDR (tail))
22629 {
22630 register Lisp_Object tem;
22631 tem = XCAR (tail);
22632 if (EQ (propval, tem))
22633 return 1;
22634 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22635 return NILP (XCDR (tem)) ? 1 : 2;
22636 }
22637
22638 if (CONSP (propval))
22639 {
22640 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22641 {
22642 Lisp_Object propelt;
22643 propelt = XCAR (proptail);
22644 for (tail = list; CONSP (tail); tail = XCDR (tail))
22645 {
22646 register Lisp_Object tem;
22647 tem = XCAR (tail);
22648 if (EQ (propelt, tem))
22649 return 1;
22650 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22651 return NILP (XCDR (tem)) ? 1 : 2;
22652 }
22653 }
22654 }
22655
22656 return 0;
22657 }
22658
22659 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22660 doc: /* Non-nil if the property makes the text invisible.
22661 POS-OR-PROP can be a marker or number, in which case it is taken to be
22662 a position in the current buffer and the value of the `invisible' property
22663 is checked; or it can be some other value, which is then presumed to be the
22664 value of the `invisible' property of the text of interest.
22665 The non-nil value returned can be t for truly invisible text or something
22666 else if the text is replaced by an ellipsis. */)
22667 (Lisp_Object pos_or_prop)
22668 {
22669 Lisp_Object prop
22670 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22671 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22672 : pos_or_prop);
22673 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22674 return (invis == 0 ? Qnil
22675 : invis == 1 ? Qt
22676 : make_number (invis));
22677 }
22678
22679 /* Calculate a width or height in pixels from a specification using
22680 the following elements:
22681
22682 SPEC ::=
22683 NUM - a (fractional) multiple of the default font width/height
22684 (NUM) - specifies exactly NUM pixels
22685 UNIT - a fixed number of pixels, see below.
22686 ELEMENT - size of a display element in pixels, see below.
22687 (NUM . SPEC) - equals NUM * SPEC
22688 (+ SPEC SPEC ...) - add pixel values
22689 (- SPEC SPEC ...) - subtract pixel values
22690 (- SPEC) - negate pixel value
22691
22692 NUM ::=
22693 INT or FLOAT - a number constant
22694 SYMBOL - use symbol's (buffer local) variable binding.
22695
22696 UNIT ::=
22697 in - pixels per inch *)
22698 mm - pixels per 1/1000 meter *)
22699 cm - pixels per 1/100 meter *)
22700 width - width of current font in pixels.
22701 height - height of current font in pixels.
22702
22703 *) using the ratio(s) defined in display-pixels-per-inch.
22704
22705 ELEMENT ::=
22706
22707 left-fringe - left fringe width in pixels
22708 right-fringe - right fringe width in pixels
22709
22710 left-margin - left margin width in pixels
22711 right-margin - right margin width in pixels
22712
22713 scroll-bar - scroll-bar area width in pixels
22714
22715 Examples:
22716
22717 Pixels corresponding to 5 inches:
22718 (5 . in)
22719
22720 Total width of non-text areas on left side of window (if scroll-bar is on left):
22721 '(space :width (+ left-fringe left-margin scroll-bar))
22722
22723 Align to first text column (in header line):
22724 '(space :align-to 0)
22725
22726 Align to middle of text area minus half the width of variable `my-image'
22727 containing a loaded image:
22728 '(space :align-to (0.5 . (- text my-image)))
22729
22730 Width of left margin minus width of 1 character in the default font:
22731 '(space :width (- left-margin 1))
22732
22733 Width of left margin minus width of 2 characters in the current font:
22734 '(space :width (- left-margin (2 . width)))
22735
22736 Center 1 character over left-margin (in header line):
22737 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22738
22739 Different ways to express width of left fringe plus left margin minus one pixel:
22740 '(space :width (- (+ left-fringe left-margin) (1)))
22741 '(space :width (+ left-fringe left-margin (- (1))))
22742 '(space :width (+ left-fringe left-margin (-1)))
22743
22744 */
22745
22746 static int
22747 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22748 struct font *font, int width_p, int *align_to)
22749 {
22750 double pixels;
22751
22752 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22753 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22754
22755 if (NILP (prop))
22756 return OK_PIXELS (0);
22757
22758 eassert (FRAME_LIVE_P (it->f));
22759
22760 if (SYMBOLP (prop))
22761 {
22762 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22763 {
22764 char *unit = SSDATA (SYMBOL_NAME (prop));
22765
22766 if (unit[0] == 'i' && unit[1] == 'n')
22767 pixels = 1.0;
22768 else if (unit[0] == 'm' && unit[1] == 'm')
22769 pixels = 25.4;
22770 else if (unit[0] == 'c' && unit[1] == 'm')
22771 pixels = 2.54;
22772 else
22773 pixels = 0;
22774 if (pixels > 0)
22775 {
22776 double ppi = (width_p ? FRAME_RES_X (it->f)
22777 : FRAME_RES_Y (it->f));
22778
22779 if (ppi > 0)
22780 return OK_PIXELS (ppi / pixels);
22781 return 0;
22782 }
22783 }
22784
22785 #ifdef HAVE_WINDOW_SYSTEM
22786 if (EQ (prop, Qheight))
22787 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22788 if (EQ (prop, Qwidth))
22789 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22790 #else
22791 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22792 return OK_PIXELS (1);
22793 #endif
22794
22795 if (EQ (prop, Qtext))
22796 return OK_PIXELS (width_p
22797 ? window_box_width (it->w, TEXT_AREA)
22798 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22799
22800 if (align_to && *align_to < 0)
22801 {
22802 *res = 0;
22803 if (EQ (prop, Qleft))
22804 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22805 if (EQ (prop, Qright))
22806 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22807 if (EQ (prop, Qcenter))
22808 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22809 + window_box_width (it->w, TEXT_AREA) / 2);
22810 if (EQ (prop, Qleft_fringe))
22811 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22812 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22813 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22814 if (EQ (prop, Qright_fringe))
22815 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22816 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22817 : window_box_right_offset (it->w, TEXT_AREA));
22818 if (EQ (prop, Qleft_margin))
22819 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22820 if (EQ (prop, Qright_margin))
22821 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22822 if (EQ (prop, Qscroll_bar))
22823 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22824 ? 0
22825 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22826 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22827 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22828 : 0)));
22829 }
22830 else
22831 {
22832 if (EQ (prop, Qleft_fringe))
22833 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22834 if (EQ (prop, Qright_fringe))
22835 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22836 if (EQ (prop, Qleft_margin))
22837 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22838 if (EQ (prop, Qright_margin))
22839 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22840 if (EQ (prop, Qscroll_bar))
22841 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22842 }
22843
22844 prop = buffer_local_value_1 (prop, it->w->contents);
22845 if (EQ (prop, Qunbound))
22846 prop = Qnil;
22847 }
22848
22849 if (INTEGERP (prop) || FLOATP (prop))
22850 {
22851 int base_unit = (width_p
22852 ? FRAME_COLUMN_WIDTH (it->f)
22853 : FRAME_LINE_HEIGHT (it->f));
22854 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22855 }
22856
22857 if (CONSP (prop))
22858 {
22859 Lisp_Object car = XCAR (prop);
22860 Lisp_Object cdr = XCDR (prop);
22861
22862 if (SYMBOLP (car))
22863 {
22864 #ifdef HAVE_WINDOW_SYSTEM
22865 if (FRAME_WINDOW_P (it->f)
22866 && valid_image_p (prop))
22867 {
22868 ptrdiff_t id = lookup_image (it->f, prop);
22869 struct image *img = IMAGE_FROM_ID (it->f, id);
22870
22871 return OK_PIXELS (width_p ? img->width : img->height);
22872 }
22873 #endif
22874 if (EQ (car, Qplus) || EQ (car, Qminus))
22875 {
22876 int first = 1;
22877 double px;
22878
22879 pixels = 0;
22880 while (CONSP (cdr))
22881 {
22882 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22883 font, width_p, align_to))
22884 return 0;
22885 if (first)
22886 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22887 else
22888 pixels += px;
22889 cdr = XCDR (cdr);
22890 }
22891 if (EQ (car, Qminus))
22892 pixels = -pixels;
22893 return OK_PIXELS (pixels);
22894 }
22895
22896 car = buffer_local_value_1 (car, it->w->contents);
22897 if (EQ (car, Qunbound))
22898 car = Qnil;
22899 }
22900
22901 if (INTEGERP (car) || FLOATP (car))
22902 {
22903 double fact;
22904 pixels = XFLOATINT (car);
22905 if (NILP (cdr))
22906 return OK_PIXELS (pixels);
22907 if (calc_pixel_width_or_height (&fact, it, cdr,
22908 font, width_p, align_to))
22909 return OK_PIXELS (pixels * fact);
22910 return 0;
22911 }
22912
22913 return 0;
22914 }
22915
22916 return 0;
22917 }
22918
22919 \f
22920 /***********************************************************************
22921 Glyph Display
22922 ***********************************************************************/
22923
22924 #ifdef HAVE_WINDOW_SYSTEM
22925
22926 #ifdef GLYPH_DEBUG
22927
22928 void
22929 dump_glyph_string (struct glyph_string *s)
22930 {
22931 fprintf (stderr, "glyph string\n");
22932 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22933 s->x, s->y, s->width, s->height);
22934 fprintf (stderr, " ybase = %d\n", s->ybase);
22935 fprintf (stderr, " hl = %d\n", s->hl);
22936 fprintf (stderr, " left overhang = %d, right = %d\n",
22937 s->left_overhang, s->right_overhang);
22938 fprintf (stderr, " nchars = %d\n", s->nchars);
22939 fprintf (stderr, " extends to end of line = %d\n",
22940 s->extends_to_end_of_line_p);
22941 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22942 fprintf (stderr, " bg width = %d\n", s->background_width);
22943 }
22944
22945 #endif /* GLYPH_DEBUG */
22946
22947 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22948 of XChar2b structures for S; it can't be allocated in
22949 init_glyph_string because it must be allocated via `alloca'. W
22950 is the window on which S is drawn. ROW and AREA are the glyph row
22951 and area within the row from which S is constructed. START is the
22952 index of the first glyph structure covered by S. HL is a
22953 face-override for drawing S. */
22954
22955 #ifdef HAVE_NTGUI
22956 #define OPTIONAL_HDC(hdc) HDC hdc,
22957 #define DECLARE_HDC(hdc) HDC hdc;
22958 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22959 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22960 #endif
22961
22962 #ifndef OPTIONAL_HDC
22963 #define OPTIONAL_HDC(hdc)
22964 #define DECLARE_HDC(hdc)
22965 #define ALLOCATE_HDC(hdc, f)
22966 #define RELEASE_HDC(hdc, f)
22967 #endif
22968
22969 static void
22970 init_glyph_string (struct glyph_string *s,
22971 OPTIONAL_HDC (hdc)
22972 XChar2b *char2b, struct window *w, struct glyph_row *row,
22973 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22974 {
22975 memset (s, 0, sizeof *s);
22976 s->w = w;
22977 s->f = XFRAME (w->frame);
22978 #ifdef HAVE_NTGUI
22979 s->hdc = hdc;
22980 #endif
22981 s->display = FRAME_X_DISPLAY (s->f);
22982 s->window = FRAME_X_WINDOW (s->f);
22983 s->char2b = char2b;
22984 s->hl = hl;
22985 s->row = row;
22986 s->area = area;
22987 s->first_glyph = row->glyphs[area] + start;
22988 s->height = row->height;
22989 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22990 s->ybase = s->y + row->ascent;
22991 }
22992
22993
22994 /* Append the list of glyph strings with head H and tail T to the list
22995 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22996
22997 static void
22998 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22999 struct glyph_string *h, struct glyph_string *t)
23000 {
23001 if (h)
23002 {
23003 if (*head)
23004 (*tail)->next = h;
23005 else
23006 *head = h;
23007 h->prev = *tail;
23008 *tail = t;
23009 }
23010 }
23011
23012
23013 /* Prepend the list of glyph strings with head H and tail T to the
23014 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23015 result. */
23016
23017 static void
23018 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23019 struct glyph_string *h, struct glyph_string *t)
23020 {
23021 if (h)
23022 {
23023 if (*head)
23024 (*head)->prev = t;
23025 else
23026 *tail = t;
23027 t->next = *head;
23028 *head = h;
23029 }
23030 }
23031
23032
23033 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23034 Set *HEAD and *TAIL to the resulting list. */
23035
23036 static void
23037 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23038 struct glyph_string *s)
23039 {
23040 s->next = s->prev = NULL;
23041 append_glyph_string_lists (head, tail, s, s);
23042 }
23043
23044
23045 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23046 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23047 make sure that X resources for the face returned are allocated.
23048 Value is a pointer to a realized face that is ready for display if
23049 DISPLAY_P is non-zero. */
23050
23051 static struct face *
23052 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23053 XChar2b *char2b, int display_p)
23054 {
23055 struct face *face = FACE_FROM_ID (f, face_id);
23056 unsigned code = 0;
23057
23058 if (face->font)
23059 {
23060 code = face->font->driver->encode_char (face->font, c);
23061
23062 if (code == FONT_INVALID_CODE)
23063 code = 0;
23064 }
23065 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23066
23067 /* Make sure X resources of the face are allocated. */
23068 #ifdef HAVE_X_WINDOWS
23069 if (display_p)
23070 #endif
23071 {
23072 eassert (face != NULL);
23073 PREPARE_FACE_FOR_DISPLAY (f, face);
23074 }
23075
23076 return face;
23077 }
23078
23079
23080 /* Get face and two-byte form of character glyph GLYPH on frame F.
23081 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23082 a pointer to a realized face that is ready for display. */
23083
23084 static struct face *
23085 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23086 XChar2b *char2b, int *two_byte_p)
23087 {
23088 struct face *face;
23089 unsigned code = 0;
23090
23091 eassert (glyph->type == CHAR_GLYPH);
23092 face = FACE_FROM_ID (f, glyph->face_id);
23093
23094 /* Make sure X resources of the face are allocated. */
23095 eassert (face != NULL);
23096 PREPARE_FACE_FOR_DISPLAY (f, face);
23097
23098 if (two_byte_p)
23099 *two_byte_p = 0;
23100
23101 if (face->font)
23102 {
23103 if (CHAR_BYTE8_P (glyph->u.ch))
23104 code = CHAR_TO_BYTE8 (glyph->u.ch);
23105 else
23106 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23107
23108 if (code == FONT_INVALID_CODE)
23109 code = 0;
23110 }
23111
23112 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23113 return face;
23114 }
23115
23116
23117 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23118 Return 1 if FONT has a glyph for C, otherwise return 0. */
23119
23120 static int
23121 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23122 {
23123 unsigned code;
23124
23125 if (CHAR_BYTE8_P (c))
23126 code = CHAR_TO_BYTE8 (c);
23127 else
23128 code = font->driver->encode_char (font, c);
23129
23130 if (code == FONT_INVALID_CODE)
23131 return 0;
23132 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23133 return 1;
23134 }
23135
23136
23137 /* Fill glyph string S with composition components specified by S->cmp.
23138
23139 BASE_FACE is the base face of the composition.
23140 S->cmp_from is the index of the first component for S.
23141
23142 OVERLAPS non-zero means S should draw the foreground only, and use
23143 its physical height for clipping. See also draw_glyphs.
23144
23145 Value is the index of a component not in S. */
23146
23147 static int
23148 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23149 int overlaps)
23150 {
23151 int i;
23152 /* For all glyphs of this composition, starting at the offset
23153 S->cmp_from, until we reach the end of the definition or encounter a
23154 glyph that requires the different face, add it to S. */
23155 struct face *face;
23156
23157 eassert (s);
23158
23159 s->for_overlaps = overlaps;
23160 s->face = NULL;
23161 s->font = NULL;
23162 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23163 {
23164 int c = COMPOSITION_GLYPH (s->cmp, i);
23165
23166 /* TAB in a composition means display glyphs with padding space
23167 on the left or right. */
23168 if (c != '\t')
23169 {
23170 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23171 -1, Qnil);
23172
23173 face = get_char_face_and_encoding (s->f, c, face_id,
23174 s->char2b + i, 1);
23175 if (face)
23176 {
23177 if (! s->face)
23178 {
23179 s->face = face;
23180 s->font = s->face->font;
23181 }
23182 else if (s->face != face)
23183 break;
23184 }
23185 }
23186 ++s->nchars;
23187 }
23188 s->cmp_to = i;
23189
23190 if (s->face == NULL)
23191 {
23192 s->face = base_face->ascii_face;
23193 s->font = s->face->font;
23194 }
23195
23196 /* All glyph strings for the same composition has the same width,
23197 i.e. the width set for the first component of the composition. */
23198 s->width = s->first_glyph->pixel_width;
23199
23200 /* If the specified font could not be loaded, use the frame's
23201 default font, but record the fact that we couldn't load it in
23202 the glyph string so that we can draw rectangles for the
23203 characters of the glyph string. */
23204 if (s->font == NULL)
23205 {
23206 s->font_not_found_p = 1;
23207 s->font = FRAME_FONT (s->f);
23208 }
23209
23210 /* Adjust base line for subscript/superscript text. */
23211 s->ybase += s->first_glyph->voffset;
23212
23213 /* This glyph string must always be drawn with 16-bit functions. */
23214 s->two_byte_p = 1;
23215
23216 return s->cmp_to;
23217 }
23218
23219 static int
23220 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23221 int start, int end, int overlaps)
23222 {
23223 struct glyph *glyph, *last;
23224 Lisp_Object lgstring;
23225 int i;
23226
23227 s->for_overlaps = overlaps;
23228 glyph = s->row->glyphs[s->area] + start;
23229 last = s->row->glyphs[s->area] + end;
23230 s->cmp_id = glyph->u.cmp.id;
23231 s->cmp_from = glyph->slice.cmp.from;
23232 s->cmp_to = glyph->slice.cmp.to + 1;
23233 s->face = FACE_FROM_ID (s->f, face_id);
23234 lgstring = composition_gstring_from_id (s->cmp_id);
23235 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23236 glyph++;
23237 while (glyph < last
23238 && glyph->u.cmp.automatic
23239 && glyph->u.cmp.id == s->cmp_id
23240 && s->cmp_to == glyph->slice.cmp.from)
23241 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23242
23243 for (i = s->cmp_from; i < s->cmp_to; i++)
23244 {
23245 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23246 unsigned code = LGLYPH_CODE (lglyph);
23247
23248 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23249 }
23250 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23251 return glyph - s->row->glyphs[s->area];
23252 }
23253
23254
23255 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23256 See the comment of fill_glyph_string for arguments.
23257 Value is the index of the first glyph not in S. */
23258
23259
23260 static int
23261 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23262 int start, int end, int overlaps)
23263 {
23264 struct glyph *glyph, *last;
23265 int voffset;
23266
23267 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23268 s->for_overlaps = overlaps;
23269 glyph = s->row->glyphs[s->area] + start;
23270 last = s->row->glyphs[s->area] + end;
23271 voffset = glyph->voffset;
23272 s->face = FACE_FROM_ID (s->f, face_id);
23273 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23274 s->nchars = 1;
23275 s->width = glyph->pixel_width;
23276 glyph++;
23277 while (glyph < last
23278 && glyph->type == GLYPHLESS_GLYPH
23279 && glyph->voffset == voffset
23280 && glyph->face_id == face_id)
23281 {
23282 s->nchars++;
23283 s->width += glyph->pixel_width;
23284 glyph++;
23285 }
23286 s->ybase += voffset;
23287 return glyph - s->row->glyphs[s->area];
23288 }
23289
23290
23291 /* Fill glyph string S from a sequence of character glyphs.
23292
23293 FACE_ID is the face id of the string. START is the index of the
23294 first glyph to consider, END is the index of the last + 1.
23295 OVERLAPS non-zero means S should draw the foreground only, and use
23296 its physical height for clipping. See also draw_glyphs.
23297
23298 Value is the index of the first glyph not in S. */
23299
23300 static int
23301 fill_glyph_string (struct glyph_string *s, int face_id,
23302 int start, int end, int overlaps)
23303 {
23304 struct glyph *glyph, *last;
23305 int voffset;
23306 int glyph_not_available_p;
23307
23308 eassert (s->f == XFRAME (s->w->frame));
23309 eassert (s->nchars == 0);
23310 eassert (start >= 0 && end > start);
23311
23312 s->for_overlaps = overlaps;
23313 glyph = s->row->glyphs[s->area] + start;
23314 last = s->row->glyphs[s->area] + end;
23315 voffset = glyph->voffset;
23316 s->padding_p = glyph->padding_p;
23317 glyph_not_available_p = glyph->glyph_not_available_p;
23318
23319 while (glyph < last
23320 && glyph->type == CHAR_GLYPH
23321 && glyph->voffset == voffset
23322 /* Same face id implies same font, nowadays. */
23323 && glyph->face_id == face_id
23324 && glyph->glyph_not_available_p == glyph_not_available_p)
23325 {
23326 int two_byte_p;
23327
23328 s->face = get_glyph_face_and_encoding (s->f, glyph,
23329 s->char2b + s->nchars,
23330 &two_byte_p);
23331 s->two_byte_p = two_byte_p;
23332 ++s->nchars;
23333 eassert (s->nchars <= end - start);
23334 s->width += glyph->pixel_width;
23335 if (glyph++->padding_p != s->padding_p)
23336 break;
23337 }
23338
23339 s->font = s->face->font;
23340
23341 /* If the specified font could not be loaded, use the frame's font,
23342 but record the fact that we couldn't load it in
23343 S->font_not_found_p so that we can draw rectangles for the
23344 characters of the glyph string. */
23345 if (s->font == NULL || glyph_not_available_p)
23346 {
23347 s->font_not_found_p = 1;
23348 s->font = FRAME_FONT (s->f);
23349 }
23350
23351 /* Adjust base line for subscript/superscript text. */
23352 s->ybase += voffset;
23353
23354 eassert (s->face && s->face->gc);
23355 return glyph - s->row->glyphs[s->area];
23356 }
23357
23358
23359 /* Fill glyph string S from image glyph S->first_glyph. */
23360
23361 static void
23362 fill_image_glyph_string (struct glyph_string *s)
23363 {
23364 eassert (s->first_glyph->type == IMAGE_GLYPH);
23365 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23366 eassert (s->img);
23367 s->slice = s->first_glyph->slice.img;
23368 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23369 s->font = s->face->font;
23370 s->width = s->first_glyph->pixel_width;
23371
23372 /* Adjust base line for subscript/superscript text. */
23373 s->ybase += s->first_glyph->voffset;
23374 }
23375
23376
23377 /* Fill glyph string S from a sequence of stretch glyphs.
23378
23379 START is the index of the first glyph to consider,
23380 END is the index of the last + 1.
23381
23382 Value is the index of the first glyph not in S. */
23383
23384 static int
23385 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23386 {
23387 struct glyph *glyph, *last;
23388 int voffset, face_id;
23389
23390 eassert (s->first_glyph->type == STRETCH_GLYPH);
23391
23392 glyph = s->row->glyphs[s->area] + start;
23393 last = s->row->glyphs[s->area] + end;
23394 face_id = glyph->face_id;
23395 s->face = FACE_FROM_ID (s->f, face_id);
23396 s->font = s->face->font;
23397 s->width = glyph->pixel_width;
23398 s->nchars = 1;
23399 voffset = glyph->voffset;
23400
23401 for (++glyph;
23402 (glyph < last
23403 && glyph->type == STRETCH_GLYPH
23404 && glyph->voffset == voffset
23405 && glyph->face_id == face_id);
23406 ++glyph)
23407 s->width += glyph->pixel_width;
23408
23409 /* Adjust base line for subscript/superscript text. */
23410 s->ybase += voffset;
23411
23412 /* The case that face->gc == 0 is handled when drawing the glyph
23413 string by calling PREPARE_FACE_FOR_DISPLAY. */
23414 eassert (s->face);
23415 return glyph - s->row->glyphs[s->area];
23416 }
23417
23418 static struct font_metrics *
23419 get_per_char_metric (struct font *font, XChar2b *char2b)
23420 {
23421 static struct font_metrics metrics;
23422 unsigned code;
23423
23424 if (! font)
23425 return NULL;
23426 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23427 if (code == FONT_INVALID_CODE)
23428 return NULL;
23429 font->driver->text_extents (font, &code, 1, &metrics);
23430 return &metrics;
23431 }
23432
23433 /* EXPORT for RIF:
23434 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23435 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23436 assumed to be zero. */
23437
23438 void
23439 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23440 {
23441 *left = *right = 0;
23442
23443 if (glyph->type == CHAR_GLYPH)
23444 {
23445 struct face *face;
23446 XChar2b char2b;
23447 struct font_metrics *pcm;
23448
23449 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23450 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23451 {
23452 if (pcm->rbearing > pcm->width)
23453 *right = pcm->rbearing - pcm->width;
23454 if (pcm->lbearing < 0)
23455 *left = -pcm->lbearing;
23456 }
23457 }
23458 else if (glyph->type == COMPOSITE_GLYPH)
23459 {
23460 if (! glyph->u.cmp.automatic)
23461 {
23462 struct composition *cmp = composition_table[glyph->u.cmp.id];
23463
23464 if (cmp->rbearing > cmp->pixel_width)
23465 *right = cmp->rbearing - cmp->pixel_width;
23466 if (cmp->lbearing < 0)
23467 *left = - cmp->lbearing;
23468 }
23469 else
23470 {
23471 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23472 struct font_metrics metrics;
23473
23474 composition_gstring_width (gstring, glyph->slice.cmp.from,
23475 glyph->slice.cmp.to + 1, &metrics);
23476 if (metrics.rbearing > metrics.width)
23477 *right = metrics.rbearing - metrics.width;
23478 if (metrics.lbearing < 0)
23479 *left = - metrics.lbearing;
23480 }
23481 }
23482 }
23483
23484
23485 /* Return the index of the first glyph preceding glyph string S that
23486 is overwritten by S because of S's left overhang. Value is -1
23487 if no glyphs are overwritten. */
23488
23489 static int
23490 left_overwritten (struct glyph_string *s)
23491 {
23492 int k;
23493
23494 if (s->left_overhang)
23495 {
23496 int x = 0, i;
23497 struct glyph *glyphs = s->row->glyphs[s->area];
23498 int first = s->first_glyph - glyphs;
23499
23500 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23501 x -= glyphs[i].pixel_width;
23502
23503 k = i + 1;
23504 }
23505 else
23506 k = -1;
23507
23508 return k;
23509 }
23510
23511
23512 /* Return the index of the first glyph preceding glyph string S that
23513 is overwriting S because of its right overhang. Value is -1 if no
23514 glyph in front of S overwrites S. */
23515
23516 static int
23517 left_overwriting (struct glyph_string *s)
23518 {
23519 int i, k, x;
23520 struct glyph *glyphs = s->row->glyphs[s->area];
23521 int first = s->first_glyph - glyphs;
23522
23523 k = -1;
23524 x = 0;
23525 for (i = first - 1; i >= 0; --i)
23526 {
23527 int left, right;
23528 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23529 if (x + right > 0)
23530 k = i;
23531 x -= glyphs[i].pixel_width;
23532 }
23533
23534 return k;
23535 }
23536
23537
23538 /* Return the index of the last glyph following glyph string S that is
23539 overwritten by S because of S's right overhang. Value is -1 if
23540 no such glyph is found. */
23541
23542 static int
23543 right_overwritten (struct glyph_string *s)
23544 {
23545 int k = -1;
23546
23547 if (s->right_overhang)
23548 {
23549 int x = 0, i;
23550 struct glyph *glyphs = s->row->glyphs[s->area];
23551 int first = (s->first_glyph - glyphs
23552 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23553 int end = s->row->used[s->area];
23554
23555 for (i = first; i < end && s->right_overhang > x; ++i)
23556 x += glyphs[i].pixel_width;
23557
23558 k = i;
23559 }
23560
23561 return k;
23562 }
23563
23564
23565 /* Return the index of the last glyph following glyph string S that
23566 overwrites S because of its left overhang. Value is negative
23567 if no such glyph is found. */
23568
23569 static int
23570 right_overwriting (struct glyph_string *s)
23571 {
23572 int i, k, x;
23573 int end = s->row->used[s->area];
23574 struct glyph *glyphs = s->row->glyphs[s->area];
23575 int first = (s->first_glyph - glyphs
23576 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23577
23578 k = -1;
23579 x = 0;
23580 for (i = first; i < end; ++i)
23581 {
23582 int left, right;
23583 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23584 if (x - left < 0)
23585 k = i;
23586 x += glyphs[i].pixel_width;
23587 }
23588
23589 return k;
23590 }
23591
23592
23593 /* Set background width of glyph string S. START is the index of the
23594 first glyph following S. LAST_X is the right-most x-position + 1
23595 in the drawing area. */
23596
23597 static void
23598 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23599 {
23600 /* If the face of this glyph string has to be drawn to the end of
23601 the drawing area, set S->extends_to_end_of_line_p. */
23602
23603 if (start == s->row->used[s->area]
23604 && s->area == TEXT_AREA
23605 && ((s->row->fill_line_p
23606 && (s->hl == DRAW_NORMAL_TEXT
23607 || s->hl == DRAW_IMAGE_RAISED
23608 || s->hl == DRAW_IMAGE_SUNKEN))
23609 || s->hl == DRAW_MOUSE_FACE))
23610 s->extends_to_end_of_line_p = 1;
23611
23612 /* If S extends its face to the end of the line, set its
23613 background_width to the distance to the right edge of the drawing
23614 area. */
23615 if (s->extends_to_end_of_line_p)
23616 s->background_width = last_x - s->x + 1;
23617 else
23618 s->background_width = s->width;
23619 }
23620
23621
23622 /* Compute overhangs and x-positions for glyph string S and its
23623 predecessors, or successors. X is the starting x-position for S.
23624 BACKWARD_P non-zero means process predecessors. */
23625
23626 static void
23627 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23628 {
23629 if (backward_p)
23630 {
23631 while (s)
23632 {
23633 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23634 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23635 x -= s->width;
23636 s->x = x;
23637 s = s->prev;
23638 }
23639 }
23640 else
23641 {
23642 while (s)
23643 {
23644 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23645 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23646 s->x = x;
23647 x += s->width;
23648 s = s->next;
23649 }
23650 }
23651 }
23652
23653
23654
23655 /* The following macros are only called from draw_glyphs below.
23656 They reference the following parameters of that function directly:
23657 `w', `row', `area', and `overlap_p'
23658 as well as the following local variables:
23659 `s', `f', and `hdc' (in W32) */
23660
23661 #ifdef HAVE_NTGUI
23662 /* On W32, silently add local `hdc' variable to argument list of
23663 init_glyph_string. */
23664 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23665 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23666 #else
23667 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23668 init_glyph_string (s, char2b, w, row, area, start, hl)
23669 #endif
23670
23671 /* Add a glyph string for a stretch glyph to the list of strings
23672 between HEAD and TAIL. START is the index of the stretch glyph in
23673 row area AREA of glyph row ROW. END is the index of the last glyph
23674 in that glyph row area. X is the current output position assigned
23675 to the new glyph string constructed. HL overrides that face of the
23676 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23677 is the right-most x-position of the drawing area. */
23678
23679 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23680 and below -- keep them on one line. */
23681 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23682 do \
23683 { \
23684 s = alloca (sizeof *s); \
23685 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23686 START = fill_stretch_glyph_string (s, START, END); \
23687 append_glyph_string (&HEAD, &TAIL, s); \
23688 s->x = (X); \
23689 } \
23690 while (0)
23691
23692
23693 /* Add a glyph string for an image glyph to the list of strings
23694 between HEAD and TAIL. START is the index of the image glyph in
23695 row area AREA of glyph row ROW. END is the index of the last glyph
23696 in that glyph row area. X is the current output position assigned
23697 to the new glyph string constructed. HL overrides that face of the
23698 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23699 is the right-most x-position of the drawing area. */
23700
23701 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23702 do \
23703 { \
23704 s = alloca (sizeof *s); \
23705 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23706 fill_image_glyph_string (s); \
23707 append_glyph_string (&HEAD, &TAIL, s); \
23708 ++START; \
23709 s->x = (X); \
23710 } \
23711 while (0)
23712
23713
23714 /* Add a glyph string for a sequence of character glyphs to the list
23715 of strings between HEAD and TAIL. START is the index of the first
23716 glyph in row area AREA of glyph row ROW that is part of the new
23717 glyph string. END is the index of the last glyph in that glyph row
23718 area. X is the current output position assigned to the new glyph
23719 string constructed. HL overrides that face of the glyph; e.g. it
23720 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23721 right-most x-position of the drawing area. */
23722
23723 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23724 do \
23725 { \
23726 int face_id; \
23727 XChar2b *char2b; \
23728 \
23729 face_id = (row)->glyphs[area][START].face_id; \
23730 \
23731 s = alloca (sizeof *s); \
23732 char2b = alloca ((END - START) * sizeof *char2b); \
23733 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23734 append_glyph_string (&HEAD, &TAIL, s); \
23735 s->x = (X); \
23736 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23737 } \
23738 while (0)
23739
23740
23741 /* Add a glyph string for a composite sequence to the list of strings
23742 between HEAD and TAIL. START is the index of the first glyph in
23743 row area AREA of glyph row ROW that is part of the new glyph
23744 string. END is the index of the last glyph in that glyph row area.
23745 X is the current output position assigned to the new glyph string
23746 constructed. HL overrides that face of the glyph; e.g. it is
23747 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23748 x-position of the drawing area. */
23749
23750 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23751 do { \
23752 int face_id = (row)->glyphs[area][START].face_id; \
23753 struct face *base_face = FACE_FROM_ID (f, face_id); \
23754 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23755 struct composition *cmp = composition_table[cmp_id]; \
23756 XChar2b *char2b; \
23757 struct glyph_string *first_s = NULL; \
23758 int n; \
23759 \
23760 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23761 \
23762 /* Make glyph_strings for each glyph sequence that is drawable by \
23763 the same face, and append them to HEAD/TAIL. */ \
23764 for (n = 0; n < cmp->glyph_len;) \
23765 { \
23766 s = alloca (sizeof *s); \
23767 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23768 append_glyph_string (&(HEAD), &(TAIL), s); \
23769 s->cmp = cmp; \
23770 s->cmp_from = n; \
23771 s->x = (X); \
23772 if (n == 0) \
23773 first_s = s; \
23774 n = fill_composite_glyph_string (s, base_face, overlaps); \
23775 } \
23776 \
23777 ++START; \
23778 s = first_s; \
23779 } while (0)
23780
23781
23782 /* Add a glyph string for a glyph-string sequence to the list of strings
23783 between HEAD and TAIL. */
23784
23785 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23786 do { \
23787 int face_id; \
23788 XChar2b *char2b; \
23789 Lisp_Object gstring; \
23790 \
23791 face_id = (row)->glyphs[area][START].face_id; \
23792 gstring = (composition_gstring_from_id \
23793 ((row)->glyphs[area][START].u.cmp.id)); \
23794 s = alloca (sizeof *s); \
23795 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23796 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23797 append_glyph_string (&(HEAD), &(TAIL), s); \
23798 s->x = (X); \
23799 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23800 } while (0)
23801
23802
23803 /* Add a glyph string for a sequence of glyphless character's glyphs
23804 to the list of strings between HEAD and TAIL. The meanings of
23805 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23806
23807 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23808 do \
23809 { \
23810 int face_id; \
23811 \
23812 face_id = (row)->glyphs[area][START].face_id; \
23813 \
23814 s = alloca (sizeof *s); \
23815 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23816 append_glyph_string (&HEAD, &TAIL, s); \
23817 s->x = (X); \
23818 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23819 overlaps); \
23820 } \
23821 while (0)
23822
23823
23824 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23825 of AREA of glyph row ROW on window W between indices START and END.
23826 HL overrides the face for drawing glyph strings, e.g. it is
23827 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23828 x-positions of the drawing area.
23829
23830 This is an ugly monster macro construct because we must use alloca
23831 to allocate glyph strings (because draw_glyphs can be called
23832 asynchronously). */
23833
23834 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23835 do \
23836 { \
23837 HEAD = TAIL = NULL; \
23838 while (START < END) \
23839 { \
23840 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23841 switch (first_glyph->type) \
23842 { \
23843 case CHAR_GLYPH: \
23844 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23845 HL, X, LAST_X); \
23846 break; \
23847 \
23848 case COMPOSITE_GLYPH: \
23849 if (first_glyph->u.cmp.automatic) \
23850 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23851 HL, X, LAST_X); \
23852 else \
23853 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23854 HL, X, LAST_X); \
23855 break; \
23856 \
23857 case STRETCH_GLYPH: \
23858 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23859 HL, X, LAST_X); \
23860 break; \
23861 \
23862 case IMAGE_GLYPH: \
23863 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23864 HL, X, LAST_X); \
23865 break; \
23866 \
23867 case GLYPHLESS_GLYPH: \
23868 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23869 HL, X, LAST_X); \
23870 break; \
23871 \
23872 default: \
23873 emacs_abort (); \
23874 } \
23875 \
23876 if (s) \
23877 { \
23878 set_glyph_string_background_width (s, START, LAST_X); \
23879 (X) += s->width; \
23880 } \
23881 } \
23882 } while (0)
23883
23884
23885 /* Draw glyphs between START and END in AREA of ROW on window W,
23886 starting at x-position X. X is relative to AREA in W. HL is a
23887 face-override with the following meaning:
23888
23889 DRAW_NORMAL_TEXT draw normally
23890 DRAW_CURSOR draw in cursor face
23891 DRAW_MOUSE_FACE draw in mouse face.
23892 DRAW_INVERSE_VIDEO draw in mode line face
23893 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23894 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23895
23896 If OVERLAPS is non-zero, draw only the foreground of characters and
23897 clip to the physical height of ROW. Non-zero value also defines
23898 the overlapping part to be drawn:
23899
23900 OVERLAPS_PRED overlap with preceding rows
23901 OVERLAPS_SUCC overlap with succeeding rows
23902 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23903 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23904
23905 Value is the x-position reached, relative to AREA of W. */
23906
23907 static int
23908 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23909 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23910 enum draw_glyphs_face hl, int overlaps)
23911 {
23912 struct glyph_string *head, *tail;
23913 struct glyph_string *s;
23914 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23915 int i, j, x_reached, last_x, area_left = 0;
23916 struct frame *f = XFRAME (WINDOW_FRAME (w));
23917 DECLARE_HDC (hdc);
23918
23919 ALLOCATE_HDC (hdc, f);
23920
23921 /* Let's rather be paranoid than getting a SEGV. */
23922 end = min (end, row->used[area]);
23923 start = clip_to_bounds (0, start, end);
23924
23925 /* Translate X to frame coordinates. Set last_x to the right
23926 end of the drawing area. */
23927 if (row->full_width_p)
23928 {
23929 /* X is relative to the left edge of W, without scroll bars
23930 or fringes. */
23931 area_left = WINDOW_LEFT_EDGE_X (w);
23932 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23933 }
23934 else
23935 {
23936 area_left = window_box_left (w, area);
23937 last_x = area_left + window_box_width (w, area);
23938 }
23939 x += area_left;
23940
23941 /* Build a doubly-linked list of glyph_string structures between
23942 head and tail from what we have to draw. Note that the macro
23943 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23944 the reason we use a separate variable `i'. */
23945 i = start;
23946 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23947 if (tail)
23948 x_reached = tail->x + tail->background_width;
23949 else
23950 x_reached = x;
23951
23952 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23953 the row, redraw some glyphs in front or following the glyph
23954 strings built above. */
23955 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23956 {
23957 struct glyph_string *h, *t;
23958 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23959 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23960 int check_mouse_face = 0;
23961 int dummy_x = 0;
23962
23963 /* If mouse highlighting is on, we may need to draw adjacent
23964 glyphs using mouse-face highlighting. */
23965 if (area == TEXT_AREA && row->mouse_face_p
23966 && hlinfo->mouse_face_beg_row >= 0
23967 && hlinfo->mouse_face_end_row >= 0)
23968 {
23969 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23970
23971 if (row_vpos >= hlinfo->mouse_face_beg_row
23972 && row_vpos <= hlinfo->mouse_face_end_row)
23973 {
23974 check_mouse_face = 1;
23975 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23976 ? hlinfo->mouse_face_beg_col : 0;
23977 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23978 ? hlinfo->mouse_face_end_col
23979 : row->used[TEXT_AREA];
23980 }
23981 }
23982
23983 /* Compute overhangs for all glyph strings. */
23984 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23985 for (s = head; s; s = s->next)
23986 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23987
23988 /* Prepend glyph strings for glyphs in front of the first glyph
23989 string that are overwritten because of the first glyph
23990 string's left overhang. The background of all strings
23991 prepended must be drawn because the first glyph string
23992 draws over it. */
23993 i = left_overwritten (head);
23994 if (i >= 0)
23995 {
23996 enum draw_glyphs_face overlap_hl;
23997
23998 /* If this row contains mouse highlighting, attempt to draw
23999 the overlapped glyphs with the correct highlight. This
24000 code fails if the overlap encompasses more than one glyph
24001 and mouse-highlight spans only some of these glyphs.
24002 However, making it work perfectly involves a lot more
24003 code, and I don't know if the pathological case occurs in
24004 practice, so we'll stick to this for now. --- cyd */
24005 if (check_mouse_face
24006 && mouse_beg_col < start && mouse_end_col > i)
24007 overlap_hl = DRAW_MOUSE_FACE;
24008 else
24009 overlap_hl = DRAW_NORMAL_TEXT;
24010
24011 j = i;
24012 BUILD_GLYPH_STRINGS (j, start, h, t,
24013 overlap_hl, dummy_x, last_x);
24014 start = i;
24015 compute_overhangs_and_x (t, head->x, 1);
24016 prepend_glyph_string_lists (&head, &tail, h, t);
24017 clip_head = head;
24018 }
24019
24020 /* Prepend glyph strings for glyphs in front of the first glyph
24021 string that overwrite that glyph string because of their
24022 right overhang. For these strings, only the foreground must
24023 be drawn, because it draws over the glyph string at `head'.
24024 The background must not be drawn because this would overwrite
24025 right overhangs of preceding glyphs for which no glyph
24026 strings exist. */
24027 i = left_overwriting (head);
24028 if (i >= 0)
24029 {
24030 enum draw_glyphs_face overlap_hl;
24031
24032 if (check_mouse_face
24033 && mouse_beg_col < start && mouse_end_col > i)
24034 overlap_hl = DRAW_MOUSE_FACE;
24035 else
24036 overlap_hl = DRAW_NORMAL_TEXT;
24037
24038 clip_head = head;
24039 BUILD_GLYPH_STRINGS (i, start, h, t,
24040 overlap_hl, dummy_x, last_x);
24041 for (s = h; s; s = s->next)
24042 s->background_filled_p = 1;
24043 compute_overhangs_and_x (t, head->x, 1);
24044 prepend_glyph_string_lists (&head, &tail, h, t);
24045 }
24046
24047 /* Append glyphs strings for glyphs following the last glyph
24048 string tail that are overwritten by tail. The background of
24049 these strings has to be drawn because tail's foreground draws
24050 over it. */
24051 i = right_overwritten (tail);
24052 if (i >= 0)
24053 {
24054 enum draw_glyphs_face overlap_hl;
24055
24056 if (check_mouse_face
24057 && mouse_beg_col < i && mouse_end_col > end)
24058 overlap_hl = DRAW_MOUSE_FACE;
24059 else
24060 overlap_hl = DRAW_NORMAL_TEXT;
24061
24062 BUILD_GLYPH_STRINGS (end, i, h, t,
24063 overlap_hl, x, last_x);
24064 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24065 we don't have `end = i;' here. */
24066 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24067 append_glyph_string_lists (&head, &tail, h, t);
24068 clip_tail = tail;
24069 }
24070
24071 /* Append glyph strings for glyphs following the last glyph
24072 string tail that overwrite tail. The foreground of such
24073 glyphs has to be drawn because it writes into the background
24074 of tail. The background must not be drawn because it could
24075 paint over the foreground of following glyphs. */
24076 i = right_overwriting (tail);
24077 if (i >= 0)
24078 {
24079 enum draw_glyphs_face overlap_hl;
24080 if (check_mouse_face
24081 && mouse_beg_col < i && mouse_end_col > end)
24082 overlap_hl = DRAW_MOUSE_FACE;
24083 else
24084 overlap_hl = DRAW_NORMAL_TEXT;
24085
24086 clip_tail = tail;
24087 i++; /* We must include the Ith glyph. */
24088 BUILD_GLYPH_STRINGS (end, i, h, t,
24089 overlap_hl, x, last_x);
24090 for (s = h; s; s = s->next)
24091 s->background_filled_p = 1;
24092 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24093 append_glyph_string_lists (&head, &tail, h, t);
24094 }
24095 if (clip_head || clip_tail)
24096 for (s = head; s; s = s->next)
24097 {
24098 s->clip_head = clip_head;
24099 s->clip_tail = clip_tail;
24100 }
24101 }
24102
24103 /* Draw all strings. */
24104 for (s = head; s; s = s->next)
24105 FRAME_RIF (f)->draw_glyph_string (s);
24106
24107 #ifndef HAVE_NS
24108 /* When focus a sole frame and move horizontally, this sets on_p to 0
24109 causing a failure to erase prev cursor position. */
24110 if (area == TEXT_AREA
24111 && !row->full_width_p
24112 /* When drawing overlapping rows, only the glyph strings'
24113 foreground is drawn, which doesn't erase a cursor
24114 completely. */
24115 && !overlaps)
24116 {
24117 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24118 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24119 : (tail ? tail->x + tail->background_width : x));
24120 x0 -= area_left;
24121 x1 -= area_left;
24122
24123 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24124 row->y, MATRIX_ROW_BOTTOM_Y (row));
24125 }
24126 #endif
24127
24128 /* Value is the x-position up to which drawn, relative to AREA of W.
24129 This doesn't include parts drawn because of overhangs. */
24130 if (row->full_width_p)
24131 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24132 else
24133 x_reached -= area_left;
24134
24135 RELEASE_HDC (hdc, f);
24136
24137 return x_reached;
24138 }
24139
24140 /* Expand row matrix if too narrow. Don't expand if area
24141 is not present. */
24142
24143 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24144 { \
24145 if (!it->f->fonts_changed \
24146 && (it->glyph_row->glyphs[area] \
24147 < it->glyph_row->glyphs[area + 1])) \
24148 { \
24149 it->w->ncols_scale_factor++; \
24150 it->f->fonts_changed = 1; \
24151 } \
24152 }
24153
24154 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24155 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24156
24157 static void
24158 append_glyph (struct it *it)
24159 {
24160 struct glyph *glyph;
24161 enum glyph_row_area area = it->area;
24162
24163 eassert (it->glyph_row);
24164 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24165
24166 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24167 if (glyph < it->glyph_row->glyphs[area + 1])
24168 {
24169 /* If the glyph row is reversed, we need to prepend the glyph
24170 rather than append it. */
24171 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24172 {
24173 struct glyph *g;
24174
24175 /* Make room for the additional glyph. */
24176 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24177 g[1] = *g;
24178 glyph = it->glyph_row->glyphs[area];
24179 }
24180 glyph->charpos = CHARPOS (it->position);
24181 glyph->object = it->object;
24182 if (it->pixel_width > 0)
24183 {
24184 glyph->pixel_width = it->pixel_width;
24185 glyph->padding_p = 0;
24186 }
24187 else
24188 {
24189 /* Assure at least 1-pixel width. Otherwise, cursor can't
24190 be displayed correctly. */
24191 glyph->pixel_width = 1;
24192 glyph->padding_p = 1;
24193 }
24194 glyph->ascent = it->ascent;
24195 glyph->descent = it->descent;
24196 glyph->voffset = it->voffset;
24197 glyph->type = CHAR_GLYPH;
24198 glyph->avoid_cursor_p = it->avoid_cursor_p;
24199 glyph->multibyte_p = it->multibyte_p;
24200 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24201 {
24202 /* In R2L rows, the left and the right box edges need to be
24203 drawn in reverse direction. */
24204 glyph->right_box_line_p = it->start_of_box_run_p;
24205 glyph->left_box_line_p = it->end_of_box_run_p;
24206 }
24207 else
24208 {
24209 glyph->left_box_line_p = it->start_of_box_run_p;
24210 glyph->right_box_line_p = it->end_of_box_run_p;
24211 }
24212 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24213 || it->phys_descent > it->descent);
24214 glyph->glyph_not_available_p = it->glyph_not_available_p;
24215 glyph->face_id = it->face_id;
24216 glyph->u.ch = it->char_to_display;
24217 glyph->slice.img = null_glyph_slice;
24218 glyph->font_type = FONT_TYPE_UNKNOWN;
24219 if (it->bidi_p)
24220 {
24221 glyph->resolved_level = it->bidi_it.resolved_level;
24222 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24223 emacs_abort ();
24224 glyph->bidi_type = it->bidi_it.type;
24225 }
24226 else
24227 {
24228 glyph->resolved_level = 0;
24229 glyph->bidi_type = UNKNOWN_BT;
24230 }
24231 ++it->glyph_row->used[area];
24232 }
24233 else
24234 IT_EXPAND_MATRIX_WIDTH (it, area);
24235 }
24236
24237 /* Store one glyph for the composition IT->cmp_it.id in
24238 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24239 non-null. */
24240
24241 static void
24242 append_composite_glyph (struct it *it)
24243 {
24244 struct glyph *glyph;
24245 enum glyph_row_area area = it->area;
24246
24247 eassert (it->glyph_row);
24248
24249 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24250 if (glyph < it->glyph_row->glyphs[area + 1])
24251 {
24252 /* If the glyph row is reversed, we need to prepend the glyph
24253 rather than append it. */
24254 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24255 {
24256 struct glyph *g;
24257
24258 /* Make room for the new glyph. */
24259 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24260 g[1] = *g;
24261 glyph = it->glyph_row->glyphs[it->area];
24262 }
24263 glyph->charpos = it->cmp_it.charpos;
24264 glyph->object = it->object;
24265 glyph->pixel_width = it->pixel_width;
24266 glyph->ascent = it->ascent;
24267 glyph->descent = it->descent;
24268 glyph->voffset = it->voffset;
24269 glyph->type = COMPOSITE_GLYPH;
24270 if (it->cmp_it.ch < 0)
24271 {
24272 glyph->u.cmp.automatic = 0;
24273 glyph->u.cmp.id = it->cmp_it.id;
24274 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24275 }
24276 else
24277 {
24278 glyph->u.cmp.automatic = 1;
24279 glyph->u.cmp.id = it->cmp_it.id;
24280 glyph->slice.cmp.from = it->cmp_it.from;
24281 glyph->slice.cmp.to = it->cmp_it.to - 1;
24282 }
24283 glyph->avoid_cursor_p = it->avoid_cursor_p;
24284 glyph->multibyte_p = it->multibyte_p;
24285 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24286 {
24287 /* In R2L rows, the left and the right box edges need to be
24288 drawn in reverse direction. */
24289 glyph->right_box_line_p = it->start_of_box_run_p;
24290 glyph->left_box_line_p = it->end_of_box_run_p;
24291 }
24292 else
24293 {
24294 glyph->left_box_line_p = it->start_of_box_run_p;
24295 glyph->right_box_line_p = it->end_of_box_run_p;
24296 }
24297 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24298 || it->phys_descent > it->descent);
24299 glyph->padding_p = 0;
24300 glyph->glyph_not_available_p = 0;
24301 glyph->face_id = it->face_id;
24302 glyph->font_type = FONT_TYPE_UNKNOWN;
24303 if (it->bidi_p)
24304 {
24305 glyph->resolved_level = it->bidi_it.resolved_level;
24306 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24307 emacs_abort ();
24308 glyph->bidi_type = it->bidi_it.type;
24309 }
24310 ++it->glyph_row->used[area];
24311 }
24312 else
24313 IT_EXPAND_MATRIX_WIDTH (it, area);
24314 }
24315
24316
24317 /* Change IT->ascent and IT->height according to the setting of
24318 IT->voffset. */
24319
24320 static void
24321 take_vertical_position_into_account (struct it *it)
24322 {
24323 if (it->voffset)
24324 {
24325 if (it->voffset < 0)
24326 /* Increase the ascent so that we can display the text higher
24327 in the line. */
24328 it->ascent -= it->voffset;
24329 else
24330 /* Increase the descent so that we can display the text lower
24331 in the line. */
24332 it->descent += it->voffset;
24333 }
24334 }
24335
24336
24337 /* Produce glyphs/get display metrics for the image IT is loaded with.
24338 See the description of struct display_iterator in dispextern.h for
24339 an overview of struct display_iterator. */
24340
24341 static void
24342 produce_image_glyph (struct it *it)
24343 {
24344 struct image *img;
24345 struct face *face;
24346 int glyph_ascent, crop;
24347 struct glyph_slice slice;
24348
24349 eassert (it->what == IT_IMAGE);
24350
24351 face = FACE_FROM_ID (it->f, it->face_id);
24352 eassert (face);
24353 /* Make sure X resources of the face is loaded. */
24354 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24355
24356 if (it->image_id < 0)
24357 {
24358 /* Fringe bitmap. */
24359 it->ascent = it->phys_ascent = 0;
24360 it->descent = it->phys_descent = 0;
24361 it->pixel_width = 0;
24362 it->nglyphs = 0;
24363 return;
24364 }
24365
24366 img = IMAGE_FROM_ID (it->f, it->image_id);
24367 eassert (img);
24368 /* Make sure X resources of the image is loaded. */
24369 prepare_image_for_display (it->f, img);
24370
24371 slice.x = slice.y = 0;
24372 slice.width = img->width;
24373 slice.height = img->height;
24374
24375 if (INTEGERP (it->slice.x))
24376 slice.x = XINT (it->slice.x);
24377 else if (FLOATP (it->slice.x))
24378 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24379
24380 if (INTEGERP (it->slice.y))
24381 slice.y = XINT (it->slice.y);
24382 else if (FLOATP (it->slice.y))
24383 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24384
24385 if (INTEGERP (it->slice.width))
24386 slice.width = XINT (it->slice.width);
24387 else if (FLOATP (it->slice.width))
24388 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24389
24390 if (INTEGERP (it->slice.height))
24391 slice.height = XINT (it->slice.height);
24392 else if (FLOATP (it->slice.height))
24393 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24394
24395 if (slice.x >= img->width)
24396 slice.x = img->width;
24397 if (slice.y >= img->height)
24398 slice.y = img->height;
24399 if (slice.x + slice.width >= img->width)
24400 slice.width = img->width - slice.x;
24401 if (slice.y + slice.height > img->height)
24402 slice.height = img->height - slice.y;
24403
24404 if (slice.width == 0 || slice.height == 0)
24405 return;
24406
24407 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24408
24409 it->descent = slice.height - glyph_ascent;
24410 if (slice.y == 0)
24411 it->descent += img->vmargin;
24412 if (slice.y + slice.height == img->height)
24413 it->descent += img->vmargin;
24414 it->phys_descent = it->descent;
24415
24416 it->pixel_width = slice.width;
24417 if (slice.x == 0)
24418 it->pixel_width += img->hmargin;
24419 if (slice.x + slice.width == img->width)
24420 it->pixel_width += img->hmargin;
24421
24422 /* It's quite possible for images to have an ascent greater than
24423 their height, so don't get confused in that case. */
24424 if (it->descent < 0)
24425 it->descent = 0;
24426
24427 it->nglyphs = 1;
24428
24429 if (face->box != FACE_NO_BOX)
24430 {
24431 if (face->box_line_width > 0)
24432 {
24433 if (slice.y == 0)
24434 it->ascent += face->box_line_width;
24435 if (slice.y + slice.height == img->height)
24436 it->descent += face->box_line_width;
24437 }
24438
24439 if (it->start_of_box_run_p && slice.x == 0)
24440 it->pixel_width += eabs (face->box_line_width);
24441 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24442 it->pixel_width += eabs (face->box_line_width);
24443 }
24444
24445 take_vertical_position_into_account (it);
24446
24447 /* Automatically crop wide image glyphs at right edge so we can
24448 draw the cursor on same display row. */
24449 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24450 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24451 {
24452 it->pixel_width -= crop;
24453 slice.width -= crop;
24454 }
24455
24456 if (it->glyph_row)
24457 {
24458 struct glyph *glyph;
24459 enum glyph_row_area area = it->area;
24460
24461 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24462 if (glyph < it->glyph_row->glyphs[area + 1])
24463 {
24464 glyph->charpos = CHARPOS (it->position);
24465 glyph->object = it->object;
24466 glyph->pixel_width = it->pixel_width;
24467 glyph->ascent = glyph_ascent;
24468 glyph->descent = it->descent;
24469 glyph->voffset = it->voffset;
24470 glyph->type = IMAGE_GLYPH;
24471 glyph->avoid_cursor_p = it->avoid_cursor_p;
24472 glyph->multibyte_p = it->multibyte_p;
24473 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24474 {
24475 /* In R2L rows, the left and the right box edges need to be
24476 drawn in reverse direction. */
24477 glyph->right_box_line_p = it->start_of_box_run_p;
24478 glyph->left_box_line_p = it->end_of_box_run_p;
24479 }
24480 else
24481 {
24482 glyph->left_box_line_p = it->start_of_box_run_p;
24483 glyph->right_box_line_p = it->end_of_box_run_p;
24484 }
24485 glyph->overlaps_vertically_p = 0;
24486 glyph->padding_p = 0;
24487 glyph->glyph_not_available_p = 0;
24488 glyph->face_id = it->face_id;
24489 glyph->u.img_id = img->id;
24490 glyph->slice.img = slice;
24491 glyph->font_type = FONT_TYPE_UNKNOWN;
24492 if (it->bidi_p)
24493 {
24494 glyph->resolved_level = it->bidi_it.resolved_level;
24495 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24496 emacs_abort ();
24497 glyph->bidi_type = it->bidi_it.type;
24498 }
24499 ++it->glyph_row->used[area];
24500 }
24501 else
24502 IT_EXPAND_MATRIX_WIDTH (it, area);
24503 }
24504 }
24505
24506
24507 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24508 of the glyph, WIDTH and HEIGHT are the width and height of the
24509 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24510
24511 static void
24512 append_stretch_glyph (struct it *it, Lisp_Object object,
24513 int width, int height, int ascent)
24514 {
24515 struct glyph *glyph;
24516 enum glyph_row_area area = it->area;
24517
24518 eassert (ascent >= 0 && ascent <= height);
24519
24520 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24521 if (glyph < it->glyph_row->glyphs[area + 1])
24522 {
24523 /* If the glyph row is reversed, we need to prepend the glyph
24524 rather than append it. */
24525 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24526 {
24527 struct glyph *g;
24528
24529 /* Make room for the additional glyph. */
24530 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24531 g[1] = *g;
24532 glyph = it->glyph_row->glyphs[area];
24533 }
24534 glyph->charpos = CHARPOS (it->position);
24535 glyph->object = object;
24536 glyph->pixel_width = width;
24537 glyph->ascent = ascent;
24538 glyph->descent = height - ascent;
24539 glyph->voffset = it->voffset;
24540 glyph->type = STRETCH_GLYPH;
24541 glyph->avoid_cursor_p = it->avoid_cursor_p;
24542 glyph->multibyte_p = it->multibyte_p;
24543 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24544 {
24545 /* In R2L rows, the left and the right box edges need to be
24546 drawn in reverse direction. */
24547 glyph->right_box_line_p = it->start_of_box_run_p;
24548 glyph->left_box_line_p = it->end_of_box_run_p;
24549 }
24550 else
24551 {
24552 glyph->left_box_line_p = it->start_of_box_run_p;
24553 glyph->right_box_line_p = it->end_of_box_run_p;
24554 }
24555 glyph->overlaps_vertically_p = 0;
24556 glyph->padding_p = 0;
24557 glyph->glyph_not_available_p = 0;
24558 glyph->face_id = it->face_id;
24559 glyph->u.stretch.ascent = ascent;
24560 glyph->u.stretch.height = height;
24561 glyph->slice.img = null_glyph_slice;
24562 glyph->font_type = FONT_TYPE_UNKNOWN;
24563 if (it->bidi_p)
24564 {
24565 glyph->resolved_level = it->bidi_it.resolved_level;
24566 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24567 emacs_abort ();
24568 glyph->bidi_type = it->bidi_it.type;
24569 }
24570 else
24571 {
24572 glyph->resolved_level = 0;
24573 glyph->bidi_type = UNKNOWN_BT;
24574 }
24575 ++it->glyph_row->used[area];
24576 }
24577 else
24578 IT_EXPAND_MATRIX_WIDTH (it, area);
24579 }
24580
24581 #endif /* HAVE_WINDOW_SYSTEM */
24582
24583 /* Produce a stretch glyph for iterator IT. IT->object is the value
24584 of the glyph property displayed. The value must be a list
24585 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24586 being recognized:
24587
24588 1. `:width WIDTH' specifies that the space should be WIDTH *
24589 canonical char width wide. WIDTH may be an integer or floating
24590 point number.
24591
24592 2. `:relative-width FACTOR' specifies that the width of the stretch
24593 should be computed from the width of the first character having the
24594 `glyph' property, and should be FACTOR times that width.
24595
24596 3. `:align-to HPOS' specifies that the space should be wide enough
24597 to reach HPOS, a value in canonical character units.
24598
24599 Exactly one of the above pairs must be present.
24600
24601 4. `:height HEIGHT' specifies that the height of the stretch produced
24602 should be HEIGHT, measured in canonical character units.
24603
24604 5. `:relative-height FACTOR' specifies that the height of the
24605 stretch should be FACTOR times the height of the characters having
24606 the glyph property.
24607
24608 Either none or exactly one of 4 or 5 must be present.
24609
24610 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24611 of the stretch should be used for the ascent of the stretch.
24612 ASCENT must be in the range 0 <= ASCENT <= 100. */
24613
24614 void
24615 produce_stretch_glyph (struct it *it)
24616 {
24617 /* (space :width WIDTH :height HEIGHT ...) */
24618 Lisp_Object prop, plist;
24619 int width = 0, height = 0, align_to = -1;
24620 int zero_width_ok_p = 0;
24621 double tem;
24622 struct font *font = NULL;
24623
24624 #ifdef HAVE_WINDOW_SYSTEM
24625 int ascent = 0;
24626 int zero_height_ok_p = 0;
24627
24628 if (FRAME_WINDOW_P (it->f))
24629 {
24630 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24631 font = face->font ? face->font : FRAME_FONT (it->f);
24632 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24633 }
24634 #endif
24635
24636 /* List should start with `space'. */
24637 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24638 plist = XCDR (it->object);
24639
24640 /* Compute the width of the stretch. */
24641 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24642 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24643 {
24644 /* Absolute width `:width WIDTH' specified and valid. */
24645 zero_width_ok_p = 1;
24646 width = (int)tem;
24647 }
24648 #ifdef HAVE_WINDOW_SYSTEM
24649 else if (FRAME_WINDOW_P (it->f)
24650 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24651 {
24652 /* Relative width `:relative-width FACTOR' specified and valid.
24653 Compute the width of the characters having the `glyph'
24654 property. */
24655 struct it it2;
24656 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24657
24658 it2 = *it;
24659 if (it->multibyte_p)
24660 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24661 else
24662 {
24663 it2.c = it2.char_to_display = *p, it2.len = 1;
24664 if (! ASCII_CHAR_P (it2.c))
24665 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24666 }
24667
24668 it2.glyph_row = NULL;
24669 it2.what = IT_CHARACTER;
24670 x_produce_glyphs (&it2);
24671 width = NUMVAL (prop) * it2.pixel_width;
24672 }
24673 #endif /* HAVE_WINDOW_SYSTEM */
24674 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24675 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24676 {
24677 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24678 align_to = (align_to < 0
24679 ? 0
24680 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24681 else if (align_to < 0)
24682 align_to = window_box_left_offset (it->w, TEXT_AREA);
24683 width = max (0, (int)tem + align_to - it->current_x);
24684 zero_width_ok_p = 1;
24685 }
24686 else
24687 /* Nothing specified -> width defaults to canonical char width. */
24688 width = FRAME_COLUMN_WIDTH (it->f);
24689
24690 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24691 width = 1;
24692
24693 #ifdef HAVE_WINDOW_SYSTEM
24694 /* Compute height. */
24695 if (FRAME_WINDOW_P (it->f))
24696 {
24697 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24698 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24699 {
24700 height = (int)tem;
24701 zero_height_ok_p = 1;
24702 }
24703 else if (prop = Fplist_get (plist, QCrelative_height),
24704 NUMVAL (prop) > 0)
24705 height = FONT_HEIGHT (font) * NUMVAL (prop);
24706 else
24707 height = FONT_HEIGHT (font);
24708
24709 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24710 height = 1;
24711
24712 /* Compute percentage of height used for ascent. If
24713 `:ascent ASCENT' is present and valid, use that. Otherwise,
24714 derive the ascent from the font in use. */
24715 if (prop = Fplist_get (plist, QCascent),
24716 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24717 ascent = height * NUMVAL (prop) / 100.0;
24718 else if (!NILP (prop)
24719 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24720 ascent = min (max (0, (int)tem), height);
24721 else
24722 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24723 }
24724 else
24725 #endif /* HAVE_WINDOW_SYSTEM */
24726 height = 1;
24727
24728 if (width > 0 && it->line_wrap != TRUNCATE
24729 && it->current_x + width > it->last_visible_x)
24730 {
24731 width = it->last_visible_x - it->current_x;
24732 #ifdef HAVE_WINDOW_SYSTEM
24733 /* Subtract one more pixel from the stretch width, but only on
24734 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24735 width -= FRAME_WINDOW_P (it->f);
24736 #endif
24737 }
24738
24739 if (width > 0 && height > 0 && it->glyph_row)
24740 {
24741 Lisp_Object o_object = it->object;
24742 Lisp_Object object = it->stack[it->sp - 1].string;
24743 int n = width;
24744
24745 if (!STRINGP (object))
24746 object = it->w->contents;
24747 #ifdef HAVE_WINDOW_SYSTEM
24748 if (FRAME_WINDOW_P (it->f))
24749 append_stretch_glyph (it, object, width, height, ascent);
24750 else
24751 #endif
24752 {
24753 it->object = object;
24754 it->char_to_display = ' ';
24755 it->pixel_width = it->len = 1;
24756 while (n--)
24757 tty_append_glyph (it);
24758 it->object = o_object;
24759 }
24760 }
24761
24762 it->pixel_width = width;
24763 #ifdef HAVE_WINDOW_SYSTEM
24764 if (FRAME_WINDOW_P (it->f))
24765 {
24766 it->ascent = it->phys_ascent = ascent;
24767 it->descent = it->phys_descent = height - it->ascent;
24768 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24769 take_vertical_position_into_account (it);
24770 }
24771 else
24772 #endif
24773 it->nglyphs = width;
24774 }
24775
24776 /* Get information about special display element WHAT in an
24777 environment described by IT. WHAT is one of IT_TRUNCATION or
24778 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24779 non-null glyph_row member. This function ensures that fields like
24780 face_id, c, len of IT are left untouched. */
24781
24782 static void
24783 produce_special_glyphs (struct it *it, enum display_element_type what)
24784 {
24785 struct it temp_it;
24786 Lisp_Object gc;
24787 GLYPH glyph;
24788
24789 temp_it = *it;
24790 temp_it.object = make_number (0);
24791 memset (&temp_it.current, 0, sizeof temp_it.current);
24792
24793 if (what == IT_CONTINUATION)
24794 {
24795 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24796 if (it->bidi_it.paragraph_dir == R2L)
24797 SET_GLYPH_FROM_CHAR (glyph, '/');
24798 else
24799 SET_GLYPH_FROM_CHAR (glyph, '\\');
24800 if (it->dp
24801 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24802 {
24803 /* FIXME: Should we mirror GC for R2L lines? */
24804 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24805 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24806 }
24807 }
24808 else if (what == IT_TRUNCATION)
24809 {
24810 /* Truncation glyph. */
24811 SET_GLYPH_FROM_CHAR (glyph, '$');
24812 if (it->dp
24813 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24814 {
24815 /* FIXME: Should we mirror GC for R2L lines? */
24816 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24817 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24818 }
24819 }
24820 else
24821 emacs_abort ();
24822
24823 #ifdef HAVE_WINDOW_SYSTEM
24824 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24825 is turned off, we precede the truncation/continuation glyphs by a
24826 stretch glyph whose width is computed such that these special
24827 glyphs are aligned at the window margin, even when very different
24828 fonts are used in different glyph rows. */
24829 if (FRAME_WINDOW_P (temp_it.f)
24830 /* init_iterator calls this with it->glyph_row == NULL, and it
24831 wants only the pixel width of the truncation/continuation
24832 glyphs. */
24833 && temp_it.glyph_row
24834 /* insert_left_trunc_glyphs calls us at the beginning of the
24835 row, and it has its own calculation of the stretch glyph
24836 width. */
24837 && temp_it.glyph_row->used[TEXT_AREA] > 0
24838 && (temp_it.glyph_row->reversed_p
24839 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24840 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24841 {
24842 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24843
24844 if (stretch_width > 0)
24845 {
24846 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24847 struct font *font =
24848 face->font ? face->font : FRAME_FONT (temp_it.f);
24849 int stretch_ascent =
24850 (((temp_it.ascent + temp_it.descent)
24851 * FONT_BASE (font)) / FONT_HEIGHT (font));
24852
24853 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24854 temp_it.ascent + temp_it.descent,
24855 stretch_ascent);
24856 }
24857 }
24858 #endif
24859
24860 temp_it.dp = NULL;
24861 temp_it.what = IT_CHARACTER;
24862 temp_it.len = 1;
24863 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24864 temp_it.face_id = GLYPH_FACE (glyph);
24865 temp_it.len = CHAR_BYTES (temp_it.c);
24866
24867 PRODUCE_GLYPHS (&temp_it);
24868 it->pixel_width = temp_it.pixel_width;
24869 it->nglyphs = temp_it.pixel_width;
24870 }
24871
24872 #ifdef HAVE_WINDOW_SYSTEM
24873
24874 /* Calculate line-height and line-spacing properties.
24875 An integer value specifies explicit pixel value.
24876 A float value specifies relative value to current face height.
24877 A cons (float . face-name) specifies relative value to
24878 height of specified face font.
24879
24880 Returns height in pixels, or nil. */
24881
24882
24883 static Lisp_Object
24884 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24885 int boff, int override)
24886 {
24887 Lisp_Object face_name = Qnil;
24888 int ascent, descent, height;
24889
24890 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24891 return val;
24892
24893 if (CONSP (val))
24894 {
24895 face_name = XCAR (val);
24896 val = XCDR (val);
24897 if (!NUMBERP (val))
24898 val = make_number (1);
24899 if (NILP (face_name))
24900 {
24901 height = it->ascent + it->descent;
24902 goto scale;
24903 }
24904 }
24905
24906 if (NILP (face_name))
24907 {
24908 font = FRAME_FONT (it->f);
24909 boff = FRAME_BASELINE_OFFSET (it->f);
24910 }
24911 else if (EQ (face_name, Qt))
24912 {
24913 override = 0;
24914 }
24915 else
24916 {
24917 int face_id;
24918 struct face *face;
24919
24920 face_id = lookup_named_face (it->f, face_name, 0);
24921 if (face_id < 0)
24922 return make_number (-1);
24923
24924 face = FACE_FROM_ID (it->f, face_id);
24925 font = face->font;
24926 if (font == NULL)
24927 return make_number (-1);
24928 boff = font->baseline_offset;
24929 if (font->vertical_centering)
24930 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24931 }
24932
24933 ascent = FONT_BASE (font) + boff;
24934 descent = FONT_DESCENT (font) - boff;
24935
24936 if (override)
24937 {
24938 it->override_ascent = ascent;
24939 it->override_descent = descent;
24940 it->override_boff = boff;
24941 }
24942
24943 height = ascent + descent;
24944
24945 scale:
24946 if (FLOATP (val))
24947 height = (int)(XFLOAT_DATA (val) * height);
24948 else if (INTEGERP (val))
24949 height *= XINT (val);
24950
24951 return make_number (height);
24952 }
24953
24954
24955 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24956 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24957 and only if this is for a character for which no font was found.
24958
24959 If the display method (it->glyphless_method) is
24960 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24961 length of the acronym or the hexadecimal string, UPPER_XOFF and
24962 UPPER_YOFF are pixel offsets for the upper part of the string,
24963 LOWER_XOFF and LOWER_YOFF are for the lower part.
24964
24965 For the other display methods, LEN through LOWER_YOFF are zero. */
24966
24967 static void
24968 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24969 short upper_xoff, short upper_yoff,
24970 short lower_xoff, short lower_yoff)
24971 {
24972 struct glyph *glyph;
24973 enum glyph_row_area area = it->area;
24974
24975 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24976 if (glyph < it->glyph_row->glyphs[area + 1])
24977 {
24978 /* If the glyph row is reversed, we need to prepend the glyph
24979 rather than append it. */
24980 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24981 {
24982 struct glyph *g;
24983
24984 /* Make room for the additional glyph. */
24985 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24986 g[1] = *g;
24987 glyph = it->glyph_row->glyphs[area];
24988 }
24989 glyph->charpos = CHARPOS (it->position);
24990 glyph->object = it->object;
24991 glyph->pixel_width = it->pixel_width;
24992 glyph->ascent = it->ascent;
24993 glyph->descent = it->descent;
24994 glyph->voffset = it->voffset;
24995 glyph->type = GLYPHLESS_GLYPH;
24996 glyph->u.glyphless.method = it->glyphless_method;
24997 glyph->u.glyphless.for_no_font = for_no_font;
24998 glyph->u.glyphless.len = len;
24999 glyph->u.glyphless.ch = it->c;
25000 glyph->slice.glyphless.upper_xoff = upper_xoff;
25001 glyph->slice.glyphless.upper_yoff = upper_yoff;
25002 glyph->slice.glyphless.lower_xoff = lower_xoff;
25003 glyph->slice.glyphless.lower_yoff = lower_yoff;
25004 glyph->avoid_cursor_p = it->avoid_cursor_p;
25005 glyph->multibyte_p = it->multibyte_p;
25006 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25007 {
25008 /* In R2L rows, the left and the right box edges need to be
25009 drawn in reverse direction. */
25010 glyph->right_box_line_p = it->start_of_box_run_p;
25011 glyph->left_box_line_p = it->end_of_box_run_p;
25012 }
25013 else
25014 {
25015 glyph->left_box_line_p = it->start_of_box_run_p;
25016 glyph->right_box_line_p = it->end_of_box_run_p;
25017 }
25018 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25019 || it->phys_descent > it->descent);
25020 glyph->padding_p = 0;
25021 glyph->glyph_not_available_p = 0;
25022 glyph->face_id = face_id;
25023 glyph->font_type = FONT_TYPE_UNKNOWN;
25024 if (it->bidi_p)
25025 {
25026 glyph->resolved_level = it->bidi_it.resolved_level;
25027 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25028 emacs_abort ();
25029 glyph->bidi_type = it->bidi_it.type;
25030 }
25031 ++it->glyph_row->used[area];
25032 }
25033 else
25034 IT_EXPAND_MATRIX_WIDTH (it, area);
25035 }
25036
25037
25038 /* Produce a glyph for a glyphless character for iterator IT.
25039 IT->glyphless_method specifies which method to use for displaying
25040 the character. See the description of enum
25041 glyphless_display_method in dispextern.h for the detail.
25042
25043 FOR_NO_FONT is nonzero if and only if this is for a character for
25044 which no font was found. ACRONYM, if non-nil, is an acronym string
25045 for the character. */
25046
25047 static void
25048 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25049 {
25050 int face_id;
25051 struct face *face;
25052 struct font *font;
25053 int base_width, base_height, width, height;
25054 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25055 int len;
25056
25057 /* Get the metrics of the base font. We always refer to the current
25058 ASCII face. */
25059 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25060 font = face->font ? face->font : FRAME_FONT (it->f);
25061 it->ascent = FONT_BASE (font) + font->baseline_offset;
25062 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25063 base_height = it->ascent + it->descent;
25064 base_width = font->average_width;
25065
25066 face_id = merge_glyphless_glyph_face (it);
25067
25068 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25069 {
25070 it->pixel_width = THIN_SPACE_WIDTH;
25071 len = 0;
25072 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25073 }
25074 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25075 {
25076 width = CHAR_WIDTH (it->c);
25077 if (width == 0)
25078 width = 1;
25079 else if (width > 4)
25080 width = 4;
25081 it->pixel_width = base_width * width;
25082 len = 0;
25083 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25084 }
25085 else
25086 {
25087 char buf[7];
25088 const char *str;
25089 unsigned int code[6];
25090 int upper_len;
25091 int ascent, descent;
25092 struct font_metrics metrics_upper, metrics_lower;
25093
25094 face = FACE_FROM_ID (it->f, face_id);
25095 font = face->font ? face->font : FRAME_FONT (it->f);
25096 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25097
25098 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25099 {
25100 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25101 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25102 if (CONSP (acronym))
25103 acronym = XCAR (acronym);
25104 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25105 }
25106 else
25107 {
25108 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25109 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25110 str = buf;
25111 }
25112 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25113 code[len] = font->driver->encode_char (font, str[len]);
25114 upper_len = (len + 1) / 2;
25115 font->driver->text_extents (font, code, upper_len,
25116 &metrics_upper);
25117 font->driver->text_extents (font, code + upper_len, len - upper_len,
25118 &metrics_lower);
25119
25120
25121
25122 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25123 width = max (metrics_upper.width, metrics_lower.width) + 4;
25124 upper_xoff = upper_yoff = 2; /* the typical case */
25125 if (base_width >= width)
25126 {
25127 /* Align the upper to the left, the lower to the right. */
25128 it->pixel_width = base_width;
25129 lower_xoff = base_width - 2 - metrics_lower.width;
25130 }
25131 else
25132 {
25133 /* Center the shorter one. */
25134 it->pixel_width = width;
25135 if (metrics_upper.width >= metrics_lower.width)
25136 lower_xoff = (width - metrics_lower.width) / 2;
25137 else
25138 {
25139 /* FIXME: This code doesn't look right. It formerly was
25140 missing the "lower_xoff = 0;", which couldn't have
25141 been right since it left lower_xoff uninitialized. */
25142 lower_xoff = 0;
25143 upper_xoff = (width - metrics_upper.width) / 2;
25144 }
25145 }
25146
25147 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25148 top, bottom, and between upper and lower strings. */
25149 height = (metrics_upper.ascent + metrics_upper.descent
25150 + metrics_lower.ascent + metrics_lower.descent) + 5;
25151 /* Center vertically.
25152 H:base_height, D:base_descent
25153 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25154
25155 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25156 descent = D - H/2 + h/2;
25157 lower_yoff = descent - 2 - ld;
25158 upper_yoff = lower_yoff - la - 1 - ud; */
25159 ascent = - (it->descent - (base_height + height + 1) / 2);
25160 descent = it->descent - (base_height - height) / 2;
25161 lower_yoff = descent - 2 - metrics_lower.descent;
25162 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25163 - metrics_upper.descent);
25164 /* Don't make the height shorter than the base height. */
25165 if (height > base_height)
25166 {
25167 it->ascent = ascent;
25168 it->descent = descent;
25169 }
25170 }
25171
25172 it->phys_ascent = it->ascent;
25173 it->phys_descent = it->descent;
25174 if (it->glyph_row)
25175 append_glyphless_glyph (it, face_id, for_no_font, len,
25176 upper_xoff, upper_yoff,
25177 lower_xoff, lower_yoff);
25178 it->nglyphs = 1;
25179 take_vertical_position_into_account (it);
25180 }
25181
25182
25183 /* RIF:
25184 Produce glyphs/get display metrics for the display element IT is
25185 loaded with. See the description of struct it in dispextern.h
25186 for an overview of struct it. */
25187
25188 void
25189 x_produce_glyphs (struct it *it)
25190 {
25191 int extra_line_spacing = it->extra_line_spacing;
25192
25193 it->glyph_not_available_p = 0;
25194
25195 if (it->what == IT_CHARACTER)
25196 {
25197 XChar2b char2b;
25198 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25199 struct font *font = face->font;
25200 struct font_metrics *pcm = NULL;
25201 int boff; /* Baseline offset. */
25202
25203 if (font == NULL)
25204 {
25205 /* When no suitable font is found, display this character by
25206 the method specified in the first extra slot of
25207 Vglyphless_char_display. */
25208 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25209
25210 eassert (it->what == IT_GLYPHLESS);
25211 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25212 goto done;
25213 }
25214
25215 boff = font->baseline_offset;
25216 if (font->vertical_centering)
25217 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25218
25219 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25220 {
25221 int stretched_p;
25222
25223 it->nglyphs = 1;
25224
25225 if (it->override_ascent >= 0)
25226 {
25227 it->ascent = it->override_ascent;
25228 it->descent = it->override_descent;
25229 boff = it->override_boff;
25230 }
25231 else
25232 {
25233 it->ascent = FONT_BASE (font) + boff;
25234 it->descent = FONT_DESCENT (font) - boff;
25235 }
25236
25237 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25238 {
25239 pcm = get_per_char_metric (font, &char2b);
25240 if (pcm->width == 0
25241 && pcm->rbearing == 0 && pcm->lbearing == 0)
25242 pcm = NULL;
25243 }
25244
25245 if (pcm)
25246 {
25247 it->phys_ascent = pcm->ascent + boff;
25248 it->phys_descent = pcm->descent - boff;
25249 it->pixel_width = pcm->width;
25250 }
25251 else
25252 {
25253 it->glyph_not_available_p = 1;
25254 it->phys_ascent = it->ascent;
25255 it->phys_descent = it->descent;
25256 it->pixel_width = font->space_width;
25257 }
25258
25259 if (it->constrain_row_ascent_descent_p)
25260 {
25261 if (it->descent > it->max_descent)
25262 {
25263 it->ascent += it->descent - it->max_descent;
25264 it->descent = it->max_descent;
25265 }
25266 if (it->ascent > it->max_ascent)
25267 {
25268 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25269 it->ascent = it->max_ascent;
25270 }
25271 it->phys_ascent = min (it->phys_ascent, it->ascent);
25272 it->phys_descent = min (it->phys_descent, it->descent);
25273 extra_line_spacing = 0;
25274 }
25275
25276 /* If this is a space inside a region of text with
25277 `space-width' property, change its width. */
25278 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25279 if (stretched_p)
25280 it->pixel_width *= XFLOATINT (it->space_width);
25281
25282 /* If face has a box, add the box thickness to the character
25283 height. If character has a box line to the left and/or
25284 right, add the box line width to the character's width. */
25285 if (face->box != FACE_NO_BOX)
25286 {
25287 int thick = face->box_line_width;
25288
25289 if (thick > 0)
25290 {
25291 it->ascent += thick;
25292 it->descent += thick;
25293 }
25294 else
25295 thick = -thick;
25296
25297 if (it->start_of_box_run_p)
25298 it->pixel_width += thick;
25299 if (it->end_of_box_run_p)
25300 it->pixel_width += thick;
25301 }
25302
25303 /* If face has an overline, add the height of the overline
25304 (1 pixel) and a 1 pixel margin to the character height. */
25305 if (face->overline_p)
25306 it->ascent += overline_margin;
25307
25308 if (it->constrain_row_ascent_descent_p)
25309 {
25310 if (it->ascent > it->max_ascent)
25311 it->ascent = it->max_ascent;
25312 if (it->descent > it->max_descent)
25313 it->descent = it->max_descent;
25314 }
25315
25316 take_vertical_position_into_account (it);
25317
25318 /* If we have to actually produce glyphs, do it. */
25319 if (it->glyph_row)
25320 {
25321 if (stretched_p)
25322 {
25323 /* Translate a space with a `space-width' property
25324 into a stretch glyph. */
25325 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25326 / FONT_HEIGHT (font));
25327 append_stretch_glyph (it, it->object, it->pixel_width,
25328 it->ascent + it->descent, ascent);
25329 }
25330 else
25331 append_glyph (it);
25332
25333 /* If characters with lbearing or rbearing are displayed
25334 in this line, record that fact in a flag of the
25335 glyph row. This is used to optimize X output code. */
25336 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25337 it->glyph_row->contains_overlapping_glyphs_p = 1;
25338 }
25339 if (! stretched_p && it->pixel_width == 0)
25340 /* We assure that all visible glyphs have at least 1-pixel
25341 width. */
25342 it->pixel_width = 1;
25343 }
25344 else if (it->char_to_display == '\n')
25345 {
25346 /* A newline has no width, but we need the height of the
25347 line. But if previous part of the line sets a height,
25348 don't increase that height. */
25349
25350 Lisp_Object height;
25351 Lisp_Object total_height = Qnil;
25352
25353 it->override_ascent = -1;
25354 it->pixel_width = 0;
25355 it->nglyphs = 0;
25356
25357 height = get_it_property (it, Qline_height);
25358 /* Split (line-height total-height) list. */
25359 if (CONSP (height)
25360 && CONSP (XCDR (height))
25361 && NILP (XCDR (XCDR (height))))
25362 {
25363 total_height = XCAR (XCDR (height));
25364 height = XCAR (height);
25365 }
25366 height = calc_line_height_property (it, height, font, boff, 1);
25367
25368 if (it->override_ascent >= 0)
25369 {
25370 it->ascent = it->override_ascent;
25371 it->descent = it->override_descent;
25372 boff = it->override_boff;
25373 }
25374 else
25375 {
25376 it->ascent = FONT_BASE (font) + boff;
25377 it->descent = FONT_DESCENT (font) - boff;
25378 }
25379
25380 if (EQ (height, Qt))
25381 {
25382 if (it->descent > it->max_descent)
25383 {
25384 it->ascent += it->descent - it->max_descent;
25385 it->descent = it->max_descent;
25386 }
25387 if (it->ascent > it->max_ascent)
25388 {
25389 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25390 it->ascent = it->max_ascent;
25391 }
25392 it->phys_ascent = min (it->phys_ascent, it->ascent);
25393 it->phys_descent = min (it->phys_descent, it->descent);
25394 it->constrain_row_ascent_descent_p = 1;
25395 extra_line_spacing = 0;
25396 }
25397 else
25398 {
25399 Lisp_Object spacing;
25400
25401 it->phys_ascent = it->ascent;
25402 it->phys_descent = it->descent;
25403
25404 if ((it->max_ascent > 0 || it->max_descent > 0)
25405 && face->box != FACE_NO_BOX
25406 && face->box_line_width > 0)
25407 {
25408 it->ascent += face->box_line_width;
25409 it->descent += face->box_line_width;
25410 }
25411 if (!NILP (height)
25412 && XINT (height) > it->ascent + it->descent)
25413 it->ascent = XINT (height) - it->descent;
25414
25415 if (!NILP (total_height))
25416 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25417 else
25418 {
25419 spacing = get_it_property (it, Qline_spacing);
25420 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25421 }
25422 if (INTEGERP (spacing))
25423 {
25424 extra_line_spacing = XINT (spacing);
25425 if (!NILP (total_height))
25426 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25427 }
25428 }
25429 }
25430 else /* i.e. (it->char_to_display == '\t') */
25431 {
25432 if (font->space_width > 0)
25433 {
25434 int tab_width = it->tab_width * font->space_width;
25435 int x = it->current_x + it->continuation_lines_width;
25436 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25437
25438 /* If the distance from the current position to the next tab
25439 stop is less than a space character width, use the
25440 tab stop after that. */
25441 if (next_tab_x - x < font->space_width)
25442 next_tab_x += tab_width;
25443
25444 it->pixel_width = next_tab_x - x;
25445 it->nglyphs = 1;
25446 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25447 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25448
25449 if (it->glyph_row)
25450 {
25451 append_stretch_glyph (it, it->object, it->pixel_width,
25452 it->ascent + it->descent, it->ascent);
25453 }
25454 }
25455 else
25456 {
25457 it->pixel_width = 0;
25458 it->nglyphs = 1;
25459 }
25460 }
25461 }
25462 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25463 {
25464 /* A static composition.
25465
25466 Note: A composition is represented as one glyph in the
25467 glyph matrix. There are no padding glyphs.
25468
25469 Important note: pixel_width, ascent, and descent are the
25470 values of what is drawn by draw_glyphs (i.e. the values of
25471 the overall glyphs composed). */
25472 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25473 int boff; /* baseline offset */
25474 struct composition *cmp = composition_table[it->cmp_it.id];
25475 int glyph_len = cmp->glyph_len;
25476 struct font *font = face->font;
25477
25478 it->nglyphs = 1;
25479
25480 /* If we have not yet calculated pixel size data of glyphs of
25481 the composition for the current face font, calculate them
25482 now. Theoretically, we have to check all fonts for the
25483 glyphs, but that requires much time and memory space. So,
25484 here we check only the font of the first glyph. This may
25485 lead to incorrect display, but it's very rare, and C-l
25486 (recenter-top-bottom) can correct the display anyway. */
25487 if (! cmp->font || cmp->font != font)
25488 {
25489 /* Ascent and descent of the font of the first character
25490 of this composition (adjusted by baseline offset).
25491 Ascent and descent of overall glyphs should not be less
25492 than these, respectively. */
25493 int font_ascent, font_descent, font_height;
25494 /* Bounding box of the overall glyphs. */
25495 int leftmost, rightmost, lowest, highest;
25496 int lbearing, rbearing;
25497 int i, width, ascent, descent;
25498 int left_padded = 0, right_padded = 0;
25499 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25500 XChar2b char2b;
25501 struct font_metrics *pcm;
25502 int font_not_found_p;
25503 ptrdiff_t pos;
25504
25505 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25506 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25507 break;
25508 if (glyph_len < cmp->glyph_len)
25509 right_padded = 1;
25510 for (i = 0; i < glyph_len; i++)
25511 {
25512 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25513 break;
25514 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25515 }
25516 if (i > 0)
25517 left_padded = 1;
25518
25519 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25520 : IT_CHARPOS (*it));
25521 /* If no suitable font is found, use the default font. */
25522 font_not_found_p = font == NULL;
25523 if (font_not_found_p)
25524 {
25525 face = face->ascii_face;
25526 font = face->font;
25527 }
25528 boff = font->baseline_offset;
25529 if (font->vertical_centering)
25530 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25531 font_ascent = FONT_BASE (font) + boff;
25532 font_descent = FONT_DESCENT (font) - boff;
25533 font_height = FONT_HEIGHT (font);
25534
25535 cmp->font = font;
25536
25537 pcm = NULL;
25538 if (! font_not_found_p)
25539 {
25540 get_char_face_and_encoding (it->f, c, it->face_id,
25541 &char2b, 0);
25542 pcm = get_per_char_metric (font, &char2b);
25543 }
25544
25545 /* Initialize the bounding box. */
25546 if (pcm)
25547 {
25548 width = cmp->glyph_len > 0 ? pcm->width : 0;
25549 ascent = pcm->ascent;
25550 descent = pcm->descent;
25551 lbearing = pcm->lbearing;
25552 rbearing = pcm->rbearing;
25553 }
25554 else
25555 {
25556 width = cmp->glyph_len > 0 ? font->space_width : 0;
25557 ascent = FONT_BASE (font);
25558 descent = FONT_DESCENT (font);
25559 lbearing = 0;
25560 rbearing = width;
25561 }
25562
25563 rightmost = width;
25564 leftmost = 0;
25565 lowest = - descent + boff;
25566 highest = ascent + boff;
25567
25568 if (! font_not_found_p
25569 && font->default_ascent
25570 && CHAR_TABLE_P (Vuse_default_ascent)
25571 && !NILP (Faref (Vuse_default_ascent,
25572 make_number (it->char_to_display))))
25573 highest = font->default_ascent + boff;
25574
25575 /* Draw the first glyph at the normal position. It may be
25576 shifted to right later if some other glyphs are drawn
25577 at the left. */
25578 cmp->offsets[i * 2] = 0;
25579 cmp->offsets[i * 2 + 1] = boff;
25580 cmp->lbearing = lbearing;
25581 cmp->rbearing = rbearing;
25582
25583 /* Set cmp->offsets for the remaining glyphs. */
25584 for (i++; i < glyph_len; i++)
25585 {
25586 int left, right, btm, top;
25587 int ch = COMPOSITION_GLYPH (cmp, i);
25588 int face_id;
25589 struct face *this_face;
25590
25591 if (ch == '\t')
25592 ch = ' ';
25593 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25594 this_face = FACE_FROM_ID (it->f, face_id);
25595 font = this_face->font;
25596
25597 if (font == NULL)
25598 pcm = NULL;
25599 else
25600 {
25601 get_char_face_and_encoding (it->f, ch, face_id,
25602 &char2b, 0);
25603 pcm = get_per_char_metric (font, &char2b);
25604 }
25605 if (! pcm)
25606 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25607 else
25608 {
25609 width = pcm->width;
25610 ascent = pcm->ascent;
25611 descent = pcm->descent;
25612 lbearing = pcm->lbearing;
25613 rbearing = pcm->rbearing;
25614 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25615 {
25616 /* Relative composition with or without
25617 alternate chars. */
25618 left = (leftmost + rightmost - width) / 2;
25619 btm = - descent + boff;
25620 if (font->relative_compose
25621 && (! CHAR_TABLE_P (Vignore_relative_composition)
25622 || NILP (Faref (Vignore_relative_composition,
25623 make_number (ch)))))
25624 {
25625
25626 if (- descent >= font->relative_compose)
25627 /* One extra pixel between two glyphs. */
25628 btm = highest + 1;
25629 else if (ascent <= 0)
25630 /* One extra pixel between two glyphs. */
25631 btm = lowest - 1 - ascent - descent;
25632 }
25633 }
25634 else
25635 {
25636 /* A composition rule is specified by an integer
25637 value that encodes global and new reference
25638 points (GREF and NREF). GREF and NREF are
25639 specified by numbers as below:
25640
25641 0---1---2 -- ascent
25642 | |
25643 | |
25644 | |
25645 9--10--11 -- center
25646 | |
25647 ---3---4---5--- baseline
25648 | |
25649 6---7---8 -- descent
25650 */
25651 int rule = COMPOSITION_RULE (cmp, i);
25652 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25653
25654 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25655 grefx = gref % 3, nrefx = nref % 3;
25656 grefy = gref / 3, nrefy = nref / 3;
25657 if (xoff)
25658 xoff = font_height * (xoff - 128) / 256;
25659 if (yoff)
25660 yoff = font_height * (yoff - 128) / 256;
25661
25662 left = (leftmost
25663 + grefx * (rightmost - leftmost) / 2
25664 - nrefx * width / 2
25665 + xoff);
25666
25667 btm = ((grefy == 0 ? highest
25668 : grefy == 1 ? 0
25669 : grefy == 2 ? lowest
25670 : (highest + lowest) / 2)
25671 - (nrefy == 0 ? ascent + descent
25672 : nrefy == 1 ? descent - boff
25673 : nrefy == 2 ? 0
25674 : (ascent + descent) / 2)
25675 + yoff);
25676 }
25677
25678 cmp->offsets[i * 2] = left;
25679 cmp->offsets[i * 2 + 1] = btm + descent;
25680
25681 /* Update the bounding box of the overall glyphs. */
25682 if (width > 0)
25683 {
25684 right = left + width;
25685 if (left < leftmost)
25686 leftmost = left;
25687 if (right > rightmost)
25688 rightmost = right;
25689 }
25690 top = btm + descent + ascent;
25691 if (top > highest)
25692 highest = top;
25693 if (btm < lowest)
25694 lowest = btm;
25695
25696 if (cmp->lbearing > left + lbearing)
25697 cmp->lbearing = left + lbearing;
25698 if (cmp->rbearing < left + rbearing)
25699 cmp->rbearing = left + rbearing;
25700 }
25701 }
25702
25703 /* If there are glyphs whose x-offsets are negative,
25704 shift all glyphs to the right and make all x-offsets
25705 non-negative. */
25706 if (leftmost < 0)
25707 {
25708 for (i = 0; i < cmp->glyph_len; i++)
25709 cmp->offsets[i * 2] -= leftmost;
25710 rightmost -= leftmost;
25711 cmp->lbearing -= leftmost;
25712 cmp->rbearing -= leftmost;
25713 }
25714
25715 if (left_padded && cmp->lbearing < 0)
25716 {
25717 for (i = 0; i < cmp->glyph_len; i++)
25718 cmp->offsets[i * 2] -= cmp->lbearing;
25719 rightmost -= cmp->lbearing;
25720 cmp->rbearing -= cmp->lbearing;
25721 cmp->lbearing = 0;
25722 }
25723 if (right_padded && rightmost < cmp->rbearing)
25724 {
25725 rightmost = cmp->rbearing;
25726 }
25727
25728 cmp->pixel_width = rightmost;
25729 cmp->ascent = highest;
25730 cmp->descent = - lowest;
25731 if (cmp->ascent < font_ascent)
25732 cmp->ascent = font_ascent;
25733 if (cmp->descent < font_descent)
25734 cmp->descent = font_descent;
25735 }
25736
25737 if (it->glyph_row
25738 && (cmp->lbearing < 0
25739 || cmp->rbearing > cmp->pixel_width))
25740 it->glyph_row->contains_overlapping_glyphs_p = 1;
25741
25742 it->pixel_width = cmp->pixel_width;
25743 it->ascent = it->phys_ascent = cmp->ascent;
25744 it->descent = it->phys_descent = cmp->descent;
25745 if (face->box != FACE_NO_BOX)
25746 {
25747 int thick = face->box_line_width;
25748
25749 if (thick > 0)
25750 {
25751 it->ascent += thick;
25752 it->descent += thick;
25753 }
25754 else
25755 thick = - thick;
25756
25757 if (it->start_of_box_run_p)
25758 it->pixel_width += thick;
25759 if (it->end_of_box_run_p)
25760 it->pixel_width += thick;
25761 }
25762
25763 /* If face has an overline, add the height of the overline
25764 (1 pixel) and a 1 pixel margin to the character height. */
25765 if (face->overline_p)
25766 it->ascent += overline_margin;
25767
25768 take_vertical_position_into_account (it);
25769 if (it->ascent < 0)
25770 it->ascent = 0;
25771 if (it->descent < 0)
25772 it->descent = 0;
25773
25774 if (it->glyph_row && cmp->glyph_len > 0)
25775 append_composite_glyph (it);
25776 }
25777 else if (it->what == IT_COMPOSITION)
25778 {
25779 /* A dynamic (automatic) composition. */
25780 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25781 Lisp_Object gstring;
25782 struct font_metrics metrics;
25783
25784 it->nglyphs = 1;
25785
25786 gstring = composition_gstring_from_id (it->cmp_it.id);
25787 it->pixel_width
25788 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25789 &metrics);
25790 if (it->glyph_row
25791 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25792 it->glyph_row->contains_overlapping_glyphs_p = 1;
25793 it->ascent = it->phys_ascent = metrics.ascent;
25794 it->descent = it->phys_descent = metrics.descent;
25795 if (face->box != FACE_NO_BOX)
25796 {
25797 int thick = face->box_line_width;
25798
25799 if (thick > 0)
25800 {
25801 it->ascent += thick;
25802 it->descent += thick;
25803 }
25804 else
25805 thick = - thick;
25806
25807 if (it->start_of_box_run_p)
25808 it->pixel_width += thick;
25809 if (it->end_of_box_run_p)
25810 it->pixel_width += thick;
25811 }
25812 /* If face has an overline, add the height of the overline
25813 (1 pixel) and a 1 pixel margin to the character height. */
25814 if (face->overline_p)
25815 it->ascent += overline_margin;
25816 take_vertical_position_into_account (it);
25817 if (it->ascent < 0)
25818 it->ascent = 0;
25819 if (it->descent < 0)
25820 it->descent = 0;
25821
25822 if (it->glyph_row)
25823 append_composite_glyph (it);
25824 }
25825 else if (it->what == IT_GLYPHLESS)
25826 produce_glyphless_glyph (it, 0, Qnil);
25827 else if (it->what == IT_IMAGE)
25828 produce_image_glyph (it);
25829 else if (it->what == IT_STRETCH)
25830 produce_stretch_glyph (it);
25831
25832 done:
25833 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25834 because this isn't true for images with `:ascent 100'. */
25835 eassert (it->ascent >= 0 && it->descent >= 0);
25836 if (it->area == TEXT_AREA)
25837 it->current_x += it->pixel_width;
25838
25839 if (extra_line_spacing > 0)
25840 {
25841 it->descent += extra_line_spacing;
25842 if (extra_line_spacing > it->max_extra_line_spacing)
25843 it->max_extra_line_spacing = extra_line_spacing;
25844 }
25845
25846 it->max_ascent = max (it->max_ascent, it->ascent);
25847 it->max_descent = max (it->max_descent, it->descent);
25848 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25849 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25850 }
25851
25852 /* EXPORT for RIF:
25853 Output LEN glyphs starting at START at the nominal cursor position.
25854 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25855 being updated, and UPDATED_AREA is the area of that row being updated. */
25856
25857 void
25858 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25859 struct glyph *start, enum glyph_row_area updated_area, int len)
25860 {
25861 int x, hpos, chpos = w->phys_cursor.hpos;
25862
25863 eassert (updated_row);
25864 /* When the window is hscrolled, cursor hpos can legitimately be out
25865 of bounds, but we draw the cursor at the corresponding window
25866 margin in that case. */
25867 if (!updated_row->reversed_p && chpos < 0)
25868 chpos = 0;
25869 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25870 chpos = updated_row->used[TEXT_AREA] - 1;
25871
25872 block_input ();
25873
25874 /* Write glyphs. */
25875
25876 hpos = start - updated_row->glyphs[updated_area];
25877 x = draw_glyphs (w, w->output_cursor.x,
25878 updated_row, updated_area,
25879 hpos, hpos + len,
25880 DRAW_NORMAL_TEXT, 0);
25881
25882 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25883 if (updated_area == TEXT_AREA
25884 && w->phys_cursor_on_p
25885 && w->phys_cursor.vpos == w->output_cursor.vpos
25886 && chpos >= hpos
25887 && chpos < hpos + len)
25888 w->phys_cursor_on_p = 0;
25889
25890 unblock_input ();
25891
25892 /* Advance the output cursor. */
25893 w->output_cursor.hpos += len;
25894 w->output_cursor.x = x;
25895 }
25896
25897
25898 /* EXPORT for RIF:
25899 Insert LEN glyphs from START at the nominal cursor position. */
25900
25901 void
25902 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25903 struct glyph *start, enum glyph_row_area updated_area, int len)
25904 {
25905 struct frame *f;
25906 int line_height, shift_by_width, shifted_region_width;
25907 struct glyph_row *row;
25908 struct glyph *glyph;
25909 int frame_x, frame_y;
25910 ptrdiff_t hpos;
25911
25912 eassert (updated_row);
25913 block_input ();
25914 f = XFRAME (WINDOW_FRAME (w));
25915
25916 /* Get the height of the line we are in. */
25917 row = updated_row;
25918 line_height = row->height;
25919
25920 /* Get the width of the glyphs to insert. */
25921 shift_by_width = 0;
25922 for (glyph = start; glyph < start + len; ++glyph)
25923 shift_by_width += glyph->pixel_width;
25924
25925 /* Get the width of the region to shift right. */
25926 shifted_region_width = (window_box_width (w, updated_area)
25927 - w->output_cursor.x
25928 - shift_by_width);
25929
25930 /* Shift right. */
25931 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25932 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25933
25934 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25935 line_height, shift_by_width);
25936
25937 /* Write the glyphs. */
25938 hpos = start - row->glyphs[updated_area];
25939 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25940 hpos, hpos + len,
25941 DRAW_NORMAL_TEXT, 0);
25942
25943 /* Advance the output cursor. */
25944 w->output_cursor.hpos += len;
25945 w->output_cursor.x += shift_by_width;
25946 unblock_input ();
25947 }
25948
25949
25950 /* EXPORT for RIF:
25951 Erase the current text line from the nominal cursor position
25952 (inclusive) to pixel column TO_X (exclusive). The idea is that
25953 everything from TO_X onward is already erased.
25954
25955 TO_X is a pixel position relative to UPDATED_AREA of currently
25956 updated window W. TO_X == -1 means clear to the end of this area. */
25957
25958 void
25959 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25960 enum glyph_row_area updated_area, int to_x)
25961 {
25962 struct frame *f;
25963 int max_x, min_y, max_y;
25964 int from_x, from_y, to_y;
25965
25966 eassert (updated_row);
25967 f = XFRAME (w->frame);
25968
25969 if (updated_row->full_width_p)
25970 max_x = WINDOW_TOTAL_WIDTH (w);
25971 else
25972 max_x = window_box_width (w, updated_area);
25973 max_y = window_text_bottom_y (w);
25974
25975 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25976 of window. For TO_X > 0, truncate to end of drawing area. */
25977 if (to_x == 0)
25978 return;
25979 else if (to_x < 0)
25980 to_x = max_x;
25981 else
25982 to_x = min (to_x, max_x);
25983
25984 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25985
25986 /* Notice if the cursor will be cleared by this operation. */
25987 if (!updated_row->full_width_p)
25988 notice_overwritten_cursor (w, updated_area,
25989 w->output_cursor.x, -1,
25990 updated_row->y,
25991 MATRIX_ROW_BOTTOM_Y (updated_row));
25992
25993 from_x = w->output_cursor.x;
25994
25995 /* Translate to frame coordinates. */
25996 if (updated_row->full_width_p)
25997 {
25998 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25999 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26000 }
26001 else
26002 {
26003 int area_left = window_box_left (w, updated_area);
26004 from_x += area_left;
26005 to_x += area_left;
26006 }
26007
26008 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26009 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26010 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26011
26012 /* Prevent inadvertently clearing to end of the X window. */
26013 if (to_x > from_x && to_y > from_y)
26014 {
26015 block_input ();
26016 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26017 to_x - from_x, to_y - from_y);
26018 unblock_input ();
26019 }
26020 }
26021
26022 #endif /* HAVE_WINDOW_SYSTEM */
26023
26024
26025 \f
26026 /***********************************************************************
26027 Cursor types
26028 ***********************************************************************/
26029
26030 /* Value is the internal representation of the specified cursor type
26031 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26032 of the bar cursor. */
26033
26034 static enum text_cursor_kinds
26035 get_specified_cursor_type (Lisp_Object arg, int *width)
26036 {
26037 enum text_cursor_kinds type;
26038
26039 if (NILP (arg))
26040 return NO_CURSOR;
26041
26042 if (EQ (arg, Qbox))
26043 return FILLED_BOX_CURSOR;
26044
26045 if (EQ (arg, Qhollow))
26046 return HOLLOW_BOX_CURSOR;
26047
26048 if (EQ (arg, Qbar))
26049 {
26050 *width = 2;
26051 return BAR_CURSOR;
26052 }
26053
26054 if (CONSP (arg)
26055 && EQ (XCAR (arg), Qbar)
26056 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26057 {
26058 *width = XINT (XCDR (arg));
26059 return BAR_CURSOR;
26060 }
26061
26062 if (EQ (arg, Qhbar))
26063 {
26064 *width = 2;
26065 return HBAR_CURSOR;
26066 }
26067
26068 if (CONSP (arg)
26069 && EQ (XCAR (arg), Qhbar)
26070 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26071 {
26072 *width = XINT (XCDR (arg));
26073 return HBAR_CURSOR;
26074 }
26075
26076 /* Treat anything unknown as "hollow box cursor".
26077 It was bad to signal an error; people have trouble fixing
26078 .Xdefaults with Emacs, when it has something bad in it. */
26079 type = HOLLOW_BOX_CURSOR;
26080
26081 return type;
26082 }
26083
26084 /* Set the default cursor types for specified frame. */
26085 void
26086 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26087 {
26088 int width = 1;
26089 Lisp_Object tem;
26090
26091 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26092 FRAME_CURSOR_WIDTH (f) = width;
26093
26094 /* By default, set up the blink-off state depending on the on-state. */
26095
26096 tem = Fassoc (arg, Vblink_cursor_alist);
26097 if (!NILP (tem))
26098 {
26099 FRAME_BLINK_OFF_CURSOR (f)
26100 = get_specified_cursor_type (XCDR (tem), &width);
26101 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26102 }
26103 else
26104 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26105
26106 /* Make sure the cursor gets redrawn. */
26107 f->cursor_type_changed = 1;
26108 }
26109
26110
26111 #ifdef HAVE_WINDOW_SYSTEM
26112
26113 /* Return the cursor we want to be displayed in window W. Return
26114 width of bar/hbar cursor through WIDTH arg. Return with
26115 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26116 (i.e. if the `system caret' should track this cursor).
26117
26118 In a mini-buffer window, we want the cursor only to appear if we
26119 are reading input from this window. For the selected window, we
26120 want the cursor type given by the frame parameter or buffer local
26121 setting of cursor-type. If explicitly marked off, draw no cursor.
26122 In all other cases, we want a hollow box cursor. */
26123
26124 static enum text_cursor_kinds
26125 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26126 int *active_cursor)
26127 {
26128 struct frame *f = XFRAME (w->frame);
26129 struct buffer *b = XBUFFER (w->contents);
26130 int cursor_type = DEFAULT_CURSOR;
26131 Lisp_Object alt_cursor;
26132 int non_selected = 0;
26133
26134 *active_cursor = 1;
26135
26136 /* Echo area */
26137 if (cursor_in_echo_area
26138 && FRAME_HAS_MINIBUF_P (f)
26139 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26140 {
26141 if (w == XWINDOW (echo_area_window))
26142 {
26143 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26144 {
26145 *width = FRAME_CURSOR_WIDTH (f);
26146 return FRAME_DESIRED_CURSOR (f);
26147 }
26148 else
26149 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26150 }
26151
26152 *active_cursor = 0;
26153 non_selected = 1;
26154 }
26155
26156 /* Detect a nonselected window or nonselected frame. */
26157 else if (w != XWINDOW (f->selected_window)
26158 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26159 {
26160 *active_cursor = 0;
26161
26162 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26163 return NO_CURSOR;
26164
26165 non_selected = 1;
26166 }
26167
26168 /* Never display a cursor in a window in which cursor-type is nil. */
26169 if (NILP (BVAR (b, cursor_type)))
26170 return NO_CURSOR;
26171
26172 /* Get the normal cursor type for this window. */
26173 if (EQ (BVAR (b, cursor_type), Qt))
26174 {
26175 cursor_type = FRAME_DESIRED_CURSOR (f);
26176 *width = FRAME_CURSOR_WIDTH (f);
26177 }
26178 else
26179 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26180
26181 /* Use cursor-in-non-selected-windows instead
26182 for non-selected window or frame. */
26183 if (non_selected)
26184 {
26185 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26186 if (!EQ (Qt, alt_cursor))
26187 return get_specified_cursor_type (alt_cursor, width);
26188 /* t means modify the normal cursor type. */
26189 if (cursor_type == FILLED_BOX_CURSOR)
26190 cursor_type = HOLLOW_BOX_CURSOR;
26191 else if (cursor_type == BAR_CURSOR && *width > 1)
26192 --*width;
26193 return cursor_type;
26194 }
26195
26196 /* Use normal cursor if not blinked off. */
26197 if (!w->cursor_off_p)
26198 {
26199 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26200 {
26201 if (cursor_type == FILLED_BOX_CURSOR)
26202 {
26203 /* Using a block cursor on large images can be very annoying.
26204 So use a hollow cursor for "large" images.
26205 If image is not transparent (no mask), also use hollow cursor. */
26206 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26207 if (img != NULL && IMAGEP (img->spec))
26208 {
26209 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26210 where N = size of default frame font size.
26211 This should cover most of the "tiny" icons people may use. */
26212 if (!img->mask
26213 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26214 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26215 cursor_type = HOLLOW_BOX_CURSOR;
26216 }
26217 }
26218 else if (cursor_type != NO_CURSOR)
26219 {
26220 /* Display current only supports BOX and HOLLOW cursors for images.
26221 So for now, unconditionally use a HOLLOW cursor when cursor is
26222 not a solid box cursor. */
26223 cursor_type = HOLLOW_BOX_CURSOR;
26224 }
26225 }
26226 return cursor_type;
26227 }
26228
26229 /* Cursor is blinked off, so determine how to "toggle" it. */
26230
26231 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26232 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26233 return get_specified_cursor_type (XCDR (alt_cursor), width);
26234
26235 /* Then see if frame has specified a specific blink off cursor type. */
26236 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26237 {
26238 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26239 return FRAME_BLINK_OFF_CURSOR (f);
26240 }
26241
26242 #if 0
26243 /* Some people liked having a permanently visible blinking cursor,
26244 while others had very strong opinions against it. So it was
26245 decided to remove it. KFS 2003-09-03 */
26246
26247 /* Finally perform built-in cursor blinking:
26248 filled box <-> hollow box
26249 wide [h]bar <-> narrow [h]bar
26250 narrow [h]bar <-> no cursor
26251 other type <-> no cursor */
26252
26253 if (cursor_type == FILLED_BOX_CURSOR)
26254 return HOLLOW_BOX_CURSOR;
26255
26256 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26257 {
26258 *width = 1;
26259 return cursor_type;
26260 }
26261 #endif
26262
26263 return NO_CURSOR;
26264 }
26265
26266
26267 /* Notice when the text cursor of window W has been completely
26268 overwritten by a drawing operation that outputs glyphs in AREA
26269 starting at X0 and ending at X1 in the line starting at Y0 and
26270 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26271 the rest of the line after X0 has been written. Y coordinates
26272 are window-relative. */
26273
26274 static void
26275 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26276 int x0, int x1, int y0, int y1)
26277 {
26278 int cx0, cx1, cy0, cy1;
26279 struct glyph_row *row;
26280
26281 if (!w->phys_cursor_on_p)
26282 return;
26283 if (area != TEXT_AREA)
26284 return;
26285
26286 if (w->phys_cursor.vpos < 0
26287 || w->phys_cursor.vpos >= w->current_matrix->nrows
26288 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26289 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26290 return;
26291
26292 if (row->cursor_in_fringe_p)
26293 {
26294 row->cursor_in_fringe_p = 0;
26295 draw_fringe_bitmap (w, row, row->reversed_p);
26296 w->phys_cursor_on_p = 0;
26297 return;
26298 }
26299
26300 cx0 = w->phys_cursor.x;
26301 cx1 = cx0 + w->phys_cursor_width;
26302 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26303 return;
26304
26305 /* The cursor image will be completely removed from the
26306 screen if the output area intersects the cursor area in
26307 y-direction. When we draw in [y0 y1[, and some part of
26308 the cursor is at y < y0, that part must have been drawn
26309 before. When scrolling, the cursor is erased before
26310 actually scrolling, so we don't come here. When not
26311 scrolling, the rows above the old cursor row must have
26312 changed, and in this case these rows must have written
26313 over the cursor image.
26314
26315 Likewise if part of the cursor is below y1, with the
26316 exception of the cursor being in the first blank row at
26317 the buffer and window end because update_text_area
26318 doesn't draw that row. (Except when it does, but
26319 that's handled in update_text_area.) */
26320
26321 cy0 = w->phys_cursor.y;
26322 cy1 = cy0 + w->phys_cursor_height;
26323 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26324 return;
26325
26326 w->phys_cursor_on_p = 0;
26327 }
26328
26329 #endif /* HAVE_WINDOW_SYSTEM */
26330
26331 \f
26332 /************************************************************************
26333 Mouse Face
26334 ************************************************************************/
26335
26336 #ifdef HAVE_WINDOW_SYSTEM
26337
26338 /* EXPORT for RIF:
26339 Fix the display of area AREA of overlapping row ROW in window W
26340 with respect to the overlapping part OVERLAPS. */
26341
26342 void
26343 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26344 enum glyph_row_area area, int overlaps)
26345 {
26346 int i, x;
26347
26348 block_input ();
26349
26350 x = 0;
26351 for (i = 0; i < row->used[area];)
26352 {
26353 if (row->glyphs[area][i].overlaps_vertically_p)
26354 {
26355 int start = i, start_x = x;
26356
26357 do
26358 {
26359 x += row->glyphs[area][i].pixel_width;
26360 ++i;
26361 }
26362 while (i < row->used[area]
26363 && row->glyphs[area][i].overlaps_vertically_p);
26364
26365 draw_glyphs (w, start_x, row, area,
26366 start, i,
26367 DRAW_NORMAL_TEXT, overlaps);
26368 }
26369 else
26370 {
26371 x += row->glyphs[area][i].pixel_width;
26372 ++i;
26373 }
26374 }
26375
26376 unblock_input ();
26377 }
26378
26379
26380 /* EXPORT:
26381 Draw the cursor glyph of window W in glyph row ROW. See the
26382 comment of draw_glyphs for the meaning of HL. */
26383
26384 void
26385 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26386 enum draw_glyphs_face hl)
26387 {
26388 /* If cursor hpos is out of bounds, don't draw garbage. This can
26389 happen in mini-buffer windows when switching between echo area
26390 glyphs and mini-buffer. */
26391 if ((row->reversed_p
26392 ? (w->phys_cursor.hpos >= 0)
26393 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26394 {
26395 int on_p = w->phys_cursor_on_p;
26396 int x1;
26397 int hpos = w->phys_cursor.hpos;
26398
26399 /* When the window is hscrolled, cursor hpos can legitimately be
26400 out of bounds, but we draw the cursor at the corresponding
26401 window margin in that case. */
26402 if (!row->reversed_p && hpos < 0)
26403 hpos = 0;
26404 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26405 hpos = row->used[TEXT_AREA] - 1;
26406
26407 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26408 hl, 0);
26409 w->phys_cursor_on_p = on_p;
26410
26411 if (hl == DRAW_CURSOR)
26412 w->phys_cursor_width = x1 - w->phys_cursor.x;
26413 /* When we erase the cursor, and ROW is overlapped by other
26414 rows, make sure that these overlapping parts of other rows
26415 are redrawn. */
26416 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26417 {
26418 w->phys_cursor_width = x1 - w->phys_cursor.x;
26419
26420 if (row > w->current_matrix->rows
26421 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26422 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26423 OVERLAPS_ERASED_CURSOR);
26424
26425 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26426 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26427 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26428 OVERLAPS_ERASED_CURSOR);
26429 }
26430 }
26431 }
26432
26433
26434 /* Erase the image of a cursor of window W from the screen. */
26435
26436 #ifndef HAVE_NTGUI
26437 static
26438 #endif
26439 void
26440 erase_phys_cursor (struct window *w)
26441 {
26442 struct frame *f = XFRAME (w->frame);
26443 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26444 int hpos = w->phys_cursor.hpos;
26445 int vpos = w->phys_cursor.vpos;
26446 int mouse_face_here_p = 0;
26447 struct glyph_matrix *active_glyphs = w->current_matrix;
26448 struct glyph_row *cursor_row;
26449 struct glyph *cursor_glyph;
26450 enum draw_glyphs_face hl;
26451
26452 /* No cursor displayed or row invalidated => nothing to do on the
26453 screen. */
26454 if (w->phys_cursor_type == NO_CURSOR)
26455 goto mark_cursor_off;
26456
26457 /* VPOS >= active_glyphs->nrows means that window has been resized.
26458 Don't bother to erase the cursor. */
26459 if (vpos >= active_glyphs->nrows)
26460 goto mark_cursor_off;
26461
26462 /* If row containing cursor is marked invalid, there is nothing we
26463 can do. */
26464 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26465 if (!cursor_row->enabled_p)
26466 goto mark_cursor_off;
26467
26468 /* If line spacing is > 0, old cursor may only be partially visible in
26469 window after split-window. So adjust visible height. */
26470 cursor_row->visible_height = min (cursor_row->visible_height,
26471 window_text_bottom_y (w) - cursor_row->y);
26472
26473 /* If row is completely invisible, don't attempt to delete a cursor which
26474 isn't there. This can happen if cursor is at top of a window, and
26475 we switch to a buffer with a header line in that window. */
26476 if (cursor_row->visible_height <= 0)
26477 goto mark_cursor_off;
26478
26479 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26480 if (cursor_row->cursor_in_fringe_p)
26481 {
26482 cursor_row->cursor_in_fringe_p = 0;
26483 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26484 goto mark_cursor_off;
26485 }
26486
26487 /* This can happen when the new row is shorter than the old one.
26488 In this case, either draw_glyphs or clear_end_of_line
26489 should have cleared the cursor. Note that we wouldn't be
26490 able to erase the cursor in this case because we don't have a
26491 cursor glyph at hand. */
26492 if ((cursor_row->reversed_p
26493 ? (w->phys_cursor.hpos < 0)
26494 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26495 goto mark_cursor_off;
26496
26497 /* When the window is hscrolled, cursor hpos can legitimately be out
26498 of bounds, but we draw the cursor at the corresponding window
26499 margin in that case. */
26500 if (!cursor_row->reversed_p && hpos < 0)
26501 hpos = 0;
26502 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26503 hpos = cursor_row->used[TEXT_AREA] - 1;
26504
26505 /* If the cursor is in the mouse face area, redisplay that when
26506 we clear the cursor. */
26507 if (! NILP (hlinfo->mouse_face_window)
26508 && coords_in_mouse_face_p (w, hpos, vpos)
26509 /* Don't redraw the cursor's spot in mouse face if it is at the
26510 end of a line (on a newline). The cursor appears there, but
26511 mouse highlighting does not. */
26512 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26513 mouse_face_here_p = 1;
26514
26515 /* Maybe clear the display under the cursor. */
26516 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26517 {
26518 int x, y, left_x;
26519 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26520 int width;
26521
26522 cursor_glyph = get_phys_cursor_glyph (w);
26523 if (cursor_glyph == NULL)
26524 goto mark_cursor_off;
26525
26526 width = cursor_glyph->pixel_width;
26527 left_x = window_box_left_offset (w, TEXT_AREA);
26528 x = w->phys_cursor.x;
26529 if (x < left_x)
26530 width -= left_x - x;
26531 width = min (width, window_box_width (w, TEXT_AREA) - x);
26532 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26533 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26534
26535 if (width > 0)
26536 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26537 }
26538
26539 /* Erase the cursor by redrawing the character underneath it. */
26540 if (mouse_face_here_p)
26541 hl = DRAW_MOUSE_FACE;
26542 else
26543 hl = DRAW_NORMAL_TEXT;
26544 draw_phys_cursor_glyph (w, cursor_row, hl);
26545
26546 mark_cursor_off:
26547 w->phys_cursor_on_p = 0;
26548 w->phys_cursor_type = NO_CURSOR;
26549 }
26550
26551
26552 /* EXPORT:
26553 Display or clear cursor of window W. If ON is zero, clear the
26554 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26555 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26556
26557 void
26558 display_and_set_cursor (struct window *w, bool on,
26559 int hpos, int vpos, int x, int y)
26560 {
26561 struct frame *f = XFRAME (w->frame);
26562 int new_cursor_type;
26563 int new_cursor_width;
26564 int active_cursor;
26565 struct glyph_row *glyph_row;
26566 struct glyph *glyph;
26567
26568 /* This is pointless on invisible frames, and dangerous on garbaged
26569 windows and frames; in the latter case, the frame or window may
26570 be in the midst of changing its size, and x and y may be off the
26571 window. */
26572 if (! FRAME_VISIBLE_P (f)
26573 || FRAME_GARBAGED_P (f)
26574 || vpos >= w->current_matrix->nrows
26575 || hpos >= w->current_matrix->matrix_w)
26576 return;
26577
26578 /* If cursor is off and we want it off, return quickly. */
26579 if (!on && !w->phys_cursor_on_p)
26580 return;
26581
26582 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26583 /* If cursor row is not enabled, we don't really know where to
26584 display the cursor. */
26585 if (!glyph_row->enabled_p)
26586 {
26587 w->phys_cursor_on_p = 0;
26588 return;
26589 }
26590
26591 glyph = NULL;
26592 if (!glyph_row->exact_window_width_line_p
26593 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26594 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26595
26596 eassert (input_blocked_p ());
26597
26598 /* Set new_cursor_type to the cursor we want to be displayed. */
26599 new_cursor_type = get_window_cursor_type (w, glyph,
26600 &new_cursor_width, &active_cursor);
26601
26602 /* If cursor is currently being shown and we don't want it to be or
26603 it is in the wrong place, or the cursor type is not what we want,
26604 erase it. */
26605 if (w->phys_cursor_on_p
26606 && (!on
26607 || w->phys_cursor.x != x
26608 || w->phys_cursor.y != y
26609 || new_cursor_type != w->phys_cursor_type
26610 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26611 && new_cursor_width != w->phys_cursor_width)))
26612 erase_phys_cursor (w);
26613
26614 /* Don't check phys_cursor_on_p here because that flag is only set
26615 to zero in some cases where we know that the cursor has been
26616 completely erased, to avoid the extra work of erasing the cursor
26617 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26618 still not be visible, or it has only been partly erased. */
26619 if (on)
26620 {
26621 w->phys_cursor_ascent = glyph_row->ascent;
26622 w->phys_cursor_height = glyph_row->height;
26623
26624 /* Set phys_cursor_.* before x_draw_.* is called because some
26625 of them may need the information. */
26626 w->phys_cursor.x = x;
26627 w->phys_cursor.y = glyph_row->y;
26628 w->phys_cursor.hpos = hpos;
26629 w->phys_cursor.vpos = vpos;
26630 }
26631
26632 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26633 new_cursor_type, new_cursor_width,
26634 on, active_cursor);
26635 }
26636
26637
26638 /* Switch the display of W's cursor on or off, according to the value
26639 of ON. */
26640
26641 static void
26642 update_window_cursor (struct window *w, bool on)
26643 {
26644 /* Don't update cursor in windows whose frame is in the process
26645 of being deleted. */
26646 if (w->current_matrix)
26647 {
26648 int hpos = w->phys_cursor.hpos;
26649 int vpos = w->phys_cursor.vpos;
26650 struct glyph_row *row;
26651
26652 if (vpos >= w->current_matrix->nrows
26653 || hpos >= w->current_matrix->matrix_w)
26654 return;
26655
26656 row = MATRIX_ROW (w->current_matrix, vpos);
26657
26658 /* When the window is hscrolled, cursor hpos can legitimately be
26659 out of bounds, but we draw the cursor at the corresponding
26660 window margin in that case. */
26661 if (!row->reversed_p && hpos < 0)
26662 hpos = 0;
26663 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26664 hpos = row->used[TEXT_AREA] - 1;
26665
26666 block_input ();
26667 display_and_set_cursor (w, on, hpos, vpos,
26668 w->phys_cursor.x, w->phys_cursor.y);
26669 unblock_input ();
26670 }
26671 }
26672
26673
26674 /* Call update_window_cursor with parameter ON_P on all leaf windows
26675 in the window tree rooted at W. */
26676
26677 static void
26678 update_cursor_in_window_tree (struct window *w, bool on_p)
26679 {
26680 while (w)
26681 {
26682 if (WINDOWP (w->contents))
26683 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26684 else
26685 update_window_cursor (w, on_p);
26686
26687 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26688 }
26689 }
26690
26691
26692 /* EXPORT:
26693 Display the cursor on window W, or clear it, according to ON_P.
26694 Don't change the cursor's position. */
26695
26696 void
26697 x_update_cursor (struct frame *f, bool on_p)
26698 {
26699 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26700 }
26701
26702
26703 /* EXPORT:
26704 Clear the cursor of window W to background color, and mark the
26705 cursor as not shown. This is used when the text where the cursor
26706 is about to be rewritten. */
26707
26708 void
26709 x_clear_cursor (struct window *w)
26710 {
26711 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26712 update_window_cursor (w, 0);
26713 }
26714
26715 #endif /* HAVE_WINDOW_SYSTEM */
26716
26717 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26718 and MSDOS. */
26719 static void
26720 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26721 int start_hpos, int end_hpos,
26722 enum draw_glyphs_face draw)
26723 {
26724 #ifdef HAVE_WINDOW_SYSTEM
26725 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26726 {
26727 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26728 return;
26729 }
26730 #endif
26731 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26732 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26733 #endif
26734 }
26735
26736 /* Display the active region described by mouse_face_* according to DRAW. */
26737
26738 static void
26739 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26740 {
26741 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26742 struct frame *f = XFRAME (WINDOW_FRAME (w));
26743
26744 if (/* If window is in the process of being destroyed, don't bother
26745 to do anything. */
26746 w->current_matrix != NULL
26747 /* Don't update mouse highlight if hidden */
26748 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26749 /* Recognize when we are called to operate on rows that don't exist
26750 anymore. This can happen when a window is split. */
26751 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26752 {
26753 int phys_cursor_on_p = w->phys_cursor_on_p;
26754 struct glyph_row *row, *first, *last;
26755
26756 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26757 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26758
26759 for (row = first; row <= last && row->enabled_p; ++row)
26760 {
26761 int start_hpos, end_hpos, start_x;
26762
26763 /* For all but the first row, the highlight starts at column 0. */
26764 if (row == first)
26765 {
26766 /* R2L rows have BEG and END in reversed order, but the
26767 screen drawing geometry is always left to right. So
26768 we need to mirror the beginning and end of the
26769 highlighted area in R2L rows. */
26770 if (!row->reversed_p)
26771 {
26772 start_hpos = hlinfo->mouse_face_beg_col;
26773 start_x = hlinfo->mouse_face_beg_x;
26774 }
26775 else if (row == last)
26776 {
26777 start_hpos = hlinfo->mouse_face_end_col;
26778 start_x = hlinfo->mouse_face_end_x;
26779 }
26780 else
26781 {
26782 start_hpos = 0;
26783 start_x = 0;
26784 }
26785 }
26786 else if (row->reversed_p && row == last)
26787 {
26788 start_hpos = hlinfo->mouse_face_end_col;
26789 start_x = hlinfo->mouse_face_end_x;
26790 }
26791 else
26792 {
26793 start_hpos = 0;
26794 start_x = 0;
26795 }
26796
26797 if (row == last)
26798 {
26799 if (!row->reversed_p)
26800 end_hpos = hlinfo->mouse_face_end_col;
26801 else if (row == first)
26802 end_hpos = hlinfo->mouse_face_beg_col;
26803 else
26804 {
26805 end_hpos = row->used[TEXT_AREA];
26806 if (draw == DRAW_NORMAL_TEXT)
26807 row->fill_line_p = 1; /* Clear to end of line */
26808 }
26809 }
26810 else if (row->reversed_p && row == first)
26811 end_hpos = hlinfo->mouse_face_beg_col;
26812 else
26813 {
26814 end_hpos = row->used[TEXT_AREA];
26815 if (draw == DRAW_NORMAL_TEXT)
26816 row->fill_line_p = 1; /* Clear to end of line */
26817 }
26818
26819 if (end_hpos > start_hpos)
26820 {
26821 draw_row_with_mouse_face (w, start_x, row,
26822 start_hpos, end_hpos, draw);
26823
26824 row->mouse_face_p
26825 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26826 }
26827 }
26828
26829 #ifdef HAVE_WINDOW_SYSTEM
26830 /* When we've written over the cursor, arrange for it to
26831 be displayed again. */
26832 if (FRAME_WINDOW_P (f)
26833 && phys_cursor_on_p && !w->phys_cursor_on_p)
26834 {
26835 int hpos = w->phys_cursor.hpos;
26836
26837 /* When the window is hscrolled, cursor hpos can legitimately be
26838 out of bounds, but we draw the cursor at the corresponding
26839 window margin in that case. */
26840 if (!row->reversed_p && hpos < 0)
26841 hpos = 0;
26842 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26843 hpos = row->used[TEXT_AREA] - 1;
26844
26845 block_input ();
26846 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26847 w->phys_cursor.x, w->phys_cursor.y);
26848 unblock_input ();
26849 }
26850 #endif /* HAVE_WINDOW_SYSTEM */
26851 }
26852
26853 #ifdef HAVE_WINDOW_SYSTEM
26854 /* Change the mouse cursor. */
26855 if (FRAME_WINDOW_P (f))
26856 {
26857 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26858 if (draw == DRAW_NORMAL_TEXT
26859 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26860 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26861 else
26862 #endif
26863 if (draw == DRAW_MOUSE_FACE)
26864 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26865 else
26866 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26867 }
26868 #endif /* HAVE_WINDOW_SYSTEM */
26869 }
26870
26871 /* EXPORT:
26872 Clear out the mouse-highlighted active region.
26873 Redraw it un-highlighted first. Value is non-zero if mouse
26874 face was actually drawn unhighlighted. */
26875
26876 int
26877 clear_mouse_face (Mouse_HLInfo *hlinfo)
26878 {
26879 int cleared = 0;
26880
26881 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26882 {
26883 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26884 cleared = 1;
26885 }
26886
26887 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26888 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26889 hlinfo->mouse_face_window = Qnil;
26890 hlinfo->mouse_face_overlay = Qnil;
26891 return cleared;
26892 }
26893
26894 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26895 within the mouse face on that window. */
26896 static int
26897 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26898 {
26899 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26900
26901 /* Quickly resolve the easy cases. */
26902 if (!(WINDOWP (hlinfo->mouse_face_window)
26903 && XWINDOW (hlinfo->mouse_face_window) == w))
26904 return 0;
26905 if (vpos < hlinfo->mouse_face_beg_row
26906 || vpos > hlinfo->mouse_face_end_row)
26907 return 0;
26908 if (vpos > hlinfo->mouse_face_beg_row
26909 && vpos < hlinfo->mouse_face_end_row)
26910 return 1;
26911
26912 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26913 {
26914 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26915 {
26916 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26917 return 1;
26918 }
26919 else if ((vpos == hlinfo->mouse_face_beg_row
26920 && hpos >= hlinfo->mouse_face_beg_col)
26921 || (vpos == hlinfo->mouse_face_end_row
26922 && hpos < hlinfo->mouse_face_end_col))
26923 return 1;
26924 }
26925 else
26926 {
26927 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26928 {
26929 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26930 return 1;
26931 }
26932 else if ((vpos == hlinfo->mouse_face_beg_row
26933 && hpos <= hlinfo->mouse_face_beg_col)
26934 || (vpos == hlinfo->mouse_face_end_row
26935 && hpos > hlinfo->mouse_face_end_col))
26936 return 1;
26937 }
26938 return 0;
26939 }
26940
26941
26942 /* EXPORT:
26943 Non-zero if physical cursor of window W is within mouse face. */
26944
26945 int
26946 cursor_in_mouse_face_p (struct window *w)
26947 {
26948 int hpos = w->phys_cursor.hpos;
26949 int vpos = w->phys_cursor.vpos;
26950 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26951
26952 /* When the window is hscrolled, cursor hpos can legitimately be out
26953 of bounds, but we draw the cursor at the corresponding window
26954 margin in that case. */
26955 if (!row->reversed_p && hpos < 0)
26956 hpos = 0;
26957 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26958 hpos = row->used[TEXT_AREA] - 1;
26959
26960 return coords_in_mouse_face_p (w, hpos, vpos);
26961 }
26962
26963
26964 \f
26965 /* Find the glyph rows START_ROW and END_ROW of window W that display
26966 characters between buffer positions START_CHARPOS and END_CHARPOS
26967 (excluding END_CHARPOS). DISP_STRING is a display string that
26968 covers these buffer positions. This is similar to
26969 row_containing_pos, but is more accurate when bidi reordering makes
26970 buffer positions change non-linearly with glyph rows. */
26971 static void
26972 rows_from_pos_range (struct window *w,
26973 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26974 Lisp_Object disp_string,
26975 struct glyph_row **start, struct glyph_row **end)
26976 {
26977 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26978 int last_y = window_text_bottom_y (w);
26979 struct glyph_row *row;
26980
26981 *start = NULL;
26982 *end = NULL;
26983
26984 while (!first->enabled_p
26985 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26986 first++;
26987
26988 /* Find the START row. */
26989 for (row = first;
26990 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26991 row++)
26992 {
26993 /* A row can potentially be the START row if the range of the
26994 characters it displays intersects the range
26995 [START_CHARPOS..END_CHARPOS). */
26996 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26997 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26998 /* See the commentary in row_containing_pos, for the
26999 explanation of the complicated way to check whether
27000 some position is beyond the end of the characters
27001 displayed by a row. */
27002 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27003 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27004 && !row->ends_at_zv_p
27005 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27006 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27007 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27008 && !row->ends_at_zv_p
27009 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27010 {
27011 /* Found a candidate row. Now make sure at least one of the
27012 glyphs it displays has a charpos from the range
27013 [START_CHARPOS..END_CHARPOS).
27014
27015 This is not obvious because bidi reordering could make
27016 buffer positions of a row be 1,2,3,102,101,100, and if we
27017 want to highlight characters in [50..60), we don't want
27018 this row, even though [50..60) does intersect [1..103),
27019 the range of character positions given by the row's start
27020 and end positions. */
27021 struct glyph *g = row->glyphs[TEXT_AREA];
27022 struct glyph *e = g + row->used[TEXT_AREA];
27023
27024 while (g < e)
27025 {
27026 if (((BUFFERP (g->object) || INTEGERP (g->object))
27027 && start_charpos <= g->charpos && g->charpos < end_charpos)
27028 /* A glyph that comes from DISP_STRING is by
27029 definition to be highlighted. */
27030 || EQ (g->object, disp_string))
27031 *start = row;
27032 g++;
27033 }
27034 if (*start)
27035 break;
27036 }
27037 }
27038
27039 /* Find the END row. */
27040 if (!*start
27041 /* If the last row is partially visible, start looking for END
27042 from that row, instead of starting from FIRST. */
27043 && !(row->enabled_p
27044 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27045 row = first;
27046 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27047 {
27048 struct glyph_row *next = row + 1;
27049 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27050
27051 if (!next->enabled_p
27052 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27053 /* The first row >= START whose range of displayed characters
27054 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27055 is the row END + 1. */
27056 || (start_charpos < next_start
27057 && end_charpos < next_start)
27058 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27059 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27060 && !next->ends_at_zv_p
27061 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27062 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27063 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27064 && !next->ends_at_zv_p
27065 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27066 {
27067 *end = row;
27068 break;
27069 }
27070 else
27071 {
27072 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27073 but none of the characters it displays are in the range, it is
27074 also END + 1. */
27075 struct glyph *g = next->glyphs[TEXT_AREA];
27076 struct glyph *s = g;
27077 struct glyph *e = g + next->used[TEXT_AREA];
27078
27079 while (g < e)
27080 {
27081 if (((BUFFERP (g->object) || INTEGERP (g->object))
27082 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27083 /* If the buffer position of the first glyph in
27084 the row is equal to END_CHARPOS, it means
27085 the last character to be highlighted is the
27086 newline of ROW, and we must consider NEXT as
27087 END, not END+1. */
27088 || (((!next->reversed_p && g == s)
27089 || (next->reversed_p && g == e - 1))
27090 && (g->charpos == end_charpos
27091 /* Special case for when NEXT is an
27092 empty line at ZV. */
27093 || (g->charpos == -1
27094 && !row->ends_at_zv_p
27095 && next_start == end_charpos)))))
27096 /* A glyph that comes from DISP_STRING is by
27097 definition to be highlighted. */
27098 || EQ (g->object, disp_string))
27099 break;
27100 g++;
27101 }
27102 if (g == e)
27103 {
27104 *end = row;
27105 break;
27106 }
27107 /* The first row that ends at ZV must be the last to be
27108 highlighted. */
27109 else if (next->ends_at_zv_p)
27110 {
27111 *end = next;
27112 break;
27113 }
27114 }
27115 }
27116 }
27117
27118 /* This function sets the mouse_face_* elements of HLINFO, assuming
27119 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27120 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27121 for the overlay or run of text properties specifying the mouse
27122 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27123 before-string and after-string that must also be highlighted.
27124 DISP_STRING, if non-nil, is a display string that may cover some
27125 or all of the highlighted text. */
27126
27127 static void
27128 mouse_face_from_buffer_pos (Lisp_Object window,
27129 Mouse_HLInfo *hlinfo,
27130 ptrdiff_t mouse_charpos,
27131 ptrdiff_t start_charpos,
27132 ptrdiff_t end_charpos,
27133 Lisp_Object before_string,
27134 Lisp_Object after_string,
27135 Lisp_Object disp_string)
27136 {
27137 struct window *w = XWINDOW (window);
27138 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27139 struct glyph_row *r1, *r2;
27140 struct glyph *glyph, *end;
27141 ptrdiff_t ignore, pos;
27142 int x;
27143
27144 eassert (NILP (disp_string) || STRINGP (disp_string));
27145 eassert (NILP (before_string) || STRINGP (before_string));
27146 eassert (NILP (after_string) || STRINGP (after_string));
27147
27148 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27149 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27150 if (r1 == NULL)
27151 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27152 /* If the before-string or display-string contains newlines,
27153 rows_from_pos_range skips to its last row. Move back. */
27154 if (!NILP (before_string) || !NILP (disp_string))
27155 {
27156 struct glyph_row *prev;
27157 while ((prev = r1 - 1, prev >= first)
27158 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27159 && prev->used[TEXT_AREA] > 0)
27160 {
27161 struct glyph *beg = prev->glyphs[TEXT_AREA];
27162 glyph = beg + prev->used[TEXT_AREA];
27163 while (--glyph >= beg && INTEGERP (glyph->object));
27164 if (glyph < beg
27165 || !(EQ (glyph->object, before_string)
27166 || EQ (glyph->object, disp_string)))
27167 break;
27168 r1 = prev;
27169 }
27170 }
27171 if (r2 == NULL)
27172 {
27173 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27174 hlinfo->mouse_face_past_end = 1;
27175 }
27176 else if (!NILP (after_string))
27177 {
27178 /* If the after-string has newlines, advance to its last row. */
27179 struct glyph_row *next;
27180 struct glyph_row *last
27181 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27182
27183 for (next = r2 + 1;
27184 next <= last
27185 && next->used[TEXT_AREA] > 0
27186 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27187 ++next)
27188 r2 = next;
27189 }
27190 /* The rest of the display engine assumes that mouse_face_beg_row is
27191 either above mouse_face_end_row or identical to it. But with
27192 bidi-reordered continued lines, the row for START_CHARPOS could
27193 be below the row for END_CHARPOS. If so, swap the rows and store
27194 them in correct order. */
27195 if (r1->y > r2->y)
27196 {
27197 struct glyph_row *tem = r2;
27198
27199 r2 = r1;
27200 r1 = tem;
27201 }
27202
27203 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27204 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27205
27206 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27207 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27208 could be anywhere in the row and in any order. The strategy
27209 below is to find the leftmost and the rightmost glyph that
27210 belongs to either of these 3 strings, or whose position is
27211 between START_CHARPOS and END_CHARPOS, and highlight all the
27212 glyphs between those two. This may cover more than just the text
27213 between START_CHARPOS and END_CHARPOS if the range of characters
27214 strides the bidi level boundary, e.g. if the beginning is in R2L
27215 text while the end is in L2R text or vice versa. */
27216 if (!r1->reversed_p)
27217 {
27218 /* This row is in a left to right paragraph. Scan it left to
27219 right. */
27220 glyph = r1->glyphs[TEXT_AREA];
27221 end = glyph + r1->used[TEXT_AREA];
27222 x = r1->x;
27223
27224 /* Skip truncation glyphs at the start of the glyph row. */
27225 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27226 for (; glyph < end
27227 && INTEGERP (glyph->object)
27228 && glyph->charpos < 0;
27229 ++glyph)
27230 x += glyph->pixel_width;
27231
27232 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27233 or DISP_STRING, and the first glyph from buffer whose
27234 position is between START_CHARPOS and END_CHARPOS. */
27235 for (; glyph < end
27236 && !INTEGERP (glyph->object)
27237 && !EQ (glyph->object, disp_string)
27238 && !(BUFFERP (glyph->object)
27239 && (glyph->charpos >= start_charpos
27240 && glyph->charpos < end_charpos));
27241 ++glyph)
27242 {
27243 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27244 are present at buffer positions between START_CHARPOS and
27245 END_CHARPOS, or if they come from an overlay. */
27246 if (EQ (glyph->object, before_string))
27247 {
27248 pos = string_buffer_position (before_string,
27249 start_charpos);
27250 /* If pos == 0, it means before_string came from an
27251 overlay, not from a buffer position. */
27252 if (!pos || (pos >= start_charpos && pos < end_charpos))
27253 break;
27254 }
27255 else if (EQ (glyph->object, after_string))
27256 {
27257 pos = string_buffer_position (after_string, end_charpos);
27258 if (!pos || (pos >= start_charpos && pos < end_charpos))
27259 break;
27260 }
27261 x += glyph->pixel_width;
27262 }
27263 hlinfo->mouse_face_beg_x = x;
27264 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27265 }
27266 else
27267 {
27268 /* This row is in a right to left paragraph. Scan it right to
27269 left. */
27270 struct glyph *g;
27271
27272 end = r1->glyphs[TEXT_AREA] - 1;
27273 glyph = end + r1->used[TEXT_AREA];
27274
27275 /* Skip truncation glyphs at the start of the glyph row. */
27276 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27277 for (; glyph > end
27278 && INTEGERP (glyph->object)
27279 && glyph->charpos < 0;
27280 --glyph)
27281 ;
27282
27283 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27284 or DISP_STRING, and the first glyph from buffer whose
27285 position is between START_CHARPOS and END_CHARPOS. */
27286 for (; glyph > end
27287 && !INTEGERP (glyph->object)
27288 && !EQ (glyph->object, disp_string)
27289 && !(BUFFERP (glyph->object)
27290 && (glyph->charpos >= start_charpos
27291 && glyph->charpos < end_charpos));
27292 --glyph)
27293 {
27294 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27295 are present at buffer positions between START_CHARPOS and
27296 END_CHARPOS, or if they come from an overlay. */
27297 if (EQ (glyph->object, before_string))
27298 {
27299 pos = string_buffer_position (before_string, start_charpos);
27300 /* If pos == 0, it means before_string came from an
27301 overlay, not from a buffer position. */
27302 if (!pos || (pos >= start_charpos && pos < end_charpos))
27303 break;
27304 }
27305 else if (EQ (glyph->object, after_string))
27306 {
27307 pos = string_buffer_position (after_string, end_charpos);
27308 if (!pos || (pos >= start_charpos && pos < end_charpos))
27309 break;
27310 }
27311 }
27312
27313 glyph++; /* first glyph to the right of the highlighted area */
27314 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27315 x += g->pixel_width;
27316 hlinfo->mouse_face_beg_x = x;
27317 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27318 }
27319
27320 /* If the highlight ends in a different row, compute GLYPH and END
27321 for the end row. Otherwise, reuse the values computed above for
27322 the row where the highlight begins. */
27323 if (r2 != r1)
27324 {
27325 if (!r2->reversed_p)
27326 {
27327 glyph = r2->glyphs[TEXT_AREA];
27328 end = glyph + r2->used[TEXT_AREA];
27329 x = r2->x;
27330 }
27331 else
27332 {
27333 end = r2->glyphs[TEXT_AREA] - 1;
27334 glyph = end + r2->used[TEXT_AREA];
27335 }
27336 }
27337
27338 if (!r2->reversed_p)
27339 {
27340 /* Skip truncation and continuation glyphs near the end of the
27341 row, and also blanks and stretch glyphs inserted by
27342 extend_face_to_end_of_line. */
27343 while (end > glyph
27344 && INTEGERP ((end - 1)->object))
27345 --end;
27346 /* Scan the rest of the glyph row from the end, looking for the
27347 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27348 DISP_STRING, or whose position is between START_CHARPOS
27349 and END_CHARPOS */
27350 for (--end;
27351 end > glyph
27352 && !INTEGERP (end->object)
27353 && !EQ (end->object, disp_string)
27354 && !(BUFFERP (end->object)
27355 && (end->charpos >= start_charpos
27356 && end->charpos < end_charpos));
27357 --end)
27358 {
27359 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27360 are present at buffer positions between START_CHARPOS and
27361 END_CHARPOS, or if they come from an overlay. */
27362 if (EQ (end->object, before_string))
27363 {
27364 pos = string_buffer_position (before_string, start_charpos);
27365 if (!pos || (pos >= start_charpos && pos < end_charpos))
27366 break;
27367 }
27368 else if (EQ (end->object, after_string))
27369 {
27370 pos = string_buffer_position (after_string, end_charpos);
27371 if (!pos || (pos >= start_charpos && pos < end_charpos))
27372 break;
27373 }
27374 }
27375 /* Find the X coordinate of the last glyph to be highlighted. */
27376 for (; glyph <= end; ++glyph)
27377 x += glyph->pixel_width;
27378
27379 hlinfo->mouse_face_end_x = x;
27380 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27381 }
27382 else
27383 {
27384 /* Skip truncation and continuation glyphs near the end of the
27385 row, and also blanks and stretch glyphs inserted by
27386 extend_face_to_end_of_line. */
27387 x = r2->x;
27388 end++;
27389 while (end < glyph
27390 && INTEGERP (end->object))
27391 {
27392 x += end->pixel_width;
27393 ++end;
27394 }
27395 /* Scan the rest of the glyph row from the end, looking for the
27396 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27397 DISP_STRING, or whose position is between START_CHARPOS
27398 and END_CHARPOS */
27399 for ( ;
27400 end < glyph
27401 && !INTEGERP (end->object)
27402 && !EQ (end->object, disp_string)
27403 && !(BUFFERP (end->object)
27404 && (end->charpos >= start_charpos
27405 && end->charpos < end_charpos));
27406 ++end)
27407 {
27408 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27409 are present at buffer positions between START_CHARPOS and
27410 END_CHARPOS, or if they come from an overlay. */
27411 if (EQ (end->object, before_string))
27412 {
27413 pos = string_buffer_position (before_string, start_charpos);
27414 if (!pos || (pos >= start_charpos && pos < end_charpos))
27415 break;
27416 }
27417 else if (EQ (end->object, after_string))
27418 {
27419 pos = string_buffer_position (after_string, end_charpos);
27420 if (!pos || (pos >= start_charpos && pos < end_charpos))
27421 break;
27422 }
27423 x += end->pixel_width;
27424 }
27425 /* If we exited the above loop because we arrived at the last
27426 glyph of the row, and its buffer position is still not in
27427 range, it means the last character in range is the preceding
27428 newline. Bump the end column and x values to get past the
27429 last glyph. */
27430 if (end == glyph
27431 && BUFFERP (end->object)
27432 && (end->charpos < start_charpos
27433 || end->charpos >= end_charpos))
27434 {
27435 x += end->pixel_width;
27436 ++end;
27437 }
27438 hlinfo->mouse_face_end_x = x;
27439 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27440 }
27441
27442 hlinfo->mouse_face_window = window;
27443 hlinfo->mouse_face_face_id
27444 = face_at_buffer_position (w, mouse_charpos, &ignore,
27445 mouse_charpos + 1,
27446 !hlinfo->mouse_face_hidden, -1);
27447 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27448 }
27449
27450 /* The following function is not used anymore (replaced with
27451 mouse_face_from_string_pos), but I leave it here for the time
27452 being, in case someone would. */
27453
27454 #if 0 /* not used */
27455
27456 /* Find the position of the glyph for position POS in OBJECT in
27457 window W's current matrix, and return in *X, *Y the pixel
27458 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27459
27460 RIGHT_P non-zero means return the position of the right edge of the
27461 glyph, RIGHT_P zero means return the left edge position.
27462
27463 If no glyph for POS exists in the matrix, return the position of
27464 the glyph with the next smaller position that is in the matrix, if
27465 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27466 exists in the matrix, return the position of the glyph with the
27467 next larger position in OBJECT.
27468
27469 Value is non-zero if a glyph was found. */
27470
27471 static int
27472 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27473 int *hpos, int *vpos, int *x, int *y, int right_p)
27474 {
27475 int yb = window_text_bottom_y (w);
27476 struct glyph_row *r;
27477 struct glyph *best_glyph = NULL;
27478 struct glyph_row *best_row = NULL;
27479 int best_x = 0;
27480
27481 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27482 r->enabled_p && r->y < yb;
27483 ++r)
27484 {
27485 struct glyph *g = r->glyphs[TEXT_AREA];
27486 struct glyph *e = g + r->used[TEXT_AREA];
27487 int gx;
27488
27489 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27490 if (EQ (g->object, object))
27491 {
27492 if (g->charpos == pos)
27493 {
27494 best_glyph = g;
27495 best_x = gx;
27496 best_row = r;
27497 goto found;
27498 }
27499 else if (best_glyph == NULL
27500 || ((eabs (g->charpos - pos)
27501 < eabs (best_glyph->charpos - pos))
27502 && (right_p
27503 ? g->charpos < pos
27504 : g->charpos > pos)))
27505 {
27506 best_glyph = g;
27507 best_x = gx;
27508 best_row = r;
27509 }
27510 }
27511 }
27512
27513 found:
27514
27515 if (best_glyph)
27516 {
27517 *x = best_x;
27518 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27519
27520 if (right_p)
27521 {
27522 *x += best_glyph->pixel_width;
27523 ++*hpos;
27524 }
27525
27526 *y = best_row->y;
27527 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27528 }
27529
27530 return best_glyph != NULL;
27531 }
27532 #endif /* not used */
27533
27534 /* Find the positions of the first and the last glyphs in window W's
27535 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27536 (assumed to be a string), and return in HLINFO's mouse_face_*
27537 members the pixel and column/row coordinates of those glyphs. */
27538
27539 static void
27540 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27541 Lisp_Object object,
27542 ptrdiff_t startpos, ptrdiff_t endpos)
27543 {
27544 int yb = window_text_bottom_y (w);
27545 struct glyph_row *r;
27546 struct glyph *g, *e;
27547 int gx;
27548 int found = 0;
27549
27550 /* Find the glyph row with at least one position in the range
27551 [STARTPOS..ENDPOS), and the first glyph in that row whose
27552 position belongs to that range. */
27553 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27554 r->enabled_p && r->y < yb;
27555 ++r)
27556 {
27557 if (!r->reversed_p)
27558 {
27559 g = r->glyphs[TEXT_AREA];
27560 e = g + r->used[TEXT_AREA];
27561 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27562 if (EQ (g->object, object)
27563 && startpos <= g->charpos && g->charpos < endpos)
27564 {
27565 hlinfo->mouse_face_beg_row
27566 = MATRIX_ROW_VPOS (r, w->current_matrix);
27567 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27568 hlinfo->mouse_face_beg_x = gx;
27569 found = 1;
27570 break;
27571 }
27572 }
27573 else
27574 {
27575 struct glyph *g1;
27576
27577 e = r->glyphs[TEXT_AREA];
27578 g = e + r->used[TEXT_AREA];
27579 for ( ; g > e; --g)
27580 if (EQ ((g-1)->object, object)
27581 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27582 {
27583 hlinfo->mouse_face_beg_row
27584 = MATRIX_ROW_VPOS (r, w->current_matrix);
27585 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27586 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27587 gx += g1->pixel_width;
27588 hlinfo->mouse_face_beg_x = gx;
27589 found = 1;
27590 break;
27591 }
27592 }
27593 if (found)
27594 break;
27595 }
27596
27597 if (!found)
27598 return;
27599
27600 /* Starting with the next row, look for the first row which does NOT
27601 include any glyphs whose positions are in the range. */
27602 for (++r; r->enabled_p && r->y < yb; ++r)
27603 {
27604 g = r->glyphs[TEXT_AREA];
27605 e = g + r->used[TEXT_AREA];
27606 found = 0;
27607 for ( ; g < e; ++g)
27608 if (EQ (g->object, object)
27609 && startpos <= g->charpos && g->charpos < endpos)
27610 {
27611 found = 1;
27612 break;
27613 }
27614 if (!found)
27615 break;
27616 }
27617
27618 /* The highlighted region ends on the previous row. */
27619 r--;
27620
27621 /* Set the end row. */
27622 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27623
27624 /* Compute and set the end column and the end column's horizontal
27625 pixel coordinate. */
27626 if (!r->reversed_p)
27627 {
27628 g = r->glyphs[TEXT_AREA];
27629 e = g + r->used[TEXT_AREA];
27630 for ( ; e > g; --e)
27631 if (EQ ((e-1)->object, object)
27632 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27633 break;
27634 hlinfo->mouse_face_end_col = e - g;
27635
27636 for (gx = r->x; g < e; ++g)
27637 gx += g->pixel_width;
27638 hlinfo->mouse_face_end_x = gx;
27639 }
27640 else
27641 {
27642 e = r->glyphs[TEXT_AREA];
27643 g = e + r->used[TEXT_AREA];
27644 for (gx = r->x ; e < g; ++e)
27645 {
27646 if (EQ (e->object, object)
27647 && startpos <= e->charpos && e->charpos < endpos)
27648 break;
27649 gx += e->pixel_width;
27650 }
27651 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27652 hlinfo->mouse_face_end_x = gx;
27653 }
27654 }
27655
27656 #ifdef HAVE_WINDOW_SYSTEM
27657
27658 /* See if position X, Y is within a hot-spot of an image. */
27659
27660 static int
27661 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27662 {
27663 if (!CONSP (hot_spot))
27664 return 0;
27665
27666 if (EQ (XCAR (hot_spot), Qrect))
27667 {
27668 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27669 Lisp_Object rect = XCDR (hot_spot);
27670 Lisp_Object tem;
27671 if (!CONSP (rect))
27672 return 0;
27673 if (!CONSP (XCAR (rect)))
27674 return 0;
27675 if (!CONSP (XCDR (rect)))
27676 return 0;
27677 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27678 return 0;
27679 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27680 return 0;
27681 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27682 return 0;
27683 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27684 return 0;
27685 return 1;
27686 }
27687 else if (EQ (XCAR (hot_spot), Qcircle))
27688 {
27689 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27690 Lisp_Object circ = XCDR (hot_spot);
27691 Lisp_Object lr, lx0, ly0;
27692 if (CONSP (circ)
27693 && CONSP (XCAR (circ))
27694 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27695 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27696 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27697 {
27698 double r = XFLOATINT (lr);
27699 double dx = XINT (lx0) - x;
27700 double dy = XINT (ly0) - y;
27701 return (dx * dx + dy * dy <= r * r);
27702 }
27703 }
27704 else if (EQ (XCAR (hot_spot), Qpoly))
27705 {
27706 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27707 if (VECTORP (XCDR (hot_spot)))
27708 {
27709 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27710 Lisp_Object *poly = v->contents;
27711 ptrdiff_t n = v->header.size;
27712 ptrdiff_t i;
27713 int inside = 0;
27714 Lisp_Object lx, ly;
27715 int x0, y0;
27716
27717 /* Need an even number of coordinates, and at least 3 edges. */
27718 if (n < 6 || n & 1)
27719 return 0;
27720
27721 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27722 If count is odd, we are inside polygon. Pixels on edges
27723 may or may not be included depending on actual geometry of the
27724 polygon. */
27725 if ((lx = poly[n-2], !INTEGERP (lx))
27726 || (ly = poly[n-1], !INTEGERP (lx)))
27727 return 0;
27728 x0 = XINT (lx), y0 = XINT (ly);
27729 for (i = 0; i < n; i += 2)
27730 {
27731 int x1 = x0, y1 = y0;
27732 if ((lx = poly[i], !INTEGERP (lx))
27733 || (ly = poly[i+1], !INTEGERP (ly)))
27734 return 0;
27735 x0 = XINT (lx), y0 = XINT (ly);
27736
27737 /* Does this segment cross the X line? */
27738 if (x0 >= x)
27739 {
27740 if (x1 >= x)
27741 continue;
27742 }
27743 else if (x1 < x)
27744 continue;
27745 if (y > y0 && y > y1)
27746 continue;
27747 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27748 inside = !inside;
27749 }
27750 return inside;
27751 }
27752 }
27753 return 0;
27754 }
27755
27756 Lisp_Object
27757 find_hot_spot (Lisp_Object map, int x, int y)
27758 {
27759 while (CONSP (map))
27760 {
27761 if (CONSP (XCAR (map))
27762 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27763 return XCAR (map);
27764 map = XCDR (map);
27765 }
27766
27767 return Qnil;
27768 }
27769
27770 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27771 3, 3, 0,
27772 doc: /* Lookup in image map MAP coordinates X and Y.
27773 An image map is an alist where each element has the format (AREA ID PLIST).
27774 An AREA is specified as either a rectangle, a circle, or a polygon:
27775 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27776 pixel coordinates of the upper left and bottom right corners.
27777 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27778 and the radius of the circle; r may be a float or integer.
27779 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27780 vector describes one corner in the polygon.
27781 Returns the alist element for the first matching AREA in MAP. */)
27782 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27783 {
27784 if (NILP (map))
27785 return Qnil;
27786
27787 CHECK_NUMBER (x);
27788 CHECK_NUMBER (y);
27789
27790 return find_hot_spot (map,
27791 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27792 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27793 }
27794
27795
27796 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27797 static void
27798 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27799 {
27800 /* Do not change cursor shape while dragging mouse. */
27801 if (!NILP (do_mouse_tracking))
27802 return;
27803
27804 if (!NILP (pointer))
27805 {
27806 if (EQ (pointer, Qarrow))
27807 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27808 else if (EQ (pointer, Qhand))
27809 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27810 else if (EQ (pointer, Qtext))
27811 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27812 else if (EQ (pointer, intern ("hdrag")))
27813 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27814 #ifdef HAVE_X_WINDOWS
27815 else if (EQ (pointer, intern ("vdrag")))
27816 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27817 #endif
27818 else if (EQ (pointer, intern ("hourglass")))
27819 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27820 else if (EQ (pointer, Qmodeline))
27821 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27822 else
27823 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27824 }
27825
27826 if (cursor != No_Cursor)
27827 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27828 }
27829
27830 #endif /* HAVE_WINDOW_SYSTEM */
27831
27832 /* Take proper action when mouse has moved to the mode or header line
27833 or marginal area AREA of window W, x-position X and y-position Y.
27834 X is relative to the start of the text display area of W, so the
27835 width of bitmap areas and scroll bars must be subtracted to get a
27836 position relative to the start of the mode line. */
27837
27838 static void
27839 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27840 enum window_part area)
27841 {
27842 struct window *w = XWINDOW (window);
27843 struct frame *f = XFRAME (w->frame);
27844 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27845 #ifdef HAVE_WINDOW_SYSTEM
27846 Display_Info *dpyinfo;
27847 #endif
27848 Cursor cursor = No_Cursor;
27849 Lisp_Object pointer = Qnil;
27850 int dx, dy, width, height;
27851 ptrdiff_t charpos;
27852 Lisp_Object string, object = Qnil;
27853 Lisp_Object pos IF_LINT (= Qnil), help;
27854
27855 Lisp_Object mouse_face;
27856 int original_x_pixel = x;
27857 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27858 struct glyph_row *row IF_LINT (= 0);
27859
27860 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27861 {
27862 int x0;
27863 struct glyph *end;
27864
27865 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27866 returns them in row/column units! */
27867 string = mode_line_string (w, area, &x, &y, &charpos,
27868 &object, &dx, &dy, &width, &height);
27869
27870 row = (area == ON_MODE_LINE
27871 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27872 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27873
27874 /* Find the glyph under the mouse pointer. */
27875 if (row->mode_line_p && row->enabled_p)
27876 {
27877 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27878 end = glyph + row->used[TEXT_AREA];
27879
27880 for (x0 = original_x_pixel;
27881 glyph < end && x0 >= glyph->pixel_width;
27882 ++glyph)
27883 x0 -= glyph->pixel_width;
27884
27885 if (glyph >= end)
27886 glyph = NULL;
27887 }
27888 }
27889 else
27890 {
27891 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27892 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27893 returns them in row/column units! */
27894 string = marginal_area_string (w, area, &x, &y, &charpos,
27895 &object, &dx, &dy, &width, &height);
27896 }
27897
27898 help = Qnil;
27899
27900 #ifdef HAVE_WINDOW_SYSTEM
27901 if (IMAGEP (object))
27902 {
27903 Lisp_Object image_map, hotspot;
27904 if ((image_map = Fplist_get (XCDR (object), QCmap),
27905 !NILP (image_map))
27906 && (hotspot = find_hot_spot (image_map, dx, dy),
27907 CONSP (hotspot))
27908 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27909 {
27910 Lisp_Object plist;
27911
27912 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27913 If so, we could look for mouse-enter, mouse-leave
27914 properties in PLIST (and do something...). */
27915 hotspot = XCDR (hotspot);
27916 if (CONSP (hotspot)
27917 && (plist = XCAR (hotspot), CONSP (plist)))
27918 {
27919 pointer = Fplist_get (plist, Qpointer);
27920 if (NILP (pointer))
27921 pointer = Qhand;
27922 help = Fplist_get (plist, Qhelp_echo);
27923 if (!NILP (help))
27924 {
27925 help_echo_string = help;
27926 XSETWINDOW (help_echo_window, w);
27927 help_echo_object = w->contents;
27928 help_echo_pos = charpos;
27929 }
27930 }
27931 }
27932 if (NILP (pointer))
27933 pointer = Fplist_get (XCDR (object), QCpointer);
27934 }
27935 #endif /* HAVE_WINDOW_SYSTEM */
27936
27937 if (STRINGP (string))
27938 pos = make_number (charpos);
27939
27940 /* Set the help text and mouse pointer. If the mouse is on a part
27941 of the mode line without any text (e.g. past the right edge of
27942 the mode line text), use the default help text and pointer. */
27943 if (STRINGP (string) || area == ON_MODE_LINE)
27944 {
27945 /* Arrange to display the help by setting the global variables
27946 help_echo_string, help_echo_object, and help_echo_pos. */
27947 if (NILP (help))
27948 {
27949 if (STRINGP (string))
27950 help = Fget_text_property (pos, Qhelp_echo, string);
27951
27952 if (!NILP (help))
27953 {
27954 help_echo_string = help;
27955 XSETWINDOW (help_echo_window, w);
27956 help_echo_object = string;
27957 help_echo_pos = charpos;
27958 }
27959 else if (area == ON_MODE_LINE)
27960 {
27961 Lisp_Object default_help
27962 = buffer_local_value_1 (Qmode_line_default_help_echo,
27963 w->contents);
27964
27965 if (STRINGP (default_help))
27966 {
27967 help_echo_string = default_help;
27968 XSETWINDOW (help_echo_window, w);
27969 help_echo_object = Qnil;
27970 help_echo_pos = -1;
27971 }
27972 }
27973 }
27974
27975 #ifdef HAVE_WINDOW_SYSTEM
27976 /* Change the mouse pointer according to what is under it. */
27977 if (FRAME_WINDOW_P (f))
27978 {
27979 dpyinfo = FRAME_DISPLAY_INFO (f);
27980 if (STRINGP (string))
27981 {
27982 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27983
27984 if (NILP (pointer))
27985 pointer = Fget_text_property (pos, Qpointer, string);
27986
27987 /* Change the mouse pointer according to what is under X/Y. */
27988 if (NILP (pointer)
27989 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27990 {
27991 Lisp_Object map;
27992 map = Fget_text_property (pos, Qlocal_map, string);
27993 if (!KEYMAPP (map))
27994 map = Fget_text_property (pos, Qkeymap, string);
27995 if (!KEYMAPP (map))
27996 cursor = dpyinfo->vertical_scroll_bar_cursor;
27997 }
27998 }
27999 else
28000 /* Default mode-line pointer. */
28001 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28002 }
28003 #endif
28004 }
28005
28006 /* Change the mouse face according to what is under X/Y. */
28007 if (STRINGP (string))
28008 {
28009 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28010 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28011 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28012 && glyph)
28013 {
28014 Lisp_Object b, e;
28015
28016 struct glyph * tmp_glyph;
28017
28018 int gpos;
28019 int gseq_length;
28020 int total_pixel_width;
28021 ptrdiff_t begpos, endpos, ignore;
28022
28023 int vpos, hpos;
28024
28025 b = Fprevious_single_property_change (make_number (charpos + 1),
28026 Qmouse_face, string, Qnil);
28027 if (NILP (b))
28028 begpos = 0;
28029 else
28030 begpos = XINT (b);
28031
28032 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28033 if (NILP (e))
28034 endpos = SCHARS (string);
28035 else
28036 endpos = XINT (e);
28037
28038 /* Calculate the glyph position GPOS of GLYPH in the
28039 displayed string, relative to the beginning of the
28040 highlighted part of the string.
28041
28042 Note: GPOS is different from CHARPOS. CHARPOS is the
28043 position of GLYPH in the internal string object. A mode
28044 line string format has structures which are converted to
28045 a flattened string by the Emacs Lisp interpreter. The
28046 internal string is an element of those structures. The
28047 displayed string is the flattened string. */
28048 tmp_glyph = row_start_glyph;
28049 while (tmp_glyph < glyph
28050 && (!(EQ (tmp_glyph->object, glyph->object)
28051 && begpos <= tmp_glyph->charpos
28052 && tmp_glyph->charpos < endpos)))
28053 tmp_glyph++;
28054 gpos = glyph - tmp_glyph;
28055
28056 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28057 the highlighted part of the displayed string to which
28058 GLYPH belongs. Note: GSEQ_LENGTH is different from
28059 SCHARS (STRING), because the latter returns the length of
28060 the internal string. */
28061 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28062 tmp_glyph > glyph
28063 && (!(EQ (tmp_glyph->object, glyph->object)
28064 && begpos <= tmp_glyph->charpos
28065 && tmp_glyph->charpos < endpos));
28066 tmp_glyph--)
28067 ;
28068 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28069
28070 /* Calculate the total pixel width of all the glyphs between
28071 the beginning of the highlighted area and GLYPH. */
28072 total_pixel_width = 0;
28073 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28074 total_pixel_width += tmp_glyph->pixel_width;
28075
28076 /* Pre calculation of re-rendering position. Note: X is in
28077 column units here, after the call to mode_line_string or
28078 marginal_area_string. */
28079 hpos = x - gpos;
28080 vpos = (area == ON_MODE_LINE
28081 ? (w->current_matrix)->nrows - 1
28082 : 0);
28083
28084 /* If GLYPH's position is included in the region that is
28085 already drawn in mouse face, we have nothing to do. */
28086 if ( EQ (window, hlinfo->mouse_face_window)
28087 && (!row->reversed_p
28088 ? (hlinfo->mouse_face_beg_col <= hpos
28089 && hpos < hlinfo->mouse_face_end_col)
28090 /* In R2L rows we swap BEG and END, see below. */
28091 : (hlinfo->mouse_face_end_col <= hpos
28092 && hpos < hlinfo->mouse_face_beg_col))
28093 && hlinfo->mouse_face_beg_row == vpos )
28094 return;
28095
28096 if (clear_mouse_face (hlinfo))
28097 cursor = No_Cursor;
28098
28099 if (!row->reversed_p)
28100 {
28101 hlinfo->mouse_face_beg_col = hpos;
28102 hlinfo->mouse_face_beg_x = original_x_pixel
28103 - (total_pixel_width + dx);
28104 hlinfo->mouse_face_end_col = hpos + gseq_length;
28105 hlinfo->mouse_face_end_x = 0;
28106 }
28107 else
28108 {
28109 /* In R2L rows, show_mouse_face expects BEG and END
28110 coordinates to be swapped. */
28111 hlinfo->mouse_face_end_col = hpos;
28112 hlinfo->mouse_face_end_x = original_x_pixel
28113 - (total_pixel_width + dx);
28114 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28115 hlinfo->mouse_face_beg_x = 0;
28116 }
28117
28118 hlinfo->mouse_face_beg_row = vpos;
28119 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28120 hlinfo->mouse_face_past_end = 0;
28121 hlinfo->mouse_face_window = window;
28122
28123 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28124 charpos,
28125 0, &ignore,
28126 glyph->face_id,
28127 1);
28128 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28129
28130 if (NILP (pointer))
28131 pointer = Qhand;
28132 }
28133 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28134 clear_mouse_face (hlinfo);
28135 }
28136 #ifdef HAVE_WINDOW_SYSTEM
28137 if (FRAME_WINDOW_P (f))
28138 define_frame_cursor1 (f, cursor, pointer);
28139 #endif
28140 }
28141
28142
28143 /* EXPORT:
28144 Take proper action when the mouse has moved to position X, Y on
28145 frame F with regards to highlighting portions of display that have
28146 mouse-face properties. Also de-highlight portions of display where
28147 the mouse was before, set the mouse pointer shape as appropriate
28148 for the mouse coordinates, and activate help echo (tooltips).
28149 X and Y can be negative or out of range. */
28150
28151 void
28152 note_mouse_highlight (struct frame *f, int x, int y)
28153 {
28154 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28155 enum window_part part = ON_NOTHING;
28156 Lisp_Object window;
28157 struct window *w;
28158 Cursor cursor = No_Cursor;
28159 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28160 struct buffer *b;
28161
28162 /* When a menu is active, don't highlight because this looks odd. */
28163 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28164 if (popup_activated ())
28165 return;
28166 #endif
28167
28168 if (!f->glyphs_initialized_p
28169 || f->pointer_invisible)
28170 return;
28171
28172 hlinfo->mouse_face_mouse_x = x;
28173 hlinfo->mouse_face_mouse_y = y;
28174 hlinfo->mouse_face_mouse_frame = f;
28175
28176 if (hlinfo->mouse_face_defer)
28177 return;
28178
28179 /* Which window is that in? */
28180 window = window_from_coordinates (f, x, y, &part, 1);
28181
28182 /* If displaying active text in another window, clear that. */
28183 if (! EQ (window, hlinfo->mouse_face_window)
28184 /* Also clear if we move out of text area in same window. */
28185 || (!NILP (hlinfo->mouse_face_window)
28186 && !NILP (window)
28187 && part != ON_TEXT
28188 && part != ON_MODE_LINE
28189 && part != ON_HEADER_LINE))
28190 clear_mouse_face (hlinfo);
28191
28192 /* Not on a window -> return. */
28193 if (!WINDOWP (window))
28194 return;
28195
28196 /* Reset help_echo_string. It will get recomputed below. */
28197 help_echo_string = Qnil;
28198
28199 /* Convert to window-relative pixel coordinates. */
28200 w = XWINDOW (window);
28201 frame_to_window_pixel_xy (w, &x, &y);
28202
28203 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28204 /* Handle tool-bar window differently since it doesn't display a
28205 buffer. */
28206 if (EQ (window, f->tool_bar_window))
28207 {
28208 note_tool_bar_highlight (f, x, y);
28209 return;
28210 }
28211 #endif
28212
28213 /* Mouse is on the mode, header line or margin? */
28214 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28215 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28216 {
28217 note_mode_line_or_margin_highlight (window, x, y, part);
28218 return;
28219 }
28220
28221 #ifdef HAVE_WINDOW_SYSTEM
28222 if (part == ON_VERTICAL_BORDER)
28223 {
28224 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28225 help_echo_string = build_string ("drag-mouse-1: resize");
28226 }
28227 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28228 || part == ON_SCROLL_BAR)
28229 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28230 else
28231 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28232 #endif
28233
28234 /* Are we in a window whose display is up to date?
28235 And verify the buffer's text has not changed. */
28236 b = XBUFFER (w->contents);
28237 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28238 {
28239 int hpos, vpos, dx, dy, area = LAST_AREA;
28240 ptrdiff_t pos;
28241 struct glyph *glyph;
28242 Lisp_Object object;
28243 Lisp_Object mouse_face = Qnil, position;
28244 Lisp_Object *overlay_vec = NULL;
28245 ptrdiff_t i, noverlays;
28246 struct buffer *obuf;
28247 ptrdiff_t obegv, ozv;
28248 int same_region;
28249
28250 /* Find the glyph under X/Y. */
28251 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28252
28253 #ifdef HAVE_WINDOW_SYSTEM
28254 /* Look for :pointer property on image. */
28255 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28256 {
28257 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28258 if (img != NULL && IMAGEP (img->spec))
28259 {
28260 Lisp_Object image_map, hotspot;
28261 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28262 !NILP (image_map))
28263 && (hotspot = find_hot_spot (image_map,
28264 glyph->slice.img.x + dx,
28265 glyph->slice.img.y + dy),
28266 CONSP (hotspot))
28267 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28268 {
28269 Lisp_Object plist;
28270
28271 /* Could check XCAR (hotspot) to see if we enter/leave
28272 this hot-spot.
28273 If so, we could look for mouse-enter, mouse-leave
28274 properties in PLIST (and do something...). */
28275 hotspot = XCDR (hotspot);
28276 if (CONSP (hotspot)
28277 && (plist = XCAR (hotspot), CONSP (plist)))
28278 {
28279 pointer = Fplist_get (plist, Qpointer);
28280 if (NILP (pointer))
28281 pointer = Qhand;
28282 help_echo_string = Fplist_get (plist, Qhelp_echo);
28283 if (!NILP (help_echo_string))
28284 {
28285 help_echo_window = window;
28286 help_echo_object = glyph->object;
28287 help_echo_pos = glyph->charpos;
28288 }
28289 }
28290 }
28291 if (NILP (pointer))
28292 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28293 }
28294 }
28295 #endif /* HAVE_WINDOW_SYSTEM */
28296
28297 /* Clear mouse face if X/Y not over text. */
28298 if (glyph == NULL
28299 || area != TEXT_AREA
28300 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28301 /* Glyph's OBJECT is an integer for glyphs inserted by the
28302 display engine for its internal purposes, like truncation
28303 and continuation glyphs and blanks beyond the end of
28304 line's text on text terminals. If we are over such a
28305 glyph, we are not over any text. */
28306 || INTEGERP (glyph->object)
28307 /* R2L rows have a stretch glyph at their front, which
28308 stands for no text, whereas L2R rows have no glyphs at
28309 all beyond the end of text. Treat such stretch glyphs
28310 like we do with NULL glyphs in L2R rows. */
28311 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28312 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28313 && glyph->type == STRETCH_GLYPH
28314 && glyph->avoid_cursor_p))
28315 {
28316 if (clear_mouse_face (hlinfo))
28317 cursor = No_Cursor;
28318 #ifdef HAVE_WINDOW_SYSTEM
28319 if (FRAME_WINDOW_P (f) && NILP (pointer))
28320 {
28321 if (area != TEXT_AREA)
28322 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28323 else
28324 pointer = Vvoid_text_area_pointer;
28325 }
28326 #endif
28327 goto set_cursor;
28328 }
28329
28330 pos = glyph->charpos;
28331 object = glyph->object;
28332 if (!STRINGP (object) && !BUFFERP (object))
28333 goto set_cursor;
28334
28335 /* If we get an out-of-range value, return now; avoid an error. */
28336 if (BUFFERP (object) && pos > BUF_Z (b))
28337 goto set_cursor;
28338
28339 /* Make the window's buffer temporarily current for
28340 overlays_at and compute_char_face. */
28341 obuf = current_buffer;
28342 current_buffer = b;
28343 obegv = BEGV;
28344 ozv = ZV;
28345 BEGV = BEG;
28346 ZV = Z;
28347
28348 /* Is this char mouse-active or does it have help-echo? */
28349 position = make_number (pos);
28350
28351 if (BUFFERP (object))
28352 {
28353 /* Put all the overlays we want in a vector in overlay_vec. */
28354 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28355 /* Sort overlays into increasing priority order. */
28356 noverlays = sort_overlays (overlay_vec, noverlays, w);
28357 }
28358 else
28359 noverlays = 0;
28360
28361 if (NILP (Vmouse_highlight))
28362 {
28363 clear_mouse_face (hlinfo);
28364 goto check_help_echo;
28365 }
28366
28367 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28368
28369 if (same_region)
28370 cursor = No_Cursor;
28371
28372 /* Check mouse-face highlighting. */
28373 if (! same_region
28374 /* If there exists an overlay with mouse-face overlapping
28375 the one we are currently highlighting, we have to
28376 check if we enter the overlapping overlay, and then
28377 highlight only that. */
28378 || (OVERLAYP (hlinfo->mouse_face_overlay)
28379 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28380 {
28381 /* Find the highest priority overlay with a mouse-face. */
28382 Lisp_Object overlay = Qnil;
28383 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28384 {
28385 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28386 if (!NILP (mouse_face))
28387 overlay = overlay_vec[i];
28388 }
28389
28390 /* If we're highlighting the same overlay as before, there's
28391 no need to do that again. */
28392 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28393 goto check_help_echo;
28394 hlinfo->mouse_face_overlay = overlay;
28395
28396 /* Clear the display of the old active region, if any. */
28397 if (clear_mouse_face (hlinfo))
28398 cursor = No_Cursor;
28399
28400 /* If no overlay applies, get a text property. */
28401 if (NILP (overlay))
28402 mouse_face = Fget_text_property (position, Qmouse_face, object);
28403
28404 /* Next, compute the bounds of the mouse highlighting and
28405 display it. */
28406 if (!NILP (mouse_face) && STRINGP (object))
28407 {
28408 /* The mouse-highlighting comes from a display string
28409 with a mouse-face. */
28410 Lisp_Object s, e;
28411 ptrdiff_t ignore;
28412
28413 s = Fprevious_single_property_change
28414 (make_number (pos + 1), Qmouse_face, object, Qnil);
28415 e = Fnext_single_property_change
28416 (position, Qmouse_face, object, Qnil);
28417 if (NILP (s))
28418 s = make_number (0);
28419 if (NILP (e))
28420 e = make_number (SCHARS (object));
28421 mouse_face_from_string_pos (w, hlinfo, object,
28422 XINT (s), XINT (e));
28423 hlinfo->mouse_face_past_end = 0;
28424 hlinfo->mouse_face_window = window;
28425 hlinfo->mouse_face_face_id
28426 = face_at_string_position (w, object, pos, 0, &ignore,
28427 glyph->face_id, 1);
28428 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28429 cursor = No_Cursor;
28430 }
28431 else
28432 {
28433 /* The mouse-highlighting, if any, comes from an overlay
28434 or text property in the buffer. */
28435 Lisp_Object buffer IF_LINT (= Qnil);
28436 Lisp_Object disp_string IF_LINT (= Qnil);
28437
28438 if (STRINGP (object))
28439 {
28440 /* If we are on a display string with no mouse-face,
28441 check if the text under it has one. */
28442 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28443 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28444 pos = string_buffer_position (object, start);
28445 if (pos > 0)
28446 {
28447 mouse_face = get_char_property_and_overlay
28448 (make_number (pos), Qmouse_face, w->contents, &overlay);
28449 buffer = w->contents;
28450 disp_string = object;
28451 }
28452 }
28453 else
28454 {
28455 buffer = object;
28456 disp_string = Qnil;
28457 }
28458
28459 if (!NILP (mouse_face))
28460 {
28461 Lisp_Object before, after;
28462 Lisp_Object before_string, after_string;
28463 /* To correctly find the limits of mouse highlight
28464 in a bidi-reordered buffer, we must not use the
28465 optimization of limiting the search in
28466 previous-single-property-change and
28467 next-single-property-change, because
28468 rows_from_pos_range needs the real start and end
28469 positions to DTRT in this case. That's because
28470 the first row visible in a window does not
28471 necessarily display the character whose position
28472 is the smallest. */
28473 Lisp_Object lim1
28474 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28475 ? Fmarker_position (w->start)
28476 : Qnil;
28477 Lisp_Object lim2
28478 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28479 ? make_number (BUF_Z (XBUFFER (buffer))
28480 - w->window_end_pos)
28481 : Qnil;
28482
28483 if (NILP (overlay))
28484 {
28485 /* Handle the text property case. */
28486 before = Fprevious_single_property_change
28487 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28488 after = Fnext_single_property_change
28489 (make_number (pos), Qmouse_face, buffer, lim2);
28490 before_string = after_string = Qnil;
28491 }
28492 else
28493 {
28494 /* Handle the overlay case. */
28495 before = Foverlay_start (overlay);
28496 after = Foverlay_end (overlay);
28497 before_string = Foverlay_get (overlay, Qbefore_string);
28498 after_string = Foverlay_get (overlay, Qafter_string);
28499
28500 if (!STRINGP (before_string)) before_string = Qnil;
28501 if (!STRINGP (after_string)) after_string = Qnil;
28502 }
28503
28504 mouse_face_from_buffer_pos (window, hlinfo, pos,
28505 NILP (before)
28506 ? 1
28507 : XFASTINT (before),
28508 NILP (after)
28509 ? BUF_Z (XBUFFER (buffer))
28510 : XFASTINT (after),
28511 before_string, after_string,
28512 disp_string);
28513 cursor = No_Cursor;
28514 }
28515 }
28516 }
28517
28518 check_help_echo:
28519
28520 /* Look for a `help-echo' property. */
28521 if (NILP (help_echo_string)) {
28522 Lisp_Object help, overlay;
28523
28524 /* Check overlays first. */
28525 help = overlay = Qnil;
28526 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28527 {
28528 overlay = overlay_vec[i];
28529 help = Foverlay_get (overlay, Qhelp_echo);
28530 }
28531
28532 if (!NILP (help))
28533 {
28534 help_echo_string = help;
28535 help_echo_window = window;
28536 help_echo_object = overlay;
28537 help_echo_pos = pos;
28538 }
28539 else
28540 {
28541 Lisp_Object obj = glyph->object;
28542 ptrdiff_t charpos = glyph->charpos;
28543
28544 /* Try text properties. */
28545 if (STRINGP (obj)
28546 && charpos >= 0
28547 && charpos < SCHARS (obj))
28548 {
28549 help = Fget_text_property (make_number (charpos),
28550 Qhelp_echo, obj);
28551 if (NILP (help))
28552 {
28553 /* If the string itself doesn't specify a help-echo,
28554 see if the buffer text ``under'' it does. */
28555 struct glyph_row *r
28556 = MATRIX_ROW (w->current_matrix, vpos);
28557 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28558 ptrdiff_t p = string_buffer_position (obj, start);
28559 if (p > 0)
28560 {
28561 help = Fget_char_property (make_number (p),
28562 Qhelp_echo, w->contents);
28563 if (!NILP (help))
28564 {
28565 charpos = p;
28566 obj = w->contents;
28567 }
28568 }
28569 }
28570 }
28571 else if (BUFFERP (obj)
28572 && charpos >= BEGV
28573 && charpos < ZV)
28574 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28575 obj);
28576
28577 if (!NILP (help))
28578 {
28579 help_echo_string = help;
28580 help_echo_window = window;
28581 help_echo_object = obj;
28582 help_echo_pos = charpos;
28583 }
28584 }
28585 }
28586
28587 #ifdef HAVE_WINDOW_SYSTEM
28588 /* Look for a `pointer' property. */
28589 if (FRAME_WINDOW_P (f) && NILP (pointer))
28590 {
28591 /* Check overlays first. */
28592 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28593 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28594
28595 if (NILP (pointer))
28596 {
28597 Lisp_Object obj = glyph->object;
28598 ptrdiff_t charpos = glyph->charpos;
28599
28600 /* Try text properties. */
28601 if (STRINGP (obj)
28602 && charpos >= 0
28603 && charpos < SCHARS (obj))
28604 {
28605 pointer = Fget_text_property (make_number (charpos),
28606 Qpointer, obj);
28607 if (NILP (pointer))
28608 {
28609 /* If the string itself doesn't specify a pointer,
28610 see if the buffer text ``under'' it does. */
28611 struct glyph_row *r
28612 = MATRIX_ROW (w->current_matrix, vpos);
28613 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28614 ptrdiff_t p = string_buffer_position (obj, start);
28615 if (p > 0)
28616 pointer = Fget_char_property (make_number (p),
28617 Qpointer, w->contents);
28618 }
28619 }
28620 else if (BUFFERP (obj)
28621 && charpos >= BEGV
28622 && charpos < ZV)
28623 pointer = Fget_text_property (make_number (charpos),
28624 Qpointer, obj);
28625 }
28626 }
28627 #endif /* HAVE_WINDOW_SYSTEM */
28628
28629 BEGV = obegv;
28630 ZV = ozv;
28631 current_buffer = obuf;
28632 }
28633
28634 set_cursor:
28635
28636 #ifdef HAVE_WINDOW_SYSTEM
28637 if (FRAME_WINDOW_P (f))
28638 define_frame_cursor1 (f, cursor, pointer);
28639 #else
28640 /* This is here to prevent a compiler error, about "label at end of
28641 compound statement". */
28642 return;
28643 #endif
28644 }
28645
28646
28647 /* EXPORT for RIF:
28648 Clear any mouse-face on window W. This function is part of the
28649 redisplay interface, and is called from try_window_id and similar
28650 functions to ensure the mouse-highlight is off. */
28651
28652 void
28653 x_clear_window_mouse_face (struct window *w)
28654 {
28655 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28656 Lisp_Object window;
28657
28658 block_input ();
28659 XSETWINDOW (window, w);
28660 if (EQ (window, hlinfo->mouse_face_window))
28661 clear_mouse_face (hlinfo);
28662 unblock_input ();
28663 }
28664
28665
28666 /* EXPORT:
28667 Just discard the mouse face information for frame F, if any.
28668 This is used when the size of F is changed. */
28669
28670 void
28671 cancel_mouse_face (struct frame *f)
28672 {
28673 Lisp_Object window;
28674 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28675
28676 window = hlinfo->mouse_face_window;
28677 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28678 reset_mouse_highlight (hlinfo);
28679 }
28680
28681
28682 \f
28683 /***********************************************************************
28684 Exposure Events
28685 ***********************************************************************/
28686
28687 #ifdef HAVE_WINDOW_SYSTEM
28688
28689 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28690 which intersects rectangle R. R is in window-relative coordinates. */
28691
28692 static void
28693 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28694 enum glyph_row_area area)
28695 {
28696 struct glyph *first = row->glyphs[area];
28697 struct glyph *end = row->glyphs[area] + row->used[area];
28698 struct glyph *last;
28699 int first_x, start_x, x;
28700
28701 if (area == TEXT_AREA && row->fill_line_p)
28702 /* If row extends face to end of line write the whole line. */
28703 draw_glyphs (w, 0, row, area,
28704 0, row->used[area],
28705 DRAW_NORMAL_TEXT, 0);
28706 else
28707 {
28708 /* Set START_X to the window-relative start position for drawing glyphs of
28709 AREA. The first glyph of the text area can be partially visible.
28710 The first glyphs of other areas cannot. */
28711 start_x = window_box_left_offset (w, area);
28712 x = start_x;
28713 if (area == TEXT_AREA)
28714 x += row->x;
28715
28716 /* Find the first glyph that must be redrawn. */
28717 while (first < end
28718 && x + first->pixel_width < r->x)
28719 {
28720 x += first->pixel_width;
28721 ++first;
28722 }
28723
28724 /* Find the last one. */
28725 last = first;
28726 first_x = x;
28727 while (last < end
28728 && x < r->x + r->width)
28729 {
28730 x += last->pixel_width;
28731 ++last;
28732 }
28733
28734 /* Repaint. */
28735 if (last > first)
28736 draw_glyphs (w, first_x - start_x, row, area,
28737 first - row->glyphs[area], last - row->glyphs[area],
28738 DRAW_NORMAL_TEXT, 0);
28739 }
28740 }
28741
28742
28743 /* Redraw the parts of the glyph row ROW on window W intersecting
28744 rectangle R. R is in window-relative coordinates. Value is
28745 non-zero if mouse-face was overwritten. */
28746
28747 static int
28748 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28749 {
28750 eassert (row->enabled_p);
28751
28752 if (row->mode_line_p || w->pseudo_window_p)
28753 draw_glyphs (w, 0, row, TEXT_AREA,
28754 0, row->used[TEXT_AREA],
28755 DRAW_NORMAL_TEXT, 0);
28756 else
28757 {
28758 if (row->used[LEFT_MARGIN_AREA])
28759 expose_area (w, row, r, LEFT_MARGIN_AREA);
28760 if (row->used[TEXT_AREA])
28761 expose_area (w, row, r, TEXT_AREA);
28762 if (row->used[RIGHT_MARGIN_AREA])
28763 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28764 draw_row_fringe_bitmaps (w, row);
28765 }
28766
28767 return row->mouse_face_p;
28768 }
28769
28770
28771 /* Redraw those parts of glyphs rows during expose event handling that
28772 overlap other rows. Redrawing of an exposed line writes over parts
28773 of lines overlapping that exposed line; this function fixes that.
28774
28775 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28776 row in W's current matrix that is exposed and overlaps other rows.
28777 LAST_OVERLAPPING_ROW is the last such row. */
28778
28779 static void
28780 expose_overlaps (struct window *w,
28781 struct glyph_row *first_overlapping_row,
28782 struct glyph_row *last_overlapping_row,
28783 XRectangle *r)
28784 {
28785 struct glyph_row *row;
28786
28787 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28788 if (row->overlapping_p)
28789 {
28790 eassert (row->enabled_p && !row->mode_line_p);
28791
28792 row->clip = r;
28793 if (row->used[LEFT_MARGIN_AREA])
28794 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28795
28796 if (row->used[TEXT_AREA])
28797 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28798
28799 if (row->used[RIGHT_MARGIN_AREA])
28800 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28801 row->clip = NULL;
28802 }
28803 }
28804
28805
28806 /* Return non-zero if W's cursor intersects rectangle R. */
28807
28808 static int
28809 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28810 {
28811 XRectangle cr, result;
28812 struct glyph *cursor_glyph;
28813 struct glyph_row *row;
28814
28815 if (w->phys_cursor.vpos >= 0
28816 && w->phys_cursor.vpos < w->current_matrix->nrows
28817 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28818 row->enabled_p)
28819 && row->cursor_in_fringe_p)
28820 {
28821 /* Cursor is in the fringe. */
28822 cr.x = window_box_right_offset (w,
28823 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28824 ? RIGHT_MARGIN_AREA
28825 : TEXT_AREA));
28826 cr.y = row->y;
28827 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28828 cr.height = row->height;
28829 return x_intersect_rectangles (&cr, r, &result);
28830 }
28831
28832 cursor_glyph = get_phys_cursor_glyph (w);
28833 if (cursor_glyph)
28834 {
28835 /* r is relative to W's box, but w->phys_cursor.x is relative
28836 to left edge of W's TEXT area. Adjust it. */
28837 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28838 cr.y = w->phys_cursor.y;
28839 cr.width = cursor_glyph->pixel_width;
28840 cr.height = w->phys_cursor_height;
28841 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28842 I assume the effect is the same -- and this is portable. */
28843 return x_intersect_rectangles (&cr, r, &result);
28844 }
28845 /* If we don't understand the format, pretend we're not in the hot-spot. */
28846 return 0;
28847 }
28848
28849
28850 /* EXPORT:
28851 Draw a vertical window border to the right of window W if W doesn't
28852 have vertical scroll bars. */
28853
28854 void
28855 x_draw_vertical_border (struct window *w)
28856 {
28857 struct frame *f = XFRAME (WINDOW_FRAME (w));
28858
28859 /* We could do better, if we knew what type of scroll-bar the adjacent
28860 windows (on either side) have... But we don't :-(
28861 However, I think this works ok. ++KFS 2003-04-25 */
28862
28863 /* Redraw borders between horizontally adjacent windows. Don't
28864 do it for frames with vertical scroll bars because either the
28865 right scroll bar of a window, or the left scroll bar of its
28866 neighbor will suffice as a border. */
28867 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28868 return;
28869
28870 /* Note: It is necessary to redraw both the left and the right
28871 borders, for when only this single window W is being
28872 redisplayed. */
28873 if (!WINDOW_RIGHTMOST_P (w)
28874 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28875 {
28876 int x0, x1, y0, y1;
28877
28878 window_box_edges (w, &x0, &y0, &x1, &y1);
28879 y1 -= 1;
28880
28881 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28882 x1 -= 1;
28883
28884 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28885 }
28886 if (!WINDOW_LEFTMOST_P (w)
28887 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28888 {
28889 int x0, x1, y0, y1;
28890
28891 window_box_edges (w, &x0, &y0, &x1, &y1);
28892 y1 -= 1;
28893
28894 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28895 x0 -= 1;
28896
28897 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28898 }
28899 }
28900
28901
28902 /* Redraw the part of window W intersection rectangle FR. Pixel
28903 coordinates in FR are frame-relative. Call this function with
28904 input blocked. Value is non-zero if the exposure overwrites
28905 mouse-face. */
28906
28907 static int
28908 expose_window (struct window *w, XRectangle *fr)
28909 {
28910 struct frame *f = XFRAME (w->frame);
28911 XRectangle wr, r;
28912 int mouse_face_overwritten_p = 0;
28913
28914 /* If window is not yet fully initialized, do nothing. This can
28915 happen when toolkit scroll bars are used and a window is split.
28916 Reconfiguring the scroll bar will generate an expose for a newly
28917 created window. */
28918 if (w->current_matrix == NULL)
28919 return 0;
28920
28921 /* When we're currently updating the window, display and current
28922 matrix usually don't agree. Arrange for a thorough display
28923 later. */
28924 if (w->must_be_updated_p)
28925 {
28926 SET_FRAME_GARBAGED (f);
28927 return 0;
28928 }
28929
28930 /* Frame-relative pixel rectangle of W. */
28931 wr.x = WINDOW_LEFT_EDGE_X (w);
28932 wr.y = WINDOW_TOP_EDGE_Y (w);
28933 wr.width = WINDOW_TOTAL_WIDTH (w);
28934 wr.height = WINDOW_TOTAL_HEIGHT (w);
28935
28936 if (x_intersect_rectangles (fr, &wr, &r))
28937 {
28938 int yb = window_text_bottom_y (w);
28939 struct glyph_row *row;
28940 int cursor_cleared_p, phys_cursor_on_p;
28941 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28942
28943 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28944 r.x, r.y, r.width, r.height));
28945
28946 /* Convert to window coordinates. */
28947 r.x -= WINDOW_LEFT_EDGE_X (w);
28948 r.y -= WINDOW_TOP_EDGE_Y (w);
28949
28950 /* Turn off the cursor. */
28951 if (!w->pseudo_window_p
28952 && phys_cursor_in_rect_p (w, &r))
28953 {
28954 x_clear_cursor (w);
28955 cursor_cleared_p = 1;
28956 }
28957 else
28958 cursor_cleared_p = 0;
28959
28960 /* If the row containing the cursor extends face to end of line,
28961 then expose_area might overwrite the cursor outside the
28962 rectangle and thus notice_overwritten_cursor might clear
28963 w->phys_cursor_on_p. We remember the original value and
28964 check later if it is changed. */
28965 phys_cursor_on_p = w->phys_cursor_on_p;
28966
28967 /* Update lines intersecting rectangle R. */
28968 first_overlapping_row = last_overlapping_row = NULL;
28969 for (row = w->current_matrix->rows;
28970 row->enabled_p;
28971 ++row)
28972 {
28973 int y0 = row->y;
28974 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28975
28976 if ((y0 >= r.y && y0 < r.y + r.height)
28977 || (y1 > r.y && y1 < r.y + r.height)
28978 || (r.y >= y0 && r.y < y1)
28979 || (r.y + r.height > y0 && r.y + r.height < y1))
28980 {
28981 /* A header line may be overlapping, but there is no need
28982 to fix overlapping areas for them. KFS 2005-02-12 */
28983 if (row->overlapping_p && !row->mode_line_p)
28984 {
28985 if (first_overlapping_row == NULL)
28986 first_overlapping_row = row;
28987 last_overlapping_row = row;
28988 }
28989
28990 row->clip = fr;
28991 if (expose_line (w, row, &r))
28992 mouse_face_overwritten_p = 1;
28993 row->clip = NULL;
28994 }
28995 else if (row->overlapping_p)
28996 {
28997 /* We must redraw a row overlapping the exposed area. */
28998 if (y0 < r.y
28999 ? y0 + row->phys_height > r.y
29000 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29001 {
29002 if (first_overlapping_row == NULL)
29003 first_overlapping_row = row;
29004 last_overlapping_row = row;
29005 }
29006 }
29007
29008 if (y1 >= yb)
29009 break;
29010 }
29011
29012 /* Display the mode line if there is one. */
29013 if (WINDOW_WANTS_MODELINE_P (w)
29014 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29015 row->enabled_p)
29016 && row->y < r.y + r.height)
29017 {
29018 if (expose_line (w, row, &r))
29019 mouse_face_overwritten_p = 1;
29020 }
29021
29022 if (!w->pseudo_window_p)
29023 {
29024 /* Fix the display of overlapping rows. */
29025 if (first_overlapping_row)
29026 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29027 fr);
29028
29029 /* Draw border between windows. */
29030 x_draw_vertical_border (w);
29031
29032 /* Turn the cursor on again. */
29033 if (cursor_cleared_p
29034 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29035 update_window_cursor (w, 1);
29036 }
29037 }
29038
29039 return mouse_face_overwritten_p;
29040 }
29041
29042
29043
29044 /* Redraw (parts) of all windows in the window tree rooted at W that
29045 intersect R. R contains frame pixel coordinates. Value is
29046 non-zero if the exposure overwrites mouse-face. */
29047
29048 static int
29049 expose_window_tree (struct window *w, XRectangle *r)
29050 {
29051 struct frame *f = XFRAME (w->frame);
29052 int mouse_face_overwritten_p = 0;
29053
29054 while (w && !FRAME_GARBAGED_P (f))
29055 {
29056 if (WINDOWP (w->contents))
29057 mouse_face_overwritten_p
29058 |= expose_window_tree (XWINDOW (w->contents), r);
29059 else
29060 mouse_face_overwritten_p |= expose_window (w, r);
29061
29062 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29063 }
29064
29065 return mouse_face_overwritten_p;
29066 }
29067
29068
29069 /* EXPORT:
29070 Redisplay an exposed area of frame F. X and Y are the upper-left
29071 corner of the exposed rectangle. W and H are width and height of
29072 the exposed area. All are pixel values. W or H zero means redraw
29073 the entire frame. */
29074
29075 void
29076 expose_frame (struct frame *f, int x, int y, int w, int h)
29077 {
29078 XRectangle r;
29079 int mouse_face_overwritten_p = 0;
29080
29081 TRACE ((stderr, "expose_frame "));
29082
29083 /* No need to redraw if frame will be redrawn soon. */
29084 if (FRAME_GARBAGED_P (f))
29085 {
29086 TRACE ((stderr, " garbaged\n"));
29087 return;
29088 }
29089
29090 /* If basic faces haven't been realized yet, there is no point in
29091 trying to redraw anything. This can happen when we get an expose
29092 event while Emacs is starting, e.g. by moving another window. */
29093 if (FRAME_FACE_CACHE (f) == NULL
29094 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29095 {
29096 TRACE ((stderr, " no faces\n"));
29097 return;
29098 }
29099
29100 if (w == 0 || h == 0)
29101 {
29102 r.x = r.y = 0;
29103 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29104 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29105 }
29106 else
29107 {
29108 r.x = x;
29109 r.y = y;
29110 r.width = w;
29111 r.height = h;
29112 }
29113
29114 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29115 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29116
29117 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29118 if (WINDOWP (f->tool_bar_window))
29119 mouse_face_overwritten_p
29120 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29121 #endif
29122
29123 #ifdef HAVE_X_WINDOWS
29124 #ifndef MSDOS
29125 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29126 if (WINDOWP (f->menu_bar_window))
29127 mouse_face_overwritten_p
29128 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29129 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29130 #endif
29131 #endif
29132
29133 /* Some window managers support a focus-follows-mouse style with
29134 delayed raising of frames. Imagine a partially obscured frame,
29135 and moving the mouse into partially obscured mouse-face on that
29136 frame. The visible part of the mouse-face will be highlighted,
29137 then the WM raises the obscured frame. With at least one WM, KDE
29138 2.1, Emacs is not getting any event for the raising of the frame
29139 (even tried with SubstructureRedirectMask), only Expose events.
29140 These expose events will draw text normally, i.e. not
29141 highlighted. Which means we must redo the highlight here.
29142 Subsume it under ``we love X''. --gerd 2001-08-15 */
29143 /* Included in Windows version because Windows most likely does not
29144 do the right thing if any third party tool offers
29145 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29146 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29147 {
29148 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29149 if (f == hlinfo->mouse_face_mouse_frame)
29150 {
29151 int mouse_x = hlinfo->mouse_face_mouse_x;
29152 int mouse_y = hlinfo->mouse_face_mouse_y;
29153 clear_mouse_face (hlinfo);
29154 note_mouse_highlight (f, mouse_x, mouse_y);
29155 }
29156 }
29157 }
29158
29159
29160 /* EXPORT:
29161 Determine the intersection of two rectangles R1 and R2. Return
29162 the intersection in *RESULT. Value is non-zero if RESULT is not
29163 empty. */
29164
29165 int
29166 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29167 {
29168 XRectangle *left, *right;
29169 XRectangle *upper, *lower;
29170 int intersection_p = 0;
29171
29172 /* Rearrange so that R1 is the left-most rectangle. */
29173 if (r1->x < r2->x)
29174 left = r1, right = r2;
29175 else
29176 left = r2, right = r1;
29177
29178 /* X0 of the intersection is right.x0, if this is inside R1,
29179 otherwise there is no intersection. */
29180 if (right->x <= left->x + left->width)
29181 {
29182 result->x = right->x;
29183
29184 /* The right end of the intersection is the minimum of
29185 the right ends of left and right. */
29186 result->width = (min (left->x + left->width, right->x + right->width)
29187 - result->x);
29188
29189 /* Same game for Y. */
29190 if (r1->y < r2->y)
29191 upper = r1, lower = r2;
29192 else
29193 upper = r2, lower = r1;
29194
29195 /* The upper end of the intersection is lower.y0, if this is inside
29196 of upper. Otherwise, there is no intersection. */
29197 if (lower->y <= upper->y + upper->height)
29198 {
29199 result->y = lower->y;
29200
29201 /* The lower end of the intersection is the minimum of the lower
29202 ends of upper and lower. */
29203 result->height = (min (lower->y + lower->height,
29204 upper->y + upper->height)
29205 - result->y);
29206 intersection_p = 1;
29207 }
29208 }
29209
29210 return intersection_p;
29211 }
29212
29213 #endif /* HAVE_WINDOW_SYSTEM */
29214
29215 \f
29216 /***********************************************************************
29217 Initialization
29218 ***********************************************************************/
29219
29220 void
29221 syms_of_xdisp (void)
29222 {
29223 Vwith_echo_area_save_vector = Qnil;
29224 staticpro (&Vwith_echo_area_save_vector);
29225
29226 Vmessage_stack = Qnil;
29227 staticpro (&Vmessage_stack);
29228
29229 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29230 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29231
29232 message_dolog_marker1 = Fmake_marker ();
29233 staticpro (&message_dolog_marker1);
29234 message_dolog_marker2 = Fmake_marker ();
29235 staticpro (&message_dolog_marker2);
29236 message_dolog_marker3 = Fmake_marker ();
29237 staticpro (&message_dolog_marker3);
29238
29239 #ifdef GLYPH_DEBUG
29240 defsubr (&Sdump_frame_glyph_matrix);
29241 defsubr (&Sdump_glyph_matrix);
29242 defsubr (&Sdump_glyph_row);
29243 defsubr (&Sdump_tool_bar_row);
29244 defsubr (&Strace_redisplay);
29245 defsubr (&Strace_to_stderr);
29246 #endif
29247 #ifdef HAVE_WINDOW_SYSTEM
29248 defsubr (&Stool_bar_lines_needed);
29249 defsubr (&Slookup_image_map);
29250 #endif
29251 defsubr (&Sline_pixel_height);
29252 defsubr (&Sformat_mode_line);
29253 defsubr (&Sinvisible_p);
29254 defsubr (&Scurrent_bidi_paragraph_direction);
29255 defsubr (&Smove_point_visually);
29256
29257 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29258 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29259 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29260 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29261 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29262 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29263 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29264 DEFSYM (Qeval, "eval");
29265 DEFSYM (QCdata, ":data");
29266 DEFSYM (Qdisplay, "display");
29267 DEFSYM (Qspace_width, "space-width");
29268 DEFSYM (Qraise, "raise");
29269 DEFSYM (Qslice, "slice");
29270 DEFSYM (Qspace, "space");
29271 DEFSYM (Qmargin, "margin");
29272 DEFSYM (Qpointer, "pointer");
29273 DEFSYM (Qleft_margin, "left-margin");
29274 DEFSYM (Qright_margin, "right-margin");
29275 DEFSYM (Qcenter, "center");
29276 DEFSYM (Qline_height, "line-height");
29277 DEFSYM (QCalign_to, ":align-to");
29278 DEFSYM (QCrelative_width, ":relative-width");
29279 DEFSYM (QCrelative_height, ":relative-height");
29280 DEFSYM (QCeval, ":eval");
29281 DEFSYM (QCpropertize, ":propertize");
29282 DEFSYM (QCfile, ":file");
29283 DEFSYM (Qfontified, "fontified");
29284 DEFSYM (Qfontification_functions, "fontification-functions");
29285 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29286 DEFSYM (Qescape_glyph, "escape-glyph");
29287 DEFSYM (Qnobreak_space, "nobreak-space");
29288 DEFSYM (Qimage, "image");
29289 DEFSYM (Qtext, "text");
29290 DEFSYM (Qboth, "both");
29291 DEFSYM (Qboth_horiz, "both-horiz");
29292 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29293 DEFSYM (QCmap, ":map");
29294 DEFSYM (QCpointer, ":pointer");
29295 DEFSYM (Qrect, "rect");
29296 DEFSYM (Qcircle, "circle");
29297 DEFSYM (Qpoly, "poly");
29298 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29299 DEFSYM (Qgrow_only, "grow-only");
29300 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29301 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29302 DEFSYM (Qposition, "position");
29303 DEFSYM (Qbuffer_position, "buffer-position");
29304 DEFSYM (Qobject, "object");
29305 DEFSYM (Qbar, "bar");
29306 DEFSYM (Qhbar, "hbar");
29307 DEFSYM (Qbox, "box");
29308 DEFSYM (Qhollow, "hollow");
29309 DEFSYM (Qhand, "hand");
29310 DEFSYM (Qarrow, "arrow");
29311 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29312
29313 list_of_error = list1 (list2 (intern_c_string ("error"),
29314 intern_c_string ("void-variable")));
29315 staticpro (&list_of_error);
29316
29317 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29318 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29319 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29320 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29321
29322 echo_buffer[0] = echo_buffer[1] = Qnil;
29323 staticpro (&echo_buffer[0]);
29324 staticpro (&echo_buffer[1]);
29325
29326 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29327 staticpro (&echo_area_buffer[0]);
29328 staticpro (&echo_area_buffer[1]);
29329
29330 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29331 staticpro (&Vmessages_buffer_name);
29332
29333 mode_line_proptrans_alist = Qnil;
29334 staticpro (&mode_line_proptrans_alist);
29335 mode_line_string_list = Qnil;
29336 staticpro (&mode_line_string_list);
29337 mode_line_string_face = Qnil;
29338 staticpro (&mode_line_string_face);
29339 mode_line_string_face_prop = Qnil;
29340 staticpro (&mode_line_string_face_prop);
29341 Vmode_line_unwind_vector = Qnil;
29342 staticpro (&Vmode_line_unwind_vector);
29343
29344 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29345
29346 help_echo_string = Qnil;
29347 staticpro (&help_echo_string);
29348 help_echo_object = Qnil;
29349 staticpro (&help_echo_object);
29350 help_echo_window = Qnil;
29351 staticpro (&help_echo_window);
29352 previous_help_echo_string = Qnil;
29353 staticpro (&previous_help_echo_string);
29354 help_echo_pos = -1;
29355
29356 DEFSYM (Qright_to_left, "right-to-left");
29357 DEFSYM (Qleft_to_right, "left-to-right");
29358
29359 #ifdef HAVE_WINDOW_SYSTEM
29360 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29361 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29362 For example, if a block cursor is over a tab, it will be drawn as
29363 wide as that tab on the display. */);
29364 x_stretch_cursor_p = 0;
29365 #endif
29366
29367 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29368 doc: /* Non-nil means highlight trailing whitespace.
29369 The face used for trailing whitespace is `trailing-whitespace'. */);
29370 Vshow_trailing_whitespace = Qnil;
29371
29372 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29373 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29374 If the value is t, Emacs highlights non-ASCII chars which have the
29375 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29376 or `escape-glyph' face respectively.
29377
29378 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29379 U+2011 (non-breaking hyphen) are affected.
29380
29381 Any other non-nil value means to display these characters as a escape
29382 glyph followed by an ordinary space or hyphen.
29383
29384 A value of nil means no special handling of these characters. */);
29385 Vnobreak_char_display = Qt;
29386
29387 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29388 doc: /* The pointer shape to show in void text areas.
29389 A value of nil means to show the text pointer. Other options are `arrow',
29390 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29391 Vvoid_text_area_pointer = Qarrow;
29392
29393 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29394 doc: /* Non-nil means don't actually do any redisplay.
29395 This is used for internal purposes. */);
29396 Vinhibit_redisplay = Qnil;
29397
29398 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29399 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29400 Vglobal_mode_string = Qnil;
29401
29402 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29403 doc: /* Marker for where to display an arrow on top of the buffer text.
29404 This must be the beginning of a line in order to work.
29405 See also `overlay-arrow-string'. */);
29406 Voverlay_arrow_position = Qnil;
29407
29408 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29409 doc: /* String to display as an arrow in non-window frames.
29410 See also `overlay-arrow-position'. */);
29411 Voverlay_arrow_string = build_pure_c_string ("=>");
29412
29413 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29414 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29415 The symbols on this list are examined during redisplay to determine
29416 where to display overlay arrows. */);
29417 Voverlay_arrow_variable_list
29418 = list1 (intern_c_string ("overlay-arrow-position"));
29419
29420 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29421 doc: /* The number of lines to try scrolling a window by when point moves out.
29422 If that fails to bring point back on frame, point is centered instead.
29423 If this is zero, point is always centered after it moves off frame.
29424 If you want scrolling to always be a line at a time, you should set
29425 `scroll-conservatively' to a large value rather than set this to 1. */);
29426
29427 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29428 doc: /* Scroll up to this many lines, to bring point back on screen.
29429 If point moves off-screen, redisplay will scroll by up to
29430 `scroll-conservatively' lines in order to bring point just barely
29431 onto the screen again. If that cannot be done, then redisplay
29432 recenters point as usual.
29433
29434 If the value is greater than 100, redisplay will never recenter point,
29435 but will always scroll just enough text to bring point into view, even
29436 if you move far away.
29437
29438 A value of zero means always recenter point if it moves off screen. */);
29439 scroll_conservatively = 0;
29440
29441 DEFVAR_INT ("scroll-margin", scroll_margin,
29442 doc: /* Number of lines of margin at the top and bottom of a window.
29443 Recenter the window whenever point gets within this many lines
29444 of the top or bottom of the window. */);
29445 scroll_margin = 0;
29446
29447 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29448 doc: /* Pixels per inch value for non-window system displays.
29449 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29450 Vdisplay_pixels_per_inch = make_float (72.0);
29451
29452 #ifdef GLYPH_DEBUG
29453 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29454 #endif
29455
29456 DEFVAR_LISP ("truncate-partial-width-windows",
29457 Vtruncate_partial_width_windows,
29458 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29459 For an integer value, truncate lines in each window narrower than the
29460 full frame width, provided the window width is less than that integer;
29461 otherwise, respect the value of `truncate-lines'.
29462
29463 For any other non-nil value, truncate lines in all windows that do
29464 not span the full frame width.
29465
29466 A value of nil means to respect the value of `truncate-lines'.
29467
29468 If `word-wrap' is enabled, you might want to reduce this. */);
29469 Vtruncate_partial_width_windows = make_number (50);
29470
29471 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29472 doc: /* Maximum buffer size for which line number should be displayed.
29473 If the buffer is bigger than this, the line number does not appear
29474 in the mode line. A value of nil means no limit. */);
29475 Vline_number_display_limit = Qnil;
29476
29477 DEFVAR_INT ("line-number-display-limit-width",
29478 line_number_display_limit_width,
29479 doc: /* Maximum line width (in characters) for line number display.
29480 If the average length of the lines near point is bigger than this, then the
29481 line number may be omitted from the mode line. */);
29482 line_number_display_limit_width = 200;
29483
29484 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29485 doc: /* Non-nil means highlight region even in nonselected windows. */);
29486 highlight_nonselected_windows = 0;
29487
29488 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29489 doc: /* Non-nil if more than one frame is visible on this display.
29490 Minibuffer-only frames don't count, but iconified frames do.
29491 This variable is not guaranteed to be accurate except while processing
29492 `frame-title-format' and `icon-title-format'. */);
29493
29494 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29495 doc: /* Template for displaying the title bar of visible frames.
29496 \(Assuming the window manager supports this feature.)
29497
29498 This variable has the same structure as `mode-line-format', except that
29499 the %c and %l constructs are ignored. It is used only on frames for
29500 which no explicit name has been set \(see `modify-frame-parameters'). */);
29501
29502 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29503 doc: /* Template for displaying the title bar of an iconified frame.
29504 \(Assuming the window manager supports this feature.)
29505 This variable has the same structure as `mode-line-format' (which see),
29506 and is used only on frames for which no explicit name has been set
29507 \(see `modify-frame-parameters'). */);
29508 Vicon_title_format
29509 = Vframe_title_format
29510 = listn (CONSTYPE_PURE, 3,
29511 intern_c_string ("multiple-frames"),
29512 build_pure_c_string ("%b"),
29513 listn (CONSTYPE_PURE, 4,
29514 empty_unibyte_string,
29515 intern_c_string ("invocation-name"),
29516 build_pure_c_string ("@"),
29517 intern_c_string ("system-name")));
29518
29519 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29520 doc: /* Maximum number of lines to keep in the message log buffer.
29521 If nil, disable message logging. If t, log messages but don't truncate
29522 the buffer when it becomes large. */);
29523 Vmessage_log_max = make_number (1000);
29524
29525 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29526 doc: /* Functions called before redisplay, if window sizes have changed.
29527 The value should be a list of functions that take one argument.
29528 Just before redisplay, for each frame, if any of its windows have changed
29529 size since the last redisplay, or have been split or deleted,
29530 all the functions in the list are called, with the frame as argument. */);
29531 Vwindow_size_change_functions = Qnil;
29532
29533 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29534 doc: /* List of functions to call before redisplaying a window with scrolling.
29535 Each function is called with two arguments, the window and its new
29536 display-start position. Note that these functions are also called by
29537 `set-window-buffer'. Also note that the value of `window-end' is not
29538 valid when these functions are called.
29539
29540 Warning: Do not use this feature to alter the way the window
29541 is scrolled. It is not designed for that, and such use probably won't
29542 work. */);
29543 Vwindow_scroll_functions = Qnil;
29544
29545 DEFVAR_LISP ("window-text-change-functions",
29546 Vwindow_text_change_functions,
29547 doc: /* Functions to call in redisplay when text in the window might change. */);
29548 Vwindow_text_change_functions = Qnil;
29549
29550 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29551 doc: /* Functions called when redisplay of a window reaches the end trigger.
29552 Each function is called with two arguments, the window and the end trigger value.
29553 See `set-window-redisplay-end-trigger'. */);
29554 Vredisplay_end_trigger_functions = Qnil;
29555
29556 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29557 doc: /* Non-nil means autoselect window with mouse pointer.
29558 If nil, do not autoselect windows.
29559 A positive number means delay autoselection by that many seconds: a
29560 window is autoselected only after the mouse has remained in that
29561 window for the duration of the delay.
29562 A negative number has a similar effect, but causes windows to be
29563 autoselected only after the mouse has stopped moving. \(Because of
29564 the way Emacs compares mouse events, you will occasionally wait twice
29565 that time before the window gets selected.\)
29566 Any other value means to autoselect window instantaneously when the
29567 mouse pointer enters it.
29568
29569 Autoselection selects the minibuffer only if it is active, and never
29570 unselects the minibuffer if it is active.
29571
29572 When customizing this variable make sure that the actual value of
29573 `focus-follows-mouse' matches the behavior of your window manager. */);
29574 Vmouse_autoselect_window = Qnil;
29575
29576 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29577 doc: /* Non-nil means automatically resize tool-bars.
29578 This dynamically changes the tool-bar's height to the minimum height
29579 that is needed to make all tool-bar items visible.
29580 If value is `grow-only', the tool-bar's height is only increased
29581 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29582 Vauto_resize_tool_bars = Qt;
29583
29584 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29585 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29586 auto_raise_tool_bar_buttons_p = 1;
29587
29588 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29589 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29590 make_cursor_line_fully_visible_p = 1;
29591
29592 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29593 doc: /* Border below tool-bar in pixels.
29594 If an integer, use it as the height of the border.
29595 If it is one of `internal-border-width' or `border-width', use the
29596 value of the corresponding frame parameter.
29597 Otherwise, no border is added below the tool-bar. */);
29598 Vtool_bar_border = Qinternal_border_width;
29599
29600 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29601 doc: /* Margin around tool-bar buttons in pixels.
29602 If an integer, use that for both horizontal and vertical margins.
29603 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29604 HORZ specifying the horizontal margin, and VERT specifying the
29605 vertical margin. */);
29606 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29607
29608 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29609 doc: /* Relief thickness of tool-bar buttons. */);
29610 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29611
29612 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29613 doc: /* Tool bar style to use.
29614 It can be one of
29615 image - show images only
29616 text - show text only
29617 both - show both, text below image
29618 both-horiz - show text to the right of the image
29619 text-image-horiz - show text to the left of the image
29620 any other - use system default or image if no system default.
29621
29622 This variable only affects the GTK+ toolkit version of Emacs. */);
29623 Vtool_bar_style = Qnil;
29624
29625 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29626 doc: /* Maximum number of characters a label can have to be shown.
29627 The tool bar style must also show labels for this to have any effect, see
29628 `tool-bar-style'. */);
29629 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29630
29631 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29632 doc: /* List of functions to call to fontify regions of text.
29633 Each function is called with one argument POS. Functions must
29634 fontify a region starting at POS in the current buffer, and give
29635 fontified regions the property `fontified'. */);
29636 Vfontification_functions = Qnil;
29637 Fmake_variable_buffer_local (Qfontification_functions);
29638
29639 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29640 unibyte_display_via_language_environment,
29641 doc: /* Non-nil means display unibyte text according to language environment.
29642 Specifically, this means that raw bytes in the range 160-255 decimal
29643 are displayed by converting them to the equivalent multibyte characters
29644 according to the current language environment. As a result, they are
29645 displayed according to the current fontset.
29646
29647 Note that this variable affects only how these bytes are displayed,
29648 but does not change the fact they are interpreted as raw bytes. */);
29649 unibyte_display_via_language_environment = 0;
29650
29651 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29652 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29653 If a float, it specifies a fraction of the mini-window frame's height.
29654 If an integer, it specifies a number of lines. */);
29655 Vmax_mini_window_height = make_float (0.25);
29656
29657 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29658 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29659 A value of nil means don't automatically resize mini-windows.
29660 A value of t means resize them to fit the text displayed in them.
29661 A value of `grow-only', the default, means let mini-windows grow only;
29662 they return to their normal size when the minibuffer is closed, or the
29663 echo area becomes empty. */);
29664 Vresize_mini_windows = Qgrow_only;
29665
29666 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29667 doc: /* Alist specifying how to blink the cursor off.
29668 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29669 `cursor-type' frame-parameter or variable equals ON-STATE,
29670 comparing using `equal', Emacs uses OFF-STATE to specify
29671 how to blink it off. ON-STATE and OFF-STATE are values for
29672 the `cursor-type' frame parameter.
29673
29674 If a frame's ON-STATE has no entry in this list,
29675 the frame's other specifications determine how to blink the cursor off. */);
29676 Vblink_cursor_alist = Qnil;
29677
29678 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29679 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29680 If non-nil, windows are automatically scrolled horizontally to make
29681 point visible. */);
29682 automatic_hscrolling_p = 1;
29683 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29684
29685 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29686 doc: /* How many columns away from the window edge point is allowed to get
29687 before automatic hscrolling will horizontally scroll the window. */);
29688 hscroll_margin = 5;
29689
29690 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29691 doc: /* How many columns to scroll the window when point gets too close to the edge.
29692 When point is less than `hscroll-margin' columns from the window
29693 edge, automatic hscrolling will scroll the window by the amount of columns
29694 determined by this variable. If its value is a positive integer, scroll that
29695 many columns. If it's a positive floating-point number, it specifies the
29696 fraction of the window's width to scroll. If it's nil or zero, point will be
29697 centered horizontally after the scroll. Any other value, including negative
29698 numbers, are treated as if the value were zero.
29699
29700 Automatic hscrolling always moves point outside the scroll margin, so if
29701 point was more than scroll step columns inside the margin, the window will
29702 scroll more than the value given by the scroll step.
29703
29704 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29705 and `scroll-right' overrides this variable's effect. */);
29706 Vhscroll_step = make_number (0);
29707
29708 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29709 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29710 Bind this around calls to `message' to let it take effect. */);
29711 message_truncate_lines = 0;
29712
29713 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29714 doc: /* Normal hook run to update the menu bar definitions.
29715 Redisplay runs this hook before it redisplays the menu bar.
29716 This is used to update submenus such as Buffers,
29717 whose contents depend on various data. */);
29718 Vmenu_bar_update_hook = Qnil;
29719
29720 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29721 doc: /* Frame for which we are updating a menu.
29722 The enable predicate for a menu binding should check this variable. */);
29723 Vmenu_updating_frame = Qnil;
29724
29725 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29726 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29727 inhibit_menubar_update = 0;
29728
29729 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29730 doc: /* Prefix prepended to all continuation lines at display time.
29731 The value may be a string, an image, or a stretch-glyph; it is
29732 interpreted in the same way as the value of a `display' text property.
29733
29734 This variable is overridden by any `wrap-prefix' text or overlay
29735 property.
29736
29737 To add a prefix to non-continuation lines, use `line-prefix'. */);
29738 Vwrap_prefix = Qnil;
29739 DEFSYM (Qwrap_prefix, "wrap-prefix");
29740 Fmake_variable_buffer_local (Qwrap_prefix);
29741
29742 DEFVAR_LISP ("line-prefix", Vline_prefix,
29743 doc: /* Prefix prepended to all non-continuation lines at display time.
29744 The value may be a string, an image, or a stretch-glyph; it is
29745 interpreted in the same way as the value of a `display' text property.
29746
29747 This variable is overridden by any `line-prefix' text or overlay
29748 property.
29749
29750 To add a prefix to continuation lines, use `wrap-prefix'. */);
29751 Vline_prefix = Qnil;
29752 DEFSYM (Qline_prefix, "line-prefix");
29753 Fmake_variable_buffer_local (Qline_prefix);
29754
29755 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29756 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29757 inhibit_eval_during_redisplay = 0;
29758
29759 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29760 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29761 inhibit_free_realized_faces = 0;
29762
29763 #ifdef GLYPH_DEBUG
29764 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29765 doc: /* Inhibit try_window_id display optimization. */);
29766 inhibit_try_window_id = 0;
29767
29768 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29769 doc: /* Inhibit try_window_reusing display optimization. */);
29770 inhibit_try_window_reusing = 0;
29771
29772 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29773 doc: /* Inhibit try_cursor_movement display optimization. */);
29774 inhibit_try_cursor_movement = 0;
29775 #endif /* GLYPH_DEBUG */
29776
29777 DEFVAR_INT ("overline-margin", overline_margin,
29778 doc: /* Space between overline and text, in pixels.
29779 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29780 margin to the character height. */);
29781 overline_margin = 2;
29782
29783 DEFVAR_INT ("underline-minimum-offset",
29784 underline_minimum_offset,
29785 doc: /* Minimum distance between baseline and underline.
29786 This can improve legibility of underlined text at small font sizes,
29787 particularly when using variable `x-use-underline-position-properties'
29788 with fonts that specify an UNDERLINE_POSITION relatively close to the
29789 baseline. The default value is 1. */);
29790 underline_minimum_offset = 1;
29791
29792 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29793 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29794 This feature only works when on a window system that can change
29795 cursor shapes. */);
29796 display_hourglass_p = 1;
29797
29798 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29799 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29800 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29801
29802 #ifdef HAVE_WINDOW_SYSTEM
29803 hourglass_atimer = NULL;
29804 hourglass_shown_p = 0;
29805 #endif /* HAVE_WINDOW_SYSTEM */
29806
29807 DEFSYM (Qglyphless_char, "glyphless-char");
29808 DEFSYM (Qhex_code, "hex-code");
29809 DEFSYM (Qempty_box, "empty-box");
29810 DEFSYM (Qthin_space, "thin-space");
29811 DEFSYM (Qzero_width, "zero-width");
29812
29813 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
29814 doc: /* Function run just before redisplay.
29815 It is called with one argument, which is the set of windows that are to
29816 be redisplayed. This set can be nil (meaning, only the selected window),
29817 or t (meaning all windows). */);
29818 Vpre_redisplay_function = intern ("ignore");
29819
29820 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29821 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29822
29823 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29824 doc: /* Char-table defining glyphless characters.
29825 Each element, if non-nil, should be one of the following:
29826 an ASCII acronym string: display this string in a box
29827 `hex-code': display the hexadecimal code of a character in a box
29828 `empty-box': display as an empty box
29829 `thin-space': display as 1-pixel width space
29830 `zero-width': don't display
29831 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29832 display method for graphical terminals and text terminals respectively.
29833 GRAPHICAL and TEXT should each have one of the values listed above.
29834
29835 The char-table has one extra slot to control the display of a character for
29836 which no font is found. This slot only takes effect on graphical terminals.
29837 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29838 `thin-space'. The default is `empty-box'.
29839
29840 If a character has a non-nil entry in an active display table, the
29841 display table takes effect; in this case, Emacs does not consult
29842 `glyphless-char-display' at all. */);
29843 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29844 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29845 Qempty_box);
29846
29847 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29848 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29849 Vdebug_on_message = Qnil;
29850
29851 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
29852 doc: /* */);
29853 Vredisplay__all_windows_cause
29854 = Fmake_vector (make_number (100), make_number (0));
29855
29856 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
29857 doc: /* */);
29858 Vredisplay__mode_lines_cause
29859 = Fmake_vector (make_number (100), make_number (0));
29860 }
29861
29862
29863 /* Initialize this module when Emacs starts. */
29864
29865 void
29866 init_xdisp (void)
29867 {
29868 CHARPOS (this_line_start_pos) = 0;
29869
29870 if (!noninteractive)
29871 {
29872 struct window *m = XWINDOW (minibuf_window);
29873 Lisp_Object frame = m->frame;
29874 struct frame *f = XFRAME (frame);
29875 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29876 struct window *r = XWINDOW (root);
29877 int i;
29878
29879 echo_area_window = minibuf_window;
29880
29881 r->top_line = FRAME_TOP_MARGIN (f);
29882 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29883 r->total_cols = FRAME_COLS (f);
29884
29885 m->top_line = FRAME_LINES (f) - 1;
29886 m->total_lines = 1;
29887 m->total_cols = FRAME_COLS (f);
29888
29889 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29890 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29891 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29892
29893 /* The default ellipsis glyphs `...'. */
29894 for (i = 0; i < 3; ++i)
29895 default_invis_vector[i] = make_number ('.');
29896 }
29897
29898 {
29899 /* Allocate the buffer for frame titles.
29900 Also used for `format-mode-line'. */
29901 int size = 100;
29902 mode_line_noprop_buf = xmalloc (size);
29903 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29904 mode_line_noprop_ptr = mode_line_noprop_buf;
29905 mode_line_target = MODE_LINE_DISPLAY;
29906 }
29907
29908 help_echo_showing_p = 0;
29909 }
29910
29911 #ifdef HAVE_WINDOW_SYSTEM
29912
29913 /* Platform-independent portion of hourglass implementation. */
29914
29915 /* Cancel a currently active hourglass timer, and start a new one. */
29916 void
29917 start_hourglass (void)
29918 {
29919 struct timespec delay;
29920
29921 cancel_hourglass ();
29922
29923 if (INTEGERP (Vhourglass_delay)
29924 && XINT (Vhourglass_delay) > 0)
29925 delay = make_timespec (min (XINT (Vhourglass_delay),
29926 TYPE_MAXIMUM (time_t)),
29927 0);
29928 else if (FLOATP (Vhourglass_delay)
29929 && XFLOAT_DATA (Vhourglass_delay) > 0)
29930 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29931 else
29932 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29933
29934 #ifdef HAVE_NTGUI
29935 {
29936 extern void w32_note_current_window (void);
29937 w32_note_current_window ();
29938 }
29939 #endif /* HAVE_NTGUI */
29940
29941 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29942 show_hourglass, NULL);
29943 }
29944
29945
29946 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29947 shown. */
29948 void
29949 cancel_hourglass (void)
29950 {
29951 if (hourglass_atimer)
29952 {
29953 cancel_atimer (hourglass_atimer);
29954 hourglass_atimer = NULL;
29955 }
29956
29957 if (hourglass_shown_p)
29958 hide_hourglass ();
29959 }
29960
29961 #endif /* HAVE_WINDOW_SYSTEM */