merge trunk
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2014 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
305
306 #ifndef FRAME_X_OUTPUT
307 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
308 #endif
309
310 #define INFINITY 10000000
311
312 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
313 Lisp_Object Qwindow_scroll_functions;
314 static Lisp_Object Qwindow_text_change_functions;
315 static Lisp_Object Qredisplay_end_trigger_functions;
316 Lisp_Object Qinhibit_point_motion_hooks;
317 static Lisp_Object QCeval, QCpropertize;
318 Lisp_Object QCfile, QCdata;
319 static Lisp_Object Qfontified;
320 static Lisp_Object Qgrow_only;
321 static Lisp_Object Qinhibit_eval_during_redisplay;
322 static Lisp_Object Qbuffer_position, Qposition, Qobject;
323 static Lisp_Object Qright_to_left, Qleft_to_right;
324
325 /* Cursor shapes. */
326 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
327
328 /* Pointer shapes. */
329 static Lisp_Object Qarrow, Qhand;
330 Lisp_Object Qtext;
331
332 /* Holds the list (error). */
333 static Lisp_Object list_of_error;
334
335 static Lisp_Object Qfontification_functions;
336
337 static Lisp_Object Qwrap_prefix;
338 static Lisp_Object Qline_prefix;
339 static Lisp_Object Qredisplay_internal;
340
341 /* Non-nil means don't actually do any redisplay. */
342
343 Lisp_Object Qinhibit_redisplay;
344
345 /* Names of text properties relevant for redisplay. */
346
347 Lisp_Object Qdisplay;
348
349 Lisp_Object Qspace, QCalign_to;
350 static Lisp_Object QCrelative_width, QCrelative_height;
351 Lisp_Object Qleft_margin, Qright_margin;
352 static Lisp_Object Qspace_width, Qraise;
353 static Lisp_Object Qslice;
354 Lisp_Object Qcenter;
355 static Lisp_Object Qmargin, Qpointer;
356 static Lisp_Object Qline_height;
357
358 #ifdef HAVE_WINDOW_SYSTEM
359
360 /* Test if overflow newline into fringe. Called with iterator IT
361 at or past right window margin, and with IT->current_x set. */
362
363 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
364 (!NILP (Voverflow_newline_into_fringe) \
365 && FRAME_WINDOW_P ((IT)->f) \
366 && ((IT)->bidi_it.paragraph_dir == R2L \
367 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
368 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
369 && (IT)->current_x == (IT)->last_visible_x)
370
371 #else /* !HAVE_WINDOW_SYSTEM */
372 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
373 #endif /* HAVE_WINDOW_SYSTEM */
374
375 /* Test if the display element loaded in IT, or the underlying buffer
376 or string character, is a space or a TAB character. This is used
377 to determine where word wrapping can occur. */
378
379 #define IT_DISPLAYING_WHITESPACE(it) \
380 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
381 || ((STRINGP (it->string) \
382 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
383 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
384 || (it->s \
385 && (it->s[IT_BYTEPOS (*it)] == ' ' \
386 || it->s[IT_BYTEPOS (*it)] == '\t')) \
387 || (IT_BYTEPOS (*it) < ZV_BYTE \
388 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
389 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
390
391 /* Name of the face used to highlight trailing whitespace. */
392
393 static Lisp_Object Qtrailing_whitespace;
394
395 /* Name and number of the face used to highlight escape glyphs. */
396
397 static Lisp_Object Qescape_glyph;
398
399 /* Name and number of the face used to highlight non-breaking spaces. */
400
401 static Lisp_Object Qnobreak_space;
402
403 /* The symbol `image' which is the car of the lists used to represent
404 images in Lisp. Also a tool bar style. */
405
406 Lisp_Object Qimage;
407
408 /* The image map types. */
409 Lisp_Object QCmap;
410 static Lisp_Object QCpointer;
411 static Lisp_Object Qrect, Qcircle, Qpoly;
412
413 /* Tool bar styles */
414 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
415
416 /* Non-zero means print newline to stdout before next mini-buffer
417 message. */
418
419 bool noninteractive_need_newline;
420
421 /* Non-zero means print newline to message log before next message. */
422
423 static bool message_log_need_newline;
424
425 /* Three markers that message_dolog uses.
426 It could allocate them itself, but that causes trouble
427 in handling memory-full errors. */
428 static Lisp_Object message_dolog_marker1;
429 static Lisp_Object message_dolog_marker2;
430 static Lisp_Object message_dolog_marker3;
431 \f
432 /* The buffer position of the first character appearing entirely or
433 partially on the line of the selected window which contains the
434 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
435 redisplay optimization in redisplay_internal. */
436
437 static struct text_pos this_line_start_pos;
438
439 /* Number of characters past the end of the line above, including the
440 terminating newline. */
441
442 static struct text_pos this_line_end_pos;
443
444 /* The vertical positions and the height of this line. */
445
446 static int this_line_vpos;
447 static int this_line_y;
448 static int this_line_pixel_height;
449
450 /* X position at which this display line starts. Usually zero;
451 negative if first character is partially visible. */
452
453 static int this_line_start_x;
454
455 /* The smallest character position seen by move_it_* functions as they
456 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
457 hscrolled lines, see display_line. */
458
459 static struct text_pos this_line_min_pos;
460
461 /* Buffer that this_line_.* variables are referring to. */
462
463 static struct buffer *this_line_buffer;
464
465
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
470
471 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
475
476 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477
478 Lisp_Object Qmenu_bar_update_hook;
479
480 /* Nonzero if an overlay arrow has been displayed in this window. */
481
482 static bool overlay_arrow_seen;
483
484 /* Vector containing glyphs for an ellipsis `...'. */
485
486 static Lisp_Object default_invis_vector[3];
487
488 /* This is the window where the echo area message was displayed. It
489 is always a mini-buffer window, but it may not be the same window
490 currently active as a mini-buffer. */
491
492 Lisp_Object echo_area_window;
493
494 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
495 pushes the current message and the value of
496 message_enable_multibyte on the stack, the function restore_message
497 pops the stack and displays MESSAGE again. */
498
499 static Lisp_Object Vmessage_stack;
500
501 /* Nonzero means multibyte characters were enabled when the echo area
502 message was specified. */
503
504 static bool message_enable_multibyte;
505
506 /* Nonzero if we should redraw the mode lines on the next redisplay.
507 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
508 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
509 (the number used is then only used to track down the cause for this
510 full-redisplay). */
511
512 int update_mode_lines;
513
514 /* Nonzero if window sizes or contents other than selected-window have changed
515 since last redisplay that finished.
516 If it has value REDISPLAY_SOME, then only redisplay the windows where
517 the `redisplay' bit has been set. Otherwise, redisplay all windows
518 (the number used is then only used to track down the cause for this
519 full-redisplay). */
520
521 int windows_or_buffers_changed;
522
523 /* Nonzero after display_mode_line if %l was used and it displayed a
524 line number. */
525
526 static bool line_number_displayed;
527
528 /* The name of the *Messages* buffer, a string. */
529
530 static Lisp_Object Vmessages_buffer_name;
531
532 /* Current, index 0, and last displayed echo area message. Either
533 buffers from echo_buffers, or nil to indicate no message. */
534
535 Lisp_Object echo_area_buffer[2];
536
537 /* The buffers referenced from echo_area_buffer. */
538
539 static Lisp_Object echo_buffer[2];
540
541 /* A vector saved used in with_area_buffer to reduce consing. */
542
543 static Lisp_Object Vwith_echo_area_save_vector;
544
545 /* Non-zero means display_echo_area should display the last echo area
546 message again. Set by redisplay_preserve_echo_area. */
547
548 static bool display_last_displayed_message_p;
549
550 /* Nonzero if echo area is being used by print; zero if being used by
551 message. */
552
553 static bool message_buf_print;
554
555 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
556
557 static Lisp_Object Qinhibit_menubar_update;
558 static Lisp_Object Qmessage_truncate_lines;
559
560 /* Set to 1 in clear_message to make redisplay_internal aware
561 of an emptied echo area. */
562
563 static bool message_cleared_p;
564
565 /* A scratch glyph row with contents used for generating truncation
566 glyphs. Also used in direct_output_for_insert. */
567
568 #define MAX_SCRATCH_GLYPHS 100
569 static struct glyph_row scratch_glyph_row;
570 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
571
572 /* Ascent and height of the last line processed by move_it_to. */
573
574 static int last_height;
575
576 /* Non-zero if there's a help-echo in the echo area. */
577
578 bool help_echo_showing_p;
579
580 /* The maximum distance to look ahead for text properties. Values
581 that are too small let us call compute_char_face and similar
582 functions too often which is expensive. Values that are too large
583 let us call compute_char_face and alike too often because we
584 might not be interested in text properties that far away. */
585
586 #define TEXT_PROP_DISTANCE_LIMIT 100
587
588 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
589 iterator state and later restore it. This is needed because the
590 bidi iterator on bidi.c keeps a stacked cache of its states, which
591 is really a singleton. When we use scratch iterator objects to
592 move around the buffer, we can cause the bidi cache to be pushed or
593 popped, and therefore we need to restore the cache state when we
594 return to the original iterator. */
595 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
596 do { \
597 if (CACHE) \
598 bidi_unshelve_cache (CACHE, 1); \
599 ITCOPY = ITORIG; \
600 CACHE = bidi_shelve_cache (); \
601 } while (0)
602
603 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
604 do { \
605 if (pITORIG != pITCOPY) \
606 *(pITORIG) = *(pITCOPY); \
607 bidi_unshelve_cache (CACHE, 0); \
608 CACHE = NULL; \
609 } while (0)
610
611 /* Functions to mark elements as needing redisplay. */
612 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
613
614 void
615 redisplay_other_windows (void)
616 {
617 if (!windows_or_buffers_changed)
618 windows_or_buffers_changed = REDISPLAY_SOME;
619 }
620
621 void
622 wset_redisplay (struct window *w)
623 {
624 /* Beware: selected_window can be nil during early stages. */
625 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
626 redisplay_other_windows ();
627 w->redisplay = true;
628 }
629
630 void
631 fset_redisplay (struct frame *f)
632 {
633 redisplay_other_windows ();
634 f->redisplay = true;
635 }
636
637 void
638 bset_redisplay (struct buffer *b)
639 {
640 int count = buffer_window_count (b);
641 if (count > 0)
642 {
643 /* ... it's visible in other window than selected, */
644 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
645 redisplay_other_windows ();
646 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
647 so that if we later set windows_or_buffers_changed, this buffer will
648 not be omitted. */
649 b->text->redisplay = true;
650 }
651 }
652
653 void
654 bset_update_mode_line (struct buffer *b)
655 {
656 if (!update_mode_lines)
657 update_mode_lines = REDISPLAY_SOME;
658 b->text->redisplay = true;
659 }
660
661 #ifdef GLYPH_DEBUG
662
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG defined. */
665
666 bool trace_redisplay_p;
667
668 #endif /* GLYPH_DEBUG */
669
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
673
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
678
679 static Lisp_Object Qauto_hscroll_mode;
680
681 /* Buffer being redisplayed -- for redisplay_window_error. */
682
683 static struct buffer *displayed_buffer;
684
685 /* Value returned from text property handlers (see below). */
686
687 enum prop_handled
688 {
689 HANDLED_NORMALLY,
690 HANDLED_RECOMPUTE_PROPS,
691 HANDLED_OVERLAY_STRING_CONSUMED,
692 HANDLED_RETURN
693 };
694
695 /* A description of text properties that redisplay is interested
696 in. */
697
698 struct props
699 {
700 /* The name of the property. */
701 Lisp_Object *name;
702
703 /* A unique index for the property. */
704 enum prop_idx idx;
705
706 /* A handler function called to set up iterator IT from the property
707 at IT's current position. Value is used to steer handle_stop. */
708 enum prop_handled (*handler) (struct it *it);
709 };
710
711 static enum prop_handled handle_face_prop (struct it *);
712 static enum prop_handled handle_invisible_prop (struct it *);
713 static enum prop_handled handle_display_prop (struct it *);
714 static enum prop_handled handle_composition_prop (struct it *);
715 static enum prop_handled handle_overlay_change (struct it *);
716 static enum prop_handled handle_fontified_prop (struct it *);
717
718 /* Properties handled by iterators. */
719
720 static struct props it_props[] =
721 {
722 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
723 /* Handle `face' before `display' because some sub-properties of
724 `display' need to know the face. */
725 {&Qface, FACE_PROP_IDX, handle_face_prop},
726 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
727 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
728 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
729 {NULL, 0, NULL}
730 };
731
732 /* Value is the position described by X. If X is a marker, value is
733 the marker_position of X. Otherwise, value is X. */
734
735 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
736
737 /* Enumeration returned by some move_it_.* functions internally. */
738
739 enum move_it_result
740 {
741 /* Not used. Undefined value. */
742 MOVE_UNDEFINED,
743
744 /* Move ended at the requested buffer position or ZV. */
745 MOVE_POS_MATCH_OR_ZV,
746
747 /* Move ended at the requested X pixel position. */
748 MOVE_X_REACHED,
749
750 /* Move within a line ended at the end of a line that must be
751 continued. */
752 MOVE_LINE_CONTINUED,
753
754 /* Move within a line ended at the end of a line that would
755 be displayed truncated. */
756 MOVE_LINE_TRUNCATED,
757
758 /* Move within a line ended at a line end. */
759 MOVE_NEWLINE_OR_CR
760 };
761
762 /* This counter is used to clear the face cache every once in a while
763 in redisplay_internal. It is incremented for each redisplay.
764 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
765 cleared. */
766
767 #define CLEAR_FACE_CACHE_COUNT 500
768 static int clear_face_cache_count;
769
770 /* Similarly for the image cache. */
771
772 #ifdef HAVE_WINDOW_SYSTEM
773 #define CLEAR_IMAGE_CACHE_COUNT 101
774 static int clear_image_cache_count;
775
776 /* Null glyph slice */
777 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
778 #endif
779
780 /* True while redisplay_internal is in progress. */
781
782 bool redisplaying_p;
783
784 static Lisp_Object Qinhibit_free_realized_faces;
785 static Lisp_Object Qmode_line_default_help_echo;
786
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
789
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 ptrdiff_t help_echo_pos;
794
795 /* Temporary variable for XTread_socket. */
796
797 Lisp_Object previous_help_echo_string;
798
799 /* Platform-independent portion of hourglass implementation. */
800
801 #ifdef HAVE_WINDOW_SYSTEM
802
803 /* Non-zero means an hourglass cursor is currently shown. */
804 bool hourglass_shown_p;
805
806 /* If non-null, an asynchronous timer that, when it expires, displays
807 an hourglass cursor on all frames. */
808 struct atimer *hourglass_atimer;
809
810 #endif /* HAVE_WINDOW_SYSTEM */
811
812 /* Name of the face used to display glyphless characters. */
813 static Lisp_Object Qglyphless_char;
814
815 /* Symbol for the purpose of Vglyphless_char_display. */
816 static Lisp_Object Qglyphless_char_display;
817
818 /* Method symbols for Vglyphless_char_display. */
819 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
820
821 /* Default number of seconds to wait before displaying an hourglass
822 cursor. */
823 #define DEFAULT_HOURGLASS_DELAY 1
824
825 #ifdef HAVE_WINDOW_SYSTEM
826
827 /* Default pixel width of `thin-space' display method. */
828 #define THIN_SPACE_WIDTH 1
829
830 #endif /* HAVE_WINDOW_SYSTEM */
831
832 /* Function prototypes. */
833
834 static void setup_for_ellipsis (struct it *, int);
835 static void set_iterator_to_next (struct it *, int);
836 static void mark_window_display_accurate_1 (struct window *, int);
837 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
838 static int display_prop_string_p (Lisp_Object, Lisp_Object);
839 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
840 static int cursor_row_p (struct glyph_row *);
841 static int redisplay_mode_lines (Lisp_Object, bool);
842 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
843
844 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
845
846 static void handle_line_prefix (struct it *);
847
848 static void pint2str (char *, int, ptrdiff_t);
849 static void pint2hrstr (char *, int, ptrdiff_t);
850 static struct text_pos run_window_scroll_functions (Lisp_Object,
851 struct text_pos);
852 static int text_outside_line_unchanged_p (struct window *,
853 ptrdiff_t, ptrdiff_t);
854 static void store_mode_line_noprop_char (char);
855 static int store_mode_line_noprop (const char *, int, int);
856 static void handle_stop (struct it *);
857 static void handle_stop_backwards (struct it *, ptrdiff_t);
858 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
859 static void ensure_echo_area_buffers (void);
860 static void unwind_with_echo_area_buffer (Lisp_Object);
861 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
862 static int with_echo_area_buffer (struct window *, int,
863 int (*) (ptrdiff_t, Lisp_Object),
864 ptrdiff_t, Lisp_Object);
865 static void clear_garbaged_frames (void);
866 static int current_message_1 (ptrdiff_t, Lisp_Object);
867 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
868 static void set_message (Lisp_Object);
869 static int set_message_1 (ptrdiff_t, Lisp_Object);
870 static int display_echo_area (struct window *);
871 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
872 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
873 static void unwind_redisplay (void);
874 static int string_char_and_length (const unsigned char *, int *);
875 static struct text_pos display_prop_end (struct it *, Lisp_Object,
876 struct text_pos);
877 static int compute_window_start_on_continuation_line (struct window *);
878 static void insert_left_trunc_glyphs (struct it *);
879 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
880 Lisp_Object);
881 static void extend_face_to_end_of_line (struct it *);
882 static int append_space_for_newline (struct it *, int);
883 static int cursor_row_fully_visible_p (struct window *, int, int);
884 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
885 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
886 static int trailing_whitespace_p (ptrdiff_t);
887 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
888 static void push_it (struct it *, struct text_pos *);
889 static void iterate_out_of_display_property (struct it *);
890 static void pop_it (struct it *);
891 static void sync_frame_with_window_matrix_rows (struct window *);
892 static void redisplay_internal (void);
893 static int echo_area_display (int);
894 static void redisplay_windows (Lisp_Object);
895 static void redisplay_window (Lisp_Object, bool);
896 static Lisp_Object redisplay_window_error (Lisp_Object);
897 static Lisp_Object redisplay_window_0 (Lisp_Object);
898 static Lisp_Object redisplay_window_1 (Lisp_Object);
899 static int set_cursor_from_row (struct window *, struct glyph_row *,
900 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
901 int, int);
902 static int update_menu_bar (struct frame *, int, int);
903 static int try_window_reusing_current_matrix (struct window *);
904 static int try_window_id (struct window *);
905 static int display_line (struct it *);
906 static int display_mode_lines (struct window *);
907 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
908 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
909 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
910 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
911 static void display_menu_bar (struct window *);
912 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
913 ptrdiff_t *);
914 static int display_string (const char *, Lisp_Object, Lisp_Object,
915 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
916 static void compute_line_metrics (struct it *);
917 static void run_redisplay_end_trigger_hook (struct it *);
918 static int get_overlay_strings (struct it *, ptrdiff_t);
919 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
920 static void next_overlay_string (struct it *);
921 static void reseat (struct it *, struct text_pos, int);
922 static void reseat_1 (struct it *, struct text_pos, int);
923 static void back_to_previous_visible_line_start (struct it *);
924 static void reseat_at_next_visible_line_start (struct it *, int);
925 static int next_element_from_ellipsis (struct it *);
926 static int next_element_from_display_vector (struct it *);
927 static int next_element_from_string (struct it *);
928 static int next_element_from_c_string (struct it *);
929 static int next_element_from_buffer (struct it *);
930 static int next_element_from_composition (struct it *);
931 static int next_element_from_image (struct it *);
932 static int next_element_from_stretch (struct it *);
933 static void load_overlay_strings (struct it *, ptrdiff_t);
934 static int init_from_display_pos (struct it *, struct window *,
935 struct display_pos *);
936 static void reseat_to_string (struct it *, const char *,
937 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
938 static int get_next_display_element (struct it *);
939 static enum move_it_result
940 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
941 enum move_operation_enum);
942 static void get_visually_first_element (struct it *);
943 static void init_to_row_start (struct it *, struct window *,
944 struct glyph_row *);
945 static int init_to_row_end (struct it *, struct window *,
946 struct glyph_row *);
947 static void back_to_previous_line_start (struct it *);
948 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
949 static struct text_pos string_pos_nchars_ahead (struct text_pos,
950 Lisp_Object, ptrdiff_t);
951 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
952 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
953 static ptrdiff_t number_of_chars (const char *, bool);
954 static void compute_stop_pos (struct it *);
955 static void compute_string_pos (struct text_pos *, struct text_pos,
956 Lisp_Object);
957 static int face_before_or_after_it_pos (struct it *, int);
958 static ptrdiff_t next_overlay_change (ptrdiff_t);
959 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
960 Lisp_Object, struct text_pos *, ptrdiff_t, int);
961 static int handle_single_display_spec (struct it *, Lisp_Object,
962 Lisp_Object, Lisp_Object,
963 struct text_pos *, ptrdiff_t, int, int);
964 static int underlying_face_id (struct it *);
965 static int in_ellipses_for_invisible_text_p (struct display_pos *,
966 struct window *);
967
968 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
969 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
970
971 #ifdef HAVE_WINDOW_SYSTEM
972
973 static void x_consider_frame_title (Lisp_Object);
974 static void update_tool_bar (struct frame *, int);
975 static int redisplay_tool_bar (struct frame *);
976 static void x_draw_bottom_divider (struct window *w);
977 static void notice_overwritten_cursor (struct window *,
978 enum glyph_row_area,
979 int, int, int, int);
980 static void append_stretch_glyph (struct it *, Lisp_Object,
981 int, int, int);
982
983
984 #endif /* HAVE_WINDOW_SYSTEM */
985
986 static void produce_special_glyphs (struct it *, enum display_element_type);
987 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
988 static bool coords_in_mouse_face_p (struct window *, int, int);
989
990
991 \f
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
995
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
999
1000 This is the height of W minus the height of a mode line, if any. */
1001
1002 int
1003 window_text_bottom_y (struct window *w)
1004 {
1005 int height = WINDOW_PIXEL_HEIGHT (w);
1006
1007 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1008
1009 if (WINDOW_WANTS_MODELINE_P (w))
1010 height -= CURRENT_MODE_LINE_HEIGHT (w);
1011
1012 return height;
1013 }
1014
1015 /* Return the pixel width of display area AREA of window W.
1016 ANY_AREA means return the total width of W, not including
1017 fringes to the left and right of the window. */
1018
1019 int
1020 window_box_width (struct window *w, enum glyph_row_area area)
1021 {
1022 int 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),
2809 PTRDIFF_MAX);
2810
2811 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2812
2813 /* Are lines in the display truncated? */
2814 if (base_face_id != DEFAULT_FACE_ID
2815 || it->w->hscroll
2816 || (! WINDOW_FULL_WIDTH_P (it->w)
2817 && ((!NILP (Vtruncate_partial_width_windows)
2818 && !INTEGERP (Vtruncate_partial_width_windows))
2819 || (INTEGERP (Vtruncate_partial_width_windows)
2820 /* PXW: Shall we do something about this? */
2821 && (WINDOW_TOTAL_COLS (it->w)
2822 < XINT (Vtruncate_partial_width_windows))))))
2823 it->line_wrap = TRUNCATE;
2824 else if (NILP (BVAR (current_buffer, truncate_lines)))
2825 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2826 ? WINDOW_WRAP : WORD_WRAP;
2827 else
2828 it->line_wrap = TRUNCATE;
2829
2830 /* Get dimensions of truncation and continuation glyphs. These are
2831 displayed as fringe bitmaps under X, but we need them for such
2832 frames when the fringes are turned off. But leave the dimensions
2833 zero for tooltip frames, as these glyphs look ugly there and also
2834 sabotage calculations of tooltip dimensions in x-show-tip. */
2835 #ifdef HAVE_WINDOW_SYSTEM
2836 if (!(FRAME_WINDOW_P (it->f)
2837 && FRAMEP (tip_frame)
2838 && it->f == XFRAME (tip_frame)))
2839 #endif
2840 {
2841 if (it->line_wrap == TRUNCATE)
2842 {
2843 /* We will need the truncation glyph. */
2844 eassert (it->glyph_row == NULL);
2845 produce_special_glyphs (it, IT_TRUNCATION);
2846 it->truncation_pixel_width = it->pixel_width;
2847 }
2848 else
2849 {
2850 /* We will need the continuation glyph. */
2851 eassert (it->glyph_row == NULL);
2852 produce_special_glyphs (it, IT_CONTINUATION);
2853 it->continuation_pixel_width = it->pixel_width;
2854 }
2855 }
2856
2857 /* Reset these values to zero because the produce_special_glyphs
2858 above has changed them. */
2859 it->pixel_width = it->ascent = it->descent = 0;
2860 it->phys_ascent = it->phys_descent = 0;
2861
2862 /* Set this after getting the dimensions of truncation and
2863 continuation glyphs, so that we don't produce glyphs when calling
2864 produce_special_glyphs, above. */
2865 it->glyph_row = row;
2866 it->area = TEXT_AREA;
2867
2868 /* Forget any previous info about this row being reversed. */
2869 if (it->glyph_row)
2870 it->glyph_row->reversed_p = 0;
2871
2872 /* Get the dimensions of the display area. The display area
2873 consists of the visible window area plus a horizontally scrolled
2874 part to the left of the window. All x-values are relative to the
2875 start of this total display area. */
2876 if (base_face_id != DEFAULT_FACE_ID)
2877 {
2878 /* Mode lines, menu bar in terminal frames. */
2879 it->first_visible_x = 0;
2880 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2881 }
2882 else
2883 {
2884 it->first_visible_x
2885 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2886 it->last_visible_x = (it->first_visible_x
2887 + window_box_width (w, TEXT_AREA));
2888
2889 /* If we truncate lines, leave room for the truncation glyph(s) at
2890 the right margin. Otherwise, leave room for the continuation
2891 glyph(s). Done only if the window has no fringes. Since we
2892 don't know at this point whether there will be any R2L lines in
2893 the window, we reserve space for truncation/continuation glyphs
2894 even if only one of the fringes is absent. */
2895 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2896 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2897 {
2898 if (it->line_wrap == TRUNCATE)
2899 it->last_visible_x -= it->truncation_pixel_width;
2900 else
2901 it->last_visible_x -= it->continuation_pixel_width;
2902 }
2903
2904 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2905 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2906 }
2907
2908 /* Leave room for a border glyph. */
2909 if (!FRAME_WINDOW_P (it->f)
2910 && !WINDOW_RIGHTMOST_P (it->w))
2911 it->last_visible_x -= 1;
2912
2913 it->last_visible_y = window_text_bottom_y (w);
2914
2915 /* For mode lines and alike, arrange for the first glyph having a
2916 left box line if the face specifies a box. */
2917 if (base_face_id != DEFAULT_FACE_ID)
2918 {
2919 struct face *face;
2920
2921 it->face_id = remapped_base_face_id;
2922
2923 /* If we have a boxed mode line, make the first character appear
2924 with a left box line. */
2925 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2926 if (face->box != FACE_NO_BOX)
2927 it->start_of_box_run_p = true;
2928 }
2929
2930 /* If a buffer position was specified, set the iterator there,
2931 getting overlays and face properties from that position. */
2932 if (charpos >= BUF_BEG (current_buffer))
2933 {
2934 it->end_charpos = ZV;
2935 eassert (charpos == BYTE_TO_CHAR (bytepos));
2936 IT_CHARPOS (*it) = charpos;
2937 IT_BYTEPOS (*it) = bytepos;
2938
2939 /* We will rely on `reseat' to set this up properly, via
2940 handle_face_prop. */
2941 it->face_id = it->base_face_id;
2942
2943 it->start = it->current;
2944 /* Do we need to reorder bidirectional text? Not if this is a
2945 unibyte buffer: by definition, none of the single-byte
2946 characters are strong R2L, so no reordering is needed. And
2947 bidi.c doesn't support unibyte buffers anyway. Also, don't
2948 reorder while we are loading loadup.el, since the tables of
2949 character properties needed for reordering are not yet
2950 available. */
2951 it->bidi_p =
2952 NILP (Vpurify_flag)
2953 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2954 && it->multibyte_p;
2955
2956 /* If we are to reorder bidirectional text, init the bidi
2957 iterator. */
2958 if (it->bidi_p)
2959 {
2960 /* Note the paragraph direction that this buffer wants to
2961 use. */
2962 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2963 Qleft_to_right))
2964 it->paragraph_embedding = L2R;
2965 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2966 Qright_to_left))
2967 it->paragraph_embedding = R2L;
2968 else
2969 it->paragraph_embedding = NEUTRAL_DIR;
2970 bidi_unshelve_cache (NULL, 0);
2971 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2972 &it->bidi_it);
2973 }
2974
2975 /* Compute faces etc. */
2976 reseat (it, it->current.pos, 1);
2977 }
2978
2979 CHECK_IT (it);
2980 }
2981
2982
2983 /* Initialize IT for the display of window W with window start POS. */
2984
2985 void
2986 start_display (struct it *it, struct window *w, struct text_pos pos)
2987 {
2988 struct glyph_row *row;
2989 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2990
2991 row = w->desired_matrix->rows + first_vpos;
2992 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2993 it->first_vpos = first_vpos;
2994
2995 /* Don't reseat to previous visible line start if current start
2996 position is in a string or image. */
2997 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2998 {
2999 int start_at_line_beg_p;
3000 int first_y = it->current_y;
3001
3002 /* If window start is not at a line start, skip forward to POS to
3003 get the correct continuation lines width. */
3004 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3005 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3006 if (!start_at_line_beg_p)
3007 {
3008 int new_x;
3009
3010 reseat_at_previous_visible_line_start (it);
3011 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3012
3013 new_x = it->current_x + it->pixel_width;
3014
3015 /* If lines are continued, this line may end in the middle
3016 of a multi-glyph character (e.g. a control character
3017 displayed as \003, or in the middle of an overlay
3018 string). In this case move_it_to above will not have
3019 taken us to the start of the continuation line but to the
3020 end of the continued line. */
3021 if (it->current_x > 0
3022 && it->line_wrap != TRUNCATE /* Lines are continued. */
3023 && (/* And glyph doesn't fit on the line. */
3024 new_x > it->last_visible_x
3025 /* Or it fits exactly and we're on a window
3026 system frame. */
3027 || (new_x == it->last_visible_x
3028 && FRAME_WINDOW_P (it->f)
3029 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3030 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3031 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3032 {
3033 if ((it->current.dpvec_index >= 0
3034 || it->current.overlay_string_index >= 0)
3035 /* If we are on a newline from a display vector or
3036 overlay string, then we are already at the end of
3037 a screen line; no need to go to the next line in
3038 that case, as this line is not really continued.
3039 (If we do go to the next line, C-e will not DTRT.) */
3040 && it->c != '\n')
3041 {
3042 set_iterator_to_next (it, 1);
3043 move_it_in_display_line_to (it, -1, -1, 0);
3044 }
3045
3046 it->continuation_lines_width += it->current_x;
3047 }
3048 /* If the character at POS is displayed via a display
3049 vector, move_it_to above stops at the final glyph of
3050 IT->dpvec. To make the caller redisplay that character
3051 again (a.k.a. start at POS), we need to reset the
3052 dpvec_index to the beginning of IT->dpvec. */
3053 else if (it->current.dpvec_index >= 0)
3054 it->current.dpvec_index = 0;
3055
3056 /* We're starting a new display line, not affected by the
3057 height of the continued line, so clear the appropriate
3058 fields in the iterator structure. */
3059 it->max_ascent = it->max_descent = 0;
3060 it->max_phys_ascent = it->max_phys_descent = 0;
3061
3062 it->current_y = first_y;
3063 it->vpos = 0;
3064 it->current_x = it->hpos = 0;
3065 }
3066 }
3067 }
3068
3069
3070 /* Return 1 if POS is a position in ellipses displayed for invisible
3071 text. W is the window we display, for text property lookup. */
3072
3073 static int
3074 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3075 {
3076 Lisp_Object prop, window;
3077 int ellipses_p = 0;
3078 ptrdiff_t charpos = CHARPOS (pos->pos);
3079
3080 /* If POS specifies a position in a display vector, this might
3081 be for an ellipsis displayed for invisible text. We won't
3082 get the iterator set up for delivering that ellipsis unless
3083 we make sure that it gets aware of the invisible text. */
3084 if (pos->dpvec_index >= 0
3085 && pos->overlay_string_index < 0
3086 && CHARPOS (pos->string_pos) < 0
3087 && charpos > BEGV
3088 && (XSETWINDOW (window, w),
3089 prop = Fget_char_property (make_number (charpos),
3090 Qinvisible, window),
3091 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3092 {
3093 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3094 window);
3095 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3096 }
3097
3098 return ellipses_p;
3099 }
3100
3101
3102 /* Initialize IT for stepping through current_buffer in window W,
3103 starting at position POS that includes overlay string and display
3104 vector/ control character translation position information. Value
3105 is zero if there are overlay strings with newlines at POS. */
3106
3107 static int
3108 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3109 {
3110 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3111 int i, overlay_strings_with_newlines = 0;
3112
3113 /* If POS specifies a position in a display vector, this might
3114 be for an ellipsis displayed for invisible text. We won't
3115 get the iterator set up for delivering that ellipsis unless
3116 we make sure that it gets aware of the invisible text. */
3117 if (in_ellipses_for_invisible_text_p (pos, w))
3118 {
3119 --charpos;
3120 bytepos = 0;
3121 }
3122
3123 /* Keep in mind: the call to reseat in init_iterator skips invisible
3124 text, so we might end up at a position different from POS. This
3125 is only a problem when POS is a row start after a newline and an
3126 overlay starts there with an after-string, and the overlay has an
3127 invisible property. Since we don't skip invisible text in
3128 display_line and elsewhere immediately after consuming the
3129 newline before the row start, such a POS will not be in a string,
3130 but the call to init_iterator below will move us to the
3131 after-string. */
3132 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3133
3134 /* This only scans the current chunk -- it should scan all chunks.
3135 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3136 to 16 in 22.1 to make this a lesser problem. */
3137 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3138 {
3139 const char *s = SSDATA (it->overlay_strings[i]);
3140 const char *e = s + SBYTES (it->overlay_strings[i]);
3141
3142 while (s < e && *s != '\n')
3143 ++s;
3144
3145 if (s < e)
3146 {
3147 overlay_strings_with_newlines = 1;
3148 break;
3149 }
3150 }
3151
3152 /* If position is within an overlay string, set up IT to the right
3153 overlay string. */
3154 if (pos->overlay_string_index >= 0)
3155 {
3156 int relative_index;
3157
3158 /* If the first overlay string happens to have a `display'
3159 property for an image, the iterator will be set up for that
3160 image, and we have to undo that setup first before we can
3161 correct the overlay string index. */
3162 if (it->method == GET_FROM_IMAGE)
3163 pop_it (it);
3164
3165 /* We already have the first chunk of overlay strings in
3166 IT->overlay_strings. Load more until the one for
3167 pos->overlay_string_index is in IT->overlay_strings. */
3168 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3169 {
3170 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3171 it->current.overlay_string_index = 0;
3172 while (n--)
3173 {
3174 load_overlay_strings (it, 0);
3175 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3176 }
3177 }
3178
3179 it->current.overlay_string_index = pos->overlay_string_index;
3180 relative_index = (it->current.overlay_string_index
3181 % OVERLAY_STRING_CHUNK_SIZE);
3182 it->string = it->overlay_strings[relative_index];
3183 eassert (STRINGP (it->string));
3184 it->current.string_pos = pos->string_pos;
3185 it->method = GET_FROM_STRING;
3186 it->end_charpos = SCHARS (it->string);
3187 /* Set up the bidi iterator for this overlay string. */
3188 if (it->bidi_p)
3189 {
3190 it->bidi_it.string.lstring = it->string;
3191 it->bidi_it.string.s = NULL;
3192 it->bidi_it.string.schars = SCHARS (it->string);
3193 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3194 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3195 it->bidi_it.string.unibyte = !it->multibyte_p;
3196 it->bidi_it.w = it->w;
3197 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3198 FRAME_WINDOW_P (it->f), &it->bidi_it);
3199
3200 /* Synchronize the state of the bidi iterator with
3201 pos->string_pos. For any string position other than
3202 zero, this will be done automagically when we resume
3203 iteration over the string and get_visually_first_element
3204 is called. But if string_pos is zero, and the string is
3205 to be reordered for display, we need to resync manually,
3206 since it could be that the iteration state recorded in
3207 pos ended at string_pos of 0 moving backwards in string. */
3208 if (CHARPOS (pos->string_pos) == 0)
3209 {
3210 get_visually_first_element (it);
3211 if (IT_STRING_CHARPOS (*it) != 0)
3212 do {
3213 /* Paranoia. */
3214 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3215 bidi_move_to_visually_next (&it->bidi_it);
3216 } while (it->bidi_it.charpos != 0);
3217 }
3218 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3219 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3220 }
3221 }
3222
3223 if (CHARPOS (pos->string_pos) >= 0)
3224 {
3225 /* Recorded position is not in an overlay string, but in another
3226 string. This can only be a string from a `display' property.
3227 IT should already be filled with that string. */
3228 it->current.string_pos = pos->string_pos;
3229 eassert (STRINGP (it->string));
3230 if (it->bidi_p)
3231 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3232 FRAME_WINDOW_P (it->f), &it->bidi_it);
3233 }
3234
3235 /* Restore position in display vector translations, control
3236 character translations or ellipses. */
3237 if (pos->dpvec_index >= 0)
3238 {
3239 if (it->dpvec == NULL)
3240 get_next_display_element (it);
3241 eassert (it->dpvec && it->current.dpvec_index == 0);
3242 it->current.dpvec_index = pos->dpvec_index;
3243 }
3244
3245 CHECK_IT (it);
3246 return !overlay_strings_with_newlines;
3247 }
3248
3249
3250 /* Initialize IT for stepping through current_buffer in window W
3251 starting at ROW->start. */
3252
3253 static void
3254 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3255 {
3256 init_from_display_pos (it, w, &row->start);
3257 it->start = row->start;
3258 it->continuation_lines_width = row->continuation_lines_width;
3259 CHECK_IT (it);
3260 }
3261
3262
3263 /* Initialize IT for stepping through current_buffer in window W
3264 starting in the line following ROW, i.e. starting at ROW->end.
3265 Value is zero if there are overlay strings with newlines at ROW's
3266 end position. */
3267
3268 static int
3269 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3270 {
3271 int success = 0;
3272
3273 if (init_from_display_pos (it, w, &row->end))
3274 {
3275 if (row->continued_p)
3276 it->continuation_lines_width
3277 = row->continuation_lines_width + row->pixel_width;
3278 CHECK_IT (it);
3279 success = 1;
3280 }
3281
3282 return success;
3283 }
3284
3285
3286
3287 \f
3288 /***********************************************************************
3289 Text properties
3290 ***********************************************************************/
3291
3292 /* Called when IT reaches IT->stop_charpos. Handle text property and
3293 overlay changes. Set IT->stop_charpos to the next position where
3294 to stop. */
3295
3296 static void
3297 handle_stop (struct it *it)
3298 {
3299 enum prop_handled handled;
3300 int handle_overlay_change_p;
3301 struct props *p;
3302
3303 it->dpvec = NULL;
3304 it->current.dpvec_index = -1;
3305 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3306 it->ignore_overlay_strings_at_pos_p = 0;
3307 it->ellipsis_p = 0;
3308
3309 /* Use face of preceding text for ellipsis (if invisible) */
3310 if (it->selective_display_ellipsis_p)
3311 it->saved_face_id = it->face_id;
3312
3313 do
3314 {
3315 handled = HANDLED_NORMALLY;
3316
3317 /* Call text property handlers. */
3318 for (p = it_props; p->handler; ++p)
3319 {
3320 handled = p->handler (it);
3321
3322 if (handled == HANDLED_RECOMPUTE_PROPS)
3323 break;
3324 else if (handled == HANDLED_RETURN)
3325 {
3326 /* We still want to show before and after strings from
3327 overlays even if the actual buffer text is replaced. */
3328 if (!handle_overlay_change_p
3329 || it->sp > 1
3330 /* Don't call get_overlay_strings_1 if we already
3331 have overlay strings loaded, because doing so
3332 will load them again and push the iterator state
3333 onto the stack one more time, which is not
3334 expected by the rest of the code that processes
3335 overlay strings. */
3336 || (it->current.overlay_string_index < 0
3337 ? !get_overlay_strings_1 (it, 0, 0)
3338 : 0))
3339 {
3340 if (it->ellipsis_p)
3341 setup_for_ellipsis (it, 0);
3342 /* When handling a display spec, we might load an
3343 empty string. In that case, discard it here. We
3344 used to discard it in handle_single_display_spec,
3345 but that causes get_overlay_strings_1, above, to
3346 ignore overlay strings that we must check. */
3347 if (STRINGP (it->string) && !SCHARS (it->string))
3348 pop_it (it);
3349 return;
3350 }
3351 else if (STRINGP (it->string) && !SCHARS (it->string))
3352 pop_it (it);
3353 else
3354 {
3355 it->ignore_overlay_strings_at_pos_p = true;
3356 it->string_from_display_prop_p = 0;
3357 it->from_disp_prop_p = 0;
3358 handle_overlay_change_p = 0;
3359 }
3360 handled = HANDLED_RECOMPUTE_PROPS;
3361 break;
3362 }
3363 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3364 handle_overlay_change_p = 0;
3365 }
3366
3367 if (handled != HANDLED_RECOMPUTE_PROPS)
3368 {
3369 /* Don't check for overlay strings below when set to deliver
3370 characters from a display vector. */
3371 if (it->method == GET_FROM_DISPLAY_VECTOR)
3372 handle_overlay_change_p = 0;
3373
3374 /* Handle overlay changes.
3375 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3376 if it finds overlays. */
3377 if (handle_overlay_change_p)
3378 handled = handle_overlay_change (it);
3379 }
3380
3381 if (it->ellipsis_p)
3382 {
3383 setup_for_ellipsis (it, 0);
3384 break;
3385 }
3386 }
3387 while (handled == HANDLED_RECOMPUTE_PROPS);
3388
3389 /* Determine where to stop next. */
3390 if (handled == HANDLED_NORMALLY)
3391 compute_stop_pos (it);
3392 }
3393
3394
3395 /* Compute IT->stop_charpos from text property and overlay change
3396 information for IT's current position. */
3397
3398 static void
3399 compute_stop_pos (struct it *it)
3400 {
3401 register INTERVAL iv, next_iv;
3402 Lisp_Object object, limit, position;
3403 ptrdiff_t charpos, bytepos;
3404
3405 if (STRINGP (it->string))
3406 {
3407 /* Strings are usually short, so don't limit the search for
3408 properties. */
3409 it->stop_charpos = it->end_charpos;
3410 object = it->string;
3411 limit = Qnil;
3412 charpos = IT_STRING_CHARPOS (*it);
3413 bytepos = IT_STRING_BYTEPOS (*it);
3414 }
3415 else
3416 {
3417 ptrdiff_t pos;
3418
3419 /* If end_charpos is out of range for some reason, such as a
3420 misbehaving display function, rationalize it (Bug#5984). */
3421 if (it->end_charpos > ZV)
3422 it->end_charpos = ZV;
3423 it->stop_charpos = it->end_charpos;
3424
3425 /* If next overlay change is in front of the current stop pos
3426 (which is IT->end_charpos), stop there. Note: value of
3427 next_overlay_change is point-max if no overlay change
3428 follows. */
3429 charpos = IT_CHARPOS (*it);
3430 bytepos = IT_BYTEPOS (*it);
3431 pos = next_overlay_change (charpos);
3432 if (pos < it->stop_charpos)
3433 it->stop_charpos = pos;
3434
3435 /* Set up variables for computing the stop position from text
3436 property changes. */
3437 XSETBUFFER (object, current_buffer);
3438 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3439 }
3440
3441 /* Get the interval containing IT's position. Value is a null
3442 interval if there isn't such an interval. */
3443 position = make_number (charpos);
3444 iv = validate_interval_range (object, &position, &position, 0);
3445 if (iv)
3446 {
3447 Lisp_Object values_here[LAST_PROP_IDX];
3448 struct props *p;
3449
3450 /* Get properties here. */
3451 for (p = it_props; p->handler; ++p)
3452 values_here[p->idx] = textget (iv->plist, *p->name);
3453
3454 /* Look for an interval following iv that has different
3455 properties. */
3456 for (next_iv = next_interval (iv);
3457 (next_iv
3458 && (NILP (limit)
3459 || XFASTINT (limit) > next_iv->position));
3460 next_iv = next_interval (next_iv))
3461 {
3462 for (p = it_props; p->handler; ++p)
3463 {
3464 Lisp_Object new_value;
3465
3466 new_value = textget (next_iv->plist, *p->name);
3467 if (!EQ (values_here[p->idx], new_value))
3468 break;
3469 }
3470
3471 if (p->handler)
3472 break;
3473 }
3474
3475 if (next_iv)
3476 {
3477 if (INTEGERP (limit)
3478 && next_iv->position >= XFASTINT (limit))
3479 /* No text property change up to limit. */
3480 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3481 else
3482 /* Text properties change in next_iv. */
3483 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3484 }
3485 }
3486
3487 if (it->cmp_it.id < 0)
3488 {
3489 ptrdiff_t stoppos = it->end_charpos;
3490
3491 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3492 stoppos = -1;
3493 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3494 stoppos, it->string);
3495 }
3496
3497 eassert (STRINGP (it->string)
3498 || (it->stop_charpos >= BEGV
3499 && it->stop_charpos >= IT_CHARPOS (*it)));
3500 }
3501
3502
3503 /* Return the position of the next overlay change after POS in
3504 current_buffer. Value is point-max if no overlay change
3505 follows. This is like `next-overlay-change' but doesn't use
3506 xmalloc. */
3507
3508 static ptrdiff_t
3509 next_overlay_change (ptrdiff_t pos)
3510 {
3511 ptrdiff_t i, noverlays;
3512 ptrdiff_t endpos;
3513 Lisp_Object *overlays;
3514
3515 /* Get all overlays at the given position. */
3516 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3517
3518 /* If any of these overlays ends before endpos,
3519 use its ending point instead. */
3520 for (i = 0; i < noverlays; ++i)
3521 {
3522 Lisp_Object oend;
3523 ptrdiff_t oendpos;
3524
3525 oend = OVERLAY_END (overlays[i]);
3526 oendpos = OVERLAY_POSITION (oend);
3527 endpos = min (endpos, oendpos);
3528 }
3529
3530 return endpos;
3531 }
3532
3533 /* How many characters forward to search for a display property or
3534 display string. Searching too far forward makes the bidi display
3535 sluggish, especially in small windows. */
3536 #define MAX_DISP_SCAN 250
3537
3538 /* Return the character position of a display string at or after
3539 position specified by POSITION. If no display string exists at or
3540 after POSITION, return ZV. A display string is either an overlay
3541 with `display' property whose value is a string, or a `display'
3542 text property whose value is a string. STRING is data about the
3543 string to iterate; if STRING->lstring is nil, we are iterating a
3544 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3545 on a GUI frame. DISP_PROP is set to zero if we searched
3546 MAX_DISP_SCAN characters forward without finding any display
3547 strings, non-zero otherwise. It is set to 2 if the display string
3548 uses any kind of `(space ...)' spec that will produce a stretch of
3549 white space in the text area. */
3550 ptrdiff_t
3551 compute_display_string_pos (struct text_pos *position,
3552 struct bidi_string_data *string,
3553 struct window *w,
3554 int frame_window_p, int *disp_prop)
3555 {
3556 /* OBJECT = nil means current buffer. */
3557 Lisp_Object object, object1;
3558 Lisp_Object pos, spec, limpos;
3559 int string_p = (string && (STRINGP (string->lstring) || string->s));
3560 ptrdiff_t eob = string_p ? string->schars : ZV;
3561 ptrdiff_t begb = string_p ? 0 : BEGV;
3562 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3563 ptrdiff_t lim =
3564 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3565 struct text_pos tpos;
3566 int rv = 0;
3567
3568 if (string && STRINGP (string->lstring))
3569 object1 = object = string->lstring;
3570 else if (w && !string_p)
3571 {
3572 XSETWINDOW (object, w);
3573 object1 = Qnil;
3574 }
3575 else
3576 object1 = object = Qnil;
3577
3578 *disp_prop = 1;
3579
3580 if (charpos >= eob
3581 /* We don't support display properties whose values are strings
3582 that have display string properties. */
3583 || string->from_disp_str
3584 /* C strings cannot have display properties. */
3585 || (string->s && !STRINGP (object)))
3586 {
3587 *disp_prop = 0;
3588 return eob;
3589 }
3590
3591 /* If the character at CHARPOS is where the display string begins,
3592 return CHARPOS. */
3593 pos = make_number (charpos);
3594 if (STRINGP (object))
3595 bufpos = string->bufpos;
3596 else
3597 bufpos = charpos;
3598 tpos = *position;
3599 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3600 && (charpos <= begb
3601 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3602 object),
3603 spec))
3604 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3605 frame_window_p)))
3606 {
3607 if (rv == 2)
3608 *disp_prop = 2;
3609 return charpos;
3610 }
3611
3612 /* Look forward for the first character with a `display' property
3613 that will replace the underlying text when displayed. */
3614 limpos = make_number (lim);
3615 do {
3616 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3617 CHARPOS (tpos) = XFASTINT (pos);
3618 if (CHARPOS (tpos) >= lim)
3619 {
3620 *disp_prop = 0;
3621 break;
3622 }
3623 if (STRINGP (object))
3624 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3625 else
3626 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3627 spec = Fget_char_property (pos, Qdisplay, object);
3628 if (!STRINGP (object))
3629 bufpos = CHARPOS (tpos);
3630 } while (NILP (spec)
3631 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3632 bufpos, frame_window_p)));
3633 if (rv == 2)
3634 *disp_prop = 2;
3635
3636 return CHARPOS (tpos);
3637 }
3638
3639 /* Return the character position of the end of the display string that
3640 started at CHARPOS. If there's no display string at CHARPOS,
3641 return -1. A display string is either an overlay with `display'
3642 property whose value is a string or a `display' text property whose
3643 value is a string. */
3644 ptrdiff_t
3645 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3646 {
3647 /* OBJECT = nil means current buffer. */
3648 Lisp_Object object =
3649 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3650 Lisp_Object pos = make_number (charpos);
3651 ptrdiff_t eob =
3652 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3653
3654 if (charpos >= eob || (string->s && !STRINGP (object)))
3655 return eob;
3656
3657 /* It could happen that the display property or overlay was removed
3658 since we found it in compute_display_string_pos above. One way
3659 this can happen is if JIT font-lock was called (through
3660 handle_fontified_prop), and jit-lock-functions remove text
3661 properties or overlays from the portion of buffer that includes
3662 CHARPOS. Muse mode is known to do that, for example. In this
3663 case, we return -1 to the caller, to signal that no display
3664 string is actually present at CHARPOS. See bidi_fetch_char for
3665 how this is handled.
3666
3667 An alternative would be to never look for display properties past
3668 it->stop_charpos. But neither compute_display_string_pos nor
3669 bidi_fetch_char that calls it know or care where the next
3670 stop_charpos is. */
3671 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3672 return -1;
3673
3674 /* Look forward for the first character where the `display' property
3675 changes. */
3676 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3677
3678 return XFASTINT (pos);
3679 }
3680
3681
3682 \f
3683 /***********************************************************************
3684 Fontification
3685 ***********************************************************************/
3686
3687 /* Handle changes in the `fontified' property of the current buffer by
3688 calling hook functions from Qfontification_functions to fontify
3689 regions of text. */
3690
3691 static enum prop_handled
3692 handle_fontified_prop (struct it *it)
3693 {
3694 Lisp_Object prop, pos;
3695 enum prop_handled handled = HANDLED_NORMALLY;
3696
3697 if (!NILP (Vmemory_full))
3698 return handled;
3699
3700 /* Get the value of the `fontified' property at IT's current buffer
3701 position. (The `fontified' property doesn't have a special
3702 meaning in strings.) If the value is nil, call functions from
3703 Qfontification_functions. */
3704 if (!STRINGP (it->string)
3705 && it->s == NULL
3706 && !NILP (Vfontification_functions)
3707 && !NILP (Vrun_hooks)
3708 && (pos = make_number (IT_CHARPOS (*it)),
3709 prop = Fget_char_property (pos, Qfontified, Qnil),
3710 /* Ignore the special cased nil value always present at EOB since
3711 no amount of fontifying will be able to change it. */
3712 NILP (prop) && IT_CHARPOS (*it) < Z))
3713 {
3714 ptrdiff_t count = SPECPDL_INDEX ();
3715 Lisp_Object val;
3716 struct buffer *obuf = current_buffer;
3717 ptrdiff_t begv = BEGV, zv = ZV;
3718 bool old_clip_changed = current_buffer->clip_changed;
3719
3720 val = Vfontification_functions;
3721 specbind (Qfontification_functions, Qnil);
3722
3723 eassert (it->end_charpos == ZV);
3724
3725 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3726 safe_call1 (val, pos);
3727 else
3728 {
3729 Lisp_Object fns, fn;
3730 struct gcpro gcpro1, gcpro2;
3731
3732 fns = Qnil;
3733 GCPRO2 (val, fns);
3734
3735 for (; CONSP (val); val = XCDR (val))
3736 {
3737 fn = XCAR (val);
3738
3739 if (EQ (fn, Qt))
3740 {
3741 /* A value of t indicates this hook has a local
3742 binding; it means to run the global binding too.
3743 In a global value, t should not occur. If it
3744 does, we must ignore it to avoid an endless
3745 loop. */
3746 for (fns = Fdefault_value (Qfontification_functions);
3747 CONSP (fns);
3748 fns = XCDR (fns))
3749 {
3750 fn = XCAR (fns);
3751 if (!EQ (fn, Qt))
3752 safe_call1 (fn, pos);
3753 }
3754 }
3755 else
3756 safe_call1 (fn, pos);
3757 }
3758
3759 UNGCPRO;
3760 }
3761
3762 unbind_to (count, Qnil);
3763
3764 /* Fontification functions routinely call `save-restriction'.
3765 Normally, this tags clip_changed, which can confuse redisplay
3766 (see discussion in Bug#6671). Since we don't perform any
3767 special handling of fontification changes in the case where
3768 `save-restriction' isn't called, there's no point doing so in
3769 this case either. So, if the buffer's restrictions are
3770 actually left unchanged, reset clip_changed. */
3771 if (obuf == current_buffer)
3772 {
3773 if (begv == BEGV && zv == ZV)
3774 current_buffer->clip_changed = old_clip_changed;
3775 }
3776 /* There isn't much we can reasonably do to protect against
3777 misbehaving fontification, but here's a fig leaf. */
3778 else if (BUFFER_LIVE_P (obuf))
3779 set_buffer_internal_1 (obuf);
3780
3781 /* The fontification code may have added/removed text.
3782 It could do even a lot worse, but let's at least protect against
3783 the most obvious case where only the text past `pos' gets changed',
3784 as is/was done in grep.el where some escapes sequences are turned
3785 into face properties (bug#7876). */
3786 it->end_charpos = ZV;
3787
3788 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3789 something. This avoids an endless loop if they failed to
3790 fontify the text for which reason ever. */
3791 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3792 handled = HANDLED_RECOMPUTE_PROPS;
3793 }
3794
3795 return handled;
3796 }
3797
3798
3799 \f
3800 /***********************************************************************
3801 Faces
3802 ***********************************************************************/
3803
3804 /* Set up iterator IT from face properties at its current position.
3805 Called from handle_stop. */
3806
3807 static enum prop_handled
3808 handle_face_prop (struct it *it)
3809 {
3810 int new_face_id;
3811 ptrdiff_t next_stop;
3812
3813 if (!STRINGP (it->string))
3814 {
3815 new_face_id
3816 = face_at_buffer_position (it->w,
3817 IT_CHARPOS (*it),
3818 &next_stop,
3819 (IT_CHARPOS (*it)
3820 + TEXT_PROP_DISTANCE_LIMIT),
3821 0, it->base_face_id);
3822
3823 /* Is this a start of a run of characters with box face?
3824 Caveat: this can be called for a freshly initialized
3825 iterator; face_id is -1 in this case. We know that the new
3826 face will not change until limit, i.e. if the new face has a
3827 box, all characters up to limit will have one. But, as
3828 usual, we don't know whether limit is really the end. */
3829 if (new_face_id != it->face_id)
3830 {
3831 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3832 /* If it->face_id is -1, old_face below will be NULL, see
3833 the definition of FACE_FROM_ID. This will happen if this
3834 is the initial call that gets the face. */
3835 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3836
3837 /* If the value of face_id of the iterator is -1, we have to
3838 look in front of IT's position and see whether there is a
3839 face there that's different from new_face_id. */
3840 if (!old_face && IT_CHARPOS (*it) > BEG)
3841 {
3842 int prev_face_id = face_before_it_pos (it);
3843
3844 old_face = FACE_FROM_ID (it->f, prev_face_id);
3845 }
3846
3847 /* If the new face has a box, but the old face does not,
3848 this is the start of a run of characters with box face,
3849 i.e. this character has a shadow on the left side. */
3850 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3851 && (old_face == NULL || !old_face->box));
3852 it->face_box_p = new_face->box != FACE_NO_BOX;
3853 }
3854 }
3855 else
3856 {
3857 int base_face_id;
3858 ptrdiff_t bufpos;
3859 int i;
3860 Lisp_Object from_overlay
3861 = (it->current.overlay_string_index >= 0
3862 ? it->string_overlays[it->current.overlay_string_index
3863 % OVERLAY_STRING_CHUNK_SIZE]
3864 : Qnil);
3865
3866 /* See if we got to this string directly or indirectly from
3867 an overlay property. That includes the before-string or
3868 after-string of an overlay, strings in display properties
3869 provided by an overlay, their text properties, etc.
3870
3871 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3872 if (! NILP (from_overlay))
3873 for (i = it->sp - 1; i >= 0; i--)
3874 {
3875 if (it->stack[i].current.overlay_string_index >= 0)
3876 from_overlay
3877 = it->string_overlays[it->stack[i].current.overlay_string_index
3878 % OVERLAY_STRING_CHUNK_SIZE];
3879 else if (! NILP (it->stack[i].from_overlay))
3880 from_overlay = it->stack[i].from_overlay;
3881
3882 if (!NILP (from_overlay))
3883 break;
3884 }
3885
3886 if (! NILP (from_overlay))
3887 {
3888 bufpos = IT_CHARPOS (*it);
3889 /* For a string from an overlay, the base face depends
3890 only on text properties and ignores overlays. */
3891 base_face_id
3892 = face_for_overlay_string (it->w,
3893 IT_CHARPOS (*it),
3894 &next_stop,
3895 (IT_CHARPOS (*it)
3896 + TEXT_PROP_DISTANCE_LIMIT),
3897 0,
3898 from_overlay);
3899 }
3900 else
3901 {
3902 bufpos = 0;
3903
3904 /* For strings from a `display' property, use the face at
3905 IT's current buffer position as the base face to merge
3906 with, so that overlay strings appear in the same face as
3907 surrounding text, unless they specify their own faces.
3908 For strings from wrap-prefix and line-prefix properties,
3909 use the default face, possibly remapped via
3910 Vface_remapping_alist. */
3911 base_face_id = it->string_from_prefix_prop_p
3912 ? (!NILP (Vface_remapping_alist)
3913 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3914 : DEFAULT_FACE_ID)
3915 : underlying_face_id (it);
3916 }
3917
3918 new_face_id = face_at_string_position (it->w,
3919 it->string,
3920 IT_STRING_CHARPOS (*it),
3921 bufpos,
3922 &next_stop,
3923 base_face_id, 0);
3924
3925 /* Is this a start of a run of characters with box? Caveat:
3926 this can be called for a freshly allocated iterator; face_id
3927 is -1 is this case. We know that the new face will not
3928 change until the next check pos, i.e. if the new face has a
3929 box, all characters up to that position will have a
3930 box. But, as usual, we don't know whether that position
3931 is really the end. */
3932 if (new_face_id != it->face_id)
3933 {
3934 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3935 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3936
3937 /* If new face has a box but old face hasn't, this is the
3938 start of a run of characters with box, i.e. it has a
3939 shadow on the left side. */
3940 it->start_of_box_run_p
3941 = new_face->box && (old_face == NULL || !old_face->box);
3942 it->face_box_p = new_face->box != FACE_NO_BOX;
3943 }
3944 }
3945
3946 it->face_id = new_face_id;
3947 return HANDLED_NORMALLY;
3948 }
3949
3950
3951 /* Return the ID of the face ``underlying'' IT's current position,
3952 which is in a string. If the iterator is associated with a
3953 buffer, return the face at IT's current buffer position.
3954 Otherwise, use the iterator's base_face_id. */
3955
3956 static int
3957 underlying_face_id (struct it *it)
3958 {
3959 int face_id = it->base_face_id, i;
3960
3961 eassert (STRINGP (it->string));
3962
3963 for (i = it->sp - 1; i >= 0; --i)
3964 if (NILP (it->stack[i].string))
3965 face_id = it->stack[i].face_id;
3966
3967 return face_id;
3968 }
3969
3970
3971 /* Compute the face one character before or after the current position
3972 of IT, in the visual order. BEFORE_P non-zero means get the face
3973 in front (to the left in L2R paragraphs, to the right in R2L
3974 paragraphs) of IT's screen position. Value is the ID of the face. */
3975
3976 static int
3977 face_before_or_after_it_pos (struct it *it, int before_p)
3978 {
3979 int face_id, limit;
3980 ptrdiff_t next_check_charpos;
3981 struct it it_copy;
3982 void *it_copy_data = NULL;
3983
3984 eassert (it->s == NULL);
3985
3986 if (STRINGP (it->string))
3987 {
3988 ptrdiff_t bufpos, charpos;
3989 int base_face_id;
3990
3991 /* No face change past the end of the string (for the case
3992 we are padding with spaces). No face change before the
3993 string start. */
3994 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3995 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3996 return it->face_id;
3997
3998 if (!it->bidi_p)
3999 {
4000 /* Set charpos to the position before or after IT's current
4001 position, in the logical order, which in the non-bidi
4002 case is the same as the visual order. */
4003 if (before_p)
4004 charpos = IT_STRING_CHARPOS (*it) - 1;
4005 else if (it->what == IT_COMPOSITION)
4006 /* For composition, we must check the character after the
4007 composition. */
4008 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4009 else
4010 charpos = IT_STRING_CHARPOS (*it) + 1;
4011 }
4012 else
4013 {
4014 if (before_p)
4015 {
4016 /* With bidi iteration, the character before the current
4017 in the visual order cannot be found by simple
4018 iteration, because "reverse" reordering is not
4019 supported. Instead, we need to use the move_it_*
4020 family of functions. */
4021 /* Ignore face changes before the first visible
4022 character on this display line. */
4023 if (it->current_x <= it->first_visible_x)
4024 return it->face_id;
4025 SAVE_IT (it_copy, *it, it_copy_data);
4026 /* Implementation note: Since move_it_in_display_line
4027 works in the iterator geometry, and thinks the first
4028 character is always the leftmost, even in R2L lines,
4029 we don't need to distinguish between the R2L and L2R
4030 cases here. */
4031 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4032 it_copy.current_x - 1, MOVE_TO_X);
4033 charpos = IT_STRING_CHARPOS (it_copy);
4034 RESTORE_IT (it, it, it_copy_data);
4035 }
4036 else
4037 {
4038 /* Set charpos to the string position of the character
4039 that comes after IT's current position in the visual
4040 order. */
4041 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4042
4043 it_copy = *it;
4044 while (n--)
4045 bidi_move_to_visually_next (&it_copy.bidi_it);
4046
4047 charpos = it_copy.bidi_it.charpos;
4048 }
4049 }
4050 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4051
4052 if (it->current.overlay_string_index >= 0)
4053 bufpos = IT_CHARPOS (*it);
4054 else
4055 bufpos = 0;
4056
4057 base_face_id = underlying_face_id (it);
4058
4059 /* Get the face for ASCII, or unibyte. */
4060 face_id = face_at_string_position (it->w,
4061 it->string,
4062 charpos,
4063 bufpos,
4064 &next_check_charpos,
4065 base_face_id, 0);
4066
4067 /* Correct the face for charsets different from ASCII. Do it
4068 for the multibyte case only. The face returned above is
4069 suitable for unibyte text if IT->string is unibyte. */
4070 if (STRING_MULTIBYTE (it->string))
4071 {
4072 struct text_pos pos1 = string_pos (charpos, it->string);
4073 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4074 int c, len;
4075 struct face *face = FACE_FROM_ID (it->f, face_id);
4076
4077 c = string_char_and_length (p, &len);
4078 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4079 }
4080 }
4081 else
4082 {
4083 struct text_pos pos;
4084
4085 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4086 || (IT_CHARPOS (*it) <= BEGV && before_p))
4087 return it->face_id;
4088
4089 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4090 pos = it->current.pos;
4091
4092 if (!it->bidi_p)
4093 {
4094 if (before_p)
4095 DEC_TEXT_POS (pos, it->multibyte_p);
4096 else
4097 {
4098 if (it->what == IT_COMPOSITION)
4099 {
4100 /* For composition, we must check the position after
4101 the composition. */
4102 pos.charpos += it->cmp_it.nchars;
4103 pos.bytepos += it->len;
4104 }
4105 else
4106 INC_TEXT_POS (pos, it->multibyte_p);
4107 }
4108 }
4109 else
4110 {
4111 if (before_p)
4112 {
4113 /* With bidi iteration, the character before the current
4114 in the visual order cannot be found by simple
4115 iteration, because "reverse" reordering is not
4116 supported. Instead, we need to use the move_it_*
4117 family of functions. */
4118 /* Ignore face changes before the first visible
4119 character on this display line. */
4120 if (it->current_x <= it->first_visible_x)
4121 return it->face_id;
4122 SAVE_IT (it_copy, *it, it_copy_data);
4123 /* Implementation note: Since move_it_in_display_line
4124 works in the iterator geometry, and thinks the first
4125 character is always the leftmost, even in R2L lines,
4126 we don't need to distinguish between the R2L and L2R
4127 cases here. */
4128 move_it_in_display_line (&it_copy, ZV,
4129 it_copy.current_x - 1, MOVE_TO_X);
4130 pos = it_copy.current.pos;
4131 RESTORE_IT (it, it, it_copy_data);
4132 }
4133 else
4134 {
4135 /* Set charpos to the buffer position of the character
4136 that comes after IT's current position in the visual
4137 order. */
4138 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4139
4140 it_copy = *it;
4141 while (n--)
4142 bidi_move_to_visually_next (&it_copy.bidi_it);
4143
4144 SET_TEXT_POS (pos,
4145 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4146 }
4147 }
4148 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4149
4150 /* Determine face for CHARSET_ASCII, or unibyte. */
4151 face_id = face_at_buffer_position (it->w,
4152 CHARPOS (pos),
4153 &next_check_charpos,
4154 limit, 0, -1);
4155
4156 /* Correct the face for charsets different from ASCII. Do it
4157 for the multibyte case only. The face returned above is
4158 suitable for unibyte text if current_buffer is unibyte. */
4159 if (it->multibyte_p)
4160 {
4161 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4162 struct face *face = FACE_FROM_ID (it->f, face_id);
4163 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4164 }
4165 }
4166
4167 return face_id;
4168 }
4169
4170
4171 \f
4172 /***********************************************************************
4173 Invisible text
4174 ***********************************************************************/
4175
4176 /* Set up iterator IT from invisible properties at its current
4177 position. Called from handle_stop. */
4178
4179 static enum prop_handled
4180 handle_invisible_prop (struct it *it)
4181 {
4182 enum prop_handled handled = HANDLED_NORMALLY;
4183 int invis_p;
4184 Lisp_Object prop;
4185
4186 if (STRINGP (it->string))
4187 {
4188 Lisp_Object end_charpos, limit, charpos;
4189
4190 /* Get the value of the invisible text property at the
4191 current position. Value will be nil if there is no such
4192 property. */
4193 charpos = make_number (IT_STRING_CHARPOS (*it));
4194 prop = Fget_text_property (charpos, Qinvisible, it->string);
4195 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4196
4197 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4198 {
4199 /* Record whether we have to display an ellipsis for the
4200 invisible text. */
4201 int display_ellipsis_p = (invis_p == 2);
4202 ptrdiff_t len, endpos;
4203
4204 handled = HANDLED_RECOMPUTE_PROPS;
4205
4206 /* Get the position at which the next visible text can be
4207 found in IT->string, if any. */
4208 endpos = len = SCHARS (it->string);
4209 XSETINT (limit, len);
4210 do
4211 {
4212 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4213 it->string, limit);
4214 if (INTEGERP (end_charpos))
4215 {
4216 endpos = XFASTINT (end_charpos);
4217 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4218 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4219 if (invis_p == 2)
4220 display_ellipsis_p = true;
4221 }
4222 }
4223 while (invis_p && endpos < len);
4224
4225 if (display_ellipsis_p)
4226 it->ellipsis_p = true;
4227
4228 if (endpos < len)
4229 {
4230 /* Text at END_CHARPOS is visible. Move IT there. */
4231 struct text_pos old;
4232 ptrdiff_t oldpos;
4233
4234 old = it->current.string_pos;
4235 oldpos = CHARPOS (old);
4236 if (it->bidi_p)
4237 {
4238 if (it->bidi_it.first_elt
4239 && it->bidi_it.charpos < SCHARS (it->string))
4240 bidi_paragraph_init (it->paragraph_embedding,
4241 &it->bidi_it, 1);
4242 /* Bidi-iterate out of the invisible text. */
4243 do
4244 {
4245 bidi_move_to_visually_next (&it->bidi_it);
4246 }
4247 while (oldpos <= it->bidi_it.charpos
4248 && it->bidi_it.charpos < endpos);
4249
4250 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4251 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4252 if (IT_CHARPOS (*it) >= endpos)
4253 it->prev_stop = endpos;
4254 }
4255 else
4256 {
4257 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4258 compute_string_pos (&it->current.string_pos, old, it->string);
4259 }
4260 }
4261 else
4262 {
4263 /* The rest of the string is invisible. If this is an
4264 overlay string, proceed with the next overlay string
4265 or whatever comes and return a character from there. */
4266 if (it->current.overlay_string_index >= 0
4267 && !display_ellipsis_p)
4268 {
4269 next_overlay_string (it);
4270 /* Don't check for overlay strings when we just
4271 finished processing them. */
4272 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4273 }
4274 else
4275 {
4276 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4277 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4278 }
4279 }
4280 }
4281 }
4282 else
4283 {
4284 ptrdiff_t newpos, next_stop, start_charpos, tem;
4285 Lisp_Object pos, overlay;
4286
4287 /* First of all, is there invisible text at this position? */
4288 tem = start_charpos = IT_CHARPOS (*it);
4289 pos = make_number (tem);
4290 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4291 &overlay);
4292 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4293
4294 /* If we are on invisible text, skip over it. */
4295 if (invis_p && start_charpos < it->end_charpos)
4296 {
4297 /* Record whether we have to display an ellipsis for the
4298 invisible text. */
4299 int display_ellipsis_p = invis_p == 2;
4300
4301 handled = HANDLED_RECOMPUTE_PROPS;
4302
4303 /* Loop skipping over invisible text. The loop is left at
4304 ZV or with IT on the first char being visible again. */
4305 do
4306 {
4307 /* Try to skip some invisible text. Return value is the
4308 position reached which can be equal to where we start
4309 if there is nothing invisible there. This skips both
4310 over invisible text properties and overlays with
4311 invisible property. */
4312 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4313
4314 /* If we skipped nothing at all we weren't at invisible
4315 text in the first place. If everything to the end of
4316 the buffer was skipped, end the loop. */
4317 if (newpos == tem || newpos >= ZV)
4318 invis_p = 0;
4319 else
4320 {
4321 /* We skipped some characters but not necessarily
4322 all there are. Check if we ended up on visible
4323 text. Fget_char_property returns the property of
4324 the char before the given position, i.e. if we
4325 get invis_p = 0, this means that the char at
4326 newpos is visible. */
4327 pos = make_number (newpos);
4328 prop = Fget_char_property (pos, Qinvisible, it->window);
4329 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4330 }
4331
4332 /* If we ended up on invisible text, proceed to
4333 skip starting with next_stop. */
4334 if (invis_p)
4335 tem = next_stop;
4336
4337 /* If there are adjacent invisible texts, don't lose the
4338 second one's ellipsis. */
4339 if (invis_p == 2)
4340 display_ellipsis_p = true;
4341 }
4342 while (invis_p);
4343
4344 /* The position newpos is now either ZV or on visible text. */
4345 if (it->bidi_p)
4346 {
4347 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4348 int on_newline
4349 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4350 int after_newline
4351 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4352
4353 /* If the invisible text ends on a newline or on a
4354 character after a newline, we can avoid the costly,
4355 character by character, bidi iteration to NEWPOS, and
4356 instead simply reseat the iterator there. That's
4357 because all bidi reordering information is tossed at
4358 the newline. This is a big win for modes that hide
4359 complete lines, like Outline, Org, etc. */
4360 if (on_newline || after_newline)
4361 {
4362 struct text_pos tpos;
4363 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4364
4365 SET_TEXT_POS (tpos, newpos, bpos);
4366 reseat_1 (it, tpos, 0);
4367 /* If we reseat on a newline/ZV, we need to prep the
4368 bidi iterator for advancing to the next character
4369 after the newline/EOB, keeping the current paragraph
4370 direction (so that PRODUCE_GLYPHS does TRT wrt
4371 prepending/appending glyphs to a glyph row). */
4372 if (on_newline)
4373 {
4374 it->bidi_it.first_elt = 0;
4375 it->bidi_it.paragraph_dir = pdir;
4376 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4377 it->bidi_it.nchars = 1;
4378 it->bidi_it.ch_len = 1;
4379 }
4380 }
4381 else /* Must use the slow method. */
4382 {
4383 /* With bidi iteration, the region of invisible text
4384 could start and/or end in the middle of a
4385 non-base embedding level. Therefore, we need to
4386 skip invisible text using the bidi iterator,
4387 starting at IT's current position, until we find
4388 ourselves outside of the invisible text.
4389 Skipping invisible text _after_ bidi iteration
4390 avoids affecting the visual order of the
4391 displayed text when invisible properties are
4392 added or removed. */
4393 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4394 {
4395 /* If we were `reseat'ed to a new paragraph,
4396 determine the paragraph base direction. We
4397 need to do it now because
4398 next_element_from_buffer may not have a
4399 chance to do it, if we are going to skip any
4400 text at the beginning, which resets the
4401 FIRST_ELT flag. */
4402 bidi_paragraph_init (it->paragraph_embedding,
4403 &it->bidi_it, 1);
4404 }
4405 do
4406 {
4407 bidi_move_to_visually_next (&it->bidi_it);
4408 }
4409 while (it->stop_charpos <= it->bidi_it.charpos
4410 && it->bidi_it.charpos < newpos);
4411 IT_CHARPOS (*it) = it->bidi_it.charpos;
4412 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4413 /* If we overstepped NEWPOS, record its position in
4414 the iterator, so that we skip invisible text if
4415 later the bidi iteration lands us in the
4416 invisible region again. */
4417 if (IT_CHARPOS (*it) >= newpos)
4418 it->prev_stop = newpos;
4419 }
4420 }
4421 else
4422 {
4423 IT_CHARPOS (*it) = newpos;
4424 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4425 }
4426
4427 /* If there are before-strings at the start of invisible
4428 text, and the text is invisible because of a text
4429 property, arrange to show before-strings because 20.x did
4430 it that way. (If the text is invisible because of an
4431 overlay property instead of a text property, this is
4432 already handled in the overlay code.) */
4433 if (NILP (overlay)
4434 && get_overlay_strings (it, it->stop_charpos))
4435 {
4436 handled = HANDLED_RECOMPUTE_PROPS;
4437 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4438 }
4439 else if (display_ellipsis_p)
4440 {
4441 /* Make sure that the glyphs of the ellipsis will get
4442 correct `charpos' values. If we would not update
4443 it->position here, the glyphs would belong to the
4444 last visible character _before_ the invisible
4445 text, which confuses `set_cursor_from_row'.
4446
4447 We use the last invisible position instead of the
4448 first because this way the cursor is always drawn on
4449 the first "." of the ellipsis, whenever PT is inside
4450 the invisible text. Otherwise the cursor would be
4451 placed _after_ the ellipsis when the point is after the
4452 first invisible character. */
4453 if (!STRINGP (it->object))
4454 {
4455 it->position.charpos = newpos - 1;
4456 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4457 }
4458 it->ellipsis_p = true;
4459 /* Let the ellipsis display before
4460 considering any properties of the following char.
4461 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4462 handled = HANDLED_RETURN;
4463 }
4464 }
4465 }
4466
4467 return handled;
4468 }
4469
4470
4471 /* Make iterator IT return `...' next.
4472 Replaces LEN characters from buffer. */
4473
4474 static void
4475 setup_for_ellipsis (struct it *it, int len)
4476 {
4477 /* Use the display table definition for `...'. Invalid glyphs
4478 will be handled by the method returning elements from dpvec. */
4479 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4480 {
4481 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4482 it->dpvec = v->contents;
4483 it->dpend = v->contents + v->header.size;
4484 }
4485 else
4486 {
4487 /* Default `...'. */
4488 it->dpvec = default_invis_vector;
4489 it->dpend = default_invis_vector + 3;
4490 }
4491
4492 it->dpvec_char_len = len;
4493 it->current.dpvec_index = 0;
4494 it->dpvec_face_id = -1;
4495
4496 /* Remember the current face id in case glyphs specify faces.
4497 IT's face is restored in set_iterator_to_next.
4498 saved_face_id was set to preceding char's face in handle_stop. */
4499 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4500 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4501
4502 it->method = GET_FROM_DISPLAY_VECTOR;
4503 it->ellipsis_p = true;
4504 }
4505
4506
4507 \f
4508 /***********************************************************************
4509 'display' property
4510 ***********************************************************************/
4511
4512 /* Set up iterator IT from `display' property at its current position.
4513 Called from handle_stop.
4514 We return HANDLED_RETURN if some part of the display property
4515 overrides the display of the buffer text itself.
4516 Otherwise we return HANDLED_NORMALLY. */
4517
4518 static enum prop_handled
4519 handle_display_prop (struct it *it)
4520 {
4521 Lisp_Object propval, object, overlay;
4522 struct text_pos *position;
4523 ptrdiff_t bufpos;
4524 /* Nonzero if some property replaces the display of the text itself. */
4525 int display_replaced_p = 0;
4526
4527 if (STRINGP (it->string))
4528 {
4529 object = it->string;
4530 position = &it->current.string_pos;
4531 bufpos = CHARPOS (it->current.pos);
4532 }
4533 else
4534 {
4535 XSETWINDOW (object, it->w);
4536 position = &it->current.pos;
4537 bufpos = CHARPOS (*position);
4538 }
4539
4540 /* Reset those iterator values set from display property values. */
4541 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4542 it->space_width = Qnil;
4543 it->font_height = Qnil;
4544 it->voffset = 0;
4545
4546 /* We don't support recursive `display' properties, i.e. string
4547 values that have a string `display' property, that have a string
4548 `display' property etc. */
4549 if (!it->string_from_display_prop_p)
4550 it->area = TEXT_AREA;
4551
4552 propval = get_char_property_and_overlay (make_number (position->charpos),
4553 Qdisplay, object, &overlay);
4554 if (NILP (propval))
4555 return HANDLED_NORMALLY;
4556 /* Now OVERLAY is the overlay that gave us this property, or nil
4557 if it was a text property. */
4558
4559 if (!STRINGP (it->string))
4560 object = it->w->contents;
4561
4562 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4563 position, bufpos,
4564 FRAME_WINDOW_P (it->f));
4565
4566 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4567 }
4568
4569 /* Subroutine of handle_display_prop. Returns non-zero if the display
4570 specification in SPEC is a replacing specification, i.e. it would
4571 replace the text covered by `display' property with something else,
4572 such as an image or a display string. If SPEC includes any kind or
4573 `(space ...) specification, the value is 2; this is used by
4574 compute_display_string_pos, which see.
4575
4576 See handle_single_display_spec for documentation of arguments.
4577 frame_window_p is non-zero if the window being redisplayed is on a
4578 GUI frame; this argument is used only if IT is NULL, see below.
4579
4580 IT can be NULL, if this is called by the bidi reordering code
4581 through compute_display_string_pos, which see. In that case, this
4582 function only examines SPEC, but does not otherwise "handle" it, in
4583 the sense that it doesn't set up members of IT from the display
4584 spec. */
4585 static int
4586 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4587 Lisp_Object overlay, struct text_pos *position,
4588 ptrdiff_t bufpos, int frame_window_p)
4589 {
4590 int replacing_p = 0;
4591 int rv;
4592
4593 if (CONSP (spec)
4594 /* Simple specifications. */
4595 && !EQ (XCAR (spec), Qimage)
4596 && !EQ (XCAR (spec), Qspace)
4597 && !EQ (XCAR (spec), Qwhen)
4598 && !EQ (XCAR (spec), Qslice)
4599 && !EQ (XCAR (spec), Qspace_width)
4600 && !EQ (XCAR (spec), Qheight)
4601 && !EQ (XCAR (spec), Qraise)
4602 /* Marginal area specifications. */
4603 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4604 && !EQ (XCAR (spec), Qleft_fringe)
4605 && !EQ (XCAR (spec), Qright_fringe)
4606 && !NILP (XCAR (spec)))
4607 {
4608 for (; CONSP (spec); spec = XCDR (spec))
4609 {
4610 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4611 overlay, position, bufpos,
4612 replacing_p, frame_window_p)))
4613 {
4614 replacing_p = rv;
4615 /* If some text in a string is replaced, `position' no
4616 longer points to the position of `object'. */
4617 if (!it || STRINGP (object))
4618 break;
4619 }
4620 }
4621 }
4622 else if (VECTORP (spec))
4623 {
4624 ptrdiff_t i;
4625 for (i = 0; i < ASIZE (spec); ++i)
4626 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4627 overlay, position, bufpos,
4628 replacing_p, frame_window_p)))
4629 {
4630 replacing_p = rv;
4631 /* If some text in a string is replaced, `position' no
4632 longer points to the position of `object'. */
4633 if (!it || STRINGP (object))
4634 break;
4635 }
4636 }
4637 else
4638 {
4639 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4640 position, bufpos, 0,
4641 frame_window_p)))
4642 replacing_p = rv;
4643 }
4644
4645 return replacing_p;
4646 }
4647
4648 /* Value is the position of the end of the `display' property starting
4649 at START_POS in OBJECT. */
4650
4651 static struct text_pos
4652 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4653 {
4654 Lisp_Object end;
4655 struct text_pos end_pos;
4656
4657 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4658 Qdisplay, object, Qnil);
4659 CHARPOS (end_pos) = XFASTINT (end);
4660 if (STRINGP (object))
4661 compute_string_pos (&end_pos, start_pos, it->string);
4662 else
4663 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4664
4665 return end_pos;
4666 }
4667
4668
4669 /* Set up IT from a single `display' property specification SPEC. OBJECT
4670 is the object in which the `display' property was found. *POSITION
4671 is the position in OBJECT at which the `display' property was found.
4672 BUFPOS is the buffer position of OBJECT (different from POSITION if
4673 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4674 previously saw a display specification which already replaced text
4675 display with something else, for example an image; we ignore such
4676 properties after the first one has been processed.
4677
4678 OVERLAY is the overlay this `display' property came from,
4679 or nil if it was a text property.
4680
4681 If SPEC is a `space' or `image' specification, and in some other
4682 cases too, set *POSITION to the position where the `display'
4683 property ends.
4684
4685 If IT is NULL, only examine the property specification in SPEC, but
4686 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4687 is intended to be displayed in a window on a GUI frame.
4688
4689 Value is non-zero if something was found which replaces the display
4690 of buffer or string text. */
4691
4692 static int
4693 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4694 Lisp_Object overlay, struct text_pos *position,
4695 ptrdiff_t bufpos, int display_replaced_p,
4696 int frame_window_p)
4697 {
4698 Lisp_Object form;
4699 Lisp_Object location, value;
4700 struct text_pos start_pos = *position;
4701 int valid_p;
4702
4703 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4704 If the result is non-nil, use VALUE instead of SPEC. */
4705 form = Qt;
4706 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4707 {
4708 spec = XCDR (spec);
4709 if (!CONSP (spec))
4710 return 0;
4711 form = XCAR (spec);
4712 spec = XCDR (spec);
4713 }
4714
4715 if (!NILP (form) && !EQ (form, Qt))
4716 {
4717 ptrdiff_t count = SPECPDL_INDEX ();
4718 struct gcpro gcpro1;
4719
4720 /* Bind `object' to the object having the `display' property, a
4721 buffer or string. Bind `position' to the position in the
4722 object where the property was found, and `buffer-position'
4723 to the current position in the buffer. */
4724
4725 if (NILP (object))
4726 XSETBUFFER (object, current_buffer);
4727 specbind (Qobject, object);
4728 specbind (Qposition, make_number (CHARPOS (*position)));
4729 specbind (Qbuffer_position, make_number (bufpos));
4730 GCPRO1 (form);
4731 form = safe_eval (form);
4732 UNGCPRO;
4733 unbind_to (count, Qnil);
4734 }
4735
4736 if (NILP (form))
4737 return 0;
4738
4739 /* Handle `(height HEIGHT)' specifications. */
4740 if (CONSP (spec)
4741 && EQ (XCAR (spec), Qheight)
4742 && CONSP (XCDR (spec)))
4743 {
4744 if (it)
4745 {
4746 if (!FRAME_WINDOW_P (it->f))
4747 return 0;
4748
4749 it->font_height = XCAR (XCDR (spec));
4750 if (!NILP (it->font_height))
4751 {
4752 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4753 int new_height = -1;
4754
4755 if (CONSP (it->font_height)
4756 && (EQ (XCAR (it->font_height), Qplus)
4757 || EQ (XCAR (it->font_height), Qminus))
4758 && CONSP (XCDR (it->font_height))
4759 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4760 {
4761 /* `(+ N)' or `(- N)' where N is an integer. */
4762 int steps = XINT (XCAR (XCDR (it->font_height)));
4763 if (EQ (XCAR (it->font_height), Qplus))
4764 steps = - steps;
4765 it->face_id = smaller_face (it->f, it->face_id, steps);
4766 }
4767 else if (FUNCTIONP (it->font_height))
4768 {
4769 /* Call function with current height as argument.
4770 Value is the new height. */
4771 Lisp_Object height;
4772 height = safe_call1 (it->font_height,
4773 face->lface[LFACE_HEIGHT_INDEX]);
4774 if (NUMBERP (height))
4775 new_height = XFLOATINT (height);
4776 }
4777 else if (NUMBERP (it->font_height))
4778 {
4779 /* Value is a multiple of the canonical char height. */
4780 struct face *f;
4781
4782 f = FACE_FROM_ID (it->f,
4783 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4784 new_height = (XFLOATINT (it->font_height)
4785 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4786 }
4787 else
4788 {
4789 /* Evaluate IT->font_height with `height' bound to the
4790 current specified height to get the new height. */
4791 ptrdiff_t count = SPECPDL_INDEX ();
4792
4793 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4794 value = safe_eval (it->font_height);
4795 unbind_to (count, Qnil);
4796
4797 if (NUMBERP (value))
4798 new_height = XFLOATINT (value);
4799 }
4800
4801 if (new_height > 0)
4802 it->face_id = face_with_height (it->f, it->face_id, new_height);
4803 }
4804 }
4805
4806 return 0;
4807 }
4808
4809 /* Handle `(space-width WIDTH)'. */
4810 if (CONSP (spec)
4811 && EQ (XCAR (spec), Qspace_width)
4812 && CONSP (XCDR (spec)))
4813 {
4814 if (it)
4815 {
4816 if (!FRAME_WINDOW_P (it->f))
4817 return 0;
4818
4819 value = XCAR (XCDR (spec));
4820 if (NUMBERP (value) && XFLOATINT (value) > 0)
4821 it->space_width = value;
4822 }
4823
4824 return 0;
4825 }
4826
4827 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4828 if (CONSP (spec)
4829 && EQ (XCAR (spec), Qslice))
4830 {
4831 Lisp_Object tem;
4832
4833 if (it)
4834 {
4835 if (!FRAME_WINDOW_P (it->f))
4836 return 0;
4837
4838 if (tem = XCDR (spec), CONSP (tem))
4839 {
4840 it->slice.x = XCAR (tem);
4841 if (tem = XCDR (tem), CONSP (tem))
4842 {
4843 it->slice.y = XCAR (tem);
4844 if (tem = XCDR (tem), CONSP (tem))
4845 {
4846 it->slice.width = XCAR (tem);
4847 if (tem = XCDR (tem), CONSP (tem))
4848 it->slice.height = XCAR (tem);
4849 }
4850 }
4851 }
4852 }
4853
4854 return 0;
4855 }
4856
4857 /* Handle `(raise FACTOR)'. */
4858 if (CONSP (spec)
4859 && EQ (XCAR (spec), Qraise)
4860 && CONSP (XCDR (spec)))
4861 {
4862 if (it)
4863 {
4864 if (!FRAME_WINDOW_P (it->f))
4865 return 0;
4866
4867 #ifdef HAVE_WINDOW_SYSTEM
4868 value = XCAR (XCDR (spec));
4869 if (NUMBERP (value))
4870 {
4871 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4872 it->voffset = - (XFLOATINT (value)
4873 * (FONT_HEIGHT (face->font)));
4874 }
4875 #endif /* HAVE_WINDOW_SYSTEM */
4876 }
4877
4878 return 0;
4879 }
4880
4881 /* Don't handle the other kinds of display specifications
4882 inside a string that we got from a `display' property. */
4883 if (it && it->string_from_display_prop_p)
4884 return 0;
4885
4886 /* Characters having this form of property are not displayed, so
4887 we have to find the end of the property. */
4888 if (it)
4889 {
4890 start_pos = *position;
4891 *position = display_prop_end (it, object, start_pos);
4892 }
4893 value = Qnil;
4894
4895 /* Stop the scan at that end position--we assume that all
4896 text properties change there. */
4897 if (it)
4898 it->stop_charpos = position->charpos;
4899
4900 /* Handle `(left-fringe BITMAP [FACE])'
4901 and `(right-fringe BITMAP [FACE])'. */
4902 if (CONSP (spec)
4903 && (EQ (XCAR (spec), Qleft_fringe)
4904 || EQ (XCAR (spec), Qright_fringe))
4905 && CONSP (XCDR (spec)))
4906 {
4907 int fringe_bitmap;
4908
4909 if (it)
4910 {
4911 if (!FRAME_WINDOW_P (it->f))
4912 /* If we return here, POSITION has been advanced
4913 across the text with this property. */
4914 {
4915 /* Synchronize the bidi iterator with POSITION. This is
4916 needed because we are not going to push the iterator
4917 on behalf of this display property, so there will be
4918 no pop_it call to do this synchronization for us. */
4919 if (it->bidi_p)
4920 {
4921 it->position = *position;
4922 iterate_out_of_display_property (it);
4923 *position = it->position;
4924 }
4925 return 1;
4926 }
4927 }
4928 else if (!frame_window_p)
4929 return 1;
4930
4931 #ifdef HAVE_WINDOW_SYSTEM
4932 value = XCAR (XCDR (spec));
4933 if (!SYMBOLP (value)
4934 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4935 /* If we return here, POSITION has been advanced
4936 across the text with this property. */
4937 {
4938 if (it && it->bidi_p)
4939 {
4940 it->position = *position;
4941 iterate_out_of_display_property (it);
4942 *position = it->position;
4943 }
4944 return 1;
4945 }
4946
4947 if (it)
4948 {
4949 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4950
4951 if (CONSP (XCDR (XCDR (spec))))
4952 {
4953 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4954 int face_id2 = lookup_derived_face (it->f, face_name,
4955 FRINGE_FACE_ID, 0);
4956 if (face_id2 >= 0)
4957 face_id = face_id2;
4958 }
4959
4960 /* Save current settings of IT so that we can restore them
4961 when we are finished with the glyph property value. */
4962 push_it (it, position);
4963
4964 it->area = TEXT_AREA;
4965 it->what = IT_IMAGE;
4966 it->image_id = -1; /* no image */
4967 it->position = start_pos;
4968 it->object = NILP (object) ? it->w->contents : object;
4969 it->method = GET_FROM_IMAGE;
4970 it->from_overlay = Qnil;
4971 it->face_id = face_id;
4972 it->from_disp_prop_p = true;
4973
4974 /* Say that we haven't consumed the characters with
4975 `display' property yet. The call to pop_it in
4976 set_iterator_to_next will clean this up. */
4977 *position = start_pos;
4978
4979 if (EQ (XCAR (spec), Qleft_fringe))
4980 {
4981 it->left_user_fringe_bitmap = fringe_bitmap;
4982 it->left_user_fringe_face_id = face_id;
4983 }
4984 else
4985 {
4986 it->right_user_fringe_bitmap = fringe_bitmap;
4987 it->right_user_fringe_face_id = face_id;
4988 }
4989 }
4990 #endif /* HAVE_WINDOW_SYSTEM */
4991 return 1;
4992 }
4993
4994 /* Prepare to handle `((margin left-margin) ...)',
4995 `((margin right-margin) ...)' and `((margin nil) ...)'
4996 prefixes for display specifications. */
4997 location = Qunbound;
4998 if (CONSP (spec) && CONSP (XCAR (spec)))
4999 {
5000 Lisp_Object tem;
5001
5002 value = XCDR (spec);
5003 if (CONSP (value))
5004 value = XCAR (value);
5005
5006 tem = XCAR (spec);
5007 if (EQ (XCAR (tem), Qmargin)
5008 && (tem = XCDR (tem),
5009 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5010 (NILP (tem)
5011 || EQ (tem, Qleft_margin)
5012 || EQ (tem, Qright_margin))))
5013 location = tem;
5014 }
5015
5016 if (EQ (location, Qunbound))
5017 {
5018 location = Qnil;
5019 value = spec;
5020 }
5021
5022 /* After this point, VALUE is the property after any
5023 margin prefix has been stripped. It must be a string,
5024 an image specification, or `(space ...)'.
5025
5026 LOCATION specifies where to display: `left-margin',
5027 `right-margin' or nil. */
5028
5029 valid_p = (STRINGP (value)
5030 #ifdef HAVE_WINDOW_SYSTEM
5031 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5032 && valid_image_p (value))
5033 #endif /* not HAVE_WINDOW_SYSTEM */
5034 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5035
5036 if (valid_p && !display_replaced_p)
5037 {
5038 int retval = 1;
5039
5040 if (!it)
5041 {
5042 /* Callers need to know whether the display spec is any kind
5043 of `(space ...)' spec that is about to affect text-area
5044 display. */
5045 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5046 retval = 2;
5047 return retval;
5048 }
5049
5050 /* Save current settings of IT so that we can restore them
5051 when we are finished with the glyph property value. */
5052 push_it (it, position);
5053 it->from_overlay = overlay;
5054 it->from_disp_prop_p = true;
5055
5056 if (NILP (location))
5057 it->area = TEXT_AREA;
5058 else if (EQ (location, Qleft_margin))
5059 it->area = LEFT_MARGIN_AREA;
5060 else
5061 it->area = RIGHT_MARGIN_AREA;
5062
5063 if (STRINGP (value))
5064 {
5065 it->string = value;
5066 it->multibyte_p = STRING_MULTIBYTE (it->string);
5067 it->current.overlay_string_index = -1;
5068 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5069 it->end_charpos = it->string_nchars = SCHARS (it->string);
5070 it->method = GET_FROM_STRING;
5071 it->stop_charpos = 0;
5072 it->prev_stop = 0;
5073 it->base_level_stop = 0;
5074 it->string_from_display_prop_p = true;
5075 /* Say that we haven't consumed the characters with
5076 `display' property yet. The call to pop_it in
5077 set_iterator_to_next will clean this up. */
5078 if (BUFFERP (object))
5079 *position = start_pos;
5080
5081 /* Force paragraph direction to be that of the parent
5082 object. If the parent object's paragraph direction is
5083 not yet determined, default to L2R. */
5084 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5085 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5086 else
5087 it->paragraph_embedding = L2R;
5088
5089 /* Set up the bidi iterator for this display string. */
5090 if (it->bidi_p)
5091 {
5092 it->bidi_it.string.lstring = it->string;
5093 it->bidi_it.string.s = NULL;
5094 it->bidi_it.string.schars = it->end_charpos;
5095 it->bidi_it.string.bufpos = bufpos;
5096 it->bidi_it.string.from_disp_str = 1;
5097 it->bidi_it.string.unibyte = !it->multibyte_p;
5098 it->bidi_it.w = it->w;
5099 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5100 }
5101 }
5102 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5103 {
5104 it->method = GET_FROM_STRETCH;
5105 it->object = value;
5106 *position = it->position = start_pos;
5107 retval = 1 + (it->area == TEXT_AREA);
5108 }
5109 #ifdef HAVE_WINDOW_SYSTEM
5110 else
5111 {
5112 it->what = IT_IMAGE;
5113 it->image_id = lookup_image (it->f, value);
5114 it->position = start_pos;
5115 it->object = NILP (object) ? it->w->contents : object;
5116 it->method = GET_FROM_IMAGE;
5117
5118 /* Say that we haven't consumed the characters with
5119 `display' property yet. The call to pop_it in
5120 set_iterator_to_next will clean this up. */
5121 *position = start_pos;
5122 }
5123 #endif /* HAVE_WINDOW_SYSTEM */
5124
5125 return retval;
5126 }
5127
5128 /* Invalid property or property not supported. Restore
5129 POSITION to what it was before. */
5130 *position = start_pos;
5131 return 0;
5132 }
5133
5134 /* Check if PROP is a display property value whose text should be
5135 treated as intangible. OVERLAY is the overlay from which PROP
5136 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5137 specify the buffer position covered by PROP. */
5138
5139 int
5140 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5141 ptrdiff_t charpos, ptrdiff_t bytepos)
5142 {
5143 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5144 struct text_pos position;
5145
5146 SET_TEXT_POS (position, charpos, bytepos);
5147 return handle_display_spec (NULL, prop, Qnil, overlay,
5148 &position, charpos, frame_window_p);
5149 }
5150
5151
5152 /* Return 1 if PROP is a display sub-property value containing STRING.
5153
5154 Implementation note: this and the following function are really
5155 special cases of handle_display_spec and
5156 handle_single_display_spec, and should ideally use the same code.
5157 Until they do, these two pairs must be consistent and must be
5158 modified in sync. */
5159
5160 static int
5161 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5162 {
5163 if (EQ (string, prop))
5164 return 1;
5165
5166 /* Skip over `when FORM'. */
5167 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5168 {
5169 prop = XCDR (prop);
5170 if (!CONSP (prop))
5171 return 0;
5172 /* Actually, the condition following `when' should be eval'ed,
5173 like handle_single_display_spec does, and we should return
5174 zero if it evaluates to nil. However, this function is
5175 called only when the buffer was already displayed and some
5176 glyph in the glyph matrix was found to come from a display
5177 string. Therefore, the condition was already evaluated, and
5178 the result was non-nil, otherwise the display string wouldn't
5179 have been displayed and we would have never been called for
5180 this property. Thus, we can skip the evaluation and assume
5181 its result is non-nil. */
5182 prop = XCDR (prop);
5183 }
5184
5185 if (CONSP (prop))
5186 /* Skip over `margin LOCATION'. */
5187 if (EQ (XCAR (prop), Qmargin))
5188 {
5189 prop = XCDR (prop);
5190 if (!CONSP (prop))
5191 return 0;
5192
5193 prop = XCDR (prop);
5194 if (!CONSP (prop))
5195 return 0;
5196 }
5197
5198 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5199 }
5200
5201
5202 /* Return 1 if STRING appears in the `display' property PROP. */
5203
5204 static int
5205 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5206 {
5207 if (CONSP (prop)
5208 && !EQ (XCAR (prop), Qwhen)
5209 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5210 {
5211 /* A list of sub-properties. */
5212 while (CONSP (prop))
5213 {
5214 if (single_display_spec_string_p (XCAR (prop), string))
5215 return 1;
5216 prop = XCDR (prop);
5217 }
5218 }
5219 else if (VECTORP (prop))
5220 {
5221 /* A vector of sub-properties. */
5222 ptrdiff_t i;
5223 for (i = 0; i < ASIZE (prop); ++i)
5224 if (single_display_spec_string_p (AREF (prop, i), string))
5225 return 1;
5226 }
5227 else
5228 return single_display_spec_string_p (prop, string);
5229
5230 return 0;
5231 }
5232
5233 /* Look for STRING in overlays and text properties in the current
5234 buffer, between character positions FROM and TO (excluding TO).
5235 BACK_P non-zero means look back (in this case, TO is supposed to be
5236 less than FROM).
5237 Value is the first character position where STRING was found, or
5238 zero if it wasn't found before hitting TO.
5239
5240 This function may only use code that doesn't eval because it is
5241 called asynchronously from note_mouse_highlight. */
5242
5243 static ptrdiff_t
5244 string_buffer_position_lim (Lisp_Object string,
5245 ptrdiff_t from, ptrdiff_t to, int back_p)
5246 {
5247 Lisp_Object limit, prop, pos;
5248 int found = 0;
5249
5250 pos = make_number (max (from, BEGV));
5251
5252 if (!back_p) /* looking forward */
5253 {
5254 limit = make_number (min (to, ZV));
5255 while (!found && !EQ (pos, limit))
5256 {
5257 prop = Fget_char_property (pos, Qdisplay, Qnil);
5258 if (!NILP (prop) && display_prop_string_p (prop, string))
5259 found = 1;
5260 else
5261 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5262 limit);
5263 }
5264 }
5265 else /* looking back */
5266 {
5267 limit = make_number (max (to, BEGV));
5268 while (!found && !EQ (pos, limit))
5269 {
5270 prop = Fget_char_property (pos, Qdisplay, Qnil);
5271 if (!NILP (prop) && display_prop_string_p (prop, string))
5272 found = 1;
5273 else
5274 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5275 limit);
5276 }
5277 }
5278
5279 return found ? XINT (pos) : 0;
5280 }
5281
5282 /* Determine which buffer position in current buffer STRING comes from.
5283 AROUND_CHARPOS is an approximate position where it could come from.
5284 Value is the buffer position or 0 if it couldn't be determined.
5285
5286 This function is necessary because we don't record buffer positions
5287 in glyphs generated from strings (to keep struct glyph small).
5288 This function may only use code that doesn't eval because it is
5289 called asynchronously from note_mouse_highlight. */
5290
5291 static ptrdiff_t
5292 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5293 {
5294 const int MAX_DISTANCE = 1000;
5295 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5296 around_charpos + MAX_DISTANCE,
5297 0);
5298
5299 if (!found)
5300 found = string_buffer_position_lim (string, around_charpos,
5301 around_charpos - MAX_DISTANCE, 1);
5302 return found;
5303 }
5304
5305
5306 \f
5307 /***********************************************************************
5308 `composition' property
5309 ***********************************************************************/
5310
5311 /* Set up iterator IT from `composition' property at its current
5312 position. Called from handle_stop. */
5313
5314 static enum prop_handled
5315 handle_composition_prop (struct it *it)
5316 {
5317 Lisp_Object prop, string;
5318 ptrdiff_t pos, pos_byte, start, end;
5319
5320 if (STRINGP (it->string))
5321 {
5322 unsigned char *s;
5323
5324 pos = IT_STRING_CHARPOS (*it);
5325 pos_byte = IT_STRING_BYTEPOS (*it);
5326 string = it->string;
5327 s = SDATA (string) + pos_byte;
5328 it->c = STRING_CHAR (s);
5329 }
5330 else
5331 {
5332 pos = IT_CHARPOS (*it);
5333 pos_byte = IT_BYTEPOS (*it);
5334 string = Qnil;
5335 it->c = FETCH_CHAR (pos_byte);
5336 }
5337
5338 /* If there's a valid composition and point is not inside of the
5339 composition (in the case that the composition is from the current
5340 buffer), draw a glyph composed from the composition components. */
5341 if (find_composition (pos, -1, &start, &end, &prop, string)
5342 && composition_valid_p (start, end, prop)
5343 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5344 {
5345 if (start < pos)
5346 /* As we can't handle this situation (perhaps font-lock added
5347 a new composition), we just return here hoping that next
5348 redisplay will detect this composition much earlier. */
5349 return HANDLED_NORMALLY;
5350 if (start != pos)
5351 {
5352 if (STRINGP (it->string))
5353 pos_byte = string_char_to_byte (it->string, start);
5354 else
5355 pos_byte = CHAR_TO_BYTE (start);
5356 }
5357 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5358 prop, string);
5359
5360 if (it->cmp_it.id >= 0)
5361 {
5362 it->cmp_it.ch = -1;
5363 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5364 it->cmp_it.nglyphs = -1;
5365 }
5366 }
5367
5368 return HANDLED_NORMALLY;
5369 }
5370
5371
5372 \f
5373 /***********************************************************************
5374 Overlay strings
5375 ***********************************************************************/
5376
5377 /* The following structure is used to record overlay strings for
5378 later sorting in load_overlay_strings. */
5379
5380 struct overlay_entry
5381 {
5382 Lisp_Object overlay;
5383 Lisp_Object string;
5384 EMACS_INT priority;
5385 int after_string_p;
5386 };
5387
5388
5389 /* Set up iterator IT from overlay strings at its current position.
5390 Called from handle_stop. */
5391
5392 static enum prop_handled
5393 handle_overlay_change (struct it *it)
5394 {
5395 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5396 return HANDLED_RECOMPUTE_PROPS;
5397 else
5398 return HANDLED_NORMALLY;
5399 }
5400
5401
5402 /* Set up the next overlay string for delivery by IT, if there is an
5403 overlay string to deliver. Called by set_iterator_to_next when the
5404 end of the current overlay string is reached. If there are more
5405 overlay strings to display, IT->string and
5406 IT->current.overlay_string_index are set appropriately here.
5407 Otherwise IT->string is set to nil. */
5408
5409 static void
5410 next_overlay_string (struct it *it)
5411 {
5412 ++it->current.overlay_string_index;
5413 if (it->current.overlay_string_index == it->n_overlay_strings)
5414 {
5415 /* No more overlay strings. Restore IT's settings to what
5416 they were before overlay strings were processed, and
5417 continue to deliver from current_buffer. */
5418
5419 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5420 pop_it (it);
5421 eassert (it->sp > 0
5422 || (NILP (it->string)
5423 && it->method == GET_FROM_BUFFER
5424 && it->stop_charpos >= BEGV
5425 && it->stop_charpos <= it->end_charpos));
5426 it->current.overlay_string_index = -1;
5427 it->n_overlay_strings = 0;
5428 it->overlay_strings_charpos = -1;
5429 /* If there's an empty display string on the stack, pop the
5430 stack, to resync the bidi iterator with IT's position. Such
5431 empty strings are pushed onto the stack in
5432 get_overlay_strings_1. */
5433 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5434 pop_it (it);
5435
5436 /* If we're at the end of the buffer, record that we have
5437 processed the overlay strings there already, so that
5438 next_element_from_buffer doesn't try it again. */
5439 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5440 it->overlay_strings_at_end_processed_p = true;
5441 }
5442 else
5443 {
5444 /* There are more overlay strings to process. If
5445 IT->current.overlay_string_index has advanced to a position
5446 where we must load IT->overlay_strings with more strings, do
5447 it. We must load at the IT->overlay_strings_charpos where
5448 IT->n_overlay_strings was originally computed; when invisible
5449 text is present, this might not be IT_CHARPOS (Bug#7016). */
5450 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5451
5452 if (it->current.overlay_string_index && i == 0)
5453 load_overlay_strings (it, it->overlay_strings_charpos);
5454
5455 /* Initialize IT to deliver display elements from the overlay
5456 string. */
5457 it->string = it->overlay_strings[i];
5458 it->multibyte_p = STRING_MULTIBYTE (it->string);
5459 SET_TEXT_POS (it->current.string_pos, 0, 0);
5460 it->method = GET_FROM_STRING;
5461 it->stop_charpos = 0;
5462 it->end_charpos = SCHARS (it->string);
5463 if (it->cmp_it.stop_pos >= 0)
5464 it->cmp_it.stop_pos = 0;
5465 it->prev_stop = 0;
5466 it->base_level_stop = 0;
5467
5468 /* Set up the bidi iterator for this overlay string. */
5469 if (it->bidi_p)
5470 {
5471 it->bidi_it.string.lstring = it->string;
5472 it->bidi_it.string.s = NULL;
5473 it->bidi_it.string.schars = SCHARS (it->string);
5474 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5475 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5476 it->bidi_it.string.unibyte = !it->multibyte_p;
5477 it->bidi_it.w = it->w;
5478 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5479 }
5480 }
5481
5482 CHECK_IT (it);
5483 }
5484
5485
5486 /* Compare two overlay_entry structures E1 and E2. Used as a
5487 comparison function for qsort in load_overlay_strings. Overlay
5488 strings for the same position are sorted so that
5489
5490 1. All after-strings come in front of before-strings, except
5491 when they come from the same overlay.
5492
5493 2. Within after-strings, strings are sorted so that overlay strings
5494 from overlays with higher priorities come first.
5495
5496 2. Within before-strings, strings are sorted so that overlay
5497 strings from overlays with higher priorities come last.
5498
5499 Value is analogous to strcmp. */
5500
5501
5502 static int
5503 compare_overlay_entries (const void *e1, const void *e2)
5504 {
5505 struct overlay_entry const *entry1 = e1;
5506 struct overlay_entry const *entry2 = e2;
5507 int result;
5508
5509 if (entry1->after_string_p != entry2->after_string_p)
5510 {
5511 /* Let after-strings appear in front of before-strings if
5512 they come from different overlays. */
5513 if (EQ (entry1->overlay, entry2->overlay))
5514 result = entry1->after_string_p ? 1 : -1;
5515 else
5516 result = entry1->after_string_p ? -1 : 1;
5517 }
5518 else if (entry1->priority != entry2->priority)
5519 {
5520 if (entry1->after_string_p)
5521 /* After-strings sorted in order of decreasing priority. */
5522 result = entry2->priority < entry1->priority ? -1 : 1;
5523 else
5524 /* Before-strings sorted in order of increasing priority. */
5525 result = entry1->priority < entry2->priority ? -1 : 1;
5526 }
5527 else
5528 result = 0;
5529
5530 return result;
5531 }
5532
5533
5534 /* Load the vector IT->overlay_strings with overlay strings from IT's
5535 current buffer position, or from CHARPOS if that is > 0. Set
5536 IT->n_overlays to the total number of overlay strings found.
5537
5538 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5539 a time. On entry into load_overlay_strings,
5540 IT->current.overlay_string_index gives the number of overlay
5541 strings that have already been loaded by previous calls to this
5542 function.
5543
5544 IT->add_overlay_start contains an additional overlay start
5545 position to consider for taking overlay strings from, if non-zero.
5546 This position comes into play when the overlay has an `invisible'
5547 property, and both before and after-strings. When we've skipped to
5548 the end of the overlay, because of its `invisible' property, we
5549 nevertheless want its before-string to appear.
5550 IT->add_overlay_start will contain the overlay start position
5551 in this case.
5552
5553 Overlay strings are sorted so that after-string strings come in
5554 front of before-string strings. Within before and after-strings,
5555 strings are sorted by overlay priority. See also function
5556 compare_overlay_entries. */
5557
5558 static void
5559 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5560 {
5561 Lisp_Object overlay, window, str, invisible;
5562 struct Lisp_Overlay *ov;
5563 ptrdiff_t start, end;
5564 ptrdiff_t size = 20;
5565 ptrdiff_t n = 0, i, j;
5566 int invis_p;
5567 struct overlay_entry *entries = alloca (size * sizeof *entries);
5568 USE_SAFE_ALLOCA;
5569
5570 if (charpos <= 0)
5571 charpos = IT_CHARPOS (*it);
5572
5573 /* Append the overlay string STRING of overlay OVERLAY to vector
5574 `entries' which has size `size' and currently contains `n'
5575 elements. AFTER_P non-zero means STRING is an after-string of
5576 OVERLAY. */
5577 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5578 do \
5579 { \
5580 Lisp_Object priority; \
5581 \
5582 if (n == size) \
5583 { \
5584 struct overlay_entry *old = entries; \
5585 SAFE_NALLOCA (entries, 2, size); \
5586 memcpy (entries, old, size * sizeof *entries); \
5587 size *= 2; \
5588 } \
5589 \
5590 entries[n].string = (STRING); \
5591 entries[n].overlay = (OVERLAY); \
5592 priority = Foverlay_get ((OVERLAY), Qpriority); \
5593 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5594 entries[n].after_string_p = (AFTER_P); \
5595 ++n; \
5596 } \
5597 while (0)
5598
5599 /* Process overlay before the overlay center. */
5600 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5601 {
5602 XSETMISC (overlay, ov);
5603 eassert (OVERLAYP (overlay));
5604 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5605 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5606
5607 if (end < charpos)
5608 break;
5609
5610 /* Skip this overlay if it doesn't start or end at IT's current
5611 position. */
5612 if (end != charpos && start != charpos)
5613 continue;
5614
5615 /* Skip this overlay if it doesn't apply to IT->w. */
5616 window = Foverlay_get (overlay, Qwindow);
5617 if (WINDOWP (window) && XWINDOW (window) != it->w)
5618 continue;
5619
5620 /* If the text ``under'' the overlay is invisible, both before-
5621 and after-strings from this overlay are visible; start and
5622 end position are indistinguishable. */
5623 invisible = Foverlay_get (overlay, Qinvisible);
5624 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5625
5626 /* If overlay has a non-empty before-string, record it. */
5627 if ((start == charpos || (end == charpos && invis_p))
5628 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5629 && SCHARS (str))
5630 RECORD_OVERLAY_STRING (overlay, str, 0);
5631
5632 /* If overlay has a non-empty after-string, record it. */
5633 if ((end == charpos || (start == charpos && invis_p))
5634 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5635 && SCHARS (str))
5636 RECORD_OVERLAY_STRING (overlay, str, 1);
5637 }
5638
5639 /* Process overlays after the overlay center. */
5640 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5641 {
5642 XSETMISC (overlay, ov);
5643 eassert (OVERLAYP (overlay));
5644 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5645 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5646
5647 if (start > charpos)
5648 break;
5649
5650 /* Skip this overlay if it doesn't start or end at IT's current
5651 position. */
5652 if (end != charpos && start != charpos)
5653 continue;
5654
5655 /* Skip this overlay if it doesn't apply to IT->w. */
5656 window = Foverlay_get (overlay, Qwindow);
5657 if (WINDOWP (window) && XWINDOW (window) != it->w)
5658 continue;
5659
5660 /* If the text ``under'' the overlay is invisible, it has a zero
5661 dimension, and both before- and after-strings apply. */
5662 invisible = Foverlay_get (overlay, Qinvisible);
5663 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5664
5665 /* If overlay has a non-empty before-string, record it. */
5666 if ((start == charpos || (end == charpos && invis_p))
5667 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5668 && SCHARS (str))
5669 RECORD_OVERLAY_STRING (overlay, str, 0);
5670
5671 /* If overlay has a non-empty after-string, record it. */
5672 if ((end == charpos || (start == charpos && invis_p))
5673 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5674 && SCHARS (str))
5675 RECORD_OVERLAY_STRING (overlay, str, 1);
5676 }
5677
5678 #undef RECORD_OVERLAY_STRING
5679
5680 /* Sort entries. */
5681 if (n > 1)
5682 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5683
5684 /* Record number of overlay strings, and where we computed it. */
5685 it->n_overlay_strings = n;
5686 it->overlay_strings_charpos = charpos;
5687
5688 /* IT->current.overlay_string_index is the number of overlay strings
5689 that have already been consumed by IT. Copy some of the
5690 remaining overlay strings to IT->overlay_strings. */
5691 i = 0;
5692 j = it->current.overlay_string_index;
5693 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5694 {
5695 it->overlay_strings[i] = entries[j].string;
5696 it->string_overlays[i++] = entries[j++].overlay;
5697 }
5698
5699 CHECK_IT (it);
5700 SAFE_FREE ();
5701 }
5702
5703
5704 /* Get the first chunk of overlay strings at IT's current buffer
5705 position, or at CHARPOS if that is > 0. Value is non-zero if at
5706 least one overlay string was found. */
5707
5708 static int
5709 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5710 {
5711 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5712 process. This fills IT->overlay_strings with strings, and sets
5713 IT->n_overlay_strings to the total number of strings to process.
5714 IT->pos.overlay_string_index has to be set temporarily to zero
5715 because load_overlay_strings needs this; it must be set to -1
5716 when no overlay strings are found because a zero value would
5717 indicate a position in the first overlay string. */
5718 it->current.overlay_string_index = 0;
5719 load_overlay_strings (it, charpos);
5720
5721 /* If we found overlay strings, set up IT to deliver display
5722 elements from the first one. Otherwise set up IT to deliver
5723 from current_buffer. */
5724 if (it->n_overlay_strings)
5725 {
5726 /* Make sure we know settings in current_buffer, so that we can
5727 restore meaningful values when we're done with the overlay
5728 strings. */
5729 if (compute_stop_p)
5730 compute_stop_pos (it);
5731 eassert (it->face_id >= 0);
5732
5733 /* Save IT's settings. They are restored after all overlay
5734 strings have been processed. */
5735 eassert (!compute_stop_p || it->sp == 0);
5736
5737 /* When called from handle_stop, there might be an empty display
5738 string loaded. In that case, don't bother saving it. But
5739 don't use this optimization with the bidi iterator, since we
5740 need the corresponding pop_it call to resync the bidi
5741 iterator's position with IT's position, after we are done
5742 with the overlay strings. (The corresponding call to pop_it
5743 in case of an empty display string is in
5744 next_overlay_string.) */
5745 if (!(!it->bidi_p
5746 && STRINGP (it->string) && !SCHARS (it->string)))
5747 push_it (it, NULL);
5748
5749 /* Set up IT to deliver display elements from the first overlay
5750 string. */
5751 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5752 it->string = it->overlay_strings[0];
5753 it->from_overlay = Qnil;
5754 it->stop_charpos = 0;
5755 eassert (STRINGP (it->string));
5756 it->end_charpos = SCHARS (it->string);
5757 it->prev_stop = 0;
5758 it->base_level_stop = 0;
5759 it->multibyte_p = STRING_MULTIBYTE (it->string);
5760 it->method = GET_FROM_STRING;
5761 it->from_disp_prop_p = 0;
5762
5763 /* Force paragraph direction to be that of the parent
5764 buffer. */
5765 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5766 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5767 else
5768 it->paragraph_embedding = L2R;
5769
5770 /* Set up the bidi iterator for this overlay string. */
5771 if (it->bidi_p)
5772 {
5773 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5774
5775 it->bidi_it.string.lstring = it->string;
5776 it->bidi_it.string.s = NULL;
5777 it->bidi_it.string.schars = SCHARS (it->string);
5778 it->bidi_it.string.bufpos = pos;
5779 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5780 it->bidi_it.string.unibyte = !it->multibyte_p;
5781 it->bidi_it.w = it->w;
5782 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5783 }
5784 return 1;
5785 }
5786
5787 it->current.overlay_string_index = -1;
5788 return 0;
5789 }
5790
5791 static int
5792 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5793 {
5794 it->string = Qnil;
5795 it->method = GET_FROM_BUFFER;
5796
5797 (void) get_overlay_strings_1 (it, charpos, 1);
5798
5799 CHECK_IT (it);
5800
5801 /* Value is non-zero if we found at least one overlay string. */
5802 return STRINGP (it->string);
5803 }
5804
5805
5806 \f
5807 /***********************************************************************
5808 Saving and restoring state
5809 ***********************************************************************/
5810
5811 /* Save current settings of IT on IT->stack. Called, for example,
5812 before setting up IT for an overlay string, to be able to restore
5813 IT's settings to what they were after the overlay string has been
5814 processed. If POSITION is non-NULL, it is the position to save on
5815 the stack instead of IT->position. */
5816
5817 static void
5818 push_it (struct it *it, struct text_pos *position)
5819 {
5820 struct iterator_stack_entry *p;
5821
5822 eassert (it->sp < IT_STACK_SIZE);
5823 p = it->stack + it->sp;
5824
5825 p->stop_charpos = it->stop_charpos;
5826 p->prev_stop = it->prev_stop;
5827 p->base_level_stop = it->base_level_stop;
5828 p->cmp_it = it->cmp_it;
5829 eassert (it->face_id >= 0);
5830 p->face_id = it->face_id;
5831 p->string = it->string;
5832 p->method = it->method;
5833 p->from_overlay = it->from_overlay;
5834 switch (p->method)
5835 {
5836 case GET_FROM_IMAGE:
5837 p->u.image.object = it->object;
5838 p->u.image.image_id = it->image_id;
5839 p->u.image.slice = it->slice;
5840 break;
5841 case GET_FROM_STRETCH:
5842 p->u.stretch.object = it->object;
5843 break;
5844 }
5845 p->position = position ? *position : it->position;
5846 p->current = it->current;
5847 p->end_charpos = it->end_charpos;
5848 p->string_nchars = it->string_nchars;
5849 p->area = it->area;
5850 p->multibyte_p = it->multibyte_p;
5851 p->avoid_cursor_p = it->avoid_cursor_p;
5852 p->space_width = it->space_width;
5853 p->font_height = it->font_height;
5854 p->voffset = it->voffset;
5855 p->string_from_display_prop_p = it->string_from_display_prop_p;
5856 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5857 p->display_ellipsis_p = 0;
5858 p->line_wrap = it->line_wrap;
5859 p->bidi_p = it->bidi_p;
5860 p->paragraph_embedding = it->paragraph_embedding;
5861 p->from_disp_prop_p = it->from_disp_prop_p;
5862 ++it->sp;
5863
5864 /* Save the state of the bidi iterator as well. */
5865 if (it->bidi_p)
5866 bidi_push_it (&it->bidi_it);
5867 }
5868
5869 static void
5870 iterate_out_of_display_property (struct it *it)
5871 {
5872 int buffer_p = !STRINGP (it->string);
5873 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5874 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5875
5876 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5877
5878 /* Maybe initialize paragraph direction. If we are at the beginning
5879 of a new paragraph, next_element_from_buffer may not have a
5880 chance to do that. */
5881 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5882 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5883 /* prev_stop can be zero, so check against BEGV as well. */
5884 while (it->bidi_it.charpos >= bob
5885 && it->prev_stop <= it->bidi_it.charpos
5886 && it->bidi_it.charpos < CHARPOS (it->position)
5887 && it->bidi_it.charpos < eob)
5888 bidi_move_to_visually_next (&it->bidi_it);
5889 /* Record the stop_pos we just crossed, for when we cross it
5890 back, maybe. */
5891 if (it->bidi_it.charpos > CHARPOS (it->position))
5892 it->prev_stop = CHARPOS (it->position);
5893 /* If we ended up not where pop_it put us, resync IT's
5894 positional members with the bidi iterator. */
5895 if (it->bidi_it.charpos != CHARPOS (it->position))
5896 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5897 if (buffer_p)
5898 it->current.pos = it->position;
5899 else
5900 it->current.string_pos = it->position;
5901 }
5902
5903 /* Restore IT's settings from IT->stack. Called, for example, when no
5904 more overlay strings must be processed, and we return to delivering
5905 display elements from a buffer, or when the end of a string from a
5906 `display' property is reached and we return to delivering display
5907 elements from an overlay string, or from a buffer. */
5908
5909 static void
5910 pop_it (struct it *it)
5911 {
5912 struct iterator_stack_entry *p;
5913 int from_display_prop = it->from_disp_prop_p;
5914
5915 eassert (it->sp > 0);
5916 --it->sp;
5917 p = it->stack + it->sp;
5918 it->stop_charpos = p->stop_charpos;
5919 it->prev_stop = p->prev_stop;
5920 it->base_level_stop = p->base_level_stop;
5921 it->cmp_it = p->cmp_it;
5922 it->face_id = p->face_id;
5923 it->current = p->current;
5924 it->position = p->position;
5925 it->string = p->string;
5926 it->from_overlay = p->from_overlay;
5927 if (NILP (it->string))
5928 SET_TEXT_POS (it->current.string_pos, -1, -1);
5929 it->method = p->method;
5930 switch (it->method)
5931 {
5932 case GET_FROM_IMAGE:
5933 it->image_id = p->u.image.image_id;
5934 it->object = p->u.image.object;
5935 it->slice = p->u.image.slice;
5936 break;
5937 case GET_FROM_STRETCH:
5938 it->object = p->u.stretch.object;
5939 break;
5940 case GET_FROM_BUFFER:
5941 it->object = it->w->contents;
5942 break;
5943 case GET_FROM_STRING:
5944 it->object = it->string;
5945 break;
5946 case GET_FROM_DISPLAY_VECTOR:
5947 if (it->s)
5948 it->method = GET_FROM_C_STRING;
5949 else if (STRINGP (it->string))
5950 it->method = GET_FROM_STRING;
5951 else
5952 {
5953 it->method = GET_FROM_BUFFER;
5954 it->object = it->w->contents;
5955 }
5956 }
5957 it->end_charpos = p->end_charpos;
5958 it->string_nchars = p->string_nchars;
5959 it->area = p->area;
5960 it->multibyte_p = p->multibyte_p;
5961 it->avoid_cursor_p = p->avoid_cursor_p;
5962 it->space_width = p->space_width;
5963 it->font_height = p->font_height;
5964 it->voffset = p->voffset;
5965 it->string_from_display_prop_p = p->string_from_display_prop_p;
5966 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5967 it->line_wrap = p->line_wrap;
5968 it->bidi_p = p->bidi_p;
5969 it->paragraph_embedding = p->paragraph_embedding;
5970 it->from_disp_prop_p = p->from_disp_prop_p;
5971 if (it->bidi_p)
5972 {
5973 bidi_pop_it (&it->bidi_it);
5974 /* Bidi-iterate until we get out of the portion of text, if any,
5975 covered by a `display' text property or by an overlay with
5976 `display' property. (We cannot just jump there, because the
5977 internal coherency of the bidi iterator state can not be
5978 preserved across such jumps.) We also must determine the
5979 paragraph base direction if the overlay we just processed is
5980 at the beginning of a new paragraph. */
5981 if (from_display_prop
5982 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5983 iterate_out_of_display_property (it);
5984
5985 eassert ((BUFFERP (it->object)
5986 && IT_CHARPOS (*it) == it->bidi_it.charpos
5987 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5988 || (STRINGP (it->object)
5989 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5990 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5991 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5992 }
5993 }
5994
5995
5996 \f
5997 /***********************************************************************
5998 Moving over lines
5999 ***********************************************************************/
6000
6001 /* Set IT's current position to the previous line start. */
6002
6003 static void
6004 back_to_previous_line_start (struct it *it)
6005 {
6006 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6007
6008 DEC_BOTH (cp, bp);
6009 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6010 }
6011
6012
6013 /* Move IT to the next line start.
6014
6015 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6016 we skipped over part of the text (as opposed to moving the iterator
6017 continuously over the text). Otherwise, don't change the value
6018 of *SKIPPED_P.
6019
6020 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6021 iterator on the newline, if it was found.
6022
6023 Newlines may come from buffer text, overlay strings, or strings
6024 displayed via the `display' property. That's the reason we can't
6025 simply use find_newline_no_quit.
6026
6027 Note that this function may not skip over invisible text that is so
6028 because of text properties and immediately follows a newline. If
6029 it would, function reseat_at_next_visible_line_start, when called
6030 from set_iterator_to_next, would effectively make invisible
6031 characters following a newline part of the wrong glyph row, which
6032 leads to wrong cursor motion. */
6033
6034 static int
6035 forward_to_next_line_start (struct it *it, int *skipped_p,
6036 struct bidi_it *bidi_it_prev)
6037 {
6038 ptrdiff_t old_selective;
6039 int newline_found_p, n;
6040 const int MAX_NEWLINE_DISTANCE = 500;
6041
6042 /* If already on a newline, just consume it to avoid unintended
6043 skipping over invisible text below. */
6044 if (it->what == IT_CHARACTER
6045 && it->c == '\n'
6046 && CHARPOS (it->position) == IT_CHARPOS (*it))
6047 {
6048 if (it->bidi_p && bidi_it_prev)
6049 *bidi_it_prev = it->bidi_it;
6050 set_iterator_to_next (it, 0);
6051 it->c = 0;
6052 return 1;
6053 }
6054
6055 /* Don't handle selective display in the following. It's (a)
6056 unnecessary because it's done by the caller, and (b) leads to an
6057 infinite recursion because next_element_from_ellipsis indirectly
6058 calls this function. */
6059 old_selective = it->selective;
6060 it->selective = 0;
6061
6062 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6063 from buffer text. */
6064 for (n = newline_found_p = 0;
6065 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6066 n += STRINGP (it->string) ? 0 : 1)
6067 {
6068 if (!get_next_display_element (it))
6069 return 0;
6070 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6071 if (newline_found_p && it->bidi_p && bidi_it_prev)
6072 *bidi_it_prev = it->bidi_it;
6073 set_iterator_to_next (it, 0);
6074 }
6075
6076 /* If we didn't find a newline near enough, see if we can use a
6077 short-cut. */
6078 if (!newline_found_p)
6079 {
6080 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6081 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6082 1, &bytepos);
6083 Lisp_Object pos;
6084
6085 eassert (!STRINGP (it->string));
6086
6087 /* If there isn't any `display' property in sight, and no
6088 overlays, we can just use the position of the newline in
6089 buffer text. */
6090 if (it->stop_charpos >= limit
6091 || ((pos = Fnext_single_property_change (make_number (start),
6092 Qdisplay, Qnil,
6093 make_number (limit)),
6094 NILP (pos))
6095 && next_overlay_change (start) == ZV))
6096 {
6097 if (!it->bidi_p)
6098 {
6099 IT_CHARPOS (*it) = limit;
6100 IT_BYTEPOS (*it) = bytepos;
6101 }
6102 else
6103 {
6104 struct bidi_it bprev;
6105
6106 /* Help bidi.c avoid expensive searches for display
6107 properties and overlays, by telling it that there are
6108 none up to `limit'. */
6109 if (it->bidi_it.disp_pos < limit)
6110 {
6111 it->bidi_it.disp_pos = limit;
6112 it->bidi_it.disp_prop = 0;
6113 }
6114 do {
6115 bprev = it->bidi_it;
6116 bidi_move_to_visually_next (&it->bidi_it);
6117 } while (it->bidi_it.charpos != limit);
6118 IT_CHARPOS (*it) = limit;
6119 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6120 if (bidi_it_prev)
6121 *bidi_it_prev = bprev;
6122 }
6123 *skipped_p = newline_found_p = true;
6124 }
6125 else
6126 {
6127 while (get_next_display_element (it)
6128 && !newline_found_p)
6129 {
6130 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6131 if (newline_found_p && it->bidi_p && bidi_it_prev)
6132 *bidi_it_prev = it->bidi_it;
6133 set_iterator_to_next (it, 0);
6134 }
6135 }
6136 }
6137
6138 it->selective = old_selective;
6139 return newline_found_p;
6140 }
6141
6142
6143 /* Set IT's current position to the previous visible line start. Skip
6144 invisible text that is so either due to text properties or due to
6145 selective display. Caution: this does not change IT->current_x and
6146 IT->hpos. */
6147
6148 static void
6149 back_to_previous_visible_line_start (struct it *it)
6150 {
6151 while (IT_CHARPOS (*it) > BEGV)
6152 {
6153 back_to_previous_line_start (it);
6154
6155 if (IT_CHARPOS (*it) <= BEGV)
6156 break;
6157
6158 /* If selective > 0, then lines indented more than its value are
6159 invisible. */
6160 if (it->selective > 0
6161 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6162 it->selective))
6163 continue;
6164
6165 /* Check the newline before point for invisibility. */
6166 {
6167 Lisp_Object prop;
6168 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6169 Qinvisible, it->window);
6170 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6171 continue;
6172 }
6173
6174 if (IT_CHARPOS (*it) <= BEGV)
6175 break;
6176
6177 {
6178 struct it it2;
6179 void *it2data = NULL;
6180 ptrdiff_t pos;
6181 ptrdiff_t beg, end;
6182 Lisp_Object val, overlay;
6183
6184 SAVE_IT (it2, *it, it2data);
6185
6186 /* If newline is part of a composition, continue from start of composition */
6187 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6188 && beg < IT_CHARPOS (*it))
6189 goto replaced;
6190
6191 /* If newline is replaced by a display property, find start of overlay
6192 or interval and continue search from that point. */
6193 pos = --IT_CHARPOS (it2);
6194 --IT_BYTEPOS (it2);
6195 it2.sp = 0;
6196 bidi_unshelve_cache (NULL, 0);
6197 it2.string_from_display_prop_p = 0;
6198 it2.from_disp_prop_p = 0;
6199 if (handle_display_prop (&it2) == HANDLED_RETURN
6200 && !NILP (val = get_char_property_and_overlay
6201 (make_number (pos), Qdisplay, Qnil, &overlay))
6202 && (OVERLAYP (overlay)
6203 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6204 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6205 {
6206 RESTORE_IT (it, it, it2data);
6207 goto replaced;
6208 }
6209
6210 /* Newline is not replaced by anything -- so we are done. */
6211 RESTORE_IT (it, it, it2data);
6212 break;
6213
6214 replaced:
6215 if (beg < BEGV)
6216 beg = BEGV;
6217 IT_CHARPOS (*it) = beg;
6218 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6219 }
6220 }
6221
6222 it->continuation_lines_width = 0;
6223
6224 eassert (IT_CHARPOS (*it) >= BEGV);
6225 eassert (IT_CHARPOS (*it) == BEGV
6226 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6227 CHECK_IT (it);
6228 }
6229
6230
6231 /* Reseat iterator IT at the previous visible line start. Skip
6232 invisible text that is so either due to text properties or due to
6233 selective display. At the end, update IT's overlay information,
6234 face information etc. */
6235
6236 void
6237 reseat_at_previous_visible_line_start (struct it *it)
6238 {
6239 back_to_previous_visible_line_start (it);
6240 reseat (it, it->current.pos, 1);
6241 CHECK_IT (it);
6242 }
6243
6244
6245 /* Reseat iterator IT on the next visible line start in the current
6246 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6247 preceding the line start. Skip over invisible text that is so
6248 because of selective display. Compute faces, overlays etc at the
6249 new position. Note that this function does not skip over text that
6250 is invisible because of text properties. */
6251
6252 static void
6253 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6254 {
6255 int newline_found_p, skipped_p = 0;
6256 struct bidi_it bidi_it_prev;
6257
6258 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6259
6260 /* Skip over lines that are invisible because they are indented
6261 more than the value of IT->selective. */
6262 if (it->selective > 0)
6263 while (IT_CHARPOS (*it) < ZV
6264 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6265 it->selective))
6266 {
6267 eassert (IT_BYTEPOS (*it) == BEGV
6268 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6269 newline_found_p =
6270 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6271 }
6272
6273 /* Position on the newline if that's what's requested. */
6274 if (on_newline_p && newline_found_p)
6275 {
6276 if (STRINGP (it->string))
6277 {
6278 if (IT_STRING_CHARPOS (*it) > 0)
6279 {
6280 if (!it->bidi_p)
6281 {
6282 --IT_STRING_CHARPOS (*it);
6283 --IT_STRING_BYTEPOS (*it);
6284 }
6285 else
6286 {
6287 /* We need to restore the bidi iterator to the state
6288 it had on the newline, and resync the IT's
6289 position with that. */
6290 it->bidi_it = bidi_it_prev;
6291 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6292 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6293 }
6294 }
6295 }
6296 else if (IT_CHARPOS (*it) > BEGV)
6297 {
6298 if (!it->bidi_p)
6299 {
6300 --IT_CHARPOS (*it);
6301 --IT_BYTEPOS (*it);
6302 }
6303 else
6304 {
6305 /* We need to restore the bidi iterator to the state it
6306 had on the newline and resync IT with that. */
6307 it->bidi_it = bidi_it_prev;
6308 IT_CHARPOS (*it) = it->bidi_it.charpos;
6309 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6310 }
6311 reseat (it, it->current.pos, 0);
6312 }
6313 }
6314 else if (skipped_p)
6315 reseat (it, it->current.pos, 0);
6316
6317 CHECK_IT (it);
6318 }
6319
6320
6321 \f
6322 /***********************************************************************
6323 Changing an iterator's position
6324 ***********************************************************************/
6325
6326 /* Change IT's current position to POS in current_buffer. If FORCE_P
6327 is non-zero, always check for text properties at the new position.
6328 Otherwise, text properties are only looked up if POS >=
6329 IT->check_charpos of a property. */
6330
6331 static void
6332 reseat (struct it *it, struct text_pos pos, int force_p)
6333 {
6334 ptrdiff_t original_pos = IT_CHARPOS (*it);
6335
6336 reseat_1 (it, pos, 0);
6337
6338 /* Determine where to check text properties. Avoid doing it
6339 where possible because text property lookup is very expensive. */
6340 if (force_p
6341 || CHARPOS (pos) > it->stop_charpos
6342 || CHARPOS (pos) < original_pos)
6343 {
6344 if (it->bidi_p)
6345 {
6346 /* For bidi iteration, we need to prime prev_stop and
6347 base_level_stop with our best estimations. */
6348 /* Implementation note: Of course, POS is not necessarily a
6349 stop position, so assigning prev_pos to it is a lie; we
6350 should have called compute_stop_backwards. However, if
6351 the current buffer does not include any R2L characters,
6352 that call would be a waste of cycles, because the
6353 iterator will never move back, and thus never cross this
6354 "fake" stop position. So we delay that backward search
6355 until the time we really need it, in next_element_from_buffer. */
6356 if (CHARPOS (pos) != it->prev_stop)
6357 it->prev_stop = CHARPOS (pos);
6358 if (CHARPOS (pos) < it->base_level_stop)
6359 it->base_level_stop = 0; /* meaning it's unknown */
6360 handle_stop (it);
6361 }
6362 else
6363 {
6364 handle_stop (it);
6365 it->prev_stop = it->base_level_stop = 0;
6366 }
6367
6368 }
6369
6370 CHECK_IT (it);
6371 }
6372
6373
6374 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6375 IT->stop_pos to POS, also. */
6376
6377 static void
6378 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6379 {
6380 /* Don't call this function when scanning a C string. */
6381 eassert (it->s == NULL);
6382
6383 /* POS must be a reasonable value. */
6384 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6385
6386 it->current.pos = it->position = pos;
6387 it->end_charpos = ZV;
6388 it->dpvec = NULL;
6389 it->current.dpvec_index = -1;
6390 it->current.overlay_string_index = -1;
6391 IT_STRING_CHARPOS (*it) = -1;
6392 IT_STRING_BYTEPOS (*it) = -1;
6393 it->string = Qnil;
6394 it->method = GET_FROM_BUFFER;
6395 it->object = it->w->contents;
6396 it->area = TEXT_AREA;
6397 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6398 it->sp = 0;
6399 it->string_from_display_prop_p = 0;
6400 it->string_from_prefix_prop_p = 0;
6401
6402 it->from_disp_prop_p = 0;
6403 it->face_before_selective_p = 0;
6404 if (it->bidi_p)
6405 {
6406 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6407 &it->bidi_it);
6408 bidi_unshelve_cache (NULL, 0);
6409 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6410 it->bidi_it.string.s = NULL;
6411 it->bidi_it.string.lstring = Qnil;
6412 it->bidi_it.string.bufpos = 0;
6413 it->bidi_it.string.from_disp_str = 0;
6414 it->bidi_it.string.unibyte = 0;
6415 it->bidi_it.w = it->w;
6416 }
6417
6418 if (set_stop_p)
6419 {
6420 it->stop_charpos = CHARPOS (pos);
6421 it->base_level_stop = CHARPOS (pos);
6422 }
6423 /* This make the information stored in it->cmp_it invalidate. */
6424 it->cmp_it.id = -1;
6425 }
6426
6427
6428 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6429 If S is non-null, it is a C string to iterate over. Otherwise,
6430 STRING gives a Lisp string to iterate over.
6431
6432 If PRECISION > 0, don't return more then PRECISION number of
6433 characters from the string.
6434
6435 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6436 characters have been returned. FIELD_WIDTH < 0 means an infinite
6437 field width.
6438
6439 MULTIBYTE = 0 means disable processing of multibyte characters,
6440 MULTIBYTE > 0 means enable it,
6441 MULTIBYTE < 0 means use IT->multibyte_p.
6442
6443 IT must be initialized via a prior call to init_iterator before
6444 calling this function. */
6445
6446 static void
6447 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6448 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6449 int multibyte)
6450 {
6451 /* No text property checks performed by default, but see below. */
6452 it->stop_charpos = -1;
6453
6454 /* Set iterator position and end position. */
6455 memset (&it->current, 0, sizeof it->current);
6456 it->current.overlay_string_index = -1;
6457 it->current.dpvec_index = -1;
6458 eassert (charpos >= 0);
6459
6460 /* If STRING is specified, use its multibyteness, otherwise use the
6461 setting of MULTIBYTE, if specified. */
6462 if (multibyte >= 0)
6463 it->multibyte_p = multibyte > 0;
6464
6465 /* Bidirectional reordering of strings is controlled by the default
6466 value of bidi-display-reordering. Don't try to reorder while
6467 loading loadup.el, as the necessary character property tables are
6468 not yet available. */
6469 it->bidi_p =
6470 NILP (Vpurify_flag)
6471 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6472
6473 if (s == NULL)
6474 {
6475 eassert (STRINGP (string));
6476 it->string = string;
6477 it->s = NULL;
6478 it->end_charpos = it->string_nchars = SCHARS (string);
6479 it->method = GET_FROM_STRING;
6480 it->current.string_pos = string_pos (charpos, string);
6481
6482 if (it->bidi_p)
6483 {
6484 it->bidi_it.string.lstring = string;
6485 it->bidi_it.string.s = NULL;
6486 it->bidi_it.string.schars = it->end_charpos;
6487 it->bidi_it.string.bufpos = 0;
6488 it->bidi_it.string.from_disp_str = 0;
6489 it->bidi_it.string.unibyte = !it->multibyte_p;
6490 it->bidi_it.w = it->w;
6491 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6492 FRAME_WINDOW_P (it->f), &it->bidi_it);
6493 }
6494 }
6495 else
6496 {
6497 it->s = (const unsigned char *) s;
6498 it->string = Qnil;
6499
6500 /* Note that we use IT->current.pos, not it->current.string_pos,
6501 for displaying C strings. */
6502 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6503 if (it->multibyte_p)
6504 {
6505 it->current.pos = c_string_pos (charpos, s, 1);
6506 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6507 }
6508 else
6509 {
6510 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6511 it->end_charpos = it->string_nchars = strlen (s);
6512 }
6513
6514 if (it->bidi_p)
6515 {
6516 it->bidi_it.string.lstring = Qnil;
6517 it->bidi_it.string.s = (const unsigned char *) s;
6518 it->bidi_it.string.schars = it->end_charpos;
6519 it->bidi_it.string.bufpos = 0;
6520 it->bidi_it.string.from_disp_str = 0;
6521 it->bidi_it.string.unibyte = !it->multibyte_p;
6522 it->bidi_it.w = it->w;
6523 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6524 &it->bidi_it);
6525 }
6526 it->method = GET_FROM_C_STRING;
6527 }
6528
6529 /* PRECISION > 0 means don't return more than PRECISION characters
6530 from the string. */
6531 if (precision > 0 && it->end_charpos - charpos > precision)
6532 {
6533 it->end_charpos = it->string_nchars = charpos + precision;
6534 if (it->bidi_p)
6535 it->bidi_it.string.schars = it->end_charpos;
6536 }
6537
6538 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6539 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6540 FIELD_WIDTH < 0 means infinite field width. This is useful for
6541 padding with `-' at the end of a mode line. */
6542 if (field_width < 0)
6543 field_width = INFINITY;
6544 /* Implementation note: We deliberately don't enlarge
6545 it->bidi_it.string.schars here to fit it->end_charpos, because
6546 the bidi iterator cannot produce characters out of thin air. */
6547 if (field_width > it->end_charpos - charpos)
6548 it->end_charpos = charpos + field_width;
6549
6550 /* Use the standard display table for displaying strings. */
6551 if (DISP_TABLE_P (Vstandard_display_table))
6552 it->dp = XCHAR_TABLE (Vstandard_display_table);
6553
6554 it->stop_charpos = charpos;
6555 it->prev_stop = charpos;
6556 it->base_level_stop = 0;
6557 if (it->bidi_p)
6558 {
6559 it->bidi_it.first_elt = 1;
6560 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6561 it->bidi_it.disp_pos = -1;
6562 }
6563 if (s == NULL && it->multibyte_p)
6564 {
6565 ptrdiff_t endpos = SCHARS (it->string);
6566 if (endpos > it->end_charpos)
6567 endpos = it->end_charpos;
6568 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6569 it->string);
6570 }
6571 CHECK_IT (it);
6572 }
6573
6574
6575 \f
6576 /***********************************************************************
6577 Iteration
6578 ***********************************************************************/
6579
6580 /* Map enum it_method value to corresponding next_element_from_* function. */
6581
6582 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6583 {
6584 next_element_from_buffer,
6585 next_element_from_display_vector,
6586 next_element_from_string,
6587 next_element_from_c_string,
6588 next_element_from_image,
6589 next_element_from_stretch
6590 };
6591
6592 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6593
6594
6595 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6596 (possibly with the following characters). */
6597
6598 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6599 ((IT)->cmp_it.id >= 0 \
6600 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6601 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6602 END_CHARPOS, (IT)->w, \
6603 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6604 (IT)->string)))
6605
6606
6607 /* Lookup the char-table Vglyphless_char_display for character C (-1
6608 if we want information for no-font case), and return the display
6609 method symbol. By side-effect, update it->what and
6610 it->glyphless_method. This function is called from
6611 get_next_display_element for each character element, and from
6612 x_produce_glyphs when no suitable font was found. */
6613
6614 Lisp_Object
6615 lookup_glyphless_char_display (int c, struct it *it)
6616 {
6617 Lisp_Object glyphless_method = Qnil;
6618
6619 if (CHAR_TABLE_P (Vglyphless_char_display)
6620 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6621 {
6622 if (c >= 0)
6623 {
6624 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6625 if (CONSP (glyphless_method))
6626 glyphless_method = FRAME_WINDOW_P (it->f)
6627 ? XCAR (glyphless_method)
6628 : XCDR (glyphless_method);
6629 }
6630 else
6631 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6632 }
6633
6634 retry:
6635 if (NILP (glyphless_method))
6636 {
6637 if (c >= 0)
6638 /* The default is to display the character by a proper font. */
6639 return Qnil;
6640 /* The default for the no-font case is to display an empty box. */
6641 glyphless_method = Qempty_box;
6642 }
6643 if (EQ (glyphless_method, Qzero_width))
6644 {
6645 if (c >= 0)
6646 return glyphless_method;
6647 /* This method can't be used for the no-font case. */
6648 glyphless_method = Qempty_box;
6649 }
6650 if (EQ (glyphless_method, Qthin_space))
6651 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6652 else if (EQ (glyphless_method, Qempty_box))
6653 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6654 else if (EQ (glyphless_method, Qhex_code))
6655 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6656 else if (STRINGP (glyphless_method))
6657 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6658 else
6659 {
6660 /* Invalid value. We use the default method. */
6661 glyphless_method = Qnil;
6662 goto retry;
6663 }
6664 it->what = IT_GLYPHLESS;
6665 return glyphless_method;
6666 }
6667
6668 /* Merge escape glyph face and cache the result. */
6669
6670 static struct frame *last_escape_glyph_frame = NULL;
6671 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6672 static int last_escape_glyph_merged_face_id = 0;
6673
6674 static int
6675 merge_escape_glyph_face (struct it *it)
6676 {
6677 int face_id;
6678
6679 if (it->f == last_escape_glyph_frame
6680 && it->face_id == last_escape_glyph_face_id)
6681 face_id = last_escape_glyph_merged_face_id;
6682 else
6683 {
6684 /* Merge the `escape-glyph' face into the current face. */
6685 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6686 last_escape_glyph_frame = it->f;
6687 last_escape_glyph_face_id = it->face_id;
6688 last_escape_glyph_merged_face_id = face_id;
6689 }
6690 return face_id;
6691 }
6692
6693 /* Likewise for glyphless glyph face. */
6694
6695 static struct frame *last_glyphless_glyph_frame = NULL;
6696 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6697 static int last_glyphless_glyph_merged_face_id = 0;
6698
6699 int
6700 merge_glyphless_glyph_face (struct it *it)
6701 {
6702 int face_id;
6703
6704 if (it->f == last_glyphless_glyph_frame
6705 && it->face_id == last_glyphless_glyph_face_id)
6706 face_id = last_glyphless_glyph_merged_face_id;
6707 else
6708 {
6709 /* Merge the `glyphless-char' face into the current face. */
6710 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6711 last_glyphless_glyph_frame = it->f;
6712 last_glyphless_glyph_face_id = it->face_id;
6713 last_glyphless_glyph_merged_face_id = face_id;
6714 }
6715 return face_id;
6716 }
6717
6718 /* Load IT's display element fields with information about the next
6719 display element from the current position of IT. Value is zero if
6720 end of buffer (or C string) is reached. */
6721
6722 static int
6723 get_next_display_element (struct it *it)
6724 {
6725 /* Non-zero means that we found a display element. Zero means that
6726 we hit the end of what we iterate over. Performance note: the
6727 function pointer `method' used here turns out to be faster than
6728 using a sequence of if-statements. */
6729 int success_p;
6730
6731 get_next:
6732 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6733
6734 if (it->what == IT_CHARACTER)
6735 {
6736 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6737 and only if (a) the resolved directionality of that character
6738 is R..." */
6739 /* FIXME: Do we need an exception for characters from display
6740 tables? */
6741 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6742 it->c = bidi_mirror_char (it->c);
6743 /* Map via display table or translate control characters.
6744 IT->c, IT->len etc. have been set to the next character by
6745 the function call above. If we have a display table, and it
6746 contains an entry for IT->c, translate it. Don't do this if
6747 IT->c itself comes from a display table, otherwise we could
6748 end up in an infinite recursion. (An alternative could be to
6749 count the recursion depth of this function and signal an
6750 error when a certain maximum depth is reached.) Is it worth
6751 it? */
6752 if (success_p && it->dpvec == NULL)
6753 {
6754 Lisp_Object dv;
6755 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6756 int nonascii_space_p = 0;
6757 int nonascii_hyphen_p = 0;
6758 int c = it->c; /* This is the character to display. */
6759
6760 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6761 {
6762 eassert (SINGLE_BYTE_CHAR_P (c));
6763 if (unibyte_display_via_language_environment)
6764 {
6765 c = DECODE_CHAR (unibyte, c);
6766 if (c < 0)
6767 c = BYTE8_TO_CHAR (it->c);
6768 }
6769 else
6770 c = BYTE8_TO_CHAR (it->c);
6771 }
6772
6773 if (it->dp
6774 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6775 VECTORP (dv)))
6776 {
6777 struct Lisp_Vector *v = XVECTOR (dv);
6778
6779 /* Return the first character from the display table
6780 entry, if not empty. If empty, don't display the
6781 current character. */
6782 if (v->header.size)
6783 {
6784 it->dpvec_char_len = it->len;
6785 it->dpvec = v->contents;
6786 it->dpend = v->contents + v->header.size;
6787 it->current.dpvec_index = 0;
6788 it->dpvec_face_id = -1;
6789 it->saved_face_id = it->face_id;
6790 it->method = GET_FROM_DISPLAY_VECTOR;
6791 it->ellipsis_p = 0;
6792 }
6793 else
6794 {
6795 set_iterator_to_next (it, 0);
6796 }
6797 goto get_next;
6798 }
6799
6800 if (! NILP (lookup_glyphless_char_display (c, it)))
6801 {
6802 if (it->what == IT_GLYPHLESS)
6803 goto done;
6804 /* Don't display this character. */
6805 set_iterator_to_next (it, 0);
6806 goto get_next;
6807 }
6808
6809 /* If `nobreak-char-display' is non-nil, we display
6810 non-ASCII spaces and hyphens specially. */
6811 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6812 {
6813 if (c == 0xA0)
6814 nonascii_space_p = true;
6815 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6816 nonascii_hyphen_p = true;
6817 }
6818
6819 /* Translate control characters into `\003' or `^C' form.
6820 Control characters coming from a display table entry are
6821 currently not translated because we use IT->dpvec to hold
6822 the translation. This could easily be changed but I
6823 don't believe that it is worth doing.
6824
6825 The characters handled by `nobreak-char-display' must be
6826 translated too.
6827
6828 Non-printable characters and raw-byte characters are also
6829 translated to octal form. */
6830 if (((c < ' ' || c == 127) /* ASCII control chars. */
6831 ? (it->area != TEXT_AREA
6832 /* In mode line, treat \n, \t like other crl chars. */
6833 || (c != '\t'
6834 && it->glyph_row
6835 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6836 || (c != '\n' && c != '\t'))
6837 : (nonascii_space_p
6838 || nonascii_hyphen_p
6839 || CHAR_BYTE8_P (c)
6840 || ! CHAR_PRINTABLE_P (c))))
6841 {
6842 /* C is a control character, non-ASCII space/hyphen,
6843 raw-byte, or a non-printable character which must be
6844 displayed either as '\003' or as `^C' where the '\\'
6845 and '^' can be defined in the display table. Fill
6846 IT->ctl_chars with glyphs for what we have to
6847 display. Then, set IT->dpvec to these glyphs. */
6848 Lisp_Object gc;
6849 int ctl_len;
6850 int face_id;
6851 int lface_id = 0;
6852 int escape_glyph;
6853
6854 /* Handle control characters with ^. */
6855
6856 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6857 {
6858 int g;
6859
6860 g = '^'; /* default glyph for Control */
6861 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6862 if (it->dp
6863 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6864 {
6865 g = GLYPH_CODE_CHAR (gc);
6866 lface_id = GLYPH_CODE_FACE (gc);
6867 }
6868
6869 face_id = (lface_id
6870 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6871 : merge_escape_glyph_face (it));
6872
6873 XSETINT (it->ctl_chars[0], g);
6874 XSETINT (it->ctl_chars[1], c ^ 0100);
6875 ctl_len = 2;
6876 goto display_control;
6877 }
6878
6879 /* Handle non-ascii space in the mode where it only gets
6880 highlighting. */
6881
6882 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6883 {
6884 /* Merge `nobreak-space' into the current face. */
6885 face_id = merge_faces (it->f, Qnobreak_space, 0,
6886 it->face_id);
6887 XSETINT (it->ctl_chars[0], ' ');
6888 ctl_len = 1;
6889 goto display_control;
6890 }
6891
6892 /* Handle sequences that start with the "escape glyph". */
6893
6894 /* the default escape glyph is \. */
6895 escape_glyph = '\\';
6896
6897 if (it->dp
6898 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6899 {
6900 escape_glyph = GLYPH_CODE_CHAR (gc);
6901 lface_id = GLYPH_CODE_FACE (gc);
6902 }
6903
6904 face_id = (lface_id
6905 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6906 : merge_escape_glyph_face (it));
6907
6908 /* Draw non-ASCII hyphen with just highlighting: */
6909
6910 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6911 {
6912 XSETINT (it->ctl_chars[0], '-');
6913 ctl_len = 1;
6914 goto display_control;
6915 }
6916
6917 /* Draw non-ASCII space/hyphen with escape glyph: */
6918
6919 if (nonascii_space_p || nonascii_hyphen_p)
6920 {
6921 XSETINT (it->ctl_chars[0], escape_glyph);
6922 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6923 ctl_len = 2;
6924 goto display_control;
6925 }
6926
6927 {
6928 char str[10];
6929 int len, i;
6930
6931 if (CHAR_BYTE8_P (c))
6932 /* Display \200 instead of \17777600. */
6933 c = CHAR_TO_BYTE8 (c);
6934 len = sprintf (str, "%03o", c);
6935
6936 XSETINT (it->ctl_chars[0], escape_glyph);
6937 for (i = 0; i < len; i++)
6938 XSETINT (it->ctl_chars[i + 1], str[i]);
6939 ctl_len = len + 1;
6940 }
6941
6942 display_control:
6943 /* Set up IT->dpvec and return first character from it. */
6944 it->dpvec_char_len = it->len;
6945 it->dpvec = it->ctl_chars;
6946 it->dpend = it->dpvec + ctl_len;
6947 it->current.dpvec_index = 0;
6948 it->dpvec_face_id = face_id;
6949 it->saved_face_id = it->face_id;
6950 it->method = GET_FROM_DISPLAY_VECTOR;
6951 it->ellipsis_p = 0;
6952 goto get_next;
6953 }
6954 it->char_to_display = c;
6955 }
6956 else if (success_p)
6957 {
6958 it->char_to_display = it->c;
6959 }
6960 }
6961
6962 #ifdef HAVE_WINDOW_SYSTEM
6963 /* Adjust face id for a multibyte character. There are no multibyte
6964 character in unibyte text. */
6965 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6966 && it->multibyte_p
6967 && success_p
6968 && FRAME_WINDOW_P (it->f))
6969 {
6970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6971
6972 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6973 {
6974 /* Automatic composition with glyph-string. */
6975 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6976
6977 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6978 }
6979 else
6980 {
6981 ptrdiff_t pos = (it->s ? -1
6982 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6983 : IT_CHARPOS (*it));
6984 int c;
6985
6986 if (it->what == IT_CHARACTER)
6987 c = it->char_to_display;
6988 else
6989 {
6990 struct composition *cmp = composition_table[it->cmp_it.id];
6991 int i;
6992
6993 c = ' ';
6994 for (i = 0; i < cmp->glyph_len; i++)
6995 /* TAB in a composition means display glyphs with
6996 padding space on the left or right. */
6997 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6998 break;
6999 }
7000 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7001 }
7002 }
7003 #endif /* HAVE_WINDOW_SYSTEM */
7004
7005 done:
7006 /* Is this character the last one of a run of characters with
7007 box? If yes, set IT->end_of_box_run_p to 1. */
7008 if (it->face_box_p
7009 && it->s == NULL)
7010 {
7011 if (it->method == GET_FROM_STRING && it->sp)
7012 {
7013 int face_id = underlying_face_id (it);
7014 struct face *face = FACE_FROM_ID (it->f, face_id);
7015
7016 if (face)
7017 {
7018 if (face->box == FACE_NO_BOX)
7019 {
7020 /* If the box comes from face properties in a
7021 display string, check faces in that string. */
7022 int string_face_id = face_after_it_pos (it);
7023 it->end_of_box_run_p
7024 = (FACE_FROM_ID (it->f, string_face_id)->box
7025 == FACE_NO_BOX);
7026 }
7027 /* Otherwise, the box comes from the underlying face.
7028 If this is the last string character displayed, check
7029 the next buffer location. */
7030 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7031 && (it->current.overlay_string_index
7032 == it->n_overlay_strings - 1))
7033 {
7034 ptrdiff_t ignore;
7035 int next_face_id;
7036 struct text_pos pos = it->current.pos;
7037 INC_TEXT_POS (pos, it->multibyte_p);
7038
7039 next_face_id = face_at_buffer_position
7040 (it->w, CHARPOS (pos), &ignore,
7041 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7042 -1);
7043 it->end_of_box_run_p
7044 = (FACE_FROM_ID (it->f, next_face_id)->box
7045 == FACE_NO_BOX);
7046 }
7047 }
7048 }
7049 /* next_element_from_display_vector sets this flag according to
7050 faces of the display vector glyphs, see there. */
7051 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7052 {
7053 int face_id = face_after_it_pos (it);
7054 it->end_of_box_run_p
7055 = (face_id != it->face_id
7056 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7057 }
7058 }
7059 /* If we reached the end of the object we've been iterating (e.g., a
7060 display string or an overlay string), and there's something on
7061 IT->stack, proceed with what's on the stack. It doesn't make
7062 sense to return zero if there's unprocessed stuff on the stack,
7063 because otherwise that stuff will never be displayed. */
7064 if (!success_p && it->sp > 0)
7065 {
7066 set_iterator_to_next (it, 0);
7067 success_p = get_next_display_element (it);
7068 }
7069
7070 /* Value is 0 if end of buffer or string reached. */
7071 return success_p;
7072 }
7073
7074
7075 /* Move IT to the next display element.
7076
7077 RESEAT_P non-zero means if called on a newline in buffer text,
7078 skip to the next visible line start.
7079
7080 Functions get_next_display_element and set_iterator_to_next are
7081 separate because I find this arrangement easier to handle than a
7082 get_next_display_element function that also increments IT's
7083 position. The way it is we can first look at an iterator's current
7084 display element, decide whether it fits on a line, and if it does,
7085 increment the iterator position. The other way around we probably
7086 would either need a flag indicating whether the iterator has to be
7087 incremented the next time, or we would have to implement a
7088 decrement position function which would not be easy to write. */
7089
7090 void
7091 set_iterator_to_next (struct it *it, int reseat_p)
7092 {
7093 /* Reset flags indicating start and end of a sequence of characters
7094 with box. Reset them at the start of this function because
7095 moving the iterator to a new position might set them. */
7096 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7097
7098 switch (it->method)
7099 {
7100 case GET_FROM_BUFFER:
7101 /* The current display element of IT is a character from
7102 current_buffer. Advance in the buffer, and maybe skip over
7103 invisible lines that are so because of selective display. */
7104 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7105 reseat_at_next_visible_line_start (it, 0);
7106 else if (it->cmp_it.id >= 0)
7107 {
7108 /* We are currently getting glyphs from a composition. */
7109 int i;
7110
7111 if (! it->bidi_p)
7112 {
7113 IT_CHARPOS (*it) += it->cmp_it.nchars;
7114 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7115 if (it->cmp_it.to < it->cmp_it.nglyphs)
7116 {
7117 it->cmp_it.from = it->cmp_it.to;
7118 }
7119 else
7120 {
7121 it->cmp_it.id = -1;
7122 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7123 IT_BYTEPOS (*it),
7124 it->end_charpos, Qnil);
7125 }
7126 }
7127 else if (! it->cmp_it.reversed_p)
7128 {
7129 /* Composition created while scanning forward. */
7130 /* Update IT's char/byte positions to point to the first
7131 character of the next grapheme cluster, or to the
7132 character visually after the current composition. */
7133 for (i = 0; i < it->cmp_it.nchars; i++)
7134 bidi_move_to_visually_next (&it->bidi_it);
7135 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7136 IT_CHARPOS (*it) = it->bidi_it.charpos;
7137
7138 if (it->cmp_it.to < it->cmp_it.nglyphs)
7139 {
7140 /* Proceed to the next grapheme cluster. */
7141 it->cmp_it.from = it->cmp_it.to;
7142 }
7143 else
7144 {
7145 /* No more grapheme clusters in this composition.
7146 Find the next stop position. */
7147 ptrdiff_t stop = it->end_charpos;
7148 if (it->bidi_it.scan_dir < 0)
7149 /* Now we are scanning backward and don't know
7150 where to stop. */
7151 stop = -1;
7152 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7153 IT_BYTEPOS (*it), stop, Qnil);
7154 }
7155 }
7156 else
7157 {
7158 /* Composition created while scanning backward. */
7159 /* Update IT's char/byte positions to point to the last
7160 character of the previous grapheme cluster, or the
7161 character visually after the current composition. */
7162 for (i = 0; i < it->cmp_it.nchars; i++)
7163 bidi_move_to_visually_next (&it->bidi_it);
7164 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7165 IT_CHARPOS (*it) = it->bidi_it.charpos;
7166 if (it->cmp_it.from > 0)
7167 {
7168 /* Proceed to the previous grapheme cluster. */
7169 it->cmp_it.to = it->cmp_it.from;
7170 }
7171 else
7172 {
7173 /* No more grapheme clusters in this composition.
7174 Find the next stop position. */
7175 ptrdiff_t stop = it->end_charpos;
7176 if (it->bidi_it.scan_dir < 0)
7177 /* Now we are scanning backward and don't know
7178 where to stop. */
7179 stop = -1;
7180 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7181 IT_BYTEPOS (*it), stop, Qnil);
7182 }
7183 }
7184 }
7185 else
7186 {
7187 eassert (it->len != 0);
7188
7189 if (!it->bidi_p)
7190 {
7191 IT_BYTEPOS (*it) += it->len;
7192 IT_CHARPOS (*it) += 1;
7193 }
7194 else
7195 {
7196 int prev_scan_dir = it->bidi_it.scan_dir;
7197 /* If this is a new paragraph, determine its base
7198 direction (a.k.a. its base embedding level). */
7199 if (it->bidi_it.new_paragraph)
7200 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7201 bidi_move_to_visually_next (&it->bidi_it);
7202 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7203 IT_CHARPOS (*it) = it->bidi_it.charpos;
7204 if (prev_scan_dir != it->bidi_it.scan_dir)
7205 {
7206 /* As the scan direction was changed, we must
7207 re-compute the stop position for composition. */
7208 ptrdiff_t stop = it->end_charpos;
7209 if (it->bidi_it.scan_dir < 0)
7210 stop = -1;
7211 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7212 IT_BYTEPOS (*it), stop, Qnil);
7213 }
7214 }
7215 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7216 }
7217 break;
7218
7219 case GET_FROM_C_STRING:
7220 /* Current display element of IT is from a C string. */
7221 if (!it->bidi_p
7222 /* If the string position is beyond string's end, it means
7223 next_element_from_c_string is padding the string with
7224 blanks, in which case we bypass the bidi iterator,
7225 because it cannot deal with such virtual characters. */
7226 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7227 {
7228 IT_BYTEPOS (*it) += it->len;
7229 IT_CHARPOS (*it) += 1;
7230 }
7231 else
7232 {
7233 bidi_move_to_visually_next (&it->bidi_it);
7234 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7235 IT_CHARPOS (*it) = it->bidi_it.charpos;
7236 }
7237 break;
7238
7239 case GET_FROM_DISPLAY_VECTOR:
7240 /* Current display element of IT is from a display table entry.
7241 Advance in the display table definition. Reset it to null if
7242 end reached, and continue with characters from buffers/
7243 strings. */
7244 ++it->current.dpvec_index;
7245
7246 /* Restore face of the iterator to what they were before the
7247 display vector entry (these entries may contain faces). */
7248 it->face_id = it->saved_face_id;
7249
7250 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7251 {
7252 int recheck_faces = it->ellipsis_p;
7253
7254 if (it->s)
7255 it->method = GET_FROM_C_STRING;
7256 else if (STRINGP (it->string))
7257 it->method = GET_FROM_STRING;
7258 else
7259 {
7260 it->method = GET_FROM_BUFFER;
7261 it->object = it->w->contents;
7262 }
7263
7264 it->dpvec = NULL;
7265 it->current.dpvec_index = -1;
7266
7267 /* Skip over characters which were displayed via IT->dpvec. */
7268 if (it->dpvec_char_len < 0)
7269 reseat_at_next_visible_line_start (it, 1);
7270 else if (it->dpvec_char_len > 0)
7271 {
7272 if (it->method == GET_FROM_STRING
7273 && it->current.overlay_string_index >= 0
7274 && it->n_overlay_strings > 0)
7275 it->ignore_overlay_strings_at_pos_p = true;
7276 it->len = it->dpvec_char_len;
7277 set_iterator_to_next (it, reseat_p);
7278 }
7279
7280 /* Maybe recheck faces after display vector. */
7281 if (recheck_faces)
7282 it->stop_charpos = IT_CHARPOS (*it);
7283 }
7284 break;
7285
7286 case GET_FROM_STRING:
7287 /* Current display element is a character from a Lisp string. */
7288 eassert (it->s == NULL && STRINGP (it->string));
7289 /* Don't advance past string end. These conditions are true
7290 when set_iterator_to_next is called at the end of
7291 get_next_display_element, in which case the Lisp string is
7292 already exhausted, and all we want is pop the iterator
7293 stack. */
7294 if (it->current.overlay_string_index >= 0)
7295 {
7296 /* This is an overlay string, so there's no padding with
7297 spaces, and the number of characters in the string is
7298 where the string ends. */
7299 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7300 goto consider_string_end;
7301 }
7302 else
7303 {
7304 /* Not an overlay string. There could be padding, so test
7305 against it->end_charpos. */
7306 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7307 goto consider_string_end;
7308 }
7309 if (it->cmp_it.id >= 0)
7310 {
7311 int i;
7312
7313 if (! it->bidi_p)
7314 {
7315 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7316 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7317 if (it->cmp_it.to < it->cmp_it.nglyphs)
7318 it->cmp_it.from = it->cmp_it.to;
7319 else
7320 {
7321 it->cmp_it.id = -1;
7322 composition_compute_stop_pos (&it->cmp_it,
7323 IT_STRING_CHARPOS (*it),
7324 IT_STRING_BYTEPOS (*it),
7325 it->end_charpos, it->string);
7326 }
7327 }
7328 else if (! it->cmp_it.reversed_p)
7329 {
7330 for (i = 0; i < it->cmp_it.nchars; i++)
7331 bidi_move_to_visually_next (&it->bidi_it);
7332 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7333 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7334
7335 if (it->cmp_it.to < it->cmp_it.nglyphs)
7336 it->cmp_it.from = it->cmp_it.to;
7337 else
7338 {
7339 ptrdiff_t stop = it->end_charpos;
7340 if (it->bidi_it.scan_dir < 0)
7341 stop = -1;
7342 composition_compute_stop_pos (&it->cmp_it,
7343 IT_STRING_CHARPOS (*it),
7344 IT_STRING_BYTEPOS (*it), stop,
7345 it->string);
7346 }
7347 }
7348 else
7349 {
7350 for (i = 0; i < it->cmp_it.nchars; i++)
7351 bidi_move_to_visually_next (&it->bidi_it);
7352 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7353 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7354 if (it->cmp_it.from > 0)
7355 it->cmp_it.to = it->cmp_it.from;
7356 else
7357 {
7358 ptrdiff_t stop = it->end_charpos;
7359 if (it->bidi_it.scan_dir < 0)
7360 stop = -1;
7361 composition_compute_stop_pos (&it->cmp_it,
7362 IT_STRING_CHARPOS (*it),
7363 IT_STRING_BYTEPOS (*it), stop,
7364 it->string);
7365 }
7366 }
7367 }
7368 else
7369 {
7370 if (!it->bidi_p
7371 /* If the string position is beyond string's end, it
7372 means next_element_from_string is padding the string
7373 with blanks, in which case we bypass the bidi
7374 iterator, because it cannot deal with such virtual
7375 characters. */
7376 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7377 {
7378 IT_STRING_BYTEPOS (*it) += it->len;
7379 IT_STRING_CHARPOS (*it) += 1;
7380 }
7381 else
7382 {
7383 int prev_scan_dir = it->bidi_it.scan_dir;
7384
7385 bidi_move_to_visually_next (&it->bidi_it);
7386 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7387 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7388 if (prev_scan_dir != it->bidi_it.scan_dir)
7389 {
7390 ptrdiff_t stop = it->end_charpos;
7391
7392 if (it->bidi_it.scan_dir < 0)
7393 stop = -1;
7394 composition_compute_stop_pos (&it->cmp_it,
7395 IT_STRING_CHARPOS (*it),
7396 IT_STRING_BYTEPOS (*it), stop,
7397 it->string);
7398 }
7399 }
7400 }
7401
7402 consider_string_end:
7403
7404 if (it->current.overlay_string_index >= 0)
7405 {
7406 /* IT->string is an overlay string. Advance to the
7407 next, if there is one. */
7408 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7409 {
7410 it->ellipsis_p = 0;
7411 next_overlay_string (it);
7412 if (it->ellipsis_p)
7413 setup_for_ellipsis (it, 0);
7414 }
7415 }
7416 else
7417 {
7418 /* IT->string is not an overlay string. If we reached
7419 its end, and there is something on IT->stack, proceed
7420 with what is on the stack. This can be either another
7421 string, this time an overlay string, or a buffer. */
7422 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7423 && it->sp > 0)
7424 {
7425 pop_it (it);
7426 if (it->method == GET_FROM_STRING)
7427 goto consider_string_end;
7428 }
7429 }
7430 break;
7431
7432 case GET_FROM_IMAGE:
7433 case GET_FROM_STRETCH:
7434 /* The position etc with which we have to proceed are on
7435 the stack. The position may be at the end of a string,
7436 if the `display' property takes up the whole string. */
7437 eassert (it->sp > 0);
7438 pop_it (it);
7439 if (it->method == GET_FROM_STRING)
7440 goto consider_string_end;
7441 break;
7442
7443 default:
7444 /* There are no other methods defined, so this should be a bug. */
7445 emacs_abort ();
7446 }
7447
7448 eassert (it->method != GET_FROM_STRING
7449 || (STRINGP (it->string)
7450 && IT_STRING_CHARPOS (*it) >= 0));
7451 }
7452
7453 /* Load IT's display element fields with information about the next
7454 display element which comes from a display table entry or from the
7455 result of translating a control character to one of the forms `^C'
7456 or `\003'.
7457
7458 IT->dpvec holds the glyphs to return as characters.
7459 IT->saved_face_id holds the face id before the display vector--it
7460 is restored into IT->face_id in set_iterator_to_next. */
7461
7462 static int
7463 next_element_from_display_vector (struct it *it)
7464 {
7465 Lisp_Object gc;
7466 int prev_face_id = it->face_id;
7467 int next_face_id;
7468
7469 /* Precondition. */
7470 eassert (it->dpvec && it->current.dpvec_index >= 0);
7471
7472 it->face_id = it->saved_face_id;
7473
7474 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7475 That seemed totally bogus - so I changed it... */
7476 gc = it->dpvec[it->current.dpvec_index];
7477
7478 if (GLYPH_CODE_P (gc))
7479 {
7480 struct face *this_face, *prev_face, *next_face;
7481
7482 it->c = GLYPH_CODE_CHAR (gc);
7483 it->len = CHAR_BYTES (it->c);
7484
7485 /* The entry may contain a face id to use. Such a face id is
7486 the id of a Lisp face, not a realized face. A face id of
7487 zero means no face is specified. */
7488 if (it->dpvec_face_id >= 0)
7489 it->face_id = it->dpvec_face_id;
7490 else
7491 {
7492 int lface_id = GLYPH_CODE_FACE (gc);
7493 if (lface_id > 0)
7494 it->face_id = merge_faces (it->f, Qt, lface_id,
7495 it->saved_face_id);
7496 }
7497
7498 /* Glyphs in the display vector could have the box face, so we
7499 need to set the related flags in the iterator, as
7500 appropriate. */
7501 this_face = FACE_FROM_ID (it->f, it->face_id);
7502 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7503
7504 /* Is this character the first character of a box-face run? */
7505 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7506 && (!prev_face
7507 || prev_face->box == FACE_NO_BOX));
7508
7509 /* For the last character of the box-face run, we need to look
7510 either at the next glyph from the display vector, or at the
7511 face we saw before the display vector. */
7512 next_face_id = it->saved_face_id;
7513 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7514 {
7515 if (it->dpvec_face_id >= 0)
7516 next_face_id = it->dpvec_face_id;
7517 else
7518 {
7519 int lface_id =
7520 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7521
7522 if (lface_id > 0)
7523 next_face_id = merge_faces (it->f, Qt, lface_id,
7524 it->saved_face_id);
7525 }
7526 }
7527 next_face = FACE_FROM_ID (it->f, next_face_id);
7528 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7529 && (!next_face
7530 || next_face->box == FACE_NO_BOX));
7531 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7532 }
7533 else
7534 /* Display table entry is invalid. Return a space. */
7535 it->c = ' ', it->len = 1;
7536
7537 /* Don't change position and object of the iterator here. They are
7538 still the values of the character that had this display table
7539 entry or was translated, and that's what we want. */
7540 it->what = IT_CHARACTER;
7541 return 1;
7542 }
7543
7544 /* Get the first element of string/buffer in the visual order, after
7545 being reseated to a new position in a string or a buffer. */
7546 static void
7547 get_visually_first_element (struct it *it)
7548 {
7549 int string_p = STRINGP (it->string) || it->s;
7550 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7551 ptrdiff_t bob = (string_p ? 0 : BEGV);
7552
7553 if (STRINGP (it->string))
7554 {
7555 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7556 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7557 }
7558 else
7559 {
7560 it->bidi_it.charpos = IT_CHARPOS (*it);
7561 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7562 }
7563
7564 if (it->bidi_it.charpos == eob)
7565 {
7566 /* Nothing to do, but reset the FIRST_ELT flag, like
7567 bidi_paragraph_init does, because we are not going to
7568 call it. */
7569 it->bidi_it.first_elt = 0;
7570 }
7571 else if (it->bidi_it.charpos == bob
7572 || (!string_p
7573 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7574 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7575 {
7576 /* If we are at the beginning of a line/string, we can produce
7577 the next element right away. */
7578 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7579 bidi_move_to_visually_next (&it->bidi_it);
7580 }
7581 else
7582 {
7583 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7584
7585 /* We need to prime the bidi iterator starting at the line's or
7586 string's beginning, before we will be able to produce the
7587 next element. */
7588 if (string_p)
7589 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7590 else
7591 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7592 IT_BYTEPOS (*it), -1,
7593 &it->bidi_it.bytepos);
7594 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7595 do
7596 {
7597 /* Now return to buffer/string position where we were asked
7598 to get the next display element, and produce that. */
7599 bidi_move_to_visually_next (&it->bidi_it);
7600 }
7601 while (it->bidi_it.bytepos != orig_bytepos
7602 && it->bidi_it.charpos < eob);
7603 }
7604
7605 /* Adjust IT's position information to where we ended up. */
7606 if (STRINGP (it->string))
7607 {
7608 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7609 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7610 }
7611 else
7612 {
7613 IT_CHARPOS (*it) = it->bidi_it.charpos;
7614 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7615 }
7616
7617 if (STRINGP (it->string) || !it->s)
7618 {
7619 ptrdiff_t stop, charpos, bytepos;
7620
7621 if (STRINGP (it->string))
7622 {
7623 eassert (!it->s);
7624 stop = SCHARS (it->string);
7625 if (stop > it->end_charpos)
7626 stop = it->end_charpos;
7627 charpos = IT_STRING_CHARPOS (*it);
7628 bytepos = IT_STRING_BYTEPOS (*it);
7629 }
7630 else
7631 {
7632 stop = it->end_charpos;
7633 charpos = IT_CHARPOS (*it);
7634 bytepos = IT_BYTEPOS (*it);
7635 }
7636 if (it->bidi_it.scan_dir < 0)
7637 stop = -1;
7638 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7639 it->string);
7640 }
7641 }
7642
7643 /* Load IT with the next display element from Lisp string IT->string.
7644 IT->current.string_pos is the current position within the string.
7645 If IT->current.overlay_string_index >= 0, the Lisp string is an
7646 overlay string. */
7647
7648 static int
7649 next_element_from_string (struct it *it)
7650 {
7651 struct text_pos position;
7652
7653 eassert (STRINGP (it->string));
7654 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7655 eassert (IT_STRING_CHARPOS (*it) >= 0);
7656 position = it->current.string_pos;
7657
7658 /* With bidi reordering, the character to display might not be the
7659 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7660 that we were reseat()ed to a new string, whose paragraph
7661 direction is not known. */
7662 if (it->bidi_p && it->bidi_it.first_elt)
7663 {
7664 get_visually_first_element (it);
7665 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7666 }
7667
7668 /* Time to check for invisible text? */
7669 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7670 {
7671 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7672 {
7673 if (!(!it->bidi_p
7674 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7675 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7676 {
7677 /* With bidi non-linear iteration, we could find
7678 ourselves far beyond the last computed stop_charpos,
7679 with several other stop positions in between that we
7680 missed. Scan them all now, in buffer's logical
7681 order, until we find and handle the last stop_charpos
7682 that precedes our current position. */
7683 handle_stop_backwards (it, it->stop_charpos);
7684 return GET_NEXT_DISPLAY_ELEMENT (it);
7685 }
7686 else
7687 {
7688 if (it->bidi_p)
7689 {
7690 /* Take note of the stop position we just moved
7691 across, for when we will move back across it. */
7692 it->prev_stop = it->stop_charpos;
7693 /* If we are at base paragraph embedding level, take
7694 note of the last stop position seen at this
7695 level. */
7696 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7697 it->base_level_stop = it->stop_charpos;
7698 }
7699 handle_stop (it);
7700
7701 /* Since a handler may have changed IT->method, we must
7702 recurse here. */
7703 return GET_NEXT_DISPLAY_ELEMENT (it);
7704 }
7705 }
7706 else if (it->bidi_p
7707 /* If we are before prev_stop, we may have overstepped
7708 on our way backwards a stop_pos, and if so, we need
7709 to handle that stop_pos. */
7710 && IT_STRING_CHARPOS (*it) < it->prev_stop
7711 /* We can sometimes back up for reasons that have nothing
7712 to do with bidi reordering. E.g., compositions. The
7713 code below is only needed when we are above the base
7714 embedding level, so test for that explicitly. */
7715 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7716 {
7717 /* If we lost track of base_level_stop, we have no better
7718 place for handle_stop_backwards to start from than string
7719 beginning. This happens, e.g., when we were reseated to
7720 the previous screenful of text by vertical-motion. */
7721 if (it->base_level_stop <= 0
7722 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7723 it->base_level_stop = 0;
7724 handle_stop_backwards (it, it->base_level_stop);
7725 return GET_NEXT_DISPLAY_ELEMENT (it);
7726 }
7727 }
7728
7729 if (it->current.overlay_string_index >= 0)
7730 {
7731 /* Get the next character from an overlay string. In overlay
7732 strings, there is no field width or padding with spaces to
7733 do. */
7734 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7735 {
7736 it->what = IT_EOB;
7737 return 0;
7738 }
7739 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7740 IT_STRING_BYTEPOS (*it),
7741 it->bidi_it.scan_dir < 0
7742 ? -1
7743 : SCHARS (it->string))
7744 && next_element_from_composition (it))
7745 {
7746 return 1;
7747 }
7748 else if (STRING_MULTIBYTE (it->string))
7749 {
7750 const unsigned char *s = (SDATA (it->string)
7751 + IT_STRING_BYTEPOS (*it));
7752 it->c = string_char_and_length (s, &it->len);
7753 }
7754 else
7755 {
7756 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7757 it->len = 1;
7758 }
7759 }
7760 else
7761 {
7762 /* Get the next character from a Lisp string that is not an
7763 overlay string. Such strings come from the mode line, for
7764 example. We may have to pad with spaces, or truncate the
7765 string. See also next_element_from_c_string. */
7766 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7767 {
7768 it->what = IT_EOB;
7769 return 0;
7770 }
7771 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7772 {
7773 /* Pad with spaces. */
7774 it->c = ' ', it->len = 1;
7775 CHARPOS (position) = BYTEPOS (position) = -1;
7776 }
7777 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7778 IT_STRING_BYTEPOS (*it),
7779 it->bidi_it.scan_dir < 0
7780 ? -1
7781 : it->string_nchars)
7782 && next_element_from_composition (it))
7783 {
7784 return 1;
7785 }
7786 else if (STRING_MULTIBYTE (it->string))
7787 {
7788 const unsigned char *s = (SDATA (it->string)
7789 + IT_STRING_BYTEPOS (*it));
7790 it->c = string_char_and_length (s, &it->len);
7791 }
7792 else
7793 {
7794 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7795 it->len = 1;
7796 }
7797 }
7798
7799 /* Record what we have and where it came from. */
7800 it->what = IT_CHARACTER;
7801 it->object = it->string;
7802 it->position = position;
7803 return 1;
7804 }
7805
7806
7807 /* Load IT with next display element from C string IT->s.
7808 IT->string_nchars is the maximum number of characters to return
7809 from the string. IT->end_charpos may be greater than
7810 IT->string_nchars when this function is called, in which case we
7811 may have to return padding spaces. Value is zero if end of string
7812 reached, including padding spaces. */
7813
7814 static int
7815 next_element_from_c_string (struct it *it)
7816 {
7817 bool success_p = true;
7818
7819 eassert (it->s);
7820 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7821 it->what = IT_CHARACTER;
7822 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7823 it->object = Qnil;
7824
7825 /* With bidi reordering, the character to display might not be the
7826 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7827 we were reseated to a new string, whose paragraph direction is
7828 not known. */
7829 if (it->bidi_p && it->bidi_it.first_elt)
7830 get_visually_first_element (it);
7831
7832 /* IT's position can be greater than IT->string_nchars in case a
7833 field width or precision has been specified when the iterator was
7834 initialized. */
7835 if (IT_CHARPOS (*it) >= it->end_charpos)
7836 {
7837 /* End of the game. */
7838 it->what = IT_EOB;
7839 success_p = 0;
7840 }
7841 else if (IT_CHARPOS (*it) >= it->string_nchars)
7842 {
7843 /* Pad with spaces. */
7844 it->c = ' ', it->len = 1;
7845 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7846 }
7847 else if (it->multibyte_p)
7848 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7849 else
7850 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7851
7852 return success_p;
7853 }
7854
7855
7856 /* Set up IT to return characters from an ellipsis, if appropriate.
7857 The definition of the ellipsis glyphs may come from a display table
7858 entry. This function fills IT with the first glyph from the
7859 ellipsis if an ellipsis is to be displayed. */
7860
7861 static int
7862 next_element_from_ellipsis (struct it *it)
7863 {
7864 if (it->selective_display_ellipsis_p)
7865 setup_for_ellipsis (it, it->len);
7866 else
7867 {
7868 /* The face at the current position may be different from the
7869 face we find after the invisible text. Remember what it
7870 was in IT->saved_face_id, and signal that it's there by
7871 setting face_before_selective_p. */
7872 it->saved_face_id = it->face_id;
7873 it->method = GET_FROM_BUFFER;
7874 it->object = it->w->contents;
7875 reseat_at_next_visible_line_start (it, 1);
7876 it->face_before_selective_p = true;
7877 }
7878
7879 return GET_NEXT_DISPLAY_ELEMENT (it);
7880 }
7881
7882
7883 /* Deliver an image display element. The iterator IT is already
7884 filled with image information (done in handle_display_prop). Value
7885 is always 1. */
7886
7887
7888 static int
7889 next_element_from_image (struct it *it)
7890 {
7891 it->what = IT_IMAGE;
7892 it->ignore_overlay_strings_at_pos_p = 0;
7893 return 1;
7894 }
7895
7896
7897 /* Fill iterator IT with next display element from a stretch glyph
7898 property. IT->object is the value of the text property. Value is
7899 always 1. */
7900
7901 static int
7902 next_element_from_stretch (struct it *it)
7903 {
7904 it->what = IT_STRETCH;
7905 return 1;
7906 }
7907
7908 /* Scan backwards from IT's current position until we find a stop
7909 position, or until BEGV. This is called when we find ourself
7910 before both the last known prev_stop and base_level_stop while
7911 reordering bidirectional text. */
7912
7913 static void
7914 compute_stop_pos_backwards (struct it *it)
7915 {
7916 const int SCAN_BACK_LIMIT = 1000;
7917 struct text_pos pos;
7918 struct display_pos save_current = it->current;
7919 struct text_pos save_position = it->position;
7920 ptrdiff_t charpos = IT_CHARPOS (*it);
7921 ptrdiff_t where_we_are = charpos;
7922 ptrdiff_t save_stop_pos = it->stop_charpos;
7923 ptrdiff_t save_end_pos = it->end_charpos;
7924
7925 eassert (NILP (it->string) && !it->s);
7926 eassert (it->bidi_p);
7927 it->bidi_p = 0;
7928 do
7929 {
7930 it->end_charpos = min (charpos + 1, ZV);
7931 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7932 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7933 reseat_1 (it, pos, 0);
7934 compute_stop_pos (it);
7935 /* We must advance forward, right? */
7936 if (it->stop_charpos <= charpos)
7937 emacs_abort ();
7938 }
7939 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7940
7941 if (it->stop_charpos <= where_we_are)
7942 it->prev_stop = it->stop_charpos;
7943 else
7944 it->prev_stop = BEGV;
7945 it->bidi_p = true;
7946 it->current = save_current;
7947 it->position = save_position;
7948 it->stop_charpos = save_stop_pos;
7949 it->end_charpos = save_end_pos;
7950 }
7951
7952 /* Scan forward from CHARPOS in the current buffer/string, until we
7953 find a stop position > current IT's position. Then handle the stop
7954 position before that. This is called when we bump into a stop
7955 position while reordering bidirectional text. CHARPOS should be
7956 the last previously processed stop_pos (or BEGV/0, if none were
7957 processed yet) whose position is less that IT's current
7958 position. */
7959
7960 static void
7961 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7962 {
7963 int bufp = !STRINGP (it->string);
7964 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7965 struct display_pos save_current = it->current;
7966 struct text_pos save_position = it->position;
7967 struct text_pos pos1;
7968 ptrdiff_t next_stop;
7969
7970 /* Scan in strict logical order. */
7971 eassert (it->bidi_p);
7972 it->bidi_p = 0;
7973 do
7974 {
7975 it->prev_stop = charpos;
7976 if (bufp)
7977 {
7978 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7979 reseat_1 (it, pos1, 0);
7980 }
7981 else
7982 it->current.string_pos = string_pos (charpos, it->string);
7983 compute_stop_pos (it);
7984 /* We must advance forward, right? */
7985 if (it->stop_charpos <= it->prev_stop)
7986 emacs_abort ();
7987 charpos = it->stop_charpos;
7988 }
7989 while (charpos <= where_we_are);
7990
7991 it->bidi_p = true;
7992 it->current = save_current;
7993 it->position = save_position;
7994 next_stop = it->stop_charpos;
7995 it->stop_charpos = it->prev_stop;
7996 handle_stop (it);
7997 it->stop_charpos = next_stop;
7998 }
7999
8000 /* Load IT with the next display element from current_buffer. Value
8001 is zero if end of buffer reached. IT->stop_charpos is the next
8002 position at which to stop and check for text properties or buffer
8003 end. */
8004
8005 static int
8006 next_element_from_buffer (struct it *it)
8007 {
8008 bool success_p = true;
8009
8010 eassert (IT_CHARPOS (*it) >= BEGV);
8011 eassert (NILP (it->string) && !it->s);
8012 eassert (!it->bidi_p
8013 || (EQ (it->bidi_it.string.lstring, Qnil)
8014 && it->bidi_it.string.s == NULL));
8015
8016 /* With bidi reordering, the character to display might not be the
8017 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8018 we were reseat()ed to a new buffer position, which is potentially
8019 a different paragraph. */
8020 if (it->bidi_p && it->bidi_it.first_elt)
8021 {
8022 get_visually_first_element (it);
8023 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8024 }
8025
8026 if (IT_CHARPOS (*it) >= it->stop_charpos)
8027 {
8028 if (IT_CHARPOS (*it) >= it->end_charpos)
8029 {
8030 int overlay_strings_follow_p;
8031
8032 /* End of the game, except when overlay strings follow that
8033 haven't been returned yet. */
8034 if (it->overlay_strings_at_end_processed_p)
8035 overlay_strings_follow_p = 0;
8036 else
8037 {
8038 it->overlay_strings_at_end_processed_p = true;
8039 overlay_strings_follow_p = get_overlay_strings (it, 0);
8040 }
8041
8042 if (overlay_strings_follow_p)
8043 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8044 else
8045 {
8046 it->what = IT_EOB;
8047 it->position = it->current.pos;
8048 success_p = 0;
8049 }
8050 }
8051 else if (!(!it->bidi_p
8052 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8053 || IT_CHARPOS (*it) == it->stop_charpos))
8054 {
8055 /* With bidi non-linear iteration, we could find ourselves
8056 far beyond the last computed stop_charpos, with several
8057 other stop positions in between that we missed. Scan
8058 them all now, in buffer's logical order, until we find
8059 and handle the last stop_charpos that precedes our
8060 current position. */
8061 handle_stop_backwards (it, it->stop_charpos);
8062 return GET_NEXT_DISPLAY_ELEMENT (it);
8063 }
8064 else
8065 {
8066 if (it->bidi_p)
8067 {
8068 /* Take note of the stop position we just moved across,
8069 for when we will move back across it. */
8070 it->prev_stop = it->stop_charpos;
8071 /* If we are at base paragraph embedding level, take
8072 note of the last stop position seen at this
8073 level. */
8074 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8075 it->base_level_stop = it->stop_charpos;
8076 }
8077 handle_stop (it);
8078 return GET_NEXT_DISPLAY_ELEMENT (it);
8079 }
8080 }
8081 else if (it->bidi_p
8082 /* If we are before prev_stop, we may have overstepped on
8083 our way backwards a stop_pos, and if so, we need to
8084 handle that stop_pos. */
8085 && IT_CHARPOS (*it) < it->prev_stop
8086 /* We can sometimes back up for reasons that have nothing
8087 to do with bidi reordering. E.g., compositions. The
8088 code below is only needed when we are above the base
8089 embedding level, so test for that explicitly. */
8090 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8091 {
8092 if (it->base_level_stop <= 0
8093 || IT_CHARPOS (*it) < it->base_level_stop)
8094 {
8095 /* If we lost track of base_level_stop, we need to find
8096 prev_stop by looking backwards. This happens, e.g., when
8097 we were reseated to the previous screenful of text by
8098 vertical-motion. */
8099 it->base_level_stop = BEGV;
8100 compute_stop_pos_backwards (it);
8101 handle_stop_backwards (it, it->prev_stop);
8102 }
8103 else
8104 handle_stop_backwards (it, it->base_level_stop);
8105 return GET_NEXT_DISPLAY_ELEMENT (it);
8106 }
8107 else
8108 {
8109 /* No face changes, overlays etc. in sight, so just return a
8110 character from current_buffer. */
8111 unsigned char *p;
8112 ptrdiff_t stop;
8113
8114 /* Maybe run the redisplay end trigger hook. Performance note:
8115 This doesn't seem to cost measurable time. */
8116 if (it->redisplay_end_trigger_charpos
8117 && it->glyph_row
8118 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8119 run_redisplay_end_trigger_hook (it);
8120
8121 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8122 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8123 stop)
8124 && next_element_from_composition (it))
8125 {
8126 return 1;
8127 }
8128
8129 /* Get the next character, maybe multibyte. */
8130 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8131 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8132 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8133 else
8134 it->c = *p, it->len = 1;
8135
8136 /* Record what we have and where it came from. */
8137 it->what = IT_CHARACTER;
8138 it->object = it->w->contents;
8139 it->position = it->current.pos;
8140
8141 /* Normally we return the character found above, except when we
8142 really want to return an ellipsis for selective display. */
8143 if (it->selective)
8144 {
8145 if (it->c == '\n')
8146 {
8147 /* A value of selective > 0 means hide lines indented more
8148 than that number of columns. */
8149 if (it->selective > 0
8150 && IT_CHARPOS (*it) + 1 < ZV
8151 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8152 IT_BYTEPOS (*it) + 1,
8153 it->selective))
8154 {
8155 success_p = next_element_from_ellipsis (it);
8156 it->dpvec_char_len = -1;
8157 }
8158 }
8159 else if (it->c == '\r' && it->selective == -1)
8160 {
8161 /* A value of selective == -1 means that everything from the
8162 CR to the end of the line is invisible, with maybe an
8163 ellipsis displayed for it. */
8164 success_p = next_element_from_ellipsis (it);
8165 it->dpvec_char_len = -1;
8166 }
8167 }
8168 }
8169
8170 /* Value is zero if end of buffer reached. */
8171 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8172 return success_p;
8173 }
8174
8175
8176 /* Run the redisplay end trigger hook for IT. */
8177
8178 static void
8179 run_redisplay_end_trigger_hook (struct it *it)
8180 {
8181 Lisp_Object args[3];
8182
8183 /* IT->glyph_row should be non-null, i.e. we should be actually
8184 displaying something, or otherwise we should not run the hook. */
8185 eassert (it->glyph_row);
8186
8187 /* Set up hook arguments. */
8188 args[0] = Qredisplay_end_trigger_functions;
8189 args[1] = it->window;
8190 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8191 it->redisplay_end_trigger_charpos = 0;
8192
8193 /* Since we are *trying* to run these functions, don't try to run
8194 them again, even if they get an error. */
8195 wset_redisplay_end_trigger (it->w, Qnil);
8196 Frun_hook_with_args (3, args);
8197
8198 /* Notice if it changed the face of the character we are on. */
8199 handle_face_prop (it);
8200 }
8201
8202
8203 /* Deliver a composition display element. Unlike the other
8204 next_element_from_XXX, this function is not registered in the array
8205 get_next_element[]. It is called from next_element_from_buffer and
8206 next_element_from_string when necessary. */
8207
8208 static int
8209 next_element_from_composition (struct it *it)
8210 {
8211 it->what = IT_COMPOSITION;
8212 it->len = it->cmp_it.nbytes;
8213 if (STRINGP (it->string))
8214 {
8215 if (it->c < 0)
8216 {
8217 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8218 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8219 return 0;
8220 }
8221 it->position = it->current.string_pos;
8222 it->object = it->string;
8223 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8224 IT_STRING_BYTEPOS (*it), it->string);
8225 }
8226 else
8227 {
8228 if (it->c < 0)
8229 {
8230 IT_CHARPOS (*it) += it->cmp_it.nchars;
8231 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8232 if (it->bidi_p)
8233 {
8234 if (it->bidi_it.new_paragraph)
8235 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8236 /* Resync the bidi iterator with IT's new position.
8237 FIXME: this doesn't support bidirectional text. */
8238 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8239 bidi_move_to_visually_next (&it->bidi_it);
8240 }
8241 return 0;
8242 }
8243 it->position = it->current.pos;
8244 it->object = it->w->contents;
8245 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8246 IT_BYTEPOS (*it), Qnil);
8247 }
8248 return 1;
8249 }
8250
8251
8252 \f
8253 /***********************************************************************
8254 Moving an iterator without producing glyphs
8255 ***********************************************************************/
8256
8257 /* Check if iterator is at a position corresponding to a valid buffer
8258 position after some move_it_ call. */
8259
8260 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8261 ((it)->method == GET_FROM_STRING \
8262 ? IT_STRING_CHARPOS (*it) == 0 \
8263 : 1)
8264
8265
8266 /* Move iterator IT to a specified buffer or X position within one
8267 line on the display without producing glyphs.
8268
8269 OP should be a bit mask including some or all of these bits:
8270 MOVE_TO_X: Stop upon reaching x-position TO_X.
8271 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8272 Regardless of OP's value, stop upon reaching the end of the display line.
8273
8274 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8275 This means, in particular, that TO_X includes window's horizontal
8276 scroll amount.
8277
8278 The return value has several possible values that
8279 say what condition caused the scan to stop:
8280
8281 MOVE_POS_MATCH_OR_ZV
8282 - when TO_POS or ZV was reached.
8283
8284 MOVE_X_REACHED
8285 -when TO_X was reached before TO_POS or ZV were reached.
8286
8287 MOVE_LINE_CONTINUED
8288 - when we reached the end of the display area and the line must
8289 be continued.
8290
8291 MOVE_LINE_TRUNCATED
8292 - when we reached the end of the display area and the line is
8293 truncated.
8294
8295 MOVE_NEWLINE_OR_CR
8296 - when we stopped at a line end, i.e. a newline or a CR and selective
8297 display is on. */
8298
8299 static enum move_it_result
8300 move_it_in_display_line_to (struct it *it,
8301 ptrdiff_t to_charpos, int to_x,
8302 enum move_operation_enum op)
8303 {
8304 enum move_it_result result = MOVE_UNDEFINED;
8305 struct glyph_row *saved_glyph_row;
8306 struct it wrap_it, atpos_it, atx_it, ppos_it;
8307 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8308 void *ppos_data = NULL;
8309 int may_wrap = 0;
8310 enum it_method prev_method = it->method;
8311 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8312 int saw_smaller_pos = prev_pos < to_charpos;
8313
8314 /* Don't produce glyphs in produce_glyphs. */
8315 saved_glyph_row = it->glyph_row;
8316 it->glyph_row = NULL;
8317
8318 /* Use wrap_it to save a copy of IT wherever a word wrap could
8319 occur. Use atpos_it to save a copy of IT at the desired buffer
8320 position, if found, so that we can scan ahead and check if the
8321 word later overshoots the window edge. Use atx_it similarly, for
8322 pixel positions. */
8323 wrap_it.sp = -1;
8324 atpos_it.sp = -1;
8325 atx_it.sp = -1;
8326
8327 /* Use ppos_it under bidi reordering to save a copy of IT for the
8328 position > CHARPOS that is the closest to CHARPOS. We restore
8329 that position in IT when we have scanned the entire display line
8330 without finding a match for CHARPOS and all the character
8331 positions are greater than CHARPOS. */
8332 if (it->bidi_p)
8333 {
8334 SAVE_IT (ppos_it, *it, ppos_data);
8335 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8336 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8337 SAVE_IT (ppos_it, *it, ppos_data);
8338 }
8339
8340 #define BUFFER_POS_REACHED_P() \
8341 ((op & MOVE_TO_POS) != 0 \
8342 && BUFFERP (it->object) \
8343 && (IT_CHARPOS (*it) == to_charpos \
8344 || ((!it->bidi_p \
8345 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8346 && IT_CHARPOS (*it) > to_charpos) \
8347 || (it->what == IT_COMPOSITION \
8348 && ((IT_CHARPOS (*it) > to_charpos \
8349 && to_charpos >= it->cmp_it.charpos) \
8350 || (IT_CHARPOS (*it) < to_charpos \
8351 && to_charpos <= it->cmp_it.charpos)))) \
8352 && (it->method == GET_FROM_BUFFER \
8353 || (it->method == GET_FROM_DISPLAY_VECTOR \
8354 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8355
8356 /* If there's a line-/wrap-prefix, handle it. */
8357 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8358 && it->current_y < it->last_visible_y)
8359 handle_line_prefix (it);
8360
8361 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8362 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8363
8364 while (1)
8365 {
8366 int x, i, ascent = 0, descent = 0;
8367
8368 /* Utility macro to reset an iterator with x, ascent, and descent. */
8369 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8370 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8371 (IT)->max_descent = descent)
8372
8373 /* Stop if we move beyond TO_CHARPOS (after an image or a
8374 display string or stretch glyph). */
8375 if ((op & MOVE_TO_POS) != 0
8376 && BUFFERP (it->object)
8377 && it->method == GET_FROM_BUFFER
8378 && (((!it->bidi_p
8379 /* When the iterator is at base embedding level, we
8380 are guaranteed that characters are delivered for
8381 display in strictly increasing order of their
8382 buffer positions. */
8383 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8384 && IT_CHARPOS (*it) > to_charpos)
8385 || (it->bidi_p
8386 && (prev_method == GET_FROM_IMAGE
8387 || prev_method == GET_FROM_STRETCH
8388 || prev_method == GET_FROM_STRING)
8389 /* Passed TO_CHARPOS from left to right. */
8390 && ((prev_pos < to_charpos
8391 && IT_CHARPOS (*it) > to_charpos)
8392 /* Passed TO_CHARPOS from right to left. */
8393 || (prev_pos > to_charpos
8394 && IT_CHARPOS (*it) < to_charpos)))))
8395 {
8396 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8397 {
8398 result = MOVE_POS_MATCH_OR_ZV;
8399 break;
8400 }
8401 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8402 /* If wrap_it is valid, the current position might be in a
8403 word that is wrapped. So, save the iterator in
8404 atpos_it and continue to see if wrapping happens. */
8405 SAVE_IT (atpos_it, *it, atpos_data);
8406 }
8407
8408 /* Stop when ZV reached.
8409 We used to stop here when TO_CHARPOS reached as well, but that is
8410 too soon if this glyph does not fit on this line. So we handle it
8411 explicitly below. */
8412 if (!get_next_display_element (it))
8413 {
8414 result = MOVE_POS_MATCH_OR_ZV;
8415 break;
8416 }
8417
8418 if (it->line_wrap == TRUNCATE)
8419 {
8420 if (BUFFER_POS_REACHED_P ())
8421 {
8422 result = MOVE_POS_MATCH_OR_ZV;
8423 break;
8424 }
8425 }
8426 else
8427 {
8428 if (it->line_wrap == WORD_WRAP)
8429 {
8430 if (IT_DISPLAYING_WHITESPACE (it))
8431 may_wrap = 1;
8432 else if (may_wrap)
8433 {
8434 /* We have reached a glyph that follows one or more
8435 whitespace characters. If the position is
8436 already found, we are done. */
8437 if (atpos_it.sp >= 0)
8438 {
8439 RESTORE_IT (it, &atpos_it, atpos_data);
8440 result = MOVE_POS_MATCH_OR_ZV;
8441 goto done;
8442 }
8443 if (atx_it.sp >= 0)
8444 {
8445 RESTORE_IT (it, &atx_it, atx_data);
8446 result = MOVE_X_REACHED;
8447 goto done;
8448 }
8449 /* Otherwise, we can wrap here. */
8450 SAVE_IT (wrap_it, *it, wrap_data);
8451 may_wrap = 0;
8452 }
8453 }
8454 }
8455
8456 /* Remember the line height for the current line, in case
8457 the next element doesn't fit on the line. */
8458 ascent = it->max_ascent;
8459 descent = it->max_descent;
8460
8461 /* The call to produce_glyphs will get the metrics of the
8462 display element IT is loaded with. Record the x-position
8463 before this display element, in case it doesn't fit on the
8464 line. */
8465 x = it->current_x;
8466
8467 PRODUCE_GLYPHS (it);
8468
8469 if (it->area != TEXT_AREA)
8470 {
8471 prev_method = it->method;
8472 if (it->method == GET_FROM_BUFFER)
8473 prev_pos = IT_CHARPOS (*it);
8474 set_iterator_to_next (it, 1);
8475 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8476 SET_TEXT_POS (this_line_min_pos,
8477 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8478 if (it->bidi_p
8479 && (op & MOVE_TO_POS)
8480 && IT_CHARPOS (*it) > to_charpos
8481 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8482 SAVE_IT (ppos_it, *it, ppos_data);
8483 continue;
8484 }
8485
8486 /* The number of glyphs we get back in IT->nglyphs will normally
8487 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8488 character on a terminal frame, or (iii) a line end. For the
8489 second case, IT->nglyphs - 1 padding glyphs will be present.
8490 (On X frames, there is only one glyph produced for a
8491 composite character.)
8492
8493 The behavior implemented below means, for continuation lines,
8494 that as many spaces of a TAB as fit on the current line are
8495 displayed there. For terminal frames, as many glyphs of a
8496 multi-glyph character are displayed in the current line, too.
8497 This is what the old redisplay code did, and we keep it that
8498 way. Under X, the whole shape of a complex character must
8499 fit on the line or it will be completely displayed in the
8500 next line.
8501
8502 Note that both for tabs and padding glyphs, all glyphs have
8503 the same width. */
8504 if (it->nglyphs)
8505 {
8506 /* More than one glyph or glyph doesn't fit on line. All
8507 glyphs have the same width. */
8508 int single_glyph_width = it->pixel_width / it->nglyphs;
8509 int new_x;
8510 int x_before_this_char = x;
8511 int hpos_before_this_char = it->hpos;
8512
8513 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8514 {
8515 new_x = x + single_glyph_width;
8516
8517 /* We want to leave anything reaching TO_X to the caller. */
8518 if ((op & MOVE_TO_X) && new_x > to_x)
8519 {
8520 if (BUFFER_POS_REACHED_P ())
8521 {
8522 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8523 goto buffer_pos_reached;
8524 if (atpos_it.sp < 0)
8525 {
8526 SAVE_IT (atpos_it, *it, atpos_data);
8527 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8528 }
8529 }
8530 else
8531 {
8532 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8533 {
8534 it->current_x = x;
8535 result = MOVE_X_REACHED;
8536 break;
8537 }
8538 if (atx_it.sp < 0)
8539 {
8540 SAVE_IT (atx_it, *it, atx_data);
8541 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8542 }
8543 }
8544 }
8545
8546 if (/* Lines are continued. */
8547 it->line_wrap != TRUNCATE
8548 && (/* And glyph doesn't fit on the line. */
8549 new_x > it->last_visible_x
8550 /* Or it fits exactly and we're on a window
8551 system frame. */
8552 || (new_x == it->last_visible_x
8553 && FRAME_WINDOW_P (it->f)
8554 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8555 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8556 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8557 {
8558 if (/* IT->hpos == 0 means the very first glyph
8559 doesn't fit on the line, e.g. a wide image. */
8560 it->hpos == 0
8561 || (new_x == it->last_visible_x
8562 && FRAME_WINDOW_P (it->f)))
8563 {
8564 ++it->hpos;
8565 it->current_x = new_x;
8566
8567 /* The character's last glyph just barely fits
8568 in this row. */
8569 if (i == it->nglyphs - 1)
8570 {
8571 /* If this is the destination position,
8572 return a position *before* it in this row,
8573 now that we know it fits in this row. */
8574 if (BUFFER_POS_REACHED_P ())
8575 {
8576 if (it->line_wrap != WORD_WRAP
8577 || wrap_it.sp < 0)
8578 {
8579 it->hpos = hpos_before_this_char;
8580 it->current_x = x_before_this_char;
8581 result = MOVE_POS_MATCH_OR_ZV;
8582 break;
8583 }
8584 if (it->line_wrap == WORD_WRAP
8585 && atpos_it.sp < 0)
8586 {
8587 SAVE_IT (atpos_it, *it, atpos_data);
8588 atpos_it.current_x = x_before_this_char;
8589 atpos_it.hpos = hpos_before_this_char;
8590 }
8591 }
8592
8593 prev_method = it->method;
8594 if (it->method == GET_FROM_BUFFER)
8595 prev_pos = IT_CHARPOS (*it);
8596 set_iterator_to_next (it, 1);
8597 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8598 SET_TEXT_POS (this_line_min_pos,
8599 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8600 /* On graphical terminals, newlines may
8601 "overflow" into the fringe if
8602 overflow-newline-into-fringe is non-nil.
8603 On text terminals, and on graphical
8604 terminals with no right margin, newlines
8605 may overflow into the last glyph on the
8606 display line.*/
8607 if (!FRAME_WINDOW_P (it->f)
8608 || ((it->bidi_p
8609 && it->bidi_it.paragraph_dir == R2L)
8610 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8611 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8612 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8613 {
8614 if (!get_next_display_element (it))
8615 {
8616 result = MOVE_POS_MATCH_OR_ZV;
8617 break;
8618 }
8619 if (BUFFER_POS_REACHED_P ())
8620 {
8621 if (ITERATOR_AT_END_OF_LINE_P (it))
8622 result = MOVE_POS_MATCH_OR_ZV;
8623 else
8624 result = MOVE_LINE_CONTINUED;
8625 break;
8626 }
8627 if (ITERATOR_AT_END_OF_LINE_P (it)
8628 && (it->line_wrap != WORD_WRAP
8629 || wrap_it.sp < 0))
8630 {
8631 result = MOVE_NEWLINE_OR_CR;
8632 break;
8633 }
8634 }
8635 }
8636 }
8637 else
8638 IT_RESET_X_ASCENT_DESCENT (it);
8639
8640 if (wrap_it.sp >= 0)
8641 {
8642 RESTORE_IT (it, &wrap_it, wrap_data);
8643 atpos_it.sp = -1;
8644 atx_it.sp = -1;
8645 }
8646
8647 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8648 IT_CHARPOS (*it)));
8649 result = MOVE_LINE_CONTINUED;
8650 break;
8651 }
8652
8653 if (BUFFER_POS_REACHED_P ())
8654 {
8655 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8656 goto buffer_pos_reached;
8657 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8658 {
8659 SAVE_IT (atpos_it, *it, atpos_data);
8660 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8661 }
8662 }
8663
8664 if (new_x > it->first_visible_x)
8665 {
8666 /* Glyph is visible. Increment number of glyphs that
8667 would be displayed. */
8668 ++it->hpos;
8669 }
8670 }
8671
8672 if (result != MOVE_UNDEFINED)
8673 break;
8674 }
8675 else if (BUFFER_POS_REACHED_P ())
8676 {
8677 buffer_pos_reached:
8678 IT_RESET_X_ASCENT_DESCENT (it);
8679 result = MOVE_POS_MATCH_OR_ZV;
8680 break;
8681 }
8682 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8683 {
8684 /* Stop when TO_X specified and reached. This check is
8685 necessary here because of lines consisting of a line end,
8686 only. The line end will not produce any glyphs and we
8687 would never get MOVE_X_REACHED. */
8688 eassert (it->nglyphs == 0);
8689 result = MOVE_X_REACHED;
8690 break;
8691 }
8692
8693 /* Is this a line end? If yes, we're done. */
8694 if (ITERATOR_AT_END_OF_LINE_P (it))
8695 {
8696 /* If we are past TO_CHARPOS, but never saw any character
8697 positions smaller than TO_CHARPOS, return
8698 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8699 did. */
8700 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8701 {
8702 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8703 {
8704 if (IT_CHARPOS (ppos_it) < ZV)
8705 {
8706 RESTORE_IT (it, &ppos_it, ppos_data);
8707 result = MOVE_POS_MATCH_OR_ZV;
8708 }
8709 else
8710 goto buffer_pos_reached;
8711 }
8712 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8713 && IT_CHARPOS (*it) > to_charpos)
8714 goto buffer_pos_reached;
8715 else
8716 result = MOVE_NEWLINE_OR_CR;
8717 }
8718 else
8719 result = MOVE_NEWLINE_OR_CR;
8720 break;
8721 }
8722
8723 prev_method = it->method;
8724 if (it->method == GET_FROM_BUFFER)
8725 prev_pos = IT_CHARPOS (*it);
8726 /* The current display element has been consumed. Advance
8727 to the next. */
8728 set_iterator_to_next (it, 1);
8729 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8730 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8731 if (IT_CHARPOS (*it) < to_charpos)
8732 saw_smaller_pos = 1;
8733 if (it->bidi_p
8734 && (op & MOVE_TO_POS)
8735 && IT_CHARPOS (*it) >= to_charpos
8736 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8737 SAVE_IT (ppos_it, *it, ppos_data);
8738
8739 /* Stop if lines are truncated and IT's current x-position is
8740 past the right edge of the window now. */
8741 if (it->line_wrap == TRUNCATE
8742 && it->current_x >= it->last_visible_x)
8743 {
8744 if (!FRAME_WINDOW_P (it->f)
8745 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8746 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8747 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8748 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8749 {
8750 int at_eob_p = 0;
8751
8752 if ((at_eob_p = !get_next_display_element (it))
8753 || BUFFER_POS_REACHED_P ()
8754 /* If we are past TO_CHARPOS, but never saw any
8755 character positions smaller than TO_CHARPOS,
8756 return MOVE_POS_MATCH_OR_ZV, like the
8757 unidirectional display did. */
8758 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8759 && !saw_smaller_pos
8760 && IT_CHARPOS (*it) > to_charpos))
8761 {
8762 if (it->bidi_p
8763 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8764 RESTORE_IT (it, &ppos_it, ppos_data);
8765 result = MOVE_POS_MATCH_OR_ZV;
8766 break;
8767 }
8768 if (ITERATOR_AT_END_OF_LINE_P (it))
8769 {
8770 result = MOVE_NEWLINE_OR_CR;
8771 break;
8772 }
8773 }
8774 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8775 && !saw_smaller_pos
8776 && IT_CHARPOS (*it) > to_charpos)
8777 {
8778 if (IT_CHARPOS (ppos_it) < ZV)
8779 RESTORE_IT (it, &ppos_it, ppos_data);
8780 result = MOVE_POS_MATCH_OR_ZV;
8781 break;
8782 }
8783 result = MOVE_LINE_TRUNCATED;
8784 break;
8785 }
8786 #undef IT_RESET_X_ASCENT_DESCENT
8787 }
8788
8789 #undef BUFFER_POS_REACHED_P
8790
8791 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8792 restore the saved iterator. */
8793 if (atpos_it.sp >= 0)
8794 RESTORE_IT (it, &atpos_it, atpos_data);
8795 else if (atx_it.sp >= 0)
8796 RESTORE_IT (it, &atx_it, atx_data);
8797
8798 done:
8799
8800 if (atpos_data)
8801 bidi_unshelve_cache (atpos_data, 1);
8802 if (atx_data)
8803 bidi_unshelve_cache (atx_data, 1);
8804 if (wrap_data)
8805 bidi_unshelve_cache (wrap_data, 1);
8806 if (ppos_data)
8807 bidi_unshelve_cache (ppos_data, 1);
8808
8809 /* Restore the iterator settings altered at the beginning of this
8810 function. */
8811 it->glyph_row = saved_glyph_row;
8812 return result;
8813 }
8814
8815 /* For external use. */
8816 void
8817 move_it_in_display_line (struct it *it,
8818 ptrdiff_t to_charpos, int to_x,
8819 enum move_operation_enum op)
8820 {
8821 if (it->line_wrap == WORD_WRAP
8822 && (op & MOVE_TO_X))
8823 {
8824 struct it save_it;
8825 void *save_data = NULL;
8826 int skip;
8827
8828 SAVE_IT (save_it, *it, save_data);
8829 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8830 /* When word-wrap is on, TO_X may lie past the end
8831 of a wrapped line. Then it->current is the
8832 character on the next line, so backtrack to the
8833 space before the wrap point. */
8834 if (skip == MOVE_LINE_CONTINUED)
8835 {
8836 int prev_x = max (it->current_x - 1, 0);
8837 RESTORE_IT (it, &save_it, save_data);
8838 move_it_in_display_line_to
8839 (it, -1, prev_x, MOVE_TO_X);
8840 }
8841 else
8842 bidi_unshelve_cache (save_data, 1);
8843 }
8844 else
8845 move_it_in_display_line_to (it, to_charpos, to_x, op);
8846 }
8847
8848
8849 /* Move IT forward until it satisfies one or more of the criteria in
8850 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8851
8852 OP is a bit-mask that specifies where to stop, and in particular,
8853 which of those four position arguments makes a difference. See the
8854 description of enum move_operation_enum.
8855
8856 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8857 screen line, this function will set IT to the next position that is
8858 displayed to the right of TO_CHARPOS on the screen.
8859
8860 Return the maximum pixel length of any line scanned but never more
8861 than it.last_visible_x. */
8862
8863 int
8864 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8865 {
8866 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8867 int line_height, line_start_x = 0, reached = 0;
8868 int max_current_x = 0;
8869 void *backup_data = NULL;
8870
8871 for (;;)
8872 {
8873 if (op & MOVE_TO_VPOS)
8874 {
8875 /* If no TO_CHARPOS and no TO_X specified, stop at the
8876 start of the line TO_VPOS. */
8877 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8878 {
8879 if (it->vpos == to_vpos)
8880 {
8881 reached = 1;
8882 break;
8883 }
8884 else
8885 skip = move_it_in_display_line_to (it, -1, -1, 0);
8886 }
8887 else
8888 {
8889 /* TO_VPOS >= 0 means stop at TO_X in the line at
8890 TO_VPOS, or at TO_POS, whichever comes first. */
8891 if (it->vpos == to_vpos)
8892 {
8893 reached = 2;
8894 break;
8895 }
8896
8897 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8898
8899 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8900 {
8901 reached = 3;
8902 break;
8903 }
8904 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8905 {
8906 /* We have reached TO_X but not in the line we want. */
8907 skip = move_it_in_display_line_to (it, to_charpos,
8908 -1, MOVE_TO_POS);
8909 if (skip == MOVE_POS_MATCH_OR_ZV)
8910 {
8911 reached = 4;
8912 break;
8913 }
8914 }
8915 }
8916 }
8917 else if (op & MOVE_TO_Y)
8918 {
8919 struct it it_backup;
8920
8921 if (it->line_wrap == WORD_WRAP)
8922 SAVE_IT (it_backup, *it, backup_data);
8923
8924 /* TO_Y specified means stop at TO_X in the line containing
8925 TO_Y---or at TO_CHARPOS if this is reached first. The
8926 problem is that we can't really tell whether the line
8927 contains TO_Y before we have completely scanned it, and
8928 this may skip past TO_X. What we do is to first scan to
8929 TO_X.
8930
8931 If TO_X is not specified, use a TO_X of zero. The reason
8932 is to make the outcome of this function more predictable.
8933 If we didn't use TO_X == 0, we would stop at the end of
8934 the line which is probably not what a caller would expect
8935 to happen. */
8936 skip = move_it_in_display_line_to
8937 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8938 (MOVE_TO_X | (op & MOVE_TO_POS)));
8939
8940 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8941 if (skip == MOVE_POS_MATCH_OR_ZV)
8942 reached = 5;
8943 else if (skip == MOVE_X_REACHED)
8944 {
8945 /* If TO_X was reached, we want to know whether TO_Y is
8946 in the line. We know this is the case if the already
8947 scanned glyphs make the line tall enough. Otherwise,
8948 we must check by scanning the rest of the line. */
8949 line_height = it->max_ascent + it->max_descent;
8950 if (to_y >= it->current_y
8951 && to_y < it->current_y + line_height)
8952 {
8953 reached = 6;
8954 break;
8955 }
8956 SAVE_IT (it_backup, *it, backup_data);
8957 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8958 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8959 op & MOVE_TO_POS);
8960 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8961 line_height = it->max_ascent + it->max_descent;
8962 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8963
8964 if (to_y >= it->current_y
8965 && to_y < it->current_y + line_height)
8966 {
8967 /* If TO_Y is in this line and TO_X was reached
8968 above, we scanned too far. We have to restore
8969 IT's settings to the ones before skipping. But
8970 keep the more accurate values of max_ascent and
8971 max_descent we've found while skipping the rest
8972 of the line, for the sake of callers, such as
8973 pos_visible_p, that need to know the line
8974 height. */
8975 int max_ascent = it->max_ascent;
8976 int max_descent = it->max_descent;
8977
8978 RESTORE_IT (it, &it_backup, backup_data);
8979 it->max_ascent = max_ascent;
8980 it->max_descent = max_descent;
8981 reached = 6;
8982 }
8983 else
8984 {
8985 skip = skip2;
8986 if (skip == MOVE_POS_MATCH_OR_ZV)
8987 reached = 7;
8988 }
8989 }
8990 else
8991 {
8992 /* Check whether TO_Y is in this line. */
8993 line_height = it->max_ascent + it->max_descent;
8994 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8995
8996 if (to_y >= it->current_y
8997 && to_y < it->current_y + line_height)
8998 {
8999 if (to_y > it->current_y)
9000 max_current_x = max (it->current_x, max_current_x);
9001
9002 /* When word-wrap is on, TO_X may lie past the end
9003 of a wrapped line. Then it->current is the
9004 character on the next line, so backtrack to the
9005 space before the wrap point. */
9006 if (skip == MOVE_LINE_CONTINUED
9007 && it->line_wrap == WORD_WRAP)
9008 {
9009 int prev_x = max (it->current_x - 1, 0);
9010 RESTORE_IT (it, &it_backup, backup_data);
9011 skip = move_it_in_display_line_to
9012 (it, -1, prev_x, MOVE_TO_X);
9013 }
9014
9015 reached = 6;
9016 }
9017 }
9018
9019 if (reached)
9020 {
9021 max_current_x = max (it->current_x, max_current_x);
9022 break;
9023 }
9024 }
9025 else if (BUFFERP (it->object)
9026 && (it->method == GET_FROM_BUFFER
9027 || it->method == GET_FROM_STRETCH)
9028 && IT_CHARPOS (*it) >= to_charpos
9029 /* Under bidi iteration, a call to set_iterator_to_next
9030 can scan far beyond to_charpos if the initial
9031 portion of the next line needs to be reordered. In
9032 that case, give move_it_in_display_line_to another
9033 chance below. */
9034 && !(it->bidi_p
9035 && it->bidi_it.scan_dir == -1))
9036 skip = MOVE_POS_MATCH_OR_ZV;
9037 else
9038 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9039
9040 switch (skip)
9041 {
9042 case MOVE_POS_MATCH_OR_ZV:
9043 max_current_x = max (it->current_x, max_current_x);
9044 reached = 8;
9045 goto out;
9046
9047 case MOVE_NEWLINE_OR_CR:
9048 max_current_x = max (it->current_x, max_current_x);
9049 set_iterator_to_next (it, 1);
9050 it->continuation_lines_width = 0;
9051 break;
9052
9053 case MOVE_LINE_TRUNCATED:
9054 max_current_x = it->last_visible_x;
9055 it->continuation_lines_width = 0;
9056 reseat_at_next_visible_line_start (it, 0);
9057 if ((op & MOVE_TO_POS) != 0
9058 && IT_CHARPOS (*it) > to_charpos)
9059 {
9060 reached = 9;
9061 goto out;
9062 }
9063 break;
9064
9065 case MOVE_LINE_CONTINUED:
9066 max_current_x = it->last_visible_x;
9067 /* For continued lines ending in a tab, some of the glyphs
9068 associated with the tab are displayed on the current
9069 line. Since it->current_x does not include these glyphs,
9070 we use it->last_visible_x instead. */
9071 if (it->c == '\t')
9072 {
9073 it->continuation_lines_width += it->last_visible_x;
9074 /* When moving by vpos, ensure that the iterator really
9075 advances to the next line (bug#847, bug#969). Fixme:
9076 do we need to do this in other circumstances? */
9077 if (it->current_x != it->last_visible_x
9078 && (op & MOVE_TO_VPOS)
9079 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9080 {
9081 line_start_x = it->current_x + it->pixel_width
9082 - it->last_visible_x;
9083 set_iterator_to_next (it, 0);
9084 }
9085 }
9086 else
9087 it->continuation_lines_width += it->current_x;
9088 break;
9089
9090 default:
9091 emacs_abort ();
9092 }
9093
9094 /* Reset/increment for the next run. */
9095 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9096 it->current_x = line_start_x;
9097 line_start_x = 0;
9098 it->hpos = 0;
9099 it->current_y += it->max_ascent + it->max_descent;
9100 ++it->vpos;
9101 last_height = it->max_ascent + it->max_descent;
9102 it->max_ascent = it->max_descent = 0;
9103 }
9104
9105 out:
9106
9107 /* On text terminals, we may stop at the end of a line in the middle
9108 of a multi-character glyph. If the glyph itself is continued,
9109 i.e. it is actually displayed on the next line, don't treat this
9110 stopping point as valid; move to the next line instead (unless
9111 that brings us offscreen). */
9112 if (!FRAME_WINDOW_P (it->f)
9113 && op & MOVE_TO_POS
9114 && IT_CHARPOS (*it) == to_charpos
9115 && it->what == IT_CHARACTER
9116 && it->nglyphs > 1
9117 && it->line_wrap == WINDOW_WRAP
9118 && it->current_x == it->last_visible_x - 1
9119 && it->c != '\n'
9120 && it->c != '\t'
9121 && it->vpos < it->w->window_end_vpos)
9122 {
9123 it->continuation_lines_width += it->current_x;
9124 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9125 it->current_y += it->max_ascent + it->max_descent;
9126 ++it->vpos;
9127 last_height = it->max_ascent + it->max_descent;
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 both, if present, 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 if (NILP (x_limit))
9581 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9582 else
9583 {
9584 CHECK_NUMBER (x_limit);
9585 it.last_visible_x = min (XINT (x_limit), INFINITY);
9586 /* Actually, we never want move_it_to stop at to_x. But to make
9587 sure that move_it_in_display_line_to always moves far enough,
9588 we set it to INT_MAX and specify MOVE_TO_X. */
9589 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9590 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9591 }
9592
9593 y = it.current_y + it.max_ascent + it.max_descent;
9594
9595 if (!EQ (mode_and_header_line, Qheader_line)
9596 && !EQ (mode_and_header_line, Qt))
9597 /* Do not count the header-line which was counted automatically by
9598 start_display. */
9599 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9600
9601 if (EQ (mode_and_header_line, Qmode_line)
9602 || EQ (mode_and_header_line, Qt))
9603 /* Do count the mode-line which is not included automatically by
9604 start_display. */
9605 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9606
9607 bidi_unshelve_cache (itdata, 0);
9608
9609 if (old_buffer)
9610 set_buffer_internal (old_buffer);
9611
9612 return Fcons (make_number (x), make_number (y));
9613 }
9614 \f
9615 /***********************************************************************
9616 Messages
9617 ***********************************************************************/
9618
9619
9620 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9621 to *Messages*. */
9622
9623 void
9624 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9625 {
9626 Lisp_Object args[3];
9627 Lisp_Object msg, fmt;
9628 char *buffer;
9629 ptrdiff_t len;
9630 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9631 USE_SAFE_ALLOCA;
9632
9633 fmt = msg = Qnil;
9634 GCPRO4 (fmt, msg, arg1, arg2);
9635
9636 args[0] = fmt = build_string (format);
9637 args[1] = arg1;
9638 args[2] = arg2;
9639 msg = Fformat (3, args);
9640
9641 len = SBYTES (msg) + 1;
9642 buffer = SAFE_ALLOCA (len);
9643 memcpy (buffer, SDATA (msg), len);
9644
9645 message_dolog (buffer, len - 1, 1, 0);
9646 SAFE_FREE ();
9647
9648 UNGCPRO;
9649 }
9650
9651
9652 /* Output a newline in the *Messages* buffer if "needs" one. */
9653
9654 void
9655 message_log_maybe_newline (void)
9656 {
9657 if (message_log_need_newline)
9658 message_dolog ("", 0, 1, 0);
9659 }
9660
9661
9662 /* Add a string M of length NBYTES to the message log, optionally
9663 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9664 true, means interpret the contents of M as multibyte. This
9665 function calls low-level routines in order to bypass text property
9666 hooks, etc. which might not be safe to run.
9667
9668 This may GC (insert may run before/after change hooks),
9669 so the buffer M must NOT point to a Lisp string. */
9670
9671 void
9672 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9673 {
9674 const unsigned char *msg = (const unsigned char *) m;
9675
9676 if (!NILP (Vmemory_full))
9677 return;
9678
9679 if (!NILP (Vmessage_log_max))
9680 {
9681 struct buffer *oldbuf;
9682 Lisp_Object oldpoint, oldbegv, oldzv;
9683 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9684 ptrdiff_t point_at_end = 0;
9685 ptrdiff_t zv_at_end = 0;
9686 Lisp_Object old_deactivate_mark;
9687 struct gcpro gcpro1;
9688
9689 old_deactivate_mark = Vdeactivate_mark;
9690 oldbuf = current_buffer;
9691
9692 /* Ensure the Messages buffer exists, and switch to it.
9693 If we created it, set the major-mode. */
9694 {
9695 int newbuffer = 0;
9696 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9697
9698 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9699
9700 if (newbuffer
9701 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9702 call0 (intern ("messages-buffer-mode"));
9703 }
9704
9705 bset_undo_list (current_buffer, Qt);
9706 bset_cache_long_scans (current_buffer, Qnil);
9707
9708 oldpoint = message_dolog_marker1;
9709 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9710 oldbegv = message_dolog_marker2;
9711 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9712 oldzv = message_dolog_marker3;
9713 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9714 GCPRO1 (old_deactivate_mark);
9715
9716 if (PT == Z)
9717 point_at_end = 1;
9718 if (ZV == Z)
9719 zv_at_end = 1;
9720
9721 BEGV = BEG;
9722 BEGV_BYTE = BEG_BYTE;
9723 ZV = Z;
9724 ZV_BYTE = Z_BYTE;
9725 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9726
9727 /* Insert the string--maybe converting multibyte to single byte
9728 or vice versa, so that all the text fits the buffer. */
9729 if (multibyte
9730 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9731 {
9732 ptrdiff_t i;
9733 int c, char_bytes;
9734 char work[1];
9735
9736 /* Convert a multibyte string to single-byte
9737 for the *Message* buffer. */
9738 for (i = 0; i < nbytes; i += char_bytes)
9739 {
9740 c = string_char_and_length (msg + i, &char_bytes);
9741 work[0] = (ASCII_CHAR_P (c)
9742 ? c
9743 : multibyte_char_to_unibyte (c));
9744 insert_1_both (work, 1, 1, 1, 0, 0);
9745 }
9746 }
9747 else if (! multibyte
9748 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9749 {
9750 ptrdiff_t i;
9751 int c, char_bytes;
9752 unsigned char str[MAX_MULTIBYTE_LENGTH];
9753 /* Convert a single-byte string to multibyte
9754 for the *Message* buffer. */
9755 for (i = 0; i < nbytes; i++)
9756 {
9757 c = msg[i];
9758 MAKE_CHAR_MULTIBYTE (c);
9759 char_bytes = CHAR_STRING (c, str);
9760 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9761 }
9762 }
9763 else if (nbytes)
9764 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9765
9766 if (nlflag)
9767 {
9768 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9769 printmax_t dups;
9770
9771 insert_1_both ("\n", 1, 1, 1, 0, 0);
9772
9773 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9774 this_bol = PT;
9775 this_bol_byte = PT_BYTE;
9776
9777 /* See if this line duplicates the previous one.
9778 If so, combine duplicates. */
9779 if (this_bol > BEG)
9780 {
9781 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9782 prev_bol = PT;
9783 prev_bol_byte = PT_BYTE;
9784
9785 dups = message_log_check_duplicate (prev_bol_byte,
9786 this_bol_byte);
9787 if (dups)
9788 {
9789 del_range_both (prev_bol, prev_bol_byte,
9790 this_bol, this_bol_byte, 0);
9791 if (dups > 1)
9792 {
9793 char dupstr[sizeof " [ times]"
9794 + INT_STRLEN_BOUND (printmax_t)];
9795
9796 /* If you change this format, don't forget to also
9797 change message_log_check_duplicate. */
9798 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9799 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9800 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9801 }
9802 }
9803 }
9804
9805 /* If we have more than the desired maximum number of lines
9806 in the *Messages* buffer now, delete the oldest ones.
9807 This is safe because we don't have undo in this buffer. */
9808
9809 if (NATNUMP (Vmessage_log_max))
9810 {
9811 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9812 -XFASTINT (Vmessage_log_max) - 1, 0);
9813 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9814 }
9815 }
9816 BEGV = marker_position (oldbegv);
9817 BEGV_BYTE = marker_byte_position (oldbegv);
9818
9819 if (zv_at_end)
9820 {
9821 ZV = Z;
9822 ZV_BYTE = Z_BYTE;
9823 }
9824 else
9825 {
9826 ZV = marker_position (oldzv);
9827 ZV_BYTE = marker_byte_position (oldzv);
9828 }
9829
9830 if (point_at_end)
9831 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9832 else
9833 /* We can't do Fgoto_char (oldpoint) because it will run some
9834 Lisp code. */
9835 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9836 marker_byte_position (oldpoint));
9837
9838 UNGCPRO;
9839 unchain_marker (XMARKER (oldpoint));
9840 unchain_marker (XMARKER (oldbegv));
9841 unchain_marker (XMARKER (oldzv));
9842
9843 /* We called insert_1_both above with its 5th argument (PREPARE)
9844 zero, which prevents insert_1_both from calling
9845 prepare_to_modify_buffer, which in turns prevents us from
9846 incrementing windows_or_buffers_changed even if *Messages* is
9847 shown in some window. So we must manually set
9848 windows_or_buffers_changed here to make up for that. */
9849 windows_or_buffers_changed = old_windows_or_buffers_changed;
9850 bset_redisplay (current_buffer);
9851
9852 set_buffer_internal (oldbuf);
9853
9854 message_log_need_newline = !nlflag;
9855 Vdeactivate_mark = old_deactivate_mark;
9856 }
9857 }
9858
9859
9860 /* We are at the end of the buffer after just having inserted a newline.
9861 (Note: We depend on the fact we won't be crossing the gap.)
9862 Check to see if the most recent message looks a lot like the previous one.
9863 Return 0 if different, 1 if the new one should just replace it, or a
9864 value N > 1 if we should also append " [N times]". */
9865
9866 static intmax_t
9867 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9868 {
9869 ptrdiff_t i;
9870 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9871 int seen_dots = 0;
9872 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9873 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9874
9875 for (i = 0; i < len; i++)
9876 {
9877 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9878 seen_dots = 1;
9879 if (p1[i] != p2[i])
9880 return seen_dots;
9881 }
9882 p1 += len;
9883 if (*p1 == '\n')
9884 return 2;
9885 if (*p1++ == ' ' && *p1++ == '[')
9886 {
9887 char *pend;
9888 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9889 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9890 return n + 1;
9891 }
9892 return 0;
9893 }
9894 \f
9895
9896 /* Display an echo area message M with a specified length of NBYTES
9897 bytes. The string may include null characters. If M is not a
9898 string, clear out any existing message, and let the mini-buffer
9899 text show through.
9900
9901 This function cancels echoing. */
9902
9903 void
9904 message3 (Lisp_Object m)
9905 {
9906 struct gcpro gcpro1;
9907
9908 GCPRO1 (m);
9909 clear_message (true, true);
9910 cancel_echoing ();
9911
9912 /* First flush out any partial line written with print. */
9913 message_log_maybe_newline ();
9914 if (STRINGP (m))
9915 {
9916 ptrdiff_t nbytes = SBYTES (m);
9917 bool multibyte = STRING_MULTIBYTE (m);
9918 USE_SAFE_ALLOCA;
9919 char *buffer = SAFE_ALLOCA (nbytes);
9920 memcpy (buffer, SDATA (m), nbytes);
9921 message_dolog (buffer, nbytes, 1, multibyte);
9922 SAFE_FREE ();
9923 }
9924 message3_nolog (m);
9925
9926 UNGCPRO;
9927 }
9928
9929
9930 /* The non-logging version of message3.
9931 This does not cancel echoing, because it is used for echoing.
9932 Perhaps we need to make a separate function for echoing
9933 and make this cancel echoing. */
9934
9935 void
9936 message3_nolog (Lisp_Object m)
9937 {
9938 struct frame *sf = SELECTED_FRAME ();
9939
9940 if (FRAME_INITIAL_P (sf))
9941 {
9942 if (noninteractive_need_newline)
9943 putc ('\n', stderr);
9944 noninteractive_need_newline = 0;
9945 if (STRINGP (m))
9946 {
9947 Lisp_Object s = ENCODE_SYSTEM (m);
9948
9949 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9950 }
9951 if (cursor_in_echo_area == 0)
9952 fprintf (stderr, "\n");
9953 fflush (stderr);
9954 }
9955 /* Error messages get reported properly by cmd_error, so this must be just an
9956 informative message; if the frame hasn't really been initialized yet, just
9957 toss it. */
9958 else if (INTERACTIVE && sf->glyphs_initialized_p)
9959 {
9960 /* Get the frame containing the mini-buffer
9961 that the selected frame is using. */
9962 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9963 Lisp_Object frame = XWINDOW (mini_window)->frame;
9964 struct frame *f = XFRAME (frame);
9965
9966 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9967 Fmake_frame_visible (frame);
9968
9969 if (STRINGP (m) && SCHARS (m) > 0)
9970 {
9971 set_message (m);
9972 if (minibuffer_auto_raise)
9973 Fraise_frame (frame);
9974 /* Assume we are not echoing.
9975 (If we are, echo_now will override this.) */
9976 echo_message_buffer = Qnil;
9977 }
9978 else
9979 clear_message (true, true);
9980
9981 do_pending_window_change (0);
9982 echo_area_display (1);
9983 do_pending_window_change (0);
9984 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9985 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9986 }
9987 }
9988
9989
9990 /* Display a null-terminated echo area message M. If M is 0, clear
9991 out any existing message, and let the mini-buffer text show through.
9992
9993 The buffer M must continue to exist until after the echo area gets
9994 cleared or some other message gets displayed there. Do not pass
9995 text that is stored in a Lisp string. Do not pass text in a buffer
9996 that was alloca'd. */
9997
9998 void
9999 message1 (const char *m)
10000 {
10001 message3 (m ? build_unibyte_string (m) : Qnil);
10002 }
10003
10004
10005 /* The non-logging counterpart of message1. */
10006
10007 void
10008 message1_nolog (const char *m)
10009 {
10010 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10011 }
10012
10013 /* Display a message M which contains a single %s
10014 which gets replaced with STRING. */
10015
10016 void
10017 message_with_string (const char *m, Lisp_Object string, int log)
10018 {
10019 CHECK_STRING (string);
10020
10021 if (noninteractive)
10022 {
10023 if (m)
10024 {
10025 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10026 String whose data pointer might be passed to us in M. So
10027 we use a local copy. */
10028 char *fmt = xstrdup (m);
10029
10030 if (noninteractive_need_newline)
10031 putc ('\n', stderr);
10032 noninteractive_need_newline = 0;
10033 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10034 if (!cursor_in_echo_area)
10035 fprintf (stderr, "\n");
10036 fflush (stderr);
10037 xfree (fmt);
10038 }
10039 }
10040 else if (INTERACTIVE)
10041 {
10042 /* The frame whose minibuffer we're going to display the message on.
10043 It may be larger than the selected frame, so we need
10044 to use its buffer, not the selected frame's buffer. */
10045 Lisp_Object mini_window;
10046 struct frame *f, *sf = SELECTED_FRAME ();
10047
10048 /* Get the frame containing the minibuffer
10049 that the selected frame is using. */
10050 mini_window = FRAME_MINIBUF_WINDOW (sf);
10051 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10052
10053 /* Error messages get reported properly by cmd_error, so this must be
10054 just an informative message; if the frame hasn't really been
10055 initialized yet, just toss it. */
10056 if (f->glyphs_initialized_p)
10057 {
10058 Lisp_Object args[2], msg;
10059 struct gcpro gcpro1, gcpro2;
10060
10061 args[0] = build_string (m);
10062 args[1] = msg = string;
10063 GCPRO2 (args[0], msg);
10064 gcpro1.nvars = 2;
10065
10066 msg = Fformat (2, args);
10067
10068 if (log)
10069 message3 (msg);
10070 else
10071 message3_nolog (msg);
10072
10073 UNGCPRO;
10074
10075 /* Print should start at the beginning of the message
10076 buffer next time. */
10077 message_buf_print = 0;
10078 }
10079 }
10080 }
10081
10082
10083 /* Dump an informative message to the minibuf. If M is 0, clear out
10084 any existing message, and let the mini-buffer text show through. */
10085
10086 static void
10087 vmessage (const char *m, va_list ap)
10088 {
10089 if (noninteractive)
10090 {
10091 if (m)
10092 {
10093 if (noninteractive_need_newline)
10094 putc ('\n', stderr);
10095 noninteractive_need_newline = 0;
10096 vfprintf (stderr, m, ap);
10097 if (cursor_in_echo_area == 0)
10098 fprintf (stderr, "\n");
10099 fflush (stderr);
10100 }
10101 }
10102 else if (INTERACTIVE)
10103 {
10104 /* The frame whose mini-buffer we're going to display the message
10105 on. It may be larger than the selected frame, so we need to
10106 use its buffer, not the selected frame's buffer. */
10107 Lisp_Object mini_window;
10108 struct frame *f, *sf = SELECTED_FRAME ();
10109
10110 /* Get the frame containing the mini-buffer
10111 that the selected frame is using. */
10112 mini_window = FRAME_MINIBUF_WINDOW (sf);
10113 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10114
10115 /* Error messages get reported properly by cmd_error, so this must be
10116 just an informative message; if the frame hasn't really been
10117 initialized yet, just toss it. */
10118 if (f->glyphs_initialized_p)
10119 {
10120 if (m)
10121 {
10122 ptrdiff_t len;
10123 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10124 char *message_buf = alloca (maxsize + 1);
10125
10126 len = doprnt (message_buf, maxsize, m, 0, ap);
10127
10128 message3 (make_string (message_buf, len));
10129 }
10130 else
10131 message1 (0);
10132
10133 /* Print should start at the beginning of the message
10134 buffer next time. */
10135 message_buf_print = 0;
10136 }
10137 }
10138 }
10139
10140 void
10141 message (const char *m, ...)
10142 {
10143 va_list ap;
10144 va_start (ap, m);
10145 vmessage (m, ap);
10146 va_end (ap);
10147 }
10148
10149
10150 #if 0
10151 /* The non-logging version of message. */
10152
10153 void
10154 message_nolog (const char *m, ...)
10155 {
10156 Lisp_Object old_log_max;
10157 va_list ap;
10158 va_start (ap, m);
10159 old_log_max = Vmessage_log_max;
10160 Vmessage_log_max = Qnil;
10161 vmessage (m, ap);
10162 Vmessage_log_max = old_log_max;
10163 va_end (ap);
10164 }
10165 #endif
10166
10167
10168 /* Display the current message in the current mini-buffer. This is
10169 only called from error handlers in process.c, and is not time
10170 critical. */
10171
10172 void
10173 update_echo_area (void)
10174 {
10175 if (!NILP (echo_area_buffer[0]))
10176 {
10177 Lisp_Object string;
10178 string = Fcurrent_message ();
10179 message3 (string);
10180 }
10181 }
10182
10183
10184 /* Make sure echo area buffers in `echo_buffers' are live.
10185 If they aren't, make new ones. */
10186
10187 static void
10188 ensure_echo_area_buffers (void)
10189 {
10190 int i;
10191
10192 for (i = 0; i < 2; ++i)
10193 if (!BUFFERP (echo_buffer[i])
10194 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10195 {
10196 char name[30];
10197 Lisp_Object old_buffer;
10198 int j;
10199
10200 old_buffer = echo_buffer[i];
10201 echo_buffer[i] = Fget_buffer_create
10202 (make_formatted_string (name, " *Echo Area %d*", i));
10203 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10204 /* to force word wrap in echo area -
10205 it was decided to postpone this*/
10206 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10207
10208 for (j = 0; j < 2; ++j)
10209 if (EQ (old_buffer, echo_area_buffer[j]))
10210 echo_area_buffer[j] = echo_buffer[i];
10211 }
10212 }
10213
10214
10215 /* Call FN with args A1..A2 with either the current or last displayed
10216 echo_area_buffer as current buffer.
10217
10218 WHICH zero means use the current message buffer
10219 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10220 from echo_buffer[] and clear it.
10221
10222 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10223 suitable buffer from echo_buffer[] and clear it.
10224
10225 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10226 that the current message becomes the last displayed one, make
10227 choose a suitable buffer for echo_area_buffer[0], and clear it.
10228
10229 Value is what FN returns. */
10230
10231 static int
10232 with_echo_area_buffer (struct window *w, int which,
10233 int (*fn) (ptrdiff_t, Lisp_Object),
10234 ptrdiff_t a1, Lisp_Object a2)
10235 {
10236 Lisp_Object buffer;
10237 int this_one, the_other, clear_buffer_p, rc;
10238 ptrdiff_t count = SPECPDL_INDEX ();
10239
10240 /* If buffers aren't live, make new ones. */
10241 ensure_echo_area_buffers ();
10242
10243 clear_buffer_p = 0;
10244
10245 if (which == 0)
10246 this_one = 0, the_other = 1;
10247 else if (which > 0)
10248 this_one = 1, the_other = 0;
10249 else
10250 {
10251 this_one = 0, the_other = 1;
10252 clear_buffer_p = true;
10253
10254 /* We need a fresh one in case the current echo buffer equals
10255 the one containing the last displayed echo area message. */
10256 if (!NILP (echo_area_buffer[this_one])
10257 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10258 echo_area_buffer[this_one] = Qnil;
10259 }
10260
10261 /* Choose a suitable buffer from echo_buffer[] is we don't
10262 have one. */
10263 if (NILP (echo_area_buffer[this_one]))
10264 {
10265 echo_area_buffer[this_one]
10266 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10267 ? echo_buffer[the_other]
10268 : echo_buffer[this_one]);
10269 clear_buffer_p = true;
10270 }
10271
10272 buffer = echo_area_buffer[this_one];
10273
10274 /* Don't get confused by reusing the buffer used for echoing
10275 for a different purpose. */
10276 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10277 cancel_echoing ();
10278
10279 record_unwind_protect (unwind_with_echo_area_buffer,
10280 with_echo_area_buffer_unwind_data (w));
10281
10282 /* Make the echo area buffer current. Note that for display
10283 purposes, it is not necessary that the displayed window's buffer
10284 == current_buffer, except for text property lookup. So, let's
10285 only set that buffer temporarily here without doing a full
10286 Fset_window_buffer. We must also change w->pointm, though,
10287 because otherwise an assertions in unshow_buffer fails, and Emacs
10288 aborts. */
10289 set_buffer_internal_1 (XBUFFER (buffer));
10290 if (w)
10291 {
10292 wset_buffer (w, buffer);
10293 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10294 }
10295
10296 bset_undo_list (current_buffer, Qt);
10297 bset_read_only (current_buffer, Qnil);
10298 specbind (Qinhibit_read_only, Qt);
10299 specbind (Qinhibit_modification_hooks, Qt);
10300
10301 if (clear_buffer_p && Z > BEG)
10302 del_range (BEG, Z);
10303
10304 eassert (BEGV >= BEG);
10305 eassert (ZV <= Z && ZV >= BEGV);
10306
10307 rc = fn (a1, a2);
10308
10309 eassert (BEGV >= BEG);
10310 eassert (ZV <= Z && ZV >= BEGV);
10311
10312 unbind_to (count, Qnil);
10313 return rc;
10314 }
10315
10316
10317 /* Save state that should be preserved around the call to the function
10318 FN called in with_echo_area_buffer. */
10319
10320 static Lisp_Object
10321 with_echo_area_buffer_unwind_data (struct window *w)
10322 {
10323 int i = 0;
10324 Lisp_Object vector, tmp;
10325
10326 /* Reduce consing by keeping one vector in
10327 Vwith_echo_area_save_vector. */
10328 vector = Vwith_echo_area_save_vector;
10329 Vwith_echo_area_save_vector = Qnil;
10330
10331 if (NILP (vector))
10332 vector = Fmake_vector (make_number (9), Qnil);
10333
10334 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10335 ASET (vector, i, Vdeactivate_mark); ++i;
10336 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10337
10338 if (w)
10339 {
10340 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10341 ASET (vector, i, w->contents); ++i;
10342 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10343 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10344 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10345 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10346 }
10347 else
10348 {
10349 int end = i + 6;
10350 for (; i < end; ++i)
10351 ASET (vector, i, Qnil);
10352 }
10353
10354 eassert (i == ASIZE (vector));
10355 return vector;
10356 }
10357
10358
10359 /* Restore global state from VECTOR which was created by
10360 with_echo_area_buffer_unwind_data. */
10361
10362 static void
10363 unwind_with_echo_area_buffer (Lisp_Object vector)
10364 {
10365 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10366 Vdeactivate_mark = AREF (vector, 1);
10367 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10368
10369 if (WINDOWP (AREF (vector, 3)))
10370 {
10371 struct window *w;
10372 Lisp_Object buffer;
10373
10374 w = XWINDOW (AREF (vector, 3));
10375 buffer = AREF (vector, 4);
10376
10377 wset_buffer (w, buffer);
10378 set_marker_both (w->pointm, buffer,
10379 XFASTINT (AREF (vector, 5)),
10380 XFASTINT (AREF (vector, 6)));
10381 set_marker_both (w->start, buffer,
10382 XFASTINT (AREF (vector, 7)),
10383 XFASTINT (AREF (vector, 8)));
10384 }
10385
10386 Vwith_echo_area_save_vector = vector;
10387 }
10388
10389
10390 /* Set up the echo area for use by print functions. MULTIBYTE_P
10391 non-zero means we will print multibyte. */
10392
10393 void
10394 setup_echo_area_for_printing (int multibyte_p)
10395 {
10396 /* If we can't find an echo area any more, exit. */
10397 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10398 Fkill_emacs (Qnil);
10399
10400 ensure_echo_area_buffers ();
10401
10402 if (!message_buf_print)
10403 {
10404 /* A message has been output since the last time we printed.
10405 Choose a fresh echo area buffer. */
10406 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10407 echo_area_buffer[0] = echo_buffer[1];
10408 else
10409 echo_area_buffer[0] = echo_buffer[0];
10410
10411 /* Switch to that buffer and clear it. */
10412 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10413 bset_truncate_lines (current_buffer, Qnil);
10414
10415 if (Z > BEG)
10416 {
10417 ptrdiff_t count = SPECPDL_INDEX ();
10418 specbind (Qinhibit_read_only, Qt);
10419 /* Note that undo recording is always disabled. */
10420 del_range (BEG, Z);
10421 unbind_to (count, Qnil);
10422 }
10423 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10424
10425 /* Set up the buffer for the multibyteness we need. */
10426 if (multibyte_p
10427 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10428 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10429
10430 /* Raise the frame containing the echo area. */
10431 if (minibuffer_auto_raise)
10432 {
10433 struct frame *sf = SELECTED_FRAME ();
10434 Lisp_Object mini_window;
10435 mini_window = FRAME_MINIBUF_WINDOW (sf);
10436 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10437 }
10438
10439 message_log_maybe_newline ();
10440 message_buf_print = 1;
10441 }
10442 else
10443 {
10444 if (NILP (echo_area_buffer[0]))
10445 {
10446 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10447 echo_area_buffer[0] = echo_buffer[1];
10448 else
10449 echo_area_buffer[0] = echo_buffer[0];
10450 }
10451
10452 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10453 {
10454 /* Someone switched buffers between print requests. */
10455 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10456 bset_truncate_lines (current_buffer, Qnil);
10457 }
10458 }
10459 }
10460
10461
10462 /* Display an echo area message in window W. Value is non-zero if W's
10463 height is changed. If display_last_displayed_message_p is
10464 non-zero, display the message that was last displayed, otherwise
10465 display the current message. */
10466
10467 static int
10468 display_echo_area (struct window *w)
10469 {
10470 int i, no_message_p, window_height_changed_p;
10471
10472 /* Temporarily disable garbage collections while displaying the echo
10473 area. This is done because a GC can print a message itself.
10474 That message would modify the echo area buffer's contents while a
10475 redisplay of the buffer is going on, and seriously confuse
10476 redisplay. */
10477 ptrdiff_t count = inhibit_garbage_collection ();
10478
10479 /* If there is no message, we must call display_echo_area_1
10480 nevertheless because it resizes the window. But we will have to
10481 reset the echo_area_buffer in question to nil at the end because
10482 with_echo_area_buffer will sets it to an empty buffer. */
10483 i = display_last_displayed_message_p ? 1 : 0;
10484 no_message_p = NILP (echo_area_buffer[i]);
10485
10486 window_height_changed_p
10487 = with_echo_area_buffer (w, display_last_displayed_message_p,
10488 display_echo_area_1,
10489 (intptr_t) w, Qnil);
10490
10491 if (no_message_p)
10492 echo_area_buffer[i] = Qnil;
10493
10494 unbind_to (count, Qnil);
10495 return window_height_changed_p;
10496 }
10497
10498
10499 /* Helper for display_echo_area. Display the current buffer which
10500 contains the current echo area message in window W, a mini-window,
10501 a pointer to which is passed in A1. A2..A4 are currently not used.
10502 Change the height of W so that all of the message is displayed.
10503 Value is non-zero if height of W was changed. */
10504
10505 static int
10506 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10507 {
10508 intptr_t i1 = a1;
10509 struct window *w = (struct window *) i1;
10510 Lisp_Object window;
10511 struct text_pos start;
10512 int window_height_changed_p = 0;
10513
10514 /* Do this before displaying, so that we have a large enough glyph
10515 matrix for the display. If we can't get enough space for the
10516 whole text, display the last N lines. That works by setting w->start. */
10517 window_height_changed_p = resize_mini_window (w, 0);
10518
10519 /* Use the starting position chosen by resize_mini_window. */
10520 SET_TEXT_POS_FROM_MARKER (start, w->start);
10521
10522 /* Display. */
10523 clear_glyph_matrix (w->desired_matrix);
10524 XSETWINDOW (window, w);
10525 try_window (window, start, 0);
10526
10527 return window_height_changed_p;
10528 }
10529
10530
10531 /* Resize the echo area window to exactly the size needed for the
10532 currently displayed message, if there is one. If a mini-buffer
10533 is active, don't shrink it. */
10534
10535 void
10536 resize_echo_area_exactly (void)
10537 {
10538 if (BUFFERP (echo_area_buffer[0])
10539 && WINDOWP (echo_area_window))
10540 {
10541 struct window *w = XWINDOW (echo_area_window);
10542 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10543 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10544 (intptr_t) w, resize_exactly);
10545 if (resized_p)
10546 {
10547 windows_or_buffers_changed = 42;
10548 update_mode_lines = 30;
10549 redisplay_internal ();
10550 }
10551 }
10552 }
10553
10554
10555 /* Callback function for with_echo_area_buffer, when used from
10556 resize_echo_area_exactly. A1 contains a pointer to the window to
10557 resize, EXACTLY non-nil means resize the mini-window exactly to the
10558 size of the text displayed. A3 and A4 are not used. Value is what
10559 resize_mini_window returns. */
10560
10561 static int
10562 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10563 {
10564 intptr_t i1 = a1;
10565 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10566 }
10567
10568
10569 /* Resize mini-window W to fit the size of its contents. EXACT_P
10570 means size the window exactly to the size needed. Otherwise, it's
10571 only enlarged until W's buffer is empty.
10572
10573 Set W->start to the right place to begin display. If the whole
10574 contents fit, start at the beginning. Otherwise, start so as
10575 to make the end of the contents appear. This is particularly
10576 important for y-or-n-p, but seems desirable generally.
10577
10578 Value is non-zero if the window height has been changed. */
10579
10580 int
10581 resize_mini_window (struct window *w, int exact_p)
10582 {
10583 struct frame *f = XFRAME (w->frame);
10584 int window_height_changed_p = 0;
10585
10586 eassert (MINI_WINDOW_P (w));
10587
10588 /* By default, start display at the beginning. */
10589 set_marker_both (w->start, w->contents,
10590 BUF_BEGV (XBUFFER (w->contents)),
10591 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10592
10593 /* Don't resize windows while redisplaying a window; it would
10594 confuse redisplay functions when the size of the window they are
10595 displaying changes from under them. Such a resizing can happen,
10596 for instance, when which-func prints a long message while
10597 we are running fontification-functions. We're running these
10598 functions with safe_call which binds inhibit-redisplay to t. */
10599 if (!NILP (Vinhibit_redisplay))
10600 return 0;
10601
10602 /* Nil means don't try to resize. */
10603 if (NILP (Vresize_mini_windows)
10604 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10605 return 0;
10606
10607 if (!FRAME_MINIBUF_ONLY_P (f))
10608 {
10609 struct it it;
10610 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10611 + WINDOW_PIXEL_HEIGHT (w));
10612 int unit = FRAME_LINE_HEIGHT (f);
10613 int height, max_height;
10614 struct text_pos start;
10615 struct buffer *old_current_buffer = NULL;
10616
10617 if (current_buffer != XBUFFER (w->contents))
10618 {
10619 old_current_buffer = current_buffer;
10620 set_buffer_internal (XBUFFER (w->contents));
10621 }
10622
10623 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10624
10625 /* Compute the max. number of lines specified by the user. */
10626 if (FLOATP (Vmax_mini_window_height))
10627 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10628 else if (INTEGERP (Vmax_mini_window_height))
10629 max_height = XINT (Vmax_mini_window_height) * unit;
10630 else
10631 max_height = total_height / 4;
10632
10633 /* Correct that max. height if it's bogus. */
10634 max_height = clip_to_bounds (unit, max_height, total_height);
10635
10636 /* Find out the height of the text in the window. */
10637 if (it.line_wrap == TRUNCATE)
10638 height = unit;
10639 else
10640 {
10641 last_height = 0;
10642 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10643 if (it.max_ascent == 0 && it.max_descent == 0)
10644 height = it.current_y + last_height;
10645 else
10646 height = it.current_y + it.max_ascent + it.max_descent;
10647 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10648 }
10649
10650 /* Compute a suitable window start. */
10651 if (height > max_height)
10652 {
10653 height = (max_height / unit) * unit;
10654 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10655 move_it_vertically_backward (&it, height - unit);
10656 start = it.current.pos;
10657 }
10658 else
10659 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10660 SET_MARKER_FROM_TEXT_POS (w->start, start);
10661
10662 if (EQ (Vresize_mini_windows, Qgrow_only))
10663 {
10664 /* Let it grow only, until we display an empty message, in which
10665 case the window shrinks again. */
10666 if (height > WINDOW_PIXEL_HEIGHT (w))
10667 {
10668 int old_height = WINDOW_PIXEL_HEIGHT (w);
10669
10670 FRAME_WINDOWS_FROZEN (f) = 1;
10671 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10672 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10673 }
10674 else if (height < WINDOW_PIXEL_HEIGHT (w)
10675 && (exact_p || BEGV == ZV))
10676 {
10677 int old_height = WINDOW_PIXEL_HEIGHT (w);
10678
10679 FRAME_WINDOWS_FROZEN (f) = 0;
10680 shrink_mini_window (w, 1);
10681 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10682 }
10683 }
10684 else
10685 {
10686 /* Always resize to exact size needed. */
10687 if (height > WINDOW_PIXEL_HEIGHT (w))
10688 {
10689 int old_height = WINDOW_PIXEL_HEIGHT (w);
10690
10691 FRAME_WINDOWS_FROZEN (f) = 1;
10692 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10693 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10694 }
10695 else if (height < WINDOW_PIXEL_HEIGHT (w))
10696 {
10697 int old_height = WINDOW_PIXEL_HEIGHT (w);
10698
10699 FRAME_WINDOWS_FROZEN (f) = 0;
10700 shrink_mini_window (w, 1);
10701
10702 if (height)
10703 {
10704 FRAME_WINDOWS_FROZEN (f) = 1;
10705 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10706 }
10707
10708 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10709 }
10710 }
10711
10712 if (old_current_buffer)
10713 set_buffer_internal (old_current_buffer);
10714 }
10715
10716 return window_height_changed_p;
10717 }
10718
10719
10720 /* Value is the current message, a string, or nil if there is no
10721 current message. */
10722
10723 Lisp_Object
10724 current_message (void)
10725 {
10726 Lisp_Object msg;
10727
10728 if (!BUFFERP (echo_area_buffer[0]))
10729 msg = Qnil;
10730 else
10731 {
10732 with_echo_area_buffer (0, 0, current_message_1,
10733 (intptr_t) &msg, Qnil);
10734 if (NILP (msg))
10735 echo_area_buffer[0] = Qnil;
10736 }
10737
10738 return msg;
10739 }
10740
10741
10742 static int
10743 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10744 {
10745 intptr_t i1 = a1;
10746 Lisp_Object *msg = (Lisp_Object *) i1;
10747
10748 if (Z > BEG)
10749 *msg = make_buffer_string (BEG, Z, 1);
10750 else
10751 *msg = Qnil;
10752 return 0;
10753 }
10754
10755
10756 /* Push the current message on Vmessage_stack for later restoration
10757 by restore_message. Value is non-zero if the current message isn't
10758 empty. This is a relatively infrequent operation, so it's not
10759 worth optimizing. */
10760
10761 bool
10762 push_message (void)
10763 {
10764 Lisp_Object msg = current_message ();
10765 Vmessage_stack = Fcons (msg, Vmessage_stack);
10766 return STRINGP (msg);
10767 }
10768
10769
10770 /* Restore message display from the top of Vmessage_stack. */
10771
10772 void
10773 restore_message (void)
10774 {
10775 eassert (CONSP (Vmessage_stack));
10776 message3_nolog (XCAR (Vmessage_stack));
10777 }
10778
10779
10780 /* Handler for unwind-protect calling pop_message. */
10781
10782 void
10783 pop_message_unwind (void)
10784 {
10785 /* Pop the top-most entry off Vmessage_stack. */
10786 eassert (CONSP (Vmessage_stack));
10787 Vmessage_stack = XCDR (Vmessage_stack);
10788 }
10789
10790
10791 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10792 exits. If the stack is not empty, we have a missing pop_message
10793 somewhere. */
10794
10795 void
10796 check_message_stack (void)
10797 {
10798 if (!NILP (Vmessage_stack))
10799 emacs_abort ();
10800 }
10801
10802
10803 /* Truncate to NCHARS what will be displayed in the echo area the next
10804 time we display it---but don't redisplay it now. */
10805
10806 void
10807 truncate_echo_area (ptrdiff_t nchars)
10808 {
10809 if (nchars == 0)
10810 echo_area_buffer[0] = Qnil;
10811 else if (!noninteractive
10812 && INTERACTIVE
10813 && !NILP (echo_area_buffer[0]))
10814 {
10815 struct frame *sf = SELECTED_FRAME ();
10816 /* Error messages get reported properly by cmd_error, so this must be
10817 just an informative message; if the frame hasn't really been
10818 initialized yet, just toss it. */
10819 if (sf->glyphs_initialized_p)
10820 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10821 }
10822 }
10823
10824
10825 /* Helper function for truncate_echo_area. Truncate the current
10826 message to at most NCHARS characters. */
10827
10828 static int
10829 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10830 {
10831 if (BEG + nchars < Z)
10832 del_range (BEG + nchars, Z);
10833 if (Z == BEG)
10834 echo_area_buffer[0] = Qnil;
10835 return 0;
10836 }
10837
10838 /* Set the current message to STRING. */
10839
10840 static void
10841 set_message (Lisp_Object string)
10842 {
10843 eassert (STRINGP (string));
10844
10845 message_enable_multibyte = STRING_MULTIBYTE (string);
10846
10847 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10848 message_buf_print = 0;
10849 help_echo_showing_p = 0;
10850
10851 if (STRINGP (Vdebug_on_message)
10852 && STRINGP (string)
10853 && fast_string_match (Vdebug_on_message, string) >= 0)
10854 call_debugger (list2 (Qerror, string));
10855 }
10856
10857
10858 /* Helper function for set_message. First argument is ignored and second
10859 argument has the same meaning as for set_message.
10860 This function is called with the echo area buffer being current. */
10861
10862 static int
10863 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10864 {
10865 eassert (STRINGP (string));
10866
10867 /* Change multibyteness of the echo buffer appropriately. */
10868 if (message_enable_multibyte
10869 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10870 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10871
10872 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10873 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10874 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10875
10876 /* Insert new message at BEG. */
10877 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10878
10879 /* This function takes care of single/multibyte conversion.
10880 We just have to ensure that the echo area buffer has the right
10881 setting of enable_multibyte_characters. */
10882 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10883
10884 return 0;
10885 }
10886
10887
10888 /* Clear messages. CURRENT_P non-zero means clear the current
10889 message. LAST_DISPLAYED_P non-zero means clear the message
10890 last displayed. */
10891
10892 void
10893 clear_message (bool current_p, bool last_displayed_p)
10894 {
10895 if (current_p)
10896 {
10897 echo_area_buffer[0] = Qnil;
10898 message_cleared_p = true;
10899 }
10900
10901 if (last_displayed_p)
10902 echo_area_buffer[1] = Qnil;
10903
10904 message_buf_print = 0;
10905 }
10906
10907 /* Clear garbaged frames.
10908
10909 This function is used where the old redisplay called
10910 redraw_garbaged_frames which in turn called redraw_frame which in
10911 turn called clear_frame. The call to clear_frame was a source of
10912 flickering. I believe a clear_frame is not necessary. It should
10913 suffice in the new redisplay to invalidate all current matrices,
10914 and ensure a complete redisplay of all windows. */
10915
10916 static void
10917 clear_garbaged_frames (void)
10918 {
10919 if (frame_garbaged)
10920 {
10921 Lisp_Object tail, frame;
10922
10923 FOR_EACH_FRAME (tail, frame)
10924 {
10925 struct frame *f = XFRAME (frame);
10926
10927 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10928 {
10929 if (f->resized_p)
10930 redraw_frame (f);
10931 else
10932 clear_current_matrices (f);
10933 fset_redisplay (f);
10934 f->garbaged = false;
10935 f->resized_p = false;
10936 }
10937 }
10938
10939 frame_garbaged = false;
10940 }
10941 }
10942
10943
10944 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10945 is non-zero update selected_frame. Value is non-zero if the
10946 mini-windows height has been changed. */
10947
10948 static int
10949 echo_area_display (int update_frame_p)
10950 {
10951 Lisp_Object mini_window;
10952 struct window *w;
10953 struct frame *f;
10954 int window_height_changed_p = 0;
10955 struct frame *sf = SELECTED_FRAME ();
10956
10957 mini_window = FRAME_MINIBUF_WINDOW (sf);
10958 w = XWINDOW (mini_window);
10959 f = XFRAME (WINDOW_FRAME (w));
10960
10961 /* Don't display if frame is invisible or not yet initialized. */
10962 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10963 return 0;
10964
10965 #ifdef HAVE_WINDOW_SYSTEM
10966 /* When Emacs starts, selected_frame may be the initial terminal
10967 frame. If we let this through, a message would be displayed on
10968 the terminal. */
10969 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10970 return 0;
10971 #endif /* HAVE_WINDOW_SYSTEM */
10972
10973 /* Redraw garbaged frames. */
10974 clear_garbaged_frames ();
10975
10976 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10977 {
10978 echo_area_window = mini_window;
10979 window_height_changed_p = display_echo_area (w);
10980 w->must_be_updated_p = true;
10981
10982 /* Update the display, unless called from redisplay_internal.
10983 Also don't update the screen during redisplay itself. The
10984 update will happen at the end of redisplay, and an update
10985 here could cause confusion. */
10986 if (update_frame_p && !redisplaying_p)
10987 {
10988 int n = 0;
10989
10990 /* If the display update has been interrupted by pending
10991 input, update mode lines in the frame. Due to the
10992 pending input, it might have been that redisplay hasn't
10993 been called, so that mode lines above the echo area are
10994 garbaged. This looks odd, so we prevent it here. */
10995 if (!display_completed)
10996 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
10997
10998 if (window_height_changed_p
10999 /* Don't do this if Emacs is shutting down. Redisplay
11000 needs to run hooks. */
11001 && !NILP (Vrun_hooks))
11002 {
11003 /* Must update other windows. Likewise as in other
11004 cases, don't let this update be interrupted by
11005 pending input. */
11006 ptrdiff_t count = SPECPDL_INDEX ();
11007 specbind (Qredisplay_dont_pause, Qt);
11008 windows_or_buffers_changed = 44;
11009 redisplay_internal ();
11010 unbind_to (count, Qnil);
11011 }
11012 else if (FRAME_WINDOW_P (f) && n == 0)
11013 {
11014 /* Window configuration is the same as before.
11015 Can do with a display update of the echo area,
11016 unless we displayed some mode lines. */
11017 update_single_window (w, 1);
11018 flush_frame (f);
11019 }
11020 else
11021 update_frame (f, 1, 1);
11022
11023 /* If cursor is in the echo area, make sure that the next
11024 redisplay displays the minibuffer, so that the cursor will
11025 be replaced with what the minibuffer wants. */
11026 if (cursor_in_echo_area)
11027 wset_redisplay (XWINDOW (mini_window));
11028 }
11029 }
11030 else if (!EQ (mini_window, selected_window))
11031 wset_redisplay (XWINDOW (mini_window));
11032
11033 /* Last displayed message is now the current message. */
11034 echo_area_buffer[1] = echo_area_buffer[0];
11035 /* Inform read_char that we're not echoing. */
11036 echo_message_buffer = Qnil;
11037
11038 /* Prevent redisplay optimization in redisplay_internal by resetting
11039 this_line_start_pos. This is done because the mini-buffer now
11040 displays the message instead of its buffer text. */
11041 if (EQ (mini_window, selected_window))
11042 CHARPOS (this_line_start_pos) = 0;
11043
11044 return window_height_changed_p;
11045 }
11046
11047 /* Nonzero if W's buffer was changed but not saved. */
11048
11049 static int
11050 window_buffer_changed (struct window *w)
11051 {
11052 struct buffer *b = XBUFFER (w->contents);
11053
11054 eassert (BUFFER_LIVE_P (b));
11055
11056 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11057 }
11058
11059 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11060
11061 static int
11062 mode_line_update_needed (struct window *w)
11063 {
11064 return (w->column_number_displayed != -1
11065 && !(PT == w->last_point && !window_outdated (w))
11066 && (w->column_number_displayed != current_column ()));
11067 }
11068
11069 /* Nonzero if window start of W is frozen and may not be changed during
11070 redisplay. */
11071
11072 static bool
11073 window_frozen_p (struct window *w)
11074 {
11075 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11076 {
11077 Lisp_Object window;
11078
11079 XSETWINDOW (window, w);
11080 if (MINI_WINDOW_P (w))
11081 return 0;
11082 else if (EQ (window, selected_window))
11083 return 0;
11084 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11085 && EQ (window, Vminibuf_scroll_window))
11086 /* This special window can't be frozen too. */
11087 return 0;
11088 else
11089 return 1;
11090 }
11091 return 0;
11092 }
11093
11094 /***********************************************************************
11095 Mode Lines and Frame Titles
11096 ***********************************************************************/
11097
11098 /* A buffer for constructing non-propertized mode-line strings and
11099 frame titles in it; allocated from the heap in init_xdisp and
11100 resized as needed in store_mode_line_noprop_char. */
11101
11102 static char *mode_line_noprop_buf;
11103
11104 /* The buffer's end, and a current output position in it. */
11105
11106 static char *mode_line_noprop_buf_end;
11107 static char *mode_line_noprop_ptr;
11108
11109 #define MODE_LINE_NOPROP_LEN(start) \
11110 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11111
11112 static enum {
11113 MODE_LINE_DISPLAY = 0,
11114 MODE_LINE_TITLE,
11115 MODE_LINE_NOPROP,
11116 MODE_LINE_STRING
11117 } mode_line_target;
11118
11119 /* Alist that caches the results of :propertize.
11120 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11121 static Lisp_Object mode_line_proptrans_alist;
11122
11123 /* List of strings making up the mode-line. */
11124 static Lisp_Object mode_line_string_list;
11125
11126 /* Base face property when building propertized mode line string. */
11127 static Lisp_Object mode_line_string_face;
11128 static Lisp_Object mode_line_string_face_prop;
11129
11130
11131 /* Unwind data for mode line strings */
11132
11133 static Lisp_Object Vmode_line_unwind_vector;
11134
11135 static Lisp_Object
11136 format_mode_line_unwind_data (struct frame *target_frame,
11137 struct buffer *obuf,
11138 Lisp_Object owin,
11139 int save_proptrans)
11140 {
11141 Lisp_Object vector, tmp;
11142
11143 /* Reduce consing by keeping one vector in
11144 Vwith_echo_area_save_vector. */
11145 vector = Vmode_line_unwind_vector;
11146 Vmode_line_unwind_vector = Qnil;
11147
11148 if (NILP (vector))
11149 vector = Fmake_vector (make_number (10), Qnil);
11150
11151 ASET (vector, 0, make_number (mode_line_target));
11152 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11153 ASET (vector, 2, mode_line_string_list);
11154 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11155 ASET (vector, 4, mode_line_string_face);
11156 ASET (vector, 5, mode_line_string_face_prop);
11157
11158 if (obuf)
11159 XSETBUFFER (tmp, obuf);
11160 else
11161 tmp = Qnil;
11162 ASET (vector, 6, tmp);
11163 ASET (vector, 7, owin);
11164 if (target_frame)
11165 {
11166 /* Similarly to `with-selected-window', if the operation selects
11167 a window on another frame, we must restore that frame's
11168 selected window, and (for a tty) the top-frame. */
11169 ASET (vector, 8, target_frame->selected_window);
11170 if (FRAME_TERMCAP_P (target_frame))
11171 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11172 }
11173
11174 return vector;
11175 }
11176
11177 static void
11178 unwind_format_mode_line (Lisp_Object vector)
11179 {
11180 Lisp_Object old_window = AREF (vector, 7);
11181 Lisp_Object target_frame_window = AREF (vector, 8);
11182 Lisp_Object old_top_frame = AREF (vector, 9);
11183
11184 mode_line_target = XINT (AREF (vector, 0));
11185 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11186 mode_line_string_list = AREF (vector, 2);
11187 if (! EQ (AREF (vector, 3), Qt))
11188 mode_line_proptrans_alist = AREF (vector, 3);
11189 mode_line_string_face = AREF (vector, 4);
11190 mode_line_string_face_prop = AREF (vector, 5);
11191
11192 /* Select window before buffer, since it may change the buffer. */
11193 if (!NILP (old_window))
11194 {
11195 /* If the operation that we are unwinding had selected a window
11196 on a different frame, reset its frame-selected-window. For a
11197 text terminal, reset its top-frame if necessary. */
11198 if (!NILP (target_frame_window))
11199 {
11200 Lisp_Object frame
11201 = WINDOW_FRAME (XWINDOW (target_frame_window));
11202
11203 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11204 Fselect_window (target_frame_window, Qt);
11205
11206 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11207 Fselect_frame (old_top_frame, Qt);
11208 }
11209
11210 Fselect_window (old_window, Qt);
11211 }
11212
11213 if (!NILP (AREF (vector, 6)))
11214 {
11215 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11216 ASET (vector, 6, Qnil);
11217 }
11218
11219 Vmode_line_unwind_vector = vector;
11220 }
11221
11222
11223 /* Store a single character C for the frame title in mode_line_noprop_buf.
11224 Re-allocate mode_line_noprop_buf if necessary. */
11225
11226 static void
11227 store_mode_line_noprop_char (char c)
11228 {
11229 /* If output position has reached the end of the allocated buffer,
11230 increase the buffer's size. */
11231 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11232 {
11233 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11234 ptrdiff_t size = len;
11235 mode_line_noprop_buf =
11236 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11237 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11238 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11239 }
11240
11241 *mode_line_noprop_ptr++ = c;
11242 }
11243
11244
11245 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11246 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11247 characters that yield more columns than PRECISION; PRECISION <= 0
11248 means copy the whole string. Pad with spaces until FIELD_WIDTH
11249 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11250 pad. Called from display_mode_element when it is used to build a
11251 frame title. */
11252
11253 static int
11254 store_mode_line_noprop (const char *string, int field_width, int precision)
11255 {
11256 const unsigned char *str = (const unsigned char *) string;
11257 int n = 0;
11258 ptrdiff_t dummy, nbytes;
11259
11260 /* Copy at most PRECISION chars from STR. */
11261 nbytes = strlen (string);
11262 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11263 while (nbytes--)
11264 store_mode_line_noprop_char (*str++);
11265
11266 /* Fill up with spaces until FIELD_WIDTH reached. */
11267 while (field_width > 0
11268 && n < field_width)
11269 {
11270 store_mode_line_noprop_char (' ');
11271 ++n;
11272 }
11273
11274 return n;
11275 }
11276
11277 /***********************************************************************
11278 Frame Titles
11279 ***********************************************************************/
11280
11281 #ifdef HAVE_WINDOW_SYSTEM
11282
11283 /* Set the title of FRAME, if it has changed. The title format is
11284 Vicon_title_format if FRAME is iconified, otherwise it is
11285 frame_title_format. */
11286
11287 static void
11288 x_consider_frame_title (Lisp_Object frame)
11289 {
11290 struct frame *f = XFRAME (frame);
11291
11292 if (FRAME_WINDOW_P (f)
11293 || FRAME_MINIBUF_ONLY_P (f)
11294 || f->explicit_name)
11295 {
11296 /* Do we have more than one visible frame on this X display? */
11297 Lisp_Object tail, other_frame, fmt;
11298 ptrdiff_t title_start;
11299 char *title;
11300 ptrdiff_t len;
11301 struct it it;
11302 ptrdiff_t count = SPECPDL_INDEX ();
11303
11304 FOR_EACH_FRAME (tail, other_frame)
11305 {
11306 struct frame *tf = XFRAME (other_frame);
11307
11308 if (tf != f
11309 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11310 && !FRAME_MINIBUF_ONLY_P (tf)
11311 && !EQ (other_frame, tip_frame)
11312 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11313 break;
11314 }
11315
11316 /* Set global variable indicating that multiple frames exist. */
11317 multiple_frames = CONSP (tail);
11318
11319 /* Switch to the buffer of selected window of the frame. Set up
11320 mode_line_target so that display_mode_element will output into
11321 mode_line_noprop_buf; then display the title. */
11322 record_unwind_protect (unwind_format_mode_line,
11323 format_mode_line_unwind_data
11324 (f, current_buffer, selected_window, 0));
11325
11326 Fselect_window (f->selected_window, Qt);
11327 set_buffer_internal_1
11328 (XBUFFER (XWINDOW (f->selected_window)->contents));
11329 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11330
11331 mode_line_target = MODE_LINE_TITLE;
11332 title_start = MODE_LINE_NOPROP_LEN (0);
11333 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11334 NULL, DEFAULT_FACE_ID);
11335 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11336 len = MODE_LINE_NOPROP_LEN (title_start);
11337 title = mode_line_noprop_buf + title_start;
11338 unbind_to (count, Qnil);
11339
11340 /* Set the title only if it's changed. This avoids consing in
11341 the common case where it hasn't. (If it turns out that we've
11342 already wasted too much time by walking through the list with
11343 display_mode_element, then we might need to optimize at a
11344 higher level than this.) */
11345 if (! STRINGP (f->name)
11346 || SBYTES (f->name) != len
11347 || memcmp (title, SDATA (f->name), len) != 0)
11348 x_implicitly_set_name (f, make_string (title, len), Qnil);
11349 }
11350 }
11351
11352 #endif /* not HAVE_WINDOW_SYSTEM */
11353
11354 \f
11355 /***********************************************************************
11356 Menu Bars
11357 ***********************************************************************/
11358
11359 /* Non-zero if we will not redisplay all visible windows. */
11360 #define REDISPLAY_SOME_P() \
11361 ((windows_or_buffers_changed == 0 \
11362 || windows_or_buffers_changed == REDISPLAY_SOME) \
11363 && (update_mode_lines == 0 \
11364 || update_mode_lines == REDISPLAY_SOME))
11365
11366 /* Prepare for redisplay by updating menu-bar item lists when
11367 appropriate. This can call eval. */
11368
11369 static void
11370 prepare_menu_bars (void)
11371 {
11372 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11373 bool some_windows = REDISPLAY_SOME_P ();
11374 struct gcpro gcpro1, gcpro2;
11375 Lisp_Object tooltip_frame;
11376
11377 #ifdef HAVE_WINDOW_SYSTEM
11378 tooltip_frame = tip_frame;
11379 #else
11380 tooltip_frame = Qnil;
11381 #endif
11382
11383 if (FUNCTIONP (Vpre_redisplay_function))
11384 {
11385 Lisp_Object windows = all_windows ? Qt : Qnil;
11386 if (all_windows && some_windows)
11387 {
11388 Lisp_Object ws = window_list ();
11389 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11390 {
11391 Lisp_Object this = XCAR (ws);
11392 struct window *w = XWINDOW (this);
11393 if (w->redisplay
11394 || XFRAME (w->frame)->redisplay
11395 || XBUFFER (w->contents)->text->redisplay)
11396 {
11397 windows = Fcons (this, windows);
11398 }
11399 }
11400 }
11401 safe_call1 (Vpre_redisplay_function, windows);
11402 }
11403
11404 /* Update all frame titles based on their buffer names, etc. We do
11405 this before the menu bars so that the buffer-menu will show the
11406 up-to-date frame titles. */
11407 #ifdef HAVE_WINDOW_SYSTEM
11408 if (all_windows)
11409 {
11410 Lisp_Object tail, frame;
11411
11412 FOR_EACH_FRAME (tail, frame)
11413 {
11414 struct frame *f = XFRAME (frame);
11415 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11416 if (some_windows
11417 && !f->redisplay
11418 && !w->redisplay
11419 && !XBUFFER (w->contents)->text->redisplay)
11420 continue;
11421
11422 if (!EQ (frame, tooltip_frame)
11423 && (FRAME_ICONIFIED_P (f)
11424 || FRAME_VISIBLE_P (f) == 1
11425 /* Exclude TTY frames that are obscured because they
11426 are not the top frame on their console. This is
11427 because x_consider_frame_title actually switches
11428 to the frame, which for TTY frames means it is
11429 marked as garbaged, and will be completely
11430 redrawn on the next redisplay cycle. This causes
11431 TTY frames to be completely redrawn, when there
11432 are more than one of them, even though nothing
11433 should be changed on display. */
11434 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11435 x_consider_frame_title (frame);
11436 }
11437 }
11438 #endif /* HAVE_WINDOW_SYSTEM */
11439
11440 /* Update the menu bar item lists, if appropriate. This has to be
11441 done before any actual redisplay or generation of display lines. */
11442
11443 if (all_windows)
11444 {
11445 Lisp_Object tail, frame;
11446 ptrdiff_t count = SPECPDL_INDEX ();
11447 /* 1 means that update_menu_bar has run its hooks
11448 so any further calls to update_menu_bar shouldn't do so again. */
11449 int menu_bar_hooks_run = 0;
11450
11451 record_unwind_save_match_data ();
11452
11453 FOR_EACH_FRAME (tail, frame)
11454 {
11455 struct frame *f = XFRAME (frame);
11456 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11457
11458 /* Ignore tooltip frame. */
11459 if (EQ (frame, tooltip_frame))
11460 continue;
11461
11462 if (some_windows
11463 && !f->redisplay
11464 && !w->redisplay
11465 && !XBUFFER (w->contents)->text->redisplay)
11466 continue;
11467
11468 /* If a window on this frame changed size, report that to
11469 the user and clear the size-change flag. */
11470 if (FRAME_WINDOW_SIZES_CHANGED (f))
11471 {
11472 Lisp_Object functions;
11473
11474 /* Clear flag first in case we get an error below. */
11475 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11476 functions = Vwindow_size_change_functions;
11477 GCPRO2 (tail, functions);
11478
11479 while (CONSP (functions))
11480 {
11481 if (!EQ (XCAR (functions), Qt))
11482 call1 (XCAR (functions), frame);
11483 functions = XCDR (functions);
11484 }
11485 UNGCPRO;
11486 }
11487
11488 GCPRO1 (tail);
11489 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11490 #ifdef HAVE_WINDOW_SYSTEM
11491 update_tool_bar (f, 0);
11492 #endif
11493 #ifdef HAVE_NS
11494 if (windows_or_buffers_changed
11495 && FRAME_NS_P (f))
11496 ns_set_doc_edited
11497 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11498 #endif
11499 UNGCPRO;
11500 }
11501
11502 unbind_to (count, Qnil);
11503 }
11504 else
11505 {
11506 struct frame *sf = SELECTED_FRAME ();
11507 update_menu_bar (sf, 1, 0);
11508 #ifdef HAVE_WINDOW_SYSTEM
11509 update_tool_bar (sf, 1);
11510 #endif
11511 }
11512 }
11513
11514
11515 /* Update the menu bar item list for frame F. This has to be done
11516 before we start to fill in any display lines, because it can call
11517 eval.
11518
11519 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11520
11521 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11522 already ran the menu bar hooks for this redisplay, so there
11523 is no need to run them again. The return value is the
11524 updated value of this flag, to pass to the next call. */
11525
11526 static int
11527 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11528 {
11529 Lisp_Object window;
11530 register struct window *w;
11531
11532 /* If called recursively during a menu update, do nothing. This can
11533 happen when, for instance, an activate-menubar-hook causes a
11534 redisplay. */
11535 if (inhibit_menubar_update)
11536 return hooks_run;
11537
11538 window = FRAME_SELECTED_WINDOW (f);
11539 w = XWINDOW (window);
11540
11541 if (FRAME_WINDOW_P (f)
11542 ?
11543 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11544 || defined (HAVE_NS) || defined (USE_GTK)
11545 FRAME_EXTERNAL_MENU_BAR (f)
11546 #else
11547 FRAME_MENU_BAR_LINES (f) > 0
11548 #endif
11549 : FRAME_MENU_BAR_LINES (f) > 0)
11550 {
11551 /* If the user has switched buffers or windows, we need to
11552 recompute to reflect the new bindings. But we'll
11553 recompute when update_mode_lines is set too; that means
11554 that people can use force-mode-line-update to request
11555 that the menu bar be recomputed. The adverse effect on
11556 the rest of the redisplay algorithm is about the same as
11557 windows_or_buffers_changed anyway. */
11558 if (windows_or_buffers_changed
11559 /* This used to test w->update_mode_line, but we believe
11560 there is no need to recompute the menu in that case. */
11561 || update_mode_lines
11562 || window_buffer_changed (w))
11563 {
11564 struct buffer *prev = current_buffer;
11565 ptrdiff_t count = SPECPDL_INDEX ();
11566
11567 specbind (Qinhibit_menubar_update, Qt);
11568
11569 set_buffer_internal_1 (XBUFFER (w->contents));
11570 if (save_match_data)
11571 record_unwind_save_match_data ();
11572 if (NILP (Voverriding_local_map_menu_flag))
11573 {
11574 specbind (Qoverriding_terminal_local_map, Qnil);
11575 specbind (Qoverriding_local_map, Qnil);
11576 }
11577
11578 if (!hooks_run)
11579 {
11580 /* Run the Lucid hook. */
11581 safe_run_hooks (Qactivate_menubar_hook);
11582
11583 /* If it has changed current-menubar from previous value,
11584 really recompute the menu-bar from the value. */
11585 if (! NILP (Vlucid_menu_bar_dirty_flag))
11586 call0 (Qrecompute_lucid_menubar);
11587
11588 safe_run_hooks (Qmenu_bar_update_hook);
11589
11590 hooks_run = 1;
11591 }
11592
11593 XSETFRAME (Vmenu_updating_frame, f);
11594 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11595
11596 /* Redisplay the menu bar in case we changed it. */
11597 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11598 || defined (HAVE_NS) || defined (USE_GTK)
11599 if (FRAME_WINDOW_P (f))
11600 {
11601 #if defined (HAVE_NS)
11602 /* All frames on Mac OS share the same menubar. So only
11603 the selected frame should be allowed to set it. */
11604 if (f == SELECTED_FRAME ())
11605 #endif
11606 set_frame_menubar (f, 0, 0);
11607 }
11608 else
11609 /* On a terminal screen, the menu bar is an ordinary screen
11610 line, and this makes it get updated. */
11611 w->update_mode_line = 1;
11612 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11613 /* In the non-toolkit version, the menu bar is an ordinary screen
11614 line, and this makes it get updated. */
11615 w->update_mode_line = 1;
11616 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11617
11618 unbind_to (count, Qnil);
11619 set_buffer_internal_1 (prev);
11620 }
11621 }
11622
11623 return hooks_run;
11624 }
11625
11626 /***********************************************************************
11627 Tool-bars
11628 ***********************************************************************/
11629
11630 #ifdef HAVE_WINDOW_SYSTEM
11631
11632 /* Tool-bar item index of the item on which a mouse button was pressed
11633 or -1. */
11634
11635 int last_tool_bar_item;
11636
11637 /* Select `frame' temporarily without running all the code in
11638 do_switch_frame.
11639 FIXME: Maybe do_switch_frame should be trimmed down similarly
11640 when `norecord' is set. */
11641 static void
11642 fast_set_selected_frame (Lisp_Object frame)
11643 {
11644 if (!EQ (selected_frame, frame))
11645 {
11646 selected_frame = frame;
11647 selected_window = XFRAME (frame)->selected_window;
11648 }
11649 }
11650
11651 /* Update the tool-bar item list for frame F. This has to be done
11652 before we start to fill in any display lines. Called from
11653 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11654 and restore it here. */
11655
11656 static void
11657 update_tool_bar (struct frame *f, int save_match_data)
11658 {
11659 #if defined (USE_GTK) || defined (HAVE_NS)
11660 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11661 #else
11662 int do_update = (WINDOWP (f->tool_bar_window)
11663 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11664 #endif
11665
11666 if (do_update)
11667 {
11668 Lisp_Object window;
11669 struct window *w;
11670
11671 window = FRAME_SELECTED_WINDOW (f);
11672 w = XWINDOW (window);
11673
11674 /* If the user has switched buffers or windows, we need to
11675 recompute to reflect the new bindings. But we'll
11676 recompute when update_mode_lines is set too; that means
11677 that people can use force-mode-line-update to request
11678 that the menu bar be recomputed. The adverse effect on
11679 the rest of the redisplay algorithm is about the same as
11680 windows_or_buffers_changed anyway. */
11681 if (windows_or_buffers_changed
11682 || w->update_mode_line
11683 || update_mode_lines
11684 || window_buffer_changed (w))
11685 {
11686 struct buffer *prev = current_buffer;
11687 ptrdiff_t count = SPECPDL_INDEX ();
11688 Lisp_Object frame, new_tool_bar;
11689 int new_n_tool_bar;
11690 struct gcpro gcpro1;
11691
11692 /* Set current_buffer to the buffer of the selected
11693 window of the frame, so that we get the right local
11694 keymaps. */
11695 set_buffer_internal_1 (XBUFFER (w->contents));
11696
11697 /* Save match data, if we must. */
11698 if (save_match_data)
11699 record_unwind_save_match_data ();
11700
11701 /* Make sure that we don't accidentally use bogus keymaps. */
11702 if (NILP (Voverriding_local_map_menu_flag))
11703 {
11704 specbind (Qoverriding_terminal_local_map, Qnil);
11705 specbind (Qoverriding_local_map, Qnil);
11706 }
11707
11708 GCPRO1 (new_tool_bar);
11709
11710 /* We must temporarily set the selected frame to this frame
11711 before calling tool_bar_items, because the calculation of
11712 the tool-bar keymap uses the selected frame (see
11713 `tool-bar-make-keymap' in tool-bar.el). */
11714 eassert (EQ (selected_window,
11715 /* Since we only explicitly preserve selected_frame,
11716 check that selected_window would be redundant. */
11717 XFRAME (selected_frame)->selected_window));
11718 record_unwind_protect (fast_set_selected_frame, selected_frame);
11719 XSETFRAME (frame, f);
11720 fast_set_selected_frame (frame);
11721
11722 /* Build desired tool-bar items from keymaps. */
11723 new_tool_bar
11724 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11725 &new_n_tool_bar);
11726
11727 /* Redisplay the tool-bar if we changed it. */
11728 if (new_n_tool_bar != f->n_tool_bar_items
11729 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11730 {
11731 /* Redisplay that happens asynchronously due to an expose event
11732 may access f->tool_bar_items. Make sure we update both
11733 variables within BLOCK_INPUT so no such event interrupts. */
11734 block_input ();
11735 fset_tool_bar_items (f, new_tool_bar);
11736 f->n_tool_bar_items = new_n_tool_bar;
11737 w->update_mode_line = 1;
11738 unblock_input ();
11739 }
11740
11741 UNGCPRO;
11742
11743 unbind_to (count, Qnil);
11744 set_buffer_internal_1 (prev);
11745 }
11746 }
11747 }
11748
11749 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11750
11751 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11752 F's desired tool-bar contents. F->tool_bar_items must have
11753 been set up previously by calling prepare_menu_bars. */
11754
11755 static void
11756 build_desired_tool_bar_string (struct frame *f)
11757 {
11758 int i, size, size_needed;
11759 struct gcpro gcpro1, gcpro2, gcpro3;
11760 Lisp_Object image, plist, props;
11761
11762 image = plist = props = Qnil;
11763 GCPRO3 (image, plist, props);
11764
11765 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11766 Otherwise, make a new string. */
11767
11768 /* The size of the string we might be able to reuse. */
11769 size = (STRINGP (f->desired_tool_bar_string)
11770 ? SCHARS (f->desired_tool_bar_string)
11771 : 0);
11772
11773 /* We need one space in the string for each image. */
11774 size_needed = f->n_tool_bar_items;
11775
11776 /* Reuse f->desired_tool_bar_string, if possible. */
11777 if (size < size_needed || NILP (f->desired_tool_bar_string))
11778 fset_desired_tool_bar_string
11779 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11780 else
11781 {
11782 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11783 Fremove_text_properties (make_number (0), make_number (size),
11784 props, f->desired_tool_bar_string);
11785 }
11786
11787 /* Put a `display' property on the string for the images to display,
11788 put a `menu_item' property on tool-bar items with a value that
11789 is the index of the item in F's tool-bar item vector. */
11790 for (i = 0; i < f->n_tool_bar_items; ++i)
11791 {
11792 #define PROP(IDX) \
11793 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11794
11795 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11796 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11797 int hmargin, vmargin, relief, idx, end;
11798
11799 /* If image is a vector, choose the image according to the
11800 button state. */
11801 image = PROP (TOOL_BAR_ITEM_IMAGES);
11802 if (VECTORP (image))
11803 {
11804 if (enabled_p)
11805 idx = (selected_p
11806 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11807 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11808 else
11809 idx = (selected_p
11810 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11811 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11812
11813 eassert (ASIZE (image) >= idx);
11814 image = AREF (image, idx);
11815 }
11816 else
11817 idx = -1;
11818
11819 /* Ignore invalid image specifications. */
11820 if (!valid_image_p (image))
11821 continue;
11822
11823 /* Display the tool-bar button pressed, or depressed. */
11824 plist = Fcopy_sequence (XCDR (image));
11825
11826 /* Compute margin and relief to draw. */
11827 relief = (tool_bar_button_relief >= 0
11828 ? tool_bar_button_relief
11829 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11830 hmargin = vmargin = relief;
11831
11832 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11833 INT_MAX - max (hmargin, vmargin)))
11834 {
11835 hmargin += XFASTINT (Vtool_bar_button_margin);
11836 vmargin += XFASTINT (Vtool_bar_button_margin);
11837 }
11838 else if (CONSP (Vtool_bar_button_margin))
11839 {
11840 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11841 INT_MAX - hmargin))
11842 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11843
11844 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11845 INT_MAX - vmargin))
11846 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11847 }
11848
11849 if (auto_raise_tool_bar_buttons_p)
11850 {
11851 /* Add a `:relief' property to the image spec if the item is
11852 selected. */
11853 if (selected_p)
11854 {
11855 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11856 hmargin -= relief;
11857 vmargin -= relief;
11858 }
11859 }
11860 else
11861 {
11862 /* If image is selected, display it pressed, i.e. with a
11863 negative relief. If it's not selected, display it with a
11864 raised relief. */
11865 plist = Fplist_put (plist, QCrelief,
11866 (selected_p
11867 ? make_number (-relief)
11868 : make_number (relief)));
11869 hmargin -= relief;
11870 vmargin -= relief;
11871 }
11872
11873 /* Put a margin around the image. */
11874 if (hmargin || vmargin)
11875 {
11876 if (hmargin == vmargin)
11877 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11878 else
11879 plist = Fplist_put (plist, QCmargin,
11880 Fcons (make_number (hmargin),
11881 make_number (vmargin)));
11882 }
11883
11884 /* If button is not enabled, and we don't have special images
11885 for the disabled state, make the image appear disabled by
11886 applying an appropriate algorithm to it. */
11887 if (!enabled_p && idx < 0)
11888 plist = Fplist_put (plist, QCconversion, Qdisabled);
11889
11890 /* Put a `display' text property on the string for the image to
11891 display. Put a `menu-item' property on the string that gives
11892 the start of this item's properties in the tool-bar items
11893 vector. */
11894 image = Fcons (Qimage, plist);
11895 props = list4 (Qdisplay, image,
11896 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11897
11898 /* Let the last image hide all remaining spaces in the tool bar
11899 string. The string can be longer than needed when we reuse a
11900 previous string. */
11901 if (i + 1 == f->n_tool_bar_items)
11902 end = SCHARS (f->desired_tool_bar_string);
11903 else
11904 end = i + 1;
11905 Fadd_text_properties (make_number (i), make_number (end),
11906 props, f->desired_tool_bar_string);
11907 #undef PROP
11908 }
11909
11910 UNGCPRO;
11911 }
11912
11913
11914 /* Display one line of the tool-bar of frame IT->f.
11915
11916 HEIGHT specifies the desired height of the tool-bar line.
11917 If the actual height of the glyph row is less than HEIGHT, the
11918 row's height is increased to HEIGHT, and the icons are centered
11919 vertically in the new height.
11920
11921 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11922 count a final empty row in case the tool-bar width exactly matches
11923 the window width.
11924 */
11925
11926 static void
11927 display_tool_bar_line (struct it *it, int height)
11928 {
11929 struct glyph_row *row = it->glyph_row;
11930 int max_x = it->last_visible_x;
11931 struct glyph *last;
11932
11933 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11934 clear_glyph_row (row);
11935 row->enabled_p = true;
11936 row->y = it->current_y;
11937
11938 /* Note that this isn't made use of if the face hasn't a box,
11939 so there's no need to check the face here. */
11940 it->start_of_box_run_p = 1;
11941
11942 while (it->current_x < max_x)
11943 {
11944 int x, n_glyphs_before, i, nglyphs;
11945 struct it it_before;
11946
11947 /* Get the next display element. */
11948 if (!get_next_display_element (it))
11949 {
11950 /* Don't count empty row if we are counting needed tool-bar lines. */
11951 if (height < 0 && !it->hpos)
11952 return;
11953 break;
11954 }
11955
11956 /* Produce glyphs. */
11957 n_glyphs_before = row->used[TEXT_AREA];
11958 it_before = *it;
11959
11960 PRODUCE_GLYPHS (it);
11961
11962 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11963 i = 0;
11964 x = it_before.current_x;
11965 while (i < nglyphs)
11966 {
11967 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11968
11969 if (x + glyph->pixel_width > max_x)
11970 {
11971 /* Glyph doesn't fit on line. Backtrack. */
11972 row->used[TEXT_AREA] = n_glyphs_before;
11973 *it = it_before;
11974 /* If this is the only glyph on this line, it will never fit on the
11975 tool-bar, so skip it. But ensure there is at least one glyph,
11976 so we don't accidentally disable the tool-bar. */
11977 if (n_glyphs_before == 0
11978 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11979 break;
11980 goto out;
11981 }
11982
11983 ++it->hpos;
11984 x += glyph->pixel_width;
11985 ++i;
11986 }
11987
11988 /* Stop at line end. */
11989 if (ITERATOR_AT_END_OF_LINE_P (it))
11990 break;
11991
11992 set_iterator_to_next (it, 1);
11993 }
11994
11995 out:;
11996
11997 row->displays_text_p = row->used[TEXT_AREA] != 0;
11998
11999 /* Use default face for the border below the tool bar.
12000
12001 FIXME: When auto-resize-tool-bars is grow-only, there is
12002 no additional border below the possibly empty tool-bar lines.
12003 So to make the extra empty lines look "normal", we have to
12004 use the tool-bar face for the border too. */
12005 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12006 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12007 it->face_id = DEFAULT_FACE_ID;
12008
12009 extend_face_to_end_of_line (it);
12010 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12011 last->right_box_line_p = 1;
12012 if (last == row->glyphs[TEXT_AREA])
12013 last->left_box_line_p = 1;
12014
12015 /* Make line the desired height and center it vertically. */
12016 if ((height -= it->max_ascent + it->max_descent) > 0)
12017 {
12018 /* Don't add more than one line height. */
12019 height %= FRAME_LINE_HEIGHT (it->f);
12020 it->max_ascent += height / 2;
12021 it->max_descent += (height + 1) / 2;
12022 }
12023
12024 compute_line_metrics (it);
12025
12026 /* If line is empty, make it occupy the rest of the tool-bar. */
12027 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12028 {
12029 row->height = row->phys_height = it->last_visible_y - row->y;
12030 row->visible_height = row->height;
12031 row->ascent = row->phys_ascent = 0;
12032 row->extra_line_spacing = 0;
12033 }
12034
12035 row->full_width_p = 1;
12036 row->continued_p = 0;
12037 row->truncated_on_left_p = 0;
12038 row->truncated_on_right_p = 0;
12039
12040 it->current_x = it->hpos = 0;
12041 it->current_y += row->height;
12042 ++it->vpos;
12043 ++it->glyph_row;
12044 }
12045
12046
12047 /* Max tool-bar height. Basically, this is what makes all other windows
12048 disappear when the frame gets too small. Rethink this! */
12049
12050 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12051 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12052
12053 /* Value is the number of pixels needed to make all tool-bar items of
12054 frame F visible. The actual number of glyph rows needed is
12055 returned in *N_ROWS if non-NULL. */
12056
12057 static int
12058 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12059 {
12060 struct window *w = XWINDOW (f->tool_bar_window);
12061 struct it it;
12062 /* tool_bar_height is called from redisplay_tool_bar after building
12063 the desired matrix, so use (unused) mode-line row as temporary row to
12064 avoid destroying the first tool-bar row. */
12065 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12066
12067 /* Initialize an iterator for iteration over
12068 F->desired_tool_bar_string in the tool-bar window of frame F. */
12069 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12070 it.first_visible_x = 0;
12071 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12072 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12073 it.paragraph_embedding = L2R;
12074
12075 while (!ITERATOR_AT_END_P (&it))
12076 {
12077 clear_glyph_row (temp_row);
12078 it.glyph_row = temp_row;
12079 display_tool_bar_line (&it, -1);
12080 }
12081 clear_glyph_row (temp_row);
12082
12083 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12084 if (n_rows)
12085 *n_rows = it.vpos > 0 ? it.vpos : -1;
12086
12087 if (pixelwise)
12088 return it.current_y;
12089 else
12090 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12091 }
12092
12093 #endif /* !USE_GTK && !HAVE_NS */
12094
12095 #if defined USE_GTK || defined HAVE_NS
12096 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12097 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12098 #endif
12099
12100 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12101 0, 2, 0,
12102 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12103 If FRAME is nil or omitted, use the selected frame. Optional argument
12104 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12105 (Lisp_Object frame, Lisp_Object pixelwise)
12106 {
12107 int height = 0;
12108
12109 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12110 struct frame *f = decode_any_frame (frame);
12111
12112 if (WINDOWP (f->tool_bar_window)
12113 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12114 {
12115 update_tool_bar (f, 1);
12116 if (f->n_tool_bar_items)
12117 {
12118 build_desired_tool_bar_string (f);
12119 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12120 }
12121 }
12122 #endif
12123
12124 return make_number (height);
12125 }
12126
12127
12128 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12129 height should be changed. */
12130
12131 static int
12132 redisplay_tool_bar (struct frame *f)
12133 {
12134 #if defined (USE_GTK) || defined (HAVE_NS)
12135
12136 if (FRAME_EXTERNAL_TOOL_BAR (f))
12137 update_frame_tool_bar (f);
12138 return 0;
12139
12140 #else /* !USE_GTK && !HAVE_NS */
12141
12142 struct window *w;
12143 struct it it;
12144 struct glyph_row *row;
12145
12146 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12147 do anything. This means you must start with tool-bar-lines
12148 non-zero to get the auto-sizing effect. Or in other words, you
12149 can turn off tool-bars by specifying tool-bar-lines zero. */
12150 if (!WINDOWP (f->tool_bar_window)
12151 || (w = XWINDOW (f->tool_bar_window),
12152 WINDOW_PIXEL_HEIGHT (w) == 0))
12153 return 0;
12154
12155 /* Set up an iterator for the tool-bar window. */
12156 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12157 it.first_visible_x = 0;
12158 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12159 row = it.glyph_row;
12160
12161 /* Build a string that represents the contents of the tool-bar. */
12162 build_desired_tool_bar_string (f);
12163 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12164 /* FIXME: This should be controlled by a user option. But it
12165 doesn't make sense to have an R2L tool bar if the menu bar cannot
12166 be drawn also R2L, and making the menu bar R2L is tricky due
12167 toolkit-specific code that implements it. If an R2L tool bar is
12168 ever supported, display_tool_bar_line should also be augmented to
12169 call unproduce_glyphs like display_line and display_string
12170 do. */
12171 it.paragraph_embedding = L2R;
12172
12173 if (f->n_tool_bar_rows == 0)
12174 {
12175 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12176
12177 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12178 {
12179 Lisp_Object frame;
12180 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12181 / FRAME_LINE_HEIGHT (f));
12182
12183 XSETFRAME (frame, f);
12184 Fmodify_frame_parameters (frame,
12185 list1 (Fcons (Qtool_bar_lines,
12186 make_number (new_lines))));
12187 /* Always do that now. */
12188 clear_glyph_matrix (w->desired_matrix);
12189 f->fonts_changed = 1;
12190 return 1;
12191 }
12192 }
12193
12194 /* Display as many lines as needed to display all tool-bar items. */
12195
12196 if (f->n_tool_bar_rows > 0)
12197 {
12198 int border, rows, height, extra;
12199
12200 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12201 border = XINT (Vtool_bar_border);
12202 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12203 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12204 else if (EQ (Vtool_bar_border, Qborder_width))
12205 border = f->border_width;
12206 else
12207 border = 0;
12208 if (border < 0)
12209 border = 0;
12210
12211 rows = f->n_tool_bar_rows;
12212 height = max (1, (it.last_visible_y - border) / rows);
12213 extra = it.last_visible_y - border - height * rows;
12214
12215 while (it.current_y < it.last_visible_y)
12216 {
12217 int h = 0;
12218 if (extra > 0 && rows-- > 0)
12219 {
12220 h = (extra + rows - 1) / rows;
12221 extra -= h;
12222 }
12223 display_tool_bar_line (&it, height + h);
12224 }
12225 }
12226 else
12227 {
12228 while (it.current_y < it.last_visible_y)
12229 display_tool_bar_line (&it, 0);
12230 }
12231
12232 /* It doesn't make much sense to try scrolling in the tool-bar
12233 window, so don't do it. */
12234 w->desired_matrix->no_scrolling_p = 1;
12235 w->must_be_updated_p = 1;
12236
12237 if (!NILP (Vauto_resize_tool_bars))
12238 {
12239 /* Do we really allow the toolbar to occupy the whole frame? */
12240 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12241 int change_height_p = 0;
12242
12243 /* If we couldn't display everything, change the tool-bar's
12244 height if there is room for more. */
12245 if (IT_STRING_CHARPOS (it) < it.end_charpos
12246 && it.current_y < max_tool_bar_height)
12247 change_height_p = 1;
12248
12249 /* We subtract 1 because display_tool_bar_line advances the
12250 glyph_row pointer before returning to its caller. We want to
12251 examine the last glyph row produced by
12252 display_tool_bar_line. */
12253 row = it.glyph_row - 1;
12254
12255 /* If there are blank lines at the end, except for a partially
12256 visible blank line at the end that is smaller than
12257 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12258 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12259 && row->height >= FRAME_LINE_HEIGHT (f))
12260 change_height_p = 1;
12261
12262 /* If row displays tool-bar items, but is partially visible,
12263 change the tool-bar's height. */
12264 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12265 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12266 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12267 change_height_p = 1;
12268
12269 /* Resize windows as needed by changing the `tool-bar-lines'
12270 frame parameter. */
12271 if (change_height_p)
12272 {
12273 Lisp_Object frame;
12274 int nrows;
12275 int new_height = tool_bar_height (f, &nrows, 1);
12276
12277 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12278 && !f->minimize_tool_bar_window_p)
12279 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12280 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12281 f->minimize_tool_bar_window_p = 0;
12282
12283 if (change_height_p)
12284 {
12285 /* Current size of the tool-bar window in canonical line
12286 units. */
12287 int old_lines = WINDOW_TOTAL_LINES (w);
12288 /* Required size of the tool-bar window in canonical
12289 line units. */
12290 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12291 / FRAME_LINE_HEIGHT (f));
12292 /* Maximum size of the tool-bar window in canonical line
12293 units that this frame can allow. */
12294 int max_lines =
12295 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12296
12297 /* Don't try to change the tool-bar window size and set
12298 the fonts_changed flag unless really necessary. That
12299 flag causes redisplay to give up and retry
12300 redisplaying the frame from scratch, so setting it
12301 unnecessarily can lead to nasty redisplay loops. */
12302 if (new_lines <= max_lines
12303 && eabs (new_lines - old_lines) >= 1)
12304 {
12305 XSETFRAME (frame, f);
12306 Fmodify_frame_parameters (frame,
12307 list1 (Fcons (Qtool_bar_lines,
12308 make_number (new_lines))));
12309 clear_glyph_matrix (w->desired_matrix);
12310 f->n_tool_bar_rows = nrows;
12311 f->fonts_changed = 1;
12312 return 1;
12313 }
12314 }
12315 }
12316 }
12317
12318 f->minimize_tool_bar_window_p = 0;
12319 return 0;
12320
12321 #endif /* USE_GTK || HAVE_NS */
12322 }
12323
12324 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12325
12326 /* Get information about the tool-bar item which is displayed in GLYPH
12327 on frame F. Return in *PROP_IDX the index where tool-bar item
12328 properties start in F->tool_bar_items. Value is zero if
12329 GLYPH doesn't display a tool-bar item. */
12330
12331 static int
12332 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12333 {
12334 Lisp_Object prop;
12335 int success_p;
12336 int charpos;
12337
12338 /* This function can be called asynchronously, which means we must
12339 exclude any possibility that Fget_text_property signals an
12340 error. */
12341 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12342 charpos = max (0, charpos);
12343
12344 /* Get the text property `menu-item' at pos. The value of that
12345 property is the start index of this item's properties in
12346 F->tool_bar_items. */
12347 prop = Fget_text_property (make_number (charpos),
12348 Qmenu_item, f->current_tool_bar_string);
12349 if (INTEGERP (prop))
12350 {
12351 *prop_idx = XINT (prop);
12352 success_p = 1;
12353 }
12354 else
12355 success_p = 0;
12356
12357 return success_p;
12358 }
12359
12360 \f
12361 /* Get information about the tool-bar item at position X/Y on frame F.
12362 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12363 the current matrix of the tool-bar window of F, or NULL if not
12364 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12365 item in F->tool_bar_items. Value is
12366
12367 -1 if X/Y is not on a tool-bar item
12368 0 if X/Y is on the same item that was highlighted before.
12369 1 otherwise. */
12370
12371 static int
12372 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12373 int *hpos, int *vpos, int *prop_idx)
12374 {
12375 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12376 struct window *w = XWINDOW (f->tool_bar_window);
12377 int area;
12378
12379 /* Find the glyph under X/Y. */
12380 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12381 if (*glyph == NULL)
12382 return -1;
12383
12384 /* Get the start of this tool-bar item's properties in
12385 f->tool_bar_items. */
12386 if (!tool_bar_item_info (f, *glyph, prop_idx))
12387 return -1;
12388
12389 /* Is mouse on the highlighted item? */
12390 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12391 && *vpos >= hlinfo->mouse_face_beg_row
12392 && *vpos <= hlinfo->mouse_face_end_row
12393 && (*vpos > hlinfo->mouse_face_beg_row
12394 || *hpos >= hlinfo->mouse_face_beg_col)
12395 && (*vpos < hlinfo->mouse_face_end_row
12396 || *hpos < hlinfo->mouse_face_end_col
12397 || hlinfo->mouse_face_past_end))
12398 return 0;
12399
12400 return 1;
12401 }
12402
12403
12404 /* EXPORT:
12405 Handle mouse button event on the tool-bar of frame F, at
12406 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12407 0 for button release. MODIFIERS is event modifiers for button
12408 release. */
12409
12410 void
12411 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12412 int modifiers)
12413 {
12414 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12415 struct window *w = XWINDOW (f->tool_bar_window);
12416 int hpos, vpos, prop_idx;
12417 struct glyph *glyph;
12418 Lisp_Object enabled_p;
12419 int ts;
12420
12421 /* If not on the highlighted tool-bar item, and mouse-highlight is
12422 non-nil, return. This is so we generate the tool-bar button
12423 click only when the mouse button is released on the same item as
12424 where it was pressed. However, when mouse-highlight is disabled,
12425 generate the click when the button is released regardless of the
12426 highlight, since tool-bar items are not highlighted in that
12427 case. */
12428 frame_to_window_pixel_xy (w, &x, &y);
12429 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12430 if (ts == -1
12431 || (ts != 0 && !NILP (Vmouse_highlight)))
12432 return;
12433
12434 /* When mouse-highlight is off, generate the click for the item
12435 where the button was pressed, disregarding where it was
12436 released. */
12437 if (NILP (Vmouse_highlight) && !down_p)
12438 prop_idx = last_tool_bar_item;
12439
12440 /* If item is disabled, do nothing. */
12441 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12442 if (NILP (enabled_p))
12443 return;
12444
12445 if (down_p)
12446 {
12447 /* Show item in pressed state. */
12448 if (!NILP (Vmouse_highlight))
12449 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12450 last_tool_bar_item = prop_idx;
12451 }
12452 else
12453 {
12454 Lisp_Object key, frame;
12455 struct input_event event;
12456 EVENT_INIT (event);
12457
12458 /* Show item in released state. */
12459 if (!NILP (Vmouse_highlight))
12460 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12461
12462 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12463
12464 XSETFRAME (frame, f);
12465 event.kind = TOOL_BAR_EVENT;
12466 event.frame_or_window = frame;
12467 event.arg = frame;
12468 kbd_buffer_store_event (&event);
12469
12470 event.kind = TOOL_BAR_EVENT;
12471 event.frame_or_window = frame;
12472 event.arg = key;
12473 event.modifiers = modifiers;
12474 kbd_buffer_store_event (&event);
12475 last_tool_bar_item = -1;
12476 }
12477 }
12478
12479
12480 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12481 tool-bar window-relative coordinates X/Y. Called from
12482 note_mouse_highlight. */
12483
12484 static void
12485 note_tool_bar_highlight (struct frame *f, int x, int y)
12486 {
12487 Lisp_Object window = f->tool_bar_window;
12488 struct window *w = XWINDOW (window);
12489 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12490 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12491 int hpos, vpos;
12492 struct glyph *glyph;
12493 struct glyph_row *row;
12494 int i;
12495 Lisp_Object enabled_p;
12496 int prop_idx;
12497 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12498 int mouse_down_p, rc;
12499
12500 /* Function note_mouse_highlight is called with negative X/Y
12501 values when mouse moves outside of the frame. */
12502 if (x <= 0 || y <= 0)
12503 {
12504 clear_mouse_face (hlinfo);
12505 return;
12506 }
12507
12508 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12509 if (rc < 0)
12510 {
12511 /* Not on tool-bar item. */
12512 clear_mouse_face (hlinfo);
12513 return;
12514 }
12515 else if (rc == 0)
12516 /* On same tool-bar item as before. */
12517 goto set_help_echo;
12518
12519 clear_mouse_face (hlinfo);
12520
12521 /* Mouse is down, but on different tool-bar item? */
12522 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12523 && f == dpyinfo->last_mouse_frame);
12524
12525 if (mouse_down_p
12526 && last_tool_bar_item != prop_idx)
12527 return;
12528
12529 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12530
12531 /* If tool-bar item is not enabled, don't highlight it. */
12532 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12533 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12534 {
12535 /* Compute the x-position of the glyph. In front and past the
12536 image is a space. We include this in the highlighted area. */
12537 row = MATRIX_ROW (w->current_matrix, vpos);
12538 for (i = x = 0; i < hpos; ++i)
12539 x += row->glyphs[TEXT_AREA][i].pixel_width;
12540
12541 /* Record this as the current active region. */
12542 hlinfo->mouse_face_beg_col = hpos;
12543 hlinfo->mouse_face_beg_row = vpos;
12544 hlinfo->mouse_face_beg_x = x;
12545 hlinfo->mouse_face_past_end = 0;
12546
12547 hlinfo->mouse_face_end_col = hpos + 1;
12548 hlinfo->mouse_face_end_row = vpos;
12549 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12550 hlinfo->mouse_face_window = window;
12551 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12552
12553 /* Display it as active. */
12554 show_mouse_face (hlinfo, draw);
12555 }
12556
12557 set_help_echo:
12558
12559 /* Set help_echo_string to a help string to display for this tool-bar item.
12560 XTread_socket does the rest. */
12561 help_echo_object = help_echo_window = Qnil;
12562 help_echo_pos = -1;
12563 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12564 if (NILP (help_echo_string))
12565 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12566 }
12567
12568 #endif /* !USE_GTK && !HAVE_NS */
12569
12570 #endif /* HAVE_WINDOW_SYSTEM */
12571
12572
12573 \f
12574 /************************************************************************
12575 Horizontal scrolling
12576 ************************************************************************/
12577
12578 static int hscroll_window_tree (Lisp_Object);
12579 static int hscroll_windows (Lisp_Object);
12580
12581 /* For all leaf windows in the window tree rooted at WINDOW, set their
12582 hscroll value so that PT is (i) visible in the window, and (ii) so
12583 that it is not within a certain margin at the window's left and
12584 right border. Value is non-zero if any window's hscroll has been
12585 changed. */
12586
12587 static int
12588 hscroll_window_tree (Lisp_Object window)
12589 {
12590 int hscrolled_p = 0;
12591 int hscroll_relative_p = FLOATP (Vhscroll_step);
12592 int hscroll_step_abs = 0;
12593 double hscroll_step_rel = 0;
12594
12595 if (hscroll_relative_p)
12596 {
12597 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12598 if (hscroll_step_rel < 0)
12599 {
12600 hscroll_relative_p = 0;
12601 hscroll_step_abs = 0;
12602 }
12603 }
12604 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12605 {
12606 hscroll_step_abs = XINT (Vhscroll_step);
12607 if (hscroll_step_abs < 0)
12608 hscroll_step_abs = 0;
12609 }
12610 else
12611 hscroll_step_abs = 0;
12612
12613 while (WINDOWP (window))
12614 {
12615 struct window *w = XWINDOW (window);
12616
12617 if (WINDOWP (w->contents))
12618 hscrolled_p |= hscroll_window_tree (w->contents);
12619 else if (w->cursor.vpos >= 0)
12620 {
12621 int h_margin;
12622 int text_area_width;
12623 struct glyph_row *cursor_row;
12624 struct glyph_row *bottom_row;
12625 int row_r2l_p;
12626
12627 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12628 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12629 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12630 else
12631 cursor_row = bottom_row - 1;
12632
12633 if (!cursor_row->enabled_p)
12634 {
12635 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12636 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12637 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12638 else
12639 cursor_row = bottom_row - 1;
12640 }
12641 row_r2l_p = cursor_row->reversed_p;
12642
12643 text_area_width = window_box_width (w, TEXT_AREA);
12644
12645 /* Scroll when cursor is inside this scroll margin. */
12646 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12647
12648 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12649 /* For left-to-right rows, hscroll when cursor is either
12650 (i) inside the right hscroll margin, or (ii) if it is
12651 inside the left margin and the window is already
12652 hscrolled. */
12653 && ((!row_r2l_p
12654 && ((w->hscroll
12655 && w->cursor.x <= h_margin)
12656 || (cursor_row->enabled_p
12657 && cursor_row->truncated_on_right_p
12658 && (w->cursor.x >= text_area_width - h_margin))))
12659 /* For right-to-left rows, the logic is similar,
12660 except that rules for scrolling to left and right
12661 are reversed. E.g., if cursor.x <= h_margin, we
12662 need to hscroll "to the right" unconditionally,
12663 and that will scroll the screen to the left so as
12664 to reveal the next portion of the row. */
12665 || (row_r2l_p
12666 && ((cursor_row->enabled_p
12667 /* FIXME: It is confusing to set the
12668 truncated_on_right_p flag when R2L rows
12669 are actually truncated on the left. */
12670 && cursor_row->truncated_on_right_p
12671 && w->cursor.x <= h_margin)
12672 || (w->hscroll
12673 && (w->cursor.x >= text_area_width - h_margin))))))
12674 {
12675 struct it it;
12676 ptrdiff_t hscroll;
12677 struct buffer *saved_current_buffer;
12678 ptrdiff_t pt;
12679 int wanted_x;
12680
12681 /* Find point in a display of infinite width. */
12682 saved_current_buffer = current_buffer;
12683 current_buffer = XBUFFER (w->contents);
12684
12685 if (w == XWINDOW (selected_window))
12686 pt = PT;
12687 else
12688 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12689
12690 /* Move iterator to pt starting at cursor_row->start in
12691 a line with infinite width. */
12692 init_to_row_start (&it, w, cursor_row);
12693 it.last_visible_x = INFINITY;
12694 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12695 current_buffer = saved_current_buffer;
12696
12697 /* Position cursor in window. */
12698 if (!hscroll_relative_p && hscroll_step_abs == 0)
12699 hscroll = max (0, (it.current_x
12700 - (ITERATOR_AT_END_OF_LINE_P (&it)
12701 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12702 : (text_area_width / 2))))
12703 / FRAME_COLUMN_WIDTH (it.f);
12704 else if ((!row_r2l_p
12705 && w->cursor.x >= text_area_width - h_margin)
12706 || (row_r2l_p && w->cursor.x <= h_margin))
12707 {
12708 if (hscroll_relative_p)
12709 wanted_x = text_area_width * (1 - hscroll_step_rel)
12710 - h_margin;
12711 else
12712 wanted_x = text_area_width
12713 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12714 - h_margin;
12715 hscroll
12716 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12717 }
12718 else
12719 {
12720 if (hscroll_relative_p)
12721 wanted_x = text_area_width * hscroll_step_rel
12722 + h_margin;
12723 else
12724 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12725 + h_margin;
12726 hscroll
12727 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12728 }
12729 hscroll = max (hscroll, w->min_hscroll);
12730
12731 /* Don't prevent redisplay optimizations if hscroll
12732 hasn't changed, as it will unnecessarily slow down
12733 redisplay. */
12734 if (w->hscroll != hscroll)
12735 {
12736 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12737 w->hscroll = hscroll;
12738 hscrolled_p = 1;
12739 }
12740 }
12741 }
12742
12743 window = w->next;
12744 }
12745
12746 /* Value is non-zero if hscroll of any leaf window has been changed. */
12747 return hscrolled_p;
12748 }
12749
12750
12751 /* Set hscroll so that cursor is visible and not inside horizontal
12752 scroll margins for all windows in the tree rooted at WINDOW. See
12753 also hscroll_window_tree above. Value is non-zero if any window's
12754 hscroll has been changed. If it has, desired matrices on the frame
12755 of WINDOW are cleared. */
12756
12757 static int
12758 hscroll_windows (Lisp_Object window)
12759 {
12760 int hscrolled_p = hscroll_window_tree (window);
12761 if (hscrolled_p)
12762 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12763 return hscrolled_p;
12764 }
12765
12766
12767 \f
12768 /************************************************************************
12769 Redisplay
12770 ************************************************************************/
12771
12772 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12773 to a non-zero value. This is sometimes handy to have in a debugger
12774 session. */
12775
12776 #ifdef GLYPH_DEBUG
12777
12778 /* First and last unchanged row for try_window_id. */
12779
12780 static int debug_first_unchanged_at_end_vpos;
12781 static int debug_last_unchanged_at_beg_vpos;
12782
12783 /* Delta vpos and y. */
12784
12785 static int debug_dvpos, debug_dy;
12786
12787 /* Delta in characters and bytes for try_window_id. */
12788
12789 static ptrdiff_t debug_delta, debug_delta_bytes;
12790
12791 /* Values of window_end_pos and window_end_vpos at the end of
12792 try_window_id. */
12793
12794 static ptrdiff_t debug_end_vpos;
12795
12796 /* Append a string to W->desired_matrix->method. FMT is a printf
12797 format string. If trace_redisplay_p is true also printf the
12798 resulting string to stderr. */
12799
12800 static void debug_method_add (struct window *, char const *, ...)
12801 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12802
12803 static void
12804 debug_method_add (struct window *w, char const *fmt, ...)
12805 {
12806 void *ptr = w;
12807 char *method = w->desired_matrix->method;
12808 int len = strlen (method);
12809 int size = sizeof w->desired_matrix->method;
12810 int remaining = size - len - 1;
12811 va_list ap;
12812
12813 if (len && remaining)
12814 {
12815 method[len] = '|';
12816 --remaining, ++len;
12817 }
12818
12819 va_start (ap, fmt);
12820 vsnprintf (method + len, remaining + 1, fmt, ap);
12821 va_end (ap);
12822
12823 if (trace_redisplay_p)
12824 fprintf (stderr, "%p (%s): %s\n",
12825 ptr,
12826 ((BUFFERP (w->contents)
12827 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12828 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12829 : "no buffer"),
12830 method + len);
12831 }
12832
12833 #endif /* GLYPH_DEBUG */
12834
12835
12836 /* Value is non-zero if all changes in window W, which displays
12837 current_buffer, are in the text between START and END. START is a
12838 buffer position, END is given as a distance from Z. Used in
12839 redisplay_internal for display optimization. */
12840
12841 static int
12842 text_outside_line_unchanged_p (struct window *w,
12843 ptrdiff_t start, ptrdiff_t end)
12844 {
12845 int unchanged_p = 1;
12846
12847 /* If text or overlays have changed, see where. */
12848 if (window_outdated (w))
12849 {
12850 /* Gap in the line? */
12851 if (GPT < start || Z - GPT < end)
12852 unchanged_p = 0;
12853
12854 /* Changes start in front of the line, or end after it? */
12855 if (unchanged_p
12856 && (BEG_UNCHANGED < start - 1
12857 || END_UNCHANGED < end))
12858 unchanged_p = 0;
12859
12860 /* If selective display, can't optimize if changes start at the
12861 beginning of the line. */
12862 if (unchanged_p
12863 && INTEGERP (BVAR (current_buffer, selective_display))
12864 && XINT (BVAR (current_buffer, selective_display)) > 0
12865 && (BEG_UNCHANGED < start || GPT <= start))
12866 unchanged_p = 0;
12867
12868 /* If there are overlays at the start or end of the line, these
12869 may have overlay strings with newlines in them. A change at
12870 START, for instance, may actually concern the display of such
12871 overlay strings as well, and they are displayed on different
12872 lines. So, quickly rule out this case. (For the future, it
12873 might be desirable to implement something more telling than
12874 just BEG/END_UNCHANGED.) */
12875 if (unchanged_p)
12876 {
12877 if (BEG + BEG_UNCHANGED == start
12878 && overlay_touches_p (start))
12879 unchanged_p = 0;
12880 if (END_UNCHANGED == end
12881 && overlay_touches_p (Z - end))
12882 unchanged_p = 0;
12883 }
12884
12885 /* Under bidi reordering, adding or deleting a character in the
12886 beginning of a paragraph, before the first strong directional
12887 character, can change the base direction of the paragraph (unless
12888 the buffer specifies a fixed paragraph direction), which will
12889 require to redisplay the whole paragraph. It might be worthwhile
12890 to find the paragraph limits and widen the range of redisplayed
12891 lines to that, but for now just give up this optimization. */
12892 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12893 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12894 unchanged_p = 0;
12895 }
12896
12897 return unchanged_p;
12898 }
12899
12900
12901 /* Do a frame update, taking possible shortcuts into account. This is
12902 the main external entry point for redisplay.
12903
12904 If the last redisplay displayed an echo area message and that message
12905 is no longer requested, we clear the echo area or bring back the
12906 mini-buffer if that is in use. */
12907
12908 void
12909 redisplay (void)
12910 {
12911 redisplay_internal ();
12912 }
12913
12914
12915 static Lisp_Object
12916 overlay_arrow_string_or_property (Lisp_Object var)
12917 {
12918 Lisp_Object val;
12919
12920 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12921 return val;
12922
12923 return Voverlay_arrow_string;
12924 }
12925
12926 /* Return 1 if there are any overlay-arrows in current_buffer. */
12927 static int
12928 overlay_arrow_in_current_buffer_p (void)
12929 {
12930 Lisp_Object vlist;
12931
12932 for (vlist = Voverlay_arrow_variable_list;
12933 CONSP (vlist);
12934 vlist = XCDR (vlist))
12935 {
12936 Lisp_Object var = XCAR (vlist);
12937 Lisp_Object val;
12938
12939 if (!SYMBOLP (var))
12940 continue;
12941 val = find_symbol_value (var);
12942 if (MARKERP (val)
12943 && current_buffer == XMARKER (val)->buffer)
12944 return 1;
12945 }
12946 return 0;
12947 }
12948
12949
12950 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12951 has changed. */
12952
12953 static int
12954 overlay_arrows_changed_p (void)
12955 {
12956 Lisp_Object vlist;
12957
12958 for (vlist = Voverlay_arrow_variable_list;
12959 CONSP (vlist);
12960 vlist = XCDR (vlist))
12961 {
12962 Lisp_Object var = XCAR (vlist);
12963 Lisp_Object val, pstr;
12964
12965 if (!SYMBOLP (var))
12966 continue;
12967 val = find_symbol_value (var);
12968 if (!MARKERP (val))
12969 continue;
12970 if (! EQ (COERCE_MARKER (val),
12971 Fget (var, Qlast_arrow_position))
12972 || ! (pstr = overlay_arrow_string_or_property (var),
12973 EQ (pstr, Fget (var, Qlast_arrow_string))))
12974 return 1;
12975 }
12976 return 0;
12977 }
12978
12979 /* Mark overlay arrows to be updated on next redisplay. */
12980
12981 static void
12982 update_overlay_arrows (int up_to_date)
12983 {
12984 Lisp_Object vlist;
12985
12986 for (vlist = Voverlay_arrow_variable_list;
12987 CONSP (vlist);
12988 vlist = XCDR (vlist))
12989 {
12990 Lisp_Object var = XCAR (vlist);
12991
12992 if (!SYMBOLP (var))
12993 continue;
12994
12995 if (up_to_date > 0)
12996 {
12997 Lisp_Object val = find_symbol_value (var);
12998 Fput (var, Qlast_arrow_position,
12999 COERCE_MARKER (val));
13000 Fput (var, Qlast_arrow_string,
13001 overlay_arrow_string_or_property (var));
13002 }
13003 else if (up_to_date < 0
13004 || !NILP (Fget (var, Qlast_arrow_position)))
13005 {
13006 Fput (var, Qlast_arrow_position, Qt);
13007 Fput (var, Qlast_arrow_string, Qt);
13008 }
13009 }
13010 }
13011
13012
13013 /* Return overlay arrow string to display at row.
13014 Return integer (bitmap number) for arrow bitmap in left fringe.
13015 Return nil if no overlay arrow. */
13016
13017 static Lisp_Object
13018 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13019 {
13020 Lisp_Object vlist;
13021
13022 for (vlist = Voverlay_arrow_variable_list;
13023 CONSP (vlist);
13024 vlist = XCDR (vlist))
13025 {
13026 Lisp_Object var = XCAR (vlist);
13027 Lisp_Object val;
13028
13029 if (!SYMBOLP (var))
13030 continue;
13031
13032 val = find_symbol_value (var);
13033
13034 if (MARKERP (val)
13035 && current_buffer == XMARKER (val)->buffer
13036 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13037 {
13038 if (FRAME_WINDOW_P (it->f)
13039 /* FIXME: if ROW->reversed_p is set, this should test
13040 the right fringe, not the left one. */
13041 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13042 {
13043 #ifdef HAVE_WINDOW_SYSTEM
13044 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13045 {
13046 int fringe_bitmap;
13047 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13048 return make_number (fringe_bitmap);
13049 }
13050 #endif
13051 return make_number (-1); /* Use default arrow bitmap. */
13052 }
13053 return overlay_arrow_string_or_property (var);
13054 }
13055 }
13056
13057 return Qnil;
13058 }
13059
13060 /* Return 1 if point moved out of or into a composition. Otherwise
13061 return 0. PREV_BUF and PREV_PT are the last point buffer and
13062 position. BUF and PT are the current point buffer and position. */
13063
13064 static int
13065 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13066 struct buffer *buf, ptrdiff_t pt)
13067 {
13068 ptrdiff_t start, end;
13069 Lisp_Object prop;
13070 Lisp_Object buffer;
13071
13072 XSETBUFFER (buffer, buf);
13073 /* Check a composition at the last point if point moved within the
13074 same buffer. */
13075 if (prev_buf == buf)
13076 {
13077 if (prev_pt == pt)
13078 /* Point didn't move. */
13079 return 0;
13080
13081 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13082 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13083 && composition_valid_p (start, end, prop)
13084 && start < prev_pt && end > prev_pt)
13085 /* The last point was within the composition. Return 1 iff
13086 point moved out of the composition. */
13087 return (pt <= start || pt >= end);
13088 }
13089
13090 /* Check a composition at the current point. */
13091 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13092 && find_composition (pt, -1, &start, &end, &prop, buffer)
13093 && composition_valid_p (start, end, prop)
13094 && start < pt && end > pt);
13095 }
13096
13097 /* Reconsider the clip changes of buffer which is displayed in W. */
13098
13099 static void
13100 reconsider_clip_changes (struct window *w)
13101 {
13102 struct buffer *b = XBUFFER (w->contents);
13103
13104 if (b->clip_changed
13105 && w->window_end_valid
13106 && w->current_matrix->buffer == b
13107 && w->current_matrix->zv == BUF_ZV (b)
13108 && w->current_matrix->begv == BUF_BEGV (b))
13109 b->clip_changed = 0;
13110
13111 /* If display wasn't paused, and W is not a tool bar window, see if
13112 point has been moved into or out of a composition. In that case,
13113 we set b->clip_changed to 1 to force updating the screen. If
13114 b->clip_changed has already been set to 1, we can skip this
13115 check. */
13116 if (!b->clip_changed && w->window_end_valid)
13117 {
13118 ptrdiff_t pt = (w == XWINDOW (selected_window)
13119 ? PT : marker_position (w->pointm));
13120
13121 if ((w->current_matrix->buffer != b || pt != w->last_point)
13122 && check_point_in_composition (w->current_matrix->buffer,
13123 w->last_point, b, pt))
13124 b->clip_changed = 1;
13125 }
13126 }
13127
13128 static void
13129 propagate_buffer_redisplay (void)
13130 { /* Resetting b->text->redisplay is problematic!
13131 We can't just reset it in the case that some window that displays
13132 it has not been redisplayed; and such a window can stay
13133 unredisplayed for a long time if it's currently invisible.
13134 But we do want to reset it at the end of redisplay otherwise
13135 its displayed windows will keep being redisplayed over and over
13136 again.
13137 So we copy all b->text->redisplay flags up to their windows here,
13138 such that mark_window_display_accurate can safely reset
13139 b->text->redisplay. */
13140 Lisp_Object ws = window_list ();
13141 for (; CONSP (ws); ws = XCDR (ws))
13142 {
13143 struct window *thisw = XWINDOW (XCAR (ws));
13144 struct buffer *thisb = XBUFFER (thisw->contents);
13145 if (thisb->text->redisplay)
13146 thisw->redisplay = true;
13147 }
13148 }
13149
13150 #define STOP_POLLING \
13151 do { if (! polling_stopped_here) stop_polling (); \
13152 polling_stopped_here = 1; } while (0)
13153
13154 #define RESUME_POLLING \
13155 do { if (polling_stopped_here) start_polling (); \
13156 polling_stopped_here = 0; } while (0)
13157
13158
13159 /* Perhaps in the future avoid recentering windows if it
13160 is not necessary; currently that causes some problems. */
13161
13162 static void
13163 redisplay_internal (void)
13164 {
13165 struct window *w = XWINDOW (selected_window);
13166 struct window *sw;
13167 struct frame *fr;
13168 int pending;
13169 bool must_finish = 0, match_p;
13170 struct text_pos tlbufpos, tlendpos;
13171 int number_of_visible_frames;
13172 ptrdiff_t count;
13173 struct frame *sf;
13174 int polling_stopped_here = 0;
13175 Lisp_Object tail, frame;
13176
13177 /* True means redisplay has to consider all windows on all
13178 frames. False, only selected_window is considered. */
13179 bool consider_all_windows_p;
13180
13181 /* True means redisplay has to redisplay the miniwindow. */
13182 bool update_miniwindow_p = false;
13183
13184 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13185
13186 /* No redisplay if running in batch mode or frame is not yet fully
13187 initialized, or redisplay is explicitly turned off by setting
13188 Vinhibit_redisplay. */
13189 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13190 || !NILP (Vinhibit_redisplay))
13191 return;
13192
13193 /* Don't examine these until after testing Vinhibit_redisplay.
13194 When Emacs is shutting down, perhaps because its connection to
13195 X has dropped, we should not look at them at all. */
13196 fr = XFRAME (w->frame);
13197 sf = SELECTED_FRAME ();
13198
13199 if (!fr->glyphs_initialized_p)
13200 return;
13201
13202 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13203 if (popup_activated ())
13204 return;
13205 #endif
13206
13207 /* I don't think this happens but let's be paranoid. */
13208 if (redisplaying_p)
13209 return;
13210
13211 /* Record a function that clears redisplaying_p
13212 when we leave this function. */
13213 count = SPECPDL_INDEX ();
13214 record_unwind_protect_void (unwind_redisplay);
13215 redisplaying_p = 1;
13216 specbind (Qinhibit_free_realized_faces, Qnil);
13217
13218 /* Record this function, so it appears on the profiler's backtraces. */
13219 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13220
13221 FOR_EACH_FRAME (tail, frame)
13222 XFRAME (frame)->already_hscrolled_p = 0;
13223
13224 retry:
13225 /* Remember the currently selected window. */
13226 sw = w;
13227
13228 pending = 0;
13229 last_escape_glyph_frame = NULL;
13230 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13231 last_glyphless_glyph_frame = NULL;
13232 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13233
13234 /* If face_change_count is non-zero, init_iterator will free all
13235 realized faces, which includes the faces referenced from current
13236 matrices. So, we can't reuse current matrices in this case. */
13237 if (face_change_count)
13238 windows_or_buffers_changed = 47;
13239
13240 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13241 && FRAME_TTY (sf)->previous_frame != sf)
13242 {
13243 /* Since frames on a single ASCII terminal share the same
13244 display area, displaying a different frame means redisplay
13245 the whole thing. */
13246 SET_FRAME_GARBAGED (sf);
13247 #ifndef DOS_NT
13248 set_tty_color_mode (FRAME_TTY (sf), sf);
13249 #endif
13250 FRAME_TTY (sf)->previous_frame = sf;
13251 }
13252
13253 /* Set the visible flags for all frames. Do this before checking for
13254 resized or garbaged frames; they want to know if their frames are
13255 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13256 number_of_visible_frames = 0;
13257
13258 FOR_EACH_FRAME (tail, frame)
13259 {
13260 struct frame *f = XFRAME (frame);
13261
13262 if (FRAME_VISIBLE_P (f))
13263 {
13264 ++number_of_visible_frames;
13265 /* Adjust matrices for visible frames only. */
13266 if (f->fonts_changed)
13267 {
13268 adjust_frame_glyphs (f);
13269 f->fonts_changed = 0;
13270 }
13271 /* If cursor type has been changed on the frame
13272 other than selected, consider all frames. */
13273 if (f != sf && f->cursor_type_changed)
13274 update_mode_lines = 31;
13275 }
13276 clear_desired_matrices (f);
13277 }
13278
13279 /* Notice any pending interrupt request to change frame size. */
13280 do_pending_window_change (1);
13281
13282 /* do_pending_window_change could change the selected_window due to
13283 frame resizing which makes the selected window too small. */
13284 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13285 sw = w;
13286
13287 /* Clear frames marked as garbaged. */
13288 clear_garbaged_frames ();
13289
13290 /* Build menubar and tool-bar items. */
13291 if (NILP (Vmemory_full))
13292 prepare_menu_bars ();
13293
13294 reconsider_clip_changes (w);
13295
13296 /* In most cases selected window displays current buffer. */
13297 match_p = XBUFFER (w->contents) == current_buffer;
13298 if (match_p)
13299 {
13300 /* Detect case that we need to write or remove a star in the mode line. */
13301 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13302 w->update_mode_line = 1;
13303
13304 if (mode_line_update_needed (w))
13305 w->update_mode_line = 1;
13306 }
13307
13308 /* Normally the message* functions will have already displayed and
13309 updated the echo area, but the frame may have been trashed, or
13310 the update may have been preempted, so display the echo area
13311 again here. Checking message_cleared_p captures the case that
13312 the echo area should be cleared. */
13313 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13314 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13315 || (message_cleared_p
13316 && minibuf_level == 0
13317 /* If the mini-window is currently selected, this means the
13318 echo-area doesn't show through. */
13319 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13320 {
13321 int window_height_changed_p = echo_area_display (0);
13322
13323 if (message_cleared_p)
13324 update_miniwindow_p = true;
13325
13326 must_finish = 1;
13327
13328 /* If we don't display the current message, don't clear the
13329 message_cleared_p flag, because, if we did, we wouldn't clear
13330 the echo area in the next redisplay which doesn't preserve
13331 the echo area. */
13332 if (!display_last_displayed_message_p)
13333 message_cleared_p = 0;
13334
13335 if (window_height_changed_p)
13336 {
13337 windows_or_buffers_changed = 50;
13338
13339 /* If window configuration was changed, frames may have been
13340 marked garbaged. Clear them or we will experience
13341 surprises wrt scrolling. */
13342 clear_garbaged_frames ();
13343 }
13344 }
13345 else if (EQ (selected_window, minibuf_window)
13346 && (current_buffer->clip_changed || window_outdated (w))
13347 && resize_mini_window (w, 0))
13348 {
13349 /* Resized active mini-window to fit the size of what it is
13350 showing if its contents might have changed. */
13351 must_finish = 1;
13352
13353 /* If window configuration was changed, frames may have been
13354 marked garbaged. Clear them or we will experience
13355 surprises wrt scrolling. */
13356 clear_garbaged_frames ();
13357 }
13358
13359 if (windows_or_buffers_changed && !update_mode_lines)
13360 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13361 only the windows's contents needs to be refreshed, or whether the
13362 mode-lines also need a refresh. */
13363 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13364 ? REDISPLAY_SOME : 32);
13365
13366 /* If specs for an arrow have changed, do thorough redisplay
13367 to ensure we remove any arrow that should no longer exist. */
13368 if (overlay_arrows_changed_p ())
13369 /* Apparently, this is the only case where we update other windows,
13370 without updating other mode-lines. */
13371 windows_or_buffers_changed = 49;
13372
13373 consider_all_windows_p = (update_mode_lines
13374 || windows_or_buffers_changed);
13375
13376 #define AINC(a,i) \
13377 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13378 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13379
13380 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13381 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13382
13383 /* Optimize the case that only the line containing the cursor in the
13384 selected window has changed. Variables starting with this_ are
13385 set in display_line and record information about the line
13386 containing the cursor. */
13387 tlbufpos = this_line_start_pos;
13388 tlendpos = this_line_end_pos;
13389 if (!consider_all_windows_p
13390 && CHARPOS (tlbufpos) > 0
13391 && !w->update_mode_line
13392 && !current_buffer->clip_changed
13393 && !current_buffer->prevent_redisplay_optimizations_p
13394 && FRAME_VISIBLE_P (XFRAME (w->frame))
13395 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13396 && !XFRAME (w->frame)->cursor_type_changed
13397 /* Make sure recorded data applies to current buffer, etc. */
13398 && this_line_buffer == current_buffer
13399 && match_p
13400 && !w->force_start
13401 && !w->optional_new_start
13402 /* Point must be on the line that we have info recorded about. */
13403 && PT >= CHARPOS (tlbufpos)
13404 && PT <= Z - CHARPOS (tlendpos)
13405 /* All text outside that line, including its final newline,
13406 must be unchanged. */
13407 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13408 CHARPOS (tlendpos)))
13409 {
13410 if (CHARPOS (tlbufpos) > BEGV
13411 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13412 && (CHARPOS (tlbufpos) == ZV
13413 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13414 /* Former continuation line has disappeared by becoming empty. */
13415 goto cancel;
13416 else if (window_outdated (w) || MINI_WINDOW_P (w))
13417 {
13418 /* We have to handle the case of continuation around a
13419 wide-column character (see the comment in indent.c around
13420 line 1340).
13421
13422 For instance, in the following case:
13423
13424 -------- Insert --------
13425 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13426 J_I_ ==> J_I_ `^^' are cursors.
13427 ^^ ^^
13428 -------- --------
13429
13430 As we have to redraw the line above, we cannot use this
13431 optimization. */
13432
13433 struct it it;
13434 int line_height_before = this_line_pixel_height;
13435
13436 /* Note that start_display will handle the case that the
13437 line starting at tlbufpos is a continuation line. */
13438 start_display (&it, w, tlbufpos);
13439
13440 /* Implementation note: It this still necessary? */
13441 if (it.current_x != this_line_start_x)
13442 goto cancel;
13443
13444 TRACE ((stderr, "trying display optimization 1\n"));
13445 w->cursor.vpos = -1;
13446 overlay_arrow_seen = 0;
13447 it.vpos = this_line_vpos;
13448 it.current_y = this_line_y;
13449 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13450 display_line (&it);
13451
13452 /* If line contains point, is not continued,
13453 and ends at same distance from eob as before, we win. */
13454 if (w->cursor.vpos >= 0
13455 /* Line is not continued, otherwise this_line_start_pos
13456 would have been set to 0 in display_line. */
13457 && CHARPOS (this_line_start_pos)
13458 /* Line ends as before. */
13459 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13460 /* Line has same height as before. Otherwise other lines
13461 would have to be shifted up or down. */
13462 && this_line_pixel_height == line_height_before)
13463 {
13464 /* If this is not the window's last line, we must adjust
13465 the charstarts of the lines below. */
13466 if (it.current_y < it.last_visible_y)
13467 {
13468 struct glyph_row *row
13469 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13470 ptrdiff_t delta, delta_bytes;
13471
13472 /* We used to distinguish between two cases here,
13473 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13474 when the line ends in a newline or the end of the
13475 buffer's accessible portion. But both cases did
13476 the same, so they were collapsed. */
13477 delta = (Z
13478 - CHARPOS (tlendpos)
13479 - MATRIX_ROW_START_CHARPOS (row));
13480 delta_bytes = (Z_BYTE
13481 - BYTEPOS (tlendpos)
13482 - MATRIX_ROW_START_BYTEPOS (row));
13483
13484 increment_matrix_positions (w->current_matrix,
13485 this_line_vpos + 1,
13486 w->current_matrix->nrows,
13487 delta, delta_bytes);
13488 }
13489
13490 /* If this row displays text now but previously didn't,
13491 or vice versa, w->window_end_vpos may have to be
13492 adjusted. */
13493 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13494 {
13495 if (w->window_end_vpos < this_line_vpos)
13496 w->window_end_vpos = this_line_vpos;
13497 }
13498 else if (w->window_end_vpos == this_line_vpos
13499 && this_line_vpos > 0)
13500 w->window_end_vpos = this_line_vpos - 1;
13501 w->window_end_valid = 0;
13502
13503 /* Update hint: No need to try to scroll in update_window. */
13504 w->desired_matrix->no_scrolling_p = 1;
13505
13506 #ifdef GLYPH_DEBUG
13507 *w->desired_matrix->method = 0;
13508 debug_method_add (w, "optimization 1");
13509 #endif
13510 #ifdef HAVE_WINDOW_SYSTEM
13511 update_window_fringes (w, 0);
13512 #endif
13513 goto update;
13514 }
13515 else
13516 goto cancel;
13517 }
13518 else if (/* Cursor position hasn't changed. */
13519 PT == w->last_point
13520 /* Make sure the cursor was last displayed
13521 in this window. Otherwise we have to reposition it. */
13522
13523 /* PXW: Must be converted to pixels, probably. */
13524 && 0 <= w->cursor.vpos
13525 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13526 {
13527 if (!must_finish)
13528 {
13529 do_pending_window_change (1);
13530 /* If selected_window changed, redisplay again. */
13531 if (WINDOWP (selected_window)
13532 && (w = XWINDOW (selected_window)) != sw)
13533 goto retry;
13534
13535 /* We used to always goto end_of_redisplay here, but this
13536 isn't enough if we have a blinking cursor. */
13537 if (w->cursor_off_p == w->last_cursor_off_p)
13538 goto end_of_redisplay;
13539 }
13540 goto update;
13541 }
13542 /* If highlighting the region, or if the cursor is in the echo area,
13543 then we can't just move the cursor. */
13544 else if (NILP (Vshow_trailing_whitespace)
13545 && !cursor_in_echo_area)
13546 {
13547 struct it it;
13548 struct glyph_row *row;
13549
13550 /* Skip from tlbufpos to PT and see where it is. Note that
13551 PT may be in invisible text. If so, we will end at the
13552 next visible position. */
13553 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13554 NULL, DEFAULT_FACE_ID);
13555 it.current_x = this_line_start_x;
13556 it.current_y = this_line_y;
13557 it.vpos = this_line_vpos;
13558
13559 /* The call to move_it_to stops in front of PT, but
13560 moves over before-strings. */
13561 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13562
13563 if (it.vpos == this_line_vpos
13564 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13565 row->enabled_p))
13566 {
13567 eassert (this_line_vpos == it.vpos);
13568 eassert (this_line_y == it.current_y);
13569 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13570 #ifdef GLYPH_DEBUG
13571 *w->desired_matrix->method = 0;
13572 debug_method_add (w, "optimization 3");
13573 #endif
13574 goto update;
13575 }
13576 else
13577 goto cancel;
13578 }
13579
13580 cancel:
13581 /* Text changed drastically or point moved off of line. */
13582 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13583 }
13584
13585 CHARPOS (this_line_start_pos) = 0;
13586 ++clear_face_cache_count;
13587 #ifdef HAVE_WINDOW_SYSTEM
13588 ++clear_image_cache_count;
13589 #endif
13590
13591 /* Build desired matrices, and update the display. If
13592 consider_all_windows_p is non-zero, do it for all windows on all
13593 frames. Otherwise do it for selected_window, only. */
13594
13595 if (consider_all_windows_p)
13596 {
13597 FOR_EACH_FRAME (tail, frame)
13598 XFRAME (frame)->updated_p = 0;
13599
13600 propagate_buffer_redisplay ();
13601
13602 FOR_EACH_FRAME (tail, frame)
13603 {
13604 struct frame *f = XFRAME (frame);
13605
13606 /* We don't have to do anything for unselected terminal
13607 frames. */
13608 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13609 && !EQ (FRAME_TTY (f)->top_frame, frame))
13610 continue;
13611
13612 retry_frame:
13613
13614 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13615 {
13616 bool gcscrollbars
13617 /* Only GC scrollbars when we redisplay the whole frame. */
13618 = f->redisplay || !REDISPLAY_SOME_P ();
13619 /* Mark all the scroll bars to be removed; we'll redeem
13620 the ones we want when we redisplay their windows. */
13621 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13622 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13623
13624 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13625 redisplay_windows (FRAME_ROOT_WINDOW (f));
13626 /* Remember that the invisible frames need to be redisplayed next
13627 time they're visible. */
13628 else if (!REDISPLAY_SOME_P ())
13629 f->redisplay = true;
13630
13631 /* The X error handler may have deleted that frame. */
13632 if (!FRAME_LIVE_P (f))
13633 continue;
13634
13635 /* Any scroll bars which redisplay_windows should have
13636 nuked should now go away. */
13637 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13638 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13639
13640 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13641 {
13642 /* If fonts changed on visible frame, display again. */
13643 if (f->fonts_changed)
13644 {
13645 adjust_frame_glyphs (f);
13646 f->fonts_changed = 0;
13647 goto retry_frame;
13648 }
13649
13650 /* See if we have to hscroll. */
13651 if (!f->already_hscrolled_p)
13652 {
13653 f->already_hscrolled_p = 1;
13654 if (hscroll_windows (f->root_window))
13655 goto retry_frame;
13656 }
13657
13658 /* Prevent various kinds of signals during display
13659 update. stdio is not robust about handling
13660 signals, which can cause an apparent I/O error. */
13661 if (interrupt_input)
13662 unrequest_sigio ();
13663 STOP_POLLING;
13664
13665 pending |= update_frame (f, 0, 0);
13666 f->cursor_type_changed = 0;
13667 f->updated_p = 1;
13668 }
13669 }
13670 }
13671
13672 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13673
13674 if (!pending)
13675 {
13676 /* Do the mark_window_display_accurate after all windows have
13677 been redisplayed because this call resets flags in buffers
13678 which are needed for proper redisplay. */
13679 FOR_EACH_FRAME (tail, frame)
13680 {
13681 struct frame *f = XFRAME (frame);
13682 if (f->updated_p)
13683 {
13684 f->redisplay = false;
13685 mark_window_display_accurate (f->root_window, 1);
13686 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13687 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13688 }
13689 }
13690 }
13691 }
13692 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13693 {
13694 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13695 struct frame *mini_frame;
13696
13697 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13698 /* Use list_of_error, not Qerror, so that
13699 we catch only errors and don't run the debugger. */
13700 internal_condition_case_1 (redisplay_window_1, selected_window,
13701 list_of_error,
13702 redisplay_window_error);
13703 if (update_miniwindow_p)
13704 internal_condition_case_1 (redisplay_window_1, mini_window,
13705 list_of_error,
13706 redisplay_window_error);
13707
13708 /* Compare desired and current matrices, perform output. */
13709
13710 update:
13711 /* If fonts changed, display again. */
13712 if (sf->fonts_changed)
13713 goto retry;
13714
13715 /* Prevent various kinds of signals during display update.
13716 stdio is not robust about handling signals,
13717 which can cause an apparent I/O error. */
13718 if (interrupt_input)
13719 unrequest_sigio ();
13720 STOP_POLLING;
13721
13722 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13723 {
13724 if (hscroll_windows (selected_window))
13725 goto retry;
13726
13727 XWINDOW (selected_window)->must_be_updated_p = true;
13728 pending = update_frame (sf, 0, 0);
13729 sf->cursor_type_changed = 0;
13730 }
13731
13732 /* We may have called echo_area_display at the top of this
13733 function. If the echo area is on another frame, that may
13734 have put text on a frame other than the selected one, so the
13735 above call to update_frame would not have caught it. Catch
13736 it here. */
13737 mini_window = FRAME_MINIBUF_WINDOW (sf);
13738 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13739
13740 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13741 {
13742 XWINDOW (mini_window)->must_be_updated_p = true;
13743 pending |= update_frame (mini_frame, 0, 0);
13744 mini_frame->cursor_type_changed = 0;
13745 if (!pending && hscroll_windows (mini_window))
13746 goto retry;
13747 }
13748 }
13749
13750 /* If display was paused because of pending input, make sure we do a
13751 thorough update the next time. */
13752 if (pending)
13753 {
13754 /* Prevent the optimization at the beginning of
13755 redisplay_internal that tries a single-line update of the
13756 line containing the cursor in the selected window. */
13757 CHARPOS (this_line_start_pos) = 0;
13758
13759 /* Let the overlay arrow be updated the next time. */
13760 update_overlay_arrows (0);
13761
13762 /* If we pause after scrolling, some rows in the current
13763 matrices of some windows are not valid. */
13764 if (!WINDOW_FULL_WIDTH_P (w)
13765 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13766 update_mode_lines = 36;
13767 }
13768 else
13769 {
13770 if (!consider_all_windows_p)
13771 {
13772 /* This has already been done above if
13773 consider_all_windows_p is set. */
13774 if (XBUFFER (w->contents)->text->redisplay
13775 && buffer_window_count (XBUFFER (w->contents)) > 1)
13776 /* This can happen if b->text->redisplay was set during
13777 jit-lock. */
13778 propagate_buffer_redisplay ();
13779 mark_window_display_accurate_1 (w, 1);
13780
13781 /* Say overlay arrows are up to date. */
13782 update_overlay_arrows (1);
13783
13784 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13785 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13786 }
13787
13788 update_mode_lines = 0;
13789 windows_or_buffers_changed = 0;
13790 }
13791
13792 /* Start SIGIO interrupts coming again. Having them off during the
13793 code above makes it less likely one will discard output, but not
13794 impossible, since there might be stuff in the system buffer here.
13795 But it is much hairier to try to do anything about that. */
13796 if (interrupt_input)
13797 request_sigio ();
13798 RESUME_POLLING;
13799
13800 /* If a frame has become visible which was not before, redisplay
13801 again, so that we display it. Expose events for such a frame
13802 (which it gets when becoming visible) don't call the parts of
13803 redisplay constructing glyphs, so simply exposing a frame won't
13804 display anything in this case. So, we have to display these
13805 frames here explicitly. */
13806 if (!pending)
13807 {
13808 int new_count = 0;
13809
13810 FOR_EACH_FRAME (tail, frame)
13811 {
13812 if (XFRAME (frame)->visible)
13813 new_count++;
13814 }
13815
13816 if (new_count != number_of_visible_frames)
13817 windows_or_buffers_changed = 52;
13818 }
13819
13820 /* Change frame size now if a change is pending. */
13821 do_pending_window_change (1);
13822
13823 /* If we just did a pending size change, or have additional
13824 visible frames, or selected_window changed, redisplay again. */
13825 if ((windows_or_buffers_changed && !pending)
13826 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13827 goto retry;
13828
13829 /* Clear the face and image caches.
13830
13831 We used to do this only if consider_all_windows_p. But the cache
13832 needs to be cleared if a timer creates images in the current
13833 buffer (e.g. the test case in Bug#6230). */
13834
13835 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13836 {
13837 clear_face_cache (0);
13838 clear_face_cache_count = 0;
13839 }
13840
13841 #ifdef HAVE_WINDOW_SYSTEM
13842 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13843 {
13844 clear_image_caches (Qnil);
13845 clear_image_cache_count = 0;
13846 }
13847 #endif /* HAVE_WINDOW_SYSTEM */
13848
13849 end_of_redisplay:
13850 if (interrupt_input && interrupts_deferred)
13851 request_sigio ();
13852
13853 unbind_to (count, Qnil);
13854 RESUME_POLLING;
13855 }
13856
13857
13858 /* Redisplay, but leave alone any recent echo area message unless
13859 another message has been requested in its place.
13860
13861 This is useful in situations where you need to redisplay but no
13862 user action has occurred, making it inappropriate for the message
13863 area to be cleared. See tracking_off and
13864 wait_reading_process_output for examples of these situations.
13865
13866 FROM_WHERE is an integer saying from where this function was
13867 called. This is useful for debugging. */
13868
13869 void
13870 redisplay_preserve_echo_area (int from_where)
13871 {
13872 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13873
13874 if (!NILP (echo_area_buffer[1]))
13875 {
13876 /* We have a previously displayed message, but no current
13877 message. Redisplay the previous message. */
13878 display_last_displayed_message_p = 1;
13879 redisplay_internal ();
13880 display_last_displayed_message_p = 0;
13881 }
13882 else
13883 redisplay_internal ();
13884
13885 flush_frame (SELECTED_FRAME ());
13886 }
13887
13888
13889 /* Function registered with record_unwind_protect in redisplay_internal. */
13890
13891 static void
13892 unwind_redisplay (void)
13893 {
13894 redisplaying_p = 0;
13895 }
13896
13897
13898 /* Mark the display of leaf window W as accurate or inaccurate.
13899 If ACCURATE_P is non-zero mark display of W as accurate. If
13900 ACCURATE_P is zero, arrange for W to be redisplayed the next
13901 time redisplay_internal is called. */
13902
13903 static void
13904 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13905 {
13906 struct buffer *b = XBUFFER (w->contents);
13907
13908 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13909 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13910 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13911
13912 if (accurate_p)
13913 {
13914 b->clip_changed = false;
13915 b->prevent_redisplay_optimizations_p = false;
13916 eassert (buffer_window_count (b) > 0);
13917 /* Resetting b->text->redisplay is problematic!
13918 In order to make it safer to do it here, redisplay_internal must
13919 have copied all b->text->redisplay to their respective windows. */
13920 b->text->redisplay = false;
13921
13922 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13923 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13924 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13925 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13926
13927 w->current_matrix->buffer = b;
13928 w->current_matrix->begv = BUF_BEGV (b);
13929 w->current_matrix->zv = BUF_ZV (b);
13930
13931 w->last_cursor_vpos = w->cursor.vpos;
13932 w->last_cursor_off_p = w->cursor_off_p;
13933
13934 if (w == XWINDOW (selected_window))
13935 w->last_point = BUF_PT (b);
13936 else
13937 w->last_point = marker_position (w->pointm);
13938
13939 w->window_end_valid = true;
13940 w->update_mode_line = false;
13941 }
13942
13943 w->redisplay = !accurate_p;
13944 }
13945
13946
13947 /* Mark the display of windows in the window tree rooted at WINDOW as
13948 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13949 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13950 be redisplayed the next time redisplay_internal is called. */
13951
13952 void
13953 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13954 {
13955 struct window *w;
13956
13957 for (; !NILP (window); window = w->next)
13958 {
13959 w = XWINDOW (window);
13960 if (WINDOWP (w->contents))
13961 mark_window_display_accurate (w->contents, accurate_p);
13962 else
13963 mark_window_display_accurate_1 (w, accurate_p);
13964 }
13965
13966 if (accurate_p)
13967 update_overlay_arrows (1);
13968 else
13969 /* Force a thorough redisplay the next time by setting
13970 last_arrow_position and last_arrow_string to t, which is
13971 unequal to any useful value of Voverlay_arrow_... */
13972 update_overlay_arrows (-1);
13973 }
13974
13975
13976 /* Return value in display table DP (Lisp_Char_Table *) for character
13977 C. Since a display table doesn't have any parent, we don't have to
13978 follow parent. Do not call this function directly but use the
13979 macro DISP_CHAR_VECTOR. */
13980
13981 Lisp_Object
13982 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13983 {
13984 Lisp_Object val;
13985
13986 if (ASCII_CHAR_P (c))
13987 {
13988 val = dp->ascii;
13989 if (SUB_CHAR_TABLE_P (val))
13990 val = XSUB_CHAR_TABLE (val)->contents[c];
13991 }
13992 else
13993 {
13994 Lisp_Object table;
13995
13996 XSETCHAR_TABLE (table, dp);
13997 val = char_table_ref (table, c);
13998 }
13999 if (NILP (val))
14000 val = dp->defalt;
14001 return val;
14002 }
14003
14004
14005 \f
14006 /***********************************************************************
14007 Window Redisplay
14008 ***********************************************************************/
14009
14010 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14011
14012 static void
14013 redisplay_windows (Lisp_Object window)
14014 {
14015 while (!NILP (window))
14016 {
14017 struct window *w = XWINDOW (window);
14018
14019 if (WINDOWP (w->contents))
14020 redisplay_windows (w->contents);
14021 else if (BUFFERP (w->contents))
14022 {
14023 displayed_buffer = XBUFFER (w->contents);
14024 /* Use list_of_error, not Qerror, so that
14025 we catch only errors and don't run the debugger. */
14026 internal_condition_case_1 (redisplay_window_0, window,
14027 list_of_error,
14028 redisplay_window_error);
14029 }
14030
14031 window = w->next;
14032 }
14033 }
14034
14035 static Lisp_Object
14036 redisplay_window_error (Lisp_Object ignore)
14037 {
14038 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14039 return Qnil;
14040 }
14041
14042 static Lisp_Object
14043 redisplay_window_0 (Lisp_Object window)
14044 {
14045 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14046 redisplay_window (window, false);
14047 return Qnil;
14048 }
14049
14050 static Lisp_Object
14051 redisplay_window_1 (Lisp_Object window)
14052 {
14053 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14054 redisplay_window (window, true);
14055 return Qnil;
14056 }
14057 \f
14058
14059 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14060 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14061 which positions recorded in ROW differ from current buffer
14062 positions.
14063
14064 Return 0 if cursor is not on this row, 1 otherwise. */
14065
14066 static int
14067 set_cursor_from_row (struct window *w, struct glyph_row *row,
14068 struct glyph_matrix *matrix,
14069 ptrdiff_t delta, ptrdiff_t delta_bytes,
14070 int dy, int dvpos)
14071 {
14072 struct glyph *glyph = row->glyphs[TEXT_AREA];
14073 struct glyph *end = glyph + row->used[TEXT_AREA];
14074 struct glyph *cursor = NULL;
14075 /* The last known character position in row. */
14076 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14077 int x = row->x;
14078 ptrdiff_t pt_old = PT - delta;
14079 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14080 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14081 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14082 /* A glyph beyond the edge of TEXT_AREA which we should never
14083 touch. */
14084 struct glyph *glyphs_end = end;
14085 /* Non-zero means we've found a match for cursor position, but that
14086 glyph has the avoid_cursor_p flag set. */
14087 int match_with_avoid_cursor = 0;
14088 /* Non-zero means we've seen at least one glyph that came from a
14089 display string. */
14090 int string_seen = 0;
14091 /* Largest and smallest buffer positions seen so far during scan of
14092 glyph row. */
14093 ptrdiff_t bpos_max = pos_before;
14094 ptrdiff_t bpos_min = pos_after;
14095 /* Last buffer position covered by an overlay string with an integer
14096 `cursor' property. */
14097 ptrdiff_t bpos_covered = 0;
14098 /* Non-zero means the display string on which to display the cursor
14099 comes from a text property, not from an overlay. */
14100 int string_from_text_prop = 0;
14101
14102 /* Don't even try doing anything if called for a mode-line or
14103 header-line row, since the rest of the code isn't prepared to
14104 deal with such calamities. */
14105 eassert (!row->mode_line_p);
14106 if (row->mode_line_p)
14107 return 0;
14108
14109 /* Skip over glyphs not having an object at the start and the end of
14110 the row. These are special glyphs like truncation marks on
14111 terminal frames. */
14112 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14113 {
14114 if (!row->reversed_p)
14115 {
14116 while (glyph < end
14117 && INTEGERP (glyph->object)
14118 && glyph->charpos < 0)
14119 {
14120 x += glyph->pixel_width;
14121 ++glyph;
14122 }
14123 while (end > glyph
14124 && INTEGERP ((end - 1)->object)
14125 /* CHARPOS is zero for blanks and stretch glyphs
14126 inserted by extend_face_to_end_of_line. */
14127 && (end - 1)->charpos <= 0)
14128 --end;
14129 glyph_before = glyph - 1;
14130 glyph_after = end;
14131 }
14132 else
14133 {
14134 struct glyph *g;
14135
14136 /* If the glyph row is reversed, we need to process it from back
14137 to front, so swap the edge pointers. */
14138 glyphs_end = end = glyph - 1;
14139 glyph += row->used[TEXT_AREA] - 1;
14140
14141 while (glyph > end + 1
14142 && INTEGERP (glyph->object)
14143 && glyph->charpos < 0)
14144 {
14145 --glyph;
14146 x -= glyph->pixel_width;
14147 }
14148 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14149 --glyph;
14150 /* By default, in reversed rows we put the cursor on the
14151 rightmost (first in the reading order) glyph. */
14152 for (g = end + 1; g < glyph; g++)
14153 x += g->pixel_width;
14154 while (end < glyph
14155 && INTEGERP ((end + 1)->object)
14156 && (end + 1)->charpos <= 0)
14157 ++end;
14158 glyph_before = glyph + 1;
14159 glyph_after = end;
14160 }
14161 }
14162 else if (row->reversed_p)
14163 {
14164 /* In R2L rows that don't display text, put the cursor on the
14165 rightmost glyph. Case in point: an empty last line that is
14166 part of an R2L paragraph. */
14167 cursor = end - 1;
14168 /* Avoid placing the cursor on the last glyph of the row, where
14169 on terminal frames we hold the vertical border between
14170 adjacent windows. */
14171 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14172 && !WINDOW_RIGHTMOST_P (w)
14173 && cursor == row->glyphs[LAST_AREA] - 1)
14174 cursor--;
14175 x = -1; /* will be computed below, at label compute_x */
14176 }
14177
14178 /* Step 1: Try to find the glyph whose character position
14179 corresponds to point. If that's not possible, find 2 glyphs
14180 whose character positions are the closest to point, one before
14181 point, the other after it. */
14182 if (!row->reversed_p)
14183 while (/* not marched to end of glyph row */
14184 glyph < end
14185 /* glyph was not inserted by redisplay for internal purposes */
14186 && !INTEGERP (glyph->object))
14187 {
14188 if (BUFFERP (glyph->object))
14189 {
14190 ptrdiff_t dpos = glyph->charpos - pt_old;
14191
14192 if (glyph->charpos > bpos_max)
14193 bpos_max = glyph->charpos;
14194 if (glyph->charpos < bpos_min)
14195 bpos_min = glyph->charpos;
14196 if (!glyph->avoid_cursor_p)
14197 {
14198 /* If we hit point, we've found the glyph on which to
14199 display the cursor. */
14200 if (dpos == 0)
14201 {
14202 match_with_avoid_cursor = 0;
14203 break;
14204 }
14205 /* See if we've found a better approximation to
14206 POS_BEFORE or to POS_AFTER. */
14207 if (0 > dpos && dpos > pos_before - pt_old)
14208 {
14209 pos_before = glyph->charpos;
14210 glyph_before = glyph;
14211 }
14212 else if (0 < dpos && dpos < pos_after - pt_old)
14213 {
14214 pos_after = glyph->charpos;
14215 glyph_after = glyph;
14216 }
14217 }
14218 else if (dpos == 0)
14219 match_with_avoid_cursor = 1;
14220 }
14221 else if (STRINGP (glyph->object))
14222 {
14223 Lisp_Object chprop;
14224 ptrdiff_t glyph_pos = glyph->charpos;
14225
14226 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14227 glyph->object);
14228 if (!NILP (chprop))
14229 {
14230 /* If the string came from a `display' text property,
14231 look up the buffer position of that property and
14232 use that position to update bpos_max, as if we
14233 actually saw such a position in one of the row's
14234 glyphs. This helps with supporting integer values
14235 of `cursor' property on the display string in
14236 situations where most or all of the row's buffer
14237 text is completely covered by display properties,
14238 so that no glyph with valid buffer positions is
14239 ever seen in the row. */
14240 ptrdiff_t prop_pos =
14241 string_buffer_position_lim (glyph->object, pos_before,
14242 pos_after, 0);
14243
14244 if (prop_pos >= pos_before)
14245 bpos_max = prop_pos - 1;
14246 }
14247 if (INTEGERP (chprop))
14248 {
14249 bpos_covered = bpos_max + XINT (chprop);
14250 /* If the `cursor' property covers buffer positions up
14251 to and including point, we should display cursor on
14252 this glyph. Note that, if a `cursor' property on one
14253 of the string's characters has an integer value, we
14254 will break out of the loop below _before_ we get to
14255 the position match above. IOW, integer values of
14256 the `cursor' property override the "exact match for
14257 point" strategy of positioning the cursor. */
14258 /* Implementation note: bpos_max == pt_old when, e.g.,
14259 we are in an empty line, where bpos_max is set to
14260 MATRIX_ROW_START_CHARPOS, see above. */
14261 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14262 {
14263 cursor = glyph;
14264 break;
14265 }
14266 }
14267
14268 string_seen = 1;
14269 }
14270 x += glyph->pixel_width;
14271 ++glyph;
14272 }
14273 else if (glyph > end) /* row is reversed */
14274 while (!INTEGERP (glyph->object))
14275 {
14276 if (BUFFERP (glyph->object))
14277 {
14278 ptrdiff_t dpos = glyph->charpos - pt_old;
14279
14280 if (glyph->charpos > bpos_max)
14281 bpos_max = glyph->charpos;
14282 if (glyph->charpos < bpos_min)
14283 bpos_min = glyph->charpos;
14284 if (!glyph->avoid_cursor_p)
14285 {
14286 if (dpos == 0)
14287 {
14288 match_with_avoid_cursor = 0;
14289 break;
14290 }
14291 if (0 > dpos && dpos > pos_before - pt_old)
14292 {
14293 pos_before = glyph->charpos;
14294 glyph_before = glyph;
14295 }
14296 else if (0 < dpos && dpos < pos_after - pt_old)
14297 {
14298 pos_after = glyph->charpos;
14299 glyph_after = glyph;
14300 }
14301 }
14302 else if (dpos == 0)
14303 match_with_avoid_cursor = 1;
14304 }
14305 else if (STRINGP (glyph->object))
14306 {
14307 Lisp_Object chprop;
14308 ptrdiff_t glyph_pos = glyph->charpos;
14309
14310 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14311 glyph->object);
14312 if (!NILP (chprop))
14313 {
14314 ptrdiff_t prop_pos =
14315 string_buffer_position_lim (glyph->object, pos_before,
14316 pos_after, 0);
14317
14318 if (prop_pos >= pos_before)
14319 bpos_max = prop_pos - 1;
14320 }
14321 if (INTEGERP (chprop))
14322 {
14323 bpos_covered = bpos_max + XINT (chprop);
14324 /* If the `cursor' property covers buffer positions up
14325 to and including point, we should display cursor on
14326 this glyph. */
14327 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14328 {
14329 cursor = glyph;
14330 break;
14331 }
14332 }
14333 string_seen = 1;
14334 }
14335 --glyph;
14336 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14337 {
14338 x--; /* can't use any pixel_width */
14339 break;
14340 }
14341 x -= glyph->pixel_width;
14342 }
14343
14344 /* Step 2: If we didn't find an exact match for point, we need to
14345 look for a proper place to put the cursor among glyphs between
14346 GLYPH_BEFORE and GLYPH_AFTER. */
14347 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14348 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14349 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14350 {
14351 /* An empty line has a single glyph whose OBJECT is zero and
14352 whose CHARPOS is the position of a newline on that line.
14353 Note that on a TTY, there are more glyphs after that, which
14354 were produced by extend_face_to_end_of_line, but their
14355 CHARPOS is zero or negative. */
14356 int empty_line_p =
14357 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14358 && INTEGERP (glyph->object) && glyph->charpos > 0
14359 /* On a TTY, continued and truncated rows also have a glyph at
14360 their end whose OBJECT is zero and whose CHARPOS is
14361 positive (the continuation and truncation glyphs), but such
14362 rows are obviously not "empty". */
14363 && !(row->continued_p || row->truncated_on_right_p);
14364
14365 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14366 {
14367 ptrdiff_t ellipsis_pos;
14368
14369 /* Scan back over the ellipsis glyphs. */
14370 if (!row->reversed_p)
14371 {
14372 ellipsis_pos = (glyph - 1)->charpos;
14373 while (glyph > row->glyphs[TEXT_AREA]
14374 && (glyph - 1)->charpos == ellipsis_pos)
14375 glyph--, x -= glyph->pixel_width;
14376 /* That loop always goes one position too far, including
14377 the glyph before the ellipsis. So scan forward over
14378 that one. */
14379 x += glyph->pixel_width;
14380 glyph++;
14381 }
14382 else /* row is reversed */
14383 {
14384 ellipsis_pos = (glyph + 1)->charpos;
14385 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14386 && (glyph + 1)->charpos == ellipsis_pos)
14387 glyph++, x += glyph->pixel_width;
14388 x -= glyph->pixel_width;
14389 glyph--;
14390 }
14391 }
14392 else if (match_with_avoid_cursor)
14393 {
14394 cursor = glyph_after;
14395 x = -1;
14396 }
14397 else if (string_seen)
14398 {
14399 int incr = row->reversed_p ? -1 : +1;
14400
14401 /* Need to find the glyph that came out of a string which is
14402 present at point. That glyph is somewhere between
14403 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14404 positioned between POS_BEFORE and POS_AFTER in the
14405 buffer. */
14406 struct glyph *start, *stop;
14407 ptrdiff_t pos = pos_before;
14408
14409 x = -1;
14410
14411 /* If the row ends in a newline from a display string,
14412 reordering could have moved the glyphs belonging to the
14413 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14414 in this case we extend the search to the last glyph in
14415 the row that was not inserted by redisplay. */
14416 if (row->ends_in_newline_from_string_p)
14417 {
14418 glyph_after = end;
14419 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14420 }
14421
14422 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14423 correspond to POS_BEFORE and POS_AFTER, respectively. We
14424 need START and STOP in the order that corresponds to the
14425 row's direction as given by its reversed_p flag. If the
14426 directionality of characters between POS_BEFORE and
14427 POS_AFTER is the opposite of the row's base direction,
14428 these characters will have been reordered for display,
14429 and we need to reverse START and STOP. */
14430 if (!row->reversed_p)
14431 {
14432 start = min (glyph_before, glyph_after);
14433 stop = max (glyph_before, glyph_after);
14434 }
14435 else
14436 {
14437 start = max (glyph_before, glyph_after);
14438 stop = min (glyph_before, glyph_after);
14439 }
14440 for (glyph = start + incr;
14441 row->reversed_p ? glyph > stop : glyph < stop; )
14442 {
14443
14444 /* Any glyphs that come from the buffer are here because
14445 of bidi reordering. Skip them, and only pay
14446 attention to glyphs that came from some string. */
14447 if (STRINGP (glyph->object))
14448 {
14449 Lisp_Object str;
14450 ptrdiff_t tem;
14451 /* If the display property covers the newline, we
14452 need to search for it one position farther. */
14453 ptrdiff_t lim = pos_after
14454 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14455
14456 string_from_text_prop = 0;
14457 str = glyph->object;
14458 tem = string_buffer_position_lim (str, pos, lim, 0);
14459 if (tem == 0 /* from overlay */
14460 || pos <= tem)
14461 {
14462 /* If the string from which this glyph came is
14463 found in the buffer at point, or at position
14464 that is closer to point than pos_after, then
14465 we've found the glyph we've been looking for.
14466 If it comes from an overlay (tem == 0), and
14467 it has the `cursor' property on one of its
14468 glyphs, record that glyph as a candidate for
14469 displaying the cursor. (As in the
14470 unidirectional version, we will display the
14471 cursor on the last candidate we find.) */
14472 if (tem == 0
14473 || tem == pt_old
14474 || (tem - pt_old > 0 && tem < pos_after))
14475 {
14476 /* The glyphs from this string could have
14477 been reordered. Find the one with the
14478 smallest string position. Or there could
14479 be a character in the string with the
14480 `cursor' property, which means display
14481 cursor on that character's glyph. */
14482 ptrdiff_t strpos = glyph->charpos;
14483
14484 if (tem)
14485 {
14486 cursor = glyph;
14487 string_from_text_prop = 1;
14488 }
14489 for ( ;
14490 (row->reversed_p ? glyph > stop : glyph < stop)
14491 && EQ (glyph->object, str);
14492 glyph += incr)
14493 {
14494 Lisp_Object cprop;
14495 ptrdiff_t gpos = glyph->charpos;
14496
14497 cprop = Fget_char_property (make_number (gpos),
14498 Qcursor,
14499 glyph->object);
14500 if (!NILP (cprop))
14501 {
14502 cursor = glyph;
14503 break;
14504 }
14505 if (tem && glyph->charpos < strpos)
14506 {
14507 strpos = glyph->charpos;
14508 cursor = glyph;
14509 }
14510 }
14511
14512 if (tem == pt_old
14513 || (tem - pt_old > 0 && tem < pos_after))
14514 goto compute_x;
14515 }
14516 if (tem)
14517 pos = tem + 1; /* don't find previous instances */
14518 }
14519 /* This string is not what we want; skip all of the
14520 glyphs that came from it. */
14521 while ((row->reversed_p ? glyph > stop : glyph < stop)
14522 && EQ (glyph->object, str))
14523 glyph += incr;
14524 }
14525 else
14526 glyph += incr;
14527 }
14528
14529 /* If we reached the end of the line, and END was from a string,
14530 the cursor is not on this line. */
14531 if (cursor == NULL
14532 && (row->reversed_p ? glyph <= end : glyph >= end)
14533 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14534 && STRINGP (end->object)
14535 && row->continued_p)
14536 return 0;
14537 }
14538 /* A truncated row may not include PT among its character positions.
14539 Setting the cursor inside the scroll margin will trigger
14540 recalculation of hscroll in hscroll_window_tree. But if a
14541 display string covers point, defer to the string-handling
14542 code below to figure this out. */
14543 else if (row->truncated_on_left_p && pt_old < bpos_min)
14544 {
14545 cursor = glyph_before;
14546 x = -1;
14547 }
14548 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14549 /* Zero-width characters produce no glyphs. */
14550 || (!empty_line_p
14551 && (row->reversed_p
14552 ? glyph_after > glyphs_end
14553 : glyph_after < glyphs_end)))
14554 {
14555 cursor = glyph_after;
14556 x = -1;
14557 }
14558 }
14559
14560 compute_x:
14561 if (cursor != NULL)
14562 glyph = cursor;
14563 else if (glyph == glyphs_end
14564 && pos_before == pos_after
14565 && STRINGP ((row->reversed_p
14566 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14567 : row->glyphs[TEXT_AREA])->object))
14568 {
14569 /* If all the glyphs of this row came from strings, put the
14570 cursor on the first glyph of the row. This avoids having the
14571 cursor outside of the text area in this very rare and hard
14572 use case. */
14573 glyph =
14574 row->reversed_p
14575 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14576 : row->glyphs[TEXT_AREA];
14577 }
14578 if (x < 0)
14579 {
14580 struct glyph *g;
14581
14582 /* Need to compute x that corresponds to GLYPH. */
14583 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14584 {
14585 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14586 emacs_abort ();
14587 x += g->pixel_width;
14588 }
14589 }
14590
14591 /* ROW could be part of a continued line, which, under bidi
14592 reordering, might have other rows whose start and end charpos
14593 occlude point. Only set w->cursor if we found a better
14594 approximation to the cursor position than we have from previously
14595 examined candidate rows belonging to the same continued line. */
14596 if (/* We already have a candidate row. */
14597 w->cursor.vpos >= 0
14598 /* That candidate is not the row we are processing. */
14599 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14600 /* Make sure cursor.vpos specifies a row whose start and end
14601 charpos occlude point, and it is valid candidate for being a
14602 cursor-row. This is because some callers of this function
14603 leave cursor.vpos at the row where the cursor was displayed
14604 during the last redisplay cycle. */
14605 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14606 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14607 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14608 {
14609 struct glyph *g1
14610 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14611
14612 /* Don't consider glyphs that are outside TEXT_AREA. */
14613 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14614 return 0;
14615 /* Keep the candidate whose buffer position is the closest to
14616 point or has the `cursor' property. */
14617 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14618 w->cursor.hpos >= 0
14619 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14620 && ((BUFFERP (g1->object)
14621 && (g1->charpos == pt_old /* An exact match always wins. */
14622 || (BUFFERP (glyph->object)
14623 && eabs (g1->charpos - pt_old)
14624 < eabs (glyph->charpos - pt_old))))
14625 /* Previous candidate is a glyph from a string that has
14626 a non-nil `cursor' property. */
14627 || (STRINGP (g1->object)
14628 && (!NILP (Fget_char_property (make_number (g1->charpos),
14629 Qcursor, g1->object))
14630 /* Previous candidate is from the same display
14631 string as this one, and the display string
14632 came from a text property. */
14633 || (EQ (g1->object, glyph->object)
14634 && string_from_text_prop)
14635 /* this candidate is from newline and its
14636 position is not an exact match */
14637 || (INTEGERP (glyph->object)
14638 && glyph->charpos != pt_old)))))
14639 return 0;
14640 /* If this candidate gives an exact match, use that. */
14641 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14642 /* If this candidate is a glyph created for the
14643 terminating newline of a line, and point is on that
14644 newline, it wins because it's an exact match. */
14645 || (!row->continued_p
14646 && INTEGERP (glyph->object)
14647 && glyph->charpos == 0
14648 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14649 /* Otherwise, keep the candidate that comes from a row
14650 spanning less buffer positions. This may win when one or
14651 both candidate positions are on glyphs that came from
14652 display strings, for which we cannot compare buffer
14653 positions. */
14654 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14655 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14656 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14657 return 0;
14658 }
14659 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14660 w->cursor.x = x;
14661 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14662 w->cursor.y = row->y + dy;
14663
14664 if (w == XWINDOW (selected_window))
14665 {
14666 if (!row->continued_p
14667 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14668 && row->x == 0)
14669 {
14670 this_line_buffer = XBUFFER (w->contents);
14671
14672 CHARPOS (this_line_start_pos)
14673 = MATRIX_ROW_START_CHARPOS (row) + delta;
14674 BYTEPOS (this_line_start_pos)
14675 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14676
14677 CHARPOS (this_line_end_pos)
14678 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14679 BYTEPOS (this_line_end_pos)
14680 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14681
14682 this_line_y = w->cursor.y;
14683 this_line_pixel_height = row->height;
14684 this_line_vpos = w->cursor.vpos;
14685 this_line_start_x = row->x;
14686 }
14687 else
14688 CHARPOS (this_line_start_pos) = 0;
14689 }
14690
14691 return 1;
14692 }
14693
14694
14695 /* Run window scroll functions, if any, for WINDOW with new window
14696 start STARTP. Sets the window start of WINDOW to that position.
14697
14698 We assume that the window's buffer is really current. */
14699
14700 static struct text_pos
14701 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14702 {
14703 struct window *w = XWINDOW (window);
14704 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14705
14706 eassert (current_buffer == XBUFFER (w->contents));
14707
14708 if (!NILP (Vwindow_scroll_functions))
14709 {
14710 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14711 make_number (CHARPOS (startp)));
14712 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14713 /* In case the hook functions switch buffers. */
14714 set_buffer_internal (XBUFFER (w->contents));
14715 }
14716
14717 return startp;
14718 }
14719
14720
14721 /* Make sure the line containing the cursor is fully visible.
14722 A value of 1 means there is nothing to be done.
14723 (Either the line is fully visible, or it cannot be made so,
14724 or we cannot tell.)
14725
14726 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14727 is higher than window.
14728
14729 A value of 0 means the caller should do scrolling
14730 as if point had gone off the screen. */
14731
14732 static int
14733 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14734 {
14735 struct glyph_matrix *matrix;
14736 struct glyph_row *row;
14737 int window_height;
14738
14739 if (!make_cursor_line_fully_visible_p)
14740 return 1;
14741
14742 /* It's not always possible to find the cursor, e.g, when a window
14743 is full of overlay strings. Don't do anything in that case. */
14744 if (w->cursor.vpos < 0)
14745 return 1;
14746
14747 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14748 row = MATRIX_ROW (matrix, w->cursor.vpos);
14749
14750 /* If the cursor row is not partially visible, there's nothing to do. */
14751 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14752 return 1;
14753
14754 /* If the row the cursor is in is taller than the window's height,
14755 it's not clear what to do, so do nothing. */
14756 window_height = window_box_height (w);
14757 if (row->height >= window_height)
14758 {
14759 if (!force_p || MINI_WINDOW_P (w)
14760 || w->vscroll || w->cursor.vpos == 0)
14761 return 1;
14762 }
14763 return 0;
14764 }
14765
14766
14767 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14768 non-zero means only WINDOW is redisplayed in redisplay_internal.
14769 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14770 in redisplay_window to bring a partially visible line into view in
14771 the case that only the cursor has moved.
14772
14773 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14774 last screen line's vertical height extends past the end of the screen.
14775
14776 Value is
14777
14778 1 if scrolling succeeded
14779
14780 0 if scrolling didn't find point.
14781
14782 -1 if new fonts have been loaded so that we must interrupt
14783 redisplay, adjust glyph matrices, and try again. */
14784
14785 enum
14786 {
14787 SCROLLING_SUCCESS,
14788 SCROLLING_FAILED,
14789 SCROLLING_NEED_LARGER_MATRICES
14790 };
14791
14792 /* If scroll-conservatively is more than this, never recenter.
14793
14794 If you change this, don't forget to update the doc string of
14795 `scroll-conservatively' and the Emacs manual. */
14796 #define SCROLL_LIMIT 100
14797
14798 static int
14799 try_scrolling (Lisp_Object window, int just_this_one_p,
14800 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14801 int temp_scroll_step, int last_line_misfit)
14802 {
14803 struct window *w = XWINDOW (window);
14804 struct frame *f = XFRAME (w->frame);
14805 struct text_pos pos, startp;
14806 struct it it;
14807 int this_scroll_margin, scroll_max, rc, height;
14808 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14809 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14810 Lisp_Object aggressive;
14811 /* We will never try scrolling more than this number of lines. */
14812 int scroll_limit = SCROLL_LIMIT;
14813 int frame_line_height = default_line_pixel_height (w);
14814 int window_total_lines
14815 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14816
14817 #ifdef GLYPH_DEBUG
14818 debug_method_add (w, "try_scrolling");
14819 #endif
14820
14821 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14822
14823 /* Compute scroll margin height in pixels. We scroll when point is
14824 within this distance from the top or bottom of the window. */
14825 if (scroll_margin > 0)
14826 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14827 * frame_line_height;
14828 else
14829 this_scroll_margin = 0;
14830
14831 /* Force arg_scroll_conservatively to have a reasonable value, to
14832 avoid scrolling too far away with slow move_it_* functions. Note
14833 that the user can supply scroll-conservatively equal to
14834 `most-positive-fixnum', which can be larger than INT_MAX. */
14835 if (arg_scroll_conservatively > scroll_limit)
14836 {
14837 arg_scroll_conservatively = scroll_limit + 1;
14838 scroll_max = scroll_limit * frame_line_height;
14839 }
14840 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14841 /* Compute how much we should try to scroll maximally to bring
14842 point into view. */
14843 scroll_max = (max (scroll_step,
14844 max (arg_scroll_conservatively, temp_scroll_step))
14845 * frame_line_height);
14846 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14847 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14848 /* We're trying to scroll because of aggressive scrolling but no
14849 scroll_step is set. Choose an arbitrary one. */
14850 scroll_max = 10 * frame_line_height;
14851 else
14852 scroll_max = 0;
14853
14854 too_near_end:
14855
14856 /* Decide whether to scroll down. */
14857 if (PT > CHARPOS (startp))
14858 {
14859 int scroll_margin_y;
14860
14861 /* Compute the pixel ypos of the scroll margin, then move IT to
14862 either that ypos or PT, whichever comes first. */
14863 start_display (&it, w, startp);
14864 scroll_margin_y = it.last_visible_y - this_scroll_margin
14865 - frame_line_height * extra_scroll_margin_lines;
14866 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14867 (MOVE_TO_POS | MOVE_TO_Y));
14868
14869 if (PT > CHARPOS (it.current.pos))
14870 {
14871 int y0 = line_bottom_y (&it);
14872 /* Compute how many pixels below window bottom to stop searching
14873 for PT. This avoids costly search for PT that is far away if
14874 the user limited scrolling by a small number of lines, but
14875 always finds PT if scroll_conservatively is set to a large
14876 number, such as most-positive-fixnum. */
14877 int slack = max (scroll_max, 10 * frame_line_height);
14878 int y_to_move = it.last_visible_y + slack;
14879
14880 /* Compute the distance from the scroll margin to PT or to
14881 the scroll limit, whichever comes first. This should
14882 include the height of the cursor line, to make that line
14883 fully visible. */
14884 move_it_to (&it, PT, -1, y_to_move,
14885 -1, MOVE_TO_POS | MOVE_TO_Y);
14886 dy = line_bottom_y (&it) - y0;
14887
14888 if (dy > scroll_max)
14889 return SCROLLING_FAILED;
14890
14891 if (dy > 0)
14892 scroll_down_p = 1;
14893 }
14894 }
14895
14896 if (scroll_down_p)
14897 {
14898 /* Point is in or below the bottom scroll margin, so move the
14899 window start down. If scrolling conservatively, move it just
14900 enough down to make point visible. If scroll_step is set,
14901 move it down by scroll_step. */
14902 if (arg_scroll_conservatively)
14903 amount_to_scroll
14904 = min (max (dy, frame_line_height),
14905 frame_line_height * arg_scroll_conservatively);
14906 else if (scroll_step || temp_scroll_step)
14907 amount_to_scroll = scroll_max;
14908 else
14909 {
14910 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14911 height = WINDOW_BOX_TEXT_HEIGHT (w);
14912 if (NUMBERP (aggressive))
14913 {
14914 double float_amount = XFLOATINT (aggressive) * height;
14915 int aggressive_scroll = float_amount;
14916 if (aggressive_scroll == 0 && float_amount > 0)
14917 aggressive_scroll = 1;
14918 /* Don't let point enter the scroll margin near top of
14919 the window. This could happen if the value of
14920 scroll_up_aggressively is too large and there are
14921 non-zero margins, because scroll_up_aggressively
14922 means put point that fraction of window height
14923 _from_the_bottom_margin_. */
14924 if (aggressive_scroll + 2*this_scroll_margin > height)
14925 aggressive_scroll = height - 2*this_scroll_margin;
14926 amount_to_scroll = dy + aggressive_scroll;
14927 }
14928 }
14929
14930 if (amount_to_scroll <= 0)
14931 return SCROLLING_FAILED;
14932
14933 start_display (&it, w, startp);
14934 if (arg_scroll_conservatively <= scroll_limit)
14935 move_it_vertically (&it, amount_to_scroll);
14936 else
14937 {
14938 /* Extra precision for users who set scroll-conservatively
14939 to a large number: make sure the amount we scroll
14940 the window start is never less than amount_to_scroll,
14941 which was computed as distance from window bottom to
14942 point. This matters when lines at window top and lines
14943 below window bottom have different height. */
14944 struct it it1;
14945 void *it1data = NULL;
14946 /* We use a temporary it1 because line_bottom_y can modify
14947 its argument, if it moves one line down; see there. */
14948 int start_y;
14949
14950 SAVE_IT (it1, it, it1data);
14951 start_y = line_bottom_y (&it1);
14952 do {
14953 RESTORE_IT (&it, &it, it1data);
14954 move_it_by_lines (&it, 1);
14955 SAVE_IT (it1, it, it1data);
14956 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14957 }
14958
14959 /* If STARTP is unchanged, move it down another screen line. */
14960 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14961 move_it_by_lines (&it, 1);
14962 startp = it.current.pos;
14963 }
14964 else
14965 {
14966 struct text_pos scroll_margin_pos = startp;
14967 int y_offset = 0;
14968
14969 /* See if point is inside the scroll margin at the top of the
14970 window. */
14971 if (this_scroll_margin)
14972 {
14973 int y_start;
14974
14975 start_display (&it, w, startp);
14976 y_start = it.current_y;
14977 move_it_vertically (&it, this_scroll_margin);
14978 scroll_margin_pos = it.current.pos;
14979 /* If we didn't move enough before hitting ZV, request
14980 additional amount of scroll, to move point out of the
14981 scroll margin. */
14982 if (IT_CHARPOS (it) == ZV
14983 && it.current_y - y_start < this_scroll_margin)
14984 y_offset = this_scroll_margin - (it.current_y - y_start);
14985 }
14986
14987 if (PT < CHARPOS (scroll_margin_pos))
14988 {
14989 /* Point is in the scroll margin at the top of the window or
14990 above what is displayed in the window. */
14991 int y0, y_to_move;
14992
14993 /* Compute the vertical distance from PT to the scroll
14994 margin position. Move as far as scroll_max allows, or
14995 one screenful, or 10 screen lines, whichever is largest.
14996 Give up if distance is greater than scroll_max or if we
14997 didn't reach the scroll margin position. */
14998 SET_TEXT_POS (pos, PT, PT_BYTE);
14999 start_display (&it, w, pos);
15000 y0 = it.current_y;
15001 y_to_move = max (it.last_visible_y,
15002 max (scroll_max, 10 * frame_line_height));
15003 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15004 y_to_move, -1,
15005 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15006 dy = it.current_y - y0;
15007 if (dy > scroll_max
15008 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15009 return SCROLLING_FAILED;
15010
15011 /* Additional scroll for when ZV was too close to point. */
15012 dy += y_offset;
15013
15014 /* Compute new window start. */
15015 start_display (&it, w, startp);
15016
15017 if (arg_scroll_conservatively)
15018 amount_to_scroll = max (dy, frame_line_height *
15019 max (scroll_step, temp_scroll_step));
15020 else if (scroll_step || temp_scroll_step)
15021 amount_to_scroll = scroll_max;
15022 else
15023 {
15024 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15025 height = WINDOW_BOX_TEXT_HEIGHT (w);
15026 if (NUMBERP (aggressive))
15027 {
15028 double float_amount = XFLOATINT (aggressive) * height;
15029 int aggressive_scroll = float_amount;
15030 if (aggressive_scroll == 0 && float_amount > 0)
15031 aggressive_scroll = 1;
15032 /* Don't let point enter the scroll margin near
15033 bottom of the window, if the value of
15034 scroll_down_aggressively happens to be too
15035 large. */
15036 if (aggressive_scroll + 2*this_scroll_margin > height)
15037 aggressive_scroll = height - 2*this_scroll_margin;
15038 amount_to_scroll = dy + aggressive_scroll;
15039 }
15040 }
15041
15042 if (amount_to_scroll <= 0)
15043 return SCROLLING_FAILED;
15044
15045 move_it_vertically_backward (&it, amount_to_scroll);
15046 startp = it.current.pos;
15047 }
15048 }
15049
15050 /* Run window scroll functions. */
15051 startp = run_window_scroll_functions (window, startp);
15052
15053 /* Display the window. Give up if new fonts are loaded, or if point
15054 doesn't appear. */
15055 if (!try_window (window, startp, 0))
15056 rc = SCROLLING_NEED_LARGER_MATRICES;
15057 else if (w->cursor.vpos < 0)
15058 {
15059 clear_glyph_matrix (w->desired_matrix);
15060 rc = SCROLLING_FAILED;
15061 }
15062 else
15063 {
15064 /* Maybe forget recorded base line for line number display. */
15065 if (!just_this_one_p
15066 || current_buffer->clip_changed
15067 || BEG_UNCHANGED < CHARPOS (startp))
15068 w->base_line_number = 0;
15069
15070 /* If cursor ends up on a partially visible line,
15071 treat that as being off the bottom of the screen. */
15072 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15073 /* It's possible that the cursor is on the first line of the
15074 buffer, which is partially obscured due to a vscroll
15075 (Bug#7537). In that case, avoid looping forever. */
15076 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15077 {
15078 clear_glyph_matrix (w->desired_matrix);
15079 ++extra_scroll_margin_lines;
15080 goto too_near_end;
15081 }
15082 rc = SCROLLING_SUCCESS;
15083 }
15084
15085 return rc;
15086 }
15087
15088
15089 /* Compute a suitable window start for window W if display of W starts
15090 on a continuation line. Value is non-zero if a new window start
15091 was computed.
15092
15093 The new window start will be computed, based on W's width, starting
15094 from the start of the continued line. It is the start of the
15095 screen line with the minimum distance from the old start W->start. */
15096
15097 static int
15098 compute_window_start_on_continuation_line (struct window *w)
15099 {
15100 struct text_pos pos, start_pos;
15101 int window_start_changed_p = 0;
15102
15103 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15104
15105 /* If window start is on a continuation line... Window start may be
15106 < BEGV in case there's invisible text at the start of the
15107 buffer (M-x rmail, for example). */
15108 if (CHARPOS (start_pos) > BEGV
15109 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15110 {
15111 struct it it;
15112 struct glyph_row *row;
15113
15114 /* Handle the case that the window start is out of range. */
15115 if (CHARPOS (start_pos) < BEGV)
15116 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15117 else if (CHARPOS (start_pos) > ZV)
15118 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15119
15120 /* Find the start of the continued line. This should be fast
15121 because find_newline is fast (newline cache). */
15122 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15123 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15124 row, DEFAULT_FACE_ID);
15125 reseat_at_previous_visible_line_start (&it);
15126
15127 /* If the line start is "too far" away from the window start,
15128 say it takes too much time to compute a new window start. */
15129 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15130 /* PXW: Do we need upper bounds here? */
15131 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15132 {
15133 int min_distance, distance;
15134
15135 /* Move forward by display lines to find the new window
15136 start. If window width was enlarged, the new start can
15137 be expected to be > the old start. If window width was
15138 decreased, the new window start will be < the old start.
15139 So, we're looking for the display line start with the
15140 minimum distance from the old window start. */
15141 pos = it.current.pos;
15142 min_distance = INFINITY;
15143 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15144 distance < min_distance)
15145 {
15146 min_distance = distance;
15147 pos = it.current.pos;
15148 if (it.line_wrap == WORD_WRAP)
15149 {
15150 /* Under WORD_WRAP, move_it_by_lines is likely to
15151 overshoot and stop not at the first, but the
15152 second character from the left margin. So in
15153 that case, we need a more tight control on the X
15154 coordinate of the iterator than move_it_by_lines
15155 promises in its contract. The method is to first
15156 go to the last (rightmost) visible character of a
15157 line, then move to the leftmost character on the
15158 next line in a separate call. */
15159 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15160 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15161 move_it_to (&it, ZV, 0,
15162 it.current_y + it.max_ascent + it.max_descent, -1,
15163 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15164 }
15165 else
15166 move_it_by_lines (&it, 1);
15167 }
15168
15169 /* Set the window start there. */
15170 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15171 window_start_changed_p = 1;
15172 }
15173 }
15174
15175 return window_start_changed_p;
15176 }
15177
15178
15179 /* Try cursor movement in case text has not changed in window WINDOW,
15180 with window start STARTP. Value is
15181
15182 CURSOR_MOVEMENT_SUCCESS if successful
15183
15184 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15185
15186 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15187 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15188 we want to scroll as if scroll-step were set to 1. See the code.
15189
15190 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15191 which case we have to abort this redisplay, and adjust matrices
15192 first. */
15193
15194 enum
15195 {
15196 CURSOR_MOVEMENT_SUCCESS,
15197 CURSOR_MOVEMENT_CANNOT_BE_USED,
15198 CURSOR_MOVEMENT_MUST_SCROLL,
15199 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15200 };
15201
15202 static int
15203 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15204 {
15205 struct window *w = XWINDOW (window);
15206 struct frame *f = XFRAME (w->frame);
15207 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15208
15209 #ifdef GLYPH_DEBUG
15210 if (inhibit_try_cursor_movement)
15211 return rc;
15212 #endif
15213
15214 /* Previously, there was a check for Lisp integer in the
15215 if-statement below. Now, this field is converted to
15216 ptrdiff_t, thus zero means invalid position in a buffer. */
15217 eassert (w->last_point > 0);
15218 /* Likewise there was a check whether window_end_vpos is nil or larger
15219 than the window. Now window_end_vpos is int and so never nil, but
15220 let's leave eassert to check whether it fits in the window. */
15221 eassert (w->window_end_vpos < w->current_matrix->nrows);
15222
15223 /* Handle case where text has not changed, only point, and it has
15224 not moved off the frame. */
15225 if (/* Point may be in this window. */
15226 PT >= CHARPOS (startp)
15227 /* Selective display hasn't changed. */
15228 && !current_buffer->clip_changed
15229 /* Function force-mode-line-update is used to force a thorough
15230 redisplay. It sets either windows_or_buffers_changed or
15231 update_mode_lines. So don't take a shortcut here for these
15232 cases. */
15233 && !update_mode_lines
15234 && !windows_or_buffers_changed
15235 && !f->cursor_type_changed
15236 && NILP (Vshow_trailing_whitespace)
15237 /* This code is not used for mini-buffer for the sake of the case
15238 of redisplaying to replace an echo area message; since in
15239 that case the mini-buffer contents per se are usually
15240 unchanged. This code is of no real use in the mini-buffer
15241 since the handling of this_line_start_pos, etc., in redisplay
15242 handles the same cases. */
15243 && !EQ (window, minibuf_window)
15244 && (FRAME_WINDOW_P (f)
15245 || !overlay_arrow_in_current_buffer_p ()))
15246 {
15247 int this_scroll_margin, top_scroll_margin;
15248 struct glyph_row *row = NULL;
15249 int frame_line_height = default_line_pixel_height (w);
15250 int window_total_lines
15251 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15252
15253 #ifdef GLYPH_DEBUG
15254 debug_method_add (w, "cursor movement");
15255 #endif
15256
15257 /* Scroll if point within this distance from the top or bottom
15258 of the window. This is a pixel value. */
15259 if (scroll_margin > 0)
15260 {
15261 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15262 this_scroll_margin *= frame_line_height;
15263 }
15264 else
15265 this_scroll_margin = 0;
15266
15267 top_scroll_margin = this_scroll_margin;
15268 if (WINDOW_WANTS_HEADER_LINE_P (w))
15269 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15270
15271 /* Start with the row the cursor was displayed during the last
15272 not paused redisplay. Give up if that row is not valid. */
15273 if (w->last_cursor_vpos < 0
15274 || w->last_cursor_vpos >= w->current_matrix->nrows)
15275 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15276 else
15277 {
15278 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15279 if (row->mode_line_p)
15280 ++row;
15281 if (!row->enabled_p)
15282 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15283 }
15284
15285 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15286 {
15287 int scroll_p = 0, must_scroll = 0;
15288 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15289
15290 if (PT > w->last_point)
15291 {
15292 /* Point has moved forward. */
15293 while (MATRIX_ROW_END_CHARPOS (row) < PT
15294 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15295 {
15296 eassert (row->enabled_p);
15297 ++row;
15298 }
15299
15300 /* If the end position of a row equals the start
15301 position of the next row, and PT is at that position,
15302 we would rather display cursor in the next line. */
15303 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15304 && MATRIX_ROW_END_CHARPOS (row) == PT
15305 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15306 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15307 && !cursor_row_p (row))
15308 ++row;
15309
15310 /* If within the scroll margin, scroll. Note that
15311 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15312 the next line would be drawn, and that
15313 this_scroll_margin can be zero. */
15314 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15315 || PT > MATRIX_ROW_END_CHARPOS (row)
15316 /* Line is completely visible last line in window
15317 and PT is to be set in the next line. */
15318 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15319 && PT == MATRIX_ROW_END_CHARPOS (row)
15320 && !row->ends_at_zv_p
15321 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15322 scroll_p = 1;
15323 }
15324 else if (PT < w->last_point)
15325 {
15326 /* Cursor has to be moved backward. Note that PT >=
15327 CHARPOS (startp) because of the outer if-statement. */
15328 while (!row->mode_line_p
15329 && (MATRIX_ROW_START_CHARPOS (row) > PT
15330 || (MATRIX_ROW_START_CHARPOS (row) == PT
15331 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15332 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15333 row > w->current_matrix->rows
15334 && (row-1)->ends_in_newline_from_string_p))))
15335 && (row->y > top_scroll_margin
15336 || CHARPOS (startp) == BEGV))
15337 {
15338 eassert (row->enabled_p);
15339 --row;
15340 }
15341
15342 /* Consider the following case: Window starts at BEGV,
15343 there is invisible, intangible text at BEGV, so that
15344 display starts at some point START > BEGV. It can
15345 happen that we are called with PT somewhere between
15346 BEGV and START. Try to handle that case. */
15347 if (row < w->current_matrix->rows
15348 || row->mode_line_p)
15349 {
15350 row = w->current_matrix->rows;
15351 if (row->mode_line_p)
15352 ++row;
15353 }
15354
15355 /* Due to newlines in overlay strings, we may have to
15356 skip forward over overlay strings. */
15357 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15358 && MATRIX_ROW_END_CHARPOS (row) == PT
15359 && !cursor_row_p (row))
15360 ++row;
15361
15362 /* If within the scroll margin, scroll. */
15363 if (row->y < top_scroll_margin
15364 && CHARPOS (startp) != BEGV)
15365 scroll_p = 1;
15366 }
15367 else
15368 {
15369 /* Cursor did not move. So don't scroll even if cursor line
15370 is partially visible, as it was so before. */
15371 rc = CURSOR_MOVEMENT_SUCCESS;
15372 }
15373
15374 if (PT < MATRIX_ROW_START_CHARPOS (row)
15375 || PT > MATRIX_ROW_END_CHARPOS (row))
15376 {
15377 /* if PT is not in the glyph row, give up. */
15378 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15379 must_scroll = 1;
15380 }
15381 else if (rc != CURSOR_MOVEMENT_SUCCESS
15382 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15383 {
15384 struct glyph_row *row1;
15385
15386 /* If rows are bidi-reordered and point moved, back up
15387 until we find a row that does not belong to a
15388 continuation line. This is because we must consider
15389 all rows of a continued line as candidates for the
15390 new cursor positioning, since row start and end
15391 positions change non-linearly with vertical position
15392 in such rows. */
15393 /* FIXME: Revisit this when glyph ``spilling'' in
15394 continuation lines' rows is implemented for
15395 bidi-reordered rows. */
15396 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15397 MATRIX_ROW_CONTINUATION_LINE_P (row);
15398 --row)
15399 {
15400 /* If we hit the beginning of the displayed portion
15401 without finding the first row of a continued
15402 line, give up. */
15403 if (row <= row1)
15404 {
15405 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15406 break;
15407 }
15408 eassert (row->enabled_p);
15409 }
15410 }
15411 if (must_scroll)
15412 ;
15413 else if (rc != CURSOR_MOVEMENT_SUCCESS
15414 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15415 /* Make sure this isn't a header line by any chance, since
15416 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15417 && !row->mode_line_p
15418 && make_cursor_line_fully_visible_p)
15419 {
15420 if (PT == MATRIX_ROW_END_CHARPOS (row)
15421 && !row->ends_at_zv_p
15422 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15423 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15424 else if (row->height > window_box_height (w))
15425 {
15426 /* If we end up in a partially visible line, let's
15427 make it fully visible, except when it's taller
15428 than the window, in which case we can't do much
15429 about it. */
15430 *scroll_step = 1;
15431 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15432 }
15433 else
15434 {
15435 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15436 if (!cursor_row_fully_visible_p (w, 0, 1))
15437 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15438 else
15439 rc = CURSOR_MOVEMENT_SUCCESS;
15440 }
15441 }
15442 else if (scroll_p)
15443 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15444 else if (rc != CURSOR_MOVEMENT_SUCCESS
15445 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15446 {
15447 /* With bidi-reordered rows, there could be more than
15448 one candidate row whose start and end positions
15449 occlude point. We need to let set_cursor_from_row
15450 find the best candidate. */
15451 /* FIXME: Revisit this when glyph ``spilling'' in
15452 continuation lines' rows is implemented for
15453 bidi-reordered rows. */
15454 int rv = 0;
15455
15456 do
15457 {
15458 int at_zv_p = 0, exact_match_p = 0;
15459
15460 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15461 && PT <= MATRIX_ROW_END_CHARPOS (row)
15462 && cursor_row_p (row))
15463 rv |= set_cursor_from_row (w, row, w->current_matrix,
15464 0, 0, 0, 0);
15465 /* As soon as we've found the exact match for point,
15466 or the first suitable row whose ends_at_zv_p flag
15467 is set, we are done. */
15468 at_zv_p =
15469 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15470 if (rv && !at_zv_p
15471 && w->cursor.hpos >= 0
15472 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15473 w->cursor.vpos))
15474 {
15475 struct glyph_row *candidate =
15476 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15477 struct glyph *g =
15478 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15479 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15480
15481 exact_match_p =
15482 (BUFFERP (g->object) && g->charpos == PT)
15483 || (INTEGERP (g->object)
15484 && (g->charpos == PT
15485 || (g->charpos == 0 && endpos - 1 == PT)));
15486 }
15487 if (rv && (at_zv_p || exact_match_p))
15488 {
15489 rc = CURSOR_MOVEMENT_SUCCESS;
15490 break;
15491 }
15492 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15493 break;
15494 ++row;
15495 }
15496 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15497 || row->continued_p)
15498 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15499 || (MATRIX_ROW_START_CHARPOS (row) == PT
15500 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15501 /* If we didn't find any candidate rows, or exited the
15502 loop before all the candidates were examined, signal
15503 to the caller that this method failed. */
15504 if (rc != CURSOR_MOVEMENT_SUCCESS
15505 && !(rv
15506 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15507 && !row->continued_p))
15508 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15509 else if (rv)
15510 rc = CURSOR_MOVEMENT_SUCCESS;
15511 }
15512 else
15513 {
15514 do
15515 {
15516 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15517 {
15518 rc = CURSOR_MOVEMENT_SUCCESS;
15519 break;
15520 }
15521 ++row;
15522 }
15523 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15524 && MATRIX_ROW_START_CHARPOS (row) == PT
15525 && cursor_row_p (row));
15526 }
15527 }
15528 }
15529
15530 return rc;
15531 }
15532
15533 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15534 static
15535 #endif
15536 void
15537 set_vertical_scroll_bar (struct window *w)
15538 {
15539 ptrdiff_t start, end, whole;
15540
15541 /* Calculate the start and end positions for the current window.
15542 At some point, it would be nice to choose between scrollbars
15543 which reflect the whole buffer size, with special markers
15544 indicating narrowing, and scrollbars which reflect only the
15545 visible region.
15546
15547 Note that mini-buffers sometimes aren't displaying any text. */
15548 if (!MINI_WINDOW_P (w)
15549 || (w == XWINDOW (minibuf_window)
15550 && NILP (echo_area_buffer[0])))
15551 {
15552 struct buffer *buf = XBUFFER (w->contents);
15553 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15554 start = marker_position (w->start) - BUF_BEGV (buf);
15555 /* I don't think this is guaranteed to be right. For the
15556 moment, we'll pretend it is. */
15557 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15558
15559 if (end < start)
15560 end = start;
15561 if (whole < (end - start))
15562 whole = end - start;
15563 }
15564 else
15565 start = end = whole = 0;
15566
15567 /* Indicate what this scroll bar ought to be displaying now. */
15568 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15569 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15570 (w, end - start, whole, start);
15571 }
15572
15573
15574 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15575 selected_window is redisplayed.
15576
15577 We can return without actually redisplaying the window if fonts has been
15578 changed on window's frame. In that case, redisplay_internal will retry. */
15579
15580 static void
15581 redisplay_window (Lisp_Object window, bool just_this_one_p)
15582 {
15583 struct window *w = XWINDOW (window);
15584 struct frame *f = XFRAME (w->frame);
15585 struct buffer *buffer = XBUFFER (w->contents);
15586 struct buffer *old = current_buffer;
15587 struct text_pos lpoint, opoint, startp;
15588 int update_mode_line;
15589 int tem;
15590 struct it it;
15591 /* Record it now because it's overwritten. */
15592 bool current_matrix_up_to_date_p = false;
15593 bool used_current_matrix_p = false;
15594 /* This is less strict than current_matrix_up_to_date_p.
15595 It indicates that the buffer contents and narrowing are unchanged. */
15596 bool buffer_unchanged_p = false;
15597 int temp_scroll_step = 0;
15598 ptrdiff_t count = SPECPDL_INDEX ();
15599 int rc;
15600 int centering_position = -1;
15601 int last_line_misfit = 0;
15602 ptrdiff_t beg_unchanged, end_unchanged;
15603 int frame_line_height;
15604
15605 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15606 opoint = lpoint;
15607
15608 #ifdef GLYPH_DEBUG
15609 *w->desired_matrix->method = 0;
15610 #endif
15611
15612 if (!just_this_one_p
15613 && REDISPLAY_SOME_P ()
15614 && !w->redisplay
15615 && !f->redisplay
15616 && !buffer->text->redisplay
15617 && BUF_PT (buffer) == w->last_point)
15618 return;
15619
15620 /* Make sure that both W's markers are valid. */
15621 eassert (XMARKER (w->start)->buffer == buffer);
15622 eassert (XMARKER (w->pointm)->buffer == buffer);
15623
15624 restart:
15625 reconsider_clip_changes (w);
15626 frame_line_height = default_line_pixel_height (w);
15627
15628 /* Has the mode line to be updated? */
15629 update_mode_line = (w->update_mode_line
15630 || update_mode_lines
15631 || buffer->clip_changed
15632 || buffer->prevent_redisplay_optimizations_p);
15633
15634 if (!just_this_one_p)
15635 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15636 cleverly elsewhere. */
15637 w->must_be_updated_p = true;
15638
15639 if (MINI_WINDOW_P (w))
15640 {
15641 if (w == XWINDOW (echo_area_window)
15642 && !NILP (echo_area_buffer[0]))
15643 {
15644 if (update_mode_line)
15645 /* We may have to update a tty frame's menu bar or a
15646 tool-bar. Example `M-x C-h C-h C-g'. */
15647 goto finish_menu_bars;
15648 else
15649 /* We've already displayed the echo area glyphs in this window. */
15650 goto finish_scroll_bars;
15651 }
15652 else if ((w != XWINDOW (minibuf_window)
15653 || minibuf_level == 0)
15654 /* When buffer is nonempty, redisplay window normally. */
15655 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15656 /* Quail displays non-mini buffers in minibuffer window.
15657 In that case, redisplay the window normally. */
15658 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15659 {
15660 /* W is a mini-buffer window, but it's not active, so clear
15661 it. */
15662 int yb = window_text_bottom_y (w);
15663 struct glyph_row *row;
15664 int y;
15665
15666 for (y = 0, row = w->desired_matrix->rows;
15667 y < yb;
15668 y += row->height, ++row)
15669 blank_row (w, row, y);
15670 goto finish_scroll_bars;
15671 }
15672
15673 clear_glyph_matrix (w->desired_matrix);
15674 }
15675
15676 /* Otherwise set up data on this window; select its buffer and point
15677 value. */
15678 /* Really select the buffer, for the sake of buffer-local
15679 variables. */
15680 set_buffer_internal_1 (XBUFFER (w->contents));
15681
15682 current_matrix_up_to_date_p
15683 = (w->window_end_valid
15684 && !current_buffer->clip_changed
15685 && !current_buffer->prevent_redisplay_optimizations_p
15686 && !window_outdated (w));
15687
15688 /* Run the window-bottom-change-functions
15689 if it is possible that the text on the screen has changed
15690 (either due to modification of the text, or any other reason). */
15691 if (!current_matrix_up_to_date_p
15692 && !NILP (Vwindow_text_change_functions))
15693 {
15694 safe_run_hooks (Qwindow_text_change_functions);
15695 goto restart;
15696 }
15697
15698 beg_unchanged = BEG_UNCHANGED;
15699 end_unchanged = END_UNCHANGED;
15700
15701 SET_TEXT_POS (opoint, PT, PT_BYTE);
15702
15703 specbind (Qinhibit_point_motion_hooks, Qt);
15704
15705 buffer_unchanged_p
15706 = (w->window_end_valid
15707 && !current_buffer->clip_changed
15708 && !window_outdated (w));
15709
15710 /* When windows_or_buffers_changed is non-zero, we can't rely
15711 on the window end being valid, so set it to zero there. */
15712 if (windows_or_buffers_changed)
15713 {
15714 /* If window starts on a continuation line, maybe adjust the
15715 window start in case the window's width changed. */
15716 if (XMARKER (w->start)->buffer == current_buffer)
15717 compute_window_start_on_continuation_line (w);
15718
15719 w->window_end_valid = false;
15720 /* If so, we also can't rely on current matrix
15721 and should not fool try_cursor_movement below. */
15722 current_matrix_up_to_date_p = false;
15723 }
15724
15725 /* Some sanity checks. */
15726 CHECK_WINDOW_END (w);
15727 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15728 emacs_abort ();
15729 if (BYTEPOS (opoint) < CHARPOS (opoint))
15730 emacs_abort ();
15731
15732 if (mode_line_update_needed (w))
15733 update_mode_line = 1;
15734
15735 /* Point refers normally to the selected window. For any other
15736 window, set up appropriate value. */
15737 if (!EQ (window, selected_window))
15738 {
15739 ptrdiff_t new_pt = marker_position (w->pointm);
15740 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15741 if (new_pt < BEGV)
15742 {
15743 new_pt = BEGV;
15744 new_pt_byte = BEGV_BYTE;
15745 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15746 }
15747 else if (new_pt > (ZV - 1))
15748 {
15749 new_pt = ZV;
15750 new_pt_byte = ZV_BYTE;
15751 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15752 }
15753
15754 /* We don't use SET_PT so that the point-motion hooks don't run. */
15755 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15756 }
15757
15758 /* If any of the character widths specified in the display table
15759 have changed, invalidate the width run cache. It's true that
15760 this may be a bit late to catch such changes, but the rest of
15761 redisplay goes (non-fatally) haywire when the display table is
15762 changed, so why should we worry about doing any better? */
15763 if (current_buffer->width_run_cache
15764 || (current_buffer->base_buffer
15765 && current_buffer->base_buffer->width_run_cache))
15766 {
15767 struct Lisp_Char_Table *disptab = buffer_display_table ();
15768
15769 if (! disptab_matches_widthtab
15770 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15771 {
15772 struct buffer *buf = current_buffer;
15773
15774 if (buf->base_buffer)
15775 buf = buf->base_buffer;
15776 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15777 recompute_width_table (current_buffer, disptab);
15778 }
15779 }
15780
15781 /* If window-start is screwed up, choose a new one. */
15782 if (XMARKER (w->start)->buffer != current_buffer)
15783 goto recenter;
15784
15785 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15786
15787 /* If someone specified a new starting point but did not insist,
15788 check whether it can be used. */
15789 if (w->optional_new_start
15790 && CHARPOS (startp) >= BEGV
15791 && CHARPOS (startp) <= ZV)
15792 {
15793 w->optional_new_start = 0;
15794 start_display (&it, w, startp);
15795 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15796 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15797 if (IT_CHARPOS (it) == PT)
15798 w->force_start = 1;
15799 /* IT may overshoot PT if text at PT is invisible. */
15800 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15801 w->force_start = 1;
15802 }
15803
15804 force_start:
15805
15806 /* Handle case where place to start displaying has been specified,
15807 unless the specified location is outside the accessible range. */
15808 if (w->force_start || window_frozen_p (w))
15809 {
15810 /* We set this later on if we have to adjust point. */
15811 int new_vpos = -1;
15812
15813 w->force_start = 0;
15814 w->vscroll = 0;
15815 w->window_end_valid = 0;
15816
15817 /* Forget any recorded base line for line number display. */
15818 if (!buffer_unchanged_p)
15819 w->base_line_number = 0;
15820
15821 /* Redisplay the mode line. Select the buffer properly for that.
15822 Also, run the hook window-scroll-functions
15823 because we have scrolled. */
15824 /* Note, we do this after clearing force_start because
15825 if there's an error, it is better to forget about force_start
15826 than to get into an infinite loop calling the hook functions
15827 and having them get more errors. */
15828 if (!update_mode_line
15829 || ! NILP (Vwindow_scroll_functions))
15830 {
15831 update_mode_line = 1;
15832 w->update_mode_line = 1;
15833 startp = run_window_scroll_functions (window, startp);
15834 }
15835
15836 if (CHARPOS (startp) < BEGV)
15837 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15838 else if (CHARPOS (startp) > ZV)
15839 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15840
15841 /* Redisplay, then check if cursor has been set during the
15842 redisplay. Give up if new fonts were loaded. */
15843 /* We used to issue a CHECK_MARGINS argument to try_window here,
15844 but this causes scrolling to fail when point begins inside
15845 the scroll margin (bug#148) -- cyd */
15846 if (!try_window (window, startp, 0))
15847 {
15848 w->force_start = 1;
15849 clear_glyph_matrix (w->desired_matrix);
15850 goto need_larger_matrices;
15851 }
15852
15853 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15854 {
15855 /* If point does not appear, try to move point so it does
15856 appear. The desired matrix has been built above, so we
15857 can use it here. */
15858 new_vpos = window_box_height (w) / 2;
15859 }
15860
15861 if (!cursor_row_fully_visible_p (w, 0, 0))
15862 {
15863 /* Point does appear, but on a line partly visible at end of window.
15864 Move it back to a fully-visible line. */
15865 new_vpos = window_box_height (w);
15866 }
15867 else if (w->cursor.vpos >= 0)
15868 {
15869 /* Some people insist on not letting point enter the scroll
15870 margin, even though this part handles windows that didn't
15871 scroll at all. */
15872 int window_total_lines
15873 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15874 int margin = min (scroll_margin, window_total_lines / 4);
15875 int pixel_margin = margin * frame_line_height;
15876 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15877
15878 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15879 below, which finds the row to move point to, advances by
15880 the Y coordinate of the _next_ row, see the definition of
15881 MATRIX_ROW_BOTTOM_Y. */
15882 if (w->cursor.vpos < margin + header_line)
15883 {
15884 w->cursor.vpos = -1;
15885 clear_glyph_matrix (w->desired_matrix);
15886 goto try_to_scroll;
15887 }
15888 else
15889 {
15890 int window_height = window_box_height (w);
15891
15892 if (header_line)
15893 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15894 if (w->cursor.y >= window_height - pixel_margin)
15895 {
15896 w->cursor.vpos = -1;
15897 clear_glyph_matrix (w->desired_matrix);
15898 goto try_to_scroll;
15899 }
15900 }
15901 }
15902
15903 /* If we need to move point for either of the above reasons,
15904 now actually do it. */
15905 if (new_vpos >= 0)
15906 {
15907 struct glyph_row *row;
15908
15909 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15910 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15911 ++row;
15912
15913 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15914 MATRIX_ROW_START_BYTEPOS (row));
15915
15916 if (w != XWINDOW (selected_window))
15917 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15918 else if (current_buffer == old)
15919 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15920
15921 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15922
15923 /* If we are highlighting the region, then we just changed
15924 the region, so redisplay to show it. */
15925 /* FIXME: We need to (re)run pre-redisplay-function! */
15926 /* if (markpos_of_region () >= 0)
15927 {
15928 clear_glyph_matrix (w->desired_matrix);
15929 if (!try_window (window, startp, 0))
15930 goto need_larger_matrices;
15931 }
15932 */
15933 }
15934
15935 #ifdef GLYPH_DEBUG
15936 debug_method_add (w, "forced window start");
15937 #endif
15938 goto done;
15939 }
15940
15941 /* Handle case where text has not changed, only point, and it has
15942 not moved off the frame, and we are not retrying after hscroll.
15943 (current_matrix_up_to_date_p is nonzero when retrying.) */
15944 if (current_matrix_up_to_date_p
15945 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15946 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15947 {
15948 switch (rc)
15949 {
15950 case CURSOR_MOVEMENT_SUCCESS:
15951 used_current_matrix_p = 1;
15952 goto done;
15953
15954 case CURSOR_MOVEMENT_MUST_SCROLL:
15955 goto try_to_scroll;
15956
15957 default:
15958 emacs_abort ();
15959 }
15960 }
15961 /* If current starting point was originally the beginning of a line
15962 but no longer is, find a new starting point. */
15963 else if (w->start_at_line_beg
15964 && !(CHARPOS (startp) <= BEGV
15965 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15966 {
15967 #ifdef GLYPH_DEBUG
15968 debug_method_add (w, "recenter 1");
15969 #endif
15970 goto recenter;
15971 }
15972
15973 /* Try scrolling with try_window_id. Value is > 0 if update has
15974 been done, it is -1 if we know that the same window start will
15975 not work. It is 0 if unsuccessful for some other reason. */
15976 else if ((tem = try_window_id (w)) != 0)
15977 {
15978 #ifdef GLYPH_DEBUG
15979 debug_method_add (w, "try_window_id %d", tem);
15980 #endif
15981
15982 if (f->fonts_changed)
15983 goto need_larger_matrices;
15984 if (tem > 0)
15985 goto done;
15986
15987 /* Otherwise try_window_id has returned -1 which means that we
15988 don't want the alternative below this comment to execute. */
15989 }
15990 else if (CHARPOS (startp) >= BEGV
15991 && CHARPOS (startp) <= ZV
15992 && PT >= CHARPOS (startp)
15993 && (CHARPOS (startp) < ZV
15994 /* Avoid starting at end of buffer. */
15995 || CHARPOS (startp) == BEGV
15996 || !window_outdated (w)))
15997 {
15998 int d1, d2, d3, d4, d5, d6;
15999
16000 /* If first window line is a continuation line, and window start
16001 is inside the modified region, but the first change is before
16002 current window start, we must select a new window start.
16003
16004 However, if this is the result of a down-mouse event (e.g. by
16005 extending the mouse-drag-overlay), we don't want to select a
16006 new window start, since that would change the position under
16007 the mouse, resulting in an unwanted mouse-movement rather
16008 than a simple mouse-click. */
16009 if (!w->start_at_line_beg
16010 && NILP (do_mouse_tracking)
16011 && CHARPOS (startp) > BEGV
16012 && CHARPOS (startp) > BEG + beg_unchanged
16013 && CHARPOS (startp) <= Z - end_unchanged
16014 /* Even if w->start_at_line_beg is nil, a new window may
16015 start at a line_beg, since that's how set_buffer_window
16016 sets it. So, we need to check the return value of
16017 compute_window_start_on_continuation_line. (See also
16018 bug#197). */
16019 && XMARKER (w->start)->buffer == current_buffer
16020 && compute_window_start_on_continuation_line (w)
16021 /* It doesn't make sense to force the window start like we
16022 do at label force_start if it is already known that point
16023 will not be visible in the resulting window, because
16024 doing so will move point from its correct position
16025 instead of scrolling the window to bring point into view.
16026 See bug#9324. */
16027 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16028 {
16029 w->force_start = 1;
16030 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16031 goto force_start;
16032 }
16033
16034 #ifdef GLYPH_DEBUG
16035 debug_method_add (w, "same window start");
16036 #endif
16037
16038 /* Try to redisplay starting at same place as before.
16039 If point has not moved off frame, accept the results. */
16040 if (!current_matrix_up_to_date_p
16041 /* Don't use try_window_reusing_current_matrix in this case
16042 because a window scroll function can have changed the
16043 buffer. */
16044 || !NILP (Vwindow_scroll_functions)
16045 || MINI_WINDOW_P (w)
16046 || !(used_current_matrix_p
16047 = try_window_reusing_current_matrix (w)))
16048 {
16049 IF_DEBUG (debug_method_add (w, "1"));
16050 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16051 /* -1 means we need to scroll.
16052 0 means we need new matrices, but fonts_changed
16053 is set in that case, so we will detect it below. */
16054 goto try_to_scroll;
16055 }
16056
16057 if (f->fonts_changed)
16058 goto need_larger_matrices;
16059
16060 if (w->cursor.vpos >= 0)
16061 {
16062 if (!just_this_one_p
16063 || current_buffer->clip_changed
16064 || BEG_UNCHANGED < CHARPOS (startp))
16065 /* Forget any recorded base line for line number display. */
16066 w->base_line_number = 0;
16067
16068 if (!cursor_row_fully_visible_p (w, 1, 0))
16069 {
16070 clear_glyph_matrix (w->desired_matrix);
16071 last_line_misfit = 1;
16072 }
16073 /* Drop through and scroll. */
16074 else
16075 goto done;
16076 }
16077 else
16078 clear_glyph_matrix (w->desired_matrix);
16079 }
16080
16081 try_to_scroll:
16082
16083 /* Redisplay the mode line. Select the buffer properly for that. */
16084 if (!update_mode_line)
16085 {
16086 update_mode_line = 1;
16087 w->update_mode_line = 1;
16088 }
16089
16090 /* Try to scroll by specified few lines. */
16091 if ((scroll_conservatively
16092 || emacs_scroll_step
16093 || temp_scroll_step
16094 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16095 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16096 && CHARPOS (startp) >= BEGV
16097 && CHARPOS (startp) <= ZV)
16098 {
16099 /* The function returns -1 if new fonts were loaded, 1 if
16100 successful, 0 if not successful. */
16101 int ss = try_scrolling (window, just_this_one_p,
16102 scroll_conservatively,
16103 emacs_scroll_step,
16104 temp_scroll_step, last_line_misfit);
16105 switch (ss)
16106 {
16107 case SCROLLING_SUCCESS:
16108 goto done;
16109
16110 case SCROLLING_NEED_LARGER_MATRICES:
16111 goto need_larger_matrices;
16112
16113 case SCROLLING_FAILED:
16114 break;
16115
16116 default:
16117 emacs_abort ();
16118 }
16119 }
16120
16121 /* Finally, just choose a place to start which positions point
16122 according to user preferences. */
16123
16124 recenter:
16125
16126 #ifdef GLYPH_DEBUG
16127 debug_method_add (w, "recenter");
16128 #endif
16129
16130 /* Forget any previously recorded base line for line number display. */
16131 if (!buffer_unchanged_p)
16132 w->base_line_number = 0;
16133
16134 /* Determine the window start relative to point. */
16135 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16136 it.current_y = it.last_visible_y;
16137 if (centering_position < 0)
16138 {
16139 int window_total_lines
16140 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16141 int margin =
16142 scroll_margin > 0
16143 ? min (scroll_margin, window_total_lines / 4)
16144 : 0;
16145 ptrdiff_t margin_pos = CHARPOS (startp);
16146 Lisp_Object aggressive;
16147 int scrolling_up;
16148
16149 /* If there is a scroll margin at the top of the window, find
16150 its character position. */
16151 if (margin
16152 /* Cannot call start_display if startp is not in the
16153 accessible region of the buffer. This can happen when we
16154 have just switched to a different buffer and/or changed
16155 its restriction. In that case, startp is initialized to
16156 the character position 1 (BEGV) because we did not yet
16157 have chance to display the buffer even once. */
16158 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16159 {
16160 struct it it1;
16161 void *it1data = NULL;
16162
16163 SAVE_IT (it1, it, it1data);
16164 start_display (&it1, w, startp);
16165 move_it_vertically (&it1, margin * frame_line_height);
16166 margin_pos = IT_CHARPOS (it1);
16167 RESTORE_IT (&it, &it, it1data);
16168 }
16169 scrolling_up = PT > margin_pos;
16170 aggressive =
16171 scrolling_up
16172 ? BVAR (current_buffer, scroll_up_aggressively)
16173 : BVAR (current_buffer, scroll_down_aggressively);
16174
16175 if (!MINI_WINDOW_P (w)
16176 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16177 {
16178 int pt_offset = 0;
16179
16180 /* Setting scroll-conservatively overrides
16181 scroll-*-aggressively. */
16182 if (!scroll_conservatively && NUMBERP (aggressive))
16183 {
16184 double float_amount = XFLOATINT (aggressive);
16185
16186 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16187 if (pt_offset == 0 && float_amount > 0)
16188 pt_offset = 1;
16189 if (pt_offset && margin > 0)
16190 margin -= 1;
16191 }
16192 /* Compute how much to move the window start backward from
16193 point so that point will be displayed where the user
16194 wants it. */
16195 if (scrolling_up)
16196 {
16197 centering_position = it.last_visible_y;
16198 if (pt_offset)
16199 centering_position -= pt_offset;
16200 centering_position -=
16201 frame_line_height * (1 + margin + (last_line_misfit != 0))
16202 + WINDOW_HEADER_LINE_HEIGHT (w);
16203 /* Don't let point enter the scroll margin near top of
16204 the window. */
16205 if (centering_position < margin * frame_line_height)
16206 centering_position = margin * frame_line_height;
16207 }
16208 else
16209 centering_position = margin * frame_line_height + pt_offset;
16210 }
16211 else
16212 /* Set the window start half the height of the window backward
16213 from point. */
16214 centering_position = window_box_height (w) / 2;
16215 }
16216 move_it_vertically_backward (&it, centering_position);
16217
16218 eassert (IT_CHARPOS (it) >= BEGV);
16219
16220 /* The function move_it_vertically_backward may move over more
16221 than the specified y-distance. If it->w is small, e.g. a
16222 mini-buffer window, we may end up in front of the window's
16223 display area. Start displaying at the start of the line
16224 containing PT in this case. */
16225 if (it.current_y <= 0)
16226 {
16227 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16228 move_it_vertically_backward (&it, 0);
16229 it.current_y = 0;
16230 }
16231
16232 it.current_x = it.hpos = 0;
16233
16234 /* Set the window start position here explicitly, to avoid an
16235 infinite loop in case the functions in window-scroll-functions
16236 get errors. */
16237 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16238
16239 /* Run scroll hooks. */
16240 startp = run_window_scroll_functions (window, it.current.pos);
16241
16242 /* Redisplay the window. */
16243 if (!current_matrix_up_to_date_p
16244 || windows_or_buffers_changed
16245 || f->cursor_type_changed
16246 /* Don't use try_window_reusing_current_matrix in this case
16247 because it can have changed the buffer. */
16248 || !NILP (Vwindow_scroll_functions)
16249 || !just_this_one_p
16250 || MINI_WINDOW_P (w)
16251 || !(used_current_matrix_p
16252 = try_window_reusing_current_matrix (w)))
16253 try_window (window, startp, 0);
16254
16255 /* If new fonts have been loaded (due to fontsets), give up. We
16256 have to start a new redisplay since we need to re-adjust glyph
16257 matrices. */
16258 if (f->fonts_changed)
16259 goto need_larger_matrices;
16260
16261 /* If cursor did not appear assume that the middle of the window is
16262 in the first line of the window. Do it again with the next line.
16263 (Imagine a window of height 100, displaying two lines of height
16264 60. Moving back 50 from it->last_visible_y will end in the first
16265 line.) */
16266 if (w->cursor.vpos < 0)
16267 {
16268 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16269 {
16270 clear_glyph_matrix (w->desired_matrix);
16271 move_it_by_lines (&it, 1);
16272 try_window (window, it.current.pos, 0);
16273 }
16274 else if (PT < IT_CHARPOS (it))
16275 {
16276 clear_glyph_matrix (w->desired_matrix);
16277 move_it_by_lines (&it, -1);
16278 try_window (window, it.current.pos, 0);
16279 }
16280 else
16281 {
16282 /* Not much we can do about it. */
16283 }
16284 }
16285
16286 /* Consider the following case: Window starts at BEGV, there is
16287 invisible, intangible text at BEGV, so that display starts at
16288 some point START > BEGV. It can happen that we are called with
16289 PT somewhere between BEGV and START. Try to handle that case. */
16290 if (w->cursor.vpos < 0)
16291 {
16292 struct glyph_row *row = w->current_matrix->rows;
16293 if (row->mode_line_p)
16294 ++row;
16295 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16296 }
16297
16298 if (!cursor_row_fully_visible_p (w, 0, 0))
16299 {
16300 /* If vscroll is enabled, disable it and try again. */
16301 if (w->vscroll)
16302 {
16303 w->vscroll = 0;
16304 clear_glyph_matrix (w->desired_matrix);
16305 goto recenter;
16306 }
16307
16308 /* Users who set scroll-conservatively to a large number want
16309 point just above/below the scroll margin. If we ended up
16310 with point's row partially visible, move the window start to
16311 make that row fully visible and out of the margin. */
16312 if (scroll_conservatively > SCROLL_LIMIT)
16313 {
16314 int window_total_lines
16315 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16316 int margin =
16317 scroll_margin > 0
16318 ? min (scroll_margin, window_total_lines / 4)
16319 : 0;
16320 int move_down = w->cursor.vpos >= window_total_lines / 2;
16321
16322 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16323 clear_glyph_matrix (w->desired_matrix);
16324 if (1 == try_window (window, it.current.pos,
16325 TRY_WINDOW_CHECK_MARGINS))
16326 goto done;
16327 }
16328
16329 /* If centering point failed to make the whole line visible,
16330 put point at the top instead. That has to make the whole line
16331 visible, if it can be done. */
16332 if (centering_position == 0)
16333 goto done;
16334
16335 clear_glyph_matrix (w->desired_matrix);
16336 centering_position = 0;
16337 goto recenter;
16338 }
16339
16340 done:
16341
16342 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16343 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16344 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16345
16346 /* Display the mode line, if we must. */
16347 if ((update_mode_line
16348 /* If window not full width, must redo its mode line
16349 if (a) the window to its side is being redone and
16350 (b) we do a frame-based redisplay. This is a consequence
16351 of how inverted lines are drawn in frame-based redisplay. */
16352 || (!just_this_one_p
16353 && !FRAME_WINDOW_P (f)
16354 && !WINDOW_FULL_WIDTH_P (w))
16355 /* Line number to display. */
16356 || w->base_line_pos > 0
16357 /* Column number is displayed and different from the one displayed. */
16358 || (w->column_number_displayed != -1
16359 && (w->column_number_displayed != current_column ())))
16360 /* This means that the window has a mode line. */
16361 && (WINDOW_WANTS_MODELINE_P (w)
16362 || WINDOW_WANTS_HEADER_LINE_P (w)))
16363 {
16364
16365 display_mode_lines (w);
16366
16367 /* If mode line height has changed, arrange for a thorough
16368 immediate redisplay using the correct mode line height. */
16369 if (WINDOW_WANTS_MODELINE_P (w)
16370 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16371 {
16372 f->fonts_changed = 1;
16373 w->mode_line_height = -1;
16374 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16375 = DESIRED_MODE_LINE_HEIGHT (w);
16376 }
16377
16378 /* If header line height has changed, arrange for a thorough
16379 immediate redisplay using the correct header line height. */
16380 if (WINDOW_WANTS_HEADER_LINE_P (w)
16381 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16382 {
16383 f->fonts_changed = 1;
16384 w->header_line_height = -1;
16385 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16386 = DESIRED_HEADER_LINE_HEIGHT (w);
16387 }
16388
16389 if (f->fonts_changed)
16390 goto need_larger_matrices;
16391 }
16392
16393 if (!line_number_displayed && w->base_line_pos != -1)
16394 {
16395 w->base_line_pos = 0;
16396 w->base_line_number = 0;
16397 }
16398
16399 finish_menu_bars:
16400
16401 /* When we reach a frame's selected window, redo the frame's menu bar. */
16402 if (update_mode_line
16403 && EQ (FRAME_SELECTED_WINDOW (f), window))
16404 {
16405 int redisplay_menu_p = 0;
16406
16407 if (FRAME_WINDOW_P (f))
16408 {
16409 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16410 || defined (HAVE_NS) || defined (USE_GTK)
16411 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16412 #else
16413 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16414 #endif
16415 }
16416 else
16417 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16418
16419 if (redisplay_menu_p)
16420 display_menu_bar (w);
16421
16422 #ifdef HAVE_WINDOW_SYSTEM
16423 if (FRAME_WINDOW_P (f))
16424 {
16425 #if defined (USE_GTK) || defined (HAVE_NS)
16426 if (FRAME_EXTERNAL_TOOL_BAR (f))
16427 redisplay_tool_bar (f);
16428 #else
16429 if (WINDOWP (f->tool_bar_window)
16430 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16431 || !NILP (Vauto_resize_tool_bars))
16432 && redisplay_tool_bar (f))
16433 ignore_mouse_drag_p = 1;
16434 #endif
16435 }
16436 #endif
16437 }
16438
16439 #ifdef HAVE_WINDOW_SYSTEM
16440 if (FRAME_WINDOW_P (f)
16441 && update_window_fringes (w, (just_this_one_p
16442 || (!used_current_matrix_p && !overlay_arrow_seen)
16443 || w->pseudo_window_p)))
16444 {
16445 update_begin (f);
16446 block_input ();
16447 if (draw_window_fringes (w, 1))
16448 {
16449 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16450 x_draw_right_divider (w);
16451 else
16452 x_draw_vertical_border (w);
16453 }
16454 unblock_input ();
16455 update_end (f);
16456 }
16457
16458 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16459 x_draw_bottom_divider (w);
16460 #endif /* HAVE_WINDOW_SYSTEM */
16461
16462 /* We go to this label, with fonts_changed set, if it is
16463 necessary to try again using larger glyph matrices.
16464 We have to redeem the scroll bar even in this case,
16465 because the loop in redisplay_internal expects that. */
16466 need_larger_matrices:
16467 ;
16468 finish_scroll_bars:
16469
16470 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16471 {
16472 /* Set the thumb's position and size. */
16473 set_vertical_scroll_bar (w);
16474
16475 /* Note that we actually used the scroll bar attached to this
16476 window, so it shouldn't be deleted at the end of redisplay. */
16477 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16478 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16479 }
16480
16481 /* Restore current_buffer and value of point in it. The window
16482 update may have changed the buffer, so first make sure `opoint'
16483 is still valid (Bug#6177). */
16484 if (CHARPOS (opoint) < BEGV)
16485 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16486 else if (CHARPOS (opoint) > ZV)
16487 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16488 else
16489 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16490
16491 set_buffer_internal_1 (old);
16492 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16493 shorter. This can be caused by log truncation in *Messages*. */
16494 if (CHARPOS (lpoint) <= ZV)
16495 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16496
16497 unbind_to (count, Qnil);
16498 }
16499
16500
16501 /* Build the complete desired matrix of WINDOW with a window start
16502 buffer position POS.
16503
16504 Value is 1 if successful. It is zero if fonts were loaded during
16505 redisplay which makes re-adjusting glyph matrices necessary, and -1
16506 if point would appear in the scroll margins.
16507 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16508 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16509 set in FLAGS.) */
16510
16511 int
16512 try_window (Lisp_Object window, struct text_pos pos, int flags)
16513 {
16514 struct window *w = XWINDOW (window);
16515 struct it it;
16516 struct glyph_row *last_text_row = NULL;
16517 struct frame *f = XFRAME (w->frame);
16518 int frame_line_height = default_line_pixel_height (w);
16519
16520 /* Make POS the new window start. */
16521 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16522
16523 /* Mark cursor position as unknown. No overlay arrow seen. */
16524 w->cursor.vpos = -1;
16525 overlay_arrow_seen = 0;
16526
16527 /* Initialize iterator and info to start at POS. */
16528 start_display (&it, w, pos);
16529
16530 /* Display all lines of W. */
16531 while (it.current_y < it.last_visible_y)
16532 {
16533 if (display_line (&it))
16534 last_text_row = it.glyph_row - 1;
16535 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16536 return 0;
16537 }
16538
16539 /* Don't let the cursor end in the scroll margins. */
16540 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16541 && !MINI_WINDOW_P (w))
16542 {
16543 int this_scroll_margin;
16544 int window_total_lines
16545 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16546
16547 if (scroll_margin > 0)
16548 {
16549 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16550 this_scroll_margin *= frame_line_height;
16551 }
16552 else
16553 this_scroll_margin = 0;
16554
16555 if ((w->cursor.y >= 0 /* not vscrolled */
16556 && w->cursor.y < this_scroll_margin
16557 && CHARPOS (pos) > BEGV
16558 && IT_CHARPOS (it) < ZV)
16559 /* rms: considering make_cursor_line_fully_visible_p here
16560 seems to give wrong results. We don't want to recenter
16561 when the last line is partly visible, we want to allow
16562 that case to be handled in the usual way. */
16563 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16564 {
16565 w->cursor.vpos = -1;
16566 clear_glyph_matrix (w->desired_matrix);
16567 return -1;
16568 }
16569 }
16570
16571 /* If bottom moved off end of frame, change mode line percentage. */
16572 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16573 w->update_mode_line = 1;
16574
16575 /* Set window_end_pos to the offset of the last character displayed
16576 on the window from the end of current_buffer. Set
16577 window_end_vpos to its row number. */
16578 if (last_text_row)
16579 {
16580 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16581 adjust_window_ends (w, last_text_row, 0);
16582 eassert
16583 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16584 w->window_end_vpos)));
16585 }
16586 else
16587 {
16588 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16589 w->window_end_pos = Z - ZV;
16590 w->window_end_vpos = 0;
16591 }
16592
16593 /* But that is not valid info until redisplay finishes. */
16594 w->window_end_valid = 0;
16595 return 1;
16596 }
16597
16598
16599 \f
16600 /************************************************************************
16601 Window redisplay reusing current matrix when buffer has not changed
16602 ************************************************************************/
16603
16604 /* Try redisplay of window W showing an unchanged buffer with a
16605 different window start than the last time it was displayed by
16606 reusing its current matrix. Value is non-zero if successful.
16607 W->start is the new window start. */
16608
16609 static int
16610 try_window_reusing_current_matrix (struct window *w)
16611 {
16612 struct frame *f = XFRAME (w->frame);
16613 struct glyph_row *bottom_row;
16614 struct it it;
16615 struct run run;
16616 struct text_pos start, new_start;
16617 int nrows_scrolled, i;
16618 struct glyph_row *last_text_row;
16619 struct glyph_row *last_reused_text_row;
16620 struct glyph_row *start_row;
16621 int start_vpos, min_y, max_y;
16622
16623 #ifdef GLYPH_DEBUG
16624 if (inhibit_try_window_reusing)
16625 return 0;
16626 #endif
16627
16628 if (/* This function doesn't handle terminal frames. */
16629 !FRAME_WINDOW_P (f)
16630 /* Don't try to reuse the display if windows have been split
16631 or such. */
16632 || windows_or_buffers_changed
16633 || f->cursor_type_changed)
16634 return 0;
16635
16636 /* Can't do this if showing trailing whitespace. */
16637 if (!NILP (Vshow_trailing_whitespace))
16638 return 0;
16639
16640 /* If top-line visibility has changed, give up. */
16641 if (WINDOW_WANTS_HEADER_LINE_P (w)
16642 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16643 return 0;
16644
16645 /* Give up if old or new display is scrolled vertically. We could
16646 make this function handle this, but right now it doesn't. */
16647 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16648 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16649 return 0;
16650
16651 /* The variable new_start now holds the new window start. The old
16652 start `start' can be determined from the current matrix. */
16653 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16654 start = start_row->minpos;
16655 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16656
16657 /* Clear the desired matrix for the display below. */
16658 clear_glyph_matrix (w->desired_matrix);
16659
16660 if (CHARPOS (new_start) <= CHARPOS (start))
16661 {
16662 /* Don't use this method if the display starts with an ellipsis
16663 displayed for invisible text. It's not easy to handle that case
16664 below, and it's certainly not worth the effort since this is
16665 not a frequent case. */
16666 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16667 return 0;
16668
16669 IF_DEBUG (debug_method_add (w, "twu1"));
16670
16671 /* Display up to a row that can be reused. The variable
16672 last_text_row is set to the last row displayed that displays
16673 text. Note that it.vpos == 0 if or if not there is a
16674 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16675 start_display (&it, w, new_start);
16676 w->cursor.vpos = -1;
16677 last_text_row = last_reused_text_row = NULL;
16678
16679 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16680 {
16681 /* If we have reached into the characters in the START row,
16682 that means the line boundaries have changed. So we
16683 can't start copying with the row START. Maybe it will
16684 work to start copying with the following row. */
16685 while (IT_CHARPOS (it) > CHARPOS (start))
16686 {
16687 /* Advance to the next row as the "start". */
16688 start_row++;
16689 start = start_row->minpos;
16690 /* If there are no more rows to try, or just one, give up. */
16691 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16692 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16693 || CHARPOS (start) == ZV)
16694 {
16695 clear_glyph_matrix (w->desired_matrix);
16696 return 0;
16697 }
16698
16699 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16700 }
16701 /* If we have reached alignment, we can copy the rest of the
16702 rows. */
16703 if (IT_CHARPOS (it) == CHARPOS (start)
16704 /* Don't accept "alignment" inside a display vector,
16705 since start_row could have started in the middle of
16706 that same display vector (thus their character
16707 positions match), and we have no way of telling if
16708 that is the case. */
16709 && it.current.dpvec_index < 0)
16710 break;
16711
16712 if (display_line (&it))
16713 last_text_row = it.glyph_row - 1;
16714
16715 }
16716
16717 /* A value of current_y < last_visible_y means that we stopped
16718 at the previous window start, which in turn means that we
16719 have at least one reusable row. */
16720 if (it.current_y < it.last_visible_y)
16721 {
16722 struct glyph_row *row;
16723
16724 /* IT.vpos always starts from 0; it counts text lines. */
16725 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16726
16727 /* Find PT if not already found in the lines displayed. */
16728 if (w->cursor.vpos < 0)
16729 {
16730 int dy = it.current_y - start_row->y;
16731
16732 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16733 row = row_containing_pos (w, PT, row, NULL, dy);
16734 if (row)
16735 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16736 dy, nrows_scrolled);
16737 else
16738 {
16739 clear_glyph_matrix (w->desired_matrix);
16740 return 0;
16741 }
16742 }
16743
16744 /* Scroll the display. Do it before the current matrix is
16745 changed. The problem here is that update has not yet
16746 run, i.e. part of the current matrix is not up to date.
16747 scroll_run_hook will clear the cursor, and use the
16748 current matrix to get the height of the row the cursor is
16749 in. */
16750 run.current_y = start_row->y;
16751 run.desired_y = it.current_y;
16752 run.height = it.last_visible_y - it.current_y;
16753
16754 if (run.height > 0 && run.current_y != run.desired_y)
16755 {
16756 update_begin (f);
16757 FRAME_RIF (f)->update_window_begin_hook (w);
16758 FRAME_RIF (f)->clear_window_mouse_face (w);
16759 FRAME_RIF (f)->scroll_run_hook (w, &run);
16760 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16761 update_end (f);
16762 }
16763
16764 /* Shift current matrix down by nrows_scrolled lines. */
16765 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16766 rotate_matrix (w->current_matrix,
16767 start_vpos,
16768 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16769 nrows_scrolled);
16770
16771 /* Disable lines that must be updated. */
16772 for (i = 0; i < nrows_scrolled; ++i)
16773 (start_row + i)->enabled_p = false;
16774
16775 /* Re-compute Y positions. */
16776 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16777 max_y = it.last_visible_y;
16778 for (row = start_row + nrows_scrolled;
16779 row < bottom_row;
16780 ++row)
16781 {
16782 row->y = it.current_y;
16783 row->visible_height = row->height;
16784
16785 if (row->y < min_y)
16786 row->visible_height -= min_y - row->y;
16787 if (row->y + row->height > max_y)
16788 row->visible_height -= row->y + row->height - max_y;
16789 if (row->fringe_bitmap_periodic_p)
16790 row->redraw_fringe_bitmaps_p = 1;
16791
16792 it.current_y += row->height;
16793
16794 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16795 last_reused_text_row = row;
16796 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16797 break;
16798 }
16799
16800 /* Disable lines in the current matrix which are now
16801 below the window. */
16802 for (++row; row < bottom_row; ++row)
16803 row->enabled_p = row->mode_line_p = 0;
16804 }
16805
16806 /* Update window_end_pos etc.; last_reused_text_row is the last
16807 reused row from the current matrix containing text, if any.
16808 The value of last_text_row is the last displayed line
16809 containing text. */
16810 if (last_reused_text_row)
16811 adjust_window_ends (w, last_reused_text_row, 1);
16812 else if (last_text_row)
16813 adjust_window_ends (w, last_text_row, 0);
16814 else
16815 {
16816 /* This window must be completely empty. */
16817 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16818 w->window_end_pos = Z - ZV;
16819 w->window_end_vpos = 0;
16820 }
16821 w->window_end_valid = 0;
16822
16823 /* Update hint: don't try scrolling again in update_window. */
16824 w->desired_matrix->no_scrolling_p = 1;
16825
16826 #ifdef GLYPH_DEBUG
16827 debug_method_add (w, "try_window_reusing_current_matrix 1");
16828 #endif
16829 return 1;
16830 }
16831 else if (CHARPOS (new_start) > CHARPOS (start))
16832 {
16833 struct glyph_row *pt_row, *row;
16834 struct glyph_row *first_reusable_row;
16835 struct glyph_row *first_row_to_display;
16836 int dy;
16837 int yb = window_text_bottom_y (w);
16838
16839 /* Find the row starting at new_start, if there is one. Don't
16840 reuse a partially visible line at the end. */
16841 first_reusable_row = start_row;
16842 while (first_reusable_row->enabled_p
16843 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16844 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16845 < CHARPOS (new_start)))
16846 ++first_reusable_row;
16847
16848 /* Give up if there is no row to reuse. */
16849 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16850 || !first_reusable_row->enabled_p
16851 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16852 != CHARPOS (new_start)))
16853 return 0;
16854
16855 /* We can reuse fully visible rows beginning with
16856 first_reusable_row to the end of the window. Set
16857 first_row_to_display to the first row that cannot be reused.
16858 Set pt_row to the row containing point, if there is any. */
16859 pt_row = NULL;
16860 for (first_row_to_display = first_reusable_row;
16861 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16862 ++first_row_to_display)
16863 {
16864 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16865 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16866 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16867 && first_row_to_display->ends_at_zv_p
16868 && pt_row == NULL)))
16869 pt_row = first_row_to_display;
16870 }
16871
16872 /* Start displaying at the start of first_row_to_display. */
16873 eassert (first_row_to_display->y < yb);
16874 init_to_row_start (&it, w, first_row_to_display);
16875
16876 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16877 - start_vpos);
16878 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16879 - nrows_scrolled);
16880 it.current_y = (first_row_to_display->y - first_reusable_row->y
16881 + WINDOW_HEADER_LINE_HEIGHT (w));
16882
16883 /* Display lines beginning with first_row_to_display in the
16884 desired matrix. Set last_text_row to the last row displayed
16885 that displays text. */
16886 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16887 if (pt_row == NULL)
16888 w->cursor.vpos = -1;
16889 last_text_row = NULL;
16890 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16891 if (display_line (&it))
16892 last_text_row = it.glyph_row - 1;
16893
16894 /* If point is in a reused row, adjust y and vpos of the cursor
16895 position. */
16896 if (pt_row)
16897 {
16898 w->cursor.vpos -= nrows_scrolled;
16899 w->cursor.y -= first_reusable_row->y - start_row->y;
16900 }
16901
16902 /* Give up if point isn't in a row displayed or reused. (This
16903 also handles the case where w->cursor.vpos < nrows_scrolled
16904 after the calls to display_line, which can happen with scroll
16905 margins. See bug#1295.) */
16906 if (w->cursor.vpos < 0)
16907 {
16908 clear_glyph_matrix (w->desired_matrix);
16909 return 0;
16910 }
16911
16912 /* Scroll the display. */
16913 run.current_y = first_reusable_row->y;
16914 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16915 run.height = it.last_visible_y - run.current_y;
16916 dy = run.current_y - run.desired_y;
16917
16918 if (run.height)
16919 {
16920 update_begin (f);
16921 FRAME_RIF (f)->update_window_begin_hook (w);
16922 FRAME_RIF (f)->clear_window_mouse_face (w);
16923 FRAME_RIF (f)->scroll_run_hook (w, &run);
16924 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16925 update_end (f);
16926 }
16927
16928 /* Adjust Y positions of reused rows. */
16929 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16930 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16931 max_y = it.last_visible_y;
16932 for (row = first_reusable_row; row < first_row_to_display; ++row)
16933 {
16934 row->y -= dy;
16935 row->visible_height = row->height;
16936 if (row->y < min_y)
16937 row->visible_height -= min_y - row->y;
16938 if (row->y + row->height > max_y)
16939 row->visible_height -= row->y + row->height - max_y;
16940 if (row->fringe_bitmap_periodic_p)
16941 row->redraw_fringe_bitmaps_p = 1;
16942 }
16943
16944 /* Scroll the current matrix. */
16945 eassert (nrows_scrolled > 0);
16946 rotate_matrix (w->current_matrix,
16947 start_vpos,
16948 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16949 -nrows_scrolled);
16950
16951 /* Disable rows not reused. */
16952 for (row -= nrows_scrolled; row < bottom_row; ++row)
16953 row->enabled_p = false;
16954
16955 /* Point may have moved to a different line, so we cannot assume that
16956 the previous cursor position is valid; locate the correct row. */
16957 if (pt_row)
16958 {
16959 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16960 row < bottom_row
16961 && PT >= MATRIX_ROW_END_CHARPOS (row)
16962 && !row->ends_at_zv_p;
16963 row++)
16964 {
16965 w->cursor.vpos++;
16966 w->cursor.y = row->y;
16967 }
16968 if (row < bottom_row)
16969 {
16970 /* Can't simply scan the row for point with
16971 bidi-reordered glyph rows. Let set_cursor_from_row
16972 figure out where to put the cursor, and if it fails,
16973 give up. */
16974 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16975 {
16976 if (!set_cursor_from_row (w, row, w->current_matrix,
16977 0, 0, 0, 0))
16978 {
16979 clear_glyph_matrix (w->desired_matrix);
16980 return 0;
16981 }
16982 }
16983 else
16984 {
16985 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16986 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16987
16988 for (; glyph < end
16989 && (!BUFFERP (glyph->object)
16990 || glyph->charpos < PT);
16991 glyph++)
16992 {
16993 w->cursor.hpos++;
16994 w->cursor.x += glyph->pixel_width;
16995 }
16996 }
16997 }
16998 }
16999
17000 /* Adjust window end. A null value of last_text_row means that
17001 the window end is in reused rows which in turn means that
17002 only its vpos can have changed. */
17003 if (last_text_row)
17004 adjust_window_ends (w, last_text_row, 0);
17005 else
17006 w->window_end_vpos -= nrows_scrolled;
17007
17008 w->window_end_valid = 0;
17009 w->desired_matrix->no_scrolling_p = 1;
17010
17011 #ifdef GLYPH_DEBUG
17012 debug_method_add (w, "try_window_reusing_current_matrix 2");
17013 #endif
17014 return 1;
17015 }
17016
17017 return 0;
17018 }
17019
17020
17021 \f
17022 /************************************************************************
17023 Window redisplay reusing current matrix when buffer has changed
17024 ************************************************************************/
17025
17026 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17027 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17028 ptrdiff_t *, ptrdiff_t *);
17029 static struct glyph_row *
17030 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17031 struct glyph_row *);
17032
17033
17034 /* Return the last row in MATRIX displaying text. If row START is
17035 non-null, start searching with that row. IT gives the dimensions
17036 of the display. Value is null if matrix is empty; otherwise it is
17037 a pointer to the row found. */
17038
17039 static struct glyph_row *
17040 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17041 struct glyph_row *start)
17042 {
17043 struct glyph_row *row, *row_found;
17044
17045 /* Set row_found to the last row in IT->w's current matrix
17046 displaying text. The loop looks funny but think of partially
17047 visible lines. */
17048 row_found = NULL;
17049 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17050 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17051 {
17052 eassert (row->enabled_p);
17053 row_found = row;
17054 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17055 break;
17056 ++row;
17057 }
17058
17059 return row_found;
17060 }
17061
17062
17063 /* Return the last row in the current matrix of W that is not affected
17064 by changes at the start of current_buffer that occurred since W's
17065 current matrix was built. Value is null if no such row exists.
17066
17067 BEG_UNCHANGED us the number of characters unchanged at the start of
17068 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17069 first changed character in current_buffer. Characters at positions <
17070 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17071 when the current matrix was built. */
17072
17073 static struct glyph_row *
17074 find_last_unchanged_at_beg_row (struct window *w)
17075 {
17076 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17077 struct glyph_row *row;
17078 struct glyph_row *row_found = NULL;
17079 int yb = window_text_bottom_y (w);
17080
17081 /* Find the last row displaying unchanged text. */
17082 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17083 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17084 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17085 ++row)
17086 {
17087 if (/* If row ends before first_changed_pos, it is unchanged,
17088 except in some case. */
17089 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17090 /* When row ends in ZV and we write at ZV it is not
17091 unchanged. */
17092 && !row->ends_at_zv_p
17093 /* When first_changed_pos is the end of a continued line,
17094 row is not unchanged because it may be no longer
17095 continued. */
17096 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17097 && (row->continued_p
17098 || row->exact_window_width_line_p))
17099 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17100 needs to be recomputed, so don't consider this row as
17101 unchanged. This happens when the last line was
17102 bidi-reordered and was killed immediately before this
17103 redisplay cycle. In that case, ROW->end stores the
17104 buffer position of the first visual-order character of
17105 the killed text, which is now beyond ZV. */
17106 && CHARPOS (row->end.pos) <= ZV)
17107 row_found = row;
17108
17109 /* Stop if last visible row. */
17110 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17111 break;
17112 }
17113
17114 return row_found;
17115 }
17116
17117
17118 /* Find the first glyph row in the current matrix of W that is not
17119 affected by changes at the end of current_buffer since the
17120 time W's current matrix was built.
17121
17122 Return in *DELTA the number of chars by which buffer positions in
17123 unchanged text at the end of current_buffer must be adjusted.
17124
17125 Return in *DELTA_BYTES the corresponding number of bytes.
17126
17127 Value is null if no such row exists, i.e. all rows are affected by
17128 changes. */
17129
17130 static struct glyph_row *
17131 find_first_unchanged_at_end_row (struct window *w,
17132 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17133 {
17134 struct glyph_row *row;
17135 struct glyph_row *row_found = NULL;
17136
17137 *delta = *delta_bytes = 0;
17138
17139 /* Display must not have been paused, otherwise the current matrix
17140 is not up to date. */
17141 eassert (w->window_end_valid);
17142
17143 /* A value of window_end_pos >= END_UNCHANGED means that the window
17144 end is in the range of changed text. If so, there is no
17145 unchanged row at the end of W's current matrix. */
17146 if (w->window_end_pos >= END_UNCHANGED)
17147 return NULL;
17148
17149 /* Set row to the last row in W's current matrix displaying text. */
17150 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17151
17152 /* If matrix is entirely empty, no unchanged row exists. */
17153 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17154 {
17155 /* The value of row is the last glyph row in the matrix having a
17156 meaningful buffer position in it. The end position of row
17157 corresponds to window_end_pos. This allows us to translate
17158 buffer positions in the current matrix to current buffer
17159 positions for characters not in changed text. */
17160 ptrdiff_t Z_old =
17161 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17162 ptrdiff_t Z_BYTE_old =
17163 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17164 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17165 struct glyph_row *first_text_row
17166 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17167
17168 *delta = Z - Z_old;
17169 *delta_bytes = Z_BYTE - Z_BYTE_old;
17170
17171 /* Set last_unchanged_pos to the buffer position of the last
17172 character in the buffer that has not been changed. Z is the
17173 index + 1 of the last character in current_buffer, i.e. by
17174 subtracting END_UNCHANGED we get the index of the last
17175 unchanged character, and we have to add BEG to get its buffer
17176 position. */
17177 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17178 last_unchanged_pos_old = last_unchanged_pos - *delta;
17179
17180 /* Search backward from ROW for a row displaying a line that
17181 starts at a minimum position >= last_unchanged_pos_old. */
17182 for (; row > first_text_row; --row)
17183 {
17184 /* This used to abort, but it can happen.
17185 It is ok to just stop the search instead here. KFS. */
17186 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17187 break;
17188
17189 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17190 row_found = row;
17191 }
17192 }
17193
17194 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17195
17196 return row_found;
17197 }
17198
17199
17200 /* Make sure that glyph rows in the current matrix of window W
17201 reference the same glyph memory as corresponding rows in the
17202 frame's frame matrix. This function is called after scrolling W's
17203 current matrix on a terminal frame in try_window_id and
17204 try_window_reusing_current_matrix. */
17205
17206 static void
17207 sync_frame_with_window_matrix_rows (struct window *w)
17208 {
17209 struct frame *f = XFRAME (w->frame);
17210 struct glyph_row *window_row, *window_row_end, *frame_row;
17211
17212 /* Preconditions: W must be a leaf window and full-width. Its frame
17213 must have a frame matrix. */
17214 eassert (BUFFERP (w->contents));
17215 eassert (WINDOW_FULL_WIDTH_P (w));
17216 eassert (!FRAME_WINDOW_P (f));
17217
17218 /* If W is a full-width window, glyph pointers in W's current matrix
17219 have, by definition, to be the same as glyph pointers in the
17220 corresponding frame matrix. Note that frame matrices have no
17221 marginal areas (see build_frame_matrix). */
17222 window_row = w->current_matrix->rows;
17223 window_row_end = window_row + w->current_matrix->nrows;
17224 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17225 while (window_row < window_row_end)
17226 {
17227 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17228 struct glyph *end = window_row->glyphs[LAST_AREA];
17229
17230 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17231 frame_row->glyphs[TEXT_AREA] = start;
17232 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17233 frame_row->glyphs[LAST_AREA] = end;
17234
17235 /* Disable frame rows whose corresponding window rows have
17236 been disabled in try_window_id. */
17237 if (!window_row->enabled_p)
17238 frame_row->enabled_p = false;
17239
17240 ++window_row, ++frame_row;
17241 }
17242 }
17243
17244
17245 /* Find the glyph row in window W containing CHARPOS. Consider all
17246 rows between START and END (not inclusive). END null means search
17247 all rows to the end of the display area of W. Value is the row
17248 containing CHARPOS or null. */
17249
17250 struct glyph_row *
17251 row_containing_pos (struct window *w, ptrdiff_t charpos,
17252 struct glyph_row *start, struct glyph_row *end, int dy)
17253 {
17254 struct glyph_row *row = start;
17255 struct glyph_row *best_row = NULL;
17256 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17257 int last_y;
17258
17259 /* If we happen to start on a header-line, skip that. */
17260 if (row->mode_line_p)
17261 ++row;
17262
17263 if ((end && row >= end) || !row->enabled_p)
17264 return NULL;
17265
17266 last_y = window_text_bottom_y (w) - dy;
17267
17268 while (1)
17269 {
17270 /* Give up if we have gone too far. */
17271 if (end && row >= end)
17272 return NULL;
17273 /* This formerly returned if they were equal.
17274 I think that both quantities are of a "last plus one" type;
17275 if so, when they are equal, the row is within the screen. -- rms. */
17276 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17277 return NULL;
17278
17279 /* If it is in this row, return this row. */
17280 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17281 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17282 /* The end position of a row equals the start
17283 position of the next row. If CHARPOS is there, we
17284 would rather consider it displayed in the next
17285 line, except when this line ends in ZV. */
17286 && !row_for_charpos_p (row, charpos)))
17287 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17288 {
17289 struct glyph *g;
17290
17291 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17292 || (!best_row && !row->continued_p))
17293 return row;
17294 /* In bidi-reordered rows, there could be several rows whose
17295 edges surround CHARPOS, all of these rows belonging to
17296 the same continued line. We need to find the row which
17297 fits CHARPOS the best. */
17298 for (g = row->glyphs[TEXT_AREA];
17299 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17300 g++)
17301 {
17302 if (!STRINGP (g->object))
17303 {
17304 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17305 {
17306 mindif = eabs (g->charpos - charpos);
17307 best_row = row;
17308 /* Exact match always wins. */
17309 if (mindif == 0)
17310 return best_row;
17311 }
17312 }
17313 }
17314 }
17315 else if (best_row && !row->continued_p)
17316 return best_row;
17317 ++row;
17318 }
17319 }
17320
17321
17322 /* Try to redisplay window W by reusing its existing display. W's
17323 current matrix must be up to date when this function is called,
17324 i.e. window_end_valid must be nonzero.
17325
17326 Value is
17327
17328 >= 1 if successful, i.e. display has been updated
17329 specifically:
17330 1 means the changes were in front of a newline that precedes
17331 the window start, and the whole current matrix was reused
17332 2 means the changes were after the last position displayed
17333 in the window, and the whole current matrix was reused
17334 3 means portions of the current matrix were reused, while
17335 some of the screen lines were redrawn
17336 -1 if redisplay with same window start is known not to succeed
17337 0 if otherwise unsuccessful
17338
17339 The following steps are performed:
17340
17341 1. Find the last row in the current matrix of W that is not
17342 affected by changes at the start of current_buffer. If no such row
17343 is found, give up.
17344
17345 2. Find the first row in W's current matrix that is not affected by
17346 changes at the end of current_buffer. Maybe there is no such row.
17347
17348 3. Display lines beginning with the row + 1 found in step 1 to the
17349 row found in step 2 or, if step 2 didn't find a row, to the end of
17350 the window.
17351
17352 4. If cursor is not known to appear on the window, give up.
17353
17354 5. If display stopped at the row found in step 2, scroll the
17355 display and current matrix as needed.
17356
17357 6. Maybe display some lines at the end of W, if we must. This can
17358 happen under various circumstances, like a partially visible line
17359 becoming fully visible, or because newly displayed lines are displayed
17360 in smaller font sizes.
17361
17362 7. Update W's window end information. */
17363
17364 static int
17365 try_window_id (struct window *w)
17366 {
17367 struct frame *f = XFRAME (w->frame);
17368 struct glyph_matrix *current_matrix = w->current_matrix;
17369 struct glyph_matrix *desired_matrix = w->desired_matrix;
17370 struct glyph_row *last_unchanged_at_beg_row;
17371 struct glyph_row *first_unchanged_at_end_row;
17372 struct glyph_row *row;
17373 struct glyph_row *bottom_row;
17374 int bottom_vpos;
17375 struct it it;
17376 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17377 int dvpos, dy;
17378 struct text_pos start_pos;
17379 struct run run;
17380 int first_unchanged_at_end_vpos = 0;
17381 struct glyph_row *last_text_row, *last_text_row_at_end;
17382 struct text_pos start;
17383 ptrdiff_t first_changed_charpos, last_changed_charpos;
17384
17385 #ifdef GLYPH_DEBUG
17386 if (inhibit_try_window_id)
17387 return 0;
17388 #endif
17389
17390 /* This is handy for debugging. */
17391 #if 0
17392 #define GIVE_UP(X) \
17393 do { \
17394 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17395 return 0; \
17396 } while (0)
17397 #else
17398 #define GIVE_UP(X) return 0
17399 #endif
17400
17401 SET_TEXT_POS_FROM_MARKER (start, w->start);
17402
17403 /* Don't use this for mini-windows because these can show
17404 messages and mini-buffers, and we don't handle that here. */
17405 if (MINI_WINDOW_P (w))
17406 GIVE_UP (1);
17407
17408 /* This flag is used to prevent redisplay optimizations. */
17409 if (windows_or_buffers_changed || f->cursor_type_changed)
17410 GIVE_UP (2);
17411
17412 /* This function's optimizations cannot be used if overlays have
17413 changed in the buffer displayed by the window, so give up if they
17414 have. */
17415 if (w->last_overlay_modified != OVERLAY_MODIFF)
17416 GIVE_UP (21);
17417
17418 /* Verify that narrowing has not changed.
17419 Also verify that we were not told to prevent redisplay optimizations.
17420 It would be nice to further
17421 reduce the number of cases where this prevents try_window_id. */
17422 if (current_buffer->clip_changed
17423 || current_buffer->prevent_redisplay_optimizations_p)
17424 GIVE_UP (3);
17425
17426 /* Window must either use window-based redisplay or be full width. */
17427 if (!FRAME_WINDOW_P (f)
17428 && (!FRAME_LINE_INS_DEL_OK (f)
17429 || !WINDOW_FULL_WIDTH_P (w)))
17430 GIVE_UP (4);
17431
17432 /* Give up if point is known NOT to appear in W. */
17433 if (PT < CHARPOS (start))
17434 GIVE_UP (5);
17435
17436 /* Another way to prevent redisplay optimizations. */
17437 if (w->last_modified == 0)
17438 GIVE_UP (6);
17439
17440 /* Verify that window is not hscrolled. */
17441 if (w->hscroll != 0)
17442 GIVE_UP (7);
17443
17444 /* Verify that display wasn't paused. */
17445 if (!w->window_end_valid)
17446 GIVE_UP (8);
17447
17448 /* Likewise if highlighting trailing whitespace. */
17449 if (!NILP (Vshow_trailing_whitespace))
17450 GIVE_UP (11);
17451
17452 /* Can't use this if overlay arrow position and/or string have
17453 changed. */
17454 if (overlay_arrows_changed_p ())
17455 GIVE_UP (12);
17456
17457 /* When word-wrap is on, adding a space to the first word of a
17458 wrapped line can change the wrap position, altering the line
17459 above it. It might be worthwhile to handle this more
17460 intelligently, but for now just redisplay from scratch. */
17461 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17462 GIVE_UP (21);
17463
17464 /* Under bidi reordering, adding or deleting a character in the
17465 beginning of a paragraph, before the first strong directional
17466 character, can change the base direction of the paragraph (unless
17467 the buffer specifies a fixed paragraph direction), which will
17468 require to redisplay the whole paragraph. It might be worthwhile
17469 to find the paragraph limits and widen the range of redisplayed
17470 lines to that, but for now just give up this optimization and
17471 redisplay from scratch. */
17472 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17473 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17474 GIVE_UP (22);
17475
17476 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17477 only if buffer has really changed. The reason is that the gap is
17478 initially at Z for freshly visited files. The code below would
17479 set end_unchanged to 0 in that case. */
17480 if (MODIFF > SAVE_MODIFF
17481 /* This seems to happen sometimes after saving a buffer. */
17482 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17483 {
17484 if (GPT - BEG < BEG_UNCHANGED)
17485 BEG_UNCHANGED = GPT - BEG;
17486 if (Z - GPT < END_UNCHANGED)
17487 END_UNCHANGED = Z - GPT;
17488 }
17489
17490 /* The position of the first and last character that has been changed. */
17491 first_changed_charpos = BEG + BEG_UNCHANGED;
17492 last_changed_charpos = Z - END_UNCHANGED;
17493
17494 /* If window starts after a line end, and the last change is in
17495 front of that newline, then changes don't affect the display.
17496 This case happens with stealth-fontification. Note that although
17497 the display is unchanged, glyph positions in the matrix have to
17498 be adjusted, of course. */
17499 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17500 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17501 && ((last_changed_charpos < CHARPOS (start)
17502 && CHARPOS (start) == BEGV)
17503 || (last_changed_charpos < CHARPOS (start) - 1
17504 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17505 {
17506 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17507 struct glyph_row *r0;
17508
17509 /* Compute how many chars/bytes have been added to or removed
17510 from the buffer. */
17511 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17512 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17513 Z_delta = Z - Z_old;
17514 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17515
17516 /* Give up if PT is not in the window. Note that it already has
17517 been checked at the start of try_window_id that PT is not in
17518 front of the window start. */
17519 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17520 GIVE_UP (13);
17521
17522 /* If window start is unchanged, we can reuse the whole matrix
17523 as is, after adjusting glyph positions. No need to compute
17524 the window end again, since its offset from Z hasn't changed. */
17525 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17526 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17527 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17528 /* PT must not be in a partially visible line. */
17529 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17530 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17531 {
17532 /* Adjust positions in the glyph matrix. */
17533 if (Z_delta || Z_delta_bytes)
17534 {
17535 struct glyph_row *r1
17536 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17537 increment_matrix_positions (w->current_matrix,
17538 MATRIX_ROW_VPOS (r0, current_matrix),
17539 MATRIX_ROW_VPOS (r1, current_matrix),
17540 Z_delta, Z_delta_bytes);
17541 }
17542
17543 /* Set the cursor. */
17544 row = row_containing_pos (w, PT, r0, NULL, 0);
17545 if (row)
17546 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17547 return 1;
17548 }
17549 }
17550
17551 /* Handle the case that changes are all below what is displayed in
17552 the window, and that PT is in the window. This shortcut cannot
17553 be taken if ZV is visible in the window, and text has been added
17554 there that is visible in the window. */
17555 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17556 /* ZV is not visible in the window, or there are no
17557 changes at ZV, actually. */
17558 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17559 || first_changed_charpos == last_changed_charpos))
17560 {
17561 struct glyph_row *r0;
17562
17563 /* Give up if PT is not in the window. Note that it already has
17564 been checked at the start of try_window_id that PT is not in
17565 front of the window start. */
17566 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17567 GIVE_UP (14);
17568
17569 /* If window start is unchanged, we can reuse the whole matrix
17570 as is, without changing glyph positions since no text has
17571 been added/removed in front of the window end. */
17572 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17573 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17574 /* PT must not be in a partially visible line. */
17575 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17576 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17577 {
17578 /* We have to compute the window end anew since text
17579 could have been added/removed after it. */
17580 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17581 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17582
17583 /* Set the cursor. */
17584 row = row_containing_pos (w, PT, r0, NULL, 0);
17585 if (row)
17586 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17587 return 2;
17588 }
17589 }
17590
17591 /* Give up if window start is in the changed area.
17592
17593 The condition used to read
17594
17595 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17596
17597 but why that was tested escapes me at the moment. */
17598 if (CHARPOS (start) >= first_changed_charpos
17599 && CHARPOS (start) <= last_changed_charpos)
17600 GIVE_UP (15);
17601
17602 /* Check that window start agrees with the start of the first glyph
17603 row in its current matrix. Check this after we know the window
17604 start is not in changed text, otherwise positions would not be
17605 comparable. */
17606 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17607 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17608 GIVE_UP (16);
17609
17610 /* Give up if the window ends in strings. Overlay strings
17611 at the end are difficult to handle, so don't try. */
17612 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17613 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17614 GIVE_UP (20);
17615
17616 /* Compute the position at which we have to start displaying new
17617 lines. Some of the lines at the top of the window might be
17618 reusable because they are not displaying changed text. Find the
17619 last row in W's current matrix not affected by changes at the
17620 start of current_buffer. Value is null if changes start in the
17621 first line of window. */
17622 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17623 if (last_unchanged_at_beg_row)
17624 {
17625 /* Avoid starting to display in the middle of a character, a TAB
17626 for instance. This is easier than to set up the iterator
17627 exactly, and it's not a frequent case, so the additional
17628 effort wouldn't really pay off. */
17629 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17630 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17631 && last_unchanged_at_beg_row > w->current_matrix->rows)
17632 --last_unchanged_at_beg_row;
17633
17634 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17635 GIVE_UP (17);
17636
17637 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17638 GIVE_UP (18);
17639 start_pos = it.current.pos;
17640
17641 /* Start displaying new lines in the desired matrix at the same
17642 vpos we would use in the current matrix, i.e. below
17643 last_unchanged_at_beg_row. */
17644 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17645 current_matrix);
17646 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17647 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17648
17649 eassert (it.hpos == 0 && it.current_x == 0);
17650 }
17651 else
17652 {
17653 /* There are no reusable lines at the start of the window.
17654 Start displaying in the first text line. */
17655 start_display (&it, w, start);
17656 it.vpos = it.first_vpos;
17657 start_pos = it.current.pos;
17658 }
17659
17660 /* Find the first row that is not affected by changes at the end of
17661 the buffer. Value will be null if there is no unchanged row, in
17662 which case we must redisplay to the end of the window. delta
17663 will be set to the value by which buffer positions beginning with
17664 first_unchanged_at_end_row have to be adjusted due to text
17665 changes. */
17666 first_unchanged_at_end_row
17667 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17668 IF_DEBUG (debug_delta = delta);
17669 IF_DEBUG (debug_delta_bytes = delta_bytes);
17670
17671 /* Set stop_pos to the buffer position up to which we will have to
17672 display new lines. If first_unchanged_at_end_row != NULL, this
17673 is the buffer position of the start of the line displayed in that
17674 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17675 that we don't stop at a buffer position. */
17676 stop_pos = 0;
17677 if (first_unchanged_at_end_row)
17678 {
17679 eassert (last_unchanged_at_beg_row == NULL
17680 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17681
17682 /* If this is a continuation line, move forward to the next one
17683 that isn't. Changes in lines above affect this line.
17684 Caution: this may move first_unchanged_at_end_row to a row
17685 not displaying text. */
17686 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17687 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17688 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17689 < it.last_visible_y))
17690 ++first_unchanged_at_end_row;
17691
17692 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17693 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17694 >= it.last_visible_y))
17695 first_unchanged_at_end_row = NULL;
17696 else
17697 {
17698 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17699 + delta);
17700 first_unchanged_at_end_vpos
17701 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17702 eassert (stop_pos >= Z - END_UNCHANGED);
17703 }
17704 }
17705 else if (last_unchanged_at_beg_row == NULL)
17706 GIVE_UP (19);
17707
17708
17709 #ifdef GLYPH_DEBUG
17710
17711 /* Either there is no unchanged row at the end, or the one we have
17712 now displays text. This is a necessary condition for the window
17713 end pos calculation at the end of this function. */
17714 eassert (first_unchanged_at_end_row == NULL
17715 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17716
17717 debug_last_unchanged_at_beg_vpos
17718 = (last_unchanged_at_beg_row
17719 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17720 : -1);
17721 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17722
17723 #endif /* GLYPH_DEBUG */
17724
17725
17726 /* Display new lines. Set last_text_row to the last new line
17727 displayed which has text on it, i.e. might end up as being the
17728 line where the window_end_vpos is. */
17729 w->cursor.vpos = -1;
17730 last_text_row = NULL;
17731 overlay_arrow_seen = 0;
17732 while (it.current_y < it.last_visible_y
17733 && !f->fonts_changed
17734 && (first_unchanged_at_end_row == NULL
17735 || IT_CHARPOS (it) < stop_pos))
17736 {
17737 if (display_line (&it))
17738 last_text_row = it.glyph_row - 1;
17739 }
17740
17741 if (f->fonts_changed)
17742 return -1;
17743
17744
17745 /* Compute differences in buffer positions, y-positions etc. for
17746 lines reused at the bottom of the window. Compute what we can
17747 scroll. */
17748 if (first_unchanged_at_end_row
17749 /* No lines reused because we displayed everything up to the
17750 bottom of the window. */
17751 && it.current_y < it.last_visible_y)
17752 {
17753 dvpos = (it.vpos
17754 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17755 current_matrix));
17756 dy = it.current_y - first_unchanged_at_end_row->y;
17757 run.current_y = first_unchanged_at_end_row->y;
17758 run.desired_y = run.current_y + dy;
17759 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17760 }
17761 else
17762 {
17763 delta = delta_bytes = dvpos = dy
17764 = run.current_y = run.desired_y = run.height = 0;
17765 first_unchanged_at_end_row = NULL;
17766 }
17767 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17768
17769
17770 /* Find the cursor if not already found. We have to decide whether
17771 PT will appear on this window (it sometimes doesn't, but this is
17772 not a very frequent case.) This decision has to be made before
17773 the current matrix is altered. A value of cursor.vpos < 0 means
17774 that PT is either in one of the lines beginning at
17775 first_unchanged_at_end_row or below the window. Don't care for
17776 lines that might be displayed later at the window end; as
17777 mentioned, this is not a frequent case. */
17778 if (w->cursor.vpos < 0)
17779 {
17780 /* Cursor in unchanged rows at the top? */
17781 if (PT < CHARPOS (start_pos)
17782 && last_unchanged_at_beg_row)
17783 {
17784 row = row_containing_pos (w, PT,
17785 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17786 last_unchanged_at_beg_row + 1, 0);
17787 if (row)
17788 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17789 }
17790
17791 /* Start from first_unchanged_at_end_row looking for PT. */
17792 else if (first_unchanged_at_end_row)
17793 {
17794 row = row_containing_pos (w, PT - delta,
17795 first_unchanged_at_end_row, NULL, 0);
17796 if (row)
17797 set_cursor_from_row (w, row, w->current_matrix, delta,
17798 delta_bytes, dy, dvpos);
17799 }
17800
17801 /* Give up if cursor was not found. */
17802 if (w->cursor.vpos < 0)
17803 {
17804 clear_glyph_matrix (w->desired_matrix);
17805 return -1;
17806 }
17807 }
17808
17809 /* Don't let the cursor end in the scroll margins. */
17810 {
17811 int this_scroll_margin, cursor_height;
17812 int frame_line_height = default_line_pixel_height (w);
17813 int window_total_lines
17814 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17815
17816 this_scroll_margin =
17817 max (0, min (scroll_margin, window_total_lines / 4));
17818 this_scroll_margin *= frame_line_height;
17819 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17820
17821 if ((w->cursor.y < this_scroll_margin
17822 && CHARPOS (start) > BEGV)
17823 /* Old redisplay didn't take scroll margin into account at the bottom,
17824 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17825 || (w->cursor.y + (make_cursor_line_fully_visible_p
17826 ? cursor_height + this_scroll_margin
17827 : 1)) > it.last_visible_y)
17828 {
17829 w->cursor.vpos = -1;
17830 clear_glyph_matrix (w->desired_matrix);
17831 return -1;
17832 }
17833 }
17834
17835 /* Scroll the display. Do it before changing the current matrix so
17836 that xterm.c doesn't get confused about where the cursor glyph is
17837 found. */
17838 if (dy && run.height)
17839 {
17840 update_begin (f);
17841
17842 if (FRAME_WINDOW_P (f))
17843 {
17844 FRAME_RIF (f)->update_window_begin_hook (w);
17845 FRAME_RIF (f)->clear_window_mouse_face (w);
17846 FRAME_RIF (f)->scroll_run_hook (w, &run);
17847 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17848 }
17849 else
17850 {
17851 /* Terminal frame. In this case, dvpos gives the number of
17852 lines to scroll by; dvpos < 0 means scroll up. */
17853 int from_vpos
17854 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17855 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17856 int end = (WINDOW_TOP_EDGE_LINE (w)
17857 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17858 + window_internal_height (w));
17859
17860 #if defined (HAVE_GPM) || defined (MSDOS)
17861 x_clear_window_mouse_face (w);
17862 #endif
17863 /* Perform the operation on the screen. */
17864 if (dvpos > 0)
17865 {
17866 /* Scroll last_unchanged_at_beg_row to the end of the
17867 window down dvpos lines. */
17868 set_terminal_window (f, end);
17869
17870 /* On dumb terminals delete dvpos lines at the end
17871 before inserting dvpos empty lines. */
17872 if (!FRAME_SCROLL_REGION_OK (f))
17873 ins_del_lines (f, end - dvpos, -dvpos);
17874
17875 /* Insert dvpos empty lines in front of
17876 last_unchanged_at_beg_row. */
17877 ins_del_lines (f, from, dvpos);
17878 }
17879 else if (dvpos < 0)
17880 {
17881 /* Scroll up last_unchanged_at_beg_vpos to the end of
17882 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17883 set_terminal_window (f, end);
17884
17885 /* Delete dvpos lines in front of
17886 last_unchanged_at_beg_vpos. ins_del_lines will set
17887 the cursor to the given vpos and emit |dvpos| delete
17888 line sequences. */
17889 ins_del_lines (f, from + dvpos, dvpos);
17890
17891 /* On a dumb terminal insert dvpos empty lines at the
17892 end. */
17893 if (!FRAME_SCROLL_REGION_OK (f))
17894 ins_del_lines (f, end + dvpos, -dvpos);
17895 }
17896
17897 set_terminal_window (f, 0);
17898 }
17899
17900 update_end (f);
17901 }
17902
17903 /* Shift reused rows of the current matrix to the right position.
17904 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17905 text. */
17906 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17907 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17908 if (dvpos < 0)
17909 {
17910 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17911 bottom_vpos, dvpos);
17912 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17913 bottom_vpos);
17914 }
17915 else if (dvpos > 0)
17916 {
17917 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17918 bottom_vpos, dvpos);
17919 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17920 first_unchanged_at_end_vpos + dvpos);
17921 }
17922
17923 /* For frame-based redisplay, make sure that current frame and window
17924 matrix are in sync with respect to glyph memory. */
17925 if (!FRAME_WINDOW_P (f))
17926 sync_frame_with_window_matrix_rows (w);
17927
17928 /* Adjust buffer positions in reused rows. */
17929 if (delta || delta_bytes)
17930 increment_matrix_positions (current_matrix,
17931 first_unchanged_at_end_vpos + dvpos,
17932 bottom_vpos, delta, delta_bytes);
17933
17934 /* Adjust Y positions. */
17935 if (dy)
17936 shift_glyph_matrix (w, current_matrix,
17937 first_unchanged_at_end_vpos + dvpos,
17938 bottom_vpos, dy);
17939
17940 if (first_unchanged_at_end_row)
17941 {
17942 first_unchanged_at_end_row += dvpos;
17943 if (first_unchanged_at_end_row->y >= it.last_visible_y
17944 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17945 first_unchanged_at_end_row = NULL;
17946 }
17947
17948 /* If scrolling up, there may be some lines to display at the end of
17949 the window. */
17950 last_text_row_at_end = NULL;
17951 if (dy < 0)
17952 {
17953 /* Scrolling up can leave for example a partially visible line
17954 at the end of the window to be redisplayed. */
17955 /* Set last_row to the glyph row in the current matrix where the
17956 window end line is found. It has been moved up or down in
17957 the matrix by dvpos. */
17958 int last_vpos = w->window_end_vpos + dvpos;
17959 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17960
17961 /* If last_row is the window end line, it should display text. */
17962 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17963
17964 /* If window end line was partially visible before, begin
17965 displaying at that line. Otherwise begin displaying with the
17966 line following it. */
17967 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17968 {
17969 init_to_row_start (&it, w, last_row);
17970 it.vpos = last_vpos;
17971 it.current_y = last_row->y;
17972 }
17973 else
17974 {
17975 init_to_row_end (&it, w, last_row);
17976 it.vpos = 1 + last_vpos;
17977 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17978 ++last_row;
17979 }
17980
17981 /* We may start in a continuation line. If so, we have to
17982 get the right continuation_lines_width and current_x. */
17983 it.continuation_lines_width = last_row->continuation_lines_width;
17984 it.hpos = it.current_x = 0;
17985
17986 /* Display the rest of the lines at the window end. */
17987 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17988 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17989 {
17990 /* Is it always sure that the display agrees with lines in
17991 the current matrix? I don't think so, so we mark rows
17992 displayed invalid in the current matrix by setting their
17993 enabled_p flag to zero. */
17994 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
17995 if (display_line (&it))
17996 last_text_row_at_end = it.glyph_row - 1;
17997 }
17998 }
17999
18000 /* Update window_end_pos and window_end_vpos. */
18001 if (first_unchanged_at_end_row && !last_text_row_at_end)
18002 {
18003 /* Window end line if one of the preserved rows from the current
18004 matrix. Set row to the last row displaying text in current
18005 matrix starting at first_unchanged_at_end_row, after
18006 scrolling. */
18007 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18008 row = find_last_row_displaying_text (w->current_matrix, &it,
18009 first_unchanged_at_end_row);
18010 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18011 adjust_window_ends (w, row, 1);
18012 eassert (w->window_end_bytepos >= 0);
18013 IF_DEBUG (debug_method_add (w, "A"));
18014 }
18015 else if (last_text_row_at_end)
18016 {
18017 adjust_window_ends (w, last_text_row_at_end, 0);
18018 eassert (w->window_end_bytepos >= 0);
18019 IF_DEBUG (debug_method_add (w, "B"));
18020 }
18021 else if (last_text_row)
18022 {
18023 /* We have displayed either to the end of the window or at the
18024 end of the window, i.e. the last row with text is to be found
18025 in the desired matrix. */
18026 adjust_window_ends (w, last_text_row, 0);
18027 eassert (w->window_end_bytepos >= 0);
18028 }
18029 else if (first_unchanged_at_end_row == NULL
18030 && last_text_row == NULL
18031 && last_text_row_at_end == NULL)
18032 {
18033 /* Displayed to end of window, but no line containing text was
18034 displayed. Lines were deleted at the end of the window. */
18035 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18036 int vpos = w->window_end_vpos;
18037 struct glyph_row *current_row = current_matrix->rows + vpos;
18038 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18039
18040 for (row = NULL;
18041 row == NULL && vpos >= first_vpos;
18042 --vpos, --current_row, --desired_row)
18043 {
18044 if (desired_row->enabled_p)
18045 {
18046 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18047 row = desired_row;
18048 }
18049 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18050 row = current_row;
18051 }
18052
18053 eassert (row != NULL);
18054 w->window_end_vpos = vpos + 1;
18055 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18056 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18057 eassert (w->window_end_bytepos >= 0);
18058 IF_DEBUG (debug_method_add (w, "C"));
18059 }
18060 else
18061 emacs_abort ();
18062
18063 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18064 debug_end_vpos = w->window_end_vpos));
18065
18066 /* Record that display has not been completed. */
18067 w->window_end_valid = 0;
18068 w->desired_matrix->no_scrolling_p = 1;
18069 return 3;
18070
18071 #undef GIVE_UP
18072 }
18073
18074
18075 \f
18076 /***********************************************************************
18077 More debugging support
18078 ***********************************************************************/
18079
18080 #ifdef GLYPH_DEBUG
18081
18082 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18083 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18084 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18085
18086
18087 /* Dump the contents of glyph matrix MATRIX on stderr.
18088
18089 GLYPHS 0 means don't show glyph contents.
18090 GLYPHS 1 means show glyphs in short form
18091 GLYPHS > 1 means show glyphs in long form. */
18092
18093 void
18094 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18095 {
18096 int i;
18097 for (i = 0; i < matrix->nrows; ++i)
18098 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18099 }
18100
18101
18102 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18103 the glyph row and area where the glyph comes from. */
18104
18105 void
18106 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18107 {
18108 if (glyph->type == CHAR_GLYPH
18109 || glyph->type == GLYPHLESS_GLYPH)
18110 {
18111 fprintf (stderr,
18112 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18113 glyph - row->glyphs[TEXT_AREA],
18114 (glyph->type == CHAR_GLYPH
18115 ? 'C'
18116 : 'G'),
18117 glyph->charpos,
18118 (BUFFERP (glyph->object)
18119 ? 'B'
18120 : (STRINGP (glyph->object)
18121 ? 'S'
18122 : (INTEGERP (glyph->object)
18123 ? '0'
18124 : '-'))),
18125 glyph->pixel_width,
18126 glyph->u.ch,
18127 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18128 ? glyph->u.ch
18129 : '.'),
18130 glyph->face_id,
18131 glyph->left_box_line_p,
18132 glyph->right_box_line_p);
18133 }
18134 else if (glyph->type == STRETCH_GLYPH)
18135 {
18136 fprintf (stderr,
18137 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18138 glyph - row->glyphs[TEXT_AREA],
18139 'S',
18140 glyph->charpos,
18141 (BUFFERP (glyph->object)
18142 ? 'B'
18143 : (STRINGP (glyph->object)
18144 ? 'S'
18145 : (INTEGERP (glyph->object)
18146 ? '0'
18147 : '-'))),
18148 glyph->pixel_width,
18149 0,
18150 ' ',
18151 glyph->face_id,
18152 glyph->left_box_line_p,
18153 glyph->right_box_line_p);
18154 }
18155 else if (glyph->type == IMAGE_GLYPH)
18156 {
18157 fprintf (stderr,
18158 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18159 glyph - row->glyphs[TEXT_AREA],
18160 'I',
18161 glyph->charpos,
18162 (BUFFERP (glyph->object)
18163 ? 'B'
18164 : (STRINGP (glyph->object)
18165 ? 'S'
18166 : (INTEGERP (glyph->object)
18167 ? '0'
18168 : '-'))),
18169 glyph->pixel_width,
18170 glyph->u.img_id,
18171 '.',
18172 glyph->face_id,
18173 glyph->left_box_line_p,
18174 glyph->right_box_line_p);
18175 }
18176 else if (glyph->type == COMPOSITE_GLYPH)
18177 {
18178 fprintf (stderr,
18179 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18180 glyph - row->glyphs[TEXT_AREA],
18181 '+',
18182 glyph->charpos,
18183 (BUFFERP (glyph->object)
18184 ? 'B'
18185 : (STRINGP (glyph->object)
18186 ? 'S'
18187 : (INTEGERP (glyph->object)
18188 ? '0'
18189 : '-'))),
18190 glyph->pixel_width,
18191 glyph->u.cmp.id);
18192 if (glyph->u.cmp.automatic)
18193 fprintf (stderr,
18194 "[%d-%d]",
18195 glyph->slice.cmp.from, glyph->slice.cmp.to);
18196 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18197 glyph->face_id,
18198 glyph->left_box_line_p,
18199 glyph->right_box_line_p);
18200 }
18201 }
18202
18203
18204 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18205 GLYPHS 0 means don't show glyph contents.
18206 GLYPHS 1 means show glyphs in short form
18207 GLYPHS > 1 means show glyphs in long form. */
18208
18209 void
18210 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18211 {
18212 if (glyphs != 1)
18213 {
18214 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18215 fprintf (stderr, "==============================================================================\n");
18216
18217 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18218 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18219 vpos,
18220 MATRIX_ROW_START_CHARPOS (row),
18221 MATRIX_ROW_END_CHARPOS (row),
18222 row->used[TEXT_AREA],
18223 row->contains_overlapping_glyphs_p,
18224 row->enabled_p,
18225 row->truncated_on_left_p,
18226 row->truncated_on_right_p,
18227 row->continued_p,
18228 MATRIX_ROW_CONTINUATION_LINE_P (row),
18229 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18230 row->ends_at_zv_p,
18231 row->fill_line_p,
18232 row->ends_in_middle_of_char_p,
18233 row->starts_in_middle_of_char_p,
18234 row->mouse_face_p,
18235 row->x,
18236 row->y,
18237 row->pixel_width,
18238 row->height,
18239 row->visible_height,
18240 row->ascent,
18241 row->phys_ascent);
18242 /* The next 3 lines should align to "Start" in the header. */
18243 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18244 row->end.overlay_string_index,
18245 row->continuation_lines_width);
18246 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18247 CHARPOS (row->start.string_pos),
18248 CHARPOS (row->end.string_pos));
18249 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18250 row->end.dpvec_index);
18251 }
18252
18253 if (glyphs > 1)
18254 {
18255 int area;
18256
18257 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18258 {
18259 struct glyph *glyph = row->glyphs[area];
18260 struct glyph *glyph_end = glyph + row->used[area];
18261
18262 /* Glyph for a line end in text. */
18263 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18264 ++glyph_end;
18265
18266 if (glyph < glyph_end)
18267 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18268
18269 for (; glyph < glyph_end; ++glyph)
18270 dump_glyph (row, glyph, area);
18271 }
18272 }
18273 else if (glyphs == 1)
18274 {
18275 int area;
18276
18277 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18278 {
18279 char *s = alloca (row->used[area] + 4);
18280 int i;
18281
18282 for (i = 0; i < row->used[area]; ++i)
18283 {
18284 struct glyph *glyph = row->glyphs[area] + i;
18285 if (i == row->used[area] - 1
18286 && area == TEXT_AREA
18287 && INTEGERP (glyph->object)
18288 && glyph->type == CHAR_GLYPH
18289 && glyph->u.ch == ' ')
18290 {
18291 strcpy (&s[i], "[\\n]");
18292 i += 4;
18293 }
18294 else if (glyph->type == CHAR_GLYPH
18295 && glyph->u.ch < 0x80
18296 && glyph->u.ch >= ' ')
18297 s[i] = glyph->u.ch;
18298 else
18299 s[i] = '.';
18300 }
18301
18302 s[i] = '\0';
18303 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18304 }
18305 }
18306 }
18307
18308
18309 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18310 Sdump_glyph_matrix, 0, 1, "p",
18311 doc: /* Dump the current matrix of the selected window to stderr.
18312 Shows contents of glyph row structures. With non-nil
18313 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18314 glyphs in short form, otherwise show glyphs in long form. */)
18315 (Lisp_Object glyphs)
18316 {
18317 struct window *w = XWINDOW (selected_window);
18318 struct buffer *buffer = XBUFFER (w->contents);
18319
18320 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18321 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18322 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18323 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18324 fprintf (stderr, "=============================================\n");
18325 dump_glyph_matrix (w->current_matrix,
18326 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18327 return Qnil;
18328 }
18329
18330
18331 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18332 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18333 (void)
18334 {
18335 struct frame *f = XFRAME (selected_frame);
18336 dump_glyph_matrix (f->current_matrix, 1);
18337 return Qnil;
18338 }
18339
18340
18341 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18342 doc: /* Dump glyph row ROW to stderr.
18343 GLYPH 0 means don't dump glyphs.
18344 GLYPH 1 means dump glyphs in short form.
18345 GLYPH > 1 or omitted means dump glyphs in long form. */)
18346 (Lisp_Object row, Lisp_Object glyphs)
18347 {
18348 struct glyph_matrix *matrix;
18349 EMACS_INT vpos;
18350
18351 CHECK_NUMBER (row);
18352 matrix = XWINDOW (selected_window)->current_matrix;
18353 vpos = XINT (row);
18354 if (vpos >= 0 && vpos < matrix->nrows)
18355 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18356 vpos,
18357 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18358 return Qnil;
18359 }
18360
18361
18362 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18363 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18364 GLYPH 0 means don't dump glyphs.
18365 GLYPH 1 means dump glyphs in short form.
18366 GLYPH > 1 or omitted means dump glyphs in long form.
18367
18368 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18369 do nothing. */)
18370 (Lisp_Object row, Lisp_Object glyphs)
18371 {
18372 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18373 struct frame *sf = SELECTED_FRAME ();
18374 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18375 EMACS_INT vpos;
18376
18377 CHECK_NUMBER (row);
18378 vpos = XINT (row);
18379 if (vpos >= 0 && vpos < m->nrows)
18380 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18381 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18382 #endif
18383 return Qnil;
18384 }
18385
18386
18387 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18388 doc: /* Toggle tracing of redisplay.
18389 With ARG, turn tracing on if and only if ARG is positive. */)
18390 (Lisp_Object arg)
18391 {
18392 if (NILP (arg))
18393 trace_redisplay_p = !trace_redisplay_p;
18394 else
18395 {
18396 arg = Fprefix_numeric_value (arg);
18397 trace_redisplay_p = XINT (arg) > 0;
18398 }
18399
18400 return Qnil;
18401 }
18402
18403
18404 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18405 doc: /* Like `format', but print result to stderr.
18406 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18407 (ptrdiff_t nargs, Lisp_Object *args)
18408 {
18409 Lisp_Object s = Fformat (nargs, args);
18410 fprintf (stderr, "%s", SDATA (s));
18411 return Qnil;
18412 }
18413
18414 #endif /* GLYPH_DEBUG */
18415
18416
18417 \f
18418 /***********************************************************************
18419 Building Desired Matrix Rows
18420 ***********************************************************************/
18421
18422 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18423 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18424
18425 static struct glyph_row *
18426 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18427 {
18428 struct frame *f = XFRAME (WINDOW_FRAME (w));
18429 struct buffer *buffer = XBUFFER (w->contents);
18430 struct buffer *old = current_buffer;
18431 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18432 int arrow_len = SCHARS (overlay_arrow_string);
18433 const unsigned char *arrow_end = arrow_string + arrow_len;
18434 const unsigned char *p;
18435 struct it it;
18436 bool multibyte_p;
18437 int n_glyphs_before;
18438
18439 set_buffer_temp (buffer);
18440 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18441 it.glyph_row->used[TEXT_AREA] = 0;
18442 SET_TEXT_POS (it.position, 0, 0);
18443
18444 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18445 p = arrow_string;
18446 while (p < arrow_end)
18447 {
18448 Lisp_Object face, ilisp;
18449
18450 /* Get the next character. */
18451 if (multibyte_p)
18452 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18453 else
18454 {
18455 it.c = it.char_to_display = *p, it.len = 1;
18456 if (! ASCII_CHAR_P (it.c))
18457 it.char_to_display = BYTE8_TO_CHAR (it.c);
18458 }
18459 p += it.len;
18460
18461 /* Get its face. */
18462 ilisp = make_number (p - arrow_string);
18463 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18464 it.face_id = compute_char_face (f, it.char_to_display, face);
18465
18466 /* Compute its width, get its glyphs. */
18467 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18468 SET_TEXT_POS (it.position, -1, -1);
18469 PRODUCE_GLYPHS (&it);
18470
18471 /* If this character doesn't fit any more in the line, we have
18472 to remove some glyphs. */
18473 if (it.current_x > it.last_visible_x)
18474 {
18475 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18476 break;
18477 }
18478 }
18479
18480 set_buffer_temp (old);
18481 return it.glyph_row;
18482 }
18483
18484
18485 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18486 glyphs to insert is determined by produce_special_glyphs. */
18487
18488 static void
18489 insert_left_trunc_glyphs (struct it *it)
18490 {
18491 struct it truncate_it;
18492 struct glyph *from, *end, *to, *toend;
18493
18494 eassert (!FRAME_WINDOW_P (it->f)
18495 || (!it->glyph_row->reversed_p
18496 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18497 || (it->glyph_row->reversed_p
18498 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18499
18500 /* Get the truncation glyphs. */
18501 truncate_it = *it;
18502 truncate_it.current_x = 0;
18503 truncate_it.face_id = DEFAULT_FACE_ID;
18504 truncate_it.glyph_row = &scratch_glyph_row;
18505 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18506 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18507 truncate_it.object = make_number (0);
18508 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18509
18510 /* Overwrite glyphs from IT with truncation glyphs. */
18511 if (!it->glyph_row->reversed_p)
18512 {
18513 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18514
18515 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18516 end = from + tused;
18517 to = it->glyph_row->glyphs[TEXT_AREA];
18518 toend = to + it->glyph_row->used[TEXT_AREA];
18519 if (FRAME_WINDOW_P (it->f))
18520 {
18521 /* On GUI frames, when variable-size fonts are displayed,
18522 the truncation glyphs may need more pixels than the row's
18523 glyphs they overwrite. We overwrite more glyphs to free
18524 enough screen real estate, and enlarge the stretch glyph
18525 on the right (see display_line), if there is one, to
18526 preserve the screen position of the truncation glyphs on
18527 the right. */
18528 int w = 0;
18529 struct glyph *g = to;
18530 short used;
18531
18532 /* The first glyph could be partially visible, in which case
18533 it->glyph_row->x will be negative. But we want the left
18534 truncation glyphs to be aligned at the left margin of the
18535 window, so we override the x coordinate at which the row
18536 will begin. */
18537 it->glyph_row->x = 0;
18538 while (g < toend && w < it->truncation_pixel_width)
18539 {
18540 w += g->pixel_width;
18541 ++g;
18542 }
18543 if (g - to - tused > 0)
18544 {
18545 memmove (to + tused, g, (toend - g) * sizeof(*g));
18546 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18547 }
18548 used = it->glyph_row->used[TEXT_AREA];
18549 if (it->glyph_row->truncated_on_right_p
18550 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18551 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18552 == STRETCH_GLYPH)
18553 {
18554 int extra = w - it->truncation_pixel_width;
18555
18556 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18557 }
18558 }
18559
18560 while (from < end)
18561 *to++ = *from++;
18562
18563 /* There may be padding glyphs left over. Overwrite them too. */
18564 if (!FRAME_WINDOW_P (it->f))
18565 {
18566 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18567 {
18568 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18569 while (from < end)
18570 *to++ = *from++;
18571 }
18572 }
18573
18574 if (to > toend)
18575 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18576 }
18577 else
18578 {
18579 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18580
18581 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18582 that back to front. */
18583 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18584 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18585 toend = it->glyph_row->glyphs[TEXT_AREA];
18586 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18587 if (FRAME_WINDOW_P (it->f))
18588 {
18589 int w = 0;
18590 struct glyph *g = to;
18591
18592 while (g >= toend && w < it->truncation_pixel_width)
18593 {
18594 w += g->pixel_width;
18595 --g;
18596 }
18597 if (to - g - tused > 0)
18598 to = g + tused;
18599 if (it->glyph_row->truncated_on_right_p
18600 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18601 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18602 {
18603 int extra = w - it->truncation_pixel_width;
18604
18605 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18606 }
18607 }
18608
18609 while (from >= end && to >= toend)
18610 *to-- = *from--;
18611 if (!FRAME_WINDOW_P (it->f))
18612 {
18613 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18614 {
18615 from =
18616 truncate_it.glyph_row->glyphs[TEXT_AREA]
18617 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18618 while (from >= end && to >= toend)
18619 *to-- = *from--;
18620 }
18621 }
18622 if (from >= end)
18623 {
18624 /* Need to free some room before prepending additional
18625 glyphs. */
18626 int move_by = from - end + 1;
18627 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18628 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18629
18630 for ( ; g >= g0; g--)
18631 g[move_by] = *g;
18632 while (from >= end)
18633 *to-- = *from--;
18634 it->glyph_row->used[TEXT_AREA] += move_by;
18635 }
18636 }
18637 }
18638
18639 /* Compute the hash code for ROW. */
18640 unsigned
18641 row_hash (struct glyph_row *row)
18642 {
18643 int area, k;
18644 unsigned hashval = 0;
18645
18646 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18647 for (k = 0; k < row->used[area]; ++k)
18648 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18649 + row->glyphs[area][k].u.val
18650 + row->glyphs[area][k].face_id
18651 + row->glyphs[area][k].padding_p
18652 + (row->glyphs[area][k].type << 2));
18653
18654 return hashval;
18655 }
18656
18657 /* Compute the pixel height and width of IT->glyph_row.
18658
18659 Most of the time, ascent and height of a display line will be equal
18660 to the max_ascent and max_height values of the display iterator
18661 structure. This is not the case if
18662
18663 1. We hit ZV without displaying anything. In this case, max_ascent
18664 and max_height will be zero.
18665
18666 2. We have some glyphs that don't contribute to the line height.
18667 (The glyph row flag contributes_to_line_height_p is for future
18668 pixmap extensions).
18669
18670 The first case is easily covered by using default values because in
18671 these cases, the line height does not really matter, except that it
18672 must not be zero. */
18673
18674 static void
18675 compute_line_metrics (struct it *it)
18676 {
18677 struct glyph_row *row = it->glyph_row;
18678
18679 if (FRAME_WINDOW_P (it->f))
18680 {
18681 int i, min_y, max_y;
18682
18683 /* The line may consist of one space only, that was added to
18684 place the cursor on it. If so, the row's height hasn't been
18685 computed yet. */
18686 if (row->height == 0)
18687 {
18688 if (it->max_ascent + it->max_descent == 0)
18689 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18690 row->ascent = it->max_ascent;
18691 row->height = it->max_ascent + it->max_descent;
18692 row->phys_ascent = it->max_phys_ascent;
18693 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18694 row->extra_line_spacing = it->max_extra_line_spacing;
18695 }
18696
18697 /* Compute the width of this line. */
18698 row->pixel_width = row->x;
18699 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18700 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18701
18702 eassert (row->pixel_width >= 0);
18703 eassert (row->ascent >= 0 && row->height > 0);
18704
18705 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18706 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18707
18708 /* If first line's physical ascent is larger than its logical
18709 ascent, use the physical ascent, and make the row taller.
18710 This makes accented characters fully visible. */
18711 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18712 && row->phys_ascent > row->ascent)
18713 {
18714 row->height += row->phys_ascent - row->ascent;
18715 row->ascent = row->phys_ascent;
18716 }
18717
18718 /* Compute how much of the line is visible. */
18719 row->visible_height = row->height;
18720
18721 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18722 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18723
18724 if (row->y < min_y)
18725 row->visible_height -= min_y - row->y;
18726 if (row->y + row->height > max_y)
18727 row->visible_height -= row->y + row->height - max_y;
18728 }
18729 else
18730 {
18731 row->pixel_width = row->used[TEXT_AREA];
18732 if (row->continued_p)
18733 row->pixel_width -= it->continuation_pixel_width;
18734 else if (row->truncated_on_right_p)
18735 row->pixel_width -= it->truncation_pixel_width;
18736 row->ascent = row->phys_ascent = 0;
18737 row->height = row->phys_height = row->visible_height = 1;
18738 row->extra_line_spacing = 0;
18739 }
18740
18741 /* Compute a hash code for this row. */
18742 row->hash = row_hash (row);
18743
18744 it->max_ascent = it->max_descent = 0;
18745 it->max_phys_ascent = it->max_phys_descent = 0;
18746 }
18747
18748
18749 /* Append one space to the glyph row of iterator IT if doing a
18750 window-based redisplay. The space has the same face as
18751 IT->face_id. Value is non-zero if a space was added.
18752
18753 This function is called to make sure that there is always one glyph
18754 at the end of a glyph row that the cursor can be set on under
18755 window-systems. (If there weren't such a glyph we would not know
18756 how wide and tall a box cursor should be displayed).
18757
18758 At the same time this space let's a nicely handle clearing to the
18759 end of the line if the row ends in italic text. */
18760
18761 static int
18762 append_space_for_newline (struct it *it, int default_face_p)
18763 {
18764 if (FRAME_WINDOW_P (it->f))
18765 {
18766 int n = it->glyph_row->used[TEXT_AREA];
18767
18768 if (it->glyph_row->glyphs[TEXT_AREA] + n
18769 < it->glyph_row->glyphs[1 + TEXT_AREA])
18770 {
18771 /* Save some values that must not be changed.
18772 Must save IT->c and IT->len because otherwise
18773 ITERATOR_AT_END_P wouldn't work anymore after
18774 append_space_for_newline has been called. */
18775 enum display_element_type saved_what = it->what;
18776 int saved_c = it->c, saved_len = it->len;
18777 int saved_char_to_display = it->char_to_display;
18778 int saved_x = it->current_x;
18779 int saved_face_id = it->face_id;
18780 int saved_box_end = it->end_of_box_run_p;
18781 struct text_pos saved_pos;
18782 Lisp_Object saved_object;
18783 struct face *face;
18784
18785 saved_object = it->object;
18786 saved_pos = it->position;
18787
18788 it->what = IT_CHARACTER;
18789 memset (&it->position, 0, sizeof it->position);
18790 it->object = make_number (0);
18791 it->c = it->char_to_display = ' ';
18792 it->len = 1;
18793
18794 /* If the default face was remapped, be sure to use the
18795 remapped face for the appended newline. */
18796 if (default_face_p)
18797 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18798 else if (it->face_before_selective_p)
18799 it->face_id = it->saved_face_id;
18800 face = FACE_FROM_ID (it->f, it->face_id);
18801 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18802 /* In R2L rows, we will prepend a stretch glyph that will
18803 have the end_of_box_run_p flag set for it, so there's no
18804 need for the appended newline glyph to have that flag
18805 set. */
18806 if (it->glyph_row->reversed_p
18807 /* But if the appended newline glyph goes all the way to
18808 the end of the row, there will be no stretch glyph,
18809 so leave the box flag set. */
18810 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18811 it->end_of_box_run_p = 0;
18812
18813 PRODUCE_GLYPHS (it);
18814
18815 it->override_ascent = -1;
18816 it->constrain_row_ascent_descent_p = 0;
18817 it->current_x = saved_x;
18818 it->object = saved_object;
18819 it->position = saved_pos;
18820 it->what = saved_what;
18821 it->face_id = saved_face_id;
18822 it->len = saved_len;
18823 it->c = saved_c;
18824 it->char_to_display = saved_char_to_display;
18825 it->end_of_box_run_p = saved_box_end;
18826 return 1;
18827 }
18828 }
18829
18830 return 0;
18831 }
18832
18833
18834 /* Extend the face of the last glyph in the text area of IT->glyph_row
18835 to the end of the display line. Called from display_line. If the
18836 glyph row is empty, add a space glyph to it so that we know the
18837 face to draw. Set the glyph row flag fill_line_p. If the glyph
18838 row is R2L, prepend a stretch glyph to cover the empty space to the
18839 left of the leftmost glyph. */
18840
18841 static void
18842 extend_face_to_end_of_line (struct it *it)
18843 {
18844 struct face *face, *default_face;
18845 struct frame *f = it->f;
18846
18847 /* If line is already filled, do nothing. Non window-system frames
18848 get a grace of one more ``pixel'' because their characters are
18849 1-``pixel'' wide, so they hit the equality too early. This grace
18850 is needed only for R2L rows that are not continued, to produce
18851 one extra blank where we could display the cursor. */
18852 if ((it->current_x >= it->last_visible_x
18853 + (!FRAME_WINDOW_P (f)
18854 && it->glyph_row->reversed_p
18855 && !it->glyph_row->continued_p))
18856 /* If the window has display margins, we will need to extend
18857 their face even if the text area is filled. */
18858 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18859 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18860 return;
18861
18862 /* The default face, possibly remapped. */
18863 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18864
18865 /* Face extension extends the background and box of IT->face_id
18866 to the end of the line. If the background equals the background
18867 of the frame, we don't have to do anything. */
18868 if (it->face_before_selective_p)
18869 face = FACE_FROM_ID (f, it->saved_face_id);
18870 else
18871 face = FACE_FROM_ID (f, it->face_id);
18872
18873 if (FRAME_WINDOW_P (f)
18874 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18875 && face->box == FACE_NO_BOX
18876 && face->background == FRAME_BACKGROUND_PIXEL (f)
18877 #ifdef HAVE_WINDOW_SYSTEM
18878 && !face->stipple
18879 #endif
18880 && !it->glyph_row->reversed_p)
18881 return;
18882
18883 /* Set the glyph row flag indicating that the face of the last glyph
18884 in the text area has to be drawn to the end of the text area. */
18885 it->glyph_row->fill_line_p = 1;
18886
18887 /* If current character of IT is not ASCII, make sure we have the
18888 ASCII face. This will be automatically undone the next time
18889 get_next_display_element returns a multibyte character. Note
18890 that the character will always be single byte in unibyte
18891 text. */
18892 if (!ASCII_CHAR_P (it->c))
18893 {
18894 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18895 }
18896
18897 if (FRAME_WINDOW_P (f))
18898 {
18899 /* If the row is empty, add a space with the current face of IT,
18900 so that we know which face to draw. */
18901 if (it->glyph_row->used[TEXT_AREA] == 0)
18902 {
18903 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18904 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18905 it->glyph_row->used[TEXT_AREA] = 1;
18906 }
18907 /* Mode line and the header line don't have margins, and
18908 likewise the frame's tool-bar window, if there is any. */
18909 if (!(it->glyph_row->mode_line_p
18910 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18911 || (WINDOWP (f->tool_bar_window)
18912 && it->w == XWINDOW (f->tool_bar_window))
18913 #endif
18914 ))
18915 {
18916 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18917 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18918 {
18919 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18920 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18921 default_face->id;
18922 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18923 }
18924 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18925 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18926 {
18927 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18928 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18929 default_face->id;
18930 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18931 }
18932 }
18933 #ifdef HAVE_WINDOW_SYSTEM
18934 if (it->glyph_row->reversed_p)
18935 {
18936 /* Prepend a stretch glyph to the row, such that the
18937 rightmost glyph will be drawn flushed all the way to the
18938 right margin of the window. The stretch glyph that will
18939 occupy the empty space, if any, to the left of the
18940 glyphs. */
18941 struct font *font = face->font ? face->font : FRAME_FONT (f);
18942 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18943 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18944 struct glyph *g;
18945 int row_width, stretch_ascent, stretch_width;
18946 struct text_pos saved_pos;
18947 int saved_face_id, saved_avoid_cursor, saved_box_start;
18948
18949 for (row_width = 0, g = row_start; g < row_end; g++)
18950 row_width += g->pixel_width;
18951 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18952 if (stretch_width > 0)
18953 {
18954 stretch_ascent =
18955 (((it->ascent + it->descent)
18956 * FONT_BASE (font)) / FONT_HEIGHT (font));
18957 saved_pos = it->position;
18958 memset (&it->position, 0, sizeof it->position);
18959 saved_avoid_cursor = it->avoid_cursor_p;
18960 it->avoid_cursor_p = 1;
18961 saved_face_id = it->face_id;
18962 saved_box_start = it->start_of_box_run_p;
18963 /* The last row's stretch glyph should get the default
18964 face, to avoid painting the rest of the window with
18965 the region face, if the region ends at ZV. */
18966 if (it->glyph_row->ends_at_zv_p)
18967 it->face_id = default_face->id;
18968 else
18969 it->face_id = face->id;
18970 it->start_of_box_run_p = 0;
18971 append_stretch_glyph (it, make_number (0), stretch_width,
18972 it->ascent + it->descent, stretch_ascent);
18973 it->position = saved_pos;
18974 it->avoid_cursor_p = saved_avoid_cursor;
18975 it->face_id = saved_face_id;
18976 it->start_of_box_run_p = saved_box_start;
18977 }
18978 }
18979 #endif /* HAVE_WINDOW_SYSTEM */
18980 }
18981 else
18982 {
18983 /* Save some values that must not be changed. */
18984 int saved_x = it->current_x;
18985 struct text_pos saved_pos;
18986 Lisp_Object saved_object;
18987 enum display_element_type saved_what = it->what;
18988 int saved_face_id = it->face_id;
18989
18990 saved_object = it->object;
18991 saved_pos = it->position;
18992
18993 it->what = IT_CHARACTER;
18994 memset (&it->position, 0, sizeof it->position);
18995 it->object = make_number (0);
18996 it->c = it->char_to_display = ' ';
18997 it->len = 1;
18998
18999 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19000 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19001 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19002 && !it->glyph_row->mode_line_p
19003 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19004 {
19005 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19006 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19007
19008 for (it->current_x = 0; g < e; g++)
19009 it->current_x += g->pixel_width;
19010
19011 it->area = LEFT_MARGIN_AREA;
19012 it->face_id = default_face->id;
19013 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19014 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19015 {
19016 PRODUCE_GLYPHS (it);
19017 /* term.c:produce_glyphs advances it->current_x only for
19018 TEXT_AREA. */
19019 it->current_x += it->pixel_width;
19020 }
19021
19022 it->current_x = saved_x;
19023 it->area = TEXT_AREA;
19024 }
19025
19026 /* The last row's blank glyphs should get the default face, to
19027 avoid painting the rest of the window with the region face,
19028 if the region ends at ZV. */
19029 if (it->glyph_row->ends_at_zv_p)
19030 it->face_id = default_face->id;
19031 else
19032 it->face_id = face->id;
19033 PRODUCE_GLYPHS (it);
19034
19035 while (it->current_x <= it->last_visible_x)
19036 PRODUCE_GLYPHS (it);
19037
19038 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19039 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19040 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19041 && !it->glyph_row->mode_line_p
19042 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19043 {
19044 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19045 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19046
19047 for ( ; g < e; g++)
19048 it->current_x += g->pixel_width;
19049
19050 it->area = RIGHT_MARGIN_AREA;
19051 it->face_id = default_face->id;
19052 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19053 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19054 {
19055 PRODUCE_GLYPHS (it);
19056 it->current_x += it->pixel_width;
19057 }
19058
19059 it->area = TEXT_AREA;
19060 }
19061
19062 /* Don't count these blanks really. It would let us insert a left
19063 truncation glyph below and make us set the cursor on them, maybe. */
19064 it->current_x = saved_x;
19065 it->object = saved_object;
19066 it->position = saved_pos;
19067 it->what = saved_what;
19068 it->face_id = saved_face_id;
19069 }
19070 }
19071
19072
19073 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19074 trailing whitespace. */
19075
19076 static int
19077 trailing_whitespace_p (ptrdiff_t charpos)
19078 {
19079 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19080 int c = 0;
19081
19082 while (bytepos < ZV_BYTE
19083 && (c = FETCH_CHAR (bytepos),
19084 c == ' ' || c == '\t'))
19085 ++bytepos;
19086
19087 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19088 {
19089 if (bytepos != PT_BYTE)
19090 return 1;
19091 }
19092 return 0;
19093 }
19094
19095
19096 /* Highlight trailing whitespace, if any, in ROW. */
19097
19098 static void
19099 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19100 {
19101 int used = row->used[TEXT_AREA];
19102
19103 if (used)
19104 {
19105 struct glyph *start = row->glyphs[TEXT_AREA];
19106 struct glyph *glyph = start + used - 1;
19107
19108 if (row->reversed_p)
19109 {
19110 /* Right-to-left rows need to be processed in the opposite
19111 direction, so swap the edge pointers. */
19112 glyph = start;
19113 start = row->glyphs[TEXT_AREA] + used - 1;
19114 }
19115
19116 /* Skip over glyphs inserted to display the cursor at the
19117 end of a line, for extending the face of the last glyph
19118 to the end of the line on terminals, and for truncation
19119 and continuation glyphs. */
19120 if (!row->reversed_p)
19121 {
19122 while (glyph >= start
19123 && glyph->type == CHAR_GLYPH
19124 && INTEGERP (glyph->object))
19125 --glyph;
19126 }
19127 else
19128 {
19129 while (glyph <= start
19130 && glyph->type == CHAR_GLYPH
19131 && INTEGERP (glyph->object))
19132 ++glyph;
19133 }
19134
19135 /* If last glyph is a space or stretch, and it's trailing
19136 whitespace, set the face of all trailing whitespace glyphs in
19137 IT->glyph_row to `trailing-whitespace'. */
19138 if ((row->reversed_p ? glyph <= start : glyph >= start)
19139 && BUFFERP (glyph->object)
19140 && (glyph->type == STRETCH_GLYPH
19141 || (glyph->type == CHAR_GLYPH
19142 && glyph->u.ch == ' '))
19143 && trailing_whitespace_p (glyph->charpos))
19144 {
19145 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19146 if (face_id < 0)
19147 return;
19148
19149 if (!row->reversed_p)
19150 {
19151 while (glyph >= start
19152 && BUFFERP (glyph->object)
19153 && (glyph->type == STRETCH_GLYPH
19154 || (glyph->type == CHAR_GLYPH
19155 && glyph->u.ch == ' ')))
19156 (glyph--)->face_id = face_id;
19157 }
19158 else
19159 {
19160 while (glyph <= start
19161 && BUFFERP (glyph->object)
19162 && (glyph->type == STRETCH_GLYPH
19163 || (glyph->type == CHAR_GLYPH
19164 && glyph->u.ch == ' ')))
19165 (glyph++)->face_id = face_id;
19166 }
19167 }
19168 }
19169 }
19170
19171
19172 /* Value is non-zero if glyph row ROW should be
19173 considered to hold the buffer position CHARPOS. */
19174
19175 static int
19176 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19177 {
19178 int result = 1;
19179
19180 if (charpos == CHARPOS (row->end.pos)
19181 || charpos == MATRIX_ROW_END_CHARPOS (row))
19182 {
19183 /* Suppose the row ends on a string.
19184 Unless the row is continued, that means it ends on a newline
19185 in the string. If it's anything other than a display string
19186 (e.g., a before-string from an overlay), we don't want the
19187 cursor there. (This heuristic seems to give the optimal
19188 behavior for the various types of multi-line strings.)
19189 One exception: if the string has `cursor' property on one of
19190 its characters, we _do_ want the cursor there. */
19191 if (CHARPOS (row->end.string_pos) >= 0)
19192 {
19193 if (row->continued_p)
19194 result = 1;
19195 else
19196 {
19197 /* Check for `display' property. */
19198 struct glyph *beg = row->glyphs[TEXT_AREA];
19199 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19200 struct glyph *glyph;
19201
19202 result = 0;
19203 for (glyph = end; glyph >= beg; --glyph)
19204 if (STRINGP (glyph->object))
19205 {
19206 Lisp_Object prop
19207 = Fget_char_property (make_number (charpos),
19208 Qdisplay, Qnil);
19209 result =
19210 (!NILP (prop)
19211 && display_prop_string_p (prop, glyph->object));
19212 /* If there's a `cursor' property on one of the
19213 string's characters, this row is a cursor row,
19214 even though this is not a display string. */
19215 if (!result)
19216 {
19217 Lisp_Object s = glyph->object;
19218
19219 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19220 {
19221 ptrdiff_t gpos = glyph->charpos;
19222
19223 if (!NILP (Fget_char_property (make_number (gpos),
19224 Qcursor, s)))
19225 {
19226 result = 1;
19227 break;
19228 }
19229 }
19230 }
19231 break;
19232 }
19233 }
19234 }
19235 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19236 {
19237 /* If the row ends in middle of a real character,
19238 and the line is continued, we want the cursor here.
19239 That's because CHARPOS (ROW->end.pos) would equal
19240 PT if PT is before the character. */
19241 if (!row->ends_in_ellipsis_p)
19242 result = row->continued_p;
19243 else
19244 /* If the row ends in an ellipsis, then
19245 CHARPOS (ROW->end.pos) will equal point after the
19246 invisible text. We want that position to be displayed
19247 after the ellipsis. */
19248 result = 0;
19249 }
19250 /* If the row ends at ZV, display the cursor at the end of that
19251 row instead of at the start of the row below. */
19252 else if (row->ends_at_zv_p)
19253 result = 1;
19254 else
19255 result = 0;
19256 }
19257
19258 return result;
19259 }
19260
19261 /* Value is non-zero if glyph row ROW should be
19262 used to hold the cursor. */
19263
19264 static int
19265 cursor_row_p (struct glyph_row *row)
19266 {
19267 return row_for_charpos_p (row, PT);
19268 }
19269
19270 \f
19271
19272 /* Push the property PROP so that it will be rendered at the current
19273 position in IT. Return 1 if PROP was successfully pushed, 0
19274 otherwise. Called from handle_line_prefix to handle the
19275 `line-prefix' and `wrap-prefix' properties. */
19276
19277 static int
19278 push_prefix_prop (struct it *it, Lisp_Object prop)
19279 {
19280 struct text_pos pos =
19281 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19282
19283 eassert (it->method == GET_FROM_BUFFER
19284 || it->method == GET_FROM_DISPLAY_VECTOR
19285 || it->method == GET_FROM_STRING);
19286
19287 /* We need to save the current buffer/string position, so it will be
19288 restored by pop_it, because iterate_out_of_display_property
19289 depends on that being set correctly, but some situations leave
19290 it->position not yet set when this function is called. */
19291 push_it (it, &pos);
19292
19293 if (STRINGP (prop))
19294 {
19295 if (SCHARS (prop) == 0)
19296 {
19297 pop_it (it);
19298 return 0;
19299 }
19300
19301 it->string = prop;
19302 it->string_from_prefix_prop_p = 1;
19303 it->multibyte_p = STRING_MULTIBYTE (it->string);
19304 it->current.overlay_string_index = -1;
19305 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19306 it->end_charpos = it->string_nchars = SCHARS (it->string);
19307 it->method = GET_FROM_STRING;
19308 it->stop_charpos = 0;
19309 it->prev_stop = 0;
19310 it->base_level_stop = 0;
19311
19312 /* Force paragraph direction to be that of the parent
19313 buffer/string. */
19314 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19315 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19316 else
19317 it->paragraph_embedding = L2R;
19318
19319 /* Set up the bidi iterator for this display string. */
19320 if (it->bidi_p)
19321 {
19322 it->bidi_it.string.lstring = it->string;
19323 it->bidi_it.string.s = NULL;
19324 it->bidi_it.string.schars = it->end_charpos;
19325 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19326 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19327 it->bidi_it.string.unibyte = !it->multibyte_p;
19328 it->bidi_it.w = it->w;
19329 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19330 }
19331 }
19332 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19333 {
19334 it->method = GET_FROM_STRETCH;
19335 it->object = prop;
19336 }
19337 #ifdef HAVE_WINDOW_SYSTEM
19338 else if (IMAGEP (prop))
19339 {
19340 it->what = IT_IMAGE;
19341 it->image_id = lookup_image (it->f, prop);
19342 it->method = GET_FROM_IMAGE;
19343 }
19344 #endif /* HAVE_WINDOW_SYSTEM */
19345 else
19346 {
19347 pop_it (it); /* bogus display property, give up */
19348 return 0;
19349 }
19350
19351 return 1;
19352 }
19353
19354 /* Return the character-property PROP at the current position in IT. */
19355
19356 static Lisp_Object
19357 get_it_property (struct it *it, Lisp_Object prop)
19358 {
19359 Lisp_Object position, object = it->object;
19360
19361 if (STRINGP (object))
19362 position = make_number (IT_STRING_CHARPOS (*it));
19363 else if (BUFFERP (object))
19364 {
19365 position = make_number (IT_CHARPOS (*it));
19366 object = it->window;
19367 }
19368 else
19369 return Qnil;
19370
19371 return Fget_char_property (position, prop, object);
19372 }
19373
19374 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19375
19376 static void
19377 handle_line_prefix (struct it *it)
19378 {
19379 Lisp_Object prefix;
19380
19381 if (it->continuation_lines_width > 0)
19382 {
19383 prefix = get_it_property (it, Qwrap_prefix);
19384 if (NILP (prefix))
19385 prefix = Vwrap_prefix;
19386 }
19387 else
19388 {
19389 prefix = get_it_property (it, Qline_prefix);
19390 if (NILP (prefix))
19391 prefix = Vline_prefix;
19392 }
19393 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19394 {
19395 /* If the prefix is wider than the window, and we try to wrap
19396 it, it would acquire its own wrap prefix, and so on till the
19397 iterator stack overflows. So, don't wrap the prefix. */
19398 it->line_wrap = TRUNCATE;
19399 it->avoid_cursor_p = 1;
19400 }
19401 }
19402
19403 \f
19404
19405 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19406 only for R2L lines from display_line and display_string, when they
19407 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19408 the line/string needs to be continued on the next glyph row. */
19409 static void
19410 unproduce_glyphs (struct it *it, int n)
19411 {
19412 struct glyph *glyph, *end;
19413
19414 eassert (it->glyph_row);
19415 eassert (it->glyph_row->reversed_p);
19416 eassert (it->area == TEXT_AREA);
19417 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19418
19419 if (n > it->glyph_row->used[TEXT_AREA])
19420 n = it->glyph_row->used[TEXT_AREA];
19421 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19422 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19423 for ( ; glyph < end; glyph++)
19424 glyph[-n] = *glyph;
19425 }
19426
19427 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19428 and ROW->maxpos. */
19429 static void
19430 find_row_edges (struct it *it, struct glyph_row *row,
19431 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19432 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19433 {
19434 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19435 lines' rows is implemented for bidi-reordered rows. */
19436
19437 /* ROW->minpos is the value of min_pos, the minimal buffer position
19438 we have in ROW, or ROW->start.pos if that is smaller. */
19439 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19440 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19441 else
19442 /* We didn't find buffer positions smaller than ROW->start, or
19443 didn't find _any_ valid buffer positions in any of the glyphs,
19444 so we must trust the iterator's computed positions. */
19445 row->minpos = row->start.pos;
19446 if (max_pos <= 0)
19447 {
19448 max_pos = CHARPOS (it->current.pos);
19449 max_bpos = BYTEPOS (it->current.pos);
19450 }
19451
19452 /* Here are the various use-cases for ending the row, and the
19453 corresponding values for ROW->maxpos:
19454
19455 Line ends in a newline from buffer eol_pos + 1
19456 Line is continued from buffer max_pos + 1
19457 Line is truncated on right it->current.pos
19458 Line ends in a newline from string max_pos + 1(*)
19459 (*) + 1 only when line ends in a forward scan
19460 Line is continued from string max_pos
19461 Line is continued from display vector max_pos
19462 Line is entirely from a string min_pos == max_pos
19463 Line is entirely from a display vector min_pos == max_pos
19464 Line that ends at ZV ZV
19465
19466 If you discover other use-cases, please add them here as
19467 appropriate. */
19468 if (row->ends_at_zv_p)
19469 row->maxpos = it->current.pos;
19470 else if (row->used[TEXT_AREA])
19471 {
19472 int seen_this_string = 0;
19473 struct glyph_row *r1 = row - 1;
19474
19475 /* Did we see the same display string on the previous row? */
19476 if (STRINGP (it->object)
19477 /* this is not the first row */
19478 && row > it->w->desired_matrix->rows
19479 /* previous row is not the header line */
19480 && !r1->mode_line_p
19481 /* previous row also ends in a newline from a string */
19482 && r1->ends_in_newline_from_string_p)
19483 {
19484 struct glyph *start, *end;
19485
19486 /* Search for the last glyph of the previous row that came
19487 from buffer or string. Depending on whether the row is
19488 L2R or R2L, we need to process it front to back or the
19489 other way round. */
19490 if (!r1->reversed_p)
19491 {
19492 start = r1->glyphs[TEXT_AREA];
19493 end = start + r1->used[TEXT_AREA];
19494 /* Glyphs inserted by redisplay have an integer (zero)
19495 as their object. */
19496 while (end > start
19497 && INTEGERP ((end - 1)->object)
19498 && (end - 1)->charpos <= 0)
19499 --end;
19500 if (end > start)
19501 {
19502 if (EQ ((end - 1)->object, it->object))
19503 seen_this_string = 1;
19504 }
19505 else
19506 /* If all the glyphs of the previous row were inserted
19507 by redisplay, it means the previous row was
19508 produced from a single newline, which is only
19509 possible if that newline came from the same string
19510 as the one which produced this ROW. */
19511 seen_this_string = 1;
19512 }
19513 else
19514 {
19515 end = r1->glyphs[TEXT_AREA] - 1;
19516 start = end + r1->used[TEXT_AREA];
19517 while (end < start
19518 && INTEGERP ((end + 1)->object)
19519 && (end + 1)->charpos <= 0)
19520 ++end;
19521 if (end < start)
19522 {
19523 if (EQ ((end + 1)->object, it->object))
19524 seen_this_string = 1;
19525 }
19526 else
19527 seen_this_string = 1;
19528 }
19529 }
19530 /* Take note of each display string that covers a newline only
19531 once, the first time we see it. This is for when a display
19532 string includes more than one newline in it. */
19533 if (row->ends_in_newline_from_string_p && !seen_this_string)
19534 {
19535 /* If we were scanning the buffer forward when we displayed
19536 the string, we want to account for at least one buffer
19537 position that belongs to this row (position covered by
19538 the display string), so that cursor positioning will
19539 consider this row as a candidate when point is at the end
19540 of the visual line represented by this row. This is not
19541 required when scanning back, because max_pos will already
19542 have a much larger value. */
19543 if (CHARPOS (row->end.pos) > max_pos)
19544 INC_BOTH (max_pos, max_bpos);
19545 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19546 }
19547 else if (CHARPOS (it->eol_pos) > 0)
19548 SET_TEXT_POS (row->maxpos,
19549 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19550 else if (row->continued_p)
19551 {
19552 /* If max_pos is different from IT's current position, it
19553 means IT->method does not belong to the display element
19554 at max_pos. However, it also means that the display
19555 element at max_pos was displayed in its entirety on this
19556 line, which is equivalent to saying that the next line
19557 starts at the next buffer position. */
19558 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19559 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19560 else
19561 {
19562 INC_BOTH (max_pos, max_bpos);
19563 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19564 }
19565 }
19566 else if (row->truncated_on_right_p)
19567 /* display_line already called reseat_at_next_visible_line_start,
19568 which puts the iterator at the beginning of the next line, in
19569 the logical order. */
19570 row->maxpos = it->current.pos;
19571 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19572 /* A line that is entirely from a string/image/stretch... */
19573 row->maxpos = row->minpos;
19574 else
19575 emacs_abort ();
19576 }
19577 else
19578 row->maxpos = it->current.pos;
19579 }
19580
19581 /* Construct the glyph row IT->glyph_row in the desired matrix of
19582 IT->w from text at the current position of IT. See dispextern.h
19583 for an overview of struct it. Value is non-zero if
19584 IT->glyph_row displays text, as opposed to a line displaying ZV
19585 only. */
19586
19587 static int
19588 display_line (struct it *it)
19589 {
19590 struct glyph_row *row = it->glyph_row;
19591 Lisp_Object overlay_arrow_string;
19592 struct it wrap_it;
19593 void *wrap_data = NULL;
19594 int may_wrap = 0, wrap_x IF_LINT (= 0);
19595 int wrap_row_used = -1;
19596 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19597 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19598 int wrap_row_extra_line_spacing IF_LINT (= 0);
19599 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19600 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19601 int cvpos;
19602 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19603 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19604
19605 /* We always start displaying at hpos zero even if hscrolled. */
19606 eassert (it->hpos == 0 && it->current_x == 0);
19607
19608 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19609 >= it->w->desired_matrix->nrows)
19610 {
19611 it->w->nrows_scale_factor++;
19612 it->f->fonts_changed = 1;
19613 return 0;
19614 }
19615
19616 /* Clear the result glyph row and enable it. */
19617 prepare_desired_row (row);
19618
19619 row->y = it->current_y;
19620 row->start = it->start;
19621 row->continuation_lines_width = it->continuation_lines_width;
19622 row->displays_text_p = 1;
19623 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19624 it->starts_in_middle_of_char_p = 0;
19625
19626 /* Arrange the overlays nicely for our purposes. Usually, we call
19627 display_line on only one line at a time, in which case this
19628 can't really hurt too much, or we call it on lines which appear
19629 one after another in the buffer, in which case all calls to
19630 recenter_overlay_lists but the first will be pretty cheap. */
19631 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19632
19633 /* Move over display elements that are not visible because we are
19634 hscrolled. This may stop at an x-position < IT->first_visible_x
19635 if the first glyph is partially visible or if we hit a line end. */
19636 if (it->current_x < it->first_visible_x)
19637 {
19638 enum move_it_result move_result;
19639
19640 this_line_min_pos = row->start.pos;
19641 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19642 MOVE_TO_POS | MOVE_TO_X);
19643 /* If we are under a large hscroll, move_it_in_display_line_to
19644 could hit the end of the line without reaching
19645 it->first_visible_x. Pretend that we did reach it. This is
19646 especially important on a TTY, where we will call
19647 extend_face_to_end_of_line, which needs to know how many
19648 blank glyphs to produce. */
19649 if (it->current_x < it->first_visible_x
19650 && (move_result == MOVE_NEWLINE_OR_CR
19651 || move_result == MOVE_POS_MATCH_OR_ZV))
19652 it->current_x = it->first_visible_x;
19653
19654 /* Record the smallest positions seen while we moved over
19655 display elements that are not visible. This is needed by
19656 redisplay_internal for optimizing the case where the cursor
19657 stays inside the same line. The rest of this function only
19658 considers positions that are actually displayed, so
19659 RECORD_MAX_MIN_POS will not otherwise record positions that
19660 are hscrolled to the left of the left edge of the window. */
19661 min_pos = CHARPOS (this_line_min_pos);
19662 min_bpos = BYTEPOS (this_line_min_pos);
19663 }
19664 else
19665 {
19666 /* We only do this when not calling `move_it_in_display_line_to'
19667 above, because move_it_in_display_line_to calls
19668 handle_line_prefix itself. */
19669 handle_line_prefix (it);
19670 }
19671
19672 /* Get the initial row height. This is either the height of the
19673 text hscrolled, if there is any, or zero. */
19674 row->ascent = it->max_ascent;
19675 row->height = it->max_ascent + it->max_descent;
19676 row->phys_ascent = it->max_phys_ascent;
19677 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19678 row->extra_line_spacing = it->max_extra_line_spacing;
19679
19680 /* Utility macro to record max and min buffer positions seen until now. */
19681 #define RECORD_MAX_MIN_POS(IT) \
19682 do \
19683 { \
19684 int composition_p = !STRINGP ((IT)->string) \
19685 && ((IT)->what == IT_COMPOSITION); \
19686 ptrdiff_t current_pos = \
19687 composition_p ? (IT)->cmp_it.charpos \
19688 : IT_CHARPOS (*(IT)); \
19689 ptrdiff_t current_bpos = \
19690 composition_p ? CHAR_TO_BYTE (current_pos) \
19691 : IT_BYTEPOS (*(IT)); \
19692 if (current_pos < min_pos) \
19693 { \
19694 min_pos = current_pos; \
19695 min_bpos = current_bpos; \
19696 } \
19697 if (IT_CHARPOS (*it) > max_pos) \
19698 { \
19699 max_pos = IT_CHARPOS (*it); \
19700 max_bpos = IT_BYTEPOS (*it); \
19701 } \
19702 } \
19703 while (0)
19704
19705 /* Loop generating characters. The loop is left with IT on the next
19706 character to display. */
19707 while (1)
19708 {
19709 int n_glyphs_before, hpos_before, x_before;
19710 int x, nglyphs;
19711 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19712
19713 /* Retrieve the next thing to display. Value is zero if end of
19714 buffer reached. */
19715 if (!get_next_display_element (it))
19716 {
19717 /* Maybe add a space at the end of this line that is used to
19718 display the cursor there under X. Set the charpos of the
19719 first glyph of blank lines not corresponding to any text
19720 to -1. */
19721 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19722 row->exact_window_width_line_p = 1;
19723 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19724 || row->used[TEXT_AREA] == 0)
19725 {
19726 row->glyphs[TEXT_AREA]->charpos = -1;
19727 row->displays_text_p = 0;
19728
19729 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19730 && (!MINI_WINDOW_P (it->w)
19731 || (minibuf_level && EQ (it->window, minibuf_window))))
19732 row->indicate_empty_line_p = 1;
19733 }
19734
19735 it->continuation_lines_width = 0;
19736 row->ends_at_zv_p = 1;
19737 /* A row that displays right-to-left text must always have
19738 its last face extended all the way to the end of line,
19739 even if this row ends in ZV, because we still write to
19740 the screen left to right. We also need to extend the
19741 last face if the default face is remapped to some
19742 different face, otherwise the functions that clear
19743 portions of the screen will clear with the default face's
19744 background color. */
19745 if (row->reversed_p
19746 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19747 extend_face_to_end_of_line (it);
19748 break;
19749 }
19750
19751 /* Now, get the metrics of what we want to display. This also
19752 generates glyphs in `row' (which is IT->glyph_row). */
19753 n_glyphs_before = row->used[TEXT_AREA];
19754 x = it->current_x;
19755
19756 /* Remember the line height so far in case the next element doesn't
19757 fit on the line. */
19758 if (it->line_wrap != TRUNCATE)
19759 {
19760 ascent = it->max_ascent;
19761 descent = it->max_descent;
19762 phys_ascent = it->max_phys_ascent;
19763 phys_descent = it->max_phys_descent;
19764
19765 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19766 {
19767 if (IT_DISPLAYING_WHITESPACE (it))
19768 may_wrap = 1;
19769 else if (may_wrap)
19770 {
19771 SAVE_IT (wrap_it, *it, wrap_data);
19772 wrap_x = x;
19773 wrap_row_used = row->used[TEXT_AREA];
19774 wrap_row_ascent = row->ascent;
19775 wrap_row_height = row->height;
19776 wrap_row_phys_ascent = row->phys_ascent;
19777 wrap_row_phys_height = row->phys_height;
19778 wrap_row_extra_line_spacing = row->extra_line_spacing;
19779 wrap_row_min_pos = min_pos;
19780 wrap_row_min_bpos = min_bpos;
19781 wrap_row_max_pos = max_pos;
19782 wrap_row_max_bpos = max_bpos;
19783 may_wrap = 0;
19784 }
19785 }
19786 }
19787
19788 PRODUCE_GLYPHS (it);
19789
19790 /* If this display element was in marginal areas, continue with
19791 the next one. */
19792 if (it->area != TEXT_AREA)
19793 {
19794 row->ascent = max (row->ascent, it->max_ascent);
19795 row->height = max (row->height, it->max_ascent + it->max_descent);
19796 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19797 row->phys_height = max (row->phys_height,
19798 it->max_phys_ascent + it->max_phys_descent);
19799 row->extra_line_spacing = max (row->extra_line_spacing,
19800 it->max_extra_line_spacing);
19801 set_iterator_to_next (it, 1);
19802 continue;
19803 }
19804
19805 /* Does the display element fit on the line? If we truncate
19806 lines, we should draw past the right edge of the window. If
19807 we don't truncate, we want to stop so that we can display the
19808 continuation glyph before the right margin. If lines are
19809 continued, there are two possible strategies for characters
19810 resulting in more than 1 glyph (e.g. tabs): Display as many
19811 glyphs as possible in this line and leave the rest for the
19812 continuation line, or display the whole element in the next
19813 line. Original redisplay did the former, so we do it also. */
19814 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19815 hpos_before = it->hpos;
19816 x_before = x;
19817
19818 if (/* Not a newline. */
19819 nglyphs > 0
19820 /* Glyphs produced fit entirely in the line. */
19821 && it->current_x < it->last_visible_x)
19822 {
19823 it->hpos += nglyphs;
19824 row->ascent = max (row->ascent, it->max_ascent);
19825 row->height = max (row->height, it->max_ascent + it->max_descent);
19826 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19827 row->phys_height = max (row->phys_height,
19828 it->max_phys_ascent + it->max_phys_descent);
19829 row->extra_line_spacing = max (row->extra_line_spacing,
19830 it->max_extra_line_spacing);
19831 if (it->current_x - it->pixel_width < it->first_visible_x)
19832 row->x = x - it->first_visible_x;
19833 /* Record the maximum and minimum buffer positions seen so
19834 far in glyphs that will be displayed by this row. */
19835 if (it->bidi_p)
19836 RECORD_MAX_MIN_POS (it);
19837 }
19838 else
19839 {
19840 int i, new_x;
19841 struct glyph *glyph;
19842
19843 for (i = 0; i < nglyphs; ++i, x = new_x)
19844 {
19845 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19846 new_x = x + glyph->pixel_width;
19847
19848 if (/* Lines are continued. */
19849 it->line_wrap != TRUNCATE
19850 && (/* Glyph doesn't fit on the line. */
19851 new_x > it->last_visible_x
19852 /* Or it fits exactly on a window system frame. */
19853 || (new_x == it->last_visible_x
19854 && FRAME_WINDOW_P (it->f)
19855 && (row->reversed_p
19856 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19857 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19858 {
19859 /* End of a continued line. */
19860
19861 if (it->hpos == 0
19862 || (new_x == it->last_visible_x
19863 && FRAME_WINDOW_P (it->f)
19864 && (row->reversed_p
19865 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19866 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19867 {
19868 /* Current glyph is the only one on the line or
19869 fits exactly on the line. We must continue
19870 the line because we can't draw the cursor
19871 after the glyph. */
19872 row->continued_p = 1;
19873 it->current_x = new_x;
19874 it->continuation_lines_width += new_x;
19875 ++it->hpos;
19876 if (i == nglyphs - 1)
19877 {
19878 /* If line-wrap is on, check if a previous
19879 wrap point was found. */
19880 if (wrap_row_used > 0
19881 /* Even if there is a previous wrap
19882 point, continue the line here as
19883 usual, if (i) the previous character
19884 was a space or tab AND (ii) the
19885 current character is not. */
19886 && (!may_wrap
19887 || IT_DISPLAYING_WHITESPACE (it)))
19888 goto back_to_wrap;
19889
19890 /* Record the maximum and minimum buffer
19891 positions seen so far in glyphs that will be
19892 displayed by this row. */
19893 if (it->bidi_p)
19894 RECORD_MAX_MIN_POS (it);
19895 set_iterator_to_next (it, 1);
19896 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19897 {
19898 if (!get_next_display_element (it))
19899 {
19900 row->exact_window_width_line_p = 1;
19901 it->continuation_lines_width = 0;
19902 row->continued_p = 0;
19903 row->ends_at_zv_p = 1;
19904 }
19905 else if (ITERATOR_AT_END_OF_LINE_P (it))
19906 {
19907 row->continued_p = 0;
19908 row->exact_window_width_line_p = 1;
19909 }
19910 }
19911 }
19912 else if (it->bidi_p)
19913 RECORD_MAX_MIN_POS (it);
19914 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19915 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19916 extend_face_to_end_of_line (it);
19917 }
19918 else if (CHAR_GLYPH_PADDING_P (*glyph)
19919 && !FRAME_WINDOW_P (it->f))
19920 {
19921 /* A padding glyph that doesn't fit on this line.
19922 This means the whole character doesn't fit
19923 on the line. */
19924 if (row->reversed_p)
19925 unproduce_glyphs (it, row->used[TEXT_AREA]
19926 - n_glyphs_before);
19927 row->used[TEXT_AREA] = n_glyphs_before;
19928
19929 /* Fill the rest of the row with continuation
19930 glyphs like in 20.x. */
19931 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19932 < row->glyphs[1 + TEXT_AREA])
19933 produce_special_glyphs (it, IT_CONTINUATION);
19934
19935 row->continued_p = 1;
19936 it->current_x = x_before;
19937 it->continuation_lines_width += x_before;
19938
19939 /* Restore the height to what it was before the
19940 element not fitting on the line. */
19941 it->max_ascent = ascent;
19942 it->max_descent = descent;
19943 it->max_phys_ascent = phys_ascent;
19944 it->max_phys_descent = phys_descent;
19945 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19946 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19947 extend_face_to_end_of_line (it);
19948 }
19949 else if (wrap_row_used > 0)
19950 {
19951 back_to_wrap:
19952 if (row->reversed_p)
19953 unproduce_glyphs (it,
19954 row->used[TEXT_AREA] - wrap_row_used);
19955 RESTORE_IT (it, &wrap_it, wrap_data);
19956 it->continuation_lines_width += wrap_x;
19957 row->used[TEXT_AREA] = wrap_row_used;
19958 row->ascent = wrap_row_ascent;
19959 row->height = wrap_row_height;
19960 row->phys_ascent = wrap_row_phys_ascent;
19961 row->phys_height = wrap_row_phys_height;
19962 row->extra_line_spacing = wrap_row_extra_line_spacing;
19963 min_pos = wrap_row_min_pos;
19964 min_bpos = wrap_row_min_bpos;
19965 max_pos = wrap_row_max_pos;
19966 max_bpos = wrap_row_max_bpos;
19967 row->continued_p = 1;
19968 row->ends_at_zv_p = 0;
19969 row->exact_window_width_line_p = 0;
19970 it->continuation_lines_width += x;
19971
19972 /* Make sure that a non-default face is extended
19973 up to the right margin of the window. */
19974 extend_face_to_end_of_line (it);
19975 }
19976 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19977 {
19978 /* A TAB that extends past the right edge of the
19979 window. This produces a single glyph on
19980 window system frames. We leave the glyph in
19981 this row and let it fill the row, but don't
19982 consume the TAB. */
19983 if ((row->reversed_p
19984 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19985 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19986 produce_special_glyphs (it, IT_CONTINUATION);
19987 it->continuation_lines_width += it->last_visible_x;
19988 row->ends_in_middle_of_char_p = 1;
19989 row->continued_p = 1;
19990 glyph->pixel_width = it->last_visible_x - x;
19991 it->starts_in_middle_of_char_p = 1;
19992 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19993 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19994 extend_face_to_end_of_line (it);
19995 }
19996 else
19997 {
19998 /* Something other than a TAB that draws past
19999 the right edge of the window. Restore
20000 positions to values before the element. */
20001 if (row->reversed_p)
20002 unproduce_glyphs (it, row->used[TEXT_AREA]
20003 - (n_glyphs_before + i));
20004 row->used[TEXT_AREA] = n_glyphs_before + i;
20005
20006 /* Display continuation glyphs. */
20007 it->current_x = x_before;
20008 it->continuation_lines_width += x;
20009 if (!FRAME_WINDOW_P (it->f)
20010 || (row->reversed_p
20011 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20012 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20013 produce_special_glyphs (it, IT_CONTINUATION);
20014 row->continued_p = 1;
20015
20016 extend_face_to_end_of_line (it);
20017
20018 if (nglyphs > 1 && i > 0)
20019 {
20020 row->ends_in_middle_of_char_p = 1;
20021 it->starts_in_middle_of_char_p = 1;
20022 }
20023
20024 /* Restore the height to what it was before the
20025 element not fitting on the line. */
20026 it->max_ascent = ascent;
20027 it->max_descent = descent;
20028 it->max_phys_ascent = phys_ascent;
20029 it->max_phys_descent = phys_descent;
20030 }
20031
20032 break;
20033 }
20034 else if (new_x > it->first_visible_x)
20035 {
20036 /* Increment number of glyphs actually displayed. */
20037 ++it->hpos;
20038
20039 /* Record the maximum and minimum buffer positions
20040 seen so far in glyphs that will be displayed by
20041 this row. */
20042 if (it->bidi_p)
20043 RECORD_MAX_MIN_POS (it);
20044
20045 if (x < it->first_visible_x)
20046 /* Glyph is partially visible, i.e. row starts at
20047 negative X position. */
20048 row->x = x - it->first_visible_x;
20049 }
20050 else
20051 {
20052 /* Glyph is completely off the left margin of the
20053 window. This should not happen because of the
20054 move_it_in_display_line at the start of this
20055 function, unless the text display area of the
20056 window is empty. */
20057 eassert (it->first_visible_x <= it->last_visible_x);
20058 }
20059 }
20060 /* Even if this display element produced no glyphs at all,
20061 we want to record its position. */
20062 if (it->bidi_p && nglyphs == 0)
20063 RECORD_MAX_MIN_POS (it);
20064
20065 row->ascent = max (row->ascent, it->max_ascent);
20066 row->height = max (row->height, it->max_ascent + it->max_descent);
20067 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20068 row->phys_height = max (row->phys_height,
20069 it->max_phys_ascent + it->max_phys_descent);
20070 row->extra_line_spacing = max (row->extra_line_spacing,
20071 it->max_extra_line_spacing);
20072
20073 /* End of this display line if row is continued. */
20074 if (row->continued_p || row->ends_at_zv_p)
20075 break;
20076 }
20077
20078 at_end_of_line:
20079 /* Is this a line end? If yes, we're also done, after making
20080 sure that a non-default face is extended up to the right
20081 margin of the window. */
20082 if (ITERATOR_AT_END_OF_LINE_P (it))
20083 {
20084 int used_before = row->used[TEXT_AREA];
20085
20086 row->ends_in_newline_from_string_p = STRINGP (it->object);
20087
20088 /* Add a space at the end of the line that is used to
20089 display the cursor there. */
20090 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20091 append_space_for_newline (it, 0);
20092
20093 /* Extend the face to the end of the line. */
20094 extend_face_to_end_of_line (it);
20095
20096 /* Make sure we have the position. */
20097 if (used_before == 0)
20098 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20099
20100 /* Record the position of the newline, for use in
20101 find_row_edges. */
20102 it->eol_pos = it->current.pos;
20103
20104 /* Consume the line end. This skips over invisible lines. */
20105 set_iterator_to_next (it, 1);
20106 it->continuation_lines_width = 0;
20107 break;
20108 }
20109
20110 /* Proceed with next display element. Note that this skips
20111 over lines invisible because of selective display. */
20112 set_iterator_to_next (it, 1);
20113
20114 /* If we truncate lines, we are done when the last displayed
20115 glyphs reach past the right margin of the window. */
20116 if (it->line_wrap == TRUNCATE
20117 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20118 ? (it->current_x >= it->last_visible_x)
20119 : (it->current_x > it->last_visible_x)))
20120 {
20121 /* Maybe add truncation glyphs. */
20122 if (!FRAME_WINDOW_P (it->f)
20123 || (row->reversed_p
20124 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20125 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20126 {
20127 int i, n;
20128
20129 if (!row->reversed_p)
20130 {
20131 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20132 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20133 break;
20134 }
20135 else
20136 {
20137 for (i = 0; i < row->used[TEXT_AREA]; i++)
20138 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20139 break;
20140 /* Remove any padding glyphs at the front of ROW, to
20141 make room for the truncation glyphs we will be
20142 adding below. The loop below always inserts at
20143 least one truncation glyph, so also remove the
20144 last glyph added to ROW. */
20145 unproduce_glyphs (it, i + 1);
20146 /* Adjust i for the loop below. */
20147 i = row->used[TEXT_AREA] - (i + 1);
20148 }
20149
20150 it->current_x = x_before;
20151 if (!FRAME_WINDOW_P (it->f))
20152 {
20153 for (n = row->used[TEXT_AREA]; i < n; ++i)
20154 {
20155 row->used[TEXT_AREA] = i;
20156 produce_special_glyphs (it, IT_TRUNCATION);
20157 }
20158 }
20159 else
20160 {
20161 row->used[TEXT_AREA] = i;
20162 produce_special_glyphs (it, IT_TRUNCATION);
20163 }
20164 }
20165 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20166 {
20167 /* Don't truncate if we can overflow newline into fringe. */
20168 if (!get_next_display_element (it))
20169 {
20170 it->continuation_lines_width = 0;
20171 row->ends_at_zv_p = 1;
20172 row->exact_window_width_line_p = 1;
20173 break;
20174 }
20175 if (ITERATOR_AT_END_OF_LINE_P (it))
20176 {
20177 row->exact_window_width_line_p = 1;
20178 goto at_end_of_line;
20179 }
20180 it->current_x = x_before;
20181 }
20182
20183 row->truncated_on_right_p = 1;
20184 it->continuation_lines_width = 0;
20185 reseat_at_next_visible_line_start (it, 0);
20186 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20187 it->hpos = hpos_before;
20188 break;
20189 }
20190 }
20191
20192 if (wrap_data)
20193 bidi_unshelve_cache (wrap_data, 1);
20194
20195 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20196 at the left window margin. */
20197 if (it->first_visible_x
20198 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20199 {
20200 if (!FRAME_WINDOW_P (it->f)
20201 || (row->reversed_p
20202 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20203 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20204 insert_left_trunc_glyphs (it);
20205 row->truncated_on_left_p = 1;
20206 }
20207
20208 /* Remember the position at which this line ends.
20209
20210 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20211 cannot be before the call to find_row_edges below, since that is
20212 where these positions are determined. */
20213 row->end = it->current;
20214 if (!it->bidi_p)
20215 {
20216 row->minpos = row->start.pos;
20217 row->maxpos = row->end.pos;
20218 }
20219 else
20220 {
20221 /* ROW->minpos and ROW->maxpos must be the smallest and
20222 `1 + the largest' buffer positions in ROW. But if ROW was
20223 bidi-reordered, these two positions can be anywhere in the
20224 row, so we must determine them now. */
20225 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20226 }
20227
20228 /* If the start of this line is the overlay arrow-position, then
20229 mark this glyph row as the one containing the overlay arrow.
20230 This is clearly a mess with variable size fonts. It would be
20231 better to let it be displayed like cursors under X. */
20232 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20233 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20234 !NILP (overlay_arrow_string)))
20235 {
20236 /* Overlay arrow in window redisplay is a fringe bitmap. */
20237 if (STRINGP (overlay_arrow_string))
20238 {
20239 struct glyph_row *arrow_row
20240 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20241 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20242 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20243 struct glyph *p = row->glyphs[TEXT_AREA];
20244 struct glyph *p2, *end;
20245
20246 /* Copy the arrow glyphs. */
20247 while (glyph < arrow_end)
20248 *p++ = *glyph++;
20249
20250 /* Throw away padding glyphs. */
20251 p2 = p;
20252 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20253 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20254 ++p2;
20255 if (p2 > p)
20256 {
20257 while (p2 < end)
20258 *p++ = *p2++;
20259 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20260 }
20261 }
20262 else
20263 {
20264 eassert (INTEGERP (overlay_arrow_string));
20265 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20266 }
20267 overlay_arrow_seen = 1;
20268 }
20269
20270 /* Highlight trailing whitespace. */
20271 if (!NILP (Vshow_trailing_whitespace))
20272 highlight_trailing_whitespace (it->f, it->glyph_row);
20273
20274 /* Compute pixel dimensions of this line. */
20275 compute_line_metrics (it);
20276
20277 /* Implementation note: No changes in the glyphs of ROW or in their
20278 faces can be done past this point, because compute_line_metrics
20279 computes ROW's hash value and stores it within the glyph_row
20280 structure. */
20281
20282 /* Record whether this row ends inside an ellipsis. */
20283 row->ends_in_ellipsis_p
20284 = (it->method == GET_FROM_DISPLAY_VECTOR
20285 && it->ellipsis_p);
20286
20287 /* Save fringe bitmaps in this row. */
20288 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20289 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20290 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20291 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20292
20293 it->left_user_fringe_bitmap = 0;
20294 it->left_user_fringe_face_id = 0;
20295 it->right_user_fringe_bitmap = 0;
20296 it->right_user_fringe_face_id = 0;
20297
20298 /* Maybe set the cursor. */
20299 cvpos = it->w->cursor.vpos;
20300 if ((cvpos < 0
20301 /* In bidi-reordered rows, keep checking for proper cursor
20302 position even if one has been found already, because buffer
20303 positions in such rows change non-linearly with ROW->VPOS,
20304 when a line is continued. One exception: when we are at ZV,
20305 display cursor on the first suitable glyph row, since all
20306 the empty rows after that also have their position set to ZV. */
20307 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20308 lines' rows is implemented for bidi-reordered rows. */
20309 || (it->bidi_p
20310 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20311 && PT >= MATRIX_ROW_START_CHARPOS (row)
20312 && PT <= MATRIX_ROW_END_CHARPOS (row)
20313 && cursor_row_p (row))
20314 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20315
20316 /* Prepare for the next line. This line starts horizontally at (X
20317 HPOS) = (0 0). Vertical positions are incremented. As a
20318 convenience for the caller, IT->glyph_row is set to the next
20319 row to be used. */
20320 it->current_x = it->hpos = 0;
20321 it->current_y += row->height;
20322 SET_TEXT_POS (it->eol_pos, 0, 0);
20323 ++it->vpos;
20324 ++it->glyph_row;
20325 /* The next row should by default use the same value of the
20326 reversed_p flag as this one. set_iterator_to_next decides when
20327 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20328 the flag accordingly. */
20329 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20330 it->glyph_row->reversed_p = row->reversed_p;
20331 it->start = row->end;
20332 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20333
20334 #undef RECORD_MAX_MIN_POS
20335 }
20336
20337 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20338 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20339 doc: /* Return paragraph direction at point in BUFFER.
20340 Value is either `left-to-right' or `right-to-left'.
20341 If BUFFER is omitted or nil, it defaults to the current buffer.
20342
20343 Paragraph direction determines how the text in the paragraph is displayed.
20344 In left-to-right paragraphs, text begins at the left margin of the window
20345 and the reading direction is generally left to right. In right-to-left
20346 paragraphs, text begins at the right margin and is read from right to left.
20347
20348 See also `bidi-paragraph-direction'. */)
20349 (Lisp_Object buffer)
20350 {
20351 struct buffer *buf = current_buffer;
20352 struct buffer *old = buf;
20353
20354 if (! NILP (buffer))
20355 {
20356 CHECK_BUFFER (buffer);
20357 buf = XBUFFER (buffer);
20358 }
20359
20360 if (NILP (BVAR (buf, bidi_display_reordering))
20361 || NILP (BVAR (buf, enable_multibyte_characters))
20362 /* When we are loading loadup.el, the character property tables
20363 needed for bidi iteration are not yet available. */
20364 || !NILP (Vpurify_flag))
20365 return Qleft_to_right;
20366 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20367 return BVAR (buf, bidi_paragraph_direction);
20368 else
20369 {
20370 /* Determine the direction from buffer text. We could try to
20371 use current_matrix if it is up to date, but this seems fast
20372 enough as it is. */
20373 struct bidi_it itb;
20374 ptrdiff_t pos = BUF_PT (buf);
20375 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20376 int c;
20377 void *itb_data = bidi_shelve_cache ();
20378
20379 set_buffer_temp (buf);
20380 /* bidi_paragraph_init finds the base direction of the paragraph
20381 by searching forward from paragraph start. We need the base
20382 direction of the current or _previous_ paragraph, so we need
20383 to make sure we are within that paragraph. To that end, find
20384 the previous non-empty line. */
20385 if (pos >= ZV && pos > BEGV)
20386 DEC_BOTH (pos, bytepos);
20387 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20388 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20389 {
20390 while ((c = FETCH_BYTE (bytepos)) == '\n'
20391 || c == ' ' || c == '\t' || c == '\f')
20392 {
20393 if (bytepos <= BEGV_BYTE)
20394 break;
20395 bytepos--;
20396 pos--;
20397 }
20398 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20399 bytepos--;
20400 }
20401 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20402 itb.paragraph_dir = NEUTRAL_DIR;
20403 itb.string.s = NULL;
20404 itb.string.lstring = Qnil;
20405 itb.string.bufpos = 0;
20406 itb.string.from_disp_str = 0;
20407 itb.string.unibyte = 0;
20408 /* We have no window to use here for ignoring window-specific
20409 overlays. Using NULL for window pointer will cause
20410 compute_display_string_pos to use the current buffer. */
20411 itb.w = NULL;
20412 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20413 bidi_unshelve_cache (itb_data, 0);
20414 set_buffer_temp (old);
20415 switch (itb.paragraph_dir)
20416 {
20417 case L2R:
20418 return Qleft_to_right;
20419 break;
20420 case R2L:
20421 return Qright_to_left;
20422 break;
20423 default:
20424 emacs_abort ();
20425 }
20426 }
20427 }
20428
20429 DEFUN ("move-point-visually", Fmove_point_visually,
20430 Smove_point_visually, 1, 1, 0,
20431 doc: /* Move point in the visual order in the specified DIRECTION.
20432 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20433 left.
20434
20435 Value is the new character position of point. */)
20436 (Lisp_Object direction)
20437 {
20438 struct window *w = XWINDOW (selected_window);
20439 struct buffer *b = XBUFFER (w->contents);
20440 struct glyph_row *row;
20441 int dir;
20442 Lisp_Object paragraph_dir;
20443
20444 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20445 (!(ROW)->continued_p \
20446 && INTEGERP ((GLYPH)->object) \
20447 && (GLYPH)->type == CHAR_GLYPH \
20448 && (GLYPH)->u.ch == ' ' \
20449 && (GLYPH)->charpos >= 0 \
20450 && !(GLYPH)->avoid_cursor_p)
20451
20452 CHECK_NUMBER (direction);
20453 dir = XINT (direction);
20454 if (dir > 0)
20455 dir = 1;
20456 else
20457 dir = -1;
20458
20459 /* If current matrix is up-to-date, we can use the information
20460 recorded in the glyphs, at least as long as the goal is on the
20461 screen. */
20462 if (w->window_end_valid
20463 && !windows_or_buffers_changed
20464 && b
20465 && !b->clip_changed
20466 && !b->prevent_redisplay_optimizations_p
20467 && !window_outdated (w)
20468 && w->cursor.vpos >= 0
20469 && w->cursor.vpos < w->current_matrix->nrows
20470 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20471 {
20472 struct glyph *g = row->glyphs[TEXT_AREA];
20473 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20474 struct glyph *gpt = g + w->cursor.hpos;
20475
20476 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20477 {
20478 if (BUFFERP (g->object) && g->charpos != PT)
20479 {
20480 SET_PT (g->charpos);
20481 w->cursor.vpos = -1;
20482 return make_number (PT);
20483 }
20484 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20485 {
20486 ptrdiff_t new_pos;
20487
20488 if (BUFFERP (gpt->object))
20489 {
20490 new_pos = PT;
20491 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20492 new_pos += (row->reversed_p ? -dir : dir);
20493 else
20494 new_pos -= (row->reversed_p ? -dir : dir);;
20495 }
20496 else if (BUFFERP (g->object))
20497 new_pos = g->charpos;
20498 else
20499 break;
20500 SET_PT (new_pos);
20501 w->cursor.vpos = -1;
20502 return make_number (PT);
20503 }
20504 else if (ROW_GLYPH_NEWLINE_P (row, g))
20505 {
20506 /* Glyphs inserted at the end of a non-empty line for
20507 positioning the cursor have zero charpos, so we must
20508 deduce the value of point by other means. */
20509 if (g->charpos > 0)
20510 SET_PT (g->charpos);
20511 else if (row->ends_at_zv_p && PT != ZV)
20512 SET_PT (ZV);
20513 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20514 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20515 else
20516 break;
20517 w->cursor.vpos = -1;
20518 return make_number (PT);
20519 }
20520 }
20521 if (g == e || INTEGERP (g->object))
20522 {
20523 if (row->truncated_on_left_p || row->truncated_on_right_p)
20524 goto simulate_display;
20525 if (!row->reversed_p)
20526 row += dir;
20527 else
20528 row -= dir;
20529 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20530 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20531 goto simulate_display;
20532
20533 if (dir > 0)
20534 {
20535 if (row->reversed_p && !row->continued_p)
20536 {
20537 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20538 w->cursor.vpos = -1;
20539 return make_number (PT);
20540 }
20541 g = row->glyphs[TEXT_AREA];
20542 e = g + row->used[TEXT_AREA];
20543 for ( ; g < e; g++)
20544 {
20545 if (BUFFERP (g->object)
20546 /* Empty lines have only one glyph, which stands
20547 for the newline, and whose charpos is the
20548 buffer position of the newline. */
20549 || ROW_GLYPH_NEWLINE_P (row, g)
20550 /* When the buffer ends in a newline, the line at
20551 EOB also has one glyph, but its charpos is -1. */
20552 || (row->ends_at_zv_p
20553 && !row->reversed_p
20554 && INTEGERP (g->object)
20555 && g->type == CHAR_GLYPH
20556 && g->u.ch == ' '))
20557 {
20558 if (g->charpos > 0)
20559 SET_PT (g->charpos);
20560 else if (!row->reversed_p
20561 && row->ends_at_zv_p
20562 && PT != ZV)
20563 SET_PT (ZV);
20564 else
20565 continue;
20566 w->cursor.vpos = -1;
20567 return make_number (PT);
20568 }
20569 }
20570 }
20571 else
20572 {
20573 if (!row->reversed_p && !row->continued_p)
20574 {
20575 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20576 w->cursor.vpos = -1;
20577 return make_number (PT);
20578 }
20579 e = row->glyphs[TEXT_AREA];
20580 g = e + row->used[TEXT_AREA] - 1;
20581 for ( ; g >= e; g--)
20582 {
20583 if (BUFFERP (g->object)
20584 || (ROW_GLYPH_NEWLINE_P (row, g)
20585 && g->charpos > 0)
20586 /* Empty R2L lines on GUI frames have the buffer
20587 position of the newline stored in the stretch
20588 glyph. */
20589 || g->type == STRETCH_GLYPH
20590 || (row->ends_at_zv_p
20591 && row->reversed_p
20592 && INTEGERP (g->object)
20593 && g->type == CHAR_GLYPH
20594 && g->u.ch == ' '))
20595 {
20596 if (g->charpos > 0)
20597 SET_PT (g->charpos);
20598 else if (row->reversed_p
20599 && row->ends_at_zv_p
20600 && PT != ZV)
20601 SET_PT (ZV);
20602 else
20603 continue;
20604 w->cursor.vpos = -1;
20605 return make_number (PT);
20606 }
20607 }
20608 }
20609 }
20610 }
20611
20612 simulate_display:
20613
20614 /* If we wind up here, we failed to move by using the glyphs, so we
20615 need to simulate display instead. */
20616
20617 if (b)
20618 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20619 else
20620 paragraph_dir = Qleft_to_right;
20621 if (EQ (paragraph_dir, Qright_to_left))
20622 dir = -dir;
20623 if (PT <= BEGV && dir < 0)
20624 xsignal0 (Qbeginning_of_buffer);
20625 else if (PT >= ZV && dir > 0)
20626 xsignal0 (Qend_of_buffer);
20627 else
20628 {
20629 struct text_pos pt;
20630 struct it it;
20631 int pt_x, target_x, pixel_width, pt_vpos;
20632 bool at_eol_p;
20633 bool overshoot_expected = false;
20634 bool target_is_eol_p = false;
20635
20636 /* Setup the arena. */
20637 SET_TEXT_POS (pt, PT, PT_BYTE);
20638 start_display (&it, w, pt);
20639
20640 if (it.cmp_it.id < 0
20641 && it.method == GET_FROM_STRING
20642 && it.area == TEXT_AREA
20643 && it.string_from_display_prop_p
20644 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20645 overshoot_expected = true;
20646
20647 /* Find the X coordinate of point. We start from the beginning
20648 of this or previous line to make sure we are before point in
20649 the logical order (since the move_it_* functions can only
20650 move forward). */
20651 reseat:
20652 reseat_at_previous_visible_line_start (&it);
20653 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20654 if (IT_CHARPOS (it) != PT)
20655 {
20656 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20657 -1, -1, -1, MOVE_TO_POS);
20658 /* If we missed point because the character there is
20659 displayed out of a display vector that has more than one
20660 glyph, retry expecting overshoot. */
20661 if (it.method == GET_FROM_DISPLAY_VECTOR
20662 && it.current.dpvec_index > 0
20663 && !overshoot_expected)
20664 {
20665 overshoot_expected = true;
20666 goto reseat;
20667 }
20668 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20669 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20670 }
20671 pt_x = it.current_x;
20672 pt_vpos = it.vpos;
20673 if (dir > 0 || overshoot_expected)
20674 {
20675 struct glyph_row *row = it.glyph_row;
20676
20677 /* When point is at beginning of line, we don't have
20678 information about the glyph there loaded into struct
20679 it. Calling get_next_display_element fixes that. */
20680 if (pt_x == 0)
20681 get_next_display_element (&it);
20682 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20683 it.glyph_row = NULL;
20684 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20685 it.glyph_row = row;
20686 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20687 it, lest it will become out of sync with it's buffer
20688 position. */
20689 it.current_x = pt_x;
20690 }
20691 else
20692 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20693 pixel_width = it.pixel_width;
20694 if (overshoot_expected && at_eol_p)
20695 pixel_width = 0;
20696 else if (pixel_width <= 0)
20697 pixel_width = 1;
20698
20699 /* If there's a display string (or something similar) at point,
20700 we are actually at the glyph to the left of point, so we need
20701 to correct the X coordinate. */
20702 if (overshoot_expected)
20703 {
20704 if (it.bidi_p)
20705 pt_x += pixel_width * it.bidi_it.scan_dir;
20706 else
20707 pt_x += pixel_width;
20708 }
20709
20710 /* Compute target X coordinate, either to the left or to the
20711 right of point. On TTY frames, all characters have the same
20712 pixel width of 1, so we can use that. On GUI frames we don't
20713 have an easy way of getting at the pixel width of the
20714 character to the left of point, so we use a different method
20715 of getting to that place. */
20716 if (dir > 0)
20717 target_x = pt_x + pixel_width;
20718 else
20719 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20720
20721 /* Target X coordinate could be one line above or below the line
20722 of point, in which case we need to adjust the target X
20723 coordinate. Also, if moving to the left, we need to begin at
20724 the left edge of the point's screen line. */
20725 if (dir < 0)
20726 {
20727 if (pt_x > 0)
20728 {
20729 start_display (&it, w, pt);
20730 reseat_at_previous_visible_line_start (&it);
20731 it.current_x = it.current_y = it.hpos = 0;
20732 if (pt_vpos != 0)
20733 move_it_by_lines (&it, pt_vpos);
20734 }
20735 else
20736 {
20737 move_it_by_lines (&it, -1);
20738 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20739 target_is_eol_p = true;
20740 }
20741 }
20742 else
20743 {
20744 if (at_eol_p
20745 || (target_x >= it.last_visible_x
20746 && it.line_wrap != TRUNCATE))
20747 {
20748 if (pt_x > 0)
20749 move_it_by_lines (&it, 0);
20750 move_it_by_lines (&it, 1);
20751 target_x = 0;
20752 }
20753 }
20754
20755 /* Move to the target X coordinate. */
20756 #ifdef HAVE_WINDOW_SYSTEM
20757 /* On GUI frames, as we don't know the X coordinate of the
20758 character to the left of point, moving point to the left
20759 requires walking, one grapheme cluster at a time, until we
20760 find ourself at a place immediately to the left of the
20761 character at point. */
20762 if (FRAME_WINDOW_P (it.f) && dir < 0)
20763 {
20764 struct text_pos new_pos;
20765 enum move_it_result rc = MOVE_X_REACHED;
20766
20767 if (it.current_x == 0)
20768 get_next_display_element (&it);
20769 if (it.what == IT_COMPOSITION)
20770 {
20771 new_pos.charpos = it.cmp_it.charpos;
20772 new_pos.bytepos = -1;
20773 }
20774 else
20775 new_pos = it.current.pos;
20776
20777 while (it.current_x + it.pixel_width <= target_x
20778 && rc == MOVE_X_REACHED)
20779 {
20780 int new_x = it.current_x + it.pixel_width;
20781
20782 /* For composed characters, we want the position of the
20783 first character in the grapheme cluster (usually, the
20784 composition's base character), whereas it.current
20785 might give us the position of the _last_ one, e.g. if
20786 the composition is rendered in reverse due to bidi
20787 reordering. */
20788 if (it.what == IT_COMPOSITION)
20789 {
20790 new_pos.charpos = it.cmp_it.charpos;
20791 new_pos.bytepos = -1;
20792 }
20793 else
20794 new_pos = it.current.pos;
20795 if (new_x == it.current_x)
20796 new_x++;
20797 rc = move_it_in_display_line_to (&it, ZV, new_x,
20798 MOVE_TO_POS | MOVE_TO_X);
20799 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20800 break;
20801 }
20802 /* The previous position we saw in the loop is the one we
20803 want. */
20804 if (new_pos.bytepos == -1)
20805 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20806 it.current.pos = new_pos;
20807 }
20808 else
20809 #endif
20810 if (it.current_x != target_x)
20811 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20812
20813 /* When lines are truncated, the above loop will stop at the
20814 window edge. But we want to get to the end of line, even if
20815 it is beyond the window edge; automatic hscroll will then
20816 scroll the window to show point as appropriate. */
20817 if (target_is_eol_p && it.line_wrap == TRUNCATE
20818 && get_next_display_element (&it))
20819 {
20820 struct text_pos new_pos = it.current.pos;
20821
20822 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20823 {
20824 set_iterator_to_next (&it, 0);
20825 if (it.method == GET_FROM_BUFFER)
20826 new_pos = it.current.pos;
20827 if (!get_next_display_element (&it))
20828 break;
20829 }
20830
20831 it.current.pos = new_pos;
20832 }
20833
20834 /* If we ended up in a display string that covers point, move to
20835 buffer position to the right in the visual order. */
20836 if (dir > 0)
20837 {
20838 while (IT_CHARPOS (it) == PT)
20839 {
20840 set_iterator_to_next (&it, 0);
20841 if (!get_next_display_element (&it))
20842 break;
20843 }
20844 }
20845
20846 /* Move point to that position. */
20847 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20848 }
20849
20850 return make_number (PT);
20851
20852 #undef ROW_GLYPH_NEWLINE_P
20853 }
20854
20855 \f
20856 /***********************************************************************
20857 Menu Bar
20858 ***********************************************************************/
20859
20860 /* Redisplay the menu bar in the frame for window W.
20861
20862 The menu bar of X frames that don't have X toolkit support is
20863 displayed in a special window W->frame->menu_bar_window.
20864
20865 The menu bar of terminal frames is treated specially as far as
20866 glyph matrices are concerned. Menu bar lines are not part of
20867 windows, so the update is done directly on the frame matrix rows
20868 for the menu bar. */
20869
20870 static void
20871 display_menu_bar (struct window *w)
20872 {
20873 struct frame *f = XFRAME (WINDOW_FRAME (w));
20874 struct it it;
20875 Lisp_Object items;
20876 int i;
20877
20878 /* Don't do all this for graphical frames. */
20879 #ifdef HAVE_NTGUI
20880 if (FRAME_W32_P (f))
20881 return;
20882 #endif
20883 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20884 if (FRAME_X_P (f))
20885 return;
20886 #endif
20887
20888 #ifdef HAVE_NS
20889 if (FRAME_NS_P (f))
20890 return;
20891 #endif /* HAVE_NS */
20892
20893 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20894 eassert (!FRAME_WINDOW_P (f));
20895 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20896 it.first_visible_x = 0;
20897 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20898 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20899 if (FRAME_WINDOW_P (f))
20900 {
20901 /* Menu bar lines are displayed in the desired matrix of the
20902 dummy window menu_bar_window. */
20903 struct window *menu_w;
20904 menu_w = XWINDOW (f->menu_bar_window);
20905 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20906 MENU_FACE_ID);
20907 it.first_visible_x = 0;
20908 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20909 }
20910 else
20911 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20912 {
20913 /* This is a TTY frame, i.e. character hpos/vpos are used as
20914 pixel x/y. */
20915 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20916 MENU_FACE_ID);
20917 it.first_visible_x = 0;
20918 it.last_visible_x = FRAME_COLS (f);
20919 }
20920
20921 /* FIXME: This should be controlled by a user option. See the
20922 comments in redisplay_tool_bar and display_mode_line about
20923 this. */
20924 it.paragraph_embedding = L2R;
20925
20926 /* Clear all rows of the menu bar. */
20927 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20928 {
20929 struct glyph_row *row = it.glyph_row + i;
20930 clear_glyph_row (row);
20931 row->enabled_p = true;
20932 row->full_width_p = 1;
20933 }
20934
20935 /* Display all items of the menu bar. */
20936 items = FRAME_MENU_BAR_ITEMS (it.f);
20937 for (i = 0; i < ASIZE (items); i += 4)
20938 {
20939 Lisp_Object string;
20940
20941 /* Stop at nil string. */
20942 string = AREF (items, i + 1);
20943 if (NILP (string))
20944 break;
20945
20946 /* Remember where item was displayed. */
20947 ASET (items, i + 3, make_number (it.hpos));
20948
20949 /* Display the item, pad with one space. */
20950 if (it.current_x < it.last_visible_x)
20951 display_string (NULL, string, Qnil, 0, 0, &it,
20952 SCHARS (string) + 1, 0, 0, -1);
20953 }
20954
20955 /* Fill out the line with spaces. */
20956 if (it.current_x < it.last_visible_x)
20957 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20958
20959 /* Compute the total height of the lines. */
20960 compute_line_metrics (&it);
20961 }
20962
20963 /* Deep copy of a glyph row, including the glyphs. */
20964 static void
20965 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20966 {
20967 struct glyph *pointers[1 + LAST_AREA];
20968 int to_used = to->used[TEXT_AREA];
20969
20970 /* Save glyph pointers of TO. */
20971 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20972
20973 /* Do a structure assignment. */
20974 *to = *from;
20975
20976 /* Restore original glyph pointers of TO. */
20977 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20978
20979 /* Copy the glyphs. */
20980 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20981 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20982
20983 /* If we filled only part of the TO row, fill the rest with
20984 space_glyph (which will display as empty space). */
20985 if (to_used > from->used[TEXT_AREA])
20986 fill_up_frame_row_with_spaces (to, to_used);
20987 }
20988
20989 /* Display one menu item on a TTY, by overwriting the glyphs in the
20990 frame F's desired glyph matrix with glyphs produced from the menu
20991 item text. Called from term.c to display TTY drop-down menus one
20992 item at a time.
20993
20994 ITEM_TEXT is the menu item text as a C string.
20995
20996 FACE_ID is the face ID to be used for this menu item. FACE_ID
20997 could specify one of 3 faces: a face for an enabled item, a face
20998 for a disabled item, or a face for a selected item.
20999
21000 X and Y are coordinates of the first glyph in the frame's desired
21001 matrix to be overwritten by the menu item. Since this is a TTY, Y
21002 is the zero-based number of the glyph row and X is the zero-based
21003 glyph number in the row, starting from left, where to start
21004 displaying the item.
21005
21006 SUBMENU non-zero means this menu item drops down a submenu, which
21007 should be indicated by displaying a proper visual cue after the
21008 item text. */
21009
21010 void
21011 display_tty_menu_item (const char *item_text, int width, int face_id,
21012 int x, int y, int submenu)
21013 {
21014 struct it it;
21015 struct frame *f = SELECTED_FRAME ();
21016 struct window *w = XWINDOW (f->selected_window);
21017 int saved_used, saved_truncated, saved_width, saved_reversed;
21018 struct glyph_row *row;
21019 size_t item_len = strlen (item_text);
21020
21021 eassert (FRAME_TERMCAP_P (f));
21022
21023 /* Don't write beyond the matrix's last row. This can happen for
21024 TTY screens that are not high enough to show the entire menu.
21025 (This is actually a bit of defensive programming, as
21026 tty_menu_display already limits the number of menu items to one
21027 less than the number of screen lines.) */
21028 if (y >= f->desired_matrix->nrows)
21029 return;
21030
21031 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21032 it.first_visible_x = 0;
21033 it.last_visible_x = FRAME_COLS (f) - 1;
21034 row = it.glyph_row;
21035 /* Start with the row contents from the current matrix. */
21036 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21037 saved_width = row->full_width_p;
21038 row->full_width_p = 1;
21039 saved_reversed = row->reversed_p;
21040 row->reversed_p = 0;
21041 row->enabled_p = true;
21042
21043 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21044 desired face. */
21045 eassert (x < f->desired_matrix->matrix_w);
21046 it.current_x = it.hpos = x;
21047 it.current_y = it.vpos = y;
21048 saved_used = row->used[TEXT_AREA];
21049 saved_truncated = row->truncated_on_right_p;
21050 row->used[TEXT_AREA] = x;
21051 it.face_id = face_id;
21052 it.line_wrap = TRUNCATE;
21053
21054 /* FIXME: This should be controlled by a user option. See the
21055 comments in redisplay_tool_bar and display_mode_line about this.
21056 Also, if paragraph_embedding could ever be R2L, changes will be
21057 needed to avoid shifting to the right the row characters in
21058 term.c:append_glyph. */
21059 it.paragraph_embedding = L2R;
21060
21061 /* Pad with a space on the left. */
21062 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21063 width--;
21064 /* Display the menu item, pad with spaces to WIDTH. */
21065 if (submenu)
21066 {
21067 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21068 item_len, 0, FRAME_COLS (f) - 1, -1);
21069 width -= item_len;
21070 /* Indicate with " >" that there's a submenu. */
21071 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21072 FRAME_COLS (f) - 1, -1);
21073 }
21074 else
21075 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21076 width, 0, FRAME_COLS (f) - 1, -1);
21077
21078 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21079 row->truncated_on_right_p = saved_truncated;
21080 row->hash = row_hash (row);
21081 row->full_width_p = saved_width;
21082 row->reversed_p = saved_reversed;
21083 }
21084 \f
21085 /***********************************************************************
21086 Mode Line
21087 ***********************************************************************/
21088
21089 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21090 FORCE is non-zero, redisplay mode lines unconditionally.
21091 Otherwise, redisplay only mode lines that are garbaged. Value is
21092 the number of windows whose mode lines were redisplayed. */
21093
21094 static int
21095 redisplay_mode_lines (Lisp_Object window, bool force)
21096 {
21097 int nwindows = 0;
21098
21099 while (!NILP (window))
21100 {
21101 struct window *w = XWINDOW (window);
21102
21103 if (WINDOWP (w->contents))
21104 nwindows += redisplay_mode_lines (w->contents, force);
21105 else if (force
21106 || FRAME_GARBAGED_P (XFRAME (w->frame))
21107 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21108 {
21109 struct text_pos lpoint;
21110 struct buffer *old = current_buffer;
21111
21112 /* Set the window's buffer for the mode line display. */
21113 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21114 set_buffer_internal_1 (XBUFFER (w->contents));
21115
21116 /* Point refers normally to the selected window. For any
21117 other window, set up appropriate value. */
21118 if (!EQ (window, selected_window))
21119 {
21120 struct text_pos pt;
21121
21122 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21123 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21124 }
21125
21126 /* Display mode lines. */
21127 clear_glyph_matrix (w->desired_matrix);
21128 if (display_mode_lines (w))
21129 ++nwindows;
21130
21131 /* Restore old settings. */
21132 set_buffer_internal_1 (old);
21133 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21134 }
21135
21136 window = w->next;
21137 }
21138
21139 return nwindows;
21140 }
21141
21142
21143 /* Display the mode and/or header line of window W. Value is the
21144 sum number of mode lines and header lines displayed. */
21145
21146 static int
21147 display_mode_lines (struct window *w)
21148 {
21149 Lisp_Object old_selected_window = selected_window;
21150 Lisp_Object old_selected_frame = selected_frame;
21151 Lisp_Object new_frame = w->frame;
21152 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21153 int n = 0;
21154
21155 selected_frame = new_frame;
21156 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21157 or window's point, then we'd need select_window_1 here as well. */
21158 XSETWINDOW (selected_window, w);
21159 XFRAME (new_frame)->selected_window = selected_window;
21160
21161 /* These will be set while the mode line specs are processed. */
21162 line_number_displayed = 0;
21163 w->column_number_displayed = -1;
21164
21165 if (WINDOW_WANTS_MODELINE_P (w))
21166 {
21167 struct window *sel_w = XWINDOW (old_selected_window);
21168
21169 /* Select mode line face based on the real selected window. */
21170 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21171 BVAR (current_buffer, mode_line_format));
21172 ++n;
21173 }
21174
21175 if (WINDOW_WANTS_HEADER_LINE_P (w))
21176 {
21177 display_mode_line (w, HEADER_LINE_FACE_ID,
21178 BVAR (current_buffer, header_line_format));
21179 ++n;
21180 }
21181
21182 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21183 selected_frame = old_selected_frame;
21184 selected_window = old_selected_window;
21185 if (n > 0)
21186 w->must_be_updated_p = true;
21187 return n;
21188 }
21189
21190
21191 /* Display mode or header line of window W. FACE_ID specifies which
21192 line to display; it is either MODE_LINE_FACE_ID or
21193 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21194 display. Value is the pixel height of the mode/header line
21195 displayed. */
21196
21197 static int
21198 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21199 {
21200 struct it it;
21201 struct face *face;
21202 ptrdiff_t count = SPECPDL_INDEX ();
21203
21204 init_iterator (&it, w, -1, -1, NULL, face_id);
21205 /* Don't extend on a previously drawn mode-line.
21206 This may happen if called from pos_visible_p. */
21207 it.glyph_row->enabled_p = false;
21208 prepare_desired_row (it.glyph_row);
21209
21210 it.glyph_row->mode_line_p = 1;
21211
21212 /* FIXME: This should be controlled by a user option. But
21213 supporting such an option is not trivial, since the mode line is
21214 made up of many separate strings. */
21215 it.paragraph_embedding = L2R;
21216
21217 record_unwind_protect (unwind_format_mode_line,
21218 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21219
21220 mode_line_target = MODE_LINE_DISPLAY;
21221
21222 /* Temporarily make frame's keyboard the current kboard so that
21223 kboard-local variables in the mode_line_format will get the right
21224 values. */
21225 push_kboard (FRAME_KBOARD (it.f));
21226 record_unwind_save_match_data ();
21227 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21228 pop_kboard ();
21229
21230 unbind_to (count, Qnil);
21231
21232 /* Fill up with spaces. */
21233 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21234
21235 compute_line_metrics (&it);
21236 it.glyph_row->full_width_p = 1;
21237 it.glyph_row->continued_p = 0;
21238 it.glyph_row->truncated_on_left_p = 0;
21239 it.glyph_row->truncated_on_right_p = 0;
21240
21241 /* Make a 3D mode-line have a shadow at its right end. */
21242 face = FACE_FROM_ID (it.f, face_id);
21243 extend_face_to_end_of_line (&it);
21244 if (face->box != FACE_NO_BOX)
21245 {
21246 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21247 + it.glyph_row->used[TEXT_AREA] - 1);
21248 last->right_box_line_p = 1;
21249 }
21250
21251 return it.glyph_row->height;
21252 }
21253
21254 /* Move element ELT in LIST to the front of LIST.
21255 Return the updated list. */
21256
21257 static Lisp_Object
21258 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21259 {
21260 register Lisp_Object tail, prev;
21261 register Lisp_Object tem;
21262
21263 tail = list;
21264 prev = Qnil;
21265 while (CONSP (tail))
21266 {
21267 tem = XCAR (tail);
21268
21269 if (EQ (elt, tem))
21270 {
21271 /* Splice out the link TAIL. */
21272 if (NILP (prev))
21273 list = XCDR (tail);
21274 else
21275 Fsetcdr (prev, XCDR (tail));
21276
21277 /* Now make it the first. */
21278 Fsetcdr (tail, list);
21279 return tail;
21280 }
21281 else
21282 prev = tail;
21283 tail = XCDR (tail);
21284 QUIT;
21285 }
21286
21287 /* Not found--return unchanged LIST. */
21288 return list;
21289 }
21290
21291 /* Contribute ELT to the mode line for window IT->w. How it
21292 translates into text depends on its data type.
21293
21294 IT describes the display environment in which we display, as usual.
21295
21296 DEPTH is the depth in recursion. It is used to prevent
21297 infinite recursion here.
21298
21299 FIELD_WIDTH is the number of characters the display of ELT should
21300 occupy in the mode line, and PRECISION is the maximum number of
21301 characters to display from ELT's representation. See
21302 display_string for details.
21303
21304 Returns the hpos of the end of the text generated by ELT.
21305
21306 PROPS is a property list to add to any string we encounter.
21307
21308 If RISKY is nonzero, remove (disregard) any properties in any string
21309 we encounter, and ignore :eval and :propertize.
21310
21311 The global variable `mode_line_target' determines whether the
21312 output is passed to `store_mode_line_noprop',
21313 `store_mode_line_string', or `display_string'. */
21314
21315 static int
21316 display_mode_element (struct it *it, int depth, int field_width, int precision,
21317 Lisp_Object elt, Lisp_Object props, int risky)
21318 {
21319 int n = 0, field, prec;
21320 int literal = 0;
21321
21322 tail_recurse:
21323 if (depth > 100)
21324 elt = build_string ("*too-deep*");
21325
21326 depth++;
21327
21328 switch (XTYPE (elt))
21329 {
21330 case Lisp_String:
21331 {
21332 /* A string: output it and check for %-constructs within it. */
21333 unsigned char c;
21334 ptrdiff_t offset = 0;
21335
21336 if (SCHARS (elt) > 0
21337 && (!NILP (props) || risky))
21338 {
21339 Lisp_Object oprops, aelt;
21340 oprops = Ftext_properties_at (make_number (0), elt);
21341
21342 /* If the starting string's properties are not what
21343 we want, translate the string. Also, if the string
21344 is risky, do that anyway. */
21345
21346 if (NILP (Fequal (props, oprops)) || risky)
21347 {
21348 /* If the starting string has properties,
21349 merge the specified ones onto the existing ones. */
21350 if (! NILP (oprops) && !risky)
21351 {
21352 Lisp_Object tem;
21353
21354 oprops = Fcopy_sequence (oprops);
21355 tem = props;
21356 while (CONSP (tem))
21357 {
21358 oprops = Fplist_put (oprops, XCAR (tem),
21359 XCAR (XCDR (tem)));
21360 tem = XCDR (XCDR (tem));
21361 }
21362 props = oprops;
21363 }
21364
21365 aelt = Fassoc (elt, mode_line_proptrans_alist);
21366 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21367 {
21368 /* AELT is what we want. Move it to the front
21369 without consing. */
21370 elt = XCAR (aelt);
21371 mode_line_proptrans_alist
21372 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21373 }
21374 else
21375 {
21376 Lisp_Object tem;
21377
21378 /* If AELT has the wrong props, it is useless.
21379 so get rid of it. */
21380 if (! NILP (aelt))
21381 mode_line_proptrans_alist
21382 = Fdelq (aelt, mode_line_proptrans_alist);
21383
21384 elt = Fcopy_sequence (elt);
21385 Fset_text_properties (make_number (0), Flength (elt),
21386 props, elt);
21387 /* Add this item to mode_line_proptrans_alist. */
21388 mode_line_proptrans_alist
21389 = Fcons (Fcons (elt, props),
21390 mode_line_proptrans_alist);
21391 /* Truncate mode_line_proptrans_alist
21392 to at most 50 elements. */
21393 tem = Fnthcdr (make_number (50),
21394 mode_line_proptrans_alist);
21395 if (! NILP (tem))
21396 XSETCDR (tem, Qnil);
21397 }
21398 }
21399 }
21400
21401 offset = 0;
21402
21403 if (literal)
21404 {
21405 prec = precision - n;
21406 switch (mode_line_target)
21407 {
21408 case MODE_LINE_NOPROP:
21409 case MODE_LINE_TITLE:
21410 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21411 break;
21412 case MODE_LINE_STRING:
21413 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21414 break;
21415 case MODE_LINE_DISPLAY:
21416 n += display_string (NULL, elt, Qnil, 0, 0, it,
21417 0, prec, 0, STRING_MULTIBYTE (elt));
21418 break;
21419 }
21420
21421 break;
21422 }
21423
21424 /* Handle the non-literal case. */
21425
21426 while ((precision <= 0 || n < precision)
21427 && SREF (elt, offset) != 0
21428 && (mode_line_target != MODE_LINE_DISPLAY
21429 || it->current_x < it->last_visible_x))
21430 {
21431 ptrdiff_t last_offset = offset;
21432
21433 /* Advance to end of string or next format specifier. */
21434 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21435 ;
21436
21437 if (offset - 1 != last_offset)
21438 {
21439 ptrdiff_t nchars, nbytes;
21440
21441 /* Output to end of string or up to '%'. Field width
21442 is length of string. Don't output more than
21443 PRECISION allows us. */
21444 offset--;
21445
21446 prec = c_string_width (SDATA (elt) + last_offset,
21447 offset - last_offset, precision - n,
21448 &nchars, &nbytes);
21449
21450 switch (mode_line_target)
21451 {
21452 case MODE_LINE_NOPROP:
21453 case MODE_LINE_TITLE:
21454 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21455 break;
21456 case MODE_LINE_STRING:
21457 {
21458 ptrdiff_t bytepos = last_offset;
21459 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21460 ptrdiff_t endpos = (precision <= 0
21461 ? string_byte_to_char (elt, offset)
21462 : charpos + nchars);
21463
21464 n += store_mode_line_string (NULL,
21465 Fsubstring (elt, make_number (charpos),
21466 make_number (endpos)),
21467 0, 0, 0, Qnil);
21468 }
21469 break;
21470 case MODE_LINE_DISPLAY:
21471 {
21472 ptrdiff_t bytepos = last_offset;
21473 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21474
21475 if (precision <= 0)
21476 nchars = string_byte_to_char (elt, offset) - charpos;
21477 n += display_string (NULL, elt, Qnil, 0, charpos,
21478 it, 0, nchars, 0,
21479 STRING_MULTIBYTE (elt));
21480 }
21481 break;
21482 }
21483 }
21484 else /* c == '%' */
21485 {
21486 ptrdiff_t percent_position = offset;
21487
21488 /* Get the specified minimum width. Zero means
21489 don't pad. */
21490 field = 0;
21491 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21492 field = field * 10 + c - '0';
21493
21494 /* Don't pad beyond the total padding allowed. */
21495 if (field_width - n > 0 && field > field_width - n)
21496 field = field_width - n;
21497
21498 /* Note that either PRECISION <= 0 or N < PRECISION. */
21499 prec = precision - n;
21500
21501 if (c == 'M')
21502 n += display_mode_element (it, depth, field, prec,
21503 Vglobal_mode_string, props,
21504 risky);
21505 else if (c != 0)
21506 {
21507 bool multibyte;
21508 ptrdiff_t bytepos, charpos;
21509 const char *spec;
21510 Lisp_Object string;
21511
21512 bytepos = percent_position;
21513 charpos = (STRING_MULTIBYTE (elt)
21514 ? string_byte_to_char (elt, bytepos)
21515 : bytepos);
21516 spec = decode_mode_spec (it->w, c, field, &string);
21517 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21518
21519 switch (mode_line_target)
21520 {
21521 case MODE_LINE_NOPROP:
21522 case MODE_LINE_TITLE:
21523 n += store_mode_line_noprop (spec, field, prec);
21524 break;
21525 case MODE_LINE_STRING:
21526 {
21527 Lisp_Object tem = build_string (spec);
21528 props = Ftext_properties_at (make_number (charpos), elt);
21529 /* Should only keep face property in props */
21530 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21531 }
21532 break;
21533 case MODE_LINE_DISPLAY:
21534 {
21535 int nglyphs_before, nwritten;
21536
21537 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21538 nwritten = display_string (spec, string, elt,
21539 charpos, 0, it,
21540 field, prec, 0,
21541 multibyte);
21542
21543 /* Assign to the glyphs written above the
21544 string where the `%x' came from, position
21545 of the `%'. */
21546 if (nwritten > 0)
21547 {
21548 struct glyph *glyph
21549 = (it->glyph_row->glyphs[TEXT_AREA]
21550 + nglyphs_before);
21551 int i;
21552
21553 for (i = 0; i < nwritten; ++i)
21554 {
21555 glyph[i].object = elt;
21556 glyph[i].charpos = charpos;
21557 }
21558
21559 n += nwritten;
21560 }
21561 }
21562 break;
21563 }
21564 }
21565 else /* c == 0 */
21566 break;
21567 }
21568 }
21569 }
21570 break;
21571
21572 case Lisp_Symbol:
21573 /* A symbol: process the value of the symbol recursively
21574 as if it appeared here directly. Avoid error if symbol void.
21575 Special case: if value of symbol is a string, output the string
21576 literally. */
21577 {
21578 register Lisp_Object tem;
21579
21580 /* If the variable is not marked as risky to set
21581 then its contents are risky to use. */
21582 if (NILP (Fget (elt, Qrisky_local_variable)))
21583 risky = 1;
21584
21585 tem = Fboundp (elt);
21586 if (!NILP (tem))
21587 {
21588 tem = Fsymbol_value (elt);
21589 /* If value is a string, output that string literally:
21590 don't check for % within it. */
21591 if (STRINGP (tem))
21592 literal = 1;
21593
21594 if (!EQ (tem, elt))
21595 {
21596 /* Give up right away for nil or t. */
21597 elt = tem;
21598 goto tail_recurse;
21599 }
21600 }
21601 }
21602 break;
21603
21604 case Lisp_Cons:
21605 {
21606 register Lisp_Object car, tem;
21607
21608 /* A cons cell: five distinct cases.
21609 If first element is :eval or :propertize, do something special.
21610 If first element is a string or a cons, process all the elements
21611 and effectively concatenate them.
21612 If first element is a negative number, truncate displaying cdr to
21613 at most that many characters. If positive, pad (with spaces)
21614 to at least that many characters.
21615 If first element is a symbol, process the cadr or caddr recursively
21616 according to whether the symbol's value is non-nil or nil. */
21617 car = XCAR (elt);
21618 if (EQ (car, QCeval))
21619 {
21620 /* An element of the form (:eval FORM) means evaluate FORM
21621 and use the result as mode line elements. */
21622
21623 if (risky)
21624 break;
21625
21626 if (CONSP (XCDR (elt)))
21627 {
21628 Lisp_Object spec;
21629 spec = safe_eval (XCAR (XCDR (elt)));
21630 n += display_mode_element (it, depth, field_width - n,
21631 precision - n, spec, props,
21632 risky);
21633 }
21634 }
21635 else if (EQ (car, QCpropertize))
21636 {
21637 /* An element of the form (:propertize ELT PROPS...)
21638 means display ELT but applying properties PROPS. */
21639
21640 if (risky)
21641 break;
21642
21643 if (CONSP (XCDR (elt)))
21644 n += display_mode_element (it, depth, field_width - n,
21645 precision - n, XCAR (XCDR (elt)),
21646 XCDR (XCDR (elt)), risky);
21647 }
21648 else if (SYMBOLP (car))
21649 {
21650 tem = Fboundp (car);
21651 elt = XCDR (elt);
21652 if (!CONSP (elt))
21653 goto invalid;
21654 /* elt is now the cdr, and we know it is a cons cell.
21655 Use its car if CAR has a non-nil value. */
21656 if (!NILP (tem))
21657 {
21658 tem = Fsymbol_value (car);
21659 if (!NILP (tem))
21660 {
21661 elt = XCAR (elt);
21662 goto tail_recurse;
21663 }
21664 }
21665 /* Symbol's value is nil (or symbol is unbound)
21666 Get the cddr of the original list
21667 and if possible find the caddr and use that. */
21668 elt = XCDR (elt);
21669 if (NILP (elt))
21670 break;
21671 else if (!CONSP (elt))
21672 goto invalid;
21673 elt = XCAR (elt);
21674 goto tail_recurse;
21675 }
21676 else if (INTEGERP (car))
21677 {
21678 register int lim = XINT (car);
21679 elt = XCDR (elt);
21680 if (lim < 0)
21681 {
21682 /* Negative int means reduce maximum width. */
21683 if (precision <= 0)
21684 precision = -lim;
21685 else
21686 precision = min (precision, -lim);
21687 }
21688 else if (lim > 0)
21689 {
21690 /* Padding specified. Don't let it be more than
21691 current maximum. */
21692 if (precision > 0)
21693 lim = min (precision, lim);
21694
21695 /* If that's more padding than already wanted, queue it.
21696 But don't reduce padding already specified even if
21697 that is beyond the current truncation point. */
21698 field_width = max (lim, field_width);
21699 }
21700 goto tail_recurse;
21701 }
21702 else if (STRINGP (car) || CONSP (car))
21703 {
21704 Lisp_Object halftail = elt;
21705 int len = 0;
21706
21707 while (CONSP (elt)
21708 && (precision <= 0 || n < precision))
21709 {
21710 n += display_mode_element (it, depth,
21711 /* Do padding only after the last
21712 element in the list. */
21713 (! CONSP (XCDR (elt))
21714 ? field_width - n
21715 : 0),
21716 precision - n, XCAR (elt),
21717 props, risky);
21718 elt = XCDR (elt);
21719 len++;
21720 if ((len & 1) == 0)
21721 halftail = XCDR (halftail);
21722 /* Check for cycle. */
21723 if (EQ (halftail, elt))
21724 break;
21725 }
21726 }
21727 }
21728 break;
21729
21730 default:
21731 invalid:
21732 elt = build_string ("*invalid*");
21733 goto tail_recurse;
21734 }
21735
21736 /* Pad to FIELD_WIDTH. */
21737 if (field_width > 0 && n < field_width)
21738 {
21739 switch (mode_line_target)
21740 {
21741 case MODE_LINE_NOPROP:
21742 case MODE_LINE_TITLE:
21743 n += store_mode_line_noprop ("", field_width - n, 0);
21744 break;
21745 case MODE_LINE_STRING:
21746 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21747 break;
21748 case MODE_LINE_DISPLAY:
21749 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21750 0, 0, 0);
21751 break;
21752 }
21753 }
21754
21755 return n;
21756 }
21757
21758 /* Store a mode-line string element in mode_line_string_list.
21759
21760 If STRING is non-null, display that C string. Otherwise, the Lisp
21761 string LISP_STRING is displayed.
21762
21763 FIELD_WIDTH is the minimum number of output glyphs to produce.
21764 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21765 with spaces. FIELD_WIDTH <= 0 means don't pad.
21766
21767 PRECISION is the maximum number of characters to output from
21768 STRING. PRECISION <= 0 means don't truncate the string.
21769
21770 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21771 properties to the string.
21772
21773 PROPS are the properties to add to the string.
21774 The mode_line_string_face face property is always added to the string.
21775 */
21776
21777 static int
21778 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21779 int field_width, int precision, Lisp_Object props)
21780 {
21781 ptrdiff_t len;
21782 int n = 0;
21783
21784 if (string != NULL)
21785 {
21786 len = strlen (string);
21787 if (precision > 0 && len > precision)
21788 len = precision;
21789 lisp_string = make_string (string, len);
21790 if (NILP (props))
21791 props = mode_line_string_face_prop;
21792 else if (!NILP (mode_line_string_face))
21793 {
21794 Lisp_Object face = Fplist_get (props, Qface);
21795 props = Fcopy_sequence (props);
21796 if (NILP (face))
21797 face = mode_line_string_face;
21798 else
21799 face = list2 (face, mode_line_string_face);
21800 props = Fplist_put (props, Qface, face);
21801 }
21802 Fadd_text_properties (make_number (0), make_number (len),
21803 props, lisp_string);
21804 }
21805 else
21806 {
21807 len = XFASTINT (Flength (lisp_string));
21808 if (precision > 0 && len > precision)
21809 {
21810 len = precision;
21811 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21812 precision = -1;
21813 }
21814 if (!NILP (mode_line_string_face))
21815 {
21816 Lisp_Object face;
21817 if (NILP (props))
21818 props = Ftext_properties_at (make_number (0), lisp_string);
21819 face = Fplist_get (props, Qface);
21820 if (NILP (face))
21821 face = mode_line_string_face;
21822 else
21823 face = list2 (face, mode_line_string_face);
21824 props = list2 (Qface, face);
21825 if (copy_string)
21826 lisp_string = Fcopy_sequence (lisp_string);
21827 }
21828 if (!NILP (props))
21829 Fadd_text_properties (make_number (0), make_number (len),
21830 props, lisp_string);
21831 }
21832
21833 if (len > 0)
21834 {
21835 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21836 n += len;
21837 }
21838
21839 if (field_width > len)
21840 {
21841 field_width -= len;
21842 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21843 if (!NILP (props))
21844 Fadd_text_properties (make_number (0), make_number (field_width),
21845 props, lisp_string);
21846 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21847 n += field_width;
21848 }
21849
21850 return n;
21851 }
21852
21853
21854 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21855 1, 4, 0,
21856 doc: /* Format a string out of a mode line format specification.
21857 First arg FORMAT specifies the mode line format (see `mode-line-format'
21858 for details) to use.
21859
21860 By default, the format is evaluated for the currently selected window.
21861
21862 Optional second arg FACE specifies the face property to put on all
21863 characters for which no face is specified. The value nil means the
21864 default face. The value t means whatever face the window's mode line
21865 currently uses (either `mode-line' or `mode-line-inactive',
21866 depending on whether the window is the selected window or not).
21867 An integer value means the value string has no text
21868 properties.
21869
21870 Optional third and fourth args WINDOW and BUFFER specify the window
21871 and buffer to use as the context for the formatting (defaults
21872 are the selected window and the WINDOW's buffer). */)
21873 (Lisp_Object format, Lisp_Object face,
21874 Lisp_Object window, Lisp_Object buffer)
21875 {
21876 struct it it;
21877 int len;
21878 struct window *w;
21879 struct buffer *old_buffer = NULL;
21880 int face_id;
21881 int no_props = INTEGERP (face);
21882 ptrdiff_t count = SPECPDL_INDEX ();
21883 Lisp_Object str;
21884 int string_start = 0;
21885
21886 w = decode_any_window (window);
21887 XSETWINDOW (window, w);
21888
21889 if (NILP (buffer))
21890 buffer = w->contents;
21891 CHECK_BUFFER (buffer);
21892
21893 /* Make formatting the modeline a non-op when noninteractive, otherwise
21894 there will be problems later caused by a partially initialized frame. */
21895 if (NILP (format) || noninteractive)
21896 return empty_unibyte_string;
21897
21898 if (no_props)
21899 face = Qnil;
21900
21901 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21902 : EQ (face, Qt) ? (EQ (window, selected_window)
21903 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21904 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21905 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21906 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21907 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21908 : DEFAULT_FACE_ID;
21909
21910 old_buffer = current_buffer;
21911
21912 /* Save things including mode_line_proptrans_alist,
21913 and set that to nil so that we don't alter the outer value. */
21914 record_unwind_protect (unwind_format_mode_line,
21915 format_mode_line_unwind_data
21916 (XFRAME (WINDOW_FRAME (w)),
21917 old_buffer, selected_window, 1));
21918 mode_line_proptrans_alist = Qnil;
21919
21920 Fselect_window (window, Qt);
21921 set_buffer_internal_1 (XBUFFER (buffer));
21922
21923 init_iterator (&it, w, -1, -1, NULL, face_id);
21924
21925 if (no_props)
21926 {
21927 mode_line_target = MODE_LINE_NOPROP;
21928 mode_line_string_face_prop = Qnil;
21929 mode_line_string_list = Qnil;
21930 string_start = MODE_LINE_NOPROP_LEN (0);
21931 }
21932 else
21933 {
21934 mode_line_target = MODE_LINE_STRING;
21935 mode_line_string_list = Qnil;
21936 mode_line_string_face = face;
21937 mode_line_string_face_prop
21938 = NILP (face) ? Qnil : list2 (Qface, face);
21939 }
21940
21941 push_kboard (FRAME_KBOARD (it.f));
21942 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21943 pop_kboard ();
21944
21945 if (no_props)
21946 {
21947 len = MODE_LINE_NOPROP_LEN (string_start);
21948 str = make_string (mode_line_noprop_buf + string_start, len);
21949 }
21950 else
21951 {
21952 mode_line_string_list = Fnreverse (mode_line_string_list);
21953 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21954 empty_unibyte_string);
21955 }
21956
21957 unbind_to (count, Qnil);
21958 return str;
21959 }
21960
21961 /* Write a null-terminated, right justified decimal representation of
21962 the positive integer D to BUF using a minimal field width WIDTH. */
21963
21964 static void
21965 pint2str (register char *buf, register int width, register ptrdiff_t d)
21966 {
21967 register char *p = buf;
21968
21969 if (d <= 0)
21970 *p++ = '0';
21971 else
21972 {
21973 while (d > 0)
21974 {
21975 *p++ = d % 10 + '0';
21976 d /= 10;
21977 }
21978 }
21979
21980 for (width -= (int) (p - buf); width > 0; --width)
21981 *p++ = ' ';
21982 *p-- = '\0';
21983 while (p > buf)
21984 {
21985 d = *buf;
21986 *buf++ = *p;
21987 *p-- = d;
21988 }
21989 }
21990
21991 /* Write a null-terminated, right justified decimal and "human
21992 readable" representation of the nonnegative integer D to BUF using
21993 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21994
21995 static const char power_letter[] =
21996 {
21997 0, /* no letter */
21998 'k', /* kilo */
21999 'M', /* mega */
22000 'G', /* giga */
22001 'T', /* tera */
22002 'P', /* peta */
22003 'E', /* exa */
22004 'Z', /* zetta */
22005 'Y' /* yotta */
22006 };
22007
22008 static void
22009 pint2hrstr (char *buf, int width, ptrdiff_t d)
22010 {
22011 /* We aim to represent the nonnegative integer D as
22012 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22013 ptrdiff_t quotient = d;
22014 int remainder = 0;
22015 /* -1 means: do not use TENTHS. */
22016 int tenths = -1;
22017 int exponent = 0;
22018
22019 /* Length of QUOTIENT.TENTHS as a string. */
22020 int length;
22021
22022 char * psuffix;
22023 char * p;
22024
22025 if (quotient >= 1000)
22026 {
22027 /* Scale to the appropriate EXPONENT. */
22028 do
22029 {
22030 remainder = quotient % 1000;
22031 quotient /= 1000;
22032 exponent++;
22033 }
22034 while (quotient >= 1000);
22035
22036 /* Round to nearest and decide whether to use TENTHS or not. */
22037 if (quotient <= 9)
22038 {
22039 tenths = remainder / 100;
22040 if (remainder % 100 >= 50)
22041 {
22042 if (tenths < 9)
22043 tenths++;
22044 else
22045 {
22046 quotient++;
22047 if (quotient == 10)
22048 tenths = -1;
22049 else
22050 tenths = 0;
22051 }
22052 }
22053 }
22054 else
22055 if (remainder >= 500)
22056 {
22057 if (quotient < 999)
22058 quotient++;
22059 else
22060 {
22061 quotient = 1;
22062 exponent++;
22063 tenths = 0;
22064 }
22065 }
22066 }
22067
22068 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22069 if (tenths == -1 && quotient <= 99)
22070 if (quotient <= 9)
22071 length = 1;
22072 else
22073 length = 2;
22074 else
22075 length = 3;
22076 p = psuffix = buf + max (width, length);
22077
22078 /* Print EXPONENT. */
22079 *psuffix++ = power_letter[exponent];
22080 *psuffix = '\0';
22081
22082 /* Print TENTHS. */
22083 if (tenths >= 0)
22084 {
22085 *--p = '0' + tenths;
22086 *--p = '.';
22087 }
22088
22089 /* Print QUOTIENT. */
22090 do
22091 {
22092 int digit = quotient % 10;
22093 *--p = '0' + digit;
22094 }
22095 while ((quotient /= 10) != 0);
22096
22097 /* Print leading spaces. */
22098 while (buf < p)
22099 *--p = ' ';
22100 }
22101
22102 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22103 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22104 type of CODING_SYSTEM. Return updated pointer into BUF. */
22105
22106 static unsigned char invalid_eol_type[] = "(*invalid*)";
22107
22108 static char *
22109 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22110 {
22111 Lisp_Object val;
22112 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22113 const unsigned char *eol_str;
22114 int eol_str_len;
22115 /* The EOL conversion we are using. */
22116 Lisp_Object eoltype;
22117
22118 val = CODING_SYSTEM_SPEC (coding_system);
22119 eoltype = Qnil;
22120
22121 if (!VECTORP (val)) /* Not yet decided. */
22122 {
22123 *buf++ = multibyte ? '-' : ' ';
22124 if (eol_flag)
22125 eoltype = eol_mnemonic_undecided;
22126 /* Don't mention EOL conversion if it isn't decided. */
22127 }
22128 else
22129 {
22130 Lisp_Object attrs;
22131 Lisp_Object eolvalue;
22132
22133 attrs = AREF (val, 0);
22134 eolvalue = AREF (val, 2);
22135
22136 *buf++ = multibyte
22137 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22138 : ' ';
22139
22140 if (eol_flag)
22141 {
22142 /* The EOL conversion that is normal on this system. */
22143
22144 if (NILP (eolvalue)) /* Not yet decided. */
22145 eoltype = eol_mnemonic_undecided;
22146 else if (VECTORP (eolvalue)) /* Not yet decided. */
22147 eoltype = eol_mnemonic_undecided;
22148 else /* eolvalue is Qunix, Qdos, or Qmac. */
22149 eoltype = (EQ (eolvalue, Qunix)
22150 ? eol_mnemonic_unix
22151 : (EQ (eolvalue, Qdos) == 1
22152 ? eol_mnemonic_dos : eol_mnemonic_mac));
22153 }
22154 }
22155
22156 if (eol_flag)
22157 {
22158 /* Mention the EOL conversion if it is not the usual one. */
22159 if (STRINGP (eoltype))
22160 {
22161 eol_str = SDATA (eoltype);
22162 eol_str_len = SBYTES (eoltype);
22163 }
22164 else if (CHARACTERP (eoltype))
22165 {
22166 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22167 int c = XFASTINT (eoltype);
22168 eol_str_len = CHAR_STRING (c, tmp);
22169 eol_str = tmp;
22170 }
22171 else
22172 {
22173 eol_str = invalid_eol_type;
22174 eol_str_len = sizeof (invalid_eol_type) - 1;
22175 }
22176 memcpy (buf, eol_str, eol_str_len);
22177 buf += eol_str_len;
22178 }
22179
22180 return buf;
22181 }
22182
22183 /* Return a string for the output of a mode line %-spec for window W,
22184 generated by character C. FIELD_WIDTH > 0 means pad the string
22185 returned with spaces to that value. Return a Lisp string in
22186 *STRING if the resulting string is taken from that Lisp string.
22187
22188 Note we operate on the current buffer for most purposes. */
22189
22190 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22191
22192 static const char *
22193 decode_mode_spec (struct window *w, register int c, int field_width,
22194 Lisp_Object *string)
22195 {
22196 Lisp_Object obj;
22197 struct frame *f = XFRAME (WINDOW_FRAME (w));
22198 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22199 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22200 produce strings from numerical values, so limit preposterously
22201 large values of FIELD_WIDTH to avoid overrunning the buffer's
22202 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22203 bytes plus the terminating null. */
22204 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22205 struct buffer *b = current_buffer;
22206
22207 obj = Qnil;
22208 *string = Qnil;
22209
22210 switch (c)
22211 {
22212 case '*':
22213 if (!NILP (BVAR (b, read_only)))
22214 return "%";
22215 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22216 return "*";
22217 return "-";
22218
22219 case '+':
22220 /* This differs from %* only for a modified read-only buffer. */
22221 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22222 return "*";
22223 if (!NILP (BVAR (b, read_only)))
22224 return "%";
22225 return "-";
22226
22227 case '&':
22228 /* This differs from %* in ignoring read-only-ness. */
22229 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22230 return "*";
22231 return "-";
22232
22233 case '%':
22234 return "%";
22235
22236 case '[':
22237 {
22238 int i;
22239 char *p;
22240
22241 if (command_loop_level > 5)
22242 return "[[[... ";
22243 p = decode_mode_spec_buf;
22244 for (i = 0; i < command_loop_level; i++)
22245 *p++ = '[';
22246 *p = 0;
22247 return decode_mode_spec_buf;
22248 }
22249
22250 case ']':
22251 {
22252 int i;
22253 char *p;
22254
22255 if (command_loop_level > 5)
22256 return " ...]]]";
22257 p = decode_mode_spec_buf;
22258 for (i = 0; i < command_loop_level; i++)
22259 *p++ = ']';
22260 *p = 0;
22261 return decode_mode_spec_buf;
22262 }
22263
22264 case '-':
22265 {
22266 register int i;
22267
22268 /* Let lots_of_dashes be a string of infinite length. */
22269 if (mode_line_target == MODE_LINE_NOPROP
22270 || mode_line_target == MODE_LINE_STRING)
22271 return "--";
22272 if (field_width <= 0
22273 || field_width > sizeof (lots_of_dashes))
22274 {
22275 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22276 decode_mode_spec_buf[i] = '-';
22277 decode_mode_spec_buf[i] = '\0';
22278 return decode_mode_spec_buf;
22279 }
22280 else
22281 return lots_of_dashes;
22282 }
22283
22284 case 'b':
22285 obj = BVAR (b, name);
22286 break;
22287
22288 case 'c':
22289 /* %c and %l are ignored in `frame-title-format'.
22290 (In redisplay_internal, the frame title is drawn _before_ the
22291 windows are updated, so the stuff which depends on actual
22292 window contents (such as %l) may fail to render properly, or
22293 even crash emacs.) */
22294 if (mode_line_target == MODE_LINE_TITLE)
22295 return "";
22296 else
22297 {
22298 ptrdiff_t col = current_column ();
22299 w->column_number_displayed = col;
22300 pint2str (decode_mode_spec_buf, width, col);
22301 return decode_mode_spec_buf;
22302 }
22303
22304 case 'e':
22305 #ifndef SYSTEM_MALLOC
22306 {
22307 if (NILP (Vmemory_full))
22308 return "";
22309 else
22310 return "!MEM FULL! ";
22311 }
22312 #else
22313 return "";
22314 #endif
22315
22316 case 'F':
22317 /* %F displays the frame name. */
22318 if (!NILP (f->title))
22319 return SSDATA (f->title);
22320 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22321 return SSDATA (f->name);
22322 return "Emacs";
22323
22324 case 'f':
22325 obj = BVAR (b, filename);
22326 break;
22327
22328 case 'i':
22329 {
22330 ptrdiff_t size = ZV - BEGV;
22331 pint2str (decode_mode_spec_buf, width, size);
22332 return decode_mode_spec_buf;
22333 }
22334
22335 case 'I':
22336 {
22337 ptrdiff_t size = ZV - BEGV;
22338 pint2hrstr (decode_mode_spec_buf, width, size);
22339 return decode_mode_spec_buf;
22340 }
22341
22342 case 'l':
22343 {
22344 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22345 ptrdiff_t topline, nlines, height;
22346 ptrdiff_t junk;
22347
22348 /* %c and %l are ignored in `frame-title-format'. */
22349 if (mode_line_target == MODE_LINE_TITLE)
22350 return "";
22351
22352 startpos = marker_position (w->start);
22353 startpos_byte = marker_byte_position (w->start);
22354 height = WINDOW_TOTAL_LINES (w);
22355
22356 /* If we decided that this buffer isn't suitable for line numbers,
22357 don't forget that too fast. */
22358 if (w->base_line_pos == -1)
22359 goto no_value;
22360
22361 /* If the buffer is very big, don't waste time. */
22362 if (INTEGERP (Vline_number_display_limit)
22363 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22364 {
22365 w->base_line_pos = 0;
22366 w->base_line_number = 0;
22367 goto no_value;
22368 }
22369
22370 if (w->base_line_number > 0
22371 && w->base_line_pos > 0
22372 && w->base_line_pos <= startpos)
22373 {
22374 line = w->base_line_number;
22375 linepos = w->base_line_pos;
22376 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22377 }
22378 else
22379 {
22380 line = 1;
22381 linepos = BUF_BEGV (b);
22382 linepos_byte = BUF_BEGV_BYTE (b);
22383 }
22384
22385 /* Count lines from base line to window start position. */
22386 nlines = display_count_lines (linepos_byte,
22387 startpos_byte,
22388 startpos, &junk);
22389
22390 topline = nlines + line;
22391
22392 /* Determine a new base line, if the old one is too close
22393 or too far away, or if we did not have one.
22394 "Too close" means it's plausible a scroll-down would
22395 go back past it. */
22396 if (startpos == BUF_BEGV (b))
22397 {
22398 w->base_line_number = topline;
22399 w->base_line_pos = BUF_BEGV (b);
22400 }
22401 else if (nlines < height + 25 || nlines > height * 3 + 50
22402 || linepos == BUF_BEGV (b))
22403 {
22404 ptrdiff_t limit = BUF_BEGV (b);
22405 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22406 ptrdiff_t position;
22407 ptrdiff_t distance =
22408 (height * 2 + 30) * line_number_display_limit_width;
22409
22410 if (startpos - distance > limit)
22411 {
22412 limit = startpos - distance;
22413 limit_byte = CHAR_TO_BYTE (limit);
22414 }
22415
22416 nlines = display_count_lines (startpos_byte,
22417 limit_byte,
22418 - (height * 2 + 30),
22419 &position);
22420 /* If we couldn't find the lines we wanted within
22421 line_number_display_limit_width chars per line,
22422 give up on line numbers for this window. */
22423 if (position == limit_byte && limit == startpos - distance)
22424 {
22425 w->base_line_pos = -1;
22426 w->base_line_number = 0;
22427 goto no_value;
22428 }
22429
22430 w->base_line_number = topline - nlines;
22431 w->base_line_pos = BYTE_TO_CHAR (position);
22432 }
22433
22434 /* Now count lines from the start pos to point. */
22435 nlines = display_count_lines (startpos_byte,
22436 PT_BYTE, PT, &junk);
22437
22438 /* Record that we did display the line number. */
22439 line_number_displayed = 1;
22440
22441 /* Make the string to show. */
22442 pint2str (decode_mode_spec_buf, width, topline + nlines);
22443 return decode_mode_spec_buf;
22444 no_value:
22445 {
22446 char* p = decode_mode_spec_buf;
22447 int pad = width - 2;
22448 while (pad-- > 0)
22449 *p++ = ' ';
22450 *p++ = '?';
22451 *p++ = '?';
22452 *p = '\0';
22453 return decode_mode_spec_buf;
22454 }
22455 }
22456 break;
22457
22458 case 'm':
22459 obj = BVAR (b, mode_name);
22460 break;
22461
22462 case 'n':
22463 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22464 return " Narrow";
22465 break;
22466
22467 case 'p':
22468 {
22469 ptrdiff_t pos = marker_position (w->start);
22470 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22471
22472 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22473 {
22474 if (pos <= BUF_BEGV (b))
22475 return "All";
22476 else
22477 return "Bottom";
22478 }
22479 else if (pos <= BUF_BEGV (b))
22480 return "Top";
22481 else
22482 {
22483 if (total > 1000000)
22484 /* Do it differently for a large value, to avoid overflow. */
22485 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22486 else
22487 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22488 /* We can't normally display a 3-digit number,
22489 so get us a 2-digit number that is close. */
22490 if (total == 100)
22491 total = 99;
22492 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22493 return decode_mode_spec_buf;
22494 }
22495 }
22496
22497 /* Display percentage of size above the bottom of the screen. */
22498 case 'P':
22499 {
22500 ptrdiff_t toppos = marker_position (w->start);
22501 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22502 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22503
22504 if (botpos >= BUF_ZV (b))
22505 {
22506 if (toppos <= BUF_BEGV (b))
22507 return "All";
22508 else
22509 return "Bottom";
22510 }
22511 else
22512 {
22513 if (total > 1000000)
22514 /* Do it differently for a large value, to avoid overflow. */
22515 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22516 else
22517 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22518 /* We can't normally display a 3-digit number,
22519 so get us a 2-digit number that is close. */
22520 if (total == 100)
22521 total = 99;
22522 if (toppos <= BUF_BEGV (b))
22523 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22524 else
22525 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22526 return decode_mode_spec_buf;
22527 }
22528 }
22529
22530 case 's':
22531 /* status of process */
22532 obj = Fget_buffer_process (Fcurrent_buffer ());
22533 if (NILP (obj))
22534 return "no process";
22535 #ifndef MSDOS
22536 obj = Fsymbol_name (Fprocess_status (obj));
22537 #endif
22538 break;
22539
22540 case '@':
22541 {
22542 ptrdiff_t count = inhibit_garbage_collection ();
22543 Lisp_Object val = call1 (intern ("file-remote-p"),
22544 BVAR (current_buffer, directory));
22545 unbind_to (count, Qnil);
22546
22547 if (NILP (val))
22548 return "-";
22549 else
22550 return "@";
22551 }
22552
22553 case 'z':
22554 /* coding-system (not including end-of-line format) */
22555 case 'Z':
22556 /* coding-system (including end-of-line type) */
22557 {
22558 int eol_flag = (c == 'Z');
22559 char *p = decode_mode_spec_buf;
22560
22561 if (! FRAME_WINDOW_P (f))
22562 {
22563 /* No need to mention EOL here--the terminal never needs
22564 to do EOL conversion. */
22565 p = decode_mode_spec_coding (CODING_ID_NAME
22566 (FRAME_KEYBOARD_CODING (f)->id),
22567 p, 0);
22568 p = decode_mode_spec_coding (CODING_ID_NAME
22569 (FRAME_TERMINAL_CODING (f)->id),
22570 p, 0);
22571 }
22572 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22573 p, eol_flag);
22574
22575 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22576 #ifdef subprocesses
22577 obj = Fget_buffer_process (Fcurrent_buffer ());
22578 if (PROCESSP (obj))
22579 {
22580 p = decode_mode_spec_coding
22581 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22582 p = decode_mode_spec_coding
22583 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22584 }
22585 #endif /* subprocesses */
22586 #endif /* 0 */
22587 *p = 0;
22588 return decode_mode_spec_buf;
22589 }
22590 }
22591
22592 if (STRINGP (obj))
22593 {
22594 *string = obj;
22595 return SSDATA (obj);
22596 }
22597 else
22598 return "";
22599 }
22600
22601
22602 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22603 means count lines back from START_BYTE. But don't go beyond
22604 LIMIT_BYTE. Return the number of lines thus found (always
22605 nonnegative).
22606
22607 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22608 either the position COUNT lines after/before START_BYTE, if we
22609 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22610 COUNT lines. */
22611
22612 static ptrdiff_t
22613 display_count_lines (ptrdiff_t start_byte,
22614 ptrdiff_t limit_byte, ptrdiff_t count,
22615 ptrdiff_t *byte_pos_ptr)
22616 {
22617 register unsigned char *cursor;
22618 unsigned char *base;
22619
22620 register ptrdiff_t ceiling;
22621 register unsigned char *ceiling_addr;
22622 ptrdiff_t orig_count = count;
22623
22624 /* If we are not in selective display mode,
22625 check only for newlines. */
22626 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22627 && !INTEGERP (BVAR (current_buffer, selective_display)));
22628
22629 if (count > 0)
22630 {
22631 while (start_byte < limit_byte)
22632 {
22633 ceiling = BUFFER_CEILING_OF (start_byte);
22634 ceiling = min (limit_byte - 1, ceiling);
22635 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22636 base = (cursor = BYTE_POS_ADDR (start_byte));
22637
22638 do
22639 {
22640 if (selective_display)
22641 {
22642 while (*cursor != '\n' && *cursor != 015
22643 && ++cursor != ceiling_addr)
22644 continue;
22645 if (cursor == ceiling_addr)
22646 break;
22647 }
22648 else
22649 {
22650 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22651 if (! cursor)
22652 break;
22653 }
22654
22655 cursor++;
22656
22657 if (--count == 0)
22658 {
22659 start_byte += cursor - base;
22660 *byte_pos_ptr = start_byte;
22661 return orig_count;
22662 }
22663 }
22664 while (cursor < ceiling_addr);
22665
22666 start_byte += ceiling_addr - base;
22667 }
22668 }
22669 else
22670 {
22671 while (start_byte > limit_byte)
22672 {
22673 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22674 ceiling = max (limit_byte, ceiling);
22675 ceiling_addr = BYTE_POS_ADDR (ceiling);
22676 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22677 while (1)
22678 {
22679 if (selective_display)
22680 {
22681 while (--cursor >= ceiling_addr
22682 && *cursor != '\n' && *cursor != 015)
22683 continue;
22684 if (cursor < ceiling_addr)
22685 break;
22686 }
22687 else
22688 {
22689 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22690 if (! cursor)
22691 break;
22692 }
22693
22694 if (++count == 0)
22695 {
22696 start_byte += cursor - base + 1;
22697 *byte_pos_ptr = start_byte;
22698 /* When scanning backwards, we should
22699 not count the newline posterior to which we stop. */
22700 return - orig_count - 1;
22701 }
22702 }
22703 start_byte += ceiling_addr - base;
22704 }
22705 }
22706
22707 *byte_pos_ptr = limit_byte;
22708
22709 if (count < 0)
22710 return - orig_count + count;
22711 return orig_count - count;
22712
22713 }
22714
22715
22716 \f
22717 /***********************************************************************
22718 Displaying strings
22719 ***********************************************************************/
22720
22721 /* Display a NUL-terminated string, starting with index START.
22722
22723 If STRING is non-null, display that C string. Otherwise, the Lisp
22724 string LISP_STRING is displayed. There's a case that STRING is
22725 non-null and LISP_STRING is not nil. It means STRING is a string
22726 data of LISP_STRING. In that case, we display LISP_STRING while
22727 ignoring its text properties.
22728
22729 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22730 FACE_STRING. Display STRING or LISP_STRING with the face at
22731 FACE_STRING_POS in FACE_STRING:
22732
22733 Display the string in the environment given by IT, but use the
22734 standard display table, temporarily.
22735
22736 FIELD_WIDTH is the minimum number of output glyphs to produce.
22737 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22738 with spaces. If STRING has more characters, more than FIELD_WIDTH
22739 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22740
22741 PRECISION is the maximum number of characters to output from
22742 STRING. PRECISION < 0 means don't truncate the string.
22743
22744 This is roughly equivalent to printf format specifiers:
22745
22746 FIELD_WIDTH PRECISION PRINTF
22747 ----------------------------------------
22748 -1 -1 %s
22749 -1 10 %.10s
22750 10 -1 %10s
22751 20 10 %20.10s
22752
22753 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22754 display them, and < 0 means obey the current buffer's value of
22755 enable_multibyte_characters.
22756
22757 Value is the number of columns displayed. */
22758
22759 static int
22760 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22761 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22762 int field_width, int precision, int max_x, int multibyte)
22763 {
22764 int hpos_at_start = it->hpos;
22765 int saved_face_id = it->face_id;
22766 struct glyph_row *row = it->glyph_row;
22767 ptrdiff_t it_charpos;
22768
22769 /* Initialize the iterator IT for iteration over STRING beginning
22770 with index START. */
22771 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22772 precision, field_width, multibyte);
22773 if (string && STRINGP (lisp_string))
22774 /* LISP_STRING is the one returned by decode_mode_spec. We should
22775 ignore its text properties. */
22776 it->stop_charpos = it->end_charpos;
22777
22778 /* If displaying STRING, set up the face of the iterator from
22779 FACE_STRING, if that's given. */
22780 if (STRINGP (face_string))
22781 {
22782 ptrdiff_t endptr;
22783 struct face *face;
22784
22785 it->face_id
22786 = face_at_string_position (it->w, face_string, face_string_pos,
22787 0, &endptr, it->base_face_id, 0);
22788 face = FACE_FROM_ID (it->f, it->face_id);
22789 it->face_box_p = face->box != FACE_NO_BOX;
22790 }
22791
22792 /* Set max_x to the maximum allowed X position. Don't let it go
22793 beyond the right edge of the window. */
22794 if (max_x <= 0)
22795 max_x = it->last_visible_x;
22796 else
22797 max_x = min (max_x, it->last_visible_x);
22798
22799 /* Skip over display elements that are not visible. because IT->w is
22800 hscrolled. */
22801 if (it->current_x < it->first_visible_x)
22802 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22803 MOVE_TO_POS | MOVE_TO_X);
22804
22805 row->ascent = it->max_ascent;
22806 row->height = it->max_ascent + it->max_descent;
22807 row->phys_ascent = it->max_phys_ascent;
22808 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22809 row->extra_line_spacing = it->max_extra_line_spacing;
22810
22811 if (STRINGP (it->string))
22812 it_charpos = IT_STRING_CHARPOS (*it);
22813 else
22814 it_charpos = IT_CHARPOS (*it);
22815
22816 /* This condition is for the case that we are called with current_x
22817 past last_visible_x. */
22818 while (it->current_x < max_x)
22819 {
22820 int x_before, x, n_glyphs_before, i, nglyphs;
22821
22822 /* Get the next display element. */
22823 if (!get_next_display_element (it))
22824 break;
22825
22826 /* Produce glyphs. */
22827 x_before = it->current_x;
22828 n_glyphs_before = row->used[TEXT_AREA];
22829 PRODUCE_GLYPHS (it);
22830
22831 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22832 i = 0;
22833 x = x_before;
22834 while (i < nglyphs)
22835 {
22836 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22837
22838 if (it->line_wrap != TRUNCATE
22839 && x + glyph->pixel_width > max_x)
22840 {
22841 /* End of continued line or max_x reached. */
22842 if (CHAR_GLYPH_PADDING_P (*glyph))
22843 {
22844 /* A wide character is unbreakable. */
22845 if (row->reversed_p)
22846 unproduce_glyphs (it, row->used[TEXT_AREA]
22847 - n_glyphs_before);
22848 row->used[TEXT_AREA] = n_glyphs_before;
22849 it->current_x = x_before;
22850 }
22851 else
22852 {
22853 if (row->reversed_p)
22854 unproduce_glyphs (it, row->used[TEXT_AREA]
22855 - (n_glyphs_before + i));
22856 row->used[TEXT_AREA] = n_glyphs_before + i;
22857 it->current_x = x;
22858 }
22859 break;
22860 }
22861 else if (x + glyph->pixel_width >= it->first_visible_x)
22862 {
22863 /* Glyph is at least partially visible. */
22864 ++it->hpos;
22865 if (x < it->first_visible_x)
22866 row->x = x - it->first_visible_x;
22867 }
22868 else
22869 {
22870 /* Glyph is off the left margin of the display area.
22871 Should not happen. */
22872 emacs_abort ();
22873 }
22874
22875 row->ascent = max (row->ascent, it->max_ascent);
22876 row->height = max (row->height, it->max_ascent + it->max_descent);
22877 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22878 row->phys_height = max (row->phys_height,
22879 it->max_phys_ascent + it->max_phys_descent);
22880 row->extra_line_spacing = max (row->extra_line_spacing,
22881 it->max_extra_line_spacing);
22882 x += glyph->pixel_width;
22883 ++i;
22884 }
22885
22886 /* Stop if max_x reached. */
22887 if (i < nglyphs)
22888 break;
22889
22890 /* Stop at line ends. */
22891 if (ITERATOR_AT_END_OF_LINE_P (it))
22892 {
22893 it->continuation_lines_width = 0;
22894 break;
22895 }
22896
22897 set_iterator_to_next (it, 1);
22898 if (STRINGP (it->string))
22899 it_charpos = IT_STRING_CHARPOS (*it);
22900 else
22901 it_charpos = IT_CHARPOS (*it);
22902
22903 /* Stop if truncating at the right edge. */
22904 if (it->line_wrap == TRUNCATE
22905 && it->current_x >= it->last_visible_x)
22906 {
22907 /* Add truncation mark, but don't do it if the line is
22908 truncated at a padding space. */
22909 if (it_charpos < it->string_nchars)
22910 {
22911 if (!FRAME_WINDOW_P (it->f))
22912 {
22913 int ii, n;
22914
22915 if (it->current_x > it->last_visible_x)
22916 {
22917 if (!row->reversed_p)
22918 {
22919 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22920 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22921 break;
22922 }
22923 else
22924 {
22925 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22926 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22927 break;
22928 unproduce_glyphs (it, ii + 1);
22929 ii = row->used[TEXT_AREA] - (ii + 1);
22930 }
22931 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22932 {
22933 row->used[TEXT_AREA] = ii;
22934 produce_special_glyphs (it, IT_TRUNCATION);
22935 }
22936 }
22937 produce_special_glyphs (it, IT_TRUNCATION);
22938 }
22939 row->truncated_on_right_p = 1;
22940 }
22941 break;
22942 }
22943 }
22944
22945 /* Maybe insert a truncation at the left. */
22946 if (it->first_visible_x
22947 && it_charpos > 0)
22948 {
22949 if (!FRAME_WINDOW_P (it->f)
22950 || (row->reversed_p
22951 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22952 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22953 insert_left_trunc_glyphs (it);
22954 row->truncated_on_left_p = 1;
22955 }
22956
22957 it->face_id = saved_face_id;
22958
22959 /* Value is number of columns displayed. */
22960 return it->hpos - hpos_at_start;
22961 }
22962
22963
22964 \f
22965 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22966 appears as an element of LIST or as the car of an element of LIST.
22967 If PROPVAL is a list, compare each element against LIST in that
22968 way, and return 1/2 if any element of PROPVAL is found in LIST.
22969 Otherwise return 0. This function cannot quit.
22970 The return value is 2 if the text is invisible but with an ellipsis
22971 and 1 if it's invisible and without an ellipsis. */
22972
22973 int
22974 invisible_p (register Lisp_Object propval, Lisp_Object list)
22975 {
22976 register Lisp_Object tail, proptail;
22977
22978 for (tail = list; CONSP (tail); tail = XCDR (tail))
22979 {
22980 register Lisp_Object tem;
22981 tem = XCAR (tail);
22982 if (EQ (propval, tem))
22983 return 1;
22984 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22985 return NILP (XCDR (tem)) ? 1 : 2;
22986 }
22987
22988 if (CONSP (propval))
22989 {
22990 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22991 {
22992 Lisp_Object propelt;
22993 propelt = XCAR (proptail);
22994 for (tail = list; CONSP (tail); tail = XCDR (tail))
22995 {
22996 register Lisp_Object tem;
22997 tem = XCAR (tail);
22998 if (EQ (propelt, tem))
22999 return 1;
23000 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23001 return NILP (XCDR (tem)) ? 1 : 2;
23002 }
23003 }
23004 }
23005
23006 return 0;
23007 }
23008
23009 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23010 doc: /* Non-nil if the property makes the text invisible.
23011 POS-OR-PROP can be a marker or number, in which case it is taken to be
23012 a position in the current buffer and the value of the `invisible' property
23013 is checked; or it can be some other value, which is then presumed to be the
23014 value of the `invisible' property of the text of interest.
23015 The non-nil value returned can be t for truly invisible text or something
23016 else if the text is replaced by an ellipsis. */)
23017 (Lisp_Object pos_or_prop)
23018 {
23019 Lisp_Object prop
23020 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23021 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23022 : pos_or_prop);
23023 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23024 return (invis == 0 ? Qnil
23025 : invis == 1 ? Qt
23026 : make_number (invis));
23027 }
23028
23029 /* Calculate a width or height in pixels from a specification using
23030 the following elements:
23031
23032 SPEC ::=
23033 NUM - a (fractional) multiple of the default font width/height
23034 (NUM) - specifies exactly NUM pixels
23035 UNIT - a fixed number of pixels, see below.
23036 ELEMENT - size of a display element in pixels, see below.
23037 (NUM . SPEC) - equals NUM * SPEC
23038 (+ SPEC SPEC ...) - add pixel values
23039 (- SPEC SPEC ...) - subtract pixel values
23040 (- SPEC) - negate pixel value
23041
23042 NUM ::=
23043 INT or FLOAT - a number constant
23044 SYMBOL - use symbol's (buffer local) variable binding.
23045
23046 UNIT ::=
23047 in - pixels per inch *)
23048 mm - pixels per 1/1000 meter *)
23049 cm - pixels per 1/100 meter *)
23050 width - width of current font in pixels.
23051 height - height of current font in pixels.
23052
23053 *) using the ratio(s) defined in display-pixels-per-inch.
23054
23055 ELEMENT ::=
23056
23057 left-fringe - left fringe width in pixels
23058 right-fringe - right fringe width in pixels
23059
23060 left-margin - left margin width in pixels
23061 right-margin - right margin width in pixels
23062
23063 scroll-bar - scroll-bar area width in pixels
23064
23065 Examples:
23066
23067 Pixels corresponding to 5 inches:
23068 (5 . in)
23069
23070 Total width of non-text areas on left side of window (if scroll-bar is on left):
23071 '(space :width (+ left-fringe left-margin scroll-bar))
23072
23073 Align to first text column (in header line):
23074 '(space :align-to 0)
23075
23076 Align to middle of text area minus half the width of variable `my-image'
23077 containing a loaded image:
23078 '(space :align-to (0.5 . (- text my-image)))
23079
23080 Width of left margin minus width of 1 character in the default font:
23081 '(space :width (- left-margin 1))
23082
23083 Width of left margin minus width of 2 characters in the current font:
23084 '(space :width (- left-margin (2 . width)))
23085
23086 Center 1 character over left-margin (in header line):
23087 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23088
23089 Different ways to express width of left fringe plus left margin minus one pixel:
23090 '(space :width (- (+ left-fringe left-margin) (1)))
23091 '(space :width (+ left-fringe left-margin (- (1))))
23092 '(space :width (+ left-fringe left-margin (-1)))
23093
23094 */
23095
23096 static int
23097 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23098 struct font *font, int width_p, int *align_to)
23099 {
23100 double pixels;
23101
23102 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23103 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23104
23105 if (NILP (prop))
23106 return OK_PIXELS (0);
23107
23108 eassert (FRAME_LIVE_P (it->f));
23109
23110 if (SYMBOLP (prop))
23111 {
23112 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23113 {
23114 char *unit = SSDATA (SYMBOL_NAME (prop));
23115
23116 if (unit[0] == 'i' && unit[1] == 'n')
23117 pixels = 1.0;
23118 else if (unit[0] == 'm' && unit[1] == 'm')
23119 pixels = 25.4;
23120 else if (unit[0] == 'c' && unit[1] == 'm')
23121 pixels = 2.54;
23122 else
23123 pixels = 0;
23124 if (pixels > 0)
23125 {
23126 double ppi = (width_p ? FRAME_RES_X (it->f)
23127 : FRAME_RES_Y (it->f));
23128
23129 if (ppi > 0)
23130 return OK_PIXELS (ppi / pixels);
23131 return 0;
23132 }
23133 }
23134
23135 #ifdef HAVE_WINDOW_SYSTEM
23136 if (EQ (prop, Qheight))
23137 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23138 if (EQ (prop, Qwidth))
23139 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23140 #else
23141 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23142 return OK_PIXELS (1);
23143 #endif
23144
23145 if (EQ (prop, Qtext))
23146 return OK_PIXELS (width_p
23147 ? window_box_width (it->w, TEXT_AREA)
23148 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23149
23150 if (align_to && *align_to < 0)
23151 {
23152 *res = 0;
23153 if (EQ (prop, Qleft))
23154 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23155 if (EQ (prop, Qright))
23156 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23157 if (EQ (prop, Qcenter))
23158 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23159 + window_box_width (it->w, TEXT_AREA) / 2);
23160 if (EQ (prop, Qleft_fringe))
23161 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23162 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23163 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23164 if (EQ (prop, Qright_fringe))
23165 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23166 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23167 : window_box_right_offset (it->w, TEXT_AREA));
23168 if (EQ (prop, Qleft_margin))
23169 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23170 if (EQ (prop, Qright_margin))
23171 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23172 if (EQ (prop, Qscroll_bar))
23173 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23174 ? 0
23175 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23176 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23177 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23178 : 0)));
23179 }
23180 else
23181 {
23182 if (EQ (prop, Qleft_fringe))
23183 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23184 if (EQ (prop, Qright_fringe))
23185 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23186 if (EQ (prop, Qleft_margin))
23187 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23188 if (EQ (prop, Qright_margin))
23189 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23190 if (EQ (prop, Qscroll_bar))
23191 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23192 }
23193
23194 prop = buffer_local_value_1 (prop, it->w->contents);
23195 if (EQ (prop, Qunbound))
23196 prop = Qnil;
23197 }
23198
23199 if (INTEGERP (prop) || FLOATP (prop))
23200 {
23201 int base_unit = (width_p
23202 ? FRAME_COLUMN_WIDTH (it->f)
23203 : FRAME_LINE_HEIGHT (it->f));
23204 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23205 }
23206
23207 if (CONSP (prop))
23208 {
23209 Lisp_Object car = XCAR (prop);
23210 Lisp_Object cdr = XCDR (prop);
23211
23212 if (SYMBOLP (car))
23213 {
23214 #ifdef HAVE_WINDOW_SYSTEM
23215 if (FRAME_WINDOW_P (it->f)
23216 && valid_image_p (prop))
23217 {
23218 ptrdiff_t id = lookup_image (it->f, prop);
23219 struct image *img = IMAGE_FROM_ID (it->f, id);
23220
23221 return OK_PIXELS (width_p ? img->width : img->height);
23222 }
23223 #endif
23224 if (EQ (car, Qplus) || EQ (car, Qminus))
23225 {
23226 int first = 1;
23227 double px;
23228
23229 pixels = 0;
23230 while (CONSP (cdr))
23231 {
23232 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23233 font, width_p, align_to))
23234 return 0;
23235 if (first)
23236 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23237 else
23238 pixels += px;
23239 cdr = XCDR (cdr);
23240 }
23241 if (EQ (car, Qminus))
23242 pixels = -pixels;
23243 return OK_PIXELS (pixels);
23244 }
23245
23246 car = buffer_local_value_1 (car, it->w->contents);
23247 if (EQ (car, Qunbound))
23248 car = Qnil;
23249 }
23250
23251 if (INTEGERP (car) || FLOATP (car))
23252 {
23253 double fact;
23254 pixels = XFLOATINT (car);
23255 if (NILP (cdr))
23256 return OK_PIXELS (pixels);
23257 if (calc_pixel_width_or_height (&fact, it, cdr,
23258 font, width_p, align_to))
23259 return OK_PIXELS (pixels * fact);
23260 return 0;
23261 }
23262
23263 return 0;
23264 }
23265
23266 return 0;
23267 }
23268
23269 \f
23270 /***********************************************************************
23271 Glyph Display
23272 ***********************************************************************/
23273
23274 #ifdef HAVE_WINDOW_SYSTEM
23275
23276 #ifdef GLYPH_DEBUG
23277
23278 void
23279 dump_glyph_string (struct glyph_string *s)
23280 {
23281 fprintf (stderr, "glyph string\n");
23282 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23283 s->x, s->y, s->width, s->height);
23284 fprintf (stderr, " ybase = %d\n", s->ybase);
23285 fprintf (stderr, " hl = %d\n", s->hl);
23286 fprintf (stderr, " left overhang = %d, right = %d\n",
23287 s->left_overhang, s->right_overhang);
23288 fprintf (stderr, " nchars = %d\n", s->nchars);
23289 fprintf (stderr, " extends to end of line = %d\n",
23290 s->extends_to_end_of_line_p);
23291 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23292 fprintf (stderr, " bg width = %d\n", s->background_width);
23293 }
23294
23295 #endif /* GLYPH_DEBUG */
23296
23297 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23298 of XChar2b structures for S; it can't be allocated in
23299 init_glyph_string because it must be allocated via `alloca'. W
23300 is the window on which S is drawn. ROW and AREA are the glyph row
23301 and area within the row from which S is constructed. START is the
23302 index of the first glyph structure covered by S. HL is a
23303 face-override for drawing S. */
23304
23305 #ifdef HAVE_NTGUI
23306 #define OPTIONAL_HDC(hdc) HDC hdc,
23307 #define DECLARE_HDC(hdc) HDC hdc;
23308 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23309 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23310 #endif
23311
23312 #ifndef OPTIONAL_HDC
23313 #define OPTIONAL_HDC(hdc)
23314 #define DECLARE_HDC(hdc)
23315 #define ALLOCATE_HDC(hdc, f)
23316 #define RELEASE_HDC(hdc, f)
23317 #endif
23318
23319 static void
23320 init_glyph_string (struct glyph_string *s,
23321 OPTIONAL_HDC (hdc)
23322 XChar2b *char2b, struct window *w, struct glyph_row *row,
23323 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23324 {
23325 memset (s, 0, sizeof *s);
23326 s->w = w;
23327 s->f = XFRAME (w->frame);
23328 #ifdef HAVE_NTGUI
23329 s->hdc = hdc;
23330 #endif
23331 s->display = FRAME_X_DISPLAY (s->f);
23332 s->window = FRAME_X_WINDOW (s->f);
23333 s->char2b = char2b;
23334 s->hl = hl;
23335 s->row = row;
23336 s->area = area;
23337 s->first_glyph = row->glyphs[area] + start;
23338 s->height = row->height;
23339 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23340 s->ybase = s->y + row->ascent;
23341 }
23342
23343
23344 /* Append the list of glyph strings with head H and tail T to the list
23345 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23346
23347 static void
23348 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23349 struct glyph_string *h, struct glyph_string *t)
23350 {
23351 if (h)
23352 {
23353 if (*head)
23354 (*tail)->next = h;
23355 else
23356 *head = h;
23357 h->prev = *tail;
23358 *tail = t;
23359 }
23360 }
23361
23362
23363 /* Prepend the list of glyph strings with head H and tail T to the
23364 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23365 result. */
23366
23367 static void
23368 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23369 struct glyph_string *h, struct glyph_string *t)
23370 {
23371 if (h)
23372 {
23373 if (*head)
23374 (*head)->prev = t;
23375 else
23376 *tail = t;
23377 t->next = *head;
23378 *head = h;
23379 }
23380 }
23381
23382
23383 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23384 Set *HEAD and *TAIL to the resulting list. */
23385
23386 static void
23387 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23388 struct glyph_string *s)
23389 {
23390 s->next = s->prev = NULL;
23391 append_glyph_string_lists (head, tail, s, s);
23392 }
23393
23394
23395 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23396 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23397 make sure that X resources for the face returned are allocated.
23398 Value is a pointer to a realized face that is ready for display if
23399 DISPLAY_P is non-zero. */
23400
23401 static struct face *
23402 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23403 XChar2b *char2b, int display_p)
23404 {
23405 struct face *face = FACE_FROM_ID (f, face_id);
23406 unsigned code = 0;
23407
23408 if (face->font)
23409 {
23410 code = face->font->driver->encode_char (face->font, c);
23411
23412 if (code == FONT_INVALID_CODE)
23413 code = 0;
23414 }
23415 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23416
23417 /* Make sure X resources of the face are allocated. */
23418 #ifdef HAVE_X_WINDOWS
23419 if (display_p)
23420 #endif
23421 {
23422 eassert (face != NULL);
23423 PREPARE_FACE_FOR_DISPLAY (f, face);
23424 }
23425
23426 return face;
23427 }
23428
23429
23430 /* Get face and two-byte form of character glyph GLYPH on frame F.
23431 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23432 a pointer to a realized face that is ready for display. */
23433
23434 static struct face *
23435 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23436 XChar2b *char2b, int *two_byte_p)
23437 {
23438 struct face *face;
23439 unsigned code = 0;
23440
23441 eassert (glyph->type == CHAR_GLYPH);
23442 face = FACE_FROM_ID (f, glyph->face_id);
23443
23444 /* Make sure X resources of the face are allocated. */
23445 eassert (face != NULL);
23446 PREPARE_FACE_FOR_DISPLAY (f, face);
23447
23448 if (two_byte_p)
23449 *two_byte_p = 0;
23450
23451 if (face->font)
23452 {
23453 if (CHAR_BYTE8_P (glyph->u.ch))
23454 code = CHAR_TO_BYTE8 (glyph->u.ch);
23455 else
23456 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23457
23458 if (code == FONT_INVALID_CODE)
23459 code = 0;
23460 }
23461
23462 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23463 return face;
23464 }
23465
23466
23467 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23468 Return 1 if FONT has a glyph for C, otherwise return 0. */
23469
23470 static int
23471 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23472 {
23473 unsigned code;
23474
23475 if (CHAR_BYTE8_P (c))
23476 code = CHAR_TO_BYTE8 (c);
23477 else
23478 code = font->driver->encode_char (font, c);
23479
23480 if (code == FONT_INVALID_CODE)
23481 return 0;
23482 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23483 return 1;
23484 }
23485
23486
23487 /* Fill glyph string S with composition components specified by S->cmp.
23488
23489 BASE_FACE is the base face of the composition.
23490 S->cmp_from is the index of the first component for S.
23491
23492 OVERLAPS non-zero means S should draw the foreground only, and use
23493 its physical height for clipping. See also draw_glyphs.
23494
23495 Value is the index of a component not in S. */
23496
23497 static int
23498 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23499 int overlaps)
23500 {
23501 int i;
23502 /* For all glyphs of this composition, starting at the offset
23503 S->cmp_from, until we reach the end of the definition or encounter a
23504 glyph that requires the different face, add it to S. */
23505 struct face *face;
23506
23507 eassert (s);
23508
23509 s->for_overlaps = overlaps;
23510 s->face = NULL;
23511 s->font = NULL;
23512 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23513 {
23514 int c = COMPOSITION_GLYPH (s->cmp, i);
23515
23516 /* TAB in a composition means display glyphs with padding space
23517 on the left or right. */
23518 if (c != '\t')
23519 {
23520 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23521 -1, Qnil);
23522
23523 face = get_char_face_and_encoding (s->f, c, face_id,
23524 s->char2b + i, 1);
23525 if (face)
23526 {
23527 if (! s->face)
23528 {
23529 s->face = face;
23530 s->font = s->face->font;
23531 }
23532 else if (s->face != face)
23533 break;
23534 }
23535 }
23536 ++s->nchars;
23537 }
23538 s->cmp_to = i;
23539
23540 if (s->face == NULL)
23541 {
23542 s->face = base_face->ascii_face;
23543 s->font = s->face->font;
23544 }
23545
23546 /* All glyph strings for the same composition has the same width,
23547 i.e. the width set for the first component of the composition. */
23548 s->width = s->first_glyph->pixel_width;
23549
23550 /* If the specified font could not be loaded, use the frame's
23551 default font, but record the fact that we couldn't load it in
23552 the glyph string so that we can draw rectangles for the
23553 characters of the glyph string. */
23554 if (s->font == NULL)
23555 {
23556 s->font_not_found_p = 1;
23557 s->font = FRAME_FONT (s->f);
23558 }
23559
23560 /* Adjust base line for subscript/superscript text. */
23561 s->ybase += s->first_glyph->voffset;
23562
23563 /* This glyph string must always be drawn with 16-bit functions. */
23564 s->two_byte_p = 1;
23565
23566 return s->cmp_to;
23567 }
23568
23569 static int
23570 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23571 int start, int end, int overlaps)
23572 {
23573 struct glyph *glyph, *last;
23574 Lisp_Object lgstring;
23575 int i;
23576
23577 s->for_overlaps = overlaps;
23578 glyph = s->row->glyphs[s->area] + start;
23579 last = s->row->glyphs[s->area] + end;
23580 s->cmp_id = glyph->u.cmp.id;
23581 s->cmp_from = glyph->slice.cmp.from;
23582 s->cmp_to = glyph->slice.cmp.to + 1;
23583 s->face = FACE_FROM_ID (s->f, face_id);
23584 lgstring = composition_gstring_from_id (s->cmp_id);
23585 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23586 glyph++;
23587 while (glyph < last
23588 && glyph->u.cmp.automatic
23589 && glyph->u.cmp.id == s->cmp_id
23590 && s->cmp_to == glyph->slice.cmp.from)
23591 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23592
23593 for (i = s->cmp_from; i < s->cmp_to; i++)
23594 {
23595 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23596 unsigned code = LGLYPH_CODE (lglyph);
23597
23598 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23599 }
23600 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23601 return glyph - s->row->glyphs[s->area];
23602 }
23603
23604
23605 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23606 See the comment of fill_glyph_string for arguments.
23607 Value is the index of the first glyph not in S. */
23608
23609
23610 static int
23611 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23612 int start, int end, int overlaps)
23613 {
23614 struct glyph *glyph, *last;
23615 int voffset;
23616
23617 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23618 s->for_overlaps = overlaps;
23619 glyph = s->row->glyphs[s->area] + start;
23620 last = s->row->glyphs[s->area] + end;
23621 voffset = glyph->voffset;
23622 s->face = FACE_FROM_ID (s->f, face_id);
23623 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23624 s->nchars = 1;
23625 s->width = glyph->pixel_width;
23626 glyph++;
23627 while (glyph < last
23628 && glyph->type == GLYPHLESS_GLYPH
23629 && glyph->voffset == voffset
23630 && glyph->face_id == face_id)
23631 {
23632 s->nchars++;
23633 s->width += glyph->pixel_width;
23634 glyph++;
23635 }
23636 s->ybase += voffset;
23637 return glyph - s->row->glyphs[s->area];
23638 }
23639
23640
23641 /* Fill glyph string S from a sequence of character glyphs.
23642
23643 FACE_ID is the face id of the string. START is the index of the
23644 first glyph to consider, END is the index of the last + 1.
23645 OVERLAPS non-zero means S should draw the foreground only, and use
23646 its physical height for clipping. See also draw_glyphs.
23647
23648 Value is the index of the first glyph not in S. */
23649
23650 static int
23651 fill_glyph_string (struct glyph_string *s, int face_id,
23652 int start, int end, int overlaps)
23653 {
23654 struct glyph *glyph, *last;
23655 int voffset;
23656 int glyph_not_available_p;
23657
23658 eassert (s->f == XFRAME (s->w->frame));
23659 eassert (s->nchars == 0);
23660 eassert (start >= 0 && end > start);
23661
23662 s->for_overlaps = overlaps;
23663 glyph = s->row->glyphs[s->area] + start;
23664 last = s->row->glyphs[s->area] + end;
23665 voffset = glyph->voffset;
23666 s->padding_p = glyph->padding_p;
23667 glyph_not_available_p = glyph->glyph_not_available_p;
23668
23669 while (glyph < last
23670 && glyph->type == CHAR_GLYPH
23671 && glyph->voffset == voffset
23672 /* Same face id implies same font, nowadays. */
23673 && glyph->face_id == face_id
23674 && glyph->glyph_not_available_p == glyph_not_available_p)
23675 {
23676 int two_byte_p;
23677
23678 s->face = get_glyph_face_and_encoding (s->f, glyph,
23679 s->char2b + s->nchars,
23680 &two_byte_p);
23681 s->two_byte_p = two_byte_p;
23682 ++s->nchars;
23683 eassert (s->nchars <= end - start);
23684 s->width += glyph->pixel_width;
23685 if (glyph++->padding_p != s->padding_p)
23686 break;
23687 }
23688
23689 s->font = s->face->font;
23690
23691 /* If the specified font could not be loaded, use the frame's font,
23692 but record the fact that we couldn't load it in
23693 S->font_not_found_p so that we can draw rectangles for the
23694 characters of the glyph string. */
23695 if (s->font == NULL || glyph_not_available_p)
23696 {
23697 s->font_not_found_p = 1;
23698 s->font = FRAME_FONT (s->f);
23699 }
23700
23701 /* Adjust base line for subscript/superscript text. */
23702 s->ybase += voffset;
23703
23704 eassert (s->face && s->face->gc);
23705 return glyph - s->row->glyphs[s->area];
23706 }
23707
23708
23709 /* Fill glyph string S from image glyph S->first_glyph. */
23710
23711 static void
23712 fill_image_glyph_string (struct glyph_string *s)
23713 {
23714 eassert (s->first_glyph->type == IMAGE_GLYPH);
23715 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23716 eassert (s->img);
23717 s->slice = s->first_glyph->slice.img;
23718 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23719 s->font = s->face->font;
23720 s->width = s->first_glyph->pixel_width;
23721
23722 /* Adjust base line for subscript/superscript text. */
23723 s->ybase += s->first_glyph->voffset;
23724 }
23725
23726
23727 /* Fill glyph string S from a sequence of stretch glyphs.
23728
23729 START is the index of the first glyph to consider,
23730 END is the index of the last + 1.
23731
23732 Value is the index of the first glyph not in S. */
23733
23734 static int
23735 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23736 {
23737 struct glyph *glyph, *last;
23738 int voffset, face_id;
23739
23740 eassert (s->first_glyph->type == STRETCH_GLYPH);
23741
23742 glyph = s->row->glyphs[s->area] + start;
23743 last = s->row->glyphs[s->area] + end;
23744 face_id = glyph->face_id;
23745 s->face = FACE_FROM_ID (s->f, face_id);
23746 s->font = s->face->font;
23747 s->width = glyph->pixel_width;
23748 s->nchars = 1;
23749 voffset = glyph->voffset;
23750
23751 for (++glyph;
23752 (glyph < last
23753 && glyph->type == STRETCH_GLYPH
23754 && glyph->voffset == voffset
23755 && glyph->face_id == face_id);
23756 ++glyph)
23757 s->width += glyph->pixel_width;
23758
23759 /* Adjust base line for subscript/superscript text. */
23760 s->ybase += voffset;
23761
23762 /* The case that face->gc == 0 is handled when drawing the glyph
23763 string by calling PREPARE_FACE_FOR_DISPLAY. */
23764 eassert (s->face);
23765 return glyph - s->row->glyphs[s->area];
23766 }
23767
23768 static struct font_metrics *
23769 get_per_char_metric (struct font *font, XChar2b *char2b)
23770 {
23771 static struct font_metrics metrics;
23772 unsigned code;
23773
23774 if (! font)
23775 return NULL;
23776 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23777 if (code == FONT_INVALID_CODE)
23778 return NULL;
23779 font->driver->text_extents (font, &code, 1, &metrics);
23780 return &metrics;
23781 }
23782
23783 /* EXPORT for RIF:
23784 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23785 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23786 assumed to be zero. */
23787
23788 void
23789 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23790 {
23791 *left = *right = 0;
23792
23793 if (glyph->type == CHAR_GLYPH)
23794 {
23795 struct face *face;
23796 XChar2b char2b;
23797 struct font_metrics *pcm;
23798
23799 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23800 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23801 {
23802 if (pcm->rbearing > pcm->width)
23803 *right = pcm->rbearing - pcm->width;
23804 if (pcm->lbearing < 0)
23805 *left = -pcm->lbearing;
23806 }
23807 }
23808 else if (glyph->type == COMPOSITE_GLYPH)
23809 {
23810 if (! glyph->u.cmp.automatic)
23811 {
23812 struct composition *cmp = composition_table[glyph->u.cmp.id];
23813
23814 if (cmp->rbearing > cmp->pixel_width)
23815 *right = cmp->rbearing - cmp->pixel_width;
23816 if (cmp->lbearing < 0)
23817 *left = - cmp->lbearing;
23818 }
23819 else
23820 {
23821 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23822 struct font_metrics metrics;
23823
23824 composition_gstring_width (gstring, glyph->slice.cmp.from,
23825 glyph->slice.cmp.to + 1, &metrics);
23826 if (metrics.rbearing > metrics.width)
23827 *right = metrics.rbearing - metrics.width;
23828 if (metrics.lbearing < 0)
23829 *left = - metrics.lbearing;
23830 }
23831 }
23832 }
23833
23834
23835 /* Return the index of the first glyph preceding glyph string S that
23836 is overwritten by S because of S's left overhang. Value is -1
23837 if no glyphs are overwritten. */
23838
23839 static int
23840 left_overwritten (struct glyph_string *s)
23841 {
23842 int k;
23843
23844 if (s->left_overhang)
23845 {
23846 int x = 0, i;
23847 struct glyph *glyphs = s->row->glyphs[s->area];
23848 int first = s->first_glyph - glyphs;
23849
23850 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23851 x -= glyphs[i].pixel_width;
23852
23853 k = i + 1;
23854 }
23855 else
23856 k = -1;
23857
23858 return k;
23859 }
23860
23861
23862 /* Return the index of the first glyph preceding glyph string S that
23863 is overwriting S because of its right overhang. Value is -1 if no
23864 glyph in front of S overwrites S. */
23865
23866 static int
23867 left_overwriting (struct glyph_string *s)
23868 {
23869 int i, k, x;
23870 struct glyph *glyphs = s->row->glyphs[s->area];
23871 int first = s->first_glyph - glyphs;
23872
23873 k = -1;
23874 x = 0;
23875 for (i = first - 1; i >= 0; --i)
23876 {
23877 int left, right;
23878 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23879 if (x + right > 0)
23880 k = i;
23881 x -= glyphs[i].pixel_width;
23882 }
23883
23884 return k;
23885 }
23886
23887
23888 /* Return the index of the last glyph following glyph string S that is
23889 overwritten by S because of S's right overhang. Value is -1 if
23890 no such glyph is found. */
23891
23892 static int
23893 right_overwritten (struct glyph_string *s)
23894 {
23895 int k = -1;
23896
23897 if (s->right_overhang)
23898 {
23899 int x = 0, i;
23900 struct glyph *glyphs = s->row->glyphs[s->area];
23901 int first = (s->first_glyph - glyphs
23902 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23903 int end = s->row->used[s->area];
23904
23905 for (i = first; i < end && s->right_overhang > x; ++i)
23906 x += glyphs[i].pixel_width;
23907
23908 k = i;
23909 }
23910
23911 return k;
23912 }
23913
23914
23915 /* Return the index of the last glyph following glyph string S that
23916 overwrites S because of its left overhang. Value is negative
23917 if no such glyph is found. */
23918
23919 static int
23920 right_overwriting (struct glyph_string *s)
23921 {
23922 int i, k, x;
23923 int end = s->row->used[s->area];
23924 struct glyph *glyphs = s->row->glyphs[s->area];
23925 int first = (s->first_glyph - glyphs
23926 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23927
23928 k = -1;
23929 x = 0;
23930 for (i = first; i < end; ++i)
23931 {
23932 int left, right;
23933 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23934 if (x - left < 0)
23935 k = i;
23936 x += glyphs[i].pixel_width;
23937 }
23938
23939 return k;
23940 }
23941
23942
23943 /* Set background width of glyph string S. START is the index of the
23944 first glyph following S. LAST_X is the right-most x-position + 1
23945 in the drawing area. */
23946
23947 static void
23948 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23949 {
23950 /* If the face of this glyph string has to be drawn to the end of
23951 the drawing area, set S->extends_to_end_of_line_p. */
23952
23953 if (start == s->row->used[s->area]
23954 && ((s->row->fill_line_p
23955 && (s->hl == DRAW_NORMAL_TEXT
23956 || s->hl == DRAW_IMAGE_RAISED
23957 || s->hl == DRAW_IMAGE_SUNKEN))
23958 || s->hl == DRAW_MOUSE_FACE))
23959 s->extends_to_end_of_line_p = 1;
23960
23961 /* If S extends its face to the end of the line, set its
23962 background_width to the distance to the right edge of the drawing
23963 area. */
23964 if (s->extends_to_end_of_line_p)
23965 s->background_width = last_x - s->x + 1;
23966 else
23967 s->background_width = s->width;
23968 }
23969
23970
23971 /* Compute overhangs and x-positions for glyph string S and its
23972 predecessors, or successors. X is the starting x-position for S.
23973 BACKWARD_P non-zero means process predecessors. */
23974
23975 static void
23976 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23977 {
23978 if (backward_p)
23979 {
23980 while (s)
23981 {
23982 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23983 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23984 x -= s->width;
23985 s->x = x;
23986 s = s->prev;
23987 }
23988 }
23989 else
23990 {
23991 while (s)
23992 {
23993 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23994 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23995 s->x = x;
23996 x += s->width;
23997 s = s->next;
23998 }
23999 }
24000 }
24001
24002
24003
24004 /* The following macros are only called from draw_glyphs below.
24005 They reference the following parameters of that function directly:
24006 `w', `row', `area', and `overlap_p'
24007 as well as the following local variables:
24008 `s', `f', and `hdc' (in W32) */
24009
24010 #ifdef HAVE_NTGUI
24011 /* On W32, silently add local `hdc' variable to argument list of
24012 init_glyph_string. */
24013 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24014 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24015 #else
24016 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24017 init_glyph_string (s, char2b, w, row, area, start, hl)
24018 #endif
24019
24020 /* Add a glyph string for a stretch glyph to the list of strings
24021 between HEAD and TAIL. START is the index of the stretch glyph in
24022 row area AREA of glyph row ROW. END is the index of the last glyph
24023 in that glyph row area. X is the current output position assigned
24024 to the new glyph string constructed. HL overrides that face of the
24025 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24026 is the right-most x-position of the drawing area. */
24027
24028 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24029 and below -- keep them on one line. */
24030 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24031 do \
24032 { \
24033 s = alloca (sizeof *s); \
24034 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24035 START = fill_stretch_glyph_string (s, START, END); \
24036 append_glyph_string (&HEAD, &TAIL, s); \
24037 s->x = (X); \
24038 } \
24039 while (0)
24040
24041
24042 /* Add a glyph string for an image glyph to the list of strings
24043 between HEAD and TAIL. START is the index of the image glyph in
24044 row area AREA of glyph row ROW. END is the index of the last glyph
24045 in that glyph row area. X is the current output position assigned
24046 to the new glyph string constructed. HL overrides that face of the
24047 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24048 is the right-most x-position of the drawing area. */
24049
24050 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24051 do \
24052 { \
24053 s = alloca (sizeof *s); \
24054 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24055 fill_image_glyph_string (s); \
24056 append_glyph_string (&HEAD, &TAIL, s); \
24057 ++START; \
24058 s->x = (X); \
24059 } \
24060 while (0)
24061
24062
24063 /* Add a glyph string for a sequence of character glyphs to the list
24064 of strings between HEAD and TAIL. START is the index of the first
24065 glyph in row area AREA of glyph row ROW that is part of the new
24066 glyph string. END is the index of the last glyph in that glyph row
24067 area. X is the current output position assigned to the new glyph
24068 string constructed. HL overrides that face of the glyph; e.g. it
24069 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24070 right-most x-position of the drawing area. */
24071
24072 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24073 do \
24074 { \
24075 int face_id; \
24076 XChar2b *char2b; \
24077 \
24078 face_id = (row)->glyphs[area][START].face_id; \
24079 \
24080 s = alloca (sizeof *s); \
24081 char2b = alloca ((END - START) * sizeof *char2b); \
24082 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24083 append_glyph_string (&HEAD, &TAIL, s); \
24084 s->x = (X); \
24085 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24086 } \
24087 while (0)
24088
24089
24090 /* Add a glyph string for a composite sequence to the list of strings
24091 between HEAD and TAIL. START is the index of the first glyph in
24092 row area AREA of glyph row ROW that is part of the new glyph
24093 string. END is the index of the last glyph in that glyph row area.
24094 X is the current output position assigned to the new glyph string
24095 constructed. HL overrides that face of the glyph; e.g. it is
24096 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24097 x-position of the drawing area. */
24098
24099 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24100 do { \
24101 int face_id = (row)->glyphs[area][START].face_id; \
24102 struct face *base_face = FACE_FROM_ID (f, face_id); \
24103 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24104 struct composition *cmp = composition_table[cmp_id]; \
24105 XChar2b *char2b; \
24106 struct glyph_string *first_s = NULL; \
24107 int n; \
24108 \
24109 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24110 \
24111 /* Make glyph_strings for each glyph sequence that is drawable by \
24112 the same face, and append them to HEAD/TAIL. */ \
24113 for (n = 0; n < cmp->glyph_len;) \
24114 { \
24115 s = alloca (sizeof *s); \
24116 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24117 append_glyph_string (&(HEAD), &(TAIL), s); \
24118 s->cmp = cmp; \
24119 s->cmp_from = n; \
24120 s->x = (X); \
24121 if (n == 0) \
24122 first_s = s; \
24123 n = fill_composite_glyph_string (s, base_face, overlaps); \
24124 } \
24125 \
24126 ++START; \
24127 s = first_s; \
24128 } while (0)
24129
24130
24131 /* Add a glyph string for a glyph-string sequence to the list of strings
24132 between HEAD and TAIL. */
24133
24134 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24135 do { \
24136 int face_id; \
24137 XChar2b *char2b; \
24138 Lisp_Object gstring; \
24139 \
24140 face_id = (row)->glyphs[area][START].face_id; \
24141 gstring = (composition_gstring_from_id \
24142 ((row)->glyphs[area][START].u.cmp.id)); \
24143 s = alloca (sizeof *s); \
24144 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24145 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24146 append_glyph_string (&(HEAD), &(TAIL), s); \
24147 s->x = (X); \
24148 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24149 } while (0)
24150
24151
24152 /* Add a glyph string for a sequence of glyphless character's glyphs
24153 to the list of strings between HEAD and TAIL. The meanings of
24154 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24155
24156 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24157 do \
24158 { \
24159 int face_id; \
24160 \
24161 face_id = (row)->glyphs[area][START].face_id; \
24162 \
24163 s = alloca (sizeof *s); \
24164 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24165 append_glyph_string (&HEAD, &TAIL, s); \
24166 s->x = (X); \
24167 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24168 overlaps); \
24169 } \
24170 while (0)
24171
24172
24173 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24174 of AREA of glyph row ROW on window W between indices START and END.
24175 HL overrides the face for drawing glyph strings, e.g. it is
24176 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24177 x-positions of the drawing area.
24178
24179 This is an ugly monster macro construct because we must use alloca
24180 to allocate glyph strings (because draw_glyphs can be called
24181 asynchronously). */
24182
24183 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24184 do \
24185 { \
24186 HEAD = TAIL = NULL; \
24187 while (START < END) \
24188 { \
24189 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24190 switch (first_glyph->type) \
24191 { \
24192 case CHAR_GLYPH: \
24193 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24194 HL, X, LAST_X); \
24195 break; \
24196 \
24197 case COMPOSITE_GLYPH: \
24198 if (first_glyph->u.cmp.automatic) \
24199 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24200 HL, X, LAST_X); \
24201 else \
24202 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24203 HL, X, LAST_X); \
24204 break; \
24205 \
24206 case STRETCH_GLYPH: \
24207 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24208 HL, X, LAST_X); \
24209 break; \
24210 \
24211 case IMAGE_GLYPH: \
24212 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24213 HL, X, LAST_X); \
24214 break; \
24215 \
24216 case GLYPHLESS_GLYPH: \
24217 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24218 HL, X, LAST_X); \
24219 break; \
24220 \
24221 default: \
24222 emacs_abort (); \
24223 } \
24224 \
24225 if (s) \
24226 { \
24227 set_glyph_string_background_width (s, START, LAST_X); \
24228 (X) += s->width; \
24229 } \
24230 } \
24231 } while (0)
24232
24233
24234 /* Draw glyphs between START and END in AREA of ROW on window W,
24235 starting at x-position X. X is relative to AREA in W. HL is a
24236 face-override with the following meaning:
24237
24238 DRAW_NORMAL_TEXT draw normally
24239 DRAW_CURSOR draw in cursor face
24240 DRAW_MOUSE_FACE draw in mouse face.
24241 DRAW_INVERSE_VIDEO draw in mode line face
24242 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24243 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24244
24245 If OVERLAPS is non-zero, draw only the foreground of characters and
24246 clip to the physical height of ROW. Non-zero value also defines
24247 the overlapping part to be drawn:
24248
24249 OVERLAPS_PRED overlap with preceding rows
24250 OVERLAPS_SUCC overlap with succeeding rows
24251 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24252 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24253
24254 Value is the x-position reached, relative to AREA of W. */
24255
24256 static int
24257 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24258 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24259 enum draw_glyphs_face hl, int overlaps)
24260 {
24261 struct glyph_string *head, *tail;
24262 struct glyph_string *s;
24263 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24264 int i, j, x_reached, last_x, area_left = 0;
24265 struct frame *f = XFRAME (WINDOW_FRAME (w));
24266 DECLARE_HDC (hdc);
24267
24268 ALLOCATE_HDC (hdc, f);
24269
24270 /* Let's rather be paranoid than getting a SEGV. */
24271 end = min (end, row->used[area]);
24272 start = clip_to_bounds (0, start, end);
24273
24274 /* Translate X to frame coordinates. Set last_x to the right
24275 end of the drawing area. */
24276 if (row->full_width_p)
24277 {
24278 /* X is relative to the left edge of W, without scroll bars
24279 or fringes. */
24280 area_left = WINDOW_LEFT_EDGE_X (w);
24281 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24282 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24283 }
24284 else
24285 {
24286 area_left = window_box_left (w, area);
24287 last_x = area_left + window_box_width (w, area);
24288 }
24289 x += area_left;
24290
24291 /* Build a doubly-linked list of glyph_string structures between
24292 head and tail from what we have to draw. Note that the macro
24293 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24294 the reason we use a separate variable `i'. */
24295 i = start;
24296 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24297 if (tail)
24298 x_reached = tail->x + tail->background_width;
24299 else
24300 x_reached = x;
24301
24302 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24303 the row, redraw some glyphs in front or following the glyph
24304 strings built above. */
24305 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24306 {
24307 struct glyph_string *h, *t;
24308 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24309 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24310 int check_mouse_face = 0;
24311 int dummy_x = 0;
24312
24313 /* If mouse highlighting is on, we may need to draw adjacent
24314 glyphs using mouse-face highlighting. */
24315 if (area == TEXT_AREA && row->mouse_face_p
24316 && hlinfo->mouse_face_beg_row >= 0
24317 && hlinfo->mouse_face_end_row >= 0)
24318 {
24319 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24320
24321 if (row_vpos >= hlinfo->mouse_face_beg_row
24322 && row_vpos <= hlinfo->mouse_face_end_row)
24323 {
24324 check_mouse_face = 1;
24325 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24326 ? hlinfo->mouse_face_beg_col : 0;
24327 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24328 ? hlinfo->mouse_face_end_col
24329 : row->used[TEXT_AREA];
24330 }
24331 }
24332
24333 /* Compute overhangs for all glyph strings. */
24334 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24335 for (s = head; s; s = s->next)
24336 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24337
24338 /* Prepend glyph strings for glyphs in front of the first glyph
24339 string that are overwritten because of the first glyph
24340 string's left overhang. The background of all strings
24341 prepended must be drawn because the first glyph string
24342 draws over it. */
24343 i = left_overwritten (head);
24344 if (i >= 0)
24345 {
24346 enum draw_glyphs_face overlap_hl;
24347
24348 /* If this row contains mouse highlighting, attempt to draw
24349 the overlapped glyphs with the correct highlight. This
24350 code fails if the overlap encompasses more than one glyph
24351 and mouse-highlight spans only some of these glyphs.
24352 However, making it work perfectly involves a lot more
24353 code, and I don't know if the pathological case occurs in
24354 practice, so we'll stick to this for now. --- cyd */
24355 if (check_mouse_face
24356 && mouse_beg_col < start && mouse_end_col > i)
24357 overlap_hl = DRAW_MOUSE_FACE;
24358 else
24359 overlap_hl = DRAW_NORMAL_TEXT;
24360
24361 j = i;
24362 BUILD_GLYPH_STRINGS (j, start, h, t,
24363 overlap_hl, dummy_x, last_x);
24364 start = i;
24365 compute_overhangs_and_x (t, head->x, 1);
24366 prepend_glyph_string_lists (&head, &tail, h, t);
24367 clip_head = head;
24368 }
24369
24370 /* Prepend glyph strings for glyphs in front of the first glyph
24371 string that overwrite that glyph string because of their
24372 right overhang. For these strings, only the foreground must
24373 be drawn, because it draws over the glyph string at `head'.
24374 The background must not be drawn because this would overwrite
24375 right overhangs of preceding glyphs for which no glyph
24376 strings exist. */
24377 i = left_overwriting (head);
24378 if (i >= 0)
24379 {
24380 enum draw_glyphs_face overlap_hl;
24381
24382 if (check_mouse_face
24383 && mouse_beg_col < start && mouse_end_col > i)
24384 overlap_hl = DRAW_MOUSE_FACE;
24385 else
24386 overlap_hl = DRAW_NORMAL_TEXT;
24387
24388 clip_head = head;
24389 BUILD_GLYPH_STRINGS (i, start, h, t,
24390 overlap_hl, dummy_x, last_x);
24391 for (s = h; s; s = s->next)
24392 s->background_filled_p = 1;
24393 compute_overhangs_and_x (t, head->x, 1);
24394 prepend_glyph_string_lists (&head, &tail, h, t);
24395 }
24396
24397 /* Append glyphs strings for glyphs following the last glyph
24398 string tail that are overwritten by tail. The background of
24399 these strings has to be drawn because tail's foreground draws
24400 over it. */
24401 i = right_overwritten (tail);
24402 if (i >= 0)
24403 {
24404 enum draw_glyphs_face overlap_hl;
24405
24406 if (check_mouse_face
24407 && mouse_beg_col < i && mouse_end_col > end)
24408 overlap_hl = DRAW_MOUSE_FACE;
24409 else
24410 overlap_hl = DRAW_NORMAL_TEXT;
24411
24412 BUILD_GLYPH_STRINGS (end, i, h, t,
24413 overlap_hl, x, last_x);
24414 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24415 we don't have `end = i;' here. */
24416 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24417 append_glyph_string_lists (&head, &tail, h, t);
24418 clip_tail = tail;
24419 }
24420
24421 /* Append glyph strings for glyphs following the last glyph
24422 string tail that overwrite tail. The foreground of such
24423 glyphs has to be drawn because it writes into the background
24424 of tail. The background must not be drawn because it could
24425 paint over the foreground of following glyphs. */
24426 i = right_overwriting (tail);
24427 if (i >= 0)
24428 {
24429 enum draw_glyphs_face overlap_hl;
24430 if (check_mouse_face
24431 && mouse_beg_col < i && mouse_end_col > end)
24432 overlap_hl = DRAW_MOUSE_FACE;
24433 else
24434 overlap_hl = DRAW_NORMAL_TEXT;
24435
24436 clip_tail = tail;
24437 i++; /* We must include the Ith glyph. */
24438 BUILD_GLYPH_STRINGS (end, i, h, t,
24439 overlap_hl, x, last_x);
24440 for (s = h; s; s = s->next)
24441 s->background_filled_p = 1;
24442 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24443 append_glyph_string_lists (&head, &tail, h, t);
24444 }
24445 if (clip_head || clip_tail)
24446 for (s = head; s; s = s->next)
24447 {
24448 s->clip_head = clip_head;
24449 s->clip_tail = clip_tail;
24450 }
24451 }
24452
24453 /* Draw all strings. */
24454 for (s = head; s; s = s->next)
24455 FRAME_RIF (f)->draw_glyph_string (s);
24456
24457 #ifndef HAVE_NS
24458 /* When focus a sole frame and move horizontally, this sets on_p to 0
24459 causing a failure to erase prev cursor position. */
24460 if (area == TEXT_AREA
24461 && !row->full_width_p
24462 /* When drawing overlapping rows, only the glyph strings'
24463 foreground is drawn, which doesn't erase a cursor
24464 completely. */
24465 && !overlaps)
24466 {
24467 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24468 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24469 : (tail ? tail->x + tail->background_width : x));
24470 x0 -= area_left;
24471 x1 -= area_left;
24472
24473 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24474 row->y, MATRIX_ROW_BOTTOM_Y (row));
24475 }
24476 #endif
24477
24478 /* Value is the x-position up to which drawn, relative to AREA of W.
24479 This doesn't include parts drawn because of overhangs. */
24480 if (row->full_width_p)
24481 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24482 else
24483 x_reached -= area_left;
24484
24485 RELEASE_HDC (hdc, f);
24486
24487 return x_reached;
24488 }
24489
24490 /* Expand row matrix if too narrow. Don't expand if area
24491 is not present. */
24492
24493 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24494 { \
24495 if (!it->f->fonts_changed \
24496 && (it->glyph_row->glyphs[area] \
24497 < it->glyph_row->glyphs[area + 1])) \
24498 { \
24499 it->w->ncols_scale_factor++; \
24500 it->f->fonts_changed = 1; \
24501 } \
24502 }
24503
24504 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24505 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24506
24507 static void
24508 append_glyph (struct it *it)
24509 {
24510 struct glyph *glyph;
24511 enum glyph_row_area area = it->area;
24512
24513 eassert (it->glyph_row);
24514 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24515
24516 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24517 if (glyph < it->glyph_row->glyphs[area + 1])
24518 {
24519 /* If the glyph row is reversed, we need to prepend the glyph
24520 rather than append it. */
24521 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24522 {
24523 struct glyph *g;
24524
24525 /* Make room for the additional glyph. */
24526 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24527 g[1] = *g;
24528 glyph = it->glyph_row->glyphs[area];
24529 }
24530 glyph->charpos = CHARPOS (it->position);
24531 glyph->object = it->object;
24532 if (it->pixel_width > 0)
24533 {
24534 glyph->pixel_width = it->pixel_width;
24535 glyph->padding_p = 0;
24536 }
24537 else
24538 {
24539 /* Assure at least 1-pixel width. Otherwise, cursor can't
24540 be displayed correctly. */
24541 glyph->pixel_width = 1;
24542 glyph->padding_p = 1;
24543 }
24544 glyph->ascent = it->ascent;
24545 glyph->descent = it->descent;
24546 glyph->voffset = it->voffset;
24547 glyph->type = CHAR_GLYPH;
24548 glyph->avoid_cursor_p = it->avoid_cursor_p;
24549 glyph->multibyte_p = it->multibyte_p;
24550 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24551 {
24552 /* In R2L rows, the left and the right box edges need to be
24553 drawn in reverse direction. */
24554 glyph->right_box_line_p = it->start_of_box_run_p;
24555 glyph->left_box_line_p = it->end_of_box_run_p;
24556 }
24557 else
24558 {
24559 glyph->left_box_line_p = it->start_of_box_run_p;
24560 glyph->right_box_line_p = it->end_of_box_run_p;
24561 }
24562 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24563 || it->phys_descent > it->descent);
24564 glyph->glyph_not_available_p = it->glyph_not_available_p;
24565 glyph->face_id = it->face_id;
24566 glyph->u.ch = it->char_to_display;
24567 glyph->slice.img = null_glyph_slice;
24568 glyph->font_type = FONT_TYPE_UNKNOWN;
24569 if (it->bidi_p)
24570 {
24571 glyph->resolved_level = it->bidi_it.resolved_level;
24572 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24573 emacs_abort ();
24574 glyph->bidi_type = it->bidi_it.type;
24575 }
24576 else
24577 {
24578 glyph->resolved_level = 0;
24579 glyph->bidi_type = UNKNOWN_BT;
24580 }
24581 ++it->glyph_row->used[area];
24582 }
24583 else
24584 IT_EXPAND_MATRIX_WIDTH (it, area);
24585 }
24586
24587 /* Store one glyph for the composition IT->cmp_it.id in
24588 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24589 non-null. */
24590
24591 static void
24592 append_composite_glyph (struct it *it)
24593 {
24594 struct glyph *glyph;
24595 enum glyph_row_area area = it->area;
24596
24597 eassert (it->glyph_row);
24598
24599 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24600 if (glyph < it->glyph_row->glyphs[area + 1])
24601 {
24602 /* If the glyph row is reversed, we need to prepend the glyph
24603 rather than append it. */
24604 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24605 {
24606 struct glyph *g;
24607
24608 /* Make room for the new glyph. */
24609 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24610 g[1] = *g;
24611 glyph = it->glyph_row->glyphs[it->area];
24612 }
24613 glyph->charpos = it->cmp_it.charpos;
24614 glyph->object = it->object;
24615 glyph->pixel_width = it->pixel_width;
24616 glyph->ascent = it->ascent;
24617 glyph->descent = it->descent;
24618 glyph->voffset = it->voffset;
24619 glyph->type = COMPOSITE_GLYPH;
24620 if (it->cmp_it.ch < 0)
24621 {
24622 glyph->u.cmp.automatic = 0;
24623 glyph->u.cmp.id = it->cmp_it.id;
24624 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24625 }
24626 else
24627 {
24628 glyph->u.cmp.automatic = 1;
24629 glyph->u.cmp.id = it->cmp_it.id;
24630 glyph->slice.cmp.from = it->cmp_it.from;
24631 glyph->slice.cmp.to = it->cmp_it.to - 1;
24632 }
24633 glyph->avoid_cursor_p = it->avoid_cursor_p;
24634 glyph->multibyte_p = it->multibyte_p;
24635 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24636 {
24637 /* In R2L rows, the left and the right box edges need to be
24638 drawn in reverse direction. */
24639 glyph->right_box_line_p = it->start_of_box_run_p;
24640 glyph->left_box_line_p = it->end_of_box_run_p;
24641 }
24642 else
24643 {
24644 glyph->left_box_line_p = it->start_of_box_run_p;
24645 glyph->right_box_line_p = it->end_of_box_run_p;
24646 }
24647 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24648 || it->phys_descent > it->descent);
24649 glyph->padding_p = 0;
24650 glyph->glyph_not_available_p = 0;
24651 glyph->face_id = it->face_id;
24652 glyph->font_type = FONT_TYPE_UNKNOWN;
24653 if (it->bidi_p)
24654 {
24655 glyph->resolved_level = it->bidi_it.resolved_level;
24656 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24657 emacs_abort ();
24658 glyph->bidi_type = it->bidi_it.type;
24659 }
24660 ++it->glyph_row->used[area];
24661 }
24662 else
24663 IT_EXPAND_MATRIX_WIDTH (it, area);
24664 }
24665
24666
24667 /* Change IT->ascent and IT->height according to the setting of
24668 IT->voffset. */
24669
24670 static void
24671 take_vertical_position_into_account (struct it *it)
24672 {
24673 if (it->voffset)
24674 {
24675 if (it->voffset < 0)
24676 /* Increase the ascent so that we can display the text higher
24677 in the line. */
24678 it->ascent -= it->voffset;
24679 else
24680 /* Increase the descent so that we can display the text lower
24681 in the line. */
24682 it->descent += it->voffset;
24683 }
24684 }
24685
24686
24687 /* Produce glyphs/get display metrics for the image IT is loaded with.
24688 See the description of struct display_iterator in dispextern.h for
24689 an overview of struct display_iterator. */
24690
24691 static void
24692 produce_image_glyph (struct it *it)
24693 {
24694 struct image *img;
24695 struct face *face;
24696 int glyph_ascent, crop;
24697 struct glyph_slice slice;
24698
24699 eassert (it->what == IT_IMAGE);
24700
24701 face = FACE_FROM_ID (it->f, it->face_id);
24702 eassert (face);
24703 /* Make sure X resources of the face is loaded. */
24704 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24705
24706 if (it->image_id < 0)
24707 {
24708 /* Fringe bitmap. */
24709 it->ascent = it->phys_ascent = 0;
24710 it->descent = it->phys_descent = 0;
24711 it->pixel_width = 0;
24712 it->nglyphs = 0;
24713 return;
24714 }
24715
24716 img = IMAGE_FROM_ID (it->f, it->image_id);
24717 eassert (img);
24718 /* Make sure X resources of the image is loaded. */
24719 prepare_image_for_display (it->f, img);
24720
24721 slice.x = slice.y = 0;
24722 slice.width = img->width;
24723 slice.height = img->height;
24724
24725 if (INTEGERP (it->slice.x))
24726 slice.x = XINT (it->slice.x);
24727 else if (FLOATP (it->slice.x))
24728 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24729
24730 if (INTEGERP (it->slice.y))
24731 slice.y = XINT (it->slice.y);
24732 else if (FLOATP (it->slice.y))
24733 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24734
24735 if (INTEGERP (it->slice.width))
24736 slice.width = XINT (it->slice.width);
24737 else if (FLOATP (it->slice.width))
24738 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24739
24740 if (INTEGERP (it->slice.height))
24741 slice.height = XINT (it->slice.height);
24742 else if (FLOATP (it->slice.height))
24743 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24744
24745 if (slice.x >= img->width)
24746 slice.x = img->width;
24747 if (slice.y >= img->height)
24748 slice.y = img->height;
24749 if (slice.x + slice.width >= img->width)
24750 slice.width = img->width - slice.x;
24751 if (slice.y + slice.height > img->height)
24752 slice.height = img->height - slice.y;
24753
24754 if (slice.width == 0 || slice.height == 0)
24755 return;
24756
24757 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24758
24759 it->descent = slice.height - glyph_ascent;
24760 if (slice.y == 0)
24761 it->descent += img->vmargin;
24762 if (slice.y + slice.height == img->height)
24763 it->descent += img->vmargin;
24764 it->phys_descent = it->descent;
24765
24766 it->pixel_width = slice.width;
24767 if (slice.x == 0)
24768 it->pixel_width += img->hmargin;
24769 if (slice.x + slice.width == img->width)
24770 it->pixel_width += img->hmargin;
24771
24772 /* It's quite possible for images to have an ascent greater than
24773 their height, so don't get confused in that case. */
24774 if (it->descent < 0)
24775 it->descent = 0;
24776
24777 it->nglyphs = 1;
24778
24779 if (face->box != FACE_NO_BOX)
24780 {
24781 if (face->box_line_width > 0)
24782 {
24783 if (slice.y == 0)
24784 it->ascent += face->box_line_width;
24785 if (slice.y + slice.height == img->height)
24786 it->descent += face->box_line_width;
24787 }
24788
24789 if (it->start_of_box_run_p && slice.x == 0)
24790 it->pixel_width += eabs (face->box_line_width);
24791 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24792 it->pixel_width += eabs (face->box_line_width);
24793 }
24794
24795 take_vertical_position_into_account (it);
24796
24797 /* Automatically crop wide image glyphs at right edge so we can
24798 draw the cursor on same display row. */
24799 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24800 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24801 {
24802 it->pixel_width -= crop;
24803 slice.width -= crop;
24804 }
24805
24806 if (it->glyph_row)
24807 {
24808 struct glyph *glyph;
24809 enum glyph_row_area area = it->area;
24810
24811 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24812 if (glyph < it->glyph_row->glyphs[area + 1])
24813 {
24814 glyph->charpos = CHARPOS (it->position);
24815 glyph->object = it->object;
24816 glyph->pixel_width = it->pixel_width;
24817 glyph->ascent = glyph_ascent;
24818 glyph->descent = it->descent;
24819 glyph->voffset = it->voffset;
24820 glyph->type = IMAGE_GLYPH;
24821 glyph->avoid_cursor_p = it->avoid_cursor_p;
24822 glyph->multibyte_p = it->multibyte_p;
24823 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24824 {
24825 /* In R2L rows, the left and the right box edges need to be
24826 drawn in reverse direction. */
24827 glyph->right_box_line_p = it->start_of_box_run_p;
24828 glyph->left_box_line_p = it->end_of_box_run_p;
24829 }
24830 else
24831 {
24832 glyph->left_box_line_p = it->start_of_box_run_p;
24833 glyph->right_box_line_p = it->end_of_box_run_p;
24834 }
24835 glyph->overlaps_vertically_p = 0;
24836 glyph->padding_p = 0;
24837 glyph->glyph_not_available_p = 0;
24838 glyph->face_id = it->face_id;
24839 glyph->u.img_id = img->id;
24840 glyph->slice.img = slice;
24841 glyph->font_type = FONT_TYPE_UNKNOWN;
24842 if (it->bidi_p)
24843 {
24844 glyph->resolved_level = it->bidi_it.resolved_level;
24845 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24846 emacs_abort ();
24847 glyph->bidi_type = it->bidi_it.type;
24848 }
24849 ++it->glyph_row->used[area];
24850 }
24851 else
24852 IT_EXPAND_MATRIX_WIDTH (it, area);
24853 }
24854 }
24855
24856
24857 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24858 of the glyph, WIDTH and HEIGHT are the width and height of the
24859 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24860
24861 static void
24862 append_stretch_glyph (struct it *it, Lisp_Object object,
24863 int width, int height, int ascent)
24864 {
24865 struct glyph *glyph;
24866 enum glyph_row_area area = it->area;
24867
24868 eassert (ascent >= 0 && ascent <= height);
24869
24870 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24871 if (glyph < it->glyph_row->glyphs[area + 1])
24872 {
24873 /* If the glyph row is reversed, we need to prepend the glyph
24874 rather than append it. */
24875 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24876 {
24877 struct glyph *g;
24878
24879 /* Make room for the additional glyph. */
24880 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24881 g[1] = *g;
24882 glyph = it->glyph_row->glyphs[area];
24883 }
24884 glyph->charpos = CHARPOS (it->position);
24885 glyph->object = object;
24886 glyph->pixel_width = width;
24887 glyph->ascent = ascent;
24888 glyph->descent = height - ascent;
24889 glyph->voffset = it->voffset;
24890 glyph->type = STRETCH_GLYPH;
24891 glyph->avoid_cursor_p = it->avoid_cursor_p;
24892 glyph->multibyte_p = it->multibyte_p;
24893 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24894 {
24895 /* In R2L rows, the left and the right box edges need to be
24896 drawn in reverse direction. */
24897 glyph->right_box_line_p = it->start_of_box_run_p;
24898 glyph->left_box_line_p = it->end_of_box_run_p;
24899 }
24900 else
24901 {
24902 glyph->left_box_line_p = it->start_of_box_run_p;
24903 glyph->right_box_line_p = it->end_of_box_run_p;
24904 }
24905 glyph->overlaps_vertically_p = 0;
24906 glyph->padding_p = 0;
24907 glyph->glyph_not_available_p = 0;
24908 glyph->face_id = it->face_id;
24909 glyph->u.stretch.ascent = ascent;
24910 glyph->u.stretch.height = height;
24911 glyph->slice.img = null_glyph_slice;
24912 glyph->font_type = FONT_TYPE_UNKNOWN;
24913 if (it->bidi_p)
24914 {
24915 glyph->resolved_level = it->bidi_it.resolved_level;
24916 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24917 emacs_abort ();
24918 glyph->bidi_type = it->bidi_it.type;
24919 }
24920 else
24921 {
24922 glyph->resolved_level = 0;
24923 glyph->bidi_type = UNKNOWN_BT;
24924 }
24925 ++it->glyph_row->used[area];
24926 }
24927 else
24928 IT_EXPAND_MATRIX_WIDTH (it, area);
24929 }
24930
24931 #endif /* HAVE_WINDOW_SYSTEM */
24932
24933 /* Produce a stretch glyph for iterator IT. IT->object is the value
24934 of the glyph property displayed. The value must be a list
24935 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24936 being recognized:
24937
24938 1. `:width WIDTH' specifies that the space should be WIDTH *
24939 canonical char width wide. WIDTH may be an integer or floating
24940 point number.
24941
24942 2. `:relative-width FACTOR' specifies that the width of the stretch
24943 should be computed from the width of the first character having the
24944 `glyph' property, and should be FACTOR times that width.
24945
24946 3. `:align-to HPOS' specifies that the space should be wide enough
24947 to reach HPOS, a value in canonical character units.
24948
24949 Exactly one of the above pairs must be present.
24950
24951 4. `:height HEIGHT' specifies that the height of the stretch produced
24952 should be HEIGHT, measured in canonical character units.
24953
24954 5. `:relative-height FACTOR' specifies that the height of the
24955 stretch should be FACTOR times the height of the characters having
24956 the glyph property.
24957
24958 Either none or exactly one of 4 or 5 must be present.
24959
24960 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24961 of the stretch should be used for the ascent of the stretch.
24962 ASCENT must be in the range 0 <= ASCENT <= 100. */
24963
24964 void
24965 produce_stretch_glyph (struct it *it)
24966 {
24967 /* (space :width WIDTH :height HEIGHT ...) */
24968 Lisp_Object prop, plist;
24969 int width = 0, height = 0, align_to = -1;
24970 int zero_width_ok_p = 0;
24971 double tem;
24972 struct font *font = NULL;
24973
24974 #ifdef HAVE_WINDOW_SYSTEM
24975 int ascent = 0;
24976 int zero_height_ok_p = 0;
24977
24978 if (FRAME_WINDOW_P (it->f))
24979 {
24980 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24981 font = face->font ? face->font : FRAME_FONT (it->f);
24982 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24983 }
24984 #endif
24985
24986 /* List should start with `space'. */
24987 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24988 plist = XCDR (it->object);
24989
24990 /* Compute the width of the stretch. */
24991 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24992 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24993 {
24994 /* Absolute width `:width WIDTH' specified and valid. */
24995 zero_width_ok_p = 1;
24996 width = (int)tem;
24997 }
24998 #ifdef HAVE_WINDOW_SYSTEM
24999 else if (FRAME_WINDOW_P (it->f)
25000 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25001 {
25002 /* Relative width `:relative-width FACTOR' specified and valid.
25003 Compute the width of the characters having the `glyph'
25004 property. */
25005 struct it it2;
25006 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25007
25008 it2 = *it;
25009 if (it->multibyte_p)
25010 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25011 else
25012 {
25013 it2.c = it2.char_to_display = *p, it2.len = 1;
25014 if (! ASCII_CHAR_P (it2.c))
25015 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25016 }
25017
25018 it2.glyph_row = NULL;
25019 it2.what = IT_CHARACTER;
25020 x_produce_glyphs (&it2);
25021 width = NUMVAL (prop) * it2.pixel_width;
25022 }
25023 #endif /* HAVE_WINDOW_SYSTEM */
25024 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25025 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25026 {
25027 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25028 align_to = (align_to < 0
25029 ? 0
25030 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25031 else if (align_to < 0)
25032 align_to = window_box_left_offset (it->w, TEXT_AREA);
25033 width = max (0, (int)tem + align_to - it->current_x);
25034 zero_width_ok_p = 1;
25035 }
25036 else
25037 /* Nothing specified -> width defaults to canonical char width. */
25038 width = FRAME_COLUMN_WIDTH (it->f);
25039
25040 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25041 width = 1;
25042
25043 #ifdef HAVE_WINDOW_SYSTEM
25044 /* Compute height. */
25045 if (FRAME_WINDOW_P (it->f))
25046 {
25047 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25048 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25049 {
25050 height = (int)tem;
25051 zero_height_ok_p = 1;
25052 }
25053 else if (prop = Fplist_get (plist, QCrelative_height),
25054 NUMVAL (prop) > 0)
25055 height = FONT_HEIGHT (font) * NUMVAL (prop);
25056 else
25057 height = FONT_HEIGHT (font);
25058
25059 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25060 height = 1;
25061
25062 /* Compute percentage of height used for ascent. If
25063 `:ascent ASCENT' is present and valid, use that. Otherwise,
25064 derive the ascent from the font in use. */
25065 if (prop = Fplist_get (plist, QCascent),
25066 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25067 ascent = height * NUMVAL (prop) / 100.0;
25068 else if (!NILP (prop)
25069 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25070 ascent = min (max (0, (int)tem), height);
25071 else
25072 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25073 }
25074 else
25075 #endif /* HAVE_WINDOW_SYSTEM */
25076 height = 1;
25077
25078 if (width > 0 && it->line_wrap != TRUNCATE
25079 && it->current_x + width > it->last_visible_x)
25080 {
25081 width = it->last_visible_x - it->current_x;
25082 #ifdef HAVE_WINDOW_SYSTEM
25083 /* Subtract one more pixel from the stretch width, but only on
25084 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25085 width -= FRAME_WINDOW_P (it->f);
25086 #endif
25087 }
25088
25089 if (width > 0 && height > 0 && it->glyph_row)
25090 {
25091 Lisp_Object o_object = it->object;
25092 Lisp_Object object = it->stack[it->sp - 1].string;
25093 int n = width;
25094
25095 if (!STRINGP (object))
25096 object = it->w->contents;
25097 #ifdef HAVE_WINDOW_SYSTEM
25098 if (FRAME_WINDOW_P (it->f))
25099 append_stretch_glyph (it, object, width, height, ascent);
25100 else
25101 #endif
25102 {
25103 it->object = object;
25104 it->char_to_display = ' ';
25105 it->pixel_width = it->len = 1;
25106 while (n--)
25107 tty_append_glyph (it);
25108 it->object = o_object;
25109 }
25110 }
25111
25112 it->pixel_width = width;
25113 #ifdef HAVE_WINDOW_SYSTEM
25114 if (FRAME_WINDOW_P (it->f))
25115 {
25116 it->ascent = it->phys_ascent = ascent;
25117 it->descent = it->phys_descent = height - it->ascent;
25118 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25119 take_vertical_position_into_account (it);
25120 }
25121 else
25122 #endif
25123 it->nglyphs = width;
25124 }
25125
25126 /* Get information about special display element WHAT in an
25127 environment described by IT. WHAT is one of IT_TRUNCATION or
25128 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25129 non-null glyph_row member. This function ensures that fields like
25130 face_id, c, len of IT are left untouched. */
25131
25132 static void
25133 produce_special_glyphs (struct it *it, enum display_element_type what)
25134 {
25135 struct it temp_it;
25136 Lisp_Object gc;
25137 GLYPH glyph;
25138
25139 temp_it = *it;
25140 temp_it.object = make_number (0);
25141 memset (&temp_it.current, 0, sizeof temp_it.current);
25142
25143 if (what == IT_CONTINUATION)
25144 {
25145 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25146 if (it->bidi_it.paragraph_dir == R2L)
25147 SET_GLYPH_FROM_CHAR (glyph, '/');
25148 else
25149 SET_GLYPH_FROM_CHAR (glyph, '\\');
25150 if (it->dp
25151 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25152 {
25153 /* FIXME: Should we mirror GC for R2L lines? */
25154 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25155 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25156 }
25157 }
25158 else if (what == IT_TRUNCATION)
25159 {
25160 /* Truncation glyph. */
25161 SET_GLYPH_FROM_CHAR (glyph, '$');
25162 if (it->dp
25163 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25164 {
25165 /* FIXME: Should we mirror GC for R2L lines? */
25166 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25167 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25168 }
25169 }
25170 else
25171 emacs_abort ();
25172
25173 #ifdef HAVE_WINDOW_SYSTEM
25174 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25175 is turned off, we precede the truncation/continuation glyphs by a
25176 stretch glyph whose width is computed such that these special
25177 glyphs are aligned at the window margin, even when very different
25178 fonts are used in different glyph rows. */
25179 if (FRAME_WINDOW_P (temp_it.f)
25180 /* init_iterator calls this with it->glyph_row == NULL, and it
25181 wants only the pixel width of the truncation/continuation
25182 glyphs. */
25183 && temp_it.glyph_row
25184 /* insert_left_trunc_glyphs calls us at the beginning of the
25185 row, and it has its own calculation of the stretch glyph
25186 width. */
25187 && temp_it.glyph_row->used[TEXT_AREA] > 0
25188 && (temp_it.glyph_row->reversed_p
25189 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25190 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25191 {
25192 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25193
25194 if (stretch_width > 0)
25195 {
25196 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25197 struct font *font =
25198 face->font ? face->font : FRAME_FONT (temp_it.f);
25199 int stretch_ascent =
25200 (((temp_it.ascent + temp_it.descent)
25201 * FONT_BASE (font)) / FONT_HEIGHT (font));
25202
25203 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25204 temp_it.ascent + temp_it.descent,
25205 stretch_ascent);
25206 }
25207 }
25208 #endif
25209
25210 temp_it.dp = NULL;
25211 temp_it.what = IT_CHARACTER;
25212 temp_it.len = 1;
25213 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25214 temp_it.face_id = GLYPH_FACE (glyph);
25215 temp_it.len = CHAR_BYTES (temp_it.c);
25216
25217 PRODUCE_GLYPHS (&temp_it);
25218 it->pixel_width = temp_it.pixel_width;
25219 it->nglyphs = temp_it.pixel_width;
25220 }
25221
25222 #ifdef HAVE_WINDOW_SYSTEM
25223
25224 /* Calculate line-height and line-spacing properties.
25225 An integer value specifies explicit pixel value.
25226 A float value specifies relative value to current face height.
25227 A cons (float . face-name) specifies relative value to
25228 height of specified face font.
25229
25230 Returns height in pixels, or nil. */
25231
25232
25233 static Lisp_Object
25234 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25235 int boff, int override)
25236 {
25237 Lisp_Object face_name = Qnil;
25238 int ascent, descent, height;
25239
25240 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25241 return val;
25242
25243 if (CONSP (val))
25244 {
25245 face_name = XCAR (val);
25246 val = XCDR (val);
25247 if (!NUMBERP (val))
25248 val = make_number (1);
25249 if (NILP (face_name))
25250 {
25251 height = it->ascent + it->descent;
25252 goto scale;
25253 }
25254 }
25255
25256 if (NILP (face_name))
25257 {
25258 font = FRAME_FONT (it->f);
25259 boff = FRAME_BASELINE_OFFSET (it->f);
25260 }
25261 else if (EQ (face_name, Qt))
25262 {
25263 override = 0;
25264 }
25265 else
25266 {
25267 int face_id;
25268 struct face *face;
25269
25270 face_id = lookup_named_face (it->f, face_name, 0);
25271 if (face_id < 0)
25272 return make_number (-1);
25273
25274 face = FACE_FROM_ID (it->f, face_id);
25275 font = face->font;
25276 if (font == NULL)
25277 return make_number (-1);
25278 boff = font->baseline_offset;
25279 if (font->vertical_centering)
25280 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25281 }
25282
25283 ascent = FONT_BASE (font) + boff;
25284 descent = FONT_DESCENT (font) - boff;
25285
25286 if (override)
25287 {
25288 it->override_ascent = ascent;
25289 it->override_descent = descent;
25290 it->override_boff = boff;
25291 }
25292
25293 height = ascent + descent;
25294
25295 scale:
25296 if (FLOATP (val))
25297 height = (int)(XFLOAT_DATA (val) * height);
25298 else if (INTEGERP (val))
25299 height *= XINT (val);
25300
25301 return make_number (height);
25302 }
25303
25304
25305 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25306 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25307 and only if this is for a character for which no font was found.
25308
25309 If the display method (it->glyphless_method) is
25310 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25311 length of the acronym or the hexadecimal string, UPPER_XOFF and
25312 UPPER_YOFF are pixel offsets for the upper part of the string,
25313 LOWER_XOFF and LOWER_YOFF are for the lower part.
25314
25315 For the other display methods, LEN through LOWER_YOFF are zero. */
25316
25317 static void
25318 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25319 short upper_xoff, short upper_yoff,
25320 short lower_xoff, short lower_yoff)
25321 {
25322 struct glyph *glyph;
25323 enum glyph_row_area area = it->area;
25324
25325 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25326 if (glyph < it->glyph_row->glyphs[area + 1])
25327 {
25328 /* If the glyph row is reversed, we need to prepend the glyph
25329 rather than append it. */
25330 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25331 {
25332 struct glyph *g;
25333
25334 /* Make room for the additional glyph. */
25335 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25336 g[1] = *g;
25337 glyph = it->glyph_row->glyphs[area];
25338 }
25339 glyph->charpos = CHARPOS (it->position);
25340 glyph->object = it->object;
25341 glyph->pixel_width = it->pixel_width;
25342 glyph->ascent = it->ascent;
25343 glyph->descent = it->descent;
25344 glyph->voffset = it->voffset;
25345 glyph->type = GLYPHLESS_GLYPH;
25346 glyph->u.glyphless.method = it->glyphless_method;
25347 glyph->u.glyphless.for_no_font = for_no_font;
25348 glyph->u.glyphless.len = len;
25349 glyph->u.glyphless.ch = it->c;
25350 glyph->slice.glyphless.upper_xoff = upper_xoff;
25351 glyph->slice.glyphless.upper_yoff = upper_yoff;
25352 glyph->slice.glyphless.lower_xoff = lower_xoff;
25353 glyph->slice.glyphless.lower_yoff = lower_yoff;
25354 glyph->avoid_cursor_p = it->avoid_cursor_p;
25355 glyph->multibyte_p = it->multibyte_p;
25356 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25357 {
25358 /* In R2L rows, the left and the right box edges need to be
25359 drawn in reverse direction. */
25360 glyph->right_box_line_p = it->start_of_box_run_p;
25361 glyph->left_box_line_p = it->end_of_box_run_p;
25362 }
25363 else
25364 {
25365 glyph->left_box_line_p = it->start_of_box_run_p;
25366 glyph->right_box_line_p = it->end_of_box_run_p;
25367 }
25368 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25369 || it->phys_descent > it->descent);
25370 glyph->padding_p = 0;
25371 glyph->glyph_not_available_p = 0;
25372 glyph->face_id = face_id;
25373 glyph->font_type = FONT_TYPE_UNKNOWN;
25374 if (it->bidi_p)
25375 {
25376 glyph->resolved_level = it->bidi_it.resolved_level;
25377 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25378 emacs_abort ();
25379 glyph->bidi_type = it->bidi_it.type;
25380 }
25381 ++it->glyph_row->used[area];
25382 }
25383 else
25384 IT_EXPAND_MATRIX_WIDTH (it, area);
25385 }
25386
25387
25388 /* Produce a glyph for a glyphless character for iterator IT.
25389 IT->glyphless_method specifies which method to use for displaying
25390 the character. See the description of enum
25391 glyphless_display_method in dispextern.h for the detail.
25392
25393 FOR_NO_FONT is nonzero if and only if this is for a character for
25394 which no font was found. ACRONYM, if non-nil, is an acronym string
25395 for the character. */
25396
25397 static void
25398 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25399 {
25400 int face_id;
25401 struct face *face;
25402 struct font *font;
25403 int base_width, base_height, width, height;
25404 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25405 int len;
25406
25407 /* Get the metrics of the base font. We always refer to the current
25408 ASCII face. */
25409 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25410 font = face->font ? face->font : FRAME_FONT (it->f);
25411 it->ascent = FONT_BASE (font) + font->baseline_offset;
25412 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25413 base_height = it->ascent + it->descent;
25414 base_width = font->average_width;
25415
25416 face_id = merge_glyphless_glyph_face (it);
25417
25418 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25419 {
25420 it->pixel_width = THIN_SPACE_WIDTH;
25421 len = 0;
25422 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25423 }
25424 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25425 {
25426 width = CHAR_WIDTH (it->c);
25427 if (width == 0)
25428 width = 1;
25429 else if (width > 4)
25430 width = 4;
25431 it->pixel_width = base_width * width;
25432 len = 0;
25433 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25434 }
25435 else
25436 {
25437 char buf[7];
25438 const char *str;
25439 unsigned int code[6];
25440 int upper_len;
25441 int ascent, descent;
25442 struct font_metrics metrics_upper, metrics_lower;
25443
25444 face = FACE_FROM_ID (it->f, face_id);
25445 font = face->font ? face->font : FRAME_FONT (it->f);
25446 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25447
25448 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25449 {
25450 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25451 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25452 if (CONSP (acronym))
25453 acronym = XCAR (acronym);
25454 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25455 }
25456 else
25457 {
25458 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25459 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25460 str = buf;
25461 }
25462 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25463 code[len] = font->driver->encode_char (font, str[len]);
25464 upper_len = (len + 1) / 2;
25465 font->driver->text_extents (font, code, upper_len,
25466 &metrics_upper);
25467 font->driver->text_extents (font, code + upper_len, len - upper_len,
25468 &metrics_lower);
25469
25470
25471
25472 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25473 width = max (metrics_upper.width, metrics_lower.width) + 4;
25474 upper_xoff = upper_yoff = 2; /* the typical case */
25475 if (base_width >= width)
25476 {
25477 /* Align the upper to the left, the lower to the right. */
25478 it->pixel_width = base_width;
25479 lower_xoff = base_width - 2 - metrics_lower.width;
25480 }
25481 else
25482 {
25483 /* Center the shorter one. */
25484 it->pixel_width = width;
25485 if (metrics_upper.width >= metrics_lower.width)
25486 lower_xoff = (width - metrics_lower.width) / 2;
25487 else
25488 {
25489 /* FIXME: This code doesn't look right. It formerly was
25490 missing the "lower_xoff = 0;", which couldn't have
25491 been right since it left lower_xoff uninitialized. */
25492 lower_xoff = 0;
25493 upper_xoff = (width - metrics_upper.width) / 2;
25494 }
25495 }
25496
25497 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25498 top, bottom, and between upper and lower strings. */
25499 height = (metrics_upper.ascent + metrics_upper.descent
25500 + metrics_lower.ascent + metrics_lower.descent) + 5;
25501 /* Center vertically.
25502 H:base_height, D:base_descent
25503 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25504
25505 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25506 descent = D - H/2 + h/2;
25507 lower_yoff = descent - 2 - ld;
25508 upper_yoff = lower_yoff - la - 1 - ud; */
25509 ascent = - (it->descent - (base_height + height + 1) / 2);
25510 descent = it->descent - (base_height - height) / 2;
25511 lower_yoff = descent - 2 - metrics_lower.descent;
25512 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25513 - metrics_upper.descent);
25514 /* Don't make the height shorter than the base height. */
25515 if (height > base_height)
25516 {
25517 it->ascent = ascent;
25518 it->descent = descent;
25519 }
25520 }
25521
25522 it->phys_ascent = it->ascent;
25523 it->phys_descent = it->descent;
25524 if (it->glyph_row)
25525 append_glyphless_glyph (it, face_id, for_no_font, len,
25526 upper_xoff, upper_yoff,
25527 lower_xoff, lower_yoff);
25528 it->nglyphs = 1;
25529 take_vertical_position_into_account (it);
25530 }
25531
25532
25533 /* RIF:
25534 Produce glyphs/get display metrics for the display element IT is
25535 loaded with. See the description of struct it in dispextern.h
25536 for an overview of struct it. */
25537
25538 void
25539 x_produce_glyphs (struct it *it)
25540 {
25541 int extra_line_spacing = it->extra_line_spacing;
25542
25543 it->glyph_not_available_p = 0;
25544
25545 if (it->what == IT_CHARACTER)
25546 {
25547 XChar2b char2b;
25548 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25549 struct font *font = face->font;
25550 struct font_metrics *pcm = NULL;
25551 int boff; /* Baseline offset. */
25552
25553 if (font == NULL)
25554 {
25555 /* When no suitable font is found, display this character by
25556 the method specified in the first extra slot of
25557 Vglyphless_char_display. */
25558 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25559
25560 eassert (it->what == IT_GLYPHLESS);
25561 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25562 goto done;
25563 }
25564
25565 boff = font->baseline_offset;
25566 if (font->vertical_centering)
25567 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25568
25569 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25570 {
25571 int stretched_p;
25572
25573 it->nglyphs = 1;
25574
25575 if (it->override_ascent >= 0)
25576 {
25577 it->ascent = it->override_ascent;
25578 it->descent = it->override_descent;
25579 boff = it->override_boff;
25580 }
25581 else
25582 {
25583 it->ascent = FONT_BASE (font) + boff;
25584 it->descent = FONT_DESCENT (font) - boff;
25585 }
25586
25587 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25588 {
25589 pcm = get_per_char_metric (font, &char2b);
25590 if (pcm->width == 0
25591 && pcm->rbearing == 0 && pcm->lbearing == 0)
25592 pcm = NULL;
25593 }
25594
25595 if (pcm)
25596 {
25597 it->phys_ascent = pcm->ascent + boff;
25598 it->phys_descent = pcm->descent - boff;
25599 it->pixel_width = pcm->width;
25600 }
25601 else
25602 {
25603 it->glyph_not_available_p = 1;
25604 it->phys_ascent = it->ascent;
25605 it->phys_descent = it->descent;
25606 it->pixel_width = font->space_width;
25607 }
25608
25609 if (it->constrain_row_ascent_descent_p)
25610 {
25611 if (it->descent > it->max_descent)
25612 {
25613 it->ascent += it->descent - it->max_descent;
25614 it->descent = it->max_descent;
25615 }
25616 if (it->ascent > it->max_ascent)
25617 {
25618 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25619 it->ascent = it->max_ascent;
25620 }
25621 it->phys_ascent = min (it->phys_ascent, it->ascent);
25622 it->phys_descent = min (it->phys_descent, it->descent);
25623 extra_line_spacing = 0;
25624 }
25625
25626 /* If this is a space inside a region of text with
25627 `space-width' property, change its width. */
25628 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25629 if (stretched_p)
25630 it->pixel_width *= XFLOATINT (it->space_width);
25631
25632 /* If face has a box, add the box thickness to the character
25633 height. If character has a box line to the left and/or
25634 right, add the box line width to the character's width. */
25635 if (face->box != FACE_NO_BOX)
25636 {
25637 int thick = face->box_line_width;
25638
25639 if (thick > 0)
25640 {
25641 it->ascent += thick;
25642 it->descent += thick;
25643 }
25644 else
25645 thick = -thick;
25646
25647 if (it->start_of_box_run_p)
25648 it->pixel_width += thick;
25649 if (it->end_of_box_run_p)
25650 it->pixel_width += thick;
25651 }
25652
25653 /* If face has an overline, add the height of the overline
25654 (1 pixel) and a 1 pixel margin to the character height. */
25655 if (face->overline_p)
25656 it->ascent += overline_margin;
25657
25658 if (it->constrain_row_ascent_descent_p)
25659 {
25660 if (it->ascent > it->max_ascent)
25661 it->ascent = it->max_ascent;
25662 if (it->descent > it->max_descent)
25663 it->descent = it->max_descent;
25664 }
25665
25666 take_vertical_position_into_account (it);
25667
25668 /* If we have to actually produce glyphs, do it. */
25669 if (it->glyph_row)
25670 {
25671 if (stretched_p)
25672 {
25673 /* Translate a space with a `space-width' property
25674 into a stretch glyph. */
25675 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25676 / FONT_HEIGHT (font));
25677 append_stretch_glyph (it, it->object, it->pixel_width,
25678 it->ascent + it->descent, ascent);
25679 }
25680 else
25681 append_glyph (it);
25682
25683 /* If characters with lbearing or rbearing are displayed
25684 in this line, record that fact in a flag of the
25685 glyph row. This is used to optimize X output code. */
25686 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25687 it->glyph_row->contains_overlapping_glyphs_p = 1;
25688 }
25689 if (! stretched_p && it->pixel_width == 0)
25690 /* We assure that all visible glyphs have at least 1-pixel
25691 width. */
25692 it->pixel_width = 1;
25693 }
25694 else if (it->char_to_display == '\n')
25695 {
25696 /* A newline has no width, but we need the height of the
25697 line. But if previous part of the line sets a height,
25698 don't increase that height. */
25699
25700 Lisp_Object height;
25701 Lisp_Object total_height = Qnil;
25702
25703 it->override_ascent = -1;
25704 it->pixel_width = 0;
25705 it->nglyphs = 0;
25706
25707 height = get_it_property (it, Qline_height);
25708 /* Split (line-height total-height) list. */
25709 if (CONSP (height)
25710 && CONSP (XCDR (height))
25711 && NILP (XCDR (XCDR (height))))
25712 {
25713 total_height = XCAR (XCDR (height));
25714 height = XCAR (height);
25715 }
25716 height = calc_line_height_property (it, height, font, boff, 1);
25717
25718 if (it->override_ascent >= 0)
25719 {
25720 it->ascent = it->override_ascent;
25721 it->descent = it->override_descent;
25722 boff = it->override_boff;
25723 }
25724 else
25725 {
25726 it->ascent = FONT_BASE (font) + boff;
25727 it->descent = FONT_DESCENT (font) - boff;
25728 }
25729
25730 if (EQ (height, Qt))
25731 {
25732 if (it->descent > it->max_descent)
25733 {
25734 it->ascent += it->descent - it->max_descent;
25735 it->descent = it->max_descent;
25736 }
25737 if (it->ascent > it->max_ascent)
25738 {
25739 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25740 it->ascent = it->max_ascent;
25741 }
25742 it->phys_ascent = min (it->phys_ascent, it->ascent);
25743 it->phys_descent = min (it->phys_descent, it->descent);
25744 it->constrain_row_ascent_descent_p = 1;
25745 extra_line_spacing = 0;
25746 }
25747 else
25748 {
25749 Lisp_Object spacing;
25750
25751 it->phys_ascent = it->ascent;
25752 it->phys_descent = it->descent;
25753
25754 if ((it->max_ascent > 0 || it->max_descent > 0)
25755 && face->box != FACE_NO_BOX
25756 && face->box_line_width > 0)
25757 {
25758 it->ascent += face->box_line_width;
25759 it->descent += face->box_line_width;
25760 }
25761 if (!NILP (height)
25762 && XINT (height) > it->ascent + it->descent)
25763 it->ascent = XINT (height) - it->descent;
25764
25765 if (!NILP (total_height))
25766 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25767 else
25768 {
25769 spacing = get_it_property (it, Qline_spacing);
25770 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25771 }
25772 if (INTEGERP (spacing))
25773 {
25774 extra_line_spacing = XINT (spacing);
25775 if (!NILP (total_height))
25776 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25777 }
25778 }
25779 }
25780 else /* i.e. (it->char_to_display == '\t') */
25781 {
25782 if (font->space_width > 0)
25783 {
25784 int tab_width = it->tab_width * font->space_width;
25785 int x = it->current_x + it->continuation_lines_width;
25786 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25787
25788 /* If the distance from the current position to the next tab
25789 stop is less than a space character width, use the
25790 tab stop after that. */
25791 if (next_tab_x - x < font->space_width)
25792 next_tab_x += tab_width;
25793
25794 it->pixel_width = next_tab_x - x;
25795 it->nglyphs = 1;
25796 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25797 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25798
25799 if (it->glyph_row)
25800 {
25801 append_stretch_glyph (it, it->object, it->pixel_width,
25802 it->ascent + it->descent, it->ascent);
25803 }
25804 }
25805 else
25806 {
25807 it->pixel_width = 0;
25808 it->nglyphs = 1;
25809 }
25810 }
25811 }
25812 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25813 {
25814 /* A static composition.
25815
25816 Note: A composition is represented as one glyph in the
25817 glyph matrix. There are no padding glyphs.
25818
25819 Important note: pixel_width, ascent, and descent are the
25820 values of what is drawn by draw_glyphs (i.e. the values of
25821 the overall glyphs composed). */
25822 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25823 int boff; /* baseline offset */
25824 struct composition *cmp = composition_table[it->cmp_it.id];
25825 int glyph_len = cmp->glyph_len;
25826 struct font *font = face->font;
25827
25828 it->nglyphs = 1;
25829
25830 /* If we have not yet calculated pixel size data of glyphs of
25831 the composition for the current face font, calculate them
25832 now. Theoretically, we have to check all fonts for the
25833 glyphs, but that requires much time and memory space. So,
25834 here we check only the font of the first glyph. This may
25835 lead to incorrect display, but it's very rare, and C-l
25836 (recenter-top-bottom) can correct the display anyway. */
25837 if (! cmp->font || cmp->font != font)
25838 {
25839 /* Ascent and descent of the font of the first character
25840 of this composition (adjusted by baseline offset).
25841 Ascent and descent of overall glyphs should not be less
25842 than these, respectively. */
25843 int font_ascent, font_descent, font_height;
25844 /* Bounding box of the overall glyphs. */
25845 int leftmost, rightmost, lowest, highest;
25846 int lbearing, rbearing;
25847 int i, width, ascent, descent;
25848 int left_padded = 0, right_padded = 0;
25849 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25850 XChar2b char2b;
25851 struct font_metrics *pcm;
25852 int font_not_found_p;
25853 ptrdiff_t pos;
25854
25855 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25856 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25857 break;
25858 if (glyph_len < cmp->glyph_len)
25859 right_padded = 1;
25860 for (i = 0; i < glyph_len; i++)
25861 {
25862 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25863 break;
25864 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25865 }
25866 if (i > 0)
25867 left_padded = 1;
25868
25869 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25870 : IT_CHARPOS (*it));
25871 /* If no suitable font is found, use the default font. */
25872 font_not_found_p = font == NULL;
25873 if (font_not_found_p)
25874 {
25875 face = face->ascii_face;
25876 font = face->font;
25877 }
25878 boff = font->baseline_offset;
25879 if (font->vertical_centering)
25880 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25881 font_ascent = FONT_BASE (font) + boff;
25882 font_descent = FONT_DESCENT (font) - boff;
25883 font_height = FONT_HEIGHT (font);
25884
25885 cmp->font = font;
25886
25887 pcm = NULL;
25888 if (! font_not_found_p)
25889 {
25890 get_char_face_and_encoding (it->f, c, it->face_id,
25891 &char2b, 0);
25892 pcm = get_per_char_metric (font, &char2b);
25893 }
25894
25895 /* Initialize the bounding box. */
25896 if (pcm)
25897 {
25898 width = cmp->glyph_len > 0 ? pcm->width : 0;
25899 ascent = pcm->ascent;
25900 descent = pcm->descent;
25901 lbearing = pcm->lbearing;
25902 rbearing = pcm->rbearing;
25903 }
25904 else
25905 {
25906 width = cmp->glyph_len > 0 ? font->space_width : 0;
25907 ascent = FONT_BASE (font);
25908 descent = FONT_DESCENT (font);
25909 lbearing = 0;
25910 rbearing = width;
25911 }
25912
25913 rightmost = width;
25914 leftmost = 0;
25915 lowest = - descent + boff;
25916 highest = ascent + boff;
25917
25918 if (! font_not_found_p
25919 && font->default_ascent
25920 && CHAR_TABLE_P (Vuse_default_ascent)
25921 && !NILP (Faref (Vuse_default_ascent,
25922 make_number (it->char_to_display))))
25923 highest = font->default_ascent + boff;
25924
25925 /* Draw the first glyph at the normal position. It may be
25926 shifted to right later if some other glyphs are drawn
25927 at the left. */
25928 cmp->offsets[i * 2] = 0;
25929 cmp->offsets[i * 2 + 1] = boff;
25930 cmp->lbearing = lbearing;
25931 cmp->rbearing = rbearing;
25932
25933 /* Set cmp->offsets for the remaining glyphs. */
25934 for (i++; i < glyph_len; i++)
25935 {
25936 int left, right, btm, top;
25937 int ch = COMPOSITION_GLYPH (cmp, i);
25938 int face_id;
25939 struct face *this_face;
25940
25941 if (ch == '\t')
25942 ch = ' ';
25943 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25944 this_face = FACE_FROM_ID (it->f, face_id);
25945 font = this_face->font;
25946
25947 if (font == NULL)
25948 pcm = NULL;
25949 else
25950 {
25951 get_char_face_and_encoding (it->f, ch, face_id,
25952 &char2b, 0);
25953 pcm = get_per_char_metric (font, &char2b);
25954 }
25955 if (! pcm)
25956 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25957 else
25958 {
25959 width = pcm->width;
25960 ascent = pcm->ascent;
25961 descent = pcm->descent;
25962 lbearing = pcm->lbearing;
25963 rbearing = pcm->rbearing;
25964 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25965 {
25966 /* Relative composition with or without
25967 alternate chars. */
25968 left = (leftmost + rightmost - width) / 2;
25969 btm = - descent + boff;
25970 if (font->relative_compose
25971 && (! CHAR_TABLE_P (Vignore_relative_composition)
25972 || NILP (Faref (Vignore_relative_composition,
25973 make_number (ch)))))
25974 {
25975
25976 if (- descent >= font->relative_compose)
25977 /* One extra pixel between two glyphs. */
25978 btm = highest + 1;
25979 else if (ascent <= 0)
25980 /* One extra pixel between two glyphs. */
25981 btm = lowest - 1 - ascent - descent;
25982 }
25983 }
25984 else
25985 {
25986 /* A composition rule is specified by an integer
25987 value that encodes global and new reference
25988 points (GREF and NREF). GREF and NREF are
25989 specified by numbers as below:
25990
25991 0---1---2 -- ascent
25992 | |
25993 | |
25994 | |
25995 9--10--11 -- center
25996 | |
25997 ---3---4---5--- baseline
25998 | |
25999 6---7---8 -- descent
26000 */
26001 int rule = COMPOSITION_RULE (cmp, i);
26002 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26003
26004 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26005 grefx = gref % 3, nrefx = nref % 3;
26006 grefy = gref / 3, nrefy = nref / 3;
26007 if (xoff)
26008 xoff = font_height * (xoff - 128) / 256;
26009 if (yoff)
26010 yoff = font_height * (yoff - 128) / 256;
26011
26012 left = (leftmost
26013 + grefx * (rightmost - leftmost) / 2
26014 - nrefx * width / 2
26015 + xoff);
26016
26017 btm = ((grefy == 0 ? highest
26018 : grefy == 1 ? 0
26019 : grefy == 2 ? lowest
26020 : (highest + lowest) / 2)
26021 - (nrefy == 0 ? ascent + descent
26022 : nrefy == 1 ? descent - boff
26023 : nrefy == 2 ? 0
26024 : (ascent + descent) / 2)
26025 + yoff);
26026 }
26027
26028 cmp->offsets[i * 2] = left;
26029 cmp->offsets[i * 2 + 1] = btm + descent;
26030
26031 /* Update the bounding box of the overall glyphs. */
26032 if (width > 0)
26033 {
26034 right = left + width;
26035 if (left < leftmost)
26036 leftmost = left;
26037 if (right > rightmost)
26038 rightmost = right;
26039 }
26040 top = btm + descent + ascent;
26041 if (top > highest)
26042 highest = top;
26043 if (btm < lowest)
26044 lowest = btm;
26045
26046 if (cmp->lbearing > left + lbearing)
26047 cmp->lbearing = left + lbearing;
26048 if (cmp->rbearing < left + rbearing)
26049 cmp->rbearing = left + rbearing;
26050 }
26051 }
26052
26053 /* If there are glyphs whose x-offsets are negative,
26054 shift all glyphs to the right and make all x-offsets
26055 non-negative. */
26056 if (leftmost < 0)
26057 {
26058 for (i = 0; i < cmp->glyph_len; i++)
26059 cmp->offsets[i * 2] -= leftmost;
26060 rightmost -= leftmost;
26061 cmp->lbearing -= leftmost;
26062 cmp->rbearing -= leftmost;
26063 }
26064
26065 if (left_padded && cmp->lbearing < 0)
26066 {
26067 for (i = 0; i < cmp->glyph_len; i++)
26068 cmp->offsets[i * 2] -= cmp->lbearing;
26069 rightmost -= cmp->lbearing;
26070 cmp->rbearing -= cmp->lbearing;
26071 cmp->lbearing = 0;
26072 }
26073 if (right_padded && rightmost < cmp->rbearing)
26074 {
26075 rightmost = cmp->rbearing;
26076 }
26077
26078 cmp->pixel_width = rightmost;
26079 cmp->ascent = highest;
26080 cmp->descent = - lowest;
26081 if (cmp->ascent < font_ascent)
26082 cmp->ascent = font_ascent;
26083 if (cmp->descent < font_descent)
26084 cmp->descent = font_descent;
26085 }
26086
26087 if (it->glyph_row
26088 && (cmp->lbearing < 0
26089 || cmp->rbearing > cmp->pixel_width))
26090 it->glyph_row->contains_overlapping_glyphs_p = 1;
26091
26092 it->pixel_width = cmp->pixel_width;
26093 it->ascent = it->phys_ascent = cmp->ascent;
26094 it->descent = it->phys_descent = cmp->descent;
26095 if (face->box != FACE_NO_BOX)
26096 {
26097 int thick = face->box_line_width;
26098
26099 if (thick > 0)
26100 {
26101 it->ascent += thick;
26102 it->descent += thick;
26103 }
26104 else
26105 thick = - thick;
26106
26107 if (it->start_of_box_run_p)
26108 it->pixel_width += thick;
26109 if (it->end_of_box_run_p)
26110 it->pixel_width += thick;
26111 }
26112
26113 /* If face has an overline, add the height of the overline
26114 (1 pixel) and a 1 pixel margin to the character height. */
26115 if (face->overline_p)
26116 it->ascent += overline_margin;
26117
26118 take_vertical_position_into_account (it);
26119 if (it->ascent < 0)
26120 it->ascent = 0;
26121 if (it->descent < 0)
26122 it->descent = 0;
26123
26124 if (it->glyph_row && cmp->glyph_len > 0)
26125 append_composite_glyph (it);
26126 }
26127 else if (it->what == IT_COMPOSITION)
26128 {
26129 /* A dynamic (automatic) composition. */
26130 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26131 Lisp_Object gstring;
26132 struct font_metrics metrics;
26133
26134 it->nglyphs = 1;
26135
26136 gstring = composition_gstring_from_id (it->cmp_it.id);
26137 it->pixel_width
26138 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26139 &metrics);
26140 if (it->glyph_row
26141 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26142 it->glyph_row->contains_overlapping_glyphs_p = 1;
26143 it->ascent = it->phys_ascent = metrics.ascent;
26144 it->descent = it->phys_descent = metrics.descent;
26145 if (face->box != FACE_NO_BOX)
26146 {
26147 int thick = face->box_line_width;
26148
26149 if (thick > 0)
26150 {
26151 it->ascent += thick;
26152 it->descent += thick;
26153 }
26154 else
26155 thick = - thick;
26156
26157 if (it->start_of_box_run_p)
26158 it->pixel_width += thick;
26159 if (it->end_of_box_run_p)
26160 it->pixel_width += thick;
26161 }
26162 /* If face has an overline, add the height of the overline
26163 (1 pixel) and a 1 pixel margin to the character height. */
26164 if (face->overline_p)
26165 it->ascent += overline_margin;
26166 take_vertical_position_into_account (it);
26167 if (it->ascent < 0)
26168 it->ascent = 0;
26169 if (it->descent < 0)
26170 it->descent = 0;
26171
26172 if (it->glyph_row)
26173 append_composite_glyph (it);
26174 }
26175 else if (it->what == IT_GLYPHLESS)
26176 produce_glyphless_glyph (it, 0, Qnil);
26177 else if (it->what == IT_IMAGE)
26178 produce_image_glyph (it);
26179 else if (it->what == IT_STRETCH)
26180 produce_stretch_glyph (it);
26181
26182 done:
26183 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26184 because this isn't true for images with `:ascent 100'. */
26185 eassert (it->ascent >= 0 && it->descent >= 0);
26186 if (it->area == TEXT_AREA)
26187 it->current_x += it->pixel_width;
26188
26189 if (extra_line_spacing > 0)
26190 {
26191 it->descent += extra_line_spacing;
26192 if (extra_line_spacing > it->max_extra_line_spacing)
26193 it->max_extra_line_spacing = extra_line_spacing;
26194 }
26195
26196 it->max_ascent = max (it->max_ascent, it->ascent);
26197 it->max_descent = max (it->max_descent, it->descent);
26198 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26199 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26200 }
26201
26202 /* EXPORT for RIF:
26203 Output LEN glyphs starting at START at the nominal cursor position.
26204 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26205 being updated, and UPDATED_AREA is the area of that row being updated. */
26206
26207 void
26208 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26209 struct glyph *start, enum glyph_row_area updated_area, int len)
26210 {
26211 int x, hpos, chpos = w->phys_cursor.hpos;
26212
26213 eassert (updated_row);
26214 /* When the window is hscrolled, cursor hpos can legitimately be out
26215 of bounds, but we draw the cursor at the corresponding window
26216 margin in that case. */
26217 if (!updated_row->reversed_p && chpos < 0)
26218 chpos = 0;
26219 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26220 chpos = updated_row->used[TEXT_AREA] - 1;
26221
26222 block_input ();
26223
26224 /* Write glyphs. */
26225
26226 hpos = start - updated_row->glyphs[updated_area];
26227 x = draw_glyphs (w, w->output_cursor.x,
26228 updated_row, updated_area,
26229 hpos, hpos + len,
26230 DRAW_NORMAL_TEXT, 0);
26231
26232 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26233 if (updated_area == TEXT_AREA
26234 && w->phys_cursor_on_p
26235 && w->phys_cursor.vpos == w->output_cursor.vpos
26236 && chpos >= hpos
26237 && chpos < hpos + len)
26238 w->phys_cursor_on_p = 0;
26239
26240 unblock_input ();
26241
26242 /* Advance the output cursor. */
26243 w->output_cursor.hpos += len;
26244 w->output_cursor.x = x;
26245 }
26246
26247
26248 /* EXPORT for RIF:
26249 Insert LEN glyphs from START at the nominal cursor position. */
26250
26251 void
26252 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26253 struct glyph *start, enum glyph_row_area updated_area, int len)
26254 {
26255 struct frame *f;
26256 int line_height, shift_by_width, shifted_region_width;
26257 struct glyph_row *row;
26258 struct glyph *glyph;
26259 int frame_x, frame_y;
26260 ptrdiff_t hpos;
26261
26262 eassert (updated_row);
26263 block_input ();
26264 f = XFRAME (WINDOW_FRAME (w));
26265
26266 /* Get the height of the line we are in. */
26267 row = updated_row;
26268 line_height = row->height;
26269
26270 /* Get the width of the glyphs to insert. */
26271 shift_by_width = 0;
26272 for (glyph = start; glyph < start + len; ++glyph)
26273 shift_by_width += glyph->pixel_width;
26274
26275 /* Get the width of the region to shift right. */
26276 shifted_region_width = (window_box_width (w, updated_area)
26277 - w->output_cursor.x
26278 - shift_by_width);
26279
26280 /* Shift right. */
26281 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26282 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26283
26284 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26285 line_height, shift_by_width);
26286
26287 /* Write the glyphs. */
26288 hpos = start - row->glyphs[updated_area];
26289 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26290 hpos, hpos + len,
26291 DRAW_NORMAL_TEXT, 0);
26292
26293 /* Advance the output cursor. */
26294 w->output_cursor.hpos += len;
26295 w->output_cursor.x += shift_by_width;
26296 unblock_input ();
26297 }
26298
26299
26300 /* EXPORT for RIF:
26301 Erase the current text line from the nominal cursor position
26302 (inclusive) to pixel column TO_X (exclusive). The idea is that
26303 everything from TO_X onward is already erased.
26304
26305 TO_X is a pixel position relative to UPDATED_AREA of currently
26306 updated window W. TO_X == -1 means clear to the end of this area. */
26307
26308 void
26309 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26310 enum glyph_row_area updated_area, int to_x)
26311 {
26312 struct frame *f;
26313 int max_x, min_y, max_y;
26314 int from_x, from_y, to_y;
26315
26316 eassert (updated_row);
26317 f = XFRAME (w->frame);
26318
26319 if (updated_row->full_width_p)
26320 max_x = (WINDOW_PIXEL_WIDTH (w)
26321 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26322 else
26323 max_x = window_box_width (w, updated_area);
26324 max_y = window_text_bottom_y (w);
26325
26326 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26327 of window. For TO_X > 0, truncate to end of drawing area. */
26328 if (to_x == 0)
26329 return;
26330 else if (to_x < 0)
26331 to_x = max_x;
26332 else
26333 to_x = min (to_x, max_x);
26334
26335 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26336
26337 /* Notice if the cursor will be cleared by this operation. */
26338 if (!updated_row->full_width_p)
26339 notice_overwritten_cursor (w, updated_area,
26340 w->output_cursor.x, -1,
26341 updated_row->y,
26342 MATRIX_ROW_BOTTOM_Y (updated_row));
26343
26344 from_x = w->output_cursor.x;
26345
26346 /* Translate to frame coordinates. */
26347 if (updated_row->full_width_p)
26348 {
26349 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26350 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26351 }
26352 else
26353 {
26354 int area_left = window_box_left (w, updated_area);
26355 from_x += area_left;
26356 to_x += area_left;
26357 }
26358
26359 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26360 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26361 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26362
26363 /* Prevent inadvertently clearing to end of the X window. */
26364 if (to_x > from_x && to_y > from_y)
26365 {
26366 block_input ();
26367 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26368 to_x - from_x, to_y - from_y);
26369 unblock_input ();
26370 }
26371 }
26372
26373 #endif /* HAVE_WINDOW_SYSTEM */
26374
26375
26376 \f
26377 /***********************************************************************
26378 Cursor types
26379 ***********************************************************************/
26380
26381 /* Value is the internal representation of the specified cursor type
26382 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26383 of the bar cursor. */
26384
26385 static enum text_cursor_kinds
26386 get_specified_cursor_type (Lisp_Object arg, int *width)
26387 {
26388 enum text_cursor_kinds type;
26389
26390 if (NILP (arg))
26391 return NO_CURSOR;
26392
26393 if (EQ (arg, Qbox))
26394 return FILLED_BOX_CURSOR;
26395
26396 if (EQ (arg, Qhollow))
26397 return HOLLOW_BOX_CURSOR;
26398
26399 if (EQ (arg, Qbar))
26400 {
26401 *width = 2;
26402 return BAR_CURSOR;
26403 }
26404
26405 if (CONSP (arg)
26406 && EQ (XCAR (arg), Qbar)
26407 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26408 {
26409 *width = XINT (XCDR (arg));
26410 return BAR_CURSOR;
26411 }
26412
26413 if (EQ (arg, Qhbar))
26414 {
26415 *width = 2;
26416 return HBAR_CURSOR;
26417 }
26418
26419 if (CONSP (arg)
26420 && EQ (XCAR (arg), Qhbar)
26421 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26422 {
26423 *width = XINT (XCDR (arg));
26424 return HBAR_CURSOR;
26425 }
26426
26427 /* Treat anything unknown as "hollow box cursor".
26428 It was bad to signal an error; people have trouble fixing
26429 .Xdefaults with Emacs, when it has something bad in it. */
26430 type = HOLLOW_BOX_CURSOR;
26431
26432 return type;
26433 }
26434
26435 /* Set the default cursor types for specified frame. */
26436 void
26437 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26438 {
26439 int width = 1;
26440 Lisp_Object tem;
26441
26442 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26443 FRAME_CURSOR_WIDTH (f) = width;
26444
26445 /* By default, set up the blink-off state depending on the on-state. */
26446
26447 tem = Fassoc (arg, Vblink_cursor_alist);
26448 if (!NILP (tem))
26449 {
26450 FRAME_BLINK_OFF_CURSOR (f)
26451 = get_specified_cursor_type (XCDR (tem), &width);
26452 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26453 }
26454 else
26455 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26456
26457 /* Make sure the cursor gets redrawn. */
26458 f->cursor_type_changed = 1;
26459 }
26460
26461
26462 #ifdef HAVE_WINDOW_SYSTEM
26463
26464 /* Return the cursor we want to be displayed in window W. Return
26465 width of bar/hbar cursor through WIDTH arg. Return with
26466 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26467 (i.e. if the `system caret' should track this cursor).
26468
26469 In a mini-buffer window, we want the cursor only to appear if we
26470 are reading input from this window. For the selected window, we
26471 want the cursor type given by the frame parameter or buffer local
26472 setting of cursor-type. If explicitly marked off, draw no cursor.
26473 In all other cases, we want a hollow box cursor. */
26474
26475 static enum text_cursor_kinds
26476 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26477 int *active_cursor)
26478 {
26479 struct frame *f = XFRAME (w->frame);
26480 struct buffer *b = XBUFFER (w->contents);
26481 int cursor_type = DEFAULT_CURSOR;
26482 Lisp_Object alt_cursor;
26483 int non_selected = 0;
26484
26485 *active_cursor = 1;
26486
26487 /* Echo area */
26488 if (cursor_in_echo_area
26489 && FRAME_HAS_MINIBUF_P (f)
26490 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26491 {
26492 if (w == XWINDOW (echo_area_window))
26493 {
26494 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26495 {
26496 *width = FRAME_CURSOR_WIDTH (f);
26497 return FRAME_DESIRED_CURSOR (f);
26498 }
26499 else
26500 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26501 }
26502
26503 *active_cursor = 0;
26504 non_selected = 1;
26505 }
26506
26507 /* Detect a nonselected window or nonselected frame. */
26508 else if (w != XWINDOW (f->selected_window)
26509 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26510 {
26511 *active_cursor = 0;
26512
26513 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26514 return NO_CURSOR;
26515
26516 non_selected = 1;
26517 }
26518
26519 /* Never display a cursor in a window in which cursor-type is nil. */
26520 if (NILP (BVAR (b, cursor_type)))
26521 return NO_CURSOR;
26522
26523 /* Get the normal cursor type for this window. */
26524 if (EQ (BVAR (b, cursor_type), Qt))
26525 {
26526 cursor_type = FRAME_DESIRED_CURSOR (f);
26527 *width = FRAME_CURSOR_WIDTH (f);
26528 }
26529 else
26530 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26531
26532 /* Use cursor-in-non-selected-windows instead
26533 for non-selected window or frame. */
26534 if (non_selected)
26535 {
26536 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26537 if (!EQ (Qt, alt_cursor))
26538 return get_specified_cursor_type (alt_cursor, width);
26539 /* t means modify the normal cursor type. */
26540 if (cursor_type == FILLED_BOX_CURSOR)
26541 cursor_type = HOLLOW_BOX_CURSOR;
26542 else if (cursor_type == BAR_CURSOR && *width > 1)
26543 --*width;
26544 return cursor_type;
26545 }
26546
26547 /* Use normal cursor if not blinked off. */
26548 if (!w->cursor_off_p)
26549 {
26550 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26551 {
26552 if (cursor_type == FILLED_BOX_CURSOR)
26553 {
26554 /* Using a block cursor on large images can be very annoying.
26555 So use a hollow cursor for "large" images.
26556 If image is not transparent (no mask), also use hollow cursor. */
26557 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26558 if (img != NULL && IMAGEP (img->spec))
26559 {
26560 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26561 where N = size of default frame font size.
26562 This should cover most of the "tiny" icons people may use. */
26563 if (!img->mask
26564 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26565 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26566 cursor_type = HOLLOW_BOX_CURSOR;
26567 }
26568 }
26569 else if (cursor_type != NO_CURSOR)
26570 {
26571 /* Display current only supports BOX and HOLLOW cursors for images.
26572 So for now, unconditionally use a HOLLOW cursor when cursor is
26573 not a solid box cursor. */
26574 cursor_type = HOLLOW_BOX_CURSOR;
26575 }
26576 }
26577 return cursor_type;
26578 }
26579
26580 /* Cursor is blinked off, so determine how to "toggle" it. */
26581
26582 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26583 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26584 return get_specified_cursor_type (XCDR (alt_cursor), width);
26585
26586 /* Then see if frame has specified a specific blink off cursor type. */
26587 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26588 {
26589 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26590 return FRAME_BLINK_OFF_CURSOR (f);
26591 }
26592
26593 #if 0
26594 /* Some people liked having a permanently visible blinking cursor,
26595 while others had very strong opinions against it. So it was
26596 decided to remove it. KFS 2003-09-03 */
26597
26598 /* Finally perform built-in cursor blinking:
26599 filled box <-> hollow box
26600 wide [h]bar <-> narrow [h]bar
26601 narrow [h]bar <-> no cursor
26602 other type <-> no cursor */
26603
26604 if (cursor_type == FILLED_BOX_CURSOR)
26605 return HOLLOW_BOX_CURSOR;
26606
26607 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26608 {
26609 *width = 1;
26610 return cursor_type;
26611 }
26612 #endif
26613
26614 return NO_CURSOR;
26615 }
26616
26617
26618 /* Notice when the text cursor of window W has been completely
26619 overwritten by a drawing operation that outputs glyphs in AREA
26620 starting at X0 and ending at X1 in the line starting at Y0 and
26621 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26622 the rest of the line after X0 has been written. Y coordinates
26623 are window-relative. */
26624
26625 static void
26626 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26627 int x0, int x1, int y0, int y1)
26628 {
26629 int cx0, cx1, cy0, cy1;
26630 struct glyph_row *row;
26631
26632 if (!w->phys_cursor_on_p)
26633 return;
26634 if (area != TEXT_AREA)
26635 return;
26636
26637 if (w->phys_cursor.vpos < 0
26638 || w->phys_cursor.vpos >= w->current_matrix->nrows
26639 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26640 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26641 return;
26642
26643 if (row->cursor_in_fringe_p)
26644 {
26645 row->cursor_in_fringe_p = 0;
26646 draw_fringe_bitmap (w, row, row->reversed_p);
26647 w->phys_cursor_on_p = 0;
26648 return;
26649 }
26650
26651 cx0 = w->phys_cursor.x;
26652 cx1 = cx0 + w->phys_cursor_width;
26653 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26654 return;
26655
26656 /* The cursor image will be completely removed from the
26657 screen if the output area intersects the cursor area in
26658 y-direction. When we draw in [y0 y1[, and some part of
26659 the cursor is at y < y0, that part must have been drawn
26660 before. When scrolling, the cursor is erased before
26661 actually scrolling, so we don't come here. When not
26662 scrolling, the rows above the old cursor row must have
26663 changed, and in this case these rows must have written
26664 over the cursor image.
26665
26666 Likewise if part of the cursor is below y1, with the
26667 exception of the cursor being in the first blank row at
26668 the buffer and window end because update_text_area
26669 doesn't draw that row. (Except when it does, but
26670 that's handled in update_text_area.) */
26671
26672 cy0 = w->phys_cursor.y;
26673 cy1 = cy0 + w->phys_cursor_height;
26674 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26675 return;
26676
26677 w->phys_cursor_on_p = 0;
26678 }
26679
26680 #endif /* HAVE_WINDOW_SYSTEM */
26681
26682 \f
26683 /************************************************************************
26684 Mouse Face
26685 ************************************************************************/
26686
26687 #ifdef HAVE_WINDOW_SYSTEM
26688
26689 /* EXPORT for RIF:
26690 Fix the display of area AREA of overlapping row ROW in window W
26691 with respect to the overlapping part OVERLAPS. */
26692
26693 void
26694 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26695 enum glyph_row_area area, int overlaps)
26696 {
26697 int i, x;
26698
26699 block_input ();
26700
26701 x = 0;
26702 for (i = 0; i < row->used[area];)
26703 {
26704 if (row->glyphs[area][i].overlaps_vertically_p)
26705 {
26706 int start = i, start_x = x;
26707
26708 do
26709 {
26710 x += row->glyphs[area][i].pixel_width;
26711 ++i;
26712 }
26713 while (i < row->used[area]
26714 && row->glyphs[area][i].overlaps_vertically_p);
26715
26716 draw_glyphs (w, start_x, row, area,
26717 start, i,
26718 DRAW_NORMAL_TEXT, overlaps);
26719 }
26720 else
26721 {
26722 x += row->glyphs[area][i].pixel_width;
26723 ++i;
26724 }
26725 }
26726
26727 unblock_input ();
26728 }
26729
26730
26731 /* EXPORT:
26732 Draw the cursor glyph of window W in glyph row ROW. See the
26733 comment of draw_glyphs for the meaning of HL. */
26734
26735 void
26736 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26737 enum draw_glyphs_face hl)
26738 {
26739 /* If cursor hpos is out of bounds, don't draw garbage. This can
26740 happen in mini-buffer windows when switching between echo area
26741 glyphs and mini-buffer. */
26742 if ((row->reversed_p
26743 ? (w->phys_cursor.hpos >= 0)
26744 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26745 {
26746 int on_p = w->phys_cursor_on_p;
26747 int x1;
26748 int hpos = w->phys_cursor.hpos;
26749
26750 /* When the window is hscrolled, cursor hpos can legitimately be
26751 out of bounds, but we draw the cursor at the corresponding
26752 window margin in that case. */
26753 if (!row->reversed_p && hpos < 0)
26754 hpos = 0;
26755 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26756 hpos = row->used[TEXT_AREA] - 1;
26757
26758 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26759 hl, 0);
26760 w->phys_cursor_on_p = on_p;
26761
26762 if (hl == DRAW_CURSOR)
26763 w->phys_cursor_width = x1 - w->phys_cursor.x;
26764 /* When we erase the cursor, and ROW is overlapped by other
26765 rows, make sure that these overlapping parts of other rows
26766 are redrawn. */
26767 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26768 {
26769 w->phys_cursor_width = x1 - w->phys_cursor.x;
26770
26771 if (row > w->current_matrix->rows
26772 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26773 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26774 OVERLAPS_ERASED_CURSOR);
26775
26776 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26777 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26778 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26779 OVERLAPS_ERASED_CURSOR);
26780 }
26781 }
26782 }
26783
26784
26785 /* Erase the image of a cursor of window W from the screen. */
26786
26787 #ifndef HAVE_NTGUI
26788 static
26789 #endif
26790 void
26791 erase_phys_cursor (struct window *w)
26792 {
26793 struct frame *f = XFRAME (w->frame);
26794 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26795 int hpos = w->phys_cursor.hpos;
26796 int vpos = w->phys_cursor.vpos;
26797 int mouse_face_here_p = 0;
26798 struct glyph_matrix *active_glyphs = w->current_matrix;
26799 struct glyph_row *cursor_row;
26800 struct glyph *cursor_glyph;
26801 enum draw_glyphs_face hl;
26802
26803 /* No cursor displayed or row invalidated => nothing to do on the
26804 screen. */
26805 if (w->phys_cursor_type == NO_CURSOR)
26806 goto mark_cursor_off;
26807
26808 /* VPOS >= active_glyphs->nrows means that window has been resized.
26809 Don't bother to erase the cursor. */
26810 if (vpos >= active_glyphs->nrows)
26811 goto mark_cursor_off;
26812
26813 /* If row containing cursor is marked invalid, there is nothing we
26814 can do. */
26815 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26816 if (!cursor_row->enabled_p)
26817 goto mark_cursor_off;
26818
26819 /* If line spacing is > 0, old cursor may only be partially visible in
26820 window after split-window. So adjust visible height. */
26821 cursor_row->visible_height = min (cursor_row->visible_height,
26822 window_text_bottom_y (w) - cursor_row->y);
26823
26824 /* If row is completely invisible, don't attempt to delete a cursor which
26825 isn't there. This can happen if cursor is at top of a window, and
26826 we switch to a buffer with a header line in that window. */
26827 if (cursor_row->visible_height <= 0)
26828 goto mark_cursor_off;
26829
26830 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26831 if (cursor_row->cursor_in_fringe_p)
26832 {
26833 cursor_row->cursor_in_fringe_p = 0;
26834 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26835 goto mark_cursor_off;
26836 }
26837
26838 /* This can happen when the new row is shorter than the old one.
26839 In this case, either draw_glyphs or clear_end_of_line
26840 should have cleared the cursor. Note that we wouldn't be
26841 able to erase the cursor in this case because we don't have a
26842 cursor glyph at hand. */
26843 if ((cursor_row->reversed_p
26844 ? (w->phys_cursor.hpos < 0)
26845 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26846 goto mark_cursor_off;
26847
26848 /* When the window is hscrolled, cursor hpos can legitimately be out
26849 of bounds, but we draw the cursor at the corresponding window
26850 margin in that case. */
26851 if (!cursor_row->reversed_p && hpos < 0)
26852 hpos = 0;
26853 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26854 hpos = cursor_row->used[TEXT_AREA] - 1;
26855
26856 /* If the cursor is in the mouse face area, redisplay that when
26857 we clear the cursor. */
26858 if (! NILP (hlinfo->mouse_face_window)
26859 && coords_in_mouse_face_p (w, hpos, vpos)
26860 /* Don't redraw the cursor's spot in mouse face if it is at the
26861 end of a line (on a newline). The cursor appears there, but
26862 mouse highlighting does not. */
26863 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26864 mouse_face_here_p = 1;
26865
26866 /* Maybe clear the display under the cursor. */
26867 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26868 {
26869 int x, y, left_x;
26870 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26871 int width;
26872
26873 cursor_glyph = get_phys_cursor_glyph (w);
26874 if (cursor_glyph == NULL)
26875 goto mark_cursor_off;
26876
26877 width = cursor_glyph->pixel_width;
26878 left_x = window_box_left_offset (w, TEXT_AREA);
26879 x = w->phys_cursor.x;
26880 if (x < left_x)
26881 width -= left_x - x;
26882 width = min (width, window_box_width (w, TEXT_AREA) - x);
26883 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26884 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26885
26886 if (width > 0)
26887 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26888 }
26889
26890 /* Erase the cursor by redrawing the character underneath it. */
26891 if (mouse_face_here_p)
26892 hl = DRAW_MOUSE_FACE;
26893 else
26894 hl = DRAW_NORMAL_TEXT;
26895 draw_phys_cursor_glyph (w, cursor_row, hl);
26896
26897 mark_cursor_off:
26898 w->phys_cursor_on_p = 0;
26899 w->phys_cursor_type = NO_CURSOR;
26900 }
26901
26902
26903 /* EXPORT:
26904 Display or clear cursor of window W. If ON is zero, clear the
26905 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26906 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26907
26908 void
26909 display_and_set_cursor (struct window *w, bool on,
26910 int hpos, int vpos, int x, int y)
26911 {
26912 struct frame *f = XFRAME (w->frame);
26913 int new_cursor_type;
26914 int new_cursor_width;
26915 int active_cursor;
26916 struct glyph_row *glyph_row;
26917 struct glyph *glyph;
26918
26919 /* This is pointless on invisible frames, and dangerous on garbaged
26920 windows and frames; in the latter case, the frame or window may
26921 be in the midst of changing its size, and x and y may be off the
26922 window. */
26923 if (! FRAME_VISIBLE_P (f)
26924 || FRAME_GARBAGED_P (f)
26925 || vpos >= w->current_matrix->nrows
26926 || hpos >= w->current_matrix->matrix_w)
26927 return;
26928
26929 /* If cursor is off and we want it off, return quickly. */
26930 if (!on && !w->phys_cursor_on_p)
26931 return;
26932
26933 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26934 /* If cursor row is not enabled, we don't really know where to
26935 display the cursor. */
26936 if (!glyph_row->enabled_p)
26937 {
26938 w->phys_cursor_on_p = 0;
26939 return;
26940 }
26941
26942 glyph = NULL;
26943 if (!glyph_row->exact_window_width_line_p
26944 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26945 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26946
26947 eassert (input_blocked_p ());
26948
26949 /* Set new_cursor_type to the cursor we want to be displayed. */
26950 new_cursor_type = get_window_cursor_type (w, glyph,
26951 &new_cursor_width, &active_cursor);
26952
26953 /* If cursor is currently being shown and we don't want it to be or
26954 it is in the wrong place, or the cursor type is not what we want,
26955 erase it. */
26956 if (w->phys_cursor_on_p
26957 && (!on
26958 || w->phys_cursor.x != x
26959 || w->phys_cursor.y != y
26960 || new_cursor_type != w->phys_cursor_type
26961 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26962 && new_cursor_width != w->phys_cursor_width)))
26963 erase_phys_cursor (w);
26964
26965 /* Don't check phys_cursor_on_p here because that flag is only set
26966 to zero in some cases where we know that the cursor has been
26967 completely erased, to avoid the extra work of erasing the cursor
26968 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26969 still not be visible, or it has only been partly erased. */
26970 if (on)
26971 {
26972 w->phys_cursor_ascent = glyph_row->ascent;
26973 w->phys_cursor_height = glyph_row->height;
26974
26975 /* Set phys_cursor_.* before x_draw_.* is called because some
26976 of them may need the information. */
26977 w->phys_cursor.x = x;
26978 w->phys_cursor.y = glyph_row->y;
26979 w->phys_cursor.hpos = hpos;
26980 w->phys_cursor.vpos = vpos;
26981 }
26982
26983 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26984 new_cursor_type, new_cursor_width,
26985 on, active_cursor);
26986 }
26987
26988
26989 /* Switch the display of W's cursor on or off, according to the value
26990 of ON. */
26991
26992 static void
26993 update_window_cursor (struct window *w, bool on)
26994 {
26995 /* Don't update cursor in windows whose frame is in the process
26996 of being deleted. */
26997 if (w->current_matrix)
26998 {
26999 int hpos = w->phys_cursor.hpos;
27000 int vpos = w->phys_cursor.vpos;
27001 struct glyph_row *row;
27002
27003 if (vpos >= w->current_matrix->nrows
27004 || hpos >= w->current_matrix->matrix_w)
27005 return;
27006
27007 row = MATRIX_ROW (w->current_matrix, vpos);
27008
27009 /* When the window is hscrolled, cursor hpos can legitimately be
27010 out of bounds, but we draw the cursor at the corresponding
27011 window margin in that case. */
27012 if (!row->reversed_p && hpos < 0)
27013 hpos = 0;
27014 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27015 hpos = row->used[TEXT_AREA] - 1;
27016
27017 block_input ();
27018 display_and_set_cursor (w, on, hpos, vpos,
27019 w->phys_cursor.x, w->phys_cursor.y);
27020 unblock_input ();
27021 }
27022 }
27023
27024
27025 /* Call update_window_cursor with parameter ON_P on all leaf windows
27026 in the window tree rooted at W. */
27027
27028 static void
27029 update_cursor_in_window_tree (struct window *w, bool on_p)
27030 {
27031 while (w)
27032 {
27033 if (WINDOWP (w->contents))
27034 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27035 else
27036 update_window_cursor (w, on_p);
27037
27038 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27039 }
27040 }
27041
27042
27043 /* EXPORT:
27044 Display the cursor on window W, or clear it, according to ON_P.
27045 Don't change the cursor's position. */
27046
27047 void
27048 x_update_cursor (struct frame *f, bool on_p)
27049 {
27050 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27051 }
27052
27053
27054 /* EXPORT:
27055 Clear the cursor of window W to background color, and mark the
27056 cursor as not shown. This is used when the text where the cursor
27057 is about to be rewritten. */
27058
27059 void
27060 x_clear_cursor (struct window *w)
27061 {
27062 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27063 update_window_cursor (w, 0);
27064 }
27065
27066 #endif /* HAVE_WINDOW_SYSTEM */
27067
27068 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27069 and MSDOS. */
27070 static void
27071 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27072 int start_hpos, int end_hpos,
27073 enum draw_glyphs_face draw)
27074 {
27075 #ifdef HAVE_WINDOW_SYSTEM
27076 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27077 {
27078 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27079 return;
27080 }
27081 #endif
27082 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27083 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27084 #endif
27085 }
27086
27087 /* Display the active region described by mouse_face_* according to DRAW. */
27088
27089 static void
27090 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27091 {
27092 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27093 struct frame *f = XFRAME (WINDOW_FRAME (w));
27094
27095 if (/* If window is in the process of being destroyed, don't bother
27096 to do anything. */
27097 w->current_matrix != NULL
27098 /* Don't update mouse highlight if hidden */
27099 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27100 /* Recognize when we are called to operate on rows that don't exist
27101 anymore. This can happen when a window is split. */
27102 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27103 {
27104 int phys_cursor_on_p = w->phys_cursor_on_p;
27105 struct glyph_row *row, *first, *last;
27106
27107 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27108 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27109
27110 for (row = first; row <= last && row->enabled_p; ++row)
27111 {
27112 int start_hpos, end_hpos, start_x;
27113
27114 /* For all but the first row, the highlight starts at column 0. */
27115 if (row == first)
27116 {
27117 /* R2L rows have BEG and END in reversed order, but the
27118 screen drawing geometry is always left to right. So
27119 we need to mirror the beginning and end of the
27120 highlighted area in R2L rows. */
27121 if (!row->reversed_p)
27122 {
27123 start_hpos = hlinfo->mouse_face_beg_col;
27124 start_x = hlinfo->mouse_face_beg_x;
27125 }
27126 else if (row == last)
27127 {
27128 start_hpos = hlinfo->mouse_face_end_col;
27129 start_x = hlinfo->mouse_face_end_x;
27130 }
27131 else
27132 {
27133 start_hpos = 0;
27134 start_x = 0;
27135 }
27136 }
27137 else if (row->reversed_p && row == last)
27138 {
27139 start_hpos = hlinfo->mouse_face_end_col;
27140 start_x = hlinfo->mouse_face_end_x;
27141 }
27142 else
27143 {
27144 start_hpos = 0;
27145 start_x = 0;
27146 }
27147
27148 if (row == last)
27149 {
27150 if (!row->reversed_p)
27151 end_hpos = hlinfo->mouse_face_end_col;
27152 else if (row == first)
27153 end_hpos = hlinfo->mouse_face_beg_col;
27154 else
27155 {
27156 end_hpos = row->used[TEXT_AREA];
27157 if (draw == DRAW_NORMAL_TEXT)
27158 row->fill_line_p = 1; /* Clear to end of line */
27159 }
27160 }
27161 else if (row->reversed_p && row == first)
27162 end_hpos = hlinfo->mouse_face_beg_col;
27163 else
27164 {
27165 end_hpos = row->used[TEXT_AREA];
27166 if (draw == DRAW_NORMAL_TEXT)
27167 row->fill_line_p = 1; /* Clear to end of line */
27168 }
27169
27170 if (end_hpos > start_hpos)
27171 {
27172 draw_row_with_mouse_face (w, start_x, row,
27173 start_hpos, end_hpos, draw);
27174
27175 row->mouse_face_p
27176 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27177 }
27178 }
27179
27180 #ifdef HAVE_WINDOW_SYSTEM
27181 /* When we've written over the cursor, arrange for it to
27182 be displayed again. */
27183 if (FRAME_WINDOW_P (f)
27184 && phys_cursor_on_p && !w->phys_cursor_on_p)
27185 {
27186 int hpos = w->phys_cursor.hpos;
27187
27188 /* When the window is hscrolled, cursor hpos can legitimately be
27189 out of bounds, but we draw the cursor at the corresponding
27190 window margin in that case. */
27191 if (!row->reversed_p && hpos < 0)
27192 hpos = 0;
27193 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27194 hpos = row->used[TEXT_AREA] - 1;
27195
27196 block_input ();
27197 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27198 w->phys_cursor.x, w->phys_cursor.y);
27199 unblock_input ();
27200 }
27201 #endif /* HAVE_WINDOW_SYSTEM */
27202 }
27203
27204 #ifdef HAVE_WINDOW_SYSTEM
27205 /* Change the mouse cursor. */
27206 if (FRAME_WINDOW_P (f))
27207 {
27208 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27209 if (draw == DRAW_NORMAL_TEXT
27210 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27211 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27212 else
27213 #endif
27214 if (draw == DRAW_MOUSE_FACE)
27215 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27216 else
27217 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27218 }
27219 #endif /* HAVE_WINDOW_SYSTEM */
27220 }
27221
27222 /* EXPORT:
27223 Clear out the mouse-highlighted active region.
27224 Redraw it un-highlighted first. Value is non-zero if mouse
27225 face was actually drawn unhighlighted. */
27226
27227 int
27228 clear_mouse_face (Mouse_HLInfo *hlinfo)
27229 {
27230 int cleared = 0;
27231
27232 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27233 {
27234 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27235 cleared = 1;
27236 }
27237
27238 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27239 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27240 hlinfo->mouse_face_window = Qnil;
27241 hlinfo->mouse_face_overlay = Qnil;
27242 return cleared;
27243 }
27244
27245 /* Return true if the coordinates HPOS and VPOS on windows W are
27246 within the mouse face on that window. */
27247 static bool
27248 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27249 {
27250 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27251
27252 /* Quickly resolve the easy cases. */
27253 if (!(WINDOWP (hlinfo->mouse_face_window)
27254 && XWINDOW (hlinfo->mouse_face_window) == w))
27255 return false;
27256 if (vpos < hlinfo->mouse_face_beg_row
27257 || vpos > hlinfo->mouse_face_end_row)
27258 return false;
27259 if (vpos > hlinfo->mouse_face_beg_row
27260 && vpos < hlinfo->mouse_face_end_row)
27261 return true;
27262
27263 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27264 {
27265 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27266 {
27267 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27268 return true;
27269 }
27270 else if ((vpos == hlinfo->mouse_face_beg_row
27271 && hpos >= hlinfo->mouse_face_beg_col)
27272 || (vpos == hlinfo->mouse_face_end_row
27273 && hpos < hlinfo->mouse_face_end_col))
27274 return true;
27275 }
27276 else
27277 {
27278 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27279 {
27280 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27281 return true;
27282 }
27283 else if ((vpos == hlinfo->mouse_face_beg_row
27284 && hpos <= hlinfo->mouse_face_beg_col)
27285 || (vpos == hlinfo->mouse_face_end_row
27286 && hpos > hlinfo->mouse_face_end_col))
27287 return true;
27288 }
27289 return false;
27290 }
27291
27292
27293 /* EXPORT:
27294 True if physical cursor of window W is within mouse face. */
27295
27296 bool
27297 cursor_in_mouse_face_p (struct window *w)
27298 {
27299 int hpos = w->phys_cursor.hpos;
27300 int vpos = w->phys_cursor.vpos;
27301 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27302
27303 /* When the window is hscrolled, cursor hpos can legitimately be out
27304 of bounds, but we draw the cursor at the corresponding window
27305 margin in that case. */
27306 if (!row->reversed_p && hpos < 0)
27307 hpos = 0;
27308 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27309 hpos = row->used[TEXT_AREA] - 1;
27310
27311 return coords_in_mouse_face_p (w, hpos, vpos);
27312 }
27313
27314
27315 \f
27316 /* Find the glyph rows START_ROW and END_ROW of window W that display
27317 characters between buffer positions START_CHARPOS and END_CHARPOS
27318 (excluding END_CHARPOS). DISP_STRING is a display string that
27319 covers these buffer positions. This is similar to
27320 row_containing_pos, but is more accurate when bidi reordering makes
27321 buffer positions change non-linearly with glyph rows. */
27322 static void
27323 rows_from_pos_range (struct window *w,
27324 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27325 Lisp_Object disp_string,
27326 struct glyph_row **start, struct glyph_row **end)
27327 {
27328 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27329 int last_y = window_text_bottom_y (w);
27330 struct glyph_row *row;
27331
27332 *start = NULL;
27333 *end = NULL;
27334
27335 while (!first->enabled_p
27336 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27337 first++;
27338
27339 /* Find the START row. */
27340 for (row = first;
27341 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27342 row++)
27343 {
27344 /* A row can potentially be the START row if the range of the
27345 characters it displays intersects the range
27346 [START_CHARPOS..END_CHARPOS). */
27347 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27348 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27349 /* See the commentary in row_containing_pos, for the
27350 explanation of the complicated way to check whether
27351 some position is beyond the end of the characters
27352 displayed by a row. */
27353 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27354 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27355 && !row->ends_at_zv_p
27356 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27357 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27358 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27359 && !row->ends_at_zv_p
27360 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27361 {
27362 /* Found a candidate row. Now make sure at least one of the
27363 glyphs it displays has a charpos from the range
27364 [START_CHARPOS..END_CHARPOS).
27365
27366 This is not obvious because bidi reordering could make
27367 buffer positions of a row be 1,2,3,102,101,100, and if we
27368 want to highlight characters in [50..60), we don't want
27369 this row, even though [50..60) does intersect [1..103),
27370 the range of character positions given by the row's start
27371 and end positions. */
27372 struct glyph *g = row->glyphs[TEXT_AREA];
27373 struct glyph *e = g + row->used[TEXT_AREA];
27374
27375 while (g < e)
27376 {
27377 if (((BUFFERP (g->object) || INTEGERP (g->object))
27378 && start_charpos <= g->charpos && g->charpos < end_charpos)
27379 /* A glyph that comes from DISP_STRING is by
27380 definition to be highlighted. */
27381 || EQ (g->object, disp_string))
27382 *start = row;
27383 g++;
27384 }
27385 if (*start)
27386 break;
27387 }
27388 }
27389
27390 /* Find the END row. */
27391 if (!*start
27392 /* If the last row is partially visible, start looking for END
27393 from that row, instead of starting from FIRST. */
27394 && !(row->enabled_p
27395 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27396 row = first;
27397 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27398 {
27399 struct glyph_row *next = row + 1;
27400 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27401
27402 if (!next->enabled_p
27403 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27404 /* The first row >= START whose range of displayed characters
27405 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27406 is the row END + 1. */
27407 || (start_charpos < next_start
27408 && end_charpos < next_start)
27409 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27410 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27411 && !next->ends_at_zv_p
27412 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27413 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27414 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27415 && !next->ends_at_zv_p
27416 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27417 {
27418 *end = row;
27419 break;
27420 }
27421 else
27422 {
27423 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27424 but none of the characters it displays are in the range, it is
27425 also END + 1. */
27426 struct glyph *g = next->glyphs[TEXT_AREA];
27427 struct glyph *s = g;
27428 struct glyph *e = g + next->used[TEXT_AREA];
27429
27430 while (g < e)
27431 {
27432 if (((BUFFERP (g->object) || INTEGERP (g->object))
27433 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27434 /* If the buffer position of the first glyph in
27435 the row is equal to END_CHARPOS, it means
27436 the last character to be highlighted is the
27437 newline of ROW, and we must consider NEXT as
27438 END, not END+1. */
27439 || (((!next->reversed_p && g == s)
27440 || (next->reversed_p && g == e - 1))
27441 && (g->charpos == end_charpos
27442 /* Special case for when NEXT is an
27443 empty line at ZV. */
27444 || (g->charpos == -1
27445 && !row->ends_at_zv_p
27446 && next_start == end_charpos)))))
27447 /* A glyph that comes from DISP_STRING is by
27448 definition to be highlighted. */
27449 || EQ (g->object, disp_string))
27450 break;
27451 g++;
27452 }
27453 if (g == e)
27454 {
27455 *end = row;
27456 break;
27457 }
27458 /* The first row that ends at ZV must be the last to be
27459 highlighted. */
27460 else if (next->ends_at_zv_p)
27461 {
27462 *end = next;
27463 break;
27464 }
27465 }
27466 }
27467 }
27468
27469 /* This function sets the mouse_face_* elements of HLINFO, assuming
27470 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27471 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27472 for the overlay or run of text properties specifying the mouse
27473 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27474 before-string and after-string that must also be highlighted.
27475 DISP_STRING, if non-nil, is a display string that may cover some
27476 or all of the highlighted text. */
27477
27478 static void
27479 mouse_face_from_buffer_pos (Lisp_Object window,
27480 Mouse_HLInfo *hlinfo,
27481 ptrdiff_t mouse_charpos,
27482 ptrdiff_t start_charpos,
27483 ptrdiff_t end_charpos,
27484 Lisp_Object before_string,
27485 Lisp_Object after_string,
27486 Lisp_Object disp_string)
27487 {
27488 struct window *w = XWINDOW (window);
27489 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27490 struct glyph_row *r1, *r2;
27491 struct glyph *glyph, *end;
27492 ptrdiff_t ignore, pos;
27493 int x;
27494
27495 eassert (NILP (disp_string) || STRINGP (disp_string));
27496 eassert (NILP (before_string) || STRINGP (before_string));
27497 eassert (NILP (after_string) || STRINGP (after_string));
27498
27499 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27500 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27501 if (r1 == NULL)
27502 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27503 /* If the before-string or display-string contains newlines,
27504 rows_from_pos_range skips to its last row. Move back. */
27505 if (!NILP (before_string) || !NILP (disp_string))
27506 {
27507 struct glyph_row *prev;
27508 while ((prev = r1 - 1, prev >= first)
27509 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27510 && prev->used[TEXT_AREA] > 0)
27511 {
27512 struct glyph *beg = prev->glyphs[TEXT_AREA];
27513 glyph = beg + prev->used[TEXT_AREA];
27514 while (--glyph >= beg && INTEGERP (glyph->object));
27515 if (glyph < beg
27516 || !(EQ (glyph->object, before_string)
27517 || EQ (glyph->object, disp_string)))
27518 break;
27519 r1 = prev;
27520 }
27521 }
27522 if (r2 == NULL)
27523 {
27524 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27525 hlinfo->mouse_face_past_end = 1;
27526 }
27527 else if (!NILP (after_string))
27528 {
27529 /* If the after-string has newlines, advance to its last row. */
27530 struct glyph_row *next;
27531 struct glyph_row *last
27532 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27533
27534 for (next = r2 + 1;
27535 next <= last
27536 && next->used[TEXT_AREA] > 0
27537 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27538 ++next)
27539 r2 = next;
27540 }
27541 /* The rest of the display engine assumes that mouse_face_beg_row is
27542 either above mouse_face_end_row or identical to it. But with
27543 bidi-reordered continued lines, the row for START_CHARPOS could
27544 be below the row for END_CHARPOS. If so, swap the rows and store
27545 them in correct order. */
27546 if (r1->y > r2->y)
27547 {
27548 struct glyph_row *tem = r2;
27549
27550 r2 = r1;
27551 r1 = tem;
27552 }
27553
27554 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27555 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27556
27557 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27558 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27559 could be anywhere in the row and in any order. The strategy
27560 below is to find the leftmost and the rightmost glyph that
27561 belongs to either of these 3 strings, or whose position is
27562 between START_CHARPOS and END_CHARPOS, and highlight all the
27563 glyphs between those two. This may cover more than just the text
27564 between START_CHARPOS and END_CHARPOS if the range of characters
27565 strides the bidi level boundary, e.g. if the beginning is in R2L
27566 text while the end is in L2R text or vice versa. */
27567 if (!r1->reversed_p)
27568 {
27569 /* This row is in a left to right paragraph. Scan it left to
27570 right. */
27571 glyph = r1->glyphs[TEXT_AREA];
27572 end = glyph + r1->used[TEXT_AREA];
27573 x = r1->x;
27574
27575 /* Skip truncation glyphs at the start of the glyph row. */
27576 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27577 for (; glyph < end
27578 && INTEGERP (glyph->object)
27579 && glyph->charpos < 0;
27580 ++glyph)
27581 x += glyph->pixel_width;
27582
27583 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27584 or DISP_STRING, and the first glyph from buffer whose
27585 position is between START_CHARPOS and END_CHARPOS. */
27586 for (; glyph < end
27587 && !INTEGERP (glyph->object)
27588 && !EQ (glyph->object, disp_string)
27589 && !(BUFFERP (glyph->object)
27590 && (glyph->charpos >= start_charpos
27591 && glyph->charpos < end_charpos));
27592 ++glyph)
27593 {
27594 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27595 are present at buffer positions between START_CHARPOS and
27596 END_CHARPOS, or if they come from an overlay. */
27597 if (EQ (glyph->object, before_string))
27598 {
27599 pos = string_buffer_position (before_string,
27600 start_charpos);
27601 /* If pos == 0, it means before_string came from an
27602 overlay, not from a buffer position. */
27603 if (!pos || (pos >= start_charpos && pos < end_charpos))
27604 break;
27605 }
27606 else if (EQ (glyph->object, after_string))
27607 {
27608 pos = string_buffer_position (after_string, end_charpos);
27609 if (!pos || (pos >= start_charpos && pos < end_charpos))
27610 break;
27611 }
27612 x += glyph->pixel_width;
27613 }
27614 hlinfo->mouse_face_beg_x = x;
27615 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27616 }
27617 else
27618 {
27619 /* This row is in a right to left paragraph. Scan it right to
27620 left. */
27621 struct glyph *g;
27622
27623 end = r1->glyphs[TEXT_AREA] - 1;
27624 glyph = end + r1->used[TEXT_AREA];
27625
27626 /* Skip truncation glyphs at the start of the glyph row. */
27627 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27628 for (; glyph > end
27629 && INTEGERP (glyph->object)
27630 && glyph->charpos < 0;
27631 --glyph)
27632 ;
27633
27634 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27635 or DISP_STRING, and the first glyph from buffer whose
27636 position is between START_CHARPOS and END_CHARPOS. */
27637 for (; glyph > end
27638 && !INTEGERP (glyph->object)
27639 && !EQ (glyph->object, disp_string)
27640 && !(BUFFERP (glyph->object)
27641 && (glyph->charpos >= start_charpos
27642 && glyph->charpos < end_charpos));
27643 --glyph)
27644 {
27645 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27646 are present at buffer positions between START_CHARPOS and
27647 END_CHARPOS, or if they come from an overlay. */
27648 if (EQ (glyph->object, before_string))
27649 {
27650 pos = string_buffer_position (before_string, start_charpos);
27651 /* If pos == 0, it means before_string came from an
27652 overlay, not from a buffer position. */
27653 if (!pos || (pos >= start_charpos && pos < end_charpos))
27654 break;
27655 }
27656 else if (EQ (glyph->object, after_string))
27657 {
27658 pos = string_buffer_position (after_string, end_charpos);
27659 if (!pos || (pos >= start_charpos && pos < end_charpos))
27660 break;
27661 }
27662 }
27663
27664 glyph++; /* first glyph to the right of the highlighted area */
27665 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27666 x += g->pixel_width;
27667 hlinfo->mouse_face_beg_x = x;
27668 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27669 }
27670
27671 /* If the highlight ends in a different row, compute GLYPH and END
27672 for the end row. Otherwise, reuse the values computed above for
27673 the row where the highlight begins. */
27674 if (r2 != r1)
27675 {
27676 if (!r2->reversed_p)
27677 {
27678 glyph = r2->glyphs[TEXT_AREA];
27679 end = glyph + r2->used[TEXT_AREA];
27680 x = r2->x;
27681 }
27682 else
27683 {
27684 end = r2->glyphs[TEXT_AREA] - 1;
27685 glyph = end + r2->used[TEXT_AREA];
27686 }
27687 }
27688
27689 if (!r2->reversed_p)
27690 {
27691 /* Skip truncation and continuation glyphs near the end of the
27692 row, and also blanks and stretch glyphs inserted by
27693 extend_face_to_end_of_line. */
27694 while (end > glyph
27695 && INTEGERP ((end - 1)->object))
27696 --end;
27697 /* Scan the rest of the glyph row from the end, looking for the
27698 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27699 DISP_STRING, or whose position is between START_CHARPOS
27700 and END_CHARPOS */
27701 for (--end;
27702 end > glyph
27703 && !INTEGERP (end->object)
27704 && !EQ (end->object, disp_string)
27705 && !(BUFFERP (end->object)
27706 && (end->charpos >= start_charpos
27707 && end->charpos < end_charpos));
27708 --end)
27709 {
27710 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27711 are present at buffer positions between START_CHARPOS and
27712 END_CHARPOS, or if they come from an overlay. */
27713 if (EQ (end->object, before_string))
27714 {
27715 pos = string_buffer_position (before_string, start_charpos);
27716 if (!pos || (pos >= start_charpos && pos < end_charpos))
27717 break;
27718 }
27719 else if (EQ (end->object, after_string))
27720 {
27721 pos = string_buffer_position (after_string, end_charpos);
27722 if (!pos || (pos >= start_charpos && pos < end_charpos))
27723 break;
27724 }
27725 }
27726 /* Find the X coordinate of the last glyph to be highlighted. */
27727 for (; glyph <= end; ++glyph)
27728 x += glyph->pixel_width;
27729
27730 hlinfo->mouse_face_end_x = x;
27731 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27732 }
27733 else
27734 {
27735 /* Skip truncation and continuation glyphs near the end of the
27736 row, and also blanks and stretch glyphs inserted by
27737 extend_face_to_end_of_line. */
27738 x = r2->x;
27739 end++;
27740 while (end < glyph
27741 && INTEGERP (end->object))
27742 {
27743 x += end->pixel_width;
27744 ++end;
27745 }
27746 /* Scan the rest of the glyph row from the end, looking for the
27747 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27748 DISP_STRING, or whose position is between START_CHARPOS
27749 and END_CHARPOS */
27750 for ( ;
27751 end < glyph
27752 && !INTEGERP (end->object)
27753 && !EQ (end->object, disp_string)
27754 && !(BUFFERP (end->object)
27755 && (end->charpos >= start_charpos
27756 && end->charpos < end_charpos));
27757 ++end)
27758 {
27759 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27760 are present at buffer positions between START_CHARPOS and
27761 END_CHARPOS, or if they come from an overlay. */
27762 if (EQ (end->object, before_string))
27763 {
27764 pos = string_buffer_position (before_string, start_charpos);
27765 if (!pos || (pos >= start_charpos && pos < end_charpos))
27766 break;
27767 }
27768 else if (EQ (end->object, after_string))
27769 {
27770 pos = string_buffer_position (after_string, end_charpos);
27771 if (!pos || (pos >= start_charpos && pos < end_charpos))
27772 break;
27773 }
27774 x += end->pixel_width;
27775 }
27776 /* If we exited the above loop because we arrived at the last
27777 glyph of the row, and its buffer position is still not in
27778 range, it means the last character in range is the preceding
27779 newline. Bump the end column and x values to get past the
27780 last glyph. */
27781 if (end == glyph
27782 && BUFFERP (end->object)
27783 && (end->charpos < start_charpos
27784 || end->charpos >= end_charpos))
27785 {
27786 x += end->pixel_width;
27787 ++end;
27788 }
27789 hlinfo->mouse_face_end_x = x;
27790 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27791 }
27792
27793 hlinfo->mouse_face_window = window;
27794 hlinfo->mouse_face_face_id
27795 = face_at_buffer_position (w, mouse_charpos, &ignore,
27796 mouse_charpos + 1,
27797 !hlinfo->mouse_face_hidden, -1);
27798 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27799 }
27800
27801 /* The following function is not used anymore (replaced with
27802 mouse_face_from_string_pos), but I leave it here for the time
27803 being, in case someone would. */
27804
27805 #if 0 /* not used */
27806
27807 /* Find the position of the glyph for position POS in OBJECT in
27808 window W's current matrix, and return in *X, *Y the pixel
27809 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27810
27811 RIGHT_P non-zero means return the position of the right edge of the
27812 glyph, RIGHT_P zero means return the left edge position.
27813
27814 If no glyph for POS exists in the matrix, return the position of
27815 the glyph with the next smaller position that is in the matrix, if
27816 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27817 exists in the matrix, return the position of the glyph with the
27818 next larger position in OBJECT.
27819
27820 Value is non-zero if a glyph was found. */
27821
27822 static int
27823 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27824 int *hpos, int *vpos, int *x, int *y, int right_p)
27825 {
27826 int yb = window_text_bottom_y (w);
27827 struct glyph_row *r;
27828 struct glyph *best_glyph = NULL;
27829 struct glyph_row *best_row = NULL;
27830 int best_x = 0;
27831
27832 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27833 r->enabled_p && r->y < yb;
27834 ++r)
27835 {
27836 struct glyph *g = r->glyphs[TEXT_AREA];
27837 struct glyph *e = g + r->used[TEXT_AREA];
27838 int gx;
27839
27840 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27841 if (EQ (g->object, object))
27842 {
27843 if (g->charpos == pos)
27844 {
27845 best_glyph = g;
27846 best_x = gx;
27847 best_row = r;
27848 goto found;
27849 }
27850 else if (best_glyph == NULL
27851 || ((eabs (g->charpos - pos)
27852 < eabs (best_glyph->charpos - pos))
27853 && (right_p
27854 ? g->charpos < pos
27855 : g->charpos > pos)))
27856 {
27857 best_glyph = g;
27858 best_x = gx;
27859 best_row = r;
27860 }
27861 }
27862 }
27863
27864 found:
27865
27866 if (best_glyph)
27867 {
27868 *x = best_x;
27869 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27870
27871 if (right_p)
27872 {
27873 *x += best_glyph->pixel_width;
27874 ++*hpos;
27875 }
27876
27877 *y = best_row->y;
27878 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27879 }
27880
27881 return best_glyph != NULL;
27882 }
27883 #endif /* not used */
27884
27885 /* Find the positions of the first and the last glyphs in window W's
27886 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27887 (assumed to be a string), and return in HLINFO's mouse_face_*
27888 members the pixel and column/row coordinates of those glyphs. */
27889
27890 static void
27891 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27892 Lisp_Object object,
27893 ptrdiff_t startpos, ptrdiff_t endpos)
27894 {
27895 int yb = window_text_bottom_y (w);
27896 struct glyph_row *r;
27897 struct glyph *g, *e;
27898 int gx;
27899 int found = 0;
27900
27901 /* Find the glyph row with at least one position in the range
27902 [STARTPOS..ENDPOS), and the first glyph in that row whose
27903 position belongs to that range. */
27904 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27905 r->enabled_p && r->y < yb;
27906 ++r)
27907 {
27908 if (!r->reversed_p)
27909 {
27910 g = r->glyphs[TEXT_AREA];
27911 e = g + r->used[TEXT_AREA];
27912 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27913 if (EQ (g->object, object)
27914 && startpos <= g->charpos && g->charpos < endpos)
27915 {
27916 hlinfo->mouse_face_beg_row
27917 = MATRIX_ROW_VPOS (r, w->current_matrix);
27918 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27919 hlinfo->mouse_face_beg_x = gx;
27920 found = 1;
27921 break;
27922 }
27923 }
27924 else
27925 {
27926 struct glyph *g1;
27927
27928 e = r->glyphs[TEXT_AREA];
27929 g = e + r->used[TEXT_AREA];
27930 for ( ; g > e; --g)
27931 if (EQ ((g-1)->object, object)
27932 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27933 {
27934 hlinfo->mouse_face_beg_row
27935 = MATRIX_ROW_VPOS (r, w->current_matrix);
27936 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27937 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27938 gx += g1->pixel_width;
27939 hlinfo->mouse_face_beg_x = gx;
27940 found = 1;
27941 break;
27942 }
27943 }
27944 if (found)
27945 break;
27946 }
27947
27948 if (!found)
27949 return;
27950
27951 /* Starting with the next row, look for the first row which does NOT
27952 include any glyphs whose positions are in the range. */
27953 for (++r; r->enabled_p && r->y < yb; ++r)
27954 {
27955 g = r->glyphs[TEXT_AREA];
27956 e = g + r->used[TEXT_AREA];
27957 found = 0;
27958 for ( ; g < e; ++g)
27959 if (EQ (g->object, object)
27960 && startpos <= g->charpos && g->charpos < endpos)
27961 {
27962 found = 1;
27963 break;
27964 }
27965 if (!found)
27966 break;
27967 }
27968
27969 /* The highlighted region ends on the previous row. */
27970 r--;
27971
27972 /* Set the end row. */
27973 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27974
27975 /* Compute and set the end column and the end column's horizontal
27976 pixel coordinate. */
27977 if (!r->reversed_p)
27978 {
27979 g = r->glyphs[TEXT_AREA];
27980 e = g + r->used[TEXT_AREA];
27981 for ( ; e > g; --e)
27982 if (EQ ((e-1)->object, object)
27983 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27984 break;
27985 hlinfo->mouse_face_end_col = e - g;
27986
27987 for (gx = r->x; g < e; ++g)
27988 gx += g->pixel_width;
27989 hlinfo->mouse_face_end_x = gx;
27990 }
27991 else
27992 {
27993 e = r->glyphs[TEXT_AREA];
27994 g = e + r->used[TEXT_AREA];
27995 for (gx = r->x ; e < g; ++e)
27996 {
27997 if (EQ (e->object, object)
27998 && startpos <= e->charpos && e->charpos < endpos)
27999 break;
28000 gx += e->pixel_width;
28001 }
28002 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28003 hlinfo->mouse_face_end_x = gx;
28004 }
28005 }
28006
28007 #ifdef HAVE_WINDOW_SYSTEM
28008
28009 /* See if position X, Y is within a hot-spot of an image. */
28010
28011 static int
28012 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28013 {
28014 if (!CONSP (hot_spot))
28015 return 0;
28016
28017 if (EQ (XCAR (hot_spot), Qrect))
28018 {
28019 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28020 Lisp_Object rect = XCDR (hot_spot);
28021 Lisp_Object tem;
28022 if (!CONSP (rect))
28023 return 0;
28024 if (!CONSP (XCAR (rect)))
28025 return 0;
28026 if (!CONSP (XCDR (rect)))
28027 return 0;
28028 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28029 return 0;
28030 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28031 return 0;
28032 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28033 return 0;
28034 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28035 return 0;
28036 return 1;
28037 }
28038 else if (EQ (XCAR (hot_spot), Qcircle))
28039 {
28040 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28041 Lisp_Object circ = XCDR (hot_spot);
28042 Lisp_Object lr, lx0, ly0;
28043 if (CONSP (circ)
28044 && CONSP (XCAR (circ))
28045 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28046 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28047 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28048 {
28049 double r = XFLOATINT (lr);
28050 double dx = XINT (lx0) - x;
28051 double dy = XINT (ly0) - y;
28052 return (dx * dx + dy * dy <= r * r);
28053 }
28054 }
28055 else if (EQ (XCAR (hot_spot), Qpoly))
28056 {
28057 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28058 if (VECTORP (XCDR (hot_spot)))
28059 {
28060 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28061 Lisp_Object *poly = v->contents;
28062 ptrdiff_t n = v->header.size;
28063 ptrdiff_t i;
28064 int inside = 0;
28065 Lisp_Object lx, ly;
28066 int x0, y0;
28067
28068 /* Need an even number of coordinates, and at least 3 edges. */
28069 if (n < 6 || n & 1)
28070 return 0;
28071
28072 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28073 If count is odd, we are inside polygon. Pixels on edges
28074 may or may not be included depending on actual geometry of the
28075 polygon. */
28076 if ((lx = poly[n-2], !INTEGERP (lx))
28077 || (ly = poly[n-1], !INTEGERP (lx)))
28078 return 0;
28079 x0 = XINT (lx), y0 = XINT (ly);
28080 for (i = 0; i < n; i += 2)
28081 {
28082 int x1 = x0, y1 = y0;
28083 if ((lx = poly[i], !INTEGERP (lx))
28084 || (ly = poly[i+1], !INTEGERP (ly)))
28085 return 0;
28086 x0 = XINT (lx), y0 = XINT (ly);
28087
28088 /* Does this segment cross the X line? */
28089 if (x0 >= x)
28090 {
28091 if (x1 >= x)
28092 continue;
28093 }
28094 else if (x1 < x)
28095 continue;
28096 if (y > y0 && y > y1)
28097 continue;
28098 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28099 inside = !inside;
28100 }
28101 return inside;
28102 }
28103 }
28104 return 0;
28105 }
28106
28107 Lisp_Object
28108 find_hot_spot (Lisp_Object map, int x, int y)
28109 {
28110 while (CONSP (map))
28111 {
28112 if (CONSP (XCAR (map))
28113 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28114 return XCAR (map);
28115 map = XCDR (map);
28116 }
28117
28118 return Qnil;
28119 }
28120
28121 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28122 3, 3, 0,
28123 doc: /* Lookup in image map MAP coordinates X and Y.
28124 An image map is an alist where each element has the format (AREA ID PLIST).
28125 An AREA is specified as either a rectangle, a circle, or a polygon:
28126 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28127 pixel coordinates of the upper left and bottom right corners.
28128 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28129 and the radius of the circle; r may be a float or integer.
28130 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28131 vector describes one corner in the polygon.
28132 Returns the alist element for the first matching AREA in MAP. */)
28133 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28134 {
28135 if (NILP (map))
28136 return Qnil;
28137
28138 CHECK_NUMBER (x);
28139 CHECK_NUMBER (y);
28140
28141 return find_hot_spot (map,
28142 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28143 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28144 }
28145
28146
28147 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28148 static void
28149 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28150 {
28151 /* Do not change cursor shape while dragging mouse. */
28152 if (!NILP (do_mouse_tracking))
28153 return;
28154
28155 if (!NILP (pointer))
28156 {
28157 if (EQ (pointer, Qarrow))
28158 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28159 else if (EQ (pointer, Qhand))
28160 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28161 else if (EQ (pointer, Qtext))
28162 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28163 else if (EQ (pointer, intern ("hdrag")))
28164 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28165 else if (EQ (pointer, intern ("nhdrag")))
28166 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28167 #ifdef HAVE_X_WINDOWS
28168 else if (EQ (pointer, intern ("vdrag")))
28169 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28170 #endif
28171 else if (EQ (pointer, intern ("hourglass")))
28172 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28173 else if (EQ (pointer, Qmodeline))
28174 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28175 else
28176 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28177 }
28178
28179 if (cursor != No_Cursor)
28180 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28181 }
28182
28183 #endif /* HAVE_WINDOW_SYSTEM */
28184
28185 /* Take proper action when mouse has moved to the mode or header line
28186 or marginal area AREA of window W, x-position X and y-position Y.
28187 X is relative to the start of the text display area of W, so the
28188 width of bitmap areas and scroll bars must be subtracted to get a
28189 position relative to the start of the mode line. */
28190
28191 static void
28192 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28193 enum window_part area)
28194 {
28195 struct window *w = XWINDOW (window);
28196 struct frame *f = XFRAME (w->frame);
28197 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28198 #ifdef HAVE_WINDOW_SYSTEM
28199 Display_Info *dpyinfo;
28200 #endif
28201 Cursor cursor = No_Cursor;
28202 Lisp_Object pointer = Qnil;
28203 int dx, dy, width, height;
28204 ptrdiff_t charpos;
28205 Lisp_Object string, object = Qnil;
28206 Lisp_Object pos IF_LINT (= Qnil), help;
28207
28208 Lisp_Object mouse_face;
28209 int original_x_pixel = x;
28210 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28211 struct glyph_row *row IF_LINT (= 0);
28212
28213 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28214 {
28215 int x0;
28216 struct glyph *end;
28217
28218 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28219 returns them in row/column units! */
28220 string = mode_line_string (w, area, &x, &y, &charpos,
28221 &object, &dx, &dy, &width, &height);
28222
28223 row = (area == ON_MODE_LINE
28224 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28225 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28226
28227 /* Find the glyph under the mouse pointer. */
28228 if (row->mode_line_p && row->enabled_p)
28229 {
28230 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28231 end = glyph + row->used[TEXT_AREA];
28232
28233 for (x0 = original_x_pixel;
28234 glyph < end && x0 >= glyph->pixel_width;
28235 ++glyph)
28236 x0 -= glyph->pixel_width;
28237
28238 if (glyph >= end)
28239 glyph = NULL;
28240 }
28241 }
28242 else
28243 {
28244 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28245 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28246 returns them in row/column units! */
28247 string = marginal_area_string (w, area, &x, &y, &charpos,
28248 &object, &dx, &dy, &width, &height);
28249 }
28250
28251 help = Qnil;
28252
28253 #ifdef HAVE_WINDOW_SYSTEM
28254 if (IMAGEP (object))
28255 {
28256 Lisp_Object image_map, hotspot;
28257 if ((image_map = Fplist_get (XCDR (object), QCmap),
28258 !NILP (image_map))
28259 && (hotspot = find_hot_spot (image_map, dx, dy),
28260 CONSP (hotspot))
28261 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28262 {
28263 Lisp_Object plist;
28264
28265 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28266 If so, we could look for mouse-enter, mouse-leave
28267 properties in PLIST (and do something...). */
28268 hotspot = XCDR (hotspot);
28269 if (CONSP (hotspot)
28270 && (plist = XCAR (hotspot), CONSP (plist)))
28271 {
28272 pointer = Fplist_get (plist, Qpointer);
28273 if (NILP (pointer))
28274 pointer = Qhand;
28275 help = Fplist_get (plist, Qhelp_echo);
28276 if (!NILP (help))
28277 {
28278 help_echo_string = help;
28279 XSETWINDOW (help_echo_window, w);
28280 help_echo_object = w->contents;
28281 help_echo_pos = charpos;
28282 }
28283 }
28284 }
28285 if (NILP (pointer))
28286 pointer = Fplist_get (XCDR (object), QCpointer);
28287 }
28288 #endif /* HAVE_WINDOW_SYSTEM */
28289
28290 if (STRINGP (string))
28291 pos = make_number (charpos);
28292
28293 /* Set the help text and mouse pointer. If the mouse is on a part
28294 of the mode line without any text (e.g. past the right edge of
28295 the mode line text), use the default help text and pointer. */
28296 if (STRINGP (string) || area == ON_MODE_LINE)
28297 {
28298 /* Arrange to display the help by setting the global variables
28299 help_echo_string, help_echo_object, and help_echo_pos. */
28300 if (NILP (help))
28301 {
28302 if (STRINGP (string))
28303 help = Fget_text_property (pos, Qhelp_echo, string);
28304
28305 if (!NILP (help))
28306 {
28307 help_echo_string = help;
28308 XSETWINDOW (help_echo_window, w);
28309 help_echo_object = string;
28310 help_echo_pos = charpos;
28311 }
28312 else if (area == ON_MODE_LINE)
28313 {
28314 Lisp_Object default_help
28315 = buffer_local_value_1 (Qmode_line_default_help_echo,
28316 w->contents);
28317
28318 if (STRINGP (default_help))
28319 {
28320 help_echo_string = default_help;
28321 XSETWINDOW (help_echo_window, w);
28322 help_echo_object = Qnil;
28323 help_echo_pos = -1;
28324 }
28325 }
28326 }
28327
28328 #ifdef HAVE_WINDOW_SYSTEM
28329 /* Change the mouse pointer according to what is under it. */
28330 if (FRAME_WINDOW_P (f))
28331 {
28332 dpyinfo = FRAME_DISPLAY_INFO (f);
28333 if (STRINGP (string))
28334 {
28335 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28336
28337 if (NILP (pointer))
28338 pointer = Fget_text_property (pos, Qpointer, string);
28339
28340 /* Change the mouse pointer according to what is under X/Y. */
28341 if (NILP (pointer)
28342 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28343 {
28344 Lisp_Object map;
28345 map = Fget_text_property (pos, Qlocal_map, string);
28346 if (!KEYMAPP (map))
28347 map = Fget_text_property (pos, Qkeymap, string);
28348 if (!KEYMAPP (map))
28349 cursor = dpyinfo->vertical_scroll_bar_cursor;
28350 }
28351 }
28352 else
28353 /* Default mode-line pointer. */
28354 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28355 }
28356 #endif
28357 }
28358
28359 /* Change the mouse face according to what is under X/Y. */
28360 if (STRINGP (string))
28361 {
28362 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28363 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28364 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28365 && glyph)
28366 {
28367 Lisp_Object b, e;
28368
28369 struct glyph * tmp_glyph;
28370
28371 int gpos;
28372 int gseq_length;
28373 int total_pixel_width;
28374 ptrdiff_t begpos, endpos, ignore;
28375
28376 int vpos, hpos;
28377
28378 b = Fprevious_single_property_change (make_number (charpos + 1),
28379 Qmouse_face, string, Qnil);
28380 if (NILP (b))
28381 begpos = 0;
28382 else
28383 begpos = XINT (b);
28384
28385 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28386 if (NILP (e))
28387 endpos = SCHARS (string);
28388 else
28389 endpos = XINT (e);
28390
28391 /* Calculate the glyph position GPOS of GLYPH in the
28392 displayed string, relative to the beginning of the
28393 highlighted part of the string.
28394
28395 Note: GPOS is different from CHARPOS. CHARPOS is the
28396 position of GLYPH in the internal string object. A mode
28397 line string format has structures which are converted to
28398 a flattened string by the Emacs Lisp interpreter. The
28399 internal string is an element of those structures. The
28400 displayed string is the flattened string. */
28401 tmp_glyph = row_start_glyph;
28402 while (tmp_glyph < glyph
28403 && (!(EQ (tmp_glyph->object, glyph->object)
28404 && begpos <= tmp_glyph->charpos
28405 && tmp_glyph->charpos < endpos)))
28406 tmp_glyph++;
28407 gpos = glyph - tmp_glyph;
28408
28409 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28410 the highlighted part of the displayed string to which
28411 GLYPH belongs. Note: GSEQ_LENGTH is different from
28412 SCHARS (STRING), because the latter returns the length of
28413 the internal string. */
28414 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28415 tmp_glyph > glyph
28416 && (!(EQ (tmp_glyph->object, glyph->object)
28417 && begpos <= tmp_glyph->charpos
28418 && tmp_glyph->charpos < endpos));
28419 tmp_glyph--)
28420 ;
28421 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28422
28423 /* Calculate the total pixel width of all the glyphs between
28424 the beginning of the highlighted area and GLYPH. */
28425 total_pixel_width = 0;
28426 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28427 total_pixel_width += tmp_glyph->pixel_width;
28428
28429 /* Pre calculation of re-rendering position. Note: X is in
28430 column units here, after the call to mode_line_string or
28431 marginal_area_string. */
28432 hpos = x - gpos;
28433 vpos = (area == ON_MODE_LINE
28434 ? (w->current_matrix)->nrows - 1
28435 : 0);
28436
28437 /* If GLYPH's position is included in the region that is
28438 already drawn in mouse face, we have nothing to do. */
28439 if ( EQ (window, hlinfo->mouse_face_window)
28440 && (!row->reversed_p
28441 ? (hlinfo->mouse_face_beg_col <= hpos
28442 && hpos < hlinfo->mouse_face_end_col)
28443 /* In R2L rows we swap BEG and END, see below. */
28444 : (hlinfo->mouse_face_end_col <= hpos
28445 && hpos < hlinfo->mouse_face_beg_col))
28446 && hlinfo->mouse_face_beg_row == vpos )
28447 return;
28448
28449 if (clear_mouse_face (hlinfo))
28450 cursor = No_Cursor;
28451
28452 if (!row->reversed_p)
28453 {
28454 hlinfo->mouse_face_beg_col = hpos;
28455 hlinfo->mouse_face_beg_x = original_x_pixel
28456 - (total_pixel_width + dx);
28457 hlinfo->mouse_face_end_col = hpos + gseq_length;
28458 hlinfo->mouse_face_end_x = 0;
28459 }
28460 else
28461 {
28462 /* In R2L rows, show_mouse_face expects BEG and END
28463 coordinates to be swapped. */
28464 hlinfo->mouse_face_end_col = hpos;
28465 hlinfo->mouse_face_end_x = original_x_pixel
28466 - (total_pixel_width + dx);
28467 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28468 hlinfo->mouse_face_beg_x = 0;
28469 }
28470
28471 hlinfo->mouse_face_beg_row = vpos;
28472 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28473 hlinfo->mouse_face_past_end = 0;
28474 hlinfo->mouse_face_window = window;
28475
28476 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28477 charpos,
28478 0, &ignore,
28479 glyph->face_id,
28480 1);
28481 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28482
28483 if (NILP (pointer))
28484 pointer = Qhand;
28485 }
28486 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28487 clear_mouse_face (hlinfo);
28488 }
28489 #ifdef HAVE_WINDOW_SYSTEM
28490 if (FRAME_WINDOW_P (f))
28491 define_frame_cursor1 (f, cursor, pointer);
28492 #endif
28493 }
28494
28495
28496 /* EXPORT:
28497 Take proper action when the mouse has moved to position X, Y on
28498 frame F with regards to highlighting portions of display that have
28499 mouse-face properties. Also de-highlight portions of display where
28500 the mouse was before, set the mouse pointer shape as appropriate
28501 for the mouse coordinates, and activate help echo (tooltips).
28502 X and Y can be negative or out of range. */
28503
28504 void
28505 note_mouse_highlight (struct frame *f, int x, int y)
28506 {
28507 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28508 enum window_part part = ON_NOTHING;
28509 Lisp_Object window;
28510 struct window *w;
28511 Cursor cursor = No_Cursor;
28512 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28513 struct buffer *b;
28514
28515 /* When a menu is active, don't highlight because this looks odd. */
28516 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28517 if (popup_activated ())
28518 return;
28519 #endif
28520
28521 if (!f->glyphs_initialized_p
28522 || f->pointer_invisible)
28523 return;
28524
28525 hlinfo->mouse_face_mouse_x = x;
28526 hlinfo->mouse_face_mouse_y = y;
28527 hlinfo->mouse_face_mouse_frame = f;
28528
28529 if (hlinfo->mouse_face_defer)
28530 return;
28531
28532 /* Which window is that in? */
28533 window = window_from_coordinates (f, x, y, &part, 1);
28534
28535 /* If displaying active text in another window, clear that. */
28536 if (! EQ (window, hlinfo->mouse_face_window)
28537 /* Also clear if we move out of text area in same window. */
28538 || (!NILP (hlinfo->mouse_face_window)
28539 && !NILP (window)
28540 && part != ON_TEXT
28541 && part != ON_MODE_LINE
28542 && part != ON_HEADER_LINE))
28543 clear_mouse_face (hlinfo);
28544
28545 /* Not on a window -> return. */
28546 if (!WINDOWP (window))
28547 return;
28548
28549 /* Reset help_echo_string. It will get recomputed below. */
28550 help_echo_string = Qnil;
28551
28552 /* Convert to window-relative pixel coordinates. */
28553 w = XWINDOW (window);
28554 frame_to_window_pixel_xy (w, &x, &y);
28555
28556 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28557 /* Handle tool-bar window differently since it doesn't display a
28558 buffer. */
28559 if (EQ (window, f->tool_bar_window))
28560 {
28561 note_tool_bar_highlight (f, x, y);
28562 return;
28563 }
28564 #endif
28565
28566 /* Mouse is on the mode, header line or margin? */
28567 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28568 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28569 {
28570 note_mode_line_or_margin_highlight (window, x, y, part);
28571 return;
28572 }
28573
28574 #ifdef HAVE_WINDOW_SYSTEM
28575 if (part == ON_VERTICAL_BORDER)
28576 {
28577 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28578 help_echo_string = build_string ("drag-mouse-1: resize");
28579 }
28580 else if (part == ON_RIGHT_DIVIDER)
28581 {
28582 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28583 help_echo_string = build_string ("drag-mouse-1: resize");
28584 }
28585 else if (part == ON_BOTTOM_DIVIDER)
28586 {
28587 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28588 help_echo_string = build_string ("drag-mouse-1: resize");
28589 }
28590 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28591 || part == ON_SCROLL_BAR)
28592 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28593 else
28594 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28595 #endif
28596
28597 /* Are we in a window whose display is up to date?
28598 And verify the buffer's text has not changed. */
28599 b = XBUFFER (w->contents);
28600 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28601 {
28602 int hpos, vpos, dx, dy, area = LAST_AREA;
28603 ptrdiff_t pos;
28604 struct glyph *glyph;
28605 Lisp_Object object;
28606 Lisp_Object mouse_face = Qnil, position;
28607 Lisp_Object *overlay_vec = NULL;
28608 ptrdiff_t i, noverlays;
28609 struct buffer *obuf;
28610 ptrdiff_t obegv, ozv;
28611 int same_region;
28612
28613 /* Find the glyph under X/Y. */
28614 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28615
28616 #ifdef HAVE_WINDOW_SYSTEM
28617 /* Look for :pointer property on image. */
28618 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28619 {
28620 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28621 if (img != NULL && IMAGEP (img->spec))
28622 {
28623 Lisp_Object image_map, hotspot;
28624 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28625 !NILP (image_map))
28626 && (hotspot = find_hot_spot (image_map,
28627 glyph->slice.img.x + dx,
28628 glyph->slice.img.y + dy),
28629 CONSP (hotspot))
28630 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28631 {
28632 Lisp_Object plist;
28633
28634 /* Could check XCAR (hotspot) to see if we enter/leave
28635 this hot-spot.
28636 If so, we could look for mouse-enter, mouse-leave
28637 properties in PLIST (and do something...). */
28638 hotspot = XCDR (hotspot);
28639 if (CONSP (hotspot)
28640 && (plist = XCAR (hotspot), CONSP (plist)))
28641 {
28642 pointer = Fplist_get (plist, Qpointer);
28643 if (NILP (pointer))
28644 pointer = Qhand;
28645 help_echo_string = Fplist_get (plist, Qhelp_echo);
28646 if (!NILP (help_echo_string))
28647 {
28648 help_echo_window = window;
28649 help_echo_object = glyph->object;
28650 help_echo_pos = glyph->charpos;
28651 }
28652 }
28653 }
28654 if (NILP (pointer))
28655 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28656 }
28657 }
28658 #endif /* HAVE_WINDOW_SYSTEM */
28659
28660 /* Clear mouse face if X/Y not over text. */
28661 if (glyph == NULL
28662 || area != TEXT_AREA
28663 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28664 /* Glyph's OBJECT is an integer for glyphs inserted by the
28665 display engine for its internal purposes, like truncation
28666 and continuation glyphs and blanks beyond the end of
28667 line's text on text terminals. If we are over such a
28668 glyph, we are not over any text. */
28669 || INTEGERP (glyph->object)
28670 /* R2L rows have a stretch glyph at their front, which
28671 stands for no text, whereas L2R rows have no glyphs at
28672 all beyond the end of text. Treat such stretch glyphs
28673 like we do with NULL glyphs in L2R rows. */
28674 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28675 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28676 && glyph->type == STRETCH_GLYPH
28677 && glyph->avoid_cursor_p))
28678 {
28679 if (clear_mouse_face (hlinfo))
28680 cursor = No_Cursor;
28681 #ifdef HAVE_WINDOW_SYSTEM
28682 if (FRAME_WINDOW_P (f) && NILP (pointer))
28683 {
28684 if (area != TEXT_AREA)
28685 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28686 else
28687 pointer = Vvoid_text_area_pointer;
28688 }
28689 #endif
28690 goto set_cursor;
28691 }
28692
28693 pos = glyph->charpos;
28694 object = glyph->object;
28695 if (!STRINGP (object) && !BUFFERP (object))
28696 goto set_cursor;
28697
28698 /* If we get an out-of-range value, return now; avoid an error. */
28699 if (BUFFERP (object) && pos > BUF_Z (b))
28700 goto set_cursor;
28701
28702 /* Make the window's buffer temporarily current for
28703 overlays_at and compute_char_face. */
28704 obuf = current_buffer;
28705 current_buffer = b;
28706 obegv = BEGV;
28707 ozv = ZV;
28708 BEGV = BEG;
28709 ZV = Z;
28710
28711 /* Is this char mouse-active or does it have help-echo? */
28712 position = make_number (pos);
28713
28714 if (BUFFERP (object))
28715 {
28716 /* Put all the overlays we want in a vector in overlay_vec. */
28717 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28718 /* Sort overlays into increasing priority order. */
28719 noverlays = sort_overlays (overlay_vec, noverlays, w);
28720 }
28721 else
28722 noverlays = 0;
28723
28724 if (NILP (Vmouse_highlight))
28725 {
28726 clear_mouse_face (hlinfo);
28727 goto check_help_echo;
28728 }
28729
28730 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28731
28732 if (same_region)
28733 cursor = No_Cursor;
28734
28735 /* Check mouse-face highlighting. */
28736 if (! same_region
28737 /* If there exists an overlay with mouse-face overlapping
28738 the one we are currently highlighting, we have to
28739 check if we enter the overlapping overlay, and then
28740 highlight only that. */
28741 || (OVERLAYP (hlinfo->mouse_face_overlay)
28742 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28743 {
28744 /* Find the highest priority overlay with a mouse-face. */
28745 Lisp_Object overlay = Qnil;
28746 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28747 {
28748 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28749 if (!NILP (mouse_face))
28750 overlay = overlay_vec[i];
28751 }
28752
28753 /* If we're highlighting the same overlay as before, there's
28754 no need to do that again. */
28755 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28756 goto check_help_echo;
28757 hlinfo->mouse_face_overlay = overlay;
28758
28759 /* Clear the display of the old active region, if any. */
28760 if (clear_mouse_face (hlinfo))
28761 cursor = No_Cursor;
28762
28763 /* If no overlay applies, get a text property. */
28764 if (NILP (overlay))
28765 mouse_face = Fget_text_property (position, Qmouse_face, object);
28766
28767 /* Next, compute the bounds of the mouse highlighting and
28768 display it. */
28769 if (!NILP (mouse_face) && STRINGP (object))
28770 {
28771 /* The mouse-highlighting comes from a display string
28772 with a mouse-face. */
28773 Lisp_Object s, e;
28774 ptrdiff_t ignore;
28775
28776 s = Fprevious_single_property_change
28777 (make_number (pos + 1), Qmouse_face, object, Qnil);
28778 e = Fnext_single_property_change
28779 (position, Qmouse_face, object, Qnil);
28780 if (NILP (s))
28781 s = make_number (0);
28782 if (NILP (e))
28783 e = make_number (SCHARS (object));
28784 mouse_face_from_string_pos (w, hlinfo, object,
28785 XINT (s), XINT (e));
28786 hlinfo->mouse_face_past_end = 0;
28787 hlinfo->mouse_face_window = window;
28788 hlinfo->mouse_face_face_id
28789 = face_at_string_position (w, object, pos, 0, &ignore,
28790 glyph->face_id, 1);
28791 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28792 cursor = No_Cursor;
28793 }
28794 else
28795 {
28796 /* The mouse-highlighting, if any, comes from an overlay
28797 or text property in the buffer. */
28798 Lisp_Object buffer IF_LINT (= Qnil);
28799 Lisp_Object disp_string IF_LINT (= Qnil);
28800
28801 if (STRINGP (object))
28802 {
28803 /* If we are on a display string with no mouse-face,
28804 check if the text under it has one. */
28805 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28806 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28807 pos = string_buffer_position (object, start);
28808 if (pos > 0)
28809 {
28810 mouse_face = get_char_property_and_overlay
28811 (make_number (pos), Qmouse_face, w->contents, &overlay);
28812 buffer = w->contents;
28813 disp_string = object;
28814 }
28815 }
28816 else
28817 {
28818 buffer = object;
28819 disp_string = Qnil;
28820 }
28821
28822 if (!NILP (mouse_face))
28823 {
28824 Lisp_Object before, after;
28825 Lisp_Object before_string, after_string;
28826 /* To correctly find the limits of mouse highlight
28827 in a bidi-reordered buffer, we must not use the
28828 optimization of limiting the search in
28829 previous-single-property-change and
28830 next-single-property-change, because
28831 rows_from_pos_range needs the real start and end
28832 positions to DTRT in this case. That's because
28833 the first row visible in a window does not
28834 necessarily display the character whose position
28835 is the smallest. */
28836 Lisp_Object lim1
28837 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28838 ? Fmarker_position (w->start)
28839 : Qnil;
28840 Lisp_Object lim2
28841 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28842 ? make_number (BUF_Z (XBUFFER (buffer))
28843 - w->window_end_pos)
28844 : Qnil;
28845
28846 if (NILP (overlay))
28847 {
28848 /* Handle the text property case. */
28849 before = Fprevious_single_property_change
28850 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28851 after = Fnext_single_property_change
28852 (make_number (pos), Qmouse_face, buffer, lim2);
28853 before_string = after_string = Qnil;
28854 }
28855 else
28856 {
28857 /* Handle the overlay case. */
28858 before = Foverlay_start (overlay);
28859 after = Foverlay_end (overlay);
28860 before_string = Foverlay_get (overlay, Qbefore_string);
28861 after_string = Foverlay_get (overlay, Qafter_string);
28862
28863 if (!STRINGP (before_string)) before_string = Qnil;
28864 if (!STRINGP (after_string)) after_string = Qnil;
28865 }
28866
28867 mouse_face_from_buffer_pos (window, hlinfo, pos,
28868 NILP (before)
28869 ? 1
28870 : XFASTINT (before),
28871 NILP (after)
28872 ? BUF_Z (XBUFFER (buffer))
28873 : XFASTINT (after),
28874 before_string, after_string,
28875 disp_string);
28876 cursor = No_Cursor;
28877 }
28878 }
28879 }
28880
28881 check_help_echo:
28882
28883 /* Look for a `help-echo' property. */
28884 if (NILP (help_echo_string)) {
28885 Lisp_Object help, overlay;
28886
28887 /* Check overlays first. */
28888 help = overlay = Qnil;
28889 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28890 {
28891 overlay = overlay_vec[i];
28892 help = Foverlay_get (overlay, Qhelp_echo);
28893 }
28894
28895 if (!NILP (help))
28896 {
28897 help_echo_string = help;
28898 help_echo_window = window;
28899 help_echo_object = overlay;
28900 help_echo_pos = pos;
28901 }
28902 else
28903 {
28904 Lisp_Object obj = glyph->object;
28905 ptrdiff_t charpos = glyph->charpos;
28906
28907 /* Try text properties. */
28908 if (STRINGP (obj)
28909 && charpos >= 0
28910 && charpos < SCHARS (obj))
28911 {
28912 help = Fget_text_property (make_number (charpos),
28913 Qhelp_echo, obj);
28914 if (NILP (help))
28915 {
28916 /* If the string itself doesn't specify a help-echo,
28917 see if the buffer text ``under'' it does. */
28918 struct glyph_row *r
28919 = MATRIX_ROW (w->current_matrix, vpos);
28920 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28921 ptrdiff_t p = string_buffer_position (obj, start);
28922 if (p > 0)
28923 {
28924 help = Fget_char_property (make_number (p),
28925 Qhelp_echo, w->contents);
28926 if (!NILP (help))
28927 {
28928 charpos = p;
28929 obj = w->contents;
28930 }
28931 }
28932 }
28933 }
28934 else if (BUFFERP (obj)
28935 && charpos >= BEGV
28936 && charpos < ZV)
28937 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28938 obj);
28939
28940 if (!NILP (help))
28941 {
28942 help_echo_string = help;
28943 help_echo_window = window;
28944 help_echo_object = obj;
28945 help_echo_pos = charpos;
28946 }
28947 }
28948 }
28949
28950 #ifdef HAVE_WINDOW_SYSTEM
28951 /* Look for a `pointer' property. */
28952 if (FRAME_WINDOW_P (f) && NILP (pointer))
28953 {
28954 /* Check overlays first. */
28955 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28956 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28957
28958 if (NILP (pointer))
28959 {
28960 Lisp_Object obj = glyph->object;
28961 ptrdiff_t charpos = glyph->charpos;
28962
28963 /* Try text properties. */
28964 if (STRINGP (obj)
28965 && charpos >= 0
28966 && charpos < SCHARS (obj))
28967 {
28968 pointer = Fget_text_property (make_number (charpos),
28969 Qpointer, obj);
28970 if (NILP (pointer))
28971 {
28972 /* If the string itself doesn't specify a pointer,
28973 see if the buffer text ``under'' it does. */
28974 struct glyph_row *r
28975 = MATRIX_ROW (w->current_matrix, vpos);
28976 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28977 ptrdiff_t p = string_buffer_position (obj, start);
28978 if (p > 0)
28979 pointer = Fget_char_property (make_number (p),
28980 Qpointer, w->contents);
28981 }
28982 }
28983 else if (BUFFERP (obj)
28984 && charpos >= BEGV
28985 && charpos < ZV)
28986 pointer = Fget_text_property (make_number (charpos),
28987 Qpointer, obj);
28988 }
28989 }
28990 #endif /* HAVE_WINDOW_SYSTEM */
28991
28992 BEGV = obegv;
28993 ZV = ozv;
28994 current_buffer = obuf;
28995 }
28996
28997 set_cursor:
28998
28999 #ifdef HAVE_WINDOW_SYSTEM
29000 if (FRAME_WINDOW_P (f))
29001 define_frame_cursor1 (f, cursor, pointer);
29002 #else
29003 /* This is here to prevent a compiler error, about "label at end of
29004 compound statement". */
29005 return;
29006 #endif
29007 }
29008
29009
29010 /* EXPORT for RIF:
29011 Clear any mouse-face on window W. This function is part of the
29012 redisplay interface, and is called from try_window_id and similar
29013 functions to ensure the mouse-highlight is off. */
29014
29015 void
29016 x_clear_window_mouse_face (struct window *w)
29017 {
29018 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29019 Lisp_Object window;
29020
29021 block_input ();
29022 XSETWINDOW (window, w);
29023 if (EQ (window, hlinfo->mouse_face_window))
29024 clear_mouse_face (hlinfo);
29025 unblock_input ();
29026 }
29027
29028
29029 /* EXPORT:
29030 Just discard the mouse face information for frame F, if any.
29031 This is used when the size of F is changed. */
29032
29033 void
29034 cancel_mouse_face (struct frame *f)
29035 {
29036 Lisp_Object window;
29037 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29038
29039 window = hlinfo->mouse_face_window;
29040 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29041 reset_mouse_highlight (hlinfo);
29042 }
29043
29044
29045 \f
29046 /***********************************************************************
29047 Exposure Events
29048 ***********************************************************************/
29049
29050 #ifdef HAVE_WINDOW_SYSTEM
29051
29052 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29053 which intersects rectangle R. R is in window-relative coordinates. */
29054
29055 static void
29056 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29057 enum glyph_row_area area)
29058 {
29059 struct glyph *first = row->glyphs[area];
29060 struct glyph *end = row->glyphs[area] + row->used[area];
29061 struct glyph *last;
29062 int first_x, start_x, x;
29063
29064 if (area == TEXT_AREA && row->fill_line_p)
29065 /* If row extends face to end of line write the whole line. */
29066 draw_glyphs (w, 0, row, area,
29067 0, row->used[area],
29068 DRAW_NORMAL_TEXT, 0);
29069 else
29070 {
29071 /* Set START_X to the window-relative start position for drawing glyphs of
29072 AREA. The first glyph of the text area can be partially visible.
29073 The first glyphs of other areas cannot. */
29074 start_x = window_box_left_offset (w, area);
29075 x = start_x;
29076 if (area == TEXT_AREA)
29077 x += row->x;
29078
29079 /* Find the first glyph that must be redrawn. */
29080 while (first < end
29081 && x + first->pixel_width < r->x)
29082 {
29083 x += first->pixel_width;
29084 ++first;
29085 }
29086
29087 /* Find the last one. */
29088 last = first;
29089 first_x = x;
29090 while (last < end
29091 && x < r->x + r->width)
29092 {
29093 x += last->pixel_width;
29094 ++last;
29095 }
29096
29097 /* Repaint. */
29098 if (last > first)
29099 draw_glyphs (w, first_x - start_x, row, area,
29100 first - row->glyphs[area], last - row->glyphs[area],
29101 DRAW_NORMAL_TEXT, 0);
29102 }
29103 }
29104
29105
29106 /* Redraw the parts of the glyph row ROW on window W intersecting
29107 rectangle R. R is in window-relative coordinates. Value is
29108 non-zero if mouse-face was overwritten. */
29109
29110 static int
29111 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29112 {
29113 eassert (row->enabled_p);
29114
29115 if (row->mode_line_p || w->pseudo_window_p)
29116 draw_glyphs (w, 0, row, TEXT_AREA,
29117 0, row->used[TEXT_AREA],
29118 DRAW_NORMAL_TEXT, 0);
29119 else
29120 {
29121 if (row->used[LEFT_MARGIN_AREA])
29122 expose_area (w, row, r, LEFT_MARGIN_AREA);
29123 if (row->used[TEXT_AREA])
29124 expose_area (w, row, r, TEXT_AREA);
29125 if (row->used[RIGHT_MARGIN_AREA])
29126 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29127 draw_row_fringe_bitmaps (w, row);
29128 }
29129
29130 return row->mouse_face_p;
29131 }
29132
29133
29134 /* Redraw those parts of glyphs rows during expose event handling that
29135 overlap other rows. Redrawing of an exposed line writes over parts
29136 of lines overlapping that exposed line; this function fixes that.
29137
29138 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29139 row in W's current matrix that is exposed and overlaps other rows.
29140 LAST_OVERLAPPING_ROW is the last such row. */
29141
29142 static void
29143 expose_overlaps (struct window *w,
29144 struct glyph_row *first_overlapping_row,
29145 struct glyph_row *last_overlapping_row,
29146 XRectangle *r)
29147 {
29148 struct glyph_row *row;
29149
29150 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29151 if (row->overlapping_p)
29152 {
29153 eassert (row->enabled_p && !row->mode_line_p);
29154
29155 row->clip = r;
29156 if (row->used[LEFT_MARGIN_AREA])
29157 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29158
29159 if (row->used[TEXT_AREA])
29160 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29161
29162 if (row->used[RIGHT_MARGIN_AREA])
29163 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29164 row->clip = NULL;
29165 }
29166 }
29167
29168
29169 /* Return non-zero if W's cursor intersects rectangle R. */
29170
29171 static int
29172 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29173 {
29174 XRectangle cr, result;
29175 struct glyph *cursor_glyph;
29176 struct glyph_row *row;
29177
29178 if (w->phys_cursor.vpos >= 0
29179 && w->phys_cursor.vpos < w->current_matrix->nrows
29180 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29181 row->enabled_p)
29182 && row->cursor_in_fringe_p)
29183 {
29184 /* Cursor is in the fringe. */
29185 cr.x = window_box_right_offset (w,
29186 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29187 ? RIGHT_MARGIN_AREA
29188 : TEXT_AREA));
29189 cr.y = row->y;
29190 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29191 cr.height = row->height;
29192 return x_intersect_rectangles (&cr, r, &result);
29193 }
29194
29195 cursor_glyph = get_phys_cursor_glyph (w);
29196 if (cursor_glyph)
29197 {
29198 /* r is relative to W's box, but w->phys_cursor.x is relative
29199 to left edge of W's TEXT area. Adjust it. */
29200 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29201 cr.y = w->phys_cursor.y;
29202 cr.width = cursor_glyph->pixel_width;
29203 cr.height = w->phys_cursor_height;
29204 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29205 I assume the effect is the same -- and this is portable. */
29206 return x_intersect_rectangles (&cr, r, &result);
29207 }
29208 /* If we don't understand the format, pretend we're not in the hot-spot. */
29209 return 0;
29210 }
29211
29212
29213 /* EXPORT:
29214 Draw a vertical window border to the right of window W if W doesn't
29215 have vertical scroll bars. */
29216
29217 void
29218 x_draw_vertical_border (struct window *w)
29219 {
29220 struct frame *f = XFRAME (WINDOW_FRAME (w));
29221
29222 /* We could do better, if we knew what type of scroll-bar the adjacent
29223 windows (on either side) have... But we don't :-(
29224 However, I think this works ok. ++KFS 2003-04-25 */
29225
29226 /* Redraw borders between horizontally adjacent windows. Don't
29227 do it for frames with vertical scroll bars because either the
29228 right scroll bar of a window, or the left scroll bar of its
29229 neighbor will suffice as a border. */
29230 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29231 return;
29232
29233 /* Note: It is necessary to redraw both the left and the right
29234 borders, for when only this single window W is being
29235 redisplayed. */
29236 if (!WINDOW_RIGHTMOST_P (w)
29237 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29238 {
29239 int x0, x1, y0, y1;
29240
29241 window_box_edges (w, &x0, &y0, &x1, &y1);
29242 y1 -= 1;
29243
29244 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29245 x1 -= 1;
29246
29247 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29248 }
29249
29250 if (!WINDOW_LEFTMOST_P (w)
29251 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29252 {
29253 int x0, x1, y0, y1;
29254
29255 window_box_edges (w, &x0, &y0, &x1, &y1);
29256 y1 -= 1;
29257
29258 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29259 x0 -= 1;
29260
29261 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29262 }
29263 }
29264
29265
29266 /* Draw window dividers for window W. */
29267
29268 void
29269 x_draw_right_divider (struct window *w)
29270 {
29271 struct frame *f = WINDOW_XFRAME (w);
29272
29273 if (w->mini || w->pseudo_window_p)
29274 return;
29275 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29276 {
29277 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29278 int x1 = WINDOW_RIGHT_EDGE_X (w);
29279 int y0 = WINDOW_TOP_EDGE_Y (w);
29280 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29281
29282 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29283 }
29284 }
29285
29286 static void
29287 x_draw_bottom_divider (struct window *w)
29288 {
29289 struct frame *f = XFRAME (WINDOW_FRAME (w));
29290
29291 if (w->mini || w->pseudo_window_p)
29292 return;
29293 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29294 {
29295 int x0 = WINDOW_LEFT_EDGE_X (w);
29296 int x1 = WINDOW_RIGHT_EDGE_X (w);
29297 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29298 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29299
29300 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29301 }
29302 }
29303
29304 /* Redraw the part of window W intersection rectangle FR. Pixel
29305 coordinates in FR are frame-relative. Call this function with
29306 input blocked. Value is non-zero if the exposure overwrites
29307 mouse-face. */
29308
29309 static int
29310 expose_window (struct window *w, XRectangle *fr)
29311 {
29312 struct frame *f = XFRAME (w->frame);
29313 XRectangle wr, r;
29314 int mouse_face_overwritten_p = 0;
29315
29316 /* If window is not yet fully initialized, do nothing. This can
29317 happen when toolkit scroll bars are used and a window is split.
29318 Reconfiguring the scroll bar will generate an expose for a newly
29319 created window. */
29320 if (w->current_matrix == NULL)
29321 return 0;
29322
29323 /* When we're currently updating the window, display and current
29324 matrix usually don't agree. Arrange for a thorough display
29325 later. */
29326 if (w->must_be_updated_p)
29327 {
29328 SET_FRAME_GARBAGED (f);
29329 return 0;
29330 }
29331
29332 /* Frame-relative pixel rectangle of W. */
29333 wr.x = WINDOW_LEFT_EDGE_X (w);
29334 wr.y = WINDOW_TOP_EDGE_Y (w);
29335 wr.width = WINDOW_PIXEL_WIDTH (w);
29336 wr.height = WINDOW_PIXEL_HEIGHT (w);
29337
29338 if (x_intersect_rectangles (fr, &wr, &r))
29339 {
29340 int yb = window_text_bottom_y (w);
29341 struct glyph_row *row;
29342 int cursor_cleared_p, phys_cursor_on_p;
29343 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29344
29345 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29346 r.x, r.y, r.width, r.height));
29347
29348 /* Convert to window coordinates. */
29349 r.x -= WINDOW_LEFT_EDGE_X (w);
29350 r.y -= WINDOW_TOP_EDGE_Y (w);
29351
29352 /* Turn off the cursor. */
29353 if (!w->pseudo_window_p
29354 && phys_cursor_in_rect_p (w, &r))
29355 {
29356 x_clear_cursor (w);
29357 cursor_cleared_p = 1;
29358 }
29359 else
29360 cursor_cleared_p = 0;
29361
29362 /* If the row containing the cursor extends face to end of line,
29363 then expose_area might overwrite the cursor outside the
29364 rectangle and thus notice_overwritten_cursor might clear
29365 w->phys_cursor_on_p. We remember the original value and
29366 check later if it is changed. */
29367 phys_cursor_on_p = w->phys_cursor_on_p;
29368
29369 /* Update lines intersecting rectangle R. */
29370 first_overlapping_row = last_overlapping_row = NULL;
29371 for (row = w->current_matrix->rows;
29372 row->enabled_p;
29373 ++row)
29374 {
29375 int y0 = row->y;
29376 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29377
29378 if ((y0 >= r.y && y0 < r.y + r.height)
29379 || (y1 > r.y && y1 < r.y + r.height)
29380 || (r.y >= y0 && r.y < y1)
29381 || (r.y + r.height > y0 && r.y + r.height < y1))
29382 {
29383 /* A header line may be overlapping, but there is no need
29384 to fix overlapping areas for them. KFS 2005-02-12 */
29385 if (row->overlapping_p && !row->mode_line_p)
29386 {
29387 if (first_overlapping_row == NULL)
29388 first_overlapping_row = row;
29389 last_overlapping_row = row;
29390 }
29391
29392 row->clip = fr;
29393 if (expose_line (w, row, &r))
29394 mouse_face_overwritten_p = 1;
29395 row->clip = NULL;
29396 }
29397 else if (row->overlapping_p)
29398 {
29399 /* We must redraw a row overlapping the exposed area. */
29400 if (y0 < r.y
29401 ? y0 + row->phys_height > r.y
29402 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29403 {
29404 if (first_overlapping_row == NULL)
29405 first_overlapping_row = row;
29406 last_overlapping_row = row;
29407 }
29408 }
29409
29410 if (y1 >= yb)
29411 break;
29412 }
29413
29414 /* Display the mode line if there is one. */
29415 if (WINDOW_WANTS_MODELINE_P (w)
29416 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29417 row->enabled_p)
29418 && row->y < r.y + r.height)
29419 {
29420 if (expose_line (w, row, &r))
29421 mouse_face_overwritten_p = 1;
29422 }
29423
29424 if (!w->pseudo_window_p)
29425 {
29426 /* Fix the display of overlapping rows. */
29427 if (first_overlapping_row)
29428 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29429 fr);
29430
29431 /* Draw border between windows. */
29432 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29433 x_draw_right_divider (w);
29434 else
29435 x_draw_vertical_border (w);
29436
29437 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29438 x_draw_bottom_divider (w);
29439
29440 /* Turn the cursor on again. */
29441 if (cursor_cleared_p
29442 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29443 update_window_cursor (w, 1);
29444 }
29445 }
29446
29447 return mouse_face_overwritten_p;
29448 }
29449
29450
29451
29452 /* Redraw (parts) of all windows in the window tree rooted at W that
29453 intersect R. R contains frame pixel coordinates. Value is
29454 non-zero if the exposure overwrites mouse-face. */
29455
29456 static int
29457 expose_window_tree (struct window *w, XRectangle *r)
29458 {
29459 struct frame *f = XFRAME (w->frame);
29460 int mouse_face_overwritten_p = 0;
29461
29462 while (w && !FRAME_GARBAGED_P (f))
29463 {
29464 if (WINDOWP (w->contents))
29465 mouse_face_overwritten_p
29466 |= expose_window_tree (XWINDOW (w->contents), r);
29467 else
29468 mouse_face_overwritten_p |= expose_window (w, r);
29469
29470 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29471 }
29472
29473 return mouse_face_overwritten_p;
29474 }
29475
29476
29477 /* EXPORT:
29478 Redisplay an exposed area of frame F. X and Y are the upper-left
29479 corner of the exposed rectangle. W and H are width and height of
29480 the exposed area. All are pixel values. W or H zero means redraw
29481 the entire frame. */
29482
29483 void
29484 expose_frame (struct frame *f, int x, int y, int w, int h)
29485 {
29486 XRectangle r;
29487 int mouse_face_overwritten_p = 0;
29488
29489 TRACE ((stderr, "expose_frame "));
29490
29491 /* No need to redraw if frame will be redrawn soon. */
29492 if (FRAME_GARBAGED_P (f))
29493 {
29494 TRACE ((stderr, " garbaged\n"));
29495 return;
29496 }
29497
29498 /* If basic faces haven't been realized yet, there is no point in
29499 trying to redraw anything. This can happen when we get an expose
29500 event while Emacs is starting, e.g. by moving another window. */
29501 if (FRAME_FACE_CACHE (f) == NULL
29502 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29503 {
29504 TRACE ((stderr, " no faces\n"));
29505 return;
29506 }
29507
29508 if (w == 0 || h == 0)
29509 {
29510 r.x = r.y = 0;
29511 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29512 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29513 }
29514 else
29515 {
29516 r.x = x;
29517 r.y = y;
29518 r.width = w;
29519 r.height = h;
29520 }
29521
29522 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29523 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29524
29525 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29526 if (WINDOWP (f->tool_bar_window))
29527 mouse_face_overwritten_p
29528 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29529 #endif
29530
29531 #ifdef HAVE_X_WINDOWS
29532 #ifndef MSDOS
29533 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29534 if (WINDOWP (f->menu_bar_window))
29535 mouse_face_overwritten_p
29536 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29537 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29538 #endif
29539 #endif
29540
29541 /* Some window managers support a focus-follows-mouse style with
29542 delayed raising of frames. Imagine a partially obscured frame,
29543 and moving the mouse into partially obscured mouse-face on that
29544 frame. The visible part of the mouse-face will be highlighted,
29545 then the WM raises the obscured frame. With at least one WM, KDE
29546 2.1, Emacs is not getting any event for the raising of the frame
29547 (even tried with SubstructureRedirectMask), only Expose events.
29548 These expose events will draw text normally, i.e. not
29549 highlighted. Which means we must redo the highlight here.
29550 Subsume it under ``we love X''. --gerd 2001-08-15 */
29551 /* Included in Windows version because Windows most likely does not
29552 do the right thing if any third party tool offers
29553 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29554 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29555 {
29556 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29557 if (f == hlinfo->mouse_face_mouse_frame)
29558 {
29559 int mouse_x = hlinfo->mouse_face_mouse_x;
29560 int mouse_y = hlinfo->mouse_face_mouse_y;
29561 clear_mouse_face (hlinfo);
29562 note_mouse_highlight (f, mouse_x, mouse_y);
29563 }
29564 }
29565 }
29566
29567
29568 /* EXPORT:
29569 Determine the intersection of two rectangles R1 and R2. Return
29570 the intersection in *RESULT. Value is non-zero if RESULT is not
29571 empty. */
29572
29573 int
29574 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29575 {
29576 XRectangle *left, *right;
29577 XRectangle *upper, *lower;
29578 int intersection_p = 0;
29579
29580 /* Rearrange so that R1 is the left-most rectangle. */
29581 if (r1->x < r2->x)
29582 left = r1, right = r2;
29583 else
29584 left = r2, right = r1;
29585
29586 /* X0 of the intersection is right.x0, if this is inside R1,
29587 otherwise there is no intersection. */
29588 if (right->x <= left->x + left->width)
29589 {
29590 result->x = right->x;
29591
29592 /* The right end of the intersection is the minimum of
29593 the right ends of left and right. */
29594 result->width = (min (left->x + left->width, right->x + right->width)
29595 - result->x);
29596
29597 /* Same game for Y. */
29598 if (r1->y < r2->y)
29599 upper = r1, lower = r2;
29600 else
29601 upper = r2, lower = r1;
29602
29603 /* The upper end of the intersection is lower.y0, if this is inside
29604 of upper. Otherwise, there is no intersection. */
29605 if (lower->y <= upper->y + upper->height)
29606 {
29607 result->y = lower->y;
29608
29609 /* The lower end of the intersection is the minimum of the lower
29610 ends of upper and lower. */
29611 result->height = (min (lower->y + lower->height,
29612 upper->y + upper->height)
29613 - result->y);
29614 intersection_p = 1;
29615 }
29616 }
29617
29618 return intersection_p;
29619 }
29620
29621 #endif /* HAVE_WINDOW_SYSTEM */
29622
29623 \f
29624 /***********************************************************************
29625 Initialization
29626 ***********************************************************************/
29627
29628 void
29629 syms_of_xdisp (void)
29630 {
29631 Vwith_echo_area_save_vector = Qnil;
29632 staticpro (&Vwith_echo_area_save_vector);
29633
29634 Vmessage_stack = Qnil;
29635 staticpro (&Vmessage_stack);
29636
29637 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29638 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29639
29640 message_dolog_marker1 = Fmake_marker ();
29641 staticpro (&message_dolog_marker1);
29642 message_dolog_marker2 = Fmake_marker ();
29643 staticpro (&message_dolog_marker2);
29644 message_dolog_marker3 = Fmake_marker ();
29645 staticpro (&message_dolog_marker3);
29646
29647 #ifdef GLYPH_DEBUG
29648 defsubr (&Sdump_frame_glyph_matrix);
29649 defsubr (&Sdump_glyph_matrix);
29650 defsubr (&Sdump_glyph_row);
29651 defsubr (&Sdump_tool_bar_row);
29652 defsubr (&Strace_redisplay);
29653 defsubr (&Strace_to_stderr);
29654 #endif
29655 #ifdef HAVE_WINDOW_SYSTEM
29656 defsubr (&Stool_bar_height);
29657 defsubr (&Slookup_image_map);
29658 #endif
29659 defsubr (&Sline_pixel_height);
29660 defsubr (&Sformat_mode_line);
29661 defsubr (&Sinvisible_p);
29662 defsubr (&Scurrent_bidi_paragraph_direction);
29663 defsubr (&Swindow_text_pixel_size);
29664 defsubr (&Smove_point_visually);
29665
29666 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29667 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29668 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29669 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29670 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29671 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29672 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29673 DEFSYM (Qeval, "eval");
29674 DEFSYM (QCdata, ":data");
29675 DEFSYM (Qdisplay, "display");
29676 DEFSYM (Qspace_width, "space-width");
29677 DEFSYM (Qraise, "raise");
29678 DEFSYM (Qslice, "slice");
29679 DEFSYM (Qspace, "space");
29680 DEFSYM (Qmargin, "margin");
29681 DEFSYM (Qpointer, "pointer");
29682 DEFSYM (Qleft_margin, "left-margin");
29683 DEFSYM (Qright_margin, "right-margin");
29684 DEFSYM (Qcenter, "center");
29685 DEFSYM (Qline_height, "line-height");
29686 DEFSYM (QCalign_to, ":align-to");
29687 DEFSYM (QCrelative_width, ":relative-width");
29688 DEFSYM (QCrelative_height, ":relative-height");
29689 DEFSYM (QCeval, ":eval");
29690 DEFSYM (QCpropertize, ":propertize");
29691 DEFSYM (QCfile, ":file");
29692 DEFSYM (Qfontified, "fontified");
29693 DEFSYM (Qfontification_functions, "fontification-functions");
29694 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29695 DEFSYM (Qescape_glyph, "escape-glyph");
29696 DEFSYM (Qnobreak_space, "nobreak-space");
29697 DEFSYM (Qimage, "image");
29698 DEFSYM (Qtext, "text");
29699 DEFSYM (Qboth, "both");
29700 DEFSYM (Qboth_horiz, "both-horiz");
29701 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29702 DEFSYM (QCmap, ":map");
29703 DEFSYM (QCpointer, ":pointer");
29704 DEFSYM (Qrect, "rect");
29705 DEFSYM (Qcircle, "circle");
29706 DEFSYM (Qpoly, "poly");
29707 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29708 DEFSYM (Qgrow_only, "grow-only");
29709 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29710 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29711 DEFSYM (Qposition, "position");
29712 DEFSYM (Qbuffer_position, "buffer-position");
29713 DEFSYM (Qobject, "object");
29714 DEFSYM (Qbar, "bar");
29715 DEFSYM (Qhbar, "hbar");
29716 DEFSYM (Qbox, "box");
29717 DEFSYM (Qhollow, "hollow");
29718 DEFSYM (Qhand, "hand");
29719 DEFSYM (Qarrow, "arrow");
29720 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29721
29722 list_of_error = list1 (list2 (intern_c_string ("error"),
29723 intern_c_string ("void-variable")));
29724 staticpro (&list_of_error);
29725
29726 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29727 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29728 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29729 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29730
29731 echo_buffer[0] = echo_buffer[1] = Qnil;
29732 staticpro (&echo_buffer[0]);
29733 staticpro (&echo_buffer[1]);
29734
29735 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29736 staticpro (&echo_area_buffer[0]);
29737 staticpro (&echo_area_buffer[1]);
29738
29739 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29740 staticpro (&Vmessages_buffer_name);
29741
29742 mode_line_proptrans_alist = Qnil;
29743 staticpro (&mode_line_proptrans_alist);
29744 mode_line_string_list = Qnil;
29745 staticpro (&mode_line_string_list);
29746 mode_line_string_face = Qnil;
29747 staticpro (&mode_line_string_face);
29748 mode_line_string_face_prop = Qnil;
29749 staticpro (&mode_line_string_face_prop);
29750 Vmode_line_unwind_vector = Qnil;
29751 staticpro (&Vmode_line_unwind_vector);
29752
29753 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29754
29755 help_echo_string = Qnil;
29756 staticpro (&help_echo_string);
29757 help_echo_object = Qnil;
29758 staticpro (&help_echo_object);
29759 help_echo_window = Qnil;
29760 staticpro (&help_echo_window);
29761 previous_help_echo_string = Qnil;
29762 staticpro (&previous_help_echo_string);
29763 help_echo_pos = -1;
29764
29765 DEFSYM (Qright_to_left, "right-to-left");
29766 DEFSYM (Qleft_to_right, "left-to-right");
29767
29768 #ifdef HAVE_WINDOW_SYSTEM
29769 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29770 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29771 For example, if a block cursor is over a tab, it will be drawn as
29772 wide as that tab on the display. */);
29773 x_stretch_cursor_p = 0;
29774 #endif
29775
29776 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29777 doc: /* Non-nil means highlight trailing whitespace.
29778 The face used for trailing whitespace is `trailing-whitespace'. */);
29779 Vshow_trailing_whitespace = Qnil;
29780
29781 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29782 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29783 If the value is t, Emacs highlights non-ASCII chars which have the
29784 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29785 or `escape-glyph' face respectively.
29786
29787 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29788 U+2011 (non-breaking hyphen) are affected.
29789
29790 Any other non-nil value means to display these characters as a escape
29791 glyph followed by an ordinary space or hyphen.
29792
29793 A value of nil means no special handling of these characters. */);
29794 Vnobreak_char_display = Qt;
29795
29796 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29797 doc: /* The pointer shape to show in void text areas.
29798 A value of nil means to show the text pointer. Other options are `arrow',
29799 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29800 Vvoid_text_area_pointer = Qarrow;
29801
29802 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29803 doc: /* Non-nil means don't actually do any redisplay.
29804 This is used for internal purposes. */);
29805 Vinhibit_redisplay = Qnil;
29806
29807 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29808 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29809 Vglobal_mode_string = Qnil;
29810
29811 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29812 doc: /* Marker for where to display an arrow on top of the buffer text.
29813 This must be the beginning of a line in order to work.
29814 See also `overlay-arrow-string'. */);
29815 Voverlay_arrow_position = Qnil;
29816
29817 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29818 doc: /* String to display as an arrow in non-window frames.
29819 See also `overlay-arrow-position'. */);
29820 Voverlay_arrow_string = build_pure_c_string ("=>");
29821
29822 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29823 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29824 The symbols on this list are examined during redisplay to determine
29825 where to display overlay arrows. */);
29826 Voverlay_arrow_variable_list
29827 = list1 (intern_c_string ("overlay-arrow-position"));
29828
29829 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29830 doc: /* The number of lines to try scrolling a window by when point moves out.
29831 If that fails to bring point back on frame, point is centered instead.
29832 If this is zero, point is always centered after it moves off frame.
29833 If you want scrolling to always be a line at a time, you should set
29834 `scroll-conservatively' to a large value rather than set this to 1. */);
29835
29836 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29837 doc: /* Scroll up to this many lines, to bring point back on screen.
29838 If point moves off-screen, redisplay will scroll by up to
29839 `scroll-conservatively' lines in order to bring point just barely
29840 onto the screen again. If that cannot be done, then redisplay
29841 recenters point as usual.
29842
29843 If the value is greater than 100, redisplay will never recenter point,
29844 but will always scroll just enough text to bring point into view, even
29845 if you move far away.
29846
29847 A value of zero means always recenter point if it moves off screen. */);
29848 scroll_conservatively = 0;
29849
29850 DEFVAR_INT ("scroll-margin", scroll_margin,
29851 doc: /* Number of lines of margin at the top and bottom of a window.
29852 Recenter the window whenever point gets within this many lines
29853 of the top or bottom of the window. */);
29854 scroll_margin = 0;
29855
29856 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29857 doc: /* Pixels per inch value for non-window system displays.
29858 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29859 Vdisplay_pixels_per_inch = make_float (72.0);
29860
29861 #ifdef GLYPH_DEBUG
29862 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29863 #endif
29864
29865 DEFVAR_LISP ("truncate-partial-width-windows",
29866 Vtruncate_partial_width_windows,
29867 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29868 For an integer value, truncate lines in each window narrower than the
29869 full frame width, provided the window width is less than that integer;
29870 otherwise, respect the value of `truncate-lines'.
29871
29872 For any other non-nil value, truncate lines in all windows that do
29873 not span the full frame width.
29874
29875 A value of nil means to respect the value of `truncate-lines'.
29876
29877 If `word-wrap' is enabled, you might want to reduce this. */);
29878 Vtruncate_partial_width_windows = make_number (50);
29879
29880 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29881 doc: /* Maximum buffer size for which line number should be displayed.
29882 If the buffer is bigger than this, the line number does not appear
29883 in the mode line. A value of nil means no limit. */);
29884 Vline_number_display_limit = Qnil;
29885
29886 DEFVAR_INT ("line-number-display-limit-width",
29887 line_number_display_limit_width,
29888 doc: /* Maximum line width (in characters) for line number display.
29889 If the average length of the lines near point is bigger than this, then the
29890 line number may be omitted from the mode line. */);
29891 line_number_display_limit_width = 200;
29892
29893 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29894 doc: /* Non-nil means highlight region even in nonselected windows. */);
29895 highlight_nonselected_windows = 0;
29896
29897 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29898 doc: /* Non-nil if more than one frame is visible on this display.
29899 Minibuffer-only frames don't count, but iconified frames do.
29900 This variable is not guaranteed to be accurate except while processing
29901 `frame-title-format' and `icon-title-format'. */);
29902
29903 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29904 doc: /* Template for displaying the title bar of visible frames.
29905 \(Assuming the window manager supports this feature.)
29906
29907 This variable has the same structure as `mode-line-format', except that
29908 the %c and %l constructs are ignored. It is used only on frames for
29909 which no explicit name has been set \(see `modify-frame-parameters'). */);
29910
29911 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29912 doc: /* Template for displaying the title bar of an iconified frame.
29913 \(Assuming the window manager supports this feature.)
29914 This variable has the same structure as `mode-line-format' (which see),
29915 and is used only on frames for which no explicit name has been set
29916 \(see `modify-frame-parameters'). */);
29917 Vicon_title_format
29918 = Vframe_title_format
29919 = listn (CONSTYPE_PURE, 3,
29920 intern_c_string ("multiple-frames"),
29921 build_pure_c_string ("%b"),
29922 listn (CONSTYPE_PURE, 4,
29923 empty_unibyte_string,
29924 intern_c_string ("invocation-name"),
29925 build_pure_c_string ("@"),
29926 intern_c_string ("system-name")));
29927
29928 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29929 doc: /* Maximum number of lines to keep in the message log buffer.
29930 If nil, disable message logging. If t, log messages but don't truncate
29931 the buffer when it becomes large. */);
29932 Vmessage_log_max = make_number (1000);
29933
29934 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29935 doc: /* Functions called before redisplay, if window sizes have changed.
29936 The value should be a list of functions that take one argument.
29937 Just before redisplay, for each frame, if any of its windows have changed
29938 size since the last redisplay, or have been split or deleted,
29939 all the functions in the list are called, with the frame as argument. */);
29940 Vwindow_size_change_functions = Qnil;
29941
29942 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29943 doc: /* List of functions to call before redisplaying a window with scrolling.
29944 Each function is called with two arguments, the window and its new
29945 display-start position. Note that these functions are also called by
29946 `set-window-buffer'. Also note that the value of `window-end' is not
29947 valid when these functions are called.
29948
29949 Warning: Do not use this feature to alter the way the window
29950 is scrolled. It is not designed for that, and such use probably won't
29951 work. */);
29952 Vwindow_scroll_functions = Qnil;
29953
29954 DEFVAR_LISP ("window-text-change-functions",
29955 Vwindow_text_change_functions,
29956 doc: /* Functions to call in redisplay when text in the window might change. */);
29957 Vwindow_text_change_functions = Qnil;
29958
29959 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29960 doc: /* Functions called when redisplay of a window reaches the end trigger.
29961 Each function is called with two arguments, the window and the end trigger value.
29962 See `set-window-redisplay-end-trigger'. */);
29963 Vredisplay_end_trigger_functions = Qnil;
29964
29965 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29966 doc: /* Non-nil means autoselect window with mouse pointer.
29967 If nil, do not autoselect windows.
29968 A positive number means delay autoselection by that many seconds: a
29969 window is autoselected only after the mouse has remained in that
29970 window for the duration of the delay.
29971 A negative number has a similar effect, but causes windows to be
29972 autoselected only after the mouse has stopped moving. \(Because of
29973 the way Emacs compares mouse events, you will occasionally wait twice
29974 that time before the window gets selected.\)
29975 Any other value means to autoselect window instantaneously when the
29976 mouse pointer enters it.
29977
29978 Autoselection selects the minibuffer only if it is active, and never
29979 unselects the minibuffer if it is active.
29980
29981 When customizing this variable make sure that the actual value of
29982 `focus-follows-mouse' matches the behavior of your window manager. */);
29983 Vmouse_autoselect_window = Qnil;
29984
29985 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29986 doc: /* Non-nil means automatically resize tool-bars.
29987 This dynamically changes the tool-bar's height to the minimum height
29988 that is needed to make all tool-bar items visible.
29989 If value is `grow-only', the tool-bar's height is only increased
29990 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29991 Vauto_resize_tool_bars = Qt;
29992
29993 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29994 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29995 auto_raise_tool_bar_buttons_p = 1;
29996
29997 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29998 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29999 make_cursor_line_fully_visible_p = 1;
30000
30001 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30002 doc: /* Border below tool-bar in pixels.
30003 If an integer, use it as the height of the border.
30004 If it is one of `internal-border-width' or `border-width', use the
30005 value of the corresponding frame parameter.
30006 Otherwise, no border is added below the tool-bar. */);
30007 Vtool_bar_border = Qinternal_border_width;
30008
30009 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30010 doc: /* Margin around tool-bar buttons in pixels.
30011 If an integer, use that for both horizontal and vertical margins.
30012 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30013 HORZ specifying the horizontal margin, and VERT specifying the
30014 vertical margin. */);
30015 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30016
30017 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30018 doc: /* Relief thickness of tool-bar buttons. */);
30019 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30020
30021 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30022 doc: /* Tool bar style to use.
30023 It can be one of
30024 image - show images only
30025 text - show text only
30026 both - show both, text below image
30027 both-horiz - show text to the right of the image
30028 text-image-horiz - show text to the left of the image
30029 any other - use system default or image if no system default.
30030
30031 This variable only affects the GTK+ toolkit version of Emacs. */);
30032 Vtool_bar_style = Qnil;
30033
30034 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30035 doc: /* Maximum number of characters a label can have to be shown.
30036 The tool bar style must also show labels for this to have any effect, see
30037 `tool-bar-style'. */);
30038 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30039
30040 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30041 doc: /* List of functions to call to fontify regions of text.
30042 Each function is called with one argument POS. Functions must
30043 fontify a region starting at POS in the current buffer, and give
30044 fontified regions the property `fontified'. */);
30045 Vfontification_functions = Qnil;
30046 Fmake_variable_buffer_local (Qfontification_functions);
30047
30048 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30049 unibyte_display_via_language_environment,
30050 doc: /* Non-nil means display unibyte text according to language environment.
30051 Specifically, this means that raw bytes in the range 160-255 decimal
30052 are displayed by converting them to the equivalent multibyte characters
30053 according to the current language environment. As a result, they are
30054 displayed according to the current fontset.
30055
30056 Note that this variable affects only how these bytes are displayed,
30057 but does not change the fact they are interpreted as raw bytes. */);
30058 unibyte_display_via_language_environment = 0;
30059
30060 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30061 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30062 If a float, it specifies a fraction of the mini-window frame's height.
30063 If an integer, it specifies a number of lines. */);
30064 Vmax_mini_window_height = make_float (0.25);
30065
30066 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30067 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30068 A value of nil means don't automatically resize mini-windows.
30069 A value of t means resize them to fit the text displayed in them.
30070 A value of `grow-only', the default, means let mini-windows grow only;
30071 they return to their normal size when the minibuffer is closed, or the
30072 echo area becomes empty. */);
30073 Vresize_mini_windows = Qgrow_only;
30074
30075 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30076 doc: /* Alist specifying how to blink the cursor off.
30077 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30078 `cursor-type' frame-parameter or variable equals ON-STATE,
30079 comparing using `equal', Emacs uses OFF-STATE to specify
30080 how to blink it off. ON-STATE and OFF-STATE are values for
30081 the `cursor-type' frame parameter.
30082
30083 If a frame's ON-STATE has no entry in this list,
30084 the frame's other specifications determine how to blink the cursor off. */);
30085 Vblink_cursor_alist = Qnil;
30086
30087 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30088 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30089 If non-nil, windows are automatically scrolled horizontally to make
30090 point visible. */);
30091 automatic_hscrolling_p = 1;
30092 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30093
30094 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30095 doc: /* How many columns away from the window edge point is allowed to get
30096 before automatic hscrolling will horizontally scroll the window. */);
30097 hscroll_margin = 5;
30098
30099 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30100 doc: /* How many columns to scroll the window when point gets too close to the edge.
30101 When point is less than `hscroll-margin' columns from the window
30102 edge, automatic hscrolling will scroll the window by the amount of columns
30103 determined by this variable. If its value is a positive integer, scroll that
30104 many columns. If it's a positive floating-point number, it specifies the
30105 fraction of the window's width to scroll. If it's nil or zero, point will be
30106 centered horizontally after the scroll. Any other value, including negative
30107 numbers, are treated as if the value were zero.
30108
30109 Automatic hscrolling always moves point outside the scroll margin, so if
30110 point was more than scroll step columns inside the margin, the window will
30111 scroll more than the value given by the scroll step.
30112
30113 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30114 and `scroll-right' overrides this variable's effect. */);
30115 Vhscroll_step = make_number (0);
30116
30117 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30118 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30119 Bind this around calls to `message' to let it take effect. */);
30120 message_truncate_lines = 0;
30121
30122 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30123 doc: /* Normal hook run to update the menu bar definitions.
30124 Redisplay runs this hook before it redisplays the menu bar.
30125 This is used to update submenus such as Buffers,
30126 whose contents depend on various data. */);
30127 Vmenu_bar_update_hook = Qnil;
30128
30129 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30130 doc: /* Frame for which we are updating a menu.
30131 The enable predicate for a menu binding should check this variable. */);
30132 Vmenu_updating_frame = Qnil;
30133
30134 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30135 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30136 inhibit_menubar_update = 0;
30137
30138 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30139 doc: /* Prefix prepended to all continuation lines at display time.
30140 The value may be a string, an image, or a stretch-glyph; it is
30141 interpreted in the same way as the value of a `display' text property.
30142
30143 This variable is overridden by any `wrap-prefix' text or overlay
30144 property.
30145
30146 To add a prefix to non-continuation lines, use `line-prefix'. */);
30147 Vwrap_prefix = Qnil;
30148 DEFSYM (Qwrap_prefix, "wrap-prefix");
30149 Fmake_variable_buffer_local (Qwrap_prefix);
30150
30151 DEFVAR_LISP ("line-prefix", Vline_prefix,
30152 doc: /* Prefix prepended to all non-continuation lines at display time.
30153 The value may be a string, an image, or a stretch-glyph; it is
30154 interpreted in the same way as the value of a `display' text property.
30155
30156 This variable is overridden by any `line-prefix' text or overlay
30157 property.
30158
30159 To add a prefix to continuation lines, use `wrap-prefix'. */);
30160 Vline_prefix = Qnil;
30161 DEFSYM (Qline_prefix, "line-prefix");
30162 Fmake_variable_buffer_local (Qline_prefix);
30163
30164 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30165 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30166 inhibit_eval_during_redisplay = 0;
30167
30168 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30169 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30170 inhibit_free_realized_faces = 0;
30171
30172 #ifdef GLYPH_DEBUG
30173 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30174 doc: /* Inhibit try_window_id display optimization. */);
30175 inhibit_try_window_id = 0;
30176
30177 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30178 doc: /* Inhibit try_window_reusing display optimization. */);
30179 inhibit_try_window_reusing = 0;
30180
30181 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30182 doc: /* Inhibit try_cursor_movement display optimization. */);
30183 inhibit_try_cursor_movement = 0;
30184 #endif /* GLYPH_DEBUG */
30185
30186 DEFVAR_INT ("overline-margin", overline_margin,
30187 doc: /* Space between overline and text, in pixels.
30188 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30189 margin to the character height. */);
30190 overline_margin = 2;
30191
30192 DEFVAR_INT ("underline-minimum-offset",
30193 underline_minimum_offset,
30194 doc: /* Minimum distance between baseline and underline.
30195 This can improve legibility of underlined text at small font sizes,
30196 particularly when using variable `x-use-underline-position-properties'
30197 with fonts that specify an UNDERLINE_POSITION relatively close to the
30198 baseline. The default value is 1. */);
30199 underline_minimum_offset = 1;
30200
30201 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30202 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30203 This feature only works when on a window system that can change
30204 cursor shapes. */);
30205 display_hourglass_p = 1;
30206
30207 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30208 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30209 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30210
30211 #ifdef HAVE_WINDOW_SYSTEM
30212 hourglass_atimer = NULL;
30213 hourglass_shown_p = 0;
30214 #endif /* HAVE_WINDOW_SYSTEM */
30215
30216 DEFSYM (Qglyphless_char, "glyphless-char");
30217 DEFSYM (Qhex_code, "hex-code");
30218 DEFSYM (Qempty_box, "empty-box");
30219 DEFSYM (Qthin_space, "thin-space");
30220 DEFSYM (Qzero_width, "zero-width");
30221
30222 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30223 doc: /* Function run just before redisplay.
30224 It is called with one argument, which is the set of windows that are to
30225 be redisplayed. This set can be nil (meaning, only the selected window),
30226 or t (meaning all windows). */);
30227 Vpre_redisplay_function = intern ("ignore");
30228
30229 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30230 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30231
30232 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30233 doc: /* Char-table defining glyphless characters.
30234 Each element, if non-nil, should be one of the following:
30235 an ASCII acronym string: display this string in a box
30236 `hex-code': display the hexadecimal code of a character in a box
30237 `empty-box': display as an empty box
30238 `thin-space': display as 1-pixel width space
30239 `zero-width': don't display
30240 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30241 display method for graphical terminals and text terminals respectively.
30242 GRAPHICAL and TEXT should each have one of the values listed above.
30243
30244 The char-table has one extra slot to control the display of a character for
30245 which no font is found. This slot only takes effect on graphical terminals.
30246 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30247 `thin-space'. The default is `empty-box'.
30248
30249 If a character has a non-nil entry in an active display table, the
30250 display table takes effect; in this case, Emacs does not consult
30251 `glyphless-char-display' at all. */);
30252 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30253 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30254 Qempty_box);
30255
30256 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30257 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30258 Vdebug_on_message = Qnil;
30259
30260 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30261 doc: /* */);
30262 Vredisplay__all_windows_cause
30263 = Fmake_vector (make_number (100), make_number (0));
30264
30265 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30266 doc: /* */);
30267 Vredisplay__mode_lines_cause
30268 = Fmake_vector (make_number (100), make_number (0));
30269 }
30270
30271
30272 /* Initialize this module when Emacs starts. */
30273
30274 void
30275 init_xdisp (void)
30276 {
30277 CHARPOS (this_line_start_pos) = 0;
30278
30279 if (!noninteractive)
30280 {
30281 struct window *m = XWINDOW (minibuf_window);
30282 Lisp_Object frame = m->frame;
30283 struct frame *f = XFRAME (frame);
30284 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30285 struct window *r = XWINDOW (root);
30286 int i;
30287
30288 echo_area_window = minibuf_window;
30289
30290 r->top_line = FRAME_TOP_MARGIN (f);
30291 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30292 r->total_cols = FRAME_COLS (f);
30293 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30294 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30295 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30296
30297 m->top_line = FRAME_LINES (f) - 1;
30298 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30299 m->total_cols = FRAME_COLS (f);
30300 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30301 m->total_lines = 1;
30302 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30303
30304 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30305 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30306 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30307
30308 /* The default ellipsis glyphs `...'. */
30309 for (i = 0; i < 3; ++i)
30310 default_invis_vector[i] = make_number ('.');
30311 }
30312
30313 {
30314 /* Allocate the buffer for frame titles.
30315 Also used for `format-mode-line'. */
30316 int size = 100;
30317 mode_line_noprop_buf = xmalloc (size);
30318 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30319 mode_line_noprop_ptr = mode_line_noprop_buf;
30320 mode_line_target = MODE_LINE_DISPLAY;
30321 }
30322
30323 help_echo_showing_p = 0;
30324 }
30325
30326 #ifdef HAVE_WINDOW_SYSTEM
30327
30328 /* Platform-independent portion of hourglass implementation. */
30329
30330 /* Cancel a currently active hourglass timer, and start a new one. */
30331 void
30332 start_hourglass (void)
30333 {
30334 struct timespec delay;
30335
30336 cancel_hourglass ();
30337
30338 if (INTEGERP (Vhourglass_delay)
30339 && XINT (Vhourglass_delay) > 0)
30340 delay = make_timespec (min (XINT (Vhourglass_delay),
30341 TYPE_MAXIMUM (time_t)),
30342 0);
30343 else if (FLOATP (Vhourglass_delay)
30344 && XFLOAT_DATA (Vhourglass_delay) > 0)
30345 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30346 else
30347 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30348
30349 #ifdef HAVE_NTGUI
30350 {
30351 extern void w32_note_current_window (void);
30352 w32_note_current_window ();
30353 }
30354 #endif /* HAVE_NTGUI */
30355
30356 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30357 show_hourglass, NULL);
30358 }
30359
30360
30361 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30362 shown. */
30363 void
30364 cancel_hourglass (void)
30365 {
30366 if (hourglass_atimer)
30367 {
30368 cancel_atimer (hourglass_atimer);
30369 hourglass_atimer = NULL;
30370 }
30371
30372 if (hourglass_shown_p)
30373 hide_hourglass ();
30374 }
30375
30376 #endif /* HAVE_WINDOW_SYSTEM */