Fix bug #16347 with updating redisplay of company-mode's "tooltip".
[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_max_ascent, 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 pixels = w->pixel_width;
1023
1024 if (!w->pseudo_window_p)
1025 {
1026 pixels -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1027 pixels -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1028
1029 if (area == TEXT_AREA)
1030 pixels -= (WINDOW_MARGINS_WIDTH (w)
1031 + WINDOW_FRINGES_WIDTH (w));
1032 else if (area == LEFT_MARGIN_AREA)
1033 pixels = WINDOW_LEFT_MARGIN_WIDTH (w);
1034 else if (area == RIGHT_MARGIN_AREA)
1035 pixels = WINDOW_RIGHT_MARGIN_WIDTH (w);
1036 }
1037
1038 return pixels;
1039 }
1040
1041
1042 /* Return the pixel height of the display area of window W, not
1043 including mode lines of W, if any. */
1044
1045 int
1046 window_box_height (struct window *w)
1047 {
1048 struct frame *f = XFRAME (w->frame);
1049 int height = WINDOW_PIXEL_HEIGHT (w);
1050
1051 eassert (height >= 0);
1052
1053 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1054
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1060
1061 if (WINDOW_WANTS_MODELINE_P (w))
1062 {
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1071 }
1072
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1074 {
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1083 }
1084
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1088 }
1089
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. ANY_AREA means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1093
1094 int
1095 window_box_left_offset (struct window *w, enum glyph_row_area area)
1096 {
1097 int x;
1098
1099 if (w->pseudo_window_p)
1100 return 0;
1101
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 ? 0
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. ANY_AREA means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 int
1127 window_box_right_offset (struct window *w, enum glyph_row_area area)
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. ANY_AREA means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 int
1137 window_box_left (struct window *w, enum glyph_row_area area)
1138 {
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1141
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1144
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1147
1148 return x;
1149 }
1150
1151
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. ANY_AREA means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1155
1156 int
1157 window_box_right (struct window *w, enum glyph_row_area area)
1158 {
1159 return window_box_left (w, area) + window_box_width (w, area);
1160 }
1161
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. ANY_AREA means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168
1169 void
1170 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1171 int *box_y, int *box_width, int *box_height)
1172 {
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1180 {
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1184 }
1185 }
1186
1187 #ifdef HAVE_WINDOW_SYSTEM
1188
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1195
1196 static void
1197 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1199 {
1200 window_box (w, ANY_AREA, top_left_x, top_left_y,
1201 bottom_right_x, bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1204 }
1205
1206 #endif /* HAVE_WINDOW_SYSTEM */
1207
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1211
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1214
1215 int
1216 line_bottom_y (struct it *it)
1217 {
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1220
1221 if (line_height == 0)
1222 {
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1226 {
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1231 }
1232 else
1233 {
1234 struct glyph_row *row = it->glyph_row;
1235
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1244 }
1245 }
1246
1247 return line_top_y + line_height;
1248 }
1249
1250 DEFUN ("line-pixel-height", Fline_pixel_height,
1251 Sline_pixel_height, 0, 0, 0,
1252 doc: /* Return height in pixels of text line in the selected window.
1253
1254 Value is the height in pixels of the line at point. */)
1255 (void)
1256 {
1257 struct it it;
1258 struct text_pos pt;
1259 struct window *w = XWINDOW (selected_window);
1260
1261 SET_TEXT_POS (pt, PT, PT_BYTE);
1262 start_display (&it, w, pt);
1263 it.vpos = it.current_y = 0;
1264 last_height = 0;
1265 return make_number (line_bottom_y (&it));
1266 }
1267
1268 /* Return the default pixel height of text lines in window W. The
1269 value is the canonical height of the W frame's default font, plus
1270 any extra space required by the line-spacing variable or frame
1271 parameter.
1272
1273 Implementation note: this ignores any line-spacing text properties
1274 put on the newline characters. This is because those properties
1275 only affect the _screen_ line ending in the newline (i.e., in a
1276 continued line, only the last screen line will be affected), which
1277 means only a small number of lines in a buffer can ever use this
1278 feature. Since this function is used to compute the default pixel
1279 equivalent of text lines in a window, we can safely ignore those
1280 few lines. For the same reasons, we ignore the line-height
1281 properties. */
1282 int
1283 default_line_pixel_height (struct window *w)
1284 {
1285 struct frame *f = WINDOW_XFRAME (w);
1286 int height = FRAME_LINE_HEIGHT (f);
1287
1288 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1289 {
1290 struct buffer *b = XBUFFER (w->contents);
1291 Lisp_Object val = BVAR (b, extra_line_spacing);
1292
1293 if (NILP (val))
1294 val = BVAR (&buffer_defaults, extra_line_spacing);
1295 if (!NILP (val))
1296 {
1297 if (RANGED_INTEGERP (0, val, INT_MAX))
1298 height += XFASTINT (val);
1299 else if (FLOATP (val))
1300 {
1301 int addon = XFLOAT_DATA (val) * height + 0.5;
1302
1303 if (addon >= 0)
1304 height += addon;
1305 }
1306 }
1307 else
1308 height += f->extra_line_spacing;
1309 }
1310
1311 return height;
1312 }
1313
1314 /* Subroutine of pos_visible_p below. Extracts a display string, if
1315 any, from the display spec given as its argument. */
1316 static Lisp_Object
1317 string_from_display_spec (Lisp_Object spec)
1318 {
1319 if (CONSP (spec))
1320 {
1321 while (CONSP (spec))
1322 {
1323 if (STRINGP (XCAR (spec)))
1324 return XCAR (spec);
1325 spec = XCDR (spec);
1326 }
1327 }
1328 else if (VECTORP (spec))
1329 {
1330 ptrdiff_t i;
1331
1332 for (i = 0; i < ASIZE (spec); i++)
1333 {
1334 if (STRINGP (AREF (spec, i)))
1335 return AREF (spec, i);
1336 }
1337 return Qnil;
1338 }
1339
1340 return spec;
1341 }
1342
1343
1344 /* Limit insanely large values of W->hscroll on frame F to the largest
1345 value that will still prevent first_visible_x and last_visible_x of
1346 'struct it' from overflowing an int. */
1347 static int
1348 window_hscroll_limited (struct window *w, struct frame *f)
1349 {
1350 ptrdiff_t window_hscroll = w->hscroll;
1351 int window_text_width = window_box_width (w, TEXT_AREA);
1352 int colwidth = FRAME_COLUMN_WIDTH (f);
1353
1354 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1355 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1356
1357 return window_hscroll;
1358 }
1359
1360 /* Return 1 if position CHARPOS is visible in window W.
1361 CHARPOS < 0 means return info about WINDOW_END position.
1362 If visible, set *X and *Y to pixel coordinates of top left corner.
1363 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1364 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1365
1366 int
1367 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1368 int *rtop, int *rbot, int *rowh, int *vpos)
1369 {
1370 struct it it;
1371 void *itdata = bidi_shelve_cache ();
1372 struct text_pos top;
1373 int visible_p = 0;
1374 struct buffer *old_buffer = NULL;
1375
1376 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1377 return visible_p;
1378
1379 if (XBUFFER (w->contents) != current_buffer)
1380 {
1381 old_buffer = current_buffer;
1382 set_buffer_internal_1 (XBUFFER (w->contents));
1383 }
1384
1385 SET_TEXT_POS_FROM_MARKER (top, w->start);
1386 /* Scrolling a minibuffer window via scroll bar when the echo area
1387 shows long text sometimes resets the minibuffer contents behind
1388 our backs. */
1389 if (CHARPOS (top) > ZV)
1390 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1391
1392 /* Compute exact mode line heights. */
1393 if (WINDOW_WANTS_MODELINE_P (w))
1394 w->mode_line_height
1395 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1396 BVAR (current_buffer, mode_line_format));
1397
1398 if (WINDOW_WANTS_HEADER_LINE_P (w))
1399 w->header_line_height
1400 = display_mode_line (w, HEADER_LINE_FACE_ID,
1401 BVAR (current_buffer, header_line_format));
1402
1403 start_display (&it, w, top);
1404 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1405 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1406
1407 if (charpos >= 0
1408 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1409 && IT_CHARPOS (it) >= charpos)
1410 /* When scanning backwards under bidi iteration, move_it_to
1411 stops at or _before_ CHARPOS, because it stops at or to
1412 the _right_ of the character at CHARPOS. */
1413 || (it.bidi_p && it.bidi_it.scan_dir == -1
1414 && IT_CHARPOS (it) <= charpos)))
1415 {
1416 /* We have reached CHARPOS, or passed it. How the call to
1417 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1418 or covered by a display property, move_it_to stops at the end
1419 of the invisible text, to the right of CHARPOS. (ii) If
1420 CHARPOS is in a display vector, move_it_to stops on its last
1421 glyph. */
1422 int top_x = it.current_x;
1423 int top_y = it.current_y;
1424 /* Calling line_bottom_y may change it.method, it.position, etc. */
1425 enum it_method it_method = it.method;
1426 int bottom_y = (last_height = 0, line_bottom_y (&it));
1427 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1428
1429 if (top_y < window_top_y)
1430 visible_p = bottom_y > window_top_y;
1431 else if (top_y < it.last_visible_y)
1432 visible_p = true;
1433 if (bottom_y >= it.last_visible_y
1434 && it.bidi_p && it.bidi_it.scan_dir == -1
1435 && IT_CHARPOS (it) < charpos)
1436 {
1437 /* When the last line of the window is scanned backwards
1438 under bidi iteration, we could be duped into thinking
1439 that we have passed CHARPOS, when in fact move_it_to
1440 simply stopped short of CHARPOS because it reached
1441 last_visible_y. To see if that's what happened, we call
1442 move_it_to again with a slightly larger vertical limit,
1443 and see if it actually moved vertically; if it did, we
1444 didn't really reach CHARPOS, which is beyond window end. */
1445 struct it save_it = it;
1446 /* Why 10? because we don't know how many canonical lines
1447 will the height of the next line(s) be. So we guess. */
1448 int ten_more_lines = 10 * default_line_pixel_height (w);
1449
1450 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1451 MOVE_TO_POS | MOVE_TO_Y);
1452 if (it.current_y > top_y)
1453 visible_p = 0;
1454
1455 it = save_it;
1456 }
1457 if (visible_p)
1458 {
1459 if (it_method == GET_FROM_DISPLAY_VECTOR)
1460 {
1461 /* We stopped on the last glyph of a display vector.
1462 Try and recompute. Hack alert! */
1463 if (charpos < 2 || top.charpos >= charpos)
1464 top_x = it.glyph_row->x;
1465 else
1466 {
1467 struct it it2, it2_prev;
1468 /* The idea is to get to the previous buffer
1469 position, consume the character there, and use
1470 the pixel coordinates we get after that. But if
1471 the previous buffer position is also displayed
1472 from a display vector, we need to consume all of
1473 the glyphs from that display vector. */
1474 start_display (&it2, w, top);
1475 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1476 /* If we didn't get to CHARPOS - 1, there's some
1477 replacing display property at that position, and
1478 we stopped after it. That is exactly the place
1479 whose coordinates we want. */
1480 if (IT_CHARPOS (it2) != charpos - 1)
1481 it2_prev = it2;
1482 else
1483 {
1484 /* Iterate until we get out of the display
1485 vector that displays the character at
1486 CHARPOS - 1. */
1487 do {
1488 get_next_display_element (&it2);
1489 PRODUCE_GLYPHS (&it2);
1490 it2_prev = it2;
1491 set_iterator_to_next (&it2, 1);
1492 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1493 && IT_CHARPOS (it2) < charpos);
1494 }
1495 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1496 || it2_prev.current_x > it2_prev.last_visible_x)
1497 top_x = it.glyph_row->x;
1498 else
1499 {
1500 top_x = it2_prev.current_x;
1501 top_y = it2_prev.current_y;
1502 }
1503 }
1504 }
1505 else if (IT_CHARPOS (it) != charpos)
1506 {
1507 Lisp_Object cpos = make_number (charpos);
1508 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1509 Lisp_Object string = string_from_display_spec (spec);
1510 struct text_pos tpos;
1511 int replacing_spec_p;
1512 bool newline_in_string
1513 = (STRINGP (string)
1514 && memchr (SDATA (string), '\n', SBYTES (string)));
1515
1516 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1517 replacing_spec_p
1518 = (!NILP (spec)
1519 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1520 charpos, FRAME_WINDOW_P (it.f)));
1521 /* The tricky code below is needed because there's a
1522 discrepancy between move_it_to and how we set cursor
1523 when PT is at the beginning of a portion of text
1524 covered by a display property or an overlay with a
1525 display property, or the display line ends in a
1526 newline from a display string. move_it_to will stop
1527 _after_ such display strings, whereas
1528 set_cursor_from_row conspires with cursor_row_p to
1529 place the cursor on the first glyph produced from the
1530 display string. */
1531
1532 /* We have overshoot PT because it is covered by a
1533 display property that replaces the text it covers.
1534 If the string includes embedded newlines, we are also
1535 in the wrong display line. Backtrack to the correct
1536 line, where the display property begins. */
1537 if (replacing_spec_p)
1538 {
1539 Lisp_Object startpos, endpos;
1540 EMACS_INT start, end;
1541 struct it it3;
1542 int it3_moved;
1543
1544 /* Find the first and the last buffer positions
1545 covered by the display string. */
1546 endpos =
1547 Fnext_single_char_property_change (cpos, Qdisplay,
1548 Qnil, Qnil);
1549 startpos =
1550 Fprevious_single_char_property_change (endpos, Qdisplay,
1551 Qnil, Qnil);
1552 start = XFASTINT (startpos);
1553 end = XFASTINT (endpos);
1554 /* Move to the last buffer position before the
1555 display property. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1558 /* Move forward one more line if the position before
1559 the display string is a newline or if it is the
1560 rightmost character on a line that is
1561 continued or word-wrapped. */
1562 if (it3.method == GET_FROM_BUFFER
1563 && (it3.c == '\n'
1564 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1565 move_it_by_lines (&it3, 1);
1566 else if (move_it_in_display_line_to (&it3, -1,
1567 it3.current_x
1568 + it3.pixel_width,
1569 MOVE_TO_X)
1570 == MOVE_LINE_CONTINUED)
1571 {
1572 move_it_by_lines (&it3, 1);
1573 /* When we are under word-wrap, the #$@%!
1574 move_it_by_lines moves 2 lines, so we need to
1575 fix that up. */
1576 if (it3.line_wrap == WORD_WRAP)
1577 move_it_by_lines (&it3, -1);
1578 }
1579
1580 /* Record the vertical coordinate of the display
1581 line where we wound up. */
1582 top_y = it3.current_y;
1583 if (it3.bidi_p)
1584 {
1585 /* When characters are reordered for display,
1586 the character displayed to the left of the
1587 display string could be _after_ the display
1588 property in the logical order. Use the
1589 smallest vertical position of these two. */
1590 start_display (&it3, w, top);
1591 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1592 if (it3.current_y < top_y)
1593 top_y = it3.current_y;
1594 }
1595 /* Move from the top of the window to the beginning
1596 of the display line where the display string
1597 begins. */
1598 start_display (&it3, w, top);
1599 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1600 /* If it3_moved stays zero after the 'while' loop
1601 below, that means we already were at a newline
1602 before the loop (e.g., the display string begins
1603 with a newline), so we don't need to (and cannot)
1604 inspect the glyphs of it3.glyph_row, because
1605 PRODUCE_GLYPHS will not produce anything for a
1606 newline, and thus it3.glyph_row stays at its
1607 stale content it got at top of the window. */
1608 it3_moved = 0;
1609 /* Finally, advance the iterator until we hit the
1610 first display element whose character position is
1611 CHARPOS, or until the first newline from the
1612 display string, which signals the end of the
1613 display line. */
1614 while (get_next_display_element (&it3))
1615 {
1616 PRODUCE_GLYPHS (&it3);
1617 if (IT_CHARPOS (it3) == charpos
1618 || ITERATOR_AT_END_OF_LINE_P (&it3))
1619 break;
1620 it3_moved = 1;
1621 set_iterator_to_next (&it3, 0);
1622 }
1623 top_x = it3.current_x - it3.pixel_width;
1624 /* Normally, we would exit the above loop because we
1625 found the display element whose character
1626 position is CHARPOS. For the contingency that we
1627 didn't, and stopped at the first newline from the
1628 display string, move back over the glyphs
1629 produced from the string, until we find the
1630 rightmost glyph not from the string. */
1631 if (it3_moved
1632 && newline_in_string
1633 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1634 {
1635 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1636 + it3.glyph_row->used[TEXT_AREA];
1637
1638 while (EQ ((g - 1)->object, string))
1639 {
1640 --g;
1641 top_x -= g->pixel_width;
1642 }
1643 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1644 + it3.glyph_row->used[TEXT_AREA]);
1645 }
1646 }
1647 }
1648
1649 *x = top_x;
1650 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1651 *rtop = max (0, window_top_y - top_y);
1652 *rbot = max (0, bottom_y - it.last_visible_y);
1653 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1654 - max (top_y, window_top_y)));
1655 *vpos = it.vpos;
1656 }
1657 }
1658 else
1659 {
1660 /* We were asked to provide info about WINDOW_END. */
1661 struct it it2;
1662 void *it2data = NULL;
1663
1664 SAVE_IT (it2, it, it2data);
1665 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1666 move_it_by_lines (&it, 1);
1667 if (charpos < IT_CHARPOS (it)
1668 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1669 {
1670 visible_p = true;
1671 RESTORE_IT (&it2, &it2, it2data);
1672 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1673 *x = it2.current_x;
1674 *y = it2.current_y + it2.max_ascent - it2.ascent;
1675 *rtop = max (0, -it2.current_y);
1676 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1677 - it.last_visible_y));
1678 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1679 it.last_visible_y)
1680 - max (it2.current_y,
1681 WINDOW_HEADER_LINE_HEIGHT (w))));
1682 *vpos = it2.vpos;
1683 }
1684 else
1685 bidi_unshelve_cache (it2data, 1);
1686 }
1687 bidi_unshelve_cache (itdata, 0);
1688
1689 if (old_buffer)
1690 set_buffer_internal_1 (old_buffer);
1691
1692 if (visible_p && w->hscroll > 0)
1693 *x -=
1694 window_hscroll_limited (w, WINDOW_XFRAME (w))
1695 * WINDOW_FRAME_COLUMN_WIDTH (w);
1696
1697 #if 0
1698 /* Debugging code. */
1699 if (visible_p)
1700 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1701 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1702 else
1703 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1704 #endif
1705
1706 return visible_p;
1707 }
1708
1709
1710 /* Return the next character from STR. Return in *LEN the length of
1711 the character. This is like STRING_CHAR_AND_LENGTH but never
1712 returns an invalid character. If we find one, we return a `?', but
1713 with the length of the invalid character. */
1714
1715 static int
1716 string_char_and_length (const unsigned char *str, int *len)
1717 {
1718 int c;
1719
1720 c = STRING_CHAR_AND_LENGTH (str, *len);
1721 if (!CHAR_VALID_P (c))
1722 /* We may not change the length here because other places in Emacs
1723 don't use this function, i.e. they silently accept invalid
1724 characters. */
1725 c = '?';
1726
1727 return c;
1728 }
1729
1730
1731
1732 /* Given a position POS containing a valid character and byte position
1733 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1734
1735 static struct text_pos
1736 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1737 {
1738 eassert (STRINGP (string) && nchars >= 0);
1739
1740 if (STRING_MULTIBYTE (string))
1741 {
1742 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1743 int len;
1744
1745 while (nchars--)
1746 {
1747 string_char_and_length (p, &len);
1748 p += len;
1749 CHARPOS (pos) += 1;
1750 BYTEPOS (pos) += len;
1751 }
1752 }
1753 else
1754 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1755
1756 return pos;
1757 }
1758
1759
1760 /* Value is the text position, i.e. character and byte position,
1761 for character position CHARPOS in STRING. */
1762
1763 static struct text_pos
1764 string_pos (ptrdiff_t charpos, Lisp_Object string)
1765 {
1766 struct text_pos pos;
1767 eassert (STRINGP (string));
1768 eassert (charpos >= 0);
1769 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1770 return pos;
1771 }
1772
1773
1774 /* Value is a text position, i.e. character and byte position, for
1775 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1776 means recognize multibyte characters. */
1777
1778 static struct text_pos
1779 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1780 {
1781 struct text_pos pos;
1782
1783 eassert (s != NULL);
1784 eassert (charpos >= 0);
1785
1786 if (multibyte_p)
1787 {
1788 int len;
1789
1790 SET_TEXT_POS (pos, 0, 0);
1791 while (charpos--)
1792 {
1793 string_char_and_length ((const unsigned char *) s, &len);
1794 s += len;
1795 CHARPOS (pos) += 1;
1796 BYTEPOS (pos) += len;
1797 }
1798 }
1799 else
1800 SET_TEXT_POS (pos, charpos, charpos);
1801
1802 return pos;
1803 }
1804
1805
1806 /* Value is the number of characters in C string S. MULTIBYTE_P
1807 non-zero means recognize multibyte characters. */
1808
1809 static ptrdiff_t
1810 number_of_chars (const char *s, bool multibyte_p)
1811 {
1812 ptrdiff_t nchars;
1813
1814 if (multibyte_p)
1815 {
1816 ptrdiff_t rest = strlen (s);
1817 int len;
1818 const unsigned char *p = (const unsigned char *) s;
1819
1820 for (nchars = 0; rest > 0; ++nchars)
1821 {
1822 string_char_and_length (p, &len);
1823 rest -= len, p += len;
1824 }
1825 }
1826 else
1827 nchars = strlen (s);
1828
1829 return nchars;
1830 }
1831
1832
1833 /* Compute byte position NEWPOS->bytepos corresponding to
1834 NEWPOS->charpos. POS is a known position in string STRING.
1835 NEWPOS->charpos must be >= POS.charpos. */
1836
1837 static void
1838 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1839 {
1840 eassert (STRINGP (string));
1841 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1842
1843 if (STRING_MULTIBYTE (string))
1844 *newpos = string_pos_nchars_ahead (pos, string,
1845 CHARPOS (*newpos) - CHARPOS (pos));
1846 else
1847 BYTEPOS (*newpos) = CHARPOS (*newpos);
1848 }
1849
1850 /* EXPORT:
1851 Return an estimation of the pixel height of mode or header lines on
1852 frame F. FACE_ID specifies what line's height to estimate. */
1853
1854 int
1855 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1856 {
1857 #ifdef HAVE_WINDOW_SYSTEM
1858 if (FRAME_WINDOW_P (f))
1859 {
1860 int height = FONT_HEIGHT (FRAME_FONT (f));
1861
1862 /* This function is called so early when Emacs starts that the face
1863 cache and mode line face are not yet initialized. */
1864 if (FRAME_FACE_CACHE (f))
1865 {
1866 struct face *face = FACE_FROM_ID (f, face_id);
1867 if (face)
1868 {
1869 if (face->font)
1870 height = FONT_HEIGHT (face->font);
1871 if (face->box_line_width > 0)
1872 height += 2 * face->box_line_width;
1873 }
1874 }
1875
1876 return height;
1877 }
1878 #endif
1879
1880 return 1;
1881 }
1882
1883 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1884 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1885 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1886 not force the value into range. */
1887
1888 void
1889 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1890 int *x, int *y, NativeRectangle *bounds, int noclip)
1891 {
1892
1893 #ifdef HAVE_WINDOW_SYSTEM
1894 if (FRAME_WINDOW_P (f))
1895 {
1896 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1897 even for negative values. */
1898 if (pix_x < 0)
1899 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1900 if (pix_y < 0)
1901 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1902
1903 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1904 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1905
1906 if (bounds)
1907 STORE_NATIVE_RECT (*bounds,
1908 FRAME_COL_TO_PIXEL_X (f, pix_x),
1909 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1910 FRAME_COLUMN_WIDTH (f) - 1,
1911 FRAME_LINE_HEIGHT (f) - 1);
1912
1913 /* PXW: Should we clip pixels before converting to columns/lines? */
1914 if (!noclip)
1915 {
1916 if (pix_x < 0)
1917 pix_x = 0;
1918 else if (pix_x > FRAME_TOTAL_COLS (f))
1919 pix_x = FRAME_TOTAL_COLS (f);
1920
1921 if (pix_y < 0)
1922 pix_y = 0;
1923 else if (pix_y > FRAME_LINES (f))
1924 pix_y = FRAME_LINES (f);
1925 }
1926 }
1927 #endif
1928
1929 *x = pix_x;
1930 *y = pix_y;
1931 }
1932
1933
1934 /* Find the glyph under window-relative coordinates X/Y in window W.
1935 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1936 strings. Return in *HPOS and *VPOS the row and column number of
1937 the glyph found. Return in *AREA the glyph area containing X.
1938 Value is a pointer to the glyph found or null if X/Y is not on
1939 text, or we can't tell because W's current matrix is not up to
1940 date. */
1941
1942 static struct glyph *
1943 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1944 int *dx, int *dy, int *area)
1945 {
1946 struct glyph *glyph, *end;
1947 struct glyph_row *row = NULL;
1948 int x0, i;
1949
1950 /* Find row containing Y. Give up if some row is not enabled. */
1951 for (i = 0; i < w->current_matrix->nrows; ++i)
1952 {
1953 row = MATRIX_ROW (w->current_matrix, i);
1954 if (!row->enabled_p)
1955 return NULL;
1956 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1957 break;
1958 }
1959
1960 *vpos = i;
1961 *hpos = 0;
1962
1963 /* Give up if Y is not in the window. */
1964 if (i == w->current_matrix->nrows)
1965 return NULL;
1966
1967 /* Get the glyph area containing X. */
1968 if (w->pseudo_window_p)
1969 {
1970 *area = TEXT_AREA;
1971 x0 = 0;
1972 }
1973 else
1974 {
1975 if (x < window_box_left_offset (w, TEXT_AREA))
1976 {
1977 *area = LEFT_MARGIN_AREA;
1978 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1979 }
1980 else if (x < window_box_right_offset (w, TEXT_AREA))
1981 {
1982 *area = TEXT_AREA;
1983 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1984 }
1985 else
1986 {
1987 *area = RIGHT_MARGIN_AREA;
1988 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1989 }
1990 }
1991
1992 /* Find glyph containing X. */
1993 glyph = row->glyphs[*area];
1994 end = glyph + row->used[*area];
1995 x -= x0;
1996 while (glyph < end && x >= glyph->pixel_width)
1997 {
1998 x -= glyph->pixel_width;
1999 ++glyph;
2000 }
2001
2002 if (glyph == end)
2003 return NULL;
2004
2005 if (dx)
2006 {
2007 *dx = x;
2008 *dy = y - (row->y + row->ascent - glyph->ascent);
2009 }
2010
2011 *hpos = glyph - row->glyphs[*area];
2012 return glyph;
2013 }
2014
2015 /* Convert frame-relative x/y to coordinates relative to window W.
2016 Takes pseudo-windows into account. */
2017
2018 static void
2019 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2020 {
2021 if (w->pseudo_window_p)
2022 {
2023 /* A pseudo-window is always full-width, and starts at the
2024 left edge of the frame, plus a frame border. */
2025 struct frame *f = XFRAME (w->frame);
2026 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2027 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2028 }
2029 else
2030 {
2031 *x -= WINDOW_LEFT_EDGE_X (w);
2032 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2033 }
2034 }
2035
2036 #ifdef HAVE_WINDOW_SYSTEM
2037
2038 /* EXPORT:
2039 Return in RECTS[] at most N clipping rectangles for glyph string S.
2040 Return the number of stored rectangles. */
2041
2042 int
2043 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2044 {
2045 XRectangle r;
2046
2047 if (n <= 0)
2048 return 0;
2049
2050 if (s->row->full_width_p)
2051 {
2052 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2053 r.x = WINDOW_LEFT_EDGE_X (s->w);
2054 if (s->row->mode_line_p)
2055 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2056 else
2057 r.width = WINDOW_PIXEL_WIDTH (s->w);
2058
2059 /* Unless displaying a mode or menu bar line, which are always
2060 fully visible, clip to the visible part of the row. */
2061 if (s->w->pseudo_window_p)
2062 r.height = s->row->visible_height;
2063 else
2064 r.height = s->height;
2065 }
2066 else
2067 {
2068 /* This is a text line that may be partially visible. */
2069 r.x = window_box_left (s->w, s->area);
2070 r.width = window_box_width (s->w, s->area);
2071 r.height = s->row->visible_height;
2072 }
2073
2074 if (s->clip_head)
2075 if (r.x < s->clip_head->x)
2076 {
2077 if (r.width >= s->clip_head->x - r.x)
2078 r.width -= s->clip_head->x - r.x;
2079 else
2080 r.width = 0;
2081 r.x = s->clip_head->x;
2082 }
2083 if (s->clip_tail)
2084 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2085 {
2086 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2087 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2088 else
2089 r.width = 0;
2090 }
2091
2092 /* If S draws overlapping rows, it's sufficient to use the top and
2093 bottom of the window for clipping because this glyph string
2094 intentionally draws over other lines. */
2095 if (s->for_overlaps)
2096 {
2097 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2098 r.height = window_text_bottom_y (s->w) - r.y;
2099
2100 /* Alas, the above simple strategy does not work for the
2101 environments with anti-aliased text: if the same text is
2102 drawn onto the same place multiple times, it gets thicker.
2103 If the overlap we are processing is for the erased cursor, we
2104 take the intersection with the rectangle of the cursor. */
2105 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2106 {
2107 XRectangle rc, r_save = r;
2108
2109 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2110 rc.y = s->w->phys_cursor.y;
2111 rc.width = s->w->phys_cursor_width;
2112 rc.height = s->w->phys_cursor_height;
2113
2114 x_intersect_rectangles (&r_save, &rc, &r);
2115 }
2116 }
2117 else
2118 {
2119 /* Don't use S->y for clipping because it doesn't take partially
2120 visible lines into account. For example, it can be negative for
2121 partially visible lines at the top of a window. */
2122 if (!s->row->full_width_p
2123 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2124 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2125 else
2126 r.y = max (0, s->row->y);
2127 }
2128
2129 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2130
2131 /* If drawing the cursor, don't let glyph draw outside its
2132 advertised boundaries. Cleartype does this under some circumstances. */
2133 if (s->hl == DRAW_CURSOR)
2134 {
2135 struct glyph *glyph = s->first_glyph;
2136 int height, max_y;
2137
2138 if (s->x > r.x)
2139 {
2140 r.width -= s->x - r.x;
2141 r.x = s->x;
2142 }
2143 r.width = min (r.width, glyph->pixel_width);
2144
2145 /* If r.y is below window bottom, ensure that we still see a cursor. */
2146 height = min (glyph->ascent + glyph->descent,
2147 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2148 max_y = window_text_bottom_y (s->w) - height;
2149 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2150 if (s->ybase - glyph->ascent > max_y)
2151 {
2152 r.y = max_y;
2153 r.height = height;
2154 }
2155 else
2156 {
2157 /* Don't draw cursor glyph taller than our actual glyph. */
2158 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2159 if (height < r.height)
2160 {
2161 max_y = r.y + r.height;
2162 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2163 r.height = min (max_y - r.y, height);
2164 }
2165 }
2166 }
2167
2168 if (s->row->clip)
2169 {
2170 XRectangle r_save = r;
2171
2172 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2173 r.width = 0;
2174 }
2175
2176 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2177 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2178 {
2179 #ifdef CONVERT_FROM_XRECT
2180 CONVERT_FROM_XRECT (r, *rects);
2181 #else
2182 *rects = r;
2183 #endif
2184 return 1;
2185 }
2186 else
2187 {
2188 /* If we are processing overlapping and allowed to return
2189 multiple clipping rectangles, we exclude the row of the glyph
2190 string from the clipping rectangle. This is to avoid drawing
2191 the same text on the environment with anti-aliasing. */
2192 #ifdef CONVERT_FROM_XRECT
2193 XRectangle rs[2];
2194 #else
2195 XRectangle *rs = rects;
2196 #endif
2197 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2198
2199 if (s->for_overlaps & OVERLAPS_PRED)
2200 {
2201 rs[i] = r;
2202 if (r.y + r.height > row_y)
2203 {
2204 if (r.y < row_y)
2205 rs[i].height = row_y - r.y;
2206 else
2207 rs[i].height = 0;
2208 }
2209 i++;
2210 }
2211 if (s->for_overlaps & OVERLAPS_SUCC)
2212 {
2213 rs[i] = r;
2214 if (r.y < row_y + s->row->visible_height)
2215 {
2216 if (r.y + r.height > row_y + s->row->visible_height)
2217 {
2218 rs[i].y = row_y + s->row->visible_height;
2219 rs[i].height = r.y + r.height - rs[i].y;
2220 }
2221 else
2222 rs[i].height = 0;
2223 }
2224 i++;
2225 }
2226
2227 n = i;
2228 #ifdef CONVERT_FROM_XRECT
2229 for (i = 0; i < n; i++)
2230 CONVERT_FROM_XRECT (rs[i], rects[i]);
2231 #endif
2232 return n;
2233 }
2234 }
2235
2236 /* EXPORT:
2237 Return in *NR the clipping rectangle for glyph string S. */
2238
2239 void
2240 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2241 {
2242 get_glyph_string_clip_rects (s, nr, 1);
2243 }
2244
2245
2246 /* EXPORT:
2247 Return the position and height of the phys cursor in window W.
2248 Set w->phys_cursor_width to width of phys cursor.
2249 */
2250
2251 void
2252 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2253 struct glyph *glyph, int *xp, int *yp, int *heightp)
2254 {
2255 struct frame *f = XFRAME (WINDOW_FRAME (w));
2256 int x, y, wd, h, h0, y0;
2257
2258 /* Compute the width of the rectangle to draw. If on a stretch
2259 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2260 rectangle as wide as the glyph, but use a canonical character
2261 width instead. */
2262 wd = glyph->pixel_width - 1;
2263 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2264 wd++; /* Why? */
2265 #endif
2266
2267 x = w->phys_cursor.x;
2268 if (x < 0)
2269 {
2270 wd += x;
2271 x = 0;
2272 }
2273
2274 if (glyph->type == STRETCH_GLYPH
2275 && !x_stretch_cursor_p)
2276 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2277 w->phys_cursor_width = wd;
2278
2279 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2280
2281 /* If y is below window bottom, ensure that we still see a cursor. */
2282 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2283
2284 h = max (h0, glyph->ascent + glyph->descent);
2285 h0 = min (h0, glyph->ascent + glyph->descent);
2286
2287 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2288 if (y < y0)
2289 {
2290 h = max (h - (y0 - y) + 1, h0);
2291 y = y0 - 1;
2292 }
2293 else
2294 {
2295 y0 = window_text_bottom_y (w) - h0;
2296 if (y > y0)
2297 {
2298 h += y - y0;
2299 y = y0;
2300 }
2301 }
2302
2303 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2304 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2305 *heightp = h;
2306 }
2307
2308 /*
2309 * Remember which glyph the mouse is over.
2310 */
2311
2312 void
2313 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2314 {
2315 Lisp_Object window;
2316 struct window *w;
2317 struct glyph_row *r, *gr, *end_row;
2318 enum window_part part;
2319 enum glyph_row_area area;
2320 int x, y, width, height;
2321
2322 /* Try to determine frame pixel position and size of the glyph under
2323 frame pixel coordinates X/Y on frame F. */
2324
2325 if (window_resize_pixelwise)
2326 {
2327 width = height = 1;
2328 goto virtual_glyph;
2329 }
2330 else if (!f->glyphs_initialized_p
2331 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2332 NILP (window)))
2333 {
2334 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2335 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2336 goto virtual_glyph;
2337 }
2338
2339 w = XWINDOW (window);
2340 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2341 height = WINDOW_FRAME_LINE_HEIGHT (w);
2342
2343 x = window_relative_x_coord (w, part, gx);
2344 y = gy - WINDOW_TOP_EDGE_Y (w);
2345
2346 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2347 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2348
2349 if (w->pseudo_window_p)
2350 {
2351 area = TEXT_AREA;
2352 part = ON_MODE_LINE; /* Don't adjust margin. */
2353 goto text_glyph;
2354 }
2355
2356 switch (part)
2357 {
2358 case ON_LEFT_MARGIN:
2359 area = LEFT_MARGIN_AREA;
2360 goto text_glyph;
2361
2362 case ON_RIGHT_MARGIN:
2363 area = RIGHT_MARGIN_AREA;
2364 goto text_glyph;
2365
2366 case ON_HEADER_LINE:
2367 case ON_MODE_LINE:
2368 gr = (part == ON_HEADER_LINE
2369 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2370 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2371 gy = gr->y;
2372 area = TEXT_AREA;
2373 goto text_glyph_row_found;
2374
2375 case ON_TEXT:
2376 area = TEXT_AREA;
2377
2378 text_glyph:
2379 gr = 0; gy = 0;
2380 for (; r <= end_row && r->enabled_p; ++r)
2381 if (r->y + r->height > y)
2382 {
2383 gr = r; gy = r->y;
2384 break;
2385 }
2386
2387 text_glyph_row_found:
2388 if (gr && gy <= y)
2389 {
2390 struct glyph *g = gr->glyphs[area];
2391 struct glyph *end = g + gr->used[area];
2392
2393 height = gr->height;
2394 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2395 if (gx + g->pixel_width > x)
2396 break;
2397
2398 if (g < end)
2399 {
2400 if (g->type == IMAGE_GLYPH)
2401 {
2402 /* Don't remember when mouse is over image, as
2403 image may have hot-spots. */
2404 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2405 return;
2406 }
2407 width = g->pixel_width;
2408 }
2409 else
2410 {
2411 /* Use nominal char spacing at end of line. */
2412 x -= gx;
2413 gx += (x / width) * width;
2414 }
2415
2416 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2417 gx += window_box_left_offset (w, area);
2418 }
2419 else
2420 {
2421 /* Use nominal line height at end of window. */
2422 gx = (x / width) * width;
2423 y -= gy;
2424 gy += (y / height) * height;
2425 }
2426 break;
2427
2428 case ON_LEFT_FRINGE:
2429 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2430 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2431 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2432 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2433 goto row_glyph;
2434
2435 case ON_RIGHT_FRINGE:
2436 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2438 : window_box_right_offset (w, TEXT_AREA));
2439 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2440 goto row_glyph;
2441
2442 case ON_SCROLL_BAR:
2443 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2444 ? 0
2445 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2446 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2447 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2448 : 0)));
2449 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2450
2451 row_glyph:
2452 gr = 0, gy = 0;
2453 for (; r <= end_row && r->enabled_p; ++r)
2454 if (r->y + r->height > y)
2455 {
2456 gr = r; gy = r->y;
2457 break;
2458 }
2459
2460 if (gr && gy <= y)
2461 height = gr->height;
2462 else
2463 {
2464 /* Use nominal line height at end of window. */
2465 y -= gy;
2466 gy += (y / height) * height;
2467 }
2468 break;
2469
2470 default:
2471 ;
2472 virtual_glyph:
2473 /* If there is no glyph under the mouse, then we divide the screen
2474 into a grid of the smallest glyph in the frame, and use that
2475 as our "glyph". */
2476
2477 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2478 round down even for negative values. */
2479 if (gx < 0)
2480 gx -= width - 1;
2481 if (gy < 0)
2482 gy -= height - 1;
2483
2484 gx = (gx / width) * width;
2485 gy = (gy / height) * height;
2486
2487 goto store_rect;
2488 }
2489
2490 gx += WINDOW_LEFT_EDGE_X (w);
2491 gy += WINDOW_TOP_EDGE_Y (w);
2492
2493 store_rect:
2494 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2495
2496 /* Visible feedback for debugging. */
2497 #if 0
2498 #if HAVE_X_WINDOWS
2499 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2500 f->output_data.x->normal_gc,
2501 gx, gy, width, height);
2502 #endif
2503 #endif
2504 }
2505
2506
2507 #endif /* HAVE_WINDOW_SYSTEM */
2508
2509 static void
2510 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2511 {
2512 eassert (w);
2513 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2514 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2515 w->window_end_vpos
2516 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2517 }
2518
2519 /***********************************************************************
2520 Lisp form evaluation
2521 ***********************************************************************/
2522
2523 /* Error handler for safe_eval and safe_call. */
2524
2525 static Lisp_Object
2526 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2527 {
2528 add_to_log ("Error during redisplay: %S signaled %S",
2529 Flist (nargs, args), arg);
2530 return Qnil;
2531 }
2532
2533 /* Call function FUNC with the rest of NARGS - 1 arguments
2534 following. Return the result, or nil if something went
2535 wrong. Prevent redisplay during the evaluation. */
2536
2537 Lisp_Object
2538 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2539 {
2540 Lisp_Object val;
2541
2542 if (inhibit_eval_during_redisplay)
2543 val = Qnil;
2544 else
2545 {
2546 va_list ap;
2547 ptrdiff_t i;
2548 ptrdiff_t count = SPECPDL_INDEX ();
2549 struct gcpro gcpro1;
2550 Lisp_Object *args = alloca (nargs * word_size);
2551
2552 args[0] = func;
2553 va_start (ap, func);
2554 for (i = 1; i < nargs; i++)
2555 args[i] = va_arg (ap, Lisp_Object);
2556 va_end (ap);
2557
2558 GCPRO1 (args[0]);
2559 gcpro1.nvars = nargs;
2560 specbind (Qinhibit_redisplay, Qt);
2561 /* Use Qt to ensure debugger does not run,
2562 so there is no possibility of wanting to redisplay. */
2563 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2564 safe_eval_handler);
2565 UNGCPRO;
2566 val = unbind_to (count, val);
2567 }
2568
2569 return val;
2570 }
2571
2572
2573 /* Call function FN with one argument ARG.
2574 Return the result, or nil if something went wrong. */
2575
2576 Lisp_Object
2577 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2578 {
2579 return safe_call (2, fn, arg);
2580 }
2581
2582 static Lisp_Object Qeval;
2583
2584 Lisp_Object
2585 safe_eval (Lisp_Object sexpr)
2586 {
2587 return safe_call1 (Qeval, sexpr);
2588 }
2589
2590 /* Call function FN with two arguments ARG1 and ARG2.
2591 Return the result, or nil if something went wrong. */
2592
2593 Lisp_Object
2594 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2595 {
2596 return safe_call (3, fn, arg1, arg2);
2597 }
2598
2599
2600 \f
2601 /***********************************************************************
2602 Debugging
2603 ***********************************************************************/
2604
2605 #if 0
2606
2607 /* Define CHECK_IT to perform sanity checks on iterators.
2608 This is for debugging. It is too slow to do unconditionally. */
2609
2610 static void
2611 check_it (struct it *it)
2612 {
2613 if (it->method == GET_FROM_STRING)
2614 {
2615 eassert (STRINGP (it->string));
2616 eassert (IT_STRING_CHARPOS (*it) >= 0);
2617 }
2618 else
2619 {
2620 eassert (IT_STRING_CHARPOS (*it) < 0);
2621 if (it->method == GET_FROM_BUFFER)
2622 {
2623 /* Check that character and byte positions agree. */
2624 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2625 }
2626 }
2627
2628 if (it->dpvec)
2629 eassert (it->current.dpvec_index >= 0);
2630 else
2631 eassert (it->current.dpvec_index < 0);
2632 }
2633
2634 #define CHECK_IT(IT) check_it ((IT))
2635
2636 #else /* not 0 */
2637
2638 #define CHECK_IT(IT) (void) 0
2639
2640 #endif /* not 0 */
2641
2642
2643 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2644
2645 /* Check that the window end of window W is what we expect it
2646 to be---the last row in the current matrix displaying text. */
2647
2648 static void
2649 check_window_end (struct window *w)
2650 {
2651 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2652 {
2653 struct glyph_row *row;
2654 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2655 !row->enabled_p
2656 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2657 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2658 }
2659 }
2660
2661 #define CHECK_WINDOW_END(W) check_window_end ((W))
2662
2663 #else
2664
2665 #define CHECK_WINDOW_END(W) (void) 0
2666
2667 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2668
2669 /***********************************************************************
2670 Iterator initialization
2671 ***********************************************************************/
2672
2673 /* Initialize IT for displaying current_buffer in window W, starting
2674 at character position CHARPOS. CHARPOS < 0 means that no buffer
2675 position is specified which is useful when the iterator is assigned
2676 a position later. BYTEPOS is the byte position corresponding to
2677 CHARPOS.
2678
2679 If ROW is not null, calls to produce_glyphs with IT as parameter
2680 will produce glyphs in that row.
2681
2682 BASE_FACE_ID is the id of a base face to use. It must be one of
2683 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2684 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2685 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2686
2687 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2688 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2689 will be initialized to use the corresponding mode line glyph row of
2690 the desired matrix of W. */
2691
2692 void
2693 init_iterator (struct it *it, struct window *w,
2694 ptrdiff_t charpos, ptrdiff_t bytepos,
2695 struct glyph_row *row, enum face_id base_face_id)
2696 {
2697 enum face_id remapped_base_face_id = base_face_id;
2698
2699 /* Some precondition checks. */
2700 eassert (w != NULL && it != NULL);
2701 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2702 && charpos <= ZV));
2703
2704 /* If face attributes have been changed since the last redisplay,
2705 free realized faces now because they depend on face definitions
2706 that might have changed. Don't free faces while there might be
2707 desired matrices pending which reference these faces. */
2708 if (face_change_count && !inhibit_free_realized_faces)
2709 {
2710 face_change_count = 0;
2711 free_all_realized_faces (Qnil);
2712 }
2713
2714 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2715 if (! NILP (Vface_remapping_alist))
2716 remapped_base_face_id
2717 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2718
2719 /* Use one of the mode line rows of W's desired matrix if
2720 appropriate. */
2721 if (row == NULL)
2722 {
2723 if (base_face_id == MODE_LINE_FACE_ID
2724 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2725 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2726 else if (base_face_id == HEADER_LINE_FACE_ID)
2727 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2728 }
2729
2730 /* Clear IT. */
2731 memset (it, 0, sizeof *it);
2732 it->current.overlay_string_index = -1;
2733 it->current.dpvec_index = -1;
2734 it->base_face_id = remapped_base_face_id;
2735 it->string = Qnil;
2736 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2737 it->paragraph_embedding = L2R;
2738 it->bidi_it.string.lstring = Qnil;
2739 it->bidi_it.string.s = NULL;
2740 it->bidi_it.string.bufpos = 0;
2741 it->bidi_it.w = w;
2742
2743 /* The window in which we iterate over current_buffer: */
2744 XSETWINDOW (it->window, w);
2745 it->w = w;
2746 it->f = XFRAME (w->frame);
2747
2748 it->cmp_it.id = -1;
2749
2750 /* Extra space between lines (on window systems only). */
2751 if (base_face_id == DEFAULT_FACE_ID
2752 && FRAME_WINDOW_P (it->f))
2753 {
2754 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2755 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2756 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2757 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2758 * FRAME_LINE_HEIGHT (it->f));
2759 else if (it->f->extra_line_spacing > 0)
2760 it->extra_line_spacing = it->f->extra_line_spacing;
2761 it->max_extra_line_spacing = 0;
2762 }
2763
2764 /* If realized faces have been removed, e.g. because of face
2765 attribute changes of named faces, recompute them. When running
2766 in batch mode, the face cache of the initial frame is null. If
2767 we happen to get called, make a dummy face cache. */
2768 if (FRAME_FACE_CACHE (it->f) == NULL)
2769 init_frame_faces (it->f);
2770 if (FRAME_FACE_CACHE (it->f)->used == 0)
2771 recompute_basic_faces (it->f);
2772
2773 /* Current value of the `slice', `space-width', and 'height' properties. */
2774 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2775 it->space_width = Qnil;
2776 it->font_height = Qnil;
2777 it->override_ascent = -1;
2778
2779 /* Are control characters displayed as `^C'? */
2780 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2781
2782 /* -1 means everything between a CR and the following line end
2783 is invisible. >0 means lines indented more than this value are
2784 invisible. */
2785 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2786 ? (clip_to_bounds
2787 (-1, XINT (BVAR (current_buffer, selective_display)),
2788 PTRDIFF_MAX))
2789 : (!NILP (BVAR (current_buffer, selective_display))
2790 ? -1 : 0));
2791 it->selective_display_ellipsis_p
2792 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2793
2794 /* Display table to use. */
2795 it->dp = window_display_table (w);
2796
2797 /* Are multibyte characters enabled in current_buffer? */
2798 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2799
2800 /* Get the position at which the redisplay_end_trigger hook should
2801 be run, if it is to be run at all. */
2802 if (MARKERP (w->redisplay_end_trigger)
2803 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2804 it->redisplay_end_trigger_charpos
2805 = marker_position (w->redisplay_end_trigger);
2806 else if (INTEGERP (w->redisplay_end_trigger))
2807 it->redisplay_end_trigger_charpos =
2808 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2809
2810 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2811
2812 /* Are lines in the display truncated? */
2813 if (base_face_id != DEFAULT_FACE_ID
2814 || it->w->hscroll
2815 || (! WINDOW_FULL_WIDTH_P (it->w)
2816 && ((!NILP (Vtruncate_partial_width_windows)
2817 && !INTEGERP (Vtruncate_partial_width_windows))
2818 || (INTEGERP (Vtruncate_partial_width_windows)
2819 /* PXW: Shall we do something about this? */
2820 && (WINDOW_TOTAL_COLS (it->w)
2821 < XINT (Vtruncate_partial_width_windows))))))
2822 it->line_wrap = TRUNCATE;
2823 else if (NILP (BVAR (current_buffer, truncate_lines)))
2824 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2825 ? WINDOW_WRAP : WORD_WRAP;
2826 else
2827 it->line_wrap = TRUNCATE;
2828
2829 /* Get dimensions of truncation and continuation glyphs. These are
2830 displayed as fringe bitmaps under X, but we need them for such
2831 frames when the fringes are turned off. But leave the dimensions
2832 zero for tooltip frames, as these glyphs look ugly there and also
2833 sabotage calculations of tooltip dimensions in x-show-tip. */
2834 #ifdef HAVE_WINDOW_SYSTEM
2835 if (!(FRAME_WINDOW_P (it->f)
2836 && FRAMEP (tip_frame)
2837 && it->f == XFRAME (tip_frame)))
2838 #endif
2839 {
2840 if (it->line_wrap == TRUNCATE)
2841 {
2842 /* We will need the truncation glyph. */
2843 eassert (it->glyph_row == NULL);
2844 produce_special_glyphs (it, IT_TRUNCATION);
2845 it->truncation_pixel_width = it->pixel_width;
2846 }
2847 else
2848 {
2849 /* We will need the continuation glyph. */
2850 eassert (it->glyph_row == NULL);
2851 produce_special_glyphs (it, IT_CONTINUATION);
2852 it->continuation_pixel_width = it->pixel_width;
2853 }
2854 }
2855
2856 /* Reset these values to zero because the produce_special_glyphs
2857 above has changed them. */
2858 it->pixel_width = it->ascent = it->descent = 0;
2859 it->phys_ascent = it->phys_descent = 0;
2860
2861 /* Set this after getting the dimensions of truncation and
2862 continuation glyphs, so that we don't produce glyphs when calling
2863 produce_special_glyphs, above. */
2864 it->glyph_row = row;
2865 it->area = TEXT_AREA;
2866
2867 /* Forget any previous info about this row being reversed. */
2868 if (it->glyph_row)
2869 it->glyph_row->reversed_p = 0;
2870
2871 /* Get the dimensions of the display area. The display area
2872 consists of the visible window area plus a horizontally scrolled
2873 part to the left of the window. All x-values are relative to the
2874 start of this total display area. */
2875 if (base_face_id != DEFAULT_FACE_ID)
2876 {
2877 /* Mode lines, menu bar in terminal frames. */
2878 it->first_visible_x = 0;
2879 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2880 }
2881 else
2882 {
2883 it->first_visible_x
2884 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2885 it->last_visible_x = (it->first_visible_x
2886 + window_box_width (w, TEXT_AREA));
2887
2888 /* If we truncate lines, leave room for the truncation glyph(s) at
2889 the right margin. Otherwise, leave room for the continuation
2890 glyph(s). Done only if the window has no fringes. Since we
2891 don't know at this point whether there will be any R2L lines in
2892 the window, we reserve space for truncation/continuation glyphs
2893 even if only one of the fringes is absent. */
2894 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2895 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2896 {
2897 if (it->line_wrap == TRUNCATE)
2898 it->last_visible_x -= it->truncation_pixel_width;
2899 else
2900 it->last_visible_x -= it->continuation_pixel_width;
2901 }
2902
2903 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2904 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2905 }
2906
2907 /* Leave room for a border glyph. */
2908 if (!FRAME_WINDOW_P (it->f)
2909 && !WINDOW_RIGHTMOST_P (it->w))
2910 it->last_visible_x -= 1;
2911
2912 it->last_visible_y = window_text_bottom_y (w);
2913
2914 /* For mode lines and alike, arrange for the first glyph having a
2915 left box line if the face specifies a box. */
2916 if (base_face_id != DEFAULT_FACE_ID)
2917 {
2918 struct face *face;
2919
2920 it->face_id = remapped_base_face_id;
2921
2922 /* If we have a boxed mode line, make the first character appear
2923 with a left box line. */
2924 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2925 if (face->box != FACE_NO_BOX)
2926 it->start_of_box_run_p = true;
2927 }
2928
2929 /* If a buffer position was specified, set the iterator there,
2930 getting overlays and face properties from that position. */
2931 if (charpos >= BUF_BEG (current_buffer))
2932 {
2933 it->end_charpos = ZV;
2934 eassert (charpos == BYTE_TO_CHAR (bytepos));
2935 IT_CHARPOS (*it) = charpos;
2936 IT_BYTEPOS (*it) = bytepos;
2937
2938 /* We will rely on `reseat' to set this up properly, via
2939 handle_face_prop. */
2940 it->face_id = it->base_face_id;
2941
2942 it->start = it->current;
2943 /* Do we need to reorder bidirectional text? Not if this is a
2944 unibyte buffer: by definition, none of the single-byte
2945 characters are strong R2L, so no reordering is needed. And
2946 bidi.c doesn't support unibyte buffers anyway. Also, don't
2947 reorder while we are loading loadup.el, since the tables of
2948 character properties needed for reordering are not yet
2949 available. */
2950 it->bidi_p =
2951 NILP (Vpurify_flag)
2952 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2953 && it->multibyte_p;
2954
2955 /* If we are to reorder bidirectional text, init the bidi
2956 iterator. */
2957 if (it->bidi_p)
2958 {
2959 /* Note the paragraph direction that this buffer wants to
2960 use. */
2961 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2962 Qleft_to_right))
2963 it->paragraph_embedding = L2R;
2964 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2965 Qright_to_left))
2966 it->paragraph_embedding = R2L;
2967 else
2968 it->paragraph_embedding = NEUTRAL_DIR;
2969 bidi_unshelve_cache (NULL, 0);
2970 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2971 &it->bidi_it);
2972 }
2973
2974 /* Compute faces etc. */
2975 reseat (it, it->current.pos, 1);
2976 }
2977
2978 CHECK_IT (it);
2979 }
2980
2981
2982 /* Initialize IT for the display of window W with window start POS. */
2983
2984 void
2985 start_display (struct it *it, struct window *w, struct text_pos pos)
2986 {
2987 struct glyph_row *row;
2988 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2989
2990 row = w->desired_matrix->rows + first_vpos;
2991 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2992 it->first_vpos = first_vpos;
2993
2994 /* Don't reseat to previous visible line start if current start
2995 position is in a string or image. */
2996 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2997 {
2998 int start_at_line_beg_p;
2999 int first_y = it->current_y;
3000
3001 /* If window start is not at a line start, skip forward to POS to
3002 get the correct continuation lines width. */
3003 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3004 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3005 if (!start_at_line_beg_p)
3006 {
3007 int new_x;
3008
3009 reseat_at_previous_visible_line_start (it);
3010 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3011
3012 new_x = it->current_x + it->pixel_width;
3013
3014 /* If lines are continued, this line may end in the middle
3015 of a multi-glyph character (e.g. a control character
3016 displayed as \003, or in the middle of an overlay
3017 string). In this case move_it_to above will not have
3018 taken us to the start of the continuation line but to the
3019 end of the continued line. */
3020 if (it->current_x > 0
3021 && it->line_wrap != TRUNCATE /* Lines are continued. */
3022 && (/* And glyph doesn't fit on the line. */
3023 new_x > it->last_visible_x
3024 /* Or it fits exactly and we're on a window
3025 system frame. */
3026 || (new_x == it->last_visible_x
3027 && FRAME_WINDOW_P (it->f)
3028 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3029 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3030 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3031 {
3032 if ((it->current.dpvec_index >= 0
3033 || it->current.overlay_string_index >= 0)
3034 /* If we are on a newline from a display vector or
3035 overlay string, then we are already at the end of
3036 a screen line; no need to go to the next line in
3037 that case, as this line is not really continued.
3038 (If we do go to the next line, C-e will not DTRT.) */
3039 && it->c != '\n')
3040 {
3041 set_iterator_to_next (it, 1);
3042 move_it_in_display_line_to (it, -1, -1, 0);
3043 }
3044
3045 it->continuation_lines_width += it->current_x;
3046 }
3047 /* If the character at POS is displayed via a display
3048 vector, move_it_to above stops at the final glyph of
3049 IT->dpvec. To make the caller redisplay that character
3050 again (a.k.a. start at POS), we need to reset the
3051 dpvec_index to the beginning of IT->dpvec. */
3052 else if (it->current.dpvec_index >= 0)
3053 it->current.dpvec_index = 0;
3054
3055 /* We're starting a new display line, not affected by the
3056 height of the continued line, so clear the appropriate
3057 fields in the iterator structure. */
3058 it->max_ascent = it->max_descent = 0;
3059 it->max_phys_ascent = it->max_phys_descent = 0;
3060
3061 it->current_y = first_y;
3062 it->vpos = 0;
3063 it->current_x = it->hpos = 0;
3064 }
3065 }
3066 }
3067
3068
3069 /* Return 1 if POS is a position in ellipses displayed for invisible
3070 text. W is the window we display, for text property lookup. */
3071
3072 static int
3073 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3074 {
3075 Lisp_Object prop, window;
3076 int ellipses_p = 0;
3077 ptrdiff_t charpos = CHARPOS (pos->pos);
3078
3079 /* If POS specifies a position in a display vector, this might
3080 be for an ellipsis displayed for invisible text. We won't
3081 get the iterator set up for delivering that ellipsis unless
3082 we make sure that it gets aware of the invisible text. */
3083 if (pos->dpvec_index >= 0
3084 && pos->overlay_string_index < 0
3085 && CHARPOS (pos->string_pos) < 0
3086 && charpos > BEGV
3087 && (XSETWINDOW (window, w),
3088 prop = Fget_char_property (make_number (charpos),
3089 Qinvisible, window),
3090 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3091 {
3092 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3093 window);
3094 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3095 }
3096
3097 return ellipses_p;
3098 }
3099
3100
3101 /* Initialize IT for stepping through current_buffer in window W,
3102 starting at position POS that includes overlay string and display
3103 vector/ control character translation position information. Value
3104 is zero if there are overlay strings with newlines at POS. */
3105
3106 static int
3107 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3108 {
3109 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3110 int i, overlay_strings_with_newlines = 0;
3111
3112 /* If POS specifies a position in a display vector, this might
3113 be for an ellipsis displayed for invisible text. We won't
3114 get the iterator set up for delivering that ellipsis unless
3115 we make sure that it gets aware of the invisible text. */
3116 if (in_ellipses_for_invisible_text_p (pos, w))
3117 {
3118 --charpos;
3119 bytepos = 0;
3120 }
3121
3122 /* Keep in mind: the call to reseat in init_iterator skips invisible
3123 text, so we might end up at a position different from POS. This
3124 is only a problem when POS is a row start after a newline and an
3125 overlay starts there with an after-string, and the overlay has an
3126 invisible property. Since we don't skip invisible text in
3127 display_line and elsewhere immediately after consuming the
3128 newline before the row start, such a POS will not be in a string,
3129 but the call to init_iterator below will move us to the
3130 after-string. */
3131 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3132
3133 /* This only scans the current chunk -- it should scan all chunks.
3134 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3135 to 16 in 22.1 to make this a lesser problem. */
3136 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3137 {
3138 const char *s = SSDATA (it->overlay_strings[i]);
3139 const char *e = s + SBYTES (it->overlay_strings[i]);
3140
3141 while (s < e && *s != '\n')
3142 ++s;
3143
3144 if (s < e)
3145 {
3146 overlay_strings_with_newlines = 1;
3147 break;
3148 }
3149 }
3150
3151 /* If position is within an overlay string, set up IT to the right
3152 overlay string. */
3153 if (pos->overlay_string_index >= 0)
3154 {
3155 int relative_index;
3156
3157 /* If the first overlay string happens to have a `display'
3158 property for an image, the iterator will be set up for that
3159 image, and we have to undo that setup first before we can
3160 correct the overlay string index. */
3161 if (it->method == GET_FROM_IMAGE)
3162 pop_it (it);
3163
3164 /* We already have the first chunk of overlay strings in
3165 IT->overlay_strings. Load more until the one for
3166 pos->overlay_string_index is in IT->overlay_strings. */
3167 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3168 {
3169 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3170 it->current.overlay_string_index = 0;
3171 while (n--)
3172 {
3173 load_overlay_strings (it, 0);
3174 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3175 }
3176 }
3177
3178 it->current.overlay_string_index = pos->overlay_string_index;
3179 relative_index = (it->current.overlay_string_index
3180 % OVERLAY_STRING_CHUNK_SIZE);
3181 it->string = it->overlay_strings[relative_index];
3182 eassert (STRINGP (it->string));
3183 it->current.string_pos = pos->string_pos;
3184 it->method = GET_FROM_STRING;
3185 it->end_charpos = SCHARS (it->string);
3186 /* Set up the bidi iterator for this overlay string. */
3187 if (it->bidi_p)
3188 {
3189 it->bidi_it.string.lstring = it->string;
3190 it->bidi_it.string.s = NULL;
3191 it->bidi_it.string.schars = SCHARS (it->string);
3192 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3193 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3194 it->bidi_it.string.unibyte = !it->multibyte_p;
3195 it->bidi_it.w = it->w;
3196 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3197 FRAME_WINDOW_P (it->f), &it->bidi_it);
3198
3199 /* Synchronize the state of the bidi iterator with
3200 pos->string_pos. For any string position other than
3201 zero, this will be done automagically when we resume
3202 iteration over the string and get_visually_first_element
3203 is called. But if string_pos is zero, and the string is
3204 to be reordered for display, we need to resync manually,
3205 since it could be that the iteration state recorded in
3206 pos ended at string_pos of 0 moving backwards in string. */
3207 if (CHARPOS (pos->string_pos) == 0)
3208 {
3209 get_visually_first_element (it);
3210 if (IT_STRING_CHARPOS (*it) != 0)
3211 do {
3212 /* Paranoia. */
3213 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3214 bidi_move_to_visually_next (&it->bidi_it);
3215 } while (it->bidi_it.charpos != 0);
3216 }
3217 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3218 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3219 }
3220 }
3221
3222 if (CHARPOS (pos->string_pos) >= 0)
3223 {
3224 /* Recorded position is not in an overlay string, but in another
3225 string. This can only be a string from a `display' property.
3226 IT should already be filled with that string. */
3227 it->current.string_pos = pos->string_pos;
3228 eassert (STRINGP (it->string));
3229 if (it->bidi_p)
3230 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3231 FRAME_WINDOW_P (it->f), &it->bidi_it);
3232 }
3233
3234 /* Restore position in display vector translations, control
3235 character translations or ellipses. */
3236 if (pos->dpvec_index >= 0)
3237 {
3238 if (it->dpvec == NULL)
3239 get_next_display_element (it);
3240 eassert (it->dpvec && it->current.dpvec_index == 0);
3241 it->current.dpvec_index = pos->dpvec_index;
3242 }
3243
3244 CHECK_IT (it);
3245 return !overlay_strings_with_newlines;
3246 }
3247
3248
3249 /* Initialize IT for stepping through current_buffer in window W
3250 starting at ROW->start. */
3251
3252 static void
3253 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3254 {
3255 init_from_display_pos (it, w, &row->start);
3256 it->start = row->start;
3257 it->continuation_lines_width = row->continuation_lines_width;
3258 CHECK_IT (it);
3259 }
3260
3261
3262 /* Initialize IT for stepping through current_buffer in window W
3263 starting in the line following ROW, i.e. starting at ROW->end.
3264 Value is zero if there are overlay strings with newlines at ROW's
3265 end position. */
3266
3267 static int
3268 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3269 {
3270 int success = 0;
3271
3272 if (init_from_display_pos (it, w, &row->end))
3273 {
3274 if (row->continued_p)
3275 it->continuation_lines_width
3276 = row->continuation_lines_width + row->pixel_width;
3277 CHECK_IT (it);
3278 success = 1;
3279 }
3280
3281 return success;
3282 }
3283
3284
3285
3286 \f
3287 /***********************************************************************
3288 Text properties
3289 ***********************************************************************/
3290
3291 /* Called when IT reaches IT->stop_charpos. Handle text property and
3292 overlay changes. Set IT->stop_charpos to the next position where
3293 to stop. */
3294
3295 static void
3296 handle_stop (struct it *it)
3297 {
3298 enum prop_handled handled;
3299 int handle_overlay_change_p;
3300 struct props *p;
3301
3302 it->dpvec = NULL;
3303 it->current.dpvec_index = -1;
3304 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3305 it->ignore_overlay_strings_at_pos_p = 0;
3306 it->ellipsis_p = 0;
3307
3308 /* Use face of preceding text for ellipsis (if invisible) */
3309 if (it->selective_display_ellipsis_p)
3310 it->saved_face_id = it->face_id;
3311
3312 do
3313 {
3314 handled = HANDLED_NORMALLY;
3315
3316 /* Call text property handlers. */
3317 for (p = it_props; p->handler; ++p)
3318 {
3319 handled = p->handler (it);
3320
3321 if (handled == HANDLED_RECOMPUTE_PROPS)
3322 break;
3323 else if (handled == HANDLED_RETURN)
3324 {
3325 /* We still want to show before and after strings from
3326 overlays even if the actual buffer text is replaced. */
3327 if (!handle_overlay_change_p
3328 || it->sp > 1
3329 /* Don't call get_overlay_strings_1 if we already
3330 have overlay strings loaded, because doing so
3331 will load them again and push the iterator state
3332 onto the stack one more time, which is not
3333 expected by the rest of the code that processes
3334 overlay strings. */
3335 || (it->current.overlay_string_index < 0
3336 ? !get_overlay_strings_1 (it, 0, 0)
3337 : 0))
3338 {
3339 if (it->ellipsis_p)
3340 setup_for_ellipsis (it, 0);
3341 /* When handling a display spec, we might load an
3342 empty string. In that case, discard it here. We
3343 used to discard it in handle_single_display_spec,
3344 but that causes get_overlay_strings_1, above, to
3345 ignore overlay strings that we must check. */
3346 if (STRINGP (it->string) && !SCHARS (it->string))
3347 pop_it (it);
3348 return;
3349 }
3350 else if (STRINGP (it->string) && !SCHARS (it->string))
3351 pop_it (it);
3352 else
3353 {
3354 it->ignore_overlay_strings_at_pos_p = true;
3355 it->string_from_display_prop_p = 0;
3356 it->from_disp_prop_p = 0;
3357 handle_overlay_change_p = 0;
3358 }
3359 handled = HANDLED_RECOMPUTE_PROPS;
3360 break;
3361 }
3362 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3363 handle_overlay_change_p = 0;
3364 }
3365
3366 if (handled != HANDLED_RECOMPUTE_PROPS)
3367 {
3368 /* Don't check for overlay strings below when set to deliver
3369 characters from a display vector. */
3370 if (it->method == GET_FROM_DISPLAY_VECTOR)
3371 handle_overlay_change_p = 0;
3372
3373 /* Handle overlay changes.
3374 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3375 if it finds overlays. */
3376 if (handle_overlay_change_p)
3377 handled = handle_overlay_change (it);
3378 }
3379
3380 if (it->ellipsis_p)
3381 {
3382 setup_for_ellipsis (it, 0);
3383 break;
3384 }
3385 }
3386 while (handled == HANDLED_RECOMPUTE_PROPS);
3387
3388 /* Determine where to stop next. */
3389 if (handled == HANDLED_NORMALLY)
3390 compute_stop_pos (it);
3391 }
3392
3393
3394 /* Compute IT->stop_charpos from text property and overlay change
3395 information for IT's current position. */
3396
3397 static void
3398 compute_stop_pos (struct it *it)
3399 {
3400 register INTERVAL iv, next_iv;
3401 Lisp_Object object, limit, position;
3402 ptrdiff_t charpos, bytepos;
3403
3404 if (STRINGP (it->string))
3405 {
3406 /* Strings are usually short, so don't limit the search for
3407 properties. */
3408 it->stop_charpos = it->end_charpos;
3409 object = it->string;
3410 limit = Qnil;
3411 charpos = IT_STRING_CHARPOS (*it);
3412 bytepos = IT_STRING_BYTEPOS (*it);
3413 }
3414 else
3415 {
3416 ptrdiff_t pos;
3417
3418 /* If end_charpos is out of range for some reason, such as a
3419 misbehaving display function, rationalize it (Bug#5984). */
3420 if (it->end_charpos > ZV)
3421 it->end_charpos = ZV;
3422 it->stop_charpos = it->end_charpos;
3423
3424 /* If next overlay change is in front of the current stop pos
3425 (which is IT->end_charpos), stop there. Note: value of
3426 next_overlay_change is point-max if no overlay change
3427 follows. */
3428 charpos = IT_CHARPOS (*it);
3429 bytepos = IT_BYTEPOS (*it);
3430 pos = next_overlay_change (charpos);
3431 if (pos < it->stop_charpos)
3432 it->stop_charpos = pos;
3433
3434 /* Set up variables for computing the stop position from text
3435 property changes. */
3436 XSETBUFFER (object, current_buffer);
3437 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3438 }
3439
3440 /* Get the interval containing IT's position. Value is a null
3441 interval if there isn't such an interval. */
3442 position = make_number (charpos);
3443 iv = validate_interval_range (object, &position, &position, 0);
3444 if (iv)
3445 {
3446 Lisp_Object values_here[LAST_PROP_IDX];
3447 struct props *p;
3448
3449 /* Get properties here. */
3450 for (p = it_props; p->handler; ++p)
3451 values_here[p->idx] = textget (iv->plist, *p->name);
3452
3453 /* Look for an interval following iv that has different
3454 properties. */
3455 for (next_iv = next_interval (iv);
3456 (next_iv
3457 && (NILP (limit)
3458 || XFASTINT (limit) > next_iv->position));
3459 next_iv = next_interval (next_iv))
3460 {
3461 for (p = it_props; p->handler; ++p)
3462 {
3463 Lisp_Object new_value;
3464
3465 new_value = textget (next_iv->plist, *p->name);
3466 if (!EQ (values_here[p->idx], new_value))
3467 break;
3468 }
3469
3470 if (p->handler)
3471 break;
3472 }
3473
3474 if (next_iv)
3475 {
3476 if (INTEGERP (limit)
3477 && next_iv->position >= XFASTINT (limit))
3478 /* No text property change up to limit. */
3479 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3480 else
3481 /* Text properties change in next_iv. */
3482 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3483 }
3484 }
3485
3486 if (it->cmp_it.id < 0)
3487 {
3488 ptrdiff_t stoppos = it->end_charpos;
3489
3490 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3491 stoppos = -1;
3492 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3493 stoppos, it->string);
3494 }
3495
3496 eassert (STRINGP (it->string)
3497 || (it->stop_charpos >= BEGV
3498 && it->stop_charpos >= IT_CHARPOS (*it)));
3499 }
3500
3501
3502 /* Return the position of the next overlay change after POS in
3503 current_buffer. Value is point-max if no overlay change
3504 follows. This is like `next-overlay-change' but doesn't use
3505 xmalloc. */
3506
3507 static ptrdiff_t
3508 next_overlay_change (ptrdiff_t pos)
3509 {
3510 ptrdiff_t i, noverlays;
3511 ptrdiff_t endpos;
3512 Lisp_Object *overlays;
3513
3514 /* Get all overlays at the given position. */
3515 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3516
3517 /* If any of these overlays ends before endpos,
3518 use its ending point instead. */
3519 for (i = 0; i < noverlays; ++i)
3520 {
3521 Lisp_Object oend;
3522 ptrdiff_t oendpos;
3523
3524 oend = OVERLAY_END (overlays[i]);
3525 oendpos = OVERLAY_POSITION (oend);
3526 endpos = min (endpos, oendpos);
3527 }
3528
3529 return endpos;
3530 }
3531
3532 /* How many characters forward to search for a display property or
3533 display string. Searching too far forward makes the bidi display
3534 sluggish, especially in small windows. */
3535 #define MAX_DISP_SCAN 250
3536
3537 /* Return the character position of a display string at or after
3538 position specified by POSITION. If no display string exists at or
3539 after POSITION, return ZV. A display string is either an overlay
3540 with `display' property whose value is a string, or a `display'
3541 text property whose value is a string. STRING is data about the
3542 string to iterate; if STRING->lstring is nil, we are iterating a
3543 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3544 on a GUI frame. DISP_PROP is set to zero if we searched
3545 MAX_DISP_SCAN characters forward without finding any display
3546 strings, non-zero otherwise. It is set to 2 if the display string
3547 uses any kind of `(space ...)' spec that will produce a stretch of
3548 white space in the text area. */
3549 ptrdiff_t
3550 compute_display_string_pos (struct text_pos *position,
3551 struct bidi_string_data *string,
3552 struct window *w,
3553 int frame_window_p, int *disp_prop)
3554 {
3555 /* OBJECT = nil means current buffer. */
3556 Lisp_Object object, object1;
3557 Lisp_Object pos, spec, limpos;
3558 int string_p = (string && (STRINGP (string->lstring) || string->s));
3559 ptrdiff_t eob = string_p ? string->schars : ZV;
3560 ptrdiff_t begb = string_p ? 0 : BEGV;
3561 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3562 ptrdiff_t lim =
3563 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3564 struct text_pos tpos;
3565 int rv = 0;
3566
3567 if (string && STRINGP (string->lstring))
3568 object1 = object = string->lstring;
3569 else if (w && !string_p)
3570 {
3571 XSETWINDOW (object, w);
3572 object1 = Qnil;
3573 }
3574 else
3575 object1 = object = Qnil;
3576
3577 *disp_prop = 1;
3578
3579 if (charpos >= eob
3580 /* We don't support display properties whose values are strings
3581 that have display string properties. */
3582 || string->from_disp_str
3583 /* C strings cannot have display properties. */
3584 || (string->s && !STRINGP (object)))
3585 {
3586 *disp_prop = 0;
3587 return eob;
3588 }
3589
3590 /* If the character at CHARPOS is where the display string begins,
3591 return CHARPOS. */
3592 pos = make_number (charpos);
3593 if (STRINGP (object))
3594 bufpos = string->bufpos;
3595 else
3596 bufpos = charpos;
3597 tpos = *position;
3598 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3599 && (charpos <= begb
3600 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3601 object),
3602 spec))
3603 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3604 frame_window_p)))
3605 {
3606 if (rv == 2)
3607 *disp_prop = 2;
3608 return charpos;
3609 }
3610
3611 /* Look forward for the first character with a `display' property
3612 that will replace the underlying text when displayed. */
3613 limpos = make_number (lim);
3614 do {
3615 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3616 CHARPOS (tpos) = XFASTINT (pos);
3617 if (CHARPOS (tpos) >= lim)
3618 {
3619 *disp_prop = 0;
3620 break;
3621 }
3622 if (STRINGP (object))
3623 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3624 else
3625 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3626 spec = Fget_char_property (pos, Qdisplay, object);
3627 if (!STRINGP (object))
3628 bufpos = CHARPOS (tpos);
3629 } while (NILP (spec)
3630 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3631 bufpos, frame_window_p)));
3632 if (rv == 2)
3633 *disp_prop = 2;
3634
3635 return CHARPOS (tpos);
3636 }
3637
3638 /* Return the character position of the end of the display string that
3639 started at CHARPOS. If there's no display string at CHARPOS,
3640 return -1. A display string is either an overlay with `display'
3641 property whose value is a string or a `display' text property whose
3642 value is a string. */
3643 ptrdiff_t
3644 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3645 {
3646 /* OBJECT = nil means current buffer. */
3647 Lisp_Object object =
3648 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3649 Lisp_Object pos = make_number (charpos);
3650 ptrdiff_t eob =
3651 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3652
3653 if (charpos >= eob || (string->s && !STRINGP (object)))
3654 return eob;
3655
3656 /* It could happen that the display property or overlay was removed
3657 since we found it in compute_display_string_pos above. One way
3658 this can happen is if JIT font-lock was called (through
3659 handle_fontified_prop), and jit-lock-functions remove text
3660 properties or overlays from the portion of buffer that includes
3661 CHARPOS. Muse mode is known to do that, for example. In this
3662 case, we return -1 to the caller, to signal that no display
3663 string is actually present at CHARPOS. See bidi_fetch_char for
3664 how this is handled.
3665
3666 An alternative would be to never look for display properties past
3667 it->stop_charpos. But neither compute_display_string_pos nor
3668 bidi_fetch_char that calls it know or care where the next
3669 stop_charpos is. */
3670 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3671 return -1;
3672
3673 /* Look forward for the first character where the `display' property
3674 changes. */
3675 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3676
3677 return XFASTINT (pos);
3678 }
3679
3680
3681 \f
3682 /***********************************************************************
3683 Fontification
3684 ***********************************************************************/
3685
3686 /* Handle changes in the `fontified' property of the current buffer by
3687 calling hook functions from Qfontification_functions to fontify
3688 regions of text. */
3689
3690 static enum prop_handled
3691 handle_fontified_prop (struct it *it)
3692 {
3693 Lisp_Object prop, pos;
3694 enum prop_handled handled = HANDLED_NORMALLY;
3695
3696 if (!NILP (Vmemory_full))
3697 return handled;
3698
3699 /* Get the value of the `fontified' property at IT's current buffer
3700 position. (The `fontified' property doesn't have a special
3701 meaning in strings.) If the value is nil, call functions from
3702 Qfontification_functions. */
3703 if (!STRINGP (it->string)
3704 && it->s == NULL
3705 && !NILP (Vfontification_functions)
3706 && !NILP (Vrun_hooks)
3707 && (pos = make_number (IT_CHARPOS (*it)),
3708 prop = Fget_char_property (pos, Qfontified, Qnil),
3709 /* Ignore the special cased nil value always present at EOB since
3710 no amount of fontifying will be able to change it. */
3711 NILP (prop) && IT_CHARPOS (*it) < Z))
3712 {
3713 ptrdiff_t count = SPECPDL_INDEX ();
3714 Lisp_Object val;
3715 struct buffer *obuf = current_buffer;
3716 ptrdiff_t begv = BEGV, zv = ZV;
3717 bool old_clip_changed = current_buffer->clip_changed;
3718
3719 val = Vfontification_functions;
3720 specbind (Qfontification_functions, Qnil);
3721
3722 eassert (it->end_charpos == ZV);
3723
3724 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3725 safe_call1 (val, pos);
3726 else
3727 {
3728 Lisp_Object fns, fn;
3729 struct gcpro gcpro1, gcpro2;
3730
3731 fns = Qnil;
3732 GCPRO2 (val, fns);
3733
3734 for (; CONSP (val); val = XCDR (val))
3735 {
3736 fn = XCAR (val);
3737
3738 if (EQ (fn, Qt))
3739 {
3740 /* A value of t indicates this hook has a local
3741 binding; it means to run the global binding too.
3742 In a global value, t should not occur. If it
3743 does, we must ignore it to avoid an endless
3744 loop. */
3745 for (fns = Fdefault_value (Qfontification_functions);
3746 CONSP (fns);
3747 fns = XCDR (fns))
3748 {
3749 fn = XCAR (fns);
3750 if (!EQ (fn, Qt))
3751 safe_call1 (fn, pos);
3752 }
3753 }
3754 else
3755 safe_call1 (fn, pos);
3756 }
3757
3758 UNGCPRO;
3759 }
3760
3761 unbind_to (count, Qnil);
3762
3763 /* Fontification functions routinely call `save-restriction'.
3764 Normally, this tags clip_changed, which can confuse redisplay
3765 (see discussion in Bug#6671). Since we don't perform any
3766 special handling of fontification changes in the case where
3767 `save-restriction' isn't called, there's no point doing so in
3768 this case either. So, if the buffer's restrictions are
3769 actually left unchanged, reset clip_changed. */
3770 if (obuf == current_buffer)
3771 {
3772 if (begv == BEGV && zv == ZV)
3773 current_buffer->clip_changed = old_clip_changed;
3774 }
3775 /* There isn't much we can reasonably do to protect against
3776 misbehaving fontification, but here's a fig leaf. */
3777 else if (BUFFER_LIVE_P (obuf))
3778 set_buffer_internal_1 (obuf);
3779
3780 /* The fontification code may have added/removed text.
3781 It could do even a lot worse, but let's at least protect against
3782 the most obvious case where only the text past `pos' gets changed',
3783 as is/was done in grep.el where some escapes sequences are turned
3784 into face properties (bug#7876). */
3785 it->end_charpos = ZV;
3786
3787 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3788 something. This avoids an endless loop if they failed to
3789 fontify the text for which reason ever. */
3790 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3791 handled = HANDLED_RECOMPUTE_PROPS;
3792 }
3793
3794 return handled;
3795 }
3796
3797
3798 \f
3799 /***********************************************************************
3800 Faces
3801 ***********************************************************************/
3802
3803 /* Set up iterator IT from face properties at its current position.
3804 Called from handle_stop. */
3805
3806 static enum prop_handled
3807 handle_face_prop (struct it *it)
3808 {
3809 int new_face_id;
3810 ptrdiff_t next_stop;
3811
3812 if (!STRINGP (it->string))
3813 {
3814 new_face_id
3815 = face_at_buffer_position (it->w,
3816 IT_CHARPOS (*it),
3817 &next_stop,
3818 (IT_CHARPOS (*it)
3819 + TEXT_PROP_DISTANCE_LIMIT),
3820 0, it->base_face_id);
3821
3822 /* Is this a start of a run of characters with box face?
3823 Caveat: this can be called for a freshly initialized
3824 iterator; face_id is -1 in this case. We know that the new
3825 face will not change until limit, i.e. if the new face has a
3826 box, all characters up to limit will have one. But, as
3827 usual, we don't know whether limit is really the end. */
3828 if (new_face_id != it->face_id)
3829 {
3830 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3831 /* If it->face_id is -1, old_face below will be NULL, see
3832 the definition of FACE_FROM_ID. This will happen if this
3833 is the initial call that gets the face. */
3834 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3835
3836 /* If the value of face_id of the iterator is -1, we have to
3837 look in front of IT's position and see whether there is a
3838 face there that's different from new_face_id. */
3839 if (!old_face && IT_CHARPOS (*it) > BEG)
3840 {
3841 int prev_face_id = face_before_it_pos (it);
3842
3843 old_face = FACE_FROM_ID (it->f, prev_face_id);
3844 }
3845
3846 /* If the new face has a box, but the old face does not,
3847 this is the start of a run of characters with box face,
3848 i.e. this character has a shadow on the left side. */
3849 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3850 && (old_face == NULL || !old_face->box));
3851 it->face_box_p = new_face->box != FACE_NO_BOX;
3852 }
3853 }
3854 else
3855 {
3856 int base_face_id;
3857 ptrdiff_t bufpos;
3858 int i;
3859 Lisp_Object from_overlay
3860 = (it->current.overlay_string_index >= 0
3861 ? it->string_overlays[it->current.overlay_string_index
3862 % OVERLAY_STRING_CHUNK_SIZE]
3863 : Qnil);
3864
3865 /* See if we got to this string directly or indirectly from
3866 an overlay property. That includes the before-string or
3867 after-string of an overlay, strings in display properties
3868 provided by an overlay, their text properties, etc.
3869
3870 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3871 if (! NILP (from_overlay))
3872 for (i = it->sp - 1; i >= 0; i--)
3873 {
3874 if (it->stack[i].current.overlay_string_index >= 0)
3875 from_overlay
3876 = it->string_overlays[it->stack[i].current.overlay_string_index
3877 % OVERLAY_STRING_CHUNK_SIZE];
3878 else if (! NILP (it->stack[i].from_overlay))
3879 from_overlay = it->stack[i].from_overlay;
3880
3881 if (!NILP (from_overlay))
3882 break;
3883 }
3884
3885 if (! NILP (from_overlay))
3886 {
3887 bufpos = IT_CHARPOS (*it);
3888 /* For a string from an overlay, the base face depends
3889 only on text properties and ignores overlays. */
3890 base_face_id
3891 = face_for_overlay_string (it->w,
3892 IT_CHARPOS (*it),
3893 &next_stop,
3894 (IT_CHARPOS (*it)
3895 + TEXT_PROP_DISTANCE_LIMIT),
3896 0,
3897 from_overlay);
3898 }
3899 else
3900 {
3901 bufpos = 0;
3902
3903 /* For strings from a `display' property, use the face at
3904 IT's current buffer position as the base face to merge
3905 with, so that overlay strings appear in the same face as
3906 surrounding text, unless they specify their own faces.
3907 For strings from wrap-prefix and line-prefix properties,
3908 use the default face, possibly remapped via
3909 Vface_remapping_alist. */
3910 base_face_id = it->string_from_prefix_prop_p
3911 ? (!NILP (Vface_remapping_alist)
3912 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3913 : DEFAULT_FACE_ID)
3914 : underlying_face_id (it);
3915 }
3916
3917 new_face_id = face_at_string_position (it->w,
3918 it->string,
3919 IT_STRING_CHARPOS (*it),
3920 bufpos,
3921 &next_stop,
3922 base_face_id, 0);
3923
3924 /* Is this a start of a run of characters with box? Caveat:
3925 this can be called for a freshly allocated iterator; face_id
3926 is -1 is this case. We know that the new face will not
3927 change until the next check pos, i.e. if the new face has a
3928 box, all characters up to that position will have a
3929 box. But, as usual, we don't know whether that position
3930 is really the end. */
3931 if (new_face_id != it->face_id)
3932 {
3933 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3934 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3935
3936 /* If new face has a box but old face hasn't, this is the
3937 start of a run of characters with box, i.e. it has a
3938 shadow on the left side. */
3939 it->start_of_box_run_p
3940 = new_face->box && (old_face == NULL || !old_face->box);
3941 it->face_box_p = new_face->box != FACE_NO_BOX;
3942 }
3943 }
3944
3945 it->face_id = new_face_id;
3946 return HANDLED_NORMALLY;
3947 }
3948
3949
3950 /* Return the ID of the face ``underlying'' IT's current position,
3951 which is in a string. If the iterator is associated with a
3952 buffer, return the face at IT's current buffer position.
3953 Otherwise, use the iterator's base_face_id. */
3954
3955 static int
3956 underlying_face_id (struct it *it)
3957 {
3958 int face_id = it->base_face_id, i;
3959
3960 eassert (STRINGP (it->string));
3961
3962 for (i = it->sp - 1; i >= 0; --i)
3963 if (NILP (it->stack[i].string))
3964 face_id = it->stack[i].face_id;
3965
3966 return face_id;
3967 }
3968
3969
3970 /* Compute the face one character before or after the current position
3971 of IT, in the visual order. BEFORE_P non-zero means get the face
3972 in front (to the left in L2R paragraphs, to the right in R2L
3973 paragraphs) of IT's screen position. Value is the ID of the face. */
3974
3975 static int
3976 face_before_or_after_it_pos (struct it *it, int before_p)
3977 {
3978 int face_id, limit;
3979 ptrdiff_t next_check_charpos;
3980 struct it it_copy;
3981 void *it_copy_data = NULL;
3982
3983 eassert (it->s == NULL);
3984
3985 if (STRINGP (it->string))
3986 {
3987 ptrdiff_t bufpos, charpos;
3988 int base_face_id;
3989
3990 /* No face change past the end of the string (for the case
3991 we are padding with spaces). No face change before the
3992 string start. */
3993 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3994 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3995 return it->face_id;
3996
3997 if (!it->bidi_p)
3998 {
3999 /* Set charpos to the position before or after IT's current
4000 position, in the logical order, which in the non-bidi
4001 case is the same as the visual order. */
4002 if (before_p)
4003 charpos = IT_STRING_CHARPOS (*it) - 1;
4004 else if (it->what == IT_COMPOSITION)
4005 /* For composition, we must check the character after the
4006 composition. */
4007 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4008 else
4009 charpos = IT_STRING_CHARPOS (*it) + 1;
4010 }
4011 else
4012 {
4013 if (before_p)
4014 {
4015 /* With bidi iteration, the character before the current
4016 in the visual order cannot be found by simple
4017 iteration, because "reverse" reordering is not
4018 supported. Instead, we need to use the move_it_*
4019 family of functions. */
4020 /* Ignore face changes before the first visible
4021 character on this display line. */
4022 if (it->current_x <= it->first_visible_x)
4023 return it->face_id;
4024 SAVE_IT (it_copy, *it, it_copy_data);
4025 /* Implementation note: Since move_it_in_display_line
4026 works in the iterator geometry, and thinks the first
4027 character is always the leftmost, even in R2L lines,
4028 we don't need to distinguish between the R2L and L2R
4029 cases here. */
4030 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4031 it_copy.current_x - 1, MOVE_TO_X);
4032 charpos = IT_STRING_CHARPOS (it_copy);
4033 RESTORE_IT (it, it, it_copy_data);
4034 }
4035 else
4036 {
4037 /* Set charpos to the string position of the character
4038 that comes after IT's current position in the visual
4039 order. */
4040 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4041
4042 it_copy = *it;
4043 while (n--)
4044 bidi_move_to_visually_next (&it_copy.bidi_it);
4045
4046 charpos = it_copy.bidi_it.charpos;
4047 }
4048 }
4049 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4050
4051 if (it->current.overlay_string_index >= 0)
4052 bufpos = IT_CHARPOS (*it);
4053 else
4054 bufpos = 0;
4055
4056 base_face_id = underlying_face_id (it);
4057
4058 /* Get the face for ASCII, or unibyte. */
4059 face_id = face_at_string_position (it->w,
4060 it->string,
4061 charpos,
4062 bufpos,
4063 &next_check_charpos,
4064 base_face_id, 0);
4065
4066 /* Correct the face for charsets different from ASCII. Do it
4067 for the multibyte case only. The face returned above is
4068 suitable for unibyte text if IT->string is unibyte. */
4069 if (STRING_MULTIBYTE (it->string))
4070 {
4071 struct text_pos pos1 = string_pos (charpos, it->string);
4072 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4073 int c, len;
4074 struct face *face = FACE_FROM_ID (it->f, face_id);
4075
4076 c = string_char_and_length (p, &len);
4077 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4078 }
4079 }
4080 else
4081 {
4082 struct text_pos pos;
4083
4084 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4085 || (IT_CHARPOS (*it) <= BEGV && before_p))
4086 return it->face_id;
4087
4088 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4089 pos = it->current.pos;
4090
4091 if (!it->bidi_p)
4092 {
4093 if (before_p)
4094 DEC_TEXT_POS (pos, it->multibyte_p);
4095 else
4096 {
4097 if (it->what == IT_COMPOSITION)
4098 {
4099 /* For composition, we must check the position after
4100 the composition. */
4101 pos.charpos += it->cmp_it.nchars;
4102 pos.bytepos += it->len;
4103 }
4104 else
4105 INC_TEXT_POS (pos, it->multibyte_p);
4106 }
4107 }
4108 else
4109 {
4110 if (before_p)
4111 {
4112 /* With bidi iteration, the character before the current
4113 in the visual order cannot be found by simple
4114 iteration, because "reverse" reordering is not
4115 supported. Instead, we need to use the move_it_*
4116 family of functions. */
4117 /* Ignore face changes before the first visible
4118 character on this display line. */
4119 if (it->current_x <= it->first_visible_x)
4120 return it->face_id;
4121 SAVE_IT (it_copy, *it, it_copy_data);
4122 /* Implementation note: Since move_it_in_display_line
4123 works in the iterator geometry, and thinks the first
4124 character is always the leftmost, even in R2L lines,
4125 we don't need to distinguish between the R2L and L2R
4126 cases here. */
4127 move_it_in_display_line (&it_copy, ZV,
4128 it_copy.current_x - 1, MOVE_TO_X);
4129 pos = it_copy.current.pos;
4130 RESTORE_IT (it, it, it_copy_data);
4131 }
4132 else
4133 {
4134 /* Set charpos to the buffer position of the character
4135 that comes after IT's current position in the visual
4136 order. */
4137 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4138
4139 it_copy = *it;
4140 while (n--)
4141 bidi_move_to_visually_next (&it_copy.bidi_it);
4142
4143 SET_TEXT_POS (pos,
4144 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4145 }
4146 }
4147 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4148
4149 /* Determine face for CHARSET_ASCII, or unibyte. */
4150 face_id = face_at_buffer_position (it->w,
4151 CHARPOS (pos),
4152 &next_check_charpos,
4153 limit, 0, -1);
4154
4155 /* Correct the face for charsets different from ASCII. Do it
4156 for the multibyte case only. The face returned above is
4157 suitable for unibyte text if current_buffer is unibyte. */
4158 if (it->multibyte_p)
4159 {
4160 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4161 struct face *face = FACE_FROM_ID (it->f, face_id);
4162 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4163 }
4164 }
4165
4166 return face_id;
4167 }
4168
4169
4170 \f
4171 /***********************************************************************
4172 Invisible text
4173 ***********************************************************************/
4174
4175 /* Set up iterator IT from invisible properties at its current
4176 position. Called from handle_stop. */
4177
4178 static enum prop_handled
4179 handle_invisible_prop (struct it *it)
4180 {
4181 enum prop_handled handled = HANDLED_NORMALLY;
4182 int invis_p;
4183 Lisp_Object prop;
4184
4185 if (STRINGP (it->string))
4186 {
4187 Lisp_Object end_charpos, limit, charpos;
4188
4189 /* Get the value of the invisible text property at the
4190 current position. Value will be nil if there is no such
4191 property. */
4192 charpos = make_number (IT_STRING_CHARPOS (*it));
4193 prop = Fget_text_property (charpos, Qinvisible, it->string);
4194 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4195
4196 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4197 {
4198 /* Record whether we have to display an ellipsis for the
4199 invisible text. */
4200 int display_ellipsis_p = (invis_p == 2);
4201 ptrdiff_t len, endpos;
4202
4203 handled = HANDLED_RECOMPUTE_PROPS;
4204
4205 /* Get the position at which the next visible text can be
4206 found in IT->string, if any. */
4207 endpos = len = SCHARS (it->string);
4208 XSETINT (limit, len);
4209 do
4210 {
4211 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4212 it->string, limit);
4213 if (INTEGERP (end_charpos))
4214 {
4215 endpos = XFASTINT (end_charpos);
4216 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4218 if (invis_p == 2)
4219 display_ellipsis_p = true;
4220 }
4221 }
4222 while (invis_p && endpos < len);
4223
4224 if (display_ellipsis_p)
4225 it->ellipsis_p = true;
4226
4227 if (endpos < len)
4228 {
4229 /* Text at END_CHARPOS is visible. Move IT there. */
4230 struct text_pos old;
4231 ptrdiff_t oldpos;
4232
4233 old = it->current.string_pos;
4234 oldpos = CHARPOS (old);
4235 if (it->bidi_p)
4236 {
4237 if (it->bidi_it.first_elt
4238 && it->bidi_it.charpos < SCHARS (it->string))
4239 bidi_paragraph_init (it->paragraph_embedding,
4240 &it->bidi_it, 1);
4241 /* Bidi-iterate out of the invisible text. */
4242 do
4243 {
4244 bidi_move_to_visually_next (&it->bidi_it);
4245 }
4246 while (oldpos <= it->bidi_it.charpos
4247 && it->bidi_it.charpos < endpos);
4248
4249 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4251 if (IT_CHARPOS (*it) >= endpos)
4252 it->prev_stop = endpos;
4253 }
4254 else
4255 {
4256 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4257 compute_string_pos (&it->current.string_pos, old, it->string);
4258 }
4259 }
4260 else
4261 {
4262 /* The rest of the string is invisible. If this is an
4263 overlay string, proceed with the next overlay string
4264 or whatever comes and return a character from there. */
4265 if (it->current.overlay_string_index >= 0
4266 && !display_ellipsis_p)
4267 {
4268 next_overlay_string (it);
4269 /* Don't check for overlay strings when we just
4270 finished processing them. */
4271 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4272 }
4273 else
4274 {
4275 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4276 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4277 }
4278 }
4279 }
4280 }
4281 else
4282 {
4283 ptrdiff_t newpos, next_stop, start_charpos, tem;
4284 Lisp_Object pos, overlay;
4285
4286 /* First of all, is there invisible text at this position? */
4287 tem = start_charpos = IT_CHARPOS (*it);
4288 pos = make_number (tem);
4289 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4290 &overlay);
4291 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4292
4293 /* If we are on invisible text, skip over it. */
4294 if (invis_p && start_charpos < it->end_charpos)
4295 {
4296 /* Record whether we have to display an ellipsis for the
4297 invisible text. */
4298 int display_ellipsis_p = invis_p == 2;
4299
4300 handled = HANDLED_RECOMPUTE_PROPS;
4301
4302 /* Loop skipping over invisible text. The loop is left at
4303 ZV or with IT on the first char being visible again. */
4304 do
4305 {
4306 /* Try to skip some invisible text. Return value is the
4307 position reached which can be equal to where we start
4308 if there is nothing invisible there. This skips both
4309 over invisible text properties and overlays with
4310 invisible property. */
4311 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4312
4313 /* If we skipped nothing at all we weren't at invisible
4314 text in the first place. If everything to the end of
4315 the buffer was skipped, end the loop. */
4316 if (newpos == tem || newpos >= ZV)
4317 invis_p = 0;
4318 else
4319 {
4320 /* We skipped some characters but not necessarily
4321 all there are. Check if we ended up on visible
4322 text. Fget_char_property returns the property of
4323 the char before the given position, i.e. if we
4324 get invis_p = 0, this means that the char at
4325 newpos is visible. */
4326 pos = make_number (newpos);
4327 prop = Fget_char_property (pos, Qinvisible, it->window);
4328 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4329 }
4330
4331 /* If we ended up on invisible text, proceed to
4332 skip starting with next_stop. */
4333 if (invis_p)
4334 tem = next_stop;
4335
4336 /* If there are adjacent invisible texts, don't lose the
4337 second one's ellipsis. */
4338 if (invis_p == 2)
4339 display_ellipsis_p = true;
4340 }
4341 while (invis_p);
4342
4343 /* The position newpos is now either ZV or on visible text. */
4344 if (it->bidi_p)
4345 {
4346 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4347 int on_newline
4348 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4349 int after_newline
4350 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4351
4352 /* If the invisible text ends on a newline or on a
4353 character after a newline, we can avoid the costly,
4354 character by character, bidi iteration to NEWPOS, and
4355 instead simply reseat the iterator there. That's
4356 because all bidi reordering information is tossed at
4357 the newline. This is a big win for modes that hide
4358 complete lines, like Outline, Org, etc. */
4359 if (on_newline || after_newline)
4360 {
4361 struct text_pos tpos;
4362 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4363
4364 SET_TEXT_POS (tpos, newpos, bpos);
4365 reseat_1 (it, tpos, 0);
4366 /* If we reseat on a newline/ZV, we need to prep the
4367 bidi iterator for advancing to the next character
4368 after the newline/EOB, keeping the current paragraph
4369 direction (so that PRODUCE_GLYPHS does TRT wrt
4370 prepending/appending glyphs to a glyph row). */
4371 if (on_newline)
4372 {
4373 it->bidi_it.first_elt = 0;
4374 it->bidi_it.paragraph_dir = pdir;
4375 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4376 it->bidi_it.nchars = 1;
4377 it->bidi_it.ch_len = 1;
4378 }
4379 }
4380 else /* Must use the slow method. */
4381 {
4382 /* With bidi iteration, the region of invisible text
4383 could start and/or end in the middle of a
4384 non-base embedding level. Therefore, we need to
4385 skip invisible text using the bidi iterator,
4386 starting at IT's current position, until we find
4387 ourselves outside of the invisible text.
4388 Skipping invisible text _after_ bidi iteration
4389 avoids affecting the visual order of the
4390 displayed text when invisible properties are
4391 added or removed. */
4392 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4393 {
4394 /* If we were `reseat'ed to a new paragraph,
4395 determine the paragraph base direction. We
4396 need to do it now because
4397 next_element_from_buffer may not have a
4398 chance to do it, if we are going to skip any
4399 text at the beginning, which resets the
4400 FIRST_ELT flag. */
4401 bidi_paragraph_init (it->paragraph_embedding,
4402 &it->bidi_it, 1);
4403 }
4404 do
4405 {
4406 bidi_move_to_visually_next (&it->bidi_it);
4407 }
4408 while (it->stop_charpos <= it->bidi_it.charpos
4409 && it->bidi_it.charpos < newpos);
4410 IT_CHARPOS (*it) = it->bidi_it.charpos;
4411 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4412 /* If we overstepped NEWPOS, record its position in
4413 the iterator, so that we skip invisible text if
4414 later the bidi iteration lands us in the
4415 invisible region again. */
4416 if (IT_CHARPOS (*it) >= newpos)
4417 it->prev_stop = newpos;
4418 }
4419 }
4420 else
4421 {
4422 IT_CHARPOS (*it) = newpos;
4423 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4424 }
4425
4426 /* If there are before-strings at the start of invisible
4427 text, and the text is invisible because of a text
4428 property, arrange to show before-strings because 20.x did
4429 it that way. (If the text is invisible because of an
4430 overlay property instead of a text property, this is
4431 already handled in the overlay code.) */
4432 if (NILP (overlay)
4433 && get_overlay_strings (it, it->stop_charpos))
4434 {
4435 handled = HANDLED_RECOMPUTE_PROPS;
4436 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4437 }
4438 else if (display_ellipsis_p)
4439 {
4440 /* Make sure that the glyphs of the ellipsis will get
4441 correct `charpos' values. If we would not update
4442 it->position here, the glyphs would belong to the
4443 last visible character _before_ the invisible
4444 text, which confuses `set_cursor_from_row'.
4445
4446 We use the last invisible position instead of the
4447 first because this way the cursor is always drawn on
4448 the first "." of the ellipsis, whenever PT is inside
4449 the invisible text. Otherwise the cursor would be
4450 placed _after_ the ellipsis when the point is after the
4451 first invisible character. */
4452 if (!STRINGP (it->object))
4453 {
4454 it->position.charpos = newpos - 1;
4455 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4456 }
4457 it->ellipsis_p = true;
4458 /* Let the ellipsis display before
4459 considering any properties of the following char.
4460 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4461 handled = HANDLED_RETURN;
4462 }
4463 }
4464 }
4465
4466 return handled;
4467 }
4468
4469
4470 /* Make iterator IT return `...' next.
4471 Replaces LEN characters from buffer. */
4472
4473 static void
4474 setup_for_ellipsis (struct it *it, int len)
4475 {
4476 /* Use the display table definition for `...'. Invalid glyphs
4477 will be handled by the method returning elements from dpvec. */
4478 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4479 {
4480 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4481 it->dpvec = v->contents;
4482 it->dpend = v->contents + v->header.size;
4483 }
4484 else
4485 {
4486 /* Default `...'. */
4487 it->dpvec = default_invis_vector;
4488 it->dpend = default_invis_vector + 3;
4489 }
4490
4491 it->dpvec_char_len = len;
4492 it->current.dpvec_index = 0;
4493 it->dpvec_face_id = -1;
4494
4495 /* Remember the current face id in case glyphs specify faces.
4496 IT's face is restored in set_iterator_to_next.
4497 saved_face_id was set to preceding char's face in handle_stop. */
4498 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4499 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4500
4501 it->method = GET_FROM_DISPLAY_VECTOR;
4502 it->ellipsis_p = true;
4503 }
4504
4505
4506 \f
4507 /***********************************************************************
4508 'display' property
4509 ***********************************************************************/
4510
4511 /* Set up iterator IT from `display' property at its current position.
4512 Called from handle_stop.
4513 We return HANDLED_RETURN if some part of the display property
4514 overrides the display of the buffer text itself.
4515 Otherwise we return HANDLED_NORMALLY. */
4516
4517 static enum prop_handled
4518 handle_display_prop (struct it *it)
4519 {
4520 Lisp_Object propval, object, overlay;
4521 struct text_pos *position;
4522 ptrdiff_t bufpos;
4523 /* Nonzero if some property replaces the display of the text itself. */
4524 int display_replaced_p = 0;
4525
4526 if (STRINGP (it->string))
4527 {
4528 object = it->string;
4529 position = &it->current.string_pos;
4530 bufpos = CHARPOS (it->current.pos);
4531 }
4532 else
4533 {
4534 XSETWINDOW (object, it->w);
4535 position = &it->current.pos;
4536 bufpos = CHARPOS (*position);
4537 }
4538
4539 /* Reset those iterator values set from display property values. */
4540 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4541 it->space_width = Qnil;
4542 it->font_height = Qnil;
4543 it->voffset = 0;
4544
4545 /* We don't support recursive `display' properties, i.e. string
4546 values that have a string `display' property, that have a string
4547 `display' property etc. */
4548 if (!it->string_from_display_prop_p)
4549 it->area = TEXT_AREA;
4550
4551 propval = get_char_property_and_overlay (make_number (position->charpos),
4552 Qdisplay, object, &overlay);
4553 if (NILP (propval))
4554 return HANDLED_NORMALLY;
4555 /* Now OVERLAY is the overlay that gave us this property, or nil
4556 if it was a text property. */
4557
4558 if (!STRINGP (it->string))
4559 object = it->w->contents;
4560
4561 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4562 position, bufpos,
4563 FRAME_WINDOW_P (it->f));
4564
4565 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4566 }
4567
4568 /* Subroutine of handle_display_prop. Returns non-zero if the display
4569 specification in SPEC is a replacing specification, i.e. it would
4570 replace the text covered by `display' property with something else,
4571 such as an image or a display string. If SPEC includes any kind or
4572 `(space ...) specification, the value is 2; this is used by
4573 compute_display_string_pos, which see.
4574
4575 See handle_single_display_spec for documentation of arguments.
4576 frame_window_p is non-zero if the window being redisplayed is on a
4577 GUI frame; this argument is used only if IT is NULL, see below.
4578
4579 IT can be NULL, if this is called by the bidi reordering code
4580 through compute_display_string_pos, which see. In that case, this
4581 function only examines SPEC, but does not otherwise "handle" it, in
4582 the sense that it doesn't set up members of IT from the display
4583 spec. */
4584 static int
4585 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4586 Lisp_Object overlay, struct text_pos *position,
4587 ptrdiff_t bufpos, int frame_window_p)
4588 {
4589 int replacing_p = 0;
4590 int rv;
4591
4592 if (CONSP (spec)
4593 /* Simple specifications. */
4594 && !EQ (XCAR (spec), Qimage)
4595 && !EQ (XCAR (spec), Qspace)
4596 && !EQ (XCAR (spec), Qwhen)
4597 && !EQ (XCAR (spec), Qslice)
4598 && !EQ (XCAR (spec), Qspace_width)
4599 && !EQ (XCAR (spec), Qheight)
4600 && !EQ (XCAR (spec), Qraise)
4601 /* Marginal area specifications. */
4602 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4603 && !EQ (XCAR (spec), Qleft_fringe)
4604 && !EQ (XCAR (spec), Qright_fringe)
4605 && !NILP (XCAR (spec)))
4606 {
4607 for (; CONSP (spec); spec = XCDR (spec))
4608 {
4609 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4610 overlay, position, bufpos,
4611 replacing_p, frame_window_p)))
4612 {
4613 replacing_p = rv;
4614 /* If some text in a string is replaced, `position' no
4615 longer points to the position of `object'. */
4616 if (!it || STRINGP (object))
4617 break;
4618 }
4619 }
4620 }
4621 else if (VECTORP (spec))
4622 {
4623 ptrdiff_t i;
4624 for (i = 0; i < ASIZE (spec); ++i)
4625 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4626 overlay, position, bufpos,
4627 replacing_p, frame_window_p)))
4628 {
4629 replacing_p = rv;
4630 /* If some text in a string is replaced, `position' no
4631 longer points to the position of `object'. */
4632 if (!it || STRINGP (object))
4633 break;
4634 }
4635 }
4636 else
4637 {
4638 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4639 position, bufpos, 0,
4640 frame_window_p)))
4641 replacing_p = rv;
4642 }
4643
4644 return replacing_p;
4645 }
4646
4647 /* Value is the position of the end of the `display' property starting
4648 at START_POS in OBJECT. */
4649
4650 static struct text_pos
4651 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4652 {
4653 Lisp_Object end;
4654 struct text_pos end_pos;
4655
4656 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4657 Qdisplay, object, Qnil);
4658 CHARPOS (end_pos) = XFASTINT (end);
4659 if (STRINGP (object))
4660 compute_string_pos (&end_pos, start_pos, it->string);
4661 else
4662 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4663
4664 return end_pos;
4665 }
4666
4667
4668 /* Set up IT from a single `display' property specification SPEC. OBJECT
4669 is the object in which the `display' property was found. *POSITION
4670 is the position in OBJECT at which the `display' property was found.
4671 BUFPOS is the buffer position of OBJECT (different from POSITION if
4672 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4673 previously saw a display specification which already replaced text
4674 display with something else, for example an image; we ignore such
4675 properties after the first one has been processed.
4676
4677 OVERLAY is the overlay this `display' property came from,
4678 or nil if it was a text property.
4679
4680 If SPEC is a `space' or `image' specification, and in some other
4681 cases too, set *POSITION to the position where the `display'
4682 property ends.
4683
4684 If IT is NULL, only examine the property specification in SPEC, but
4685 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4686 is intended to be displayed in a window on a GUI frame.
4687
4688 Value is non-zero if something was found which replaces the display
4689 of buffer or string text. */
4690
4691 static int
4692 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4693 Lisp_Object overlay, struct text_pos *position,
4694 ptrdiff_t bufpos, int display_replaced_p,
4695 int frame_window_p)
4696 {
4697 Lisp_Object form;
4698 Lisp_Object location, value;
4699 struct text_pos start_pos = *position;
4700 int valid_p;
4701
4702 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4703 If the result is non-nil, use VALUE instead of SPEC. */
4704 form = Qt;
4705 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4706 {
4707 spec = XCDR (spec);
4708 if (!CONSP (spec))
4709 return 0;
4710 form = XCAR (spec);
4711 spec = XCDR (spec);
4712 }
4713
4714 if (!NILP (form) && !EQ (form, Qt))
4715 {
4716 ptrdiff_t count = SPECPDL_INDEX ();
4717 struct gcpro gcpro1;
4718
4719 /* Bind `object' to the object having the `display' property, a
4720 buffer or string. Bind `position' to the position in the
4721 object where the property was found, and `buffer-position'
4722 to the current position in the buffer. */
4723
4724 if (NILP (object))
4725 XSETBUFFER (object, current_buffer);
4726 specbind (Qobject, object);
4727 specbind (Qposition, make_number (CHARPOS (*position)));
4728 specbind (Qbuffer_position, make_number (bufpos));
4729 GCPRO1 (form);
4730 form = safe_eval (form);
4731 UNGCPRO;
4732 unbind_to (count, Qnil);
4733 }
4734
4735 if (NILP (form))
4736 return 0;
4737
4738 /* Handle `(height HEIGHT)' specifications. */
4739 if (CONSP (spec)
4740 && EQ (XCAR (spec), Qheight)
4741 && CONSP (XCDR (spec)))
4742 {
4743 if (it)
4744 {
4745 if (!FRAME_WINDOW_P (it->f))
4746 return 0;
4747
4748 it->font_height = XCAR (XCDR (spec));
4749 if (!NILP (it->font_height))
4750 {
4751 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4752 int new_height = -1;
4753
4754 if (CONSP (it->font_height)
4755 && (EQ (XCAR (it->font_height), Qplus)
4756 || EQ (XCAR (it->font_height), Qminus))
4757 && CONSP (XCDR (it->font_height))
4758 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4759 {
4760 /* `(+ N)' or `(- N)' where N is an integer. */
4761 int steps = XINT (XCAR (XCDR (it->font_height)));
4762 if (EQ (XCAR (it->font_height), Qplus))
4763 steps = - steps;
4764 it->face_id = smaller_face (it->f, it->face_id, steps);
4765 }
4766 else if (FUNCTIONP (it->font_height))
4767 {
4768 /* Call function with current height as argument.
4769 Value is the new height. */
4770 Lisp_Object height;
4771 height = safe_call1 (it->font_height,
4772 face->lface[LFACE_HEIGHT_INDEX]);
4773 if (NUMBERP (height))
4774 new_height = XFLOATINT (height);
4775 }
4776 else if (NUMBERP (it->font_height))
4777 {
4778 /* Value is a multiple of the canonical char height. */
4779 struct face *f;
4780
4781 f = FACE_FROM_ID (it->f,
4782 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4783 new_height = (XFLOATINT (it->font_height)
4784 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4785 }
4786 else
4787 {
4788 /* Evaluate IT->font_height with `height' bound to the
4789 current specified height to get the new height. */
4790 ptrdiff_t count = SPECPDL_INDEX ();
4791
4792 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4793 value = safe_eval (it->font_height);
4794 unbind_to (count, Qnil);
4795
4796 if (NUMBERP (value))
4797 new_height = XFLOATINT (value);
4798 }
4799
4800 if (new_height > 0)
4801 it->face_id = face_with_height (it->f, it->face_id, new_height);
4802 }
4803 }
4804
4805 return 0;
4806 }
4807
4808 /* Handle `(space-width WIDTH)'. */
4809 if (CONSP (spec)
4810 && EQ (XCAR (spec), Qspace_width)
4811 && CONSP (XCDR (spec)))
4812 {
4813 if (it)
4814 {
4815 if (!FRAME_WINDOW_P (it->f))
4816 return 0;
4817
4818 value = XCAR (XCDR (spec));
4819 if (NUMBERP (value) && XFLOATINT (value) > 0)
4820 it->space_width = value;
4821 }
4822
4823 return 0;
4824 }
4825
4826 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4827 if (CONSP (spec)
4828 && EQ (XCAR (spec), Qslice))
4829 {
4830 Lisp_Object tem;
4831
4832 if (it)
4833 {
4834 if (!FRAME_WINDOW_P (it->f))
4835 return 0;
4836
4837 if (tem = XCDR (spec), CONSP (tem))
4838 {
4839 it->slice.x = XCAR (tem);
4840 if (tem = XCDR (tem), CONSP (tem))
4841 {
4842 it->slice.y = XCAR (tem);
4843 if (tem = XCDR (tem), CONSP (tem))
4844 {
4845 it->slice.width = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4847 it->slice.height = XCAR (tem);
4848 }
4849 }
4850 }
4851 }
4852
4853 return 0;
4854 }
4855
4856 /* Handle `(raise FACTOR)'. */
4857 if (CONSP (spec)
4858 && EQ (XCAR (spec), Qraise)
4859 && CONSP (XCDR (spec)))
4860 {
4861 if (it)
4862 {
4863 if (!FRAME_WINDOW_P (it->f))
4864 return 0;
4865
4866 #ifdef HAVE_WINDOW_SYSTEM
4867 value = XCAR (XCDR (spec));
4868 if (NUMBERP (value))
4869 {
4870 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4871 it->voffset = - (XFLOATINT (value)
4872 * (FONT_HEIGHT (face->font)));
4873 }
4874 #endif /* HAVE_WINDOW_SYSTEM */
4875 }
4876
4877 return 0;
4878 }
4879
4880 /* Don't handle the other kinds of display specifications
4881 inside a string that we got from a `display' property. */
4882 if (it && it->string_from_display_prop_p)
4883 return 0;
4884
4885 /* Characters having this form of property are not displayed, so
4886 we have to find the end of the property. */
4887 if (it)
4888 {
4889 start_pos = *position;
4890 *position = display_prop_end (it, object, start_pos);
4891 }
4892 value = Qnil;
4893
4894 /* Stop the scan at that end position--we assume that all
4895 text properties change there. */
4896 if (it)
4897 it->stop_charpos = position->charpos;
4898
4899 /* Handle `(left-fringe BITMAP [FACE])'
4900 and `(right-fringe BITMAP [FACE])'. */
4901 if (CONSP (spec)
4902 && (EQ (XCAR (spec), Qleft_fringe)
4903 || EQ (XCAR (spec), Qright_fringe))
4904 && CONSP (XCDR (spec)))
4905 {
4906 int fringe_bitmap;
4907
4908 if (it)
4909 {
4910 if (!FRAME_WINDOW_P (it->f))
4911 /* If we return here, POSITION has been advanced
4912 across the text with this property. */
4913 {
4914 /* Synchronize the bidi iterator with POSITION. This is
4915 needed because we are not going to push the iterator
4916 on behalf of this display property, so there will be
4917 no pop_it call to do this synchronization for us. */
4918 if (it->bidi_p)
4919 {
4920 it->position = *position;
4921 iterate_out_of_display_property (it);
4922 *position = it->position;
4923 }
4924 return 1;
4925 }
4926 }
4927 else if (!frame_window_p)
4928 return 1;
4929
4930 #ifdef HAVE_WINDOW_SYSTEM
4931 value = XCAR (XCDR (spec));
4932 if (!SYMBOLP (value)
4933 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4934 /* If we return here, POSITION has been advanced
4935 across the text with this property. */
4936 {
4937 if (it && it->bidi_p)
4938 {
4939 it->position = *position;
4940 iterate_out_of_display_property (it);
4941 *position = it->position;
4942 }
4943 return 1;
4944 }
4945
4946 if (it)
4947 {
4948 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4949
4950 if (CONSP (XCDR (XCDR (spec))))
4951 {
4952 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4953 int face_id2 = lookup_derived_face (it->f, face_name,
4954 FRINGE_FACE_ID, 0);
4955 if (face_id2 >= 0)
4956 face_id = face_id2;
4957 }
4958
4959 /* Save current settings of IT so that we can restore them
4960 when we are finished with the glyph property value. */
4961 push_it (it, position);
4962
4963 it->area = TEXT_AREA;
4964 it->what = IT_IMAGE;
4965 it->image_id = -1; /* no image */
4966 it->position = start_pos;
4967 it->object = NILP (object) ? it->w->contents : object;
4968 it->method = GET_FROM_IMAGE;
4969 it->from_overlay = Qnil;
4970 it->face_id = face_id;
4971 it->from_disp_prop_p = true;
4972
4973 /* Say that we haven't consumed the characters with
4974 `display' property yet. The call to pop_it in
4975 set_iterator_to_next will clean this up. */
4976 *position = start_pos;
4977
4978 if (EQ (XCAR (spec), Qleft_fringe))
4979 {
4980 it->left_user_fringe_bitmap = fringe_bitmap;
4981 it->left_user_fringe_face_id = face_id;
4982 }
4983 else
4984 {
4985 it->right_user_fringe_bitmap = fringe_bitmap;
4986 it->right_user_fringe_face_id = face_id;
4987 }
4988 }
4989 #endif /* HAVE_WINDOW_SYSTEM */
4990 return 1;
4991 }
4992
4993 /* Prepare to handle `((margin left-margin) ...)',
4994 `((margin right-margin) ...)' and `((margin nil) ...)'
4995 prefixes for display specifications. */
4996 location = Qunbound;
4997 if (CONSP (spec) && CONSP (XCAR (spec)))
4998 {
4999 Lisp_Object tem;
5000
5001 value = XCDR (spec);
5002 if (CONSP (value))
5003 value = XCAR (value);
5004
5005 tem = XCAR (spec);
5006 if (EQ (XCAR (tem), Qmargin)
5007 && (tem = XCDR (tem),
5008 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5009 (NILP (tem)
5010 || EQ (tem, Qleft_margin)
5011 || EQ (tem, Qright_margin))))
5012 location = tem;
5013 }
5014
5015 if (EQ (location, Qunbound))
5016 {
5017 location = Qnil;
5018 value = spec;
5019 }
5020
5021 /* After this point, VALUE is the property after any
5022 margin prefix has been stripped. It must be a string,
5023 an image specification, or `(space ...)'.
5024
5025 LOCATION specifies where to display: `left-margin',
5026 `right-margin' or nil. */
5027
5028 valid_p = (STRINGP (value)
5029 #ifdef HAVE_WINDOW_SYSTEM
5030 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5031 && valid_image_p (value))
5032 #endif /* not HAVE_WINDOW_SYSTEM */
5033 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5034
5035 if (valid_p && !display_replaced_p)
5036 {
5037 int retval = 1;
5038
5039 if (!it)
5040 {
5041 /* Callers need to know whether the display spec is any kind
5042 of `(space ...)' spec that is about to affect text-area
5043 display. */
5044 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5045 retval = 2;
5046 return retval;
5047 }
5048
5049 /* Save current settings of IT so that we can restore them
5050 when we are finished with the glyph property value. */
5051 push_it (it, position);
5052 it->from_overlay = overlay;
5053 it->from_disp_prop_p = true;
5054
5055 if (NILP (location))
5056 it->area = TEXT_AREA;
5057 else if (EQ (location, Qleft_margin))
5058 it->area = LEFT_MARGIN_AREA;
5059 else
5060 it->area = RIGHT_MARGIN_AREA;
5061
5062 if (STRINGP (value))
5063 {
5064 it->string = value;
5065 it->multibyte_p = STRING_MULTIBYTE (it->string);
5066 it->current.overlay_string_index = -1;
5067 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5068 it->end_charpos = it->string_nchars = SCHARS (it->string);
5069 it->method = GET_FROM_STRING;
5070 it->stop_charpos = 0;
5071 it->prev_stop = 0;
5072 it->base_level_stop = 0;
5073 it->string_from_display_prop_p = true;
5074 /* Say that we haven't consumed the characters with
5075 `display' property yet. The call to pop_it in
5076 set_iterator_to_next will clean this up. */
5077 if (BUFFERP (object))
5078 *position = start_pos;
5079
5080 /* Force paragraph direction to be that of the parent
5081 object. If the parent object's paragraph direction is
5082 not yet determined, default to L2R. */
5083 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5084 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5085 else
5086 it->paragraph_embedding = L2R;
5087
5088 /* Set up the bidi iterator for this display string. */
5089 if (it->bidi_p)
5090 {
5091 it->bidi_it.string.lstring = it->string;
5092 it->bidi_it.string.s = NULL;
5093 it->bidi_it.string.schars = it->end_charpos;
5094 it->bidi_it.string.bufpos = bufpos;
5095 it->bidi_it.string.from_disp_str = 1;
5096 it->bidi_it.string.unibyte = !it->multibyte_p;
5097 it->bidi_it.w = it->w;
5098 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5099 }
5100 }
5101 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5102 {
5103 it->method = GET_FROM_STRETCH;
5104 it->object = value;
5105 *position = it->position = start_pos;
5106 retval = 1 + (it->area == TEXT_AREA);
5107 }
5108 #ifdef HAVE_WINDOW_SYSTEM
5109 else
5110 {
5111 it->what = IT_IMAGE;
5112 it->image_id = lookup_image (it->f, value);
5113 it->position = start_pos;
5114 it->object = NILP (object) ? it->w->contents : object;
5115 it->method = GET_FROM_IMAGE;
5116
5117 /* Say that we haven't consumed the characters with
5118 `display' property yet. The call to pop_it in
5119 set_iterator_to_next will clean this up. */
5120 *position = start_pos;
5121 }
5122 #endif /* HAVE_WINDOW_SYSTEM */
5123
5124 return retval;
5125 }
5126
5127 /* Invalid property or property not supported. Restore
5128 POSITION to what it was before. */
5129 *position = start_pos;
5130 return 0;
5131 }
5132
5133 /* Check if PROP is a display property value whose text should be
5134 treated as intangible. OVERLAY is the overlay from which PROP
5135 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5136 specify the buffer position covered by PROP. */
5137
5138 int
5139 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5140 ptrdiff_t charpos, ptrdiff_t bytepos)
5141 {
5142 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5143 struct text_pos position;
5144
5145 SET_TEXT_POS (position, charpos, bytepos);
5146 return handle_display_spec (NULL, prop, Qnil, overlay,
5147 &position, charpos, frame_window_p);
5148 }
5149
5150
5151 /* Return 1 if PROP is a display sub-property value containing STRING.
5152
5153 Implementation note: this and the following function are really
5154 special cases of handle_display_spec and
5155 handle_single_display_spec, and should ideally use the same code.
5156 Until they do, these two pairs must be consistent and must be
5157 modified in sync. */
5158
5159 static int
5160 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5161 {
5162 if (EQ (string, prop))
5163 return 1;
5164
5165 /* Skip over `when FORM'. */
5166 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5167 {
5168 prop = XCDR (prop);
5169 if (!CONSP (prop))
5170 return 0;
5171 /* Actually, the condition following `when' should be eval'ed,
5172 like handle_single_display_spec does, and we should return
5173 zero if it evaluates to nil. However, this function is
5174 called only when the buffer was already displayed and some
5175 glyph in the glyph matrix was found to come from a display
5176 string. Therefore, the condition was already evaluated, and
5177 the result was non-nil, otherwise the display string wouldn't
5178 have been displayed and we would have never been called for
5179 this property. Thus, we can skip the evaluation and assume
5180 its result is non-nil. */
5181 prop = XCDR (prop);
5182 }
5183
5184 if (CONSP (prop))
5185 /* Skip over `margin LOCATION'. */
5186 if (EQ (XCAR (prop), Qmargin))
5187 {
5188 prop = XCDR (prop);
5189 if (!CONSP (prop))
5190 return 0;
5191
5192 prop = XCDR (prop);
5193 if (!CONSP (prop))
5194 return 0;
5195 }
5196
5197 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5198 }
5199
5200
5201 /* Return 1 if STRING appears in the `display' property PROP. */
5202
5203 static int
5204 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5205 {
5206 if (CONSP (prop)
5207 && !EQ (XCAR (prop), Qwhen)
5208 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5209 {
5210 /* A list of sub-properties. */
5211 while (CONSP (prop))
5212 {
5213 if (single_display_spec_string_p (XCAR (prop), string))
5214 return 1;
5215 prop = XCDR (prop);
5216 }
5217 }
5218 else if (VECTORP (prop))
5219 {
5220 /* A vector of sub-properties. */
5221 ptrdiff_t i;
5222 for (i = 0; i < ASIZE (prop); ++i)
5223 if (single_display_spec_string_p (AREF (prop, i), string))
5224 return 1;
5225 }
5226 else
5227 return single_display_spec_string_p (prop, string);
5228
5229 return 0;
5230 }
5231
5232 /* Look for STRING in overlays and text properties in the current
5233 buffer, between character positions FROM and TO (excluding TO).
5234 BACK_P non-zero means look back (in this case, TO is supposed to be
5235 less than FROM).
5236 Value is the first character position where STRING was found, or
5237 zero if it wasn't found before hitting TO.
5238
5239 This function may only use code that doesn't eval because it is
5240 called asynchronously from note_mouse_highlight. */
5241
5242 static ptrdiff_t
5243 string_buffer_position_lim (Lisp_Object string,
5244 ptrdiff_t from, ptrdiff_t to, int back_p)
5245 {
5246 Lisp_Object limit, prop, pos;
5247 int found = 0;
5248
5249 pos = make_number (max (from, BEGV));
5250
5251 if (!back_p) /* looking forward */
5252 {
5253 limit = make_number (min (to, ZV));
5254 while (!found && !EQ (pos, limit))
5255 {
5256 prop = Fget_char_property (pos, Qdisplay, Qnil);
5257 if (!NILP (prop) && display_prop_string_p (prop, string))
5258 found = 1;
5259 else
5260 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5261 limit);
5262 }
5263 }
5264 else /* looking back */
5265 {
5266 limit = make_number (max (to, BEGV));
5267 while (!found && !EQ (pos, limit))
5268 {
5269 prop = Fget_char_property (pos, Qdisplay, Qnil);
5270 if (!NILP (prop) && display_prop_string_p (prop, string))
5271 found = 1;
5272 else
5273 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5274 limit);
5275 }
5276 }
5277
5278 return found ? XINT (pos) : 0;
5279 }
5280
5281 /* Determine which buffer position in current buffer STRING comes from.
5282 AROUND_CHARPOS is an approximate position where it could come from.
5283 Value is the buffer position or 0 if it couldn't be determined.
5284
5285 This function is necessary because we don't record buffer positions
5286 in glyphs generated from strings (to keep struct glyph small).
5287 This function may only use code that doesn't eval because it is
5288 called asynchronously from note_mouse_highlight. */
5289
5290 static ptrdiff_t
5291 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5292 {
5293 const int MAX_DISTANCE = 1000;
5294 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5295 around_charpos + MAX_DISTANCE,
5296 0);
5297
5298 if (!found)
5299 found = string_buffer_position_lim (string, around_charpos,
5300 around_charpos - MAX_DISTANCE, 1);
5301 return found;
5302 }
5303
5304
5305 \f
5306 /***********************************************************************
5307 `composition' property
5308 ***********************************************************************/
5309
5310 /* Set up iterator IT from `composition' property at its current
5311 position. Called from handle_stop. */
5312
5313 static enum prop_handled
5314 handle_composition_prop (struct it *it)
5315 {
5316 Lisp_Object prop, string;
5317 ptrdiff_t pos, pos_byte, start, end;
5318
5319 if (STRINGP (it->string))
5320 {
5321 unsigned char *s;
5322
5323 pos = IT_STRING_CHARPOS (*it);
5324 pos_byte = IT_STRING_BYTEPOS (*it);
5325 string = it->string;
5326 s = SDATA (string) + pos_byte;
5327 it->c = STRING_CHAR (s);
5328 }
5329 else
5330 {
5331 pos = IT_CHARPOS (*it);
5332 pos_byte = IT_BYTEPOS (*it);
5333 string = Qnil;
5334 it->c = FETCH_CHAR (pos_byte);
5335 }
5336
5337 /* If there's a valid composition and point is not inside of the
5338 composition (in the case that the composition is from the current
5339 buffer), draw a glyph composed from the composition components. */
5340 if (find_composition (pos, -1, &start, &end, &prop, string)
5341 && composition_valid_p (start, end, prop)
5342 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5343 {
5344 if (start < pos)
5345 /* As we can't handle this situation (perhaps font-lock added
5346 a new composition), we just return here hoping that next
5347 redisplay will detect this composition much earlier. */
5348 return HANDLED_NORMALLY;
5349 if (start != pos)
5350 {
5351 if (STRINGP (it->string))
5352 pos_byte = string_char_to_byte (it->string, start);
5353 else
5354 pos_byte = CHAR_TO_BYTE (start);
5355 }
5356 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5357 prop, string);
5358
5359 if (it->cmp_it.id >= 0)
5360 {
5361 it->cmp_it.ch = -1;
5362 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5363 it->cmp_it.nglyphs = -1;
5364 }
5365 }
5366
5367 return HANDLED_NORMALLY;
5368 }
5369
5370
5371 \f
5372 /***********************************************************************
5373 Overlay strings
5374 ***********************************************************************/
5375
5376 /* The following structure is used to record overlay strings for
5377 later sorting in load_overlay_strings. */
5378
5379 struct overlay_entry
5380 {
5381 Lisp_Object overlay;
5382 Lisp_Object string;
5383 EMACS_INT priority;
5384 int after_string_p;
5385 };
5386
5387
5388 /* Set up iterator IT from overlay strings at its current position.
5389 Called from handle_stop. */
5390
5391 static enum prop_handled
5392 handle_overlay_change (struct it *it)
5393 {
5394 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5395 return HANDLED_RECOMPUTE_PROPS;
5396 else
5397 return HANDLED_NORMALLY;
5398 }
5399
5400
5401 /* Set up the next overlay string for delivery by IT, if there is an
5402 overlay string to deliver. Called by set_iterator_to_next when the
5403 end of the current overlay string is reached. If there are more
5404 overlay strings to display, IT->string and
5405 IT->current.overlay_string_index are set appropriately here.
5406 Otherwise IT->string is set to nil. */
5407
5408 static void
5409 next_overlay_string (struct it *it)
5410 {
5411 ++it->current.overlay_string_index;
5412 if (it->current.overlay_string_index == it->n_overlay_strings)
5413 {
5414 /* No more overlay strings. Restore IT's settings to what
5415 they were before overlay strings were processed, and
5416 continue to deliver from current_buffer. */
5417
5418 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5419 pop_it (it);
5420 eassert (it->sp > 0
5421 || (NILP (it->string)
5422 && it->method == GET_FROM_BUFFER
5423 && it->stop_charpos >= BEGV
5424 && it->stop_charpos <= it->end_charpos));
5425 it->current.overlay_string_index = -1;
5426 it->n_overlay_strings = 0;
5427 it->overlay_strings_charpos = -1;
5428 /* If there's an empty display string on the stack, pop the
5429 stack, to resync the bidi iterator with IT's position. Such
5430 empty strings are pushed onto the stack in
5431 get_overlay_strings_1. */
5432 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5433 pop_it (it);
5434
5435 /* If we're at the end of the buffer, record that we have
5436 processed the overlay strings there already, so that
5437 next_element_from_buffer doesn't try it again. */
5438 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5439 it->overlay_strings_at_end_processed_p = true;
5440 }
5441 else
5442 {
5443 /* There are more overlay strings to process. If
5444 IT->current.overlay_string_index has advanced to a position
5445 where we must load IT->overlay_strings with more strings, do
5446 it. We must load at the IT->overlay_strings_charpos where
5447 IT->n_overlay_strings was originally computed; when invisible
5448 text is present, this might not be IT_CHARPOS (Bug#7016). */
5449 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5450
5451 if (it->current.overlay_string_index && i == 0)
5452 load_overlay_strings (it, it->overlay_strings_charpos);
5453
5454 /* Initialize IT to deliver display elements from the overlay
5455 string. */
5456 it->string = it->overlay_strings[i];
5457 it->multibyte_p = STRING_MULTIBYTE (it->string);
5458 SET_TEXT_POS (it->current.string_pos, 0, 0);
5459 it->method = GET_FROM_STRING;
5460 it->stop_charpos = 0;
5461 it->end_charpos = SCHARS (it->string);
5462 if (it->cmp_it.stop_pos >= 0)
5463 it->cmp_it.stop_pos = 0;
5464 it->prev_stop = 0;
5465 it->base_level_stop = 0;
5466
5467 /* Set up the bidi iterator for this overlay string. */
5468 if (it->bidi_p)
5469 {
5470 it->bidi_it.string.lstring = it->string;
5471 it->bidi_it.string.s = NULL;
5472 it->bidi_it.string.schars = SCHARS (it->string);
5473 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5474 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5475 it->bidi_it.string.unibyte = !it->multibyte_p;
5476 it->bidi_it.w = it->w;
5477 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5478 }
5479 }
5480
5481 CHECK_IT (it);
5482 }
5483
5484
5485 /* Compare two overlay_entry structures E1 and E2. Used as a
5486 comparison function for qsort in load_overlay_strings. Overlay
5487 strings for the same position are sorted so that
5488
5489 1. All after-strings come in front of before-strings, except
5490 when they come from the same overlay.
5491
5492 2. Within after-strings, strings are sorted so that overlay strings
5493 from overlays with higher priorities come first.
5494
5495 2. Within before-strings, strings are sorted so that overlay
5496 strings from overlays with higher priorities come last.
5497
5498 Value is analogous to strcmp. */
5499
5500
5501 static int
5502 compare_overlay_entries (const void *e1, const void *e2)
5503 {
5504 struct overlay_entry const *entry1 = e1;
5505 struct overlay_entry const *entry2 = e2;
5506 int result;
5507
5508 if (entry1->after_string_p != entry2->after_string_p)
5509 {
5510 /* Let after-strings appear in front of before-strings if
5511 they come from different overlays. */
5512 if (EQ (entry1->overlay, entry2->overlay))
5513 result = entry1->after_string_p ? 1 : -1;
5514 else
5515 result = entry1->after_string_p ? -1 : 1;
5516 }
5517 else if (entry1->priority != entry2->priority)
5518 {
5519 if (entry1->after_string_p)
5520 /* After-strings sorted in order of decreasing priority. */
5521 result = entry2->priority < entry1->priority ? -1 : 1;
5522 else
5523 /* Before-strings sorted in order of increasing priority. */
5524 result = entry1->priority < entry2->priority ? -1 : 1;
5525 }
5526 else
5527 result = 0;
5528
5529 return result;
5530 }
5531
5532
5533 /* Load the vector IT->overlay_strings with overlay strings from IT's
5534 current buffer position, or from CHARPOS if that is > 0. Set
5535 IT->n_overlays to the total number of overlay strings found.
5536
5537 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5538 a time. On entry into load_overlay_strings,
5539 IT->current.overlay_string_index gives the number of overlay
5540 strings that have already been loaded by previous calls to this
5541 function.
5542
5543 IT->add_overlay_start contains an additional overlay start
5544 position to consider for taking overlay strings from, if non-zero.
5545 This position comes into play when the overlay has an `invisible'
5546 property, and both before and after-strings. When we've skipped to
5547 the end of the overlay, because of its `invisible' property, we
5548 nevertheless want its before-string to appear.
5549 IT->add_overlay_start will contain the overlay start position
5550 in this case.
5551
5552 Overlay strings are sorted so that after-string strings come in
5553 front of before-string strings. Within before and after-strings,
5554 strings are sorted by overlay priority. See also function
5555 compare_overlay_entries. */
5556
5557 static void
5558 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5559 {
5560 Lisp_Object overlay, window, str, invisible;
5561 struct Lisp_Overlay *ov;
5562 ptrdiff_t start, end;
5563 ptrdiff_t size = 20;
5564 ptrdiff_t n = 0, i, j;
5565 int invis_p;
5566 struct overlay_entry *entries = alloca (size * sizeof *entries);
5567 USE_SAFE_ALLOCA;
5568
5569 if (charpos <= 0)
5570 charpos = IT_CHARPOS (*it);
5571
5572 /* Append the overlay string STRING of overlay OVERLAY to vector
5573 `entries' which has size `size' and currently contains `n'
5574 elements. AFTER_P non-zero means STRING is an after-string of
5575 OVERLAY. */
5576 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5577 do \
5578 { \
5579 Lisp_Object priority; \
5580 \
5581 if (n == size) \
5582 { \
5583 struct overlay_entry *old = entries; \
5584 SAFE_NALLOCA (entries, 2, size); \
5585 memcpy (entries, old, size * sizeof *entries); \
5586 size *= 2; \
5587 } \
5588 \
5589 entries[n].string = (STRING); \
5590 entries[n].overlay = (OVERLAY); \
5591 priority = Foverlay_get ((OVERLAY), Qpriority); \
5592 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5593 entries[n].after_string_p = (AFTER_P); \
5594 ++n; \
5595 } \
5596 while (0)
5597
5598 /* Process overlay before the overlay center. */
5599 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5600 {
5601 XSETMISC (overlay, ov);
5602 eassert (OVERLAYP (overlay));
5603 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5604 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5605
5606 if (end < charpos)
5607 break;
5608
5609 /* Skip this overlay if it doesn't start or end at IT's current
5610 position. */
5611 if (end != charpos && start != charpos)
5612 continue;
5613
5614 /* Skip this overlay if it doesn't apply to IT->w. */
5615 window = Foverlay_get (overlay, Qwindow);
5616 if (WINDOWP (window) && XWINDOW (window) != it->w)
5617 continue;
5618
5619 /* If the text ``under'' the overlay is invisible, both before-
5620 and after-strings from this overlay are visible; start and
5621 end position are indistinguishable. */
5622 invisible = Foverlay_get (overlay, Qinvisible);
5623 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5624
5625 /* If overlay has a non-empty before-string, record it. */
5626 if ((start == charpos || (end == charpos && invis_p))
5627 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5628 && SCHARS (str))
5629 RECORD_OVERLAY_STRING (overlay, str, 0);
5630
5631 /* If overlay has a non-empty after-string, record it. */
5632 if ((end == charpos || (start == charpos && invis_p))
5633 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5634 && SCHARS (str))
5635 RECORD_OVERLAY_STRING (overlay, str, 1);
5636 }
5637
5638 /* Process overlays after the overlay center. */
5639 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5640 {
5641 XSETMISC (overlay, ov);
5642 eassert (OVERLAYP (overlay));
5643 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5644 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5645
5646 if (start > charpos)
5647 break;
5648
5649 /* Skip this overlay if it doesn't start or end at IT's current
5650 position. */
5651 if (end != charpos && start != charpos)
5652 continue;
5653
5654 /* Skip this overlay if it doesn't apply to IT->w. */
5655 window = Foverlay_get (overlay, Qwindow);
5656 if (WINDOWP (window) && XWINDOW (window) != it->w)
5657 continue;
5658
5659 /* If the text ``under'' the overlay is invisible, it has a zero
5660 dimension, and both before- and after-strings apply. */
5661 invisible = Foverlay_get (overlay, Qinvisible);
5662 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5663
5664 /* If overlay has a non-empty before-string, record it. */
5665 if ((start == charpos || (end == charpos && invis_p))
5666 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5667 && SCHARS (str))
5668 RECORD_OVERLAY_STRING (overlay, str, 0);
5669
5670 /* If overlay has a non-empty after-string, record it. */
5671 if ((end == charpos || (start == charpos && invis_p))
5672 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5673 && SCHARS (str))
5674 RECORD_OVERLAY_STRING (overlay, str, 1);
5675 }
5676
5677 #undef RECORD_OVERLAY_STRING
5678
5679 /* Sort entries. */
5680 if (n > 1)
5681 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5682
5683 /* Record number of overlay strings, and where we computed it. */
5684 it->n_overlay_strings = n;
5685 it->overlay_strings_charpos = charpos;
5686
5687 /* IT->current.overlay_string_index is the number of overlay strings
5688 that have already been consumed by IT. Copy some of the
5689 remaining overlay strings to IT->overlay_strings. */
5690 i = 0;
5691 j = it->current.overlay_string_index;
5692 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5693 {
5694 it->overlay_strings[i] = entries[j].string;
5695 it->string_overlays[i++] = entries[j++].overlay;
5696 }
5697
5698 CHECK_IT (it);
5699 SAFE_FREE ();
5700 }
5701
5702
5703 /* Get the first chunk of overlay strings at IT's current buffer
5704 position, or at CHARPOS if that is > 0. Value is non-zero if at
5705 least one overlay string was found. */
5706
5707 static int
5708 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5709 {
5710 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5711 process. This fills IT->overlay_strings with strings, and sets
5712 IT->n_overlay_strings to the total number of strings to process.
5713 IT->pos.overlay_string_index has to be set temporarily to zero
5714 because load_overlay_strings needs this; it must be set to -1
5715 when no overlay strings are found because a zero value would
5716 indicate a position in the first overlay string. */
5717 it->current.overlay_string_index = 0;
5718 load_overlay_strings (it, charpos);
5719
5720 /* If we found overlay strings, set up IT to deliver display
5721 elements from the first one. Otherwise set up IT to deliver
5722 from current_buffer. */
5723 if (it->n_overlay_strings)
5724 {
5725 /* Make sure we know settings in current_buffer, so that we can
5726 restore meaningful values when we're done with the overlay
5727 strings. */
5728 if (compute_stop_p)
5729 compute_stop_pos (it);
5730 eassert (it->face_id >= 0);
5731
5732 /* Save IT's settings. They are restored after all overlay
5733 strings have been processed. */
5734 eassert (!compute_stop_p || it->sp == 0);
5735
5736 /* When called from handle_stop, there might be an empty display
5737 string loaded. In that case, don't bother saving it. But
5738 don't use this optimization with the bidi iterator, since we
5739 need the corresponding pop_it call to resync the bidi
5740 iterator's position with IT's position, after we are done
5741 with the overlay strings. (The corresponding call to pop_it
5742 in case of an empty display string is in
5743 next_overlay_string.) */
5744 if (!(!it->bidi_p
5745 && STRINGP (it->string) && !SCHARS (it->string)))
5746 push_it (it, NULL);
5747
5748 /* Set up IT to deliver display elements from the first overlay
5749 string. */
5750 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5751 it->string = it->overlay_strings[0];
5752 it->from_overlay = Qnil;
5753 it->stop_charpos = 0;
5754 eassert (STRINGP (it->string));
5755 it->end_charpos = SCHARS (it->string);
5756 it->prev_stop = 0;
5757 it->base_level_stop = 0;
5758 it->multibyte_p = STRING_MULTIBYTE (it->string);
5759 it->method = GET_FROM_STRING;
5760 it->from_disp_prop_p = 0;
5761
5762 /* Force paragraph direction to be that of the parent
5763 buffer. */
5764 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5765 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5766 else
5767 it->paragraph_embedding = L2R;
5768
5769 /* Set up the bidi iterator for this overlay string. */
5770 if (it->bidi_p)
5771 {
5772 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5773
5774 it->bidi_it.string.lstring = it->string;
5775 it->bidi_it.string.s = NULL;
5776 it->bidi_it.string.schars = SCHARS (it->string);
5777 it->bidi_it.string.bufpos = pos;
5778 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5779 it->bidi_it.string.unibyte = !it->multibyte_p;
5780 it->bidi_it.w = it->w;
5781 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5782 }
5783 return 1;
5784 }
5785
5786 it->current.overlay_string_index = -1;
5787 return 0;
5788 }
5789
5790 static int
5791 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5792 {
5793 it->string = Qnil;
5794 it->method = GET_FROM_BUFFER;
5795
5796 (void) get_overlay_strings_1 (it, charpos, 1);
5797
5798 CHECK_IT (it);
5799
5800 /* Value is non-zero if we found at least one overlay string. */
5801 return STRINGP (it->string);
5802 }
5803
5804
5805 \f
5806 /***********************************************************************
5807 Saving and restoring state
5808 ***********************************************************************/
5809
5810 /* Save current settings of IT on IT->stack. Called, for example,
5811 before setting up IT for an overlay string, to be able to restore
5812 IT's settings to what they were after the overlay string has been
5813 processed. If POSITION is non-NULL, it is the position to save on
5814 the stack instead of IT->position. */
5815
5816 static void
5817 push_it (struct it *it, struct text_pos *position)
5818 {
5819 struct iterator_stack_entry *p;
5820
5821 eassert (it->sp < IT_STACK_SIZE);
5822 p = it->stack + it->sp;
5823
5824 p->stop_charpos = it->stop_charpos;
5825 p->prev_stop = it->prev_stop;
5826 p->base_level_stop = it->base_level_stop;
5827 p->cmp_it = it->cmp_it;
5828 eassert (it->face_id >= 0);
5829 p->face_id = it->face_id;
5830 p->string = it->string;
5831 p->method = it->method;
5832 p->from_overlay = it->from_overlay;
5833 switch (p->method)
5834 {
5835 case GET_FROM_IMAGE:
5836 p->u.image.object = it->object;
5837 p->u.image.image_id = it->image_id;
5838 p->u.image.slice = it->slice;
5839 break;
5840 case GET_FROM_STRETCH:
5841 p->u.stretch.object = it->object;
5842 break;
5843 }
5844 p->position = position ? *position : it->position;
5845 p->current = it->current;
5846 p->end_charpos = it->end_charpos;
5847 p->string_nchars = it->string_nchars;
5848 p->area = it->area;
5849 p->multibyte_p = it->multibyte_p;
5850 p->avoid_cursor_p = it->avoid_cursor_p;
5851 p->space_width = it->space_width;
5852 p->font_height = it->font_height;
5853 p->voffset = it->voffset;
5854 p->string_from_display_prop_p = it->string_from_display_prop_p;
5855 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5856 p->display_ellipsis_p = 0;
5857 p->line_wrap = it->line_wrap;
5858 p->bidi_p = it->bidi_p;
5859 p->paragraph_embedding = it->paragraph_embedding;
5860 p->from_disp_prop_p = it->from_disp_prop_p;
5861 ++it->sp;
5862
5863 /* Save the state of the bidi iterator as well. */
5864 if (it->bidi_p)
5865 bidi_push_it (&it->bidi_it);
5866 }
5867
5868 static void
5869 iterate_out_of_display_property (struct it *it)
5870 {
5871 int buffer_p = !STRINGP (it->string);
5872 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5873 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5874
5875 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5876
5877 /* Maybe initialize paragraph direction. If we are at the beginning
5878 of a new paragraph, next_element_from_buffer may not have a
5879 chance to do that. */
5880 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5881 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5882 /* prev_stop can be zero, so check against BEGV as well. */
5883 while (it->bidi_it.charpos >= bob
5884 && it->prev_stop <= it->bidi_it.charpos
5885 && it->bidi_it.charpos < CHARPOS (it->position)
5886 && it->bidi_it.charpos < eob)
5887 bidi_move_to_visually_next (&it->bidi_it);
5888 /* Record the stop_pos we just crossed, for when we cross it
5889 back, maybe. */
5890 if (it->bidi_it.charpos > CHARPOS (it->position))
5891 it->prev_stop = CHARPOS (it->position);
5892 /* If we ended up not where pop_it put us, resync IT's
5893 positional members with the bidi iterator. */
5894 if (it->bidi_it.charpos != CHARPOS (it->position))
5895 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5896 if (buffer_p)
5897 it->current.pos = it->position;
5898 else
5899 it->current.string_pos = it->position;
5900 }
5901
5902 /* Restore IT's settings from IT->stack. Called, for example, when no
5903 more overlay strings must be processed, and we return to delivering
5904 display elements from a buffer, or when the end of a string from a
5905 `display' property is reached and we return to delivering display
5906 elements from an overlay string, or from a buffer. */
5907
5908 static void
5909 pop_it (struct it *it)
5910 {
5911 struct iterator_stack_entry *p;
5912 int from_display_prop = it->from_disp_prop_p;
5913
5914 eassert (it->sp > 0);
5915 --it->sp;
5916 p = it->stack + it->sp;
5917 it->stop_charpos = p->stop_charpos;
5918 it->prev_stop = p->prev_stop;
5919 it->base_level_stop = p->base_level_stop;
5920 it->cmp_it = p->cmp_it;
5921 it->face_id = p->face_id;
5922 it->current = p->current;
5923 it->position = p->position;
5924 it->string = p->string;
5925 it->from_overlay = p->from_overlay;
5926 if (NILP (it->string))
5927 SET_TEXT_POS (it->current.string_pos, -1, -1);
5928 it->method = p->method;
5929 switch (it->method)
5930 {
5931 case GET_FROM_IMAGE:
5932 it->image_id = p->u.image.image_id;
5933 it->object = p->u.image.object;
5934 it->slice = p->u.image.slice;
5935 break;
5936 case GET_FROM_STRETCH:
5937 it->object = p->u.stretch.object;
5938 break;
5939 case GET_FROM_BUFFER:
5940 it->object = it->w->contents;
5941 break;
5942 case GET_FROM_STRING:
5943 it->object = it->string;
5944 break;
5945 case GET_FROM_DISPLAY_VECTOR:
5946 if (it->s)
5947 it->method = GET_FROM_C_STRING;
5948 else if (STRINGP (it->string))
5949 it->method = GET_FROM_STRING;
5950 else
5951 {
5952 it->method = GET_FROM_BUFFER;
5953 it->object = it->w->contents;
5954 }
5955 }
5956 it->end_charpos = p->end_charpos;
5957 it->string_nchars = p->string_nchars;
5958 it->area = p->area;
5959 it->multibyte_p = p->multibyte_p;
5960 it->avoid_cursor_p = p->avoid_cursor_p;
5961 it->space_width = p->space_width;
5962 it->font_height = p->font_height;
5963 it->voffset = p->voffset;
5964 it->string_from_display_prop_p = p->string_from_display_prop_p;
5965 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5966 it->line_wrap = p->line_wrap;
5967 it->bidi_p = p->bidi_p;
5968 it->paragraph_embedding = p->paragraph_embedding;
5969 it->from_disp_prop_p = p->from_disp_prop_p;
5970 if (it->bidi_p)
5971 {
5972 bidi_pop_it (&it->bidi_it);
5973 /* Bidi-iterate until we get out of the portion of text, if any,
5974 covered by a `display' text property or by an overlay with
5975 `display' property. (We cannot just jump there, because the
5976 internal coherency of the bidi iterator state can not be
5977 preserved across such jumps.) We also must determine the
5978 paragraph base direction if the overlay we just processed is
5979 at the beginning of a new paragraph. */
5980 if (from_display_prop
5981 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5982 iterate_out_of_display_property (it);
5983
5984 eassert ((BUFFERP (it->object)
5985 && IT_CHARPOS (*it) == it->bidi_it.charpos
5986 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5987 || (STRINGP (it->object)
5988 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5989 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5990 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5991 }
5992 }
5993
5994
5995 \f
5996 /***********************************************************************
5997 Moving over lines
5998 ***********************************************************************/
5999
6000 /* Set IT's current position to the previous line start. */
6001
6002 static void
6003 back_to_previous_line_start (struct it *it)
6004 {
6005 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6006
6007 DEC_BOTH (cp, bp);
6008 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6009 }
6010
6011
6012 /* Move IT to the next line start.
6013
6014 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6015 we skipped over part of the text (as opposed to moving the iterator
6016 continuously over the text). Otherwise, don't change the value
6017 of *SKIPPED_P.
6018
6019 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6020 iterator on the newline, if it was found.
6021
6022 Newlines may come from buffer text, overlay strings, or strings
6023 displayed via the `display' property. That's the reason we can't
6024 simply use find_newline_no_quit.
6025
6026 Note that this function may not skip over invisible text that is so
6027 because of text properties and immediately follows a newline. If
6028 it would, function reseat_at_next_visible_line_start, when called
6029 from set_iterator_to_next, would effectively make invisible
6030 characters following a newline part of the wrong glyph row, which
6031 leads to wrong cursor motion. */
6032
6033 static int
6034 forward_to_next_line_start (struct it *it, int *skipped_p,
6035 struct bidi_it *bidi_it_prev)
6036 {
6037 ptrdiff_t old_selective;
6038 int newline_found_p, n;
6039 const int MAX_NEWLINE_DISTANCE = 500;
6040
6041 /* If already on a newline, just consume it to avoid unintended
6042 skipping over invisible text below. */
6043 if (it->what == IT_CHARACTER
6044 && it->c == '\n'
6045 && CHARPOS (it->position) == IT_CHARPOS (*it))
6046 {
6047 if (it->bidi_p && bidi_it_prev)
6048 *bidi_it_prev = it->bidi_it;
6049 set_iterator_to_next (it, 0);
6050 it->c = 0;
6051 return 1;
6052 }
6053
6054 /* Don't handle selective display in the following. It's (a)
6055 unnecessary because it's done by the caller, and (b) leads to an
6056 infinite recursion because next_element_from_ellipsis indirectly
6057 calls this function. */
6058 old_selective = it->selective;
6059 it->selective = 0;
6060
6061 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6062 from buffer text. */
6063 for (n = newline_found_p = 0;
6064 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6065 n += STRINGP (it->string) ? 0 : 1)
6066 {
6067 if (!get_next_display_element (it))
6068 return 0;
6069 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6070 if (newline_found_p && it->bidi_p && bidi_it_prev)
6071 *bidi_it_prev = it->bidi_it;
6072 set_iterator_to_next (it, 0);
6073 }
6074
6075 /* If we didn't find a newline near enough, see if we can use a
6076 short-cut. */
6077 if (!newline_found_p)
6078 {
6079 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6080 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6081 1, &bytepos);
6082 Lisp_Object pos;
6083
6084 eassert (!STRINGP (it->string));
6085
6086 /* If there isn't any `display' property in sight, and no
6087 overlays, we can just use the position of the newline in
6088 buffer text. */
6089 if (it->stop_charpos >= limit
6090 || ((pos = Fnext_single_property_change (make_number (start),
6091 Qdisplay, Qnil,
6092 make_number (limit)),
6093 NILP (pos))
6094 && next_overlay_change (start) == ZV))
6095 {
6096 if (!it->bidi_p)
6097 {
6098 IT_CHARPOS (*it) = limit;
6099 IT_BYTEPOS (*it) = bytepos;
6100 }
6101 else
6102 {
6103 struct bidi_it bprev;
6104
6105 /* Help bidi.c avoid expensive searches for display
6106 properties and overlays, by telling it that there are
6107 none up to `limit'. */
6108 if (it->bidi_it.disp_pos < limit)
6109 {
6110 it->bidi_it.disp_pos = limit;
6111 it->bidi_it.disp_prop = 0;
6112 }
6113 do {
6114 bprev = it->bidi_it;
6115 bidi_move_to_visually_next (&it->bidi_it);
6116 } while (it->bidi_it.charpos != limit);
6117 IT_CHARPOS (*it) = limit;
6118 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6119 if (bidi_it_prev)
6120 *bidi_it_prev = bprev;
6121 }
6122 *skipped_p = newline_found_p = true;
6123 }
6124 else
6125 {
6126 while (get_next_display_element (it)
6127 && !newline_found_p)
6128 {
6129 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6130 if (newline_found_p && it->bidi_p && bidi_it_prev)
6131 *bidi_it_prev = it->bidi_it;
6132 set_iterator_to_next (it, 0);
6133 }
6134 }
6135 }
6136
6137 it->selective = old_selective;
6138 return newline_found_p;
6139 }
6140
6141
6142 /* Set IT's current position to the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. Caution: this does not change IT->current_x and
6145 IT->hpos. */
6146
6147 static void
6148 back_to_previous_visible_line_start (struct it *it)
6149 {
6150 while (IT_CHARPOS (*it) > BEGV)
6151 {
6152 back_to_previous_line_start (it);
6153
6154 if (IT_CHARPOS (*it) <= BEGV)
6155 break;
6156
6157 /* If selective > 0, then lines indented more than its value are
6158 invisible. */
6159 if (it->selective > 0
6160 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6161 it->selective))
6162 continue;
6163
6164 /* Check the newline before point for invisibility. */
6165 {
6166 Lisp_Object prop;
6167 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6168 Qinvisible, it->window);
6169 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6170 continue;
6171 }
6172
6173 if (IT_CHARPOS (*it) <= BEGV)
6174 break;
6175
6176 {
6177 struct it it2;
6178 void *it2data = NULL;
6179 ptrdiff_t pos;
6180 ptrdiff_t beg, end;
6181 Lisp_Object val, overlay;
6182
6183 SAVE_IT (it2, *it, it2data);
6184
6185 /* If newline is part of a composition, continue from start of composition */
6186 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6187 && beg < IT_CHARPOS (*it))
6188 goto replaced;
6189
6190 /* If newline is replaced by a display property, find start of overlay
6191 or interval and continue search from that point. */
6192 pos = --IT_CHARPOS (it2);
6193 --IT_BYTEPOS (it2);
6194 it2.sp = 0;
6195 bidi_unshelve_cache (NULL, 0);
6196 it2.string_from_display_prop_p = 0;
6197 it2.from_disp_prop_p = 0;
6198 if (handle_display_prop (&it2) == HANDLED_RETURN
6199 && !NILP (val = get_char_property_and_overlay
6200 (make_number (pos), Qdisplay, Qnil, &overlay))
6201 && (OVERLAYP (overlay)
6202 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6203 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6204 {
6205 RESTORE_IT (it, it, it2data);
6206 goto replaced;
6207 }
6208
6209 /* Newline is not replaced by anything -- so we are done. */
6210 RESTORE_IT (it, it, it2data);
6211 break;
6212
6213 replaced:
6214 if (beg < BEGV)
6215 beg = BEGV;
6216 IT_CHARPOS (*it) = beg;
6217 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6218 }
6219 }
6220
6221 it->continuation_lines_width = 0;
6222
6223 eassert (IT_CHARPOS (*it) >= BEGV);
6224 eassert (IT_CHARPOS (*it) == BEGV
6225 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6226 CHECK_IT (it);
6227 }
6228
6229
6230 /* Reseat iterator IT at the previous visible line start. Skip
6231 invisible text that is so either due to text properties or due to
6232 selective display. At the end, update IT's overlay information,
6233 face information etc. */
6234
6235 void
6236 reseat_at_previous_visible_line_start (struct it *it)
6237 {
6238 back_to_previous_visible_line_start (it);
6239 reseat (it, it->current.pos, 1);
6240 CHECK_IT (it);
6241 }
6242
6243
6244 /* Reseat iterator IT on the next visible line start in the current
6245 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6246 preceding the line start. Skip over invisible text that is so
6247 because of selective display. Compute faces, overlays etc at the
6248 new position. Note that this function does not skip over text that
6249 is invisible because of text properties. */
6250
6251 static void
6252 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6253 {
6254 int newline_found_p, skipped_p = 0;
6255 struct bidi_it bidi_it_prev;
6256
6257 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6258
6259 /* Skip over lines that are invisible because they are indented
6260 more than the value of IT->selective. */
6261 if (it->selective > 0)
6262 while (IT_CHARPOS (*it) < ZV
6263 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6264 it->selective))
6265 {
6266 eassert (IT_BYTEPOS (*it) == BEGV
6267 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6268 newline_found_p =
6269 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6270 }
6271
6272 /* Position on the newline if that's what's requested. */
6273 if (on_newline_p && newline_found_p)
6274 {
6275 if (STRINGP (it->string))
6276 {
6277 if (IT_STRING_CHARPOS (*it) > 0)
6278 {
6279 if (!it->bidi_p)
6280 {
6281 --IT_STRING_CHARPOS (*it);
6282 --IT_STRING_BYTEPOS (*it);
6283 }
6284 else
6285 {
6286 /* We need to restore the bidi iterator to the state
6287 it had on the newline, and resync the IT's
6288 position with that. */
6289 it->bidi_it = bidi_it_prev;
6290 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6291 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6292 }
6293 }
6294 }
6295 else if (IT_CHARPOS (*it) > BEGV)
6296 {
6297 if (!it->bidi_p)
6298 {
6299 --IT_CHARPOS (*it);
6300 --IT_BYTEPOS (*it);
6301 }
6302 else
6303 {
6304 /* We need to restore the bidi iterator to the state it
6305 had on the newline and resync IT with that. */
6306 it->bidi_it = bidi_it_prev;
6307 IT_CHARPOS (*it) = it->bidi_it.charpos;
6308 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6309 }
6310 reseat (it, it->current.pos, 0);
6311 }
6312 }
6313 else if (skipped_p)
6314 reseat (it, it->current.pos, 0);
6315
6316 CHECK_IT (it);
6317 }
6318
6319
6320 \f
6321 /***********************************************************************
6322 Changing an iterator's position
6323 ***********************************************************************/
6324
6325 /* Change IT's current position to POS in current_buffer. If FORCE_P
6326 is non-zero, always check for text properties at the new position.
6327 Otherwise, text properties are only looked up if POS >=
6328 IT->check_charpos of a property. */
6329
6330 static void
6331 reseat (struct it *it, struct text_pos pos, int force_p)
6332 {
6333 ptrdiff_t original_pos = IT_CHARPOS (*it);
6334
6335 reseat_1 (it, pos, 0);
6336
6337 /* Determine where to check text properties. Avoid doing it
6338 where possible because text property lookup is very expensive. */
6339 if (force_p
6340 || CHARPOS (pos) > it->stop_charpos
6341 || CHARPOS (pos) < original_pos)
6342 {
6343 if (it->bidi_p)
6344 {
6345 /* For bidi iteration, we need to prime prev_stop and
6346 base_level_stop with our best estimations. */
6347 /* Implementation note: Of course, POS is not necessarily a
6348 stop position, so assigning prev_pos to it is a lie; we
6349 should have called compute_stop_backwards. However, if
6350 the current buffer does not include any R2L characters,
6351 that call would be a waste of cycles, because the
6352 iterator will never move back, and thus never cross this
6353 "fake" stop position. So we delay that backward search
6354 until the time we really need it, in next_element_from_buffer. */
6355 if (CHARPOS (pos) != it->prev_stop)
6356 it->prev_stop = CHARPOS (pos);
6357 if (CHARPOS (pos) < it->base_level_stop)
6358 it->base_level_stop = 0; /* meaning it's unknown */
6359 handle_stop (it);
6360 }
6361 else
6362 {
6363 handle_stop (it);
6364 it->prev_stop = it->base_level_stop = 0;
6365 }
6366
6367 }
6368
6369 CHECK_IT (it);
6370 }
6371
6372
6373 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6374 IT->stop_pos to POS, also. */
6375
6376 static void
6377 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6378 {
6379 /* Don't call this function when scanning a C string. */
6380 eassert (it->s == NULL);
6381
6382 /* POS must be a reasonable value. */
6383 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6384
6385 it->current.pos = it->position = pos;
6386 it->end_charpos = ZV;
6387 it->dpvec = NULL;
6388 it->current.dpvec_index = -1;
6389 it->current.overlay_string_index = -1;
6390 IT_STRING_CHARPOS (*it) = -1;
6391 IT_STRING_BYTEPOS (*it) = -1;
6392 it->string = Qnil;
6393 it->method = GET_FROM_BUFFER;
6394 it->object = it->w->contents;
6395 it->area = TEXT_AREA;
6396 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6397 it->sp = 0;
6398 it->string_from_display_prop_p = 0;
6399 it->string_from_prefix_prop_p = 0;
6400
6401 it->from_disp_prop_p = 0;
6402 it->face_before_selective_p = 0;
6403 if (it->bidi_p)
6404 {
6405 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6406 &it->bidi_it);
6407 bidi_unshelve_cache (NULL, 0);
6408 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6409 it->bidi_it.string.s = NULL;
6410 it->bidi_it.string.lstring = Qnil;
6411 it->bidi_it.string.bufpos = 0;
6412 it->bidi_it.string.unibyte = 0;
6413 it->bidi_it.w = it->w;
6414 }
6415
6416 if (set_stop_p)
6417 {
6418 it->stop_charpos = CHARPOS (pos);
6419 it->base_level_stop = CHARPOS (pos);
6420 }
6421 /* This make the information stored in it->cmp_it invalidate. */
6422 it->cmp_it.id = -1;
6423 }
6424
6425
6426 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6427 If S is non-null, it is a C string to iterate over. Otherwise,
6428 STRING gives a Lisp string to iterate over.
6429
6430 If PRECISION > 0, don't return more then PRECISION number of
6431 characters from the string.
6432
6433 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6434 characters have been returned. FIELD_WIDTH < 0 means an infinite
6435 field width.
6436
6437 MULTIBYTE = 0 means disable processing of multibyte characters,
6438 MULTIBYTE > 0 means enable it,
6439 MULTIBYTE < 0 means use IT->multibyte_p.
6440
6441 IT must be initialized via a prior call to init_iterator before
6442 calling this function. */
6443
6444 static void
6445 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6446 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6447 int multibyte)
6448 {
6449 /* No text property checks performed by default, but see below. */
6450 it->stop_charpos = -1;
6451
6452 /* Set iterator position and end position. */
6453 memset (&it->current, 0, sizeof it->current);
6454 it->current.overlay_string_index = -1;
6455 it->current.dpvec_index = -1;
6456 eassert (charpos >= 0);
6457
6458 /* If STRING is specified, use its multibyteness, otherwise use the
6459 setting of MULTIBYTE, if specified. */
6460 if (multibyte >= 0)
6461 it->multibyte_p = multibyte > 0;
6462
6463 /* Bidirectional reordering of strings is controlled by the default
6464 value of bidi-display-reordering. Don't try to reorder while
6465 loading loadup.el, as the necessary character property tables are
6466 not yet available. */
6467 it->bidi_p =
6468 NILP (Vpurify_flag)
6469 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6470
6471 if (s == NULL)
6472 {
6473 eassert (STRINGP (string));
6474 it->string = string;
6475 it->s = NULL;
6476 it->end_charpos = it->string_nchars = SCHARS (string);
6477 it->method = GET_FROM_STRING;
6478 it->current.string_pos = string_pos (charpos, string);
6479
6480 if (it->bidi_p)
6481 {
6482 it->bidi_it.string.lstring = string;
6483 it->bidi_it.string.s = NULL;
6484 it->bidi_it.string.schars = it->end_charpos;
6485 it->bidi_it.string.bufpos = 0;
6486 it->bidi_it.string.from_disp_str = 0;
6487 it->bidi_it.string.unibyte = !it->multibyte_p;
6488 it->bidi_it.w = it->w;
6489 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6490 FRAME_WINDOW_P (it->f), &it->bidi_it);
6491 }
6492 }
6493 else
6494 {
6495 it->s = (const unsigned char *) s;
6496 it->string = Qnil;
6497
6498 /* Note that we use IT->current.pos, not it->current.string_pos,
6499 for displaying C strings. */
6500 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6501 if (it->multibyte_p)
6502 {
6503 it->current.pos = c_string_pos (charpos, s, 1);
6504 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6505 }
6506 else
6507 {
6508 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6509 it->end_charpos = it->string_nchars = strlen (s);
6510 }
6511
6512 if (it->bidi_p)
6513 {
6514 it->bidi_it.string.lstring = Qnil;
6515 it->bidi_it.string.s = (const unsigned char *) s;
6516 it->bidi_it.string.schars = it->end_charpos;
6517 it->bidi_it.string.bufpos = 0;
6518 it->bidi_it.string.from_disp_str = 0;
6519 it->bidi_it.string.unibyte = !it->multibyte_p;
6520 it->bidi_it.w = it->w;
6521 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6522 &it->bidi_it);
6523 }
6524 it->method = GET_FROM_C_STRING;
6525 }
6526
6527 /* PRECISION > 0 means don't return more than PRECISION characters
6528 from the string. */
6529 if (precision > 0 && it->end_charpos - charpos > precision)
6530 {
6531 it->end_charpos = it->string_nchars = charpos + precision;
6532 if (it->bidi_p)
6533 it->bidi_it.string.schars = it->end_charpos;
6534 }
6535
6536 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6537 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6538 FIELD_WIDTH < 0 means infinite field width. This is useful for
6539 padding with `-' at the end of a mode line. */
6540 if (field_width < 0)
6541 field_width = INFINITY;
6542 /* Implementation note: We deliberately don't enlarge
6543 it->bidi_it.string.schars here to fit it->end_charpos, because
6544 the bidi iterator cannot produce characters out of thin air. */
6545 if (field_width > it->end_charpos - charpos)
6546 it->end_charpos = charpos + field_width;
6547
6548 /* Use the standard display table for displaying strings. */
6549 if (DISP_TABLE_P (Vstandard_display_table))
6550 it->dp = XCHAR_TABLE (Vstandard_display_table);
6551
6552 it->stop_charpos = charpos;
6553 it->prev_stop = charpos;
6554 it->base_level_stop = 0;
6555 if (it->bidi_p)
6556 {
6557 it->bidi_it.first_elt = 1;
6558 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6559 it->bidi_it.disp_pos = -1;
6560 }
6561 if (s == NULL && it->multibyte_p)
6562 {
6563 ptrdiff_t endpos = SCHARS (it->string);
6564 if (endpos > it->end_charpos)
6565 endpos = it->end_charpos;
6566 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6567 it->string);
6568 }
6569 CHECK_IT (it);
6570 }
6571
6572
6573 \f
6574 /***********************************************************************
6575 Iteration
6576 ***********************************************************************/
6577
6578 /* Map enum it_method value to corresponding next_element_from_* function. */
6579
6580 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6581 {
6582 next_element_from_buffer,
6583 next_element_from_display_vector,
6584 next_element_from_string,
6585 next_element_from_c_string,
6586 next_element_from_image,
6587 next_element_from_stretch
6588 };
6589
6590 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6591
6592
6593 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6594 (possibly with the following characters). */
6595
6596 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6597 ((IT)->cmp_it.id >= 0 \
6598 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6599 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6600 END_CHARPOS, (IT)->w, \
6601 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6602 (IT)->string)))
6603
6604
6605 /* Lookup the char-table Vglyphless_char_display for character C (-1
6606 if we want information for no-font case), and return the display
6607 method symbol. By side-effect, update it->what and
6608 it->glyphless_method. This function is called from
6609 get_next_display_element for each character element, and from
6610 x_produce_glyphs when no suitable font was found. */
6611
6612 Lisp_Object
6613 lookup_glyphless_char_display (int c, struct it *it)
6614 {
6615 Lisp_Object glyphless_method = Qnil;
6616
6617 if (CHAR_TABLE_P (Vglyphless_char_display)
6618 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6619 {
6620 if (c >= 0)
6621 {
6622 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6623 if (CONSP (glyphless_method))
6624 glyphless_method = FRAME_WINDOW_P (it->f)
6625 ? XCAR (glyphless_method)
6626 : XCDR (glyphless_method);
6627 }
6628 else
6629 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6630 }
6631
6632 retry:
6633 if (NILP (glyphless_method))
6634 {
6635 if (c >= 0)
6636 /* The default is to display the character by a proper font. */
6637 return Qnil;
6638 /* The default for the no-font case is to display an empty box. */
6639 glyphless_method = Qempty_box;
6640 }
6641 if (EQ (glyphless_method, Qzero_width))
6642 {
6643 if (c >= 0)
6644 return glyphless_method;
6645 /* This method can't be used for the no-font case. */
6646 glyphless_method = Qempty_box;
6647 }
6648 if (EQ (glyphless_method, Qthin_space))
6649 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6650 else if (EQ (glyphless_method, Qempty_box))
6651 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6652 else if (EQ (glyphless_method, Qhex_code))
6653 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6654 else if (STRINGP (glyphless_method))
6655 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6656 else
6657 {
6658 /* Invalid value. We use the default method. */
6659 glyphless_method = Qnil;
6660 goto retry;
6661 }
6662 it->what = IT_GLYPHLESS;
6663 return glyphless_method;
6664 }
6665
6666 /* Merge escape glyph face and cache the result. */
6667
6668 static struct frame *last_escape_glyph_frame = NULL;
6669 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6670 static int last_escape_glyph_merged_face_id = 0;
6671
6672 static int
6673 merge_escape_glyph_face (struct it *it)
6674 {
6675 int face_id;
6676
6677 if (it->f == last_escape_glyph_frame
6678 && it->face_id == last_escape_glyph_face_id)
6679 face_id = last_escape_glyph_merged_face_id;
6680 else
6681 {
6682 /* Merge the `escape-glyph' face into the current face. */
6683 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6684 last_escape_glyph_frame = it->f;
6685 last_escape_glyph_face_id = it->face_id;
6686 last_escape_glyph_merged_face_id = face_id;
6687 }
6688 return face_id;
6689 }
6690
6691 /* Likewise for glyphless glyph face. */
6692
6693 static struct frame *last_glyphless_glyph_frame = NULL;
6694 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6695 static int last_glyphless_glyph_merged_face_id = 0;
6696
6697 int
6698 merge_glyphless_glyph_face (struct it *it)
6699 {
6700 int face_id;
6701
6702 if (it->f == last_glyphless_glyph_frame
6703 && it->face_id == last_glyphless_glyph_face_id)
6704 face_id = last_glyphless_glyph_merged_face_id;
6705 else
6706 {
6707 /* Merge the `glyphless-char' face into the current face. */
6708 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6709 last_glyphless_glyph_frame = it->f;
6710 last_glyphless_glyph_face_id = it->face_id;
6711 last_glyphless_glyph_merged_face_id = face_id;
6712 }
6713 return face_id;
6714 }
6715
6716 /* Load IT's display element fields with information about the next
6717 display element from the current position of IT. Value is zero if
6718 end of buffer (or C string) is reached. */
6719
6720 static int
6721 get_next_display_element (struct it *it)
6722 {
6723 /* Non-zero means that we found a display element. Zero means that
6724 we hit the end of what we iterate over. Performance note: the
6725 function pointer `method' used here turns out to be faster than
6726 using a sequence of if-statements. */
6727 int success_p;
6728
6729 get_next:
6730 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6731
6732 if (it->what == IT_CHARACTER)
6733 {
6734 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6735 and only if (a) the resolved directionality of that character
6736 is R..." */
6737 /* FIXME: Do we need an exception for characters from display
6738 tables? */
6739 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6740 it->c = bidi_mirror_char (it->c);
6741 /* Map via display table or translate control characters.
6742 IT->c, IT->len etc. have been set to the next character by
6743 the function call above. If we have a display table, and it
6744 contains an entry for IT->c, translate it. Don't do this if
6745 IT->c itself comes from a display table, otherwise we could
6746 end up in an infinite recursion. (An alternative could be to
6747 count the recursion depth of this function and signal an
6748 error when a certain maximum depth is reached.) Is it worth
6749 it? */
6750 if (success_p && it->dpvec == NULL)
6751 {
6752 Lisp_Object dv;
6753 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6754 int nonascii_space_p = 0;
6755 int nonascii_hyphen_p = 0;
6756 int c = it->c; /* This is the character to display. */
6757
6758 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6759 {
6760 eassert (SINGLE_BYTE_CHAR_P (c));
6761 if (unibyte_display_via_language_environment)
6762 {
6763 c = DECODE_CHAR (unibyte, c);
6764 if (c < 0)
6765 c = BYTE8_TO_CHAR (it->c);
6766 }
6767 else
6768 c = BYTE8_TO_CHAR (it->c);
6769 }
6770
6771 if (it->dp
6772 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6773 VECTORP (dv)))
6774 {
6775 struct Lisp_Vector *v = XVECTOR (dv);
6776
6777 /* Return the first character from the display table
6778 entry, if not empty. If empty, don't display the
6779 current character. */
6780 if (v->header.size)
6781 {
6782 it->dpvec_char_len = it->len;
6783 it->dpvec = v->contents;
6784 it->dpend = v->contents + v->header.size;
6785 it->current.dpvec_index = 0;
6786 it->dpvec_face_id = -1;
6787 it->saved_face_id = it->face_id;
6788 it->method = GET_FROM_DISPLAY_VECTOR;
6789 it->ellipsis_p = 0;
6790 }
6791 else
6792 {
6793 set_iterator_to_next (it, 0);
6794 }
6795 goto get_next;
6796 }
6797
6798 if (! NILP (lookup_glyphless_char_display (c, it)))
6799 {
6800 if (it->what == IT_GLYPHLESS)
6801 goto done;
6802 /* Don't display this character. */
6803 set_iterator_to_next (it, 0);
6804 goto get_next;
6805 }
6806
6807 /* If `nobreak-char-display' is non-nil, we display
6808 non-ASCII spaces and hyphens specially. */
6809 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6810 {
6811 if (c == 0xA0)
6812 nonascii_space_p = true;
6813 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6814 nonascii_hyphen_p = true;
6815 }
6816
6817 /* Translate control characters into `\003' or `^C' form.
6818 Control characters coming from a display table entry are
6819 currently not translated because we use IT->dpvec to hold
6820 the translation. This could easily be changed but I
6821 don't believe that it is worth doing.
6822
6823 The characters handled by `nobreak-char-display' must be
6824 translated too.
6825
6826 Non-printable characters and raw-byte characters are also
6827 translated to octal form. */
6828 if (((c < ' ' || c == 127) /* ASCII control chars. */
6829 ? (it->area != TEXT_AREA
6830 /* In mode line, treat \n, \t like other crl chars. */
6831 || (c != '\t'
6832 && it->glyph_row
6833 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6834 || (c != '\n' && c != '\t'))
6835 : (nonascii_space_p
6836 || nonascii_hyphen_p
6837 || CHAR_BYTE8_P (c)
6838 || ! CHAR_PRINTABLE_P (c))))
6839 {
6840 /* C is a control character, non-ASCII space/hyphen,
6841 raw-byte, or a non-printable character which must be
6842 displayed either as '\003' or as `^C' where the '\\'
6843 and '^' can be defined in the display table. Fill
6844 IT->ctl_chars with glyphs for what we have to
6845 display. Then, set IT->dpvec to these glyphs. */
6846 Lisp_Object gc;
6847 int ctl_len;
6848 int face_id;
6849 int lface_id = 0;
6850 int escape_glyph;
6851
6852 /* Handle control characters with ^. */
6853
6854 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6855 {
6856 int g;
6857
6858 g = '^'; /* default glyph for Control */
6859 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6860 if (it->dp
6861 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6862 {
6863 g = GLYPH_CODE_CHAR (gc);
6864 lface_id = GLYPH_CODE_FACE (gc);
6865 }
6866
6867 face_id = (lface_id
6868 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6869 : merge_escape_glyph_face (it));
6870
6871 XSETINT (it->ctl_chars[0], g);
6872 XSETINT (it->ctl_chars[1], c ^ 0100);
6873 ctl_len = 2;
6874 goto display_control;
6875 }
6876
6877 /* Handle non-ascii space in the mode where it only gets
6878 highlighting. */
6879
6880 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6881 {
6882 /* Merge `nobreak-space' into the current face. */
6883 face_id = merge_faces (it->f, Qnobreak_space, 0,
6884 it->face_id);
6885 XSETINT (it->ctl_chars[0], ' ');
6886 ctl_len = 1;
6887 goto display_control;
6888 }
6889
6890 /* Handle sequences that start with the "escape glyph". */
6891
6892 /* the default escape glyph is \. */
6893 escape_glyph = '\\';
6894
6895 if (it->dp
6896 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6897 {
6898 escape_glyph = GLYPH_CODE_CHAR (gc);
6899 lface_id = GLYPH_CODE_FACE (gc);
6900 }
6901
6902 face_id = (lface_id
6903 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6904 : merge_escape_glyph_face (it));
6905
6906 /* Draw non-ASCII hyphen with just highlighting: */
6907
6908 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6909 {
6910 XSETINT (it->ctl_chars[0], '-');
6911 ctl_len = 1;
6912 goto display_control;
6913 }
6914
6915 /* Draw non-ASCII space/hyphen with escape glyph: */
6916
6917 if (nonascii_space_p || nonascii_hyphen_p)
6918 {
6919 XSETINT (it->ctl_chars[0], escape_glyph);
6920 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6921 ctl_len = 2;
6922 goto display_control;
6923 }
6924
6925 {
6926 char str[10];
6927 int len, i;
6928
6929 if (CHAR_BYTE8_P (c))
6930 /* Display \200 instead of \17777600. */
6931 c = CHAR_TO_BYTE8 (c);
6932 len = sprintf (str, "%03o", c);
6933
6934 XSETINT (it->ctl_chars[0], escape_glyph);
6935 for (i = 0; i < len; i++)
6936 XSETINT (it->ctl_chars[i + 1], str[i]);
6937 ctl_len = len + 1;
6938 }
6939
6940 display_control:
6941 /* Set up IT->dpvec and return first character from it. */
6942 it->dpvec_char_len = it->len;
6943 it->dpvec = it->ctl_chars;
6944 it->dpend = it->dpvec + ctl_len;
6945 it->current.dpvec_index = 0;
6946 it->dpvec_face_id = face_id;
6947 it->saved_face_id = it->face_id;
6948 it->method = GET_FROM_DISPLAY_VECTOR;
6949 it->ellipsis_p = 0;
6950 goto get_next;
6951 }
6952 it->char_to_display = c;
6953 }
6954 else if (success_p)
6955 {
6956 it->char_to_display = it->c;
6957 }
6958 }
6959
6960 #ifdef HAVE_WINDOW_SYSTEM
6961 /* Adjust face id for a multibyte character. There are no multibyte
6962 character in unibyte text. */
6963 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6964 && it->multibyte_p
6965 && success_p
6966 && FRAME_WINDOW_P (it->f))
6967 {
6968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6969
6970 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6971 {
6972 /* Automatic composition with glyph-string. */
6973 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6974
6975 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6976 }
6977 else
6978 {
6979 ptrdiff_t pos = (it->s ? -1
6980 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6981 : IT_CHARPOS (*it));
6982 int c;
6983
6984 if (it->what == IT_CHARACTER)
6985 c = it->char_to_display;
6986 else
6987 {
6988 struct composition *cmp = composition_table[it->cmp_it.id];
6989 int i;
6990
6991 c = ' ';
6992 for (i = 0; i < cmp->glyph_len; i++)
6993 /* TAB in a composition means display glyphs with
6994 padding space on the left or right. */
6995 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6996 break;
6997 }
6998 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6999 }
7000 }
7001 #endif /* HAVE_WINDOW_SYSTEM */
7002
7003 done:
7004 /* Is this character the last one of a run of characters with
7005 box? If yes, set IT->end_of_box_run_p to 1. */
7006 if (it->face_box_p
7007 && it->s == NULL)
7008 {
7009 if (it->method == GET_FROM_STRING && it->sp)
7010 {
7011 int face_id = underlying_face_id (it);
7012 struct face *face = FACE_FROM_ID (it->f, face_id);
7013
7014 if (face)
7015 {
7016 if (face->box == FACE_NO_BOX)
7017 {
7018 /* If the box comes from face properties in a
7019 display string, check faces in that string. */
7020 int string_face_id = face_after_it_pos (it);
7021 it->end_of_box_run_p
7022 = (FACE_FROM_ID (it->f, string_face_id)->box
7023 == FACE_NO_BOX);
7024 }
7025 /* Otherwise, the box comes from the underlying face.
7026 If this is the last string character displayed, check
7027 the next buffer location. */
7028 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7029 && (it->current.overlay_string_index
7030 == it->n_overlay_strings - 1))
7031 {
7032 ptrdiff_t ignore;
7033 int next_face_id;
7034 struct text_pos pos = it->current.pos;
7035 INC_TEXT_POS (pos, it->multibyte_p);
7036
7037 next_face_id = face_at_buffer_position
7038 (it->w, CHARPOS (pos), &ignore,
7039 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7040 -1);
7041 it->end_of_box_run_p
7042 = (FACE_FROM_ID (it->f, next_face_id)->box
7043 == FACE_NO_BOX);
7044 }
7045 }
7046 }
7047 /* next_element_from_display_vector sets this flag according to
7048 faces of the display vector glyphs, see there. */
7049 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7050 {
7051 int face_id = face_after_it_pos (it);
7052 it->end_of_box_run_p
7053 = (face_id != it->face_id
7054 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7055 }
7056 }
7057 /* If we reached the end of the object we've been iterating (e.g., a
7058 display string or an overlay string), and there's something on
7059 IT->stack, proceed with what's on the stack. It doesn't make
7060 sense to return zero if there's unprocessed stuff on the stack,
7061 because otherwise that stuff will never be displayed. */
7062 if (!success_p && it->sp > 0)
7063 {
7064 set_iterator_to_next (it, 0);
7065 success_p = get_next_display_element (it);
7066 }
7067
7068 /* Value is 0 if end of buffer or string reached. */
7069 return success_p;
7070 }
7071
7072
7073 /* Move IT to the next display element.
7074
7075 RESEAT_P non-zero means if called on a newline in buffer text,
7076 skip to the next visible line start.
7077
7078 Functions get_next_display_element and set_iterator_to_next are
7079 separate because I find this arrangement easier to handle than a
7080 get_next_display_element function that also increments IT's
7081 position. The way it is we can first look at an iterator's current
7082 display element, decide whether it fits on a line, and if it does,
7083 increment the iterator position. The other way around we probably
7084 would either need a flag indicating whether the iterator has to be
7085 incremented the next time, or we would have to implement a
7086 decrement position function which would not be easy to write. */
7087
7088 void
7089 set_iterator_to_next (struct it *it, int reseat_p)
7090 {
7091 /* Reset flags indicating start and end of a sequence of characters
7092 with box. Reset them at the start of this function because
7093 moving the iterator to a new position might set them. */
7094 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7095
7096 switch (it->method)
7097 {
7098 case GET_FROM_BUFFER:
7099 /* The current display element of IT is a character from
7100 current_buffer. Advance in the buffer, and maybe skip over
7101 invisible lines that are so because of selective display. */
7102 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7103 reseat_at_next_visible_line_start (it, 0);
7104 else if (it->cmp_it.id >= 0)
7105 {
7106 /* We are currently getting glyphs from a composition. */
7107 int i;
7108
7109 if (! it->bidi_p)
7110 {
7111 IT_CHARPOS (*it) += it->cmp_it.nchars;
7112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7113 if (it->cmp_it.to < it->cmp_it.nglyphs)
7114 {
7115 it->cmp_it.from = it->cmp_it.to;
7116 }
7117 else
7118 {
7119 it->cmp_it.id = -1;
7120 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7121 IT_BYTEPOS (*it),
7122 it->end_charpos, Qnil);
7123 }
7124 }
7125 else if (! it->cmp_it.reversed_p)
7126 {
7127 /* Composition created while scanning forward. */
7128 /* Update IT's char/byte positions to point to the first
7129 character of the next grapheme cluster, or to the
7130 character visually after the current composition. */
7131 for (i = 0; i < it->cmp_it.nchars; i++)
7132 bidi_move_to_visually_next (&it->bidi_it);
7133 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7134 IT_CHARPOS (*it) = it->bidi_it.charpos;
7135
7136 if (it->cmp_it.to < it->cmp_it.nglyphs)
7137 {
7138 /* Proceed to the next grapheme cluster. */
7139 it->cmp_it.from = it->cmp_it.to;
7140 }
7141 else
7142 {
7143 /* No more grapheme clusters in this composition.
7144 Find the next stop position. */
7145 ptrdiff_t stop = it->end_charpos;
7146 if (it->bidi_it.scan_dir < 0)
7147 /* Now we are scanning backward and don't know
7148 where to stop. */
7149 stop = -1;
7150 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7151 IT_BYTEPOS (*it), stop, Qnil);
7152 }
7153 }
7154 else
7155 {
7156 /* Composition created while scanning backward. */
7157 /* Update IT's char/byte positions to point to the last
7158 character of the previous grapheme cluster, or the
7159 character visually after the current composition. */
7160 for (i = 0; i < it->cmp_it.nchars; i++)
7161 bidi_move_to_visually_next (&it->bidi_it);
7162 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7163 IT_CHARPOS (*it) = it->bidi_it.charpos;
7164 if (it->cmp_it.from > 0)
7165 {
7166 /* Proceed to the previous grapheme cluster. */
7167 it->cmp_it.to = it->cmp_it.from;
7168 }
7169 else
7170 {
7171 /* No more grapheme clusters in this composition.
7172 Find the next stop position. */
7173 ptrdiff_t stop = it->end_charpos;
7174 if (it->bidi_it.scan_dir < 0)
7175 /* Now we are scanning backward and don't know
7176 where to stop. */
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7179 IT_BYTEPOS (*it), stop, Qnil);
7180 }
7181 }
7182 }
7183 else
7184 {
7185 eassert (it->len != 0);
7186
7187 if (!it->bidi_p)
7188 {
7189 IT_BYTEPOS (*it) += it->len;
7190 IT_CHARPOS (*it) += 1;
7191 }
7192 else
7193 {
7194 int prev_scan_dir = it->bidi_it.scan_dir;
7195 /* If this is a new paragraph, determine its base
7196 direction (a.k.a. its base embedding level). */
7197 if (it->bidi_it.new_paragraph)
7198 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7199 bidi_move_to_visually_next (&it->bidi_it);
7200 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7201 IT_CHARPOS (*it) = it->bidi_it.charpos;
7202 if (prev_scan_dir != it->bidi_it.scan_dir)
7203 {
7204 /* As the scan direction was changed, we must
7205 re-compute the stop position for composition. */
7206 ptrdiff_t stop = it->end_charpos;
7207 if (it->bidi_it.scan_dir < 0)
7208 stop = -1;
7209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7210 IT_BYTEPOS (*it), stop, Qnil);
7211 }
7212 }
7213 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7214 }
7215 break;
7216
7217 case GET_FROM_C_STRING:
7218 /* Current display element of IT is from a C string. */
7219 if (!it->bidi_p
7220 /* If the string position is beyond string's end, it means
7221 next_element_from_c_string is padding the string with
7222 blanks, in which case we bypass the bidi iterator,
7223 because it cannot deal with such virtual characters. */
7224 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7225 {
7226 IT_BYTEPOS (*it) += it->len;
7227 IT_CHARPOS (*it) += 1;
7228 }
7229 else
7230 {
7231 bidi_move_to_visually_next (&it->bidi_it);
7232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7233 IT_CHARPOS (*it) = it->bidi_it.charpos;
7234 }
7235 break;
7236
7237 case GET_FROM_DISPLAY_VECTOR:
7238 /* Current display element of IT is from a display table entry.
7239 Advance in the display table definition. Reset it to null if
7240 end reached, and continue with characters from buffers/
7241 strings. */
7242 ++it->current.dpvec_index;
7243
7244 /* Restore face of the iterator to what they were before the
7245 display vector entry (these entries may contain faces). */
7246 it->face_id = it->saved_face_id;
7247
7248 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7249 {
7250 int recheck_faces = it->ellipsis_p;
7251
7252 if (it->s)
7253 it->method = GET_FROM_C_STRING;
7254 else if (STRINGP (it->string))
7255 it->method = GET_FROM_STRING;
7256 else
7257 {
7258 it->method = GET_FROM_BUFFER;
7259 it->object = it->w->contents;
7260 }
7261
7262 it->dpvec = NULL;
7263 it->current.dpvec_index = -1;
7264
7265 /* Skip over characters which were displayed via IT->dpvec. */
7266 if (it->dpvec_char_len < 0)
7267 reseat_at_next_visible_line_start (it, 1);
7268 else if (it->dpvec_char_len > 0)
7269 {
7270 if (it->method == GET_FROM_STRING
7271 && it->current.overlay_string_index >= 0
7272 && it->n_overlay_strings > 0)
7273 it->ignore_overlay_strings_at_pos_p = true;
7274 it->len = it->dpvec_char_len;
7275 set_iterator_to_next (it, reseat_p);
7276 }
7277
7278 /* Maybe recheck faces after display vector. */
7279 if (recheck_faces)
7280 it->stop_charpos = IT_CHARPOS (*it);
7281 }
7282 break;
7283
7284 case GET_FROM_STRING:
7285 /* Current display element is a character from a Lisp string. */
7286 eassert (it->s == NULL && STRINGP (it->string));
7287 /* Don't advance past string end. These conditions are true
7288 when set_iterator_to_next is called at the end of
7289 get_next_display_element, in which case the Lisp string is
7290 already exhausted, and all we want is pop the iterator
7291 stack. */
7292 if (it->current.overlay_string_index >= 0)
7293 {
7294 /* This is an overlay string, so there's no padding with
7295 spaces, and the number of characters in the string is
7296 where the string ends. */
7297 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7298 goto consider_string_end;
7299 }
7300 else
7301 {
7302 /* Not an overlay string. There could be padding, so test
7303 against it->end_charpos. */
7304 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7305 goto consider_string_end;
7306 }
7307 if (it->cmp_it.id >= 0)
7308 {
7309 int i;
7310
7311 if (! it->bidi_p)
7312 {
7313 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7314 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7315 if (it->cmp_it.to < it->cmp_it.nglyphs)
7316 it->cmp_it.from = it->cmp_it.to;
7317 else
7318 {
7319 it->cmp_it.id = -1;
7320 composition_compute_stop_pos (&it->cmp_it,
7321 IT_STRING_CHARPOS (*it),
7322 IT_STRING_BYTEPOS (*it),
7323 it->end_charpos, it->string);
7324 }
7325 }
7326 else if (! it->cmp_it.reversed_p)
7327 {
7328 for (i = 0; i < it->cmp_it.nchars; i++)
7329 bidi_move_to_visually_next (&it->bidi_it);
7330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7331 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7332
7333 if (it->cmp_it.to < it->cmp_it.nglyphs)
7334 it->cmp_it.from = it->cmp_it.to;
7335 else
7336 {
7337 ptrdiff_t stop = it->end_charpos;
7338 if (it->bidi_it.scan_dir < 0)
7339 stop = -1;
7340 composition_compute_stop_pos (&it->cmp_it,
7341 IT_STRING_CHARPOS (*it),
7342 IT_STRING_BYTEPOS (*it), stop,
7343 it->string);
7344 }
7345 }
7346 else
7347 {
7348 for (i = 0; i < it->cmp_it.nchars; i++)
7349 bidi_move_to_visually_next (&it->bidi_it);
7350 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7352 if (it->cmp_it.from > 0)
7353 it->cmp_it.to = it->cmp_it.from;
7354 else
7355 {
7356 ptrdiff_t stop = it->end_charpos;
7357 if (it->bidi_it.scan_dir < 0)
7358 stop = -1;
7359 composition_compute_stop_pos (&it->cmp_it,
7360 IT_STRING_CHARPOS (*it),
7361 IT_STRING_BYTEPOS (*it), stop,
7362 it->string);
7363 }
7364 }
7365 }
7366 else
7367 {
7368 if (!it->bidi_p
7369 /* If the string position is beyond string's end, it
7370 means next_element_from_string is padding the string
7371 with blanks, in which case we bypass the bidi
7372 iterator, because it cannot deal with such virtual
7373 characters. */
7374 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7375 {
7376 IT_STRING_BYTEPOS (*it) += it->len;
7377 IT_STRING_CHARPOS (*it) += 1;
7378 }
7379 else
7380 {
7381 int prev_scan_dir = it->bidi_it.scan_dir;
7382
7383 bidi_move_to_visually_next (&it->bidi_it);
7384 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7385 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7386 if (prev_scan_dir != it->bidi_it.scan_dir)
7387 {
7388 ptrdiff_t stop = it->end_charpos;
7389
7390 if (it->bidi_it.scan_dir < 0)
7391 stop = -1;
7392 composition_compute_stop_pos (&it->cmp_it,
7393 IT_STRING_CHARPOS (*it),
7394 IT_STRING_BYTEPOS (*it), stop,
7395 it->string);
7396 }
7397 }
7398 }
7399
7400 consider_string_end:
7401
7402 if (it->current.overlay_string_index >= 0)
7403 {
7404 /* IT->string is an overlay string. Advance to the
7405 next, if there is one. */
7406 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7407 {
7408 it->ellipsis_p = 0;
7409 next_overlay_string (it);
7410 if (it->ellipsis_p)
7411 setup_for_ellipsis (it, 0);
7412 }
7413 }
7414 else
7415 {
7416 /* IT->string is not an overlay string. If we reached
7417 its end, and there is something on IT->stack, proceed
7418 with what is on the stack. This can be either another
7419 string, this time an overlay string, or a buffer. */
7420 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7421 && it->sp > 0)
7422 {
7423 pop_it (it);
7424 if (it->method == GET_FROM_STRING)
7425 goto consider_string_end;
7426 }
7427 }
7428 break;
7429
7430 case GET_FROM_IMAGE:
7431 case GET_FROM_STRETCH:
7432 /* The position etc with which we have to proceed are on
7433 the stack. The position may be at the end of a string,
7434 if the `display' property takes up the whole string. */
7435 eassert (it->sp > 0);
7436 pop_it (it);
7437 if (it->method == GET_FROM_STRING)
7438 goto consider_string_end;
7439 break;
7440
7441 default:
7442 /* There are no other methods defined, so this should be a bug. */
7443 emacs_abort ();
7444 }
7445
7446 eassert (it->method != GET_FROM_STRING
7447 || (STRINGP (it->string)
7448 && IT_STRING_CHARPOS (*it) >= 0));
7449 }
7450
7451 /* Load IT's display element fields with information about the next
7452 display element which comes from a display table entry or from the
7453 result of translating a control character to one of the forms `^C'
7454 or `\003'.
7455
7456 IT->dpvec holds the glyphs to return as characters.
7457 IT->saved_face_id holds the face id before the display vector--it
7458 is restored into IT->face_id in set_iterator_to_next. */
7459
7460 static int
7461 next_element_from_display_vector (struct it *it)
7462 {
7463 Lisp_Object gc;
7464 int prev_face_id = it->face_id;
7465 int next_face_id;
7466
7467 /* Precondition. */
7468 eassert (it->dpvec && it->current.dpvec_index >= 0);
7469
7470 it->face_id = it->saved_face_id;
7471
7472 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7473 That seemed totally bogus - so I changed it... */
7474 gc = it->dpvec[it->current.dpvec_index];
7475
7476 if (GLYPH_CODE_P (gc))
7477 {
7478 struct face *this_face, *prev_face, *next_face;
7479
7480 it->c = GLYPH_CODE_CHAR (gc);
7481 it->len = CHAR_BYTES (it->c);
7482
7483 /* The entry may contain a face id to use. Such a face id is
7484 the id of a Lisp face, not a realized face. A face id of
7485 zero means no face is specified. */
7486 if (it->dpvec_face_id >= 0)
7487 it->face_id = it->dpvec_face_id;
7488 else
7489 {
7490 int lface_id = GLYPH_CODE_FACE (gc);
7491 if (lface_id > 0)
7492 it->face_id = merge_faces (it->f, Qt, lface_id,
7493 it->saved_face_id);
7494 }
7495
7496 /* Glyphs in the display vector could have the box face, so we
7497 need to set the related flags in the iterator, as
7498 appropriate. */
7499 this_face = FACE_FROM_ID (it->f, it->face_id);
7500 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7501
7502 /* Is this character the first character of a box-face run? */
7503 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7504 && (!prev_face
7505 || prev_face->box == FACE_NO_BOX));
7506
7507 /* For the last character of the box-face run, we need to look
7508 either at the next glyph from the display vector, or at the
7509 face we saw before the display vector. */
7510 next_face_id = it->saved_face_id;
7511 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7512 {
7513 if (it->dpvec_face_id >= 0)
7514 next_face_id = it->dpvec_face_id;
7515 else
7516 {
7517 int lface_id =
7518 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7519
7520 if (lface_id > 0)
7521 next_face_id = merge_faces (it->f, Qt, lface_id,
7522 it->saved_face_id);
7523 }
7524 }
7525 next_face = FACE_FROM_ID (it->f, next_face_id);
7526 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7527 && (!next_face
7528 || next_face->box == FACE_NO_BOX));
7529 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7530 }
7531 else
7532 /* Display table entry is invalid. Return a space. */
7533 it->c = ' ', it->len = 1;
7534
7535 /* Don't change position and object of the iterator here. They are
7536 still the values of the character that had this display table
7537 entry or was translated, and that's what we want. */
7538 it->what = IT_CHARACTER;
7539 return 1;
7540 }
7541
7542 /* Get the first element of string/buffer in the visual order, after
7543 being reseated to a new position in a string or a buffer. */
7544 static void
7545 get_visually_first_element (struct it *it)
7546 {
7547 int string_p = STRINGP (it->string) || it->s;
7548 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7549 ptrdiff_t bob = (string_p ? 0 : BEGV);
7550
7551 if (STRINGP (it->string))
7552 {
7553 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7554 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7555 }
7556 else
7557 {
7558 it->bidi_it.charpos = IT_CHARPOS (*it);
7559 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7560 }
7561
7562 if (it->bidi_it.charpos == eob)
7563 {
7564 /* Nothing to do, but reset the FIRST_ELT flag, like
7565 bidi_paragraph_init does, because we are not going to
7566 call it. */
7567 it->bidi_it.first_elt = 0;
7568 }
7569 else if (it->bidi_it.charpos == bob
7570 || (!string_p
7571 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7572 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7573 {
7574 /* If we are at the beginning of a line/string, we can produce
7575 the next element right away. */
7576 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7577 bidi_move_to_visually_next (&it->bidi_it);
7578 }
7579 else
7580 {
7581 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7582
7583 /* We need to prime the bidi iterator starting at the line's or
7584 string's beginning, before we will be able to produce the
7585 next element. */
7586 if (string_p)
7587 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7588 else
7589 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7590 IT_BYTEPOS (*it), -1,
7591 &it->bidi_it.bytepos);
7592 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7593 do
7594 {
7595 /* Now return to buffer/string position where we were asked
7596 to get the next display element, and produce that. */
7597 bidi_move_to_visually_next (&it->bidi_it);
7598 }
7599 while (it->bidi_it.bytepos != orig_bytepos
7600 && it->bidi_it.charpos < eob);
7601 }
7602
7603 /* Adjust IT's position information to where we ended up. */
7604 if (STRINGP (it->string))
7605 {
7606 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7607 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7608 }
7609 else
7610 {
7611 IT_CHARPOS (*it) = it->bidi_it.charpos;
7612 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7613 }
7614
7615 if (STRINGP (it->string) || !it->s)
7616 {
7617 ptrdiff_t stop, charpos, bytepos;
7618
7619 if (STRINGP (it->string))
7620 {
7621 eassert (!it->s);
7622 stop = SCHARS (it->string);
7623 if (stop > it->end_charpos)
7624 stop = it->end_charpos;
7625 charpos = IT_STRING_CHARPOS (*it);
7626 bytepos = IT_STRING_BYTEPOS (*it);
7627 }
7628 else
7629 {
7630 stop = it->end_charpos;
7631 charpos = IT_CHARPOS (*it);
7632 bytepos = IT_BYTEPOS (*it);
7633 }
7634 if (it->bidi_it.scan_dir < 0)
7635 stop = -1;
7636 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7637 it->string);
7638 }
7639 }
7640
7641 /* Load IT with the next display element from Lisp string IT->string.
7642 IT->current.string_pos is the current position within the string.
7643 If IT->current.overlay_string_index >= 0, the Lisp string is an
7644 overlay string. */
7645
7646 static int
7647 next_element_from_string (struct it *it)
7648 {
7649 struct text_pos position;
7650
7651 eassert (STRINGP (it->string));
7652 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7653 eassert (IT_STRING_CHARPOS (*it) >= 0);
7654 position = it->current.string_pos;
7655
7656 /* With bidi reordering, the character to display might not be the
7657 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7658 that we were reseat()ed to a new string, whose paragraph
7659 direction is not known. */
7660 if (it->bidi_p && it->bidi_it.first_elt)
7661 {
7662 get_visually_first_element (it);
7663 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7664 }
7665
7666 /* Time to check for invisible text? */
7667 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7668 {
7669 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7670 {
7671 if (!(!it->bidi_p
7672 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7673 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7674 {
7675 /* With bidi non-linear iteration, we could find
7676 ourselves far beyond the last computed stop_charpos,
7677 with several other stop positions in between that we
7678 missed. Scan them all now, in buffer's logical
7679 order, until we find and handle the last stop_charpos
7680 that precedes our current position. */
7681 handle_stop_backwards (it, it->stop_charpos);
7682 return GET_NEXT_DISPLAY_ELEMENT (it);
7683 }
7684 else
7685 {
7686 if (it->bidi_p)
7687 {
7688 /* Take note of the stop position we just moved
7689 across, for when we will move back across it. */
7690 it->prev_stop = it->stop_charpos;
7691 /* If we are at base paragraph embedding level, take
7692 note of the last stop position seen at this
7693 level. */
7694 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7695 it->base_level_stop = it->stop_charpos;
7696 }
7697 handle_stop (it);
7698
7699 /* Since a handler may have changed IT->method, we must
7700 recurse here. */
7701 return GET_NEXT_DISPLAY_ELEMENT (it);
7702 }
7703 }
7704 else if (it->bidi_p
7705 /* If we are before prev_stop, we may have overstepped
7706 on our way backwards a stop_pos, and if so, we need
7707 to handle that stop_pos. */
7708 && IT_STRING_CHARPOS (*it) < it->prev_stop
7709 /* We can sometimes back up for reasons that have nothing
7710 to do with bidi reordering. E.g., compositions. The
7711 code below is only needed when we are above the base
7712 embedding level, so test for that explicitly. */
7713 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7714 {
7715 /* If we lost track of base_level_stop, we have no better
7716 place for handle_stop_backwards to start from than string
7717 beginning. This happens, e.g., when we were reseated to
7718 the previous screenful of text by vertical-motion. */
7719 if (it->base_level_stop <= 0
7720 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7721 it->base_level_stop = 0;
7722 handle_stop_backwards (it, it->base_level_stop);
7723 return GET_NEXT_DISPLAY_ELEMENT (it);
7724 }
7725 }
7726
7727 if (it->current.overlay_string_index >= 0)
7728 {
7729 /* Get the next character from an overlay string. In overlay
7730 strings, there is no field width or padding with spaces to
7731 do. */
7732 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7733 {
7734 it->what = IT_EOB;
7735 return 0;
7736 }
7737 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7738 IT_STRING_BYTEPOS (*it),
7739 it->bidi_it.scan_dir < 0
7740 ? -1
7741 : SCHARS (it->string))
7742 && next_element_from_composition (it))
7743 {
7744 return 1;
7745 }
7746 else if (STRING_MULTIBYTE (it->string))
7747 {
7748 const unsigned char *s = (SDATA (it->string)
7749 + IT_STRING_BYTEPOS (*it));
7750 it->c = string_char_and_length (s, &it->len);
7751 }
7752 else
7753 {
7754 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7755 it->len = 1;
7756 }
7757 }
7758 else
7759 {
7760 /* Get the next character from a Lisp string that is not an
7761 overlay string. Such strings come from the mode line, for
7762 example. We may have to pad with spaces, or truncate the
7763 string. See also next_element_from_c_string. */
7764 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7765 {
7766 it->what = IT_EOB;
7767 return 0;
7768 }
7769 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7770 {
7771 /* Pad with spaces. */
7772 it->c = ' ', it->len = 1;
7773 CHARPOS (position) = BYTEPOS (position) = -1;
7774 }
7775 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7776 IT_STRING_BYTEPOS (*it),
7777 it->bidi_it.scan_dir < 0
7778 ? -1
7779 : it->string_nchars)
7780 && next_element_from_composition (it))
7781 {
7782 return 1;
7783 }
7784 else if (STRING_MULTIBYTE (it->string))
7785 {
7786 const unsigned char *s = (SDATA (it->string)
7787 + IT_STRING_BYTEPOS (*it));
7788 it->c = string_char_and_length (s, &it->len);
7789 }
7790 else
7791 {
7792 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7793 it->len = 1;
7794 }
7795 }
7796
7797 /* Record what we have and where it came from. */
7798 it->what = IT_CHARACTER;
7799 it->object = it->string;
7800 it->position = position;
7801 return 1;
7802 }
7803
7804
7805 /* Load IT with next display element from C string IT->s.
7806 IT->string_nchars is the maximum number of characters to return
7807 from the string. IT->end_charpos may be greater than
7808 IT->string_nchars when this function is called, in which case we
7809 may have to return padding spaces. Value is zero if end of string
7810 reached, including padding spaces. */
7811
7812 static int
7813 next_element_from_c_string (struct it *it)
7814 {
7815 bool success_p = true;
7816
7817 eassert (it->s);
7818 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7819 it->what = IT_CHARACTER;
7820 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7821 it->object = Qnil;
7822
7823 /* With bidi reordering, the character to display might not be the
7824 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7825 we were reseated to a new string, whose paragraph direction is
7826 not known. */
7827 if (it->bidi_p && it->bidi_it.first_elt)
7828 get_visually_first_element (it);
7829
7830 /* IT's position can be greater than IT->string_nchars in case a
7831 field width or precision has been specified when the iterator was
7832 initialized. */
7833 if (IT_CHARPOS (*it) >= it->end_charpos)
7834 {
7835 /* End of the game. */
7836 it->what = IT_EOB;
7837 success_p = 0;
7838 }
7839 else if (IT_CHARPOS (*it) >= it->string_nchars)
7840 {
7841 /* Pad with spaces. */
7842 it->c = ' ', it->len = 1;
7843 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7844 }
7845 else if (it->multibyte_p)
7846 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7847 else
7848 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7849
7850 return success_p;
7851 }
7852
7853
7854 /* Set up IT to return characters from an ellipsis, if appropriate.
7855 The definition of the ellipsis glyphs may come from a display table
7856 entry. This function fills IT with the first glyph from the
7857 ellipsis if an ellipsis is to be displayed. */
7858
7859 static int
7860 next_element_from_ellipsis (struct it *it)
7861 {
7862 if (it->selective_display_ellipsis_p)
7863 setup_for_ellipsis (it, it->len);
7864 else
7865 {
7866 /* The face at the current position may be different from the
7867 face we find after the invisible text. Remember what it
7868 was in IT->saved_face_id, and signal that it's there by
7869 setting face_before_selective_p. */
7870 it->saved_face_id = it->face_id;
7871 it->method = GET_FROM_BUFFER;
7872 it->object = it->w->contents;
7873 reseat_at_next_visible_line_start (it, 1);
7874 it->face_before_selective_p = true;
7875 }
7876
7877 return GET_NEXT_DISPLAY_ELEMENT (it);
7878 }
7879
7880
7881 /* Deliver an image display element. The iterator IT is already
7882 filled with image information (done in handle_display_prop). Value
7883 is always 1. */
7884
7885
7886 static int
7887 next_element_from_image (struct it *it)
7888 {
7889 it->what = IT_IMAGE;
7890 it->ignore_overlay_strings_at_pos_p = 0;
7891 return 1;
7892 }
7893
7894
7895 /* Fill iterator IT with next display element from a stretch glyph
7896 property. IT->object is the value of the text property. Value is
7897 always 1. */
7898
7899 static int
7900 next_element_from_stretch (struct it *it)
7901 {
7902 it->what = IT_STRETCH;
7903 return 1;
7904 }
7905
7906 /* Scan backwards from IT's current position until we find a stop
7907 position, or until BEGV. This is called when we find ourself
7908 before both the last known prev_stop and base_level_stop while
7909 reordering bidirectional text. */
7910
7911 static void
7912 compute_stop_pos_backwards (struct it *it)
7913 {
7914 const int SCAN_BACK_LIMIT = 1000;
7915 struct text_pos pos;
7916 struct display_pos save_current = it->current;
7917 struct text_pos save_position = it->position;
7918 ptrdiff_t charpos = IT_CHARPOS (*it);
7919 ptrdiff_t where_we_are = charpos;
7920 ptrdiff_t save_stop_pos = it->stop_charpos;
7921 ptrdiff_t save_end_pos = it->end_charpos;
7922
7923 eassert (NILP (it->string) && !it->s);
7924 eassert (it->bidi_p);
7925 it->bidi_p = 0;
7926 do
7927 {
7928 it->end_charpos = min (charpos + 1, ZV);
7929 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7930 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7931 reseat_1 (it, pos, 0);
7932 compute_stop_pos (it);
7933 /* We must advance forward, right? */
7934 if (it->stop_charpos <= charpos)
7935 emacs_abort ();
7936 }
7937 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7938
7939 if (it->stop_charpos <= where_we_are)
7940 it->prev_stop = it->stop_charpos;
7941 else
7942 it->prev_stop = BEGV;
7943 it->bidi_p = true;
7944 it->current = save_current;
7945 it->position = save_position;
7946 it->stop_charpos = save_stop_pos;
7947 it->end_charpos = save_end_pos;
7948 }
7949
7950 /* Scan forward from CHARPOS in the current buffer/string, until we
7951 find a stop position > current IT's position. Then handle the stop
7952 position before that. This is called when we bump into a stop
7953 position while reordering bidirectional text. CHARPOS should be
7954 the last previously processed stop_pos (or BEGV/0, if none were
7955 processed yet) whose position is less that IT's current
7956 position. */
7957
7958 static void
7959 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7960 {
7961 int bufp = !STRINGP (it->string);
7962 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7963 struct display_pos save_current = it->current;
7964 struct text_pos save_position = it->position;
7965 struct text_pos pos1;
7966 ptrdiff_t next_stop;
7967
7968 /* Scan in strict logical order. */
7969 eassert (it->bidi_p);
7970 it->bidi_p = 0;
7971 do
7972 {
7973 it->prev_stop = charpos;
7974 if (bufp)
7975 {
7976 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7977 reseat_1 (it, pos1, 0);
7978 }
7979 else
7980 it->current.string_pos = string_pos (charpos, it->string);
7981 compute_stop_pos (it);
7982 /* We must advance forward, right? */
7983 if (it->stop_charpos <= it->prev_stop)
7984 emacs_abort ();
7985 charpos = it->stop_charpos;
7986 }
7987 while (charpos <= where_we_are);
7988
7989 it->bidi_p = true;
7990 it->current = save_current;
7991 it->position = save_position;
7992 next_stop = it->stop_charpos;
7993 it->stop_charpos = it->prev_stop;
7994 handle_stop (it);
7995 it->stop_charpos = next_stop;
7996 }
7997
7998 /* Load IT with the next display element from current_buffer. Value
7999 is zero if end of buffer reached. IT->stop_charpos is the next
8000 position at which to stop and check for text properties or buffer
8001 end. */
8002
8003 static int
8004 next_element_from_buffer (struct it *it)
8005 {
8006 bool success_p = true;
8007
8008 eassert (IT_CHARPOS (*it) >= BEGV);
8009 eassert (NILP (it->string) && !it->s);
8010 eassert (!it->bidi_p
8011 || (EQ (it->bidi_it.string.lstring, Qnil)
8012 && it->bidi_it.string.s == NULL));
8013
8014 /* With bidi reordering, the character to display might not be the
8015 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8016 we were reseat()ed to a new buffer position, which is potentially
8017 a different paragraph. */
8018 if (it->bidi_p && it->bidi_it.first_elt)
8019 {
8020 get_visually_first_element (it);
8021 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8022 }
8023
8024 if (IT_CHARPOS (*it) >= it->stop_charpos)
8025 {
8026 if (IT_CHARPOS (*it) >= it->end_charpos)
8027 {
8028 int overlay_strings_follow_p;
8029
8030 /* End of the game, except when overlay strings follow that
8031 haven't been returned yet. */
8032 if (it->overlay_strings_at_end_processed_p)
8033 overlay_strings_follow_p = 0;
8034 else
8035 {
8036 it->overlay_strings_at_end_processed_p = true;
8037 overlay_strings_follow_p = get_overlay_strings (it, 0);
8038 }
8039
8040 if (overlay_strings_follow_p)
8041 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8042 else
8043 {
8044 it->what = IT_EOB;
8045 it->position = it->current.pos;
8046 success_p = 0;
8047 }
8048 }
8049 else if (!(!it->bidi_p
8050 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8051 || IT_CHARPOS (*it) == it->stop_charpos))
8052 {
8053 /* With bidi non-linear iteration, we could find ourselves
8054 far beyond the last computed stop_charpos, with several
8055 other stop positions in between that we missed. Scan
8056 them all now, in buffer's logical order, until we find
8057 and handle the last stop_charpos that precedes our
8058 current position. */
8059 handle_stop_backwards (it, it->stop_charpos);
8060 return GET_NEXT_DISPLAY_ELEMENT (it);
8061 }
8062 else
8063 {
8064 if (it->bidi_p)
8065 {
8066 /* Take note of the stop position we just moved across,
8067 for when we will move back across it. */
8068 it->prev_stop = it->stop_charpos;
8069 /* If we are at base paragraph embedding level, take
8070 note of the last stop position seen at this
8071 level. */
8072 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8073 it->base_level_stop = it->stop_charpos;
8074 }
8075 handle_stop (it);
8076 return GET_NEXT_DISPLAY_ELEMENT (it);
8077 }
8078 }
8079 else if (it->bidi_p
8080 /* If we are before prev_stop, we may have overstepped on
8081 our way backwards a stop_pos, and if so, we need to
8082 handle that stop_pos. */
8083 && IT_CHARPOS (*it) < it->prev_stop
8084 /* We can sometimes back up for reasons that have nothing
8085 to do with bidi reordering. E.g., compositions. The
8086 code below is only needed when we are above the base
8087 embedding level, so test for that explicitly. */
8088 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8089 {
8090 if (it->base_level_stop <= 0
8091 || IT_CHARPOS (*it) < it->base_level_stop)
8092 {
8093 /* If we lost track of base_level_stop, we need to find
8094 prev_stop by looking backwards. This happens, e.g., when
8095 we were reseated to the previous screenful of text by
8096 vertical-motion. */
8097 it->base_level_stop = BEGV;
8098 compute_stop_pos_backwards (it);
8099 handle_stop_backwards (it, it->prev_stop);
8100 }
8101 else
8102 handle_stop_backwards (it, it->base_level_stop);
8103 return GET_NEXT_DISPLAY_ELEMENT (it);
8104 }
8105 else
8106 {
8107 /* No face changes, overlays etc. in sight, so just return a
8108 character from current_buffer. */
8109 unsigned char *p;
8110 ptrdiff_t stop;
8111
8112 /* Maybe run the redisplay end trigger hook. Performance note:
8113 This doesn't seem to cost measurable time. */
8114 if (it->redisplay_end_trigger_charpos
8115 && it->glyph_row
8116 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8117 run_redisplay_end_trigger_hook (it);
8118
8119 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8120 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8121 stop)
8122 && next_element_from_composition (it))
8123 {
8124 return 1;
8125 }
8126
8127 /* Get the next character, maybe multibyte. */
8128 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8129 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8130 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8131 else
8132 it->c = *p, it->len = 1;
8133
8134 /* Record what we have and where it came from. */
8135 it->what = IT_CHARACTER;
8136 it->object = it->w->contents;
8137 it->position = it->current.pos;
8138
8139 /* Normally we return the character found above, except when we
8140 really want to return an ellipsis for selective display. */
8141 if (it->selective)
8142 {
8143 if (it->c == '\n')
8144 {
8145 /* A value of selective > 0 means hide lines indented more
8146 than that number of columns. */
8147 if (it->selective > 0
8148 && IT_CHARPOS (*it) + 1 < ZV
8149 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8150 IT_BYTEPOS (*it) + 1,
8151 it->selective))
8152 {
8153 success_p = next_element_from_ellipsis (it);
8154 it->dpvec_char_len = -1;
8155 }
8156 }
8157 else if (it->c == '\r' && it->selective == -1)
8158 {
8159 /* A value of selective == -1 means that everything from the
8160 CR to the end of the line is invisible, with maybe an
8161 ellipsis displayed for it. */
8162 success_p = next_element_from_ellipsis (it);
8163 it->dpvec_char_len = -1;
8164 }
8165 }
8166 }
8167
8168 /* Value is zero if end of buffer reached. */
8169 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8170 return success_p;
8171 }
8172
8173
8174 /* Run the redisplay end trigger hook for IT. */
8175
8176 static void
8177 run_redisplay_end_trigger_hook (struct it *it)
8178 {
8179 Lisp_Object args[3];
8180
8181 /* IT->glyph_row should be non-null, i.e. we should be actually
8182 displaying something, or otherwise we should not run the hook. */
8183 eassert (it->glyph_row);
8184
8185 /* Set up hook arguments. */
8186 args[0] = Qredisplay_end_trigger_functions;
8187 args[1] = it->window;
8188 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8189 it->redisplay_end_trigger_charpos = 0;
8190
8191 /* Since we are *trying* to run these functions, don't try to run
8192 them again, even if they get an error. */
8193 wset_redisplay_end_trigger (it->w, Qnil);
8194 Frun_hook_with_args (3, args);
8195
8196 /* Notice if it changed the face of the character we are on. */
8197 handle_face_prop (it);
8198 }
8199
8200
8201 /* Deliver a composition display element. Unlike the other
8202 next_element_from_XXX, this function is not registered in the array
8203 get_next_element[]. It is called from next_element_from_buffer and
8204 next_element_from_string when necessary. */
8205
8206 static int
8207 next_element_from_composition (struct it *it)
8208 {
8209 it->what = IT_COMPOSITION;
8210 it->len = it->cmp_it.nbytes;
8211 if (STRINGP (it->string))
8212 {
8213 if (it->c < 0)
8214 {
8215 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8216 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8217 return 0;
8218 }
8219 it->position = it->current.string_pos;
8220 it->object = it->string;
8221 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8222 IT_STRING_BYTEPOS (*it), it->string);
8223 }
8224 else
8225 {
8226 if (it->c < 0)
8227 {
8228 IT_CHARPOS (*it) += it->cmp_it.nchars;
8229 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8230 if (it->bidi_p)
8231 {
8232 if (it->bidi_it.new_paragraph)
8233 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8234 /* Resync the bidi iterator with IT's new position.
8235 FIXME: this doesn't support bidirectional text. */
8236 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8237 bidi_move_to_visually_next (&it->bidi_it);
8238 }
8239 return 0;
8240 }
8241 it->position = it->current.pos;
8242 it->object = it->w->contents;
8243 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8244 IT_BYTEPOS (*it), Qnil);
8245 }
8246 return 1;
8247 }
8248
8249
8250 \f
8251 /***********************************************************************
8252 Moving an iterator without producing glyphs
8253 ***********************************************************************/
8254
8255 /* Check if iterator is at a position corresponding to a valid buffer
8256 position after some move_it_ call. */
8257
8258 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8259 ((it)->method == GET_FROM_STRING \
8260 ? IT_STRING_CHARPOS (*it) == 0 \
8261 : 1)
8262
8263
8264 /* Move iterator IT to a specified buffer or X position within one
8265 line on the display without producing glyphs.
8266
8267 OP should be a bit mask including some or all of these bits:
8268 MOVE_TO_X: Stop upon reaching x-position TO_X.
8269 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8270 Regardless of OP's value, stop upon reaching the end of the display line.
8271
8272 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8273 This means, in particular, that TO_X includes window's horizontal
8274 scroll amount.
8275
8276 The return value has several possible values that
8277 say what condition caused the scan to stop:
8278
8279 MOVE_POS_MATCH_OR_ZV
8280 - when TO_POS or ZV was reached.
8281
8282 MOVE_X_REACHED
8283 -when TO_X was reached before TO_POS or ZV were reached.
8284
8285 MOVE_LINE_CONTINUED
8286 - when we reached the end of the display area and the line must
8287 be continued.
8288
8289 MOVE_LINE_TRUNCATED
8290 - when we reached the end of the display area and the line is
8291 truncated.
8292
8293 MOVE_NEWLINE_OR_CR
8294 - when we stopped at a line end, i.e. a newline or a CR and selective
8295 display is on. */
8296
8297 static enum move_it_result
8298 move_it_in_display_line_to (struct it *it,
8299 ptrdiff_t to_charpos, int to_x,
8300 enum move_operation_enum op)
8301 {
8302 enum move_it_result result = MOVE_UNDEFINED;
8303 struct glyph_row *saved_glyph_row;
8304 struct it wrap_it, atpos_it, atx_it, ppos_it;
8305 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8306 void *ppos_data = NULL;
8307 int may_wrap = 0;
8308 enum it_method prev_method = it->method;
8309 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8310 int saw_smaller_pos = prev_pos < to_charpos;
8311
8312 /* Don't produce glyphs in produce_glyphs. */
8313 saved_glyph_row = it->glyph_row;
8314 it->glyph_row = NULL;
8315
8316 /* Use wrap_it to save a copy of IT wherever a word wrap could
8317 occur. Use atpos_it to save a copy of IT at the desired buffer
8318 position, if found, so that we can scan ahead and check if the
8319 word later overshoots the window edge. Use atx_it similarly, for
8320 pixel positions. */
8321 wrap_it.sp = -1;
8322 atpos_it.sp = -1;
8323 atx_it.sp = -1;
8324
8325 /* Use ppos_it under bidi reordering to save a copy of IT for the
8326 position > CHARPOS that is the closest to CHARPOS. We restore
8327 that position in IT when we have scanned the entire display line
8328 without finding a match for CHARPOS and all the character
8329 positions are greater than CHARPOS. */
8330 if (it->bidi_p)
8331 {
8332 SAVE_IT (ppos_it, *it, ppos_data);
8333 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8334 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8335 SAVE_IT (ppos_it, *it, ppos_data);
8336 }
8337
8338 #define BUFFER_POS_REACHED_P() \
8339 ((op & MOVE_TO_POS) != 0 \
8340 && BUFFERP (it->object) \
8341 && (IT_CHARPOS (*it) == to_charpos \
8342 || ((!it->bidi_p \
8343 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8344 && IT_CHARPOS (*it) > to_charpos) \
8345 || (it->what == IT_COMPOSITION \
8346 && ((IT_CHARPOS (*it) > to_charpos \
8347 && to_charpos >= it->cmp_it.charpos) \
8348 || (IT_CHARPOS (*it) < to_charpos \
8349 && to_charpos <= it->cmp_it.charpos)))) \
8350 && (it->method == GET_FROM_BUFFER \
8351 || (it->method == GET_FROM_DISPLAY_VECTOR \
8352 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8353
8354 /* If there's a line-/wrap-prefix, handle it. */
8355 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8356 && it->current_y < it->last_visible_y)
8357 handle_line_prefix (it);
8358
8359 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8360 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8361
8362 while (1)
8363 {
8364 int x, i, ascent = 0, descent = 0;
8365
8366 /* Utility macro to reset an iterator with x, ascent, and descent. */
8367 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8368 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8369 (IT)->max_descent = descent)
8370
8371 /* Stop if we move beyond TO_CHARPOS (after an image or a
8372 display string or stretch glyph). */
8373 if ((op & MOVE_TO_POS) != 0
8374 && BUFFERP (it->object)
8375 && it->method == GET_FROM_BUFFER
8376 && (((!it->bidi_p
8377 /* When the iterator is at base embedding level, we
8378 are guaranteed that characters are delivered for
8379 display in strictly increasing order of their
8380 buffer positions. */
8381 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8382 && IT_CHARPOS (*it) > to_charpos)
8383 || (it->bidi_p
8384 && (prev_method == GET_FROM_IMAGE
8385 || prev_method == GET_FROM_STRETCH
8386 || prev_method == GET_FROM_STRING)
8387 /* Passed TO_CHARPOS from left to right. */
8388 && ((prev_pos < to_charpos
8389 && IT_CHARPOS (*it) > to_charpos)
8390 /* Passed TO_CHARPOS from right to left. */
8391 || (prev_pos > to_charpos
8392 && IT_CHARPOS (*it) < to_charpos)))))
8393 {
8394 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8395 {
8396 result = MOVE_POS_MATCH_OR_ZV;
8397 break;
8398 }
8399 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8400 /* If wrap_it is valid, the current position might be in a
8401 word that is wrapped. So, save the iterator in
8402 atpos_it and continue to see if wrapping happens. */
8403 SAVE_IT (atpos_it, *it, atpos_data);
8404 }
8405
8406 /* Stop when ZV reached.
8407 We used to stop here when TO_CHARPOS reached as well, but that is
8408 too soon if this glyph does not fit on this line. So we handle it
8409 explicitly below. */
8410 if (!get_next_display_element (it))
8411 {
8412 result = MOVE_POS_MATCH_OR_ZV;
8413 break;
8414 }
8415
8416 if (it->line_wrap == TRUNCATE)
8417 {
8418 if (BUFFER_POS_REACHED_P ())
8419 {
8420 result = MOVE_POS_MATCH_OR_ZV;
8421 break;
8422 }
8423 }
8424 else
8425 {
8426 if (it->line_wrap == WORD_WRAP)
8427 {
8428 if (IT_DISPLAYING_WHITESPACE (it))
8429 may_wrap = 1;
8430 else if (may_wrap)
8431 {
8432 /* We have reached a glyph that follows one or more
8433 whitespace characters. If the position is
8434 already found, we are done. */
8435 if (atpos_it.sp >= 0)
8436 {
8437 RESTORE_IT (it, &atpos_it, atpos_data);
8438 result = MOVE_POS_MATCH_OR_ZV;
8439 goto done;
8440 }
8441 if (atx_it.sp >= 0)
8442 {
8443 RESTORE_IT (it, &atx_it, atx_data);
8444 result = MOVE_X_REACHED;
8445 goto done;
8446 }
8447 /* Otherwise, we can wrap here. */
8448 SAVE_IT (wrap_it, *it, wrap_data);
8449 may_wrap = 0;
8450 }
8451 }
8452 }
8453
8454 /* Remember the line height for the current line, in case
8455 the next element doesn't fit on the line. */
8456 ascent = it->max_ascent;
8457 descent = it->max_descent;
8458
8459 /* The call to produce_glyphs will get the metrics of the
8460 display element IT is loaded with. Record the x-position
8461 before this display element, in case it doesn't fit on the
8462 line. */
8463 x = it->current_x;
8464
8465 PRODUCE_GLYPHS (it);
8466
8467 if (it->area != TEXT_AREA)
8468 {
8469 prev_method = it->method;
8470 if (it->method == GET_FROM_BUFFER)
8471 prev_pos = IT_CHARPOS (*it);
8472 set_iterator_to_next (it, 1);
8473 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8474 SET_TEXT_POS (this_line_min_pos,
8475 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8476 if (it->bidi_p
8477 && (op & MOVE_TO_POS)
8478 && IT_CHARPOS (*it) > to_charpos
8479 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8480 SAVE_IT (ppos_it, *it, ppos_data);
8481 continue;
8482 }
8483
8484 /* The number of glyphs we get back in IT->nglyphs will normally
8485 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8486 character on a terminal frame, or (iii) a line end. For the
8487 second case, IT->nglyphs - 1 padding glyphs will be present.
8488 (On X frames, there is only one glyph produced for a
8489 composite character.)
8490
8491 The behavior implemented below means, for continuation lines,
8492 that as many spaces of a TAB as fit on the current line are
8493 displayed there. For terminal frames, as many glyphs of a
8494 multi-glyph character are displayed in the current line, too.
8495 This is what the old redisplay code did, and we keep it that
8496 way. Under X, the whole shape of a complex character must
8497 fit on the line or it will be completely displayed in the
8498 next line.
8499
8500 Note that both for tabs and padding glyphs, all glyphs have
8501 the same width. */
8502 if (it->nglyphs)
8503 {
8504 /* More than one glyph or glyph doesn't fit on line. All
8505 glyphs have the same width. */
8506 int single_glyph_width = it->pixel_width / it->nglyphs;
8507 int new_x;
8508 int x_before_this_char = x;
8509 int hpos_before_this_char = it->hpos;
8510
8511 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8512 {
8513 new_x = x + single_glyph_width;
8514
8515 /* We want to leave anything reaching TO_X to the caller. */
8516 if ((op & MOVE_TO_X) && new_x > to_x)
8517 {
8518 if (BUFFER_POS_REACHED_P ())
8519 {
8520 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8521 goto buffer_pos_reached;
8522 if (atpos_it.sp < 0)
8523 {
8524 SAVE_IT (atpos_it, *it, atpos_data);
8525 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8526 }
8527 }
8528 else
8529 {
8530 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8531 {
8532 it->current_x = x;
8533 result = MOVE_X_REACHED;
8534 break;
8535 }
8536 if (atx_it.sp < 0)
8537 {
8538 SAVE_IT (atx_it, *it, atx_data);
8539 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8540 }
8541 }
8542 }
8543
8544 if (/* Lines are continued. */
8545 it->line_wrap != TRUNCATE
8546 && (/* And glyph doesn't fit on the line. */
8547 new_x > it->last_visible_x
8548 /* Or it fits exactly and we're on a window
8549 system frame. */
8550 || (new_x == it->last_visible_x
8551 && FRAME_WINDOW_P (it->f)
8552 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8553 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8554 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8555 {
8556 if (/* IT->hpos == 0 means the very first glyph
8557 doesn't fit on the line, e.g. a wide image. */
8558 it->hpos == 0
8559 || (new_x == it->last_visible_x
8560 && FRAME_WINDOW_P (it->f)))
8561 {
8562 ++it->hpos;
8563 it->current_x = new_x;
8564
8565 /* The character's last glyph just barely fits
8566 in this row. */
8567 if (i == it->nglyphs - 1)
8568 {
8569 /* If this is the destination position,
8570 return a position *before* it in this row,
8571 now that we know it fits in this row. */
8572 if (BUFFER_POS_REACHED_P ())
8573 {
8574 if (it->line_wrap != WORD_WRAP
8575 || wrap_it.sp < 0)
8576 {
8577 it->hpos = hpos_before_this_char;
8578 it->current_x = x_before_this_char;
8579 result = MOVE_POS_MATCH_OR_ZV;
8580 break;
8581 }
8582 if (it->line_wrap == WORD_WRAP
8583 && atpos_it.sp < 0)
8584 {
8585 SAVE_IT (atpos_it, *it, atpos_data);
8586 atpos_it.current_x = x_before_this_char;
8587 atpos_it.hpos = hpos_before_this_char;
8588 }
8589 }
8590
8591 prev_method = it->method;
8592 if (it->method == GET_FROM_BUFFER)
8593 prev_pos = IT_CHARPOS (*it);
8594 set_iterator_to_next (it, 1);
8595 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8596 SET_TEXT_POS (this_line_min_pos,
8597 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8598 /* On graphical terminals, newlines may
8599 "overflow" into the fringe if
8600 overflow-newline-into-fringe is non-nil.
8601 On text terminals, and on graphical
8602 terminals with no right margin, newlines
8603 may overflow into the last glyph on the
8604 display line.*/
8605 if (!FRAME_WINDOW_P (it->f)
8606 || ((it->bidi_p
8607 && it->bidi_it.paragraph_dir == R2L)
8608 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8609 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8610 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8611 {
8612 if (!get_next_display_element (it))
8613 {
8614 result = MOVE_POS_MATCH_OR_ZV;
8615 break;
8616 }
8617 if (BUFFER_POS_REACHED_P ())
8618 {
8619 if (ITERATOR_AT_END_OF_LINE_P (it))
8620 result = MOVE_POS_MATCH_OR_ZV;
8621 else
8622 result = MOVE_LINE_CONTINUED;
8623 break;
8624 }
8625 if (ITERATOR_AT_END_OF_LINE_P (it)
8626 && (it->line_wrap != WORD_WRAP
8627 || wrap_it.sp < 0))
8628 {
8629 result = MOVE_NEWLINE_OR_CR;
8630 break;
8631 }
8632 }
8633 }
8634 }
8635 else
8636 IT_RESET_X_ASCENT_DESCENT (it);
8637
8638 if (wrap_it.sp >= 0)
8639 {
8640 RESTORE_IT (it, &wrap_it, wrap_data);
8641 atpos_it.sp = -1;
8642 atx_it.sp = -1;
8643 }
8644
8645 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8646 IT_CHARPOS (*it)));
8647 result = MOVE_LINE_CONTINUED;
8648 break;
8649 }
8650
8651 if (BUFFER_POS_REACHED_P ())
8652 {
8653 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8654 goto buffer_pos_reached;
8655 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8656 {
8657 SAVE_IT (atpos_it, *it, atpos_data);
8658 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8659 }
8660 }
8661
8662 if (new_x > it->first_visible_x)
8663 {
8664 /* Glyph is visible. Increment number of glyphs that
8665 would be displayed. */
8666 ++it->hpos;
8667 }
8668 }
8669
8670 if (result != MOVE_UNDEFINED)
8671 break;
8672 }
8673 else if (BUFFER_POS_REACHED_P ())
8674 {
8675 buffer_pos_reached:
8676 IT_RESET_X_ASCENT_DESCENT (it);
8677 result = MOVE_POS_MATCH_OR_ZV;
8678 break;
8679 }
8680 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8681 {
8682 /* Stop when TO_X specified and reached. This check is
8683 necessary here because of lines consisting of a line end,
8684 only. The line end will not produce any glyphs and we
8685 would never get MOVE_X_REACHED. */
8686 eassert (it->nglyphs == 0);
8687 result = MOVE_X_REACHED;
8688 break;
8689 }
8690
8691 /* Is this a line end? If yes, we're done. */
8692 if (ITERATOR_AT_END_OF_LINE_P (it))
8693 {
8694 /* If we are past TO_CHARPOS, but never saw any character
8695 positions smaller than TO_CHARPOS, return
8696 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8697 did. */
8698 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8699 {
8700 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8701 {
8702 if (IT_CHARPOS (ppos_it) < ZV)
8703 {
8704 RESTORE_IT (it, &ppos_it, ppos_data);
8705 result = MOVE_POS_MATCH_OR_ZV;
8706 }
8707 else
8708 goto buffer_pos_reached;
8709 }
8710 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8711 && IT_CHARPOS (*it) > to_charpos)
8712 goto buffer_pos_reached;
8713 else
8714 result = MOVE_NEWLINE_OR_CR;
8715 }
8716 else
8717 result = MOVE_NEWLINE_OR_CR;
8718 break;
8719 }
8720
8721 prev_method = it->method;
8722 if (it->method == GET_FROM_BUFFER)
8723 prev_pos = IT_CHARPOS (*it);
8724 /* The current display element has been consumed. Advance
8725 to the next. */
8726 set_iterator_to_next (it, 1);
8727 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8728 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8729 if (IT_CHARPOS (*it) < to_charpos)
8730 saw_smaller_pos = 1;
8731 if (it->bidi_p
8732 && (op & MOVE_TO_POS)
8733 && IT_CHARPOS (*it) >= to_charpos
8734 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8735 SAVE_IT (ppos_it, *it, ppos_data);
8736
8737 /* Stop if lines are truncated and IT's current x-position is
8738 past the right edge of the window now. */
8739 if (it->line_wrap == TRUNCATE
8740 && it->current_x >= it->last_visible_x)
8741 {
8742 if (!FRAME_WINDOW_P (it->f)
8743 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8744 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8745 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8746 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8747 {
8748 int at_eob_p = 0;
8749
8750 if ((at_eob_p = !get_next_display_element (it))
8751 || BUFFER_POS_REACHED_P ()
8752 /* If we are past TO_CHARPOS, but never saw any
8753 character positions smaller than TO_CHARPOS,
8754 return MOVE_POS_MATCH_OR_ZV, like the
8755 unidirectional display did. */
8756 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8757 && !saw_smaller_pos
8758 && IT_CHARPOS (*it) > to_charpos))
8759 {
8760 if (it->bidi_p
8761 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8762 RESTORE_IT (it, &ppos_it, ppos_data);
8763 result = MOVE_POS_MATCH_OR_ZV;
8764 break;
8765 }
8766 if (ITERATOR_AT_END_OF_LINE_P (it))
8767 {
8768 result = MOVE_NEWLINE_OR_CR;
8769 break;
8770 }
8771 }
8772 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8773 && !saw_smaller_pos
8774 && IT_CHARPOS (*it) > to_charpos)
8775 {
8776 if (IT_CHARPOS (ppos_it) < ZV)
8777 RESTORE_IT (it, &ppos_it, ppos_data);
8778 result = MOVE_POS_MATCH_OR_ZV;
8779 break;
8780 }
8781 result = MOVE_LINE_TRUNCATED;
8782 break;
8783 }
8784 #undef IT_RESET_X_ASCENT_DESCENT
8785 }
8786
8787 #undef BUFFER_POS_REACHED_P
8788
8789 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8790 restore the saved iterator. */
8791 if (atpos_it.sp >= 0)
8792 RESTORE_IT (it, &atpos_it, atpos_data);
8793 else if (atx_it.sp >= 0)
8794 RESTORE_IT (it, &atx_it, atx_data);
8795
8796 done:
8797
8798 if (atpos_data)
8799 bidi_unshelve_cache (atpos_data, 1);
8800 if (atx_data)
8801 bidi_unshelve_cache (atx_data, 1);
8802 if (wrap_data)
8803 bidi_unshelve_cache (wrap_data, 1);
8804 if (ppos_data)
8805 bidi_unshelve_cache (ppos_data, 1);
8806
8807 /* Restore the iterator settings altered at the beginning of this
8808 function. */
8809 it->glyph_row = saved_glyph_row;
8810 return result;
8811 }
8812
8813 /* For external use. */
8814 void
8815 move_it_in_display_line (struct it *it,
8816 ptrdiff_t to_charpos, int to_x,
8817 enum move_operation_enum op)
8818 {
8819 if (it->line_wrap == WORD_WRAP
8820 && (op & MOVE_TO_X))
8821 {
8822 struct it save_it;
8823 void *save_data = NULL;
8824 int skip;
8825
8826 SAVE_IT (save_it, *it, save_data);
8827 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8828 /* When word-wrap is on, TO_X may lie past the end
8829 of a wrapped line. Then it->current is the
8830 character on the next line, so backtrack to the
8831 space before the wrap point. */
8832 if (skip == MOVE_LINE_CONTINUED)
8833 {
8834 int prev_x = max (it->current_x - 1, 0);
8835 RESTORE_IT (it, &save_it, save_data);
8836 move_it_in_display_line_to
8837 (it, -1, prev_x, MOVE_TO_X);
8838 }
8839 else
8840 bidi_unshelve_cache (save_data, 1);
8841 }
8842 else
8843 move_it_in_display_line_to (it, to_charpos, to_x, op);
8844 }
8845
8846
8847 /* Move IT forward until it satisfies one or more of the criteria in
8848 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8849
8850 OP is a bit-mask that specifies where to stop, and in particular,
8851 which of those four position arguments makes a difference. See the
8852 description of enum move_operation_enum.
8853
8854 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8855 screen line, this function will set IT to the next position that is
8856 displayed to the right of TO_CHARPOS on the screen.
8857
8858 Return the maximum pixel length of any line scanned but never more
8859 than it.last_visible_x. */
8860
8861 int
8862 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8863 {
8864 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8865 int line_height, line_start_x = 0, reached = 0;
8866 int max_current_x = 0;
8867 void *backup_data = NULL;
8868
8869 for (;;)
8870 {
8871 if (op & MOVE_TO_VPOS)
8872 {
8873 /* If no TO_CHARPOS and no TO_X specified, stop at the
8874 start of the line TO_VPOS. */
8875 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8876 {
8877 if (it->vpos == to_vpos)
8878 {
8879 reached = 1;
8880 break;
8881 }
8882 else
8883 skip = move_it_in_display_line_to (it, -1, -1, 0);
8884 }
8885 else
8886 {
8887 /* TO_VPOS >= 0 means stop at TO_X in the line at
8888 TO_VPOS, or at TO_POS, whichever comes first. */
8889 if (it->vpos == to_vpos)
8890 {
8891 reached = 2;
8892 break;
8893 }
8894
8895 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8896
8897 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8898 {
8899 reached = 3;
8900 break;
8901 }
8902 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8903 {
8904 /* We have reached TO_X but not in the line we want. */
8905 skip = move_it_in_display_line_to (it, to_charpos,
8906 -1, MOVE_TO_POS);
8907 if (skip == MOVE_POS_MATCH_OR_ZV)
8908 {
8909 reached = 4;
8910 break;
8911 }
8912 }
8913 }
8914 }
8915 else if (op & MOVE_TO_Y)
8916 {
8917 struct it it_backup;
8918
8919 if (it->line_wrap == WORD_WRAP)
8920 SAVE_IT (it_backup, *it, backup_data);
8921
8922 /* TO_Y specified means stop at TO_X in the line containing
8923 TO_Y---or at TO_CHARPOS if this is reached first. The
8924 problem is that we can't really tell whether the line
8925 contains TO_Y before we have completely scanned it, and
8926 this may skip past TO_X. What we do is to first scan to
8927 TO_X.
8928
8929 If TO_X is not specified, use a TO_X of zero. The reason
8930 is to make the outcome of this function more predictable.
8931 If we didn't use TO_X == 0, we would stop at the end of
8932 the line which is probably not what a caller would expect
8933 to happen. */
8934 skip = move_it_in_display_line_to
8935 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8936 (MOVE_TO_X | (op & MOVE_TO_POS)));
8937
8938 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8939 if (skip == MOVE_POS_MATCH_OR_ZV)
8940 reached = 5;
8941 else if (skip == MOVE_X_REACHED)
8942 {
8943 /* If TO_X was reached, we want to know whether TO_Y is
8944 in the line. We know this is the case if the already
8945 scanned glyphs make the line tall enough. Otherwise,
8946 we must check by scanning the rest of the line. */
8947 line_height = it->max_ascent + it->max_descent;
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8950 {
8951 reached = 6;
8952 break;
8953 }
8954 SAVE_IT (it_backup, *it, backup_data);
8955 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8956 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8957 op & MOVE_TO_POS);
8958 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8959 line_height = it->max_ascent + it->max_descent;
8960 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8961
8962 if (to_y >= it->current_y
8963 && to_y < it->current_y + line_height)
8964 {
8965 /* If TO_Y is in this line and TO_X was reached
8966 above, we scanned too far. We have to restore
8967 IT's settings to the ones before skipping. But
8968 keep the more accurate values of max_ascent and
8969 max_descent we've found while skipping the rest
8970 of the line, for the sake of callers, such as
8971 pos_visible_p, that need to know the line
8972 height. */
8973 int max_ascent = it->max_ascent;
8974 int max_descent = it->max_descent;
8975
8976 RESTORE_IT (it, &it_backup, backup_data);
8977 it->max_ascent = max_ascent;
8978 it->max_descent = max_descent;
8979 reached = 6;
8980 }
8981 else
8982 {
8983 skip = skip2;
8984 if (skip == MOVE_POS_MATCH_OR_ZV)
8985 reached = 7;
8986 }
8987 }
8988 else
8989 {
8990 /* Check whether TO_Y is in this line. */
8991 line_height = it->max_ascent + it->max_descent;
8992 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8993
8994 if (to_y >= it->current_y
8995 && to_y < it->current_y + line_height)
8996 {
8997 if (to_y > it->current_y)
8998 max_current_x = max (it->current_x, max_current_x);
8999
9000 /* When word-wrap is on, TO_X may lie past the end
9001 of a wrapped line. Then it->current is the
9002 character on the next line, so backtrack to the
9003 space before the wrap point. */
9004 if (skip == MOVE_LINE_CONTINUED
9005 && it->line_wrap == WORD_WRAP)
9006 {
9007 int prev_x = max (it->current_x - 1, 0);
9008 RESTORE_IT (it, &it_backup, backup_data);
9009 skip = move_it_in_display_line_to
9010 (it, -1, prev_x, MOVE_TO_X);
9011 }
9012
9013 reached = 6;
9014 }
9015 }
9016
9017 if (reached)
9018 {
9019 max_current_x = max (it->current_x, max_current_x);
9020 break;
9021 }
9022 }
9023 else if (BUFFERP (it->object)
9024 && (it->method == GET_FROM_BUFFER
9025 || it->method == GET_FROM_STRETCH)
9026 && IT_CHARPOS (*it) >= to_charpos
9027 /* Under bidi iteration, a call to set_iterator_to_next
9028 can scan far beyond to_charpos if the initial
9029 portion of the next line needs to be reordered. In
9030 that case, give move_it_in_display_line_to another
9031 chance below. */
9032 && !(it->bidi_p
9033 && it->bidi_it.scan_dir == -1))
9034 skip = MOVE_POS_MATCH_OR_ZV;
9035 else
9036 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9037
9038 switch (skip)
9039 {
9040 case MOVE_POS_MATCH_OR_ZV:
9041 max_current_x = max (it->current_x, max_current_x);
9042 reached = 8;
9043 goto out;
9044
9045 case MOVE_NEWLINE_OR_CR:
9046 max_current_x = max (it->current_x, max_current_x);
9047 set_iterator_to_next (it, 1);
9048 it->continuation_lines_width = 0;
9049 break;
9050
9051 case MOVE_LINE_TRUNCATED:
9052 max_current_x = it->last_visible_x;
9053 it->continuation_lines_width = 0;
9054 reseat_at_next_visible_line_start (it, 0);
9055 if ((op & MOVE_TO_POS) != 0
9056 && IT_CHARPOS (*it) > to_charpos)
9057 {
9058 reached = 9;
9059 goto out;
9060 }
9061 break;
9062
9063 case MOVE_LINE_CONTINUED:
9064 max_current_x = it->last_visible_x;
9065 /* For continued lines ending in a tab, some of the glyphs
9066 associated with the tab are displayed on the current
9067 line. Since it->current_x does not include these glyphs,
9068 we use it->last_visible_x instead. */
9069 if (it->c == '\t')
9070 {
9071 it->continuation_lines_width += it->last_visible_x;
9072 /* When moving by vpos, ensure that the iterator really
9073 advances to the next line (bug#847, bug#969). Fixme:
9074 do we need to do this in other circumstances? */
9075 if (it->current_x != it->last_visible_x
9076 && (op & MOVE_TO_VPOS)
9077 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9078 {
9079 line_start_x = it->current_x + it->pixel_width
9080 - it->last_visible_x;
9081 set_iterator_to_next (it, 0);
9082 }
9083 }
9084 else
9085 it->continuation_lines_width += it->current_x;
9086 break;
9087
9088 default:
9089 emacs_abort ();
9090 }
9091
9092 /* Reset/increment for the next run. */
9093 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9094 it->current_x = line_start_x;
9095 line_start_x = 0;
9096 it->hpos = 0;
9097 it->current_y += it->max_ascent + it->max_descent;
9098 ++it->vpos;
9099 last_height = it->max_ascent + it->max_descent;
9100 last_max_ascent = it->max_ascent;
9101 it->max_ascent = it->max_descent = 0;
9102 }
9103
9104 out:
9105
9106 /* On text terminals, we may stop at the end of a line in the middle
9107 of a multi-character glyph. If the glyph itself is continued,
9108 i.e. it is actually displayed on the next line, don't treat this
9109 stopping point as valid; move to the next line instead (unless
9110 that brings us offscreen). */
9111 if (!FRAME_WINDOW_P (it->f)
9112 && op & MOVE_TO_POS
9113 && IT_CHARPOS (*it) == to_charpos
9114 && it->what == IT_CHARACTER
9115 && it->nglyphs > 1
9116 && it->line_wrap == WINDOW_WRAP
9117 && it->current_x == it->last_visible_x - 1
9118 && it->c != '\n'
9119 && it->c != '\t'
9120 && it->vpos < it->w->window_end_vpos)
9121 {
9122 it->continuation_lines_width += it->current_x;
9123 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9124 it->current_y += it->max_ascent + it->max_descent;
9125 ++it->vpos;
9126 last_height = it->max_ascent + it->max_descent;
9127 last_max_ascent = it->max_ascent;
9128 }
9129
9130 if (backup_data)
9131 bidi_unshelve_cache (backup_data, 1);
9132
9133 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9134
9135 return max_current_x;
9136 }
9137
9138
9139 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9140
9141 If DY > 0, move IT backward at least that many pixels. DY = 0
9142 means move IT backward to the preceding line start or BEGV. This
9143 function may move over more than DY pixels if IT->current_y - DY
9144 ends up in the middle of a line; in this case IT->current_y will be
9145 set to the top of the line moved to. */
9146
9147 void
9148 move_it_vertically_backward (struct it *it, int dy)
9149 {
9150 int nlines, h;
9151 struct it it2, it3;
9152 void *it2data = NULL, *it3data = NULL;
9153 ptrdiff_t start_pos;
9154 int nchars_per_row
9155 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9156 ptrdiff_t pos_limit;
9157
9158 move_further_back:
9159 eassert (dy >= 0);
9160
9161 start_pos = IT_CHARPOS (*it);
9162
9163 /* Estimate how many newlines we must move back. */
9164 nlines = max (1, dy / default_line_pixel_height (it->w));
9165 if (it->line_wrap == TRUNCATE)
9166 pos_limit = BEGV;
9167 else
9168 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9169
9170 /* Set the iterator's position that many lines back. But don't go
9171 back more than NLINES full screen lines -- this wins a day with
9172 buffers which have very long lines. */
9173 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9174 back_to_previous_visible_line_start (it);
9175
9176 /* Reseat the iterator here. When moving backward, we don't want
9177 reseat to skip forward over invisible text, set up the iterator
9178 to deliver from overlay strings at the new position etc. So,
9179 use reseat_1 here. */
9180 reseat_1 (it, it->current.pos, 1);
9181
9182 /* We are now surely at a line start. */
9183 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9184 reordering is in effect. */
9185 it->continuation_lines_width = 0;
9186
9187 /* Move forward and see what y-distance we moved. First move to the
9188 start of the next line so that we get its height. We need this
9189 height to be able to tell whether we reached the specified
9190 y-distance. */
9191 SAVE_IT (it2, *it, it2data);
9192 it2.max_ascent = it2.max_descent = 0;
9193 do
9194 {
9195 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9196 MOVE_TO_POS | MOVE_TO_VPOS);
9197 }
9198 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9199 /* If we are in a display string which starts at START_POS,
9200 and that display string includes a newline, and we are
9201 right after that newline (i.e. at the beginning of a
9202 display line), exit the loop, because otherwise we will
9203 infloop, since move_it_to will see that it is already at
9204 START_POS and will not move. */
9205 || (it2.method == GET_FROM_STRING
9206 && IT_CHARPOS (it2) == start_pos
9207 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9208 eassert (IT_CHARPOS (*it) >= BEGV);
9209 SAVE_IT (it3, it2, it3data);
9210
9211 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9212 eassert (IT_CHARPOS (*it) >= BEGV);
9213 /* H is the actual vertical distance from the position in *IT
9214 and the starting position. */
9215 h = it2.current_y - it->current_y;
9216 /* NLINES is the distance in number of lines. */
9217 nlines = it2.vpos - it->vpos;
9218
9219 /* Correct IT's y and vpos position
9220 so that they are relative to the starting point. */
9221 it->vpos -= nlines;
9222 it->current_y -= h;
9223
9224 if (dy == 0)
9225 {
9226 /* DY == 0 means move to the start of the screen line. The
9227 value of nlines is > 0 if continuation lines were involved,
9228 or if the original IT position was at start of a line. */
9229 RESTORE_IT (it, it, it2data);
9230 if (nlines > 0)
9231 move_it_by_lines (it, nlines);
9232 /* The above code moves us to some position NLINES down,
9233 usually to its first glyph (leftmost in an L2R line), but
9234 that's not necessarily the start of the line, under bidi
9235 reordering. We want to get to the character position
9236 that is immediately after the newline of the previous
9237 line. */
9238 if (it->bidi_p
9239 && !it->continuation_lines_width
9240 && !STRINGP (it->string)
9241 && IT_CHARPOS (*it) > BEGV
9242 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9243 {
9244 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9245
9246 DEC_BOTH (cp, bp);
9247 cp = find_newline_no_quit (cp, bp, -1, NULL);
9248 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9249 }
9250 bidi_unshelve_cache (it3data, 1);
9251 }
9252 else
9253 {
9254 /* The y-position we try to reach, relative to *IT.
9255 Note that H has been subtracted in front of the if-statement. */
9256 int target_y = it->current_y + h - dy;
9257 int y0 = it3.current_y;
9258 int y1;
9259 int line_height;
9260
9261 RESTORE_IT (&it3, &it3, it3data);
9262 y1 = line_bottom_y (&it3);
9263 line_height = y1 - y0;
9264 RESTORE_IT (it, it, it2data);
9265 /* If we did not reach target_y, try to move further backward if
9266 we can. If we moved too far backward, try to move forward. */
9267 if (target_y < it->current_y
9268 /* This is heuristic. In a window that's 3 lines high, with
9269 a line height of 13 pixels each, recentering with point
9270 on the bottom line will try to move -39/2 = 19 pixels
9271 backward. Try to avoid moving into the first line. */
9272 && (it->current_y - target_y
9273 > min (window_box_height (it->w), line_height * 2 / 3))
9274 && IT_CHARPOS (*it) > BEGV)
9275 {
9276 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9277 target_y - it->current_y));
9278 dy = it->current_y - target_y;
9279 goto move_further_back;
9280 }
9281 else if (target_y >= it->current_y + line_height
9282 && IT_CHARPOS (*it) < ZV)
9283 {
9284 /* Should move forward by at least one line, maybe more.
9285
9286 Note: Calling move_it_by_lines can be expensive on
9287 terminal frames, where compute_motion is used (via
9288 vmotion) to do the job, when there are very long lines
9289 and truncate-lines is nil. That's the reason for
9290 treating terminal frames specially here. */
9291
9292 if (!FRAME_WINDOW_P (it->f))
9293 move_it_vertically (it, target_y - (it->current_y + line_height));
9294 else
9295 {
9296 do
9297 {
9298 move_it_by_lines (it, 1);
9299 }
9300 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9301 }
9302 }
9303 }
9304 }
9305
9306
9307 /* Move IT by a specified amount of pixel lines DY. DY negative means
9308 move backwards. DY = 0 means move to start of screen line. At the
9309 end, IT will be on the start of a screen line. */
9310
9311 void
9312 move_it_vertically (struct it *it, int dy)
9313 {
9314 if (dy <= 0)
9315 move_it_vertically_backward (it, -dy);
9316 else
9317 {
9318 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9319 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9320 MOVE_TO_POS | MOVE_TO_Y);
9321 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9322
9323 /* If buffer ends in ZV without a newline, move to the start of
9324 the line to satisfy the post-condition. */
9325 if (IT_CHARPOS (*it) == ZV
9326 && ZV > BEGV
9327 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9328 move_it_by_lines (it, 0);
9329 }
9330 }
9331
9332
9333 /* Move iterator IT past the end of the text line it is in. */
9334
9335 void
9336 move_it_past_eol (struct it *it)
9337 {
9338 enum move_it_result rc;
9339
9340 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9341 if (rc == MOVE_NEWLINE_OR_CR)
9342 set_iterator_to_next (it, 0);
9343 }
9344
9345
9346 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9347 negative means move up. DVPOS == 0 means move to the start of the
9348 screen line.
9349
9350 Optimization idea: If we would know that IT->f doesn't use
9351 a face with proportional font, we could be faster for
9352 truncate-lines nil. */
9353
9354 void
9355 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9356 {
9357
9358 /* The commented-out optimization uses vmotion on terminals. This
9359 gives bad results, because elements like it->what, on which
9360 callers such as pos_visible_p rely, aren't updated. */
9361 /* struct position pos;
9362 if (!FRAME_WINDOW_P (it->f))
9363 {
9364 struct text_pos textpos;
9365
9366 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9367 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9368 reseat (it, textpos, 1);
9369 it->vpos += pos.vpos;
9370 it->current_y += pos.vpos;
9371 }
9372 else */
9373
9374 if (dvpos == 0)
9375 {
9376 /* DVPOS == 0 means move to the start of the screen line. */
9377 move_it_vertically_backward (it, 0);
9378 /* Let next call to line_bottom_y calculate real line height. */
9379 last_height = 0;
9380 }
9381 else if (dvpos > 0)
9382 {
9383 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9384 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9385 {
9386 /* Only move to the next buffer position if we ended up in a
9387 string from display property, not in an overlay string
9388 (before-string or after-string). That is because the
9389 latter don't conceal the underlying buffer position, so
9390 we can ask to move the iterator to the exact position we
9391 are interested in. Note that, even if we are already at
9392 IT_CHARPOS (*it), the call below is not a no-op, as it
9393 will detect that we are at the end of the string, pop the
9394 iterator, and compute it->current_x and it->hpos
9395 correctly. */
9396 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9397 -1, -1, -1, MOVE_TO_POS);
9398 }
9399 }
9400 else
9401 {
9402 struct it it2;
9403 void *it2data = NULL;
9404 ptrdiff_t start_charpos, i;
9405 int nchars_per_row
9406 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9407 ptrdiff_t pos_limit;
9408
9409 /* Start at the beginning of the screen line containing IT's
9410 position. This may actually move vertically backwards,
9411 in case of overlays, so adjust dvpos accordingly. */
9412 dvpos += it->vpos;
9413 move_it_vertically_backward (it, 0);
9414 dvpos -= it->vpos;
9415
9416 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9417 screen lines, and reseat the iterator there. */
9418 start_charpos = IT_CHARPOS (*it);
9419 if (it->line_wrap == TRUNCATE)
9420 pos_limit = BEGV;
9421 else
9422 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9423 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9424 back_to_previous_visible_line_start (it);
9425 reseat (it, it->current.pos, 1);
9426
9427 /* Move further back if we end up in a string or an image. */
9428 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9429 {
9430 /* First try to move to start of display line. */
9431 dvpos += it->vpos;
9432 move_it_vertically_backward (it, 0);
9433 dvpos -= it->vpos;
9434 if (IT_POS_VALID_AFTER_MOVE_P (it))
9435 break;
9436 /* If start of line is still in string or image,
9437 move further back. */
9438 back_to_previous_visible_line_start (it);
9439 reseat (it, it->current.pos, 1);
9440 dvpos--;
9441 }
9442
9443 it->current_x = it->hpos = 0;
9444
9445 /* Above call may have moved too far if continuation lines
9446 are involved. Scan forward and see if it did. */
9447 SAVE_IT (it2, *it, it2data);
9448 it2.vpos = it2.current_y = 0;
9449 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9450 it->vpos -= it2.vpos;
9451 it->current_y -= it2.current_y;
9452 it->current_x = it->hpos = 0;
9453
9454 /* If we moved too far back, move IT some lines forward. */
9455 if (it2.vpos > -dvpos)
9456 {
9457 int delta = it2.vpos + dvpos;
9458
9459 RESTORE_IT (&it2, &it2, it2data);
9460 SAVE_IT (it2, *it, it2data);
9461 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9462 /* Move back again if we got too far ahead. */
9463 if (IT_CHARPOS (*it) >= start_charpos)
9464 RESTORE_IT (it, &it2, it2data);
9465 else
9466 bidi_unshelve_cache (it2data, 1);
9467 }
9468 else
9469 RESTORE_IT (it, it, it2data);
9470 }
9471 }
9472
9473 /* Return true if IT points into the middle of a display vector. */
9474
9475 bool
9476 in_display_vector_p (struct it *it)
9477 {
9478 return (it->method == GET_FROM_DISPLAY_VECTOR
9479 && it->current.dpvec_index > 0
9480 && it->dpvec + it->current.dpvec_index != it->dpend);
9481 }
9482
9483 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9484 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9485 WINDOW must be a live window and defaults to the selected one. The
9486 return value is a cons of the maximum pixel-width of any text line and
9487 the maximum pixel-height of all text lines.
9488
9489 The optional argument FROM, if non-nil, specifies the first text
9490 position and defaults to the minimum accessible position of the buffer.
9491 If FROM is t, use the minimum accessible position that is not a newline
9492 character. TO, if non-nil, specifies the last text position and
9493 defaults to the maximum accessible position of the buffer. If TO is t,
9494 use the maximum accessible position that is not a newline character.
9495
9496 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9497 width that can be returned. X_LIMIT nil or omitted, means to use the
9498 pixel-width of WINDOW's body; use this if you do not intend to change
9499 the width of WINDOW. Use the maximum width WINDOW may assume if you
9500 intend to change WINDOW's width.
9501
9502 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9503 height that can be returned. Text lines whose y-coordinate is beyond
9504 Y_LIMIT are ignored. Since calculating the text height of a large
9505 buffer can take some time, it makes sense to specify this argument if
9506 the size of the buffer is unknown.
9507
9508 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9509 include the height of the mode- or header-line of WINDOW in the return
9510 value. If it is either the symbol `mode-line' or `header-line', include
9511 only the height of that line, if present, in the return value. If t,
9512 include the height of any of these lines in the return value. */)
9513 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9514 Lisp_Object mode_and_header_line)
9515 {
9516 struct window *w = decode_live_window (window);
9517 Lisp_Object buf;
9518 struct buffer *b;
9519 struct it it;
9520 struct buffer *old_buffer = NULL;
9521 ptrdiff_t start, end, pos;
9522 struct text_pos startp;
9523 void *itdata = NULL;
9524 int c, max_y = -1, x = 0, y = 0;
9525
9526 buf = w->contents;
9527 CHECK_BUFFER (buf);
9528 b = XBUFFER (buf);
9529
9530 if (b != current_buffer)
9531 {
9532 old_buffer = current_buffer;
9533 set_buffer_internal (b);
9534 }
9535
9536 if (NILP (from))
9537 start = BEGV;
9538 else if (EQ (from, Qt))
9539 {
9540 start = pos = BEGV;
9541 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9542 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9543 start = pos;
9544 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9545 start = pos;
9546 }
9547 else
9548 {
9549 CHECK_NUMBER_COERCE_MARKER (from);
9550 start = min (max (XINT (from), BEGV), ZV);
9551 }
9552
9553 if (NILP (to))
9554 end = ZV;
9555 else if (EQ (to, Qt))
9556 {
9557 end = pos = ZV;
9558 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9559 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9560 end = pos;
9561 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9562 end = pos;
9563 }
9564 else
9565 {
9566 CHECK_NUMBER_COERCE_MARKER (to);
9567 end = max (start, min (XINT (to), ZV));
9568 }
9569
9570 if (!NILP (y_limit))
9571 {
9572 CHECK_NUMBER (y_limit);
9573 max_y = min (XINT (y_limit), INT_MAX);
9574 }
9575
9576 itdata = bidi_shelve_cache ();
9577 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9578 start_display (&it, w, startp);
9579
9580 /** move_it_vertically_backward (&it, 0); **/
9581 if (NILP (x_limit))
9582 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9583 else
9584 {
9585 CHECK_NUMBER (x_limit);
9586 it.last_visible_x = min (XINT (x_limit), INFINITY);
9587 /* Actually, we never want move_it_to stop at to_x. But to make
9588 sure that move_it_in_display_line_to always moves far enough,
9589 we set it to INT_MAX and specify MOVE_TO_X. */
9590 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9591 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9592 }
9593
9594 if (start == end)
9595 y = it.current_y;
9596 else
9597 {
9598 /* Count last line. */
9599 last_height = 0;
9600 y = line_bottom_y (&it); /* - y; */
9601 }
9602
9603 if (!EQ (mode_and_header_line, Qheader_line)
9604 && !EQ (mode_and_header_line, Qt))
9605 /* Do not count the header-line which was counted automatically by
9606 start_display. */
9607 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9608
9609 if (EQ (mode_and_header_line, Qmode_line)
9610 || EQ (mode_and_header_line, Qt))
9611 /* Do count the mode-line which is not included automatically by
9612 start_display. */
9613 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9614
9615 bidi_unshelve_cache (itdata, 0);
9616
9617 if (old_buffer)
9618 set_buffer_internal (old_buffer);
9619
9620 return Fcons (make_number (x), make_number (y));
9621 }
9622 \f
9623 /***********************************************************************
9624 Messages
9625 ***********************************************************************/
9626
9627
9628 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9629 to *Messages*. */
9630
9631 void
9632 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9633 {
9634 Lisp_Object args[3];
9635 Lisp_Object msg, fmt;
9636 char *buffer;
9637 ptrdiff_t len;
9638 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9639 USE_SAFE_ALLOCA;
9640
9641 fmt = msg = Qnil;
9642 GCPRO4 (fmt, msg, arg1, arg2);
9643
9644 args[0] = fmt = build_string (format);
9645 args[1] = arg1;
9646 args[2] = arg2;
9647 msg = Fformat (3, args);
9648
9649 len = SBYTES (msg) + 1;
9650 buffer = SAFE_ALLOCA (len);
9651 memcpy (buffer, SDATA (msg), len);
9652
9653 message_dolog (buffer, len - 1, 1, 0);
9654 SAFE_FREE ();
9655
9656 UNGCPRO;
9657 }
9658
9659
9660 /* Output a newline in the *Messages* buffer if "needs" one. */
9661
9662 void
9663 message_log_maybe_newline (void)
9664 {
9665 if (message_log_need_newline)
9666 message_dolog ("", 0, 1, 0);
9667 }
9668
9669
9670 /* Add a string M of length NBYTES to the message log, optionally
9671 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9672 true, means interpret the contents of M as multibyte. This
9673 function calls low-level routines in order to bypass text property
9674 hooks, etc. which might not be safe to run.
9675
9676 This may GC (insert may run before/after change hooks),
9677 so the buffer M must NOT point to a Lisp string. */
9678
9679 void
9680 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9681 {
9682 const unsigned char *msg = (const unsigned char *) m;
9683
9684 if (!NILP (Vmemory_full))
9685 return;
9686
9687 if (!NILP (Vmessage_log_max))
9688 {
9689 struct buffer *oldbuf;
9690 Lisp_Object oldpoint, oldbegv, oldzv;
9691 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9692 ptrdiff_t point_at_end = 0;
9693 ptrdiff_t zv_at_end = 0;
9694 Lisp_Object old_deactivate_mark;
9695 struct gcpro gcpro1;
9696
9697 old_deactivate_mark = Vdeactivate_mark;
9698 oldbuf = current_buffer;
9699
9700 /* Ensure the Messages buffer exists, and switch to it.
9701 If we created it, set the major-mode. */
9702 {
9703 int newbuffer = 0;
9704 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9705
9706 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9707
9708 if (newbuffer
9709 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9710 call0 (intern ("messages-buffer-mode"));
9711 }
9712
9713 bset_undo_list (current_buffer, Qt);
9714 bset_cache_long_scans (current_buffer, Qnil);
9715
9716 oldpoint = message_dolog_marker1;
9717 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9718 oldbegv = message_dolog_marker2;
9719 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9720 oldzv = message_dolog_marker3;
9721 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9722 GCPRO1 (old_deactivate_mark);
9723
9724 if (PT == Z)
9725 point_at_end = 1;
9726 if (ZV == Z)
9727 zv_at_end = 1;
9728
9729 BEGV = BEG;
9730 BEGV_BYTE = BEG_BYTE;
9731 ZV = Z;
9732 ZV_BYTE = Z_BYTE;
9733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9734
9735 /* Insert the string--maybe converting multibyte to single byte
9736 or vice versa, so that all the text fits the buffer. */
9737 if (multibyte
9738 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9739 {
9740 ptrdiff_t i;
9741 int c, char_bytes;
9742 char work[1];
9743
9744 /* Convert a multibyte string to single-byte
9745 for the *Message* buffer. */
9746 for (i = 0; i < nbytes; i += char_bytes)
9747 {
9748 c = string_char_and_length (msg + i, &char_bytes);
9749 work[0] = (ASCII_CHAR_P (c)
9750 ? c
9751 : multibyte_char_to_unibyte (c));
9752 insert_1_both (work, 1, 1, 1, 0, 0);
9753 }
9754 }
9755 else if (! multibyte
9756 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9757 {
9758 ptrdiff_t i;
9759 int c, char_bytes;
9760 unsigned char str[MAX_MULTIBYTE_LENGTH];
9761 /* Convert a single-byte string to multibyte
9762 for the *Message* buffer. */
9763 for (i = 0; i < nbytes; i++)
9764 {
9765 c = msg[i];
9766 MAKE_CHAR_MULTIBYTE (c);
9767 char_bytes = CHAR_STRING (c, str);
9768 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9769 }
9770 }
9771 else if (nbytes)
9772 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9773
9774 if (nlflag)
9775 {
9776 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9777 printmax_t dups;
9778
9779 insert_1_both ("\n", 1, 1, 1, 0, 0);
9780
9781 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9782 this_bol = PT;
9783 this_bol_byte = PT_BYTE;
9784
9785 /* See if this line duplicates the previous one.
9786 If so, combine duplicates. */
9787 if (this_bol > BEG)
9788 {
9789 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9790 prev_bol = PT;
9791 prev_bol_byte = PT_BYTE;
9792
9793 dups = message_log_check_duplicate (prev_bol_byte,
9794 this_bol_byte);
9795 if (dups)
9796 {
9797 del_range_both (prev_bol, prev_bol_byte,
9798 this_bol, this_bol_byte, 0);
9799 if (dups > 1)
9800 {
9801 char dupstr[sizeof " [ times]"
9802 + INT_STRLEN_BOUND (printmax_t)];
9803
9804 /* If you change this format, don't forget to also
9805 change message_log_check_duplicate. */
9806 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9807 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9808 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9809 }
9810 }
9811 }
9812
9813 /* If we have more than the desired maximum number of lines
9814 in the *Messages* buffer now, delete the oldest ones.
9815 This is safe because we don't have undo in this buffer. */
9816
9817 if (NATNUMP (Vmessage_log_max))
9818 {
9819 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9820 -XFASTINT (Vmessage_log_max) - 1, 0);
9821 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9822 }
9823 }
9824 BEGV = marker_position (oldbegv);
9825 BEGV_BYTE = marker_byte_position (oldbegv);
9826
9827 if (zv_at_end)
9828 {
9829 ZV = Z;
9830 ZV_BYTE = Z_BYTE;
9831 }
9832 else
9833 {
9834 ZV = marker_position (oldzv);
9835 ZV_BYTE = marker_byte_position (oldzv);
9836 }
9837
9838 if (point_at_end)
9839 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9840 else
9841 /* We can't do Fgoto_char (oldpoint) because it will run some
9842 Lisp code. */
9843 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9844 marker_byte_position (oldpoint));
9845
9846 UNGCPRO;
9847 unchain_marker (XMARKER (oldpoint));
9848 unchain_marker (XMARKER (oldbegv));
9849 unchain_marker (XMARKER (oldzv));
9850
9851 /* We called insert_1_both above with its 5th argument (PREPARE)
9852 zero, which prevents insert_1_both from calling
9853 prepare_to_modify_buffer, which in turns prevents us from
9854 incrementing windows_or_buffers_changed even if *Messages* is
9855 shown in some window. So we must manually set
9856 windows_or_buffers_changed here to make up for that. */
9857 windows_or_buffers_changed = old_windows_or_buffers_changed;
9858 bset_redisplay (current_buffer);
9859
9860 set_buffer_internal (oldbuf);
9861
9862 message_log_need_newline = !nlflag;
9863 Vdeactivate_mark = old_deactivate_mark;
9864 }
9865 }
9866
9867
9868 /* We are at the end of the buffer after just having inserted a newline.
9869 (Note: We depend on the fact we won't be crossing the gap.)
9870 Check to see if the most recent message looks a lot like the previous one.
9871 Return 0 if different, 1 if the new one should just replace it, or a
9872 value N > 1 if we should also append " [N times]". */
9873
9874 static intmax_t
9875 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9876 {
9877 ptrdiff_t i;
9878 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9879 int seen_dots = 0;
9880 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9881 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9882
9883 for (i = 0; i < len; i++)
9884 {
9885 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9886 seen_dots = 1;
9887 if (p1[i] != p2[i])
9888 return seen_dots;
9889 }
9890 p1 += len;
9891 if (*p1 == '\n')
9892 return 2;
9893 if (*p1++ == ' ' && *p1++ == '[')
9894 {
9895 char *pend;
9896 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9897 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9898 return n + 1;
9899 }
9900 return 0;
9901 }
9902 \f
9903
9904 /* Display an echo area message M with a specified length of NBYTES
9905 bytes. The string may include null characters. If M is not a
9906 string, clear out any existing message, and let the mini-buffer
9907 text show through.
9908
9909 This function cancels echoing. */
9910
9911 void
9912 message3 (Lisp_Object m)
9913 {
9914 struct gcpro gcpro1;
9915
9916 GCPRO1 (m);
9917 clear_message (true, true);
9918 cancel_echoing ();
9919
9920 /* First flush out any partial line written with print. */
9921 message_log_maybe_newline ();
9922 if (STRINGP (m))
9923 {
9924 ptrdiff_t nbytes = SBYTES (m);
9925 bool multibyte = STRING_MULTIBYTE (m);
9926 USE_SAFE_ALLOCA;
9927 char *buffer = SAFE_ALLOCA (nbytes);
9928 memcpy (buffer, SDATA (m), nbytes);
9929 message_dolog (buffer, nbytes, 1, multibyte);
9930 SAFE_FREE ();
9931 }
9932 message3_nolog (m);
9933
9934 UNGCPRO;
9935 }
9936
9937
9938 /* The non-logging version of message3.
9939 This does not cancel echoing, because it is used for echoing.
9940 Perhaps we need to make a separate function for echoing
9941 and make this cancel echoing. */
9942
9943 void
9944 message3_nolog (Lisp_Object m)
9945 {
9946 struct frame *sf = SELECTED_FRAME ();
9947
9948 if (FRAME_INITIAL_P (sf))
9949 {
9950 if (noninteractive_need_newline)
9951 putc ('\n', stderr);
9952 noninteractive_need_newline = 0;
9953 if (STRINGP (m))
9954 {
9955 Lisp_Object s = ENCODE_SYSTEM (m);
9956
9957 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9958 }
9959 if (cursor_in_echo_area == 0)
9960 fprintf (stderr, "\n");
9961 fflush (stderr);
9962 }
9963 /* Error messages get reported properly by cmd_error, so this must be just an
9964 informative message; if the frame hasn't really been initialized yet, just
9965 toss it. */
9966 else if (INTERACTIVE && sf->glyphs_initialized_p)
9967 {
9968 /* Get the frame containing the mini-buffer
9969 that the selected frame is using. */
9970 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9971 Lisp_Object frame = XWINDOW (mini_window)->frame;
9972 struct frame *f = XFRAME (frame);
9973
9974 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9975 Fmake_frame_visible (frame);
9976
9977 if (STRINGP (m) && SCHARS (m) > 0)
9978 {
9979 set_message (m);
9980 if (minibuffer_auto_raise)
9981 Fraise_frame (frame);
9982 /* Assume we are not echoing.
9983 (If we are, echo_now will override this.) */
9984 echo_message_buffer = Qnil;
9985 }
9986 else
9987 clear_message (true, true);
9988
9989 do_pending_window_change (0);
9990 echo_area_display (1);
9991 do_pending_window_change (0);
9992 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9993 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9994 }
9995 }
9996
9997
9998 /* Display a null-terminated echo area message M. If M is 0, clear
9999 out any existing message, and let the mini-buffer text show through.
10000
10001 The buffer M must continue to exist until after the echo area gets
10002 cleared or some other message gets displayed there. Do not pass
10003 text that is stored in a Lisp string. Do not pass text in a buffer
10004 that was alloca'd. */
10005
10006 void
10007 message1 (const char *m)
10008 {
10009 message3 (m ? build_unibyte_string (m) : Qnil);
10010 }
10011
10012
10013 /* The non-logging counterpart of message1. */
10014
10015 void
10016 message1_nolog (const char *m)
10017 {
10018 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10019 }
10020
10021 /* Display a message M which contains a single %s
10022 which gets replaced with STRING. */
10023
10024 void
10025 message_with_string (const char *m, Lisp_Object string, int log)
10026 {
10027 CHECK_STRING (string);
10028
10029 if (noninteractive)
10030 {
10031 if (m)
10032 {
10033 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10034 String whose data pointer might be passed to us in M. So
10035 we use a local copy. */
10036 char *fmt = xstrdup (m);
10037
10038 if (noninteractive_need_newline)
10039 putc ('\n', stderr);
10040 noninteractive_need_newline = 0;
10041 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10042 if (!cursor_in_echo_area)
10043 fprintf (stderr, "\n");
10044 fflush (stderr);
10045 xfree (fmt);
10046 }
10047 }
10048 else if (INTERACTIVE)
10049 {
10050 /* The frame whose minibuffer we're going to display the message on.
10051 It may be larger than the selected frame, so we need
10052 to use its buffer, not the selected frame's buffer. */
10053 Lisp_Object mini_window;
10054 struct frame *f, *sf = SELECTED_FRAME ();
10055
10056 /* Get the frame containing the minibuffer
10057 that the selected frame is using. */
10058 mini_window = FRAME_MINIBUF_WINDOW (sf);
10059 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10060
10061 /* Error messages get reported properly by cmd_error, so this must be
10062 just an informative message; if the frame hasn't really been
10063 initialized yet, just toss it. */
10064 if (f->glyphs_initialized_p)
10065 {
10066 Lisp_Object args[2], msg;
10067 struct gcpro gcpro1, gcpro2;
10068
10069 args[0] = build_string (m);
10070 args[1] = msg = string;
10071 GCPRO2 (args[0], msg);
10072 gcpro1.nvars = 2;
10073
10074 msg = Fformat (2, args);
10075
10076 if (log)
10077 message3 (msg);
10078 else
10079 message3_nolog (msg);
10080
10081 UNGCPRO;
10082
10083 /* Print should start at the beginning of the message
10084 buffer next time. */
10085 message_buf_print = 0;
10086 }
10087 }
10088 }
10089
10090
10091 /* Dump an informative message to the minibuf. If M is 0, clear out
10092 any existing message, and let the mini-buffer text show through. */
10093
10094 static void
10095 vmessage (const char *m, va_list ap)
10096 {
10097 if (noninteractive)
10098 {
10099 if (m)
10100 {
10101 if (noninteractive_need_newline)
10102 putc ('\n', stderr);
10103 noninteractive_need_newline = 0;
10104 vfprintf (stderr, m, ap);
10105 if (cursor_in_echo_area == 0)
10106 fprintf (stderr, "\n");
10107 fflush (stderr);
10108 }
10109 }
10110 else if (INTERACTIVE)
10111 {
10112 /* The frame whose mini-buffer we're going to display the message
10113 on. It may be larger than the selected frame, so we need to
10114 use its buffer, not the selected frame's buffer. */
10115 Lisp_Object mini_window;
10116 struct frame *f, *sf = SELECTED_FRAME ();
10117
10118 /* Get the frame containing the mini-buffer
10119 that the selected frame is using. */
10120 mini_window = FRAME_MINIBUF_WINDOW (sf);
10121 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10122
10123 /* Error messages get reported properly by cmd_error, so this must be
10124 just an informative message; if the frame hasn't really been
10125 initialized yet, just toss it. */
10126 if (f->glyphs_initialized_p)
10127 {
10128 if (m)
10129 {
10130 ptrdiff_t len;
10131 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10132 char *message_buf = alloca (maxsize + 1);
10133
10134 len = doprnt (message_buf, maxsize, m, 0, ap);
10135
10136 message3 (make_string (message_buf, len));
10137 }
10138 else
10139 message1 (0);
10140
10141 /* Print should start at the beginning of the message
10142 buffer next time. */
10143 message_buf_print = 0;
10144 }
10145 }
10146 }
10147
10148 void
10149 message (const char *m, ...)
10150 {
10151 va_list ap;
10152 va_start (ap, m);
10153 vmessage (m, ap);
10154 va_end (ap);
10155 }
10156
10157
10158 #if 0
10159 /* The non-logging version of message. */
10160
10161 void
10162 message_nolog (const char *m, ...)
10163 {
10164 Lisp_Object old_log_max;
10165 va_list ap;
10166 va_start (ap, m);
10167 old_log_max = Vmessage_log_max;
10168 Vmessage_log_max = Qnil;
10169 vmessage (m, ap);
10170 Vmessage_log_max = old_log_max;
10171 va_end (ap);
10172 }
10173 #endif
10174
10175
10176 /* Display the current message in the current mini-buffer. This is
10177 only called from error handlers in process.c, and is not time
10178 critical. */
10179
10180 void
10181 update_echo_area (void)
10182 {
10183 if (!NILP (echo_area_buffer[0]))
10184 {
10185 Lisp_Object string;
10186 string = Fcurrent_message ();
10187 message3 (string);
10188 }
10189 }
10190
10191
10192 /* Make sure echo area buffers in `echo_buffers' are live.
10193 If they aren't, make new ones. */
10194
10195 static void
10196 ensure_echo_area_buffers (void)
10197 {
10198 int i;
10199
10200 for (i = 0; i < 2; ++i)
10201 if (!BUFFERP (echo_buffer[i])
10202 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10203 {
10204 char name[30];
10205 Lisp_Object old_buffer;
10206 int j;
10207
10208 old_buffer = echo_buffer[i];
10209 echo_buffer[i] = Fget_buffer_create
10210 (make_formatted_string (name, " *Echo Area %d*", i));
10211 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10212 /* to force word wrap in echo area -
10213 it was decided to postpone this*/
10214 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10215
10216 for (j = 0; j < 2; ++j)
10217 if (EQ (old_buffer, echo_area_buffer[j]))
10218 echo_area_buffer[j] = echo_buffer[i];
10219 }
10220 }
10221
10222
10223 /* Call FN with args A1..A2 with either the current or last displayed
10224 echo_area_buffer as current buffer.
10225
10226 WHICH zero means use the current message buffer
10227 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10228 from echo_buffer[] and clear it.
10229
10230 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10231 suitable buffer from echo_buffer[] and clear it.
10232
10233 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10234 that the current message becomes the last displayed one, make
10235 choose a suitable buffer for echo_area_buffer[0], and clear it.
10236
10237 Value is what FN returns. */
10238
10239 static int
10240 with_echo_area_buffer (struct window *w, int which,
10241 int (*fn) (ptrdiff_t, Lisp_Object),
10242 ptrdiff_t a1, Lisp_Object a2)
10243 {
10244 Lisp_Object buffer;
10245 int this_one, the_other, clear_buffer_p, rc;
10246 ptrdiff_t count = SPECPDL_INDEX ();
10247
10248 /* If buffers aren't live, make new ones. */
10249 ensure_echo_area_buffers ();
10250
10251 clear_buffer_p = 0;
10252
10253 if (which == 0)
10254 this_one = 0, the_other = 1;
10255 else if (which > 0)
10256 this_one = 1, the_other = 0;
10257 else
10258 {
10259 this_one = 0, the_other = 1;
10260 clear_buffer_p = true;
10261
10262 /* We need a fresh one in case the current echo buffer equals
10263 the one containing the last displayed echo area message. */
10264 if (!NILP (echo_area_buffer[this_one])
10265 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10266 echo_area_buffer[this_one] = Qnil;
10267 }
10268
10269 /* Choose a suitable buffer from echo_buffer[] is we don't
10270 have one. */
10271 if (NILP (echo_area_buffer[this_one]))
10272 {
10273 echo_area_buffer[this_one]
10274 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10275 ? echo_buffer[the_other]
10276 : echo_buffer[this_one]);
10277 clear_buffer_p = true;
10278 }
10279
10280 buffer = echo_area_buffer[this_one];
10281
10282 /* Don't get confused by reusing the buffer used for echoing
10283 for a different purpose. */
10284 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10285 cancel_echoing ();
10286
10287 record_unwind_protect (unwind_with_echo_area_buffer,
10288 with_echo_area_buffer_unwind_data (w));
10289
10290 /* Make the echo area buffer current. Note that for display
10291 purposes, it is not necessary that the displayed window's buffer
10292 == current_buffer, except for text property lookup. So, let's
10293 only set that buffer temporarily here without doing a full
10294 Fset_window_buffer. We must also change w->pointm, though,
10295 because otherwise an assertions in unshow_buffer fails, and Emacs
10296 aborts. */
10297 set_buffer_internal_1 (XBUFFER (buffer));
10298 if (w)
10299 {
10300 wset_buffer (w, buffer);
10301 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10302 }
10303
10304 bset_undo_list (current_buffer, Qt);
10305 bset_read_only (current_buffer, Qnil);
10306 specbind (Qinhibit_read_only, Qt);
10307 specbind (Qinhibit_modification_hooks, Qt);
10308
10309 if (clear_buffer_p && Z > BEG)
10310 del_range (BEG, Z);
10311
10312 eassert (BEGV >= BEG);
10313 eassert (ZV <= Z && ZV >= BEGV);
10314
10315 rc = fn (a1, a2);
10316
10317 eassert (BEGV >= BEG);
10318 eassert (ZV <= Z && ZV >= BEGV);
10319
10320 unbind_to (count, Qnil);
10321 return rc;
10322 }
10323
10324
10325 /* Save state that should be preserved around the call to the function
10326 FN called in with_echo_area_buffer. */
10327
10328 static Lisp_Object
10329 with_echo_area_buffer_unwind_data (struct window *w)
10330 {
10331 int i = 0;
10332 Lisp_Object vector, tmp;
10333
10334 /* Reduce consing by keeping one vector in
10335 Vwith_echo_area_save_vector. */
10336 vector = Vwith_echo_area_save_vector;
10337 Vwith_echo_area_save_vector = Qnil;
10338
10339 if (NILP (vector))
10340 vector = Fmake_vector (make_number (9), Qnil);
10341
10342 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10343 ASET (vector, i, Vdeactivate_mark); ++i;
10344 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10345
10346 if (w)
10347 {
10348 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10349 ASET (vector, i, w->contents); ++i;
10350 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10351 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10352 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10353 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10354 }
10355 else
10356 {
10357 int end = i + 6;
10358 for (; i < end; ++i)
10359 ASET (vector, i, Qnil);
10360 }
10361
10362 eassert (i == ASIZE (vector));
10363 return vector;
10364 }
10365
10366
10367 /* Restore global state from VECTOR which was created by
10368 with_echo_area_buffer_unwind_data. */
10369
10370 static void
10371 unwind_with_echo_area_buffer (Lisp_Object vector)
10372 {
10373 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10374 Vdeactivate_mark = AREF (vector, 1);
10375 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10376
10377 if (WINDOWP (AREF (vector, 3)))
10378 {
10379 struct window *w;
10380 Lisp_Object buffer;
10381
10382 w = XWINDOW (AREF (vector, 3));
10383 buffer = AREF (vector, 4);
10384
10385 wset_buffer (w, buffer);
10386 set_marker_both (w->pointm, buffer,
10387 XFASTINT (AREF (vector, 5)),
10388 XFASTINT (AREF (vector, 6)));
10389 set_marker_both (w->start, buffer,
10390 XFASTINT (AREF (vector, 7)),
10391 XFASTINT (AREF (vector, 8)));
10392 }
10393
10394 Vwith_echo_area_save_vector = vector;
10395 }
10396
10397
10398 /* Set up the echo area for use by print functions. MULTIBYTE_P
10399 non-zero means we will print multibyte. */
10400
10401 void
10402 setup_echo_area_for_printing (int multibyte_p)
10403 {
10404 /* If we can't find an echo area any more, exit. */
10405 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10406 Fkill_emacs (Qnil);
10407
10408 ensure_echo_area_buffers ();
10409
10410 if (!message_buf_print)
10411 {
10412 /* A message has been output since the last time we printed.
10413 Choose a fresh echo area buffer. */
10414 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10415 echo_area_buffer[0] = echo_buffer[1];
10416 else
10417 echo_area_buffer[0] = echo_buffer[0];
10418
10419 /* Switch to that buffer and clear it. */
10420 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10421 bset_truncate_lines (current_buffer, Qnil);
10422
10423 if (Z > BEG)
10424 {
10425 ptrdiff_t count = SPECPDL_INDEX ();
10426 specbind (Qinhibit_read_only, Qt);
10427 /* Note that undo recording is always disabled. */
10428 del_range (BEG, Z);
10429 unbind_to (count, Qnil);
10430 }
10431 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10432
10433 /* Set up the buffer for the multibyteness we need. */
10434 if (multibyte_p
10435 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10436 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10437
10438 /* Raise the frame containing the echo area. */
10439 if (minibuffer_auto_raise)
10440 {
10441 struct frame *sf = SELECTED_FRAME ();
10442 Lisp_Object mini_window;
10443 mini_window = FRAME_MINIBUF_WINDOW (sf);
10444 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10445 }
10446
10447 message_log_maybe_newline ();
10448 message_buf_print = 1;
10449 }
10450 else
10451 {
10452 if (NILP (echo_area_buffer[0]))
10453 {
10454 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10455 echo_area_buffer[0] = echo_buffer[1];
10456 else
10457 echo_area_buffer[0] = echo_buffer[0];
10458 }
10459
10460 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10461 {
10462 /* Someone switched buffers between print requests. */
10463 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10464 bset_truncate_lines (current_buffer, Qnil);
10465 }
10466 }
10467 }
10468
10469
10470 /* Display an echo area message in window W. Value is non-zero if W's
10471 height is changed. If display_last_displayed_message_p is
10472 non-zero, display the message that was last displayed, otherwise
10473 display the current message. */
10474
10475 static int
10476 display_echo_area (struct window *w)
10477 {
10478 int i, no_message_p, window_height_changed_p;
10479
10480 /* Temporarily disable garbage collections while displaying the echo
10481 area. This is done because a GC can print a message itself.
10482 That message would modify the echo area buffer's contents while a
10483 redisplay of the buffer is going on, and seriously confuse
10484 redisplay. */
10485 ptrdiff_t count = inhibit_garbage_collection ();
10486
10487 /* If there is no message, we must call display_echo_area_1
10488 nevertheless because it resizes the window. But we will have to
10489 reset the echo_area_buffer in question to nil at the end because
10490 with_echo_area_buffer will sets it to an empty buffer. */
10491 i = display_last_displayed_message_p ? 1 : 0;
10492 no_message_p = NILP (echo_area_buffer[i]);
10493
10494 window_height_changed_p
10495 = with_echo_area_buffer (w, display_last_displayed_message_p,
10496 display_echo_area_1,
10497 (intptr_t) w, Qnil);
10498
10499 if (no_message_p)
10500 echo_area_buffer[i] = Qnil;
10501
10502 unbind_to (count, Qnil);
10503 return window_height_changed_p;
10504 }
10505
10506
10507 /* Helper for display_echo_area. Display the current buffer which
10508 contains the current echo area message in window W, a mini-window,
10509 a pointer to which is passed in A1. A2..A4 are currently not used.
10510 Change the height of W so that all of the message is displayed.
10511 Value is non-zero if height of W was changed. */
10512
10513 static int
10514 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10515 {
10516 intptr_t i1 = a1;
10517 struct window *w = (struct window *) i1;
10518 Lisp_Object window;
10519 struct text_pos start;
10520 int window_height_changed_p = 0;
10521
10522 /* Do this before displaying, so that we have a large enough glyph
10523 matrix for the display. If we can't get enough space for the
10524 whole text, display the last N lines. That works by setting w->start. */
10525 window_height_changed_p = resize_mini_window (w, 0);
10526
10527 /* Use the starting position chosen by resize_mini_window. */
10528 SET_TEXT_POS_FROM_MARKER (start, w->start);
10529
10530 /* Display. */
10531 clear_glyph_matrix (w->desired_matrix);
10532 XSETWINDOW (window, w);
10533 try_window (window, start, 0);
10534
10535 return window_height_changed_p;
10536 }
10537
10538
10539 /* Resize the echo area window to exactly the size needed for the
10540 currently displayed message, if there is one. If a mini-buffer
10541 is active, don't shrink it. */
10542
10543 void
10544 resize_echo_area_exactly (void)
10545 {
10546 if (BUFFERP (echo_area_buffer[0])
10547 && WINDOWP (echo_area_window))
10548 {
10549 struct window *w = XWINDOW (echo_area_window);
10550 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10551 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10552 (intptr_t) w, resize_exactly);
10553 if (resized_p)
10554 {
10555 windows_or_buffers_changed = 42;
10556 update_mode_lines = 30;
10557 redisplay_internal ();
10558 }
10559 }
10560 }
10561
10562
10563 /* Callback function for with_echo_area_buffer, when used from
10564 resize_echo_area_exactly. A1 contains a pointer to the window to
10565 resize, EXACTLY non-nil means resize the mini-window exactly to the
10566 size of the text displayed. A3 and A4 are not used. Value is what
10567 resize_mini_window returns. */
10568
10569 static int
10570 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10571 {
10572 intptr_t i1 = a1;
10573 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10574 }
10575
10576
10577 /* Resize mini-window W to fit the size of its contents. EXACT_P
10578 means size the window exactly to the size needed. Otherwise, it's
10579 only enlarged until W's buffer is empty.
10580
10581 Set W->start to the right place to begin display. If the whole
10582 contents fit, start at the beginning. Otherwise, start so as
10583 to make the end of the contents appear. This is particularly
10584 important for y-or-n-p, but seems desirable generally.
10585
10586 Value is non-zero if the window height has been changed. */
10587
10588 int
10589 resize_mini_window (struct window *w, int exact_p)
10590 {
10591 struct frame *f = XFRAME (w->frame);
10592 int window_height_changed_p = 0;
10593
10594 eassert (MINI_WINDOW_P (w));
10595
10596 /* By default, start display at the beginning. */
10597 set_marker_both (w->start, w->contents,
10598 BUF_BEGV (XBUFFER (w->contents)),
10599 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10600
10601 /* Don't resize windows while redisplaying a window; it would
10602 confuse redisplay functions when the size of the window they are
10603 displaying changes from under them. Such a resizing can happen,
10604 for instance, when which-func prints a long message while
10605 we are running fontification-functions. We're running these
10606 functions with safe_call which binds inhibit-redisplay to t. */
10607 if (!NILP (Vinhibit_redisplay))
10608 return 0;
10609
10610 /* Nil means don't try to resize. */
10611 if (NILP (Vresize_mini_windows)
10612 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10613 return 0;
10614
10615 if (!FRAME_MINIBUF_ONLY_P (f))
10616 {
10617 struct it it;
10618 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10619 + WINDOW_PIXEL_HEIGHT (w));
10620 int unit = FRAME_LINE_HEIGHT (f);
10621 int height, max_height;
10622 struct text_pos start;
10623 struct buffer *old_current_buffer = NULL;
10624
10625 if (current_buffer != XBUFFER (w->contents))
10626 {
10627 old_current_buffer = current_buffer;
10628 set_buffer_internal (XBUFFER (w->contents));
10629 }
10630
10631 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10632
10633 /* Compute the max. number of lines specified by the user. */
10634 if (FLOATP (Vmax_mini_window_height))
10635 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10636 else if (INTEGERP (Vmax_mini_window_height))
10637 max_height = XINT (Vmax_mini_window_height) * unit;
10638 else
10639 max_height = total_height / 4;
10640
10641 /* Correct that max. height if it's bogus. */
10642 max_height = clip_to_bounds (unit, max_height, total_height);
10643
10644 /* Find out the height of the text in the window. */
10645 if (it.line_wrap == TRUNCATE)
10646 height = unit;
10647 else
10648 {
10649 last_height = 0;
10650 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10651 if (it.max_ascent == 0 && it.max_descent == 0)
10652 height = it.current_y + last_height;
10653 else
10654 height = it.current_y + it.max_ascent + it.max_descent;
10655 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10656 }
10657
10658 /* Compute a suitable window start. */
10659 if (height > max_height)
10660 {
10661 height = max_height;
10662 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10663 move_it_vertically_backward (&it, height);
10664 start = it.current.pos;
10665 }
10666 else
10667 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10668 SET_MARKER_FROM_TEXT_POS (w->start, start);
10669
10670 if (EQ (Vresize_mini_windows, Qgrow_only))
10671 {
10672 /* Let it grow only, until we display an empty message, in which
10673 case the window shrinks again. */
10674 if (height > WINDOW_PIXEL_HEIGHT (w))
10675 {
10676 int old_height = WINDOW_PIXEL_HEIGHT (w);
10677
10678 FRAME_WINDOWS_FROZEN (f) = 1;
10679 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10680 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10681 }
10682 else if (height < WINDOW_PIXEL_HEIGHT (w)
10683 && (exact_p || BEGV == ZV))
10684 {
10685 int old_height = WINDOW_PIXEL_HEIGHT (w);
10686
10687 FRAME_WINDOWS_FROZEN (f) = 0;
10688 shrink_mini_window (w, 1);
10689 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10690 }
10691 }
10692 else
10693 {
10694 /* Always resize to exact size needed. */
10695 if (height > WINDOW_PIXEL_HEIGHT (w))
10696 {
10697 int old_height = WINDOW_PIXEL_HEIGHT (w);
10698
10699 FRAME_WINDOWS_FROZEN (f) = 1;
10700 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10701 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10702 }
10703 else if (height < WINDOW_PIXEL_HEIGHT (w))
10704 {
10705 int old_height = WINDOW_PIXEL_HEIGHT (w);
10706
10707 FRAME_WINDOWS_FROZEN (f) = 0;
10708 shrink_mini_window (w, 1);
10709
10710 if (height)
10711 {
10712 FRAME_WINDOWS_FROZEN (f) = 1;
10713 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10714 }
10715
10716 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10717 }
10718 }
10719
10720 if (old_current_buffer)
10721 set_buffer_internal (old_current_buffer);
10722 }
10723
10724 return window_height_changed_p;
10725 }
10726
10727
10728 /* Value is the current message, a string, or nil if there is no
10729 current message. */
10730
10731 Lisp_Object
10732 current_message (void)
10733 {
10734 Lisp_Object msg;
10735
10736 if (!BUFFERP (echo_area_buffer[0]))
10737 msg = Qnil;
10738 else
10739 {
10740 with_echo_area_buffer (0, 0, current_message_1,
10741 (intptr_t) &msg, Qnil);
10742 if (NILP (msg))
10743 echo_area_buffer[0] = Qnil;
10744 }
10745
10746 return msg;
10747 }
10748
10749
10750 static int
10751 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10752 {
10753 intptr_t i1 = a1;
10754 Lisp_Object *msg = (Lisp_Object *) i1;
10755
10756 if (Z > BEG)
10757 *msg = make_buffer_string (BEG, Z, 1);
10758 else
10759 *msg = Qnil;
10760 return 0;
10761 }
10762
10763
10764 /* Push the current message on Vmessage_stack for later restoration
10765 by restore_message. Value is non-zero if the current message isn't
10766 empty. This is a relatively infrequent operation, so it's not
10767 worth optimizing. */
10768
10769 bool
10770 push_message (void)
10771 {
10772 Lisp_Object msg = current_message ();
10773 Vmessage_stack = Fcons (msg, Vmessage_stack);
10774 return STRINGP (msg);
10775 }
10776
10777
10778 /* Restore message display from the top of Vmessage_stack. */
10779
10780 void
10781 restore_message (void)
10782 {
10783 eassert (CONSP (Vmessage_stack));
10784 message3_nolog (XCAR (Vmessage_stack));
10785 }
10786
10787
10788 /* Handler for unwind-protect calling pop_message. */
10789
10790 void
10791 pop_message_unwind (void)
10792 {
10793 /* Pop the top-most entry off Vmessage_stack. */
10794 eassert (CONSP (Vmessage_stack));
10795 Vmessage_stack = XCDR (Vmessage_stack);
10796 }
10797
10798
10799 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10800 exits. If the stack is not empty, we have a missing pop_message
10801 somewhere. */
10802
10803 void
10804 check_message_stack (void)
10805 {
10806 if (!NILP (Vmessage_stack))
10807 emacs_abort ();
10808 }
10809
10810
10811 /* Truncate to NCHARS what will be displayed in the echo area the next
10812 time we display it---but don't redisplay it now. */
10813
10814 void
10815 truncate_echo_area (ptrdiff_t nchars)
10816 {
10817 if (nchars == 0)
10818 echo_area_buffer[0] = Qnil;
10819 else if (!noninteractive
10820 && INTERACTIVE
10821 && !NILP (echo_area_buffer[0]))
10822 {
10823 struct frame *sf = SELECTED_FRAME ();
10824 /* Error messages get reported properly by cmd_error, so this must be
10825 just an informative message; if the frame hasn't really been
10826 initialized yet, just toss it. */
10827 if (sf->glyphs_initialized_p)
10828 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10829 }
10830 }
10831
10832
10833 /* Helper function for truncate_echo_area. Truncate the current
10834 message to at most NCHARS characters. */
10835
10836 static int
10837 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10838 {
10839 if (BEG + nchars < Z)
10840 del_range (BEG + nchars, Z);
10841 if (Z == BEG)
10842 echo_area_buffer[0] = Qnil;
10843 return 0;
10844 }
10845
10846 /* Set the current message to STRING. */
10847
10848 static void
10849 set_message (Lisp_Object string)
10850 {
10851 eassert (STRINGP (string));
10852
10853 message_enable_multibyte = STRING_MULTIBYTE (string);
10854
10855 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10856 message_buf_print = 0;
10857 help_echo_showing_p = 0;
10858
10859 if (STRINGP (Vdebug_on_message)
10860 && STRINGP (string)
10861 && fast_string_match (Vdebug_on_message, string) >= 0)
10862 call_debugger (list2 (Qerror, string));
10863 }
10864
10865
10866 /* Helper function for set_message. First argument is ignored and second
10867 argument has the same meaning as for set_message.
10868 This function is called with the echo area buffer being current. */
10869
10870 static int
10871 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10872 {
10873 eassert (STRINGP (string));
10874
10875 /* Change multibyteness of the echo buffer appropriately. */
10876 if (message_enable_multibyte
10877 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10878 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10879
10880 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10881 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10882 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10883
10884 /* Insert new message at BEG. */
10885 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10886
10887 /* This function takes care of single/multibyte conversion.
10888 We just have to ensure that the echo area buffer has the right
10889 setting of enable_multibyte_characters. */
10890 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10891
10892 return 0;
10893 }
10894
10895
10896 /* Clear messages. CURRENT_P non-zero means clear the current
10897 message. LAST_DISPLAYED_P non-zero means clear the message
10898 last displayed. */
10899
10900 void
10901 clear_message (bool current_p, bool last_displayed_p)
10902 {
10903 if (current_p)
10904 {
10905 echo_area_buffer[0] = Qnil;
10906 message_cleared_p = true;
10907 }
10908
10909 if (last_displayed_p)
10910 echo_area_buffer[1] = Qnil;
10911
10912 message_buf_print = 0;
10913 }
10914
10915 /* Clear garbaged frames.
10916
10917 This function is used where the old redisplay called
10918 redraw_garbaged_frames which in turn called redraw_frame which in
10919 turn called clear_frame. The call to clear_frame was a source of
10920 flickering. I believe a clear_frame is not necessary. It should
10921 suffice in the new redisplay to invalidate all current matrices,
10922 and ensure a complete redisplay of all windows. */
10923
10924 static void
10925 clear_garbaged_frames (void)
10926 {
10927 if (frame_garbaged)
10928 {
10929 Lisp_Object tail, frame;
10930
10931 FOR_EACH_FRAME (tail, frame)
10932 {
10933 struct frame *f = XFRAME (frame);
10934
10935 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10936 {
10937 if (f->resized_p)
10938 redraw_frame (f);
10939 else
10940 clear_current_matrices (f);
10941 fset_redisplay (f);
10942 f->garbaged = false;
10943 f->resized_p = false;
10944 }
10945 }
10946
10947 frame_garbaged = false;
10948 }
10949 }
10950
10951
10952 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10953 is non-zero update selected_frame. Value is non-zero if the
10954 mini-windows height has been changed. */
10955
10956 static int
10957 echo_area_display (int update_frame_p)
10958 {
10959 Lisp_Object mini_window;
10960 struct window *w;
10961 struct frame *f;
10962 int window_height_changed_p = 0;
10963 struct frame *sf = SELECTED_FRAME ();
10964
10965 mini_window = FRAME_MINIBUF_WINDOW (sf);
10966 w = XWINDOW (mini_window);
10967 f = XFRAME (WINDOW_FRAME (w));
10968
10969 /* Don't display if frame is invisible or not yet initialized. */
10970 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10971 return 0;
10972
10973 #ifdef HAVE_WINDOW_SYSTEM
10974 /* When Emacs starts, selected_frame may be the initial terminal
10975 frame. If we let this through, a message would be displayed on
10976 the terminal. */
10977 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10978 return 0;
10979 #endif /* HAVE_WINDOW_SYSTEM */
10980
10981 /* Redraw garbaged frames. */
10982 clear_garbaged_frames ();
10983
10984 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10985 {
10986 echo_area_window = mini_window;
10987 window_height_changed_p = display_echo_area (w);
10988 w->must_be_updated_p = true;
10989
10990 /* Update the display, unless called from redisplay_internal.
10991 Also don't update the screen during redisplay itself. The
10992 update will happen at the end of redisplay, and an update
10993 here could cause confusion. */
10994 if (update_frame_p && !redisplaying_p)
10995 {
10996 int n = 0;
10997
10998 /* If the display update has been interrupted by pending
10999 input, update mode lines in the frame. Due to the
11000 pending input, it might have been that redisplay hasn't
11001 been called, so that mode lines above the echo area are
11002 garbaged. This looks odd, so we prevent it here. */
11003 if (!display_completed)
11004 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11005
11006 if (window_height_changed_p
11007 /* Don't do this if Emacs is shutting down. Redisplay
11008 needs to run hooks. */
11009 && !NILP (Vrun_hooks))
11010 {
11011 /* Must update other windows. Likewise as in other
11012 cases, don't let this update be interrupted by
11013 pending input. */
11014 ptrdiff_t count = SPECPDL_INDEX ();
11015 specbind (Qredisplay_dont_pause, Qt);
11016 windows_or_buffers_changed = 44;
11017 redisplay_internal ();
11018 unbind_to (count, Qnil);
11019 }
11020 else if (FRAME_WINDOW_P (f) && n == 0)
11021 {
11022 /* Window configuration is the same as before.
11023 Can do with a display update of the echo area,
11024 unless we displayed some mode lines. */
11025 update_single_window (w, 1);
11026 flush_frame (f);
11027 }
11028 else
11029 update_frame (f, 1, 1);
11030
11031 /* If cursor is in the echo area, make sure that the next
11032 redisplay displays the minibuffer, so that the cursor will
11033 be replaced with what the minibuffer wants. */
11034 if (cursor_in_echo_area)
11035 wset_redisplay (XWINDOW (mini_window));
11036 }
11037 }
11038 else if (!EQ (mini_window, selected_window))
11039 wset_redisplay (XWINDOW (mini_window));
11040
11041 /* Last displayed message is now the current message. */
11042 echo_area_buffer[1] = echo_area_buffer[0];
11043 /* Inform read_char that we're not echoing. */
11044 echo_message_buffer = Qnil;
11045
11046 /* Prevent redisplay optimization in redisplay_internal by resetting
11047 this_line_start_pos. This is done because the mini-buffer now
11048 displays the message instead of its buffer text. */
11049 if (EQ (mini_window, selected_window))
11050 CHARPOS (this_line_start_pos) = 0;
11051
11052 return window_height_changed_p;
11053 }
11054
11055 /* Nonzero if W's buffer was changed but not saved. */
11056
11057 static int
11058 window_buffer_changed (struct window *w)
11059 {
11060 struct buffer *b = XBUFFER (w->contents);
11061
11062 eassert (BUFFER_LIVE_P (b));
11063
11064 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11065 }
11066
11067 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11068
11069 static int
11070 mode_line_update_needed (struct window *w)
11071 {
11072 return (w->column_number_displayed != -1
11073 && !(PT == w->last_point && !window_outdated (w))
11074 && (w->column_number_displayed != current_column ()));
11075 }
11076
11077 /* Nonzero if window start of W is frozen and may not be changed during
11078 redisplay. */
11079
11080 static bool
11081 window_frozen_p (struct window *w)
11082 {
11083 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11084 {
11085 Lisp_Object window;
11086
11087 XSETWINDOW (window, w);
11088 if (MINI_WINDOW_P (w))
11089 return 0;
11090 else if (EQ (window, selected_window))
11091 return 0;
11092 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11093 && EQ (window, Vminibuf_scroll_window))
11094 /* This special window can't be frozen too. */
11095 return 0;
11096 else
11097 return 1;
11098 }
11099 return 0;
11100 }
11101
11102 /***********************************************************************
11103 Mode Lines and Frame Titles
11104 ***********************************************************************/
11105
11106 /* A buffer for constructing non-propertized mode-line strings and
11107 frame titles in it; allocated from the heap in init_xdisp and
11108 resized as needed in store_mode_line_noprop_char. */
11109
11110 static char *mode_line_noprop_buf;
11111
11112 /* The buffer's end, and a current output position in it. */
11113
11114 static char *mode_line_noprop_buf_end;
11115 static char *mode_line_noprop_ptr;
11116
11117 #define MODE_LINE_NOPROP_LEN(start) \
11118 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11119
11120 static enum {
11121 MODE_LINE_DISPLAY = 0,
11122 MODE_LINE_TITLE,
11123 MODE_LINE_NOPROP,
11124 MODE_LINE_STRING
11125 } mode_line_target;
11126
11127 /* Alist that caches the results of :propertize.
11128 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11129 static Lisp_Object mode_line_proptrans_alist;
11130
11131 /* List of strings making up the mode-line. */
11132 static Lisp_Object mode_line_string_list;
11133
11134 /* Base face property when building propertized mode line string. */
11135 static Lisp_Object mode_line_string_face;
11136 static Lisp_Object mode_line_string_face_prop;
11137
11138
11139 /* Unwind data for mode line strings */
11140
11141 static Lisp_Object Vmode_line_unwind_vector;
11142
11143 static Lisp_Object
11144 format_mode_line_unwind_data (struct frame *target_frame,
11145 struct buffer *obuf,
11146 Lisp_Object owin,
11147 int save_proptrans)
11148 {
11149 Lisp_Object vector, tmp;
11150
11151 /* Reduce consing by keeping one vector in
11152 Vwith_echo_area_save_vector. */
11153 vector = Vmode_line_unwind_vector;
11154 Vmode_line_unwind_vector = Qnil;
11155
11156 if (NILP (vector))
11157 vector = Fmake_vector (make_number (10), Qnil);
11158
11159 ASET (vector, 0, make_number (mode_line_target));
11160 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11161 ASET (vector, 2, mode_line_string_list);
11162 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11163 ASET (vector, 4, mode_line_string_face);
11164 ASET (vector, 5, mode_line_string_face_prop);
11165
11166 if (obuf)
11167 XSETBUFFER (tmp, obuf);
11168 else
11169 tmp = Qnil;
11170 ASET (vector, 6, tmp);
11171 ASET (vector, 7, owin);
11172 if (target_frame)
11173 {
11174 /* Similarly to `with-selected-window', if the operation selects
11175 a window on another frame, we must restore that frame's
11176 selected window, and (for a tty) the top-frame. */
11177 ASET (vector, 8, target_frame->selected_window);
11178 if (FRAME_TERMCAP_P (target_frame))
11179 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11180 }
11181
11182 return vector;
11183 }
11184
11185 static void
11186 unwind_format_mode_line (Lisp_Object vector)
11187 {
11188 Lisp_Object old_window = AREF (vector, 7);
11189 Lisp_Object target_frame_window = AREF (vector, 8);
11190 Lisp_Object old_top_frame = AREF (vector, 9);
11191
11192 mode_line_target = XINT (AREF (vector, 0));
11193 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11194 mode_line_string_list = AREF (vector, 2);
11195 if (! EQ (AREF (vector, 3), Qt))
11196 mode_line_proptrans_alist = AREF (vector, 3);
11197 mode_line_string_face = AREF (vector, 4);
11198 mode_line_string_face_prop = AREF (vector, 5);
11199
11200 /* Select window before buffer, since it may change the buffer. */
11201 if (!NILP (old_window))
11202 {
11203 /* If the operation that we are unwinding had selected a window
11204 on a different frame, reset its frame-selected-window. For a
11205 text terminal, reset its top-frame if necessary. */
11206 if (!NILP (target_frame_window))
11207 {
11208 Lisp_Object frame
11209 = WINDOW_FRAME (XWINDOW (target_frame_window));
11210
11211 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11212 Fselect_window (target_frame_window, Qt);
11213
11214 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11215 Fselect_frame (old_top_frame, Qt);
11216 }
11217
11218 Fselect_window (old_window, Qt);
11219 }
11220
11221 if (!NILP (AREF (vector, 6)))
11222 {
11223 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11224 ASET (vector, 6, Qnil);
11225 }
11226
11227 Vmode_line_unwind_vector = vector;
11228 }
11229
11230
11231 /* Store a single character C for the frame title in mode_line_noprop_buf.
11232 Re-allocate mode_line_noprop_buf if necessary. */
11233
11234 static void
11235 store_mode_line_noprop_char (char c)
11236 {
11237 /* If output position has reached the end of the allocated buffer,
11238 increase the buffer's size. */
11239 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11240 {
11241 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11242 ptrdiff_t size = len;
11243 mode_line_noprop_buf =
11244 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11245 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11246 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11247 }
11248
11249 *mode_line_noprop_ptr++ = c;
11250 }
11251
11252
11253 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11254 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11255 characters that yield more columns than PRECISION; PRECISION <= 0
11256 means copy the whole string. Pad with spaces until FIELD_WIDTH
11257 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11258 pad. Called from display_mode_element when it is used to build a
11259 frame title. */
11260
11261 static int
11262 store_mode_line_noprop (const char *string, int field_width, int precision)
11263 {
11264 const unsigned char *str = (const unsigned char *) string;
11265 int n = 0;
11266 ptrdiff_t dummy, nbytes;
11267
11268 /* Copy at most PRECISION chars from STR. */
11269 nbytes = strlen (string);
11270 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11271 while (nbytes--)
11272 store_mode_line_noprop_char (*str++);
11273
11274 /* Fill up with spaces until FIELD_WIDTH reached. */
11275 while (field_width > 0
11276 && n < field_width)
11277 {
11278 store_mode_line_noprop_char (' ');
11279 ++n;
11280 }
11281
11282 return n;
11283 }
11284
11285 /***********************************************************************
11286 Frame Titles
11287 ***********************************************************************/
11288
11289 #ifdef HAVE_WINDOW_SYSTEM
11290
11291 /* Set the title of FRAME, if it has changed. The title format is
11292 Vicon_title_format if FRAME is iconified, otherwise it is
11293 frame_title_format. */
11294
11295 static void
11296 x_consider_frame_title (Lisp_Object frame)
11297 {
11298 struct frame *f = XFRAME (frame);
11299
11300 if (FRAME_WINDOW_P (f)
11301 || FRAME_MINIBUF_ONLY_P (f)
11302 || f->explicit_name)
11303 {
11304 /* Do we have more than one visible frame on this X display? */
11305 Lisp_Object tail, other_frame, fmt;
11306 ptrdiff_t title_start;
11307 char *title;
11308 ptrdiff_t len;
11309 struct it it;
11310 ptrdiff_t count = SPECPDL_INDEX ();
11311
11312 FOR_EACH_FRAME (tail, other_frame)
11313 {
11314 struct frame *tf = XFRAME (other_frame);
11315
11316 if (tf != f
11317 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11318 && !FRAME_MINIBUF_ONLY_P (tf)
11319 && !EQ (other_frame, tip_frame)
11320 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11321 break;
11322 }
11323
11324 /* Set global variable indicating that multiple frames exist. */
11325 multiple_frames = CONSP (tail);
11326
11327 /* Switch to the buffer of selected window of the frame. Set up
11328 mode_line_target so that display_mode_element will output into
11329 mode_line_noprop_buf; then display the title. */
11330 record_unwind_protect (unwind_format_mode_line,
11331 format_mode_line_unwind_data
11332 (f, current_buffer, selected_window, 0));
11333
11334 Fselect_window (f->selected_window, Qt);
11335 set_buffer_internal_1
11336 (XBUFFER (XWINDOW (f->selected_window)->contents));
11337 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11338
11339 mode_line_target = MODE_LINE_TITLE;
11340 title_start = MODE_LINE_NOPROP_LEN (0);
11341 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11342 NULL, DEFAULT_FACE_ID);
11343 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11344 len = MODE_LINE_NOPROP_LEN (title_start);
11345 title = mode_line_noprop_buf + title_start;
11346 unbind_to (count, Qnil);
11347
11348 /* Set the title only if it's changed. This avoids consing in
11349 the common case where it hasn't. (If it turns out that we've
11350 already wasted too much time by walking through the list with
11351 display_mode_element, then we might need to optimize at a
11352 higher level than this.) */
11353 if (! STRINGP (f->name)
11354 || SBYTES (f->name) != len
11355 || memcmp (title, SDATA (f->name), len) != 0)
11356 x_implicitly_set_name (f, make_string (title, len), Qnil);
11357 }
11358 }
11359
11360 #endif /* not HAVE_WINDOW_SYSTEM */
11361
11362 \f
11363 /***********************************************************************
11364 Menu Bars
11365 ***********************************************************************/
11366
11367 /* Non-zero if we will not redisplay all visible windows. */
11368 #define REDISPLAY_SOME_P() \
11369 ((windows_or_buffers_changed == 0 \
11370 || windows_or_buffers_changed == REDISPLAY_SOME) \
11371 && (update_mode_lines == 0 \
11372 || update_mode_lines == REDISPLAY_SOME))
11373
11374 /* Prepare for redisplay by updating menu-bar item lists when
11375 appropriate. This can call eval. */
11376
11377 static void
11378 prepare_menu_bars (void)
11379 {
11380 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11381 bool some_windows = REDISPLAY_SOME_P ();
11382 struct gcpro gcpro1, gcpro2;
11383 Lisp_Object tooltip_frame;
11384
11385 #ifdef HAVE_WINDOW_SYSTEM
11386 tooltip_frame = tip_frame;
11387 #else
11388 tooltip_frame = Qnil;
11389 #endif
11390
11391 if (FUNCTIONP (Vpre_redisplay_function))
11392 {
11393 Lisp_Object windows = all_windows ? Qt : Qnil;
11394 if (all_windows && some_windows)
11395 {
11396 Lisp_Object ws = window_list ();
11397 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11398 {
11399 Lisp_Object this = XCAR (ws);
11400 struct window *w = XWINDOW (this);
11401 if (w->redisplay
11402 || XFRAME (w->frame)->redisplay
11403 || XBUFFER (w->contents)->text->redisplay)
11404 {
11405 windows = Fcons (this, windows);
11406 }
11407 }
11408 }
11409 safe_call1 (Vpre_redisplay_function, windows);
11410 }
11411
11412 /* Update all frame titles based on their buffer names, etc. We do
11413 this before the menu bars so that the buffer-menu will show the
11414 up-to-date frame titles. */
11415 #ifdef HAVE_WINDOW_SYSTEM
11416 if (all_windows)
11417 {
11418 Lisp_Object tail, frame;
11419
11420 FOR_EACH_FRAME (tail, frame)
11421 {
11422 struct frame *f = XFRAME (frame);
11423 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11424 if (some_windows
11425 && !f->redisplay
11426 && !w->redisplay
11427 && !XBUFFER (w->contents)->text->redisplay)
11428 continue;
11429
11430 if (!EQ (frame, tooltip_frame)
11431 && (FRAME_ICONIFIED_P (f)
11432 || FRAME_VISIBLE_P (f) == 1
11433 /* Exclude TTY frames that are obscured because they
11434 are not the top frame on their console. This is
11435 because x_consider_frame_title actually switches
11436 to the frame, which for TTY frames means it is
11437 marked as garbaged, and will be completely
11438 redrawn on the next redisplay cycle. This causes
11439 TTY frames to be completely redrawn, when there
11440 are more than one of them, even though nothing
11441 should be changed on display. */
11442 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11443 x_consider_frame_title (frame);
11444 }
11445 }
11446 #endif /* HAVE_WINDOW_SYSTEM */
11447
11448 /* Update the menu bar item lists, if appropriate. This has to be
11449 done before any actual redisplay or generation of display lines. */
11450
11451 if (all_windows)
11452 {
11453 Lisp_Object tail, frame;
11454 ptrdiff_t count = SPECPDL_INDEX ();
11455 /* 1 means that update_menu_bar has run its hooks
11456 so any further calls to update_menu_bar shouldn't do so again. */
11457 int menu_bar_hooks_run = 0;
11458
11459 record_unwind_save_match_data ();
11460
11461 FOR_EACH_FRAME (tail, frame)
11462 {
11463 struct frame *f = XFRAME (frame);
11464 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11465
11466 /* Ignore tooltip frame. */
11467 if (EQ (frame, tooltip_frame))
11468 continue;
11469
11470 if (some_windows
11471 && !f->redisplay
11472 && !w->redisplay
11473 && !XBUFFER (w->contents)->text->redisplay)
11474 continue;
11475
11476 /* If a window on this frame changed size, report that to
11477 the user and clear the size-change flag. */
11478 if (FRAME_WINDOW_SIZES_CHANGED (f))
11479 {
11480 Lisp_Object functions;
11481
11482 /* Clear flag first in case we get an error below. */
11483 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11484 functions = Vwindow_size_change_functions;
11485 GCPRO2 (tail, functions);
11486
11487 while (CONSP (functions))
11488 {
11489 if (!EQ (XCAR (functions), Qt))
11490 call1 (XCAR (functions), frame);
11491 functions = XCDR (functions);
11492 }
11493 UNGCPRO;
11494 }
11495
11496 GCPRO1 (tail);
11497 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11498 #ifdef HAVE_WINDOW_SYSTEM
11499 update_tool_bar (f, 0);
11500 #endif
11501 #ifdef HAVE_NS
11502 if (windows_or_buffers_changed
11503 && FRAME_NS_P (f))
11504 ns_set_doc_edited
11505 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11506 #endif
11507 UNGCPRO;
11508 }
11509
11510 unbind_to (count, Qnil);
11511 }
11512 else
11513 {
11514 struct frame *sf = SELECTED_FRAME ();
11515 update_menu_bar (sf, 1, 0);
11516 #ifdef HAVE_WINDOW_SYSTEM
11517 update_tool_bar (sf, 1);
11518 #endif
11519 }
11520 }
11521
11522
11523 /* Update the menu bar item list for frame F. This has to be done
11524 before we start to fill in any display lines, because it can call
11525 eval.
11526
11527 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11528
11529 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11530 already ran the menu bar hooks for this redisplay, so there
11531 is no need to run them again. The return value is the
11532 updated value of this flag, to pass to the next call. */
11533
11534 static int
11535 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11536 {
11537 Lisp_Object window;
11538 register struct window *w;
11539
11540 /* If called recursively during a menu update, do nothing. This can
11541 happen when, for instance, an activate-menubar-hook causes a
11542 redisplay. */
11543 if (inhibit_menubar_update)
11544 return hooks_run;
11545
11546 window = FRAME_SELECTED_WINDOW (f);
11547 w = XWINDOW (window);
11548
11549 if (FRAME_WINDOW_P (f)
11550 ?
11551 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11552 || defined (HAVE_NS) || defined (USE_GTK)
11553 FRAME_EXTERNAL_MENU_BAR (f)
11554 #else
11555 FRAME_MENU_BAR_LINES (f) > 0
11556 #endif
11557 : FRAME_MENU_BAR_LINES (f) > 0)
11558 {
11559 /* If the user has switched buffers or windows, we need to
11560 recompute to reflect the new bindings. But we'll
11561 recompute when update_mode_lines is set too; that means
11562 that people can use force-mode-line-update to request
11563 that the menu bar be recomputed. The adverse effect on
11564 the rest of the redisplay algorithm is about the same as
11565 windows_or_buffers_changed anyway. */
11566 if (windows_or_buffers_changed
11567 /* This used to test w->update_mode_line, but we believe
11568 there is no need to recompute the menu in that case. */
11569 || update_mode_lines
11570 || window_buffer_changed (w))
11571 {
11572 struct buffer *prev = current_buffer;
11573 ptrdiff_t count = SPECPDL_INDEX ();
11574
11575 specbind (Qinhibit_menubar_update, Qt);
11576
11577 set_buffer_internal_1 (XBUFFER (w->contents));
11578 if (save_match_data)
11579 record_unwind_save_match_data ();
11580 if (NILP (Voverriding_local_map_menu_flag))
11581 {
11582 specbind (Qoverriding_terminal_local_map, Qnil);
11583 specbind (Qoverriding_local_map, Qnil);
11584 }
11585
11586 if (!hooks_run)
11587 {
11588 /* Run the Lucid hook. */
11589 safe_run_hooks (Qactivate_menubar_hook);
11590
11591 /* If it has changed current-menubar from previous value,
11592 really recompute the menu-bar from the value. */
11593 if (! NILP (Vlucid_menu_bar_dirty_flag))
11594 call0 (Qrecompute_lucid_menubar);
11595
11596 safe_run_hooks (Qmenu_bar_update_hook);
11597
11598 hooks_run = 1;
11599 }
11600
11601 XSETFRAME (Vmenu_updating_frame, f);
11602 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11603
11604 /* Redisplay the menu bar in case we changed it. */
11605 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11606 || defined (HAVE_NS) || defined (USE_GTK)
11607 if (FRAME_WINDOW_P (f))
11608 {
11609 #if defined (HAVE_NS)
11610 /* All frames on Mac OS share the same menubar. So only
11611 the selected frame should be allowed to set it. */
11612 if (f == SELECTED_FRAME ())
11613 #endif
11614 set_frame_menubar (f, 0, 0);
11615 }
11616 else
11617 /* On a terminal screen, the menu bar is an ordinary screen
11618 line, and this makes it get updated. */
11619 w->update_mode_line = 1;
11620 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11621 /* In the non-toolkit version, the menu bar is an ordinary screen
11622 line, and this makes it get updated. */
11623 w->update_mode_line = 1;
11624 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11625
11626 unbind_to (count, Qnil);
11627 set_buffer_internal_1 (prev);
11628 }
11629 }
11630
11631 return hooks_run;
11632 }
11633
11634 /***********************************************************************
11635 Tool-bars
11636 ***********************************************************************/
11637
11638 #ifdef HAVE_WINDOW_SYSTEM
11639
11640 /* Tool-bar item index of the item on which a mouse button was pressed
11641 or -1. */
11642
11643 int last_tool_bar_item;
11644
11645 /* Select `frame' temporarily without running all the code in
11646 do_switch_frame.
11647 FIXME: Maybe do_switch_frame should be trimmed down similarly
11648 when `norecord' is set. */
11649 static void
11650 fast_set_selected_frame (Lisp_Object frame)
11651 {
11652 if (!EQ (selected_frame, frame))
11653 {
11654 selected_frame = frame;
11655 selected_window = XFRAME (frame)->selected_window;
11656 }
11657 }
11658
11659 /* Update the tool-bar item list for frame F. This has to be done
11660 before we start to fill in any display lines. Called from
11661 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11662 and restore it here. */
11663
11664 static void
11665 update_tool_bar (struct frame *f, int save_match_data)
11666 {
11667 #if defined (USE_GTK) || defined (HAVE_NS)
11668 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11669 #else
11670 int do_update = (WINDOWP (f->tool_bar_window)
11671 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11672 #endif
11673
11674 if (do_update)
11675 {
11676 Lisp_Object window;
11677 struct window *w;
11678
11679 window = FRAME_SELECTED_WINDOW (f);
11680 w = XWINDOW (window);
11681
11682 /* If the user has switched buffers or windows, we need to
11683 recompute to reflect the new bindings. But we'll
11684 recompute when update_mode_lines is set too; that means
11685 that people can use force-mode-line-update to request
11686 that the menu bar be recomputed. The adverse effect on
11687 the rest of the redisplay algorithm is about the same as
11688 windows_or_buffers_changed anyway. */
11689 if (windows_or_buffers_changed
11690 || w->update_mode_line
11691 || update_mode_lines
11692 || window_buffer_changed (w))
11693 {
11694 struct buffer *prev = current_buffer;
11695 ptrdiff_t count = SPECPDL_INDEX ();
11696 Lisp_Object frame, new_tool_bar;
11697 int new_n_tool_bar;
11698 struct gcpro gcpro1;
11699
11700 /* Set current_buffer to the buffer of the selected
11701 window of the frame, so that we get the right local
11702 keymaps. */
11703 set_buffer_internal_1 (XBUFFER (w->contents));
11704
11705 /* Save match data, if we must. */
11706 if (save_match_data)
11707 record_unwind_save_match_data ();
11708
11709 /* Make sure that we don't accidentally use bogus keymaps. */
11710 if (NILP (Voverriding_local_map_menu_flag))
11711 {
11712 specbind (Qoverriding_terminal_local_map, Qnil);
11713 specbind (Qoverriding_local_map, Qnil);
11714 }
11715
11716 GCPRO1 (new_tool_bar);
11717
11718 /* We must temporarily set the selected frame to this frame
11719 before calling tool_bar_items, because the calculation of
11720 the tool-bar keymap uses the selected frame (see
11721 `tool-bar-make-keymap' in tool-bar.el). */
11722 eassert (EQ (selected_window,
11723 /* Since we only explicitly preserve selected_frame,
11724 check that selected_window would be redundant. */
11725 XFRAME (selected_frame)->selected_window));
11726 record_unwind_protect (fast_set_selected_frame, selected_frame);
11727 XSETFRAME (frame, f);
11728 fast_set_selected_frame (frame);
11729
11730 /* Build desired tool-bar items from keymaps. */
11731 new_tool_bar
11732 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11733 &new_n_tool_bar);
11734
11735 /* Redisplay the tool-bar if we changed it. */
11736 if (new_n_tool_bar != f->n_tool_bar_items
11737 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11738 {
11739 /* Redisplay that happens asynchronously due to an expose event
11740 may access f->tool_bar_items. Make sure we update both
11741 variables within BLOCK_INPUT so no such event interrupts. */
11742 block_input ();
11743 fset_tool_bar_items (f, new_tool_bar);
11744 f->n_tool_bar_items = new_n_tool_bar;
11745 w->update_mode_line = 1;
11746 unblock_input ();
11747 }
11748
11749 UNGCPRO;
11750
11751 unbind_to (count, Qnil);
11752 set_buffer_internal_1 (prev);
11753 }
11754 }
11755 }
11756
11757 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11758
11759 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11760 F's desired tool-bar contents. F->tool_bar_items must have
11761 been set up previously by calling prepare_menu_bars. */
11762
11763 static void
11764 build_desired_tool_bar_string (struct frame *f)
11765 {
11766 int i, size, size_needed;
11767 struct gcpro gcpro1, gcpro2, gcpro3;
11768 Lisp_Object image, plist, props;
11769
11770 image = plist = props = Qnil;
11771 GCPRO3 (image, plist, props);
11772
11773 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11774 Otherwise, make a new string. */
11775
11776 /* The size of the string we might be able to reuse. */
11777 size = (STRINGP (f->desired_tool_bar_string)
11778 ? SCHARS (f->desired_tool_bar_string)
11779 : 0);
11780
11781 /* We need one space in the string for each image. */
11782 size_needed = f->n_tool_bar_items;
11783
11784 /* Reuse f->desired_tool_bar_string, if possible. */
11785 if (size < size_needed || NILP (f->desired_tool_bar_string))
11786 fset_desired_tool_bar_string
11787 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11788 else
11789 {
11790 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11791 Fremove_text_properties (make_number (0), make_number (size),
11792 props, f->desired_tool_bar_string);
11793 }
11794
11795 /* Put a `display' property on the string for the images to display,
11796 put a `menu_item' property on tool-bar items with a value that
11797 is the index of the item in F's tool-bar item vector. */
11798 for (i = 0; i < f->n_tool_bar_items; ++i)
11799 {
11800 #define PROP(IDX) \
11801 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11802
11803 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11804 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11805 int hmargin, vmargin, relief, idx, end;
11806
11807 /* If image is a vector, choose the image according to the
11808 button state. */
11809 image = PROP (TOOL_BAR_ITEM_IMAGES);
11810 if (VECTORP (image))
11811 {
11812 if (enabled_p)
11813 idx = (selected_p
11814 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11815 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11816 else
11817 idx = (selected_p
11818 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11819 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11820
11821 eassert (ASIZE (image) >= idx);
11822 image = AREF (image, idx);
11823 }
11824 else
11825 idx = -1;
11826
11827 /* Ignore invalid image specifications. */
11828 if (!valid_image_p (image))
11829 continue;
11830
11831 /* Display the tool-bar button pressed, or depressed. */
11832 plist = Fcopy_sequence (XCDR (image));
11833
11834 /* Compute margin and relief to draw. */
11835 relief = (tool_bar_button_relief >= 0
11836 ? tool_bar_button_relief
11837 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11838 hmargin = vmargin = relief;
11839
11840 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11841 INT_MAX - max (hmargin, vmargin)))
11842 {
11843 hmargin += XFASTINT (Vtool_bar_button_margin);
11844 vmargin += XFASTINT (Vtool_bar_button_margin);
11845 }
11846 else if (CONSP (Vtool_bar_button_margin))
11847 {
11848 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11849 INT_MAX - hmargin))
11850 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11851
11852 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11853 INT_MAX - vmargin))
11854 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11855 }
11856
11857 if (auto_raise_tool_bar_buttons_p)
11858 {
11859 /* Add a `:relief' property to the image spec if the item is
11860 selected. */
11861 if (selected_p)
11862 {
11863 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11864 hmargin -= relief;
11865 vmargin -= relief;
11866 }
11867 }
11868 else
11869 {
11870 /* If image is selected, display it pressed, i.e. with a
11871 negative relief. If it's not selected, display it with a
11872 raised relief. */
11873 plist = Fplist_put (plist, QCrelief,
11874 (selected_p
11875 ? make_number (-relief)
11876 : make_number (relief)));
11877 hmargin -= relief;
11878 vmargin -= relief;
11879 }
11880
11881 /* Put a margin around the image. */
11882 if (hmargin || vmargin)
11883 {
11884 if (hmargin == vmargin)
11885 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11886 else
11887 plist = Fplist_put (plist, QCmargin,
11888 Fcons (make_number (hmargin),
11889 make_number (vmargin)));
11890 }
11891
11892 /* If button is not enabled, and we don't have special images
11893 for the disabled state, make the image appear disabled by
11894 applying an appropriate algorithm to it. */
11895 if (!enabled_p && idx < 0)
11896 plist = Fplist_put (plist, QCconversion, Qdisabled);
11897
11898 /* Put a `display' text property on the string for the image to
11899 display. Put a `menu-item' property on the string that gives
11900 the start of this item's properties in the tool-bar items
11901 vector. */
11902 image = Fcons (Qimage, plist);
11903 props = list4 (Qdisplay, image,
11904 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11905
11906 /* Let the last image hide all remaining spaces in the tool bar
11907 string. The string can be longer than needed when we reuse a
11908 previous string. */
11909 if (i + 1 == f->n_tool_bar_items)
11910 end = SCHARS (f->desired_tool_bar_string);
11911 else
11912 end = i + 1;
11913 Fadd_text_properties (make_number (i), make_number (end),
11914 props, f->desired_tool_bar_string);
11915 #undef PROP
11916 }
11917
11918 UNGCPRO;
11919 }
11920
11921
11922 /* Display one line of the tool-bar of frame IT->f.
11923
11924 HEIGHT specifies the desired height of the tool-bar line.
11925 If the actual height of the glyph row is less than HEIGHT, the
11926 row's height is increased to HEIGHT, and the icons are centered
11927 vertically in the new height.
11928
11929 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11930 count a final empty row in case the tool-bar width exactly matches
11931 the window width.
11932 */
11933
11934 static void
11935 display_tool_bar_line (struct it *it, int height)
11936 {
11937 struct glyph_row *row = it->glyph_row;
11938 int max_x = it->last_visible_x;
11939 struct glyph *last;
11940
11941 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11942 clear_glyph_row (row);
11943 row->enabled_p = true;
11944 row->y = it->current_y;
11945
11946 /* Note that this isn't made use of if the face hasn't a box,
11947 so there's no need to check the face here. */
11948 it->start_of_box_run_p = 1;
11949
11950 while (it->current_x < max_x)
11951 {
11952 int x, n_glyphs_before, i, nglyphs;
11953 struct it it_before;
11954
11955 /* Get the next display element. */
11956 if (!get_next_display_element (it))
11957 {
11958 /* Don't count empty row if we are counting needed tool-bar lines. */
11959 if (height < 0 && !it->hpos)
11960 return;
11961 break;
11962 }
11963
11964 /* Produce glyphs. */
11965 n_glyphs_before = row->used[TEXT_AREA];
11966 it_before = *it;
11967
11968 PRODUCE_GLYPHS (it);
11969
11970 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11971 i = 0;
11972 x = it_before.current_x;
11973 while (i < nglyphs)
11974 {
11975 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11976
11977 if (x + glyph->pixel_width > max_x)
11978 {
11979 /* Glyph doesn't fit on line. Backtrack. */
11980 row->used[TEXT_AREA] = n_glyphs_before;
11981 *it = it_before;
11982 /* If this is the only glyph on this line, it will never fit on the
11983 tool-bar, so skip it. But ensure there is at least one glyph,
11984 so we don't accidentally disable the tool-bar. */
11985 if (n_glyphs_before == 0
11986 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11987 break;
11988 goto out;
11989 }
11990
11991 ++it->hpos;
11992 x += glyph->pixel_width;
11993 ++i;
11994 }
11995
11996 /* Stop at line end. */
11997 if (ITERATOR_AT_END_OF_LINE_P (it))
11998 break;
11999
12000 set_iterator_to_next (it, 1);
12001 }
12002
12003 out:;
12004
12005 row->displays_text_p = row->used[TEXT_AREA] != 0;
12006
12007 /* Use default face for the border below the tool bar.
12008
12009 FIXME: When auto-resize-tool-bars is grow-only, there is
12010 no additional border below the possibly empty tool-bar lines.
12011 So to make the extra empty lines look "normal", we have to
12012 use the tool-bar face for the border too. */
12013 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12014 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12015 it->face_id = DEFAULT_FACE_ID;
12016
12017 extend_face_to_end_of_line (it);
12018 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12019 last->right_box_line_p = 1;
12020 if (last == row->glyphs[TEXT_AREA])
12021 last->left_box_line_p = 1;
12022
12023 /* Make line the desired height and center it vertically. */
12024 if ((height -= it->max_ascent + it->max_descent) > 0)
12025 {
12026 /* Don't add more than one line height. */
12027 height %= FRAME_LINE_HEIGHT (it->f);
12028 it->max_ascent += height / 2;
12029 it->max_descent += (height + 1) / 2;
12030 }
12031
12032 compute_line_metrics (it);
12033
12034 /* If line is empty, make it occupy the rest of the tool-bar. */
12035 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12036 {
12037 row->height = row->phys_height = it->last_visible_y - row->y;
12038 row->visible_height = row->height;
12039 row->ascent = row->phys_ascent = 0;
12040 row->extra_line_spacing = 0;
12041 }
12042
12043 row->full_width_p = 1;
12044 row->continued_p = 0;
12045 row->truncated_on_left_p = 0;
12046 row->truncated_on_right_p = 0;
12047
12048 it->current_x = it->hpos = 0;
12049 it->current_y += row->height;
12050 ++it->vpos;
12051 ++it->glyph_row;
12052 }
12053
12054
12055 /* Max tool-bar height. Basically, this is what makes all other windows
12056 disappear when the frame gets too small. Rethink this! */
12057
12058 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12059 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12060
12061 /* Value is the number of pixels needed to make all tool-bar items of
12062 frame F visible. The actual number of glyph rows needed is
12063 returned in *N_ROWS if non-NULL. */
12064
12065 static int
12066 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12067 {
12068 struct window *w = XWINDOW (f->tool_bar_window);
12069 struct it it;
12070 /* tool_bar_height is called from redisplay_tool_bar after building
12071 the desired matrix, so use (unused) mode-line row as temporary row to
12072 avoid destroying the first tool-bar row. */
12073 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12074
12075 /* Initialize an iterator for iteration over
12076 F->desired_tool_bar_string in the tool-bar window of frame F. */
12077 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12078 it.first_visible_x = 0;
12079 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12080 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12081 it.paragraph_embedding = L2R;
12082
12083 while (!ITERATOR_AT_END_P (&it))
12084 {
12085 clear_glyph_row (temp_row);
12086 it.glyph_row = temp_row;
12087 display_tool_bar_line (&it, -1);
12088 }
12089 clear_glyph_row (temp_row);
12090
12091 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12092 if (n_rows)
12093 *n_rows = it.vpos > 0 ? it.vpos : -1;
12094
12095 if (pixelwise)
12096 return it.current_y;
12097 else
12098 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12099 }
12100
12101 #endif /* !USE_GTK && !HAVE_NS */
12102
12103 #if defined USE_GTK || defined HAVE_NS
12104 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12105 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12106 #endif
12107
12108 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12109 0, 2, 0,
12110 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12111 If FRAME is nil or omitted, use the selected frame. Optional argument
12112 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12113 (Lisp_Object frame, Lisp_Object pixelwise)
12114 {
12115 int height = 0;
12116
12117 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12118 struct frame *f = decode_any_frame (frame);
12119
12120 if (WINDOWP (f->tool_bar_window)
12121 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12122 {
12123 update_tool_bar (f, 1);
12124 if (f->n_tool_bar_items)
12125 {
12126 build_desired_tool_bar_string (f);
12127 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12128 }
12129 }
12130 #endif
12131
12132 return make_number (height);
12133 }
12134
12135
12136 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12137 height should be changed. */
12138
12139 static int
12140 redisplay_tool_bar (struct frame *f)
12141 {
12142 #if defined (USE_GTK) || defined (HAVE_NS)
12143
12144 if (FRAME_EXTERNAL_TOOL_BAR (f))
12145 update_frame_tool_bar (f);
12146 return 0;
12147
12148 #else /* !USE_GTK && !HAVE_NS */
12149
12150 struct window *w;
12151 struct it it;
12152 struct glyph_row *row;
12153
12154 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12155 do anything. This means you must start with tool-bar-lines
12156 non-zero to get the auto-sizing effect. Or in other words, you
12157 can turn off tool-bars by specifying tool-bar-lines zero. */
12158 if (!WINDOWP (f->tool_bar_window)
12159 || (w = XWINDOW (f->tool_bar_window),
12160 WINDOW_PIXEL_HEIGHT (w) == 0))
12161 return 0;
12162
12163 /* Set up an iterator for the tool-bar window. */
12164 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12165 it.first_visible_x = 0;
12166 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12167 row = it.glyph_row;
12168
12169 /* Build a string that represents the contents of the tool-bar. */
12170 build_desired_tool_bar_string (f);
12171 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12172 /* FIXME: This should be controlled by a user option. But it
12173 doesn't make sense to have an R2L tool bar if the menu bar cannot
12174 be drawn also R2L, and making the menu bar R2L is tricky due
12175 toolkit-specific code that implements it. If an R2L tool bar is
12176 ever supported, display_tool_bar_line should also be augmented to
12177 call unproduce_glyphs like display_line and display_string
12178 do. */
12179 it.paragraph_embedding = L2R;
12180
12181 if (f->n_tool_bar_rows == 0)
12182 {
12183 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12184
12185 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12186 {
12187 Lisp_Object frame;
12188 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12189 / FRAME_LINE_HEIGHT (f));
12190
12191 XSETFRAME (frame, f);
12192 Fmodify_frame_parameters (frame,
12193 list1 (Fcons (Qtool_bar_lines,
12194 make_number (new_lines))));
12195 /* Always do that now. */
12196 clear_glyph_matrix (w->desired_matrix);
12197 f->fonts_changed = 1;
12198 return 1;
12199 }
12200 }
12201
12202 /* Display as many lines as needed to display all tool-bar items. */
12203
12204 if (f->n_tool_bar_rows > 0)
12205 {
12206 int border, rows, height, extra;
12207
12208 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12209 border = XINT (Vtool_bar_border);
12210 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12211 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12212 else if (EQ (Vtool_bar_border, Qborder_width))
12213 border = f->border_width;
12214 else
12215 border = 0;
12216 if (border < 0)
12217 border = 0;
12218
12219 rows = f->n_tool_bar_rows;
12220 height = max (1, (it.last_visible_y - border) / rows);
12221 extra = it.last_visible_y - border - height * rows;
12222
12223 while (it.current_y < it.last_visible_y)
12224 {
12225 int h = 0;
12226 if (extra > 0 && rows-- > 0)
12227 {
12228 h = (extra + rows - 1) / rows;
12229 extra -= h;
12230 }
12231 display_tool_bar_line (&it, height + h);
12232 }
12233 }
12234 else
12235 {
12236 while (it.current_y < it.last_visible_y)
12237 display_tool_bar_line (&it, 0);
12238 }
12239
12240 /* It doesn't make much sense to try scrolling in the tool-bar
12241 window, so don't do it. */
12242 w->desired_matrix->no_scrolling_p = 1;
12243 w->must_be_updated_p = 1;
12244
12245 if (!NILP (Vauto_resize_tool_bars))
12246 {
12247 /* Do we really allow the toolbar to occupy the whole frame? */
12248 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12249 int change_height_p = 0;
12250
12251 /* If we couldn't display everything, change the tool-bar's
12252 height if there is room for more. */
12253 if (IT_STRING_CHARPOS (it) < it.end_charpos
12254 && it.current_y < max_tool_bar_height)
12255 change_height_p = 1;
12256
12257 /* We subtract 1 because display_tool_bar_line advances the
12258 glyph_row pointer before returning to its caller. We want to
12259 examine the last glyph row produced by
12260 display_tool_bar_line. */
12261 row = it.glyph_row - 1;
12262
12263 /* If there are blank lines at the end, except for a partially
12264 visible blank line at the end that is smaller than
12265 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12266 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12267 && row->height >= FRAME_LINE_HEIGHT (f))
12268 change_height_p = 1;
12269
12270 /* If row displays tool-bar items, but is partially visible,
12271 change the tool-bar's height. */
12272 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12273 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12274 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12275 change_height_p = 1;
12276
12277 /* Resize windows as needed by changing the `tool-bar-lines'
12278 frame parameter. */
12279 if (change_height_p)
12280 {
12281 Lisp_Object frame;
12282 int nrows;
12283 int new_height = tool_bar_height (f, &nrows, 1);
12284
12285 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12286 && !f->minimize_tool_bar_window_p)
12287 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12288 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12289 f->minimize_tool_bar_window_p = 0;
12290
12291 if (change_height_p)
12292 {
12293 /* Current size of the tool-bar window in canonical line
12294 units. */
12295 int old_lines = WINDOW_TOTAL_LINES (w);
12296 /* Required size of the tool-bar window in canonical
12297 line units. */
12298 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12299 / FRAME_LINE_HEIGHT (f));
12300 /* Maximum size of the tool-bar window in canonical line
12301 units that this frame can allow. */
12302 int max_lines =
12303 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12304
12305 /* Don't try to change the tool-bar window size and set
12306 the fonts_changed flag unless really necessary. That
12307 flag causes redisplay to give up and retry
12308 redisplaying the frame from scratch, so setting it
12309 unnecessarily can lead to nasty redisplay loops. */
12310 if (new_lines <= max_lines
12311 && eabs (new_lines - old_lines) >= 1)
12312 {
12313 XSETFRAME (frame, f);
12314 Fmodify_frame_parameters (frame,
12315 list1 (Fcons (Qtool_bar_lines,
12316 make_number (new_lines))));
12317 clear_glyph_matrix (w->desired_matrix);
12318 f->n_tool_bar_rows = nrows;
12319 f->fonts_changed = 1;
12320 return 1;
12321 }
12322 }
12323 }
12324 }
12325
12326 f->minimize_tool_bar_window_p = 0;
12327 return 0;
12328
12329 #endif /* USE_GTK || HAVE_NS */
12330 }
12331
12332 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12333
12334 /* Get information about the tool-bar item which is displayed in GLYPH
12335 on frame F. Return in *PROP_IDX the index where tool-bar item
12336 properties start in F->tool_bar_items. Value is zero if
12337 GLYPH doesn't display a tool-bar item. */
12338
12339 static int
12340 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12341 {
12342 Lisp_Object prop;
12343 int success_p;
12344 int charpos;
12345
12346 /* This function can be called asynchronously, which means we must
12347 exclude any possibility that Fget_text_property signals an
12348 error. */
12349 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12350 charpos = max (0, charpos);
12351
12352 /* Get the text property `menu-item' at pos. The value of that
12353 property is the start index of this item's properties in
12354 F->tool_bar_items. */
12355 prop = Fget_text_property (make_number (charpos),
12356 Qmenu_item, f->current_tool_bar_string);
12357 if (INTEGERP (prop))
12358 {
12359 *prop_idx = XINT (prop);
12360 success_p = 1;
12361 }
12362 else
12363 success_p = 0;
12364
12365 return success_p;
12366 }
12367
12368 \f
12369 /* Get information about the tool-bar item at position X/Y on frame F.
12370 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12371 the current matrix of the tool-bar window of F, or NULL if not
12372 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12373 item in F->tool_bar_items. Value is
12374
12375 -1 if X/Y is not on a tool-bar item
12376 0 if X/Y is on the same item that was highlighted before.
12377 1 otherwise. */
12378
12379 static int
12380 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12381 int *hpos, int *vpos, int *prop_idx)
12382 {
12383 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12384 struct window *w = XWINDOW (f->tool_bar_window);
12385 int area;
12386
12387 /* Find the glyph under X/Y. */
12388 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12389 if (*glyph == NULL)
12390 return -1;
12391
12392 /* Get the start of this tool-bar item's properties in
12393 f->tool_bar_items. */
12394 if (!tool_bar_item_info (f, *glyph, prop_idx))
12395 return -1;
12396
12397 /* Is mouse on the highlighted item? */
12398 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12399 && *vpos >= hlinfo->mouse_face_beg_row
12400 && *vpos <= hlinfo->mouse_face_end_row
12401 && (*vpos > hlinfo->mouse_face_beg_row
12402 || *hpos >= hlinfo->mouse_face_beg_col)
12403 && (*vpos < hlinfo->mouse_face_end_row
12404 || *hpos < hlinfo->mouse_face_end_col
12405 || hlinfo->mouse_face_past_end))
12406 return 0;
12407
12408 return 1;
12409 }
12410
12411
12412 /* EXPORT:
12413 Handle mouse button event on the tool-bar of frame F, at
12414 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12415 0 for button release. MODIFIERS is event modifiers for button
12416 release. */
12417
12418 void
12419 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12420 int modifiers)
12421 {
12422 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12423 struct window *w = XWINDOW (f->tool_bar_window);
12424 int hpos, vpos, prop_idx;
12425 struct glyph *glyph;
12426 Lisp_Object enabled_p;
12427 int ts;
12428
12429 /* If not on the highlighted tool-bar item, and mouse-highlight is
12430 non-nil, return. This is so we generate the tool-bar button
12431 click only when the mouse button is released on the same item as
12432 where it was pressed. However, when mouse-highlight is disabled,
12433 generate the click when the button is released regardless of the
12434 highlight, since tool-bar items are not highlighted in that
12435 case. */
12436 frame_to_window_pixel_xy (w, &x, &y);
12437 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12438 if (ts == -1
12439 || (ts != 0 && !NILP (Vmouse_highlight)))
12440 return;
12441
12442 /* When mouse-highlight is off, generate the click for the item
12443 where the button was pressed, disregarding where it was
12444 released. */
12445 if (NILP (Vmouse_highlight) && !down_p)
12446 prop_idx = last_tool_bar_item;
12447
12448 /* If item is disabled, do nothing. */
12449 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12450 if (NILP (enabled_p))
12451 return;
12452
12453 if (down_p)
12454 {
12455 /* Show item in pressed state. */
12456 if (!NILP (Vmouse_highlight))
12457 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12458 last_tool_bar_item = prop_idx;
12459 }
12460 else
12461 {
12462 Lisp_Object key, frame;
12463 struct input_event event;
12464 EVENT_INIT (event);
12465
12466 /* Show item in released state. */
12467 if (!NILP (Vmouse_highlight))
12468 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12469
12470 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12471
12472 XSETFRAME (frame, f);
12473 event.kind = TOOL_BAR_EVENT;
12474 event.frame_or_window = frame;
12475 event.arg = frame;
12476 kbd_buffer_store_event (&event);
12477
12478 event.kind = TOOL_BAR_EVENT;
12479 event.frame_or_window = frame;
12480 event.arg = key;
12481 event.modifiers = modifiers;
12482 kbd_buffer_store_event (&event);
12483 last_tool_bar_item = -1;
12484 }
12485 }
12486
12487
12488 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12489 tool-bar window-relative coordinates X/Y. Called from
12490 note_mouse_highlight. */
12491
12492 static void
12493 note_tool_bar_highlight (struct frame *f, int x, int y)
12494 {
12495 Lisp_Object window = f->tool_bar_window;
12496 struct window *w = XWINDOW (window);
12497 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12498 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12499 int hpos, vpos;
12500 struct glyph *glyph;
12501 struct glyph_row *row;
12502 int i;
12503 Lisp_Object enabled_p;
12504 int prop_idx;
12505 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12506 int mouse_down_p, rc;
12507
12508 /* Function note_mouse_highlight is called with negative X/Y
12509 values when mouse moves outside of the frame. */
12510 if (x <= 0 || y <= 0)
12511 {
12512 clear_mouse_face (hlinfo);
12513 return;
12514 }
12515
12516 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12517 if (rc < 0)
12518 {
12519 /* Not on tool-bar item. */
12520 clear_mouse_face (hlinfo);
12521 return;
12522 }
12523 else if (rc == 0)
12524 /* On same tool-bar item as before. */
12525 goto set_help_echo;
12526
12527 clear_mouse_face (hlinfo);
12528
12529 /* Mouse is down, but on different tool-bar item? */
12530 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12531 && f == dpyinfo->last_mouse_frame);
12532
12533 if (mouse_down_p
12534 && last_tool_bar_item != prop_idx)
12535 return;
12536
12537 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12538
12539 /* If tool-bar item is not enabled, don't highlight it. */
12540 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12541 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12542 {
12543 /* Compute the x-position of the glyph. In front and past the
12544 image is a space. We include this in the highlighted area. */
12545 row = MATRIX_ROW (w->current_matrix, vpos);
12546 for (i = x = 0; i < hpos; ++i)
12547 x += row->glyphs[TEXT_AREA][i].pixel_width;
12548
12549 /* Record this as the current active region. */
12550 hlinfo->mouse_face_beg_col = hpos;
12551 hlinfo->mouse_face_beg_row = vpos;
12552 hlinfo->mouse_face_beg_x = x;
12553 hlinfo->mouse_face_past_end = 0;
12554
12555 hlinfo->mouse_face_end_col = hpos + 1;
12556 hlinfo->mouse_face_end_row = vpos;
12557 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12558 hlinfo->mouse_face_window = window;
12559 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12560
12561 /* Display it as active. */
12562 show_mouse_face (hlinfo, draw);
12563 }
12564
12565 set_help_echo:
12566
12567 /* Set help_echo_string to a help string to display for this tool-bar item.
12568 XTread_socket does the rest. */
12569 help_echo_object = help_echo_window = Qnil;
12570 help_echo_pos = -1;
12571 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12572 if (NILP (help_echo_string))
12573 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12574 }
12575
12576 #endif /* !USE_GTK && !HAVE_NS */
12577
12578 #endif /* HAVE_WINDOW_SYSTEM */
12579
12580
12581 \f
12582 /************************************************************************
12583 Horizontal scrolling
12584 ************************************************************************/
12585
12586 static int hscroll_window_tree (Lisp_Object);
12587 static int hscroll_windows (Lisp_Object);
12588
12589 /* For all leaf windows in the window tree rooted at WINDOW, set their
12590 hscroll value so that PT is (i) visible in the window, and (ii) so
12591 that it is not within a certain margin at the window's left and
12592 right border. Value is non-zero if any window's hscroll has been
12593 changed. */
12594
12595 static int
12596 hscroll_window_tree (Lisp_Object window)
12597 {
12598 int hscrolled_p = 0;
12599 int hscroll_relative_p = FLOATP (Vhscroll_step);
12600 int hscroll_step_abs = 0;
12601 double hscroll_step_rel = 0;
12602
12603 if (hscroll_relative_p)
12604 {
12605 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12606 if (hscroll_step_rel < 0)
12607 {
12608 hscroll_relative_p = 0;
12609 hscroll_step_abs = 0;
12610 }
12611 }
12612 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12613 {
12614 hscroll_step_abs = XINT (Vhscroll_step);
12615 if (hscroll_step_abs < 0)
12616 hscroll_step_abs = 0;
12617 }
12618 else
12619 hscroll_step_abs = 0;
12620
12621 while (WINDOWP (window))
12622 {
12623 struct window *w = XWINDOW (window);
12624
12625 if (WINDOWP (w->contents))
12626 hscrolled_p |= hscroll_window_tree (w->contents);
12627 else if (w->cursor.vpos >= 0)
12628 {
12629 int h_margin;
12630 int text_area_width;
12631 struct glyph_row *cursor_row;
12632 struct glyph_row *bottom_row;
12633 int row_r2l_p;
12634
12635 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12636 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12637 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12638 else
12639 cursor_row = bottom_row - 1;
12640
12641 if (!cursor_row->enabled_p)
12642 {
12643 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12644 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12645 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12646 else
12647 cursor_row = bottom_row - 1;
12648 }
12649 row_r2l_p = cursor_row->reversed_p;
12650
12651 text_area_width = window_box_width (w, TEXT_AREA);
12652
12653 /* Scroll when cursor is inside this scroll margin. */
12654 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12655
12656 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12657 /* For left-to-right rows, hscroll when cursor is either
12658 (i) inside the right hscroll margin, or (ii) if it is
12659 inside the left margin and the window is already
12660 hscrolled. */
12661 && ((!row_r2l_p
12662 && ((w->hscroll
12663 && w->cursor.x <= h_margin)
12664 || (cursor_row->enabled_p
12665 && cursor_row->truncated_on_right_p
12666 && (w->cursor.x >= text_area_width - h_margin))))
12667 /* For right-to-left rows, the logic is similar,
12668 except that rules for scrolling to left and right
12669 are reversed. E.g., if cursor.x <= h_margin, we
12670 need to hscroll "to the right" unconditionally,
12671 and that will scroll the screen to the left so as
12672 to reveal the next portion of the row. */
12673 || (row_r2l_p
12674 && ((cursor_row->enabled_p
12675 /* FIXME: It is confusing to set the
12676 truncated_on_right_p flag when R2L rows
12677 are actually truncated on the left. */
12678 && cursor_row->truncated_on_right_p
12679 && w->cursor.x <= h_margin)
12680 || (w->hscroll
12681 && (w->cursor.x >= text_area_width - h_margin))))))
12682 {
12683 struct it it;
12684 ptrdiff_t hscroll;
12685 struct buffer *saved_current_buffer;
12686 ptrdiff_t pt;
12687 int wanted_x;
12688
12689 /* Find point in a display of infinite width. */
12690 saved_current_buffer = current_buffer;
12691 current_buffer = XBUFFER (w->contents);
12692
12693 if (w == XWINDOW (selected_window))
12694 pt = PT;
12695 else
12696 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12697
12698 /* Move iterator to pt starting at cursor_row->start in
12699 a line with infinite width. */
12700 init_to_row_start (&it, w, cursor_row);
12701 it.last_visible_x = INFINITY;
12702 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12703 current_buffer = saved_current_buffer;
12704
12705 /* Position cursor in window. */
12706 if (!hscroll_relative_p && hscroll_step_abs == 0)
12707 hscroll = max (0, (it.current_x
12708 - (ITERATOR_AT_END_OF_LINE_P (&it)
12709 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12710 : (text_area_width / 2))))
12711 / FRAME_COLUMN_WIDTH (it.f);
12712 else if ((!row_r2l_p
12713 && w->cursor.x >= text_area_width - h_margin)
12714 || (row_r2l_p && w->cursor.x <= h_margin))
12715 {
12716 if (hscroll_relative_p)
12717 wanted_x = text_area_width * (1 - hscroll_step_rel)
12718 - h_margin;
12719 else
12720 wanted_x = text_area_width
12721 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12722 - h_margin;
12723 hscroll
12724 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12725 }
12726 else
12727 {
12728 if (hscroll_relative_p)
12729 wanted_x = text_area_width * hscroll_step_rel
12730 + h_margin;
12731 else
12732 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12733 + h_margin;
12734 hscroll
12735 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12736 }
12737 hscroll = max (hscroll, w->min_hscroll);
12738
12739 /* Don't prevent redisplay optimizations if hscroll
12740 hasn't changed, as it will unnecessarily slow down
12741 redisplay. */
12742 if (w->hscroll != hscroll)
12743 {
12744 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12745 w->hscroll = hscroll;
12746 hscrolled_p = 1;
12747 }
12748 }
12749 }
12750
12751 window = w->next;
12752 }
12753
12754 /* Value is non-zero if hscroll of any leaf window has been changed. */
12755 return hscrolled_p;
12756 }
12757
12758
12759 /* Set hscroll so that cursor is visible and not inside horizontal
12760 scroll margins for all windows in the tree rooted at WINDOW. See
12761 also hscroll_window_tree above. Value is non-zero if any window's
12762 hscroll has been changed. If it has, desired matrices on the frame
12763 of WINDOW are cleared. */
12764
12765 static int
12766 hscroll_windows (Lisp_Object window)
12767 {
12768 int hscrolled_p = hscroll_window_tree (window);
12769 if (hscrolled_p)
12770 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12771 return hscrolled_p;
12772 }
12773
12774
12775 \f
12776 /************************************************************************
12777 Redisplay
12778 ************************************************************************/
12779
12780 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12781 to a non-zero value. This is sometimes handy to have in a debugger
12782 session. */
12783
12784 #ifdef GLYPH_DEBUG
12785
12786 /* First and last unchanged row for try_window_id. */
12787
12788 static int debug_first_unchanged_at_end_vpos;
12789 static int debug_last_unchanged_at_beg_vpos;
12790
12791 /* Delta vpos and y. */
12792
12793 static int debug_dvpos, debug_dy;
12794
12795 /* Delta in characters and bytes for try_window_id. */
12796
12797 static ptrdiff_t debug_delta, debug_delta_bytes;
12798
12799 /* Values of window_end_pos and window_end_vpos at the end of
12800 try_window_id. */
12801
12802 static ptrdiff_t debug_end_vpos;
12803
12804 /* Append a string to W->desired_matrix->method. FMT is a printf
12805 format string. If trace_redisplay_p is true also printf the
12806 resulting string to stderr. */
12807
12808 static void debug_method_add (struct window *, char const *, ...)
12809 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12810
12811 static void
12812 debug_method_add (struct window *w, char const *fmt, ...)
12813 {
12814 void *ptr = w;
12815 char *method = w->desired_matrix->method;
12816 int len = strlen (method);
12817 int size = sizeof w->desired_matrix->method;
12818 int remaining = size - len - 1;
12819 va_list ap;
12820
12821 if (len && remaining)
12822 {
12823 method[len] = '|';
12824 --remaining, ++len;
12825 }
12826
12827 va_start (ap, fmt);
12828 vsnprintf (method + len, remaining + 1, fmt, ap);
12829 va_end (ap);
12830
12831 if (trace_redisplay_p)
12832 fprintf (stderr, "%p (%s): %s\n",
12833 ptr,
12834 ((BUFFERP (w->contents)
12835 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12836 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12837 : "no buffer"),
12838 method + len);
12839 }
12840
12841 #endif /* GLYPH_DEBUG */
12842
12843
12844 /* Value is non-zero if all changes in window W, which displays
12845 current_buffer, are in the text between START and END. START is a
12846 buffer position, END is given as a distance from Z. Used in
12847 redisplay_internal for display optimization. */
12848
12849 static int
12850 text_outside_line_unchanged_p (struct window *w,
12851 ptrdiff_t start, ptrdiff_t end)
12852 {
12853 int unchanged_p = 1;
12854
12855 /* If text or overlays have changed, see where. */
12856 if (window_outdated (w))
12857 {
12858 /* Gap in the line? */
12859 if (GPT < start || Z - GPT < end)
12860 unchanged_p = 0;
12861
12862 /* Changes start in front of the line, or end after it? */
12863 if (unchanged_p
12864 && (BEG_UNCHANGED < start - 1
12865 || END_UNCHANGED < end))
12866 unchanged_p = 0;
12867
12868 /* If selective display, can't optimize if changes start at the
12869 beginning of the line. */
12870 if (unchanged_p
12871 && INTEGERP (BVAR (current_buffer, selective_display))
12872 && XINT (BVAR (current_buffer, selective_display)) > 0
12873 && (BEG_UNCHANGED < start || GPT <= start))
12874 unchanged_p = 0;
12875
12876 /* If there are overlays at the start or end of the line, these
12877 may have overlay strings with newlines in them. A change at
12878 START, for instance, may actually concern the display of such
12879 overlay strings as well, and they are displayed on different
12880 lines. So, quickly rule out this case. (For the future, it
12881 might be desirable to implement something more telling than
12882 just BEG/END_UNCHANGED.) */
12883 if (unchanged_p)
12884 {
12885 if (BEG + BEG_UNCHANGED == start
12886 && overlay_touches_p (start))
12887 unchanged_p = 0;
12888 if (END_UNCHANGED == end
12889 && overlay_touches_p (Z - end))
12890 unchanged_p = 0;
12891 }
12892
12893 /* Under bidi reordering, adding or deleting a character in the
12894 beginning of a paragraph, before the first strong directional
12895 character, can change the base direction of the paragraph (unless
12896 the buffer specifies a fixed paragraph direction), which will
12897 require to redisplay the whole paragraph. It might be worthwhile
12898 to find the paragraph limits and widen the range of redisplayed
12899 lines to that, but for now just give up this optimization. */
12900 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12901 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12902 unchanged_p = 0;
12903 }
12904
12905 return unchanged_p;
12906 }
12907
12908
12909 /* Do a frame update, taking possible shortcuts into account. This is
12910 the main external entry point for redisplay.
12911
12912 If the last redisplay displayed an echo area message and that message
12913 is no longer requested, we clear the echo area or bring back the
12914 mini-buffer if that is in use. */
12915
12916 void
12917 redisplay (void)
12918 {
12919 redisplay_internal ();
12920 }
12921
12922
12923 static Lisp_Object
12924 overlay_arrow_string_or_property (Lisp_Object var)
12925 {
12926 Lisp_Object val;
12927
12928 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12929 return val;
12930
12931 return Voverlay_arrow_string;
12932 }
12933
12934 /* Return 1 if there are any overlay-arrows in current_buffer. */
12935 static int
12936 overlay_arrow_in_current_buffer_p (void)
12937 {
12938 Lisp_Object vlist;
12939
12940 for (vlist = Voverlay_arrow_variable_list;
12941 CONSP (vlist);
12942 vlist = XCDR (vlist))
12943 {
12944 Lisp_Object var = XCAR (vlist);
12945 Lisp_Object val;
12946
12947 if (!SYMBOLP (var))
12948 continue;
12949 val = find_symbol_value (var);
12950 if (MARKERP (val)
12951 && current_buffer == XMARKER (val)->buffer)
12952 return 1;
12953 }
12954 return 0;
12955 }
12956
12957
12958 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12959 has changed. */
12960
12961 static int
12962 overlay_arrows_changed_p (void)
12963 {
12964 Lisp_Object vlist;
12965
12966 for (vlist = Voverlay_arrow_variable_list;
12967 CONSP (vlist);
12968 vlist = XCDR (vlist))
12969 {
12970 Lisp_Object var = XCAR (vlist);
12971 Lisp_Object val, pstr;
12972
12973 if (!SYMBOLP (var))
12974 continue;
12975 val = find_symbol_value (var);
12976 if (!MARKERP (val))
12977 continue;
12978 if (! EQ (COERCE_MARKER (val),
12979 Fget (var, Qlast_arrow_position))
12980 || ! (pstr = overlay_arrow_string_or_property (var),
12981 EQ (pstr, Fget (var, Qlast_arrow_string))))
12982 return 1;
12983 }
12984 return 0;
12985 }
12986
12987 /* Mark overlay arrows to be updated on next redisplay. */
12988
12989 static void
12990 update_overlay_arrows (int up_to_date)
12991 {
12992 Lisp_Object vlist;
12993
12994 for (vlist = Voverlay_arrow_variable_list;
12995 CONSP (vlist);
12996 vlist = XCDR (vlist))
12997 {
12998 Lisp_Object var = XCAR (vlist);
12999
13000 if (!SYMBOLP (var))
13001 continue;
13002
13003 if (up_to_date > 0)
13004 {
13005 Lisp_Object val = find_symbol_value (var);
13006 Fput (var, Qlast_arrow_position,
13007 COERCE_MARKER (val));
13008 Fput (var, Qlast_arrow_string,
13009 overlay_arrow_string_or_property (var));
13010 }
13011 else if (up_to_date < 0
13012 || !NILP (Fget (var, Qlast_arrow_position)))
13013 {
13014 Fput (var, Qlast_arrow_position, Qt);
13015 Fput (var, Qlast_arrow_string, Qt);
13016 }
13017 }
13018 }
13019
13020
13021 /* Return overlay arrow string to display at row.
13022 Return integer (bitmap number) for arrow bitmap in left fringe.
13023 Return nil if no overlay arrow. */
13024
13025 static Lisp_Object
13026 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13027 {
13028 Lisp_Object vlist;
13029
13030 for (vlist = Voverlay_arrow_variable_list;
13031 CONSP (vlist);
13032 vlist = XCDR (vlist))
13033 {
13034 Lisp_Object var = XCAR (vlist);
13035 Lisp_Object val;
13036
13037 if (!SYMBOLP (var))
13038 continue;
13039
13040 val = find_symbol_value (var);
13041
13042 if (MARKERP (val)
13043 && current_buffer == XMARKER (val)->buffer
13044 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13045 {
13046 if (FRAME_WINDOW_P (it->f)
13047 /* FIXME: if ROW->reversed_p is set, this should test
13048 the right fringe, not the left one. */
13049 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13050 {
13051 #ifdef HAVE_WINDOW_SYSTEM
13052 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13053 {
13054 int fringe_bitmap;
13055 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13056 return make_number (fringe_bitmap);
13057 }
13058 #endif
13059 return make_number (-1); /* Use default arrow bitmap. */
13060 }
13061 return overlay_arrow_string_or_property (var);
13062 }
13063 }
13064
13065 return Qnil;
13066 }
13067
13068 /* Return 1 if point moved out of or into a composition. Otherwise
13069 return 0. PREV_BUF and PREV_PT are the last point buffer and
13070 position. BUF and PT are the current point buffer and position. */
13071
13072 static int
13073 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13074 struct buffer *buf, ptrdiff_t pt)
13075 {
13076 ptrdiff_t start, end;
13077 Lisp_Object prop;
13078 Lisp_Object buffer;
13079
13080 XSETBUFFER (buffer, buf);
13081 /* Check a composition at the last point if point moved within the
13082 same buffer. */
13083 if (prev_buf == buf)
13084 {
13085 if (prev_pt == pt)
13086 /* Point didn't move. */
13087 return 0;
13088
13089 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13090 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13091 && composition_valid_p (start, end, prop)
13092 && start < prev_pt && end > prev_pt)
13093 /* The last point was within the composition. Return 1 iff
13094 point moved out of the composition. */
13095 return (pt <= start || pt >= end);
13096 }
13097
13098 /* Check a composition at the current point. */
13099 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13100 && find_composition (pt, -1, &start, &end, &prop, buffer)
13101 && composition_valid_p (start, end, prop)
13102 && start < pt && end > pt);
13103 }
13104
13105 /* Reconsider the clip changes of buffer which is displayed in W. */
13106
13107 static void
13108 reconsider_clip_changes (struct window *w)
13109 {
13110 struct buffer *b = XBUFFER (w->contents);
13111
13112 if (b->clip_changed
13113 && w->window_end_valid
13114 && w->current_matrix->buffer == b
13115 && w->current_matrix->zv == BUF_ZV (b)
13116 && w->current_matrix->begv == BUF_BEGV (b))
13117 b->clip_changed = 0;
13118
13119 /* If display wasn't paused, and W is not a tool bar window, see if
13120 point has been moved into or out of a composition. In that case,
13121 we set b->clip_changed to 1 to force updating the screen. If
13122 b->clip_changed has already been set to 1, we can skip this
13123 check. */
13124 if (!b->clip_changed && w->window_end_valid)
13125 {
13126 ptrdiff_t pt = (w == XWINDOW (selected_window)
13127 ? PT : marker_position (w->pointm));
13128
13129 if ((w->current_matrix->buffer != b || pt != w->last_point)
13130 && check_point_in_composition (w->current_matrix->buffer,
13131 w->last_point, b, pt))
13132 b->clip_changed = 1;
13133 }
13134 }
13135
13136 static void
13137 propagate_buffer_redisplay (void)
13138 { /* Resetting b->text->redisplay is problematic!
13139 We can't just reset it in the case that some window that displays
13140 it has not been redisplayed; and such a window can stay
13141 unredisplayed for a long time if it's currently invisible.
13142 But we do want to reset it at the end of redisplay otherwise
13143 its displayed windows will keep being redisplayed over and over
13144 again.
13145 So we copy all b->text->redisplay flags up to their windows here,
13146 such that mark_window_display_accurate can safely reset
13147 b->text->redisplay. */
13148 Lisp_Object ws = window_list ();
13149 for (; CONSP (ws); ws = XCDR (ws))
13150 {
13151 struct window *thisw = XWINDOW (XCAR (ws));
13152 struct buffer *thisb = XBUFFER (thisw->contents);
13153 if (thisb->text->redisplay)
13154 thisw->redisplay = true;
13155 }
13156 }
13157
13158 #define STOP_POLLING \
13159 do { if (! polling_stopped_here) stop_polling (); \
13160 polling_stopped_here = 1; } while (0)
13161
13162 #define RESUME_POLLING \
13163 do { if (polling_stopped_here) start_polling (); \
13164 polling_stopped_here = 0; } while (0)
13165
13166
13167 /* Perhaps in the future avoid recentering windows if it
13168 is not necessary; currently that causes some problems. */
13169
13170 static void
13171 redisplay_internal (void)
13172 {
13173 struct window *w = XWINDOW (selected_window);
13174 struct window *sw;
13175 struct frame *fr;
13176 int pending;
13177 bool must_finish = 0, match_p;
13178 struct text_pos tlbufpos, tlendpos;
13179 int number_of_visible_frames;
13180 ptrdiff_t count;
13181 struct frame *sf;
13182 int polling_stopped_here = 0;
13183 Lisp_Object tail, frame;
13184
13185 /* True means redisplay has to consider all windows on all
13186 frames. False, only selected_window is considered. */
13187 bool consider_all_windows_p;
13188
13189 /* True means redisplay has to redisplay the miniwindow. */
13190 bool update_miniwindow_p = false;
13191
13192 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13193
13194 /* No redisplay if running in batch mode or frame is not yet fully
13195 initialized, or redisplay is explicitly turned off by setting
13196 Vinhibit_redisplay. */
13197 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13198 || !NILP (Vinhibit_redisplay))
13199 return;
13200
13201 /* Don't examine these until after testing Vinhibit_redisplay.
13202 When Emacs is shutting down, perhaps because its connection to
13203 X has dropped, we should not look at them at all. */
13204 fr = XFRAME (w->frame);
13205 sf = SELECTED_FRAME ();
13206
13207 if (!fr->glyphs_initialized_p)
13208 return;
13209
13210 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13211 if (popup_activated ())
13212 return;
13213 #endif
13214
13215 /* I don't think this happens but let's be paranoid. */
13216 if (redisplaying_p)
13217 return;
13218
13219 /* Record a function that clears redisplaying_p
13220 when we leave this function. */
13221 count = SPECPDL_INDEX ();
13222 record_unwind_protect_void (unwind_redisplay);
13223 redisplaying_p = 1;
13224 specbind (Qinhibit_free_realized_faces, Qnil);
13225
13226 /* Record this function, so it appears on the profiler's backtraces. */
13227 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13228
13229 FOR_EACH_FRAME (tail, frame)
13230 XFRAME (frame)->already_hscrolled_p = 0;
13231
13232 retry:
13233 /* Remember the currently selected window. */
13234 sw = w;
13235
13236 pending = 0;
13237 last_escape_glyph_frame = NULL;
13238 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13239 last_glyphless_glyph_frame = NULL;
13240 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13241
13242 /* If face_change_count is non-zero, init_iterator will free all
13243 realized faces, which includes the faces referenced from current
13244 matrices. So, we can't reuse current matrices in this case. */
13245 if (face_change_count)
13246 windows_or_buffers_changed = 47;
13247
13248 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13249 && FRAME_TTY (sf)->previous_frame != sf)
13250 {
13251 /* Since frames on a single ASCII terminal share the same
13252 display area, displaying a different frame means redisplay
13253 the whole thing. */
13254 SET_FRAME_GARBAGED (sf);
13255 #ifndef DOS_NT
13256 set_tty_color_mode (FRAME_TTY (sf), sf);
13257 #endif
13258 FRAME_TTY (sf)->previous_frame = sf;
13259 }
13260
13261 /* Set the visible flags for all frames. Do this before checking for
13262 resized or garbaged frames; they want to know if their frames are
13263 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13264 number_of_visible_frames = 0;
13265
13266 FOR_EACH_FRAME (tail, frame)
13267 {
13268 struct frame *f = XFRAME (frame);
13269
13270 if (FRAME_VISIBLE_P (f))
13271 {
13272 ++number_of_visible_frames;
13273 /* Adjust matrices for visible frames only. */
13274 if (f->fonts_changed)
13275 {
13276 adjust_frame_glyphs (f);
13277 f->fonts_changed = 0;
13278 }
13279 /* If cursor type has been changed on the frame
13280 other than selected, consider all frames. */
13281 if (f != sf && f->cursor_type_changed)
13282 update_mode_lines = 31;
13283 }
13284 clear_desired_matrices (f);
13285 }
13286
13287 /* Notice any pending interrupt request to change frame size. */
13288 do_pending_window_change (1);
13289
13290 /* do_pending_window_change could change the selected_window due to
13291 frame resizing which makes the selected window too small. */
13292 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13293 sw = w;
13294
13295 /* Clear frames marked as garbaged. */
13296 clear_garbaged_frames ();
13297
13298 /* Build menubar and tool-bar items. */
13299 if (NILP (Vmemory_full))
13300 prepare_menu_bars ();
13301
13302 reconsider_clip_changes (w);
13303
13304 /* In most cases selected window displays current buffer. */
13305 match_p = XBUFFER (w->contents) == current_buffer;
13306 if (match_p)
13307 {
13308 /* Detect case that we need to write or remove a star in the mode line. */
13309 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13310 w->update_mode_line = 1;
13311
13312 if (mode_line_update_needed (w))
13313 w->update_mode_line = 1;
13314 }
13315
13316 /* Normally the message* functions will have already displayed and
13317 updated the echo area, but the frame may have been trashed, or
13318 the update may have been preempted, so display the echo area
13319 again here. Checking message_cleared_p captures the case that
13320 the echo area should be cleared. */
13321 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13322 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13323 || (message_cleared_p
13324 && minibuf_level == 0
13325 /* If the mini-window is currently selected, this means the
13326 echo-area doesn't show through. */
13327 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13328 {
13329 int window_height_changed_p = echo_area_display (0);
13330
13331 if (message_cleared_p)
13332 update_miniwindow_p = true;
13333
13334 must_finish = 1;
13335
13336 /* If we don't display the current message, don't clear the
13337 message_cleared_p flag, because, if we did, we wouldn't clear
13338 the echo area in the next redisplay which doesn't preserve
13339 the echo area. */
13340 if (!display_last_displayed_message_p)
13341 message_cleared_p = 0;
13342
13343 if (window_height_changed_p)
13344 {
13345 windows_or_buffers_changed = 50;
13346
13347 /* If window configuration was changed, frames may have been
13348 marked garbaged. Clear them or we will experience
13349 surprises wrt scrolling. */
13350 clear_garbaged_frames ();
13351 }
13352 }
13353 else if (EQ (selected_window, minibuf_window)
13354 && (current_buffer->clip_changed || window_outdated (w))
13355 && resize_mini_window (w, 0))
13356 {
13357 /* Resized active mini-window to fit the size of what it is
13358 showing if its contents might have changed. */
13359 must_finish = 1;
13360
13361 /* If window configuration was changed, frames may have been
13362 marked garbaged. Clear them or we will experience
13363 surprises wrt scrolling. */
13364 clear_garbaged_frames ();
13365 }
13366
13367 if (windows_or_buffers_changed && !update_mode_lines)
13368 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13369 only the windows's contents needs to be refreshed, or whether the
13370 mode-lines also need a refresh. */
13371 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13372 ? REDISPLAY_SOME : 32);
13373
13374 /* If specs for an arrow have changed, do thorough redisplay
13375 to ensure we remove any arrow that should no longer exist. */
13376 if (overlay_arrows_changed_p ())
13377 /* Apparently, this is the only case where we update other windows,
13378 without updating other mode-lines. */
13379 windows_or_buffers_changed = 49;
13380
13381 consider_all_windows_p = (update_mode_lines
13382 || windows_or_buffers_changed);
13383
13384 #define AINC(a,i) \
13385 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13386 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13387
13388 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13389 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13390
13391 /* Optimize the case that only the line containing the cursor in the
13392 selected window has changed. Variables starting with this_ are
13393 set in display_line and record information about the line
13394 containing the cursor. */
13395 tlbufpos = this_line_start_pos;
13396 tlendpos = this_line_end_pos;
13397 if (!consider_all_windows_p
13398 && CHARPOS (tlbufpos) > 0
13399 && !w->update_mode_line
13400 && !current_buffer->clip_changed
13401 && !current_buffer->prevent_redisplay_optimizations_p
13402 && FRAME_VISIBLE_P (XFRAME (w->frame))
13403 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13404 && !XFRAME (w->frame)->cursor_type_changed
13405 /* Make sure recorded data applies to current buffer, etc. */
13406 && this_line_buffer == current_buffer
13407 && match_p
13408 && !w->force_start
13409 && !w->optional_new_start
13410 /* Point must be on the line that we have info recorded about. */
13411 && PT >= CHARPOS (tlbufpos)
13412 && PT <= Z - CHARPOS (tlendpos)
13413 /* All text outside that line, including its final newline,
13414 must be unchanged. */
13415 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13416 CHARPOS (tlendpos)))
13417 {
13418 if (CHARPOS (tlbufpos) > BEGV
13419 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13420 && (CHARPOS (tlbufpos) == ZV
13421 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13422 /* Former continuation line has disappeared by becoming empty. */
13423 goto cancel;
13424 else if (window_outdated (w) || MINI_WINDOW_P (w))
13425 {
13426 /* We have to handle the case of continuation around a
13427 wide-column character (see the comment in indent.c around
13428 line 1340).
13429
13430 For instance, in the following case:
13431
13432 -------- Insert --------
13433 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13434 J_I_ ==> J_I_ `^^' are cursors.
13435 ^^ ^^
13436 -------- --------
13437
13438 As we have to redraw the line above, we cannot use this
13439 optimization. */
13440
13441 struct it it;
13442 int line_height_before = this_line_pixel_height;
13443
13444 /* Note that start_display will handle the case that the
13445 line starting at tlbufpos is a continuation line. */
13446 start_display (&it, w, tlbufpos);
13447
13448 /* Implementation note: It this still necessary? */
13449 if (it.current_x != this_line_start_x)
13450 goto cancel;
13451
13452 TRACE ((stderr, "trying display optimization 1\n"));
13453 w->cursor.vpos = -1;
13454 overlay_arrow_seen = 0;
13455 it.vpos = this_line_vpos;
13456 it.current_y = this_line_y;
13457 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13458 display_line (&it);
13459
13460 /* If line contains point, is not continued,
13461 and ends at same distance from eob as before, we win. */
13462 if (w->cursor.vpos >= 0
13463 /* Line is not continued, otherwise this_line_start_pos
13464 would have been set to 0 in display_line. */
13465 && CHARPOS (this_line_start_pos)
13466 /* Line ends as before. */
13467 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13468 /* Line has same height as before. Otherwise other lines
13469 would have to be shifted up or down. */
13470 && this_line_pixel_height == line_height_before)
13471 {
13472 /* If this is not the window's last line, we must adjust
13473 the charstarts of the lines below. */
13474 if (it.current_y < it.last_visible_y)
13475 {
13476 struct glyph_row *row
13477 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13478 ptrdiff_t delta, delta_bytes;
13479
13480 /* We used to distinguish between two cases here,
13481 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13482 when the line ends in a newline or the end of the
13483 buffer's accessible portion. But both cases did
13484 the same, so they were collapsed. */
13485 delta = (Z
13486 - CHARPOS (tlendpos)
13487 - MATRIX_ROW_START_CHARPOS (row));
13488 delta_bytes = (Z_BYTE
13489 - BYTEPOS (tlendpos)
13490 - MATRIX_ROW_START_BYTEPOS (row));
13491
13492 increment_matrix_positions (w->current_matrix,
13493 this_line_vpos + 1,
13494 w->current_matrix->nrows,
13495 delta, delta_bytes);
13496 }
13497
13498 /* If this row displays text now but previously didn't,
13499 or vice versa, w->window_end_vpos may have to be
13500 adjusted. */
13501 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13502 {
13503 if (w->window_end_vpos < this_line_vpos)
13504 w->window_end_vpos = this_line_vpos;
13505 }
13506 else if (w->window_end_vpos == this_line_vpos
13507 && this_line_vpos > 0)
13508 w->window_end_vpos = this_line_vpos - 1;
13509 w->window_end_valid = 0;
13510
13511 /* Update hint: No need to try to scroll in update_window. */
13512 w->desired_matrix->no_scrolling_p = 1;
13513
13514 #ifdef GLYPH_DEBUG
13515 *w->desired_matrix->method = 0;
13516 debug_method_add (w, "optimization 1");
13517 #endif
13518 #ifdef HAVE_WINDOW_SYSTEM
13519 update_window_fringes (w, 0);
13520 #endif
13521 goto update;
13522 }
13523 else
13524 goto cancel;
13525 }
13526 else if (/* Cursor position hasn't changed. */
13527 PT == w->last_point
13528 /* Make sure the cursor was last displayed
13529 in this window. Otherwise we have to reposition it. */
13530
13531 /* PXW: Must be converted to pixels, probably. */
13532 && 0 <= w->cursor.vpos
13533 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13534 {
13535 if (!must_finish)
13536 {
13537 do_pending_window_change (1);
13538 /* If selected_window changed, redisplay again. */
13539 if (WINDOWP (selected_window)
13540 && (w = XWINDOW (selected_window)) != sw)
13541 goto retry;
13542
13543 /* We used to always goto end_of_redisplay here, but this
13544 isn't enough if we have a blinking cursor. */
13545 if (w->cursor_off_p == w->last_cursor_off_p)
13546 goto end_of_redisplay;
13547 }
13548 goto update;
13549 }
13550 /* If highlighting the region, or if the cursor is in the echo area,
13551 then we can't just move the cursor. */
13552 else if (NILP (Vshow_trailing_whitespace)
13553 && !cursor_in_echo_area)
13554 {
13555 struct it it;
13556 struct glyph_row *row;
13557
13558 /* Skip from tlbufpos to PT and see where it is. Note that
13559 PT may be in invisible text. If so, we will end at the
13560 next visible position. */
13561 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13562 NULL, DEFAULT_FACE_ID);
13563 it.current_x = this_line_start_x;
13564 it.current_y = this_line_y;
13565 it.vpos = this_line_vpos;
13566
13567 /* The call to move_it_to stops in front of PT, but
13568 moves over before-strings. */
13569 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13570
13571 if (it.vpos == this_line_vpos
13572 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13573 row->enabled_p))
13574 {
13575 eassert (this_line_vpos == it.vpos);
13576 eassert (this_line_y == it.current_y);
13577 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13578 #ifdef GLYPH_DEBUG
13579 *w->desired_matrix->method = 0;
13580 debug_method_add (w, "optimization 3");
13581 #endif
13582 goto update;
13583 }
13584 else
13585 goto cancel;
13586 }
13587
13588 cancel:
13589 /* Text changed drastically or point moved off of line. */
13590 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13591 }
13592
13593 CHARPOS (this_line_start_pos) = 0;
13594 ++clear_face_cache_count;
13595 #ifdef HAVE_WINDOW_SYSTEM
13596 ++clear_image_cache_count;
13597 #endif
13598
13599 /* Build desired matrices, and update the display. If
13600 consider_all_windows_p is non-zero, do it for all windows on all
13601 frames. Otherwise do it for selected_window, only. */
13602
13603 if (consider_all_windows_p)
13604 {
13605 FOR_EACH_FRAME (tail, frame)
13606 XFRAME (frame)->updated_p = 0;
13607
13608 propagate_buffer_redisplay ();
13609
13610 FOR_EACH_FRAME (tail, frame)
13611 {
13612 struct frame *f = XFRAME (frame);
13613
13614 /* We don't have to do anything for unselected terminal
13615 frames. */
13616 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13617 && !EQ (FRAME_TTY (f)->top_frame, frame))
13618 continue;
13619
13620 retry_frame:
13621
13622 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13623 {
13624 bool gcscrollbars
13625 /* Only GC scrollbars when we redisplay the whole frame. */
13626 = f->redisplay || !REDISPLAY_SOME_P ();
13627 /* Mark all the scroll bars to be removed; we'll redeem
13628 the ones we want when we redisplay their windows. */
13629 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13630 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13631
13632 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13633 redisplay_windows (FRAME_ROOT_WINDOW (f));
13634 /* Remember that the invisible frames need to be redisplayed next
13635 time they're visible. */
13636 else if (!REDISPLAY_SOME_P ())
13637 f->redisplay = true;
13638
13639 /* The X error handler may have deleted that frame. */
13640 if (!FRAME_LIVE_P (f))
13641 continue;
13642
13643 /* Any scroll bars which redisplay_windows should have
13644 nuked should now go away. */
13645 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13646 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13647
13648 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13649 {
13650 /* If fonts changed on visible frame, display again. */
13651 if (f->fonts_changed)
13652 {
13653 adjust_frame_glyphs (f);
13654 f->fonts_changed = 0;
13655 goto retry_frame;
13656 }
13657
13658 /* See if we have to hscroll. */
13659 if (!f->already_hscrolled_p)
13660 {
13661 f->already_hscrolled_p = 1;
13662 if (hscroll_windows (f->root_window))
13663 goto retry_frame;
13664 }
13665
13666 /* Prevent various kinds of signals during display
13667 update. stdio is not robust about handling
13668 signals, which can cause an apparent I/O error. */
13669 if (interrupt_input)
13670 unrequest_sigio ();
13671 STOP_POLLING;
13672
13673 pending |= update_frame (f, 0, 0);
13674 f->cursor_type_changed = 0;
13675 f->updated_p = 1;
13676 }
13677 }
13678 }
13679
13680 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13681
13682 if (!pending)
13683 {
13684 /* Do the mark_window_display_accurate after all windows have
13685 been redisplayed because this call resets flags in buffers
13686 which are needed for proper redisplay. */
13687 FOR_EACH_FRAME (tail, frame)
13688 {
13689 struct frame *f = XFRAME (frame);
13690 if (f->updated_p)
13691 {
13692 f->redisplay = false;
13693 mark_window_display_accurate (f->root_window, 1);
13694 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13695 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13696 }
13697 }
13698 }
13699 }
13700 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13701 {
13702 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13703 struct frame *mini_frame;
13704
13705 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13706 /* Use list_of_error, not Qerror, so that
13707 we catch only errors and don't run the debugger. */
13708 internal_condition_case_1 (redisplay_window_1, selected_window,
13709 list_of_error,
13710 redisplay_window_error);
13711 if (update_miniwindow_p)
13712 internal_condition_case_1 (redisplay_window_1, mini_window,
13713 list_of_error,
13714 redisplay_window_error);
13715
13716 /* Compare desired and current matrices, perform output. */
13717
13718 update:
13719 /* If fonts changed, display again. */
13720 if (sf->fonts_changed)
13721 goto retry;
13722
13723 /* Prevent various kinds of signals during display update.
13724 stdio is not robust about handling signals,
13725 which can cause an apparent I/O error. */
13726 if (interrupt_input)
13727 unrequest_sigio ();
13728 STOP_POLLING;
13729
13730 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13731 {
13732 if (hscroll_windows (selected_window))
13733 goto retry;
13734
13735 XWINDOW (selected_window)->must_be_updated_p = true;
13736 pending = update_frame (sf, 0, 0);
13737 sf->cursor_type_changed = 0;
13738 }
13739
13740 /* We may have called echo_area_display at the top of this
13741 function. If the echo area is on another frame, that may
13742 have put text on a frame other than the selected one, so the
13743 above call to update_frame would not have caught it. Catch
13744 it here. */
13745 mini_window = FRAME_MINIBUF_WINDOW (sf);
13746 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13747
13748 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13749 {
13750 XWINDOW (mini_window)->must_be_updated_p = true;
13751 pending |= update_frame (mini_frame, 0, 0);
13752 mini_frame->cursor_type_changed = 0;
13753 if (!pending && hscroll_windows (mini_window))
13754 goto retry;
13755 }
13756 }
13757
13758 /* If display was paused because of pending input, make sure we do a
13759 thorough update the next time. */
13760 if (pending)
13761 {
13762 /* Prevent the optimization at the beginning of
13763 redisplay_internal that tries a single-line update of the
13764 line containing the cursor in the selected window. */
13765 CHARPOS (this_line_start_pos) = 0;
13766
13767 /* Let the overlay arrow be updated the next time. */
13768 update_overlay_arrows (0);
13769
13770 /* If we pause after scrolling, some rows in the current
13771 matrices of some windows are not valid. */
13772 if (!WINDOW_FULL_WIDTH_P (w)
13773 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13774 update_mode_lines = 36;
13775 }
13776 else
13777 {
13778 if (!consider_all_windows_p)
13779 {
13780 /* This has already been done above if
13781 consider_all_windows_p is set. */
13782 if (XBUFFER (w->contents)->text->redisplay
13783 && buffer_window_count (XBUFFER (w->contents)) > 1)
13784 /* This can happen if b->text->redisplay was set during
13785 jit-lock. */
13786 propagate_buffer_redisplay ();
13787 mark_window_display_accurate_1 (w, 1);
13788
13789 /* Say overlay arrows are up to date. */
13790 update_overlay_arrows (1);
13791
13792 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13793 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13794 }
13795
13796 update_mode_lines = 0;
13797 windows_or_buffers_changed = 0;
13798 }
13799
13800 /* Start SIGIO interrupts coming again. Having them off during the
13801 code above makes it less likely one will discard output, but not
13802 impossible, since there might be stuff in the system buffer here.
13803 But it is much hairier to try to do anything about that. */
13804 if (interrupt_input)
13805 request_sigio ();
13806 RESUME_POLLING;
13807
13808 /* If a frame has become visible which was not before, redisplay
13809 again, so that we display it. Expose events for such a frame
13810 (which it gets when becoming visible) don't call the parts of
13811 redisplay constructing glyphs, so simply exposing a frame won't
13812 display anything in this case. So, we have to display these
13813 frames here explicitly. */
13814 if (!pending)
13815 {
13816 int new_count = 0;
13817
13818 FOR_EACH_FRAME (tail, frame)
13819 {
13820 if (XFRAME (frame)->visible)
13821 new_count++;
13822 }
13823
13824 if (new_count != number_of_visible_frames)
13825 windows_or_buffers_changed = 52;
13826 }
13827
13828 /* Change frame size now if a change is pending. */
13829 do_pending_window_change (1);
13830
13831 /* If we just did a pending size change, or have additional
13832 visible frames, or selected_window changed, redisplay again. */
13833 if ((windows_or_buffers_changed && !pending)
13834 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13835 goto retry;
13836
13837 /* Clear the face and image caches.
13838
13839 We used to do this only if consider_all_windows_p. But the cache
13840 needs to be cleared if a timer creates images in the current
13841 buffer (e.g. the test case in Bug#6230). */
13842
13843 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13844 {
13845 clear_face_cache (0);
13846 clear_face_cache_count = 0;
13847 }
13848
13849 #ifdef HAVE_WINDOW_SYSTEM
13850 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13851 {
13852 clear_image_caches (Qnil);
13853 clear_image_cache_count = 0;
13854 }
13855 #endif /* HAVE_WINDOW_SYSTEM */
13856
13857 end_of_redisplay:
13858 if (interrupt_input && interrupts_deferred)
13859 request_sigio ();
13860
13861 unbind_to (count, Qnil);
13862 RESUME_POLLING;
13863 }
13864
13865
13866 /* Redisplay, but leave alone any recent echo area message unless
13867 another message has been requested in its place.
13868
13869 This is useful in situations where you need to redisplay but no
13870 user action has occurred, making it inappropriate for the message
13871 area to be cleared. See tracking_off and
13872 wait_reading_process_output for examples of these situations.
13873
13874 FROM_WHERE is an integer saying from where this function was
13875 called. This is useful for debugging. */
13876
13877 void
13878 redisplay_preserve_echo_area (int from_where)
13879 {
13880 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13881
13882 if (!NILP (echo_area_buffer[1]))
13883 {
13884 /* We have a previously displayed message, but no current
13885 message. Redisplay the previous message. */
13886 display_last_displayed_message_p = 1;
13887 redisplay_internal ();
13888 display_last_displayed_message_p = 0;
13889 }
13890 else
13891 redisplay_internal ();
13892
13893 flush_frame (SELECTED_FRAME ());
13894 }
13895
13896
13897 /* Function registered with record_unwind_protect in redisplay_internal. */
13898
13899 static void
13900 unwind_redisplay (void)
13901 {
13902 redisplaying_p = 0;
13903 }
13904
13905
13906 /* Mark the display of leaf window W as accurate or inaccurate.
13907 If ACCURATE_P is non-zero mark display of W as accurate. If
13908 ACCURATE_P is zero, arrange for W to be redisplayed the next
13909 time redisplay_internal is called. */
13910
13911 static void
13912 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13913 {
13914 struct buffer *b = XBUFFER (w->contents);
13915
13916 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13917 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13918 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13919
13920 if (accurate_p)
13921 {
13922 b->clip_changed = false;
13923 b->prevent_redisplay_optimizations_p = false;
13924 eassert (buffer_window_count (b) > 0);
13925 /* Resetting b->text->redisplay is problematic!
13926 In order to make it safer to do it here, redisplay_internal must
13927 have copied all b->text->redisplay to their respective windows. */
13928 b->text->redisplay = false;
13929
13930 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13931 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13932 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13933 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13934
13935 w->current_matrix->buffer = b;
13936 w->current_matrix->begv = BUF_BEGV (b);
13937 w->current_matrix->zv = BUF_ZV (b);
13938
13939 w->last_cursor_vpos = w->cursor.vpos;
13940 w->last_cursor_off_p = w->cursor_off_p;
13941
13942 if (w == XWINDOW (selected_window))
13943 w->last_point = BUF_PT (b);
13944 else
13945 w->last_point = marker_position (w->pointm);
13946
13947 w->window_end_valid = true;
13948 w->update_mode_line = false;
13949 }
13950
13951 w->redisplay = !accurate_p;
13952 }
13953
13954
13955 /* Mark the display of windows in the window tree rooted at WINDOW as
13956 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13957 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13958 be redisplayed the next time redisplay_internal is called. */
13959
13960 void
13961 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13962 {
13963 struct window *w;
13964
13965 for (; !NILP (window); window = w->next)
13966 {
13967 w = XWINDOW (window);
13968 if (WINDOWP (w->contents))
13969 mark_window_display_accurate (w->contents, accurate_p);
13970 else
13971 mark_window_display_accurate_1 (w, accurate_p);
13972 }
13973
13974 if (accurate_p)
13975 update_overlay_arrows (1);
13976 else
13977 /* Force a thorough redisplay the next time by setting
13978 last_arrow_position and last_arrow_string to t, which is
13979 unequal to any useful value of Voverlay_arrow_... */
13980 update_overlay_arrows (-1);
13981 }
13982
13983
13984 /* Return value in display table DP (Lisp_Char_Table *) for character
13985 C. Since a display table doesn't have any parent, we don't have to
13986 follow parent. Do not call this function directly but use the
13987 macro DISP_CHAR_VECTOR. */
13988
13989 Lisp_Object
13990 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13991 {
13992 Lisp_Object val;
13993
13994 if (ASCII_CHAR_P (c))
13995 {
13996 val = dp->ascii;
13997 if (SUB_CHAR_TABLE_P (val))
13998 val = XSUB_CHAR_TABLE (val)->contents[c];
13999 }
14000 else
14001 {
14002 Lisp_Object table;
14003
14004 XSETCHAR_TABLE (table, dp);
14005 val = char_table_ref (table, c);
14006 }
14007 if (NILP (val))
14008 val = dp->defalt;
14009 return val;
14010 }
14011
14012
14013 \f
14014 /***********************************************************************
14015 Window Redisplay
14016 ***********************************************************************/
14017
14018 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14019
14020 static void
14021 redisplay_windows (Lisp_Object window)
14022 {
14023 while (!NILP (window))
14024 {
14025 struct window *w = XWINDOW (window);
14026
14027 if (WINDOWP (w->contents))
14028 redisplay_windows (w->contents);
14029 else if (BUFFERP (w->contents))
14030 {
14031 displayed_buffer = XBUFFER (w->contents);
14032 /* Use list_of_error, not Qerror, so that
14033 we catch only errors and don't run the debugger. */
14034 internal_condition_case_1 (redisplay_window_0, window,
14035 list_of_error,
14036 redisplay_window_error);
14037 }
14038
14039 window = w->next;
14040 }
14041 }
14042
14043 static Lisp_Object
14044 redisplay_window_error (Lisp_Object ignore)
14045 {
14046 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14047 return Qnil;
14048 }
14049
14050 static Lisp_Object
14051 redisplay_window_0 (Lisp_Object window)
14052 {
14053 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14054 redisplay_window (window, false);
14055 return Qnil;
14056 }
14057
14058 static Lisp_Object
14059 redisplay_window_1 (Lisp_Object window)
14060 {
14061 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14062 redisplay_window (window, true);
14063 return Qnil;
14064 }
14065 \f
14066
14067 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14068 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14069 which positions recorded in ROW differ from current buffer
14070 positions.
14071
14072 Return 0 if cursor is not on this row, 1 otherwise. */
14073
14074 static int
14075 set_cursor_from_row (struct window *w, struct glyph_row *row,
14076 struct glyph_matrix *matrix,
14077 ptrdiff_t delta, ptrdiff_t delta_bytes,
14078 int dy, int dvpos)
14079 {
14080 struct glyph *glyph = row->glyphs[TEXT_AREA];
14081 struct glyph *end = glyph + row->used[TEXT_AREA];
14082 struct glyph *cursor = NULL;
14083 /* The last known character position in row. */
14084 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14085 int x = row->x;
14086 ptrdiff_t pt_old = PT - delta;
14087 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14088 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14089 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14090 /* A glyph beyond the edge of TEXT_AREA which we should never
14091 touch. */
14092 struct glyph *glyphs_end = end;
14093 /* Non-zero means we've found a match for cursor position, but that
14094 glyph has the avoid_cursor_p flag set. */
14095 int match_with_avoid_cursor = 0;
14096 /* Non-zero means we've seen at least one glyph that came from a
14097 display string. */
14098 int string_seen = 0;
14099 /* Largest and smallest buffer positions seen so far during scan of
14100 glyph row. */
14101 ptrdiff_t bpos_max = pos_before;
14102 ptrdiff_t bpos_min = pos_after;
14103 /* Last buffer position covered by an overlay string with an integer
14104 `cursor' property. */
14105 ptrdiff_t bpos_covered = 0;
14106 /* Non-zero means the display string on which to display the cursor
14107 comes from a text property, not from an overlay. */
14108 int string_from_text_prop = 0;
14109
14110 /* Don't even try doing anything if called for a mode-line or
14111 header-line row, since the rest of the code isn't prepared to
14112 deal with such calamities. */
14113 eassert (!row->mode_line_p);
14114 if (row->mode_line_p)
14115 return 0;
14116
14117 /* Skip over glyphs not having an object at the start and the end of
14118 the row. These are special glyphs like truncation marks on
14119 terminal frames. */
14120 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14121 {
14122 if (!row->reversed_p)
14123 {
14124 while (glyph < end
14125 && INTEGERP (glyph->object)
14126 && glyph->charpos < 0)
14127 {
14128 x += glyph->pixel_width;
14129 ++glyph;
14130 }
14131 while (end > glyph
14132 && INTEGERP ((end - 1)->object)
14133 /* CHARPOS is zero for blanks and stretch glyphs
14134 inserted by extend_face_to_end_of_line. */
14135 && (end - 1)->charpos <= 0)
14136 --end;
14137 glyph_before = glyph - 1;
14138 glyph_after = end;
14139 }
14140 else
14141 {
14142 struct glyph *g;
14143
14144 /* If the glyph row is reversed, we need to process it from back
14145 to front, so swap the edge pointers. */
14146 glyphs_end = end = glyph - 1;
14147 glyph += row->used[TEXT_AREA] - 1;
14148
14149 while (glyph > end + 1
14150 && INTEGERP (glyph->object)
14151 && glyph->charpos < 0)
14152 {
14153 --glyph;
14154 x -= glyph->pixel_width;
14155 }
14156 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14157 --glyph;
14158 /* By default, in reversed rows we put the cursor on the
14159 rightmost (first in the reading order) glyph. */
14160 for (g = end + 1; g < glyph; g++)
14161 x += g->pixel_width;
14162 while (end < glyph
14163 && INTEGERP ((end + 1)->object)
14164 && (end + 1)->charpos <= 0)
14165 ++end;
14166 glyph_before = glyph + 1;
14167 glyph_after = end;
14168 }
14169 }
14170 else if (row->reversed_p)
14171 {
14172 /* In R2L rows that don't display text, put the cursor on the
14173 rightmost glyph. Case in point: an empty last line that is
14174 part of an R2L paragraph. */
14175 cursor = end - 1;
14176 /* Avoid placing the cursor on the last glyph of the row, where
14177 on terminal frames we hold the vertical border between
14178 adjacent windows. */
14179 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14180 && !WINDOW_RIGHTMOST_P (w)
14181 && cursor == row->glyphs[LAST_AREA] - 1)
14182 cursor--;
14183 x = -1; /* will be computed below, at label compute_x */
14184 }
14185
14186 /* Step 1: Try to find the glyph whose character position
14187 corresponds to point. If that's not possible, find 2 glyphs
14188 whose character positions are the closest to point, one before
14189 point, the other after it. */
14190 if (!row->reversed_p)
14191 while (/* not marched to end of glyph row */
14192 glyph < end
14193 /* glyph was not inserted by redisplay for internal purposes */
14194 && !INTEGERP (glyph->object))
14195 {
14196 if (BUFFERP (glyph->object))
14197 {
14198 ptrdiff_t dpos = glyph->charpos - pt_old;
14199
14200 if (glyph->charpos > bpos_max)
14201 bpos_max = glyph->charpos;
14202 if (glyph->charpos < bpos_min)
14203 bpos_min = glyph->charpos;
14204 if (!glyph->avoid_cursor_p)
14205 {
14206 /* If we hit point, we've found the glyph on which to
14207 display the cursor. */
14208 if (dpos == 0)
14209 {
14210 match_with_avoid_cursor = 0;
14211 break;
14212 }
14213 /* See if we've found a better approximation to
14214 POS_BEFORE or to POS_AFTER. */
14215 if (0 > dpos && dpos > pos_before - pt_old)
14216 {
14217 pos_before = glyph->charpos;
14218 glyph_before = glyph;
14219 }
14220 else if (0 < dpos && dpos < pos_after - pt_old)
14221 {
14222 pos_after = glyph->charpos;
14223 glyph_after = glyph;
14224 }
14225 }
14226 else if (dpos == 0)
14227 match_with_avoid_cursor = 1;
14228 }
14229 else if (STRINGP (glyph->object))
14230 {
14231 Lisp_Object chprop;
14232 ptrdiff_t glyph_pos = glyph->charpos;
14233
14234 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14235 glyph->object);
14236 if (!NILP (chprop))
14237 {
14238 /* If the string came from a `display' text property,
14239 look up the buffer position of that property and
14240 use that position to update bpos_max, as if we
14241 actually saw such a position in one of the row's
14242 glyphs. This helps with supporting integer values
14243 of `cursor' property on the display string in
14244 situations where most or all of the row's buffer
14245 text is completely covered by display properties,
14246 so that no glyph with valid buffer positions is
14247 ever seen in the row. */
14248 ptrdiff_t prop_pos =
14249 string_buffer_position_lim (glyph->object, pos_before,
14250 pos_after, 0);
14251
14252 if (prop_pos >= pos_before)
14253 bpos_max = prop_pos - 1;
14254 }
14255 if (INTEGERP (chprop))
14256 {
14257 bpos_covered = bpos_max + XINT (chprop);
14258 /* If the `cursor' property covers buffer positions up
14259 to and including point, we should display cursor on
14260 this glyph. Note that, if a `cursor' property on one
14261 of the string's characters has an integer value, we
14262 will break out of the loop below _before_ we get to
14263 the position match above. IOW, integer values of
14264 the `cursor' property override the "exact match for
14265 point" strategy of positioning the cursor. */
14266 /* Implementation note: bpos_max == pt_old when, e.g.,
14267 we are in an empty line, where bpos_max is set to
14268 MATRIX_ROW_START_CHARPOS, see above. */
14269 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14270 {
14271 cursor = glyph;
14272 break;
14273 }
14274 }
14275
14276 string_seen = 1;
14277 }
14278 x += glyph->pixel_width;
14279 ++glyph;
14280 }
14281 else if (glyph > end) /* row is reversed */
14282 while (!INTEGERP (glyph->object))
14283 {
14284 if (BUFFERP (glyph->object))
14285 {
14286 ptrdiff_t dpos = glyph->charpos - pt_old;
14287
14288 if (glyph->charpos > bpos_max)
14289 bpos_max = glyph->charpos;
14290 if (glyph->charpos < bpos_min)
14291 bpos_min = glyph->charpos;
14292 if (!glyph->avoid_cursor_p)
14293 {
14294 if (dpos == 0)
14295 {
14296 match_with_avoid_cursor = 0;
14297 break;
14298 }
14299 if (0 > dpos && dpos > pos_before - pt_old)
14300 {
14301 pos_before = glyph->charpos;
14302 glyph_before = glyph;
14303 }
14304 else if (0 < dpos && dpos < pos_after - pt_old)
14305 {
14306 pos_after = glyph->charpos;
14307 glyph_after = glyph;
14308 }
14309 }
14310 else if (dpos == 0)
14311 match_with_avoid_cursor = 1;
14312 }
14313 else if (STRINGP (glyph->object))
14314 {
14315 Lisp_Object chprop;
14316 ptrdiff_t glyph_pos = glyph->charpos;
14317
14318 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14319 glyph->object);
14320 if (!NILP (chprop))
14321 {
14322 ptrdiff_t prop_pos =
14323 string_buffer_position_lim (glyph->object, pos_before,
14324 pos_after, 0);
14325
14326 if (prop_pos >= pos_before)
14327 bpos_max = prop_pos - 1;
14328 }
14329 if (INTEGERP (chprop))
14330 {
14331 bpos_covered = bpos_max + XINT (chprop);
14332 /* If the `cursor' property covers buffer positions up
14333 to and including point, we should display cursor on
14334 this glyph. */
14335 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14336 {
14337 cursor = glyph;
14338 break;
14339 }
14340 }
14341 string_seen = 1;
14342 }
14343 --glyph;
14344 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14345 {
14346 x--; /* can't use any pixel_width */
14347 break;
14348 }
14349 x -= glyph->pixel_width;
14350 }
14351
14352 /* Step 2: If we didn't find an exact match for point, we need to
14353 look for a proper place to put the cursor among glyphs between
14354 GLYPH_BEFORE and GLYPH_AFTER. */
14355 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14356 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14357 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14358 {
14359 /* An empty line has a single glyph whose OBJECT is zero and
14360 whose CHARPOS is the position of a newline on that line.
14361 Note that on a TTY, there are more glyphs after that, which
14362 were produced by extend_face_to_end_of_line, but their
14363 CHARPOS is zero or negative. */
14364 int empty_line_p =
14365 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14366 && INTEGERP (glyph->object) && glyph->charpos > 0
14367 /* On a TTY, continued and truncated rows also have a glyph at
14368 their end whose OBJECT is zero and whose CHARPOS is
14369 positive (the continuation and truncation glyphs), but such
14370 rows are obviously not "empty". */
14371 && !(row->continued_p || row->truncated_on_right_p);
14372
14373 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14374 {
14375 ptrdiff_t ellipsis_pos;
14376
14377 /* Scan back over the ellipsis glyphs. */
14378 if (!row->reversed_p)
14379 {
14380 ellipsis_pos = (glyph - 1)->charpos;
14381 while (glyph > row->glyphs[TEXT_AREA]
14382 && (glyph - 1)->charpos == ellipsis_pos)
14383 glyph--, x -= glyph->pixel_width;
14384 /* That loop always goes one position too far, including
14385 the glyph before the ellipsis. So scan forward over
14386 that one. */
14387 x += glyph->pixel_width;
14388 glyph++;
14389 }
14390 else /* row is reversed */
14391 {
14392 ellipsis_pos = (glyph + 1)->charpos;
14393 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14394 && (glyph + 1)->charpos == ellipsis_pos)
14395 glyph++, x += glyph->pixel_width;
14396 x -= glyph->pixel_width;
14397 glyph--;
14398 }
14399 }
14400 else if (match_with_avoid_cursor)
14401 {
14402 cursor = glyph_after;
14403 x = -1;
14404 }
14405 else if (string_seen)
14406 {
14407 int incr = row->reversed_p ? -1 : +1;
14408
14409 /* Need to find the glyph that came out of a string which is
14410 present at point. That glyph is somewhere between
14411 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14412 positioned between POS_BEFORE and POS_AFTER in the
14413 buffer. */
14414 struct glyph *start, *stop;
14415 ptrdiff_t pos = pos_before;
14416
14417 x = -1;
14418
14419 /* If the row ends in a newline from a display string,
14420 reordering could have moved the glyphs belonging to the
14421 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14422 in this case we extend the search to the last glyph in
14423 the row that was not inserted by redisplay. */
14424 if (row->ends_in_newline_from_string_p)
14425 {
14426 glyph_after = end;
14427 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14428 }
14429
14430 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14431 correspond to POS_BEFORE and POS_AFTER, respectively. We
14432 need START and STOP in the order that corresponds to the
14433 row's direction as given by its reversed_p flag. If the
14434 directionality of characters between POS_BEFORE and
14435 POS_AFTER is the opposite of the row's base direction,
14436 these characters will have been reordered for display,
14437 and we need to reverse START and STOP. */
14438 if (!row->reversed_p)
14439 {
14440 start = min (glyph_before, glyph_after);
14441 stop = max (glyph_before, glyph_after);
14442 }
14443 else
14444 {
14445 start = max (glyph_before, glyph_after);
14446 stop = min (glyph_before, glyph_after);
14447 }
14448 for (glyph = start + incr;
14449 row->reversed_p ? glyph > stop : glyph < stop; )
14450 {
14451
14452 /* Any glyphs that come from the buffer are here because
14453 of bidi reordering. Skip them, and only pay
14454 attention to glyphs that came from some string. */
14455 if (STRINGP (glyph->object))
14456 {
14457 Lisp_Object str;
14458 ptrdiff_t tem;
14459 /* If the display property covers the newline, we
14460 need to search for it one position farther. */
14461 ptrdiff_t lim = pos_after
14462 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14463
14464 string_from_text_prop = 0;
14465 str = glyph->object;
14466 tem = string_buffer_position_lim (str, pos, lim, 0);
14467 if (tem == 0 /* from overlay */
14468 || pos <= tem)
14469 {
14470 /* If the string from which this glyph came is
14471 found in the buffer at point, or at position
14472 that is closer to point than pos_after, then
14473 we've found the glyph we've been looking for.
14474 If it comes from an overlay (tem == 0), and
14475 it has the `cursor' property on one of its
14476 glyphs, record that glyph as a candidate for
14477 displaying the cursor. (As in the
14478 unidirectional version, we will display the
14479 cursor on the last candidate we find.) */
14480 if (tem == 0
14481 || tem == pt_old
14482 || (tem - pt_old > 0 && tem < pos_after))
14483 {
14484 /* The glyphs from this string could have
14485 been reordered. Find the one with the
14486 smallest string position. Or there could
14487 be a character in the string with the
14488 `cursor' property, which means display
14489 cursor on that character's glyph. */
14490 ptrdiff_t strpos = glyph->charpos;
14491
14492 if (tem)
14493 {
14494 cursor = glyph;
14495 string_from_text_prop = 1;
14496 }
14497 for ( ;
14498 (row->reversed_p ? glyph > stop : glyph < stop)
14499 && EQ (glyph->object, str);
14500 glyph += incr)
14501 {
14502 Lisp_Object cprop;
14503 ptrdiff_t gpos = glyph->charpos;
14504
14505 cprop = Fget_char_property (make_number (gpos),
14506 Qcursor,
14507 glyph->object);
14508 if (!NILP (cprop))
14509 {
14510 cursor = glyph;
14511 break;
14512 }
14513 if (tem && glyph->charpos < strpos)
14514 {
14515 strpos = glyph->charpos;
14516 cursor = glyph;
14517 }
14518 }
14519
14520 if (tem == pt_old
14521 || (tem - pt_old > 0 && tem < pos_after))
14522 goto compute_x;
14523 }
14524 if (tem)
14525 pos = tem + 1; /* don't find previous instances */
14526 }
14527 /* This string is not what we want; skip all of the
14528 glyphs that came from it. */
14529 while ((row->reversed_p ? glyph > stop : glyph < stop)
14530 && EQ (glyph->object, str))
14531 glyph += incr;
14532 }
14533 else
14534 glyph += incr;
14535 }
14536
14537 /* If we reached the end of the line, and END was from a string,
14538 the cursor is not on this line. */
14539 if (cursor == NULL
14540 && (row->reversed_p ? glyph <= end : glyph >= end)
14541 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14542 && STRINGP (end->object)
14543 && row->continued_p)
14544 return 0;
14545 }
14546 /* A truncated row may not include PT among its character positions.
14547 Setting the cursor inside the scroll margin will trigger
14548 recalculation of hscroll in hscroll_window_tree. But if a
14549 display string covers point, defer to the string-handling
14550 code below to figure this out. */
14551 else if (row->truncated_on_left_p && pt_old < bpos_min)
14552 {
14553 cursor = glyph_before;
14554 x = -1;
14555 }
14556 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14557 /* Zero-width characters produce no glyphs. */
14558 || (!empty_line_p
14559 && (row->reversed_p
14560 ? glyph_after > glyphs_end
14561 : glyph_after < glyphs_end)))
14562 {
14563 cursor = glyph_after;
14564 x = -1;
14565 }
14566 }
14567
14568 compute_x:
14569 if (cursor != NULL)
14570 glyph = cursor;
14571 else if (glyph == glyphs_end
14572 && pos_before == pos_after
14573 && STRINGP ((row->reversed_p
14574 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14575 : row->glyphs[TEXT_AREA])->object))
14576 {
14577 /* If all the glyphs of this row came from strings, put the
14578 cursor on the first glyph of the row. This avoids having the
14579 cursor outside of the text area in this very rare and hard
14580 use case. */
14581 glyph =
14582 row->reversed_p
14583 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14584 : row->glyphs[TEXT_AREA];
14585 }
14586 if (x < 0)
14587 {
14588 struct glyph *g;
14589
14590 /* Need to compute x that corresponds to GLYPH. */
14591 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14592 {
14593 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14594 emacs_abort ();
14595 x += g->pixel_width;
14596 }
14597 }
14598
14599 /* ROW could be part of a continued line, which, under bidi
14600 reordering, might have other rows whose start and end charpos
14601 occlude point. Only set w->cursor if we found a better
14602 approximation to the cursor position than we have from previously
14603 examined candidate rows belonging to the same continued line. */
14604 if (/* We already have a candidate row. */
14605 w->cursor.vpos >= 0
14606 /* That candidate is not the row we are processing. */
14607 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14608 /* Make sure cursor.vpos specifies a row whose start and end
14609 charpos occlude point, and it is valid candidate for being a
14610 cursor-row. This is because some callers of this function
14611 leave cursor.vpos at the row where the cursor was displayed
14612 during the last redisplay cycle. */
14613 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14614 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14615 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14616 {
14617 struct glyph *g1
14618 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14619
14620 /* Don't consider glyphs that are outside TEXT_AREA. */
14621 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14622 return 0;
14623 /* Keep the candidate whose buffer position is the closest to
14624 point or has the `cursor' property. */
14625 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14626 w->cursor.hpos >= 0
14627 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14628 && ((BUFFERP (g1->object)
14629 && (g1->charpos == pt_old /* An exact match always wins. */
14630 || (BUFFERP (glyph->object)
14631 && eabs (g1->charpos - pt_old)
14632 < eabs (glyph->charpos - pt_old))))
14633 /* Previous candidate is a glyph from a string that has
14634 a non-nil `cursor' property. */
14635 || (STRINGP (g1->object)
14636 && (!NILP (Fget_char_property (make_number (g1->charpos),
14637 Qcursor, g1->object))
14638 /* Previous candidate is from the same display
14639 string as this one, and the display string
14640 came from a text property. */
14641 || (EQ (g1->object, glyph->object)
14642 && string_from_text_prop)
14643 /* this candidate is from newline and its
14644 position is not an exact match */
14645 || (INTEGERP (glyph->object)
14646 && glyph->charpos != pt_old)))))
14647 return 0;
14648 /* If this candidate gives an exact match, use that. */
14649 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14650 /* If this candidate is a glyph created for the
14651 terminating newline of a line, and point is on that
14652 newline, it wins because it's an exact match. */
14653 || (!row->continued_p
14654 && INTEGERP (glyph->object)
14655 && glyph->charpos == 0
14656 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14657 /* Otherwise, keep the candidate that comes from a row
14658 spanning less buffer positions. This may win when one or
14659 both candidate positions are on glyphs that came from
14660 display strings, for which we cannot compare buffer
14661 positions. */
14662 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14663 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14664 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14665 return 0;
14666 }
14667 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14668 w->cursor.x = x;
14669 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14670 w->cursor.y = row->y + dy;
14671
14672 if (w == XWINDOW (selected_window))
14673 {
14674 if (!row->continued_p
14675 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14676 && row->x == 0)
14677 {
14678 this_line_buffer = XBUFFER (w->contents);
14679
14680 CHARPOS (this_line_start_pos)
14681 = MATRIX_ROW_START_CHARPOS (row) + delta;
14682 BYTEPOS (this_line_start_pos)
14683 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14684
14685 CHARPOS (this_line_end_pos)
14686 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14687 BYTEPOS (this_line_end_pos)
14688 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14689
14690 this_line_y = w->cursor.y;
14691 this_line_pixel_height = row->height;
14692 this_line_vpos = w->cursor.vpos;
14693 this_line_start_x = row->x;
14694 }
14695 else
14696 CHARPOS (this_line_start_pos) = 0;
14697 }
14698
14699 return 1;
14700 }
14701
14702
14703 /* Run window scroll functions, if any, for WINDOW with new window
14704 start STARTP. Sets the window start of WINDOW to that position.
14705
14706 We assume that the window's buffer is really current. */
14707
14708 static struct text_pos
14709 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14710 {
14711 struct window *w = XWINDOW (window);
14712 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14713
14714 eassert (current_buffer == XBUFFER (w->contents));
14715
14716 if (!NILP (Vwindow_scroll_functions))
14717 {
14718 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14719 make_number (CHARPOS (startp)));
14720 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14721 /* In case the hook functions switch buffers. */
14722 set_buffer_internal (XBUFFER (w->contents));
14723 }
14724
14725 return startp;
14726 }
14727
14728
14729 /* Make sure the line containing the cursor is fully visible.
14730 A value of 1 means there is nothing to be done.
14731 (Either the line is fully visible, or it cannot be made so,
14732 or we cannot tell.)
14733
14734 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14735 is higher than window.
14736
14737 A value of 0 means the caller should do scrolling
14738 as if point had gone off the screen. */
14739
14740 static int
14741 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14742 {
14743 struct glyph_matrix *matrix;
14744 struct glyph_row *row;
14745 int window_height;
14746
14747 if (!make_cursor_line_fully_visible_p)
14748 return 1;
14749
14750 /* It's not always possible to find the cursor, e.g, when a window
14751 is full of overlay strings. Don't do anything in that case. */
14752 if (w->cursor.vpos < 0)
14753 return 1;
14754
14755 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14756 row = MATRIX_ROW (matrix, w->cursor.vpos);
14757
14758 /* If the cursor row is not partially visible, there's nothing to do. */
14759 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14760 return 1;
14761
14762 /* If the row the cursor is in is taller than the window's height,
14763 it's not clear what to do, so do nothing. */
14764 window_height = window_box_height (w);
14765 if (row->height >= window_height)
14766 {
14767 if (!force_p || MINI_WINDOW_P (w)
14768 || w->vscroll || w->cursor.vpos == 0)
14769 return 1;
14770 }
14771 return 0;
14772 }
14773
14774
14775 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14776 non-zero means only WINDOW is redisplayed in redisplay_internal.
14777 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14778 in redisplay_window to bring a partially visible line into view in
14779 the case that only the cursor has moved.
14780
14781 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14782 last screen line's vertical height extends past the end of the screen.
14783
14784 Value is
14785
14786 1 if scrolling succeeded
14787
14788 0 if scrolling didn't find point.
14789
14790 -1 if new fonts have been loaded so that we must interrupt
14791 redisplay, adjust glyph matrices, and try again. */
14792
14793 enum
14794 {
14795 SCROLLING_SUCCESS,
14796 SCROLLING_FAILED,
14797 SCROLLING_NEED_LARGER_MATRICES
14798 };
14799
14800 /* If scroll-conservatively is more than this, never recenter.
14801
14802 If you change this, don't forget to update the doc string of
14803 `scroll-conservatively' and the Emacs manual. */
14804 #define SCROLL_LIMIT 100
14805
14806 static int
14807 try_scrolling (Lisp_Object window, int just_this_one_p,
14808 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14809 int temp_scroll_step, int last_line_misfit)
14810 {
14811 struct window *w = XWINDOW (window);
14812 struct frame *f = XFRAME (w->frame);
14813 struct text_pos pos, startp;
14814 struct it it;
14815 int this_scroll_margin, scroll_max, rc, height;
14816 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14817 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14818 Lisp_Object aggressive;
14819 /* We will never try scrolling more than this number of lines. */
14820 int scroll_limit = SCROLL_LIMIT;
14821 int frame_line_height = default_line_pixel_height (w);
14822 int window_total_lines
14823 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14824
14825 #ifdef GLYPH_DEBUG
14826 debug_method_add (w, "try_scrolling");
14827 #endif
14828
14829 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14830
14831 /* Compute scroll margin height in pixels. We scroll when point is
14832 within this distance from the top or bottom of the window. */
14833 if (scroll_margin > 0)
14834 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14835 * frame_line_height;
14836 else
14837 this_scroll_margin = 0;
14838
14839 /* Force arg_scroll_conservatively to have a reasonable value, to
14840 avoid scrolling too far away with slow move_it_* functions. Note
14841 that the user can supply scroll-conservatively equal to
14842 `most-positive-fixnum', which can be larger than INT_MAX. */
14843 if (arg_scroll_conservatively > scroll_limit)
14844 {
14845 arg_scroll_conservatively = scroll_limit + 1;
14846 scroll_max = scroll_limit * frame_line_height;
14847 }
14848 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14849 /* Compute how much we should try to scroll maximally to bring
14850 point into view. */
14851 scroll_max = (max (scroll_step,
14852 max (arg_scroll_conservatively, temp_scroll_step))
14853 * frame_line_height);
14854 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14855 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14856 /* We're trying to scroll because of aggressive scrolling but no
14857 scroll_step is set. Choose an arbitrary one. */
14858 scroll_max = 10 * frame_line_height;
14859 else
14860 scroll_max = 0;
14861
14862 too_near_end:
14863
14864 /* Decide whether to scroll down. */
14865 if (PT > CHARPOS (startp))
14866 {
14867 int scroll_margin_y;
14868
14869 /* Compute the pixel ypos of the scroll margin, then move IT to
14870 either that ypos or PT, whichever comes first. */
14871 start_display (&it, w, startp);
14872 scroll_margin_y = it.last_visible_y - this_scroll_margin
14873 - frame_line_height * extra_scroll_margin_lines;
14874 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14875 (MOVE_TO_POS | MOVE_TO_Y));
14876
14877 if (PT > CHARPOS (it.current.pos))
14878 {
14879 int y0 = line_bottom_y (&it);
14880 /* Compute how many pixels below window bottom to stop searching
14881 for PT. This avoids costly search for PT that is far away if
14882 the user limited scrolling by a small number of lines, but
14883 always finds PT if scroll_conservatively is set to a large
14884 number, such as most-positive-fixnum. */
14885 int slack = max (scroll_max, 10 * frame_line_height);
14886 int y_to_move = it.last_visible_y + slack;
14887
14888 /* Compute the distance from the scroll margin to PT or to
14889 the scroll limit, whichever comes first. This should
14890 include the height of the cursor line, to make that line
14891 fully visible. */
14892 move_it_to (&it, PT, -1, y_to_move,
14893 -1, MOVE_TO_POS | MOVE_TO_Y);
14894 dy = line_bottom_y (&it) - y0;
14895
14896 if (dy > scroll_max)
14897 return SCROLLING_FAILED;
14898
14899 if (dy > 0)
14900 scroll_down_p = 1;
14901 }
14902 }
14903
14904 if (scroll_down_p)
14905 {
14906 /* Point is in or below the bottom scroll margin, so move the
14907 window start down. If scrolling conservatively, move it just
14908 enough down to make point visible. If scroll_step is set,
14909 move it down by scroll_step. */
14910 if (arg_scroll_conservatively)
14911 amount_to_scroll
14912 = min (max (dy, frame_line_height),
14913 frame_line_height * arg_scroll_conservatively);
14914 else if (scroll_step || temp_scroll_step)
14915 amount_to_scroll = scroll_max;
14916 else
14917 {
14918 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14919 height = WINDOW_BOX_TEXT_HEIGHT (w);
14920 if (NUMBERP (aggressive))
14921 {
14922 double float_amount = XFLOATINT (aggressive) * height;
14923 int aggressive_scroll = float_amount;
14924 if (aggressive_scroll == 0 && float_amount > 0)
14925 aggressive_scroll = 1;
14926 /* Don't let point enter the scroll margin near top of
14927 the window. This could happen if the value of
14928 scroll_up_aggressively is too large and there are
14929 non-zero margins, because scroll_up_aggressively
14930 means put point that fraction of window height
14931 _from_the_bottom_margin_. */
14932 if (aggressive_scroll + 2*this_scroll_margin > height)
14933 aggressive_scroll = height - 2*this_scroll_margin;
14934 amount_to_scroll = dy + aggressive_scroll;
14935 }
14936 }
14937
14938 if (amount_to_scroll <= 0)
14939 return SCROLLING_FAILED;
14940
14941 start_display (&it, w, startp);
14942 if (arg_scroll_conservatively <= scroll_limit)
14943 move_it_vertically (&it, amount_to_scroll);
14944 else
14945 {
14946 /* Extra precision for users who set scroll-conservatively
14947 to a large number: make sure the amount we scroll
14948 the window start is never less than amount_to_scroll,
14949 which was computed as distance from window bottom to
14950 point. This matters when lines at window top and lines
14951 below window bottom have different height. */
14952 struct it it1;
14953 void *it1data = NULL;
14954 /* We use a temporary it1 because line_bottom_y can modify
14955 its argument, if it moves one line down; see there. */
14956 int start_y;
14957
14958 SAVE_IT (it1, it, it1data);
14959 start_y = line_bottom_y (&it1);
14960 do {
14961 RESTORE_IT (&it, &it, it1data);
14962 move_it_by_lines (&it, 1);
14963 SAVE_IT (it1, it, it1data);
14964 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14965 }
14966
14967 /* If STARTP is unchanged, move it down another screen line. */
14968 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14969 move_it_by_lines (&it, 1);
14970 startp = it.current.pos;
14971 }
14972 else
14973 {
14974 struct text_pos scroll_margin_pos = startp;
14975 int y_offset = 0;
14976
14977 /* See if point is inside the scroll margin at the top of the
14978 window. */
14979 if (this_scroll_margin)
14980 {
14981 int y_start;
14982
14983 start_display (&it, w, startp);
14984 y_start = it.current_y;
14985 move_it_vertically (&it, this_scroll_margin);
14986 scroll_margin_pos = it.current.pos;
14987 /* If we didn't move enough before hitting ZV, request
14988 additional amount of scroll, to move point out of the
14989 scroll margin. */
14990 if (IT_CHARPOS (it) == ZV
14991 && it.current_y - y_start < this_scroll_margin)
14992 y_offset = this_scroll_margin - (it.current_y - y_start);
14993 }
14994
14995 if (PT < CHARPOS (scroll_margin_pos))
14996 {
14997 /* Point is in the scroll margin at the top of the window or
14998 above what is displayed in the window. */
14999 int y0, y_to_move;
15000
15001 /* Compute the vertical distance from PT to the scroll
15002 margin position. Move as far as scroll_max allows, or
15003 one screenful, or 10 screen lines, whichever is largest.
15004 Give up if distance is greater than scroll_max or if we
15005 didn't reach the scroll margin position. */
15006 SET_TEXT_POS (pos, PT, PT_BYTE);
15007 start_display (&it, w, pos);
15008 y0 = it.current_y;
15009 y_to_move = max (it.last_visible_y,
15010 max (scroll_max, 10 * frame_line_height));
15011 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15012 y_to_move, -1,
15013 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15014 dy = it.current_y - y0;
15015 if (dy > scroll_max
15016 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15017 return SCROLLING_FAILED;
15018
15019 /* Additional scroll for when ZV was too close to point. */
15020 dy += y_offset;
15021
15022 /* Compute new window start. */
15023 start_display (&it, w, startp);
15024
15025 if (arg_scroll_conservatively)
15026 amount_to_scroll = max (dy, frame_line_height *
15027 max (scroll_step, temp_scroll_step));
15028 else if (scroll_step || temp_scroll_step)
15029 amount_to_scroll = scroll_max;
15030 else
15031 {
15032 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15033 height = WINDOW_BOX_TEXT_HEIGHT (w);
15034 if (NUMBERP (aggressive))
15035 {
15036 double float_amount = XFLOATINT (aggressive) * height;
15037 int aggressive_scroll = float_amount;
15038 if (aggressive_scroll == 0 && float_amount > 0)
15039 aggressive_scroll = 1;
15040 /* Don't let point enter the scroll margin near
15041 bottom of the window, if the value of
15042 scroll_down_aggressively happens to be too
15043 large. */
15044 if (aggressive_scroll + 2*this_scroll_margin > height)
15045 aggressive_scroll = height - 2*this_scroll_margin;
15046 amount_to_scroll = dy + aggressive_scroll;
15047 }
15048 }
15049
15050 if (amount_to_scroll <= 0)
15051 return SCROLLING_FAILED;
15052
15053 move_it_vertically_backward (&it, amount_to_scroll);
15054 startp = it.current.pos;
15055 }
15056 }
15057
15058 /* Run window scroll functions. */
15059 startp = run_window_scroll_functions (window, startp);
15060
15061 /* Display the window. Give up if new fonts are loaded, or if point
15062 doesn't appear. */
15063 if (!try_window (window, startp, 0))
15064 rc = SCROLLING_NEED_LARGER_MATRICES;
15065 else if (w->cursor.vpos < 0)
15066 {
15067 clear_glyph_matrix (w->desired_matrix);
15068 rc = SCROLLING_FAILED;
15069 }
15070 else
15071 {
15072 /* Maybe forget recorded base line for line number display. */
15073 if (!just_this_one_p
15074 || current_buffer->clip_changed
15075 || BEG_UNCHANGED < CHARPOS (startp))
15076 w->base_line_number = 0;
15077
15078 /* If cursor ends up on a partially visible line,
15079 treat that as being off the bottom of the screen. */
15080 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15081 /* It's possible that the cursor is on the first line of the
15082 buffer, which is partially obscured due to a vscroll
15083 (Bug#7537). In that case, avoid looping forever. */
15084 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15085 {
15086 clear_glyph_matrix (w->desired_matrix);
15087 ++extra_scroll_margin_lines;
15088 goto too_near_end;
15089 }
15090 rc = SCROLLING_SUCCESS;
15091 }
15092
15093 return rc;
15094 }
15095
15096
15097 /* Compute a suitable window start for window W if display of W starts
15098 on a continuation line. Value is non-zero if a new window start
15099 was computed.
15100
15101 The new window start will be computed, based on W's width, starting
15102 from the start of the continued line. It is the start of the
15103 screen line with the minimum distance from the old start W->start. */
15104
15105 static int
15106 compute_window_start_on_continuation_line (struct window *w)
15107 {
15108 struct text_pos pos, start_pos;
15109 int window_start_changed_p = 0;
15110
15111 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15112
15113 /* If window start is on a continuation line... Window start may be
15114 < BEGV in case there's invisible text at the start of the
15115 buffer (M-x rmail, for example). */
15116 if (CHARPOS (start_pos) > BEGV
15117 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15118 {
15119 struct it it;
15120 struct glyph_row *row;
15121
15122 /* Handle the case that the window start is out of range. */
15123 if (CHARPOS (start_pos) < BEGV)
15124 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15125 else if (CHARPOS (start_pos) > ZV)
15126 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15127
15128 /* Find the start of the continued line. This should be fast
15129 because find_newline is fast (newline cache). */
15130 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15131 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15132 row, DEFAULT_FACE_ID);
15133 reseat_at_previous_visible_line_start (&it);
15134
15135 /* If the line start is "too far" away from the window start,
15136 say it takes too much time to compute a new window start. */
15137 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15138 /* PXW: Do we need upper bounds here? */
15139 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15140 {
15141 int min_distance, distance;
15142
15143 /* Move forward by display lines to find the new window
15144 start. If window width was enlarged, the new start can
15145 be expected to be > the old start. If window width was
15146 decreased, the new window start will be < the old start.
15147 So, we're looking for the display line start with the
15148 minimum distance from the old window start. */
15149 pos = it.current.pos;
15150 min_distance = INFINITY;
15151 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15152 distance < min_distance)
15153 {
15154 min_distance = distance;
15155 pos = it.current.pos;
15156 if (it.line_wrap == WORD_WRAP)
15157 {
15158 /* Under WORD_WRAP, move_it_by_lines is likely to
15159 overshoot and stop not at the first, but the
15160 second character from the left margin. So in
15161 that case, we need a more tight control on the X
15162 coordinate of the iterator than move_it_by_lines
15163 promises in its contract. The method is to first
15164 go to the last (rightmost) visible character of a
15165 line, then move to the leftmost character on the
15166 next line in a separate call. */
15167 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15168 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15169 move_it_to (&it, ZV, 0,
15170 it.current_y + it.max_ascent + it.max_descent, -1,
15171 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15172 }
15173 else
15174 move_it_by_lines (&it, 1);
15175 }
15176
15177 /* Set the window start there. */
15178 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15179 window_start_changed_p = 1;
15180 }
15181 }
15182
15183 return window_start_changed_p;
15184 }
15185
15186
15187 /* Try cursor movement in case text has not changed in window WINDOW,
15188 with window start STARTP. Value is
15189
15190 CURSOR_MOVEMENT_SUCCESS if successful
15191
15192 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15193
15194 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15195 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15196 we want to scroll as if scroll-step were set to 1. See the code.
15197
15198 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15199 which case we have to abort this redisplay, and adjust matrices
15200 first. */
15201
15202 enum
15203 {
15204 CURSOR_MOVEMENT_SUCCESS,
15205 CURSOR_MOVEMENT_CANNOT_BE_USED,
15206 CURSOR_MOVEMENT_MUST_SCROLL,
15207 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15208 };
15209
15210 static int
15211 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15212 {
15213 struct window *w = XWINDOW (window);
15214 struct frame *f = XFRAME (w->frame);
15215 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15216
15217 #ifdef GLYPH_DEBUG
15218 if (inhibit_try_cursor_movement)
15219 return rc;
15220 #endif
15221
15222 /* Previously, there was a check for Lisp integer in the
15223 if-statement below. Now, this field is converted to
15224 ptrdiff_t, thus zero means invalid position in a buffer. */
15225 eassert (w->last_point > 0);
15226 /* Likewise there was a check whether window_end_vpos is nil or larger
15227 than the window. Now window_end_vpos is int and so never nil, but
15228 let's leave eassert to check whether it fits in the window. */
15229 eassert (w->window_end_vpos < w->current_matrix->nrows);
15230
15231 /* Handle case where text has not changed, only point, and it has
15232 not moved off the frame. */
15233 if (/* Point may be in this window. */
15234 PT >= CHARPOS (startp)
15235 /* Selective display hasn't changed. */
15236 && !current_buffer->clip_changed
15237 /* Function force-mode-line-update is used to force a thorough
15238 redisplay. It sets either windows_or_buffers_changed or
15239 update_mode_lines. So don't take a shortcut here for these
15240 cases. */
15241 && !update_mode_lines
15242 && !windows_or_buffers_changed
15243 && !f->cursor_type_changed
15244 && NILP (Vshow_trailing_whitespace)
15245 /* This code is not used for mini-buffer for the sake of the case
15246 of redisplaying to replace an echo area message; since in
15247 that case the mini-buffer contents per se are usually
15248 unchanged. This code is of no real use in the mini-buffer
15249 since the handling of this_line_start_pos, etc., in redisplay
15250 handles the same cases. */
15251 && !EQ (window, minibuf_window)
15252 && (FRAME_WINDOW_P (f)
15253 || !overlay_arrow_in_current_buffer_p ()))
15254 {
15255 int this_scroll_margin, top_scroll_margin;
15256 struct glyph_row *row = NULL;
15257 int frame_line_height = default_line_pixel_height (w);
15258 int window_total_lines
15259 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15260
15261 #ifdef GLYPH_DEBUG
15262 debug_method_add (w, "cursor movement");
15263 #endif
15264
15265 /* Scroll if point within this distance from the top or bottom
15266 of the window. This is a pixel value. */
15267 if (scroll_margin > 0)
15268 {
15269 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15270 this_scroll_margin *= frame_line_height;
15271 }
15272 else
15273 this_scroll_margin = 0;
15274
15275 top_scroll_margin = this_scroll_margin;
15276 if (WINDOW_WANTS_HEADER_LINE_P (w))
15277 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15278
15279 /* Start with the row the cursor was displayed during the last
15280 not paused redisplay. Give up if that row is not valid. */
15281 if (w->last_cursor_vpos < 0
15282 || w->last_cursor_vpos >= w->current_matrix->nrows)
15283 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15284 else
15285 {
15286 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15287 if (row->mode_line_p)
15288 ++row;
15289 if (!row->enabled_p)
15290 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15291 }
15292
15293 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15294 {
15295 int scroll_p = 0, must_scroll = 0;
15296 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15297
15298 if (PT > w->last_point)
15299 {
15300 /* Point has moved forward. */
15301 while (MATRIX_ROW_END_CHARPOS (row) < PT
15302 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15303 {
15304 eassert (row->enabled_p);
15305 ++row;
15306 }
15307
15308 /* If the end position of a row equals the start
15309 position of the next row, and PT is at that position,
15310 we would rather display cursor in the next line. */
15311 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15312 && MATRIX_ROW_END_CHARPOS (row) == PT
15313 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15314 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15315 && !cursor_row_p (row))
15316 ++row;
15317
15318 /* If within the scroll margin, scroll. Note that
15319 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15320 the next line would be drawn, and that
15321 this_scroll_margin can be zero. */
15322 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15323 || PT > MATRIX_ROW_END_CHARPOS (row)
15324 /* Line is completely visible last line in window
15325 and PT is to be set in the next line. */
15326 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15327 && PT == MATRIX_ROW_END_CHARPOS (row)
15328 && !row->ends_at_zv_p
15329 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15330 scroll_p = 1;
15331 }
15332 else if (PT < w->last_point)
15333 {
15334 /* Cursor has to be moved backward. Note that PT >=
15335 CHARPOS (startp) because of the outer if-statement. */
15336 while (!row->mode_line_p
15337 && (MATRIX_ROW_START_CHARPOS (row) > PT
15338 || (MATRIX_ROW_START_CHARPOS (row) == PT
15339 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15340 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15341 row > w->current_matrix->rows
15342 && (row-1)->ends_in_newline_from_string_p))))
15343 && (row->y > top_scroll_margin
15344 || CHARPOS (startp) == BEGV))
15345 {
15346 eassert (row->enabled_p);
15347 --row;
15348 }
15349
15350 /* Consider the following case: Window starts at BEGV,
15351 there is invisible, intangible text at BEGV, so that
15352 display starts at some point START > BEGV. It can
15353 happen that we are called with PT somewhere between
15354 BEGV and START. Try to handle that case. */
15355 if (row < w->current_matrix->rows
15356 || row->mode_line_p)
15357 {
15358 row = w->current_matrix->rows;
15359 if (row->mode_line_p)
15360 ++row;
15361 }
15362
15363 /* Due to newlines in overlay strings, we may have to
15364 skip forward over overlay strings. */
15365 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15366 && MATRIX_ROW_END_CHARPOS (row) == PT
15367 && !cursor_row_p (row))
15368 ++row;
15369
15370 /* If within the scroll margin, scroll. */
15371 if (row->y < top_scroll_margin
15372 && CHARPOS (startp) != BEGV)
15373 scroll_p = 1;
15374 }
15375 else
15376 {
15377 /* Cursor did not move. So don't scroll even if cursor line
15378 is partially visible, as it was so before. */
15379 rc = CURSOR_MOVEMENT_SUCCESS;
15380 }
15381
15382 if (PT < MATRIX_ROW_START_CHARPOS (row)
15383 || PT > MATRIX_ROW_END_CHARPOS (row))
15384 {
15385 /* if PT is not in the glyph row, give up. */
15386 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15387 must_scroll = 1;
15388 }
15389 else if (rc != CURSOR_MOVEMENT_SUCCESS
15390 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15391 {
15392 struct glyph_row *row1;
15393
15394 /* If rows are bidi-reordered and point moved, back up
15395 until we find a row that does not belong to a
15396 continuation line. This is because we must consider
15397 all rows of a continued line as candidates for the
15398 new cursor positioning, since row start and end
15399 positions change non-linearly with vertical position
15400 in such rows. */
15401 /* FIXME: Revisit this when glyph ``spilling'' in
15402 continuation lines' rows is implemented for
15403 bidi-reordered rows. */
15404 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15405 MATRIX_ROW_CONTINUATION_LINE_P (row);
15406 --row)
15407 {
15408 /* If we hit the beginning of the displayed portion
15409 without finding the first row of a continued
15410 line, give up. */
15411 if (row <= row1)
15412 {
15413 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15414 break;
15415 }
15416 eassert (row->enabled_p);
15417 }
15418 }
15419 if (must_scroll)
15420 ;
15421 else if (rc != CURSOR_MOVEMENT_SUCCESS
15422 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15423 /* Make sure this isn't a header line by any chance, since
15424 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15425 && !row->mode_line_p
15426 && make_cursor_line_fully_visible_p)
15427 {
15428 if (PT == MATRIX_ROW_END_CHARPOS (row)
15429 && !row->ends_at_zv_p
15430 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15431 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15432 else if (row->height > window_box_height (w))
15433 {
15434 /* If we end up in a partially visible line, let's
15435 make it fully visible, except when it's taller
15436 than the window, in which case we can't do much
15437 about it. */
15438 *scroll_step = 1;
15439 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15440 }
15441 else
15442 {
15443 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15444 if (!cursor_row_fully_visible_p (w, 0, 1))
15445 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15446 else
15447 rc = CURSOR_MOVEMENT_SUCCESS;
15448 }
15449 }
15450 else if (scroll_p)
15451 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15452 else if (rc != CURSOR_MOVEMENT_SUCCESS
15453 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15454 {
15455 /* With bidi-reordered rows, there could be more than
15456 one candidate row whose start and end positions
15457 occlude point. We need to let set_cursor_from_row
15458 find the best candidate. */
15459 /* FIXME: Revisit this when glyph ``spilling'' in
15460 continuation lines' rows is implemented for
15461 bidi-reordered rows. */
15462 int rv = 0;
15463
15464 do
15465 {
15466 int at_zv_p = 0, exact_match_p = 0;
15467
15468 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15469 && PT <= MATRIX_ROW_END_CHARPOS (row)
15470 && cursor_row_p (row))
15471 rv |= set_cursor_from_row (w, row, w->current_matrix,
15472 0, 0, 0, 0);
15473 /* As soon as we've found the exact match for point,
15474 or the first suitable row whose ends_at_zv_p flag
15475 is set, we are done. */
15476 at_zv_p =
15477 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15478 if (rv && !at_zv_p
15479 && w->cursor.hpos >= 0
15480 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15481 w->cursor.vpos))
15482 {
15483 struct glyph_row *candidate =
15484 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15485 struct glyph *g =
15486 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15487 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15488
15489 exact_match_p =
15490 (BUFFERP (g->object) && g->charpos == PT)
15491 || (INTEGERP (g->object)
15492 && (g->charpos == PT
15493 || (g->charpos == 0 && endpos - 1 == PT)));
15494 }
15495 if (rv && (at_zv_p || exact_match_p))
15496 {
15497 rc = CURSOR_MOVEMENT_SUCCESS;
15498 break;
15499 }
15500 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15501 break;
15502 ++row;
15503 }
15504 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15505 || row->continued_p)
15506 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15507 || (MATRIX_ROW_START_CHARPOS (row) == PT
15508 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15509 /* If we didn't find any candidate rows, or exited the
15510 loop before all the candidates were examined, signal
15511 to the caller that this method failed. */
15512 if (rc != CURSOR_MOVEMENT_SUCCESS
15513 && !(rv
15514 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15515 && !row->continued_p))
15516 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15517 else if (rv)
15518 rc = CURSOR_MOVEMENT_SUCCESS;
15519 }
15520 else
15521 {
15522 do
15523 {
15524 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15525 {
15526 rc = CURSOR_MOVEMENT_SUCCESS;
15527 break;
15528 }
15529 ++row;
15530 }
15531 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15532 && MATRIX_ROW_START_CHARPOS (row) == PT
15533 && cursor_row_p (row));
15534 }
15535 }
15536 }
15537
15538 return rc;
15539 }
15540
15541 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15542 static
15543 #endif
15544 void
15545 set_vertical_scroll_bar (struct window *w)
15546 {
15547 ptrdiff_t start, end, whole;
15548
15549 /* Calculate the start and end positions for the current window.
15550 At some point, it would be nice to choose between scrollbars
15551 which reflect the whole buffer size, with special markers
15552 indicating narrowing, and scrollbars which reflect only the
15553 visible region.
15554
15555 Note that mini-buffers sometimes aren't displaying any text. */
15556 if (!MINI_WINDOW_P (w)
15557 || (w == XWINDOW (minibuf_window)
15558 && NILP (echo_area_buffer[0])))
15559 {
15560 struct buffer *buf = XBUFFER (w->contents);
15561 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15562 start = marker_position (w->start) - BUF_BEGV (buf);
15563 /* I don't think this is guaranteed to be right. For the
15564 moment, we'll pretend it is. */
15565 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15566
15567 if (end < start)
15568 end = start;
15569 if (whole < (end - start))
15570 whole = end - start;
15571 }
15572 else
15573 start = end = whole = 0;
15574
15575 /* Indicate what this scroll bar ought to be displaying now. */
15576 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15577 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15578 (w, end - start, whole, start);
15579 }
15580
15581
15582 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15583 selected_window is redisplayed.
15584
15585 We can return without actually redisplaying the window if fonts has been
15586 changed on window's frame. In that case, redisplay_internal will retry. */
15587
15588 static void
15589 redisplay_window (Lisp_Object window, bool just_this_one_p)
15590 {
15591 struct window *w = XWINDOW (window);
15592 struct frame *f = XFRAME (w->frame);
15593 struct buffer *buffer = XBUFFER (w->contents);
15594 struct buffer *old = current_buffer;
15595 struct text_pos lpoint, opoint, startp;
15596 int update_mode_line;
15597 int tem;
15598 struct it it;
15599 /* Record it now because it's overwritten. */
15600 bool current_matrix_up_to_date_p = false;
15601 bool used_current_matrix_p = false;
15602 /* This is less strict than current_matrix_up_to_date_p.
15603 It indicates that the buffer contents and narrowing are unchanged. */
15604 bool buffer_unchanged_p = false;
15605 int temp_scroll_step = 0;
15606 ptrdiff_t count = SPECPDL_INDEX ();
15607 int rc;
15608 int centering_position = -1;
15609 int last_line_misfit = 0;
15610 ptrdiff_t beg_unchanged, end_unchanged;
15611 int frame_line_height;
15612
15613 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15614 opoint = lpoint;
15615
15616 #ifdef GLYPH_DEBUG
15617 *w->desired_matrix->method = 0;
15618 #endif
15619
15620 if (!just_this_one_p
15621 && REDISPLAY_SOME_P ()
15622 && !w->redisplay
15623 && !f->redisplay
15624 && !buffer->text->redisplay
15625 && BUF_PT (buffer) == w->last_point)
15626 return;
15627
15628 /* Make sure that both W's markers are valid. */
15629 eassert (XMARKER (w->start)->buffer == buffer);
15630 eassert (XMARKER (w->pointm)->buffer == buffer);
15631
15632 restart:
15633 reconsider_clip_changes (w);
15634 frame_line_height = default_line_pixel_height (w);
15635
15636 /* Has the mode line to be updated? */
15637 update_mode_line = (w->update_mode_line
15638 || update_mode_lines
15639 || buffer->clip_changed
15640 || buffer->prevent_redisplay_optimizations_p);
15641
15642 if (!just_this_one_p)
15643 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15644 cleverly elsewhere. */
15645 w->must_be_updated_p = true;
15646
15647 if (MINI_WINDOW_P (w))
15648 {
15649 if (w == XWINDOW (echo_area_window)
15650 && !NILP (echo_area_buffer[0]))
15651 {
15652 if (update_mode_line)
15653 /* We may have to update a tty frame's menu bar or a
15654 tool-bar. Example `M-x C-h C-h C-g'. */
15655 goto finish_menu_bars;
15656 else
15657 /* We've already displayed the echo area glyphs in this window. */
15658 goto finish_scroll_bars;
15659 }
15660 else if ((w != XWINDOW (minibuf_window)
15661 || minibuf_level == 0)
15662 /* When buffer is nonempty, redisplay window normally. */
15663 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15664 /* Quail displays non-mini buffers in minibuffer window.
15665 In that case, redisplay the window normally. */
15666 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15667 {
15668 /* W is a mini-buffer window, but it's not active, so clear
15669 it. */
15670 int yb = window_text_bottom_y (w);
15671 struct glyph_row *row;
15672 int y;
15673
15674 for (y = 0, row = w->desired_matrix->rows;
15675 y < yb;
15676 y += row->height, ++row)
15677 blank_row (w, row, y);
15678 goto finish_scroll_bars;
15679 }
15680
15681 clear_glyph_matrix (w->desired_matrix);
15682 }
15683
15684 /* Otherwise set up data on this window; select its buffer and point
15685 value. */
15686 /* Really select the buffer, for the sake of buffer-local
15687 variables. */
15688 set_buffer_internal_1 (XBUFFER (w->contents));
15689
15690 current_matrix_up_to_date_p
15691 = (w->window_end_valid
15692 && !current_buffer->clip_changed
15693 && !current_buffer->prevent_redisplay_optimizations_p
15694 && !window_outdated (w));
15695
15696 /* Run the window-bottom-change-functions
15697 if it is possible that the text on the screen has changed
15698 (either due to modification of the text, or any other reason). */
15699 if (!current_matrix_up_to_date_p
15700 && !NILP (Vwindow_text_change_functions))
15701 {
15702 safe_run_hooks (Qwindow_text_change_functions);
15703 goto restart;
15704 }
15705
15706 beg_unchanged = BEG_UNCHANGED;
15707 end_unchanged = END_UNCHANGED;
15708
15709 SET_TEXT_POS (opoint, PT, PT_BYTE);
15710
15711 specbind (Qinhibit_point_motion_hooks, Qt);
15712
15713 buffer_unchanged_p
15714 = (w->window_end_valid
15715 && !current_buffer->clip_changed
15716 && !window_outdated (w));
15717
15718 /* When windows_or_buffers_changed is non-zero, we can't rely
15719 on the window end being valid, so set it to zero there. */
15720 if (windows_or_buffers_changed)
15721 {
15722 /* If window starts on a continuation line, maybe adjust the
15723 window start in case the window's width changed. */
15724 if (XMARKER (w->start)->buffer == current_buffer)
15725 compute_window_start_on_continuation_line (w);
15726
15727 w->window_end_valid = false;
15728 /* If so, we also can't rely on current matrix
15729 and should not fool try_cursor_movement below. */
15730 current_matrix_up_to_date_p = false;
15731 }
15732
15733 /* Some sanity checks. */
15734 CHECK_WINDOW_END (w);
15735 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15736 emacs_abort ();
15737 if (BYTEPOS (opoint) < CHARPOS (opoint))
15738 emacs_abort ();
15739
15740 if (mode_line_update_needed (w))
15741 update_mode_line = 1;
15742
15743 /* Point refers normally to the selected window. For any other
15744 window, set up appropriate value. */
15745 if (!EQ (window, selected_window))
15746 {
15747 ptrdiff_t new_pt = marker_position (w->pointm);
15748 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15749 if (new_pt < BEGV)
15750 {
15751 new_pt = BEGV;
15752 new_pt_byte = BEGV_BYTE;
15753 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15754 }
15755 else if (new_pt > (ZV - 1))
15756 {
15757 new_pt = ZV;
15758 new_pt_byte = ZV_BYTE;
15759 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15760 }
15761
15762 /* We don't use SET_PT so that the point-motion hooks don't run. */
15763 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15764 }
15765
15766 /* If any of the character widths specified in the display table
15767 have changed, invalidate the width run cache. It's true that
15768 this may be a bit late to catch such changes, but the rest of
15769 redisplay goes (non-fatally) haywire when the display table is
15770 changed, so why should we worry about doing any better? */
15771 if (current_buffer->width_run_cache
15772 || (current_buffer->base_buffer
15773 && current_buffer->base_buffer->width_run_cache))
15774 {
15775 struct Lisp_Char_Table *disptab = buffer_display_table ();
15776
15777 if (! disptab_matches_widthtab
15778 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15779 {
15780 struct buffer *buf = current_buffer;
15781
15782 if (buf->base_buffer)
15783 buf = buf->base_buffer;
15784 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15785 recompute_width_table (current_buffer, disptab);
15786 }
15787 }
15788
15789 /* If window-start is screwed up, choose a new one. */
15790 if (XMARKER (w->start)->buffer != current_buffer)
15791 goto recenter;
15792
15793 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15794
15795 /* If someone specified a new starting point but did not insist,
15796 check whether it can be used. */
15797 if (w->optional_new_start
15798 && CHARPOS (startp) >= BEGV
15799 && CHARPOS (startp) <= ZV)
15800 {
15801 w->optional_new_start = 0;
15802 start_display (&it, w, startp);
15803 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15804 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15805 if (IT_CHARPOS (it) == PT)
15806 w->force_start = 1;
15807 /* IT may overshoot PT if text at PT is invisible. */
15808 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15809 w->force_start = 1;
15810 }
15811
15812 force_start:
15813
15814 /* Handle case where place to start displaying has been specified,
15815 unless the specified location is outside the accessible range. */
15816 if (w->force_start || window_frozen_p (w))
15817 {
15818 /* We set this later on if we have to adjust point. */
15819 int new_vpos = -1;
15820
15821 w->force_start = 0;
15822 w->vscroll = 0;
15823 w->window_end_valid = 0;
15824
15825 /* Forget any recorded base line for line number display. */
15826 if (!buffer_unchanged_p)
15827 w->base_line_number = 0;
15828
15829 /* Redisplay the mode line. Select the buffer properly for that.
15830 Also, run the hook window-scroll-functions
15831 because we have scrolled. */
15832 /* Note, we do this after clearing force_start because
15833 if there's an error, it is better to forget about force_start
15834 than to get into an infinite loop calling the hook functions
15835 and having them get more errors. */
15836 if (!update_mode_line
15837 || ! NILP (Vwindow_scroll_functions))
15838 {
15839 update_mode_line = 1;
15840 w->update_mode_line = 1;
15841 startp = run_window_scroll_functions (window, startp);
15842 }
15843
15844 if (CHARPOS (startp) < BEGV)
15845 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15846 else if (CHARPOS (startp) > ZV)
15847 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15848
15849 /* Redisplay, then check if cursor has been set during the
15850 redisplay. Give up if new fonts were loaded. */
15851 /* We used to issue a CHECK_MARGINS argument to try_window here,
15852 but this causes scrolling to fail when point begins inside
15853 the scroll margin (bug#148) -- cyd */
15854 if (!try_window (window, startp, 0))
15855 {
15856 w->force_start = 1;
15857 clear_glyph_matrix (w->desired_matrix);
15858 goto need_larger_matrices;
15859 }
15860
15861 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15862 {
15863 /* If point does not appear, try to move point so it does
15864 appear. The desired matrix has been built above, so we
15865 can use it here. */
15866 new_vpos = window_box_height (w) / 2;
15867 }
15868
15869 if (!cursor_row_fully_visible_p (w, 0, 0))
15870 {
15871 /* Point does appear, but on a line partly visible at end of window.
15872 Move it back to a fully-visible line. */
15873 new_vpos = window_box_height (w);
15874 }
15875 else if (w->cursor.vpos >= 0)
15876 {
15877 /* Some people insist on not letting point enter the scroll
15878 margin, even though this part handles windows that didn't
15879 scroll at all. */
15880 int window_total_lines
15881 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15882 int margin = min (scroll_margin, window_total_lines / 4);
15883 int pixel_margin = margin * frame_line_height;
15884 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15885
15886 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15887 below, which finds the row to move point to, advances by
15888 the Y coordinate of the _next_ row, see the definition of
15889 MATRIX_ROW_BOTTOM_Y. */
15890 if (w->cursor.vpos < margin + header_line)
15891 {
15892 w->cursor.vpos = -1;
15893 clear_glyph_matrix (w->desired_matrix);
15894 goto try_to_scroll;
15895 }
15896 else
15897 {
15898 int window_height = window_box_height (w);
15899
15900 if (header_line)
15901 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15902 if (w->cursor.y >= window_height - pixel_margin)
15903 {
15904 w->cursor.vpos = -1;
15905 clear_glyph_matrix (w->desired_matrix);
15906 goto try_to_scroll;
15907 }
15908 }
15909 }
15910
15911 /* If we need to move point for either of the above reasons,
15912 now actually do it. */
15913 if (new_vpos >= 0)
15914 {
15915 struct glyph_row *row;
15916
15917 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15918 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15919 ++row;
15920
15921 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15922 MATRIX_ROW_START_BYTEPOS (row));
15923
15924 if (w != XWINDOW (selected_window))
15925 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15926 else if (current_buffer == old)
15927 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15928
15929 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15930
15931 /* If we are highlighting the region, then we just changed
15932 the region, so redisplay to show it. */
15933 /* FIXME: We need to (re)run pre-redisplay-function! */
15934 /* if (markpos_of_region () >= 0)
15935 {
15936 clear_glyph_matrix (w->desired_matrix);
15937 if (!try_window (window, startp, 0))
15938 goto need_larger_matrices;
15939 }
15940 */
15941 }
15942
15943 #ifdef GLYPH_DEBUG
15944 debug_method_add (w, "forced window start");
15945 #endif
15946 goto done;
15947 }
15948
15949 /* Handle case where text has not changed, only point, and it has
15950 not moved off the frame, and we are not retrying after hscroll.
15951 (current_matrix_up_to_date_p is nonzero when retrying.) */
15952 if (current_matrix_up_to_date_p
15953 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15954 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15955 {
15956 switch (rc)
15957 {
15958 case CURSOR_MOVEMENT_SUCCESS:
15959 used_current_matrix_p = 1;
15960 goto done;
15961
15962 case CURSOR_MOVEMENT_MUST_SCROLL:
15963 goto try_to_scroll;
15964
15965 default:
15966 emacs_abort ();
15967 }
15968 }
15969 /* If current starting point was originally the beginning of a line
15970 but no longer is, find a new starting point. */
15971 else if (w->start_at_line_beg
15972 && !(CHARPOS (startp) <= BEGV
15973 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15974 {
15975 #ifdef GLYPH_DEBUG
15976 debug_method_add (w, "recenter 1");
15977 #endif
15978 goto recenter;
15979 }
15980
15981 /* Try scrolling with try_window_id. Value is > 0 if update has
15982 been done, it is -1 if we know that the same window start will
15983 not work. It is 0 if unsuccessful for some other reason. */
15984 else if ((tem = try_window_id (w)) != 0)
15985 {
15986 #ifdef GLYPH_DEBUG
15987 debug_method_add (w, "try_window_id %d", tem);
15988 #endif
15989
15990 if (f->fonts_changed)
15991 goto need_larger_matrices;
15992 if (tem > 0)
15993 goto done;
15994
15995 /* Otherwise try_window_id has returned -1 which means that we
15996 don't want the alternative below this comment to execute. */
15997 }
15998 else if (CHARPOS (startp) >= BEGV
15999 && CHARPOS (startp) <= ZV
16000 && PT >= CHARPOS (startp)
16001 && (CHARPOS (startp) < ZV
16002 /* Avoid starting at end of buffer. */
16003 || CHARPOS (startp) == BEGV
16004 || !window_outdated (w)))
16005 {
16006 int d1, d2, d3, d4, d5, d6;
16007
16008 /* If first window line is a continuation line, and window start
16009 is inside the modified region, but the first change is before
16010 current window start, we must select a new window start.
16011
16012 However, if this is the result of a down-mouse event (e.g. by
16013 extending the mouse-drag-overlay), we don't want to select a
16014 new window start, since that would change the position under
16015 the mouse, resulting in an unwanted mouse-movement rather
16016 than a simple mouse-click. */
16017 if (!w->start_at_line_beg
16018 && NILP (do_mouse_tracking)
16019 && CHARPOS (startp) > BEGV
16020 && CHARPOS (startp) > BEG + beg_unchanged
16021 && CHARPOS (startp) <= Z - end_unchanged
16022 /* Even if w->start_at_line_beg is nil, a new window may
16023 start at a line_beg, since that's how set_buffer_window
16024 sets it. So, we need to check the return value of
16025 compute_window_start_on_continuation_line. (See also
16026 bug#197). */
16027 && XMARKER (w->start)->buffer == current_buffer
16028 && compute_window_start_on_continuation_line (w)
16029 /* It doesn't make sense to force the window start like we
16030 do at label force_start if it is already known that point
16031 will not be visible in the resulting window, because
16032 doing so will move point from its correct position
16033 instead of scrolling the window to bring point into view.
16034 See bug#9324. */
16035 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16036 {
16037 w->force_start = 1;
16038 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16039 goto force_start;
16040 }
16041
16042 #ifdef GLYPH_DEBUG
16043 debug_method_add (w, "same window start");
16044 #endif
16045
16046 /* Try to redisplay starting at same place as before.
16047 If point has not moved off frame, accept the results. */
16048 if (!current_matrix_up_to_date_p
16049 /* Don't use try_window_reusing_current_matrix in this case
16050 because a window scroll function can have changed the
16051 buffer. */
16052 || !NILP (Vwindow_scroll_functions)
16053 || MINI_WINDOW_P (w)
16054 || !(used_current_matrix_p
16055 = try_window_reusing_current_matrix (w)))
16056 {
16057 IF_DEBUG (debug_method_add (w, "1"));
16058 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16059 /* -1 means we need to scroll.
16060 0 means we need new matrices, but fonts_changed
16061 is set in that case, so we will detect it below. */
16062 goto try_to_scroll;
16063 }
16064
16065 if (f->fonts_changed)
16066 goto need_larger_matrices;
16067
16068 if (w->cursor.vpos >= 0)
16069 {
16070 if (!just_this_one_p
16071 || current_buffer->clip_changed
16072 || BEG_UNCHANGED < CHARPOS (startp))
16073 /* Forget any recorded base line for line number display. */
16074 w->base_line_number = 0;
16075
16076 if (!cursor_row_fully_visible_p (w, 1, 0))
16077 {
16078 clear_glyph_matrix (w->desired_matrix);
16079 last_line_misfit = 1;
16080 }
16081 /* Drop through and scroll. */
16082 else
16083 goto done;
16084 }
16085 else
16086 clear_glyph_matrix (w->desired_matrix);
16087 }
16088
16089 try_to_scroll:
16090
16091 /* Redisplay the mode line. Select the buffer properly for that. */
16092 if (!update_mode_line)
16093 {
16094 update_mode_line = 1;
16095 w->update_mode_line = 1;
16096 }
16097
16098 /* Try to scroll by specified few lines. */
16099 if ((scroll_conservatively
16100 || emacs_scroll_step
16101 || temp_scroll_step
16102 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16103 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16104 && CHARPOS (startp) >= BEGV
16105 && CHARPOS (startp) <= ZV)
16106 {
16107 /* The function returns -1 if new fonts were loaded, 1 if
16108 successful, 0 if not successful. */
16109 int ss = try_scrolling (window, just_this_one_p,
16110 scroll_conservatively,
16111 emacs_scroll_step,
16112 temp_scroll_step, last_line_misfit);
16113 switch (ss)
16114 {
16115 case SCROLLING_SUCCESS:
16116 goto done;
16117
16118 case SCROLLING_NEED_LARGER_MATRICES:
16119 goto need_larger_matrices;
16120
16121 case SCROLLING_FAILED:
16122 break;
16123
16124 default:
16125 emacs_abort ();
16126 }
16127 }
16128
16129 /* Finally, just choose a place to start which positions point
16130 according to user preferences. */
16131
16132 recenter:
16133
16134 #ifdef GLYPH_DEBUG
16135 debug_method_add (w, "recenter");
16136 #endif
16137
16138 /* Forget any previously recorded base line for line number display. */
16139 if (!buffer_unchanged_p)
16140 w->base_line_number = 0;
16141
16142 /* Determine the window start relative to point. */
16143 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16144 it.current_y = it.last_visible_y;
16145 if (centering_position < 0)
16146 {
16147 int window_total_lines
16148 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16149 int margin =
16150 scroll_margin > 0
16151 ? min (scroll_margin, window_total_lines / 4)
16152 : 0;
16153 ptrdiff_t margin_pos = CHARPOS (startp);
16154 Lisp_Object aggressive;
16155 int scrolling_up;
16156
16157 /* If there is a scroll margin at the top of the window, find
16158 its character position. */
16159 if (margin
16160 /* Cannot call start_display if startp is not in the
16161 accessible region of the buffer. This can happen when we
16162 have just switched to a different buffer and/or changed
16163 its restriction. In that case, startp is initialized to
16164 the character position 1 (BEGV) because we did not yet
16165 have chance to display the buffer even once. */
16166 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16167 {
16168 struct it it1;
16169 void *it1data = NULL;
16170
16171 SAVE_IT (it1, it, it1data);
16172 start_display (&it1, w, startp);
16173 move_it_vertically (&it1, margin * frame_line_height);
16174 margin_pos = IT_CHARPOS (it1);
16175 RESTORE_IT (&it, &it, it1data);
16176 }
16177 scrolling_up = PT > margin_pos;
16178 aggressive =
16179 scrolling_up
16180 ? BVAR (current_buffer, scroll_up_aggressively)
16181 : BVAR (current_buffer, scroll_down_aggressively);
16182
16183 if (!MINI_WINDOW_P (w)
16184 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16185 {
16186 int pt_offset = 0;
16187
16188 /* Setting scroll-conservatively overrides
16189 scroll-*-aggressively. */
16190 if (!scroll_conservatively && NUMBERP (aggressive))
16191 {
16192 double float_amount = XFLOATINT (aggressive);
16193
16194 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16195 if (pt_offset == 0 && float_amount > 0)
16196 pt_offset = 1;
16197 if (pt_offset && margin > 0)
16198 margin -= 1;
16199 }
16200 /* Compute how much to move the window start backward from
16201 point so that point will be displayed where the user
16202 wants it. */
16203 if (scrolling_up)
16204 {
16205 centering_position = it.last_visible_y;
16206 if (pt_offset)
16207 centering_position -= pt_offset;
16208 centering_position -=
16209 frame_line_height * (1 + margin + (last_line_misfit != 0))
16210 + WINDOW_HEADER_LINE_HEIGHT (w);
16211 /* Don't let point enter the scroll margin near top of
16212 the window. */
16213 if (centering_position < margin * frame_line_height)
16214 centering_position = margin * frame_line_height;
16215 }
16216 else
16217 centering_position = margin * frame_line_height + pt_offset;
16218 }
16219 else
16220 /* Set the window start half the height of the window backward
16221 from point. */
16222 centering_position = window_box_height (w) / 2;
16223 }
16224 move_it_vertically_backward (&it, centering_position);
16225
16226 eassert (IT_CHARPOS (it) >= BEGV);
16227
16228 /* The function move_it_vertically_backward may move over more
16229 than the specified y-distance. If it->w is small, e.g. a
16230 mini-buffer window, we may end up in front of the window's
16231 display area. Start displaying at the start of the line
16232 containing PT in this case. */
16233 if (it.current_y <= 0)
16234 {
16235 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16236 move_it_vertically_backward (&it, 0);
16237 it.current_y = 0;
16238 }
16239
16240 it.current_x = it.hpos = 0;
16241
16242 /* Set the window start position here explicitly, to avoid an
16243 infinite loop in case the functions in window-scroll-functions
16244 get errors. */
16245 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16246
16247 /* Run scroll hooks. */
16248 startp = run_window_scroll_functions (window, it.current.pos);
16249
16250 /* Redisplay the window. */
16251 if (!current_matrix_up_to_date_p
16252 || windows_or_buffers_changed
16253 || f->cursor_type_changed
16254 /* Don't use try_window_reusing_current_matrix in this case
16255 because it can have changed the buffer. */
16256 || !NILP (Vwindow_scroll_functions)
16257 || !just_this_one_p
16258 || MINI_WINDOW_P (w)
16259 || !(used_current_matrix_p
16260 = try_window_reusing_current_matrix (w)))
16261 try_window (window, startp, 0);
16262
16263 /* If new fonts have been loaded (due to fontsets), give up. We
16264 have to start a new redisplay since we need to re-adjust glyph
16265 matrices. */
16266 if (f->fonts_changed)
16267 goto need_larger_matrices;
16268
16269 /* If cursor did not appear assume that the middle of the window is
16270 in the first line of the window. Do it again with the next line.
16271 (Imagine a window of height 100, displaying two lines of height
16272 60. Moving back 50 from it->last_visible_y will end in the first
16273 line.) */
16274 if (w->cursor.vpos < 0)
16275 {
16276 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16277 {
16278 clear_glyph_matrix (w->desired_matrix);
16279 move_it_by_lines (&it, 1);
16280 try_window (window, it.current.pos, 0);
16281 }
16282 else if (PT < IT_CHARPOS (it))
16283 {
16284 clear_glyph_matrix (w->desired_matrix);
16285 move_it_by_lines (&it, -1);
16286 try_window (window, it.current.pos, 0);
16287 }
16288 else
16289 {
16290 /* Not much we can do about it. */
16291 }
16292 }
16293
16294 /* Consider the following case: Window starts at BEGV, there is
16295 invisible, intangible text at BEGV, so that display starts at
16296 some point START > BEGV. It can happen that we are called with
16297 PT somewhere between BEGV and START. Try to handle that case. */
16298 if (w->cursor.vpos < 0)
16299 {
16300 struct glyph_row *row = w->current_matrix->rows;
16301 if (row->mode_line_p)
16302 ++row;
16303 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16304 }
16305
16306 if (!cursor_row_fully_visible_p (w, 0, 0))
16307 {
16308 /* If vscroll is enabled, disable it and try again. */
16309 if (w->vscroll)
16310 {
16311 w->vscroll = 0;
16312 clear_glyph_matrix (w->desired_matrix);
16313 goto recenter;
16314 }
16315
16316 /* Users who set scroll-conservatively to a large number want
16317 point just above/below the scroll margin. If we ended up
16318 with point's row partially visible, move the window start to
16319 make that row fully visible and out of the margin. */
16320 if (scroll_conservatively > SCROLL_LIMIT)
16321 {
16322 int window_total_lines
16323 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16324 int margin =
16325 scroll_margin > 0
16326 ? min (scroll_margin, window_total_lines / 4)
16327 : 0;
16328 int move_down = w->cursor.vpos >= window_total_lines / 2;
16329
16330 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16331 clear_glyph_matrix (w->desired_matrix);
16332 if (1 == try_window (window, it.current.pos,
16333 TRY_WINDOW_CHECK_MARGINS))
16334 goto done;
16335 }
16336
16337 /* If centering point failed to make the whole line visible,
16338 put point at the top instead. That has to make the whole line
16339 visible, if it can be done. */
16340 if (centering_position == 0)
16341 goto done;
16342
16343 clear_glyph_matrix (w->desired_matrix);
16344 centering_position = 0;
16345 goto recenter;
16346 }
16347
16348 done:
16349
16350 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16351 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16352 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16353
16354 /* Display the mode line, if we must. */
16355 if ((update_mode_line
16356 /* If window not full width, must redo its mode line
16357 if (a) the window to its side is being redone and
16358 (b) we do a frame-based redisplay. This is a consequence
16359 of how inverted lines are drawn in frame-based redisplay. */
16360 || (!just_this_one_p
16361 && !FRAME_WINDOW_P (f)
16362 && !WINDOW_FULL_WIDTH_P (w))
16363 /* Line number to display. */
16364 || w->base_line_pos > 0
16365 /* Column number is displayed and different from the one displayed. */
16366 || (w->column_number_displayed != -1
16367 && (w->column_number_displayed != current_column ())))
16368 /* This means that the window has a mode line. */
16369 && (WINDOW_WANTS_MODELINE_P (w)
16370 || WINDOW_WANTS_HEADER_LINE_P (w)))
16371 {
16372
16373 display_mode_lines (w);
16374
16375 /* If mode line height has changed, arrange for a thorough
16376 immediate redisplay using the correct mode line height. */
16377 if (WINDOW_WANTS_MODELINE_P (w)
16378 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16379 {
16380 f->fonts_changed = 1;
16381 w->mode_line_height = -1;
16382 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16383 = DESIRED_MODE_LINE_HEIGHT (w);
16384 }
16385
16386 /* If header line height has changed, arrange for a thorough
16387 immediate redisplay using the correct header line height. */
16388 if (WINDOW_WANTS_HEADER_LINE_P (w)
16389 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16390 {
16391 f->fonts_changed = 1;
16392 w->header_line_height = -1;
16393 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16394 = DESIRED_HEADER_LINE_HEIGHT (w);
16395 }
16396
16397 if (f->fonts_changed)
16398 goto need_larger_matrices;
16399 }
16400
16401 if (!line_number_displayed && w->base_line_pos != -1)
16402 {
16403 w->base_line_pos = 0;
16404 w->base_line_number = 0;
16405 }
16406
16407 finish_menu_bars:
16408
16409 /* When we reach a frame's selected window, redo the frame's menu bar. */
16410 if (update_mode_line
16411 && EQ (FRAME_SELECTED_WINDOW (f), window))
16412 {
16413 int redisplay_menu_p = 0;
16414
16415 if (FRAME_WINDOW_P (f))
16416 {
16417 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16418 || defined (HAVE_NS) || defined (USE_GTK)
16419 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16420 #else
16421 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16422 #endif
16423 }
16424 else
16425 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16426
16427 if (redisplay_menu_p)
16428 display_menu_bar (w);
16429
16430 #ifdef HAVE_WINDOW_SYSTEM
16431 if (FRAME_WINDOW_P (f))
16432 {
16433 #if defined (USE_GTK) || defined (HAVE_NS)
16434 if (FRAME_EXTERNAL_TOOL_BAR (f))
16435 redisplay_tool_bar (f);
16436 #else
16437 if (WINDOWP (f->tool_bar_window)
16438 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16439 || !NILP (Vauto_resize_tool_bars))
16440 && redisplay_tool_bar (f))
16441 ignore_mouse_drag_p = 1;
16442 #endif
16443 }
16444 #endif
16445 }
16446
16447 #ifdef HAVE_WINDOW_SYSTEM
16448 if (FRAME_WINDOW_P (f)
16449 && update_window_fringes (w, (just_this_one_p
16450 || (!used_current_matrix_p && !overlay_arrow_seen)
16451 || w->pseudo_window_p)))
16452 {
16453 update_begin (f);
16454 block_input ();
16455 if (draw_window_fringes (w, 1))
16456 {
16457 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16458 x_draw_right_divider (w);
16459 else
16460 x_draw_vertical_border (w);
16461 }
16462 unblock_input ();
16463 update_end (f);
16464 }
16465
16466 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16467 x_draw_bottom_divider (w);
16468 #endif /* HAVE_WINDOW_SYSTEM */
16469
16470 /* We go to this label, with fonts_changed set, if it is
16471 necessary to try again using larger glyph matrices.
16472 We have to redeem the scroll bar even in this case,
16473 because the loop in redisplay_internal expects that. */
16474 need_larger_matrices:
16475 ;
16476 finish_scroll_bars:
16477
16478 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16479 {
16480 /* Set the thumb's position and size. */
16481 set_vertical_scroll_bar (w);
16482
16483 /* Note that we actually used the scroll bar attached to this
16484 window, so it shouldn't be deleted at the end of redisplay. */
16485 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16486 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16487 }
16488
16489 /* Restore current_buffer and value of point in it. The window
16490 update may have changed the buffer, so first make sure `opoint'
16491 is still valid (Bug#6177). */
16492 if (CHARPOS (opoint) < BEGV)
16493 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16494 else if (CHARPOS (opoint) > ZV)
16495 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16496 else
16497 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16498
16499 set_buffer_internal_1 (old);
16500 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16501 shorter. This can be caused by log truncation in *Messages*. */
16502 if (CHARPOS (lpoint) <= ZV)
16503 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16504
16505 unbind_to (count, Qnil);
16506 }
16507
16508
16509 /* Build the complete desired matrix of WINDOW with a window start
16510 buffer position POS.
16511
16512 Value is 1 if successful. It is zero if fonts were loaded during
16513 redisplay which makes re-adjusting glyph matrices necessary, and -1
16514 if point would appear in the scroll margins.
16515 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16516 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16517 set in FLAGS.) */
16518
16519 int
16520 try_window (Lisp_Object window, struct text_pos pos, int flags)
16521 {
16522 struct window *w = XWINDOW (window);
16523 struct it it;
16524 struct glyph_row *last_text_row = NULL;
16525 struct frame *f = XFRAME (w->frame);
16526 int frame_line_height = default_line_pixel_height (w);
16527
16528 /* Make POS the new window start. */
16529 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16530
16531 /* Mark cursor position as unknown. No overlay arrow seen. */
16532 w->cursor.vpos = -1;
16533 overlay_arrow_seen = 0;
16534
16535 /* Initialize iterator and info to start at POS. */
16536 start_display (&it, w, pos);
16537
16538 /* Display all lines of W. */
16539 while (it.current_y < it.last_visible_y)
16540 {
16541 if (display_line (&it))
16542 last_text_row = it.glyph_row - 1;
16543 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16544 return 0;
16545 }
16546
16547 /* Don't let the cursor end in the scroll margins. */
16548 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16549 && !MINI_WINDOW_P (w))
16550 {
16551 int this_scroll_margin;
16552 int window_total_lines
16553 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16554
16555 if (scroll_margin > 0)
16556 {
16557 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16558 this_scroll_margin *= frame_line_height;
16559 }
16560 else
16561 this_scroll_margin = 0;
16562
16563 if ((w->cursor.y >= 0 /* not vscrolled */
16564 && w->cursor.y < this_scroll_margin
16565 && CHARPOS (pos) > BEGV
16566 && IT_CHARPOS (it) < ZV)
16567 /* rms: considering make_cursor_line_fully_visible_p here
16568 seems to give wrong results. We don't want to recenter
16569 when the last line is partly visible, we want to allow
16570 that case to be handled in the usual way. */
16571 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16572 {
16573 w->cursor.vpos = -1;
16574 clear_glyph_matrix (w->desired_matrix);
16575 return -1;
16576 }
16577 }
16578
16579 /* If bottom moved off end of frame, change mode line percentage. */
16580 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16581 w->update_mode_line = 1;
16582
16583 /* Set window_end_pos to the offset of the last character displayed
16584 on the window from the end of current_buffer. Set
16585 window_end_vpos to its row number. */
16586 if (last_text_row)
16587 {
16588 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16589 adjust_window_ends (w, last_text_row, 0);
16590 eassert
16591 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16592 w->window_end_vpos)));
16593 }
16594 else
16595 {
16596 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16597 w->window_end_pos = Z - ZV;
16598 w->window_end_vpos = 0;
16599 }
16600
16601 /* But that is not valid info until redisplay finishes. */
16602 w->window_end_valid = 0;
16603 return 1;
16604 }
16605
16606
16607 \f
16608 /************************************************************************
16609 Window redisplay reusing current matrix when buffer has not changed
16610 ************************************************************************/
16611
16612 /* Try redisplay of window W showing an unchanged buffer with a
16613 different window start than the last time it was displayed by
16614 reusing its current matrix. Value is non-zero if successful.
16615 W->start is the new window start. */
16616
16617 static int
16618 try_window_reusing_current_matrix (struct window *w)
16619 {
16620 struct frame *f = XFRAME (w->frame);
16621 struct glyph_row *bottom_row;
16622 struct it it;
16623 struct run run;
16624 struct text_pos start, new_start;
16625 int nrows_scrolled, i;
16626 struct glyph_row *last_text_row;
16627 struct glyph_row *last_reused_text_row;
16628 struct glyph_row *start_row;
16629 int start_vpos, min_y, max_y;
16630
16631 #ifdef GLYPH_DEBUG
16632 if (inhibit_try_window_reusing)
16633 return 0;
16634 #endif
16635
16636 if (/* This function doesn't handle terminal frames. */
16637 !FRAME_WINDOW_P (f)
16638 /* Don't try to reuse the display if windows have been split
16639 or such. */
16640 || windows_or_buffers_changed
16641 || f->cursor_type_changed)
16642 return 0;
16643
16644 /* Can't do this if showing trailing whitespace. */
16645 if (!NILP (Vshow_trailing_whitespace))
16646 return 0;
16647
16648 /* If top-line visibility has changed, give up. */
16649 if (WINDOW_WANTS_HEADER_LINE_P (w)
16650 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16651 return 0;
16652
16653 /* Give up if old or new display is scrolled vertically. We could
16654 make this function handle this, but right now it doesn't. */
16655 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16656 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16657 return 0;
16658
16659 /* The variable new_start now holds the new window start. The old
16660 start `start' can be determined from the current matrix. */
16661 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16662 start = start_row->minpos;
16663 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16664
16665 /* Clear the desired matrix for the display below. */
16666 clear_glyph_matrix (w->desired_matrix);
16667
16668 if (CHARPOS (new_start) <= CHARPOS (start))
16669 {
16670 /* Don't use this method if the display starts with an ellipsis
16671 displayed for invisible text. It's not easy to handle that case
16672 below, and it's certainly not worth the effort since this is
16673 not a frequent case. */
16674 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16675 return 0;
16676
16677 IF_DEBUG (debug_method_add (w, "twu1"));
16678
16679 /* Display up to a row that can be reused. The variable
16680 last_text_row is set to the last row displayed that displays
16681 text. Note that it.vpos == 0 if or if not there is a
16682 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16683 start_display (&it, w, new_start);
16684 w->cursor.vpos = -1;
16685 last_text_row = last_reused_text_row = NULL;
16686
16687 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16688 {
16689 /* If we have reached into the characters in the START row,
16690 that means the line boundaries have changed. So we
16691 can't start copying with the row START. Maybe it will
16692 work to start copying with the following row. */
16693 while (IT_CHARPOS (it) > CHARPOS (start))
16694 {
16695 /* Advance to the next row as the "start". */
16696 start_row++;
16697 start = start_row->minpos;
16698 /* If there are no more rows to try, or just one, give up. */
16699 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16700 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16701 || CHARPOS (start) == ZV)
16702 {
16703 clear_glyph_matrix (w->desired_matrix);
16704 return 0;
16705 }
16706
16707 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16708 }
16709 /* If we have reached alignment, we can copy the rest of the
16710 rows. */
16711 if (IT_CHARPOS (it) == CHARPOS (start)
16712 /* Don't accept "alignment" inside a display vector,
16713 since start_row could have started in the middle of
16714 that same display vector (thus their character
16715 positions match), and we have no way of telling if
16716 that is the case. */
16717 && it.current.dpvec_index < 0)
16718 break;
16719
16720 if (display_line (&it))
16721 last_text_row = it.glyph_row - 1;
16722
16723 }
16724
16725 /* A value of current_y < last_visible_y means that we stopped
16726 at the previous window start, which in turn means that we
16727 have at least one reusable row. */
16728 if (it.current_y < it.last_visible_y)
16729 {
16730 struct glyph_row *row;
16731
16732 /* IT.vpos always starts from 0; it counts text lines. */
16733 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16734
16735 /* Find PT if not already found in the lines displayed. */
16736 if (w->cursor.vpos < 0)
16737 {
16738 int dy = it.current_y - start_row->y;
16739
16740 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16741 row = row_containing_pos (w, PT, row, NULL, dy);
16742 if (row)
16743 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16744 dy, nrows_scrolled);
16745 else
16746 {
16747 clear_glyph_matrix (w->desired_matrix);
16748 return 0;
16749 }
16750 }
16751
16752 /* Scroll the display. Do it before the current matrix is
16753 changed. The problem here is that update has not yet
16754 run, i.e. part of the current matrix is not up to date.
16755 scroll_run_hook will clear the cursor, and use the
16756 current matrix to get the height of the row the cursor is
16757 in. */
16758 run.current_y = start_row->y;
16759 run.desired_y = it.current_y;
16760 run.height = it.last_visible_y - it.current_y;
16761
16762 if (run.height > 0 && run.current_y != run.desired_y)
16763 {
16764 update_begin (f);
16765 FRAME_RIF (f)->update_window_begin_hook (w);
16766 FRAME_RIF (f)->clear_window_mouse_face (w);
16767 FRAME_RIF (f)->scroll_run_hook (w, &run);
16768 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16769 update_end (f);
16770 }
16771
16772 /* Shift current matrix down by nrows_scrolled lines. */
16773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16774 rotate_matrix (w->current_matrix,
16775 start_vpos,
16776 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16777 nrows_scrolled);
16778
16779 /* Disable lines that must be updated. */
16780 for (i = 0; i < nrows_scrolled; ++i)
16781 (start_row + i)->enabled_p = false;
16782
16783 /* Re-compute Y positions. */
16784 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16785 max_y = it.last_visible_y;
16786 for (row = start_row + nrows_scrolled;
16787 row < bottom_row;
16788 ++row)
16789 {
16790 row->y = it.current_y;
16791 row->visible_height = row->height;
16792
16793 if (row->y < min_y)
16794 row->visible_height -= min_y - row->y;
16795 if (row->y + row->height > max_y)
16796 row->visible_height -= row->y + row->height - max_y;
16797 if (row->fringe_bitmap_periodic_p)
16798 row->redraw_fringe_bitmaps_p = 1;
16799
16800 it.current_y += row->height;
16801
16802 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16803 last_reused_text_row = row;
16804 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16805 break;
16806 }
16807
16808 /* Disable lines in the current matrix which are now
16809 below the window. */
16810 for (++row; row < bottom_row; ++row)
16811 row->enabled_p = row->mode_line_p = 0;
16812 }
16813
16814 /* Update window_end_pos etc.; last_reused_text_row is the last
16815 reused row from the current matrix containing text, if any.
16816 The value of last_text_row is the last displayed line
16817 containing text. */
16818 if (last_reused_text_row)
16819 adjust_window_ends (w, last_reused_text_row, 1);
16820 else if (last_text_row)
16821 adjust_window_ends (w, last_text_row, 0);
16822 else
16823 {
16824 /* This window must be completely empty. */
16825 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16826 w->window_end_pos = Z - ZV;
16827 w->window_end_vpos = 0;
16828 }
16829 w->window_end_valid = 0;
16830
16831 /* Update hint: don't try scrolling again in update_window. */
16832 w->desired_matrix->no_scrolling_p = 1;
16833
16834 #ifdef GLYPH_DEBUG
16835 debug_method_add (w, "try_window_reusing_current_matrix 1");
16836 #endif
16837 return 1;
16838 }
16839 else if (CHARPOS (new_start) > CHARPOS (start))
16840 {
16841 struct glyph_row *pt_row, *row;
16842 struct glyph_row *first_reusable_row;
16843 struct glyph_row *first_row_to_display;
16844 int dy;
16845 int yb = window_text_bottom_y (w);
16846
16847 /* Find the row starting at new_start, if there is one. Don't
16848 reuse a partially visible line at the end. */
16849 first_reusable_row = start_row;
16850 while (first_reusable_row->enabled_p
16851 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16852 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16853 < CHARPOS (new_start)))
16854 ++first_reusable_row;
16855
16856 /* Give up if there is no row to reuse. */
16857 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16858 || !first_reusable_row->enabled_p
16859 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16860 != CHARPOS (new_start)))
16861 return 0;
16862
16863 /* We can reuse fully visible rows beginning with
16864 first_reusable_row to the end of the window. Set
16865 first_row_to_display to the first row that cannot be reused.
16866 Set pt_row to the row containing point, if there is any. */
16867 pt_row = NULL;
16868 for (first_row_to_display = first_reusable_row;
16869 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16870 ++first_row_to_display)
16871 {
16872 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16873 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16874 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16875 && first_row_to_display->ends_at_zv_p
16876 && pt_row == NULL)))
16877 pt_row = first_row_to_display;
16878 }
16879
16880 /* Start displaying at the start of first_row_to_display. */
16881 eassert (first_row_to_display->y < yb);
16882 init_to_row_start (&it, w, first_row_to_display);
16883
16884 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16885 - start_vpos);
16886 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16887 - nrows_scrolled);
16888 it.current_y = (first_row_to_display->y - first_reusable_row->y
16889 + WINDOW_HEADER_LINE_HEIGHT (w));
16890
16891 /* Display lines beginning with first_row_to_display in the
16892 desired matrix. Set last_text_row to the last row displayed
16893 that displays text. */
16894 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16895 if (pt_row == NULL)
16896 w->cursor.vpos = -1;
16897 last_text_row = NULL;
16898 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16899 if (display_line (&it))
16900 last_text_row = it.glyph_row - 1;
16901
16902 /* If point is in a reused row, adjust y and vpos of the cursor
16903 position. */
16904 if (pt_row)
16905 {
16906 w->cursor.vpos -= nrows_scrolled;
16907 w->cursor.y -= first_reusable_row->y - start_row->y;
16908 }
16909
16910 /* Give up if point isn't in a row displayed or reused. (This
16911 also handles the case where w->cursor.vpos < nrows_scrolled
16912 after the calls to display_line, which can happen with scroll
16913 margins. See bug#1295.) */
16914 if (w->cursor.vpos < 0)
16915 {
16916 clear_glyph_matrix (w->desired_matrix);
16917 return 0;
16918 }
16919
16920 /* Scroll the display. */
16921 run.current_y = first_reusable_row->y;
16922 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16923 run.height = it.last_visible_y - run.current_y;
16924 dy = run.current_y - run.desired_y;
16925
16926 if (run.height)
16927 {
16928 update_begin (f);
16929 FRAME_RIF (f)->update_window_begin_hook (w);
16930 FRAME_RIF (f)->clear_window_mouse_face (w);
16931 FRAME_RIF (f)->scroll_run_hook (w, &run);
16932 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16933 update_end (f);
16934 }
16935
16936 /* Adjust Y positions of reused rows. */
16937 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16938 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16939 max_y = it.last_visible_y;
16940 for (row = first_reusable_row; row < first_row_to_display; ++row)
16941 {
16942 row->y -= dy;
16943 row->visible_height = row->height;
16944 if (row->y < min_y)
16945 row->visible_height -= min_y - row->y;
16946 if (row->y + row->height > max_y)
16947 row->visible_height -= row->y + row->height - max_y;
16948 if (row->fringe_bitmap_periodic_p)
16949 row->redraw_fringe_bitmaps_p = 1;
16950 }
16951
16952 /* Scroll the current matrix. */
16953 eassert (nrows_scrolled > 0);
16954 rotate_matrix (w->current_matrix,
16955 start_vpos,
16956 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16957 -nrows_scrolled);
16958
16959 /* Disable rows not reused. */
16960 for (row -= nrows_scrolled; row < bottom_row; ++row)
16961 row->enabled_p = false;
16962
16963 /* Point may have moved to a different line, so we cannot assume that
16964 the previous cursor position is valid; locate the correct row. */
16965 if (pt_row)
16966 {
16967 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16968 row < bottom_row
16969 && PT >= MATRIX_ROW_END_CHARPOS (row)
16970 && !row->ends_at_zv_p;
16971 row++)
16972 {
16973 w->cursor.vpos++;
16974 w->cursor.y = row->y;
16975 }
16976 if (row < bottom_row)
16977 {
16978 /* Can't simply scan the row for point with
16979 bidi-reordered glyph rows. Let set_cursor_from_row
16980 figure out where to put the cursor, and if it fails,
16981 give up. */
16982 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16983 {
16984 if (!set_cursor_from_row (w, row, w->current_matrix,
16985 0, 0, 0, 0))
16986 {
16987 clear_glyph_matrix (w->desired_matrix);
16988 return 0;
16989 }
16990 }
16991 else
16992 {
16993 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16994 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16995
16996 for (; glyph < end
16997 && (!BUFFERP (glyph->object)
16998 || glyph->charpos < PT);
16999 glyph++)
17000 {
17001 w->cursor.hpos++;
17002 w->cursor.x += glyph->pixel_width;
17003 }
17004 }
17005 }
17006 }
17007
17008 /* Adjust window end. A null value of last_text_row means that
17009 the window end is in reused rows which in turn means that
17010 only its vpos can have changed. */
17011 if (last_text_row)
17012 adjust_window_ends (w, last_text_row, 0);
17013 else
17014 w->window_end_vpos -= nrows_scrolled;
17015
17016 w->window_end_valid = 0;
17017 w->desired_matrix->no_scrolling_p = 1;
17018
17019 #ifdef GLYPH_DEBUG
17020 debug_method_add (w, "try_window_reusing_current_matrix 2");
17021 #endif
17022 return 1;
17023 }
17024
17025 return 0;
17026 }
17027
17028
17029 \f
17030 /************************************************************************
17031 Window redisplay reusing current matrix when buffer has changed
17032 ************************************************************************/
17033
17034 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17035 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17036 ptrdiff_t *, ptrdiff_t *);
17037 static struct glyph_row *
17038 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17039 struct glyph_row *);
17040
17041
17042 /* Return the last row in MATRIX displaying text. If row START is
17043 non-null, start searching with that row. IT gives the dimensions
17044 of the display. Value is null if matrix is empty; otherwise it is
17045 a pointer to the row found. */
17046
17047 static struct glyph_row *
17048 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17049 struct glyph_row *start)
17050 {
17051 struct glyph_row *row, *row_found;
17052
17053 /* Set row_found to the last row in IT->w's current matrix
17054 displaying text. The loop looks funny but think of partially
17055 visible lines. */
17056 row_found = NULL;
17057 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17058 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17059 {
17060 eassert (row->enabled_p);
17061 row_found = row;
17062 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17063 break;
17064 ++row;
17065 }
17066
17067 return row_found;
17068 }
17069
17070
17071 /* Return the last row in the current matrix of W that is not affected
17072 by changes at the start of current_buffer that occurred since W's
17073 current matrix was built. Value is null if no such row exists.
17074
17075 BEG_UNCHANGED us the number of characters unchanged at the start of
17076 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17077 first changed character in current_buffer. Characters at positions <
17078 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17079 when the current matrix was built. */
17080
17081 static struct glyph_row *
17082 find_last_unchanged_at_beg_row (struct window *w)
17083 {
17084 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17085 struct glyph_row *row;
17086 struct glyph_row *row_found = NULL;
17087 int yb = window_text_bottom_y (w);
17088
17089 /* Find the last row displaying unchanged text. */
17090 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17091 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17092 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17093 ++row)
17094 {
17095 if (/* If row ends before first_changed_pos, it is unchanged,
17096 except in some case. */
17097 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17098 /* When row ends in ZV and we write at ZV it is not
17099 unchanged. */
17100 && !row->ends_at_zv_p
17101 /* When first_changed_pos is the end of a continued line,
17102 row is not unchanged because it may be no longer
17103 continued. */
17104 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17105 && (row->continued_p
17106 || row->exact_window_width_line_p))
17107 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17108 needs to be recomputed, so don't consider this row as
17109 unchanged. This happens when the last line was
17110 bidi-reordered and was killed immediately before this
17111 redisplay cycle. In that case, ROW->end stores the
17112 buffer position of the first visual-order character of
17113 the killed text, which is now beyond ZV. */
17114 && CHARPOS (row->end.pos) <= ZV)
17115 row_found = row;
17116
17117 /* Stop if last visible row. */
17118 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17119 break;
17120 }
17121
17122 return row_found;
17123 }
17124
17125
17126 /* Find the first glyph row in the current matrix of W that is not
17127 affected by changes at the end of current_buffer since the
17128 time W's current matrix was built.
17129
17130 Return in *DELTA the number of chars by which buffer positions in
17131 unchanged text at the end of current_buffer must be adjusted.
17132
17133 Return in *DELTA_BYTES the corresponding number of bytes.
17134
17135 Value is null if no such row exists, i.e. all rows are affected by
17136 changes. */
17137
17138 static struct glyph_row *
17139 find_first_unchanged_at_end_row (struct window *w,
17140 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17141 {
17142 struct glyph_row *row;
17143 struct glyph_row *row_found = NULL;
17144
17145 *delta = *delta_bytes = 0;
17146
17147 /* Display must not have been paused, otherwise the current matrix
17148 is not up to date. */
17149 eassert (w->window_end_valid);
17150
17151 /* A value of window_end_pos >= END_UNCHANGED means that the window
17152 end is in the range of changed text. If so, there is no
17153 unchanged row at the end of W's current matrix. */
17154 if (w->window_end_pos >= END_UNCHANGED)
17155 return NULL;
17156
17157 /* Set row to the last row in W's current matrix displaying text. */
17158 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17159
17160 /* If matrix is entirely empty, no unchanged row exists. */
17161 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17162 {
17163 /* The value of row is the last glyph row in the matrix having a
17164 meaningful buffer position in it. The end position of row
17165 corresponds to window_end_pos. This allows us to translate
17166 buffer positions in the current matrix to current buffer
17167 positions for characters not in changed text. */
17168 ptrdiff_t Z_old =
17169 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17170 ptrdiff_t Z_BYTE_old =
17171 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17172 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17173 struct glyph_row *first_text_row
17174 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17175
17176 *delta = Z - Z_old;
17177 *delta_bytes = Z_BYTE - Z_BYTE_old;
17178
17179 /* Set last_unchanged_pos to the buffer position of the last
17180 character in the buffer that has not been changed. Z is the
17181 index + 1 of the last character in current_buffer, i.e. by
17182 subtracting END_UNCHANGED we get the index of the last
17183 unchanged character, and we have to add BEG to get its buffer
17184 position. */
17185 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17186 last_unchanged_pos_old = last_unchanged_pos - *delta;
17187
17188 /* Search backward from ROW for a row displaying a line that
17189 starts at a minimum position >= last_unchanged_pos_old. */
17190 for (; row > first_text_row; --row)
17191 {
17192 /* This used to abort, but it can happen.
17193 It is ok to just stop the search instead here. KFS. */
17194 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17195 break;
17196
17197 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17198 row_found = row;
17199 }
17200 }
17201
17202 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17203
17204 return row_found;
17205 }
17206
17207
17208 /* Make sure that glyph rows in the current matrix of window W
17209 reference the same glyph memory as corresponding rows in the
17210 frame's frame matrix. This function is called after scrolling W's
17211 current matrix on a terminal frame in try_window_id and
17212 try_window_reusing_current_matrix. */
17213
17214 static void
17215 sync_frame_with_window_matrix_rows (struct window *w)
17216 {
17217 struct frame *f = XFRAME (w->frame);
17218 struct glyph_row *window_row, *window_row_end, *frame_row;
17219
17220 /* Preconditions: W must be a leaf window and full-width. Its frame
17221 must have a frame matrix. */
17222 eassert (BUFFERP (w->contents));
17223 eassert (WINDOW_FULL_WIDTH_P (w));
17224 eassert (!FRAME_WINDOW_P (f));
17225
17226 /* If W is a full-width window, glyph pointers in W's current matrix
17227 have, by definition, to be the same as glyph pointers in the
17228 corresponding frame matrix. Note that frame matrices have no
17229 marginal areas (see build_frame_matrix). */
17230 window_row = w->current_matrix->rows;
17231 window_row_end = window_row + w->current_matrix->nrows;
17232 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17233 while (window_row < window_row_end)
17234 {
17235 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17236 struct glyph *end = window_row->glyphs[LAST_AREA];
17237
17238 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17239 frame_row->glyphs[TEXT_AREA] = start;
17240 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17241 frame_row->glyphs[LAST_AREA] = end;
17242
17243 /* Disable frame rows whose corresponding window rows have
17244 been disabled in try_window_id. */
17245 if (!window_row->enabled_p)
17246 frame_row->enabled_p = false;
17247
17248 ++window_row, ++frame_row;
17249 }
17250 }
17251
17252
17253 /* Find the glyph row in window W containing CHARPOS. Consider all
17254 rows between START and END (not inclusive). END null means search
17255 all rows to the end of the display area of W. Value is the row
17256 containing CHARPOS or null. */
17257
17258 struct glyph_row *
17259 row_containing_pos (struct window *w, ptrdiff_t charpos,
17260 struct glyph_row *start, struct glyph_row *end, int dy)
17261 {
17262 struct glyph_row *row = start;
17263 struct glyph_row *best_row = NULL;
17264 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17265 int last_y;
17266
17267 /* If we happen to start on a header-line, skip that. */
17268 if (row->mode_line_p)
17269 ++row;
17270
17271 if ((end && row >= end) || !row->enabled_p)
17272 return NULL;
17273
17274 last_y = window_text_bottom_y (w) - dy;
17275
17276 while (1)
17277 {
17278 /* Give up if we have gone too far. */
17279 if (end && row >= end)
17280 return NULL;
17281 /* This formerly returned if they were equal.
17282 I think that both quantities are of a "last plus one" type;
17283 if so, when they are equal, the row is within the screen. -- rms. */
17284 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17285 return NULL;
17286
17287 /* If it is in this row, return this row. */
17288 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17289 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17290 /* The end position of a row equals the start
17291 position of the next row. If CHARPOS is there, we
17292 would rather consider it displayed in the next
17293 line, except when this line ends in ZV. */
17294 && !row_for_charpos_p (row, charpos)))
17295 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17296 {
17297 struct glyph *g;
17298
17299 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17300 || (!best_row && !row->continued_p))
17301 return row;
17302 /* In bidi-reordered rows, there could be several rows whose
17303 edges surround CHARPOS, all of these rows belonging to
17304 the same continued line. We need to find the row which
17305 fits CHARPOS the best. */
17306 for (g = row->glyphs[TEXT_AREA];
17307 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17308 g++)
17309 {
17310 if (!STRINGP (g->object))
17311 {
17312 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17313 {
17314 mindif = eabs (g->charpos - charpos);
17315 best_row = row;
17316 /* Exact match always wins. */
17317 if (mindif == 0)
17318 return best_row;
17319 }
17320 }
17321 }
17322 }
17323 else if (best_row && !row->continued_p)
17324 return best_row;
17325 ++row;
17326 }
17327 }
17328
17329
17330 /* Try to redisplay window W by reusing its existing display. W's
17331 current matrix must be up to date when this function is called,
17332 i.e. window_end_valid must be nonzero.
17333
17334 Value is
17335
17336 >= 1 if successful, i.e. display has been updated
17337 specifically:
17338 1 means the changes were in front of a newline that precedes
17339 the window start, and the whole current matrix was reused
17340 2 means the changes were after the last position displayed
17341 in the window, and the whole current matrix was reused
17342 3 means portions of the current matrix were reused, while
17343 some of the screen lines were redrawn
17344 -1 if redisplay with same window start is known not to succeed
17345 0 if otherwise unsuccessful
17346
17347 The following steps are performed:
17348
17349 1. Find the last row in the current matrix of W that is not
17350 affected by changes at the start of current_buffer. If no such row
17351 is found, give up.
17352
17353 2. Find the first row in W's current matrix that is not affected by
17354 changes at the end of current_buffer. Maybe there is no such row.
17355
17356 3. Display lines beginning with the row + 1 found in step 1 to the
17357 row found in step 2 or, if step 2 didn't find a row, to the end of
17358 the window.
17359
17360 4. If cursor is not known to appear on the window, give up.
17361
17362 5. If display stopped at the row found in step 2, scroll the
17363 display and current matrix as needed.
17364
17365 6. Maybe display some lines at the end of W, if we must. This can
17366 happen under various circumstances, like a partially visible line
17367 becoming fully visible, or because newly displayed lines are displayed
17368 in smaller font sizes.
17369
17370 7. Update W's window end information. */
17371
17372 static int
17373 try_window_id (struct window *w)
17374 {
17375 struct frame *f = XFRAME (w->frame);
17376 struct glyph_matrix *current_matrix = w->current_matrix;
17377 struct glyph_matrix *desired_matrix = w->desired_matrix;
17378 struct glyph_row *last_unchanged_at_beg_row;
17379 struct glyph_row *first_unchanged_at_end_row;
17380 struct glyph_row *row;
17381 struct glyph_row *bottom_row;
17382 int bottom_vpos;
17383 struct it it;
17384 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17385 int dvpos, dy;
17386 struct text_pos start_pos;
17387 struct run run;
17388 int first_unchanged_at_end_vpos = 0;
17389 struct glyph_row *last_text_row, *last_text_row_at_end;
17390 struct text_pos start;
17391 ptrdiff_t first_changed_charpos, last_changed_charpos;
17392
17393 #ifdef GLYPH_DEBUG
17394 if (inhibit_try_window_id)
17395 return 0;
17396 #endif
17397
17398 /* This is handy for debugging. */
17399 #if 0
17400 #define GIVE_UP(X) \
17401 do { \
17402 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17403 return 0; \
17404 } while (0)
17405 #else
17406 #define GIVE_UP(X) return 0
17407 #endif
17408
17409 SET_TEXT_POS_FROM_MARKER (start, w->start);
17410
17411 /* Don't use this for mini-windows because these can show
17412 messages and mini-buffers, and we don't handle that here. */
17413 if (MINI_WINDOW_P (w))
17414 GIVE_UP (1);
17415
17416 /* This flag is used to prevent redisplay optimizations. */
17417 if (windows_or_buffers_changed || f->cursor_type_changed)
17418 GIVE_UP (2);
17419
17420 /* This function's optimizations cannot be used if overlays have
17421 changed in the buffer displayed by the window, so give up if they
17422 have. */
17423 if (w->last_overlay_modified != OVERLAY_MODIFF)
17424 GIVE_UP (21);
17425
17426 /* Verify that narrowing has not changed.
17427 Also verify that we were not told to prevent redisplay optimizations.
17428 It would be nice to further
17429 reduce the number of cases where this prevents try_window_id. */
17430 if (current_buffer->clip_changed
17431 || current_buffer->prevent_redisplay_optimizations_p)
17432 GIVE_UP (3);
17433
17434 /* Window must either use window-based redisplay or be full width. */
17435 if (!FRAME_WINDOW_P (f)
17436 && (!FRAME_LINE_INS_DEL_OK (f)
17437 || !WINDOW_FULL_WIDTH_P (w)))
17438 GIVE_UP (4);
17439
17440 /* Give up if point is known NOT to appear in W. */
17441 if (PT < CHARPOS (start))
17442 GIVE_UP (5);
17443
17444 /* Another way to prevent redisplay optimizations. */
17445 if (w->last_modified == 0)
17446 GIVE_UP (6);
17447
17448 /* Verify that window is not hscrolled. */
17449 if (w->hscroll != 0)
17450 GIVE_UP (7);
17451
17452 /* Verify that display wasn't paused. */
17453 if (!w->window_end_valid)
17454 GIVE_UP (8);
17455
17456 /* Likewise if highlighting trailing whitespace. */
17457 if (!NILP (Vshow_trailing_whitespace))
17458 GIVE_UP (11);
17459
17460 /* Can't use this if overlay arrow position and/or string have
17461 changed. */
17462 if (overlay_arrows_changed_p ())
17463 GIVE_UP (12);
17464
17465 /* When word-wrap is on, adding a space to the first word of a
17466 wrapped line can change the wrap position, altering the line
17467 above it. It might be worthwhile to handle this more
17468 intelligently, but for now just redisplay from scratch. */
17469 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17470 GIVE_UP (21);
17471
17472 /* Under bidi reordering, adding or deleting a character in the
17473 beginning of a paragraph, before the first strong directional
17474 character, can change the base direction of the paragraph (unless
17475 the buffer specifies a fixed paragraph direction), which will
17476 require to redisplay the whole paragraph. It might be worthwhile
17477 to find the paragraph limits and widen the range of redisplayed
17478 lines to that, but for now just give up this optimization and
17479 redisplay from scratch. */
17480 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17481 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17482 GIVE_UP (22);
17483
17484 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17485 only if buffer has really changed. The reason is that the gap is
17486 initially at Z for freshly visited files. The code below would
17487 set end_unchanged to 0 in that case. */
17488 if (MODIFF > SAVE_MODIFF
17489 /* This seems to happen sometimes after saving a buffer. */
17490 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17491 {
17492 if (GPT - BEG < BEG_UNCHANGED)
17493 BEG_UNCHANGED = GPT - BEG;
17494 if (Z - GPT < END_UNCHANGED)
17495 END_UNCHANGED = Z - GPT;
17496 }
17497
17498 /* The position of the first and last character that has been changed. */
17499 first_changed_charpos = BEG + BEG_UNCHANGED;
17500 last_changed_charpos = Z - END_UNCHANGED;
17501
17502 /* If window starts after a line end, and the last change is in
17503 front of that newline, then changes don't affect the display.
17504 This case happens with stealth-fontification. Note that although
17505 the display is unchanged, glyph positions in the matrix have to
17506 be adjusted, of course. */
17507 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17508 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17509 && ((last_changed_charpos < CHARPOS (start)
17510 && CHARPOS (start) == BEGV)
17511 || (last_changed_charpos < CHARPOS (start) - 1
17512 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17513 {
17514 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17515 struct glyph_row *r0;
17516
17517 /* Compute how many chars/bytes have been added to or removed
17518 from the buffer. */
17519 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17520 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17521 Z_delta = Z - Z_old;
17522 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17523
17524 /* Give up if PT is not in the window. Note that it already has
17525 been checked at the start of try_window_id that PT is not in
17526 front of the window start. */
17527 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17528 GIVE_UP (13);
17529
17530 /* If window start is unchanged, we can reuse the whole matrix
17531 as is, after adjusting glyph positions. No need to compute
17532 the window end again, since its offset from Z hasn't changed. */
17533 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17534 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17535 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17536 /* PT must not be in a partially visible line. */
17537 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17538 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17539 {
17540 /* Adjust positions in the glyph matrix. */
17541 if (Z_delta || Z_delta_bytes)
17542 {
17543 struct glyph_row *r1
17544 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17545 increment_matrix_positions (w->current_matrix,
17546 MATRIX_ROW_VPOS (r0, current_matrix),
17547 MATRIX_ROW_VPOS (r1, current_matrix),
17548 Z_delta, Z_delta_bytes);
17549 }
17550
17551 /* Set the cursor. */
17552 row = row_containing_pos (w, PT, r0, NULL, 0);
17553 if (row)
17554 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17555 return 1;
17556 }
17557 }
17558
17559 /* Handle the case that changes are all below what is displayed in
17560 the window, and that PT is in the window. This shortcut cannot
17561 be taken if ZV is visible in the window, and text has been added
17562 there that is visible in the window. */
17563 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17564 /* ZV is not visible in the window, or there are no
17565 changes at ZV, actually. */
17566 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17567 || first_changed_charpos == last_changed_charpos))
17568 {
17569 struct glyph_row *r0;
17570
17571 /* Give up if PT is not in the window. Note that it already has
17572 been checked at the start of try_window_id that PT is not in
17573 front of the window start. */
17574 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17575 GIVE_UP (14);
17576
17577 /* If window start is unchanged, we can reuse the whole matrix
17578 as is, without changing glyph positions since no text has
17579 been added/removed in front of the window end. */
17580 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17581 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17582 /* PT must not be in a partially visible line. */
17583 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17584 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17585 {
17586 /* We have to compute the window end anew since text
17587 could have been added/removed after it. */
17588 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17589 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17590
17591 /* Set the cursor. */
17592 row = row_containing_pos (w, PT, r0, NULL, 0);
17593 if (row)
17594 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17595 return 2;
17596 }
17597 }
17598
17599 /* Give up if window start is in the changed area.
17600
17601 The condition used to read
17602
17603 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17604
17605 but why that was tested escapes me at the moment. */
17606 if (CHARPOS (start) >= first_changed_charpos
17607 && CHARPOS (start) <= last_changed_charpos)
17608 GIVE_UP (15);
17609
17610 /* Check that window start agrees with the start of the first glyph
17611 row in its current matrix. Check this after we know the window
17612 start is not in changed text, otherwise positions would not be
17613 comparable. */
17614 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17615 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17616 GIVE_UP (16);
17617
17618 /* Give up if the window ends in strings. Overlay strings
17619 at the end are difficult to handle, so don't try. */
17620 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17621 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17622 GIVE_UP (20);
17623
17624 /* Compute the position at which we have to start displaying new
17625 lines. Some of the lines at the top of the window might be
17626 reusable because they are not displaying changed text. Find the
17627 last row in W's current matrix not affected by changes at the
17628 start of current_buffer. Value is null if changes start in the
17629 first line of window. */
17630 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17631 if (last_unchanged_at_beg_row)
17632 {
17633 /* Avoid starting to display in the middle of a character, a TAB
17634 for instance. This is easier than to set up the iterator
17635 exactly, and it's not a frequent case, so the additional
17636 effort wouldn't really pay off. */
17637 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17638 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17639 && last_unchanged_at_beg_row > w->current_matrix->rows)
17640 --last_unchanged_at_beg_row;
17641
17642 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17643 GIVE_UP (17);
17644
17645 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17646 GIVE_UP (18);
17647 start_pos = it.current.pos;
17648
17649 /* Start displaying new lines in the desired matrix at the same
17650 vpos we would use in the current matrix, i.e. below
17651 last_unchanged_at_beg_row. */
17652 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17653 current_matrix);
17654 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17655 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17656
17657 eassert (it.hpos == 0 && it.current_x == 0);
17658 }
17659 else
17660 {
17661 /* There are no reusable lines at the start of the window.
17662 Start displaying in the first text line. */
17663 start_display (&it, w, start);
17664 it.vpos = it.first_vpos;
17665 start_pos = it.current.pos;
17666 }
17667
17668 /* Find the first row that is not affected by changes at the end of
17669 the buffer. Value will be null if there is no unchanged row, in
17670 which case we must redisplay to the end of the window. delta
17671 will be set to the value by which buffer positions beginning with
17672 first_unchanged_at_end_row have to be adjusted due to text
17673 changes. */
17674 first_unchanged_at_end_row
17675 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17676 IF_DEBUG (debug_delta = delta);
17677 IF_DEBUG (debug_delta_bytes = delta_bytes);
17678
17679 /* Set stop_pos to the buffer position up to which we will have to
17680 display new lines. If first_unchanged_at_end_row != NULL, this
17681 is the buffer position of the start of the line displayed in that
17682 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17683 that we don't stop at a buffer position. */
17684 stop_pos = 0;
17685 if (first_unchanged_at_end_row)
17686 {
17687 eassert (last_unchanged_at_beg_row == NULL
17688 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17689
17690 /* If this is a continuation line, move forward to the next one
17691 that isn't. Changes in lines above affect this line.
17692 Caution: this may move first_unchanged_at_end_row to a row
17693 not displaying text. */
17694 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17695 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17696 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17697 < it.last_visible_y))
17698 ++first_unchanged_at_end_row;
17699
17700 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17701 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17702 >= it.last_visible_y))
17703 first_unchanged_at_end_row = NULL;
17704 else
17705 {
17706 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17707 + delta);
17708 first_unchanged_at_end_vpos
17709 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17710 eassert (stop_pos >= Z - END_UNCHANGED);
17711 }
17712 }
17713 else if (last_unchanged_at_beg_row == NULL)
17714 GIVE_UP (19);
17715
17716
17717 #ifdef GLYPH_DEBUG
17718
17719 /* Either there is no unchanged row at the end, or the one we have
17720 now displays text. This is a necessary condition for the window
17721 end pos calculation at the end of this function. */
17722 eassert (first_unchanged_at_end_row == NULL
17723 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17724
17725 debug_last_unchanged_at_beg_vpos
17726 = (last_unchanged_at_beg_row
17727 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17728 : -1);
17729 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17730
17731 #endif /* GLYPH_DEBUG */
17732
17733
17734 /* Display new lines. Set last_text_row to the last new line
17735 displayed which has text on it, i.e. might end up as being the
17736 line where the window_end_vpos is. */
17737 w->cursor.vpos = -1;
17738 last_text_row = NULL;
17739 overlay_arrow_seen = 0;
17740 while (it.current_y < it.last_visible_y
17741 && !f->fonts_changed
17742 && (first_unchanged_at_end_row == NULL
17743 || IT_CHARPOS (it) < stop_pos))
17744 {
17745 if (display_line (&it))
17746 last_text_row = it.glyph_row - 1;
17747 }
17748
17749 if (f->fonts_changed)
17750 return -1;
17751
17752
17753 /* Compute differences in buffer positions, y-positions etc. for
17754 lines reused at the bottom of the window. Compute what we can
17755 scroll. */
17756 if (first_unchanged_at_end_row
17757 /* No lines reused because we displayed everything up to the
17758 bottom of the window. */
17759 && it.current_y < it.last_visible_y)
17760 {
17761 dvpos = (it.vpos
17762 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17763 current_matrix));
17764 dy = it.current_y - first_unchanged_at_end_row->y;
17765 run.current_y = first_unchanged_at_end_row->y;
17766 run.desired_y = run.current_y + dy;
17767 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17768 }
17769 else
17770 {
17771 delta = delta_bytes = dvpos = dy
17772 = run.current_y = run.desired_y = run.height = 0;
17773 first_unchanged_at_end_row = NULL;
17774 }
17775 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17776
17777
17778 /* Find the cursor if not already found. We have to decide whether
17779 PT will appear on this window (it sometimes doesn't, but this is
17780 not a very frequent case.) This decision has to be made before
17781 the current matrix is altered. A value of cursor.vpos < 0 means
17782 that PT is either in one of the lines beginning at
17783 first_unchanged_at_end_row or below the window. Don't care for
17784 lines that might be displayed later at the window end; as
17785 mentioned, this is not a frequent case. */
17786 if (w->cursor.vpos < 0)
17787 {
17788 /* Cursor in unchanged rows at the top? */
17789 if (PT < CHARPOS (start_pos)
17790 && last_unchanged_at_beg_row)
17791 {
17792 row = row_containing_pos (w, PT,
17793 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17794 last_unchanged_at_beg_row + 1, 0);
17795 if (row)
17796 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17797 }
17798
17799 /* Start from first_unchanged_at_end_row looking for PT. */
17800 else if (first_unchanged_at_end_row)
17801 {
17802 row = row_containing_pos (w, PT - delta,
17803 first_unchanged_at_end_row, NULL, 0);
17804 if (row)
17805 set_cursor_from_row (w, row, w->current_matrix, delta,
17806 delta_bytes, dy, dvpos);
17807 }
17808
17809 /* Give up if cursor was not found. */
17810 if (w->cursor.vpos < 0)
17811 {
17812 clear_glyph_matrix (w->desired_matrix);
17813 return -1;
17814 }
17815 }
17816
17817 /* Don't let the cursor end in the scroll margins. */
17818 {
17819 int this_scroll_margin, cursor_height;
17820 int frame_line_height = default_line_pixel_height (w);
17821 int window_total_lines
17822 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17823
17824 this_scroll_margin =
17825 max (0, min (scroll_margin, window_total_lines / 4));
17826 this_scroll_margin *= frame_line_height;
17827 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17828
17829 if ((w->cursor.y < this_scroll_margin
17830 && CHARPOS (start) > BEGV)
17831 /* Old redisplay didn't take scroll margin into account at the bottom,
17832 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17833 || (w->cursor.y + (make_cursor_line_fully_visible_p
17834 ? cursor_height + this_scroll_margin
17835 : 1)) > it.last_visible_y)
17836 {
17837 w->cursor.vpos = -1;
17838 clear_glyph_matrix (w->desired_matrix);
17839 return -1;
17840 }
17841 }
17842
17843 /* Scroll the display. Do it before changing the current matrix so
17844 that xterm.c doesn't get confused about where the cursor glyph is
17845 found. */
17846 if (dy && run.height)
17847 {
17848 update_begin (f);
17849
17850 if (FRAME_WINDOW_P (f))
17851 {
17852 FRAME_RIF (f)->update_window_begin_hook (w);
17853 FRAME_RIF (f)->clear_window_mouse_face (w);
17854 FRAME_RIF (f)->scroll_run_hook (w, &run);
17855 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17856 }
17857 else
17858 {
17859 /* Terminal frame. In this case, dvpos gives the number of
17860 lines to scroll by; dvpos < 0 means scroll up. */
17861 int from_vpos
17862 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17863 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17864 int end = (WINDOW_TOP_EDGE_LINE (w)
17865 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17866 + window_internal_height (w));
17867
17868 #if defined (HAVE_GPM) || defined (MSDOS)
17869 x_clear_window_mouse_face (w);
17870 #endif
17871 /* Perform the operation on the screen. */
17872 if (dvpos > 0)
17873 {
17874 /* Scroll last_unchanged_at_beg_row to the end of the
17875 window down dvpos lines. */
17876 set_terminal_window (f, end);
17877
17878 /* On dumb terminals delete dvpos lines at the end
17879 before inserting dvpos empty lines. */
17880 if (!FRAME_SCROLL_REGION_OK (f))
17881 ins_del_lines (f, end - dvpos, -dvpos);
17882
17883 /* Insert dvpos empty lines in front of
17884 last_unchanged_at_beg_row. */
17885 ins_del_lines (f, from, dvpos);
17886 }
17887 else if (dvpos < 0)
17888 {
17889 /* Scroll up last_unchanged_at_beg_vpos to the end of
17890 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17891 set_terminal_window (f, end);
17892
17893 /* Delete dvpos lines in front of
17894 last_unchanged_at_beg_vpos. ins_del_lines will set
17895 the cursor to the given vpos and emit |dvpos| delete
17896 line sequences. */
17897 ins_del_lines (f, from + dvpos, dvpos);
17898
17899 /* On a dumb terminal insert dvpos empty lines at the
17900 end. */
17901 if (!FRAME_SCROLL_REGION_OK (f))
17902 ins_del_lines (f, end + dvpos, -dvpos);
17903 }
17904
17905 set_terminal_window (f, 0);
17906 }
17907
17908 update_end (f);
17909 }
17910
17911 /* Shift reused rows of the current matrix to the right position.
17912 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17913 text. */
17914 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17915 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17916 if (dvpos < 0)
17917 {
17918 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17919 bottom_vpos, dvpos);
17920 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17921 bottom_vpos);
17922 }
17923 else if (dvpos > 0)
17924 {
17925 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17926 bottom_vpos, dvpos);
17927 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17928 first_unchanged_at_end_vpos + dvpos);
17929 }
17930
17931 /* For frame-based redisplay, make sure that current frame and window
17932 matrix are in sync with respect to glyph memory. */
17933 if (!FRAME_WINDOW_P (f))
17934 sync_frame_with_window_matrix_rows (w);
17935
17936 /* Adjust buffer positions in reused rows. */
17937 if (delta || delta_bytes)
17938 increment_matrix_positions (current_matrix,
17939 first_unchanged_at_end_vpos + dvpos,
17940 bottom_vpos, delta, delta_bytes);
17941
17942 /* Adjust Y positions. */
17943 if (dy)
17944 shift_glyph_matrix (w, current_matrix,
17945 first_unchanged_at_end_vpos + dvpos,
17946 bottom_vpos, dy);
17947
17948 if (first_unchanged_at_end_row)
17949 {
17950 first_unchanged_at_end_row += dvpos;
17951 if (first_unchanged_at_end_row->y >= it.last_visible_y
17952 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17953 first_unchanged_at_end_row = NULL;
17954 }
17955
17956 /* If scrolling up, there may be some lines to display at the end of
17957 the window. */
17958 last_text_row_at_end = NULL;
17959 if (dy < 0)
17960 {
17961 /* Scrolling up can leave for example a partially visible line
17962 at the end of the window to be redisplayed. */
17963 /* Set last_row to the glyph row in the current matrix where the
17964 window end line is found. It has been moved up or down in
17965 the matrix by dvpos. */
17966 int last_vpos = w->window_end_vpos + dvpos;
17967 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17968
17969 /* If last_row is the window end line, it should display text. */
17970 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17971
17972 /* If window end line was partially visible before, begin
17973 displaying at that line. Otherwise begin displaying with the
17974 line following it. */
17975 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17976 {
17977 init_to_row_start (&it, w, last_row);
17978 it.vpos = last_vpos;
17979 it.current_y = last_row->y;
17980 }
17981 else
17982 {
17983 init_to_row_end (&it, w, last_row);
17984 it.vpos = 1 + last_vpos;
17985 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17986 ++last_row;
17987 }
17988
17989 /* We may start in a continuation line. If so, we have to
17990 get the right continuation_lines_width and current_x. */
17991 it.continuation_lines_width = last_row->continuation_lines_width;
17992 it.hpos = it.current_x = 0;
17993
17994 /* Display the rest of the lines at the window end. */
17995 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17996 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17997 {
17998 /* Is it always sure that the display agrees with lines in
17999 the current matrix? I don't think so, so we mark rows
18000 displayed invalid in the current matrix by setting their
18001 enabled_p flag to zero. */
18002 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18003 if (display_line (&it))
18004 last_text_row_at_end = it.glyph_row - 1;
18005 }
18006 }
18007
18008 /* Update window_end_pos and window_end_vpos. */
18009 if (first_unchanged_at_end_row && !last_text_row_at_end)
18010 {
18011 /* Window end line if one of the preserved rows from the current
18012 matrix. Set row to the last row displaying text in current
18013 matrix starting at first_unchanged_at_end_row, after
18014 scrolling. */
18015 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18016 row = find_last_row_displaying_text (w->current_matrix, &it,
18017 first_unchanged_at_end_row);
18018 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18019 adjust_window_ends (w, row, 1);
18020 eassert (w->window_end_bytepos >= 0);
18021 IF_DEBUG (debug_method_add (w, "A"));
18022 }
18023 else if (last_text_row_at_end)
18024 {
18025 adjust_window_ends (w, last_text_row_at_end, 0);
18026 eassert (w->window_end_bytepos >= 0);
18027 IF_DEBUG (debug_method_add (w, "B"));
18028 }
18029 else if (last_text_row)
18030 {
18031 /* We have displayed either to the end of the window or at the
18032 end of the window, i.e. the last row with text is to be found
18033 in the desired matrix. */
18034 adjust_window_ends (w, last_text_row, 0);
18035 eassert (w->window_end_bytepos >= 0);
18036 }
18037 else if (first_unchanged_at_end_row == NULL
18038 && last_text_row == NULL
18039 && last_text_row_at_end == NULL)
18040 {
18041 /* Displayed to end of window, but no line containing text was
18042 displayed. Lines were deleted at the end of the window. */
18043 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18044 int vpos = w->window_end_vpos;
18045 struct glyph_row *current_row = current_matrix->rows + vpos;
18046 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18047
18048 for (row = NULL;
18049 row == NULL && vpos >= first_vpos;
18050 --vpos, --current_row, --desired_row)
18051 {
18052 if (desired_row->enabled_p)
18053 {
18054 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18055 row = desired_row;
18056 }
18057 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18058 row = current_row;
18059 }
18060
18061 eassert (row != NULL);
18062 w->window_end_vpos = vpos + 1;
18063 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18064 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18065 eassert (w->window_end_bytepos >= 0);
18066 IF_DEBUG (debug_method_add (w, "C"));
18067 }
18068 else
18069 emacs_abort ();
18070
18071 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18072 debug_end_vpos = w->window_end_vpos));
18073
18074 /* Record that display has not been completed. */
18075 w->window_end_valid = 0;
18076 w->desired_matrix->no_scrolling_p = 1;
18077 return 3;
18078
18079 #undef GIVE_UP
18080 }
18081
18082
18083 \f
18084 /***********************************************************************
18085 More debugging support
18086 ***********************************************************************/
18087
18088 #ifdef GLYPH_DEBUG
18089
18090 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18091 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18092 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18093
18094
18095 /* Dump the contents of glyph matrix MATRIX on stderr.
18096
18097 GLYPHS 0 means don't show glyph contents.
18098 GLYPHS 1 means show glyphs in short form
18099 GLYPHS > 1 means show glyphs in long form. */
18100
18101 void
18102 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18103 {
18104 int i;
18105 for (i = 0; i < matrix->nrows; ++i)
18106 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18107 }
18108
18109
18110 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18111 the glyph row and area where the glyph comes from. */
18112
18113 void
18114 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18115 {
18116 if (glyph->type == CHAR_GLYPH
18117 || glyph->type == GLYPHLESS_GLYPH)
18118 {
18119 fprintf (stderr,
18120 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18121 glyph - row->glyphs[TEXT_AREA],
18122 (glyph->type == CHAR_GLYPH
18123 ? 'C'
18124 : 'G'),
18125 glyph->charpos,
18126 (BUFFERP (glyph->object)
18127 ? 'B'
18128 : (STRINGP (glyph->object)
18129 ? 'S'
18130 : (INTEGERP (glyph->object)
18131 ? '0'
18132 : '-'))),
18133 glyph->pixel_width,
18134 glyph->u.ch,
18135 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18136 ? glyph->u.ch
18137 : '.'),
18138 glyph->face_id,
18139 glyph->left_box_line_p,
18140 glyph->right_box_line_p);
18141 }
18142 else if (glyph->type == STRETCH_GLYPH)
18143 {
18144 fprintf (stderr,
18145 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18146 glyph - row->glyphs[TEXT_AREA],
18147 'S',
18148 glyph->charpos,
18149 (BUFFERP (glyph->object)
18150 ? 'B'
18151 : (STRINGP (glyph->object)
18152 ? 'S'
18153 : (INTEGERP (glyph->object)
18154 ? '0'
18155 : '-'))),
18156 glyph->pixel_width,
18157 0,
18158 ' ',
18159 glyph->face_id,
18160 glyph->left_box_line_p,
18161 glyph->right_box_line_p);
18162 }
18163 else if (glyph->type == IMAGE_GLYPH)
18164 {
18165 fprintf (stderr,
18166 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18167 glyph - row->glyphs[TEXT_AREA],
18168 'I',
18169 glyph->charpos,
18170 (BUFFERP (glyph->object)
18171 ? 'B'
18172 : (STRINGP (glyph->object)
18173 ? 'S'
18174 : (INTEGERP (glyph->object)
18175 ? '0'
18176 : '-'))),
18177 glyph->pixel_width,
18178 glyph->u.img_id,
18179 '.',
18180 glyph->face_id,
18181 glyph->left_box_line_p,
18182 glyph->right_box_line_p);
18183 }
18184 else if (glyph->type == COMPOSITE_GLYPH)
18185 {
18186 fprintf (stderr,
18187 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18188 glyph - row->glyphs[TEXT_AREA],
18189 '+',
18190 glyph->charpos,
18191 (BUFFERP (glyph->object)
18192 ? 'B'
18193 : (STRINGP (glyph->object)
18194 ? 'S'
18195 : (INTEGERP (glyph->object)
18196 ? '0'
18197 : '-'))),
18198 glyph->pixel_width,
18199 glyph->u.cmp.id);
18200 if (glyph->u.cmp.automatic)
18201 fprintf (stderr,
18202 "[%d-%d]",
18203 glyph->slice.cmp.from, glyph->slice.cmp.to);
18204 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18205 glyph->face_id,
18206 glyph->left_box_line_p,
18207 glyph->right_box_line_p);
18208 }
18209 }
18210
18211
18212 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18213 GLYPHS 0 means don't show glyph contents.
18214 GLYPHS 1 means show glyphs in short form
18215 GLYPHS > 1 means show glyphs in long form. */
18216
18217 void
18218 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18219 {
18220 if (glyphs != 1)
18221 {
18222 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18223 fprintf (stderr, "==============================================================================\n");
18224
18225 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18226 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18227 vpos,
18228 MATRIX_ROW_START_CHARPOS (row),
18229 MATRIX_ROW_END_CHARPOS (row),
18230 row->used[TEXT_AREA],
18231 row->contains_overlapping_glyphs_p,
18232 row->enabled_p,
18233 row->truncated_on_left_p,
18234 row->truncated_on_right_p,
18235 row->continued_p,
18236 MATRIX_ROW_CONTINUATION_LINE_P (row),
18237 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18238 row->ends_at_zv_p,
18239 row->fill_line_p,
18240 row->ends_in_middle_of_char_p,
18241 row->starts_in_middle_of_char_p,
18242 row->mouse_face_p,
18243 row->x,
18244 row->y,
18245 row->pixel_width,
18246 row->height,
18247 row->visible_height,
18248 row->ascent,
18249 row->phys_ascent);
18250 /* The next 3 lines should align to "Start" in the header. */
18251 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18252 row->end.overlay_string_index,
18253 row->continuation_lines_width);
18254 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18255 CHARPOS (row->start.string_pos),
18256 CHARPOS (row->end.string_pos));
18257 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18258 row->end.dpvec_index);
18259 }
18260
18261 if (glyphs > 1)
18262 {
18263 int area;
18264
18265 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18266 {
18267 struct glyph *glyph = row->glyphs[area];
18268 struct glyph *glyph_end = glyph + row->used[area];
18269
18270 /* Glyph for a line end in text. */
18271 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18272 ++glyph_end;
18273
18274 if (glyph < glyph_end)
18275 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18276
18277 for (; glyph < glyph_end; ++glyph)
18278 dump_glyph (row, glyph, area);
18279 }
18280 }
18281 else if (glyphs == 1)
18282 {
18283 int area;
18284
18285 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18286 {
18287 char *s = alloca (row->used[area] + 4);
18288 int i;
18289
18290 for (i = 0; i < row->used[area]; ++i)
18291 {
18292 struct glyph *glyph = row->glyphs[area] + i;
18293 if (i == row->used[area] - 1
18294 && area == TEXT_AREA
18295 && INTEGERP (glyph->object)
18296 && glyph->type == CHAR_GLYPH
18297 && glyph->u.ch == ' ')
18298 {
18299 strcpy (&s[i], "[\\n]");
18300 i += 4;
18301 }
18302 else if (glyph->type == CHAR_GLYPH
18303 && glyph->u.ch < 0x80
18304 && glyph->u.ch >= ' ')
18305 s[i] = glyph->u.ch;
18306 else
18307 s[i] = '.';
18308 }
18309
18310 s[i] = '\0';
18311 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18312 }
18313 }
18314 }
18315
18316
18317 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18318 Sdump_glyph_matrix, 0, 1, "p",
18319 doc: /* Dump the current matrix of the selected window to stderr.
18320 Shows contents of glyph row structures. With non-nil
18321 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18322 glyphs in short form, otherwise show glyphs in long form. */)
18323 (Lisp_Object glyphs)
18324 {
18325 struct window *w = XWINDOW (selected_window);
18326 struct buffer *buffer = XBUFFER (w->contents);
18327
18328 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18329 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18330 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18331 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18332 fprintf (stderr, "=============================================\n");
18333 dump_glyph_matrix (w->current_matrix,
18334 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18335 return Qnil;
18336 }
18337
18338
18339 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18340 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18341 (void)
18342 {
18343 struct frame *f = XFRAME (selected_frame);
18344 dump_glyph_matrix (f->current_matrix, 1);
18345 return Qnil;
18346 }
18347
18348
18349 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18350 doc: /* Dump glyph row ROW to stderr.
18351 GLYPH 0 means don't dump glyphs.
18352 GLYPH 1 means dump glyphs in short form.
18353 GLYPH > 1 or omitted means dump glyphs in long form. */)
18354 (Lisp_Object row, Lisp_Object glyphs)
18355 {
18356 struct glyph_matrix *matrix;
18357 EMACS_INT vpos;
18358
18359 CHECK_NUMBER (row);
18360 matrix = XWINDOW (selected_window)->current_matrix;
18361 vpos = XINT (row);
18362 if (vpos >= 0 && vpos < matrix->nrows)
18363 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18364 vpos,
18365 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18366 return Qnil;
18367 }
18368
18369
18370 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18371 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18372 GLYPH 0 means don't dump glyphs.
18373 GLYPH 1 means dump glyphs in short form.
18374 GLYPH > 1 or omitted means dump glyphs in long form.
18375
18376 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18377 do nothing. */)
18378 (Lisp_Object row, Lisp_Object glyphs)
18379 {
18380 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18381 struct frame *sf = SELECTED_FRAME ();
18382 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18383 EMACS_INT vpos;
18384
18385 CHECK_NUMBER (row);
18386 vpos = XINT (row);
18387 if (vpos >= 0 && vpos < m->nrows)
18388 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18389 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18390 #endif
18391 return Qnil;
18392 }
18393
18394
18395 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18396 doc: /* Toggle tracing of redisplay.
18397 With ARG, turn tracing on if and only if ARG is positive. */)
18398 (Lisp_Object arg)
18399 {
18400 if (NILP (arg))
18401 trace_redisplay_p = !trace_redisplay_p;
18402 else
18403 {
18404 arg = Fprefix_numeric_value (arg);
18405 trace_redisplay_p = XINT (arg) > 0;
18406 }
18407
18408 return Qnil;
18409 }
18410
18411
18412 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18413 doc: /* Like `format', but print result to stderr.
18414 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18415 (ptrdiff_t nargs, Lisp_Object *args)
18416 {
18417 Lisp_Object s = Fformat (nargs, args);
18418 fprintf (stderr, "%s", SDATA (s));
18419 return Qnil;
18420 }
18421
18422 #endif /* GLYPH_DEBUG */
18423
18424
18425 \f
18426 /***********************************************************************
18427 Building Desired Matrix Rows
18428 ***********************************************************************/
18429
18430 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18431 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18432
18433 static struct glyph_row *
18434 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18435 {
18436 struct frame *f = XFRAME (WINDOW_FRAME (w));
18437 struct buffer *buffer = XBUFFER (w->contents);
18438 struct buffer *old = current_buffer;
18439 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18440 int arrow_len = SCHARS (overlay_arrow_string);
18441 const unsigned char *arrow_end = arrow_string + arrow_len;
18442 const unsigned char *p;
18443 struct it it;
18444 bool multibyte_p;
18445 int n_glyphs_before;
18446
18447 set_buffer_temp (buffer);
18448 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18449 it.glyph_row->used[TEXT_AREA] = 0;
18450 SET_TEXT_POS (it.position, 0, 0);
18451
18452 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18453 p = arrow_string;
18454 while (p < arrow_end)
18455 {
18456 Lisp_Object face, ilisp;
18457
18458 /* Get the next character. */
18459 if (multibyte_p)
18460 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18461 else
18462 {
18463 it.c = it.char_to_display = *p, it.len = 1;
18464 if (! ASCII_CHAR_P (it.c))
18465 it.char_to_display = BYTE8_TO_CHAR (it.c);
18466 }
18467 p += it.len;
18468
18469 /* Get its face. */
18470 ilisp = make_number (p - arrow_string);
18471 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18472 it.face_id = compute_char_face (f, it.char_to_display, face);
18473
18474 /* Compute its width, get its glyphs. */
18475 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18476 SET_TEXT_POS (it.position, -1, -1);
18477 PRODUCE_GLYPHS (&it);
18478
18479 /* If this character doesn't fit any more in the line, we have
18480 to remove some glyphs. */
18481 if (it.current_x > it.last_visible_x)
18482 {
18483 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18484 break;
18485 }
18486 }
18487
18488 set_buffer_temp (old);
18489 return it.glyph_row;
18490 }
18491
18492
18493 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18494 glyphs to insert is determined by produce_special_glyphs. */
18495
18496 static void
18497 insert_left_trunc_glyphs (struct it *it)
18498 {
18499 struct it truncate_it;
18500 struct glyph *from, *end, *to, *toend;
18501
18502 eassert (!FRAME_WINDOW_P (it->f)
18503 || (!it->glyph_row->reversed_p
18504 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18505 || (it->glyph_row->reversed_p
18506 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18507
18508 /* Get the truncation glyphs. */
18509 truncate_it = *it;
18510 truncate_it.current_x = 0;
18511 truncate_it.face_id = DEFAULT_FACE_ID;
18512 truncate_it.glyph_row = &scratch_glyph_row;
18513 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18514 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18515 truncate_it.object = make_number (0);
18516 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18517
18518 /* Overwrite glyphs from IT with truncation glyphs. */
18519 if (!it->glyph_row->reversed_p)
18520 {
18521 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18522
18523 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18524 end = from + tused;
18525 to = it->glyph_row->glyphs[TEXT_AREA];
18526 toend = to + it->glyph_row->used[TEXT_AREA];
18527 if (FRAME_WINDOW_P (it->f))
18528 {
18529 /* On GUI frames, when variable-size fonts are displayed,
18530 the truncation glyphs may need more pixels than the row's
18531 glyphs they overwrite. We overwrite more glyphs to free
18532 enough screen real estate, and enlarge the stretch glyph
18533 on the right (see display_line), if there is one, to
18534 preserve the screen position of the truncation glyphs on
18535 the right. */
18536 int w = 0;
18537 struct glyph *g = to;
18538 short used;
18539
18540 /* The first glyph could be partially visible, in which case
18541 it->glyph_row->x will be negative. But we want the left
18542 truncation glyphs to be aligned at the left margin of the
18543 window, so we override the x coordinate at which the row
18544 will begin. */
18545 it->glyph_row->x = 0;
18546 while (g < toend && w < it->truncation_pixel_width)
18547 {
18548 w += g->pixel_width;
18549 ++g;
18550 }
18551 if (g - to - tused > 0)
18552 {
18553 memmove (to + tused, g, (toend - g) * sizeof(*g));
18554 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18555 }
18556 used = it->glyph_row->used[TEXT_AREA];
18557 if (it->glyph_row->truncated_on_right_p
18558 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18559 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18560 == STRETCH_GLYPH)
18561 {
18562 int extra = w - it->truncation_pixel_width;
18563
18564 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18565 }
18566 }
18567
18568 while (from < end)
18569 *to++ = *from++;
18570
18571 /* There may be padding glyphs left over. Overwrite them too. */
18572 if (!FRAME_WINDOW_P (it->f))
18573 {
18574 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18575 {
18576 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18577 while (from < end)
18578 *to++ = *from++;
18579 }
18580 }
18581
18582 if (to > toend)
18583 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18584 }
18585 else
18586 {
18587 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18588
18589 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18590 that back to front. */
18591 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18592 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18593 toend = it->glyph_row->glyphs[TEXT_AREA];
18594 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18595 if (FRAME_WINDOW_P (it->f))
18596 {
18597 int w = 0;
18598 struct glyph *g = to;
18599
18600 while (g >= toend && w < it->truncation_pixel_width)
18601 {
18602 w += g->pixel_width;
18603 --g;
18604 }
18605 if (to - g - tused > 0)
18606 to = g + tused;
18607 if (it->glyph_row->truncated_on_right_p
18608 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18609 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18610 {
18611 int extra = w - it->truncation_pixel_width;
18612
18613 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18614 }
18615 }
18616
18617 while (from >= end && to >= toend)
18618 *to-- = *from--;
18619 if (!FRAME_WINDOW_P (it->f))
18620 {
18621 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18622 {
18623 from =
18624 truncate_it.glyph_row->glyphs[TEXT_AREA]
18625 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18626 while (from >= end && to >= toend)
18627 *to-- = *from--;
18628 }
18629 }
18630 if (from >= end)
18631 {
18632 /* Need to free some room before prepending additional
18633 glyphs. */
18634 int move_by = from - end + 1;
18635 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18636 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18637
18638 for ( ; g >= g0; g--)
18639 g[move_by] = *g;
18640 while (from >= end)
18641 *to-- = *from--;
18642 it->glyph_row->used[TEXT_AREA] += move_by;
18643 }
18644 }
18645 }
18646
18647 /* Compute the hash code for ROW. */
18648 unsigned
18649 row_hash (struct glyph_row *row)
18650 {
18651 int area, k;
18652 unsigned hashval = 0;
18653
18654 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18655 for (k = 0; k < row->used[area]; ++k)
18656 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18657 + row->glyphs[area][k].u.val
18658 + row->glyphs[area][k].face_id
18659 + row->glyphs[area][k].padding_p
18660 + (row->glyphs[area][k].type << 2));
18661
18662 return hashval;
18663 }
18664
18665 /* Compute the pixel height and width of IT->glyph_row.
18666
18667 Most of the time, ascent and height of a display line will be equal
18668 to the max_ascent and max_height values of the display iterator
18669 structure. This is not the case if
18670
18671 1. We hit ZV without displaying anything. In this case, max_ascent
18672 and max_height will be zero.
18673
18674 2. We have some glyphs that don't contribute to the line height.
18675 (The glyph row flag contributes_to_line_height_p is for future
18676 pixmap extensions).
18677
18678 The first case is easily covered by using default values because in
18679 these cases, the line height does not really matter, except that it
18680 must not be zero. */
18681
18682 static void
18683 compute_line_metrics (struct it *it)
18684 {
18685 struct glyph_row *row = it->glyph_row;
18686
18687 if (FRAME_WINDOW_P (it->f))
18688 {
18689 int i, min_y, max_y;
18690
18691 /* The line may consist of one space only, that was added to
18692 place the cursor on it. If so, the row's height hasn't been
18693 computed yet. */
18694 if (row->height == 0)
18695 {
18696 if (it->max_ascent + it->max_descent == 0)
18697 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18698 row->ascent = it->max_ascent;
18699 row->height = it->max_ascent + it->max_descent;
18700 row->phys_ascent = it->max_phys_ascent;
18701 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18702 row->extra_line_spacing = it->max_extra_line_spacing;
18703 }
18704
18705 /* Compute the width of this line. */
18706 row->pixel_width = row->x;
18707 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18708 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18709
18710 eassert (row->pixel_width >= 0);
18711 eassert (row->ascent >= 0 && row->height > 0);
18712
18713 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18714 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18715
18716 /* If first line's physical ascent is larger than its logical
18717 ascent, use the physical ascent, and make the row taller.
18718 This makes accented characters fully visible. */
18719 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18720 && row->phys_ascent > row->ascent)
18721 {
18722 row->height += row->phys_ascent - row->ascent;
18723 row->ascent = row->phys_ascent;
18724 }
18725
18726 /* Compute how much of the line is visible. */
18727 row->visible_height = row->height;
18728
18729 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18730 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18731
18732 if (row->y < min_y)
18733 row->visible_height -= min_y - row->y;
18734 if (row->y + row->height > max_y)
18735 row->visible_height -= row->y + row->height - max_y;
18736 }
18737 else
18738 {
18739 row->pixel_width = row->used[TEXT_AREA];
18740 if (row->continued_p)
18741 row->pixel_width -= it->continuation_pixel_width;
18742 else if (row->truncated_on_right_p)
18743 row->pixel_width -= it->truncation_pixel_width;
18744 row->ascent = row->phys_ascent = 0;
18745 row->height = row->phys_height = row->visible_height = 1;
18746 row->extra_line_spacing = 0;
18747 }
18748
18749 /* Compute a hash code for this row. */
18750 row->hash = row_hash (row);
18751
18752 it->max_ascent = it->max_descent = 0;
18753 it->max_phys_ascent = it->max_phys_descent = 0;
18754 }
18755
18756
18757 /* Append one space to the glyph row of iterator IT if doing a
18758 window-based redisplay. The space has the same face as
18759 IT->face_id. Value is non-zero if a space was added.
18760
18761 This function is called to make sure that there is always one glyph
18762 at the end of a glyph row that the cursor can be set on under
18763 window-systems. (If there weren't such a glyph we would not know
18764 how wide and tall a box cursor should be displayed).
18765
18766 At the same time this space let's a nicely handle clearing to the
18767 end of the line if the row ends in italic text. */
18768
18769 static int
18770 append_space_for_newline (struct it *it, int default_face_p)
18771 {
18772 if (FRAME_WINDOW_P (it->f))
18773 {
18774 int n = it->glyph_row->used[TEXT_AREA];
18775
18776 if (it->glyph_row->glyphs[TEXT_AREA] + n
18777 < it->glyph_row->glyphs[1 + TEXT_AREA])
18778 {
18779 /* Save some values that must not be changed.
18780 Must save IT->c and IT->len because otherwise
18781 ITERATOR_AT_END_P wouldn't work anymore after
18782 append_space_for_newline has been called. */
18783 enum display_element_type saved_what = it->what;
18784 int saved_c = it->c, saved_len = it->len;
18785 int saved_char_to_display = it->char_to_display;
18786 int saved_x = it->current_x;
18787 int saved_face_id = it->face_id;
18788 int saved_box_end = it->end_of_box_run_p;
18789 struct text_pos saved_pos;
18790 Lisp_Object saved_object;
18791 struct face *face;
18792
18793 saved_object = it->object;
18794 saved_pos = it->position;
18795
18796 it->what = IT_CHARACTER;
18797 memset (&it->position, 0, sizeof it->position);
18798 it->object = make_number (0);
18799 it->c = it->char_to_display = ' ';
18800 it->len = 1;
18801
18802 /* If the default face was remapped, be sure to use the
18803 remapped face for the appended newline. */
18804 if (default_face_p)
18805 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18806 else if (it->face_before_selective_p)
18807 it->face_id = it->saved_face_id;
18808 face = FACE_FROM_ID (it->f, it->face_id);
18809 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18810 /* In R2L rows, we will prepend a stretch glyph that will
18811 have the end_of_box_run_p flag set for it, so there's no
18812 need for the appended newline glyph to have that flag
18813 set. */
18814 if (it->glyph_row->reversed_p
18815 /* But if the appended newline glyph goes all the way to
18816 the end of the row, there will be no stretch glyph,
18817 so leave the box flag set. */
18818 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18819 it->end_of_box_run_p = 0;
18820
18821 PRODUCE_GLYPHS (it);
18822
18823 it->override_ascent = -1;
18824 it->constrain_row_ascent_descent_p = 0;
18825 it->current_x = saved_x;
18826 it->object = saved_object;
18827 it->position = saved_pos;
18828 it->what = saved_what;
18829 it->face_id = saved_face_id;
18830 it->len = saved_len;
18831 it->c = saved_c;
18832 it->char_to_display = saved_char_to_display;
18833 it->end_of_box_run_p = saved_box_end;
18834 return 1;
18835 }
18836 }
18837
18838 return 0;
18839 }
18840
18841
18842 /* Extend the face of the last glyph in the text area of IT->glyph_row
18843 to the end of the display line. Called from display_line. If the
18844 glyph row is empty, add a space glyph to it so that we know the
18845 face to draw. Set the glyph row flag fill_line_p. If the glyph
18846 row is R2L, prepend a stretch glyph to cover the empty space to the
18847 left of the leftmost glyph. */
18848
18849 static void
18850 extend_face_to_end_of_line (struct it *it)
18851 {
18852 struct face *face, *default_face;
18853 struct frame *f = it->f;
18854
18855 /* If line is already filled, do nothing. Non window-system frames
18856 get a grace of one more ``pixel'' because their characters are
18857 1-``pixel'' wide, so they hit the equality too early. This grace
18858 is needed only for R2L rows that are not continued, to produce
18859 one extra blank where we could display the cursor. */
18860 if ((it->current_x >= it->last_visible_x
18861 + (!FRAME_WINDOW_P (f)
18862 && it->glyph_row->reversed_p
18863 && !it->glyph_row->continued_p))
18864 /* If the window has display margins, we will need to extend
18865 their face even if the text area is filled. */
18866 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18867 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18868 return;
18869
18870 /* The default face, possibly remapped. */
18871 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18872
18873 /* Face extension extends the background and box of IT->face_id
18874 to the end of the line. If the background equals the background
18875 of the frame, we don't have to do anything. */
18876 if (it->face_before_selective_p)
18877 face = FACE_FROM_ID (f, it->saved_face_id);
18878 else
18879 face = FACE_FROM_ID (f, it->face_id);
18880
18881 if (FRAME_WINDOW_P (f)
18882 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18883 && face->box == FACE_NO_BOX
18884 && face->background == FRAME_BACKGROUND_PIXEL (f)
18885 #ifdef HAVE_WINDOW_SYSTEM
18886 && !face->stipple
18887 #endif
18888 && !it->glyph_row->reversed_p)
18889 return;
18890
18891 /* Set the glyph row flag indicating that the face of the last glyph
18892 in the text area has to be drawn to the end of the text area. */
18893 it->glyph_row->fill_line_p = 1;
18894
18895 /* If current character of IT is not ASCII, make sure we have the
18896 ASCII face. This will be automatically undone the next time
18897 get_next_display_element returns a multibyte character. Note
18898 that the character will always be single byte in unibyte
18899 text. */
18900 if (!ASCII_CHAR_P (it->c))
18901 {
18902 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18903 }
18904
18905 if (FRAME_WINDOW_P (f))
18906 {
18907 /* If the row is empty, add a space with the current face of IT,
18908 so that we know which face to draw. */
18909 if (it->glyph_row->used[TEXT_AREA] == 0)
18910 {
18911 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18912 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18913 it->glyph_row->used[TEXT_AREA] = 1;
18914 }
18915 /* Mode line and the header line don't have margins, and
18916 likewise the frame's tool-bar window, if there is any. */
18917 if (!(it->glyph_row->mode_line_p
18918 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18919 || (WINDOWP (f->tool_bar_window)
18920 && it->w == XWINDOW (f->tool_bar_window))
18921 #endif
18922 ))
18923 {
18924 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18925 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18926 {
18927 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18928 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18929 default_face->id;
18930 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18931 }
18932 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18933 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18934 {
18935 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18936 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18937 default_face->id;
18938 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18939 }
18940 }
18941 #ifdef HAVE_WINDOW_SYSTEM
18942 if (it->glyph_row->reversed_p)
18943 {
18944 /* Prepend a stretch glyph to the row, such that the
18945 rightmost glyph will be drawn flushed all the way to the
18946 right margin of the window. The stretch glyph that will
18947 occupy the empty space, if any, to the left of the
18948 glyphs. */
18949 struct font *font = face->font ? face->font : FRAME_FONT (f);
18950 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18951 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18952 struct glyph *g;
18953 int row_width, stretch_ascent, stretch_width;
18954 struct text_pos saved_pos;
18955 int saved_face_id, saved_avoid_cursor, saved_box_start;
18956
18957 for (row_width = 0, g = row_start; g < row_end; g++)
18958 row_width += g->pixel_width;
18959 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18960 if (stretch_width > 0)
18961 {
18962 stretch_ascent =
18963 (((it->ascent + it->descent)
18964 * FONT_BASE (font)) / FONT_HEIGHT (font));
18965 saved_pos = it->position;
18966 memset (&it->position, 0, sizeof it->position);
18967 saved_avoid_cursor = it->avoid_cursor_p;
18968 it->avoid_cursor_p = 1;
18969 saved_face_id = it->face_id;
18970 saved_box_start = it->start_of_box_run_p;
18971 /* The last row's stretch glyph should get the default
18972 face, to avoid painting the rest of the window with
18973 the region face, if the region ends at ZV. */
18974 if (it->glyph_row->ends_at_zv_p)
18975 it->face_id = default_face->id;
18976 else
18977 it->face_id = face->id;
18978 it->start_of_box_run_p = 0;
18979 append_stretch_glyph (it, make_number (0), stretch_width,
18980 it->ascent + it->descent, stretch_ascent);
18981 it->position = saved_pos;
18982 it->avoid_cursor_p = saved_avoid_cursor;
18983 it->face_id = saved_face_id;
18984 it->start_of_box_run_p = saved_box_start;
18985 }
18986 }
18987 #endif /* HAVE_WINDOW_SYSTEM */
18988 }
18989 else
18990 {
18991 /* Save some values that must not be changed. */
18992 int saved_x = it->current_x;
18993 struct text_pos saved_pos;
18994 Lisp_Object saved_object;
18995 enum display_element_type saved_what = it->what;
18996 int saved_face_id = it->face_id;
18997
18998 saved_object = it->object;
18999 saved_pos = it->position;
19000
19001 it->what = IT_CHARACTER;
19002 memset (&it->position, 0, sizeof it->position);
19003 it->object = make_number (0);
19004 it->c = it->char_to_display = ' ';
19005 it->len = 1;
19006
19007 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19008 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19009 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19010 && !it->glyph_row->mode_line_p
19011 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19012 {
19013 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19014 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19015
19016 for (it->current_x = 0; g < e; g++)
19017 it->current_x += g->pixel_width;
19018
19019 it->area = LEFT_MARGIN_AREA;
19020 it->face_id = default_face->id;
19021 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19022 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19023 {
19024 PRODUCE_GLYPHS (it);
19025 /* term.c:produce_glyphs advances it->current_x only for
19026 TEXT_AREA. */
19027 it->current_x += it->pixel_width;
19028 }
19029
19030 it->current_x = saved_x;
19031 it->area = TEXT_AREA;
19032 }
19033
19034 /* The last row's blank glyphs should get the default face, to
19035 avoid painting the rest of the window with the region face,
19036 if the region ends at ZV. */
19037 if (it->glyph_row->ends_at_zv_p)
19038 it->face_id = default_face->id;
19039 else
19040 it->face_id = face->id;
19041 PRODUCE_GLYPHS (it);
19042
19043 while (it->current_x <= it->last_visible_x)
19044 PRODUCE_GLYPHS (it);
19045
19046 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19047 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19048 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19049 && !it->glyph_row->mode_line_p
19050 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19051 {
19052 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19053 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19054
19055 for ( ; g < e; g++)
19056 it->current_x += g->pixel_width;
19057
19058 it->area = RIGHT_MARGIN_AREA;
19059 it->face_id = default_face->id;
19060 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19061 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19062 {
19063 PRODUCE_GLYPHS (it);
19064 it->current_x += it->pixel_width;
19065 }
19066
19067 it->area = TEXT_AREA;
19068 }
19069
19070 /* Don't count these blanks really. It would let us insert a left
19071 truncation glyph below and make us set the cursor on them, maybe. */
19072 it->current_x = saved_x;
19073 it->object = saved_object;
19074 it->position = saved_pos;
19075 it->what = saved_what;
19076 it->face_id = saved_face_id;
19077 }
19078 }
19079
19080
19081 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19082 trailing whitespace. */
19083
19084 static int
19085 trailing_whitespace_p (ptrdiff_t charpos)
19086 {
19087 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19088 int c = 0;
19089
19090 while (bytepos < ZV_BYTE
19091 && (c = FETCH_CHAR (bytepos),
19092 c == ' ' || c == '\t'))
19093 ++bytepos;
19094
19095 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19096 {
19097 if (bytepos != PT_BYTE)
19098 return 1;
19099 }
19100 return 0;
19101 }
19102
19103
19104 /* Highlight trailing whitespace, if any, in ROW. */
19105
19106 static void
19107 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19108 {
19109 int used = row->used[TEXT_AREA];
19110
19111 if (used)
19112 {
19113 struct glyph *start = row->glyphs[TEXT_AREA];
19114 struct glyph *glyph = start + used - 1;
19115
19116 if (row->reversed_p)
19117 {
19118 /* Right-to-left rows need to be processed in the opposite
19119 direction, so swap the edge pointers. */
19120 glyph = start;
19121 start = row->glyphs[TEXT_AREA] + used - 1;
19122 }
19123
19124 /* Skip over glyphs inserted to display the cursor at the
19125 end of a line, for extending the face of the last glyph
19126 to the end of the line on terminals, and for truncation
19127 and continuation glyphs. */
19128 if (!row->reversed_p)
19129 {
19130 while (glyph >= start
19131 && glyph->type == CHAR_GLYPH
19132 && INTEGERP (glyph->object))
19133 --glyph;
19134 }
19135 else
19136 {
19137 while (glyph <= start
19138 && glyph->type == CHAR_GLYPH
19139 && INTEGERP (glyph->object))
19140 ++glyph;
19141 }
19142
19143 /* If last glyph is a space or stretch, and it's trailing
19144 whitespace, set the face of all trailing whitespace glyphs in
19145 IT->glyph_row to `trailing-whitespace'. */
19146 if ((row->reversed_p ? glyph <= start : glyph >= start)
19147 && BUFFERP (glyph->object)
19148 && (glyph->type == STRETCH_GLYPH
19149 || (glyph->type == CHAR_GLYPH
19150 && glyph->u.ch == ' '))
19151 && trailing_whitespace_p (glyph->charpos))
19152 {
19153 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19154 if (face_id < 0)
19155 return;
19156
19157 if (!row->reversed_p)
19158 {
19159 while (glyph >= start
19160 && BUFFERP (glyph->object)
19161 && (glyph->type == STRETCH_GLYPH
19162 || (glyph->type == CHAR_GLYPH
19163 && glyph->u.ch == ' ')))
19164 (glyph--)->face_id = face_id;
19165 }
19166 else
19167 {
19168 while (glyph <= start
19169 && BUFFERP (glyph->object)
19170 && (glyph->type == STRETCH_GLYPH
19171 || (glyph->type == CHAR_GLYPH
19172 && glyph->u.ch == ' ')))
19173 (glyph++)->face_id = face_id;
19174 }
19175 }
19176 }
19177 }
19178
19179
19180 /* Value is non-zero if glyph row ROW should be
19181 considered to hold the buffer position CHARPOS. */
19182
19183 static int
19184 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19185 {
19186 int result = 1;
19187
19188 if (charpos == CHARPOS (row->end.pos)
19189 || charpos == MATRIX_ROW_END_CHARPOS (row))
19190 {
19191 /* Suppose the row ends on a string.
19192 Unless the row is continued, that means it ends on a newline
19193 in the string. If it's anything other than a display string
19194 (e.g., a before-string from an overlay), we don't want the
19195 cursor there. (This heuristic seems to give the optimal
19196 behavior for the various types of multi-line strings.)
19197 One exception: if the string has `cursor' property on one of
19198 its characters, we _do_ want the cursor there. */
19199 if (CHARPOS (row->end.string_pos) >= 0)
19200 {
19201 if (row->continued_p)
19202 result = 1;
19203 else
19204 {
19205 /* Check for `display' property. */
19206 struct glyph *beg = row->glyphs[TEXT_AREA];
19207 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19208 struct glyph *glyph;
19209
19210 result = 0;
19211 for (glyph = end; glyph >= beg; --glyph)
19212 if (STRINGP (glyph->object))
19213 {
19214 Lisp_Object prop
19215 = Fget_char_property (make_number (charpos),
19216 Qdisplay, Qnil);
19217 result =
19218 (!NILP (prop)
19219 && display_prop_string_p (prop, glyph->object));
19220 /* If there's a `cursor' property on one of the
19221 string's characters, this row is a cursor row,
19222 even though this is not a display string. */
19223 if (!result)
19224 {
19225 Lisp_Object s = glyph->object;
19226
19227 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19228 {
19229 ptrdiff_t gpos = glyph->charpos;
19230
19231 if (!NILP (Fget_char_property (make_number (gpos),
19232 Qcursor, s)))
19233 {
19234 result = 1;
19235 break;
19236 }
19237 }
19238 }
19239 break;
19240 }
19241 }
19242 }
19243 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19244 {
19245 /* If the row ends in middle of a real character,
19246 and the line is continued, we want the cursor here.
19247 That's because CHARPOS (ROW->end.pos) would equal
19248 PT if PT is before the character. */
19249 if (!row->ends_in_ellipsis_p)
19250 result = row->continued_p;
19251 else
19252 /* If the row ends in an ellipsis, then
19253 CHARPOS (ROW->end.pos) will equal point after the
19254 invisible text. We want that position to be displayed
19255 after the ellipsis. */
19256 result = 0;
19257 }
19258 /* If the row ends at ZV, display the cursor at the end of that
19259 row instead of at the start of the row below. */
19260 else if (row->ends_at_zv_p)
19261 result = 1;
19262 else
19263 result = 0;
19264 }
19265
19266 return result;
19267 }
19268
19269 /* Value is non-zero if glyph row ROW should be
19270 used to hold the cursor. */
19271
19272 static int
19273 cursor_row_p (struct glyph_row *row)
19274 {
19275 return row_for_charpos_p (row, PT);
19276 }
19277
19278 \f
19279
19280 /* Push the property PROP so that it will be rendered at the current
19281 position in IT. Return 1 if PROP was successfully pushed, 0
19282 otherwise. Called from handle_line_prefix to handle the
19283 `line-prefix' and `wrap-prefix' properties. */
19284
19285 static int
19286 push_prefix_prop (struct it *it, Lisp_Object prop)
19287 {
19288 struct text_pos pos =
19289 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19290
19291 eassert (it->method == GET_FROM_BUFFER
19292 || it->method == GET_FROM_DISPLAY_VECTOR
19293 || it->method == GET_FROM_STRING);
19294
19295 /* We need to save the current buffer/string position, so it will be
19296 restored by pop_it, because iterate_out_of_display_property
19297 depends on that being set correctly, but some situations leave
19298 it->position not yet set when this function is called. */
19299 push_it (it, &pos);
19300
19301 if (STRINGP (prop))
19302 {
19303 if (SCHARS (prop) == 0)
19304 {
19305 pop_it (it);
19306 return 0;
19307 }
19308
19309 it->string = prop;
19310 it->string_from_prefix_prop_p = 1;
19311 it->multibyte_p = STRING_MULTIBYTE (it->string);
19312 it->current.overlay_string_index = -1;
19313 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19314 it->end_charpos = it->string_nchars = SCHARS (it->string);
19315 it->method = GET_FROM_STRING;
19316 it->stop_charpos = 0;
19317 it->prev_stop = 0;
19318 it->base_level_stop = 0;
19319
19320 /* Force paragraph direction to be that of the parent
19321 buffer/string. */
19322 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19323 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19324 else
19325 it->paragraph_embedding = L2R;
19326
19327 /* Set up the bidi iterator for this display string. */
19328 if (it->bidi_p)
19329 {
19330 it->bidi_it.string.lstring = it->string;
19331 it->bidi_it.string.s = NULL;
19332 it->bidi_it.string.schars = it->end_charpos;
19333 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19334 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19335 it->bidi_it.string.unibyte = !it->multibyte_p;
19336 it->bidi_it.w = it->w;
19337 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19338 }
19339 }
19340 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19341 {
19342 it->method = GET_FROM_STRETCH;
19343 it->object = prop;
19344 }
19345 #ifdef HAVE_WINDOW_SYSTEM
19346 else if (IMAGEP (prop))
19347 {
19348 it->what = IT_IMAGE;
19349 it->image_id = lookup_image (it->f, prop);
19350 it->method = GET_FROM_IMAGE;
19351 }
19352 #endif /* HAVE_WINDOW_SYSTEM */
19353 else
19354 {
19355 pop_it (it); /* bogus display property, give up */
19356 return 0;
19357 }
19358
19359 return 1;
19360 }
19361
19362 /* Return the character-property PROP at the current position in IT. */
19363
19364 static Lisp_Object
19365 get_it_property (struct it *it, Lisp_Object prop)
19366 {
19367 Lisp_Object position, object = it->object;
19368
19369 if (STRINGP (object))
19370 position = make_number (IT_STRING_CHARPOS (*it));
19371 else if (BUFFERP (object))
19372 {
19373 position = make_number (IT_CHARPOS (*it));
19374 object = it->window;
19375 }
19376 else
19377 return Qnil;
19378
19379 return Fget_char_property (position, prop, object);
19380 }
19381
19382 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19383
19384 static void
19385 handle_line_prefix (struct it *it)
19386 {
19387 Lisp_Object prefix;
19388
19389 if (it->continuation_lines_width > 0)
19390 {
19391 prefix = get_it_property (it, Qwrap_prefix);
19392 if (NILP (prefix))
19393 prefix = Vwrap_prefix;
19394 }
19395 else
19396 {
19397 prefix = get_it_property (it, Qline_prefix);
19398 if (NILP (prefix))
19399 prefix = Vline_prefix;
19400 }
19401 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19402 {
19403 /* If the prefix is wider than the window, and we try to wrap
19404 it, it would acquire its own wrap prefix, and so on till the
19405 iterator stack overflows. So, don't wrap the prefix. */
19406 it->line_wrap = TRUNCATE;
19407 it->avoid_cursor_p = 1;
19408 }
19409 }
19410
19411 \f
19412
19413 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19414 only for R2L lines from display_line and display_string, when they
19415 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19416 the line/string needs to be continued on the next glyph row. */
19417 static void
19418 unproduce_glyphs (struct it *it, int n)
19419 {
19420 struct glyph *glyph, *end;
19421
19422 eassert (it->glyph_row);
19423 eassert (it->glyph_row->reversed_p);
19424 eassert (it->area == TEXT_AREA);
19425 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19426
19427 if (n > it->glyph_row->used[TEXT_AREA])
19428 n = it->glyph_row->used[TEXT_AREA];
19429 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19430 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19431 for ( ; glyph < end; glyph++)
19432 glyph[-n] = *glyph;
19433 }
19434
19435 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19436 and ROW->maxpos. */
19437 static void
19438 find_row_edges (struct it *it, struct glyph_row *row,
19439 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19440 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19441 {
19442 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19443 lines' rows is implemented for bidi-reordered rows. */
19444
19445 /* ROW->minpos is the value of min_pos, the minimal buffer position
19446 we have in ROW, or ROW->start.pos if that is smaller. */
19447 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19448 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19449 else
19450 /* We didn't find buffer positions smaller than ROW->start, or
19451 didn't find _any_ valid buffer positions in any of the glyphs,
19452 so we must trust the iterator's computed positions. */
19453 row->minpos = row->start.pos;
19454 if (max_pos <= 0)
19455 {
19456 max_pos = CHARPOS (it->current.pos);
19457 max_bpos = BYTEPOS (it->current.pos);
19458 }
19459
19460 /* Here are the various use-cases for ending the row, and the
19461 corresponding values for ROW->maxpos:
19462
19463 Line ends in a newline from buffer eol_pos + 1
19464 Line is continued from buffer max_pos + 1
19465 Line is truncated on right it->current.pos
19466 Line ends in a newline from string max_pos + 1(*)
19467 (*) + 1 only when line ends in a forward scan
19468 Line is continued from string max_pos
19469 Line is continued from display vector max_pos
19470 Line is entirely from a string min_pos == max_pos
19471 Line is entirely from a display vector min_pos == max_pos
19472 Line that ends at ZV ZV
19473
19474 If you discover other use-cases, please add them here as
19475 appropriate. */
19476 if (row->ends_at_zv_p)
19477 row->maxpos = it->current.pos;
19478 else if (row->used[TEXT_AREA])
19479 {
19480 int seen_this_string = 0;
19481 struct glyph_row *r1 = row - 1;
19482
19483 /* Did we see the same display string on the previous row? */
19484 if (STRINGP (it->object)
19485 /* this is not the first row */
19486 && row > it->w->desired_matrix->rows
19487 /* previous row is not the header line */
19488 && !r1->mode_line_p
19489 /* previous row also ends in a newline from a string */
19490 && r1->ends_in_newline_from_string_p)
19491 {
19492 struct glyph *start, *end;
19493
19494 /* Search for the last glyph of the previous row that came
19495 from buffer or string. Depending on whether the row is
19496 L2R or R2L, we need to process it front to back or the
19497 other way round. */
19498 if (!r1->reversed_p)
19499 {
19500 start = r1->glyphs[TEXT_AREA];
19501 end = start + r1->used[TEXT_AREA];
19502 /* Glyphs inserted by redisplay have an integer (zero)
19503 as their object. */
19504 while (end > start
19505 && INTEGERP ((end - 1)->object)
19506 && (end - 1)->charpos <= 0)
19507 --end;
19508 if (end > start)
19509 {
19510 if (EQ ((end - 1)->object, it->object))
19511 seen_this_string = 1;
19512 }
19513 else
19514 /* If all the glyphs of the previous row were inserted
19515 by redisplay, it means the previous row was
19516 produced from a single newline, which is only
19517 possible if that newline came from the same string
19518 as the one which produced this ROW. */
19519 seen_this_string = 1;
19520 }
19521 else
19522 {
19523 end = r1->glyphs[TEXT_AREA] - 1;
19524 start = end + r1->used[TEXT_AREA];
19525 while (end < start
19526 && INTEGERP ((end + 1)->object)
19527 && (end + 1)->charpos <= 0)
19528 ++end;
19529 if (end < start)
19530 {
19531 if (EQ ((end + 1)->object, it->object))
19532 seen_this_string = 1;
19533 }
19534 else
19535 seen_this_string = 1;
19536 }
19537 }
19538 /* Take note of each display string that covers a newline only
19539 once, the first time we see it. This is for when a display
19540 string includes more than one newline in it. */
19541 if (row->ends_in_newline_from_string_p && !seen_this_string)
19542 {
19543 /* If we were scanning the buffer forward when we displayed
19544 the string, we want to account for at least one buffer
19545 position that belongs to this row (position covered by
19546 the display string), so that cursor positioning will
19547 consider this row as a candidate when point is at the end
19548 of the visual line represented by this row. This is not
19549 required when scanning back, because max_pos will already
19550 have a much larger value. */
19551 if (CHARPOS (row->end.pos) > max_pos)
19552 INC_BOTH (max_pos, max_bpos);
19553 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19554 }
19555 else if (CHARPOS (it->eol_pos) > 0)
19556 SET_TEXT_POS (row->maxpos,
19557 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19558 else if (row->continued_p)
19559 {
19560 /* If max_pos is different from IT's current position, it
19561 means IT->method does not belong to the display element
19562 at max_pos. However, it also means that the display
19563 element at max_pos was displayed in its entirety on this
19564 line, which is equivalent to saying that the next line
19565 starts at the next buffer position. */
19566 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19567 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19568 else
19569 {
19570 INC_BOTH (max_pos, max_bpos);
19571 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19572 }
19573 }
19574 else if (row->truncated_on_right_p)
19575 /* display_line already called reseat_at_next_visible_line_start,
19576 which puts the iterator at the beginning of the next line, in
19577 the logical order. */
19578 row->maxpos = it->current.pos;
19579 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19580 /* A line that is entirely from a string/image/stretch... */
19581 row->maxpos = row->minpos;
19582 else
19583 emacs_abort ();
19584 }
19585 else
19586 row->maxpos = it->current.pos;
19587 }
19588
19589 /* Construct the glyph row IT->glyph_row in the desired matrix of
19590 IT->w from text at the current position of IT. See dispextern.h
19591 for an overview of struct it. Value is non-zero if
19592 IT->glyph_row displays text, as opposed to a line displaying ZV
19593 only. */
19594
19595 static int
19596 display_line (struct it *it)
19597 {
19598 struct glyph_row *row = it->glyph_row;
19599 Lisp_Object overlay_arrow_string;
19600 struct it wrap_it;
19601 void *wrap_data = NULL;
19602 int may_wrap = 0, wrap_x IF_LINT (= 0);
19603 int wrap_row_used = -1;
19604 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19605 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19606 int wrap_row_extra_line_spacing IF_LINT (= 0);
19607 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19608 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19609 int cvpos;
19610 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19611 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19612
19613 /* We always start displaying at hpos zero even if hscrolled. */
19614 eassert (it->hpos == 0 && it->current_x == 0);
19615
19616 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19617 >= it->w->desired_matrix->nrows)
19618 {
19619 it->w->nrows_scale_factor++;
19620 it->f->fonts_changed = 1;
19621 return 0;
19622 }
19623
19624 /* Clear the result glyph row and enable it. */
19625 prepare_desired_row (row);
19626
19627 row->y = it->current_y;
19628 row->start = it->start;
19629 row->continuation_lines_width = it->continuation_lines_width;
19630 row->displays_text_p = 1;
19631 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19632 it->starts_in_middle_of_char_p = 0;
19633
19634 /* Arrange the overlays nicely for our purposes. Usually, we call
19635 display_line on only one line at a time, in which case this
19636 can't really hurt too much, or we call it on lines which appear
19637 one after another in the buffer, in which case all calls to
19638 recenter_overlay_lists but the first will be pretty cheap. */
19639 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19640
19641 /* Move over display elements that are not visible because we are
19642 hscrolled. This may stop at an x-position < IT->first_visible_x
19643 if the first glyph is partially visible or if we hit a line end. */
19644 if (it->current_x < it->first_visible_x)
19645 {
19646 enum move_it_result move_result;
19647
19648 this_line_min_pos = row->start.pos;
19649 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19650 MOVE_TO_POS | MOVE_TO_X);
19651 /* If we are under a large hscroll, move_it_in_display_line_to
19652 could hit the end of the line without reaching
19653 it->first_visible_x. Pretend that we did reach it. This is
19654 especially important on a TTY, where we will call
19655 extend_face_to_end_of_line, which needs to know how many
19656 blank glyphs to produce. */
19657 if (it->current_x < it->first_visible_x
19658 && (move_result == MOVE_NEWLINE_OR_CR
19659 || move_result == MOVE_POS_MATCH_OR_ZV))
19660 it->current_x = it->first_visible_x;
19661
19662 /* Record the smallest positions seen while we moved over
19663 display elements that are not visible. This is needed by
19664 redisplay_internal for optimizing the case where the cursor
19665 stays inside the same line. The rest of this function only
19666 considers positions that are actually displayed, so
19667 RECORD_MAX_MIN_POS will not otherwise record positions that
19668 are hscrolled to the left of the left edge of the window. */
19669 min_pos = CHARPOS (this_line_min_pos);
19670 min_bpos = BYTEPOS (this_line_min_pos);
19671 }
19672 else
19673 {
19674 /* We only do this when not calling `move_it_in_display_line_to'
19675 above, because move_it_in_display_line_to calls
19676 handle_line_prefix itself. */
19677 handle_line_prefix (it);
19678 }
19679
19680 /* Get the initial row height. This is either the height of the
19681 text hscrolled, if there is any, or zero. */
19682 row->ascent = it->max_ascent;
19683 row->height = it->max_ascent + it->max_descent;
19684 row->phys_ascent = it->max_phys_ascent;
19685 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19686 row->extra_line_spacing = it->max_extra_line_spacing;
19687
19688 /* Utility macro to record max and min buffer positions seen until now. */
19689 #define RECORD_MAX_MIN_POS(IT) \
19690 do \
19691 { \
19692 int composition_p = !STRINGP ((IT)->string) \
19693 && ((IT)->what == IT_COMPOSITION); \
19694 ptrdiff_t current_pos = \
19695 composition_p ? (IT)->cmp_it.charpos \
19696 : IT_CHARPOS (*(IT)); \
19697 ptrdiff_t current_bpos = \
19698 composition_p ? CHAR_TO_BYTE (current_pos) \
19699 : IT_BYTEPOS (*(IT)); \
19700 if (current_pos < min_pos) \
19701 { \
19702 min_pos = current_pos; \
19703 min_bpos = current_bpos; \
19704 } \
19705 if (IT_CHARPOS (*it) > max_pos) \
19706 { \
19707 max_pos = IT_CHARPOS (*it); \
19708 max_bpos = IT_BYTEPOS (*it); \
19709 } \
19710 } \
19711 while (0)
19712
19713 /* Loop generating characters. The loop is left with IT on the next
19714 character to display. */
19715 while (1)
19716 {
19717 int n_glyphs_before, hpos_before, x_before;
19718 int x, nglyphs;
19719 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19720
19721 /* Retrieve the next thing to display. Value is zero if end of
19722 buffer reached. */
19723 if (!get_next_display_element (it))
19724 {
19725 /* Maybe add a space at the end of this line that is used to
19726 display the cursor there under X. Set the charpos of the
19727 first glyph of blank lines not corresponding to any text
19728 to -1. */
19729 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19730 row->exact_window_width_line_p = 1;
19731 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19732 || row->used[TEXT_AREA] == 0)
19733 {
19734 row->glyphs[TEXT_AREA]->charpos = -1;
19735 row->displays_text_p = 0;
19736
19737 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19738 && (!MINI_WINDOW_P (it->w)
19739 || (minibuf_level && EQ (it->window, minibuf_window))))
19740 row->indicate_empty_line_p = 1;
19741 }
19742
19743 it->continuation_lines_width = 0;
19744 row->ends_at_zv_p = 1;
19745 /* A row that displays right-to-left text must always have
19746 its last face extended all the way to the end of line,
19747 even if this row ends in ZV, because we still write to
19748 the screen left to right. We also need to extend the
19749 last face if the default face is remapped to some
19750 different face, otherwise the functions that clear
19751 portions of the screen will clear with the default face's
19752 background color. */
19753 if (row->reversed_p
19754 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19755 extend_face_to_end_of_line (it);
19756 break;
19757 }
19758
19759 /* Now, get the metrics of what we want to display. This also
19760 generates glyphs in `row' (which is IT->glyph_row). */
19761 n_glyphs_before = row->used[TEXT_AREA];
19762 x = it->current_x;
19763
19764 /* Remember the line height so far in case the next element doesn't
19765 fit on the line. */
19766 if (it->line_wrap != TRUNCATE)
19767 {
19768 ascent = it->max_ascent;
19769 descent = it->max_descent;
19770 phys_ascent = it->max_phys_ascent;
19771 phys_descent = it->max_phys_descent;
19772
19773 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19774 {
19775 if (IT_DISPLAYING_WHITESPACE (it))
19776 may_wrap = 1;
19777 else if (may_wrap)
19778 {
19779 SAVE_IT (wrap_it, *it, wrap_data);
19780 wrap_x = x;
19781 wrap_row_used = row->used[TEXT_AREA];
19782 wrap_row_ascent = row->ascent;
19783 wrap_row_height = row->height;
19784 wrap_row_phys_ascent = row->phys_ascent;
19785 wrap_row_phys_height = row->phys_height;
19786 wrap_row_extra_line_spacing = row->extra_line_spacing;
19787 wrap_row_min_pos = min_pos;
19788 wrap_row_min_bpos = min_bpos;
19789 wrap_row_max_pos = max_pos;
19790 wrap_row_max_bpos = max_bpos;
19791 may_wrap = 0;
19792 }
19793 }
19794 }
19795
19796 PRODUCE_GLYPHS (it);
19797
19798 /* If this display element was in marginal areas, continue with
19799 the next one. */
19800 if (it->area != TEXT_AREA)
19801 {
19802 row->ascent = max (row->ascent, it->max_ascent);
19803 row->height = max (row->height, it->max_ascent + it->max_descent);
19804 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19805 row->phys_height = max (row->phys_height,
19806 it->max_phys_ascent + it->max_phys_descent);
19807 row->extra_line_spacing = max (row->extra_line_spacing,
19808 it->max_extra_line_spacing);
19809 set_iterator_to_next (it, 1);
19810 continue;
19811 }
19812
19813 /* Does the display element fit on the line? If we truncate
19814 lines, we should draw past the right edge of the window. If
19815 we don't truncate, we want to stop so that we can display the
19816 continuation glyph before the right margin. If lines are
19817 continued, there are two possible strategies for characters
19818 resulting in more than 1 glyph (e.g. tabs): Display as many
19819 glyphs as possible in this line and leave the rest for the
19820 continuation line, or display the whole element in the next
19821 line. Original redisplay did the former, so we do it also. */
19822 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19823 hpos_before = it->hpos;
19824 x_before = x;
19825
19826 if (/* Not a newline. */
19827 nglyphs > 0
19828 /* Glyphs produced fit entirely in the line. */
19829 && it->current_x < it->last_visible_x)
19830 {
19831 it->hpos += nglyphs;
19832 row->ascent = max (row->ascent, it->max_ascent);
19833 row->height = max (row->height, it->max_ascent + it->max_descent);
19834 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19835 row->phys_height = max (row->phys_height,
19836 it->max_phys_ascent + it->max_phys_descent);
19837 row->extra_line_spacing = max (row->extra_line_spacing,
19838 it->max_extra_line_spacing);
19839 if (it->current_x - it->pixel_width < it->first_visible_x)
19840 row->x = x - it->first_visible_x;
19841 /* Record the maximum and minimum buffer positions seen so
19842 far in glyphs that will be displayed by this row. */
19843 if (it->bidi_p)
19844 RECORD_MAX_MIN_POS (it);
19845 }
19846 else
19847 {
19848 int i, new_x;
19849 struct glyph *glyph;
19850
19851 for (i = 0; i < nglyphs; ++i, x = new_x)
19852 {
19853 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19854 new_x = x + glyph->pixel_width;
19855
19856 if (/* Lines are continued. */
19857 it->line_wrap != TRUNCATE
19858 && (/* Glyph doesn't fit on the line. */
19859 new_x > it->last_visible_x
19860 /* Or it fits exactly on a window system frame. */
19861 || (new_x == it->last_visible_x
19862 && FRAME_WINDOW_P (it->f)
19863 && (row->reversed_p
19864 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19865 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19866 {
19867 /* End of a continued line. */
19868
19869 if (it->hpos == 0
19870 || (new_x == it->last_visible_x
19871 && FRAME_WINDOW_P (it->f)
19872 && (row->reversed_p
19873 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19874 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19875 {
19876 /* Current glyph is the only one on the line or
19877 fits exactly on the line. We must continue
19878 the line because we can't draw the cursor
19879 after the glyph. */
19880 row->continued_p = 1;
19881 it->current_x = new_x;
19882 it->continuation_lines_width += new_x;
19883 ++it->hpos;
19884 if (i == nglyphs - 1)
19885 {
19886 /* If line-wrap is on, check if a previous
19887 wrap point was found. */
19888 if (wrap_row_used > 0
19889 /* Even if there is a previous wrap
19890 point, continue the line here as
19891 usual, if (i) the previous character
19892 was a space or tab AND (ii) the
19893 current character is not. */
19894 && (!may_wrap
19895 || IT_DISPLAYING_WHITESPACE (it)))
19896 goto back_to_wrap;
19897
19898 /* Record the maximum and minimum buffer
19899 positions seen so far in glyphs that will be
19900 displayed by this row. */
19901 if (it->bidi_p)
19902 RECORD_MAX_MIN_POS (it);
19903 set_iterator_to_next (it, 1);
19904 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19905 {
19906 if (!get_next_display_element (it))
19907 {
19908 row->exact_window_width_line_p = 1;
19909 it->continuation_lines_width = 0;
19910 row->continued_p = 0;
19911 row->ends_at_zv_p = 1;
19912 }
19913 else if (ITERATOR_AT_END_OF_LINE_P (it))
19914 {
19915 row->continued_p = 0;
19916 row->exact_window_width_line_p = 1;
19917 }
19918 }
19919 }
19920 else if (it->bidi_p)
19921 RECORD_MAX_MIN_POS (it);
19922 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19923 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19924 extend_face_to_end_of_line (it);
19925 }
19926 else if (CHAR_GLYPH_PADDING_P (*glyph)
19927 && !FRAME_WINDOW_P (it->f))
19928 {
19929 /* A padding glyph that doesn't fit on this line.
19930 This means the whole character doesn't fit
19931 on the line. */
19932 if (row->reversed_p)
19933 unproduce_glyphs (it, row->used[TEXT_AREA]
19934 - n_glyphs_before);
19935 row->used[TEXT_AREA] = n_glyphs_before;
19936
19937 /* Fill the rest of the row with continuation
19938 glyphs like in 20.x. */
19939 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19940 < row->glyphs[1 + TEXT_AREA])
19941 produce_special_glyphs (it, IT_CONTINUATION);
19942
19943 row->continued_p = 1;
19944 it->current_x = x_before;
19945 it->continuation_lines_width += x_before;
19946
19947 /* Restore the height to what it was before the
19948 element not fitting on the line. */
19949 it->max_ascent = ascent;
19950 it->max_descent = descent;
19951 it->max_phys_ascent = phys_ascent;
19952 it->max_phys_descent = phys_descent;
19953 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19954 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19955 extend_face_to_end_of_line (it);
19956 }
19957 else if (wrap_row_used > 0)
19958 {
19959 back_to_wrap:
19960 if (row->reversed_p)
19961 unproduce_glyphs (it,
19962 row->used[TEXT_AREA] - wrap_row_used);
19963 RESTORE_IT (it, &wrap_it, wrap_data);
19964 it->continuation_lines_width += wrap_x;
19965 row->used[TEXT_AREA] = wrap_row_used;
19966 row->ascent = wrap_row_ascent;
19967 row->height = wrap_row_height;
19968 row->phys_ascent = wrap_row_phys_ascent;
19969 row->phys_height = wrap_row_phys_height;
19970 row->extra_line_spacing = wrap_row_extra_line_spacing;
19971 min_pos = wrap_row_min_pos;
19972 min_bpos = wrap_row_min_bpos;
19973 max_pos = wrap_row_max_pos;
19974 max_bpos = wrap_row_max_bpos;
19975 row->continued_p = 1;
19976 row->ends_at_zv_p = 0;
19977 row->exact_window_width_line_p = 0;
19978 it->continuation_lines_width += x;
19979
19980 /* Make sure that a non-default face is extended
19981 up to the right margin of the window. */
19982 extend_face_to_end_of_line (it);
19983 }
19984 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19985 {
19986 /* A TAB that extends past the right edge of the
19987 window. This produces a single glyph on
19988 window system frames. We leave the glyph in
19989 this row and let it fill the row, but don't
19990 consume the TAB. */
19991 if ((row->reversed_p
19992 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19993 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19994 produce_special_glyphs (it, IT_CONTINUATION);
19995 it->continuation_lines_width += it->last_visible_x;
19996 row->ends_in_middle_of_char_p = 1;
19997 row->continued_p = 1;
19998 glyph->pixel_width = it->last_visible_x - x;
19999 it->starts_in_middle_of_char_p = 1;
20000 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20001 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20002 extend_face_to_end_of_line (it);
20003 }
20004 else
20005 {
20006 /* Something other than a TAB that draws past
20007 the right edge of the window. Restore
20008 positions to values before the element. */
20009 if (row->reversed_p)
20010 unproduce_glyphs (it, row->used[TEXT_AREA]
20011 - (n_glyphs_before + i));
20012 row->used[TEXT_AREA] = n_glyphs_before + i;
20013
20014 /* Display continuation glyphs. */
20015 it->current_x = x_before;
20016 it->continuation_lines_width += x;
20017 if (!FRAME_WINDOW_P (it->f)
20018 || (row->reversed_p
20019 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20020 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20021 produce_special_glyphs (it, IT_CONTINUATION);
20022 row->continued_p = 1;
20023
20024 extend_face_to_end_of_line (it);
20025
20026 if (nglyphs > 1 && i > 0)
20027 {
20028 row->ends_in_middle_of_char_p = 1;
20029 it->starts_in_middle_of_char_p = 1;
20030 }
20031
20032 /* Restore the height to what it was before the
20033 element not fitting on the line. */
20034 it->max_ascent = ascent;
20035 it->max_descent = descent;
20036 it->max_phys_ascent = phys_ascent;
20037 it->max_phys_descent = phys_descent;
20038 }
20039
20040 break;
20041 }
20042 else if (new_x > it->first_visible_x)
20043 {
20044 /* Increment number of glyphs actually displayed. */
20045 ++it->hpos;
20046
20047 /* Record the maximum and minimum buffer positions
20048 seen so far in glyphs that will be displayed by
20049 this row. */
20050 if (it->bidi_p)
20051 RECORD_MAX_MIN_POS (it);
20052
20053 if (x < it->first_visible_x)
20054 /* Glyph is partially visible, i.e. row starts at
20055 negative X position. */
20056 row->x = x - it->first_visible_x;
20057 }
20058 else
20059 {
20060 /* Glyph is completely off the left margin of the
20061 window. This should not happen because of the
20062 move_it_in_display_line at the start of this
20063 function, unless the text display area of the
20064 window is empty. */
20065 eassert (it->first_visible_x <= it->last_visible_x);
20066 }
20067 }
20068 /* Even if this display element produced no glyphs at all,
20069 we want to record its position. */
20070 if (it->bidi_p && nglyphs == 0)
20071 RECORD_MAX_MIN_POS (it);
20072
20073 row->ascent = max (row->ascent, it->max_ascent);
20074 row->height = max (row->height, it->max_ascent + it->max_descent);
20075 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20076 row->phys_height = max (row->phys_height,
20077 it->max_phys_ascent + it->max_phys_descent);
20078 row->extra_line_spacing = max (row->extra_line_spacing,
20079 it->max_extra_line_spacing);
20080
20081 /* End of this display line if row is continued. */
20082 if (row->continued_p || row->ends_at_zv_p)
20083 break;
20084 }
20085
20086 at_end_of_line:
20087 /* Is this a line end? If yes, we're also done, after making
20088 sure that a non-default face is extended up to the right
20089 margin of the window. */
20090 if (ITERATOR_AT_END_OF_LINE_P (it))
20091 {
20092 int used_before = row->used[TEXT_AREA];
20093
20094 row->ends_in_newline_from_string_p = STRINGP (it->object);
20095
20096 /* Add a space at the end of the line that is used to
20097 display the cursor there. */
20098 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20099 append_space_for_newline (it, 0);
20100
20101 /* Extend the face to the end of the line. */
20102 extend_face_to_end_of_line (it);
20103
20104 /* Make sure we have the position. */
20105 if (used_before == 0)
20106 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20107
20108 /* Record the position of the newline, for use in
20109 find_row_edges. */
20110 it->eol_pos = it->current.pos;
20111
20112 /* Consume the line end. This skips over invisible lines. */
20113 set_iterator_to_next (it, 1);
20114 it->continuation_lines_width = 0;
20115 break;
20116 }
20117
20118 /* Proceed with next display element. Note that this skips
20119 over lines invisible because of selective display. */
20120 set_iterator_to_next (it, 1);
20121
20122 /* If we truncate lines, we are done when the last displayed
20123 glyphs reach past the right margin of the window. */
20124 if (it->line_wrap == TRUNCATE
20125 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20126 ? (it->current_x >= it->last_visible_x)
20127 : (it->current_x > it->last_visible_x)))
20128 {
20129 /* Maybe add truncation glyphs. */
20130 if (!FRAME_WINDOW_P (it->f)
20131 || (row->reversed_p
20132 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20133 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20134 {
20135 int i, n;
20136
20137 if (!row->reversed_p)
20138 {
20139 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20140 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20141 break;
20142 }
20143 else
20144 {
20145 for (i = 0; i < row->used[TEXT_AREA]; i++)
20146 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20147 break;
20148 /* Remove any padding glyphs at the front of ROW, to
20149 make room for the truncation glyphs we will be
20150 adding below. The loop below always inserts at
20151 least one truncation glyph, so also remove the
20152 last glyph added to ROW. */
20153 unproduce_glyphs (it, i + 1);
20154 /* Adjust i for the loop below. */
20155 i = row->used[TEXT_AREA] - (i + 1);
20156 }
20157
20158 it->current_x = x_before;
20159 if (!FRAME_WINDOW_P (it->f))
20160 {
20161 for (n = row->used[TEXT_AREA]; i < n; ++i)
20162 {
20163 row->used[TEXT_AREA] = i;
20164 produce_special_glyphs (it, IT_TRUNCATION);
20165 }
20166 }
20167 else
20168 {
20169 row->used[TEXT_AREA] = i;
20170 produce_special_glyphs (it, IT_TRUNCATION);
20171 }
20172 }
20173 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20174 {
20175 /* Don't truncate if we can overflow newline into fringe. */
20176 if (!get_next_display_element (it))
20177 {
20178 it->continuation_lines_width = 0;
20179 row->ends_at_zv_p = 1;
20180 row->exact_window_width_line_p = 1;
20181 break;
20182 }
20183 if (ITERATOR_AT_END_OF_LINE_P (it))
20184 {
20185 row->exact_window_width_line_p = 1;
20186 goto at_end_of_line;
20187 }
20188 it->current_x = x_before;
20189 }
20190
20191 row->truncated_on_right_p = 1;
20192 it->continuation_lines_width = 0;
20193 reseat_at_next_visible_line_start (it, 0);
20194 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20195 it->hpos = hpos_before;
20196 break;
20197 }
20198 }
20199
20200 if (wrap_data)
20201 bidi_unshelve_cache (wrap_data, 1);
20202
20203 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20204 at the left window margin. */
20205 if (it->first_visible_x
20206 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20207 {
20208 if (!FRAME_WINDOW_P (it->f)
20209 || (row->reversed_p
20210 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20211 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20212 insert_left_trunc_glyphs (it);
20213 row->truncated_on_left_p = 1;
20214 }
20215
20216 /* Remember the position at which this line ends.
20217
20218 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20219 cannot be before the call to find_row_edges below, since that is
20220 where these positions are determined. */
20221 row->end = it->current;
20222 if (!it->bidi_p)
20223 {
20224 row->minpos = row->start.pos;
20225 row->maxpos = row->end.pos;
20226 }
20227 else
20228 {
20229 /* ROW->minpos and ROW->maxpos must be the smallest and
20230 `1 + the largest' buffer positions in ROW. But if ROW was
20231 bidi-reordered, these two positions can be anywhere in the
20232 row, so we must determine them now. */
20233 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20234 }
20235
20236 /* If the start of this line is the overlay arrow-position, then
20237 mark this glyph row as the one containing the overlay arrow.
20238 This is clearly a mess with variable size fonts. It would be
20239 better to let it be displayed like cursors under X. */
20240 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20241 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20242 !NILP (overlay_arrow_string)))
20243 {
20244 /* Overlay arrow in window redisplay is a fringe bitmap. */
20245 if (STRINGP (overlay_arrow_string))
20246 {
20247 struct glyph_row *arrow_row
20248 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20249 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20250 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20251 struct glyph *p = row->glyphs[TEXT_AREA];
20252 struct glyph *p2, *end;
20253
20254 /* Copy the arrow glyphs. */
20255 while (glyph < arrow_end)
20256 *p++ = *glyph++;
20257
20258 /* Throw away padding glyphs. */
20259 p2 = p;
20260 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20261 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20262 ++p2;
20263 if (p2 > p)
20264 {
20265 while (p2 < end)
20266 *p++ = *p2++;
20267 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20268 }
20269 }
20270 else
20271 {
20272 eassert (INTEGERP (overlay_arrow_string));
20273 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20274 }
20275 overlay_arrow_seen = 1;
20276 }
20277
20278 /* Highlight trailing whitespace. */
20279 if (!NILP (Vshow_trailing_whitespace))
20280 highlight_trailing_whitespace (it->f, it->glyph_row);
20281
20282 /* Compute pixel dimensions of this line. */
20283 compute_line_metrics (it);
20284
20285 /* Implementation note: No changes in the glyphs of ROW or in their
20286 faces can be done past this point, because compute_line_metrics
20287 computes ROW's hash value and stores it within the glyph_row
20288 structure. */
20289
20290 /* Record whether this row ends inside an ellipsis. */
20291 row->ends_in_ellipsis_p
20292 = (it->method == GET_FROM_DISPLAY_VECTOR
20293 && it->ellipsis_p);
20294
20295 /* Save fringe bitmaps in this row. */
20296 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20297 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20298 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20299 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20300
20301 it->left_user_fringe_bitmap = 0;
20302 it->left_user_fringe_face_id = 0;
20303 it->right_user_fringe_bitmap = 0;
20304 it->right_user_fringe_face_id = 0;
20305
20306 /* Maybe set the cursor. */
20307 cvpos = it->w->cursor.vpos;
20308 if ((cvpos < 0
20309 /* In bidi-reordered rows, keep checking for proper cursor
20310 position even if one has been found already, because buffer
20311 positions in such rows change non-linearly with ROW->VPOS,
20312 when a line is continued. One exception: when we are at ZV,
20313 display cursor on the first suitable glyph row, since all
20314 the empty rows after that also have their position set to ZV. */
20315 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20316 lines' rows is implemented for bidi-reordered rows. */
20317 || (it->bidi_p
20318 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20319 && PT >= MATRIX_ROW_START_CHARPOS (row)
20320 && PT <= MATRIX_ROW_END_CHARPOS (row)
20321 && cursor_row_p (row))
20322 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20323
20324 /* Prepare for the next line. This line starts horizontally at (X
20325 HPOS) = (0 0). Vertical positions are incremented. As a
20326 convenience for the caller, IT->glyph_row is set to the next
20327 row to be used. */
20328 it->current_x = it->hpos = 0;
20329 it->current_y += row->height;
20330 SET_TEXT_POS (it->eol_pos, 0, 0);
20331 ++it->vpos;
20332 ++it->glyph_row;
20333 /* The next row should by default use the same value of the
20334 reversed_p flag as this one. set_iterator_to_next decides when
20335 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20336 the flag accordingly. */
20337 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20338 it->glyph_row->reversed_p = row->reversed_p;
20339 it->start = row->end;
20340 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20341
20342 #undef RECORD_MAX_MIN_POS
20343 }
20344
20345 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20346 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20347 doc: /* Return paragraph direction at point in BUFFER.
20348 Value is either `left-to-right' or `right-to-left'.
20349 If BUFFER is omitted or nil, it defaults to the current buffer.
20350
20351 Paragraph direction determines how the text in the paragraph is displayed.
20352 In left-to-right paragraphs, text begins at the left margin of the window
20353 and the reading direction is generally left to right. In right-to-left
20354 paragraphs, text begins at the right margin and is read from right to left.
20355
20356 See also `bidi-paragraph-direction'. */)
20357 (Lisp_Object buffer)
20358 {
20359 struct buffer *buf = current_buffer;
20360 struct buffer *old = buf;
20361
20362 if (! NILP (buffer))
20363 {
20364 CHECK_BUFFER (buffer);
20365 buf = XBUFFER (buffer);
20366 }
20367
20368 if (NILP (BVAR (buf, bidi_display_reordering))
20369 || NILP (BVAR (buf, enable_multibyte_characters))
20370 /* When we are loading loadup.el, the character property tables
20371 needed for bidi iteration are not yet available. */
20372 || !NILP (Vpurify_flag))
20373 return Qleft_to_right;
20374 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20375 return BVAR (buf, bidi_paragraph_direction);
20376 else
20377 {
20378 /* Determine the direction from buffer text. We could try to
20379 use current_matrix if it is up to date, but this seems fast
20380 enough as it is. */
20381 struct bidi_it itb;
20382 ptrdiff_t pos = BUF_PT (buf);
20383 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20384 int c;
20385 void *itb_data = bidi_shelve_cache ();
20386
20387 set_buffer_temp (buf);
20388 /* bidi_paragraph_init finds the base direction of the paragraph
20389 by searching forward from paragraph start. We need the base
20390 direction of the current or _previous_ paragraph, so we need
20391 to make sure we are within that paragraph. To that end, find
20392 the previous non-empty line. */
20393 if (pos >= ZV && pos > BEGV)
20394 DEC_BOTH (pos, bytepos);
20395 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20396 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20397 {
20398 while ((c = FETCH_BYTE (bytepos)) == '\n'
20399 || c == ' ' || c == '\t' || c == '\f')
20400 {
20401 if (bytepos <= BEGV_BYTE)
20402 break;
20403 bytepos--;
20404 pos--;
20405 }
20406 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20407 bytepos--;
20408 }
20409 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20410 itb.paragraph_dir = NEUTRAL_DIR;
20411 itb.string.s = NULL;
20412 itb.string.lstring = Qnil;
20413 itb.string.bufpos = 0;
20414 itb.string.unibyte = 0;
20415 /* We have no window to use here for ignoring window-specific
20416 overlays. Using NULL for window pointer will cause
20417 compute_display_string_pos to use the current buffer. */
20418 itb.w = NULL;
20419 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20420 bidi_unshelve_cache (itb_data, 0);
20421 set_buffer_temp (old);
20422 switch (itb.paragraph_dir)
20423 {
20424 case L2R:
20425 return Qleft_to_right;
20426 break;
20427 case R2L:
20428 return Qright_to_left;
20429 break;
20430 default:
20431 emacs_abort ();
20432 }
20433 }
20434 }
20435
20436 DEFUN ("move-point-visually", Fmove_point_visually,
20437 Smove_point_visually, 1, 1, 0,
20438 doc: /* Move point in the visual order in the specified DIRECTION.
20439 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20440 left.
20441
20442 Value is the new character position of point. */)
20443 (Lisp_Object direction)
20444 {
20445 struct window *w = XWINDOW (selected_window);
20446 struct buffer *b = XBUFFER (w->contents);
20447 struct glyph_row *row;
20448 int dir;
20449 Lisp_Object paragraph_dir;
20450
20451 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20452 (!(ROW)->continued_p \
20453 && INTEGERP ((GLYPH)->object) \
20454 && (GLYPH)->type == CHAR_GLYPH \
20455 && (GLYPH)->u.ch == ' ' \
20456 && (GLYPH)->charpos >= 0 \
20457 && !(GLYPH)->avoid_cursor_p)
20458
20459 CHECK_NUMBER (direction);
20460 dir = XINT (direction);
20461 if (dir > 0)
20462 dir = 1;
20463 else
20464 dir = -1;
20465
20466 /* If current matrix is up-to-date, we can use the information
20467 recorded in the glyphs, at least as long as the goal is on the
20468 screen. */
20469 if (w->window_end_valid
20470 && !windows_or_buffers_changed
20471 && b
20472 && !b->clip_changed
20473 && !b->prevent_redisplay_optimizations_p
20474 && !window_outdated (w)
20475 && w->cursor.vpos >= 0
20476 && w->cursor.vpos < w->current_matrix->nrows
20477 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20478 {
20479 struct glyph *g = row->glyphs[TEXT_AREA];
20480 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20481 struct glyph *gpt = g + w->cursor.hpos;
20482
20483 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20484 {
20485 if (BUFFERP (g->object) && g->charpos != PT)
20486 {
20487 SET_PT (g->charpos);
20488 w->cursor.vpos = -1;
20489 return make_number (PT);
20490 }
20491 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20492 {
20493 ptrdiff_t new_pos;
20494
20495 if (BUFFERP (gpt->object))
20496 {
20497 new_pos = PT;
20498 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20499 new_pos += (row->reversed_p ? -dir : dir);
20500 else
20501 new_pos -= (row->reversed_p ? -dir : dir);;
20502 }
20503 else if (BUFFERP (g->object))
20504 new_pos = g->charpos;
20505 else
20506 break;
20507 SET_PT (new_pos);
20508 w->cursor.vpos = -1;
20509 return make_number (PT);
20510 }
20511 else if (ROW_GLYPH_NEWLINE_P (row, g))
20512 {
20513 /* Glyphs inserted at the end of a non-empty line for
20514 positioning the cursor have zero charpos, so we must
20515 deduce the value of point by other means. */
20516 if (g->charpos > 0)
20517 SET_PT (g->charpos);
20518 else if (row->ends_at_zv_p && PT != ZV)
20519 SET_PT (ZV);
20520 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20521 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20522 else
20523 break;
20524 w->cursor.vpos = -1;
20525 return make_number (PT);
20526 }
20527 }
20528 if (g == e || INTEGERP (g->object))
20529 {
20530 if (row->truncated_on_left_p || row->truncated_on_right_p)
20531 goto simulate_display;
20532 if (!row->reversed_p)
20533 row += dir;
20534 else
20535 row -= dir;
20536 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20537 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20538 goto simulate_display;
20539
20540 if (dir > 0)
20541 {
20542 if (row->reversed_p && !row->continued_p)
20543 {
20544 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20545 w->cursor.vpos = -1;
20546 return make_number (PT);
20547 }
20548 g = row->glyphs[TEXT_AREA];
20549 e = g + row->used[TEXT_AREA];
20550 for ( ; g < e; g++)
20551 {
20552 if (BUFFERP (g->object)
20553 /* Empty lines have only one glyph, which stands
20554 for the newline, and whose charpos is the
20555 buffer position of the newline. */
20556 || ROW_GLYPH_NEWLINE_P (row, g)
20557 /* When the buffer ends in a newline, the line at
20558 EOB also has one glyph, but its charpos is -1. */
20559 || (row->ends_at_zv_p
20560 && !row->reversed_p
20561 && INTEGERP (g->object)
20562 && g->type == CHAR_GLYPH
20563 && g->u.ch == ' '))
20564 {
20565 if (g->charpos > 0)
20566 SET_PT (g->charpos);
20567 else if (!row->reversed_p
20568 && row->ends_at_zv_p
20569 && PT != ZV)
20570 SET_PT (ZV);
20571 else
20572 continue;
20573 w->cursor.vpos = -1;
20574 return make_number (PT);
20575 }
20576 }
20577 }
20578 else
20579 {
20580 if (!row->reversed_p && !row->continued_p)
20581 {
20582 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20583 w->cursor.vpos = -1;
20584 return make_number (PT);
20585 }
20586 e = row->glyphs[TEXT_AREA];
20587 g = e + row->used[TEXT_AREA] - 1;
20588 for ( ; g >= e; g--)
20589 {
20590 if (BUFFERP (g->object)
20591 || (ROW_GLYPH_NEWLINE_P (row, g)
20592 && g->charpos > 0)
20593 /* Empty R2L lines on GUI frames have the buffer
20594 position of the newline stored in the stretch
20595 glyph. */
20596 || g->type == STRETCH_GLYPH
20597 || (row->ends_at_zv_p
20598 && row->reversed_p
20599 && INTEGERP (g->object)
20600 && g->type == CHAR_GLYPH
20601 && g->u.ch == ' '))
20602 {
20603 if (g->charpos > 0)
20604 SET_PT (g->charpos);
20605 else if (row->reversed_p
20606 && row->ends_at_zv_p
20607 && PT != ZV)
20608 SET_PT (ZV);
20609 else
20610 continue;
20611 w->cursor.vpos = -1;
20612 return make_number (PT);
20613 }
20614 }
20615 }
20616 }
20617 }
20618
20619 simulate_display:
20620
20621 /* If we wind up here, we failed to move by using the glyphs, so we
20622 need to simulate display instead. */
20623
20624 if (b)
20625 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20626 else
20627 paragraph_dir = Qleft_to_right;
20628 if (EQ (paragraph_dir, Qright_to_left))
20629 dir = -dir;
20630 if (PT <= BEGV && dir < 0)
20631 xsignal0 (Qbeginning_of_buffer);
20632 else if (PT >= ZV && dir > 0)
20633 xsignal0 (Qend_of_buffer);
20634 else
20635 {
20636 struct text_pos pt;
20637 struct it it;
20638 int pt_x, target_x, pixel_width, pt_vpos;
20639 bool at_eol_p;
20640 bool overshoot_expected = false;
20641 bool target_is_eol_p = false;
20642
20643 /* Setup the arena. */
20644 SET_TEXT_POS (pt, PT, PT_BYTE);
20645 start_display (&it, w, pt);
20646
20647 if (it.cmp_it.id < 0
20648 && it.method == GET_FROM_STRING
20649 && it.area == TEXT_AREA
20650 && it.string_from_display_prop_p
20651 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20652 overshoot_expected = true;
20653
20654 /* Find the X coordinate of point. We start from the beginning
20655 of this or previous line to make sure we are before point in
20656 the logical order (since the move_it_* functions can only
20657 move forward). */
20658 reseat:
20659 reseat_at_previous_visible_line_start (&it);
20660 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20661 if (IT_CHARPOS (it) != PT)
20662 {
20663 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20664 -1, -1, -1, MOVE_TO_POS);
20665 /* If we missed point because the character there is
20666 displayed out of a display vector that has more than one
20667 glyph, retry expecting overshoot. */
20668 if (it.method == GET_FROM_DISPLAY_VECTOR
20669 && it.current.dpvec_index > 0
20670 && !overshoot_expected)
20671 {
20672 overshoot_expected = true;
20673 goto reseat;
20674 }
20675 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20676 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20677 }
20678 pt_x = it.current_x;
20679 pt_vpos = it.vpos;
20680 if (dir > 0 || overshoot_expected)
20681 {
20682 struct glyph_row *row = it.glyph_row;
20683
20684 /* When point is at beginning of line, we don't have
20685 information about the glyph there loaded into struct
20686 it. Calling get_next_display_element fixes that. */
20687 if (pt_x == 0)
20688 get_next_display_element (&it);
20689 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20690 it.glyph_row = NULL;
20691 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20692 it.glyph_row = row;
20693 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20694 it, lest it will become out of sync with it's buffer
20695 position. */
20696 it.current_x = pt_x;
20697 }
20698 else
20699 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20700 pixel_width = it.pixel_width;
20701 if (overshoot_expected && at_eol_p)
20702 pixel_width = 0;
20703 else if (pixel_width <= 0)
20704 pixel_width = 1;
20705
20706 /* If there's a display string (or something similar) at point,
20707 we are actually at the glyph to the left of point, so we need
20708 to correct the X coordinate. */
20709 if (overshoot_expected)
20710 {
20711 if (it.bidi_p)
20712 pt_x += pixel_width * it.bidi_it.scan_dir;
20713 else
20714 pt_x += pixel_width;
20715 }
20716
20717 /* Compute target X coordinate, either to the left or to the
20718 right of point. On TTY frames, all characters have the same
20719 pixel width of 1, so we can use that. On GUI frames we don't
20720 have an easy way of getting at the pixel width of the
20721 character to the left of point, so we use a different method
20722 of getting to that place. */
20723 if (dir > 0)
20724 target_x = pt_x + pixel_width;
20725 else
20726 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20727
20728 /* Target X coordinate could be one line above or below the line
20729 of point, in which case we need to adjust the target X
20730 coordinate. Also, if moving to the left, we need to begin at
20731 the left edge of the point's screen line. */
20732 if (dir < 0)
20733 {
20734 if (pt_x > 0)
20735 {
20736 start_display (&it, w, pt);
20737 reseat_at_previous_visible_line_start (&it);
20738 it.current_x = it.current_y = it.hpos = 0;
20739 if (pt_vpos != 0)
20740 move_it_by_lines (&it, pt_vpos);
20741 }
20742 else
20743 {
20744 move_it_by_lines (&it, -1);
20745 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20746 target_is_eol_p = true;
20747 }
20748 }
20749 else
20750 {
20751 if (at_eol_p
20752 || (target_x >= it.last_visible_x
20753 && it.line_wrap != TRUNCATE))
20754 {
20755 if (pt_x > 0)
20756 move_it_by_lines (&it, 0);
20757 move_it_by_lines (&it, 1);
20758 target_x = 0;
20759 }
20760 }
20761
20762 /* Move to the target X coordinate. */
20763 #ifdef HAVE_WINDOW_SYSTEM
20764 /* On GUI frames, as we don't know the X coordinate of the
20765 character to the left of point, moving point to the left
20766 requires walking, one grapheme cluster at a time, until we
20767 find ourself at a place immediately to the left of the
20768 character at point. */
20769 if (FRAME_WINDOW_P (it.f) && dir < 0)
20770 {
20771 struct text_pos new_pos;
20772 enum move_it_result rc = MOVE_X_REACHED;
20773
20774 if (it.current_x == 0)
20775 get_next_display_element (&it);
20776 if (it.what == IT_COMPOSITION)
20777 {
20778 new_pos.charpos = it.cmp_it.charpos;
20779 new_pos.bytepos = -1;
20780 }
20781 else
20782 new_pos = it.current.pos;
20783
20784 while (it.current_x + it.pixel_width <= target_x
20785 && rc == MOVE_X_REACHED)
20786 {
20787 int new_x = it.current_x + it.pixel_width;
20788
20789 /* For composed characters, we want the position of the
20790 first character in the grapheme cluster (usually, the
20791 composition's base character), whereas it.current
20792 might give us the position of the _last_ one, e.g. if
20793 the composition is rendered in reverse due to bidi
20794 reordering. */
20795 if (it.what == IT_COMPOSITION)
20796 {
20797 new_pos.charpos = it.cmp_it.charpos;
20798 new_pos.bytepos = -1;
20799 }
20800 else
20801 new_pos = it.current.pos;
20802 if (new_x == it.current_x)
20803 new_x++;
20804 rc = move_it_in_display_line_to (&it, ZV, new_x,
20805 MOVE_TO_POS | MOVE_TO_X);
20806 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20807 break;
20808 }
20809 /* The previous position we saw in the loop is the one we
20810 want. */
20811 if (new_pos.bytepos == -1)
20812 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20813 it.current.pos = new_pos;
20814 }
20815 else
20816 #endif
20817 if (it.current_x != target_x)
20818 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20819
20820 /* When lines are truncated, the above loop will stop at the
20821 window edge. But we want to get to the end of line, even if
20822 it is beyond the window edge; automatic hscroll will then
20823 scroll the window to show point as appropriate. */
20824 if (target_is_eol_p && it.line_wrap == TRUNCATE
20825 && get_next_display_element (&it))
20826 {
20827 struct text_pos new_pos = it.current.pos;
20828
20829 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20830 {
20831 set_iterator_to_next (&it, 0);
20832 if (it.method == GET_FROM_BUFFER)
20833 new_pos = it.current.pos;
20834 if (!get_next_display_element (&it))
20835 break;
20836 }
20837
20838 it.current.pos = new_pos;
20839 }
20840
20841 /* If we ended up in a display string that covers point, move to
20842 buffer position to the right in the visual order. */
20843 if (dir > 0)
20844 {
20845 while (IT_CHARPOS (it) == PT)
20846 {
20847 set_iterator_to_next (&it, 0);
20848 if (!get_next_display_element (&it))
20849 break;
20850 }
20851 }
20852
20853 /* Move point to that position. */
20854 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20855 }
20856
20857 return make_number (PT);
20858
20859 #undef ROW_GLYPH_NEWLINE_P
20860 }
20861
20862 \f
20863 /***********************************************************************
20864 Menu Bar
20865 ***********************************************************************/
20866
20867 /* Redisplay the menu bar in the frame for window W.
20868
20869 The menu bar of X frames that don't have X toolkit support is
20870 displayed in a special window W->frame->menu_bar_window.
20871
20872 The menu bar of terminal frames is treated specially as far as
20873 glyph matrices are concerned. Menu bar lines are not part of
20874 windows, so the update is done directly on the frame matrix rows
20875 for the menu bar. */
20876
20877 static void
20878 display_menu_bar (struct window *w)
20879 {
20880 struct frame *f = XFRAME (WINDOW_FRAME (w));
20881 struct it it;
20882 Lisp_Object items;
20883 int i;
20884
20885 /* Don't do all this for graphical frames. */
20886 #ifdef HAVE_NTGUI
20887 if (FRAME_W32_P (f))
20888 return;
20889 #endif
20890 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20891 if (FRAME_X_P (f))
20892 return;
20893 #endif
20894
20895 #ifdef HAVE_NS
20896 if (FRAME_NS_P (f))
20897 return;
20898 #endif /* HAVE_NS */
20899
20900 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20901 eassert (!FRAME_WINDOW_P (f));
20902 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20903 it.first_visible_x = 0;
20904 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20905 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20906 if (FRAME_WINDOW_P (f))
20907 {
20908 /* Menu bar lines are displayed in the desired matrix of the
20909 dummy window menu_bar_window. */
20910 struct window *menu_w;
20911 menu_w = XWINDOW (f->menu_bar_window);
20912 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20913 MENU_FACE_ID);
20914 it.first_visible_x = 0;
20915 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20916 }
20917 else
20918 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20919 {
20920 /* This is a TTY frame, i.e. character hpos/vpos are used as
20921 pixel x/y. */
20922 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20923 MENU_FACE_ID);
20924 it.first_visible_x = 0;
20925 it.last_visible_x = FRAME_COLS (f);
20926 }
20927
20928 /* FIXME: This should be controlled by a user option. See the
20929 comments in redisplay_tool_bar and display_mode_line about
20930 this. */
20931 it.paragraph_embedding = L2R;
20932
20933 /* Clear all rows of the menu bar. */
20934 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20935 {
20936 struct glyph_row *row = it.glyph_row + i;
20937 clear_glyph_row (row);
20938 row->enabled_p = true;
20939 row->full_width_p = 1;
20940 }
20941
20942 /* Display all items of the menu bar. */
20943 items = FRAME_MENU_BAR_ITEMS (it.f);
20944 for (i = 0; i < ASIZE (items); i += 4)
20945 {
20946 Lisp_Object string;
20947
20948 /* Stop at nil string. */
20949 string = AREF (items, i + 1);
20950 if (NILP (string))
20951 break;
20952
20953 /* Remember where item was displayed. */
20954 ASET (items, i + 3, make_number (it.hpos));
20955
20956 /* Display the item, pad with one space. */
20957 if (it.current_x < it.last_visible_x)
20958 display_string (NULL, string, Qnil, 0, 0, &it,
20959 SCHARS (string) + 1, 0, 0, -1);
20960 }
20961
20962 /* Fill out the line with spaces. */
20963 if (it.current_x < it.last_visible_x)
20964 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20965
20966 /* Compute the total height of the lines. */
20967 compute_line_metrics (&it);
20968 }
20969
20970 /* Deep copy of a glyph row, including the glyphs. */
20971 static void
20972 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20973 {
20974 struct glyph *pointers[1 + LAST_AREA];
20975 int to_used = to->used[TEXT_AREA];
20976
20977 /* Save glyph pointers of TO. */
20978 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20979
20980 /* Do a structure assignment. */
20981 *to = *from;
20982
20983 /* Restore original glyph pointers of TO. */
20984 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20985
20986 /* Copy the glyphs. */
20987 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20988 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20989
20990 /* If we filled only part of the TO row, fill the rest with
20991 space_glyph (which will display as empty space). */
20992 if (to_used > from->used[TEXT_AREA])
20993 fill_up_frame_row_with_spaces (to, to_used);
20994 }
20995
20996 /* Display one menu item on a TTY, by overwriting the glyphs in the
20997 frame F's desired glyph matrix with glyphs produced from the menu
20998 item text. Called from term.c to display TTY drop-down menus one
20999 item at a time.
21000
21001 ITEM_TEXT is the menu item text as a C string.
21002
21003 FACE_ID is the face ID to be used for this menu item. FACE_ID
21004 could specify one of 3 faces: a face for an enabled item, a face
21005 for a disabled item, or a face for a selected item.
21006
21007 X and Y are coordinates of the first glyph in the frame's desired
21008 matrix to be overwritten by the menu item. Since this is a TTY, Y
21009 is the zero-based number of the glyph row and X is the zero-based
21010 glyph number in the row, starting from left, where to start
21011 displaying the item.
21012
21013 SUBMENU non-zero means this menu item drops down a submenu, which
21014 should be indicated by displaying a proper visual cue after the
21015 item text. */
21016
21017 void
21018 display_tty_menu_item (const char *item_text, int width, int face_id,
21019 int x, int y, int submenu)
21020 {
21021 struct it it;
21022 struct frame *f = SELECTED_FRAME ();
21023 struct window *w = XWINDOW (f->selected_window);
21024 int saved_used, saved_truncated, saved_width, saved_reversed;
21025 struct glyph_row *row;
21026 size_t item_len = strlen (item_text);
21027
21028 eassert (FRAME_TERMCAP_P (f));
21029
21030 /* Don't write beyond the matrix's last row. This can happen for
21031 TTY screens that are not high enough to show the entire menu.
21032 (This is actually a bit of defensive programming, as
21033 tty_menu_display already limits the number of menu items to one
21034 less than the number of screen lines.) */
21035 if (y >= f->desired_matrix->nrows)
21036 return;
21037
21038 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21039 it.first_visible_x = 0;
21040 it.last_visible_x = FRAME_COLS (f) - 1;
21041 row = it.glyph_row;
21042 /* Start with the row contents from the current matrix. */
21043 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21044 saved_width = row->full_width_p;
21045 row->full_width_p = 1;
21046 saved_reversed = row->reversed_p;
21047 row->reversed_p = 0;
21048 row->enabled_p = true;
21049
21050 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21051 desired face. */
21052 eassert (x < f->desired_matrix->matrix_w);
21053 it.current_x = it.hpos = x;
21054 it.current_y = it.vpos = y;
21055 saved_used = row->used[TEXT_AREA];
21056 saved_truncated = row->truncated_on_right_p;
21057 row->used[TEXT_AREA] = x;
21058 it.face_id = face_id;
21059 it.line_wrap = TRUNCATE;
21060
21061 /* FIXME: This should be controlled by a user option. See the
21062 comments in redisplay_tool_bar and display_mode_line about this.
21063 Also, if paragraph_embedding could ever be R2L, changes will be
21064 needed to avoid shifting to the right the row characters in
21065 term.c:append_glyph. */
21066 it.paragraph_embedding = L2R;
21067
21068 /* Pad with a space on the left. */
21069 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21070 width--;
21071 /* Display the menu item, pad with spaces to WIDTH. */
21072 if (submenu)
21073 {
21074 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21075 item_len, 0, FRAME_COLS (f) - 1, -1);
21076 width -= item_len;
21077 /* Indicate with " >" that there's a submenu. */
21078 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21079 FRAME_COLS (f) - 1, -1);
21080 }
21081 else
21082 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21083 width, 0, FRAME_COLS (f) - 1, -1);
21084
21085 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21086 row->truncated_on_right_p = saved_truncated;
21087 row->hash = row_hash (row);
21088 row->full_width_p = saved_width;
21089 row->reversed_p = saved_reversed;
21090 }
21091 \f
21092 /***********************************************************************
21093 Mode Line
21094 ***********************************************************************/
21095
21096 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21097 FORCE is non-zero, redisplay mode lines unconditionally.
21098 Otherwise, redisplay only mode lines that are garbaged. Value is
21099 the number of windows whose mode lines were redisplayed. */
21100
21101 static int
21102 redisplay_mode_lines (Lisp_Object window, bool force)
21103 {
21104 int nwindows = 0;
21105
21106 while (!NILP (window))
21107 {
21108 struct window *w = XWINDOW (window);
21109
21110 if (WINDOWP (w->contents))
21111 nwindows += redisplay_mode_lines (w->contents, force);
21112 else if (force
21113 || FRAME_GARBAGED_P (XFRAME (w->frame))
21114 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21115 {
21116 struct text_pos lpoint;
21117 struct buffer *old = current_buffer;
21118
21119 /* Set the window's buffer for the mode line display. */
21120 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21121 set_buffer_internal_1 (XBUFFER (w->contents));
21122
21123 /* Point refers normally to the selected window. For any
21124 other window, set up appropriate value. */
21125 if (!EQ (window, selected_window))
21126 {
21127 struct text_pos pt;
21128
21129 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21130 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21131 }
21132
21133 /* Display mode lines. */
21134 clear_glyph_matrix (w->desired_matrix);
21135 if (display_mode_lines (w))
21136 ++nwindows;
21137
21138 /* Restore old settings. */
21139 set_buffer_internal_1 (old);
21140 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21141 }
21142
21143 window = w->next;
21144 }
21145
21146 return nwindows;
21147 }
21148
21149
21150 /* Display the mode and/or header line of window W. Value is the
21151 sum number of mode lines and header lines displayed. */
21152
21153 static int
21154 display_mode_lines (struct window *w)
21155 {
21156 Lisp_Object old_selected_window = selected_window;
21157 Lisp_Object old_selected_frame = selected_frame;
21158 Lisp_Object new_frame = w->frame;
21159 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21160 int n = 0;
21161
21162 selected_frame = new_frame;
21163 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21164 or window's point, then we'd need select_window_1 here as well. */
21165 XSETWINDOW (selected_window, w);
21166 XFRAME (new_frame)->selected_window = selected_window;
21167
21168 /* These will be set while the mode line specs are processed. */
21169 line_number_displayed = 0;
21170 w->column_number_displayed = -1;
21171
21172 if (WINDOW_WANTS_MODELINE_P (w))
21173 {
21174 struct window *sel_w = XWINDOW (old_selected_window);
21175
21176 /* Select mode line face based on the real selected window. */
21177 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21178 BVAR (current_buffer, mode_line_format));
21179 ++n;
21180 }
21181
21182 if (WINDOW_WANTS_HEADER_LINE_P (w))
21183 {
21184 display_mode_line (w, HEADER_LINE_FACE_ID,
21185 BVAR (current_buffer, header_line_format));
21186 ++n;
21187 }
21188
21189 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21190 selected_frame = old_selected_frame;
21191 selected_window = old_selected_window;
21192 if (n > 0)
21193 w->must_be_updated_p = true;
21194 return n;
21195 }
21196
21197
21198 /* Display mode or header line of window W. FACE_ID specifies which
21199 line to display; it is either MODE_LINE_FACE_ID or
21200 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21201 display. Value is the pixel height of the mode/header line
21202 displayed. */
21203
21204 static int
21205 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21206 {
21207 struct it it;
21208 struct face *face;
21209 ptrdiff_t count = SPECPDL_INDEX ();
21210
21211 init_iterator (&it, w, -1, -1, NULL, face_id);
21212 /* Don't extend on a previously drawn mode-line.
21213 This may happen if called from pos_visible_p. */
21214 it.glyph_row->enabled_p = false;
21215 prepare_desired_row (it.glyph_row);
21216
21217 it.glyph_row->mode_line_p = 1;
21218
21219 /* FIXME: This should be controlled by a user option. But
21220 supporting such an option is not trivial, since the mode line is
21221 made up of many separate strings. */
21222 it.paragraph_embedding = L2R;
21223
21224 record_unwind_protect (unwind_format_mode_line,
21225 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21226
21227 mode_line_target = MODE_LINE_DISPLAY;
21228
21229 /* Temporarily make frame's keyboard the current kboard so that
21230 kboard-local variables in the mode_line_format will get the right
21231 values. */
21232 push_kboard (FRAME_KBOARD (it.f));
21233 record_unwind_save_match_data ();
21234 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21235 pop_kboard ();
21236
21237 unbind_to (count, Qnil);
21238
21239 /* Fill up with spaces. */
21240 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21241
21242 compute_line_metrics (&it);
21243 it.glyph_row->full_width_p = 1;
21244 it.glyph_row->continued_p = 0;
21245 it.glyph_row->truncated_on_left_p = 0;
21246 it.glyph_row->truncated_on_right_p = 0;
21247
21248 /* Make a 3D mode-line have a shadow at its right end. */
21249 face = FACE_FROM_ID (it.f, face_id);
21250 extend_face_to_end_of_line (&it);
21251 if (face->box != FACE_NO_BOX)
21252 {
21253 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21254 + it.glyph_row->used[TEXT_AREA] - 1);
21255 last->right_box_line_p = 1;
21256 }
21257
21258 return it.glyph_row->height;
21259 }
21260
21261 /* Move element ELT in LIST to the front of LIST.
21262 Return the updated list. */
21263
21264 static Lisp_Object
21265 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21266 {
21267 register Lisp_Object tail, prev;
21268 register Lisp_Object tem;
21269
21270 tail = list;
21271 prev = Qnil;
21272 while (CONSP (tail))
21273 {
21274 tem = XCAR (tail);
21275
21276 if (EQ (elt, tem))
21277 {
21278 /* Splice out the link TAIL. */
21279 if (NILP (prev))
21280 list = XCDR (tail);
21281 else
21282 Fsetcdr (prev, XCDR (tail));
21283
21284 /* Now make it the first. */
21285 Fsetcdr (tail, list);
21286 return tail;
21287 }
21288 else
21289 prev = tail;
21290 tail = XCDR (tail);
21291 QUIT;
21292 }
21293
21294 /* Not found--return unchanged LIST. */
21295 return list;
21296 }
21297
21298 /* Contribute ELT to the mode line for window IT->w. How it
21299 translates into text depends on its data type.
21300
21301 IT describes the display environment in which we display, as usual.
21302
21303 DEPTH is the depth in recursion. It is used to prevent
21304 infinite recursion here.
21305
21306 FIELD_WIDTH is the number of characters the display of ELT should
21307 occupy in the mode line, and PRECISION is the maximum number of
21308 characters to display from ELT's representation. See
21309 display_string for details.
21310
21311 Returns the hpos of the end of the text generated by ELT.
21312
21313 PROPS is a property list to add to any string we encounter.
21314
21315 If RISKY is nonzero, remove (disregard) any properties in any string
21316 we encounter, and ignore :eval and :propertize.
21317
21318 The global variable `mode_line_target' determines whether the
21319 output is passed to `store_mode_line_noprop',
21320 `store_mode_line_string', or `display_string'. */
21321
21322 static int
21323 display_mode_element (struct it *it, int depth, int field_width, int precision,
21324 Lisp_Object elt, Lisp_Object props, int risky)
21325 {
21326 int n = 0, field, prec;
21327 int literal = 0;
21328
21329 tail_recurse:
21330 if (depth > 100)
21331 elt = build_string ("*too-deep*");
21332
21333 depth++;
21334
21335 switch (XTYPE (elt))
21336 {
21337 case Lisp_String:
21338 {
21339 /* A string: output it and check for %-constructs within it. */
21340 unsigned char c;
21341 ptrdiff_t offset = 0;
21342
21343 if (SCHARS (elt) > 0
21344 && (!NILP (props) || risky))
21345 {
21346 Lisp_Object oprops, aelt;
21347 oprops = Ftext_properties_at (make_number (0), elt);
21348
21349 /* If the starting string's properties are not what
21350 we want, translate the string. Also, if the string
21351 is risky, do that anyway. */
21352
21353 if (NILP (Fequal (props, oprops)) || risky)
21354 {
21355 /* If the starting string has properties,
21356 merge the specified ones onto the existing ones. */
21357 if (! NILP (oprops) && !risky)
21358 {
21359 Lisp_Object tem;
21360
21361 oprops = Fcopy_sequence (oprops);
21362 tem = props;
21363 while (CONSP (tem))
21364 {
21365 oprops = Fplist_put (oprops, XCAR (tem),
21366 XCAR (XCDR (tem)));
21367 tem = XCDR (XCDR (tem));
21368 }
21369 props = oprops;
21370 }
21371
21372 aelt = Fassoc (elt, mode_line_proptrans_alist);
21373 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21374 {
21375 /* AELT is what we want. Move it to the front
21376 without consing. */
21377 elt = XCAR (aelt);
21378 mode_line_proptrans_alist
21379 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21380 }
21381 else
21382 {
21383 Lisp_Object tem;
21384
21385 /* If AELT has the wrong props, it is useless.
21386 so get rid of it. */
21387 if (! NILP (aelt))
21388 mode_line_proptrans_alist
21389 = Fdelq (aelt, mode_line_proptrans_alist);
21390
21391 elt = Fcopy_sequence (elt);
21392 Fset_text_properties (make_number (0), Flength (elt),
21393 props, elt);
21394 /* Add this item to mode_line_proptrans_alist. */
21395 mode_line_proptrans_alist
21396 = Fcons (Fcons (elt, props),
21397 mode_line_proptrans_alist);
21398 /* Truncate mode_line_proptrans_alist
21399 to at most 50 elements. */
21400 tem = Fnthcdr (make_number (50),
21401 mode_line_proptrans_alist);
21402 if (! NILP (tem))
21403 XSETCDR (tem, Qnil);
21404 }
21405 }
21406 }
21407
21408 offset = 0;
21409
21410 if (literal)
21411 {
21412 prec = precision - n;
21413 switch (mode_line_target)
21414 {
21415 case MODE_LINE_NOPROP:
21416 case MODE_LINE_TITLE:
21417 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21418 break;
21419 case MODE_LINE_STRING:
21420 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21421 break;
21422 case MODE_LINE_DISPLAY:
21423 n += display_string (NULL, elt, Qnil, 0, 0, it,
21424 0, prec, 0, STRING_MULTIBYTE (elt));
21425 break;
21426 }
21427
21428 break;
21429 }
21430
21431 /* Handle the non-literal case. */
21432
21433 while ((precision <= 0 || n < precision)
21434 && SREF (elt, offset) != 0
21435 && (mode_line_target != MODE_LINE_DISPLAY
21436 || it->current_x < it->last_visible_x))
21437 {
21438 ptrdiff_t last_offset = offset;
21439
21440 /* Advance to end of string or next format specifier. */
21441 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21442 ;
21443
21444 if (offset - 1 != last_offset)
21445 {
21446 ptrdiff_t nchars, nbytes;
21447
21448 /* Output to end of string or up to '%'. Field width
21449 is length of string. Don't output more than
21450 PRECISION allows us. */
21451 offset--;
21452
21453 prec = c_string_width (SDATA (elt) + last_offset,
21454 offset - last_offset, precision - n,
21455 &nchars, &nbytes);
21456
21457 switch (mode_line_target)
21458 {
21459 case MODE_LINE_NOPROP:
21460 case MODE_LINE_TITLE:
21461 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21462 break;
21463 case MODE_LINE_STRING:
21464 {
21465 ptrdiff_t bytepos = last_offset;
21466 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21467 ptrdiff_t endpos = (precision <= 0
21468 ? string_byte_to_char (elt, offset)
21469 : charpos + nchars);
21470
21471 n += store_mode_line_string (NULL,
21472 Fsubstring (elt, make_number (charpos),
21473 make_number (endpos)),
21474 0, 0, 0, Qnil);
21475 }
21476 break;
21477 case MODE_LINE_DISPLAY:
21478 {
21479 ptrdiff_t bytepos = last_offset;
21480 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21481
21482 if (precision <= 0)
21483 nchars = string_byte_to_char (elt, offset) - charpos;
21484 n += display_string (NULL, elt, Qnil, 0, charpos,
21485 it, 0, nchars, 0,
21486 STRING_MULTIBYTE (elt));
21487 }
21488 break;
21489 }
21490 }
21491 else /* c == '%' */
21492 {
21493 ptrdiff_t percent_position = offset;
21494
21495 /* Get the specified minimum width. Zero means
21496 don't pad. */
21497 field = 0;
21498 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21499 field = field * 10 + c - '0';
21500
21501 /* Don't pad beyond the total padding allowed. */
21502 if (field_width - n > 0 && field > field_width - n)
21503 field = field_width - n;
21504
21505 /* Note that either PRECISION <= 0 or N < PRECISION. */
21506 prec = precision - n;
21507
21508 if (c == 'M')
21509 n += display_mode_element (it, depth, field, prec,
21510 Vglobal_mode_string, props,
21511 risky);
21512 else if (c != 0)
21513 {
21514 bool multibyte;
21515 ptrdiff_t bytepos, charpos;
21516 const char *spec;
21517 Lisp_Object string;
21518
21519 bytepos = percent_position;
21520 charpos = (STRING_MULTIBYTE (elt)
21521 ? string_byte_to_char (elt, bytepos)
21522 : bytepos);
21523 spec = decode_mode_spec (it->w, c, field, &string);
21524 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21525
21526 switch (mode_line_target)
21527 {
21528 case MODE_LINE_NOPROP:
21529 case MODE_LINE_TITLE:
21530 n += store_mode_line_noprop (spec, field, prec);
21531 break;
21532 case MODE_LINE_STRING:
21533 {
21534 Lisp_Object tem = build_string (spec);
21535 props = Ftext_properties_at (make_number (charpos), elt);
21536 /* Should only keep face property in props */
21537 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21538 }
21539 break;
21540 case MODE_LINE_DISPLAY:
21541 {
21542 int nglyphs_before, nwritten;
21543
21544 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21545 nwritten = display_string (spec, string, elt,
21546 charpos, 0, it,
21547 field, prec, 0,
21548 multibyte);
21549
21550 /* Assign to the glyphs written above the
21551 string where the `%x' came from, position
21552 of the `%'. */
21553 if (nwritten > 0)
21554 {
21555 struct glyph *glyph
21556 = (it->glyph_row->glyphs[TEXT_AREA]
21557 + nglyphs_before);
21558 int i;
21559
21560 for (i = 0; i < nwritten; ++i)
21561 {
21562 glyph[i].object = elt;
21563 glyph[i].charpos = charpos;
21564 }
21565
21566 n += nwritten;
21567 }
21568 }
21569 break;
21570 }
21571 }
21572 else /* c == 0 */
21573 break;
21574 }
21575 }
21576 }
21577 break;
21578
21579 case Lisp_Symbol:
21580 /* A symbol: process the value of the symbol recursively
21581 as if it appeared here directly. Avoid error if symbol void.
21582 Special case: if value of symbol is a string, output the string
21583 literally. */
21584 {
21585 register Lisp_Object tem;
21586
21587 /* If the variable is not marked as risky to set
21588 then its contents are risky to use. */
21589 if (NILP (Fget (elt, Qrisky_local_variable)))
21590 risky = 1;
21591
21592 tem = Fboundp (elt);
21593 if (!NILP (tem))
21594 {
21595 tem = Fsymbol_value (elt);
21596 /* If value is a string, output that string literally:
21597 don't check for % within it. */
21598 if (STRINGP (tem))
21599 literal = 1;
21600
21601 if (!EQ (tem, elt))
21602 {
21603 /* Give up right away for nil or t. */
21604 elt = tem;
21605 goto tail_recurse;
21606 }
21607 }
21608 }
21609 break;
21610
21611 case Lisp_Cons:
21612 {
21613 register Lisp_Object car, tem;
21614
21615 /* A cons cell: five distinct cases.
21616 If first element is :eval or :propertize, do something special.
21617 If first element is a string or a cons, process all the elements
21618 and effectively concatenate them.
21619 If first element is a negative number, truncate displaying cdr to
21620 at most that many characters. If positive, pad (with spaces)
21621 to at least that many characters.
21622 If first element is a symbol, process the cadr or caddr recursively
21623 according to whether the symbol's value is non-nil or nil. */
21624 car = XCAR (elt);
21625 if (EQ (car, QCeval))
21626 {
21627 /* An element of the form (:eval FORM) means evaluate FORM
21628 and use the result as mode line elements. */
21629
21630 if (risky)
21631 break;
21632
21633 if (CONSP (XCDR (elt)))
21634 {
21635 Lisp_Object spec;
21636 spec = safe_eval (XCAR (XCDR (elt)));
21637 n += display_mode_element (it, depth, field_width - n,
21638 precision - n, spec, props,
21639 risky);
21640 }
21641 }
21642 else if (EQ (car, QCpropertize))
21643 {
21644 /* An element of the form (:propertize ELT PROPS...)
21645 means display ELT but applying properties PROPS. */
21646
21647 if (risky)
21648 break;
21649
21650 if (CONSP (XCDR (elt)))
21651 n += display_mode_element (it, depth, field_width - n,
21652 precision - n, XCAR (XCDR (elt)),
21653 XCDR (XCDR (elt)), risky);
21654 }
21655 else if (SYMBOLP (car))
21656 {
21657 tem = Fboundp (car);
21658 elt = XCDR (elt);
21659 if (!CONSP (elt))
21660 goto invalid;
21661 /* elt is now the cdr, and we know it is a cons cell.
21662 Use its car if CAR has a non-nil value. */
21663 if (!NILP (tem))
21664 {
21665 tem = Fsymbol_value (car);
21666 if (!NILP (tem))
21667 {
21668 elt = XCAR (elt);
21669 goto tail_recurse;
21670 }
21671 }
21672 /* Symbol's value is nil (or symbol is unbound)
21673 Get the cddr of the original list
21674 and if possible find the caddr and use that. */
21675 elt = XCDR (elt);
21676 if (NILP (elt))
21677 break;
21678 else if (!CONSP (elt))
21679 goto invalid;
21680 elt = XCAR (elt);
21681 goto tail_recurse;
21682 }
21683 else if (INTEGERP (car))
21684 {
21685 register int lim = XINT (car);
21686 elt = XCDR (elt);
21687 if (lim < 0)
21688 {
21689 /* Negative int means reduce maximum width. */
21690 if (precision <= 0)
21691 precision = -lim;
21692 else
21693 precision = min (precision, -lim);
21694 }
21695 else if (lim > 0)
21696 {
21697 /* Padding specified. Don't let it be more than
21698 current maximum. */
21699 if (precision > 0)
21700 lim = min (precision, lim);
21701
21702 /* If that's more padding than already wanted, queue it.
21703 But don't reduce padding already specified even if
21704 that is beyond the current truncation point. */
21705 field_width = max (lim, field_width);
21706 }
21707 goto tail_recurse;
21708 }
21709 else if (STRINGP (car) || CONSP (car))
21710 {
21711 Lisp_Object halftail = elt;
21712 int len = 0;
21713
21714 while (CONSP (elt)
21715 && (precision <= 0 || n < precision))
21716 {
21717 n += display_mode_element (it, depth,
21718 /* Do padding only after the last
21719 element in the list. */
21720 (! CONSP (XCDR (elt))
21721 ? field_width - n
21722 : 0),
21723 precision - n, XCAR (elt),
21724 props, risky);
21725 elt = XCDR (elt);
21726 len++;
21727 if ((len & 1) == 0)
21728 halftail = XCDR (halftail);
21729 /* Check for cycle. */
21730 if (EQ (halftail, elt))
21731 break;
21732 }
21733 }
21734 }
21735 break;
21736
21737 default:
21738 invalid:
21739 elt = build_string ("*invalid*");
21740 goto tail_recurse;
21741 }
21742
21743 /* Pad to FIELD_WIDTH. */
21744 if (field_width > 0 && n < field_width)
21745 {
21746 switch (mode_line_target)
21747 {
21748 case MODE_LINE_NOPROP:
21749 case MODE_LINE_TITLE:
21750 n += store_mode_line_noprop ("", field_width - n, 0);
21751 break;
21752 case MODE_LINE_STRING:
21753 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21754 break;
21755 case MODE_LINE_DISPLAY:
21756 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21757 0, 0, 0);
21758 break;
21759 }
21760 }
21761
21762 return n;
21763 }
21764
21765 /* Store a mode-line string element in mode_line_string_list.
21766
21767 If STRING is non-null, display that C string. Otherwise, the Lisp
21768 string LISP_STRING is displayed.
21769
21770 FIELD_WIDTH is the minimum number of output glyphs to produce.
21771 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21772 with spaces. FIELD_WIDTH <= 0 means don't pad.
21773
21774 PRECISION is the maximum number of characters to output from
21775 STRING. PRECISION <= 0 means don't truncate the string.
21776
21777 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21778 properties to the string.
21779
21780 PROPS are the properties to add to the string.
21781 The mode_line_string_face face property is always added to the string.
21782 */
21783
21784 static int
21785 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21786 int field_width, int precision, Lisp_Object props)
21787 {
21788 ptrdiff_t len;
21789 int n = 0;
21790
21791 if (string != NULL)
21792 {
21793 len = strlen (string);
21794 if (precision > 0 && len > precision)
21795 len = precision;
21796 lisp_string = make_string (string, len);
21797 if (NILP (props))
21798 props = mode_line_string_face_prop;
21799 else if (!NILP (mode_line_string_face))
21800 {
21801 Lisp_Object face = Fplist_get (props, Qface);
21802 props = Fcopy_sequence (props);
21803 if (NILP (face))
21804 face = mode_line_string_face;
21805 else
21806 face = list2 (face, mode_line_string_face);
21807 props = Fplist_put (props, Qface, face);
21808 }
21809 Fadd_text_properties (make_number (0), make_number (len),
21810 props, lisp_string);
21811 }
21812 else
21813 {
21814 len = XFASTINT (Flength (lisp_string));
21815 if (precision > 0 && len > precision)
21816 {
21817 len = precision;
21818 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21819 precision = -1;
21820 }
21821 if (!NILP (mode_line_string_face))
21822 {
21823 Lisp_Object face;
21824 if (NILP (props))
21825 props = Ftext_properties_at (make_number (0), lisp_string);
21826 face = Fplist_get (props, Qface);
21827 if (NILP (face))
21828 face = mode_line_string_face;
21829 else
21830 face = list2 (face, mode_line_string_face);
21831 props = list2 (Qface, face);
21832 if (copy_string)
21833 lisp_string = Fcopy_sequence (lisp_string);
21834 }
21835 if (!NILP (props))
21836 Fadd_text_properties (make_number (0), make_number (len),
21837 props, lisp_string);
21838 }
21839
21840 if (len > 0)
21841 {
21842 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21843 n += len;
21844 }
21845
21846 if (field_width > len)
21847 {
21848 field_width -= len;
21849 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21850 if (!NILP (props))
21851 Fadd_text_properties (make_number (0), make_number (field_width),
21852 props, lisp_string);
21853 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21854 n += field_width;
21855 }
21856
21857 return n;
21858 }
21859
21860
21861 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21862 1, 4, 0,
21863 doc: /* Format a string out of a mode line format specification.
21864 First arg FORMAT specifies the mode line format (see `mode-line-format'
21865 for details) to use.
21866
21867 By default, the format is evaluated for the currently selected window.
21868
21869 Optional second arg FACE specifies the face property to put on all
21870 characters for which no face is specified. The value nil means the
21871 default face. The value t means whatever face the window's mode line
21872 currently uses (either `mode-line' or `mode-line-inactive',
21873 depending on whether the window is the selected window or not).
21874 An integer value means the value string has no text
21875 properties.
21876
21877 Optional third and fourth args WINDOW and BUFFER specify the window
21878 and buffer to use as the context for the formatting (defaults
21879 are the selected window and the WINDOW's buffer). */)
21880 (Lisp_Object format, Lisp_Object face,
21881 Lisp_Object window, Lisp_Object buffer)
21882 {
21883 struct it it;
21884 int len;
21885 struct window *w;
21886 struct buffer *old_buffer = NULL;
21887 int face_id;
21888 int no_props = INTEGERP (face);
21889 ptrdiff_t count = SPECPDL_INDEX ();
21890 Lisp_Object str;
21891 int string_start = 0;
21892
21893 w = decode_any_window (window);
21894 XSETWINDOW (window, w);
21895
21896 if (NILP (buffer))
21897 buffer = w->contents;
21898 CHECK_BUFFER (buffer);
21899
21900 /* Make formatting the modeline a non-op when noninteractive, otherwise
21901 there will be problems later caused by a partially initialized frame. */
21902 if (NILP (format) || noninteractive)
21903 return empty_unibyte_string;
21904
21905 if (no_props)
21906 face = Qnil;
21907
21908 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21909 : EQ (face, Qt) ? (EQ (window, selected_window)
21910 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21911 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21912 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21913 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21914 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21915 : DEFAULT_FACE_ID;
21916
21917 old_buffer = current_buffer;
21918
21919 /* Save things including mode_line_proptrans_alist,
21920 and set that to nil so that we don't alter the outer value. */
21921 record_unwind_protect (unwind_format_mode_line,
21922 format_mode_line_unwind_data
21923 (XFRAME (WINDOW_FRAME (w)),
21924 old_buffer, selected_window, 1));
21925 mode_line_proptrans_alist = Qnil;
21926
21927 Fselect_window (window, Qt);
21928 set_buffer_internal_1 (XBUFFER (buffer));
21929
21930 init_iterator (&it, w, -1, -1, NULL, face_id);
21931
21932 if (no_props)
21933 {
21934 mode_line_target = MODE_LINE_NOPROP;
21935 mode_line_string_face_prop = Qnil;
21936 mode_line_string_list = Qnil;
21937 string_start = MODE_LINE_NOPROP_LEN (0);
21938 }
21939 else
21940 {
21941 mode_line_target = MODE_LINE_STRING;
21942 mode_line_string_list = Qnil;
21943 mode_line_string_face = face;
21944 mode_line_string_face_prop
21945 = NILP (face) ? Qnil : list2 (Qface, face);
21946 }
21947
21948 push_kboard (FRAME_KBOARD (it.f));
21949 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21950 pop_kboard ();
21951
21952 if (no_props)
21953 {
21954 len = MODE_LINE_NOPROP_LEN (string_start);
21955 str = make_string (mode_line_noprop_buf + string_start, len);
21956 }
21957 else
21958 {
21959 mode_line_string_list = Fnreverse (mode_line_string_list);
21960 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21961 empty_unibyte_string);
21962 }
21963
21964 unbind_to (count, Qnil);
21965 return str;
21966 }
21967
21968 /* Write a null-terminated, right justified decimal representation of
21969 the positive integer D to BUF using a minimal field width WIDTH. */
21970
21971 static void
21972 pint2str (register char *buf, register int width, register ptrdiff_t d)
21973 {
21974 register char *p = buf;
21975
21976 if (d <= 0)
21977 *p++ = '0';
21978 else
21979 {
21980 while (d > 0)
21981 {
21982 *p++ = d % 10 + '0';
21983 d /= 10;
21984 }
21985 }
21986
21987 for (width -= (int) (p - buf); width > 0; --width)
21988 *p++ = ' ';
21989 *p-- = '\0';
21990 while (p > buf)
21991 {
21992 d = *buf;
21993 *buf++ = *p;
21994 *p-- = d;
21995 }
21996 }
21997
21998 /* Write a null-terminated, right justified decimal and "human
21999 readable" representation of the nonnegative integer D to BUF using
22000 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22001
22002 static const char power_letter[] =
22003 {
22004 0, /* no letter */
22005 'k', /* kilo */
22006 'M', /* mega */
22007 'G', /* giga */
22008 'T', /* tera */
22009 'P', /* peta */
22010 'E', /* exa */
22011 'Z', /* zetta */
22012 'Y' /* yotta */
22013 };
22014
22015 static void
22016 pint2hrstr (char *buf, int width, ptrdiff_t d)
22017 {
22018 /* We aim to represent the nonnegative integer D as
22019 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22020 ptrdiff_t quotient = d;
22021 int remainder = 0;
22022 /* -1 means: do not use TENTHS. */
22023 int tenths = -1;
22024 int exponent = 0;
22025
22026 /* Length of QUOTIENT.TENTHS as a string. */
22027 int length;
22028
22029 char * psuffix;
22030 char * p;
22031
22032 if (quotient >= 1000)
22033 {
22034 /* Scale to the appropriate EXPONENT. */
22035 do
22036 {
22037 remainder = quotient % 1000;
22038 quotient /= 1000;
22039 exponent++;
22040 }
22041 while (quotient >= 1000);
22042
22043 /* Round to nearest and decide whether to use TENTHS or not. */
22044 if (quotient <= 9)
22045 {
22046 tenths = remainder / 100;
22047 if (remainder % 100 >= 50)
22048 {
22049 if (tenths < 9)
22050 tenths++;
22051 else
22052 {
22053 quotient++;
22054 if (quotient == 10)
22055 tenths = -1;
22056 else
22057 tenths = 0;
22058 }
22059 }
22060 }
22061 else
22062 if (remainder >= 500)
22063 {
22064 if (quotient < 999)
22065 quotient++;
22066 else
22067 {
22068 quotient = 1;
22069 exponent++;
22070 tenths = 0;
22071 }
22072 }
22073 }
22074
22075 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22076 if (tenths == -1 && quotient <= 99)
22077 if (quotient <= 9)
22078 length = 1;
22079 else
22080 length = 2;
22081 else
22082 length = 3;
22083 p = psuffix = buf + max (width, length);
22084
22085 /* Print EXPONENT. */
22086 *psuffix++ = power_letter[exponent];
22087 *psuffix = '\0';
22088
22089 /* Print TENTHS. */
22090 if (tenths >= 0)
22091 {
22092 *--p = '0' + tenths;
22093 *--p = '.';
22094 }
22095
22096 /* Print QUOTIENT. */
22097 do
22098 {
22099 int digit = quotient % 10;
22100 *--p = '0' + digit;
22101 }
22102 while ((quotient /= 10) != 0);
22103
22104 /* Print leading spaces. */
22105 while (buf < p)
22106 *--p = ' ';
22107 }
22108
22109 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22110 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22111 type of CODING_SYSTEM. Return updated pointer into BUF. */
22112
22113 static unsigned char invalid_eol_type[] = "(*invalid*)";
22114
22115 static char *
22116 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22117 {
22118 Lisp_Object val;
22119 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22120 const unsigned char *eol_str;
22121 int eol_str_len;
22122 /* The EOL conversion we are using. */
22123 Lisp_Object eoltype;
22124
22125 val = CODING_SYSTEM_SPEC (coding_system);
22126 eoltype = Qnil;
22127
22128 if (!VECTORP (val)) /* Not yet decided. */
22129 {
22130 *buf++ = multibyte ? '-' : ' ';
22131 if (eol_flag)
22132 eoltype = eol_mnemonic_undecided;
22133 /* Don't mention EOL conversion if it isn't decided. */
22134 }
22135 else
22136 {
22137 Lisp_Object attrs;
22138 Lisp_Object eolvalue;
22139
22140 attrs = AREF (val, 0);
22141 eolvalue = AREF (val, 2);
22142
22143 *buf++ = multibyte
22144 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22145 : ' ';
22146
22147 if (eol_flag)
22148 {
22149 /* The EOL conversion that is normal on this system. */
22150
22151 if (NILP (eolvalue)) /* Not yet decided. */
22152 eoltype = eol_mnemonic_undecided;
22153 else if (VECTORP (eolvalue)) /* Not yet decided. */
22154 eoltype = eol_mnemonic_undecided;
22155 else /* eolvalue is Qunix, Qdos, or Qmac. */
22156 eoltype = (EQ (eolvalue, Qunix)
22157 ? eol_mnemonic_unix
22158 : (EQ (eolvalue, Qdos) == 1
22159 ? eol_mnemonic_dos : eol_mnemonic_mac));
22160 }
22161 }
22162
22163 if (eol_flag)
22164 {
22165 /* Mention the EOL conversion if it is not the usual one. */
22166 if (STRINGP (eoltype))
22167 {
22168 eol_str = SDATA (eoltype);
22169 eol_str_len = SBYTES (eoltype);
22170 }
22171 else if (CHARACTERP (eoltype))
22172 {
22173 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22174 int c = XFASTINT (eoltype);
22175 eol_str_len = CHAR_STRING (c, tmp);
22176 eol_str = tmp;
22177 }
22178 else
22179 {
22180 eol_str = invalid_eol_type;
22181 eol_str_len = sizeof (invalid_eol_type) - 1;
22182 }
22183 memcpy (buf, eol_str, eol_str_len);
22184 buf += eol_str_len;
22185 }
22186
22187 return buf;
22188 }
22189
22190 /* Return a string for the output of a mode line %-spec for window W,
22191 generated by character C. FIELD_WIDTH > 0 means pad the string
22192 returned with spaces to that value. Return a Lisp string in
22193 *STRING if the resulting string is taken from that Lisp string.
22194
22195 Note we operate on the current buffer for most purposes. */
22196
22197 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22198
22199 static const char *
22200 decode_mode_spec (struct window *w, register int c, int field_width,
22201 Lisp_Object *string)
22202 {
22203 Lisp_Object obj;
22204 struct frame *f = XFRAME (WINDOW_FRAME (w));
22205 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22206 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22207 produce strings from numerical values, so limit preposterously
22208 large values of FIELD_WIDTH to avoid overrunning the buffer's
22209 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22210 bytes plus the terminating null. */
22211 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22212 struct buffer *b = current_buffer;
22213
22214 obj = Qnil;
22215 *string = Qnil;
22216
22217 switch (c)
22218 {
22219 case '*':
22220 if (!NILP (BVAR (b, read_only)))
22221 return "%";
22222 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22223 return "*";
22224 return "-";
22225
22226 case '+':
22227 /* This differs from %* only for a modified read-only buffer. */
22228 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22229 return "*";
22230 if (!NILP (BVAR (b, read_only)))
22231 return "%";
22232 return "-";
22233
22234 case '&':
22235 /* This differs from %* in ignoring read-only-ness. */
22236 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22237 return "*";
22238 return "-";
22239
22240 case '%':
22241 return "%";
22242
22243 case '[':
22244 {
22245 int i;
22246 char *p;
22247
22248 if (command_loop_level > 5)
22249 return "[[[... ";
22250 p = decode_mode_spec_buf;
22251 for (i = 0; i < command_loop_level; i++)
22252 *p++ = '[';
22253 *p = 0;
22254 return decode_mode_spec_buf;
22255 }
22256
22257 case ']':
22258 {
22259 int i;
22260 char *p;
22261
22262 if (command_loop_level > 5)
22263 return " ...]]]";
22264 p = decode_mode_spec_buf;
22265 for (i = 0; i < command_loop_level; i++)
22266 *p++ = ']';
22267 *p = 0;
22268 return decode_mode_spec_buf;
22269 }
22270
22271 case '-':
22272 {
22273 register int i;
22274
22275 /* Let lots_of_dashes be a string of infinite length. */
22276 if (mode_line_target == MODE_LINE_NOPROP
22277 || mode_line_target == MODE_LINE_STRING)
22278 return "--";
22279 if (field_width <= 0
22280 || field_width > sizeof (lots_of_dashes))
22281 {
22282 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22283 decode_mode_spec_buf[i] = '-';
22284 decode_mode_spec_buf[i] = '\0';
22285 return decode_mode_spec_buf;
22286 }
22287 else
22288 return lots_of_dashes;
22289 }
22290
22291 case 'b':
22292 obj = BVAR (b, name);
22293 break;
22294
22295 case 'c':
22296 /* %c and %l are ignored in `frame-title-format'.
22297 (In redisplay_internal, the frame title is drawn _before_ the
22298 windows are updated, so the stuff which depends on actual
22299 window contents (such as %l) may fail to render properly, or
22300 even crash emacs.) */
22301 if (mode_line_target == MODE_LINE_TITLE)
22302 return "";
22303 else
22304 {
22305 ptrdiff_t col = current_column ();
22306 w->column_number_displayed = col;
22307 pint2str (decode_mode_spec_buf, width, col);
22308 return decode_mode_spec_buf;
22309 }
22310
22311 case 'e':
22312 #ifndef SYSTEM_MALLOC
22313 {
22314 if (NILP (Vmemory_full))
22315 return "";
22316 else
22317 return "!MEM FULL! ";
22318 }
22319 #else
22320 return "";
22321 #endif
22322
22323 case 'F':
22324 /* %F displays the frame name. */
22325 if (!NILP (f->title))
22326 return SSDATA (f->title);
22327 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22328 return SSDATA (f->name);
22329 return "Emacs";
22330
22331 case 'f':
22332 obj = BVAR (b, filename);
22333 break;
22334
22335 case 'i':
22336 {
22337 ptrdiff_t size = ZV - BEGV;
22338 pint2str (decode_mode_spec_buf, width, size);
22339 return decode_mode_spec_buf;
22340 }
22341
22342 case 'I':
22343 {
22344 ptrdiff_t size = ZV - BEGV;
22345 pint2hrstr (decode_mode_spec_buf, width, size);
22346 return decode_mode_spec_buf;
22347 }
22348
22349 case 'l':
22350 {
22351 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22352 ptrdiff_t topline, nlines, height;
22353 ptrdiff_t junk;
22354
22355 /* %c and %l are ignored in `frame-title-format'. */
22356 if (mode_line_target == MODE_LINE_TITLE)
22357 return "";
22358
22359 startpos = marker_position (w->start);
22360 startpos_byte = marker_byte_position (w->start);
22361 height = WINDOW_TOTAL_LINES (w);
22362
22363 /* If we decided that this buffer isn't suitable for line numbers,
22364 don't forget that too fast. */
22365 if (w->base_line_pos == -1)
22366 goto no_value;
22367
22368 /* If the buffer is very big, don't waste time. */
22369 if (INTEGERP (Vline_number_display_limit)
22370 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22371 {
22372 w->base_line_pos = 0;
22373 w->base_line_number = 0;
22374 goto no_value;
22375 }
22376
22377 if (w->base_line_number > 0
22378 && w->base_line_pos > 0
22379 && w->base_line_pos <= startpos)
22380 {
22381 line = w->base_line_number;
22382 linepos = w->base_line_pos;
22383 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22384 }
22385 else
22386 {
22387 line = 1;
22388 linepos = BUF_BEGV (b);
22389 linepos_byte = BUF_BEGV_BYTE (b);
22390 }
22391
22392 /* Count lines from base line to window start position. */
22393 nlines = display_count_lines (linepos_byte,
22394 startpos_byte,
22395 startpos, &junk);
22396
22397 topline = nlines + line;
22398
22399 /* Determine a new base line, if the old one is too close
22400 or too far away, or if we did not have one.
22401 "Too close" means it's plausible a scroll-down would
22402 go back past it. */
22403 if (startpos == BUF_BEGV (b))
22404 {
22405 w->base_line_number = topline;
22406 w->base_line_pos = BUF_BEGV (b);
22407 }
22408 else if (nlines < height + 25 || nlines > height * 3 + 50
22409 || linepos == BUF_BEGV (b))
22410 {
22411 ptrdiff_t limit = BUF_BEGV (b);
22412 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22413 ptrdiff_t position;
22414 ptrdiff_t distance =
22415 (height * 2 + 30) * line_number_display_limit_width;
22416
22417 if (startpos - distance > limit)
22418 {
22419 limit = startpos - distance;
22420 limit_byte = CHAR_TO_BYTE (limit);
22421 }
22422
22423 nlines = display_count_lines (startpos_byte,
22424 limit_byte,
22425 - (height * 2 + 30),
22426 &position);
22427 /* If we couldn't find the lines we wanted within
22428 line_number_display_limit_width chars per line,
22429 give up on line numbers for this window. */
22430 if (position == limit_byte && limit == startpos - distance)
22431 {
22432 w->base_line_pos = -1;
22433 w->base_line_number = 0;
22434 goto no_value;
22435 }
22436
22437 w->base_line_number = topline - nlines;
22438 w->base_line_pos = BYTE_TO_CHAR (position);
22439 }
22440
22441 /* Now count lines from the start pos to point. */
22442 nlines = display_count_lines (startpos_byte,
22443 PT_BYTE, PT, &junk);
22444
22445 /* Record that we did display the line number. */
22446 line_number_displayed = 1;
22447
22448 /* Make the string to show. */
22449 pint2str (decode_mode_spec_buf, width, topline + nlines);
22450 return decode_mode_spec_buf;
22451 no_value:
22452 {
22453 char* p = decode_mode_spec_buf;
22454 int pad = width - 2;
22455 while (pad-- > 0)
22456 *p++ = ' ';
22457 *p++ = '?';
22458 *p++ = '?';
22459 *p = '\0';
22460 return decode_mode_spec_buf;
22461 }
22462 }
22463 break;
22464
22465 case 'm':
22466 obj = BVAR (b, mode_name);
22467 break;
22468
22469 case 'n':
22470 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22471 return " Narrow";
22472 break;
22473
22474 case 'p':
22475 {
22476 ptrdiff_t pos = marker_position (w->start);
22477 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22478
22479 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22480 {
22481 if (pos <= BUF_BEGV (b))
22482 return "All";
22483 else
22484 return "Bottom";
22485 }
22486 else if (pos <= BUF_BEGV (b))
22487 return "Top";
22488 else
22489 {
22490 if (total > 1000000)
22491 /* Do it differently for a large value, to avoid overflow. */
22492 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22493 else
22494 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22495 /* We can't normally display a 3-digit number,
22496 so get us a 2-digit number that is close. */
22497 if (total == 100)
22498 total = 99;
22499 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22500 return decode_mode_spec_buf;
22501 }
22502 }
22503
22504 /* Display percentage of size above the bottom of the screen. */
22505 case 'P':
22506 {
22507 ptrdiff_t toppos = marker_position (w->start);
22508 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22509 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22510
22511 if (botpos >= BUF_ZV (b))
22512 {
22513 if (toppos <= BUF_BEGV (b))
22514 return "All";
22515 else
22516 return "Bottom";
22517 }
22518 else
22519 {
22520 if (total > 1000000)
22521 /* Do it differently for a large value, to avoid overflow. */
22522 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22523 else
22524 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22525 /* We can't normally display a 3-digit number,
22526 so get us a 2-digit number that is close. */
22527 if (total == 100)
22528 total = 99;
22529 if (toppos <= BUF_BEGV (b))
22530 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22531 else
22532 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22533 return decode_mode_spec_buf;
22534 }
22535 }
22536
22537 case 's':
22538 /* status of process */
22539 obj = Fget_buffer_process (Fcurrent_buffer ());
22540 if (NILP (obj))
22541 return "no process";
22542 #ifndef MSDOS
22543 obj = Fsymbol_name (Fprocess_status (obj));
22544 #endif
22545 break;
22546
22547 case '@':
22548 {
22549 ptrdiff_t count = inhibit_garbage_collection ();
22550 Lisp_Object val = call1 (intern ("file-remote-p"),
22551 BVAR (current_buffer, directory));
22552 unbind_to (count, Qnil);
22553
22554 if (NILP (val))
22555 return "-";
22556 else
22557 return "@";
22558 }
22559
22560 case 'z':
22561 /* coding-system (not including end-of-line format) */
22562 case 'Z':
22563 /* coding-system (including end-of-line type) */
22564 {
22565 int eol_flag = (c == 'Z');
22566 char *p = decode_mode_spec_buf;
22567
22568 if (! FRAME_WINDOW_P (f))
22569 {
22570 /* No need to mention EOL here--the terminal never needs
22571 to do EOL conversion. */
22572 p = decode_mode_spec_coding (CODING_ID_NAME
22573 (FRAME_KEYBOARD_CODING (f)->id),
22574 p, 0);
22575 p = decode_mode_spec_coding (CODING_ID_NAME
22576 (FRAME_TERMINAL_CODING (f)->id),
22577 p, 0);
22578 }
22579 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22580 p, eol_flag);
22581
22582 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22583 #ifdef subprocesses
22584 obj = Fget_buffer_process (Fcurrent_buffer ());
22585 if (PROCESSP (obj))
22586 {
22587 p = decode_mode_spec_coding
22588 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22589 p = decode_mode_spec_coding
22590 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22591 }
22592 #endif /* subprocesses */
22593 #endif /* 0 */
22594 *p = 0;
22595 return decode_mode_spec_buf;
22596 }
22597 }
22598
22599 if (STRINGP (obj))
22600 {
22601 *string = obj;
22602 return SSDATA (obj);
22603 }
22604 else
22605 return "";
22606 }
22607
22608
22609 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22610 means count lines back from START_BYTE. But don't go beyond
22611 LIMIT_BYTE. Return the number of lines thus found (always
22612 nonnegative).
22613
22614 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22615 either the position COUNT lines after/before START_BYTE, if we
22616 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22617 COUNT lines. */
22618
22619 static ptrdiff_t
22620 display_count_lines (ptrdiff_t start_byte,
22621 ptrdiff_t limit_byte, ptrdiff_t count,
22622 ptrdiff_t *byte_pos_ptr)
22623 {
22624 register unsigned char *cursor;
22625 unsigned char *base;
22626
22627 register ptrdiff_t ceiling;
22628 register unsigned char *ceiling_addr;
22629 ptrdiff_t orig_count = count;
22630
22631 /* If we are not in selective display mode,
22632 check only for newlines. */
22633 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22634 && !INTEGERP (BVAR (current_buffer, selective_display)));
22635
22636 if (count > 0)
22637 {
22638 while (start_byte < limit_byte)
22639 {
22640 ceiling = BUFFER_CEILING_OF (start_byte);
22641 ceiling = min (limit_byte - 1, ceiling);
22642 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22643 base = (cursor = BYTE_POS_ADDR (start_byte));
22644
22645 do
22646 {
22647 if (selective_display)
22648 {
22649 while (*cursor != '\n' && *cursor != 015
22650 && ++cursor != ceiling_addr)
22651 continue;
22652 if (cursor == ceiling_addr)
22653 break;
22654 }
22655 else
22656 {
22657 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22658 if (! cursor)
22659 break;
22660 }
22661
22662 cursor++;
22663
22664 if (--count == 0)
22665 {
22666 start_byte += cursor - base;
22667 *byte_pos_ptr = start_byte;
22668 return orig_count;
22669 }
22670 }
22671 while (cursor < ceiling_addr);
22672
22673 start_byte += ceiling_addr - base;
22674 }
22675 }
22676 else
22677 {
22678 while (start_byte > limit_byte)
22679 {
22680 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22681 ceiling = max (limit_byte, ceiling);
22682 ceiling_addr = BYTE_POS_ADDR (ceiling);
22683 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22684 while (1)
22685 {
22686 if (selective_display)
22687 {
22688 while (--cursor >= ceiling_addr
22689 && *cursor != '\n' && *cursor != 015)
22690 continue;
22691 if (cursor < ceiling_addr)
22692 break;
22693 }
22694 else
22695 {
22696 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22697 if (! cursor)
22698 break;
22699 }
22700
22701 if (++count == 0)
22702 {
22703 start_byte += cursor - base + 1;
22704 *byte_pos_ptr = start_byte;
22705 /* When scanning backwards, we should
22706 not count the newline posterior to which we stop. */
22707 return - orig_count - 1;
22708 }
22709 }
22710 start_byte += ceiling_addr - base;
22711 }
22712 }
22713
22714 *byte_pos_ptr = limit_byte;
22715
22716 if (count < 0)
22717 return - orig_count + count;
22718 return orig_count - count;
22719
22720 }
22721
22722
22723 \f
22724 /***********************************************************************
22725 Displaying strings
22726 ***********************************************************************/
22727
22728 /* Display a NUL-terminated string, starting with index START.
22729
22730 If STRING is non-null, display that C string. Otherwise, the Lisp
22731 string LISP_STRING is displayed. There's a case that STRING is
22732 non-null and LISP_STRING is not nil. It means STRING is a string
22733 data of LISP_STRING. In that case, we display LISP_STRING while
22734 ignoring its text properties.
22735
22736 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22737 FACE_STRING. Display STRING or LISP_STRING with the face at
22738 FACE_STRING_POS in FACE_STRING:
22739
22740 Display the string in the environment given by IT, but use the
22741 standard display table, temporarily.
22742
22743 FIELD_WIDTH is the minimum number of output glyphs to produce.
22744 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22745 with spaces. If STRING has more characters, more than FIELD_WIDTH
22746 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22747
22748 PRECISION is the maximum number of characters to output from
22749 STRING. PRECISION < 0 means don't truncate the string.
22750
22751 This is roughly equivalent to printf format specifiers:
22752
22753 FIELD_WIDTH PRECISION PRINTF
22754 ----------------------------------------
22755 -1 -1 %s
22756 -1 10 %.10s
22757 10 -1 %10s
22758 20 10 %20.10s
22759
22760 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22761 display them, and < 0 means obey the current buffer's value of
22762 enable_multibyte_characters.
22763
22764 Value is the number of columns displayed. */
22765
22766 static int
22767 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22768 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22769 int field_width, int precision, int max_x, int multibyte)
22770 {
22771 int hpos_at_start = it->hpos;
22772 int saved_face_id = it->face_id;
22773 struct glyph_row *row = it->glyph_row;
22774 ptrdiff_t it_charpos;
22775
22776 /* Initialize the iterator IT for iteration over STRING beginning
22777 with index START. */
22778 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22779 precision, field_width, multibyte);
22780 if (string && STRINGP (lisp_string))
22781 /* LISP_STRING is the one returned by decode_mode_spec. We should
22782 ignore its text properties. */
22783 it->stop_charpos = it->end_charpos;
22784
22785 /* If displaying STRING, set up the face of the iterator from
22786 FACE_STRING, if that's given. */
22787 if (STRINGP (face_string))
22788 {
22789 ptrdiff_t endptr;
22790 struct face *face;
22791
22792 it->face_id
22793 = face_at_string_position (it->w, face_string, face_string_pos,
22794 0, &endptr, it->base_face_id, 0);
22795 face = FACE_FROM_ID (it->f, it->face_id);
22796 it->face_box_p = face->box != FACE_NO_BOX;
22797 }
22798
22799 /* Set max_x to the maximum allowed X position. Don't let it go
22800 beyond the right edge of the window. */
22801 if (max_x <= 0)
22802 max_x = it->last_visible_x;
22803 else
22804 max_x = min (max_x, it->last_visible_x);
22805
22806 /* Skip over display elements that are not visible. because IT->w is
22807 hscrolled. */
22808 if (it->current_x < it->first_visible_x)
22809 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22810 MOVE_TO_POS | MOVE_TO_X);
22811
22812 row->ascent = it->max_ascent;
22813 row->height = it->max_ascent + it->max_descent;
22814 row->phys_ascent = it->max_phys_ascent;
22815 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22816 row->extra_line_spacing = it->max_extra_line_spacing;
22817
22818 if (STRINGP (it->string))
22819 it_charpos = IT_STRING_CHARPOS (*it);
22820 else
22821 it_charpos = IT_CHARPOS (*it);
22822
22823 /* This condition is for the case that we are called with current_x
22824 past last_visible_x. */
22825 while (it->current_x < max_x)
22826 {
22827 int x_before, x, n_glyphs_before, i, nglyphs;
22828
22829 /* Get the next display element. */
22830 if (!get_next_display_element (it))
22831 break;
22832
22833 /* Produce glyphs. */
22834 x_before = it->current_x;
22835 n_glyphs_before = row->used[TEXT_AREA];
22836 PRODUCE_GLYPHS (it);
22837
22838 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22839 i = 0;
22840 x = x_before;
22841 while (i < nglyphs)
22842 {
22843 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22844
22845 if (it->line_wrap != TRUNCATE
22846 && x + glyph->pixel_width > max_x)
22847 {
22848 /* End of continued line or max_x reached. */
22849 if (CHAR_GLYPH_PADDING_P (*glyph))
22850 {
22851 /* A wide character is unbreakable. */
22852 if (row->reversed_p)
22853 unproduce_glyphs (it, row->used[TEXT_AREA]
22854 - n_glyphs_before);
22855 row->used[TEXT_AREA] = n_glyphs_before;
22856 it->current_x = x_before;
22857 }
22858 else
22859 {
22860 if (row->reversed_p)
22861 unproduce_glyphs (it, row->used[TEXT_AREA]
22862 - (n_glyphs_before + i));
22863 row->used[TEXT_AREA] = n_glyphs_before + i;
22864 it->current_x = x;
22865 }
22866 break;
22867 }
22868 else if (x + glyph->pixel_width >= it->first_visible_x)
22869 {
22870 /* Glyph is at least partially visible. */
22871 ++it->hpos;
22872 if (x < it->first_visible_x)
22873 row->x = x - it->first_visible_x;
22874 }
22875 else
22876 {
22877 /* Glyph is off the left margin of the display area.
22878 Should not happen. */
22879 emacs_abort ();
22880 }
22881
22882 row->ascent = max (row->ascent, it->max_ascent);
22883 row->height = max (row->height, it->max_ascent + it->max_descent);
22884 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22885 row->phys_height = max (row->phys_height,
22886 it->max_phys_ascent + it->max_phys_descent);
22887 row->extra_line_spacing = max (row->extra_line_spacing,
22888 it->max_extra_line_spacing);
22889 x += glyph->pixel_width;
22890 ++i;
22891 }
22892
22893 /* Stop if max_x reached. */
22894 if (i < nglyphs)
22895 break;
22896
22897 /* Stop at line ends. */
22898 if (ITERATOR_AT_END_OF_LINE_P (it))
22899 {
22900 it->continuation_lines_width = 0;
22901 break;
22902 }
22903
22904 set_iterator_to_next (it, 1);
22905 if (STRINGP (it->string))
22906 it_charpos = IT_STRING_CHARPOS (*it);
22907 else
22908 it_charpos = IT_CHARPOS (*it);
22909
22910 /* Stop if truncating at the right edge. */
22911 if (it->line_wrap == TRUNCATE
22912 && it->current_x >= it->last_visible_x)
22913 {
22914 /* Add truncation mark, but don't do it if the line is
22915 truncated at a padding space. */
22916 if (it_charpos < it->string_nchars)
22917 {
22918 if (!FRAME_WINDOW_P (it->f))
22919 {
22920 int ii, n;
22921
22922 if (it->current_x > it->last_visible_x)
22923 {
22924 if (!row->reversed_p)
22925 {
22926 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22927 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22928 break;
22929 }
22930 else
22931 {
22932 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22933 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22934 break;
22935 unproduce_glyphs (it, ii + 1);
22936 ii = row->used[TEXT_AREA] - (ii + 1);
22937 }
22938 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22939 {
22940 row->used[TEXT_AREA] = ii;
22941 produce_special_glyphs (it, IT_TRUNCATION);
22942 }
22943 }
22944 produce_special_glyphs (it, IT_TRUNCATION);
22945 }
22946 row->truncated_on_right_p = 1;
22947 }
22948 break;
22949 }
22950 }
22951
22952 /* Maybe insert a truncation at the left. */
22953 if (it->first_visible_x
22954 && it_charpos > 0)
22955 {
22956 if (!FRAME_WINDOW_P (it->f)
22957 || (row->reversed_p
22958 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22959 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22960 insert_left_trunc_glyphs (it);
22961 row->truncated_on_left_p = 1;
22962 }
22963
22964 it->face_id = saved_face_id;
22965
22966 /* Value is number of columns displayed. */
22967 return it->hpos - hpos_at_start;
22968 }
22969
22970
22971 \f
22972 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22973 appears as an element of LIST or as the car of an element of LIST.
22974 If PROPVAL is a list, compare each element against LIST in that
22975 way, and return 1/2 if any element of PROPVAL is found in LIST.
22976 Otherwise return 0. This function cannot quit.
22977 The return value is 2 if the text is invisible but with an ellipsis
22978 and 1 if it's invisible and without an ellipsis. */
22979
22980 int
22981 invisible_p (register Lisp_Object propval, Lisp_Object list)
22982 {
22983 register Lisp_Object tail, proptail;
22984
22985 for (tail = list; CONSP (tail); tail = XCDR (tail))
22986 {
22987 register Lisp_Object tem;
22988 tem = XCAR (tail);
22989 if (EQ (propval, tem))
22990 return 1;
22991 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22992 return NILP (XCDR (tem)) ? 1 : 2;
22993 }
22994
22995 if (CONSP (propval))
22996 {
22997 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22998 {
22999 Lisp_Object propelt;
23000 propelt = XCAR (proptail);
23001 for (tail = list; CONSP (tail); tail = XCDR (tail))
23002 {
23003 register Lisp_Object tem;
23004 tem = XCAR (tail);
23005 if (EQ (propelt, tem))
23006 return 1;
23007 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23008 return NILP (XCDR (tem)) ? 1 : 2;
23009 }
23010 }
23011 }
23012
23013 return 0;
23014 }
23015
23016 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23017 doc: /* Non-nil if the property makes the text invisible.
23018 POS-OR-PROP can be a marker or number, in which case it is taken to be
23019 a position in the current buffer and the value of the `invisible' property
23020 is checked; or it can be some other value, which is then presumed to be the
23021 value of the `invisible' property of the text of interest.
23022 The non-nil value returned can be t for truly invisible text or something
23023 else if the text is replaced by an ellipsis. */)
23024 (Lisp_Object pos_or_prop)
23025 {
23026 Lisp_Object prop
23027 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23028 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23029 : pos_or_prop);
23030 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23031 return (invis == 0 ? Qnil
23032 : invis == 1 ? Qt
23033 : make_number (invis));
23034 }
23035
23036 /* Calculate a width or height in pixels from a specification using
23037 the following elements:
23038
23039 SPEC ::=
23040 NUM - a (fractional) multiple of the default font width/height
23041 (NUM) - specifies exactly NUM pixels
23042 UNIT - a fixed number of pixels, see below.
23043 ELEMENT - size of a display element in pixels, see below.
23044 (NUM . SPEC) - equals NUM * SPEC
23045 (+ SPEC SPEC ...) - add pixel values
23046 (- SPEC SPEC ...) - subtract pixel values
23047 (- SPEC) - negate pixel value
23048
23049 NUM ::=
23050 INT or FLOAT - a number constant
23051 SYMBOL - use symbol's (buffer local) variable binding.
23052
23053 UNIT ::=
23054 in - pixels per inch *)
23055 mm - pixels per 1/1000 meter *)
23056 cm - pixels per 1/100 meter *)
23057 width - width of current font in pixels.
23058 height - height of current font in pixels.
23059
23060 *) using the ratio(s) defined in display-pixels-per-inch.
23061
23062 ELEMENT ::=
23063
23064 left-fringe - left fringe width in pixels
23065 right-fringe - right fringe width in pixels
23066
23067 left-margin - left margin width in pixels
23068 right-margin - right margin width in pixels
23069
23070 scroll-bar - scroll-bar area width in pixels
23071
23072 Examples:
23073
23074 Pixels corresponding to 5 inches:
23075 (5 . in)
23076
23077 Total width of non-text areas on left side of window (if scroll-bar is on left):
23078 '(space :width (+ left-fringe left-margin scroll-bar))
23079
23080 Align to first text column (in header line):
23081 '(space :align-to 0)
23082
23083 Align to middle of text area minus half the width of variable `my-image'
23084 containing a loaded image:
23085 '(space :align-to (0.5 . (- text my-image)))
23086
23087 Width of left margin minus width of 1 character in the default font:
23088 '(space :width (- left-margin 1))
23089
23090 Width of left margin minus width of 2 characters in the current font:
23091 '(space :width (- left-margin (2 . width)))
23092
23093 Center 1 character over left-margin (in header line):
23094 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23095
23096 Different ways to express width of left fringe plus left margin minus one pixel:
23097 '(space :width (- (+ left-fringe left-margin) (1)))
23098 '(space :width (+ left-fringe left-margin (- (1))))
23099 '(space :width (+ left-fringe left-margin (-1)))
23100
23101 */
23102
23103 static int
23104 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23105 struct font *font, int width_p, int *align_to)
23106 {
23107 double pixels;
23108
23109 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23110 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23111
23112 if (NILP (prop))
23113 return OK_PIXELS (0);
23114
23115 eassert (FRAME_LIVE_P (it->f));
23116
23117 if (SYMBOLP (prop))
23118 {
23119 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23120 {
23121 char *unit = SSDATA (SYMBOL_NAME (prop));
23122
23123 if (unit[0] == 'i' && unit[1] == 'n')
23124 pixels = 1.0;
23125 else if (unit[0] == 'm' && unit[1] == 'm')
23126 pixels = 25.4;
23127 else if (unit[0] == 'c' && unit[1] == 'm')
23128 pixels = 2.54;
23129 else
23130 pixels = 0;
23131 if (pixels > 0)
23132 {
23133 double ppi = (width_p ? FRAME_RES_X (it->f)
23134 : FRAME_RES_Y (it->f));
23135
23136 if (ppi > 0)
23137 return OK_PIXELS (ppi / pixels);
23138 return 0;
23139 }
23140 }
23141
23142 #ifdef HAVE_WINDOW_SYSTEM
23143 if (EQ (prop, Qheight))
23144 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23145 if (EQ (prop, Qwidth))
23146 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23147 #else
23148 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23149 return OK_PIXELS (1);
23150 #endif
23151
23152 if (EQ (prop, Qtext))
23153 return OK_PIXELS (width_p
23154 ? window_box_width (it->w, TEXT_AREA)
23155 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23156
23157 if (align_to && *align_to < 0)
23158 {
23159 *res = 0;
23160 if (EQ (prop, Qleft))
23161 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23162 if (EQ (prop, Qright))
23163 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23164 if (EQ (prop, Qcenter))
23165 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23166 + window_box_width (it->w, TEXT_AREA) / 2);
23167 if (EQ (prop, Qleft_fringe))
23168 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23169 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23170 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23171 if (EQ (prop, Qright_fringe))
23172 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23173 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23174 : window_box_right_offset (it->w, TEXT_AREA));
23175 if (EQ (prop, Qleft_margin))
23176 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23177 if (EQ (prop, Qright_margin))
23178 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23179 if (EQ (prop, Qscroll_bar))
23180 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23181 ? 0
23182 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23183 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23184 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23185 : 0)));
23186 }
23187 else
23188 {
23189 if (EQ (prop, Qleft_fringe))
23190 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23191 if (EQ (prop, Qright_fringe))
23192 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23193 if (EQ (prop, Qleft_margin))
23194 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23195 if (EQ (prop, Qright_margin))
23196 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23197 if (EQ (prop, Qscroll_bar))
23198 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23199 }
23200
23201 prop = buffer_local_value_1 (prop, it->w->contents);
23202 if (EQ (prop, Qunbound))
23203 prop = Qnil;
23204 }
23205
23206 if (INTEGERP (prop) || FLOATP (prop))
23207 {
23208 int base_unit = (width_p
23209 ? FRAME_COLUMN_WIDTH (it->f)
23210 : FRAME_LINE_HEIGHT (it->f));
23211 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23212 }
23213
23214 if (CONSP (prop))
23215 {
23216 Lisp_Object car = XCAR (prop);
23217 Lisp_Object cdr = XCDR (prop);
23218
23219 if (SYMBOLP (car))
23220 {
23221 #ifdef HAVE_WINDOW_SYSTEM
23222 if (FRAME_WINDOW_P (it->f)
23223 && valid_image_p (prop))
23224 {
23225 ptrdiff_t id = lookup_image (it->f, prop);
23226 struct image *img = IMAGE_FROM_ID (it->f, id);
23227
23228 return OK_PIXELS (width_p ? img->width : img->height);
23229 }
23230 #endif
23231 if (EQ (car, Qplus) || EQ (car, Qminus))
23232 {
23233 int first = 1;
23234 double px;
23235
23236 pixels = 0;
23237 while (CONSP (cdr))
23238 {
23239 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23240 font, width_p, align_to))
23241 return 0;
23242 if (first)
23243 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23244 else
23245 pixels += px;
23246 cdr = XCDR (cdr);
23247 }
23248 if (EQ (car, Qminus))
23249 pixels = -pixels;
23250 return OK_PIXELS (pixels);
23251 }
23252
23253 car = buffer_local_value_1 (car, it->w->contents);
23254 if (EQ (car, Qunbound))
23255 car = Qnil;
23256 }
23257
23258 if (INTEGERP (car) || FLOATP (car))
23259 {
23260 double fact;
23261 pixels = XFLOATINT (car);
23262 if (NILP (cdr))
23263 return OK_PIXELS (pixels);
23264 if (calc_pixel_width_or_height (&fact, it, cdr,
23265 font, width_p, align_to))
23266 return OK_PIXELS (pixels * fact);
23267 return 0;
23268 }
23269
23270 return 0;
23271 }
23272
23273 return 0;
23274 }
23275
23276 \f
23277 /***********************************************************************
23278 Glyph Display
23279 ***********************************************************************/
23280
23281 #ifdef HAVE_WINDOW_SYSTEM
23282
23283 #ifdef GLYPH_DEBUG
23284
23285 void
23286 dump_glyph_string (struct glyph_string *s)
23287 {
23288 fprintf (stderr, "glyph string\n");
23289 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23290 s->x, s->y, s->width, s->height);
23291 fprintf (stderr, " ybase = %d\n", s->ybase);
23292 fprintf (stderr, " hl = %d\n", s->hl);
23293 fprintf (stderr, " left overhang = %d, right = %d\n",
23294 s->left_overhang, s->right_overhang);
23295 fprintf (stderr, " nchars = %d\n", s->nchars);
23296 fprintf (stderr, " extends to end of line = %d\n",
23297 s->extends_to_end_of_line_p);
23298 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23299 fprintf (stderr, " bg width = %d\n", s->background_width);
23300 }
23301
23302 #endif /* GLYPH_DEBUG */
23303
23304 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23305 of XChar2b structures for S; it can't be allocated in
23306 init_glyph_string because it must be allocated via `alloca'. W
23307 is the window on which S is drawn. ROW and AREA are the glyph row
23308 and area within the row from which S is constructed. START is the
23309 index of the first glyph structure covered by S. HL is a
23310 face-override for drawing S. */
23311
23312 #ifdef HAVE_NTGUI
23313 #define OPTIONAL_HDC(hdc) HDC hdc,
23314 #define DECLARE_HDC(hdc) HDC hdc;
23315 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23316 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23317 #endif
23318
23319 #ifndef OPTIONAL_HDC
23320 #define OPTIONAL_HDC(hdc)
23321 #define DECLARE_HDC(hdc)
23322 #define ALLOCATE_HDC(hdc, f)
23323 #define RELEASE_HDC(hdc, f)
23324 #endif
23325
23326 static void
23327 init_glyph_string (struct glyph_string *s,
23328 OPTIONAL_HDC (hdc)
23329 XChar2b *char2b, struct window *w, struct glyph_row *row,
23330 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23331 {
23332 memset (s, 0, sizeof *s);
23333 s->w = w;
23334 s->f = XFRAME (w->frame);
23335 #ifdef HAVE_NTGUI
23336 s->hdc = hdc;
23337 #endif
23338 s->display = FRAME_X_DISPLAY (s->f);
23339 s->window = FRAME_X_WINDOW (s->f);
23340 s->char2b = char2b;
23341 s->hl = hl;
23342 s->row = row;
23343 s->area = area;
23344 s->first_glyph = row->glyphs[area] + start;
23345 s->height = row->height;
23346 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23347 s->ybase = s->y + row->ascent;
23348 }
23349
23350
23351 /* Append the list of glyph strings with head H and tail T to the list
23352 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23353
23354 static void
23355 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23356 struct glyph_string *h, struct glyph_string *t)
23357 {
23358 if (h)
23359 {
23360 if (*head)
23361 (*tail)->next = h;
23362 else
23363 *head = h;
23364 h->prev = *tail;
23365 *tail = t;
23366 }
23367 }
23368
23369
23370 /* Prepend the list of glyph strings with head H and tail T to the
23371 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23372 result. */
23373
23374 static void
23375 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23376 struct glyph_string *h, struct glyph_string *t)
23377 {
23378 if (h)
23379 {
23380 if (*head)
23381 (*head)->prev = t;
23382 else
23383 *tail = t;
23384 t->next = *head;
23385 *head = h;
23386 }
23387 }
23388
23389
23390 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23391 Set *HEAD and *TAIL to the resulting list. */
23392
23393 static void
23394 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23395 struct glyph_string *s)
23396 {
23397 s->next = s->prev = NULL;
23398 append_glyph_string_lists (head, tail, s, s);
23399 }
23400
23401
23402 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23403 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23404 make sure that X resources for the face returned are allocated.
23405 Value is a pointer to a realized face that is ready for display if
23406 DISPLAY_P is non-zero. */
23407
23408 static struct face *
23409 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23410 XChar2b *char2b, int display_p)
23411 {
23412 struct face *face = FACE_FROM_ID (f, face_id);
23413 unsigned code = 0;
23414
23415 if (face->font)
23416 {
23417 code = face->font->driver->encode_char (face->font, c);
23418
23419 if (code == FONT_INVALID_CODE)
23420 code = 0;
23421 }
23422 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23423
23424 /* Make sure X resources of the face are allocated. */
23425 #ifdef HAVE_X_WINDOWS
23426 if (display_p)
23427 #endif
23428 {
23429 eassert (face != NULL);
23430 PREPARE_FACE_FOR_DISPLAY (f, face);
23431 }
23432
23433 return face;
23434 }
23435
23436
23437 /* Get face and two-byte form of character glyph GLYPH on frame F.
23438 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23439 a pointer to a realized face that is ready for display. */
23440
23441 static struct face *
23442 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23443 XChar2b *char2b, int *two_byte_p)
23444 {
23445 struct face *face;
23446 unsigned code = 0;
23447
23448 eassert (glyph->type == CHAR_GLYPH);
23449 face = FACE_FROM_ID (f, glyph->face_id);
23450
23451 /* Make sure X resources of the face are allocated. */
23452 eassert (face != NULL);
23453 PREPARE_FACE_FOR_DISPLAY (f, face);
23454
23455 if (two_byte_p)
23456 *two_byte_p = 0;
23457
23458 if (face->font)
23459 {
23460 if (CHAR_BYTE8_P (glyph->u.ch))
23461 code = CHAR_TO_BYTE8 (glyph->u.ch);
23462 else
23463 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23464
23465 if (code == FONT_INVALID_CODE)
23466 code = 0;
23467 }
23468
23469 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23470 return face;
23471 }
23472
23473
23474 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23475 Return 1 if FONT has a glyph for C, otherwise return 0. */
23476
23477 static int
23478 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23479 {
23480 unsigned code;
23481
23482 if (CHAR_BYTE8_P (c))
23483 code = CHAR_TO_BYTE8 (c);
23484 else
23485 code = font->driver->encode_char (font, c);
23486
23487 if (code == FONT_INVALID_CODE)
23488 return 0;
23489 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23490 return 1;
23491 }
23492
23493
23494 /* Fill glyph string S with composition components specified by S->cmp.
23495
23496 BASE_FACE is the base face of the composition.
23497 S->cmp_from is the index of the first component for S.
23498
23499 OVERLAPS non-zero means S should draw the foreground only, and use
23500 its physical height for clipping. See also draw_glyphs.
23501
23502 Value is the index of a component not in S. */
23503
23504 static int
23505 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23506 int overlaps)
23507 {
23508 int i;
23509 /* For all glyphs of this composition, starting at the offset
23510 S->cmp_from, until we reach the end of the definition or encounter a
23511 glyph that requires the different face, add it to S. */
23512 struct face *face;
23513
23514 eassert (s);
23515
23516 s->for_overlaps = overlaps;
23517 s->face = NULL;
23518 s->font = NULL;
23519 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23520 {
23521 int c = COMPOSITION_GLYPH (s->cmp, i);
23522
23523 /* TAB in a composition means display glyphs with padding space
23524 on the left or right. */
23525 if (c != '\t')
23526 {
23527 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23528 -1, Qnil);
23529
23530 face = get_char_face_and_encoding (s->f, c, face_id,
23531 s->char2b + i, 1);
23532 if (face)
23533 {
23534 if (! s->face)
23535 {
23536 s->face = face;
23537 s->font = s->face->font;
23538 }
23539 else if (s->face != face)
23540 break;
23541 }
23542 }
23543 ++s->nchars;
23544 }
23545 s->cmp_to = i;
23546
23547 if (s->face == NULL)
23548 {
23549 s->face = base_face->ascii_face;
23550 s->font = s->face->font;
23551 }
23552
23553 /* All glyph strings for the same composition has the same width,
23554 i.e. the width set for the first component of the composition. */
23555 s->width = s->first_glyph->pixel_width;
23556
23557 /* If the specified font could not be loaded, use the frame's
23558 default font, but record the fact that we couldn't load it in
23559 the glyph string so that we can draw rectangles for the
23560 characters of the glyph string. */
23561 if (s->font == NULL)
23562 {
23563 s->font_not_found_p = 1;
23564 s->font = FRAME_FONT (s->f);
23565 }
23566
23567 /* Adjust base line for subscript/superscript text. */
23568 s->ybase += s->first_glyph->voffset;
23569
23570 /* This glyph string must always be drawn with 16-bit functions. */
23571 s->two_byte_p = 1;
23572
23573 return s->cmp_to;
23574 }
23575
23576 static int
23577 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23578 int start, int end, int overlaps)
23579 {
23580 struct glyph *glyph, *last;
23581 Lisp_Object lgstring;
23582 int i;
23583
23584 s->for_overlaps = overlaps;
23585 glyph = s->row->glyphs[s->area] + start;
23586 last = s->row->glyphs[s->area] + end;
23587 s->cmp_id = glyph->u.cmp.id;
23588 s->cmp_from = glyph->slice.cmp.from;
23589 s->cmp_to = glyph->slice.cmp.to + 1;
23590 s->face = FACE_FROM_ID (s->f, face_id);
23591 lgstring = composition_gstring_from_id (s->cmp_id);
23592 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23593 glyph++;
23594 while (glyph < last
23595 && glyph->u.cmp.automatic
23596 && glyph->u.cmp.id == s->cmp_id
23597 && s->cmp_to == glyph->slice.cmp.from)
23598 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23599
23600 for (i = s->cmp_from; i < s->cmp_to; i++)
23601 {
23602 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23603 unsigned code = LGLYPH_CODE (lglyph);
23604
23605 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23606 }
23607 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23608 return glyph - s->row->glyphs[s->area];
23609 }
23610
23611
23612 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23613 See the comment of fill_glyph_string for arguments.
23614 Value is the index of the first glyph not in S. */
23615
23616
23617 static int
23618 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23619 int start, int end, int overlaps)
23620 {
23621 struct glyph *glyph, *last;
23622 int voffset;
23623
23624 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23625 s->for_overlaps = overlaps;
23626 glyph = s->row->glyphs[s->area] + start;
23627 last = s->row->glyphs[s->area] + end;
23628 voffset = glyph->voffset;
23629 s->face = FACE_FROM_ID (s->f, face_id);
23630 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23631 s->nchars = 1;
23632 s->width = glyph->pixel_width;
23633 glyph++;
23634 while (glyph < last
23635 && glyph->type == GLYPHLESS_GLYPH
23636 && glyph->voffset == voffset
23637 && glyph->face_id == face_id)
23638 {
23639 s->nchars++;
23640 s->width += glyph->pixel_width;
23641 glyph++;
23642 }
23643 s->ybase += voffset;
23644 return glyph - s->row->glyphs[s->area];
23645 }
23646
23647
23648 /* Fill glyph string S from a sequence of character glyphs.
23649
23650 FACE_ID is the face id of the string. START is the index of the
23651 first glyph to consider, END is the index of the last + 1.
23652 OVERLAPS non-zero means S should draw the foreground only, and use
23653 its physical height for clipping. See also draw_glyphs.
23654
23655 Value is the index of the first glyph not in S. */
23656
23657 static int
23658 fill_glyph_string (struct glyph_string *s, int face_id,
23659 int start, int end, int overlaps)
23660 {
23661 struct glyph *glyph, *last;
23662 int voffset;
23663 int glyph_not_available_p;
23664
23665 eassert (s->f == XFRAME (s->w->frame));
23666 eassert (s->nchars == 0);
23667 eassert (start >= 0 && end > start);
23668
23669 s->for_overlaps = overlaps;
23670 glyph = s->row->glyphs[s->area] + start;
23671 last = s->row->glyphs[s->area] + end;
23672 voffset = glyph->voffset;
23673 s->padding_p = glyph->padding_p;
23674 glyph_not_available_p = glyph->glyph_not_available_p;
23675
23676 while (glyph < last
23677 && glyph->type == CHAR_GLYPH
23678 && glyph->voffset == voffset
23679 /* Same face id implies same font, nowadays. */
23680 && glyph->face_id == face_id
23681 && glyph->glyph_not_available_p == glyph_not_available_p)
23682 {
23683 int two_byte_p;
23684
23685 s->face = get_glyph_face_and_encoding (s->f, glyph,
23686 s->char2b + s->nchars,
23687 &two_byte_p);
23688 s->two_byte_p = two_byte_p;
23689 ++s->nchars;
23690 eassert (s->nchars <= end - start);
23691 s->width += glyph->pixel_width;
23692 if (glyph++->padding_p != s->padding_p)
23693 break;
23694 }
23695
23696 s->font = s->face->font;
23697
23698 /* If the specified font could not be loaded, use the frame's font,
23699 but record the fact that we couldn't load it in
23700 S->font_not_found_p so that we can draw rectangles for the
23701 characters of the glyph string. */
23702 if (s->font == NULL || glyph_not_available_p)
23703 {
23704 s->font_not_found_p = 1;
23705 s->font = FRAME_FONT (s->f);
23706 }
23707
23708 /* Adjust base line for subscript/superscript text. */
23709 s->ybase += voffset;
23710
23711 eassert (s->face && s->face->gc);
23712 return glyph - s->row->glyphs[s->area];
23713 }
23714
23715
23716 /* Fill glyph string S from image glyph S->first_glyph. */
23717
23718 static void
23719 fill_image_glyph_string (struct glyph_string *s)
23720 {
23721 eassert (s->first_glyph->type == IMAGE_GLYPH);
23722 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23723 eassert (s->img);
23724 s->slice = s->first_glyph->slice.img;
23725 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23726 s->font = s->face->font;
23727 s->width = s->first_glyph->pixel_width;
23728
23729 /* Adjust base line for subscript/superscript text. */
23730 s->ybase += s->first_glyph->voffset;
23731 }
23732
23733
23734 /* Fill glyph string S from a sequence of stretch glyphs.
23735
23736 START is the index of the first glyph to consider,
23737 END is the index of the last + 1.
23738
23739 Value is the index of the first glyph not in S. */
23740
23741 static int
23742 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23743 {
23744 struct glyph *glyph, *last;
23745 int voffset, face_id;
23746
23747 eassert (s->first_glyph->type == STRETCH_GLYPH);
23748
23749 glyph = s->row->glyphs[s->area] + start;
23750 last = s->row->glyphs[s->area] + end;
23751 face_id = glyph->face_id;
23752 s->face = FACE_FROM_ID (s->f, face_id);
23753 s->font = s->face->font;
23754 s->width = glyph->pixel_width;
23755 s->nchars = 1;
23756 voffset = glyph->voffset;
23757
23758 for (++glyph;
23759 (glyph < last
23760 && glyph->type == STRETCH_GLYPH
23761 && glyph->voffset == voffset
23762 && glyph->face_id == face_id);
23763 ++glyph)
23764 s->width += glyph->pixel_width;
23765
23766 /* Adjust base line for subscript/superscript text. */
23767 s->ybase += voffset;
23768
23769 /* The case that face->gc == 0 is handled when drawing the glyph
23770 string by calling PREPARE_FACE_FOR_DISPLAY. */
23771 eassert (s->face);
23772 return glyph - s->row->glyphs[s->area];
23773 }
23774
23775 static struct font_metrics *
23776 get_per_char_metric (struct font *font, XChar2b *char2b)
23777 {
23778 static struct font_metrics metrics;
23779 unsigned code;
23780
23781 if (! font)
23782 return NULL;
23783 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23784 if (code == FONT_INVALID_CODE)
23785 return NULL;
23786 font->driver->text_extents (font, &code, 1, &metrics);
23787 return &metrics;
23788 }
23789
23790 /* EXPORT for RIF:
23791 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23792 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23793 assumed to be zero. */
23794
23795 void
23796 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23797 {
23798 *left = *right = 0;
23799
23800 if (glyph->type == CHAR_GLYPH)
23801 {
23802 struct face *face;
23803 XChar2b char2b;
23804 struct font_metrics *pcm;
23805
23806 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23807 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23808 {
23809 if (pcm->rbearing > pcm->width)
23810 *right = pcm->rbearing - pcm->width;
23811 if (pcm->lbearing < 0)
23812 *left = -pcm->lbearing;
23813 }
23814 }
23815 else if (glyph->type == COMPOSITE_GLYPH)
23816 {
23817 if (! glyph->u.cmp.automatic)
23818 {
23819 struct composition *cmp = composition_table[glyph->u.cmp.id];
23820
23821 if (cmp->rbearing > cmp->pixel_width)
23822 *right = cmp->rbearing - cmp->pixel_width;
23823 if (cmp->lbearing < 0)
23824 *left = - cmp->lbearing;
23825 }
23826 else
23827 {
23828 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23829 struct font_metrics metrics;
23830
23831 composition_gstring_width (gstring, glyph->slice.cmp.from,
23832 glyph->slice.cmp.to + 1, &metrics);
23833 if (metrics.rbearing > metrics.width)
23834 *right = metrics.rbearing - metrics.width;
23835 if (metrics.lbearing < 0)
23836 *left = - metrics.lbearing;
23837 }
23838 }
23839 }
23840
23841
23842 /* Return the index of the first glyph preceding glyph string S that
23843 is overwritten by S because of S's left overhang. Value is -1
23844 if no glyphs are overwritten. */
23845
23846 static int
23847 left_overwritten (struct glyph_string *s)
23848 {
23849 int k;
23850
23851 if (s->left_overhang)
23852 {
23853 int x = 0, i;
23854 struct glyph *glyphs = s->row->glyphs[s->area];
23855 int first = s->first_glyph - glyphs;
23856
23857 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23858 x -= glyphs[i].pixel_width;
23859
23860 k = i + 1;
23861 }
23862 else
23863 k = -1;
23864
23865 return k;
23866 }
23867
23868
23869 /* Return the index of the first glyph preceding glyph string S that
23870 is overwriting S because of its right overhang. Value is -1 if no
23871 glyph in front of S overwrites S. */
23872
23873 static int
23874 left_overwriting (struct glyph_string *s)
23875 {
23876 int i, k, x;
23877 struct glyph *glyphs = s->row->glyphs[s->area];
23878 int first = s->first_glyph - glyphs;
23879
23880 k = -1;
23881 x = 0;
23882 for (i = first - 1; i >= 0; --i)
23883 {
23884 int left, right;
23885 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23886 if (x + right > 0)
23887 k = i;
23888 x -= glyphs[i].pixel_width;
23889 }
23890
23891 return k;
23892 }
23893
23894
23895 /* Return the index of the last glyph following glyph string S that is
23896 overwritten by S because of S's right overhang. Value is -1 if
23897 no such glyph is found. */
23898
23899 static int
23900 right_overwritten (struct glyph_string *s)
23901 {
23902 int k = -1;
23903
23904 if (s->right_overhang)
23905 {
23906 int x = 0, i;
23907 struct glyph *glyphs = s->row->glyphs[s->area];
23908 int first = (s->first_glyph - glyphs
23909 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23910 int end = s->row->used[s->area];
23911
23912 for (i = first; i < end && s->right_overhang > x; ++i)
23913 x += glyphs[i].pixel_width;
23914
23915 k = i;
23916 }
23917
23918 return k;
23919 }
23920
23921
23922 /* Return the index of the last glyph following glyph string S that
23923 overwrites S because of its left overhang. Value is negative
23924 if no such glyph is found. */
23925
23926 static int
23927 right_overwriting (struct glyph_string *s)
23928 {
23929 int i, k, x;
23930 int end = s->row->used[s->area];
23931 struct glyph *glyphs = s->row->glyphs[s->area];
23932 int first = (s->first_glyph - glyphs
23933 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23934
23935 k = -1;
23936 x = 0;
23937 for (i = first; i < end; ++i)
23938 {
23939 int left, right;
23940 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23941 if (x - left < 0)
23942 k = i;
23943 x += glyphs[i].pixel_width;
23944 }
23945
23946 return k;
23947 }
23948
23949
23950 /* Set background width of glyph string S. START is the index of the
23951 first glyph following S. LAST_X is the right-most x-position + 1
23952 in the drawing area. */
23953
23954 static void
23955 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23956 {
23957 /* If the face of this glyph string has to be drawn to the end of
23958 the drawing area, set S->extends_to_end_of_line_p. */
23959
23960 if (start == s->row->used[s->area]
23961 && ((s->row->fill_line_p
23962 && (s->hl == DRAW_NORMAL_TEXT
23963 || s->hl == DRAW_IMAGE_RAISED
23964 || s->hl == DRAW_IMAGE_SUNKEN))
23965 || s->hl == DRAW_MOUSE_FACE))
23966 s->extends_to_end_of_line_p = 1;
23967
23968 /* If S extends its face to the end of the line, set its
23969 background_width to the distance to the right edge of the drawing
23970 area. */
23971 if (s->extends_to_end_of_line_p)
23972 s->background_width = last_x - s->x + 1;
23973 else
23974 s->background_width = s->width;
23975 }
23976
23977
23978 /* Compute overhangs and x-positions for glyph string S and its
23979 predecessors, or successors. X is the starting x-position for S.
23980 BACKWARD_P non-zero means process predecessors. */
23981
23982 static void
23983 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23984 {
23985 if (backward_p)
23986 {
23987 while (s)
23988 {
23989 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23990 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23991 x -= s->width;
23992 s->x = x;
23993 s = s->prev;
23994 }
23995 }
23996 else
23997 {
23998 while (s)
23999 {
24000 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24001 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24002 s->x = x;
24003 x += s->width;
24004 s = s->next;
24005 }
24006 }
24007 }
24008
24009
24010
24011 /* The following macros are only called from draw_glyphs below.
24012 They reference the following parameters of that function directly:
24013 `w', `row', `area', and `overlap_p'
24014 as well as the following local variables:
24015 `s', `f', and `hdc' (in W32) */
24016
24017 #ifdef HAVE_NTGUI
24018 /* On W32, silently add local `hdc' variable to argument list of
24019 init_glyph_string. */
24020 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24021 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24022 #else
24023 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24024 init_glyph_string (s, char2b, w, row, area, start, hl)
24025 #endif
24026
24027 /* Add a glyph string for a stretch glyph to the list of strings
24028 between HEAD and TAIL. START is the index of the stretch glyph in
24029 row area AREA of glyph row ROW. END is the index of the last glyph
24030 in that glyph row area. X is the current output position assigned
24031 to the new glyph string constructed. HL overrides that face of the
24032 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24033 is the right-most x-position of the drawing area. */
24034
24035 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24036 and below -- keep them on one line. */
24037 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24038 do \
24039 { \
24040 s = alloca (sizeof *s); \
24041 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24042 START = fill_stretch_glyph_string (s, START, END); \
24043 append_glyph_string (&HEAD, &TAIL, s); \
24044 s->x = (X); \
24045 } \
24046 while (0)
24047
24048
24049 /* Add a glyph string for an image glyph to the list of strings
24050 between HEAD and TAIL. START is the index of the image glyph in
24051 row area AREA of glyph row ROW. END is the index of the last glyph
24052 in that glyph row area. X is the current output position assigned
24053 to the new glyph string constructed. HL overrides that face of the
24054 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24055 is the right-most x-position of the drawing area. */
24056
24057 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24058 do \
24059 { \
24060 s = alloca (sizeof *s); \
24061 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24062 fill_image_glyph_string (s); \
24063 append_glyph_string (&HEAD, &TAIL, s); \
24064 ++START; \
24065 s->x = (X); \
24066 } \
24067 while (0)
24068
24069
24070 /* Add a glyph string for a sequence of character glyphs to the list
24071 of strings between HEAD and TAIL. START is the index of the first
24072 glyph in row area AREA of glyph row ROW that is part of the new
24073 glyph string. END is the index of the last glyph in that glyph row
24074 area. X is the current output position assigned to the new glyph
24075 string constructed. HL overrides that face of the glyph; e.g. it
24076 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24077 right-most x-position of the drawing area. */
24078
24079 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24080 do \
24081 { \
24082 int face_id; \
24083 XChar2b *char2b; \
24084 \
24085 face_id = (row)->glyphs[area][START].face_id; \
24086 \
24087 s = alloca (sizeof *s); \
24088 char2b = alloca ((END - START) * sizeof *char2b); \
24089 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24090 append_glyph_string (&HEAD, &TAIL, s); \
24091 s->x = (X); \
24092 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24093 } \
24094 while (0)
24095
24096
24097 /* Add a glyph string for a composite sequence to the list of strings
24098 between HEAD and TAIL. START is the index of the first glyph in
24099 row area AREA of glyph row ROW that is part of the new glyph
24100 string. END is the index of the last glyph in that glyph row area.
24101 X is the current output position assigned to the new glyph string
24102 constructed. HL overrides that face of the glyph; e.g. it is
24103 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24104 x-position of the drawing area. */
24105
24106 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24107 do { \
24108 int face_id = (row)->glyphs[area][START].face_id; \
24109 struct face *base_face = FACE_FROM_ID (f, face_id); \
24110 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24111 struct composition *cmp = composition_table[cmp_id]; \
24112 XChar2b *char2b; \
24113 struct glyph_string *first_s = NULL; \
24114 int n; \
24115 \
24116 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24117 \
24118 /* Make glyph_strings for each glyph sequence that is drawable by \
24119 the same face, and append them to HEAD/TAIL. */ \
24120 for (n = 0; n < cmp->glyph_len;) \
24121 { \
24122 s = alloca (sizeof *s); \
24123 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24124 append_glyph_string (&(HEAD), &(TAIL), s); \
24125 s->cmp = cmp; \
24126 s->cmp_from = n; \
24127 s->x = (X); \
24128 if (n == 0) \
24129 first_s = s; \
24130 n = fill_composite_glyph_string (s, base_face, overlaps); \
24131 } \
24132 \
24133 ++START; \
24134 s = first_s; \
24135 } while (0)
24136
24137
24138 /* Add a glyph string for a glyph-string sequence to the list of strings
24139 between HEAD and TAIL. */
24140
24141 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24142 do { \
24143 int face_id; \
24144 XChar2b *char2b; \
24145 Lisp_Object gstring; \
24146 \
24147 face_id = (row)->glyphs[area][START].face_id; \
24148 gstring = (composition_gstring_from_id \
24149 ((row)->glyphs[area][START].u.cmp.id)); \
24150 s = alloca (sizeof *s); \
24151 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24152 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24153 append_glyph_string (&(HEAD), &(TAIL), s); \
24154 s->x = (X); \
24155 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24156 } while (0)
24157
24158
24159 /* Add a glyph string for a sequence of glyphless character's glyphs
24160 to the list of strings between HEAD and TAIL. The meanings of
24161 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24162
24163 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24164 do \
24165 { \
24166 int face_id; \
24167 \
24168 face_id = (row)->glyphs[area][START].face_id; \
24169 \
24170 s = alloca (sizeof *s); \
24171 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24172 append_glyph_string (&HEAD, &TAIL, s); \
24173 s->x = (X); \
24174 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24175 overlaps); \
24176 } \
24177 while (0)
24178
24179
24180 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24181 of AREA of glyph row ROW on window W between indices START and END.
24182 HL overrides the face for drawing glyph strings, e.g. it is
24183 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24184 x-positions of the drawing area.
24185
24186 This is an ugly monster macro construct because we must use alloca
24187 to allocate glyph strings (because draw_glyphs can be called
24188 asynchronously). */
24189
24190 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24191 do \
24192 { \
24193 HEAD = TAIL = NULL; \
24194 while (START < END) \
24195 { \
24196 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24197 switch (first_glyph->type) \
24198 { \
24199 case CHAR_GLYPH: \
24200 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24201 HL, X, LAST_X); \
24202 break; \
24203 \
24204 case COMPOSITE_GLYPH: \
24205 if (first_glyph->u.cmp.automatic) \
24206 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24207 HL, X, LAST_X); \
24208 else \
24209 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24210 HL, X, LAST_X); \
24211 break; \
24212 \
24213 case STRETCH_GLYPH: \
24214 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24215 HL, X, LAST_X); \
24216 break; \
24217 \
24218 case IMAGE_GLYPH: \
24219 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24220 HL, X, LAST_X); \
24221 break; \
24222 \
24223 case GLYPHLESS_GLYPH: \
24224 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24225 HL, X, LAST_X); \
24226 break; \
24227 \
24228 default: \
24229 emacs_abort (); \
24230 } \
24231 \
24232 if (s) \
24233 { \
24234 set_glyph_string_background_width (s, START, LAST_X); \
24235 (X) += s->width; \
24236 } \
24237 } \
24238 } while (0)
24239
24240
24241 /* Draw glyphs between START and END in AREA of ROW on window W,
24242 starting at x-position X. X is relative to AREA in W. HL is a
24243 face-override with the following meaning:
24244
24245 DRAW_NORMAL_TEXT draw normally
24246 DRAW_CURSOR draw in cursor face
24247 DRAW_MOUSE_FACE draw in mouse face.
24248 DRAW_INVERSE_VIDEO draw in mode line face
24249 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24250 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24251
24252 If OVERLAPS is non-zero, draw only the foreground of characters and
24253 clip to the physical height of ROW. Non-zero value also defines
24254 the overlapping part to be drawn:
24255
24256 OVERLAPS_PRED overlap with preceding rows
24257 OVERLAPS_SUCC overlap with succeeding rows
24258 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24259 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24260
24261 Value is the x-position reached, relative to AREA of W. */
24262
24263 static int
24264 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24265 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24266 enum draw_glyphs_face hl, int overlaps)
24267 {
24268 struct glyph_string *head, *tail;
24269 struct glyph_string *s;
24270 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24271 int i, j, x_reached, last_x, area_left = 0;
24272 struct frame *f = XFRAME (WINDOW_FRAME (w));
24273 DECLARE_HDC (hdc);
24274
24275 ALLOCATE_HDC (hdc, f);
24276
24277 /* Let's rather be paranoid than getting a SEGV. */
24278 end = min (end, row->used[area]);
24279 start = clip_to_bounds (0, start, end);
24280
24281 /* Translate X to frame coordinates. Set last_x to the right
24282 end of the drawing area. */
24283 if (row->full_width_p)
24284 {
24285 /* X is relative to the left edge of W, without scroll bars
24286 or fringes. */
24287 area_left = WINDOW_LEFT_EDGE_X (w);
24288 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24289 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24290 }
24291 else
24292 {
24293 area_left = window_box_left (w, area);
24294 last_x = area_left + window_box_width (w, area);
24295 }
24296 x += area_left;
24297
24298 /* Build a doubly-linked list of glyph_string structures between
24299 head and tail from what we have to draw. Note that the macro
24300 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24301 the reason we use a separate variable `i'. */
24302 i = start;
24303 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24304 if (tail)
24305 x_reached = tail->x + tail->background_width;
24306 else
24307 x_reached = x;
24308
24309 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24310 the row, redraw some glyphs in front or following the glyph
24311 strings built above. */
24312 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24313 {
24314 struct glyph_string *h, *t;
24315 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24316 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24317 int check_mouse_face = 0;
24318 int dummy_x = 0;
24319
24320 /* If mouse highlighting is on, we may need to draw adjacent
24321 glyphs using mouse-face highlighting. */
24322 if (area == TEXT_AREA && row->mouse_face_p
24323 && hlinfo->mouse_face_beg_row >= 0
24324 && hlinfo->mouse_face_end_row >= 0)
24325 {
24326 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24327
24328 if (row_vpos >= hlinfo->mouse_face_beg_row
24329 && row_vpos <= hlinfo->mouse_face_end_row)
24330 {
24331 check_mouse_face = 1;
24332 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24333 ? hlinfo->mouse_face_beg_col : 0;
24334 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24335 ? hlinfo->mouse_face_end_col
24336 : row->used[TEXT_AREA];
24337 }
24338 }
24339
24340 /* Compute overhangs for all glyph strings. */
24341 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24342 for (s = head; s; s = s->next)
24343 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24344
24345 /* Prepend glyph strings for glyphs in front of the first glyph
24346 string that are overwritten because of the first glyph
24347 string's left overhang. The background of all strings
24348 prepended must be drawn because the first glyph string
24349 draws over it. */
24350 i = left_overwritten (head);
24351 if (i >= 0)
24352 {
24353 enum draw_glyphs_face overlap_hl;
24354
24355 /* If this row contains mouse highlighting, attempt to draw
24356 the overlapped glyphs with the correct highlight. This
24357 code fails if the overlap encompasses more than one glyph
24358 and mouse-highlight spans only some of these glyphs.
24359 However, making it work perfectly involves a lot more
24360 code, and I don't know if the pathological case occurs in
24361 practice, so we'll stick to this for now. --- cyd */
24362 if (check_mouse_face
24363 && mouse_beg_col < start && mouse_end_col > i)
24364 overlap_hl = DRAW_MOUSE_FACE;
24365 else
24366 overlap_hl = DRAW_NORMAL_TEXT;
24367
24368 j = i;
24369 BUILD_GLYPH_STRINGS (j, start, h, t,
24370 overlap_hl, dummy_x, last_x);
24371 start = i;
24372 compute_overhangs_and_x (t, head->x, 1);
24373 prepend_glyph_string_lists (&head, &tail, h, t);
24374 clip_head = head;
24375 }
24376
24377 /* Prepend glyph strings for glyphs in front of the first glyph
24378 string that overwrite that glyph string because of their
24379 right overhang. For these strings, only the foreground must
24380 be drawn, because it draws over the glyph string at `head'.
24381 The background must not be drawn because this would overwrite
24382 right overhangs of preceding glyphs for which no glyph
24383 strings exist. */
24384 i = left_overwriting (head);
24385 if (i >= 0)
24386 {
24387 enum draw_glyphs_face overlap_hl;
24388
24389 if (check_mouse_face
24390 && mouse_beg_col < start && mouse_end_col > i)
24391 overlap_hl = DRAW_MOUSE_FACE;
24392 else
24393 overlap_hl = DRAW_NORMAL_TEXT;
24394
24395 clip_head = head;
24396 BUILD_GLYPH_STRINGS (i, start, h, t,
24397 overlap_hl, dummy_x, last_x);
24398 for (s = h; s; s = s->next)
24399 s->background_filled_p = 1;
24400 compute_overhangs_and_x (t, head->x, 1);
24401 prepend_glyph_string_lists (&head, &tail, h, t);
24402 }
24403
24404 /* Append glyphs strings for glyphs following the last glyph
24405 string tail that are overwritten by tail. The background of
24406 these strings has to be drawn because tail's foreground draws
24407 over it. */
24408 i = right_overwritten (tail);
24409 if (i >= 0)
24410 {
24411 enum draw_glyphs_face overlap_hl;
24412
24413 if (check_mouse_face
24414 && mouse_beg_col < i && mouse_end_col > end)
24415 overlap_hl = DRAW_MOUSE_FACE;
24416 else
24417 overlap_hl = DRAW_NORMAL_TEXT;
24418
24419 BUILD_GLYPH_STRINGS (end, i, h, t,
24420 overlap_hl, x, last_x);
24421 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24422 we don't have `end = i;' here. */
24423 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24424 append_glyph_string_lists (&head, &tail, h, t);
24425 clip_tail = tail;
24426 }
24427
24428 /* Append glyph strings for glyphs following the last glyph
24429 string tail that overwrite tail. The foreground of such
24430 glyphs has to be drawn because it writes into the background
24431 of tail. The background must not be drawn because it could
24432 paint over the foreground of following glyphs. */
24433 i = right_overwriting (tail);
24434 if (i >= 0)
24435 {
24436 enum draw_glyphs_face overlap_hl;
24437 if (check_mouse_face
24438 && mouse_beg_col < i && mouse_end_col > end)
24439 overlap_hl = DRAW_MOUSE_FACE;
24440 else
24441 overlap_hl = DRAW_NORMAL_TEXT;
24442
24443 clip_tail = tail;
24444 i++; /* We must include the Ith glyph. */
24445 BUILD_GLYPH_STRINGS (end, i, h, t,
24446 overlap_hl, x, last_x);
24447 for (s = h; s; s = s->next)
24448 s->background_filled_p = 1;
24449 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24450 append_glyph_string_lists (&head, &tail, h, t);
24451 }
24452 if (clip_head || clip_tail)
24453 for (s = head; s; s = s->next)
24454 {
24455 s->clip_head = clip_head;
24456 s->clip_tail = clip_tail;
24457 }
24458 }
24459
24460 /* Draw all strings. */
24461 for (s = head; s; s = s->next)
24462 FRAME_RIF (f)->draw_glyph_string (s);
24463
24464 #ifndef HAVE_NS
24465 /* When focus a sole frame and move horizontally, this sets on_p to 0
24466 causing a failure to erase prev cursor position. */
24467 if (area == TEXT_AREA
24468 && !row->full_width_p
24469 /* When drawing overlapping rows, only the glyph strings'
24470 foreground is drawn, which doesn't erase a cursor
24471 completely. */
24472 && !overlaps)
24473 {
24474 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24475 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24476 : (tail ? tail->x + tail->background_width : x));
24477 x0 -= area_left;
24478 x1 -= area_left;
24479
24480 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24481 row->y, MATRIX_ROW_BOTTOM_Y (row));
24482 }
24483 #endif
24484
24485 /* Value is the x-position up to which drawn, relative to AREA of W.
24486 This doesn't include parts drawn because of overhangs. */
24487 if (row->full_width_p)
24488 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24489 else
24490 x_reached -= area_left;
24491
24492 RELEASE_HDC (hdc, f);
24493
24494 return x_reached;
24495 }
24496
24497 /* Expand row matrix if too narrow. Don't expand if area
24498 is not present. */
24499
24500 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24501 { \
24502 if (!it->f->fonts_changed \
24503 && (it->glyph_row->glyphs[area] \
24504 < it->glyph_row->glyphs[area + 1])) \
24505 { \
24506 it->w->ncols_scale_factor++; \
24507 it->f->fonts_changed = 1; \
24508 } \
24509 }
24510
24511 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24512 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24513
24514 static void
24515 append_glyph (struct it *it)
24516 {
24517 struct glyph *glyph;
24518 enum glyph_row_area area = it->area;
24519
24520 eassert (it->glyph_row);
24521 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24522
24523 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24524 if (glyph < it->glyph_row->glyphs[area + 1])
24525 {
24526 /* If the glyph row is reversed, we need to prepend the glyph
24527 rather than append it. */
24528 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24529 {
24530 struct glyph *g;
24531
24532 /* Make room for the additional glyph. */
24533 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24534 g[1] = *g;
24535 glyph = it->glyph_row->glyphs[area];
24536 }
24537 glyph->charpos = CHARPOS (it->position);
24538 glyph->object = it->object;
24539 if (it->pixel_width > 0)
24540 {
24541 glyph->pixel_width = it->pixel_width;
24542 glyph->padding_p = 0;
24543 }
24544 else
24545 {
24546 /* Assure at least 1-pixel width. Otherwise, cursor can't
24547 be displayed correctly. */
24548 glyph->pixel_width = 1;
24549 glyph->padding_p = 1;
24550 }
24551 glyph->ascent = it->ascent;
24552 glyph->descent = it->descent;
24553 glyph->voffset = it->voffset;
24554 glyph->type = CHAR_GLYPH;
24555 glyph->avoid_cursor_p = it->avoid_cursor_p;
24556 glyph->multibyte_p = it->multibyte_p;
24557 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24558 {
24559 /* In R2L rows, the left and the right box edges need to be
24560 drawn in reverse direction. */
24561 glyph->right_box_line_p = it->start_of_box_run_p;
24562 glyph->left_box_line_p = it->end_of_box_run_p;
24563 }
24564 else
24565 {
24566 glyph->left_box_line_p = it->start_of_box_run_p;
24567 glyph->right_box_line_p = it->end_of_box_run_p;
24568 }
24569 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24570 || it->phys_descent > it->descent);
24571 glyph->glyph_not_available_p = it->glyph_not_available_p;
24572 glyph->face_id = it->face_id;
24573 glyph->u.ch = it->char_to_display;
24574 glyph->slice.img = null_glyph_slice;
24575 glyph->font_type = FONT_TYPE_UNKNOWN;
24576 if (it->bidi_p)
24577 {
24578 glyph->resolved_level = it->bidi_it.resolved_level;
24579 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24580 emacs_abort ();
24581 glyph->bidi_type = it->bidi_it.type;
24582 }
24583 else
24584 {
24585 glyph->resolved_level = 0;
24586 glyph->bidi_type = UNKNOWN_BT;
24587 }
24588 ++it->glyph_row->used[area];
24589 }
24590 else
24591 IT_EXPAND_MATRIX_WIDTH (it, area);
24592 }
24593
24594 /* Store one glyph for the composition IT->cmp_it.id in
24595 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24596 non-null. */
24597
24598 static void
24599 append_composite_glyph (struct it *it)
24600 {
24601 struct glyph *glyph;
24602 enum glyph_row_area area = it->area;
24603
24604 eassert (it->glyph_row);
24605
24606 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24607 if (glyph < it->glyph_row->glyphs[area + 1])
24608 {
24609 /* If the glyph row is reversed, we need to prepend the glyph
24610 rather than append it. */
24611 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24612 {
24613 struct glyph *g;
24614
24615 /* Make room for the new glyph. */
24616 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24617 g[1] = *g;
24618 glyph = it->glyph_row->glyphs[it->area];
24619 }
24620 glyph->charpos = it->cmp_it.charpos;
24621 glyph->object = it->object;
24622 glyph->pixel_width = it->pixel_width;
24623 glyph->ascent = it->ascent;
24624 glyph->descent = it->descent;
24625 glyph->voffset = it->voffset;
24626 glyph->type = COMPOSITE_GLYPH;
24627 if (it->cmp_it.ch < 0)
24628 {
24629 glyph->u.cmp.automatic = 0;
24630 glyph->u.cmp.id = it->cmp_it.id;
24631 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24632 }
24633 else
24634 {
24635 glyph->u.cmp.automatic = 1;
24636 glyph->u.cmp.id = it->cmp_it.id;
24637 glyph->slice.cmp.from = it->cmp_it.from;
24638 glyph->slice.cmp.to = it->cmp_it.to - 1;
24639 }
24640 glyph->avoid_cursor_p = it->avoid_cursor_p;
24641 glyph->multibyte_p = it->multibyte_p;
24642 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24643 {
24644 /* In R2L rows, the left and the right box edges need to be
24645 drawn in reverse direction. */
24646 glyph->right_box_line_p = it->start_of_box_run_p;
24647 glyph->left_box_line_p = it->end_of_box_run_p;
24648 }
24649 else
24650 {
24651 glyph->left_box_line_p = it->start_of_box_run_p;
24652 glyph->right_box_line_p = it->end_of_box_run_p;
24653 }
24654 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24655 || it->phys_descent > it->descent);
24656 glyph->padding_p = 0;
24657 glyph->glyph_not_available_p = 0;
24658 glyph->face_id = it->face_id;
24659 glyph->font_type = FONT_TYPE_UNKNOWN;
24660 if (it->bidi_p)
24661 {
24662 glyph->resolved_level = it->bidi_it.resolved_level;
24663 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24664 emacs_abort ();
24665 glyph->bidi_type = it->bidi_it.type;
24666 }
24667 ++it->glyph_row->used[area];
24668 }
24669 else
24670 IT_EXPAND_MATRIX_WIDTH (it, area);
24671 }
24672
24673
24674 /* Change IT->ascent and IT->height according to the setting of
24675 IT->voffset. */
24676
24677 static void
24678 take_vertical_position_into_account (struct it *it)
24679 {
24680 if (it->voffset)
24681 {
24682 if (it->voffset < 0)
24683 /* Increase the ascent so that we can display the text higher
24684 in the line. */
24685 it->ascent -= it->voffset;
24686 else
24687 /* Increase the descent so that we can display the text lower
24688 in the line. */
24689 it->descent += it->voffset;
24690 }
24691 }
24692
24693
24694 /* Produce glyphs/get display metrics for the image IT is loaded with.
24695 See the description of struct display_iterator in dispextern.h for
24696 an overview of struct display_iterator. */
24697
24698 static void
24699 produce_image_glyph (struct it *it)
24700 {
24701 struct image *img;
24702 struct face *face;
24703 int glyph_ascent, crop;
24704 struct glyph_slice slice;
24705
24706 eassert (it->what == IT_IMAGE);
24707
24708 face = FACE_FROM_ID (it->f, it->face_id);
24709 eassert (face);
24710 /* Make sure X resources of the face is loaded. */
24711 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24712
24713 if (it->image_id < 0)
24714 {
24715 /* Fringe bitmap. */
24716 it->ascent = it->phys_ascent = 0;
24717 it->descent = it->phys_descent = 0;
24718 it->pixel_width = 0;
24719 it->nglyphs = 0;
24720 return;
24721 }
24722
24723 img = IMAGE_FROM_ID (it->f, it->image_id);
24724 eassert (img);
24725 /* Make sure X resources of the image is loaded. */
24726 prepare_image_for_display (it->f, img);
24727
24728 slice.x = slice.y = 0;
24729 slice.width = img->width;
24730 slice.height = img->height;
24731
24732 if (INTEGERP (it->slice.x))
24733 slice.x = XINT (it->slice.x);
24734 else if (FLOATP (it->slice.x))
24735 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24736
24737 if (INTEGERP (it->slice.y))
24738 slice.y = XINT (it->slice.y);
24739 else if (FLOATP (it->slice.y))
24740 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24741
24742 if (INTEGERP (it->slice.width))
24743 slice.width = XINT (it->slice.width);
24744 else if (FLOATP (it->slice.width))
24745 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24746
24747 if (INTEGERP (it->slice.height))
24748 slice.height = XINT (it->slice.height);
24749 else if (FLOATP (it->slice.height))
24750 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24751
24752 if (slice.x >= img->width)
24753 slice.x = img->width;
24754 if (slice.y >= img->height)
24755 slice.y = img->height;
24756 if (slice.x + slice.width >= img->width)
24757 slice.width = img->width - slice.x;
24758 if (slice.y + slice.height > img->height)
24759 slice.height = img->height - slice.y;
24760
24761 if (slice.width == 0 || slice.height == 0)
24762 return;
24763
24764 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24765
24766 it->descent = slice.height - glyph_ascent;
24767 if (slice.y == 0)
24768 it->descent += img->vmargin;
24769 if (slice.y + slice.height == img->height)
24770 it->descent += img->vmargin;
24771 it->phys_descent = it->descent;
24772
24773 it->pixel_width = slice.width;
24774 if (slice.x == 0)
24775 it->pixel_width += img->hmargin;
24776 if (slice.x + slice.width == img->width)
24777 it->pixel_width += img->hmargin;
24778
24779 /* It's quite possible for images to have an ascent greater than
24780 their height, so don't get confused in that case. */
24781 if (it->descent < 0)
24782 it->descent = 0;
24783
24784 it->nglyphs = 1;
24785
24786 if (face->box != FACE_NO_BOX)
24787 {
24788 if (face->box_line_width > 0)
24789 {
24790 if (slice.y == 0)
24791 it->ascent += face->box_line_width;
24792 if (slice.y + slice.height == img->height)
24793 it->descent += face->box_line_width;
24794 }
24795
24796 if (it->start_of_box_run_p && slice.x == 0)
24797 it->pixel_width += eabs (face->box_line_width);
24798 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24799 it->pixel_width += eabs (face->box_line_width);
24800 }
24801
24802 take_vertical_position_into_account (it);
24803
24804 /* Automatically crop wide image glyphs at right edge so we can
24805 draw the cursor on same display row. */
24806 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24807 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24808 {
24809 it->pixel_width -= crop;
24810 slice.width -= crop;
24811 }
24812
24813 if (it->glyph_row)
24814 {
24815 struct glyph *glyph;
24816 enum glyph_row_area area = it->area;
24817
24818 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24819 if (glyph < it->glyph_row->glyphs[area + 1])
24820 {
24821 glyph->charpos = CHARPOS (it->position);
24822 glyph->object = it->object;
24823 glyph->pixel_width = it->pixel_width;
24824 glyph->ascent = glyph_ascent;
24825 glyph->descent = it->descent;
24826 glyph->voffset = it->voffset;
24827 glyph->type = IMAGE_GLYPH;
24828 glyph->avoid_cursor_p = it->avoid_cursor_p;
24829 glyph->multibyte_p = it->multibyte_p;
24830 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24831 {
24832 /* In R2L rows, the left and the right box edges need to be
24833 drawn in reverse direction. */
24834 glyph->right_box_line_p = it->start_of_box_run_p;
24835 glyph->left_box_line_p = it->end_of_box_run_p;
24836 }
24837 else
24838 {
24839 glyph->left_box_line_p = it->start_of_box_run_p;
24840 glyph->right_box_line_p = it->end_of_box_run_p;
24841 }
24842 glyph->overlaps_vertically_p = 0;
24843 glyph->padding_p = 0;
24844 glyph->glyph_not_available_p = 0;
24845 glyph->face_id = it->face_id;
24846 glyph->u.img_id = img->id;
24847 glyph->slice.img = slice;
24848 glyph->font_type = FONT_TYPE_UNKNOWN;
24849 if (it->bidi_p)
24850 {
24851 glyph->resolved_level = it->bidi_it.resolved_level;
24852 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24853 emacs_abort ();
24854 glyph->bidi_type = it->bidi_it.type;
24855 }
24856 ++it->glyph_row->used[area];
24857 }
24858 else
24859 IT_EXPAND_MATRIX_WIDTH (it, area);
24860 }
24861 }
24862
24863
24864 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24865 of the glyph, WIDTH and HEIGHT are the width and height of the
24866 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24867
24868 static void
24869 append_stretch_glyph (struct it *it, Lisp_Object object,
24870 int width, int height, int ascent)
24871 {
24872 struct glyph *glyph;
24873 enum glyph_row_area area = it->area;
24874
24875 eassert (ascent >= 0 && ascent <= height);
24876
24877 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24878 if (glyph < it->glyph_row->glyphs[area + 1])
24879 {
24880 /* If the glyph row is reversed, we need to prepend the glyph
24881 rather than append it. */
24882 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24883 {
24884 struct glyph *g;
24885
24886 /* Make room for the additional glyph. */
24887 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24888 g[1] = *g;
24889 glyph = it->glyph_row->glyphs[area];
24890 }
24891 glyph->charpos = CHARPOS (it->position);
24892 glyph->object = object;
24893 glyph->pixel_width = width;
24894 glyph->ascent = ascent;
24895 glyph->descent = height - ascent;
24896 glyph->voffset = it->voffset;
24897 glyph->type = STRETCH_GLYPH;
24898 glyph->avoid_cursor_p = it->avoid_cursor_p;
24899 glyph->multibyte_p = it->multibyte_p;
24900 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24901 {
24902 /* In R2L rows, the left and the right box edges need to be
24903 drawn in reverse direction. */
24904 glyph->right_box_line_p = it->start_of_box_run_p;
24905 glyph->left_box_line_p = it->end_of_box_run_p;
24906 }
24907 else
24908 {
24909 glyph->left_box_line_p = it->start_of_box_run_p;
24910 glyph->right_box_line_p = it->end_of_box_run_p;
24911 }
24912 glyph->overlaps_vertically_p = 0;
24913 glyph->padding_p = 0;
24914 glyph->glyph_not_available_p = 0;
24915 glyph->face_id = it->face_id;
24916 glyph->u.stretch.ascent = ascent;
24917 glyph->u.stretch.height = height;
24918 glyph->slice.img = null_glyph_slice;
24919 glyph->font_type = FONT_TYPE_UNKNOWN;
24920 if (it->bidi_p)
24921 {
24922 glyph->resolved_level = it->bidi_it.resolved_level;
24923 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24924 emacs_abort ();
24925 glyph->bidi_type = it->bidi_it.type;
24926 }
24927 else
24928 {
24929 glyph->resolved_level = 0;
24930 glyph->bidi_type = UNKNOWN_BT;
24931 }
24932 ++it->glyph_row->used[area];
24933 }
24934 else
24935 IT_EXPAND_MATRIX_WIDTH (it, area);
24936 }
24937
24938 #endif /* HAVE_WINDOW_SYSTEM */
24939
24940 /* Produce a stretch glyph for iterator IT. IT->object is the value
24941 of the glyph property displayed. The value must be a list
24942 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24943 being recognized:
24944
24945 1. `:width WIDTH' specifies that the space should be WIDTH *
24946 canonical char width wide. WIDTH may be an integer or floating
24947 point number.
24948
24949 2. `:relative-width FACTOR' specifies that the width of the stretch
24950 should be computed from the width of the first character having the
24951 `glyph' property, and should be FACTOR times that width.
24952
24953 3. `:align-to HPOS' specifies that the space should be wide enough
24954 to reach HPOS, a value in canonical character units.
24955
24956 Exactly one of the above pairs must be present.
24957
24958 4. `:height HEIGHT' specifies that the height of the stretch produced
24959 should be HEIGHT, measured in canonical character units.
24960
24961 5. `:relative-height FACTOR' specifies that the height of the
24962 stretch should be FACTOR times the height of the characters having
24963 the glyph property.
24964
24965 Either none or exactly one of 4 or 5 must be present.
24966
24967 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24968 of the stretch should be used for the ascent of the stretch.
24969 ASCENT must be in the range 0 <= ASCENT <= 100. */
24970
24971 void
24972 produce_stretch_glyph (struct it *it)
24973 {
24974 /* (space :width WIDTH :height HEIGHT ...) */
24975 Lisp_Object prop, plist;
24976 int width = 0, height = 0, align_to = -1;
24977 int zero_width_ok_p = 0;
24978 double tem;
24979 struct font *font = NULL;
24980
24981 #ifdef HAVE_WINDOW_SYSTEM
24982 int ascent = 0;
24983 int zero_height_ok_p = 0;
24984
24985 if (FRAME_WINDOW_P (it->f))
24986 {
24987 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24988 font = face->font ? face->font : FRAME_FONT (it->f);
24989 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24990 }
24991 #endif
24992
24993 /* List should start with `space'. */
24994 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24995 plist = XCDR (it->object);
24996
24997 /* Compute the width of the stretch. */
24998 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24999 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25000 {
25001 /* Absolute width `:width WIDTH' specified and valid. */
25002 zero_width_ok_p = 1;
25003 width = (int)tem;
25004 }
25005 #ifdef HAVE_WINDOW_SYSTEM
25006 else if (FRAME_WINDOW_P (it->f)
25007 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25008 {
25009 /* Relative width `:relative-width FACTOR' specified and valid.
25010 Compute the width of the characters having the `glyph'
25011 property. */
25012 struct it it2;
25013 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25014
25015 it2 = *it;
25016 if (it->multibyte_p)
25017 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25018 else
25019 {
25020 it2.c = it2.char_to_display = *p, it2.len = 1;
25021 if (! ASCII_CHAR_P (it2.c))
25022 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25023 }
25024
25025 it2.glyph_row = NULL;
25026 it2.what = IT_CHARACTER;
25027 x_produce_glyphs (&it2);
25028 width = NUMVAL (prop) * it2.pixel_width;
25029 }
25030 #endif /* HAVE_WINDOW_SYSTEM */
25031 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25032 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25033 {
25034 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25035 align_to = (align_to < 0
25036 ? 0
25037 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25038 else if (align_to < 0)
25039 align_to = window_box_left_offset (it->w, TEXT_AREA);
25040 width = max (0, (int)tem + align_to - it->current_x);
25041 zero_width_ok_p = 1;
25042 }
25043 else
25044 /* Nothing specified -> width defaults to canonical char width. */
25045 width = FRAME_COLUMN_WIDTH (it->f);
25046
25047 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25048 width = 1;
25049
25050 #ifdef HAVE_WINDOW_SYSTEM
25051 /* Compute height. */
25052 if (FRAME_WINDOW_P (it->f))
25053 {
25054 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25055 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25056 {
25057 height = (int)tem;
25058 zero_height_ok_p = 1;
25059 }
25060 else if (prop = Fplist_get (plist, QCrelative_height),
25061 NUMVAL (prop) > 0)
25062 height = FONT_HEIGHT (font) * NUMVAL (prop);
25063 else
25064 height = FONT_HEIGHT (font);
25065
25066 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25067 height = 1;
25068
25069 /* Compute percentage of height used for ascent. If
25070 `:ascent ASCENT' is present and valid, use that. Otherwise,
25071 derive the ascent from the font in use. */
25072 if (prop = Fplist_get (plist, QCascent),
25073 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25074 ascent = height * NUMVAL (prop) / 100.0;
25075 else if (!NILP (prop)
25076 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25077 ascent = min (max (0, (int)tem), height);
25078 else
25079 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25080 }
25081 else
25082 #endif /* HAVE_WINDOW_SYSTEM */
25083 height = 1;
25084
25085 if (width > 0 && it->line_wrap != TRUNCATE
25086 && it->current_x + width > it->last_visible_x)
25087 {
25088 width = it->last_visible_x - it->current_x;
25089 #ifdef HAVE_WINDOW_SYSTEM
25090 /* Subtract one more pixel from the stretch width, but only on
25091 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25092 width -= FRAME_WINDOW_P (it->f);
25093 #endif
25094 }
25095
25096 if (width > 0 && height > 0 && it->glyph_row)
25097 {
25098 Lisp_Object o_object = it->object;
25099 Lisp_Object object = it->stack[it->sp - 1].string;
25100 int n = width;
25101
25102 if (!STRINGP (object))
25103 object = it->w->contents;
25104 #ifdef HAVE_WINDOW_SYSTEM
25105 if (FRAME_WINDOW_P (it->f))
25106 append_stretch_glyph (it, object, width, height, ascent);
25107 else
25108 #endif
25109 {
25110 it->object = object;
25111 it->char_to_display = ' ';
25112 it->pixel_width = it->len = 1;
25113 while (n--)
25114 tty_append_glyph (it);
25115 it->object = o_object;
25116 }
25117 }
25118
25119 it->pixel_width = width;
25120 #ifdef HAVE_WINDOW_SYSTEM
25121 if (FRAME_WINDOW_P (it->f))
25122 {
25123 it->ascent = it->phys_ascent = ascent;
25124 it->descent = it->phys_descent = height - it->ascent;
25125 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25126 take_vertical_position_into_account (it);
25127 }
25128 else
25129 #endif
25130 it->nglyphs = width;
25131 }
25132
25133 /* Get information about special display element WHAT in an
25134 environment described by IT. WHAT is one of IT_TRUNCATION or
25135 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25136 non-null glyph_row member. This function ensures that fields like
25137 face_id, c, len of IT are left untouched. */
25138
25139 static void
25140 produce_special_glyphs (struct it *it, enum display_element_type what)
25141 {
25142 struct it temp_it;
25143 Lisp_Object gc;
25144 GLYPH glyph;
25145
25146 temp_it = *it;
25147 temp_it.object = make_number (0);
25148 memset (&temp_it.current, 0, sizeof temp_it.current);
25149
25150 if (what == IT_CONTINUATION)
25151 {
25152 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25153 if (it->bidi_it.paragraph_dir == R2L)
25154 SET_GLYPH_FROM_CHAR (glyph, '/');
25155 else
25156 SET_GLYPH_FROM_CHAR (glyph, '\\');
25157 if (it->dp
25158 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25159 {
25160 /* FIXME: Should we mirror GC for R2L lines? */
25161 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25162 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25163 }
25164 }
25165 else if (what == IT_TRUNCATION)
25166 {
25167 /* Truncation glyph. */
25168 SET_GLYPH_FROM_CHAR (glyph, '$');
25169 if (it->dp
25170 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25171 {
25172 /* FIXME: Should we mirror GC for R2L lines? */
25173 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25174 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25175 }
25176 }
25177 else
25178 emacs_abort ();
25179
25180 #ifdef HAVE_WINDOW_SYSTEM
25181 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25182 is turned off, we precede the truncation/continuation glyphs by a
25183 stretch glyph whose width is computed such that these special
25184 glyphs are aligned at the window margin, even when very different
25185 fonts are used in different glyph rows. */
25186 if (FRAME_WINDOW_P (temp_it.f)
25187 /* init_iterator calls this with it->glyph_row == NULL, and it
25188 wants only the pixel width of the truncation/continuation
25189 glyphs. */
25190 && temp_it.glyph_row
25191 /* insert_left_trunc_glyphs calls us at the beginning of the
25192 row, and it has its own calculation of the stretch glyph
25193 width. */
25194 && temp_it.glyph_row->used[TEXT_AREA] > 0
25195 && (temp_it.glyph_row->reversed_p
25196 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25197 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25198 {
25199 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25200
25201 if (stretch_width > 0)
25202 {
25203 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25204 struct font *font =
25205 face->font ? face->font : FRAME_FONT (temp_it.f);
25206 int stretch_ascent =
25207 (((temp_it.ascent + temp_it.descent)
25208 * FONT_BASE (font)) / FONT_HEIGHT (font));
25209
25210 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25211 temp_it.ascent + temp_it.descent,
25212 stretch_ascent);
25213 }
25214 }
25215 #endif
25216
25217 temp_it.dp = NULL;
25218 temp_it.what = IT_CHARACTER;
25219 temp_it.len = 1;
25220 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25221 temp_it.face_id = GLYPH_FACE (glyph);
25222 temp_it.len = CHAR_BYTES (temp_it.c);
25223
25224 PRODUCE_GLYPHS (&temp_it);
25225 it->pixel_width = temp_it.pixel_width;
25226 it->nglyphs = temp_it.pixel_width;
25227 }
25228
25229 #ifdef HAVE_WINDOW_SYSTEM
25230
25231 /* Calculate line-height and line-spacing properties.
25232 An integer value specifies explicit pixel value.
25233 A float value specifies relative value to current face height.
25234 A cons (float . face-name) specifies relative value to
25235 height of specified face font.
25236
25237 Returns height in pixels, or nil. */
25238
25239
25240 static Lisp_Object
25241 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25242 int boff, int override)
25243 {
25244 Lisp_Object face_name = Qnil;
25245 int ascent, descent, height;
25246
25247 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25248 return val;
25249
25250 if (CONSP (val))
25251 {
25252 face_name = XCAR (val);
25253 val = XCDR (val);
25254 if (!NUMBERP (val))
25255 val = make_number (1);
25256 if (NILP (face_name))
25257 {
25258 height = it->ascent + it->descent;
25259 goto scale;
25260 }
25261 }
25262
25263 if (NILP (face_name))
25264 {
25265 font = FRAME_FONT (it->f);
25266 boff = FRAME_BASELINE_OFFSET (it->f);
25267 }
25268 else if (EQ (face_name, Qt))
25269 {
25270 override = 0;
25271 }
25272 else
25273 {
25274 int face_id;
25275 struct face *face;
25276
25277 face_id = lookup_named_face (it->f, face_name, 0);
25278 if (face_id < 0)
25279 return make_number (-1);
25280
25281 face = FACE_FROM_ID (it->f, face_id);
25282 font = face->font;
25283 if (font == NULL)
25284 return make_number (-1);
25285 boff = font->baseline_offset;
25286 if (font->vertical_centering)
25287 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25288 }
25289
25290 ascent = FONT_BASE (font) + boff;
25291 descent = FONT_DESCENT (font) - boff;
25292
25293 if (override)
25294 {
25295 it->override_ascent = ascent;
25296 it->override_descent = descent;
25297 it->override_boff = boff;
25298 }
25299
25300 height = ascent + descent;
25301
25302 scale:
25303 if (FLOATP (val))
25304 height = (int)(XFLOAT_DATA (val) * height);
25305 else if (INTEGERP (val))
25306 height *= XINT (val);
25307
25308 return make_number (height);
25309 }
25310
25311
25312 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25313 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25314 and only if this is for a character for which no font was found.
25315
25316 If the display method (it->glyphless_method) is
25317 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25318 length of the acronym or the hexadecimal string, UPPER_XOFF and
25319 UPPER_YOFF are pixel offsets for the upper part of the string,
25320 LOWER_XOFF and LOWER_YOFF are for the lower part.
25321
25322 For the other display methods, LEN through LOWER_YOFF are zero. */
25323
25324 static void
25325 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25326 short upper_xoff, short upper_yoff,
25327 short lower_xoff, short lower_yoff)
25328 {
25329 struct glyph *glyph;
25330 enum glyph_row_area area = it->area;
25331
25332 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25333 if (glyph < it->glyph_row->glyphs[area + 1])
25334 {
25335 /* If the glyph row is reversed, we need to prepend the glyph
25336 rather than append it. */
25337 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25338 {
25339 struct glyph *g;
25340
25341 /* Make room for the additional glyph. */
25342 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25343 g[1] = *g;
25344 glyph = it->glyph_row->glyphs[area];
25345 }
25346 glyph->charpos = CHARPOS (it->position);
25347 glyph->object = it->object;
25348 glyph->pixel_width = it->pixel_width;
25349 glyph->ascent = it->ascent;
25350 glyph->descent = it->descent;
25351 glyph->voffset = it->voffset;
25352 glyph->type = GLYPHLESS_GLYPH;
25353 glyph->u.glyphless.method = it->glyphless_method;
25354 glyph->u.glyphless.for_no_font = for_no_font;
25355 glyph->u.glyphless.len = len;
25356 glyph->u.glyphless.ch = it->c;
25357 glyph->slice.glyphless.upper_xoff = upper_xoff;
25358 glyph->slice.glyphless.upper_yoff = upper_yoff;
25359 glyph->slice.glyphless.lower_xoff = lower_xoff;
25360 glyph->slice.glyphless.lower_yoff = lower_yoff;
25361 glyph->avoid_cursor_p = it->avoid_cursor_p;
25362 glyph->multibyte_p = it->multibyte_p;
25363 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25364 {
25365 /* In R2L rows, the left and the right box edges need to be
25366 drawn in reverse direction. */
25367 glyph->right_box_line_p = it->start_of_box_run_p;
25368 glyph->left_box_line_p = it->end_of_box_run_p;
25369 }
25370 else
25371 {
25372 glyph->left_box_line_p = it->start_of_box_run_p;
25373 glyph->right_box_line_p = it->end_of_box_run_p;
25374 }
25375 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25376 || it->phys_descent > it->descent);
25377 glyph->padding_p = 0;
25378 glyph->glyph_not_available_p = 0;
25379 glyph->face_id = face_id;
25380 glyph->font_type = FONT_TYPE_UNKNOWN;
25381 if (it->bidi_p)
25382 {
25383 glyph->resolved_level = it->bidi_it.resolved_level;
25384 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25385 emacs_abort ();
25386 glyph->bidi_type = it->bidi_it.type;
25387 }
25388 ++it->glyph_row->used[area];
25389 }
25390 else
25391 IT_EXPAND_MATRIX_WIDTH (it, area);
25392 }
25393
25394
25395 /* Produce a glyph for a glyphless character for iterator IT.
25396 IT->glyphless_method specifies which method to use for displaying
25397 the character. See the description of enum
25398 glyphless_display_method in dispextern.h for the detail.
25399
25400 FOR_NO_FONT is nonzero if and only if this is for a character for
25401 which no font was found. ACRONYM, if non-nil, is an acronym string
25402 for the character. */
25403
25404 static void
25405 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25406 {
25407 int face_id;
25408 struct face *face;
25409 struct font *font;
25410 int base_width, base_height, width, height;
25411 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25412 int len;
25413
25414 /* Get the metrics of the base font. We always refer to the current
25415 ASCII face. */
25416 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25417 font = face->font ? face->font : FRAME_FONT (it->f);
25418 it->ascent = FONT_BASE (font) + font->baseline_offset;
25419 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25420 base_height = it->ascent + it->descent;
25421 base_width = font->average_width;
25422
25423 face_id = merge_glyphless_glyph_face (it);
25424
25425 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25426 {
25427 it->pixel_width = THIN_SPACE_WIDTH;
25428 len = 0;
25429 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25430 }
25431 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25432 {
25433 width = CHAR_WIDTH (it->c);
25434 if (width == 0)
25435 width = 1;
25436 else if (width > 4)
25437 width = 4;
25438 it->pixel_width = base_width * width;
25439 len = 0;
25440 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25441 }
25442 else
25443 {
25444 char buf[7];
25445 const char *str;
25446 unsigned int code[6];
25447 int upper_len;
25448 int ascent, descent;
25449 struct font_metrics metrics_upper, metrics_lower;
25450
25451 face = FACE_FROM_ID (it->f, face_id);
25452 font = face->font ? face->font : FRAME_FONT (it->f);
25453 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25454
25455 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25456 {
25457 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25458 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25459 if (CONSP (acronym))
25460 acronym = XCAR (acronym);
25461 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25462 }
25463 else
25464 {
25465 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25466 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25467 str = buf;
25468 }
25469 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25470 code[len] = font->driver->encode_char (font, str[len]);
25471 upper_len = (len + 1) / 2;
25472 font->driver->text_extents (font, code, upper_len,
25473 &metrics_upper);
25474 font->driver->text_extents (font, code + upper_len, len - upper_len,
25475 &metrics_lower);
25476
25477
25478
25479 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25480 width = max (metrics_upper.width, metrics_lower.width) + 4;
25481 upper_xoff = upper_yoff = 2; /* the typical case */
25482 if (base_width >= width)
25483 {
25484 /* Align the upper to the left, the lower to the right. */
25485 it->pixel_width = base_width;
25486 lower_xoff = base_width - 2 - metrics_lower.width;
25487 }
25488 else
25489 {
25490 /* Center the shorter one. */
25491 it->pixel_width = width;
25492 if (metrics_upper.width >= metrics_lower.width)
25493 lower_xoff = (width - metrics_lower.width) / 2;
25494 else
25495 {
25496 /* FIXME: This code doesn't look right. It formerly was
25497 missing the "lower_xoff = 0;", which couldn't have
25498 been right since it left lower_xoff uninitialized. */
25499 lower_xoff = 0;
25500 upper_xoff = (width - metrics_upper.width) / 2;
25501 }
25502 }
25503
25504 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25505 top, bottom, and between upper and lower strings. */
25506 height = (metrics_upper.ascent + metrics_upper.descent
25507 + metrics_lower.ascent + metrics_lower.descent) + 5;
25508 /* Center vertically.
25509 H:base_height, D:base_descent
25510 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25511
25512 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25513 descent = D - H/2 + h/2;
25514 lower_yoff = descent - 2 - ld;
25515 upper_yoff = lower_yoff - la - 1 - ud; */
25516 ascent = - (it->descent - (base_height + height + 1) / 2);
25517 descent = it->descent - (base_height - height) / 2;
25518 lower_yoff = descent - 2 - metrics_lower.descent;
25519 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25520 - metrics_upper.descent);
25521 /* Don't make the height shorter than the base height. */
25522 if (height > base_height)
25523 {
25524 it->ascent = ascent;
25525 it->descent = descent;
25526 }
25527 }
25528
25529 it->phys_ascent = it->ascent;
25530 it->phys_descent = it->descent;
25531 if (it->glyph_row)
25532 append_glyphless_glyph (it, face_id, for_no_font, len,
25533 upper_xoff, upper_yoff,
25534 lower_xoff, lower_yoff);
25535 it->nglyphs = 1;
25536 take_vertical_position_into_account (it);
25537 }
25538
25539
25540 /* RIF:
25541 Produce glyphs/get display metrics for the display element IT is
25542 loaded with. See the description of struct it in dispextern.h
25543 for an overview of struct it. */
25544
25545 void
25546 x_produce_glyphs (struct it *it)
25547 {
25548 int extra_line_spacing = it->extra_line_spacing;
25549
25550 it->glyph_not_available_p = 0;
25551
25552 if (it->what == IT_CHARACTER)
25553 {
25554 XChar2b char2b;
25555 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25556 struct font *font = face->font;
25557 struct font_metrics *pcm = NULL;
25558 int boff; /* Baseline offset. */
25559
25560 if (font == NULL)
25561 {
25562 /* When no suitable font is found, display this character by
25563 the method specified in the first extra slot of
25564 Vglyphless_char_display. */
25565 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25566
25567 eassert (it->what == IT_GLYPHLESS);
25568 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25569 goto done;
25570 }
25571
25572 boff = font->baseline_offset;
25573 if (font->vertical_centering)
25574 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25575
25576 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25577 {
25578 int stretched_p;
25579
25580 it->nglyphs = 1;
25581
25582 if (it->override_ascent >= 0)
25583 {
25584 it->ascent = it->override_ascent;
25585 it->descent = it->override_descent;
25586 boff = it->override_boff;
25587 }
25588 else
25589 {
25590 it->ascent = FONT_BASE (font) + boff;
25591 it->descent = FONT_DESCENT (font) - boff;
25592 }
25593
25594 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25595 {
25596 pcm = get_per_char_metric (font, &char2b);
25597 if (pcm->width == 0
25598 && pcm->rbearing == 0 && pcm->lbearing == 0)
25599 pcm = NULL;
25600 }
25601
25602 if (pcm)
25603 {
25604 it->phys_ascent = pcm->ascent + boff;
25605 it->phys_descent = pcm->descent - boff;
25606 it->pixel_width = pcm->width;
25607 }
25608 else
25609 {
25610 it->glyph_not_available_p = 1;
25611 it->phys_ascent = it->ascent;
25612 it->phys_descent = it->descent;
25613 it->pixel_width = font->space_width;
25614 }
25615
25616 if (it->constrain_row_ascent_descent_p)
25617 {
25618 if (it->descent > it->max_descent)
25619 {
25620 it->ascent += it->descent - it->max_descent;
25621 it->descent = it->max_descent;
25622 }
25623 if (it->ascent > it->max_ascent)
25624 {
25625 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25626 it->ascent = it->max_ascent;
25627 }
25628 it->phys_ascent = min (it->phys_ascent, it->ascent);
25629 it->phys_descent = min (it->phys_descent, it->descent);
25630 extra_line_spacing = 0;
25631 }
25632
25633 /* If this is a space inside a region of text with
25634 `space-width' property, change its width. */
25635 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25636 if (stretched_p)
25637 it->pixel_width *= XFLOATINT (it->space_width);
25638
25639 /* If face has a box, add the box thickness to the character
25640 height. If character has a box line to the left and/or
25641 right, add the box line width to the character's width. */
25642 if (face->box != FACE_NO_BOX)
25643 {
25644 int thick = face->box_line_width;
25645
25646 if (thick > 0)
25647 {
25648 it->ascent += thick;
25649 it->descent += thick;
25650 }
25651 else
25652 thick = -thick;
25653
25654 if (it->start_of_box_run_p)
25655 it->pixel_width += thick;
25656 if (it->end_of_box_run_p)
25657 it->pixel_width += thick;
25658 }
25659
25660 /* If face has an overline, add the height of the overline
25661 (1 pixel) and a 1 pixel margin to the character height. */
25662 if (face->overline_p)
25663 it->ascent += overline_margin;
25664
25665 if (it->constrain_row_ascent_descent_p)
25666 {
25667 if (it->ascent > it->max_ascent)
25668 it->ascent = it->max_ascent;
25669 if (it->descent > it->max_descent)
25670 it->descent = it->max_descent;
25671 }
25672
25673 take_vertical_position_into_account (it);
25674
25675 /* If we have to actually produce glyphs, do it. */
25676 if (it->glyph_row)
25677 {
25678 if (stretched_p)
25679 {
25680 /* Translate a space with a `space-width' property
25681 into a stretch glyph. */
25682 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25683 / FONT_HEIGHT (font));
25684 append_stretch_glyph (it, it->object, it->pixel_width,
25685 it->ascent + it->descent, ascent);
25686 }
25687 else
25688 append_glyph (it);
25689
25690 /* If characters with lbearing or rbearing are displayed
25691 in this line, record that fact in a flag of the
25692 glyph row. This is used to optimize X output code. */
25693 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25694 it->glyph_row->contains_overlapping_glyphs_p = 1;
25695 }
25696 if (! stretched_p && it->pixel_width == 0)
25697 /* We assure that all visible glyphs have at least 1-pixel
25698 width. */
25699 it->pixel_width = 1;
25700 }
25701 else if (it->char_to_display == '\n')
25702 {
25703 /* A newline has no width, but we need the height of the
25704 line. But if previous part of the line sets a height,
25705 don't increase that height. */
25706
25707 Lisp_Object height;
25708 Lisp_Object total_height = Qnil;
25709
25710 it->override_ascent = -1;
25711 it->pixel_width = 0;
25712 it->nglyphs = 0;
25713
25714 height = get_it_property (it, Qline_height);
25715 /* Split (line-height total-height) list. */
25716 if (CONSP (height)
25717 && CONSP (XCDR (height))
25718 && NILP (XCDR (XCDR (height))))
25719 {
25720 total_height = XCAR (XCDR (height));
25721 height = XCAR (height);
25722 }
25723 height = calc_line_height_property (it, height, font, boff, 1);
25724
25725 if (it->override_ascent >= 0)
25726 {
25727 it->ascent = it->override_ascent;
25728 it->descent = it->override_descent;
25729 boff = it->override_boff;
25730 }
25731 else
25732 {
25733 it->ascent = FONT_BASE (font) + boff;
25734 it->descent = FONT_DESCENT (font) - boff;
25735 }
25736
25737 if (EQ (height, Qt))
25738 {
25739 if (it->descent > it->max_descent)
25740 {
25741 it->ascent += it->descent - it->max_descent;
25742 it->descent = it->max_descent;
25743 }
25744 if (it->ascent > it->max_ascent)
25745 {
25746 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25747 it->ascent = it->max_ascent;
25748 }
25749 it->phys_ascent = min (it->phys_ascent, it->ascent);
25750 it->phys_descent = min (it->phys_descent, it->descent);
25751 it->constrain_row_ascent_descent_p = 1;
25752 extra_line_spacing = 0;
25753 }
25754 else
25755 {
25756 Lisp_Object spacing;
25757
25758 it->phys_ascent = it->ascent;
25759 it->phys_descent = it->descent;
25760
25761 if ((it->max_ascent > 0 || it->max_descent > 0)
25762 && face->box != FACE_NO_BOX
25763 && face->box_line_width > 0)
25764 {
25765 it->ascent += face->box_line_width;
25766 it->descent += face->box_line_width;
25767 }
25768 if (!NILP (height)
25769 && XINT (height) > it->ascent + it->descent)
25770 it->ascent = XINT (height) - it->descent;
25771
25772 if (!NILP (total_height))
25773 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25774 else
25775 {
25776 spacing = get_it_property (it, Qline_spacing);
25777 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25778 }
25779 if (INTEGERP (spacing))
25780 {
25781 extra_line_spacing = XINT (spacing);
25782 if (!NILP (total_height))
25783 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25784 }
25785 }
25786 }
25787 else /* i.e. (it->char_to_display == '\t') */
25788 {
25789 if (font->space_width > 0)
25790 {
25791 int tab_width = it->tab_width * font->space_width;
25792 int x = it->current_x + it->continuation_lines_width;
25793 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25794
25795 /* If the distance from the current position to the next tab
25796 stop is less than a space character width, use the
25797 tab stop after that. */
25798 if (next_tab_x - x < font->space_width)
25799 next_tab_x += tab_width;
25800
25801 it->pixel_width = next_tab_x - x;
25802 it->nglyphs = 1;
25803 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25804 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25805
25806 if (it->glyph_row)
25807 {
25808 append_stretch_glyph (it, it->object, it->pixel_width,
25809 it->ascent + it->descent, it->ascent);
25810 }
25811 }
25812 else
25813 {
25814 it->pixel_width = 0;
25815 it->nglyphs = 1;
25816 }
25817 }
25818 }
25819 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25820 {
25821 /* A static composition.
25822
25823 Note: A composition is represented as one glyph in the
25824 glyph matrix. There are no padding glyphs.
25825
25826 Important note: pixel_width, ascent, and descent are the
25827 values of what is drawn by draw_glyphs (i.e. the values of
25828 the overall glyphs composed). */
25829 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25830 int boff; /* baseline offset */
25831 struct composition *cmp = composition_table[it->cmp_it.id];
25832 int glyph_len = cmp->glyph_len;
25833 struct font *font = face->font;
25834
25835 it->nglyphs = 1;
25836
25837 /* If we have not yet calculated pixel size data of glyphs of
25838 the composition for the current face font, calculate them
25839 now. Theoretically, we have to check all fonts for the
25840 glyphs, but that requires much time and memory space. So,
25841 here we check only the font of the first glyph. This may
25842 lead to incorrect display, but it's very rare, and C-l
25843 (recenter-top-bottom) can correct the display anyway. */
25844 if (! cmp->font || cmp->font != font)
25845 {
25846 /* Ascent and descent of the font of the first character
25847 of this composition (adjusted by baseline offset).
25848 Ascent and descent of overall glyphs should not be less
25849 than these, respectively. */
25850 int font_ascent, font_descent, font_height;
25851 /* Bounding box of the overall glyphs. */
25852 int leftmost, rightmost, lowest, highest;
25853 int lbearing, rbearing;
25854 int i, width, ascent, descent;
25855 int left_padded = 0, right_padded = 0;
25856 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25857 XChar2b char2b;
25858 struct font_metrics *pcm;
25859 int font_not_found_p;
25860 ptrdiff_t pos;
25861
25862 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25863 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25864 break;
25865 if (glyph_len < cmp->glyph_len)
25866 right_padded = 1;
25867 for (i = 0; i < glyph_len; i++)
25868 {
25869 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25870 break;
25871 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25872 }
25873 if (i > 0)
25874 left_padded = 1;
25875
25876 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25877 : IT_CHARPOS (*it));
25878 /* If no suitable font is found, use the default font. */
25879 font_not_found_p = font == NULL;
25880 if (font_not_found_p)
25881 {
25882 face = face->ascii_face;
25883 font = face->font;
25884 }
25885 boff = font->baseline_offset;
25886 if (font->vertical_centering)
25887 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25888 font_ascent = FONT_BASE (font) + boff;
25889 font_descent = FONT_DESCENT (font) - boff;
25890 font_height = FONT_HEIGHT (font);
25891
25892 cmp->font = font;
25893
25894 pcm = NULL;
25895 if (! font_not_found_p)
25896 {
25897 get_char_face_and_encoding (it->f, c, it->face_id,
25898 &char2b, 0);
25899 pcm = get_per_char_metric (font, &char2b);
25900 }
25901
25902 /* Initialize the bounding box. */
25903 if (pcm)
25904 {
25905 width = cmp->glyph_len > 0 ? pcm->width : 0;
25906 ascent = pcm->ascent;
25907 descent = pcm->descent;
25908 lbearing = pcm->lbearing;
25909 rbearing = pcm->rbearing;
25910 }
25911 else
25912 {
25913 width = cmp->glyph_len > 0 ? font->space_width : 0;
25914 ascent = FONT_BASE (font);
25915 descent = FONT_DESCENT (font);
25916 lbearing = 0;
25917 rbearing = width;
25918 }
25919
25920 rightmost = width;
25921 leftmost = 0;
25922 lowest = - descent + boff;
25923 highest = ascent + boff;
25924
25925 if (! font_not_found_p
25926 && font->default_ascent
25927 && CHAR_TABLE_P (Vuse_default_ascent)
25928 && !NILP (Faref (Vuse_default_ascent,
25929 make_number (it->char_to_display))))
25930 highest = font->default_ascent + boff;
25931
25932 /* Draw the first glyph at the normal position. It may be
25933 shifted to right later if some other glyphs are drawn
25934 at the left. */
25935 cmp->offsets[i * 2] = 0;
25936 cmp->offsets[i * 2 + 1] = boff;
25937 cmp->lbearing = lbearing;
25938 cmp->rbearing = rbearing;
25939
25940 /* Set cmp->offsets for the remaining glyphs. */
25941 for (i++; i < glyph_len; i++)
25942 {
25943 int left, right, btm, top;
25944 int ch = COMPOSITION_GLYPH (cmp, i);
25945 int face_id;
25946 struct face *this_face;
25947
25948 if (ch == '\t')
25949 ch = ' ';
25950 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25951 this_face = FACE_FROM_ID (it->f, face_id);
25952 font = this_face->font;
25953
25954 if (font == NULL)
25955 pcm = NULL;
25956 else
25957 {
25958 get_char_face_and_encoding (it->f, ch, face_id,
25959 &char2b, 0);
25960 pcm = get_per_char_metric (font, &char2b);
25961 }
25962 if (! pcm)
25963 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25964 else
25965 {
25966 width = pcm->width;
25967 ascent = pcm->ascent;
25968 descent = pcm->descent;
25969 lbearing = pcm->lbearing;
25970 rbearing = pcm->rbearing;
25971 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25972 {
25973 /* Relative composition with or without
25974 alternate chars. */
25975 left = (leftmost + rightmost - width) / 2;
25976 btm = - descent + boff;
25977 if (font->relative_compose
25978 && (! CHAR_TABLE_P (Vignore_relative_composition)
25979 || NILP (Faref (Vignore_relative_composition,
25980 make_number (ch)))))
25981 {
25982
25983 if (- descent >= font->relative_compose)
25984 /* One extra pixel between two glyphs. */
25985 btm = highest + 1;
25986 else if (ascent <= 0)
25987 /* One extra pixel between two glyphs. */
25988 btm = lowest - 1 - ascent - descent;
25989 }
25990 }
25991 else
25992 {
25993 /* A composition rule is specified by an integer
25994 value that encodes global and new reference
25995 points (GREF and NREF). GREF and NREF are
25996 specified by numbers as below:
25997
25998 0---1---2 -- ascent
25999 | |
26000 | |
26001 | |
26002 9--10--11 -- center
26003 | |
26004 ---3---4---5--- baseline
26005 | |
26006 6---7---8 -- descent
26007 */
26008 int rule = COMPOSITION_RULE (cmp, i);
26009 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26010
26011 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26012 grefx = gref % 3, nrefx = nref % 3;
26013 grefy = gref / 3, nrefy = nref / 3;
26014 if (xoff)
26015 xoff = font_height * (xoff - 128) / 256;
26016 if (yoff)
26017 yoff = font_height * (yoff - 128) / 256;
26018
26019 left = (leftmost
26020 + grefx * (rightmost - leftmost) / 2
26021 - nrefx * width / 2
26022 + xoff);
26023
26024 btm = ((grefy == 0 ? highest
26025 : grefy == 1 ? 0
26026 : grefy == 2 ? lowest
26027 : (highest + lowest) / 2)
26028 - (nrefy == 0 ? ascent + descent
26029 : nrefy == 1 ? descent - boff
26030 : nrefy == 2 ? 0
26031 : (ascent + descent) / 2)
26032 + yoff);
26033 }
26034
26035 cmp->offsets[i * 2] = left;
26036 cmp->offsets[i * 2 + 1] = btm + descent;
26037
26038 /* Update the bounding box of the overall glyphs. */
26039 if (width > 0)
26040 {
26041 right = left + width;
26042 if (left < leftmost)
26043 leftmost = left;
26044 if (right > rightmost)
26045 rightmost = right;
26046 }
26047 top = btm + descent + ascent;
26048 if (top > highest)
26049 highest = top;
26050 if (btm < lowest)
26051 lowest = btm;
26052
26053 if (cmp->lbearing > left + lbearing)
26054 cmp->lbearing = left + lbearing;
26055 if (cmp->rbearing < left + rbearing)
26056 cmp->rbearing = left + rbearing;
26057 }
26058 }
26059
26060 /* If there are glyphs whose x-offsets are negative,
26061 shift all glyphs to the right and make all x-offsets
26062 non-negative. */
26063 if (leftmost < 0)
26064 {
26065 for (i = 0; i < cmp->glyph_len; i++)
26066 cmp->offsets[i * 2] -= leftmost;
26067 rightmost -= leftmost;
26068 cmp->lbearing -= leftmost;
26069 cmp->rbearing -= leftmost;
26070 }
26071
26072 if (left_padded && cmp->lbearing < 0)
26073 {
26074 for (i = 0; i < cmp->glyph_len; i++)
26075 cmp->offsets[i * 2] -= cmp->lbearing;
26076 rightmost -= cmp->lbearing;
26077 cmp->rbearing -= cmp->lbearing;
26078 cmp->lbearing = 0;
26079 }
26080 if (right_padded && rightmost < cmp->rbearing)
26081 {
26082 rightmost = cmp->rbearing;
26083 }
26084
26085 cmp->pixel_width = rightmost;
26086 cmp->ascent = highest;
26087 cmp->descent = - lowest;
26088 if (cmp->ascent < font_ascent)
26089 cmp->ascent = font_ascent;
26090 if (cmp->descent < font_descent)
26091 cmp->descent = font_descent;
26092 }
26093
26094 if (it->glyph_row
26095 && (cmp->lbearing < 0
26096 || cmp->rbearing > cmp->pixel_width))
26097 it->glyph_row->contains_overlapping_glyphs_p = 1;
26098
26099 it->pixel_width = cmp->pixel_width;
26100 it->ascent = it->phys_ascent = cmp->ascent;
26101 it->descent = it->phys_descent = cmp->descent;
26102 if (face->box != FACE_NO_BOX)
26103 {
26104 int thick = face->box_line_width;
26105
26106 if (thick > 0)
26107 {
26108 it->ascent += thick;
26109 it->descent += thick;
26110 }
26111 else
26112 thick = - thick;
26113
26114 if (it->start_of_box_run_p)
26115 it->pixel_width += thick;
26116 if (it->end_of_box_run_p)
26117 it->pixel_width += thick;
26118 }
26119
26120 /* If face has an overline, add the height of the overline
26121 (1 pixel) and a 1 pixel margin to the character height. */
26122 if (face->overline_p)
26123 it->ascent += overline_margin;
26124
26125 take_vertical_position_into_account (it);
26126 if (it->ascent < 0)
26127 it->ascent = 0;
26128 if (it->descent < 0)
26129 it->descent = 0;
26130
26131 if (it->glyph_row && cmp->glyph_len > 0)
26132 append_composite_glyph (it);
26133 }
26134 else if (it->what == IT_COMPOSITION)
26135 {
26136 /* A dynamic (automatic) composition. */
26137 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26138 Lisp_Object gstring;
26139 struct font_metrics metrics;
26140
26141 it->nglyphs = 1;
26142
26143 gstring = composition_gstring_from_id (it->cmp_it.id);
26144 it->pixel_width
26145 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26146 &metrics);
26147 if (it->glyph_row
26148 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26149 it->glyph_row->contains_overlapping_glyphs_p = 1;
26150 it->ascent = it->phys_ascent = metrics.ascent;
26151 it->descent = it->phys_descent = metrics.descent;
26152 if (face->box != FACE_NO_BOX)
26153 {
26154 int thick = face->box_line_width;
26155
26156 if (thick > 0)
26157 {
26158 it->ascent += thick;
26159 it->descent += thick;
26160 }
26161 else
26162 thick = - thick;
26163
26164 if (it->start_of_box_run_p)
26165 it->pixel_width += thick;
26166 if (it->end_of_box_run_p)
26167 it->pixel_width += thick;
26168 }
26169 /* If face has an overline, add the height of the overline
26170 (1 pixel) and a 1 pixel margin to the character height. */
26171 if (face->overline_p)
26172 it->ascent += overline_margin;
26173 take_vertical_position_into_account (it);
26174 if (it->ascent < 0)
26175 it->ascent = 0;
26176 if (it->descent < 0)
26177 it->descent = 0;
26178
26179 if (it->glyph_row)
26180 append_composite_glyph (it);
26181 }
26182 else if (it->what == IT_GLYPHLESS)
26183 produce_glyphless_glyph (it, 0, Qnil);
26184 else if (it->what == IT_IMAGE)
26185 produce_image_glyph (it);
26186 else if (it->what == IT_STRETCH)
26187 produce_stretch_glyph (it);
26188
26189 done:
26190 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26191 because this isn't true for images with `:ascent 100'. */
26192 eassert (it->ascent >= 0 && it->descent >= 0);
26193 if (it->area == TEXT_AREA)
26194 it->current_x += it->pixel_width;
26195
26196 if (extra_line_spacing > 0)
26197 {
26198 it->descent += extra_line_spacing;
26199 if (extra_line_spacing > it->max_extra_line_spacing)
26200 it->max_extra_line_spacing = extra_line_spacing;
26201 }
26202
26203 it->max_ascent = max (it->max_ascent, it->ascent);
26204 it->max_descent = max (it->max_descent, it->descent);
26205 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26206 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26207 }
26208
26209 /* EXPORT for RIF:
26210 Output LEN glyphs starting at START at the nominal cursor position.
26211 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26212 being updated, and UPDATED_AREA is the area of that row being updated. */
26213
26214 void
26215 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26216 struct glyph *start, enum glyph_row_area updated_area, int len)
26217 {
26218 int x, hpos, chpos = w->phys_cursor.hpos;
26219
26220 eassert (updated_row);
26221 /* When the window is hscrolled, cursor hpos can legitimately be out
26222 of bounds, but we draw the cursor at the corresponding window
26223 margin in that case. */
26224 if (!updated_row->reversed_p && chpos < 0)
26225 chpos = 0;
26226 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26227 chpos = updated_row->used[TEXT_AREA] - 1;
26228
26229 block_input ();
26230
26231 /* Write glyphs. */
26232
26233 hpos = start - updated_row->glyphs[updated_area];
26234 x = draw_glyphs (w, w->output_cursor.x,
26235 updated_row, updated_area,
26236 hpos, hpos + len,
26237 DRAW_NORMAL_TEXT, 0);
26238
26239 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26240 if (updated_area == TEXT_AREA
26241 && w->phys_cursor_on_p
26242 && w->phys_cursor.vpos == w->output_cursor.vpos
26243 && chpos >= hpos
26244 && chpos < hpos + len)
26245 w->phys_cursor_on_p = 0;
26246
26247 unblock_input ();
26248
26249 /* Advance the output cursor. */
26250 w->output_cursor.hpos += len;
26251 w->output_cursor.x = x;
26252 }
26253
26254
26255 /* EXPORT for RIF:
26256 Insert LEN glyphs from START at the nominal cursor position. */
26257
26258 void
26259 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26260 struct glyph *start, enum glyph_row_area updated_area, int len)
26261 {
26262 struct frame *f;
26263 int line_height, shift_by_width, shifted_region_width;
26264 struct glyph_row *row;
26265 struct glyph *glyph;
26266 int frame_x, frame_y;
26267 ptrdiff_t hpos;
26268
26269 eassert (updated_row);
26270 block_input ();
26271 f = XFRAME (WINDOW_FRAME (w));
26272
26273 /* Get the height of the line we are in. */
26274 row = updated_row;
26275 line_height = row->height;
26276
26277 /* Get the width of the glyphs to insert. */
26278 shift_by_width = 0;
26279 for (glyph = start; glyph < start + len; ++glyph)
26280 shift_by_width += glyph->pixel_width;
26281
26282 /* Get the width of the region to shift right. */
26283 shifted_region_width = (window_box_width (w, updated_area)
26284 - w->output_cursor.x
26285 - shift_by_width);
26286
26287 /* Shift right. */
26288 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26289 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26290
26291 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26292 line_height, shift_by_width);
26293
26294 /* Write the glyphs. */
26295 hpos = start - row->glyphs[updated_area];
26296 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26297 hpos, hpos + len,
26298 DRAW_NORMAL_TEXT, 0);
26299
26300 /* Advance the output cursor. */
26301 w->output_cursor.hpos += len;
26302 w->output_cursor.x += shift_by_width;
26303 unblock_input ();
26304 }
26305
26306
26307 /* EXPORT for RIF:
26308 Erase the current text line from the nominal cursor position
26309 (inclusive) to pixel column TO_X (exclusive). The idea is that
26310 everything from TO_X onward is already erased.
26311
26312 TO_X is a pixel position relative to UPDATED_AREA of currently
26313 updated window W. TO_X == -1 means clear to the end of this area. */
26314
26315 void
26316 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26317 enum glyph_row_area updated_area, int to_x)
26318 {
26319 struct frame *f;
26320 int max_x, min_y, max_y;
26321 int from_x, from_y, to_y;
26322
26323 eassert (updated_row);
26324 f = XFRAME (w->frame);
26325
26326 if (updated_row->full_width_p)
26327 max_x = (WINDOW_PIXEL_WIDTH (w)
26328 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26329 else
26330 max_x = window_box_width (w, updated_area);
26331 max_y = window_text_bottom_y (w);
26332
26333 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26334 of window. For TO_X > 0, truncate to end of drawing area. */
26335 if (to_x == 0)
26336 return;
26337 else if (to_x < 0)
26338 to_x = max_x;
26339 else
26340 to_x = min (to_x, max_x);
26341
26342 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26343
26344 /* Notice if the cursor will be cleared by this operation. */
26345 if (!updated_row->full_width_p)
26346 notice_overwritten_cursor (w, updated_area,
26347 w->output_cursor.x, -1,
26348 updated_row->y,
26349 MATRIX_ROW_BOTTOM_Y (updated_row));
26350
26351 from_x = w->output_cursor.x;
26352
26353 /* Translate to frame coordinates. */
26354 if (updated_row->full_width_p)
26355 {
26356 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26357 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26358 }
26359 else
26360 {
26361 int area_left = window_box_left (w, updated_area);
26362 from_x += area_left;
26363 to_x += area_left;
26364 }
26365
26366 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26367 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26368 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26369
26370 /* Prevent inadvertently clearing to end of the X window. */
26371 if (to_x > from_x && to_y > from_y)
26372 {
26373 block_input ();
26374 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26375 to_x - from_x, to_y - from_y);
26376 unblock_input ();
26377 }
26378 }
26379
26380 #endif /* HAVE_WINDOW_SYSTEM */
26381
26382
26383 \f
26384 /***********************************************************************
26385 Cursor types
26386 ***********************************************************************/
26387
26388 /* Value is the internal representation of the specified cursor type
26389 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26390 of the bar cursor. */
26391
26392 static enum text_cursor_kinds
26393 get_specified_cursor_type (Lisp_Object arg, int *width)
26394 {
26395 enum text_cursor_kinds type;
26396
26397 if (NILP (arg))
26398 return NO_CURSOR;
26399
26400 if (EQ (arg, Qbox))
26401 return FILLED_BOX_CURSOR;
26402
26403 if (EQ (arg, Qhollow))
26404 return HOLLOW_BOX_CURSOR;
26405
26406 if (EQ (arg, Qbar))
26407 {
26408 *width = 2;
26409 return BAR_CURSOR;
26410 }
26411
26412 if (CONSP (arg)
26413 && EQ (XCAR (arg), Qbar)
26414 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26415 {
26416 *width = XINT (XCDR (arg));
26417 return BAR_CURSOR;
26418 }
26419
26420 if (EQ (arg, Qhbar))
26421 {
26422 *width = 2;
26423 return HBAR_CURSOR;
26424 }
26425
26426 if (CONSP (arg)
26427 && EQ (XCAR (arg), Qhbar)
26428 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26429 {
26430 *width = XINT (XCDR (arg));
26431 return HBAR_CURSOR;
26432 }
26433
26434 /* Treat anything unknown as "hollow box cursor".
26435 It was bad to signal an error; people have trouble fixing
26436 .Xdefaults with Emacs, when it has something bad in it. */
26437 type = HOLLOW_BOX_CURSOR;
26438
26439 return type;
26440 }
26441
26442 /* Set the default cursor types for specified frame. */
26443 void
26444 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26445 {
26446 int width = 1;
26447 Lisp_Object tem;
26448
26449 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26450 FRAME_CURSOR_WIDTH (f) = width;
26451
26452 /* By default, set up the blink-off state depending on the on-state. */
26453
26454 tem = Fassoc (arg, Vblink_cursor_alist);
26455 if (!NILP (tem))
26456 {
26457 FRAME_BLINK_OFF_CURSOR (f)
26458 = get_specified_cursor_type (XCDR (tem), &width);
26459 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26460 }
26461 else
26462 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26463
26464 /* Make sure the cursor gets redrawn. */
26465 f->cursor_type_changed = 1;
26466 }
26467
26468
26469 #ifdef HAVE_WINDOW_SYSTEM
26470
26471 /* Return the cursor we want to be displayed in window W. Return
26472 width of bar/hbar cursor through WIDTH arg. Return with
26473 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26474 (i.e. if the `system caret' should track this cursor).
26475
26476 In a mini-buffer window, we want the cursor only to appear if we
26477 are reading input from this window. For the selected window, we
26478 want the cursor type given by the frame parameter or buffer local
26479 setting of cursor-type. If explicitly marked off, draw no cursor.
26480 In all other cases, we want a hollow box cursor. */
26481
26482 static enum text_cursor_kinds
26483 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26484 int *active_cursor)
26485 {
26486 struct frame *f = XFRAME (w->frame);
26487 struct buffer *b = XBUFFER (w->contents);
26488 int cursor_type = DEFAULT_CURSOR;
26489 Lisp_Object alt_cursor;
26490 int non_selected = 0;
26491
26492 *active_cursor = 1;
26493
26494 /* Echo area */
26495 if (cursor_in_echo_area
26496 && FRAME_HAS_MINIBUF_P (f)
26497 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26498 {
26499 if (w == XWINDOW (echo_area_window))
26500 {
26501 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26502 {
26503 *width = FRAME_CURSOR_WIDTH (f);
26504 return FRAME_DESIRED_CURSOR (f);
26505 }
26506 else
26507 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26508 }
26509
26510 *active_cursor = 0;
26511 non_selected = 1;
26512 }
26513
26514 /* Detect a nonselected window or nonselected frame. */
26515 else if (w != XWINDOW (f->selected_window)
26516 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26517 {
26518 *active_cursor = 0;
26519
26520 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26521 return NO_CURSOR;
26522
26523 non_selected = 1;
26524 }
26525
26526 /* Never display a cursor in a window in which cursor-type is nil. */
26527 if (NILP (BVAR (b, cursor_type)))
26528 return NO_CURSOR;
26529
26530 /* Get the normal cursor type for this window. */
26531 if (EQ (BVAR (b, cursor_type), Qt))
26532 {
26533 cursor_type = FRAME_DESIRED_CURSOR (f);
26534 *width = FRAME_CURSOR_WIDTH (f);
26535 }
26536 else
26537 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26538
26539 /* Use cursor-in-non-selected-windows instead
26540 for non-selected window or frame. */
26541 if (non_selected)
26542 {
26543 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26544 if (!EQ (Qt, alt_cursor))
26545 return get_specified_cursor_type (alt_cursor, width);
26546 /* t means modify the normal cursor type. */
26547 if (cursor_type == FILLED_BOX_CURSOR)
26548 cursor_type = HOLLOW_BOX_CURSOR;
26549 else if (cursor_type == BAR_CURSOR && *width > 1)
26550 --*width;
26551 return cursor_type;
26552 }
26553
26554 /* Use normal cursor if not blinked off. */
26555 if (!w->cursor_off_p)
26556 {
26557 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26558 {
26559 if (cursor_type == FILLED_BOX_CURSOR)
26560 {
26561 /* Using a block cursor on large images can be very annoying.
26562 So use a hollow cursor for "large" images.
26563 If image is not transparent (no mask), also use hollow cursor. */
26564 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26565 if (img != NULL && IMAGEP (img->spec))
26566 {
26567 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26568 where N = size of default frame font size.
26569 This should cover most of the "tiny" icons people may use. */
26570 if (!img->mask
26571 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26572 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26573 cursor_type = HOLLOW_BOX_CURSOR;
26574 }
26575 }
26576 else if (cursor_type != NO_CURSOR)
26577 {
26578 /* Display current only supports BOX and HOLLOW cursors for images.
26579 So for now, unconditionally use a HOLLOW cursor when cursor is
26580 not a solid box cursor. */
26581 cursor_type = HOLLOW_BOX_CURSOR;
26582 }
26583 }
26584 return cursor_type;
26585 }
26586
26587 /* Cursor is blinked off, so determine how to "toggle" it. */
26588
26589 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26590 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26591 return get_specified_cursor_type (XCDR (alt_cursor), width);
26592
26593 /* Then see if frame has specified a specific blink off cursor type. */
26594 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26595 {
26596 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26597 return FRAME_BLINK_OFF_CURSOR (f);
26598 }
26599
26600 #if 0
26601 /* Some people liked having a permanently visible blinking cursor,
26602 while others had very strong opinions against it. So it was
26603 decided to remove it. KFS 2003-09-03 */
26604
26605 /* Finally perform built-in cursor blinking:
26606 filled box <-> hollow box
26607 wide [h]bar <-> narrow [h]bar
26608 narrow [h]bar <-> no cursor
26609 other type <-> no cursor */
26610
26611 if (cursor_type == FILLED_BOX_CURSOR)
26612 return HOLLOW_BOX_CURSOR;
26613
26614 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26615 {
26616 *width = 1;
26617 return cursor_type;
26618 }
26619 #endif
26620
26621 return NO_CURSOR;
26622 }
26623
26624
26625 /* Notice when the text cursor of window W has been completely
26626 overwritten by a drawing operation that outputs glyphs in AREA
26627 starting at X0 and ending at X1 in the line starting at Y0 and
26628 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26629 the rest of the line after X0 has been written. Y coordinates
26630 are window-relative. */
26631
26632 static void
26633 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26634 int x0, int x1, int y0, int y1)
26635 {
26636 int cx0, cx1, cy0, cy1;
26637 struct glyph_row *row;
26638
26639 if (!w->phys_cursor_on_p)
26640 return;
26641 if (area != TEXT_AREA)
26642 return;
26643
26644 if (w->phys_cursor.vpos < 0
26645 || w->phys_cursor.vpos >= w->current_matrix->nrows
26646 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26647 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26648 return;
26649
26650 if (row->cursor_in_fringe_p)
26651 {
26652 row->cursor_in_fringe_p = 0;
26653 draw_fringe_bitmap (w, row, row->reversed_p);
26654 w->phys_cursor_on_p = 0;
26655 return;
26656 }
26657
26658 cx0 = w->phys_cursor.x;
26659 cx1 = cx0 + w->phys_cursor_width;
26660 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26661 return;
26662
26663 /* The cursor image will be completely removed from the
26664 screen if the output area intersects the cursor area in
26665 y-direction. When we draw in [y0 y1[, and some part of
26666 the cursor is at y < y0, that part must have been drawn
26667 before. When scrolling, the cursor is erased before
26668 actually scrolling, so we don't come here. When not
26669 scrolling, the rows above the old cursor row must have
26670 changed, and in this case these rows must have written
26671 over the cursor image.
26672
26673 Likewise if part of the cursor is below y1, with the
26674 exception of the cursor being in the first blank row at
26675 the buffer and window end because update_text_area
26676 doesn't draw that row. (Except when it does, but
26677 that's handled in update_text_area.) */
26678
26679 cy0 = w->phys_cursor.y;
26680 cy1 = cy0 + w->phys_cursor_height;
26681 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26682 return;
26683
26684 w->phys_cursor_on_p = 0;
26685 }
26686
26687 #endif /* HAVE_WINDOW_SYSTEM */
26688
26689 \f
26690 /************************************************************************
26691 Mouse Face
26692 ************************************************************************/
26693
26694 #ifdef HAVE_WINDOW_SYSTEM
26695
26696 /* EXPORT for RIF:
26697 Fix the display of area AREA of overlapping row ROW in window W
26698 with respect to the overlapping part OVERLAPS. */
26699
26700 void
26701 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26702 enum glyph_row_area area, int overlaps)
26703 {
26704 int i, x;
26705
26706 block_input ();
26707
26708 x = 0;
26709 for (i = 0; i < row->used[area];)
26710 {
26711 if (row->glyphs[area][i].overlaps_vertically_p)
26712 {
26713 int start = i, start_x = x;
26714
26715 do
26716 {
26717 x += row->glyphs[area][i].pixel_width;
26718 ++i;
26719 }
26720 while (i < row->used[area]
26721 && row->glyphs[area][i].overlaps_vertically_p);
26722
26723 draw_glyphs (w, start_x, row, area,
26724 start, i,
26725 DRAW_NORMAL_TEXT, overlaps);
26726 }
26727 else
26728 {
26729 x += row->glyphs[area][i].pixel_width;
26730 ++i;
26731 }
26732 }
26733
26734 unblock_input ();
26735 }
26736
26737
26738 /* EXPORT:
26739 Draw the cursor glyph of window W in glyph row ROW. See the
26740 comment of draw_glyphs for the meaning of HL. */
26741
26742 void
26743 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26744 enum draw_glyphs_face hl)
26745 {
26746 /* If cursor hpos is out of bounds, don't draw garbage. This can
26747 happen in mini-buffer windows when switching between echo area
26748 glyphs and mini-buffer. */
26749 if ((row->reversed_p
26750 ? (w->phys_cursor.hpos >= 0)
26751 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26752 {
26753 int on_p = w->phys_cursor_on_p;
26754 int x1;
26755 int hpos = w->phys_cursor.hpos;
26756
26757 /* When the window is hscrolled, cursor hpos can legitimately be
26758 out of bounds, but we draw the cursor at the corresponding
26759 window margin in that case. */
26760 if (!row->reversed_p && hpos < 0)
26761 hpos = 0;
26762 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26763 hpos = row->used[TEXT_AREA] - 1;
26764
26765 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26766 hl, 0);
26767 w->phys_cursor_on_p = on_p;
26768
26769 if (hl == DRAW_CURSOR)
26770 w->phys_cursor_width = x1 - w->phys_cursor.x;
26771 /* When we erase the cursor, and ROW is overlapped by other
26772 rows, make sure that these overlapping parts of other rows
26773 are redrawn. */
26774 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26775 {
26776 w->phys_cursor_width = x1 - w->phys_cursor.x;
26777
26778 if (row > w->current_matrix->rows
26779 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26780 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26781 OVERLAPS_ERASED_CURSOR);
26782
26783 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26784 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26785 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26786 OVERLAPS_ERASED_CURSOR);
26787 }
26788 }
26789 }
26790
26791
26792 /* Erase the image of a cursor of window W from the screen. */
26793
26794 #ifndef HAVE_NTGUI
26795 static
26796 #endif
26797 void
26798 erase_phys_cursor (struct window *w)
26799 {
26800 struct frame *f = XFRAME (w->frame);
26801 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26802 int hpos = w->phys_cursor.hpos;
26803 int vpos = w->phys_cursor.vpos;
26804 int mouse_face_here_p = 0;
26805 struct glyph_matrix *active_glyphs = w->current_matrix;
26806 struct glyph_row *cursor_row;
26807 struct glyph *cursor_glyph;
26808 enum draw_glyphs_face hl;
26809
26810 /* No cursor displayed or row invalidated => nothing to do on the
26811 screen. */
26812 if (w->phys_cursor_type == NO_CURSOR)
26813 goto mark_cursor_off;
26814
26815 /* VPOS >= active_glyphs->nrows means that window has been resized.
26816 Don't bother to erase the cursor. */
26817 if (vpos >= active_glyphs->nrows)
26818 goto mark_cursor_off;
26819
26820 /* If row containing cursor is marked invalid, there is nothing we
26821 can do. */
26822 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26823 if (!cursor_row->enabled_p)
26824 goto mark_cursor_off;
26825
26826 /* If line spacing is > 0, old cursor may only be partially visible in
26827 window after split-window. So adjust visible height. */
26828 cursor_row->visible_height = min (cursor_row->visible_height,
26829 window_text_bottom_y (w) - cursor_row->y);
26830
26831 /* If row is completely invisible, don't attempt to delete a cursor which
26832 isn't there. This can happen if cursor is at top of a window, and
26833 we switch to a buffer with a header line in that window. */
26834 if (cursor_row->visible_height <= 0)
26835 goto mark_cursor_off;
26836
26837 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26838 if (cursor_row->cursor_in_fringe_p)
26839 {
26840 cursor_row->cursor_in_fringe_p = 0;
26841 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26842 goto mark_cursor_off;
26843 }
26844
26845 /* This can happen when the new row is shorter than the old one.
26846 In this case, either draw_glyphs or clear_end_of_line
26847 should have cleared the cursor. Note that we wouldn't be
26848 able to erase the cursor in this case because we don't have a
26849 cursor glyph at hand. */
26850 if ((cursor_row->reversed_p
26851 ? (w->phys_cursor.hpos < 0)
26852 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26853 goto mark_cursor_off;
26854
26855 /* When the window is hscrolled, cursor hpos can legitimately be out
26856 of bounds, but we draw the cursor at the corresponding window
26857 margin in that case. */
26858 if (!cursor_row->reversed_p && hpos < 0)
26859 hpos = 0;
26860 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26861 hpos = cursor_row->used[TEXT_AREA] - 1;
26862
26863 /* If the cursor is in the mouse face area, redisplay that when
26864 we clear the cursor. */
26865 if (! NILP (hlinfo->mouse_face_window)
26866 && coords_in_mouse_face_p (w, hpos, vpos)
26867 /* Don't redraw the cursor's spot in mouse face if it is at the
26868 end of a line (on a newline). The cursor appears there, but
26869 mouse highlighting does not. */
26870 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26871 mouse_face_here_p = 1;
26872
26873 /* Maybe clear the display under the cursor. */
26874 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26875 {
26876 int x, y, left_x;
26877 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26878 int width;
26879
26880 cursor_glyph = get_phys_cursor_glyph (w);
26881 if (cursor_glyph == NULL)
26882 goto mark_cursor_off;
26883
26884 width = cursor_glyph->pixel_width;
26885 left_x = window_box_left_offset (w, TEXT_AREA);
26886 x = w->phys_cursor.x;
26887 if (x < left_x)
26888 width -= left_x - x;
26889 width = min (width, window_box_width (w, TEXT_AREA) - x);
26890 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26891 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26892
26893 if (width > 0)
26894 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26895 }
26896
26897 /* Erase the cursor by redrawing the character underneath it. */
26898 if (mouse_face_here_p)
26899 hl = DRAW_MOUSE_FACE;
26900 else
26901 hl = DRAW_NORMAL_TEXT;
26902 draw_phys_cursor_glyph (w, cursor_row, hl);
26903
26904 mark_cursor_off:
26905 w->phys_cursor_on_p = 0;
26906 w->phys_cursor_type = NO_CURSOR;
26907 }
26908
26909
26910 /* EXPORT:
26911 Display or clear cursor of window W. If ON is zero, clear the
26912 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26913 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26914
26915 void
26916 display_and_set_cursor (struct window *w, bool on,
26917 int hpos, int vpos, int x, int y)
26918 {
26919 struct frame *f = XFRAME (w->frame);
26920 int new_cursor_type;
26921 int new_cursor_width;
26922 int active_cursor;
26923 struct glyph_row *glyph_row;
26924 struct glyph *glyph;
26925
26926 /* This is pointless on invisible frames, and dangerous on garbaged
26927 windows and frames; in the latter case, the frame or window may
26928 be in the midst of changing its size, and x and y may be off the
26929 window. */
26930 if (! FRAME_VISIBLE_P (f)
26931 || FRAME_GARBAGED_P (f)
26932 || vpos >= w->current_matrix->nrows
26933 || hpos >= w->current_matrix->matrix_w)
26934 return;
26935
26936 /* If cursor is off and we want it off, return quickly. */
26937 if (!on && !w->phys_cursor_on_p)
26938 return;
26939
26940 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26941 /* If cursor row is not enabled, we don't really know where to
26942 display the cursor. */
26943 if (!glyph_row->enabled_p)
26944 {
26945 w->phys_cursor_on_p = 0;
26946 return;
26947 }
26948
26949 glyph = NULL;
26950 if (!glyph_row->exact_window_width_line_p
26951 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26952 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26953
26954 eassert (input_blocked_p ());
26955
26956 /* Set new_cursor_type to the cursor we want to be displayed. */
26957 new_cursor_type = get_window_cursor_type (w, glyph,
26958 &new_cursor_width, &active_cursor);
26959
26960 /* If cursor is currently being shown and we don't want it to be or
26961 it is in the wrong place, or the cursor type is not what we want,
26962 erase it. */
26963 if (w->phys_cursor_on_p
26964 && (!on
26965 || w->phys_cursor.x != x
26966 || w->phys_cursor.y != y
26967 || new_cursor_type != w->phys_cursor_type
26968 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26969 && new_cursor_width != w->phys_cursor_width)))
26970 erase_phys_cursor (w);
26971
26972 /* Don't check phys_cursor_on_p here because that flag is only set
26973 to zero in some cases where we know that the cursor has been
26974 completely erased, to avoid the extra work of erasing the cursor
26975 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26976 still not be visible, or it has only been partly erased. */
26977 if (on)
26978 {
26979 w->phys_cursor_ascent = glyph_row->ascent;
26980 w->phys_cursor_height = glyph_row->height;
26981
26982 /* Set phys_cursor_.* before x_draw_.* is called because some
26983 of them may need the information. */
26984 w->phys_cursor.x = x;
26985 w->phys_cursor.y = glyph_row->y;
26986 w->phys_cursor.hpos = hpos;
26987 w->phys_cursor.vpos = vpos;
26988 }
26989
26990 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26991 new_cursor_type, new_cursor_width,
26992 on, active_cursor);
26993 }
26994
26995
26996 /* Switch the display of W's cursor on or off, according to the value
26997 of ON. */
26998
26999 static void
27000 update_window_cursor (struct window *w, bool on)
27001 {
27002 /* Don't update cursor in windows whose frame is in the process
27003 of being deleted. */
27004 if (w->current_matrix)
27005 {
27006 int hpos = w->phys_cursor.hpos;
27007 int vpos = w->phys_cursor.vpos;
27008 struct glyph_row *row;
27009
27010 if (vpos >= w->current_matrix->nrows
27011 || hpos >= w->current_matrix->matrix_w)
27012 return;
27013
27014 row = MATRIX_ROW (w->current_matrix, vpos);
27015
27016 /* When the window is hscrolled, cursor hpos can legitimately be
27017 out of bounds, but we draw the cursor at the corresponding
27018 window margin in that case. */
27019 if (!row->reversed_p && hpos < 0)
27020 hpos = 0;
27021 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27022 hpos = row->used[TEXT_AREA] - 1;
27023
27024 block_input ();
27025 display_and_set_cursor (w, on, hpos, vpos,
27026 w->phys_cursor.x, w->phys_cursor.y);
27027 unblock_input ();
27028 }
27029 }
27030
27031
27032 /* Call update_window_cursor with parameter ON_P on all leaf windows
27033 in the window tree rooted at W. */
27034
27035 static void
27036 update_cursor_in_window_tree (struct window *w, bool on_p)
27037 {
27038 while (w)
27039 {
27040 if (WINDOWP (w->contents))
27041 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27042 else
27043 update_window_cursor (w, on_p);
27044
27045 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27046 }
27047 }
27048
27049
27050 /* EXPORT:
27051 Display the cursor on window W, or clear it, according to ON_P.
27052 Don't change the cursor's position. */
27053
27054 void
27055 x_update_cursor (struct frame *f, bool on_p)
27056 {
27057 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27058 }
27059
27060
27061 /* EXPORT:
27062 Clear the cursor of window W to background color, and mark the
27063 cursor as not shown. This is used when the text where the cursor
27064 is about to be rewritten. */
27065
27066 void
27067 x_clear_cursor (struct window *w)
27068 {
27069 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27070 update_window_cursor (w, 0);
27071 }
27072
27073 #endif /* HAVE_WINDOW_SYSTEM */
27074
27075 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27076 and MSDOS. */
27077 static void
27078 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27079 int start_hpos, int end_hpos,
27080 enum draw_glyphs_face draw)
27081 {
27082 #ifdef HAVE_WINDOW_SYSTEM
27083 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27084 {
27085 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27086 return;
27087 }
27088 #endif
27089 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27090 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27091 #endif
27092 }
27093
27094 /* Display the active region described by mouse_face_* according to DRAW. */
27095
27096 static void
27097 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27098 {
27099 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27100 struct frame *f = XFRAME (WINDOW_FRAME (w));
27101
27102 if (/* If window is in the process of being destroyed, don't bother
27103 to do anything. */
27104 w->current_matrix != NULL
27105 /* Don't update mouse highlight if hidden */
27106 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27107 /* Recognize when we are called to operate on rows that don't exist
27108 anymore. This can happen when a window is split. */
27109 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27110 {
27111 int phys_cursor_on_p = w->phys_cursor_on_p;
27112 struct glyph_row *row, *first, *last;
27113
27114 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27115 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27116
27117 for (row = first; row <= last && row->enabled_p; ++row)
27118 {
27119 int start_hpos, end_hpos, start_x;
27120
27121 /* For all but the first row, the highlight starts at column 0. */
27122 if (row == first)
27123 {
27124 /* R2L rows have BEG and END in reversed order, but the
27125 screen drawing geometry is always left to right. So
27126 we need to mirror the beginning and end of the
27127 highlighted area in R2L rows. */
27128 if (!row->reversed_p)
27129 {
27130 start_hpos = hlinfo->mouse_face_beg_col;
27131 start_x = hlinfo->mouse_face_beg_x;
27132 }
27133 else if (row == last)
27134 {
27135 start_hpos = hlinfo->mouse_face_end_col;
27136 start_x = hlinfo->mouse_face_end_x;
27137 }
27138 else
27139 {
27140 start_hpos = 0;
27141 start_x = 0;
27142 }
27143 }
27144 else if (row->reversed_p && row == last)
27145 {
27146 start_hpos = hlinfo->mouse_face_end_col;
27147 start_x = hlinfo->mouse_face_end_x;
27148 }
27149 else
27150 {
27151 start_hpos = 0;
27152 start_x = 0;
27153 }
27154
27155 if (row == last)
27156 {
27157 if (!row->reversed_p)
27158 end_hpos = hlinfo->mouse_face_end_col;
27159 else if (row == first)
27160 end_hpos = hlinfo->mouse_face_beg_col;
27161 else
27162 {
27163 end_hpos = row->used[TEXT_AREA];
27164 if (draw == DRAW_NORMAL_TEXT)
27165 row->fill_line_p = 1; /* Clear to end of line */
27166 }
27167 }
27168 else if (row->reversed_p && row == first)
27169 end_hpos = hlinfo->mouse_face_beg_col;
27170 else
27171 {
27172 end_hpos = row->used[TEXT_AREA];
27173 if (draw == DRAW_NORMAL_TEXT)
27174 row->fill_line_p = 1; /* Clear to end of line */
27175 }
27176
27177 if (end_hpos > start_hpos)
27178 {
27179 draw_row_with_mouse_face (w, start_x, row,
27180 start_hpos, end_hpos, draw);
27181
27182 row->mouse_face_p
27183 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27184 }
27185 }
27186
27187 #ifdef HAVE_WINDOW_SYSTEM
27188 /* When we've written over the cursor, arrange for it to
27189 be displayed again. */
27190 if (FRAME_WINDOW_P (f)
27191 && phys_cursor_on_p && !w->phys_cursor_on_p)
27192 {
27193 int hpos = w->phys_cursor.hpos;
27194
27195 /* When the window is hscrolled, cursor hpos can legitimately be
27196 out of bounds, but we draw the cursor at the corresponding
27197 window margin in that case. */
27198 if (!row->reversed_p && hpos < 0)
27199 hpos = 0;
27200 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27201 hpos = row->used[TEXT_AREA] - 1;
27202
27203 block_input ();
27204 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27205 w->phys_cursor.x, w->phys_cursor.y);
27206 unblock_input ();
27207 }
27208 #endif /* HAVE_WINDOW_SYSTEM */
27209 }
27210
27211 #ifdef HAVE_WINDOW_SYSTEM
27212 /* Change the mouse cursor. */
27213 if (FRAME_WINDOW_P (f))
27214 {
27215 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27216 if (draw == DRAW_NORMAL_TEXT
27217 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27218 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27219 else
27220 #endif
27221 if (draw == DRAW_MOUSE_FACE)
27222 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27223 else
27224 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27225 }
27226 #endif /* HAVE_WINDOW_SYSTEM */
27227 }
27228
27229 /* EXPORT:
27230 Clear out the mouse-highlighted active region.
27231 Redraw it un-highlighted first. Value is non-zero if mouse
27232 face was actually drawn unhighlighted. */
27233
27234 int
27235 clear_mouse_face (Mouse_HLInfo *hlinfo)
27236 {
27237 int cleared = 0;
27238
27239 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27240 {
27241 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27242 cleared = 1;
27243 }
27244
27245 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27246 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27247 hlinfo->mouse_face_window = Qnil;
27248 hlinfo->mouse_face_overlay = Qnil;
27249 return cleared;
27250 }
27251
27252 /* Return true if the coordinates HPOS and VPOS on windows W are
27253 within the mouse face on that window. */
27254 static bool
27255 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27256 {
27257 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27258
27259 /* Quickly resolve the easy cases. */
27260 if (!(WINDOWP (hlinfo->mouse_face_window)
27261 && XWINDOW (hlinfo->mouse_face_window) == w))
27262 return false;
27263 if (vpos < hlinfo->mouse_face_beg_row
27264 || vpos > hlinfo->mouse_face_end_row)
27265 return false;
27266 if (vpos > hlinfo->mouse_face_beg_row
27267 && vpos < hlinfo->mouse_face_end_row)
27268 return true;
27269
27270 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27271 {
27272 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27273 {
27274 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27275 return true;
27276 }
27277 else if ((vpos == hlinfo->mouse_face_beg_row
27278 && hpos >= hlinfo->mouse_face_beg_col)
27279 || (vpos == hlinfo->mouse_face_end_row
27280 && hpos < hlinfo->mouse_face_end_col))
27281 return true;
27282 }
27283 else
27284 {
27285 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27286 {
27287 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_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 return false;
27297 }
27298
27299
27300 /* EXPORT:
27301 True if physical cursor of window W is within mouse face. */
27302
27303 bool
27304 cursor_in_mouse_face_p (struct window *w)
27305 {
27306 int hpos = w->phys_cursor.hpos;
27307 int vpos = w->phys_cursor.vpos;
27308 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27309
27310 /* When the window is hscrolled, cursor hpos can legitimately be out
27311 of bounds, but we draw the cursor at the corresponding window
27312 margin in that case. */
27313 if (!row->reversed_p && hpos < 0)
27314 hpos = 0;
27315 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27316 hpos = row->used[TEXT_AREA] - 1;
27317
27318 return coords_in_mouse_face_p (w, hpos, vpos);
27319 }
27320
27321
27322 \f
27323 /* Find the glyph rows START_ROW and END_ROW of window W that display
27324 characters between buffer positions START_CHARPOS and END_CHARPOS
27325 (excluding END_CHARPOS). DISP_STRING is a display string that
27326 covers these buffer positions. This is similar to
27327 row_containing_pos, but is more accurate when bidi reordering makes
27328 buffer positions change non-linearly with glyph rows. */
27329 static void
27330 rows_from_pos_range (struct window *w,
27331 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27332 Lisp_Object disp_string,
27333 struct glyph_row **start, struct glyph_row **end)
27334 {
27335 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27336 int last_y = window_text_bottom_y (w);
27337 struct glyph_row *row;
27338
27339 *start = NULL;
27340 *end = NULL;
27341
27342 while (!first->enabled_p
27343 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27344 first++;
27345
27346 /* Find the START row. */
27347 for (row = first;
27348 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27349 row++)
27350 {
27351 /* A row can potentially be the START row if the range of the
27352 characters it displays intersects the range
27353 [START_CHARPOS..END_CHARPOS). */
27354 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27355 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27356 /* See the commentary in row_containing_pos, for the
27357 explanation of the complicated way to check whether
27358 some position is beyond the end of the characters
27359 displayed by a row. */
27360 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27361 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27362 && !row->ends_at_zv_p
27363 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27364 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27365 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27366 && !row->ends_at_zv_p
27367 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27368 {
27369 /* Found a candidate row. Now make sure at least one of the
27370 glyphs it displays has a charpos from the range
27371 [START_CHARPOS..END_CHARPOS).
27372
27373 This is not obvious because bidi reordering could make
27374 buffer positions of a row be 1,2,3,102,101,100, and if we
27375 want to highlight characters in [50..60), we don't want
27376 this row, even though [50..60) does intersect [1..103),
27377 the range of character positions given by the row's start
27378 and end positions. */
27379 struct glyph *g = row->glyphs[TEXT_AREA];
27380 struct glyph *e = g + row->used[TEXT_AREA];
27381
27382 while (g < e)
27383 {
27384 if (((BUFFERP (g->object) || INTEGERP (g->object))
27385 && start_charpos <= g->charpos && g->charpos < end_charpos)
27386 /* A glyph that comes from DISP_STRING is by
27387 definition to be highlighted. */
27388 || EQ (g->object, disp_string))
27389 *start = row;
27390 g++;
27391 }
27392 if (*start)
27393 break;
27394 }
27395 }
27396
27397 /* Find the END row. */
27398 if (!*start
27399 /* If the last row is partially visible, start looking for END
27400 from that row, instead of starting from FIRST. */
27401 && !(row->enabled_p
27402 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27403 row = first;
27404 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27405 {
27406 struct glyph_row *next = row + 1;
27407 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27408
27409 if (!next->enabled_p
27410 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27411 /* The first row >= START whose range of displayed characters
27412 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27413 is the row END + 1. */
27414 || (start_charpos < next_start
27415 && end_charpos < next_start)
27416 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27417 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27418 && !next->ends_at_zv_p
27419 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27420 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27421 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27422 && !next->ends_at_zv_p
27423 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27424 {
27425 *end = row;
27426 break;
27427 }
27428 else
27429 {
27430 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27431 but none of the characters it displays are in the range, it is
27432 also END + 1. */
27433 struct glyph *g = next->glyphs[TEXT_AREA];
27434 struct glyph *s = g;
27435 struct glyph *e = g + next->used[TEXT_AREA];
27436
27437 while (g < e)
27438 {
27439 if (((BUFFERP (g->object) || INTEGERP (g->object))
27440 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27441 /* If the buffer position of the first glyph in
27442 the row is equal to END_CHARPOS, it means
27443 the last character to be highlighted is the
27444 newline of ROW, and we must consider NEXT as
27445 END, not END+1. */
27446 || (((!next->reversed_p && g == s)
27447 || (next->reversed_p && g == e - 1))
27448 && (g->charpos == end_charpos
27449 /* Special case for when NEXT is an
27450 empty line at ZV. */
27451 || (g->charpos == -1
27452 && !row->ends_at_zv_p
27453 && next_start == end_charpos)))))
27454 /* A glyph that comes from DISP_STRING is by
27455 definition to be highlighted. */
27456 || EQ (g->object, disp_string))
27457 break;
27458 g++;
27459 }
27460 if (g == e)
27461 {
27462 *end = row;
27463 break;
27464 }
27465 /* The first row that ends at ZV must be the last to be
27466 highlighted. */
27467 else if (next->ends_at_zv_p)
27468 {
27469 *end = next;
27470 break;
27471 }
27472 }
27473 }
27474 }
27475
27476 /* This function sets the mouse_face_* elements of HLINFO, assuming
27477 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27478 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27479 for the overlay or run of text properties specifying the mouse
27480 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27481 before-string and after-string that must also be highlighted.
27482 DISP_STRING, if non-nil, is a display string that may cover some
27483 or all of the highlighted text. */
27484
27485 static void
27486 mouse_face_from_buffer_pos (Lisp_Object window,
27487 Mouse_HLInfo *hlinfo,
27488 ptrdiff_t mouse_charpos,
27489 ptrdiff_t start_charpos,
27490 ptrdiff_t end_charpos,
27491 Lisp_Object before_string,
27492 Lisp_Object after_string,
27493 Lisp_Object disp_string)
27494 {
27495 struct window *w = XWINDOW (window);
27496 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27497 struct glyph_row *r1, *r2;
27498 struct glyph *glyph, *end;
27499 ptrdiff_t ignore, pos;
27500 int x;
27501
27502 eassert (NILP (disp_string) || STRINGP (disp_string));
27503 eassert (NILP (before_string) || STRINGP (before_string));
27504 eassert (NILP (after_string) || STRINGP (after_string));
27505
27506 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27507 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27508 if (r1 == NULL)
27509 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27510 /* If the before-string or display-string contains newlines,
27511 rows_from_pos_range skips to its last row. Move back. */
27512 if (!NILP (before_string) || !NILP (disp_string))
27513 {
27514 struct glyph_row *prev;
27515 while ((prev = r1 - 1, prev >= first)
27516 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27517 && prev->used[TEXT_AREA] > 0)
27518 {
27519 struct glyph *beg = prev->glyphs[TEXT_AREA];
27520 glyph = beg + prev->used[TEXT_AREA];
27521 while (--glyph >= beg && INTEGERP (glyph->object));
27522 if (glyph < beg
27523 || !(EQ (glyph->object, before_string)
27524 || EQ (glyph->object, disp_string)))
27525 break;
27526 r1 = prev;
27527 }
27528 }
27529 if (r2 == NULL)
27530 {
27531 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27532 hlinfo->mouse_face_past_end = 1;
27533 }
27534 else if (!NILP (after_string))
27535 {
27536 /* If the after-string has newlines, advance to its last row. */
27537 struct glyph_row *next;
27538 struct glyph_row *last
27539 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27540
27541 for (next = r2 + 1;
27542 next <= last
27543 && next->used[TEXT_AREA] > 0
27544 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27545 ++next)
27546 r2 = next;
27547 }
27548 /* The rest of the display engine assumes that mouse_face_beg_row is
27549 either above mouse_face_end_row or identical to it. But with
27550 bidi-reordered continued lines, the row for START_CHARPOS could
27551 be below the row for END_CHARPOS. If so, swap the rows and store
27552 them in correct order. */
27553 if (r1->y > r2->y)
27554 {
27555 struct glyph_row *tem = r2;
27556
27557 r2 = r1;
27558 r1 = tem;
27559 }
27560
27561 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27562 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27563
27564 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27565 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27566 could be anywhere in the row and in any order. The strategy
27567 below is to find the leftmost and the rightmost glyph that
27568 belongs to either of these 3 strings, or whose position is
27569 between START_CHARPOS and END_CHARPOS, and highlight all the
27570 glyphs between those two. This may cover more than just the text
27571 between START_CHARPOS and END_CHARPOS if the range of characters
27572 strides the bidi level boundary, e.g. if the beginning is in R2L
27573 text while the end is in L2R text or vice versa. */
27574 if (!r1->reversed_p)
27575 {
27576 /* This row is in a left to right paragraph. Scan it left to
27577 right. */
27578 glyph = r1->glyphs[TEXT_AREA];
27579 end = glyph + r1->used[TEXT_AREA];
27580 x = r1->x;
27581
27582 /* Skip truncation glyphs at the start of the glyph row. */
27583 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27584 for (; glyph < end
27585 && INTEGERP (glyph->object)
27586 && glyph->charpos < 0;
27587 ++glyph)
27588 x += glyph->pixel_width;
27589
27590 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27591 or DISP_STRING, and the first glyph from buffer whose
27592 position is between START_CHARPOS and END_CHARPOS. */
27593 for (; glyph < end
27594 && !INTEGERP (glyph->object)
27595 && !EQ (glyph->object, disp_string)
27596 && !(BUFFERP (glyph->object)
27597 && (glyph->charpos >= start_charpos
27598 && glyph->charpos < end_charpos));
27599 ++glyph)
27600 {
27601 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27602 are present at buffer positions between START_CHARPOS and
27603 END_CHARPOS, or if they come from an overlay. */
27604 if (EQ (glyph->object, before_string))
27605 {
27606 pos = string_buffer_position (before_string,
27607 start_charpos);
27608 /* If pos == 0, it means before_string came from an
27609 overlay, not from a buffer position. */
27610 if (!pos || (pos >= start_charpos && pos < end_charpos))
27611 break;
27612 }
27613 else if (EQ (glyph->object, after_string))
27614 {
27615 pos = string_buffer_position (after_string, end_charpos);
27616 if (!pos || (pos >= start_charpos && pos < end_charpos))
27617 break;
27618 }
27619 x += glyph->pixel_width;
27620 }
27621 hlinfo->mouse_face_beg_x = x;
27622 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27623 }
27624 else
27625 {
27626 /* This row is in a right to left paragraph. Scan it right to
27627 left. */
27628 struct glyph *g;
27629
27630 end = r1->glyphs[TEXT_AREA] - 1;
27631 glyph = end + r1->used[TEXT_AREA];
27632
27633 /* Skip truncation glyphs at the start of the glyph row. */
27634 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27635 for (; glyph > end
27636 && INTEGERP (glyph->object)
27637 && glyph->charpos < 0;
27638 --glyph)
27639 ;
27640
27641 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27642 or DISP_STRING, and the first glyph from buffer whose
27643 position is between START_CHARPOS and END_CHARPOS. */
27644 for (; glyph > end
27645 && !INTEGERP (glyph->object)
27646 && !EQ (glyph->object, disp_string)
27647 && !(BUFFERP (glyph->object)
27648 && (glyph->charpos >= start_charpos
27649 && glyph->charpos < end_charpos));
27650 --glyph)
27651 {
27652 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27653 are present at buffer positions between START_CHARPOS and
27654 END_CHARPOS, or if they come from an overlay. */
27655 if (EQ (glyph->object, before_string))
27656 {
27657 pos = string_buffer_position (before_string, start_charpos);
27658 /* If pos == 0, it means before_string came from an
27659 overlay, not from a buffer position. */
27660 if (!pos || (pos >= start_charpos && pos < end_charpos))
27661 break;
27662 }
27663 else if (EQ (glyph->object, after_string))
27664 {
27665 pos = string_buffer_position (after_string, end_charpos);
27666 if (!pos || (pos >= start_charpos && pos < end_charpos))
27667 break;
27668 }
27669 }
27670
27671 glyph++; /* first glyph to the right of the highlighted area */
27672 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27673 x += g->pixel_width;
27674 hlinfo->mouse_face_beg_x = x;
27675 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27676 }
27677
27678 /* If the highlight ends in a different row, compute GLYPH and END
27679 for the end row. Otherwise, reuse the values computed above for
27680 the row where the highlight begins. */
27681 if (r2 != r1)
27682 {
27683 if (!r2->reversed_p)
27684 {
27685 glyph = r2->glyphs[TEXT_AREA];
27686 end = glyph + r2->used[TEXT_AREA];
27687 x = r2->x;
27688 }
27689 else
27690 {
27691 end = r2->glyphs[TEXT_AREA] - 1;
27692 glyph = end + r2->used[TEXT_AREA];
27693 }
27694 }
27695
27696 if (!r2->reversed_p)
27697 {
27698 /* Skip truncation and continuation glyphs near the end of the
27699 row, and also blanks and stretch glyphs inserted by
27700 extend_face_to_end_of_line. */
27701 while (end > glyph
27702 && INTEGERP ((end - 1)->object))
27703 --end;
27704 /* Scan the rest of the glyph row from the end, looking for the
27705 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27706 DISP_STRING, or whose position is between START_CHARPOS
27707 and END_CHARPOS */
27708 for (--end;
27709 end > glyph
27710 && !INTEGERP (end->object)
27711 && !EQ (end->object, disp_string)
27712 && !(BUFFERP (end->object)
27713 && (end->charpos >= start_charpos
27714 && end->charpos < end_charpos));
27715 --end)
27716 {
27717 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27718 are present at buffer positions between START_CHARPOS and
27719 END_CHARPOS, or if they come from an overlay. */
27720 if (EQ (end->object, before_string))
27721 {
27722 pos = string_buffer_position (before_string, start_charpos);
27723 if (!pos || (pos >= start_charpos && pos < end_charpos))
27724 break;
27725 }
27726 else if (EQ (end->object, after_string))
27727 {
27728 pos = string_buffer_position (after_string, end_charpos);
27729 if (!pos || (pos >= start_charpos && pos < end_charpos))
27730 break;
27731 }
27732 }
27733 /* Find the X coordinate of the last glyph to be highlighted. */
27734 for (; glyph <= end; ++glyph)
27735 x += glyph->pixel_width;
27736
27737 hlinfo->mouse_face_end_x = x;
27738 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27739 }
27740 else
27741 {
27742 /* Skip truncation and continuation glyphs near the end of the
27743 row, and also blanks and stretch glyphs inserted by
27744 extend_face_to_end_of_line. */
27745 x = r2->x;
27746 end++;
27747 while (end < glyph
27748 && INTEGERP (end->object))
27749 {
27750 x += end->pixel_width;
27751 ++end;
27752 }
27753 /* Scan the rest of the glyph row from the end, looking for the
27754 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27755 DISP_STRING, or whose position is between START_CHARPOS
27756 and END_CHARPOS */
27757 for ( ;
27758 end < glyph
27759 && !INTEGERP (end->object)
27760 && !EQ (end->object, disp_string)
27761 && !(BUFFERP (end->object)
27762 && (end->charpos >= start_charpos
27763 && end->charpos < end_charpos));
27764 ++end)
27765 {
27766 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27767 are present at buffer positions between START_CHARPOS and
27768 END_CHARPOS, or if they come from an overlay. */
27769 if (EQ (end->object, before_string))
27770 {
27771 pos = string_buffer_position (before_string, start_charpos);
27772 if (!pos || (pos >= start_charpos && pos < end_charpos))
27773 break;
27774 }
27775 else if (EQ (end->object, after_string))
27776 {
27777 pos = string_buffer_position (after_string, end_charpos);
27778 if (!pos || (pos >= start_charpos && pos < end_charpos))
27779 break;
27780 }
27781 x += end->pixel_width;
27782 }
27783 /* If we exited the above loop because we arrived at the last
27784 glyph of the row, and its buffer position is still not in
27785 range, it means the last character in range is the preceding
27786 newline. Bump the end column and x values to get past the
27787 last glyph. */
27788 if (end == glyph
27789 && BUFFERP (end->object)
27790 && (end->charpos < start_charpos
27791 || end->charpos >= end_charpos))
27792 {
27793 x += end->pixel_width;
27794 ++end;
27795 }
27796 hlinfo->mouse_face_end_x = x;
27797 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27798 }
27799
27800 hlinfo->mouse_face_window = window;
27801 hlinfo->mouse_face_face_id
27802 = face_at_buffer_position (w, mouse_charpos, &ignore,
27803 mouse_charpos + 1,
27804 !hlinfo->mouse_face_hidden, -1);
27805 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27806 }
27807
27808 /* The following function is not used anymore (replaced with
27809 mouse_face_from_string_pos), but I leave it here for the time
27810 being, in case someone would. */
27811
27812 #if 0 /* not used */
27813
27814 /* Find the position of the glyph for position POS in OBJECT in
27815 window W's current matrix, and return in *X, *Y the pixel
27816 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27817
27818 RIGHT_P non-zero means return the position of the right edge of the
27819 glyph, RIGHT_P zero means return the left edge position.
27820
27821 If no glyph for POS exists in the matrix, return the position of
27822 the glyph with the next smaller position that is in the matrix, if
27823 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27824 exists in the matrix, return the position of the glyph with the
27825 next larger position in OBJECT.
27826
27827 Value is non-zero if a glyph was found. */
27828
27829 static int
27830 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27831 int *hpos, int *vpos, int *x, int *y, int right_p)
27832 {
27833 int yb = window_text_bottom_y (w);
27834 struct glyph_row *r;
27835 struct glyph *best_glyph = NULL;
27836 struct glyph_row *best_row = NULL;
27837 int best_x = 0;
27838
27839 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27840 r->enabled_p && r->y < yb;
27841 ++r)
27842 {
27843 struct glyph *g = r->glyphs[TEXT_AREA];
27844 struct glyph *e = g + r->used[TEXT_AREA];
27845 int gx;
27846
27847 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27848 if (EQ (g->object, object))
27849 {
27850 if (g->charpos == pos)
27851 {
27852 best_glyph = g;
27853 best_x = gx;
27854 best_row = r;
27855 goto found;
27856 }
27857 else if (best_glyph == NULL
27858 || ((eabs (g->charpos - pos)
27859 < eabs (best_glyph->charpos - pos))
27860 && (right_p
27861 ? g->charpos < pos
27862 : g->charpos > pos)))
27863 {
27864 best_glyph = g;
27865 best_x = gx;
27866 best_row = r;
27867 }
27868 }
27869 }
27870
27871 found:
27872
27873 if (best_glyph)
27874 {
27875 *x = best_x;
27876 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27877
27878 if (right_p)
27879 {
27880 *x += best_glyph->pixel_width;
27881 ++*hpos;
27882 }
27883
27884 *y = best_row->y;
27885 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27886 }
27887
27888 return best_glyph != NULL;
27889 }
27890 #endif /* not used */
27891
27892 /* Find the positions of the first and the last glyphs in window W's
27893 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27894 (assumed to be a string), and return in HLINFO's mouse_face_*
27895 members the pixel and column/row coordinates of those glyphs. */
27896
27897 static void
27898 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27899 Lisp_Object object,
27900 ptrdiff_t startpos, ptrdiff_t endpos)
27901 {
27902 int yb = window_text_bottom_y (w);
27903 struct glyph_row *r;
27904 struct glyph *g, *e;
27905 int gx;
27906 int found = 0;
27907
27908 /* Find the glyph row with at least one position in the range
27909 [STARTPOS..ENDPOS), and the first glyph in that row whose
27910 position belongs to that range. */
27911 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27912 r->enabled_p && r->y < yb;
27913 ++r)
27914 {
27915 if (!r->reversed_p)
27916 {
27917 g = r->glyphs[TEXT_AREA];
27918 e = g + r->used[TEXT_AREA];
27919 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27920 if (EQ (g->object, object)
27921 && startpos <= g->charpos && g->charpos < endpos)
27922 {
27923 hlinfo->mouse_face_beg_row
27924 = MATRIX_ROW_VPOS (r, w->current_matrix);
27925 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27926 hlinfo->mouse_face_beg_x = gx;
27927 found = 1;
27928 break;
27929 }
27930 }
27931 else
27932 {
27933 struct glyph *g1;
27934
27935 e = r->glyphs[TEXT_AREA];
27936 g = e + r->used[TEXT_AREA];
27937 for ( ; g > e; --g)
27938 if (EQ ((g-1)->object, object)
27939 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27940 {
27941 hlinfo->mouse_face_beg_row
27942 = MATRIX_ROW_VPOS (r, w->current_matrix);
27943 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27944 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27945 gx += g1->pixel_width;
27946 hlinfo->mouse_face_beg_x = gx;
27947 found = 1;
27948 break;
27949 }
27950 }
27951 if (found)
27952 break;
27953 }
27954
27955 if (!found)
27956 return;
27957
27958 /* Starting with the next row, look for the first row which does NOT
27959 include any glyphs whose positions are in the range. */
27960 for (++r; r->enabled_p && r->y < yb; ++r)
27961 {
27962 g = r->glyphs[TEXT_AREA];
27963 e = g + r->used[TEXT_AREA];
27964 found = 0;
27965 for ( ; g < e; ++g)
27966 if (EQ (g->object, object)
27967 && startpos <= g->charpos && g->charpos < endpos)
27968 {
27969 found = 1;
27970 break;
27971 }
27972 if (!found)
27973 break;
27974 }
27975
27976 /* The highlighted region ends on the previous row. */
27977 r--;
27978
27979 /* Set the end row. */
27980 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27981
27982 /* Compute and set the end column and the end column's horizontal
27983 pixel coordinate. */
27984 if (!r->reversed_p)
27985 {
27986 g = r->glyphs[TEXT_AREA];
27987 e = g + r->used[TEXT_AREA];
27988 for ( ; e > g; --e)
27989 if (EQ ((e-1)->object, object)
27990 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27991 break;
27992 hlinfo->mouse_face_end_col = e - g;
27993
27994 for (gx = r->x; g < e; ++g)
27995 gx += g->pixel_width;
27996 hlinfo->mouse_face_end_x = gx;
27997 }
27998 else
27999 {
28000 e = r->glyphs[TEXT_AREA];
28001 g = e + r->used[TEXT_AREA];
28002 for (gx = r->x ; e < g; ++e)
28003 {
28004 if (EQ (e->object, object)
28005 && startpos <= e->charpos && e->charpos < endpos)
28006 break;
28007 gx += e->pixel_width;
28008 }
28009 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28010 hlinfo->mouse_face_end_x = gx;
28011 }
28012 }
28013
28014 #ifdef HAVE_WINDOW_SYSTEM
28015
28016 /* See if position X, Y is within a hot-spot of an image. */
28017
28018 static int
28019 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28020 {
28021 if (!CONSP (hot_spot))
28022 return 0;
28023
28024 if (EQ (XCAR (hot_spot), Qrect))
28025 {
28026 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28027 Lisp_Object rect = XCDR (hot_spot);
28028 Lisp_Object tem;
28029 if (!CONSP (rect))
28030 return 0;
28031 if (!CONSP (XCAR (rect)))
28032 return 0;
28033 if (!CONSP (XCDR (rect)))
28034 return 0;
28035 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28036 return 0;
28037 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28038 return 0;
28039 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28040 return 0;
28041 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28042 return 0;
28043 return 1;
28044 }
28045 else if (EQ (XCAR (hot_spot), Qcircle))
28046 {
28047 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28048 Lisp_Object circ = XCDR (hot_spot);
28049 Lisp_Object lr, lx0, ly0;
28050 if (CONSP (circ)
28051 && CONSP (XCAR (circ))
28052 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28053 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28054 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28055 {
28056 double r = XFLOATINT (lr);
28057 double dx = XINT (lx0) - x;
28058 double dy = XINT (ly0) - y;
28059 return (dx * dx + dy * dy <= r * r);
28060 }
28061 }
28062 else if (EQ (XCAR (hot_spot), Qpoly))
28063 {
28064 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28065 if (VECTORP (XCDR (hot_spot)))
28066 {
28067 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28068 Lisp_Object *poly = v->contents;
28069 ptrdiff_t n = v->header.size;
28070 ptrdiff_t i;
28071 int inside = 0;
28072 Lisp_Object lx, ly;
28073 int x0, y0;
28074
28075 /* Need an even number of coordinates, and at least 3 edges. */
28076 if (n < 6 || n & 1)
28077 return 0;
28078
28079 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28080 If count is odd, we are inside polygon. Pixels on edges
28081 may or may not be included depending on actual geometry of the
28082 polygon. */
28083 if ((lx = poly[n-2], !INTEGERP (lx))
28084 || (ly = poly[n-1], !INTEGERP (lx)))
28085 return 0;
28086 x0 = XINT (lx), y0 = XINT (ly);
28087 for (i = 0; i < n; i += 2)
28088 {
28089 int x1 = x0, y1 = y0;
28090 if ((lx = poly[i], !INTEGERP (lx))
28091 || (ly = poly[i+1], !INTEGERP (ly)))
28092 return 0;
28093 x0 = XINT (lx), y0 = XINT (ly);
28094
28095 /* Does this segment cross the X line? */
28096 if (x0 >= x)
28097 {
28098 if (x1 >= x)
28099 continue;
28100 }
28101 else if (x1 < x)
28102 continue;
28103 if (y > y0 && y > y1)
28104 continue;
28105 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28106 inside = !inside;
28107 }
28108 return inside;
28109 }
28110 }
28111 return 0;
28112 }
28113
28114 Lisp_Object
28115 find_hot_spot (Lisp_Object map, int x, int y)
28116 {
28117 while (CONSP (map))
28118 {
28119 if (CONSP (XCAR (map))
28120 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28121 return XCAR (map);
28122 map = XCDR (map);
28123 }
28124
28125 return Qnil;
28126 }
28127
28128 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28129 3, 3, 0,
28130 doc: /* Lookup in image map MAP coordinates X and Y.
28131 An image map is an alist where each element has the format (AREA ID PLIST).
28132 An AREA is specified as either a rectangle, a circle, or a polygon:
28133 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28134 pixel coordinates of the upper left and bottom right corners.
28135 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28136 and the radius of the circle; r may be a float or integer.
28137 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28138 vector describes one corner in the polygon.
28139 Returns the alist element for the first matching AREA in MAP. */)
28140 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28141 {
28142 if (NILP (map))
28143 return Qnil;
28144
28145 CHECK_NUMBER (x);
28146 CHECK_NUMBER (y);
28147
28148 return find_hot_spot (map,
28149 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28150 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28151 }
28152
28153
28154 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28155 static void
28156 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28157 {
28158 /* Do not change cursor shape while dragging mouse. */
28159 if (!NILP (do_mouse_tracking))
28160 return;
28161
28162 if (!NILP (pointer))
28163 {
28164 if (EQ (pointer, Qarrow))
28165 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28166 else if (EQ (pointer, Qhand))
28167 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28168 else if (EQ (pointer, Qtext))
28169 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28170 else if (EQ (pointer, intern ("hdrag")))
28171 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28172 else if (EQ (pointer, intern ("nhdrag")))
28173 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28174 #ifdef HAVE_X_WINDOWS
28175 else if (EQ (pointer, intern ("vdrag")))
28176 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28177 #endif
28178 else if (EQ (pointer, intern ("hourglass")))
28179 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28180 else if (EQ (pointer, Qmodeline))
28181 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28182 else
28183 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28184 }
28185
28186 if (cursor != No_Cursor)
28187 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28188 }
28189
28190 #endif /* HAVE_WINDOW_SYSTEM */
28191
28192 /* Take proper action when mouse has moved to the mode or header line
28193 or marginal area AREA of window W, x-position X and y-position Y.
28194 X is relative to the start of the text display area of W, so the
28195 width of bitmap areas and scroll bars must be subtracted to get a
28196 position relative to the start of the mode line. */
28197
28198 static void
28199 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28200 enum window_part area)
28201 {
28202 struct window *w = XWINDOW (window);
28203 struct frame *f = XFRAME (w->frame);
28204 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28205 #ifdef HAVE_WINDOW_SYSTEM
28206 Display_Info *dpyinfo;
28207 #endif
28208 Cursor cursor = No_Cursor;
28209 Lisp_Object pointer = Qnil;
28210 int dx, dy, width, height;
28211 ptrdiff_t charpos;
28212 Lisp_Object string, object = Qnil;
28213 Lisp_Object pos IF_LINT (= Qnil), help;
28214
28215 Lisp_Object mouse_face;
28216 int original_x_pixel = x;
28217 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28218 struct glyph_row *row IF_LINT (= 0);
28219
28220 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28221 {
28222 int x0;
28223 struct glyph *end;
28224
28225 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28226 returns them in row/column units! */
28227 string = mode_line_string (w, area, &x, &y, &charpos,
28228 &object, &dx, &dy, &width, &height);
28229
28230 row = (area == ON_MODE_LINE
28231 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28232 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28233
28234 /* Find the glyph under the mouse pointer. */
28235 if (row->mode_line_p && row->enabled_p)
28236 {
28237 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28238 end = glyph + row->used[TEXT_AREA];
28239
28240 for (x0 = original_x_pixel;
28241 glyph < end && x0 >= glyph->pixel_width;
28242 ++glyph)
28243 x0 -= glyph->pixel_width;
28244
28245 if (glyph >= end)
28246 glyph = NULL;
28247 }
28248 }
28249 else
28250 {
28251 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28252 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28253 returns them in row/column units! */
28254 string = marginal_area_string (w, area, &x, &y, &charpos,
28255 &object, &dx, &dy, &width, &height);
28256 }
28257
28258 help = Qnil;
28259
28260 #ifdef HAVE_WINDOW_SYSTEM
28261 if (IMAGEP (object))
28262 {
28263 Lisp_Object image_map, hotspot;
28264 if ((image_map = Fplist_get (XCDR (object), QCmap),
28265 !NILP (image_map))
28266 && (hotspot = find_hot_spot (image_map, dx, dy),
28267 CONSP (hotspot))
28268 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28269 {
28270 Lisp_Object plist;
28271
28272 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28273 If so, we could look for mouse-enter, mouse-leave
28274 properties in PLIST (and do something...). */
28275 hotspot = XCDR (hotspot);
28276 if (CONSP (hotspot)
28277 && (plist = XCAR (hotspot), CONSP (plist)))
28278 {
28279 pointer = Fplist_get (plist, Qpointer);
28280 if (NILP (pointer))
28281 pointer = Qhand;
28282 help = Fplist_get (plist, Qhelp_echo);
28283 if (!NILP (help))
28284 {
28285 help_echo_string = help;
28286 XSETWINDOW (help_echo_window, w);
28287 help_echo_object = w->contents;
28288 help_echo_pos = charpos;
28289 }
28290 }
28291 }
28292 if (NILP (pointer))
28293 pointer = Fplist_get (XCDR (object), QCpointer);
28294 }
28295 #endif /* HAVE_WINDOW_SYSTEM */
28296
28297 if (STRINGP (string))
28298 pos = make_number (charpos);
28299
28300 /* Set the help text and mouse pointer. If the mouse is on a part
28301 of the mode line without any text (e.g. past the right edge of
28302 the mode line text), use the default help text and pointer. */
28303 if (STRINGP (string) || area == ON_MODE_LINE)
28304 {
28305 /* Arrange to display the help by setting the global variables
28306 help_echo_string, help_echo_object, and help_echo_pos. */
28307 if (NILP (help))
28308 {
28309 if (STRINGP (string))
28310 help = Fget_text_property (pos, Qhelp_echo, string);
28311
28312 if (!NILP (help))
28313 {
28314 help_echo_string = help;
28315 XSETWINDOW (help_echo_window, w);
28316 help_echo_object = string;
28317 help_echo_pos = charpos;
28318 }
28319 else if (area == ON_MODE_LINE)
28320 {
28321 Lisp_Object default_help
28322 = buffer_local_value_1 (Qmode_line_default_help_echo,
28323 w->contents);
28324
28325 if (STRINGP (default_help))
28326 {
28327 help_echo_string = default_help;
28328 XSETWINDOW (help_echo_window, w);
28329 help_echo_object = Qnil;
28330 help_echo_pos = -1;
28331 }
28332 }
28333 }
28334
28335 #ifdef HAVE_WINDOW_SYSTEM
28336 /* Change the mouse pointer according to what is under it. */
28337 if (FRAME_WINDOW_P (f))
28338 {
28339 dpyinfo = FRAME_DISPLAY_INFO (f);
28340 if (STRINGP (string))
28341 {
28342 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28343
28344 if (NILP (pointer))
28345 pointer = Fget_text_property (pos, Qpointer, string);
28346
28347 /* Change the mouse pointer according to what is under X/Y. */
28348 if (NILP (pointer)
28349 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28350 {
28351 Lisp_Object map;
28352 map = Fget_text_property (pos, Qlocal_map, string);
28353 if (!KEYMAPP (map))
28354 map = Fget_text_property (pos, Qkeymap, string);
28355 if (!KEYMAPP (map))
28356 cursor = dpyinfo->vertical_scroll_bar_cursor;
28357 }
28358 }
28359 else
28360 /* Default mode-line pointer. */
28361 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28362 }
28363 #endif
28364 }
28365
28366 /* Change the mouse face according to what is under X/Y. */
28367 if (STRINGP (string))
28368 {
28369 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28370 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28371 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28372 && glyph)
28373 {
28374 Lisp_Object b, e;
28375
28376 struct glyph * tmp_glyph;
28377
28378 int gpos;
28379 int gseq_length;
28380 int total_pixel_width;
28381 ptrdiff_t begpos, endpos, ignore;
28382
28383 int vpos, hpos;
28384
28385 b = Fprevious_single_property_change (make_number (charpos + 1),
28386 Qmouse_face, string, Qnil);
28387 if (NILP (b))
28388 begpos = 0;
28389 else
28390 begpos = XINT (b);
28391
28392 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28393 if (NILP (e))
28394 endpos = SCHARS (string);
28395 else
28396 endpos = XINT (e);
28397
28398 /* Calculate the glyph position GPOS of GLYPH in the
28399 displayed string, relative to the beginning of the
28400 highlighted part of the string.
28401
28402 Note: GPOS is different from CHARPOS. CHARPOS is the
28403 position of GLYPH in the internal string object. A mode
28404 line string format has structures which are converted to
28405 a flattened string by the Emacs Lisp interpreter. The
28406 internal string is an element of those structures. The
28407 displayed string is the flattened string. */
28408 tmp_glyph = row_start_glyph;
28409 while (tmp_glyph < glyph
28410 && (!(EQ (tmp_glyph->object, glyph->object)
28411 && begpos <= tmp_glyph->charpos
28412 && tmp_glyph->charpos < endpos)))
28413 tmp_glyph++;
28414 gpos = glyph - tmp_glyph;
28415
28416 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28417 the highlighted part of the displayed string to which
28418 GLYPH belongs. Note: GSEQ_LENGTH is different from
28419 SCHARS (STRING), because the latter returns the length of
28420 the internal string. */
28421 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28422 tmp_glyph > glyph
28423 && (!(EQ (tmp_glyph->object, glyph->object)
28424 && begpos <= tmp_glyph->charpos
28425 && tmp_glyph->charpos < endpos));
28426 tmp_glyph--)
28427 ;
28428 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28429
28430 /* Calculate the total pixel width of all the glyphs between
28431 the beginning of the highlighted area and GLYPH. */
28432 total_pixel_width = 0;
28433 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28434 total_pixel_width += tmp_glyph->pixel_width;
28435
28436 /* Pre calculation of re-rendering position. Note: X is in
28437 column units here, after the call to mode_line_string or
28438 marginal_area_string. */
28439 hpos = x - gpos;
28440 vpos = (area == ON_MODE_LINE
28441 ? (w->current_matrix)->nrows - 1
28442 : 0);
28443
28444 /* If GLYPH's position is included in the region that is
28445 already drawn in mouse face, we have nothing to do. */
28446 if ( EQ (window, hlinfo->mouse_face_window)
28447 && (!row->reversed_p
28448 ? (hlinfo->mouse_face_beg_col <= hpos
28449 && hpos < hlinfo->mouse_face_end_col)
28450 /* In R2L rows we swap BEG and END, see below. */
28451 : (hlinfo->mouse_face_end_col <= hpos
28452 && hpos < hlinfo->mouse_face_beg_col))
28453 && hlinfo->mouse_face_beg_row == vpos )
28454 return;
28455
28456 if (clear_mouse_face (hlinfo))
28457 cursor = No_Cursor;
28458
28459 if (!row->reversed_p)
28460 {
28461 hlinfo->mouse_face_beg_col = hpos;
28462 hlinfo->mouse_face_beg_x = original_x_pixel
28463 - (total_pixel_width + dx);
28464 hlinfo->mouse_face_end_col = hpos + gseq_length;
28465 hlinfo->mouse_face_end_x = 0;
28466 }
28467 else
28468 {
28469 /* In R2L rows, show_mouse_face expects BEG and END
28470 coordinates to be swapped. */
28471 hlinfo->mouse_face_end_col = hpos;
28472 hlinfo->mouse_face_end_x = original_x_pixel
28473 - (total_pixel_width + dx);
28474 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28475 hlinfo->mouse_face_beg_x = 0;
28476 }
28477
28478 hlinfo->mouse_face_beg_row = vpos;
28479 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28480 hlinfo->mouse_face_past_end = 0;
28481 hlinfo->mouse_face_window = window;
28482
28483 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28484 charpos,
28485 0, &ignore,
28486 glyph->face_id,
28487 1);
28488 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28489
28490 if (NILP (pointer))
28491 pointer = Qhand;
28492 }
28493 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28494 clear_mouse_face (hlinfo);
28495 }
28496 #ifdef HAVE_WINDOW_SYSTEM
28497 if (FRAME_WINDOW_P (f))
28498 define_frame_cursor1 (f, cursor, pointer);
28499 #endif
28500 }
28501
28502
28503 /* EXPORT:
28504 Take proper action when the mouse has moved to position X, Y on
28505 frame F with regards to highlighting portions of display that have
28506 mouse-face properties. Also de-highlight portions of display where
28507 the mouse was before, set the mouse pointer shape as appropriate
28508 for the mouse coordinates, and activate help echo (tooltips).
28509 X and Y can be negative or out of range. */
28510
28511 void
28512 note_mouse_highlight (struct frame *f, int x, int y)
28513 {
28514 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28515 enum window_part part = ON_NOTHING;
28516 Lisp_Object window;
28517 struct window *w;
28518 Cursor cursor = No_Cursor;
28519 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28520 struct buffer *b;
28521
28522 /* When a menu is active, don't highlight because this looks odd. */
28523 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28524 if (popup_activated ())
28525 return;
28526 #endif
28527
28528 if (!f->glyphs_initialized_p
28529 || f->pointer_invisible)
28530 return;
28531
28532 hlinfo->mouse_face_mouse_x = x;
28533 hlinfo->mouse_face_mouse_y = y;
28534 hlinfo->mouse_face_mouse_frame = f;
28535
28536 if (hlinfo->mouse_face_defer)
28537 return;
28538
28539 /* Which window is that in? */
28540 window = window_from_coordinates (f, x, y, &part, 1);
28541
28542 /* If displaying active text in another window, clear that. */
28543 if (! EQ (window, hlinfo->mouse_face_window)
28544 /* Also clear if we move out of text area in same window. */
28545 || (!NILP (hlinfo->mouse_face_window)
28546 && !NILP (window)
28547 && part != ON_TEXT
28548 && part != ON_MODE_LINE
28549 && part != ON_HEADER_LINE))
28550 clear_mouse_face (hlinfo);
28551
28552 /* Not on a window -> return. */
28553 if (!WINDOWP (window))
28554 return;
28555
28556 /* Reset help_echo_string. It will get recomputed below. */
28557 help_echo_string = Qnil;
28558
28559 /* Convert to window-relative pixel coordinates. */
28560 w = XWINDOW (window);
28561 frame_to_window_pixel_xy (w, &x, &y);
28562
28563 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28564 /* Handle tool-bar window differently since it doesn't display a
28565 buffer. */
28566 if (EQ (window, f->tool_bar_window))
28567 {
28568 note_tool_bar_highlight (f, x, y);
28569 return;
28570 }
28571 #endif
28572
28573 /* Mouse is on the mode, header line or margin? */
28574 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28575 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28576 {
28577 note_mode_line_or_margin_highlight (window, x, y, part);
28578 return;
28579 }
28580
28581 #ifdef HAVE_WINDOW_SYSTEM
28582 if (part == ON_VERTICAL_BORDER)
28583 {
28584 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28585 help_echo_string = build_string ("drag-mouse-1: resize");
28586 }
28587 else if (part == ON_RIGHT_DIVIDER)
28588 {
28589 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28590 help_echo_string = build_string ("drag-mouse-1: resize");
28591 }
28592 else if (part == ON_BOTTOM_DIVIDER)
28593 {
28594 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28595 help_echo_string = build_string ("drag-mouse-1: resize");
28596 }
28597 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28598 || part == ON_SCROLL_BAR)
28599 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28600 else
28601 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28602 #endif
28603
28604 /* Are we in a window whose display is up to date?
28605 And verify the buffer's text has not changed. */
28606 b = XBUFFER (w->contents);
28607 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28608 {
28609 int hpos, vpos, dx, dy, area = LAST_AREA;
28610 ptrdiff_t pos;
28611 struct glyph *glyph;
28612 Lisp_Object object;
28613 Lisp_Object mouse_face = Qnil, position;
28614 Lisp_Object *overlay_vec = NULL;
28615 ptrdiff_t i, noverlays;
28616 struct buffer *obuf;
28617 ptrdiff_t obegv, ozv;
28618 int same_region;
28619
28620 /* Find the glyph under X/Y. */
28621 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28622
28623 #ifdef HAVE_WINDOW_SYSTEM
28624 /* Look for :pointer property on image. */
28625 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28626 {
28627 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28628 if (img != NULL && IMAGEP (img->spec))
28629 {
28630 Lisp_Object image_map, hotspot;
28631 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28632 !NILP (image_map))
28633 && (hotspot = find_hot_spot (image_map,
28634 glyph->slice.img.x + dx,
28635 glyph->slice.img.y + dy),
28636 CONSP (hotspot))
28637 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28638 {
28639 Lisp_Object plist;
28640
28641 /* Could check XCAR (hotspot) to see if we enter/leave
28642 this hot-spot.
28643 If so, we could look for mouse-enter, mouse-leave
28644 properties in PLIST (and do something...). */
28645 hotspot = XCDR (hotspot);
28646 if (CONSP (hotspot)
28647 && (plist = XCAR (hotspot), CONSP (plist)))
28648 {
28649 pointer = Fplist_get (plist, Qpointer);
28650 if (NILP (pointer))
28651 pointer = Qhand;
28652 help_echo_string = Fplist_get (plist, Qhelp_echo);
28653 if (!NILP (help_echo_string))
28654 {
28655 help_echo_window = window;
28656 help_echo_object = glyph->object;
28657 help_echo_pos = glyph->charpos;
28658 }
28659 }
28660 }
28661 if (NILP (pointer))
28662 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28663 }
28664 }
28665 #endif /* HAVE_WINDOW_SYSTEM */
28666
28667 /* Clear mouse face if X/Y not over text. */
28668 if (glyph == NULL
28669 || area != TEXT_AREA
28670 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28671 /* Glyph's OBJECT is an integer for glyphs inserted by the
28672 display engine for its internal purposes, like truncation
28673 and continuation glyphs and blanks beyond the end of
28674 line's text on text terminals. If we are over such a
28675 glyph, we are not over any text. */
28676 || INTEGERP (glyph->object)
28677 /* R2L rows have a stretch glyph at their front, which
28678 stands for no text, whereas L2R rows have no glyphs at
28679 all beyond the end of text. Treat such stretch glyphs
28680 like we do with NULL glyphs in L2R rows. */
28681 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28682 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28683 && glyph->type == STRETCH_GLYPH
28684 && glyph->avoid_cursor_p))
28685 {
28686 if (clear_mouse_face (hlinfo))
28687 cursor = No_Cursor;
28688 #ifdef HAVE_WINDOW_SYSTEM
28689 if (FRAME_WINDOW_P (f) && NILP (pointer))
28690 {
28691 if (area != TEXT_AREA)
28692 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28693 else
28694 pointer = Vvoid_text_area_pointer;
28695 }
28696 #endif
28697 goto set_cursor;
28698 }
28699
28700 pos = glyph->charpos;
28701 object = glyph->object;
28702 if (!STRINGP (object) && !BUFFERP (object))
28703 goto set_cursor;
28704
28705 /* If we get an out-of-range value, return now; avoid an error. */
28706 if (BUFFERP (object) && pos > BUF_Z (b))
28707 goto set_cursor;
28708
28709 /* Make the window's buffer temporarily current for
28710 overlays_at and compute_char_face. */
28711 obuf = current_buffer;
28712 current_buffer = b;
28713 obegv = BEGV;
28714 ozv = ZV;
28715 BEGV = BEG;
28716 ZV = Z;
28717
28718 /* Is this char mouse-active or does it have help-echo? */
28719 position = make_number (pos);
28720
28721 if (BUFFERP (object))
28722 {
28723 /* Put all the overlays we want in a vector in overlay_vec. */
28724 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28725 /* Sort overlays into increasing priority order. */
28726 noverlays = sort_overlays (overlay_vec, noverlays, w);
28727 }
28728 else
28729 noverlays = 0;
28730
28731 if (NILP (Vmouse_highlight))
28732 {
28733 clear_mouse_face (hlinfo);
28734 goto check_help_echo;
28735 }
28736
28737 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28738
28739 if (same_region)
28740 cursor = No_Cursor;
28741
28742 /* Check mouse-face highlighting. */
28743 if (! same_region
28744 /* If there exists an overlay with mouse-face overlapping
28745 the one we are currently highlighting, we have to
28746 check if we enter the overlapping overlay, and then
28747 highlight only that. */
28748 || (OVERLAYP (hlinfo->mouse_face_overlay)
28749 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28750 {
28751 /* Find the highest priority overlay with a mouse-face. */
28752 Lisp_Object overlay = Qnil;
28753 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28754 {
28755 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28756 if (!NILP (mouse_face))
28757 overlay = overlay_vec[i];
28758 }
28759
28760 /* If we're highlighting the same overlay as before, there's
28761 no need to do that again. */
28762 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28763 goto check_help_echo;
28764 hlinfo->mouse_face_overlay = overlay;
28765
28766 /* Clear the display of the old active region, if any. */
28767 if (clear_mouse_face (hlinfo))
28768 cursor = No_Cursor;
28769
28770 /* If no overlay applies, get a text property. */
28771 if (NILP (overlay))
28772 mouse_face = Fget_text_property (position, Qmouse_face, object);
28773
28774 /* Next, compute the bounds of the mouse highlighting and
28775 display it. */
28776 if (!NILP (mouse_face) && STRINGP (object))
28777 {
28778 /* The mouse-highlighting comes from a display string
28779 with a mouse-face. */
28780 Lisp_Object s, e;
28781 ptrdiff_t ignore;
28782
28783 s = Fprevious_single_property_change
28784 (make_number (pos + 1), Qmouse_face, object, Qnil);
28785 e = Fnext_single_property_change
28786 (position, Qmouse_face, object, Qnil);
28787 if (NILP (s))
28788 s = make_number (0);
28789 if (NILP (e))
28790 e = make_number (SCHARS (object));
28791 mouse_face_from_string_pos (w, hlinfo, object,
28792 XINT (s), XINT (e));
28793 hlinfo->mouse_face_past_end = 0;
28794 hlinfo->mouse_face_window = window;
28795 hlinfo->mouse_face_face_id
28796 = face_at_string_position (w, object, pos, 0, &ignore,
28797 glyph->face_id, 1);
28798 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28799 cursor = No_Cursor;
28800 }
28801 else
28802 {
28803 /* The mouse-highlighting, if any, comes from an overlay
28804 or text property in the buffer. */
28805 Lisp_Object buffer IF_LINT (= Qnil);
28806 Lisp_Object disp_string IF_LINT (= Qnil);
28807
28808 if (STRINGP (object))
28809 {
28810 /* If we are on a display string with no mouse-face,
28811 check if the text under it has one. */
28812 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28813 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28814 pos = string_buffer_position (object, start);
28815 if (pos > 0)
28816 {
28817 mouse_face = get_char_property_and_overlay
28818 (make_number (pos), Qmouse_face, w->contents, &overlay);
28819 buffer = w->contents;
28820 disp_string = object;
28821 }
28822 }
28823 else
28824 {
28825 buffer = object;
28826 disp_string = Qnil;
28827 }
28828
28829 if (!NILP (mouse_face))
28830 {
28831 Lisp_Object before, after;
28832 Lisp_Object before_string, after_string;
28833 /* To correctly find the limits of mouse highlight
28834 in a bidi-reordered buffer, we must not use the
28835 optimization of limiting the search in
28836 previous-single-property-change and
28837 next-single-property-change, because
28838 rows_from_pos_range needs the real start and end
28839 positions to DTRT in this case. That's because
28840 the first row visible in a window does not
28841 necessarily display the character whose position
28842 is the smallest. */
28843 Lisp_Object lim1
28844 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28845 ? Fmarker_position (w->start)
28846 : Qnil;
28847 Lisp_Object lim2
28848 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28849 ? make_number (BUF_Z (XBUFFER (buffer))
28850 - w->window_end_pos)
28851 : Qnil;
28852
28853 if (NILP (overlay))
28854 {
28855 /* Handle the text property case. */
28856 before = Fprevious_single_property_change
28857 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28858 after = Fnext_single_property_change
28859 (make_number (pos), Qmouse_face, buffer, lim2);
28860 before_string = after_string = Qnil;
28861 }
28862 else
28863 {
28864 /* Handle the overlay case. */
28865 before = Foverlay_start (overlay);
28866 after = Foverlay_end (overlay);
28867 before_string = Foverlay_get (overlay, Qbefore_string);
28868 after_string = Foverlay_get (overlay, Qafter_string);
28869
28870 if (!STRINGP (before_string)) before_string = Qnil;
28871 if (!STRINGP (after_string)) after_string = Qnil;
28872 }
28873
28874 mouse_face_from_buffer_pos (window, hlinfo, pos,
28875 NILP (before)
28876 ? 1
28877 : XFASTINT (before),
28878 NILP (after)
28879 ? BUF_Z (XBUFFER (buffer))
28880 : XFASTINT (after),
28881 before_string, after_string,
28882 disp_string);
28883 cursor = No_Cursor;
28884 }
28885 }
28886 }
28887
28888 check_help_echo:
28889
28890 /* Look for a `help-echo' property. */
28891 if (NILP (help_echo_string)) {
28892 Lisp_Object help, overlay;
28893
28894 /* Check overlays first. */
28895 help = overlay = Qnil;
28896 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28897 {
28898 overlay = overlay_vec[i];
28899 help = Foverlay_get (overlay, Qhelp_echo);
28900 }
28901
28902 if (!NILP (help))
28903 {
28904 help_echo_string = help;
28905 help_echo_window = window;
28906 help_echo_object = overlay;
28907 help_echo_pos = pos;
28908 }
28909 else
28910 {
28911 Lisp_Object obj = glyph->object;
28912 ptrdiff_t charpos = glyph->charpos;
28913
28914 /* Try text properties. */
28915 if (STRINGP (obj)
28916 && charpos >= 0
28917 && charpos < SCHARS (obj))
28918 {
28919 help = Fget_text_property (make_number (charpos),
28920 Qhelp_echo, obj);
28921 if (NILP (help))
28922 {
28923 /* If the string itself doesn't specify a help-echo,
28924 see if the buffer text ``under'' it does. */
28925 struct glyph_row *r
28926 = MATRIX_ROW (w->current_matrix, vpos);
28927 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28928 ptrdiff_t p = string_buffer_position (obj, start);
28929 if (p > 0)
28930 {
28931 help = Fget_char_property (make_number (p),
28932 Qhelp_echo, w->contents);
28933 if (!NILP (help))
28934 {
28935 charpos = p;
28936 obj = w->contents;
28937 }
28938 }
28939 }
28940 }
28941 else if (BUFFERP (obj)
28942 && charpos >= BEGV
28943 && charpos < ZV)
28944 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28945 obj);
28946
28947 if (!NILP (help))
28948 {
28949 help_echo_string = help;
28950 help_echo_window = window;
28951 help_echo_object = obj;
28952 help_echo_pos = charpos;
28953 }
28954 }
28955 }
28956
28957 #ifdef HAVE_WINDOW_SYSTEM
28958 /* Look for a `pointer' property. */
28959 if (FRAME_WINDOW_P (f) && NILP (pointer))
28960 {
28961 /* Check overlays first. */
28962 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28963 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28964
28965 if (NILP (pointer))
28966 {
28967 Lisp_Object obj = glyph->object;
28968 ptrdiff_t charpos = glyph->charpos;
28969
28970 /* Try text properties. */
28971 if (STRINGP (obj)
28972 && charpos >= 0
28973 && charpos < SCHARS (obj))
28974 {
28975 pointer = Fget_text_property (make_number (charpos),
28976 Qpointer, obj);
28977 if (NILP (pointer))
28978 {
28979 /* If the string itself doesn't specify a pointer,
28980 see if the buffer text ``under'' it does. */
28981 struct glyph_row *r
28982 = MATRIX_ROW (w->current_matrix, vpos);
28983 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28984 ptrdiff_t p = string_buffer_position (obj, start);
28985 if (p > 0)
28986 pointer = Fget_char_property (make_number (p),
28987 Qpointer, w->contents);
28988 }
28989 }
28990 else if (BUFFERP (obj)
28991 && charpos >= BEGV
28992 && charpos < ZV)
28993 pointer = Fget_text_property (make_number (charpos),
28994 Qpointer, obj);
28995 }
28996 }
28997 #endif /* HAVE_WINDOW_SYSTEM */
28998
28999 BEGV = obegv;
29000 ZV = ozv;
29001 current_buffer = obuf;
29002 }
29003
29004 set_cursor:
29005
29006 #ifdef HAVE_WINDOW_SYSTEM
29007 if (FRAME_WINDOW_P (f))
29008 define_frame_cursor1 (f, cursor, pointer);
29009 #else
29010 /* This is here to prevent a compiler error, about "label at end of
29011 compound statement". */
29012 return;
29013 #endif
29014 }
29015
29016
29017 /* EXPORT for RIF:
29018 Clear any mouse-face on window W. This function is part of the
29019 redisplay interface, and is called from try_window_id and similar
29020 functions to ensure the mouse-highlight is off. */
29021
29022 void
29023 x_clear_window_mouse_face (struct window *w)
29024 {
29025 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29026 Lisp_Object window;
29027
29028 block_input ();
29029 XSETWINDOW (window, w);
29030 if (EQ (window, hlinfo->mouse_face_window))
29031 clear_mouse_face (hlinfo);
29032 unblock_input ();
29033 }
29034
29035
29036 /* EXPORT:
29037 Just discard the mouse face information for frame F, if any.
29038 This is used when the size of F is changed. */
29039
29040 void
29041 cancel_mouse_face (struct frame *f)
29042 {
29043 Lisp_Object window;
29044 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29045
29046 window = hlinfo->mouse_face_window;
29047 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29048 reset_mouse_highlight (hlinfo);
29049 }
29050
29051
29052 \f
29053 /***********************************************************************
29054 Exposure Events
29055 ***********************************************************************/
29056
29057 #ifdef HAVE_WINDOW_SYSTEM
29058
29059 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29060 which intersects rectangle R. R is in window-relative coordinates. */
29061
29062 static void
29063 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29064 enum glyph_row_area area)
29065 {
29066 struct glyph *first = row->glyphs[area];
29067 struct glyph *end = row->glyphs[area] + row->used[area];
29068 struct glyph *last;
29069 int first_x, start_x, x;
29070
29071 if (area == TEXT_AREA && row->fill_line_p)
29072 /* If row extends face to end of line write the whole line. */
29073 draw_glyphs (w, 0, row, area,
29074 0, row->used[area],
29075 DRAW_NORMAL_TEXT, 0);
29076 else
29077 {
29078 /* Set START_X to the window-relative start position for drawing glyphs of
29079 AREA. The first glyph of the text area can be partially visible.
29080 The first glyphs of other areas cannot. */
29081 start_x = window_box_left_offset (w, area);
29082 x = start_x;
29083 if (area == TEXT_AREA)
29084 x += row->x;
29085
29086 /* Find the first glyph that must be redrawn. */
29087 while (first < end
29088 && x + first->pixel_width < r->x)
29089 {
29090 x += first->pixel_width;
29091 ++first;
29092 }
29093
29094 /* Find the last one. */
29095 last = first;
29096 first_x = x;
29097 while (last < end
29098 && x < r->x + r->width)
29099 {
29100 x += last->pixel_width;
29101 ++last;
29102 }
29103
29104 /* Repaint. */
29105 if (last > first)
29106 draw_glyphs (w, first_x - start_x, row, area,
29107 first - row->glyphs[area], last - row->glyphs[area],
29108 DRAW_NORMAL_TEXT, 0);
29109 }
29110 }
29111
29112
29113 /* Redraw the parts of the glyph row ROW on window W intersecting
29114 rectangle R. R is in window-relative coordinates. Value is
29115 non-zero if mouse-face was overwritten. */
29116
29117 static int
29118 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29119 {
29120 eassert (row->enabled_p);
29121
29122 if (row->mode_line_p || w->pseudo_window_p)
29123 draw_glyphs (w, 0, row, TEXT_AREA,
29124 0, row->used[TEXT_AREA],
29125 DRAW_NORMAL_TEXT, 0);
29126 else
29127 {
29128 if (row->used[LEFT_MARGIN_AREA])
29129 expose_area (w, row, r, LEFT_MARGIN_AREA);
29130 if (row->used[TEXT_AREA])
29131 expose_area (w, row, r, TEXT_AREA);
29132 if (row->used[RIGHT_MARGIN_AREA])
29133 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29134 draw_row_fringe_bitmaps (w, row);
29135 }
29136
29137 return row->mouse_face_p;
29138 }
29139
29140
29141 /* Redraw those parts of glyphs rows during expose event handling that
29142 overlap other rows. Redrawing of an exposed line writes over parts
29143 of lines overlapping that exposed line; this function fixes that.
29144
29145 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29146 row in W's current matrix that is exposed and overlaps other rows.
29147 LAST_OVERLAPPING_ROW is the last such row. */
29148
29149 static void
29150 expose_overlaps (struct window *w,
29151 struct glyph_row *first_overlapping_row,
29152 struct glyph_row *last_overlapping_row,
29153 XRectangle *r)
29154 {
29155 struct glyph_row *row;
29156
29157 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29158 if (row->overlapping_p)
29159 {
29160 eassert (row->enabled_p && !row->mode_line_p);
29161
29162 row->clip = r;
29163 if (row->used[LEFT_MARGIN_AREA])
29164 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29165
29166 if (row->used[TEXT_AREA])
29167 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29168
29169 if (row->used[RIGHT_MARGIN_AREA])
29170 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29171 row->clip = NULL;
29172 }
29173 }
29174
29175
29176 /* Return non-zero if W's cursor intersects rectangle R. */
29177
29178 static int
29179 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29180 {
29181 XRectangle cr, result;
29182 struct glyph *cursor_glyph;
29183 struct glyph_row *row;
29184
29185 if (w->phys_cursor.vpos >= 0
29186 && w->phys_cursor.vpos < w->current_matrix->nrows
29187 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29188 row->enabled_p)
29189 && row->cursor_in_fringe_p)
29190 {
29191 /* Cursor is in the fringe. */
29192 cr.x = window_box_right_offset (w,
29193 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29194 ? RIGHT_MARGIN_AREA
29195 : TEXT_AREA));
29196 cr.y = row->y;
29197 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29198 cr.height = row->height;
29199 return x_intersect_rectangles (&cr, r, &result);
29200 }
29201
29202 cursor_glyph = get_phys_cursor_glyph (w);
29203 if (cursor_glyph)
29204 {
29205 /* r is relative to W's box, but w->phys_cursor.x is relative
29206 to left edge of W's TEXT area. Adjust it. */
29207 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29208 cr.y = w->phys_cursor.y;
29209 cr.width = cursor_glyph->pixel_width;
29210 cr.height = w->phys_cursor_height;
29211 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29212 I assume the effect is the same -- and this is portable. */
29213 return x_intersect_rectangles (&cr, r, &result);
29214 }
29215 /* If we don't understand the format, pretend we're not in the hot-spot. */
29216 return 0;
29217 }
29218
29219
29220 /* EXPORT:
29221 Draw a vertical window border to the right of window W if W doesn't
29222 have vertical scroll bars. */
29223
29224 void
29225 x_draw_vertical_border (struct window *w)
29226 {
29227 struct frame *f = XFRAME (WINDOW_FRAME (w));
29228
29229 /* We could do better, if we knew what type of scroll-bar the adjacent
29230 windows (on either side) have... But we don't :-(
29231 However, I think this works ok. ++KFS 2003-04-25 */
29232
29233 /* Redraw borders between horizontally adjacent windows. Don't
29234 do it for frames with vertical scroll bars because either the
29235 right scroll bar of a window, or the left scroll bar of its
29236 neighbor will suffice as a border. */
29237 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29238 return;
29239
29240 /* Note: It is necessary to redraw both the left and the right
29241 borders, for when only this single window W is being
29242 redisplayed. */
29243 if (!WINDOW_RIGHTMOST_P (w)
29244 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29245 {
29246 int x0, x1, y0, y1;
29247
29248 window_box_edges (w, &x0, &y0, &x1, &y1);
29249 y1 -= 1;
29250
29251 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29252 x1 -= 1;
29253
29254 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29255 }
29256
29257 if (!WINDOW_LEFTMOST_P (w)
29258 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29259 {
29260 int x0, x1, y0, y1;
29261
29262 window_box_edges (w, &x0, &y0, &x1, &y1);
29263 y1 -= 1;
29264
29265 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29266 x0 -= 1;
29267
29268 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29269 }
29270 }
29271
29272
29273 /* Draw window dividers for window W. */
29274
29275 void
29276 x_draw_right_divider (struct window *w)
29277 {
29278 struct frame *f = WINDOW_XFRAME (w);
29279
29280 if (w->mini || w->pseudo_window_p)
29281 return;
29282 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29283 {
29284 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29285 int x1 = WINDOW_RIGHT_EDGE_X (w);
29286 int y0 = WINDOW_TOP_EDGE_Y (w);
29287 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29288
29289 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29290 }
29291 }
29292
29293 static void
29294 x_draw_bottom_divider (struct window *w)
29295 {
29296 struct frame *f = XFRAME (WINDOW_FRAME (w));
29297
29298 if (w->mini || w->pseudo_window_p)
29299 return;
29300 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29301 {
29302 int x0 = WINDOW_LEFT_EDGE_X (w);
29303 int x1 = WINDOW_RIGHT_EDGE_X (w);
29304 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29305 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29306
29307 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29308 }
29309 }
29310
29311 /* Redraw the part of window W intersection rectangle FR. Pixel
29312 coordinates in FR are frame-relative. Call this function with
29313 input blocked. Value is non-zero if the exposure overwrites
29314 mouse-face. */
29315
29316 static int
29317 expose_window (struct window *w, XRectangle *fr)
29318 {
29319 struct frame *f = XFRAME (w->frame);
29320 XRectangle wr, r;
29321 int mouse_face_overwritten_p = 0;
29322
29323 /* If window is not yet fully initialized, do nothing. This can
29324 happen when toolkit scroll bars are used and a window is split.
29325 Reconfiguring the scroll bar will generate an expose for a newly
29326 created window. */
29327 if (w->current_matrix == NULL)
29328 return 0;
29329
29330 /* When we're currently updating the window, display and current
29331 matrix usually don't agree. Arrange for a thorough display
29332 later. */
29333 if (w->must_be_updated_p)
29334 {
29335 SET_FRAME_GARBAGED (f);
29336 return 0;
29337 }
29338
29339 /* Frame-relative pixel rectangle of W. */
29340 wr.x = WINDOW_LEFT_EDGE_X (w);
29341 wr.y = WINDOW_TOP_EDGE_Y (w);
29342 wr.width = WINDOW_PIXEL_WIDTH (w);
29343 wr.height = WINDOW_PIXEL_HEIGHT (w);
29344
29345 if (x_intersect_rectangles (fr, &wr, &r))
29346 {
29347 int yb = window_text_bottom_y (w);
29348 struct glyph_row *row;
29349 int cursor_cleared_p, phys_cursor_on_p;
29350 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29351
29352 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29353 r.x, r.y, r.width, r.height));
29354
29355 /* Convert to window coordinates. */
29356 r.x -= WINDOW_LEFT_EDGE_X (w);
29357 r.y -= WINDOW_TOP_EDGE_Y (w);
29358
29359 /* Turn off the cursor. */
29360 if (!w->pseudo_window_p
29361 && phys_cursor_in_rect_p (w, &r))
29362 {
29363 x_clear_cursor (w);
29364 cursor_cleared_p = 1;
29365 }
29366 else
29367 cursor_cleared_p = 0;
29368
29369 /* If the row containing the cursor extends face to end of line,
29370 then expose_area might overwrite the cursor outside the
29371 rectangle and thus notice_overwritten_cursor might clear
29372 w->phys_cursor_on_p. We remember the original value and
29373 check later if it is changed. */
29374 phys_cursor_on_p = w->phys_cursor_on_p;
29375
29376 /* Update lines intersecting rectangle R. */
29377 first_overlapping_row = last_overlapping_row = NULL;
29378 for (row = w->current_matrix->rows;
29379 row->enabled_p;
29380 ++row)
29381 {
29382 int y0 = row->y;
29383 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29384
29385 if ((y0 >= r.y && y0 < r.y + r.height)
29386 || (y1 > r.y && y1 < r.y + r.height)
29387 || (r.y >= y0 && r.y < y1)
29388 || (r.y + r.height > y0 && r.y + r.height < y1))
29389 {
29390 /* A header line may be overlapping, but there is no need
29391 to fix overlapping areas for them. KFS 2005-02-12 */
29392 if (row->overlapping_p && !row->mode_line_p)
29393 {
29394 if (first_overlapping_row == NULL)
29395 first_overlapping_row = row;
29396 last_overlapping_row = row;
29397 }
29398
29399 row->clip = fr;
29400 if (expose_line (w, row, &r))
29401 mouse_face_overwritten_p = 1;
29402 row->clip = NULL;
29403 }
29404 else if (row->overlapping_p)
29405 {
29406 /* We must redraw a row overlapping the exposed area. */
29407 if (y0 < r.y
29408 ? y0 + row->phys_height > r.y
29409 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29410 {
29411 if (first_overlapping_row == NULL)
29412 first_overlapping_row = row;
29413 last_overlapping_row = row;
29414 }
29415 }
29416
29417 if (y1 >= yb)
29418 break;
29419 }
29420
29421 /* Display the mode line if there is one. */
29422 if (WINDOW_WANTS_MODELINE_P (w)
29423 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29424 row->enabled_p)
29425 && row->y < r.y + r.height)
29426 {
29427 if (expose_line (w, row, &r))
29428 mouse_face_overwritten_p = 1;
29429 }
29430
29431 if (!w->pseudo_window_p)
29432 {
29433 /* Fix the display of overlapping rows. */
29434 if (first_overlapping_row)
29435 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29436 fr);
29437
29438 /* Draw border between windows. */
29439 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29440 x_draw_right_divider (w);
29441 else
29442 x_draw_vertical_border (w);
29443
29444 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29445 x_draw_bottom_divider (w);
29446
29447 /* Turn the cursor on again. */
29448 if (cursor_cleared_p
29449 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29450 update_window_cursor (w, 1);
29451 }
29452 }
29453
29454 return mouse_face_overwritten_p;
29455 }
29456
29457
29458
29459 /* Redraw (parts) of all windows in the window tree rooted at W that
29460 intersect R. R contains frame pixel coordinates. Value is
29461 non-zero if the exposure overwrites mouse-face. */
29462
29463 static int
29464 expose_window_tree (struct window *w, XRectangle *r)
29465 {
29466 struct frame *f = XFRAME (w->frame);
29467 int mouse_face_overwritten_p = 0;
29468
29469 while (w && !FRAME_GARBAGED_P (f))
29470 {
29471 if (WINDOWP (w->contents))
29472 mouse_face_overwritten_p
29473 |= expose_window_tree (XWINDOW (w->contents), r);
29474 else
29475 mouse_face_overwritten_p |= expose_window (w, r);
29476
29477 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29478 }
29479
29480 return mouse_face_overwritten_p;
29481 }
29482
29483
29484 /* EXPORT:
29485 Redisplay an exposed area of frame F. X and Y are the upper-left
29486 corner of the exposed rectangle. W and H are width and height of
29487 the exposed area. All are pixel values. W or H zero means redraw
29488 the entire frame. */
29489
29490 void
29491 expose_frame (struct frame *f, int x, int y, int w, int h)
29492 {
29493 XRectangle r;
29494 int mouse_face_overwritten_p = 0;
29495
29496 TRACE ((stderr, "expose_frame "));
29497
29498 /* No need to redraw if frame will be redrawn soon. */
29499 if (FRAME_GARBAGED_P (f))
29500 {
29501 TRACE ((stderr, " garbaged\n"));
29502 return;
29503 }
29504
29505 /* If basic faces haven't been realized yet, there is no point in
29506 trying to redraw anything. This can happen when we get an expose
29507 event while Emacs is starting, e.g. by moving another window. */
29508 if (FRAME_FACE_CACHE (f) == NULL
29509 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29510 {
29511 TRACE ((stderr, " no faces\n"));
29512 return;
29513 }
29514
29515 if (w == 0 || h == 0)
29516 {
29517 r.x = r.y = 0;
29518 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29519 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29520 }
29521 else
29522 {
29523 r.x = x;
29524 r.y = y;
29525 r.width = w;
29526 r.height = h;
29527 }
29528
29529 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29530 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29531
29532 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29533 if (WINDOWP (f->tool_bar_window))
29534 mouse_face_overwritten_p
29535 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29536 #endif
29537
29538 #ifdef HAVE_X_WINDOWS
29539 #ifndef MSDOS
29540 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29541 if (WINDOWP (f->menu_bar_window))
29542 mouse_face_overwritten_p
29543 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29544 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29545 #endif
29546 #endif
29547
29548 /* Some window managers support a focus-follows-mouse style with
29549 delayed raising of frames. Imagine a partially obscured frame,
29550 and moving the mouse into partially obscured mouse-face on that
29551 frame. The visible part of the mouse-face will be highlighted,
29552 then the WM raises the obscured frame. With at least one WM, KDE
29553 2.1, Emacs is not getting any event for the raising of the frame
29554 (even tried with SubstructureRedirectMask), only Expose events.
29555 These expose events will draw text normally, i.e. not
29556 highlighted. Which means we must redo the highlight here.
29557 Subsume it under ``we love X''. --gerd 2001-08-15 */
29558 /* Included in Windows version because Windows most likely does not
29559 do the right thing if any third party tool offers
29560 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29561 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29562 {
29563 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29564 if (f == hlinfo->mouse_face_mouse_frame)
29565 {
29566 int mouse_x = hlinfo->mouse_face_mouse_x;
29567 int mouse_y = hlinfo->mouse_face_mouse_y;
29568 clear_mouse_face (hlinfo);
29569 note_mouse_highlight (f, mouse_x, mouse_y);
29570 }
29571 }
29572 }
29573
29574
29575 /* EXPORT:
29576 Determine the intersection of two rectangles R1 and R2. Return
29577 the intersection in *RESULT. Value is non-zero if RESULT is not
29578 empty. */
29579
29580 int
29581 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29582 {
29583 XRectangle *left, *right;
29584 XRectangle *upper, *lower;
29585 int intersection_p = 0;
29586
29587 /* Rearrange so that R1 is the left-most rectangle. */
29588 if (r1->x < r2->x)
29589 left = r1, right = r2;
29590 else
29591 left = r2, right = r1;
29592
29593 /* X0 of the intersection is right.x0, if this is inside R1,
29594 otherwise there is no intersection. */
29595 if (right->x <= left->x + left->width)
29596 {
29597 result->x = right->x;
29598
29599 /* The right end of the intersection is the minimum of
29600 the right ends of left and right. */
29601 result->width = (min (left->x + left->width, right->x + right->width)
29602 - result->x);
29603
29604 /* Same game for Y. */
29605 if (r1->y < r2->y)
29606 upper = r1, lower = r2;
29607 else
29608 upper = r2, lower = r1;
29609
29610 /* The upper end of the intersection is lower.y0, if this is inside
29611 of upper. Otherwise, there is no intersection. */
29612 if (lower->y <= upper->y + upper->height)
29613 {
29614 result->y = lower->y;
29615
29616 /* The lower end of the intersection is the minimum of the lower
29617 ends of upper and lower. */
29618 result->height = (min (lower->y + lower->height,
29619 upper->y + upper->height)
29620 - result->y);
29621 intersection_p = 1;
29622 }
29623 }
29624
29625 return intersection_p;
29626 }
29627
29628 #endif /* HAVE_WINDOW_SYSTEM */
29629
29630 \f
29631 /***********************************************************************
29632 Initialization
29633 ***********************************************************************/
29634
29635 void
29636 syms_of_xdisp (void)
29637 {
29638 Vwith_echo_area_save_vector = Qnil;
29639 staticpro (&Vwith_echo_area_save_vector);
29640
29641 Vmessage_stack = Qnil;
29642 staticpro (&Vmessage_stack);
29643
29644 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29645 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29646
29647 message_dolog_marker1 = Fmake_marker ();
29648 staticpro (&message_dolog_marker1);
29649 message_dolog_marker2 = Fmake_marker ();
29650 staticpro (&message_dolog_marker2);
29651 message_dolog_marker3 = Fmake_marker ();
29652 staticpro (&message_dolog_marker3);
29653
29654 #ifdef GLYPH_DEBUG
29655 defsubr (&Sdump_frame_glyph_matrix);
29656 defsubr (&Sdump_glyph_matrix);
29657 defsubr (&Sdump_glyph_row);
29658 defsubr (&Sdump_tool_bar_row);
29659 defsubr (&Strace_redisplay);
29660 defsubr (&Strace_to_stderr);
29661 #endif
29662 #ifdef HAVE_WINDOW_SYSTEM
29663 defsubr (&Stool_bar_height);
29664 defsubr (&Slookup_image_map);
29665 #endif
29666 defsubr (&Sline_pixel_height);
29667 defsubr (&Sformat_mode_line);
29668 defsubr (&Sinvisible_p);
29669 defsubr (&Scurrent_bidi_paragraph_direction);
29670 defsubr (&Swindow_text_pixel_size);
29671 defsubr (&Smove_point_visually);
29672
29673 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29674 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29675 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29676 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29677 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29678 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29679 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29680 DEFSYM (Qeval, "eval");
29681 DEFSYM (QCdata, ":data");
29682 DEFSYM (Qdisplay, "display");
29683 DEFSYM (Qspace_width, "space-width");
29684 DEFSYM (Qraise, "raise");
29685 DEFSYM (Qslice, "slice");
29686 DEFSYM (Qspace, "space");
29687 DEFSYM (Qmargin, "margin");
29688 DEFSYM (Qpointer, "pointer");
29689 DEFSYM (Qleft_margin, "left-margin");
29690 DEFSYM (Qright_margin, "right-margin");
29691 DEFSYM (Qcenter, "center");
29692 DEFSYM (Qline_height, "line-height");
29693 DEFSYM (QCalign_to, ":align-to");
29694 DEFSYM (QCrelative_width, ":relative-width");
29695 DEFSYM (QCrelative_height, ":relative-height");
29696 DEFSYM (QCeval, ":eval");
29697 DEFSYM (QCpropertize, ":propertize");
29698 DEFSYM (QCfile, ":file");
29699 DEFSYM (Qfontified, "fontified");
29700 DEFSYM (Qfontification_functions, "fontification-functions");
29701 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29702 DEFSYM (Qescape_glyph, "escape-glyph");
29703 DEFSYM (Qnobreak_space, "nobreak-space");
29704 DEFSYM (Qimage, "image");
29705 DEFSYM (Qtext, "text");
29706 DEFSYM (Qboth, "both");
29707 DEFSYM (Qboth_horiz, "both-horiz");
29708 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29709 DEFSYM (QCmap, ":map");
29710 DEFSYM (QCpointer, ":pointer");
29711 DEFSYM (Qrect, "rect");
29712 DEFSYM (Qcircle, "circle");
29713 DEFSYM (Qpoly, "poly");
29714 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29715 DEFSYM (Qgrow_only, "grow-only");
29716 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29717 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29718 DEFSYM (Qposition, "position");
29719 DEFSYM (Qbuffer_position, "buffer-position");
29720 DEFSYM (Qobject, "object");
29721 DEFSYM (Qbar, "bar");
29722 DEFSYM (Qhbar, "hbar");
29723 DEFSYM (Qbox, "box");
29724 DEFSYM (Qhollow, "hollow");
29725 DEFSYM (Qhand, "hand");
29726 DEFSYM (Qarrow, "arrow");
29727 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29728
29729 list_of_error = list1 (list2 (intern_c_string ("error"),
29730 intern_c_string ("void-variable")));
29731 staticpro (&list_of_error);
29732
29733 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29734 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29735 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29736 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29737
29738 echo_buffer[0] = echo_buffer[1] = Qnil;
29739 staticpro (&echo_buffer[0]);
29740 staticpro (&echo_buffer[1]);
29741
29742 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29743 staticpro (&echo_area_buffer[0]);
29744 staticpro (&echo_area_buffer[1]);
29745
29746 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29747 staticpro (&Vmessages_buffer_name);
29748
29749 mode_line_proptrans_alist = Qnil;
29750 staticpro (&mode_line_proptrans_alist);
29751 mode_line_string_list = Qnil;
29752 staticpro (&mode_line_string_list);
29753 mode_line_string_face = Qnil;
29754 staticpro (&mode_line_string_face);
29755 mode_line_string_face_prop = Qnil;
29756 staticpro (&mode_line_string_face_prop);
29757 Vmode_line_unwind_vector = Qnil;
29758 staticpro (&Vmode_line_unwind_vector);
29759
29760 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29761
29762 help_echo_string = Qnil;
29763 staticpro (&help_echo_string);
29764 help_echo_object = Qnil;
29765 staticpro (&help_echo_object);
29766 help_echo_window = Qnil;
29767 staticpro (&help_echo_window);
29768 previous_help_echo_string = Qnil;
29769 staticpro (&previous_help_echo_string);
29770 help_echo_pos = -1;
29771
29772 DEFSYM (Qright_to_left, "right-to-left");
29773 DEFSYM (Qleft_to_right, "left-to-right");
29774
29775 #ifdef HAVE_WINDOW_SYSTEM
29776 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29777 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29778 For example, if a block cursor is over a tab, it will be drawn as
29779 wide as that tab on the display. */);
29780 x_stretch_cursor_p = 0;
29781 #endif
29782
29783 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29784 doc: /* Non-nil means highlight trailing whitespace.
29785 The face used for trailing whitespace is `trailing-whitespace'. */);
29786 Vshow_trailing_whitespace = Qnil;
29787
29788 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29789 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29790 If the value is t, Emacs highlights non-ASCII chars which have the
29791 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29792 or `escape-glyph' face respectively.
29793
29794 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29795 U+2011 (non-breaking hyphen) are affected.
29796
29797 Any other non-nil value means to display these characters as a escape
29798 glyph followed by an ordinary space or hyphen.
29799
29800 A value of nil means no special handling of these characters. */);
29801 Vnobreak_char_display = Qt;
29802
29803 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29804 doc: /* The pointer shape to show in void text areas.
29805 A value of nil means to show the text pointer. Other options are `arrow',
29806 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29807 Vvoid_text_area_pointer = Qarrow;
29808
29809 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29810 doc: /* Non-nil means don't actually do any redisplay.
29811 This is used for internal purposes. */);
29812 Vinhibit_redisplay = Qnil;
29813
29814 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29815 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29816 Vglobal_mode_string = Qnil;
29817
29818 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29819 doc: /* Marker for where to display an arrow on top of the buffer text.
29820 This must be the beginning of a line in order to work.
29821 See also `overlay-arrow-string'. */);
29822 Voverlay_arrow_position = Qnil;
29823
29824 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29825 doc: /* String to display as an arrow in non-window frames.
29826 See also `overlay-arrow-position'. */);
29827 Voverlay_arrow_string = build_pure_c_string ("=>");
29828
29829 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29830 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29831 The symbols on this list are examined during redisplay to determine
29832 where to display overlay arrows. */);
29833 Voverlay_arrow_variable_list
29834 = list1 (intern_c_string ("overlay-arrow-position"));
29835
29836 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29837 doc: /* The number of lines to try scrolling a window by when point moves out.
29838 If that fails to bring point back on frame, point is centered instead.
29839 If this is zero, point is always centered after it moves off frame.
29840 If you want scrolling to always be a line at a time, you should set
29841 `scroll-conservatively' to a large value rather than set this to 1. */);
29842
29843 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29844 doc: /* Scroll up to this many lines, to bring point back on screen.
29845 If point moves off-screen, redisplay will scroll by up to
29846 `scroll-conservatively' lines in order to bring point just barely
29847 onto the screen again. If that cannot be done, then redisplay
29848 recenters point as usual.
29849
29850 If the value is greater than 100, redisplay will never recenter point,
29851 but will always scroll just enough text to bring point into view, even
29852 if you move far away.
29853
29854 A value of zero means always recenter point if it moves off screen. */);
29855 scroll_conservatively = 0;
29856
29857 DEFVAR_INT ("scroll-margin", scroll_margin,
29858 doc: /* Number of lines of margin at the top and bottom of a window.
29859 Recenter the window whenever point gets within this many lines
29860 of the top or bottom of the window. */);
29861 scroll_margin = 0;
29862
29863 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29864 doc: /* Pixels per inch value for non-window system displays.
29865 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29866 Vdisplay_pixels_per_inch = make_float (72.0);
29867
29868 #ifdef GLYPH_DEBUG
29869 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29870 #endif
29871
29872 DEFVAR_LISP ("truncate-partial-width-windows",
29873 Vtruncate_partial_width_windows,
29874 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29875 For an integer value, truncate lines in each window narrower than the
29876 full frame width, provided the window width is less than that integer;
29877 otherwise, respect the value of `truncate-lines'.
29878
29879 For any other non-nil value, truncate lines in all windows that do
29880 not span the full frame width.
29881
29882 A value of nil means to respect the value of `truncate-lines'.
29883
29884 If `word-wrap' is enabled, you might want to reduce this. */);
29885 Vtruncate_partial_width_windows = make_number (50);
29886
29887 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29888 doc: /* Maximum buffer size for which line number should be displayed.
29889 If the buffer is bigger than this, the line number does not appear
29890 in the mode line. A value of nil means no limit. */);
29891 Vline_number_display_limit = Qnil;
29892
29893 DEFVAR_INT ("line-number-display-limit-width",
29894 line_number_display_limit_width,
29895 doc: /* Maximum line width (in characters) for line number display.
29896 If the average length of the lines near point is bigger than this, then the
29897 line number may be omitted from the mode line. */);
29898 line_number_display_limit_width = 200;
29899
29900 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29901 doc: /* Non-nil means highlight region even in nonselected windows. */);
29902 highlight_nonselected_windows = 0;
29903
29904 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29905 doc: /* Non-nil if more than one frame is visible on this display.
29906 Minibuffer-only frames don't count, but iconified frames do.
29907 This variable is not guaranteed to be accurate except while processing
29908 `frame-title-format' and `icon-title-format'. */);
29909
29910 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29911 doc: /* Template for displaying the title bar of visible frames.
29912 \(Assuming the window manager supports this feature.)
29913
29914 This variable has the same structure as `mode-line-format', except that
29915 the %c and %l constructs are ignored. It is used only on frames for
29916 which no explicit name has been set \(see `modify-frame-parameters'). */);
29917
29918 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29919 doc: /* Template for displaying the title bar of an iconified frame.
29920 \(Assuming the window manager supports this feature.)
29921 This variable has the same structure as `mode-line-format' (which see),
29922 and is used only on frames for which no explicit name has been set
29923 \(see `modify-frame-parameters'). */);
29924 Vicon_title_format
29925 = Vframe_title_format
29926 = listn (CONSTYPE_PURE, 3,
29927 intern_c_string ("multiple-frames"),
29928 build_pure_c_string ("%b"),
29929 listn (CONSTYPE_PURE, 4,
29930 empty_unibyte_string,
29931 intern_c_string ("invocation-name"),
29932 build_pure_c_string ("@"),
29933 intern_c_string ("system-name")));
29934
29935 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29936 doc: /* Maximum number of lines to keep in the message log buffer.
29937 If nil, disable message logging. If t, log messages but don't truncate
29938 the buffer when it becomes large. */);
29939 Vmessage_log_max = make_number (1000);
29940
29941 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29942 doc: /* Functions called before redisplay, if window sizes have changed.
29943 The value should be a list of functions that take one argument.
29944 Just before redisplay, for each frame, if any of its windows have changed
29945 size since the last redisplay, or have been split or deleted,
29946 all the functions in the list are called, with the frame as argument. */);
29947 Vwindow_size_change_functions = Qnil;
29948
29949 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29950 doc: /* List of functions to call before redisplaying a window with scrolling.
29951 Each function is called with two arguments, the window and its new
29952 display-start position. Note that these functions are also called by
29953 `set-window-buffer'. Also note that the value of `window-end' is not
29954 valid when these functions are called.
29955
29956 Warning: Do not use this feature to alter the way the window
29957 is scrolled. It is not designed for that, and such use probably won't
29958 work. */);
29959 Vwindow_scroll_functions = Qnil;
29960
29961 DEFVAR_LISP ("window-text-change-functions",
29962 Vwindow_text_change_functions,
29963 doc: /* Functions to call in redisplay when text in the window might change. */);
29964 Vwindow_text_change_functions = Qnil;
29965
29966 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29967 doc: /* Functions called when redisplay of a window reaches the end trigger.
29968 Each function is called with two arguments, the window and the end trigger value.
29969 See `set-window-redisplay-end-trigger'. */);
29970 Vredisplay_end_trigger_functions = Qnil;
29971
29972 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29973 doc: /* Non-nil means autoselect window with mouse pointer.
29974 If nil, do not autoselect windows.
29975 A positive number means delay autoselection by that many seconds: a
29976 window is autoselected only after the mouse has remained in that
29977 window for the duration of the delay.
29978 A negative number has a similar effect, but causes windows to be
29979 autoselected only after the mouse has stopped moving. \(Because of
29980 the way Emacs compares mouse events, you will occasionally wait twice
29981 that time before the window gets selected.\)
29982 Any other value means to autoselect window instantaneously when the
29983 mouse pointer enters it.
29984
29985 Autoselection selects the minibuffer only if it is active, and never
29986 unselects the minibuffer if it is active.
29987
29988 When customizing this variable make sure that the actual value of
29989 `focus-follows-mouse' matches the behavior of your window manager. */);
29990 Vmouse_autoselect_window = Qnil;
29991
29992 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29993 doc: /* Non-nil means automatically resize tool-bars.
29994 This dynamically changes the tool-bar's height to the minimum height
29995 that is needed to make all tool-bar items visible.
29996 If value is `grow-only', the tool-bar's height is only increased
29997 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29998 Vauto_resize_tool_bars = Qt;
29999
30000 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30001 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30002 auto_raise_tool_bar_buttons_p = 1;
30003
30004 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30005 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30006 make_cursor_line_fully_visible_p = 1;
30007
30008 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30009 doc: /* Border below tool-bar in pixels.
30010 If an integer, use it as the height of the border.
30011 If it is one of `internal-border-width' or `border-width', use the
30012 value of the corresponding frame parameter.
30013 Otherwise, no border is added below the tool-bar. */);
30014 Vtool_bar_border = Qinternal_border_width;
30015
30016 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30017 doc: /* Margin around tool-bar buttons in pixels.
30018 If an integer, use that for both horizontal and vertical margins.
30019 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30020 HORZ specifying the horizontal margin, and VERT specifying the
30021 vertical margin. */);
30022 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30023
30024 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30025 doc: /* Relief thickness of tool-bar buttons. */);
30026 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30027
30028 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30029 doc: /* Tool bar style to use.
30030 It can be one of
30031 image - show images only
30032 text - show text only
30033 both - show both, text below image
30034 both-horiz - show text to the right of the image
30035 text-image-horiz - show text to the left of the image
30036 any other - use system default or image if no system default.
30037
30038 This variable only affects the GTK+ toolkit version of Emacs. */);
30039 Vtool_bar_style = Qnil;
30040
30041 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30042 doc: /* Maximum number of characters a label can have to be shown.
30043 The tool bar style must also show labels for this to have any effect, see
30044 `tool-bar-style'. */);
30045 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30046
30047 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30048 doc: /* List of functions to call to fontify regions of text.
30049 Each function is called with one argument POS. Functions must
30050 fontify a region starting at POS in the current buffer, and give
30051 fontified regions the property `fontified'. */);
30052 Vfontification_functions = Qnil;
30053 Fmake_variable_buffer_local (Qfontification_functions);
30054
30055 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30056 unibyte_display_via_language_environment,
30057 doc: /* Non-nil means display unibyte text according to language environment.
30058 Specifically, this means that raw bytes in the range 160-255 decimal
30059 are displayed by converting them to the equivalent multibyte characters
30060 according to the current language environment. As a result, they are
30061 displayed according to the current fontset.
30062
30063 Note that this variable affects only how these bytes are displayed,
30064 but does not change the fact they are interpreted as raw bytes. */);
30065 unibyte_display_via_language_environment = 0;
30066
30067 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30068 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30069 If a float, it specifies a fraction of the mini-window frame's height.
30070 If an integer, it specifies a number of lines. */);
30071 Vmax_mini_window_height = make_float (0.25);
30072
30073 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30074 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30075 A value of nil means don't automatically resize mini-windows.
30076 A value of t means resize them to fit the text displayed in them.
30077 A value of `grow-only', the default, means let mini-windows grow only;
30078 they return to their normal size when the minibuffer is closed, or the
30079 echo area becomes empty. */);
30080 Vresize_mini_windows = Qgrow_only;
30081
30082 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30083 doc: /* Alist specifying how to blink the cursor off.
30084 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30085 `cursor-type' frame-parameter or variable equals ON-STATE,
30086 comparing using `equal', Emacs uses OFF-STATE to specify
30087 how to blink it off. ON-STATE and OFF-STATE are values for
30088 the `cursor-type' frame parameter.
30089
30090 If a frame's ON-STATE has no entry in this list,
30091 the frame's other specifications determine how to blink the cursor off. */);
30092 Vblink_cursor_alist = Qnil;
30093
30094 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30095 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30096 If non-nil, windows are automatically scrolled horizontally to make
30097 point visible. */);
30098 automatic_hscrolling_p = 1;
30099 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30100
30101 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30102 doc: /* How many columns away from the window edge point is allowed to get
30103 before automatic hscrolling will horizontally scroll the window. */);
30104 hscroll_margin = 5;
30105
30106 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30107 doc: /* How many columns to scroll the window when point gets too close to the edge.
30108 When point is less than `hscroll-margin' columns from the window
30109 edge, automatic hscrolling will scroll the window by the amount of columns
30110 determined by this variable. If its value is a positive integer, scroll that
30111 many columns. If it's a positive floating-point number, it specifies the
30112 fraction of the window's width to scroll. If it's nil or zero, point will be
30113 centered horizontally after the scroll. Any other value, including negative
30114 numbers, are treated as if the value were zero.
30115
30116 Automatic hscrolling always moves point outside the scroll margin, so if
30117 point was more than scroll step columns inside the margin, the window will
30118 scroll more than the value given by the scroll step.
30119
30120 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30121 and `scroll-right' overrides this variable's effect. */);
30122 Vhscroll_step = make_number (0);
30123
30124 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30125 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30126 Bind this around calls to `message' to let it take effect. */);
30127 message_truncate_lines = 0;
30128
30129 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30130 doc: /* Normal hook run to update the menu bar definitions.
30131 Redisplay runs this hook before it redisplays the menu bar.
30132 This is used to update submenus such as Buffers,
30133 whose contents depend on various data. */);
30134 Vmenu_bar_update_hook = Qnil;
30135
30136 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30137 doc: /* Frame for which we are updating a menu.
30138 The enable predicate for a menu binding should check this variable. */);
30139 Vmenu_updating_frame = Qnil;
30140
30141 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30142 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30143 inhibit_menubar_update = 0;
30144
30145 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30146 doc: /* Prefix prepended to all continuation lines at display time.
30147 The value may be a string, an image, or a stretch-glyph; it is
30148 interpreted in the same way as the value of a `display' text property.
30149
30150 This variable is overridden by any `wrap-prefix' text or overlay
30151 property.
30152
30153 To add a prefix to non-continuation lines, use `line-prefix'. */);
30154 Vwrap_prefix = Qnil;
30155 DEFSYM (Qwrap_prefix, "wrap-prefix");
30156 Fmake_variable_buffer_local (Qwrap_prefix);
30157
30158 DEFVAR_LISP ("line-prefix", Vline_prefix,
30159 doc: /* Prefix prepended to all non-continuation lines at display time.
30160 The value may be a string, an image, or a stretch-glyph; it is
30161 interpreted in the same way as the value of a `display' text property.
30162
30163 This variable is overridden by any `line-prefix' text or overlay
30164 property.
30165
30166 To add a prefix to continuation lines, use `wrap-prefix'. */);
30167 Vline_prefix = Qnil;
30168 DEFSYM (Qline_prefix, "line-prefix");
30169 Fmake_variable_buffer_local (Qline_prefix);
30170
30171 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30172 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30173 inhibit_eval_during_redisplay = 0;
30174
30175 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30176 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30177 inhibit_free_realized_faces = 0;
30178
30179 #ifdef GLYPH_DEBUG
30180 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30181 doc: /* Inhibit try_window_id display optimization. */);
30182 inhibit_try_window_id = 0;
30183
30184 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30185 doc: /* Inhibit try_window_reusing display optimization. */);
30186 inhibit_try_window_reusing = 0;
30187
30188 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30189 doc: /* Inhibit try_cursor_movement display optimization. */);
30190 inhibit_try_cursor_movement = 0;
30191 #endif /* GLYPH_DEBUG */
30192
30193 DEFVAR_INT ("overline-margin", overline_margin,
30194 doc: /* Space between overline and text, in pixels.
30195 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30196 margin to the character height. */);
30197 overline_margin = 2;
30198
30199 DEFVAR_INT ("underline-minimum-offset",
30200 underline_minimum_offset,
30201 doc: /* Minimum distance between baseline and underline.
30202 This can improve legibility of underlined text at small font sizes,
30203 particularly when using variable `x-use-underline-position-properties'
30204 with fonts that specify an UNDERLINE_POSITION relatively close to the
30205 baseline. The default value is 1. */);
30206 underline_minimum_offset = 1;
30207
30208 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30209 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30210 This feature only works when on a window system that can change
30211 cursor shapes. */);
30212 display_hourglass_p = 1;
30213
30214 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30215 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30216 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30217
30218 #ifdef HAVE_WINDOW_SYSTEM
30219 hourglass_atimer = NULL;
30220 hourglass_shown_p = 0;
30221 #endif /* HAVE_WINDOW_SYSTEM */
30222
30223 DEFSYM (Qglyphless_char, "glyphless-char");
30224 DEFSYM (Qhex_code, "hex-code");
30225 DEFSYM (Qempty_box, "empty-box");
30226 DEFSYM (Qthin_space, "thin-space");
30227 DEFSYM (Qzero_width, "zero-width");
30228
30229 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30230 doc: /* Function run just before redisplay.
30231 It is called with one argument, which is the set of windows that are to
30232 be redisplayed. This set can be nil (meaning, only the selected window),
30233 or t (meaning all windows). */);
30234 Vpre_redisplay_function = intern ("ignore");
30235
30236 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30237 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30238
30239 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30240 doc: /* Char-table defining glyphless characters.
30241 Each element, if non-nil, should be one of the following:
30242 an ASCII acronym string: display this string in a box
30243 `hex-code': display the hexadecimal code of a character in a box
30244 `empty-box': display as an empty box
30245 `thin-space': display as 1-pixel width space
30246 `zero-width': don't display
30247 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30248 display method for graphical terminals and text terminals respectively.
30249 GRAPHICAL and TEXT should each have one of the values listed above.
30250
30251 The char-table has one extra slot to control the display of a character for
30252 which no font is found. This slot only takes effect on graphical terminals.
30253 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30254 `thin-space'. The default is `empty-box'.
30255
30256 If a character has a non-nil entry in an active display table, the
30257 display table takes effect; in this case, Emacs does not consult
30258 `glyphless-char-display' at all. */);
30259 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30260 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30261 Qempty_box);
30262
30263 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30264 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30265 Vdebug_on_message = Qnil;
30266
30267 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30268 doc: /* */);
30269 Vredisplay__all_windows_cause
30270 = Fmake_vector (make_number (100), make_number (0));
30271
30272 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30273 doc: /* */);
30274 Vredisplay__mode_lines_cause
30275 = Fmake_vector (make_number (100), make_number (0));
30276 }
30277
30278
30279 /* Initialize this module when Emacs starts. */
30280
30281 void
30282 init_xdisp (void)
30283 {
30284 CHARPOS (this_line_start_pos) = 0;
30285
30286 if (!noninteractive)
30287 {
30288 struct window *m = XWINDOW (minibuf_window);
30289 Lisp_Object frame = m->frame;
30290 struct frame *f = XFRAME (frame);
30291 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30292 struct window *r = XWINDOW (root);
30293 int i;
30294
30295 echo_area_window = minibuf_window;
30296
30297 r->top_line = FRAME_TOP_MARGIN (f);
30298 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30299 r->total_cols = FRAME_COLS (f);
30300 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30301 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30302 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30303
30304 m->top_line = FRAME_LINES (f) - 1;
30305 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30306 m->total_cols = FRAME_COLS (f);
30307 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30308 m->total_lines = 1;
30309 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30310
30311 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30312 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30313 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30314
30315 /* The default ellipsis glyphs `...'. */
30316 for (i = 0; i < 3; ++i)
30317 default_invis_vector[i] = make_number ('.');
30318 }
30319
30320 {
30321 /* Allocate the buffer for frame titles.
30322 Also used for `format-mode-line'. */
30323 int size = 100;
30324 mode_line_noprop_buf = xmalloc (size);
30325 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30326 mode_line_noprop_ptr = mode_line_noprop_buf;
30327 mode_line_target = MODE_LINE_DISPLAY;
30328 }
30329
30330 help_echo_showing_p = 0;
30331 }
30332
30333 #ifdef HAVE_WINDOW_SYSTEM
30334
30335 /* Platform-independent portion of hourglass implementation. */
30336
30337 /* Cancel a currently active hourglass timer, and start a new one. */
30338 void
30339 start_hourglass (void)
30340 {
30341 struct timespec delay;
30342
30343 cancel_hourglass ();
30344
30345 if (INTEGERP (Vhourglass_delay)
30346 && XINT (Vhourglass_delay) > 0)
30347 delay = make_timespec (min (XINT (Vhourglass_delay),
30348 TYPE_MAXIMUM (time_t)),
30349 0);
30350 else if (FLOATP (Vhourglass_delay)
30351 && XFLOAT_DATA (Vhourglass_delay) > 0)
30352 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30353 else
30354 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30355
30356 #ifdef HAVE_NTGUI
30357 {
30358 extern void w32_note_current_window (void);
30359 w32_note_current_window ();
30360 }
30361 #endif /* HAVE_NTGUI */
30362
30363 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30364 show_hourglass, NULL);
30365 }
30366
30367
30368 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30369 shown. */
30370 void
30371 cancel_hourglass (void)
30372 {
30373 if (hourglass_atimer)
30374 {
30375 cancel_atimer (hourglass_atimer);
30376 hourglass_atimer = NULL;
30377 }
30378
30379 if (hourglass_shown_p)
30380 hide_hourglass ();
30381 }
30382
30383 #endif /* HAVE_WINDOW_SYSTEM */