* lisp/term/xterm.el (xterm--version-handler): Adapt to xterm-280's output.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2014 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
305
306 #ifndef FRAME_X_OUTPUT
307 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
308 #endif
309
310 #define INFINITY 10000000
311
312 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
313 Lisp_Object Qwindow_scroll_functions;
314 static Lisp_Object Qwindow_text_change_functions;
315 static Lisp_Object Qredisplay_end_trigger_functions;
316 Lisp_Object Qinhibit_point_motion_hooks;
317 static Lisp_Object QCeval, QCpropertize;
318 Lisp_Object QCfile, QCdata;
319 static Lisp_Object Qfontified;
320 static Lisp_Object Qgrow_only;
321 static Lisp_Object Qinhibit_eval_during_redisplay;
322 static Lisp_Object Qbuffer_position, Qposition, Qobject;
323 static Lisp_Object Qright_to_left, Qleft_to_right;
324
325 /* Cursor shapes. */
326 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
327
328 /* Pointer shapes. */
329 static Lisp_Object Qarrow, Qhand;
330 Lisp_Object Qtext;
331
332 /* Holds the list (error). */
333 static Lisp_Object list_of_error;
334
335 static Lisp_Object Qfontification_functions;
336
337 static Lisp_Object Qwrap_prefix;
338 static Lisp_Object Qline_prefix;
339 static Lisp_Object Qredisplay_internal;
340
341 /* Non-nil means don't actually do any redisplay. */
342
343 Lisp_Object Qinhibit_redisplay;
344
345 /* Names of text properties relevant for redisplay. */
346
347 Lisp_Object Qdisplay;
348
349 Lisp_Object Qspace, QCalign_to;
350 static Lisp_Object QCrelative_width, QCrelative_height;
351 Lisp_Object Qleft_margin, Qright_margin;
352 static Lisp_Object Qspace_width, Qraise;
353 static Lisp_Object Qslice;
354 Lisp_Object Qcenter;
355 static Lisp_Object Qmargin, Qpointer;
356 static Lisp_Object Qline_height;
357
358 #ifdef HAVE_WINDOW_SYSTEM
359
360 /* Test if overflow newline into fringe. Called with iterator IT
361 at or past right window margin, and with IT->current_x set. */
362
363 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
364 (!NILP (Voverflow_newline_into_fringe) \
365 && FRAME_WINDOW_P ((IT)->f) \
366 && ((IT)->bidi_it.paragraph_dir == R2L \
367 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
368 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
369 && (IT)->current_x == (IT)->last_visible_x)
370
371 #else /* !HAVE_WINDOW_SYSTEM */
372 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
373 #endif /* HAVE_WINDOW_SYSTEM */
374
375 /* Test if the display element loaded in IT, or the underlying buffer
376 or string character, is a space or a TAB character. This is used
377 to determine where word wrapping can occur. */
378
379 #define IT_DISPLAYING_WHITESPACE(it) \
380 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
381 || ((STRINGP (it->string) \
382 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
383 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
384 || (it->s \
385 && (it->s[IT_BYTEPOS (*it)] == ' ' \
386 || it->s[IT_BYTEPOS (*it)] == '\t')) \
387 || (IT_BYTEPOS (*it) < ZV_BYTE \
388 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
389 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
390
391 /* Name of the face used to highlight trailing whitespace. */
392
393 static Lisp_Object Qtrailing_whitespace;
394
395 /* Name and number of the face used to highlight escape glyphs. */
396
397 static Lisp_Object Qescape_glyph;
398
399 /* Name and number of the face used to highlight non-breaking spaces. */
400
401 static Lisp_Object Qnobreak_space;
402
403 /* The symbol `image' which is the car of the lists used to represent
404 images in Lisp. Also a tool bar style. */
405
406 Lisp_Object Qimage;
407
408 /* The image map types. */
409 Lisp_Object QCmap;
410 static Lisp_Object QCpointer;
411 static Lisp_Object Qrect, Qcircle, Qpoly;
412
413 /* Tool bar styles */
414 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
415
416 /* Non-zero means print newline to stdout before next mini-buffer
417 message. */
418
419 bool noninteractive_need_newline;
420
421 /* Non-zero means print newline to message log before next message. */
422
423 static bool message_log_need_newline;
424
425 /* Three markers that message_dolog uses.
426 It could allocate them itself, but that causes trouble
427 in handling memory-full errors. */
428 static Lisp_Object message_dolog_marker1;
429 static Lisp_Object message_dolog_marker2;
430 static Lisp_Object message_dolog_marker3;
431 \f
432 /* The buffer position of the first character appearing entirely or
433 partially on the line of the selected window which contains the
434 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
435 redisplay optimization in redisplay_internal. */
436
437 static struct text_pos this_line_start_pos;
438
439 /* Number of characters past the end of the line above, including the
440 terminating newline. */
441
442 static struct text_pos this_line_end_pos;
443
444 /* The vertical positions and the height of this line. */
445
446 static int this_line_vpos;
447 static int this_line_y;
448 static int this_line_pixel_height;
449
450 /* X position at which this display line starts. Usually zero;
451 negative if first character is partially visible. */
452
453 static int this_line_start_x;
454
455 /* The smallest character position seen by move_it_* functions as they
456 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
457 hscrolled lines, see display_line. */
458
459 static struct text_pos this_line_min_pos;
460
461 /* Buffer that this_line_.* variables are referring to. */
462
463 static struct buffer *this_line_buffer;
464
465
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
470
471 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
475
476 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477
478 Lisp_Object Qmenu_bar_update_hook;
479
480 /* Nonzero if an overlay arrow has been displayed in this window. */
481
482 static bool overlay_arrow_seen;
483
484 /* Vector containing glyphs for an ellipsis `...'. */
485
486 static Lisp_Object default_invis_vector[3];
487
488 /* This is the window where the echo area message was displayed. It
489 is always a mini-buffer window, but it may not be the same window
490 currently active as a mini-buffer. */
491
492 Lisp_Object echo_area_window;
493
494 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
495 pushes the current message and the value of
496 message_enable_multibyte on the stack, the function restore_message
497 pops the stack and displays MESSAGE again. */
498
499 static Lisp_Object Vmessage_stack;
500
501 /* Nonzero means multibyte characters were enabled when the echo area
502 message was specified. */
503
504 static bool message_enable_multibyte;
505
506 /* Nonzero if we should redraw the mode lines on the next redisplay.
507 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
508 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
509 (the number used is then only used to track down the cause for this
510 full-redisplay). */
511
512 int update_mode_lines;
513
514 /* Nonzero if window sizes or contents other than selected-window have changed
515 since last redisplay that finished.
516 If it has value REDISPLAY_SOME, then only redisplay the windows where
517 the `redisplay' bit has been set. Otherwise, redisplay all windows
518 (the number used is then only used to track down the cause for this
519 full-redisplay). */
520
521 int windows_or_buffers_changed;
522
523 /* Nonzero after display_mode_line if %l was used and it displayed a
524 line number. */
525
526 static bool line_number_displayed;
527
528 /* The name of the *Messages* buffer, a string. */
529
530 static Lisp_Object Vmessages_buffer_name;
531
532 /* Current, index 0, and last displayed echo area message. Either
533 buffers from echo_buffers, or nil to indicate no message. */
534
535 Lisp_Object echo_area_buffer[2];
536
537 /* The buffers referenced from echo_area_buffer. */
538
539 static Lisp_Object echo_buffer[2];
540
541 /* A vector saved used in with_area_buffer to reduce consing. */
542
543 static Lisp_Object Vwith_echo_area_save_vector;
544
545 /* Non-zero means display_echo_area should display the last echo area
546 message again. Set by redisplay_preserve_echo_area. */
547
548 static bool display_last_displayed_message_p;
549
550 /* Nonzero if echo area is being used by print; zero if being used by
551 message. */
552
553 static bool message_buf_print;
554
555 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
556
557 static Lisp_Object Qinhibit_menubar_update;
558 static Lisp_Object Qmessage_truncate_lines;
559
560 /* Set to 1 in clear_message to make redisplay_internal aware
561 of an emptied echo area. */
562
563 static bool message_cleared_p;
564
565 /* A scratch glyph row with contents used for generating truncation
566 glyphs. Also used in direct_output_for_insert. */
567
568 #define MAX_SCRATCH_GLYPHS 100
569 static struct glyph_row scratch_glyph_row;
570 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
571
572 /* Ascent and height of the last line processed by move_it_to. */
573
574 static int last_height;
575
576 /* Non-zero if there's a help-echo in the echo area. */
577
578 bool help_echo_showing_p;
579
580 /* The maximum distance to look ahead for text properties. Values
581 that are too small let us call compute_char_face and similar
582 functions too often which is expensive. Values that are too large
583 let us call compute_char_face and alike too often because we
584 might not be interested in text properties that far away. */
585
586 #define TEXT_PROP_DISTANCE_LIMIT 100
587
588 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
589 iterator state and later restore it. This is needed because the
590 bidi iterator on bidi.c keeps a stacked cache of its states, which
591 is really a singleton. When we use scratch iterator objects to
592 move around the buffer, we can cause the bidi cache to be pushed or
593 popped, and therefore we need to restore the cache state when we
594 return to the original iterator. */
595 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
596 do { \
597 if (CACHE) \
598 bidi_unshelve_cache (CACHE, 1); \
599 ITCOPY = ITORIG; \
600 CACHE = bidi_shelve_cache (); \
601 } while (0)
602
603 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
604 do { \
605 if (pITORIG != pITCOPY) \
606 *(pITORIG) = *(pITCOPY); \
607 bidi_unshelve_cache (CACHE, 0); \
608 CACHE = NULL; \
609 } while (0)
610
611 /* Functions to mark elements as needing redisplay. */
612 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
613
614 void
615 redisplay_other_windows (void)
616 {
617 if (!windows_or_buffers_changed)
618 windows_or_buffers_changed = REDISPLAY_SOME;
619 }
620
621 void
622 wset_redisplay (struct window *w)
623 {
624 /* Beware: selected_window can be nil during early stages. */
625 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
626 redisplay_other_windows ();
627 w->redisplay = true;
628 }
629
630 void
631 fset_redisplay (struct frame *f)
632 {
633 redisplay_other_windows ();
634 f->redisplay = true;
635 }
636
637 void
638 bset_redisplay (struct buffer *b)
639 {
640 int count = buffer_window_count (b);
641 if (count > 0)
642 {
643 /* ... it's visible in other window than selected, */
644 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
645 redisplay_other_windows ();
646 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
647 so that if we later set windows_or_buffers_changed, this buffer will
648 not be omitted. */
649 b->text->redisplay = true;
650 }
651 }
652
653 void
654 bset_update_mode_line (struct buffer *b)
655 {
656 if (!update_mode_lines)
657 update_mode_lines = REDISPLAY_SOME;
658 b->text->redisplay = true;
659 }
660
661 #ifdef GLYPH_DEBUG
662
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG defined. */
665
666 bool trace_redisplay_p;
667
668 #endif /* GLYPH_DEBUG */
669
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
673
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
678
679 static Lisp_Object Qauto_hscroll_mode;
680
681 /* Buffer being redisplayed -- for redisplay_window_error. */
682
683 static struct buffer *displayed_buffer;
684
685 /* Value returned from text property handlers (see below). */
686
687 enum prop_handled
688 {
689 HANDLED_NORMALLY,
690 HANDLED_RECOMPUTE_PROPS,
691 HANDLED_OVERLAY_STRING_CONSUMED,
692 HANDLED_RETURN
693 };
694
695 /* A description of text properties that redisplay is interested
696 in. */
697
698 struct props
699 {
700 /* The name of the property. */
701 Lisp_Object *name;
702
703 /* A unique index for the property. */
704 enum prop_idx idx;
705
706 /* A handler function called to set up iterator IT from the property
707 at IT's current position. Value is used to steer handle_stop. */
708 enum prop_handled (*handler) (struct it *it);
709 };
710
711 static enum prop_handled handle_face_prop (struct it *);
712 static enum prop_handled handle_invisible_prop (struct it *);
713 static enum prop_handled handle_display_prop (struct it *);
714 static enum prop_handled handle_composition_prop (struct it *);
715 static enum prop_handled handle_overlay_change (struct it *);
716 static enum prop_handled handle_fontified_prop (struct it *);
717
718 /* Properties handled by iterators. */
719
720 static struct props it_props[] =
721 {
722 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
723 /* Handle `face' before `display' because some sub-properties of
724 `display' need to know the face. */
725 {&Qface, FACE_PROP_IDX, handle_face_prop},
726 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
727 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
728 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
729 {NULL, 0, NULL}
730 };
731
732 /* Value is the position described by X. If X is a marker, value is
733 the marker_position of X. Otherwise, value is X. */
734
735 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
736
737 /* Enumeration returned by some move_it_.* functions internally. */
738
739 enum move_it_result
740 {
741 /* Not used. Undefined value. */
742 MOVE_UNDEFINED,
743
744 /* Move ended at the requested buffer position or ZV. */
745 MOVE_POS_MATCH_OR_ZV,
746
747 /* Move ended at the requested X pixel position. */
748 MOVE_X_REACHED,
749
750 /* Move within a line ended at the end of a line that must be
751 continued. */
752 MOVE_LINE_CONTINUED,
753
754 /* Move within a line ended at the end of a line that would
755 be displayed truncated. */
756 MOVE_LINE_TRUNCATED,
757
758 /* Move within a line ended at a line end. */
759 MOVE_NEWLINE_OR_CR
760 };
761
762 /* This counter is used to clear the face cache every once in a while
763 in redisplay_internal. It is incremented for each redisplay.
764 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
765 cleared. */
766
767 #define CLEAR_FACE_CACHE_COUNT 500
768 static int clear_face_cache_count;
769
770 /* Similarly for the image cache. */
771
772 #ifdef HAVE_WINDOW_SYSTEM
773 #define CLEAR_IMAGE_CACHE_COUNT 101
774 static int clear_image_cache_count;
775
776 /* Null glyph slice */
777 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
778 #endif
779
780 /* True while redisplay_internal is in progress. */
781
782 bool redisplaying_p;
783
784 static Lisp_Object Qinhibit_free_realized_faces;
785 static Lisp_Object Qmode_line_default_help_echo;
786
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
789
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 ptrdiff_t help_echo_pos;
794
795 /* Temporary variable for XTread_socket. */
796
797 Lisp_Object previous_help_echo_string;
798
799 /* Platform-independent portion of hourglass implementation. */
800
801 #ifdef HAVE_WINDOW_SYSTEM
802
803 /* Non-zero means an hourglass cursor is currently shown. */
804 bool hourglass_shown_p;
805
806 /* If non-null, an asynchronous timer that, when it expires, displays
807 an hourglass cursor on all frames. */
808 struct atimer *hourglass_atimer;
809
810 #endif /* HAVE_WINDOW_SYSTEM */
811
812 /* Name of the face used to display glyphless characters. */
813 static Lisp_Object Qglyphless_char;
814
815 /* Symbol for the purpose of Vglyphless_char_display. */
816 static Lisp_Object Qglyphless_char_display;
817
818 /* Method symbols for Vglyphless_char_display. */
819 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
820
821 /* Default number of seconds to wait before displaying an hourglass
822 cursor. */
823 #define DEFAULT_HOURGLASS_DELAY 1
824
825 #ifdef HAVE_WINDOW_SYSTEM
826
827 /* Default pixel width of `thin-space' display method. */
828 #define THIN_SPACE_WIDTH 1
829
830 #endif /* HAVE_WINDOW_SYSTEM */
831
832 /* Function prototypes. */
833
834 static void setup_for_ellipsis (struct it *, int);
835 static void set_iterator_to_next (struct it *, int);
836 static void mark_window_display_accurate_1 (struct window *, int);
837 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
838 static int display_prop_string_p (Lisp_Object, Lisp_Object);
839 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
840 static int cursor_row_p (struct glyph_row *);
841 static int redisplay_mode_lines (Lisp_Object, bool);
842 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
843
844 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
845
846 static void handle_line_prefix (struct it *);
847
848 static void pint2str (char *, int, ptrdiff_t);
849 static void pint2hrstr (char *, int, ptrdiff_t);
850 static struct text_pos run_window_scroll_functions (Lisp_Object,
851 struct text_pos);
852 static int text_outside_line_unchanged_p (struct window *,
853 ptrdiff_t, ptrdiff_t);
854 static void store_mode_line_noprop_char (char);
855 static int store_mode_line_noprop (const char *, int, int);
856 static void handle_stop (struct it *);
857 static void handle_stop_backwards (struct it *, ptrdiff_t);
858 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
859 static void ensure_echo_area_buffers (void);
860 static void unwind_with_echo_area_buffer (Lisp_Object);
861 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
862 static int with_echo_area_buffer (struct window *, int,
863 int (*) (ptrdiff_t, Lisp_Object),
864 ptrdiff_t, Lisp_Object);
865 static void clear_garbaged_frames (void);
866 static int current_message_1 (ptrdiff_t, Lisp_Object);
867 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
868 static void set_message (Lisp_Object);
869 static int set_message_1 (ptrdiff_t, Lisp_Object);
870 static int display_echo_area (struct window *);
871 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
872 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
873 static void unwind_redisplay (void);
874 static int string_char_and_length (const unsigned char *, int *);
875 static struct text_pos display_prop_end (struct it *, Lisp_Object,
876 struct text_pos);
877 static int compute_window_start_on_continuation_line (struct window *);
878 static void insert_left_trunc_glyphs (struct it *);
879 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
880 Lisp_Object);
881 static void extend_face_to_end_of_line (struct it *);
882 static int append_space_for_newline (struct it *, int);
883 static int cursor_row_fully_visible_p (struct window *, int, int);
884 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
885 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
886 static int trailing_whitespace_p (ptrdiff_t);
887 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
888 static void push_it (struct it *, struct text_pos *);
889 static void iterate_out_of_display_property (struct it *);
890 static void pop_it (struct it *);
891 static void sync_frame_with_window_matrix_rows (struct window *);
892 static void redisplay_internal (void);
893 static int echo_area_display (int);
894 static void redisplay_windows (Lisp_Object);
895 static void redisplay_window (Lisp_Object, bool);
896 static Lisp_Object redisplay_window_error (Lisp_Object);
897 static Lisp_Object redisplay_window_0 (Lisp_Object);
898 static Lisp_Object redisplay_window_1 (Lisp_Object);
899 static int set_cursor_from_row (struct window *, struct glyph_row *,
900 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
901 int, int);
902 static int update_menu_bar (struct frame *, int, int);
903 static int try_window_reusing_current_matrix (struct window *);
904 static int try_window_id (struct window *);
905 static int display_line (struct it *);
906 static int display_mode_lines (struct window *);
907 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
908 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
909 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
910 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
911 static void display_menu_bar (struct window *);
912 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
913 ptrdiff_t *);
914 static int display_string (const char *, Lisp_Object, Lisp_Object,
915 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
916 static void compute_line_metrics (struct it *);
917 static void run_redisplay_end_trigger_hook (struct it *);
918 static int get_overlay_strings (struct it *, ptrdiff_t);
919 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
920 static void next_overlay_string (struct it *);
921 static void reseat (struct it *, struct text_pos, int);
922 static void reseat_1 (struct it *, struct text_pos, int);
923 static void back_to_previous_visible_line_start (struct it *);
924 static void reseat_at_next_visible_line_start (struct it *, int);
925 static int next_element_from_ellipsis (struct it *);
926 static int next_element_from_display_vector (struct it *);
927 static int next_element_from_string (struct it *);
928 static int next_element_from_c_string (struct it *);
929 static int next_element_from_buffer (struct it *);
930 static int next_element_from_composition (struct it *);
931 static int next_element_from_image (struct it *);
932 static int next_element_from_stretch (struct it *);
933 static void load_overlay_strings (struct it *, ptrdiff_t);
934 static int init_from_display_pos (struct it *, struct window *,
935 struct display_pos *);
936 static void reseat_to_string (struct it *, const char *,
937 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
938 static int get_next_display_element (struct it *);
939 static enum move_it_result
940 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
941 enum move_operation_enum);
942 static void get_visually_first_element (struct it *);
943 static void init_to_row_start (struct it *, struct window *,
944 struct glyph_row *);
945 static int init_to_row_end (struct it *, struct window *,
946 struct glyph_row *);
947 static void back_to_previous_line_start (struct it *);
948 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
949 static struct text_pos string_pos_nchars_ahead (struct text_pos,
950 Lisp_Object, ptrdiff_t);
951 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
952 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
953 static ptrdiff_t number_of_chars (const char *, bool);
954 static void compute_stop_pos (struct it *);
955 static void compute_string_pos (struct text_pos *, struct text_pos,
956 Lisp_Object);
957 static int face_before_or_after_it_pos (struct it *, int);
958 static ptrdiff_t next_overlay_change (ptrdiff_t);
959 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
960 Lisp_Object, struct text_pos *, ptrdiff_t, int);
961 static int handle_single_display_spec (struct it *, Lisp_Object,
962 Lisp_Object, Lisp_Object,
963 struct text_pos *, ptrdiff_t, int, int);
964 static int underlying_face_id (struct it *);
965 static int in_ellipses_for_invisible_text_p (struct display_pos *,
966 struct window *);
967
968 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
969 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
970
971 #ifdef HAVE_WINDOW_SYSTEM
972
973 static void x_consider_frame_title (Lisp_Object);
974 static void update_tool_bar (struct frame *, int);
975 static int redisplay_tool_bar (struct frame *);
976 static void x_draw_bottom_divider (struct window *w);
977 static void notice_overwritten_cursor (struct window *,
978 enum glyph_row_area,
979 int, int, int, int);
980 static void append_stretch_glyph (struct it *, Lisp_Object,
981 int, int, int);
982
983
984 #endif /* HAVE_WINDOW_SYSTEM */
985
986 static void produce_special_glyphs (struct it *, enum display_element_type);
987 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
988 static bool coords_in_mouse_face_p (struct window *, int, int);
989
990
991 \f
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
995
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
999
1000 This is the height of W minus the height of a mode line, if any. */
1001
1002 int
1003 window_text_bottom_y (struct window *w)
1004 {
1005 int height = WINDOW_PIXEL_HEIGHT (w);
1006
1007 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1008
1009 if (WINDOW_WANTS_MODELINE_P (w))
1010 height -= CURRENT_MODE_LINE_HEIGHT (w);
1011
1012 return height;
1013 }
1014
1015 /* Return the pixel width of display area AREA of window W.
1016 ANY_AREA means return the total width of W, not including
1017 fringes to the left and right of the window. */
1018
1019 int
1020 window_box_width (struct window *w, enum glyph_row_area area)
1021 {
1022 int width = w->pixel_width;
1023
1024 if (!w->pseudo_window_p)
1025 {
1026 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1027 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1028
1029 if (area == TEXT_AREA)
1030 width -= (WINDOW_MARGINS_WIDTH (w)
1031 + WINDOW_FRINGES_WIDTH (w));
1032 else if (area == LEFT_MARGIN_AREA)
1033 width = WINDOW_LEFT_MARGIN_WIDTH (w);
1034 else if (area == RIGHT_MARGIN_AREA)
1035 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
1036 }
1037
1038 /* With wide margins, fringes, etc. we might end up with a negative
1039 width, correct that here. */
1040 return max (0, width);
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_PIXEL_HEIGHT (w);
1052
1053 eassert (height >= 0);
1054
1055 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1056
1057 /* Note: the code below that determines the mode-line/header-line
1058 height is essentially the same as that contained in the macro
1059 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1060 the appropriate glyph row has its `mode_line_p' flag set,
1061 and if it doesn't, uses estimate_mode_line_height instead. */
1062
1063 if (WINDOW_WANTS_MODELINE_P (w))
1064 {
1065 struct glyph_row *ml_row
1066 = (w->current_matrix && w->current_matrix->rows
1067 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1068 : 0);
1069 if (ml_row && ml_row->mode_line_p)
1070 height -= ml_row->height;
1071 else
1072 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1073 }
1074
1075 if (WINDOW_WANTS_HEADER_LINE_P (w))
1076 {
1077 struct glyph_row *hl_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (hl_row && hl_row->mode_line_p)
1082 height -= hl_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1085 }
1086
1087 /* With a very small font and a mode-line that's taller than
1088 default, we might end up with a negative height. */
1089 return max (0, height);
1090 }
1091
1092 /* Return the window-relative coordinate of the left edge of display
1093 area AREA of window W. ANY_AREA means return the left edge of the
1094 whole window, to the right of the left fringe of W. */
1095
1096 int
1097 window_box_left_offset (struct window *w, enum glyph_row_area area)
1098 {
1099 int x;
1100
1101 if (w->pseudo_window_p)
1102 return 0;
1103
1104 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1105
1106 if (area == TEXT_AREA)
1107 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1108 + window_box_width (w, LEFT_MARGIN_AREA));
1109 else if (area == RIGHT_MARGIN_AREA)
1110 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1111 + window_box_width (w, LEFT_MARGIN_AREA)
1112 + window_box_width (w, TEXT_AREA)
1113 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1114 ? 0
1115 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1116 else if (area == LEFT_MARGIN_AREA
1117 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1118 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1119
1120 /* Don't return more than the window's pixel width. */
1121 return min (x, w->pixel_width);
1122 }
1123
1124
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. ANY_AREA means return the right edge of the
1127 whole window, to the left of the right fringe of W. */
1128
1129 int
1130 window_box_right_offset (struct window *w, enum glyph_row_area area)
1131 {
1132 /* Don't return more than the window's pixel width. */
1133 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1134 w->pixel_width);
1135 }
1136
1137 /* Return the frame-relative coordinate of the left edge of display
1138 area AREA of window W. ANY_AREA means return the left edge of the
1139 whole window, to the right of the left fringe of W. */
1140
1141 int
1142 window_box_left (struct window *w, enum glyph_row_area area)
1143 {
1144 struct frame *f = XFRAME (w->frame);
1145 int x;
1146
1147 if (w->pseudo_window_p)
1148 return FRAME_INTERNAL_BORDER_WIDTH (f);
1149
1150 x = (WINDOW_LEFT_EDGE_X (w)
1151 + window_box_left_offset (w, area));
1152
1153 return x;
1154 }
1155
1156
1157 /* Return the frame-relative coordinate of the right edge of display
1158 area AREA of window W. ANY_AREA means return the right edge of the
1159 whole window, to the left of the right fringe of W. */
1160
1161 int
1162 window_box_right (struct window *w, enum glyph_row_area area)
1163 {
1164 return window_box_left (w, area) + window_box_width (w, area);
1165 }
1166
1167 /* Get the bounding box of the display area AREA of window W, without
1168 mode lines, in frame-relative coordinates. ANY_AREA means the
1169 whole window, not including the left and right fringes of
1170 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1171 coordinates of the upper-left corner of the box. Return in
1172 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1173
1174 void
1175 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1176 int *box_y, int *box_width, int *box_height)
1177 {
1178 if (box_width)
1179 *box_width = window_box_width (w, area);
1180 if (box_height)
1181 *box_height = window_box_height (w);
1182 if (box_x)
1183 *box_x = window_box_left (w, area);
1184 if (box_y)
1185 {
1186 *box_y = WINDOW_TOP_EDGE_Y (w);
1187 if (WINDOW_WANTS_HEADER_LINE_P (w))
1188 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1189 }
1190 }
1191
1192 #ifdef HAVE_WINDOW_SYSTEM
1193
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1196 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1197 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1198 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1199 box. */
1200
1201 static void
1202 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1203 int *bottom_right_x, int *bottom_right_y)
1204 {
1205 window_box (w, ANY_AREA, top_left_x, top_left_y,
1206 bottom_right_x, bottom_right_y);
1207 *bottom_right_x += *top_left_x;
1208 *bottom_right_y += *top_left_y;
1209 }
1210
1211 #endif /* HAVE_WINDOW_SYSTEM */
1212
1213 /***********************************************************************
1214 Utilities
1215 ***********************************************************************/
1216
1217 /* Return the bottom y-position of the line the iterator IT is in.
1218 This can modify IT's settings. */
1219
1220 int
1221 line_bottom_y (struct it *it)
1222 {
1223 int line_height = it->max_ascent + it->max_descent;
1224 int line_top_y = it->current_y;
1225
1226 if (line_height == 0)
1227 {
1228 if (last_height)
1229 line_height = last_height;
1230 else if (IT_CHARPOS (*it) < ZV)
1231 {
1232 move_it_by_lines (it, 1);
1233 line_height = (it->max_ascent || it->max_descent
1234 ? it->max_ascent + it->max_descent
1235 : last_height);
1236 }
1237 else
1238 {
1239 struct glyph_row *row = it->glyph_row;
1240
1241 /* Use the default character height. */
1242 it->glyph_row = NULL;
1243 it->what = IT_CHARACTER;
1244 it->c = ' ';
1245 it->len = 1;
1246 PRODUCE_GLYPHS (it);
1247 line_height = it->ascent + it->descent;
1248 it->glyph_row = row;
1249 }
1250 }
1251
1252 return line_top_y + line_height;
1253 }
1254
1255 DEFUN ("line-pixel-height", Fline_pixel_height,
1256 Sline_pixel_height, 0, 0, 0,
1257 doc: /* Return height in pixels of text line in the selected window.
1258
1259 Value is the height in pixels of the line at point. */)
1260 (void)
1261 {
1262 struct it it;
1263 struct text_pos pt;
1264 struct window *w = XWINDOW (selected_window);
1265
1266 SET_TEXT_POS (pt, PT, PT_BYTE);
1267 start_display (&it, w, pt);
1268 it.vpos = it.current_y = 0;
1269 last_height = 0;
1270 return make_number (line_bottom_y (&it));
1271 }
1272
1273 /* Return the default pixel height of text lines in window W. The
1274 value is the canonical height of the W frame's default font, plus
1275 any extra space required by the line-spacing variable or frame
1276 parameter.
1277
1278 Implementation note: this ignores any line-spacing text properties
1279 put on the newline characters. This is because those properties
1280 only affect the _screen_ line ending in the newline (i.e., in a
1281 continued line, only the last screen line will be affected), which
1282 means only a small number of lines in a buffer can ever use this
1283 feature. Since this function is used to compute the default pixel
1284 equivalent of text lines in a window, we can safely ignore those
1285 few lines. For the same reasons, we ignore the line-height
1286 properties. */
1287 int
1288 default_line_pixel_height (struct window *w)
1289 {
1290 struct frame *f = WINDOW_XFRAME (w);
1291 int height = FRAME_LINE_HEIGHT (f);
1292
1293 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1294 {
1295 struct buffer *b = XBUFFER (w->contents);
1296 Lisp_Object val = BVAR (b, extra_line_spacing);
1297
1298 if (NILP (val))
1299 val = BVAR (&buffer_defaults, extra_line_spacing);
1300 if (!NILP (val))
1301 {
1302 if (RANGED_INTEGERP (0, val, INT_MAX))
1303 height += XFASTINT (val);
1304 else if (FLOATP (val))
1305 {
1306 int addon = XFLOAT_DATA (val) * height + 0.5;
1307
1308 if (addon >= 0)
1309 height += addon;
1310 }
1311 }
1312 else
1313 height += f->extra_line_spacing;
1314 }
1315
1316 return height;
1317 }
1318
1319 /* Subroutine of pos_visible_p below. Extracts a display string, if
1320 any, from the display spec given as its argument. */
1321 static Lisp_Object
1322 string_from_display_spec (Lisp_Object spec)
1323 {
1324 if (CONSP (spec))
1325 {
1326 while (CONSP (spec))
1327 {
1328 if (STRINGP (XCAR (spec)))
1329 return XCAR (spec);
1330 spec = XCDR (spec);
1331 }
1332 }
1333 else if (VECTORP (spec))
1334 {
1335 ptrdiff_t i;
1336
1337 for (i = 0; i < ASIZE (spec); i++)
1338 {
1339 if (STRINGP (AREF (spec, i)))
1340 return AREF (spec, i);
1341 }
1342 return Qnil;
1343 }
1344
1345 return spec;
1346 }
1347
1348
1349 /* Limit insanely large values of W->hscroll on frame F to the largest
1350 value that will still prevent first_visible_x and last_visible_x of
1351 'struct it' from overflowing an int. */
1352 static int
1353 window_hscroll_limited (struct window *w, struct frame *f)
1354 {
1355 ptrdiff_t window_hscroll = w->hscroll;
1356 int window_text_width = window_box_width (w, TEXT_AREA);
1357 int colwidth = FRAME_COLUMN_WIDTH (f);
1358
1359 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1360 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1361
1362 return window_hscroll;
1363 }
1364
1365 /* Return 1 if position CHARPOS is visible in window W.
1366 CHARPOS < 0 means return info about WINDOW_END position.
1367 If visible, set *X and *Y to pixel coordinates of top left corner.
1368 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1369 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1370
1371 int
1372 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1373 int *rtop, int *rbot, int *rowh, int *vpos)
1374 {
1375 struct it it;
1376 void *itdata = bidi_shelve_cache ();
1377 struct text_pos top;
1378 int visible_p = 0;
1379 struct buffer *old_buffer = NULL;
1380
1381 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1382 return visible_p;
1383
1384 if (XBUFFER (w->contents) != current_buffer)
1385 {
1386 old_buffer = current_buffer;
1387 set_buffer_internal_1 (XBUFFER (w->contents));
1388 }
1389
1390 SET_TEXT_POS_FROM_MARKER (top, w->start);
1391 /* Scrolling a minibuffer window via scroll bar when the echo area
1392 shows long text sometimes resets the minibuffer contents behind
1393 our backs. */
1394 if (CHARPOS (top) > ZV)
1395 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1396
1397 /* Compute exact mode line heights. */
1398 if (WINDOW_WANTS_MODELINE_P (w))
1399 w->mode_line_height
1400 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1401 BVAR (current_buffer, mode_line_format));
1402
1403 if (WINDOW_WANTS_HEADER_LINE_P (w))
1404 w->header_line_height
1405 = display_mode_line (w, HEADER_LINE_FACE_ID,
1406 BVAR (current_buffer, header_line_format));
1407
1408 start_display (&it, w, top);
1409 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1410 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1411
1412 if (charpos >= 0
1413 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1414 && IT_CHARPOS (it) >= charpos)
1415 /* When scanning backwards under bidi iteration, move_it_to
1416 stops at or _before_ CHARPOS, because it stops at or to
1417 the _right_ of the character at CHARPOS. */
1418 || (it.bidi_p && it.bidi_it.scan_dir == -1
1419 && IT_CHARPOS (it) <= charpos)))
1420 {
1421 /* We have reached CHARPOS, or passed it. How the call to
1422 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1423 or covered by a display property, move_it_to stops at the end
1424 of the invisible text, to the right of CHARPOS. (ii) If
1425 CHARPOS is in a display vector, move_it_to stops on its last
1426 glyph. */
1427 int top_x = it.current_x;
1428 int top_y = it.current_y;
1429 /* Calling line_bottom_y may change it.method, it.position, etc. */
1430 enum it_method it_method = it.method;
1431 int bottom_y = (last_height = 0, line_bottom_y (&it));
1432 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1433
1434 if (top_y < window_top_y)
1435 visible_p = bottom_y > window_top_y;
1436 else if (top_y < it.last_visible_y)
1437 visible_p = true;
1438 if (bottom_y >= it.last_visible_y
1439 && it.bidi_p && it.bidi_it.scan_dir == -1
1440 && IT_CHARPOS (it) < charpos)
1441 {
1442 /* When the last line of the window is scanned backwards
1443 under bidi iteration, we could be duped into thinking
1444 that we have passed CHARPOS, when in fact move_it_to
1445 simply stopped short of CHARPOS because it reached
1446 last_visible_y. To see if that's what happened, we call
1447 move_it_to again with a slightly larger vertical limit,
1448 and see if it actually moved vertically; if it did, we
1449 didn't really reach CHARPOS, which is beyond window end. */
1450 struct it save_it = it;
1451 /* Why 10? because we don't know how many canonical lines
1452 will the height of the next line(s) be. So we guess. */
1453 int ten_more_lines = 10 * default_line_pixel_height (w);
1454
1455 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1456 MOVE_TO_POS | MOVE_TO_Y);
1457 if (it.current_y > top_y)
1458 visible_p = 0;
1459
1460 it = save_it;
1461 }
1462 if (visible_p)
1463 {
1464 if (it_method == GET_FROM_DISPLAY_VECTOR)
1465 {
1466 /* We stopped on the last glyph of a display vector.
1467 Try and recompute. Hack alert! */
1468 if (charpos < 2 || top.charpos >= charpos)
1469 top_x = it.glyph_row->x;
1470 else
1471 {
1472 struct it it2, it2_prev;
1473 /* The idea is to get to the previous buffer
1474 position, consume the character there, and use
1475 the pixel coordinates we get after that. But if
1476 the previous buffer position is also displayed
1477 from a display vector, we need to consume all of
1478 the glyphs from that display vector. */
1479 start_display (&it2, w, top);
1480 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1481 /* If we didn't get to CHARPOS - 1, there's some
1482 replacing display property at that position, and
1483 we stopped after it. That is exactly the place
1484 whose coordinates we want. */
1485 if (IT_CHARPOS (it2) != charpos - 1)
1486 it2_prev = it2;
1487 else
1488 {
1489 /* Iterate until we get out of the display
1490 vector that displays the character at
1491 CHARPOS - 1. */
1492 do {
1493 get_next_display_element (&it2);
1494 PRODUCE_GLYPHS (&it2);
1495 it2_prev = it2;
1496 set_iterator_to_next (&it2, 1);
1497 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1498 && IT_CHARPOS (it2) < charpos);
1499 }
1500 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1501 || it2_prev.current_x > it2_prev.last_visible_x)
1502 top_x = it.glyph_row->x;
1503 else
1504 {
1505 top_x = it2_prev.current_x;
1506 top_y = it2_prev.current_y;
1507 }
1508 }
1509 }
1510 else if (IT_CHARPOS (it) != charpos)
1511 {
1512 Lisp_Object cpos = make_number (charpos);
1513 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1514 Lisp_Object string = string_from_display_spec (spec);
1515 struct text_pos tpos;
1516 int replacing_spec_p;
1517 bool newline_in_string
1518 = (STRINGP (string)
1519 && memchr (SDATA (string), '\n', SBYTES (string)));
1520
1521 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1522 replacing_spec_p
1523 = (!NILP (spec)
1524 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1525 charpos, FRAME_WINDOW_P (it.f)));
1526 /* The tricky code below is needed because there's a
1527 discrepancy between move_it_to and how we set cursor
1528 when PT is at the beginning of a portion of text
1529 covered by a display property or an overlay with a
1530 display property, or the display line ends in a
1531 newline from a display string. move_it_to will stop
1532 _after_ such display strings, whereas
1533 set_cursor_from_row conspires with cursor_row_p to
1534 place the cursor on the first glyph produced from the
1535 display string. */
1536
1537 /* We have overshoot PT because it is covered by a
1538 display property that replaces the text it covers.
1539 If the string includes embedded newlines, we are also
1540 in the wrong display line. Backtrack to the correct
1541 line, where the display property begins. */
1542 if (replacing_spec_p)
1543 {
1544 Lisp_Object startpos, endpos;
1545 EMACS_INT start, end;
1546 struct it it3;
1547 int it3_moved;
1548
1549 /* Find the first and the last buffer positions
1550 covered by the display string. */
1551 endpos =
1552 Fnext_single_char_property_change (cpos, Qdisplay,
1553 Qnil, Qnil);
1554 startpos =
1555 Fprevious_single_char_property_change (endpos, Qdisplay,
1556 Qnil, Qnil);
1557 start = XFASTINT (startpos);
1558 end = XFASTINT (endpos);
1559 /* Move to the last buffer position before the
1560 display property. */
1561 start_display (&it3, w, top);
1562 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1563 /* Move forward one more line if the position before
1564 the display string is a newline or if it is the
1565 rightmost character on a line that is
1566 continued or word-wrapped. */
1567 if (it3.method == GET_FROM_BUFFER
1568 && (it3.c == '\n'
1569 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1570 move_it_by_lines (&it3, 1);
1571 else if (move_it_in_display_line_to (&it3, -1,
1572 it3.current_x
1573 + it3.pixel_width,
1574 MOVE_TO_X)
1575 == MOVE_LINE_CONTINUED)
1576 {
1577 move_it_by_lines (&it3, 1);
1578 /* When we are under word-wrap, the #$@%!
1579 move_it_by_lines moves 2 lines, so we need to
1580 fix that up. */
1581 if (it3.line_wrap == WORD_WRAP)
1582 move_it_by_lines (&it3, -1);
1583 }
1584
1585 /* Record the vertical coordinate of the display
1586 line where we wound up. */
1587 top_y = it3.current_y;
1588 if (it3.bidi_p)
1589 {
1590 /* When characters are reordered for display,
1591 the character displayed to the left of the
1592 display string could be _after_ the display
1593 property in the logical order. Use the
1594 smallest vertical position of these two. */
1595 start_display (&it3, w, top);
1596 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1597 if (it3.current_y < top_y)
1598 top_y = it3.current_y;
1599 }
1600 /* Move from the top of the window to the beginning
1601 of the display line where the display string
1602 begins. */
1603 start_display (&it3, w, top);
1604 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1605 /* If it3_moved stays zero after the 'while' loop
1606 below, that means we already were at a newline
1607 before the loop (e.g., the display string begins
1608 with a newline), so we don't need to (and cannot)
1609 inspect the glyphs of it3.glyph_row, because
1610 PRODUCE_GLYPHS will not produce anything for a
1611 newline, and thus it3.glyph_row stays at its
1612 stale content it got at top of the window. */
1613 it3_moved = 0;
1614 /* Finally, advance the iterator until we hit the
1615 first display element whose character position is
1616 CHARPOS, or until the first newline from the
1617 display string, which signals the end of the
1618 display line. */
1619 while (get_next_display_element (&it3))
1620 {
1621 PRODUCE_GLYPHS (&it3);
1622 if (IT_CHARPOS (it3) == charpos
1623 || ITERATOR_AT_END_OF_LINE_P (&it3))
1624 break;
1625 it3_moved = 1;
1626 set_iterator_to_next (&it3, 0);
1627 }
1628 top_x = it3.current_x - it3.pixel_width;
1629 /* Normally, we would exit the above loop because we
1630 found the display element whose character
1631 position is CHARPOS. For the contingency that we
1632 didn't, and stopped at the first newline from the
1633 display string, move back over the glyphs
1634 produced from the string, until we find the
1635 rightmost glyph not from the string. */
1636 if (it3_moved
1637 && newline_in_string
1638 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1639 {
1640 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1641 + it3.glyph_row->used[TEXT_AREA];
1642
1643 while (EQ ((g - 1)->object, string))
1644 {
1645 --g;
1646 top_x -= g->pixel_width;
1647 }
1648 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1649 + it3.glyph_row->used[TEXT_AREA]);
1650 }
1651 }
1652 }
1653
1654 *x = top_x;
1655 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1656 *rtop = max (0, window_top_y - top_y);
1657 *rbot = max (0, bottom_y - it.last_visible_y);
1658 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1659 - max (top_y, window_top_y)));
1660 *vpos = it.vpos;
1661 }
1662 }
1663 else
1664 {
1665 /* We were asked to provide info about WINDOW_END. */
1666 struct it it2;
1667 void *it2data = NULL;
1668
1669 SAVE_IT (it2, it, it2data);
1670 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1671 move_it_by_lines (&it, 1);
1672 if (charpos < IT_CHARPOS (it)
1673 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1674 {
1675 visible_p = true;
1676 RESTORE_IT (&it2, &it2, it2data);
1677 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1678 *x = it2.current_x;
1679 *y = it2.current_y + it2.max_ascent - it2.ascent;
1680 *rtop = max (0, -it2.current_y);
1681 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1682 - it.last_visible_y));
1683 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1684 it.last_visible_y)
1685 - max (it2.current_y,
1686 WINDOW_HEADER_LINE_HEIGHT (w))));
1687 *vpos = it2.vpos;
1688 }
1689 else
1690 bidi_unshelve_cache (it2data, 1);
1691 }
1692 bidi_unshelve_cache (itdata, 0);
1693
1694 if (old_buffer)
1695 set_buffer_internal_1 (old_buffer);
1696
1697 if (visible_p && w->hscroll > 0)
1698 *x -=
1699 window_hscroll_limited (w, WINDOW_XFRAME (w))
1700 * WINDOW_FRAME_COLUMN_WIDTH (w);
1701
1702 #if 0
1703 /* Debugging code. */
1704 if (visible_p)
1705 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1706 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1707 else
1708 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1709 #endif
1710
1711 return visible_p;
1712 }
1713
1714
1715 /* Return the next character from STR. Return in *LEN the length of
1716 the character. This is like STRING_CHAR_AND_LENGTH but never
1717 returns an invalid character. If we find one, we return a `?', but
1718 with the length of the invalid character. */
1719
1720 static int
1721 string_char_and_length (const unsigned char *str, int *len)
1722 {
1723 int c;
1724
1725 c = STRING_CHAR_AND_LENGTH (str, *len);
1726 if (!CHAR_VALID_P (c))
1727 /* We may not change the length here because other places in Emacs
1728 don't use this function, i.e. they silently accept invalid
1729 characters. */
1730 c = '?';
1731
1732 return c;
1733 }
1734
1735
1736
1737 /* Given a position POS containing a valid character and byte position
1738 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1739
1740 static struct text_pos
1741 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1742 {
1743 eassert (STRINGP (string) && nchars >= 0);
1744
1745 if (STRING_MULTIBYTE (string))
1746 {
1747 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1748 int len;
1749
1750 while (nchars--)
1751 {
1752 string_char_and_length (p, &len);
1753 p += len;
1754 CHARPOS (pos) += 1;
1755 BYTEPOS (pos) += len;
1756 }
1757 }
1758 else
1759 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1760
1761 return pos;
1762 }
1763
1764
1765 /* Value is the text position, i.e. character and byte position,
1766 for character position CHARPOS in STRING. */
1767
1768 static struct text_pos
1769 string_pos (ptrdiff_t charpos, Lisp_Object string)
1770 {
1771 struct text_pos pos;
1772 eassert (STRINGP (string));
1773 eassert (charpos >= 0);
1774 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1775 return pos;
1776 }
1777
1778
1779 /* Value is a text position, i.e. character and byte position, for
1780 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1781 means recognize multibyte characters. */
1782
1783 static struct text_pos
1784 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1785 {
1786 struct text_pos pos;
1787
1788 eassert (s != NULL);
1789 eassert (charpos >= 0);
1790
1791 if (multibyte_p)
1792 {
1793 int len;
1794
1795 SET_TEXT_POS (pos, 0, 0);
1796 while (charpos--)
1797 {
1798 string_char_and_length ((const unsigned char *) s, &len);
1799 s += len;
1800 CHARPOS (pos) += 1;
1801 BYTEPOS (pos) += len;
1802 }
1803 }
1804 else
1805 SET_TEXT_POS (pos, charpos, charpos);
1806
1807 return pos;
1808 }
1809
1810
1811 /* Value is the number of characters in C string S. MULTIBYTE_P
1812 non-zero means recognize multibyte characters. */
1813
1814 static ptrdiff_t
1815 number_of_chars (const char *s, bool multibyte_p)
1816 {
1817 ptrdiff_t nchars;
1818
1819 if (multibyte_p)
1820 {
1821 ptrdiff_t rest = strlen (s);
1822 int len;
1823 const unsigned char *p = (const unsigned char *) s;
1824
1825 for (nchars = 0; rest > 0; ++nchars)
1826 {
1827 string_char_and_length (p, &len);
1828 rest -= len, p += len;
1829 }
1830 }
1831 else
1832 nchars = strlen (s);
1833
1834 return nchars;
1835 }
1836
1837
1838 /* Compute byte position NEWPOS->bytepos corresponding to
1839 NEWPOS->charpos. POS is a known position in string STRING.
1840 NEWPOS->charpos must be >= POS.charpos. */
1841
1842 static void
1843 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1844 {
1845 eassert (STRINGP (string));
1846 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1847
1848 if (STRING_MULTIBYTE (string))
1849 *newpos = string_pos_nchars_ahead (pos, string,
1850 CHARPOS (*newpos) - CHARPOS (pos));
1851 else
1852 BYTEPOS (*newpos) = CHARPOS (*newpos);
1853 }
1854
1855 /* EXPORT:
1856 Return an estimation of the pixel height of mode or header lines on
1857 frame F. FACE_ID specifies what line's height to estimate. */
1858
1859 int
1860 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1861 {
1862 #ifdef HAVE_WINDOW_SYSTEM
1863 if (FRAME_WINDOW_P (f))
1864 {
1865 int height = FONT_HEIGHT (FRAME_FONT (f));
1866
1867 /* This function is called so early when Emacs starts that the face
1868 cache and mode line face are not yet initialized. */
1869 if (FRAME_FACE_CACHE (f))
1870 {
1871 struct face *face = FACE_FROM_ID (f, face_id);
1872 if (face)
1873 {
1874 if (face->font)
1875 height = FONT_HEIGHT (face->font);
1876 if (face->box_line_width > 0)
1877 height += 2 * face->box_line_width;
1878 }
1879 }
1880
1881 return height;
1882 }
1883 #endif
1884
1885 return 1;
1886 }
1887
1888 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1889 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1890 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1891 not force the value into range. */
1892
1893 void
1894 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1895 int *x, int *y, NativeRectangle *bounds, int noclip)
1896 {
1897
1898 #ifdef HAVE_WINDOW_SYSTEM
1899 if (FRAME_WINDOW_P (f))
1900 {
1901 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1902 even for negative values. */
1903 if (pix_x < 0)
1904 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1905 if (pix_y < 0)
1906 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1907
1908 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1909 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1910
1911 if (bounds)
1912 STORE_NATIVE_RECT (*bounds,
1913 FRAME_COL_TO_PIXEL_X (f, pix_x),
1914 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1915 FRAME_COLUMN_WIDTH (f) - 1,
1916 FRAME_LINE_HEIGHT (f) - 1);
1917
1918 /* PXW: Should we clip pixels before converting to columns/lines? */
1919 if (!noclip)
1920 {
1921 if (pix_x < 0)
1922 pix_x = 0;
1923 else if (pix_x > FRAME_TOTAL_COLS (f))
1924 pix_x = FRAME_TOTAL_COLS (f);
1925
1926 if (pix_y < 0)
1927 pix_y = 0;
1928 else if (pix_y > FRAME_LINES (f))
1929 pix_y = FRAME_LINES (f);
1930 }
1931 }
1932 #endif
1933
1934 *x = pix_x;
1935 *y = pix_y;
1936 }
1937
1938
1939 /* Find the glyph under window-relative coordinates X/Y in window W.
1940 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1941 strings. Return in *HPOS and *VPOS the row and column number of
1942 the glyph found. Return in *AREA the glyph area containing X.
1943 Value is a pointer to the glyph found or null if X/Y is not on
1944 text, or we can't tell because W's current matrix is not up to
1945 date. */
1946
1947 static struct glyph *
1948 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1949 int *dx, int *dy, int *area)
1950 {
1951 struct glyph *glyph, *end;
1952 struct glyph_row *row = NULL;
1953 int x0, i;
1954
1955 /* Find row containing Y. Give up if some row is not enabled. */
1956 for (i = 0; i < w->current_matrix->nrows; ++i)
1957 {
1958 row = MATRIX_ROW (w->current_matrix, i);
1959 if (!row->enabled_p)
1960 return NULL;
1961 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1962 break;
1963 }
1964
1965 *vpos = i;
1966 *hpos = 0;
1967
1968 /* Give up if Y is not in the window. */
1969 if (i == w->current_matrix->nrows)
1970 return NULL;
1971
1972 /* Get the glyph area containing X. */
1973 if (w->pseudo_window_p)
1974 {
1975 *area = TEXT_AREA;
1976 x0 = 0;
1977 }
1978 else
1979 {
1980 if (x < window_box_left_offset (w, TEXT_AREA))
1981 {
1982 *area = LEFT_MARGIN_AREA;
1983 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1984 }
1985 else if (x < window_box_right_offset (w, TEXT_AREA))
1986 {
1987 *area = TEXT_AREA;
1988 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1989 }
1990 else
1991 {
1992 *area = RIGHT_MARGIN_AREA;
1993 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1994 }
1995 }
1996
1997 /* Find glyph containing X. */
1998 glyph = row->glyphs[*area];
1999 end = glyph + row->used[*area];
2000 x -= x0;
2001 while (glyph < end && x >= glyph->pixel_width)
2002 {
2003 x -= glyph->pixel_width;
2004 ++glyph;
2005 }
2006
2007 if (glyph == end)
2008 return NULL;
2009
2010 if (dx)
2011 {
2012 *dx = x;
2013 *dy = y - (row->y + row->ascent - glyph->ascent);
2014 }
2015
2016 *hpos = glyph - row->glyphs[*area];
2017 return glyph;
2018 }
2019
2020 /* Convert frame-relative x/y to coordinates relative to window W.
2021 Takes pseudo-windows into account. */
2022
2023 static void
2024 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2025 {
2026 if (w->pseudo_window_p)
2027 {
2028 /* A pseudo-window is always full-width, and starts at the
2029 left edge of the frame, plus a frame border. */
2030 struct frame *f = XFRAME (w->frame);
2031 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2032 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2033 }
2034 else
2035 {
2036 *x -= WINDOW_LEFT_EDGE_X (w);
2037 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2038 }
2039 }
2040
2041 #ifdef HAVE_WINDOW_SYSTEM
2042
2043 /* EXPORT:
2044 Return in RECTS[] at most N clipping rectangles for glyph string S.
2045 Return the number of stored rectangles. */
2046
2047 int
2048 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2049 {
2050 XRectangle r;
2051
2052 if (n <= 0)
2053 return 0;
2054
2055 if (s->row->full_width_p)
2056 {
2057 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2058 r.x = WINDOW_LEFT_EDGE_X (s->w);
2059 if (s->row->mode_line_p)
2060 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2061 else
2062 r.width = WINDOW_PIXEL_WIDTH (s->w);
2063
2064 /* Unless displaying a mode or menu bar line, which are always
2065 fully visible, clip to the visible part of the row. */
2066 if (s->w->pseudo_window_p)
2067 r.height = s->row->visible_height;
2068 else
2069 r.height = s->height;
2070 }
2071 else
2072 {
2073 /* This is a text line that may be partially visible. */
2074 r.x = window_box_left (s->w, s->area);
2075 r.width = window_box_width (s->w, s->area);
2076 r.height = s->row->visible_height;
2077 }
2078
2079 if (s->clip_head)
2080 if (r.x < s->clip_head->x)
2081 {
2082 if (r.width >= s->clip_head->x - r.x)
2083 r.width -= s->clip_head->x - r.x;
2084 else
2085 r.width = 0;
2086 r.x = s->clip_head->x;
2087 }
2088 if (s->clip_tail)
2089 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2090 {
2091 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2092 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2093 else
2094 r.width = 0;
2095 }
2096
2097 /* If S draws overlapping rows, it's sufficient to use the top and
2098 bottom of the window for clipping because this glyph string
2099 intentionally draws over other lines. */
2100 if (s->for_overlaps)
2101 {
2102 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2103 r.height = window_text_bottom_y (s->w) - r.y;
2104
2105 /* Alas, the above simple strategy does not work for the
2106 environments with anti-aliased text: if the same text is
2107 drawn onto the same place multiple times, it gets thicker.
2108 If the overlap we are processing is for the erased cursor, we
2109 take the intersection with the rectangle of the cursor. */
2110 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2111 {
2112 XRectangle rc, r_save = r;
2113
2114 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2115 rc.y = s->w->phys_cursor.y;
2116 rc.width = s->w->phys_cursor_width;
2117 rc.height = s->w->phys_cursor_height;
2118
2119 x_intersect_rectangles (&r_save, &rc, &r);
2120 }
2121 }
2122 else
2123 {
2124 /* Don't use S->y for clipping because it doesn't take partially
2125 visible lines into account. For example, it can be negative for
2126 partially visible lines at the top of a window. */
2127 if (!s->row->full_width_p
2128 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2129 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2130 else
2131 r.y = max (0, s->row->y);
2132 }
2133
2134 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2135
2136 /* If drawing the cursor, don't let glyph draw outside its
2137 advertised boundaries. Cleartype does this under some circumstances. */
2138 if (s->hl == DRAW_CURSOR)
2139 {
2140 struct glyph *glyph = s->first_glyph;
2141 int height, max_y;
2142
2143 if (s->x > r.x)
2144 {
2145 r.width -= s->x - r.x;
2146 r.x = s->x;
2147 }
2148 r.width = min (r.width, glyph->pixel_width);
2149
2150 /* If r.y is below window bottom, ensure that we still see a cursor. */
2151 height = min (glyph->ascent + glyph->descent,
2152 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2153 max_y = window_text_bottom_y (s->w) - height;
2154 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2155 if (s->ybase - glyph->ascent > max_y)
2156 {
2157 r.y = max_y;
2158 r.height = height;
2159 }
2160 else
2161 {
2162 /* Don't draw cursor glyph taller than our actual glyph. */
2163 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2164 if (height < r.height)
2165 {
2166 max_y = r.y + r.height;
2167 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2168 r.height = min (max_y - r.y, height);
2169 }
2170 }
2171 }
2172
2173 if (s->row->clip)
2174 {
2175 XRectangle r_save = r;
2176
2177 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2178 r.width = 0;
2179 }
2180
2181 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2182 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2183 {
2184 #ifdef CONVERT_FROM_XRECT
2185 CONVERT_FROM_XRECT (r, *rects);
2186 #else
2187 *rects = r;
2188 #endif
2189 return 1;
2190 }
2191 else
2192 {
2193 /* If we are processing overlapping and allowed to return
2194 multiple clipping rectangles, we exclude the row of the glyph
2195 string from the clipping rectangle. This is to avoid drawing
2196 the same text on the environment with anti-aliasing. */
2197 #ifdef CONVERT_FROM_XRECT
2198 XRectangle rs[2];
2199 #else
2200 XRectangle *rs = rects;
2201 #endif
2202 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2203
2204 if (s->for_overlaps & OVERLAPS_PRED)
2205 {
2206 rs[i] = r;
2207 if (r.y + r.height > row_y)
2208 {
2209 if (r.y < row_y)
2210 rs[i].height = row_y - r.y;
2211 else
2212 rs[i].height = 0;
2213 }
2214 i++;
2215 }
2216 if (s->for_overlaps & OVERLAPS_SUCC)
2217 {
2218 rs[i] = r;
2219 if (r.y < row_y + s->row->visible_height)
2220 {
2221 if (r.y + r.height > row_y + s->row->visible_height)
2222 {
2223 rs[i].y = row_y + s->row->visible_height;
2224 rs[i].height = r.y + r.height - rs[i].y;
2225 }
2226 else
2227 rs[i].height = 0;
2228 }
2229 i++;
2230 }
2231
2232 n = i;
2233 #ifdef CONVERT_FROM_XRECT
2234 for (i = 0; i < n; i++)
2235 CONVERT_FROM_XRECT (rs[i], rects[i]);
2236 #endif
2237 return n;
2238 }
2239 }
2240
2241 /* EXPORT:
2242 Return in *NR the clipping rectangle for glyph string S. */
2243
2244 void
2245 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2246 {
2247 get_glyph_string_clip_rects (s, nr, 1);
2248 }
2249
2250
2251 /* EXPORT:
2252 Return the position and height of the phys cursor in window W.
2253 Set w->phys_cursor_width to width of phys cursor.
2254 */
2255
2256 void
2257 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2258 struct glyph *glyph, int *xp, int *yp, int *heightp)
2259 {
2260 struct frame *f = XFRAME (WINDOW_FRAME (w));
2261 int x, y, wd, h, h0, y0;
2262
2263 /* Compute the width of the rectangle to draw. If on a stretch
2264 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2265 rectangle as wide as the glyph, but use a canonical character
2266 width instead. */
2267 wd = glyph->pixel_width - 1;
2268 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2269 wd++; /* Why? */
2270 #endif
2271
2272 x = w->phys_cursor.x;
2273 if (x < 0)
2274 {
2275 wd += x;
2276 x = 0;
2277 }
2278
2279 if (glyph->type == STRETCH_GLYPH
2280 && !x_stretch_cursor_p)
2281 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2282 w->phys_cursor_width = wd;
2283
2284 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2285
2286 /* If y is below window bottom, ensure that we still see a cursor. */
2287 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2288
2289 h = max (h0, glyph->ascent + glyph->descent);
2290 h0 = min (h0, glyph->ascent + glyph->descent);
2291
2292 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2293 if (y < y0)
2294 {
2295 h = max (h - (y0 - y) + 1, h0);
2296 y = y0 - 1;
2297 }
2298 else
2299 {
2300 y0 = window_text_bottom_y (w) - h0;
2301 if (y > y0)
2302 {
2303 h += y - y0;
2304 y = y0;
2305 }
2306 }
2307
2308 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2309 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2310 *heightp = h;
2311 }
2312
2313 /*
2314 * Remember which glyph the mouse is over.
2315 */
2316
2317 void
2318 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2319 {
2320 Lisp_Object window;
2321 struct window *w;
2322 struct glyph_row *r, *gr, *end_row;
2323 enum window_part part;
2324 enum glyph_row_area area;
2325 int x, y, width, height;
2326
2327 /* Try to determine frame pixel position and size of the glyph under
2328 frame pixel coordinates X/Y on frame F. */
2329
2330 if (window_resize_pixelwise)
2331 {
2332 width = height = 1;
2333 goto virtual_glyph;
2334 }
2335 else if (!f->glyphs_initialized_p
2336 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2337 NILP (window)))
2338 {
2339 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2340 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2341 goto virtual_glyph;
2342 }
2343
2344 w = XWINDOW (window);
2345 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2346 height = WINDOW_FRAME_LINE_HEIGHT (w);
2347
2348 x = window_relative_x_coord (w, part, gx);
2349 y = gy - WINDOW_TOP_EDGE_Y (w);
2350
2351 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2352 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2353
2354 if (w->pseudo_window_p)
2355 {
2356 area = TEXT_AREA;
2357 part = ON_MODE_LINE; /* Don't adjust margin. */
2358 goto text_glyph;
2359 }
2360
2361 switch (part)
2362 {
2363 case ON_LEFT_MARGIN:
2364 area = LEFT_MARGIN_AREA;
2365 goto text_glyph;
2366
2367 case ON_RIGHT_MARGIN:
2368 area = RIGHT_MARGIN_AREA;
2369 goto text_glyph;
2370
2371 case ON_HEADER_LINE:
2372 case ON_MODE_LINE:
2373 gr = (part == ON_HEADER_LINE
2374 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2375 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2376 gy = gr->y;
2377 area = TEXT_AREA;
2378 goto text_glyph_row_found;
2379
2380 case ON_TEXT:
2381 area = TEXT_AREA;
2382
2383 text_glyph:
2384 gr = 0; gy = 0;
2385 for (; r <= end_row && r->enabled_p; ++r)
2386 if (r->y + r->height > y)
2387 {
2388 gr = r; gy = r->y;
2389 break;
2390 }
2391
2392 text_glyph_row_found:
2393 if (gr && gy <= y)
2394 {
2395 struct glyph *g = gr->glyphs[area];
2396 struct glyph *end = g + gr->used[area];
2397
2398 height = gr->height;
2399 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2400 if (gx + g->pixel_width > x)
2401 break;
2402
2403 if (g < end)
2404 {
2405 if (g->type == IMAGE_GLYPH)
2406 {
2407 /* Don't remember when mouse is over image, as
2408 image may have hot-spots. */
2409 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2410 return;
2411 }
2412 width = g->pixel_width;
2413 }
2414 else
2415 {
2416 /* Use nominal char spacing at end of line. */
2417 x -= gx;
2418 gx += (x / width) * width;
2419 }
2420
2421 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2422 gx += window_box_left_offset (w, area);
2423 }
2424 else
2425 {
2426 /* Use nominal line height at end of window. */
2427 gx = (x / width) * width;
2428 y -= gy;
2429 gy += (y / height) * height;
2430 }
2431 break;
2432
2433 case ON_LEFT_FRINGE:
2434 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2435 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2436 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2437 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2438 goto row_glyph;
2439
2440 case ON_RIGHT_FRINGE:
2441 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2442 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2443 : window_box_right_offset (w, TEXT_AREA));
2444 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2445 goto row_glyph;
2446
2447 case ON_SCROLL_BAR:
2448 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2449 ? 0
2450 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2451 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2452 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2453 : 0)));
2454 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2455
2456 row_glyph:
2457 gr = 0, gy = 0;
2458 for (; r <= end_row && r->enabled_p; ++r)
2459 if (r->y + r->height > y)
2460 {
2461 gr = r; gy = r->y;
2462 break;
2463 }
2464
2465 if (gr && gy <= y)
2466 height = gr->height;
2467 else
2468 {
2469 /* Use nominal line height at end of window. */
2470 y -= gy;
2471 gy += (y / height) * height;
2472 }
2473 break;
2474
2475 default:
2476 ;
2477 virtual_glyph:
2478 /* If there is no glyph under the mouse, then we divide the screen
2479 into a grid of the smallest glyph in the frame, and use that
2480 as our "glyph". */
2481
2482 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2483 round down even for negative values. */
2484 if (gx < 0)
2485 gx -= width - 1;
2486 if (gy < 0)
2487 gy -= height - 1;
2488
2489 gx = (gx / width) * width;
2490 gy = (gy / height) * height;
2491
2492 goto store_rect;
2493 }
2494
2495 gx += WINDOW_LEFT_EDGE_X (w);
2496 gy += WINDOW_TOP_EDGE_Y (w);
2497
2498 store_rect:
2499 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2500
2501 /* Visible feedback for debugging. */
2502 #if 0
2503 #if HAVE_X_WINDOWS
2504 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2505 f->output_data.x->normal_gc,
2506 gx, gy, width, height);
2507 #endif
2508 #endif
2509 }
2510
2511
2512 #endif /* HAVE_WINDOW_SYSTEM */
2513
2514 static void
2515 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2516 {
2517 eassert (w);
2518 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2519 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2520 w->window_end_vpos
2521 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2522 }
2523
2524 /***********************************************************************
2525 Lisp form evaluation
2526 ***********************************************************************/
2527
2528 /* Error handler for safe_eval and safe_call. */
2529
2530 static Lisp_Object
2531 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2532 {
2533 add_to_log ("Error during redisplay: %S signaled %S",
2534 Flist (nargs, args), arg);
2535 return Qnil;
2536 }
2537
2538 /* Call function FUNC with the rest of NARGS - 1 arguments
2539 following. Return the result, or nil if something went
2540 wrong. Prevent redisplay during the evaluation. */
2541
2542 Lisp_Object
2543 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2544 {
2545 Lisp_Object val;
2546
2547 if (inhibit_eval_during_redisplay)
2548 val = Qnil;
2549 else
2550 {
2551 va_list ap;
2552 ptrdiff_t i;
2553 ptrdiff_t count = SPECPDL_INDEX ();
2554 struct gcpro gcpro1;
2555 Lisp_Object *args = alloca (nargs * word_size);
2556
2557 args[0] = func;
2558 va_start (ap, func);
2559 for (i = 1; i < nargs; i++)
2560 args[i] = va_arg (ap, Lisp_Object);
2561 va_end (ap);
2562
2563 GCPRO1 (args[0]);
2564 gcpro1.nvars = nargs;
2565 specbind (Qinhibit_redisplay, Qt);
2566 /* Use Qt to ensure debugger does not run,
2567 so there is no possibility of wanting to redisplay. */
2568 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2569 safe_eval_handler);
2570 UNGCPRO;
2571 val = unbind_to (count, val);
2572 }
2573
2574 return val;
2575 }
2576
2577
2578 /* Call function FN with one argument ARG.
2579 Return the result, or nil if something went wrong. */
2580
2581 Lisp_Object
2582 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2583 {
2584 return safe_call (2, fn, arg);
2585 }
2586
2587 static Lisp_Object Qeval;
2588
2589 Lisp_Object
2590 safe_eval (Lisp_Object sexpr)
2591 {
2592 return safe_call1 (Qeval, sexpr);
2593 }
2594
2595 /* Call function FN with two arguments ARG1 and ARG2.
2596 Return the result, or nil if something went wrong. */
2597
2598 Lisp_Object
2599 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2600 {
2601 return safe_call (3, fn, arg1, arg2);
2602 }
2603
2604
2605 \f
2606 /***********************************************************************
2607 Debugging
2608 ***********************************************************************/
2609
2610 #if 0
2611
2612 /* Define CHECK_IT to perform sanity checks on iterators.
2613 This is for debugging. It is too slow to do unconditionally. */
2614
2615 static void
2616 check_it (struct it *it)
2617 {
2618 if (it->method == GET_FROM_STRING)
2619 {
2620 eassert (STRINGP (it->string));
2621 eassert (IT_STRING_CHARPOS (*it) >= 0);
2622 }
2623 else
2624 {
2625 eassert (IT_STRING_CHARPOS (*it) < 0);
2626 if (it->method == GET_FROM_BUFFER)
2627 {
2628 /* Check that character and byte positions agree. */
2629 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2630 }
2631 }
2632
2633 if (it->dpvec)
2634 eassert (it->current.dpvec_index >= 0);
2635 else
2636 eassert (it->current.dpvec_index < 0);
2637 }
2638
2639 #define CHECK_IT(IT) check_it ((IT))
2640
2641 #else /* not 0 */
2642
2643 #define CHECK_IT(IT) (void) 0
2644
2645 #endif /* not 0 */
2646
2647
2648 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2649
2650 /* Check that the window end of window W is what we expect it
2651 to be---the last row in the current matrix displaying text. */
2652
2653 static void
2654 check_window_end (struct window *w)
2655 {
2656 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2657 {
2658 struct glyph_row *row;
2659 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2660 !row->enabled_p
2661 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2662 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2663 }
2664 }
2665
2666 #define CHECK_WINDOW_END(W) check_window_end ((W))
2667
2668 #else
2669
2670 #define CHECK_WINDOW_END(W) (void) 0
2671
2672 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2673
2674 /***********************************************************************
2675 Iterator initialization
2676 ***********************************************************************/
2677
2678 /* Initialize IT for displaying current_buffer in window W, starting
2679 at character position CHARPOS. CHARPOS < 0 means that no buffer
2680 position is specified which is useful when the iterator is assigned
2681 a position later. BYTEPOS is the byte position corresponding to
2682 CHARPOS.
2683
2684 If ROW is not null, calls to produce_glyphs with IT as parameter
2685 will produce glyphs in that row.
2686
2687 BASE_FACE_ID is the id of a base face to use. It must be one of
2688 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2689 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2690 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2691
2692 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2693 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2694 will be initialized to use the corresponding mode line glyph row of
2695 the desired matrix of W. */
2696
2697 void
2698 init_iterator (struct it *it, struct window *w,
2699 ptrdiff_t charpos, ptrdiff_t bytepos,
2700 struct glyph_row *row, enum face_id base_face_id)
2701 {
2702 enum face_id remapped_base_face_id = base_face_id;
2703
2704 /* Some precondition checks. */
2705 eassert (w != NULL && it != NULL);
2706 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2707 && charpos <= ZV));
2708
2709 /* If face attributes have been changed since the last redisplay,
2710 free realized faces now because they depend on face definitions
2711 that might have changed. Don't free faces while there might be
2712 desired matrices pending which reference these faces. */
2713 if (face_change_count && !inhibit_free_realized_faces)
2714 {
2715 face_change_count = 0;
2716 free_all_realized_faces (Qnil);
2717 }
2718
2719 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2720 if (! NILP (Vface_remapping_alist))
2721 remapped_base_face_id
2722 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2723
2724 /* Use one of the mode line rows of W's desired matrix if
2725 appropriate. */
2726 if (row == NULL)
2727 {
2728 if (base_face_id == MODE_LINE_FACE_ID
2729 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2730 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2731 else if (base_face_id == HEADER_LINE_FACE_ID)
2732 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2733 }
2734
2735 /* Clear IT. */
2736 memset (it, 0, sizeof *it);
2737 it->current.overlay_string_index = -1;
2738 it->current.dpvec_index = -1;
2739 it->base_face_id = remapped_base_face_id;
2740 it->string = Qnil;
2741 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2742 it->paragraph_embedding = L2R;
2743 it->bidi_it.string.lstring = Qnil;
2744 it->bidi_it.string.s = NULL;
2745 it->bidi_it.string.bufpos = 0;
2746 it->bidi_it.w = w;
2747
2748 /* The window in which we iterate over current_buffer: */
2749 XSETWINDOW (it->window, w);
2750 it->w = w;
2751 it->f = XFRAME (w->frame);
2752
2753 it->cmp_it.id = -1;
2754
2755 /* Extra space between lines (on window systems only). */
2756 if (base_face_id == DEFAULT_FACE_ID
2757 && FRAME_WINDOW_P (it->f))
2758 {
2759 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2760 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2761 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2762 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2763 * FRAME_LINE_HEIGHT (it->f));
2764 else if (it->f->extra_line_spacing > 0)
2765 it->extra_line_spacing = it->f->extra_line_spacing;
2766 it->max_extra_line_spacing = 0;
2767 }
2768
2769 /* If realized faces have been removed, e.g. because of face
2770 attribute changes of named faces, recompute them. When running
2771 in batch mode, the face cache of the initial frame is null. If
2772 we happen to get called, make a dummy face cache. */
2773 if (FRAME_FACE_CACHE (it->f) == NULL)
2774 init_frame_faces (it->f);
2775 if (FRAME_FACE_CACHE (it->f)->used == 0)
2776 recompute_basic_faces (it->f);
2777
2778 /* Current value of the `slice', `space-width', and 'height' properties. */
2779 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2780 it->space_width = Qnil;
2781 it->font_height = Qnil;
2782 it->override_ascent = -1;
2783
2784 /* Are control characters displayed as `^C'? */
2785 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2786
2787 /* -1 means everything between a CR and the following line end
2788 is invisible. >0 means lines indented more than this value are
2789 invisible. */
2790 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2791 ? (clip_to_bounds
2792 (-1, XINT (BVAR (current_buffer, selective_display)),
2793 PTRDIFF_MAX))
2794 : (!NILP (BVAR (current_buffer, selective_display))
2795 ? -1 : 0));
2796 it->selective_display_ellipsis_p
2797 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2798
2799 /* Display table to use. */
2800 it->dp = window_display_table (w);
2801
2802 /* Are multibyte characters enabled in current_buffer? */
2803 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2804
2805 /* Get the position at which the redisplay_end_trigger hook should
2806 be run, if it is to be run at all. */
2807 if (MARKERP (w->redisplay_end_trigger)
2808 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2809 it->redisplay_end_trigger_charpos
2810 = marker_position (w->redisplay_end_trigger);
2811 else if (INTEGERP (w->redisplay_end_trigger))
2812 it->redisplay_end_trigger_charpos
2813 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2814 PTRDIFF_MAX);
2815
2816 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2817
2818 /* Are lines in the display truncated? */
2819 if (base_face_id != DEFAULT_FACE_ID
2820 || it->w->hscroll
2821 || (! WINDOW_FULL_WIDTH_P (it->w)
2822 && ((!NILP (Vtruncate_partial_width_windows)
2823 && !INTEGERP (Vtruncate_partial_width_windows))
2824 || (INTEGERP (Vtruncate_partial_width_windows)
2825 /* PXW: Shall we do something about this? */
2826 && (WINDOW_TOTAL_COLS (it->w)
2827 < XINT (Vtruncate_partial_width_windows))))))
2828 it->line_wrap = TRUNCATE;
2829 else if (NILP (BVAR (current_buffer, truncate_lines)))
2830 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2831 ? WINDOW_WRAP : WORD_WRAP;
2832 else
2833 it->line_wrap = TRUNCATE;
2834
2835 /* Get dimensions of truncation and continuation glyphs. These are
2836 displayed as fringe bitmaps under X, but we need them for such
2837 frames when the fringes are turned off. But leave the dimensions
2838 zero for tooltip frames, as these glyphs look ugly there and also
2839 sabotage calculations of tooltip dimensions in x-show-tip. */
2840 #ifdef HAVE_WINDOW_SYSTEM
2841 if (!(FRAME_WINDOW_P (it->f)
2842 && FRAMEP (tip_frame)
2843 && it->f == XFRAME (tip_frame)))
2844 #endif
2845 {
2846 if (it->line_wrap == TRUNCATE)
2847 {
2848 /* We will need the truncation glyph. */
2849 eassert (it->glyph_row == NULL);
2850 produce_special_glyphs (it, IT_TRUNCATION);
2851 it->truncation_pixel_width = it->pixel_width;
2852 }
2853 else
2854 {
2855 /* We will need the continuation glyph. */
2856 eassert (it->glyph_row == NULL);
2857 produce_special_glyphs (it, IT_CONTINUATION);
2858 it->continuation_pixel_width = it->pixel_width;
2859 }
2860 }
2861
2862 /* Reset these values to zero because the produce_special_glyphs
2863 above has changed them. */
2864 it->pixel_width = it->ascent = it->descent = 0;
2865 it->phys_ascent = it->phys_descent = 0;
2866
2867 /* Set this after getting the dimensions of truncation and
2868 continuation glyphs, so that we don't produce glyphs when calling
2869 produce_special_glyphs, above. */
2870 it->glyph_row = row;
2871 it->area = TEXT_AREA;
2872
2873 /* Forget any previous info about this row being reversed. */
2874 if (it->glyph_row)
2875 it->glyph_row->reversed_p = 0;
2876
2877 /* Get the dimensions of the display area. The display area
2878 consists of the visible window area plus a horizontally scrolled
2879 part to the left of the window. All x-values are relative to the
2880 start of this total display area. */
2881 if (base_face_id != DEFAULT_FACE_ID)
2882 {
2883 /* Mode lines, menu bar in terminal frames. */
2884 it->first_visible_x = 0;
2885 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2886 }
2887 else
2888 {
2889 it->first_visible_x
2890 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2891 it->last_visible_x = (it->first_visible_x
2892 + window_box_width (w, TEXT_AREA));
2893
2894 /* If we truncate lines, leave room for the truncation glyph(s) at
2895 the right margin. Otherwise, leave room for the continuation
2896 glyph(s). Done only if the window has no fringes. Since we
2897 don't know at this point whether there will be any R2L lines in
2898 the window, we reserve space for truncation/continuation glyphs
2899 even if only one of the fringes is absent. */
2900 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2901 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2902 {
2903 if (it->line_wrap == TRUNCATE)
2904 it->last_visible_x -= it->truncation_pixel_width;
2905 else
2906 it->last_visible_x -= it->continuation_pixel_width;
2907 }
2908
2909 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2910 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2911 }
2912
2913 /* Leave room for a border glyph. */
2914 if (!FRAME_WINDOW_P (it->f)
2915 && !WINDOW_RIGHTMOST_P (it->w))
2916 it->last_visible_x -= 1;
2917
2918 it->last_visible_y = window_text_bottom_y (w);
2919
2920 /* For mode lines and alike, arrange for the first glyph having a
2921 left box line if the face specifies a box. */
2922 if (base_face_id != DEFAULT_FACE_ID)
2923 {
2924 struct face *face;
2925
2926 it->face_id = remapped_base_face_id;
2927
2928 /* If we have a boxed mode line, make the first character appear
2929 with a left box line. */
2930 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2931 if (face->box != FACE_NO_BOX)
2932 it->start_of_box_run_p = true;
2933 }
2934
2935 /* If a buffer position was specified, set the iterator there,
2936 getting overlays and face properties from that position. */
2937 if (charpos >= BUF_BEG (current_buffer))
2938 {
2939 it->end_charpos = ZV;
2940 eassert (charpos == BYTE_TO_CHAR (bytepos));
2941 IT_CHARPOS (*it) = charpos;
2942 IT_BYTEPOS (*it) = bytepos;
2943
2944 /* We will rely on `reseat' to set this up properly, via
2945 handle_face_prop. */
2946 it->face_id = it->base_face_id;
2947
2948 it->start = it->current;
2949 /* Do we need to reorder bidirectional text? Not if this is a
2950 unibyte buffer: by definition, none of the single-byte
2951 characters are strong R2L, so no reordering is needed. And
2952 bidi.c doesn't support unibyte buffers anyway. Also, don't
2953 reorder while we are loading loadup.el, since the tables of
2954 character properties needed for reordering are not yet
2955 available. */
2956 it->bidi_p =
2957 NILP (Vpurify_flag)
2958 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2959 && it->multibyte_p;
2960
2961 /* If we are to reorder bidirectional text, init the bidi
2962 iterator. */
2963 if (it->bidi_p)
2964 {
2965 /* Note the paragraph direction that this buffer wants to
2966 use. */
2967 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2968 Qleft_to_right))
2969 it->paragraph_embedding = L2R;
2970 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2971 Qright_to_left))
2972 it->paragraph_embedding = R2L;
2973 else
2974 it->paragraph_embedding = NEUTRAL_DIR;
2975 bidi_unshelve_cache (NULL, 0);
2976 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2977 &it->bidi_it);
2978 }
2979
2980 /* Compute faces etc. */
2981 reseat (it, it->current.pos, 1);
2982 }
2983
2984 CHECK_IT (it);
2985 }
2986
2987
2988 /* Initialize IT for the display of window W with window start POS. */
2989
2990 void
2991 start_display (struct it *it, struct window *w, struct text_pos pos)
2992 {
2993 struct glyph_row *row;
2994 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2995
2996 row = w->desired_matrix->rows + first_vpos;
2997 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2998 it->first_vpos = first_vpos;
2999
3000 /* Don't reseat to previous visible line start if current start
3001 position is in a string or image. */
3002 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3003 {
3004 int start_at_line_beg_p;
3005 int first_y = it->current_y;
3006
3007 /* If window start is not at a line start, skip forward to POS to
3008 get the correct continuation lines width. */
3009 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3010 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3011 if (!start_at_line_beg_p)
3012 {
3013 int new_x;
3014
3015 reseat_at_previous_visible_line_start (it);
3016 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3017
3018 new_x = it->current_x + it->pixel_width;
3019
3020 /* If lines are continued, this line may end in the middle
3021 of a multi-glyph character (e.g. a control character
3022 displayed as \003, or in the middle of an overlay
3023 string). In this case move_it_to above will not have
3024 taken us to the start of the continuation line but to the
3025 end of the continued line. */
3026 if (it->current_x > 0
3027 && it->line_wrap != TRUNCATE /* Lines are continued. */
3028 && (/* And glyph doesn't fit on the line. */
3029 new_x > it->last_visible_x
3030 /* Or it fits exactly and we're on a window
3031 system frame. */
3032 || (new_x == it->last_visible_x
3033 && FRAME_WINDOW_P (it->f)
3034 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3035 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3036 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3037 {
3038 if ((it->current.dpvec_index >= 0
3039 || it->current.overlay_string_index >= 0)
3040 /* If we are on a newline from a display vector or
3041 overlay string, then we are already at the end of
3042 a screen line; no need to go to the next line in
3043 that case, as this line is not really continued.
3044 (If we do go to the next line, C-e will not DTRT.) */
3045 && it->c != '\n')
3046 {
3047 set_iterator_to_next (it, 1);
3048 move_it_in_display_line_to (it, -1, -1, 0);
3049 }
3050
3051 it->continuation_lines_width += it->current_x;
3052 }
3053 /* If the character at POS is displayed via a display
3054 vector, move_it_to above stops at the final glyph of
3055 IT->dpvec. To make the caller redisplay that character
3056 again (a.k.a. start at POS), we need to reset the
3057 dpvec_index to the beginning of IT->dpvec. */
3058 else if (it->current.dpvec_index >= 0)
3059 it->current.dpvec_index = 0;
3060
3061 /* We're starting a new display line, not affected by the
3062 height of the continued line, so clear the appropriate
3063 fields in the iterator structure. */
3064 it->max_ascent = it->max_descent = 0;
3065 it->max_phys_ascent = it->max_phys_descent = 0;
3066
3067 it->current_y = first_y;
3068 it->vpos = 0;
3069 it->current_x = it->hpos = 0;
3070 }
3071 }
3072 }
3073
3074
3075 /* Return 1 if POS is a position in ellipses displayed for invisible
3076 text. W is the window we display, for text property lookup. */
3077
3078 static int
3079 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3080 {
3081 Lisp_Object prop, window;
3082 int ellipses_p = 0;
3083 ptrdiff_t charpos = CHARPOS (pos->pos);
3084
3085 /* If POS specifies a position in a display vector, this might
3086 be for an ellipsis displayed for invisible text. We won't
3087 get the iterator set up for delivering that ellipsis unless
3088 we make sure that it gets aware of the invisible text. */
3089 if (pos->dpvec_index >= 0
3090 && pos->overlay_string_index < 0
3091 && CHARPOS (pos->string_pos) < 0
3092 && charpos > BEGV
3093 && (XSETWINDOW (window, w),
3094 prop = Fget_char_property (make_number (charpos),
3095 Qinvisible, window),
3096 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3097 {
3098 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3099 window);
3100 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3101 }
3102
3103 return ellipses_p;
3104 }
3105
3106
3107 /* Initialize IT for stepping through current_buffer in window W,
3108 starting at position POS that includes overlay string and display
3109 vector/ control character translation position information. Value
3110 is zero if there are overlay strings with newlines at POS. */
3111
3112 static int
3113 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3114 {
3115 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3116 int i, overlay_strings_with_newlines = 0;
3117
3118 /* If POS specifies a position in a display vector, this might
3119 be for an ellipsis displayed for invisible text. We won't
3120 get the iterator set up for delivering that ellipsis unless
3121 we make sure that it gets aware of the invisible text. */
3122 if (in_ellipses_for_invisible_text_p (pos, w))
3123 {
3124 --charpos;
3125 bytepos = 0;
3126 }
3127
3128 /* Keep in mind: the call to reseat in init_iterator skips invisible
3129 text, so we might end up at a position different from POS. This
3130 is only a problem when POS is a row start after a newline and an
3131 overlay starts there with an after-string, and the overlay has an
3132 invisible property. Since we don't skip invisible text in
3133 display_line and elsewhere immediately after consuming the
3134 newline before the row start, such a POS will not be in a string,
3135 but the call to init_iterator below will move us to the
3136 after-string. */
3137 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3138
3139 /* This only scans the current chunk -- it should scan all chunks.
3140 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3141 to 16 in 22.1 to make this a lesser problem. */
3142 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3143 {
3144 const char *s = SSDATA (it->overlay_strings[i]);
3145 const char *e = s + SBYTES (it->overlay_strings[i]);
3146
3147 while (s < e && *s != '\n')
3148 ++s;
3149
3150 if (s < e)
3151 {
3152 overlay_strings_with_newlines = 1;
3153 break;
3154 }
3155 }
3156
3157 /* If position is within an overlay string, set up IT to the right
3158 overlay string. */
3159 if (pos->overlay_string_index >= 0)
3160 {
3161 int relative_index;
3162
3163 /* If the first overlay string happens to have a `display'
3164 property for an image, the iterator will be set up for that
3165 image, and we have to undo that setup first before we can
3166 correct the overlay string index. */
3167 if (it->method == GET_FROM_IMAGE)
3168 pop_it (it);
3169
3170 /* We already have the first chunk of overlay strings in
3171 IT->overlay_strings. Load more until the one for
3172 pos->overlay_string_index is in IT->overlay_strings. */
3173 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3174 {
3175 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3176 it->current.overlay_string_index = 0;
3177 while (n--)
3178 {
3179 load_overlay_strings (it, 0);
3180 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3181 }
3182 }
3183
3184 it->current.overlay_string_index = pos->overlay_string_index;
3185 relative_index = (it->current.overlay_string_index
3186 % OVERLAY_STRING_CHUNK_SIZE);
3187 it->string = it->overlay_strings[relative_index];
3188 eassert (STRINGP (it->string));
3189 it->current.string_pos = pos->string_pos;
3190 it->method = GET_FROM_STRING;
3191 it->end_charpos = SCHARS (it->string);
3192 /* Set up the bidi iterator for this overlay string. */
3193 if (it->bidi_p)
3194 {
3195 it->bidi_it.string.lstring = it->string;
3196 it->bidi_it.string.s = NULL;
3197 it->bidi_it.string.schars = SCHARS (it->string);
3198 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3199 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3200 it->bidi_it.string.unibyte = !it->multibyte_p;
3201 it->bidi_it.w = it->w;
3202 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3203 FRAME_WINDOW_P (it->f), &it->bidi_it);
3204
3205 /* Synchronize the state of the bidi iterator with
3206 pos->string_pos. For any string position other than
3207 zero, this will be done automagically when we resume
3208 iteration over the string and get_visually_first_element
3209 is called. But if string_pos is zero, and the string is
3210 to be reordered for display, we need to resync manually,
3211 since it could be that the iteration state recorded in
3212 pos ended at string_pos of 0 moving backwards in string. */
3213 if (CHARPOS (pos->string_pos) == 0)
3214 {
3215 get_visually_first_element (it);
3216 if (IT_STRING_CHARPOS (*it) != 0)
3217 do {
3218 /* Paranoia. */
3219 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3220 bidi_move_to_visually_next (&it->bidi_it);
3221 } while (it->bidi_it.charpos != 0);
3222 }
3223 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3224 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3225 }
3226 }
3227
3228 if (CHARPOS (pos->string_pos) >= 0)
3229 {
3230 /* Recorded position is not in an overlay string, but in another
3231 string. This can only be a string from a `display' property.
3232 IT should already be filled with that string. */
3233 it->current.string_pos = pos->string_pos;
3234 eassert (STRINGP (it->string));
3235 if (it->bidi_p)
3236 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3237 FRAME_WINDOW_P (it->f), &it->bidi_it);
3238 }
3239
3240 /* Restore position in display vector translations, control
3241 character translations or ellipses. */
3242 if (pos->dpvec_index >= 0)
3243 {
3244 if (it->dpvec == NULL)
3245 get_next_display_element (it);
3246 eassert (it->dpvec && it->current.dpvec_index == 0);
3247 it->current.dpvec_index = pos->dpvec_index;
3248 }
3249
3250 CHECK_IT (it);
3251 return !overlay_strings_with_newlines;
3252 }
3253
3254
3255 /* Initialize IT for stepping through current_buffer in window W
3256 starting at ROW->start. */
3257
3258 static void
3259 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3260 {
3261 init_from_display_pos (it, w, &row->start);
3262 it->start = row->start;
3263 it->continuation_lines_width = row->continuation_lines_width;
3264 CHECK_IT (it);
3265 }
3266
3267
3268 /* Initialize IT for stepping through current_buffer in window W
3269 starting in the line following ROW, i.e. starting at ROW->end.
3270 Value is zero if there are overlay strings with newlines at ROW's
3271 end position. */
3272
3273 static int
3274 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3275 {
3276 int success = 0;
3277
3278 if (init_from_display_pos (it, w, &row->end))
3279 {
3280 if (row->continued_p)
3281 it->continuation_lines_width
3282 = row->continuation_lines_width + row->pixel_width;
3283 CHECK_IT (it);
3284 success = 1;
3285 }
3286
3287 return success;
3288 }
3289
3290
3291
3292 \f
3293 /***********************************************************************
3294 Text properties
3295 ***********************************************************************/
3296
3297 /* Called when IT reaches IT->stop_charpos. Handle text property and
3298 overlay changes. Set IT->stop_charpos to the next position where
3299 to stop. */
3300
3301 static void
3302 handle_stop (struct it *it)
3303 {
3304 enum prop_handled handled;
3305 int handle_overlay_change_p;
3306 struct props *p;
3307
3308 it->dpvec = NULL;
3309 it->current.dpvec_index = -1;
3310 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3311 it->ignore_overlay_strings_at_pos_p = 0;
3312 it->ellipsis_p = 0;
3313
3314 /* Use face of preceding text for ellipsis (if invisible) */
3315 if (it->selective_display_ellipsis_p)
3316 it->saved_face_id = it->face_id;
3317
3318 do
3319 {
3320 handled = HANDLED_NORMALLY;
3321
3322 /* Call text property handlers. */
3323 for (p = it_props; p->handler; ++p)
3324 {
3325 handled = p->handler (it);
3326
3327 if (handled == HANDLED_RECOMPUTE_PROPS)
3328 break;
3329 else if (handled == HANDLED_RETURN)
3330 {
3331 /* We still want to show before and after strings from
3332 overlays even if the actual buffer text is replaced. */
3333 if (!handle_overlay_change_p
3334 || it->sp > 1
3335 /* Don't call get_overlay_strings_1 if we already
3336 have overlay strings loaded, because doing so
3337 will load them again and push the iterator state
3338 onto the stack one more time, which is not
3339 expected by the rest of the code that processes
3340 overlay strings. */
3341 || (it->current.overlay_string_index < 0
3342 ? !get_overlay_strings_1 (it, 0, 0)
3343 : 0))
3344 {
3345 if (it->ellipsis_p)
3346 setup_for_ellipsis (it, 0);
3347 /* When handling a display spec, we might load an
3348 empty string. In that case, discard it here. We
3349 used to discard it in handle_single_display_spec,
3350 but that causes get_overlay_strings_1, above, to
3351 ignore overlay strings that we must check. */
3352 if (STRINGP (it->string) && !SCHARS (it->string))
3353 pop_it (it);
3354 return;
3355 }
3356 else if (STRINGP (it->string) && !SCHARS (it->string))
3357 pop_it (it);
3358 else
3359 {
3360 it->ignore_overlay_strings_at_pos_p = true;
3361 it->string_from_display_prop_p = 0;
3362 it->from_disp_prop_p = 0;
3363 handle_overlay_change_p = 0;
3364 }
3365 handled = HANDLED_RECOMPUTE_PROPS;
3366 break;
3367 }
3368 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3369 handle_overlay_change_p = 0;
3370 }
3371
3372 if (handled != HANDLED_RECOMPUTE_PROPS)
3373 {
3374 /* Don't check for overlay strings below when set to deliver
3375 characters from a display vector. */
3376 if (it->method == GET_FROM_DISPLAY_VECTOR)
3377 handle_overlay_change_p = 0;
3378
3379 /* Handle overlay changes.
3380 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3381 if it finds overlays. */
3382 if (handle_overlay_change_p)
3383 handled = handle_overlay_change (it);
3384 }
3385
3386 if (it->ellipsis_p)
3387 {
3388 setup_for_ellipsis (it, 0);
3389 break;
3390 }
3391 }
3392 while (handled == HANDLED_RECOMPUTE_PROPS);
3393
3394 /* Determine where to stop next. */
3395 if (handled == HANDLED_NORMALLY)
3396 compute_stop_pos (it);
3397 }
3398
3399
3400 /* Compute IT->stop_charpos from text property and overlay change
3401 information for IT's current position. */
3402
3403 static void
3404 compute_stop_pos (struct it *it)
3405 {
3406 register INTERVAL iv, next_iv;
3407 Lisp_Object object, limit, position;
3408 ptrdiff_t charpos, bytepos;
3409
3410 if (STRINGP (it->string))
3411 {
3412 /* Strings are usually short, so don't limit the search for
3413 properties. */
3414 it->stop_charpos = it->end_charpos;
3415 object = it->string;
3416 limit = Qnil;
3417 charpos = IT_STRING_CHARPOS (*it);
3418 bytepos = IT_STRING_BYTEPOS (*it);
3419 }
3420 else
3421 {
3422 ptrdiff_t pos;
3423
3424 /* If end_charpos is out of range for some reason, such as a
3425 misbehaving display function, rationalize it (Bug#5984). */
3426 if (it->end_charpos > ZV)
3427 it->end_charpos = ZV;
3428 it->stop_charpos = it->end_charpos;
3429
3430 /* If next overlay change is in front of the current stop pos
3431 (which is IT->end_charpos), stop there. Note: value of
3432 next_overlay_change is point-max if no overlay change
3433 follows. */
3434 charpos = IT_CHARPOS (*it);
3435 bytepos = IT_BYTEPOS (*it);
3436 pos = next_overlay_change (charpos);
3437 if (pos < it->stop_charpos)
3438 it->stop_charpos = pos;
3439
3440 /* Set up variables for computing the stop position from text
3441 property changes. */
3442 XSETBUFFER (object, current_buffer);
3443 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3444 }
3445
3446 /* Get the interval containing IT's position. Value is a null
3447 interval if there isn't such an interval. */
3448 position = make_number (charpos);
3449 iv = validate_interval_range (object, &position, &position, 0);
3450 if (iv)
3451 {
3452 Lisp_Object values_here[LAST_PROP_IDX];
3453 struct props *p;
3454
3455 /* Get properties here. */
3456 for (p = it_props; p->handler; ++p)
3457 values_here[p->idx] = textget (iv->plist, *p->name);
3458
3459 /* Look for an interval following iv that has different
3460 properties. */
3461 for (next_iv = next_interval (iv);
3462 (next_iv
3463 && (NILP (limit)
3464 || XFASTINT (limit) > next_iv->position));
3465 next_iv = next_interval (next_iv))
3466 {
3467 for (p = it_props; p->handler; ++p)
3468 {
3469 Lisp_Object new_value;
3470
3471 new_value = textget (next_iv->plist, *p->name);
3472 if (!EQ (values_here[p->idx], new_value))
3473 break;
3474 }
3475
3476 if (p->handler)
3477 break;
3478 }
3479
3480 if (next_iv)
3481 {
3482 if (INTEGERP (limit)
3483 && next_iv->position >= XFASTINT (limit))
3484 /* No text property change up to limit. */
3485 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3486 else
3487 /* Text properties change in next_iv. */
3488 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3489 }
3490 }
3491
3492 if (it->cmp_it.id < 0)
3493 {
3494 ptrdiff_t stoppos = it->end_charpos;
3495
3496 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3497 stoppos = -1;
3498 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3499 stoppos, it->string);
3500 }
3501
3502 eassert (STRINGP (it->string)
3503 || (it->stop_charpos >= BEGV
3504 && it->stop_charpos >= IT_CHARPOS (*it)));
3505 }
3506
3507
3508 /* Return the position of the next overlay change after POS in
3509 current_buffer. Value is point-max if no overlay change
3510 follows. This is like `next-overlay-change' but doesn't use
3511 xmalloc. */
3512
3513 static ptrdiff_t
3514 next_overlay_change (ptrdiff_t pos)
3515 {
3516 ptrdiff_t i, noverlays;
3517 ptrdiff_t endpos;
3518 Lisp_Object *overlays;
3519
3520 /* Get all overlays at the given position. */
3521 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3522
3523 /* If any of these overlays ends before endpos,
3524 use its ending point instead. */
3525 for (i = 0; i < noverlays; ++i)
3526 {
3527 Lisp_Object oend;
3528 ptrdiff_t oendpos;
3529
3530 oend = OVERLAY_END (overlays[i]);
3531 oendpos = OVERLAY_POSITION (oend);
3532 endpos = min (endpos, oendpos);
3533 }
3534
3535 return endpos;
3536 }
3537
3538 /* How many characters forward to search for a display property or
3539 display string. Searching too far forward makes the bidi display
3540 sluggish, especially in small windows. */
3541 #define MAX_DISP_SCAN 250
3542
3543 /* Return the character position of a display string at or after
3544 position specified by POSITION. If no display string exists at or
3545 after POSITION, return ZV. A display string is either an overlay
3546 with `display' property whose value is a string, or a `display'
3547 text property whose value is a string. STRING is data about the
3548 string to iterate; if STRING->lstring is nil, we are iterating a
3549 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3550 on a GUI frame. DISP_PROP is set to zero if we searched
3551 MAX_DISP_SCAN characters forward without finding any display
3552 strings, non-zero otherwise. It is set to 2 if the display string
3553 uses any kind of `(space ...)' spec that will produce a stretch of
3554 white space in the text area. */
3555 ptrdiff_t
3556 compute_display_string_pos (struct text_pos *position,
3557 struct bidi_string_data *string,
3558 struct window *w,
3559 int frame_window_p, int *disp_prop)
3560 {
3561 /* OBJECT = nil means current buffer. */
3562 Lisp_Object object, object1;
3563 Lisp_Object pos, spec, limpos;
3564 int string_p = (string && (STRINGP (string->lstring) || string->s));
3565 ptrdiff_t eob = string_p ? string->schars : ZV;
3566 ptrdiff_t begb = string_p ? 0 : BEGV;
3567 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3568 ptrdiff_t lim =
3569 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3570 struct text_pos tpos;
3571 int rv = 0;
3572
3573 if (string && STRINGP (string->lstring))
3574 object1 = object = string->lstring;
3575 else if (w && !string_p)
3576 {
3577 XSETWINDOW (object, w);
3578 object1 = Qnil;
3579 }
3580 else
3581 object1 = object = Qnil;
3582
3583 *disp_prop = 1;
3584
3585 if (charpos >= eob
3586 /* We don't support display properties whose values are strings
3587 that have display string properties. */
3588 || string->from_disp_str
3589 /* C strings cannot have display properties. */
3590 || (string->s && !STRINGP (object)))
3591 {
3592 *disp_prop = 0;
3593 return eob;
3594 }
3595
3596 /* If the character at CHARPOS is where the display string begins,
3597 return CHARPOS. */
3598 pos = make_number (charpos);
3599 if (STRINGP (object))
3600 bufpos = string->bufpos;
3601 else
3602 bufpos = charpos;
3603 tpos = *position;
3604 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3605 && (charpos <= begb
3606 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3607 object),
3608 spec))
3609 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3610 frame_window_p)))
3611 {
3612 if (rv == 2)
3613 *disp_prop = 2;
3614 return charpos;
3615 }
3616
3617 /* Look forward for the first character with a `display' property
3618 that will replace the underlying text when displayed. */
3619 limpos = make_number (lim);
3620 do {
3621 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3622 CHARPOS (tpos) = XFASTINT (pos);
3623 if (CHARPOS (tpos) >= lim)
3624 {
3625 *disp_prop = 0;
3626 break;
3627 }
3628 if (STRINGP (object))
3629 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3630 else
3631 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3632 spec = Fget_char_property (pos, Qdisplay, object);
3633 if (!STRINGP (object))
3634 bufpos = CHARPOS (tpos);
3635 } while (NILP (spec)
3636 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3637 bufpos, frame_window_p)));
3638 if (rv == 2)
3639 *disp_prop = 2;
3640
3641 return CHARPOS (tpos);
3642 }
3643
3644 /* Return the character position of the end of the display string that
3645 started at CHARPOS. If there's no display string at CHARPOS,
3646 return -1. A display string is either an overlay with `display'
3647 property whose value is a string or a `display' text property whose
3648 value is a string. */
3649 ptrdiff_t
3650 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3651 {
3652 /* OBJECT = nil means current buffer. */
3653 Lisp_Object object =
3654 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3655 Lisp_Object pos = make_number (charpos);
3656 ptrdiff_t eob =
3657 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3658
3659 if (charpos >= eob || (string->s && !STRINGP (object)))
3660 return eob;
3661
3662 /* It could happen that the display property or overlay was removed
3663 since we found it in compute_display_string_pos above. One way
3664 this can happen is if JIT font-lock was called (through
3665 handle_fontified_prop), and jit-lock-functions remove text
3666 properties or overlays from the portion of buffer that includes
3667 CHARPOS. Muse mode is known to do that, for example. In this
3668 case, we return -1 to the caller, to signal that no display
3669 string is actually present at CHARPOS. See bidi_fetch_char for
3670 how this is handled.
3671
3672 An alternative would be to never look for display properties past
3673 it->stop_charpos. But neither compute_display_string_pos nor
3674 bidi_fetch_char that calls it know or care where the next
3675 stop_charpos is. */
3676 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3677 return -1;
3678
3679 /* Look forward for the first character where the `display' property
3680 changes. */
3681 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3682
3683 return XFASTINT (pos);
3684 }
3685
3686
3687 \f
3688 /***********************************************************************
3689 Fontification
3690 ***********************************************************************/
3691
3692 /* Handle changes in the `fontified' property of the current buffer by
3693 calling hook functions from Qfontification_functions to fontify
3694 regions of text. */
3695
3696 static enum prop_handled
3697 handle_fontified_prop (struct it *it)
3698 {
3699 Lisp_Object prop, pos;
3700 enum prop_handled handled = HANDLED_NORMALLY;
3701
3702 if (!NILP (Vmemory_full))
3703 return handled;
3704
3705 /* Get the value of the `fontified' property at IT's current buffer
3706 position. (The `fontified' property doesn't have a special
3707 meaning in strings.) If the value is nil, call functions from
3708 Qfontification_functions. */
3709 if (!STRINGP (it->string)
3710 && it->s == NULL
3711 && !NILP (Vfontification_functions)
3712 && !NILP (Vrun_hooks)
3713 && (pos = make_number (IT_CHARPOS (*it)),
3714 prop = Fget_char_property (pos, Qfontified, Qnil),
3715 /* Ignore the special cased nil value always present at EOB since
3716 no amount of fontifying will be able to change it. */
3717 NILP (prop) && IT_CHARPOS (*it) < Z))
3718 {
3719 ptrdiff_t count = SPECPDL_INDEX ();
3720 Lisp_Object val;
3721 struct buffer *obuf = current_buffer;
3722 ptrdiff_t begv = BEGV, zv = ZV;
3723 bool old_clip_changed = current_buffer->clip_changed;
3724
3725 val = Vfontification_functions;
3726 specbind (Qfontification_functions, Qnil);
3727
3728 eassert (it->end_charpos == ZV);
3729
3730 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3731 safe_call1 (val, pos);
3732 else
3733 {
3734 Lisp_Object fns, fn;
3735 struct gcpro gcpro1, gcpro2;
3736
3737 fns = Qnil;
3738 GCPRO2 (val, fns);
3739
3740 for (; CONSP (val); val = XCDR (val))
3741 {
3742 fn = XCAR (val);
3743
3744 if (EQ (fn, Qt))
3745 {
3746 /* A value of t indicates this hook has a local
3747 binding; it means to run the global binding too.
3748 In a global value, t should not occur. If it
3749 does, we must ignore it to avoid an endless
3750 loop. */
3751 for (fns = Fdefault_value (Qfontification_functions);
3752 CONSP (fns);
3753 fns = XCDR (fns))
3754 {
3755 fn = XCAR (fns);
3756 if (!EQ (fn, Qt))
3757 safe_call1 (fn, pos);
3758 }
3759 }
3760 else
3761 safe_call1 (fn, pos);
3762 }
3763
3764 UNGCPRO;
3765 }
3766
3767 unbind_to (count, Qnil);
3768
3769 /* Fontification functions routinely call `save-restriction'.
3770 Normally, this tags clip_changed, which can confuse redisplay
3771 (see discussion in Bug#6671). Since we don't perform any
3772 special handling of fontification changes in the case where
3773 `save-restriction' isn't called, there's no point doing so in
3774 this case either. So, if the buffer's restrictions are
3775 actually left unchanged, reset clip_changed. */
3776 if (obuf == current_buffer)
3777 {
3778 if (begv == BEGV && zv == ZV)
3779 current_buffer->clip_changed = old_clip_changed;
3780 }
3781 /* There isn't much we can reasonably do to protect against
3782 misbehaving fontification, but here's a fig leaf. */
3783 else if (BUFFER_LIVE_P (obuf))
3784 set_buffer_internal_1 (obuf);
3785
3786 /* The fontification code may have added/removed text.
3787 It could do even a lot worse, but let's at least protect against
3788 the most obvious case where only the text past `pos' gets changed',
3789 as is/was done in grep.el where some escapes sequences are turned
3790 into face properties (bug#7876). */
3791 it->end_charpos = ZV;
3792
3793 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3794 something. This avoids an endless loop if they failed to
3795 fontify the text for which reason ever. */
3796 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3797 handled = HANDLED_RECOMPUTE_PROPS;
3798 }
3799
3800 return handled;
3801 }
3802
3803
3804 \f
3805 /***********************************************************************
3806 Faces
3807 ***********************************************************************/
3808
3809 /* Set up iterator IT from face properties at its current position.
3810 Called from handle_stop. */
3811
3812 static enum prop_handled
3813 handle_face_prop (struct it *it)
3814 {
3815 int new_face_id;
3816 ptrdiff_t next_stop;
3817
3818 if (!STRINGP (it->string))
3819 {
3820 new_face_id
3821 = face_at_buffer_position (it->w,
3822 IT_CHARPOS (*it),
3823 &next_stop,
3824 (IT_CHARPOS (*it)
3825 + TEXT_PROP_DISTANCE_LIMIT),
3826 0, it->base_face_id);
3827
3828 /* Is this a start of a run of characters with box face?
3829 Caveat: this can be called for a freshly initialized
3830 iterator; face_id is -1 in this case. We know that the new
3831 face will not change until limit, i.e. if the new face has a
3832 box, all characters up to limit will have one. But, as
3833 usual, we don't know whether limit is really the end. */
3834 if (new_face_id != it->face_id)
3835 {
3836 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3837 /* If it->face_id is -1, old_face below will be NULL, see
3838 the definition of FACE_FROM_ID. This will happen if this
3839 is the initial call that gets the face. */
3840 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3841
3842 /* If the value of face_id of the iterator is -1, we have to
3843 look in front of IT's position and see whether there is a
3844 face there that's different from new_face_id. */
3845 if (!old_face && IT_CHARPOS (*it) > BEG)
3846 {
3847 int prev_face_id = face_before_it_pos (it);
3848
3849 old_face = FACE_FROM_ID (it->f, prev_face_id);
3850 }
3851
3852 /* If the new face has a box, but the old face does not,
3853 this is the start of a run of characters with box face,
3854 i.e. this character has a shadow on the left side. */
3855 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3856 && (old_face == NULL || !old_face->box));
3857 it->face_box_p = new_face->box != FACE_NO_BOX;
3858 }
3859 }
3860 else
3861 {
3862 int base_face_id;
3863 ptrdiff_t bufpos;
3864 int i;
3865 Lisp_Object from_overlay
3866 = (it->current.overlay_string_index >= 0
3867 ? it->string_overlays[it->current.overlay_string_index
3868 % OVERLAY_STRING_CHUNK_SIZE]
3869 : Qnil);
3870
3871 /* See if we got to this string directly or indirectly from
3872 an overlay property. That includes the before-string or
3873 after-string of an overlay, strings in display properties
3874 provided by an overlay, their text properties, etc.
3875
3876 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3877 if (! NILP (from_overlay))
3878 for (i = it->sp - 1; i >= 0; i--)
3879 {
3880 if (it->stack[i].current.overlay_string_index >= 0)
3881 from_overlay
3882 = it->string_overlays[it->stack[i].current.overlay_string_index
3883 % OVERLAY_STRING_CHUNK_SIZE];
3884 else if (! NILP (it->stack[i].from_overlay))
3885 from_overlay = it->stack[i].from_overlay;
3886
3887 if (!NILP (from_overlay))
3888 break;
3889 }
3890
3891 if (! NILP (from_overlay))
3892 {
3893 bufpos = IT_CHARPOS (*it);
3894 /* For a string from an overlay, the base face depends
3895 only on text properties and ignores overlays. */
3896 base_face_id
3897 = face_for_overlay_string (it->w,
3898 IT_CHARPOS (*it),
3899 &next_stop,
3900 (IT_CHARPOS (*it)
3901 + TEXT_PROP_DISTANCE_LIMIT),
3902 0,
3903 from_overlay);
3904 }
3905 else
3906 {
3907 bufpos = 0;
3908
3909 /* For strings from a `display' property, use the face at
3910 IT's current buffer position as the base face to merge
3911 with, so that overlay strings appear in the same face as
3912 surrounding text, unless they specify their own faces.
3913 For strings from wrap-prefix and line-prefix properties,
3914 use the default face, possibly remapped via
3915 Vface_remapping_alist. */
3916 base_face_id = it->string_from_prefix_prop_p
3917 ? (!NILP (Vface_remapping_alist)
3918 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3919 : DEFAULT_FACE_ID)
3920 : underlying_face_id (it);
3921 }
3922
3923 new_face_id = face_at_string_position (it->w,
3924 it->string,
3925 IT_STRING_CHARPOS (*it),
3926 bufpos,
3927 &next_stop,
3928 base_face_id, 0);
3929
3930 /* Is this a start of a run of characters with box? Caveat:
3931 this can be called for a freshly allocated iterator; face_id
3932 is -1 is this case. We know that the new face will not
3933 change until the next check pos, i.e. if the new face has a
3934 box, all characters up to that position will have a
3935 box. But, as usual, we don't know whether that position
3936 is really the end. */
3937 if (new_face_id != it->face_id)
3938 {
3939 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3940 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3941
3942 /* If new face has a box but old face hasn't, this is the
3943 start of a run of characters with box, i.e. it has a
3944 shadow on the left side. */
3945 it->start_of_box_run_p
3946 = new_face->box && (old_face == NULL || !old_face->box);
3947 it->face_box_p = new_face->box != FACE_NO_BOX;
3948 }
3949 }
3950
3951 it->face_id = new_face_id;
3952 return HANDLED_NORMALLY;
3953 }
3954
3955
3956 /* Return the ID of the face ``underlying'' IT's current position,
3957 which is in a string. If the iterator is associated with a
3958 buffer, return the face at IT's current buffer position.
3959 Otherwise, use the iterator's base_face_id. */
3960
3961 static int
3962 underlying_face_id (struct it *it)
3963 {
3964 int face_id = it->base_face_id, i;
3965
3966 eassert (STRINGP (it->string));
3967
3968 for (i = it->sp - 1; i >= 0; --i)
3969 if (NILP (it->stack[i].string))
3970 face_id = it->stack[i].face_id;
3971
3972 return face_id;
3973 }
3974
3975
3976 /* Compute the face one character before or after the current position
3977 of IT, in the visual order. BEFORE_P non-zero means get the face
3978 in front (to the left in L2R paragraphs, to the right in R2L
3979 paragraphs) of IT's screen position. Value is the ID of the face. */
3980
3981 static int
3982 face_before_or_after_it_pos (struct it *it, int before_p)
3983 {
3984 int face_id, limit;
3985 ptrdiff_t next_check_charpos;
3986 struct it it_copy;
3987 void *it_copy_data = NULL;
3988
3989 eassert (it->s == NULL);
3990
3991 if (STRINGP (it->string))
3992 {
3993 ptrdiff_t bufpos, charpos;
3994 int base_face_id;
3995
3996 /* No face change past the end of the string (for the case
3997 we are padding with spaces). No face change before the
3998 string start. */
3999 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4000 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4001 return it->face_id;
4002
4003 if (!it->bidi_p)
4004 {
4005 /* Set charpos to the position before or after IT's current
4006 position, in the logical order, which in the non-bidi
4007 case is the same as the visual order. */
4008 if (before_p)
4009 charpos = IT_STRING_CHARPOS (*it) - 1;
4010 else if (it->what == IT_COMPOSITION)
4011 /* For composition, we must check the character after the
4012 composition. */
4013 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4014 else
4015 charpos = IT_STRING_CHARPOS (*it) + 1;
4016 }
4017 else
4018 {
4019 if (before_p)
4020 {
4021 /* With bidi iteration, the character before the current
4022 in the visual order cannot be found by simple
4023 iteration, because "reverse" reordering is not
4024 supported. Instead, we need to use the move_it_*
4025 family of functions. */
4026 /* Ignore face changes before the first visible
4027 character on this display line. */
4028 if (it->current_x <= it->first_visible_x)
4029 return it->face_id;
4030 SAVE_IT (it_copy, *it, it_copy_data);
4031 /* Implementation note: Since move_it_in_display_line
4032 works in the iterator geometry, and thinks the first
4033 character is always the leftmost, even in R2L lines,
4034 we don't need to distinguish between the R2L and L2R
4035 cases here. */
4036 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4037 it_copy.current_x - 1, MOVE_TO_X);
4038 charpos = IT_STRING_CHARPOS (it_copy);
4039 RESTORE_IT (it, it, it_copy_data);
4040 }
4041 else
4042 {
4043 /* Set charpos to the string position of the character
4044 that comes after IT's current position in the visual
4045 order. */
4046 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4047
4048 it_copy = *it;
4049 while (n--)
4050 bidi_move_to_visually_next (&it_copy.bidi_it);
4051
4052 charpos = it_copy.bidi_it.charpos;
4053 }
4054 }
4055 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4056
4057 if (it->current.overlay_string_index >= 0)
4058 bufpos = IT_CHARPOS (*it);
4059 else
4060 bufpos = 0;
4061
4062 base_face_id = underlying_face_id (it);
4063
4064 /* Get the face for ASCII, or unibyte. */
4065 face_id = face_at_string_position (it->w,
4066 it->string,
4067 charpos,
4068 bufpos,
4069 &next_check_charpos,
4070 base_face_id, 0);
4071
4072 /* Correct the face for charsets different from ASCII. Do it
4073 for the multibyte case only. The face returned above is
4074 suitable for unibyte text if IT->string is unibyte. */
4075 if (STRING_MULTIBYTE (it->string))
4076 {
4077 struct text_pos pos1 = string_pos (charpos, it->string);
4078 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4079 int c, len;
4080 struct face *face = FACE_FROM_ID (it->f, face_id);
4081
4082 c = string_char_and_length (p, &len);
4083 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4084 }
4085 }
4086 else
4087 {
4088 struct text_pos pos;
4089
4090 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4091 || (IT_CHARPOS (*it) <= BEGV && before_p))
4092 return it->face_id;
4093
4094 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4095 pos = it->current.pos;
4096
4097 if (!it->bidi_p)
4098 {
4099 if (before_p)
4100 DEC_TEXT_POS (pos, it->multibyte_p);
4101 else
4102 {
4103 if (it->what == IT_COMPOSITION)
4104 {
4105 /* For composition, we must check the position after
4106 the composition. */
4107 pos.charpos += it->cmp_it.nchars;
4108 pos.bytepos += it->len;
4109 }
4110 else
4111 INC_TEXT_POS (pos, it->multibyte_p);
4112 }
4113 }
4114 else
4115 {
4116 if (before_p)
4117 {
4118 /* With bidi iteration, the character before the current
4119 in the visual order cannot be found by simple
4120 iteration, because "reverse" reordering is not
4121 supported. Instead, we need to use the move_it_*
4122 family of functions. */
4123 /* Ignore face changes before the first visible
4124 character on this display line. */
4125 if (it->current_x <= it->first_visible_x)
4126 return it->face_id;
4127 SAVE_IT (it_copy, *it, it_copy_data);
4128 /* Implementation note: Since move_it_in_display_line
4129 works in the iterator geometry, and thinks the first
4130 character is always the leftmost, even in R2L lines,
4131 we don't need to distinguish between the R2L and L2R
4132 cases here. */
4133 move_it_in_display_line (&it_copy, ZV,
4134 it_copy.current_x - 1, MOVE_TO_X);
4135 pos = it_copy.current.pos;
4136 RESTORE_IT (it, it, it_copy_data);
4137 }
4138 else
4139 {
4140 /* Set charpos to the buffer position of the character
4141 that comes after IT's current position in the visual
4142 order. */
4143 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4144
4145 it_copy = *it;
4146 while (n--)
4147 bidi_move_to_visually_next (&it_copy.bidi_it);
4148
4149 SET_TEXT_POS (pos,
4150 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4151 }
4152 }
4153 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4154
4155 /* Determine face for CHARSET_ASCII, or unibyte. */
4156 face_id = face_at_buffer_position (it->w,
4157 CHARPOS (pos),
4158 &next_check_charpos,
4159 limit, 0, -1);
4160
4161 /* Correct the face for charsets different from ASCII. Do it
4162 for the multibyte case only. The face returned above is
4163 suitable for unibyte text if current_buffer is unibyte. */
4164 if (it->multibyte_p)
4165 {
4166 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4167 struct face *face = FACE_FROM_ID (it->f, face_id);
4168 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4169 }
4170 }
4171
4172 return face_id;
4173 }
4174
4175
4176 \f
4177 /***********************************************************************
4178 Invisible text
4179 ***********************************************************************/
4180
4181 /* Set up iterator IT from invisible properties at its current
4182 position. Called from handle_stop. */
4183
4184 static enum prop_handled
4185 handle_invisible_prop (struct it *it)
4186 {
4187 enum prop_handled handled = HANDLED_NORMALLY;
4188 int invis_p;
4189 Lisp_Object prop;
4190
4191 if (STRINGP (it->string))
4192 {
4193 Lisp_Object end_charpos, limit, charpos;
4194
4195 /* Get the value of the invisible text property at the
4196 current position. Value will be nil if there is no such
4197 property. */
4198 charpos = make_number (IT_STRING_CHARPOS (*it));
4199 prop = Fget_text_property (charpos, Qinvisible, it->string);
4200 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4201
4202 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4203 {
4204 /* Record whether we have to display an ellipsis for the
4205 invisible text. */
4206 int display_ellipsis_p = (invis_p == 2);
4207 ptrdiff_t len, endpos;
4208
4209 handled = HANDLED_RECOMPUTE_PROPS;
4210
4211 /* Get the position at which the next visible text can be
4212 found in IT->string, if any. */
4213 endpos = len = SCHARS (it->string);
4214 XSETINT (limit, len);
4215 do
4216 {
4217 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4218 it->string, limit);
4219 if (INTEGERP (end_charpos))
4220 {
4221 endpos = XFASTINT (end_charpos);
4222 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4223 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4224 if (invis_p == 2)
4225 display_ellipsis_p = true;
4226 }
4227 }
4228 while (invis_p && endpos < len);
4229
4230 if (display_ellipsis_p)
4231 it->ellipsis_p = true;
4232
4233 if (endpos < len)
4234 {
4235 /* Text at END_CHARPOS is visible. Move IT there. */
4236 struct text_pos old;
4237 ptrdiff_t oldpos;
4238
4239 old = it->current.string_pos;
4240 oldpos = CHARPOS (old);
4241 if (it->bidi_p)
4242 {
4243 if (it->bidi_it.first_elt
4244 && it->bidi_it.charpos < SCHARS (it->string))
4245 bidi_paragraph_init (it->paragraph_embedding,
4246 &it->bidi_it, 1);
4247 /* Bidi-iterate out of the invisible text. */
4248 do
4249 {
4250 bidi_move_to_visually_next (&it->bidi_it);
4251 }
4252 while (oldpos <= it->bidi_it.charpos
4253 && it->bidi_it.charpos < endpos);
4254
4255 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4256 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4257 if (IT_CHARPOS (*it) >= endpos)
4258 it->prev_stop = endpos;
4259 }
4260 else
4261 {
4262 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4263 compute_string_pos (&it->current.string_pos, old, it->string);
4264 }
4265 }
4266 else
4267 {
4268 /* The rest of the string is invisible. If this is an
4269 overlay string, proceed with the next overlay string
4270 or whatever comes and return a character from there. */
4271 if (it->current.overlay_string_index >= 0
4272 && !display_ellipsis_p)
4273 {
4274 next_overlay_string (it);
4275 /* Don't check for overlay strings when we just
4276 finished processing them. */
4277 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4278 }
4279 else
4280 {
4281 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4282 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4283 }
4284 }
4285 }
4286 }
4287 else
4288 {
4289 ptrdiff_t newpos, next_stop, start_charpos, tem;
4290 Lisp_Object pos, overlay;
4291
4292 /* First of all, is there invisible text at this position? */
4293 tem = start_charpos = IT_CHARPOS (*it);
4294 pos = make_number (tem);
4295 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4296 &overlay);
4297 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4298
4299 /* If we are on invisible text, skip over it. */
4300 if (invis_p && start_charpos < it->end_charpos)
4301 {
4302 /* Record whether we have to display an ellipsis for the
4303 invisible text. */
4304 int display_ellipsis_p = invis_p == 2;
4305
4306 handled = HANDLED_RECOMPUTE_PROPS;
4307
4308 /* Loop skipping over invisible text. The loop is left at
4309 ZV or with IT on the first char being visible again. */
4310 do
4311 {
4312 /* Try to skip some invisible text. Return value is the
4313 position reached which can be equal to where we start
4314 if there is nothing invisible there. This skips both
4315 over invisible text properties and overlays with
4316 invisible property. */
4317 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4318
4319 /* If we skipped nothing at all we weren't at invisible
4320 text in the first place. If everything to the end of
4321 the buffer was skipped, end the loop. */
4322 if (newpos == tem || newpos >= ZV)
4323 invis_p = 0;
4324 else
4325 {
4326 /* We skipped some characters but not necessarily
4327 all there are. Check if we ended up on visible
4328 text. Fget_char_property returns the property of
4329 the char before the given position, i.e. if we
4330 get invis_p = 0, this means that the char at
4331 newpos is visible. */
4332 pos = make_number (newpos);
4333 prop = Fget_char_property (pos, Qinvisible, it->window);
4334 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4335 }
4336
4337 /* If we ended up on invisible text, proceed to
4338 skip starting with next_stop. */
4339 if (invis_p)
4340 tem = next_stop;
4341
4342 /* If there are adjacent invisible texts, don't lose the
4343 second one's ellipsis. */
4344 if (invis_p == 2)
4345 display_ellipsis_p = true;
4346 }
4347 while (invis_p);
4348
4349 /* The position newpos is now either ZV or on visible text. */
4350 if (it->bidi_p)
4351 {
4352 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4353 int on_newline
4354 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4355 int after_newline
4356 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4357
4358 /* If the invisible text ends on a newline or on a
4359 character after a newline, we can avoid the costly,
4360 character by character, bidi iteration to NEWPOS, and
4361 instead simply reseat the iterator there. That's
4362 because all bidi reordering information is tossed at
4363 the newline. This is a big win for modes that hide
4364 complete lines, like Outline, Org, etc. */
4365 if (on_newline || after_newline)
4366 {
4367 struct text_pos tpos;
4368 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4369
4370 SET_TEXT_POS (tpos, newpos, bpos);
4371 reseat_1 (it, tpos, 0);
4372 /* If we reseat on a newline/ZV, we need to prep the
4373 bidi iterator for advancing to the next character
4374 after the newline/EOB, keeping the current paragraph
4375 direction (so that PRODUCE_GLYPHS does TRT wrt
4376 prepending/appending glyphs to a glyph row). */
4377 if (on_newline)
4378 {
4379 it->bidi_it.first_elt = 0;
4380 it->bidi_it.paragraph_dir = pdir;
4381 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4382 it->bidi_it.nchars = 1;
4383 it->bidi_it.ch_len = 1;
4384 }
4385 }
4386 else /* Must use the slow method. */
4387 {
4388 /* With bidi iteration, the region of invisible text
4389 could start and/or end in the middle of a
4390 non-base embedding level. Therefore, we need to
4391 skip invisible text using the bidi iterator,
4392 starting at IT's current position, until we find
4393 ourselves outside of the invisible text.
4394 Skipping invisible text _after_ bidi iteration
4395 avoids affecting the visual order of the
4396 displayed text when invisible properties are
4397 added or removed. */
4398 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4399 {
4400 /* If we were `reseat'ed to a new paragraph,
4401 determine the paragraph base direction. We
4402 need to do it now because
4403 next_element_from_buffer may not have a
4404 chance to do it, if we are going to skip any
4405 text at the beginning, which resets the
4406 FIRST_ELT flag. */
4407 bidi_paragraph_init (it->paragraph_embedding,
4408 &it->bidi_it, 1);
4409 }
4410 do
4411 {
4412 bidi_move_to_visually_next (&it->bidi_it);
4413 }
4414 while (it->stop_charpos <= it->bidi_it.charpos
4415 && it->bidi_it.charpos < newpos);
4416 IT_CHARPOS (*it) = it->bidi_it.charpos;
4417 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4418 /* If we overstepped NEWPOS, record its position in
4419 the iterator, so that we skip invisible text if
4420 later the bidi iteration lands us in the
4421 invisible region again. */
4422 if (IT_CHARPOS (*it) >= newpos)
4423 it->prev_stop = newpos;
4424 }
4425 }
4426 else
4427 {
4428 IT_CHARPOS (*it) = newpos;
4429 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4430 }
4431
4432 /* If there are before-strings at the start of invisible
4433 text, and the text is invisible because of a text
4434 property, arrange to show before-strings because 20.x did
4435 it that way. (If the text is invisible because of an
4436 overlay property instead of a text property, this is
4437 already handled in the overlay code.) */
4438 if (NILP (overlay)
4439 && get_overlay_strings (it, it->stop_charpos))
4440 {
4441 handled = HANDLED_RECOMPUTE_PROPS;
4442 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4443 }
4444 else if (display_ellipsis_p)
4445 {
4446 /* Make sure that the glyphs of the ellipsis will get
4447 correct `charpos' values. If we would not update
4448 it->position here, the glyphs would belong to the
4449 last visible character _before_ the invisible
4450 text, which confuses `set_cursor_from_row'.
4451
4452 We use the last invisible position instead of the
4453 first because this way the cursor is always drawn on
4454 the first "." of the ellipsis, whenever PT is inside
4455 the invisible text. Otherwise the cursor would be
4456 placed _after_ the ellipsis when the point is after the
4457 first invisible character. */
4458 if (!STRINGP (it->object))
4459 {
4460 it->position.charpos = newpos - 1;
4461 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4462 }
4463 it->ellipsis_p = true;
4464 /* Let the ellipsis display before
4465 considering any properties of the following char.
4466 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4467 handled = HANDLED_RETURN;
4468 }
4469 }
4470 }
4471
4472 return handled;
4473 }
4474
4475
4476 /* Make iterator IT return `...' next.
4477 Replaces LEN characters from buffer. */
4478
4479 static void
4480 setup_for_ellipsis (struct it *it, int len)
4481 {
4482 /* Use the display table definition for `...'. Invalid glyphs
4483 will be handled by the method returning elements from dpvec. */
4484 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4485 {
4486 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4487 it->dpvec = v->contents;
4488 it->dpend = v->contents + v->header.size;
4489 }
4490 else
4491 {
4492 /* Default `...'. */
4493 it->dpvec = default_invis_vector;
4494 it->dpend = default_invis_vector + 3;
4495 }
4496
4497 it->dpvec_char_len = len;
4498 it->current.dpvec_index = 0;
4499 it->dpvec_face_id = -1;
4500
4501 /* Remember the current face id in case glyphs specify faces.
4502 IT's face is restored in set_iterator_to_next.
4503 saved_face_id was set to preceding char's face in handle_stop. */
4504 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4505 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4506
4507 it->method = GET_FROM_DISPLAY_VECTOR;
4508 it->ellipsis_p = true;
4509 }
4510
4511
4512 \f
4513 /***********************************************************************
4514 'display' property
4515 ***********************************************************************/
4516
4517 /* Set up iterator IT from `display' property at its current position.
4518 Called from handle_stop.
4519 We return HANDLED_RETURN if some part of the display property
4520 overrides the display of the buffer text itself.
4521 Otherwise we return HANDLED_NORMALLY. */
4522
4523 static enum prop_handled
4524 handle_display_prop (struct it *it)
4525 {
4526 Lisp_Object propval, object, overlay;
4527 struct text_pos *position;
4528 ptrdiff_t bufpos;
4529 /* Nonzero if some property replaces the display of the text itself. */
4530 int display_replaced_p = 0;
4531
4532 if (STRINGP (it->string))
4533 {
4534 object = it->string;
4535 position = &it->current.string_pos;
4536 bufpos = CHARPOS (it->current.pos);
4537 }
4538 else
4539 {
4540 XSETWINDOW (object, it->w);
4541 position = &it->current.pos;
4542 bufpos = CHARPOS (*position);
4543 }
4544
4545 /* Reset those iterator values set from display property values. */
4546 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4547 it->space_width = Qnil;
4548 it->font_height = Qnil;
4549 it->voffset = 0;
4550
4551 /* We don't support recursive `display' properties, i.e. string
4552 values that have a string `display' property, that have a string
4553 `display' property etc. */
4554 if (!it->string_from_display_prop_p)
4555 it->area = TEXT_AREA;
4556
4557 propval = get_char_property_and_overlay (make_number (position->charpos),
4558 Qdisplay, object, &overlay);
4559 if (NILP (propval))
4560 return HANDLED_NORMALLY;
4561 /* Now OVERLAY is the overlay that gave us this property, or nil
4562 if it was a text property. */
4563
4564 if (!STRINGP (it->string))
4565 object = it->w->contents;
4566
4567 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4568 position, bufpos,
4569 FRAME_WINDOW_P (it->f));
4570
4571 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4572 }
4573
4574 /* Subroutine of handle_display_prop. Returns non-zero if the display
4575 specification in SPEC is a replacing specification, i.e. it would
4576 replace the text covered by `display' property with something else,
4577 such as an image or a display string. If SPEC includes any kind or
4578 `(space ...) specification, the value is 2; this is used by
4579 compute_display_string_pos, which see.
4580
4581 See handle_single_display_spec for documentation of arguments.
4582 frame_window_p is non-zero if the window being redisplayed is on a
4583 GUI frame; this argument is used only if IT is NULL, see below.
4584
4585 IT can be NULL, if this is called by the bidi reordering code
4586 through compute_display_string_pos, which see. In that case, this
4587 function only examines SPEC, but does not otherwise "handle" it, in
4588 the sense that it doesn't set up members of IT from the display
4589 spec. */
4590 static int
4591 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4592 Lisp_Object overlay, struct text_pos *position,
4593 ptrdiff_t bufpos, int frame_window_p)
4594 {
4595 int replacing_p = 0;
4596 int rv;
4597
4598 if (CONSP (spec)
4599 /* Simple specifications. */
4600 && !EQ (XCAR (spec), Qimage)
4601 && !EQ (XCAR (spec), Qspace)
4602 && !EQ (XCAR (spec), Qwhen)
4603 && !EQ (XCAR (spec), Qslice)
4604 && !EQ (XCAR (spec), Qspace_width)
4605 && !EQ (XCAR (spec), Qheight)
4606 && !EQ (XCAR (spec), Qraise)
4607 /* Marginal area specifications. */
4608 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4609 && !EQ (XCAR (spec), Qleft_fringe)
4610 && !EQ (XCAR (spec), Qright_fringe)
4611 && !NILP (XCAR (spec)))
4612 {
4613 for (; CONSP (spec); spec = XCDR (spec))
4614 {
4615 if ((rv = handle_single_display_spec (it, XCAR (spec), 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 }
4627 else if (VECTORP (spec))
4628 {
4629 ptrdiff_t i;
4630 for (i = 0; i < ASIZE (spec); ++i)
4631 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4632 overlay, position, bufpos,
4633 replacing_p, frame_window_p)))
4634 {
4635 replacing_p = rv;
4636 /* If some text in a string is replaced, `position' no
4637 longer points to the position of `object'. */
4638 if (!it || STRINGP (object))
4639 break;
4640 }
4641 }
4642 else
4643 {
4644 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4645 position, bufpos, 0,
4646 frame_window_p)))
4647 replacing_p = rv;
4648 }
4649
4650 return replacing_p;
4651 }
4652
4653 /* Value is the position of the end of the `display' property starting
4654 at START_POS in OBJECT. */
4655
4656 static struct text_pos
4657 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4658 {
4659 Lisp_Object end;
4660 struct text_pos end_pos;
4661
4662 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4663 Qdisplay, object, Qnil);
4664 CHARPOS (end_pos) = XFASTINT (end);
4665 if (STRINGP (object))
4666 compute_string_pos (&end_pos, start_pos, it->string);
4667 else
4668 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4669
4670 return end_pos;
4671 }
4672
4673
4674 /* Set up IT from a single `display' property specification SPEC. OBJECT
4675 is the object in which the `display' property was found. *POSITION
4676 is the position in OBJECT at which the `display' property was found.
4677 BUFPOS is the buffer position of OBJECT (different from POSITION if
4678 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4679 previously saw a display specification which already replaced text
4680 display with something else, for example an image; we ignore such
4681 properties after the first one has been processed.
4682
4683 OVERLAY is the overlay this `display' property came from,
4684 or nil if it was a text property.
4685
4686 If SPEC is a `space' or `image' specification, and in some other
4687 cases too, set *POSITION to the position where the `display'
4688 property ends.
4689
4690 If IT is NULL, only examine the property specification in SPEC, but
4691 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4692 is intended to be displayed in a window on a GUI frame.
4693
4694 Value is non-zero if something was found which replaces the display
4695 of buffer or string text. */
4696
4697 static int
4698 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4699 Lisp_Object overlay, struct text_pos *position,
4700 ptrdiff_t bufpos, int display_replaced_p,
4701 int frame_window_p)
4702 {
4703 Lisp_Object form;
4704 Lisp_Object location, value;
4705 struct text_pos start_pos = *position;
4706 int valid_p;
4707
4708 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4709 If the result is non-nil, use VALUE instead of SPEC. */
4710 form = Qt;
4711 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4712 {
4713 spec = XCDR (spec);
4714 if (!CONSP (spec))
4715 return 0;
4716 form = XCAR (spec);
4717 spec = XCDR (spec);
4718 }
4719
4720 if (!NILP (form) && !EQ (form, Qt))
4721 {
4722 ptrdiff_t count = SPECPDL_INDEX ();
4723 struct gcpro gcpro1;
4724
4725 /* Bind `object' to the object having the `display' property, a
4726 buffer or string. Bind `position' to the position in the
4727 object where the property was found, and `buffer-position'
4728 to the current position in the buffer. */
4729
4730 if (NILP (object))
4731 XSETBUFFER (object, current_buffer);
4732 specbind (Qobject, object);
4733 specbind (Qposition, make_number (CHARPOS (*position)));
4734 specbind (Qbuffer_position, make_number (bufpos));
4735 GCPRO1 (form);
4736 form = safe_eval (form);
4737 UNGCPRO;
4738 unbind_to (count, Qnil);
4739 }
4740
4741 if (NILP (form))
4742 return 0;
4743
4744 /* Handle `(height HEIGHT)' specifications. */
4745 if (CONSP (spec)
4746 && EQ (XCAR (spec), Qheight)
4747 && CONSP (XCDR (spec)))
4748 {
4749 if (it)
4750 {
4751 if (!FRAME_WINDOW_P (it->f))
4752 return 0;
4753
4754 it->font_height = XCAR (XCDR (spec));
4755 if (!NILP (it->font_height))
4756 {
4757 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4758 int new_height = -1;
4759
4760 if (CONSP (it->font_height)
4761 && (EQ (XCAR (it->font_height), Qplus)
4762 || EQ (XCAR (it->font_height), Qminus))
4763 && CONSP (XCDR (it->font_height))
4764 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4765 {
4766 /* `(+ N)' or `(- N)' where N is an integer. */
4767 int steps = XINT (XCAR (XCDR (it->font_height)));
4768 if (EQ (XCAR (it->font_height), Qplus))
4769 steps = - steps;
4770 it->face_id = smaller_face (it->f, it->face_id, steps);
4771 }
4772 else if (FUNCTIONP (it->font_height))
4773 {
4774 /* Call function with current height as argument.
4775 Value is the new height. */
4776 Lisp_Object height;
4777 height = safe_call1 (it->font_height,
4778 face->lface[LFACE_HEIGHT_INDEX]);
4779 if (NUMBERP (height))
4780 new_height = XFLOATINT (height);
4781 }
4782 else if (NUMBERP (it->font_height))
4783 {
4784 /* Value is a multiple of the canonical char height. */
4785 struct face *f;
4786
4787 f = FACE_FROM_ID (it->f,
4788 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4789 new_height = (XFLOATINT (it->font_height)
4790 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4791 }
4792 else
4793 {
4794 /* Evaluate IT->font_height with `height' bound to the
4795 current specified height to get the new height. */
4796 ptrdiff_t count = SPECPDL_INDEX ();
4797
4798 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4799 value = safe_eval (it->font_height);
4800 unbind_to (count, Qnil);
4801
4802 if (NUMBERP (value))
4803 new_height = XFLOATINT (value);
4804 }
4805
4806 if (new_height > 0)
4807 it->face_id = face_with_height (it->f, it->face_id, new_height);
4808 }
4809 }
4810
4811 return 0;
4812 }
4813
4814 /* Handle `(space-width WIDTH)'. */
4815 if (CONSP (spec)
4816 && EQ (XCAR (spec), Qspace_width)
4817 && CONSP (XCDR (spec)))
4818 {
4819 if (it)
4820 {
4821 if (!FRAME_WINDOW_P (it->f))
4822 return 0;
4823
4824 value = XCAR (XCDR (spec));
4825 if (NUMBERP (value) && XFLOATINT (value) > 0)
4826 it->space_width = value;
4827 }
4828
4829 return 0;
4830 }
4831
4832 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4833 if (CONSP (spec)
4834 && EQ (XCAR (spec), Qslice))
4835 {
4836 Lisp_Object tem;
4837
4838 if (it)
4839 {
4840 if (!FRAME_WINDOW_P (it->f))
4841 return 0;
4842
4843 if (tem = XCDR (spec), CONSP (tem))
4844 {
4845 it->slice.x = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4847 {
4848 it->slice.y = XCAR (tem);
4849 if (tem = XCDR (tem), CONSP (tem))
4850 {
4851 it->slice.width = XCAR (tem);
4852 if (tem = XCDR (tem), CONSP (tem))
4853 it->slice.height = XCAR (tem);
4854 }
4855 }
4856 }
4857 }
4858
4859 return 0;
4860 }
4861
4862 /* Handle `(raise FACTOR)'. */
4863 if (CONSP (spec)
4864 && EQ (XCAR (spec), Qraise)
4865 && CONSP (XCDR (spec)))
4866 {
4867 if (it)
4868 {
4869 if (!FRAME_WINDOW_P (it->f))
4870 return 0;
4871
4872 #ifdef HAVE_WINDOW_SYSTEM
4873 value = XCAR (XCDR (spec));
4874 if (NUMBERP (value))
4875 {
4876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4877 it->voffset = - (XFLOATINT (value)
4878 * (FONT_HEIGHT (face->font)));
4879 }
4880 #endif /* HAVE_WINDOW_SYSTEM */
4881 }
4882
4883 return 0;
4884 }
4885
4886 /* Don't handle the other kinds of display specifications
4887 inside a string that we got from a `display' property. */
4888 if (it && it->string_from_display_prop_p)
4889 return 0;
4890
4891 /* Characters having this form of property are not displayed, so
4892 we have to find the end of the property. */
4893 if (it)
4894 {
4895 start_pos = *position;
4896 *position = display_prop_end (it, object, start_pos);
4897 }
4898 value = Qnil;
4899
4900 /* Stop the scan at that end position--we assume that all
4901 text properties change there. */
4902 if (it)
4903 it->stop_charpos = position->charpos;
4904
4905 /* Handle `(left-fringe BITMAP [FACE])'
4906 and `(right-fringe BITMAP [FACE])'. */
4907 if (CONSP (spec)
4908 && (EQ (XCAR (spec), Qleft_fringe)
4909 || EQ (XCAR (spec), Qright_fringe))
4910 && CONSP (XCDR (spec)))
4911 {
4912 int fringe_bitmap;
4913
4914 if (it)
4915 {
4916 if (!FRAME_WINDOW_P (it->f))
4917 /* If we return here, POSITION has been advanced
4918 across the text with this property. */
4919 {
4920 /* Synchronize the bidi iterator with POSITION. This is
4921 needed because we are not going to push the iterator
4922 on behalf of this display property, so there will be
4923 no pop_it call to do this synchronization for us. */
4924 if (it->bidi_p)
4925 {
4926 it->position = *position;
4927 iterate_out_of_display_property (it);
4928 *position = it->position;
4929 }
4930 return 1;
4931 }
4932 }
4933 else if (!frame_window_p)
4934 return 1;
4935
4936 #ifdef HAVE_WINDOW_SYSTEM
4937 value = XCAR (XCDR (spec));
4938 if (!SYMBOLP (value)
4939 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4940 /* If we return here, POSITION has been advanced
4941 across the text with this property. */
4942 {
4943 if (it && it->bidi_p)
4944 {
4945 it->position = *position;
4946 iterate_out_of_display_property (it);
4947 *position = it->position;
4948 }
4949 return 1;
4950 }
4951
4952 if (it)
4953 {
4954 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4955
4956 if (CONSP (XCDR (XCDR (spec))))
4957 {
4958 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4959 int face_id2 = lookup_derived_face (it->f, face_name,
4960 FRINGE_FACE_ID, 0);
4961 if (face_id2 >= 0)
4962 face_id = face_id2;
4963 }
4964
4965 /* Save current settings of IT so that we can restore them
4966 when we are finished with the glyph property value. */
4967 push_it (it, position);
4968
4969 it->area = TEXT_AREA;
4970 it->what = IT_IMAGE;
4971 it->image_id = -1; /* no image */
4972 it->position = start_pos;
4973 it->object = NILP (object) ? it->w->contents : object;
4974 it->method = GET_FROM_IMAGE;
4975 it->from_overlay = Qnil;
4976 it->face_id = face_id;
4977 it->from_disp_prop_p = true;
4978
4979 /* Say that we haven't consumed the characters with
4980 `display' property yet. The call to pop_it in
4981 set_iterator_to_next will clean this up. */
4982 *position = start_pos;
4983
4984 if (EQ (XCAR (spec), Qleft_fringe))
4985 {
4986 it->left_user_fringe_bitmap = fringe_bitmap;
4987 it->left_user_fringe_face_id = face_id;
4988 }
4989 else
4990 {
4991 it->right_user_fringe_bitmap = fringe_bitmap;
4992 it->right_user_fringe_face_id = face_id;
4993 }
4994 }
4995 #endif /* HAVE_WINDOW_SYSTEM */
4996 return 1;
4997 }
4998
4999 /* Prepare to handle `((margin left-margin) ...)',
5000 `((margin right-margin) ...)' and `((margin nil) ...)'
5001 prefixes for display specifications. */
5002 location = Qunbound;
5003 if (CONSP (spec) && CONSP (XCAR (spec)))
5004 {
5005 Lisp_Object tem;
5006
5007 value = XCDR (spec);
5008 if (CONSP (value))
5009 value = XCAR (value);
5010
5011 tem = XCAR (spec);
5012 if (EQ (XCAR (tem), Qmargin)
5013 && (tem = XCDR (tem),
5014 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5015 (NILP (tem)
5016 || EQ (tem, Qleft_margin)
5017 || EQ (tem, Qright_margin))))
5018 location = tem;
5019 }
5020
5021 if (EQ (location, Qunbound))
5022 {
5023 location = Qnil;
5024 value = spec;
5025 }
5026
5027 /* After this point, VALUE is the property after any
5028 margin prefix has been stripped. It must be a string,
5029 an image specification, or `(space ...)'.
5030
5031 LOCATION specifies where to display: `left-margin',
5032 `right-margin' or nil. */
5033
5034 valid_p = (STRINGP (value)
5035 #ifdef HAVE_WINDOW_SYSTEM
5036 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5037 && valid_image_p (value))
5038 #endif /* not HAVE_WINDOW_SYSTEM */
5039 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5040
5041 if (valid_p && !display_replaced_p)
5042 {
5043 int retval = 1;
5044
5045 if (!it)
5046 {
5047 /* Callers need to know whether the display spec is any kind
5048 of `(space ...)' spec that is about to affect text-area
5049 display. */
5050 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5051 retval = 2;
5052 return retval;
5053 }
5054
5055 /* Save current settings of IT so that we can restore them
5056 when we are finished with the glyph property value. */
5057 push_it (it, position);
5058 it->from_overlay = overlay;
5059 it->from_disp_prop_p = true;
5060
5061 if (NILP (location))
5062 it->area = TEXT_AREA;
5063 else if (EQ (location, Qleft_margin))
5064 it->area = LEFT_MARGIN_AREA;
5065 else
5066 it->area = RIGHT_MARGIN_AREA;
5067
5068 if (STRINGP (value))
5069 {
5070 it->string = value;
5071 it->multibyte_p = STRING_MULTIBYTE (it->string);
5072 it->current.overlay_string_index = -1;
5073 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5074 it->end_charpos = it->string_nchars = SCHARS (it->string);
5075 it->method = GET_FROM_STRING;
5076 it->stop_charpos = 0;
5077 it->prev_stop = 0;
5078 it->base_level_stop = 0;
5079 it->string_from_display_prop_p = true;
5080 /* Say that we haven't consumed the characters with
5081 `display' property yet. The call to pop_it in
5082 set_iterator_to_next will clean this up. */
5083 if (BUFFERP (object))
5084 *position = start_pos;
5085
5086 /* Force paragraph direction to be that of the parent
5087 object. If the parent object's paragraph direction is
5088 not yet determined, default to L2R. */
5089 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5090 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5091 else
5092 it->paragraph_embedding = L2R;
5093
5094 /* Set up the bidi iterator for this display string. */
5095 if (it->bidi_p)
5096 {
5097 it->bidi_it.string.lstring = it->string;
5098 it->bidi_it.string.s = NULL;
5099 it->bidi_it.string.schars = it->end_charpos;
5100 it->bidi_it.string.bufpos = bufpos;
5101 it->bidi_it.string.from_disp_str = 1;
5102 it->bidi_it.string.unibyte = !it->multibyte_p;
5103 it->bidi_it.w = it->w;
5104 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5105 }
5106 }
5107 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5108 {
5109 it->method = GET_FROM_STRETCH;
5110 it->object = value;
5111 *position = it->position = start_pos;
5112 retval = 1 + (it->area == TEXT_AREA);
5113 }
5114 #ifdef HAVE_WINDOW_SYSTEM
5115 else
5116 {
5117 it->what = IT_IMAGE;
5118 it->image_id = lookup_image (it->f, value);
5119 it->position = start_pos;
5120 it->object = NILP (object) ? it->w->contents : object;
5121 it->method = GET_FROM_IMAGE;
5122
5123 /* Say that we haven't consumed the characters with
5124 `display' property yet. The call to pop_it in
5125 set_iterator_to_next will clean this up. */
5126 *position = start_pos;
5127 }
5128 #endif /* HAVE_WINDOW_SYSTEM */
5129
5130 return retval;
5131 }
5132
5133 /* Invalid property or property not supported. Restore
5134 POSITION to what it was before. */
5135 *position = start_pos;
5136 return 0;
5137 }
5138
5139 /* Check if PROP is a display property value whose text should be
5140 treated as intangible. OVERLAY is the overlay from which PROP
5141 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5142 specify the buffer position covered by PROP. */
5143
5144 int
5145 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5146 ptrdiff_t charpos, ptrdiff_t bytepos)
5147 {
5148 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5149 struct text_pos position;
5150
5151 SET_TEXT_POS (position, charpos, bytepos);
5152 return handle_display_spec (NULL, prop, Qnil, overlay,
5153 &position, charpos, frame_window_p);
5154 }
5155
5156
5157 /* Return 1 if PROP is a display sub-property value containing STRING.
5158
5159 Implementation note: this and the following function are really
5160 special cases of handle_display_spec and
5161 handle_single_display_spec, and should ideally use the same code.
5162 Until they do, these two pairs must be consistent and must be
5163 modified in sync. */
5164
5165 static int
5166 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5167 {
5168 if (EQ (string, prop))
5169 return 1;
5170
5171 /* Skip over `when FORM'. */
5172 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5173 {
5174 prop = XCDR (prop);
5175 if (!CONSP (prop))
5176 return 0;
5177 /* Actually, the condition following `when' should be eval'ed,
5178 like handle_single_display_spec does, and we should return
5179 zero if it evaluates to nil. However, this function is
5180 called only when the buffer was already displayed and some
5181 glyph in the glyph matrix was found to come from a display
5182 string. Therefore, the condition was already evaluated, and
5183 the result was non-nil, otherwise the display string wouldn't
5184 have been displayed and we would have never been called for
5185 this property. Thus, we can skip the evaluation and assume
5186 its result is non-nil. */
5187 prop = XCDR (prop);
5188 }
5189
5190 if (CONSP (prop))
5191 /* Skip over `margin LOCATION'. */
5192 if (EQ (XCAR (prop), Qmargin))
5193 {
5194 prop = XCDR (prop);
5195 if (!CONSP (prop))
5196 return 0;
5197
5198 prop = XCDR (prop);
5199 if (!CONSP (prop))
5200 return 0;
5201 }
5202
5203 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5204 }
5205
5206
5207 /* Return 1 if STRING appears in the `display' property PROP. */
5208
5209 static int
5210 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5211 {
5212 if (CONSP (prop)
5213 && !EQ (XCAR (prop), Qwhen)
5214 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5215 {
5216 /* A list of sub-properties. */
5217 while (CONSP (prop))
5218 {
5219 if (single_display_spec_string_p (XCAR (prop), string))
5220 return 1;
5221 prop = XCDR (prop);
5222 }
5223 }
5224 else if (VECTORP (prop))
5225 {
5226 /* A vector of sub-properties. */
5227 ptrdiff_t i;
5228 for (i = 0; i < ASIZE (prop); ++i)
5229 if (single_display_spec_string_p (AREF (prop, i), string))
5230 return 1;
5231 }
5232 else
5233 return single_display_spec_string_p (prop, string);
5234
5235 return 0;
5236 }
5237
5238 /* Look for STRING in overlays and text properties in the current
5239 buffer, between character positions FROM and TO (excluding TO).
5240 BACK_P non-zero means look back (in this case, TO is supposed to be
5241 less than FROM).
5242 Value is the first character position where STRING was found, or
5243 zero if it wasn't found before hitting TO.
5244
5245 This function may only use code that doesn't eval because it is
5246 called asynchronously from note_mouse_highlight. */
5247
5248 static ptrdiff_t
5249 string_buffer_position_lim (Lisp_Object string,
5250 ptrdiff_t from, ptrdiff_t to, int back_p)
5251 {
5252 Lisp_Object limit, prop, pos;
5253 int found = 0;
5254
5255 pos = make_number (max (from, BEGV));
5256
5257 if (!back_p) /* looking forward */
5258 {
5259 limit = make_number (min (to, ZV));
5260 while (!found && !EQ (pos, limit))
5261 {
5262 prop = Fget_char_property (pos, Qdisplay, Qnil);
5263 if (!NILP (prop) && display_prop_string_p (prop, string))
5264 found = 1;
5265 else
5266 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5267 limit);
5268 }
5269 }
5270 else /* looking back */
5271 {
5272 limit = make_number (max (to, BEGV));
5273 while (!found && !EQ (pos, limit))
5274 {
5275 prop = Fget_char_property (pos, Qdisplay, Qnil);
5276 if (!NILP (prop) && display_prop_string_p (prop, string))
5277 found = 1;
5278 else
5279 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5280 limit);
5281 }
5282 }
5283
5284 return found ? XINT (pos) : 0;
5285 }
5286
5287 /* Determine which buffer position in current buffer STRING comes from.
5288 AROUND_CHARPOS is an approximate position where it could come from.
5289 Value is the buffer position or 0 if it couldn't be determined.
5290
5291 This function is necessary because we don't record buffer positions
5292 in glyphs generated from strings (to keep struct glyph small).
5293 This function may only use code that doesn't eval because it is
5294 called asynchronously from note_mouse_highlight. */
5295
5296 static ptrdiff_t
5297 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5298 {
5299 const int MAX_DISTANCE = 1000;
5300 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5301 around_charpos + MAX_DISTANCE,
5302 0);
5303
5304 if (!found)
5305 found = string_buffer_position_lim (string, around_charpos,
5306 around_charpos - MAX_DISTANCE, 1);
5307 return found;
5308 }
5309
5310
5311 \f
5312 /***********************************************************************
5313 `composition' property
5314 ***********************************************************************/
5315
5316 /* Set up iterator IT from `composition' property at its current
5317 position. Called from handle_stop. */
5318
5319 static enum prop_handled
5320 handle_composition_prop (struct it *it)
5321 {
5322 Lisp_Object prop, string;
5323 ptrdiff_t pos, pos_byte, start, end;
5324
5325 if (STRINGP (it->string))
5326 {
5327 unsigned char *s;
5328
5329 pos = IT_STRING_CHARPOS (*it);
5330 pos_byte = IT_STRING_BYTEPOS (*it);
5331 string = it->string;
5332 s = SDATA (string) + pos_byte;
5333 it->c = STRING_CHAR (s);
5334 }
5335 else
5336 {
5337 pos = IT_CHARPOS (*it);
5338 pos_byte = IT_BYTEPOS (*it);
5339 string = Qnil;
5340 it->c = FETCH_CHAR (pos_byte);
5341 }
5342
5343 /* If there's a valid composition and point is not inside of the
5344 composition (in the case that the composition is from the current
5345 buffer), draw a glyph composed from the composition components. */
5346 if (find_composition (pos, -1, &start, &end, &prop, string)
5347 && composition_valid_p (start, end, prop)
5348 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5349 {
5350 if (start < pos)
5351 /* As we can't handle this situation (perhaps font-lock added
5352 a new composition), we just return here hoping that next
5353 redisplay will detect this composition much earlier. */
5354 return HANDLED_NORMALLY;
5355 if (start != pos)
5356 {
5357 if (STRINGP (it->string))
5358 pos_byte = string_char_to_byte (it->string, start);
5359 else
5360 pos_byte = CHAR_TO_BYTE (start);
5361 }
5362 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5363 prop, string);
5364
5365 if (it->cmp_it.id >= 0)
5366 {
5367 it->cmp_it.ch = -1;
5368 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5369 it->cmp_it.nglyphs = -1;
5370 }
5371 }
5372
5373 return HANDLED_NORMALLY;
5374 }
5375
5376
5377 \f
5378 /***********************************************************************
5379 Overlay strings
5380 ***********************************************************************/
5381
5382 /* The following structure is used to record overlay strings for
5383 later sorting in load_overlay_strings. */
5384
5385 struct overlay_entry
5386 {
5387 Lisp_Object overlay;
5388 Lisp_Object string;
5389 EMACS_INT priority;
5390 int after_string_p;
5391 };
5392
5393
5394 /* Set up iterator IT from overlay strings at its current position.
5395 Called from handle_stop. */
5396
5397 static enum prop_handled
5398 handle_overlay_change (struct it *it)
5399 {
5400 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5401 return HANDLED_RECOMPUTE_PROPS;
5402 else
5403 return HANDLED_NORMALLY;
5404 }
5405
5406
5407 /* Set up the next overlay string for delivery by IT, if there is an
5408 overlay string to deliver. Called by set_iterator_to_next when the
5409 end of the current overlay string is reached. If there are more
5410 overlay strings to display, IT->string and
5411 IT->current.overlay_string_index are set appropriately here.
5412 Otherwise IT->string is set to nil. */
5413
5414 static void
5415 next_overlay_string (struct it *it)
5416 {
5417 ++it->current.overlay_string_index;
5418 if (it->current.overlay_string_index == it->n_overlay_strings)
5419 {
5420 /* No more overlay strings. Restore IT's settings to what
5421 they were before overlay strings were processed, and
5422 continue to deliver from current_buffer. */
5423
5424 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5425 pop_it (it);
5426 eassert (it->sp > 0
5427 || (NILP (it->string)
5428 && it->method == GET_FROM_BUFFER
5429 && it->stop_charpos >= BEGV
5430 && it->stop_charpos <= it->end_charpos));
5431 it->current.overlay_string_index = -1;
5432 it->n_overlay_strings = 0;
5433 it->overlay_strings_charpos = -1;
5434 /* If there's an empty display string on the stack, pop the
5435 stack, to resync the bidi iterator with IT's position. Such
5436 empty strings are pushed onto the stack in
5437 get_overlay_strings_1. */
5438 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5439 pop_it (it);
5440
5441 /* If we're at the end of the buffer, record that we have
5442 processed the overlay strings there already, so that
5443 next_element_from_buffer doesn't try it again. */
5444 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5445 it->overlay_strings_at_end_processed_p = true;
5446 }
5447 else
5448 {
5449 /* There are more overlay strings to process. If
5450 IT->current.overlay_string_index has advanced to a position
5451 where we must load IT->overlay_strings with more strings, do
5452 it. We must load at the IT->overlay_strings_charpos where
5453 IT->n_overlay_strings was originally computed; when invisible
5454 text is present, this might not be IT_CHARPOS (Bug#7016). */
5455 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5456
5457 if (it->current.overlay_string_index && i == 0)
5458 load_overlay_strings (it, it->overlay_strings_charpos);
5459
5460 /* Initialize IT to deliver display elements from the overlay
5461 string. */
5462 it->string = it->overlay_strings[i];
5463 it->multibyte_p = STRING_MULTIBYTE (it->string);
5464 SET_TEXT_POS (it->current.string_pos, 0, 0);
5465 it->method = GET_FROM_STRING;
5466 it->stop_charpos = 0;
5467 it->end_charpos = SCHARS (it->string);
5468 if (it->cmp_it.stop_pos >= 0)
5469 it->cmp_it.stop_pos = 0;
5470 it->prev_stop = 0;
5471 it->base_level_stop = 0;
5472
5473 /* Set up the bidi iterator for this overlay string. */
5474 if (it->bidi_p)
5475 {
5476 it->bidi_it.string.lstring = it->string;
5477 it->bidi_it.string.s = NULL;
5478 it->bidi_it.string.schars = SCHARS (it->string);
5479 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5480 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5481 it->bidi_it.string.unibyte = !it->multibyte_p;
5482 it->bidi_it.w = it->w;
5483 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5484 }
5485 }
5486
5487 CHECK_IT (it);
5488 }
5489
5490
5491 /* Compare two overlay_entry structures E1 and E2. Used as a
5492 comparison function for qsort in load_overlay_strings. Overlay
5493 strings for the same position are sorted so that
5494
5495 1. All after-strings come in front of before-strings, except
5496 when they come from the same overlay.
5497
5498 2. Within after-strings, strings are sorted so that overlay strings
5499 from overlays with higher priorities come first.
5500
5501 2. Within before-strings, strings are sorted so that overlay
5502 strings from overlays with higher priorities come last.
5503
5504 Value is analogous to strcmp. */
5505
5506
5507 static int
5508 compare_overlay_entries (const void *e1, const void *e2)
5509 {
5510 struct overlay_entry const *entry1 = e1;
5511 struct overlay_entry const *entry2 = e2;
5512 int result;
5513
5514 if (entry1->after_string_p != entry2->after_string_p)
5515 {
5516 /* Let after-strings appear in front of before-strings if
5517 they come from different overlays. */
5518 if (EQ (entry1->overlay, entry2->overlay))
5519 result = entry1->after_string_p ? 1 : -1;
5520 else
5521 result = entry1->after_string_p ? -1 : 1;
5522 }
5523 else if (entry1->priority != entry2->priority)
5524 {
5525 if (entry1->after_string_p)
5526 /* After-strings sorted in order of decreasing priority. */
5527 result = entry2->priority < entry1->priority ? -1 : 1;
5528 else
5529 /* Before-strings sorted in order of increasing priority. */
5530 result = entry1->priority < entry2->priority ? -1 : 1;
5531 }
5532 else
5533 result = 0;
5534
5535 return result;
5536 }
5537
5538
5539 /* Load the vector IT->overlay_strings with overlay strings from IT's
5540 current buffer position, or from CHARPOS if that is > 0. Set
5541 IT->n_overlays to the total number of overlay strings found.
5542
5543 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5544 a time. On entry into load_overlay_strings,
5545 IT->current.overlay_string_index gives the number of overlay
5546 strings that have already been loaded by previous calls to this
5547 function.
5548
5549 IT->add_overlay_start contains an additional overlay start
5550 position to consider for taking overlay strings from, if non-zero.
5551 This position comes into play when the overlay has an `invisible'
5552 property, and both before and after-strings. When we've skipped to
5553 the end of the overlay, because of its `invisible' property, we
5554 nevertheless want its before-string to appear.
5555 IT->add_overlay_start will contain the overlay start position
5556 in this case.
5557
5558 Overlay strings are sorted so that after-string strings come in
5559 front of before-string strings. Within before and after-strings,
5560 strings are sorted by overlay priority. See also function
5561 compare_overlay_entries. */
5562
5563 static void
5564 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5565 {
5566 Lisp_Object overlay, window, str, invisible;
5567 struct Lisp_Overlay *ov;
5568 ptrdiff_t start, end;
5569 ptrdiff_t size = 20;
5570 ptrdiff_t n = 0, i, j;
5571 int invis_p;
5572 struct overlay_entry *entries = alloca (size * sizeof *entries);
5573 USE_SAFE_ALLOCA;
5574
5575 if (charpos <= 0)
5576 charpos = IT_CHARPOS (*it);
5577
5578 /* Append the overlay string STRING of overlay OVERLAY to vector
5579 `entries' which has size `size' and currently contains `n'
5580 elements. AFTER_P non-zero means STRING is an after-string of
5581 OVERLAY. */
5582 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5583 do \
5584 { \
5585 Lisp_Object priority; \
5586 \
5587 if (n == size) \
5588 { \
5589 struct overlay_entry *old = entries; \
5590 SAFE_NALLOCA (entries, 2, size); \
5591 memcpy (entries, old, size * sizeof *entries); \
5592 size *= 2; \
5593 } \
5594 \
5595 entries[n].string = (STRING); \
5596 entries[n].overlay = (OVERLAY); \
5597 priority = Foverlay_get ((OVERLAY), Qpriority); \
5598 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5599 entries[n].after_string_p = (AFTER_P); \
5600 ++n; \
5601 } \
5602 while (0)
5603
5604 /* Process overlay before the overlay center. */
5605 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5606 {
5607 XSETMISC (overlay, ov);
5608 eassert (OVERLAYP (overlay));
5609 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5610 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5611
5612 if (end < charpos)
5613 break;
5614
5615 /* Skip this overlay if it doesn't start or end at IT's current
5616 position. */
5617 if (end != charpos && start != charpos)
5618 continue;
5619
5620 /* Skip this overlay if it doesn't apply to IT->w. */
5621 window = Foverlay_get (overlay, Qwindow);
5622 if (WINDOWP (window) && XWINDOW (window) != it->w)
5623 continue;
5624
5625 /* If the text ``under'' the overlay is invisible, both before-
5626 and after-strings from this overlay are visible; start and
5627 end position are indistinguishable. */
5628 invisible = Foverlay_get (overlay, Qinvisible);
5629 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5630
5631 /* If overlay has a non-empty before-string, record it. */
5632 if ((start == charpos || (end == charpos && invis_p))
5633 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5634 && SCHARS (str))
5635 RECORD_OVERLAY_STRING (overlay, str, 0);
5636
5637 /* If overlay has a non-empty after-string, record it. */
5638 if ((end == charpos || (start == charpos && invis_p))
5639 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5640 && SCHARS (str))
5641 RECORD_OVERLAY_STRING (overlay, str, 1);
5642 }
5643
5644 /* Process overlays after the overlay center. */
5645 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5646 {
5647 XSETMISC (overlay, ov);
5648 eassert (OVERLAYP (overlay));
5649 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5650 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5651
5652 if (start > charpos)
5653 break;
5654
5655 /* Skip this overlay if it doesn't start or end at IT's current
5656 position. */
5657 if (end != charpos && start != charpos)
5658 continue;
5659
5660 /* Skip this overlay if it doesn't apply to IT->w. */
5661 window = Foverlay_get (overlay, Qwindow);
5662 if (WINDOWP (window) && XWINDOW (window) != it->w)
5663 continue;
5664
5665 /* If the text ``under'' the overlay is invisible, it has a zero
5666 dimension, and both before- and after-strings apply. */
5667 invisible = Foverlay_get (overlay, Qinvisible);
5668 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5669
5670 /* If overlay has a non-empty before-string, record it. */
5671 if ((start == charpos || (end == charpos && invis_p))
5672 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5673 && SCHARS (str))
5674 RECORD_OVERLAY_STRING (overlay, str, 0);
5675
5676 /* If overlay has a non-empty after-string, record it. */
5677 if ((end == charpos || (start == charpos && invis_p))
5678 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5679 && SCHARS (str))
5680 RECORD_OVERLAY_STRING (overlay, str, 1);
5681 }
5682
5683 #undef RECORD_OVERLAY_STRING
5684
5685 /* Sort entries. */
5686 if (n > 1)
5687 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5688
5689 /* Record number of overlay strings, and where we computed it. */
5690 it->n_overlay_strings = n;
5691 it->overlay_strings_charpos = charpos;
5692
5693 /* IT->current.overlay_string_index is the number of overlay strings
5694 that have already been consumed by IT. Copy some of the
5695 remaining overlay strings to IT->overlay_strings. */
5696 i = 0;
5697 j = it->current.overlay_string_index;
5698 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5699 {
5700 it->overlay_strings[i] = entries[j].string;
5701 it->string_overlays[i++] = entries[j++].overlay;
5702 }
5703
5704 CHECK_IT (it);
5705 SAFE_FREE ();
5706 }
5707
5708
5709 /* Get the first chunk of overlay strings at IT's current buffer
5710 position, or at CHARPOS if that is > 0. Value is non-zero if at
5711 least one overlay string was found. */
5712
5713 static int
5714 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5715 {
5716 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5717 process. This fills IT->overlay_strings with strings, and sets
5718 IT->n_overlay_strings to the total number of strings to process.
5719 IT->pos.overlay_string_index has to be set temporarily to zero
5720 because load_overlay_strings needs this; it must be set to -1
5721 when no overlay strings are found because a zero value would
5722 indicate a position in the first overlay string. */
5723 it->current.overlay_string_index = 0;
5724 load_overlay_strings (it, charpos);
5725
5726 /* If we found overlay strings, set up IT to deliver display
5727 elements from the first one. Otherwise set up IT to deliver
5728 from current_buffer. */
5729 if (it->n_overlay_strings)
5730 {
5731 /* Make sure we know settings in current_buffer, so that we can
5732 restore meaningful values when we're done with the overlay
5733 strings. */
5734 if (compute_stop_p)
5735 compute_stop_pos (it);
5736 eassert (it->face_id >= 0);
5737
5738 /* Save IT's settings. They are restored after all overlay
5739 strings have been processed. */
5740 eassert (!compute_stop_p || it->sp == 0);
5741
5742 /* When called from handle_stop, there might be an empty display
5743 string loaded. In that case, don't bother saving it. But
5744 don't use this optimization with the bidi iterator, since we
5745 need the corresponding pop_it call to resync the bidi
5746 iterator's position with IT's position, after we are done
5747 with the overlay strings. (The corresponding call to pop_it
5748 in case of an empty display string is in
5749 next_overlay_string.) */
5750 if (!(!it->bidi_p
5751 && STRINGP (it->string) && !SCHARS (it->string)))
5752 push_it (it, NULL);
5753
5754 /* Set up IT to deliver display elements from the first overlay
5755 string. */
5756 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5757 it->string = it->overlay_strings[0];
5758 it->from_overlay = Qnil;
5759 it->stop_charpos = 0;
5760 eassert (STRINGP (it->string));
5761 it->end_charpos = SCHARS (it->string);
5762 it->prev_stop = 0;
5763 it->base_level_stop = 0;
5764 it->multibyte_p = STRING_MULTIBYTE (it->string);
5765 it->method = GET_FROM_STRING;
5766 it->from_disp_prop_p = 0;
5767
5768 /* Force paragraph direction to be that of the parent
5769 buffer. */
5770 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5771 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5772 else
5773 it->paragraph_embedding = L2R;
5774
5775 /* Set up the bidi iterator for this overlay string. */
5776 if (it->bidi_p)
5777 {
5778 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5779
5780 it->bidi_it.string.lstring = it->string;
5781 it->bidi_it.string.s = NULL;
5782 it->bidi_it.string.schars = SCHARS (it->string);
5783 it->bidi_it.string.bufpos = pos;
5784 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5785 it->bidi_it.string.unibyte = !it->multibyte_p;
5786 it->bidi_it.w = it->w;
5787 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5788 }
5789 return 1;
5790 }
5791
5792 it->current.overlay_string_index = -1;
5793 return 0;
5794 }
5795
5796 static int
5797 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5798 {
5799 it->string = Qnil;
5800 it->method = GET_FROM_BUFFER;
5801
5802 (void) get_overlay_strings_1 (it, charpos, 1);
5803
5804 CHECK_IT (it);
5805
5806 /* Value is non-zero if we found at least one overlay string. */
5807 return STRINGP (it->string);
5808 }
5809
5810
5811 \f
5812 /***********************************************************************
5813 Saving and restoring state
5814 ***********************************************************************/
5815
5816 /* Save current settings of IT on IT->stack. Called, for example,
5817 before setting up IT for an overlay string, to be able to restore
5818 IT's settings to what they were after the overlay string has been
5819 processed. If POSITION is non-NULL, it is the position to save on
5820 the stack instead of IT->position. */
5821
5822 static void
5823 push_it (struct it *it, struct text_pos *position)
5824 {
5825 struct iterator_stack_entry *p;
5826
5827 eassert (it->sp < IT_STACK_SIZE);
5828 p = it->stack + it->sp;
5829
5830 p->stop_charpos = it->stop_charpos;
5831 p->prev_stop = it->prev_stop;
5832 p->base_level_stop = it->base_level_stop;
5833 p->cmp_it = it->cmp_it;
5834 eassert (it->face_id >= 0);
5835 p->face_id = it->face_id;
5836 p->string = it->string;
5837 p->method = it->method;
5838 p->from_overlay = it->from_overlay;
5839 switch (p->method)
5840 {
5841 case GET_FROM_IMAGE:
5842 p->u.image.object = it->object;
5843 p->u.image.image_id = it->image_id;
5844 p->u.image.slice = it->slice;
5845 break;
5846 case GET_FROM_STRETCH:
5847 p->u.stretch.object = it->object;
5848 break;
5849 }
5850 p->position = position ? *position : it->position;
5851 p->current = it->current;
5852 p->end_charpos = it->end_charpos;
5853 p->string_nchars = it->string_nchars;
5854 p->area = it->area;
5855 p->multibyte_p = it->multibyte_p;
5856 p->avoid_cursor_p = it->avoid_cursor_p;
5857 p->space_width = it->space_width;
5858 p->font_height = it->font_height;
5859 p->voffset = it->voffset;
5860 p->string_from_display_prop_p = it->string_from_display_prop_p;
5861 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5862 p->display_ellipsis_p = 0;
5863 p->line_wrap = it->line_wrap;
5864 p->bidi_p = it->bidi_p;
5865 p->paragraph_embedding = it->paragraph_embedding;
5866 p->from_disp_prop_p = it->from_disp_prop_p;
5867 ++it->sp;
5868
5869 /* Save the state of the bidi iterator as well. */
5870 if (it->bidi_p)
5871 bidi_push_it (&it->bidi_it);
5872 }
5873
5874 static void
5875 iterate_out_of_display_property (struct it *it)
5876 {
5877 int buffer_p = !STRINGP (it->string);
5878 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5879 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5880
5881 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5882
5883 /* Maybe initialize paragraph direction. If we are at the beginning
5884 of a new paragraph, next_element_from_buffer may not have a
5885 chance to do that. */
5886 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5887 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5888 /* prev_stop can be zero, so check against BEGV as well. */
5889 while (it->bidi_it.charpos >= bob
5890 && it->prev_stop <= it->bidi_it.charpos
5891 && it->bidi_it.charpos < CHARPOS (it->position)
5892 && it->bidi_it.charpos < eob)
5893 bidi_move_to_visually_next (&it->bidi_it);
5894 /* Record the stop_pos we just crossed, for when we cross it
5895 back, maybe. */
5896 if (it->bidi_it.charpos > CHARPOS (it->position))
5897 it->prev_stop = CHARPOS (it->position);
5898 /* If we ended up not where pop_it put us, resync IT's
5899 positional members with the bidi iterator. */
5900 if (it->bidi_it.charpos != CHARPOS (it->position))
5901 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5902 if (buffer_p)
5903 it->current.pos = it->position;
5904 else
5905 it->current.string_pos = it->position;
5906 }
5907
5908 /* Restore IT's settings from IT->stack. Called, for example, when no
5909 more overlay strings must be processed, and we return to delivering
5910 display elements from a buffer, or when the end of a string from a
5911 `display' property is reached and we return to delivering display
5912 elements from an overlay string, or from a buffer. */
5913
5914 static void
5915 pop_it (struct it *it)
5916 {
5917 struct iterator_stack_entry *p;
5918 int from_display_prop = it->from_disp_prop_p;
5919
5920 eassert (it->sp > 0);
5921 --it->sp;
5922 p = it->stack + it->sp;
5923 it->stop_charpos = p->stop_charpos;
5924 it->prev_stop = p->prev_stop;
5925 it->base_level_stop = p->base_level_stop;
5926 it->cmp_it = p->cmp_it;
5927 it->face_id = p->face_id;
5928 it->current = p->current;
5929 it->position = p->position;
5930 it->string = p->string;
5931 it->from_overlay = p->from_overlay;
5932 if (NILP (it->string))
5933 SET_TEXT_POS (it->current.string_pos, -1, -1);
5934 it->method = p->method;
5935 switch (it->method)
5936 {
5937 case GET_FROM_IMAGE:
5938 it->image_id = p->u.image.image_id;
5939 it->object = p->u.image.object;
5940 it->slice = p->u.image.slice;
5941 break;
5942 case GET_FROM_STRETCH:
5943 it->object = p->u.stretch.object;
5944 break;
5945 case GET_FROM_BUFFER:
5946 it->object = it->w->contents;
5947 break;
5948 case GET_FROM_STRING:
5949 it->object = it->string;
5950 break;
5951 case GET_FROM_DISPLAY_VECTOR:
5952 if (it->s)
5953 it->method = GET_FROM_C_STRING;
5954 else if (STRINGP (it->string))
5955 it->method = GET_FROM_STRING;
5956 else
5957 {
5958 it->method = GET_FROM_BUFFER;
5959 it->object = it->w->contents;
5960 }
5961 }
5962 it->end_charpos = p->end_charpos;
5963 it->string_nchars = p->string_nchars;
5964 it->area = p->area;
5965 it->multibyte_p = p->multibyte_p;
5966 it->avoid_cursor_p = p->avoid_cursor_p;
5967 it->space_width = p->space_width;
5968 it->font_height = p->font_height;
5969 it->voffset = p->voffset;
5970 it->string_from_display_prop_p = p->string_from_display_prop_p;
5971 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5972 it->line_wrap = p->line_wrap;
5973 it->bidi_p = p->bidi_p;
5974 it->paragraph_embedding = p->paragraph_embedding;
5975 it->from_disp_prop_p = p->from_disp_prop_p;
5976 if (it->bidi_p)
5977 {
5978 bidi_pop_it (&it->bidi_it);
5979 /* Bidi-iterate until we get out of the portion of text, if any,
5980 covered by a `display' text property or by an overlay with
5981 `display' property. (We cannot just jump there, because the
5982 internal coherency of the bidi iterator state can not be
5983 preserved across such jumps.) We also must determine the
5984 paragraph base direction if the overlay we just processed is
5985 at the beginning of a new paragraph. */
5986 if (from_display_prop
5987 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5988 iterate_out_of_display_property (it);
5989
5990 eassert ((BUFFERP (it->object)
5991 && IT_CHARPOS (*it) == it->bidi_it.charpos
5992 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5993 || (STRINGP (it->object)
5994 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5995 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5996 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5997 }
5998 }
5999
6000
6001 \f
6002 /***********************************************************************
6003 Moving over lines
6004 ***********************************************************************/
6005
6006 /* Set IT's current position to the previous line start. */
6007
6008 static void
6009 back_to_previous_line_start (struct it *it)
6010 {
6011 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6012
6013 DEC_BOTH (cp, bp);
6014 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6015 }
6016
6017
6018 /* Move IT to the next line start.
6019
6020 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6021 we skipped over part of the text (as opposed to moving the iterator
6022 continuously over the text). Otherwise, don't change the value
6023 of *SKIPPED_P.
6024
6025 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6026 iterator on the newline, if it was found.
6027
6028 Newlines may come from buffer text, overlay strings, or strings
6029 displayed via the `display' property. That's the reason we can't
6030 simply use find_newline_no_quit.
6031
6032 Note that this function may not skip over invisible text that is so
6033 because of text properties and immediately follows a newline. If
6034 it would, function reseat_at_next_visible_line_start, when called
6035 from set_iterator_to_next, would effectively make invisible
6036 characters following a newline part of the wrong glyph row, which
6037 leads to wrong cursor motion. */
6038
6039 static int
6040 forward_to_next_line_start (struct it *it, int *skipped_p,
6041 struct bidi_it *bidi_it_prev)
6042 {
6043 ptrdiff_t old_selective;
6044 int newline_found_p, n;
6045 const int MAX_NEWLINE_DISTANCE = 500;
6046
6047 /* If already on a newline, just consume it to avoid unintended
6048 skipping over invisible text below. */
6049 if (it->what == IT_CHARACTER
6050 && it->c == '\n'
6051 && CHARPOS (it->position) == IT_CHARPOS (*it))
6052 {
6053 if (it->bidi_p && bidi_it_prev)
6054 *bidi_it_prev = it->bidi_it;
6055 set_iterator_to_next (it, 0);
6056 it->c = 0;
6057 return 1;
6058 }
6059
6060 /* Don't handle selective display in the following. It's (a)
6061 unnecessary because it's done by the caller, and (b) leads to an
6062 infinite recursion because next_element_from_ellipsis indirectly
6063 calls this function. */
6064 old_selective = it->selective;
6065 it->selective = 0;
6066
6067 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6068 from buffer text. */
6069 for (n = newline_found_p = 0;
6070 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6071 n += STRINGP (it->string) ? 0 : 1)
6072 {
6073 if (!get_next_display_element (it))
6074 return 0;
6075 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6076 if (newline_found_p && it->bidi_p && bidi_it_prev)
6077 *bidi_it_prev = it->bidi_it;
6078 set_iterator_to_next (it, 0);
6079 }
6080
6081 /* If we didn't find a newline near enough, see if we can use a
6082 short-cut. */
6083 if (!newline_found_p)
6084 {
6085 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6086 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6087 1, &bytepos);
6088 Lisp_Object pos;
6089
6090 eassert (!STRINGP (it->string));
6091
6092 /* If there isn't any `display' property in sight, and no
6093 overlays, we can just use the position of the newline in
6094 buffer text. */
6095 if (it->stop_charpos >= limit
6096 || ((pos = Fnext_single_property_change (make_number (start),
6097 Qdisplay, Qnil,
6098 make_number (limit)),
6099 NILP (pos))
6100 && next_overlay_change (start) == ZV))
6101 {
6102 if (!it->bidi_p)
6103 {
6104 IT_CHARPOS (*it) = limit;
6105 IT_BYTEPOS (*it) = bytepos;
6106 }
6107 else
6108 {
6109 struct bidi_it bprev;
6110
6111 /* Help bidi.c avoid expensive searches for display
6112 properties and overlays, by telling it that there are
6113 none up to `limit'. */
6114 if (it->bidi_it.disp_pos < limit)
6115 {
6116 it->bidi_it.disp_pos = limit;
6117 it->bidi_it.disp_prop = 0;
6118 }
6119 do {
6120 bprev = it->bidi_it;
6121 bidi_move_to_visually_next (&it->bidi_it);
6122 } while (it->bidi_it.charpos != limit);
6123 IT_CHARPOS (*it) = limit;
6124 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6125 if (bidi_it_prev)
6126 *bidi_it_prev = bprev;
6127 }
6128 *skipped_p = newline_found_p = true;
6129 }
6130 else
6131 {
6132 while (get_next_display_element (it)
6133 && !newline_found_p)
6134 {
6135 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6136 if (newline_found_p && it->bidi_p && bidi_it_prev)
6137 *bidi_it_prev = it->bidi_it;
6138 set_iterator_to_next (it, 0);
6139 }
6140 }
6141 }
6142
6143 it->selective = old_selective;
6144 return newline_found_p;
6145 }
6146
6147
6148 /* Set IT's current position to the previous visible line start. Skip
6149 invisible text that is so either due to text properties or due to
6150 selective display. Caution: this does not change IT->current_x and
6151 IT->hpos. */
6152
6153 static void
6154 back_to_previous_visible_line_start (struct it *it)
6155 {
6156 while (IT_CHARPOS (*it) > BEGV)
6157 {
6158 back_to_previous_line_start (it);
6159
6160 if (IT_CHARPOS (*it) <= BEGV)
6161 break;
6162
6163 /* If selective > 0, then lines indented more than its value are
6164 invisible. */
6165 if (it->selective > 0
6166 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6167 it->selective))
6168 continue;
6169
6170 /* Check the newline before point for invisibility. */
6171 {
6172 Lisp_Object prop;
6173 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6174 Qinvisible, it->window);
6175 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6176 continue;
6177 }
6178
6179 if (IT_CHARPOS (*it) <= BEGV)
6180 break;
6181
6182 {
6183 struct it it2;
6184 void *it2data = NULL;
6185 ptrdiff_t pos;
6186 ptrdiff_t beg, end;
6187 Lisp_Object val, overlay;
6188
6189 SAVE_IT (it2, *it, it2data);
6190
6191 /* If newline is part of a composition, continue from start of composition */
6192 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6193 && beg < IT_CHARPOS (*it))
6194 goto replaced;
6195
6196 /* If newline is replaced by a display property, find start of overlay
6197 or interval and continue search from that point. */
6198 pos = --IT_CHARPOS (it2);
6199 --IT_BYTEPOS (it2);
6200 it2.sp = 0;
6201 bidi_unshelve_cache (NULL, 0);
6202 it2.string_from_display_prop_p = 0;
6203 it2.from_disp_prop_p = 0;
6204 if (handle_display_prop (&it2) == HANDLED_RETURN
6205 && !NILP (val = get_char_property_and_overlay
6206 (make_number (pos), Qdisplay, Qnil, &overlay))
6207 && (OVERLAYP (overlay)
6208 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6209 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6210 {
6211 RESTORE_IT (it, it, it2data);
6212 goto replaced;
6213 }
6214
6215 /* Newline is not replaced by anything -- so we are done. */
6216 RESTORE_IT (it, it, it2data);
6217 break;
6218
6219 replaced:
6220 if (beg < BEGV)
6221 beg = BEGV;
6222 IT_CHARPOS (*it) = beg;
6223 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6224 }
6225 }
6226
6227 it->continuation_lines_width = 0;
6228
6229 eassert (IT_CHARPOS (*it) >= BEGV);
6230 eassert (IT_CHARPOS (*it) == BEGV
6231 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6232 CHECK_IT (it);
6233 }
6234
6235
6236 /* Reseat iterator IT at the previous visible line start. Skip
6237 invisible text that is so either due to text properties or due to
6238 selective display. At the end, update IT's overlay information,
6239 face information etc. */
6240
6241 void
6242 reseat_at_previous_visible_line_start (struct it *it)
6243 {
6244 back_to_previous_visible_line_start (it);
6245 reseat (it, it->current.pos, 1);
6246 CHECK_IT (it);
6247 }
6248
6249
6250 /* Reseat iterator IT on the next visible line start in the current
6251 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6252 preceding the line start. Skip over invisible text that is so
6253 because of selective display. Compute faces, overlays etc at the
6254 new position. Note that this function does not skip over text that
6255 is invisible because of text properties. */
6256
6257 static void
6258 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6259 {
6260 int newline_found_p, skipped_p = 0;
6261 struct bidi_it bidi_it_prev;
6262
6263 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6264
6265 /* Skip over lines that are invisible because they are indented
6266 more than the value of IT->selective. */
6267 if (it->selective > 0)
6268 while (IT_CHARPOS (*it) < ZV
6269 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6270 it->selective))
6271 {
6272 eassert (IT_BYTEPOS (*it) == BEGV
6273 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6274 newline_found_p =
6275 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6276 }
6277
6278 /* Position on the newline if that's what's requested. */
6279 if (on_newline_p && newline_found_p)
6280 {
6281 if (STRINGP (it->string))
6282 {
6283 if (IT_STRING_CHARPOS (*it) > 0)
6284 {
6285 if (!it->bidi_p)
6286 {
6287 --IT_STRING_CHARPOS (*it);
6288 --IT_STRING_BYTEPOS (*it);
6289 }
6290 else
6291 {
6292 /* We need to restore the bidi iterator to the state
6293 it had on the newline, and resync the IT's
6294 position with that. */
6295 it->bidi_it = bidi_it_prev;
6296 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6297 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6298 }
6299 }
6300 }
6301 else if (IT_CHARPOS (*it) > BEGV)
6302 {
6303 if (!it->bidi_p)
6304 {
6305 --IT_CHARPOS (*it);
6306 --IT_BYTEPOS (*it);
6307 }
6308 else
6309 {
6310 /* We need to restore the bidi iterator to the state it
6311 had on the newline and resync IT with that. */
6312 it->bidi_it = bidi_it_prev;
6313 IT_CHARPOS (*it) = it->bidi_it.charpos;
6314 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6315 }
6316 reseat (it, it->current.pos, 0);
6317 }
6318 }
6319 else if (skipped_p)
6320 reseat (it, it->current.pos, 0);
6321
6322 CHECK_IT (it);
6323 }
6324
6325
6326 \f
6327 /***********************************************************************
6328 Changing an iterator's position
6329 ***********************************************************************/
6330
6331 /* Change IT's current position to POS in current_buffer. If FORCE_P
6332 is non-zero, always check for text properties at the new position.
6333 Otherwise, text properties are only looked up if POS >=
6334 IT->check_charpos of a property. */
6335
6336 static void
6337 reseat (struct it *it, struct text_pos pos, int force_p)
6338 {
6339 ptrdiff_t original_pos = IT_CHARPOS (*it);
6340
6341 reseat_1 (it, pos, 0);
6342
6343 /* Determine where to check text properties. Avoid doing it
6344 where possible because text property lookup is very expensive. */
6345 if (force_p
6346 || CHARPOS (pos) > it->stop_charpos
6347 || CHARPOS (pos) < original_pos)
6348 {
6349 if (it->bidi_p)
6350 {
6351 /* For bidi iteration, we need to prime prev_stop and
6352 base_level_stop with our best estimations. */
6353 /* Implementation note: Of course, POS is not necessarily a
6354 stop position, so assigning prev_pos to it is a lie; we
6355 should have called compute_stop_backwards. However, if
6356 the current buffer does not include any R2L characters,
6357 that call would be a waste of cycles, because the
6358 iterator will never move back, and thus never cross this
6359 "fake" stop position. So we delay that backward search
6360 until the time we really need it, in next_element_from_buffer. */
6361 if (CHARPOS (pos) != it->prev_stop)
6362 it->prev_stop = CHARPOS (pos);
6363 if (CHARPOS (pos) < it->base_level_stop)
6364 it->base_level_stop = 0; /* meaning it's unknown */
6365 handle_stop (it);
6366 }
6367 else
6368 {
6369 handle_stop (it);
6370 it->prev_stop = it->base_level_stop = 0;
6371 }
6372
6373 }
6374
6375 CHECK_IT (it);
6376 }
6377
6378
6379 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6380 IT->stop_pos to POS, also. */
6381
6382 static void
6383 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6384 {
6385 /* Don't call this function when scanning a C string. */
6386 eassert (it->s == NULL);
6387
6388 /* POS must be a reasonable value. */
6389 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6390
6391 it->current.pos = it->position = pos;
6392 it->end_charpos = ZV;
6393 it->dpvec = NULL;
6394 it->current.dpvec_index = -1;
6395 it->current.overlay_string_index = -1;
6396 IT_STRING_CHARPOS (*it) = -1;
6397 IT_STRING_BYTEPOS (*it) = -1;
6398 it->string = Qnil;
6399 it->method = GET_FROM_BUFFER;
6400 it->object = it->w->contents;
6401 it->area = TEXT_AREA;
6402 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6403 it->sp = 0;
6404 it->string_from_display_prop_p = 0;
6405 it->string_from_prefix_prop_p = 0;
6406
6407 it->from_disp_prop_p = 0;
6408 it->face_before_selective_p = 0;
6409 if (it->bidi_p)
6410 {
6411 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6412 &it->bidi_it);
6413 bidi_unshelve_cache (NULL, 0);
6414 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6415 it->bidi_it.string.s = NULL;
6416 it->bidi_it.string.lstring = Qnil;
6417 it->bidi_it.string.bufpos = 0;
6418 it->bidi_it.string.from_disp_str = 0;
6419 it->bidi_it.string.unibyte = 0;
6420 it->bidi_it.w = it->w;
6421 }
6422
6423 if (set_stop_p)
6424 {
6425 it->stop_charpos = CHARPOS (pos);
6426 it->base_level_stop = CHARPOS (pos);
6427 }
6428 /* This make the information stored in it->cmp_it invalidate. */
6429 it->cmp_it.id = -1;
6430 }
6431
6432
6433 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6434 If S is non-null, it is a C string to iterate over. Otherwise,
6435 STRING gives a Lisp string to iterate over.
6436
6437 If PRECISION > 0, don't return more then PRECISION number of
6438 characters from the string.
6439
6440 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6441 characters have been returned. FIELD_WIDTH < 0 means an infinite
6442 field width.
6443
6444 MULTIBYTE = 0 means disable processing of multibyte characters,
6445 MULTIBYTE > 0 means enable it,
6446 MULTIBYTE < 0 means use IT->multibyte_p.
6447
6448 IT must be initialized via a prior call to init_iterator before
6449 calling this function. */
6450
6451 static void
6452 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6453 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6454 int multibyte)
6455 {
6456 /* No text property checks performed by default, but see below. */
6457 it->stop_charpos = -1;
6458
6459 /* Set iterator position and end position. */
6460 memset (&it->current, 0, sizeof it->current);
6461 it->current.overlay_string_index = -1;
6462 it->current.dpvec_index = -1;
6463 eassert (charpos >= 0);
6464
6465 /* If STRING is specified, use its multibyteness, otherwise use the
6466 setting of MULTIBYTE, if specified. */
6467 if (multibyte >= 0)
6468 it->multibyte_p = multibyte > 0;
6469
6470 /* Bidirectional reordering of strings is controlled by the default
6471 value of bidi-display-reordering. Don't try to reorder while
6472 loading loadup.el, as the necessary character property tables are
6473 not yet available. */
6474 it->bidi_p =
6475 NILP (Vpurify_flag)
6476 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6477
6478 if (s == NULL)
6479 {
6480 eassert (STRINGP (string));
6481 it->string = string;
6482 it->s = NULL;
6483 it->end_charpos = it->string_nchars = SCHARS (string);
6484 it->method = GET_FROM_STRING;
6485 it->current.string_pos = string_pos (charpos, string);
6486
6487 if (it->bidi_p)
6488 {
6489 it->bidi_it.string.lstring = string;
6490 it->bidi_it.string.s = NULL;
6491 it->bidi_it.string.schars = it->end_charpos;
6492 it->bidi_it.string.bufpos = 0;
6493 it->bidi_it.string.from_disp_str = 0;
6494 it->bidi_it.string.unibyte = !it->multibyte_p;
6495 it->bidi_it.w = it->w;
6496 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6497 FRAME_WINDOW_P (it->f), &it->bidi_it);
6498 }
6499 }
6500 else
6501 {
6502 it->s = (const unsigned char *) s;
6503 it->string = Qnil;
6504
6505 /* Note that we use IT->current.pos, not it->current.string_pos,
6506 for displaying C strings. */
6507 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6508 if (it->multibyte_p)
6509 {
6510 it->current.pos = c_string_pos (charpos, s, 1);
6511 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6512 }
6513 else
6514 {
6515 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6516 it->end_charpos = it->string_nchars = strlen (s);
6517 }
6518
6519 if (it->bidi_p)
6520 {
6521 it->bidi_it.string.lstring = Qnil;
6522 it->bidi_it.string.s = (const unsigned char *) s;
6523 it->bidi_it.string.schars = it->end_charpos;
6524 it->bidi_it.string.bufpos = 0;
6525 it->bidi_it.string.from_disp_str = 0;
6526 it->bidi_it.string.unibyte = !it->multibyte_p;
6527 it->bidi_it.w = it->w;
6528 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6529 &it->bidi_it);
6530 }
6531 it->method = GET_FROM_C_STRING;
6532 }
6533
6534 /* PRECISION > 0 means don't return more than PRECISION characters
6535 from the string. */
6536 if (precision > 0 && it->end_charpos - charpos > precision)
6537 {
6538 it->end_charpos = it->string_nchars = charpos + precision;
6539 if (it->bidi_p)
6540 it->bidi_it.string.schars = it->end_charpos;
6541 }
6542
6543 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6544 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6545 FIELD_WIDTH < 0 means infinite field width. This is useful for
6546 padding with `-' at the end of a mode line. */
6547 if (field_width < 0)
6548 field_width = INFINITY;
6549 /* Implementation note: We deliberately don't enlarge
6550 it->bidi_it.string.schars here to fit it->end_charpos, because
6551 the bidi iterator cannot produce characters out of thin air. */
6552 if (field_width > it->end_charpos - charpos)
6553 it->end_charpos = charpos + field_width;
6554
6555 /* Use the standard display table for displaying strings. */
6556 if (DISP_TABLE_P (Vstandard_display_table))
6557 it->dp = XCHAR_TABLE (Vstandard_display_table);
6558
6559 it->stop_charpos = charpos;
6560 it->prev_stop = charpos;
6561 it->base_level_stop = 0;
6562 if (it->bidi_p)
6563 {
6564 it->bidi_it.first_elt = 1;
6565 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6566 it->bidi_it.disp_pos = -1;
6567 }
6568 if (s == NULL && it->multibyte_p)
6569 {
6570 ptrdiff_t endpos = SCHARS (it->string);
6571 if (endpos > it->end_charpos)
6572 endpos = it->end_charpos;
6573 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6574 it->string);
6575 }
6576 CHECK_IT (it);
6577 }
6578
6579
6580 \f
6581 /***********************************************************************
6582 Iteration
6583 ***********************************************************************/
6584
6585 /* Map enum it_method value to corresponding next_element_from_* function. */
6586
6587 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6588 {
6589 next_element_from_buffer,
6590 next_element_from_display_vector,
6591 next_element_from_string,
6592 next_element_from_c_string,
6593 next_element_from_image,
6594 next_element_from_stretch
6595 };
6596
6597 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6598
6599
6600 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6601 (possibly with the following characters). */
6602
6603 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6604 ((IT)->cmp_it.id >= 0 \
6605 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6606 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6607 END_CHARPOS, (IT)->w, \
6608 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6609 (IT)->string)))
6610
6611
6612 /* Lookup the char-table Vglyphless_char_display for character C (-1
6613 if we want information for no-font case), and return the display
6614 method symbol. By side-effect, update it->what and
6615 it->glyphless_method. This function is called from
6616 get_next_display_element for each character element, and from
6617 x_produce_glyphs when no suitable font was found. */
6618
6619 Lisp_Object
6620 lookup_glyphless_char_display (int c, struct it *it)
6621 {
6622 Lisp_Object glyphless_method = Qnil;
6623
6624 if (CHAR_TABLE_P (Vglyphless_char_display)
6625 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6626 {
6627 if (c >= 0)
6628 {
6629 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6630 if (CONSP (glyphless_method))
6631 glyphless_method = FRAME_WINDOW_P (it->f)
6632 ? XCAR (glyphless_method)
6633 : XCDR (glyphless_method);
6634 }
6635 else
6636 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6637 }
6638
6639 retry:
6640 if (NILP (glyphless_method))
6641 {
6642 if (c >= 0)
6643 /* The default is to display the character by a proper font. */
6644 return Qnil;
6645 /* The default for the no-font case is to display an empty box. */
6646 glyphless_method = Qempty_box;
6647 }
6648 if (EQ (glyphless_method, Qzero_width))
6649 {
6650 if (c >= 0)
6651 return glyphless_method;
6652 /* This method can't be used for the no-font case. */
6653 glyphless_method = Qempty_box;
6654 }
6655 if (EQ (glyphless_method, Qthin_space))
6656 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6657 else if (EQ (glyphless_method, Qempty_box))
6658 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6659 else if (EQ (glyphless_method, Qhex_code))
6660 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6661 else if (STRINGP (glyphless_method))
6662 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6663 else
6664 {
6665 /* Invalid value. We use the default method. */
6666 glyphless_method = Qnil;
6667 goto retry;
6668 }
6669 it->what = IT_GLYPHLESS;
6670 return glyphless_method;
6671 }
6672
6673 /* Merge escape glyph face and cache the result. */
6674
6675 static struct frame *last_escape_glyph_frame = NULL;
6676 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6677 static int last_escape_glyph_merged_face_id = 0;
6678
6679 static int
6680 merge_escape_glyph_face (struct it *it)
6681 {
6682 int face_id;
6683
6684 if (it->f == last_escape_glyph_frame
6685 && it->face_id == last_escape_glyph_face_id)
6686 face_id = last_escape_glyph_merged_face_id;
6687 else
6688 {
6689 /* Merge the `escape-glyph' face into the current face. */
6690 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6691 last_escape_glyph_frame = it->f;
6692 last_escape_glyph_face_id = it->face_id;
6693 last_escape_glyph_merged_face_id = face_id;
6694 }
6695 return face_id;
6696 }
6697
6698 /* Likewise for glyphless glyph face. */
6699
6700 static struct frame *last_glyphless_glyph_frame = NULL;
6701 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6702 static int last_glyphless_glyph_merged_face_id = 0;
6703
6704 int
6705 merge_glyphless_glyph_face (struct it *it)
6706 {
6707 int face_id;
6708
6709 if (it->f == last_glyphless_glyph_frame
6710 && it->face_id == last_glyphless_glyph_face_id)
6711 face_id = last_glyphless_glyph_merged_face_id;
6712 else
6713 {
6714 /* Merge the `glyphless-char' face into the current face. */
6715 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6716 last_glyphless_glyph_frame = it->f;
6717 last_glyphless_glyph_face_id = it->face_id;
6718 last_glyphless_glyph_merged_face_id = face_id;
6719 }
6720 return face_id;
6721 }
6722
6723 /* Load IT's display element fields with information about the next
6724 display element from the current position of IT. Value is zero if
6725 end of buffer (or C string) is reached. */
6726
6727 static int
6728 get_next_display_element (struct it *it)
6729 {
6730 /* Non-zero means that we found a display element. Zero means that
6731 we hit the end of what we iterate over. Performance note: the
6732 function pointer `method' used here turns out to be faster than
6733 using a sequence of if-statements. */
6734 int success_p;
6735
6736 get_next:
6737 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6738
6739 if (it->what == IT_CHARACTER)
6740 {
6741 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6742 and only if (a) the resolved directionality of that character
6743 is R..." */
6744 /* FIXME: Do we need an exception for characters from display
6745 tables? */
6746 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6747 it->c = bidi_mirror_char (it->c);
6748 /* Map via display table or translate control characters.
6749 IT->c, IT->len etc. have been set to the next character by
6750 the function call above. If we have a display table, and it
6751 contains an entry for IT->c, translate it. Don't do this if
6752 IT->c itself comes from a display table, otherwise we could
6753 end up in an infinite recursion. (An alternative could be to
6754 count the recursion depth of this function and signal an
6755 error when a certain maximum depth is reached.) Is it worth
6756 it? */
6757 if (success_p && it->dpvec == NULL)
6758 {
6759 Lisp_Object dv;
6760 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6761 int nonascii_space_p = 0;
6762 int nonascii_hyphen_p = 0;
6763 int c = it->c; /* This is the character to display. */
6764
6765 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6766 {
6767 eassert (SINGLE_BYTE_CHAR_P (c));
6768 if (unibyte_display_via_language_environment)
6769 {
6770 c = DECODE_CHAR (unibyte, c);
6771 if (c < 0)
6772 c = BYTE8_TO_CHAR (it->c);
6773 }
6774 else
6775 c = BYTE8_TO_CHAR (it->c);
6776 }
6777
6778 if (it->dp
6779 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6780 VECTORP (dv)))
6781 {
6782 struct Lisp_Vector *v = XVECTOR (dv);
6783
6784 /* Return the first character from the display table
6785 entry, if not empty. If empty, don't display the
6786 current character. */
6787 if (v->header.size)
6788 {
6789 it->dpvec_char_len = it->len;
6790 it->dpvec = v->contents;
6791 it->dpend = v->contents + v->header.size;
6792 it->current.dpvec_index = 0;
6793 it->dpvec_face_id = -1;
6794 it->saved_face_id = it->face_id;
6795 it->method = GET_FROM_DISPLAY_VECTOR;
6796 it->ellipsis_p = 0;
6797 }
6798 else
6799 {
6800 set_iterator_to_next (it, 0);
6801 }
6802 goto get_next;
6803 }
6804
6805 if (! NILP (lookup_glyphless_char_display (c, it)))
6806 {
6807 if (it->what == IT_GLYPHLESS)
6808 goto done;
6809 /* Don't display this character. */
6810 set_iterator_to_next (it, 0);
6811 goto get_next;
6812 }
6813
6814 /* If `nobreak-char-display' is non-nil, we display
6815 non-ASCII spaces and hyphens specially. */
6816 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6817 {
6818 if (c == 0xA0)
6819 nonascii_space_p = true;
6820 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6821 nonascii_hyphen_p = true;
6822 }
6823
6824 /* Translate control characters into `\003' or `^C' form.
6825 Control characters coming from a display table entry are
6826 currently not translated because we use IT->dpvec to hold
6827 the translation. This could easily be changed but I
6828 don't believe that it is worth doing.
6829
6830 The characters handled by `nobreak-char-display' must be
6831 translated too.
6832
6833 Non-printable characters and raw-byte characters are also
6834 translated to octal form. */
6835 if (((c < ' ' || c == 127) /* ASCII control chars. */
6836 ? (it->area != TEXT_AREA
6837 /* In mode line, treat \n, \t like other crl chars. */
6838 || (c != '\t'
6839 && it->glyph_row
6840 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6841 || (c != '\n' && c != '\t'))
6842 : (nonascii_space_p
6843 || nonascii_hyphen_p
6844 || CHAR_BYTE8_P (c)
6845 || ! CHAR_PRINTABLE_P (c))))
6846 {
6847 /* C is a control character, non-ASCII space/hyphen,
6848 raw-byte, or a non-printable character which must be
6849 displayed either as '\003' or as `^C' where the '\\'
6850 and '^' can be defined in the display table. Fill
6851 IT->ctl_chars with glyphs for what we have to
6852 display. Then, set IT->dpvec to these glyphs. */
6853 Lisp_Object gc;
6854 int ctl_len;
6855 int face_id;
6856 int lface_id = 0;
6857 int escape_glyph;
6858
6859 /* Handle control characters with ^. */
6860
6861 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6862 {
6863 int g;
6864
6865 g = '^'; /* default glyph for Control */
6866 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6867 if (it->dp
6868 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6869 {
6870 g = GLYPH_CODE_CHAR (gc);
6871 lface_id = GLYPH_CODE_FACE (gc);
6872 }
6873
6874 face_id = (lface_id
6875 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6876 : merge_escape_glyph_face (it));
6877
6878 XSETINT (it->ctl_chars[0], g);
6879 XSETINT (it->ctl_chars[1], c ^ 0100);
6880 ctl_len = 2;
6881 goto display_control;
6882 }
6883
6884 /* Handle non-ascii space in the mode where it only gets
6885 highlighting. */
6886
6887 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6888 {
6889 /* Merge `nobreak-space' into the current face. */
6890 face_id = merge_faces (it->f, Qnobreak_space, 0,
6891 it->face_id);
6892 XSETINT (it->ctl_chars[0], ' ');
6893 ctl_len = 1;
6894 goto display_control;
6895 }
6896
6897 /* Handle sequences that start with the "escape glyph". */
6898
6899 /* the default escape glyph is \. */
6900 escape_glyph = '\\';
6901
6902 if (it->dp
6903 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6904 {
6905 escape_glyph = GLYPH_CODE_CHAR (gc);
6906 lface_id = GLYPH_CODE_FACE (gc);
6907 }
6908
6909 face_id = (lface_id
6910 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6911 : merge_escape_glyph_face (it));
6912
6913 /* Draw non-ASCII hyphen with just highlighting: */
6914
6915 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6916 {
6917 XSETINT (it->ctl_chars[0], '-');
6918 ctl_len = 1;
6919 goto display_control;
6920 }
6921
6922 /* Draw non-ASCII space/hyphen with escape glyph: */
6923
6924 if (nonascii_space_p || nonascii_hyphen_p)
6925 {
6926 XSETINT (it->ctl_chars[0], escape_glyph);
6927 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6928 ctl_len = 2;
6929 goto display_control;
6930 }
6931
6932 {
6933 char str[10];
6934 int len, i;
6935
6936 if (CHAR_BYTE8_P (c))
6937 /* Display \200 instead of \17777600. */
6938 c = CHAR_TO_BYTE8 (c);
6939 len = sprintf (str, "%03o", c);
6940
6941 XSETINT (it->ctl_chars[0], escape_glyph);
6942 for (i = 0; i < len; i++)
6943 XSETINT (it->ctl_chars[i + 1], str[i]);
6944 ctl_len = len + 1;
6945 }
6946
6947 display_control:
6948 /* Set up IT->dpvec and return first character from it. */
6949 it->dpvec_char_len = it->len;
6950 it->dpvec = it->ctl_chars;
6951 it->dpend = it->dpvec + ctl_len;
6952 it->current.dpvec_index = 0;
6953 it->dpvec_face_id = face_id;
6954 it->saved_face_id = it->face_id;
6955 it->method = GET_FROM_DISPLAY_VECTOR;
6956 it->ellipsis_p = 0;
6957 goto get_next;
6958 }
6959 it->char_to_display = c;
6960 }
6961 else if (success_p)
6962 {
6963 it->char_to_display = it->c;
6964 }
6965 }
6966
6967 #ifdef HAVE_WINDOW_SYSTEM
6968 /* Adjust face id for a multibyte character. There are no multibyte
6969 character in unibyte text. */
6970 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6971 && it->multibyte_p
6972 && success_p
6973 && FRAME_WINDOW_P (it->f))
6974 {
6975 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6976
6977 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6978 {
6979 /* Automatic composition with glyph-string. */
6980 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6981
6982 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6983 }
6984 else
6985 {
6986 ptrdiff_t pos = (it->s ? -1
6987 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6988 : IT_CHARPOS (*it));
6989 int c;
6990
6991 if (it->what == IT_CHARACTER)
6992 c = it->char_to_display;
6993 else
6994 {
6995 struct composition *cmp = composition_table[it->cmp_it.id];
6996 int i;
6997
6998 c = ' ';
6999 for (i = 0; i < cmp->glyph_len; i++)
7000 /* TAB in a composition means display glyphs with
7001 padding space on the left or right. */
7002 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7003 break;
7004 }
7005 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7006 }
7007 }
7008 #endif /* HAVE_WINDOW_SYSTEM */
7009
7010 done:
7011 /* Is this character the last one of a run of characters with
7012 box? If yes, set IT->end_of_box_run_p to 1. */
7013 if (it->face_box_p
7014 && it->s == NULL)
7015 {
7016 if (it->method == GET_FROM_STRING && it->sp)
7017 {
7018 int face_id = underlying_face_id (it);
7019 struct face *face = FACE_FROM_ID (it->f, face_id);
7020
7021 if (face)
7022 {
7023 if (face->box == FACE_NO_BOX)
7024 {
7025 /* If the box comes from face properties in a
7026 display string, check faces in that string. */
7027 int string_face_id = face_after_it_pos (it);
7028 it->end_of_box_run_p
7029 = (FACE_FROM_ID (it->f, string_face_id)->box
7030 == FACE_NO_BOX);
7031 }
7032 /* Otherwise, the box comes from the underlying face.
7033 If this is the last string character displayed, check
7034 the next buffer location. */
7035 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7036 && (it->current.overlay_string_index
7037 == it->n_overlay_strings - 1))
7038 {
7039 ptrdiff_t ignore;
7040 int next_face_id;
7041 struct text_pos pos = it->current.pos;
7042 INC_TEXT_POS (pos, it->multibyte_p);
7043
7044 next_face_id = face_at_buffer_position
7045 (it->w, CHARPOS (pos), &ignore,
7046 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7047 -1);
7048 it->end_of_box_run_p
7049 = (FACE_FROM_ID (it->f, next_face_id)->box
7050 == FACE_NO_BOX);
7051 }
7052 }
7053 }
7054 /* next_element_from_display_vector sets this flag according to
7055 faces of the display vector glyphs, see there. */
7056 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7057 {
7058 int face_id = face_after_it_pos (it);
7059 it->end_of_box_run_p
7060 = (face_id != it->face_id
7061 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7062 }
7063 }
7064 /* If we reached the end of the object we've been iterating (e.g., a
7065 display string or an overlay string), and there's something on
7066 IT->stack, proceed with what's on the stack. It doesn't make
7067 sense to return zero if there's unprocessed stuff on the stack,
7068 because otherwise that stuff will never be displayed. */
7069 if (!success_p && it->sp > 0)
7070 {
7071 set_iterator_to_next (it, 0);
7072 success_p = get_next_display_element (it);
7073 }
7074
7075 /* Value is 0 if end of buffer or string reached. */
7076 return success_p;
7077 }
7078
7079
7080 /* Move IT to the next display element.
7081
7082 RESEAT_P non-zero means if called on a newline in buffer text,
7083 skip to the next visible line start.
7084
7085 Functions get_next_display_element and set_iterator_to_next are
7086 separate because I find this arrangement easier to handle than a
7087 get_next_display_element function that also increments IT's
7088 position. The way it is we can first look at an iterator's current
7089 display element, decide whether it fits on a line, and if it does,
7090 increment the iterator position. The other way around we probably
7091 would either need a flag indicating whether the iterator has to be
7092 incremented the next time, or we would have to implement a
7093 decrement position function which would not be easy to write. */
7094
7095 void
7096 set_iterator_to_next (struct it *it, int reseat_p)
7097 {
7098 /* Reset flags indicating start and end of a sequence of characters
7099 with box. Reset them at the start of this function because
7100 moving the iterator to a new position might set them. */
7101 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7102
7103 switch (it->method)
7104 {
7105 case GET_FROM_BUFFER:
7106 /* The current display element of IT is a character from
7107 current_buffer. Advance in the buffer, and maybe skip over
7108 invisible lines that are so because of selective display. */
7109 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7110 reseat_at_next_visible_line_start (it, 0);
7111 else if (it->cmp_it.id >= 0)
7112 {
7113 /* We are currently getting glyphs from a composition. */
7114 int i;
7115
7116 if (! it->bidi_p)
7117 {
7118 IT_CHARPOS (*it) += it->cmp_it.nchars;
7119 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7120 if (it->cmp_it.to < it->cmp_it.nglyphs)
7121 {
7122 it->cmp_it.from = it->cmp_it.to;
7123 }
7124 else
7125 {
7126 it->cmp_it.id = -1;
7127 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7128 IT_BYTEPOS (*it),
7129 it->end_charpos, Qnil);
7130 }
7131 }
7132 else if (! it->cmp_it.reversed_p)
7133 {
7134 /* Composition created while scanning forward. */
7135 /* Update IT's char/byte positions to point to the first
7136 character of the next grapheme cluster, or to the
7137 character visually after the current composition. */
7138 for (i = 0; i < it->cmp_it.nchars; i++)
7139 bidi_move_to_visually_next (&it->bidi_it);
7140 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7141 IT_CHARPOS (*it) = it->bidi_it.charpos;
7142
7143 if (it->cmp_it.to < it->cmp_it.nglyphs)
7144 {
7145 /* Proceed to the next grapheme cluster. */
7146 it->cmp_it.from = it->cmp_it.to;
7147 }
7148 else
7149 {
7150 /* No more grapheme clusters in this composition.
7151 Find the next stop position. */
7152 ptrdiff_t stop = it->end_charpos;
7153 if (it->bidi_it.scan_dir < 0)
7154 /* Now we are scanning backward and don't know
7155 where to stop. */
7156 stop = -1;
7157 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7158 IT_BYTEPOS (*it), stop, Qnil);
7159 }
7160 }
7161 else
7162 {
7163 /* Composition created while scanning backward. */
7164 /* Update IT's char/byte positions to point to the last
7165 character of the previous grapheme cluster, or the
7166 character visually after the current composition. */
7167 for (i = 0; i < it->cmp_it.nchars; i++)
7168 bidi_move_to_visually_next (&it->bidi_it);
7169 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7170 IT_CHARPOS (*it) = it->bidi_it.charpos;
7171 if (it->cmp_it.from > 0)
7172 {
7173 /* Proceed to the previous grapheme cluster. */
7174 it->cmp_it.to = it->cmp_it.from;
7175 }
7176 else
7177 {
7178 /* No more grapheme clusters in this composition.
7179 Find the next stop position. */
7180 ptrdiff_t stop = it->end_charpos;
7181 if (it->bidi_it.scan_dir < 0)
7182 /* Now we are scanning backward and don't know
7183 where to stop. */
7184 stop = -1;
7185 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7186 IT_BYTEPOS (*it), stop, Qnil);
7187 }
7188 }
7189 }
7190 else
7191 {
7192 eassert (it->len != 0);
7193
7194 if (!it->bidi_p)
7195 {
7196 IT_BYTEPOS (*it) += it->len;
7197 IT_CHARPOS (*it) += 1;
7198 }
7199 else
7200 {
7201 int prev_scan_dir = it->bidi_it.scan_dir;
7202 /* If this is a new paragraph, determine its base
7203 direction (a.k.a. its base embedding level). */
7204 if (it->bidi_it.new_paragraph)
7205 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7206 bidi_move_to_visually_next (&it->bidi_it);
7207 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7208 IT_CHARPOS (*it) = it->bidi_it.charpos;
7209 if (prev_scan_dir != it->bidi_it.scan_dir)
7210 {
7211 /* As the scan direction was changed, we must
7212 re-compute the stop position for composition. */
7213 ptrdiff_t stop = it->end_charpos;
7214 if (it->bidi_it.scan_dir < 0)
7215 stop = -1;
7216 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7217 IT_BYTEPOS (*it), stop, Qnil);
7218 }
7219 }
7220 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7221 }
7222 break;
7223
7224 case GET_FROM_C_STRING:
7225 /* Current display element of IT is from a C string. */
7226 if (!it->bidi_p
7227 /* If the string position is beyond string's end, it means
7228 next_element_from_c_string is padding the string with
7229 blanks, in which case we bypass the bidi iterator,
7230 because it cannot deal with such virtual characters. */
7231 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7232 {
7233 IT_BYTEPOS (*it) += it->len;
7234 IT_CHARPOS (*it) += 1;
7235 }
7236 else
7237 {
7238 bidi_move_to_visually_next (&it->bidi_it);
7239 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7240 IT_CHARPOS (*it) = it->bidi_it.charpos;
7241 }
7242 break;
7243
7244 case GET_FROM_DISPLAY_VECTOR:
7245 /* Current display element of IT is from a display table entry.
7246 Advance in the display table definition. Reset it to null if
7247 end reached, and continue with characters from buffers/
7248 strings. */
7249 ++it->current.dpvec_index;
7250
7251 /* Restore face of the iterator to what they were before the
7252 display vector entry (these entries may contain faces). */
7253 it->face_id = it->saved_face_id;
7254
7255 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7256 {
7257 int recheck_faces = it->ellipsis_p;
7258
7259 if (it->s)
7260 it->method = GET_FROM_C_STRING;
7261 else if (STRINGP (it->string))
7262 it->method = GET_FROM_STRING;
7263 else
7264 {
7265 it->method = GET_FROM_BUFFER;
7266 it->object = it->w->contents;
7267 }
7268
7269 it->dpvec = NULL;
7270 it->current.dpvec_index = -1;
7271
7272 /* Skip over characters which were displayed via IT->dpvec. */
7273 if (it->dpvec_char_len < 0)
7274 reseat_at_next_visible_line_start (it, 1);
7275 else if (it->dpvec_char_len > 0)
7276 {
7277 if (it->method == GET_FROM_STRING
7278 && it->current.overlay_string_index >= 0
7279 && it->n_overlay_strings > 0)
7280 it->ignore_overlay_strings_at_pos_p = true;
7281 it->len = it->dpvec_char_len;
7282 set_iterator_to_next (it, reseat_p);
7283 }
7284
7285 /* Maybe recheck faces after display vector. */
7286 if (recheck_faces)
7287 it->stop_charpos = IT_CHARPOS (*it);
7288 }
7289 break;
7290
7291 case GET_FROM_STRING:
7292 /* Current display element is a character from a Lisp string. */
7293 eassert (it->s == NULL && STRINGP (it->string));
7294 /* Don't advance past string end. These conditions are true
7295 when set_iterator_to_next is called at the end of
7296 get_next_display_element, in which case the Lisp string is
7297 already exhausted, and all we want is pop the iterator
7298 stack. */
7299 if (it->current.overlay_string_index >= 0)
7300 {
7301 /* This is an overlay string, so there's no padding with
7302 spaces, and the number of characters in the string is
7303 where the string ends. */
7304 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7305 goto consider_string_end;
7306 }
7307 else
7308 {
7309 /* Not an overlay string. There could be padding, so test
7310 against it->end_charpos. */
7311 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7312 goto consider_string_end;
7313 }
7314 if (it->cmp_it.id >= 0)
7315 {
7316 int i;
7317
7318 if (! it->bidi_p)
7319 {
7320 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7321 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7322 if (it->cmp_it.to < it->cmp_it.nglyphs)
7323 it->cmp_it.from = it->cmp_it.to;
7324 else
7325 {
7326 it->cmp_it.id = -1;
7327 composition_compute_stop_pos (&it->cmp_it,
7328 IT_STRING_CHARPOS (*it),
7329 IT_STRING_BYTEPOS (*it),
7330 it->end_charpos, it->string);
7331 }
7332 }
7333 else if (! it->cmp_it.reversed_p)
7334 {
7335 for (i = 0; i < it->cmp_it.nchars; i++)
7336 bidi_move_to_visually_next (&it->bidi_it);
7337 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7338 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7339
7340 if (it->cmp_it.to < it->cmp_it.nglyphs)
7341 it->cmp_it.from = it->cmp_it.to;
7342 else
7343 {
7344 ptrdiff_t stop = it->end_charpos;
7345 if (it->bidi_it.scan_dir < 0)
7346 stop = -1;
7347 composition_compute_stop_pos (&it->cmp_it,
7348 IT_STRING_CHARPOS (*it),
7349 IT_STRING_BYTEPOS (*it), stop,
7350 it->string);
7351 }
7352 }
7353 else
7354 {
7355 for (i = 0; i < it->cmp_it.nchars; i++)
7356 bidi_move_to_visually_next (&it->bidi_it);
7357 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7358 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7359 if (it->cmp_it.from > 0)
7360 it->cmp_it.to = it->cmp_it.from;
7361 else
7362 {
7363 ptrdiff_t stop = it->end_charpos;
7364 if (it->bidi_it.scan_dir < 0)
7365 stop = -1;
7366 composition_compute_stop_pos (&it->cmp_it,
7367 IT_STRING_CHARPOS (*it),
7368 IT_STRING_BYTEPOS (*it), stop,
7369 it->string);
7370 }
7371 }
7372 }
7373 else
7374 {
7375 if (!it->bidi_p
7376 /* If the string position is beyond string's end, it
7377 means next_element_from_string is padding the string
7378 with blanks, in which case we bypass the bidi
7379 iterator, because it cannot deal with such virtual
7380 characters. */
7381 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7382 {
7383 IT_STRING_BYTEPOS (*it) += it->len;
7384 IT_STRING_CHARPOS (*it) += 1;
7385 }
7386 else
7387 {
7388 int prev_scan_dir = it->bidi_it.scan_dir;
7389
7390 bidi_move_to_visually_next (&it->bidi_it);
7391 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7392 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7393 if (prev_scan_dir != it->bidi_it.scan_dir)
7394 {
7395 ptrdiff_t stop = it->end_charpos;
7396
7397 if (it->bidi_it.scan_dir < 0)
7398 stop = -1;
7399 composition_compute_stop_pos (&it->cmp_it,
7400 IT_STRING_CHARPOS (*it),
7401 IT_STRING_BYTEPOS (*it), stop,
7402 it->string);
7403 }
7404 }
7405 }
7406
7407 consider_string_end:
7408
7409 if (it->current.overlay_string_index >= 0)
7410 {
7411 /* IT->string is an overlay string. Advance to the
7412 next, if there is one. */
7413 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7414 {
7415 it->ellipsis_p = 0;
7416 next_overlay_string (it);
7417 if (it->ellipsis_p)
7418 setup_for_ellipsis (it, 0);
7419 }
7420 }
7421 else
7422 {
7423 /* IT->string is not an overlay string. If we reached
7424 its end, and there is something on IT->stack, proceed
7425 with what is on the stack. This can be either another
7426 string, this time an overlay string, or a buffer. */
7427 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7428 && it->sp > 0)
7429 {
7430 pop_it (it);
7431 if (it->method == GET_FROM_STRING)
7432 goto consider_string_end;
7433 }
7434 }
7435 break;
7436
7437 case GET_FROM_IMAGE:
7438 case GET_FROM_STRETCH:
7439 /* The position etc with which we have to proceed are on
7440 the stack. The position may be at the end of a string,
7441 if the `display' property takes up the whole string. */
7442 eassert (it->sp > 0);
7443 pop_it (it);
7444 if (it->method == GET_FROM_STRING)
7445 goto consider_string_end;
7446 break;
7447
7448 default:
7449 /* There are no other methods defined, so this should be a bug. */
7450 emacs_abort ();
7451 }
7452
7453 eassert (it->method != GET_FROM_STRING
7454 || (STRINGP (it->string)
7455 && IT_STRING_CHARPOS (*it) >= 0));
7456 }
7457
7458 /* Load IT's display element fields with information about the next
7459 display element which comes from a display table entry or from the
7460 result of translating a control character to one of the forms `^C'
7461 or `\003'.
7462
7463 IT->dpvec holds the glyphs to return as characters.
7464 IT->saved_face_id holds the face id before the display vector--it
7465 is restored into IT->face_id in set_iterator_to_next. */
7466
7467 static int
7468 next_element_from_display_vector (struct it *it)
7469 {
7470 Lisp_Object gc;
7471 int prev_face_id = it->face_id;
7472 int next_face_id;
7473
7474 /* Precondition. */
7475 eassert (it->dpvec && it->current.dpvec_index >= 0);
7476
7477 it->face_id = it->saved_face_id;
7478
7479 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7480 That seemed totally bogus - so I changed it... */
7481 gc = it->dpvec[it->current.dpvec_index];
7482
7483 if (GLYPH_CODE_P (gc))
7484 {
7485 struct face *this_face, *prev_face, *next_face;
7486
7487 it->c = GLYPH_CODE_CHAR (gc);
7488 it->len = CHAR_BYTES (it->c);
7489
7490 /* The entry may contain a face id to use. Such a face id is
7491 the id of a Lisp face, not a realized face. A face id of
7492 zero means no face is specified. */
7493 if (it->dpvec_face_id >= 0)
7494 it->face_id = it->dpvec_face_id;
7495 else
7496 {
7497 int lface_id = GLYPH_CODE_FACE (gc);
7498 if (lface_id > 0)
7499 it->face_id = merge_faces (it->f, Qt, lface_id,
7500 it->saved_face_id);
7501 }
7502
7503 /* Glyphs in the display vector could have the box face, so we
7504 need to set the related flags in the iterator, as
7505 appropriate. */
7506 this_face = FACE_FROM_ID (it->f, it->face_id);
7507 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7508
7509 /* Is this character the first character of a box-face run? */
7510 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7511 && (!prev_face
7512 || prev_face->box == FACE_NO_BOX));
7513
7514 /* For the last character of the box-face run, we need to look
7515 either at the next glyph from the display vector, or at the
7516 face we saw before the display vector. */
7517 next_face_id = it->saved_face_id;
7518 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7519 {
7520 if (it->dpvec_face_id >= 0)
7521 next_face_id = it->dpvec_face_id;
7522 else
7523 {
7524 int lface_id =
7525 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7526
7527 if (lface_id > 0)
7528 next_face_id = merge_faces (it->f, Qt, lface_id,
7529 it->saved_face_id);
7530 }
7531 }
7532 next_face = FACE_FROM_ID (it->f, next_face_id);
7533 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7534 && (!next_face
7535 || next_face->box == FACE_NO_BOX));
7536 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7537 }
7538 else
7539 /* Display table entry is invalid. Return a space. */
7540 it->c = ' ', it->len = 1;
7541
7542 /* Don't change position and object of the iterator here. They are
7543 still the values of the character that had this display table
7544 entry or was translated, and that's what we want. */
7545 it->what = IT_CHARACTER;
7546 return 1;
7547 }
7548
7549 /* Get the first element of string/buffer in the visual order, after
7550 being reseated to a new position in a string or a buffer. */
7551 static void
7552 get_visually_first_element (struct it *it)
7553 {
7554 int string_p = STRINGP (it->string) || it->s;
7555 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7556 ptrdiff_t bob = (string_p ? 0 : BEGV);
7557
7558 if (STRINGP (it->string))
7559 {
7560 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7561 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7562 }
7563 else
7564 {
7565 it->bidi_it.charpos = IT_CHARPOS (*it);
7566 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7567 }
7568
7569 if (it->bidi_it.charpos == eob)
7570 {
7571 /* Nothing to do, but reset the FIRST_ELT flag, like
7572 bidi_paragraph_init does, because we are not going to
7573 call it. */
7574 it->bidi_it.first_elt = 0;
7575 }
7576 else if (it->bidi_it.charpos == bob
7577 || (!string_p
7578 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7579 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7580 {
7581 /* If we are at the beginning of a line/string, we can produce
7582 the next element right away. */
7583 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7584 bidi_move_to_visually_next (&it->bidi_it);
7585 }
7586 else
7587 {
7588 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7589
7590 /* We need to prime the bidi iterator starting at the line's or
7591 string's beginning, before we will be able to produce the
7592 next element. */
7593 if (string_p)
7594 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7595 else
7596 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7597 IT_BYTEPOS (*it), -1,
7598 &it->bidi_it.bytepos);
7599 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7600 do
7601 {
7602 /* Now return to buffer/string position where we were asked
7603 to get the next display element, and produce that. */
7604 bidi_move_to_visually_next (&it->bidi_it);
7605 }
7606 while (it->bidi_it.bytepos != orig_bytepos
7607 && it->bidi_it.charpos < eob);
7608 }
7609
7610 /* Adjust IT's position information to where we ended up. */
7611 if (STRINGP (it->string))
7612 {
7613 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7614 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7615 }
7616 else
7617 {
7618 IT_CHARPOS (*it) = it->bidi_it.charpos;
7619 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7620 }
7621
7622 if (STRINGP (it->string) || !it->s)
7623 {
7624 ptrdiff_t stop, charpos, bytepos;
7625
7626 if (STRINGP (it->string))
7627 {
7628 eassert (!it->s);
7629 stop = SCHARS (it->string);
7630 if (stop > it->end_charpos)
7631 stop = it->end_charpos;
7632 charpos = IT_STRING_CHARPOS (*it);
7633 bytepos = IT_STRING_BYTEPOS (*it);
7634 }
7635 else
7636 {
7637 stop = it->end_charpos;
7638 charpos = IT_CHARPOS (*it);
7639 bytepos = IT_BYTEPOS (*it);
7640 }
7641 if (it->bidi_it.scan_dir < 0)
7642 stop = -1;
7643 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7644 it->string);
7645 }
7646 }
7647
7648 /* Load IT with the next display element from Lisp string IT->string.
7649 IT->current.string_pos is the current position within the string.
7650 If IT->current.overlay_string_index >= 0, the Lisp string is an
7651 overlay string. */
7652
7653 static int
7654 next_element_from_string (struct it *it)
7655 {
7656 struct text_pos position;
7657
7658 eassert (STRINGP (it->string));
7659 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7660 eassert (IT_STRING_CHARPOS (*it) >= 0);
7661 position = it->current.string_pos;
7662
7663 /* With bidi reordering, the character to display might not be the
7664 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7665 that we were reseat()ed to a new string, whose paragraph
7666 direction is not known. */
7667 if (it->bidi_p && it->bidi_it.first_elt)
7668 {
7669 get_visually_first_element (it);
7670 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7671 }
7672
7673 /* Time to check for invisible text? */
7674 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7675 {
7676 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7677 {
7678 if (!(!it->bidi_p
7679 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7680 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7681 {
7682 /* With bidi non-linear iteration, we could find
7683 ourselves far beyond the last computed stop_charpos,
7684 with several other stop positions in between that we
7685 missed. Scan them all now, in buffer's logical
7686 order, until we find and handle the last stop_charpos
7687 that precedes our current position. */
7688 handle_stop_backwards (it, it->stop_charpos);
7689 return GET_NEXT_DISPLAY_ELEMENT (it);
7690 }
7691 else
7692 {
7693 if (it->bidi_p)
7694 {
7695 /* Take note of the stop position we just moved
7696 across, for when we will move back across it. */
7697 it->prev_stop = it->stop_charpos;
7698 /* If we are at base paragraph embedding level, take
7699 note of the last stop position seen at this
7700 level. */
7701 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7702 it->base_level_stop = it->stop_charpos;
7703 }
7704 handle_stop (it);
7705
7706 /* Since a handler may have changed IT->method, we must
7707 recurse here. */
7708 return GET_NEXT_DISPLAY_ELEMENT (it);
7709 }
7710 }
7711 else if (it->bidi_p
7712 /* If we are before prev_stop, we may have overstepped
7713 on our way backwards a stop_pos, and if so, we need
7714 to handle that stop_pos. */
7715 && IT_STRING_CHARPOS (*it) < it->prev_stop
7716 /* We can sometimes back up for reasons that have nothing
7717 to do with bidi reordering. E.g., compositions. The
7718 code below is only needed when we are above the base
7719 embedding level, so test for that explicitly. */
7720 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7721 {
7722 /* If we lost track of base_level_stop, we have no better
7723 place for handle_stop_backwards to start from than string
7724 beginning. This happens, e.g., when we were reseated to
7725 the previous screenful of text by vertical-motion. */
7726 if (it->base_level_stop <= 0
7727 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7728 it->base_level_stop = 0;
7729 handle_stop_backwards (it, it->base_level_stop);
7730 return GET_NEXT_DISPLAY_ELEMENT (it);
7731 }
7732 }
7733
7734 if (it->current.overlay_string_index >= 0)
7735 {
7736 /* Get the next character from an overlay string. In overlay
7737 strings, there is no field width or padding with spaces to
7738 do. */
7739 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7740 {
7741 it->what = IT_EOB;
7742 return 0;
7743 }
7744 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7745 IT_STRING_BYTEPOS (*it),
7746 it->bidi_it.scan_dir < 0
7747 ? -1
7748 : SCHARS (it->string))
7749 && next_element_from_composition (it))
7750 {
7751 return 1;
7752 }
7753 else if (STRING_MULTIBYTE (it->string))
7754 {
7755 const unsigned char *s = (SDATA (it->string)
7756 + IT_STRING_BYTEPOS (*it));
7757 it->c = string_char_and_length (s, &it->len);
7758 }
7759 else
7760 {
7761 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7762 it->len = 1;
7763 }
7764 }
7765 else
7766 {
7767 /* Get the next character from a Lisp string that is not an
7768 overlay string. Such strings come from the mode line, for
7769 example. We may have to pad with spaces, or truncate the
7770 string. See also next_element_from_c_string. */
7771 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7772 {
7773 it->what = IT_EOB;
7774 return 0;
7775 }
7776 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7777 {
7778 /* Pad with spaces. */
7779 it->c = ' ', it->len = 1;
7780 CHARPOS (position) = BYTEPOS (position) = -1;
7781 }
7782 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7783 IT_STRING_BYTEPOS (*it),
7784 it->bidi_it.scan_dir < 0
7785 ? -1
7786 : it->string_nchars)
7787 && next_element_from_composition (it))
7788 {
7789 return 1;
7790 }
7791 else if (STRING_MULTIBYTE (it->string))
7792 {
7793 const unsigned char *s = (SDATA (it->string)
7794 + IT_STRING_BYTEPOS (*it));
7795 it->c = string_char_and_length (s, &it->len);
7796 }
7797 else
7798 {
7799 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7800 it->len = 1;
7801 }
7802 }
7803
7804 /* Record what we have and where it came from. */
7805 it->what = IT_CHARACTER;
7806 it->object = it->string;
7807 it->position = position;
7808 return 1;
7809 }
7810
7811
7812 /* Load IT with next display element from C string IT->s.
7813 IT->string_nchars is the maximum number of characters to return
7814 from the string. IT->end_charpos may be greater than
7815 IT->string_nchars when this function is called, in which case we
7816 may have to return padding spaces. Value is zero if end of string
7817 reached, including padding spaces. */
7818
7819 static int
7820 next_element_from_c_string (struct it *it)
7821 {
7822 bool success_p = true;
7823
7824 eassert (it->s);
7825 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7826 it->what = IT_CHARACTER;
7827 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7828 it->object = Qnil;
7829
7830 /* With bidi reordering, the character to display might not be the
7831 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7832 we were reseated to a new string, whose paragraph direction is
7833 not known. */
7834 if (it->bidi_p && it->bidi_it.first_elt)
7835 get_visually_first_element (it);
7836
7837 /* IT's position can be greater than IT->string_nchars in case a
7838 field width or precision has been specified when the iterator was
7839 initialized. */
7840 if (IT_CHARPOS (*it) >= it->end_charpos)
7841 {
7842 /* End of the game. */
7843 it->what = IT_EOB;
7844 success_p = 0;
7845 }
7846 else if (IT_CHARPOS (*it) >= it->string_nchars)
7847 {
7848 /* Pad with spaces. */
7849 it->c = ' ', it->len = 1;
7850 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7851 }
7852 else if (it->multibyte_p)
7853 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7854 else
7855 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7856
7857 return success_p;
7858 }
7859
7860
7861 /* Set up IT to return characters from an ellipsis, if appropriate.
7862 The definition of the ellipsis glyphs may come from a display table
7863 entry. This function fills IT with the first glyph from the
7864 ellipsis if an ellipsis is to be displayed. */
7865
7866 static int
7867 next_element_from_ellipsis (struct it *it)
7868 {
7869 if (it->selective_display_ellipsis_p)
7870 setup_for_ellipsis (it, it->len);
7871 else
7872 {
7873 /* The face at the current position may be different from the
7874 face we find after the invisible text. Remember what it
7875 was in IT->saved_face_id, and signal that it's there by
7876 setting face_before_selective_p. */
7877 it->saved_face_id = it->face_id;
7878 it->method = GET_FROM_BUFFER;
7879 it->object = it->w->contents;
7880 reseat_at_next_visible_line_start (it, 1);
7881 it->face_before_selective_p = true;
7882 }
7883
7884 return GET_NEXT_DISPLAY_ELEMENT (it);
7885 }
7886
7887
7888 /* Deliver an image display element. The iterator IT is already
7889 filled with image information (done in handle_display_prop). Value
7890 is always 1. */
7891
7892
7893 static int
7894 next_element_from_image (struct it *it)
7895 {
7896 it->what = IT_IMAGE;
7897 it->ignore_overlay_strings_at_pos_p = 0;
7898 return 1;
7899 }
7900
7901
7902 /* Fill iterator IT with next display element from a stretch glyph
7903 property. IT->object is the value of the text property. Value is
7904 always 1. */
7905
7906 static int
7907 next_element_from_stretch (struct it *it)
7908 {
7909 it->what = IT_STRETCH;
7910 return 1;
7911 }
7912
7913 /* Scan backwards from IT's current position until we find a stop
7914 position, or until BEGV. This is called when we find ourself
7915 before both the last known prev_stop and base_level_stop while
7916 reordering bidirectional text. */
7917
7918 static void
7919 compute_stop_pos_backwards (struct it *it)
7920 {
7921 const int SCAN_BACK_LIMIT = 1000;
7922 struct text_pos pos;
7923 struct display_pos save_current = it->current;
7924 struct text_pos save_position = it->position;
7925 ptrdiff_t charpos = IT_CHARPOS (*it);
7926 ptrdiff_t where_we_are = charpos;
7927 ptrdiff_t save_stop_pos = it->stop_charpos;
7928 ptrdiff_t save_end_pos = it->end_charpos;
7929
7930 eassert (NILP (it->string) && !it->s);
7931 eassert (it->bidi_p);
7932 it->bidi_p = 0;
7933 do
7934 {
7935 it->end_charpos = min (charpos + 1, ZV);
7936 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7937 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7938 reseat_1 (it, pos, 0);
7939 compute_stop_pos (it);
7940 /* We must advance forward, right? */
7941 if (it->stop_charpos <= charpos)
7942 emacs_abort ();
7943 }
7944 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7945
7946 if (it->stop_charpos <= where_we_are)
7947 it->prev_stop = it->stop_charpos;
7948 else
7949 it->prev_stop = BEGV;
7950 it->bidi_p = true;
7951 it->current = save_current;
7952 it->position = save_position;
7953 it->stop_charpos = save_stop_pos;
7954 it->end_charpos = save_end_pos;
7955 }
7956
7957 /* Scan forward from CHARPOS in the current buffer/string, until we
7958 find a stop position > current IT's position. Then handle the stop
7959 position before that. This is called when we bump into a stop
7960 position while reordering bidirectional text. CHARPOS should be
7961 the last previously processed stop_pos (or BEGV/0, if none were
7962 processed yet) whose position is less that IT's current
7963 position. */
7964
7965 static void
7966 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7967 {
7968 int bufp = !STRINGP (it->string);
7969 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7970 struct display_pos save_current = it->current;
7971 struct text_pos save_position = it->position;
7972 struct text_pos pos1;
7973 ptrdiff_t next_stop;
7974
7975 /* Scan in strict logical order. */
7976 eassert (it->bidi_p);
7977 it->bidi_p = 0;
7978 do
7979 {
7980 it->prev_stop = charpos;
7981 if (bufp)
7982 {
7983 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7984 reseat_1 (it, pos1, 0);
7985 }
7986 else
7987 it->current.string_pos = string_pos (charpos, it->string);
7988 compute_stop_pos (it);
7989 /* We must advance forward, right? */
7990 if (it->stop_charpos <= it->prev_stop)
7991 emacs_abort ();
7992 charpos = it->stop_charpos;
7993 }
7994 while (charpos <= where_we_are);
7995
7996 it->bidi_p = true;
7997 it->current = save_current;
7998 it->position = save_position;
7999 next_stop = it->stop_charpos;
8000 it->stop_charpos = it->prev_stop;
8001 handle_stop (it);
8002 it->stop_charpos = next_stop;
8003 }
8004
8005 /* Load IT with the next display element from current_buffer. Value
8006 is zero if end of buffer reached. IT->stop_charpos is the next
8007 position at which to stop and check for text properties or buffer
8008 end. */
8009
8010 static int
8011 next_element_from_buffer (struct it *it)
8012 {
8013 bool success_p = true;
8014
8015 eassert (IT_CHARPOS (*it) >= BEGV);
8016 eassert (NILP (it->string) && !it->s);
8017 eassert (!it->bidi_p
8018 || (EQ (it->bidi_it.string.lstring, Qnil)
8019 && it->bidi_it.string.s == NULL));
8020
8021 /* With bidi reordering, the character to display might not be the
8022 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8023 we were reseat()ed to a new buffer position, which is potentially
8024 a different paragraph. */
8025 if (it->bidi_p && it->bidi_it.first_elt)
8026 {
8027 get_visually_first_element (it);
8028 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8029 }
8030
8031 if (IT_CHARPOS (*it) >= it->stop_charpos)
8032 {
8033 if (IT_CHARPOS (*it) >= it->end_charpos)
8034 {
8035 int overlay_strings_follow_p;
8036
8037 /* End of the game, except when overlay strings follow that
8038 haven't been returned yet. */
8039 if (it->overlay_strings_at_end_processed_p)
8040 overlay_strings_follow_p = 0;
8041 else
8042 {
8043 it->overlay_strings_at_end_processed_p = true;
8044 overlay_strings_follow_p = get_overlay_strings (it, 0);
8045 }
8046
8047 if (overlay_strings_follow_p)
8048 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8049 else
8050 {
8051 it->what = IT_EOB;
8052 it->position = it->current.pos;
8053 success_p = 0;
8054 }
8055 }
8056 else if (!(!it->bidi_p
8057 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8058 || IT_CHARPOS (*it) == it->stop_charpos))
8059 {
8060 /* With bidi non-linear iteration, we could find ourselves
8061 far beyond the last computed stop_charpos, with several
8062 other stop positions in between that we missed. Scan
8063 them all now, in buffer's logical order, until we find
8064 and handle the last stop_charpos that precedes our
8065 current position. */
8066 handle_stop_backwards (it, it->stop_charpos);
8067 return GET_NEXT_DISPLAY_ELEMENT (it);
8068 }
8069 else
8070 {
8071 if (it->bidi_p)
8072 {
8073 /* Take note of the stop position we just moved across,
8074 for when we will move back across it. */
8075 it->prev_stop = it->stop_charpos;
8076 /* If we are at base paragraph embedding level, take
8077 note of the last stop position seen at this
8078 level. */
8079 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8080 it->base_level_stop = it->stop_charpos;
8081 }
8082 handle_stop (it);
8083 return GET_NEXT_DISPLAY_ELEMENT (it);
8084 }
8085 }
8086 else if (it->bidi_p
8087 /* If we are before prev_stop, we may have overstepped on
8088 our way backwards a stop_pos, and if so, we need to
8089 handle that stop_pos. */
8090 && IT_CHARPOS (*it) < it->prev_stop
8091 /* We can sometimes back up for reasons that have nothing
8092 to do with bidi reordering. E.g., compositions. The
8093 code below is only needed when we are above the base
8094 embedding level, so test for that explicitly. */
8095 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8096 {
8097 if (it->base_level_stop <= 0
8098 || IT_CHARPOS (*it) < it->base_level_stop)
8099 {
8100 /* If we lost track of base_level_stop, we need to find
8101 prev_stop by looking backwards. This happens, e.g., when
8102 we were reseated to the previous screenful of text by
8103 vertical-motion. */
8104 it->base_level_stop = BEGV;
8105 compute_stop_pos_backwards (it);
8106 handle_stop_backwards (it, it->prev_stop);
8107 }
8108 else
8109 handle_stop_backwards (it, it->base_level_stop);
8110 return GET_NEXT_DISPLAY_ELEMENT (it);
8111 }
8112 else
8113 {
8114 /* No face changes, overlays etc. in sight, so just return a
8115 character from current_buffer. */
8116 unsigned char *p;
8117 ptrdiff_t stop;
8118
8119 /* Maybe run the redisplay end trigger hook. Performance note:
8120 This doesn't seem to cost measurable time. */
8121 if (it->redisplay_end_trigger_charpos
8122 && it->glyph_row
8123 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8124 run_redisplay_end_trigger_hook (it);
8125
8126 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8127 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8128 stop)
8129 && next_element_from_composition (it))
8130 {
8131 return 1;
8132 }
8133
8134 /* Get the next character, maybe multibyte. */
8135 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8136 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8137 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8138 else
8139 it->c = *p, it->len = 1;
8140
8141 /* Record what we have and where it came from. */
8142 it->what = IT_CHARACTER;
8143 it->object = it->w->contents;
8144 it->position = it->current.pos;
8145
8146 /* Normally we return the character found above, except when we
8147 really want to return an ellipsis for selective display. */
8148 if (it->selective)
8149 {
8150 if (it->c == '\n')
8151 {
8152 /* A value of selective > 0 means hide lines indented more
8153 than that number of columns. */
8154 if (it->selective > 0
8155 && IT_CHARPOS (*it) + 1 < ZV
8156 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8157 IT_BYTEPOS (*it) + 1,
8158 it->selective))
8159 {
8160 success_p = next_element_from_ellipsis (it);
8161 it->dpvec_char_len = -1;
8162 }
8163 }
8164 else if (it->c == '\r' && it->selective == -1)
8165 {
8166 /* A value of selective == -1 means that everything from the
8167 CR to the end of the line is invisible, with maybe an
8168 ellipsis displayed for it. */
8169 success_p = next_element_from_ellipsis (it);
8170 it->dpvec_char_len = -1;
8171 }
8172 }
8173 }
8174
8175 /* Value is zero if end of buffer reached. */
8176 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8177 return success_p;
8178 }
8179
8180
8181 /* Run the redisplay end trigger hook for IT. */
8182
8183 static void
8184 run_redisplay_end_trigger_hook (struct it *it)
8185 {
8186 Lisp_Object args[3];
8187
8188 /* IT->glyph_row should be non-null, i.e. we should be actually
8189 displaying something, or otherwise we should not run the hook. */
8190 eassert (it->glyph_row);
8191
8192 /* Set up hook arguments. */
8193 args[0] = Qredisplay_end_trigger_functions;
8194 args[1] = it->window;
8195 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8196 it->redisplay_end_trigger_charpos = 0;
8197
8198 /* Since we are *trying* to run these functions, don't try to run
8199 them again, even if they get an error. */
8200 wset_redisplay_end_trigger (it->w, Qnil);
8201 Frun_hook_with_args (3, args);
8202
8203 /* Notice if it changed the face of the character we are on. */
8204 handle_face_prop (it);
8205 }
8206
8207
8208 /* Deliver a composition display element. Unlike the other
8209 next_element_from_XXX, this function is not registered in the array
8210 get_next_element[]. It is called from next_element_from_buffer and
8211 next_element_from_string when necessary. */
8212
8213 static int
8214 next_element_from_composition (struct it *it)
8215 {
8216 it->what = IT_COMPOSITION;
8217 it->len = it->cmp_it.nbytes;
8218 if (STRINGP (it->string))
8219 {
8220 if (it->c < 0)
8221 {
8222 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8223 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8224 return 0;
8225 }
8226 it->position = it->current.string_pos;
8227 it->object = it->string;
8228 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8229 IT_STRING_BYTEPOS (*it), it->string);
8230 }
8231 else
8232 {
8233 if (it->c < 0)
8234 {
8235 IT_CHARPOS (*it) += it->cmp_it.nchars;
8236 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8237 if (it->bidi_p)
8238 {
8239 if (it->bidi_it.new_paragraph)
8240 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8241 /* Resync the bidi iterator with IT's new position.
8242 FIXME: this doesn't support bidirectional text. */
8243 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8244 bidi_move_to_visually_next (&it->bidi_it);
8245 }
8246 return 0;
8247 }
8248 it->position = it->current.pos;
8249 it->object = it->w->contents;
8250 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8251 IT_BYTEPOS (*it), Qnil);
8252 }
8253 return 1;
8254 }
8255
8256
8257 \f
8258 /***********************************************************************
8259 Moving an iterator without producing glyphs
8260 ***********************************************************************/
8261
8262 /* Check if iterator is at a position corresponding to a valid buffer
8263 position after some move_it_ call. */
8264
8265 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8266 ((it)->method == GET_FROM_STRING \
8267 ? IT_STRING_CHARPOS (*it) == 0 \
8268 : 1)
8269
8270
8271 /* Move iterator IT to a specified buffer or X position within one
8272 line on the display without producing glyphs.
8273
8274 OP should be a bit mask including some or all of these bits:
8275 MOVE_TO_X: Stop upon reaching x-position TO_X.
8276 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8277 Regardless of OP's value, stop upon reaching the end of the display line.
8278
8279 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8280 This means, in particular, that TO_X includes window's horizontal
8281 scroll amount.
8282
8283 The return value has several possible values that
8284 say what condition caused the scan to stop:
8285
8286 MOVE_POS_MATCH_OR_ZV
8287 - when TO_POS or ZV was reached.
8288
8289 MOVE_X_REACHED
8290 -when TO_X was reached before TO_POS or ZV were reached.
8291
8292 MOVE_LINE_CONTINUED
8293 - when we reached the end of the display area and the line must
8294 be continued.
8295
8296 MOVE_LINE_TRUNCATED
8297 - when we reached the end of the display area and the line is
8298 truncated.
8299
8300 MOVE_NEWLINE_OR_CR
8301 - when we stopped at a line end, i.e. a newline or a CR and selective
8302 display is on. */
8303
8304 static enum move_it_result
8305 move_it_in_display_line_to (struct it *it,
8306 ptrdiff_t to_charpos, int to_x,
8307 enum move_operation_enum op)
8308 {
8309 enum move_it_result result = MOVE_UNDEFINED;
8310 struct glyph_row *saved_glyph_row;
8311 struct it wrap_it, atpos_it, atx_it, ppos_it;
8312 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8313 void *ppos_data = NULL;
8314 int may_wrap = 0;
8315 enum it_method prev_method = it->method;
8316 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8317 int saw_smaller_pos = prev_pos < to_charpos;
8318
8319 /* Don't produce glyphs in produce_glyphs. */
8320 saved_glyph_row = it->glyph_row;
8321 it->glyph_row = NULL;
8322
8323 /* Use wrap_it to save a copy of IT wherever a word wrap could
8324 occur. Use atpos_it to save a copy of IT at the desired buffer
8325 position, if found, so that we can scan ahead and check if the
8326 word later overshoots the window edge. Use atx_it similarly, for
8327 pixel positions. */
8328 wrap_it.sp = -1;
8329 atpos_it.sp = -1;
8330 atx_it.sp = -1;
8331
8332 /* Use ppos_it under bidi reordering to save a copy of IT for the
8333 position > CHARPOS that is the closest to CHARPOS. We restore
8334 that position in IT when we have scanned the entire display line
8335 without finding a match for CHARPOS and all the character
8336 positions are greater than CHARPOS. */
8337 if (it->bidi_p)
8338 {
8339 SAVE_IT (ppos_it, *it, ppos_data);
8340 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8341 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8342 SAVE_IT (ppos_it, *it, ppos_data);
8343 }
8344
8345 #define BUFFER_POS_REACHED_P() \
8346 ((op & MOVE_TO_POS) != 0 \
8347 && BUFFERP (it->object) \
8348 && (IT_CHARPOS (*it) == to_charpos \
8349 || ((!it->bidi_p \
8350 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8351 && IT_CHARPOS (*it) > to_charpos) \
8352 || (it->what == IT_COMPOSITION \
8353 && ((IT_CHARPOS (*it) > to_charpos \
8354 && to_charpos >= it->cmp_it.charpos) \
8355 || (IT_CHARPOS (*it) < to_charpos \
8356 && to_charpos <= it->cmp_it.charpos)))) \
8357 && (it->method == GET_FROM_BUFFER \
8358 || (it->method == GET_FROM_DISPLAY_VECTOR \
8359 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8360
8361 /* If there's a line-/wrap-prefix, handle it. */
8362 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8363 && it->current_y < it->last_visible_y)
8364 handle_line_prefix (it);
8365
8366 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8367 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8368
8369 while (1)
8370 {
8371 int x, i, ascent = 0, descent = 0;
8372
8373 /* Utility macro to reset an iterator with x, ascent, and descent. */
8374 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8375 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8376 (IT)->max_descent = descent)
8377
8378 /* Stop if we move beyond TO_CHARPOS (after an image or a
8379 display string or stretch glyph). */
8380 if ((op & MOVE_TO_POS) != 0
8381 && BUFFERP (it->object)
8382 && it->method == GET_FROM_BUFFER
8383 && (((!it->bidi_p
8384 /* When the iterator is at base embedding level, we
8385 are guaranteed that characters are delivered for
8386 display in strictly increasing order of their
8387 buffer positions. */
8388 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8389 && IT_CHARPOS (*it) > to_charpos)
8390 || (it->bidi_p
8391 && (prev_method == GET_FROM_IMAGE
8392 || prev_method == GET_FROM_STRETCH
8393 || prev_method == GET_FROM_STRING)
8394 /* Passed TO_CHARPOS from left to right. */
8395 && ((prev_pos < to_charpos
8396 && IT_CHARPOS (*it) > to_charpos)
8397 /* Passed TO_CHARPOS from right to left. */
8398 || (prev_pos > to_charpos
8399 && IT_CHARPOS (*it) < to_charpos)))))
8400 {
8401 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8402 {
8403 result = MOVE_POS_MATCH_OR_ZV;
8404 break;
8405 }
8406 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8407 /* If wrap_it is valid, the current position might be in a
8408 word that is wrapped. So, save the iterator in
8409 atpos_it and continue to see if wrapping happens. */
8410 SAVE_IT (atpos_it, *it, atpos_data);
8411 }
8412
8413 /* Stop when ZV reached.
8414 We used to stop here when TO_CHARPOS reached as well, but that is
8415 too soon if this glyph does not fit on this line. So we handle it
8416 explicitly below. */
8417 if (!get_next_display_element (it))
8418 {
8419 result = MOVE_POS_MATCH_OR_ZV;
8420 break;
8421 }
8422
8423 if (it->line_wrap == TRUNCATE)
8424 {
8425 if (BUFFER_POS_REACHED_P ())
8426 {
8427 result = MOVE_POS_MATCH_OR_ZV;
8428 break;
8429 }
8430 }
8431 else
8432 {
8433 if (it->line_wrap == WORD_WRAP)
8434 {
8435 if (IT_DISPLAYING_WHITESPACE (it))
8436 may_wrap = 1;
8437 else if (may_wrap)
8438 {
8439 /* We have reached a glyph that follows one or more
8440 whitespace characters. If the position is
8441 already found, we are done. */
8442 if (atpos_it.sp >= 0)
8443 {
8444 RESTORE_IT (it, &atpos_it, atpos_data);
8445 result = MOVE_POS_MATCH_OR_ZV;
8446 goto done;
8447 }
8448 if (atx_it.sp >= 0)
8449 {
8450 RESTORE_IT (it, &atx_it, atx_data);
8451 result = MOVE_X_REACHED;
8452 goto done;
8453 }
8454 /* Otherwise, we can wrap here. */
8455 SAVE_IT (wrap_it, *it, wrap_data);
8456 may_wrap = 0;
8457 }
8458 }
8459 }
8460
8461 /* Remember the line height for the current line, in case
8462 the next element doesn't fit on the line. */
8463 ascent = it->max_ascent;
8464 descent = it->max_descent;
8465
8466 /* The call to produce_glyphs will get the metrics of the
8467 display element IT is loaded with. Record the x-position
8468 before this display element, in case it doesn't fit on the
8469 line. */
8470 x = it->current_x;
8471
8472 PRODUCE_GLYPHS (it);
8473
8474 if (it->area != TEXT_AREA)
8475 {
8476 prev_method = it->method;
8477 if (it->method == GET_FROM_BUFFER)
8478 prev_pos = IT_CHARPOS (*it);
8479 set_iterator_to_next (it, 1);
8480 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8481 SET_TEXT_POS (this_line_min_pos,
8482 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8483 if (it->bidi_p
8484 && (op & MOVE_TO_POS)
8485 && IT_CHARPOS (*it) > to_charpos
8486 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8487 SAVE_IT (ppos_it, *it, ppos_data);
8488 continue;
8489 }
8490
8491 /* The number of glyphs we get back in IT->nglyphs will normally
8492 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8493 character on a terminal frame, or (iii) a line end. For the
8494 second case, IT->nglyphs - 1 padding glyphs will be present.
8495 (On X frames, there is only one glyph produced for a
8496 composite character.)
8497
8498 The behavior implemented below means, for continuation lines,
8499 that as many spaces of a TAB as fit on the current line are
8500 displayed there. For terminal frames, as many glyphs of a
8501 multi-glyph character are displayed in the current line, too.
8502 This is what the old redisplay code did, and we keep it that
8503 way. Under X, the whole shape of a complex character must
8504 fit on the line or it will be completely displayed in the
8505 next line.
8506
8507 Note that both for tabs and padding glyphs, all glyphs have
8508 the same width. */
8509 if (it->nglyphs)
8510 {
8511 /* More than one glyph or glyph doesn't fit on line. All
8512 glyphs have the same width. */
8513 int single_glyph_width = it->pixel_width / it->nglyphs;
8514 int new_x;
8515 int x_before_this_char = x;
8516 int hpos_before_this_char = it->hpos;
8517
8518 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8519 {
8520 new_x = x + single_glyph_width;
8521
8522 /* We want to leave anything reaching TO_X to the caller. */
8523 if ((op & MOVE_TO_X) && new_x > to_x)
8524 {
8525 if (BUFFER_POS_REACHED_P ())
8526 {
8527 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8528 goto buffer_pos_reached;
8529 if (atpos_it.sp < 0)
8530 {
8531 SAVE_IT (atpos_it, *it, atpos_data);
8532 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8533 }
8534 }
8535 else
8536 {
8537 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8538 {
8539 it->current_x = x;
8540 result = MOVE_X_REACHED;
8541 break;
8542 }
8543 if (atx_it.sp < 0)
8544 {
8545 SAVE_IT (atx_it, *it, atx_data);
8546 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8547 }
8548 }
8549 }
8550
8551 if (/* Lines are continued. */
8552 it->line_wrap != TRUNCATE
8553 && (/* And glyph doesn't fit on the line. */
8554 new_x > it->last_visible_x
8555 /* Or it fits exactly and we're on a window
8556 system frame. */
8557 || (new_x == it->last_visible_x
8558 && FRAME_WINDOW_P (it->f)
8559 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8560 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8561 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8562 {
8563 if (/* IT->hpos == 0 means the very first glyph
8564 doesn't fit on the line, e.g. a wide image. */
8565 it->hpos == 0
8566 || (new_x == it->last_visible_x
8567 && FRAME_WINDOW_P (it->f)))
8568 {
8569 ++it->hpos;
8570 it->current_x = new_x;
8571
8572 /* The character's last glyph just barely fits
8573 in this row. */
8574 if (i == it->nglyphs - 1)
8575 {
8576 /* If this is the destination position,
8577 return a position *before* it in this row,
8578 now that we know it fits in this row. */
8579 if (BUFFER_POS_REACHED_P ())
8580 {
8581 if (it->line_wrap != WORD_WRAP
8582 || wrap_it.sp < 0)
8583 {
8584 it->hpos = hpos_before_this_char;
8585 it->current_x = x_before_this_char;
8586 result = MOVE_POS_MATCH_OR_ZV;
8587 break;
8588 }
8589 if (it->line_wrap == WORD_WRAP
8590 && atpos_it.sp < 0)
8591 {
8592 SAVE_IT (atpos_it, *it, atpos_data);
8593 atpos_it.current_x = x_before_this_char;
8594 atpos_it.hpos = hpos_before_this_char;
8595 }
8596 }
8597
8598 prev_method = it->method;
8599 if (it->method == GET_FROM_BUFFER)
8600 prev_pos = IT_CHARPOS (*it);
8601 set_iterator_to_next (it, 1);
8602 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8603 SET_TEXT_POS (this_line_min_pos,
8604 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8605 /* On graphical terminals, newlines may
8606 "overflow" into the fringe if
8607 overflow-newline-into-fringe is non-nil.
8608 On text terminals, and on graphical
8609 terminals with no right margin, newlines
8610 may overflow into the last glyph on the
8611 display line.*/
8612 if (!FRAME_WINDOW_P (it->f)
8613 || ((it->bidi_p
8614 && it->bidi_it.paragraph_dir == R2L)
8615 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8616 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8617 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8618 {
8619 if (!get_next_display_element (it))
8620 {
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 break;
8623 }
8624 if (BUFFER_POS_REACHED_P ())
8625 {
8626 if (ITERATOR_AT_END_OF_LINE_P (it))
8627 result = MOVE_POS_MATCH_OR_ZV;
8628 else
8629 result = MOVE_LINE_CONTINUED;
8630 break;
8631 }
8632 if (ITERATOR_AT_END_OF_LINE_P (it)
8633 && (it->line_wrap != WORD_WRAP
8634 || wrap_it.sp < 0))
8635 {
8636 result = MOVE_NEWLINE_OR_CR;
8637 break;
8638 }
8639 }
8640 }
8641 }
8642 else
8643 IT_RESET_X_ASCENT_DESCENT (it);
8644
8645 if (wrap_it.sp >= 0)
8646 {
8647 RESTORE_IT (it, &wrap_it, wrap_data);
8648 atpos_it.sp = -1;
8649 atx_it.sp = -1;
8650 }
8651
8652 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8653 IT_CHARPOS (*it)));
8654 result = MOVE_LINE_CONTINUED;
8655 break;
8656 }
8657
8658 if (BUFFER_POS_REACHED_P ())
8659 {
8660 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8661 goto buffer_pos_reached;
8662 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8663 {
8664 SAVE_IT (atpos_it, *it, atpos_data);
8665 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8666 }
8667 }
8668
8669 if (new_x > it->first_visible_x)
8670 {
8671 /* Glyph is visible. Increment number of glyphs that
8672 would be displayed. */
8673 ++it->hpos;
8674 }
8675 }
8676
8677 if (result != MOVE_UNDEFINED)
8678 break;
8679 }
8680 else if (BUFFER_POS_REACHED_P ())
8681 {
8682 buffer_pos_reached:
8683 IT_RESET_X_ASCENT_DESCENT (it);
8684 result = MOVE_POS_MATCH_OR_ZV;
8685 break;
8686 }
8687 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8688 {
8689 /* Stop when TO_X specified and reached. This check is
8690 necessary here because of lines consisting of a line end,
8691 only. The line end will not produce any glyphs and we
8692 would never get MOVE_X_REACHED. */
8693 eassert (it->nglyphs == 0);
8694 result = MOVE_X_REACHED;
8695 break;
8696 }
8697
8698 /* Is this a line end? If yes, we're done. */
8699 if (ITERATOR_AT_END_OF_LINE_P (it))
8700 {
8701 /* If we are past TO_CHARPOS, but never saw any character
8702 positions smaller than TO_CHARPOS, return
8703 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8704 did. */
8705 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8706 {
8707 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8708 {
8709 if (IT_CHARPOS (ppos_it) < ZV)
8710 {
8711 RESTORE_IT (it, &ppos_it, ppos_data);
8712 result = MOVE_POS_MATCH_OR_ZV;
8713 }
8714 else
8715 goto buffer_pos_reached;
8716 }
8717 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8718 && IT_CHARPOS (*it) > to_charpos)
8719 goto buffer_pos_reached;
8720 else
8721 result = MOVE_NEWLINE_OR_CR;
8722 }
8723 else
8724 result = MOVE_NEWLINE_OR_CR;
8725 break;
8726 }
8727
8728 prev_method = it->method;
8729 if (it->method == GET_FROM_BUFFER)
8730 prev_pos = IT_CHARPOS (*it);
8731 /* The current display element has been consumed. Advance
8732 to the next. */
8733 set_iterator_to_next (it, 1);
8734 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8735 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8736 if (IT_CHARPOS (*it) < to_charpos)
8737 saw_smaller_pos = 1;
8738 if (it->bidi_p
8739 && (op & MOVE_TO_POS)
8740 && IT_CHARPOS (*it) >= to_charpos
8741 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8742 SAVE_IT (ppos_it, *it, ppos_data);
8743
8744 /* Stop if lines are truncated and IT's current x-position is
8745 past the right edge of the window now. */
8746 if (it->line_wrap == TRUNCATE
8747 && it->current_x >= it->last_visible_x)
8748 {
8749 if (!FRAME_WINDOW_P (it->f)
8750 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8751 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8752 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8753 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8754 {
8755 int at_eob_p = 0;
8756
8757 if ((at_eob_p = !get_next_display_element (it))
8758 || BUFFER_POS_REACHED_P ()
8759 /* If we are past TO_CHARPOS, but never saw any
8760 character positions smaller than TO_CHARPOS,
8761 return MOVE_POS_MATCH_OR_ZV, like the
8762 unidirectional display did. */
8763 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8764 && !saw_smaller_pos
8765 && IT_CHARPOS (*it) > to_charpos))
8766 {
8767 if (it->bidi_p
8768 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8769 RESTORE_IT (it, &ppos_it, ppos_data);
8770 result = MOVE_POS_MATCH_OR_ZV;
8771 break;
8772 }
8773 if (ITERATOR_AT_END_OF_LINE_P (it))
8774 {
8775 result = MOVE_NEWLINE_OR_CR;
8776 break;
8777 }
8778 }
8779 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8780 && !saw_smaller_pos
8781 && IT_CHARPOS (*it) > to_charpos)
8782 {
8783 if (IT_CHARPOS (ppos_it) < ZV)
8784 RESTORE_IT (it, &ppos_it, ppos_data);
8785 result = MOVE_POS_MATCH_OR_ZV;
8786 break;
8787 }
8788 result = MOVE_LINE_TRUNCATED;
8789 break;
8790 }
8791 #undef IT_RESET_X_ASCENT_DESCENT
8792 }
8793
8794 #undef BUFFER_POS_REACHED_P
8795
8796 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8797 restore the saved iterator. */
8798 if (atpos_it.sp >= 0)
8799 RESTORE_IT (it, &atpos_it, atpos_data);
8800 else if (atx_it.sp >= 0)
8801 RESTORE_IT (it, &atx_it, atx_data);
8802
8803 done:
8804
8805 if (atpos_data)
8806 bidi_unshelve_cache (atpos_data, 1);
8807 if (atx_data)
8808 bidi_unshelve_cache (atx_data, 1);
8809 if (wrap_data)
8810 bidi_unshelve_cache (wrap_data, 1);
8811 if (ppos_data)
8812 bidi_unshelve_cache (ppos_data, 1);
8813
8814 /* Restore the iterator settings altered at the beginning of this
8815 function. */
8816 it->glyph_row = saved_glyph_row;
8817 return result;
8818 }
8819
8820 /* For external use. */
8821 void
8822 move_it_in_display_line (struct it *it,
8823 ptrdiff_t to_charpos, int to_x,
8824 enum move_operation_enum op)
8825 {
8826 if (it->line_wrap == WORD_WRAP
8827 && (op & MOVE_TO_X))
8828 {
8829 struct it save_it;
8830 void *save_data = NULL;
8831 int skip;
8832
8833 SAVE_IT (save_it, *it, save_data);
8834 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8835 /* When word-wrap is on, TO_X may lie past the end
8836 of a wrapped line. Then it->current is the
8837 character on the next line, so backtrack to the
8838 space before the wrap point. */
8839 if (skip == MOVE_LINE_CONTINUED)
8840 {
8841 int prev_x = max (it->current_x - 1, 0);
8842 RESTORE_IT (it, &save_it, save_data);
8843 move_it_in_display_line_to
8844 (it, -1, prev_x, MOVE_TO_X);
8845 }
8846 else
8847 bidi_unshelve_cache (save_data, 1);
8848 }
8849 else
8850 move_it_in_display_line_to (it, to_charpos, to_x, op);
8851 }
8852
8853
8854 /* Move IT forward until it satisfies one or more of the criteria in
8855 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8856
8857 OP is a bit-mask that specifies where to stop, and in particular,
8858 which of those four position arguments makes a difference. See the
8859 description of enum move_operation_enum.
8860
8861 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8862 screen line, this function will set IT to the next position that is
8863 displayed to the right of TO_CHARPOS on the screen.
8864
8865 Return the maximum pixel length of any line scanned but never more
8866 than it.last_visible_x. */
8867
8868 int
8869 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8870 {
8871 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8872 int line_height, line_start_x = 0, reached = 0;
8873 int max_current_x = 0;
8874 void *backup_data = NULL;
8875
8876 for (;;)
8877 {
8878 if (op & MOVE_TO_VPOS)
8879 {
8880 /* If no TO_CHARPOS and no TO_X specified, stop at the
8881 start of the line TO_VPOS. */
8882 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8883 {
8884 if (it->vpos == to_vpos)
8885 {
8886 reached = 1;
8887 break;
8888 }
8889 else
8890 skip = move_it_in_display_line_to (it, -1, -1, 0);
8891 }
8892 else
8893 {
8894 /* TO_VPOS >= 0 means stop at TO_X in the line at
8895 TO_VPOS, or at TO_POS, whichever comes first. */
8896 if (it->vpos == to_vpos)
8897 {
8898 reached = 2;
8899 break;
8900 }
8901
8902 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8903
8904 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8905 {
8906 reached = 3;
8907 break;
8908 }
8909 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8910 {
8911 /* We have reached TO_X but not in the line we want. */
8912 skip = move_it_in_display_line_to (it, to_charpos,
8913 -1, MOVE_TO_POS);
8914 if (skip == MOVE_POS_MATCH_OR_ZV)
8915 {
8916 reached = 4;
8917 break;
8918 }
8919 }
8920 }
8921 }
8922 else if (op & MOVE_TO_Y)
8923 {
8924 struct it it_backup;
8925
8926 if (it->line_wrap == WORD_WRAP)
8927 SAVE_IT (it_backup, *it, backup_data);
8928
8929 /* TO_Y specified means stop at TO_X in the line containing
8930 TO_Y---or at TO_CHARPOS if this is reached first. The
8931 problem is that we can't really tell whether the line
8932 contains TO_Y before we have completely scanned it, and
8933 this may skip past TO_X. What we do is to first scan to
8934 TO_X.
8935
8936 If TO_X is not specified, use a TO_X of zero. The reason
8937 is to make the outcome of this function more predictable.
8938 If we didn't use TO_X == 0, we would stop at the end of
8939 the line which is probably not what a caller would expect
8940 to happen. */
8941 skip = move_it_in_display_line_to
8942 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8943 (MOVE_TO_X | (op & MOVE_TO_POS)));
8944
8945 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8946 if (skip == MOVE_POS_MATCH_OR_ZV)
8947 reached = 5;
8948 else if (skip == MOVE_X_REACHED)
8949 {
8950 /* If TO_X was reached, we want to know whether TO_Y is
8951 in the line. We know this is the case if the already
8952 scanned glyphs make the line tall enough. Otherwise,
8953 we must check by scanning the rest of the line. */
8954 line_height = it->max_ascent + it->max_descent;
8955 if (to_y >= it->current_y
8956 && to_y < it->current_y + line_height)
8957 {
8958 reached = 6;
8959 break;
8960 }
8961 SAVE_IT (it_backup, *it, backup_data);
8962 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8963 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8964 op & MOVE_TO_POS);
8965 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8966 line_height = it->max_ascent + it->max_descent;
8967 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8968
8969 if (to_y >= it->current_y
8970 && to_y < it->current_y + line_height)
8971 {
8972 /* If TO_Y is in this line and TO_X was reached
8973 above, we scanned too far. We have to restore
8974 IT's settings to the ones before skipping. But
8975 keep the more accurate values of max_ascent and
8976 max_descent we've found while skipping the rest
8977 of the line, for the sake of callers, such as
8978 pos_visible_p, that need to know the line
8979 height. */
8980 int max_ascent = it->max_ascent;
8981 int max_descent = it->max_descent;
8982
8983 RESTORE_IT (it, &it_backup, backup_data);
8984 it->max_ascent = max_ascent;
8985 it->max_descent = max_descent;
8986 reached = 6;
8987 }
8988 else
8989 {
8990 skip = skip2;
8991 if (skip == MOVE_POS_MATCH_OR_ZV)
8992 reached = 7;
8993 }
8994 }
8995 else
8996 {
8997 /* Check whether TO_Y is in this line. */
8998 line_height = it->max_ascent + it->max_descent;
8999 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9000
9001 if (to_y >= it->current_y
9002 && to_y < it->current_y + line_height)
9003 {
9004 if (to_y > it->current_y)
9005 max_current_x = max (it->current_x, max_current_x);
9006
9007 /* When word-wrap is on, TO_X may lie past the end
9008 of a wrapped line. Then it->current is the
9009 character on the next line, so backtrack to the
9010 space before the wrap point. */
9011 if (skip == MOVE_LINE_CONTINUED
9012 && it->line_wrap == WORD_WRAP)
9013 {
9014 int prev_x = max (it->current_x - 1, 0);
9015 RESTORE_IT (it, &it_backup, backup_data);
9016 skip = move_it_in_display_line_to
9017 (it, -1, prev_x, MOVE_TO_X);
9018 }
9019
9020 reached = 6;
9021 }
9022 }
9023
9024 if (reached)
9025 {
9026 max_current_x = max (it->current_x, max_current_x);
9027 break;
9028 }
9029 }
9030 else if (BUFFERP (it->object)
9031 && (it->method == GET_FROM_BUFFER
9032 || it->method == GET_FROM_STRETCH)
9033 && IT_CHARPOS (*it) >= to_charpos
9034 /* Under bidi iteration, a call to set_iterator_to_next
9035 can scan far beyond to_charpos if the initial
9036 portion of the next line needs to be reordered. In
9037 that case, give move_it_in_display_line_to another
9038 chance below. */
9039 && !(it->bidi_p
9040 && it->bidi_it.scan_dir == -1))
9041 skip = MOVE_POS_MATCH_OR_ZV;
9042 else
9043 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9044
9045 switch (skip)
9046 {
9047 case MOVE_POS_MATCH_OR_ZV:
9048 max_current_x = max (it->current_x, max_current_x);
9049 reached = 8;
9050 goto out;
9051
9052 case MOVE_NEWLINE_OR_CR:
9053 max_current_x = max (it->current_x, max_current_x);
9054 set_iterator_to_next (it, 1);
9055 it->continuation_lines_width = 0;
9056 break;
9057
9058 case MOVE_LINE_TRUNCATED:
9059 max_current_x = it->last_visible_x;
9060 it->continuation_lines_width = 0;
9061 reseat_at_next_visible_line_start (it, 0);
9062 if ((op & MOVE_TO_POS) != 0
9063 && IT_CHARPOS (*it) > to_charpos)
9064 {
9065 reached = 9;
9066 goto out;
9067 }
9068 break;
9069
9070 case MOVE_LINE_CONTINUED:
9071 max_current_x = it->last_visible_x;
9072 /* For continued lines ending in a tab, some of the glyphs
9073 associated with the tab are displayed on the current
9074 line. Since it->current_x does not include these glyphs,
9075 we use it->last_visible_x instead. */
9076 if (it->c == '\t')
9077 {
9078 it->continuation_lines_width += it->last_visible_x;
9079 /* When moving by vpos, ensure that the iterator really
9080 advances to the next line (bug#847, bug#969). Fixme:
9081 do we need to do this in other circumstances? */
9082 if (it->current_x != it->last_visible_x
9083 && (op & MOVE_TO_VPOS)
9084 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9085 {
9086 line_start_x = it->current_x + it->pixel_width
9087 - it->last_visible_x;
9088 set_iterator_to_next (it, 0);
9089 }
9090 }
9091 else
9092 it->continuation_lines_width += it->current_x;
9093 break;
9094
9095 default:
9096 emacs_abort ();
9097 }
9098
9099 /* Reset/increment for the next run. */
9100 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9101 it->current_x = line_start_x;
9102 line_start_x = 0;
9103 it->hpos = 0;
9104 it->current_y += it->max_ascent + it->max_descent;
9105 ++it->vpos;
9106 last_height = it->max_ascent + it->max_descent;
9107 it->max_ascent = it->max_descent = 0;
9108 }
9109
9110 out:
9111
9112 /* On text terminals, we may stop at the end of a line in the middle
9113 of a multi-character glyph. If the glyph itself is continued,
9114 i.e. it is actually displayed on the next line, don't treat this
9115 stopping point as valid; move to the next line instead (unless
9116 that brings us offscreen). */
9117 if (!FRAME_WINDOW_P (it->f)
9118 && op & MOVE_TO_POS
9119 && IT_CHARPOS (*it) == to_charpos
9120 && it->what == IT_CHARACTER
9121 && it->nglyphs > 1
9122 && it->line_wrap == WINDOW_WRAP
9123 && it->current_x == it->last_visible_x - 1
9124 && it->c != '\n'
9125 && it->c != '\t'
9126 && it->vpos < it->w->window_end_vpos)
9127 {
9128 it->continuation_lines_width += it->current_x;
9129 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9130 it->current_y += it->max_ascent + it->max_descent;
9131 ++it->vpos;
9132 last_height = it->max_ascent + it->max_descent;
9133 }
9134
9135 if (backup_data)
9136 bidi_unshelve_cache (backup_data, 1);
9137
9138 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9139
9140 return max_current_x;
9141 }
9142
9143
9144 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9145
9146 If DY > 0, move IT backward at least that many pixels. DY = 0
9147 means move IT backward to the preceding line start or BEGV. This
9148 function may move over more than DY pixels if IT->current_y - DY
9149 ends up in the middle of a line; in this case IT->current_y will be
9150 set to the top of the line moved to. */
9151
9152 void
9153 move_it_vertically_backward (struct it *it, int dy)
9154 {
9155 int nlines, h;
9156 struct it it2, it3;
9157 void *it2data = NULL, *it3data = NULL;
9158 ptrdiff_t start_pos;
9159 int nchars_per_row
9160 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9161 ptrdiff_t pos_limit;
9162
9163 move_further_back:
9164 eassert (dy >= 0);
9165
9166 start_pos = IT_CHARPOS (*it);
9167
9168 /* Estimate how many newlines we must move back. */
9169 nlines = max (1, dy / default_line_pixel_height (it->w));
9170 if (it->line_wrap == TRUNCATE)
9171 pos_limit = BEGV;
9172 else
9173 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9174
9175 /* Set the iterator's position that many lines back. But don't go
9176 back more than NLINES full screen lines -- this wins a day with
9177 buffers which have very long lines. */
9178 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9179 back_to_previous_visible_line_start (it);
9180
9181 /* Reseat the iterator here. When moving backward, we don't want
9182 reseat to skip forward over invisible text, set up the iterator
9183 to deliver from overlay strings at the new position etc. So,
9184 use reseat_1 here. */
9185 reseat_1 (it, it->current.pos, 1);
9186
9187 /* We are now surely at a line start. */
9188 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9189 reordering is in effect. */
9190 it->continuation_lines_width = 0;
9191
9192 /* Move forward and see what y-distance we moved. First move to the
9193 start of the next line so that we get its height. We need this
9194 height to be able to tell whether we reached the specified
9195 y-distance. */
9196 SAVE_IT (it2, *it, it2data);
9197 it2.max_ascent = it2.max_descent = 0;
9198 do
9199 {
9200 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9201 MOVE_TO_POS | MOVE_TO_VPOS);
9202 }
9203 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9204 /* If we are in a display string which starts at START_POS,
9205 and that display string includes a newline, and we are
9206 right after that newline (i.e. at the beginning of a
9207 display line), exit the loop, because otherwise we will
9208 infloop, since move_it_to will see that it is already at
9209 START_POS and will not move. */
9210 || (it2.method == GET_FROM_STRING
9211 && IT_CHARPOS (it2) == start_pos
9212 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9213 eassert (IT_CHARPOS (*it) >= BEGV);
9214 SAVE_IT (it3, it2, it3data);
9215
9216 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9217 eassert (IT_CHARPOS (*it) >= BEGV);
9218 /* H is the actual vertical distance from the position in *IT
9219 and the starting position. */
9220 h = it2.current_y - it->current_y;
9221 /* NLINES is the distance in number of lines. */
9222 nlines = it2.vpos - it->vpos;
9223
9224 /* Correct IT's y and vpos position
9225 so that they are relative to the starting point. */
9226 it->vpos -= nlines;
9227 it->current_y -= h;
9228
9229 if (dy == 0)
9230 {
9231 /* DY == 0 means move to the start of the screen line. The
9232 value of nlines is > 0 if continuation lines were involved,
9233 or if the original IT position was at start of a line. */
9234 RESTORE_IT (it, it, it2data);
9235 if (nlines > 0)
9236 move_it_by_lines (it, nlines);
9237 /* The above code moves us to some position NLINES down,
9238 usually to its first glyph (leftmost in an L2R line), but
9239 that's not necessarily the start of the line, under bidi
9240 reordering. We want to get to the character position
9241 that is immediately after the newline of the previous
9242 line. */
9243 if (it->bidi_p
9244 && !it->continuation_lines_width
9245 && !STRINGP (it->string)
9246 && IT_CHARPOS (*it) > BEGV
9247 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9248 {
9249 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9250
9251 DEC_BOTH (cp, bp);
9252 cp = find_newline_no_quit (cp, bp, -1, NULL);
9253 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9254 }
9255 bidi_unshelve_cache (it3data, 1);
9256 }
9257 else
9258 {
9259 /* The y-position we try to reach, relative to *IT.
9260 Note that H has been subtracted in front of the if-statement. */
9261 int target_y = it->current_y + h - dy;
9262 int y0 = it3.current_y;
9263 int y1;
9264 int line_height;
9265
9266 RESTORE_IT (&it3, &it3, it3data);
9267 y1 = line_bottom_y (&it3);
9268 line_height = y1 - y0;
9269 RESTORE_IT (it, it, it2data);
9270 /* If we did not reach target_y, try to move further backward if
9271 we can. If we moved too far backward, try to move forward. */
9272 if (target_y < it->current_y
9273 /* This is heuristic. In a window that's 3 lines high, with
9274 a line height of 13 pixels each, recentering with point
9275 on the bottom line will try to move -39/2 = 19 pixels
9276 backward. Try to avoid moving into the first line. */
9277 && (it->current_y - target_y
9278 > min (window_box_height (it->w), line_height * 2 / 3))
9279 && IT_CHARPOS (*it) > BEGV)
9280 {
9281 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9282 target_y - it->current_y));
9283 dy = it->current_y - target_y;
9284 goto move_further_back;
9285 }
9286 else if (target_y >= it->current_y + line_height
9287 && IT_CHARPOS (*it) < ZV)
9288 {
9289 /* Should move forward by at least one line, maybe more.
9290
9291 Note: Calling move_it_by_lines can be expensive on
9292 terminal frames, where compute_motion is used (via
9293 vmotion) to do the job, when there are very long lines
9294 and truncate-lines is nil. That's the reason for
9295 treating terminal frames specially here. */
9296
9297 if (!FRAME_WINDOW_P (it->f))
9298 move_it_vertically (it, target_y - (it->current_y + line_height));
9299 else
9300 {
9301 do
9302 {
9303 move_it_by_lines (it, 1);
9304 }
9305 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9306 }
9307 }
9308 }
9309 }
9310
9311
9312 /* Move IT by a specified amount of pixel lines DY. DY negative means
9313 move backwards. DY = 0 means move to start of screen line. At the
9314 end, IT will be on the start of a screen line. */
9315
9316 void
9317 move_it_vertically (struct it *it, int dy)
9318 {
9319 if (dy <= 0)
9320 move_it_vertically_backward (it, -dy);
9321 else
9322 {
9323 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9324 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9325 MOVE_TO_POS | MOVE_TO_Y);
9326 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9327
9328 /* If buffer ends in ZV without a newline, move to the start of
9329 the line to satisfy the post-condition. */
9330 if (IT_CHARPOS (*it) == ZV
9331 && ZV > BEGV
9332 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9333 move_it_by_lines (it, 0);
9334 }
9335 }
9336
9337
9338 /* Move iterator IT past the end of the text line it is in. */
9339
9340 void
9341 move_it_past_eol (struct it *it)
9342 {
9343 enum move_it_result rc;
9344
9345 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9346 if (rc == MOVE_NEWLINE_OR_CR)
9347 set_iterator_to_next (it, 0);
9348 }
9349
9350
9351 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9352 negative means move up. DVPOS == 0 means move to the start of the
9353 screen line.
9354
9355 Optimization idea: If we would know that IT->f doesn't use
9356 a face with proportional font, we could be faster for
9357 truncate-lines nil. */
9358
9359 void
9360 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9361 {
9362
9363 /* The commented-out optimization uses vmotion on terminals. This
9364 gives bad results, because elements like it->what, on which
9365 callers such as pos_visible_p rely, aren't updated. */
9366 /* struct position pos;
9367 if (!FRAME_WINDOW_P (it->f))
9368 {
9369 struct text_pos textpos;
9370
9371 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9372 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9373 reseat (it, textpos, 1);
9374 it->vpos += pos.vpos;
9375 it->current_y += pos.vpos;
9376 }
9377 else */
9378
9379 if (dvpos == 0)
9380 {
9381 /* DVPOS == 0 means move to the start of the screen line. */
9382 move_it_vertically_backward (it, 0);
9383 /* Let next call to line_bottom_y calculate real line height. */
9384 last_height = 0;
9385 }
9386 else if (dvpos > 0)
9387 {
9388 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9389 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9390 {
9391 /* Only move to the next buffer position if we ended up in a
9392 string from display property, not in an overlay string
9393 (before-string or after-string). That is because the
9394 latter don't conceal the underlying buffer position, so
9395 we can ask to move the iterator to the exact position we
9396 are interested in. Note that, even if we are already at
9397 IT_CHARPOS (*it), the call below is not a no-op, as it
9398 will detect that we are at the end of the string, pop the
9399 iterator, and compute it->current_x and it->hpos
9400 correctly. */
9401 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9402 -1, -1, -1, MOVE_TO_POS);
9403 }
9404 }
9405 else
9406 {
9407 struct it it2;
9408 void *it2data = NULL;
9409 ptrdiff_t start_charpos, i;
9410 int nchars_per_row
9411 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9412 ptrdiff_t pos_limit;
9413
9414 /* Start at the beginning of the screen line containing IT's
9415 position. This may actually move vertically backwards,
9416 in case of overlays, so adjust dvpos accordingly. */
9417 dvpos += it->vpos;
9418 move_it_vertically_backward (it, 0);
9419 dvpos -= it->vpos;
9420
9421 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9422 screen lines, and reseat the iterator there. */
9423 start_charpos = IT_CHARPOS (*it);
9424 if (it->line_wrap == TRUNCATE)
9425 pos_limit = BEGV;
9426 else
9427 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9428 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9429 back_to_previous_visible_line_start (it);
9430 reseat (it, it->current.pos, 1);
9431
9432 /* Move further back if we end up in a string or an image. */
9433 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9434 {
9435 /* First try to move to start of display line. */
9436 dvpos += it->vpos;
9437 move_it_vertically_backward (it, 0);
9438 dvpos -= it->vpos;
9439 if (IT_POS_VALID_AFTER_MOVE_P (it))
9440 break;
9441 /* If start of line is still in string or image,
9442 move further back. */
9443 back_to_previous_visible_line_start (it);
9444 reseat (it, it->current.pos, 1);
9445 dvpos--;
9446 }
9447
9448 it->current_x = it->hpos = 0;
9449
9450 /* Above call may have moved too far if continuation lines
9451 are involved. Scan forward and see if it did. */
9452 SAVE_IT (it2, *it, it2data);
9453 it2.vpos = it2.current_y = 0;
9454 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9455 it->vpos -= it2.vpos;
9456 it->current_y -= it2.current_y;
9457 it->current_x = it->hpos = 0;
9458
9459 /* If we moved too far back, move IT some lines forward. */
9460 if (it2.vpos > -dvpos)
9461 {
9462 int delta = it2.vpos + dvpos;
9463
9464 RESTORE_IT (&it2, &it2, it2data);
9465 SAVE_IT (it2, *it, it2data);
9466 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9467 /* Move back again if we got too far ahead. */
9468 if (IT_CHARPOS (*it) >= start_charpos)
9469 RESTORE_IT (it, &it2, it2data);
9470 else
9471 bidi_unshelve_cache (it2data, 1);
9472 }
9473 else
9474 RESTORE_IT (it, it, it2data);
9475 }
9476 }
9477
9478 /* Return true if IT points into the middle of a display vector. */
9479
9480 bool
9481 in_display_vector_p (struct it *it)
9482 {
9483 return (it->method == GET_FROM_DISPLAY_VECTOR
9484 && it->current.dpvec_index > 0
9485 && it->dpvec + it->current.dpvec_index != it->dpend);
9486 }
9487
9488 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9489 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9490 WINDOW must be a live window and defaults to the selected one. The
9491 return value is a cons of the maximum pixel-width of any text line and
9492 the maximum pixel-height of all text lines.
9493
9494 The optional argument FROM, if non-nil, specifies the first text
9495 position and defaults to the minimum accessible position of the buffer.
9496 If FROM is t, use the minimum accessible position that is not a newline
9497 character. TO, if non-nil, specifies the last text position and
9498 defaults to the maximum accessible position of the buffer. If TO is t,
9499 use the maximum accessible position that is not a newline character.
9500
9501 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9502 width that can be returned. X_LIMIT nil or omitted, means to use the
9503 pixel-width of WINDOW's body; use this if you do not intend to change
9504 the width of WINDOW. Use the maximum width WINDOW may assume if you
9505 intend to change WINDOW's width.
9506
9507 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9508 height that can be returned. Text lines whose y-coordinate is beyond
9509 Y_LIMIT are ignored. Since calculating the text height of a large
9510 buffer can take some time, it makes sense to specify this argument if
9511 the size of the buffer is unknown.
9512
9513 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9514 include the height of the mode- or header-line of WINDOW in the return
9515 value. If it is either the symbol `mode-line' or `header-line', include
9516 only the height of that line, if present, in the return value. If t,
9517 include the height of both, if present, in the return value. */)
9518 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9519 Lisp_Object mode_and_header_line)
9520 {
9521 struct window *w = decode_live_window (window);
9522 Lisp_Object buf;
9523 struct buffer *b;
9524 struct it it;
9525 struct buffer *old_buffer = NULL;
9526 ptrdiff_t start, end, pos;
9527 struct text_pos startp;
9528 void *itdata = NULL;
9529 int c, max_y = -1, x = 0, y = 0;
9530
9531 buf = w->contents;
9532 CHECK_BUFFER (buf);
9533 b = XBUFFER (buf);
9534
9535 if (b != current_buffer)
9536 {
9537 old_buffer = current_buffer;
9538 set_buffer_internal (b);
9539 }
9540
9541 if (NILP (from))
9542 start = BEGV;
9543 else if (EQ (from, Qt))
9544 {
9545 start = pos = BEGV;
9546 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9547 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9548 start = pos;
9549 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9550 start = pos;
9551 }
9552 else
9553 {
9554 CHECK_NUMBER_COERCE_MARKER (from);
9555 start = min (max (XINT (from), BEGV), ZV);
9556 }
9557
9558 if (NILP (to))
9559 end = ZV;
9560 else if (EQ (to, Qt))
9561 {
9562 end = pos = ZV;
9563 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9564 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9565 end = pos;
9566 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9567 end = pos;
9568 }
9569 else
9570 {
9571 CHECK_NUMBER_COERCE_MARKER (to);
9572 end = max (start, min (XINT (to), ZV));
9573 }
9574
9575 if (!NILP (y_limit))
9576 {
9577 CHECK_NUMBER (y_limit);
9578 max_y = min (XINT (y_limit), INT_MAX);
9579 }
9580
9581 itdata = bidi_shelve_cache ();
9582 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9583 start_display (&it, w, startp);
9584
9585 if (NILP (x_limit))
9586 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9587 else
9588 {
9589 CHECK_NUMBER (x_limit);
9590 it.last_visible_x = min (XINT (x_limit), INFINITY);
9591 /* Actually, we never want move_it_to stop at to_x. But to make
9592 sure that move_it_in_display_line_to always moves far enough,
9593 we set it to INT_MAX and specify MOVE_TO_X. */
9594 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9595 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9596 }
9597
9598 y = it.current_y + it.max_ascent + it.max_descent;
9599
9600 if (!EQ (mode_and_header_line, Qheader_line)
9601 && !EQ (mode_and_header_line, Qt))
9602 /* Do not count the header-line which was counted automatically by
9603 start_display. */
9604 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9605
9606 if (EQ (mode_and_header_line, Qmode_line)
9607 || EQ (mode_and_header_line, Qt))
9608 /* Do count the mode-line which is not included automatically by
9609 start_display. */
9610 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9611
9612 bidi_unshelve_cache (itdata, 0);
9613
9614 if (old_buffer)
9615 set_buffer_internal (old_buffer);
9616
9617 return Fcons (make_number (x), make_number (y));
9618 }
9619 \f
9620 /***********************************************************************
9621 Messages
9622 ***********************************************************************/
9623
9624
9625 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9626 to *Messages*. */
9627
9628 void
9629 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9630 {
9631 Lisp_Object args[3];
9632 Lisp_Object msg, fmt;
9633 char *buffer;
9634 ptrdiff_t len;
9635 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9636 USE_SAFE_ALLOCA;
9637
9638 fmt = msg = Qnil;
9639 GCPRO4 (fmt, msg, arg1, arg2);
9640
9641 args[0] = fmt = build_string (format);
9642 args[1] = arg1;
9643 args[2] = arg2;
9644 msg = Fformat (3, args);
9645
9646 len = SBYTES (msg) + 1;
9647 buffer = SAFE_ALLOCA (len);
9648 memcpy (buffer, SDATA (msg), len);
9649
9650 message_dolog (buffer, len - 1, 1, 0);
9651 SAFE_FREE ();
9652
9653 UNGCPRO;
9654 }
9655
9656
9657 /* Output a newline in the *Messages* buffer if "needs" one. */
9658
9659 void
9660 message_log_maybe_newline (void)
9661 {
9662 if (message_log_need_newline)
9663 message_dolog ("", 0, 1, 0);
9664 }
9665
9666
9667 /* Add a string M of length NBYTES to the message log, optionally
9668 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9669 true, means interpret the contents of M as multibyte. This
9670 function calls low-level routines in order to bypass text property
9671 hooks, etc. which might not be safe to run.
9672
9673 This may GC (insert may run before/after change hooks),
9674 so the buffer M must NOT point to a Lisp string. */
9675
9676 void
9677 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9678 {
9679 const unsigned char *msg = (const unsigned char *) m;
9680
9681 if (!NILP (Vmemory_full))
9682 return;
9683
9684 if (!NILP (Vmessage_log_max))
9685 {
9686 struct buffer *oldbuf;
9687 Lisp_Object oldpoint, oldbegv, oldzv;
9688 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9689 ptrdiff_t point_at_end = 0;
9690 ptrdiff_t zv_at_end = 0;
9691 Lisp_Object old_deactivate_mark;
9692 struct gcpro gcpro1;
9693
9694 old_deactivate_mark = Vdeactivate_mark;
9695 oldbuf = current_buffer;
9696
9697 /* Ensure the Messages buffer exists, and switch to it.
9698 If we created it, set the major-mode. */
9699 {
9700 int newbuffer = 0;
9701 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9702
9703 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9704
9705 if (newbuffer
9706 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9707 call0 (intern ("messages-buffer-mode"));
9708 }
9709
9710 bset_undo_list (current_buffer, Qt);
9711 bset_cache_long_scans (current_buffer, Qnil);
9712
9713 oldpoint = message_dolog_marker1;
9714 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9715 oldbegv = message_dolog_marker2;
9716 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9717 oldzv = message_dolog_marker3;
9718 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9719 GCPRO1 (old_deactivate_mark);
9720
9721 if (PT == Z)
9722 point_at_end = 1;
9723 if (ZV == Z)
9724 zv_at_end = 1;
9725
9726 BEGV = BEG;
9727 BEGV_BYTE = BEG_BYTE;
9728 ZV = Z;
9729 ZV_BYTE = Z_BYTE;
9730 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9731
9732 /* Insert the string--maybe converting multibyte to single byte
9733 or vice versa, so that all the text fits the buffer. */
9734 if (multibyte
9735 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9736 {
9737 ptrdiff_t i;
9738 int c, char_bytes;
9739 char work[1];
9740
9741 /* Convert a multibyte string to single-byte
9742 for the *Message* buffer. */
9743 for (i = 0; i < nbytes; i += char_bytes)
9744 {
9745 c = string_char_and_length (msg + i, &char_bytes);
9746 work[0] = (ASCII_CHAR_P (c)
9747 ? c
9748 : multibyte_char_to_unibyte (c));
9749 insert_1_both (work, 1, 1, 1, 0, 0);
9750 }
9751 }
9752 else if (! multibyte
9753 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9754 {
9755 ptrdiff_t i;
9756 int c, char_bytes;
9757 unsigned char str[MAX_MULTIBYTE_LENGTH];
9758 /* Convert a single-byte string to multibyte
9759 for the *Message* buffer. */
9760 for (i = 0; i < nbytes; i++)
9761 {
9762 c = msg[i];
9763 MAKE_CHAR_MULTIBYTE (c);
9764 char_bytes = CHAR_STRING (c, str);
9765 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9766 }
9767 }
9768 else if (nbytes)
9769 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9770
9771 if (nlflag)
9772 {
9773 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9774 printmax_t dups;
9775
9776 insert_1_both ("\n", 1, 1, 1, 0, 0);
9777
9778 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9779 this_bol = PT;
9780 this_bol_byte = PT_BYTE;
9781
9782 /* See if this line duplicates the previous one.
9783 If so, combine duplicates. */
9784 if (this_bol > BEG)
9785 {
9786 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9787 prev_bol = PT;
9788 prev_bol_byte = PT_BYTE;
9789
9790 dups = message_log_check_duplicate (prev_bol_byte,
9791 this_bol_byte);
9792 if (dups)
9793 {
9794 del_range_both (prev_bol, prev_bol_byte,
9795 this_bol, this_bol_byte, 0);
9796 if (dups > 1)
9797 {
9798 char dupstr[sizeof " [ times]"
9799 + INT_STRLEN_BOUND (printmax_t)];
9800
9801 /* If you change this format, don't forget to also
9802 change message_log_check_duplicate. */
9803 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9805 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9806 }
9807 }
9808 }
9809
9810 /* If we have more than the desired maximum number of lines
9811 in the *Messages* buffer now, delete the oldest ones.
9812 This is safe because we don't have undo in this buffer. */
9813
9814 if (NATNUMP (Vmessage_log_max))
9815 {
9816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9817 -XFASTINT (Vmessage_log_max) - 1, 0);
9818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9819 }
9820 }
9821 BEGV = marker_position (oldbegv);
9822 BEGV_BYTE = marker_byte_position (oldbegv);
9823
9824 if (zv_at_end)
9825 {
9826 ZV = Z;
9827 ZV_BYTE = Z_BYTE;
9828 }
9829 else
9830 {
9831 ZV = marker_position (oldzv);
9832 ZV_BYTE = marker_byte_position (oldzv);
9833 }
9834
9835 if (point_at_end)
9836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9837 else
9838 /* We can't do Fgoto_char (oldpoint) because it will run some
9839 Lisp code. */
9840 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9841 marker_byte_position (oldpoint));
9842
9843 UNGCPRO;
9844 unchain_marker (XMARKER (oldpoint));
9845 unchain_marker (XMARKER (oldbegv));
9846 unchain_marker (XMARKER (oldzv));
9847
9848 /* We called insert_1_both above with its 5th argument (PREPARE)
9849 zero, which prevents insert_1_both from calling
9850 prepare_to_modify_buffer, which in turns prevents us from
9851 incrementing windows_or_buffers_changed even if *Messages* is
9852 shown in some window. So we must manually set
9853 windows_or_buffers_changed here to make up for that. */
9854 windows_or_buffers_changed = old_windows_or_buffers_changed;
9855 bset_redisplay (current_buffer);
9856
9857 set_buffer_internal (oldbuf);
9858
9859 message_log_need_newline = !nlflag;
9860 Vdeactivate_mark = old_deactivate_mark;
9861 }
9862 }
9863
9864
9865 /* We are at the end of the buffer after just having inserted a newline.
9866 (Note: We depend on the fact we won't be crossing the gap.)
9867 Check to see if the most recent message looks a lot like the previous one.
9868 Return 0 if different, 1 if the new one should just replace it, or a
9869 value N > 1 if we should also append " [N times]". */
9870
9871 static intmax_t
9872 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9873 {
9874 ptrdiff_t i;
9875 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9876 int seen_dots = 0;
9877 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9878 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9879
9880 for (i = 0; i < len; i++)
9881 {
9882 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9883 seen_dots = 1;
9884 if (p1[i] != p2[i])
9885 return seen_dots;
9886 }
9887 p1 += len;
9888 if (*p1 == '\n')
9889 return 2;
9890 if (*p1++ == ' ' && *p1++ == '[')
9891 {
9892 char *pend;
9893 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9894 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9895 return n + 1;
9896 }
9897 return 0;
9898 }
9899 \f
9900
9901 /* Display an echo area message M with a specified length of NBYTES
9902 bytes. The string may include null characters. If M is not a
9903 string, clear out any existing message, and let the mini-buffer
9904 text show through.
9905
9906 This function cancels echoing. */
9907
9908 void
9909 message3 (Lisp_Object m)
9910 {
9911 struct gcpro gcpro1;
9912
9913 GCPRO1 (m);
9914 clear_message (true, true);
9915 cancel_echoing ();
9916
9917 /* First flush out any partial line written with print. */
9918 message_log_maybe_newline ();
9919 if (STRINGP (m))
9920 {
9921 ptrdiff_t nbytes = SBYTES (m);
9922 bool multibyte = STRING_MULTIBYTE (m);
9923 USE_SAFE_ALLOCA;
9924 char *buffer = SAFE_ALLOCA (nbytes);
9925 memcpy (buffer, SDATA (m), nbytes);
9926 message_dolog (buffer, nbytes, 1, multibyte);
9927 SAFE_FREE ();
9928 }
9929 message3_nolog (m);
9930
9931 UNGCPRO;
9932 }
9933
9934
9935 /* The non-logging version of message3.
9936 This does not cancel echoing, because it is used for echoing.
9937 Perhaps we need to make a separate function for echoing
9938 and make this cancel echoing. */
9939
9940 void
9941 message3_nolog (Lisp_Object m)
9942 {
9943 struct frame *sf = SELECTED_FRAME ();
9944
9945 if (FRAME_INITIAL_P (sf))
9946 {
9947 if (noninteractive_need_newline)
9948 putc ('\n', stderr);
9949 noninteractive_need_newline = 0;
9950 if (STRINGP (m))
9951 {
9952 Lisp_Object s = ENCODE_SYSTEM (m);
9953
9954 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9955 }
9956 if (cursor_in_echo_area == 0)
9957 fprintf (stderr, "\n");
9958 fflush (stderr);
9959 }
9960 /* Error messages get reported properly by cmd_error, so this must be just an
9961 informative message; if the frame hasn't really been initialized yet, just
9962 toss it. */
9963 else if (INTERACTIVE && sf->glyphs_initialized_p)
9964 {
9965 /* Get the frame containing the mini-buffer
9966 that the selected frame is using. */
9967 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9968 Lisp_Object frame = XWINDOW (mini_window)->frame;
9969 struct frame *f = XFRAME (frame);
9970
9971 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9972 Fmake_frame_visible (frame);
9973
9974 if (STRINGP (m) && SCHARS (m) > 0)
9975 {
9976 set_message (m);
9977 if (minibuffer_auto_raise)
9978 Fraise_frame (frame);
9979 /* Assume we are not echoing.
9980 (If we are, echo_now will override this.) */
9981 echo_message_buffer = Qnil;
9982 }
9983 else
9984 clear_message (true, true);
9985
9986 do_pending_window_change (0);
9987 echo_area_display (1);
9988 do_pending_window_change (0);
9989 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9990 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9991 }
9992 }
9993
9994
9995 /* Display a null-terminated echo area message M. If M is 0, clear
9996 out any existing message, and let the mini-buffer text show through.
9997
9998 The buffer M must continue to exist until after the echo area gets
9999 cleared or some other message gets displayed there. Do not pass
10000 text that is stored in a Lisp string. Do not pass text in a buffer
10001 that was alloca'd. */
10002
10003 void
10004 message1 (const char *m)
10005 {
10006 message3 (m ? build_unibyte_string (m) : Qnil);
10007 }
10008
10009
10010 /* The non-logging counterpart of message1. */
10011
10012 void
10013 message1_nolog (const char *m)
10014 {
10015 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10016 }
10017
10018 /* Display a message M which contains a single %s
10019 which gets replaced with STRING. */
10020
10021 void
10022 message_with_string (const char *m, Lisp_Object string, int log)
10023 {
10024 CHECK_STRING (string);
10025
10026 if (noninteractive)
10027 {
10028 if (m)
10029 {
10030 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10031 String whose data pointer might be passed to us in M. So
10032 we use a local copy. */
10033 char *fmt = xstrdup (m);
10034
10035 if (noninteractive_need_newline)
10036 putc ('\n', stderr);
10037 noninteractive_need_newline = 0;
10038 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10039 if (!cursor_in_echo_area)
10040 fprintf (stderr, "\n");
10041 fflush (stderr);
10042 xfree (fmt);
10043 }
10044 }
10045 else if (INTERACTIVE)
10046 {
10047 /* The frame whose minibuffer we're going to display the message on.
10048 It may be larger than the selected frame, so we need
10049 to use its buffer, not the selected frame's buffer. */
10050 Lisp_Object mini_window;
10051 struct frame *f, *sf = SELECTED_FRAME ();
10052
10053 /* Get the frame containing the minibuffer
10054 that the selected frame is using. */
10055 mini_window = FRAME_MINIBUF_WINDOW (sf);
10056 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10057
10058 /* Error messages get reported properly by cmd_error, so this must be
10059 just an informative message; if the frame hasn't really been
10060 initialized yet, just toss it. */
10061 if (f->glyphs_initialized_p)
10062 {
10063 Lisp_Object args[2], msg;
10064 struct gcpro gcpro1, gcpro2;
10065
10066 args[0] = build_string (m);
10067 args[1] = msg = string;
10068 GCPRO2 (args[0], msg);
10069 gcpro1.nvars = 2;
10070
10071 msg = Fformat (2, args);
10072
10073 if (log)
10074 message3 (msg);
10075 else
10076 message3_nolog (msg);
10077
10078 UNGCPRO;
10079
10080 /* Print should start at the beginning of the message
10081 buffer next time. */
10082 message_buf_print = 0;
10083 }
10084 }
10085 }
10086
10087
10088 /* Dump an informative message to the minibuf. If M is 0, clear out
10089 any existing message, and let the mini-buffer text show through. */
10090
10091 static void
10092 vmessage (const char *m, va_list ap)
10093 {
10094 if (noninteractive)
10095 {
10096 if (m)
10097 {
10098 if (noninteractive_need_newline)
10099 putc ('\n', stderr);
10100 noninteractive_need_newline = 0;
10101 vfprintf (stderr, m, ap);
10102 if (cursor_in_echo_area == 0)
10103 fprintf (stderr, "\n");
10104 fflush (stderr);
10105 }
10106 }
10107 else if (INTERACTIVE)
10108 {
10109 /* The frame whose mini-buffer we're going to display the message
10110 on. It may be larger than the selected frame, so we need to
10111 use its buffer, not the selected frame's buffer. */
10112 Lisp_Object mini_window;
10113 struct frame *f, *sf = SELECTED_FRAME ();
10114
10115 /* Get the frame containing the mini-buffer
10116 that the selected frame is using. */
10117 mini_window = FRAME_MINIBUF_WINDOW (sf);
10118 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10119
10120 /* Error messages get reported properly by cmd_error, so this must be
10121 just an informative message; if the frame hasn't really been
10122 initialized yet, just toss it. */
10123 if (f->glyphs_initialized_p)
10124 {
10125 if (m)
10126 {
10127 ptrdiff_t len;
10128 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10129 char *message_buf = alloca (maxsize + 1);
10130
10131 len = doprnt (message_buf, maxsize, m, 0, ap);
10132
10133 message3 (make_string (message_buf, len));
10134 }
10135 else
10136 message1 (0);
10137
10138 /* Print should start at the beginning of the message
10139 buffer next time. */
10140 message_buf_print = 0;
10141 }
10142 }
10143 }
10144
10145 void
10146 message (const char *m, ...)
10147 {
10148 va_list ap;
10149 va_start (ap, m);
10150 vmessage (m, ap);
10151 va_end (ap);
10152 }
10153
10154
10155 #if 0
10156 /* The non-logging version of message. */
10157
10158 void
10159 message_nolog (const char *m, ...)
10160 {
10161 Lisp_Object old_log_max;
10162 va_list ap;
10163 va_start (ap, m);
10164 old_log_max = Vmessage_log_max;
10165 Vmessage_log_max = Qnil;
10166 vmessage (m, ap);
10167 Vmessage_log_max = old_log_max;
10168 va_end (ap);
10169 }
10170 #endif
10171
10172
10173 /* Display the current message in the current mini-buffer. This is
10174 only called from error handlers in process.c, and is not time
10175 critical. */
10176
10177 void
10178 update_echo_area (void)
10179 {
10180 if (!NILP (echo_area_buffer[0]))
10181 {
10182 Lisp_Object string;
10183 string = Fcurrent_message ();
10184 message3 (string);
10185 }
10186 }
10187
10188
10189 /* Make sure echo area buffers in `echo_buffers' are live.
10190 If they aren't, make new ones. */
10191
10192 static void
10193 ensure_echo_area_buffers (void)
10194 {
10195 int i;
10196
10197 for (i = 0; i < 2; ++i)
10198 if (!BUFFERP (echo_buffer[i])
10199 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10200 {
10201 char name[30];
10202 Lisp_Object old_buffer;
10203 int j;
10204
10205 old_buffer = echo_buffer[i];
10206 echo_buffer[i] = Fget_buffer_create
10207 (make_formatted_string (name, " *Echo Area %d*", i));
10208 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10209 /* to force word wrap in echo area -
10210 it was decided to postpone this*/
10211 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10212
10213 for (j = 0; j < 2; ++j)
10214 if (EQ (old_buffer, echo_area_buffer[j]))
10215 echo_area_buffer[j] = echo_buffer[i];
10216 }
10217 }
10218
10219
10220 /* Call FN with args A1..A2 with either the current or last displayed
10221 echo_area_buffer as current buffer.
10222
10223 WHICH zero means use the current message buffer
10224 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10225 from echo_buffer[] and clear it.
10226
10227 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10228 suitable buffer from echo_buffer[] and clear it.
10229
10230 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10231 that the current message becomes the last displayed one, make
10232 choose a suitable buffer for echo_area_buffer[0], and clear it.
10233
10234 Value is what FN returns. */
10235
10236 static int
10237 with_echo_area_buffer (struct window *w, int which,
10238 int (*fn) (ptrdiff_t, Lisp_Object),
10239 ptrdiff_t a1, Lisp_Object a2)
10240 {
10241 Lisp_Object buffer;
10242 int this_one, the_other, clear_buffer_p, rc;
10243 ptrdiff_t count = SPECPDL_INDEX ();
10244
10245 /* If buffers aren't live, make new ones. */
10246 ensure_echo_area_buffers ();
10247
10248 clear_buffer_p = 0;
10249
10250 if (which == 0)
10251 this_one = 0, the_other = 1;
10252 else if (which > 0)
10253 this_one = 1, the_other = 0;
10254 else
10255 {
10256 this_one = 0, the_other = 1;
10257 clear_buffer_p = true;
10258
10259 /* We need a fresh one in case the current echo buffer equals
10260 the one containing the last displayed echo area message. */
10261 if (!NILP (echo_area_buffer[this_one])
10262 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10263 echo_area_buffer[this_one] = Qnil;
10264 }
10265
10266 /* Choose a suitable buffer from echo_buffer[] is we don't
10267 have one. */
10268 if (NILP (echo_area_buffer[this_one]))
10269 {
10270 echo_area_buffer[this_one]
10271 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10272 ? echo_buffer[the_other]
10273 : echo_buffer[this_one]);
10274 clear_buffer_p = true;
10275 }
10276
10277 buffer = echo_area_buffer[this_one];
10278
10279 /* Don't get confused by reusing the buffer used for echoing
10280 for a different purpose. */
10281 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10282 cancel_echoing ();
10283
10284 record_unwind_protect (unwind_with_echo_area_buffer,
10285 with_echo_area_buffer_unwind_data (w));
10286
10287 /* Make the echo area buffer current. Note that for display
10288 purposes, it is not necessary that the displayed window's buffer
10289 == current_buffer, except for text property lookup. So, let's
10290 only set that buffer temporarily here without doing a full
10291 Fset_window_buffer. We must also change w->pointm, though,
10292 because otherwise an assertions in unshow_buffer fails, and Emacs
10293 aborts. */
10294 set_buffer_internal_1 (XBUFFER (buffer));
10295 if (w)
10296 {
10297 wset_buffer (w, buffer);
10298 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10299 }
10300
10301 bset_undo_list (current_buffer, Qt);
10302 bset_read_only (current_buffer, Qnil);
10303 specbind (Qinhibit_read_only, Qt);
10304 specbind (Qinhibit_modification_hooks, Qt);
10305
10306 if (clear_buffer_p && Z > BEG)
10307 del_range (BEG, Z);
10308
10309 eassert (BEGV >= BEG);
10310 eassert (ZV <= Z && ZV >= BEGV);
10311
10312 rc = fn (a1, a2);
10313
10314 eassert (BEGV >= BEG);
10315 eassert (ZV <= Z && ZV >= BEGV);
10316
10317 unbind_to (count, Qnil);
10318 return rc;
10319 }
10320
10321
10322 /* Save state that should be preserved around the call to the function
10323 FN called in with_echo_area_buffer. */
10324
10325 static Lisp_Object
10326 with_echo_area_buffer_unwind_data (struct window *w)
10327 {
10328 int i = 0;
10329 Lisp_Object vector, tmp;
10330
10331 /* Reduce consing by keeping one vector in
10332 Vwith_echo_area_save_vector. */
10333 vector = Vwith_echo_area_save_vector;
10334 Vwith_echo_area_save_vector = Qnil;
10335
10336 if (NILP (vector))
10337 vector = Fmake_vector (make_number (9), Qnil);
10338
10339 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10340 ASET (vector, i, Vdeactivate_mark); ++i;
10341 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10342
10343 if (w)
10344 {
10345 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10346 ASET (vector, i, w->contents); ++i;
10347 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10348 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10349 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10350 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10351 }
10352 else
10353 {
10354 int end = i + 6;
10355 for (; i < end; ++i)
10356 ASET (vector, i, Qnil);
10357 }
10358
10359 eassert (i == ASIZE (vector));
10360 return vector;
10361 }
10362
10363
10364 /* Restore global state from VECTOR which was created by
10365 with_echo_area_buffer_unwind_data. */
10366
10367 static void
10368 unwind_with_echo_area_buffer (Lisp_Object vector)
10369 {
10370 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10371 Vdeactivate_mark = AREF (vector, 1);
10372 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10373
10374 if (WINDOWP (AREF (vector, 3)))
10375 {
10376 struct window *w;
10377 Lisp_Object buffer;
10378
10379 w = XWINDOW (AREF (vector, 3));
10380 buffer = AREF (vector, 4);
10381
10382 wset_buffer (w, buffer);
10383 set_marker_both (w->pointm, buffer,
10384 XFASTINT (AREF (vector, 5)),
10385 XFASTINT (AREF (vector, 6)));
10386 set_marker_both (w->start, buffer,
10387 XFASTINT (AREF (vector, 7)),
10388 XFASTINT (AREF (vector, 8)));
10389 }
10390
10391 Vwith_echo_area_save_vector = vector;
10392 }
10393
10394
10395 /* Set up the echo area for use by print functions. MULTIBYTE_P
10396 non-zero means we will print multibyte. */
10397
10398 void
10399 setup_echo_area_for_printing (int multibyte_p)
10400 {
10401 /* If we can't find an echo area any more, exit. */
10402 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10403 Fkill_emacs (Qnil);
10404
10405 ensure_echo_area_buffers ();
10406
10407 if (!message_buf_print)
10408 {
10409 /* A message has been output since the last time we printed.
10410 Choose a fresh echo area buffer. */
10411 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10412 echo_area_buffer[0] = echo_buffer[1];
10413 else
10414 echo_area_buffer[0] = echo_buffer[0];
10415
10416 /* Switch to that buffer and clear it. */
10417 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10418 bset_truncate_lines (current_buffer, Qnil);
10419
10420 if (Z > BEG)
10421 {
10422 ptrdiff_t count = SPECPDL_INDEX ();
10423 specbind (Qinhibit_read_only, Qt);
10424 /* Note that undo recording is always disabled. */
10425 del_range (BEG, Z);
10426 unbind_to (count, Qnil);
10427 }
10428 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10429
10430 /* Set up the buffer for the multibyteness we need. */
10431 if (multibyte_p
10432 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10433 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10434
10435 /* Raise the frame containing the echo area. */
10436 if (minibuffer_auto_raise)
10437 {
10438 struct frame *sf = SELECTED_FRAME ();
10439 Lisp_Object mini_window;
10440 mini_window = FRAME_MINIBUF_WINDOW (sf);
10441 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10442 }
10443
10444 message_log_maybe_newline ();
10445 message_buf_print = 1;
10446 }
10447 else
10448 {
10449 if (NILP (echo_area_buffer[0]))
10450 {
10451 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10452 echo_area_buffer[0] = echo_buffer[1];
10453 else
10454 echo_area_buffer[0] = echo_buffer[0];
10455 }
10456
10457 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10458 {
10459 /* Someone switched buffers between print requests. */
10460 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10461 bset_truncate_lines (current_buffer, Qnil);
10462 }
10463 }
10464 }
10465
10466
10467 /* Display an echo area message in window W. Value is non-zero if W's
10468 height is changed. If display_last_displayed_message_p is
10469 non-zero, display the message that was last displayed, otherwise
10470 display the current message. */
10471
10472 static int
10473 display_echo_area (struct window *w)
10474 {
10475 int i, no_message_p, window_height_changed_p;
10476
10477 /* Temporarily disable garbage collections while displaying the echo
10478 area. This is done because a GC can print a message itself.
10479 That message would modify the echo area buffer's contents while a
10480 redisplay of the buffer is going on, and seriously confuse
10481 redisplay. */
10482 ptrdiff_t count = inhibit_garbage_collection ();
10483
10484 /* If there is no message, we must call display_echo_area_1
10485 nevertheless because it resizes the window. But we will have to
10486 reset the echo_area_buffer in question to nil at the end because
10487 with_echo_area_buffer will sets it to an empty buffer. */
10488 i = display_last_displayed_message_p ? 1 : 0;
10489 no_message_p = NILP (echo_area_buffer[i]);
10490
10491 window_height_changed_p
10492 = with_echo_area_buffer (w, display_last_displayed_message_p,
10493 display_echo_area_1,
10494 (intptr_t) w, Qnil);
10495
10496 if (no_message_p)
10497 echo_area_buffer[i] = Qnil;
10498
10499 unbind_to (count, Qnil);
10500 return window_height_changed_p;
10501 }
10502
10503
10504 /* Helper for display_echo_area. Display the current buffer which
10505 contains the current echo area message in window W, a mini-window,
10506 a pointer to which is passed in A1. A2..A4 are currently not used.
10507 Change the height of W so that all of the message is displayed.
10508 Value is non-zero if height of W was changed. */
10509
10510 static int
10511 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10512 {
10513 intptr_t i1 = a1;
10514 struct window *w = (struct window *) i1;
10515 Lisp_Object window;
10516 struct text_pos start;
10517 int window_height_changed_p = 0;
10518
10519 /* Do this before displaying, so that we have a large enough glyph
10520 matrix for the display. If we can't get enough space for the
10521 whole text, display the last N lines. That works by setting w->start. */
10522 window_height_changed_p = resize_mini_window (w, 0);
10523
10524 /* Use the starting position chosen by resize_mini_window. */
10525 SET_TEXT_POS_FROM_MARKER (start, w->start);
10526
10527 /* Display. */
10528 clear_glyph_matrix (w->desired_matrix);
10529 XSETWINDOW (window, w);
10530 try_window (window, start, 0);
10531
10532 return window_height_changed_p;
10533 }
10534
10535
10536 /* Resize the echo area window to exactly the size needed for the
10537 currently displayed message, if there is one. If a mini-buffer
10538 is active, don't shrink it. */
10539
10540 void
10541 resize_echo_area_exactly (void)
10542 {
10543 if (BUFFERP (echo_area_buffer[0])
10544 && WINDOWP (echo_area_window))
10545 {
10546 struct window *w = XWINDOW (echo_area_window);
10547 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10548 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10549 (intptr_t) w, resize_exactly);
10550 if (resized_p)
10551 {
10552 windows_or_buffers_changed = 42;
10553 update_mode_lines = 30;
10554 redisplay_internal ();
10555 }
10556 }
10557 }
10558
10559
10560 /* Callback function for with_echo_area_buffer, when used from
10561 resize_echo_area_exactly. A1 contains a pointer to the window to
10562 resize, EXACTLY non-nil means resize the mini-window exactly to the
10563 size of the text displayed. A3 and A4 are not used. Value is what
10564 resize_mini_window returns. */
10565
10566 static int
10567 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10568 {
10569 intptr_t i1 = a1;
10570 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10571 }
10572
10573
10574 /* Resize mini-window W to fit the size of its contents. EXACT_P
10575 means size the window exactly to the size needed. Otherwise, it's
10576 only enlarged until W's buffer is empty.
10577
10578 Set W->start to the right place to begin display. If the whole
10579 contents fit, start at the beginning. Otherwise, start so as
10580 to make the end of the contents appear. This is particularly
10581 important for y-or-n-p, but seems desirable generally.
10582
10583 Value is non-zero if the window height has been changed. */
10584
10585 int
10586 resize_mini_window (struct window *w, int exact_p)
10587 {
10588 struct frame *f = XFRAME (w->frame);
10589 int window_height_changed_p = 0;
10590
10591 eassert (MINI_WINDOW_P (w));
10592
10593 /* By default, start display at the beginning. */
10594 set_marker_both (w->start, w->contents,
10595 BUF_BEGV (XBUFFER (w->contents)),
10596 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10597
10598 /* Don't resize windows while redisplaying a window; it would
10599 confuse redisplay functions when the size of the window they are
10600 displaying changes from under them. Such a resizing can happen,
10601 for instance, when which-func prints a long message while
10602 we are running fontification-functions. We're running these
10603 functions with safe_call which binds inhibit-redisplay to t. */
10604 if (!NILP (Vinhibit_redisplay))
10605 return 0;
10606
10607 /* Nil means don't try to resize. */
10608 if (NILP (Vresize_mini_windows)
10609 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10610 return 0;
10611
10612 if (!FRAME_MINIBUF_ONLY_P (f))
10613 {
10614 struct it it;
10615 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10616 + WINDOW_PIXEL_HEIGHT (w));
10617 int unit = FRAME_LINE_HEIGHT (f);
10618 int height, max_height;
10619 struct text_pos start;
10620 struct buffer *old_current_buffer = NULL;
10621
10622 if (current_buffer != XBUFFER (w->contents))
10623 {
10624 old_current_buffer = current_buffer;
10625 set_buffer_internal (XBUFFER (w->contents));
10626 }
10627
10628 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10629
10630 /* Compute the max. number of lines specified by the user. */
10631 if (FLOATP (Vmax_mini_window_height))
10632 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10633 else if (INTEGERP (Vmax_mini_window_height))
10634 max_height = XINT (Vmax_mini_window_height) * unit;
10635 else
10636 max_height = total_height / 4;
10637
10638 /* Correct that max. height if it's bogus. */
10639 max_height = clip_to_bounds (unit, max_height, total_height);
10640
10641 /* Find out the height of the text in the window. */
10642 if (it.line_wrap == TRUNCATE)
10643 height = unit;
10644 else
10645 {
10646 last_height = 0;
10647 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10648 if (it.max_ascent == 0 && it.max_descent == 0)
10649 height = it.current_y + last_height;
10650 else
10651 height = it.current_y + it.max_ascent + it.max_descent;
10652 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10653 }
10654
10655 /* Compute a suitable window start. */
10656 if (height > max_height)
10657 {
10658 height = (max_height / unit) * unit;
10659 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10660 move_it_vertically_backward (&it, height - unit);
10661 start = it.current.pos;
10662 }
10663 else
10664 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10665 SET_MARKER_FROM_TEXT_POS (w->start, start);
10666
10667 if (EQ (Vresize_mini_windows, Qgrow_only))
10668 {
10669 /* Let it grow only, until we display an empty message, in which
10670 case the window shrinks again. */
10671 if (height > WINDOW_PIXEL_HEIGHT (w))
10672 {
10673 int old_height = WINDOW_PIXEL_HEIGHT (w);
10674
10675 FRAME_WINDOWS_FROZEN (f) = 1;
10676 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10677 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10678 }
10679 else if (height < WINDOW_PIXEL_HEIGHT (w)
10680 && (exact_p || BEGV == ZV))
10681 {
10682 int old_height = WINDOW_PIXEL_HEIGHT (w);
10683
10684 FRAME_WINDOWS_FROZEN (f) = 0;
10685 shrink_mini_window (w, 1);
10686 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10687 }
10688 }
10689 else
10690 {
10691 /* Always resize to exact size needed. */
10692 if (height > WINDOW_PIXEL_HEIGHT (w))
10693 {
10694 int old_height = WINDOW_PIXEL_HEIGHT (w);
10695
10696 FRAME_WINDOWS_FROZEN (f) = 1;
10697 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10698 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10699 }
10700 else if (height < WINDOW_PIXEL_HEIGHT (w))
10701 {
10702 int old_height = WINDOW_PIXEL_HEIGHT (w);
10703
10704 FRAME_WINDOWS_FROZEN (f) = 0;
10705 shrink_mini_window (w, 1);
10706
10707 if (height)
10708 {
10709 FRAME_WINDOWS_FROZEN (f) = 1;
10710 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10711 }
10712
10713 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10714 }
10715 }
10716
10717 if (old_current_buffer)
10718 set_buffer_internal (old_current_buffer);
10719 }
10720
10721 return window_height_changed_p;
10722 }
10723
10724
10725 /* Value is the current message, a string, or nil if there is no
10726 current message. */
10727
10728 Lisp_Object
10729 current_message (void)
10730 {
10731 Lisp_Object msg;
10732
10733 if (!BUFFERP (echo_area_buffer[0]))
10734 msg = Qnil;
10735 else
10736 {
10737 with_echo_area_buffer (0, 0, current_message_1,
10738 (intptr_t) &msg, Qnil);
10739 if (NILP (msg))
10740 echo_area_buffer[0] = Qnil;
10741 }
10742
10743 return msg;
10744 }
10745
10746
10747 static int
10748 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10749 {
10750 intptr_t i1 = a1;
10751 Lisp_Object *msg = (Lisp_Object *) i1;
10752
10753 if (Z > BEG)
10754 *msg = make_buffer_string (BEG, Z, 1);
10755 else
10756 *msg = Qnil;
10757 return 0;
10758 }
10759
10760
10761 /* Push the current message on Vmessage_stack for later restoration
10762 by restore_message. Value is non-zero if the current message isn't
10763 empty. This is a relatively infrequent operation, so it's not
10764 worth optimizing. */
10765
10766 bool
10767 push_message (void)
10768 {
10769 Lisp_Object msg = current_message ();
10770 Vmessage_stack = Fcons (msg, Vmessage_stack);
10771 return STRINGP (msg);
10772 }
10773
10774
10775 /* Restore message display from the top of Vmessage_stack. */
10776
10777 void
10778 restore_message (void)
10779 {
10780 eassert (CONSP (Vmessage_stack));
10781 message3_nolog (XCAR (Vmessage_stack));
10782 }
10783
10784
10785 /* Handler for unwind-protect calling pop_message. */
10786
10787 void
10788 pop_message_unwind (void)
10789 {
10790 /* Pop the top-most entry off Vmessage_stack. */
10791 eassert (CONSP (Vmessage_stack));
10792 Vmessage_stack = XCDR (Vmessage_stack);
10793 }
10794
10795
10796 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10797 exits. If the stack is not empty, we have a missing pop_message
10798 somewhere. */
10799
10800 void
10801 check_message_stack (void)
10802 {
10803 if (!NILP (Vmessage_stack))
10804 emacs_abort ();
10805 }
10806
10807
10808 /* Truncate to NCHARS what will be displayed in the echo area the next
10809 time we display it---but don't redisplay it now. */
10810
10811 void
10812 truncate_echo_area (ptrdiff_t nchars)
10813 {
10814 if (nchars == 0)
10815 echo_area_buffer[0] = Qnil;
10816 else if (!noninteractive
10817 && INTERACTIVE
10818 && !NILP (echo_area_buffer[0]))
10819 {
10820 struct frame *sf = SELECTED_FRAME ();
10821 /* Error messages get reported properly by cmd_error, so this must be
10822 just an informative message; if the frame hasn't really been
10823 initialized yet, just toss it. */
10824 if (sf->glyphs_initialized_p)
10825 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10826 }
10827 }
10828
10829
10830 /* Helper function for truncate_echo_area. Truncate the current
10831 message to at most NCHARS characters. */
10832
10833 static int
10834 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10835 {
10836 if (BEG + nchars < Z)
10837 del_range (BEG + nchars, Z);
10838 if (Z == BEG)
10839 echo_area_buffer[0] = Qnil;
10840 return 0;
10841 }
10842
10843 /* Set the current message to STRING. */
10844
10845 static void
10846 set_message (Lisp_Object string)
10847 {
10848 eassert (STRINGP (string));
10849
10850 message_enable_multibyte = STRING_MULTIBYTE (string);
10851
10852 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10853 message_buf_print = 0;
10854 help_echo_showing_p = 0;
10855
10856 if (STRINGP (Vdebug_on_message)
10857 && STRINGP (string)
10858 && fast_string_match (Vdebug_on_message, string) >= 0)
10859 call_debugger (list2 (Qerror, string));
10860 }
10861
10862
10863 /* Helper function for set_message. First argument is ignored and second
10864 argument has the same meaning as for set_message.
10865 This function is called with the echo area buffer being current. */
10866
10867 static int
10868 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10869 {
10870 eassert (STRINGP (string));
10871
10872 /* Change multibyteness of the echo buffer appropriately. */
10873 if (message_enable_multibyte
10874 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10875 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10876
10877 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10878 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10879 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10880
10881 /* Insert new message at BEG. */
10882 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10883
10884 /* This function takes care of single/multibyte conversion.
10885 We just have to ensure that the echo area buffer has the right
10886 setting of enable_multibyte_characters. */
10887 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10888
10889 return 0;
10890 }
10891
10892
10893 /* Clear messages. CURRENT_P non-zero means clear the current
10894 message. LAST_DISPLAYED_P non-zero means clear the message
10895 last displayed. */
10896
10897 void
10898 clear_message (bool current_p, bool last_displayed_p)
10899 {
10900 if (current_p)
10901 {
10902 echo_area_buffer[0] = Qnil;
10903 message_cleared_p = true;
10904 }
10905
10906 if (last_displayed_p)
10907 echo_area_buffer[1] = Qnil;
10908
10909 message_buf_print = 0;
10910 }
10911
10912 /* Clear garbaged frames.
10913
10914 This function is used where the old redisplay called
10915 redraw_garbaged_frames which in turn called redraw_frame which in
10916 turn called clear_frame. The call to clear_frame was a source of
10917 flickering. I believe a clear_frame is not necessary. It should
10918 suffice in the new redisplay to invalidate all current matrices,
10919 and ensure a complete redisplay of all windows. */
10920
10921 static void
10922 clear_garbaged_frames (void)
10923 {
10924 if (frame_garbaged)
10925 {
10926 Lisp_Object tail, frame;
10927
10928 FOR_EACH_FRAME (tail, frame)
10929 {
10930 struct frame *f = XFRAME (frame);
10931
10932 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10933 {
10934 if (f->resized_p)
10935 redraw_frame (f);
10936 else
10937 clear_current_matrices (f);
10938 fset_redisplay (f);
10939 f->garbaged = false;
10940 f->resized_p = false;
10941 }
10942 }
10943
10944 frame_garbaged = false;
10945 }
10946 }
10947
10948
10949 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10950 is non-zero update selected_frame. Value is non-zero if the
10951 mini-windows height has been changed. */
10952
10953 static int
10954 echo_area_display (int update_frame_p)
10955 {
10956 Lisp_Object mini_window;
10957 struct window *w;
10958 struct frame *f;
10959 int window_height_changed_p = 0;
10960 struct frame *sf = SELECTED_FRAME ();
10961
10962 mini_window = FRAME_MINIBUF_WINDOW (sf);
10963 w = XWINDOW (mini_window);
10964 f = XFRAME (WINDOW_FRAME (w));
10965
10966 /* Don't display if frame is invisible or not yet initialized. */
10967 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10968 return 0;
10969
10970 #ifdef HAVE_WINDOW_SYSTEM
10971 /* When Emacs starts, selected_frame may be the initial terminal
10972 frame. If we let this through, a message would be displayed on
10973 the terminal. */
10974 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10975 return 0;
10976 #endif /* HAVE_WINDOW_SYSTEM */
10977
10978 /* Redraw garbaged frames. */
10979 clear_garbaged_frames ();
10980
10981 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10982 {
10983 echo_area_window = mini_window;
10984 window_height_changed_p = display_echo_area (w);
10985 w->must_be_updated_p = true;
10986
10987 /* Update the display, unless called from redisplay_internal.
10988 Also don't update the screen during redisplay itself. The
10989 update will happen at the end of redisplay, and an update
10990 here could cause confusion. */
10991 if (update_frame_p && !redisplaying_p)
10992 {
10993 int n = 0;
10994
10995 /* If the display update has been interrupted by pending
10996 input, update mode lines in the frame. Due to the
10997 pending input, it might have been that redisplay hasn't
10998 been called, so that mode lines above the echo area are
10999 garbaged. This looks odd, so we prevent it here. */
11000 if (!display_completed)
11001 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11002
11003 if (window_height_changed_p
11004 /* Don't do this if Emacs is shutting down. Redisplay
11005 needs to run hooks. */
11006 && !NILP (Vrun_hooks))
11007 {
11008 /* Must update other windows. Likewise as in other
11009 cases, don't let this update be interrupted by
11010 pending input. */
11011 ptrdiff_t count = SPECPDL_INDEX ();
11012 specbind (Qredisplay_dont_pause, Qt);
11013 windows_or_buffers_changed = 44;
11014 redisplay_internal ();
11015 unbind_to (count, Qnil);
11016 }
11017 else if (FRAME_WINDOW_P (f) && n == 0)
11018 {
11019 /* Window configuration is the same as before.
11020 Can do with a display update of the echo area,
11021 unless we displayed some mode lines. */
11022 update_single_window (w, 1);
11023 flush_frame (f);
11024 }
11025 else
11026 update_frame (f, 1, 1);
11027
11028 /* If cursor is in the echo area, make sure that the next
11029 redisplay displays the minibuffer, so that the cursor will
11030 be replaced with what the minibuffer wants. */
11031 if (cursor_in_echo_area)
11032 wset_redisplay (XWINDOW (mini_window));
11033 }
11034 }
11035 else if (!EQ (mini_window, selected_window))
11036 wset_redisplay (XWINDOW (mini_window));
11037
11038 /* Last displayed message is now the current message. */
11039 echo_area_buffer[1] = echo_area_buffer[0];
11040 /* Inform read_char that we're not echoing. */
11041 echo_message_buffer = Qnil;
11042
11043 /* Prevent redisplay optimization in redisplay_internal by resetting
11044 this_line_start_pos. This is done because the mini-buffer now
11045 displays the message instead of its buffer text. */
11046 if (EQ (mini_window, selected_window))
11047 CHARPOS (this_line_start_pos) = 0;
11048
11049 return window_height_changed_p;
11050 }
11051
11052 /* Nonzero if W's buffer was changed but not saved. */
11053
11054 static int
11055 window_buffer_changed (struct window *w)
11056 {
11057 struct buffer *b = XBUFFER (w->contents);
11058
11059 eassert (BUFFER_LIVE_P (b));
11060
11061 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11062 }
11063
11064 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11065
11066 static int
11067 mode_line_update_needed (struct window *w)
11068 {
11069 return (w->column_number_displayed != -1
11070 && !(PT == w->last_point && !window_outdated (w))
11071 && (w->column_number_displayed != current_column ()));
11072 }
11073
11074 /* Nonzero if window start of W is frozen and may not be changed during
11075 redisplay. */
11076
11077 static bool
11078 window_frozen_p (struct window *w)
11079 {
11080 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11081 {
11082 Lisp_Object window;
11083
11084 XSETWINDOW (window, w);
11085 if (MINI_WINDOW_P (w))
11086 return 0;
11087 else if (EQ (window, selected_window))
11088 return 0;
11089 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11090 && EQ (window, Vminibuf_scroll_window))
11091 /* This special window can't be frozen too. */
11092 return 0;
11093 else
11094 return 1;
11095 }
11096 return 0;
11097 }
11098
11099 /***********************************************************************
11100 Mode Lines and Frame Titles
11101 ***********************************************************************/
11102
11103 /* A buffer for constructing non-propertized mode-line strings and
11104 frame titles in it; allocated from the heap in init_xdisp and
11105 resized as needed in store_mode_line_noprop_char. */
11106
11107 static char *mode_line_noprop_buf;
11108
11109 /* The buffer's end, and a current output position in it. */
11110
11111 static char *mode_line_noprop_buf_end;
11112 static char *mode_line_noprop_ptr;
11113
11114 #define MODE_LINE_NOPROP_LEN(start) \
11115 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11116
11117 static enum {
11118 MODE_LINE_DISPLAY = 0,
11119 MODE_LINE_TITLE,
11120 MODE_LINE_NOPROP,
11121 MODE_LINE_STRING
11122 } mode_line_target;
11123
11124 /* Alist that caches the results of :propertize.
11125 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11126 static Lisp_Object mode_line_proptrans_alist;
11127
11128 /* List of strings making up the mode-line. */
11129 static Lisp_Object mode_line_string_list;
11130
11131 /* Base face property when building propertized mode line string. */
11132 static Lisp_Object mode_line_string_face;
11133 static Lisp_Object mode_line_string_face_prop;
11134
11135
11136 /* Unwind data for mode line strings */
11137
11138 static Lisp_Object Vmode_line_unwind_vector;
11139
11140 static Lisp_Object
11141 format_mode_line_unwind_data (struct frame *target_frame,
11142 struct buffer *obuf,
11143 Lisp_Object owin,
11144 int save_proptrans)
11145 {
11146 Lisp_Object vector, tmp;
11147
11148 /* Reduce consing by keeping one vector in
11149 Vwith_echo_area_save_vector. */
11150 vector = Vmode_line_unwind_vector;
11151 Vmode_line_unwind_vector = Qnil;
11152
11153 if (NILP (vector))
11154 vector = Fmake_vector (make_number (10), Qnil);
11155
11156 ASET (vector, 0, make_number (mode_line_target));
11157 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11158 ASET (vector, 2, mode_line_string_list);
11159 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11160 ASET (vector, 4, mode_line_string_face);
11161 ASET (vector, 5, mode_line_string_face_prop);
11162
11163 if (obuf)
11164 XSETBUFFER (tmp, obuf);
11165 else
11166 tmp = Qnil;
11167 ASET (vector, 6, tmp);
11168 ASET (vector, 7, owin);
11169 if (target_frame)
11170 {
11171 /* Similarly to `with-selected-window', if the operation selects
11172 a window on another frame, we must restore that frame's
11173 selected window, and (for a tty) the top-frame. */
11174 ASET (vector, 8, target_frame->selected_window);
11175 if (FRAME_TERMCAP_P (target_frame))
11176 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11177 }
11178
11179 return vector;
11180 }
11181
11182 static void
11183 unwind_format_mode_line (Lisp_Object vector)
11184 {
11185 Lisp_Object old_window = AREF (vector, 7);
11186 Lisp_Object target_frame_window = AREF (vector, 8);
11187 Lisp_Object old_top_frame = AREF (vector, 9);
11188
11189 mode_line_target = XINT (AREF (vector, 0));
11190 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11191 mode_line_string_list = AREF (vector, 2);
11192 if (! EQ (AREF (vector, 3), Qt))
11193 mode_line_proptrans_alist = AREF (vector, 3);
11194 mode_line_string_face = AREF (vector, 4);
11195 mode_line_string_face_prop = AREF (vector, 5);
11196
11197 /* Select window before buffer, since it may change the buffer. */
11198 if (!NILP (old_window))
11199 {
11200 /* If the operation that we are unwinding had selected a window
11201 on a different frame, reset its frame-selected-window. For a
11202 text terminal, reset its top-frame if necessary. */
11203 if (!NILP (target_frame_window))
11204 {
11205 Lisp_Object frame
11206 = WINDOW_FRAME (XWINDOW (target_frame_window));
11207
11208 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11209 Fselect_window (target_frame_window, Qt);
11210
11211 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11212 Fselect_frame (old_top_frame, Qt);
11213 }
11214
11215 Fselect_window (old_window, Qt);
11216 }
11217
11218 if (!NILP (AREF (vector, 6)))
11219 {
11220 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11221 ASET (vector, 6, Qnil);
11222 }
11223
11224 Vmode_line_unwind_vector = vector;
11225 }
11226
11227
11228 /* Store a single character C for the frame title in mode_line_noprop_buf.
11229 Re-allocate mode_line_noprop_buf if necessary. */
11230
11231 static void
11232 store_mode_line_noprop_char (char c)
11233 {
11234 /* If output position has reached the end of the allocated buffer,
11235 increase the buffer's size. */
11236 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11237 {
11238 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11239 ptrdiff_t size = len;
11240 mode_line_noprop_buf =
11241 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11242 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11243 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11244 }
11245
11246 *mode_line_noprop_ptr++ = c;
11247 }
11248
11249
11250 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11251 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11252 characters that yield more columns than PRECISION; PRECISION <= 0
11253 means copy the whole string. Pad with spaces until FIELD_WIDTH
11254 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11255 pad. Called from display_mode_element when it is used to build a
11256 frame title. */
11257
11258 static int
11259 store_mode_line_noprop (const char *string, int field_width, int precision)
11260 {
11261 const unsigned char *str = (const unsigned char *) string;
11262 int n = 0;
11263 ptrdiff_t dummy, nbytes;
11264
11265 /* Copy at most PRECISION chars from STR. */
11266 nbytes = strlen (string);
11267 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11268 while (nbytes--)
11269 store_mode_line_noprop_char (*str++);
11270
11271 /* Fill up with spaces until FIELD_WIDTH reached. */
11272 while (field_width > 0
11273 && n < field_width)
11274 {
11275 store_mode_line_noprop_char (' ');
11276 ++n;
11277 }
11278
11279 return n;
11280 }
11281
11282 /***********************************************************************
11283 Frame Titles
11284 ***********************************************************************/
11285
11286 #ifdef HAVE_WINDOW_SYSTEM
11287
11288 /* Set the title of FRAME, if it has changed. The title format is
11289 Vicon_title_format if FRAME is iconified, otherwise it is
11290 frame_title_format. */
11291
11292 static void
11293 x_consider_frame_title (Lisp_Object frame)
11294 {
11295 struct frame *f = XFRAME (frame);
11296
11297 if (FRAME_WINDOW_P (f)
11298 || FRAME_MINIBUF_ONLY_P (f)
11299 || f->explicit_name)
11300 {
11301 /* Do we have more than one visible frame on this X display? */
11302 Lisp_Object tail, other_frame, fmt;
11303 ptrdiff_t title_start;
11304 char *title;
11305 ptrdiff_t len;
11306 struct it it;
11307 ptrdiff_t count = SPECPDL_INDEX ();
11308
11309 FOR_EACH_FRAME (tail, other_frame)
11310 {
11311 struct frame *tf = XFRAME (other_frame);
11312
11313 if (tf != f
11314 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11315 && !FRAME_MINIBUF_ONLY_P (tf)
11316 && !EQ (other_frame, tip_frame)
11317 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11318 break;
11319 }
11320
11321 /* Set global variable indicating that multiple frames exist. */
11322 multiple_frames = CONSP (tail);
11323
11324 /* Switch to the buffer of selected window of the frame. Set up
11325 mode_line_target so that display_mode_element will output into
11326 mode_line_noprop_buf; then display the title. */
11327 record_unwind_protect (unwind_format_mode_line,
11328 format_mode_line_unwind_data
11329 (f, current_buffer, selected_window, 0));
11330
11331 Fselect_window (f->selected_window, Qt);
11332 set_buffer_internal_1
11333 (XBUFFER (XWINDOW (f->selected_window)->contents));
11334 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11335
11336 mode_line_target = MODE_LINE_TITLE;
11337 title_start = MODE_LINE_NOPROP_LEN (0);
11338 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11339 NULL, DEFAULT_FACE_ID);
11340 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11341 len = MODE_LINE_NOPROP_LEN (title_start);
11342 title = mode_line_noprop_buf + title_start;
11343 unbind_to (count, Qnil);
11344
11345 /* Set the title only if it's changed. This avoids consing in
11346 the common case where it hasn't. (If it turns out that we've
11347 already wasted too much time by walking through the list with
11348 display_mode_element, then we might need to optimize at a
11349 higher level than this.) */
11350 if (! STRINGP (f->name)
11351 || SBYTES (f->name) != len
11352 || memcmp (title, SDATA (f->name), len) != 0)
11353 x_implicitly_set_name (f, make_string (title, len), Qnil);
11354 }
11355 }
11356
11357 #endif /* not HAVE_WINDOW_SYSTEM */
11358
11359 \f
11360 /***********************************************************************
11361 Menu Bars
11362 ***********************************************************************/
11363
11364 /* Non-zero if we will not redisplay all visible windows. */
11365 #define REDISPLAY_SOME_P() \
11366 ((windows_or_buffers_changed == 0 \
11367 || windows_or_buffers_changed == REDISPLAY_SOME) \
11368 && (update_mode_lines == 0 \
11369 || update_mode_lines == REDISPLAY_SOME))
11370
11371 /* Prepare for redisplay by updating menu-bar item lists when
11372 appropriate. This can call eval. */
11373
11374 static void
11375 prepare_menu_bars (void)
11376 {
11377 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11378 bool some_windows = REDISPLAY_SOME_P ();
11379 struct gcpro gcpro1, gcpro2;
11380 Lisp_Object tooltip_frame;
11381
11382 #ifdef HAVE_WINDOW_SYSTEM
11383 tooltip_frame = tip_frame;
11384 #else
11385 tooltip_frame = Qnil;
11386 #endif
11387
11388 if (FUNCTIONP (Vpre_redisplay_function))
11389 {
11390 Lisp_Object windows = all_windows ? Qt : Qnil;
11391 if (all_windows && some_windows)
11392 {
11393 Lisp_Object ws = window_list ();
11394 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11395 {
11396 Lisp_Object this = XCAR (ws);
11397 struct window *w = XWINDOW (this);
11398 if (w->redisplay
11399 || XFRAME (w->frame)->redisplay
11400 || XBUFFER (w->contents)->text->redisplay)
11401 {
11402 windows = Fcons (this, windows);
11403 }
11404 }
11405 }
11406 safe_call1 (Vpre_redisplay_function, windows);
11407 }
11408
11409 /* Update all frame titles based on their buffer names, etc. We do
11410 this before the menu bars so that the buffer-menu will show the
11411 up-to-date frame titles. */
11412 #ifdef HAVE_WINDOW_SYSTEM
11413 if (all_windows)
11414 {
11415 Lisp_Object tail, frame;
11416
11417 FOR_EACH_FRAME (tail, frame)
11418 {
11419 struct frame *f = XFRAME (frame);
11420 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11421 if (some_windows
11422 && !f->redisplay
11423 && !w->redisplay
11424 && !XBUFFER (w->contents)->text->redisplay)
11425 continue;
11426
11427 if (!EQ (frame, tooltip_frame)
11428 && (FRAME_ICONIFIED_P (f)
11429 || FRAME_VISIBLE_P (f) == 1
11430 /* Exclude TTY frames that are obscured because they
11431 are not the top frame on their console. This is
11432 because x_consider_frame_title actually switches
11433 to the frame, which for TTY frames means it is
11434 marked as garbaged, and will be completely
11435 redrawn on the next redisplay cycle. This causes
11436 TTY frames to be completely redrawn, when there
11437 are more than one of them, even though nothing
11438 should be changed on display. */
11439 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11440 x_consider_frame_title (frame);
11441 }
11442 }
11443 #endif /* HAVE_WINDOW_SYSTEM */
11444
11445 /* Update the menu bar item lists, if appropriate. This has to be
11446 done before any actual redisplay or generation of display lines. */
11447
11448 if (all_windows)
11449 {
11450 Lisp_Object tail, frame;
11451 ptrdiff_t count = SPECPDL_INDEX ();
11452 /* 1 means that update_menu_bar has run its hooks
11453 so any further calls to update_menu_bar shouldn't do so again. */
11454 int menu_bar_hooks_run = 0;
11455
11456 record_unwind_save_match_data ();
11457
11458 FOR_EACH_FRAME (tail, frame)
11459 {
11460 struct frame *f = XFRAME (frame);
11461 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11462
11463 /* Ignore tooltip frame. */
11464 if (EQ (frame, tooltip_frame))
11465 continue;
11466
11467 if (some_windows
11468 && !f->redisplay
11469 && !w->redisplay
11470 && !XBUFFER (w->contents)->text->redisplay)
11471 continue;
11472
11473 /* If a window on this frame changed size, report that to
11474 the user and clear the size-change flag. */
11475 if (FRAME_WINDOW_SIZES_CHANGED (f))
11476 {
11477 Lisp_Object functions;
11478
11479 /* Clear flag first in case we get an error below. */
11480 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11481 functions = Vwindow_size_change_functions;
11482 GCPRO2 (tail, functions);
11483
11484 while (CONSP (functions))
11485 {
11486 if (!EQ (XCAR (functions), Qt))
11487 call1 (XCAR (functions), frame);
11488 functions = XCDR (functions);
11489 }
11490 UNGCPRO;
11491 }
11492
11493 GCPRO1 (tail);
11494 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11495 #ifdef HAVE_WINDOW_SYSTEM
11496 update_tool_bar (f, 0);
11497 #endif
11498 #ifdef HAVE_NS
11499 if (windows_or_buffers_changed
11500 && FRAME_NS_P (f))
11501 ns_set_doc_edited
11502 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11503 #endif
11504 UNGCPRO;
11505 }
11506
11507 unbind_to (count, Qnil);
11508 }
11509 else
11510 {
11511 struct frame *sf = SELECTED_FRAME ();
11512 update_menu_bar (sf, 1, 0);
11513 #ifdef HAVE_WINDOW_SYSTEM
11514 update_tool_bar (sf, 1);
11515 #endif
11516 }
11517 }
11518
11519
11520 /* Update the menu bar item list for frame F. This has to be done
11521 before we start to fill in any display lines, because it can call
11522 eval.
11523
11524 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11525
11526 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11527 already ran the menu bar hooks for this redisplay, so there
11528 is no need to run them again. The return value is the
11529 updated value of this flag, to pass to the next call. */
11530
11531 static int
11532 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11533 {
11534 Lisp_Object window;
11535 register struct window *w;
11536
11537 /* If called recursively during a menu update, do nothing. This can
11538 happen when, for instance, an activate-menubar-hook causes a
11539 redisplay. */
11540 if (inhibit_menubar_update)
11541 return hooks_run;
11542
11543 window = FRAME_SELECTED_WINDOW (f);
11544 w = XWINDOW (window);
11545
11546 if (FRAME_WINDOW_P (f)
11547 ?
11548 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11549 || defined (HAVE_NS) || defined (USE_GTK)
11550 FRAME_EXTERNAL_MENU_BAR (f)
11551 #else
11552 FRAME_MENU_BAR_LINES (f) > 0
11553 #endif
11554 : FRAME_MENU_BAR_LINES (f) > 0)
11555 {
11556 /* If the user has switched buffers or windows, we need to
11557 recompute to reflect the new bindings. But we'll
11558 recompute when update_mode_lines is set too; that means
11559 that people can use force-mode-line-update to request
11560 that the menu bar be recomputed. The adverse effect on
11561 the rest of the redisplay algorithm is about the same as
11562 windows_or_buffers_changed anyway. */
11563 if (windows_or_buffers_changed
11564 /* This used to test w->update_mode_line, but we believe
11565 there is no need to recompute the menu in that case. */
11566 || update_mode_lines
11567 || window_buffer_changed (w))
11568 {
11569 struct buffer *prev = current_buffer;
11570 ptrdiff_t count = SPECPDL_INDEX ();
11571
11572 specbind (Qinhibit_menubar_update, Qt);
11573
11574 set_buffer_internal_1 (XBUFFER (w->contents));
11575 if (save_match_data)
11576 record_unwind_save_match_data ();
11577 if (NILP (Voverriding_local_map_menu_flag))
11578 {
11579 specbind (Qoverriding_terminal_local_map, Qnil);
11580 specbind (Qoverriding_local_map, Qnil);
11581 }
11582
11583 if (!hooks_run)
11584 {
11585 /* Run the Lucid hook. */
11586 safe_run_hooks (Qactivate_menubar_hook);
11587
11588 /* If it has changed current-menubar from previous value,
11589 really recompute the menu-bar from the value. */
11590 if (! NILP (Vlucid_menu_bar_dirty_flag))
11591 call0 (Qrecompute_lucid_menubar);
11592
11593 safe_run_hooks (Qmenu_bar_update_hook);
11594
11595 hooks_run = 1;
11596 }
11597
11598 XSETFRAME (Vmenu_updating_frame, f);
11599 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11600
11601 /* Redisplay the menu bar in case we changed it. */
11602 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11603 || defined (HAVE_NS) || defined (USE_GTK)
11604 if (FRAME_WINDOW_P (f))
11605 {
11606 #if defined (HAVE_NS)
11607 /* All frames on Mac OS share the same menubar. So only
11608 the selected frame should be allowed to set it. */
11609 if (f == SELECTED_FRAME ())
11610 #endif
11611 set_frame_menubar (f, 0, 0);
11612 }
11613 else
11614 /* On a terminal screen, the menu bar is an ordinary screen
11615 line, and this makes it get updated. */
11616 w->update_mode_line = 1;
11617 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11618 /* In the non-toolkit version, the menu bar is an ordinary screen
11619 line, and this makes it get updated. */
11620 w->update_mode_line = 1;
11621 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11622
11623 unbind_to (count, Qnil);
11624 set_buffer_internal_1 (prev);
11625 }
11626 }
11627
11628 return hooks_run;
11629 }
11630
11631 /***********************************************************************
11632 Tool-bars
11633 ***********************************************************************/
11634
11635 #ifdef HAVE_WINDOW_SYSTEM
11636
11637 /* Tool-bar item index of the item on which a mouse button was pressed
11638 or -1. */
11639
11640 int last_tool_bar_item;
11641
11642 /* Select `frame' temporarily without running all the code in
11643 do_switch_frame.
11644 FIXME: Maybe do_switch_frame should be trimmed down similarly
11645 when `norecord' is set. */
11646 static void
11647 fast_set_selected_frame (Lisp_Object frame)
11648 {
11649 if (!EQ (selected_frame, frame))
11650 {
11651 selected_frame = frame;
11652 selected_window = XFRAME (frame)->selected_window;
11653 }
11654 }
11655
11656 /* Update the tool-bar item list for frame F. This has to be done
11657 before we start to fill in any display lines. Called from
11658 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11659 and restore it here. */
11660
11661 static void
11662 update_tool_bar (struct frame *f, int save_match_data)
11663 {
11664 #if defined (USE_GTK) || defined (HAVE_NS)
11665 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11666 #else
11667 int do_update = (WINDOWP (f->tool_bar_window)
11668 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11669 #endif
11670
11671 if (do_update)
11672 {
11673 Lisp_Object window;
11674 struct window *w;
11675
11676 window = FRAME_SELECTED_WINDOW (f);
11677 w = XWINDOW (window);
11678
11679 /* If the user has switched buffers or windows, we need to
11680 recompute to reflect the new bindings. But we'll
11681 recompute when update_mode_lines is set too; that means
11682 that people can use force-mode-line-update to request
11683 that the menu bar be recomputed. The adverse effect on
11684 the rest of the redisplay algorithm is about the same as
11685 windows_or_buffers_changed anyway. */
11686 if (windows_or_buffers_changed
11687 || w->update_mode_line
11688 || update_mode_lines
11689 || window_buffer_changed (w))
11690 {
11691 struct buffer *prev = current_buffer;
11692 ptrdiff_t count = SPECPDL_INDEX ();
11693 Lisp_Object frame, new_tool_bar;
11694 int new_n_tool_bar;
11695 struct gcpro gcpro1;
11696
11697 /* Set current_buffer to the buffer of the selected
11698 window of the frame, so that we get the right local
11699 keymaps. */
11700 set_buffer_internal_1 (XBUFFER (w->contents));
11701
11702 /* Save match data, if we must. */
11703 if (save_match_data)
11704 record_unwind_save_match_data ();
11705
11706 /* Make sure that we don't accidentally use bogus keymaps. */
11707 if (NILP (Voverriding_local_map_menu_flag))
11708 {
11709 specbind (Qoverriding_terminal_local_map, Qnil);
11710 specbind (Qoverriding_local_map, Qnil);
11711 }
11712
11713 GCPRO1 (new_tool_bar);
11714
11715 /* We must temporarily set the selected frame to this frame
11716 before calling tool_bar_items, because the calculation of
11717 the tool-bar keymap uses the selected frame (see
11718 `tool-bar-make-keymap' in tool-bar.el). */
11719 eassert (EQ (selected_window,
11720 /* Since we only explicitly preserve selected_frame,
11721 check that selected_window would be redundant. */
11722 XFRAME (selected_frame)->selected_window));
11723 record_unwind_protect (fast_set_selected_frame, selected_frame);
11724 XSETFRAME (frame, f);
11725 fast_set_selected_frame (frame);
11726
11727 /* Build desired tool-bar items from keymaps. */
11728 new_tool_bar
11729 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11730 &new_n_tool_bar);
11731
11732 /* Redisplay the tool-bar if we changed it. */
11733 if (new_n_tool_bar != f->n_tool_bar_items
11734 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11735 {
11736 /* Redisplay that happens asynchronously due to an expose event
11737 may access f->tool_bar_items. Make sure we update both
11738 variables within BLOCK_INPUT so no such event interrupts. */
11739 block_input ();
11740 fset_tool_bar_items (f, new_tool_bar);
11741 f->n_tool_bar_items = new_n_tool_bar;
11742 w->update_mode_line = 1;
11743 unblock_input ();
11744 }
11745
11746 UNGCPRO;
11747
11748 unbind_to (count, Qnil);
11749 set_buffer_internal_1 (prev);
11750 }
11751 }
11752 }
11753
11754 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11755
11756 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11757 F's desired tool-bar contents. F->tool_bar_items must have
11758 been set up previously by calling prepare_menu_bars. */
11759
11760 static void
11761 build_desired_tool_bar_string (struct frame *f)
11762 {
11763 int i, size, size_needed;
11764 struct gcpro gcpro1, gcpro2, gcpro3;
11765 Lisp_Object image, plist, props;
11766
11767 image = plist = props = Qnil;
11768 GCPRO3 (image, plist, props);
11769
11770 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11771 Otherwise, make a new string. */
11772
11773 /* The size of the string we might be able to reuse. */
11774 size = (STRINGP (f->desired_tool_bar_string)
11775 ? SCHARS (f->desired_tool_bar_string)
11776 : 0);
11777
11778 /* We need one space in the string for each image. */
11779 size_needed = f->n_tool_bar_items;
11780
11781 /* Reuse f->desired_tool_bar_string, if possible. */
11782 if (size < size_needed || NILP (f->desired_tool_bar_string))
11783 fset_desired_tool_bar_string
11784 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11785 else
11786 {
11787 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11788 Fremove_text_properties (make_number (0), make_number (size),
11789 props, f->desired_tool_bar_string);
11790 }
11791
11792 /* Put a `display' property on the string for the images to display,
11793 put a `menu_item' property on tool-bar items with a value that
11794 is the index of the item in F's tool-bar item vector. */
11795 for (i = 0; i < f->n_tool_bar_items; ++i)
11796 {
11797 #define PROP(IDX) \
11798 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11799
11800 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11801 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11802 int hmargin, vmargin, relief, idx, end;
11803
11804 /* If image is a vector, choose the image according to the
11805 button state. */
11806 image = PROP (TOOL_BAR_ITEM_IMAGES);
11807 if (VECTORP (image))
11808 {
11809 if (enabled_p)
11810 idx = (selected_p
11811 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11812 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11813 else
11814 idx = (selected_p
11815 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11816 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11817
11818 eassert (ASIZE (image) >= idx);
11819 image = AREF (image, idx);
11820 }
11821 else
11822 idx = -1;
11823
11824 /* Ignore invalid image specifications. */
11825 if (!valid_image_p (image))
11826 continue;
11827
11828 /* Display the tool-bar button pressed, or depressed. */
11829 plist = Fcopy_sequence (XCDR (image));
11830
11831 /* Compute margin and relief to draw. */
11832 relief = (tool_bar_button_relief >= 0
11833 ? tool_bar_button_relief
11834 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11835 hmargin = vmargin = relief;
11836
11837 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11838 INT_MAX - max (hmargin, vmargin)))
11839 {
11840 hmargin += XFASTINT (Vtool_bar_button_margin);
11841 vmargin += XFASTINT (Vtool_bar_button_margin);
11842 }
11843 else if (CONSP (Vtool_bar_button_margin))
11844 {
11845 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11846 INT_MAX - hmargin))
11847 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11848
11849 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11850 INT_MAX - vmargin))
11851 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11852 }
11853
11854 if (auto_raise_tool_bar_buttons_p)
11855 {
11856 /* Add a `:relief' property to the image spec if the item is
11857 selected. */
11858 if (selected_p)
11859 {
11860 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11861 hmargin -= relief;
11862 vmargin -= relief;
11863 }
11864 }
11865 else
11866 {
11867 /* If image is selected, display it pressed, i.e. with a
11868 negative relief. If it's not selected, display it with a
11869 raised relief. */
11870 plist = Fplist_put (plist, QCrelief,
11871 (selected_p
11872 ? make_number (-relief)
11873 : make_number (relief)));
11874 hmargin -= relief;
11875 vmargin -= relief;
11876 }
11877
11878 /* Put a margin around the image. */
11879 if (hmargin || vmargin)
11880 {
11881 if (hmargin == vmargin)
11882 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11883 else
11884 plist = Fplist_put (plist, QCmargin,
11885 Fcons (make_number (hmargin),
11886 make_number (vmargin)));
11887 }
11888
11889 /* If button is not enabled, and we don't have special images
11890 for the disabled state, make the image appear disabled by
11891 applying an appropriate algorithm to it. */
11892 if (!enabled_p && idx < 0)
11893 plist = Fplist_put (plist, QCconversion, Qdisabled);
11894
11895 /* Put a `display' text property on the string for the image to
11896 display. Put a `menu-item' property on the string that gives
11897 the start of this item's properties in the tool-bar items
11898 vector. */
11899 image = Fcons (Qimage, plist);
11900 props = list4 (Qdisplay, image,
11901 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11902
11903 /* Let the last image hide all remaining spaces in the tool bar
11904 string. The string can be longer than needed when we reuse a
11905 previous string. */
11906 if (i + 1 == f->n_tool_bar_items)
11907 end = SCHARS (f->desired_tool_bar_string);
11908 else
11909 end = i + 1;
11910 Fadd_text_properties (make_number (i), make_number (end),
11911 props, f->desired_tool_bar_string);
11912 #undef PROP
11913 }
11914
11915 UNGCPRO;
11916 }
11917
11918
11919 /* Display one line of the tool-bar of frame IT->f.
11920
11921 HEIGHT specifies the desired height of the tool-bar line.
11922 If the actual height of the glyph row is less than HEIGHT, the
11923 row's height is increased to HEIGHT, and the icons are centered
11924 vertically in the new height.
11925
11926 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11927 count a final empty row in case the tool-bar width exactly matches
11928 the window width.
11929 */
11930
11931 static void
11932 display_tool_bar_line (struct it *it, int height)
11933 {
11934 struct glyph_row *row = it->glyph_row;
11935 int max_x = it->last_visible_x;
11936 struct glyph *last;
11937
11938 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11939 clear_glyph_row (row);
11940 row->enabled_p = true;
11941 row->y = it->current_y;
11942
11943 /* Note that this isn't made use of if the face hasn't a box,
11944 so there's no need to check the face here. */
11945 it->start_of_box_run_p = 1;
11946
11947 while (it->current_x < max_x)
11948 {
11949 int x, n_glyphs_before, i, nglyphs;
11950 struct it it_before;
11951
11952 /* Get the next display element. */
11953 if (!get_next_display_element (it))
11954 {
11955 /* Don't count empty row if we are counting needed tool-bar lines. */
11956 if (height < 0 && !it->hpos)
11957 return;
11958 break;
11959 }
11960
11961 /* Produce glyphs. */
11962 n_glyphs_before = row->used[TEXT_AREA];
11963 it_before = *it;
11964
11965 PRODUCE_GLYPHS (it);
11966
11967 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11968 i = 0;
11969 x = it_before.current_x;
11970 while (i < nglyphs)
11971 {
11972 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11973
11974 if (x + glyph->pixel_width > max_x)
11975 {
11976 /* Glyph doesn't fit on line. Backtrack. */
11977 row->used[TEXT_AREA] = n_glyphs_before;
11978 *it = it_before;
11979 /* If this is the only glyph on this line, it will never fit on the
11980 tool-bar, so skip it. But ensure there is at least one glyph,
11981 so we don't accidentally disable the tool-bar. */
11982 if (n_glyphs_before == 0
11983 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11984 break;
11985 goto out;
11986 }
11987
11988 ++it->hpos;
11989 x += glyph->pixel_width;
11990 ++i;
11991 }
11992
11993 /* Stop at line end. */
11994 if (ITERATOR_AT_END_OF_LINE_P (it))
11995 break;
11996
11997 set_iterator_to_next (it, 1);
11998 }
11999
12000 out:;
12001
12002 row->displays_text_p = row->used[TEXT_AREA] != 0;
12003
12004 /* Use default face for the border below the tool bar.
12005
12006 FIXME: When auto-resize-tool-bars is grow-only, there is
12007 no additional border below the possibly empty tool-bar lines.
12008 So to make the extra empty lines look "normal", we have to
12009 use the tool-bar face for the border too. */
12010 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12011 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12012 it->face_id = DEFAULT_FACE_ID;
12013
12014 extend_face_to_end_of_line (it);
12015 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12016 last->right_box_line_p = 1;
12017 if (last == row->glyphs[TEXT_AREA])
12018 last->left_box_line_p = 1;
12019
12020 /* Make line the desired height and center it vertically. */
12021 if ((height -= it->max_ascent + it->max_descent) > 0)
12022 {
12023 /* Don't add more than one line height. */
12024 height %= FRAME_LINE_HEIGHT (it->f);
12025 it->max_ascent += height / 2;
12026 it->max_descent += (height + 1) / 2;
12027 }
12028
12029 compute_line_metrics (it);
12030
12031 /* If line is empty, make it occupy the rest of the tool-bar. */
12032 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12033 {
12034 row->height = row->phys_height = it->last_visible_y - row->y;
12035 row->visible_height = row->height;
12036 row->ascent = row->phys_ascent = 0;
12037 row->extra_line_spacing = 0;
12038 }
12039
12040 row->full_width_p = 1;
12041 row->continued_p = 0;
12042 row->truncated_on_left_p = 0;
12043 row->truncated_on_right_p = 0;
12044
12045 it->current_x = it->hpos = 0;
12046 it->current_y += row->height;
12047 ++it->vpos;
12048 ++it->glyph_row;
12049 }
12050
12051
12052 /* Max tool-bar height. Basically, this is what makes all other windows
12053 disappear when the frame gets too small. Rethink this! */
12054
12055 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12056 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12057
12058 /* Value is the number of pixels needed to make all tool-bar items of
12059 frame F visible. The actual number of glyph rows needed is
12060 returned in *N_ROWS if non-NULL. */
12061
12062 static int
12063 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12064 {
12065 struct window *w = XWINDOW (f->tool_bar_window);
12066 struct it it;
12067 /* tool_bar_height is called from redisplay_tool_bar after building
12068 the desired matrix, so use (unused) mode-line row as temporary row to
12069 avoid destroying the first tool-bar row. */
12070 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12071
12072 /* Initialize an iterator for iteration over
12073 F->desired_tool_bar_string in the tool-bar window of frame F. */
12074 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12075 it.first_visible_x = 0;
12076 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12077 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12078 it.paragraph_embedding = L2R;
12079
12080 while (!ITERATOR_AT_END_P (&it))
12081 {
12082 clear_glyph_row (temp_row);
12083 it.glyph_row = temp_row;
12084 display_tool_bar_line (&it, -1);
12085 }
12086 clear_glyph_row (temp_row);
12087
12088 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12089 if (n_rows)
12090 *n_rows = it.vpos > 0 ? it.vpos : -1;
12091
12092 if (pixelwise)
12093 return it.current_y;
12094 else
12095 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12096 }
12097
12098 #endif /* !USE_GTK && !HAVE_NS */
12099
12100 #if defined USE_GTK || defined HAVE_NS
12101 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12102 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12103 #endif
12104
12105 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12106 0, 2, 0,
12107 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12108 If FRAME is nil or omitted, use the selected frame. Optional argument
12109 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12110 (Lisp_Object frame, Lisp_Object pixelwise)
12111 {
12112 int height = 0;
12113
12114 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12115 struct frame *f = decode_any_frame (frame);
12116
12117 if (WINDOWP (f->tool_bar_window)
12118 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12119 {
12120 update_tool_bar (f, 1);
12121 if (f->n_tool_bar_items)
12122 {
12123 build_desired_tool_bar_string (f);
12124 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12125 }
12126 }
12127 #endif
12128
12129 return make_number (height);
12130 }
12131
12132
12133 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12134 height should be changed. */
12135
12136 static int
12137 redisplay_tool_bar (struct frame *f)
12138 {
12139 #if defined (USE_GTK) || defined (HAVE_NS)
12140
12141 if (FRAME_EXTERNAL_TOOL_BAR (f))
12142 update_frame_tool_bar (f);
12143 return 0;
12144
12145 #else /* !USE_GTK && !HAVE_NS */
12146
12147 struct window *w;
12148 struct it it;
12149 struct glyph_row *row;
12150
12151 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12152 do anything. This means you must start with tool-bar-lines
12153 non-zero to get the auto-sizing effect. Or in other words, you
12154 can turn off tool-bars by specifying tool-bar-lines zero. */
12155 if (!WINDOWP (f->tool_bar_window)
12156 || (w = XWINDOW (f->tool_bar_window),
12157 WINDOW_PIXEL_HEIGHT (w) == 0))
12158 return 0;
12159
12160 /* Set up an iterator for the tool-bar window. */
12161 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12162 it.first_visible_x = 0;
12163 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12164 row = it.glyph_row;
12165
12166 /* Build a string that represents the contents of the tool-bar. */
12167 build_desired_tool_bar_string (f);
12168 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12169 /* FIXME: This should be controlled by a user option. But it
12170 doesn't make sense to have an R2L tool bar if the menu bar cannot
12171 be drawn also R2L, and making the menu bar R2L is tricky due
12172 toolkit-specific code that implements it. If an R2L tool bar is
12173 ever supported, display_tool_bar_line should also be augmented to
12174 call unproduce_glyphs like display_line and display_string
12175 do. */
12176 it.paragraph_embedding = L2R;
12177
12178 if (f->n_tool_bar_rows == 0)
12179 {
12180 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12181
12182 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12183 {
12184 Lisp_Object frame;
12185 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12186 / FRAME_LINE_HEIGHT (f));
12187
12188 XSETFRAME (frame, f);
12189 Fmodify_frame_parameters (frame,
12190 list1 (Fcons (Qtool_bar_lines,
12191 make_number (new_lines))));
12192 /* Always do that now. */
12193 clear_glyph_matrix (w->desired_matrix);
12194 f->fonts_changed = 1;
12195 return 1;
12196 }
12197 }
12198
12199 /* Display as many lines as needed to display all tool-bar items. */
12200
12201 if (f->n_tool_bar_rows > 0)
12202 {
12203 int border, rows, height, extra;
12204
12205 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12206 border = XINT (Vtool_bar_border);
12207 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12208 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12209 else if (EQ (Vtool_bar_border, Qborder_width))
12210 border = f->border_width;
12211 else
12212 border = 0;
12213 if (border < 0)
12214 border = 0;
12215
12216 rows = f->n_tool_bar_rows;
12217 height = max (1, (it.last_visible_y - border) / rows);
12218 extra = it.last_visible_y - border - height * rows;
12219
12220 while (it.current_y < it.last_visible_y)
12221 {
12222 int h = 0;
12223 if (extra > 0 && rows-- > 0)
12224 {
12225 h = (extra + rows - 1) / rows;
12226 extra -= h;
12227 }
12228 display_tool_bar_line (&it, height + h);
12229 }
12230 }
12231 else
12232 {
12233 while (it.current_y < it.last_visible_y)
12234 display_tool_bar_line (&it, 0);
12235 }
12236
12237 /* It doesn't make much sense to try scrolling in the tool-bar
12238 window, so don't do it. */
12239 w->desired_matrix->no_scrolling_p = 1;
12240 w->must_be_updated_p = 1;
12241
12242 if (!NILP (Vauto_resize_tool_bars))
12243 {
12244 /* Do we really allow the toolbar to occupy the whole frame? */
12245 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12246 int change_height_p = 0;
12247
12248 /* If we couldn't display everything, change the tool-bar's
12249 height if there is room for more. */
12250 if (IT_STRING_CHARPOS (it) < it.end_charpos
12251 && it.current_y < max_tool_bar_height)
12252 change_height_p = 1;
12253
12254 /* We subtract 1 because display_tool_bar_line advances the
12255 glyph_row pointer before returning to its caller. We want to
12256 examine the last glyph row produced by
12257 display_tool_bar_line. */
12258 row = it.glyph_row - 1;
12259
12260 /* If there are blank lines at the end, except for a partially
12261 visible blank line at the end that is smaller than
12262 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12263 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12264 && row->height >= FRAME_LINE_HEIGHT (f))
12265 change_height_p = 1;
12266
12267 /* If row displays tool-bar items, but is partially visible,
12268 change the tool-bar's height. */
12269 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12270 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12271 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12272 change_height_p = 1;
12273
12274 /* Resize windows as needed by changing the `tool-bar-lines'
12275 frame parameter. */
12276 if (change_height_p)
12277 {
12278 Lisp_Object frame;
12279 int nrows;
12280 int new_height = tool_bar_height (f, &nrows, 1);
12281
12282 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12283 && !f->minimize_tool_bar_window_p)
12284 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12285 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12286 f->minimize_tool_bar_window_p = 0;
12287
12288 if (change_height_p)
12289 {
12290 /* Current size of the tool-bar window in canonical line
12291 units. */
12292 int old_lines = WINDOW_TOTAL_LINES (w);
12293 /* Required size of the tool-bar window in canonical
12294 line units. */
12295 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12296 / FRAME_LINE_HEIGHT (f));
12297 /* Maximum size of the tool-bar window in canonical line
12298 units that this frame can allow. */
12299 int max_lines =
12300 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12301
12302 /* Don't try to change the tool-bar window size and set
12303 the fonts_changed flag unless really necessary. That
12304 flag causes redisplay to give up and retry
12305 redisplaying the frame from scratch, so setting it
12306 unnecessarily can lead to nasty redisplay loops. */
12307 if (new_lines <= max_lines
12308 && eabs (new_lines - old_lines) >= 1)
12309 {
12310 XSETFRAME (frame, f);
12311 Fmodify_frame_parameters (frame,
12312 list1 (Fcons (Qtool_bar_lines,
12313 make_number (new_lines))));
12314 clear_glyph_matrix (w->desired_matrix);
12315 f->n_tool_bar_rows = nrows;
12316 f->fonts_changed = 1;
12317 return 1;
12318 }
12319 }
12320 }
12321 }
12322
12323 f->minimize_tool_bar_window_p = 0;
12324 return 0;
12325
12326 #endif /* USE_GTK || HAVE_NS */
12327 }
12328
12329 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12330
12331 /* Get information about the tool-bar item which is displayed in GLYPH
12332 on frame F. Return in *PROP_IDX the index where tool-bar item
12333 properties start in F->tool_bar_items. Value is zero if
12334 GLYPH doesn't display a tool-bar item. */
12335
12336 static int
12337 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12338 {
12339 Lisp_Object prop;
12340 int success_p;
12341 int charpos;
12342
12343 /* This function can be called asynchronously, which means we must
12344 exclude any possibility that Fget_text_property signals an
12345 error. */
12346 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12347 charpos = max (0, charpos);
12348
12349 /* Get the text property `menu-item' at pos. The value of that
12350 property is the start index of this item's properties in
12351 F->tool_bar_items. */
12352 prop = Fget_text_property (make_number (charpos),
12353 Qmenu_item, f->current_tool_bar_string);
12354 if (INTEGERP (prop))
12355 {
12356 *prop_idx = XINT (prop);
12357 success_p = 1;
12358 }
12359 else
12360 success_p = 0;
12361
12362 return success_p;
12363 }
12364
12365 \f
12366 /* Get information about the tool-bar item at position X/Y on frame F.
12367 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12368 the current matrix of the tool-bar window of F, or NULL if not
12369 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12370 item in F->tool_bar_items. Value is
12371
12372 -1 if X/Y is not on a tool-bar item
12373 0 if X/Y is on the same item that was highlighted before.
12374 1 otherwise. */
12375
12376 static int
12377 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12378 int *hpos, int *vpos, int *prop_idx)
12379 {
12380 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12381 struct window *w = XWINDOW (f->tool_bar_window);
12382 int area;
12383
12384 /* Find the glyph under X/Y. */
12385 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12386 if (*glyph == NULL)
12387 return -1;
12388
12389 /* Get the start of this tool-bar item's properties in
12390 f->tool_bar_items. */
12391 if (!tool_bar_item_info (f, *glyph, prop_idx))
12392 return -1;
12393
12394 /* Is mouse on the highlighted item? */
12395 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12396 && *vpos >= hlinfo->mouse_face_beg_row
12397 && *vpos <= hlinfo->mouse_face_end_row
12398 && (*vpos > hlinfo->mouse_face_beg_row
12399 || *hpos >= hlinfo->mouse_face_beg_col)
12400 && (*vpos < hlinfo->mouse_face_end_row
12401 || *hpos < hlinfo->mouse_face_end_col
12402 || hlinfo->mouse_face_past_end))
12403 return 0;
12404
12405 return 1;
12406 }
12407
12408
12409 /* EXPORT:
12410 Handle mouse button event on the tool-bar of frame F, at
12411 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12412 0 for button release. MODIFIERS is event modifiers for button
12413 release. */
12414
12415 void
12416 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12417 int modifiers)
12418 {
12419 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12420 struct window *w = XWINDOW (f->tool_bar_window);
12421 int hpos, vpos, prop_idx;
12422 struct glyph *glyph;
12423 Lisp_Object enabled_p;
12424 int ts;
12425
12426 /* If not on the highlighted tool-bar item, and mouse-highlight is
12427 non-nil, return. This is so we generate the tool-bar button
12428 click only when the mouse button is released on the same item as
12429 where it was pressed. However, when mouse-highlight is disabled,
12430 generate the click when the button is released regardless of the
12431 highlight, since tool-bar items are not highlighted in that
12432 case. */
12433 frame_to_window_pixel_xy (w, &x, &y);
12434 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12435 if (ts == -1
12436 || (ts != 0 && !NILP (Vmouse_highlight)))
12437 return;
12438
12439 /* When mouse-highlight is off, generate the click for the item
12440 where the button was pressed, disregarding where it was
12441 released. */
12442 if (NILP (Vmouse_highlight) && !down_p)
12443 prop_idx = last_tool_bar_item;
12444
12445 /* If item is disabled, do nothing. */
12446 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12447 if (NILP (enabled_p))
12448 return;
12449
12450 if (down_p)
12451 {
12452 /* Show item in pressed state. */
12453 if (!NILP (Vmouse_highlight))
12454 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12455 last_tool_bar_item = prop_idx;
12456 }
12457 else
12458 {
12459 Lisp_Object key, frame;
12460 struct input_event event;
12461 EVENT_INIT (event);
12462
12463 /* Show item in released state. */
12464 if (!NILP (Vmouse_highlight))
12465 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12466
12467 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12468
12469 XSETFRAME (frame, f);
12470 event.kind = TOOL_BAR_EVENT;
12471 event.frame_or_window = frame;
12472 event.arg = frame;
12473 kbd_buffer_store_event (&event);
12474
12475 event.kind = TOOL_BAR_EVENT;
12476 event.frame_or_window = frame;
12477 event.arg = key;
12478 event.modifiers = modifiers;
12479 kbd_buffer_store_event (&event);
12480 last_tool_bar_item = -1;
12481 }
12482 }
12483
12484
12485 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12486 tool-bar window-relative coordinates X/Y. Called from
12487 note_mouse_highlight. */
12488
12489 static void
12490 note_tool_bar_highlight (struct frame *f, int x, int y)
12491 {
12492 Lisp_Object window = f->tool_bar_window;
12493 struct window *w = XWINDOW (window);
12494 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12495 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12496 int hpos, vpos;
12497 struct glyph *glyph;
12498 struct glyph_row *row;
12499 int i;
12500 Lisp_Object enabled_p;
12501 int prop_idx;
12502 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12503 int mouse_down_p, rc;
12504
12505 /* Function note_mouse_highlight is called with negative X/Y
12506 values when mouse moves outside of the frame. */
12507 if (x <= 0 || y <= 0)
12508 {
12509 clear_mouse_face (hlinfo);
12510 return;
12511 }
12512
12513 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12514 if (rc < 0)
12515 {
12516 /* Not on tool-bar item. */
12517 clear_mouse_face (hlinfo);
12518 return;
12519 }
12520 else if (rc == 0)
12521 /* On same tool-bar item as before. */
12522 goto set_help_echo;
12523
12524 clear_mouse_face (hlinfo);
12525
12526 /* Mouse is down, but on different tool-bar item? */
12527 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12528 && f == dpyinfo->last_mouse_frame);
12529
12530 if (mouse_down_p
12531 && last_tool_bar_item != prop_idx)
12532 return;
12533
12534 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12535
12536 /* If tool-bar item is not enabled, don't highlight it. */
12537 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12538 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12539 {
12540 /* Compute the x-position of the glyph. In front and past the
12541 image is a space. We include this in the highlighted area. */
12542 row = MATRIX_ROW (w->current_matrix, vpos);
12543 for (i = x = 0; i < hpos; ++i)
12544 x += row->glyphs[TEXT_AREA][i].pixel_width;
12545
12546 /* Record this as the current active region. */
12547 hlinfo->mouse_face_beg_col = hpos;
12548 hlinfo->mouse_face_beg_row = vpos;
12549 hlinfo->mouse_face_beg_x = x;
12550 hlinfo->mouse_face_past_end = 0;
12551
12552 hlinfo->mouse_face_end_col = hpos + 1;
12553 hlinfo->mouse_face_end_row = vpos;
12554 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12555 hlinfo->mouse_face_window = window;
12556 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12557
12558 /* Display it as active. */
12559 show_mouse_face (hlinfo, draw);
12560 }
12561
12562 set_help_echo:
12563
12564 /* Set help_echo_string to a help string to display for this tool-bar item.
12565 XTread_socket does the rest. */
12566 help_echo_object = help_echo_window = Qnil;
12567 help_echo_pos = -1;
12568 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12569 if (NILP (help_echo_string))
12570 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12571 }
12572
12573 #endif /* !USE_GTK && !HAVE_NS */
12574
12575 #endif /* HAVE_WINDOW_SYSTEM */
12576
12577
12578 \f
12579 /************************************************************************
12580 Horizontal scrolling
12581 ************************************************************************/
12582
12583 static int hscroll_window_tree (Lisp_Object);
12584 static int hscroll_windows (Lisp_Object);
12585
12586 /* For all leaf windows in the window tree rooted at WINDOW, set their
12587 hscroll value so that PT is (i) visible in the window, and (ii) so
12588 that it is not within a certain margin at the window's left and
12589 right border. Value is non-zero if any window's hscroll has been
12590 changed. */
12591
12592 static int
12593 hscroll_window_tree (Lisp_Object window)
12594 {
12595 int hscrolled_p = 0;
12596 int hscroll_relative_p = FLOATP (Vhscroll_step);
12597 int hscroll_step_abs = 0;
12598 double hscroll_step_rel = 0;
12599
12600 if (hscroll_relative_p)
12601 {
12602 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12603 if (hscroll_step_rel < 0)
12604 {
12605 hscroll_relative_p = 0;
12606 hscroll_step_abs = 0;
12607 }
12608 }
12609 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12610 {
12611 hscroll_step_abs = XINT (Vhscroll_step);
12612 if (hscroll_step_abs < 0)
12613 hscroll_step_abs = 0;
12614 }
12615 else
12616 hscroll_step_abs = 0;
12617
12618 while (WINDOWP (window))
12619 {
12620 struct window *w = XWINDOW (window);
12621
12622 if (WINDOWP (w->contents))
12623 hscrolled_p |= hscroll_window_tree (w->contents);
12624 else if (w->cursor.vpos >= 0)
12625 {
12626 int h_margin;
12627 int text_area_width;
12628 struct glyph_row *cursor_row;
12629 struct glyph_row *bottom_row;
12630 int row_r2l_p;
12631
12632 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12633 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12634 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12635 else
12636 cursor_row = bottom_row - 1;
12637
12638 if (!cursor_row->enabled_p)
12639 {
12640 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12641 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12642 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12643 else
12644 cursor_row = bottom_row - 1;
12645 }
12646 row_r2l_p = cursor_row->reversed_p;
12647
12648 text_area_width = window_box_width (w, TEXT_AREA);
12649
12650 /* Scroll when cursor is inside this scroll margin. */
12651 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12652
12653 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12654 /* For left-to-right rows, hscroll when cursor is either
12655 (i) inside the right hscroll margin, or (ii) if it is
12656 inside the left margin and the window is already
12657 hscrolled. */
12658 && ((!row_r2l_p
12659 && ((w->hscroll
12660 && w->cursor.x <= h_margin)
12661 || (cursor_row->enabled_p
12662 && cursor_row->truncated_on_right_p
12663 && (w->cursor.x >= text_area_width - h_margin))))
12664 /* For right-to-left rows, the logic is similar,
12665 except that rules for scrolling to left and right
12666 are reversed. E.g., if cursor.x <= h_margin, we
12667 need to hscroll "to the right" unconditionally,
12668 and that will scroll the screen to the left so as
12669 to reveal the next portion of the row. */
12670 || (row_r2l_p
12671 && ((cursor_row->enabled_p
12672 /* FIXME: It is confusing to set the
12673 truncated_on_right_p flag when R2L rows
12674 are actually truncated on the left. */
12675 && cursor_row->truncated_on_right_p
12676 && w->cursor.x <= h_margin)
12677 || (w->hscroll
12678 && (w->cursor.x >= text_area_width - h_margin))))))
12679 {
12680 struct it it;
12681 ptrdiff_t hscroll;
12682 struct buffer *saved_current_buffer;
12683 ptrdiff_t pt;
12684 int wanted_x;
12685
12686 /* Find point in a display of infinite width. */
12687 saved_current_buffer = current_buffer;
12688 current_buffer = XBUFFER (w->contents);
12689
12690 if (w == XWINDOW (selected_window))
12691 pt = PT;
12692 else
12693 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12694
12695 /* Move iterator to pt starting at cursor_row->start in
12696 a line with infinite width. */
12697 init_to_row_start (&it, w, cursor_row);
12698 it.last_visible_x = INFINITY;
12699 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12700 current_buffer = saved_current_buffer;
12701
12702 /* Position cursor in window. */
12703 if (!hscroll_relative_p && hscroll_step_abs == 0)
12704 hscroll = max (0, (it.current_x
12705 - (ITERATOR_AT_END_OF_LINE_P (&it)
12706 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12707 : (text_area_width / 2))))
12708 / FRAME_COLUMN_WIDTH (it.f);
12709 else if ((!row_r2l_p
12710 && w->cursor.x >= text_area_width - h_margin)
12711 || (row_r2l_p && w->cursor.x <= h_margin))
12712 {
12713 if (hscroll_relative_p)
12714 wanted_x = text_area_width * (1 - hscroll_step_rel)
12715 - h_margin;
12716 else
12717 wanted_x = text_area_width
12718 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12719 - h_margin;
12720 hscroll
12721 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12722 }
12723 else
12724 {
12725 if (hscroll_relative_p)
12726 wanted_x = text_area_width * hscroll_step_rel
12727 + h_margin;
12728 else
12729 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12730 + h_margin;
12731 hscroll
12732 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12733 }
12734 hscroll = max (hscroll, w->min_hscroll);
12735
12736 /* Don't prevent redisplay optimizations if hscroll
12737 hasn't changed, as it will unnecessarily slow down
12738 redisplay. */
12739 if (w->hscroll != hscroll)
12740 {
12741 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12742 w->hscroll = hscroll;
12743 hscrolled_p = 1;
12744 }
12745 }
12746 }
12747
12748 window = w->next;
12749 }
12750
12751 /* Value is non-zero if hscroll of any leaf window has been changed. */
12752 return hscrolled_p;
12753 }
12754
12755
12756 /* Set hscroll so that cursor is visible and not inside horizontal
12757 scroll margins for all windows in the tree rooted at WINDOW. See
12758 also hscroll_window_tree above. Value is non-zero if any window's
12759 hscroll has been changed. If it has, desired matrices on the frame
12760 of WINDOW are cleared. */
12761
12762 static int
12763 hscroll_windows (Lisp_Object window)
12764 {
12765 int hscrolled_p = hscroll_window_tree (window);
12766 if (hscrolled_p)
12767 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12768 return hscrolled_p;
12769 }
12770
12771
12772 \f
12773 /************************************************************************
12774 Redisplay
12775 ************************************************************************/
12776
12777 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12778 to a non-zero value. This is sometimes handy to have in a debugger
12779 session. */
12780
12781 #ifdef GLYPH_DEBUG
12782
12783 /* First and last unchanged row for try_window_id. */
12784
12785 static int debug_first_unchanged_at_end_vpos;
12786 static int debug_last_unchanged_at_beg_vpos;
12787
12788 /* Delta vpos and y. */
12789
12790 static int debug_dvpos, debug_dy;
12791
12792 /* Delta in characters and bytes for try_window_id. */
12793
12794 static ptrdiff_t debug_delta, debug_delta_bytes;
12795
12796 /* Values of window_end_pos and window_end_vpos at the end of
12797 try_window_id. */
12798
12799 static ptrdiff_t debug_end_vpos;
12800
12801 /* Append a string to W->desired_matrix->method. FMT is a printf
12802 format string. If trace_redisplay_p is true also printf the
12803 resulting string to stderr. */
12804
12805 static void debug_method_add (struct window *, char const *, ...)
12806 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12807
12808 static void
12809 debug_method_add (struct window *w, char const *fmt, ...)
12810 {
12811 void *ptr = w;
12812 char *method = w->desired_matrix->method;
12813 int len = strlen (method);
12814 int size = sizeof w->desired_matrix->method;
12815 int remaining = size - len - 1;
12816 va_list ap;
12817
12818 if (len && remaining)
12819 {
12820 method[len] = '|';
12821 --remaining, ++len;
12822 }
12823
12824 va_start (ap, fmt);
12825 vsnprintf (method + len, remaining + 1, fmt, ap);
12826 va_end (ap);
12827
12828 if (trace_redisplay_p)
12829 fprintf (stderr, "%p (%s): %s\n",
12830 ptr,
12831 ((BUFFERP (w->contents)
12832 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12833 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12834 : "no buffer"),
12835 method + len);
12836 }
12837
12838 #endif /* GLYPH_DEBUG */
12839
12840
12841 /* Value is non-zero if all changes in window W, which displays
12842 current_buffer, are in the text between START and END. START is a
12843 buffer position, END is given as a distance from Z. Used in
12844 redisplay_internal for display optimization. */
12845
12846 static int
12847 text_outside_line_unchanged_p (struct window *w,
12848 ptrdiff_t start, ptrdiff_t end)
12849 {
12850 int unchanged_p = 1;
12851
12852 /* If text or overlays have changed, see where. */
12853 if (window_outdated (w))
12854 {
12855 /* Gap in the line? */
12856 if (GPT < start || Z - GPT < end)
12857 unchanged_p = 0;
12858
12859 /* Changes start in front of the line, or end after it? */
12860 if (unchanged_p
12861 && (BEG_UNCHANGED < start - 1
12862 || END_UNCHANGED < end))
12863 unchanged_p = 0;
12864
12865 /* If selective display, can't optimize if changes start at the
12866 beginning of the line. */
12867 if (unchanged_p
12868 && INTEGERP (BVAR (current_buffer, selective_display))
12869 && XINT (BVAR (current_buffer, selective_display)) > 0
12870 && (BEG_UNCHANGED < start || GPT <= start))
12871 unchanged_p = 0;
12872
12873 /* If there are overlays at the start or end of the line, these
12874 may have overlay strings with newlines in them. A change at
12875 START, for instance, may actually concern the display of such
12876 overlay strings as well, and they are displayed on different
12877 lines. So, quickly rule out this case. (For the future, it
12878 might be desirable to implement something more telling than
12879 just BEG/END_UNCHANGED.) */
12880 if (unchanged_p)
12881 {
12882 if (BEG + BEG_UNCHANGED == start
12883 && overlay_touches_p (start))
12884 unchanged_p = 0;
12885 if (END_UNCHANGED == end
12886 && overlay_touches_p (Z - end))
12887 unchanged_p = 0;
12888 }
12889
12890 /* Under bidi reordering, adding or deleting a character in the
12891 beginning of a paragraph, before the first strong directional
12892 character, can change the base direction of the paragraph (unless
12893 the buffer specifies a fixed paragraph direction), which will
12894 require to redisplay the whole paragraph. It might be worthwhile
12895 to find the paragraph limits and widen the range of redisplayed
12896 lines to that, but for now just give up this optimization. */
12897 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12898 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12899 unchanged_p = 0;
12900 }
12901
12902 return unchanged_p;
12903 }
12904
12905
12906 /* Do a frame update, taking possible shortcuts into account. This is
12907 the main external entry point for redisplay.
12908
12909 If the last redisplay displayed an echo area message and that message
12910 is no longer requested, we clear the echo area or bring back the
12911 mini-buffer if that is in use. */
12912
12913 void
12914 redisplay (void)
12915 {
12916 redisplay_internal ();
12917 }
12918
12919
12920 static Lisp_Object
12921 overlay_arrow_string_or_property (Lisp_Object var)
12922 {
12923 Lisp_Object val;
12924
12925 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12926 return val;
12927
12928 return Voverlay_arrow_string;
12929 }
12930
12931 /* Return 1 if there are any overlay-arrows in current_buffer. */
12932 static int
12933 overlay_arrow_in_current_buffer_p (void)
12934 {
12935 Lisp_Object vlist;
12936
12937 for (vlist = Voverlay_arrow_variable_list;
12938 CONSP (vlist);
12939 vlist = XCDR (vlist))
12940 {
12941 Lisp_Object var = XCAR (vlist);
12942 Lisp_Object val;
12943
12944 if (!SYMBOLP (var))
12945 continue;
12946 val = find_symbol_value (var);
12947 if (MARKERP (val)
12948 && current_buffer == XMARKER (val)->buffer)
12949 return 1;
12950 }
12951 return 0;
12952 }
12953
12954
12955 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12956 has changed. */
12957
12958 static int
12959 overlay_arrows_changed_p (void)
12960 {
12961 Lisp_Object vlist;
12962
12963 for (vlist = Voverlay_arrow_variable_list;
12964 CONSP (vlist);
12965 vlist = XCDR (vlist))
12966 {
12967 Lisp_Object var = XCAR (vlist);
12968 Lisp_Object val, pstr;
12969
12970 if (!SYMBOLP (var))
12971 continue;
12972 val = find_symbol_value (var);
12973 if (!MARKERP (val))
12974 continue;
12975 if (! EQ (COERCE_MARKER (val),
12976 Fget (var, Qlast_arrow_position))
12977 || ! (pstr = overlay_arrow_string_or_property (var),
12978 EQ (pstr, Fget (var, Qlast_arrow_string))))
12979 return 1;
12980 }
12981 return 0;
12982 }
12983
12984 /* Mark overlay arrows to be updated on next redisplay. */
12985
12986 static void
12987 update_overlay_arrows (int up_to_date)
12988 {
12989 Lisp_Object vlist;
12990
12991 for (vlist = Voverlay_arrow_variable_list;
12992 CONSP (vlist);
12993 vlist = XCDR (vlist))
12994 {
12995 Lisp_Object var = XCAR (vlist);
12996
12997 if (!SYMBOLP (var))
12998 continue;
12999
13000 if (up_to_date > 0)
13001 {
13002 Lisp_Object val = find_symbol_value (var);
13003 Fput (var, Qlast_arrow_position,
13004 COERCE_MARKER (val));
13005 Fput (var, Qlast_arrow_string,
13006 overlay_arrow_string_or_property (var));
13007 }
13008 else if (up_to_date < 0
13009 || !NILP (Fget (var, Qlast_arrow_position)))
13010 {
13011 Fput (var, Qlast_arrow_position, Qt);
13012 Fput (var, Qlast_arrow_string, Qt);
13013 }
13014 }
13015 }
13016
13017
13018 /* Return overlay arrow string to display at row.
13019 Return integer (bitmap number) for arrow bitmap in left fringe.
13020 Return nil if no overlay arrow. */
13021
13022 static Lisp_Object
13023 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13024 {
13025 Lisp_Object vlist;
13026
13027 for (vlist = Voverlay_arrow_variable_list;
13028 CONSP (vlist);
13029 vlist = XCDR (vlist))
13030 {
13031 Lisp_Object var = XCAR (vlist);
13032 Lisp_Object val;
13033
13034 if (!SYMBOLP (var))
13035 continue;
13036
13037 val = find_symbol_value (var);
13038
13039 if (MARKERP (val)
13040 && current_buffer == XMARKER (val)->buffer
13041 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13042 {
13043 if (FRAME_WINDOW_P (it->f)
13044 /* FIXME: if ROW->reversed_p is set, this should test
13045 the right fringe, not the left one. */
13046 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13047 {
13048 #ifdef HAVE_WINDOW_SYSTEM
13049 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13050 {
13051 int fringe_bitmap;
13052 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13053 return make_number (fringe_bitmap);
13054 }
13055 #endif
13056 return make_number (-1); /* Use default arrow bitmap. */
13057 }
13058 return overlay_arrow_string_or_property (var);
13059 }
13060 }
13061
13062 return Qnil;
13063 }
13064
13065 /* Return 1 if point moved out of or into a composition. Otherwise
13066 return 0. PREV_BUF and PREV_PT are the last point buffer and
13067 position. BUF and PT are the current point buffer and position. */
13068
13069 static int
13070 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13071 struct buffer *buf, ptrdiff_t pt)
13072 {
13073 ptrdiff_t start, end;
13074 Lisp_Object prop;
13075 Lisp_Object buffer;
13076
13077 XSETBUFFER (buffer, buf);
13078 /* Check a composition at the last point if point moved within the
13079 same buffer. */
13080 if (prev_buf == buf)
13081 {
13082 if (prev_pt == pt)
13083 /* Point didn't move. */
13084 return 0;
13085
13086 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13087 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13088 && composition_valid_p (start, end, prop)
13089 && start < prev_pt && end > prev_pt)
13090 /* The last point was within the composition. Return 1 iff
13091 point moved out of the composition. */
13092 return (pt <= start || pt >= end);
13093 }
13094
13095 /* Check a composition at the current point. */
13096 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13097 && find_composition (pt, -1, &start, &end, &prop, buffer)
13098 && composition_valid_p (start, end, prop)
13099 && start < pt && end > pt);
13100 }
13101
13102 /* Reconsider the clip changes of buffer which is displayed in W. */
13103
13104 static void
13105 reconsider_clip_changes (struct window *w)
13106 {
13107 struct buffer *b = XBUFFER (w->contents);
13108
13109 if (b->clip_changed
13110 && w->window_end_valid
13111 && w->current_matrix->buffer == b
13112 && w->current_matrix->zv == BUF_ZV (b)
13113 && w->current_matrix->begv == BUF_BEGV (b))
13114 b->clip_changed = 0;
13115
13116 /* If display wasn't paused, and W is not a tool bar window, see if
13117 point has been moved into or out of a composition. In that case,
13118 we set b->clip_changed to 1 to force updating the screen. If
13119 b->clip_changed has already been set to 1, we can skip this
13120 check. */
13121 if (!b->clip_changed && w->window_end_valid)
13122 {
13123 ptrdiff_t pt = (w == XWINDOW (selected_window)
13124 ? PT : marker_position (w->pointm));
13125
13126 if ((w->current_matrix->buffer != b || pt != w->last_point)
13127 && check_point_in_composition (w->current_matrix->buffer,
13128 w->last_point, b, pt))
13129 b->clip_changed = 1;
13130 }
13131 }
13132
13133 static void
13134 propagate_buffer_redisplay (void)
13135 { /* Resetting b->text->redisplay is problematic!
13136 We can't just reset it in the case that some window that displays
13137 it has not been redisplayed; and such a window can stay
13138 unredisplayed for a long time if it's currently invisible.
13139 But we do want to reset it at the end of redisplay otherwise
13140 its displayed windows will keep being redisplayed over and over
13141 again.
13142 So we copy all b->text->redisplay flags up to their windows here,
13143 such that mark_window_display_accurate can safely reset
13144 b->text->redisplay. */
13145 Lisp_Object ws = window_list ();
13146 for (; CONSP (ws); ws = XCDR (ws))
13147 {
13148 struct window *thisw = XWINDOW (XCAR (ws));
13149 struct buffer *thisb = XBUFFER (thisw->contents);
13150 if (thisb->text->redisplay)
13151 thisw->redisplay = true;
13152 }
13153 }
13154
13155 #define STOP_POLLING \
13156 do { if (! polling_stopped_here) stop_polling (); \
13157 polling_stopped_here = 1; } while (0)
13158
13159 #define RESUME_POLLING \
13160 do { if (polling_stopped_here) start_polling (); \
13161 polling_stopped_here = 0; } while (0)
13162
13163
13164 /* Perhaps in the future avoid recentering windows if it
13165 is not necessary; currently that causes some problems. */
13166
13167 static void
13168 redisplay_internal (void)
13169 {
13170 struct window *w = XWINDOW (selected_window);
13171 struct window *sw;
13172 struct frame *fr;
13173 int pending;
13174 bool must_finish = 0, match_p;
13175 struct text_pos tlbufpos, tlendpos;
13176 int number_of_visible_frames;
13177 ptrdiff_t count;
13178 struct frame *sf;
13179 int polling_stopped_here = 0;
13180 Lisp_Object tail, frame;
13181
13182 /* True means redisplay has to consider all windows on all
13183 frames. False, only selected_window is considered. */
13184 bool consider_all_windows_p;
13185
13186 /* True means redisplay has to redisplay the miniwindow. */
13187 bool update_miniwindow_p = false;
13188
13189 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13190
13191 /* No redisplay if running in batch mode or frame is not yet fully
13192 initialized, or redisplay is explicitly turned off by setting
13193 Vinhibit_redisplay. */
13194 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13195 || !NILP (Vinhibit_redisplay))
13196 return;
13197
13198 /* Don't examine these until after testing Vinhibit_redisplay.
13199 When Emacs is shutting down, perhaps because its connection to
13200 X has dropped, we should not look at them at all. */
13201 fr = XFRAME (w->frame);
13202 sf = SELECTED_FRAME ();
13203
13204 if (!fr->glyphs_initialized_p)
13205 return;
13206
13207 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13208 if (popup_activated ())
13209 return;
13210 #endif
13211
13212 /* I don't think this happens but let's be paranoid. */
13213 if (redisplaying_p)
13214 return;
13215
13216 /* Record a function that clears redisplaying_p
13217 when we leave this function. */
13218 count = SPECPDL_INDEX ();
13219 record_unwind_protect_void (unwind_redisplay);
13220 redisplaying_p = 1;
13221 specbind (Qinhibit_free_realized_faces, Qnil);
13222
13223 /* Record this function, so it appears on the profiler's backtraces. */
13224 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13225
13226 FOR_EACH_FRAME (tail, frame)
13227 XFRAME (frame)->already_hscrolled_p = 0;
13228
13229 retry:
13230 /* Remember the currently selected window. */
13231 sw = w;
13232
13233 pending = 0;
13234 last_escape_glyph_frame = NULL;
13235 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13236 last_glyphless_glyph_frame = NULL;
13237 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13238
13239 /* If face_change_count is non-zero, init_iterator will free all
13240 realized faces, which includes the faces referenced from current
13241 matrices. So, we can't reuse current matrices in this case. */
13242 if (face_change_count)
13243 windows_or_buffers_changed = 47;
13244
13245 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13246 && FRAME_TTY (sf)->previous_frame != sf)
13247 {
13248 /* Since frames on a single ASCII terminal share the same
13249 display area, displaying a different frame means redisplay
13250 the whole thing. */
13251 SET_FRAME_GARBAGED (sf);
13252 #ifndef DOS_NT
13253 set_tty_color_mode (FRAME_TTY (sf), sf);
13254 #endif
13255 FRAME_TTY (sf)->previous_frame = sf;
13256 }
13257
13258 /* Set the visible flags for all frames. Do this before checking for
13259 resized or garbaged frames; they want to know if their frames are
13260 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13261 number_of_visible_frames = 0;
13262
13263 FOR_EACH_FRAME (tail, frame)
13264 {
13265 struct frame *f = XFRAME (frame);
13266
13267 if (FRAME_VISIBLE_P (f))
13268 {
13269 ++number_of_visible_frames;
13270 /* Adjust matrices for visible frames only. */
13271 if (f->fonts_changed)
13272 {
13273 adjust_frame_glyphs (f);
13274 f->fonts_changed = 0;
13275 }
13276 /* If cursor type has been changed on the frame
13277 other than selected, consider all frames. */
13278 if (f != sf && f->cursor_type_changed)
13279 update_mode_lines = 31;
13280 }
13281 clear_desired_matrices (f);
13282 }
13283
13284 /* Notice any pending interrupt request to change frame size. */
13285 do_pending_window_change (1);
13286
13287 /* do_pending_window_change could change the selected_window due to
13288 frame resizing which makes the selected window too small. */
13289 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13290 sw = w;
13291
13292 /* Clear frames marked as garbaged. */
13293 clear_garbaged_frames ();
13294
13295 /* Build menubar and tool-bar items. */
13296 if (NILP (Vmemory_full))
13297 prepare_menu_bars ();
13298
13299 reconsider_clip_changes (w);
13300
13301 /* In most cases selected window displays current buffer. */
13302 match_p = XBUFFER (w->contents) == current_buffer;
13303 if (match_p)
13304 {
13305 /* Detect case that we need to write or remove a star in the mode line. */
13306 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13307 w->update_mode_line = 1;
13308
13309 if (mode_line_update_needed (w))
13310 w->update_mode_line = 1;
13311 }
13312
13313 /* Normally the message* functions will have already displayed and
13314 updated the echo area, but the frame may have been trashed, or
13315 the update may have been preempted, so display the echo area
13316 again here. Checking message_cleared_p captures the case that
13317 the echo area should be cleared. */
13318 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13319 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13320 || (message_cleared_p
13321 && minibuf_level == 0
13322 /* If the mini-window is currently selected, this means the
13323 echo-area doesn't show through. */
13324 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13325 {
13326 int window_height_changed_p = echo_area_display (0);
13327
13328 if (message_cleared_p)
13329 update_miniwindow_p = true;
13330
13331 must_finish = 1;
13332
13333 /* If we don't display the current message, don't clear the
13334 message_cleared_p flag, because, if we did, we wouldn't clear
13335 the echo area in the next redisplay which doesn't preserve
13336 the echo area. */
13337 if (!display_last_displayed_message_p)
13338 message_cleared_p = 0;
13339
13340 if (window_height_changed_p)
13341 {
13342 windows_or_buffers_changed = 50;
13343
13344 /* If window configuration was changed, frames may have been
13345 marked garbaged. Clear them or we will experience
13346 surprises wrt scrolling. */
13347 clear_garbaged_frames ();
13348 }
13349 }
13350 else if (EQ (selected_window, minibuf_window)
13351 && (current_buffer->clip_changed || window_outdated (w))
13352 && resize_mini_window (w, 0))
13353 {
13354 /* Resized active mini-window to fit the size of what it is
13355 showing if its contents might have changed. */
13356 must_finish = 1;
13357
13358 /* If window configuration was changed, frames may have been
13359 marked garbaged. Clear them or we will experience
13360 surprises wrt scrolling. */
13361 clear_garbaged_frames ();
13362 }
13363
13364 if (windows_or_buffers_changed && !update_mode_lines)
13365 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13366 only the windows's contents needs to be refreshed, or whether the
13367 mode-lines also need a refresh. */
13368 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13369 ? REDISPLAY_SOME : 32);
13370
13371 /* If specs for an arrow have changed, do thorough redisplay
13372 to ensure we remove any arrow that should no longer exist. */
13373 if (overlay_arrows_changed_p ())
13374 /* Apparently, this is the only case where we update other windows,
13375 without updating other mode-lines. */
13376 windows_or_buffers_changed = 49;
13377
13378 consider_all_windows_p = (update_mode_lines
13379 || windows_or_buffers_changed);
13380
13381 #define AINC(a,i) \
13382 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13383 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13384
13385 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13386 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13387
13388 /* Optimize the case that only the line containing the cursor in the
13389 selected window has changed. Variables starting with this_ are
13390 set in display_line and record information about the line
13391 containing the cursor. */
13392 tlbufpos = this_line_start_pos;
13393 tlendpos = this_line_end_pos;
13394 if (!consider_all_windows_p
13395 && CHARPOS (tlbufpos) > 0
13396 && !w->update_mode_line
13397 && !current_buffer->clip_changed
13398 && !current_buffer->prevent_redisplay_optimizations_p
13399 && FRAME_VISIBLE_P (XFRAME (w->frame))
13400 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13401 && !XFRAME (w->frame)->cursor_type_changed
13402 /* Make sure recorded data applies to current buffer, etc. */
13403 && this_line_buffer == current_buffer
13404 && match_p
13405 && !w->force_start
13406 && !w->optional_new_start
13407 /* Point must be on the line that we have info recorded about. */
13408 && PT >= CHARPOS (tlbufpos)
13409 && PT <= Z - CHARPOS (tlendpos)
13410 /* All text outside that line, including its final newline,
13411 must be unchanged. */
13412 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13413 CHARPOS (tlendpos)))
13414 {
13415 if (CHARPOS (tlbufpos) > BEGV
13416 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13417 && (CHARPOS (tlbufpos) == ZV
13418 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13419 /* Former continuation line has disappeared by becoming empty. */
13420 goto cancel;
13421 else if (window_outdated (w) || MINI_WINDOW_P (w))
13422 {
13423 /* We have to handle the case of continuation around a
13424 wide-column character (see the comment in indent.c around
13425 line 1340).
13426
13427 For instance, in the following case:
13428
13429 -------- Insert --------
13430 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13431 J_I_ ==> J_I_ `^^' are cursors.
13432 ^^ ^^
13433 -------- --------
13434
13435 As we have to redraw the line above, we cannot use this
13436 optimization. */
13437
13438 struct it it;
13439 int line_height_before = this_line_pixel_height;
13440
13441 /* Note that start_display will handle the case that the
13442 line starting at tlbufpos is a continuation line. */
13443 start_display (&it, w, tlbufpos);
13444
13445 /* Implementation note: It this still necessary? */
13446 if (it.current_x != this_line_start_x)
13447 goto cancel;
13448
13449 TRACE ((stderr, "trying display optimization 1\n"));
13450 w->cursor.vpos = -1;
13451 overlay_arrow_seen = 0;
13452 it.vpos = this_line_vpos;
13453 it.current_y = this_line_y;
13454 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13455 display_line (&it);
13456
13457 /* If line contains point, is not continued,
13458 and ends at same distance from eob as before, we win. */
13459 if (w->cursor.vpos >= 0
13460 /* Line is not continued, otherwise this_line_start_pos
13461 would have been set to 0 in display_line. */
13462 && CHARPOS (this_line_start_pos)
13463 /* Line ends as before. */
13464 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13465 /* Line has same height as before. Otherwise other lines
13466 would have to be shifted up or down. */
13467 && this_line_pixel_height == line_height_before)
13468 {
13469 /* If this is not the window's last line, we must adjust
13470 the charstarts of the lines below. */
13471 if (it.current_y < it.last_visible_y)
13472 {
13473 struct glyph_row *row
13474 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13475 ptrdiff_t delta, delta_bytes;
13476
13477 /* We used to distinguish between two cases here,
13478 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13479 when the line ends in a newline or the end of the
13480 buffer's accessible portion. But both cases did
13481 the same, so they were collapsed. */
13482 delta = (Z
13483 - CHARPOS (tlendpos)
13484 - MATRIX_ROW_START_CHARPOS (row));
13485 delta_bytes = (Z_BYTE
13486 - BYTEPOS (tlendpos)
13487 - MATRIX_ROW_START_BYTEPOS (row));
13488
13489 increment_matrix_positions (w->current_matrix,
13490 this_line_vpos + 1,
13491 w->current_matrix->nrows,
13492 delta, delta_bytes);
13493 }
13494
13495 /* If this row displays text now but previously didn't,
13496 or vice versa, w->window_end_vpos may have to be
13497 adjusted. */
13498 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13499 {
13500 if (w->window_end_vpos < this_line_vpos)
13501 w->window_end_vpos = this_line_vpos;
13502 }
13503 else if (w->window_end_vpos == this_line_vpos
13504 && this_line_vpos > 0)
13505 w->window_end_vpos = this_line_vpos - 1;
13506 w->window_end_valid = 0;
13507
13508 /* Update hint: No need to try to scroll in update_window. */
13509 w->desired_matrix->no_scrolling_p = 1;
13510
13511 #ifdef GLYPH_DEBUG
13512 *w->desired_matrix->method = 0;
13513 debug_method_add (w, "optimization 1");
13514 #endif
13515 #ifdef HAVE_WINDOW_SYSTEM
13516 update_window_fringes (w, 0);
13517 #endif
13518 goto update;
13519 }
13520 else
13521 goto cancel;
13522 }
13523 else if (/* Cursor position hasn't changed. */
13524 PT == w->last_point
13525 /* Make sure the cursor was last displayed
13526 in this window. Otherwise we have to reposition it. */
13527
13528 /* PXW: Must be converted to pixels, probably. */
13529 && 0 <= w->cursor.vpos
13530 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13531 {
13532 if (!must_finish)
13533 {
13534 do_pending_window_change (1);
13535 /* If selected_window changed, redisplay again. */
13536 if (WINDOWP (selected_window)
13537 && (w = XWINDOW (selected_window)) != sw)
13538 goto retry;
13539
13540 /* We used to always goto end_of_redisplay here, but this
13541 isn't enough if we have a blinking cursor. */
13542 if (w->cursor_off_p == w->last_cursor_off_p)
13543 goto end_of_redisplay;
13544 }
13545 goto update;
13546 }
13547 /* If highlighting the region, or if the cursor is in the echo area,
13548 then we can't just move the cursor. */
13549 else if (NILP (Vshow_trailing_whitespace)
13550 && !cursor_in_echo_area)
13551 {
13552 struct it it;
13553 struct glyph_row *row;
13554
13555 /* Skip from tlbufpos to PT and see where it is. Note that
13556 PT may be in invisible text. If so, we will end at the
13557 next visible position. */
13558 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13559 NULL, DEFAULT_FACE_ID);
13560 it.current_x = this_line_start_x;
13561 it.current_y = this_line_y;
13562 it.vpos = this_line_vpos;
13563
13564 /* The call to move_it_to stops in front of PT, but
13565 moves over before-strings. */
13566 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13567
13568 if (it.vpos == this_line_vpos
13569 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13570 row->enabled_p))
13571 {
13572 eassert (this_line_vpos == it.vpos);
13573 eassert (this_line_y == it.current_y);
13574 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13575 #ifdef GLYPH_DEBUG
13576 *w->desired_matrix->method = 0;
13577 debug_method_add (w, "optimization 3");
13578 #endif
13579 goto update;
13580 }
13581 else
13582 goto cancel;
13583 }
13584
13585 cancel:
13586 /* Text changed drastically or point moved off of line. */
13587 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13588 }
13589
13590 CHARPOS (this_line_start_pos) = 0;
13591 ++clear_face_cache_count;
13592 #ifdef HAVE_WINDOW_SYSTEM
13593 ++clear_image_cache_count;
13594 #endif
13595
13596 /* Build desired matrices, and update the display. If
13597 consider_all_windows_p is non-zero, do it for all windows on all
13598 frames. Otherwise do it for selected_window, only. */
13599
13600 if (consider_all_windows_p)
13601 {
13602 FOR_EACH_FRAME (tail, frame)
13603 XFRAME (frame)->updated_p = 0;
13604
13605 propagate_buffer_redisplay ();
13606
13607 FOR_EACH_FRAME (tail, frame)
13608 {
13609 struct frame *f = XFRAME (frame);
13610
13611 /* We don't have to do anything for unselected terminal
13612 frames. */
13613 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13614 && !EQ (FRAME_TTY (f)->top_frame, frame))
13615 continue;
13616
13617 retry_frame:
13618
13619 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13620 {
13621 bool gcscrollbars
13622 /* Only GC scrollbars when we redisplay the whole frame. */
13623 = f->redisplay || !REDISPLAY_SOME_P ();
13624 /* Mark all the scroll bars to be removed; we'll redeem
13625 the ones we want when we redisplay their windows. */
13626 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13627 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13628
13629 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13630 redisplay_windows (FRAME_ROOT_WINDOW (f));
13631 /* Remember that the invisible frames need to be redisplayed next
13632 time they're visible. */
13633 else if (!REDISPLAY_SOME_P ())
13634 f->redisplay = true;
13635
13636 /* The X error handler may have deleted that frame. */
13637 if (!FRAME_LIVE_P (f))
13638 continue;
13639
13640 /* Any scroll bars which redisplay_windows should have
13641 nuked should now go away. */
13642 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13643 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13644
13645 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13646 {
13647 /* If fonts changed on visible frame, display again. */
13648 if (f->fonts_changed)
13649 {
13650 adjust_frame_glyphs (f);
13651 f->fonts_changed = 0;
13652 goto retry_frame;
13653 }
13654
13655 /* See if we have to hscroll. */
13656 if (!f->already_hscrolled_p)
13657 {
13658 f->already_hscrolled_p = 1;
13659 if (hscroll_windows (f->root_window))
13660 goto retry_frame;
13661 }
13662
13663 /* Prevent various kinds of signals during display
13664 update. stdio is not robust about handling
13665 signals, which can cause an apparent I/O error. */
13666 if (interrupt_input)
13667 unrequest_sigio ();
13668 STOP_POLLING;
13669
13670 pending |= update_frame (f, 0, 0);
13671 f->cursor_type_changed = 0;
13672 f->updated_p = 1;
13673 }
13674 }
13675 }
13676
13677 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13678
13679 if (!pending)
13680 {
13681 /* Do the mark_window_display_accurate after all windows have
13682 been redisplayed because this call resets flags in buffers
13683 which are needed for proper redisplay. */
13684 FOR_EACH_FRAME (tail, frame)
13685 {
13686 struct frame *f = XFRAME (frame);
13687 if (f->updated_p)
13688 {
13689 f->redisplay = false;
13690 mark_window_display_accurate (f->root_window, 1);
13691 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13692 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13693 }
13694 }
13695 }
13696 }
13697 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13698 {
13699 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13700 struct frame *mini_frame;
13701
13702 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13703 /* Use list_of_error, not Qerror, so that
13704 we catch only errors and don't run the debugger. */
13705 internal_condition_case_1 (redisplay_window_1, selected_window,
13706 list_of_error,
13707 redisplay_window_error);
13708 if (update_miniwindow_p)
13709 internal_condition_case_1 (redisplay_window_1, mini_window,
13710 list_of_error,
13711 redisplay_window_error);
13712
13713 /* Compare desired and current matrices, perform output. */
13714
13715 update:
13716 /* If fonts changed, display again. */
13717 if (sf->fonts_changed)
13718 goto retry;
13719
13720 /* Prevent various kinds of signals during display update.
13721 stdio is not robust about handling signals,
13722 which can cause an apparent I/O error. */
13723 if (interrupt_input)
13724 unrequest_sigio ();
13725 STOP_POLLING;
13726
13727 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13728 {
13729 if (hscroll_windows (selected_window))
13730 goto retry;
13731
13732 XWINDOW (selected_window)->must_be_updated_p = true;
13733 pending = update_frame (sf, 0, 0);
13734 sf->cursor_type_changed = 0;
13735 }
13736
13737 /* We may have called echo_area_display at the top of this
13738 function. If the echo area is on another frame, that may
13739 have put text on a frame other than the selected one, so the
13740 above call to update_frame would not have caught it. Catch
13741 it here. */
13742 mini_window = FRAME_MINIBUF_WINDOW (sf);
13743 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13744
13745 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13746 {
13747 XWINDOW (mini_window)->must_be_updated_p = true;
13748 pending |= update_frame (mini_frame, 0, 0);
13749 mini_frame->cursor_type_changed = 0;
13750 if (!pending && hscroll_windows (mini_window))
13751 goto retry;
13752 }
13753 }
13754
13755 /* If display was paused because of pending input, make sure we do a
13756 thorough update the next time. */
13757 if (pending)
13758 {
13759 /* Prevent the optimization at the beginning of
13760 redisplay_internal that tries a single-line update of the
13761 line containing the cursor in the selected window. */
13762 CHARPOS (this_line_start_pos) = 0;
13763
13764 /* Let the overlay arrow be updated the next time. */
13765 update_overlay_arrows (0);
13766
13767 /* If we pause after scrolling, some rows in the current
13768 matrices of some windows are not valid. */
13769 if (!WINDOW_FULL_WIDTH_P (w)
13770 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13771 update_mode_lines = 36;
13772 }
13773 else
13774 {
13775 if (!consider_all_windows_p)
13776 {
13777 /* This has already been done above if
13778 consider_all_windows_p is set. */
13779 if (XBUFFER (w->contents)->text->redisplay
13780 && buffer_window_count (XBUFFER (w->contents)) > 1)
13781 /* This can happen if b->text->redisplay was set during
13782 jit-lock. */
13783 propagate_buffer_redisplay ();
13784 mark_window_display_accurate_1 (w, 1);
13785
13786 /* Say overlay arrows are up to date. */
13787 update_overlay_arrows (1);
13788
13789 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13790 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13791 }
13792
13793 update_mode_lines = 0;
13794 windows_or_buffers_changed = 0;
13795 }
13796
13797 /* Start SIGIO interrupts coming again. Having them off during the
13798 code above makes it less likely one will discard output, but not
13799 impossible, since there might be stuff in the system buffer here.
13800 But it is much hairier to try to do anything about that. */
13801 if (interrupt_input)
13802 request_sigio ();
13803 RESUME_POLLING;
13804
13805 /* If a frame has become visible which was not before, redisplay
13806 again, so that we display it. Expose events for such a frame
13807 (which it gets when becoming visible) don't call the parts of
13808 redisplay constructing glyphs, so simply exposing a frame won't
13809 display anything in this case. So, we have to display these
13810 frames here explicitly. */
13811 if (!pending)
13812 {
13813 int new_count = 0;
13814
13815 FOR_EACH_FRAME (tail, frame)
13816 {
13817 if (XFRAME (frame)->visible)
13818 new_count++;
13819 }
13820
13821 if (new_count != number_of_visible_frames)
13822 windows_or_buffers_changed = 52;
13823 }
13824
13825 /* Change frame size now if a change is pending. */
13826 do_pending_window_change (1);
13827
13828 /* If we just did a pending size change, or have additional
13829 visible frames, or selected_window changed, redisplay again. */
13830 if ((windows_or_buffers_changed && !pending)
13831 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13832 goto retry;
13833
13834 /* Clear the face and image caches.
13835
13836 We used to do this only if consider_all_windows_p. But the cache
13837 needs to be cleared if a timer creates images in the current
13838 buffer (e.g. the test case in Bug#6230). */
13839
13840 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13841 {
13842 clear_face_cache (0);
13843 clear_face_cache_count = 0;
13844 }
13845
13846 #ifdef HAVE_WINDOW_SYSTEM
13847 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13848 {
13849 clear_image_caches (Qnil);
13850 clear_image_cache_count = 0;
13851 }
13852 #endif /* HAVE_WINDOW_SYSTEM */
13853
13854 end_of_redisplay:
13855 if (interrupt_input && interrupts_deferred)
13856 request_sigio ();
13857
13858 unbind_to (count, Qnil);
13859 RESUME_POLLING;
13860 }
13861
13862
13863 /* Redisplay, but leave alone any recent echo area message unless
13864 another message has been requested in its place.
13865
13866 This is useful in situations where you need to redisplay but no
13867 user action has occurred, making it inappropriate for the message
13868 area to be cleared. See tracking_off and
13869 wait_reading_process_output for examples of these situations.
13870
13871 FROM_WHERE is an integer saying from where this function was
13872 called. This is useful for debugging. */
13873
13874 void
13875 redisplay_preserve_echo_area (int from_where)
13876 {
13877 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13878
13879 if (!NILP (echo_area_buffer[1]))
13880 {
13881 /* We have a previously displayed message, but no current
13882 message. Redisplay the previous message. */
13883 display_last_displayed_message_p = 1;
13884 redisplay_internal ();
13885 display_last_displayed_message_p = 0;
13886 }
13887 else
13888 redisplay_internal ();
13889
13890 flush_frame (SELECTED_FRAME ());
13891 }
13892
13893
13894 /* Function registered with record_unwind_protect in redisplay_internal. */
13895
13896 static void
13897 unwind_redisplay (void)
13898 {
13899 redisplaying_p = 0;
13900 }
13901
13902
13903 /* Mark the display of leaf window W as accurate or inaccurate.
13904 If ACCURATE_P is non-zero mark display of W as accurate. If
13905 ACCURATE_P is zero, arrange for W to be redisplayed the next
13906 time redisplay_internal is called. */
13907
13908 static void
13909 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13910 {
13911 struct buffer *b = XBUFFER (w->contents);
13912
13913 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13914 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13915 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13916
13917 if (accurate_p)
13918 {
13919 b->clip_changed = false;
13920 b->prevent_redisplay_optimizations_p = false;
13921 eassert (buffer_window_count (b) > 0);
13922 /* Resetting b->text->redisplay is problematic!
13923 In order to make it safer to do it here, redisplay_internal must
13924 have copied all b->text->redisplay to their respective windows. */
13925 b->text->redisplay = false;
13926
13927 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13928 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13929 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13930 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13931
13932 w->current_matrix->buffer = b;
13933 w->current_matrix->begv = BUF_BEGV (b);
13934 w->current_matrix->zv = BUF_ZV (b);
13935
13936 w->last_cursor_vpos = w->cursor.vpos;
13937 w->last_cursor_off_p = w->cursor_off_p;
13938
13939 if (w == XWINDOW (selected_window))
13940 w->last_point = BUF_PT (b);
13941 else
13942 w->last_point = marker_position (w->pointm);
13943
13944 w->window_end_valid = true;
13945 w->update_mode_line = false;
13946 }
13947
13948 w->redisplay = !accurate_p;
13949 }
13950
13951
13952 /* Mark the display of windows in the window tree rooted at WINDOW as
13953 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13954 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13955 be redisplayed the next time redisplay_internal is called. */
13956
13957 void
13958 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13959 {
13960 struct window *w;
13961
13962 for (; !NILP (window); window = w->next)
13963 {
13964 w = XWINDOW (window);
13965 if (WINDOWP (w->contents))
13966 mark_window_display_accurate (w->contents, accurate_p);
13967 else
13968 mark_window_display_accurate_1 (w, accurate_p);
13969 }
13970
13971 if (accurate_p)
13972 update_overlay_arrows (1);
13973 else
13974 /* Force a thorough redisplay the next time by setting
13975 last_arrow_position and last_arrow_string to t, which is
13976 unequal to any useful value of Voverlay_arrow_... */
13977 update_overlay_arrows (-1);
13978 }
13979
13980
13981 /* Return value in display table DP (Lisp_Char_Table *) for character
13982 C. Since a display table doesn't have any parent, we don't have to
13983 follow parent. Do not call this function directly but use the
13984 macro DISP_CHAR_VECTOR. */
13985
13986 Lisp_Object
13987 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13988 {
13989 Lisp_Object val;
13990
13991 if (ASCII_CHAR_P (c))
13992 {
13993 val = dp->ascii;
13994 if (SUB_CHAR_TABLE_P (val))
13995 val = XSUB_CHAR_TABLE (val)->contents[c];
13996 }
13997 else
13998 {
13999 Lisp_Object table;
14000
14001 XSETCHAR_TABLE (table, dp);
14002 val = char_table_ref (table, c);
14003 }
14004 if (NILP (val))
14005 val = dp->defalt;
14006 return val;
14007 }
14008
14009
14010 \f
14011 /***********************************************************************
14012 Window Redisplay
14013 ***********************************************************************/
14014
14015 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14016
14017 static void
14018 redisplay_windows (Lisp_Object window)
14019 {
14020 while (!NILP (window))
14021 {
14022 struct window *w = XWINDOW (window);
14023
14024 if (WINDOWP (w->contents))
14025 redisplay_windows (w->contents);
14026 else if (BUFFERP (w->contents))
14027 {
14028 displayed_buffer = XBUFFER (w->contents);
14029 /* Use list_of_error, not Qerror, so that
14030 we catch only errors and don't run the debugger. */
14031 internal_condition_case_1 (redisplay_window_0, window,
14032 list_of_error,
14033 redisplay_window_error);
14034 }
14035
14036 window = w->next;
14037 }
14038 }
14039
14040 static Lisp_Object
14041 redisplay_window_error (Lisp_Object ignore)
14042 {
14043 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14044 return Qnil;
14045 }
14046
14047 static Lisp_Object
14048 redisplay_window_0 (Lisp_Object window)
14049 {
14050 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14051 redisplay_window (window, false);
14052 return Qnil;
14053 }
14054
14055 static Lisp_Object
14056 redisplay_window_1 (Lisp_Object window)
14057 {
14058 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14059 redisplay_window (window, true);
14060 return Qnil;
14061 }
14062 \f
14063
14064 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14065 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14066 which positions recorded in ROW differ from current buffer
14067 positions.
14068
14069 Return 0 if cursor is not on this row, 1 otherwise. */
14070
14071 static int
14072 set_cursor_from_row (struct window *w, struct glyph_row *row,
14073 struct glyph_matrix *matrix,
14074 ptrdiff_t delta, ptrdiff_t delta_bytes,
14075 int dy, int dvpos)
14076 {
14077 struct glyph *glyph = row->glyphs[TEXT_AREA];
14078 struct glyph *end = glyph + row->used[TEXT_AREA];
14079 struct glyph *cursor = NULL;
14080 /* The last known character position in row. */
14081 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14082 int x = row->x;
14083 ptrdiff_t pt_old = PT - delta;
14084 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14085 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14086 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14087 /* A glyph beyond the edge of TEXT_AREA which we should never
14088 touch. */
14089 struct glyph *glyphs_end = end;
14090 /* Non-zero means we've found a match for cursor position, but that
14091 glyph has the avoid_cursor_p flag set. */
14092 int match_with_avoid_cursor = 0;
14093 /* Non-zero means we've seen at least one glyph that came from a
14094 display string. */
14095 int string_seen = 0;
14096 /* Largest and smallest buffer positions seen so far during scan of
14097 glyph row. */
14098 ptrdiff_t bpos_max = pos_before;
14099 ptrdiff_t bpos_min = pos_after;
14100 /* Last buffer position covered by an overlay string with an integer
14101 `cursor' property. */
14102 ptrdiff_t bpos_covered = 0;
14103 /* Non-zero means the display string on which to display the cursor
14104 comes from a text property, not from an overlay. */
14105 int string_from_text_prop = 0;
14106
14107 /* Don't even try doing anything if called for a mode-line or
14108 header-line row, since the rest of the code isn't prepared to
14109 deal with such calamities. */
14110 eassert (!row->mode_line_p);
14111 if (row->mode_line_p)
14112 return 0;
14113
14114 /* Skip over glyphs not having an object at the start and the end of
14115 the row. These are special glyphs like truncation marks on
14116 terminal frames. */
14117 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14118 {
14119 if (!row->reversed_p)
14120 {
14121 while (glyph < end
14122 && INTEGERP (glyph->object)
14123 && glyph->charpos < 0)
14124 {
14125 x += glyph->pixel_width;
14126 ++glyph;
14127 }
14128 while (end > glyph
14129 && INTEGERP ((end - 1)->object)
14130 /* CHARPOS is zero for blanks and stretch glyphs
14131 inserted by extend_face_to_end_of_line. */
14132 && (end - 1)->charpos <= 0)
14133 --end;
14134 glyph_before = glyph - 1;
14135 glyph_after = end;
14136 }
14137 else
14138 {
14139 struct glyph *g;
14140
14141 /* If the glyph row is reversed, we need to process it from back
14142 to front, so swap the edge pointers. */
14143 glyphs_end = end = glyph - 1;
14144 glyph += row->used[TEXT_AREA] - 1;
14145
14146 while (glyph > end + 1
14147 && INTEGERP (glyph->object)
14148 && glyph->charpos < 0)
14149 {
14150 --glyph;
14151 x -= glyph->pixel_width;
14152 }
14153 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14154 --glyph;
14155 /* By default, in reversed rows we put the cursor on the
14156 rightmost (first in the reading order) glyph. */
14157 for (g = end + 1; g < glyph; g++)
14158 x += g->pixel_width;
14159 while (end < glyph
14160 && INTEGERP ((end + 1)->object)
14161 && (end + 1)->charpos <= 0)
14162 ++end;
14163 glyph_before = glyph + 1;
14164 glyph_after = end;
14165 }
14166 }
14167 else if (row->reversed_p)
14168 {
14169 /* In R2L rows that don't display text, put the cursor on the
14170 rightmost glyph. Case in point: an empty last line that is
14171 part of an R2L paragraph. */
14172 cursor = end - 1;
14173 /* Avoid placing the cursor on the last glyph of the row, where
14174 on terminal frames we hold the vertical border between
14175 adjacent windows. */
14176 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14177 && !WINDOW_RIGHTMOST_P (w)
14178 && cursor == row->glyphs[LAST_AREA] - 1)
14179 cursor--;
14180 x = -1; /* will be computed below, at label compute_x */
14181 }
14182
14183 /* Step 1: Try to find the glyph whose character position
14184 corresponds to point. If that's not possible, find 2 glyphs
14185 whose character positions are the closest to point, one before
14186 point, the other after it. */
14187 if (!row->reversed_p)
14188 while (/* not marched to end of glyph row */
14189 glyph < end
14190 /* glyph was not inserted by redisplay for internal purposes */
14191 && !INTEGERP (glyph->object))
14192 {
14193 if (BUFFERP (glyph->object))
14194 {
14195 ptrdiff_t dpos = glyph->charpos - pt_old;
14196
14197 if (glyph->charpos > bpos_max)
14198 bpos_max = glyph->charpos;
14199 if (glyph->charpos < bpos_min)
14200 bpos_min = glyph->charpos;
14201 if (!glyph->avoid_cursor_p)
14202 {
14203 /* If we hit point, we've found the glyph on which to
14204 display the cursor. */
14205 if (dpos == 0)
14206 {
14207 match_with_avoid_cursor = 0;
14208 break;
14209 }
14210 /* See if we've found a better approximation to
14211 POS_BEFORE or to POS_AFTER. */
14212 if (0 > dpos && dpos > pos_before - pt_old)
14213 {
14214 pos_before = glyph->charpos;
14215 glyph_before = glyph;
14216 }
14217 else if (0 < dpos && dpos < pos_after - pt_old)
14218 {
14219 pos_after = glyph->charpos;
14220 glyph_after = glyph;
14221 }
14222 }
14223 else if (dpos == 0)
14224 match_with_avoid_cursor = 1;
14225 }
14226 else if (STRINGP (glyph->object))
14227 {
14228 Lisp_Object chprop;
14229 ptrdiff_t glyph_pos = glyph->charpos;
14230
14231 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14232 glyph->object);
14233 if (!NILP (chprop))
14234 {
14235 /* If the string came from a `display' text property,
14236 look up the buffer position of that property and
14237 use that position to update bpos_max, as if we
14238 actually saw such a position in one of the row's
14239 glyphs. This helps with supporting integer values
14240 of `cursor' property on the display string in
14241 situations where most or all of the row's buffer
14242 text is completely covered by display properties,
14243 so that no glyph with valid buffer positions is
14244 ever seen in the row. */
14245 ptrdiff_t prop_pos =
14246 string_buffer_position_lim (glyph->object, pos_before,
14247 pos_after, 0);
14248
14249 if (prop_pos >= pos_before)
14250 bpos_max = prop_pos - 1;
14251 }
14252 if (INTEGERP (chprop))
14253 {
14254 bpos_covered = bpos_max + XINT (chprop);
14255 /* If the `cursor' property covers buffer positions up
14256 to and including point, we should display cursor on
14257 this glyph. Note that, if a `cursor' property on one
14258 of the string's characters has an integer value, we
14259 will break out of the loop below _before_ we get to
14260 the position match above. IOW, integer values of
14261 the `cursor' property override the "exact match for
14262 point" strategy of positioning the cursor. */
14263 /* Implementation note: bpos_max == pt_old when, e.g.,
14264 we are in an empty line, where bpos_max is set to
14265 MATRIX_ROW_START_CHARPOS, see above. */
14266 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14267 {
14268 cursor = glyph;
14269 break;
14270 }
14271 }
14272
14273 string_seen = 1;
14274 }
14275 x += glyph->pixel_width;
14276 ++glyph;
14277 }
14278 else if (glyph > end) /* row is reversed */
14279 while (!INTEGERP (glyph->object))
14280 {
14281 if (BUFFERP (glyph->object))
14282 {
14283 ptrdiff_t dpos = glyph->charpos - pt_old;
14284
14285 if (glyph->charpos > bpos_max)
14286 bpos_max = glyph->charpos;
14287 if (glyph->charpos < bpos_min)
14288 bpos_min = glyph->charpos;
14289 if (!glyph->avoid_cursor_p)
14290 {
14291 if (dpos == 0)
14292 {
14293 match_with_avoid_cursor = 0;
14294 break;
14295 }
14296 if (0 > dpos && dpos > pos_before - pt_old)
14297 {
14298 pos_before = glyph->charpos;
14299 glyph_before = glyph;
14300 }
14301 else if (0 < dpos && dpos < pos_after - pt_old)
14302 {
14303 pos_after = glyph->charpos;
14304 glyph_after = glyph;
14305 }
14306 }
14307 else if (dpos == 0)
14308 match_with_avoid_cursor = 1;
14309 }
14310 else if (STRINGP (glyph->object))
14311 {
14312 Lisp_Object chprop;
14313 ptrdiff_t glyph_pos = glyph->charpos;
14314
14315 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14316 glyph->object);
14317 if (!NILP (chprop))
14318 {
14319 ptrdiff_t prop_pos =
14320 string_buffer_position_lim (glyph->object, pos_before,
14321 pos_after, 0);
14322
14323 if (prop_pos >= pos_before)
14324 bpos_max = prop_pos - 1;
14325 }
14326 if (INTEGERP (chprop))
14327 {
14328 bpos_covered = bpos_max + XINT (chprop);
14329 /* If the `cursor' property covers buffer positions up
14330 to and including point, we should display cursor on
14331 this glyph. */
14332 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14333 {
14334 cursor = glyph;
14335 break;
14336 }
14337 }
14338 string_seen = 1;
14339 }
14340 --glyph;
14341 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14342 {
14343 x--; /* can't use any pixel_width */
14344 break;
14345 }
14346 x -= glyph->pixel_width;
14347 }
14348
14349 /* Step 2: If we didn't find an exact match for point, we need to
14350 look for a proper place to put the cursor among glyphs between
14351 GLYPH_BEFORE and GLYPH_AFTER. */
14352 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14353 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14354 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14355 {
14356 /* An empty line has a single glyph whose OBJECT is zero and
14357 whose CHARPOS is the position of a newline on that line.
14358 Note that on a TTY, there are more glyphs after that, which
14359 were produced by extend_face_to_end_of_line, but their
14360 CHARPOS is zero or negative. */
14361 int empty_line_p =
14362 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14363 && INTEGERP (glyph->object) && glyph->charpos > 0
14364 /* On a TTY, continued and truncated rows also have a glyph at
14365 their end whose OBJECT is zero and whose CHARPOS is
14366 positive (the continuation and truncation glyphs), but such
14367 rows are obviously not "empty". */
14368 && !(row->continued_p || row->truncated_on_right_p);
14369
14370 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14371 {
14372 ptrdiff_t ellipsis_pos;
14373
14374 /* Scan back over the ellipsis glyphs. */
14375 if (!row->reversed_p)
14376 {
14377 ellipsis_pos = (glyph - 1)->charpos;
14378 while (glyph > row->glyphs[TEXT_AREA]
14379 && (glyph - 1)->charpos == ellipsis_pos)
14380 glyph--, x -= glyph->pixel_width;
14381 /* That loop always goes one position too far, including
14382 the glyph before the ellipsis. So scan forward over
14383 that one. */
14384 x += glyph->pixel_width;
14385 glyph++;
14386 }
14387 else /* row is reversed */
14388 {
14389 ellipsis_pos = (glyph + 1)->charpos;
14390 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14391 && (glyph + 1)->charpos == ellipsis_pos)
14392 glyph++, x += glyph->pixel_width;
14393 x -= glyph->pixel_width;
14394 glyph--;
14395 }
14396 }
14397 else if (match_with_avoid_cursor)
14398 {
14399 cursor = glyph_after;
14400 x = -1;
14401 }
14402 else if (string_seen)
14403 {
14404 int incr = row->reversed_p ? -1 : +1;
14405
14406 /* Need to find the glyph that came out of a string which is
14407 present at point. That glyph is somewhere between
14408 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14409 positioned between POS_BEFORE and POS_AFTER in the
14410 buffer. */
14411 struct glyph *start, *stop;
14412 ptrdiff_t pos = pos_before;
14413
14414 x = -1;
14415
14416 /* If the row ends in a newline from a display string,
14417 reordering could have moved the glyphs belonging to the
14418 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14419 in this case we extend the search to the last glyph in
14420 the row that was not inserted by redisplay. */
14421 if (row->ends_in_newline_from_string_p)
14422 {
14423 glyph_after = end;
14424 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14425 }
14426
14427 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14428 correspond to POS_BEFORE and POS_AFTER, respectively. We
14429 need START and STOP in the order that corresponds to the
14430 row's direction as given by its reversed_p flag. If the
14431 directionality of characters between POS_BEFORE and
14432 POS_AFTER is the opposite of the row's base direction,
14433 these characters will have been reordered for display,
14434 and we need to reverse START and STOP. */
14435 if (!row->reversed_p)
14436 {
14437 start = min (glyph_before, glyph_after);
14438 stop = max (glyph_before, glyph_after);
14439 }
14440 else
14441 {
14442 start = max (glyph_before, glyph_after);
14443 stop = min (glyph_before, glyph_after);
14444 }
14445 for (glyph = start + incr;
14446 row->reversed_p ? glyph > stop : glyph < stop; )
14447 {
14448
14449 /* Any glyphs that come from the buffer are here because
14450 of bidi reordering. Skip them, and only pay
14451 attention to glyphs that came from some string. */
14452 if (STRINGP (glyph->object))
14453 {
14454 Lisp_Object str;
14455 ptrdiff_t tem;
14456 /* If the display property covers the newline, we
14457 need to search for it one position farther. */
14458 ptrdiff_t lim = pos_after
14459 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14460
14461 string_from_text_prop = 0;
14462 str = glyph->object;
14463 tem = string_buffer_position_lim (str, pos, lim, 0);
14464 if (tem == 0 /* from overlay */
14465 || pos <= tem)
14466 {
14467 /* If the string from which this glyph came is
14468 found in the buffer at point, or at position
14469 that is closer to point than pos_after, then
14470 we've found the glyph we've been looking for.
14471 If it comes from an overlay (tem == 0), and
14472 it has the `cursor' property on one of its
14473 glyphs, record that glyph as a candidate for
14474 displaying the cursor. (As in the
14475 unidirectional version, we will display the
14476 cursor on the last candidate we find.) */
14477 if (tem == 0
14478 || tem == pt_old
14479 || (tem - pt_old > 0 && tem < pos_after))
14480 {
14481 /* The glyphs from this string could have
14482 been reordered. Find the one with the
14483 smallest string position. Or there could
14484 be a character in the string with the
14485 `cursor' property, which means display
14486 cursor on that character's glyph. */
14487 ptrdiff_t strpos = glyph->charpos;
14488
14489 if (tem)
14490 {
14491 cursor = glyph;
14492 string_from_text_prop = 1;
14493 }
14494 for ( ;
14495 (row->reversed_p ? glyph > stop : glyph < stop)
14496 && EQ (glyph->object, str);
14497 glyph += incr)
14498 {
14499 Lisp_Object cprop;
14500 ptrdiff_t gpos = glyph->charpos;
14501
14502 cprop = Fget_char_property (make_number (gpos),
14503 Qcursor,
14504 glyph->object);
14505 if (!NILP (cprop))
14506 {
14507 cursor = glyph;
14508 break;
14509 }
14510 if (tem && glyph->charpos < strpos)
14511 {
14512 strpos = glyph->charpos;
14513 cursor = glyph;
14514 }
14515 }
14516
14517 if (tem == pt_old
14518 || (tem - pt_old > 0 && tem < pos_after))
14519 goto compute_x;
14520 }
14521 if (tem)
14522 pos = tem + 1; /* don't find previous instances */
14523 }
14524 /* This string is not what we want; skip all of the
14525 glyphs that came from it. */
14526 while ((row->reversed_p ? glyph > stop : glyph < stop)
14527 && EQ (glyph->object, str))
14528 glyph += incr;
14529 }
14530 else
14531 glyph += incr;
14532 }
14533
14534 /* If we reached the end of the line, and END was from a string,
14535 the cursor is not on this line. */
14536 if (cursor == NULL
14537 && (row->reversed_p ? glyph <= end : glyph >= end)
14538 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14539 && STRINGP (end->object)
14540 && row->continued_p)
14541 return 0;
14542 }
14543 /* A truncated row may not include PT among its character positions.
14544 Setting the cursor inside the scroll margin will trigger
14545 recalculation of hscroll in hscroll_window_tree. But if a
14546 display string covers point, defer to the string-handling
14547 code below to figure this out. */
14548 else if (row->truncated_on_left_p && pt_old < bpos_min)
14549 {
14550 cursor = glyph_before;
14551 x = -1;
14552 }
14553 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14554 /* Zero-width characters produce no glyphs. */
14555 || (!empty_line_p
14556 && (row->reversed_p
14557 ? glyph_after > glyphs_end
14558 : glyph_after < glyphs_end)))
14559 {
14560 cursor = glyph_after;
14561 x = -1;
14562 }
14563 }
14564
14565 compute_x:
14566 if (cursor != NULL)
14567 glyph = cursor;
14568 else if (glyph == glyphs_end
14569 && pos_before == pos_after
14570 && STRINGP ((row->reversed_p
14571 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14572 : row->glyphs[TEXT_AREA])->object))
14573 {
14574 /* If all the glyphs of this row came from strings, put the
14575 cursor on the first glyph of the row. This avoids having the
14576 cursor outside of the text area in this very rare and hard
14577 use case. */
14578 glyph =
14579 row->reversed_p
14580 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14581 : row->glyphs[TEXT_AREA];
14582 }
14583 if (x < 0)
14584 {
14585 struct glyph *g;
14586
14587 /* Need to compute x that corresponds to GLYPH. */
14588 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14589 {
14590 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14591 emacs_abort ();
14592 x += g->pixel_width;
14593 }
14594 }
14595
14596 /* ROW could be part of a continued line, which, under bidi
14597 reordering, might have other rows whose start and end charpos
14598 occlude point. Only set w->cursor if we found a better
14599 approximation to the cursor position than we have from previously
14600 examined candidate rows belonging to the same continued line. */
14601 if (/* We already have a candidate row. */
14602 w->cursor.vpos >= 0
14603 /* That candidate is not the row we are processing. */
14604 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14605 /* Make sure cursor.vpos specifies a row whose start and end
14606 charpos occlude point, and it is valid candidate for being a
14607 cursor-row. This is because some callers of this function
14608 leave cursor.vpos at the row where the cursor was displayed
14609 during the last redisplay cycle. */
14610 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14611 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14612 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14613 {
14614 struct glyph *g1
14615 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14616
14617 /* Don't consider glyphs that are outside TEXT_AREA. */
14618 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14619 return 0;
14620 /* Keep the candidate whose buffer position is the closest to
14621 point or has the `cursor' property. */
14622 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14623 w->cursor.hpos >= 0
14624 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14625 && ((BUFFERP (g1->object)
14626 && (g1->charpos == pt_old /* An exact match always wins. */
14627 || (BUFFERP (glyph->object)
14628 && eabs (g1->charpos - pt_old)
14629 < eabs (glyph->charpos - pt_old))))
14630 /* Previous candidate is a glyph from a string that has
14631 a non-nil `cursor' property. */
14632 || (STRINGP (g1->object)
14633 && (!NILP (Fget_char_property (make_number (g1->charpos),
14634 Qcursor, g1->object))
14635 /* Previous candidate is from the same display
14636 string as this one, and the display string
14637 came from a text property. */
14638 || (EQ (g1->object, glyph->object)
14639 && string_from_text_prop)
14640 /* this candidate is from newline and its
14641 position is not an exact match */
14642 || (INTEGERP (glyph->object)
14643 && glyph->charpos != pt_old)))))
14644 return 0;
14645 /* If this candidate gives an exact match, use that. */
14646 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14647 /* If this candidate is a glyph created for the
14648 terminating newline of a line, and point is on that
14649 newline, it wins because it's an exact match. */
14650 || (!row->continued_p
14651 && INTEGERP (glyph->object)
14652 && glyph->charpos == 0
14653 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14654 /* Otherwise, keep the candidate that comes from a row
14655 spanning less buffer positions. This may win when one or
14656 both candidate positions are on glyphs that came from
14657 display strings, for which we cannot compare buffer
14658 positions. */
14659 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14660 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14661 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14662 return 0;
14663 }
14664 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14665 w->cursor.x = x;
14666 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14667 w->cursor.y = row->y + dy;
14668
14669 if (w == XWINDOW (selected_window))
14670 {
14671 if (!row->continued_p
14672 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14673 && row->x == 0)
14674 {
14675 this_line_buffer = XBUFFER (w->contents);
14676
14677 CHARPOS (this_line_start_pos)
14678 = MATRIX_ROW_START_CHARPOS (row) + delta;
14679 BYTEPOS (this_line_start_pos)
14680 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14681
14682 CHARPOS (this_line_end_pos)
14683 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14684 BYTEPOS (this_line_end_pos)
14685 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14686
14687 this_line_y = w->cursor.y;
14688 this_line_pixel_height = row->height;
14689 this_line_vpos = w->cursor.vpos;
14690 this_line_start_x = row->x;
14691 }
14692 else
14693 CHARPOS (this_line_start_pos) = 0;
14694 }
14695
14696 return 1;
14697 }
14698
14699
14700 /* Run window scroll functions, if any, for WINDOW with new window
14701 start STARTP. Sets the window start of WINDOW to that position.
14702
14703 We assume that the window's buffer is really current. */
14704
14705 static struct text_pos
14706 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14707 {
14708 struct window *w = XWINDOW (window);
14709 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14710
14711 eassert (current_buffer == XBUFFER (w->contents));
14712
14713 if (!NILP (Vwindow_scroll_functions))
14714 {
14715 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14716 make_number (CHARPOS (startp)));
14717 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14718 /* In case the hook functions switch buffers. */
14719 set_buffer_internal (XBUFFER (w->contents));
14720 }
14721
14722 return startp;
14723 }
14724
14725
14726 /* Make sure the line containing the cursor is fully visible.
14727 A value of 1 means there is nothing to be done.
14728 (Either the line is fully visible, or it cannot be made so,
14729 or we cannot tell.)
14730
14731 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14732 is higher than window.
14733
14734 A value of 0 means the caller should do scrolling
14735 as if point had gone off the screen. */
14736
14737 static int
14738 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14739 {
14740 struct glyph_matrix *matrix;
14741 struct glyph_row *row;
14742 int window_height;
14743
14744 if (!make_cursor_line_fully_visible_p)
14745 return 1;
14746
14747 /* It's not always possible to find the cursor, e.g, when a window
14748 is full of overlay strings. Don't do anything in that case. */
14749 if (w->cursor.vpos < 0)
14750 return 1;
14751
14752 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14753 row = MATRIX_ROW (matrix, w->cursor.vpos);
14754
14755 /* If the cursor row is not partially visible, there's nothing to do. */
14756 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14757 return 1;
14758
14759 /* If the row the cursor is in is taller than the window's height,
14760 it's not clear what to do, so do nothing. */
14761 window_height = window_box_height (w);
14762 if (row->height >= window_height)
14763 {
14764 if (!force_p || MINI_WINDOW_P (w)
14765 || w->vscroll || w->cursor.vpos == 0)
14766 return 1;
14767 }
14768 return 0;
14769 }
14770
14771
14772 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14773 non-zero means only WINDOW is redisplayed in redisplay_internal.
14774 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14775 in redisplay_window to bring a partially visible line into view in
14776 the case that only the cursor has moved.
14777
14778 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14779 last screen line's vertical height extends past the end of the screen.
14780
14781 Value is
14782
14783 1 if scrolling succeeded
14784
14785 0 if scrolling didn't find point.
14786
14787 -1 if new fonts have been loaded so that we must interrupt
14788 redisplay, adjust glyph matrices, and try again. */
14789
14790 enum
14791 {
14792 SCROLLING_SUCCESS,
14793 SCROLLING_FAILED,
14794 SCROLLING_NEED_LARGER_MATRICES
14795 };
14796
14797 /* If scroll-conservatively is more than this, never recenter.
14798
14799 If you change this, don't forget to update the doc string of
14800 `scroll-conservatively' and the Emacs manual. */
14801 #define SCROLL_LIMIT 100
14802
14803 static int
14804 try_scrolling (Lisp_Object window, int just_this_one_p,
14805 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14806 int temp_scroll_step, int last_line_misfit)
14807 {
14808 struct window *w = XWINDOW (window);
14809 struct frame *f = XFRAME (w->frame);
14810 struct text_pos pos, startp;
14811 struct it it;
14812 int this_scroll_margin, scroll_max, rc, height;
14813 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14814 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14815 Lisp_Object aggressive;
14816 /* We will never try scrolling more than this number of lines. */
14817 int scroll_limit = SCROLL_LIMIT;
14818 int frame_line_height = default_line_pixel_height (w);
14819 int window_total_lines
14820 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14821
14822 #ifdef GLYPH_DEBUG
14823 debug_method_add (w, "try_scrolling");
14824 #endif
14825
14826 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14827
14828 /* Compute scroll margin height in pixels. We scroll when point is
14829 within this distance from the top or bottom of the window. */
14830 if (scroll_margin > 0)
14831 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14832 * frame_line_height;
14833 else
14834 this_scroll_margin = 0;
14835
14836 /* Force arg_scroll_conservatively to have a reasonable value, to
14837 avoid scrolling too far away with slow move_it_* functions. Note
14838 that the user can supply scroll-conservatively equal to
14839 `most-positive-fixnum', which can be larger than INT_MAX. */
14840 if (arg_scroll_conservatively > scroll_limit)
14841 {
14842 arg_scroll_conservatively = scroll_limit + 1;
14843 scroll_max = scroll_limit * frame_line_height;
14844 }
14845 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14846 /* Compute how much we should try to scroll maximally to bring
14847 point into view. */
14848 scroll_max = (max (scroll_step,
14849 max (arg_scroll_conservatively, temp_scroll_step))
14850 * frame_line_height);
14851 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14852 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14853 /* We're trying to scroll because of aggressive scrolling but no
14854 scroll_step is set. Choose an arbitrary one. */
14855 scroll_max = 10 * frame_line_height;
14856 else
14857 scroll_max = 0;
14858
14859 too_near_end:
14860
14861 /* Decide whether to scroll down. */
14862 if (PT > CHARPOS (startp))
14863 {
14864 int scroll_margin_y;
14865
14866 /* Compute the pixel ypos of the scroll margin, then move IT to
14867 either that ypos or PT, whichever comes first. */
14868 start_display (&it, w, startp);
14869 scroll_margin_y = it.last_visible_y - this_scroll_margin
14870 - frame_line_height * extra_scroll_margin_lines;
14871 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14872 (MOVE_TO_POS | MOVE_TO_Y));
14873
14874 if (PT > CHARPOS (it.current.pos))
14875 {
14876 int y0 = line_bottom_y (&it);
14877 /* Compute how many pixels below window bottom to stop searching
14878 for PT. This avoids costly search for PT that is far away if
14879 the user limited scrolling by a small number of lines, but
14880 always finds PT if scroll_conservatively is set to a large
14881 number, such as most-positive-fixnum. */
14882 int slack = max (scroll_max, 10 * frame_line_height);
14883 int y_to_move = it.last_visible_y + slack;
14884
14885 /* Compute the distance from the scroll margin to PT or to
14886 the scroll limit, whichever comes first. This should
14887 include the height of the cursor line, to make that line
14888 fully visible. */
14889 move_it_to (&it, PT, -1, y_to_move,
14890 -1, MOVE_TO_POS | MOVE_TO_Y);
14891 dy = line_bottom_y (&it) - y0;
14892
14893 if (dy > scroll_max)
14894 return SCROLLING_FAILED;
14895
14896 if (dy > 0)
14897 scroll_down_p = 1;
14898 }
14899 }
14900
14901 if (scroll_down_p)
14902 {
14903 /* Point is in or below the bottom scroll margin, so move the
14904 window start down. If scrolling conservatively, move it just
14905 enough down to make point visible. If scroll_step is set,
14906 move it down by scroll_step. */
14907 if (arg_scroll_conservatively)
14908 amount_to_scroll
14909 = min (max (dy, frame_line_height),
14910 frame_line_height * arg_scroll_conservatively);
14911 else if (scroll_step || temp_scroll_step)
14912 amount_to_scroll = scroll_max;
14913 else
14914 {
14915 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14916 height = WINDOW_BOX_TEXT_HEIGHT (w);
14917 if (NUMBERP (aggressive))
14918 {
14919 double float_amount = XFLOATINT (aggressive) * height;
14920 int aggressive_scroll = float_amount;
14921 if (aggressive_scroll == 0 && float_amount > 0)
14922 aggressive_scroll = 1;
14923 /* Don't let point enter the scroll margin near top of
14924 the window. This could happen if the value of
14925 scroll_up_aggressively is too large and there are
14926 non-zero margins, because scroll_up_aggressively
14927 means put point that fraction of window height
14928 _from_the_bottom_margin_. */
14929 if (aggressive_scroll + 2*this_scroll_margin > height)
14930 aggressive_scroll = height - 2*this_scroll_margin;
14931 amount_to_scroll = dy + aggressive_scroll;
14932 }
14933 }
14934
14935 if (amount_to_scroll <= 0)
14936 return SCROLLING_FAILED;
14937
14938 start_display (&it, w, startp);
14939 if (arg_scroll_conservatively <= scroll_limit)
14940 move_it_vertically (&it, amount_to_scroll);
14941 else
14942 {
14943 /* Extra precision for users who set scroll-conservatively
14944 to a large number: make sure the amount we scroll
14945 the window start is never less than amount_to_scroll,
14946 which was computed as distance from window bottom to
14947 point. This matters when lines at window top and lines
14948 below window bottom have different height. */
14949 struct it it1;
14950 void *it1data = NULL;
14951 /* We use a temporary it1 because line_bottom_y can modify
14952 its argument, if it moves one line down; see there. */
14953 int start_y;
14954
14955 SAVE_IT (it1, it, it1data);
14956 start_y = line_bottom_y (&it1);
14957 do {
14958 RESTORE_IT (&it, &it, it1data);
14959 move_it_by_lines (&it, 1);
14960 SAVE_IT (it1, it, it1data);
14961 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14962 }
14963
14964 /* If STARTP is unchanged, move it down another screen line. */
14965 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14966 move_it_by_lines (&it, 1);
14967 startp = it.current.pos;
14968 }
14969 else
14970 {
14971 struct text_pos scroll_margin_pos = startp;
14972 int y_offset = 0;
14973
14974 /* See if point is inside the scroll margin at the top of the
14975 window. */
14976 if (this_scroll_margin)
14977 {
14978 int y_start;
14979
14980 start_display (&it, w, startp);
14981 y_start = it.current_y;
14982 move_it_vertically (&it, this_scroll_margin);
14983 scroll_margin_pos = it.current.pos;
14984 /* If we didn't move enough before hitting ZV, request
14985 additional amount of scroll, to move point out of the
14986 scroll margin. */
14987 if (IT_CHARPOS (it) == ZV
14988 && it.current_y - y_start < this_scroll_margin)
14989 y_offset = this_scroll_margin - (it.current_y - y_start);
14990 }
14991
14992 if (PT < CHARPOS (scroll_margin_pos))
14993 {
14994 /* Point is in the scroll margin at the top of the window or
14995 above what is displayed in the window. */
14996 int y0, y_to_move;
14997
14998 /* Compute the vertical distance from PT to the scroll
14999 margin position. Move as far as scroll_max allows, or
15000 one screenful, or 10 screen lines, whichever is largest.
15001 Give up if distance is greater than scroll_max or if we
15002 didn't reach the scroll margin position. */
15003 SET_TEXT_POS (pos, PT, PT_BYTE);
15004 start_display (&it, w, pos);
15005 y0 = it.current_y;
15006 y_to_move = max (it.last_visible_y,
15007 max (scroll_max, 10 * frame_line_height));
15008 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15009 y_to_move, -1,
15010 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15011 dy = it.current_y - y0;
15012 if (dy > scroll_max
15013 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15014 return SCROLLING_FAILED;
15015
15016 /* Additional scroll for when ZV was too close to point. */
15017 dy += y_offset;
15018
15019 /* Compute new window start. */
15020 start_display (&it, w, startp);
15021
15022 if (arg_scroll_conservatively)
15023 amount_to_scroll = max (dy, frame_line_height *
15024 max (scroll_step, temp_scroll_step));
15025 else if (scroll_step || temp_scroll_step)
15026 amount_to_scroll = scroll_max;
15027 else
15028 {
15029 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15030 height = WINDOW_BOX_TEXT_HEIGHT (w);
15031 if (NUMBERP (aggressive))
15032 {
15033 double float_amount = XFLOATINT (aggressive) * height;
15034 int aggressive_scroll = float_amount;
15035 if (aggressive_scroll == 0 && float_amount > 0)
15036 aggressive_scroll = 1;
15037 /* Don't let point enter the scroll margin near
15038 bottom of the window, if the value of
15039 scroll_down_aggressively happens to be too
15040 large. */
15041 if (aggressive_scroll + 2*this_scroll_margin > height)
15042 aggressive_scroll = height - 2*this_scroll_margin;
15043 amount_to_scroll = dy + aggressive_scroll;
15044 }
15045 }
15046
15047 if (amount_to_scroll <= 0)
15048 return SCROLLING_FAILED;
15049
15050 move_it_vertically_backward (&it, amount_to_scroll);
15051 startp = it.current.pos;
15052 }
15053 }
15054
15055 /* Run window scroll functions. */
15056 startp = run_window_scroll_functions (window, startp);
15057
15058 /* Display the window. Give up if new fonts are loaded, or if point
15059 doesn't appear. */
15060 if (!try_window (window, startp, 0))
15061 rc = SCROLLING_NEED_LARGER_MATRICES;
15062 else if (w->cursor.vpos < 0)
15063 {
15064 clear_glyph_matrix (w->desired_matrix);
15065 rc = SCROLLING_FAILED;
15066 }
15067 else
15068 {
15069 /* Maybe forget recorded base line for line number display. */
15070 if (!just_this_one_p
15071 || current_buffer->clip_changed
15072 || BEG_UNCHANGED < CHARPOS (startp))
15073 w->base_line_number = 0;
15074
15075 /* If cursor ends up on a partially visible line,
15076 treat that as being off the bottom of the screen. */
15077 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15078 /* It's possible that the cursor is on the first line of the
15079 buffer, which is partially obscured due to a vscroll
15080 (Bug#7537). In that case, avoid looping forever. */
15081 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15082 {
15083 clear_glyph_matrix (w->desired_matrix);
15084 ++extra_scroll_margin_lines;
15085 goto too_near_end;
15086 }
15087 rc = SCROLLING_SUCCESS;
15088 }
15089
15090 return rc;
15091 }
15092
15093
15094 /* Compute a suitable window start for window W if display of W starts
15095 on a continuation line. Value is non-zero if a new window start
15096 was computed.
15097
15098 The new window start will be computed, based on W's width, starting
15099 from the start of the continued line. It is the start of the
15100 screen line with the minimum distance from the old start W->start. */
15101
15102 static int
15103 compute_window_start_on_continuation_line (struct window *w)
15104 {
15105 struct text_pos pos, start_pos;
15106 int window_start_changed_p = 0;
15107
15108 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15109
15110 /* If window start is on a continuation line... Window start may be
15111 < BEGV in case there's invisible text at the start of the
15112 buffer (M-x rmail, for example). */
15113 if (CHARPOS (start_pos) > BEGV
15114 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15115 {
15116 struct it it;
15117 struct glyph_row *row;
15118
15119 /* Handle the case that the window start is out of range. */
15120 if (CHARPOS (start_pos) < BEGV)
15121 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15122 else if (CHARPOS (start_pos) > ZV)
15123 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15124
15125 /* Find the start of the continued line. This should be fast
15126 because find_newline is fast (newline cache). */
15127 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15128 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15129 row, DEFAULT_FACE_ID);
15130 reseat_at_previous_visible_line_start (&it);
15131
15132 /* If the line start is "too far" away from the window start,
15133 say it takes too much time to compute a new window start. */
15134 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15135 /* PXW: Do we need upper bounds here? */
15136 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15137 {
15138 int min_distance, distance;
15139
15140 /* Move forward by display lines to find the new window
15141 start. If window width was enlarged, the new start can
15142 be expected to be > the old start. If window width was
15143 decreased, the new window start will be < the old start.
15144 So, we're looking for the display line start with the
15145 minimum distance from the old window start. */
15146 pos = it.current.pos;
15147 min_distance = INFINITY;
15148 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15149 distance < min_distance)
15150 {
15151 min_distance = distance;
15152 pos = it.current.pos;
15153 if (it.line_wrap == WORD_WRAP)
15154 {
15155 /* Under WORD_WRAP, move_it_by_lines is likely to
15156 overshoot and stop not at the first, but the
15157 second character from the left margin. So in
15158 that case, we need a more tight control on the X
15159 coordinate of the iterator than move_it_by_lines
15160 promises in its contract. The method is to first
15161 go to the last (rightmost) visible character of a
15162 line, then move to the leftmost character on the
15163 next line in a separate call. */
15164 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15165 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15166 move_it_to (&it, ZV, 0,
15167 it.current_y + it.max_ascent + it.max_descent, -1,
15168 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15169 }
15170 else
15171 move_it_by_lines (&it, 1);
15172 }
15173
15174 /* Set the window start there. */
15175 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15176 window_start_changed_p = 1;
15177 }
15178 }
15179
15180 return window_start_changed_p;
15181 }
15182
15183
15184 /* Try cursor movement in case text has not changed in window WINDOW,
15185 with window start STARTP. Value is
15186
15187 CURSOR_MOVEMENT_SUCCESS if successful
15188
15189 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15190
15191 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15192 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15193 we want to scroll as if scroll-step were set to 1. See the code.
15194
15195 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15196 which case we have to abort this redisplay, and adjust matrices
15197 first. */
15198
15199 enum
15200 {
15201 CURSOR_MOVEMENT_SUCCESS,
15202 CURSOR_MOVEMENT_CANNOT_BE_USED,
15203 CURSOR_MOVEMENT_MUST_SCROLL,
15204 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15205 };
15206
15207 static int
15208 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15209 {
15210 struct window *w = XWINDOW (window);
15211 struct frame *f = XFRAME (w->frame);
15212 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15213
15214 #ifdef GLYPH_DEBUG
15215 if (inhibit_try_cursor_movement)
15216 return rc;
15217 #endif
15218
15219 /* Previously, there was a check for Lisp integer in the
15220 if-statement below. Now, this field is converted to
15221 ptrdiff_t, thus zero means invalid position in a buffer. */
15222 eassert (w->last_point > 0);
15223 /* Likewise there was a check whether window_end_vpos is nil or larger
15224 than the window. Now window_end_vpos is int and so never nil, but
15225 let's leave eassert to check whether it fits in the window. */
15226 eassert (w->window_end_vpos < w->current_matrix->nrows);
15227
15228 /* Handle case where text has not changed, only point, and it has
15229 not moved off the frame. */
15230 if (/* Point may be in this window. */
15231 PT >= CHARPOS (startp)
15232 /* Selective display hasn't changed. */
15233 && !current_buffer->clip_changed
15234 /* Function force-mode-line-update is used to force a thorough
15235 redisplay. It sets either windows_or_buffers_changed or
15236 update_mode_lines. So don't take a shortcut here for these
15237 cases. */
15238 && !update_mode_lines
15239 && !windows_or_buffers_changed
15240 && !f->cursor_type_changed
15241 && NILP (Vshow_trailing_whitespace)
15242 /* This code is not used for mini-buffer for the sake of the case
15243 of redisplaying to replace an echo area message; since in
15244 that case the mini-buffer contents per se are usually
15245 unchanged. This code is of no real use in the mini-buffer
15246 since the handling of this_line_start_pos, etc., in redisplay
15247 handles the same cases. */
15248 && !EQ (window, minibuf_window)
15249 && (FRAME_WINDOW_P (f)
15250 || !overlay_arrow_in_current_buffer_p ()))
15251 {
15252 int this_scroll_margin, top_scroll_margin;
15253 struct glyph_row *row = NULL;
15254 int frame_line_height = default_line_pixel_height (w);
15255 int window_total_lines
15256 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15257
15258 #ifdef GLYPH_DEBUG
15259 debug_method_add (w, "cursor movement");
15260 #endif
15261
15262 /* Scroll if point within this distance from the top or bottom
15263 of the window. This is a pixel value. */
15264 if (scroll_margin > 0)
15265 {
15266 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15267 this_scroll_margin *= frame_line_height;
15268 }
15269 else
15270 this_scroll_margin = 0;
15271
15272 top_scroll_margin = this_scroll_margin;
15273 if (WINDOW_WANTS_HEADER_LINE_P (w))
15274 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15275
15276 /* Start with the row the cursor was displayed during the last
15277 not paused redisplay. Give up if that row is not valid. */
15278 if (w->last_cursor_vpos < 0
15279 || w->last_cursor_vpos >= w->current_matrix->nrows)
15280 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15281 else
15282 {
15283 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15284 if (row->mode_line_p)
15285 ++row;
15286 if (!row->enabled_p)
15287 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15288 }
15289
15290 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15291 {
15292 int scroll_p = 0, must_scroll = 0;
15293 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15294
15295 if (PT > w->last_point)
15296 {
15297 /* Point has moved forward. */
15298 while (MATRIX_ROW_END_CHARPOS (row) < PT
15299 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15300 {
15301 eassert (row->enabled_p);
15302 ++row;
15303 }
15304
15305 /* If the end position of a row equals the start
15306 position of the next row, and PT is at that position,
15307 we would rather display cursor in the next line. */
15308 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15309 && MATRIX_ROW_END_CHARPOS (row) == PT
15310 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15311 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15312 && !cursor_row_p (row))
15313 ++row;
15314
15315 /* If within the scroll margin, scroll. Note that
15316 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15317 the next line would be drawn, and that
15318 this_scroll_margin can be zero. */
15319 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15320 || PT > MATRIX_ROW_END_CHARPOS (row)
15321 /* Line is completely visible last line in window
15322 and PT is to be set in the next line. */
15323 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15324 && PT == MATRIX_ROW_END_CHARPOS (row)
15325 && !row->ends_at_zv_p
15326 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15327 scroll_p = 1;
15328 }
15329 else if (PT < w->last_point)
15330 {
15331 /* Cursor has to be moved backward. Note that PT >=
15332 CHARPOS (startp) because of the outer if-statement. */
15333 while (!row->mode_line_p
15334 && (MATRIX_ROW_START_CHARPOS (row) > PT
15335 || (MATRIX_ROW_START_CHARPOS (row) == PT
15336 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15337 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15338 row > w->current_matrix->rows
15339 && (row-1)->ends_in_newline_from_string_p))))
15340 && (row->y > top_scroll_margin
15341 || CHARPOS (startp) == BEGV))
15342 {
15343 eassert (row->enabled_p);
15344 --row;
15345 }
15346
15347 /* Consider the following case: Window starts at BEGV,
15348 there is invisible, intangible text at BEGV, so that
15349 display starts at some point START > BEGV. It can
15350 happen that we are called with PT somewhere between
15351 BEGV and START. Try to handle that case. */
15352 if (row < w->current_matrix->rows
15353 || row->mode_line_p)
15354 {
15355 row = w->current_matrix->rows;
15356 if (row->mode_line_p)
15357 ++row;
15358 }
15359
15360 /* Due to newlines in overlay strings, we may have to
15361 skip forward over overlay strings. */
15362 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15363 && MATRIX_ROW_END_CHARPOS (row) == PT
15364 && !cursor_row_p (row))
15365 ++row;
15366
15367 /* If within the scroll margin, scroll. */
15368 if (row->y < top_scroll_margin
15369 && CHARPOS (startp) != BEGV)
15370 scroll_p = 1;
15371 }
15372 else
15373 {
15374 /* Cursor did not move. So don't scroll even if cursor line
15375 is partially visible, as it was so before. */
15376 rc = CURSOR_MOVEMENT_SUCCESS;
15377 }
15378
15379 if (PT < MATRIX_ROW_START_CHARPOS (row)
15380 || PT > MATRIX_ROW_END_CHARPOS (row))
15381 {
15382 /* if PT is not in the glyph row, give up. */
15383 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15384 must_scroll = 1;
15385 }
15386 else if (rc != CURSOR_MOVEMENT_SUCCESS
15387 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15388 {
15389 struct glyph_row *row1;
15390
15391 /* If rows are bidi-reordered and point moved, back up
15392 until we find a row that does not belong to a
15393 continuation line. This is because we must consider
15394 all rows of a continued line as candidates for the
15395 new cursor positioning, since row start and end
15396 positions change non-linearly with vertical position
15397 in such rows. */
15398 /* FIXME: Revisit this when glyph ``spilling'' in
15399 continuation lines' rows is implemented for
15400 bidi-reordered rows. */
15401 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15402 MATRIX_ROW_CONTINUATION_LINE_P (row);
15403 --row)
15404 {
15405 /* If we hit the beginning of the displayed portion
15406 without finding the first row of a continued
15407 line, give up. */
15408 if (row <= row1)
15409 {
15410 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15411 break;
15412 }
15413 eassert (row->enabled_p);
15414 }
15415 }
15416 if (must_scroll)
15417 ;
15418 else if (rc != CURSOR_MOVEMENT_SUCCESS
15419 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15420 /* Make sure this isn't a header line by any chance, since
15421 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15422 && !row->mode_line_p
15423 && make_cursor_line_fully_visible_p)
15424 {
15425 if (PT == MATRIX_ROW_END_CHARPOS (row)
15426 && !row->ends_at_zv_p
15427 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15428 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15429 else if (row->height > window_box_height (w))
15430 {
15431 /* If we end up in a partially visible line, let's
15432 make it fully visible, except when it's taller
15433 than the window, in which case we can't do much
15434 about it. */
15435 *scroll_step = 1;
15436 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15437 }
15438 else
15439 {
15440 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15441 if (!cursor_row_fully_visible_p (w, 0, 1))
15442 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15443 else
15444 rc = CURSOR_MOVEMENT_SUCCESS;
15445 }
15446 }
15447 else if (scroll_p)
15448 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15449 else if (rc != CURSOR_MOVEMENT_SUCCESS
15450 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15451 {
15452 /* With bidi-reordered rows, there could be more than
15453 one candidate row whose start and end positions
15454 occlude point. We need to let set_cursor_from_row
15455 find the best candidate. */
15456 /* FIXME: Revisit this when glyph ``spilling'' in
15457 continuation lines' rows is implemented for
15458 bidi-reordered rows. */
15459 int rv = 0;
15460
15461 do
15462 {
15463 int at_zv_p = 0, exact_match_p = 0;
15464
15465 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15466 && PT <= MATRIX_ROW_END_CHARPOS (row)
15467 && cursor_row_p (row))
15468 rv |= set_cursor_from_row (w, row, w->current_matrix,
15469 0, 0, 0, 0);
15470 /* As soon as we've found the exact match for point,
15471 or the first suitable row whose ends_at_zv_p flag
15472 is set, we are done. */
15473 at_zv_p =
15474 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15475 if (rv && !at_zv_p
15476 && w->cursor.hpos >= 0
15477 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15478 w->cursor.vpos))
15479 {
15480 struct glyph_row *candidate =
15481 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15482 struct glyph *g =
15483 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15484 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15485
15486 exact_match_p =
15487 (BUFFERP (g->object) && g->charpos == PT)
15488 || (INTEGERP (g->object)
15489 && (g->charpos == PT
15490 || (g->charpos == 0 && endpos - 1 == PT)));
15491 }
15492 if (rv && (at_zv_p || exact_match_p))
15493 {
15494 rc = CURSOR_MOVEMENT_SUCCESS;
15495 break;
15496 }
15497 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15498 break;
15499 ++row;
15500 }
15501 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15502 || row->continued_p)
15503 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15504 || (MATRIX_ROW_START_CHARPOS (row) == PT
15505 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15506 /* If we didn't find any candidate rows, or exited the
15507 loop before all the candidates were examined, signal
15508 to the caller that this method failed. */
15509 if (rc != CURSOR_MOVEMENT_SUCCESS
15510 && !(rv
15511 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15512 && !row->continued_p))
15513 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15514 else if (rv)
15515 rc = CURSOR_MOVEMENT_SUCCESS;
15516 }
15517 else
15518 {
15519 do
15520 {
15521 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15522 {
15523 rc = CURSOR_MOVEMENT_SUCCESS;
15524 break;
15525 }
15526 ++row;
15527 }
15528 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15529 && MATRIX_ROW_START_CHARPOS (row) == PT
15530 && cursor_row_p (row));
15531 }
15532 }
15533 }
15534
15535 return rc;
15536 }
15537
15538 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15539 static
15540 #endif
15541 void
15542 set_vertical_scroll_bar (struct window *w)
15543 {
15544 ptrdiff_t start, end, whole;
15545
15546 /* Calculate the start and end positions for the current window.
15547 At some point, it would be nice to choose between scrollbars
15548 which reflect the whole buffer size, with special markers
15549 indicating narrowing, and scrollbars which reflect only the
15550 visible region.
15551
15552 Note that mini-buffers sometimes aren't displaying any text. */
15553 if (!MINI_WINDOW_P (w)
15554 || (w == XWINDOW (minibuf_window)
15555 && NILP (echo_area_buffer[0])))
15556 {
15557 struct buffer *buf = XBUFFER (w->contents);
15558 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15559 start = marker_position (w->start) - BUF_BEGV (buf);
15560 /* I don't think this is guaranteed to be right. For the
15561 moment, we'll pretend it is. */
15562 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15563
15564 if (end < start)
15565 end = start;
15566 if (whole < (end - start))
15567 whole = end - start;
15568 }
15569 else
15570 start = end = whole = 0;
15571
15572 /* Indicate what this scroll bar ought to be displaying now. */
15573 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15574 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15575 (w, end - start, whole, start);
15576 }
15577
15578
15579 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15580 selected_window is redisplayed.
15581
15582 We can return without actually redisplaying the window if fonts has been
15583 changed on window's frame. In that case, redisplay_internal will retry. */
15584
15585 static void
15586 redisplay_window (Lisp_Object window, bool just_this_one_p)
15587 {
15588 struct window *w = XWINDOW (window);
15589 struct frame *f = XFRAME (w->frame);
15590 struct buffer *buffer = XBUFFER (w->contents);
15591 struct buffer *old = current_buffer;
15592 struct text_pos lpoint, opoint, startp;
15593 int update_mode_line;
15594 int tem;
15595 struct it it;
15596 /* Record it now because it's overwritten. */
15597 bool current_matrix_up_to_date_p = false;
15598 bool used_current_matrix_p = false;
15599 /* This is less strict than current_matrix_up_to_date_p.
15600 It indicates that the buffer contents and narrowing are unchanged. */
15601 bool buffer_unchanged_p = false;
15602 int temp_scroll_step = 0;
15603 ptrdiff_t count = SPECPDL_INDEX ();
15604 int rc;
15605 int centering_position = -1;
15606 int last_line_misfit = 0;
15607 ptrdiff_t beg_unchanged, end_unchanged;
15608 int frame_line_height;
15609
15610 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15611 opoint = lpoint;
15612
15613 #ifdef GLYPH_DEBUG
15614 *w->desired_matrix->method = 0;
15615 #endif
15616
15617 if (!just_this_one_p
15618 && REDISPLAY_SOME_P ()
15619 && !w->redisplay
15620 && !f->redisplay
15621 && !buffer->text->redisplay
15622 && BUF_PT (buffer) == w->last_point)
15623 return;
15624
15625 /* Make sure that both W's markers are valid. */
15626 eassert (XMARKER (w->start)->buffer == buffer);
15627 eassert (XMARKER (w->pointm)->buffer == buffer);
15628
15629 restart:
15630 reconsider_clip_changes (w);
15631 frame_line_height = default_line_pixel_height (w);
15632
15633 /* Has the mode line to be updated? */
15634 update_mode_line = (w->update_mode_line
15635 || update_mode_lines
15636 || buffer->clip_changed
15637 || buffer->prevent_redisplay_optimizations_p);
15638
15639 if (!just_this_one_p)
15640 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15641 cleverly elsewhere. */
15642 w->must_be_updated_p = true;
15643
15644 if (MINI_WINDOW_P (w))
15645 {
15646 if (w == XWINDOW (echo_area_window)
15647 && !NILP (echo_area_buffer[0]))
15648 {
15649 if (update_mode_line)
15650 /* We may have to update a tty frame's menu bar or a
15651 tool-bar. Example `M-x C-h C-h C-g'. */
15652 goto finish_menu_bars;
15653 else
15654 /* We've already displayed the echo area glyphs in this window. */
15655 goto finish_scroll_bars;
15656 }
15657 else if ((w != XWINDOW (minibuf_window)
15658 || minibuf_level == 0)
15659 /* When buffer is nonempty, redisplay window normally. */
15660 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15661 /* Quail displays non-mini buffers in minibuffer window.
15662 In that case, redisplay the window normally. */
15663 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15664 {
15665 /* W is a mini-buffer window, but it's not active, so clear
15666 it. */
15667 int yb = window_text_bottom_y (w);
15668 struct glyph_row *row;
15669 int y;
15670
15671 for (y = 0, row = w->desired_matrix->rows;
15672 y < yb;
15673 y += row->height, ++row)
15674 blank_row (w, row, y);
15675 goto finish_scroll_bars;
15676 }
15677
15678 clear_glyph_matrix (w->desired_matrix);
15679 }
15680
15681 /* Otherwise set up data on this window; select its buffer and point
15682 value. */
15683 /* Really select the buffer, for the sake of buffer-local
15684 variables. */
15685 set_buffer_internal_1 (XBUFFER (w->contents));
15686
15687 current_matrix_up_to_date_p
15688 = (w->window_end_valid
15689 && !current_buffer->clip_changed
15690 && !current_buffer->prevent_redisplay_optimizations_p
15691 && !window_outdated (w));
15692
15693 /* Run the window-bottom-change-functions
15694 if it is possible that the text on the screen has changed
15695 (either due to modification of the text, or any other reason). */
15696 if (!current_matrix_up_to_date_p
15697 && !NILP (Vwindow_text_change_functions))
15698 {
15699 safe_run_hooks (Qwindow_text_change_functions);
15700 goto restart;
15701 }
15702
15703 beg_unchanged = BEG_UNCHANGED;
15704 end_unchanged = END_UNCHANGED;
15705
15706 SET_TEXT_POS (opoint, PT, PT_BYTE);
15707
15708 specbind (Qinhibit_point_motion_hooks, Qt);
15709
15710 buffer_unchanged_p
15711 = (w->window_end_valid
15712 && !current_buffer->clip_changed
15713 && !window_outdated (w));
15714
15715 /* When windows_or_buffers_changed is non-zero, we can't rely
15716 on the window end being valid, so set it to zero there. */
15717 if (windows_or_buffers_changed)
15718 {
15719 /* If window starts on a continuation line, maybe adjust the
15720 window start in case the window's width changed. */
15721 if (XMARKER (w->start)->buffer == current_buffer)
15722 compute_window_start_on_continuation_line (w);
15723
15724 w->window_end_valid = false;
15725 /* If so, we also can't rely on current matrix
15726 and should not fool try_cursor_movement below. */
15727 current_matrix_up_to_date_p = false;
15728 }
15729
15730 /* Some sanity checks. */
15731 CHECK_WINDOW_END (w);
15732 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15733 emacs_abort ();
15734 if (BYTEPOS (opoint) < CHARPOS (opoint))
15735 emacs_abort ();
15736
15737 if (mode_line_update_needed (w))
15738 update_mode_line = 1;
15739
15740 /* Point refers normally to the selected window. For any other
15741 window, set up appropriate value. */
15742 if (!EQ (window, selected_window))
15743 {
15744 ptrdiff_t new_pt = marker_position (w->pointm);
15745 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15746 if (new_pt < BEGV)
15747 {
15748 new_pt = BEGV;
15749 new_pt_byte = BEGV_BYTE;
15750 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15751 }
15752 else if (new_pt > (ZV - 1))
15753 {
15754 new_pt = ZV;
15755 new_pt_byte = ZV_BYTE;
15756 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15757 }
15758
15759 /* We don't use SET_PT so that the point-motion hooks don't run. */
15760 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15761 }
15762
15763 /* If any of the character widths specified in the display table
15764 have changed, invalidate the width run cache. It's true that
15765 this may be a bit late to catch such changes, but the rest of
15766 redisplay goes (non-fatally) haywire when the display table is
15767 changed, so why should we worry about doing any better? */
15768 if (current_buffer->width_run_cache
15769 || (current_buffer->base_buffer
15770 && current_buffer->base_buffer->width_run_cache))
15771 {
15772 struct Lisp_Char_Table *disptab = buffer_display_table ();
15773
15774 if (! disptab_matches_widthtab
15775 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15776 {
15777 struct buffer *buf = current_buffer;
15778
15779 if (buf->base_buffer)
15780 buf = buf->base_buffer;
15781 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15782 recompute_width_table (current_buffer, disptab);
15783 }
15784 }
15785
15786 /* If window-start is screwed up, choose a new one. */
15787 if (XMARKER (w->start)->buffer != current_buffer)
15788 goto recenter;
15789
15790 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15791
15792 /* If someone specified a new starting point but did not insist,
15793 check whether it can be used. */
15794 if (w->optional_new_start
15795 && CHARPOS (startp) >= BEGV
15796 && CHARPOS (startp) <= ZV)
15797 {
15798 w->optional_new_start = 0;
15799 start_display (&it, w, startp);
15800 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15801 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15802 if (IT_CHARPOS (it) == PT)
15803 w->force_start = 1;
15804 /* IT may overshoot PT if text at PT is invisible. */
15805 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15806 w->force_start = 1;
15807 }
15808
15809 force_start:
15810
15811 /* Handle case where place to start displaying has been specified,
15812 unless the specified location is outside the accessible range. */
15813 if (w->force_start || window_frozen_p (w))
15814 {
15815 /* We set this later on if we have to adjust point. */
15816 int new_vpos = -1;
15817
15818 w->force_start = 0;
15819 w->vscroll = 0;
15820 w->window_end_valid = 0;
15821
15822 /* Forget any recorded base line for line number display. */
15823 if (!buffer_unchanged_p)
15824 w->base_line_number = 0;
15825
15826 /* Redisplay the mode line. Select the buffer properly for that.
15827 Also, run the hook window-scroll-functions
15828 because we have scrolled. */
15829 /* Note, we do this after clearing force_start because
15830 if there's an error, it is better to forget about force_start
15831 than to get into an infinite loop calling the hook functions
15832 and having them get more errors. */
15833 if (!update_mode_line
15834 || ! NILP (Vwindow_scroll_functions))
15835 {
15836 update_mode_line = 1;
15837 w->update_mode_line = 1;
15838 startp = run_window_scroll_functions (window, startp);
15839 }
15840
15841 if (CHARPOS (startp) < BEGV)
15842 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15843 else if (CHARPOS (startp) > ZV)
15844 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15845
15846 /* Redisplay, then check if cursor has been set during the
15847 redisplay. Give up if new fonts were loaded. */
15848 /* We used to issue a CHECK_MARGINS argument to try_window here,
15849 but this causes scrolling to fail when point begins inside
15850 the scroll margin (bug#148) -- cyd */
15851 if (!try_window (window, startp, 0))
15852 {
15853 w->force_start = 1;
15854 clear_glyph_matrix (w->desired_matrix);
15855 goto need_larger_matrices;
15856 }
15857
15858 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15859 {
15860 /* If point does not appear, try to move point so it does
15861 appear. The desired matrix has been built above, so we
15862 can use it here. */
15863 new_vpos = window_box_height (w) / 2;
15864 }
15865
15866 if (!cursor_row_fully_visible_p (w, 0, 0))
15867 {
15868 /* Point does appear, but on a line partly visible at end of window.
15869 Move it back to a fully-visible line. */
15870 new_vpos = window_box_height (w);
15871 }
15872 else if (w->cursor.vpos >= 0)
15873 {
15874 /* Some people insist on not letting point enter the scroll
15875 margin, even though this part handles windows that didn't
15876 scroll at all. */
15877 int window_total_lines
15878 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15879 int margin = min (scroll_margin, window_total_lines / 4);
15880 int pixel_margin = margin * frame_line_height;
15881 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15882
15883 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15884 below, which finds the row to move point to, advances by
15885 the Y coordinate of the _next_ row, see the definition of
15886 MATRIX_ROW_BOTTOM_Y. */
15887 if (w->cursor.vpos < margin + header_line)
15888 {
15889 w->cursor.vpos = -1;
15890 clear_glyph_matrix (w->desired_matrix);
15891 goto try_to_scroll;
15892 }
15893 else
15894 {
15895 int window_height = window_box_height (w);
15896
15897 if (header_line)
15898 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15899 if (w->cursor.y >= window_height - pixel_margin)
15900 {
15901 w->cursor.vpos = -1;
15902 clear_glyph_matrix (w->desired_matrix);
15903 goto try_to_scroll;
15904 }
15905 }
15906 }
15907
15908 /* If we need to move point for either of the above reasons,
15909 now actually do it. */
15910 if (new_vpos >= 0)
15911 {
15912 struct glyph_row *row;
15913
15914 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15915 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15916 ++row;
15917
15918 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15919 MATRIX_ROW_START_BYTEPOS (row));
15920
15921 if (w != XWINDOW (selected_window))
15922 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15923 else if (current_buffer == old)
15924 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15925
15926 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15927
15928 /* If we are highlighting the region, then we just changed
15929 the region, so redisplay to show it. */
15930 /* FIXME: We need to (re)run pre-redisplay-function! */
15931 /* if (markpos_of_region () >= 0)
15932 {
15933 clear_glyph_matrix (w->desired_matrix);
15934 if (!try_window (window, startp, 0))
15935 goto need_larger_matrices;
15936 }
15937 */
15938 }
15939
15940 #ifdef GLYPH_DEBUG
15941 debug_method_add (w, "forced window start");
15942 #endif
15943 goto done;
15944 }
15945
15946 /* Handle case where text has not changed, only point, and it has
15947 not moved off the frame, and we are not retrying after hscroll.
15948 (current_matrix_up_to_date_p is nonzero when retrying.) */
15949 if (current_matrix_up_to_date_p
15950 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15951 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15952 {
15953 switch (rc)
15954 {
15955 case CURSOR_MOVEMENT_SUCCESS:
15956 used_current_matrix_p = 1;
15957 goto done;
15958
15959 case CURSOR_MOVEMENT_MUST_SCROLL:
15960 goto try_to_scroll;
15961
15962 default:
15963 emacs_abort ();
15964 }
15965 }
15966 /* If current starting point was originally the beginning of a line
15967 but no longer is, find a new starting point. */
15968 else if (w->start_at_line_beg
15969 && !(CHARPOS (startp) <= BEGV
15970 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15971 {
15972 #ifdef GLYPH_DEBUG
15973 debug_method_add (w, "recenter 1");
15974 #endif
15975 goto recenter;
15976 }
15977
15978 /* Try scrolling with try_window_id. Value is > 0 if update has
15979 been done, it is -1 if we know that the same window start will
15980 not work. It is 0 if unsuccessful for some other reason. */
15981 else if ((tem = try_window_id (w)) != 0)
15982 {
15983 #ifdef GLYPH_DEBUG
15984 debug_method_add (w, "try_window_id %d", tem);
15985 #endif
15986
15987 if (f->fonts_changed)
15988 goto need_larger_matrices;
15989 if (tem > 0)
15990 goto done;
15991
15992 /* Otherwise try_window_id has returned -1 which means that we
15993 don't want the alternative below this comment to execute. */
15994 }
15995 else if (CHARPOS (startp) >= BEGV
15996 && CHARPOS (startp) <= ZV
15997 && PT >= CHARPOS (startp)
15998 && (CHARPOS (startp) < ZV
15999 /* Avoid starting at end of buffer. */
16000 || CHARPOS (startp) == BEGV
16001 || !window_outdated (w)))
16002 {
16003 int d1, d2, d3, d4, d5, d6;
16004
16005 /* If first window line is a continuation line, and window start
16006 is inside the modified region, but the first change is before
16007 current window start, we must select a new window start.
16008
16009 However, if this is the result of a down-mouse event (e.g. by
16010 extending the mouse-drag-overlay), we don't want to select a
16011 new window start, since that would change the position under
16012 the mouse, resulting in an unwanted mouse-movement rather
16013 than a simple mouse-click. */
16014 if (!w->start_at_line_beg
16015 && NILP (do_mouse_tracking)
16016 && CHARPOS (startp) > BEGV
16017 && CHARPOS (startp) > BEG + beg_unchanged
16018 && CHARPOS (startp) <= Z - end_unchanged
16019 /* Even if w->start_at_line_beg is nil, a new window may
16020 start at a line_beg, since that's how set_buffer_window
16021 sets it. So, we need to check the return value of
16022 compute_window_start_on_continuation_line. (See also
16023 bug#197). */
16024 && XMARKER (w->start)->buffer == current_buffer
16025 && compute_window_start_on_continuation_line (w)
16026 /* It doesn't make sense to force the window start like we
16027 do at label force_start if it is already known that point
16028 will not be visible in the resulting window, because
16029 doing so will move point from its correct position
16030 instead of scrolling the window to bring point into view.
16031 See bug#9324. */
16032 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16033 {
16034 w->force_start = 1;
16035 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16036 goto force_start;
16037 }
16038
16039 #ifdef GLYPH_DEBUG
16040 debug_method_add (w, "same window start");
16041 #endif
16042
16043 /* Try to redisplay starting at same place as before.
16044 If point has not moved off frame, accept the results. */
16045 if (!current_matrix_up_to_date_p
16046 /* Don't use try_window_reusing_current_matrix in this case
16047 because a window scroll function can have changed the
16048 buffer. */
16049 || !NILP (Vwindow_scroll_functions)
16050 || MINI_WINDOW_P (w)
16051 || !(used_current_matrix_p
16052 = try_window_reusing_current_matrix (w)))
16053 {
16054 IF_DEBUG (debug_method_add (w, "1"));
16055 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16056 /* -1 means we need to scroll.
16057 0 means we need new matrices, but fonts_changed
16058 is set in that case, so we will detect it below. */
16059 goto try_to_scroll;
16060 }
16061
16062 if (f->fonts_changed)
16063 goto need_larger_matrices;
16064
16065 if (w->cursor.vpos >= 0)
16066 {
16067 if (!just_this_one_p
16068 || current_buffer->clip_changed
16069 || BEG_UNCHANGED < CHARPOS (startp))
16070 /* Forget any recorded base line for line number display. */
16071 w->base_line_number = 0;
16072
16073 if (!cursor_row_fully_visible_p (w, 1, 0))
16074 {
16075 clear_glyph_matrix (w->desired_matrix);
16076 last_line_misfit = 1;
16077 }
16078 /* Drop through and scroll. */
16079 else
16080 goto done;
16081 }
16082 else
16083 clear_glyph_matrix (w->desired_matrix);
16084 }
16085
16086 try_to_scroll:
16087
16088 /* Redisplay the mode line. Select the buffer properly for that. */
16089 if (!update_mode_line)
16090 {
16091 update_mode_line = 1;
16092 w->update_mode_line = 1;
16093 }
16094
16095 /* Try to scroll by specified few lines. */
16096 if ((scroll_conservatively
16097 || emacs_scroll_step
16098 || temp_scroll_step
16099 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16100 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16101 && CHARPOS (startp) >= BEGV
16102 && CHARPOS (startp) <= ZV)
16103 {
16104 /* The function returns -1 if new fonts were loaded, 1 if
16105 successful, 0 if not successful. */
16106 int ss = try_scrolling (window, just_this_one_p,
16107 scroll_conservatively,
16108 emacs_scroll_step,
16109 temp_scroll_step, last_line_misfit);
16110 switch (ss)
16111 {
16112 case SCROLLING_SUCCESS:
16113 goto done;
16114
16115 case SCROLLING_NEED_LARGER_MATRICES:
16116 goto need_larger_matrices;
16117
16118 case SCROLLING_FAILED:
16119 break;
16120
16121 default:
16122 emacs_abort ();
16123 }
16124 }
16125
16126 /* Finally, just choose a place to start which positions point
16127 according to user preferences. */
16128
16129 recenter:
16130
16131 #ifdef GLYPH_DEBUG
16132 debug_method_add (w, "recenter");
16133 #endif
16134
16135 /* Forget any previously recorded base line for line number display. */
16136 if (!buffer_unchanged_p)
16137 w->base_line_number = 0;
16138
16139 /* Determine the window start relative to point. */
16140 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16141 it.current_y = it.last_visible_y;
16142 if (centering_position < 0)
16143 {
16144 int window_total_lines
16145 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16146 int margin =
16147 scroll_margin > 0
16148 ? min (scroll_margin, window_total_lines / 4)
16149 : 0;
16150 ptrdiff_t margin_pos = CHARPOS (startp);
16151 Lisp_Object aggressive;
16152 int scrolling_up;
16153
16154 /* If there is a scroll margin at the top of the window, find
16155 its character position. */
16156 if (margin
16157 /* Cannot call start_display if startp is not in the
16158 accessible region of the buffer. This can happen when we
16159 have just switched to a different buffer and/or changed
16160 its restriction. In that case, startp is initialized to
16161 the character position 1 (BEGV) because we did not yet
16162 have chance to display the buffer even once. */
16163 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16164 {
16165 struct it it1;
16166 void *it1data = NULL;
16167
16168 SAVE_IT (it1, it, it1data);
16169 start_display (&it1, w, startp);
16170 move_it_vertically (&it1, margin * frame_line_height);
16171 margin_pos = IT_CHARPOS (it1);
16172 RESTORE_IT (&it, &it, it1data);
16173 }
16174 scrolling_up = PT > margin_pos;
16175 aggressive =
16176 scrolling_up
16177 ? BVAR (current_buffer, scroll_up_aggressively)
16178 : BVAR (current_buffer, scroll_down_aggressively);
16179
16180 if (!MINI_WINDOW_P (w)
16181 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16182 {
16183 int pt_offset = 0;
16184
16185 /* Setting scroll-conservatively overrides
16186 scroll-*-aggressively. */
16187 if (!scroll_conservatively && NUMBERP (aggressive))
16188 {
16189 double float_amount = XFLOATINT (aggressive);
16190
16191 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16192 if (pt_offset == 0 && float_amount > 0)
16193 pt_offset = 1;
16194 if (pt_offset && margin > 0)
16195 margin -= 1;
16196 }
16197 /* Compute how much to move the window start backward from
16198 point so that point will be displayed where the user
16199 wants it. */
16200 if (scrolling_up)
16201 {
16202 centering_position = it.last_visible_y;
16203 if (pt_offset)
16204 centering_position -= pt_offset;
16205 centering_position -=
16206 frame_line_height * (1 + margin + (last_line_misfit != 0))
16207 + WINDOW_HEADER_LINE_HEIGHT (w);
16208 /* Don't let point enter the scroll margin near top of
16209 the window. */
16210 if (centering_position < margin * frame_line_height)
16211 centering_position = margin * frame_line_height;
16212 }
16213 else
16214 centering_position = margin * frame_line_height + pt_offset;
16215 }
16216 else
16217 /* Set the window start half the height of the window backward
16218 from point. */
16219 centering_position = window_box_height (w) / 2;
16220 }
16221 move_it_vertically_backward (&it, centering_position);
16222
16223 eassert (IT_CHARPOS (it) >= BEGV);
16224
16225 /* The function move_it_vertically_backward may move over more
16226 than the specified y-distance. If it->w is small, e.g. a
16227 mini-buffer window, we may end up in front of the window's
16228 display area. Start displaying at the start of the line
16229 containing PT in this case. */
16230 if (it.current_y <= 0)
16231 {
16232 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16233 move_it_vertically_backward (&it, 0);
16234 it.current_y = 0;
16235 }
16236
16237 it.current_x = it.hpos = 0;
16238
16239 /* Set the window start position here explicitly, to avoid an
16240 infinite loop in case the functions in window-scroll-functions
16241 get errors. */
16242 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16243
16244 /* Run scroll hooks. */
16245 startp = run_window_scroll_functions (window, it.current.pos);
16246
16247 /* Redisplay the window. */
16248 if (!current_matrix_up_to_date_p
16249 || windows_or_buffers_changed
16250 || f->cursor_type_changed
16251 /* Don't use try_window_reusing_current_matrix in this case
16252 because it can have changed the buffer. */
16253 || !NILP (Vwindow_scroll_functions)
16254 || !just_this_one_p
16255 || MINI_WINDOW_P (w)
16256 || !(used_current_matrix_p
16257 = try_window_reusing_current_matrix (w)))
16258 try_window (window, startp, 0);
16259
16260 /* If new fonts have been loaded (due to fontsets), give up. We
16261 have to start a new redisplay since we need to re-adjust glyph
16262 matrices. */
16263 if (f->fonts_changed)
16264 goto need_larger_matrices;
16265
16266 /* If cursor did not appear assume that the middle of the window is
16267 in the first line of the window. Do it again with the next line.
16268 (Imagine a window of height 100, displaying two lines of height
16269 60. Moving back 50 from it->last_visible_y will end in the first
16270 line.) */
16271 if (w->cursor.vpos < 0)
16272 {
16273 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16274 {
16275 clear_glyph_matrix (w->desired_matrix);
16276 move_it_by_lines (&it, 1);
16277 try_window (window, it.current.pos, 0);
16278 }
16279 else if (PT < IT_CHARPOS (it))
16280 {
16281 clear_glyph_matrix (w->desired_matrix);
16282 move_it_by_lines (&it, -1);
16283 try_window (window, it.current.pos, 0);
16284 }
16285 else
16286 {
16287 /* Not much we can do about it. */
16288 }
16289 }
16290
16291 /* Consider the following case: Window starts at BEGV, there is
16292 invisible, intangible text at BEGV, so that display starts at
16293 some point START > BEGV. It can happen that we are called with
16294 PT somewhere between BEGV and START. Try to handle that case. */
16295 if (w->cursor.vpos < 0)
16296 {
16297 struct glyph_row *row = w->current_matrix->rows;
16298 if (row->mode_line_p)
16299 ++row;
16300 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16301 }
16302
16303 if (!cursor_row_fully_visible_p (w, 0, 0))
16304 {
16305 /* If vscroll is enabled, disable it and try again. */
16306 if (w->vscroll)
16307 {
16308 w->vscroll = 0;
16309 clear_glyph_matrix (w->desired_matrix);
16310 goto recenter;
16311 }
16312
16313 /* Users who set scroll-conservatively to a large number want
16314 point just above/below the scroll margin. If we ended up
16315 with point's row partially visible, move the window start to
16316 make that row fully visible and out of the margin. */
16317 if (scroll_conservatively > SCROLL_LIMIT)
16318 {
16319 int window_total_lines
16320 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16321 int margin =
16322 scroll_margin > 0
16323 ? min (scroll_margin, window_total_lines / 4)
16324 : 0;
16325 int move_down = w->cursor.vpos >= window_total_lines / 2;
16326
16327 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16328 clear_glyph_matrix (w->desired_matrix);
16329 if (1 == try_window (window, it.current.pos,
16330 TRY_WINDOW_CHECK_MARGINS))
16331 goto done;
16332 }
16333
16334 /* If centering point failed to make the whole line visible,
16335 put point at the top instead. That has to make the whole line
16336 visible, if it can be done. */
16337 if (centering_position == 0)
16338 goto done;
16339
16340 clear_glyph_matrix (w->desired_matrix);
16341 centering_position = 0;
16342 goto recenter;
16343 }
16344
16345 done:
16346
16347 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16348 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16349 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16350
16351 /* Display the mode line, if we must. */
16352 if ((update_mode_line
16353 /* If window not full width, must redo its mode line
16354 if (a) the window to its side is being redone and
16355 (b) we do a frame-based redisplay. This is a consequence
16356 of how inverted lines are drawn in frame-based redisplay. */
16357 || (!just_this_one_p
16358 && !FRAME_WINDOW_P (f)
16359 && !WINDOW_FULL_WIDTH_P (w))
16360 /* Line number to display. */
16361 || w->base_line_pos > 0
16362 /* Column number is displayed and different from the one displayed. */
16363 || (w->column_number_displayed != -1
16364 && (w->column_number_displayed != current_column ())))
16365 /* This means that the window has a mode line. */
16366 && (WINDOW_WANTS_MODELINE_P (w)
16367 || WINDOW_WANTS_HEADER_LINE_P (w)))
16368 {
16369
16370 display_mode_lines (w);
16371
16372 /* If mode line height has changed, arrange for a thorough
16373 immediate redisplay using the correct mode line height. */
16374 if (WINDOW_WANTS_MODELINE_P (w)
16375 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16376 {
16377 f->fonts_changed = 1;
16378 w->mode_line_height = -1;
16379 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16380 = DESIRED_MODE_LINE_HEIGHT (w);
16381 }
16382
16383 /* If header line height has changed, arrange for a thorough
16384 immediate redisplay using the correct header line height. */
16385 if (WINDOW_WANTS_HEADER_LINE_P (w)
16386 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16387 {
16388 f->fonts_changed = 1;
16389 w->header_line_height = -1;
16390 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16391 = DESIRED_HEADER_LINE_HEIGHT (w);
16392 }
16393
16394 if (f->fonts_changed)
16395 goto need_larger_matrices;
16396 }
16397
16398 if (!line_number_displayed && w->base_line_pos != -1)
16399 {
16400 w->base_line_pos = 0;
16401 w->base_line_number = 0;
16402 }
16403
16404 finish_menu_bars:
16405
16406 /* When we reach a frame's selected window, redo the frame's menu bar. */
16407 if (update_mode_line
16408 && EQ (FRAME_SELECTED_WINDOW (f), window))
16409 {
16410 int redisplay_menu_p = 0;
16411
16412 if (FRAME_WINDOW_P (f))
16413 {
16414 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16415 || defined (HAVE_NS) || defined (USE_GTK)
16416 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16417 #else
16418 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16419 #endif
16420 }
16421 else
16422 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16423
16424 if (redisplay_menu_p)
16425 display_menu_bar (w);
16426
16427 #ifdef HAVE_WINDOW_SYSTEM
16428 if (FRAME_WINDOW_P (f))
16429 {
16430 #if defined (USE_GTK) || defined (HAVE_NS)
16431 if (FRAME_EXTERNAL_TOOL_BAR (f))
16432 redisplay_tool_bar (f);
16433 #else
16434 if (WINDOWP (f->tool_bar_window)
16435 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16436 || !NILP (Vauto_resize_tool_bars))
16437 && redisplay_tool_bar (f))
16438 ignore_mouse_drag_p = 1;
16439 #endif
16440 }
16441 #endif
16442 }
16443
16444 #ifdef HAVE_WINDOW_SYSTEM
16445 if (FRAME_WINDOW_P (f)
16446 && update_window_fringes (w, (just_this_one_p
16447 || (!used_current_matrix_p && !overlay_arrow_seen)
16448 || w->pseudo_window_p)))
16449 {
16450 update_begin (f);
16451 block_input ();
16452 if (draw_window_fringes (w, 1))
16453 {
16454 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16455 x_draw_right_divider (w);
16456 else
16457 x_draw_vertical_border (w);
16458 }
16459 unblock_input ();
16460 update_end (f);
16461 }
16462
16463 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16464 x_draw_bottom_divider (w);
16465 #endif /* HAVE_WINDOW_SYSTEM */
16466
16467 /* We go to this label, with fonts_changed set, if it is
16468 necessary to try again using larger glyph matrices.
16469 We have to redeem the scroll bar even in this case,
16470 because the loop in redisplay_internal expects that. */
16471 need_larger_matrices:
16472 ;
16473 finish_scroll_bars:
16474
16475 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16476 {
16477 /* Set the thumb's position and size. */
16478 set_vertical_scroll_bar (w);
16479
16480 /* Note that we actually used the scroll bar attached to this
16481 window, so it shouldn't be deleted at the end of redisplay. */
16482 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16483 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16484 }
16485
16486 /* Restore current_buffer and value of point in it. The window
16487 update may have changed the buffer, so first make sure `opoint'
16488 is still valid (Bug#6177). */
16489 if (CHARPOS (opoint) < BEGV)
16490 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16491 else if (CHARPOS (opoint) > ZV)
16492 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16493 else
16494 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16495
16496 set_buffer_internal_1 (old);
16497 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16498 shorter. This can be caused by log truncation in *Messages*. */
16499 if (CHARPOS (lpoint) <= ZV)
16500 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16501
16502 unbind_to (count, Qnil);
16503 }
16504
16505
16506 /* Build the complete desired matrix of WINDOW with a window start
16507 buffer position POS.
16508
16509 Value is 1 if successful. It is zero if fonts were loaded during
16510 redisplay which makes re-adjusting glyph matrices necessary, and -1
16511 if point would appear in the scroll margins.
16512 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16513 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16514 set in FLAGS.) */
16515
16516 int
16517 try_window (Lisp_Object window, struct text_pos pos, int flags)
16518 {
16519 struct window *w = XWINDOW (window);
16520 struct it it;
16521 struct glyph_row *last_text_row = NULL;
16522 struct frame *f = XFRAME (w->frame);
16523 int frame_line_height = default_line_pixel_height (w);
16524
16525 /* Make POS the new window start. */
16526 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16527
16528 /* Mark cursor position as unknown. No overlay arrow seen. */
16529 w->cursor.vpos = -1;
16530 overlay_arrow_seen = 0;
16531
16532 /* Initialize iterator and info to start at POS. */
16533 start_display (&it, w, pos);
16534
16535 /* Display all lines of W. */
16536 while (it.current_y < it.last_visible_y)
16537 {
16538 if (display_line (&it))
16539 last_text_row = it.glyph_row - 1;
16540 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16541 return 0;
16542 }
16543
16544 /* Don't let the cursor end in the scroll margins. */
16545 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16546 && !MINI_WINDOW_P (w))
16547 {
16548 int this_scroll_margin;
16549 int window_total_lines
16550 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16551
16552 if (scroll_margin > 0)
16553 {
16554 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16555 this_scroll_margin *= frame_line_height;
16556 }
16557 else
16558 this_scroll_margin = 0;
16559
16560 if ((w->cursor.y >= 0 /* not vscrolled */
16561 && w->cursor.y < this_scroll_margin
16562 && CHARPOS (pos) > BEGV
16563 && IT_CHARPOS (it) < ZV)
16564 /* rms: considering make_cursor_line_fully_visible_p here
16565 seems to give wrong results. We don't want to recenter
16566 when the last line is partly visible, we want to allow
16567 that case to be handled in the usual way. */
16568 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16569 {
16570 w->cursor.vpos = -1;
16571 clear_glyph_matrix (w->desired_matrix);
16572 return -1;
16573 }
16574 }
16575
16576 /* If bottom moved off end of frame, change mode line percentage. */
16577 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16578 w->update_mode_line = 1;
16579
16580 /* Set window_end_pos to the offset of the last character displayed
16581 on the window from the end of current_buffer. Set
16582 window_end_vpos to its row number. */
16583 if (last_text_row)
16584 {
16585 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16586 adjust_window_ends (w, last_text_row, 0);
16587 eassert
16588 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16589 w->window_end_vpos)));
16590 }
16591 else
16592 {
16593 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16594 w->window_end_pos = Z - ZV;
16595 w->window_end_vpos = 0;
16596 }
16597
16598 /* But that is not valid info until redisplay finishes. */
16599 w->window_end_valid = 0;
16600 return 1;
16601 }
16602
16603
16604 \f
16605 /************************************************************************
16606 Window redisplay reusing current matrix when buffer has not changed
16607 ************************************************************************/
16608
16609 /* Try redisplay of window W showing an unchanged buffer with a
16610 different window start than the last time it was displayed by
16611 reusing its current matrix. Value is non-zero if successful.
16612 W->start is the new window start. */
16613
16614 static int
16615 try_window_reusing_current_matrix (struct window *w)
16616 {
16617 struct frame *f = XFRAME (w->frame);
16618 struct glyph_row *bottom_row;
16619 struct it it;
16620 struct run run;
16621 struct text_pos start, new_start;
16622 int nrows_scrolled, i;
16623 struct glyph_row *last_text_row;
16624 struct glyph_row *last_reused_text_row;
16625 struct glyph_row *start_row;
16626 int start_vpos, min_y, max_y;
16627
16628 #ifdef GLYPH_DEBUG
16629 if (inhibit_try_window_reusing)
16630 return 0;
16631 #endif
16632
16633 if (/* This function doesn't handle terminal frames. */
16634 !FRAME_WINDOW_P (f)
16635 /* Don't try to reuse the display if windows have been split
16636 or such. */
16637 || windows_or_buffers_changed
16638 || f->cursor_type_changed)
16639 return 0;
16640
16641 /* Can't do this if showing trailing whitespace. */
16642 if (!NILP (Vshow_trailing_whitespace))
16643 return 0;
16644
16645 /* If top-line visibility has changed, give up. */
16646 if (WINDOW_WANTS_HEADER_LINE_P (w)
16647 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16648 return 0;
16649
16650 /* Give up if old or new display is scrolled vertically. We could
16651 make this function handle this, but right now it doesn't. */
16652 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16653 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16654 return 0;
16655
16656 /* The variable new_start now holds the new window start. The old
16657 start `start' can be determined from the current matrix. */
16658 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16659 start = start_row->minpos;
16660 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16661
16662 /* Clear the desired matrix for the display below. */
16663 clear_glyph_matrix (w->desired_matrix);
16664
16665 if (CHARPOS (new_start) <= CHARPOS (start))
16666 {
16667 /* Don't use this method if the display starts with an ellipsis
16668 displayed for invisible text. It's not easy to handle that case
16669 below, and it's certainly not worth the effort since this is
16670 not a frequent case. */
16671 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16672 return 0;
16673
16674 IF_DEBUG (debug_method_add (w, "twu1"));
16675
16676 /* Display up to a row that can be reused. The variable
16677 last_text_row is set to the last row displayed that displays
16678 text. Note that it.vpos == 0 if or if not there is a
16679 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16680 start_display (&it, w, new_start);
16681 w->cursor.vpos = -1;
16682 last_text_row = last_reused_text_row = NULL;
16683
16684 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16685 {
16686 /* If we have reached into the characters in the START row,
16687 that means the line boundaries have changed. So we
16688 can't start copying with the row START. Maybe it will
16689 work to start copying with the following row. */
16690 while (IT_CHARPOS (it) > CHARPOS (start))
16691 {
16692 /* Advance to the next row as the "start". */
16693 start_row++;
16694 start = start_row->minpos;
16695 /* If there are no more rows to try, or just one, give up. */
16696 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16697 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16698 || CHARPOS (start) == ZV)
16699 {
16700 clear_glyph_matrix (w->desired_matrix);
16701 return 0;
16702 }
16703
16704 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16705 }
16706 /* If we have reached alignment, we can copy the rest of the
16707 rows. */
16708 if (IT_CHARPOS (it) == CHARPOS (start)
16709 /* Don't accept "alignment" inside a display vector,
16710 since start_row could have started in the middle of
16711 that same display vector (thus their character
16712 positions match), and we have no way of telling if
16713 that is the case. */
16714 && it.current.dpvec_index < 0)
16715 break;
16716
16717 if (display_line (&it))
16718 last_text_row = it.glyph_row - 1;
16719
16720 }
16721
16722 /* A value of current_y < last_visible_y means that we stopped
16723 at the previous window start, which in turn means that we
16724 have at least one reusable row. */
16725 if (it.current_y < it.last_visible_y)
16726 {
16727 struct glyph_row *row;
16728
16729 /* IT.vpos always starts from 0; it counts text lines. */
16730 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16731
16732 /* Find PT if not already found in the lines displayed. */
16733 if (w->cursor.vpos < 0)
16734 {
16735 int dy = it.current_y - start_row->y;
16736
16737 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16738 row = row_containing_pos (w, PT, row, NULL, dy);
16739 if (row)
16740 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16741 dy, nrows_scrolled);
16742 else
16743 {
16744 clear_glyph_matrix (w->desired_matrix);
16745 return 0;
16746 }
16747 }
16748
16749 /* Scroll the display. Do it before the current matrix is
16750 changed. The problem here is that update has not yet
16751 run, i.e. part of the current matrix is not up to date.
16752 scroll_run_hook will clear the cursor, and use the
16753 current matrix to get the height of the row the cursor is
16754 in. */
16755 run.current_y = start_row->y;
16756 run.desired_y = it.current_y;
16757 run.height = it.last_visible_y - it.current_y;
16758
16759 if (run.height > 0 && run.current_y != run.desired_y)
16760 {
16761 update_begin (f);
16762 FRAME_RIF (f)->update_window_begin_hook (w);
16763 FRAME_RIF (f)->clear_window_mouse_face (w);
16764 FRAME_RIF (f)->scroll_run_hook (w, &run);
16765 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16766 update_end (f);
16767 }
16768
16769 /* Shift current matrix down by nrows_scrolled lines. */
16770 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16771 rotate_matrix (w->current_matrix,
16772 start_vpos,
16773 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16774 nrows_scrolled);
16775
16776 /* Disable lines that must be updated. */
16777 for (i = 0; i < nrows_scrolled; ++i)
16778 (start_row + i)->enabled_p = false;
16779
16780 /* Re-compute Y positions. */
16781 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16782 max_y = it.last_visible_y;
16783 for (row = start_row + nrows_scrolled;
16784 row < bottom_row;
16785 ++row)
16786 {
16787 row->y = it.current_y;
16788 row->visible_height = row->height;
16789
16790 if (row->y < min_y)
16791 row->visible_height -= min_y - row->y;
16792 if (row->y + row->height > max_y)
16793 row->visible_height -= row->y + row->height - max_y;
16794 if (row->fringe_bitmap_periodic_p)
16795 row->redraw_fringe_bitmaps_p = 1;
16796
16797 it.current_y += row->height;
16798
16799 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16800 last_reused_text_row = row;
16801 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16802 break;
16803 }
16804
16805 /* Disable lines in the current matrix which are now
16806 below the window. */
16807 for (++row; row < bottom_row; ++row)
16808 row->enabled_p = row->mode_line_p = 0;
16809 }
16810
16811 /* Update window_end_pos etc.; last_reused_text_row is the last
16812 reused row from the current matrix containing text, if any.
16813 The value of last_text_row is the last displayed line
16814 containing text. */
16815 if (last_reused_text_row)
16816 adjust_window_ends (w, last_reused_text_row, 1);
16817 else if (last_text_row)
16818 adjust_window_ends (w, last_text_row, 0);
16819 else
16820 {
16821 /* This window must be completely empty. */
16822 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16823 w->window_end_pos = Z - ZV;
16824 w->window_end_vpos = 0;
16825 }
16826 w->window_end_valid = 0;
16827
16828 /* Update hint: don't try scrolling again in update_window. */
16829 w->desired_matrix->no_scrolling_p = 1;
16830
16831 #ifdef GLYPH_DEBUG
16832 debug_method_add (w, "try_window_reusing_current_matrix 1");
16833 #endif
16834 return 1;
16835 }
16836 else if (CHARPOS (new_start) > CHARPOS (start))
16837 {
16838 struct glyph_row *pt_row, *row;
16839 struct glyph_row *first_reusable_row;
16840 struct glyph_row *first_row_to_display;
16841 int dy;
16842 int yb = window_text_bottom_y (w);
16843
16844 /* Find the row starting at new_start, if there is one. Don't
16845 reuse a partially visible line at the end. */
16846 first_reusable_row = start_row;
16847 while (first_reusable_row->enabled_p
16848 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16849 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16850 < CHARPOS (new_start)))
16851 ++first_reusable_row;
16852
16853 /* Give up if there is no row to reuse. */
16854 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16855 || !first_reusable_row->enabled_p
16856 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16857 != CHARPOS (new_start)))
16858 return 0;
16859
16860 /* We can reuse fully visible rows beginning with
16861 first_reusable_row to the end of the window. Set
16862 first_row_to_display to the first row that cannot be reused.
16863 Set pt_row to the row containing point, if there is any. */
16864 pt_row = NULL;
16865 for (first_row_to_display = first_reusable_row;
16866 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16867 ++first_row_to_display)
16868 {
16869 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16870 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16871 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16872 && first_row_to_display->ends_at_zv_p
16873 && pt_row == NULL)))
16874 pt_row = first_row_to_display;
16875 }
16876
16877 /* Start displaying at the start of first_row_to_display. */
16878 eassert (first_row_to_display->y < yb);
16879 init_to_row_start (&it, w, first_row_to_display);
16880
16881 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16882 - start_vpos);
16883 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16884 - nrows_scrolled);
16885 it.current_y = (first_row_to_display->y - first_reusable_row->y
16886 + WINDOW_HEADER_LINE_HEIGHT (w));
16887
16888 /* Display lines beginning with first_row_to_display in the
16889 desired matrix. Set last_text_row to the last row displayed
16890 that displays text. */
16891 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16892 if (pt_row == NULL)
16893 w->cursor.vpos = -1;
16894 last_text_row = NULL;
16895 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16896 if (display_line (&it))
16897 last_text_row = it.glyph_row - 1;
16898
16899 /* If point is in a reused row, adjust y and vpos of the cursor
16900 position. */
16901 if (pt_row)
16902 {
16903 w->cursor.vpos -= nrows_scrolled;
16904 w->cursor.y -= first_reusable_row->y - start_row->y;
16905 }
16906
16907 /* Give up if point isn't in a row displayed or reused. (This
16908 also handles the case where w->cursor.vpos < nrows_scrolled
16909 after the calls to display_line, which can happen with scroll
16910 margins. See bug#1295.) */
16911 if (w->cursor.vpos < 0)
16912 {
16913 clear_glyph_matrix (w->desired_matrix);
16914 return 0;
16915 }
16916
16917 /* Scroll the display. */
16918 run.current_y = first_reusable_row->y;
16919 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16920 run.height = it.last_visible_y - run.current_y;
16921 dy = run.current_y - run.desired_y;
16922
16923 if (run.height)
16924 {
16925 update_begin (f);
16926 FRAME_RIF (f)->update_window_begin_hook (w);
16927 FRAME_RIF (f)->clear_window_mouse_face (w);
16928 FRAME_RIF (f)->scroll_run_hook (w, &run);
16929 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16930 update_end (f);
16931 }
16932
16933 /* Adjust Y positions of reused rows. */
16934 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16935 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16936 max_y = it.last_visible_y;
16937 for (row = first_reusable_row; row < first_row_to_display; ++row)
16938 {
16939 row->y -= dy;
16940 row->visible_height = row->height;
16941 if (row->y < min_y)
16942 row->visible_height -= min_y - row->y;
16943 if (row->y + row->height > max_y)
16944 row->visible_height -= row->y + row->height - max_y;
16945 if (row->fringe_bitmap_periodic_p)
16946 row->redraw_fringe_bitmaps_p = 1;
16947 }
16948
16949 /* Scroll the current matrix. */
16950 eassert (nrows_scrolled > 0);
16951 rotate_matrix (w->current_matrix,
16952 start_vpos,
16953 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16954 -nrows_scrolled);
16955
16956 /* Disable rows not reused. */
16957 for (row -= nrows_scrolled; row < bottom_row; ++row)
16958 row->enabled_p = false;
16959
16960 /* Point may have moved to a different line, so we cannot assume that
16961 the previous cursor position is valid; locate the correct row. */
16962 if (pt_row)
16963 {
16964 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16965 row < bottom_row
16966 && PT >= MATRIX_ROW_END_CHARPOS (row)
16967 && !row->ends_at_zv_p;
16968 row++)
16969 {
16970 w->cursor.vpos++;
16971 w->cursor.y = row->y;
16972 }
16973 if (row < bottom_row)
16974 {
16975 /* Can't simply scan the row for point with
16976 bidi-reordered glyph rows. Let set_cursor_from_row
16977 figure out where to put the cursor, and if it fails,
16978 give up. */
16979 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16980 {
16981 if (!set_cursor_from_row (w, row, w->current_matrix,
16982 0, 0, 0, 0))
16983 {
16984 clear_glyph_matrix (w->desired_matrix);
16985 return 0;
16986 }
16987 }
16988 else
16989 {
16990 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16991 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16992
16993 for (; glyph < end
16994 && (!BUFFERP (glyph->object)
16995 || glyph->charpos < PT);
16996 glyph++)
16997 {
16998 w->cursor.hpos++;
16999 w->cursor.x += glyph->pixel_width;
17000 }
17001 }
17002 }
17003 }
17004
17005 /* Adjust window end. A null value of last_text_row means that
17006 the window end is in reused rows which in turn means that
17007 only its vpos can have changed. */
17008 if (last_text_row)
17009 adjust_window_ends (w, last_text_row, 0);
17010 else
17011 w->window_end_vpos -= nrows_scrolled;
17012
17013 w->window_end_valid = 0;
17014 w->desired_matrix->no_scrolling_p = 1;
17015
17016 #ifdef GLYPH_DEBUG
17017 debug_method_add (w, "try_window_reusing_current_matrix 2");
17018 #endif
17019 return 1;
17020 }
17021
17022 return 0;
17023 }
17024
17025
17026 \f
17027 /************************************************************************
17028 Window redisplay reusing current matrix when buffer has changed
17029 ************************************************************************/
17030
17031 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17032 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17033 ptrdiff_t *, ptrdiff_t *);
17034 static struct glyph_row *
17035 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17036 struct glyph_row *);
17037
17038
17039 /* Return the last row in MATRIX displaying text. If row START is
17040 non-null, start searching with that row. IT gives the dimensions
17041 of the display. Value is null if matrix is empty; otherwise it is
17042 a pointer to the row found. */
17043
17044 static struct glyph_row *
17045 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17046 struct glyph_row *start)
17047 {
17048 struct glyph_row *row, *row_found;
17049
17050 /* Set row_found to the last row in IT->w's current matrix
17051 displaying text. The loop looks funny but think of partially
17052 visible lines. */
17053 row_found = NULL;
17054 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17055 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17056 {
17057 eassert (row->enabled_p);
17058 row_found = row;
17059 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17060 break;
17061 ++row;
17062 }
17063
17064 return row_found;
17065 }
17066
17067
17068 /* Return the last row in the current matrix of W that is not affected
17069 by changes at the start of current_buffer that occurred since W's
17070 current matrix was built. Value is null if no such row exists.
17071
17072 BEG_UNCHANGED us the number of characters unchanged at the start of
17073 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17074 first changed character in current_buffer. Characters at positions <
17075 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17076 when the current matrix was built. */
17077
17078 static struct glyph_row *
17079 find_last_unchanged_at_beg_row (struct window *w)
17080 {
17081 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17082 struct glyph_row *row;
17083 struct glyph_row *row_found = NULL;
17084 int yb = window_text_bottom_y (w);
17085
17086 /* Find the last row displaying unchanged text. */
17087 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17088 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17089 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17090 ++row)
17091 {
17092 if (/* If row ends before first_changed_pos, it is unchanged,
17093 except in some case. */
17094 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17095 /* When row ends in ZV and we write at ZV it is not
17096 unchanged. */
17097 && !row->ends_at_zv_p
17098 /* When first_changed_pos is the end of a continued line,
17099 row is not unchanged because it may be no longer
17100 continued. */
17101 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17102 && (row->continued_p
17103 || row->exact_window_width_line_p))
17104 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17105 needs to be recomputed, so don't consider this row as
17106 unchanged. This happens when the last line was
17107 bidi-reordered and was killed immediately before this
17108 redisplay cycle. In that case, ROW->end stores the
17109 buffer position of the first visual-order character of
17110 the killed text, which is now beyond ZV. */
17111 && CHARPOS (row->end.pos) <= ZV)
17112 row_found = row;
17113
17114 /* Stop if last visible row. */
17115 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17116 break;
17117 }
17118
17119 return row_found;
17120 }
17121
17122
17123 /* Find the first glyph row in the current matrix of W that is not
17124 affected by changes at the end of current_buffer since the
17125 time W's current matrix was built.
17126
17127 Return in *DELTA the number of chars by which buffer positions in
17128 unchanged text at the end of current_buffer must be adjusted.
17129
17130 Return in *DELTA_BYTES the corresponding number of bytes.
17131
17132 Value is null if no such row exists, i.e. all rows are affected by
17133 changes. */
17134
17135 static struct glyph_row *
17136 find_first_unchanged_at_end_row (struct window *w,
17137 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17138 {
17139 struct glyph_row *row;
17140 struct glyph_row *row_found = NULL;
17141
17142 *delta = *delta_bytes = 0;
17143
17144 /* Display must not have been paused, otherwise the current matrix
17145 is not up to date. */
17146 eassert (w->window_end_valid);
17147
17148 /* A value of window_end_pos >= END_UNCHANGED means that the window
17149 end is in the range of changed text. If so, there is no
17150 unchanged row at the end of W's current matrix. */
17151 if (w->window_end_pos >= END_UNCHANGED)
17152 return NULL;
17153
17154 /* Set row to the last row in W's current matrix displaying text. */
17155 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17156
17157 /* If matrix is entirely empty, no unchanged row exists. */
17158 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17159 {
17160 /* The value of row is the last glyph row in the matrix having a
17161 meaningful buffer position in it. The end position of row
17162 corresponds to window_end_pos. This allows us to translate
17163 buffer positions in the current matrix to current buffer
17164 positions for characters not in changed text. */
17165 ptrdiff_t Z_old =
17166 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17167 ptrdiff_t Z_BYTE_old =
17168 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17169 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17170 struct glyph_row *first_text_row
17171 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17172
17173 *delta = Z - Z_old;
17174 *delta_bytes = Z_BYTE - Z_BYTE_old;
17175
17176 /* Set last_unchanged_pos to the buffer position of the last
17177 character in the buffer that has not been changed. Z is the
17178 index + 1 of the last character in current_buffer, i.e. by
17179 subtracting END_UNCHANGED we get the index of the last
17180 unchanged character, and we have to add BEG to get its buffer
17181 position. */
17182 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17183 last_unchanged_pos_old = last_unchanged_pos - *delta;
17184
17185 /* Search backward from ROW for a row displaying a line that
17186 starts at a minimum position >= last_unchanged_pos_old. */
17187 for (; row > first_text_row; --row)
17188 {
17189 /* This used to abort, but it can happen.
17190 It is ok to just stop the search instead here. KFS. */
17191 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17192 break;
17193
17194 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17195 row_found = row;
17196 }
17197 }
17198
17199 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17200
17201 return row_found;
17202 }
17203
17204
17205 /* Make sure that glyph rows in the current matrix of window W
17206 reference the same glyph memory as corresponding rows in the
17207 frame's frame matrix. This function is called after scrolling W's
17208 current matrix on a terminal frame in try_window_id and
17209 try_window_reusing_current_matrix. */
17210
17211 static void
17212 sync_frame_with_window_matrix_rows (struct window *w)
17213 {
17214 struct frame *f = XFRAME (w->frame);
17215 struct glyph_row *window_row, *window_row_end, *frame_row;
17216
17217 /* Preconditions: W must be a leaf window and full-width. Its frame
17218 must have a frame matrix. */
17219 eassert (BUFFERP (w->contents));
17220 eassert (WINDOW_FULL_WIDTH_P (w));
17221 eassert (!FRAME_WINDOW_P (f));
17222
17223 /* If W is a full-width window, glyph pointers in W's current matrix
17224 have, by definition, to be the same as glyph pointers in the
17225 corresponding frame matrix. Note that frame matrices have no
17226 marginal areas (see build_frame_matrix). */
17227 window_row = w->current_matrix->rows;
17228 window_row_end = window_row + w->current_matrix->nrows;
17229 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17230 while (window_row < window_row_end)
17231 {
17232 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17233 struct glyph *end = window_row->glyphs[LAST_AREA];
17234
17235 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17236 frame_row->glyphs[TEXT_AREA] = start;
17237 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17238 frame_row->glyphs[LAST_AREA] = end;
17239
17240 /* Disable frame rows whose corresponding window rows have
17241 been disabled in try_window_id. */
17242 if (!window_row->enabled_p)
17243 frame_row->enabled_p = false;
17244
17245 ++window_row, ++frame_row;
17246 }
17247 }
17248
17249
17250 /* Find the glyph row in window W containing CHARPOS. Consider all
17251 rows between START and END (not inclusive). END null means search
17252 all rows to the end of the display area of W. Value is the row
17253 containing CHARPOS or null. */
17254
17255 struct glyph_row *
17256 row_containing_pos (struct window *w, ptrdiff_t charpos,
17257 struct glyph_row *start, struct glyph_row *end, int dy)
17258 {
17259 struct glyph_row *row = start;
17260 struct glyph_row *best_row = NULL;
17261 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17262 int last_y;
17263
17264 /* If we happen to start on a header-line, skip that. */
17265 if (row->mode_line_p)
17266 ++row;
17267
17268 if ((end && row >= end) || !row->enabled_p)
17269 return NULL;
17270
17271 last_y = window_text_bottom_y (w) - dy;
17272
17273 while (1)
17274 {
17275 /* Give up if we have gone too far. */
17276 if (end && row >= end)
17277 return NULL;
17278 /* This formerly returned if they were equal.
17279 I think that both quantities are of a "last plus one" type;
17280 if so, when they are equal, the row is within the screen. -- rms. */
17281 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17282 return NULL;
17283
17284 /* If it is in this row, return this row. */
17285 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17286 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17287 /* The end position of a row equals the start
17288 position of the next row. If CHARPOS is there, we
17289 would rather consider it displayed in the next
17290 line, except when this line ends in ZV. */
17291 && !row_for_charpos_p (row, charpos)))
17292 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17293 {
17294 struct glyph *g;
17295
17296 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17297 || (!best_row && !row->continued_p))
17298 return row;
17299 /* In bidi-reordered rows, there could be several rows whose
17300 edges surround CHARPOS, all of these rows belonging to
17301 the same continued line. We need to find the row which
17302 fits CHARPOS the best. */
17303 for (g = row->glyphs[TEXT_AREA];
17304 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17305 g++)
17306 {
17307 if (!STRINGP (g->object))
17308 {
17309 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17310 {
17311 mindif = eabs (g->charpos - charpos);
17312 best_row = row;
17313 /* Exact match always wins. */
17314 if (mindif == 0)
17315 return best_row;
17316 }
17317 }
17318 }
17319 }
17320 else if (best_row && !row->continued_p)
17321 return best_row;
17322 ++row;
17323 }
17324 }
17325
17326
17327 /* Try to redisplay window W by reusing its existing display. W's
17328 current matrix must be up to date when this function is called,
17329 i.e. window_end_valid must be nonzero.
17330
17331 Value is
17332
17333 >= 1 if successful, i.e. display has been updated
17334 specifically:
17335 1 means the changes were in front of a newline that precedes
17336 the window start, and the whole current matrix was reused
17337 2 means the changes were after the last position displayed
17338 in the window, and the whole current matrix was reused
17339 3 means portions of the current matrix were reused, while
17340 some of the screen lines were redrawn
17341 -1 if redisplay with same window start is known not to succeed
17342 0 if otherwise unsuccessful
17343
17344 The following steps are performed:
17345
17346 1. Find the last row in the current matrix of W that is not
17347 affected by changes at the start of current_buffer. If no such row
17348 is found, give up.
17349
17350 2. Find the first row in W's current matrix that is not affected by
17351 changes at the end of current_buffer. Maybe there is no such row.
17352
17353 3. Display lines beginning with the row + 1 found in step 1 to the
17354 row found in step 2 or, if step 2 didn't find a row, to the end of
17355 the window.
17356
17357 4. If cursor is not known to appear on the window, give up.
17358
17359 5. If display stopped at the row found in step 2, scroll the
17360 display and current matrix as needed.
17361
17362 6. Maybe display some lines at the end of W, if we must. This can
17363 happen under various circumstances, like a partially visible line
17364 becoming fully visible, or because newly displayed lines are displayed
17365 in smaller font sizes.
17366
17367 7. Update W's window end information. */
17368
17369 static int
17370 try_window_id (struct window *w)
17371 {
17372 struct frame *f = XFRAME (w->frame);
17373 struct glyph_matrix *current_matrix = w->current_matrix;
17374 struct glyph_matrix *desired_matrix = w->desired_matrix;
17375 struct glyph_row *last_unchanged_at_beg_row;
17376 struct glyph_row *first_unchanged_at_end_row;
17377 struct glyph_row *row;
17378 struct glyph_row *bottom_row;
17379 int bottom_vpos;
17380 struct it it;
17381 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17382 int dvpos, dy;
17383 struct text_pos start_pos;
17384 struct run run;
17385 int first_unchanged_at_end_vpos = 0;
17386 struct glyph_row *last_text_row, *last_text_row_at_end;
17387 struct text_pos start;
17388 ptrdiff_t first_changed_charpos, last_changed_charpos;
17389
17390 #ifdef GLYPH_DEBUG
17391 if (inhibit_try_window_id)
17392 return 0;
17393 #endif
17394
17395 /* This is handy for debugging. */
17396 #if 0
17397 #define GIVE_UP(X) \
17398 do { \
17399 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17400 return 0; \
17401 } while (0)
17402 #else
17403 #define GIVE_UP(X) return 0
17404 #endif
17405
17406 SET_TEXT_POS_FROM_MARKER (start, w->start);
17407
17408 /* Don't use this for mini-windows because these can show
17409 messages and mini-buffers, and we don't handle that here. */
17410 if (MINI_WINDOW_P (w))
17411 GIVE_UP (1);
17412
17413 /* This flag is used to prevent redisplay optimizations. */
17414 if (windows_or_buffers_changed || f->cursor_type_changed)
17415 GIVE_UP (2);
17416
17417 /* This function's optimizations cannot be used if overlays have
17418 changed in the buffer displayed by the window, so give up if they
17419 have. */
17420 if (w->last_overlay_modified != OVERLAY_MODIFF)
17421 GIVE_UP (21);
17422
17423 /* Verify that narrowing has not changed.
17424 Also verify that we were not told to prevent redisplay optimizations.
17425 It would be nice to further
17426 reduce the number of cases where this prevents try_window_id. */
17427 if (current_buffer->clip_changed
17428 || current_buffer->prevent_redisplay_optimizations_p)
17429 GIVE_UP (3);
17430
17431 /* Window must either use window-based redisplay or be full width. */
17432 if (!FRAME_WINDOW_P (f)
17433 && (!FRAME_LINE_INS_DEL_OK (f)
17434 || !WINDOW_FULL_WIDTH_P (w)))
17435 GIVE_UP (4);
17436
17437 /* Give up if point is known NOT to appear in W. */
17438 if (PT < CHARPOS (start))
17439 GIVE_UP (5);
17440
17441 /* Another way to prevent redisplay optimizations. */
17442 if (w->last_modified == 0)
17443 GIVE_UP (6);
17444
17445 /* Verify that window is not hscrolled. */
17446 if (w->hscroll != 0)
17447 GIVE_UP (7);
17448
17449 /* Verify that display wasn't paused. */
17450 if (!w->window_end_valid)
17451 GIVE_UP (8);
17452
17453 /* Likewise if highlighting trailing whitespace. */
17454 if (!NILP (Vshow_trailing_whitespace))
17455 GIVE_UP (11);
17456
17457 /* Can't use this if overlay arrow position and/or string have
17458 changed. */
17459 if (overlay_arrows_changed_p ())
17460 GIVE_UP (12);
17461
17462 /* When word-wrap is on, adding a space to the first word of a
17463 wrapped line can change the wrap position, altering the line
17464 above it. It might be worthwhile to handle this more
17465 intelligently, but for now just redisplay from scratch. */
17466 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17467 GIVE_UP (21);
17468
17469 /* Under bidi reordering, adding or deleting a character in the
17470 beginning of a paragraph, before the first strong directional
17471 character, can change the base direction of the paragraph (unless
17472 the buffer specifies a fixed paragraph direction), which will
17473 require to redisplay the whole paragraph. It might be worthwhile
17474 to find the paragraph limits and widen the range of redisplayed
17475 lines to that, but for now just give up this optimization and
17476 redisplay from scratch. */
17477 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17478 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17479 GIVE_UP (22);
17480
17481 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17482 only if buffer has really changed. The reason is that the gap is
17483 initially at Z for freshly visited files. The code below would
17484 set end_unchanged to 0 in that case. */
17485 if (MODIFF > SAVE_MODIFF
17486 /* This seems to happen sometimes after saving a buffer. */
17487 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17488 {
17489 if (GPT - BEG < BEG_UNCHANGED)
17490 BEG_UNCHANGED = GPT - BEG;
17491 if (Z - GPT < END_UNCHANGED)
17492 END_UNCHANGED = Z - GPT;
17493 }
17494
17495 /* The position of the first and last character that has been changed. */
17496 first_changed_charpos = BEG + BEG_UNCHANGED;
17497 last_changed_charpos = Z - END_UNCHANGED;
17498
17499 /* If window starts after a line end, and the last change is in
17500 front of that newline, then changes don't affect the display.
17501 This case happens with stealth-fontification. Note that although
17502 the display is unchanged, glyph positions in the matrix have to
17503 be adjusted, of course. */
17504 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17505 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17506 && ((last_changed_charpos < CHARPOS (start)
17507 && CHARPOS (start) == BEGV)
17508 || (last_changed_charpos < CHARPOS (start) - 1
17509 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17510 {
17511 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17512 struct glyph_row *r0;
17513
17514 /* Compute how many chars/bytes have been added to or removed
17515 from the buffer. */
17516 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17517 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17518 Z_delta = Z - Z_old;
17519 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17520
17521 /* Give up if PT is not in the window. Note that it already has
17522 been checked at the start of try_window_id that PT is not in
17523 front of the window start. */
17524 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17525 GIVE_UP (13);
17526
17527 /* If window start is unchanged, we can reuse the whole matrix
17528 as is, after adjusting glyph positions. No need to compute
17529 the window end again, since its offset from Z hasn't changed. */
17530 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17531 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17532 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17533 /* PT must not be in a partially visible line. */
17534 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17535 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17536 {
17537 /* Adjust positions in the glyph matrix. */
17538 if (Z_delta || Z_delta_bytes)
17539 {
17540 struct glyph_row *r1
17541 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17542 increment_matrix_positions (w->current_matrix,
17543 MATRIX_ROW_VPOS (r0, current_matrix),
17544 MATRIX_ROW_VPOS (r1, current_matrix),
17545 Z_delta, Z_delta_bytes);
17546 }
17547
17548 /* Set the cursor. */
17549 row = row_containing_pos (w, PT, r0, NULL, 0);
17550 if (row)
17551 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17552 return 1;
17553 }
17554 }
17555
17556 /* Handle the case that changes are all below what is displayed in
17557 the window, and that PT is in the window. This shortcut cannot
17558 be taken if ZV is visible in the window, and text has been added
17559 there that is visible in the window. */
17560 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17561 /* ZV is not visible in the window, or there are no
17562 changes at ZV, actually. */
17563 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17564 || first_changed_charpos == last_changed_charpos))
17565 {
17566 struct glyph_row *r0;
17567
17568 /* Give up if PT is not in the window. Note that it already has
17569 been checked at the start of try_window_id that PT is not in
17570 front of the window start. */
17571 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17572 GIVE_UP (14);
17573
17574 /* If window start is unchanged, we can reuse the whole matrix
17575 as is, without changing glyph positions since no text has
17576 been added/removed in front of the window end. */
17577 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17578 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17579 /* PT must not be in a partially visible line. */
17580 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17581 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17582 {
17583 /* We have to compute the window end anew since text
17584 could have been added/removed after it. */
17585 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17586 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17587
17588 /* Set the cursor. */
17589 row = row_containing_pos (w, PT, r0, NULL, 0);
17590 if (row)
17591 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17592 return 2;
17593 }
17594 }
17595
17596 /* Give up if window start is in the changed area.
17597
17598 The condition used to read
17599
17600 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17601
17602 but why that was tested escapes me at the moment. */
17603 if (CHARPOS (start) >= first_changed_charpos
17604 && CHARPOS (start) <= last_changed_charpos)
17605 GIVE_UP (15);
17606
17607 /* Check that window start agrees with the start of the first glyph
17608 row in its current matrix. Check this after we know the window
17609 start is not in changed text, otherwise positions would not be
17610 comparable. */
17611 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17612 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17613 GIVE_UP (16);
17614
17615 /* Give up if the window ends in strings. Overlay strings
17616 at the end are difficult to handle, so don't try. */
17617 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17618 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17619 GIVE_UP (20);
17620
17621 /* Compute the position at which we have to start displaying new
17622 lines. Some of the lines at the top of the window might be
17623 reusable because they are not displaying changed text. Find the
17624 last row in W's current matrix not affected by changes at the
17625 start of current_buffer. Value is null if changes start in the
17626 first line of window. */
17627 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17628 if (last_unchanged_at_beg_row)
17629 {
17630 /* Avoid starting to display in the middle of a character, a TAB
17631 for instance. This is easier than to set up the iterator
17632 exactly, and it's not a frequent case, so the additional
17633 effort wouldn't really pay off. */
17634 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17635 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17636 && last_unchanged_at_beg_row > w->current_matrix->rows)
17637 --last_unchanged_at_beg_row;
17638
17639 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17640 GIVE_UP (17);
17641
17642 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17643 GIVE_UP (18);
17644 start_pos = it.current.pos;
17645
17646 /* Start displaying new lines in the desired matrix at the same
17647 vpos we would use in the current matrix, i.e. below
17648 last_unchanged_at_beg_row. */
17649 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17650 current_matrix);
17651 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17652 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17653
17654 eassert (it.hpos == 0 && it.current_x == 0);
17655 }
17656 else
17657 {
17658 /* There are no reusable lines at the start of the window.
17659 Start displaying in the first text line. */
17660 start_display (&it, w, start);
17661 it.vpos = it.first_vpos;
17662 start_pos = it.current.pos;
17663 }
17664
17665 /* Find the first row that is not affected by changes at the end of
17666 the buffer. Value will be null if there is no unchanged row, in
17667 which case we must redisplay to the end of the window. delta
17668 will be set to the value by which buffer positions beginning with
17669 first_unchanged_at_end_row have to be adjusted due to text
17670 changes. */
17671 first_unchanged_at_end_row
17672 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17673 IF_DEBUG (debug_delta = delta);
17674 IF_DEBUG (debug_delta_bytes = delta_bytes);
17675
17676 /* Set stop_pos to the buffer position up to which we will have to
17677 display new lines. If first_unchanged_at_end_row != NULL, this
17678 is the buffer position of the start of the line displayed in that
17679 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17680 that we don't stop at a buffer position. */
17681 stop_pos = 0;
17682 if (first_unchanged_at_end_row)
17683 {
17684 eassert (last_unchanged_at_beg_row == NULL
17685 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17686
17687 /* If this is a continuation line, move forward to the next one
17688 that isn't. Changes in lines above affect this line.
17689 Caution: this may move first_unchanged_at_end_row to a row
17690 not displaying text. */
17691 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17692 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17693 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17694 < it.last_visible_y))
17695 ++first_unchanged_at_end_row;
17696
17697 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17698 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17699 >= it.last_visible_y))
17700 first_unchanged_at_end_row = NULL;
17701 else
17702 {
17703 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17704 + delta);
17705 first_unchanged_at_end_vpos
17706 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17707 eassert (stop_pos >= Z - END_UNCHANGED);
17708 }
17709 }
17710 else if (last_unchanged_at_beg_row == NULL)
17711 GIVE_UP (19);
17712
17713
17714 #ifdef GLYPH_DEBUG
17715
17716 /* Either there is no unchanged row at the end, or the one we have
17717 now displays text. This is a necessary condition for the window
17718 end pos calculation at the end of this function. */
17719 eassert (first_unchanged_at_end_row == NULL
17720 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17721
17722 debug_last_unchanged_at_beg_vpos
17723 = (last_unchanged_at_beg_row
17724 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17725 : -1);
17726 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17727
17728 #endif /* GLYPH_DEBUG */
17729
17730
17731 /* Display new lines. Set last_text_row to the last new line
17732 displayed which has text on it, i.e. might end up as being the
17733 line where the window_end_vpos is. */
17734 w->cursor.vpos = -1;
17735 last_text_row = NULL;
17736 overlay_arrow_seen = 0;
17737 while (it.current_y < it.last_visible_y
17738 && !f->fonts_changed
17739 && (first_unchanged_at_end_row == NULL
17740 || IT_CHARPOS (it) < stop_pos))
17741 {
17742 if (display_line (&it))
17743 last_text_row = it.glyph_row - 1;
17744 }
17745
17746 if (f->fonts_changed)
17747 return -1;
17748
17749
17750 /* Compute differences in buffer positions, y-positions etc. for
17751 lines reused at the bottom of the window. Compute what we can
17752 scroll. */
17753 if (first_unchanged_at_end_row
17754 /* No lines reused because we displayed everything up to the
17755 bottom of the window. */
17756 && it.current_y < it.last_visible_y)
17757 {
17758 dvpos = (it.vpos
17759 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17760 current_matrix));
17761 dy = it.current_y - first_unchanged_at_end_row->y;
17762 run.current_y = first_unchanged_at_end_row->y;
17763 run.desired_y = run.current_y + dy;
17764 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17765 }
17766 else
17767 {
17768 delta = delta_bytes = dvpos = dy
17769 = run.current_y = run.desired_y = run.height = 0;
17770 first_unchanged_at_end_row = NULL;
17771 }
17772 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17773
17774
17775 /* Find the cursor if not already found. We have to decide whether
17776 PT will appear on this window (it sometimes doesn't, but this is
17777 not a very frequent case.) This decision has to be made before
17778 the current matrix is altered. A value of cursor.vpos < 0 means
17779 that PT is either in one of the lines beginning at
17780 first_unchanged_at_end_row or below the window. Don't care for
17781 lines that might be displayed later at the window end; as
17782 mentioned, this is not a frequent case. */
17783 if (w->cursor.vpos < 0)
17784 {
17785 /* Cursor in unchanged rows at the top? */
17786 if (PT < CHARPOS (start_pos)
17787 && last_unchanged_at_beg_row)
17788 {
17789 row = row_containing_pos (w, PT,
17790 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17791 last_unchanged_at_beg_row + 1, 0);
17792 if (row)
17793 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17794 }
17795
17796 /* Start from first_unchanged_at_end_row looking for PT. */
17797 else if (first_unchanged_at_end_row)
17798 {
17799 row = row_containing_pos (w, PT - delta,
17800 first_unchanged_at_end_row, NULL, 0);
17801 if (row)
17802 set_cursor_from_row (w, row, w->current_matrix, delta,
17803 delta_bytes, dy, dvpos);
17804 }
17805
17806 /* Give up if cursor was not found. */
17807 if (w->cursor.vpos < 0)
17808 {
17809 clear_glyph_matrix (w->desired_matrix);
17810 return -1;
17811 }
17812 }
17813
17814 /* Don't let the cursor end in the scroll margins. */
17815 {
17816 int this_scroll_margin, cursor_height;
17817 int frame_line_height = default_line_pixel_height (w);
17818 int window_total_lines
17819 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17820
17821 this_scroll_margin =
17822 max (0, min (scroll_margin, window_total_lines / 4));
17823 this_scroll_margin *= frame_line_height;
17824 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17825
17826 if ((w->cursor.y < this_scroll_margin
17827 && CHARPOS (start) > BEGV)
17828 /* Old redisplay didn't take scroll margin into account at the bottom,
17829 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17830 || (w->cursor.y + (make_cursor_line_fully_visible_p
17831 ? cursor_height + this_scroll_margin
17832 : 1)) > it.last_visible_y)
17833 {
17834 w->cursor.vpos = -1;
17835 clear_glyph_matrix (w->desired_matrix);
17836 return -1;
17837 }
17838 }
17839
17840 /* Scroll the display. Do it before changing the current matrix so
17841 that xterm.c doesn't get confused about where the cursor glyph is
17842 found. */
17843 if (dy && run.height)
17844 {
17845 update_begin (f);
17846
17847 if (FRAME_WINDOW_P (f))
17848 {
17849 FRAME_RIF (f)->update_window_begin_hook (w);
17850 FRAME_RIF (f)->clear_window_mouse_face (w);
17851 FRAME_RIF (f)->scroll_run_hook (w, &run);
17852 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17853 }
17854 else
17855 {
17856 /* Terminal frame. In this case, dvpos gives the number of
17857 lines to scroll by; dvpos < 0 means scroll up. */
17858 int from_vpos
17859 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17860 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17861 int end = (WINDOW_TOP_EDGE_LINE (w)
17862 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17863 + window_internal_height (w));
17864
17865 #if defined (HAVE_GPM) || defined (MSDOS)
17866 x_clear_window_mouse_face (w);
17867 #endif
17868 /* Perform the operation on the screen. */
17869 if (dvpos > 0)
17870 {
17871 /* Scroll last_unchanged_at_beg_row to the end of the
17872 window down dvpos lines. */
17873 set_terminal_window (f, end);
17874
17875 /* On dumb terminals delete dvpos lines at the end
17876 before inserting dvpos empty lines. */
17877 if (!FRAME_SCROLL_REGION_OK (f))
17878 ins_del_lines (f, end - dvpos, -dvpos);
17879
17880 /* Insert dvpos empty lines in front of
17881 last_unchanged_at_beg_row. */
17882 ins_del_lines (f, from, dvpos);
17883 }
17884 else if (dvpos < 0)
17885 {
17886 /* Scroll up last_unchanged_at_beg_vpos to the end of
17887 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17888 set_terminal_window (f, end);
17889
17890 /* Delete dvpos lines in front of
17891 last_unchanged_at_beg_vpos. ins_del_lines will set
17892 the cursor to the given vpos and emit |dvpos| delete
17893 line sequences. */
17894 ins_del_lines (f, from + dvpos, dvpos);
17895
17896 /* On a dumb terminal insert dvpos empty lines at the
17897 end. */
17898 if (!FRAME_SCROLL_REGION_OK (f))
17899 ins_del_lines (f, end + dvpos, -dvpos);
17900 }
17901
17902 set_terminal_window (f, 0);
17903 }
17904
17905 update_end (f);
17906 }
17907
17908 /* Shift reused rows of the current matrix to the right position.
17909 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17910 text. */
17911 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17912 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17913 if (dvpos < 0)
17914 {
17915 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17916 bottom_vpos, dvpos);
17917 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17918 bottom_vpos);
17919 }
17920 else if (dvpos > 0)
17921 {
17922 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17923 bottom_vpos, dvpos);
17924 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17925 first_unchanged_at_end_vpos + dvpos);
17926 }
17927
17928 /* For frame-based redisplay, make sure that current frame and window
17929 matrix are in sync with respect to glyph memory. */
17930 if (!FRAME_WINDOW_P (f))
17931 sync_frame_with_window_matrix_rows (w);
17932
17933 /* Adjust buffer positions in reused rows. */
17934 if (delta || delta_bytes)
17935 increment_matrix_positions (current_matrix,
17936 first_unchanged_at_end_vpos + dvpos,
17937 bottom_vpos, delta, delta_bytes);
17938
17939 /* Adjust Y positions. */
17940 if (dy)
17941 shift_glyph_matrix (w, current_matrix,
17942 first_unchanged_at_end_vpos + dvpos,
17943 bottom_vpos, dy);
17944
17945 if (first_unchanged_at_end_row)
17946 {
17947 first_unchanged_at_end_row += dvpos;
17948 if (first_unchanged_at_end_row->y >= it.last_visible_y
17949 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17950 first_unchanged_at_end_row = NULL;
17951 }
17952
17953 /* If scrolling up, there may be some lines to display at the end of
17954 the window. */
17955 last_text_row_at_end = NULL;
17956 if (dy < 0)
17957 {
17958 /* Scrolling up can leave for example a partially visible line
17959 at the end of the window to be redisplayed. */
17960 /* Set last_row to the glyph row in the current matrix where the
17961 window end line is found. It has been moved up or down in
17962 the matrix by dvpos. */
17963 int last_vpos = w->window_end_vpos + dvpos;
17964 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17965
17966 /* If last_row is the window end line, it should display text. */
17967 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17968
17969 /* If window end line was partially visible before, begin
17970 displaying at that line. Otherwise begin displaying with the
17971 line following it. */
17972 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17973 {
17974 init_to_row_start (&it, w, last_row);
17975 it.vpos = last_vpos;
17976 it.current_y = last_row->y;
17977 }
17978 else
17979 {
17980 init_to_row_end (&it, w, last_row);
17981 it.vpos = 1 + last_vpos;
17982 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17983 ++last_row;
17984 }
17985
17986 /* We may start in a continuation line. If so, we have to
17987 get the right continuation_lines_width and current_x. */
17988 it.continuation_lines_width = last_row->continuation_lines_width;
17989 it.hpos = it.current_x = 0;
17990
17991 /* Display the rest of the lines at the window end. */
17992 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17993 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17994 {
17995 /* Is it always sure that the display agrees with lines in
17996 the current matrix? I don't think so, so we mark rows
17997 displayed invalid in the current matrix by setting their
17998 enabled_p flag to zero. */
17999 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18000 if (display_line (&it))
18001 last_text_row_at_end = it.glyph_row - 1;
18002 }
18003 }
18004
18005 /* Update window_end_pos and window_end_vpos. */
18006 if (first_unchanged_at_end_row && !last_text_row_at_end)
18007 {
18008 /* Window end line if one of the preserved rows from the current
18009 matrix. Set row to the last row displaying text in current
18010 matrix starting at first_unchanged_at_end_row, after
18011 scrolling. */
18012 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18013 row = find_last_row_displaying_text (w->current_matrix, &it,
18014 first_unchanged_at_end_row);
18015 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18016 adjust_window_ends (w, row, 1);
18017 eassert (w->window_end_bytepos >= 0);
18018 IF_DEBUG (debug_method_add (w, "A"));
18019 }
18020 else if (last_text_row_at_end)
18021 {
18022 adjust_window_ends (w, last_text_row_at_end, 0);
18023 eassert (w->window_end_bytepos >= 0);
18024 IF_DEBUG (debug_method_add (w, "B"));
18025 }
18026 else if (last_text_row)
18027 {
18028 /* We have displayed either to the end of the window or at the
18029 end of the window, i.e. the last row with text is to be found
18030 in the desired matrix. */
18031 adjust_window_ends (w, last_text_row, 0);
18032 eassert (w->window_end_bytepos >= 0);
18033 }
18034 else if (first_unchanged_at_end_row == NULL
18035 && last_text_row == NULL
18036 && last_text_row_at_end == NULL)
18037 {
18038 /* Displayed to end of window, but no line containing text was
18039 displayed. Lines were deleted at the end of the window. */
18040 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18041 int vpos = w->window_end_vpos;
18042 struct glyph_row *current_row = current_matrix->rows + vpos;
18043 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18044
18045 for (row = NULL;
18046 row == NULL && vpos >= first_vpos;
18047 --vpos, --current_row, --desired_row)
18048 {
18049 if (desired_row->enabled_p)
18050 {
18051 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18052 row = desired_row;
18053 }
18054 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18055 row = current_row;
18056 }
18057
18058 eassert (row != NULL);
18059 w->window_end_vpos = vpos + 1;
18060 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18061 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18062 eassert (w->window_end_bytepos >= 0);
18063 IF_DEBUG (debug_method_add (w, "C"));
18064 }
18065 else
18066 emacs_abort ();
18067
18068 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18069 debug_end_vpos = w->window_end_vpos));
18070
18071 /* Record that display has not been completed. */
18072 w->window_end_valid = 0;
18073 w->desired_matrix->no_scrolling_p = 1;
18074 return 3;
18075
18076 #undef GIVE_UP
18077 }
18078
18079
18080 \f
18081 /***********************************************************************
18082 More debugging support
18083 ***********************************************************************/
18084
18085 #ifdef GLYPH_DEBUG
18086
18087 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18088 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18089 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18090
18091
18092 /* Dump the contents of glyph matrix MATRIX on stderr.
18093
18094 GLYPHS 0 means don't show glyph contents.
18095 GLYPHS 1 means show glyphs in short form
18096 GLYPHS > 1 means show glyphs in long form. */
18097
18098 void
18099 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18100 {
18101 int i;
18102 for (i = 0; i < matrix->nrows; ++i)
18103 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18104 }
18105
18106
18107 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18108 the glyph row and area where the glyph comes from. */
18109
18110 void
18111 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18112 {
18113 if (glyph->type == CHAR_GLYPH
18114 || glyph->type == GLYPHLESS_GLYPH)
18115 {
18116 fprintf (stderr,
18117 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18118 glyph - row->glyphs[TEXT_AREA],
18119 (glyph->type == CHAR_GLYPH
18120 ? 'C'
18121 : 'G'),
18122 glyph->charpos,
18123 (BUFFERP (glyph->object)
18124 ? 'B'
18125 : (STRINGP (glyph->object)
18126 ? 'S'
18127 : (INTEGERP (glyph->object)
18128 ? '0'
18129 : '-'))),
18130 glyph->pixel_width,
18131 glyph->u.ch,
18132 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18133 ? glyph->u.ch
18134 : '.'),
18135 glyph->face_id,
18136 glyph->left_box_line_p,
18137 glyph->right_box_line_p);
18138 }
18139 else if (glyph->type == STRETCH_GLYPH)
18140 {
18141 fprintf (stderr,
18142 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18143 glyph - row->glyphs[TEXT_AREA],
18144 'S',
18145 glyph->charpos,
18146 (BUFFERP (glyph->object)
18147 ? 'B'
18148 : (STRINGP (glyph->object)
18149 ? 'S'
18150 : (INTEGERP (glyph->object)
18151 ? '0'
18152 : '-'))),
18153 glyph->pixel_width,
18154 0,
18155 ' ',
18156 glyph->face_id,
18157 glyph->left_box_line_p,
18158 glyph->right_box_line_p);
18159 }
18160 else if (glyph->type == IMAGE_GLYPH)
18161 {
18162 fprintf (stderr,
18163 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18164 glyph - row->glyphs[TEXT_AREA],
18165 'I',
18166 glyph->charpos,
18167 (BUFFERP (glyph->object)
18168 ? 'B'
18169 : (STRINGP (glyph->object)
18170 ? 'S'
18171 : (INTEGERP (glyph->object)
18172 ? '0'
18173 : '-'))),
18174 glyph->pixel_width,
18175 glyph->u.img_id,
18176 '.',
18177 glyph->face_id,
18178 glyph->left_box_line_p,
18179 glyph->right_box_line_p);
18180 }
18181 else if (glyph->type == COMPOSITE_GLYPH)
18182 {
18183 fprintf (stderr,
18184 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18185 glyph - row->glyphs[TEXT_AREA],
18186 '+',
18187 glyph->charpos,
18188 (BUFFERP (glyph->object)
18189 ? 'B'
18190 : (STRINGP (glyph->object)
18191 ? 'S'
18192 : (INTEGERP (glyph->object)
18193 ? '0'
18194 : '-'))),
18195 glyph->pixel_width,
18196 glyph->u.cmp.id);
18197 if (glyph->u.cmp.automatic)
18198 fprintf (stderr,
18199 "[%d-%d]",
18200 glyph->slice.cmp.from, glyph->slice.cmp.to);
18201 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18202 glyph->face_id,
18203 glyph->left_box_line_p,
18204 glyph->right_box_line_p);
18205 }
18206 }
18207
18208
18209 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18210 GLYPHS 0 means don't show glyph contents.
18211 GLYPHS 1 means show glyphs in short form
18212 GLYPHS > 1 means show glyphs in long form. */
18213
18214 void
18215 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18216 {
18217 if (glyphs != 1)
18218 {
18219 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18220 fprintf (stderr, "==============================================================================\n");
18221
18222 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18223 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18224 vpos,
18225 MATRIX_ROW_START_CHARPOS (row),
18226 MATRIX_ROW_END_CHARPOS (row),
18227 row->used[TEXT_AREA],
18228 row->contains_overlapping_glyphs_p,
18229 row->enabled_p,
18230 row->truncated_on_left_p,
18231 row->truncated_on_right_p,
18232 row->continued_p,
18233 MATRIX_ROW_CONTINUATION_LINE_P (row),
18234 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18235 row->ends_at_zv_p,
18236 row->fill_line_p,
18237 row->ends_in_middle_of_char_p,
18238 row->starts_in_middle_of_char_p,
18239 row->mouse_face_p,
18240 row->x,
18241 row->y,
18242 row->pixel_width,
18243 row->height,
18244 row->visible_height,
18245 row->ascent,
18246 row->phys_ascent);
18247 /* The next 3 lines should align to "Start" in the header. */
18248 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18249 row->end.overlay_string_index,
18250 row->continuation_lines_width);
18251 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18252 CHARPOS (row->start.string_pos),
18253 CHARPOS (row->end.string_pos));
18254 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18255 row->end.dpvec_index);
18256 }
18257
18258 if (glyphs > 1)
18259 {
18260 int area;
18261
18262 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18263 {
18264 struct glyph *glyph = row->glyphs[area];
18265 struct glyph *glyph_end = glyph + row->used[area];
18266
18267 /* Glyph for a line end in text. */
18268 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18269 ++glyph_end;
18270
18271 if (glyph < glyph_end)
18272 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18273
18274 for (; glyph < glyph_end; ++glyph)
18275 dump_glyph (row, glyph, area);
18276 }
18277 }
18278 else if (glyphs == 1)
18279 {
18280 int area;
18281
18282 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18283 {
18284 char *s = alloca (row->used[area] + 4);
18285 int i;
18286
18287 for (i = 0; i < row->used[area]; ++i)
18288 {
18289 struct glyph *glyph = row->glyphs[area] + i;
18290 if (i == row->used[area] - 1
18291 && area == TEXT_AREA
18292 && INTEGERP (glyph->object)
18293 && glyph->type == CHAR_GLYPH
18294 && glyph->u.ch == ' ')
18295 {
18296 strcpy (&s[i], "[\\n]");
18297 i += 4;
18298 }
18299 else if (glyph->type == CHAR_GLYPH
18300 && glyph->u.ch < 0x80
18301 && glyph->u.ch >= ' ')
18302 s[i] = glyph->u.ch;
18303 else
18304 s[i] = '.';
18305 }
18306
18307 s[i] = '\0';
18308 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18309 }
18310 }
18311 }
18312
18313
18314 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18315 Sdump_glyph_matrix, 0, 1, "p",
18316 doc: /* Dump the current matrix of the selected window to stderr.
18317 Shows contents of glyph row structures. With non-nil
18318 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18319 glyphs in short form, otherwise show glyphs in long form. */)
18320 (Lisp_Object glyphs)
18321 {
18322 struct window *w = XWINDOW (selected_window);
18323 struct buffer *buffer = XBUFFER (w->contents);
18324
18325 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18326 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18327 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18328 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18329 fprintf (stderr, "=============================================\n");
18330 dump_glyph_matrix (w->current_matrix,
18331 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18332 return Qnil;
18333 }
18334
18335
18336 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18337 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18338 (void)
18339 {
18340 struct frame *f = XFRAME (selected_frame);
18341 dump_glyph_matrix (f->current_matrix, 1);
18342 return Qnil;
18343 }
18344
18345
18346 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18347 doc: /* Dump glyph row ROW to stderr.
18348 GLYPH 0 means don't dump glyphs.
18349 GLYPH 1 means dump glyphs in short form.
18350 GLYPH > 1 or omitted means dump glyphs in long form. */)
18351 (Lisp_Object row, Lisp_Object glyphs)
18352 {
18353 struct glyph_matrix *matrix;
18354 EMACS_INT vpos;
18355
18356 CHECK_NUMBER (row);
18357 matrix = XWINDOW (selected_window)->current_matrix;
18358 vpos = XINT (row);
18359 if (vpos >= 0 && vpos < matrix->nrows)
18360 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18361 vpos,
18362 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18363 return Qnil;
18364 }
18365
18366
18367 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18368 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18369 GLYPH 0 means don't dump glyphs.
18370 GLYPH 1 means dump glyphs in short form.
18371 GLYPH > 1 or omitted means dump glyphs in long form.
18372
18373 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18374 do nothing. */)
18375 (Lisp_Object row, Lisp_Object glyphs)
18376 {
18377 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18378 struct frame *sf = SELECTED_FRAME ();
18379 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18380 EMACS_INT vpos;
18381
18382 CHECK_NUMBER (row);
18383 vpos = XINT (row);
18384 if (vpos >= 0 && vpos < m->nrows)
18385 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18386 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18387 #endif
18388 return Qnil;
18389 }
18390
18391
18392 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18393 doc: /* Toggle tracing of redisplay.
18394 With ARG, turn tracing on if and only if ARG is positive. */)
18395 (Lisp_Object arg)
18396 {
18397 if (NILP (arg))
18398 trace_redisplay_p = !trace_redisplay_p;
18399 else
18400 {
18401 arg = Fprefix_numeric_value (arg);
18402 trace_redisplay_p = XINT (arg) > 0;
18403 }
18404
18405 return Qnil;
18406 }
18407
18408
18409 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18410 doc: /* Like `format', but print result to stderr.
18411 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18412 (ptrdiff_t nargs, Lisp_Object *args)
18413 {
18414 Lisp_Object s = Fformat (nargs, args);
18415 fprintf (stderr, "%s", SDATA (s));
18416 return Qnil;
18417 }
18418
18419 #endif /* GLYPH_DEBUG */
18420
18421
18422 \f
18423 /***********************************************************************
18424 Building Desired Matrix Rows
18425 ***********************************************************************/
18426
18427 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18428 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18429
18430 static struct glyph_row *
18431 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18432 {
18433 struct frame *f = XFRAME (WINDOW_FRAME (w));
18434 struct buffer *buffer = XBUFFER (w->contents);
18435 struct buffer *old = current_buffer;
18436 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18437 int arrow_len = SCHARS (overlay_arrow_string);
18438 const unsigned char *arrow_end = arrow_string + arrow_len;
18439 const unsigned char *p;
18440 struct it it;
18441 bool multibyte_p;
18442 int n_glyphs_before;
18443
18444 set_buffer_temp (buffer);
18445 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18446 it.glyph_row->used[TEXT_AREA] = 0;
18447 SET_TEXT_POS (it.position, 0, 0);
18448
18449 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18450 p = arrow_string;
18451 while (p < arrow_end)
18452 {
18453 Lisp_Object face, ilisp;
18454
18455 /* Get the next character. */
18456 if (multibyte_p)
18457 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18458 else
18459 {
18460 it.c = it.char_to_display = *p, it.len = 1;
18461 if (! ASCII_CHAR_P (it.c))
18462 it.char_to_display = BYTE8_TO_CHAR (it.c);
18463 }
18464 p += it.len;
18465
18466 /* Get its face. */
18467 ilisp = make_number (p - arrow_string);
18468 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18469 it.face_id = compute_char_face (f, it.char_to_display, face);
18470
18471 /* Compute its width, get its glyphs. */
18472 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18473 SET_TEXT_POS (it.position, -1, -1);
18474 PRODUCE_GLYPHS (&it);
18475
18476 /* If this character doesn't fit any more in the line, we have
18477 to remove some glyphs. */
18478 if (it.current_x > it.last_visible_x)
18479 {
18480 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18481 break;
18482 }
18483 }
18484
18485 set_buffer_temp (old);
18486 return it.glyph_row;
18487 }
18488
18489
18490 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18491 glyphs to insert is determined by produce_special_glyphs. */
18492
18493 static void
18494 insert_left_trunc_glyphs (struct it *it)
18495 {
18496 struct it truncate_it;
18497 struct glyph *from, *end, *to, *toend;
18498
18499 eassert (!FRAME_WINDOW_P (it->f)
18500 || (!it->glyph_row->reversed_p
18501 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18502 || (it->glyph_row->reversed_p
18503 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18504
18505 /* Get the truncation glyphs. */
18506 truncate_it = *it;
18507 truncate_it.current_x = 0;
18508 truncate_it.face_id = DEFAULT_FACE_ID;
18509 truncate_it.glyph_row = &scratch_glyph_row;
18510 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18511 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18512 truncate_it.object = make_number (0);
18513 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18514
18515 /* Overwrite glyphs from IT with truncation glyphs. */
18516 if (!it->glyph_row->reversed_p)
18517 {
18518 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18519
18520 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18521 end = from + tused;
18522 to = it->glyph_row->glyphs[TEXT_AREA];
18523 toend = to + it->glyph_row->used[TEXT_AREA];
18524 if (FRAME_WINDOW_P (it->f))
18525 {
18526 /* On GUI frames, when variable-size fonts are displayed,
18527 the truncation glyphs may need more pixels than the row's
18528 glyphs they overwrite. We overwrite more glyphs to free
18529 enough screen real estate, and enlarge the stretch glyph
18530 on the right (see display_line), if there is one, to
18531 preserve the screen position of the truncation glyphs on
18532 the right. */
18533 int w = 0;
18534 struct glyph *g = to;
18535 short used;
18536
18537 /* The first glyph could be partially visible, in which case
18538 it->glyph_row->x will be negative. But we want the left
18539 truncation glyphs to be aligned at the left margin of the
18540 window, so we override the x coordinate at which the row
18541 will begin. */
18542 it->glyph_row->x = 0;
18543 while (g < toend && w < it->truncation_pixel_width)
18544 {
18545 w += g->pixel_width;
18546 ++g;
18547 }
18548 if (g - to - tused > 0)
18549 {
18550 memmove (to + tused, g, (toend - g) * sizeof(*g));
18551 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18552 }
18553 used = it->glyph_row->used[TEXT_AREA];
18554 if (it->glyph_row->truncated_on_right_p
18555 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18556 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18557 == STRETCH_GLYPH)
18558 {
18559 int extra = w - it->truncation_pixel_width;
18560
18561 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18562 }
18563 }
18564
18565 while (from < end)
18566 *to++ = *from++;
18567
18568 /* There may be padding glyphs left over. Overwrite them too. */
18569 if (!FRAME_WINDOW_P (it->f))
18570 {
18571 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18572 {
18573 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18574 while (from < end)
18575 *to++ = *from++;
18576 }
18577 }
18578
18579 if (to > toend)
18580 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18581 }
18582 else
18583 {
18584 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18585
18586 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18587 that back to front. */
18588 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18589 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18590 toend = it->glyph_row->glyphs[TEXT_AREA];
18591 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18592 if (FRAME_WINDOW_P (it->f))
18593 {
18594 int w = 0;
18595 struct glyph *g = to;
18596
18597 while (g >= toend && w < it->truncation_pixel_width)
18598 {
18599 w += g->pixel_width;
18600 --g;
18601 }
18602 if (to - g - tused > 0)
18603 to = g + tused;
18604 if (it->glyph_row->truncated_on_right_p
18605 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18606 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18607 {
18608 int extra = w - it->truncation_pixel_width;
18609
18610 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18611 }
18612 }
18613
18614 while (from >= end && to >= toend)
18615 *to-- = *from--;
18616 if (!FRAME_WINDOW_P (it->f))
18617 {
18618 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18619 {
18620 from =
18621 truncate_it.glyph_row->glyphs[TEXT_AREA]
18622 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18623 while (from >= end && to >= toend)
18624 *to-- = *from--;
18625 }
18626 }
18627 if (from >= end)
18628 {
18629 /* Need to free some room before prepending additional
18630 glyphs. */
18631 int move_by = from - end + 1;
18632 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18633 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18634
18635 for ( ; g >= g0; g--)
18636 g[move_by] = *g;
18637 while (from >= end)
18638 *to-- = *from--;
18639 it->glyph_row->used[TEXT_AREA] += move_by;
18640 }
18641 }
18642 }
18643
18644 /* Compute the hash code for ROW. */
18645 unsigned
18646 row_hash (struct glyph_row *row)
18647 {
18648 int area, k;
18649 unsigned hashval = 0;
18650
18651 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18652 for (k = 0; k < row->used[area]; ++k)
18653 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18654 + row->glyphs[area][k].u.val
18655 + row->glyphs[area][k].face_id
18656 + row->glyphs[area][k].padding_p
18657 + (row->glyphs[area][k].type << 2));
18658
18659 return hashval;
18660 }
18661
18662 /* Compute the pixel height and width of IT->glyph_row.
18663
18664 Most of the time, ascent and height of a display line will be equal
18665 to the max_ascent and max_height values of the display iterator
18666 structure. This is not the case if
18667
18668 1. We hit ZV without displaying anything. In this case, max_ascent
18669 and max_height will be zero.
18670
18671 2. We have some glyphs that don't contribute to the line height.
18672 (The glyph row flag contributes_to_line_height_p is for future
18673 pixmap extensions).
18674
18675 The first case is easily covered by using default values because in
18676 these cases, the line height does not really matter, except that it
18677 must not be zero. */
18678
18679 static void
18680 compute_line_metrics (struct it *it)
18681 {
18682 struct glyph_row *row = it->glyph_row;
18683
18684 if (FRAME_WINDOW_P (it->f))
18685 {
18686 int i, min_y, max_y;
18687
18688 /* The line may consist of one space only, that was added to
18689 place the cursor on it. If so, the row's height hasn't been
18690 computed yet. */
18691 if (row->height == 0)
18692 {
18693 if (it->max_ascent + it->max_descent == 0)
18694 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18695 row->ascent = it->max_ascent;
18696 row->height = it->max_ascent + it->max_descent;
18697 row->phys_ascent = it->max_phys_ascent;
18698 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18699 row->extra_line_spacing = it->max_extra_line_spacing;
18700 }
18701
18702 /* Compute the width of this line. */
18703 row->pixel_width = row->x;
18704 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18705 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18706
18707 eassert (row->pixel_width >= 0);
18708 eassert (row->ascent >= 0 && row->height > 0);
18709
18710 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18711 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18712
18713 /* If first line's physical ascent is larger than its logical
18714 ascent, use the physical ascent, and make the row taller.
18715 This makes accented characters fully visible. */
18716 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18717 && row->phys_ascent > row->ascent)
18718 {
18719 row->height += row->phys_ascent - row->ascent;
18720 row->ascent = row->phys_ascent;
18721 }
18722
18723 /* Compute how much of the line is visible. */
18724 row->visible_height = row->height;
18725
18726 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18727 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18728
18729 if (row->y < min_y)
18730 row->visible_height -= min_y - row->y;
18731 if (row->y + row->height > max_y)
18732 row->visible_height -= row->y + row->height - max_y;
18733 }
18734 else
18735 {
18736 row->pixel_width = row->used[TEXT_AREA];
18737 if (row->continued_p)
18738 row->pixel_width -= it->continuation_pixel_width;
18739 else if (row->truncated_on_right_p)
18740 row->pixel_width -= it->truncation_pixel_width;
18741 row->ascent = row->phys_ascent = 0;
18742 row->height = row->phys_height = row->visible_height = 1;
18743 row->extra_line_spacing = 0;
18744 }
18745
18746 /* Compute a hash code for this row. */
18747 row->hash = row_hash (row);
18748
18749 it->max_ascent = it->max_descent = 0;
18750 it->max_phys_ascent = it->max_phys_descent = 0;
18751 }
18752
18753
18754 /* Append one space to the glyph row of iterator IT if doing a
18755 window-based redisplay. The space has the same face as
18756 IT->face_id. Value is non-zero if a space was added.
18757
18758 This function is called to make sure that there is always one glyph
18759 at the end of a glyph row that the cursor can be set on under
18760 window-systems. (If there weren't such a glyph we would not know
18761 how wide and tall a box cursor should be displayed).
18762
18763 At the same time this space let's a nicely handle clearing to the
18764 end of the line if the row ends in italic text. */
18765
18766 static int
18767 append_space_for_newline (struct it *it, int default_face_p)
18768 {
18769 if (FRAME_WINDOW_P (it->f))
18770 {
18771 int n = it->glyph_row->used[TEXT_AREA];
18772
18773 if (it->glyph_row->glyphs[TEXT_AREA] + n
18774 < it->glyph_row->glyphs[1 + TEXT_AREA])
18775 {
18776 /* Save some values that must not be changed.
18777 Must save IT->c and IT->len because otherwise
18778 ITERATOR_AT_END_P wouldn't work anymore after
18779 append_space_for_newline has been called. */
18780 enum display_element_type saved_what = it->what;
18781 int saved_c = it->c, saved_len = it->len;
18782 int saved_char_to_display = it->char_to_display;
18783 int saved_x = it->current_x;
18784 int saved_face_id = it->face_id;
18785 int saved_box_end = it->end_of_box_run_p;
18786 struct text_pos saved_pos;
18787 Lisp_Object saved_object;
18788 struct face *face;
18789
18790 saved_object = it->object;
18791 saved_pos = it->position;
18792
18793 it->what = IT_CHARACTER;
18794 memset (&it->position, 0, sizeof it->position);
18795 it->object = make_number (0);
18796 it->c = it->char_to_display = ' ';
18797 it->len = 1;
18798
18799 /* If the default face was remapped, be sure to use the
18800 remapped face for the appended newline. */
18801 if (default_face_p)
18802 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18803 else if (it->face_before_selective_p)
18804 it->face_id = it->saved_face_id;
18805 face = FACE_FROM_ID (it->f, it->face_id);
18806 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18807 /* In R2L rows, we will prepend a stretch glyph that will
18808 have the end_of_box_run_p flag set for it, so there's no
18809 need for the appended newline glyph to have that flag
18810 set. */
18811 if (it->glyph_row->reversed_p
18812 /* But if the appended newline glyph goes all the way to
18813 the end of the row, there will be no stretch glyph,
18814 so leave the box flag set. */
18815 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18816 it->end_of_box_run_p = 0;
18817
18818 PRODUCE_GLYPHS (it);
18819
18820 it->override_ascent = -1;
18821 it->constrain_row_ascent_descent_p = 0;
18822 it->current_x = saved_x;
18823 it->object = saved_object;
18824 it->position = saved_pos;
18825 it->what = saved_what;
18826 it->face_id = saved_face_id;
18827 it->len = saved_len;
18828 it->c = saved_c;
18829 it->char_to_display = saved_char_to_display;
18830 it->end_of_box_run_p = saved_box_end;
18831 return 1;
18832 }
18833 }
18834
18835 return 0;
18836 }
18837
18838
18839 /* Extend the face of the last glyph in the text area of IT->glyph_row
18840 to the end of the display line. Called from display_line. If the
18841 glyph row is empty, add a space glyph to it so that we know the
18842 face to draw. Set the glyph row flag fill_line_p. If the glyph
18843 row is R2L, prepend a stretch glyph to cover the empty space to the
18844 left of the leftmost glyph. */
18845
18846 static void
18847 extend_face_to_end_of_line (struct it *it)
18848 {
18849 struct face *face, *default_face;
18850 struct frame *f = it->f;
18851
18852 /* If line is already filled, do nothing. Non window-system frames
18853 get a grace of one more ``pixel'' because their characters are
18854 1-``pixel'' wide, so they hit the equality too early. This grace
18855 is needed only for R2L rows that are not continued, to produce
18856 one extra blank where we could display the cursor. */
18857 if ((it->current_x >= it->last_visible_x
18858 + (!FRAME_WINDOW_P (f)
18859 && it->glyph_row->reversed_p
18860 && !it->glyph_row->continued_p))
18861 /* If the window has display margins, we will need to extend
18862 their face even if the text area is filled. */
18863 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18864 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18865 return;
18866
18867 /* The default face, possibly remapped. */
18868 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18869
18870 /* Face extension extends the background and box of IT->face_id
18871 to the end of the line. If the background equals the background
18872 of the frame, we don't have to do anything. */
18873 if (it->face_before_selective_p)
18874 face = FACE_FROM_ID (f, it->saved_face_id);
18875 else
18876 face = FACE_FROM_ID (f, it->face_id);
18877
18878 if (FRAME_WINDOW_P (f)
18879 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18880 && face->box == FACE_NO_BOX
18881 && face->background == FRAME_BACKGROUND_PIXEL (f)
18882 #ifdef HAVE_WINDOW_SYSTEM
18883 && !face->stipple
18884 #endif
18885 && !it->glyph_row->reversed_p)
18886 return;
18887
18888 /* Set the glyph row flag indicating that the face of the last glyph
18889 in the text area has to be drawn to the end of the text area. */
18890 it->glyph_row->fill_line_p = 1;
18891
18892 /* If current character of IT is not ASCII, make sure we have the
18893 ASCII face. This will be automatically undone the next time
18894 get_next_display_element returns a multibyte character. Note
18895 that the character will always be single byte in unibyte
18896 text. */
18897 if (!ASCII_CHAR_P (it->c))
18898 {
18899 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18900 }
18901
18902 if (FRAME_WINDOW_P (f))
18903 {
18904 /* If the row is empty, add a space with the current face of IT,
18905 so that we know which face to draw. */
18906 if (it->glyph_row->used[TEXT_AREA] == 0)
18907 {
18908 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18909 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18910 it->glyph_row->used[TEXT_AREA] = 1;
18911 }
18912 /* Mode line and the header line don't have margins, and
18913 likewise the frame's tool-bar window, if there is any. */
18914 if (!(it->glyph_row->mode_line_p
18915 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18916 || (WINDOWP (f->tool_bar_window)
18917 && it->w == XWINDOW (f->tool_bar_window))
18918 #endif
18919 ))
18920 {
18921 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18922 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18923 {
18924 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18925 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18926 default_face->id;
18927 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18928 }
18929 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18930 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18931 {
18932 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18933 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18934 default_face->id;
18935 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18936 }
18937 }
18938 #ifdef HAVE_WINDOW_SYSTEM
18939 if (it->glyph_row->reversed_p)
18940 {
18941 /* Prepend a stretch glyph to the row, such that the
18942 rightmost glyph will be drawn flushed all the way to the
18943 right margin of the window. The stretch glyph that will
18944 occupy the empty space, if any, to the left of the
18945 glyphs. */
18946 struct font *font = face->font ? face->font : FRAME_FONT (f);
18947 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18948 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18949 struct glyph *g;
18950 int row_width, stretch_ascent, stretch_width;
18951 struct text_pos saved_pos;
18952 int saved_face_id, saved_avoid_cursor, saved_box_start;
18953
18954 for (row_width = 0, g = row_start; g < row_end; g++)
18955 row_width += g->pixel_width;
18956 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18957 if (stretch_width > 0)
18958 {
18959 stretch_ascent =
18960 (((it->ascent + it->descent)
18961 * FONT_BASE (font)) / FONT_HEIGHT (font));
18962 saved_pos = it->position;
18963 memset (&it->position, 0, sizeof it->position);
18964 saved_avoid_cursor = it->avoid_cursor_p;
18965 it->avoid_cursor_p = 1;
18966 saved_face_id = it->face_id;
18967 saved_box_start = it->start_of_box_run_p;
18968 /* The last row's stretch glyph should get the default
18969 face, to avoid painting the rest of the window with
18970 the region face, if the region ends at ZV. */
18971 if (it->glyph_row->ends_at_zv_p)
18972 it->face_id = default_face->id;
18973 else
18974 it->face_id = face->id;
18975 it->start_of_box_run_p = 0;
18976 append_stretch_glyph (it, make_number (0), stretch_width,
18977 it->ascent + it->descent, stretch_ascent);
18978 it->position = saved_pos;
18979 it->avoid_cursor_p = saved_avoid_cursor;
18980 it->face_id = saved_face_id;
18981 it->start_of_box_run_p = saved_box_start;
18982 }
18983 }
18984 #endif /* HAVE_WINDOW_SYSTEM */
18985 }
18986 else
18987 {
18988 /* Save some values that must not be changed. */
18989 int saved_x = it->current_x;
18990 struct text_pos saved_pos;
18991 Lisp_Object saved_object;
18992 enum display_element_type saved_what = it->what;
18993 int saved_face_id = it->face_id;
18994
18995 saved_object = it->object;
18996 saved_pos = it->position;
18997
18998 it->what = IT_CHARACTER;
18999 memset (&it->position, 0, sizeof it->position);
19000 it->object = make_number (0);
19001 it->c = it->char_to_display = ' ';
19002 it->len = 1;
19003
19004 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19005 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19006 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19007 && !it->glyph_row->mode_line_p
19008 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19009 {
19010 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19011 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19012
19013 for (it->current_x = 0; g < e; g++)
19014 it->current_x += g->pixel_width;
19015
19016 it->area = LEFT_MARGIN_AREA;
19017 it->face_id = default_face->id;
19018 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19019 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19020 {
19021 PRODUCE_GLYPHS (it);
19022 /* term.c:produce_glyphs advances it->current_x only for
19023 TEXT_AREA. */
19024 it->current_x += it->pixel_width;
19025 }
19026
19027 it->current_x = saved_x;
19028 it->area = TEXT_AREA;
19029 }
19030
19031 /* The last row's blank glyphs should get the default face, to
19032 avoid painting the rest of the window with the region face,
19033 if the region ends at ZV. */
19034 if (it->glyph_row->ends_at_zv_p)
19035 it->face_id = default_face->id;
19036 else
19037 it->face_id = face->id;
19038 PRODUCE_GLYPHS (it);
19039
19040 while (it->current_x <= it->last_visible_x)
19041 PRODUCE_GLYPHS (it);
19042
19043 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19044 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19045 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19046 && !it->glyph_row->mode_line_p
19047 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19048 {
19049 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19050 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19051
19052 for ( ; g < e; g++)
19053 it->current_x += g->pixel_width;
19054
19055 it->area = RIGHT_MARGIN_AREA;
19056 it->face_id = default_face->id;
19057 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19058 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19059 {
19060 PRODUCE_GLYPHS (it);
19061 it->current_x += it->pixel_width;
19062 }
19063
19064 it->area = TEXT_AREA;
19065 }
19066
19067 /* Don't count these blanks really. It would let us insert a left
19068 truncation glyph below and make us set the cursor on them, maybe. */
19069 it->current_x = saved_x;
19070 it->object = saved_object;
19071 it->position = saved_pos;
19072 it->what = saved_what;
19073 it->face_id = saved_face_id;
19074 }
19075 }
19076
19077
19078 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19079 trailing whitespace. */
19080
19081 static int
19082 trailing_whitespace_p (ptrdiff_t charpos)
19083 {
19084 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19085 int c = 0;
19086
19087 while (bytepos < ZV_BYTE
19088 && (c = FETCH_CHAR (bytepos),
19089 c == ' ' || c == '\t'))
19090 ++bytepos;
19091
19092 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19093 {
19094 if (bytepos != PT_BYTE)
19095 return 1;
19096 }
19097 return 0;
19098 }
19099
19100
19101 /* Highlight trailing whitespace, if any, in ROW. */
19102
19103 static void
19104 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19105 {
19106 int used = row->used[TEXT_AREA];
19107
19108 if (used)
19109 {
19110 struct glyph *start = row->glyphs[TEXT_AREA];
19111 struct glyph *glyph = start + used - 1;
19112
19113 if (row->reversed_p)
19114 {
19115 /* Right-to-left rows need to be processed in the opposite
19116 direction, so swap the edge pointers. */
19117 glyph = start;
19118 start = row->glyphs[TEXT_AREA] + used - 1;
19119 }
19120
19121 /* Skip over glyphs inserted to display the cursor at the
19122 end of a line, for extending the face of the last glyph
19123 to the end of the line on terminals, and for truncation
19124 and continuation glyphs. */
19125 if (!row->reversed_p)
19126 {
19127 while (glyph >= start
19128 && glyph->type == CHAR_GLYPH
19129 && INTEGERP (glyph->object))
19130 --glyph;
19131 }
19132 else
19133 {
19134 while (glyph <= start
19135 && glyph->type == CHAR_GLYPH
19136 && INTEGERP (glyph->object))
19137 ++glyph;
19138 }
19139
19140 /* If last glyph is a space or stretch, and it's trailing
19141 whitespace, set the face of all trailing whitespace glyphs in
19142 IT->glyph_row to `trailing-whitespace'. */
19143 if ((row->reversed_p ? glyph <= start : glyph >= start)
19144 && BUFFERP (glyph->object)
19145 && (glyph->type == STRETCH_GLYPH
19146 || (glyph->type == CHAR_GLYPH
19147 && glyph->u.ch == ' '))
19148 && trailing_whitespace_p (glyph->charpos))
19149 {
19150 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19151 if (face_id < 0)
19152 return;
19153
19154 if (!row->reversed_p)
19155 {
19156 while (glyph >= start
19157 && BUFFERP (glyph->object)
19158 && (glyph->type == STRETCH_GLYPH
19159 || (glyph->type == CHAR_GLYPH
19160 && glyph->u.ch == ' ')))
19161 (glyph--)->face_id = face_id;
19162 }
19163 else
19164 {
19165 while (glyph <= start
19166 && BUFFERP (glyph->object)
19167 && (glyph->type == STRETCH_GLYPH
19168 || (glyph->type == CHAR_GLYPH
19169 && glyph->u.ch == ' ')))
19170 (glyph++)->face_id = face_id;
19171 }
19172 }
19173 }
19174 }
19175
19176
19177 /* Value is non-zero if glyph row ROW should be
19178 considered to hold the buffer position CHARPOS. */
19179
19180 static int
19181 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19182 {
19183 int result = 1;
19184
19185 if (charpos == CHARPOS (row->end.pos)
19186 || charpos == MATRIX_ROW_END_CHARPOS (row))
19187 {
19188 /* Suppose the row ends on a string.
19189 Unless the row is continued, that means it ends on a newline
19190 in the string. If it's anything other than a display string
19191 (e.g., a before-string from an overlay), we don't want the
19192 cursor there. (This heuristic seems to give the optimal
19193 behavior for the various types of multi-line strings.)
19194 One exception: if the string has `cursor' property on one of
19195 its characters, we _do_ want the cursor there. */
19196 if (CHARPOS (row->end.string_pos) >= 0)
19197 {
19198 if (row->continued_p)
19199 result = 1;
19200 else
19201 {
19202 /* Check for `display' property. */
19203 struct glyph *beg = row->glyphs[TEXT_AREA];
19204 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19205 struct glyph *glyph;
19206
19207 result = 0;
19208 for (glyph = end; glyph >= beg; --glyph)
19209 if (STRINGP (glyph->object))
19210 {
19211 Lisp_Object prop
19212 = Fget_char_property (make_number (charpos),
19213 Qdisplay, Qnil);
19214 result =
19215 (!NILP (prop)
19216 && display_prop_string_p (prop, glyph->object));
19217 /* If there's a `cursor' property on one of the
19218 string's characters, this row is a cursor row,
19219 even though this is not a display string. */
19220 if (!result)
19221 {
19222 Lisp_Object s = glyph->object;
19223
19224 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19225 {
19226 ptrdiff_t gpos = glyph->charpos;
19227
19228 if (!NILP (Fget_char_property (make_number (gpos),
19229 Qcursor, s)))
19230 {
19231 result = 1;
19232 break;
19233 }
19234 }
19235 }
19236 break;
19237 }
19238 }
19239 }
19240 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19241 {
19242 /* If the row ends in middle of a real character,
19243 and the line is continued, we want the cursor here.
19244 That's because CHARPOS (ROW->end.pos) would equal
19245 PT if PT is before the character. */
19246 if (!row->ends_in_ellipsis_p)
19247 result = row->continued_p;
19248 else
19249 /* If the row ends in an ellipsis, then
19250 CHARPOS (ROW->end.pos) will equal point after the
19251 invisible text. We want that position to be displayed
19252 after the ellipsis. */
19253 result = 0;
19254 }
19255 /* If the row ends at ZV, display the cursor at the end of that
19256 row instead of at the start of the row below. */
19257 else if (row->ends_at_zv_p)
19258 result = 1;
19259 else
19260 result = 0;
19261 }
19262
19263 return result;
19264 }
19265
19266 /* Value is non-zero if glyph row ROW should be
19267 used to hold the cursor. */
19268
19269 static int
19270 cursor_row_p (struct glyph_row *row)
19271 {
19272 return row_for_charpos_p (row, PT);
19273 }
19274
19275 \f
19276
19277 /* Push the property PROP so that it will be rendered at the current
19278 position in IT. Return 1 if PROP was successfully pushed, 0
19279 otherwise. Called from handle_line_prefix to handle the
19280 `line-prefix' and `wrap-prefix' properties. */
19281
19282 static int
19283 push_prefix_prop (struct it *it, Lisp_Object prop)
19284 {
19285 struct text_pos pos =
19286 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19287
19288 eassert (it->method == GET_FROM_BUFFER
19289 || it->method == GET_FROM_DISPLAY_VECTOR
19290 || it->method == GET_FROM_STRING);
19291
19292 /* We need to save the current buffer/string position, so it will be
19293 restored by pop_it, because iterate_out_of_display_property
19294 depends on that being set correctly, but some situations leave
19295 it->position not yet set when this function is called. */
19296 push_it (it, &pos);
19297
19298 if (STRINGP (prop))
19299 {
19300 if (SCHARS (prop) == 0)
19301 {
19302 pop_it (it);
19303 return 0;
19304 }
19305
19306 it->string = prop;
19307 it->string_from_prefix_prop_p = 1;
19308 it->multibyte_p = STRING_MULTIBYTE (it->string);
19309 it->current.overlay_string_index = -1;
19310 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19311 it->end_charpos = it->string_nchars = SCHARS (it->string);
19312 it->method = GET_FROM_STRING;
19313 it->stop_charpos = 0;
19314 it->prev_stop = 0;
19315 it->base_level_stop = 0;
19316
19317 /* Force paragraph direction to be that of the parent
19318 buffer/string. */
19319 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19320 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19321 else
19322 it->paragraph_embedding = L2R;
19323
19324 /* Set up the bidi iterator for this display string. */
19325 if (it->bidi_p)
19326 {
19327 it->bidi_it.string.lstring = it->string;
19328 it->bidi_it.string.s = NULL;
19329 it->bidi_it.string.schars = it->end_charpos;
19330 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19331 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19332 it->bidi_it.string.unibyte = !it->multibyte_p;
19333 it->bidi_it.w = it->w;
19334 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19335 }
19336 }
19337 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19338 {
19339 it->method = GET_FROM_STRETCH;
19340 it->object = prop;
19341 }
19342 #ifdef HAVE_WINDOW_SYSTEM
19343 else if (IMAGEP (prop))
19344 {
19345 it->what = IT_IMAGE;
19346 it->image_id = lookup_image (it->f, prop);
19347 it->method = GET_FROM_IMAGE;
19348 }
19349 #endif /* HAVE_WINDOW_SYSTEM */
19350 else
19351 {
19352 pop_it (it); /* bogus display property, give up */
19353 return 0;
19354 }
19355
19356 return 1;
19357 }
19358
19359 /* Return the character-property PROP at the current position in IT. */
19360
19361 static Lisp_Object
19362 get_it_property (struct it *it, Lisp_Object prop)
19363 {
19364 Lisp_Object position, object = it->object;
19365
19366 if (STRINGP (object))
19367 position = make_number (IT_STRING_CHARPOS (*it));
19368 else if (BUFFERP (object))
19369 {
19370 position = make_number (IT_CHARPOS (*it));
19371 object = it->window;
19372 }
19373 else
19374 return Qnil;
19375
19376 return Fget_char_property (position, prop, object);
19377 }
19378
19379 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19380
19381 static void
19382 handle_line_prefix (struct it *it)
19383 {
19384 Lisp_Object prefix;
19385
19386 if (it->continuation_lines_width > 0)
19387 {
19388 prefix = get_it_property (it, Qwrap_prefix);
19389 if (NILP (prefix))
19390 prefix = Vwrap_prefix;
19391 }
19392 else
19393 {
19394 prefix = get_it_property (it, Qline_prefix);
19395 if (NILP (prefix))
19396 prefix = Vline_prefix;
19397 }
19398 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19399 {
19400 /* If the prefix is wider than the window, and we try to wrap
19401 it, it would acquire its own wrap prefix, and so on till the
19402 iterator stack overflows. So, don't wrap the prefix. */
19403 it->line_wrap = TRUNCATE;
19404 it->avoid_cursor_p = 1;
19405 }
19406 }
19407
19408 \f
19409
19410 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19411 only for R2L lines from display_line and display_string, when they
19412 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19413 the line/string needs to be continued on the next glyph row. */
19414 static void
19415 unproduce_glyphs (struct it *it, int n)
19416 {
19417 struct glyph *glyph, *end;
19418
19419 eassert (it->glyph_row);
19420 eassert (it->glyph_row->reversed_p);
19421 eassert (it->area == TEXT_AREA);
19422 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19423
19424 if (n > it->glyph_row->used[TEXT_AREA])
19425 n = it->glyph_row->used[TEXT_AREA];
19426 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19427 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19428 for ( ; glyph < end; glyph++)
19429 glyph[-n] = *glyph;
19430 }
19431
19432 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19433 and ROW->maxpos. */
19434 static void
19435 find_row_edges (struct it *it, struct glyph_row *row,
19436 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19437 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19438 {
19439 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19440 lines' rows is implemented for bidi-reordered rows. */
19441
19442 /* ROW->minpos is the value of min_pos, the minimal buffer position
19443 we have in ROW, or ROW->start.pos if that is smaller. */
19444 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19445 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19446 else
19447 /* We didn't find buffer positions smaller than ROW->start, or
19448 didn't find _any_ valid buffer positions in any of the glyphs,
19449 so we must trust the iterator's computed positions. */
19450 row->minpos = row->start.pos;
19451 if (max_pos <= 0)
19452 {
19453 max_pos = CHARPOS (it->current.pos);
19454 max_bpos = BYTEPOS (it->current.pos);
19455 }
19456
19457 /* Here are the various use-cases for ending the row, and the
19458 corresponding values for ROW->maxpos:
19459
19460 Line ends in a newline from buffer eol_pos + 1
19461 Line is continued from buffer max_pos + 1
19462 Line is truncated on right it->current.pos
19463 Line ends in a newline from string max_pos + 1(*)
19464 (*) + 1 only when line ends in a forward scan
19465 Line is continued from string max_pos
19466 Line is continued from display vector max_pos
19467 Line is entirely from a string min_pos == max_pos
19468 Line is entirely from a display vector min_pos == max_pos
19469 Line that ends at ZV ZV
19470
19471 If you discover other use-cases, please add them here as
19472 appropriate. */
19473 if (row->ends_at_zv_p)
19474 row->maxpos = it->current.pos;
19475 else if (row->used[TEXT_AREA])
19476 {
19477 int seen_this_string = 0;
19478 struct glyph_row *r1 = row - 1;
19479
19480 /* Did we see the same display string on the previous row? */
19481 if (STRINGP (it->object)
19482 /* this is not the first row */
19483 && row > it->w->desired_matrix->rows
19484 /* previous row is not the header line */
19485 && !r1->mode_line_p
19486 /* previous row also ends in a newline from a string */
19487 && r1->ends_in_newline_from_string_p)
19488 {
19489 struct glyph *start, *end;
19490
19491 /* Search for the last glyph of the previous row that came
19492 from buffer or string. Depending on whether the row is
19493 L2R or R2L, we need to process it front to back or the
19494 other way round. */
19495 if (!r1->reversed_p)
19496 {
19497 start = r1->glyphs[TEXT_AREA];
19498 end = start + r1->used[TEXT_AREA];
19499 /* Glyphs inserted by redisplay have an integer (zero)
19500 as their object. */
19501 while (end > start
19502 && INTEGERP ((end - 1)->object)
19503 && (end - 1)->charpos <= 0)
19504 --end;
19505 if (end > start)
19506 {
19507 if (EQ ((end - 1)->object, it->object))
19508 seen_this_string = 1;
19509 }
19510 else
19511 /* If all the glyphs of the previous row were inserted
19512 by redisplay, it means the previous row was
19513 produced from a single newline, which is only
19514 possible if that newline came from the same string
19515 as the one which produced this ROW. */
19516 seen_this_string = 1;
19517 }
19518 else
19519 {
19520 end = r1->glyphs[TEXT_AREA] - 1;
19521 start = end + r1->used[TEXT_AREA];
19522 while (end < start
19523 && INTEGERP ((end + 1)->object)
19524 && (end + 1)->charpos <= 0)
19525 ++end;
19526 if (end < start)
19527 {
19528 if (EQ ((end + 1)->object, it->object))
19529 seen_this_string = 1;
19530 }
19531 else
19532 seen_this_string = 1;
19533 }
19534 }
19535 /* Take note of each display string that covers a newline only
19536 once, the first time we see it. This is for when a display
19537 string includes more than one newline in it. */
19538 if (row->ends_in_newline_from_string_p && !seen_this_string)
19539 {
19540 /* If we were scanning the buffer forward when we displayed
19541 the string, we want to account for at least one buffer
19542 position that belongs to this row (position covered by
19543 the display string), so that cursor positioning will
19544 consider this row as a candidate when point is at the end
19545 of the visual line represented by this row. This is not
19546 required when scanning back, because max_pos will already
19547 have a much larger value. */
19548 if (CHARPOS (row->end.pos) > max_pos)
19549 INC_BOTH (max_pos, max_bpos);
19550 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19551 }
19552 else if (CHARPOS (it->eol_pos) > 0)
19553 SET_TEXT_POS (row->maxpos,
19554 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19555 else if (row->continued_p)
19556 {
19557 /* If max_pos is different from IT's current position, it
19558 means IT->method does not belong to the display element
19559 at max_pos. However, it also means that the display
19560 element at max_pos was displayed in its entirety on this
19561 line, which is equivalent to saying that the next line
19562 starts at the next buffer position. */
19563 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19564 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19565 else
19566 {
19567 INC_BOTH (max_pos, max_bpos);
19568 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19569 }
19570 }
19571 else if (row->truncated_on_right_p)
19572 /* display_line already called reseat_at_next_visible_line_start,
19573 which puts the iterator at the beginning of the next line, in
19574 the logical order. */
19575 row->maxpos = it->current.pos;
19576 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19577 /* A line that is entirely from a string/image/stretch... */
19578 row->maxpos = row->minpos;
19579 else
19580 emacs_abort ();
19581 }
19582 else
19583 row->maxpos = it->current.pos;
19584 }
19585
19586 /* Construct the glyph row IT->glyph_row in the desired matrix of
19587 IT->w from text at the current position of IT. See dispextern.h
19588 for an overview of struct it. Value is non-zero if
19589 IT->glyph_row displays text, as opposed to a line displaying ZV
19590 only. */
19591
19592 static int
19593 display_line (struct it *it)
19594 {
19595 struct glyph_row *row = it->glyph_row;
19596 Lisp_Object overlay_arrow_string;
19597 struct it wrap_it;
19598 void *wrap_data = NULL;
19599 int may_wrap = 0, wrap_x IF_LINT (= 0);
19600 int wrap_row_used = -1;
19601 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19602 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19603 int wrap_row_extra_line_spacing IF_LINT (= 0);
19604 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19605 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19606 int cvpos;
19607 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19608 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19609
19610 /* We always start displaying at hpos zero even if hscrolled. */
19611 eassert (it->hpos == 0 && it->current_x == 0);
19612
19613 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19614 >= it->w->desired_matrix->nrows)
19615 {
19616 it->w->nrows_scale_factor++;
19617 it->f->fonts_changed = 1;
19618 return 0;
19619 }
19620
19621 /* Clear the result glyph row and enable it. */
19622 prepare_desired_row (row);
19623
19624 row->y = it->current_y;
19625 row->start = it->start;
19626 row->continuation_lines_width = it->continuation_lines_width;
19627 row->displays_text_p = 1;
19628 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19629 it->starts_in_middle_of_char_p = 0;
19630
19631 /* Arrange the overlays nicely for our purposes. Usually, we call
19632 display_line on only one line at a time, in which case this
19633 can't really hurt too much, or we call it on lines which appear
19634 one after another in the buffer, in which case all calls to
19635 recenter_overlay_lists but the first will be pretty cheap. */
19636 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19637
19638 /* Move over display elements that are not visible because we are
19639 hscrolled. This may stop at an x-position < IT->first_visible_x
19640 if the first glyph is partially visible or if we hit a line end. */
19641 if (it->current_x < it->first_visible_x)
19642 {
19643 enum move_it_result move_result;
19644
19645 this_line_min_pos = row->start.pos;
19646 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19647 MOVE_TO_POS | MOVE_TO_X);
19648 /* If we are under a large hscroll, move_it_in_display_line_to
19649 could hit the end of the line without reaching
19650 it->first_visible_x. Pretend that we did reach it. This is
19651 especially important on a TTY, where we will call
19652 extend_face_to_end_of_line, which needs to know how many
19653 blank glyphs to produce. */
19654 if (it->current_x < it->first_visible_x
19655 && (move_result == MOVE_NEWLINE_OR_CR
19656 || move_result == MOVE_POS_MATCH_OR_ZV))
19657 it->current_x = it->first_visible_x;
19658
19659 /* Record the smallest positions seen while we moved over
19660 display elements that are not visible. This is needed by
19661 redisplay_internal for optimizing the case where the cursor
19662 stays inside the same line. The rest of this function only
19663 considers positions that are actually displayed, so
19664 RECORD_MAX_MIN_POS will not otherwise record positions that
19665 are hscrolled to the left of the left edge of the window. */
19666 min_pos = CHARPOS (this_line_min_pos);
19667 min_bpos = BYTEPOS (this_line_min_pos);
19668 }
19669 else
19670 {
19671 /* We only do this when not calling `move_it_in_display_line_to'
19672 above, because move_it_in_display_line_to calls
19673 handle_line_prefix itself. */
19674 handle_line_prefix (it);
19675 }
19676
19677 /* Get the initial row height. This is either the height of the
19678 text hscrolled, if there is any, or zero. */
19679 row->ascent = it->max_ascent;
19680 row->height = it->max_ascent + it->max_descent;
19681 row->phys_ascent = it->max_phys_ascent;
19682 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19683 row->extra_line_spacing = it->max_extra_line_spacing;
19684
19685 /* Utility macro to record max and min buffer positions seen until now. */
19686 #define RECORD_MAX_MIN_POS(IT) \
19687 do \
19688 { \
19689 int composition_p = !STRINGP ((IT)->string) \
19690 && ((IT)->what == IT_COMPOSITION); \
19691 ptrdiff_t current_pos = \
19692 composition_p ? (IT)->cmp_it.charpos \
19693 : IT_CHARPOS (*(IT)); \
19694 ptrdiff_t current_bpos = \
19695 composition_p ? CHAR_TO_BYTE (current_pos) \
19696 : IT_BYTEPOS (*(IT)); \
19697 if (current_pos < min_pos) \
19698 { \
19699 min_pos = current_pos; \
19700 min_bpos = current_bpos; \
19701 } \
19702 if (IT_CHARPOS (*it) > max_pos) \
19703 { \
19704 max_pos = IT_CHARPOS (*it); \
19705 max_bpos = IT_BYTEPOS (*it); \
19706 } \
19707 } \
19708 while (0)
19709
19710 /* Loop generating characters. The loop is left with IT on the next
19711 character to display. */
19712 while (1)
19713 {
19714 int n_glyphs_before, hpos_before, x_before;
19715 int x, nglyphs;
19716 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19717
19718 /* Retrieve the next thing to display. Value is zero if end of
19719 buffer reached. */
19720 if (!get_next_display_element (it))
19721 {
19722 /* Maybe add a space at the end of this line that is used to
19723 display the cursor there under X. Set the charpos of the
19724 first glyph of blank lines not corresponding to any text
19725 to -1. */
19726 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19727 row->exact_window_width_line_p = 1;
19728 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19729 || row->used[TEXT_AREA] == 0)
19730 {
19731 row->glyphs[TEXT_AREA]->charpos = -1;
19732 row->displays_text_p = 0;
19733
19734 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19735 && (!MINI_WINDOW_P (it->w)
19736 || (minibuf_level && EQ (it->window, minibuf_window))))
19737 row->indicate_empty_line_p = 1;
19738 }
19739
19740 it->continuation_lines_width = 0;
19741 row->ends_at_zv_p = 1;
19742 /* A row that displays right-to-left text must always have
19743 its last face extended all the way to the end of line,
19744 even if this row ends in ZV, because we still write to
19745 the screen left to right. We also need to extend the
19746 last face if the default face is remapped to some
19747 different face, otherwise the functions that clear
19748 portions of the screen will clear with the default face's
19749 background color. */
19750 if (row->reversed_p
19751 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19752 extend_face_to_end_of_line (it);
19753 break;
19754 }
19755
19756 /* Now, get the metrics of what we want to display. This also
19757 generates glyphs in `row' (which is IT->glyph_row). */
19758 n_glyphs_before = row->used[TEXT_AREA];
19759 x = it->current_x;
19760
19761 /* Remember the line height so far in case the next element doesn't
19762 fit on the line. */
19763 if (it->line_wrap != TRUNCATE)
19764 {
19765 ascent = it->max_ascent;
19766 descent = it->max_descent;
19767 phys_ascent = it->max_phys_ascent;
19768 phys_descent = it->max_phys_descent;
19769
19770 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19771 {
19772 if (IT_DISPLAYING_WHITESPACE (it))
19773 may_wrap = 1;
19774 else if (may_wrap)
19775 {
19776 SAVE_IT (wrap_it, *it, wrap_data);
19777 wrap_x = x;
19778 wrap_row_used = row->used[TEXT_AREA];
19779 wrap_row_ascent = row->ascent;
19780 wrap_row_height = row->height;
19781 wrap_row_phys_ascent = row->phys_ascent;
19782 wrap_row_phys_height = row->phys_height;
19783 wrap_row_extra_line_spacing = row->extra_line_spacing;
19784 wrap_row_min_pos = min_pos;
19785 wrap_row_min_bpos = min_bpos;
19786 wrap_row_max_pos = max_pos;
19787 wrap_row_max_bpos = max_bpos;
19788 may_wrap = 0;
19789 }
19790 }
19791 }
19792
19793 PRODUCE_GLYPHS (it);
19794
19795 /* If this display element was in marginal areas, continue with
19796 the next one. */
19797 if (it->area != TEXT_AREA)
19798 {
19799 row->ascent = max (row->ascent, it->max_ascent);
19800 row->height = max (row->height, it->max_ascent + it->max_descent);
19801 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19802 row->phys_height = max (row->phys_height,
19803 it->max_phys_ascent + it->max_phys_descent);
19804 row->extra_line_spacing = max (row->extra_line_spacing,
19805 it->max_extra_line_spacing);
19806 set_iterator_to_next (it, 1);
19807 continue;
19808 }
19809
19810 /* Does the display element fit on the line? If we truncate
19811 lines, we should draw past the right edge of the window. If
19812 we don't truncate, we want to stop so that we can display the
19813 continuation glyph before the right margin. If lines are
19814 continued, there are two possible strategies for characters
19815 resulting in more than 1 glyph (e.g. tabs): Display as many
19816 glyphs as possible in this line and leave the rest for the
19817 continuation line, or display the whole element in the next
19818 line. Original redisplay did the former, so we do it also. */
19819 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19820 hpos_before = it->hpos;
19821 x_before = x;
19822
19823 if (/* Not a newline. */
19824 nglyphs > 0
19825 /* Glyphs produced fit entirely in the line. */
19826 && it->current_x < it->last_visible_x)
19827 {
19828 it->hpos += nglyphs;
19829 row->ascent = max (row->ascent, it->max_ascent);
19830 row->height = max (row->height, it->max_ascent + it->max_descent);
19831 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19832 row->phys_height = max (row->phys_height,
19833 it->max_phys_ascent + it->max_phys_descent);
19834 row->extra_line_spacing = max (row->extra_line_spacing,
19835 it->max_extra_line_spacing);
19836 if (it->current_x - it->pixel_width < it->first_visible_x)
19837 row->x = x - it->first_visible_x;
19838 /* Record the maximum and minimum buffer positions seen so
19839 far in glyphs that will be displayed by this row. */
19840 if (it->bidi_p)
19841 RECORD_MAX_MIN_POS (it);
19842 }
19843 else
19844 {
19845 int i, new_x;
19846 struct glyph *glyph;
19847
19848 for (i = 0; i < nglyphs; ++i, x = new_x)
19849 {
19850 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19851 new_x = x + glyph->pixel_width;
19852
19853 if (/* Lines are continued. */
19854 it->line_wrap != TRUNCATE
19855 && (/* Glyph doesn't fit on the line. */
19856 new_x > it->last_visible_x
19857 /* Or it fits exactly on a window system frame. */
19858 || (new_x == it->last_visible_x
19859 && FRAME_WINDOW_P (it->f)
19860 && (row->reversed_p
19861 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19862 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19863 {
19864 /* End of a continued line. */
19865
19866 if (it->hpos == 0
19867 || (new_x == it->last_visible_x
19868 && FRAME_WINDOW_P (it->f)
19869 && (row->reversed_p
19870 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19871 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19872 {
19873 /* Current glyph is the only one on the line or
19874 fits exactly on the line. We must continue
19875 the line because we can't draw the cursor
19876 after the glyph. */
19877 row->continued_p = 1;
19878 it->current_x = new_x;
19879 it->continuation_lines_width += new_x;
19880 ++it->hpos;
19881 if (i == nglyphs - 1)
19882 {
19883 /* If line-wrap is on, check if a previous
19884 wrap point was found. */
19885 if (wrap_row_used > 0
19886 /* Even if there is a previous wrap
19887 point, continue the line here as
19888 usual, if (i) the previous character
19889 was a space or tab AND (ii) the
19890 current character is not. */
19891 && (!may_wrap
19892 || IT_DISPLAYING_WHITESPACE (it)))
19893 goto back_to_wrap;
19894
19895 /* Record the maximum and minimum buffer
19896 positions seen so far in glyphs that will be
19897 displayed by this row. */
19898 if (it->bidi_p)
19899 RECORD_MAX_MIN_POS (it);
19900 set_iterator_to_next (it, 1);
19901 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19902 {
19903 if (!get_next_display_element (it))
19904 {
19905 row->exact_window_width_line_p = 1;
19906 it->continuation_lines_width = 0;
19907 row->continued_p = 0;
19908 row->ends_at_zv_p = 1;
19909 }
19910 else if (ITERATOR_AT_END_OF_LINE_P (it))
19911 {
19912 row->continued_p = 0;
19913 row->exact_window_width_line_p = 1;
19914 }
19915 }
19916 }
19917 else if (it->bidi_p)
19918 RECORD_MAX_MIN_POS (it);
19919 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19920 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19921 extend_face_to_end_of_line (it);
19922 }
19923 else if (CHAR_GLYPH_PADDING_P (*glyph)
19924 && !FRAME_WINDOW_P (it->f))
19925 {
19926 /* A padding glyph that doesn't fit on this line.
19927 This means the whole character doesn't fit
19928 on the line. */
19929 if (row->reversed_p)
19930 unproduce_glyphs (it, row->used[TEXT_AREA]
19931 - n_glyphs_before);
19932 row->used[TEXT_AREA] = n_glyphs_before;
19933
19934 /* Fill the rest of the row with continuation
19935 glyphs like in 20.x. */
19936 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19937 < row->glyphs[1 + TEXT_AREA])
19938 produce_special_glyphs (it, IT_CONTINUATION);
19939
19940 row->continued_p = 1;
19941 it->current_x = x_before;
19942 it->continuation_lines_width += x_before;
19943
19944 /* Restore the height to what it was before the
19945 element not fitting on the line. */
19946 it->max_ascent = ascent;
19947 it->max_descent = descent;
19948 it->max_phys_ascent = phys_ascent;
19949 it->max_phys_descent = phys_descent;
19950 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19951 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19952 extend_face_to_end_of_line (it);
19953 }
19954 else if (wrap_row_used > 0)
19955 {
19956 back_to_wrap:
19957 if (row->reversed_p)
19958 unproduce_glyphs (it,
19959 row->used[TEXT_AREA] - wrap_row_used);
19960 RESTORE_IT (it, &wrap_it, wrap_data);
19961 it->continuation_lines_width += wrap_x;
19962 row->used[TEXT_AREA] = wrap_row_used;
19963 row->ascent = wrap_row_ascent;
19964 row->height = wrap_row_height;
19965 row->phys_ascent = wrap_row_phys_ascent;
19966 row->phys_height = wrap_row_phys_height;
19967 row->extra_line_spacing = wrap_row_extra_line_spacing;
19968 min_pos = wrap_row_min_pos;
19969 min_bpos = wrap_row_min_bpos;
19970 max_pos = wrap_row_max_pos;
19971 max_bpos = wrap_row_max_bpos;
19972 row->continued_p = 1;
19973 row->ends_at_zv_p = 0;
19974 row->exact_window_width_line_p = 0;
19975 it->continuation_lines_width += x;
19976
19977 /* Make sure that a non-default face is extended
19978 up to the right margin of the window. */
19979 extend_face_to_end_of_line (it);
19980 }
19981 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19982 {
19983 /* A TAB that extends past the right edge of the
19984 window. This produces a single glyph on
19985 window system frames. We leave the glyph in
19986 this row and let it fill the row, but don't
19987 consume the TAB. */
19988 if ((row->reversed_p
19989 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19990 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19991 produce_special_glyphs (it, IT_CONTINUATION);
19992 it->continuation_lines_width += it->last_visible_x;
19993 row->ends_in_middle_of_char_p = 1;
19994 row->continued_p = 1;
19995 glyph->pixel_width = it->last_visible_x - x;
19996 it->starts_in_middle_of_char_p = 1;
19997 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19998 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19999 extend_face_to_end_of_line (it);
20000 }
20001 else
20002 {
20003 /* Something other than a TAB that draws past
20004 the right edge of the window. Restore
20005 positions to values before the element. */
20006 if (row->reversed_p)
20007 unproduce_glyphs (it, row->used[TEXT_AREA]
20008 - (n_glyphs_before + i));
20009 row->used[TEXT_AREA] = n_glyphs_before + i;
20010
20011 /* Display continuation glyphs. */
20012 it->current_x = x_before;
20013 it->continuation_lines_width += x;
20014 if (!FRAME_WINDOW_P (it->f)
20015 || (row->reversed_p
20016 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20017 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20018 produce_special_glyphs (it, IT_CONTINUATION);
20019 row->continued_p = 1;
20020
20021 extend_face_to_end_of_line (it);
20022
20023 if (nglyphs > 1 && i > 0)
20024 {
20025 row->ends_in_middle_of_char_p = 1;
20026 it->starts_in_middle_of_char_p = 1;
20027 }
20028
20029 /* Restore the height to what it was before the
20030 element not fitting on the line. */
20031 it->max_ascent = ascent;
20032 it->max_descent = descent;
20033 it->max_phys_ascent = phys_ascent;
20034 it->max_phys_descent = phys_descent;
20035 }
20036
20037 break;
20038 }
20039 else if (new_x > it->first_visible_x)
20040 {
20041 /* Increment number of glyphs actually displayed. */
20042 ++it->hpos;
20043
20044 /* Record the maximum and minimum buffer positions
20045 seen so far in glyphs that will be displayed by
20046 this row. */
20047 if (it->bidi_p)
20048 RECORD_MAX_MIN_POS (it);
20049
20050 if (x < it->first_visible_x)
20051 /* Glyph is partially visible, i.e. row starts at
20052 negative X position. */
20053 row->x = x - it->first_visible_x;
20054 }
20055 else
20056 {
20057 /* Glyph is completely off the left margin of the
20058 window. This should not happen because of the
20059 move_it_in_display_line at the start of this
20060 function, unless the text display area of the
20061 window is empty. */
20062 eassert (it->first_visible_x <= it->last_visible_x);
20063 }
20064 }
20065 /* Even if this display element produced no glyphs at all,
20066 we want to record its position. */
20067 if (it->bidi_p && nglyphs == 0)
20068 RECORD_MAX_MIN_POS (it);
20069
20070 row->ascent = max (row->ascent, it->max_ascent);
20071 row->height = max (row->height, it->max_ascent + it->max_descent);
20072 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20073 row->phys_height = max (row->phys_height,
20074 it->max_phys_ascent + it->max_phys_descent);
20075 row->extra_line_spacing = max (row->extra_line_spacing,
20076 it->max_extra_line_spacing);
20077
20078 /* End of this display line if row is continued. */
20079 if (row->continued_p || row->ends_at_zv_p)
20080 break;
20081 }
20082
20083 at_end_of_line:
20084 /* Is this a line end? If yes, we're also done, after making
20085 sure that a non-default face is extended up to the right
20086 margin of the window. */
20087 if (ITERATOR_AT_END_OF_LINE_P (it))
20088 {
20089 int used_before = row->used[TEXT_AREA];
20090
20091 row->ends_in_newline_from_string_p = STRINGP (it->object);
20092
20093 /* Add a space at the end of the line that is used to
20094 display the cursor there. */
20095 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20096 append_space_for_newline (it, 0);
20097
20098 /* Extend the face to the end of the line. */
20099 extend_face_to_end_of_line (it);
20100
20101 /* Make sure we have the position. */
20102 if (used_before == 0)
20103 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20104
20105 /* Record the position of the newline, for use in
20106 find_row_edges. */
20107 it->eol_pos = it->current.pos;
20108
20109 /* Consume the line end. This skips over invisible lines. */
20110 set_iterator_to_next (it, 1);
20111 it->continuation_lines_width = 0;
20112 break;
20113 }
20114
20115 /* Proceed with next display element. Note that this skips
20116 over lines invisible because of selective display. */
20117 set_iterator_to_next (it, 1);
20118
20119 /* If we truncate lines, we are done when the last displayed
20120 glyphs reach past the right margin of the window. */
20121 if (it->line_wrap == TRUNCATE
20122 && ((FRAME_WINDOW_P (it->f)
20123 /* Images are preprocessed in produce_image_glyph such
20124 that they are cropped at the right edge of the
20125 window, so an image glyph will always end exactly at
20126 last_visible_x, even if there's no right fringe. */
20127 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20128 ? (it->current_x >= it->last_visible_x)
20129 : (it->current_x > it->last_visible_x)))
20130 {
20131 /* Maybe add truncation glyphs. */
20132 if (!FRAME_WINDOW_P (it->f)
20133 || (row->reversed_p
20134 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20135 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20136 {
20137 int i, n;
20138
20139 if (!row->reversed_p)
20140 {
20141 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20142 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20143 break;
20144 }
20145 else
20146 {
20147 for (i = 0; i < row->used[TEXT_AREA]; i++)
20148 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20149 break;
20150 /* Remove any padding glyphs at the front of ROW, to
20151 make room for the truncation glyphs we will be
20152 adding below. The loop below always inserts at
20153 least one truncation glyph, so also remove the
20154 last glyph added to ROW. */
20155 unproduce_glyphs (it, i + 1);
20156 /* Adjust i for the loop below. */
20157 i = row->used[TEXT_AREA] - (i + 1);
20158 }
20159
20160 /* produce_special_glyphs overwrites the last glyph, so
20161 we don't want that if we want to keep that last
20162 glyph, which means it's an image. */
20163 if (it->current_x > it->last_visible_x)
20164 {
20165 it->current_x = x_before;
20166 if (!FRAME_WINDOW_P (it->f))
20167 {
20168 for (n = row->used[TEXT_AREA]; i < n; ++i)
20169 {
20170 row->used[TEXT_AREA] = i;
20171 produce_special_glyphs (it, IT_TRUNCATION);
20172 }
20173 }
20174 else
20175 {
20176 row->used[TEXT_AREA] = i;
20177 produce_special_glyphs (it, IT_TRUNCATION);
20178 }
20179 it->hpos = hpos_before;
20180 }
20181 }
20182 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20183 {
20184 /* Don't truncate if we can overflow newline into fringe. */
20185 if (!get_next_display_element (it))
20186 {
20187 it->continuation_lines_width = 0;
20188 row->ends_at_zv_p = 1;
20189 row->exact_window_width_line_p = 1;
20190 break;
20191 }
20192 if (ITERATOR_AT_END_OF_LINE_P (it))
20193 {
20194 row->exact_window_width_line_p = 1;
20195 goto at_end_of_line;
20196 }
20197 it->current_x = x_before;
20198 it->hpos = hpos_before;
20199 }
20200
20201 row->truncated_on_right_p = 1;
20202 it->continuation_lines_width = 0;
20203 reseat_at_next_visible_line_start (it, 0);
20204 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20205 break;
20206 }
20207 }
20208
20209 if (wrap_data)
20210 bidi_unshelve_cache (wrap_data, 1);
20211
20212 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20213 at the left window margin. */
20214 if (it->first_visible_x
20215 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20216 {
20217 if (!FRAME_WINDOW_P (it->f)
20218 || (((row->reversed_p
20219 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20220 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20221 /* Don't let insert_left_trunc_glyphs overwrite the
20222 first glyph of the row if it is an image. */
20223 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20224 insert_left_trunc_glyphs (it);
20225 row->truncated_on_left_p = 1;
20226 }
20227
20228 /* Remember the position at which this line ends.
20229
20230 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20231 cannot be before the call to find_row_edges below, since that is
20232 where these positions are determined. */
20233 row->end = it->current;
20234 if (!it->bidi_p)
20235 {
20236 row->minpos = row->start.pos;
20237 row->maxpos = row->end.pos;
20238 }
20239 else
20240 {
20241 /* ROW->minpos and ROW->maxpos must be the smallest and
20242 `1 + the largest' buffer positions in ROW. But if ROW was
20243 bidi-reordered, these two positions can be anywhere in the
20244 row, so we must determine them now. */
20245 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20246 }
20247
20248 /* If the start of this line is the overlay arrow-position, then
20249 mark this glyph row as the one containing the overlay arrow.
20250 This is clearly a mess with variable size fonts. It would be
20251 better to let it be displayed like cursors under X. */
20252 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20253 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20254 !NILP (overlay_arrow_string)))
20255 {
20256 /* Overlay arrow in window redisplay is a fringe bitmap. */
20257 if (STRINGP (overlay_arrow_string))
20258 {
20259 struct glyph_row *arrow_row
20260 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20261 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20262 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20263 struct glyph *p = row->glyphs[TEXT_AREA];
20264 struct glyph *p2, *end;
20265
20266 /* Copy the arrow glyphs. */
20267 while (glyph < arrow_end)
20268 *p++ = *glyph++;
20269
20270 /* Throw away padding glyphs. */
20271 p2 = p;
20272 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20273 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20274 ++p2;
20275 if (p2 > p)
20276 {
20277 while (p2 < end)
20278 *p++ = *p2++;
20279 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20280 }
20281 }
20282 else
20283 {
20284 eassert (INTEGERP (overlay_arrow_string));
20285 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20286 }
20287 overlay_arrow_seen = 1;
20288 }
20289
20290 /* Highlight trailing whitespace. */
20291 if (!NILP (Vshow_trailing_whitespace))
20292 highlight_trailing_whitespace (it->f, it->glyph_row);
20293
20294 /* Compute pixel dimensions of this line. */
20295 compute_line_metrics (it);
20296
20297 /* Implementation note: No changes in the glyphs of ROW or in their
20298 faces can be done past this point, because compute_line_metrics
20299 computes ROW's hash value and stores it within the glyph_row
20300 structure. */
20301
20302 /* Record whether this row ends inside an ellipsis. */
20303 row->ends_in_ellipsis_p
20304 = (it->method == GET_FROM_DISPLAY_VECTOR
20305 && it->ellipsis_p);
20306
20307 /* Save fringe bitmaps in this row. */
20308 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20309 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20310 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20311 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20312
20313 it->left_user_fringe_bitmap = 0;
20314 it->left_user_fringe_face_id = 0;
20315 it->right_user_fringe_bitmap = 0;
20316 it->right_user_fringe_face_id = 0;
20317
20318 /* Maybe set the cursor. */
20319 cvpos = it->w->cursor.vpos;
20320 if ((cvpos < 0
20321 /* In bidi-reordered rows, keep checking for proper cursor
20322 position even if one has been found already, because buffer
20323 positions in such rows change non-linearly with ROW->VPOS,
20324 when a line is continued. One exception: when we are at ZV,
20325 display cursor on the first suitable glyph row, since all
20326 the empty rows after that also have their position set to ZV. */
20327 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20328 lines' rows is implemented for bidi-reordered rows. */
20329 || (it->bidi_p
20330 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20331 && PT >= MATRIX_ROW_START_CHARPOS (row)
20332 && PT <= MATRIX_ROW_END_CHARPOS (row)
20333 && cursor_row_p (row))
20334 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20335
20336 /* Prepare for the next line. This line starts horizontally at (X
20337 HPOS) = (0 0). Vertical positions are incremented. As a
20338 convenience for the caller, IT->glyph_row is set to the next
20339 row to be used. */
20340 it->current_x = it->hpos = 0;
20341 it->current_y += row->height;
20342 SET_TEXT_POS (it->eol_pos, 0, 0);
20343 ++it->vpos;
20344 ++it->glyph_row;
20345 /* The next row should by default use the same value of the
20346 reversed_p flag as this one. set_iterator_to_next decides when
20347 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20348 the flag accordingly. */
20349 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20350 it->glyph_row->reversed_p = row->reversed_p;
20351 it->start = row->end;
20352 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20353
20354 #undef RECORD_MAX_MIN_POS
20355 }
20356
20357 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20358 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20359 doc: /* Return paragraph direction at point in BUFFER.
20360 Value is either `left-to-right' or `right-to-left'.
20361 If BUFFER is omitted or nil, it defaults to the current buffer.
20362
20363 Paragraph direction determines how the text in the paragraph is displayed.
20364 In left-to-right paragraphs, text begins at the left margin of the window
20365 and the reading direction is generally left to right. In right-to-left
20366 paragraphs, text begins at the right margin and is read from right to left.
20367
20368 See also `bidi-paragraph-direction'. */)
20369 (Lisp_Object buffer)
20370 {
20371 struct buffer *buf = current_buffer;
20372 struct buffer *old = buf;
20373
20374 if (! NILP (buffer))
20375 {
20376 CHECK_BUFFER (buffer);
20377 buf = XBUFFER (buffer);
20378 }
20379
20380 if (NILP (BVAR (buf, bidi_display_reordering))
20381 || NILP (BVAR (buf, enable_multibyte_characters))
20382 /* When we are loading loadup.el, the character property tables
20383 needed for bidi iteration are not yet available. */
20384 || !NILP (Vpurify_flag))
20385 return Qleft_to_right;
20386 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20387 return BVAR (buf, bidi_paragraph_direction);
20388 else
20389 {
20390 /* Determine the direction from buffer text. We could try to
20391 use current_matrix if it is up to date, but this seems fast
20392 enough as it is. */
20393 struct bidi_it itb;
20394 ptrdiff_t pos = BUF_PT (buf);
20395 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20396 int c;
20397 void *itb_data = bidi_shelve_cache ();
20398
20399 set_buffer_temp (buf);
20400 /* bidi_paragraph_init finds the base direction of the paragraph
20401 by searching forward from paragraph start. We need the base
20402 direction of the current or _previous_ paragraph, so we need
20403 to make sure we are within that paragraph. To that end, find
20404 the previous non-empty line. */
20405 if (pos >= ZV && pos > BEGV)
20406 DEC_BOTH (pos, bytepos);
20407 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20408 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20409 {
20410 while ((c = FETCH_BYTE (bytepos)) == '\n'
20411 || c == ' ' || c == '\t' || c == '\f')
20412 {
20413 if (bytepos <= BEGV_BYTE)
20414 break;
20415 bytepos--;
20416 pos--;
20417 }
20418 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20419 bytepos--;
20420 }
20421 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20422 itb.paragraph_dir = NEUTRAL_DIR;
20423 itb.string.s = NULL;
20424 itb.string.lstring = Qnil;
20425 itb.string.bufpos = 0;
20426 itb.string.from_disp_str = 0;
20427 itb.string.unibyte = 0;
20428 /* We have no window to use here for ignoring window-specific
20429 overlays. Using NULL for window pointer will cause
20430 compute_display_string_pos to use the current buffer. */
20431 itb.w = NULL;
20432 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20433 bidi_unshelve_cache (itb_data, 0);
20434 set_buffer_temp (old);
20435 switch (itb.paragraph_dir)
20436 {
20437 case L2R:
20438 return Qleft_to_right;
20439 break;
20440 case R2L:
20441 return Qright_to_left;
20442 break;
20443 default:
20444 emacs_abort ();
20445 }
20446 }
20447 }
20448
20449 DEFUN ("move-point-visually", Fmove_point_visually,
20450 Smove_point_visually, 1, 1, 0,
20451 doc: /* Move point in the visual order in the specified DIRECTION.
20452 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20453 left.
20454
20455 Value is the new character position of point. */)
20456 (Lisp_Object direction)
20457 {
20458 struct window *w = XWINDOW (selected_window);
20459 struct buffer *b = XBUFFER (w->contents);
20460 struct glyph_row *row;
20461 int dir;
20462 Lisp_Object paragraph_dir;
20463
20464 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20465 (!(ROW)->continued_p \
20466 && INTEGERP ((GLYPH)->object) \
20467 && (GLYPH)->type == CHAR_GLYPH \
20468 && (GLYPH)->u.ch == ' ' \
20469 && (GLYPH)->charpos >= 0 \
20470 && !(GLYPH)->avoid_cursor_p)
20471
20472 CHECK_NUMBER (direction);
20473 dir = XINT (direction);
20474 if (dir > 0)
20475 dir = 1;
20476 else
20477 dir = -1;
20478
20479 /* If current matrix is up-to-date, we can use the information
20480 recorded in the glyphs, at least as long as the goal is on the
20481 screen. */
20482 if (w->window_end_valid
20483 && !windows_or_buffers_changed
20484 && b
20485 && !b->clip_changed
20486 && !b->prevent_redisplay_optimizations_p
20487 && !window_outdated (w)
20488 && w->cursor.vpos >= 0
20489 && w->cursor.vpos < w->current_matrix->nrows
20490 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20491 {
20492 struct glyph *g = row->glyphs[TEXT_AREA];
20493 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20494 struct glyph *gpt = g + w->cursor.hpos;
20495
20496 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20497 {
20498 if (BUFFERP (g->object) && g->charpos != PT)
20499 {
20500 SET_PT (g->charpos);
20501 w->cursor.vpos = -1;
20502 return make_number (PT);
20503 }
20504 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20505 {
20506 ptrdiff_t new_pos;
20507
20508 if (BUFFERP (gpt->object))
20509 {
20510 new_pos = PT;
20511 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20512 new_pos += (row->reversed_p ? -dir : dir);
20513 else
20514 new_pos -= (row->reversed_p ? -dir : dir);;
20515 }
20516 else if (BUFFERP (g->object))
20517 new_pos = g->charpos;
20518 else
20519 break;
20520 SET_PT (new_pos);
20521 w->cursor.vpos = -1;
20522 return make_number (PT);
20523 }
20524 else if (ROW_GLYPH_NEWLINE_P (row, g))
20525 {
20526 /* Glyphs inserted at the end of a non-empty line for
20527 positioning the cursor have zero charpos, so we must
20528 deduce the value of point by other means. */
20529 if (g->charpos > 0)
20530 SET_PT (g->charpos);
20531 else if (row->ends_at_zv_p && PT != ZV)
20532 SET_PT (ZV);
20533 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20534 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20535 else
20536 break;
20537 w->cursor.vpos = -1;
20538 return make_number (PT);
20539 }
20540 }
20541 if (g == e || INTEGERP (g->object))
20542 {
20543 if (row->truncated_on_left_p || row->truncated_on_right_p)
20544 goto simulate_display;
20545 if (!row->reversed_p)
20546 row += dir;
20547 else
20548 row -= dir;
20549 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20550 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20551 goto simulate_display;
20552
20553 if (dir > 0)
20554 {
20555 if (row->reversed_p && !row->continued_p)
20556 {
20557 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20558 w->cursor.vpos = -1;
20559 return make_number (PT);
20560 }
20561 g = row->glyphs[TEXT_AREA];
20562 e = g + row->used[TEXT_AREA];
20563 for ( ; g < e; g++)
20564 {
20565 if (BUFFERP (g->object)
20566 /* Empty lines have only one glyph, which stands
20567 for the newline, and whose charpos is the
20568 buffer position of the newline. */
20569 || ROW_GLYPH_NEWLINE_P (row, g)
20570 /* When the buffer ends in a newline, the line at
20571 EOB also has one glyph, but its charpos is -1. */
20572 || (row->ends_at_zv_p
20573 && !row->reversed_p
20574 && INTEGERP (g->object)
20575 && g->type == CHAR_GLYPH
20576 && g->u.ch == ' '))
20577 {
20578 if (g->charpos > 0)
20579 SET_PT (g->charpos);
20580 else if (!row->reversed_p
20581 && row->ends_at_zv_p
20582 && PT != ZV)
20583 SET_PT (ZV);
20584 else
20585 continue;
20586 w->cursor.vpos = -1;
20587 return make_number (PT);
20588 }
20589 }
20590 }
20591 else
20592 {
20593 if (!row->reversed_p && !row->continued_p)
20594 {
20595 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20596 w->cursor.vpos = -1;
20597 return make_number (PT);
20598 }
20599 e = row->glyphs[TEXT_AREA];
20600 g = e + row->used[TEXT_AREA] - 1;
20601 for ( ; g >= e; g--)
20602 {
20603 if (BUFFERP (g->object)
20604 || (ROW_GLYPH_NEWLINE_P (row, g)
20605 && g->charpos > 0)
20606 /* Empty R2L lines on GUI frames have the buffer
20607 position of the newline stored in the stretch
20608 glyph. */
20609 || g->type == STRETCH_GLYPH
20610 || (row->ends_at_zv_p
20611 && row->reversed_p
20612 && INTEGERP (g->object)
20613 && g->type == CHAR_GLYPH
20614 && g->u.ch == ' '))
20615 {
20616 if (g->charpos > 0)
20617 SET_PT (g->charpos);
20618 else if (row->reversed_p
20619 && row->ends_at_zv_p
20620 && PT != ZV)
20621 SET_PT (ZV);
20622 else
20623 continue;
20624 w->cursor.vpos = -1;
20625 return make_number (PT);
20626 }
20627 }
20628 }
20629 }
20630 }
20631
20632 simulate_display:
20633
20634 /* If we wind up here, we failed to move by using the glyphs, so we
20635 need to simulate display instead. */
20636
20637 if (b)
20638 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20639 else
20640 paragraph_dir = Qleft_to_right;
20641 if (EQ (paragraph_dir, Qright_to_left))
20642 dir = -dir;
20643 if (PT <= BEGV && dir < 0)
20644 xsignal0 (Qbeginning_of_buffer);
20645 else if (PT >= ZV && dir > 0)
20646 xsignal0 (Qend_of_buffer);
20647 else
20648 {
20649 struct text_pos pt;
20650 struct it it;
20651 int pt_x, target_x, pixel_width, pt_vpos;
20652 bool at_eol_p;
20653 bool overshoot_expected = false;
20654 bool target_is_eol_p = false;
20655
20656 /* Setup the arena. */
20657 SET_TEXT_POS (pt, PT, PT_BYTE);
20658 start_display (&it, w, pt);
20659
20660 if (it.cmp_it.id < 0
20661 && it.method == GET_FROM_STRING
20662 && it.area == TEXT_AREA
20663 && it.string_from_display_prop_p
20664 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20665 overshoot_expected = true;
20666
20667 /* Find the X coordinate of point. We start from the beginning
20668 of this or previous line to make sure we are before point in
20669 the logical order (since the move_it_* functions can only
20670 move forward). */
20671 reseat:
20672 reseat_at_previous_visible_line_start (&it);
20673 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20674 if (IT_CHARPOS (it) != PT)
20675 {
20676 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20677 -1, -1, -1, MOVE_TO_POS);
20678 /* If we missed point because the character there is
20679 displayed out of a display vector that has more than one
20680 glyph, retry expecting overshoot. */
20681 if (it.method == GET_FROM_DISPLAY_VECTOR
20682 && it.current.dpvec_index > 0
20683 && !overshoot_expected)
20684 {
20685 overshoot_expected = true;
20686 goto reseat;
20687 }
20688 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20689 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20690 }
20691 pt_x = it.current_x;
20692 pt_vpos = it.vpos;
20693 if (dir > 0 || overshoot_expected)
20694 {
20695 struct glyph_row *row = it.glyph_row;
20696
20697 /* When point is at beginning of line, we don't have
20698 information about the glyph there loaded into struct
20699 it. Calling get_next_display_element fixes that. */
20700 if (pt_x == 0)
20701 get_next_display_element (&it);
20702 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20703 it.glyph_row = NULL;
20704 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20705 it.glyph_row = row;
20706 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20707 it, lest it will become out of sync with it's buffer
20708 position. */
20709 it.current_x = pt_x;
20710 }
20711 else
20712 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20713 pixel_width = it.pixel_width;
20714 if (overshoot_expected && at_eol_p)
20715 pixel_width = 0;
20716 else if (pixel_width <= 0)
20717 pixel_width = 1;
20718
20719 /* If there's a display string (or something similar) at point,
20720 we are actually at the glyph to the left of point, so we need
20721 to correct the X coordinate. */
20722 if (overshoot_expected)
20723 {
20724 if (it.bidi_p)
20725 pt_x += pixel_width * it.bidi_it.scan_dir;
20726 else
20727 pt_x += pixel_width;
20728 }
20729
20730 /* Compute target X coordinate, either to the left or to the
20731 right of point. On TTY frames, all characters have the same
20732 pixel width of 1, so we can use that. On GUI frames we don't
20733 have an easy way of getting at the pixel width of the
20734 character to the left of point, so we use a different method
20735 of getting to that place. */
20736 if (dir > 0)
20737 target_x = pt_x + pixel_width;
20738 else
20739 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20740
20741 /* Target X coordinate could be one line above or below the line
20742 of point, in which case we need to adjust the target X
20743 coordinate. Also, if moving to the left, we need to begin at
20744 the left edge of the point's screen line. */
20745 if (dir < 0)
20746 {
20747 if (pt_x > 0)
20748 {
20749 start_display (&it, w, pt);
20750 reseat_at_previous_visible_line_start (&it);
20751 it.current_x = it.current_y = it.hpos = 0;
20752 if (pt_vpos != 0)
20753 move_it_by_lines (&it, pt_vpos);
20754 }
20755 else
20756 {
20757 move_it_by_lines (&it, -1);
20758 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20759 target_is_eol_p = true;
20760 }
20761 }
20762 else
20763 {
20764 if (at_eol_p
20765 || (target_x >= it.last_visible_x
20766 && it.line_wrap != TRUNCATE))
20767 {
20768 if (pt_x > 0)
20769 move_it_by_lines (&it, 0);
20770 move_it_by_lines (&it, 1);
20771 target_x = 0;
20772 }
20773 }
20774
20775 /* Move to the target X coordinate. */
20776 #ifdef HAVE_WINDOW_SYSTEM
20777 /* On GUI frames, as we don't know the X coordinate of the
20778 character to the left of point, moving point to the left
20779 requires walking, one grapheme cluster at a time, until we
20780 find ourself at a place immediately to the left of the
20781 character at point. */
20782 if (FRAME_WINDOW_P (it.f) && dir < 0)
20783 {
20784 struct text_pos new_pos;
20785 enum move_it_result rc = MOVE_X_REACHED;
20786
20787 if (it.current_x == 0)
20788 get_next_display_element (&it);
20789 if (it.what == IT_COMPOSITION)
20790 {
20791 new_pos.charpos = it.cmp_it.charpos;
20792 new_pos.bytepos = -1;
20793 }
20794 else
20795 new_pos = it.current.pos;
20796
20797 while (it.current_x + it.pixel_width <= target_x
20798 && rc == MOVE_X_REACHED)
20799 {
20800 int new_x = it.current_x + it.pixel_width;
20801
20802 /* For composed characters, we want the position of the
20803 first character in the grapheme cluster (usually, the
20804 composition's base character), whereas it.current
20805 might give us the position of the _last_ one, e.g. if
20806 the composition is rendered in reverse due to bidi
20807 reordering. */
20808 if (it.what == IT_COMPOSITION)
20809 {
20810 new_pos.charpos = it.cmp_it.charpos;
20811 new_pos.bytepos = -1;
20812 }
20813 else
20814 new_pos = it.current.pos;
20815 if (new_x == it.current_x)
20816 new_x++;
20817 rc = move_it_in_display_line_to (&it, ZV, new_x,
20818 MOVE_TO_POS | MOVE_TO_X);
20819 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20820 break;
20821 }
20822 /* The previous position we saw in the loop is the one we
20823 want. */
20824 if (new_pos.bytepos == -1)
20825 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20826 it.current.pos = new_pos;
20827 }
20828 else
20829 #endif
20830 if (it.current_x != target_x)
20831 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20832
20833 /* When lines are truncated, the above loop will stop at the
20834 window edge. But we want to get to the end of line, even if
20835 it is beyond the window edge; automatic hscroll will then
20836 scroll the window to show point as appropriate. */
20837 if (target_is_eol_p && it.line_wrap == TRUNCATE
20838 && get_next_display_element (&it))
20839 {
20840 struct text_pos new_pos = it.current.pos;
20841
20842 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20843 {
20844 set_iterator_to_next (&it, 0);
20845 if (it.method == GET_FROM_BUFFER)
20846 new_pos = it.current.pos;
20847 if (!get_next_display_element (&it))
20848 break;
20849 }
20850
20851 it.current.pos = new_pos;
20852 }
20853
20854 /* If we ended up in a display string that covers point, move to
20855 buffer position to the right in the visual order. */
20856 if (dir > 0)
20857 {
20858 while (IT_CHARPOS (it) == PT)
20859 {
20860 set_iterator_to_next (&it, 0);
20861 if (!get_next_display_element (&it))
20862 break;
20863 }
20864 }
20865
20866 /* Move point to that position. */
20867 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20868 }
20869
20870 return make_number (PT);
20871
20872 #undef ROW_GLYPH_NEWLINE_P
20873 }
20874
20875 \f
20876 /***********************************************************************
20877 Menu Bar
20878 ***********************************************************************/
20879
20880 /* Redisplay the menu bar in the frame for window W.
20881
20882 The menu bar of X frames that don't have X toolkit support is
20883 displayed in a special window W->frame->menu_bar_window.
20884
20885 The menu bar of terminal frames is treated specially as far as
20886 glyph matrices are concerned. Menu bar lines are not part of
20887 windows, so the update is done directly on the frame matrix rows
20888 for the menu bar. */
20889
20890 static void
20891 display_menu_bar (struct window *w)
20892 {
20893 struct frame *f = XFRAME (WINDOW_FRAME (w));
20894 struct it it;
20895 Lisp_Object items;
20896 int i;
20897
20898 /* Don't do all this for graphical frames. */
20899 #ifdef HAVE_NTGUI
20900 if (FRAME_W32_P (f))
20901 return;
20902 #endif
20903 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20904 if (FRAME_X_P (f))
20905 return;
20906 #endif
20907
20908 #ifdef HAVE_NS
20909 if (FRAME_NS_P (f))
20910 return;
20911 #endif /* HAVE_NS */
20912
20913 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20914 eassert (!FRAME_WINDOW_P (f));
20915 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20916 it.first_visible_x = 0;
20917 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20918 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20919 if (FRAME_WINDOW_P (f))
20920 {
20921 /* Menu bar lines are displayed in the desired matrix of the
20922 dummy window menu_bar_window. */
20923 struct window *menu_w;
20924 menu_w = XWINDOW (f->menu_bar_window);
20925 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20926 MENU_FACE_ID);
20927 it.first_visible_x = 0;
20928 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20929 }
20930 else
20931 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20932 {
20933 /* This is a TTY frame, i.e. character hpos/vpos are used as
20934 pixel x/y. */
20935 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20936 MENU_FACE_ID);
20937 it.first_visible_x = 0;
20938 it.last_visible_x = FRAME_COLS (f);
20939 }
20940
20941 /* FIXME: This should be controlled by a user option. See the
20942 comments in redisplay_tool_bar and display_mode_line about
20943 this. */
20944 it.paragraph_embedding = L2R;
20945
20946 /* Clear all rows of the menu bar. */
20947 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20948 {
20949 struct glyph_row *row = it.glyph_row + i;
20950 clear_glyph_row (row);
20951 row->enabled_p = true;
20952 row->full_width_p = 1;
20953 }
20954
20955 /* Display all items of the menu bar. */
20956 items = FRAME_MENU_BAR_ITEMS (it.f);
20957 for (i = 0; i < ASIZE (items); i += 4)
20958 {
20959 Lisp_Object string;
20960
20961 /* Stop at nil string. */
20962 string = AREF (items, i + 1);
20963 if (NILP (string))
20964 break;
20965
20966 /* Remember where item was displayed. */
20967 ASET (items, i + 3, make_number (it.hpos));
20968
20969 /* Display the item, pad with one space. */
20970 if (it.current_x < it.last_visible_x)
20971 display_string (NULL, string, Qnil, 0, 0, &it,
20972 SCHARS (string) + 1, 0, 0, -1);
20973 }
20974
20975 /* Fill out the line with spaces. */
20976 if (it.current_x < it.last_visible_x)
20977 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20978
20979 /* Compute the total height of the lines. */
20980 compute_line_metrics (&it);
20981 }
20982
20983 /* Deep copy of a glyph row, including the glyphs. */
20984 static void
20985 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20986 {
20987 struct glyph *pointers[1 + LAST_AREA];
20988 int to_used = to->used[TEXT_AREA];
20989
20990 /* Save glyph pointers of TO. */
20991 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20992
20993 /* Do a structure assignment. */
20994 *to = *from;
20995
20996 /* Restore original glyph pointers of TO. */
20997 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20998
20999 /* Copy the glyphs. */
21000 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21001 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21002
21003 /* If we filled only part of the TO row, fill the rest with
21004 space_glyph (which will display as empty space). */
21005 if (to_used > from->used[TEXT_AREA])
21006 fill_up_frame_row_with_spaces (to, to_used);
21007 }
21008
21009 /* Display one menu item on a TTY, by overwriting the glyphs in the
21010 frame F's desired glyph matrix with glyphs produced from the menu
21011 item text. Called from term.c to display TTY drop-down menus one
21012 item at a time.
21013
21014 ITEM_TEXT is the menu item text as a C string.
21015
21016 FACE_ID is the face ID to be used for this menu item. FACE_ID
21017 could specify one of 3 faces: a face for an enabled item, a face
21018 for a disabled item, or a face for a selected item.
21019
21020 X and Y are coordinates of the first glyph in the frame's desired
21021 matrix to be overwritten by the menu item. Since this is a TTY, Y
21022 is the zero-based number of the glyph row and X is the zero-based
21023 glyph number in the row, starting from left, where to start
21024 displaying the item.
21025
21026 SUBMENU non-zero means this menu item drops down a submenu, which
21027 should be indicated by displaying a proper visual cue after the
21028 item text. */
21029
21030 void
21031 display_tty_menu_item (const char *item_text, int width, int face_id,
21032 int x, int y, int submenu)
21033 {
21034 struct it it;
21035 struct frame *f = SELECTED_FRAME ();
21036 struct window *w = XWINDOW (f->selected_window);
21037 int saved_used, saved_truncated, saved_width, saved_reversed;
21038 struct glyph_row *row;
21039 size_t item_len = strlen (item_text);
21040
21041 eassert (FRAME_TERMCAP_P (f));
21042
21043 /* Don't write beyond the matrix's last row. This can happen for
21044 TTY screens that are not high enough to show the entire menu.
21045 (This is actually a bit of defensive programming, as
21046 tty_menu_display already limits the number of menu items to one
21047 less than the number of screen lines.) */
21048 if (y >= f->desired_matrix->nrows)
21049 return;
21050
21051 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21052 it.first_visible_x = 0;
21053 it.last_visible_x = FRAME_COLS (f) - 1;
21054 row = it.glyph_row;
21055 /* Start with the row contents from the current matrix. */
21056 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21057 saved_width = row->full_width_p;
21058 row->full_width_p = 1;
21059 saved_reversed = row->reversed_p;
21060 row->reversed_p = 0;
21061 row->enabled_p = true;
21062
21063 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21064 desired face. */
21065 eassert (x < f->desired_matrix->matrix_w);
21066 it.current_x = it.hpos = x;
21067 it.current_y = it.vpos = y;
21068 saved_used = row->used[TEXT_AREA];
21069 saved_truncated = row->truncated_on_right_p;
21070 row->used[TEXT_AREA] = x;
21071 it.face_id = face_id;
21072 it.line_wrap = TRUNCATE;
21073
21074 /* FIXME: This should be controlled by a user option. See the
21075 comments in redisplay_tool_bar and display_mode_line about this.
21076 Also, if paragraph_embedding could ever be R2L, changes will be
21077 needed to avoid shifting to the right the row characters in
21078 term.c:append_glyph. */
21079 it.paragraph_embedding = L2R;
21080
21081 /* Pad with a space on the left. */
21082 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21083 width--;
21084 /* Display the menu item, pad with spaces to WIDTH. */
21085 if (submenu)
21086 {
21087 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21088 item_len, 0, FRAME_COLS (f) - 1, -1);
21089 width -= item_len;
21090 /* Indicate with " >" that there's a submenu. */
21091 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21092 FRAME_COLS (f) - 1, -1);
21093 }
21094 else
21095 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21096 width, 0, FRAME_COLS (f) - 1, -1);
21097
21098 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21099 row->truncated_on_right_p = saved_truncated;
21100 row->hash = row_hash (row);
21101 row->full_width_p = saved_width;
21102 row->reversed_p = saved_reversed;
21103 }
21104 \f
21105 /***********************************************************************
21106 Mode Line
21107 ***********************************************************************/
21108
21109 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21110 FORCE is non-zero, redisplay mode lines unconditionally.
21111 Otherwise, redisplay only mode lines that are garbaged. Value is
21112 the number of windows whose mode lines were redisplayed. */
21113
21114 static int
21115 redisplay_mode_lines (Lisp_Object window, bool force)
21116 {
21117 int nwindows = 0;
21118
21119 while (!NILP (window))
21120 {
21121 struct window *w = XWINDOW (window);
21122
21123 if (WINDOWP (w->contents))
21124 nwindows += redisplay_mode_lines (w->contents, force);
21125 else if (force
21126 || FRAME_GARBAGED_P (XFRAME (w->frame))
21127 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21128 {
21129 struct text_pos lpoint;
21130 struct buffer *old = current_buffer;
21131
21132 /* Set the window's buffer for the mode line display. */
21133 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21134 set_buffer_internal_1 (XBUFFER (w->contents));
21135
21136 /* Point refers normally to the selected window. For any
21137 other window, set up appropriate value. */
21138 if (!EQ (window, selected_window))
21139 {
21140 struct text_pos pt;
21141
21142 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21143 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21144 }
21145
21146 /* Display mode lines. */
21147 clear_glyph_matrix (w->desired_matrix);
21148 if (display_mode_lines (w))
21149 ++nwindows;
21150
21151 /* Restore old settings. */
21152 set_buffer_internal_1 (old);
21153 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21154 }
21155
21156 window = w->next;
21157 }
21158
21159 return nwindows;
21160 }
21161
21162
21163 /* Display the mode and/or header line of window W. Value is the
21164 sum number of mode lines and header lines displayed. */
21165
21166 static int
21167 display_mode_lines (struct window *w)
21168 {
21169 Lisp_Object old_selected_window = selected_window;
21170 Lisp_Object old_selected_frame = selected_frame;
21171 Lisp_Object new_frame = w->frame;
21172 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21173 int n = 0;
21174
21175 selected_frame = new_frame;
21176 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21177 or window's point, then we'd need select_window_1 here as well. */
21178 XSETWINDOW (selected_window, w);
21179 XFRAME (new_frame)->selected_window = selected_window;
21180
21181 /* These will be set while the mode line specs are processed. */
21182 line_number_displayed = 0;
21183 w->column_number_displayed = -1;
21184
21185 if (WINDOW_WANTS_MODELINE_P (w))
21186 {
21187 struct window *sel_w = XWINDOW (old_selected_window);
21188
21189 /* Select mode line face based on the real selected window. */
21190 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21191 BVAR (current_buffer, mode_line_format));
21192 ++n;
21193 }
21194
21195 if (WINDOW_WANTS_HEADER_LINE_P (w))
21196 {
21197 display_mode_line (w, HEADER_LINE_FACE_ID,
21198 BVAR (current_buffer, header_line_format));
21199 ++n;
21200 }
21201
21202 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21203 selected_frame = old_selected_frame;
21204 selected_window = old_selected_window;
21205 if (n > 0)
21206 w->must_be_updated_p = true;
21207 return n;
21208 }
21209
21210
21211 /* Display mode or header line of window W. FACE_ID specifies which
21212 line to display; it is either MODE_LINE_FACE_ID or
21213 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21214 display. Value is the pixel height of the mode/header line
21215 displayed. */
21216
21217 static int
21218 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21219 {
21220 struct it it;
21221 struct face *face;
21222 ptrdiff_t count = SPECPDL_INDEX ();
21223
21224 init_iterator (&it, w, -1, -1, NULL, face_id);
21225 /* Don't extend on a previously drawn mode-line.
21226 This may happen if called from pos_visible_p. */
21227 it.glyph_row->enabled_p = false;
21228 prepare_desired_row (it.glyph_row);
21229
21230 it.glyph_row->mode_line_p = 1;
21231
21232 /* FIXME: This should be controlled by a user option. But
21233 supporting such an option is not trivial, since the mode line is
21234 made up of many separate strings. */
21235 it.paragraph_embedding = L2R;
21236
21237 record_unwind_protect (unwind_format_mode_line,
21238 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21239
21240 mode_line_target = MODE_LINE_DISPLAY;
21241
21242 /* Temporarily make frame's keyboard the current kboard so that
21243 kboard-local variables in the mode_line_format will get the right
21244 values. */
21245 push_kboard (FRAME_KBOARD (it.f));
21246 record_unwind_save_match_data ();
21247 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21248 pop_kboard ();
21249
21250 unbind_to (count, Qnil);
21251
21252 /* Fill up with spaces. */
21253 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21254
21255 compute_line_metrics (&it);
21256 it.glyph_row->full_width_p = 1;
21257 it.glyph_row->continued_p = 0;
21258 it.glyph_row->truncated_on_left_p = 0;
21259 it.glyph_row->truncated_on_right_p = 0;
21260
21261 /* Make a 3D mode-line have a shadow at its right end. */
21262 face = FACE_FROM_ID (it.f, face_id);
21263 extend_face_to_end_of_line (&it);
21264 if (face->box != FACE_NO_BOX)
21265 {
21266 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21267 + it.glyph_row->used[TEXT_AREA] - 1);
21268 last->right_box_line_p = 1;
21269 }
21270
21271 return it.glyph_row->height;
21272 }
21273
21274 /* Move element ELT in LIST to the front of LIST.
21275 Return the updated list. */
21276
21277 static Lisp_Object
21278 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21279 {
21280 register Lisp_Object tail, prev;
21281 register Lisp_Object tem;
21282
21283 tail = list;
21284 prev = Qnil;
21285 while (CONSP (tail))
21286 {
21287 tem = XCAR (tail);
21288
21289 if (EQ (elt, tem))
21290 {
21291 /* Splice out the link TAIL. */
21292 if (NILP (prev))
21293 list = XCDR (tail);
21294 else
21295 Fsetcdr (prev, XCDR (tail));
21296
21297 /* Now make it the first. */
21298 Fsetcdr (tail, list);
21299 return tail;
21300 }
21301 else
21302 prev = tail;
21303 tail = XCDR (tail);
21304 QUIT;
21305 }
21306
21307 /* Not found--return unchanged LIST. */
21308 return list;
21309 }
21310
21311 /* Contribute ELT to the mode line for window IT->w. How it
21312 translates into text depends on its data type.
21313
21314 IT describes the display environment in which we display, as usual.
21315
21316 DEPTH is the depth in recursion. It is used to prevent
21317 infinite recursion here.
21318
21319 FIELD_WIDTH is the number of characters the display of ELT should
21320 occupy in the mode line, and PRECISION is the maximum number of
21321 characters to display from ELT's representation. See
21322 display_string for details.
21323
21324 Returns the hpos of the end of the text generated by ELT.
21325
21326 PROPS is a property list to add to any string we encounter.
21327
21328 If RISKY is nonzero, remove (disregard) any properties in any string
21329 we encounter, and ignore :eval and :propertize.
21330
21331 The global variable `mode_line_target' determines whether the
21332 output is passed to `store_mode_line_noprop',
21333 `store_mode_line_string', or `display_string'. */
21334
21335 static int
21336 display_mode_element (struct it *it, int depth, int field_width, int precision,
21337 Lisp_Object elt, Lisp_Object props, int risky)
21338 {
21339 int n = 0, field, prec;
21340 int literal = 0;
21341
21342 tail_recurse:
21343 if (depth > 100)
21344 elt = build_string ("*too-deep*");
21345
21346 depth++;
21347
21348 switch (XTYPE (elt))
21349 {
21350 case Lisp_String:
21351 {
21352 /* A string: output it and check for %-constructs within it. */
21353 unsigned char c;
21354 ptrdiff_t offset = 0;
21355
21356 if (SCHARS (elt) > 0
21357 && (!NILP (props) || risky))
21358 {
21359 Lisp_Object oprops, aelt;
21360 oprops = Ftext_properties_at (make_number (0), elt);
21361
21362 /* If the starting string's properties are not what
21363 we want, translate the string. Also, if the string
21364 is risky, do that anyway. */
21365
21366 if (NILP (Fequal (props, oprops)) || risky)
21367 {
21368 /* If the starting string has properties,
21369 merge the specified ones onto the existing ones. */
21370 if (! NILP (oprops) && !risky)
21371 {
21372 Lisp_Object tem;
21373
21374 oprops = Fcopy_sequence (oprops);
21375 tem = props;
21376 while (CONSP (tem))
21377 {
21378 oprops = Fplist_put (oprops, XCAR (tem),
21379 XCAR (XCDR (tem)));
21380 tem = XCDR (XCDR (tem));
21381 }
21382 props = oprops;
21383 }
21384
21385 aelt = Fassoc (elt, mode_line_proptrans_alist);
21386 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21387 {
21388 /* AELT is what we want. Move it to the front
21389 without consing. */
21390 elt = XCAR (aelt);
21391 mode_line_proptrans_alist
21392 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21393 }
21394 else
21395 {
21396 Lisp_Object tem;
21397
21398 /* If AELT has the wrong props, it is useless.
21399 so get rid of it. */
21400 if (! NILP (aelt))
21401 mode_line_proptrans_alist
21402 = Fdelq (aelt, mode_line_proptrans_alist);
21403
21404 elt = Fcopy_sequence (elt);
21405 Fset_text_properties (make_number (0), Flength (elt),
21406 props, elt);
21407 /* Add this item to mode_line_proptrans_alist. */
21408 mode_line_proptrans_alist
21409 = Fcons (Fcons (elt, props),
21410 mode_line_proptrans_alist);
21411 /* Truncate mode_line_proptrans_alist
21412 to at most 50 elements. */
21413 tem = Fnthcdr (make_number (50),
21414 mode_line_proptrans_alist);
21415 if (! NILP (tem))
21416 XSETCDR (tem, Qnil);
21417 }
21418 }
21419 }
21420
21421 offset = 0;
21422
21423 if (literal)
21424 {
21425 prec = precision - n;
21426 switch (mode_line_target)
21427 {
21428 case MODE_LINE_NOPROP:
21429 case MODE_LINE_TITLE:
21430 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21431 break;
21432 case MODE_LINE_STRING:
21433 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21434 break;
21435 case MODE_LINE_DISPLAY:
21436 n += display_string (NULL, elt, Qnil, 0, 0, it,
21437 0, prec, 0, STRING_MULTIBYTE (elt));
21438 break;
21439 }
21440
21441 break;
21442 }
21443
21444 /* Handle the non-literal case. */
21445
21446 while ((precision <= 0 || n < precision)
21447 && SREF (elt, offset) != 0
21448 && (mode_line_target != MODE_LINE_DISPLAY
21449 || it->current_x < it->last_visible_x))
21450 {
21451 ptrdiff_t last_offset = offset;
21452
21453 /* Advance to end of string or next format specifier. */
21454 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21455 ;
21456
21457 if (offset - 1 != last_offset)
21458 {
21459 ptrdiff_t nchars, nbytes;
21460
21461 /* Output to end of string or up to '%'. Field width
21462 is length of string. Don't output more than
21463 PRECISION allows us. */
21464 offset--;
21465
21466 prec = c_string_width (SDATA (elt) + last_offset,
21467 offset - last_offset, precision - n,
21468 &nchars, &nbytes);
21469
21470 switch (mode_line_target)
21471 {
21472 case MODE_LINE_NOPROP:
21473 case MODE_LINE_TITLE:
21474 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21475 break;
21476 case MODE_LINE_STRING:
21477 {
21478 ptrdiff_t bytepos = last_offset;
21479 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21480 ptrdiff_t endpos = (precision <= 0
21481 ? string_byte_to_char (elt, offset)
21482 : charpos + nchars);
21483
21484 n += store_mode_line_string (NULL,
21485 Fsubstring (elt, make_number (charpos),
21486 make_number (endpos)),
21487 0, 0, 0, Qnil);
21488 }
21489 break;
21490 case MODE_LINE_DISPLAY:
21491 {
21492 ptrdiff_t bytepos = last_offset;
21493 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21494
21495 if (precision <= 0)
21496 nchars = string_byte_to_char (elt, offset) - charpos;
21497 n += display_string (NULL, elt, Qnil, 0, charpos,
21498 it, 0, nchars, 0,
21499 STRING_MULTIBYTE (elt));
21500 }
21501 break;
21502 }
21503 }
21504 else /* c == '%' */
21505 {
21506 ptrdiff_t percent_position = offset;
21507
21508 /* Get the specified minimum width. Zero means
21509 don't pad. */
21510 field = 0;
21511 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21512 field = field * 10 + c - '0';
21513
21514 /* Don't pad beyond the total padding allowed. */
21515 if (field_width - n > 0 && field > field_width - n)
21516 field = field_width - n;
21517
21518 /* Note that either PRECISION <= 0 or N < PRECISION. */
21519 prec = precision - n;
21520
21521 if (c == 'M')
21522 n += display_mode_element (it, depth, field, prec,
21523 Vglobal_mode_string, props,
21524 risky);
21525 else if (c != 0)
21526 {
21527 bool multibyte;
21528 ptrdiff_t bytepos, charpos;
21529 const char *spec;
21530 Lisp_Object string;
21531
21532 bytepos = percent_position;
21533 charpos = (STRING_MULTIBYTE (elt)
21534 ? string_byte_to_char (elt, bytepos)
21535 : bytepos);
21536 spec = decode_mode_spec (it->w, c, field, &string);
21537 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21538
21539 switch (mode_line_target)
21540 {
21541 case MODE_LINE_NOPROP:
21542 case MODE_LINE_TITLE:
21543 n += store_mode_line_noprop (spec, field, prec);
21544 break;
21545 case MODE_LINE_STRING:
21546 {
21547 Lisp_Object tem = build_string (spec);
21548 props = Ftext_properties_at (make_number (charpos), elt);
21549 /* Should only keep face property in props */
21550 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21551 }
21552 break;
21553 case MODE_LINE_DISPLAY:
21554 {
21555 int nglyphs_before, nwritten;
21556
21557 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21558 nwritten = display_string (spec, string, elt,
21559 charpos, 0, it,
21560 field, prec, 0,
21561 multibyte);
21562
21563 /* Assign to the glyphs written above the
21564 string where the `%x' came from, position
21565 of the `%'. */
21566 if (nwritten > 0)
21567 {
21568 struct glyph *glyph
21569 = (it->glyph_row->glyphs[TEXT_AREA]
21570 + nglyphs_before);
21571 int i;
21572
21573 for (i = 0; i < nwritten; ++i)
21574 {
21575 glyph[i].object = elt;
21576 glyph[i].charpos = charpos;
21577 }
21578
21579 n += nwritten;
21580 }
21581 }
21582 break;
21583 }
21584 }
21585 else /* c == 0 */
21586 break;
21587 }
21588 }
21589 }
21590 break;
21591
21592 case Lisp_Symbol:
21593 /* A symbol: process the value of the symbol recursively
21594 as if it appeared here directly. Avoid error if symbol void.
21595 Special case: if value of symbol is a string, output the string
21596 literally. */
21597 {
21598 register Lisp_Object tem;
21599
21600 /* If the variable is not marked as risky to set
21601 then its contents are risky to use. */
21602 if (NILP (Fget (elt, Qrisky_local_variable)))
21603 risky = 1;
21604
21605 tem = Fboundp (elt);
21606 if (!NILP (tem))
21607 {
21608 tem = Fsymbol_value (elt);
21609 /* If value is a string, output that string literally:
21610 don't check for % within it. */
21611 if (STRINGP (tem))
21612 literal = 1;
21613
21614 if (!EQ (tem, elt))
21615 {
21616 /* Give up right away for nil or t. */
21617 elt = tem;
21618 goto tail_recurse;
21619 }
21620 }
21621 }
21622 break;
21623
21624 case Lisp_Cons:
21625 {
21626 register Lisp_Object car, tem;
21627
21628 /* A cons cell: five distinct cases.
21629 If first element is :eval or :propertize, do something special.
21630 If first element is a string or a cons, process all the elements
21631 and effectively concatenate them.
21632 If first element is a negative number, truncate displaying cdr to
21633 at most that many characters. If positive, pad (with spaces)
21634 to at least that many characters.
21635 If first element is a symbol, process the cadr or caddr recursively
21636 according to whether the symbol's value is non-nil or nil. */
21637 car = XCAR (elt);
21638 if (EQ (car, QCeval))
21639 {
21640 /* An element of the form (:eval FORM) means evaluate FORM
21641 and use the result as mode line elements. */
21642
21643 if (risky)
21644 break;
21645
21646 if (CONSP (XCDR (elt)))
21647 {
21648 Lisp_Object spec;
21649 spec = safe_eval (XCAR (XCDR (elt)));
21650 n += display_mode_element (it, depth, field_width - n,
21651 precision - n, spec, props,
21652 risky);
21653 }
21654 }
21655 else if (EQ (car, QCpropertize))
21656 {
21657 /* An element of the form (:propertize ELT PROPS...)
21658 means display ELT but applying properties PROPS. */
21659
21660 if (risky)
21661 break;
21662
21663 if (CONSP (XCDR (elt)))
21664 n += display_mode_element (it, depth, field_width - n,
21665 precision - n, XCAR (XCDR (elt)),
21666 XCDR (XCDR (elt)), risky);
21667 }
21668 else if (SYMBOLP (car))
21669 {
21670 tem = Fboundp (car);
21671 elt = XCDR (elt);
21672 if (!CONSP (elt))
21673 goto invalid;
21674 /* elt is now the cdr, and we know it is a cons cell.
21675 Use its car if CAR has a non-nil value. */
21676 if (!NILP (tem))
21677 {
21678 tem = Fsymbol_value (car);
21679 if (!NILP (tem))
21680 {
21681 elt = XCAR (elt);
21682 goto tail_recurse;
21683 }
21684 }
21685 /* Symbol's value is nil (or symbol is unbound)
21686 Get the cddr of the original list
21687 and if possible find the caddr and use that. */
21688 elt = XCDR (elt);
21689 if (NILP (elt))
21690 break;
21691 else if (!CONSP (elt))
21692 goto invalid;
21693 elt = XCAR (elt);
21694 goto tail_recurse;
21695 }
21696 else if (INTEGERP (car))
21697 {
21698 register int lim = XINT (car);
21699 elt = XCDR (elt);
21700 if (lim < 0)
21701 {
21702 /* Negative int means reduce maximum width. */
21703 if (precision <= 0)
21704 precision = -lim;
21705 else
21706 precision = min (precision, -lim);
21707 }
21708 else if (lim > 0)
21709 {
21710 /* Padding specified. Don't let it be more than
21711 current maximum. */
21712 if (precision > 0)
21713 lim = min (precision, lim);
21714
21715 /* If that's more padding than already wanted, queue it.
21716 But don't reduce padding already specified even if
21717 that is beyond the current truncation point. */
21718 field_width = max (lim, field_width);
21719 }
21720 goto tail_recurse;
21721 }
21722 else if (STRINGP (car) || CONSP (car))
21723 {
21724 Lisp_Object halftail = elt;
21725 int len = 0;
21726
21727 while (CONSP (elt)
21728 && (precision <= 0 || n < precision))
21729 {
21730 n += display_mode_element (it, depth,
21731 /* Do padding only after the last
21732 element in the list. */
21733 (! CONSP (XCDR (elt))
21734 ? field_width - n
21735 : 0),
21736 precision - n, XCAR (elt),
21737 props, risky);
21738 elt = XCDR (elt);
21739 len++;
21740 if ((len & 1) == 0)
21741 halftail = XCDR (halftail);
21742 /* Check for cycle. */
21743 if (EQ (halftail, elt))
21744 break;
21745 }
21746 }
21747 }
21748 break;
21749
21750 default:
21751 invalid:
21752 elt = build_string ("*invalid*");
21753 goto tail_recurse;
21754 }
21755
21756 /* Pad to FIELD_WIDTH. */
21757 if (field_width > 0 && n < field_width)
21758 {
21759 switch (mode_line_target)
21760 {
21761 case MODE_LINE_NOPROP:
21762 case MODE_LINE_TITLE:
21763 n += store_mode_line_noprop ("", field_width - n, 0);
21764 break;
21765 case MODE_LINE_STRING:
21766 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21767 break;
21768 case MODE_LINE_DISPLAY:
21769 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21770 0, 0, 0);
21771 break;
21772 }
21773 }
21774
21775 return n;
21776 }
21777
21778 /* Store a mode-line string element in mode_line_string_list.
21779
21780 If STRING is non-null, display that C string. Otherwise, the Lisp
21781 string LISP_STRING is displayed.
21782
21783 FIELD_WIDTH is the minimum number of output glyphs to produce.
21784 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21785 with spaces. FIELD_WIDTH <= 0 means don't pad.
21786
21787 PRECISION is the maximum number of characters to output from
21788 STRING. PRECISION <= 0 means don't truncate the string.
21789
21790 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21791 properties to the string.
21792
21793 PROPS are the properties to add to the string.
21794 The mode_line_string_face face property is always added to the string.
21795 */
21796
21797 static int
21798 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21799 int field_width, int precision, Lisp_Object props)
21800 {
21801 ptrdiff_t len;
21802 int n = 0;
21803
21804 if (string != NULL)
21805 {
21806 len = strlen (string);
21807 if (precision > 0 && len > precision)
21808 len = precision;
21809 lisp_string = make_string (string, len);
21810 if (NILP (props))
21811 props = mode_line_string_face_prop;
21812 else if (!NILP (mode_line_string_face))
21813 {
21814 Lisp_Object face = Fplist_get (props, Qface);
21815 props = Fcopy_sequence (props);
21816 if (NILP (face))
21817 face = mode_line_string_face;
21818 else
21819 face = list2 (face, mode_line_string_face);
21820 props = Fplist_put (props, Qface, face);
21821 }
21822 Fadd_text_properties (make_number (0), make_number (len),
21823 props, lisp_string);
21824 }
21825 else
21826 {
21827 len = XFASTINT (Flength (lisp_string));
21828 if (precision > 0 && len > precision)
21829 {
21830 len = precision;
21831 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21832 precision = -1;
21833 }
21834 if (!NILP (mode_line_string_face))
21835 {
21836 Lisp_Object face;
21837 if (NILP (props))
21838 props = Ftext_properties_at (make_number (0), lisp_string);
21839 face = Fplist_get (props, Qface);
21840 if (NILP (face))
21841 face = mode_line_string_face;
21842 else
21843 face = list2 (face, mode_line_string_face);
21844 props = list2 (Qface, face);
21845 if (copy_string)
21846 lisp_string = Fcopy_sequence (lisp_string);
21847 }
21848 if (!NILP (props))
21849 Fadd_text_properties (make_number (0), make_number (len),
21850 props, lisp_string);
21851 }
21852
21853 if (len > 0)
21854 {
21855 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21856 n += len;
21857 }
21858
21859 if (field_width > len)
21860 {
21861 field_width -= len;
21862 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21863 if (!NILP (props))
21864 Fadd_text_properties (make_number (0), make_number (field_width),
21865 props, lisp_string);
21866 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21867 n += field_width;
21868 }
21869
21870 return n;
21871 }
21872
21873
21874 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21875 1, 4, 0,
21876 doc: /* Format a string out of a mode line format specification.
21877 First arg FORMAT specifies the mode line format (see `mode-line-format'
21878 for details) to use.
21879
21880 By default, the format is evaluated for the currently selected window.
21881
21882 Optional second arg FACE specifies the face property to put on all
21883 characters for which no face is specified. The value nil means the
21884 default face. The value t means whatever face the window's mode line
21885 currently uses (either `mode-line' or `mode-line-inactive',
21886 depending on whether the window is the selected window or not).
21887 An integer value means the value string has no text
21888 properties.
21889
21890 Optional third and fourth args WINDOW and BUFFER specify the window
21891 and buffer to use as the context for the formatting (defaults
21892 are the selected window and the WINDOW's buffer). */)
21893 (Lisp_Object format, Lisp_Object face,
21894 Lisp_Object window, Lisp_Object buffer)
21895 {
21896 struct it it;
21897 int len;
21898 struct window *w;
21899 struct buffer *old_buffer = NULL;
21900 int face_id;
21901 int no_props = INTEGERP (face);
21902 ptrdiff_t count = SPECPDL_INDEX ();
21903 Lisp_Object str;
21904 int string_start = 0;
21905
21906 w = decode_any_window (window);
21907 XSETWINDOW (window, w);
21908
21909 if (NILP (buffer))
21910 buffer = w->contents;
21911 CHECK_BUFFER (buffer);
21912
21913 /* Make formatting the modeline a non-op when noninteractive, otherwise
21914 there will be problems later caused by a partially initialized frame. */
21915 if (NILP (format) || noninteractive)
21916 return empty_unibyte_string;
21917
21918 if (no_props)
21919 face = Qnil;
21920
21921 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21922 : EQ (face, Qt) ? (EQ (window, selected_window)
21923 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21924 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21925 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21926 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21927 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21928 : DEFAULT_FACE_ID;
21929
21930 old_buffer = current_buffer;
21931
21932 /* Save things including mode_line_proptrans_alist,
21933 and set that to nil so that we don't alter the outer value. */
21934 record_unwind_protect (unwind_format_mode_line,
21935 format_mode_line_unwind_data
21936 (XFRAME (WINDOW_FRAME (w)),
21937 old_buffer, selected_window, 1));
21938 mode_line_proptrans_alist = Qnil;
21939
21940 Fselect_window (window, Qt);
21941 set_buffer_internal_1 (XBUFFER (buffer));
21942
21943 init_iterator (&it, w, -1, -1, NULL, face_id);
21944
21945 if (no_props)
21946 {
21947 mode_line_target = MODE_LINE_NOPROP;
21948 mode_line_string_face_prop = Qnil;
21949 mode_line_string_list = Qnil;
21950 string_start = MODE_LINE_NOPROP_LEN (0);
21951 }
21952 else
21953 {
21954 mode_line_target = MODE_LINE_STRING;
21955 mode_line_string_list = Qnil;
21956 mode_line_string_face = face;
21957 mode_line_string_face_prop
21958 = NILP (face) ? Qnil : list2 (Qface, face);
21959 }
21960
21961 push_kboard (FRAME_KBOARD (it.f));
21962 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21963 pop_kboard ();
21964
21965 if (no_props)
21966 {
21967 len = MODE_LINE_NOPROP_LEN (string_start);
21968 str = make_string (mode_line_noprop_buf + string_start, len);
21969 }
21970 else
21971 {
21972 mode_line_string_list = Fnreverse (mode_line_string_list);
21973 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21974 empty_unibyte_string);
21975 }
21976
21977 unbind_to (count, Qnil);
21978 return str;
21979 }
21980
21981 /* Write a null-terminated, right justified decimal representation of
21982 the positive integer D to BUF using a minimal field width WIDTH. */
21983
21984 static void
21985 pint2str (register char *buf, register int width, register ptrdiff_t d)
21986 {
21987 register char *p = buf;
21988
21989 if (d <= 0)
21990 *p++ = '0';
21991 else
21992 {
21993 while (d > 0)
21994 {
21995 *p++ = d % 10 + '0';
21996 d /= 10;
21997 }
21998 }
21999
22000 for (width -= (int) (p - buf); width > 0; --width)
22001 *p++ = ' ';
22002 *p-- = '\0';
22003 while (p > buf)
22004 {
22005 d = *buf;
22006 *buf++ = *p;
22007 *p-- = d;
22008 }
22009 }
22010
22011 /* Write a null-terminated, right justified decimal and "human
22012 readable" representation of the nonnegative integer D to BUF using
22013 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22014
22015 static const char power_letter[] =
22016 {
22017 0, /* no letter */
22018 'k', /* kilo */
22019 'M', /* mega */
22020 'G', /* giga */
22021 'T', /* tera */
22022 'P', /* peta */
22023 'E', /* exa */
22024 'Z', /* zetta */
22025 'Y' /* yotta */
22026 };
22027
22028 static void
22029 pint2hrstr (char *buf, int width, ptrdiff_t d)
22030 {
22031 /* We aim to represent the nonnegative integer D as
22032 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22033 ptrdiff_t quotient = d;
22034 int remainder = 0;
22035 /* -1 means: do not use TENTHS. */
22036 int tenths = -1;
22037 int exponent = 0;
22038
22039 /* Length of QUOTIENT.TENTHS as a string. */
22040 int length;
22041
22042 char * psuffix;
22043 char * p;
22044
22045 if (quotient >= 1000)
22046 {
22047 /* Scale to the appropriate EXPONENT. */
22048 do
22049 {
22050 remainder = quotient % 1000;
22051 quotient /= 1000;
22052 exponent++;
22053 }
22054 while (quotient >= 1000);
22055
22056 /* Round to nearest and decide whether to use TENTHS or not. */
22057 if (quotient <= 9)
22058 {
22059 tenths = remainder / 100;
22060 if (remainder % 100 >= 50)
22061 {
22062 if (tenths < 9)
22063 tenths++;
22064 else
22065 {
22066 quotient++;
22067 if (quotient == 10)
22068 tenths = -1;
22069 else
22070 tenths = 0;
22071 }
22072 }
22073 }
22074 else
22075 if (remainder >= 500)
22076 {
22077 if (quotient < 999)
22078 quotient++;
22079 else
22080 {
22081 quotient = 1;
22082 exponent++;
22083 tenths = 0;
22084 }
22085 }
22086 }
22087
22088 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22089 if (tenths == -1 && quotient <= 99)
22090 if (quotient <= 9)
22091 length = 1;
22092 else
22093 length = 2;
22094 else
22095 length = 3;
22096 p = psuffix = buf + max (width, length);
22097
22098 /* Print EXPONENT. */
22099 *psuffix++ = power_letter[exponent];
22100 *psuffix = '\0';
22101
22102 /* Print TENTHS. */
22103 if (tenths >= 0)
22104 {
22105 *--p = '0' + tenths;
22106 *--p = '.';
22107 }
22108
22109 /* Print QUOTIENT. */
22110 do
22111 {
22112 int digit = quotient % 10;
22113 *--p = '0' + digit;
22114 }
22115 while ((quotient /= 10) != 0);
22116
22117 /* Print leading spaces. */
22118 while (buf < p)
22119 *--p = ' ';
22120 }
22121
22122 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22123 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22124 type of CODING_SYSTEM. Return updated pointer into BUF. */
22125
22126 static unsigned char invalid_eol_type[] = "(*invalid*)";
22127
22128 static char *
22129 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22130 {
22131 Lisp_Object val;
22132 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22133 const unsigned char *eol_str;
22134 int eol_str_len;
22135 /* The EOL conversion we are using. */
22136 Lisp_Object eoltype;
22137
22138 val = CODING_SYSTEM_SPEC (coding_system);
22139 eoltype = Qnil;
22140
22141 if (!VECTORP (val)) /* Not yet decided. */
22142 {
22143 *buf++ = multibyte ? '-' : ' ';
22144 if (eol_flag)
22145 eoltype = eol_mnemonic_undecided;
22146 /* Don't mention EOL conversion if it isn't decided. */
22147 }
22148 else
22149 {
22150 Lisp_Object attrs;
22151 Lisp_Object eolvalue;
22152
22153 attrs = AREF (val, 0);
22154 eolvalue = AREF (val, 2);
22155
22156 *buf++ = multibyte
22157 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22158 : ' ';
22159
22160 if (eol_flag)
22161 {
22162 /* The EOL conversion that is normal on this system. */
22163
22164 if (NILP (eolvalue)) /* Not yet decided. */
22165 eoltype = eol_mnemonic_undecided;
22166 else if (VECTORP (eolvalue)) /* Not yet decided. */
22167 eoltype = eol_mnemonic_undecided;
22168 else /* eolvalue is Qunix, Qdos, or Qmac. */
22169 eoltype = (EQ (eolvalue, Qunix)
22170 ? eol_mnemonic_unix
22171 : (EQ (eolvalue, Qdos) == 1
22172 ? eol_mnemonic_dos : eol_mnemonic_mac));
22173 }
22174 }
22175
22176 if (eol_flag)
22177 {
22178 /* Mention the EOL conversion if it is not the usual one. */
22179 if (STRINGP (eoltype))
22180 {
22181 eol_str = SDATA (eoltype);
22182 eol_str_len = SBYTES (eoltype);
22183 }
22184 else if (CHARACTERP (eoltype))
22185 {
22186 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22187 int c = XFASTINT (eoltype);
22188 eol_str_len = CHAR_STRING (c, tmp);
22189 eol_str = tmp;
22190 }
22191 else
22192 {
22193 eol_str = invalid_eol_type;
22194 eol_str_len = sizeof (invalid_eol_type) - 1;
22195 }
22196 memcpy (buf, eol_str, eol_str_len);
22197 buf += eol_str_len;
22198 }
22199
22200 return buf;
22201 }
22202
22203 /* Return a string for the output of a mode line %-spec for window W,
22204 generated by character C. FIELD_WIDTH > 0 means pad the string
22205 returned with spaces to that value. Return a Lisp string in
22206 *STRING if the resulting string is taken from that Lisp string.
22207
22208 Note we operate on the current buffer for most purposes. */
22209
22210 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22211
22212 static const char *
22213 decode_mode_spec (struct window *w, register int c, int field_width,
22214 Lisp_Object *string)
22215 {
22216 Lisp_Object obj;
22217 struct frame *f = XFRAME (WINDOW_FRAME (w));
22218 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22219 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22220 produce strings from numerical values, so limit preposterously
22221 large values of FIELD_WIDTH to avoid overrunning the buffer's
22222 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22223 bytes plus the terminating null. */
22224 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22225 struct buffer *b = current_buffer;
22226
22227 obj = Qnil;
22228 *string = Qnil;
22229
22230 switch (c)
22231 {
22232 case '*':
22233 if (!NILP (BVAR (b, read_only)))
22234 return "%";
22235 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22236 return "*";
22237 return "-";
22238
22239 case '+':
22240 /* This differs from %* only for a modified read-only buffer. */
22241 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22242 return "*";
22243 if (!NILP (BVAR (b, read_only)))
22244 return "%";
22245 return "-";
22246
22247 case '&':
22248 /* This differs from %* in ignoring read-only-ness. */
22249 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22250 return "*";
22251 return "-";
22252
22253 case '%':
22254 return "%";
22255
22256 case '[':
22257 {
22258 int i;
22259 char *p;
22260
22261 if (command_loop_level > 5)
22262 return "[[[... ";
22263 p = decode_mode_spec_buf;
22264 for (i = 0; i < command_loop_level; i++)
22265 *p++ = '[';
22266 *p = 0;
22267 return decode_mode_spec_buf;
22268 }
22269
22270 case ']':
22271 {
22272 int i;
22273 char *p;
22274
22275 if (command_loop_level > 5)
22276 return " ...]]]";
22277 p = decode_mode_spec_buf;
22278 for (i = 0; i < command_loop_level; i++)
22279 *p++ = ']';
22280 *p = 0;
22281 return decode_mode_spec_buf;
22282 }
22283
22284 case '-':
22285 {
22286 register int i;
22287
22288 /* Let lots_of_dashes be a string of infinite length. */
22289 if (mode_line_target == MODE_LINE_NOPROP
22290 || mode_line_target == MODE_LINE_STRING)
22291 return "--";
22292 if (field_width <= 0
22293 || field_width > sizeof (lots_of_dashes))
22294 {
22295 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22296 decode_mode_spec_buf[i] = '-';
22297 decode_mode_spec_buf[i] = '\0';
22298 return decode_mode_spec_buf;
22299 }
22300 else
22301 return lots_of_dashes;
22302 }
22303
22304 case 'b':
22305 obj = BVAR (b, name);
22306 break;
22307
22308 case 'c':
22309 /* %c and %l are ignored in `frame-title-format'.
22310 (In redisplay_internal, the frame title is drawn _before_ the
22311 windows are updated, so the stuff which depends on actual
22312 window contents (such as %l) may fail to render properly, or
22313 even crash emacs.) */
22314 if (mode_line_target == MODE_LINE_TITLE)
22315 return "";
22316 else
22317 {
22318 ptrdiff_t col = current_column ();
22319 w->column_number_displayed = col;
22320 pint2str (decode_mode_spec_buf, width, col);
22321 return decode_mode_spec_buf;
22322 }
22323
22324 case 'e':
22325 #ifndef SYSTEM_MALLOC
22326 {
22327 if (NILP (Vmemory_full))
22328 return "";
22329 else
22330 return "!MEM FULL! ";
22331 }
22332 #else
22333 return "";
22334 #endif
22335
22336 case 'F':
22337 /* %F displays the frame name. */
22338 if (!NILP (f->title))
22339 return SSDATA (f->title);
22340 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22341 return SSDATA (f->name);
22342 return "Emacs";
22343
22344 case 'f':
22345 obj = BVAR (b, filename);
22346 break;
22347
22348 case 'i':
22349 {
22350 ptrdiff_t size = ZV - BEGV;
22351 pint2str (decode_mode_spec_buf, width, size);
22352 return decode_mode_spec_buf;
22353 }
22354
22355 case 'I':
22356 {
22357 ptrdiff_t size = ZV - BEGV;
22358 pint2hrstr (decode_mode_spec_buf, width, size);
22359 return decode_mode_spec_buf;
22360 }
22361
22362 case 'l':
22363 {
22364 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22365 ptrdiff_t topline, nlines, height;
22366 ptrdiff_t junk;
22367
22368 /* %c and %l are ignored in `frame-title-format'. */
22369 if (mode_line_target == MODE_LINE_TITLE)
22370 return "";
22371
22372 startpos = marker_position (w->start);
22373 startpos_byte = marker_byte_position (w->start);
22374 height = WINDOW_TOTAL_LINES (w);
22375
22376 /* If we decided that this buffer isn't suitable for line numbers,
22377 don't forget that too fast. */
22378 if (w->base_line_pos == -1)
22379 goto no_value;
22380
22381 /* If the buffer is very big, don't waste time. */
22382 if (INTEGERP (Vline_number_display_limit)
22383 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22384 {
22385 w->base_line_pos = 0;
22386 w->base_line_number = 0;
22387 goto no_value;
22388 }
22389
22390 if (w->base_line_number > 0
22391 && w->base_line_pos > 0
22392 && w->base_line_pos <= startpos)
22393 {
22394 line = w->base_line_number;
22395 linepos = w->base_line_pos;
22396 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22397 }
22398 else
22399 {
22400 line = 1;
22401 linepos = BUF_BEGV (b);
22402 linepos_byte = BUF_BEGV_BYTE (b);
22403 }
22404
22405 /* Count lines from base line to window start position. */
22406 nlines = display_count_lines (linepos_byte,
22407 startpos_byte,
22408 startpos, &junk);
22409
22410 topline = nlines + line;
22411
22412 /* Determine a new base line, if the old one is too close
22413 or too far away, or if we did not have one.
22414 "Too close" means it's plausible a scroll-down would
22415 go back past it. */
22416 if (startpos == BUF_BEGV (b))
22417 {
22418 w->base_line_number = topline;
22419 w->base_line_pos = BUF_BEGV (b);
22420 }
22421 else if (nlines < height + 25 || nlines > height * 3 + 50
22422 || linepos == BUF_BEGV (b))
22423 {
22424 ptrdiff_t limit = BUF_BEGV (b);
22425 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22426 ptrdiff_t position;
22427 ptrdiff_t distance =
22428 (height * 2 + 30) * line_number_display_limit_width;
22429
22430 if (startpos - distance > limit)
22431 {
22432 limit = startpos - distance;
22433 limit_byte = CHAR_TO_BYTE (limit);
22434 }
22435
22436 nlines = display_count_lines (startpos_byte,
22437 limit_byte,
22438 - (height * 2 + 30),
22439 &position);
22440 /* If we couldn't find the lines we wanted within
22441 line_number_display_limit_width chars per line,
22442 give up on line numbers for this window. */
22443 if (position == limit_byte && limit == startpos - distance)
22444 {
22445 w->base_line_pos = -1;
22446 w->base_line_number = 0;
22447 goto no_value;
22448 }
22449
22450 w->base_line_number = topline - nlines;
22451 w->base_line_pos = BYTE_TO_CHAR (position);
22452 }
22453
22454 /* Now count lines from the start pos to point. */
22455 nlines = display_count_lines (startpos_byte,
22456 PT_BYTE, PT, &junk);
22457
22458 /* Record that we did display the line number. */
22459 line_number_displayed = 1;
22460
22461 /* Make the string to show. */
22462 pint2str (decode_mode_spec_buf, width, topline + nlines);
22463 return decode_mode_spec_buf;
22464 no_value:
22465 {
22466 char* p = decode_mode_spec_buf;
22467 int pad = width - 2;
22468 while (pad-- > 0)
22469 *p++ = ' ';
22470 *p++ = '?';
22471 *p++ = '?';
22472 *p = '\0';
22473 return decode_mode_spec_buf;
22474 }
22475 }
22476 break;
22477
22478 case 'm':
22479 obj = BVAR (b, mode_name);
22480 break;
22481
22482 case 'n':
22483 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22484 return " Narrow";
22485 break;
22486
22487 case 'p':
22488 {
22489 ptrdiff_t pos = marker_position (w->start);
22490 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22491
22492 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22493 {
22494 if (pos <= BUF_BEGV (b))
22495 return "All";
22496 else
22497 return "Bottom";
22498 }
22499 else if (pos <= BUF_BEGV (b))
22500 return "Top";
22501 else
22502 {
22503 if (total > 1000000)
22504 /* Do it differently for a large value, to avoid overflow. */
22505 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22506 else
22507 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22508 /* We can't normally display a 3-digit number,
22509 so get us a 2-digit number that is close. */
22510 if (total == 100)
22511 total = 99;
22512 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22513 return decode_mode_spec_buf;
22514 }
22515 }
22516
22517 /* Display percentage of size above the bottom of the screen. */
22518 case 'P':
22519 {
22520 ptrdiff_t toppos = marker_position (w->start);
22521 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22522 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22523
22524 if (botpos >= BUF_ZV (b))
22525 {
22526 if (toppos <= BUF_BEGV (b))
22527 return "All";
22528 else
22529 return "Bottom";
22530 }
22531 else
22532 {
22533 if (total > 1000000)
22534 /* Do it differently for a large value, to avoid overflow. */
22535 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22536 else
22537 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22538 /* We can't normally display a 3-digit number,
22539 so get us a 2-digit number that is close. */
22540 if (total == 100)
22541 total = 99;
22542 if (toppos <= BUF_BEGV (b))
22543 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22544 else
22545 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22546 return decode_mode_spec_buf;
22547 }
22548 }
22549
22550 case 's':
22551 /* status of process */
22552 obj = Fget_buffer_process (Fcurrent_buffer ());
22553 if (NILP (obj))
22554 return "no process";
22555 #ifndef MSDOS
22556 obj = Fsymbol_name (Fprocess_status (obj));
22557 #endif
22558 break;
22559
22560 case '@':
22561 {
22562 ptrdiff_t count = inhibit_garbage_collection ();
22563 Lisp_Object val = call1 (intern ("file-remote-p"),
22564 BVAR (current_buffer, directory));
22565 unbind_to (count, Qnil);
22566
22567 if (NILP (val))
22568 return "-";
22569 else
22570 return "@";
22571 }
22572
22573 case 'z':
22574 /* coding-system (not including end-of-line format) */
22575 case 'Z':
22576 /* coding-system (including end-of-line type) */
22577 {
22578 int eol_flag = (c == 'Z');
22579 char *p = decode_mode_spec_buf;
22580
22581 if (! FRAME_WINDOW_P (f))
22582 {
22583 /* No need to mention EOL here--the terminal never needs
22584 to do EOL conversion. */
22585 p = decode_mode_spec_coding (CODING_ID_NAME
22586 (FRAME_KEYBOARD_CODING (f)->id),
22587 p, 0);
22588 p = decode_mode_spec_coding (CODING_ID_NAME
22589 (FRAME_TERMINAL_CODING (f)->id),
22590 p, 0);
22591 }
22592 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22593 p, eol_flag);
22594
22595 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22596 #ifdef subprocesses
22597 obj = Fget_buffer_process (Fcurrent_buffer ());
22598 if (PROCESSP (obj))
22599 {
22600 p = decode_mode_spec_coding
22601 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22602 p = decode_mode_spec_coding
22603 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22604 }
22605 #endif /* subprocesses */
22606 #endif /* 0 */
22607 *p = 0;
22608 return decode_mode_spec_buf;
22609 }
22610 }
22611
22612 if (STRINGP (obj))
22613 {
22614 *string = obj;
22615 return SSDATA (obj);
22616 }
22617 else
22618 return "";
22619 }
22620
22621
22622 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22623 means count lines back from START_BYTE. But don't go beyond
22624 LIMIT_BYTE. Return the number of lines thus found (always
22625 nonnegative).
22626
22627 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22628 either the position COUNT lines after/before START_BYTE, if we
22629 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22630 COUNT lines. */
22631
22632 static ptrdiff_t
22633 display_count_lines (ptrdiff_t start_byte,
22634 ptrdiff_t limit_byte, ptrdiff_t count,
22635 ptrdiff_t *byte_pos_ptr)
22636 {
22637 register unsigned char *cursor;
22638 unsigned char *base;
22639
22640 register ptrdiff_t ceiling;
22641 register unsigned char *ceiling_addr;
22642 ptrdiff_t orig_count = count;
22643
22644 /* If we are not in selective display mode,
22645 check only for newlines. */
22646 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22647 && !INTEGERP (BVAR (current_buffer, selective_display)));
22648
22649 if (count > 0)
22650 {
22651 while (start_byte < limit_byte)
22652 {
22653 ceiling = BUFFER_CEILING_OF (start_byte);
22654 ceiling = min (limit_byte - 1, ceiling);
22655 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22656 base = (cursor = BYTE_POS_ADDR (start_byte));
22657
22658 do
22659 {
22660 if (selective_display)
22661 {
22662 while (*cursor != '\n' && *cursor != 015
22663 && ++cursor != ceiling_addr)
22664 continue;
22665 if (cursor == ceiling_addr)
22666 break;
22667 }
22668 else
22669 {
22670 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22671 if (! cursor)
22672 break;
22673 }
22674
22675 cursor++;
22676
22677 if (--count == 0)
22678 {
22679 start_byte += cursor - base;
22680 *byte_pos_ptr = start_byte;
22681 return orig_count;
22682 }
22683 }
22684 while (cursor < ceiling_addr);
22685
22686 start_byte += ceiling_addr - base;
22687 }
22688 }
22689 else
22690 {
22691 while (start_byte > limit_byte)
22692 {
22693 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22694 ceiling = max (limit_byte, ceiling);
22695 ceiling_addr = BYTE_POS_ADDR (ceiling);
22696 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22697 while (1)
22698 {
22699 if (selective_display)
22700 {
22701 while (--cursor >= ceiling_addr
22702 && *cursor != '\n' && *cursor != 015)
22703 continue;
22704 if (cursor < ceiling_addr)
22705 break;
22706 }
22707 else
22708 {
22709 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22710 if (! cursor)
22711 break;
22712 }
22713
22714 if (++count == 0)
22715 {
22716 start_byte += cursor - base + 1;
22717 *byte_pos_ptr = start_byte;
22718 /* When scanning backwards, we should
22719 not count the newline posterior to which we stop. */
22720 return - orig_count - 1;
22721 }
22722 }
22723 start_byte += ceiling_addr - base;
22724 }
22725 }
22726
22727 *byte_pos_ptr = limit_byte;
22728
22729 if (count < 0)
22730 return - orig_count + count;
22731 return orig_count - count;
22732
22733 }
22734
22735
22736 \f
22737 /***********************************************************************
22738 Displaying strings
22739 ***********************************************************************/
22740
22741 /* Display a NUL-terminated string, starting with index START.
22742
22743 If STRING is non-null, display that C string. Otherwise, the Lisp
22744 string LISP_STRING is displayed. There's a case that STRING is
22745 non-null and LISP_STRING is not nil. It means STRING is a string
22746 data of LISP_STRING. In that case, we display LISP_STRING while
22747 ignoring its text properties.
22748
22749 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22750 FACE_STRING. Display STRING or LISP_STRING with the face at
22751 FACE_STRING_POS in FACE_STRING:
22752
22753 Display the string in the environment given by IT, but use the
22754 standard display table, temporarily.
22755
22756 FIELD_WIDTH is the minimum number of output glyphs to produce.
22757 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22758 with spaces. If STRING has more characters, more than FIELD_WIDTH
22759 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22760
22761 PRECISION is the maximum number of characters to output from
22762 STRING. PRECISION < 0 means don't truncate the string.
22763
22764 This is roughly equivalent to printf format specifiers:
22765
22766 FIELD_WIDTH PRECISION PRINTF
22767 ----------------------------------------
22768 -1 -1 %s
22769 -1 10 %.10s
22770 10 -1 %10s
22771 20 10 %20.10s
22772
22773 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22774 display them, and < 0 means obey the current buffer's value of
22775 enable_multibyte_characters.
22776
22777 Value is the number of columns displayed. */
22778
22779 static int
22780 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22781 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22782 int field_width, int precision, int max_x, int multibyte)
22783 {
22784 int hpos_at_start = it->hpos;
22785 int saved_face_id = it->face_id;
22786 struct glyph_row *row = it->glyph_row;
22787 ptrdiff_t it_charpos;
22788
22789 /* Initialize the iterator IT for iteration over STRING beginning
22790 with index START. */
22791 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22792 precision, field_width, multibyte);
22793 if (string && STRINGP (lisp_string))
22794 /* LISP_STRING is the one returned by decode_mode_spec. We should
22795 ignore its text properties. */
22796 it->stop_charpos = it->end_charpos;
22797
22798 /* If displaying STRING, set up the face of the iterator from
22799 FACE_STRING, if that's given. */
22800 if (STRINGP (face_string))
22801 {
22802 ptrdiff_t endptr;
22803 struct face *face;
22804
22805 it->face_id
22806 = face_at_string_position (it->w, face_string, face_string_pos,
22807 0, &endptr, it->base_face_id, 0);
22808 face = FACE_FROM_ID (it->f, it->face_id);
22809 it->face_box_p = face->box != FACE_NO_BOX;
22810 }
22811
22812 /* Set max_x to the maximum allowed X position. Don't let it go
22813 beyond the right edge of the window. */
22814 if (max_x <= 0)
22815 max_x = it->last_visible_x;
22816 else
22817 max_x = min (max_x, it->last_visible_x);
22818
22819 /* Skip over display elements that are not visible. because IT->w is
22820 hscrolled. */
22821 if (it->current_x < it->first_visible_x)
22822 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22823 MOVE_TO_POS | MOVE_TO_X);
22824
22825 row->ascent = it->max_ascent;
22826 row->height = it->max_ascent + it->max_descent;
22827 row->phys_ascent = it->max_phys_ascent;
22828 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22829 row->extra_line_spacing = it->max_extra_line_spacing;
22830
22831 if (STRINGP (it->string))
22832 it_charpos = IT_STRING_CHARPOS (*it);
22833 else
22834 it_charpos = IT_CHARPOS (*it);
22835
22836 /* This condition is for the case that we are called with current_x
22837 past last_visible_x. */
22838 while (it->current_x < max_x)
22839 {
22840 int x_before, x, n_glyphs_before, i, nglyphs;
22841
22842 /* Get the next display element. */
22843 if (!get_next_display_element (it))
22844 break;
22845
22846 /* Produce glyphs. */
22847 x_before = it->current_x;
22848 n_glyphs_before = row->used[TEXT_AREA];
22849 PRODUCE_GLYPHS (it);
22850
22851 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22852 i = 0;
22853 x = x_before;
22854 while (i < nglyphs)
22855 {
22856 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22857
22858 if (it->line_wrap != TRUNCATE
22859 && x + glyph->pixel_width > max_x)
22860 {
22861 /* End of continued line or max_x reached. */
22862 if (CHAR_GLYPH_PADDING_P (*glyph))
22863 {
22864 /* A wide character is unbreakable. */
22865 if (row->reversed_p)
22866 unproduce_glyphs (it, row->used[TEXT_AREA]
22867 - n_glyphs_before);
22868 row->used[TEXT_AREA] = n_glyphs_before;
22869 it->current_x = x_before;
22870 }
22871 else
22872 {
22873 if (row->reversed_p)
22874 unproduce_glyphs (it, row->used[TEXT_AREA]
22875 - (n_glyphs_before + i));
22876 row->used[TEXT_AREA] = n_glyphs_before + i;
22877 it->current_x = x;
22878 }
22879 break;
22880 }
22881 else if (x + glyph->pixel_width >= it->first_visible_x)
22882 {
22883 /* Glyph is at least partially visible. */
22884 ++it->hpos;
22885 if (x < it->first_visible_x)
22886 row->x = x - it->first_visible_x;
22887 }
22888 else
22889 {
22890 /* Glyph is off the left margin of the display area.
22891 Should not happen. */
22892 emacs_abort ();
22893 }
22894
22895 row->ascent = max (row->ascent, it->max_ascent);
22896 row->height = max (row->height, it->max_ascent + it->max_descent);
22897 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22898 row->phys_height = max (row->phys_height,
22899 it->max_phys_ascent + it->max_phys_descent);
22900 row->extra_line_spacing = max (row->extra_line_spacing,
22901 it->max_extra_line_spacing);
22902 x += glyph->pixel_width;
22903 ++i;
22904 }
22905
22906 /* Stop if max_x reached. */
22907 if (i < nglyphs)
22908 break;
22909
22910 /* Stop at line ends. */
22911 if (ITERATOR_AT_END_OF_LINE_P (it))
22912 {
22913 it->continuation_lines_width = 0;
22914 break;
22915 }
22916
22917 set_iterator_to_next (it, 1);
22918 if (STRINGP (it->string))
22919 it_charpos = IT_STRING_CHARPOS (*it);
22920 else
22921 it_charpos = IT_CHARPOS (*it);
22922
22923 /* Stop if truncating at the right edge. */
22924 if (it->line_wrap == TRUNCATE
22925 && it->current_x >= it->last_visible_x)
22926 {
22927 /* Add truncation mark, but don't do it if the line is
22928 truncated at a padding space. */
22929 if (it_charpos < it->string_nchars)
22930 {
22931 if (!FRAME_WINDOW_P (it->f))
22932 {
22933 int ii, n;
22934
22935 if (it->current_x > it->last_visible_x)
22936 {
22937 if (!row->reversed_p)
22938 {
22939 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22940 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22941 break;
22942 }
22943 else
22944 {
22945 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22946 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22947 break;
22948 unproduce_glyphs (it, ii + 1);
22949 ii = row->used[TEXT_AREA] - (ii + 1);
22950 }
22951 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22952 {
22953 row->used[TEXT_AREA] = ii;
22954 produce_special_glyphs (it, IT_TRUNCATION);
22955 }
22956 }
22957 produce_special_glyphs (it, IT_TRUNCATION);
22958 }
22959 row->truncated_on_right_p = 1;
22960 }
22961 break;
22962 }
22963 }
22964
22965 /* Maybe insert a truncation at the left. */
22966 if (it->first_visible_x
22967 && it_charpos > 0)
22968 {
22969 if (!FRAME_WINDOW_P (it->f)
22970 || (row->reversed_p
22971 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22972 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22973 insert_left_trunc_glyphs (it);
22974 row->truncated_on_left_p = 1;
22975 }
22976
22977 it->face_id = saved_face_id;
22978
22979 /* Value is number of columns displayed. */
22980 return it->hpos - hpos_at_start;
22981 }
22982
22983
22984 \f
22985 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22986 appears as an element of LIST or as the car of an element of LIST.
22987 If PROPVAL is a list, compare each element against LIST in that
22988 way, and return 1/2 if any element of PROPVAL is found in LIST.
22989 Otherwise return 0. This function cannot quit.
22990 The return value is 2 if the text is invisible but with an ellipsis
22991 and 1 if it's invisible and without an ellipsis. */
22992
22993 int
22994 invisible_p (register Lisp_Object propval, Lisp_Object list)
22995 {
22996 register Lisp_Object tail, proptail;
22997
22998 for (tail = list; CONSP (tail); tail = XCDR (tail))
22999 {
23000 register Lisp_Object tem;
23001 tem = XCAR (tail);
23002 if (EQ (propval, tem))
23003 return 1;
23004 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23005 return NILP (XCDR (tem)) ? 1 : 2;
23006 }
23007
23008 if (CONSP (propval))
23009 {
23010 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23011 {
23012 Lisp_Object propelt;
23013 propelt = XCAR (proptail);
23014 for (tail = list; CONSP (tail); tail = XCDR (tail))
23015 {
23016 register Lisp_Object tem;
23017 tem = XCAR (tail);
23018 if (EQ (propelt, tem))
23019 return 1;
23020 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23021 return NILP (XCDR (tem)) ? 1 : 2;
23022 }
23023 }
23024 }
23025
23026 return 0;
23027 }
23028
23029 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23030 doc: /* Non-nil if the property makes the text invisible.
23031 POS-OR-PROP can be a marker or number, in which case it is taken to be
23032 a position in the current buffer and the value of the `invisible' property
23033 is checked; or it can be some other value, which is then presumed to be the
23034 value of the `invisible' property of the text of interest.
23035 The non-nil value returned can be t for truly invisible text or something
23036 else if the text is replaced by an ellipsis. */)
23037 (Lisp_Object pos_or_prop)
23038 {
23039 Lisp_Object prop
23040 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23041 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23042 : pos_or_prop);
23043 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23044 return (invis == 0 ? Qnil
23045 : invis == 1 ? Qt
23046 : make_number (invis));
23047 }
23048
23049 /* Calculate a width or height in pixels from a specification using
23050 the following elements:
23051
23052 SPEC ::=
23053 NUM - a (fractional) multiple of the default font width/height
23054 (NUM) - specifies exactly NUM pixels
23055 UNIT - a fixed number of pixels, see below.
23056 ELEMENT - size of a display element in pixels, see below.
23057 (NUM . SPEC) - equals NUM * SPEC
23058 (+ SPEC SPEC ...) - add pixel values
23059 (- SPEC SPEC ...) - subtract pixel values
23060 (- SPEC) - negate pixel value
23061
23062 NUM ::=
23063 INT or FLOAT - a number constant
23064 SYMBOL - use symbol's (buffer local) variable binding.
23065
23066 UNIT ::=
23067 in - pixels per inch *)
23068 mm - pixels per 1/1000 meter *)
23069 cm - pixels per 1/100 meter *)
23070 width - width of current font in pixels.
23071 height - height of current font in pixels.
23072
23073 *) using the ratio(s) defined in display-pixels-per-inch.
23074
23075 ELEMENT ::=
23076
23077 left-fringe - left fringe width in pixels
23078 right-fringe - right fringe width in pixels
23079
23080 left-margin - left margin width in pixels
23081 right-margin - right margin width in pixels
23082
23083 scroll-bar - scroll-bar area width in pixels
23084
23085 Examples:
23086
23087 Pixels corresponding to 5 inches:
23088 (5 . in)
23089
23090 Total width of non-text areas on left side of window (if scroll-bar is on left):
23091 '(space :width (+ left-fringe left-margin scroll-bar))
23092
23093 Align to first text column (in header line):
23094 '(space :align-to 0)
23095
23096 Align to middle of text area minus half the width of variable `my-image'
23097 containing a loaded image:
23098 '(space :align-to (0.5 . (- text my-image)))
23099
23100 Width of left margin minus width of 1 character in the default font:
23101 '(space :width (- left-margin 1))
23102
23103 Width of left margin minus width of 2 characters in the current font:
23104 '(space :width (- left-margin (2 . width)))
23105
23106 Center 1 character over left-margin (in header line):
23107 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23108
23109 Different ways to express width of left fringe plus left margin minus one pixel:
23110 '(space :width (- (+ left-fringe left-margin) (1)))
23111 '(space :width (+ left-fringe left-margin (- (1))))
23112 '(space :width (+ left-fringe left-margin (-1)))
23113
23114 */
23115
23116 static int
23117 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23118 struct font *font, int width_p, int *align_to)
23119 {
23120 double pixels;
23121
23122 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23123 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23124
23125 if (NILP (prop))
23126 return OK_PIXELS (0);
23127
23128 eassert (FRAME_LIVE_P (it->f));
23129
23130 if (SYMBOLP (prop))
23131 {
23132 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23133 {
23134 char *unit = SSDATA (SYMBOL_NAME (prop));
23135
23136 if (unit[0] == 'i' && unit[1] == 'n')
23137 pixels = 1.0;
23138 else if (unit[0] == 'm' && unit[1] == 'm')
23139 pixels = 25.4;
23140 else if (unit[0] == 'c' && unit[1] == 'm')
23141 pixels = 2.54;
23142 else
23143 pixels = 0;
23144 if (pixels > 0)
23145 {
23146 double ppi = (width_p ? FRAME_RES_X (it->f)
23147 : FRAME_RES_Y (it->f));
23148
23149 if (ppi > 0)
23150 return OK_PIXELS (ppi / pixels);
23151 return 0;
23152 }
23153 }
23154
23155 #ifdef HAVE_WINDOW_SYSTEM
23156 if (EQ (prop, Qheight))
23157 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23158 if (EQ (prop, Qwidth))
23159 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23160 #else
23161 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23162 return OK_PIXELS (1);
23163 #endif
23164
23165 if (EQ (prop, Qtext))
23166 return OK_PIXELS (width_p
23167 ? window_box_width (it->w, TEXT_AREA)
23168 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23169
23170 if (align_to && *align_to < 0)
23171 {
23172 *res = 0;
23173 if (EQ (prop, Qleft))
23174 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23175 if (EQ (prop, Qright))
23176 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23177 if (EQ (prop, Qcenter))
23178 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23179 + window_box_width (it->w, TEXT_AREA) / 2);
23180 if (EQ (prop, Qleft_fringe))
23181 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23182 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23183 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23184 if (EQ (prop, Qright_fringe))
23185 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23186 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23187 : window_box_right_offset (it->w, TEXT_AREA));
23188 if (EQ (prop, Qleft_margin))
23189 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23190 if (EQ (prop, Qright_margin))
23191 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23192 if (EQ (prop, Qscroll_bar))
23193 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23194 ? 0
23195 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23196 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23197 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23198 : 0)));
23199 }
23200 else
23201 {
23202 if (EQ (prop, Qleft_fringe))
23203 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23204 if (EQ (prop, Qright_fringe))
23205 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23206 if (EQ (prop, Qleft_margin))
23207 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23208 if (EQ (prop, Qright_margin))
23209 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23210 if (EQ (prop, Qscroll_bar))
23211 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23212 }
23213
23214 prop = buffer_local_value_1 (prop, it->w->contents);
23215 if (EQ (prop, Qunbound))
23216 prop = Qnil;
23217 }
23218
23219 if (INTEGERP (prop) || FLOATP (prop))
23220 {
23221 int base_unit = (width_p
23222 ? FRAME_COLUMN_WIDTH (it->f)
23223 : FRAME_LINE_HEIGHT (it->f));
23224 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23225 }
23226
23227 if (CONSP (prop))
23228 {
23229 Lisp_Object car = XCAR (prop);
23230 Lisp_Object cdr = XCDR (prop);
23231
23232 if (SYMBOLP (car))
23233 {
23234 #ifdef HAVE_WINDOW_SYSTEM
23235 if (FRAME_WINDOW_P (it->f)
23236 && valid_image_p (prop))
23237 {
23238 ptrdiff_t id = lookup_image (it->f, prop);
23239 struct image *img = IMAGE_FROM_ID (it->f, id);
23240
23241 return OK_PIXELS (width_p ? img->width : img->height);
23242 }
23243 #endif
23244 if (EQ (car, Qplus) || EQ (car, Qminus))
23245 {
23246 int first = 1;
23247 double px;
23248
23249 pixels = 0;
23250 while (CONSP (cdr))
23251 {
23252 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23253 font, width_p, align_to))
23254 return 0;
23255 if (first)
23256 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23257 else
23258 pixels += px;
23259 cdr = XCDR (cdr);
23260 }
23261 if (EQ (car, Qminus))
23262 pixels = -pixels;
23263 return OK_PIXELS (pixels);
23264 }
23265
23266 car = buffer_local_value_1 (car, it->w->contents);
23267 if (EQ (car, Qunbound))
23268 car = Qnil;
23269 }
23270
23271 if (INTEGERP (car) || FLOATP (car))
23272 {
23273 double fact;
23274 pixels = XFLOATINT (car);
23275 if (NILP (cdr))
23276 return OK_PIXELS (pixels);
23277 if (calc_pixel_width_or_height (&fact, it, cdr,
23278 font, width_p, align_to))
23279 return OK_PIXELS (pixels * fact);
23280 return 0;
23281 }
23282
23283 return 0;
23284 }
23285
23286 return 0;
23287 }
23288
23289 \f
23290 /***********************************************************************
23291 Glyph Display
23292 ***********************************************************************/
23293
23294 #ifdef HAVE_WINDOW_SYSTEM
23295
23296 #ifdef GLYPH_DEBUG
23297
23298 void
23299 dump_glyph_string (struct glyph_string *s)
23300 {
23301 fprintf (stderr, "glyph string\n");
23302 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23303 s->x, s->y, s->width, s->height);
23304 fprintf (stderr, " ybase = %d\n", s->ybase);
23305 fprintf (stderr, " hl = %d\n", s->hl);
23306 fprintf (stderr, " left overhang = %d, right = %d\n",
23307 s->left_overhang, s->right_overhang);
23308 fprintf (stderr, " nchars = %d\n", s->nchars);
23309 fprintf (stderr, " extends to end of line = %d\n",
23310 s->extends_to_end_of_line_p);
23311 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23312 fprintf (stderr, " bg width = %d\n", s->background_width);
23313 }
23314
23315 #endif /* GLYPH_DEBUG */
23316
23317 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23318 of XChar2b structures for S; it can't be allocated in
23319 init_glyph_string because it must be allocated via `alloca'. W
23320 is the window on which S is drawn. ROW and AREA are the glyph row
23321 and area within the row from which S is constructed. START is the
23322 index of the first glyph structure covered by S. HL is a
23323 face-override for drawing S. */
23324
23325 #ifdef HAVE_NTGUI
23326 #define OPTIONAL_HDC(hdc) HDC hdc,
23327 #define DECLARE_HDC(hdc) HDC hdc;
23328 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23329 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23330 #endif
23331
23332 #ifndef OPTIONAL_HDC
23333 #define OPTIONAL_HDC(hdc)
23334 #define DECLARE_HDC(hdc)
23335 #define ALLOCATE_HDC(hdc, f)
23336 #define RELEASE_HDC(hdc, f)
23337 #endif
23338
23339 static void
23340 init_glyph_string (struct glyph_string *s,
23341 OPTIONAL_HDC (hdc)
23342 XChar2b *char2b, struct window *w, struct glyph_row *row,
23343 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23344 {
23345 memset (s, 0, sizeof *s);
23346 s->w = w;
23347 s->f = XFRAME (w->frame);
23348 #ifdef HAVE_NTGUI
23349 s->hdc = hdc;
23350 #endif
23351 s->display = FRAME_X_DISPLAY (s->f);
23352 s->window = FRAME_X_WINDOW (s->f);
23353 s->char2b = char2b;
23354 s->hl = hl;
23355 s->row = row;
23356 s->area = area;
23357 s->first_glyph = row->glyphs[area] + start;
23358 s->height = row->height;
23359 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23360 s->ybase = s->y + row->ascent;
23361 }
23362
23363
23364 /* Append the list of glyph strings with head H and tail T to the list
23365 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23366
23367 static void
23368 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23369 struct glyph_string *h, struct glyph_string *t)
23370 {
23371 if (h)
23372 {
23373 if (*head)
23374 (*tail)->next = h;
23375 else
23376 *head = h;
23377 h->prev = *tail;
23378 *tail = t;
23379 }
23380 }
23381
23382
23383 /* Prepend the list of glyph strings with head H and tail T to the
23384 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23385 result. */
23386
23387 static void
23388 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23389 struct glyph_string *h, struct glyph_string *t)
23390 {
23391 if (h)
23392 {
23393 if (*head)
23394 (*head)->prev = t;
23395 else
23396 *tail = t;
23397 t->next = *head;
23398 *head = h;
23399 }
23400 }
23401
23402
23403 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23404 Set *HEAD and *TAIL to the resulting list. */
23405
23406 static void
23407 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23408 struct glyph_string *s)
23409 {
23410 s->next = s->prev = NULL;
23411 append_glyph_string_lists (head, tail, s, s);
23412 }
23413
23414
23415 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23416 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23417 make sure that X resources for the face returned are allocated.
23418 Value is a pointer to a realized face that is ready for display if
23419 DISPLAY_P is non-zero. */
23420
23421 static struct face *
23422 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23423 XChar2b *char2b, int display_p)
23424 {
23425 struct face *face = FACE_FROM_ID (f, face_id);
23426 unsigned code = 0;
23427
23428 if (face->font)
23429 {
23430 code = face->font->driver->encode_char (face->font, c);
23431
23432 if (code == FONT_INVALID_CODE)
23433 code = 0;
23434 }
23435 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23436
23437 /* Make sure X resources of the face are allocated. */
23438 #ifdef HAVE_X_WINDOWS
23439 if (display_p)
23440 #endif
23441 {
23442 eassert (face != NULL);
23443 PREPARE_FACE_FOR_DISPLAY (f, face);
23444 }
23445
23446 return face;
23447 }
23448
23449
23450 /* Get face and two-byte form of character glyph GLYPH on frame F.
23451 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23452 a pointer to a realized face that is ready for display. */
23453
23454 static struct face *
23455 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23456 XChar2b *char2b, int *two_byte_p)
23457 {
23458 struct face *face;
23459 unsigned code = 0;
23460
23461 eassert (glyph->type == CHAR_GLYPH);
23462 face = FACE_FROM_ID (f, glyph->face_id);
23463
23464 /* Make sure X resources of the face are allocated. */
23465 eassert (face != NULL);
23466 PREPARE_FACE_FOR_DISPLAY (f, face);
23467
23468 if (two_byte_p)
23469 *two_byte_p = 0;
23470
23471 if (face->font)
23472 {
23473 if (CHAR_BYTE8_P (glyph->u.ch))
23474 code = CHAR_TO_BYTE8 (glyph->u.ch);
23475 else
23476 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23477
23478 if (code == FONT_INVALID_CODE)
23479 code = 0;
23480 }
23481
23482 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23483 return face;
23484 }
23485
23486
23487 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23488 Return 1 if FONT has a glyph for C, otherwise return 0. */
23489
23490 static int
23491 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23492 {
23493 unsigned code;
23494
23495 if (CHAR_BYTE8_P (c))
23496 code = CHAR_TO_BYTE8 (c);
23497 else
23498 code = font->driver->encode_char (font, c);
23499
23500 if (code == FONT_INVALID_CODE)
23501 return 0;
23502 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23503 return 1;
23504 }
23505
23506
23507 /* Fill glyph string S with composition components specified by S->cmp.
23508
23509 BASE_FACE is the base face of the composition.
23510 S->cmp_from is the index of the first component for S.
23511
23512 OVERLAPS non-zero means S should draw the foreground only, and use
23513 its physical height for clipping. See also draw_glyphs.
23514
23515 Value is the index of a component not in S. */
23516
23517 static int
23518 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23519 int overlaps)
23520 {
23521 int i;
23522 /* For all glyphs of this composition, starting at the offset
23523 S->cmp_from, until we reach the end of the definition or encounter a
23524 glyph that requires the different face, add it to S. */
23525 struct face *face;
23526
23527 eassert (s);
23528
23529 s->for_overlaps = overlaps;
23530 s->face = NULL;
23531 s->font = NULL;
23532 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23533 {
23534 int c = COMPOSITION_GLYPH (s->cmp, i);
23535
23536 /* TAB in a composition means display glyphs with padding space
23537 on the left or right. */
23538 if (c != '\t')
23539 {
23540 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23541 -1, Qnil);
23542
23543 face = get_char_face_and_encoding (s->f, c, face_id,
23544 s->char2b + i, 1);
23545 if (face)
23546 {
23547 if (! s->face)
23548 {
23549 s->face = face;
23550 s->font = s->face->font;
23551 }
23552 else if (s->face != face)
23553 break;
23554 }
23555 }
23556 ++s->nchars;
23557 }
23558 s->cmp_to = i;
23559
23560 if (s->face == NULL)
23561 {
23562 s->face = base_face->ascii_face;
23563 s->font = s->face->font;
23564 }
23565
23566 /* All glyph strings for the same composition has the same width,
23567 i.e. the width set for the first component of the composition. */
23568 s->width = s->first_glyph->pixel_width;
23569
23570 /* If the specified font could not be loaded, use the frame's
23571 default font, but record the fact that we couldn't load it in
23572 the glyph string so that we can draw rectangles for the
23573 characters of the glyph string. */
23574 if (s->font == NULL)
23575 {
23576 s->font_not_found_p = 1;
23577 s->font = FRAME_FONT (s->f);
23578 }
23579
23580 /* Adjust base line for subscript/superscript text. */
23581 s->ybase += s->first_glyph->voffset;
23582
23583 /* This glyph string must always be drawn with 16-bit functions. */
23584 s->two_byte_p = 1;
23585
23586 return s->cmp_to;
23587 }
23588
23589 static int
23590 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23591 int start, int end, int overlaps)
23592 {
23593 struct glyph *glyph, *last;
23594 Lisp_Object lgstring;
23595 int i;
23596
23597 s->for_overlaps = overlaps;
23598 glyph = s->row->glyphs[s->area] + start;
23599 last = s->row->glyphs[s->area] + end;
23600 s->cmp_id = glyph->u.cmp.id;
23601 s->cmp_from = glyph->slice.cmp.from;
23602 s->cmp_to = glyph->slice.cmp.to + 1;
23603 s->face = FACE_FROM_ID (s->f, face_id);
23604 lgstring = composition_gstring_from_id (s->cmp_id);
23605 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23606 glyph++;
23607 while (glyph < last
23608 && glyph->u.cmp.automatic
23609 && glyph->u.cmp.id == s->cmp_id
23610 && s->cmp_to == glyph->slice.cmp.from)
23611 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23612
23613 for (i = s->cmp_from; i < s->cmp_to; i++)
23614 {
23615 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23616 unsigned code = LGLYPH_CODE (lglyph);
23617
23618 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23619 }
23620 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23621 return glyph - s->row->glyphs[s->area];
23622 }
23623
23624
23625 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23626 See the comment of fill_glyph_string for arguments.
23627 Value is the index of the first glyph not in S. */
23628
23629
23630 static int
23631 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23632 int start, int end, int overlaps)
23633 {
23634 struct glyph *glyph, *last;
23635 int voffset;
23636
23637 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23638 s->for_overlaps = overlaps;
23639 glyph = s->row->glyphs[s->area] + start;
23640 last = s->row->glyphs[s->area] + end;
23641 voffset = glyph->voffset;
23642 s->face = FACE_FROM_ID (s->f, face_id);
23643 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23644 s->nchars = 1;
23645 s->width = glyph->pixel_width;
23646 glyph++;
23647 while (glyph < last
23648 && glyph->type == GLYPHLESS_GLYPH
23649 && glyph->voffset == voffset
23650 && glyph->face_id == face_id)
23651 {
23652 s->nchars++;
23653 s->width += glyph->pixel_width;
23654 glyph++;
23655 }
23656 s->ybase += voffset;
23657 return glyph - s->row->glyphs[s->area];
23658 }
23659
23660
23661 /* Fill glyph string S from a sequence of character glyphs.
23662
23663 FACE_ID is the face id of the string. START is the index of the
23664 first glyph to consider, END is the index of the last + 1.
23665 OVERLAPS non-zero means S should draw the foreground only, and use
23666 its physical height for clipping. See also draw_glyphs.
23667
23668 Value is the index of the first glyph not in S. */
23669
23670 static int
23671 fill_glyph_string (struct glyph_string *s, int face_id,
23672 int start, int end, int overlaps)
23673 {
23674 struct glyph *glyph, *last;
23675 int voffset;
23676 int glyph_not_available_p;
23677
23678 eassert (s->f == XFRAME (s->w->frame));
23679 eassert (s->nchars == 0);
23680 eassert (start >= 0 && end > start);
23681
23682 s->for_overlaps = overlaps;
23683 glyph = s->row->glyphs[s->area] + start;
23684 last = s->row->glyphs[s->area] + end;
23685 voffset = glyph->voffset;
23686 s->padding_p = glyph->padding_p;
23687 glyph_not_available_p = glyph->glyph_not_available_p;
23688
23689 while (glyph < last
23690 && glyph->type == CHAR_GLYPH
23691 && glyph->voffset == voffset
23692 /* Same face id implies same font, nowadays. */
23693 && glyph->face_id == face_id
23694 && glyph->glyph_not_available_p == glyph_not_available_p)
23695 {
23696 int two_byte_p;
23697
23698 s->face = get_glyph_face_and_encoding (s->f, glyph,
23699 s->char2b + s->nchars,
23700 &two_byte_p);
23701 s->two_byte_p = two_byte_p;
23702 ++s->nchars;
23703 eassert (s->nchars <= end - start);
23704 s->width += glyph->pixel_width;
23705 if (glyph++->padding_p != s->padding_p)
23706 break;
23707 }
23708
23709 s->font = s->face->font;
23710
23711 /* If the specified font could not be loaded, use the frame's font,
23712 but record the fact that we couldn't load it in
23713 S->font_not_found_p so that we can draw rectangles for the
23714 characters of the glyph string. */
23715 if (s->font == NULL || glyph_not_available_p)
23716 {
23717 s->font_not_found_p = 1;
23718 s->font = FRAME_FONT (s->f);
23719 }
23720
23721 /* Adjust base line for subscript/superscript text. */
23722 s->ybase += voffset;
23723
23724 eassert (s->face && s->face->gc);
23725 return glyph - s->row->glyphs[s->area];
23726 }
23727
23728
23729 /* Fill glyph string S from image glyph S->first_glyph. */
23730
23731 static void
23732 fill_image_glyph_string (struct glyph_string *s)
23733 {
23734 eassert (s->first_glyph->type == IMAGE_GLYPH);
23735 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23736 eassert (s->img);
23737 s->slice = s->first_glyph->slice.img;
23738 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23739 s->font = s->face->font;
23740 s->width = s->first_glyph->pixel_width;
23741
23742 /* Adjust base line for subscript/superscript text. */
23743 s->ybase += s->first_glyph->voffset;
23744 }
23745
23746
23747 /* Fill glyph string S from a sequence of stretch glyphs.
23748
23749 START is the index of the first glyph to consider,
23750 END is the index of the last + 1.
23751
23752 Value is the index of the first glyph not in S. */
23753
23754 static int
23755 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23756 {
23757 struct glyph *glyph, *last;
23758 int voffset, face_id;
23759
23760 eassert (s->first_glyph->type == STRETCH_GLYPH);
23761
23762 glyph = s->row->glyphs[s->area] + start;
23763 last = s->row->glyphs[s->area] + end;
23764 face_id = glyph->face_id;
23765 s->face = FACE_FROM_ID (s->f, face_id);
23766 s->font = s->face->font;
23767 s->width = glyph->pixel_width;
23768 s->nchars = 1;
23769 voffset = glyph->voffset;
23770
23771 for (++glyph;
23772 (glyph < last
23773 && glyph->type == STRETCH_GLYPH
23774 && glyph->voffset == voffset
23775 && glyph->face_id == face_id);
23776 ++glyph)
23777 s->width += glyph->pixel_width;
23778
23779 /* Adjust base line for subscript/superscript text. */
23780 s->ybase += voffset;
23781
23782 /* The case that face->gc == 0 is handled when drawing the glyph
23783 string by calling PREPARE_FACE_FOR_DISPLAY. */
23784 eassert (s->face);
23785 return glyph - s->row->glyphs[s->area];
23786 }
23787
23788 static struct font_metrics *
23789 get_per_char_metric (struct font *font, XChar2b *char2b)
23790 {
23791 static struct font_metrics metrics;
23792 unsigned code;
23793
23794 if (! font)
23795 return NULL;
23796 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23797 if (code == FONT_INVALID_CODE)
23798 return NULL;
23799 font->driver->text_extents (font, &code, 1, &metrics);
23800 return &metrics;
23801 }
23802
23803 /* EXPORT for RIF:
23804 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23805 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23806 assumed to be zero. */
23807
23808 void
23809 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23810 {
23811 *left = *right = 0;
23812
23813 if (glyph->type == CHAR_GLYPH)
23814 {
23815 struct face *face;
23816 XChar2b char2b;
23817 struct font_metrics *pcm;
23818
23819 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23820 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23821 {
23822 if (pcm->rbearing > pcm->width)
23823 *right = pcm->rbearing - pcm->width;
23824 if (pcm->lbearing < 0)
23825 *left = -pcm->lbearing;
23826 }
23827 }
23828 else if (glyph->type == COMPOSITE_GLYPH)
23829 {
23830 if (! glyph->u.cmp.automatic)
23831 {
23832 struct composition *cmp = composition_table[glyph->u.cmp.id];
23833
23834 if (cmp->rbearing > cmp->pixel_width)
23835 *right = cmp->rbearing - cmp->pixel_width;
23836 if (cmp->lbearing < 0)
23837 *left = - cmp->lbearing;
23838 }
23839 else
23840 {
23841 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23842 struct font_metrics metrics;
23843
23844 composition_gstring_width (gstring, glyph->slice.cmp.from,
23845 glyph->slice.cmp.to + 1, &metrics);
23846 if (metrics.rbearing > metrics.width)
23847 *right = metrics.rbearing - metrics.width;
23848 if (metrics.lbearing < 0)
23849 *left = - metrics.lbearing;
23850 }
23851 }
23852 }
23853
23854
23855 /* Return the index of the first glyph preceding glyph string S that
23856 is overwritten by S because of S's left overhang. Value is -1
23857 if no glyphs are overwritten. */
23858
23859 static int
23860 left_overwritten (struct glyph_string *s)
23861 {
23862 int k;
23863
23864 if (s->left_overhang)
23865 {
23866 int x = 0, i;
23867 struct glyph *glyphs = s->row->glyphs[s->area];
23868 int first = s->first_glyph - glyphs;
23869
23870 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23871 x -= glyphs[i].pixel_width;
23872
23873 k = i + 1;
23874 }
23875 else
23876 k = -1;
23877
23878 return k;
23879 }
23880
23881
23882 /* Return the index of the first glyph preceding glyph string S that
23883 is overwriting S because of its right overhang. Value is -1 if no
23884 glyph in front of S overwrites S. */
23885
23886 static int
23887 left_overwriting (struct glyph_string *s)
23888 {
23889 int i, k, x;
23890 struct glyph *glyphs = s->row->glyphs[s->area];
23891 int first = s->first_glyph - glyphs;
23892
23893 k = -1;
23894 x = 0;
23895 for (i = first - 1; i >= 0; --i)
23896 {
23897 int left, right;
23898 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23899 if (x + right > 0)
23900 k = i;
23901 x -= glyphs[i].pixel_width;
23902 }
23903
23904 return k;
23905 }
23906
23907
23908 /* Return the index of the last glyph following glyph string S that is
23909 overwritten by S because of S's right overhang. Value is -1 if
23910 no such glyph is found. */
23911
23912 static int
23913 right_overwritten (struct glyph_string *s)
23914 {
23915 int k = -1;
23916
23917 if (s->right_overhang)
23918 {
23919 int x = 0, i;
23920 struct glyph *glyphs = s->row->glyphs[s->area];
23921 int first = (s->first_glyph - glyphs
23922 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23923 int end = s->row->used[s->area];
23924
23925 for (i = first; i < end && s->right_overhang > x; ++i)
23926 x += glyphs[i].pixel_width;
23927
23928 k = i;
23929 }
23930
23931 return k;
23932 }
23933
23934
23935 /* Return the index of the last glyph following glyph string S that
23936 overwrites S because of its left overhang. Value is negative
23937 if no such glyph is found. */
23938
23939 static int
23940 right_overwriting (struct glyph_string *s)
23941 {
23942 int i, k, x;
23943 int end = s->row->used[s->area];
23944 struct glyph *glyphs = s->row->glyphs[s->area];
23945 int first = (s->first_glyph - glyphs
23946 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23947
23948 k = -1;
23949 x = 0;
23950 for (i = first; i < end; ++i)
23951 {
23952 int left, right;
23953 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23954 if (x - left < 0)
23955 k = i;
23956 x += glyphs[i].pixel_width;
23957 }
23958
23959 return k;
23960 }
23961
23962
23963 /* Set background width of glyph string S. START is the index of the
23964 first glyph following S. LAST_X is the right-most x-position + 1
23965 in the drawing area. */
23966
23967 static void
23968 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23969 {
23970 /* If the face of this glyph string has to be drawn to the end of
23971 the drawing area, set S->extends_to_end_of_line_p. */
23972
23973 if (start == s->row->used[s->area]
23974 && ((s->row->fill_line_p
23975 && (s->hl == DRAW_NORMAL_TEXT
23976 || s->hl == DRAW_IMAGE_RAISED
23977 || s->hl == DRAW_IMAGE_SUNKEN))
23978 || s->hl == DRAW_MOUSE_FACE))
23979 s->extends_to_end_of_line_p = 1;
23980
23981 /* If S extends its face to the end of the line, set its
23982 background_width to the distance to the right edge of the drawing
23983 area. */
23984 if (s->extends_to_end_of_line_p)
23985 s->background_width = last_x - s->x + 1;
23986 else
23987 s->background_width = s->width;
23988 }
23989
23990
23991 /* Compute overhangs and x-positions for glyph string S and its
23992 predecessors, or successors. X is the starting x-position for S.
23993 BACKWARD_P non-zero means process predecessors. */
23994
23995 static void
23996 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23997 {
23998 if (backward_p)
23999 {
24000 while (s)
24001 {
24002 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24003 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24004 x -= s->width;
24005 s->x = x;
24006 s = s->prev;
24007 }
24008 }
24009 else
24010 {
24011 while (s)
24012 {
24013 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24014 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24015 s->x = x;
24016 x += s->width;
24017 s = s->next;
24018 }
24019 }
24020 }
24021
24022
24023
24024 /* The following macros are only called from draw_glyphs below.
24025 They reference the following parameters of that function directly:
24026 `w', `row', `area', and `overlap_p'
24027 as well as the following local variables:
24028 `s', `f', and `hdc' (in W32) */
24029
24030 #ifdef HAVE_NTGUI
24031 /* On W32, silently add local `hdc' variable to argument list of
24032 init_glyph_string. */
24033 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24034 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24035 #else
24036 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24037 init_glyph_string (s, char2b, w, row, area, start, hl)
24038 #endif
24039
24040 /* Add a glyph string for a stretch glyph to the list of strings
24041 between HEAD and TAIL. START is the index of the stretch glyph in
24042 row area AREA of glyph row ROW. END is the index of the last glyph
24043 in that glyph row area. X is the current output position assigned
24044 to the new glyph string constructed. HL overrides that face of the
24045 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24046 is the right-most x-position of the drawing area. */
24047
24048 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24049 and below -- keep them on one line. */
24050 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24051 do \
24052 { \
24053 s = alloca (sizeof *s); \
24054 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24055 START = fill_stretch_glyph_string (s, START, END); \
24056 append_glyph_string (&HEAD, &TAIL, s); \
24057 s->x = (X); \
24058 } \
24059 while (0)
24060
24061
24062 /* Add a glyph string for an image glyph to the list of strings
24063 between HEAD and TAIL. START is the index of the image glyph in
24064 row area AREA of glyph row ROW. END is the index of the last glyph
24065 in that glyph row area. X is the current output position assigned
24066 to the new glyph string constructed. HL overrides that face of the
24067 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24068 is the right-most x-position of the drawing area. */
24069
24070 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24071 do \
24072 { \
24073 s = alloca (sizeof *s); \
24074 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24075 fill_image_glyph_string (s); \
24076 append_glyph_string (&HEAD, &TAIL, s); \
24077 ++START; \
24078 s->x = (X); \
24079 } \
24080 while (0)
24081
24082
24083 /* Add a glyph string for a sequence of character glyphs to the list
24084 of strings between HEAD and TAIL. START is the index of the first
24085 glyph in row area AREA of glyph row ROW that is part of the new
24086 glyph string. END is the index of the last glyph in that glyph row
24087 area. X is the current output position assigned to the new glyph
24088 string constructed. HL overrides that face of the glyph; e.g. it
24089 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24090 right-most x-position of the drawing area. */
24091
24092 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24093 do \
24094 { \
24095 int face_id; \
24096 XChar2b *char2b; \
24097 \
24098 face_id = (row)->glyphs[area][START].face_id; \
24099 \
24100 s = alloca (sizeof *s); \
24101 char2b = alloca ((END - START) * sizeof *char2b); \
24102 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24103 append_glyph_string (&HEAD, &TAIL, s); \
24104 s->x = (X); \
24105 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24106 } \
24107 while (0)
24108
24109
24110 /* Add a glyph string for a composite sequence to the list of strings
24111 between HEAD and TAIL. START is the index of the first glyph in
24112 row area AREA of glyph row ROW that is part of the new glyph
24113 string. END is the index of the last glyph in that glyph row area.
24114 X is the current output position assigned to the new glyph string
24115 constructed. HL overrides that face of the glyph; e.g. it is
24116 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24117 x-position of the drawing area. */
24118
24119 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24120 do { \
24121 int face_id = (row)->glyphs[area][START].face_id; \
24122 struct face *base_face = FACE_FROM_ID (f, face_id); \
24123 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24124 struct composition *cmp = composition_table[cmp_id]; \
24125 XChar2b *char2b; \
24126 struct glyph_string *first_s = NULL; \
24127 int n; \
24128 \
24129 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24130 \
24131 /* Make glyph_strings for each glyph sequence that is drawable by \
24132 the same face, and append them to HEAD/TAIL. */ \
24133 for (n = 0; n < cmp->glyph_len;) \
24134 { \
24135 s = alloca (sizeof *s); \
24136 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24137 append_glyph_string (&(HEAD), &(TAIL), s); \
24138 s->cmp = cmp; \
24139 s->cmp_from = n; \
24140 s->x = (X); \
24141 if (n == 0) \
24142 first_s = s; \
24143 n = fill_composite_glyph_string (s, base_face, overlaps); \
24144 } \
24145 \
24146 ++START; \
24147 s = first_s; \
24148 } while (0)
24149
24150
24151 /* Add a glyph string for a glyph-string sequence to the list of strings
24152 between HEAD and TAIL. */
24153
24154 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24155 do { \
24156 int face_id; \
24157 XChar2b *char2b; \
24158 Lisp_Object gstring; \
24159 \
24160 face_id = (row)->glyphs[area][START].face_id; \
24161 gstring = (composition_gstring_from_id \
24162 ((row)->glyphs[area][START].u.cmp.id)); \
24163 s = alloca (sizeof *s); \
24164 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24165 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24166 append_glyph_string (&(HEAD), &(TAIL), s); \
24167 s->x = (X); \
24168 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24169 } while (0)
24170
24171
24172 /* Add a glyph string for a sequence of glyphless character's glyphs
24173 to the list of strings between HEAD and TAIL. The meanings of
24174 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24175
24176 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24177 do \
24178 { \
24179 int face_id; \
24180 \
24181 face_id = (row)->glyphs[area][START].face_id; \
24182 \
24183 s = alloca (sizeof *s); \
24184 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24185 append_glyph_string (&HEAD, &TAIL, s); \
24186 s->x = (X); \
24187 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24188 overlaps); \
24189 } \
24190 while (0)
24191
24192
24193 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24194 of AREA of glyph row ROW on window W between indices START and END.
24195 HL overrides the face for drawing glyph strings, e.g. it is
24196 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24197 x-positions of the drawing area.
24198
24199 This is an ugly monster macro construct because we must use alloca
24200 to allocate glyph strings (because draw_glyphs can be called
24201 asynchronously). */
24202
24203 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24204 do \
24205 { \
24206 HEAD = TAIL = NULL; \
24207 while (START < END) \
24208 { \
24209 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24210 switch (first_glyph->type) \
24211 { \
24212 case CHAR_GLYPH: \
24213 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24214 HL, X, LAST_X); \
24215 break; \
24216 \
24217 case COMPOSITE_GLYPH: \
24218 if (first_glyph->u.cmp.automatic) \
24219 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24220 HL, X, LAST_X); \
24221 else \
24222 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24223 HL, X, LAST_X); \
24224 break; \
24225 \
24226 case STRETCH_GLYPH: \
24227 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24228 HL, X, LAST_X); \
24229 break; \
24230 \
24231 case IMAGE_GLYPH: \
24232 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24233 HL, X, LAST_X); \
24234 break; \
24235 \
24236 case GLYPHLESS_GLYPH: \
24237 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24238 HL, X, LAST_X); \
24239 break; \
24240 \
24241 default: \
24242 emacs_abort (); \
24243 } \
24244 \
24245 if (s) \
24246 { \
24247 set_glyph_string_background_width (s, START, LAST_X); \
24248 (X) += s->width; \
24249 } \
24250 } \
24251 } while (0)
24252
24253
24254 /* Draw glyphs between START and END in AREA of ROW on window W,
24255 starting at x-position X. X is relative to AREA in W. HL is a
24256 face-override with the following meaning:
24257
24258 DRAW_NORMAL_TEXT draw normally
24259 DRAW_CURSOR draw in cursor face
24260 DRAW_MOUSE_FACE draw in mouse face.
24261 DRAW_INVERSE_VIDEO draw in mode line face
24262 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24263 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24264
24265 If OVERLAPS is non-zero, draw only the foreground of characters and
24266 clip to the physical height of ROW. Non-zero value also defines
24267 the overlapping part to be drawn:
24268
24269 OVERLAPS_PRED overlap with preceding rows
24270 OVERLAPS_SUCC overlap with succeeding rows
24271 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24272 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24273
24274 Value is the x-position reached, relative to AREA of W. */
24275
24276 static int
24277 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24278 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24279 enum draw_glyphs_face hl, int overlaps)
24280 {
24281 struct glyph_string *head, *tail;
24282 struct glyph_string *s;
24283 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24284 int i, j, x_reached, last_x, area_left = 0;
24285 struct frame *f = XFRAME (WINDOW_FRAME (w));
24286 DECLARE_HDC (hdc);
24287
24288 ALLOCATE_HDC (hdc, f);
24289
24290 /* Let's rather be paranoid than getting a SEGV. */
24291 end = min (end, row->used[area]);
24292 start = clip_to_bounds (0, start, end);
24293
24294 /* Translate X to frame coordinates. Set last_x to the right
24295 end of the drawing area. */
24296 if (row->full_width_p)
24297 {
24298 /* X is relative to the left edge of W, without scroll bars
24299 or fringes. */
24300 area_left = WINDOW_LEFT_EDGE_X (w);
24301 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24302 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24303 }
24304 else
24305 {
24306 area_left = window_box_left (w, area);
24307 last_x = area_left + window_box_width (w, area);
24308 }
24309 x += area_left;
24310
24311 /* Build a doubly-linked list of glyph_string structures between
24312 head and tail from what we have to draw. Note that the macro
24313 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24314 the reason we use a separate variable `i'. */
24315 i = start;
24316 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24317 if (tail)
24318 x_reached = tail->x + tail->background_width;
24319 else
24320 x_reached = x;
24321
24322 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24323 the row, redraw some glyphs in front or following the glyph
24324 strings built above. */
24325 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24326 {
24327 struct glyph_string *h, *t;
24328 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24329 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24330 int check_mouse_face = 0;
24331 int dummy_x = 0;
24332
24333 /* If mouse highlighting is on, we may need to draw adjacent
24334 glyphs using mouse-face highlighting. */
24335 if (area == TEXT_AREA && row->mouse_face_p
24336 && hlinfo->mouse_face_beg_row >= 0
24337 && hlinfo->mouse_face_end_row >= 0)
24338 {
24339 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24340
24341 if (row_vpos >= hlinfo->mouse_face_beg_row
24342 && row_vpos <= hlinfo->mouse_face_end_row)
24343 {
24344 check_mouse_face = 1;
24345 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24346 ? hlinfo->mouse_face_beg_col : 0;
24347 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24348 ? hlinfo->mouse_face_end_col
24349 : row->used[TEXT_AREA];
24350 }
24351 }
24352
24353 /* Compute overhangs for all glyph strings. */
24354 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24355 for (s = head; s; s = s->next)
24356 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24357
24358 /* Prepend glyph strings for glyphs in front of the first glyph
24359 string that are overwritten because of the first glyph
24360 string's left overhang. The background of all strings
24361 prepended must be drawn because the first glyph string
24362 draws over it. */
24363 i = left_overwritten (head);
24364 if (i >= 0)
24365 {
24366 enum draw_glyphs_face overlap_hl;
24367
24368 /* If this row contains mouse highlighting, attempt to draw
24369 the overlapped glyphs with the correct highlight. This
24370 code fails if the overlap encompasses more than one glyph
24371 and mouse-highlight spans only some of these glyphs.
24372 However, making it work perfectly involves a lot more
24373 code, and I don't know if the pathological case occurs in
24374 practice, so we'll stick to this for now. --- cyd */
24375 if (check_mouse_face
24376 && mouse_beg_col < start && mouse_end_col > i)
24377 overlap_hl = DRAW_MOUSE_FACE;
24378 else
24379 overlap_hl = DRAW_NORMAL_TEXT;
24380
24381 j = i;
24382 BUILD_GLYPH_STRINGS (j, start, h, t,
24383 overlap_hl, dummy_x, last_x);
24384 start = i;
24385 compute_overhangs_and_x (t, head->x, 1);
24386 prepend_glyph_string_lists (&head, &tail, h, t);
24387 clip_head = head;
24388 }
24389
24390 /* Prepend glyph strings for glyphs in front of the first glyph
24391 string that overwrite that glyph string because of their
24392 right overhang. For these strings, only the foreground must
24393 be drawn, because it draws over the glyph string at `head'.
24394 The background must not be drawn because this would overwrite
24395 right overhangs of preceding glyphs for which no glyph
24396 strings exist. */
24397 i = left_overwriting (head);
24398 if (i >= 0)
24399 {
24400 enum draw_glyphs_face overlap_hl;
24401
24402 if (check_mouse_face
24403 && mouse_beg_col < start && mouse_end_col > i)
24404 overlap_hl = DRAW_MOUSE_FACE;
24405 else
24406 overlap_hl = DRAW_NORMAL_TEXT;
24407
24408 clip_head = head;
24409 BUILD_GLYPH_STRINGS (i, start, h, t,
24410 overlap_hl, dummy_x, last_x);
24411 for (s = h; s; s = s->next)
24412 s->background_filled_p = 1;
24413 compute_overhangs_and_x (t, head->x, 1);
24414 prepend_glyph_string_lists (&head, &tail, h, t);
24415 }
24416
24417 /* Append glyphs strings for glyphs following the last glyph
24418 string tail that are overwritten by tail. The background of
24419 these strings has to be drawn because tail's foreground draws
24420 over it. */
24421 i = right_overwritten (tail);
24422 if (i >= 0)
24423 {
24424 enum draw_glyphs_face overlap_hl;
24425
24426 if (check_mouse_face
24427 && mouse_beg_col < i && mouse_end_col > end)
24428 overlap_hl = DRAW_MOUSE_FACE;
24429 else
24430 overlap_hl = DRAW_NORMAL_TEXT;
24431
24432 BUILD_GLYPH_STRINGS (end, i, h, t,
24433 overlap_hl, x, last_x);
24434 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24435 we don't have `end = i;' here. */
24436 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24437 append_glyph_string_lists (&head, &tail, h, t);
24438 clip_tail = tail;
24439 }
24440
24441 /* Append glyph strings for glyphs following the last glyph
24442 string tail that overwrite tail. The foreground of such
24443 glyphs has to be drawn because it writes into the background
24444 of tail. The background must not be drawn because it could
24445 paint over the foreground of following glyphs. */
24446 i = right_overwriting (tail);
24447 if (i >= 0)
24448 {
24449 enum draw_glyphs_face overlap_hl;
24450 if (check_mouse_face
24451 && mouse_beg_col < i && mouse_end_col > end)
24452 overlap_hl = DRAW_MOUSE_FACE;
24453 else
24454 overlap_hl = DRAW_NORMAL_TEXT;
24455
24456 clip_tail = tail;
24457 i++; /* We must include the Ith glyph. */
24458 BUILD_GLYPH_STRINGS (end, i, h, t,
24459 overlap_hl, x, last_x);
24460 for (s = h; s; s = s->next)
24461 s->background_filled_p = 1;
24462 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24463 append_glyph_string_lists (&head, &tail, h, t);
24464 }
24465 if (clip_head || clip_tail)
24466 for (s = head; s; s = s->next)
24467 {
24468 s->clip_head = clip_head;
24469 s->clip_tail = clip_tail;
24470 }
24471 }
24472
24473 /* Draw all strings. */
24474 for (s = head; s; s = s->next)
24475 FRAME_RIF (f)->draw_glyph_string (s);
24476
24477 #ifndef HAVE_NS
24478 /* When focus a sole frame and move horizontally, this sets on_p to 0
24479 causing a failure to erase prev cursor position. */
24480 if (area == TEXT_AREA
24481 && !row->full_width_p
24482 /* When drawing overlapping rows, only the glyph strings'
24483 foreground is drawn, which doesn't erase a cursor
24484 completely. */
24485 && !overlaps)
24486 {
24487 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24488 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24489 : (tail ? tail->x + tail->background_width : x));
24490 x0 -= area_left;
24491 x1 -= area_left;
24492
24493 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24494 row->y, MATRIX_ROW_BOTTOM_Y (row));
24495 }
24496 #endif
24497
24498 /* Value is the x-position up to which drawn, relative to AREA of W.
24499 This doesn't include parts drawn because of overhangs. */
24500 if (row->full_width_p)
24501 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24502 else
24503 x_reached -= area_left;
24504
24505 RELEASE_HDC (hdc, f);
24506
24507 return x_reached;
24508 }
24509
24510 /* Expand row matrix if too narrow. Don't expand if area
24511 is not present. */
24512
24513 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24514 { \
24515 if (!it->f->fonts_changed \
24516 && (it->glyph_row->glyphs[area] \
24517 < it->glyph_row->glyphs[area + 1])) \
24518 { \
24519 it->w->ncols_scale_factor++; \
24520 it->f->fonts_changed = 1; \
24521 } \
24522 }
24523
24524 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24525 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24526
24527 static void
24528 append_glyph (struct it *it)
24529 {
24530 struct glyph *glyph;
24531 enum glyph_row_area area = it->area;
24532
24533 eassert (it->glyph_row);
24534 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24535
24536 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24537 if (glyph < it->glyph_row->glyphs[area + 1])
24538 {
24539 /* If the glyph row is reversed, we need to prepend the glyph
24540 rather than append it. */
24541 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24542 {
24543 struct glyph *g;
24544
24545 /* Make room for the additional glyph. */
24546 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24547 g[1] = *g;
24548 glyph = it->glyph_row->glyphs[area];
24549 }
24550 glyph->charpos = CHARPOS (it->position);
24551 glyph->object = it->object;
24552 if (it->pixel_width > 0)
24553 {
24554 glyph->pixel_width = it->pixel_width;
24555 glyph->padding_p = 0;
24556 }
24557 else
24558 {
24559 /* Assure at least 1-pixel width. Otherwise, cursor can't
24560 be displayed correctly. */
24561 glyph->pixel_width = 1;
24562 glyph->padding_p = 1;
24563 }
24564 glyph->ascent = it->ascent;
24565 glyph->descent = it->descent;
24566 glyph->voffset = it->voffset;
24567 glyph->type = CHAR_GLYPH;
24568 glyph->avoid_cursor_p = it->avoid_cursor_p;
24569 glyph->multibyte_p = it->multibyte_p;
24570 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24571 {
24572 /* In R2L rows, the left and the right box edges need to be
24573 drawn in reverse direction. */
24574 glyph->right_box_line_p = it->start_of_box_run_p;
24575 glyph->left_box_line_p = it->end_of_box_run_p;
24576 }
24577 else
24578 {
24579 glyph->left_box_line_p = it->start_of_box_run_p;
24580 glyph->right_box_line_p = it->end_of_box_run_p;
24581 }
24582 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24583 || it->phys_descent > it->descent);
24584 glyph->glyph_not_available_p = it->glyph_not_available_p;
24585 glyph->face_id = it->face_id;
24586 glyph->u.ch = it->char_to_display;
24587 glyph->slice.img = null_glyph_slice;
24588 glyph->font_type = FONT_TYPE_UNKNOWN;
24589 if (it->bidi_p)
24590 {
24591 glyph->resolved_level = it->bidi_it.resolved_level;
24592 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24593 emacs_abort ();
24594 glyph->bidi_type = it->bidi_it.type;
24595 }
24596 else
24597 {
24598 glyph->resolved_level = 0;
24599 glyph->bidi_type = UNKNOWN_BT;
24600 }
24601 ++it->glyph_row->used[area];
24602 }
24603 else
24604 IT_EXPAND_MATRIX_WIDTH (it, area);
24605 }
24606
24607 /* Store one glyph for the composition IT->cmp_it.id in
24608 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24609 non-null. */
24610
24611 static void
24612 append_composite_glyph (struct it *it)
24613 {
24614 struct glyph *glyph;
24615 enum glyph_row_area area = it->area;
24616
24617 eassert (it->glyph_row);
24618
24619 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24620 if (glyph < it->glyph_row->glyphs[area + 1])
24621 {
24622 /* If the glyph row is reversed, we need to prepend the glyph
24623 rather than append it. */
24624 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24625 {
24626 struct glyph *g;
24627
24628 /* Make room for the new glyph. */
24629 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24630 g[1] = *g;
24631 glyph = it->glyph_row->glyphs[it->area];
24632 }
24633 glyph->charpos = it->cmp_it.charpos;
24634 glyph->object = it->object;
24635 glyph->pixel_width = it->pixel_width;
24636 glyph->ascent = it->ascent;
24637 glyph->descent = it->descent;
24638 glyph->voffset = it->voffset;
24639 glyph->type = COMPOSITE_GLYPH;
24640 if (it->cmp_it.ch < 0)
24641 {
24642 glyph->u.cmp.automatic = 0;
24643 glyph->u.cmp.id = it->cmp_it.id;
24644 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24645 }
24646 else
24647 {
24648 glyph->u.cmp.automatic = 1;
24649 glyph->u.cmp.id = it->cmp_it.id;
24650 glyph->slice.cmp.from = it->cmp_it.from;
24651 glyph->slice.cmp.to = it->cmp_it.to - 1;
24652 }
24653 glyph->avoid_cursor_p = it->avoid_cursor_p;
24654 glyph->multibyte_p = it->multibyte_p;
24655 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24656 {
24657 /* In R2L rows, the left and the right box edges need to be
24658 drawn in reverse direction. */
24659 glyph->right_box_line_p = it->start_of_box_run_p;
24660 glyph->left_box_line_p = it->end_of_box_run_p;
24661 }
24662 else
24663 {
24664 glyph->left_box_line_p = it->start_of_box_run_p;
24665 glyph->right_box_line_p = it->end_of_box_run_p;
24666 }
24667 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24668 || it->phys_descent > it->descent);
24669 glyph->padding_p = 0;
24670 glyph->glyph_not_available_p = 0;
24671 glyph->face_id = it->face_id;
24672 glyph->font_type = FONT_TYPE_UNKNOWN;
24673 if (it->bidi_p)
24674 {
24675 glyph->resolved_level = it->bidi_it.resolved_level;
24676 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24677 emacs_abort ();
24678 glyph->bidi_type = it->bidi_it.type;
24679 }
24680 ++it->glyph_row->used[area];
24681 }
24682 else
24683 IT_EXPAND_MATRIX_WIDTH (it, area);
24684 }
24685
24686
24687 /* Change IT->ascent and IT->height according to the setting of
24688 IT->voffset. */
24689
24690 static void
24691 take_vertical_position_into_account (struct it *it)
24692 {
24693 if (it->voffset)
24694 {
24695 if (it->voffset < 0)
24696 /* Increase the ascent so that we can display the text higher
24697 in the line. */
24698 it->ascent -= it->voffset;
24699 else
24700 /* Increase the descent so that we can display the text lower
24701 in the line. */
24702 it->descent += it->voffset;
24703 }
24704 }
24705
24706
24707 /* Produce glyphs/get display metrics for the image IT is loaded with.
24708 See the description of struct display_iterator in dispextern.h for
24709 an overview of struct display_iterator. */
24710
24711 static void
24712 produce_image_glyph (struct it *it)
24713 {
24714 struct image *img;
24715 struct face *face;
24716 int glyph_ascent, crop;
24717 struct glyph_slice slice;
24718
24719 eassert (it->what == IT_IMAGE);
24720
24721 face = FACE_FROM_ID (it->f, it->face_id);
24722 eassert (face);
24723 /* Make sure X resources of the face is loaded. */
24724 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24725
24726 if (it->image_id < 0)
24727 {
24728 /* Fringe bitmap. */
24729 it->ascent = it->phys_ascent = 0;
24730 it->descent = it->phys_descent = 0;
24731 it->pixel_width = 0;
24732 it->nglyphs = 0;
24733 return;
24734 }
24735
24736 img = IMAGE_FROM_ID (it->f, it->image_id);
24737 eassert (img);
24738 /* Make sure X resources of the image is loaded. */
24739 prepare_image_for_display (it->f, img);
24740
24741 slice.x = slice.y = 0;
24742 slice.width = img->width;
24743 slice.height = img->height;
24744
24745 if (INTEGERP (it->slice.x))
24746 slice.x = XINT (it->slice.x);
24747 else if (FLOATP (it->slice.x))
24748 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24749
24750 if (INTEGERP (it->slice.y))
24751 slice.y = XINT (it->slice.y);
24752 else if (FLOATP (it->slice.y))
24753 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24754
24755 if (INTEGERP (it->slice.width))
24756 slice.width = XINT (it->slice.width);
24757 else if (FLOATP (it->slice.width))
24758 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24759
24760 if (INTEGERP (it->slice.height))
24761 slice.height = XINT (it->slice.height);
24762 else if (FLOATP (it->slice.height))
24763 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24764
24765 if (slice.x >= img->width)
24766 slice.x = img->width;
24767 if (slice.y >= img->height)
24768 slice.y = img->height;
24769 if (slice.x + slice.width >= img->width)
24770 slice.width = img->width - slice.x;
24771 if (slice.y + slice.height > img->height)
24772 slice.height = img->height - slice.y;
24773
24774 if (slice.width == 0 || slice.height == 0)
24775 return;
24776
24777 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24778
24779 it->descent = slice.height - glyph_ascent;
24780 if (slice.y == 0)
24781 it->descent += img->vmargin;
24782 if (slice.y + slice.height == img->height)
24783 it->descent += img->vmargin;
24784 it->phys_descent = it->descent;
24785
24786 it->pixel_width = slice.width;
24787 if (slice.x == 0)
24788 it->pixel_width += img->hmargin;
24789 if (slice.x + slice.width == img->width)
24790 it->pixel_width += img->hmargin;
24791
24792 /* It's quite possible for images to have an ascent greater than
24793 their height, so don't get confused in that case. */
24794 if (it->descent < 0)
24795 it->descent = 0;
24796
24797 it->nglyphs = 1;
24798
24799 if (face->box != FACE_NO_BOX)
24800 {
24801 if (face->box_line_width > 0)
24802 {
24803 if (slice.y == 0)
24804 it->ascent += face->box_line_width;
24805 if (slice.y + slice.height == img->height)
24806 it->descent += face->box_line_width;
24807 }
24808
24809 if (it->start_of_box_run_p && slice.x == 0)
24810 it->pixel_width += eabs (face->box_line_width);
24811 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24812 it->pixel_width += eabs (face->box_line_width);
24813 }
24814
24815 take_vertical_position_into_account (it);
24816
24817 /* Automatically crop wide image glyphs at right edge so we can
24818 draw the cursor on same display row. */
24819 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24820 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24821 {
24822 it->pixel_width -= crop;
24823 slice.width -= crop;
24824 }
24825
24826 if (it->glyph_row)
24827 {
24828 struct glyph *glyph;
24829 enum glyph_row_area area = it->area;
24830
24831 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24832 if (glyph < it->glyph_row->glyphs[area + 1])
24833 {
24834 glyph->charpos = CHARPOS (it->position);
24835 glyph->object = it->object;
24836 glyph->pixel_width = it->pixel_width;
24837 glyph->ascent = glyph_ascent;
24838 glyph->descent = it->descent;
24839 glyph->voffset = it->voffset;
24840 glyph->type = IMAGE_GLYPH;
24841 glyph->avoid_cursor_p = it->avoid_cursor_p;
24842 glyph->multibyte_p = it->multibyte_p;
24843 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24844 {
24845 /* In R2L rows, the left and the right box edges need to be
24846 drawn in reverse direction. */
24847 glyph->right_box_line_p = it->start_of_box_run_p;
24848 glyph->left_box_line_p = it->end_of_box_run_p;
24849 }
24850 else
24851 {
24852 glyph->left_box_line_p = it->start_of_box_run_p;
24853 glyph->right_box_line_p = it->end_of_box_run_p;
24854 }
24855 glyph->overlaps_vertically_p = 0;
24856 glyph->padding_p = 0;
24857 glyph->glyph_not_available_p = 0;
24858 glyph->face_id = it->face_id;
24859 glyph->u.img_id = img->id;
24860 glyph->slice.img = slice;
24861 glyph->font_type = FONT_TYPE_UNKNOWN;
24862 if (it->bidi_p)
24863 {
24864 glyph->resolved_level = it->bidi_it.resolved_level;
24865 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24866 emacs_abort ();
24867 glyph->bidi_type = it->bidi_it.type;
24868 }
24869 ++it->glyph_row->used[area];
24870 }
24871 else
24872 IT_EXPAND_MATRIX_WIDTH (it, area);
24873 }
24874 }
24875
24876
24877 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24878 of the glyph, WIDTH and HEIGHT are the width and height of the
24879 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24880
24881 static void
24882 append_stretch_glyph (struct it *it, Lisp_Object object,
24883 int width, int height, int ascent)
24884 {
24885 struct glyph *glyph;
24886 enum glyph_row_area area = it->area;
24887
24888 eassert (ascent >= 0 && ascent <= height);
24889
24890 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24891 if (glyph < it->glyph_row->glyphs[area + 1])
24892 {
24893 /* If the glyph row is reversed, we need to prepend the glyph
24894 rather than append it. */
24895 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24896 {
24897 struct glyph *g;
24898
24899 /* Make room for the additional glyph. */
24900 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24901 g[1] = *g;
24902 glyph = it->glyph_row->glyphs[area];
24903 }
24904 glyph->charpos = CHARPOS (it->position);
24905 glyph->object = object;
24906 glyph->pixel_width = width;
24907 glyph->ascent = ascent;
24908 glyph->descent = height - ascent;
24909 glyph->voffset = it->voffset;
24910 glyph->type = STRETCH_GLYPH;
24911 glyph->avoid_cursor_p = it->avoid_cursor_p;
24912 glyph->multibyte_p = it->multibyte_p;
24913 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24914 {
24915 /* In R2L rows, the left and the right box edges need to be
24916 drawn in reverse direction. */
24917 glyph->right_box_line_p = it->start_of_box_run_p;
24918 glyph->left_box_line_p = it->end_of_box_run_p;
24919 }
24920 else
24921 {
24922 glyph->left_box_line_p = it->start_of_box_run_p;
24923 glyph->right_box_line_p = it->end_of_box_run_p;
24924 }
24925 glyph->overlaps_vertically_p = 0;
24926 glyph->padding_p = 0;
24927 glyph->glyph_not_available_p = 0;
24928 glyph->face_id = it->face_id;
24929 glyph->u.stretch.ascent = ascent;
24930 glyph->u.stretch.height = height;
24931 glyph->slice.img = null_glyph_slice;
24932 glyph->font_type = FONT_TYPE_UNKNOWN;
24933 if (it->bidi_p)
24934 {
24935 glyph->resolved_level = it->bidi_it.resolved_level;
24936 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24937 emacs_abort ();
24938 glyph->bidi_type = it->bidi_it.type;
24939 }
24940 else
24941 {
24942 glyph->resolved_level = 0;
24943 glyph->bidi_type = UNKNOWN_BT;
24944 }
24945 ++it->glyph_row->used[area];
24946 }
24947 else
24948 IT_EXPAND_MATRIX_WIDTH (it, area);
24949 }
24950
24951 #endif /* HAVE_WINDOW_SYSTEM */
24952
24953 /* Produce a stretch glyph for iterator IT. IT->object is the value
24954 of the glyph property displayed. The value must be a list
24955 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24956 being recognized:
24957
24958 1. `:width WIDTH' specifies that the space should be WIDTH *
24959 canonical char width wide. WIDTH may be an integer or floating
24960 point number.
24961
24962 2. `:relative-width FACTOR' specifies that the width of the stretch
24963 should be computed from the width of the first character having the
24964 `glyph' property, and should be FACTOR times that width.
24965
24966 3. `:align-to HPOS' specifies that the space should be wide enough
24967 to reach HPOS, a value in canonical character units.
24968
24969 Exactly one of the above pairs must be present.
24970
24971 4. `:height HEIGHT' specifies that the height of the stretch produced
24972 should be HEIGHT, measured in canonical character units.
24973
24974 5. `:relative-height FACTOR' specifies that the height of the
24975 stretch should be FACTOR times the height of the characters having
24976 the glyph property.
24977
24978 Either none or exactly one of 4 or 5 must be present.
24979
24980 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24981 of the stretch should be used for the ascent of the stretch.
24982 ASCENT must be in the range 0 <= ASCENT <= 100. */
24983
24984 void
24985 produce_stretch_glyph (struct it *it)
24986 {
24987 /* (space :width WIDTH :height HEIGHT ...) */
24988 Lisp_Object prop, plist;
24989 int width = 0, height = 0, align_to = -1;
24990 int zero_width_ok_p = 0;
24991 double tem;
24992 struct font *font = NULL;
24993
24994 #ifdef HAVE_WINDOW_SYSTEM
24995 int ascent = 0;
24996 int zero_height_ok_p = 0;
24997
24998 if (FRAME_WINDOW_P (it->f))
24999 {
25000 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25001 font = face->font ? face->font : FRAME_FONT (it->f);
25002 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25003 }
25004 #endif
25005
25006 /* List should start with `space'. */
25007 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25008 plist = XCDR (it->object);
25009
25010 /* Compute the width of the stretch. */
25011 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25012 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25013 {
25014 /* Absolute width `:width WIDTH' specified and valid. */
25015 zero_width_ok_p = 1;
25016 width = (int)tem;
25017 }
25018 #ifdef HAVE_WINDOW_SYSTEM
25019 else if (FRAME_WINDOW_P (it->f)
25020 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25021 {
25022 /* Relative width `:relative-width FACTOR' specified and valid.
25023 Compute the width of the characters having the `glyph'
25024 property. */
25025 struct it it2;
25026 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25027
25028 it2 = *it;
25029 if (it->multibyte_p)
25030 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25031 else
25032 {
25033 it2.c = it2.char_to_display = *p, it2.len = 1;
25034 if (! ASCII_CHAR_P (it2.c))
25035 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25036 }
25037
25038 it2.glyph_row = NULL;
25039 it2.what = IT_CHARACTER;
25040 x_produce_glyphs (&it2);
25041 width = NUMVAL (prop) * it2.pixel_width;
25042 }
25043 #endif /* HAVE_WINDOW_SYSTEM */
25044 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25045 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25046 {
25047 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25048 align_to = (align_to < 0
25049 ? 0
25050 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25051 else if (align_to < 0)
25052 align_to = window_box_left_offset (it->w, TEXT_AREA);
25053 width = max (0, (int)tem + align_to - it->current_x);
25054 zero_width_ok_p = 1;
25055 }
25056 else
25057 /* Nothing specified -> width defaults to canonical char width. */
25058 width = FRAME_COLUMN_WIDTH (it->f);
25059
25060 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25061 width = 1;
25062
25063 #ifdef HAVE_WINDOW_SYSTEM
25064 /* Compute height. */
25065 if (FRAME_WINDOW_P (it->f))
25066 {
25067 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25068 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25069 {
25070 height = (int)tem;
25071 zero_height_ok_p = 1;
25072 }
25073 else if (prop = Fplist_get (plist, QCrelative_height),
25074 NUMVAL (prop) > 0)
25075 height = FONT_HEIGHT (font) * NUMVAL (prop);
25076 else
25077 height = FONT_HEIGHT (font);
25078
25079 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25080 height = 1;
25081
25082 /* Compute percentage of height used for ascent. If
25083 `:ascent ASCENT' is present and valid, use that. Otherwise,
25084 derive the ascent from the font in use. */
25085 if (prop = Fplist_get (plist, QCascent),
25086 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25087 ascent = height * NUMVAL (prop) / 100.0;
25088 else if (!NILP (prop)
25089 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25090 ascent = min (max (0, (int)tem), height);
25091 else
25092 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25093 }
25094 else
25095 #endif /* HAVE_WINDOW_SYSTEM */
25096 height = 1;
25097
25098 if (width > 0 && it->line_wrap != TRUNCATE
25099 && it->current_x + width > it->last_visible_x)
25100 {
25101 width = it->last_visible_x - it->current_x;
25102 #ifdef HAVE_WINDOW_SYSTEM
25103 /* Subtract one more pixel from the stretch width, but only on
25104 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25105 width -= FRAME_WINDOW_P (it->f);
25106 #endif
25107 }
25108
25109 if (width > 0 && height > 0 && it->glyph_row)
25110 {
25111 Lisp_Object o_object = it->object;
25112 Lisp_Object object = it->stack[it->sp - 1].string;
25113 int n = width;
25114
25115 if (!STRINGP (object))
25116 object = it->w->contents;
25117 #ifdef HAVE_WINDOW_SYSTEM
25118 if (FRAME_WINDOW_P (it->f))
25119 append_stretch_glyph (it, object, width, height, ascent);
25120 else
25121 #endif
25122 {
25123 it->object = object;
25124 it->char_to_display = ' ';
25125 it->pixel_width = it->len = 1;
25126 while (n--)
25127 tty_append_glyph (it);
25128 it->object = o_object;
25129 }
25130 }
25131
25132 it->pixel_width = width;
25133 #ifdef HAVE_WINDOW_SYSTEM
25134 if (FRAME_WINDOW_P (it->f))
25135 {
25136 it->ascent = it->phys_ascent = ascent;
25137 it->descent = it->phys_descent = height - it->ascent;
25138 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25139 take_vertical_position_into_account (it);
25140 }
25141 else
25142 #endif
25143 it->nglyphs = width;
25144 }
25145
25146 /* Get information about special display element WHAT in an
25147 environment described by IT. WHAT is one of IT_TRUNCATION or
25148 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25149 non-null glyph_row member. This function ensures that fields like
25150 face_id, c, len of IT are left untouched. */
25151
25152 static void
25153 produce_special_glyphs (struct it *it, enum display_element_type what)
25154 {
25155 struct it temp_it;
25156 Lisp_Object gc;
25157 GLYPH glyph;
25158
25159 temp_it = *it;
25160 temp_it.object = make_number (0);
25161 memset (&temp_it.current, 0, sizeof temp_it.current);
25162
25163 if (what == IT_CONTINUATION)
25164 {
25165 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25166 if (it->bidi_it.paragraph_dir == R2L)
25167 SET_GLYPH_FROM_CHAR (glyph, '/');
25168 else
25169 SET_GLYPH_FROM_CHAR (glyph, '\\');
25170 if (it->dp
25171 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25172 {
25173 /* FIXME: Should we mirror GC for R2L lines? */
25174 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25175 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25176 }
25177 }
25178 else if (what == IT_TRUNCATION)
25179 {
25180 /* Truncation glyph. */
25181 SET_GLYPH_FROM_CHAR (glyph, '$');
25182 if (it->dp
25183 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25184 {
25185 /* FIXME: Should we mirror GC for R2L lines? */
25186 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25187 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25188 }
25189 }
25190 else
25191 emacs_abort ();
25192
25193 #ifdef HAVE_WINDOW_SYSTEM
25194 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25195 is turned off, we precede the truncation/continuation glyphs by a
25196 stretch glyph whose width is computed such that these special
25197 glyphs are aligned at the window margin, even when very different
25198 fonts are used in different glyph rows. */
25199 if (FRAME_WINDOW_P (temp_it.f)
25200 /* init_iterator calls this with it->glyph_row == NULL, and it
25201 wants only the pixel width of the truncation/continuation
25202 glyphs. */
25203 && temp_it.glyph_row
25204 /* insert_left_trunc_glyphs calls us at the beginning of the
25205 row, and it has its own calculation of the stretch glyph
25206 width. */
25207 && temp_it.glyph_row->used[TEXT_AREA] > 0
25208 && (temp_it.glyph_row->reversed_p
25209 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25210 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25211 {
25212 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25213
25214 if (stretch_width > 0)
25215 {
25216 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25217 struct font *font =
25218 face->font ? face->font : FRAME_FONT (temp_it.f);
25219 int stretch_ascent =
25220 (((temp_it.ascent + temp_it.descent)
25221 * FONT_BASE (font)) / FONT_HEIGHT (font));
25222
25223 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25224 temp_it.ascent + temp_it.descent,
25225 stretch_ascent);
25226 }
25227 }
25228 #endif
25229
25230 temp_it.dp = NULL;
25231 temp_it.what = IT_CHARACTER;
25232 temp_it.len = 1;
25233 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25234 temp_it.face_id = GLYPH_FACE (glyph);
25235 temp_it.len = CHAR_BYTES (temp_it.c);
25236
25237 PRODUCE_GLYPHS (&temp_it);
25238 it->pixel_width = temp_it.pixel_width;
25239 it->nglyphs = temp_it.pixel_width;
25240 }
25241
25242 #ifdef HAVE_WINDOW_SYSTEM
25243
25244 /* Calculate line-height and line-spacing properties.
25245 An integer value specifies explicit pixel value.
25246 A float value specifies relative value to current face height.
25247 A cons (float . face-name) specifies relative value to
25248 height of specified face font.
25249
25250 Returns height in pixels, or nil. */
25251
25252
25253 static Lisp_Object
25254 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25255 int boff, int override)
25256 {
25257 Lisp_Object face_name = Qnil;
25258 int ascent, descent, height;
25259
25260 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25261 return val;
25262
25263 if (CONSP (val))
25264 {
25265 face_name = XCAR (val);
25266 val = XCDR (val);
25267 if (!NUMBERP (val))
25268 val = make_number (1);
25269 if (NILP (face_name))
25270 {
25271 height = it->ascent + it->descent;
25272 goto scale;
25273 }
25274 }
25275
25276 if (NILP (face_name))
25277 {
25278 font = FRAME_FONT (it->f);
25279 boff = FRAME_BASELINE_OFFSET (it->f);
25280 }
25281 else if (EQ (face_name, Qt))
25282 {
25283 override = 0;
25284 }
25285 else
25286 {
25287 int face_id;
25288 struct face *face;
25289
25290 face_id = lookup_named_face (it->f, face_name, 0);
25291 if (face_id < 0)
25292 return make_number (-1);
25293
25294 face = FACE_FROM_ID (it->f, face_id);
25295 font = face->font;
25296 if (font == NULL)
25297 return make_number (-1);
25298 boff = font->baseline_offset;
25299 if (font->vertical_centering)
25300 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25301 }
25302
25303 ascent = FONT_BASE (font) + boff;
25304 descent = FONT_DESCENT (font) - boff;
25305
25306 if (override)
25307 {
25308 it->override_ascent = ascent;
25309 it->override_descent = descent;
25310 it->override_boff = boff;
25311 }
25312
25313 height = ascent + descent;
25314
25315 scale:
25316 if (FLOATP (val))
25317 height = (int)(XFLOAT_DATA (val) * height);
25318 else if (INTEGERP (val))
25319 height *= XINT (val);
25320
25321 return make_number (height);
25322 }
25323
25324
25325 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25326 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25327 and only if this is for a character for which no font was found.
25328
25329 If the display method (it->glyphless_method) is
25330 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25331 length of the acronym or the hexadecimal string, UPPER_XOFF and
25332 UPPER_YOFF are pixel offsets for the upper part of the string,
25333 LOWER_XOFF and LOWER_YOFF are for the lower part.
25334
25335 For the other display methods, LEN through LOWER_YOFF are zero. */
25336
25337 static void
25338 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25339 short upper_xoff, short upper_yoff,
25340 short lower_xoff, short lower_yoff)
25341 {
25342 struct glyph *glyph;
25343 enum glyph_row_area area = it->area;
25344
25345 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25346 if (glyph < it->glyph_row->glyphs[area + 1])
25347 {
25348 /* If the glyph row is reversed, we need to prepend the glyph
25349 rather than append it. */
25350 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25351 {
25352 struct glyph *g;
25353
25354 /* Make room for the additional glyph. */
25355 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25356 g[1] = *g;
25357 glyph = it->glyph_row->glyphs[area];
25358 }
25359 glyph->charpos = CHARPOS (it->position);
25360 glyph->object = it->object;
25361 glyph->pixel_width = it->pixel_width;
25362 glyph->ascent = it->ascent;
25363 glyph->descent = it->descent;
25364 glyph->voffset = it->voffset;
25365 glyph->type = GLYPHLESS_GLYPH;
25366 glyph->u.glyphless.method = it->glyphless_method;
25367 glyph->u.glyphless.for_no_font = for_no_font;
25368 glyph->u.glyphless.len = len;
25369 glyph->u.glyphless.ch = it->c;
25370 glyph->slice.glyphless.upper_xoff = upper_xoff;
25371 glyph->slice.glyphless.upper_yoff = upper_yoff;
25372 glyph->slice.glyphless.lower_xoff = lower_xoff;
25373 glyph->slice.glyphless.lower_yoff = lower_yoff;
25374 glyph->avoid_cursor_p = it->avoid_cursor_p;
25375 glyph->multibyte_p = it->multibyte_p;
25376 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25377 {
25378 /* In R2L rows, the left and the right box edges need to be
25379 drawn in reverse direction. */
25380 glyph->right_box_line_p = it->start_of_box_run_p;
25381 glyph->left_box_line_p = it->end_of_box_run_p;
25382 }
25383 else
25384 {
25385 glyph->left_box_line_p = it->start_of_box_run_p;
25386 glyph->right_box_line_p = it->end_of_box_run_p;
25387 }
25388 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25389 || it->phys_descent > it->descent);
25390 glyph->padding_p = 0;
25391 glyph->glyph_not_available_p = 0;
25392 glyph->face_id = face_id;
25393 glyph->font_type = FONT_TYPE_UNKNOWN;
25394 if (it->bidi_p)
25395 {
25396 glyph->resolved_level = it->bidi_it.resolved_level;
25397 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25398 emacs_abort ();
25399 glyph->bidi_type = it->bidi_it.type;
25400 }
25401 ++it->glyph_row->used[area];
25402 }
25403 else
25404 IT_EXPAND_MATRIX_WIDTH (it, area);
25405 }
25406
25407
25408 /* Produce a glyph for a glyphless character for iterator IT.
25409 IT->glyphless_method specifies which method to use for displaying
25410 the character. See the description of enum
25411 glyphless_display_method in dispextern.h for the detail.
25412
25413 FOR_NO_FONT is nonzero if and only if this is for a character for
25414 which no font was found. ACRONYM, if non-nil, is an acronym string
25415 for the character. */
25416
25417 static void
25418 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25419 {
25420 int face_id;
25421 struct face *face;
25422 struct font *font;
25423 int base_width, base_height, width, height;
25424 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25425 int len;
25426
25427 /* Get the metrics of the base font. We always refer to the current
25428 ASCII face. */
25429 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25430 font = face->font ? face->font : FRAME_FONT (it->f);
25431 it->ascent = FONT_BASE (font) + font->baseline_offset;
25432 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25433 base_height = it->ascent + it->descent;
25434 base_width = font->average_width;
25435
25436 face_id = merge_glyphless_glyph_face (it);
25437
25438 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25439 {
25440 it->pixel_width = THIN_SPACE_WIDTH;
25441 len = 0;
25442 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25443 }
25444 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25445 {
25446 width = CHAR_WIDTH (it->c);
25447 if (width == 0)
25448 width = 1;
25449 else if (width > 4)
25450 width = 4;
25451 it->pixel_width = base_width * width;
25452 len = 0;
25453 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25454 }
25455 else
25456 {
25457 char buf[7];
25458 const char *str;
25459 unsigned int code[6];
25460 int upper_len;
25461 int ascent, descent;
25462 struct font_metrics metrics_upper, metrics_lower;
25463
25464 face = FACE_FROM_ID (it->f, face_id);
25465 font = face->font ? face->font : FRAME_FONT (it->f);
25466 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25467
25468 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25469 {
25470 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25471 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25472 if (CONSP (acronym))
25473 acronym = XCAR (acronym);
25474 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25475 }
25476 else
25477 {
25478 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25479 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25480 str = buf;
25481 }
25482 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25483 code[len] = font->driver->encode_char (font, str[len]);
25484 upper_len = (len + 1) / 2;
25485 font->driver->text_extents (font, code, upper_len,
25486 &metrics_upper);
25487 font->driver->text_extents (font, code + upper_len, len - upper_len,
25488 &metrics_lower);
25489
25490
25491
25492 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25493 width = max (metrics_upper.width, metrics_lower.width) + 4;
25494 upper_xoff = upper_yoff = 2; /* the typical case */
25495 if (base_width >= width)
25496 {
25497 /* Align the upper to the left, the lower to the right. */
25498 it->pixel_width = base_width;
25499 lower_xoff = base_width - 2 - metrics_lower.width;
25500 }
25501 else
25502 {
25503 /* Center the shorter one. */
25504 it->pixel_width = width;
25505 if (metrics_upper.width >= metrics_lower.width)
25506 lower_xoff = (width - metrics_lower.width) / 2;
25507 else
25508 {
25509 /* FIXME: This code doesn't look right. It formerly was
25510 missing the "lower_xoff = 0;", which couldn't have
25511 been right since it left lower_xoff uninitialized. */
25512 lower_xoff = 0;
25513 upper_xoff = (width - metrics_upper.width) / 2;
25514 }
25515 }
25516
25517 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25518 top, bottom, and between upper and lower strings. */
25519 height = (metrics_upper.ascent + metrics_upper.descent
25520 + metrics_lower.ascent + metrics_lower.descent) + 5;
25521 /* Center vertically.
25522 H:base_height, D:base_descent
25523 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25524
25525 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25526 descent = D - H/2 + h/2;
25527 lower_yoff = descent - 2 - ld;
25528 upper_yoff = lower_yoff - la - 1 - ud; */
25529 ascent = - (it->descent - (base_height + height + 1) / 2);
25530 descent = it->descent - (base_height - height) / 2;
25531 lower_yoff = descent - 2 - metrics_lower.descent;
25532 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25533 - metrics_upper.descent);
25534 /* Don't make the height shorter than the base height. */
25535 if (height > base_height)
25536 {
25537 it->ascent = ascent;
25538 it->descent = descent;
25539 }
25540 }
25541
25542 it->phys_ascent = it->ascent;
25543 it->phys_descent = it->descent;
25544 if (it->glyph_row)
25545 append_glyphless_glyph (it, face_id, for_no_font, len,
25546 upper_xoff, upper_yoff,
25547 lower_xoff, lower_yoff);
25548 it->nglyphs = 1;
25549 take_vertical_position_into_account (it);
25550 }
25551
25552
25553 /* RIF:
25554 Produce glyphs/get display metrics for the display element IT is
25555 loaded with. See the description of struct it in dispextern.h
25556 for an overview of struct it. */
25557
25558 void
25559 x_produce_glyphs (struct it *it)
25560 {
25561 int extra_line_spacing = it->extra_line_spacing;
25562
25563 it->glyph_not_available_p = 0;
25564
25565 if (it->what == IT_CHARACTER)
25566 {
25567 XChar2b char2b;
25568 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25569 struct font *font = face->font;
25570 struct font_metrics *pcm = NULL;
25571 int boff; /* Baseline offset. */
25572
25573 if (font == NULL)
25574 {
25575 /* When no suitable font is found, display this character by
25576 the method specified in the first extra slot of
25577 Vglyphless_char_display. */
25578 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25579
25580 eassert (it->what == IT_GLYPHLESS);
25581 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25582 goto done;
25583 }
25584
25585 boff = font->baseline_offset;
25586 if (font->vertical_centering)
25587 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25588
25589 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25590 {
25591 int stretched_p;
25592
25593 it->nglyphs = 1;
25594
25595 if (it->override_ascent >= 0)
25596 {
25597 it->ascent = it->override_ascent;
25598 it->descent = it->override_descent;
25599 boff = it->override_boff;
25600 }
25601 else
25602 {
25603 it->ascent = FONT_BASE (font) + boff;
25604 it->descent = FONT_DESCENT (font) - boff;
25605 }
25606
25607 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25608 {
25609 pcm = get_per_char_metric (font, &char2b);
25610 if (pcm->width == 0
25611 && pcm->rbearing == 0 && pcm->lbearing == 0)
25612 pcm = NULL;
25613 }
25614
25615 if (pcm)
25616 {
25617 it->phys_ascent = pcm->ascent + boff;
25618 it->phys_descent = pcm->descent - boff;
25619 it->pixel_width = pcm->width;
25620 }
25621 else
25622 {
25623 it->glyph_not_available_p = 1;
25624 it->phys_ascent = it->ascent;
25625 it->phys_descent = it->descent;
25626 it->pixel_width = font->space_width;
25627 }
25628
25629 if (it->constrain_row_ascent_descent_p)
25630 {
25631 if (it->descent > it->max_descent)
25632 {
25633 it->ascent += it->descent - it->max_descent;
25634 it->descent = it->max_descent;
25635 }
25636 if (it->ascent > it->max_ascent)
25637 {
25638 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25639 it->ascent = it->max_ascent;
25640 }
25641 it->phys_ascent = min (it->phys_ascent, it->ascent);
25642 it->phys_descent = min (it->phys_descent, it->descent);
25643 extra_line_spacing = 0;
25644 }
25645
25646 /* If this is a space inside a region of text with
25647 `space-width' property, change its width. */
25648 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25649 if (stretched_p)
25650 it->pixel_width *= XFLOATINT (it->space_width);
25651
25652 /* If face has a box, add the box thickness to the character
25653 height. If character has a box line to the left and/or
25654 right, add the box line width to the character's width. */
25655 if (face->box != FACE_NO_BOX)
25656 {
25657 int thick = face->box_line_width;
25658
25659 if (thick > 0)
25660 {
25661 it->ascent += thick;
25662 it->descent += thick;
25663 }
25664 else
25665 thick = -thick;
25666
25667 if (it->start_of_box_run_p)
25668 it->pixel_width += thick;
25669 if (it->end_of_box_run_p)
25670 it->pixel_width += thick;
25671 }
25672
25673 /* If face has an overline, add the height of the overline
25674 (1 pixel) and a 1 pixel margin to the character height. */
25675 if (face->overline_p)
25676 it->ascent += overline_margin;
25677
25678 if (it->constrain_row_ascent_descent_p)
25679 {
25680 if (it->ascent > it->max_ascent)
25681 it->ascent = it->max_ascent;
25682 if (it->descent > it->max_descent)
25683 it->descent = it->max_descent;
25684 }
25685
25686 take_vertical_position_into_account (it);
25687
25688 /* If we have to actually produce glyphs, do it. */
25689 if (it->glyph_row)
25690 {
25691 if (stretched_p)
25692 {
25693 /* Translate a space with a `space-width' property
25694 into a stretch glyph. */
25695 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25696 / FONT_HEIGHT (font));
25697 append_stretch_glyph (it, it->object, it->pixel_width,
25698 it->ascent + it->descent, ascent);
25699 }
25700 else
25701 append_glyph (it);
25702
25703 /* If characters with lbearing or rbearing are displayed
25704 in this line, record that fact in a flag of the
25705 glyph row. This is used to optimize X output code. */
25706 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25707 it->glyph_row->contains_overlapping_glyphs_p = 1;
25708 }
25709 if (! stretched_p && it->pixel_width == 0)
25710 /* We assure that all visible glyphs have at least 1-pixel
25711 width. */
25712 it->pixel_width = 1;
25713 }
25714 else if (it->char_to_display == '\n')
25715 {
25716 /* A newline has no width, but we need the height of the
25717 line. But if previous part of the line sets a height,
25718 don't increase that height. */
25719
25720 Lisp_Object height;
25721 Lisp_Object total_height = Qnil;
25722
25723 it->override_ascent = -1;
25724 it->pixel_width = 0;
25725 it->nglyphs = 0;
25726
25727 height = get_it_property (it, Qline_height);
25728 /* Split (line-height total-height) list. */
25729 if (CONSP (height)
25730 && CONSP (XCDR (height))
25731 && NILP (XCDR (XCDR (height))))
25732 {
25733 total_height = XCAR (XCDR (height));
25734 height = XCAR (height);
25735 }
25736 height = calc_line_height_property (it, height, font, boff, 1);
25737
25738 if (it->override_ascent >= 0)
25739 {
25740 it->ascent = it->override_ascent;
25741 it->descent = it->override_descent;
25742 boff = it->override_boff;
25743 }
25744 else
25745 {
25746 it->ascent = FONT_BASE (font) + boff;
25747 it->descent = FONT_DESCENT (font) - boff;
25748 }
25749
25750 if (EQ (height, Qt))
25751 {
25752 if (it->descent > it->max_descent)
25753 {
25754 it->ascent += it->descent - it->max_descent;
25755 it->descent = it->max_descent;
25756 }
25757 if (it->ascent > it->max_ascent)
25758 {
25759 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25760 it->ascent = it->max_ascent;
25761 }
25762 it->phys_ascent = min (it->phys_ascent, it->ascent);
25763 it->phys_descent = min (it->phys_descent, it->descent);
25764 it->constrain_row_ascent_descent_p = 1;
25765 extra_line_spacing = 0;
25766 }
25767 else
25768 {
25769 Lisp_Object spacing;
25770
25771 it->phys_ascent = it->ascent;
25772 it->phys_descent = it->descent;
25773
25774 if ((it->max_ascent > 0 || it->max_descent > 0)
25775 && face->box != FACE_NO_BOX
25776 && face->box_line_width > 0)
25777 {
25778 it->ascent += face->box_line_width;
25779 it->descent += face->box_line_width;
25780 }
25781 if (!NILP (height)
25782 && XINT (height) > it->ascent + it->descent)
25783 it->ascent = XINT (height) - it->descent;
25784
25785 if (!NILP (total_height))
25786 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25787 else
25788 {
25789 spacing = get_it_property (it, Qline_spacing);
25790 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25791 }
25792 if (INTEGERP (spacing))
25793 {
25794 extra_line_spacing = XINT (spacing);
25795 if (!NILP (total_height))
25796 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25797 }
25798 }
25799 }
25800 else /* i.e. (it->char_to_display == '\t') */
25801 {
25802 if (font->space_width > 0)
25803 {
25804 int tab_width = it->tab_width * font->space_width;
25805 int x = it->current_x + it->continuation_lines_width;
25806 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25807
25808 /* If the distance from the current position to the next tab
25809 stop is less than a space character width, use the
25810 tab stop after that. */
25811 if (next_tab_x - x < font->space_width)
25812 next_tab_x += tab_width;
25813
25814 it->pixel_width = next_tab_x - x;
25815 it->nglyphs = 1;
25816 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25817 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25818
25819 if (it->glyph_row)
25820 {
25821 append_stretch_glyph (it, it->object, it->pixel_width,
25822 it->ascent + it->descent, it->ascent);
25823 }
25824 }
25825 else
25826 {
25827 it->pixel_width = 0;
25828 it->nglyphs = 1;
25829 }
25830 }
25831 }
25832 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25833 {
25834 /* A static composition.
25835
25836 Note: A composition is represented as one glyph in the
25837 glyph matrix. There are no padding glyphs.
25838
25839 Important note: pixel_width, ascent, and descent are the
25840 values of what is drawn by draw_glyphs (i.e. the values of
25841 the overall glyphs composed). */
25842 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25843 int boff; /* baseline offset */
25844 struct composition *cmp = composition_table[it->cmp_it.id];
25845 int glyph_len = cmp->glyph_len;
25846 struct font *font = face->font;
25847
25848 it->nglyphs = 1;
25849
25850 /* If we have not yet calculated pixel size data of glyphs of
25851 the composition for the current face font, calculate them
25852 now. Theoretically, we have to check all fonts for the
25853 glyphs, but that requires much time and memory space. So,
25854 here we check only the font of the first glyph. This may
25855 lead to incorrect display, but it's very rare, and C-l
25856 (recenter-top-bottom) can correct the display anyway. */
25857 if (! cmp->font || cmp->font != font)
25858 {
25859 /* Ascent and descent of the font of the first character
25860 of this composition (adjusted by baseline offset).
25861 Ascent and descent of overall glyphs should not be less
25862 than these, respectively. */
25863 int font_ascent, font_descent, font_height;
25864 /* Bounding box of the overall glyphs. */
25865 int leftmost, rightmost, lowest, highest;
25866 int lbearing, rbearing;
25867 int i, width, ascent, descent;
25868 int left_padded = 0, right_padded = 0;
25869 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25870 XChar2b char2b;
25871 struct font_metrics *pcm;
25872 int font_not_found_p;
25873 ptrdiff_t pos;
25874
25875 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25876 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25877 break;
25878 if (glyph_len < cmp->glyph_len)
25879 right_padded = 1;
25880 for (i = 0; i < glyph_len; i++)
25881 {
25882 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25883 break;
25884 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25885 }
25886 if (i > 0)
25887 left_padded = 1;
25888
25889 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25890 : IT_CHARPOS (*it));
25891 /* If no suitable font is found, use the default font. */
25892 font_not_found_p = font == NULL;
25893 if (font_not_found_p)
25894 {
25895 face = face->ascii_face;
25896 font = face->font;
25897 }
25898 boff = font->baseline_offset;
25899 if (font->vertical_centering)
25900 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25901 font_ascent = FONT_BASE (font) + boff;
25902 font_descent = FONT_DESCENT (font) - boff;
25903 font_height = FONT_HEIGHT (font);
25904
25905 cmp->font = font;
25906
25907 pcm = NULL;
25908 if (! font_not_found_p)
25909 {
25910 get_char_face_and_encoding (it->f, c, it->face_id,
25911 &char2b, 0);
25912 pcm = get_per_char_metric (font, &char2b);
25913 }
25914
25915 /* Initialize the bounding box. */
25916 if (pcm)
25917 {
25918 width = cmp->glyph_len > 0 ? pcm->width : 0;
25919 ascent = pcm->ascent;
25920 descent = pcm->descent;
25921 lbearing = pcm->lbearing;
25922 rbearing = pcm->rbearing;
25923 }
25924 else
25925 {
25926 width = cmp->glyph_len > 0 ? font->space_width : 0;
25927 ascent = FONT_BASE (font);
25928 descent = FONT_DESCENT (font);
25929 lbearing = 0;
25930 rbearing = width;
25931 }
25932
25933 rightmost = width;
25934 leftmost = 0;
25935 lowest = - descent + boff;
25936 highest = ascent + boff;
25937
25938 if (! font_not_found_p
25939 && font->default_ascent
25940 && CHAR_TABLE_P (Vuse_default_ascent)
25941 && !NILP (Faref (Vuse_default_ascent,
25942 make_number (it->char_to_display))))
25943 highest = font->default_ascent + boff;
25944
25945 /* Draw the first glyph at the normal position. It may be
25946 shifted to right later if some other glyphs are drawn
25947 at the left. */
25948 cmp->offsets[i * 2] = 0;
25949 cmp->offsets[i * 2 + 1] = boff;
25950 cmp->lbearing = lbearing;
25951 cmp->rbearing = rbearing;
25952
25953 /* Set cmp->offsets for the remaining glyphs. */
25954 for (i++; i < glyph_len; i++)
25955 {
25956 int left, right, btm, top;
25957 int ch = COMPOSITION_GLYPH (cmp, i);
25958 int face_id;
25959 struct face *this_face;
25960
25961 if (ch == '\t')
25962 ch = ' ';
25963 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25964 this_face = FACE_FROM_ID (it->f, face_id);
25965 font = this_face->font;
25966
25967 if (font == NULL)
25968 pcm = NULL;
25969 else
25970 {
25971 get_char_face_and_encoding (it->f, ch, face_id,
25972 &char2b, 0);
25973 pcm = get_per_char_metric (font, &char2b);
25974 }
25975 if (! pcm)
25976 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25977 else
25978 {
25979 width = pcm->width;
25980 ascent = pcm->ascent;
25981 descent = pcm->descent;
25982 lbearing = pcm->lbearing;
25983 rbearing = pcm->rbearing;
25984 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25985 {
25986 /* Relative composition with or without
25987 alternate chars. */
25988 left = (leftmost + rightmost - width) / 2;
25989 btm = - descent + boff;
25990 if (font->relative_compose
25991 && (! CHAR_TABLE_P (Vignore_relative_composition)
25992 || NILP (Faref (Vignore_relative_composition,
25993 make_number (ch)))))
25994 {
25995
25996 if (- descent >= font->relative_compose)
25997 /* One extra pixel between two glyphs. */
25998 btm = highest + 1;
25999 else if (ascent <= 0)
26000 /* One extra pixel between two glyphs. */
26001 btm = lowest - 1 - ascent - descent;
26002 }
26003 }
26004 else
26005 {
26006 /* A composition rule is specified by an integer
26007 value that encodes global and new reference
26008 points (GREF and NREF). GREF and NREF are
26009 specified by numbers as below:
26010
26011 0---1---2 -- ascent
26012 | |
26013 | |
26014 | |
26015 9--10--11 -- center
26016 | |
26017 ---3---4---5--- baseline
26018 | |
26019 6---7---8 -- descent
26020 */
26021 int rule = COMPOSITION_RULE (cmp, i);
26022 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26023
26024 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26025 grefx = gref % 3, nrefx = nref % 3;
26026 grefy = gref / 3, nrefy = nref / 3;
26027 if (xoff)
26028 xoff = font_height * (xoff - 128) / 256;
26029 if (yoff)
26030 yoff = font_height * (yoff - 128) / 256;
26031
26032 left = (leftmost
26033 + grefx * (rightmost - leftmost) / 2
26034 - nrefx * width / 2
26035 + xoff);
26036
26037 btm = ((grefy == 0 ? highest
26038 : grefy == 1 ? 0
26039 : grefy == 2 ? lowest
26040 : (highest + lowest) / 2)
26041 - (nrefy == 0 ? ascent + descent
26042 : nrefy == 1 ? descent - boff
26043 : nrefy == 2 ? 0
26044 : (ascent + descent) / 2)
26045 + yoff);
26046 }
26047
26048 cmp->offsets[i * 2] = left;
26049 cmp->offsets[i * 2 + 1] = btm + descent;
26050
26051 /* Update the bounding box of the overall glyphs. */
26052 if (width > 0)
26053 {
26054 right = left + width;
26055 if (left < leftmost)
26056 leftmost = left;
26057 if (right > rightmost)
26058 rightmost = right;
26059 }
26060 top = btm + descent + ascent;
26061 if (top > highest)
26062 highest = top;
26063 if (btm < lowest)
26064 lowest = btm;
26065
26066 if (cmp->lbearing > left + lbearing)
26067 cmp->lbearing = left + lbearing;
26068 if (cmp->rbearing < left + rbearing)
26069 cmp->rbearing = left + rbearing;
26070 }
26071 }
26072
26073 /* If there are glyphs whose x-offsets are negative,
26074 shift all glyphs to the right and make all x-offsets
26075 non-negative. */
26076 if (leftmost < 0)
26077 {
26078 for (i = 0; i < cmp->glyph_len; i++)
26079 cmp->offsets[i * 2] -= leftmost;
26080 rightmost -= leftmost;
26081 cmp->lbearing -= leftmost;
26082 cmp->rbearing -= leftmost;
26083 }
26084
26085 if (left_padded && cmp->lbearing < 0)
26086 {
26087 for (i = 0; i < cmp->glyph_len; i++)
26088 cmp->offsets[i * 2] -= cmp->lbearing;
26089 rightmost -= cmp->lbearing;
26090 cmp->rbearing -= cmp->lbearing;
26091 cmp->lbearing = 0;
26092 }
26093 if (right_padded && rightmost < cmp->rbearing)
26094 {
26095 rightmost = cmp->rbearing;
26096 }
26097
26098 cmp->pixel_width = rightmost;
26099 cmp->ascent = highest;
26100 cmp->descent = - lowest;
26101 if (cmp->ascent < font_ascent)
26102 cmp->ascent = font_ascent;
26103 if (cmp->descent < font_descent)
26104 cmp->descent = font_descent;
26105 }
26106
26107 if (it->glyph_row
26108 && (cmp->lbearing < 0
26109 || cmp->rbearing > cmp->pixel_width))
26110 it->glyph_row->contains_overlapping_glyphs_p = 1;
26111
26112 it->pixel_width = cmp->pixel_width;
26113 it->ascent = it->phys_ascent = cmp->ascent;
26114 it->descent = it->phys_descent = cmp->descent;
26115 if (face->box != FACE_NO_BOX)
26116 {
26117 int thick = face->box_line_width;
26118
26119 if (thick > 0)
26120 {
26121 it->ascent += thick;
26122 it->descent += thick;
26123 }
26124 else
26125 thick = - thick;
26126
26127 if (it->start_of_box_run_p)
26128 it->pixel_width += thick;
26129 if (it->end_of_box_run_p)
26130 it->pixel_width += thick;
26131 }
26132
26133 /* If face has an overline, add the height of the overline
26134 (1 pixel) and a 1 pixel margin to the character height. */
26135 if (face->overline_p)
26136 it->ascent += overline_margin;
26137
26138 take_vertical_position_into_account (it);
26139 if (it->ascent < 0)
26140 it->ascent = 0;
26141 if (it->descent < 0)
26142 it->descent = 0;
26143
26144 if (it->glyph_row && cmp->glyph_len > 0)
26145 append_composite_glyph (it);
26146 }
26147 else if (it->what == IT_COMPOSITION)
26148 {
26149 /* A dynamic (automatic) composition. */
26150 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26151 Lisp_Object gstring;
26152 struct font_metrics metrics;
26153
26154 it->nglyphs = 1;
26155
26156 gstring = composition_gstring_from_id (it->cmp_it.id);
26157 it->pixel_width
26158 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26159 &metrics);
26160 if (it->glyph_row
26161 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26162 it->glyph_row->contains_overlapping_glyphs_p = 1;
26163 it->ascent = it->phys_ascent = metrics.ascent;
26164 it->descent = it->phys_descent = metrics.descent;
26165 if (face->box != FACE_NO_BOX)
26166 {
26167 int thick = face->box_line_width;
26168
26169 if (thick > 0)
26170 {
26171 it->ascent += thick;
26172 it->descent += thick;
26173 }
26174 else
26175 thick = - thick;
26176
26177 if (it->start_of_box_run_p)
26178 it->pixel_width += thick;
26179 if (it->end_of_box_run_p)
26180 it->pixel_width += thick;
26181 }
26182 /* If face has an overline, add the height of the overline
26183 (1 pixel) and a 1 pixel margin to the character height. */
26184 if (face->overline_p)
26185 it->ascent += overline_margin;
26186 take_vertical_position_into_account (it);
26187 if (it->ascent < 0)
26188 it->ascent = 0;
26189 if (it->descent < 0)
26190 it->descent = 0;
26191
26192 if (it->glyph_row)
26193 append_composite_glyph (it);
26194 }
26195 else if (it->what == IT_GLYPHLESS)
26196 produce_glyphless_glyph (it, 0, Qnil);
26197 else if (it->what == IT_IMAGE)
26198 produce_image_glyph (it);
26199 else if (it->what == IT_STRETCH)
26200 produce_stretch_glyph (it);
26201
26202 done:
26203 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26204 because this isn't true for images with `:ascent 100'. */
26205 eassert (it->ascent >= 0 && it->descent >= 0);
26206 if (it->area == TEXT_AREA)
26207 it->current_x += it->pixel_width;
26208
26209 if (extra_line_spacing > 0)
26210 {
26211 it->descent += extra_line_spacing;
26212 if (extra_line_spacing > it->max_extra_line_spacing)
26213 it->max_extra_line_spacing = extra_line_spacing;
26214 }
26215
26216 it->max_ascent = max (it->max_ascent, it->ascent);
26217 it->max_descent = max (it->max_descent, it->descent);
26218 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26219 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26220 }
26221
26222 /* EXPORT for RIF:
26223 Output LEN glyphs starting at START at the nominal cursor position.
26224 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26225 being updated, and UPDATED_AREA is the area of that row being updated. */
26226
26227 void
26228 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26229 struct glyph *start, enum glyph_row_area updated_area, int len)
26230 {
26231 int x, hpos, chpos = w->phys_cursor.hpos;
26232
26233 eassert (updated_row);
26234 /* When the window is hscrolled, cursor hpos can legitimately be out
26235 of bounds, but we draw the cursor at the corresponding window
26236 margin in that case. */
26237 if (!updated_row->reversed_p && chpos < 0)
26238 chpos = 0;
26239 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26240 chpos = updated_row->used[TEXT_AREA] - 1;
26241
26242 block_input ();
26243
26244 /* Write glyphs. */
26245
26246 hpos = start - updated_row->glyphs[updated_area];
26247 x = draw_glyphs (w, w->output_cursor.x,
26248 updated_row, updated_area,
26249 hpos, hpos + len,
26250 DRAW_NORMAL_TEXT, 0);
26251
26252 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26253 if (updated_area == TEXT_AREA
26254 && w->phys_cursor_on_p
26255 && w->phys_cursor.vpos == w->output_cursor.vpos
26256 && chpos >= hpos
26257 && chpos < hpos + len)
26258 w->phys_cursor_on_p = 0;
26259
26260 unblock_input ();
26261
26262 /* Advance the output cursor. */
26263 w->output_cursor.hpos += len;
26264 w->output_cursor.x = x;
26265 }
26266
26267
26268 /* EXPORT for RIF:
26269 Insert LEN glyphs from START at the nominal cursor position. */
26270
26271 void
26272 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26273 struct glyph *start, enum glyph_row_area updated_area, int len)
26274 {
26275 struct frame *f;
26276 int line_height, shift_by_width, shifted_region_width;
26277 struct glyph_row *row;
26278 struct glyph *glyph;
26279 int frame_x, frame_y;
26280 ptrdiff_t hpos;
26281
26282 eassert (updated_row);
26283 block_input ();
26284 f = XFRAME (WINDOW_FRAME (w));
26285
26286 /* Get the height of the line we are in. */
26287 row = updated_row;
26288 line_height = row->height;
26289
26290 /* Get the width of the glyphs to insert. */
26291 shift_by_width = 0;
26292 for (glyph = start; glyph < start + len; ++glyph)
26293 shift_by_width += glyph->pixel_width;
26294
26295 /* Get the width of the region to shift right. */
26296 shifted_region_width = (window_box_width (w, updated_area)
26297 - w->output_cursor.x
26298 - shift_by_width);
26299
26300 /* Shift right. */
26301 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26302 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26303
26304 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26305 line_height, shift_by_width);
26306
26307 /* Write the glyphs. */
26308 hpos = start - row->glyphs[updated_area];
26309 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26310 hpos, hpos + len,
26311 DRAW_NORMAL_TEXT, 0);
26312
26313 /* Advance the output cursor. */
26314 w->output_cursor.hpos += len;
26315 w->output_cursor.x += shift_by_width;
26316 unblock_input ();
26317 }
26318
26319
26320 /* EXPORT for RIF:
26321 Erase the current text line from the nominal cursor position
26322 (inclusive) to pixel column TO_X (exclusive). The idea is that
26323 everything from TO_X onward is already erased.
26324
26325 TO_X is a pixel position relative to UPDATED_AREA of currently
26326 updated window W. TO_X == -1 means clear to the end of this area. */
26327
26328 void
26329 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26330 enum glyph_row_area updated_area, int to_x)
26331 {
26332 struct frame *f;
26333 int max_x, min_y, max_y;
26334 int from_x, from_y, to_y;
26335
26336 eassert (updated_row);
26337 f = XFRAME (w->frame);
26338
26339 if (updated_row->full_width_p)
26340 max_x = (WINDOW_PIXEL_WIDTH (w)
26341 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26342 else
26343 max_x = window_box_width (w, updated_area);
26344 max_y = window_text_bottom_y (w);
26345
26346 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26347 of window. For TO_X > 0, truncate to end of drawing area. */
26348 if (to_x == 0)
26349 return;
26350 else if (to_x < 0)
26351 to_x = max_x;
26352 else
26353 to_x = min (to_x, max_x);
26354
26355 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26356
26357 /* Notice if the cursor will be cleared by this operation. */
26358 if (!updated_row->full_width_p)
26359 notice_overwritten_cursor (w, updated_area,
26360 w->output_cursor.x, -1,
26361 updated_row->y,
26362 MATRIX_ROW_BOTTOM_Y (updated_row));
26363
26364 from_x = w->output_cursor.x;
26365
26366 /* Translate to frame coordinates. */
26367 if (updated_row->full_width_p)
26368 {
26369 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26370 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26371 }
26372 else
26373 {
26374 int area_left = window_box_left (w, updated_area);
26375 from_x += area_left;
26376 to_x += area_left;
26377 }
26378
26379 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26380 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26381 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26382
26383 /* Prevent inadvertently clearing to end of the X window. */
26384 if (to_x > from_x && to_y > from_y)
26385 {
26386 block_input ();
26387 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26388 to_x - from_x, to_y - from_y);
26389 unblock_input ();
26390 }
26391 }
26392
26393 #endif /* HAVE_WINDOW_SYSTEM */
26394
26395
26396 \f
26397 /***********************************************************************
26398 Cursor types
26399 ***********************************************************************/
26400
26401 /* Value is the internal representation of the specified cursor type
26402 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26403 of the bar cursor. */
26404
26405 static enum text_cursor_kinds
26406 get_specified_cursor_type (Lisp_Object arg, int *width)
26407 {
26408 enum text_cursor_kinds type;
26409
26410 if (NILP (arg))
26411 return NO_CURSOR;
26412
26413 if (EQ (arg, Qbox))
26414 return FILLED_BOX_CURSOR;
26415
26416 if (EQ (arg, Qhollow))
26417 return HOLLOW_BOX_CURSOR;
26418
26419 if (EQ (arg, Qbar))
26420 {
26421 *width = 2;
26422 return BAR_CURSOR;
26423 }
26424
26425 if (CONSP (arg)
26426 && EQ (XCAR (arg), Qbar)
26427 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26428 {
26429 *width = XINT (XCDR (arg));
26430 return BAR_CURSOR;
26431 }
26432
26433 if (EQ (arg, Qhbar))
26434 {
26435 *width = 2;
26436 return HBAR_CURSOR;
26437 }
26438
26439 if (CONSP (arg)
26440 && EQ (XCAR (arg), Qhbar)
26441 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26442 {
26443 *width = XINT (XCDR (arg));
26444 return HBAR_CURSOR;
26445 }
26446
26447 /* Treat anything unknown as "hollow box cursor".
26448 It was bad to signal an error; people have trouble fixing
26449 .Xdefaults with Emacs, when it has something bad in it. */
26450 type = HOLLOW_BOX_CURSOR;
26451
26452 return type;
26453 }
26454
26455 /* Set the default cursor types for specified frame. */
26456 void
26457 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26458 {
26459 int width = 1;
26460 Lisp_Object tem;
26461
26462 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26463 FRAME_CURSOR_WIDTH (f) = width;
26464
26465 /* By default, set up the blink-off state depending on the on-state. */
26466
26467 tem = Fassoc (arg, Vblink_cursor_alist);
26468 if (!NILP (tem))
26469 {
26470 FRAME_BLINK_OFF_CURSOR (f)
26471 = get_specified_cursor_type (XCDR (tem), &width);
26472 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26473 }
26474 else
26475 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26476
26477 /* Make sure the cursor gets redrawn. */
26478 f->cursor_type_changed = 1;
26479 }
26480
26481
26482 #ifdef HAVE_WINDOW_SYSTEM
26483
26484 /* Return the cursor we want to be displayed in window W. Return
26485 width of bar/hbar cursor through WIDTH arg. Return with
26486 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26487 (i.e. if the `system caret' should track this cursor).
26488
26489 In a mini-buffer window, we want the cursor only to appear if we
26490 are reading input from this window. For the selected window, we
26491 want the cursor type given by the frame parameter or buffer local
26492 setting of cursor-type. If explicitly marked off, draw no cursor.
26493 In all other cases, we want a hollow box cursor. */
26494
26495 static enum text_cursor_kinds
26496 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26497 int *active_cursor)
26498 {
26499 struct frame *f = XFRAME (w->frame);
26500 struct buffer *b = XBUFFER (w->contents);
26501 int cursor_type = DEFAULT_CURSOR;
26502 Lisp_Object alt_cursor;
26503 int non_selected = 0;
26504
26505 *active_cursor = 1;
26506
26507 /* Echo area */
26508 if (cursor_in_echo_area
26509 && FRAME_HAS_MINIBUF_P (f)
26510 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26511 {
26512 if (w == XWINDOW (echo_area_window))
26513 {
26514 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26515 {
26516 *width = FRAME_CURSOR_WIDTH (f);
26517 return FRAME_DESIRED_CURSOR (f);
26518 }
26519 else
26520 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26521 }
26522
26523 *active_cursor = 0;
26524 non_selected = 1;
26525 }
26526
26527 /* Detect a nonselected window or nonselected frame. */
26528 else if (w != XWINDOW (f->selected_window)
26529 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26530 {
26531 *active_cursor = 0;
26532
26533 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26534 return NO_CURSOR;
26535
26536 non_selected = 1;
26537 }
26538
26539 /* Never display a cursor in a window in which cursor-type is nil. */
26540 if (NILP (BVAR (b, cursor_type)))
26541 return NO_CURSOR;
26542
26543 /* Get the normal cursor type for this window. */
26544 if (EQ (BVAR (b, cursor_type), Qt))
26545 {
26546 cursor_type = FRAME_DESIRED_CURSOR (f);
26547 *width = FRAME_CURSOR_WIDTH (f);
26548 }
26549 else
26550 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26551
26552 /* Use cursor-in-non-selected-windows instead
26553 for non-selected window or frame. */
26554 if (non_selected)
26555 {
26556 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26557 if (!EQ (Qt, alt_cursor))
26558 return get_specified_cursor_type (alt_cursor, width);
26559 /* t means modify the normal cursor type. */
26560 if (cursor_type == FILLED_BOX_CURSOR)
26561 cursor_type = HOLLOW_BOX_CURSOR;
26562 else if (cursor_type == BAR_CURSOR && *width > 1)
26563 --*width;
26564 return cursor_type;
26565 }
26566
26567 /* Use normal cursor if not blinked off. */
26568 if (!w->cursor_off_p)
26569 {
26570 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26571 {
26572 if (cursor_type == FILLED_BOX_CURSOR)
26573 {
26574 /* Using a block cursor on large images can be very annoying.
26575 So use a hollow cursor for "large" images.
26576 If image is not transparent (no mask), also use hollow cursor. */
26577 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26578 if (img != NULL && IMAGEP (img->spec))
26579 {
26580 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26581 where N = size of default frame font size.
26582 This should cover most of the "tiny" icons people may use. */
26583 if (!img->mask
26584 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26585 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26586 cursor_type = HOLLOW_BOX_CURSOR;
26587 }
26588 }
26589 else if (cursor_type != NO_CURSOR)
26590 {
26591 /* Display current only supports BOX and HOLLOW cursors for images.
26592 So for now, unconditionally use a HOLLOW cursor when cursor is
26593 not a solid box cursor. */
26594 cursor_type = HOLLOW_BOX_CURSOR;
26595 }
26596 }
26597 return cursor_type;
26598 }
26599
26600 /* Cursor is blinked off, so determine how to "toggle" it. */
26601
26602 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26603 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26604 return get_specified_cursor_type (XCDR (alt_cursor), width);
26605
26606 /* Then see if frame has specified a specific blink off cursor type. */
26607 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26608 {
26609 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26610 return FRAME_BLINK_OFF_CURSOR (f);
26611 }
26612
26613 #if 0
26614 /* Some people liked having a permanently visible blinking cursor,
26615 while others had very strong opinions against it. So it was
26616 decided to remove it. KFS 2003-09-03 */
26617
26618 /* Finally perform built-in cursor blinking:
26619 filled box <-> hollow box
26620 wide [h]bar <-> narrow [h]bar
26621 narrow [h]bar <-> no cursor
26622 other type <-> no cursor */
26623
26624 if (cursor_type == FILLED_BOX_CURSOR)
26625 return HOLLOW_BOX_CURSOR;
26626
26627 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26628 {
26629 *width = 1;
26630 return cursor_type;
26631 }
26632 #endif
26633
26634 return NO_CURSOR;
26635 }
26636
26637
26638 /* Notice when the text cursor of window W has been completely
26639 overwritten by a drawing operation that outputs glyphs in AREA
26640 starting at X0 and ending at X1 in the line starting at Y0 and
26641 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26642 the rest of the line after X0 has been written. Y coordinates
26643 are window-relative. */
26644
26645 static void
26646 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26647 int x0, int x1, int y0, int y1)
26648 {
26649 int cx0, cx1, cy0, cy1;
26650 struct glyph_row *row;
26651
26652 if (!w->phys_cursor_on_p)
26653 return;
26654 if (area != TEXT_AREA)
26655 return;
26656
26657 if (w->phys_cursor.vpos < 0
26658 || w->phys_cursor.vpos >= w->current_matrix->nrows
26659 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26660 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26661 return;
26662
26663 if (row->cursor_in_fringe_p)
26664 {
26665 row->cursor_in_fringe_p = 0;
26666 draw_fringe_bitmap (w, row, row->reversed_p);
26667 w->phys_cursor_on_p = 0;
26668 return;
26669 }
26670
26671 cx0 = w->phys_cursor.x;
26672 cx1 = cx0 + w->phys_cursor_width;
26673 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26674 return;
26675
26676 /* The cursor image will be completely removed from the
26677 screen if the output area intersects the cursor area in
26678 y-direction. When we draw in [y0 y1[, and some part of
26679 the cursor is at y < y0, that part must have been drawn
26680 before. When scrolling, the cursor is erased before
26681 actually scrolling, so we don't come here. When not
26682 scrolling, the rows above the old cursor row must have
26683 changed, and in this case these rows must have written
26684 over the cursor image.
26685
26686 Likewise if part of the cursor is below y1, with the
26687 exception of the cursor being in the first blank row at
26688 the buffer and window end because update_text_area
26689 doesn't draw that row. (Except when it does, but
26690 that's handled in update_text_area.) */
26691
26692 cy0 = w->phys_cursor.y;
26693 cy1 = cy0 + w->phys_cursor_height;
26694 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26695 return;
26696
26697 w->phys_cursor_on_p = 0;
26698 }
26699
26700 #endif /* HAVE_WINDOW_SYSTEM */
26701
26702 \f
26703 /************************************************************************
26704 Mouse Face
26705 ************************************************************************/
26706
26707 #ifdef HAVE_WINDOW_SYSTEM
26708
26709 /* EXPORT for RIF:
26710 Fix the display of area AREA of overlapping row ROW in window W
26711 with respect to the overlapping part OVERLAPS. */
26712
26713 void
26714 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26715 enum glyph_row_area area, int overlaps)
26716 {
26717 int i, x;
26718
26719 block_input ();
26720
26721 x = 0;
26722 for (i = 0; i < row->used[area];)
26723 {
26724 if (row->glyphs[area][i].overlaps_vertically_p)
26725 {
26726 int start = i, start_x = x;
26727
26728 do
26729 {
26730 x += row->glyphs[area][i].pixel_width;
26731 ++i;
26732 }
26733 while (i < row->used[area]
26734 && row->glyphs[area][i].overlaps_vertically_p);
26735
26736 draw_glyphs (w, start_x, row, area,
26737 start, i,
26738 DRAW_NORMAL_TEXT, overlaps);
26739 }
26740 else
26741 {
26742 x += row->glyphs[area][i].pixel_width;
26743 ++i;
26744 }
26745 }
26746
26747 unblock_input ();
26748 }
26749
26750
26751 /* EXPORT:
26752 Draw the cursor glyph of window W in glyph row ROW. See the
26753 comment of draw_glyphs for the meaning of HL. */
26754
26755 void
26756 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26757 enum draw_glyphs_face hl)
26758 {
26759 /* If cursor hpos is out of bounds, don't draw garbage. This can
26760 happen in mini-buffer windows when switching between echo area
26761 glyphs and mini-buffer. */
26762 if ((row->reversed_p
26763 ? (w->phys_cursor.hpos >= 0)
26764 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26765 {
26766 int on_p = w->phys_cursor_on_p;
26767 int x1;
26768 int hpos = w->phys_cursor.hpos;
26769
26770 /* When the window is hscrolled, cursor hpos can legitimately be
26771 out of bounds, but we draw the cursor at the corresponding
26772 window margin in that case. */
26773 if (!row->reversed_p && hpos < 0)
26774 hpos = 0;
26775 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26776 hpos = row->used[TEXT_AREA] - 1;
26777
26778 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26779 hl, 0);
26780 w->phys_cursor_on_p = on_p;
26781
26782 if (hl == DRAW_CURSOR)
26783 w->phys_cursor_width = x1 - w->phys_cursor.x;
26784 /* When we erase the cursor, and ROW is overlapped by other
26785 rows, make sure that these overlapping parts of other rows
26786 are redrawn. */
26787 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26788 {
26789 w->phys_cursor_width = x1 - w->phys_cursor.x;
26790
26791 if (row > w->current_matrix->rows
26792 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26793 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26794 OVERLAPS_ERASED_CURSOR);
26795
26796 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26797 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26798 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26799 OVERLAPS_ERASED_CURSOR);
26800 }
26801 }
26802 }
26803
26804
26805 /* Erase the image of a cursor of window W from the screen. */
26806
26807 #ifndef HAVE_NTGUI
26808 static
26809 #endif
26810 void
26811 erase_phys_cursor (struct window *w)
26812 {
26813 struct frame *f = XFRAME (w->frame);
26814 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26815 int hpos = w->phys_cursor.hpos;
26816 int vpos = w->phys_cursor.vpos;
26817 int mouse_face_here_p = 0;
26818 struct glyph_matrix *active_glyphs = w->current_matrix;
26819 struct glyph_row *cursor_row;
26820 struct glyph *cursor_glyph;
26821 enum draw_glyphs_face hl;
26822
26823 /* No cursor displayed or row invalidated => nothing to do on the
26824 screen. */
26825 if (w->phys_cursor_type == NO_CURSOR)
26826 goto mark_cursor_off;
26827
26828 /* VPOS >= active_glyphs->nrows means that window has been resized.
26829 Don't bother to erase the cursor. */
26830 if (vpos >= active_glyphs->nrows)
26831 goto mark_cursor_off;
26832
26833 /* If row containing cursor is marked invalid, there is nothing we
26834 can do. */
26835 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26836 if (!cursor_row->enabled_p)
26837 goto mark_cursor_off;
26838
26839 /* If line spacing is > 0, old cursor may only be partially visible in
26840 window after split-window. So adjust visible height. */
26841 cursor_row->visible_height = min (cursor_row->visible_height,
26842 window_text_bottom_y (w) - cursor_row->y);
26843
26844 /* If row is completely invisible, don't attempt to delete a cursor which
26845 isn't there. This can happen if cursor is at top of a window, and
26846 we switch to a buffer with a header line in that window. */
26847 if (cursor_row->visible_height <= 0)
26848 goto mark_cursor_off;
26849
26850 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26851 if (cursor_row->cursor_in_fringe_p)
26852 {
26853 cursor_row->cursor_in_fringe_p = 0;
26854 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26855 goto mark_cursor_off;
26856 }
26857
26858 /* This can happen when the new row is shorter than the old one.
26859 In this case, either draw_glyphs or clear_end_of_line
26860 should have cleared the cursor. Note that we wouldn't be
26861 able to erase the cursor in this case because we don't have a
26862 cursor glyph at hand. */
26863 if ((cursor_row->reversed_p
26864 ? (w->phys_cursor.hpos < 0)
26865 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26866 goto mark_cursor_off;
26867
26868 /* When the window is hscrolled, cursor hpos can legitimately be out
26869 of bounds, but we draw the cursor at the corresponding window
26870 margin in that case. */
26871 if (!cursor_row->reversed_p && hpos < 0)
26872 hpos = 0;
26873 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26874 hpos = cursor_row->used[TEXT_AREA] - 1;
26875
26876 /* If the cursor is in the mouse face area, redisplay that when
26877 we clear the cursor. */
26878 if (! NILP (hlinfo->mouse_face_window)
26879 && coords_in_mouse_face_p (w, hpos, vpos)
26880 /* Don't redraw the cursor's spot in mouse face if it is at the
26881 end of a line (on a newline). The cursor appears there, but
26882 mouse highlighting does not. */
26883 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26884 mouse_face_here_p = 1;
26885
26886 /* Maybe clear the display under the cursor. */
26887 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26888 {
26889 int x, y, left_x;
26890 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26891 int width;
26892
26893 cursor_glyph = get_phys_cursor_glyph (w);
26894 if (cursor_glyph == NULL)
26895 goto mark_cursor_off;
26896
26897 width = cursor_glyph->pixel_width;
26898 left_x = window_box_left_offset (w, TEXT_AREA);
26899 x = w->phys_cursor.x;
26900 if (x < left_x)
26901 width -= left_x - x;
26902 width = min (width, window_box_width (w, TEXT_AREA) - x);
26903 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26904 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26905
26906 if (width > 0)
26907 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26908 }
26909
26910 /* Erase the cursor by redrawing the character underneath it. */
26911 if (mouse_face_here_p)
26912 hl = DRAW_MOUSE_FACE;
26913 else
26914 hl = DRAW_NORMAL_TEXT;
26915 draw_phys_cursor_glyph (w, cursor_row, hl);
26916
26917 mark_cursor_off:
26918 w->phys_cursor_on_p = 0;
26919 w->phys_cursor_type = NO_CURSOR;
26920 }
26921
26922
26923 /* EXPORT:
26924 Display or clear cursor of window W. If ON is zero, clear the
26925 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26926 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26927
26928 void
26929 display_and_set_cursor (struct window *w, bool on,
26930 int hpos, int vpos, int x, int y)
26931 {
26932 struct frame *f = XFRAME (w->frame);
26933 int new_cursor_type;
26934 int new_cursor_width;
26935 int active_cursor;
26936 struct glyph_row *glyph_row;
26937 struct glyph *glyph;
26938
26939 /* This is pointless on invisible frames, and dangerous on garbaged
26940 windows and frames; in the latter case, the frame or window may
26941 be in the midst of changing its size, and x and y may be off the
26942 window. */
26943 if (! FRAME_VISIBLE_P (f)
26944 || FRAME_GARBAGED_P (f)
26945 || vpos >= w->current_matrix->nrows
26946 || hpos >= w->current_matrix->matrix_w)
26947 return;
26948
26949 /* If cursor is off and we want it off, return quickly. */
26950 if (!on && !w->phys_cursor_on_p)
26951 return;
26952
26953 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26954 /* If cursor row is not enabled, we don't really know where to
26955 display the cursor. */
26956 if (!glyph_row->enabled_p)
26957 {
26958 w->phys_cursor_on_p = 0;
26959 return;
26960 }
26961
26962 glyph = NULL;
26963 if (!glyph_row->exact_window_width_line_p
26964 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26965 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26966
26967 eassert (input_blocked_p ());
26968
26969 /* Set new_cursor_type to the cursor we want to be displayed. */
26970 new_cursor_type = get_window_cursor_type (w, glyph,
26971 &new_cursor_width, &active_cursor);
26972
26973 /* If cursor is currently being shown and we don't want it to be or
26974 it is in the wrong place, or the cursor type is not what we want,
26975 erase it. */
26976 if (w->phys_cursor_on_p
26977 && (!on
26978 || w->phys_cursor.x != x
26979 || w->phys_cursor.y != y
26980 || new_cursor_type != w->phys_cursor_type
26981 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26982 && new_cursor_width != w->phys_cursor_width)))
26983 erase_phys_cursor (w);
26984
26985 /* Don't check phys_cursor_on_p here because that flag is only set
26986 to zero in some cases where we know that the cursor has been
26987 completely erased, to avoid the extra work of erasing the cursor
26988 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26989 still not be visible, or it has only been partly erased. */
26990 if (on)
26991 {
26992 w->phys_cursor_ascent = glyph_row->ascent;
26993 w->phys_cursor_height = glyph_row->height;
26994
26995 /* Set phys_cursor_.* before x_draw_.* is called because some
26996 of them may need the information. */
26997 w->phys_cursor.x = x;
26998 w->phys_cursor.y = glyph_row->y;
26999 w->phys_cursor.hpos = hpos;
27000 w->phys_cursor.vpos = vpos;
27001 }
27002
27003 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27004 new_cursor_type, new_cursor_width,
27005 on, active_cursor);
27006 }
27007
27008
27009 /* Switch the display of W's cursor on or off, according to the value
27010 of ON. */
27011
27012 static void
27013 update_window_cursor (struct window *w, bool on)
27014 {
27015 /* Don't update cursor in windows whose frame is in the process
27016 of being deleted. */
27017 if (w->current_matrix)
27018 {
27019 int hpos = w->phys_cursor.hpos;
27020 int vpos = w->phys_cursor.vpos;
27021 struct glyph_row *row;
27022
27023 if (vpos >= w->current_matrix->nrows
27024 || hpos >= w->current_matrix->matrix_w)
27025 return;
27026
27027 row = MATRIX_ROW (w->current_matrix, vpos);
27028
27029 /* When the window is hscrolled, cursor hpos can legitimately be
27030 out of bounds, but we draw the cursor at the corresponding
27031 window margin in that case. */
27032 if (!row->reversed_p && hpos < 0)
27033 hpos = 0;
27034 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27035 hpos = row->used[TEXT_AREA] - 1;
27036
27037 block_input ();
27038 display_and_set_cursor (w, on, hpos, vpos,
27039 w->phys_cursor.x, w->phys_cursor.y);
27040 unblock_input ();
27041 }
27042 }
27043
27044
27045 /* Call update_window_cursor with parameter ON_P on all leaf windows
27046 in the window tree rooted at W. */
27047
27048 static void
27049 update_cursor_in_window_tree (struct window *w, bool on_p)
27050 {
27051 while (w)
27052 {
27053 if (WINDOWP (w->contents))
27054 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27055 else
27056 update_window_cursor (w, on_p);
27057
27058 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27059 }
27060 }
27061
27062
27063 /* EXPORT:
27064 Display the cursor on window W, or clear it, according to ON_P.
27065 Don't change the cursor's position. */
27066
27067 void
27068 x_update_cursor (struct frame *f, bool on_p)
27069 {
27070 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27071 }
27072
27073
27074 /* EXPORT:
27075 Clear the cursor of window W to background color, and mark the
27076 cursor as not shown. This is used when the text where the cursor
27077 is about to be rewritten. */
27078
27079 void
27080 x_clear_cursor (struct window *w)
27081 {
27082 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27083 update_window_cursor (w, 0);
27084 }
27085
27086 #endif /* HAVE_WINDOW_SYSTEM */
27087
27088 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27089 and MSDOS. */
27090 static void
27091 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27092 int start_hpos, int end_hpos,
27093 enum draw_glyphs_face draw)
27094 {
27095 #ifdef HAVE_WINDOW_SYSTEM
27096 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27097 {
27098 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27099 return;
27100 }
27101 #endif
27102 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27103 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27104 #endif
27105 }
27106
27107 /* Display the active region described by mouse_face_* according to DRAW. */
27108
27109 static void
27110 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27111 {
27112 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27113 struct frame *f = XFRAME (WINDOW_FRAME (w));
27114
27115 if (/* If window is in the process of being destroyed, don't bother
27116 to do anything. */
27117 w->current_matrix != NULL
27118 /* Don't update mouse highlight if hidden */
27119 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27120 /* Recognize when we are called to operate on rows that don't exist
27121 anymore. This can happen when a window is split. */
27122 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27123 {
27124 int phys_cursor_on_p = w->phys_cursor_on_p;
27125 struct glyph_row *row, *first, *last;
27126
27127 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27128 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27129
27130 for (row = first; row <= last && row->enabled_p; ++row)
27131 {
27132 int start_hpos, end_hpos, start_x;
27133
27134 /* For all but the first row, the highlight starts at column 0. */
27135 if (row == first)
27136 {
27137 /* R2L rows have BEG and END in reversed order, but the
27138 screen drawing geometry is always left to right. So
27139 we need to mirror the beginning and end of the
27140 highlighted area in R2L rows. */
27141 if (!row->reversed_p)
27142 {
27143 start_hpos = hlinfo->mouse_face_beg_col;
27144 start_x = hlinfo->mouse_face_beg_x;
27145 }
27146 else if (row == last)
27147 {
27148 start_hpos = hlinfo->mouse_face_end_col;
27149 start_x = hlinfo->mouse_face_end_x;
27150 }
27151 else
27152 {
27153 start_hpos = 0;
27154 start_x = 0;
27155 }
27156 }
27157 else if (row->reversed_p && row == last)
27158 {
27159 start_hpos = hlinfo->mouse_face_end_col;
27160 start_x = hlinfo->mouse_face_end_x;
27161 }
27162 else
27163 {
27164 start_hpos = 0;
27165 start_x = 0;
27166 }
27167
27168 if (row == last)
27169 {
27170 if (!row->reversed_p)
27171 end_hpos = hlinfo->mouse_face_end_col;
27172 else if (row == first)
27173 end_hpos = hlinfo->mouse_face_beg_col;
27174 else
27175 {
27176 end_hpos = row->used[TEXT_AREA];
27177 if (draw == DRAW_NORMAL_TEXT)
27178 row->fill_line_p = 1; /* Clear to end of line */
27179 }
27180 }
27181 else if (row->reversed_p && row == first)
27182 end_hpos = hlinfo->mouse_face_beg_col;
27183 else
27184 {
27185 end_hpos = row->used[TEXT_AREA];
27186 if (draw == DRAW_NORMAL_TEXT)
27187 row->fill_line_p = 1; /* Clear to end of line */
27188 }
27189
27190 if (end_hpos > start_hpos)
27191 {
27192 draw_row_with_mouse_face (w, start_x, row,
27193 start_hpos, end_hpos, draw);
27194
27195 row->mouse_face_p
27196 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27197 }
27198 }
27199
27200 #ifdef HAVE_WINDOW_SYSTEM
27201 /* When we've written over the cursor, arrange for it to
27202 be displayed again. */
27203 if (FRAME_WINDOW_P (f)
27204 && phys_cursor_on_p && !w->phys_cursor_on_p)
27205 {
27206 int hpos = w->phys_cursor.hpos;
27207
27208 /* When the window is hscrolled, cursor hpos can legitimately be
27209 out of bounds, but we draw the cursor at the corresponding
27210 window margin in that case. */
27211 if (!row->reversed_p && hpos < 0)
27212 hpos = 0;
27213 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27214 hpos = row->used[TEXT_AREA] - 1;
27215
27216 block_input ();
27217 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27218 w->phys_cursor.x, w->phys_cursor.y);
27219 unblock_input ();
27220 }
27221 #endif /* HAVE_WINDOW_SYSTEM */
27222 }
27223
27224 #ifdef HAVE_WINDOW_SYSTEM
27225 /* Change the mouse cursor. */
27226 if (FRAME_WINDOW_P (f))
27227 {
27228 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27229 if (draw == DRAW_NORMAL_TEXT
27230 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27231 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27232 else
27233 #endif
27234 if (draw == DRAW_MOUSE_FACE)
27235 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27236 else
27237 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27238 }
27239 #endif /* HAVE_WINDOW_SYSTEM */
27240 }
27241
27242 /* EXPORT:
27243 Clear out the mouse-highlighted active region.
27244 Redraw it un-highlighted first. Value is non-zero if mouse
27245 face was actually drawn unhighlighted. */
27246
27247 int
27248 clear_mouse_face (Mouse_HLInfo *hlinfo)
27249 {
27250 int cleared = 0;
27251
27252 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27253 {
27254 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27255 cleared = 1;
27256 }
27257
27258 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27259 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27260 hlinfo->mouse_face_window = Qnil;
27261 hlinfo->mouse_face_overlay = Qnil;
27262 return cleared;
27263 }
27264
27265 /* Return true if the coordinates HPOS and VPOS on windows W are
27266 within the mouse face on that window. */
27267 static bool
27268 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27269 {
27270 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27271
27272 /* Quickly resolve the easy cases. */
27273 if (!(WINDOWP (hlinfo->mouse_face_window)
27274 && XWINDOW (hlinfo->mouse_face_window) == w))
27275 return false;
27276 if (vpos < hlinfo->mouse_face_beg_row
27277 || vpos > hlinfo->mouse_face_end_row)
27278 return false;
27279 if (vpos > hlinfo->mouse_face_beg_row
27280 && vpos < hlinfo->mouse_face_end_row)
27281 return true;
27282
27283 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27284 {
27285 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27286 {
27287 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27288 return true;
27289 }
27290 else if ((vpos == hlinfo->mouse_face_beg_row
27291 && hpos >= hlinfo->mouse_face_beg_col)
27292 || (vpos == hlinfo->mouse_face_end_row
27293 && hpos < hlinfo->mouse_face_end_col))
27294 return true;
27295 }
27296 else
27297 {
27298 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27299 {
27300 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27301 return true;
27302 }
27303 else if ((vpos == hlinfo->mouse_face_beg_row
27304 && hpos <= hlinfo->mouse_face_beg_col)
27305 || (vpos == hlinfo->mouse_face_end_row
27306 && hpos > hlinfo->mouse_face_end_col))
27307 return true;
27308 }
27309 return false;
27310 }
27311
27312
27313 /* EXPORT:
27314 True if physical cursor of window W is within mouse face. */
27315
27316 bool
27317 cursor_in_mouse_face_p (struct window *w)
27318 {
27319 int hpos = w->phys_cursor.hpos;
27320 int vpos = w->phys_cursor.vpos;
27321 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27322
27323 /* When the window is hscrolled, cursor hpos can legitimately be out
27324 of bounds, but we draw the cursor at the corresponding window
27325 margin in that case. */
27326 if (!row->reversed_p && hpos < 0)
27327 hpos = 0;
27328 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27329 hpos = row->used[TEXT_AREA] - 1;
27330
27331 return coords_in_mouse_face_p (w, hpos, vpos);
27332 }
27333
27334
27335 \f
27336 /* Find the glyph rows START_ROW and END_ROW of window W that display
27337 characters between buffer positions START_CHARPOS and END_CHARPOS
27338 (excluding END_CHARPOS). DISP_STRING is a display string that
27339 covers these buffer positions. This is similar to
27340 row_containing_pos, but is more accurate when bidi reordering makes
27341 buffer positions change non-linearly with glyph rows. */
27342 static void
27343 rows_from_pos_range (struct window *w,
27344 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27345 Lisp_Object disp_string,
27346 struct glyph_row **start, struct glyph_row **end)
27347 {
27348 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27349 int last_y = window_text_bottom_y (w);
27350 struct glyph_row *row;
27351
27352 *start = NULL;
27353 *end = NULL;
27354
27355 while (!first->enabled_p
27356 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27357 first++;
27358
27359 /* Find the START row. */
27360 for (row = first;
27361 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27362 row++)
27363 {
27364 /* A row can potentially be the START row if the range of the
27365 characters it displays intersects the range
27366 [START_CHARPOS..END_CHARPOS). */
27367 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27368 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27369 /* See the commentary in row_containing_pos, for the
27370 explanation of the complicated way to check whether
27371 some position is beyond the end of the characters
27372 displayed by a row. */
27373 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27374 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27375 && !row->ends_at_zv_p
27376 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27377 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27378 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27379 && !row->ends_at_zv_p
27380 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27381 {
27382 /* Found a candidate row. Now make sure at least one of the
27383 glyphs it displays has a charpos from the range
27384 [START_CHARPOS..END_CHARPOS).
27385
27386 This is not obvious because bidi reordering could make
27387 buffer positions of a row be 1,2,3,102,101,100, and if we
27388 want to highlight characters in [50..60), we don't want
27389 this row, even though [50..60) does intersect [1..103),
27390 the range of character positions given by the row's start
27391 and end positions. */
27392 struct glyph *g = row->glyphs[TEXT_AREA];
27393 struct glyph *e = g + row->used[TEXT_AREA];
27394
27395 while (g < e)
27396 {
27397 if (((BUFFERP (g->object) || INTEGERP (g->object))
27398 && start_charpos <= g->charpos && g->charpos < end_charpos)
27399 /* A glyph that comes from DISP_STRING is by
27400 definition to be highlighted. */
27401 || EQ (g->object, disp_string))
27402 *start = row;
27403 g++;
27404 }
27405 if (*start)
27406 break;
27407 }
27408 }
27409
27410 /* Find the END row. */
27411 if (!*start
27412 /* If the last row is partially visible, start looking for END
27413 from that row, instead of starting from FIRST. */
27414 && !(row->enabled_p
27415 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27416 row = first;
27417 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27418 {
27419 struct glyph_row *next = row + 1;
27420 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27421
27422 if (!next->enabled_p
27423 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27424 /* The first row >= START whose range of displayed characters
27425 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27426 is the row END + 1. */
27427 || (start_charpos < next_start
27428 && end_charpos < next_start)
27429 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27430 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27431 && !next->ends_at_zv_p
27432 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27433 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27434 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27435 && !next->ends_at_zv_p
27436 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27437 {
27438 *end = row;
27439 break;
27440 }
27441 else
27442 {
27443 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27444 but none of the characters it displays are in the range, it is
27445 also END + 1. */
27446 struct glyph *g = next->glyphs[TEXT_AREA];
27447 struct glyph *s = g;
27448 struct glyph *e = g + next->used[TEXT_AREA];
27449
27450 while (g < e)
27451 {
27452 if (((BUFFERP (g->object) || INTEGERP (g->object))
27453 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27454 /* If the buffer position of the first glyph in
27455 the row is equal to END_CHARPOS, it means
27456 the last character to be highlighted is the
27457 newline of ROW, and we must consider NEXT as
27458 END, not END+1. */
27459 || (((!next->reversed_p && g == s)
27460 || (next->reversed_p && g == e - 1))
27461 && (g->charpos == end_charpos
27462 /* Special case for when NEXT is an
27463 empty line at ZV. */
27464 || (g->charpos == -1
27465 && !row->ends_at_zv_p
27466 && next_start == end_charpos)))))
27467 /* A glyph that comes from DISP_STRING is by
27468 definition to be highlighted. */
27469 || EQ (g->object, disp_string))
27470 break;
27471 g++;
27472 }
27473 if (g == e)
27474 {
27475 *end = row;
27476 break;
27477 }
27478 /* The first row that ends at ZV must be the last to be
27479 highlighted. */
27480 else if (next->ends_at_zv_p)
27481 {
27482 *end = next;
27483 break;
27484 }
27485 }
27486 }
27487 }
27488
27489 /* This function sets the mouse_face_* elements of HLINFO, assuming
27490 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27491 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27492 for the overlay or run of text properties specifying the mouse
27493 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27494 before-string and after-string that must also be highlighted.
27495 DISP_STRING, if non-nil, is a display string that may cover some
27496 or all of the highlighted text. */
27497
27498 static void
27499 mouse_face_from_buffer_pos (Lisp_Object window,
27500 Mouse_HLInfo *hlinfo,
27501 ptrdiff_t mouse_charpos,
27502 ptrdiff_t start_charpos,
27503 ptrdiff_t end_charpos,
27504 Lisp_Object before_string,
27505 Lisp_Object after_string,
27506 Lisp_Object disp_string)
27507 {
27508 struct window *w = XWINDOW (window);
27509 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27510 struct glyph_row *r1, *r2;
27511 struct glyph *glyph, *end;
27512 ptrdiff_t ignore, pos;
27513 int x;
27514
27515 eassert (NILP (disp_string) || STRINGP (disp_string));
27516 eassert (NILP (before_string) || STRINGP (before_string));
27517 eassert (NILP (after_string) || STRINGP (after_string));
27518
27519 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27520 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27521 if (r1 == NULL)
27522 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27523 /* If the before-string or display-string contains newlines,
27524 rows_from_pos_range skips to its last row. Move back. */
27525 if (!NILP (before_string) || !NILP (disp_string))
27526 {
27527 struct glyph_row *prev;
27528 while ((prev = r1 - 1, prev >= first)
27529 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27530 && prev->used[TEXT_AREA] > 0)
27531 {
27532 struct glyph *beg = prev->glyphs[TEXT_AREA];
27533 glyph = beg + prev->used[TEXT_AREA];
27534 while (--glyph >= beg && INTEGERP (glyph->object));
27535 if (glyph < beg
27536 || !(EQ (glyph->object, before_string)
27537 || EQ (glyph->object, disp_string)))
27538 break;
27539 r1 = prev;
27540 }
27541 }
27542 if (r2 == NULL)
27543 {
27544 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27545 hlinfo->mouse_face_past_end = 1;
27546 }
27547 else if (!NILP (after_string))
27548 {
27549 /* If the after-string has newlines, advance to its last row. */
27550 struct glyph_row *next;
27551 struct glyph_row *last
27552 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27553
27554 for (next = r2 + 1;
27555 next <= last
27556 && next->used[TEXT_AREA] > 0
27557 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27558 ++next)
27559 r2 = next;
27560 }
27561 /* The rest of the display engine assumes that mouse_face_beg_row is
27562 either above mouse_face_end_row or identical to it. But with
27563 bidi-reordered continued lines, the row for START_CHARPOS could
27564 be below the row for END_CHARPOS. If so, swap the rows and store
27565 them in correct order. */
27566 if (r1->y > r2->y)
27567 {
27568 struct glyph_row *tem = r2;
27569
27570 r2 = r1;
27571 r1 = tem;
27572 }
27573
27574 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27575 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27576
27577 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27578 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27579 could be anywhere in the row and in any order. The strategy
27580 below is to find the leftmost and the rightmost glyph that
27581 belongs to either of these 3 strings, or whose position is
27582 between START_CHARPOS and END_CHARPOS, and highlight all the
27583 glyphs between those two. This may cover more than just the text
27584 between START_CHARPOS and END_CHARPOS if the range of characters
27585 strides the bidi level boundary, e.g. if the beginning is in R2L
27586 text while the end is in L2R text or vice versa. */
27587 if (!r1->reversed_p)
27588 {
27589 /* This row is in a left to right paragraph. Scan it left to
27590 right. */
27591 glyph = r1->glyphs[TEXT_AREA];
27592 end = glyph + r1->used[TEXT_AREA];
27593 x = r1->x;
27594
27595 /* Skip truncation glyphs at the start of the glyph row. */
27596 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27597 for (; glyph < end
27598 && INTEGERP (glyph->object)
27599 && glyph->charpos < 0;
27600 ++glyph)
27601 x += glyph->pixel_width;
27602
27603 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27604 or DISP_STRING, and the first glyph from buffer whose
27605 position is between START_CHARPOS and END_CHARPOS. */
27606 for (; glyph < end
27607 && !INTEGERP (glyph->object)
27608 && !EQ (glyph->object, disp_string)
27609 && !(BUFFERP (glyph->object)
27610 && (glyph->charpos >= start_charpos
27611 && glyph->charpos < end_charpos));
27612 ++glyph)
27613 {
27614 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27615 are present at buffer positions between START_CHARPOS and
27616 END_CHARPOS, or if they come from an overlay. */
27617 if (EQ (glyph->object, before_string))
27618 {
27619 pos = string_buffer_position (before_string,
27620 start_charpos);
27621 /* If pos == 0, it means before_string came from an
27622 overlay, not from a buffer position. */
27623 if (!pos || (pos >= start_charpos && pos < end_charpos))
27624 break;
27625 }
27626 else if (EQ (glyph->object, after_string))
27627 {
27628 pos = string_buffer_position (after_string, end_charpos);
27629 if (!pos || (pos >= start_charpos && pos < end_charpos))
27630 break;
27631 }
27632 x += glyph->pixel_width;
27633 }
27634 hlinfo->mouse_face_beg_x = x;
27635 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27636 }
27637 else
27638 {
27639 /* This row is in a right to left paragraph. Scan it right to
27640 left. */
27641 struct glyph *g;
27642
27643 end = r1->glyphs[TEXT_AREA] - 1;
27644 glyph = end + r1->used[TEXT_AREA];
27645
27646 /* Skip truncation glyphs at the start of the glyph row. */
27647 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27648 for (; glyph > end
27649 && INTEGERP (glyph->object)
27650 && glyph->charpos < 0;
27651 --glyph)
27652 ;
27653
27654 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27655 or DISP_STRING, and the first glyph from buffer whose
27656 position is between START_CHARPOS and END_CHARPOS. */
27657 for (; glyph > end
27658 && !INTEGERP (glyph->object)
27659 && !EQ (glyph->object, disp_string)
27660 && !(BUFFERP (glyph->object)
27661 && (glyph->charpos >= start_charpos
27662 && glyph->charpos < end_charpos));
27663 --glyph)
27664 {
27665 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27666 are present at buffer positions between START_CHARPOS and
27667 END_CHARPOS, or if they come from an overlay. */
27668 if (EQ (glyph->object, before_string))
27669 {
27670 pos = string_buffer_position (before_string, start_charpos);
27671 /* If pos == 0, it means before_string came from an
27672 overlay, not from a buffer position. */
27673 if (!pos || (pos >= start_charpos && pos < end_charpos))
27674 break;
27675 }
27676 else if (EQ (glyph->object, after_string))
27677 {
27678 pos = string_buffer_position (after_string, end_charpos);
27679 if (!pos || (pos >= start_charpos && pos < end_charpos))
27680 break;
27681 }
27682 }
27683
27684 glyph++; /* first glyph to the right of the highlighted area */
27685 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27686 x += g->pixel_width;
27687 hlinfo->mouse_face_beg_x = x;
27688 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27689 }
27690
27691 /* If the highlight ends in a different row, compute GLYPH and END
27692 for the end row. Otherwise, reuse the values computed above for
27693 the row where the highlight begins. */
27694 if (r2 != r1)
27695 {
27696 if (!r2->reversed_p)
27697 {
27698 glyph = r2->glyphs[TEXT_AREA];
27699 end = glyph + r2->used[TEXT_AREA];
27700 x = r2->x;
27701 }
27702 else
27703 {
27704 end = r2->glyphs[TEXT_AREA] - 1;
27705 glyph = end + r2->used[TEXT_AREA];
27706 }
27707 }
27708
27709 if (!r2->reversed_p)
27710 {
27711 /* Skip truncation and continuation glyphs near the end of the
27712 row, and also blanks and stretch glyphs inserted by
27713 extend_face_to_end_of_line. */
27714 while (end > glyph
27715 && INTEGERP ((end - 1)->object))
27716 --end;
27717 /* Scan the rest of the glyph row from the end, looking for the
27718 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27719 DISP_STRING, or whose position is between START_CHARPOS
27720 and END_CHARPOS */
27721 for (--end;
27722 end > glyph
27723 && !INTEGERP (end->object)
27724 && !EQ (end->object, disp_string)
27725 && !(BUFFERP (end->object)
27726 && (end->charpos >= start_charpos
27727 && end->charpos < end_charpos));
27728 --end)
27729 {
27730 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27731 are present at buffer positions between START_CHARPOS and
27732 END_CHARPOS, or if they come from an overlay. */
27733 if (EQ (end->object, before_string))
27734 {
27735 pos = string_buffer_position (before_string, start_charpos);
27736 if (!pos || (pos >= start_charpos && pos < end_charpos))
27737 break;
27738 }
27739 else if (EQ (end->object, after_string))
27740 {
27741 pos = string_buffer_position (after_string, end_charpos);
27742 if (!pos || (pos >= start_charpos && pos < end_charpos))
27743 break;
27744 }
27745 }
27746 /* Find the X coordinate of the last glyph to be highlighted. */
27747 for (; glyph <= end; ++glyph)
27748 x += glyph->pixel_width;
27749
27750 hlinfo->mouse_face_end_x = x;
27751 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27752 }
27753 else
27754 {
27755 /* Skip truncation and continuation glyphs near the end of the
27756 row, and also blanks and stretch glyphs inserted by
27757 extend_face_to_end_of_line. */
27758 x = r2->x;
27759 end++;
27760 while (end < glyph
27761 && INTEGERP (end->object))
27762 {
27763 x += end->pixel_width;
27764 ++end;
27765 }
27766 /* Scan the rest of the glyph row from the end, looking for the
27767 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27768 DISP_STRING, or whose position is between START_CHARPOS
27769 and END_CHARPOS */
27770 for ( ;
27771 end < glyph
27772 && !INTEGERP (end->object)
27773 && !EQ (end->object, disp_string)
27774 && !(BUFFERP (end->object)
27775 && (end->charpos >= start_charpos
27776 && end->charpos < end_charpos));
27777 ++end)
27778 {
27779 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27780 are present at buffer positions between START_CHARPOS and
27781 END_CHARPOS, or if they come from an overlay. */
27782 if (EQ (end->object, before_string))
27783 {
27784 pos = string_buffer_position (before_string, start_charpos);
27785 if (!pos || (pos >= start_charpos && pos < end_charpos))
27786 break;
27787 }
27788 else if (EQ (end->object, after_string))
27789 {
27790 pos = string_buffer_position (after_string, end_charpos);
27791 if (!pos || (pos >= start_charpos && pos < end_charpos))
27792 break;
27793 }
27794 x += end->pixel_width;
27795 }
27796 /* If we exited the above loop because we arrived at the last
27797 glyph of the row, and its buffer position is still not in
27798 range, it means the last character in range is the preceding
27799 newline. Bump the end column and x values to get past the
27800 last glyph. */
27801 if (end == glyph
27802 && BUFFERP (end->object)
27803 && (end->charpos < start_charpos
27804 || end->charpos >= end_charpos))
27805 {
27806 x += end->pixel_width;
27807 ++end;
27808 }
27809 hlinfo->mouse_face_end_x = x;
27810 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27811 }
27812
27813 hlinfo->mouse_face_window = window;
27814 hlinfo->mouse_face_face_id
27815 = face_at_buffer_position (w, mouse_charpos, &ignore,
27816 mouse_charpos + 1,
27817 !hlinfo->mouse_face_hidden, -1);
27818 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27819 }
27820
27821 /* The following function is not used anymore (replaced with
27822 mouse_face_from_string_pos), but I leave it here for the time
27823 being, in case someone would. */
27824
27825 #if 0 /* not used */
27826
27827 /* Find the position of the glyph for position POS in OBJECT in
27828 window W's current matrix, and return in *X, *Y the pixel
27829 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27830
27831 RIGHT_P non-zero means return the position of the right edge of the
27832 glyph, RIGHT_P zero means return the left edge position.
27833
27834 If no glyph for POS exists in the matrix, return the position of
27835 the glyph with the next smaller position that is in the matrix, if
27836 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27837 exists in the matrix, return the position of the glyph with the
27838 next larger position in OBJECT.
27839
27840 Value is non-zero if a glyph was found. */
27841
27842 static int
27843 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27844 int *hpos, int *vpos, int *x, int *y, int right_p)
27845 {
27846 int yb = window_text_bottom_y (w);
27847 struct glyph_row *r;
27848 struct glyph *best_glyph = NULL;
27849 struct glyph_row *best_row = NULL;
27850 int best_x = 0;
27851
27852 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27853 r->enabled_p && r->y < yb;
27854 ++r)
27855 {
27856 struct glyph *g = r->glyphs[TEXT_AREA];
27857 struct glyph *e = g + r->used[TEXT_AREA];
27858 int gx;
27859
27860 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27861 if (EQ (g->object, object))
27862 {
27863 if (g->charpos == pos)
27864 {
27865 best_glyph = g;
27866 best_x = gx;
27867 best_row = r;
27868 goto found;
27869 }
27870 else if (best_glyph == NULL
27871 || ((eabs (g->charpos - pos)
27872 < eabs (best_glyph->charpos - pos))
27873 && (right_p
27874 ? g->charpos < pos
27875 : g->charpos > pos)))
27876 {
27877 best_glyph = g;
27878 best_x = gx;
27879 best_row = r;
27880 }
27881 }
27882 }
27883
27884 found:
27885
27886 if (best_glyph)
27887 {
27888 *x = best_x;
27889 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27890
27891 if (right_p)
27892 {
27893 *x += best_glyph->pixel_width;
27894 ++*hpos;
27895 }
27896
27897 *y = best_row->y;
27898 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27899 }
27900
27901 return best_glyph != NULL;
27902 }
27903 #endif /* not used */
27904
27905 /* Find the positions of the first and the last glyphs in window W's
27906 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27907 (assumed to be a string), and return in HLINFO's mouse_face_*
27908 members the pixel and column/row coordinates of those glyphs. */
27909
27910 static void
27911 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27912 Lisp_Object object,
27913 ptrdiff_t startpos, ptrdiff_t endpos)
27914 {
27915 int yb = window_text_bottom_y (w);
27916 struct glyph_row *r;
27917 struct glyph *g, *e;
27918 int gx;
27919 int found = 0;
27920
27921 /* Find the glyph row with at least one position in the range
27922 [STARTPOS..ENDPOS), and the first glyph in that row whose
27923 position belongs to that range. */
27924 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27925 r->enabled_p && r->y < yb;
27926 ++r)
27927 {
27928 if (!r->reversed_p)
27929 {
27930 g = r->glyphs[TEXT_AREA];
27931 e = g + r->used[TEXT_AREA];
27932 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27933 if (EQ (g->object, object)
27934 && startpos <= g->charpos && g->charpos < endpos)
27935 {
27936 hlinfo->mouse_face_beg_row
27937 = MATRIX_ROW_VPOS (r, w->current_matrix);
27938 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27939 hlinfo->mouse_face_beg_x = gx;
27940 found = 1;
27941 break;
27942 }
27943 }
27944 else
27945 {
27946 struct glyph *g1;
27947
27948 e = r->glyphs[TEXT_AREA];
27949 g = e + r->used[TEXT_AREA];
27950 for ( ; g > e; --g)
27951 if (EQ ((g-1)->object, object)
27952 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27953 {
27954 hlinfo->mouse_face_beg_row
27955 = MATRIX_ROW_VPOS (r, w->current_matrix);
27956 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27957 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27958 gx += g1->pixel_width;
27959 hlinfo->mouse_face_beg_x = gx;
27960 found = 1;
27961 break;
27962 }
27963 }
27964 if (found)
27965 break;
27966 }
27967
27968 if (!found)
27969 return;
27970
27971 /* Starting with the next row, look for the first row which does NOT
27972 include any glyphs whose positions are in the range. */
27973 for (++r; r->enabled_p && r->y < yb; ++r)
27974 {
27975 g = r->glyphs[TEXT_AREA];
27976 e = g + r->used[TEXT_AREA];
27977 found = 0;
27978 for ( ; g < e; ++g)
27979 if (EQ (g->object, object)
27980 && startpos <= g->charpos && g->charpos < endpos)
27981 {
27982 found = 1;
27983 break;
27984 }
27985 if (!found)
27986 break;
27987 }
27988
27989 /* The highlighted region ends on the previous row. */
27990 r--;
27991
27992 /* Set the end row. */
27993 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27994
27995 /* Compute and set the end column and the end column's horizontal
27996 pixel coordinate. */
27997 if (!r->reversed_p)
27998 {
27999 g = r->glyphs[TEXT_AREA];
28000 e = g + r->used[TEXT_AREA];
28001 for ( ; e > g; --e)
28002 if (EQ ((e-1)->object, object)
28003 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28004 break;
28005 hlinfo->mouse_face_end_col = e - g;
28006
28007 for (gx = r->x; g < e; ++g)
28008 gx += g->pixel_width;
28009 hlinfo->mouse_face_end_x = gx;
28010 }
28011 else
28012 {
28013 e = r->glyphs[TEXT_AREA];
28014 g = e + r->used[TEXT_AREA];
28015 for (gx = r->x ; e < g; ++e)
28016 {
28017 if (EQ (e->object, object)
28018 && startpos <= e->charpos && e->charpos < endpos)
28019 break;
28020 gx += e->pixel_width;
28021 }
28022 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28023 hlinfo->mouse_face_end_x = gx;
28024 }
28025 }
28026
28027 #ifdef HAVE_WINDOW_SYSTEM
28028
28029 /* See if position X, Y is within a hot-spot of an image. */
28030
28031 static int
28032 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28033 {
28034 if (!CONSP (hot_spot))
28035 return 0;
28036
28037 if (EQ (XCAR (hot_spot), Qrect))
28038 {
28039 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28040 Lisp_Object rect = XCDR (hot_spot);
28041 Lisp_Object tem;
28042 if (!CONSP (rect))
28043 return 0;
28044 if (!CONSP (XCAR (rect)))
28045 return 0;
28046 if (!CONSP (XCDR (rect)))
28047 return 0;
28048 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28049 return 0;
28050 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28051 return 0;
28052 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28053 return 0;
28054 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28055 return 0;
28056 return 1;
28057 }
28058 else if (EQ (XCAR (hot_spot), Qcircle))
28059 {
28060 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28061 Lisp_Object circ = XCDR (hot_spot);
28062 Lisp_Object lr, lx0, ly0;
28063 if (CONSP (circ)
28064 && CONSP (XCAR (circ))
28065 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28066 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28067 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28068 {
28069 double r = XFLOATINT (lr);
28070 double dx = XINT (lx0) - x;
28071 double dy = XINT (ly0) - y;
28072 return (dx * dx + dy * dy <= r * r);
28073 }
28074 }
28075 else if (EQ (XCAR (hot_spot), Qpoly))
28076 {
28077 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28078 if (VECTORP (XCDR (hot_spot)))
28079 {
28080 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28081 Lisp_Object *poly = v->contents;
28082 ptrdiff_t n = v->header.size;
28083 ptrdiff_t i;
28084 int inside = 0;
28085 Lisp_Object lx, ly;
28086 int x0, y0;
28087
28088 /* Need an even number of coordinates, and at least 3 edges. */
28089 if (n < 6 || n & 1)
28090 return 0;
28091
28092 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28093 If count is odd, we are inside polygon. Pixels on edges
28094 may or may not be included depending on actual geometry of the
28095 polygon. */
28096 if ((lx = poly[n-2], !INTEGERP (lx))
28097 || (ly = poly[n-1], !INTEGERP (lx)))
28098 return 0;
28099 x0 = XINT (lx), y0 = XINT (ly);
28100 for (i = 0; i < n; i += 2)
28101 {
28102 int x1 = x0, y1 = y0;
28103 if ((lx = poly[i], !INTEGERP (lx))
28104 || (ly = poly[i+1], !INTEGERP (ly)))
28105 return 0;
28106 x0 = XINT (lx), y0 = XINT (ly);
28107
28108 /* Does this segment cross the X line? */
28109 if (x0 >= x)
28110 {
28111 if (x1 >= x)
28112 continue;
28113 }
28114 else if (x1 < x)
28115 continue;
28116 if (y > y0 && y > y1)
28117 continue;
28118 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28119 inside = !inside;
28120 }
28121 return inside;
28122 }
28123 }
28124 return 0;
28125 }
28126
28127 Lisp_Object
28128 find_hot_spot (Lisp_Object map, int x, int y)
28129 {
28130 while (CONSP (map))
28131 {
28132 if (CONSP (XCAR (map))
28133 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28134 return XCAR (map);
28135 map = XCDR (map);
28136 }
28137
28138 return Qnil;
28139 }
28140
28141 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28142 3, 3, 0,
28143 doc: /* Lookup in image map MAP coordinates X and Y.
28144 An image map is an alist where each element has the format (AREA ID PLIST).
28145 An AREA is specified as either a rectangle, a circle, or a polygon:
28146 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28147 pixel coordinates of the upper left and bottom right corners.
28148 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28149 and the radius of the circle; r may be a float or integer.
28150 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28151 vector describes one corner in the polygon.
28152 Returns the alist element for the first matching AREA in MAP. */)
28153 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28154 {
28155 if (NILP (map))
28156 return Qnil;
28157
28158 CHECK_NUMBER (x);
28159 CHECK_NUMBER (y);
28160
28161 return find_hot_spot (map,
28162 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28163 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28164 }
28165
28166
28167 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28168 static void
28169 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28170 {
28171 /* Do not change cursor shape while dragging mouse. */
28172 if (!NILP (do_mouse_tracking))
28173 return;
28174
28175 if (!NILP (pointer))
28176 {
28177 if (EQ (pointer, Qarrow))
28178 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28179 else if (EQ (pointer, Qhand))
28180 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28181 else if (EQ (pointer, Qtext))
28182 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28183 else if (EQ (pointer, intern ("hdrag")))
28184 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28185 else if (EQ (pointer, intern ("nhdrag")))
28186 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28187 #ifdef HAVE_X_WINDOWS
28188 else if (EQ (pointer, intern ("vdrag")))
28189 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28190 #endif
28191 else if (EQ (pointer, intern ("hourglass")))
28192 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28193 else if (EQ (pointer, Qmodeline))
28194 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28195 else
28196 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28197 }
28198
28199 if (cursor != No_Cursor)
28200 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28201 }
28202
28203 #endif /* HAVE_WINDOW_SYSTEM */
28204
28205 /* Take proper action when mouse has moved to the mode or header line
28206 or marginal area AREA of window W, x-position X and y-position Y.
28207 X is relative to the start of the text display area of W, so the
28208 width of bitmap areas and scroll bars must be subtracted to get a
28209 position relative to the start of the mode line. */
28210
28211 static void
28212 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28213 enum window_part area)
28214 {
28215 struct window *w = XWINDOW (window);
28216 struct frame *f = XFRAME (w->frame);
28217 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28218 #ifdef HAVE_WINDOW_SYSTEM
28219 Display_Info *dpyinfo;
28220 #endif
28221 Cursor cursor = No_Cursor;
28222 Lisp_Object pointer = Qnil;
28223 int dx, dy, width, height;
28224 ptrdiff_t charpos;
28225 Lisp_Object string, object = Qnil;
28226 Lisp_Object pos IF_LINT (= Qnil), help;
28227
28228 Lisp_Object mouse_face;
28229 int original_x_pixel = x;
28230 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28231 struct glyph_row *row IF_LINT (= 0);
28232
28233 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28234 {
28235 int x0;
28236 struct glyph *end;
28237
28238 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28239 returns them in row/column units! */
28240 string = mode_line_string (w, area, &x, &y, &charpos,
28241 &object, &dx, &dy, &width, &height);
28242
28243 row = (area == ON_MODE_LINE
28244 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28245 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28246
28247 /* Find the glyph under the mouse pointer. */
28248 if (row->mode_line_p && row->enabled_p)
28249 {
28250 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28251 end = glyph + row->used[TEXT_AREA];
28252
28253 for (x0 = original_x_pixel;
28254 glyph < end && x0 >= glyph->pixel_width;
28255 ++glyph)
28256 x0 -= glyph->pixel_width;
28257
28258 if (glyph >= end)
28259 glyph = NULL;
28260 }
28261 }
28262 else
28263 {
28264 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28265 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28266 returns them in row/column units! */
28267 string = marginal_area_string (w, area, &x, &y, &charpos,
28268 &object, &dx, &dy, &width, &height);
28269 }
28270
28271 help = Qnil;
28272
28273 #ifdef HAVE_WINDOW_SYSTEM
28274 if (IMAGEP (object))
28275 {
28276 Lisp_Object image_map, hotspot;
28277 if ((image_map = Fplist_get (XCDR (object), QCmap),
28278 !NILP (image_map))
28279 && (hotspot = find_hot_spot (image_map, dx, dy),
28280 CONSP (hotspot))
28281 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28282 {
28283 Lisp_Object plist;
28284
28285 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28286 If so, we could look for mouse-enter, mouse-leave
28287 properties in PLIST (and do something...). */
28288 hotspot = XCDR (hotspot);
28289 if (CONSP (hotspot)
28290 && (plist = XCAR (hotspot), CONSP (plist)))
28291 {
28292 pointer = Fplist_get (plist, Qpointer);
28293 if (NILP (pointer))
28294 pointer = Qhand;
28295 help = Fplist_get (plist, Qhelp_echo);
28296 if (!NILP (help))
28297 {
28298 help_echo_string = help;
28299 XSETWINDOW (help_echo_window, w);
28300 help_echo_object = w->contents;
28301 help_echo_pos = charpos;
28302 }
28303 }
28304 }
28305 if (NILP (pointer))
28306 pointer = Fplist_get (XCDR (object), QCpointer);
28307 }
28308 #endif /* HAVE_WINDOW_SYSTEM */
28309
28310 if (STRINGP (string))
28311 pos = make_number (charpos);
28312
28313 /* Set the help text and mouse pointer. If the mouse is on a part
28314 of the mode line without any text (e.g. past the right edge of
28315 the mode line text), use the default help text and pointer. */
28316 if (STRINGP (string) || area == ON_MODE_LINE)
28317 {
28318 /* Arrange to display the help by setting the global variables
28319 help_echo_string, help_echo_object, and help_echo_pos. */
28320 if (NILP (help))
28321 {
28322 if (STRINGP (string))
28323 help = Fget_text_property (pos, Qhelp_echo, string);
28324
28325 if (!NILP (help))
28326 {
28327 help_echo_string = help;
28328 XSETWINDOW (help_echo_window, w);
28329 help_echo_object = string;
28330 help_echo_pos = charpos;
28331 }
28332 else if (area == ON_MODE_LINE)
28333 {
28334 Lisp_Object default_help
28335 = buffer_local_value_1 (Qmode_line_default_help_echo,
28336 w->contents);
28337
28338 if (STRINGP (default_help))
28339 {
28340 help_echo_string = default_help;
28341 XSETWINDOW (help_echo_window, w);
28342 help_echo_object = Qnil;
28343 help_echo_pos = -1;
28344 }
28345 }
28346 }
28347
28348 #ifdef HAVE_WINDOW_SYSTEM
28349 /* Change the mouse pointer according to what is under it. */
28350 if (FRAME_WINDOW_P (f))
28351 {
28352 dpyinfo = FRAME_DISPLAY_INFO (f);
28353 if (STRINGP (string))
28354 {
28355 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28356
28357 if (NILP (pointer))
28358 pointer = Fget_text_property (pos, Qpointer, string);
28359
28360 /* Change the mouse pointer according to what is under X/Y. */
28361 if (NILP (pointer)
28362 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28363 {
28364 Lisp_Object map;
28365 map = Fget_text_property (pos, Qlocal_map, string);
28366 if (!KEYMAPP (map))
28367 map = Fget_text_property (pos, Qkeymap, string);
28368 if (!KEYMAPP (map))
28369 cursor = dpyinfo->vertical_scroll_bar_cursor;
28370 }
28371 }
28372 else
28373 /* Default mode-line pointer. */
28374 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28375 }
28376 #endif
28377 }
28378
28379 /* Change the mouse face according to what is under X/Y. */
28380 if (STRINGP (string))
28381 {
28382 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28383 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28384 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28385 && glyph)
28386 {
28387 Lisp_Object b, e;
28388
28389 struct glyph * tmp_glyph;
28390
28391 int gpos;
28392 int gseq_length;
28393 int total_pixel_width;
28394 ptrdiff_t begpos, endpos, ignore;
28395
28396 int vpos, hpos;
28397
28398 b = Fprevious_single_property_change (make_number (charpos + 1),
28399 Qmouse_face, string, Qnil);
28400 if (NILP (b))
28401 begpos = 0;
28402 else
28403 begpos = XINT (b);
28404
28405 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28406 if (NILP (e))
28407 endpos = SCHARS (string);
28408 else
28409 endpos = XINT (e);
28410
28411 /* Calculate the glyph position GPOS of GLYPH in the
28412 displayed string, relative to the beginning of the
28413 highlighted part of the string.
28414
28415 Note: GPOS is different from CHARPOS. CHARPOS is the
28416 position of GLYPH in the internal string object. A mode
28417 line string format has structures which are converted to
28418 a flattened string by the Emacs Lisp interpreter. The
28419 internal string is an element of those structures. The
28420 displayed string is the flattened string. */
28421 tmp_glyph = row_start_glyph;
28422 while (tmp_glyph < glyph
28423 && (!(EQ (tmp_glyph->object, glyph->object)
28424 && begpos <= tmp_glyph->charpos
28425 && tmp_glyph->charpos < endpos)))
28426 tmp_glyph++;
28427 gpos = glyph - tmp_glyph;
28428
28429 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28430 the highlighted part of the displayed string to which
28431 GLYPH belongs. Note: GSEQ_LENGTH is different from
28432 SCHARS (STRING), because the latter returns the length of
28433 the internal string. */
28434 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28435 tmp_glyph > glyph
28436 && (!(EQ (tmp_glyph->object, glyph->object)
28437 && begpos <= tmp_glyph->charpos
28438 && tmp_glyph->charpos < endpos));
28439 tmp_glyph--)
28440 ;
28441 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28442
28443 /* Calculate the total pixel width of all the glyphs between
28444 the beginning of the highlighted area and GLYPH. */
28445 total_pixel_width = 0;
28446 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28447 total_pixel_width += tmp_glyph->pixel_width;
28448
28449 /* Pre calculation of re-rendering position. Note: X is in
28450 column units here, after the call to mode_line_string or
28451 marginal_area_string. */
28452 hpos = x - gpos;
28453 vpos = (area == ON_MODE_LINE
28454 ? (w->current_matrix)->nrows - 1
28455 : 0);
28456
28457 /* If GLYPH's position is included in the region that is
28458 already drawn in mouse face, we have nothing to do. */
28459 if ( EQ (window, hlinfo->mouse_face_window)
28460 && (!row->reversed_p
28461 ? (hlinfo->mouse_face_beg_col <= hpos
28462 && hpos < hlinfo->mouse_face_end_col)
28463 /* In R2L rows we swap BEG and END, see below. */
28464 : (hlinfo->mouse_face_end_col <= hpos
28465 && hpos < hlinfo->mouse_face_beg_col))
28466 && hlinfo->mouse_face_beg_row == vpos )
28467 return;
28468
28469 if (clear_mouse_face (hlinfo))
28470 cursor = No_Cursor;
28471
28472 if (!row->reversed_p)
28473 {
28474 hlinfo->mouse_face_beg_col = hpos;
28475 hlinfo->mouse_face_beg_x = original_x_pixel
28476 - (total_pixel_width + dx);
28477 hlinfo->mouse_face_end_col = hpos + gseq_length;
28478 hlinfo->mouse_face_end_x = 0;
28479 }
28480 else
28481 {
28482 /* In R2L rows, show_mouse_face expects BEG and END
28483 coordinates to be swapped. */
28484 hlinfo->mouse_face_end_col = hpos;
28485 hlinfo->mouse_face_end_x = original_x_pixel
28486 - (total_pixel_width + dx);
28487 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28488 hlinfo->mouse_face_beg_x = 0;
28489 }
28490
28491 hlinfo->mouse_face_beg_row = vpos;
28492 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28493 hlinfo->mouse_face_past_end = 0;
28494 hlinfo->mouse_face_window = window;
28495
28496 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28497 charpos,
28498 0, &ignore,
28499 glyph->face_id,
28500 1);
28501 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28502
28503 if (NILP (pointer))
28504 pointer = Qhand;
28505 }
28506 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28507 clear_mouse_face (hlinfo);
28508 }
28509 #ifdef HAVE_WINDOW_SYSTEM
28510 if (FRAME_WINDOW_P (f))
28511 define_frame_cursor1 (f, cursor, pointer);
28512 #endif
28513 }
28514
28515
28516 /* EXPORT:
28517 Take proper action when the mouse has moved to position X, Y on
28518 frame F with regards to highlighting portions of display that have
28519 mouse-face properties. Also de-highlight portions of display where
28520 the mouse was before, set the mouse pointer shape as appropriate
28521 for the mouse coordinates, and activate help echo (tooltips).
28522 X and Y can be negative or out of range. */
28523
28524 void
28525 note_mouse_highlight (struct frame *f, int x, int y)
28526 {
28527 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28528 enum window_part part = ON_NOTHING;
28529 Lisp_Object window;
28530 struct window *w;
28531 Cursor cursor = No_Cursor;
28532 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28533 struct buffer *b;
28534
28535 /* When a menu is active, don't highlight because this looks odd. */
28536 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28537 if (popup_activated ())
28538 return;
28539 #endif
28540
28541 if (!f->glyphs_initialized_p
28542 || f->pointer_invisible)
28543 return;
28544
28545 hlinfo->mouse_face_mouse_x = x;
28546 hlinfo->mouse_face_mouse_y = y;
28547 hlinfo->mouse_face_mouse_frame = f;
28548
28549 if (hlinfo->mouse_face_defer)
28550 return;
28551
28552 /* Which window is that in? */
28553 window = window_from_coordinates (f, x, y, &part, 1);
28554
28555 /* If displaying active text in another window, clear that. */
28556 if (! EQ (window, hlinfo->mouse_face_window)
28557 /* Also clear if we move out of text area in same window. */
28558 || (!NILP (hlinfo->mouse_face_window)
28559 && !NILP (window)
28560 && part != ON_TEXT
28561 && part != ON_MODE_LINE
28562 && part != ON_HEADER_LINE))
28563 clear_mouse_face (hlinfo);
28564
28565 /* Not on a window -> return. */
28566 if (!WINDOWP (window))
28567 return;
28568
28569 /* Reset help_echo_string. It will get recomputed below. */
28570 help_echo_string = Qnil;
28571
28572 /* Convert to window-relative pixel coordinates. */
28573 w = XWINDOW (window);
28574 frame_to_window_pixel_xy (w, &x, &y);
28575
28576 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28577 /* Handle tool-bar window differently since it doesn't display a
28578 buffer. */
28579 if (EQ (window, f->tool_bar_window))
28580 {
28581 note_tool_bar_highlight (f, x, y);
28582 return;
28583 }
28584 #endif
28585
28586 /* Mouse is on the mode, header line or margin? */
28587 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28588 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28589 {
28590 note_mode_line_or_margin_highlight (window, x, y, part);
28591
28592 #ifdef HAVE_WINDOW_SYSTEM
28593 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28594 {
28595 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28596 /* Show non-text cursor (Bug#16647). */
28597 goto set_cursor;
28598 }
28599 else
28600 #endif
28601 return;
28602 }
28603
28604 #ifdef HAVE_WINDOW_SYSTEM
28605 if (part == ON_VERTICAL_BORDER)
28606 {
28607 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28608 help_echo_string = build_string ("drag-mouse-1: resize");
28609 }
28610 else if (part == ON_RIGHT_DIVIDER)
28611 {
28612 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28613 help_echo_string = build_string ("drag-mouse-1: resize");
28614 }
28615 else if (part == ON_BOTTOM_DIVIDER)
28616 {
28617 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28618 help_echo_string = build_string ("drag-mouse-1: resize");
28619 }
28620 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28621 || part == ON_SCROLL_BAR)
28622 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28623 else
28624 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28625 #endif
28626
28627 /* Are we in a window whose display is up to date?
28628 And verify the buffer's text has not changed. */
28629 b = XBUFFER (w->contents);
28630 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28631 {
28632 int hpos, vpos, dx, dy, area = LAST_AREA;
28633 ptrdiff_t pos;
28634 struct glyph *glyph;
28635 Lisp_Object object;
28636 Lisp_Object mouse_face = Qnil, position;
28637 Lisp_Object *overlay_vec = NULL;
28638 ptrdiff_t i, noverlays;
28639 struct buffer *obuf;
28640 ptrdiff_t obegv, ozv;
28641 int same_region;
28642
28643 /* Find the glyph under X/Y. */
28644 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28645
28646 #ifdef HAVE_WINDOW_SYSTEM
28647 /* Look for :pointer property on image. */
28648 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28649 {
28650 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28651 if (img != NULL && IMAGEP (img->spec))
28652 {
28653 Lisp_Object image_map, hotspot;
28654 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28655 !NILP (image_map))
28656 && (hotspot = find_hot_spot (image_map,
28657 glyph->slice.img.x + dx,
28658 glyph->slice.img.y + dy),
28659 CONSP (hotspot))
28660 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28661 {
28662 Lisp_Object plist;
28663
28664 /* Could check XCAR (hotspot) to see if we enter/leave
28665 this hot-spot.
28666 If so, we could look for mouse-enter, mouse-leave
28667 properties in PLIST (and do something...). */
28668 hotspot = XCDR (hotspot);
28669 if (CONSP (hotspot)
28670 && (plist = XCAR (hotspot), CONSP (plist)))
28671 {
28672 pointer = Fplist_get (plist, Qpointer);
28673 if (NILP (pointer))
28674 pointer = Qhand;
28675 help_echo_string = Fplist_get (plist, Qhelp_echo);
28676 if (!NILP (help_echo_string))
28677 {
28678 help_echo_window = window;
28679 help_echo_object = glyph->object;
28680 help_echo_pos = glyph->charpos;
28681 }
28682 }
28683 }
28684 if (NILP (pointer))
28685 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28686 }
28687 }
28688 #endif /* HAVE_WINDOW_SYSTEM */
28689
28690 /* Clear mouse face if X/Y not over text. */
28691 if (glyph == NULL
28692 || area != TEXT_AREA
28693 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28694 /* Glyph's OBJECT is an integer for glyphs inserted by the
28695 display engine for its internal purposes, like truncation
28696 and continuation glyphs and blanks beyond the end of
28697 line's text on text terminals. If we are over such a
28698 glyph, we are not over any text. */
28699 || INTEGERP (glyph->object)
28700 /* R2L rows have a stretch glyph at their front, which
28701 stands for no text, whereas L2R rows have no glyphs at
28702 all beyond the end of text. Treat such stretch glyphs
28703 like we do with NULL glyphs in L2R rows. */
28704 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28705 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28706 && glyph->type == STRETCH_GLYPH
28707 && glyph->avoid_cursor_p))
28708 {
28709 if (clear_mouse_face (hlinfo))
28710 cursor = No_Cursor;
28711 #ifdef HAVE_WINDOW_SYSTEM
28712 if (FRAME_WINDOW_P (f) && NILP (pointer))
28713 {
28714 if (area != TEXT_AREA)
28715 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28716 else
28717 pointer = Vvoid_text_area_pointer;
28718 }
28719 #endif
28720 goto set_cursor;
28721 }
28722
28723 pos = glyph->charpos;
28724 object = glyph->object;
28725 if (!STRINGP (object) && !BUFFERP (object))
28726 goto set_cursor;
28727
28728 /* If we get an out-of-range value, return now; avoid an error. */
28729 if (BUFFERP (object) && pos > BUF_Z (b))
28730 goto set_cursor;
28731
28732 /* Make the window's buffer temporarily current for
28733 overlays_at and compute_char_face. */
28734 obuf = current_buffer;
28735 current_buffer = b;
28736 obegv = BEGV;
28737 ozv = ZV;
28738 BEGV = BEG;
28739 ZV = Z;
28740
28741 /* Is this char mouse-active or does it have help-echo? */
28742 position = make_number (pos);
28743
28744 if (BUFFERP (object))
28745 {
28746 /* Put all the overlays we want in a vector in overlay_vec. */
28747 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28748 /* Sort overlays into increasing priority order. */
28749 noverlays = sort_overlays (overlay_vec, noverlays, w);
28750 }
28751 else
28752 noverlays = 0;
28753
28754 if (NILP (Vmouse_highlight))
28755 {
28756 clear_mouse_face (hlinfo);
28757 goto check_help_echo;
28758 }
28759
28760 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28761
28762 if (same_region)
28763 cursor = No_Cursor;
28764
28765 /* Check mouse-face highlighting. */
28766 if (! same_region
28767 /* If there exists an overlay with mouse-face overlapping
28768 the one we are currently highlighting, we have to
28769 check if we enter the overlapping overlay, and then
28770 highlight only that. */
28771 || (OVERLAYP (hlinfo->mouse_face_overlay)
28772 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28773 {
28774 /* Find the highest priority overlay with a mouse-face. */
28775 Lisp_Object overlay = Qnil;
28776 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28777 {
28778 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28779 if (!NILP (mouse_face))
28780 overlay = overlay_vec[i];
28781 }
28782
28783 /* If we're highlighting the same overlay as before, there's
28784 no need to do that again. */
28785 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28786 goto check_help_echo;
28787 hlinfo->mouse_face_overlay = overlay;
28788
28789 /* Clear the display of the old active region, if any. */
28790 if (clear_mouse_face (hlinfo))
28791 cursor = No_Cursor;
28792
28793 /* If no overlay applies, get a text property. */
28794 if (NILP (overlay))
28795 mouse_face = Fget_text_property (position, Qmouse_face, object);
28796
28797 /* Next, compute the bounds of the mouse highlighting and
28798 display it. */
28799 if (!NILP (mouse_face) && STRINGP (object))
28800 {
28801 /* The mouse-highlighting comes from a display string
28802 with a mouse-face. */
28803 Lisp_Object s, e;
28804 ptrdiff_t ignore;
28805
28806 s = Fprevious_single_property_change
28807 (make_number (pos + 1), Qmouse_face, object, Qnil);
28808 e = Fnext_single_property_change
28809 (position, Qmouse_face, object, Qnil);
28810 if (NILP (s))
28811 s = make_number (0);
28812 if (NILP (e))
28813 e = make_number (SCHARS (object));
28814 mouse_face_from_string_pos (w, hlinfo, object,
28815 XINT (s), XINT (e));
28816 hlinfo->mouse_face_past_end = 0;
28817 hlinfo->mouse_face_window = window;
28818 hlinfo->mouse_face_face_id
28819 = face_at_string_position (w, object, pos, 0, &ignore,
28820 glyph->face_id, 1);
28821 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28822 cursor = No_Cursor;
28823 }
28824 else
28825 {
28826 /* The mouse-highlighting, if any, comes from an overlay
28827 or text property in the buffer. */
28828 Lisp_Object buffer IF_LINT (= Qnil);
28829 Lisp_Object disp_string IF_LINT (= Qnil);
28830
28831 if (STRINGP (object))
28832 {
28833 /* If we are on a display string with no mouse-face,
28834 check if the text under it has one. */
28835 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28836 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28837 pos = string_buffer_position (object, start);
28838 if (pos > 0)
28839 {
28840 mouse_face = get_char_property_and_overlay
28841 (make_number (pos), Qmouse_face, w->contents, &overlay);
28842 buffer = w->contents;
28843 disp_string = object;
28844 }
28845 }
28846 else
28847 {
28848 buffer = object;
28849 disp_string = Qnil;
28850 }
28851
28852 if (!NILP (mouse_face))
28853 {
28854 Lisp_Object before, after;
28855 Lisp_Object before_string, after_string;
28856 /* To correctly find the limits of mouse highlight
28857 in a bidi-reordered buffer, we must not use the
28858 optimization of limiting the search in
28859 previous-single-property-change and
28860 next-single-property-change, because
28861 rows_from_pos_range needs the real start and end
28862 positions to DTRT in this case. That's because
28863 the first row visible in a window does not
28864 necessarily display the character whose position
28865 is the smallest. */
28866 Lisp_Object lim1
28867 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28868 ? Fmarker_position (w->start)
28869 : Qnil;
28870 Lisp_Object lim2
28871 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28872 ? make_number (BUF_Z (XBUFFER (buffer))
28873 - w->window_end_pos)
28874 : Qnil;
28875
28876 if (NILP (overlay))
28877 {
28878 /* Handle the text property case. */
28879 before = Fprevious_single_property_change
28880 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28881 after = Fnext_single_property_change
28882 (make_number (pos), Qmouse_face, buffer, lim2);
28883 before_string = after_string = Qnil;
28884 }
28885 else
28886 {
28887 /* Handle the overlay case. */
28888 before = Foverlay_start (overlay);
28889 after = Foverlay_end (overlay);
28890 before_string = Foverlay_get (overlay, Qbefore_string);
28891 after_string = Foverlay_get (overlay, Qafter_string);
28892
28893 if (!STRINGP (before_string)) before_string = Qnil;
28894 if (!STRINGP (after_string)) after_string = Qnil;
28895 }
28896
28897 mouse_face_from_buffer_pos (window, hlinfo, pos,
28898 NILP (before)
28899 ? 1
28900 : XFASTINT (before),
28901 NILP (after)
28902 ? BUF_Z (XBUFFER (buffer))
28903 : XFASTINT (after),
28904 before_string, after_string,
28905 disp_string);
28906 cursor = No_Cursor;
28907 }
28908 }
28909 }
28910
28911 check_help_echo:
28912
28913 /* Look for a `help-echo' property. */
28914 if (NILP (help_echo_string)) {
28915 Lisp_Object help, overlay;
28916
28917 /* Check overlays first. */
28918 help = overlay = Qnil;
28919 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28920 {
28921 overlay = overlay_vec[i];
28922 help = Foverlay_get (overlay, Qhelp_echo);
28923 }
28924
28925 if (!NILP (help))
28926 {
28927 help_echo_string = help;
28928 help_echo_window = window;
28929 help_echo_object = overlay;
28930 help_echo_pos = pos;
28931 }
28932 else
28933 {
28934 Lisp_Object obj = glyph->object;
28935 ptrdiff_t charpos = glyph->charpos;
28936
28937 /* Try text properties. */
28938 if (STRINGP (obj)
28939 && charpos >= 0
28940 && charpos < SCHARS (obj))
28941 {
28942 help = Fget_text_property (make_number (charpos),
28943 Qhelp_echo, obj);
28944 if (NILP (help))
28945 {
28946 /* If the string itself doesn't specify a help-echo,
28947 see if the buffer text ``under'' it does. */
28948 struct glyph_row *r
28949 = MATRIX_ROW (w->current_matrix, vpos);
28950 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28951 ptrdiff_t p = string_buffer_position (obj, start);
28952 if (p > 0)
28953 {
28954 help = Fget_char_property (make_number (p),
28955 Qhelp_echo, w->contents);
28956 if (!NILP (help))
28957 {
28958 charpos = p;
28959 obj = w->contents;
28960 }
28961 }
28962 }
28963 }
28964 else if (BUFFERP (obj)
28965 && charpos >= BEGV
28966 && charpos < ZV)
28967 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28968 obj);
28969
28970 if (!NILP (help))
28971 {
28972 help_echo_string = help;
28973 help_echo_window = window;
28974 help_echo_object = obj;
28975 help_echo_pos = charpos;
28976 }
28977 }
28978 }
28979
28980 #ifdef HAVE_WINDOW_SYSTEM
28981 /* Look for a `pointer' property. */
28982 if (FRAME_WINDOW_P (f) && NILP (pointer))
28983 {
28984 /* Check overlays first. */
28985 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28986 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28987
28988 if (NILP (pointer))
28989 {
28990 Lisp_Object obj = glyph->object;
28991 ptrdiff_t charpos = glyph->charpos;
28992
28993 /* Try text properties. */
28994 if (STRINGP (obj)
28995 && charpos >= 0
28996 && charpos < SCHARS (obj))
28997 {
28998 pointer = Fget_text_property (make_number (charpos),
28999 Qpointer, obj);
29000 if (NILP (pointer))
29001 {
29002 /* If the string itself doesn't specify a pointer,
29003 see if the buffer text ``under'' it does. */
29004 struct glyph_row *r
29005 = MATRIX_ROW (w->current_matrix, vpos);
29006 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29007 ptrdiff_t p = string_buffer_position (obj, start);
29008 if (p > 0)
29009 pointer = Fget_char_property (make_number (p),
29010 Qpointer, w->contents);
29011 }
29012 }
29013 else if (BUFFERP (obj)
29014 && charpos >= BEGV
29015 && charpos < ZV)
29016 pointer = Fget_text_property (make_number (charpos),
29017 Qpointer, obj);
29018 }
29019 }
29020 #endif /* HAVE_WINDOW_SYSTEM */
29021
29022 BEGV = obegv;
29023 ZV = ozv;
29024 current_buffer = obuf;
29025 }
29026
29027 set_cursor:
29028
29029 #ifdef HAVE_WINDOW_SYSTEM
29030 if (FRAME_WINDOW_P (f))
29031 define_frame_cursor1 (f, cursor, pointer);
29032 #else
29033 /* This is here to prevent a compiler error, about "label at end of
29034 compound statement". */
29035 return;
29036 #endif
29037 }
29038
29039
29040 /* EXPORT for RIF:
29041 Clear any mouse-face on window W. This function is part of the
29042 redisplay interface, and is called from try_window_id and similar
29043 functions to ensure the mouse-highlight is off. */
29044
29045 void
29046 x_clear_window_mouse_face (struct window *w)
29047 {
29048 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29049 Lisp_Object window;
29050
29051 block_input ();
29052 XSETWINDOW (window, w);
29053 if (EQ (window, hlinfo->mouse_face_window))
29054 clear_mouse_face (hlinfo);
29055 unblock_input ();
29056 }
29057
29058
29059 /* EXPORT:
29060 Just discard the mouse face information for frame F, if any.
29061 This is used when the size of F is changed. */
29062
29063 void
29064 cancel_mouse_face (struct frame *f)
29065 {
29066 Lisp_Object window;
29067 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29068
29069 window = hlinfo->mouse_face_window;
29070 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29071 reset_mouse_highlight (hlinfo);
29072 }
29073
29074
29075 \f
29076 /***********************************************************************
29077 Exposure Events
29078 ***********************************************************************/
29079
29080 #ifdef HAVE_WINDOW_SYSTEM
29081
29082 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29083 which intersects rectangle R. R is in window-relative coordinates. */
29084
29085 static void
29086 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29087 enum glyph_row_area area)
29088 {
29089 struct glyph *first = row->glyphs[area];
29090 struct glyph *end = row->glyphs[area] + row->used[area];
29091 struct glyph *last;
29092 int first_x, start_x, x;
29093
29094 if (area == TEXT_AREA && row->fill_line_p)
29095 /* If row extends face to end of line write the whole line. */
29096 draw_glyphs (w, 0, row, area,
29097 0, row->used[area],
29098 DRAW_NORMAL_TEXT, 0);
29099 else
29100 {
29101 /* Set START_X to the window-relative start position for drawing glyphs of
29102 AREA. The first glyph of the text area can be partially visible.
29103 The first glyphs of other areas cannot. */
29104 start_x = window_box_left_offset (w, area);
29105 x = start_x;
29106 if (area == TEXT_AREA)
29107 x += row->x;
29108
29109 /* Find the first glyph that must be redrawn. */
29110 while (first < end
29111 && x + first->pixel_width < r->x)
29112 {
29113 x += first->pixel_width;
29114 ++first;
29115 }
29116
29117 /* Find the last one. */
29118 last = first;
29119 first_x = x;
29120 while (last < end
29121 && x < r->x + r->width)
29122 {
29123 x += last->pixel_width;
29124 ++last;
29125 }
29126
29127 /* Repaint. */
29128 if (last > first)
29129 draw_glyphs (w, first_x - start_x, row, area,
29130 first - row->glyphs[area], last - row->glyphs[area],
29131 DRAW_NORMAL_TEXT, 0);
29132 }
29133 }
29134
29135
29136 /* Redraw the parts of the glyph row ROW on window W intersecting
29137 rectangle R. R is in window-relative coordinates. Value is
29138 non-zero if mouse-face was overwritten. */
29139
29140 static int
29141 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29142 {
29143 eassert (row->enabled_p);
29144
29145 if (row->mode_line_p || w->pseudo_window_p)
29146 draw_glyphs (w, 0, row, TEXT_AREA,
29147 0, row->used[TEXT_AREA],
29148 DRAW_NORMAL_TEXT, 0);
29149 else
29150 {
29151 if (row->used[LEFT_MARGIN_AREA])
29152 expose_area (w, row, r, LEFT_MARGIN_AREA);
29153 if (row->used[TEXT_AREA])
29154 expose_area (w, row, r, TEXT_AREA);
29155 if (row->used[RIGHT_MARGIN_AREA])
29156 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29157 draw_row_fringe_bitmaps (w, row);
29158 }
29159
29160 return row->mouse_face_p;
29161 }
29162
29163
29164 /* Redraw those parts of glyphs rows during expose event handling that
29165 overlap other rows. Redrawing of an exposed line writes over parts
29166 of lines overlapping that exposed line; this function fixes that.
29167
29168 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29169 row in W's current matrix that is exposed and overlaps other rows.
29170 LAST_OVERLAPPING_ROW is the last such row. */
29171
29172 static void
29173 expose_overlaps (struct window *w,
29174 struct glyph_row *first_overlapping_row,
29175 struct glyph_row *last_overlapping_row,
29176 XRectangle *r)
29177 {
29178 struct glyph_row *row;
29179
29180 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29181 if (row->overlapping_p)
29182 {
29183 eassert (row->enabled_p && !row->mode_line_p);
29184
29185 row->clip = r;
29186 if (row->used[LEFT_MARGIN_AREA])
29187 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29188
29189 if (row->used[TEXT_AREA])
29190 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29191
29192 if (row->used[RIGHT_MARGIN_AREA])
29193 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29194 row->clip = NULL;
29195 }
29196 }
29197
29198
29199 /* Return non-zero if W's cursor intersects rectangle R. */
29200
29201 static int
29202 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29203 {
29204 XRectangle cr, result;
29205 struct glyph *cursor_glyph;
29206 struct glyph_row *row;
29207
29208 if (w->phys_cursor.vpos >= 0
29209 && w->phys_cursor.vpos < w->current_matrix->nrows
29210 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29211 row->enabled_p)
29212 && row->cursor_in_fringe_p)
29213 {
29214 /* Cursor is in the fringe. */
29215 cr.x = window_box_right_offset (w,
29216 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29217 ? RIGHT_MARGIN_AREA
29218 : TEXT_AREA));
29219 cr.y = row->y;
29220 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29221 cr.height = row->height;
29222 return x_intersect_rectangles (&cr, r, &result);
29223 }
29224
29225 cursor_glyph = get_phys_cursor_glyph (w);
29226 if (cursor_glyph)
29227 {
29228 /* r is relative to W's box, but w->phys_cursor.x is relative
29229 to left edge of W's TEXT area. Adjust it. */
29230 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29231 cr.y = w->phys_cursor.y;
29232 cr.width = cursor_glyph->pixel_width;
29233 cr.height = w->phys_cursor_height;
29234 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29235 I assume the effect is the same -- and this is portable. */
29236 return x_intersect_rectangles (&cr, r, &result);
29237 }
29238 /* If we don't understand the format, pretend we're not in the hot-spot. */
29239 return 0;
29240 }
29241
29242
29243 /* EXPORT:
29244 Draw a vertical window border to the right of window W if W doesn't
29245 have vertical scroll bars. */
29246
29247 void
29248 x_draw_vertical_border (struct window *w)
29249 {
29250 struct frame *f = XFRAME (WINDOW_FRAME (w));
29251
29252 /* We could do better, if we knew what type of scroll-bar the adjacent
29253 windows (on either side) have... But we don't :-(
29254 However, I think this works ok. ++KFS 2003-04-25 */
29255
29256 /* Redraw borders between horizontally adjacent windows. Don't
29257 do it for frames with vertical scroll bars because either the
29258 right scroll bar of a window, or the left scroll bar of its
29259 neighbor will suffice as a border. */
29260 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29261 return;
29262
29263 /* Note: It is necessary to redraw both the left and the right
29264 borders, for when only this single window W is being
29265 redisplayed. */
29266 if (!WINDOW_RIGHTMOST_P (w)
29267 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29268 {
29269 int x0, x1, y0, y1;
29270
29271 window_box_edges (w, &x0, &y0, &x1, &y1);
29272 y1 -= 1;
29273
29274 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29275 x1 -= 1;
29276
29277 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29278 }
29279
29280 if (!WINDOW_LEFTMOST_P (w)
29281 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29282 {
29283 int x0, x1, y0, y1;
29284
29285 window_box_edges (w, &x0, &y0, &x1, &y1);
29286 y1 -= 1;
29287
29288 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29289 x0 -= 1;
29290
29291 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29292 }
29293 }
29294
29295
29296 /* Draw window dividers for window W. */
29297
29298 void
29299 x_draw_right_divider (struct window *w)
29300 {
29301 struct frame *f = WINDOW_XFRAME (w);
29302
29303 if (w->mini || w->pseudo_window_p)
29304 return;
29305 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29306 {
29307 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29308 int x1 = WINDOW_RIGHT_EDGE_X (w);
29309 int y0 = WINDOW_TOP_EDGE_Y (w);
29310 /* The bottom divider prevails. */
29311 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29312
29313 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29314 }
29315 }
29316
29317 static void
29318 x_draw_bottom_divider (struct window *w)
29319 {
29320 struct frame *f = XFRAME (WINDOW_FRAME (w));
29321
29322 if (w->mini || w->pseudo_window_p)
29323 return;
29324 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29325 {
29326 int x0 = WINDOW_LEFT_EDGE_X (w);
29327 int x1 = WINDOW_RIGHT_EDGE_X (w);
29328 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29329 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29330
29331 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29332 }
29333 }
29334
29335 /* Redraw the part of window W intersection rectangle FR. Pixel
29336 coordinates in FR are frame-relative. Call this function with
29337 input blocked. Value is non-zero if the exposure overwrites
29338 mouse-face. */
29339
29340 static int
29341 expose_window (struct window *w, XRectangle *fr)
29342 {
29343 struct frame *f = XFRAME (w->frame);
29344 XRectangle wr, r;
29345 int mouse_face_overwritten_p = 0;
29346
29347 /* If window is not yet fully initialized, do nothing. This can
29348 happen when toolkit scroll bars are used and a window is split.
29349 Reconfiguring the scroll bar will generate an expose for a newly
29350 created window. */
29351 if (w->current_matrix == NULL)
29352 return 0;
29353
29354 /* When we're currently updating the window, display and current
29355 matrix usually don't agree. Arrange for a thorough display
29356 later. */
29357 if (w->must_be_updated_p)
29358 {
29359 SET_FRAME_GARBAGED (f);
29360 return 0;
29361 }
29362
29363 /* Frame-relative pixel rectangle of W. */
29364 wr.x = WINDOW_LEFT_EDGE_X (w);
29365 wr.y = WINDOW_TOP_EDGE_Y (w);
29366 wr.width = WINDOW_PIXEL_WIDTH (w);
29367 wr.height = WINDOW_PIXEL_HEIGHT (w);
29368
29369 if (x_intersect_rectangles (fr, &wr, &r))
29370 {
29371 int yb = window_text_bottom_y (w);
29372 struct glyph_row *row;
29373 int cursor_cleared_p, phys_cursor_on_p;
29374 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29375
29376 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29377 r.x, r.y, r.width, r.height));
29378
29379 /* Convert to window coordinates. */
29380 r.x -= WINDOW_LEFT_EDGE_X (w);
29381 r.y -= WINDOW_TOP_EDGE_Y (w);
29382
29383 /* Turn off the cursor. */
29384 if (!w->pseudo_window_p
29385 && phys_cursor_in_rect_p (w, &r))
29386 {
29387 x_clear_cursor (w);
29388 cursor_cleared_p = 1;
29389 }
29390 else
29391 cursor_cleared_p = 0;
29392
29393 /* If the row containing the cursor extends face to end of line,
29394 then expose_area might overwrite the cursor outside the
29395 rectangle and thus notice_overwritten_cursor might clear
29396 w->phys_cursor_on_p. We remember the original value and
29397 check later if it is changed. */
29398 phys_cursor_on_p = w->phys_cursor_on_p;
29399
29400 /* Update lines intersecting rectangle R. */
29401 first_overlapping_row = last_overlapping_row = NULL;
29402 for (row = w->current_matrix->rows;
29403 row->enabled_p;
29404 ++row)
29405 {
29406 int y0 = row->y;
29407 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29408
29409 if ((y0 >= r.y && y0 < r.y + r.height)
29410 || (y1 > r.y && y1 < r.y + r.height)
29411 || (r.y >= y0 && r.y < y1)
29412 || (r.y + r.height > y0 && r.y + r.height < y1))
29413 {
29414 /* A header line may be overlapping, but there is no need
29415 to fix overlapping areas for them. KFS 2005-02-12 */
29416 if (row->overlapping_p && !row->mode_line_p)
29417 {
29418 if (first_overlapping_row == NULL)
29419 first_overlapping_row = row;
29420 last_overlapping_row = row;
29421 }
29422
29423 row->clip = fr;
29424 if (expose_line (w, row, &r))
29425 mouse_face_overwritten_p = 1;
29426 row->clip = NULL;
29427 }
29428 else if (row->overlapping_p)
29429 {
29430 /* We must redraw a row overlapping the exposed area. */
29431 if (y0 < r.y
29432 ? y0 + row->phys_height > r.y
29433 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29434 {
29435 if (first_overlapping_row == NULL)
29436 first_overlapping_row = row;
29437 last_overlapping_row = row;
29438 }
29439 }
29440
29441 if (y1 >= yb)
29442 break;
29443 }
29444
29445 /* Display the mode line if there is one. */
29446 if (WINDOW_WANTS_MODELINE_P (w)
29447 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29448 row->enabled_p)
29449 && row->y < r.y + r.height)
29450 {
29451 if (expose_line (w, row, &r))
29452 mouse_face_overwritten_p = 1;
29453 }
29454
29455 if (!w->pseudo_window_p)
29456 {
29457 /* Fix the display of overlapping rows. */
29458 if (first_overlapping_row)
29459 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29460 fr);
29461
29462 /* Draw border between windows. */
29463 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29464 x_draw_right_divider (w);
29465 else
29466 x_draw_vertical_border (w);
29467
29468 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29469 x_draw_bottom_divider (w);
29470
29471 /* Turn the cursor on again. */
29472 if (cursor_cleared_p
29473 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29474 update_window_cursor (w, 1);
29475 }
29476 }
29477
29478 return mouse_face_overwritten_p;
29479 }
29480
29481
29482
29483 /* Redraw (parts) of all windows in the window tree rooted at W that
29484 intersect R. R contains frame pixel coordinates. Value is
29485 non-zero if the exposure overwrites mouse-face. */
29486
29487 static int
29488 expose_window_tree (struct window *w, XRectangle *r)
29489 {
29490 struct frame *f = XFRAME (w->frame);
29491 int mouse_face_overwritten_p = 0;
29492
29493 while (w && !FRAME_GARBAGED_P (f))
29494 {
29495 if (WINDOWP (w->contents))
29496 mouse_face_overwritten_p
29497 |= expose_window_tree (XWINDOW (w->contents), r);
29498 else
29499 mouse_face_overwritten_p |= expose_window (w, r);
29500
29501 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29502 }
29503
29504 return mouse_face_overwritten_p;
29505 }
29506
29507
29508 /* EXPORT:
29509 Redisplay an exposed area of frame F. X and Y are the upper-left
29510 corner of the exposed rectangle. W and H are width and height of
29511 the exposed area. All are pixel values. W or H zero means redraw
29512 the entire frame. */
29513
29514 void
29515 expose_frame (struct frame *f, int x, int y, int w, int h)
29516 {
29517 XRectangle r;
29518 int mouse_face_overwritten_p = 0;
29519
29520 TRACE ((stderr, "expose_frame "));
29521
29522 /* No need to redraw if frame will be redrawn soon. */
29523 if (FRAME_GARBAGED_P (f))
29524 {
29525 TRACE ((stderr, " garbaged\n"));
29526 return;
29527 }
29528
29529 /* If basic faces haven't been realized yet, there is no point in
29530 trying to redraw anything. This can happen when we get an expose
29531 event while Emacs is starting, e.g. by moving another window. */
29532 if (FRAME_FACE_CACHE (f) == NULL
29533 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29534 {
29535 TRACE ((stderr, " no faces\n"));
29536 return;
29537 }
29538
29539 if (w == 0 || h == 0)
29540 {
29541 r.x = r.y = 0;
29542 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29543 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29544 }
29545 else
29546 {
29547 r.x = x;
29548 r.y = y;
29549 r.width = w;
29550 r.height = h;
29551 }
29552
29553 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29554 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29555
29556 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29557 if (WINDOWP (f->tool_bar_window))
29558 mouse_face_overwritten_p
29559 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29560 #endif
29561
29562 #ifdef HAVE_X_WINDOWS
29563 #ifndef MSDOS
29564 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29565 if (WINDOWP (f->menu_bar_window))
29566 mouse_face_overwritten_p
29567 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29568 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29569 #endif
29570 #endif
29571
29572 /* Some window managers support a focus-follows-mouse style with
29573 delayed raising of frames. Imagine a partially obscured frame,
29574 and moving the mouse into partially obscured mouse-face on that
29575 frame. The visible part of the mouse-face will be highlighted,
29576 then the WM raises the obscured frame. With at least one WM, KDE
29577 2.1, Emacs is not getting any event for the raising of the frame
29578 (even tried with SubstructureRedirectMask), only Expose events.
29579 These expose events will draw text normally, i.e. not
29580 highlighted. Which means we must redo the highlight here.
29581 Subsume it under ``we love X''. --gerd 2001-08-15 */
29582 /* Included in Windows version because Windows most likely does not
29583 do the right thing if any third party tool offers
29584 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29585 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29586 {
29587 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29588 if (f == hlinfo->mouse_face_mouse_frame)
29589 {
29590 int mouse_x = hlinfo->mouse_face_mouse_x;
29591 int mouse_y = hlinfo->mouse_face_mouse_y;
29592 clear_mouse_face (hlinfo);
29593 note_mouse_highlight (f, mouse_x, mouse_y);
29594 }
29595 }
29596 }
29597
29598
29599 /* EXPORT:
29600 Determine the intersection of two rectangles R1 and R2. Return
29601 the intersection in *RESULT. Value is non-zero if RESULT is not
29602 empty. */
29603
29604 int
29605 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29606 {
29607 XRectangle *left, *right;
29608 XRectangle *upper, *lower;
29609 int intersection_p = 0;
29610
29611 /* Rearrange so that R1 is the left-most rectangle. */
29612 if (r1->x < r2->x)
29613 left = r1, right = r2;
29614 else
29615 left = r2, right = r1;
29616
29617 /* X0 of the intersection is right.x0, if this is inside R1,
29618 otherwise there is no intersection. */
29619 if (right->x <= left->x + left->width)
29620 {
29621 result->x = right->x;
29622
29623 /* The right end of the intersection is the minimum of
29624 the right ends of left and right. */
29625 result->width = (min (left->x + left->width, right->x + right->width)
29626 - result->x);
29627
29628 /* Same game for Y. */
29629 if (r1->y < r2->y)
29630 upper = r1, lower = r2;
29631 else
29632 upper = r2, lower = r1;
29633
29634 /* The upper end of the intersection is lower.y0, if this is inside
29635 of upper. Otherwise, there is no intersection. */
29636 if (lower->y <= upper->y + upper->height)
29637 {
29638 result->y = lower->y;
29639
29640 /* The lower end of the intersection is the minimum of the lower
29641 ends of upper and lower. */
29642 result->height = (min (lower->y + lower->height,
29643 upper->y + upper->height)
29644 - result->y);
29645 intersection_p = 1;
29646 }
29647 }
29648
29649 return intersection_p;
29650 }
29651
29652 #endif /* HAVE_WINDOW_SYSTEM */
29653
29654 \f
29655 /***********************************************************************
29656 Initialization
29657 ***********************************************************************/
29658
29659 void
29660 syms_of_xdisp (void)
29661 {
29662 Vwith_echo_area_save_vector = Qnil;
29663 staticpro (&Vwith_echo_area_save_vector);
29664
29665 Vmessage_stack = Qnil;
29666 staticpro (&Vmessage_stack);
29667
29668 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29669 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29670
29671 message_dolog_marker1 = Fmake_marker ();
29672 staticpro (&message_dolog_marker1);
29673 message_dolog_marker2 = Fmake_marker ();
29674 staticpro (&message_dolog_marker2);
29675 message_dolog_marker3 = Fmake_marker ();
29676 staticpro (&message_dolog_marker3);
29677
29678 #ifdef GLYPH_DEBUG
29679 defsubr (&Sdump_frame_glyph_matrix);
29680 defsubr (&Sdump_glyph_matrix);
29681 defsubr (&Sdump_glyph_row);
29682 defsubr (&Sdump_tool_bar_row);
29683 defsubr (&Strace_redisplay);
29684 defsubr (&Strace_to_stderr);
29685 #endif
29686 #ifdef HAVE_WINDOW_SYSTEM
29687 defsubr (&Stool_bar_height);
29688 defsubr (&Slookup_image_map);
29689 #endif
29690 defsubr (&Sline_pixel_height);
29691 defsubr (&Sformat_mode_line);
29692 defsubr (&Sinvisible_p);
29693 defsubr (&Scurrent_bidi_paragraph_direction);
29694 defsubr (&Swindow_text_pixel_size);
29695 defsubr (&Smove_point_visually);
29696
29697 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29698 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29699 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29700 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29701 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29702 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29703 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29704 DEFSYM (Qeval, "eval");
29705 DEFSYM (QCdata, ":data");
29706 DEFSYM (Qdisplay, "display");
29707 DEFSYM (Qspace_width, "space-width");
29708 DEFSYM (Qraise, "raise");
29709 DEFSYM (Qslice, "slice");
29710 DEFSYM (Qspace, "space");
29711 DEFSYM (Qmargin, "margin");
29712 DEFSYM (Qpointer, "pointer");
29713 DEFSYM (Qleft_margin, "left-margin");
29714 DEFSYM (Qright_margin, "right-margin");
29715 DEFSYM (Qcenter, "center");
29716 DEFSYM (Qline_height, "line-height");
29717 DEFSYM (QCalign_to, ":align-to");
29718 DEFSYM (QCrelative_width, ":relative-width");
29719 DEFSYM (QCrelative_height, ":relative-height");
29720 DEFSYM (QCeval, ":eval");
29721 DEFSYM (QCpropertize, ":propertize");
29722 DEFSYM (QCfile, ":file");
29723 DEFSYM (Qfontified, "fontified");
29724 DEFSYM (Qfontification_functions, "fontification-functions");
29725 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29726 DEFSYM (Qescape_glyph, "escape-glyph");
29727 DEFSYM (Qnobreak_space, "nobreak-space");
29728 DEFSYM (Qimage, "image");
29729 DEFSYM (Qtext, "text");
29730 DEFSYM (Qboth, "both");
29731 DEFSYM (Qboth_horiz, "both-horiz");
29732 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29733 DEFSYM (QCmap, ":map");
29734 DEFSYM (QCpointer, ":pointer");
29735 DEFSYM (Qrect, "rect");
29736 DEFSYM (Qcircle, "circle");
29737 DEFSYM (Qpoly, "poly");
29738 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29739 DEFSYM (Qgrow_only, "grow-only");
29740 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29741 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29742 DEFSYM (Qposition, "position");
29743 DEFSYM (Qbuffer_position, "buffer-position");
29744 DEFSYM (Qobject, "object");
29745 DEFSYM (Qbar, "bar");
29746 DEFSYM (Qhbar, "hbar");
29747 DEFSYM (Qbox, "box");
29748 DEFSYM (Qhollow, "hollow");
29749 DEFSYM (Qhand, "hand");
29750 DEFSYM (Qarrow, "arrow");
29751 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29752
29753 list_of_error = list1 (list2 (intern_c_string ("error"),
29754 intern_c_string ("void-variable")));
29755 staticpro (&list_of_error);
29756
29757 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29758 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29759 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29760 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29761
29762 echo_buffer[0] = echo_buffer[1] = Qnil;
29763 staticpro (&echo_buffer[0]);
29764 staticpro (&echo_buffer[1]);
29765
29766 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29767 staticpro (&echo_area_buffer[0]);
29768 staticpro (&echo_area_buffer[1]);
29769
29770 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29771 staticpro (&Vmessages_buffer_name);
29772
29773 mode_line_proptrans_alist = Qnil;
29774 staticpro (&mode_line_proptrans_alist);
29775 mode_line_string_list = Qnil;
29776 staticpro (&mode_line_string_list);
29777 mode_line_string_face = Qnil;
29778 staticpro (&mode_line_string_face);
29779 mode_line_string_face_prop = Qnil;
29780 staticpro (&mode_line_string_face_prop);
29781 Vmode_line_unwind_vector = Qnil;
29782 staticpro (&Vmode_line_unwind_vector);
29783
29784 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29785
29786 help_echo_string = Qnil;
29787 staticpro (&help_echo_string);
29788 help_echo_object = Qnil;
29789 staticpro (&help_echo_object);
29790 help_echo_window = Qnil;
29791 staticpro (&help_echo_window);
29792 previous_help_echo_string = Qnil;
29793 staticpro (&previous_help_echo_string);
29794 help_echo_pos = -1;
29795
29796 DEFSYM (Qright_to_left, "right-to-left");
29797 DEFSYM (Qleft_to_right, "left-to-right");
29798
29799 #ifdef HAVE_WINDOW_SYSTEM
29800 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29801 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29802 For example, if a block cursor is over a tab, it will be drawn as
29803 wide as that tab on the display. */);
29804 x_stretch_cursor_p = 0;
29805 #endif
29806
29807 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29808 doc: /* Non-nil means highlight trailing whitespace.
29809 The face used for trailing whitespace is `trailing-whitespace'. */);
29810 Vshow_trailing_whitespace = Qnil;
29811
29812 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29813 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29814 If the value is t, Emacs highlights non-ASCII chars which have the
29815 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29816 or `escape-glyph' face respectively.
29817
29818 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29819 U+2011 (non-breaking hyphen) are affected.
29820
29821 Any other non-nil value means to display these characters as a escape
29822 glyph followed by an ordinary space or hyphen.
29823
29824 A value of nil means no special handling of these characters. */);
29825 Vnobreak_char_display = Qt;
29826
29827 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29828 doc: /* The pointer shape to show in void text areas.
29829 A value of nil means to show the text pointer. Other options are `arrow',
29830 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29831 Vvoid_text_area_pointer = Qarrow;
29832
29833 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29834 doc: /* Non-nil means don't actually do any redisplay.
29835 This is used for internal purposes. */);
29836 Vinhibit_redisplay = Qnil;
29837
29838 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29839 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29840 Vglobal_mode_string = Qnil;
29841
29842 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29843 doc: /* Marker for where to display an arrow on top of the buffer text.
29844 This must be the beginning of a line in order to work.
29845 See also `overlay-arrow-string'. */);
29846 Voverlay_arrow_position = Qnil;
29847
29848 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29849 doc: /* String to display as an arrow in non-window frames.
29850 See also `overlay-arrow-position'. */);
29851 Voverlay_arrow_string = build_pure_c_string ("=>");
29852
29853 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29854 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29855 The symbols on this list are examined during redisplay to determine
29856 where to display overlay arrows. */);
29857 Voverlay_arrow_variable_list
29858 = list1 (intern_c_string ("overlay-arrow-position"));
29859
29860 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29861 doc: /* The number of lines to try scrolling a window by when point moves out.
29862 If that fails to bring point back on frame, point is centered instead.
29863 If this is zero, point is always centered after it moves off frame.
29864 If you want scrolling to always be a line at a time, you should set
29865 `scroll-conservatively' to a large value rather than set this to 1. */);
29866
29867 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29868 doc: /* Scroll up to this many lines, to bring point back on screen.
29869 If point moves off-screen, redisplay will scroll by up to
29870 `scroll-conservatively' lines in order to bring point just barely
29871 onto the screen again. If that cannot be done, then redisplay
29872 recenters point as usual.
29873
29874 If the value is greater than 100, redisplay will never recenter point,
29875 but will always scroll just enough text to bring point into view, even
29876 if you move far away.
29877
29878 A value of zero means always recenter point if it moves off screen. */);
29879 scroll_conservatively = 0;
29880
29881 DEFVAR_INT ("scroll-margin", scroll_margin,
29882 doc: /* Number of lines of margin at the top and bottom of a window.
29883 Recenter the window whenever point gets within this many lines
29884 of the top or bottom of the window. */);
29885 scroll_margin = 0;
29886
29887 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29888 doc: /* Pixels per inch value for non-window system displays.
29889 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29890 Vdisplay_pixels_per_inch = make_float (72.0);
29891
29892 #ifdef GLYPH_DEBUG
29893 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29894 #endif
29895
29896 DEFVAR_LISP ("truncate-partial-width-windows",
29897 Vtruncate_partial_width_windows,
29898 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29899 For an integer value, truncate lines in each window narrower than the
29900 full frame width, provided the window width is less than that integer;
29901 otherwise, respect the value of `truncate-lines'.
29902
29903 For any other non-nil value, truncate lines in all windows that do
29904 not span the full frame width.
29905
29906 A value of nil means to respect the value of `truncate-lines'.
29907
29908 If `word-wrap' is enabled, you might want to reduce this. */);
29909 Vtruncate_partial_width_windows = make_number (50);
29910
29911 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29912 doc: /* Maximum buffer size for which line number should be displayed.
29913 If the buffer is bigger than this, the line number does not appear
29914 in the mode line. A value of nil means no limit. */);
29915 Vline_number_display_limit = Qnil;
29916
29917 DEFVAR_INT ("line-number-display-limit-width",
29918 line_number_display_limit_width,
29919 doc: /* Maximum line width (in characters) for line number display.
29920 If the average length of the lines near point is bigger than this, then the
29921 line number may be omitted from the mode line. */);
29922 line_number_display_limit_width = 200;
29923
29924 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29925 doc: /* Non-nil means highlight region even in nonselected windows. */);
29926 highlight_nonselected_windows = 0;
29927
29928 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29929 doc: /* Non-nil if more than one frame is visible on this display.
29930 Minibuffer-only frames don't count, but iconified frames do.
29931 This variable is not guaranteed to be accurate except while processing
29932 `frame-title-format' and `icon-title-format'. */);
29933
29934 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29935 doc: /* Template for displaying the title bar of visible frames.
29936 \(Assuming the window manager supports this feature.)
29937
29938 This variable has the same structure as `mode-line-format', except that
29939 the %c and %l constructs are ignored. It is used only on frames for
29940 which no explicit name has been set \(see `modify-frame-parameters'). */);
29941
29942 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29943 doc: /* Template for displaying the title bar of an iconified frame.
29944 \(Assuming the window manager supports this feature.)
29945 This variable has the same structure as `mode-line-format' (which see),
29946 and is used only on frames for which no explicit name has been set
29947 \(see `modify-frame-parameters'). */);
29948 Vicon_title_format
29949 = Vframe_title_format
29950 = listn (CONSTYPE_PURE, 3,
29951 intern_c_string ("multiple-frames"),
29952 build_pure_c_string ("%b"),
29953 listn (CONSTYPE_PURE, 4,
29954 empty_unibyte_string,
29955 intern_c_string ("invocation-name"),
29956 build_pure_c_string ("@"),
29957 intern_c_string ("system-name")));
29958
29959 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29960 doc: /* Maximum number of lines to keep in the message log buffer.
29961 If nil, disable message logging. If t, log messages but don't truncate
29962 the buffer when it becomes large. */);
29963 Vmessage_log_max = make_number (1000);
29964
29965 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29966 doc: /* Functions called before redisplay, if window sizes have changed.
29967 The value should be a list of functions that take one argument.
29968 Just before redisplay, for each frame, if any of its windows have changed
29969 size since the last redisplay, or have been split or deleted,
29970 all the functions in the list are called, with the frame as argument. */);
29971 Vwindow_size_change_functions = Qnil;
29972
29973 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29974 doc: /* List of functions to call before redisplaying a window with scrolling.
29975 Each function is called with two arguments, the window and its new
29976 display-start position. Note that these functions are also called by
29977 `set-window-buffer'. Also note that the value of `window-end' is not
29978 valid when these functions are called.
29979
29980 Warning: Do not use this feature to alter the way the window
29981 is scrolled. It is not designed for that, and such use probably won't
29982 work. */);
29983 Vwindow_scroll_functions = Qnil;
29984
29985 DEFVAR_LISP ("window-text-change-functions",
29986 Vwindow_text_change_functions,
29987 doc: /* Functions to call in redisplay when text in the window might change. */);
29988 Vwindow_text_change_functions = Qnil;
29989
29990 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29991 doc: /* Functions called when redisplay of a window reaches the end trigger.
29992 Each function is called with two arguments, the window and the end trigger value.
29993 See `set-window-redisplay-end-trigger'. */);
29994 Vredisplay_end_trigger_functions = Qnil;
29995
29996 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29997 doc: /* Non-nil means autoselect window with mouse pointer.
29998 If nil, do not autoselect windows.
29999 A positive number means delay autoselection by that many seconds: a
30000 window is autoselected only after the mouse has remained in that
30001 window for the duration of the delay.
30002 A negative number has a similar effect, but causes windows to be
30003 autoselected only after the mouse has stopped moving. \(Because of
30004 the way Emacs compares mouse events, you will occasionally wait twice
30005 that time before the window gets selected.\)
30006 Any other value means to autoselect window instantaneously when the
30007 mouse pointer enters it.
30008
30009 Autoselection selects the minibuffer only if it is active, and never
30010 unselects the minibuffer if it is active.
30011
30012 When customizing this variable make sure that the actual value of
30013 `focus-follows-mouse' matches the behavior of your window manager. */);
30014 Vmouse_autoselect_window = Qnil;
30015
30016 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30017 doc: /* Non-nil means automatically resize tool-bars.
30018 This dynamically changes the tool-bar's height to the minimum height
30019 that is needed to make all tool-bar items visible.
30020 If value is `grow-only', the tool-bar's height is only increased
30021 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30022 Vauto_resize_tool_bars = Qt;
30023
30024 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30025 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30026 auto_raise_tool_bar_buttons_p = 1;
30027
30028 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30029 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30030 make_cursor_line_fully_visible_p = 1;
30031
30032 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30033 doc: /* Border below tool-bar in pixels.
30034 If an integer, use it as the height of the border.
30035 If it is one of `internal-border-width' or `border-width', use the
30036 value of the corresponding frame parameter.
30037 Otherwise, no border is added below the tool-bar. */);
30038 Vtool_bar_border = Qinternal_border_width;
30039
30040 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30041 doc: /* Margin around tool-bar buttons in pixels.
30042 If an integer, use that for both horizontal and vertical margins.
30043 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30044 HORZ specifying the horizontal margin, and VERT specifying the
30045 vertical margin. */);
30046 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30047
30048 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30049 doc: /* Relief thickness of tool-bar buttons. */);
30050 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30051
30052 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30053 doc: /* Tool bar style to use.
30054 It can be one of
30055 image - show images only
30056 text - show text only
30057 both - show both, text below image
30058 both-horiz - show text to the right of the image
30059 text-image-horiz - show text to the left of the image
30060 any other - use system default or image if no system default.
30061
30062 This variable only affects the GTK+ toolkit version of Emacs. */);
30063 Vtool_bar_style = Qnil;
30064
30065 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30066 doc: /* Maximum number of characters a label can have to be shown.
30067 The tool bar style must also show labels for this to have any effect, see
30068 `tool-bar-style'. */);
30069 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30070
30071 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30072 doc: /* List of functions to call to fontify regions of text.
30073 Each function is called with one argument POS. Functions must
30074 fontify a region starting at POS in the current buffer, and give
30075 fontified regions the property `fontified'. */);
30076 Vfontification_functions = Qnil;
30077 Fmake_variable_buffer_local (Qfontification_functions);
30078
30079 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30080 unibyte_display_via_language_environment,
30081 doc: /* Non-nil means display unibyte text according to language environment.
30082 Specifically, this means that raw bytes in the range 160-255 decimal
30083 are displayed by converting them to the equivalent multibyte characters
30084 according to the current language environment. As a result, they are
30085 displayed according to the current fontset.
30086
30087 Note that this variable affects only how these bytes are displayed,
30088 but does not change the fact they are interpreted as raw bytes. */);
30089 unibyte_display_via_language_environment = 0;
30090
30091 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30092 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30093 If a float, it specifies a fraction of the mini-window frame's height.
30094 If an integer, it specifies a number of lines. */);
30095 Vmax_mini_window_height = make_float (0.25);
30096
30097 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30098 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30099 A value of nil means don't automatically resize mini-windows.
30100 A value of t means resize them to fit the text displayed in them.
30101 A value of `grow-only', the default, means let mini-windows grow only;
30102 they return to their normal size when the minibuffer is closed, or the
30103 echo area becomes empty. */);
30104 Vresize_mini_windows = Qgrow_only;
30105
30106 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30107 doc: /* Alist specifying how to blink the cursor off.
30108 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30109 `cursor-type' frame-parameter or variable equals ON-STATE,
30110 comparing using `equal', Emacs uses OFF-STATE to specify
30111 how to blink it off. ON-STATE and OFF-STATE are values for
30112 the `cursor-type' frame parameter.
30113
30114 If a frame's ON-STATE has no entry in this list,
30115 the frame's other specifications determine how to blink the cursor off. */);
30116 Vblink_cursor_alist = Qnil;
30117
30118 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30119 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30120 If non-nil, windows are automatically scrolled horizontally to make
30121 point visible. */);
30122 automatic_hscrolling_p = 1;
30123 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30124
30125 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30126 doc: /* How many columns away from the window edge point is allowed to get
30127 before automatic hscrolling will horizontally scroll the window. */);
30128 hscroll_margin = 5;
30129
30130 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30131 doc: /* How many columns to scroll the window when point gets too close to the edge.
30132 When point is less than `hscroll-margin' columns from the window
30133 edge, automatic hscrolling will scroll the window by the amount of columns
30134 determined by this variable. If its value is a positive integer, scroll that
30135 many columns. If it's a positive floating-point number, it specifies the
30136 fraction of the window's width to scroll. If it's nil or zero, point will be
30137 centered horizontally after the scroll. Any other value, including negative
30138 numbers, are treated as if the value were zero.
30139
30140 Automatic hscrolling always moves point outside the scroll margin, so if
30141 point was more than scroll step columns inside the margin, the window will
30142 scroll more than the value given by the scroll step.
30143
30144 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30145 and `scroll-right' overrides this variable's effect. */);
30146 Vhscroll_step = make_number (0);
30147
30148 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30149 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30150 Bind this around calls to `message' to let it take effect. */);
30151 message_truncate_lines = 0;
30152
30153 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30154 doc: /* Normal hook run to update the menu bar definitions.
30155 Redisplay runs this hook before it redisplays the menu bar.
30156 This is used to update menus such as Buffers, whose contents depend on
30157 various data. */);
30158 Vmenu_bar_update_hook = Qnil;
30159
30160 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30161 doc: /* Frame for which we are updating a menu.
30162 The enable predicate for a menu binding should check this variable. */);
30163 Vmenu_updating_frame = Qnil;
30164
30165 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30166 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30167 inhibit_menubar_update = 0;
30168
30169 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30170 doc: /* Prefix prepended to all continuation lines at display time.
30171 The value may be a string, an image, or a stretch-glyph; it is
30172 interpreted in the same way as the value of a `display' text property.
30173
30174 This variable is overridden by any `wrap-prefix' text or overlay
30175 property.
30176
30177 To add a prefix to non-continuation lines, use `line-prefix'. */);
30178 Vwrap_prefix = Qnil;
30179 DEFSYM (Qwrap_prefix, "wrap-prefix");
30180 Fmake_variable_buffer_local (Qwrap_prefix);
30181
30182 DEFVAR_LISP ("line-prefix", Vline_prefix,
30183 doc: /* Prefix prepended to all non-continuation lines at display time.
30184 The value may be a string, an image, or a stretch-glyph; it is
30185 interpreted in the same way as the value of a `display' text property.
30186
30187 This variable is overridden by any `line-prefix' text or overlay
30188 property.
30189
30190 To add a prefix to continuation lines, use `wrap-prefix'. */);
30191 Vline_prefix = Qnil;
30192 DEFSYM (Qline_prefix, "line-prefix");
30193 Fmake_variable_buffer_local (Qline_prefix);
30194
30195 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30196 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30197 inhibit_eval_during_redisplay = 0;
30198
30199 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30200 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30201 inhibit_free_realized_faces = 0;
30202
30203 #ifdef GLYPH_DEBUG
30204 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30205 doc: /* Inhibit try_window_id display optimization. */);
30206 inhibit_try_window_id = 0;
30207
30208 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30209 doc: /* Inhibit try_window_reusing display optimization. */);
30210 inhibit_try_window_reusing = 0;
30211
30212 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30213 doc: /* Inhibit try_cursor_movement display optimization. */);
30214 inhibit_try_cursor_movement = 0;
30215 #endif /* GLYPH_DEBUG */
30216
30217 DEFVAR_INT ("overline-margin", overline_margin,
30218 doc: /* Space between overline and text, in pixels.
30219 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30220 margin to the character height. */);
30221 overline_margin = 2;
30222
30223 DEFVAR_INT ("underline-minimum-offset",
30224 underline_minimum_offset,
30225 doc: /* Minimum distance between baseline and underline.
30226 This can improve legibility of underlined text at small font sizes,
30227 particularly when using variable `x-use-underline-position-properties'
30228 with fonts that specify an UNDERLINE_POSITION relatively close to the
30229 baseline. The default value is 1. */);
30230 underline_minimum_offset = 1;
30231
30232 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30233 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30234 This feature only works when on a window system that can change
30235 cursor shapes. */);
30236 display_hourglass_p = 1;
30237
30238 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30239 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30240 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30241
30242 #ifdef HAVE_WINDOW_SYSTEM
30243 hourglass_atimer = NULL;
30244 hourglass_shown_p = 0;
30245 #endif /* HAVE_WINDOW_SYSTEM */
30246
30247 DEFSYM (Qglyphless_char, "glyphless-char");
30248 DEFSYM (Qhex_code, "hex-code");
30249 DEFSYM (Qempty_box, "empty-box");
30250 DEFSYM (Qthin_space, "thin-space");
30251 DEFSYM (Qzero_width, "zero-width");
30252
30253 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30254 doc: /* Function run just before redisplay.
30255 It is called with one argument, which is the set of windows that are to
30256 be redisplayed. This set can be nil (meaning, only the selected window),
30257 or t (meaning all windows). */);
30258 Vpre_redisplay_function = intern ("ignore");
30259
30260 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30261 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30262
30263 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30264 doc: /* Char-table defining glyphless characters.
30265 Each element, if non-nil, should be one of the following:
30266 an ASCII acronym string: display this string in a box
30267 `hex-code': display the hexadecimal code of a character in a box
30268 `empty-box': display as an empty box
30269 `thin-space': display as 1-pixel width space
30270 `zero-width': don't display
30271 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30272 display method for graphical terminals and text terminals respectively.
30273 GRAPHICAL and TEXT should each have one of the values listed above.
30274
30275 The char-table has one extra slot to control the display of a character for
30276 which no font is found. This slot only takes effect on graphical terminals.
30277 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30278 `thin-space'. The default is `empty-box'.
30279
30280 If a character has a non-nil entry in an active display table, the
30281 display table takes effect; in this case, Emacs does not consult
30282 `glyphless-char-display' at all. */);
30283 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30284 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30285 Qempty_box);
30286
30287 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30288 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30289 Vdebug_on_message = Qnil;
30290
30291 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30292 doc: /* */);
30293 Vredisplay__all_windows_cause
30294 = Fmake_vector (make_number (100), make_number (0));
30295
30296 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30297 doc: /* */);
30298 Vredisplay__mode_lines_cause
30299 = Fmake_vector (make_number (100), make_number (0));
30300 }
30301
30302
30303 /* Initialize this module when Emacs starts. */
30304
30305 void
30306 init_xdisp (void)
30307 {
30308 CHARPOS (this_line_start_pos) = 0;
30309
30310 if (!noninteractive)
30311 {
30312 struct window *m = XWINDOW (minibuf_window);
30313 Lisp_Object frame = m->frame;
30314 struct frame *f = XFRAME (frame);
30315 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30316 struct window *r = XWINDOW (root);
30317 int i;
30318
30319 echo_area_window = minibuf_window;
30320
30321 r->top_line = FRAME_TOP_MARGIN (f);
30322 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30323 r->total_cols = FRAME_COLS (f);
30324 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30325 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30326 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30327
30328 m->top_line = FRAME_LINES (f) - 1;
30329 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30330 m->total_cols = FRAME_COLS (f);
30331 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30332 m->total_lines = 1;
30333 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30334
30335 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30336 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30337 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30338
30339 /* The default ellipsis glyphs `...'. */
30340 for (i = 0; i < 3; ++i)
30341 default_invis_vector[i] = make_number ('.');
30342 }
30343
30344 {
30345 /* Allocate the buffer for frame titles.
30346 Also used for `format-mode-line'. */
30347 int size = 100;
30348 mode_line_noprop_buf = xmalloc (size);
30349 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30350 mode_line_noprop_ptr = mode_line_noprop_buf;
30351 mode_line_target = MODE_LINE_DISPLAY;
30352 }
30353
30354 help_echo_showing_p = 0;
30355 }
30356
30357 #ifdef HAVE_WINDOW_SYSTEM
30358
30359 /* Platform-independent portion of hourglass implementation. */
30360
30361 /* Cancel a currently active hourglass timer, and start a new one. */
30362 void
30363 start_hourglass (void)
30364 {
30365 struct timespec delay;
30366
30367 cancel_hourglass ();
30368
30369 if (INTEGERP (Vhourglass_delay)
30370 && XINT (Vhourglass_delay) > 0)
30371 delay = make_timespec (min (XINT (Vhourglass_delay),
30372 TYPE_MAXIMUM (time_t)),
30373 0);
30374 else if (FLOATP (Vhourglass_delay)
30375 && XFLOAT_DATA (Vhourglass_delay) > 0)
30376 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30377 else
30378 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30379
30380 #ifdef HAVE_NTGUI
30381 {
30382 extern void w32_note_current_window (void);
30383 w32_note_current_window ();
30384 }
30385 #endif /* HAVE_NTGUI */
30386
30387 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30388 show_hourglass, NULL);
30389 }
30390
30391
30392 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30393 shown. */
30394 void
30395 cancel_hourglass (void)
30396 {
30397 if (hourglass_atimer)
30398 {
30399 cancel_atimer (hourglass_atimer);
30400 hourglass_atimer = NULL;
30401 }
30402
30403 if (hourglass_shown_p)
30404 hide_hourglass ();
30405 }
30406
30407 #endif /* HAVE_WINDOW_SYSTEM */