]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Fix handling of internal borders (Bug#16348).
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2014 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
305
306 #ifndef FRAME_X_OUTPUT
307 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
308 #endif
309
310 #define INFINITY 10000000
311
312 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
313 Lisp_Object Qwindow_scroll_functions;
314 static Lisp_Object Qwindow_text_change_functions;
315 static Lisp_Object Qredisplay_end_trigger_functions;
316 Lisp_Object Qinhibit_point_motion_hooks;
317 static Lisp_Object QCeval, QCpropertize;
318 Lisp_Object QCfile, QCdata;
319 static Lisp_Object Qfontified;
320 static Lisp_Object Qgrow_only;
321 static Lisp_Object Qinhibit_eval_during_redisplay;
322 static Lisp_Object Qbuffer_position, Qposition, Qobject;
323 static Lisp_Object Qright_to_left, Qleft_to_right;
324
325 /* Cursor shapes. */
326 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
327
328 /* Pointer shapes. */
329 static Lisp_Object Qarrow, Qhand;
330 Lisp_Object Qtext;
331
332 /* Holds the list (error). */
333 static Lisp_Object list_of_error;
334
335 static Lisp_Object Qfontification_functions;
336
337 static Lisp_Object Qwrap_prefix;
338 static Lisp_Object Qline_prefix;
339 static Lisp_Object Qredisplay_internal;
340
341 /* Non-nil means don't actually do any redisplay. */
342
343 Lisp_Object Qinhibit_redisplay;
344
345 /* Names of text properties relevant for redisplay. */
346
347 Lisp_Object Qdisplay;
348
349 Lisp_Object Qspace, QCalign_to;
350 static Lisp_Object QCrelative_width, QCrelative_height;
351 Lisp_Object Qleft_margin, Qright_margin;
352 static Lisp_Object Qspace_width, Qraise;
353 static Lisp_Object Qslice;
354 Lisp_Object Qcenter;
355 static Lisp_Object Qmargin, Qpointer;
356 static Lisp_Object Qline_height;
357
358 #ifdef HAVE_WINDOW_SYSTEM
359
360 /* Test if overflow newline into fringe. Called with iterator IT
361 at or past right window margin, and with IT->current_x set. */
362
363 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
364 (!NILP (Voverflow_newline_into_fringe) \
365 && FRAME_WINDOW_P ((IT)->f) \
366 && ((IT)->bidi_it.paragraph_dir == R2L \
367 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
368 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
369 && (IT)->current_x == (IT)->last_visible_x)
370
371 #else /* !HAVE_WINDOW_SYSTEM */
372 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
373 #endif /* HAVE_WINDOW_SYSTEM */
374
375 /* Test if the display element loaded in IT, or the underlying buffer
376 or string character, is a space or a TAB character. This is used
377 to determine where word wrapping can occur. */
378
379 #define IT_DISPLAYING_WHITESPACE(it) \
380 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
381 || ((STRINGP (it->string) \
382 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
383 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
384 || (it->s \
385 && (it->s[IT_BYTEPOS (*it)] == ' ' \
386 || it->s[IT_BYTEPOS (*it)] == '\t')) \
387 || (IT_BYTEPOS (*it) < ZV_BYTE \
388 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
389 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
390
391 /* Name of the face used to highlight trailing whitespace. */
392
393 static Lisp_Object Qtrailing_whitespace;
394
395 /* Name and number of the face used to highlight escape glyphs. */
396
397 static Lisp_Object Qescape_glyph;
398
399 /* Name and number of the face used to highlight non-breaking spaces. */
400
401 static Lisp_Object Qnobreak_space;
402
403 /* The symbol `image' which is the car of the lists used to represent
404 images in Lisp. Also a tool bar style. */
405
406 Lisp_Object Qimage;
407
408 /* The image map types. */
409 Lisp_Object QCmap;
410 static Lisp_Object QCpointer;
411 static Lisp_Object Qrect, Qcircle, Qpoly;
412
413 /* Tool bar styles */
414 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
415
416 /* Non-zero means print newline to stdout before next mini-buffer
417 message. */
418
419 bool noninteractive_need_newline;
420
421 /* Non-zero means print newline to message log before next message. */
422
423 static bool message_log_need_newline;
424
425 /* Three markers that message_dolog uses.
426 It could allocate them itself, but that causes trouble
427 in handling memory-full errors. */
428 static Lisp_Object message_dolog_marker1;
429 static Lisp_Object message_dolog_marker2;
430 static Lisp_Object message_dolog_marker3;
431 \f
432 /* The buffer position of the first character appearing entirely or
433 partially on the line of the selected window which contains the
434 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
435 redisplay optimization in redisplay_internal. */
436
437 static struct text_pos this_line_start_pos;
438
439 /* Number of characters past the end of the line above, including the
440 terminating newline. */
441
442 static struct text_pos this_line_end_pos;
443
444 /* The vertical positions and the height of this line. */
445
446 static int this_line_vpos;
447 static int this_line_y;
448 static int this_line_pixel_height;
449
450 /* X position at which this display line starts. Usually zero;
451 negative if first character is partially visible. */
452
453 static int this_line_start_x;
454
455 /* The smallest character position seen by move_it_* functions as they
456 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
457 hscrolled lines, see display_line. */
458
459 static struct text_pos this_line_min_pos;
460
461 /* Buffer that this_line_.* variables are referring to. */
462
463 static struct buffer *this_line_buffer;
464
465
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
470
471 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
475
476 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477
478 Lisp_Object Qmenu_bar_update_hook;
479
480 /* Nonzero if an overlay arrow has been displayed in this window. */
481
482 static bool overlay_arrow_seen;
483
484 /* Vector containing glyphs for an ellipsis `...'. */
485
486 static Lisp_Object default_invis_vector[3];
487
488 /* This is the window where the echo area message was displayed. It
489 is always a mini-buffer window, but it may not be the same window
490 currently active as a mini-buffer. */
491
492 Lisp_Object echo_area_window;
493
494 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
495 pushes the current message and the value of
496 message_enable_multibyte on the stack, the function restore_message
497 pops the stack and displays MESSAGE again. */
498
499 static Lisp_Object Vmessage_stack;
500
501 /* Nonzero means multibyte characters were enabled when the echo area
502 message was specified. */
503
504 static bool message_enable_multibyte;
505
506 /* Nonzero if we should redraw the mode lines on the next redisplay.
507 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
508 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
509 (the number used is then only used to track down the cause for this
510 full-redisplay). */
511
512 int update_mode_lines;
513
514 /* Nonzero if window sizes or contents other than selected-window have changed
515 since last redisplay that finished.
516 If it has value REDISPLAY_SOME, then only redisplay the windows where
517 the `redisplay' bit has been set. Otherwise, redisplay all windows
518 (the number used is then only used to track down the cause for this
519 full-redisplay). */
520
521 int windows_or_buffers_changed;
522
523 /* Nonzero after display_mode_line if %l was used and it displayed a
524 line number. */
525
526 static bool line_number_displayed;
527
528 /* The name of the *Messages* buffer, a string. */
529
530 static Lisp_Object Vmessages_buffer_name;
531
532 /* Current, index 0, and last displayed echo area message. Either
533 buffers from echo_buffers, or nil to indicate no message. */
534
535 Lisp_Object echo_area_buffer[2];
536
537 /* The buffers referenced from echo_area_buffer. */
538
539 static Lisp_Object echo_buffer[2];
540
541 /* A vector saved used in with_area_buffer to reduce consing. */
542
543 static Lisp_Object Vwith_echo_area_save_vector;
544
545 /* Non-zero means display_echo_area should display the last echo area
546 message again. Set by redisplay_preserve_echo_area. */
547
548 static bool display_last_displayed_message_p;
549
550 /* Nonzero if echo area is being used by print; zero if being used by
551 message. */
552
553 static bool message_buf_print;
554
555 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
556
557 static Lisp_Object Qinhibit_menubar_update;
558 static Lisp_Object Qmessage_truncate_lines;
559
560 /* Set to 1 in clear_message to make redisplay_internal aware
561 of an emptied echo area. */
562
563 static bool message_cleared_p;
564
565 /* A scratch glyph row with contents used for generating truncation
566 glyphs. Also used in direct_output_for_insert. */
567
568 #define MAX_SCRATCH_GLYPHS 100
569 static struct glyph_row scratch_glyph_row;
570 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
571
572 /* Ascent and height of the last line processed by move_it_to. */
573
574 static int last_max_ascent, last_height;
575
576 /* Non-zero if there's a help-echo in the echo area. */
577
578 bool help_echo_showing_p;
579
580 /* The maximum distance to look ahead for text properties. Values
581 that are too small let us call compute_char_face and similar
582 functions too often which is expensive. Values that are too large
583 let us call compute_char_face and alike too often because we
584 might not be interested in text properties that far away. */
585
586 #define TEXT_PROP_DISTANCE_LIMIT 100
587
588 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
589 iterator state and later restore it. This is needed because the
590 bidi iterator on bidi.c keeps a stacked cache of its states, which
591 is really a singleton. When we use scratch iterator objects to
592 move around the buffer, we can cause the bidi cache to be pushed or
593 popped, and therefore we need to restore the cache state when we
594 return to the original iterator. */
595 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
596 do { \
597 if (CACHE) \
598 bidi_unshelve_cache (CACHE, 1); \
599 ITCOPY = ITORIG; \
600 CACHE = bidi_shelve_cache (); \
601 } while (0)
602
603 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
604 do { \
605 if (pITORIG != pITCOPY) \
606 *(pITORIG) = *(pITCOPY); \
607 bidi_unshelve_cache (CACHE, 0); \
608 CACHE = NULL; \
609 } while (0)
610
611 /* Functions to mark elements as needing redisplay. */
612 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
613
614 void
615 redisplay_other_windows (void)
616 {
617 if (!windows_or_buffers_changed)
618 windows_or_buffers_changed = REDISPLAY_SOME;
619 }
620
621 void
622 wset_redisplay (struct window *w)
623 {
624 /* Beware: selected_window can be nil during early stages. */
625 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
626 redisplay_other_windows ();
627 w->redisplay = true;
628 }
629
630 void
631 fset_redisplay (struct frame *f)
632 {
633 redisplay_other_windows ();
634 f->redisplay = true;
635 }
636
637 void
638 bset_redisplay (struct buffer *b)
639 {
640 int count = buffer_window_count (b);
641 if (count > 0)
642 {
643 /* ... it's visible in other window than selected, */
644 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
645 redisplay_other_windows ();
646 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
647 so that if we later set windows_or_buffers_changed, this buffer will
648 not be omitted. */
649 b->text->redisplay = true;
650 }
651 }
652
653 void
654 bset_update_mode_line (struct buffer *b)
655 {
656 if (!update_mode_lines)
657 update_mode_lines = REDISPLAY_SOME;
658 b->text->redisplay = true;
659 }
660
661 #ifdef GLYPH_DEBUG
662
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG defined. */
665
666 bool trace_redisplay_p;
667
668 #endif /* GLYPH_DEBUG */
669
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
673
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
678
679 static Lisp_Object Qauto_hscroll_mode;
680
681 /* Buffer being redisplayed -- for redisplay_window_error. */
682
683 static struct buffer *displayed_buffer;
684
685 /* Value returned from text property handlers (see below). */
686
687 enum prop_handled
688 {
689 HANDLED_NORMALLY,
690 HANDLED_RECOMPUTE_PROPS,
691 HANDLED_OVERLAY_STRING_CONSUMED,
692 HANDLED_RETURN
693 };
694
695 /* A description of text properties that redisplay is interested
696 in. */
697
698 struct props
699 {
700 /* The name of the property. */
701 Lisp_Object *name;
702
703 /* A unique index for the property. */
704 enum prop_idx idx;
705
706 /* A handler function called to set up iterator IT from the property
707 at IT's current position. Value is used to steer handle_stop. */
708 enum prop_handled (*handler) (struct it *it);
709 };
710
711 static enum prop_handled handle_face_prop (struct it *);
712 static enum prop_handled handle_invisible_prop (struct it *);
713 static enum prop_handled handle_display_prop (struct it *);
714 static enum prop_handled handle_composition_prop (struct it *);
715 static enum prop_handled handle_overlay_change (struct it *);
716 static enum prop_handled handle_fontified_prop (struct it *);
717
718 /* Properties handled by iterators. */
719
720 static struct props it_props[] =
721 {
722 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
723 /* Handle `face' before `display' because some sub-properties of
724 `display' need to know the face. */
725 {&Qface, FACE_PROP_IDX, handle_face_prop},
726 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
727 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
728 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
729 {NULL, 0, NULL}
730 };
731
732 /* Value is the position described by X. If X is a marker, value is
733 the marker_position of X. Otherwise, value is X. */
734
735 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
736
737 /* Enumeration returned by some move_it_.* functions internally. */
738
739 enum move_it_result
740 {
741 /* Not used. Undefined value. */
742 MOVE_UNDEFINED,
743
744 /* Move ended at the requested buffer position or ZV. */
745 MOVE_POS_MATCH_OR_ZV,
746
747 /* Move ended at the requested X pixel position. */
748 MOVE_X_REACHED,
749
750 /* Move within a line ended at the end of a line that must be
751 continued. */
752 MOVE_LINE_CONTINUED,
753
754 /* Move within a line ended at the end of a line that would
755 be displayed truncated. */
756 MOVE_LINE_TRUNCATED,
757
758 /* Move within a line ended at a line end. */
759 MOVE_NEWLINE_OR_CR
760 };
761
762 /* This counter is used to clear the face cache every once in a while
763 in redisplay_internal. It is incremented for each redisplay.
764 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
765 cleared. */
766
767 #define CLEAR_FACE_CACHE_COUNT 500
768 static int clear_face_cache_count;
769
770 /* Similarly for the image cache. */
771
772 #ifdef HAVE_WINDOW_SYSTEM
773 #define CLEAR_IMAGE_CACHE_COUNT 101
774 static int clear_image_cache_count;
775
776 /* Null glyph slice */
777 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
778 #endif
779
780 /* True while redisplay_internal is in progress. */
781
782 bool redisplaying_p;
783
784 static Lisp_Object Qinhibit_free_realized_faces;
785 static Lisp_Object Qmode_line_default_help_echo;
786
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
789
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 ptrdiff_t help_echo_pos;
794
795 /* Temporary variable for XTread_socket. */
796
797 Lisp_Object previous_help_echo_string;
798
799 /* Platform-independent portion of hourglass implementation. */
800
801 #ifdef HAVE_WINDOW_SYSTEM
802
803 /* Non-zero means an hourglass cursor is currently shown. */
804 bool hourglass_shown_p;
805
806 /* If non-null, an asynchronous timer that, when it expires, displays
807 an hourglass cursor on all frames. */
808 struct atimer *hourglass_atimer;
809
810 #endif /* HAVE_WINDOW_SYSTEM */
811
812 /* Name of the face used to display glyphless characters. */
813 static Lisp_Object Qglyphless_char;
814
815 /* Symbol for the purpose of Vglyphless_char_display. */
816 static Lisp_Object Qglyphless_char_display;
817
818 /* Method symbols for Vglyphless_char_display. */
819 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
820
821 /* Default number of seconds to wait before displaying an hourglass
822 cursor. */
823 #define DEFAULT_HOURGLASS_DELAY 1
824
825 #ifdef HAVE_WINDOW_SYSTEM
826
827 /* Default pixel width of `thin-space' display method. */
828 #define THIN_SPACE_WIDTH 1
829
830 #endif /* HAVE_WINDOW_SYSTEM */
831
832 /* Function prototypes. */
833
834 static void setup_for_ellipsis (struct it *, int);
835 static void set_iterator_to_next (struct it *, int);
836 static void mark_window_display_accurate_1 (struct window *, int);
837 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
838 static int display_prop_string_p (Lisp_Object, Lisp_Object);
839 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
840 static int cursor_row_p (struct glyph_row *);
841 static int redisplay_mode_lines (Lisp_Object, bool);
842 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
843
844 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
845
846 static void handle_line_prefix (struct it *);
847
848 static void pint2str (char *, int, ptrdiff_t);
849 static void pint2hrstr (char *, int, ptrdiff_t);
850 static struct text_pos run_window_scroll_functions (Lisp_Object,
851 struct text_pos);
852 static int text_outside_line_unchanged_p (struct window *,
853 ptrdiff_t, ptrdiff_t);
854 static void store_mode_line_noprop_char (char);
855 static int store_mode_line_noprop (const char *, int, int);
856 static void handle_stop (struct it *);
857 static void handle_stop_backwards (struct it *, ptrdiff_t);
858 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
859 static void ensure_echo_area_buffers (void);
860 static void unwind_with_echo_area_buffer (Lisp_Object);
861 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
862 static int with_echo_area_buffer (struct window *, int,
863 int (*) (ptrdiff_t, Lisp_Object),
864 ptrdiff_t, Lisp_Object);
865 static void clear_garbaged_frames (void);
866 static int current_message_1 (ptrdiff_t, Lisp_Object);
867 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
868 static void set_message (Lisp_Object);
869 static int set_message_1 (ptrdiff_t, Lisp_Object);
870 static int display_echo_area (struct window *);
871 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
872 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
873 static void unwind_redisplay (void);
874 static int string_char_and_length (const unsigned char *, int *);
875 static struct text_pos display_prop_end (struct it *, Lisp_Object,
876 struct text_pos);
877 static int compute_window_start_on_continuation_line (struct window *);
878 static void insert_left_trunc_glyphs (struct it *);
879 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
880 Lisp_Object);
881 static void extend_face_to_end_of_line (struct it *);
882 static int append_space_for_newline (struct it *, int);
883 static int cursor_row_fully_visible_p (struct window *, int, int);
884 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
885 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
886 static int trailing_whitespace_p (ptrdiff_t);
887 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
888 static void push_it (struct it *, struct text_pos *);
889 static void iterate_out_of_display_property (struct it *);
890 static void pop_it (struct it *);
891 static void sync_frame_with_window_matrix_rows (struct window *);
892 static void redisplay_internal (void);
893 static int echo_area_display (int);
894 static void redisplay_windows (Lisp_Object);
895 static void redisplay_window (Lisp_Object, bool);
896 static Lisp_Object redisplay_window_error (Lisp_Object);
897 static Lisp_Object redisplay_window_0 (Lisp_Object);
898 static Lisp_Object redisplay_window_1 (Lisp_Object);
899 static int set_cursor_from_row (struct window *, struct glyph_row *,
900 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
901 int, int);
902 static int update_menu_bar (struct frame *, int, int);
903 static int try_window_reusing_current_matrix (struct window *);
904 static int try_window_id (struct window *);
905 static int display_line (struct it *);
906 static int display_mode_lines (struct window *);
907 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
908 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
909 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
910 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
911 static void display_menu_bar (struct window *);
912 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
913 ptrdiff_t *);
914 static int display_string (const char *, Lisp_Object, Lisp_Object,
915 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
916 static void compute_line_metrics (struct it *);
917 static void run_redisplay_end_trigger_hook (struct it *);
918 static int get_overlay_strings (struct it *, ptrdiff_t);
919 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
920 static void next_overlay_string (struct it *);
921 static void reseat (struct it *, struct text_pos, int);
922 static void reseat_1 (struct it *, struct text_pos, int);
923 static void back_to_previous_visible_line_start (struct it *);
924 static void reseat_at_next_visible_line_start (struct it *, int);
925 static int next_element_from_ellipsis (struct it *);
926 static int next_element_from_display_vector (struct it *);
927 static int next_element_from_string (struct it *);
928 static int next_element_from_c_string (struct it *);
929 static int next_element_from_buffer (struct it *);
930 static int next_element_from_composition (struct it *);
931 static int next_element_from_image (struct it *);
932 static int next_element_from_stretch (struct it *);
933 static void load_overlay_strings (struct it *, ptrdiff_t);
934 static int init_from_display_pos (struct it *, struct window *,
935 struct display_pos *);
936 static void reseat_to_string (struct it *, const char *,
937 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
938 static int get_next_display_element (struct it *);
939 static enum move_it_result
940 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
941 enum move_operation_enum);
942 static void get_visually_first_element (struct it *);
943 static void init_to_row_start (struct it *, struct window *,
944 struct glyph_row *);
945 static int init_to_row_end (struct it *, struct window *,
946 struct glyph_row *);
947 static void back_to_previous_line_start (struct it *);
948 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
949 static struct text_pos string_pos_nchars_ahead (struct text_pos,
950 Lisp_Object, ptrdiff_t);
951 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
952 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
953 static ptrdiff_t number_of_chars (const char *, bool);
954 static void compute_stop_pos (struct it *);
955 static void compute_string_pos (struct text_pos *, struct text_pos,
956 Lisp_Object);
957 static int face_before_or_after_it_pos (struct it *, int);
958 static ptrdiff_t next_overlay_change (ptrdiff_t);
959 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
960 Lisp_Object, struct text_pos *, ptrdiff_t, int);
961 static int handle_single_display_spec (struct it *, Lisp_Object,
962 Lisp_Object, Lisp_Object,
963 struct text_pos *, ptrdiff_t, int, int);
964 static int underlying_face_id (struct it *);
965 static int in_ellipses_for_invisible_text_p (struct display_pos *,
966 struct window *);
967
968 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
969 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
970
971 #ifdef HAVE_WINDOW_SYSTEM
972
973 static void x_consider_frame_title (Lisp_Object);
974 static void update_tool_bar (struct frame *, int);
975 static int redisplay_tool_bar (struct frame *);
976 static void x_draw_bottom_divider (struct window *w);
977 static void notice_overwritten_cursor (struct window *,
978 enum glyph_row_area,
979 int, int, int, int);
980 static void append_stretch_glyph (struct it *, Lisp_Object,
981 int, int, int);
982
983
984 #endif /* HAVE_WINDOW_SYSTEM */
985
986 static void produce_special_glyphs (struct it *, enum display_element_type);
987 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
988 static bool coords_in_mouse_face_p (struct window *, int, int);
989
990
991 \f
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
995
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
999
1000 This is the height of W minus the height of a mode line, if any. */
1001
1002 int
1003 window_text_bottom_y (struct window *w)
1004 {
1005 int height = WINDOW_PIXEL_HEIGHT (w);
1006
1007 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1008
1009 if (WINDOW_WANTS_MODELINE_P (w))
1010 height -= CURRENT_MODE_LINE_HEIGHT (w);
1011
1012 return height;
1013 }
1014
1015 /* Return the pixel width of display area AREA of window W.
1016 ANY_AREA means return the total width of W, not including
1017 fringes to the left and right of the window. */
1018
1019 int
1020 window_box_width (struct window *w, enum glyph_row_area area)
1021 {
1022 int pixels = w->pixel_width;
1023
1024 if (!w->pseudo_window_p)
1025 {
1026 pixels -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1027 pixels -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1028
1029 if (area == TEXT_AREA)
1030 pixels -= (WINDOW_MARGINS_WIDTH (w)
1031 + WINDOW_FRINGES_WIDTH (w));
1032 else if (area == LEFT_MARGIN_AREA)
1033 pixels = WINDOW_LEFT_MARGIN_WIDTH (w);
1034 else if (area == RIGHT_MARGIN_AREA)
1035 pixels = WINDOW_RIGHT_MARGIN_WIDTH (w);
1036 }
1037
1038 return pixels;
1039 }
1040
1041
1042 /* Return the pixel height of the display area of window W, not
1043 including mode lines of W, if any. */
1044
1045 int
1046 window_box_height (struct window *w)
1047 {
1048 struct frame *f = XFRAME (w->frame);
1049 int height = WINDOW_PIXEL_HEIGHT (w);
1050
1051 eassert (height >= 0);
1052
1053 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1054
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1060
1061 if (WINDOW_WANTS_MODELINE_P (w))
1062 {
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1071 }
1072
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1074 {
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1083 }
1084
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1088 }
1089
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. ANY_AREA means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1093
1094 int
1095 window_box_left_offset (struct window *w, enum glyph_row_area area)
1096 {
1097 int x;
1098
1099 if (w->pseudo_window_p)
1100 return 0;
1101
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 ? 0
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. ANY_AREA means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 int
1127 window_box_right_offset (struct window *w, enum glyph_row_area area)
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. ANY_AREA means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 int
1137 window_box_left (struct window *w, enum glyph_row_area area)
1138 {
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1141
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1144
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1147
1148 return x;
1149 }
1150
1151
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. ANY_AREA means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1155
1156 int
1157 window_box_right (struct window *w, enum glyph_row_area area)
1158 {
1159 return window_box_left (w, area) + window_box_width (w, area);
1160 }
1161
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. ANY_AREA means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168
1169 void
1170 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1171 int *box_y, int *box_width, int *box_height)
1172 {
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1180 {
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1184 }
1185 }
1186
1187 #ifdef HAVE_WINDOW_SYSTEM
1188
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1195
1196 static void
1197 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1199 {
1200 window_box (w, ANY_AREA, top_left_x, top_left_y,
1201 bottom_right_x, bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1204 }
1205
1206 #endif /* HAVE_WINDOW_SYSTEM */
1207
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1211
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1214
1215 int
1216 line_bottom_y (struct it *it)
1217 {
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1220
1221 if (line_height == 0)
1222 {
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1226 {
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1231 }
1232 else
1233 {
1234 struct glyph_row *row = it->glyph_row;
1235
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1244 }
1245 }
1246
1247 return line_top_y + line_height;
1248 }
1249
1250 DEFUN ("line-pixel-height", Fline_pixel_height,
1251 Sline_pixel_height, 0, 0, 0,
1252 doc: /* Return height in pixels of text line in the selected window.
1253
1254 Value is the height in pixels of the line at point. */)
1255 (void)
1256 {
1257 struct it it;
1258 struct text_pos pt;
1259 struct window *w = XWINDOW (selected_window);
1260
1261 SET_TEXT_POS (pt, PT, PT_BYTE);
1262 start_display (&it, w, pt);
1263 it.vpos = it.current_y = 0;
1264 last_height = 0;
1265 return make_number (line_bottom_y (&it));
1266 }
1267
1268 /* Return the default pixel height of text lines in window W. The
1269 value is the canonical height of the W frame's default font, plus
1270 any extra space required by the line-spacing variable or frame
1271 parameter.
1272
1273 Implementation note: this ignores any line-spacing text properties
1274 put on the newline characters. This is because those properties
1275 only affect the _screen_ line ending in the newline (i.e., in a
1276 continued line, only the last screen line will be affected), which
1277 means only a small number of lines in a buffer can ever use this
1278 feature. Since this function is used to compute the default pixel
1279 equivalent of text lines in a window, we can safely ignore those
1280 few lines. For the same reasons, we ignore the line-height
1281 properties. */
1282 int
1283 default_line_pixel_height (struct window *w)
1284 {
1285 struct frame *f = WINDOW_XFRAME (w);
1286 int height = FRAME_LINE_HEIGHT (f);
1287
1288 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1289 {
1290 struct buffer *b = XBUFFER (w->contents);
1291 Lisp_Object val = BVAR (b, extra_line_spacing);
1292
1293 if (NILP (val))
1294 val = BVAR (&buffer_defaults, extra_line_spacing);
1295 if (!NILP (val))
1296 {
1297 if (RANGED_INTEGERP (0, val, INT_MAX))
1298 height += XFASTINT (val);
1299 else if (FLOATP (val))
1300 {
1301 int addon = XFLOAT_DATA (val) * height + 0.5;
1302
1303 if (addon >= 0)
1304 height += addon;
1305 }
1306 }
1307 else
1308 height += f->extra_line_spacing;
1309 }
1310
1311 return height;
1312 }
1313
1314 /* Subroutine of pos_visible_p below. Extracts a display string, if
1315 any, from the display spec given as its argument. */
1316 static Lisp_Object
1317 string_from_display_spec (Lisp_Object spec)
1318 {
1319 if (CONSP (spec))
1320 {
1321 while (CONSP (spec))
1322 {
1323 if (STRINGP (XCAR (spec)))
1324 return XCAR (spec);
1325 spec = XCDR (spec);
1326 }
1327 }
1328 else if (VECTORP (spec))
1329 {
1330 ptrdiff_t i;
1331
1332 for (i = 0; i < ASIZE (spec); i++)
1333 {
1334 if (STRINGP (AREF (spec, i)))
1335 return AREF (spec, i);
1336 }
1337 return Qnil;
1338 }
1339
1340 return spec;
1341 }
1342
1343
1344 /* Limit insanely large values of W->hscroll on frame F to the largest
1345 value that will still prevent first_visible_x and last_visible_x of
1346 'struct it' from overflowing an int. */
1347 static int
1348 window_hscroll_limited (struct window *w, struct frame *f)
1349 {
1350 ptrdiff_t window_hscroll = w->hscroll;
1351 int window_text_width = window_box_width (w, TEXT_AREA);
1352 int colwidth = FRAME_COLUMN_WIDTH (f);
1353
1354 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1355 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1356
1357 return window_hscroll;
1358 }
1359
1360 /* Return 1 if position CHARPOS is visible in window W.
1361 CHARPOS < 0 means return info about WINDOW_END position.
1362 If visible, set *X and *Y to pixel coordinates of top left corner.
1363 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1364 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1365
1366 int
1367 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1368 int *rtop, int *rbot, int *rowh, int *vpos)
1369 {
1370 struct it it;
1371 void *itdata = bidi_shelve_cache ();
1372 struct text_pos top;
1373 int visible_p = 0;
1374 struct buffer *old_buffer = NULL;
1375
1376 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1377 return visible_p;
1378
1379 if (XBUFFER (w->contents) != current_buffer)
1380 {
1381 old_buffer = current_buffer;
1382 set_buffer_internal_1 (XBUFFER (w->contents));
1383 }
1384
1385 SET_TEXT_POS_FROM_MARKER (top, w->start);
1386 /* Scrolling a minibuffer window via scroll bar when the echo area
1387 shows long text sometimes resets the minibuffer contents behind
1388 our backs. */
1389 if (CHARPOS (top) > ZV)
1390 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1391
1392 /* Compute exact mode line heights. */
1393 if (WINDOW_WANTS_MODELINE_P (w))
1394 w->mode_line_height
1395 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1396 BVAR (current_buffer, mode_line_format));
1397
1398 if (WINDOW_WANTS_HEADER_LINE_P (w))
1399 w->header_line_height
1400 = display_mode_line (w, HEADER_LINE_FACE_ID,
1401 BVAR (current_buffer, header_line_format));
1402
1403 start_display (&it, w, top);
1404 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1405 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1406
1407 if (charpos >= 0
1408 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1409 && IT_CHARPOS (it) >= charpos)
1410 /* When scanning backwards under bidi iteration, move_it_to
1411 stops at or _before_ CHARPOS, because it stops at or to
1412 the _right_ of the character at CHARPOS. */
1413 || (it.bidi_p && it.bidi_it.scan_dir == -1
1414 && IT_CHARPOS (it) <= charpos)))
1415 {
1416 /* We have reached CHARPOS, or passed it. How the call to
1417 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1418 or covered by a display property, move_it_to stops at the end
1419 of the invisible text, to the right of CHARPOS. (ii) If
1420 CHARPOS is in a display vector, move_it_to stops on its last
1421 glyph. */
1422 int top_x = it.current_x;
1423 int top_y = it.current_y;
1424 /* Calling line_bottom_y may change it.method, it.position, etc. */
1425 enum it_method it_method = it.method;
1426 int bottom_y = (last_height = 0, line_bottom_y (&it));
1427 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1428
1429 if (top_y < window_top_y)
1430 visible_p = bottom_y > window_top_y;
1431 else if (top_y < it.last_visible_y)
1432 visible_p = true;
1433 if (bottom_y >= it.last_visible_y
1434 && it.bidi_p && it.bidi_it.scan_dir == -1
1435 && IT_CHARPOS (it) < charpos)
1436 {
1437 /* When the last line of the window is scanned backwards
1438 under bidi iteration, we could be duped into thinking
1439 that we have passed CHARPOS, when in fact move_it_to
1440 simply stopped short of CHARPOS because it reached
1441 last_visible_y. To see if that's what happened, we call
1442 move_it_to again with a slightly larger vertical limit,
1443 and see if it actually moved vertically; if it did, we
1444 didn't really reach CHARPOS, which is beyond window end. */
1445 struct it save_it = it;
1446 /* Why 10? because we don't know how many canonical lines
1447 will the height of the next line(s) be. So we guess. */
1448 int ten_more_lines = 10 * default_line_pixel_height (w);
1449
1450 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1451 MOVE_TO_POS | MOVE_TO_Y);
1452 if (it.current_y > top_y)
1453 visible_p = 0;
1454
1455 it = save_it;
1456 }
1457 if (visible_p)
1458 {
1459 if (it_method == GET_FROM_DISPLAY_VECTOR)
1460 {
1461 /* We stopped on the last glyph of a display vector.
1462 Try and recompute. Hack alert! */
1463 if (charpos < 2 || top.charpos >= charpos)
1464 top_x = it.glyph_row->x;
1465 else
1466 {
1467 struct it it2, it2_prev;
1468 /* The idea is to get to the previous buffer
1469 position, consume the character there, and use
1470 the pixel coordinates we get after that. But if
1471 the previous buffer position is also displayed
1472 from a display vector, we need to consume all of
1473 the glyphs from that display vector. */
1474 start_display (&it2, w, top);
1475 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1476 /* If we didn't get to CHARPOS - 1, there's some
1477 replacing display property at that position, and
1478 we stopped after it. That is exactly the place
1479 whose coordinates we want. */
1480 if (IT_CHARPOS (it2) != charpos - 1)
1481 it2_prev = it2;
1482 else
1483 {
1484 /* Iterate until we get out of the display
1485 vector that displays the character at
1486 CHARPOS - 1. */
1487 do {
1488 get_next_display_element (&it2);
1489 PRODUCE_GLYPHS (&it2);
1490 it2_prev = it2;
1491 set_iterator_to_next (&it2, 1);
1492 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1493 && IT_CHARPOS (it2) < charpos);
1494 }
1495 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1496 || it2_prev.current_x > it2_prev.last_visible_x)
1497 top_x = it.glyph_row->x;
1498 else
1499 {
1500 top_x = it2_prev.current_x;
1501 top_y = it2_prev.current_y;
1502 }
1503 }
1504 }
1505 else if (IT_CHARPOS (it) != charpos)
1506 {
1507 Lisp_Object cpos = make_number (charpos);
1508 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1509 Lisp_Object string = string_from_display_spec (spec);
1510 struct text_pos tpos;
1511 int replacing_spec_p;
1512 bool newline_in_string
1513 = (STRINGP (string)
1514 && memchr (SDATA (string), '\n', SBYTES (string)));
1515
1516 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1517 replacing_spec_p
1518 = (!NILP (spec)
1519 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1520 charpos, FRAME_WINDOW_P (it.f)));
1521 /* The tricky code below is needed because there's a
1522 discrepancy between move_it_to and how we set cursor
1523 when PT is at the beginning of a portion of text
1524 covered by a display property or an overlay with a
1525 display property, or the display line ends in a
1526 newline from a display string. move_it_to will stop
1527 _after_ such display strings, whereas
1528 set_cursor_from_row conspires with cursor_row_p to
1529 place the cursor on the first glyph produced from the
1530 display string. */
1531
1532 /* We have overshoot PT because it is covered by a
1533 display property that replaces the text it covers.
1534 If the string includes embedded newlines, we are also
1535 in the wrong display line. Backtrack to the correct
1536 line, where the display property begins. */
1537 if (replacing_spec_p)
1538 {
1539 Lisp_Object startpos, endpos;
1540 EMACS_INT start, end;
1541 struct it it3;
1542 int it3_moved;
1543
1544 /* Find the first and the last buffer positions
1545 covered by the display string. */
1546 endpos =
1547 Fnext_single_char_property_change (cpos, Qdisplay,
1548 Qnil, Qnil);
1549 startpos =
1550 Fprevious_single_char_property_change (endpos, Qdisplay,
1551 Qnil, Qnil);
1552 start = XFASTINT (startpos);
1553 end = XFASTINT (endpos);
1554 /* Move to the last buffer position before the
1555 display property. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1558 /* Move forward one more line if the position before
1559 the display string is a newline or if it is the
1560 rightmost character on a line that is
1561 continued or word-wrapped. */
1562 if (it3.method == GET_FROM_BUFFER
1563 && (it3.c == '\n'
1564 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1565 move_it_by_lines (&it3, 1);
1566 else if (move_it_in_display_line_to (&it3, -1,
1567 it3.current_x
1568 + it3.pixel_width,
1569 MOVE_TO_X)
1570 == MOVE_LINE_CONTINUED)
1571 {
1572 move_it_by_lines (&it3, 1);
1573 /* When we are under word-wrap, the #$@%!
1574 move_it_by_lines moves 2 lines, so we need to
1575 fix that up. */
1576 if (it3.line_wrap == WORD_WRAP)
1577 move_it_by_lines (&it3, -1);
1578 }
1579
1580 /* Record the vertical coordinate of the display
1581 line where we wound up. */
1582 top_y = it3.current_y;
1583 if (it3.bidi_p)
1584 {
1585 /* When characters are reordered for display,
1586 the character displayed to the left of the
1587 display string could be _after_ the display
1588 property in the logical order. Use the
1589 smallest vertical position of these two. */
1590 start_display (&it3, w, top);
1591 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1592 if (it3.current_y < top_y)
1593 top_y = it3.current_y;
1594 }
1595 /* Move from the top of the window to the beginning
1596 of the display line where the display string
1597 begins. */
1598 start_display (&it3, w, top);
1599 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1600 /* If it3_moved stays zero after the 'while' loop
1601 below, that means we already were at a newline
1602 before the loop (e.g., the display string begins
1603 with a newline), so we don't need to (and cannot)
1604 inspect the glyphs of it3.glyph_row, because
1605 PRODUCE_GLYPHS will not produce anything for a
1606 newline, and thus it3.glyph_row stays at its
1607 stale content it got at top of the window. */
1608 it3_moved = 0;
1609 /* Finally, advance the iterator until we hit the
1610 first display element whose character position is
1611 CHARPOS, or until the first newline from the
1612 display string, which signals the end of the
1613 display line. */
1614 while (get_next_display_element (&it3))
1615 {
1616 PRODUCE_GLYPHS (&it3);
1617 if (IT_CHARPOS (it3) == charpos
1618 || ITERATOR_AT_END_OF_LINE_P (&it3))
1619 break;
1620 it3_moved = 1;
1621 set_iterator_to_next (&it3, 0);
1622 }
1623 top_x = it3.current_x - it3.pixel_width;
1624 /* Normally, we would exit the above loop because we
1625 found the display element whose character
1626 position is CHARPOS. For the contingency that we
1627 didn't, and stopped at the first newline from the
1628 display string, move back over the glyphs
1629 produced from the string, until we find the
1630 rightmost glyph not from the string. */
1631 if (it3_moved
1632 && newline_in_string
1633 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1634 {
1635 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1636 + it3.glyph_row->used[TEXT_AREA];
1637
1638 while (EQ ((g - 1)->object, string))
1639 {
1640 --g;
1641 top_x -= g->pixel_width;
1642 }
1643 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1644 + it3.glyph_row->used[TEXT_AREA]);
1645 }
1646 }
1647 }
1648
1649 *x = top_x;
1650 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1651 *rtop = max (0, window_top_y - top_y);
1652 *rbot = max (0, bottom_y - it.last_visible_y);
1653 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1654 - max (top_y, window_top_y)));
1655 *vpos = it.vpos;
1656 }
1657 }
1658 else
1659 {
1660 /* We were asked to provide info about WINDOW_END. */
1661 struct it it2;
1662 void *it2data = NULL;
1663
1664 SAVE_IT (it2, it, it2data);
1665 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1666 move_it_by_lines (&it, 1);
1667 if (charpos < IT_CHARPOS (it)
1668 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1669 {
1670 visible_p = true;
1671 RESTORE_IT (&it2, &it2, it2data);
1672 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1673 *x = it2.current_x;
1674 *y = it2.current_y + it2.max_ascent - it2.ascent;
1675 *rtop = max (0, -it2.current_y);
1676 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1677 - it.last_visible_y));
1678 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1679 it.last_visible_y)
1680 - max (it2.current_y,
1681 WINDOW_HEADER_LINE_HEIGHT (w))));
1682 *vpos = it2.vpos;
1683 }
1684 else
1685 bidi_unshelve_cache (it2data, 1);
1686 }
1687 bidi_unshelve_cache (itdata, 0);
1688
1689 if (old_buffer)
1690 set_buffer_internal_1 (old_buffer);
1691
1692 if (visible_p && w->hscroll > 0)
1693 *x -=
1694 window_hscroll_limited (w, WINDOW_XFRAME (w))
1695 * WINDOW_FRAME_COLUMN_WIDTH (w);
1696
1697 #if 0
1698 /* Debugging code. */
1699 if (visible_p)
1700 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1701 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1702 else
1703 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1704 #endif
1705
1706 return visible_p;
1707 }
1708
1709
1710 /* Return the next character from STR. Return in *LEN the length of
1711 the character. This is like STRING_CHAR_AND_LENGTH but never
1712 returns an invalid character. If we find one, we return a `?', but
1713 with the length of the invalid character. */
1714
1715 static int
1716 string_char_and_length (const unsigned char *str, int *len)
1717 {
1718 int c;
1719
1720 c = STRING_CHAR_AND_LENGTH (str, *len);
1721 if (!CHAR_VALID_P (c))
1722 /* We may not change the length here because other places in Emacs
1723 don't use this function, i.e. they silently accept invalid
1724 characters. */
1725 c = '?';
1726
1727 return c;
1728 }
1729
1730
1731
1732 /* Given a position POS containing a valid character and byte position
1733 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1734
1735 static struct text_pos
1736 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1737 {
1738 eassert (STRINGP (string) && nchars >= 0);
1739
1740 if (STRING_MULTIBYTE (string))
1741 {
1742 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1743 int len;
1744
1745 while (nchars--)
1746 {
1747 string_char_and_length (p, &len);
1748 p += len;
1749 CHARPOS (pos) += 1;
1750 BYTEPOS (pos) += len;
1751 }
1752 }
1753 else
1754 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1755
1756 return pos;
1757 }
1758
1759
1760 /* Value is the text position, i.e. character and byte position,
1761 for character position CHARPOS in STRING. */
1762
1763 static struct text_pos
1764 string_pos (ptrdiff_t charpos, Lisp_Object string)
1765 {
1766 struct text_pos pos;
1767 eassert (STRINGP (string));
1768 eassert (charpos >= 0);
1769 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1770 return pos;
1771 }
1772
1773
1774 /* Value is a text position, i.e. character and byte position, for
1775 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1776 means recognize multibyte characters. */
1777
1778 static struct text_pos
1779 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1780 {
1781 struct text_pos pos;
1782
1783 eassert (s != NULL);
1784 eassert (charpos >= 0);
1785
1786 if (multibyte_p)
1787 {
1788 int len;
1789
1790 SET_TEXT_POS (pos, 0, 0);
1791 while (charpos--)
1792 {
1793 string_char_and_length ((const unsigned char *) s, &len);
1794 s += len;
1795 CHARPOS (pos) += 1;
1796 BYTEPOS (pos) += len;
1797 }
1798 }
1799 else
1800 SET_TEXT_POS (pos, charpos, charpos);
1801
1802 return pos;
1803 }
1804
1805
1806 /* Value is the number of characters in C string S. MULTIBYTE_P
1807 non-zero means recognize multibyte characters. */
1808
1809 static ptrdiff_t
1810 number_of_chars (const char *s, bool multibyte_p)
1811 {
1812 ptrdiff_t nchars;
1813
1814 if (multibyte_p)
1815 {
1816 ptrdiff_t rest = strlen (s);
1817 int len;
1818 const unsigned char *p = (const unsigned char *) s;
1819
1820 for (nchars = 0; rest > 0; ++nchars)
1821 {
1822 string_char_and_length (p, &len);
1823 rest -= len, p += len;
1824 }
1825 }
1826 else
1827 nchars = strlen (s);
1828
1829 return nchars;
1830 }
1831
1832
1833 /* Compute byte position NEWPOS->bytepos corresponding to
1834 NEWPOS->charpos. POS is a known position in string STRING.
1835 NEWPOS->charpos must be >= POS.charpos. */
1836
1837 static void
1838 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1839 {
1840 eassert (STRINGP (string));
1841 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1842
1843 if (STRING_MULTIBYTE (string))
1844 *newpos = string_pos_nchars_ahead (pos, string,
1845 CHARPOS (*newpos) - CHARPOS (pos));
1846 else
1847 BYTEPOS (*newpos) = CHARPOS (*newpos);
1848 }
1849
1850 /* EXPORT:
1851 Return an estimation of the pixel height of mode or header lines on
1852 frame F. FACE_ID specifies what line's height to estimate. */
1853
1854 int
1855 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1856 {
1857 #ifdef HAVE_WINDOW_SYSTEM
1858 if (FRAME_WINDOW_P (f))
1859 {
1860 int height = FONT_HEIGHT (FRAME_FONT (f));
1861
1862 /* This function is called so early when Emacs starts that the face
1863 cache and mode line face are not yet initialized. */
1864 if (FRAME_FACE_CACHE (f))
1865 {
1866 struct face *face = FACE_FROM_ID (f, face_id);
1867 if (face)
1868 {
1869 if (face->font)
1870 height = FONT_HEIGHT (face->font);
1871 if (face->box_line_width > 0)
1872 height += 2 * face->box_line_width;
1873 }
1874 }
1875
1876 return height;
1877 }
1878 #endif
1879
1880 return 1;
1881 }
1882
1883 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1884 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1885 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1886 not force the value into range. */
1887
1888 void
1889 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1890 int *x, int *y, NativeRectangle *bounds, int noclip)
1891 {
1892
1893 #ifdef HAVE_WINDOW_SYSTEM
1894 if (FRAME_WINDOW_P (f))
1895 {
1896 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1897 even for negative values. */
1898 if (pix_x < 0)
1899 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1900 if (pix_y < 0)
1901 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1902
1903 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1904 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1905
1906 if (bounds)
1907 STORE_NATIVE_RECT (*bounds,
1908 FRAME_COL_TO_PIXEL_X (f, pix_x),
1909 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1910 FRAME_COLUMN_WIDTH (f) - 1,
1911 FRAME_LINE_HEIGHT (f) - 1);
1912
1913 /* PXW: Should we clip pixels before converting to columns/lines? */
1914 if (!noclip)
1915 {
1916 if (pix_x < 0)
1917 pix_x = 0;
1918 else if (pix_x > FRAME_TOTAL_COLS (f))
1919 pix_x = FRAME_TOTAL_COLS (f);
1920
1921 if (pix_y < 0)
1922 pix_y = 0;
1923 else if (pix_y > FRAME_LINES (f))
1924 pix_y = FRAME_LINES (f);
1925 }
1926 }
1927 #endif
1928
1929 *x = pix_x;
1930 *y = pix_y;
1931 }
1932
1933
1934 /* Find the glyph under window-relative coordinates X/Y in window W.
1935 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1936 strings. Return in *HPOS and *VPOS the row and column number of
1937 the glyph found. Return in *AREA the glyph area containing X.
1938 Value is a pointer to the glyph found or null if X/Y is not on
1939 text, or we can't tell because W's current matrix is not up to
1940 date. */
1941
1942 static struct glyph *
1943 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1944 int *dx, int *dy, int *area)
1945 {
1946 struct glyph *glyph, *end;
1947 struct glyph_row *row = NULL;
1948 int x0, i;
1949
1950 /* Find row containing Y. Give up if some row is not enabled. */
1951 for (i = 0; i < w->current_matrix->nrows; ++i)
1952 {
1953 row = MATRIX_ROW (w->current_matrix, i);
1954 if (!row->enabled_p)
1955 return NULL;
1956 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1957 break;
1958 }
1959
1960 *vpos = i;
1961 *hpos = 0;
1962
1963 /* Give up if Y is not in the window. */
1964 if (i == w->current_matrix->nrows)
1965 return NULL;
1966
1967 /* Get the glyph area containing X. */
1968 if (w->pseudo_window_p)
1969 {
1970 *area = TEXT_AREA;
1971 x0 = 0;
1972 }
1973 else
1974 {
1975 if (x < window_box_left_offset (w, TEXT_AREA))
1976 {
1977 *area = LEFT_MARGIN_AREA;
1978 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1979 }
1980 else if (x < window_box_right_offset (w, TEXT_AREA))
1981 {
1982 *area = TEXT_AREA;
1983 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1984 }
1985 else
1986 {
1987 *area = RIGHT_MARGIN_AREA;
1988 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1989 }
1990 }
1991
1992 /* Find glyph containing X. */
1993 glyph = row->glyphs[*area];
1994 end = glyph + row->used[*area];
1995 x -= x0;
1996 while (glyph < end && x >= glyph->pixel_width)
1997 {
1998 x -= glyph->pixel_width;
1999 ++glyph;
2000 }
2001
2002 if (glyph == end)
2003 return NULL;
2004
2005 if (dx)
2006 {
2007 *dx = x;
2008 *dy = y - (row->y + row->ascent - glyph->ascent);
2009 }
2010
2011 *hpos = glyph - row->glyphs[*area];
2012 return glyph;
2013 }
2014
2015 /* Convert frame-relative x/y to coordinates relative to window W.
2016 Takes pseudo-windows into account. */
2017
2018 static void
2019 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2020 {
2021 if (w->pseudo_window_p)
2022 {
2023 /* A pseudo-window is always full-width, and starts at the
2024 left edge of the frame, plus a frame border. */
2025 struct frame *f = XFRAME (w->frame);
2026 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2027 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2028 }
2029 else
2030 {
2031 *x -= WINDOW_LEFT_EDGE_X (w);
2032 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2033 }
2034 }
2035
2036 #ifdef HAVE_WINDOW_SYSTEM
2037
2038 /* EXPORT:
2039 Return in RECTS[] at most N clipping rectangles for glyph string S.
2040 Return the number of stored rectangles. */
2041
2042 int
2043 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2044 {
2045 XRectangle r;
2046
2047 if (n <= 0)
2048 return 0;
2049
2050 if (s->row->full_width_p)
2051 {
2052 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2053 r.x = WINDOW_LEFT_EDGE_X (s->w);
2054 if (s->row->mode_line_p)
2055 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2056 else
2057 r.width = WINDOW_PIXEL_WIDTH (s->w);
2058
2059 /* Unless displaying a mode or menu bar line, which are always
2060 fully visible, clip to the visible part of the row. */
2061 if (s->w->pseudo_window_p)
2062 r.height = s->row->visible_height;
2063 else
2064 r.height = s->height;
2065 }
2066 else
2067 {
2068 /* This is a text line that may be partially visible. */
2069 r.x = window_box_left (s->w, s->area);
2070 r.width = window_box_width (s->w, s->area);
2071 r.height = s->row->visible_height;
2072 }
2073
2074 if (s->clip_head)
2075 if (r.x < s->clip_head->x)
2076 {
2077 if (r.width >= s->clip_head->x - r.x)
2078 r.width -= s->clip_head->x - r.x;
2079 else
2080 r.width = 0;
2081 r.x = s->clip_head->x;
2082 }
2083 if (s->clip_tail)
2084 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2085 {
2086 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2087 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2088 else
2089 r.width = 0;
2090 }
2091
2092 /* If S draws overlapping rows, it's sufficient to use the top and
2093 bottom of the window for clipping because this glyph string
2094 intentionally draws over other lines. */
2095 if (s->for_overlaps)
2096 {
2097 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2098 r.height = window_text_bottom_y (s->w) - r.y;
2099
2100 /* Alas, the above simple strategy does not work for the
2101 environments with anti-aliased text: if the same text is
2102 drawn onto the same place multiple times, it gets thicker.
2103 If the overlap we are processing is for the erased cursor, we
2104 take the intersection with the rectangle of the cursor. */
2105 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2106 {
2107 XRectangle rc, r_save = r;
2108
2109 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2110 rc.y = s->w->phys_cursor.y;
2111 rc.width = s->w->phys_cursor_width;
2112 rc.height = s->w->phys_cursor_height;
2113
2114 x_intersect_rectangles (&r_save, &rc, &r);
2115 }
2116 }
2117 else
2118 {
2119 /* Don't use S->y for clipping because it doesn't take partially
2120 visible lines into account. For example, it can be negative for
2121 partially visible lines at the top of a window. */
2122 if (!s->row->full_width_p
2123 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2124 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2125 else
2126 r.y = max (0, s->row->y);
2127 }
2128
2129 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2130
2131 /* If drawing the cursor, don't let glyph draw outside its
2132 advertised boundaries. Cleartype does this under some circumstances. */
2133 if (s->hl == DRAW_CURSOR)
2134 {
2135 struct glyph *glyph = s->first_glyph;
2136 int height, max_y;
2137
2138 if (s->x > r.x)
2139 {
2140 r.width -= s->x - r.x;
2141 r.x = s->x;
2142 }
2143 r.width = min (r.width, glyph->pixel_width);
2144
2145 /* If r.y is below window bottom, ensure that we still see a cursor. */
2146 height = min (glyph->ascent + glyph->descent,
2147 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2148 max_y = window_text_bottom_y (s->w) - height;
2149 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2150 if (s->ybase - glyph->ascent > max_y)
2151 {
2152 r.y = max_y;
2153 r.height = height;
2154 }
2155 else
2156 {
2157 /* Don't draw cursor glyph taller than our actual glyph. */
2158 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2159 if (height < r.height)
2160 {
2161 max_y = r.y + r.height;
2162 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2163 r.height = min (max_y - r.y, height);
2164 }
2165 }
2166 }
2167
2168 if (s->row->clip)
2169 {
2170 XRectangle r_save = r;
2171
2172 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2173 r.width = 0;
2174 }
2175
2176 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2177 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2178 {
2179 #ifdef CONVERT_FROM_XRECT
2180 CONVERT_FROM_XRECT (r, *rects);
2181 #else
2182 *rects = r;
2183 #endif
2184 return 1;
2185 }
2186 else
2187 {
2188 /* If we are processing overlapping and allowed to return
2189 multiple clipping rectangles, we exclude the row of the glyph
2190 string from the clipping rectangle. This is to avoid drawing
2191 the same text on the environment with anti-aliasing. */
2192 #ifdef CONVERT_FROM_XRECT
2193 XRectangle rs[2];
2194 #else
2195 XRectangle *rs = rects;
2196 #endif
2197 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2198
2199 if (s->for_overlaps & OVERLAPS_PRED)
2200 {
2201 rs[i] = r;
2202 if (r.y + r.height > row_y)
2203 {
2204 if (r.y < row_y)
2205 rs[i].height = row_y - r.y;
2206 else
2207 rs[i].height = 0;
2208 }
2209 i++;
2210 }
2211 if (s->for_overlaps & OVERLAPS_SUCC)
2212 {
2213 rs[i] = r;
2214 if (r.y < row_y + s->row->visible_height)
2215 {
2216 if (r.y + r.height > row_y + s->row->visible_height)
2217 {
2218 rs[i].y = row_y + s->row->visible_height;
2219 rs[i].height = r.y + r.height - rs[i].y;
2220 }
2221 else
2222 rs[i].height = 0;
2223 }
2224 i++;
2225 }
2226
2227 n = i;
2228 #ifdef CONVERT_FROM_XRECT
2229 for (i = 0; i < n; i++)
2230 CONVERT_FROM_XRECT (rs[i], rects[i]);
2231 #endif
2232 return n;
2233 }
2234 }
2235
2236 /* EXPORT:
2237 Return in *NR the clipping rectangle for glyph string S. */
2238
2239 void
2240 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2241 {
2242 get_glyph_string_clip_rects (s, nr, 1);
2243 }
2244
2245
2246 /* EXPORT:
2247 Return the position and height of the phys cursor in window W.
2248 Set w->phys_cursor_width to width of phys cursor.
2249 */
2250
2251 void
2252 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2253 struct glyph *glyph, int *xp, int *yp, int *heightp)
2254 {
2255 struct frame *f = XFRAME (WINDOW_FRAME (w));
2256 int x, y, wd, h, h0, y0;
2257
2258 /* Compute the width of the rectangle to draw. If on a stretch
2259 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2260 rectangle as wide as the glyph, but use a canonical character
2261 width instead. */
2262 wd = glyph->pixel_width - 1;
2263 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2264 wd++; /* Why? */
2265 #endif
2266
2267 x = w->phys_cursor.x;
2268 if (x < 0)
2269 {
2270 wd += x;
2271 x = 0;
2272 }
2273
2274 if (glyph->type == STRETCH_GLYPH
2275 && !x_stretch_cursor_p)
2276 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2277 w->phys_cursor_width = wd;
2278
2279 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2280
2281 /* If y is below window bottom, ensure that we still see a cursor. */
2282 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2283
2284 h = max (h0, glyph->ascent + glyph->descent);
2285 h0 = min (h0, glyph->ascent + glyph->descent);
2286
2287 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2288 if (y < y0)
2289 {
2290 h = max (h - (y0 - y) + 1, h0);
2291 y = y0 - 1;
2292 }
2293 else
2294 {
2295 y0 = window_text_bottom_y (w) - h0;
2296 if (y > y0)
2297 {
2298 h += y - y0;
2299 y = y0;
2300 }
2301 }
2302
2303 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2304 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2305 *heightp = h;
2306 }
2307
2308 /*
2309 * Remember which glyph the mouse is over.
2310 */
2311
2312 void
2313 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2314 {
2315 Lisp_Object window;
2316 struct window *w;
2317 struct glyph_row *r, *gr, *end_row;
2318 enum window_part part;
2319 enum glyph_row_area area;
2320 int x, y, width, height;
2321
2322 /* Try to determine frame pixel position and size of the glyph under
2323 frame pixel coordinates X/Y on frame F. */
2324
2325 if (window_resize_pixelwise)
2326 {
2327 width = height = 1;
2328 goto virtual_glyph;
2329 }
2330 else if (!f->glyphs_initialized_p
2331 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2332 NILP (window)))
2333 {
2334 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2335 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2336 goto virtual_glyph;
2337 }
2338
2339 w = XWINDOW (window);
2340 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2341 height = WINDOW_FRAME_LINE_HEIGHT (w);
2342
2343 x = window_relative_x_coord (w, part, gx);
2344 y = gy - WINDOW_TOP_EDGE_Y (w);
2345
2346 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2347 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2348
2349 if (w->pseudo_window_p)
2350 {
2351 area = TEXT_AREA;
2352 part = ON_MODE_LINE; /* Don't adjust margin. */
2353 goto text_glyph;
2354 }
2355
2356 switch (part)
2357 {
2358 case ON_LEFT_MARGIN:
2359 area = LEFT_MARGIN_AREA;
2360 goto text_glyph;
2361
2362 case ON_RIGHT_MARGIN:
2363 area = RIGHT_MARGIN_AREA;
2364 goto text_glyph;
2365
2366 case ON_HEADER_LINE:
2367 case ON_MODE_LINE:
2368 gr = (part == ON_HEADER_LINE
2369 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2370 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2371 gy = gr->y;
2372 area = TEXT_AREA;
2373 goto text_glyph_row_found;
2374
2375 case ON_TEXT:
2376 area = TEXT_AREA;
2377
2378 text_glyph:
2379 gr = 0; gy = 0;
2380 for (; r <= end_row && r->enabled_p; ++r)
2381 if (r->y + r->height > y)
2382 {
2383 gr = r; gy = r->y;
2384 break;
2385 }
2386
2387 text_glyph_row_found:
2388 if (gr && gy <= y)
2389 {
2390 struct glyph *g = gr->glyphs[area];
2391 struct glyph *end = g + gr->used[area];
2392
2393 height = gr->height;
2394 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2395 if (gx + g->pixel_width > x)
2396 break;
2397
2398 if (g < end)
2399 {
2400 if (g->type == IMAGE_GLYPH)
2401 {
2402 /* Don't remember when mouse is over image, as
2403 image may have hot-spots. */
2404 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2405 return;
2406 }
2407 width = g->pixel_width;
2408 }
2409 else
2410 {
2411 /* Use nominal char spacing at end of line. */
2412 x -= gx;
2413 gx += (x / width) * width;
2414 }
2415
2416 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2417 gx += window_box_left_offset (w, area);
2418 }
2419 else
2420 {
2421 /* Use nominal line height at end of window. */
2422 gx = (x / width) * width;
2423 y -= gy;
2424 gy += (y / height) * height;
2425 }
2426 break;
2427
2428 case ON_LEFT_FRINGE:
2429 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2430 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2431 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2432 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2433 goto row_glyph;
2434
2435 case ON_RIGHT_FRINGE:
2436 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2438 : window_box_right_offset (w, TEXT_AREA));
2439 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2440 goto row_glyph;
2441
2442 case ON_SCROLL_BAR:
2443 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2444 ? 0
2445 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2446 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2447 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2448 : 0)));
2449 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2450
2451 row_glyph:
2452 gr = 0, gy = 0;
2453 for (; r <= end_row && r->enabled_p; ++r)
2454 if (r->y + r->height > y)
2455 {
2456 gr = r; gy = r->y;
2457 break;
2458 }
2459
2460 if (gr && gy <= y)
2461 height = gr->height;
2462 else
2463 {
2464 /* Use nominal line height at end of window. */
2465 y -= gy;
2466 gy += (y / height) * height;
2467 }
2468 break;
2469
2470 default:
2471 ;
2472 virtual_glyph:
2473 /* If there is no glyph under the mouse, then we divide the screen
2474 into a grid of the smallest glyph in the frame, and use that
2475 as our "glyph". */
2476
2477 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2478 round down even for negative values. */
2479 if (gx < 0)
2480 gx -= width - 1;
2481 if (gy < 0)
2482 gy -= height - 1;
2483
2484 gx = (gx / width) * width;
2485 gy = (gy / height) * height;
2486
2487 goto store_rect;
2488 }
2489
2490 gx += WINDOW_LEFT_EDGE_X (w);
2491 gy += WINDOW_TOP_EDGE_Y (w);
2492
2493 store_rect:
2494 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2495
2496 /* Visible feedback for debugging. */
2497 #if 0
2498 #if HAVE_X_WINDOWS
2499 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2500 f->output_data.x->normal_gc,
2501 gx, gy, width, height);
2502 #endif
2503 #endif
2504 }
2505
2506
2507 #endif /* HAVE_WINDOW_SYSTEM */
2508
2509 static void
2510 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2511 {
2512 eassert (w);
2513 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2514 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2515 w->window_end_vpos
2516 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2517 }
2518
2519 /***********************************************************************
2520 Lisp form evaluation
2521 ***********************************************************************/
2522
2523 /* Error handler for safe_eval and safe_call. */
2524
2525 static Lisp_Object
2526 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2527 {
2528 add_to_log ("Error during redisplay: %S signaled %S",
2529 Flist (nargs, args), arg);
2530 return Qnil;
2531 }
2532
2533 /* Call function FUNC with the rest of NARGS - 1 arguments
2534 following. Return the result, or nil if something went
2535 wrong. Prevent redisplay during the evaluation. */
2536
2537 Lisp_Object
2538 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2539 {
2540 Lisp_Object val;
2541
2542 if (inhibit_eval_during_redisplay)
2543 val = Qnil;
2544 else
2545 {
2546 va_list ap;
2547 ptrdiff_t i;
2548 ptrdiff_t count = SPECPDL_INDEX ();
2549 struct gcpro gcpro1;
2550 Lisp_Object *args = alloca (nargs * word_size);
2551
2552 args[0] = func;
2553 va_start (ap, func);
2554 for (i = 1; i < nargs; i++)
2555 args[i] = va_arg (ap, Lisp_Object);
2556 va_end (ap);
2557
2558 GCPRO1 (args[0]);
2559 gcpro1.nvars = nargs;
2560 specbind (Qinhibit_redisplay, Qt);
2561 /* Use Qt to ensure debugger does not run,
2562 so there is no possibility of wanting to redisplay. */
2563 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2564 safe_eval_handler);
2565 UNGCPRO;
2566 val = unbind_to (count, val);
2567 }
2568
2569 return val;
2570 }
2571
2572
2573 /* Call function FN with one argument ARG.
2574 Return the result, or nil if something went wrong. */
2575
2576 Lisp_Object
2577 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2578 {
2579 return safe_call (2, fn, arg);
2580 }
2581
2582 static Lisp_Object Qeval;
2583
2584 Lisp_Object
2585 safe_eval (Lisp_Object sexpr)
2586 {
2587 return safe_call1 (Qeval, sexpr);
2588 }
2589
2590 /* Call function FN with two arguments ARG1 and ARG2.
2591 Return the result, or nil if something went wrong. */
2592
2593 Lisp_Object
2594 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2595 {
2596 return safe_call (3, fn, arg1, arg2);
2597 }
2598
2599
2600 \f
2601 /***********************************************************************
2602 Debugging
2603 ***********************************************************************/
2604
2605 #if 0
2606
2607 /* Define CHECK_IT to perform sanity checks on iterators.
2608 This is for debugging. It is too slow to do unconditionally. */
2609
2610 static void
2611 check_it (struct it *it)
2612 {
2613 if (it->method == GET_FROM_STRING)
2614 {
2615 eassert (STRINGP (it->string));
2616 eassert (IT_STRING_CHARPOS (*it) >= 0);
2617 }
2618 else
2619 {
2620 eassert (IT_STRING_CHARPOS (*it) < 0);
2621 if (it->method == GET_FROM_BUFFER)
2622 {
2623 /* Check that character and byte positions agree. */
2624 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2625 }
2626 }
2627
2628 if (it->dpvec)
2629 eassert (it->current.dpvec_index >= 0);
2630 else
2631 eassert (it->current.dpvec_index < 0);
2632 }
2633
2634 #define CHECK_IT(IT) check_it ((IT))
2635
2636 #else /* not 0 */
2637
2638 #define CHECK_IT(IT) (void) 0
2639
2640 #endif /* not 0 */
2641
2642
2643 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2644
2645 /* Check that the window end of window W is what we expect it
2646 to be---the last row in the current matrix displaying text. */
2647
2648 static void
2649 check_window_end (struct window *w)
2650 {
2651 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2652 {
2653 struct glyph_row *row;
2654 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2655 !row->enabled_p
2656 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2657 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2658 }
2659 }
2660
2661 #define CHECK_WINDOW_END(W) check_window_end ((W))
2662
2663 #else
2664
2665 #define CHECK_WINDOW_END(W) (void) 0
2666
2667 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2668
2669 /***********************************************************************
2670 Iterator initialization
2671 ***********************************************************************/
2672
2673 /* Initialize IT for displaying current_buffer in window W, starting
2674 at character position CHARPOS. CHARPOS < 0 means that no buffer
2675 position is specified which is useful when the iterator is assigned
2676 a position later. BYTEPOS is the byte position corresponding to
2677 CHARPOS.
2678
2679 If ROW is not null, calls to produce_glyphs with IT as parameter
2680 will produce glyphs in that row.
2681
2682 BASE_FACE_ID is the id of a base face to use. It must be one of
2683 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2684 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2685 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2686
2687 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2688 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2689 will be initialized to use the corresponding mode line glyph row of
2690 the desired matrix of W. */
2691
2692 void
2693 init_iterator (struct it *it, struct window *w,
2694 ptrdiff_t charpos, ptrdiff_t bytepos,
2695 struct glyph_row *row, enum face_id base_face_id)
2696 {
2697 enum face_id remapped_base_face_id = base_face_id;
2698
2699 /* Some precondition checks. */
2700 eassert (w != NULL && it != NULL);
2701 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2702 && charpos <= ZV));
2703
2704 /* If face attributes have been changed since the last redisplay,
2705 free realized faces now because they depend on face definitions
2706 that might have changed. Don't free faces while there might be
2707 desired matrices pending which reference these faces. */
2708 if (face_change_count && !inhibit_free_realized_faces)
2709 {
2710 face_change_count = 0;
2711 free_all_realized_faces (Qnil);
2712 }
2713
2714 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2715 if (! NILP (Vface_remapping_alist))
2716 remapped_base_face_id
2717 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2718
2719 /* Use one of the mode line rows of W's desired matrix if
2720 appropriate. */
2721 if (row == NULL)
2722 {
2723 if (base_face_id == MODE_LINE_FACE_ID
2724 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2725 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2726 else if (base_face_id == HEADER_LINE_FACE_ID)
2727 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2728 }
2729
2730 /* Clear IT. */
2731 memset (it, 0, sizeof *it);
2732 it->current.overlay_string_index = -1;
2733 it->current.dpvec_index = -1;
2734 it->base_face_id = remapped_base_face_id;
2735 it->string = Qnil;
2736 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2737 it->paragraph_embedding = L2R;
2738 it->bidi_it.string.lstring = Qnil;
2739 it->bidi_it.string.s = NULL;
2740 it->bidi_it.string.bufpos = 0;
2741 it->bidi_it.w = w;
2742
2743 /* The window in which we iterate over current_buffer: */
2744 XSETWINDOW (it->window, w);
2745 it->w = w;
2746 it->f = XFRAME (w->frame);
2747
2748 it->cmp_it.id = -1;
2749
2750 /* Extra space between lines (on window systems only). */
2751 if (base_face_id == DEFAULT_FACE_ID
2752 && FRAME_WINDOW_P (it->f))
2753 {
2754 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2755 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2756 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2757 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2758 * FRAME_LINE_HEIGHT (it->f));
2759 else if (it->f->extra_line_spacing > 0)
2760 it->extra_line_spacing = it->f->extra_line_spacing;
2761 it->max_extra_line_spacing = 0;
2762 }
2763
2764 /* If realized faces have been removed, e.g. because of face
2765 attribute changes of named faces, recompute them. When running
2766 in batch mode, the face cache of the initial frame is null. If
2767 we happen to get called, make a dummy face cache. */
2768 if (FRAME_FACE_CACHE (it->f) == NULL)
2769 init_frame_faces (it->f);
2770 if (FRAME_FACE_CACHE (it->f)->used == 0)
2771 recompute_basic_faces (it->f);
2772
2773 /* Current value of the `slice', `space-width', and 'height' properties. */
2774 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2775 it->space_width = Qnil;
2776 it->font_height = Qnil;
2777 it->override_ascent = -1;
2778
2779 /* Are control characters displayed as `^C'? */
2780 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2781
2782 /* -1 means everything between a CR and the following line end
2783 is invisible. >0 means lines indented more than this value are
2784 invisible. */
2785 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2786 ? (clip_to_bounds
2787 (-1, XINT (BVAR (current_buffer, selective_display)),
2788 PTRDIFF_MAX))
2789 : (!NILP (BVAR (current_buffer, selective_display))
2790 ? -1 : 0));
2791 it->selective_display_ellipsis_p
2792 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2793
2794 /* Display table to use. */
2795 it->dp = window_display_table (w);
2796
2797 /* Are multibyte characters enabled in current_buffer? */
2798 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2799
2800 /* Get the position at which the redisplay_end_trigger hook should
2801 be run, if it is to be run at all. */
2802 if (MARKERP (w->redisplay_end_trigger)
2803 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2804 it->redisplay_end_trigger_charpos
2805 = marker_position (w->redisplay_end_trigger);
2806 else if (INTEGERP (w->redisplay_end_trigger))
2807 it->redisplay_end_trigger_charpos =
2808 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2809
2810 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2811
2812 /* Are lines in the display truncated? */
2813 if (base_face_id != DEFAULT_FACE_ID
2814 || it->w->hscroll
2815 || (! WINDOW_FULL_WIDTH_P (it->w)
2816 && ((!NILP (Vtruncate_partial_width_windows)
2817 && !INTEGERP (Vtruncate_partial_width_windows))
2818 || (INTEGERP (Vtruncate_partial_width_windows)
2819 /* PXW: Shall we do something about this? */
2820 && (WINDOW_TOTAL_COLS (it->w)
2821 < XINT (Vtruncate_partial_width_windows))))))
2822 it->line_wrap = TRUNCATE;
2823 else if (NILP (BVAR (current_buffer, truncate_lines)))
2824 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2825 ? WINDOW_WRAP : WORD_WRAP;
2826 else
2827 it->line_wrap = TRUNCATE;
2828
2829 /* Get dimensions of truncation and continuation glyphs. These are
2830 displayed as fringe bitmaps under X, but we need them for such
2831 frames when the fringes are turned off. But leave the dimensions
2832 zero for tooltip frames, as these glyphs look ugly there and also
2833 sabotage calculations of tooltip dimensions in x-show-tip. */
2834 #ifdef HAVE_WINDOW_SYSTEM
2835 if (!(FRAME_WINDOW_P (it->f)
2836 && FRAMEP (tip_frame)
2837 && it->f == XFRAME (tip_frame)))
2838 #endif
2839 {
2840 if (it->line_wrap == TRUNCATE)
2841 {
2842 /* We will need the truncation glyph. */
2843 eassert (it->glyph_row == NULL);
2844 produce_special_glyphs (it, IT_TRUNCATION);
2845 it->truncation_pixel_width = it->pixel_width;
2846 }
2847 else
2848 {
2849 /* We will need the continuation glyph. */
2850 eassert (it->glyph_row == NULL);
2851 produce_special_glyphs (it, IT_CONTINUATION);
2852 it->continuation_pixel_width = it->pixel_width;
2853 }
2854 }
2855
2856 /* Reset these values to zero because the produce_special_glyphs
2857 above has changed them. */
2858 it->pixel_width = it->ascent = it->descent = 0;
2859 it->phys_ascent = it->phys_descent = 0;
2860
2861 /* Set this after getting the dimensions of truncation and
2862 continuation glyphs, so that we don't produce glyphs when calling
2863 produce_special_glyphs, above. */
2864 it->glyph_row = row;
2865 it->area = TEXT_AREA;
2866
2867 /* Forget any previous info about this row being reversed. */
2868 if (it->glyph_row)
2869 it->glyph_row->reversed_p = 0;
2870
2871 /* Get the dimensions of the display area. The display area
2872 consists of the visible window area plus a horizontally scrolled
2873 part to the left of the window. All x-values are relative to the
2874 start of this total display area. */
2875 if (base_face_id != DEFAULT_FACE_ID)
2876 {
2877 /* Mode lines, menu bar in terminal frames. */
2878 it->first_visible_x = 0;
2879 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2880 }
2881 else
2882 {
2883 it->first_visible_x
2884 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2885 it->last_visible_x = (it->first_visible_x
2886 + window_box_width (w, TEXT_AREA));
2887
2888 /* If we truncate lines, leave room for the truncation glyph(s) at
2889 the right margin. Otherwise, leave room for the continuation
2890 glyph(s). Done only if the window has no fringes. Since we
2891 don't know at this point whether there will be any R2L lines in
2892 the window, we reserve space for truncation/continuation glyphs
2893 even if only one of the fringes is absent. */
2894 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2895 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2896 {
2897 if (it->line_wrap == TRUNCATE)
2898 it->last_visible_x -= it->truncation_pixel_width;
2899 else
2900 it->last_visible_x -= it->continuation_pixel_width;
2901 }
2902
2903 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2904 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2905 }
2906
2907 /* Leave room for a border glyph. */
2908 if (!FRAME_WINDOW_P (it->f)
2909 && !WINDOW_RIGHTMOST_P (it->w))
2910 it->last_visible_x -= 1;
2911
2912 it->last_visible_y = window_text_bottom_y (w);
2913
2914 /* For mode lines and alike, arrange for the first glyph having a
2915 left box line if the face specifies a box. */
2916 if (base_face_id != DEFAULT_FACE_ID)
2917 {
2918 struct face *face;
2919
2920 it->face_id = remapped_base_face_id;
2921
2922 /* If we have a boxed mode line, make the first character appear
2923 with a left box line. */
2924 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2925 if (face->box != FACE_NO_BOX)
2926 it->start_of_box_run_p = true;
2927 }
2928
2929 /* If a buffer position was specified, set the iterator there,
2930 getting overlays and face properties from that position. */
2931 if (charpos >= BUF_BEG (current_buffer))
2932 {
2933 it->end_charpos = ZV;
2934 eassert (charpos == BYTE_TO_CHAR (bytepos));
2935 IT_CHARPOS (*it) = charpos;
2936 IT_BYTEPOS (*it) = bytepos;
2937
2938 /* We will rely on `reseat' to set this up properly, via
2939 handle_face_prop. */
2940 it->face_id = it->base_face_id;
2941
2942 it->start = it->current;
2943 /* Do we need to reorder bidirectional text? Not if this is a
2944 unibyte buffer: by definition, none of the single-byte
2945 characters are strong R2L, so no reordering is needed. And
2946 bidi.c doesn't support unibyte buffers anyway. Also, don't
2947 reorder while we are loading loadup.el, since the tables of
2948 character properties needed for reordering are not yet
2949 available. */
2950 it->bidi_p =
2951 NILP (Vpurify_flag)
2952 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2953 && it->multibyte_p;
2954
2955 /* If we are to reorder bidirectional text, init the bidi
2956 iterator. */
2957 if (it->bidi_p)
2958 {
2959 /* Note the paragraph direction that this buffer wants to
2960 use. */
2961 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2962 Qleft_to_right))
2963 it->paragraph_embedding = L2R;
2964 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2965 Qright_to_left))
2966 it->paragraph_embedding = R2L;
2967 else
2968 it->paragraph_embedding = NEUTRAL_DIR;
2969 bidi_unshelve_cache (NULL, 0);
2970 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2971 &it->bidi_it);
2972 }
2973
2974 /* Compute faces etc. */
2975 reseat (it, it->current.pos, 1);
2976 }
2977
2978 CHECK_IT (it);
2979 }
2980
2981
2982 /* Initialize IT for the display of window W with window start POS. */
2983
2984 void
2985 start_display (struct it *it, struct window *w, struct text_pos pos)
2986 {
2987 struct glyph_row *row;
2988 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2989
2990 row = w->desired_matrix->rows + first_vpos;
2991 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2992 it->first_vpos = first_vpos;
2993
2994 /* Don't reseat to previous visible line start if current start
2995 position is in a string or image. */
2996 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2997 {
2998 int start_at_line_beg_p;
2999 int first_y = it->current_y;
3000
3001 /* If window start is not at a line start, skip forward to POS to
3002 get the correct continuation lines width. */
3003 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3004 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3005 if (!start_at_line_beg_p)
3006 {
3007 int new_x;
3008
3009 reseat_at_previous_visible_line_start (it);
3010 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3011
3012 new_x = it->current_x + it->pixel_width;
3013
3014 /* If lines are continued, this line may end in the middle
3015 of a multi-glyph character (e.g. a control character
3016 displayed as \003, or in the middle of an overlay
3017 string). In this case move_it_to above will not have
3018 taken us to the start of the continuation line but to the
3019 end of the continued line. */
3020 if (it->current_x > 0
3021 && it->line_wrap != TRUNCATE /* Lines are continued. */
3022 && (/* And glyph doesn't fit on the line. */
3023 new_x > it->last_visible_x
3024 /* Or it fits exactly and we're on a window
3025 system frame. */
3026 || (new_x == it->last_visible_x
3027 && FRAME_WINDOW_P (it->f)
3028 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3029 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3030 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3031 {
3032 if ((it->current.dpvec_index >= 0
3033 || it->current.overlay_string_index >= 0)
3034 /* If we are on a newline from a display vector or
3035 overlay string, then we are already at the end of
3036 a screen line; no need to go to the next line in
3037 that case, as this line is not really continued.
3038 (If we do go to the next line, C-e will not DTRT.) */
3039 && it->c != '\n')
3040 {
3041 set_iterator_to_next (it, 1);
3042 move_it_in_display_line_to (it, -1, -1, 0);
3043 }
3044
3045 it->continuation_lines_width += it->current_x;
3046 }
3047 /* If the character at POS is displayed via a display
3048 vector, move_it_to above stops at the final glyph of
3049 IT->dpvec. To make the caller redisplay that character
3050 again (a.k.a. start at POS), we need to reset the
3051 dpvec_index to the beginning of IT->dpvec. */
3052 else if (it->current.dpvec_index >= 0)
3053 it->current.dpvec_index = 0;
3054
3055 /* We're starting a new display line, not affected by the
3056 height of the continued line, so clear the appropriate
3057 fields in the iterator structure. */
3058 it->max_ascent = it->max_descent = 0;
3059 it->max_phys_ascent = it->max_phys_descent = 0;
3060
3061 it->current_y = first_y;
3062 it->vpos = 0;
3063 it->current_x = it->hpos = 0;
3064 }
3065 }
3066 }
3067
3068
3069 /* Return 1 if POS is a position in ellipses displayed for invisible
3070 text. W is the window we display, for text property lookup. */
3071
3072 static int
3073 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3074 {
3075 Lisp_Object prop, window;
3076 int ellipses_p = 0;
3077 ptrdiff_t charpos = CHARPOS (pos->pos);
3078
3079 /* If POS specifies a position in a display vector, this might
3080 be for an ellipsis displayed for invisible text. We won't
3081 get the iterator set up for delivering that ellipsis unless
3082 we make sure that it gets aware of the invisible text. */
3083 if (pos->dpvec_index >= 0
3084 && pos->overlay_string_index < 0
3085 && CHARPOS (pos->string_pos) < 0
3086 && charpos > BEGV
3087 && (XSETWINDOW (window, w),
3088 prop = Fget_char_property (make_number (charpos),
3089 Qinvisible, window),
3090 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3091 {
3092 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3093 window);
3094 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3095 }
3096
3097 return ellipses_p;
3098 }
3099
3100
3101 /* Initialize IT for stepping through current_buffer in window W,
3102 starting at position POS that includes overlay string and display
3103 vector/ control character translation position information. Value
3104 is zero if there are overlay strings with newlines at POS. */
3105
3106 static int
3107 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3108 {
3109 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3110 int i, overlay_strings_with_newlines = 0;
3111
3112 /* If POS specifies a position in a display vector, this might
3113 be for an ellipsis displayed for invisible text. We won't
3114 get the iterator set up for delivering that ellipsis unless
3115 we make sure that it gets aware of the invisible text. */
3116 if (in_ellipses_for_invisible_text_p (pos, w))
3117 {
3118 --charpos;
3119 bytepos = 0;
3120 }
3121
3122 /* Keep in mind: the call to reseat in init_iterator skips invisible
3123 text, so we might end up at a position different from POS. This
3124 is only a problem when POS is a row start after a newline and an
3125 overlay starts there with an after-string, and the overlay has an
3126 invisible property. Since we don't skip invisible text in
3127 display_line and elsewhere immediately after consuming the
3128 newline before the row start, such a POS will not be in a string,
3129 but the call to init_iterator below will move us to the
3130 after-string. */
3131 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3132
3133 /* This only scans the current chunk -- it should scan all chunks.
3134 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3135 to 16 in 22.1 to make this a lesser problem. */
3136 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3137 {
3138 const char *s = SSDATA (it->overlay_strings[i]);
3139 const char *e = s + SBYTES (it->overlay_strings[i]);
3140
3141 while (s < e && *s != '\n')
3142 ++s;
3143
3144 if (s < e)
3145 {
3146 overlay_strings_with_newlines = 1;
3147 break;
3148 }
3149 }
3150
3151 /* If position is within an overlay string, set up IT to the right
3152 overlay string. */
3153 if (pos->overlay_string_index >= 0)
3154 {
3155 int relative_index;
3156
3157 /* If the first overlay string happens to have a `display'
3158 property for an image, the iterator will be set up for that
3159 image, and we have to undo that setup first before we can
3160 correct the overlay string index. */
3161 if (it->method == GET_FROM_IMAGE)
3162 pop_it (it);
3163
3164 /* We already have the first chunk of overlay strings in
3165 IT->overlay_strings. Load more until the one for
3166 pos->overlay_string_index is in IT->overlay_strings. */
3167 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3168 {
3169 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3170 it->current.overlay_string_index = 0;
3171 while (n--)
3172 {
3173 load_overlay_strings (it, 0);
3174 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3175 }
3176 }
3177
3178 it->current.overlay_string_index = pos->overlay_string_index;
3179 relative_index = (it->current.overlay_string_index
3180 % OVERLAY_STRING_CHUNK_SIZE);
3181 it->string = it->overlay_strings[relative_index];
3182 eassert (STRINGP (it->string));
3183 it->current.string_pos = pos->string_pos;
3184 it->method = GET_FROM_STRING;
3185 it->end_charpos = SCHARS (it->string);
3186 /* Set up the bidi iterator for this overlay string. */
3187 if (it->bidi_p)
3188 {
3189 it->bidi_it.string.lstring = it->string;
3190 it->bidi_it.string.s = NULL;
3191 it->bidi_it.string.schars = SCHARS (it->string);
3192 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3193 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3194 it->bidi_it.string.unibyte = !it->multibyte_p;
3195 it->bidi_it.w = it->w;
3196 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3197 FRAME_WINDOW_P (it->f), &it->bidi_it);
3198
3199 /* Synchronize the state of the bidi iterator with
3200 pos->string_pos. For any string position other than
3201 zero, this will be done automagically when we resume
3202 iteration over the string and get_visually_first_element
3203 is called. But if string_pos is zero, and the string is
3204 to be reordered for display, we need to resync manually,
3205 since it could be that the iteration state recorded in
3206 pos ended at string_pos of 0 moving backwards in string. */
3207 if (CHARPOS (pos->string_pos) == 0)
3208 {
3209 get_visually_first_element (it);
3210 if (IT_STRING_CHARPOS (*it) != 0)
3211 do {
3212 /* Paranoia. */
3213 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3214 bidi_move_to_visually_next (&it->bidi_it);
3215 } while (it->bidi_it.charpos != 0);
3216 }
3217 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3218 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3219 }
3220 }
3221
3222 if (CHARPOS (pos->string_pos) >= 0)
3223 {
3224 /* Recorded position is not in an overlay string, but in another
3225 string. This can only be a string from a `display' property.
3226 IT should already be filled with that string. */
3227 it->current.string_pos = pos->string_pos;
3228 eassert (STRINGP (it->string));
3229 if (it->bidi_p)
3230 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3231 FRAME_WINDOW_P (it->f), &it->bidi_it);
3232 }
3233
3234 /* Restore position in display vector translations, control
3235 character translations or ellipses. */
3236 if (pos->dpvec_index >= 0)
3237 {
3238 if (it->dpvec == NULL)
3239 get_next_display_element (it);
3240 eassert (it->dpvec && it->current.dpvec_index == 0);
3241 it->current.dpvec_index = pos->dpvec_index;
3242 }
3243
3244 CHECK_IT (it);
3245 return !overlay_strings_with_newlines;
3246 }
3247
3248
3249 /* Initialize IT for stepping through current_buffer in window W
3250 starting at ROW->start. */
3251
3252 static void
3253 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3254 {
3255 init_from_display_pos (it, w, &row->start);
3256 it->start = row->start;
3257 it->continuation_lines_width = row->continuation_lines_width;
3258 CHECK_IT (it);
3259 }
3260
3261
3262 /* Initialize IT for stepping through current_buffer in window W
3263 starting in the line following ROW, i.e. starting at ROW->end.
3264 Value is zero if there are overlay strings with newlines at ROW's
3265 end position. */
3266
3267 static int
3268 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3269 {
3270 int success = 0;
3271
3272 if (init_from_display_pos (it, w, &row->end))
3273 {
3274 if (row->continued_p)
3275 it->continuation_lines_width
3276 = row->continuation_lines_width + row->pixel_width;
3277 CHECK_IT (it);
3278 success = 1;
3279 }
3280
3281 return success;
3282 }
3283
3284
3285
3286 \f
3287 /***********************************************************************
3288 Text properties
3289 ***********************************************************************/
3290
3291 /* Called when IT reaches IT->stop_charpos. Handle text property and
3292 overlay changes. Set IT->stop_charpos to the next position where
3293 to stop. */
3294
3295 static void
3296 handle_stop (struct it *it)
3297 {
3298 enum prop_handled handled;
3299 int handle_overlay_change_p;
3300 struct props *p;
3301
3302 it->dpvec = NULL;
3303 it->current.dpvec_index = -1;
3304 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3305 it->ignore_overlay_strings_at_pos_p = 0;
3306 it->ellipsis_p = 0;
3307
3308 /* Use face of preceding text for ellipsis (if invisible) */
3309 if (it->selective_display_ellipsis_p)
3310 it->saved_face_id = it->face_id;
3311
3312 do
3313 {
3314 handled = HANDLED_NORMALLY;
3315
3316 /* Call text property handlers. */
3317 for (p = it_props; p->handler; ++p)
3318 {
3319 handled = p->handler (it);
3320
3321 if (handled == HANDLED_RECOMPUTE_PROPS)
3322 break;
3323 else if (handled == HANDLED_RETURN)
3324 {
3325 /* We still want to show before and after strings from
3326 overlays even if the actual buffer text is replaced. */
3327 if (!handle_overlay_change_p
3328 || it->sp > 1
3329 /* Don't call get_overlay_strings_1 if we already
3330 have overlay strings loaded, because doing so
3331 will load them again and push the iterator state
3332 onto the stack one more time, which is not
3333 expected by the rest of the code that processes
3334 overlay strings. */
3335 || (it->current.overlay_string_index < 0
3336 ? !get_overlay_strings_1 (it, 0, 0)
3337 : 0))
3338 {
3339 if (it->ellipsis_p)
3340 setup_for_ellipsis (it, 0);
3341 /* When handling a display spec, we might load an
3342 empty string. In that case, discard it here. We
3343 used to discard it in handle_single_display_spec,
3344 but that causes get_overlay_strings_1, above, to
3345 ignore overlay strings that we must check. */
3346 if (STRINGP (it->string) && !SCHARS (it->string))
3347 pop_it (it);
3348 return;
3349 }
3350 else if (STRINGP (it->string) && !SCHARS (it->string))
3351 pop_it (it);
3352 else
3353 {
3354 it->ignore_overlay_strings_at_pos_p = true;
3355 it->string_from_display_prop_p = 0;
3356 it->from_disp_prop_p = 0;
3357 handle_overlay_change_p = 0;
3358 }
3359 handled = HANDLED_RECOMPUTE_PROPS;
3360 break;
3361 }
3362 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3363 handle_overlay_change_p = 0;
3364 }
3365
3366 if (handled != HANDLED_RECOMPUTE_PROPS)
3367 {
3368 /* Don't check for overlay strings below when set to deliver
3369 characters from a display vector. */
3370 if (it->method == GET_FROM_DISPLAY_VECTOR)
3371 handle_overlay_change_p = 0;
3372
3373 /* Handle overlay changes.
3374 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3375 if it finds overlays. */
3376 if (handle_overlay_change_p)
3377 handled = handle_overlay_change (it);
3378 }
3379
3380 if (it->ellipsis_p)
3381 {
3382 setup_for_ellipsis (it, 0);
3383 break;
3384 }
3385 }
3386 while (handled == HANDLED_RECOMPUTE_PROPS);
3387
3388 /* Determine where to stop next. */
3389 if (handled == HANDLED_NORMALLY)
3390 compute_stop_pos (it);
3391 }
3392
3393
3394 /* Compute IT->stop_charpos from text property and overlay change
3395 information for IT's current position. */
3396
3397 static void
3398 compute_stop_pos (struct it *it)
3399 {
3400 register INTERVAL iv, next_iv;
3401 Lisp_Object object, limit, position;
3402 ptrdiff_t charpos, bytepos;
3403
3404 if (STRINGP (it->string))
3405 {
3406 /* Strings are usually short, so don't limit the search for
3407 properties. */
3408 it->stop_charpos = it->end_charpos;
3409 object = it->string;
3410 limit = Qnil;
3411 charpos = IT_STRING_CHARPOS (*it);
3412 bytepos = IT_STRING_BYTEPOS (*it);
3413 }
3414 else
3415 {
3416 ptrdiff_t pos;
3417
3418 /* If end_charpos is out of range for some reason, such as a
3419 misbehaving display function, rationalize it (Bug#5984). */
3420 if (it->end_charpos > ZV)
3421 it->end_charpos = ZV;
3422 it->stop_charpos = it->end_charpos;
3423
3424 /* If next overlay change is in front of the current stop pos
3425 (which is IT->end_charpos), stop there. Note: value of
3426 next_overlay_change is point-max if no overlay change
3427 follows. */
3428 charpos = IT_CHARPOS (*it);
3429 bytepos = IT_BYTEPOS (*it);
3430 pos = next_overlay_change (charpos);
3431 if (pos < it->stop_charpos)
3432 it->stop_charpos = pos;
3433
3434 /* Set up variables for computing the stop position from text
3435 property changes. */
3436 XSETBUFFER (object, current_buffer);
3437 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3438 }
3439
3440 /* Get the interval containing IT's position. Value is a null
3441 interval if there isn't such an interval. */
3442 position = make_number (charpos);
3443 iv = validate_interval_range (object, &position, &position, 0);
3444 if (iv)
3445 {
3446 Lisp_Object values_here[LAST_PROP_IDX];
3447 struct props *p;
3448
3449 /* Get properties here. */
3450 for (p = it_props; p->handler; ++p)
3451 values_here[p->idx] = textget (iv->plist, *p->name);
3452
3453 /* Look for an interval following iv that has different
3454 properties. */
3455 for (next_iv = next_interval (iv);
3456 (next_iv
3457 && (NILP (limit)
3458 || XFASTINT (limit) > next_iv->position));
3459 next_iv = next_interval (next_iv))
3460 {
3461 for (p = it_props; p->handler; ++p)
3462 {
3463 Lisp_Object new_value;
3464
3465 new_value = textget (next_iv->plist, *p->name);
3466 if (!EQ (values_here[p->idx], new_value))
3467 break;
3468 }
3469
3470 if (p->handler)
3471 break;
3472 }
3473
3474 if (next_iv)
3475 {
3476 if (INTEGERP (limit)
3477 && next_iv->position >= XFASTINT (limit))
3478 /* No text property change up to limit. */
3479 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3480 else
3481 /* Text properties change in next_iv. */
3482 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3483 }
3484 }
3485
3486 if (it->cmp_it.id < 0)
3487 {
3488 ptrdiff_t stoppos = it->end_charpos;
3489
3490 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3491 stoppos = -1;
3492 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3493 stoppos, it->string);
3494 }
3495
3496 eassert (STRINGP (it->string)
3497 || (it->stop_charpos >= BEGV
3498 && it->stop_charpos >= IT_CHARPOS (*it)));
3499 }
3500
3501
3502 /* Return the position of the next overlay change after POS in
3503 current_buffer. Value is point-max if no overlay change
3504 follows. This is like `next-overlay-change' but doesn't use
3505 xmalloc. */
3506
3507 static ptrdiff_t
3508 next_overlay_change (ptrdiff_t pos)
3509 {
3510 ptrdiff_t i, noverlays;
3511 ptrdiff_t endpos;
3512 Lisp_Object *overlays;
3513
3514 /* Get all overlays at the given position. */
3515 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3516
3517 /* If any of these overlays ends before endpos,
3518 use its ending point instead. */
3519 for (i = 0; i < noverlays; ++i)
3520 {
3521 Lisp_Object oend;
3522 ptrdiff_t oendpos;
3523
3524 oend = OVERLAY_END (overlays[i]);
3525 oendpos = OVERLAY_POSITION (oend);
3526 endpos = min (endpos, oendpos);
3527 }
3528
3529 return endpos;
3530 }
3531
3532 /* How many characters forward to search for a display property or
3533 display string. Searching too far forward makes the bidi display
3534 sluggish, especially in small windows. */
3535 #define MAX_DISP_SCAN 250
3536
3537 /* Return the character position of a display string at or after
3538 position specified by POSITION. If no display string exists at or
3539 after POSITION, return ZV. A display string is either an overlay
3540 with `display' property whose value is a string, or a `display'
3541 text property whose value is a string. STRING is data about the
3542 string to iterate; if STRING->lstring is nil, we are iterating a
3543 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3544 on a GUI frame. DISP_PROP is set to zero if we searched
3545 MAX_DISP_SCAN characters forward without finding any display
3546 strings, non-zero otherwise. It is set to 2 if the display string
3547 uses any kind of `(space ...)' spec that will produce a stretch of
3548 white space in the text area. */
3549 ptrdiff_t
3550 compute_display_string_pos (struct text_pos *position,
3551 struct bidi_string_data *string,
3552 struct window *w,
3553 int frame_window_p, int *disp_prop)
3554 {
3555 /* OBJECT = nil means current buffer. */
3556 Lisp_Object object, object1;
3557 Lisp_Object pos, spec, limpos;
3558 int string_p = (string && (STRINGP (string->lstring) || string->s));
3559 ptrdiff_t eob = string_p ? string->schars : ZV;
3560 ptrdiff_t begb = string_p ? 0 : BEGV;
3561 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3562 ptrdiff_t lim =
3563 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3564 struct text_pos tpos;
3565 int rv = 0;
3566
3567 if (string && STRINGP (string->lstring))
3568 object1 = object = string->lstring;
3569 else if (w && !string_p)
3570 {
3571 XSETWINDOW (object, w);
3572 object1 = Qnil;
3573 }
3574 else
3575 object1 = object = Qnil;
3576
3577 *disp_prop = 1;
3578
3579 if (charpos >= eob
3580 /* We don't support display properties whose values are strings
3581 that have display string properties. */
3582 || string->from_disp_str
3583 /* C strings cannot have display properties. */
3584 || (string->s && !STRINGP (object)))
3585 {
3586 *disp_prop = 0;
3587 return eob;
3588 }
3589
3590 /* If the character at CHARPOS is where the display string begins,
3591 return CHARPOS. */
3592 pos = make_number (charpos);
3593 if (STRINGP (object))
3594 bufpos = string->bufpos;
3595 else
3596 bufpos = charpos;
3597 tpos = *position;
3598 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3599 && (charpos <= begb
3600 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3601 object),
3602 spec))
3603 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3604 frame_window_p)))
3605 {
3606 if (rv == 2)
3607 *disp_prop = 2;
3608 return charpos;
3609 }
3610
3611 /* Look forward for the first character with a `display' property
3612 that will replace the underlying text when displayed. */
3613 limpos = make_number (lim);
3614 do {
3615 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3616 CHARPOS (tpos) = XFASTINT (pos);
3617 if (CHARPOS (tpos) >= lim)
3618 {
3619 *disp_prop = 0;
3620 break;
3621 }
3622 if (STRINGP (object))
3623 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3624 else
3625 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3626 spec = Fget_char_property (pos, Qdisplay, object);
3627 if (!STRINGP (object))
3628 bufpos = CHARPOS (tpos);
3629 } while (NILP (spec)
3630 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3631 bufpos, frame_window_p)));
3632 if (rv == 2)
3633 *disp_prop = 2;
3634
3635 return CHARPOS (tpos);
3636 }
3637
3638 /* Return the character position of the end of the display string that
3639 started at CHARPOS. If there's no display string at CHARPOS,
3640 return -1. A display string is either an overlay with `display'
3641 property whose value is a string or a `display' text property whose
3642 value is a string. */
3643 ptrdiff_t
3644 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3645 {
3646 /* OBJECT = nil means current buffer. */
3647 Lisp_Object object =
3648 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3649 Lisp_Object pos = make_number (charpos);
3650 ptrdiff_t eob =
3651 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3652
3653 if (charpos >= eob || (string->s && !STRINGP (object)))
3654 return eob;
3655
3656 /* It could happen that the display property or overlay was removed
3657 since we found it in compute_display_string_pos above. One way
3658 this can happen is if JIT font-lock was called (through
3659 handle_fontified_prop), and jit-lock-functions remove text
3660 properties or overlays from the portion of buffer that includes
3661 CHARPOS. Muse mode is known to do that, for example. In this
3662 case, we return -1 to the caller, to signal that no display
3663 string is actually present at CHARPOS. See bidi_fetch_char for
3664 how this is handled.
3665
3666 An alternative would be to never look for display properties past
3667 it->stop_charpos. But neither compute_display_string_pos nor
3668 bidi_fetch_char that calls it know or care where the next
3669 stop_charpos is. */
3670 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3671 return -1;
3672
3673 /* Look forward for the first character where the `display' property
3674 changes. */
3675 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3676
3677 return XFASTINT (pos);
3678 }
3679
3680
3681 \f
3682 /***********************************************************************
3683 Fontification
3684 ***********************************************************************/
3685
3686 /* Handle changes in the `fontified' property of the current buffer by
3687 calling hook functions from Qfontification_functions to fontify
3688 regions of text. */
3689
3690 static enum prop_handled
3691 handle_fontified_prop (struct it *it)
3692 {
3693 Lisp_Object prop, pos;
3694 enum prop_handled handled = HANDLED_NORMALLY;
3695
3696 if (!NILP (Vmemory_full))
3697 return handled;
3698
3699 /* Get the value of the `fontified' property at IT's current buffer
3700 position. (The `fontified' property doesn't have a special
3701 meaning in strings.) If the value is nil, call functions from
3702 Qfontification_functions. */
3703 if (!STRINGP (it->string)
3704 && it->s == NULL
3705 && !NILP (Vfontification_functions)
3706 && !NILP (Vrun_hooks)
3707 && (pos = make_number (IT_CHARPOS (*it)),
3708 prop = Fget_char_property (pos, Qfontified, Qnil),
3709 /* Ignore the special cased nil value always present at EOB since
3710 no amount of fontifying will be able to change it. */
3711 NILP (prop) && IT_CHARPOS (*it) < Z))
3712 {
3713 ptrdiff_t count = SPECPDL_INDEX ();
3714 Lisp_Object val;
3715 struct buffer *obuf = current_buffer;
3716 ptrdiff_t begv = BEGV, zv = ZV;
3717 bool old_clip_changed = current_buffer->clip_changed;
3718
3719 val = Vfontification_functions;
3720 specbind (Qfontification_functions, Qnil);
3721
3722 eassert (it->end_charpos == ZV);
3723
3724 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3725 safe_call1 (val, pos);
3726 else
3727 {
3728 Lisp_Object fns, fn;
3729 struct gcpro gcpro1, gcpro2;
3730
3731 fns = Qnil;
3732 GCPRO2 (val, fns);
3733
3734 for (; CONSP (val); val = XCDR (val))
3735 {
3736 fn = XCAR (val);
3737
3738 if (EQ (fn, Qt))
3739 {
3740 /* A value of t indicates this hook has a local
3741 binding; it means to run the global binding too.
3742 In a global value, t should not occur. If it
3743 does, we must ignore it to avoid an endless
3744 loop. */
3745 for (fns = Fdefault_value (Qfontification_functions);
3746 CONSP (fns);
3747 fns = XCDR (fns))
3748 {
3749 fn = XCAR (fns);
3750 if (!EQ (fn, Qt))
3751 safe_call1 (fn, pos);
3752 }
3753 }
3754 else
3755 safe_call1 (fn, pos);
3756 }
3757
3758 UNGCPRO;
3759 }
3760
3761 unbind_to (count, Qnil);
3762
3763 /* Fontification functions routinely call `save-restriction'.
3764 Normally, this tags clip_changed, which can confuse redisplay
3765 (see discussion in Bug#6671). Since we don't perform any
3766 special handling of fontification changes in the case where
3767 `save-restriction' isn't called, there's no point doing so in
3768 this case either. So, if the buffer's restrictions are
3769 actually left unchanged, reset clip_changed. */
3770 if (obuf == current_buffer)
3771 {
3772 if (begv == BEGV && zv == ZV)
3773 current_buffer->clip_changed = old_clip_changed;
3774 }
3775 /* There isn't much we can reasonably do to protect against
3776 misbehaving fontification, but here's a fig leaf. */
3777 else if (BUFFER_LIVE_P (obuf))
3778 set_buffer_internal_1 (obuf);
3779
3780 /* The fontification code may have added/removed text.
3781 It could do even a lot worse, but let's at least protect against
3782 the most obvious case where only the text past `pos' gets changed',
3783 as is/was done in grep.el where some escapes sequences are turned
3784 into face properties (bug#7876). */
3785 it->end_charpos = ZV;
3786
3787 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3788 something. This avoids an endless loop if they failed to
3789 fontify the text for which reason ever. */
3790 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3791 handled = HANDLED_RECOMPUTE_PROPS;
3792 }
3793
3794 return handled;
3795 }
3796
3797
3798 \f
3799 /***********************************************************************
3800 Faces
3801 ***********************************************************************/
3802
3803 /* Set up iterator IT from face properties at its current position.
3804 Called from handle_stop. */
3805
3806 static enum prop_handled
3807 handle_face_prop (struct it *it)
3808 {
3809 int new_face_id;
3810 ptrdiff_t next_stop;
3811
3812 if (!STRINGP (it->string))
3813 {
3814 new_face_id
3815 = face_at_buffer_position (it->w,
3816 IT_CHARPOS (*it),
3817 &next_stop,
3818 (IT_CHARPOS (*it)
3819 + TEXT_PROP_DISTANCE_LIMIT),
3820 0, it->base_face_id);
3821
3822 /* Is this a start of a run of characters with box face?
3823 Caveat: this can be called for a freshly initialized
3824 iterator; face_id is -1 in this case. We know that the new
3825 face will not change until limit, i.e. if the new face has a
3826 box, all characters up to limit will have one. But, as
3827 usual, we don't know whether limit is really the end. */
3828 if (new_face_id != it->face_id)
3829 {
3830 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3831 /* If it->face_id is -1, old_face below will be NULL, see
3832 the definition of FACE_FROM_ID. This will happen if this
3833 is the initial call that gets the face. */
3834 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3835
3836 /* If the value of face_id of the iterator is -1, we have to
3837 look in front of IT's position and see whether there is a
3838 face there that's different from new_face_id. */
3839 if (!old_face && IT_CHARPOS (*it) > BEG)
3840 {
3841 int prev_face_id = face_before_it_pos (it);
3842
3843 old_face = FACE_FROM_ID (it->f, prev_face_id);
3844 }
3845
3846 /* If the new face has a box, but the old face does not,
3847 this is the start of a run of characters with box face,
3848 i.e. this character has a shadow on the left side. */
3849 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3850 && (old_face == NULL || !old_face->box));
3851 it->face_box_p = new_face->box != FACE_NO_BOX;
3852 }
3853 }
3854 else
3855 {
3856 int base_face_id;
3857 ptrdiff_t bufpos;
3858 int i;
3859 Lisp_Object from_overlay
3860 = (it->current.overlay_string_index >= 0
3861 ? it->string_overlays[it->current.overlay_string_index
3862 % OVERLAY_STRING_CHUNK_SIZE]
3863 : Qnil);
3864
3865 /* See if we got to this string directly or indirectly from
3866 an overlay property. That includes the before-string or
3867 after-string of an overlay, strings in display properties
3868 provided by an overlay, their text properties, etc.
3869
3870 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3871 if (! NILP (from_overlay))
3872 for (i = it->sp - 1; i >= 0; i--)
3873 {
3874 if (it->stack[i].current.overlay_string_index >= 0)
3875 from_overlay
3876 = it->string_overlays[it->stack[i].current.overlay_string_index
3877 % OVERLAY_STRING_CHUNK_SIZE];
3878 else if (! NILP (it->stack[i].from_overlay))
3879 from_overlay = it->stack[i].from_overlay;
3880
3881 if (!NILP (from_overlay))
3882 break;
3883 }
3884
3885 if (! NILP (from_overlay))
3886 {
3887 bufpos = IT_CHARPOS (*it);
3888 /* For a string from an overlay, the base face depends
3889 only on text properties and ignores overlays. */
3890 base_face_id
3891 = face_for_overlay_string (it->w,
3892 IT_CHARPOS (*it),
3893 &next_stop,
3894 (IT_CHARPOS (*it)
3895 + TEXT_PROP_DISTANCE_LIMIT),
3896 0,
3897 from_overlay);
3898 }
3899 else
3900 {
3901 bufpos = 0;
3902
3903 /* For strings from a `display' property, use the face at
3904 IT's current buffer position as the base face to merge
3905 with, so that overlay strings appear in the same face as
3906 surrounding text, unless they specify their own faces.
3907 For strings from wrap-prefix and line-prefix properties,
3908 use the default face, possibly remapped via
3909 Vface_remapping_alist. */
3910 base_face_id = it->string_from_prefix_prop_p
3911 ? (!NILP (Vface_remapping_alist)
3912 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3913 : DEFAULT_FACE_ID)
3914 : underlying_face_id (it);
3915 }
3916
3917 new_face_id = face_at_string_position (it->w,
3918 it->string,
3919 IT_STRING_CHARPOS (*it),
3920 bufpos,
3921 &next_stop,
3922 base_face_id, 0);
3923
3924 /* Is this a start of a run of characters with box? Caveat:
3925 this can be called for a freshly allocated iterator; face_id
3926 is -1 is this case. We know that the new face will not
3927 change until the next check pos, i.e. if the new face has a
3928 box, all characters up to that position will have a
3929 box. But, as usual, we don't know whether that position
3930 is really the end. */
3931 if (new_face_id != it->face_id)
3932 {
3933 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3934 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3935
3936 /* If new face has a box but old face hasn't, this is the
3937 start of a run of characters with box, i.e. it has a
3938 shadow on the left side. */
3939 it->start_of_box_run_p
3940 = new_face->box && (old_face == NULL || !old_face->box);
3941 it->face_box_p = new_face->box != FACE_NO_BOX;
3942 }
3943 }
3944
3945 it->face_id = new_face_id;
3946 return HANDLED_NORMALLY;
3947 }
3948
3949
3950 /* Return the ID of the face ``underlying'' IT's current position,
3951 which is in a string. If the iterator is associated with a
3952 buffer, return the face at IT's current buffer position.
3953 Otherwise, use the iterator's base_face_id. */
3954
3955 static int
3956 underlying_face_id (struct it *it)
3957 {
3958 int face_id = it->base_face_id, i;
3959
3960 eassert (STRINGP (it->string));
3961
3962 for (i = it->sp - 1; i >= 0; --i)
3963 if (NILP (it->stack[i].string))
3964 face_id = it->stack[i].face_id;
3965
3966 return face_id;
3967 }
3968
3969
3970 /* Compute the face one character before or after the current position
3971 of IT, in the visual order. BEFORE_P non-zero means get the face
3972 in front (to the left in L2R paragraphs, to the right in R2L
3973 paragraphs) of IT's screen position. Value is the ID of the face. */
3974
3975 static int
3976 face_before_or_after_it_pos (struct it *it, int before_p)
3977 {
3978 int face_id, limit;
3979 ptrdiff_t next_check_charpos;
3980 struct it it_copy;
3981 void *it_copy_data = NULL;
3982
3983 eassert (it->s == NULL);
3984
3985 if (STRINGP (it->string))
3986 {
3987 ptrdiff_t bufpos, charpos;
3988 int base_face_id;
3989
3990 /* No face change past the end of the string (for the case
3991 we are padding with spaces). No face change before the
3992 string start. */
3993 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3994 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3995 return it->face_id;
3996
3997 if (!it->bidi_p)
3998 {
3999 /* Set charpos to the position before or after IT's current
4000 position, in the logical order, which in the non-bidi
4001 case is the same as the visual order. */
4002 if (before_p)
4003 charpos = IT_STRING_CHARPOS (*it) - 1;
4004 else if (it->what == IT_COMPOSITION)
4005 /* For composition, we must check the character after the
4006 composition. */
4007 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4008 else
4009 charpos = IT_STRING_CHARPOS (*it) + 1;
4010 }
4011 else
4012 {
4013 if (before_p)
4014 {
4015 /* With bidi iteration, the character before the current
4016 in the visual order cannot be found by simple
4017 iteration, because "reverse" reordering is not
4018 supported. Instead, we need to use the move_it_*
4019 family of functions. */
4020 /* Ignore face changes before the first visible
4021 character on this display line. */
4022 if (it->current_x <= it->first_visible_x)
4023 return it->face_id;
4024 SAVE_IT (it_copy, *it, it_copy_data);
4025 /* Implementation note: Since move_it_in_display_line
4026 works in the iterator geometry, and thinks the first
4027 character is always the leftmost, even in R2L lines,
4028 we don't need to distinguish between the R2L and L2R
4029 cases here. */
4030 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4031 it_copy.current_x - 1, MOVE_TO_X);
4032 charpos = IT_STRING_CHARPOS (it_copy);
4033 RESTORE_IT (it, it, it_copy_data);
4034 }
4035 else
4036 {
4037 /* Set charpos to the string position of the character
4038 that comes after IT's current position in the visual
4039 order. */
4040 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4041
4042 it_copy = *it;
4043 while (n--)
4044 bidi_move_to_visually_next (&it_copy.bidi_it);
4045
4046 charpos = it_copy.bidi_it.charpos;
4047 }
4048 }
4049 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4050
4051 if (it->current.overlay_string_index >= 0)
4052 bufpos = IT_CHARPOS (*it);
4053 else
4054 bufpos = 0;
4055
4056 base_face_id = underlying_face_id (it);
4057
4058 /* Get the face for ASCII, or unibyte. */
4059 face_id = face_at_string_position (it->w,
4060 it->string,
4061 charpos,
4062 bufpos,
4063 &next_check_charpos,
4064 base_face_id, 0);
4065
4066 /* Correct the face for charsets different from ASCII. Do it
4067 for the multibyte case only. The face returned above is
4068 suitable for unibyte text if IT->string is unibyte. */
4069 if (STRING_MULTIBYTE (it->string))
4070 {
4071 struct text_pos pos1 = string_pos (charpos, it->string);
4072 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4073 int c, len;
4074 struct face *face = FACE_FROM_ID (it->f, face_id);
4075
4076 c = string_char_and_length (p, &len);
4077 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4078 }
4079 }
4080 else
4081 {
4082 struct text_pos pos;
4083
4084 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4085 || (IT_CHARPOS (*it) <= BEGV && before_p))
4086 return it->face_id;
4087
4088 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4089 pos = it->current.pos;
4090
4091 if (!it->bidi_p)
4092 {
4093 if (before_p)
4094 DEC_TEXT_POS (pos, it->multibyte_p);
4095 else
4096 {
4097 if (it->what == IT_COMPOSITION)
4098 {
4099 /* For composition, we must check the position after
4100 the composition. */
4101 pos.charpos += it->cmp_it.nchars;
4102 pos.bytepos += it->len;
4103 }
4104 else
4105 INC_TEXT_POS (pos, it->multibyte_p);
4106 }
4107 }
4108 else
4109 {
4110 if (before_p)
4111 {
4112 /* With bidi iteration, the character before the current
4113 in the visual order cannot be found by simple
4114 iteration, because "reverse" reordering is not
4115 supported. Instead, we need to use the move_it_*
4116 family of functions. */
4117 /* Ignore face changes before the first visible
4118 character on this display line. */
4119 if (it->current_x <= it->first_visible_x)
4120 return it->face_id;
4121 SAVE_IT (it_copy, *it, it_copy_data);
4122 /* Implementation note: Since move_it_in_display_line
4123 works in the iterator geometry, and thinks the first
4124 character is always the leftmost, even in R2L lines,
4125 we don't need to distinguish between the R2L and L2R
4126 cases here. */
4127 move_it_in_display_line (&it_copy, ZV,
4128 it_copy.current_x - 1, MOVE_TO_X);
4129 pos = it_copy.current.pos;
4130 RESTORE_IT (it, it, it_copy_data);
4131 }
4132 else
4133 {
4134 /* Set charpos to the buffer position of the character
4135 that comes after IT's current position in the visual
4136 order. */
4137 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4138
4139 it_copy = *it;
4140 while (n--)
4141 bidi_move_to_visually_next (&it_copy.bidi_it);
4142
4143 SET_TEXT_POS (pos,
4144 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4145 }
4146 }
4147 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4148
4149 /* Determine face for CHARSET_ASCII, or unibyte. */
4150 face_id = face_at_buffer_position (it->w,
4151 CHARPOS (pos),
4152 &next_check_charpos,
4153 limit, 0, -1);
4154
4155 /* Correct the face for charsets different from ASCII. Do it
4156 for the multibyte case only. The face returned above is
4157 suitable for unibyte text if current_buffer is unibyte. */
4158 if (it->multibyte_p)
4159 {
4160 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4161 struct face *face = FACE_FROM_ID (it->f, face_id);
4162 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4163 }
4164 }
4165
4166 return face_id;
4167 }
4168
4169
4170 \f
4171 /***********************************************************************
4172 Invisible text
4173 ***********************************************************************/
4174
4175 /* Set up iterator IT from invisible properties at its current
4176 position. Called from handle_stop. */
4177
4178 static enum prop_handled
4179 handle_invisible_prop (struct it *it)
4180 {
4181 enum prop_handled handled = HANDLED_NORMALLY;
4182 int invis_p;
4183 Lisp_Object prop;
4184
4185 if (STRINGP (it->string))
4186 {
4187 Lisp_Object end_charpos, limit, charpos;
4188
4189 /* Get the value of the invisible text property at the
4190 current position. Value will be nil if there is no such
4191 property. */
4192 charpos = make_number (IT_STRING_CHARPOS (*it));
4193 prop = Fget_text_property (charpos, Qinvisible, it->string);
4194 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4195
4196 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4197 {
4198 /* Record whether we have to display an ellipsis for the
4199 invisible text. */
4200 int display_ellipsis_p = (invis_p == 2);
4201 ptrdiff_t len, endpos;
4202
4203 handled = HANDLED_RECOMPUTE_PROPS;
4204
4205 /* Get the position at which the next visible text can be
4206 found in IT->string, if any. */
4207 endpos = len = SCHARS (it->string);
4208 XSETINT (limit, len);
4209 do
4210 {
4211 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4212 it->string, limit);
4213 if (INTEGERP (end_charpos))
4214 {
4215 endpos = XFASTINT (end_charpos);
4216 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4218 if (invis_p == 2)
4219 display_ellipsis_p = true;
4220 }
4221 }
4222 while (invis_p && endpos < len);
4223
4224 if (display_ellipsis_p)
4225 it->ellipsis_p = true;
4226
4227 if (endpos < len)
4228 {
4229 /* Text at END_CHARPOS is visible. Move IT there. */
4230 struct text_pos old;
4231 ptrdiff_t oldpos;
4232
4233 old = it->current.string_pos;
4234 oldpos = CHARPOS (old);
4235 if (it->bidi_p)
4236 {
4237 if (it->bidi_it.first_elt
4238 && it->bidi_it.charpos < SCHARS (it->string))
4239 bidi_paragraph_init (it->paragraph_embedding,
4240 &it->bidi_it, 1);
4241 /* Bidi-iterate out of the invisible text. */
4242 do
4243 {
4244 bidi_move_to_visually_next (&it->bidi_it);
4245 }
4246 while (oldpos <= it->bidi_it.charpos
4247 && it->bidi_it.charpos < endpos);
4248
4249 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4251 if (IT_CHARPOS (*it) >= endpos)
4252 it->prev_stop = endpos;
4253 }
4254 else
4255 {
4256 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4257 compute_string_pos (&it->current.string_pos, old, it->string);
4258 }
4259 }
4260 else
4261 {
4262 /* The rest of the string is invisible. If this is an
4263 overlay string, proceed with the next overlay string
4264 or whatever comes and return a character from there. */
4265 if (it->current.overlay_string_index >= 0
4266 && !display_ellipsis_p)
4267 {
4268 next_overlay_string (it);
4269 /* Don't check for overlay strings when we just
4270 finished processing them. */
4271 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4272 }
4273 else
4274 {
4275 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4276 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4277 }
4278 }
4279 }
4280 }
4281 else
4282 {
4283 ptrdiff_t newpos, next_stop, start_charpos, tem;
4284 Lisp_Object pos, overlay;
4285
4286 /* First of all, is there invisible text at this position? */
4287 tem = start_charpos = IT_CHARPOS (*it);
4288 pos = make_number (tem);
4289 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4290 &overlay);
4291 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4292
4293 /* If we are on invisible text, skip over it. */
4294 if (invis_p && start_charpos < it->end_charpos)
4295 {
4296 /* Record whether we have to display an ellipsis for the
4297 invisible text. */
4298 int display_ellipsis_p = invis_p == 2;
4299
4300 handled = HANDLED_RECOMPUTE_PROPS;
4301
4302 /* Loop skipping over invisible text. The loop is left at
4303 ZV or with IT on the first char being visible again. */
4304 do
4305 {
4306 /* Try to skip some invisible text. Return value is the
4307 position reached which can be equal to where we start
4308 if there is nothing invisible there. This skips both
4309 over invisible text properties and overlays with
4310 invisible property. */
4311 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4312
4313 /* If we skipped nothing at all we weren't at invisible
4314 text in the first place. If everything to the end of
4315 the buffer was skipped, end the loop. */
4316 if (newpos == tem || newpos >= ZV)
4317 invis_p = 0;
4318 else
4319 {
4320 /* We skipped some characters but not necessarily
4321 all there are. Check if we ended up on visible
4322 text. Fget_char_property returns the property of
4323 the char before the given position, i.e. if we
4324 get invis_p = 0, this means that the char at
4325 newpos is visible. */
4326 pos = make_number (newpos);
4327 prop = Fget_char_property (pos, Qinvisible, it->window);
4328 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4329 }
4330
4331 /* If we ended up on invisible text, proceed to
4332 skip starting with next_stop. */
4333 if (invis_p)
4334 tem = next_stop;
4335
4336 /* If there are adjacent invisible texts, don't lose the
4337 second one's ellipsis. */
4338 if (invis_p == 2)
4339 display_ellipsis_p = true;
4340 }
4341 while (invis_p);
4342
4343 /* The position newpos is now either ZV or on visible text. */
4344 if (it->bidi_p)
4345 {
4346 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4347 int on_newline
4348 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4349 int after_newline
4350 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4351
4352 /* If the invisible text ends on a newline or on a
4353 character after a newline, we can avoid the costly,
4354 character by character, bidi iteration to NEWPOS, and
4355 instead simply reseat the iterator there. That's
4356 because all bidi reordering information is tossed at
4357 the newline. This is a big win for modes that hide
4358 complete lines, like Outline, Org, etc. */
4359 if (on_newline || after_newline)
4360 {
4361 struct text_pos tpos;
4362 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4363
4364 SET_TEXT_POS (tpos, newpos, bpos);
4365 reseat_1 (it, tpos, 0);
4366 /* If we reseat on a newline/ZV, we need to prep the
4367 bidi iterator for advancing to the next character
4368 after the newline/EOB, keeping the current paragraph
4369 direction (so that PRODUCE_GLYPHS does TRT wrt
4370 prepending/appending glyphs to a glyph row). */
4371 if (on_newline)
4372 {
4373 it->bidi_it.first_elt = 0;
4374 it->bidi_it.paragraph_dir = pdir;
4375 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4376 it->bidi_it.nchars = 1;
4377 it->bidi_it.ch_len = 1;
4378 }
4379 }
4380 else /* Must use the slow method. */
4381 {
4382 /* With bidi iteration, the region of invisible text
4383 could start and/or end in the middle of a
4384 non-base embedding level. Therefore, we need to
4385 skip invisible text using the bidi iterator,
4386 starting at IT's current position, until we find
4387 ourselves outside of the invisible text.
4388 Skipping invisible text _after_ bidi iteration
4389 avoids affecting the visual order of the
4390 displayed text when invisible properties are
4391 added or removed. */
4392 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4393 {
4394 /* If we were `reseat'ed to a new paragraph,
4395 determine the paragraph base direction. We
4396 need to do it now because
4397 next_element_from_buffer may not have a
4398 chance to do it, if we are going to skip any
4399 text at the beginning, which resets the
4400 FIRST_ELT flag. */
4401 bidi_paragraph_init (it->paragraph_embedding,
4402 &it->bidi_it, 1);
4403 }
4404 do
4405 {
4406 bidi_move_to_visually_next (&it->bidi_it);
4407 }
4408 while (it->stop_charpos <= it->bidi_it.charpos
4409 && it->bidi_it.charpos < newpos);
4410 IT_CHARPOS (*it) = it->bidi_it.charpos;
4411 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4412 /* If we overstepped NEWPOS, record its position in
4413 the iterator, so that we skip invisible text if
4414 later the bidi iteration lands us in the
4415 invisible region again. */
4416 if (IT_CHARPOS (*it) >= newpos)
4417 it->prev_stop = newpos;
4418 }
4419 }
4420 else
4421 {
4422 IT_CHARPOS (*it) = newpos;
4423 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4424 }
4425
4426 /* If there are before-strings at the start of invisible
4427 text, and the text is invisible because of a text
4428 property, arrange to show before-strings because 20.x did
4429 it that way. (If the text is invisible because of an
4430 overlay property instead of a text property, this is
4431 already handled in the overlay code.) */
4432 if (NILP (overlay)
4433 && get_overlay_strings (it, it->stop_charpos))
4434 {
4435 handled = HANDLED_RECOMPUTE_PROPS;
4436 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4437 }
4438 else if (display_ellipsis_p)
4439 {
4440 /* Make sure that the glyphs of the ellipsis will get
4441 correct `charpos' values. If we would not update
4442 it->position here, the glyphs would belong to the
4443 last visible character _before_ the invisible
4444 text, which confuses `set_cursor_from_row'.
4445
4446 We use the last invisible position instead of the
4447 first because this way the cursor is always drawn on
4448 the first "." of the ellipsis, whenever PT is inside
4449 the invisible text. Otherwise the cursor would be
4450 placed _after_ the ellipsis when the point is after the
4451 first invisible character. */
4452 if (!STRINGP (it->object))
4453 {
4454 it->position.charpos = newpos - 1;
4455 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4456 }
4457 it->ellipsis_p = true;
4458 /* Let the ellipsis display before
4459 considering any properties of the following char.
4460 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4461 handled = HANDLED_RETURN;
4462 }
4463 }
4464 }
4465
4466 return handled;
4467 }
4468
4469
4470 /* Make iterator IT return `...' next.
4471 Replaces LEN characters from buffer. */
4472
4473 static void
4474 setup_for_ellipsis (struct it *it, int len)
4475 {
4476 /* Use the display table definition for `...'. Invalid glyphs
4477 will be handled by the method returning elements from dpvec. */
4478 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4479 {
4480 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4481 it->dpvec = v->contents;
4482 it->dpend = v->contents + v->header.size;
4483 }
4484 else
4485 {
4486 /* Default `...'. */
4487 it->dpvec = default_invis_vector;
4488 it->dpend = default_invis_vector + 3;
4489 }
4490
4491 it->dpvec_char_len = len;
4492 it->current.dpvec_index = 0;
4493 it->dpvec_face_id = -1;
4494
4495 /* Remember the current face id in case glyphs specify faces.
4496 IT's face is restored in set_iterator_to_next.
4497 saved_face_id was set to preceding char's face in handle_stop. */
4498 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4499 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4500
4501 it->method = GET_FROM_DISPLAY_VECTOR;
4502 it->ellipsis_p = true;
4503 }
4504
4505
4506 \f
4507 /***********************************************************************
4508 'display' property
4509 ***********************************************************************/
4510
4511 /* Set up iterator IT from `display' property at its current position.
4512 Called from handle_stop.
4513 We return HANDLED_RETURN if some part of the display property
4514 overrides the display of the buffer text itself.
4515 Otherwise we return HANDLED_NORMALLY. */
4516
4517 static enum prop_handled
4518 handle_display_prop (struct it *it)
4519 {
4520 Lisp_Object propval, object, overlay;
4521 struct text_pos *position;
4522 ptrdiff_t bufpos;
4523 /* Nonzero if some property replaces the display of the text itself. */
4524 int display_replaced_p = 0;
4525
4526 if (STRINGP (it->string))
4527 {
4528 object = it->string;
4529 position = &it->current.string_pos;
4530 bufpos = CHARPOS (it->current.pos);
4531 }
4532 else
4533 {
4534 XSETWINDOW (object, it->w);
4535 position = &it->current.pos;
4536 bufpos = CHARPOS (*position);
4537 }
4538
4539 /* Reset those iterator values set from display property values. */
4540 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4541 it->space_width = Qnil;
4542 it->font_height = Qnil;
4543 it->voffset = 0;
4544
4545 /* We don't support recursive `display' properties, i.e. string
4546 values that have a string `display' property, that have a string
4547 `display' property etc. */
4548 if (!it->string_from_display_prop_p)
4549 it->area = TEXT_AREA;
4550
4551 propval = get_char_property_and_overlay (make_number (position->charpos),
4552 Qdisplay, object, &overlay);
4553 if (NILP (propval))
4554 return HANDLED_NORMALLY;
4555 /* Now OVERLAY is the overlay that gave us this property, or nil
4556 if it was a text property. */
4557
4558 if (!STRINGP (it->string))
4559 object = it->w->contents;
4560
4561 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4562 position, bufpos,
4563 FRAME_WINDOW_P (it->f));
4564
4565 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4566 }
4567
4568 /* Subroutine of handle_display_prop. Returns non-zero if the display
4569 specification in SPEC is a replacing specification, i.e. it would
4570 replace the text covered by `display' property with something else,
4571 such as an image or a display string. If SPEC includes any kind or
4572 `(space ...) specification, the value is 2; this is used by
4573 compute_display_string_pos, which see.
4574
4575 See handle_single_display_spec for documentation of arguments.
4576 frame_window_p is non-zero if the window being redisplayed is on a
4577 GUI frame; this argument is used only if IT is NULL, see below.
4578
4579 IT can be NULL, if this is called by the bidi reordering code
4580 through compute_display_string_pos, which see. In that case, this
4581 function only examines SPEC, but does not otherwise "handle" it, in
4582 the sense that it doesn't set up members of IT from the display
4583 spec. */
4584 static int
4585 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4586 Lisp_Object overlay, struct text_pos *position,
4587 ptrdiff_t bufpos, int frame_window_p)
4588 {
4589 int replacing_p = 0;
4590 int rv;
4591
4592 if (CONSP (spec)
4593 /* Simple specifications. */
4594 && !EQ (XCAR (spec), Qimage)
4595 && !EQ (XCAR (spec), Qspace)
4596 && !EQ (XCAR (spec), Qwhen)
4597 && !EQ (XCAR (spec), Qslice)
4598 && !EQ (XCAR (spec), Qspace_width)
4599 && !EQ (XCAR (spec), Qheight)
4600 && !EQ (XCAR (spec), Qraise)
4601 /* Marginal area specifications. */
4602 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4603 && !EQ (XCAR (spec), Qleft_fringe)
4604 && !EQ (XCAR (spec), Qright_fringe)
4605 && !NILP (XCAR (spec)))
4606 {
4607 for (; CONSP (spec); spec = XCDR (spec))
4608 {
4609 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4610 overlay, position, bufpos,
4611 replacing_p, frame_window_p)))
4612 {
4613 replacing_p = rv;
4614 /* If some text in a string is replaced, `position' no
4615 longer points to the position of `object'. */
4616 if (!it || STRINGP (object))
4617 break;
4618 }
4619 }
4620 }
4621 else if (VECTORP (spec))
4622 {
4623 ptrdiff_t i;
4624 for (i = 0; i < ASIZE (spec); ++i)
4625 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4626 overlay, position, bufpos,
4627 replacing_p, frame_window_p)))
4628 {
4629 replacing_p = rv;
4630 /* If some text in a string is replaced, `position' no
4631 longer points to the position of `object'. */
4632 if (!it || STRINGP (object))
4633 break;
4634 }
4635 }
4636 else
4637 {
4638 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4639 position, bufpos, 0,
4640 frame_window_p)))
4641 replacing_p = rv;
4642 }
4643
4644 return replacing_p;
4645 }
4646
4647 /* Value is the position of the end of the `display' property starting
4648 at START_POS in OBJECT. */
4649
4650 static struct text_pos
4651 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4652 {
4653 Lisp_Object end;
4654 struct text_pos end_pos;
4655
4656 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4657 Qdisplay, object, Qnil);
4658 CHARPOS (end_pos) = XFASTINT (end);
4659 if (STRINGP (object))
4660 compute_string_pos (&end_pos, start_pos, it->string);
4661 else
4662 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4663
4664 return end_pos;
4665 }
4666
4667
4668 /* Set up IT from a single `display' property specification SPEC. OBJECT
4669 is the object in which the `display' property was found. *POSITION
4670 is the position in OBJECT at which the `display' property was found.
4671 BUFPOS is the buffer position of OBJECT (different from POSITION if
4672 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4673 previously saw a display specification which already replaced text
4674 display with something else, for example an image; we ignore such
4675 properties after the first one has been processed.
4676
4677 OVERLAY is the overlay this `display' property came from,
4678 or nil if it was a text property.
4679
4680 If SPEC is a `space' or `image' specification, and in some other
4681 cases too, set *POSITION to the position where the `display'
4682 property ends.
4683
4684 If IT is NULL, only examine the property specification in SPEC, but
4685 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4686 is intended to be displayed in a window on a GUI frame.
4687
4688 Value is non-zero if something was found which replaces the display
4689 of buffer or string text. */
4690
4691 static int
4692 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4693 Lisp_Object overlay, struct text_pos *position,
4694 ptrdiff_t bufpos, int display_replaced_p,
4695 int frame_window_p)
4696 {
4697 Lisp_Object form;
4698 Lisp_Object location, value;
4699 struct text_pos start_pos = *position;
4700 int valid_p;
4701
4702 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4703 If the result is non-nil, use VALUE instead of SPEC. */
4704 form = Qt;
4705 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4706 {
4707 spec = XCDR (spec);
4708 if (!CONSP (spec))
4709 return 0;
4710 form = XCAR (spec);
4711 spec = XCDR (spec);
4712 }
4713
4714 if (!NILP (form) && !EQ (form, Qt))
4715 {
4716 ptrdiff_t count = SPECPDL_INDEX ();
4717 struct gcpro gcpro1;
4718
4719 /* Bind `object' to the object having the `display' property, a
4720 buffer or string. Bind `position' to the position in the
4721 object where the property was found, and `buffer-position'
4722 to the current position in the buffer. */
4723
4724 if (NILP (object))
4725 XSETBUFFER (object, current_buffer);
4726 specbind (Qobject, object);
4727 specbind (Qposition, make_number (CHARPOS (*position)));
4728 specbind (Qbuffer_position, make_number (bufpos));
4729 GCPRO1 (form);
4730 form = safe_eval (form);
4731 UNGCPRO;
4732 unbind_to (count, Qnil);
4733 }
4734
4735 if (NILP (form))
4736 return 0;
4737
4738 /* Handle `(height HEIGHT)' specifications. */
4739 if (CONSP (spec)
4740 && EQ (XCAR (spec), Qheight)
4741 && CONSP (XCDR (spec)))
4742 {
4743 if (it)
4744 {
4745 if (!FRAME_WINDOW_P (it->f))
4746 return 0;
4747
4748 it->font_height = XCAR (XCDR (spec));
4749 if (!NILP (it->font_height))
4750 {
4751 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4752 int new_height = -1;
4753
4754 if (CONSP (it->font_height)
4755 && (EQ (XCAR (it->font_height), Qplus)
4756 || EQ (XCAR (it->font_height), Qminus))
4757 && CONSP (XCDR (it->font_height))
4758 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4759 {
4760 /* `(+ N)' or `(- N)' where N is an integer. */
4761 int steps = XINT (XCAR (XCDR (it->font_height)));
4762 if (EQ (XCAR (it->font_height), Qplus))
4763 steps = - steps;
4764 it->face_id = smaller_face (it->f, it->face_id, steps);
4765 }
4766 else if (FUNCTIONP (it->font_height))
4767 {
4768 /* Call function with current height as argument.
4769 Value is the new height. */
4770 Lisp_Object height;
4771 height = safe_call1 (it->font_height,
4772 face->lface[LFACE_HEIGHT_INDEX]);
4773 if (NUMBERP (height))
4774 new_height = XFLOATINT (height);
4775 }
4776 else if (NUMBERP (it->font_height))
4777 {
4778 /* Value is a multiple of the canonical char height. */
4779 struct face *f;
4780
4781 f = FACE_FROM_ID (it->f,
4782 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4783 new_height = (XFLOATINT (it->font_height)
4784 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4785 }
4786 else
4787 {
4788 /* Evaluate IT->font_height with `height' bound to the
4789 current specified height to get the new height. */
4790 ptrdiff_t count = SPECPDL_INDEX ();
4791
4792 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4793 value = safe_eval (it->font_height);
4794 unbind_to (count, Qnil);
4795
4796 if (NUMBERP (value))
4797 new_height = XFLOATINT (value);
4798 }
4799
4800 if (new_height > 0)
4801 it->face_id = face_with_height (it->f, it->face_id, new_height);
4802 }
4803 }
4804
4805 return 0;
4806 }
4807
4808 /* Handle `(space-width WIDTH)'. */
4809 if (CONSP (spec)
4810 && EQ (XCAR (spec), Qspace_width)
4811 && CONSP (XCDR (spec)))
4812 {
4813 if (it)
4814 {
4815 if (!FRAME_WINDOW_P (it->f))
4816 return 0;
4817
4818 value = XCAR (XCDR (spec));
4819 if (NUMBERP (value) && XFLOATINT (value) > 0)
4820 it->space_width = value;
4821 }
4822
4823 return 0;
4824 }
4825
4826 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4827 if (CONSP (spec)
4828 && EQ (XCAR (spec), Qslice))
4829 {
4830 Lisp_Object tem;
4831
4832 if (it)
4833 {
4834 if (!FRAME_WINDOW_P (it->f))
4835 return 0;
4836
4837 if (tem = XCDR (spec), CONSP (tem))
4838 {
4839 it->slice.x = XCAR (tem);
4840 if (tem = XCDR (tem), CONSP (tem))
4841 {
4842 it->slice.y = XCAR (tem);
4843 if (tem = XCDR (tem), CONSP (tem))
4844 {
4845 it->slice.width = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4847 it->slice.height = XCAR (tem);
4848 }
4849 }
4850 }
4851 }
4852
4853 return 0;
4854 }
4855
4856 /* Handle `(raise FACTOR)'. */
4857 if (CONSP (spec)
4858 && EQ (XCAR (spec), Qraise)
4859 && CONSP (XCDR (spec)))
4860 {
4861 if (it)
4862 {
4863 if (!FRAME_WINDOW_P (it->f))
4864 return 0;
4865
4866 #ifdef HAVE_WINDOW_SYSTEM
4867 value = XCAR (XCDR (spec));
4868 if (NUMBERP (value))
4869 {
4870 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4871 it->voffset = - (XFLOATINT (value)
4872 * (FONT_HEIGHT (face->font)));
4873 }
4874 #endif /* HAVE_WINDOW_SYSTEM */
4875 }
4876
4877 return 0;
4878 }
4879
4880 /* Don't handle the other kinds of display specifications
4881 inside a string that we got from a `display' property. */
4882 if (it && it->string_from_display_prop_p)
4883 return 0;
4884
4885 /* Characters having this form of property are not displayed, so
4886 we have to find the end of the property. */
4887 if (it)
4888 {
4889 start_pos = *position;
4890 *position = display_prop_end (it, object, start_pos);
4891 }
4892 value = Qnil;
4893
4894 /* Stop the scan at that end position--we assume that all
4895 text properties change there. */
4896 if (it)
4897 it->stop_charpos = position->charpos;
4898
4899 /* Handle `(left-fringe BITMAP [FACE])'
4900 and `(right-fringe BITMAP [FACE])'. */
4901 if (CONSP (spec)
4902 && (EQ (XCAR (spec), Qleft_fringe)
4903 || EQ (XCAR (spec), Qright_fringe))
4904 && CONSP (XCDR (spec)))
4905 {
4906 int fringe_bitmap;
4907
4908 if (it)
4909 {
4910 if (!FRAME_WINDOW_P (it->f))
4911 /* If we return here, POSITION has been advanced
4912 across the text with this property. */
4913 {
4914 /* Synchronize the bidi iterator with POSITION. This is
4915 needed because we are not going to push the iterator
4916 on behalf of this display property, so there will be
4917 no pop_it call to do this synchronization for us. */
4918 if (it->bidi_p)
4919 {
4920 it->position = *position;
4921 iterate_out_of_display_property (it);
4922 *position = it->position;
4923 }
4924 return 1;
4925 }
4926 }
4927 else if (!frame_window_p)
4928 return 1;
4929
4930 #ifdef HAVE_WINDOW_SYSTEM
4931 value = XCAR (XCDR (spec));
4932 if (!SYMBOLP (value)
4933 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4934 /* If we return here, POSITION has been advanced
4935 across the text with this property. */
4936 {
4937 if (it && it->bidi_p)
4938 {
4939 it->position = *position;
4940 iterate_out_of_display_property (it);
4941 *position = it->position;
4942 }
4943 return 1;
4944 }
4945
4946 if (it)
4947 {
4948 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4949
4950 if (CONSP (XCDR (XCDR (spec))))
4951 {
4952 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4953 int face_id2 = lookup_derived_face (it->f, face_name,
4954 FRINGE_FACE_ID, 0);
4955 if (face_id2 >= 0)
4956 face_id = face_id2;
4957 }
4958
4959 /* Save current settings of IT so that we can restore them
4960 when we are finished with the glyph property value. */
4961 push_it (it, position);
4962
4963 it->area = TEXT_AREA;
4964 it->what = IT_IMAGE;
4965 it->image_id = -1; /* no image */
4966 it->position = start_pos;
4967 it->object = NILP (object) ? it->w->contents : object;
4968 it->method = GET_FROM_IMAGE;
4969 it->from_overlay = Qnil;
4970 it->face_id = face_id;
4971 it->from_disp_prop_p = true;
4972
4973 /* Say that we haven't consumed the characters with
4974 `display' property yet. The call to pop_it in
4975 set_iterator_to_next will clean this up. */
4976 *position = start_pos;
4977
4978 if (EQ (XCAR (spec), Qleft_fringe))
4979 {
4980 it->left_user_fringe_bitmap = fringe_bitmap;
4981 it->left_user_fringe_face_id = face_id;
4982 }
4983 else
4984 {
4985 it->right_user_fringe_bitmap = fringe_bitmap;
4986 it->right_user_fringe_face_id = face_id;
4987 }
4988 }
4989 #endif /* HAVE_WINDOW_SYSTEM */
4990 return 1;
4991 }
4992
4993 /* Prepare to handle `((margin left-margin) ...)',
4994 `((margin right-margin) ...)' and `((margin nil) ...)'
4995 prefixes for display specifications. */
4996 location = Qunbound;
4997 if (CONSP (spec) && CONSP (XCAR (spec)))
4998 {
4999 Lisp_Object tem;
5000
5001 value = XCDR (spec);
5002 if (CONSP (value))
5003 value = XCAR (value);
5004
5005 tem = XCAR (spec);
5006 if (EQ (XCAR (tem), Qmargin)
5007 && (tem = XCDR (tem),
5008 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5009 (NILP (tem)
5010 || EQ (tem, Qleft_margin)
5011 || EQ (tem, Qright_margin))))
5012 location = tem;
5013 }
5014
5015 if (EQ (location, Qunbound))
5016 {
5017 location = Qnil;
5018 value = spec;
5019 }
5020
5021 /* After this point, VALUE is the property after any
5022 margin prefix has been stripped. It must be a string,
5023 an image specification, or `(space ...)'.
5024
5025 LOCATION specifies where to display: `left-margin',
5026 `right-margin' or nil. */
5027
5028 valid_p = (STRINGP (value)
5029 #ifdef HAVE_WINDOW_SYSTEM
5030 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5031 && valid_image_p (value))
5032 #endif /* not HAVE_WINDOW_SYSTEM */
5033 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5034
5035 if (valid_p && !display_replaced_p)
5036 {
5037 int retval = 1;
5038
5039 if (!it)
5040 {
5041 /* Callers need to know whether the display spec is any kind
5042 of `(space ...)' spec that is about to affect text-area
5043 display. */
5044 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5045 retval = 2;
5046 return retval;
5047 }
5048
5049 /* Save current settings of IT so that we can restore them
5050 when we are finished with the glyph property value. */
5051 push_it (it, position);
5052 it->from_overlay = overlay;
5053 it->from_disp_prop_p = true;
5054
5055 if (NILP (location))
5056 it->area = TEXT_AREA;
5057 else if (EQ (location, Qleft_margin))
5058 it->area = LEFT_MARGIN_AREA;
5059 else
5060 it->area = RIGHT_MARGIN_AREA;
5061
5062 if (STRINGP (value))
5063 {
5064 it->string = value;
5065 it->multibyte_p = STRING_MULTIBYTE (it->string);
5066 it->current.overlay_string_index = -1;
5067 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5068 it->end_charpos = it->string_nchars = SCHARS (it->string);
5069 it->method = GET_FROM_STRING;
5070 it->stop_charpos = 0;
5071 it->prev_stop = 0;
5072 it->base_level_stop = 0;
5073 it->string_from_display_prop_p = true;
5074 /* Say that we haven't consumed the characters with
5075 `display' property yet. The call to pop_it in
5076 set_iterator_to_next will clean this up. */
5077 if (BUFFERP (object))
5078 *position = start_pos;
5079
5080 /* Force paragraph direction to be that of the parent
5081 object. If the parent object's paragraph direction is
5082 not yet determined, default to L2R. */
5083 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5084 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5085 else
5086 it->paragraph_embedding = L2R;
5087
5088 /* Set up the bidi iterator for this display string. */
5089 if (it->bidi_p)
5090 {
5091 it->bidi_it.string.lstring = it->string;
5092 it->bidi_it.string.s = NULL;
5093 it->bidi_it.string.schars = it->end_charpos;
5094 it->bidi_it.string.bufpos = bufpos;
5095 it->bidi_it.string.from_disp_str = 1;
5096 it->bidi_it.string.unibyte = !it->multibyte_p;
5097 it->bidi_it.w = it->w;
5098 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5099 }
5100 }
5101 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5102 {
5103 it->method = GET_FROM_STRETCH;
5104 it->object = value;
5105 *position = it->position = start_pos;
5106 retval = 1 + (it->area == TEXT_AREA);
5107 }
5108 #ifdef HAVE_WINDOW_SYSTEM
5109 else
5110 {
5111 it->what = IT_IMAGE;
5112 it->image_id = lookup_image (it->f, value);
5113 it->position = start_pos;
5114 it->object = NILP (object) ? it->w->contents : object;
5115 it->method = GET_FROM_IMAGE;
5116
5117 /* Say that we haven't consumed the characters with
5118 `display' property yet. The call to pop_it in
5119 set_iterator_to_next will clean this up. */
5120 *position = start_pos;
5121 }
5122 #endif /* HAVE_WINDOW_SYSTEM */
5123
5124 return retval;
5125 }
5126
5127 /* Invalid property or property not supported. Restore
5128 POSITION to what it was before. */
5129 *position = start_pos;
5130 return 0;
5131 }
5132
5133 /* Check if PROP is a display property value whose text should be
5134 treated as intangible. OVERLAY is the overlay from which PROP
5135 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5136 specify the buffer position covered by PROP. */
5137
5138 int
5139 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5140 ptrdiff_t charpos, ptrdiff_t bytepos)
5141 {
5142 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5143 struct text_pos position;
5144
5145 SET_TEXT_POS (position, charpos, bytepos);
5146 return handle_display_spec (NULL, prop, Qnil, overlay,
5147 &position, charpos, frame_window_p);
5148 }
5149
5150
5151 /* Return 1 if PROP is a display sub-property value containing STRING.
5152
5153 Implementation note: this and the following function are really
5154 special cases of handle_display_spec and
5155 handle_single_display_spec, and should ideally use the same code.
5156 Until they do, these two pairs must be consistent and must be
5157 modified in sync. */
5158
5159 static int
5160 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5161 {
5162 if (EQ (string, prop))
5163 return 1;
5164
5165 /* Skip over `when FORM'. */
5166 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5167 {
5168 prop = XCDR (prop);
5169 if (!CONSP (prop))
5170 return 0;
5171 /* Actually, the condition following `when' should be eval'ed,
5172 like handle_single_display_spec does, and we should return
5173 zero if it evaluates to nil. However, this function is
5174 called only when the buffer was already displayed and some
5175 glyph in the glyph matrix was found to come from a display
5176 string. Therefore, the condition was already evaluated, and
5177 the result was non-nil, otherwise the display string wouldn't
5178 have been displayed and we would have never been called for
5179 this property. Thus, we can skip the evaluation and assume
5180 its result is non-nil. */
5181 prop = XCDR (prop);
5182 }
5183
5184 if (CONSP (prop))
5185 /* Skip over `margin LOCATION'. */
5186 if (EQ (XCAR (prop), Qmargin))
5187 {
5188 prop = XCDR (prop);
5189 if (!CONSP (prop))
5190 return 0;
5191
5192 prop = XCDR (prop);
5193 if (!CONSP (prop))
5194 return 0;
5195 }
5196
5197 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5198 }
5199
5200
5201 /* Return 1 if STRING appears in the `display' property PROP. */
5202
5203 static int
5204 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5205 {
5206 if (CONSP (prop)
5207 && !EQ (XCAR (prop), Qwhen)
5208 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5209 {
5210 /* A list of sub-properties. */
5211 while (CONSP (prop))
5212 {
5213 if (single_display_spec_string_p (XCAR (prop), string))
5214 return 1;
5215 prop = XCDR (prop);
5216 }
5217 }
5218 else if (VECTORP (prop))
5219 {
5220 /* A vector of sub-properties. */
5221 ptrdiff_t i;
5222 for (i = 0; i < ASIZE (prop); ++i)
5223 if (single_display_spec_string_p (AREF (prop, i), string))
5224 return 1;
5225 }
5226 else
5227 return single_display_spec_string_p (prop, string);
5228
5229 return 0;
5230 }
5231
5232 /* Look for STRING in overlays and text properties in the current
5233 buffer, between character positions FROM and TO (excluding TO).
5234 BACK_P non-zero means look back (in this case, TO is supposed to be
5235 less than FROM).
5236 Value is the first character position where STRING was found, or
5237 zero if it wasn't found before hitting TO.
5238
5239 This function may only use code that doesn't eval because it is
5240 called asynchronously from note_mouse_highlight. */
5241
5242 static ptrdiff_t
5243 string_buffer_position_lim (Lisp_Object string,
5244 ptrdiff_t from, ptrdiff_t to, int back_p)
5245 {
5246 Lisp_Object limit, prop, pos;
5247 int found = 0;
5248
5249 pos = make_number (max (from, BEGV));
5250
5251 if (!back_p) /* looking forward */
5252 {
5253 limit = make_number (min (to, ZV));
5254 while (!found && !EQ (pos, limit))
5255 {
5256 prop = Fget_char_property (pos, Qdisplay, Qnil);
5257 if (!NILP (prop) && display_prop_string_p (prop, string))
5258 found = 1;
5259 else
5260 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5261 limit);
5262 }
5263 }
5264 else /* looking back */
5265 {
5266 limit = make_number (max (to, BEGV));
5267 while (!found && !EQ (pos, limit))
5268 {
5269 prop = Fget_char_property (pos, Qdisplay, Qnil);
5270 if (!NILP (prop) && display_prop_string_p (prop, string))
5271 found = 1;
5272 else
5273 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5274 limit);
5275 }
5276 }
5277
5278 return found ? XINT (pos) : 0;
5279 }
5280
5281 /* Determine which buffer position in current buffer STRING comes from.
5282 AROUND_CHARPOS is an approximate position where it could come from.
5283 Value is the buffer position or 0 if it couldn't be determined.
5284
5285 This function is necessary because we don't record buffer positions
5286 in glyphs generated from strings (to keep struct glyph small).
5287 This function may only use code that doesn't eval because it is
5288 called asynchronously from note_mouse_highlight. */
5289
5290 static ptrdiff_t
5291 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5292 {
5293 const int MAX_DISTANCE = 1000;
5294 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5295 around_charpos + MAX_DISTANCE,
5296 0);
5297
5298 if (!found)
5299 found = string_buffer_position_lim (string, around_charpos,
5300 around_charpos - MAX_DISTANCE, 1);
5301 return found;
5302 }
5303
5304
5305 \f
5306 /***********************************************************************
5307 `composition' property
5308 ***********************************************************************/
5309
5310 /* Set up iterator IT from `composition' property at its current
5311 position. Called from handle_stop. */
5312
5313 static enum prop_handled
5314 handle_composition_prop (struct it *it)
5315 {
5316 Lisp_Object prop, string;
5317 ptrdiff_t pos, pos_byte, start, end;
5318
5319 if (STRINGP (it->string))
5320 {
5321 unsigned char *s;
5322
5323 pos = IT_STRING_CHARPOS (*it);
5324 pos_byte = IT_STRING_BYTEPOS (*it);
5325 string = it->string;
5326 s = SDATA (string) + pos_byte;
5327 it->c = STRING_CHAR (s);
5328 }
5329 else
5330 {
5331 pos = IT_CHARPOS (*it);
5332 pos_byte = IT_BYTEPOS (*it);
5333 string = Qnil;
5334 it->c = FETCH_CHAR (pos_byte);
5335 }
5336
5337 /* If there's a valid composition and point is not inside of the
5338 composition (in the case that the composition is from the current
5339 buffer), draw a glyph composed from the composition components. */
5340 if (find_composition (pos, -1, &start, &end, &prop, string)
5341 && composition_valid_p (start, end, prop)
5342 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5343 {
5344 if (start < pos)
5345 /* As we can't handle this situation (perhaps font-lock added
5346 a new composition), we just return here hoping that next
5347 redisplay will detect this composition much earlier. */
5348 return HANDLED_NORMALLY;
5349 if (start != pos)
5350 {
5351 if (STRINGP (it->string))
5352 pos_byte = string_char_to_byte (it->string, start);
5353 else
5354 pos_byte = CHAR_TO_BYTE (start);
5355 }
5356 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5357 prop, string);
5358
5359 if (it->cmp_it.id >= 0)
5360 {
5361 it->cmp_it.ch = -1;
5362 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5363 it->cmp_it.nglyphs = -1;
5364 }
5365 }
5366
5367 return HANDLED_NORMALLY;
5368 }
5369
5370
5371 \f
5372 /***********************************************************************
5373 Overlay strings
5374 ***********************************************************************/
5375
5376 /* The following structure is used to record overlay strings for
5377 later sorting in load_overlay_strings. */
5378
5379 struct overlay_entry
5380 {
5381 Lisp_Object overlay;
5382 Lisp_Object string;
5383 EMACS_INT priority;
5384 int after_string_p;
5385 };
5386
5387
5388 /* Set up iterator IT from overlay strings at its current position.
5389 Called from handle_stop. */
5390
5391 static enum prop_handled
5392 handle_overlay_change (struct it *it)
5393 {
5394 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5395 return HANDLED_RECOMPUTE_PROPS;
5396 else
5397 return HANDLED_NORMALLY;
5398 }
5399
5400
5401 /* Set up the next overlay string for delivery by IT, if there is an
5402 overlay string to deliver. Called by set_iterator_to_next when the
5403 end of the current overlay string is reached. If there are more
5404 overlay strings to display, IT->string and
5405 IT->current.overlay_string_index are set appropriately here.
5406 Otherwise IT->string is set to nil. */
5407
5408 static void
5409 next_overlay_string (struct it *it)
5410 {
5411 ++it->current.overlay_string_index;
5412 if (it->current.overlay_string_index == it->n_overlay_strings)
5413 {
5414 /* No more overlay strings. Restore IT's settings to what
5415 they were before overlay strings were processed, and
5416 continue to deliver from current_buffer. */
5417
5418 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5419 pop_it (it);
5420 eassert (it->sp > 0
5421 || (NILP (it->string)
5422 && it->method == GET_FROM_BUFFER
5423 && it->stop_charpos >= BEGV
5424 && it->stop_charpos <= it->end_charpos));
5425 it->current.overlay_string_index = -1;
5426 it->n_overlay_strings = 0;
5427 it->overlay_strings_charpos = -1;
5428 /* If there's an empty display string on the stack, pop the
5429 stack, to resync the bidi iterator with IT's position. Such
5430 empty strings are pushed onto the stack in
5431 get_overlay_strings_1. */
5432 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5433 pop_it (it);
5434
5435 /* If we're at the end of the buffer, record that we have
5436 processed the overlay strings there already, so that
5437 next_element_from_buffer doesn't try it again. */
5438 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5439 it->overlay_strings_at_end_processed_p = true;
5440 }
5441 else
5442 {
5443 /* There are more overlay strings to process. If
5444 IT->current.overlay_string_index has advanced to a position
5445 where we must load IT->overlay_strings with more strings, do
5446 it. We must load at the IT->overlay_strings_charpos where
5447 IT->n_overlay_strings was originally computed; when invisible
5448 text is present, this might not be IT_CHARPOS (Bug#7016). */
5449 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5450
5451 if (it->current.overlay_string_index && i == 0)
5452 load_overlay_strings (it, it->overlay_strings_charpos);
5453
5454 /* Initialize IT to deliver display elements from the overlay
5455 string. */
5456 it->string = it->overlay_strings[i];
5457 it->multibyte_p = STRING_MULTIBYTE (it->string);
5458 SET_TEXT_POS (it->current.string_pos, 0, 0);
5459 it->method = GET_FROM_STRING;
5460 it->stop_charpos = 0;
5461 it->end_charpos = SCHARS (it->string);
5462 if (it->cmp_it.stop_pos >= 0)
5463 it->cmp_it.stop_pos = 0;
5464 it->prev_stop = 0;
5465 it->base_level_stop = 0;
5466
5467 /* Set up the bidi iterator for this overlay string. */
5468 if (it->bidi_p)
5469 {
5470 it->bidi_it.string.lstring = it->string;
5471 it->bidi_it.string.s = NULL;
5472 it->bidi_it.string.schars = SCHARS (it->string);
5473 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5474 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5475 it->bidi_it.string.unibyte = !it->multibyte_p;
5476 it->bidi_it.w = it->w;
5477 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5478 }
5479 }
5480
5481 CHECK_IT (it);
5482 }
5483
5484
5485 /* Compare two overlay_entry structures E1 and E2. Used as a
5486 comparison function for qsort in load_overlay_strings. Overlay
5487 strings for the same position are sorted so that
5488
5489 1. All after-strings come in front of before-strings, except
5490 when they come from the same overlay.
5491
5492 2. Within after-strings, strings are sorted so that overlay strings
5493 from overlays with higher priorities come first.
5494
5495 2. Within before-strings, strings are sorted so that overlay
5496 strings from overlays with higher priorities come last.
5497
5498 Value is analogous to strcmp. */
5499
5500
5501 static int
5502 compare_overlay_entries (const void *e1, const void *e2)
5503 {
5504 struct overlay_entry const *entry1 = e1;
5505 struct overlay_entry const *entry2 = e2;
5506 int result;
5507
5508 if (entry1->after_string_p != entry2->after_string_p)
5509 {
5510 /* Let after-strings appear in front of before-strings if
5511 they come from different overlays. */
5512 if (EQ (entry1->overlay, entry2->overlay))
5513 result = entry1->after_string_p ? 1 : -1;
5514 else
5515 result = entry1->after_string_p ? -1 : 1;
5516 }
5517 else if (entry1->priority != entry2->priority)
5518 {
5519 if (entry1->after_string_p)
5520 /* After-strings sorted in order of decreasing priority. */
5521 result = entry2->priority < entry1->priority ? -1 : 1;
5522 else
5523 /* Before-strings sorted in order of increasing priority. */
5524 result = entry1->priority < entry2->priority ? -1 : 1;
5525 }
5526 else
5527 result = 0;
5528
5529 return result;
5530 }
5531
5532
5533 /* Load the vector IT->overlay_strings with overlay strings from IT's
5534 current buffer position, or from CHARPOS if that is > 0. Set
5535 IT->n_overlays to the total number of overlay strings found.
5536
5537 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5538 a time. On entry into load_overlay_strings,
5539 IT->current.overlay_string_index gives the number of overlay
5540 strings that have already been loaded by previous calls to this
5541 function.
5542
5543 IT->add_overlay_start contains an additional overlay start
5544 position to consider for taking overlay strings from, if non-zero.
5545 This position comes into play when the overlay has an `invisible'
5546 property, and both before and after-strings. When we've skipped to
5547 the end of the overlay, because of its `invisible' property, we
5548 nevertheless want its before-string to appear.
5549 IT->add_overlay_start will contain the overlay start position
5550 in this case.
5551
5552 Overlay strings are sorted so that after-string strings come in
5553 front of before-string strings. Within before and after-strings,
5554 strings are sorted by overlay priority. See also function
5555 compare_overlay_entries. */
5556
5557 static void
5558 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5559 {
5560 Lisp_Object overlay, window, str, invisible;
5561 struct Lisp_Overlay *ov;
5562 ptrdiff_t start, end;
5563 ptrdiff_t size = 20;
5564 ptrdiff_t n = 0, i, j;
5565 int invis_p;
5566 struct overlay_entry *entries = alloca (size * sizeof *entries);
5567 USE_SAFE_ALLOCA;
5568
5569 if (charpos <= 0)
5570 charpos = IT_CHARPOS (*it);
5571
5572 /* Append the overlay string STRING of overlay OVERLAY to vector
5573 `entries' which has size `size' and currently contains `n'
5574 elements. AFTER_P non-zero means STRING is an after-string of
5575 OVERLAY. */
5576 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5577 do \
5578 { \
5579 Lisp_Object priority; \
5580 \
5581 if (n == size) \
5582 { \
5583 struct overlay_entry *old = entries; \
5584 SAFE_NALLOCA (entries, 2, size); \
5585 memcpy (entries, old, size * sizeof *entries); \
5586 size *= 2; \
5587 } \
5588 \
5589 entries[n].string = (STRING); \
5590 entries[n].overlay = (OVERLAY); \
5591 priority = Foverlay_get ((OVERLAY), Qpriority); \
5592 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5593 entries[n].after_string_p = (AFTER_P); \
5594 ++n; \
5595 } \
5596 while (0)
5597
5598 /* Process overlay before the overlay center. */
5599 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5600 {
5601 XSETMISC (overlay, ov);
5602 eassert (OVERLAYP (overlay));
5603 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5604 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5605
5606 if (end < charpos)
5607 break;
5608
5609 /* Skip this overlay if it doesn't start or end at IT's current
5610 position. */
5611 if (end != charpos && start != charpos)
5612 continue;
5613
5614 /* Skip this overlay if it doesn't apply to IT->w. */
5615 window = Foverlay_get (overlay, Qwindow);
5616 if (WINDOWP (window) && XWINDOW (window) != it->w)
5617 continue;
5618
5619 /* If the text ``under'' the overlay is invisible, both before-
5620 and after-strings from this overlay are visible; start and
5621 end position are indistinguishable. */
5622 invisible = Foverlay_get (overlay, Qinvisible);
5623 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5624
5625 /* If overlay has a non-empty before-string, record it. */
5626 if ((start == charpos || (end == charpos && invis_p))
5627 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5628 && SCHARS (str))
5629 RECORD_OVERLAY_STRING (overlay, str, 0);
5630
5631 /* If overlay has a non-empty after-string, record it. */
5632 if ((end == charpos || (start == charpos && invis_p))
5633 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5634 && SCHARS (str))
5635 RECORD_OVERLAY_STRING (overlay, str, 1);
5636 }
5637
5638 /* Process overlays after the overlay center. */
5639 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5640 {
5641 XSETMISC (overlay, ov);
5642 eassert (OVERLAYP (overlay));
5643 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5644 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5645
5646 if (start > charpos)
5647 break;
5648
5649 /* Skip this overlay if it doesn't start or end at IT's current
5650 position. */
5651 if (end != charpos && start != charpos)
5652 continue;
5653
5654 /* Skip this overlay if it doesn't apply to IT->w. */
5655 window = Foverlay_get (overlay, Qwindow);
5656 if (WINDOWP (window) && XWINDOW (window) != it->w)
5657 continue;
5658
5659 /* If the text ``under'' the overlay is invisible, it has a zero
5660 dimension, and both before- and after-strings apply. */
5661 invisible = Foverlay_get (overlay, Qinvisible);
5662 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5663
5664 /* If overlay has a non-empty before-string, record it. */
5665 if ((start == charpos || (end == charpos && invis_p))
5666 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5667 && SCHARS (str))
5668 RECORD_OVERLAY_STRING (overlay, str, 0);
5669
5670 /* If overlay has a non-empty after-string, record it. */
5671 if ((end == charpos || (start == charpos && invis_p))
5672 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5673 && SCHARS (str))
5674 RECORD_OVERLAY_STRING (overlay, str, 1);
5675 }
5676
5677 #undef RECORD_OVERLAY_STRING
5678
5679 /* Sort entries. */
5680 if (n > 1)
5681 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5682
5683 /* Record number of overlay strings, and where we computed it. */
5684 it->n_overlay_strings = n;
5685 it->overlay_strings_charpos = charpos;
5686
5687 /* IT->current.overlay_string_index is the number of overlay strings
5688 that have already been consumed by IT. Copy some of the
5689 remaining overlay strings to IT->overlay_strings. */
5690 i = 0;
5691 j = it->current.overlay_string_index;
5692 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5693 {
5694 it->overlay_strings[i] = entries[j].string;
5695 it->string_overlays[i++] = entries[j++].overlay;
5696 }
5697
5698 CHECK_IT (it);
5699 SAFE_FREE ();
5700 }
5701
5702
5703 /* Get the first chunk of overlay strings at IT's current buffer
5704 position, or at CHARPOS if that is > 0. Value is non-zero if at
5705 least one overlay string was found. */
5706
5707 static int
5708 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5709 {
5710 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5711 process. This fills IT->overlay_strings with strings, and sets
5712 IT->n_overlay_strings to the total number of strings to process.
5713 IT->pos.overlay_string_index has to be set temporarily to zero
5714 because load_overlay_strings needs this; it must be set to -1
5715 when no overlay strings are found because a zero value would
5716 indicate a position in the first overlay string. */
5717 it->current.overlay_string_index = 0;
5718 load_overlay_strings (it, charpos);
5719
5720 /* If we found overlay strings, set up IT to deliver display
5721 elements from the first one. Otherwise set up IT to deliver
5722 from current_buffer. */
5723 if (it->n_overlay_strings)
5724 {
5725 /* Make sure we know settings in current_buffer, so that we can
5726 restore meaningful values when we're done with the overlay
5727 strings. */
5728 if (compute_stop_p)
5729 compute_stop_pos (it);
5730 eassert (it->face_id >= 0);
5731
5732 /* Save IT's settings. They are restored after all overlay
5733 strings have been processed. */
5734 eassert (!compute_stop_p || it->sp == 0);
5735
5736 /* When called from handle_stop, there might be an empty display
5737 string loaded. In that case, don't bother saving it. But
5738 don't use this optimization with the bidi iterator, since we
5739 need the corresponding pop_it call to resync the bidi
5740 iterator's position with IT's position, after we are done
5741 with the overlay strings. (The corresponding call to pop_it
5742 in case of an empty display string is in
5743 next_overlay_string.) */
5744 if (!(!it->bidi_p
5745 && STRINGP (it->string) && !SCHARS (it->string)))
5746 push_it (it, NULL);
5747
5748 /* Set up IT to deliver display elements from the first overlay
5749 string. */
5750 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5751 it->string = it->overlay_strings[0];
5752 it->from_overlay = Qnil;
5753 it->stop_charpos = 0;
5754 eassert (STRINGP (it->string));
5755 it->end_charpos = SCHARS (it->string);
5756 it->prev_stop = 0;
5757 it->base_level_stop = 0;
5758 it->multibyte_p = STRING_MULTIBYTE (it->string);
5759 it->method = GET_FROM_STRING;
5760 it->from_disp_prop_p = 0;
5761
5762 /* Force paragraph direction to be that of the parent
5763 buffer. */
5764 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5765 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5766 else
5767 it->paragraph_embedding = L2R;
5768
5769 /* Set up the bidi iterator for this overlay string. */
5770 if (it->bidi_p)
5771 {
5772 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5773
5774 it->bidi_it.string.lstring = it->string;
5775 it->bidi_it.string.s = NULL;
5776 it->bidi_it.string.schars = SCHARS (it->string);
5777 it->bidi_it.string.bufpos = pos;
5778 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5779 it->bidi_it.string.unibyte = !it->multibyte_p;
5780 it->bidi_it.w = it->w;
5781 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5782 }
5783 return 1;
5784 }
5785
5786 it->current.overlay_string_index = -1;
5787 return 0;
5788 }
5789
5790 static int
5791 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5792 {
5793 it->string = Qnil;
5794 it->method = GET_FROM_BUFFER;
5795
5796 (void) get_overlay_strings_1 (it, charpos, 1);
5797
5798 CHECK_IT (it);
5799
5800 /* Value is non-zero if we found at least one overlay string. */
5801 return STRINGP (it->string);
5802 }
5803
5804
5805 \f
5806 /***********************************************************************
5807 Saving and restoring state
5808 ***********************************************************************/
5809
5810 /* Save current settings of IT on IT->stack. Called, for example,
5811 before setting up IT for an overlay string, to be able to restore
5812 IT's settings to what they were after the overlay string has been
5813 processed. If POSITION is non-NULL, it is the position to save on
5814 the stack instead of IT->position. */
5815
5816 static void
5817 push_it (struct it *it, struct text_pos *position)
5818 {
5819 struct iterator_stack_entry *p;
5820
5821 eassert (it->sp < IT_STACK_SIZE);
5822 p = it->stack + it->sp;
5823
5824 p->stop_charpos = it->stop_charpos;
5825 p->prev_stop = it->prev_stop;
5826 p->base_level_stop = it->base_level_stop;
5827 p->cmp_it = it->cmp_it;
5828 eassert (it->face_id >= 0);
5829 p->face_id = it->face_id;
5830 p->string = it->string;
5831 p->method = it->method;
5832 p->from_overlay = it->from_overlay;
5833 switch (p->method)
5834 {
5835 case GET_FROM_IMAGE:
5836 p->u.image.object = it->object;
5837 p->u.image.image_id = it->image_id;
5838 p->u.image.slice = it->slice;
5839 break;
5840 case GET_FROM_STRETCH:
5841 p->u.stretch.object = it->object;
5842 break;
5843 }
5844 p->position = position ? *position : it->position;
5845 p->current = it->current;
5846 p->end_charpos = it->end_charpos;
5847 p->string_nchars = it->string_nchars;
5848 p->area = it->area;
5849 p->multibyte_p = it->multibyte_p;
5850 p->avoid_cursor_p = it->avoid_cursor_p;
5851 p->space_width = it->space_width;
5852 p->font_height = it->font_height;
5853 p->voffset = it->voffset;
5854 p->string_from_display_prop_p = it->string_from_display_prop_p;
5855 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5856 p->display_ellipsis_p = 0;
5857 p->line_wrap = it->line_wrap;
5858 p->bidi_p = it->bidi_p;
5859 p->paragraph_embedding = it->paragraph_embedding;
5860 p->from_disp_prop_p = it->from_disp_prop_p;
5861 ++it->sp;
5862
5863 /* Save the state of the bidi iterator as well. */
5864 if (it->bidi_p)
5865 bidi_push_it (&it->bidi_it);
5866 }
5867
5868 static void
5869 iterate_out_of_display_property (struct it *it)
5870 {
5871 int buffer_p = !STRINGP (it->string);
5872 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5873 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5874
5875 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5876
5877 /* Maybe initialize paragraph direction. If we are at the beginning
5878 of a new paragraph, next_element_from_buffer may not have a
5879 chance to do that. */
5880 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5881 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5882 /* prev_stop can be zero, so check against BEGV as well. */
5883 while (it->bidi_it.charpos >= bob
5884 && it->prev_stop <= it->bidi_it.charpos
5885 && it->bidi_it.charpos < CHARPOS (it->position)
5886 && it->bidi_it.charpos < eob)
5887 bidi_move_to_visually_next (&it->bidi_it);
5888 /* Record the stop_pos we just crossed, for when we cross it
5889 back, maybe. */
5890 if (it->bidi_it.charpos > CHARPOS (it->position))
5891 it->prev_stop = CHARPOS (it->position);
5892 /* If we ended up not where pop_it put us, resync IT's
5893 positional members with the bidi iterator. */
5894 if (it->bidi_it.charpos != CHARPOS (it->position))
5895 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5896 if (buffer_p)
5897 it->current.pos = it->position;
5898 else
5899 it->current.string_pos = it->position;
5900 }
5901
5902 /* Restore IT's settings from IT->stack. Called, for example, when no
5903 more overlay strings must be processed, and we return to delivering
5904 display elements from a buffer, or when the end of a string from a
5905 `display' property is reached and we return to delivering display
5906 elements from an overlay string, or from a buffer. */
5907
5908 static void
5909 pop_it (struct it *it)
5910 {
5911 struct iterator_stack_entry *p;
5912 int from_display_prop = it->from_disp_prop_p;
5913
5914 eassert (it->sp > 0);
5915 --it->sp;
5916 p = it->stack + it->sp;
5917 it->stop_charpos = p->stop_charpos;
5918 it->prev_stop = p->prev_stop;
5919 it->base_level_stop = p->base_level_stop;
5920 it->cmp_it = p->cmp_it;
5921 it->face_id = p->face_id;
5922 it->current = p->current;
5923 it->position = p->position;
5924 it->string = p->string;
5925 it->from_overlay = p->from_overlay;
5926 if (NILP (it->string))
5927 SET_TEXT_POS (it->current.string_pos, -1, -1);
5928 it->method = p->method;
5929 switch (it->method)
5930 {
5931 case GET_FROM_IMAGE:
5932 it->image_id = p->u.image.image_id;
5933 it->object = p->u.image.object;
5934 it->slice = p->u.image.slice;
5935 break;
5936 case GET_FROM_STRETCH:
5937 it->object = p->u.stretch.object;
5938 break;
5939 case GET_FROM_BUFFER:
5940 it->object = it->w->contents;
5941 break;
5942 case GET_FROM_STRING:
5943 it->object = it->string;
5944 break;
5945 case GET_FROM_DISPLAY_VECTOR:
5946 if (it->s)
5947 it->method = GET_FROM_C_STRING;
5948 else if (STRINGP (it->string))
5949 it->method = GET_FROM_STRING;
5950 else
5951 {
5952 it->method = GET_FROM_BUFFER;
5953 it->object = it->w->contents;
5954 }
5955 }
5956 it->end_charpos = p->end_charpos;
5957 it->string_nchars = p->string_nchars;
5958 it->area = p->area;
5959 it->multibyte_p = p->multibyte_p;
5960 it->avoid_cursor_p = p->avoid_cursor_p;
5961 it->space_width = p->space_width;
5962 it->font_height = p->font_height;
5963 it->voffset = p->voffset;
5964 it->string_from_display_prop_p = p->string_from_display_prop_p;
5965 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5966 it->line_wrap = p->line_wrap;
5967 it->bidi_p = p->bidi_p;
5968 it->paragraph_embedding = p->paragraph_embedding;
5969 it->from_disp_prop_p = p->from_disp_prop_p;
5970 if (it->bidi_p)
5971 {
5972 bidi_pop_it (&it->bidi_it);
5973 /* Bidi-iterate until we get out of the portion of text, if any,
5974 covered by a `display' text property or by an overlay with
5975 `display' property. (We cannot just jump there, because the
5976 internal coherency of the bidi iterator state can not be
5977 preserved across such jumps.) We also must determine the
5978 paragraph base direction if the overlay we just processed is
5979 at the beginning of a new paragraph. */
5980 if (from_display_prop
5981 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5982 iterate_out_of_display_property (it);
5983
5984 eassert ((BUFFERP (it->object)
5985 && IT_CHARPOS (*it) == it->bidi_it.charpos
5986 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5987 || (STRINGP (it->object)
5988 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5989 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5990 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5991 }
5992 }
5993
5994
5995 \f
5996 /***********************************************************************
5997 Moving over lines
5998 ***********************************************************************/
5999
6000 /* Set IT's current position to the previous line start. */
6001
6002 static void
6003 back_to_previous_line_start (struct it *it)
6004 {
6005 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6006
6007 DEC_BOTH (cp, bp);
6008 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6009 }
6010
6011
6012 /* Move IT to the next line start.
6013
6014 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6015 we skipped over part of the text (as opposed to moving the iterator
6016 continuously over the text). Otherwise, don't change the value
6017 of *SKIPPED_P.
6018
6019 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6020 iterator on the newline, if it was found.
6021
6022 Newlines may come from buffer text, overlay strings, or strings
6023 displayed via the `display' property. That's the reason we can't
6024 simply use find_newline_no_quit.
6025
6026 Note that this function may not skip over invisible text that is so
6027 because of text properties and immediately follows a newline. If
6028 it would, function reseat_at_next_visible_line_start, when called
6029 from set_iterator_to_next, would effectively make invisible
6030 characters following a newline part of the wrong glyph row, which
6031 leads to wrong cursor motion. */
6032
6033 static int
6034 forward_to_next_line_start (struct it *it, int *skipped_p,
6035 struct bidi_it *bidi_it_prev)
6036 {
6037 ptrdiff_t old_selective;
6038 int newline_found_p, n;
6039 const int MAX_NEWLINE_DISTANCE = 500;
6040
6041 /* If already on a newline, just consume it to avoid unintended
6042 skipping over invisible text below. */
6043 if (it->what == IT_CHARACTER
6044 && it->c == '\n'
6045 && CHARPOS (it->position) == IT_CHARPOS (*it))
6046 {
6047 if (it->bidi_p && bidi_it_prev)
6048 *bidi_it_prev = it->bidi_it;
6049 set_iterator_to_next (it, 0);
6050 it->c = 0;
6051 return 1;
6052 }
6053
6054 /* Don't handle selective display in the following. It's (a)
6055 unnecessary because it's done by the caller, and (b) leads to an
6056 infinite recursion because next_element_from_ellipsis indirectly
6057 calls this function. */
6058 old_selective = it->selective;
6059 it->selective = 0;
6060
6061 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6062 from buffer text. */
6063 for (n = newline_found_p = 0;
6064 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6065 n += STRINGP (it->string) ? 0 : 1)
6066 {
6067 if (!get_next_display_element (it))
6068 return 0;
6069 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6070 if (newline_found_p && it->bidi_p && bidi_it_prev)
6071 *bidi_it_prev = it->bidi_it;
6072 set_iterator_to_next (it, 0);
6073 }
6074
6075 /* If we didn't find a newline near enough, see if we can use a
6076 short-cut. */
6077 if (!newline_found_p)
6078 {
6079 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6080 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6081 1, &bytepos);
6082 Lisp_Object pos;
6083
6084 eassert (!STRINGP (it->string));
6085
6086 /* If there isn't any `display' property in sight, and no
6087 overlays, we can just use the position of the newline in
6088 buffer text. */
6089 if (it->stop_charpos >= limit
6090 || ((pos = Fnext_single_property_change (make_number (start),
6091 Qdisplay, Qnil,
6092 make_number (limit)),
6093 NILP (pos))
6094 && next_overlay_change (start) == ZV))
6095 {
6096 if (!it->bidi_p)
6097 {
6098 IT_CHARPOS (*it) = limit;
6099 IT_BYTEPOS (*it) = bytepos;
6100 }
6101 else
6102 {
6103 struct bidi_it bprev;
6104
6105 /* Help bidi.c avoid expensive searches for display
6106 properties and overlays, by telling it that there are
6107 none up to `limit'. */
6108 if (it->bidi_it.disp_pos < limit)
6109 {
6110 it->bidi_it.disp_pos = limit;
6111 it->bidi_it.disp_prop = 0;
6112 }
6113 do {
6114 bprev = it->bidi_it;
6115 bidi_move_to_visually_next (&it->bidi_it);
6116 } while (it->bidi_it.charpos != limit);
6117 IT_CHARPOS (*it) = limit;
6118 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6119 if (bidi_it_prev)
6120 *bidi_it_prev = bprev;
6121 }
6122 *skipped_p = newline_found_p = true;
6123 }
6124 else
6125 {
6126 while (get_next_display_element (it)
6127 && !newline_found_p)
6128 {
6129 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6130 if (newline_found_p && it->bidi_p && bidi_it_prev)
6131 *bidi_it_prev = it->bidi_it;
6132 set_iterator_to_next (it, 0);
6133 }
6134 }
6135 }
6136
6137 it->selective = old_selective;
6138 return newline_found_p;
6139 }
6140
6141
6142 /* Set IT's current position to the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. Caution: this does not change IT->current_x and
6145 IT->hpos. */
6146
6147 static void
6148 back_to_previous_visible_line_start (struct it *it)
6149 {
6150 while (IT_CHARPOS (*it) > BEGV)
6151 {
6152 back_to_previous_line_start (it);
6153
6154 if (IT_CHARPOS (*it) <= BEGV)
6155 break;
6156
6157 /* If selective > 0, then lines indented more than its value are
6158 invisible. */
6159 if (it->selective > 0
6160 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6161 it->selective))
6162 continue;
6163
6164 /* Check the newline before point for invisibility. */
6165 {
6166 Lisp_Object prop;
6167 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6168 Qinvisible, it->window);
6169 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6170 continue;
6171 }
6172
6173 if (IT_CHARPOS (*it) <= BEGV)
6174 break;
6175
6176 {
6177 struct it it2;
6178 void *it2data = NULL;
6179 ptrdiff_t pos;
6180 ptrdiff_t beg, end;
6181 Lisp_Object val, overlay;
6182
6183 SAVE_IT (it2, *it, it2data);
6184
6185 /* If newline is part of a composition, continue from start of composition */
6186 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6187 && beg < IT_CHARPOS (*it))
6188 goto replaced;
6189
6190 /* If newline is replaced by a display property, find start of overlay
6191 or interval and continue search from that point. */
6192 pos = --IT_CHARPOS (it2);
6193 --IT_BYTEPOS (it2);
6194 it2.sp = 0;
6195 bidi_unshelve_cache (NULL, 0);
6196 it2.string_from_display_prop_p = 0;
6197 it2.from_disp_prop_p = 0;
6198 if (handle_display_prop (&it2) == HANDLED_RETURN
6199 && !NILP (val = get_char_property_and_overlay
6200 (make_number (pos), Qdisplay, Qnil, &overlay))
6201 && (OVERLAYP (overlay)
6202 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6203 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6204 {
6205 RESTORE_IT (it, it, it2data);
6206 goto replaced;
6207 }
6208
6209 /* Newline is not replaced by anything -- so we are done. */
6210 RESTORE_IT (it, it, it2data);
6211 break;
6212
6213 replaced:
6214 if (beg < BEGV)
6215 beg = BEGV;
6216 IT_CHARPOS (*it) = beg;
6217 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6218 }
6219 }
6220
6221 it->continuation_lines_width = 0;
6222
6223 eassert (IT_CHARPOS (*it) >= BEGV);
6224 eassert (IT_CHARPOS (*it) == BEGV
6225 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6226 CHECK_IT (it);
6227 }
6228
6229
6230 /* Reseat iterator IT at the previous visible line start. Skip
6231 invisible text that is so either due to text properties or due to
6232 selective display. At the end, update IT's overlay information,
6233 face information etc. */
6234
6235 void
6236 reseat_at_previous_visible_line_start (struct it *it)
6237 {
6238 back_to_previous_visible_line_start (it);
6239 reseat (it, it->current.pos, 1);
6240 CHECK_IT (it);
6241 }
6242
6243
6244 /* Reseat iterator IT on the next visible line start in the current
6245 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6246 preceding the line start. Skip over invisible text that is so
6247 because of selective display. Compute faces, overlays etc at the
6248 new position. Note that this function does not skip over text that
6249 is invisible because of text properties. */
6250
6251 static void
6252 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6253 {
6254 int newline_found_p, skipped_p = 0;
6255 struct bidi_it bidi_it_prev;
6256
6257 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6258
6259 /* Skip over lines that are invisible because they are indented
6260 more than the value of IT->selective. */
6261 if (it->selective > 0)
6262 while (IT_CHARPOS (*it) < ZV
6263 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6264 it->selective))
6265 {
6266 eassert (IT_BYTEPOS (*it) == BEGV
6267 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6268 newline_found_p =
6269 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6270 }
6271
6272 /* Position on the newline if that's what's requested. */
6273 if (on_newline_p && newline_found_p)
6274 {
6275 if (STRINGP (it->string))
6276 {
6277 if (IT_STRING_CHARPOS (*it) > 0)
6278 {
6279 if (!it->bidi_p)
6280 {
6281 --IT_STRING_CHARPOS (*it);
6282 --IT_STRING_BYTEPOS (*it);
6283 }
6284 else
6285 {
6286 /* We need to restore the bidi iterator to the state
6287 it had on the newline, and resync the IT's
6288 position with that. */
6289 it->bidi_it = bidi_it_prev;
6290 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6291 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6292 }
6293 }
6294 }
6295 else if (IT_CHARPOS (*it) > BEGV)
6296 {
6297 if (!it->bidi_p)
6298 {
6299 --IT_CHARPOS (*it);
6300 --IT_BYTEPOS (*it);
6301 }
6302 else
6303 {
6304 /* We need to restore the bidi iterator to the state it
6305 had on the newline and resync IT with that. */
6306 it->bidi_it = bidi_it_prev;
6307 IT_CHARPOS (*it) = it->bidi_it.charpos;
6308 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6309 }
6310 reseat (it, it->current.pos, 0);
6311 }
6312 }
6313 else if (skipped_p)
6314 reseat (it, it->current.pos, 0);
6315
6316 CHECK_IT (it);
6317 }
6318
6319
6320 \f
6321 /***********************************************************************
6322 Changing an iterator's position
6323 ***********************************************************************/
6324
6325 /* Change IT's current position to POS in current_buffer. If FORCE_P
6326 is non-zero, always check for text properties at the new position.
6327 Otherwise, text properties are only looked up if POS >=
6328 IT->check_charpos of a property. */
6329
6330 static void
6331 reseat (struct it *it, struct text_pos pos, int force_p)
6332 {
6333 ptrdiff_t original_pos = IT_CHARPOS (*it);
6334
6335 reseat_1 (it, pos, 0);
6336
6337 /* Determine where to check text properties. Avoid doing it
6338 where possible because text property lookup is very expensive. */
6339 if (force_p
6340 || CHARPOS (pos) > it->stop_charpos
6341 || CHARPOS (pos) < original_pos)
6342 {
6343 if (it->bidi_p)
6344 {
6345 /* For bidi iteration, we need to prime prev_stop and
6346 base_level_stop with our best estimations. */
6347 /* Implementation note: Of course, POS is not necessarily a
6348 stop position, so assigning prev_pos to it is a lie; we
6349 should have called compute_stop_backwards. However, if
6350 the current buffer does not include any R2L characters,
6351 that call would be a waste of cycles, because the
6352 iterator will never move back, and thus never cross this
6353 "fake" stop position. So we delay that backward search
6354 until the time we really need it, in next_element_from_buffer. */
6355 if (CHARPOS (pos) != it->prev_stop)
6356 it->prev_stop = CHARPOS (pos);
6357 if (CHARPOS (pos) < it->base_level_stop)
6358 it->base_level_stop = 0; /* meaning it's unknown */
6359 handle_stop (it);
6360 }
6361 else
6362 {
6363 handle_stop (it);
6364 it->prev_stop = it->base_level_stop = 0;
6365 }
6366
6367 }
6368
6369 CHECK_IT (it);
6370 }
6371
6372
6373 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6374 IT->stop_pos to POS, also. */
6375
6376 static void
6377 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6378 {
6379 /* Don't call this function when scanning a C string. */
6380 eassert (it->s == NULL);
6381
6382 /* POS must be a reasonable value. */
6383 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6384
6385 it->current.pos = it->position = pos;
6386 it->end_charpos = ZV;
6387 it->dpvec = NULL;
6388 it->current.dpvec_index = -1;
6389 it->current.overlay_string_index = -1;
6390 IT_STRING_CHARPOS (*it) = -1;
6391 IT_STRING_BYTEPOS (*it) = -1;
6392 it->string = Qnil;
6393 it->method = GET_FROM_BUFFER;
6394 it->object = it->w->contents;
6395 it->area = TEXT_AREA;
6396 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6397 it->sp = 0;
6398 it->string_from_display_prop_p = 0;
6399 it->string_from_prefix_prop_p = 0;
6400
6401 it->from_disp_prop_p = 0;
6402 it->face_before_selective_p = 0;
6403 if (it->bidi_p)
6404 {
6405 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6406 &it->bidi_it);
6407 bidi_unshelve_cache (NULL, 0);
6408 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6409 it->bidi_it.string.s = NULL;
6410 it->bidi_it.string.lstring = Qnil;
6411 it->bidi_it.string.bufpos = 0;
6412 it->bidi_it.string.unibyte = 0;
6413 it->bidi_it.w = it->w;
6414 }
6415
6416 if (set_stop_p)
6417 {
6418 it->stop_charpos = CHARPOS (pos);
6419 it->base_level_stop = CHARPOS (pos);
6420 }
6421 /* This make the information stored in it->cmp_it invalidate. */
6422 it->cmp_it.id = -1;
6423 }
6424
6425
6426 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6427 If S is non-null, it is a C string to iterate over. Otherwise,
6428 STRING gives a Lisp string to iterate over.
6429
6430 If PRECISION > 0, don't return more then PRECISION number of
6431 characters from the string.
6432
6433 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6434 characters have been returned. FIELD_WIDTH < 0 means an infinite
6435 field width.
6436
6437 MULTIBYTE = 0 means disable processing of multibyte characters,
6438 MULTIBYTE > 0 means enable it,
6439 MULTIBYTE < 0 means use IT->multibyte_p.
6440
6441 IT must be initialized via a prior call to init_iterator before
6442 calling this function. */
6443
6444 static void
6445 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6446 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6447 int multibyte)
6448 {
6449 /* No text property checks performed by default, but see below. */
6450 it->stop_charpos = -1;
6451
6452 /* Set iterator position and end position. */
6453 memset (&it->current, 0, sizeof it->current);
6454 it->current.overlay_string_index = -1;
6455 it->current.dpvec_index = -1;
6456 eassert (charpos >= 0);
6457
6458 /* If STRING is specified, use its multibyteness, otherwise use the
6459 setting of MULTIBYTE, if specified. */
6460 if (multibyte >= 0)
6461 it->multibyte_p = multibyte > 0;
6462
6463 /* Bidirectional reordering of strings is controlled by the default
6464 value of bidi-display-reordering. Don't try to reorder while
6465 loading loadup.el, as the necessary character property tables are
6466 not yet available. */
6467 it->bidi_p =
6468 NILP (Vpurify_flag)
6469 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6470
6471 if (s == NULL)
6472 {
6473 eassert (STRINGP (string));
6474 it->string = string;
6475 it->s = NULL;
6476 it->end_charpos = it->string_nchars = SCHARS (string);
6477 it->method = GET_FROM_STRING;
6478 it->current.string_pos = string_pos (charpos, string);
6479
6480 if (it->bidi_p)
6481 {
6482 it->bidi_it.string.lstring = string;
6483 it->bidi_it.string.s = NULL;
6484 it->bidi_it.string.schars = it->end_charpos;
6485 it->bidi_it.string.bufpos = 0;
6486 it->bidi_it.string.from_disp_str = 0;
6487 it->bidi_it.string.unibyte = !it->multibyte_p;
6488 it->bidi_it.w = it->w;
6489 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6490 FRAME_WINDOW_P (it->f), &it->bidi_it);
6491 }
6492 }
6493 else
6494 {
6495 it->s = (const unsigned char *) s;
6496 it->string = Qnil;
6497
6498 /* Note that we use IT->current.pos, not it->current.string_pos,
6499 for displaying C strings. */
6500 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6501 if (it->multibyte_p)
6502 {
6503 it->current.pos = c_string_pos (charpos, s, 1);
6504 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6505 }
6506 else
6507 {
6508 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6509 it->end_charpos = it->string_nchars = strlen (s);
6510 }
6511
6512 if (it->bidi_p)
6513 {
6514 it->bidi_it.string.lstring = Qnil;
6515 it->bidi_it.string.s = (const unsigned char *) s;
6516 it->bidi_it.string.schars = it->end_charpos;
6517 it->bidi_it.string.bufpos = 0;
6518 it->bidi_it.string.from_disp_str = 0;
6519 it->bidi_it.string.unibyte = !it->multibyte_p;
6520 it->bidi_it.w = it->w;
6521 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6522 &it->bidi_it);
6523 }
6524 it->method = GET_FROM_C_STRING;
6525 }
6526
6527 /* PRECISION > 0 means don't return more than PRECISION characters
6528 from the string. */
6529 if (precision > 0 && it->end_charpos - charpos > precision)
6530 {
6531 it->end_charpos = it->string_nchars = charpos + precision;
6532 if (it->bidi_p)
6533 it->bidi_it.string.schars = it->end_charpos;
6534 }
6535
6536 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6537 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6538 FIELD_WIDTH < 0 means infinite field width. This is useful for
6539 padding with `-' at the end of a mode line. */
6540 if (field_width < 0)
6541 field_width = INFINITY;
6542 /* Implementation note: We deliberately don't enlarge
6543 it->bidi_it.string.schars here to fit it->end_charpos, because
6544 the bidi iterator cannot produce characters out of thin air. */
6545 if (field_width > it->end_charpos - charpos)
6546 it->end_charpos = charpos + field_width;
6547
6548 /* Use the standard display table for displaying strings. */
6549 if (DISP_TABLE_P (Vstandard_display_table))
6550 it->dp = XCHAR_TABLE (Vstandard_display_table);
6551
6552 it->stop_charpos = charpos;
6553 it->prev_stop = charpos;
6554 it->base_level_stop = 0;
6555 if (it->bidi_p)
6556 {
6557 it->bidi_it.first_elt = 1;
6558 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6559 it->bidi_it.disp_pos = -1;
6560 }
6561 if (s == NULL && it->multibyte_p)
6562 {
6563 ptrdiff_t endpos = SCHARS (it->string);
6564 if (endpos > it->end_charpos)
6565 endpos = it->end_charpos;
6566 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6567 it->string);
6568 }
6569 CHECK_IT (it);
6570 }
6571
6572
6573 \f
6574 /***********************************************************************
6575 Iteration
6576 ***********************************************************************/
6577
6578 /* Map enum it_method value to corresponding next_element_from_* function. */
6579
6580 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6581 {
6582 next_element_from_buffer,
6583 next_element_from_display_vector,
6584 next_element_from_string,
6585 next_element_from_c_string,
6586 next_element_from_image,
6587 next_element_from_stretch
6588 };
6589
6590 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6591
6592
6593 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6594 (possibly with the following characters). */
6595
6596 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6597 ((IT)->cmp_it.id >= 0 \
6598 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6599 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6600 END_CHARPOS, (IT)->w, \
6601 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6602 (IT)->string)))
6603
6604
6605 /* Lookup the char-table Vglyphless_char_display for character C (-1
6606 if we want information for no-font case), and return the display
6607 method symbol. By side-effect, update it->what and
6608 it->glyphless_method. This function is called from
6609 get_next_display_element for each character element, and from
6610 x_produce_glyphs when no suitable font was found. */
6611
6612 Lisp_Object
6613 lookup_glyphless_char_display (int c, struct it *it)
6614 {
6615 Lisp_Object glyphless_method = Qnil;
6616
6617 if (CHAR_TABLE_P (Vglyphless_char_display)
6618 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6619 {
6620 if (c >= 0)
6621 {
6622 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6623 if (CONSP (glyphless_method))
6624 glyphless_method = FRAME_WINDOW_P (it->f)
6625 ? XCAR (glyphless_method)
6626 : XCDR (glyphless_method);
6627 }
6628 else
6629 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6630 }
6631
6632 retry:
6633 if (NILP (glyphless_method))
6634 {
6635 if (c >= 0)
6636 /* The default is to display the character by a proper font. */
6637 return Qnil;
6638 /* The default for the no-font case is to display an empty box. */
6639 glyphless_method = Qempty_box;
6640 }
6641 if (EQ (glyphless_method, Qzero_width))
6642 {
6643 if (c >= 0)
6644 return glyphless_method;
6645 /* This method can't be used for the no-font case. */
6646 glyphless_method = Qempty_box;
6647 }
6648 if (EQ (glyphless_method, Qthin_space))
6649 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6650 else if (EQ (glyphless_method, Qempty_box))
6651 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6652 else if (EQ (glyphless_method, Qhex_code))
6653 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6654 else if (STRINGP (glyphless_method))
6655 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6656 else
6657 {
6658 /* Invalid value. We use the default method. */
6659 glyphless_method = Qnil;
6660 goto retry;
6661 }
6662 it->what = IT_GLYPHLESS;
6663 return glyphless_method;
6664 }
6665
6666 /* Merge escape glyph face and cache the result. */
6667
6668 static struct frame *last_escape_glyph_frame = NULL;
6669 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6670 static int last_escape_glyph_merged_face_id = 0;
6671
6672 static int
6673 merge_escape_glyph_face (struct it *it)
6674 {
6675 int face_id;
6676
6677 if (it->f == last_escape_glyph_frame
6678 && it->face_id == last_escape_glyph_face_id)
6679 face_id = last_escape_glyph_merged_face_id;
6680 else
6681 {
6682 /* Merge the `escape-glyph' face into the current face. */
6683 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6684 last_escape_glyph_frame = it->f;
6685 last_escape_glyph_face_id = it->face_id;
6686 last_escape_glyph_merged_face_id = face_id;
6687 }
6688 return face_id;
6689 }
6690
6691 /* Likewise for glyphless glyph face. */
6692
6693 static struct frame *last_glyphless_glyph_frame = NULL;
6694 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6695 static int last_glyphless_glyph_merged_face_id = 0;
6696
6697 int
6698 merge_glyphless_glyph_face (struct it *it)
6699 {
6700 int face_id;
6701
6702 if (it->f == last_glyphless_glyph_frame
6703 && it->face_id == last_glyphless_glyph_face_id)
6704 face_id = last_glyphless_glyph_merged_face_id;
6705 else
6706 {
6707 /* Merge the `glyphless-char' face into the current face. */
6708 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6709 last_glyphless_glyph_frame = it->f;
6710 last_glyphless_glyph_face_id = it->face_id;
6711 last_glyphless_glyph_merged_face_id = face_id;
6712 }
6713 return face_id;
6714 }
6715
6716 /* Load IT's display element fields with information about the next
6717 display element from the current position of IT. Value is zero if
6718 end of buffer (or C string) is reached. */
6719
6720 static int
6721 get_next_display_element (struct it *it)
6722 {
6723 /* Non-zero means that we found a display element. Zero means that
6724 we hit the end of what we iterate over. Performance note: the
6725 function pointer `method' used here turns out to be faster than
6726 using a sequence of if-statements. */
6727 int success_p;
6728
6729 get_next:
6730 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6731
6732 if (it->what == IT_CHARACTER)
6733 {
6734 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6735 and only if (a) the resolved directionality of that character
6736 is R..." */
6737 /* FIXME: Do we need an exception for characters from display
6738 tables? */
6739 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6740 it->c = bidi_mirror_char (it->c);
6741 /* Map via display table or translate control characters.
6742 IT->c, IT->len etc. have been set to the next character by
6743 the function call above. If we have a display table, and it
6744 contains an entry for IT->c, translate it. Don't do this if
6745 IT->c itself comes from a display table, otherwise we could
6746 end up in an infinite recursion. (An alternative could be to
6747 count the recursion depth of this function and signal an
6748 error when a certain maximum depth is reached.) Is it worth
6749 it? */
6750 if (success_p && it->dpvec == NULL)
6751 {
6752 Lisp_Object dv;
6753 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6754 int nonascii_space_p = 0;
6755 int nonascii_hyphen_p = 0;
6756 int c = it->c; /* This is the character to display. */
6757
6758 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6759 {
6760 eassert (SINGLE_BYTE_CHAR_P (c));
6761 if (unibyte_display_via_language_environment)
6762 {
6763 c = DECODE_CHAR (unibyte, c);
6764 if (c < 0)
6765 c = BYTE8_TO_CHAR (it->c);
6766 }
6767 else
6768 c = BYTE8_TO_CHAR (it->c);
6769 }
6770
6771 if (it->dp
6772 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6773 VECTORP (dv)))
6774 {
6775 struct Lisp_Vector *v = XVECTOR (dv);
6776
6777 /* Return the first character from the display table
6778 entry, if not empty. If empty, don't display the
6779 current character. */
6780 if (v->header.size)
6781 {
6782 it->dpvec_char_len = it->len;
6783 it->dpvec = v->contents;
6784 it->dpend = v->contents + v->header.size;
6785 it->current.dpvec_index = 0;
6786 it->dpvec_face_id = -1;
6787 it->saved_face_id = it->face_id;
6788 it->method = GET_FROM_DISPLAY_VECTOR;
6789 it->ellipsis_p = 0;
6790 }
6791 else
6792 {
6793 set_iterator_to_next (it, 0);
6794 }
6795 goto get_next;
6796 }
6797
6798 if (! NILP (lookup_glyphless_char_display (c, it)))
6799 {
6800 if (it->what == IT_GLYPHLESS)
6801 goto done;
6802 /* Don't display this character. */
6803 set_iterator_to_next (it, 0);
6804 goto get_next;
6805 }
6806
6807 /* If `nobreak-char-display' is non-nil, we display
6808 non-ASCII spaces and hyphens specially. */
6809 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6810 {
6811 if (c == 0xA0)
6812 nonascii_space_p = true;
6813 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6814 nonascii_hyphen_p = true;
6815 }
6816
6817 /* Translate control characters into `\003' or `^C' form.
6818 Control characters coming from a display table entry are
6819 currently not translated because we use IT->dpvec to hold
6820 the translation. This could easily be changed but I
6821 don't believe that it is worth doing.
6822
6823 The characters handled by `nobreak-char-display' must be
6824 translated too.
6825
6826 Non-printable characters and raw-byte characters are also
6827 translated to octal form. */
6828 if (((c < ' ' || c == 127) /* ASCII control chars. */
6829 ? (it->area != TEXT_AREA
6830 /* In mode line, treat \n, \t like other crl chars. */
6831 || (c != '\t'
6832 && it->glyph_row
6833 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6834 || (c != '\n' && c != '\t'))
6835 : (nonascii_space_p
6836 || nonascii_hyphen_p
6837 || CHAR_BYTE8_P (c)
6838 || ! CHAR_PRINTABLE_P (c))))
6839 {
6840 /* C is a control character, non-ASCII space/hyphen,
6841 raw-byte, or a non-printable character which must be
6842 displayed either as '\003' or as `^C' where the '\\'
6843 and '^' can be defined in the display table. Fill
6844 IT->ctl_chars with glyphs for what we have to
6845 display. Then, set IT->dpvec to these glyphs. */
6846 Lisp_Object gc;
6847 int ctl_len;
6848 int face_id;
6849 int lface_id = 0;
6850 int escape_glyph;
6851
6852 /* Handle control characters with ^. */
6853
6854 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6855 {
6856 int g;
6857
6858 g = '^'; /* default glyph for Control */
6859 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6860 if (it->dp
6861 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6862 {
6863 g = GLYPH_CODE_CHAR (gc);
6864 lface_id = GLYPH_CODE_FACE (gc);
6865 }
6866
6867 face_id = (lface_id
6868 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6869 : merge_escape_glyph_face (it));
6870
6871 XSETINT (it->ctl_chars[0], g);
6872 XSETINT (it->ctl_chars[1], c ^ 0100);
6873 ctl_len = 2;
6874 goto display_control;
6875 }
6876
6877 /* Handle non-ascii space in the mode where it only gets
6878 highlighting. */
6879
6880 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6881 {
6882 /* Merge `nobreak-space' into the current face. */
6883 face_id = merge_faces (it->f, Qnobreak_space, 0,
6884 it->face_id);
6885 XSETINT (it->ctl_chars[0], ' ');
6886 ctl_len = 1;
6887 goto display_control;
6888 }
6889
6890 /* Handle sequences that start with the "escape glyph". */
6891
6892 /* the default escape glyph is \. */
6893 escape_glyph = '\\';
6894
6895 if (it->dp
6896 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6897 {
6898 escape_glyph = GLYPH_CODE_CHAR (gc);
6899 lface_id = GLYPH_CODE_FACE (gc);
6900 }
6901
6902 face_id = (lface_id
6903 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6904 : merge_escape_glyph_face (it));
6905
6906 /* Draw non-ASCII hyphen with just highlighting: */
6907
6908 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6909 {
6910 XSETINT (it->ctl_chars[0], '-');
6911 ctl_len = 1;
6912 goto display_control;
6913 }
6914
6915 /* Draw non-ASCII space/hyphen with escape glyph: */
6916
6917 if (nonascii_space_p || nonascii_hyphen_p)
6918 {
6919 XSETINT (it->ctl_chars[0], escape_glyph);
6920 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6921 ctl_len = 2;
6922 goto display_control;
6923 }
6924
6925 {
6926 char str[10];
6927 int len, i;
6928
6929 if (CHAR_BYTE8_P (c))
6930 /* Display \200 instead of \17777600. */
6931 c = CHAR_TO_BYTE8 (c);
6932 len = sprintf (str, "%03o", c);
6933
6934 XSETINT (it->ctl_chars[0], escape_glyph);
6935 for (i = 0; i < len; i++)
6936 XSETINT (it->ctl_chars[i + 1], str[i]);
6937 ctl_len = len + 1;
6938 }
6939
6940 display_control:
6941 /* Set up IT->dpvec and return first character from it. */
6942 it->dpvec_char_len = it->len;
6943 it->dpvec = it->ctl_chars;
6944 it->dpend = it->dpvec + ctl_len;
6945 it->current.dpvec_index = 0;
6946 it->dpvec_face_id = face_id;
6947 it->saved_face_id = it->face_id;
6948 it->method = GET_FROM_DISPLAY_VECTOR;
6949 it->ellipsis_p = 0;
6950 goto get_next;
6951 }
6952 it->char_to_display = c;
6953 }
6954 else if (success_p)
6955 {
6956 it->char_to_display = it->c;
6957 }
6958 }
6959
6960 #ifdef HAVE_WINDOW_SYSTEM
6961 /* Adjust face id for a multibyte character. There are no multibyte
6962 character in unibyte text. */
6963 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6964 && it->multibyte_p
6965 && success_p
6966 && FRAME_WINDOW_P (it->f))
6967 {
6968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6969
6970 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6971 {
6972 /* Automatic composition with glyph-string. */
6973 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6974
6975 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6976 }
6977 else
6978 {
6979 ptrdiff_t pos = (it->s ? -1
6980 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6981 : IT_CHARPOS (*it));
6982 int c;
6983
6984 if (it->what == IT_CHARACTER)
6985 c = it->char_to_display;
6986 else
6987 {
6988 struct composition *cmp = composition_table[it->cmp_it.id];
6989 int i;
6990
6991 c = ' ';
6992 for (i = 0; i < cmp->glyph_len; i++)
6993 /* TAB in a composition means display glyphs with
6994 padding space on the left or right. */
6995 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6996 break;
6997 }
6998 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6999 }
7000 }
7001 #endif /* HAVE_WINDOW_SYSTEM */
7002
7003 done:
7004 /* Is this character the last one of a run of characters with
7005 box? If yes, set IT->end_of_box_run_p to 1. */
7006 if (it->face_box_p
7007 && it->s == NULL)
7008 {
7009 if (it->method == GET_FROM_STRING && it->sp)
7010 {
7011 int face_id = underlying_face_id (it);
7012 struct face *face = FACE_FROM_ID (it->f, face_id);
7013
7014 if (face)
7015 {
7016 if (face->box == FACE_NO_BOX)
7017 {
7018 /* If the box comes from face properties in a
7019 display string, check faces in that string. */
7020 int string_face_id = face_after_it_pos (it);
7021 it->end_of_box_run_p
7022 = (FACE_FROM_ID (it->f, string_face_id)->box
7023 == FACE_NO_BOX);
7024 }
7025 /* Otherwise, the box comes from the underlying face.
7026 If this is the last string character displayed, check
7027 the next buffer location. */
7028 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7029 && (it->current.overlay_string_index
7030 == it->n_overlay_strings - 1))
7031 {
7032 ptrdiff_t ignore;
7033 int next_face_id;
7034 struct text_pos pos = it->current.pos;
7035 INC_TEXT_POS (pos, it->multibyte_p);
7036
7037 next_face_id = face_at_buffer_position
7038 (it->w, CHARPOS (pos), &ignore,
7039 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7040 -1);
7041 it->end_of_box_run_p
7042 = (FACE_FROM_ID (it->f, next_face_id)->box
7043 == FACE_NO_BOX);
7044 }
7045 }
7046 }
7047 /* next_element_from_display_vector sets this flag according to
7048 faces of the display vector glyphs, see there. */
7049 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7050 {
7051 int face_id = face_after_it_pos (it);
7052 it->end_of_box_run_p
7053 = (face_id != it->face_id
7054 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7055 }
7056 }
7057 /* If we reached the end of the object we've been iterating (e.g., a
7058 display string or an overlay string), and there's something on
7059 IT->stack, proceed with what's on the stack. It doesn't make
7060 sense to return zero if there's unprocessed stuff on the stack,
7061 because otherwise that stuff will never be displayed. */
7062 if (!success_p && it->sp > 0)
7063 {
7064 set_iterator_to_next (it, 0);
7065 success_p = get_next_display_element (it);
7066 }
7067
7068 /* Value is 0 if end of buffer or string reached. */
7069 return success_p;
7070 }
7071
7072
7073 /* Move IT to the next display element.
7074
7075 RESEAT_P non-zero means if called on a newline in buffer text,
7076 skip to the next visible line start.
7077
7078 Functions get_next_display_element and set_iterator_to_next are
7079 separate because I find this arrangement easier to handle than a
7080 get_next_display_element function that also increments IT's
7081 position. The way it is we can first look at an iterator's current
7082 display element, decide whether it fits on a line, and if it does,
7083 increment the iterator position. The other way around we probably
7084 would either need a flag indicating whether the iterator has to be
7085 incremented the next time, or we would have to implement a
7086 decrement position function which would not be easy to write. */
7087
7088 void
7089 set_iterator_to_next (struct it *it, int reseat_p)
7090 {
7091 /* Reset flags indicating start and end of a sequence of characters
7092 with box. Reset them at the start of this function because
7093 moving the iterator to a new position might set them. */
7094 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7095
7096 switch (it->method)
7097 {
7098 case GET_FROM_BUFFER:
7099 /* The current display element of IT is a character from
7100 current_buffer. Advance in the buffer, and maybe skip over
7101 invisible lines that are so because of selective display. */
7102 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7103 reseat_at_next_visible_line_start (it, 0);
7104 else if (it->cmp_it.id >= 0)
7105 {
7106 /* We are currently getting glyphs from a composition. */
7107 int i;
7108
7109 if (! it->bidi_p)
7110 {
7111 IT_CHARPOS (*it) += it->cmp_it.nchars;
7112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7113 if (it->cmp_it.to < it->cmp_it.nglyphs)
7114 {
7115 it->cmp_it.from = it->cmp_it.to;
7116 }
7117 else
7118 {
7119 it->cmp_it.id = -1;
7120 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7121 IT_BYTEPOS (*it),
7122 it->end_charpos, Qnil);
7123 }
7124 }
7125 else if (! it->cmp_it.reversed_p)
7126 {
7127 /* Composition created while scanning forward. */
7128 /* Update IT's char/byte positions to point to the first
7129 character of the next grapheme cluster, or to the
7130 character visually after the current composition. */
7131 for (i = 0; i < it->cmp_it.nchars; i++)
7132 bidi_move_to_visually_next (&it->bidi_it);
7133 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7134 IT_CHARPOS (*it) = it->bidi_it.charpos;
7135
7136 if (it->cmp_it.to < it->cmp_it.nglyphs)
7137 {
7138 /* Proceed to the next grapheme cluster. */
7139 it->cmp_it.from = it->cmp_it.to;
7140 }
7141 else
7142 {
7143 /* No more grapheme clusters in this composition.
7144 Find the next stop position. */
7145 ptrdiff_t stop = it->end_charpos;
7146 if (it->bidi_it.scan_dir < 0)
7147 /* Now we are scanning backward and don't know
7148 where to stop. */
7149 stop = -1;
7150 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7151 IT_BYTEPOS (*it), stop, Qnil);
7152 }
7153 }
7154 else
7155 {
7156 /* Composition created while scanning backward. */
7157 /* Update IT's char/byte positions to point to the last
7158 character of the previous grapheme cluster, or the
7159 character visually after the current composition. */
7160 for (i = 0; i < it->cmp_it.nchars; i++)
7161 bidi_move_to_visually_next (&it->bidi_it);
7162 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7163 IT_CHARPOS (*it) = it->bidi_it.charpos;
7164 if (it->cmp_it.from > 0)
7165 {
7166 /* Proceed to the previous grapheme cluster. */
7167 it->cmp_it.to = it->cmp_it.from;
7168 }
7169 else
7170 {
7171 /* No more grapheme clusters in this composition.
7172 Find the next stop position. */
7173 ptrdiff_t stop = it->end_charpos;
7174 if (it->bidi_it.scan_dir < 0)
7175 /* Now we are scanning backward and don't know
7176 where to stop. */
7177 stop = -1;
7178 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7179 IT_BYTEPOS (*it), stop, Qnil);
7180 }
7181 }
7182 }
7183 else
7184 {
7185 eassert (it->len != 0);
7186
7187 if (!it->bidi_p)
7188 {
7189 IT_BYTEPOS (*it) += it->len;
7190 IT_CHARPOS (*it) += 1;
7191 }
7192 else
7193 {
7194 int prev_scan_dir = it->bidi_it.scan_dir;
7195 /* If this is a new paragraph, determine its base
7196 direction (a.k.a. its base embedding level). */
7197 if (it->bidi_it.new_paragraph)
7198 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7199 bidi_move_to_visually_next (&it->bidi_it);
7200 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7201 IT_CHARPOS (*it) = it->bidi_it.charpos;
7202 if (prev_scan_dir != it->bidi_it.scan_dir)
7203 {
7204 /* As the scan direction was changed, we must
7205 re-compute the stop position for composition. */
7206 ptrdiff_t stop = it->end_charpos;
7207 if (it->bidi_it.scan_dir < 0)
7208 stop = -1;
7209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7210 IT_BYTEPOS (*it), stop, Qnil);
7211 }
7212 }
7213 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7214 }
7215 break;
7216
7217 case GET_FROM_C_STRING:
7218 /* Current display element of IT is from a C string. */
7219 if (!it->bidi_p
7220 /* If the string position is beyond string's end, it means
7221 next_element_from_c_string is padding the string with
7222 blanks, in which case we bypass the bidi iterator,
7223 because it cannot deal with such virtual characters. */
7224 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7225 {
7226 IT_BYTEPOS (*it) += it->len;
7227 IT_CHARPOS (*it) += 1;
7228 }
7229 else
7230 {
7231 bidi_move_to_visually_next (&it->bidi_it);
7232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7233 IT_CHARPOS (*it) = it->bidi_it.charpos;
7234 }
7235 break;
7236
7237 case GET_FROM_DISPLAY_VECTOR:
7238 /* Current display element of IT is from a display table entry.
7239 Advance in the display table definition. Reset it to null if
7240 end reached, and continue with characters from buffers/
7241 strings. */
7242 ++it->current.dpvec_index;
7243
7244 /* Restore face of the iterator to what they were before the
7245 display vector entry (these entries may contain faces). */
7246 it->face_id = it->saved_face_id;
7247
7248 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7249 {
7250 int recheck_faces = it->ellipsis_p;
7251
7252 if (it->s)
7253 it->method = GET_FROM_C_STRING;
7254 else if (STRINGP (it->string))
7255 it->method = GET_FROM_STRING;
7256 else
7257 {
7258 it->method = GET_FROM_BUFFER;
7259 it->object = it->w->contents;
7260 }
7261
7262 it->dpvec = NULL;
7263 it->current.dpvec_index = -1;
7264
7265 /* Skip over characters which were displayed via IT->dpvec. */
7266 if (it->dpvec_char_len < 0)
7267 reseat_at_next_visible_line_start (it, 1);
7268 else if (it->dpvec_char_len > 0)
7269 {
7270 if (it->method == GET_FROM_STRING
7271 && it->current.overlay_string_index >= 0
7272 && it->n_overlay_strings > 0)
7273 it->ignore_overlay_strings_at_pos_p = true;
7274 it->len = it->dpvec_char_len;
7275 set_iterator_to_next (it, reseat_p);
7276 }
7277
7278 /* Maybe recheck faces after display vector. */
7279 if (recheck_faces)
7280 it->stop_charpos = IT_CHARPOS (*it);
7281 }
7282 break;
7283
7284 case GET_FROM_STRING:
7285 /* Current display element is a character from a Lisp string. */
7286 eassert (it->s == NULL && STRINGP (it->string));
7287 /* Don't advance past string end. These conditions are true
7288 when set_iterator_to_next is called at the end of
7289 get_next_display_element, in which case the Lisp string is
7290 already exhausted, and all we want is pop the iterator
7291 stack. */
7292 if (it->current.overlay_string_index >= 0)
7293 {
7294 /* This is an overlay string, so there's no padding with
7295 spaces, and the number of characters in the string is
7296 where the string ends. */
7297 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7298 goto consider_string_end;
7299 }
7300 else
7301 {
7302 /* Not an overlay string. There could be padding, so test
7303 against it->end_charpos. */
7304 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7305 goto consider_string_end;
7306 }
7307 if (it->cmp_it.id >= 0)
7308 {
7309 int i;
7310
7311 if (! it->bidi_p)
7312 {
7313 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7314 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7315 if (it->cmp_it.to < it->cmp_it.nglyphs)
7316 it->cmp_it.from = it->cmp_it.to;
7317 else
7318 {
7319 it->cmp_it.id = -1;
7320 composition_compute_stop_pos (&it->cmp_it,
7321 IT_STRING_CHARPOS (*it),
7322 IT_STRING_BYTEPOS (*it),
7323 it->end_charpos, it->string);
7324 }
7325 }
7326 else if (! it->cmp_it.reversed_p)
7327 {
7328 for (i = 0; i < it->cmp_it.nchars; i++)
7329 bidi_move_to_visually_next (&it->bidi_it);
7330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7331 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7332
7333 if (it->cmp_it.to < it->cmp_it.nglyphs)
7334 it->cmp_it.from = it->cmp_it.to;
7335 else
7336 {
7337 ptrdiff_t stop = it->end_charpos;
7338 if (it->bidi_it.scan_dir < 0)
7339 stop = -1;
7340 composition_compute_stop_pos (&it->cmp_it,
7341 IT_STRING_CHARPOS (*it),
7342 IT_STRING_BYTEPOS (*it), stop,
7343 it->string);
7344 }
7345 }
7346 else
7347 {
7348 for (i = 0; i < it->cmp_it.nchars; i++)
7349 bidi_move_to_visually_next (&it->bidi_it);
7350 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7352 if (it->cmp_it.from > 0)
7353 it->cmp_it.to = it->cmp_it.from;
7354 else
7355 {
7356 ptrdiff_t stop = it->end_charpos;
7357 if (it->bidi_it.scan_dir < 0)
7358 stop = -1;
7359 composition_compute_stop_pos (&it->cmp_it,
7360 IT_STRING_CHARPOS (*it),
7361 IT_STRING_BYTEPOS (*it), stop,
7362 it->string);
7363 }
7364 }
7365 }
7366 else
7367 {
7368 if (!it->bidi_p
7369 /* If the string position is beyond string's end, it
7370 means next_element_from_string is padding the string
7371 with blanks, in which case we bypass the bidi
7372 iterator, because it cannot deal with such virtual
7373 characters. */
7374 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7375 {
7376 IT_STRING_BYTEPOS (*it) += it->len;
7377 IT_STRING_CHARPOS (*it) += 1;
7378 }
7379 else
7380 {
7381 int prev_scan_dir = it->bidi_it.scan_dir;
7382
7383 bidi_move_to_visually_next (&it->bidi_it);
7384 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7385 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7386 if (prev_scan_dir != it->bidi_it.scan_dir)
7387 {
7388 ptrdiff_t stop = it->end_charpos;
7389
7390 if (it->bidi_it.scan_dir < 0)
7391 stop = -1;
7392 composition_compute_stop_pos (&it->cmp_it,
7393 IT_STRING_CHARPOS (*it),
7394 IT_STRING_BYTEPOS (*it), stop,
7395 it->string);
7396 }
7397 }
7398 }
7399
7400 consider_string_end:
7401
7402 if (it->current.overlay_string_index >= 0)
7403 {
7404 /* IT->string is an overlay string. Advance to the
7405 next, if there is one. */
7406 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7407 {
7408 it->ellipsis_p = 0;
7409 next_overlay_string (it);
7410 if (it->ellipsis_p)
7411 setup_for_ellipsis (it, 0);
7412 }
7413 }
7414 else
7415 {
7416 /* IT->string is not an overlay string. If we reached
7417 its end, and there is something on IT->stack, proceed
7418 with what is on the stack. This can be either another
7419 string, this time an overlay string, or a buffer. */
7420 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7421 && it->sp > 0)
7422 {
7423 pop_it (it);
7424 if (it->method == GET_FROM_STRING)
7425 goto consider_string_end;
7426 }
7427 }
7428 break;
7429
7430 case GET_FROM_IMAGE:
7431 case GET_FROM_STRETCH:
7432 /* The position etc with which we have to proceed are on
7433 the stack. The position may be at the end of a string,
7434 if the `display' property takes up the whole string. */
7435 eassert (it->sp > 0);
7436 pop_it (it);
7437 if (it->method == GET_FROM_STRING)
7438 goto consider_string_end;
7439 break;
7440
7441 default:
7442 /* There are no other methods defined, so this should be a bug. */
7443 emacs_abort ();
7444 }
7445
7446 eassert (it->method != GET_FROM_STRING
7447 || (STRINGP (it->string)
7448 && IT_STRING_CHARPOS (*it) >= 0));
7449 }
7450
7451 /* Load IT's display element fields with information about the next
7452 display element which comes from a display table entry or from the
7453 result of translating a control character to one of the forms `^C'
7454 or `\003'.
7455
7456 IT->dpvec holds the glyphs to return as characters.
7457 IT->saved_face_id holds the face id before the display vector--it
7458 is restored into IT->face_id in set_iterator_to_next. */
7459
7460 static int
7461 next_element_from_display_vector (struct it *it)
7462 {
7463 Lisp_Object gc;
7464 int prev_face_id = it->face_id;
7465 int next_face_id;
7466
7467 /* Precondition. */
7468 eassert (it->dpvec && it->current.dpvec_index >= 0);
7469
7470 it->face_id = it->saved_face_id;
7471
7472 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7473 That seemed totally bogus - so I changed it... */
7474 gc = it->dpvec[it->current.dpvec_index];
7475
7476 if (GLYPH_CODE_P (gc))
7477 {
7478 struct face *this_face, *prev_face, *next_face;
7479
7480 it->c = GLYPH_CODE_CHAR (gc);
7481 it->len = CHAR_BYTES (it->c);
7482
7483 /* The entry may contain a face id to use. Such a face id is
7484 the id of a Lisp face, not a realized face. A face id of
7485 zero means no face is specified. */
7486 if (it->dpvec_face_id >= 0)
7487 it->face_id = it->dpvec_face_id;
7488 else
7489 {
7490 int lface_id = GLYPH_CODE_FACE (gc);
7491 if (lface_id > 0)
7492 it->face_id = merge_faces (it->f, Qt, lface_id,
7493 it->saved_face_id);
7494 }
7495
7496 /* Glyphs in the display vector could have the box face, so we
7497 need to set the related flags in the iterator, as
7498 appropriate. */
7499 this_face = FACE_FROM_ID (it->f, it->face_id);
7500 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7501
7502 /* Is this character the first character of a box-face run? */
7503 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7504 && (!prev_face
7505 || prev_face->box == FACE_NO_BOX));
7506
7507 /* For the last character of the box-face run, we need to look
7508 either at the next glyph from the display vector, or at the
7509 face we saw before the display vector. */
7510 next_face_id = it->saved_face_id;
7511 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7512 {
7513 if (it->dpvec_face_id >= 0)
7514 next_face_id = it->dpvec_face_id;
7515 else
7516 {
7517 int lface_id =
7518 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7519
7520 if (lface_id > 0)
7521 next_face_id = merge_faces (it->f, Qt, lface_id,
7522 it->saved_face_id);
7523 }
7524 }
7525 next_face = FACE_FROM_ID (it->f, next_face_id);
7526 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7527 && (!next_face
7528 || next_face->box == FACE_NO_BOX));
7529 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7530 }
7531 else
7532 /* Display table entry is invalid. Return a space. */
7533 it->c = ' ', it->len = 1;
7534
7535 /* Don't change position and object of the iterator here. They are
7536 still the values of the character that had this display table
7537 entry or was translated, and that's what we want. */
7538 it->what = IT_CHARACTER;
7539 return 1;
7540 }
7541
7542 /* Get the first element of string/buffer in the visual order, after
7543 being reseated to a new position in a string or a buffer. */
7544 static void
7545 get_visually_first_element (struct it *it)
7546 {
7547 int string_p = STRINGP (it->string) || it->s;
7548 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7549 ptrdiff_t bob = (string_p ? 0 : BEGV);
7550
7551 if (STRINGP (it->string))
7552 {
7553 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7554 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7555 }
7556 else
7557 {
7558 it->bidi_it.charpos = IT_CHARPOS (*it);
7559 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7560 }
7561
7562 if (it->bidi_it.charpos == eob)
7563 {
7564 /* Nothing to do, but reset the FIRST_ELT flag, like
7565 bidi_paragraph_init does, because we are not going to
7566 call it. */
7567 it->bidi_it.first_elt = 0;
7568 }
7569 else if (it->bidi_it.charpos == bob
7570 || (!string_p
7571 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7572 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7573 {
7574 /* If we are at the beginning of a line/string, we can produce
7575 the next element right away. */
7576 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7577 bidi_move_to_visually_next (&it->bidi_it);
7578 }
7579 else
7580 {
7581 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7582
7583 /* We need to prime the bidi iterator starting at the line's or
7584 string's beginning, before we will be able to produce the
7585 next element. */
7586 if (string_p)
7587 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7588 else
7589 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7590 IT_BYTEPOS (*it), -1,
7591 &it->bidi_it.bytepos);
7592 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7593 do
7594 {
7595 /* Now return to buffer/string position where we were asked
7596 to get the next display element, and produce that. */
7597 bidi_move_to_visually_next (&it->bidi_it);
7598 }
7599 while (it->bidi_it.bytepos != orig_bytepos
7600 && it->bidi_it.charpos < eob);
7601 }
7602
7603 /* Adjust IT's position information to where we ended up. */
7604 if (STRINGP (it->string))
7605 {
7606 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7607 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7608 }
7609 else
7610 {
7611 IT_CHARPOS (*it) = it->bidi_it.charpos;
7612 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7613 }
7614
7615 if (STRINGP (it->string) || !it->s)
7616 {
7617 ptrdiff_t stop, charpos, bytepos;
7618
7619 if (STRINGP (it->string))
7620 {
7621 eassert (!it->s);
7622 stop = SCHARS (it->string);
7623 if (stop > it->end_charpos)
7624 stop = it->end_charpos;
7625 charpos = IT_STRING_CHARPOS (*it);
7626 bytepos = IT_STRING_BYTEPOS (*it);
7627 }
7628 else
7629 {
7630 stop = it->end_charpos;
7631 charpos = IT_CHARPOS (*it);
7632 bytepos = IT_BYTEPOS (*it);
7633 }
7634 if (it->bidi_it.scan_dir < 0)
7635 stop = -1;
7636 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7637 it->string);
7638 }
7639 }
7640
7641 /* Load IT with the next display element from Lisp string IT->string.
7642 IT->current.string_pos is the current position within the string.
7643 If IT->current.overlay_string_index >= 0, the Lisp string is an
7644 overlay string. */
7645
7646 static int
7647 next_element_from_string (struct it *it)
7648 {
7649 struct text_pos position;
7650
7651 eassert (STRINGP (it->string));
7652 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7653 eassert (IT_STRING_CHARPOS (*it) >= 0);
7654 position = it->current.string_pos;
7655
7656 /* With bidi reordering, the character to display might not be the
7657 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7658 that we were reseat()ed to a new string, whose paragraph
7659 direction is not known. */
7660 if (it->bidi_p && it->bidi_it.first_elt)
7661 {
7662 get_visually_first_element (it);
7663 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7664 }
7665
7666 /* Time to check for invisible text? */
7667 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7668 {
7669 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7670 {
7671 if (!(!it->bidi_p
7672 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7673 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7674 {
7675 /* With bidi non-linear iteration, we could find
7676 ourselves far beyond the last computed stop_charpos,
7677 with several other stop positions in between that we
7678 missed. Scan them all now, in buffer's logical
7679 order, until we find and handle the last stop_charpos
7680 that precedes our current position. */
7681 handle_stop_backwards (it, it->stop_charpos);
7682 return GET_NEXT_DISPLAY_ELEMENT (it);
7683 }
7684 else
7685 {
7686 if (it->bidi_p)
7687 {
7688 /* Take note of the stop position we just moved
7689 across, for when we will move back across it. */
7690 it->prev_stop = it->stop_charpos;
7691 /* If we are at base paragraph embedding level, take
7692 note of the last stop position seen at this
7693 level. */
7694 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7695 it->base_level_stop = it->stop_charpos;
7696 }
7697 handle_stop (it);
7698
7699 /* Since a handler may have changed IT->method, we must
7700 recurse here. */
7701 return GET_NEXT_DISPLAY_ELEMENT (it);
7702 }
7703 }
7704 else if (it->bidi_p
7705 /* If we are before prev_stop, we may have overstepped
7706 on our way backwards a stop_pos, and if so, we need
7707 to handle that stop_pos. */
7708 && IT_STRING_CHARPOS (*it) < it->prev_stop
7709 /* We can sometimes back up for reasons that have nothing
7710 to do with bidi reordering. E.g., compositions. The
7711 code below is only needed when we are above the base
7712 embedding level, so test for that explicitly. */
7713 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7714 {
7715 /* If we lost track of base_level_stop, we have no better
7716 place for handle_stop_backwards to start from than string
7717 beginning. This happens, e.g., when we were reseated to
7718 the previous screenful of text by vertical-motion. */
7719 if (it->base_level_stop <= 0
7720 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7721 it->base_level_stop = 0;
7722 handle_stop_backwards (it, it->base_level_stop);
7723 return GET_NEXT_DISPLAY_ELEMENT (it);
7724 }
7725 }
7726
7727 if (it->current.overlay_string_index >= 0)
7728 {
7729 /* Get the next character from an overlay string. In overlay
7730 strings, there is no field width or padding with spaces to
7731 do. */
7732 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7733 {
7734 it->what = IT_EOB;
7735 return 0;
7736 }
7737 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7738 IT_STRING_BYTEPOS (*it),
7739 it->bidi_it.scan_dir < 0
7740 ? -1
7741 : SCHARS (it->string))
7742 && next_element_from_composition (it))
7743 {
7744 return 1;
7745 }
7746 else if (STRING_MULTIBYTE (it->string))
7747 {
7748 const unsigned char *s = (SDATA (it->string)
7749 + IT_STRING_BYTEPOS (*it));
7750 it->c = string_char_and_length (s, &it->len);
7751 }
7752 else
7753 {
7754 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7755 it->len = 1;
7756 }
7757 }
7758 else
7759 {
7760 /* Get the next character from a Lisp string that is not an
7761 overlay string. Such strings come from the mode line, for
7762 example. We may have to pad with spaces, or truncate the
7763 string. See also next_element_from_c_string. */
7764 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7765 {
7766 it->what = IT_EOB;
7767 return 0;
7768 }
7769 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7770 {
7771 /* Pad with spaces. */
7772 it->c = ' ', it->len = 1;
7773 CHARPOS (position) = BYTEPOS (position) = -1;
7774 }
7775 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7776 IT_STRING_BYTEPOS (*it),
7777 it->bidi_it.scan_dir < 0
7778 ? -1
7779 : it->string_nchars)
7780 && next_element_from_composition (it))
7781 {
7782 return 1;
7783 }
7784 else if (STRING_MULTIBYTE (it->string))
7785 {
7786 const unsigned char *s = (SDATA (it->string)
7787 + IT_STRING_BYTEPOS (*it));
7788 it->c = string_char_and_length (s, &it->len);
7789 }
7790 else
7791 {
7792 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7793 it->len = 1;
7794 }
7795 }
7796
7797 /* Record what we have and where it came from. */
7798 it->what = IT_CHARACTER;
7799 it->object = it->string;
7800 it->position = position;
7801 return 1;
7802 }
7803
7804
7805 /* Load IT with next display element from C string IT->s.
7806 IT->string_nchars is the maximum number of characters to return
7807 from the string. IT->end_charpos may be greater than
7808 IT->string_nchars when this function is called, in which case we
7809 may have to return padding spaces. Value is zero if end of string
7810 reached, including padding spaces. */
7811
7812 static int
7813 next_element_from_c_string (struct it *it)
7814 {
7815 bool success_p = true;
7816
7817 eassert (it->s);
7818 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7819 it->what = IT_CHARACTER;
7820 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7821 it->object = Qnil;
7822
7823 /* With bidi reordering, the character to display might not be the
7824 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7825 we were reseated to a new string, whose paragraph direction is
7826 not known. */
7827 if (it->bidi_p && it->bidi_it.first_elt)
7828 get_visually_first_element (it);
7829
7830 /* IT's position can be greater than IT->string_nchars in case a
7831 field width or precision has been specified when the iterator was
7832 initialized. */
7833 if (IT_CHARPOS (*it) >= it->end_charpos)
7834 {
7835 /* End of the game. */
7836 it->what = IT_EOB;
7837 success_p = 0;
7838 }
7839 else if (IT_CHARPOS (*it) >= it->string_nchars)
7840 {
7841 /* Pad with spaces. */
7842 it->c = ' ', it->len = 1;
7843 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7844 }
7845 else if (it->multibyte_p)
7846 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7847 else
7848 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7849
7850 return success_p;
7851 }
7852
7853
7854 /* Set up IT to return characters from an ellipsis, if appropriate.
7855 The definition of the ellipsis glyphs may come from a display table
7856 entry. This function fills IT with the first glyph from the
7857 ellipsis if an ellipsis is to be displayed. */
7858
7859 static int
7860 next_element_from_ellipsis (struct it *it)
7861 {
7862 if (it->selective_display_ellipsis_p)
7863 setup_for_ellipsis (it, it->len);
7864 else
7865 {
7866 /* The face at the current position may be different from the
7867 face we find after the invisible text. Remember what it
7868 was in IT->saved_face_id, and signal that it's there by
7869 setting face_before_selective_p. */
7870 it->saved_face_id = it->face_id;
7871 it->method = GET_FROM_BUFFER;
7872 it->object = it->w->contents;
7873 reseat_at_next_visible_line_start (it, 1);
7874 it->face_before_selective_p = true;
7875 }
7876
7877 return GET_NEXT_DISPLAY_ELEMENT (it);
7878 }
7879
7880
7881 /* Deliver an image display element. The iterator IT is already
7882 filled with image information (done in handle_display_prop). Value
7883 is always 1. */
7884
7885
7886 static int
7887 next_element_from_image (struct it *it)
7888 {
7889 it->what = IT_IMAGE;
7890 it->ignore_overlay_strings_at_pos_p = 0;
7891 return 1;
7892 }
7893
7894
7895 /* Fill iterator IT with next display element from a stretch glyph
7896 property. IT->object is the value of the text property. Value is
7897 always 1. */
7898
7899 static int
7900 next_element_from_stretch (struct it *it)
7901 {
7902 it->what = IT_STRETCH;
7903 return 1;
7904 }
7905
7906 /* Scan backwards from IT's current position until we find a stop
7907 position, or until BEGV. This is called when we find ourself
7908 before both the last known prev_stop and base_level_stop while
7909 reordering bidirectional text. */
7910
7911 static void
7912 compute_stop_pos_backwards (struct it *it)
7913 {
7914 const int SCAN_BACK_LIMIT = 1000;
7915 struct text_pos pos;
7916 struct display_pos save_current = it->current;
7917 struct text_pos save_position = it->position;
7918 ptrdiff_t charpos = IT_CHARPOS (*it);
7919 ptrdiff_t where_we_are = charpos;
7920 ptrdiff_t save_stop_pos = it->stop_charpos;
7921 ptrdiff_t save_end_pos = it->end_charpos;
7922
7923 eassert (NILP (it->string) && !it->s);
7924 eassert (it->bidi_p);
7925 it->bidi_p = 0;
7926 do
7927 {
7928 it->end_charpos = min (charpos + 1, ZV);
7929 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7930 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7931 reseat_1 (it, pos, 0);
7932 compute_stop_pos (it);
7933 /* We must advance forward, right? */
7934 if (it->stop_charpos <= charpos)
7935 emacs_abort ();
7936 }
7937 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7938
7939 if (it->stop_charpos <= where_we_are)
7940 it->prev_stop = it->stop_charpos;
7941 else
7942 it->prev_stop = BEGV;
7943 it->bidi_p = true;
7944 it->current = save_current;
7945 it->position = save_position;
7946 it->stop_charpos = save_stop_pos;
7947 it->end_charpos = save_end_pos;
7948 }
7949
7950 /* Scan forward from CHARPOS in the current buffer/string, until we
7951 find a stop position > current IT's position. Then handle the stop
7952 position before that. This is called when we bump into a stop
7953 position while reordering bidirectional text. CHARPOS should be
7954 the last previously processed stop_pos (or BEGV/0, if none were
7955 processed yet) whose position is less that IT's current
7956 position. */
7957
7958 static void
7959 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7960 {
7961 int bufp = !STRINGP (it->string);
7962 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7963 struct display_pos save_current = it->current;
7964 struct text_pos save_position = it->position;
7965 struct text_pos pos1;
7966 ptrdiff_t next_stop;
7967
7968 /* Scan in strict logical order. */
7969 eassert (it->bidi_p);
7970 it->bidi_p = 0;
7971 do
7972 {
7973 it->prev_stop = charpos;
7974 if (bufp)
7975 {
7976 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7977 reseat_1 (it, pos1, 0);
7978 }
7979 else
7980 it->current.string_pos = string_pos (charpos, it->string);
7981 compute_stop_pos (it);
7982 /* We must advance forward, right? */
7983 if (it->stop_charpos <= it->prev_stop)
7984 emacs_abort ();
7985 charpos = it->stop_charpos;
7986 }
7987 while (charpos <= where_we_are);
7988
7989 it->bidi_p = true;
7990 it->current = save_current;
7991 it->position = save_position;
7992 next_stop = it->stop_charpos;
7993 it->stop_charpos = it->prev_stop;
7994 handle_stop (it);
7995 it->stop_charpos = next_stop;
7996 }
7997
7998 /* Load IT with the next display element from current_buffer. Value
7999 is zero if end of buffer reached. IT->stop_charpos is the next
8000 position at which to stop and check for text properties or buffer
8001 end. */
8002
8003 static int
8004 next_element_from_buffer (struct it *it)
8005 {
8006 bool success_p = true;
8007
8008 eassert (IT_CHARPOS (*it) >= BEGV);
8009 eassert (NILP (it->string) && !it->s);
8010 eassert (!it->bidi_p
8011 || (EQ (it->bidi_it.string.lstring, Qnil)
8012 && it->bidi_it.string.s == NULL));
8013
8014 /* With bidi reordering, the character to display might not be the
8015 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8016 we were reseat()ed to a new buffer position, which is potentially
8017 a different paragraph. */
8018 if (it->bidi_p && it->bidi_it.first_elt)
8019 {
8020 get_visually_first_element (it);
8021 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8022 }
8023
8024 if (IT_CHARPOS (*it) >= it->stop_charpos)
8025 {
8026 if (IT_CHARPOS (*it) >= it->end_charpos)
8027 {
8028 int overlay_strings_follow_p;
8029
8030 /* End of the game, except when overlay strings follow that
8031 haven't been returned yet. */
8032 if (it->overlay_strings_at_end_processed_p)
8033 overlay_strings_follow_p = 0;
8034 else
8035 {
8036 it->overlay_strings_at_end_processed_p = true;
8037 overlay_strings_follow_p = get_overlay_strings (it, 0);
8038 }
8039
8040 if (overlay_strings_follow_p)
8041 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8042 else
8043 {
8044 it->what = IT_EOB;
8045 it->position = it->current.pos;
8046 success_p = 0;
8047 }
8048 }
8049 else if (!(!it->bidi_p
8050 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8051 || IT_CHARPOS (*it) == it->stop_charpos))
8052 {
8053 /* With bidi non-linear iteration, we could find ourselves
8054 far beyond the last computed stop_charpos, with several
8055 other stop positions in between that we missed. Scan
8056 them all now, in buffer's logical order, until we find
8057 and handle the last stop_charpos that precedes our
8058 current position. */
8059 handle_stop_backwards (it, it->stop_charpos);
8060 return GET_NEXT_DISPLAY_ELEMENT (it);
8061 }
8062 else
8063 {
8064 if (it->bidi_p)
8065 {
8066 /* Take note of the stop position we just moved across,
8067 for when we will move back across it. */
8068 it->prev_stop = it->stop_charpos;
8069 /* If we are at base paragraph embedding level, take
8070 note of the last stop position seen at this
8071 level. */
8072 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8073 it->base_level_stop = it->stop_charpos;
8074 }
8075 handle_stop (it);
8076 return GET_NEXT_DISPLAY_ELEMENT (it);
8077 }
8078 }
8079 else if (it->bidi_p
8080 /* If we are before prev_stop, we may have overstepped on
8081 our way backwards a stop_pos, and if so, we need to
8082 handle that stop_pos. */
8083 && IT_CHARPOS (*it) < it->prev_stop
8084 /* We can sometimes back up for reasons that have nothing
8085 to do with bidi reordering. E.g., compositions. The
8086 code below is only needed when we are above the base
8087 embedding level, so test for that explicitly. */
8088 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8089 {
8090 if (it->base_level_stop <= 0
8091 || IT_CHARPOS (*it) < it->base_level_stop)
8092 {
8093 /* If we lost track of base_level_stop, we need to find
8094 prev_stop by looking backwards. This happens, e.g., when
8095 we were reseated to the previous screenful of text by
8096 vertical-motion. */
8097 it->base_level_stop = BEGV;
8098 compute_stop_pos_backwards (it);
8099 handle_stop_backwards (it, it->prev_stop);
8100 }
8101 else
8102 handle_stop_backwards (it, it->base_level_stop);
8103 return GET_NEXT_DISPLAY_ELEMENT (it);
8104 }
8105 else
8106 {
8107 /* No face changes, overlays etc. in sight, so just return a
8108 character from current_buffer. */
8109 unsigned char *p;
8110 ptrdiff_t stop;
8111
8112 /* Maybe run the redisplay end trigger hook. Performance note:
8113 This doesn't seem to cost measurable time. */
8114 if (it->redisplay_end_trigger_charpos
8115 && it->glyph_row
8116 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8117 run_redisplay_end_trigger_hook (it);
8118
8119 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8120 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8121 stop)
8122 && next_element_from_composition (it))
8123 {
8124 return 1;
8125 }
8126
8127 /* Get the next character, maybe multibyte. */
8128 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8129 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8130 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8131 else
8132 it->c = *p, it->len = 1;
8133
8134 /* Record what we have and where it came from. */
8135 it->what = IT_CHARACTER;
8136 it->object = it->w->contents;
8137 it->position = it->current.pos;
8138
8139 /* Normally we return the character found above, except when we
8140 really want to return an ellipsis for selective display. */
8141 if (it->selective)
8142 {
8143 if (it->c == '\n')
8144 {
8145 /* A value of selective > 0 means hide lines indented more
8146 than that number of columns. */
8147 if (it->selective > 0
8148 && IT_CHARPOS (*it) + 1 < ZV
8149 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8150 IT_BYTEPOS (*it) + 1,
8151 it->selective))
8152 {
8153 success_p = next_element_from_ellipsis (it);
8154 it->dpvec_char_len = -1;
8155 }
8156 }
8157 else if (it->c == '\r' && it->selective == -1)
8158 {
8159 /* A value of selective == -1 means that everything from the
8160 CR to the end of the line is invisible, with maybe an
8161 ellipsis displayed for it. */
8162 success_p = next_element_from_ellipsis (it);
8163 it->dpvec_char_len = -1;
8164 }
8165 }
8166 }
8167
8168 /* Value is zero if end of buffer reached. */
8169 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8170 return success_p;
8171 }
8172
8173
8174 /* Run the redisplay end trigger hook for IT. */
8175
8176 static void
8177 run_redisplay_end_trigger_hook (struct it *it)
8178 {
8179 Lisp_Object args[3];
8180
8181 /* IT->glyph_row should be non-null, i.e. we should be actually
8182 displaying something, or otherwise we should not run the hook. */
8183 eassert (it->glyph_row);
8184
8185 /* Set up hook arguments. */
8186 args[0] = Qredisplay_end_trigger_functions;
8187 args[1] = it->window;
8188 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8189 it->redisplay_end_trigger_charpos = 0;
8190
8191 /* Since we are *trying* to run these functions, don't try to run
8192 them again, even if they get an error. */
8193 wset_redisplay_end_trigger (it->w, Qnil);
8194 Frun_hook_with_args (3, args);
8195
8196 /* Notice if it changed the face of the character we are on. */
8197 handle_face_prop (it);
8198 }
8199
8200
8201 /* Deliver a composition display element. Unlike the other
8202 next_element_from_XXX, this function is not registered in the array
8203 get_next_element[]. It is called from next_element_from_buffer and
8204 next_element_from_string when necessary. */
8205
8206 static int
8207 next_element_from_composition (struct it *it)
8208 {
8209 it->what = IT_COMPOSITION;
8210 it->len = it->cmp_it.nbytes;
8211 if (STRINGP (it->string))
8212 {
8213 if (it->c < 0)
8214 {
8215 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8216 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8217 return 0;
8218 }
8219 it->position = it->current.string_pos;
8220 it->object = it->string;
8221 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8222 IT_STRING_BYTEPOS (*it), it->string);
8223 }
8224 else
8225 {
8226 if (it->c < 0)
8227 {
8228 IT_CHARPOS (*it) += it->cmp_it.nchars;
8229 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8230 if (it->bidi_p)
8231 {
8232 if (it->bidi_it.new_paragraph)
8233 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8234 /* Resync the bidi iterator with IT's new position.
8235 FIXME: this doesn't support bidirectional text. */
8236 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8237 bidi_move_to_visually_next (&it->bidi_it);
8238 }
8239 return 0;
8240 }
8241 it->position = it->current.pos;
8242 it->object = it->w->contents;
8243 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8244 IT_BYTEPOS (*it), Qnil);
8245 }
8246 return 1;
8247 }
8248
8249
8250 \f
8251 /***********************************************************************
8252 Moving an iterator without producing glyphs
8253 ***********************************************************************/
8254
8255 /* Check if iterator is at a position corresponding to a valid buffer
8256 position after some move_it_ call. */
8257
8258 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8259 ((it)->method == GET_FROM_STRING \
8260 ? IT_STRING_CHARPOS (*it) == 0 \
8261 : 1)
8262
8263
8264 /* Move iterator IT to a specified buffer or X position within one
8265 line on the display without producing glyphs.
8266
8267 OP should be a bit mask including some or all of these bits:
8268 MOVE_TO_X: Stop upon reaching x-position TO_X.
8269 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8270 Regardless of OP's value, stop upon reaching the end of the display line.
8271
8272 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8273 This means, in particular, that TO_X includes window's horizontal
8274 scroll amount.
8275
8276 The return value has several possible values that
8277 say what condition caused the scan to stop:
8278
8279 MOVE_POS_MATCH_OR_ZV
8280 - when TO_POS or ZV was reached.
8281
8282 MOVE_X_REACHED
8283 -when TO_X was reached before TO_POS or ZV were reached.
8284
8285 MOVE_LINE_CONTINUED
8286 - when we reached the end of the display area and the line must
8287 be continued.
8288
8289 MOVE_LINE_TRUNCATED
8290 - when we reached the end of the display area and the line is
8291 truncated.
8292
8293 MOVE_NEWLINE_OR_CR
8294 - when we stopped at a line end, i.e. a newline or a CR and selective
8295 display is on. */
8296
8297 static enum move_it_result
8298 move_it_in_display_line_to (struct it *it,
8299 ptrdiff_t to_charpos, int to_x,
8300 enum move_operation_enum op)
8301 {
8302 enum move_it_result result = MOVE_UNDEFINED;
8303 struct glyph_row *saved_glyph_row;
8304 struct it wrap_it, atpos_it, atx_it, ppos_it;
8305 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8306 void *ppos_data = NULL;
8307 int may_wrap = 0;
8308 enum it_method prev_method = it->method;
8309 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8310 int saw_smaller_pos = prev_pos < to_charpos;
8311
8312 /* Don't produce glyphs in produce_glyphs. */
8313 saved_glyph_row = it->glyph_row;
8314 it->glyph_row = NULL;
8315
8316 /* Use wrap_it to save a copy of IT wherever a word wrap could
8317 occur. Use atpos_it to save a copy of IT at the desired buffer
8318 position, if found, so that we can scan ahead and check if the
8319 word later overshoots the window edge. Use atx_it similarly, for
8320 pixel positions. */
8321 wrap_it.sp = -1;
8322 atpos_it.sp = -1;
8323 atx_it.sp = -1;
8324
8325 /* Use ppos_it under bidi reordering to save a copy of IT for the
8326 position > CHARPOS that is the closest to CHARPOS. We restore
8327 that position in IT when we have scanned the entire display line
8328 without finding a match for CHARPOS and all the character
8329 positions are greater than CHARPOS. */
8330 if (it->bidi_p)
8331 {
8332 SAVE_IT (ppos_it, *it, ppos_data);
8333 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8334 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8335 SAVE_IT (ppos_it, *it, ppos_data);
8336 }
8337
8338 #define BUFFER_POS_REACHED_P() \
8339 ((op & MOVE_TO_POS) != 0 \
8340 && BUFFERP (it->object) \
8341 && (IT_CHARPOS (*it) == to_charpos \
8342 || ((!it->bidi_p \
8343 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8344 && IT_CHARPOS (*it) > to_charpos) \
8345 || (it->what == IT_COMPOSITION \
8346 && ((IT_CHARPOS (*it) > to_charpos \
8347 && to_charpos >= it->cmp_it.charpos) \
8348 || (IT_CHARPOS (*it) < to_charpos \
8349 && to_charpos <= it->cmp_it.charpos)))) \
8350 && (it->method == GET_FROM_BUFFER \
8351 || (it->method == GET_FROM_DISPLAY_VECTOR \
8352 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8353
8354 /* If there's a line-/wrap-prefix, handle it. */
8355 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8356 && it->current_y < it->last_visible_y)
8357 handle_line_prefix (it);
8358
8359 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8360 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8361
8362 while (1)
8363 {
8364 int x, i, ascent = 0, descent = 0;
8365
8366 /* Utility macro to reset an iterator with x, ascent, and descent. */
8367 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8368 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8369 (IT)->max_descent = descent)
8370
8371 /* Stop if we move beyond TO_CHARPOS (after an image or a
8372 display string or stretch glyph). */
8373 if ((op & MOVE_TO_POS) != 0
8374 && BUFFERP (it->object)
8375 && it->method == GET_FROM_BUFFER
8376 && (((!it->bidi_p
8377 /* When the iterator is at base embedding level, we
8378 are guaranteed that characters are delivered for
8379 display in strictly increasing order of their
8380 buffer positions. */
8381 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8382 && IT_CHARPOS (*it) > to_charpos)
8383 || (it->bidi_p
8384 && (prev_method == GET_FROM_IMAGE
8385 || prev_method == GET_FROM_STRETCH
8386 || prev_method == GET_FROM_STRING)
8387 /* Passed TO_CHARPOS from left to right. */
8388 && ((prev_pos < to_charpos
8389 && IT_CHARPOS (*it) > to_charpos)
8390 /* Passed TO_CHARPOS from right to left. */
8391 || (prev_pos > to_charpos
8392 && IT_CHARPOS (*it) < to_charpos)))))
8393 {
8394 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8395 {
8396 result = MOVE_POS_MATCH_OR_ZV;
8397 break;
8398 }
8399 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8400 /* If wrap_it is valid, the current position might be in a
8401 word that is wrapped. So, save the iterator in
8402 atpos_it and continue to see if wrapping happens. */
8403 SAVE_IT (atpos_it, *it, atpos_data);
8404 }
8405
8406 /* Stop when ZV reached.
8407 We used to stop here when TO_CHARPOS reached as well, but that is
8408 too soon if this glyph does not fit on this line. So we handle it
8409 explicitly below. */
8410 if (!get_next_display_element (it))
8411 {
8412 result = MOVE_POS_MATCH_OR_ZV;
8413 break;
8414 }
8415
8416 if (it->line_wrap == TRUNCATE)
8417 {
8418 if (BUFFER_POS_REACHED_P ())
8419 {
8420 result = MOVE_POS_MATCH_OR_ZV;
8421 break;
8422 }
8423 }
8424 else
8425 {
8426 if (it->line_wrap == WORD_WRAP)
8427 {
8428 if (IT_DISPLAYING_WHITESPACE (it))
8429 may_wrap = 1;
8430 else if (may_wrap)
8431 {
8432 /* We have reached a glyph that follows one or more
8433 whitespace characters. If the position is
8434 already found, we are done. */
8435 if (atpos_it.sp >= 0)
8436 {
8437 RESTORE_IT (it, &atpos_it, atpos_data);
8438 result = MOVE_POS_MATCH_OR_ZV;
8439 goto done;
8440 }
8441 if (atx_it.sp >= 0)
8442 {
8443 RESTORE_IT (it, &atx_it, atx_data);
8444 result = MOVE_X_REACHED;
8445 goto done;
8446 }
8447 /* Otherwise, we can wrap here. */
8448 SAVE_IT (wrap_it, *it, wrap_data);
8449 may_wrap = 0;
8450 }
8451 }
8452 }
8453
8454 /* Remember the line height for the current line, in case
8455 the next element doesn't fit on the line. */
8456 ascent = it->max_ascent;
8457 descent = it->max_descent;
8458
8459 /* The call to produce_glyphs will get the metrics of the
8460 display element IT is loaded with. Record the x-position
8461 before this display element, in case it doesn't fit on the
8462 line. */
8463 x = it->current_x;
8464
8465 PRODUCE_GLYPHS (it);
8466
8467 if (it->area != TEXT_AREA)
8468 {
8469 prev_method = it->method;
8470 if (it->method == GET_FROM_BUFFER)
8471 prev_pos = IT_CHARPOS (*it);
8472 set_iterator_to_next (it, 1);
8473 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8474 SET_TEXT_POS (this_line_min_pos,
8475 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8476 if (it->bidi_p
8477 && (op & MOVE_TO_POS)
8478 && IT_CHARPOS (*it) > to_charpos
8479 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8480 SAVE_IT (ppos_it, *it, ppos_data);
8481 continue;
8482 }
8483
8484 /* The number of glyphs we get back in IT->nglyphs will normally
8485 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8486 character on a terminal frame, or (iii) a line end. For the
8487 second case, IT->nglyphs - 1 padding glyphs will be present.
8488 (On X frames, there is only one glyph produced for a
8489 composite character.)
8490
8491 The behavior implemented below means, for continuation lines,
8492 that as many spaces of a TAB as fit on the current line are
8493 displayed there. For terminal frames, as many glyphs of a
8494 multi-glyph character are displayed in the current line, too.
8495 This is what the old redisplay code did, and we keep it that
8496 way. Under X, the whole shape of a complex character must
8497 fit on the line or it will be completely displayed in the
8498 next line.
8499
8500 Note that both for tabs and padding glyphs, all glyphs have
8501 the same width. */
8502 if (it->nglyphs)
8503 {
8504 /* More than one glyph or glyph doesn't fit on line. All
8505 glyphs have the same width. */
8506 int single_glyph_width = it->pixel_width / it->nglyphs;
8507 int new_x;
8508 int x_before_this_char = x;
8509 int hpos_before_this_char = it->hpos;
8510
8511 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8512 {
8513 new_x = x + single_glyph_width;
8514
8515 /* We want to leave anything reaching TO_X to the caller. */
8516 if ((op & MOVE_TO_X) && new_x > to_x)
8517 {
8518 if (BUFFER_POS_REACHED_P ())
8519 {
8520 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8521 goto buffer_pos_reached;
8522 if (atpos_it.sp < 0)
8523 {
8524 SAVE_IT (atpos_it, *it, atpos_data);
8525 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8526 }
8527 }
8528 else
8529 {
8530 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8531 {
8532 it->current_x = x;
8533 result = MOVE_X_REACHED;
8534 break;
8535 }
8536 if (atx_it.sp < 0)
8537 {
8538 SAVE_IT (atx_it, *it, atx_data);
8539 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8540 }
8541 }
8542 }
8543
8544 if (/* Lines are continued. */
8545 it->line_wrap != TRUNCATE
8546 && (/* And glyph doesn't fit on the line. */
8547 new_x > it->last_visible_x
8548 /* Or it fits exactly and we're on a window
8549 system frame. */
8550 || (new_x == it->last_visible_x
8551 && FRAME_WINDOW_P (it->f)
8552 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8553 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8554 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8555 {
8556 if (/* IT->hpos == 0 means the very first glyph
8557 doesn't fit on the line, e.g. a wide image. */
8558 it->hpos == 0
8559 || (new_x == it->last_visible_x
8560 && FRAME_WINDOW_P (it->f)))
8561 {
8562 ++it->hpos;
8563 it->current_x = new_x;
8564
8565 /* The character's last glyph just barely fits
8566 in this row. */
8567 if (i == it->nglyphs - 1)
8568 {
8569 /* If this is the destination position,
8570 return a position *before* it in this row,
8571 now that we know it fits in this row. */
8572 if (BUFFER_POS_REACHED_P ())
8573 {
8574 if (it->line_wrap != WORD_WRAP
8575 || wrap_it.sp < 0)
8576 {
8577 it->hpos = hpos_before_this_char;
8578 it->current_x = x_before_this_char;
8579 result = MOVE_POS_MATCH_OR_ZV;
8580 break;
8581 }
8582 if (it->line_wrap == WORD_WRAP
8583 && atpos_it.sp < 0)
8584 {
8585 SAVE_IT (atpos_it, *it, atpos_data);
8586 atpos_it.current_x = x_before_this_char;
8587 atpos_it.hpos = hpos_before_this_char;
8588 }
8589 }
8590
8591 prev_method = it->method;
8592 if (it->method == GET_FROM_BUFFER)
8593 prev_pos = IT_CHARPOS (*it);
8594 set_iterator_to_next (it, 1);
8595 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8596 SET_TEXT_POS (this_line_min_pos,
8597 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8598 /* On graphical terminals, newlines may
8599 "overflow" into the fringe if
8600 overflow-newline-into-fringe is non-nil.
8601 On text terminals, and on graphical
8602 terminals with no right margin, newlines
8603 may overflow into the last glyph on the
8604 display line.*/
8605 if (!FRAME_WINDOW_P (it->f)
8606 || ((it->bidi_p
8607 && it->bidi_it.paragraph_dir == R2L)
8608 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8609 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8610 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8611 {
8612 if (!get_next_display_element (it))
8613 {
8614 result = MOVE_POS_MATCH_OR_ZV;
8615 break;
8616 }
8617 if (BUFFER_POS_REACHED_P ())
8618 {
8619 if (ITERATOR_AT_END_OF_LINE_P (it))
8620 result = MOVE_POS_MATCH_OR_ZV;
8621 else
8622 result = MOVE_LINE_CONTINUED;
8623 break;
8624 }
8625 if (ITERATOR_AT_END_OF_LINE_P (it)
8626 && (it->line_wrap != WORD_WRAP
8627 || wrap_it.sp < 0))
8628 {
8629 result = MOVE_NEWLINE_OR_CR;
8630 break;
8631 }
8632 }
8633 }
8634 }
8635 else
8636 IT_RESET_X_ASCENT_DESCENT (it);
8637
8638 if (wrap_it.sp >= 0)
8639 {
8640 RESTORE_IT (it, &wrap_it, wrap_data);
8641 atpos_it.sp = -1;
8642 atx_it.sp = -1;
8643 }
8644
8645 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8646 IT_CHARPOS (*it)));
8647 result = MOVE_LINE_CONTINUED;
8648 break;
8649 }
8650
8651 if (BUFFER_POS_REACHED_P ())
8652 {
8653 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8654 goto buffer_pos_reached;
8655 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8656 {
8657 SAVE_IT (atpos_it, *it, atpos_data);
8658 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8659 }
8660 }
8661
8662 if (new_x > it->first_visible_x)
8663 {
8664 /* Glyph is visible. Increment number of glyphs that
8665 would be displayed. */
8666 ++it->hpos;
8667 }
8668 }
8669
8670 if (result != MOVE_UNDEFINED)
8671 break;
8672 }
8673 else if (BUFFER_POS_REACHED_P ())
8674 {
8675 buffer_pos_reached:
8676 IT_RESET_X_ASCENT_DESCENT (it);
8677 result = MOVE_POS_MATCH_OR_ZV;
8678 break;
8679 }
8680 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8681 {
8682 /* Stop when TO_X specified and reached. This check is
8683 necessary here because of lines consisting of a line end,
8684 only. The line end will not produce any glyphs and we
8685 would never get MOVE_X_REACHED. */
8686 eassert (it->nglyphs == 0);
8687 result = MOVE_X_REACHED;
8688 break;
8689 }
8690
8691 /* Is this a line end? If yes, we're done. */
8692 if (ITERATOR_AT_END_OF_LINE_P (it))
8693 {
8694 /* If we are past TO_CHARPOS, but never saw any character
8695 positions smaller than TO_CHARPOS, return
8696 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8697 did. */
8698 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8699 {
8700 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8701 {
8702 if (IT_CHARPOS (ppos_it) < ZV)
8703 {
8704 RESTORE_IT (it, &ppos_it, ppos_data);
8705 result = MOVE_POS_MATCH_OR_ZV;
8706 }
8707 else
8708 goto buffer_pos_reached;
8709 }
8710 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8711 && IT_CHARPOS (*it) > to_charpos)
8712 goto buffer_pos_reached;
8713 else
8714 result = MOVE_NEWLINE_OR_CR;
8715 }
8716 else
8717 result = MOVE_NEWLINE_OR_CR;
8718 break;
8719 }
8720
8721 prev_method = it->method;
8722 if (it->method == GET_FROM_BUFFER)
8723 prev_pos = IT_CHARPOS (*it);
8724 /* The current display element has been consumed. Advance
8725 to the next. */
8726 set_iterator_to_next (it, 1);
8727 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8728 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8729 if (IT_CHARPOS (*it) < to_charpos)
8730 saw_smaller_pos = 1;
8731 if (it->bidi_p
8732 && (op & MOVE_TO_POS)
8733 && IT_CHARPOS (*it) >= to_charpos
8734 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8735 SAVE_IT (ppos_it, *it, ppos_data);
8736
8737 /* Stop if lines are truncated and IT's current x-position is
8738 past the right edge of the window now. */
8739 if (it->line_wrap == TRUNCATE
8740 && it->current_x >= it->last_visible_x)
8741 {
8742 if (!FRAME_WINDOW_P (it->f)
8743 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8744 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8745 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8746 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8747 {
8748 int at_eob_p = 0;
8749
8750 if ((at_eob_p = !get_next_display_element (it))
8751 || BUFFER_POS_REACHED_P ()
8752 /* If we are past TO_CHARPOS, but never saw any
8753 character positions smaller than TO_CHARPOS,
8754 return MOVE_POS_MATCH_OR_ZV, like the
8755 unidirectional display did. */
8756 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8757 && !saw_smaller_pos
8758 && IT_CHARPOS (*it) > to_charpos))
8759 {
8760 if (it->bidi_p
8761 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8762 RESTORE_IT (it, &ppos_it, ppos_data);
8763 result = MOVE_POS_MATCH_OR_ZV;
8764 break;
8765 }
8766 if (ITERATOR_AT_END_OF_LINE_P (it))
8767 {
8768 result = MOVE_NEWLINE_OR_CR;
8769 break;
8770 }
8771 }
8772 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8773 && !saw_smaller_pos
8774 && IT_CHARPOS (*it) > to_charpos)
8775 {
8776 if (IT_CHARPOS (ppos_it) < ZV)
8777 RESTORE_IT (it, &ppos_it, ppos_data);
8778 result = MOVE_POS_MATCH_OR_ZV;
8779 break;
8780 }
8781 result = MOVE_LINE_TRUNCATED;
8782 break;
8783 }
8784 #undef IT_RESET_X_ASCENT_DESCENT
8785 }
8786
8787 #undef BUFFER_POS_REACHED_P
8788
8789 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8790 restore the saved iterator. */
8791 if (atpos_it.sp >= 0)
8792 RESTORE_IT (it, &atpos_it, atpos_data);
8793 else if (atx_it.sp >= 0)
8794 RESTORE_IT (it, &atx_it, atx_data);
8795
8796 done:
8797
8798 if (atpos_data)
8799 bidi_unshelve_cache (atpos_data, 1);
8800 if (atx_data)
8801 bidi_unshelve_cache (atx_data, 1);
8802 if (wrap_data)
8803 bidi_unshelve_cache (wrap_data, 1);
8804 if (ppos_data)
8805 bidi_unshelve_cache (ppos_data, 1);
8806
8807 /* Restore the iterator settings altered at the beginning of this
8808 function. */
8809 it->glyph_row = saved_glyph_row;
8810 return result;
8811 }
8812
8813 /* For external use. */
8814 void
8815 move_it_in_display_line (struct it *it,
8816 ptrdiff_t to_charpos, int to_x,
8817 enum move_operation_enum op)
8818 {
8819 if (it->line_wrap == WORD_WRAP
8820 && (op & MOVE_TO_X))
8821 {
8822 struct it save_it;
8823 void *save_data = NULL;
8824 int skip;
8825
8826 SAVE_IT (save_it, *it, save_data);
8827 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8828 /* When word-wrap is on, TO_X may lie past the end
8829 of a wrapped line. Then it->current is the
8830 character on the next line, so backtrack to the
8831 space before the wrap point. */
8832 if (skip == MOVE_LINE_CONTINUED)
8833 {
8834 int prev_x = max (it->current_x - 1, 0);
8835 RESTORE_IT (it, &save_it, save_data);
8836 move_it_in_display_line_to
8837 (it, -1, prev_x, MOVE_TO_X);
8838 }
8839 else
8840 bidi_unshelve_cache (save_data, 1);
8841 }
8842 else
8843 move_it_in_display_line_to (it, to_charpos, to_x, op);
8844 }
8845
8846
8847 /* Move IT forward until it satisfies one or more of the criteria in
8848 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8849
8850 OP is a bit-mask that specifies where to stop, and in particular,
8851 which of those four position arguments makes a difference. See the
8852 description of enum move_operation_enum.
8853
8854 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8855 screen line, this function will set IT to the next position that is
8856 displayed to the right of TO_CHARPOS on the screen.
8857
8858 Return the maximum pixel length of any line scanned but never more
8859 than it.last_visible_x. */
8860
8861 int
8862 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8863 {
8864 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8865 int line_height, line_start_x = 0, reached = 0;
8866 int max_current_x = 0;
8867 void *backup_data = NULL;
8868
8869 for (;;)
8870 {
8871 if (op & MOVE_TO_VPOS)
8872 {
8873 /* If no TO_CHARPOS and no TO_X specified, stop at the
8874 start of the line TO_VPOS. */
8875 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8876 {
8877 if (it->vpos == to_vpos)
8878 {
8879 reached = 1;
8880 break;
8881 }
8882 else
8883 skip = move_it_in_display_line_to (it, -1, -1, 0);
8884 }
8885 else
8886 {
8887 /* TO_VPOS >= 0 means stop at TO_X in the line at
8888 TO_VPOS, or at TO_POS, whichever comes first. */
8889 if (it->vpos == to_vpos)
8890 {
8891 reached = 2;
8892 break;
8893 }
8894
8895 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8896
8897 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8898 {
8899 reached = 3;
8900 break;
8901 }
8902 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8903 {
8904 /* We have reached TO_X but not in the line we want. */
8905 skip = move_it_in_display_line_to (it, to_charpos,
8906 -1, MOVE_TO_POS);
8907 if (skip == MOVE_POS_MATCH_OR_ZV)
8908 {
8909 reached = 4;
8910 break;
8911 }
8912 }
8913 }
8914 }
8915 else if (op & MOVE_TO_Y)
8916 {
8917 struct it it_backup;
8918
8919 if (it->line_wrap == WORD_WRAP)
8920 SAVE_IT (it_backup, *it, backup_data);
8921
8922 /* TO_Y specified means stop at TO_X in the line containing
8923 TO_Y---or at TO_CHARPOS if this is reached first. The
8924 problem is that we can't really tell whether the line
8925 contains TO_Y before we have completely scanned it, and
8926 this may skip past TO_X. What we do is to first scan to
8927 TO_X.
8928
8929 If TO_X is not specified, use a TO_X of zero. The reason
8930 is to make the outcome of this function more predictable.
8931 If we didn't use TO_X == 0, we would stop at the end of
8932 the line which is probably not what a caller would expect
8933 to happen. */
8934 skip = move_it_in_display_line_to
8935 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8936 (MOVE_TO_X | (op & MOVE_TO_POS)));
8937
8938 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8939 if (skip == MOVE_POS_MATCH_OR_ZV)
8940 reached = 5;
8941 else if (skip == MOVE_X_REACHED)
8942 {
8943 /* If TO_X was reached, we want to know whether TO_Y is
8944 in the line. We know this is the case if the already
8945 scanned glyphs make the line tall enough. Otherwise,
8946 we must check by scanning the rest of the line. */
8947 line_height = it->max_ascent + it->max_descent;
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8950 {
8951 reached = 6;
8952 break;
8953 }
8954 SAVE_IT (it_backup, *it, backup_data);
8955 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8956 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8957 op & MOVE_TO_POS);
8958 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8959 line_height = it->max_ascent + it->max_descent;
8960 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8961
8962 if (to_y >= it->current_y
8963 && to_y < it->current_y + line_height)
8964 {
8965 /* If TO_Y is in this line and TO_X was reached
8966 above, we scanned too far. We have to restore
8967 IT's settings to the ones before skipping. But
8968 keep the more accurate values of max_ascent and
8969 max_descent we've found while skipping the rest
8970 of the line, for the sake of callers, such as
8971 pos_visible_p, that need to know the line
8972 height. */
8973 int max_ascent = it->max_ascent;
8974 int max_descent = it->max_descent;
8975
8976 RESTORE_IT (it, &it_backup, backup_data);
8977 it->max_ascent = max_ascent;
8978 it->max_descent = max_descent;
8979 reached = 6;
8980 }
8981 else
8982 {
8983 skip = skip2;
8984 if (skip == MOVE_POS_MATCH_OR_ZV)
8985 reached = 7;
8986 }
8987 }
8988 else
8989 {
8990 /* Check whether TO_Y is in this line. */
8991 line_height = it->max_ascent + it->max_descent;
8992 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8993
8994 if (to_y >= it->current_y
8995 && to_y < it->current_y + line_height)
8996 {
8997 if (to_y > it->current_y)
8998 max_current_x = max (it->current_x, max_current_x);
8999
9000 /* When word-wrap is on, TO_X may lie past the end
9001 of a wrapped line. Then it->current is the
9002 character on the next line, so backtrack to the
9003 space before the wrap point. */
9004 if (skip == MOVE_LINE_CONTINUED
9005 && it->line_wrap == WORD_WRAP)
9006 {
9007 int prev_x = max (it->current_x - 1, 0);
9008 RESTORE_IT (it, &it_backup, backup_data);
9009 skip = move_it_in_display_line_to
9010 (it, -1, prev_x, MOVE_TO_X);
9011 }
9012
9013 reached = 6;
9014 }
9015 }
9016
9017 if (reached)
9018 {
9019 max_current_x = max (it->current_x, max_current_x);
9020 break;
9021 }
9022 }
9023 else if (BUFFERP (it->object)
9024 && (it->method == GET_FROM_BUFFER
9025 || it->method == GET_FROM_STRETCH)
9026 && IT_CHARPOS (*it) >= to_charpos
9027 /* Under bidi iteration, a call to set_iterator_to_next
9028 can scan far beyond to_charpos if the initial
9029 portion of the next line needs to be reordered. In
9030 that case, give move_it_in_display_line_to another
9031 chance below. */
9032 && !(it->bidi_p
9033 && it->bidi_it.scan_dir == -1))
9034 skip = MOVE_POS_MATCH_OR_ZV;
9035 else
9036 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9037
9038 switch (skip)
9039 {
9040 case MOVE_POS_MATCH_OR_ZV:
9041 max_current_x = max (it->current_x, max_current_x);
9042 reached = 8;
9043 goto out;
9044
9045 case MOVE_NEWLINE_OR_CR:
9046 max_current_x = max (it->current_x, max_current_x);
9047 set_iterator_to_next (it, 1);
9048 it->continuation_lines_width = 0;
9049 break;
9050
9051 case MOVE_LINE_TRUNCATED:
9052 max_current_x = it->last_visible_x;
9053 it->continuation_lines_width = 0;
9054 reseat_at_next_visible_line_start (it, 0);
9055 if ((op & MOVE_TO_POS) != 0
9056 && IT_CHARPOS (*it) > to_charpos)
9057 {
9058 reached = 9;
9059 goto out;
9060 }
9061 break;
9062
9063 case MOVE_LINE_CONTINUED:
9064 max_current_x = it->last_visible_x;
9065 /* For continued lines ending in a tab, some of the glyphs
9066 associated with the tab are displayed on the current
9067 line. Since it->current_x does not include these glyphs,
9068 we use it->last_visible_x instead. */
9069 if (it->c == '\t')
9070 {
9071 it->continuation_lines_width += it->last_visible_x;
9072 /* When moving by vpos, ensure that the iterator really
9073 advances to the next line (bug#847, bug#969). Fixme:
9074 do we need to do this in other circumstances? */
9075 if (it->current_x != it->last_visible_x
9076 && (op & MOVE_TO_VPOS)
9077 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9078 {
9079 line_start_x = it->current_x + it->pixel_width
9080 - it->last_visible_x;
9081 set_iterator_to_next (it, 0);
9082 }
9083 }
9084 else
9085 it->continuation_lines_width += it->current_x;
9086 break;
9087
9088 default:
9089 emacs_abort ();
9090 }
9091
9092 /* Reset/increment for the next run. */
9093 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9094 it->current_x = line_start_x;
9095 line_start_x = 0;
9096 it->hpos = 0;
9097 it->current_y += it->max_ascent + it->max_descent;
9098 ++it->vpos;
9099 last_height = it->max_ascent + it->max_descent;
9100 last_max_ascent = it->max_ascent;
9101 it->max_ascent = it->max_descent = 0;
9102 }
9103
9104 out:
9105
9106 /* On text terminals, we may stop at the end of a line in the middle
9107 of a multi-character glyph. If the glyph itself is continued,
9108 i.e. it is actually displayed on the next line, don't treat this
9109 stopping point as valid; move to the next line instead (unless
9110 that brings us offscreen). */
9111 if (!FRAME_WINDOW_P (it->f)
9112 && op & MOVE_TO_POS
9113 && IT_CHARPOS (*it) == to_charpos
9114 && it->what == IT_CHARACTER
9115 && it->nglyphs > 1
9116 && it->line_wrap == WINDOW_WRAP
9117 && it->current_x == it->last_visible_x - 1
9118 && it->c != '\n'
9119 && it->c != '\t'
9120 && it->vpos < it->w->window_end_vpos)
9121 {
9122 it->continuation_lines_width += it->current_x;
9123 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9124 it->current_y += it->max_ascent + it->max_descent;
9125 ++it->vpos;
9126 last_height = it->max_ascent + it->max_descent;
9127 last_max_ascent = it->max_ascent;
9128 }
9129
9130 if (backup_data)
9131 bidi_unshelve_cache (backup_data, 1);
9132
9133 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9134
9135 return max_current_x;
9136 }
9137
9138
9139 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9140
9141 If DY > 0, move IT backward at least that many pixels. DY = 0
9142 means move IT backward to the preceding line start or BEGV. This
9143 function may move over more than DY pixels if IT->current_y - DY
9144 ends up in the middle of a line; in this case IT->current_y will be
9145 set to the top of the line moved to. */
9146
9147 void
9148 move_it_vertically_backward (struct it *it, int dy)
9149 {
9150 int nlines, h;
9151 struct it it2, it3;
9152 void *it2data = NULL, *it3data = NULL;
9153 ptrdiff_t start_pos;
9154 int nchars_per_row
9155 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9156 ptrdiff_t pos_limit;
9157
9158 move_further_back:
9159 eassert (dy >= 0);
9160
9161 start_pos = IT_CHARPOS (*it);
9162
9163 /* Estimate how many newlines we must move back. */
9164 nlines = max (1, dy / default_line_pixel_height (it->w));
9165 if (it->line_wrap == TRUNCATE)
9166 pos_limit = BEGV;
9167 else
9168 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9169
9170 /* Set the iterator's position that many lines back. But don't go
9171 back more than NLINES full screen lines -- this wins a day with
9172 buffers which have very long lines. */
9173 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9174 back_to_previous_visible_line_start (it);
9175
9176 /* Reseat the iterator here. When moving backward, we don't want
9177 reseat to skip forward over invisible text, set up the iterator
9178 to deliver from overlay strings at the new position etc. So,
9179 use reseat_1 here. */
9180 reseat_1 (it, it->current.pos, 1);
9181
9182 /* We are now surely at a line start. */
9183 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9184 reordering is in effect. */
9185 it->continuation_lines_width = 0;
9186
9187 /* Move forward and see what y-distance we moved. First move to the
9188 start of the next line so that we get its height. We need this
9189 height to be able to tell whether we reached the specified
9190 y-distance. */
9191 SAVE_IT (it2, *it, it2data);
9192 it2.max_ascent = it2.max_descent = 0;
9193 do
9194 {
9195 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9196 MOVE_TO_POS | MOVE_TO_VPOS);
9197 }
9198 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9199 /* If we are in a display string which starts at START_POS,
9200 and that display string includes a newline, and we are
9201 right after that newline (i.e. at the beginning of a
9202 display line), exit the loop, because otherwise we will
9203 infloop, since move_it_to will see that it is already at
9204 START_POS and will not move. */
9205 || (it2.method == GET_FROM_STRING
9206 && IT_CHARPOS (it2) == start_pos
9207 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9208 eassert (IT_CHARPOS (*it) >= BEGV);
9209 SAVE_IT (it3, it2, it3data);
9210
9211 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9212 eassert (IT_CHARPOS (*it) >= BEGV);
9213 /* H is the actual vertical distance from the position in *IT
9214 and the starting position. */
9215 h = it2.current_y - it->current_y;
9216 /* NLINES is the distance in number of lines. */
9217 nlines = it2.vpos - it->vpos;
9218
9219 /* Correct IT's y and vpos position
9220 so that they are relative to the starting point. */
9221 it->vpos -= nlines;
9222 it->current_y -= h;
9223
9224 if (dy == 0)
9225 {
9226 /* DY == 0 means move to the start of the screen line. The
9227 value of nlines is > 0 if continuation lines were involved,
9228 or if the original IT position was at start of a line. */
9229 RESTORE_IT (it, it, it2data);
9230 if (nlines > 0)
9231 move_it_by_lines (it, nlines);
9232 /* The above code moves us to some position NLINES down,
9233 usually to its first glyph (leftmost in an L2R line), but
9234 that's not necessarily the start of the line, under bidi
9235 reordering. We want to get to the character position
9236 that is immediately after the newline of the previous
9237 line. */
9238 if (it->bidi_p
9239 && !it->continuation_lines_width
9240 && !STRINGP (it->string)
9241 && IT_CHARPOS (*it) > BEGV
9242 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9243 {
9244 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9245
9246 DEC_BOTH (cp, bp);
9247 cp = find_newline_no_quit (cp, bp, -1, NULL);
9248 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9249 }
9250 bidi_unshelve_cache (it3data, 1);
9251 }
9252 else
9253 {
9254 /* The y-position we try to reach, relative to *IT.
9255 Note that H has been subtracted in front of the if-statement. */
9256 int target_y = it->current_y + h - dy;
9257 int y0 = it3.current_y;
9258 int y1;
9259 int line_height;
9260
9261 RESTORE_IT (&it3, &it3, it3data);
9262 y1 = line_bottom_y (&it3);
9263 line_height = y1 - y0;
9264 RESTORE_IT (it, it, it2data);
9265 /* If we did not reach target_y, try to move further backward if
9266 we can. If we moved too far backward, try to move forward. */
9267 if (target_y < it->current_y
9268 /* This is heuristic. In a window that's 3 lines high, with
9269 a line height of 13 pixels each, recentering with point
9270 on the bottom line will try to move -39/2 = 19 pixels
9271 backward. Try to avoid moving into the first line. */
9272 && (it->current_y - target_y
9273 > min (window_box_height (it->w), line_height * 2 / 3))
9274 && IT_CHARPOS (*it) > BEGV)
9275 {
9276 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9277 target_y - it->current_y));
9278 dy = it->current_y - target_y;
9279 goto move_further_back;
9280 }
9281 else if (target_y >= it->current_y + line_height
9282 && IT_CHARPOS (*it) < ZV)
9283 {
9284 /* Should move forward by at least one line, maybe more.
9285
9286 Note: Calling move_it_by_lines can be expensive on
9287 terminal frames, where compute_motion is used (via
9288 vmotion) to do the job, when there are very long lines
9289 and truncate-lines is nil. That's the reason for
9290 treating terminal frames specially here. */
9291
9292 if (!FRAME_WINDOW_P (it->f))
9293 move_it_vertically (it, target_y - (it->current_y + line_height));
9294 else
9295 {
9296 do
9297 {
9298 move_it_by_lines (it, 1);
9299 }
9300 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9301 }
9302 }
9303 }
9304 }
9305
9306
9307 /* Move IT by a specified amount of pixel lines DY. DY negative means
9308 move backwards. DY = 0 means move to start of screen line. At the
9309 end, IT will be on the start of a screen line. */
9310
9311 void
9312 move_it_vertically (struct it *it, int dy)
9313 {
9314 if (dy <= 0)
9315 move_it_vertically_backward (it, -dy);
9316 else
9317 {
9318 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9319 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9320 MOVE_TO_POS | MOVE_TO_Y);
9321 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9322
9323 /* If buffer ends in ZV without a newline, move to the start of
9324 the line to satisfy the post-condition. */
9325 if (IT_CHARPOS (*it) == ZV
9326 && ZV > BEGV
9327 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9328 move_it_by_lines (it, 0);
9329 }
9330 }
9331
9332
9333 /* Move iterator IT past the end of the text line it is in. */
9334
9335 void
9336 move_it_past_eol (struct it *it)
9337 {
9338 enum move_it_result rc;
9339
9340 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9341 if (rc == MOVE_NEWLINE_OR_CR)
9342 set_iterator_to_next (it, 0);
9343 }
9344
9345
9346 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9347 negative means move up. DVPOS == 0 means move to the start of the
9348 screen line.
9349
9350 Optimization idea: If we would know that IT->f doesn't use
9351 a face with proportional font, we could be faster for
9352 truncate-lines nil. */
9353
9354 void
9355 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9356 {
9357
9358 /* The commented-out optimization uses vmotion on terminals. This
9359 gives bad results, because elements like it->what, on which
9360 callers such as pos_visible_p rely, aren't updated. */
9361 /* struct position pos;
9362 if (!FRAME_WINDOW_P (it->f))
9363 {
9364 struct text_pos textpos;
9365
9366 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9367 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9368 reseat (it, textpos, 1);
9369 it->vpos += pos.vpos;
9370 it->current_y += pos.vpos;
9371 }
9372 else */
9373
9374 if (dvpos == 0)
9375 {
9376 /* DVPOS == 0 means move to the start of the screen line. */
9377 move_it_vertically_backward (it, 0);
9378 /* Let next call to line_bottom_y calculate real line height. */
9379 last_height = 0;
9380 }
9381 else if (dvpos > 0)
9382 {
9383 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9384 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9385 {
9386 /* Only move to the next buffer position if we ended up in a
9387 string from display property, not in an overlay string
9388 (before-string or after-string). That is because the
9389 latter don't conceal the underlying buffer position, so
9390 we can ask to move the iterator to the exact position we
9391 are interested in. Note that, even if we are already at
9392 IT_CHARPOS (*it), the call below is not a no-op, as it
9393 will detect that we are at the end of the string, pop the
9394 iterator, and compute it->current_x and it->hpos
9395 correctly. */
9396 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9397 -1, -1, -1, MOVE_TO_POS);
9398 }
9399 }
9400 else
9401 {
9402 struct it it2;
9403 void *it2data = NULL;
9404 ptrdiff_t start_charpos, i;
9405 int nchars_per_row
9406 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9407 ptrdiff_t pos_limit;
9408
9409 /* Start at the beginning of the screen line containing IT's
9410 position. This may actually move vertically backwards,
9411 in case of overlays, so adjust dvpos accordingly. */
9412 dvpos += it->vpos;
9413 move_it_vertically_backward (it, 0);
9414 dvpos -= it->vpos;
9415
9416 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9417 screen lines, and reseat the iterator there. */
9418 start_charpos = IT_CHARPOS (*it);
9419 if (it->line_wrap == TRUNCATE)
9420 pos_limit = BEGV;
9421 else
9422 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9423 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9424 back_to_previous_visible_line_start (it);
9425 reseat (it, it->current.pos, 1);
9426
9427 /* Move further back if we end up in a string or an image. */
9428 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9429 {
9430 /* First try to move to start of display line. */
9431 dvpos += it->vpos;
9432 move_it_vertically_backward (it, 0);
9433 dvpos -= it->vpos;
9434 if (IT_POS_VALID_AFTER_MOVE_P (it))
9435 break;
9436 /* If start of line is still in string or image,
9437 move further back. */
9438 back_to_previous_visible_line_start (it);
9439 reseat (it, it->current.pos, 1);
9440 dvpos--;
9441 }
9442
9443 it->current_x = it->hpos = 0;
9444
9445 /* Above call may have moved too far if continuation lines
9446 are involved. Scan forward and see if it did. */
9447 SAVE_IT (it2, *it, it2data);
9448 it2.vpos = it2.current_y = 0;
9449 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9450 it->vpos -= it2.vpos;
9451 it->current_y -= it2.current_y;
9452 it->current_x = it->hpos = 0;
9453
9454 /* If we moved too far back, move IT some lines forward. */
9455 if (it2.vpos > -dvpos)
9456 {
9457 int delta = it2.vpos + dvpos;
9458
9459 RESTORE_IT (&it2, &it2, it2data);
9460 SAVE_IT (it2, *it, it2data);
9461 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9462 /* Move back again if we got too far ahead. */
9463 if (IT_CHARPOS (*it) >= start_charpos)
9464 RESTORE_IT (it, &it2, it2data);
9465 else
9466 bidi_unshelve_cache (it2data, 1);
9467 }
9468 else
9469 RESTORE_IT (it, it, it2data);
9470 }
9471 }
9472
9473 /* Return true if IT points into the middle of a display vector. */
9474
9475 bool
9476 in_display_vector_p (struct it *it)
9477 {
9478 return (it->method == GET_FROM_DISPLAY_VECTOR
9479 && it->current.dpvec_index > 0
9480 && it->dpvec + it->current.dpvec_index != it->dpend);
9481 }
9482
9483 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9484 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9485 WINDOW must be a live window and defaults to the selected one. The
9486 return value is a cons of the maximum pixel-width of any text line and
9487 the maximum pixel-height of all text lines.
9488
9489 The optional argument FROM, if non-nil, specifies the first text
9490 position and defaults to the minimum accessible position of the buffer.
9491 If FROM is t, use the minimum accessible position that is not a newline
9492 character. TO, if non-nil, specifies the last text position and
9493 defaults to the maximum accessible position of the buffer. If TO is t,
9494 use the maximum accessible position that is not a newline character.
9495
9496 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9497 width that can be returned. X_LIMIT nil or omitted, means to use the
9498 pixel-width of WINDOW's body; use this if you do not intend to change
9499 the width of WINDOW. Use the maximum width WINDOW may assume if you
9500 intend to change WINDOW's width.
9501
9502 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9503 height that can be returned. Text lines whose y-coordinate is beyond
9504 Y_LIMIT are ignored. Since calculating the text height of a large
9505 buffer can take some time, it makes sense to specify this argument if
9506 the size of the buffer is unknown.
9507
9508 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9509 include the height of the mode- or header-line of WINDOW in the return
9510 value. If it is either the symbol `mode-line' or `header-line', include
9511 only the height of that line, if present, in the return value. If t,
9512 include the height of any of these lines in the return value. */)
9513 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9514 Lisp_Object mode_and_header_line)
9515 {
9516 struct window *w = decode_live_window (window);
9517 Lisp_Object buf;
9518 struct buffer *b;
9519 struct it it;
9520 struct buffer *old_buffer = NULL;
9521 ptrdiff_t start, end, pos;
9522 struct text_pos startp;
9523 void *itdata = NULL;
9524 int c, max_y = -1, x = 0, y = 0;
9525
9526 buf = w->contents;
9527 CHECK_BUFFER (buf);
9528 b = XBUFFER (buf);
9529
9530 if (b != current_buffer)
9531 {
9532 old_buffer = current_buffer;
9533 set_buffer_internal (b);
9534 }
9535
9536 if (NILP (from))
9537 start = BEGV;
9538 else if (EQ (from, Qt))
9539 {
9540 start = pos = BEGV;
9541 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9542 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9543 start = pos;
9544 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9545 start = pos;
9546 }
9547 else
9548 {
9549 CHECK_NUMBER_COERCE_MARKER (from);
9550 start = min (max (XINT (from), BEGV), ZV);
9551 }
9552
9553 if (NILP (to))
9554 end = ZV;
9555 else if (EQ (to, Qt))
9556 {
9557 end = pos = ZV;
9558 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9559 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9560 end = pos;
9561 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9562 end = pos;
9563 }
9564 else
9565 {
9566 CHECK_NUMBER_COERCE_MARKER (to);
9567 end = max (start, min (XINT (to), ZV));
9568 }
9569
9570 if (!NILP (y_limit))
9571 {
9572 CHECK_NUMBER (y_limit);
9573 max_y = min (XINT (y_limit), INT_MAX);
9574 }
9575
9576 itdata = bidi_shelve_cache ();
9577 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9578 start_display (&it, w, startp);
9579
9580 /** move_it_vertically_backward (&it, 0); **/
9581 if (NILP (x_limit))
9582 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9583 else
9584 {
9585 CHECK_NUMBER (x_limit);
9586 it.last_visible_x = min (XINT (x_limit), INFINITY);
9587 /* Actually, we never want move_it_to stop at to_x. But to make
9588 sure that move_it_in_display_line_to always moves far enough,
9589 we set it to INT_MAX and specify MOVE_TO_X. */
9590 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9591 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9592 }
9593
9594 if (start == end)
9595 y = it.current_y;
9596 else
9597 {
9598 /* Count last line. */
9599 last_height = 0;
9600 y = line_bottom_y (&it); /* - y; */
9601 }
9602
9603 if (!EQ (mode_and_header_line, Qheader_line)
9604 && !EQ (mode_and_header_line, Qt))
9605 /* Do not count the header-line which was counted automatically by
9606 start_display. */
9607 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9608
9609 if (EQ (mode_and_header_line, Qmode_line)
9610 || EQ (mode_and_header_line, Qt))
9611 /* Do count the mode-line which is not included automatically by
9612 start_display. */
9613 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9614
9615 bidi_unshelve_cache (itdata, 0);
9616
9617 if (old_buffer)
9618 set_buffer_internal (old_buffer);
9619
9620 return Fcons (make_number (x), make_number (y));
9621 }
9622 \f
9623 /***********************************************************************
9624 Messages
9625 ***********************************************************************/
9626
9627
9628 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9629 to *Messages*. */
9630
9631 void
9632 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9633 {
9634 Lisp_Object args[3];
9635 Lisp_Object msg, fmt;
9636 char *buffer;
9637 ptrdiff_t len;
9638 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9639 USE_SAFE_ALLOCA;
9640
9641 fmt = msg = Qnil;
9642 GCPRO4 (fmt, msg, arg1, arg2);
9643
9644 args[0] = fmt = build_string (format);
9645 args[1] = arg1;
9646 args[2] = arg2;
9647 msg = Fformat (3, args);
9648
9649 len = SBYTES (msg) + 1;
9650 buffer = SAFE_ALLOCA (len);
9651 memcpy (buffer, SDATA (msg), len);
9652
9653 message_dolog (buffer, len - 1, 1, 0);
9654 SAFE_FREE ();
9655
9656 UNGCPRO;
9657 }
9658
9659
9660 /* Output a newline in the *Messages* buffer if "needs" one. */
9661
9662 void
9663 message_log_maybe_newline (void)
9664 {
9665 if (message_log_need_newline)
9666 message_dolog ("", 0, 1, 0);
9667 }
9668
9669
9670 /* Add a string M of length NBYTES to the message log, optionally
9671 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9672 true, means interpret the contents of M as multibyte. This
9673 function calls low-level routines in order to bypass text property
9674 hooks, etc. which might not be safe to run.
9675
9676 This may GC (insert may run before/after change hooks),
9677 so the buffer M must NOT point to a Lisp string. */
9678
9679 void
9680 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9681 {
9682 const unsigned char *msg = (const unsigned char *) m;
9683
9684 if (!NILP (Vmemory_full))
9685 return;
9686
9687 if (!NILP (Vmessage_log_max))
9688 {
9689 struct buffer *oldbuf;
9690 Lisp_Object oldpoint, oldbegv, oldzv;
9691 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9692 ptrdiff_t point_at_end = 0;
9693 ptrdiff_t zv_at_end = 0;
9694 Lisp_Object old_deactivate_mark;
9695 struct gcpro gcpro1;
9696
9697 old_deactivate_mark = Vdeactivate_mark;
9698 oldbuf = current_buffer;
9699
9700 /* Ensure the Messages buffer exists, and switch to it.
9701 If we created it, set the major-mode. */
9702 {
9703 int newbuffer = 0;
9704 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9705
9706 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9707
9708 if (newbuffer
9709 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9710 call0 (intern ("messages-buffer-mode"));
9711 }
9712
9713 bset_undo_list (current_buffer, Qt);
9714 bset_cache_long_scans (current_buffer, Qnil);
9715
9716 oldpoint = message_dolog_marker1;
9717 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9718 oldbegv = message_dolog_marker2;
9719 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9720 oldzv = message_dolog_marker3;
9721 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9722 GCPRO1 (old_deactivate_mark);
9723
9724 if (PT == Z)
9725 point_at_end = 1;
9726 if (ZV == Z)
9727 zv_at_end = 1;
9728
9729 BEGV = BEG;
9730 BEGV_BYTE = BEG_BYTE;
9731 ZV = Z;
9732 ZV_BYTE = Z_BYTE;
9733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9734
9735 /* Insert the string--maybe converting multibyte to single byte
9736 or vice versa, so that all the text fits the buffer. */
9737 if (multibyte
9738 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9739 {
9740 ptrdiff_t i;
9741 int c, char_bytes;
9742 char work[1];
9743
9744 /* Convert a multibyte string to single-byte
9745 for the *Message* buffer. */
9746 for (i = 0; i < nbytes; i += char_bytes)
9747 {
9748 c = string_char_and_length (msg + i, &char_bytes);
9749 work[0] = (ASCII_CHAR_P (c)
9750 ? c
9751 : multibyte_char_to_unibyte (c));
9752 insert_1_both (work, 1, 1, 1, 0, 0);
9753 }
9754 }
9755 else if (! multibyte
9756 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9757 {
9758 ptrdiff_t i;
9759 int c, char_bytes;
9760 unsigned char str[MAX_MULTIBYTE_LENGTH];
9761 /* Convert a single-byte string to multibyte
9762 for the *Message* buffer. */
9763 for (i = 0; i < nbytes; i++)
9764 {
9765 c = msg[i];
9766 MAKE_CHAR_MULTIBYTE (c);
9767 char_bytes = CHAR_STRING (c, str);
9768 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9769 }
9770 }
9771 else if (nbytes)
9772 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9773
9774 if (nlflag)
9775 {
9776 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9777 printmax_t dups;
9778
9779 insert_1_both ("\n", 1, 1, 1, 0, 0);
9780
9781 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9782 this_bol = PT;
9783 this_bol_byte = PT_BYTE;
9784
9785 /* See if this line duplicates the previous one.
9786 If so, combine duplicates. */
9787 if (this_bol > BEG)
9788 {
9789 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9790 prev_bol = PT;
9791 prev_bol_byte = PT_BYTE;
9792
9793 dups = message_log_check_duplicate (prev_bol_byte,
9794 this_bol_byte);
9795 if (dups)
9796 {
9797 del_range_both (prev_bol, prev_bol_byte,
9798 this_bol, this_bol_byte, 0);
9799 if (dups > 1)
9800 {
9801 char dupstr[sizeof " [ times]"
9802 + INT_STRLEN_BOUND (printmax_t)];
9803
9804 /* If you change this format, don't forget to also
9805 change message_log_check_duplicate. */
9806 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9807 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9808 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9809 }
9810 }
9811 }
9812
9813 /* If we have more than the desired maximum number of lines
9814 in the *Messages* buffer now, delete the oldest ones.
9815 This is safe because we don't have undo in this buffer. */
9816
9817 if (NATNUMP (Vmessage_log_max))
9818 {
9819 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9820 -XFASTINT (Vmessage_log_max) - 1, 0);
9821 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9822 }
9823 }
9824 BEGV = marker_position (oldbegv);
9825 BEGV_BYTE = marker_byte_position (oldbegv);
9826
9827 if (zv_at_end)
9828 {
9829 ZV = Z;
9830 ZV_BYTE = Z_BYTE;
9831 }
9832 else
9833 {
9834 ZV = marker_position (oldzv);
9835 ZV_BYTE = marker_byte_position (oldzv);
9836 }
9837
9838 if (point_at_end)
9839 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9840 else
9841 /* We can't do Fgoto_char (oldpoint) because it will run some
9842 Lisp code. */
9843 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9844 marker_byte_position (oldpoint));
9845
9846 UNGCPRO;
9847 unchain_marker (XMARKER (oldpoint));
9848 unchain_marker (XMARKER (oldbegv));
9849 unchain_marker (XMARKER (oldzv));
9850
9851 /* We called insert_1_both above with its 5th argument (PREPARE)
9852 zero, which prevents insert_1_both from calling
9853 prepare_to_modify_buffer, which in turns prevents us from
9854 incrementing windows_or_buffers_changed even if *Messages* is
9855 shown in some window. So we must manually set
9856 windows_or_buffers_changed here to make up for that. */
9857 windows_or_buffers_changed = old_windows_or_buffers_changed;
9858 bset_redisplay (current_buffer);
9859
9860 set_buffer_internal (oldbuf);
9861
9862 message_log_need_newline = !nlflag;
9863 Vdeactivate_mark = old_deactivate_mark;
9864 }
9865 }
9866
9867
9868 /* We are at the end of the buffer after just having inserted a newline.
9869 (Note: We depend on the fact we won't be crossing the gap.)
9870 Check to see if the most recent message looks a lot like the previous one.
9871 Return 0 if different, 1 if the new one should just replace it, or a
9872 value N > 1 if we should also append " [N times]". */
9873
9874 static intmax_t
9875 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9876 {
9877 ptrdiff_t i;
9878 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9879 int seen_dots = 0;
9880 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9881 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9882
9883 for (i = 0; i < len; i++)
9884 {
9885 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9886 seen_dots = 1;
9887 if (p1[i] != p2[i])
9888 return seen_dots;
9889 }
9890 p1 += len;
9891 if (*p1 == '\n')
9892 return 2;
9893 if (*p1++ == ' ' && *p1++ == '[')
9894 {
9895 char *pend;
9896 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9897 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9898 return n + 1;
9899 }
9900 return 0;
9901 }
9902 \f
9903
9904 /* Display an echo area message M with a specified length of NBYTES
9905 bytes. The string may include null characters. If M is not a
9906 string, clear out any existing message, and let the mini-buffer
9907 text show through.
9908
9909 This function cancels echoing. */
9910
9911 void
9912 message3 (Lisp_Object m)
9913 {
9914 struct gcpro gcpro1;
9915
9916 GCPRO1 (m);
9917 clear_message (true, true);
9918 cancel_echoing ();
9919
9920 /* First flush out any partial line written with print. */
9921 message_log_maybe_newline ();
9922 if (STRINGP (m))
9923 {
9924 ptrdiff_t nbytes = SBYTES (m);
9925 bool multibyte = STRING_MULTIBYTE (m);
9926 USE_SAFE_ALLOCA;
9927 char *buffer = SAFE_ALLOCA (nbytes);
9928 memcpy (buffer, SDATA (m), nbytes);
9929 message_dolog (buffer, nbytes, 1, multibyte);
9930 SAFE_FREE ();
9931 }
9932 message3_nolog (m);
9933
9934 UNGCPRO;
9935 }
9936
9937
9938 /* The non-logging version of message3.
9939 This does not cancel echoing, because it is used for echoing.
9940 Perhaps we need to make a separate function for echoing
9941 and make this cancel echoing. */
9942
9943 void
9944 message3_nolog (Lisp_Object m)
9945 {
9946 struct frame *sf = SELECTED_FRAME ();
9947
9948 if (FRAME_INITIAL_P (sf))
9949 {
9950 if (noninteractive_need_newline)
9951 putc ('\n', stderr);
9952 noninteractive_need_newline = 0;
9953 if (STRINGP (m))
9954 {
9955 Lisp_Object s = ENCODE_SYSTEM (m);
9956
9957 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9958 }
9959 if (cursor_in_echo_area == 0)
9960 fprintf (stderr, "\n");
9961 fflush (stderr);
9962 }
9963 /* Error messages get reported properly by cmd_error, so this must be just an
9964 informative message; if the frame hasn't really been initialized yet, just
9965 toss it. */
9966 else if (INTERACTIVE && sf->glyphs_initialized_p)
9967 {
9968 /* Get the frame containing the mini-buffer
9969 that the selected frame is using. */
9970 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9971 Lisp_Object frame = XWINDOW (mini_window)->frame;
9972 struct frame *f = XFRAME (frame);
9973
9974 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9975 Fmake_frame_visible (frame);
9976
9977 if (STRINGP (m) && SCHARS (m) > 0)
9978 {
9979 set_message (m);
9980 if (minibuffer_auto_raise)
9981 Fraise_frame (frame);
9982 /* Assume we are not echoing.
9983 (If we are, echo_now will override this.) */
9984 echo_message_buffer = Qnil;
9985 }
9986 else
9987 clear_message (true, true);
9988
9989 do_pending_window_change (0);
9990 echo_area_display (1);
9991 do_pending_window_change (0);
9992 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9993 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9994 }
9995 }
9996
9997
9998 /* Display a null-terminated echo area message M. If M is 0, clear
9999 out any existing message, and let the mini-buffer text show through.
10000
10001 The buffer M must continue to exist until after the echo area gets
10002 cleared or some other message gets displayed there. Do not pass
10003 text that is stored in a Lisp string. Do not pass text in a buffer
10004 that was alloca'd. */
10005
10006 void
10007 message1 (const char *m)
10008 {
10009 message3 (m ? build_unibyte_string (m) : Qnil);
10010 }
10011
10012
10013 /* The non-logging counterpart of message1. */
10014
10015 void
10016 message1_nolog (const char *m)
10017 {
10018 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10019 }
10020
10021 /* Display a message M which contains a single %s
10022 which gets replaced with STRING. */
10023
10024 void
10025 message_with_string (const char *m, Lisp_Object string, int log)
10026 {
10027 CHECK_STRING (string);
10028
10029 if (noninteractive)
10030 {
10031 if (m)
10032 {
10033 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10034 String whose data pointer might be passed to us in M. So
10035 we use a local copy. */
10036 char *fmt = xstrdup (m);
10037
10038 if (noninteractive_need_newline)
10039 putc ('\n', stderr);
10040 noninteractive_need_newline = 0;
10041 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10042 if (!cursor_in_echo_area)
10043 fprintf (stderr, "\n");
10044 fflush (stderr);
10045 xfree (fmt);
10046 }
10047 }
10048 else if (INTERACTIVE)
10049 {
10050 /* The frame whose minibuffer we're going to display the message on.
10051 It may be larger than the selected frame, so we need
10052 to use its buffer, not the selected frame's buffer. */
10053 Lisp_Object mini_window;
10054 struct frame *f, *sf = SELECTED_FRAME ();
10055
10056 /* Get the frame containing the minibuffer
10057 that the selected frame is using. */
10058 mini_window = FRAME_MINIBUF_WINDOW (sf);
10059 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10060
10061 /* Error messages get reported properly by cmd_error, so this must be
10062 just an informative message; if the frame hasn't really been
10063 initialized yet, just toss it. */
10064 if (f->glyphs_initialized_p)
10065 {
10066 Lisp_Object args[2], msg;
10067 struct gcpro gcpro1, gcpro2;
10068
10069 args[0] = build_string (m);
10070 args[1] = msg = string;
10071 GCPRO2 (args[0], msg);
10072 gcpro1.nvars = 2;
10073
10074 msg = Fformat (2, args);
10075
10076 if (log)
10077 message3 (msg);
10078 else
10079 message3_nolog (msg);
10080
10081 UNGCPRO;
10082
10083 /* Print should start at the beginning of the message
10084 buffer next time. */
10085 message_buf_print = 0;
10086 }
10087 }
10088 }
10089
10090
10091 /* Dump an informative message to the minibuf. If M is 0, clear out
10092 any existing message, and let the mini-buffer text show through. */
10093
10094 static void
10095 vmessage (const char *m, va_list ap)
10096 {
10097 if (noninteractive)
10098 {
10099 if (m)
10100 {
10101 if (noninteractive_need_newline)
10102 putc ('\n', stderr);
10103 noninteractive_need_newline = 0;
10104 vfprintf (stderr, m, ap);
10105 if (cursor_in_echo_area == 0)
10106 fprintf (stderr, "\n");
10107 fflush (stderr);
10108 }
10109 }
10110 else if (INTERACTIVE)
10111 {
10112 /* The frame whose mini-buffer we're going to display the message
10113 on. It may be larger than the selected frame, so we need to
10114 use its buffer, not the selected frame's buffer. */
10115 Lisp_Object mini_window;
10116 struct frame *f, *sf = SELECTED_FRAME ();
10117
10118 /* Get the frame containing the mini-buffer
10119 that the selected frame is using. */
10120 mini_window = FRAME_MINIBUF_WINDOW (sf);
10121 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10122
10123 /* Error messages get reported properly by cmd_error, so this must be
10124 just an informative message; if the frame hasn't really been
10125 initialized yet, just toss it. */
10126 if (f->glyphs_initialized_p)
10127 {
10128 if (m)
10129 {
10130 ptrdiff_t len;
10131 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10132 char *message_buf = alloca (maxsize + 1);
10133
10134 len = doprnt (message_buf, maxsize, m, 0, ap);
10135
10136 message3 (make_string (message_buf, len));
10137 }
10138 else
10139 message1 (0);
10140
10141 /* Print should start at the beginning of the message
10142 buffer next time. */
10143 message_buf_print = 0;
10144 }
10145 }
10146 }
10147
10148 void
10149 message (const char *m, ...)
10150 {
10151 va_list ap;
10152 va_start (ap, m);
10153 vmessage (m, ap);
10154 va_end (ap);
10155 }
10156
10157
10158 #if 0
10159 /* The non-logging version of message. */
10160
10161 void
10162 message_nolog (const char *m, ...)
10163 {
10164 Lisp_Object old_log_max;
10165 va_list ap;
10166 va_start (ap, m);
10167 old_log_max = Vmessage_log_max;
10168 Vmessage_log_max = Qnil;
10169 vmessage (m, ap);
10170 Vmessage_log_max = old_log_max;
10171 va_end (ap);
10172 }
10173 #endif
10174
10175
10176 /* Display the current message in the current mini-buffer. This is
10177 only called from error handlers in process.c, and is not time
10178 critical. */
10179
10180 void
10181 update_echo_area (void)
10182 {
10183 if (!NILP (echo_area_buffer[0]))
10184 {
10185 Lisp_Object string;
10186 string = Fcurrent_message ();
10187 message3 (string);
10188 }
10189 }
10190
10191
10192 /* Make sure echo area buffers in `echo_buffers' are live.
10193 If they aren't, make new ones. */
10194
10195 static void
10196 ensure_echo_area_buffers (void)
10197 {
10198 int i;
10199
10200 for (i = 0; i < 2; ++i)
10201 if (!BUFFERP (echo_buffer[i])
10202 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10203 {
10204 char name[30];
10205 Lisp_Object old_buffer;
10206 int j;
10207
10208 old_buffer = echo_buffer[i];
10209 echo_buffer[i] = Fget_buffer_create
10210 (make_formatted_string (name, " *Echo Area %d*", i));
10211 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10212 /* to force word wrap in echo area -
10213 it was decided to postpone this*/
10214 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10215
10216 for (j = 0; j < 2; ++j)
10217 if (EQ (old_buffer, echo_area_buffer[j]))
10218 echo_area_buffer[j] = echo_buffer[i];
10219 }
10220 }
10221
10222
10223 /* Call FN with args A1..A2 with either the current or last displayed
10224 echo_area_buffer as current buffer.
10225
10226 WHICH zero means use the current message buffer
10227 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10228 from echo_buffer[] and clear it.
10229
10230 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10231 suitable buffer from echo_buffer[] and clear it.
10232
10233 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10234 that the current message becomes the last displayed one, make
10235 choose a suitable buffer for echo_area_buffer[0], and clear it.
10236
10237 Value is what FN returns. */
10238
10239 static int
10240 with_echo_area_buffer (struct window *w, int which,
10241 int (*fn) (ptrdiff_t, Lisp_Object),
10242 ptrdiff_t a1, Lisp_Object a2)
10243 {
10244 Lisp_Object buffer;
10245 int this_one, the_other, clear_buffer_p, rc;
10246 ptrdiff_t count = SPECPDL_INDEX ();
10247
10248 /* If buffers aren't live, make new ones. */
10249 ensure_echo_area_buffers ();
10250
10251 clear_buffer_p = 0;
10252
10253 if (which == 0)
10254 this_one = 0, the_other = 1;
10255 else if (which > 0)
10256 this_one = 1, the_other = 0;
10257 else
10258 {
10259 this_one = 0, the_other = 1;
10260 clear_buffer_p = true;
10261
10262 /* We need a fresh one in case the current echo buffer equals
10263 the one containing the last displayed echo area message. */
10264 if (!NILP (echo_area_buffer[this_one])
10265 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10266 echo_area_buffer[this_one] = Qnil;
10267 }
10268
10269 /* Choose a suitable buffer from echo_buffer[] is we don't
10270 have one. */
10271 if (NILP (echo_area_buffer[this_one]))
10272 {
10273 echo_area_buffer[this_one]
10274 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10275 ? echo_buffer[the_other]
10276 : echo_buffer[this_one]);
10277 clear_buffer_p = true;
10278 }
10279
10280 buffer = echo_area_buffer[this_one];
10281
10282 /* Don't get confused by reusing the buffer used for echoing
10283 for a different purpose. */
10284 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10285 cancel_echoing ();
10286
10287 record_unwind_protect (unwind_with_echo_area_buffer,
10288 with_echo_area_buffer_unwind_data (w));
10289
10290 /* Make the echo area buffer current. Note that for display
10291 purposes, it is not necessary that the displayed window's buffer
10292 == current_buffer, except for text property lookup. So, let's
10293 only set that buffer temporarily here without doing a full
10294 Fset_window_buffer. We must also change w->pointm, though,
10295 because otherwise an assertions in unshow_buffer fails, and Emacs
10296 aborts. */
10297 set_buffer_internal_1 (XBUFFER (buffer));
10298 if (w)
10299 {
10300 wset_buffer (w, buffer);
10301 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10302 }
10303
10304 bset_undo_list (current_buffer, Qt);
10305 bset_read_only (current_buffer, Qnil);
10306 specbind (Qinhibit_read_only, Qt);
10307 specbind (Qinhibit_modification_hooks, Qt);
10308
10309 if (clear_buffer_p && Z > BEG)
10310 del_range (BEG, Z);
10311
10312 eassert (BEGV >= BEG);
10313 eassert (ZV <= Z && ZV >= BEGV);
10314
10315 rc = fn (a1, a2);
10316
10317 eassert (BEGV >= BEG);
10318 eassert (ZV <= Z && ZV >= BEGV);
10319
10320 unbind_to (count, Qnil);
10321 return rc;
10322 }
10323
10324
10325 /* Save state that should be preserved around the call to the function
10326 FN called in with_echo_area_buffer. */
10327
10328 static Lisp_Object
10329 with_echo_area_buffer_unwind_data (struct window *w)
10330 {
10331 int i = 0;
10332 Lisp_Object vector, tmp;
10333
10334 /* Reduce consing by keeping one vector in
10335 Vwith_echo_area_save_vector. */
10336 vector = Vwith_echo_area_save_vector;
10337 Vwith_echo_area_save_vector = Qnil;
10338
10339 if (NILP (vector))
10340 vector = Fmake_vector (make_number (9), Qnil);
10341
10342 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10343 ASET (vector, i, Vdeactivate_mark); ++i;
10344 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10345
10346 if (w)
10347 {
10348 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10349 ASET (vector, i, w->contents); ++i;
10350 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10351 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10352 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10353 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10354 }
10355 else
10356 {
10357 int end = i + 6;
10358 for (; i < end; ++i)
10359 ASET (vector, i, Qnil);
10360 }
10361
10362 eassert (i == ASIZE (vector));
10363 return vector;
10364 }
10365
10366
10367 /* Restore global state from VECTOR which was created by
10368 with_echo_area_buffer_unwind_data. */
10369
10370 static void
10371 unwind_with_echo_area_buffer (Lisp_Object vector)
10372 {
10373 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10374 Vdeactivate_mark = AREF (vector, 1);
10375 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10376
10377 if (WINDOWP (AREF (vector, 3)))
10378 {
10379 struct window *w;
10380 Lisp_Object buffer;
10381
10382 w = XWINDOW (AREF (vector, 3));
10383 buffer = AREF (vector, 4);
10384
10385 wset_buffer (w, buffer);
10386 set_marker_both (w->pointm, buffer,
10387 XFASTINT (AREF (vector, 5)),
10388 XFASTINT (AREF (vector, 6)));
10389 set_marker_both (w->start, buffer,
10390 XFASTINT (AREF (vector, 7)),
10391 XFASTINT (AREF (vector, 8)));
10392 }
10393
10394 Vwith_echo_area_save_vector = vector;
10395 }
10396
10397
10398 /* Set up the echo area for use by print functions. MULTIBYTE_P
10399 non-zero means we will print multibyte. */
10400
10401 void
10402 setup_echo_area_for_printing (int multibyte_p)
10403 {
10404 /* If we can't find an echo area any more, exit. */
10405 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10406 Fkill_emacs (Qnil);
10407
10408 ensure_echo_area_buffers ();
10409
10410 if (!message_buf_print)
10411 {
10412 /* A message has been output since the last time we printed.
10413 Choose a fresh echo area buffer. */
10414 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10415 echo_area_buffer[0] = echo_buffer[1];
10416 else
10417 echo_area_buffer[0] = echo_buffer[0];
10418
10419 /* Switch to that buffer and clear it. */
10420 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10421 bset_truncate_lines (current_buffer, Qnil);
10422
10423 if (Z > BEG)
10424 {
10425 ptrdiff_t count = SPECPDL_INDEX ();
10426 specbind (Qinhibit_read_only, Qt);
10427 /* Note that undo recording is always disabled. */
10428 del_range (BEG, Z);
10429 unbind_to (count, Qnil);
10430 }
10431 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10432
10433 /* Set up the buffer for the multibyteness we need. */
10434 if (multibyte_p
10435 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10436 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10437
10438 /* Raise the frame containing the echo area. */
10439 if (minibuffer_auto_raise)
10440 {
10441 struct frame *sf = SELECTED_FRAME ();
10442 Lisp_Object mini_window;
10443 mini_window = FRAME_MINIBUF_WINDOW (sf);
10444 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10445 }
10446
10447 message_log_maybe_newline ();
10448 message_buf_print = 1;
10449 }
10450 else
10451 {
10452 if (NILP (echo_area_buffer[0]))
10453 {
10454 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10455 echo_area_buffer[0] = echo_buffer[1];
10456 else
10457 echo_area_buffer[0] = echo_buffer[0];
10458 }
10459
10460 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10461 {
10462 /* Someone switched buffers between print requests. */
10463 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10464 bset_truncate_lines (current_buffer, Qnil);
10465 }
10466 }
10467 }
10468
10469
10470 /* Display an echo area message in window W. Value is non-zero if W's
10471 height is changed. If display_last_displayed_message_p is
10472 non-zero, display the message that was last displayed, otherwise
10473 display the current message. */
10474
10475 static int
10476 display_echo_area (struct window *w)
10477 {
10478 int i, no_message_p, window_height_changed_p;
10479
10480 /* Temporarily disable garbage collections while displaying the echo
10481 area. This is done because a GC can print a message itself.
10482 That message would modify the echo area buffer's contents while a
10483 redisplay of the buffer is going on, and seriously confuse
10484 redisplay. */
10485 ptrdiff_t count = inhibit_garbage_collection ();
10486
10487 /* If there is no message, we must call display_echo_area_1
10488 nevertheless because it resizes the window. But we will have to
10489 reset the echo_area_buffer in question to nil at the end because
10490 with_echo_area_buffer will sets it to an empty buffer. */
10491 i = display_last_displayed_message_p ? 1 : 0;
10492 no_message_p = NILP (echo_area_buffer[i]);
10493
10494 window_height_changed_p
10495 = with_echo_area_buffer (w, display_last_displayed_message_p,
10496 display_echo_area_1,
10497 (intptr_t) w, Qnil);
10498
10499 if (no_message_p)
10500 echo_area_buffer[i] = Qnil;
10501
10502 unbind_to (count, Qnil);
10503 return window_height_changed_p;
10504 }
10505
10506
10507 /* Helper for display_echo_area. Display the current buffer which
10508 contains the current echo area message in window W, a mini-window,
10509 a pointer to which is passed in A1. A2..A4 are currently not used.
10510 Change the height of W so that all of the message is displayed.
10511 Value is non-zero if height of W was changed. */
10512
10513 static int
10514 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10515 {
10516 intptr_t i1 = a1;
10517 struct window *w = (struct window *) i1;
10518 Lisp_Object window;
10519 struct text_pos start;
10520 int window_height_changed_p = 0;
10521
10522 /* Do this before displaying, so that we have a large enough glyph
10523 matrix for the display. If we can't get enough space for the
10524 whole text, display the last N lines. That works by setting w->start. */
10525 window_height_changed_p = resize_mini_window (w, 0);
10526
10527 /* Use the starting position chosen by resize_mini_window. */
10528 SET_TEXT_POS_FROM_MARKER (start, w->start);
10529
10530 /* Display. */
10531 clear_glyph_matrix (w->desired_matrix);
10532 XSETWINDOW (window, w);
10533 try_window (window, start, 0);
10534
10535 return window_height_changed_p;
10536 }
10537
10538
10539 /* Resize the echo area window to exactly the size needed for the
10540 currently displayed message, if there is one. If a mini-buffer
10541 is active, don't shrink it. */
10542
10543 void
10544 resize_echo_area_exactly (void)
10545 {
10546 if (BUFFERP (echo_area_buffer[0])
10547 && WINDOWP (echo_area_window))
10548 {
10549 struct window *w = XWINDOW (echo_area_window);
10550 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10551 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10552 (intptr_t) w, resize_exactly);
10553 if (resized_p)
10554 {
10555 windows_or_buffers_changed = 42;
10556 update_mode_lines = 30;
10557 redisplay_internal ();
10558 }
10559 }
10560 }
10561
10562
10563 /* Callback function for with_echo_area_buffer, when used from
10564 resize_echo_area_exactly. A1 contains a pointer to the window to
10565 resize, EXACTLY non-nil means resize the mini-window exactly to the
10566 size of the text displayed. A3 and A4 are not used. Value is what
10567 resize_mini_window returns. */
10568
10569 static int
10570 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10571 {
10572 intptr_t i1 = a1;
10573 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10574 }
10575
10576
10577 /* Resize mini-window W to fit the size of its contents. EXACT_P
10578 means size the window exactly to the size needed. Otherwise, it's
10579 only enlarged until W's buffer is empty.
10580
10581 Set W->start to the right place to begin display. If the whole
10582 contents fit, start at the beginning. Otherwise, start so as
10583 to make the end of the contents appear. This is particularly
10584 important for y-or-n-p, but seems desirable generally.
10585
10586 Value is non-zero if the window height has been changed. */
10587
10588 int
10589 resize_mini_window (struct window *w, int exact_p)
10590 {
10591 struct frame *f = XFRAME (w->frame);
10592 int window_height_changed_p = 0;
10593
10594 eassert (MINI_WINDOW_P (w));
10595
10596 /* By default, start display at the beginning. */
10597 set_marker_both (w->start, w->contents,
10598 BUF_BEGV (XBUFFER (w->contents)),
10599 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10600
10601 /* Don't resize windows while redisplaying a window; it would
10602 confuse redisplay functions when the size of the window they are
10603 displaying changes from under them. Such a resizing can happen,
10604 for instance, when which-func prints a long message while
10605 we are running fontification-functions. We're running these
10606 functions with safe_call which binds inhibit-redisplay to t. */
10607 if (!NILP (Vinhibit_redisplay))
10608 return 0;
10609
10610 /* Nil means don't try to resize. */
10611 if (NILP (Vresize_mini_windows)
10612 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10613 return 0;
10614
10615 if (!FRAME_MINIBUF_ONLY_P (f))
10616 {
10617 struct it it;
10618 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10619 + WINDOW_PIXEL_HEIGHT (w));
10620 int unit = FRAME_LINE_HEIGHT (f);
10621 int height, max_height;
10622 struct text_pos start;
10623 struct buffer *old_current_buffer = NULL;
10624
10625 if (current_buffer != XBUFFER (w->contents))
10626 {
10627 old_current_buffer = current_buffer;
10628 set_buffer_internal (XBUFFER (w->contents));
10629 }
10630
10631 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10632
10633 /* Compute the max. number of lines specified by the user. */
10634 if (FLOATP (Vmax_mini_window_height))
10635 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10636 else if (INTEGERP (Vmax_mini_window_height))
10637 max_height = XINT (Vmax_mini_window_height) * unit;
10638 else
10639 max_height = total_height / 4;
10640
10641 /* Correct that max. height if it's bogus. */
10642 max_height = clip_to_bounds (unit, max_height, total_height);
10643
10644 /* Find out the height of the text in the window. */
10645 if (it.line_wrap == TRUNCATE)
10646 height = unit;
10647 else
10648 {
10649 last_height = 0;
10650 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10651 if (it.max_ascent == 0 && it.max_descent == 0)
10652 height = it.current_y + last_height;
10653 else
10654 height = it.current_y + it.max_ascent + it.max_descent;
10655 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10656 }
10657
10658 /* Compute a suitable window start. */
10659 if (height > max_height)
10660 {
10661 height = max_height;
10662 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10663 move_it_vertically_backward (&it, height);
10664 start = it.current.pos;
10665 }
10666 else
10667 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10668 SET_MARKER_FROM_TEXT_POS (w->start, start);
10669
10670 if (EQ (Vresize_mini_windows, Qgrow_only))
10671 {
10672 /* Let it grow only, until we display an empty message, in which
10673 case the window shrinks again. */
10674 if (height > WINDOW_PIXEL_HEIGHT (w))
10675 {
10676 int old_height = WINDOW_PIXEL_HEIGHT (w);
10677
10678 FRAME_WINDOWS_FROZEN (f) = 1;
10679 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10680 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10681 }
10682 else if (height < WINDOW_PIXEL_HEIGHT (w)
10683 && (exact_p || BEGV == ZV))
10684 {
10685 int old_height = WINDOW_PIXEL_HEIGHT (w);
10686
10687 FRAME_WINDOWS_FROZEN (f) = 0;
10688 shrink_mini_window (w, 1);
10689 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10690 }
10691 }
10692 else
10693 {
10694 /* Always resize to exact size needed. */
10695 if (height > WINDOW_PIXEL_HEIGHT (w))
10696 {
10697 int old_height = WINDOW_PIXEL_HEIGHT (w);
10698
10699 FRAME_WINDOWS_FROZEN (f) = 1;
10700 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10701 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10702 }
10703 else if (height < WINDOW_PIXEL_HEIGHT (w))
10704 {
10705 int old_height = WINDOW_PIXEL_HEIGHT (w);
10706
10707 FRAME_WINDOWS_FROZEN (f) = 0;
10708 shrink_mini_window (w, 1);
10709
10710 if (height)
10711 {
10712 FRAME_WINDOWS_FROZEN (f) = 1;
10713 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10714 }
10715
10716 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10717 }
10718 }
10719
10720 if (old_current_buffer)
10721 set_buffer_internal (old_current_buffer);
10722 }
10723
10724 return window_height_changed_p;
10725 }
10726
10727
10728 /* Value is the current message, a string, or nil if there is no
10729 current message. */
10730
10731 Lisp_Object
10732 current_message (void)
10733 {
10734 Lisp_Object msg;
10735
10736 if (!BUFFERP (echo_area_buffer[0]))
10737 msg = Qnil;
10738 else
10739 {
10740 with_echo_area_buffer (0, 0, current_message_1,
10741 (intptr_t) &msg, Qnil);
10742 if (NILP (msg))
10743 echo_area_buffer[0] = Qnil;
10744 }
10745
10746 return msg;
10747 }
10748
10749
10750 static int
10751 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10752 {
10753 intptr_t i1 = a1;
10754 Lisp_Object *msg = (Lisp_Object *) i1;
10755
10756 if (Z > BEG)
10757 *msg = make_buffer_string (BEG, Z, 1);
10758 else
10759 *msg = Qnil;
10760 return 0;
10761 }
10762
10763
10764 /* Push the current message on Vmessage_stack for later restoration
10765 by restore_message. Value is non-zero if the current message isn't
10766 empty. This is a relatively infrequent operation, so it's not
10767 worth optimizing. */
10768
10769 bool
10770 push_message (void)
10771 {
10772 Lisp_Object msg = current_message ();
10773 Vmessage_stack = Fcons (msg, Vmessage_stack);
10774 return STRINGP (msg);
10775 }
10776
10777
10778 /* Restore message display from the top of Vmessage_stack. */
10779
10780 void
10781 restore_message (void)
10782 {
10783 eassert (CONSP (Vmessage_stack));
10784 message3_nolog (XCAR (Vmessage_stack));
10785 }
10786
10787
10788 /* Handler for unwind-protect calling pop_message. */
10789
10790 void
10791 pop_message_unwind (void)
10792 {
10793 /* Pop the top-most entry off Vmessage_stack. */
10794 eassert (CONSP (Vmessage_stack));
10795 Vmessage_stack = XCDR (Vmessage_stack);
10796 }
10797
10798
10799 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10800 exits. If the stack is not empty, we have a missing pop_message
10801 somewhere. */
10802
10803 void
10804 check_message_stack (void)
10805 {
10806 if (!NILP (Vmessage_stack))
10807 emacs_abort ();
10808 }
10809
10810
10811 /* Truncate to NCHARS what will be displayed in the echo area the next
10812 time we display it---but don't redisplay it now. */
10813
10814 void
10815 truncate_echo_area (ptrdiff_t nchars)
10816 {
10817 if (nchars == 0)
10818 echo_area_buffer[0] = Qnil;
10819 else if (!noninteractive
10820 && INTERACTIVE
10821 && !NILP (echo_area_buffer[0]))
10822 {
10823 struct frame *sf = SELECTED_FRAME ();
10824 /* Error messages get reported properly by cmd_error, so this must be
10825 just an informative message; if the frame hasn't really been
10826 initialized yet, just toss it. */
10827 if (sf->glyphs_initialized_p)
10828 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10829 }
10830 }
10831
10832
10833 /* Helper function for truncate_echo_area. Truncate the current
10834 message to at most NCHARS characters. */
10835
10836 static int
10837 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10838 {
10839 if (BEG + nchars < Z)
10840 del_range (BEG + nchars, Z);
10841 if (Z == BEG)
10842 echo_area_buffer[0] = Qnil;
10843 return 0;
10844 }
10845
10846 /* Set the current message to STRING. */
10847
10848 static void
10849 set_message (Lisp_Object string)
10850 {
10851 eassert (STRINGP (string));
10852
10853 message_enable_multibyte = STRING_MULTIBYTE (string);
10854
10855 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10856 message_buf_print = 0;
10857 help_echo_showing_p = 0;
10858
10859 if (STRINGP (Vdebug_on_message)
10860 && STRINGP (string)
10861 && fast_string_match (Vdebug_on_message, string) >= 0)
10862 call_debugger (list2 (Qerror, string));
10863 }
10864
10865
10866 /* Helper function for set_message. First argument is ignored and second
10867 argument has the same meaning as for set_message.
10868 This function is called with the echo area buffer being current. */
10869
10870 static int
10871 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10872 {
10873 eassert (STRINGP (string));
10874
10875 /* Change multibyteness of the echo buffer appropriately. */
10876 if (message_enable_multibyte
10877 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10878 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10879
10880 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10881 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10882 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10883
10884 /* Insert new message at BEG. */
10885 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10886
10887 /* This function takes care of single/multibyte conversion.
10888 We just have to ensure that the echo area buffer has the right
10889 setting of enable_multibyte_characters. */
10890 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10891
10892 return 0;
10893 }
10894
10895
10896 /* Clear messages. CURRENT_P non-zero means clear the current
10897 message. LAST_DISPLAYED_P non-zero means clear the message
10898 last displayed. */
10899
10900 void
10901 clear_message (bool current_p, bool last_displayed_p)
10902 {
10903 if (current_p)
10904 {
10905 echo_area_buffer[0] = Qnil;
10906 message_cleared_p = true;
10907 }
10908
10909 if (last_displayed_p)
10910 echo_area_buffer[1] = Qnil;
10911
10912 message_buf_print = 0;
10913 }
10914
10915 /* Clear garbaged frames.
10916
10917 This function is used where the old redisplay called
10918 redraw_garbaged_frames which in turn called redraw_frame which in
10919 turn called clear_frame. The call to clear_frame was a source of
10920 flickering. I believe a clear_frame is not necessary. It should
10921 suffice in the new redisplay to invalidate all current matrices,
10922 and ensure a complete redisplay of all windows. */
10923
10924 static void
10925 clear_garbaged_frames (void)
10926 {
10927 if (frame_garbaged)
10928 {
10929 Lisp_Object tail, frame;
10930
10931 FOR_EACH_FRAME (tail, frame)
10932 {
10933 struct frame *f = XFRAME (frame);
10934
10935 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10936 {
10937 if (f->resized_p)
10938 redraw_frame (f);
10939 else
10940 clear_current_matrices (f);
10941 fset_redisplay (f);
10942 f->garbaged = false;
10943 f->resized_p = false;
10944 }
10945 }
10946
10947 frame_garbaged = false;
10948 }
10949 }
10950
10951
10952 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10953 is non-zero update selected_frame. Value is non-zero if the
10954 mini-windows height has been changed. */
10955
10956 static int
10957 echo_area_display (int update_frame_p)
10958 {
10959 Lisp_Object mini_window;
10960 struct window *w;
10961 struct frame *f;
10962 int window_height_changed_p = 0;
10963 struct frame *sf = SELECTED_FRAME ();
10964
10965 mini_window = FRAME_MINIBUF_WINDOW (sf);
10966 w = XWINDOW (mini_window);
10967 f = XFRAME (WINDOW_FRAME (w));
10968
10969 /* Don't display if frame is invisible or not yet initialized. */
10970 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10971 return 0;
10972
10973 #ifdef HAVE_WINDOW_SYSTEM
10974 /* When Emacs starts, selected_frame may be the initial terminal
10975 frame. If we let this through, a message would be displayed on
10976 the terminal. */
10977 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10978 return 0;
10979 #endif /* HAVE_WINDOW_SYSTEM */
10980
10981 /* Redraw garbaged frames. */
10982 clear_garbaged_frames ();
10983
10984 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10985 {
10986 echo_area_window = mini_window;
10987 window_height_changed_p = display_echo_area (w);
10988 w->must_be_updated_p = true;
10989
10990 /* Update the display, unless called from redisplay_internal.
10991 Also don't update the screen during redisplay itself. The
10992 update will happen at the end of redisplay, and an update
10993 here could cause confusion. */
10994 if (update_frame_p && !redisplaying_p)
10995 {
10996 int n = 0;
10997
10998 /* If the display update has been interrupted by pending
10999 input, update mode lines in the frame. Due to the
11000 pending input, it might have been that redisplay hasn't
11001 been called, so that mode lines above the echo area are
11002 garbaged. This looks odd, so we prevent it here. */
11003 if (!display_completed)
11004 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11005
11006 if (window_height_changed_p
11007 /* Don't do this if Emacs is shutting down. Redisplay
11008 needs to run hooks. */
11009 && !NILP (Vrun_hooks))
11010 {
11011 /* Must update other windows. Likewise as in other
11012 cases, don't let this update be interrupted by
11013 pending input. */
11014 ptrdiff_t count = SPECPDL_INDEX ();
11015 specbind (Qredisplay_dont_pause, Qt);
11016 windows_or_buffers_changed = 44;
11017 redisplay_internal ();
11018 unbind_to (count, Qnil);
11019 }
11020 else if (FRAME_WINDOW_P (f) && n == 0)
11021 {
11022 /* Window configuration is the same as before.
11023 Can do with a display update of the echo area,
11024 unless we displayed some mode lines. */
11025 update_single_window (w, 1);
11026 flush_frame (f);
11027 }
11028 else
11029 update_frame (f, 1, 1);
11030
11031 /* If cursor is in the echo area, make sure that the next
11032 redisplay displays the minibuffer, so that the cursor will
11033 be replaced with what the minibuffer wants. */
11034 if (cursor_in_echo_area)
11035 wset_redisplay (XWINDOW (mini_window));
11036 }
11037 }
11038 else if (!EQ (mini_window, selected_window))
11039 wset_redisplay (XWINDOW (mini_window));
11040
11041 /* Last displayed message is now the current message. */
11042 echo_area_buffer[1] = echo_area_buffer[0];
11043 /* Inform read_char that we're not echoing. */
11044 echo_message_buffer = Qnil;
11045
11046 /* Prevent redisplay optimization in redisplay_internal by resetting
11047 this_line_start_pos. This is done because the mini-buffer now
11048 displays the message instead of its buffer text. */
11049 if (EQ (mini_window, selected_window))
11050 CHARPOS (this_line_start_pos) = 0;
11051
11052 return window_height_changed_p;
11053 }
11054
11055 /* Nonzero if W's buffer was changed but not saved. */
11056
11057 static int
11058 window_buffer_changed (struct window *w)
11059 {
11060 struct buffer *b = XBUFFER (w->contents);
11061
11062 eassert (BUFFER_LIVE_P (b));
11063
11064 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11065 }
11066
11067 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11068
11069 static int
11070 mode_line_update_needed (struct window *w)
11071 {
11072 return (w->column_number_displayed != -1
11073 && !(PT == w->last_point && !window_outdated (w))
11074 && (w->column_number_displayed != current_column ()));
11075 }
11076
11077 /* Nonzero if window start of W is frozen and may not be changed during
11078 redisplay. */
11079
11080 static bool
11081 window_frozen_p (struct window *w)
11082 {
11083 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11084 {
11085 Lisp_Object window;
11086
11087 XSETWINDOW (window, w);
11088 if (MINI_WINDOW_P (w))
11089 return 0;
11090 else if (EQ (window, selected_window))
11091 return 0;
11092 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11093 && EQ (window, Vminibuf_scroll_window))
11094 /* This special window can't be frozen too. */
11095 return 0;
11096 else
11097 return 1;
11098 }
11099 return 0;
11100 }
11101
11102 /***********************************************************************
11103 Mode Lines and Frame Titles
11104 ***********************************************************************/
11105
11106 /* A buffer for constructing non-propertized mode-line strings and
11107 frame titles in it; allocated from the heap in init_xdisp and
11108 resized as needed in store_mode_line_noprop_char. */
11109
11110 static char *mode_line_noprop_buf;
11111
11112 /* The buffer's end, and a current output position in it. */
11113
11114 static char *mode_line_noprop_buf_end;
11115 static char *mode_line_noprop_ptr;
11116
11117 #define MODE_LINE_NOPROP_LEN(start) \
11118 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11119
11120 static enum {
11121 MODE_LINE_DISPLAY = 0,
11122 MODE_LINE_TITLE,
11123 MODE_LINE_NOPROP,
11124 MODE_LINE_STRING
11125 } mode_line_target;
11126
11127 /* Alist that caches the results of :propertize.
11128 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11129 static Lisp_Object mode_line_proptrans_alist;
11130
11131 /* List of strings making up the mode-line. */
11132 static Lisp_Object mode_line_string_list;
11133
11134 /* Base face property when building propertized mode line string. */
11135 static Lisp_Object mode_line_string_face;
11136 static Lisp_Object mode_line_string_face_prop;
11137
11138
11139 /* Unwind data for mode line strings */
11140
11141 static Lisp_Object Vmode_line_unwind_vector;
11142
11143 static Lisp_Object
11144 format_mode_line_unwind_data (struct frame *target_frame,
11145 struct buffer *obuf,
11146 Lisp_Object owin,
11147 int save_proptrans)
11148 {
11149 Lisp_Object vector, tmp;
11150
11151 /* Reduce consing by keeping one vector in
11152 Vwith_echo_area_save_vector. */
11153 vector = Vmode_line_unwind_vector;
11154 Vmode_line_unwind_vector = Qnil;
11155
11156 if (NILP (vector))
11157 vector = Fmake_vector (make_number (10), Qnil);
11158
11159 ASET (vector, 0, make_number (mode_line_target));
11160 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11161 ASET (vector, 2, mode_line_string_list);
11162 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11163 ASET (vector, 4, mode_line_string_face);
11164 ASET (vector, 5, mode_line_string_face_prop);
11165
11166 if (obuf)
11167 XSETBUFFER (tmp, obuf);
11168 else
11169 tmp = Qnil;
11170 ASET (vector, 6, tmp);
11171 ASET (vector, 7, owin);
11172 if (target_frame)
11173 {
11174 /* Similarly to `with-selected-window', if the operation selects
11175 a window on another frame, we must restore that frame's
11176 selected window, and (for a tty) the top-frame. */
11177 ASET (vector, 8, target_frame->selected_window);
11178 if (FRAME_TERMCAP_P (target_frame))
11179 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11180 }
11181
11182 return vector;
11183 }
11184
11185 static void
11186 unwind_format_mode_line (Lisp_Object vector)
11187 {
11188 Lisp_Object old_window = AREF (vector, 7);
11189 Lisp_Object target_frame_window = AREF (vector, 8);
11190 Lisp_Object old_top_frame = AREF (vector, 9);
11191
11192 mode_line_target = XINT (AREF (vector, 0));
11193 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11194 mode_line_string_list = AREF (vector, 2);
11195 if (! EQ (AREF (vector, 3), Qt))
11196 mode_line_proptrans_alist = AREF (vector, 3);
11197 mode_line_string_face = AREF (vector, 4);
11198 mode_line_string_face_prop = AREF (vector, 5);
11199
11200 /* Select window before buffer, since it may change the buffer. */
11201 if (!NILP (old_window))
11202 {
11203 /* If the operation that we are unwinding had selected a window
11204 on a different frame, reset its frame-selected-window. For a
11205 text terminal, reset its top-frame if necessary. */
11206 if (!NILP (target_frame_window))
11207 {
11208 Lisp_Object frame
11209 = WINDOW_FRAME (XWINDOW (target_frame_window));
11210
11211 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11212 Fselect_window (target_frame_window, Qt);
11213
11214 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11215 Fselect_frame (old_top_frame, Qt);
11216 }
11217
11218 Fselect_window (old_window, Qt);
11219 }
11220
11221 if (!NILP (AREF (vector, 6)))
11222 {
11223 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11224 ASET (vector, 6, Qnil);
11225 }
11226
11227 Vmode_line_unwind_vector = vector;
11228 }
11229
11230
11231 /* Store a single character C for the frame title in mode_line_noprop_buf.
11232 Re-allocate mode_line_noprop_buf if necessary. */
11233
11234 static void
11235 store_mode_line_noprop_char (char c)
11236 {
11237 /* If output position has reached the end of the allocated buffer,
11238 increase the buffer's size. */
11239 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11240 {
11241 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11242 ptrdiff_t size = len;
11243 mode_line_noprop_buf =
11244 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11245 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11246 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11247 }
11248
11249 *mode_line_noprop_ptr++ = c;
11250 }
11251
11252
11253 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11254 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11255 characters that yield more columns than PRECISION; PRECISION <= 0
11256 means copy the whole string. Pad with spaces until FIELD_WIDTH
11257 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11258 pad. Called from display_mode_element when it is used to build a
11259 frame title. */
11260
11261 static int
11262 store_mode_line_noprop (const char *string, int field_width, int precision)
11263 {
11264 const unsigned char *str = (const unsigned char *) string;
11265 int n = 0;
11266 ptrdiff_t dummy, nbytes;
11267
11268 /* Copy at most PRECISION chars from STR. */
11269 nbytes = strlen (string);
11270 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11271 while (nbytes--)
11272 store_mode_line_noprop_char (*str++);
11273
11274 /* Fill up with spaces until FIELD_WIDTH reached. */
11275 while (field_width > 0
11276 && n < field_width)
11277 {
11278 store_mode_line_noprop_char (' ');
11279 ++n;
11280 }
11281
11282 return n;
11283 }
11284
11285 /***********************************************************************
11286 Frame Titles
11287 ***********************************************************************/
11288
11289 #ifdef HAVE_WINDOW_SYSTEM
11290
11291 /* Set the title of FRAME, if it has changed. The title format is
11292 Vicon_title_format if FRAME is iconified, otherwise it is
11293 frame_title_format. */
11294
11295 static void
11296 x_consider_frame_title (Lisp_Object frame)
11297 {
11298 struct frame *f = XFRAME (frame);
11299
11300 if (FRAME_WINDOW_P (f)
11301 || FRAME_MINIBUF_ONLY_P (f)
11302 || f->explicit_name)
11303 {
11304 /* Do we have more than one visible frame on this X display? */
11305 Lisp_Object tail, other_frame, fmt;
11306 ptrdiff_t title_start;
11307 char *title;
11308 ptrdiff_t len;
11309 struct it it;
11310 ptrdiff_t count = SPECPDL_INDEX ();
11311
11312 FOR_EACH_FRAME (tail, other_frame)
11313 {
11314 struct frame *tf = XFRAME (other_frame);
11315
11316 if (tf != f
11317 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11318 && !FRAME_MINIBUF_ONLY_P (tf)
11319 && !EQ (other_frame, tip_frame)
11320 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11321 break;
11322 }
11323
11324 /* Set global variable indicating that multiple frames exist. */
11325 multiple_frames = CONSP (tail);
11326
11327 /* Switch to the buffer of selected window of the frame. Set up
11328 mode_line_target so that display_mode_element will output into
11329 mode_line_noprop_buf; then display the title. */
11330 record_unwind_protect (unwind_format_mode_line,
11331 format_mode_line_unwind_data
11332 (f, current_buffer, selected_window, 0));
11333
11334 Fselect_window (f->selected_window, Qt);
11335 set_buffer_internal_1
11336 (XBUFFER (XWINDOW (f->selected_window)->contents));
11337 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11338
11339 mode_line_target = MODE_LINE_TITLE;
11340 title_start = MODE_LINE_NOPROP_LEN (0);
11341 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11342 NULL, DEFAULT_FACE_ID);
11343 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11344 len = MODE_LINE_NOPROP_LEN (title_start);
11345 title = mode_line_noprop_buf + title_start;
11346 unbind_to (count, Qnil);
11347
11348 /* Set the title only if it's changed. This avoids consing in
11349 the common case where it hasn't. (If it turns out that we've
11350 already wasted too much time by walking through the list with
11351 display_mode_element, then we might need to optimize at a
11352 higher level than this.) */
11353 if (! STRINGP (f->name)
11354 || SBYTES (f->name) != len
11355 || memcmp (title, SDATA (f->name), len) != 0)
11356 x_implicitly_set_name (f, make_string (title, len), Qnil);
11357 }
11358 }
11359
11360 #endif /* not HAVE_WINDOW_SYSTEM */
11361
11362 \f
11363 /***********************************************************************
11364 Menu Bars
11365 ***********************************************************************/
11366
11367 /* Non-zero if we will not redisplay all visible windows. */
11368 #define REDISPLAY_SOME_P() \
11369 ((windows_or_buffers_changed == 0 \
11370 || windows_or_buffers_changed == REDISPLAY_SOME) \
11371 && (update_mode_lines == 0 \
11372 || update_mode_lines == REDISPLAY_SOME))
11373
11374 /* Prepare for redisplay by updating menu-bar item lists when
11375 appropriate. This can call eval. */
11376
11377 static void
11378 prepare_menu_bars (void)
11379 {
11380 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11381 bool some_windows = REDISPLAY_SOME_P ();
11382 struct gcpro gcpro1, gcpro2;
11383 Lisp_Object tooltip_frame;
11384
11385 #ifdef HAVE_WINDOW_SYSTEM
11386 tooltip_frame = tip_frame;
11387 #else
11388 tooltip_frame = Qnil;
11389 #endif
11390
11391 if (FUNCTIONP (Vpre_redisplay_function))
11392 {
11393 Lisp_Object windows = all_windows ? Qt : Qnil;
11394 if (all_windows && some_windows)
11395 {
11396 Lisp_Object ws = window_list ();
11397 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11398 {
11399 Lisp_Object this = XCAR (ws);
11400 struct window *w = XWINDOW (this);
11401 if (w->redisplay
11402 || XFRAME (w->frame)->redisplay
11403 || XBUFFER (w->contents)->text->redisplay)
11404 {
11405 windows = Fcons (this, windows);
11406 }
11407 }
11408 }
11409 safe_call1 (Vpre_redisplay_function, windows);
11410 }
11411
11412 /* Update all frame titles based on their buffer names, etc. We do
11413 this before the menu bars so that the buffer-menu will show the
11414 up-to-date frame titles. */
11415 #ifdef HAVE_WINDOW_SYSTEM
11416 if (all_windows)
11417 {
11418 Lisp_Object tail, frame;
11419
11420 FOR_EACH_FRAME (tail, frame)
11421 {
11422 struct frame *f = XFRAME (frame);
11423 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11424 if (some_windows
11425 && !f->redisplay
11426 && !w->redisplay
11427 && !XBUFFER (w->contents)->text->redisplay)
11428 continue;
11429
11430 if (!EQ (frame, tooltip_frame)
11431 && (FRAME_ICONIFIED_P (f)
11432 || FRAME_VISIBLE_P (f) == 1
11433 /* Exclude TTY frames that are obscured because they
11434 are not the top frame on their console. This is
11435 because x_consider_frame_title actually switches
11436 to the frame, which for TTY frames means it is
11437 marked as garbaged, and will be completely
11438 redrawn on the next redisplay cycle. This causes
11439 TTY frames to be completely redrawn, when there
11440 are more than one of them, even though nothing
11441 should be changed on display. */
11442 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11443 x_consider_frame_title (frame);
11444 }
11445 }
11446 #endif /* HAVE_WINDOW_SYSTEM */
11447
11448 /* Update the menu bar item lists, if appropriate. This has to be
11449 done before any actual redisplay or generation of display lines. */
11450
11451 if (all_windows)
11452 {
11453 Lisp_Object tail, frame;
11454 ptrdiff_t count = SPECPDL_INDEX ();
11455 /* 1 means that update_menu_bar has run its hooks
11456 so any further calls to update_menu_bar shouldn't do so again. */
11457 int menu_bar_hooks_run = 0;
11458
11459 record_unwind_save_match_data ();
11460
11461 FOR_EACH_FRAME (tail, frame)
11462 {
11463 struct frame *f = XFRAME (frame);
11464 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11465
11466 /* Ignore tooltip frame. */
11467 if (EQ (frame, tooltip_frame))
11468 continue;
11469
11470 if (some_windows
11471 && !f->redisplay
11472 && !w->redisplay
11473 && !XBUFFER (w->contents)->text->redisplay)
11474 continue;
11475
11476 /* If a window on this frame changed size, report that to
11477 the user and clear the size-change flag. */
11478 if (FRAME_WINDOW_SIZES_CHANGED (f))
11479 {
11480 Lisp_Object functions;
11481
11482 /* Clear flag first in case we get an error below. */
11483 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11484 functions = Vwindow_size_change_functions;
11485 GCPRO2 (tail, functions);
11486
11487 while (CONSP (functions))
11488 {
11489 if (!EQ (XCAR (functions), Qt))
11490 call1 (XCAR (functions), frame);
11491 functions = XCDR (functions);
11492 }
11493 UNGCPRO;
11494 }
11495
11496 GCPRO1 (tail);
11497 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11498 #ifdef HAVE_WINDOW_SYSTEM
11499 update_tool_bar (f, 0);
11500 #endif
11501 #ifdef HAVE_NS
11502 if (windows_or_buffers_changed
11503 && FRAME_NS_P (f))
11504 ns_set_doc_edited
11505 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11506 #endif
11507 UNGCPRO;
11508 }
11509
11510 unbind_to (count, Qnil);
11511 }
11512 else
11513 {
11514 struct frame *sf = SELECTED_FRAME ();
11515 update_menu_bar (sf, 1, 0);
11516 #ifdef HAVE_WINDOW_SYSTEM
11517 update_tool_bar (sf, 1);
11518 #endif
11519 }
11520 }
11521
11522
11523 /* Update the menu bar item list for frame F. This has to be done
11524 before we start to fill in any display lines, because it can call
11525 eval.
11526
11527 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11528
11529 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11530 already ran the menu bar hooks for this redisplay, so there
11531 is no need to run them again. The return value is the
11532 updated value of this flag, to pass to the next call. */
11533
11534 static int
11535 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11536 {
11537 Lisp_Object window;
11538 register struct window *w;
11539
11540 /* If called recursively during a menu update, do nothing. This can
11541 happen when, for instance, an activate-menubar-hook causes a
11542 redisplay. */
11543 if (inhibit_menubar_update)
11544 return hooks_run;
11545
11546 window = FRAME_SELECTED_WINDOW (f);
11547 w = XWINDOW (window);
11548
11549 if (FRAME_WINDOW_P (f)
11550 ?
11551 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11552 || defined (HAVE_NS) || defined (USE_GTK)
11553 FRAME_EXTERNAL_MENU_BAR (f)
11554 #else
11555 FRAME_MENU_BAR_LINES (f) > 0
11556 #endif
11557 : FRAME_MENU_BAR_LINES (f) > 0)
11558 {
11559 /* If the user has switched buffers or windows, we need to
11560 recompute to reflect the new bindings. But we'll
11561 recompute when update_mode_lines is set too; that means
11562 that people can use force-mode-line-update to request
11563 that the menu bar be recomputed. The adverse effect on
11564 the rest of the redisplay algorithm is about the same as
11565 windows_or_buffers_changed anyway. */
11566 if (windows_or_buffers_changed
11567 /* This used to test w->update_mode_line, but we believe
11568 there is no need to recompute the menu in that case. */
11569 || update_mode_lines
11570 || window_buffer_changed (w))
11571 {
11572 struct buffer *prev = current_buffer;
11573 ptrdiff_t count = SPECPDL_INDEX ();
11574
11575 specbind (Qinhibit_menubar_update, Qt);
11576
11577 set_buffer_internal_1 (XBUFFER (w->contents));
11578 if (save_match_data)
11579 record_unwind_save_match_data ();
11580 if (NILP (Voverriding_local_map_menu_flag))
11581 {
11582 specbind (Qoverriding_terminal_local_map, Qnil);
11583 specbind (Qoverriding_local_map, Qnil);
11584 }
11585
11586 if (!hooks_run)
11587 {
11588 /* Run the Lucid hook. */
11589 safe_run_hooks (Qactivate_menubar_hook);
11590
11591 /* If it has changed current-menubar from previous value,
11592 really recompute the menu-bar from the value. */
11593 if (! NILP (Vlucid_menu_bar_dirty_flag))
11594 call0 (Qrecompute_lucid_menubar);
11595
11596 safe_run_hooks (Qmenu_bar_update_hook);
11597
11598 hooks_run = 1;
11599 }
11600
11601 XSETFRAME (Vmenu_updating_frame, f);
11602 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11603
11604 /* Redisplay the menu bar in case we changed it. */
11605 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11606 || defined (HAVE_NS) || defined (USE_GTK)
11607 if (FRAME_WINDOW_P (f))
11608 {
11609 #if defined (HAVE_NS)
11610 /* All frames on Mac OS share the same menubar. So only
11611 the selected frame should be allowed to set it. */
11612 if (f == SELECTED_FRAME ())
11613 #endif
11614 set_frame_menubar (f, 0, 0);
11615 }
11616 else
11617 /* On a terminal screen, the menu bar is an ordinary screen
11618 line, and this makes it get updated. */
11619 w->update_mode_line = 1;
11620 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11621 /* In the non-toolkit version, the menu bar is an ordinary screen
11622 line, and this makes it get updated. */
11623 w->update_mode_line = 1;
11624 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11625
11626 unbind_to (count, Qnil);
11627 set_buffer_internal_1 (prev);
11628 }
11629 }
11630
11631 return hooks_run;
11632 }
11633
11634 /***********************************************************************
11635 Tool-bars
11636 ***********************************************************************/
11637
11638 #ifdef HAVE_WINDOW_SYSTEM
11639
11640 /* Tool-bar item index of the item on which a mouse button was pressed
11641 or -1. */
11642
11643 int last_tool_bar_item;
11644
11645 /* Select `frame' temporarily without running all the code in
11646 do_switch_frame.
11647 FIXME: Maybe do_switch_frame should be trimmed down similarly
11648 when `norecord' is set. */
11649 static void
11650 fast_set_selected_frame (Lisp_Object frame)
11651 {
11652 if (!EQ (selected_frame, frame))
11653 {
11654 selected_frame = frame;
11655 selected_window = XFRAME (frame)->selected_window;
11656 }
11657 }
11658
11659 /* Update the tool-bar item list for frame F. This has to be done
11660 before we start to fill in any display lines. Called from
11661 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11662 and restore it here. */
11663
11664 static void
11665 update_tool_bar (struct frame *f, int save_match_data)
11666 {
11667 #if defined (USE_GTK) || defined (HAVE_NS)
11668 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11669 #else
11670 int do_update = (WINDOWP (f->tool_bar_window)
11671 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11672 #endif
11673
11674 if (do_update)
11675 {
11676 Lisp_Object window;
11677 struct window *w;
11678
11679 window = FRAME_SELECTED_WINDOW (f);
11680 w = XWINDOW (window);
11681
11682 /* If the user has switched buffers or windows, we need to
11683 recompute to reflect the new bindings. But we'll
11684 recompute when update_mode_lines is set too; that means
11685 that people can use force-mode-line-update to request
11686 that the menu bar be recomputed. The adverse effect on
11687 the rest of the redisplay algorithm is about the same as
11688 windows_or_buffers_changed anyway. */
11689 if (windows_or_buffers_changed
11690 || w->update_mode_line
11691 || update_mode_lines
11692 || window_buffer_changed (w))
11693 {
11694 struct buffer *prev = current_buffer;
11695 ptrdiff_t count = SPECPDL_INDEX ();
11696 Lisp_Object frame, new_tool_bar;
11697 int new_n_tool_bar;
11698 struct gcpro gcpro1;
11699
11700 /* Set current_buffer to the buffer of the selected
11701 window of the frame, so that we get the right local
11702 keymaps. */
11703 set_buffer_internal_1 (XBUFFER (w->contents));
11704
11705 /* Save match data, if we must. */
11706 if (save_match_data)
11707 record_unwind_save_match_data ();
11708
11709 /* Make sure that we don't accidentally use bogus keymaps. */
11710 if (NILP (Voverriding_local_map_menu_flag))
11711 {
11712 specbind (Qoverriding_terminal_local_map, Qnil);
11713 specbind (Qoverriding_local_map, Qnil);
11714 }
11715
11716 GCPRO1 (new_tool_bar);
11717
11718 /* We must temporarily set the selected frame to this frame
11719 before calling tool_bar_items, because the calculation of
11720 the tool-bar keymap uses the selected frame (see
11721 `tool-bar-make-keymap' in tool-bar.el). */
11722 eassert (EQ (selected_window,
11723 /* Since we only explicitly preserve selected_frame,
11724 check that selected_window would be redundant. */
11725 XFRAME (selected_frame)->selected_window));
11726 record_unwind_protect (fast_set_selected_frame, selected_frame);
11727 XSETFRAME (frame, f);
11728 fast_set_selected_frame (frame);
11729
11730 /* Build desired tool-bar items from keymaps. */
11731 new_tool_bar
11732 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11733 &new_n_tool_bar);
11734
11735 /* Redisplay the tool-bar if we changed it. */
11736 if (new_n_tool_bar != f->n_tool_bar_items
11737 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11738 {
11739 /* Redisplay that happens asynchronously due to an expose event
11740 may access f->tool_bar_items. Make sure we update both
11741 variables within BLOCK_INPUT so no such event interrupts. */
11742 block_input ();
11743 fset_tool_bar_items (f, new_tool_bar);
11744 f->n_tool_bar_items = new_n_tool_bar;
11745 w->update_mode_line = 1;
11746 unblock_input ();
11747 }
11748
11749 UNGCPRO;
11750
11751 unbind_to (count, Qnil);
11752 set_buffer_internal_1 (prev);
11753 }
11754 }
11755 }
11756
11757 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11758
11759 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11760 F's desired tool-bar contents. F->tool_bar_items must have
11761 been set up previously by calling prepare_menu_bars. */
11762
11763 static void
11764 build_desired_tool_bar_string (struct frame *f)
11765 {
11766 int i, size, size_needed;
11767 struct gcpro gcpro1, gcpro2, gcpro3;
11768 Lisp_Object image, plist, props;
11769
11770 image = plist = props = Qnil;
11771 GCPRO3 (image, plist, props);
11772
11773 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11774 Otherwise, make a new string. */
11775
11776 /* The size of the string we might be able to reuse. */
11777 size = (STRINGP (f->desired_tool_bar_string)
11778 ? SCHARS (f->desired_tool_bar_string)
11779 : 0);
11780
11781 /* We need one space in the string for each image. */
11782 size_needed = f->n_tool_bar_items;
11783
11784 /* Reuse f->desired_tool_bar_string, if possible. */
11785 if (size < size_needed || NILP (f->desired_tool_bar_string))
11786 fset_desired_tool_bar_string
11787 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11788 else
11789 {
11790 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11791 Fremove_text_properties (make_number (0), make_number (size),
11792 props, f->desired_tool_bar_string);
11793 }
11794
11795 /* Put a `display' property on the string for the images to display,
11796 put a `menu_item' property on tool-bar items with a value that
11797 is the index of the item in F's tool-bar item vector. */
11798 for (i = 0; i < f->n_tool_bar_items; ++i)
11799 {
11800 #define PROP(IDX) \
11801 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11802
11803 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11804 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11805 int hmargin, vmargin, relief, idx, end;
11806
11807 /* If image is a vector, choose the image according to the
11808 button state. */
11809 image = PROP (TOOL_BAR_ITEM_IMAGES);
11810 if (VECTORP (image))
11811 {
11812 if (enabled_p)
11813 idx = (selected_p
11814 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11815 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11816 else
11817 idx = (selected_p
11818 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11819 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11820
11821 eassert (ASIZE (image) >= idx);
11822 image = AREF (image, idx);
11823 }
11824 else
11825 idx = -1;
11826
11827 /* Ignore invalid image specifications. */
11828 if (!valid_image_p (image))
11829 continue;
11830
11831 /* Display the tool-bar button pressed, or depressed. */
11832 plist = Fcopy_sequence (XCDR (image));
11833
11834 /* Compute margin and relief to draw. */
11835 relief = (tool_bar_button_relief >= 0
11836 ? tool_bar_button_relief
11837 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11838 hmargin = vmargin = relief;
11839
11840 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11841 INT_MAX - max (hmargin, vmargin)))
11842 {
11843 hmargin += XFASTINT (Vtool_bar_button_margin);
11844 vmargin += XFASTINT (Vtool_bar_button_margin);
11845 }
11846 else if (CONSP (Vtool_bar_button_margin))
11847 {
11848 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11849 INT_MAX - hmargin))
11850 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11851
11852 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11853 INT_MAX - vmargin))
11854 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11855 }
11856
11857 if (auto_raise_tool_bar_buttons_p)
11858 {
11859 /* Add a `:relief' property to the image spec if the item is
11860 selected. */
11861 if (selected_p)
11862 {
11863 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11864 hmargin -= relief;
11865 vmargin -= relief;
11866 }
11867 }
11868 else
11869 {
11870 /* If image is selected, display it pressed, i.e. with a
11871 negative relief. If it's not selected, display it with a
11872 raised relief. */
11873 plist = Fplist_put (plist, QCrelief,
11874 (selected_p
11875 ? make_number (-relief)
11876 : make_number (relief)));
11877 hmargin -= relief;
11878 vmargin -= relief;
11879 }
11880
11881 /* Put a margin around the image. */
11882 if (hmargin || vmargin)
11883 {
11884 if (hmargin == vmargin)
11885 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11886 else
11887 plist = Fplist_put (plist, QCmargin,
11888 Fcons (make_number (hmargin),
11889 make_number (vmargin)));
11890 }
11891
11892 /* If button is not enabled, and we don't have special images
11893 for the disabled state, make the image appear disabled by
11894 applying an appropriate algorithm to it. */
11895 if (!enabled_p && idx < 0)
11896 plist = Fplist_put (plist, QCconversion, Qdisabled);
11897
11898 /* Put a `display' text property on the string for the image to
11899 display. Put a `menu-item' property on the string that gives
11900 the start of this item's properties in the tool-bar items
11901 vector. */
11902 image = Fcons (Qimage, plist);
11903 props = list4 (Qdisplay, image,
11904 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11905
11906 /* Let the last image hide all remaining spaces in the tool bar
11907 string. The string can be longer than needed when we reuse a
11908 previous string. */
11909 if (i + 1 == f->n_tool_bar_items)
11910 end = SCHARS (f->desired_tool_bar_string);
11911 else
11912 end = i + 1;
11913 Fadd_text_properties (make_number (i), make_number (end),
11914 props, f->desired_tool_bar_string);
11915 #undef PROP
11916 }
11917
11918 UNGCPRO;
11919 }
11920
11921
11922 /* Display one line of the tool-bar of frame IT->f.
11923
11924 HEIGHT specifies the desired height of the tool-bar line.
11925 If the actual height of the glyph row is less than HEIGHT, the
11926 row's height is increased to HEIGHT, and the icons are centered
11927 vertically in the new height.
11928
11929 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11930 count a final empty row in case the tool-bar width exactly matches
11931 the window width.
11932 */
11933
11934 static void
11935 display_tool_bar_line (struct it *it, int height)
11936 {
11937 struct glyph_row *row = it->glyph_row;
11938 int max_x = it->last_visible_x;
11939 struct glyph *last;
11940
11941 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
11942 clear_glyph_row (row);
11943 row->enabled_p = true;
11944 row->y = it->current_y;
11945
11946 /* Note that this isn't made use of if the face hasn't a box,
11947 so there's no need to check the face here. */
11948 it->start_of_box_run_p = 1;
11949
11950 while (it->current_x < max_x)
11951 {
11952 int x, n_glyphs_before, i, nglyphs;
11953 struct it it_before;
11954
11955 /* Get the next display element. */
11956 if (!get_next_display_element (it))
11957 {
11958 /* Don't count empty row if we are counting needed tool-bar lines. */
11959 if (height < 0 && !it->hpos)
11960 return;
11961 break;
11962 }
11963
11964 /* Produce glyphs. */
11965 n_glyphs_before = row->used[TEXT_AREA];
11966 it_before = *it;
11967
11968 PRODUCE_GLYPHS (it);
11969
11970 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11971 i = 0;
11972 x = it_before.current_x;
11973 while (i < nglyphs)
11974 {
11975 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11976
11977 if (x + glyph->pixel_width > max_x)
11978 {
11979 /* Glyph doesn't fit on line. Backtrack. */
11980 row->used[TEXT_AREA] = n_glyphs_before;
11981 *it = it_before;
11982 /* If this is the only glyph on this line, it will never fit on the
11983 tool-bar, so skip it. But ensure there is at least one glyph,
11984 so we don't accidentally disable the tool-bar. */
11985 if (n_glyphs_before == 0
11986 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11987 break;
11988 goto out;
11989 }
11990
11991 ++it->hpos;
11992 x += glyph->pixel_width;
11993 ++i;
11994 }
11995
11996 /* Stop at line end. */
11997 if (ITERATOR_AT_END_OF_LINE_P (it))
11998 break;
11999
12000 set_iterator_to_next (it, 1);
12001 }
12002
12003 out:;
12004
12005 row->displays_text_p = row->used[TEXT_AREA] != 0;
12006
12007 /* Use default face for the border below the tool bar.
12008
12009 FIXME: When auto-resize-tool-bars is grow-only, there is
12010 no additional border below the possibly empty tool-bar lines.
12011 So to make the extra empty lines look "normal", we have to
12012 use the tool-bar face for the border too. */
12013 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12014 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12015 it->face_id = DEFAULT_FACE_ID;
12016
12017 extend_face_to_end_of_line (it);
12018 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12019 last->right_box_line_p = 1;
12020 if (last == row->glyphs[TEXT_AREA])
12021 last->left_box_line_p = 1;
12022
12023 /* Make line the desired height and center it vertically. */
12024 if ((height -= it->max_ascent + it->max_descent) > 0)
12025 {
12026 /* Don't add more than one line height. */
12027 height %= FRAME_LINE_HEIGHT (it->f);
12028 it->max_ascent += height / 2;
12029 it->max_descent += (height + 1) / 2;
12030 }
12031
12032 compute_line_metrics (it);
12033
12034 /* If line is empty, make it occupy the rest of the tool-bar. */
12035 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12036 {
12037 row->height = row->phys_height = it->last_visible_y - row->y;
12038 row->visible_height = row->height;
12039 row->ascent = row->phys_ascent = 0;
12040 row->extra_line_spacing = 0;
12041 }
12042
12043 row->full_width_p = 1;
12044 row->continued_p = 0;
12045 row->truncated_on_left_p = 0;
12046 row->truncated_on_right_p = 0;
12047
12048 it->current_x = it->hpos = 0;
12049 it->current_y += row->height;
12050 ++it->vpos;
12051 ++it->glyph_row;
12052 }
12053
12054
12055 /* Max tool-bar height. Basically, this is what makes all other windows
12056 disappear when the frame gets too small. Rethink this! */
12057
12058 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12059 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12060
12061 /* Value is the number of pixels needed to make all tool-bar items of
12062 frame F visible. The actual number of glyph rows needed is
12063 returned in *N_ROWS if non-NULL. */
12064
12065 static int
12066 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12067 {
12068 struct window *w = XWINDOW (f->tool_bar_window);
12069 struct it it;
12070 /* tool_bar_height is called from redisplay_tool_bar after building
12071 the desired matrix, so use (unused) mode-line row as temporary row to
12072 avoid destroying the first tool-bar row. */
12073 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12074
12075 /* Initialize an iterator for iteration over
12076 F->desired_tool_bar_string in the tool-bar window of frame F. */
12077 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12078 it.first_visible_x = 0;
12079 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12080 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12081 it.paragraph_embedding = L2R;
12082
12083 while (!ITERATOR_AT_END_P (&it))
12084 {
12085 clear_glyph_row (temp_row);
12086 it.glyph_row = temp_row;
12087 display_tool_bar_line (&it, -1);
12088 }
12089 clear_glyph_row (temp_row);
12090
12091 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12092 if (n_rows)
12093 *n_rows = it.vpos > 0 ? it.vpos : -1;
12094
12095 if (pixelwise)
12096 return it.current_y;
12097 else
12098 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12099 }
12100
12101 #endif /* !USE_GTK && !HAVE_NS */
12102
12103 #if defined USE_GTK || defined HAVE_NS
12104 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12105 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12106 #endif
12107
12108 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12109 0, 2, 0,
12110 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12111 If FRAME is nil or omitted, use the selected frame. Optional argument
12112 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12113 (Lisp_Object frame, Lisp_Object pixelwise)
12114 {
12115 int height = 0;
12116
12117 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12118 struct frame *f = decode_any_frame (frame);
12119
12120 if (WINDOWP (f->tool_bar_window)
12121 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12122 {
12123 update_tool_bar (f, 1);
12124 if (f->n_tool_bar_items)
12125 {
12126 build_desired_tool_bar_string (f);
12127 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12128 }
12129 }
12130 #endif
12131
12132 return make_number (height);
12133 }
12134
12135
12136 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12137 height should be changed. */
12138
12139 static int
12140 redisplay_tool_bar (struct frame *f)
12141 {
12142 #if defined (USE_GTK) || defined (HAVE_NS)
12143
12144 if (FRAME_EXTERNAL_TOOL_BAR (f))
12145 update_frame_tool_bar (f);
12146 return 0;
12147
12148 #else /* !USE_GTK && !HAVE_NS */
12149
12150 struct window *w;
12151 struct it it;
12152 struct glyph_row *row;
12153
12154 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12155 do anything. This means you must start with tool-bar-lines
12156 non-zero to get the auto-sizing effect. Or in other words, you
12157 can turn off tool-bars by specifying tool-bar-lines zero. */
12158 if (!WINDOWP (f->tool_bar_window)
12159 || (w = XWINDOW (f->tool_bar_window),
12160 WINDOW_PIXEL_HEIGHT (w) == 0))
12161 return 0;
12162
12163 /* Set up an iterator for the tool-bar window. */
12164 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12165 it.first_visible_x = 0;
12166 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12167 row = it.glyph_row;
12168
12169 /* Build a string that represents the contents of the tool-bar. */
12170 build_desired_tool_bar_string (f);
12171 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12172 /* FIXME: This should be controlled by a user option. But it
12173 doesn't make sense to have an R2L tool bar if the menu bar cannot
12174 be drawn also R2L, and making the menu bar R2L is tricky due
12175 toolkit-specific code that implements it. If an R2L tool bar is
12176 ever supported, display_tool_bar_line should also be augmented to
12177 call unproduce_glyphs like display_line and display_string
12178 do. */
12179 it.paragraph_embedding = L2R;
12180
12181 if (f->n_tool_bar_rows == 0)
12182 {
12183 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12184
12185 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12186 {
12187 Lisp_Object frame;
12188 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12189 / FRAME_LINE_HEIGHT (f));
12190
12191 XSETFRAME (frame, f);
12192 Fmodify_frame_parameters (frame,
12193 list1 (Fcons (Qtool_bar_lines,
12194 make_number (new_lines))));
12195 /* Always do that now. */
12196 clear_glyph_matrix (w->desired_matrix);
12197 f->fonts_changed = 1;
12198 return 1;
12199 }
12200 }
12201
12202 /* Display as many lines as needed to display all tool-bar items. */
12203
12204 if (f->n_tool_bar_rows > 0)
12205 {
12206 int border, rows, height, extra;
12207
12208 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12209 border = XINT (Vtool_bar_border);
12210 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12211 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12212 else if (EQ (Vtool_bar_border, Qborder_width))
12213 border = f->border_width;
12214 else
12215 border = 0;
12216 if (border < 0)
12217 border = 0;
12218
12219 rows = f->n_tool_bar_rows;
12220 height = max (1, (it.last_visible_y - border) / rows);
12221 extra = it.last_visible_y - border - height * rows;
12222
12223 while (it.current_y < it.last_visible_y)
12224 {
12225 int h = 0;
12226 if (extra > 0 && rows-- > 0)
12227 {
12228 h = (extra + rows - 1) / rows;
12229 extra -= h;
12230 }
12231 display_tool_bar_line (&it, height + h);
12232 }
12233 }
12234 else
12235 {
12236 while (it.current_y < it.last_visible_y)
12237 display_tool_bar_line (&it, 0);
12238 }
12239
12240 /* It doesn't make much sense to try scrolling in the tool-bar
12241 window, so don't do it. */
12242 w->desired_matrix->no_scrolling_p = 1;
12243 w->must_be_updated_p = 1;
12244
12245 if (!NILP (Vauto_resize_tool_bars))
12246 {
12247 /* Do we really allow the toolbar to occupy the whole frame? */
12248 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12249 int change_height_p = 0;
12250
12251 /* If we couldn't display everything, change the tool-bar's
12252 height if there is room for more. */
12253 if (IT_STRING_CHARPOS (it) < it.end_charpos
12254 && it.current_y < max_tool_bar_height)
12255 change_height_p = 1;
12256
12257 /* We subtract 1 because display_tool_bar_line advances the
12258 glyph_row pointer before returning to its caller. We want to
12259 examine the last glyph row produced by
12260 display_tool_bar_line. */
12261 row = it.glyph_row - 1;
12262
12263 /* If there are blank lines at the end, except for a partially
12264 visible blank line at the end that is smaller than
12265 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12266 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12267 && row->height >= FRAME_LINE_HEIGHT (f))
12268 change_height_p = 1;
12269
12270 /* If row displays tool-bar items, but is partially visible,
12271 change the tool-bar's height. */
12272 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12273 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12274 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12275 change_height_p = 1;
12276
12277 /* Resize windows as needed by changing the `tool-bar-lines'
12278 frame parameter. */
12279 if (change_height_p)
12280 {
12281 Lisp_Object frame;
12282 int nrows;
12283 int new_height = tool_bar_height (f, &nrows, 1);
12284
12285 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12286 && !f->minimize_tool_bar_window_p)
12287 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12288 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12289 f->minimize_tool_bar_window_p = 0;
12290
12291 if (change_height_p)
12292 {
12293 /* Current size of the tool-bar window in canonical line
12294 units. */
12295 int old_lines = WINDOW_TOTAL_LINES (w);
12296 /* Required size of the tool-bar window in canonical
12297 line units. */
12298 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12299 / FRAME_LINE_HEIGHT (f));
12300 /* Maximum size of the tool-bar window in canonical line
12301 units that this frame can allow. */
12302 int max_lines =
12303 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12304
12305 /* Don't try to change the tool-bar window size and set
12306 the fonts_changed flag unless really necessary. That
12307 flag causes redisplay to give up and retry
12308 redisplaying the frame from scratch, so setting it
12309 unnecessarily can lead to nasty redisplay loops. */
12310 if (new_lines <= max_lines
12311 && eabs (new_lines - old_lines) >= 1)
12312 {
12313 XSETFRAME (frame, f);
12314 Fmodify_frame_parameters (frame,
12315 list1 (Fcons (Qtool_bar_lines,
12316 make_number (new_lines))));
12317 clear_glyph_matrix (w->desired_matrix);
12318 f->n_tool_bar_rows = nrows;
12319 f->fonts_changed = 1;
12320 return 1;
12321 }
12322 }
12323 }
12324 }
12325
12326 f->minimize_tool_bar_window_p = 0;
12327 return 0;
12328
12329 #endif /* USE_GTK || HAVE_NS */
12330 }
12331
12332 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12333
12334 /* Get information about the tool-bar item which is displayed in GLYPH
12335 on frame F. Return in *PROP_IDX the index where tool-bar item
12336 properties start in F->tool_bar_items. Value is zero if
12337 GLYPH doesn't display a tool-bar item. */
12338
12339 static int
12340 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12341 {
12342 Lisp_Object prop;
12343 int success_p;
12344 int charpos;
12345
12346 /* This function can be called asynchronously, which means we must
12347 exclude any possibility that Fget_text_property signals an
12348 error. */
12349 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12350 charpos = max (0, charpos);
12351
12352 /* Get the text property `menu-item' at pos. The value of that
12353 property is the start index of this item's properties in
12354 F->tool_bar_items. */
12355 prop = Fget_text_property (make_number (charpos),
12356 Qmenu_item, f->current_tool_bar_string);
12357 if (INTEGERP (prop))
12358 {
12359 *prop_idx = XINT (prop);
12360 success_p = 1;
12361 }
12362 else
12363 success_p = 0;
12364
12365 return success_p;
12366 }
12367
12368 \f
12369 /* Get information about the tool-bar item at position X/Y on frame F.
12370 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12371 the current matrix of the tool-bar window of F, or NULL if not
12372 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12373 item in F->tool_bar_items. Value is
12374
12375 -1 if X/Y is not on a tool-bar item
12376 0 if X/Y is on the same item that was highlighted before.
12377 1 otherwise. */
12378
12379 static int
12380 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12381 int *hpos, int *vpos, int *prop_idx)
12382 {
12383 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12384 struct window *w = XWINDOW (f->tool_bar_window);
12385 int area;
12386
12387 /* Find the glyph under X/Y. */
12388 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12389 if (*glyph == NULL)
12390 return -1;
12391
12392 /* Get the start of this tool-bar item's properties in
12393 f->tool_bar_items. */
12394 if (!tool_bar_item_info (f, *glyph, prop_idx))
12395 return -1;
12396
12397 /* Is mouse on the highlighted item? */
12398 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12399 && *vpos >= hlinfo->mouse_face_beg_row
12400 && *vpos <= hlinfo->mouse_face_end_row
12401 && (*vpos > hlinfo->mouse_face_beg_row
12402 || *hpos >= hlinfo->mouse_face_beg_col)
12403 && (*vpos < hlinfo->mouse_face_end_row
12404 || *hpos < hlinfo->mouse_face_end_col
12405 || hlinfo->mouse_face_past_end))
12406 return 0;
12407
12408 return 1;
12409 }
12410
12411
12412 /* EXPORT:
12413 Handle mouse button event on the tool-bar of frame F, at
12414 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12415 0 for button release. MODIFIERS is event modifiers for button
12416 release. */
12417
12418 void
12419 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12420 int modifiers)
12421 {
12422 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12423 struct window *w = XWINDOW (f->tool_bar_window);
12424 int hpos, vpos, prop_idx;
12425 struct glyph *glyph;
12426 Lisp_Object enabled_p;
12427 int ts;
12428
12429 /* If not on the highlighted tool-bar item, and mouse-highlight is
12430 non-nil, return. This is so we generate the tool-bar button
12431 click only when the mouse button is released on the same item as
12432 where it was pressed. However, when mouse-highlight is disabled,
12433 generate the click when the button is released regardless of the
12434 highlight, since tool-bar items are not highlighted in that
12435 case. */
12436 frame_to_window_pixel_xy (w, &x, &y);
12437 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12438 if (ts == -1
12439 || (ts != 0 && !NILP (Vmouse_highlight)))
12440 return;
12441
12442 /* When mouse-highlight is off, generate the click for the item
12443 where the button was pressed, disregarding where it was
12444 released. */
12445 if (NILP (Vmouse_highlight) && !down_p)
12446 prop_idx = last_tool_bar_item;
12447
12448 /* If item is disabled, do nothing. */
12449 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12450 if (NILP (enabled_p))
12451 return;
12452
12453 if (down_p)
12454 {
12455 /* Show item in pressed state. */
12456 if (!NILP (Vmouse_highlight))
12457 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12458 last_tool_bar_item = prop_idx;
12459 }
12460 else
12461 {
12462 Lisp_Object key, frame;
12463 struct input_event event;
12464 EVENT_INIT (event);
12465
12466 /* Show item in released state. */
12467 if (!NILP (Vmouse_highlight))
12468 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12469
12470 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12471
12472 XSETFRAME (frame, f);
12473 event.kind = TOOL_BAR_EVENT;
12474 event.frame_or_window = frame;
12475 event.arg = frame;
12476 kbd_buffer_store_event (&event);
12477
12478 event.kind = TOOL_BAR_EVENT;
12479 event.frame_or_window = frame;
12480 event.arg = key;
12481 event.modifiers = modifiers;
12482 kbd_buffer_store_event (&event);
12483 last_tool_bar_item = -1;
12484 }
12485 }
12486
12487
12488 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12489 tool-bar window-relative coordinates X/Y. Called from
12490 note_mouse_highlight. */
12491
12492 static void
12493 note_tool_bar_highlight (struct frame *f, int x, int y)
12494 {
12495 Lisp_Object window = f->tool_bar_window;
12496 struct window *w = XWINDOW (window);
12497 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12498 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12499 int hpos, vpos;
12500 struct glyph *glyph;
12501 struct glyph_row *row;
12502 int i;
12503 Lisp_Object enabled_p;
12504 int prop_idx;
12505 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12506 int mouse_down_p, rc;
12507
12508 /* Function note_mouse_highlight is called with negative X/Y
12509 values when mouse moves outside of the frame. */
12510 if (x <= 0 || y <= 0)
12511 {
12512 clear_mouse_face (hlinfo);
12513 return;
12514 }
12515
12516 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12517 if (rc < 0)
12518 {
12519 /* Not on tool-bar item. */
12520 clear_mouse_face (hlinfo);
12521 return;
12522 }
12523 else if (rc == 0)
12524 /* On same tool-bar item as before. */
12525 goto set_help_echo;
12526
12527 clear_mouse_face (hlinfo);
12528
12529 /* Mouse is down, but on different tool-bar item? */
12530 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12531 && f == dpyinfo->last_mouse_frame);
12532
12533 if (mouse_down_p
12534 && last_tool_bar_item != prop_idx)
12535 return;
12536
12537 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12538
12539 /* If tool-bar item is not enabled, don't highlight it. */
12540 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12541 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12542 {
12543 /* Compute the x-position of the glyph. In front and past the
12544 image is a space. We include this in the highlighted area. */
12545 row = MATRIX_ROW (w->current_matrix, vpos);
12546 for (i = x = 0; i < hpos; ++i)
12547 x += row->glyphs[TEXT_AREA][i].pixel_width;
12548
12549 /* Record this as the current active region. */
12550 hlinfo->mouse_face_beg_col = hpos;
12551 hlinfo->mouse_face_beg_row = vpos;
12552 hlinfo->mouse_face_beg_x = x;
12553 hlinfo->mouse_face_past_end = 0;
12554
12555 hlinfo->mouse_face_end_col = hpos + 1;
12556 hlinfo->mouse_face_end_row = vpos;
12557 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12558 hlinfo->mouse_face_window = window;
12559 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12560
12561 /* Display it as active. */
12562 show_mouse_face (hlinfo, draw);
12563 }
12564
12565 set_help_echo:
12566
12567 /* Set help_echo_string to a help string to display for this tool-bar item.
12568 XTread_socket does the rest. */
12569 help_echo_object = help_echo_window = Qnil;
12570 help_echo_pos = -1;
12571 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12572 if (NILP (help_echo_string))
12573 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12574 }
12575
12576 #endif /* !USE_GTK && !HAVE_NS */
12577
12578 #endif /* HAVE_WINDOW_SYSTEM */
12579
12580
12581 \f
12582 /************************************************************************
12583 Horizontal scrolling
12584 ************************************************************************/
12585
12586 static int hscroll_window_tree (Lisp_Object);
12587 static int hscroll_windows (Lisp_Object);
12588
12589 /* For all leaf windows in the window tree rooted at WINDOW, set their
12590 hscroll value so that PT is (i) visible in the window, and (ii) so
12591 that it is not within a certain margin at the window's left and
12592 right border. Value is non-zero if any window's hscroll has been
12593 changed. */
12594
12595 static int
12596 hscroll_window_tree (Lisp_Object window)
12597 {
12598 int hscrolled_p = 0;
12599 int hscroll_relative_p = FLOATP (Vhscroll_step);
12600 int hscroll_step_abs = 0;
12601 double hscroll_step_rel = 0;
12602
12603 if (hscroll_relative_p)
12604 {
12605 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12606 if (hscroll_step_rel < 0)
12607 {
12608 hscroll_relative_p = 0;
12609 hscroll_step_abs = 0;
12610 }
12611 }
12612 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12613 {
12614 hscroll_step_abs = XINT (Vhscroll_step);
12615 if (hscroll_step_abs < 0)
12616 hscroll_step_abs = 0;
12617 }
12618 else
12619 hscroll_step_abs = 0;
12620
12621 while (WINDOWP (window))
12622 {
12623 struct window *w = XWINDOW (window);
12624
12625 if (WINDOWP (w->contents))
12626 hscrolled_p |= hscroll_window_tree (w->contents);
12627 else if (w->cursor.vpos >= 0)
12628 {
12629 int h_margin;
12630 int text_area_width;
12631 struct glyph_row *cursor_row;
12632 struct glyph_row *bottom_row;
12633 int row_r2l_p;
12634
12635 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12636 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12637 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12638 else
12639 cursor_row = bottom_row - 1;
12640
12641 if (!cursor_row->enabled_p)
12642 {
12643 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12644 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12645 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12646 else
12647 cursor_row = bottom_row - 1;
12648 }
12649 row_r2l_p = cursor_row->reversed_p;
12650
12651 text_area_width = window_box_width (w, TEXT_AREA);
12652
12653 /* Scroll when cursor is inside this scroll margin. */
12654 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12655
12656 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12657 /* For left-to-right rows, hscroll when cursor is either
12658 (i) inside the right hscroll margin, or (ii) if it is
12659 inside the left margin and the window is already
12660 hscrolled. */
12661 && ((!row_r2l_p
12662 && ((w->hscroll
12663 && w->cursor.x <= h_margin)
12664 || (cursor_row->enabled_p
12665 && cursor_row->truncated_on_right_p
12666 && (w->cursor.x >= text_area_width - h_margin))))
12667 /* For right-to-left rows, the logic is similar,
12668 except that rules for scrolling to left and right
12669 are reversed. E.g., if cursor.x <= h_margin, we
12670 need to hscroll "to the right" unconditionally,
12671 and that will scroll the screen to the left so as
12672 to reveal the next portion of the row. */
12673 || (row_r2l_p
12674 && ((cursor_row->enabled_p
12675 /* FIXME: It is confusing to set the
12676 truncated_on_right_p flag when R2L rows
12677 are actually truncated on the left. */
12678 && cursor_row->truncated_on_right_p
12679 && w->cursor.x <= h_margin)
12680 || (w->hscroll
12681 && (w->cursor.x >= text_area_width - h_margin))))))
12682 {
12683 struct it it;
12684 ptrdiff_t hscroll;
12685 struct buffer *saved_current_buffer;
12686 ptrdiff_t pt;
12687 int wanted_x;
12688
12689 /* Find point in a display of infinite width. */
12690 saved_current_buffer = current_buffer;
12691 current_buffer = XBUFFER (w->contents);
12692
12693 if (w == XWINDOW (selected_window))
12694 pt = PT;
12695 else
12696 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12697
12698 /* Move iterator to pt starting at cursor_row->start in
12699 a line with infinite width. */
12700 init_to_row_start (&it, w, cursor_row);
12701 it.last_visible_x = INFINITY;
12702 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12703 current_buffer = saved_current_buffer;
12704
12705 /* Position cursor in window. */
12706 if (!hscroll_relative_p && hscroll_step_abs == 0)
12707 hscroll = max (0, (it.current_x
12708 - (ITERATOR_AT_END_OF_LINE_P (&it)
12709 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12710 : (text_area_width / 2))))
12711 / FRAME_COLUMN_WIDTH (it.f);
12712 else if ((!row_r2l_p
12713 && w->cursor.x >= text_area_width - h_margin)
12714 || (row_r2l_p && w->cursor.x <= h_margin))
12715 {
12716 if (hscroll_relative_p)
12717 wanted_x = text_area_width * (1 - hscroll_step_rel)
12718 - h_margin;
12719 else
12720 wanted_x = text_area_width
12721 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12722 - h_margin;
12723 hscroll
12724 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12725 }
12726 else
12727 {
12728 if (hscroll_relative_p)
12729 wanted_x = text_area_width * hscroll_step_rel
12730 + h_margin;
12731 else
12732 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12733 + h_margin;
12734 hscroll
12735 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12736 }
12737 hscroll = max (hscroll, w->min_hscroll);
12738
12739 /* Don't prevent redisplay optimizations if hscroll
12740 hasn't changed, as it will unnecessarily slow down
12741 redisplay. */
12742 if (w->hscroll != hscroll)
12743 {
12744 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12745 w->hscroll = hscroll;
12746 hscrolled_p = 1;
12747 }
12748 }
12749 }
12750
12751 window = w->next;
12752 }
12753
12754 /* Value is non-zero if hscroll of any leaf window has been changed. */
12755 return hscrolled_p;
12756 }
12757
12758
12759 /* Set hscroll so that cursor is visible and not inside horizontal
12760 scroll margins for all windows in the tree rooted at WINDOW. See
12761 also hscroll_window_tree above. Value is non-zero if any window's
12762 hscroll has been changed. If it has, desired matrices on the frame
12763 of WINDOW are cleared. */
12764
12765 static int
12766 hscroll_windows (Lisp_Object window)
12767 {
12768 int hscrolled_p = hscroll_window_tree (window);
12769 if (hscrolled_p)
12770 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12771 return hscrolled_p;
12772 }
12773
12774
12775 \f
12776 /************************************************************************
12777 Redisplay
12778 ************************************************************************/
12779
12780 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12781 to a non-zero value. This is sometimes handy to have in a debugger
12782 session. */
12783
12784 #ifdef GLYPH_DEBUG
12785
12786 /* First and last unchanged row for try_window_id. */
12787
12788 static int debug_first_unchanged_at_end_vpos;
12789 static int debug_last_unchanged_at_beg_vpos;
12790
12791 /* Delta vpos and y. */
12792
12793 static int debug_dvpos, debug_dy;
12794
12795 /* Delta in characters and bytes for try_window_id. */
12796
12797 static ptrdiff_t debug_delta, debug_delta_bytes;
12798
12799 /* Values of window_end_pos and window_end_vpos at the end of
12800 try_window_id. */
12801
12802 static ptrdiff_t debug_end_vpos;
12803
12804 /* Append a string to W->desired_matrix->method. FMT is a printf
12805 format string. If trace_redisplay_p is true also printf the
12806 resulting string to stderr. */
12807
12808 static void debug_method_add (struct window *, char const *, ...)
12809 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12810
12811 static void
12812 debug_method_add (struct window *w, char const *fmt, ...)
12813 {
12814 void *ptr = w;
12815 char *method = w->desired_matrix->method;
12816 int len = strlen (method);
12817 int size = sizeof w->desired_matrix->method;
12818 int remaining = size - len - 1;
12819 va_list ap;
12820
12821 if (len && remaining)
12822 {
12823 method[len] = '|';
12824 --remaining, ++len;
12825 }
12826
12827 va_start (ap, fmt);
12828 vsnprintf (method + len, remaining + 1, fmt, ap);
12829 va_end (ap);
12830
12831 if (trace_redisplay_p)
12832 fprintf (stderr, "%p (%s): %s\n",
12833 ptr,
12834 ((BUFFERP (w->contents)
12835 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12836 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12837 : "no buffer"),
12838 method + len);
12839 }
12840
12841 #endif /* GLYPH_DEBUG */
12842
12843
12844 /* Value is non-zero if all changes in window W, which displays
12845 current_buffer, are in the text between START and END. START is a
12846 buffer position, END is given as a distance from Z. Used in
12847 redisplay_internal for display optimization. */
12848
12849 static int
12850 text_outside_line_unchanged_p (struct window *w,
12851 ptrdiff_t start, ptrdiff_t end)
12852 {
12853 int unchanged_p = 1;
12854
12855 /* If text or overlays have changed, see where. */
12856 if (window_outdated (w))
12857 {
12858 /* Gap in the line? */
12859 if (GPT < start || Z - GPT < end)
12860 unchanged_p = 0;
12861
12862 /* Changes start in front of the line, or end after it? */
12863 if (unchanged_p
12864 && (BEG_UNCHANGED < start - 1
12865 || END_UNCHANGED < end))
12866 unchanged_p = 0;
12867
12868 /* If selective display, can't optimize if changes start at the
12869 beginning of the line. */
12870 if (unchanged_p
12871 && INTEGERP (BVAR (current_buffer, selective_display))
12872 && XINT (BVAR (current_buffer, selective_display)) > 0
12873 && (BEG_UNCHANGED < start || GPT <= start))
12874 unchanged_p = 0;
12875
12876 /* If there are overlays at the start or end of the line, these
12877 may have overlay strings with newlines in them. A change at
12878 START, for instance, may actually concern the display of such
12879 overlay strings as well, and they are displayed on different
12880 lines. So, quickly rule out this case. (For the future, it
12881 might be desirable to implement something more telling than
12882 just BEG/END_UNCHANGED.) */
12883 if (unchanged_p)
12884 {
12885 if (BEG + BEG_UNCHANGED == start
12886 && overlay_touches_p (start))
12887 unchanged_p = 0;
12888 if (END_UNCHANGED == end
12889 && overlay_touches_p (Z - end))
12890 unchanged_p = 0;
12891 }
12892
12893 /* Under bidi reordering, adding or deleting a character in the
12894 beginning of a paragraph, before the first strong directional
12895 character, can change the base direction of the paragraph (unless
12896 the buffer specifies a fixed paragraph direction), which will
12897 require to redisplay the whole paragraph. It might be worthwhile
12898 to find the paragraph limits and widen the range of redisplayed
12899 lines to that, but for now just give up this optimization. */
12900 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12901 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12902 unchanged_p = 0;
12903 }
12904
12905 return unchanged_p;
12906 }
12907
12908
12909 /* Do a frame update, taking possible shortcuts into account. This is
12910 the main external entry point for redisplay.
12911
12912 If the last redisplay displayed an echo area message and that message
12913 is no longer requested, we clear the echo area or bring back the
12914 mini-buffer if that is in use. */
12915
12916 void
12917 redisplay (void)
12918 {
12919 redisplay_internal ();
12920 }
12921
12922
12923 static Lisp_Object
12924 overlay_arrow_string_or_property (Lisp_Object var)
12925 {
12926 Lisp_Object val;
12927
12928 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12929 return val;
12930
12931 return Voverlay_arrow_string;
12932 }
12933
12934 /* Return 1 if there are any overlay-arrows in current_buffer. */
12935 static int
12936 overlay_arrow_in_current_buffer_p (void)
12937 {
12938 Lisp_Object vlist;
12939
12940 for (vlist = Voverlay_arrow_variable_list;
12941 CONSP (vlist);
12942 vlist = XCDR (vlist))
12943 {
12944 Lisp_Object var = XCAR (vlist);
12945 Lisp_Object val;
12946
12947 if (!SYMBOLP (var))
12948 continue;
12949 val = find_symbol_value (var);
12950 if (MARKERP (val)
12951 && current_buffer == XMARKER (val)->buffer)
12952 return 1;
12953 }
12954 return 0;
12955 }
12956
12957
12958 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12959 has changed. */
12960
12961 static int
12962 overlay_arrows_changed_p (void)
12963 {
12964 Lisp_Object vlist;
12965
12966 for (vlist = Voverlay_arrow_variable_list;
12967 CONSP (vlist);
12968 vlist = XCDR (vlist))
12969 {
12970 Lisp_Object var = XCAR (vlist);
12971 Lisp_Object val, pstr;
12972
12973 if (!SYMBOLP (var))
12974 continue;
12975 val = find_symbol_value (var);
12976 if (!MARKERP (val))
12977 continue;
12978 if (! EQ (COERCE_MARKER (val),
12979 Fget (var, Qlast_arrow_position))
12980 || ! (pstr = overlay_arrow_string_or_property (var),
12981 EQ (pstr, Fget (var, Qlast_arrow_string))))
12982 return 1;
12983 }
12984 return 0;
12985 }
12986
12987 /* Mark overlay arrows to be updated on next redisplay. */
12988
12989 static void
12990 update_overlay_arrows (int up_to_date)
12991 {
12992 Lisp_Object vlist;
12993
12994 for (vlist = Voverlay_arrow_variable_list;
12995 CONSP (vlist);
12996 vlist = XCDR (vlist))
12997 {
12998 Lisp_Object var = XCAR (vlist);
12999
13000 if (!SYMBOLP (var))
13001 continue;
13002
13003 if (up_to_date > 0)
13004 {
13005 Lisp_Object val = find_symbol_value (var);
13006 Fput (var, Qlast_arrow_position,
13007 COERCE_MARKER (val));
13008 Fput (var, Qlast_arrow_string,
13009 overlay_arrow_string_or_property (var));
13010 }
13011 else if (up_to_date < 0
13012 || !NILP (Fget (var, Qlast_arrow_position)))
13013 {
13014 Fput (var, Qlast_arrow_position, Qt);
13015 Fput (var, Qlast_arrow_string, Qt);
13016 }
13017 }
13018 }
13019
13020
13021 /* Return overlay arrow string to display at row.
13022 Return integer (bitmap number) for arrow bitmap in left fringe.
13023 Return nil if no overlay arrow. */
13024
13025 static Lisp_Object
13026 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13027 {
13028 Lisp_Object vlist;
13029
13030 for (vlist = Voverlay_arrow_variable_list;
13031 CONSP (vlist);
13032 vlist = XCDR (vlist))
13033 {
13034 Lisp_Object var = XCAR (vlist);
13035 Lisp_Object val;
13036
13037 if (!SYMBOLP (var))
13038 continue;
13039
13040 val = find_symbol_value (var);
13041
13042 if (MARKERP (val)
13043 && current_buffer == XMARKER (val)->buffer
13044 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13045 {
13046 if (FRAME_WINDOW_P (it->f)
13047 /* FIXME: if ROW->reversed_p is set, this should test
13048 the right fringe, not the left one. */
13049 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13050 {
13051 #ifdef HAVE_WINDOW_SYSTEM
13052 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13053 {
13054 int fringe_bitmap;
13055 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13056 return make_number (fringe_bitmap);
13057 }
13058 #endif
13059 return make_number (-1); /* Use default arrow bitmap. */
13060 }
13061 return overlay_arrow_string_or_property (var);
13062 }
13063 }
13064
13065 return Qnil;
13066 }
13067
13068 /* Return 1 if point moved out of or into a composition. Otherwise
13069 return 0. PREV_BUF and PREV_PT are the last point buffer and
13070 position. BUF and PT are the current point buffer and position. */
13071
13072 static int
13073 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13074 struct buffer *buf, ptrdiff_t pt)
13075 {
13076 ptrdiff_t start, end;
13077 Lisp_Object prop;
13078 Lisp_Object buffer;
13079
13080 XSETBUFFER (buffer, buf);
13081 /* Check a composition at the last point if point moved within the
13082 same buffer. */
13083 if (prev_buf == buf)
13084 {
13085 if (prev_pt == pt)
13086 /* Point didn't move. */
13087 return 0;
13088
13089 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13090 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13091 && composition_valid_p (start, end, prop)
13092 && start < prev_pt && end > prev_pt)
13093 /* The last point was within the composition. Return 1 iff
13094 point moved out of the composition. */
13095 return (pt <= start || pt >= end);
13096 }
13097
13098 /* Check a composition at the current point. */
13099 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13100 && find_composition (pt, -1, &start, &end, &prop, buffer)
13101 && composition_valid_p (start, end, prop)
13102 && start < pt && end > pt);
13103 }
13104
13105 /* Reconsider the clip changes of buffer which is displayed in W. */
13106
13107 static void
13108 reconsider_clip_changes (struct window *w)
13109 {
13110 struct buffer *b = XBUFFER (w->contents);
13111
13112 if (b->clip_changed
13113 && w->window_end_valid
13114 && w->current_matrix->buffer == b
13115 && w->current_matrix->zv == BUF_ZV (b)
13116 && w->current_matrix->begv == BUF_BEGV (b))
13117 b->clip_changed = 0;
13118
13119 /* If display wasn't paused, and W is not a tool bar window, see if
13120 point has been moved into or out of a composition. In that case,
13121 we set b->clip_changed to 1 to force updating the screen. If
13122 b->clip_changed has already been set to 1, we can skip this
13123 check. */
13124 if (!b->clip_changed && w->window_end_valid)
13125 {
13126 ptrdiff_t pt = (w == XWINDOW (selected_window)
13127 ? PT : marker_position (w->pointm));
13128
13129 if ((w->current_matrix->buffer != b || pt != w->last_point)
13130 && check_point_in_composition (w->current_matrix->buffer,
13131 w->last_point, b, pt))
13132 b->clip_changed = 1;
13133 }
13134 }
13135
13136 static void
13137 propagate_buffer_redisplay (void)
13138 { /* Resetting b->text->redisplay is problematic!
13139 We can't just reset it in the case that some window that displays
13140 it has not been redisplayed; and such a window can stay
13141 unredisplayed for a long time if it's currently invisible.
13142 But we do want to reset it at the end of redisplay otherwise
13143 its displayed windows will keep being redisplayed over and over
13144 again.
13145 So we copy all b->text->redisplay flags up to their windows here,
13146 such that mark_window_display_accurate can safely reset
13147 b->text->redisplay. */
13148 Lisp_Object ws = window_list ();
13149 for (; CONSP (ws); ws = XCDR (ws))
13150 {
13151 struct window *thisw = XWINDOW (XCAR (ws));
13152 struct buffer *thisb = XBUFFER (thisw->contents);
13153 if (thisb->text->redisplay)
13154 thisw->redisplay = true;
13155 }
13156 }
13157
13158 #define STOP_POLLING \
13159 do { if (! polling_stopped_here) stop_polling (); \
13160 polling_stopped_here = 1; } while (0)
13161
13162 #define RESUME_POLLING \
13163 do { if (polling_stopped_here) start_polling (); \
13164 polling_stopped_here = 0; } while (0)
13165
13166
13167 /* Perhaps in the future avoid recentering windows if it
13168 is not necessary; currently that causes some problems. */
13169
13170 static void
13171 redisplay_internal (void)
13172 {
13173 struct window *w = XWINDOW (selected_window);
13174 struct window *sw;
13175 struct frame *fr;
13176 int pending;
13177 bool must_finish = 0, match_p;
13178 struct text_pos tlbufpos, tlendpos;
13179 int number_of_visible_frames;
13180 ptrdiff_t count;
13181 struct frame *sf;
13182 int polling_stopped_here = 0;
13183 Lisp_Object tail, frame;
13184
13185 /* True means redisplay has to consider all windows on all
13186 frames. False, only selected_window is considered. */
13187 bool consider_all_windows_p;
13188
13189 /* True means redisplay has to redisplay the miniwindow. */
13190 bool update_miniwindow_p = false;
13191
13192 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13193
13194 /* No redisplay if running in batch mode or frame is not yet fully
13195 initialized, or redisplay is explicitly turned off by setting
13196 Vinhibit_redisplay. */
13197 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13198 || !NILP (Vinhibit_redisplay))
13199 return;
13200
13201 /* Don't examine these until after testing Vinhibit_redisplay.
13202 When Emacs is shutting down, perhaps because its connection to
13203 X has dropped, we should not look at them at all. */
13204 fr = XFRAME (w->frame);
13205 sf = SELECTED_FRAME ();
13206
13207 if (!fr->glyphs_initialized_p)
13208 return;
13209
13210 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13211 if (popup_activated ())
13212 return;
13213 #endif
13214
13215 /* I don't think this happens but let's be paranoid. */
13216 if (redisplaying_p)
13217 return;
13218
13219 /* Record a function that clears redisplaying_p
13220 when we leave this function. */
13221 count = SPECPDL_INDEX ();
13222 record_unwind_protect_void (unwind_redisplay);
13223 redisplaying_p = 1;
13224 specbind (Qinhibit_free_realized_faces, Qnil);
13225
13226 /* Record this function, so it appears on the profiler's backtraces. */
13227 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13228
13229 FOR_EACH_FRAME (tail, frame)
13230 XFRAME (frame)->already_hscrolled_p = 0;
13231
13232 retry:
13233 /* Remember the currently selected window. */
13234 sw = w;
13235
13236 pending = 0;
13237 last_escape_glyph_frame = NULL;
13238 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13239 last_glyphless_glyph_frame = NULL;
13240 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13241
13242 /* If face_change_count is non-zero, init_iterator will free all
13243 realized faces, which includes the faces referenced from current
13244 matrices. So, we can't reuse current matrices in this case. */
13245 if (face_change_count)
13246 windows_or_buffers_changed = 47;
13247
13248 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13249 && FRAME_TTY (sf)->previous_frame != sf)
13250 {
13251 /* Since frames on a single ASCII terminal share the same
13252 display area, displaying a different frame means redisplay
13253 the whole thing. */
13254 SET_FRAME_GARBAGED (sf);
13255 #ifndef DOS_NT
13256 set_tty_color_mode (FRAME_TTY (sf), sf);
13257 #endif
13258 FRAME_TTY (sf)->previous_frame = sf;
13259 }
13260
13261 /* Set the visible flags for all frames. Do this before checking for
13262 resized or garbaged frames; they want to know if their frames are
13263 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13264 number_of_visible_frames = 0;
13265
13266 FOR_EACH_FRAME (tail, frame)
13267 {
13268 struct frame *f = XFRAME (frame);
13269
13270 if (FRAME_VISIBLE_P (f))
13271 {
13272 ++number_of_visible_frames;
13273 /* Adjust matrices for visible frames only. */
13274 if (f->fonts_changed)
13275 {
13276 adjust_frame_glyphs (f);
13277 f->fonts_changed = 0;
13278 }
13279 /* If cursor type has been changed on the frame
13280 other than selected, consider all frames. */
13281 if (f != sf && f->cursor_type_changed)
13282 update_mode_lines = 31;
13283 }
13284 clear_desired_matrices (f);
13285 }
13286
13287 /* Notice any pending interrupt request to change frame size. */
13288 do_pending_window_change (1);
13289
13290 /* do_pending_window_change could change the selected_window due to
13291 frame resizing which makes the selected window too small. */
13292 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13293 sw = w;
13294
13295 /* Clear frames marked as garbaged. */
13296 clear_garbaged_frames ();
13297
13298 /* Build menubar and tool-bar items. */
13299 if (NILP (Vmemory_full))
13300 prepare_menu_bars ();
13301
13302 reconsider_clip_changes (w);
13303
13304 /* In most cases selected window displays current buffer. */
13305 match_p = XBUFFER (w->contents) == current_buffer;
13306 if (match_p)
13307 {
13308 /* Detect case that we need to write or remove a star in the mode line. */
13309 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13310 w->update_mode_line = 1;
13311
13312 if (mode_line_update_needed (w))
13313 w->update_mode_line = 1;
13314 }
13315
13316 /* Normally the message* functions will have already displayed and
13317 updated the echo area, but the frame may have been trashed, or
13318 the update may have been preempted, so display the echo area
13319 again here. Checking message_cleared_p captures the case that
13320 the echo area should be cleared. */
13321 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13322 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13323 || (message_cleared_p
13324 && minibuf_level == 0
13325 /* If the mini-window is currently selected, this means the
13326 echo-area doesn't show through. */
13327 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13328 {
13329 int window_height_changed_p = echo_area_display (0);
13330
13331 if (message_cleared_p)
13332 update_miniwindow_p = true;
13333
13334 must_finish = 1;
13335
13336 /* If we don't display the current message, don't clear the
13337 message_cleared_p flag, because, if we did, we wouldn't clear
13338 the echo area in the next redisplay which doesn't preserve
13339 the echo area. */
13340 if (!display_last_displayed_message_p)
13341 message_cleared_p = 0;
13342
13343 if (window_height_changed_p)
13344 {
13345 windows_or_buffers_changed = 50;
13346
13347 /* If window configuration was changed, frames may have been
13348 marked garbaged. Clear them or we will experience
13349 surprises wrt scrolling. */
13350 clear_garbaged_frames ();
13351 }
13352 }
13353 else if (EQ (selected_window, minibuf_window)
13354 && (current_buffer->clip_changed || window_outdated (w))
13355 && resize_mini_window (w, 0))
13356 {
13357 /* Resized active mini-window to fit the size of what it is
13358 showing if its contents might have changed. */
13359 must_finish = 1;
13360
13361 /* If window configuration was changed, frames may have been
13362 marked garbaged. Clear them or we will experience
13363 surprises wrt scrolling. */
13364 clear_garbaged_frames ();
13365 }
13366
13367 if (windows_or_buffers_changed && !update_mode_lines)
13368 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13369 only the windows's contents needs to be refreshed, or whether the
13370 mode-lines also need a refresh. */
13371 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13372 ? REDISPLAY_SOME : 32);
13373
13374 /* If specs for an arrow have changed, do thorough redisplay
13375 to ensure we remove any arrow that should no longer exist. */
13376 if (overlay_arrows_changed_p ())
13377 /* Apparently, this is the only case where we update other windows,
13378 without updating other mode-lines. */
13379 windows_or_buffers_changed = 49;
13380
13381 consider_all_windows_p = (update_mode_lines
13382 || windows_or_buffers_changed);
13383
13384 #define AINC(a,i) \
13385 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13386 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13387
13388 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13389 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13390
13391 /* Optimize the case that only the line containing the cursor in the
13392 selected window has changed. Variables starting with this_ are
13393 set in display_line and record information about the line
13394 containing the cursor. */
13395 tlbufpos = this_line_start_pos;
13396 tlendpos = this_line_end_pos;
13397 if (!consider_all_windows_p
13398 && CHARPOS (tlbufpos) > 0
13399 && !w->update_mode_line
13400 && !current_buffer->clip_changed
13401 && !current_buffer->prevent_redisplay_optimizations_p
13402 && FRAME_VISIBLE_P (XFRAME (w->frame))
13403 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13404 && !XFRAME (w->frame)->cursor_type_changed
13405 /* Make sure recorded data applies to current buffer, etc. */
13406 && this_line_buffer == current_buffer
13407 && match_p
13408 && !w->force_start
13409 && !w->optional_new_start
13410 /* Point must be on the line that we have info recorded about. */
13411 && PT >= CHARPOS (tlbufpos)
13412 && PT <= Z - CHARPOS (tlendpos)
13413 /* All text outside that line, including its final newline,
13414 must be unchanged. */
13415 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13416 CHARPOS (tlendpos)))
13417 {
13418 if (CHARPOS (tlbufpos) > BEGV
13419 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13420 && (CHARPOS (tlbufpos) == ZV
13421 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13422 /* Former continuation line has disappeared by becoming empty. */
13423 goto cancel;
13424 else if (window_outdated (w) || MINI_WINDOW_P (w))
13425 {
13426 /* We have to handle the case of continuation around a
13427 wide-column character (see the comment in indent.c around
13428 line 1340).
13429
13430 For instance, in the following case:
13431
13432 -------- Insert --------
13433 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13434 J_I_ ==> J_I_ `^^' are cursors.
13435 ^^ ^^
13436 -------- --------
13437
13438 As we have to redraw the line above, we cannot use this
13439 optimization. */
13440
13441 struct it it;
13442 int line_height_before = this_line_pixel_height;
13443
13444 /* Note that start_display will handle the case that the
13445 line starting at tlbufpos is a continuation line. */
13446 start_display (&it, w, tlbufpos);
13447
13448 /* Implementation note: It this still necessary? */
13449 if (it.current_x != this_line_start_x)
13450 goto cancel;
13451
13452 TRACE ((stderr, "trying display optimization 1\n"));
13453 w->cursor.vpos = -1;
13454 overlay_arrow_seen = 0;
13455 it.vpos = this_line_vpos;
13456 it.current_y = this_line_y;
13457 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13458 display_line (&it);
13459
13460 /* If line contains point, is not continued,
13461 and ends at same distance from eob as before, we win. */
13462 if (w->cursor.vpos >= 0
13463 /* Line is not continued, otherwise this_line_start_pos
13464 would have been set to 0 in display_line. */
13465 && CHARPOS (this_line_start_pos)
13466 /* Line ends as before. */
13467 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13468 /* Line has same height as before. Otherwise other lines
13469 would have to be shifted up or down. */
13470 && this_line_pixel_height == line_height_before)
13471 {
13472 /* If this is not the window's last line, we must adjust
13473 the charstarts of the lines below. */
13474 if (it.current_y < it.last_visible_y)
13475 {
13476 struct glyph_row *row
13477 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13478 ptrdiff_t delta, delta_bytes;
13479
13480 /* We used to distinguish between two cases here,
13481 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13482 when the line ends in a newline or the end of the
13483 buffer's accessible portion. But both cases did
13484 the same, so they were collapsed. */
13485 delta = (Z
13486 - CHARPOS (tlendpos)
13487 - MATRIX_ROW_START_CHARPOS (row));
13488 delta_bytes = (Z_BYTE
13489 - BYTEPOS (tlendpos)
13490 - MATRIX_ROW_START_BYTEPOS (row));
13491
13492 increment_matrix_positions (w->current_matrix,
13493 this_line_vpos + 1,
13494 w->current_matrix->nrows,
13495 delta, delta_bytes);
13496 }
13497
13498 /* If this row displays text now but previously didn't,
13499 or vice versa, w->window_end_vpos may have to be
13500 adjusted. */
13501 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13502 {
13503 if (w->window_end_vpos < this_line_vpos)
13504 w->window_end_vpos = this_line_vpos;
13505 }
13506 else if (w->window_end_vpos == this_line_vpos
13507 && this_line_vpos > 0)
13508 w->window_end_vpos = this_line_vpos - 1;
13509 w->window_end_valid = 0;
13510
13511 /* Update hint: No need to try to scroll in update_window. */
13512 w->desired_matrix->no_scrolling_p = 1;
13513
13514 #ifdef GLYPH_DEBUG
13515 *w->desired_matrix->method = 0;
13516 debug_method_add (w, "optimization 1");
13517 #endif
13518 #ifdef HAVE_WINDOW_SYSTEM
13519 update_window_fringes (w, 0);
13520 #endif
13521 goto update;
13522 }
13523 else
13524 goto cancel;
13525 }
13526 else if (/* Cursor position hasn't changed. */
13527 PT == w->last_point
13528 /* Make sure the cursor was last displayed
13529 in this window. Otherwise we have to reposition it. */
13530
13531 /* PXW: Must be converted to pixels, probably. */
13532 && 0 <= w->cursor.vpos
13533 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13534 {
13535 if (!must_finish)
13536 {
13537 do_pending_window_change (1);
13538 /* If selected_window changed, redisplay again. */
13539 if (WINDOWP (selected_window)
13540 && (w = XWINDOW (selected_window)) != sw)
13541 goto retry;
13542
13543 /* We used to always goto end_of_redisplay here, but this
13544 isn't enough if we have a blinking cursor. */
13545 if (w->cursor_off_p == w->last_cursor_off_p)
13546 goto end_of_redisplay;
13547 }
13548 goto update;
13549 }
13550 /* If highlighting the region, or if the cursor is in the echo area,
13551 then we can't just move the cursor. */
13552 else if (NILP (Vshow_trailing_whitespace)
13553 && !cursor_in_echo_area)
13554 {
13555 struct it it;
13556 struct glyph_row *row;
13557
13558 /* Skip from tlbufpos to PT and see where it is. Note that
13559 PT may be in invisible text. If so, we will end at the
13560 next visible position. */
13561 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13562 NULL, DEFAULT_FACE_ID);
13563 it.current_x = this_line_start_x;
13564 it.current_y = this_line_y;
13565 it.vpos = this_line_vpos;
13566
13567 /* The call to move_it_to stops in front of PT, but
13568 moves over before-strings. */
13569 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13570
13571 if (it.vpos == this_line_vpos
13572 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13573 row->enabled_p))
13574 {
13575 eassert (this_line_vpos == it.vpos);
13576 eassert (this_line_y == it.current_y);
13577 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13578 #ifdef GLYPH_DEBUG
13579 *w->desired_matrix->method = 0;
13580 debug_method_add (w, "optimization 3");
13581 #endif
13582 goto update;
13583 }
13584 else
13585 goto cancel;
13586 }
13587
13588 cancel:
13589 /* Text changed drastically or point moved off of line. */
13590 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13591 }
13592
13593 CHARPOS (this_line_start_pos) = 0;
13594 ++clear_face_cache_count;
13595 #ifdef HAVE_WINDOW_SYSTEM
13596 ++clear_image_cache_count;
13597 #endif
13598
13599 /* Build desired matrices, and update the display. If
13600 consider_all_windows_p is non-zero, do it for all windows on all
13601 frames. Otherwise do it for selected_window, only. */
13602
13603 if (consider_all_windows_p)
13604 {
13605 FOR_EACH_FRAME (tail, frame)
13606 XFRAME (frame)->updated_p = 0;
13607
13608 propagate_buffer_redisplay ();
13609
13610 FOR_EACH_FRAME (tail, frame)
13611 {
13612 struct frame *f = XFRAME (frame);
13613
13614 /* We don't have to do anything for unselected terminal
13615 frames. */
13616 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13617 && !EQ (FRAME_TTY (f)->top_frame, frame))
13618 continue;
13619
13620 retry_frame:
13621
13622 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13623 {
13624 bool gcscrollbars
13625 /* Only GC scrollbars when we redisplay the whole frame. */
13626 = f->redisplay || !REDISPLAY_SOME_P ();
13627 /* Mark all the scroll bars to be removed; we'll redeem
13628 the ones we want when we redisplay their windows. */
13629 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13630 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13631
13632 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13633 redisplay_windows (FRAME_ROOT_WINDOW (f));
13634 /* Remember that the invisible frames need to be redisplayed next
13635 time they're visible. */
13636 else if (!REDISPLAY_SOME_P ())
13637 f->redisplay = true;
13638
13639 /* The X error handler may have deleted that frame. */
13640 if (!FRAME_LIVE_P (f))
13641 continue;
13642
13643 /* Any scroll bars which redisplay_windows should have
13644 nuked should now go away. */
13645 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13646 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13647
13648 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13649 {
13650 /* If fonts changed on visible frame, display again. */
13651 if (f->fonts_changed)
13652 {
13653 adjust_frame_glyphs (f);
13654 f->fonts_changed = 0;
13655 goto retry_frame;
13656 }
13657
13658 /* See if we have to hscroll. */
13659 if (!f->already_hscrolled_p)
13660 {
13661 f->already_hscrolled_p = 1;
13662 if (hscroll_windows (f->root_window))
13663 goto retry_frame;
13664 }
13665
13666 /* Prevent various kinds of signals during display
13667 update. stdio is not robust about handling
13668 signals, which can cause an apparent I/O error. */
13669 if (interrupt_input)
13670 unrequest_sigio ();
13671 STOP_POLLING;
13672
13673 pending |= update_frame (f, 0, 0);
13674 f->cursor_type_changed = 0;
13675 f->updated_p = 1;
13676 }
13677 }
13678 }
13679
13680 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13681
13682 if (!pending)
13683 {
13684 /* Do the mark_window_display_accurate after all windows have
13685 been redisplayed because this call resets flags in buffers
13686 which are needed for proper redisplay. */
13687 FOR_EACH_FRAME (tail, frame)
13688 {
13689 struct frame *f = XFRAME (frame);
13690 if (f->updated_p)
13691 {
13692 f->redisplay = false;
13693 mark_window_display_accurate (f->root_window, 1);
13694 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13695 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13696 }
13697 }
13698 }
13699 }
13700 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13701 {
13702 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13703 struct frame *mini_frame;
13704
13705 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13706 /* Use list_of_error, not Qerror, so that
13707 we catch only errors and don't run the debugger. */
13708 internal_condition_case_1 (redisplay_window_1, selected_window,
13709 list_of_error,
13710 redisplay_window_error);
13711 if (update_miniwindow_p)
13712 internal_condition_case_1 (redisplay_window_1, mini_window,
13713 list_of_error,
13714 redisplay_window_error);
13715
13716 /* Compare desired and current matrices, perform output. */
13717
13718 update:
13719 /* If fonts changed, display again. */
13720 if (sf->fonts_changed)
13721 goto retry;
13722
13723 /* Prevent various kinds of signals during display update.
13724 stdio is not robust about handling signals,
13725 which can cause an apparent I/O error. */
13726 if (interrupt_input)
13727 unrequest_sigio ();
13728 STOP_POLLING;
13729
13730 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13731 {
13732 if (hscroll_windows (selected_window))
13733 goto retry;
13734
13735 XWINDOW (selected_window)->must_be_updated_p = true;
13736 pending = update_frame (sf, 0, 0);
13737 sf->cursor_type_changed = 0;
13738 }
13739
13740 /* We may have called echo_area_display at the top of this
13741 function. If the echo area is on another frame, that may
13742 have put text on a frame other than the selected one, so the
13743 above call to update_frame would not have caught it. Catch
13744 it here. */
13745 mini_window = FRAME_MINIBUF_WINDOW (sf);
13746 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13747
13748 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13749 {
13750 XWINDOW (mini_window)->must_be_updated_p = true;
13751 pending |= update_frame (mini_frame, 0, 0);
13752 mini_frame->cursor_type_changed = 0;
13753 if (!pending && hscroll_windows (mini_window))
13754 goto retry;
13755 }
13756 }
13757
13758 /* If display was paused because of pending input, make sure we do a
13759 thorough update the next time. */
13760 if (pending)
13761 {
13762 /* Prevent the optimization at the beginning of
13763 redisplay_internal that tries a single-line update of the
13764 line containing the cursor in the selected window. */
13765 CHARPOS (this_line_start_pos) = 0;
13766
13767 /* Let the overlay arrow be updated the next time. */
13768 update_overlay_arrows (0);
13769
13770 /* If we pause after scrolling, some rows in the current
13771 matrices of some windows are not valid. */
13772 if (!WINDOW_FULL_WIDTH_P (w)
13773 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13774 update_mode_lines = 36;
13775 }
13776 else
13777 {
13778 if (!consider_all_windows_p)
13779 {
13780 /* This has already been done above if
13781 consider_all_windows_p is set. */
13782 if (XBUFFER (w->contents)->text->redisplay
13783 && buffer_window_count (XBUFFER (w->contents)) > 1)
13784 /* This can happen if b->text->redisplay was set during
13785 jit-lock. */
13786 propagate_buffer_redisplay ();
13787 mark_window_display_accurate_1 (w, 1);
13788
13789 /* Say overlay arrows are up to date. */
13790 update_overlay_arrows (1);
13791
13792 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13793 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13794 }
13795
13796 update_mode_lines = 0;
13797 windows_or_buffers_changed = 0;
13798 }
13799
13800 /* Start SIGIO interrupts coming again. Having them off during the
13801 code above makes it less likely one will discard output, but not
13802 impossible, since there might be stuff in the system buffer here.
13803 But it is much hairier to try to do anything about that. */
13804 if (interrupt_input)
13805 request_sigio ();
13806 RESUME_POLLING;
13807
13808 /* If a frame has become visible which was not before, redisplay
13809 again, so that we display it. Expose events for such a frame
13810 (which it gets when becoming visible) don't call the parts of
13811 redisplay constructing glyphs, so simply exposing a frame won't
13812 display anything in this case. So, we have to display these
13813 frames here explicitly. */
13814 if (!pending)
13815 {
13816 int new_count = 0;
13817
13818 FOR_EACH_FRAME (tail, frame)
13819 {
13820 if (XFRAME (frame)->visible)
13821 new_count++;
13822 }
13823
13824 if (new_count != number_of_visible_frames)
13825 windows_or_buffers_changed = 52;
13826 }
13827
13828 /* Change frame size now if a change is pending. */
13829 do_pending_window_change (1);
13830
13831 /* If we just did a pending size change, or have additional
13832 visible frames, or selected_window changed, redisplay again. */
13833 if ((windows_or_buffers_changed && !pending)
13834 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13835 goto retry;
13836
13837 /* Clear the face and image caches.
13838
13839 We used to do this only if consider_all_windows_p. But the cache
13840 needs to be cleared if a timer creates images in the current
13841 buffer (e.g. the test case in Bug#6230). */
13842
13843 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13844 {
13845 clear_face_cache (0);
13846 clear_face_cache_count = 0;
13847 }
13848
13849 #ifdef HAVE_WINDOW_SYSTEM
13850 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13851 {
13852 clear_image_caches (Qnil);
13853 clear_image_cache_count = 0;
13854 }
13855 #endif /* HAVE_WINDOW_SYSTEM */
13856
13857 end_of_redisplay:
13858 if (interrupt_input && interrupts_deferred)
13859 request_sigio ();
13860
13861 unbind_to (count, Qnil);
13862 RESUME_POLLING;
13863 }
13864
13865
13866 /* Redisplay, but leave alone any recent echo area message unless
13867 another message has been requested in its place.
13868
13869 This is useful in situations where you need to redisplay but no
13870 user action has occurred, making it inappropriate for the message
13871 area to be cleared. See tracking_off and
13872 wait_reading_process_output for examples of these situations.
13873
13874 FROM_WHERE is an integer saying from where this function was
13875 called. This is useful for debugging. */
13876
13877 void
13878 redisplay_preserve_echo_area (int from_where)
13879 {
13880 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13881
13882 if (!NILP (echo_area_buffer[1]))
13883 {
13884 /* We have a previously displayed message, but no current
13885 message. Redisplay the previous message. */
13886 display_last_displayed_message_p = 1;
13887 redisplay_internal ();
13888 display_last_displayed_message_p = 0;
13889 }
13890 else
13891 redisplay_internal ();
13892
13893 flush_frame (SELECTED_FRAME ());
13894 }
13895
13896
13897 /* Function registered with record_unwind_protect in redisplay_internal. */
13898
13899 static void
13900 unwind_redisplay (void)
13901 {
13902 redisplaying_p = 0;
13903 }
13904
13905
13906 /* Mark the display of leaf window W as accurate or inaccurate.
13907 If ACCURATE_P is non-zero mark display of W as accurate. If
13908 ACCURATE_P is zero, arrange for W to be redisplayed the next
13909 time redisplay_internal is called. */
13910
13911 static void
13912 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13913 {
13914 struct buffer *b = XBUFFER (w->contents);
13915
13916 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13917 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13918 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13919
13920 if (accurate_p)
13921 {
13922 b->clip_changed = false;
13923 b->prevent_redisplay_optimizations_p = false;
13924 eassert (buffer_window_count (b) > 0);
13925 /* Resetting b->text->redisplay is problematic!
13926 In order to make it safer to do it here, redisplay_internal must
13927 have copied all b->text->redisplay to their respective windows. */
13928 b->text->redisplay = false;
13929
13930 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13931 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13932 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13933 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13934
13935 w->current_matrix->buffer = b;
13936 w->current_matrix->begv = BUF_BEGV (b);
13937 w->current_matrix->zv = BUF_ZV (b);
13938
13939 w->last_cursor_vpos = w->cursor.vpos;
13940 w->last_cursor_off_p = w->cursor_off_p;
13941
13942 if (w == XWINDOW (selected_window))
13943 w->last_point = BUF_PT (b);
13944 else
13945 w->last_point = marker_position (w->pointm);
13946
13947 w->window_end_valid = true;
13948 w->update_mode_line = false;
13949 }
13950
13951 w->redisplay = !accurate_p;
13952 }
13953
13954
13955 /* Mark the display of windows in the window tree rooted at WINDOW as
13956 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13957 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13958 be redisplayed the next time redisplay_internal is called. */
13959
13960 void
13961 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13962 {
13963 struct window *w;
13964
13965 for (; !NILP (window); window = w->next)
13966 {
13967 w = XWINDOW (window);
13968 if (WINDOWP (w->contents))
13969 mark_window_display_accurate (w->contents, accurate_p);
13970 else
13971 mark_window_display_accurate_1 (w, accurate_p);
13972 }
13973
13974 if (accurate_p)
13975 update_overlay_arrows (1);
13976 else
13977 /* Force a thorough redisplay the next time by setting
13978 last_arrow_position and last_arrow_string to t, which is
13979 unequal to any useful value of Voverlay_arrow_... */
13980 update_overlay_arrows (-1);
13981 }
13982
13983
13984 /* Return value in display table DP (Lisp_Char_Table *) for character
13985 C. Since a display table doesn't have any parent, we don't have to
13986 follow parent. Do not call this function directly but use the
13987 macro DISP_CHAR_VECTOR. */
13988
13989 Lisp_Object
13990 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13991 {
13992 Lisp_Object val;
13993
13994 if (ASCII_CHAR_P (c))
13995 {
13996 val = dp->ascii;
13997 if (SUB_CHAR_TABLE_P (val))
13998 val = XSUB_CHAR_TABLE (val)->contents[c];
13999 }
14000 else
14001 {
14002 Lisp_Object table;
14003
14004 XSETCHAR_TABLE (table, dp);
14005 val = char_table_ref (table, c);
14006 }
14007 if (NILP (val))
14008 val = dp->defalt;
14009 return val;
14010 }
14011
14012
14013 \f
14014 /***********************************************************************
14015 Window Redisplay
14016 ***********************************************************************/
14017
14018 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14019
14020 static void
14021 redisplay_windows (Lisp_Object window)
14022 {
14023 while (!NILP (window))
14024 {
14025 struct window *w = XWINDOW (window);
14026
14027 if (WINDOWP (w->contents))
14028 redisplay_windows (w->contents);
14029 else if (BUFFERP (w->contents))
14030 {
14031 displayed_buffer = XBUFFER (w->contents);
14032 /* Use list_of_error, not Qerror, so that
14033 we catch only errors and don't run the debugger. */
14034 internal_condition_case_1 (redisplay_window_0, window,
14035 list_of_error,
14036 redisplay_window_error);
14037 }
14038
14039 window = w->next;
14040 }
14041 }
14042
14043 static Lisp_Object
14044 redisplay_window_error (Lisp_Object ignore)
14045 {
14046 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14047 return Qnil;
14048 }
14049
14050 static Lisp_Object
14051 redisplay_window_0 (Lisp_Object window)
14052 {
14053 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14054 redisplay_window (window, false);
14055 return Qnil;
14056 }
14057
14058 static Lisp_Object
14059 redisplay_window_1 (Lisp_Object window)
14060 {
14061 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14062 redisplay_window (window, true);
14063 return Qnil;
14064 }
14065 \f
14066
14067 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14068 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14069 which positions recorded in ROW differ from current buffer
14070 positions.
14071
14072 Return 0 if cursor is not on this row, 1 otherwise. */
14073
14074 static int
14075 set_cursor_from_row (struct window *w, struct glyph_row *row,
14076 struct glyph_matrix *matrix,
14077 ptrdiff_t delta, ptrdiff_t delta_bytes,
14078 int dy, int dvpos)
14079 {
14080 struct glyph *glyph = row->glyphs[TEXT_AREA];
14081 struct glyph *end = glyph + row->used[TEXT_AREA];
14082 struct glyph *cursor = NULL;
14083 /* The last known character position in row. */
14084 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14085 int x = row->x;
14086 ptrdiff_t pt_old = PT - delta;
14087 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14088 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14089 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14090 /* A glyph beyond the edge of TEXT_AREA which we should never
14091 touch. */
14092 struct glyph *glyphs_end = end;
14093 /* Non-zero means we've found a match for cursor position, but that
14094 glyph has the avoid_cursor_p flag set. */
14095 int match_with_avoid_cursor = 0;
14096 /* Non-zero means we've seen at least one glyph that came from a
14097 display string. */
14098 int string_seen = 0;
14099 /* Largest and smallest buffer positions seen so far during scan of
14100 glyph row. */
14101 ptrdiff_t bpos_max = pos_before;
14102 ptrdiff_t bpos_min = pos_after;
14103 /* Last buffer position covered by an overlay string with an integer
14104 `cursor' property. */
14105 ptrdiff_t bpos_covered = 0;
14106 /* Non-zero means the display string on which to display the cursor
14107 comes from a text property, not from an overlay. */
14108 int string_from_text_prop = 0;
14109
14110 /* Don't even try doing anything if called for a mode-line or
14111 header-line row, since the rest of the code isn't prepared to
14112 deal with such calamities. */
14113 eassert (!row->mode_line_p);
14114 if (row->mode_line_p)
14115 return 0;
14116
14117 /* Skip over glyphs not having an object at the start and the end of
14118 the row. These are special glyphs like truncation marks on
14119 terminal frames. */
14120 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14121 {
14122 if (!row->reversed_p)
14123 {
14124 while (glyph < end
14125 && INTEGERP (glyph->object)
14126 && glyph->charpos < 0)
14127 {
14128 x += glyph->pixel_width;
14129 ++glyph;
14130 }
14131 while (end > glyph
14132 && INTEGERP ((end - 1)->object)
14133 /* CHARPOS is zero for blanks and stretch glyphs
14134 inserted by extend_face_to_end_of_line. */
14135 && (end - 1)->charpos <= 0)
14136 --end;
14137 glyph_before = glyph - 1;
14138 glyph_after = end;
14139 }
14140 else
14141 {
14142 struct glyph *g;
14143
14144 /* If the glyph row is reversed, we need to process it from back
14145 to front, so swap the edge pointers. */
14146 glyphs_end = end = glyph - 1;
14147 glyph += row->used[TEXT_AREA] - 1;
14148
14149 while (glyph > end + 1
14150 && INTEGERP (glyph->object)
14151 && glyph->charpos < 0)
14152 {
14153 --glyph;
14154 x -= glyph->pixel_width;
14155 }
14156 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14157 --glyph;
14158 /* By default, in reversed rows we put the cursor on the
14159 rightmost (first in the reading order) glyph. */
14160 for (g = end + 1; g < glyph; g++)
14161 x += g->pixel_width;
14162 while (end < glyph
14163 && INTEGERP ((end + 1)->object)
14164 && (end + 1)->charpos <= 0)
14165 ++end;
14166 glyph_before = glyph + 1;
14167 glyph_after = end;
14168 }
14169 }
14170 else if (row->reversed_p)
14171 {
14172 /* In R2L rows that don't display text, put the cursor on the
14173 rightmost glyph. Case in point: an empty last line that is
14174 part of an R2L paragraph. */
14175 cursor = end - 1;
14176 /* Avoid placing the cursor on the last glyph of the row, where
14177 on terminal frames we hold the vertical border between
14178 adjacent windows. */
14179 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14180 && !WINDOW_RIGHTMOST_P (w)
14181 && cursor == row->glyphs[LAST_AREA] - 1)
14182 cursor--;
14183 x = -1; /* will be computed below, at label compute_x */
14184 }
14185
14186 /* Step 1: Try to find the glyph whose character position
14187 corresponds to point. If that's not possible, find 2 glyphs
14188 whose character positions are the closest to point, one before
14189 point, the other after it. */
14190 if (!row->reversed_p)
14191 while (/* not marched to end of glyph row */
14192 glyph < end
14193 /* glyph was not inserted by redisplay for internal purposes */
14194 && !INTEGERP (glyph->object))
14195 {
14196 if (BUFFERP (glyph->object))
14197 {
14198 ptrdiff_t dpos = glyph->charpos - pt_old;
14199
14200 if (glyph->charpos > bpos_max)
14201 bpos_max = glyph->charpos;
14202 if (glyph->charpos < bpos_min)
14203 bpos_min = glyph->charpos;
14204 if (!glyph->avoid_cursor_p)
14205 {
14206 /* If we hit point, we've found the glyph on which to
14207 display the cursor. */
14208 if (dpos == 0)
14209 {
14210 match_with_avoid_cursor = 0;
14211 break;
14212 }
14213 /* See if we've found a better approximation to
14214 POS_BEFORE or to POS_AFTER. */
14215 if (0 > dpos && dpos > pos_before - pt_old)
14216 {
14217 pos_before = glyph->charpos;
14218 glyph_before = glyph;
14219 }
14220 else if (0 < dpos && dpos < pos_after - pt_old)
14221 {
14222 pos_after = glyph->charpos;
14223 glyph_after = glyph;
14224 }
14225 }
14226 else if (dpos == 0)
14227 match_with_avoid_cursor = 1;
14228 }
14229 else if (STRINGP (glyph->object))
14230 {
14231 Lisp_Object chprop;
14232 ptrdiff_t glyph_pos = glyph->charpos;
14233
14234 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14235 glyph->object);
14236 if (!NILP (chprop))
14237 {
14238 /* If the string came from a `display' text property,
14239 look up the buffer position of that property and
14240 use that position to update bpos_max, as if we
14241 actually saw such a position in one of the row's
14242 glyphs. This helps with supporting integer values
14243 of `cursor' property on the display string in
14244 situations where most or all of the row's buffer
14245 text is completely covered by display properties,
14246 so that no glyph with valid buffer positions is
14247 ever seen in the row. */
14248 ptrdiff_t prop_pos =
14249 string_buffer_position_lim (glyph->object, pos_before,
14250 pos_after, 0);
14251
14252 if (prop_pos >= pos_before)
14253 bpos_max = prop_pos - 1;
14254 }
14255 if (INTEGERP (chprop))
14256 {
14257 bpos_covered = bpos_max + XINT (chprop);
14258 /* If the `cursor' property covers buffer positions up
14259 to and including point, we should display cursor on
14260 this glyph. Note that, if a `cursor' property on one
14261 of the string's characters has an integer value, we
14262 will break out of the loop below _before_ we get to
14263 the position match above. IOW, integer values of
14264 the `cursor' property override the "exact match for
14265 point" strategy of positioning the cursor. */
14266 /* Implementation note: bpos_max == pt_old when, e.g.,
14267 we are in an empty line, where bpos_max is set to
14268 MATRIX_ROW_START_CHARPOS, see above. */
14269 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14270 {
14271 cursor = glyph;
14272 break;
14273 }
14274 }
14275
14276 string_seen = 1;
14277 }
14278 x += glyph->pixel_width;
14279 ++glyph;
14280 }
14281 else if (glyph > end) /* row is reversed */
14282 while (!INTEGERP (glyph->object))
14283 {
14284 if (BUFFERP (glyph->object))
14285 {
14286 ptrdiff_t dpos = glyph->charpos - pt_old;
14287
14288 if (glyph->charpos > bpos_max)
14289 bpos_max = glyph->charpos;
14290 if (glyph->charpos < bpos_min)
14291 bpos_min = glyph->charpos;
14292 if (!glyph->avoid_cursor_p)
14293 {
14294 if (dpos == 0)
14295 {
14296 match_with_avoid_cursor = 0;
14297 break;
14298 }
14299 if (0 > dpos && dpos > pos_before - pt_old)
14300 {
14301 pos_before = glyph->charpos;
14302 glyph_before = glyph;
14303 }
14304 else if (0 < dpos && dpos < pos_after - pt_old)
14305 {
14306 pos_after = glyph->charpos;
14307 glyph_after = glyph;
14308 }
14309 }
14310 else if (dpos == 0)
14311 match_with_avoid_cursor = 1;
14312 }
14313 else if (STRINGP (glyph->object))
14314 {
14315 Lisp_Object chprop;
14316 ptrdiff_t glyph_pos = glyph->charpos;
14317
14318 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14319 glyph->object);
14320 if (!NILP (chprop))
14321 {
14322 ptrdiff_t prop_pos =
14323 string_buffer_position_lim (glyph->object, pos_before,
14324 pos_after, 0);
14325
14326 if (prop_pos >= pos_before)
14327 bpos_max = prop_pos - 1;
14328 }
14329 if (INTEGERP (chprop))
14330 {
14331 bpos_covered = bpos_max + XINT (chprop);
14332 /* If the `cursor' property covers buffer positions up
14333 to and including point, we should display cursor on
14334 this glyph. */
14335 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14336 {
14337 cursor = glyph;
14338 break;
14339 }
14340 }
14341 string_seen = 1;
14342 }
14343 --glyph;
14344 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14345 {
14346 x--; /* can't use any pixel_width */
14347 break;
14348 }
14349 x -= glyph->pixel_width;
14350 }
14351
14352 /* Step 2: If we didn't find an exact match for point, we need to
14353 look for a proper place to put the cursor among glyphs between
14354 GLYPH_BEFORE and GLYPH_AFTER. */
14355 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14356 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14357 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14358 {
14359 /* An empty line has a single glyph whose OBJECT is zero and
14360 whose CHARPOS is the position of a newline on that line.
14361 Note that on a TTY, there are more glyphs after that, which
14362 were produced by extend_face_to_end_of_line, but their
14363 CHARPOS is zero or negative. */
14364 int empty_line_p =
14365 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14366 && INTEGERP (glyph->object) && glyph->charpos > 0
14367 /* On a TTY, continued and truncated rows also have a glyph at
14368 their end whose OBJECT is zero and whose CHARPOS is
14369 positive (the continuation and truncation glyphs), but such
14370 rows are obviously not "empty". */
14371 && !(row->continued_p || row->truncated_on_right_p);
14372
14373 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14374 {
14375 ptrdiff_t ellipsis_pos;
14376
14377 /* Scan back over the ellipsis glyphs. */
14378 if (!row->reversed_p)
14379 {
14380 ellipsis_pos = (glyph - 1)->charpos;
14381 while (glyph > row->glyphs[TEXT_AREA]
14382 && (glyph - 1)->charpos == ellipsis_pos)
14383 glyph--, x -= glyph->pixel_width;
14384 /* That loop always goes one position too far, including
14385 the glyph before the ellipsis. So scan forward over
14386 that one. */
14387 x += glyph->pixel_width;
14388 glyph++;
14389 }
14390 else /* row is reversed */
14391 {
14392 ellipsis_pos = (glyph + 1)->charpos;
14393 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14394 && (glyph + 1)->charpos == ellipsis_pos)
14395 glyph++, x += glyph->pixel_width;
14396 x -= glyph->pixel_width;
14397 glyph--;
14398 }
14399 }
14400 else if (match_with_avoid_cursor)
14401 {
14402 cursor = glyph_after;
14403 x = -1;
14404 }
14405 else if (string_seen)
14406 {
14407 int incr = row->reversed_p ? -1 : +1;
14408
14409 /* Need to find the glyph that came out of a string which is
14410 present at point. That glyph is somewhere between
14411 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14412 positioned between POS_BEFORE and POS_AFTER in the
14413 buffer. */
14414 struct glyph *start, *stop;
14415 ptrdiff_t pos = pos_before;
14416
14417 x = -1;
14418
14419 /* If the row ends in a newline from a display string,
14420 reordering could have moved the glyphs belonging to the
14421 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14422 in this case we extend the search to the last glyph in
14423 the row that was not inserted by redisplay. */
14424 if (row->ends_in_newline_from_string_p)
14425 {
14426 glyph_after = end;
14427 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14428 }
14429
14430 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14431 correspond to POS_BEFORE and POS_AFTER, respectively. We
14432 need START and STOP in the order that corresponds to the
14433 row's direction as given by its reversed_p flag. If the
14434 directionality of characters between POS_BEFORE and
14435 POS_AFTER is the opposite of the row's base direction,
14436 these characters will have been reordered for display,
14437 and we need to reverse START and STOP. */
14438 if (!row->reversed_p)
14439 {
14440 start = min (glyph_before, glyph_after);
14441 stop = max (glyph_before, glyph_after);
14442 }
14443 else
14444 {
14445 start = max (glyph_before, glyph_after);
14446 stop = min (glyph_before, glyph_after);
14447 }
14448 for (glyph = start + incr;
14449 row->reversed_p ? glyph > stop : glyph < stop; )
14450 {
14451
14452 /* Any glyphs that come from the buffer are here because
14453 of bidi reordering. Skip them, and only pay
14454 attention to glyphs that came from some string. */
14455 if (STRINGP (glyph->object))
14456 {
14457 Lisp_Object str;
14458 ptrdiff_t tem;
14459 /* If the display property covers the newline, we
14460 need to search for it one position farther. */
14461 ptrdiff_t lim = pos_after
14462 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14463
14464 string_from_text_prop = 0;
14465 str = glyph->object;
14466 tem = string_buffer_position_lim (str, pos, lim, 0);
14467 if (tem == 0 /* from overlay */
14468 || pos <= tem)
14469 {
14470 /* If the string from which this glyph came is
14471 found in the buffer at point, or at position
14472 that is closer to point than pos_after, then
14473 we've found the glyph we've been looking for.
14474 If it comes from an overlay (tem == 0), and
14475 it has the `cursor' property on one of its
14476 glyphs, record that glyph as a candidate for
14477 displaying the cursor. (As in the
14478 unidirectional version, we will display the
14479 cursor on the last candidate we find.) */
14480 if (tem == 0
14481 || tem == pt_old
14482 || (tem - pt_old > 0 && tem < pos_after))
14483 {
14484 /* The glyphs from this string could have
14485 been reordered. Find the one with the
14486 smallest string position. Or there could
14487 be a character in the string with the
14488 `cursor' property, which means display
14489 cursor on that character's glyph. */
14490 ptrdiff_t strpos = glyph->charpos;
14491
14492 if (tem)
14493 {
14494 cursor = glyph;
14495 string_from_text_prop = 1;
14496 }
14497 for ( ;
14498 (row->reversed_p ? glyph > stop : glyph < stop)
14499 && EQ (glyph->object, str);
14500 glyph += incr)
14501 {
14502 Lisp_Object cprop;
14503 ptrdiff_t gpos = glyph->charpos;
14504
14505 cprop = Fget_char_property (make_number (gpos),
14506 Qcursor,
14507 glyph->object);
14508 if (!NILP (cprop))
14509 {
14510 cursor = glyph;
14511 break;
14512 }
14513 if (tem && glyph->charpos < strpos)
14514 {
14515 strpos = glyph->charpos;
14516 cursor = glyph;
14517 }
14518 }
14519
14520 if (tem == pt_old
14521 || (tem - pt_old > 0 && tem < pos_after))
14522 goto compute_x;
14523 }
14524 if (tem)
14525 pos = tem + 1; /* don't find previous instances */
14526 }
14527 /* This string is not what we want; skip all of the
14528 glyphs that came from it. */
14529 while ((row->reversed_p ? glyph > stop : glyph < stop)
14530 && EQ (glyph->object, str))
14531 glyph += incr;
14532 }
14533 else
14534 glyph += incr;
14535 }
14536
14537 /* If we reached the end of the line, and END was from a string,
14538 the cursor is not on this line. */
14539 if (cursor == NULL
14540 && (row->reversed_p ? glyph <= end : glyph >= end)
14541 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14542 && STRINGP (end->object)
14543 && row->continued_p)
14544 return 0;
14545 }
14546 /* A truncated row may not include PT among its character positions.
14547 Setting the cursor inside the scroll margin will trigger
14548 recalculation of hscroll in hscroll_window_tree. But if a
14549 display string covers point, defer to the string-handling
14550 code below to figure this out. */
14551 else if (row->truncated_on_left_p && pt_old < bpos_min)
14552 {
14553 cursor = glyph_before;
14554 x = -1;
14555 }
14556 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14557 /* Zero-width characters produce no glyphs. */
14558 || (!empty_line_p
14559 && (row->reversed_p
14560 ? glyph_after > glyphs_end
14561 : glyph_after < glyphs_end)))
14562 {
14563 cursor = glyph_after;
14564 x = -1;
14565 }
14566 }
14567
14568 compute_x:
14569 if (cursor != NULL)
14570 glyph = cursor;
14571 else if (glyph == glyphs_end
14572 && pos_before == pos_after
14573 && STRINGP ((row->reversed_p
14574 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14575 : row->glyphs[TEXT_AREA])->object))
14576 {
14577 /* If all the glyphs of this row came from strings, put the
14578 cursor on the first glyph of the row. This avoids having the
14579 cursor outside of the text area in this very rare and hard
14580 use case. */
14581 glyph =
14582 row->reversed_p
14583 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14584 : row->glyphs[TEXT_AREA];
14585 }
14586 if (x < 0)
14587 {
14588 struct glyph *g;
14589
14590 /* Need to compute x that corresponds to GLYPH. */
14591 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14592 {
14593 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14594 emacs_abort ();
14595 x += g->pixel_width;
14596 }
14597 }
14598
14599 /* ROW could be part of a continued line, which, under bidi
14600 reordering, might have other rows whose start and end charpos
14601 occlude point. Only set w->cursor if we found a better
14602 approximation to the cursor position than we have from previously
14603 examined candidate rows belonging to the same continued line. */
14604 if (/* We already have a candidate row. */
14605 w->cursor.vpos >= 0
14606 /* That candidate is not the row we are processing. */
14607 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14608 /* Make sure cursor.vpos specifies a row whose start and end
14609 charpos occlude point, and it is valid candidate for being a
14610 cursor-row. This is because some callers of this function
14611 leave cursor.vpos at the row where the cursor was displayed
14612 during the last redisplay cycle. */
14613 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14614 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14615 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14616 {
14617 struct glyph *g1
14618 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14619
14620 /* Don't consider glyphs that are outside TEXT_AREA. */
14621 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14622 return 0;
14623 /* Keep the candidate whose buffer position is the closest to
14624 point or has the `cursor' property. */
14625 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14626 w->cursor.hpos >= 0
14627 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14628 && ((BUFFERP (g1->object)
14629 && (g1->charpos == pt_old /* An exact match always wins. */
14630 || (BUFFERP (glyph->object)
14631 && eabs (g1->charpos - pt_old)
14632 < eabs (glyph->charpos - pt_old))))
14633 /* Previous candidate is a glyph from a string that has
14634 a non-nil `cursor' property. */
14635 || (STRINGP (g1->object)
14636 && (!NILP (Fget_char_property (make_number (g1->charpos),
14637 Qcursor, g1->object))
14638 /* Previous candidate is from the same display
14639 string as this one, and the display string
14640 came from a text property. */
14641 || (EQ (g1->object, glyph->object)
14642 && string_from_text_prop)
14643 /* this candidate is from newline and its
14644 position is not an exact match */
14645 || (INTEGERP (glyph->object)
14646 && glyph->charpos != pt_old)))))
14647 return 0;
14648 /* If this candidate gives an exact match, use that. */
14649 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14650 /* If this candidate is a glyph created for the
14651 terminating newline of a line, and point is on that
14652 newline, it wins because it's an exact match. */
14653 || (!row->continued_p
14654 && INTEGERP (glyph->object)
14655 && glyph->charpos == 0
14656 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14657 /* Otherwise, keep the candidate that comes from a row
14658 spanning less buffer positions. This may win when one or
14659 both candidate positions are on glyphs that came from
14660 display strings, for which we cannot compare buffer
14661 positions. */
14662 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14663 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14664 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14665 return 0;
14666 }
14667 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14668 w->cursor.x = x;
14669 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14670 w->cursor.y = row->y + dy;
14671
14672 if (w == XWINDOW (selected_window))
14673 {
14674 if (!row->continued_p
14675 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14676 && row->x == 0)
14677 {
14678 this_line_buffer = XBUFFER (w->contents);
14679
14680 CHARPOS (this_line_start_pos)
14681 = MATRIX_ROW_START_CHARPOS (row) + delta;
14682 BYTEPOS (this_line_start_pos)
14683 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14684
14685 CHARPOS (this_line_end_pos)
14686 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14687 BYTEPOS (this_line_end_pos)
14688 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14689
14690 this_line_y = w->cursor.y;
14691 this_line_pixel_height = row->height;
14692 this_line_vpos = w->cursor.vpos;
14693 this_line_start_x = row->x;
14694 }
14695 else
14696 CHARPOS (this_line_start_pos) = 0;
14697 }
14698
14699 return 1;
14700 }
14701
14702
14703 /* Run window scroll functions, if any, for WINDOW with new window
14704 start STARTP. Sets the window start of WINDOW to that position.
14705
14706 We assume that the window's buffer is really current. */
14707
14708 static struct text_pos
14709 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14710 {
14711 struct window *w = XWINDOW (window);
14712 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14713
14714 eassert (current_buffer == XBUFFER (w->contents));
14715
14716 if (!NILP (Vwindow_scroll_functions))
14717 {
14718 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14719 make_number (CHARPOS (startp)));
14720 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14721 /* In case the hook functions switch buffers. */
14722 set_buffer_internal (XBUFFER (w->contents));
14723 }
14724
14725 return startp;
14726 }
14727
14728
14729 /* Make sure the line containing the cursor is fully visible.
14730 A value of 1 means there is nothing to be done.
14731 (Either the line is fully visible, or it cannot be made so,
14732 or we cannot tell.)
14733
14734 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14735 is higher than window.
14736
14737 A value of 0 means the caller should do scrolling
14738 as if point had gone off the screen. */
14739
14740 static int
14741 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14742 {
14743 struct glyph_matrix *matrix;
14744 struct glyph_row *row;
14745 int window_height;
14746
14747 if (!make_cursor_line_fully_visible_p)
14748 return 1;
14749
14750 /* It's not always possible to find the cursor, e.g, when a window
14751 is full of overlay strings. Don't do anything in that case. */
14752 if (w->cursor.vpos < 0)
14753 return 1;
14754
14755 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14756 row = MATRIX_ROW (matrix, w->cursor.vpos);
14757
14758 /* If the cursor row is not partially visible, there's nothing to do. */
14759 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14760 return 1;
14761
14762 /* If the row the cursor is in is taller than the window's height,
14763 it's not clear what to do, so do nothing. */
14764 window_height = window_box_height (w);
14765 if (row->height >= window_height)
14766 {
14767 if (!force_p || MINI_WINDOW_P (w)
14768 || w->vscroll || w->cursor.vpos == 0)
14769 return 1;
14770 }
14771 return 0;
14772 }
14773
14774
14775 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14776 non-zero means only WINDOW is redisplayed in redisplay_internal.
14777 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14778 in redisplay_window to bring a partially visible line into view in
14779 the case that only the cursor has moved.
14780
14781 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14782 last screen line's vertical height extends past the end of the screen.
14783
14784 Value is
14785
14786 1 if scrolling succeeded
14787
14788 0 if scrolling didn't find point.
14789
14790 -1 if new fonts have been loaded so that we must interrupt
14791 redisplay, adjust glyph matrices, and try again. */
14792
14793 enum
14794 {
14795 SCROLLING_SUCCESS,
14796 SCROLLING_FAILED,
14797 SCROLLING_NEED_LARGER_MATRICES
14798 };
14799
14800 /* If scroll-conservatively is more than this, never recenter.
14801
14802 If you change this, don't forget to update the doc string of
14803 `scroll-conservatively' and the Emacs manual. */
14804 #define SCROLL_LIMIT 100
14805
14806 static int
14807 try_scrolling (Lisp_Object window, int just_this_one_p,
14808 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14809 int temp_scroll_step, int last_line_misfit)
14810 {
14811 struct window *w = XWINDOW (window);
14812 struct frame *f = XFRAME (w->frame);
14813 struct text_pos pos, startp;
14814 struct it it;
14815 int this_scroll_margin, scroll_max, rc, height;
14816 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14817 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14818 Lisp_Object aggressive;
14819 /* We will never try scrolling more than this number of lines. */
14820 int scroll_limit = SCROLL_LIMIT;
14821 int frame_line_height = default_line_pixel_height (w);
14822 int window_total_lines
14823 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14824
14825 #ifdef GLYPH_DEBUG
14826 debug_method_add (w, "try_scrolling");
14827 #endif
14828
14829 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14830
14831 /* Compute scroll margin height in pixels. We scroll when point is
14832 within this distance from the top or bottom of the window. */
14833 if (scroll_margin > 0)
14834 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14835 * frame_line_height;
14836 else
14837 this_scroll_margin = 0;
14838
14839 /* Force arg_scroll_conservatively to have a reasonable value, to
14840 avoid scrolling too far away with slow move_it_* functions. Note
14841 that the user can supply scroll-conservatively equal to
14842 `most-positive-fixnum', which can be larger than INT_MAX. */
14843 if (arg_scroll_conservatively > scroll_limit)
14844 {
14845 arg_scroll_conservatively = scroll_limit + 1;
14846 scroll_max = scroll_limit * frame_line_height;
14847 }
14848 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14849 /* Compute how much we should try to scroll maximally to bring
14850 point into view. */
14851 scroll_max = (max (scroll_step,
14852 max (arg_scroll_conservatively, temp_scroll_step))
14853 * frame_line_height);
14854 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14855 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14856 /* We're trying to scroll because of aggressive scrolling but no
14857 scroll_step is set. Choose an arbitrary one. */
14858 scroll_max = 10 * frame_line_height;
14859 else
14860 scroll_max = 0;
14861
14862 too_near_end:
14863
14864 /* Decide whether to scroll down. */
14865 if (PT > CHARPOS (startp))
14866 {
14867 int scroll_margin_y;
14868
14869 /* Compute the pixel ypos of the scroll margin, then move IT to
14870 either that ypos or PT, whichever comes first. */
14871 start_display (&it, w, startp);
14872 scroll_margin_y = it.last_visible_y - this_scroll_margin
14873 - frame_line_height * extra_scroll_margin_lines;
14874 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14875 (MOVE_TO_POS | MOVE_TO_Y));
14876
14877 if (PT > CHARPOS (it.current.pos))
14878 {
14879 int y0 = line_bottom_y (&it);
14880 /* Compute how many pixels below window bottom to stop searching
14881 for PT. This avoids costly search for PT that is far away if
14882 the user limited scrolling by a small number of lines, but
14883 always finds PT if scroll_conservatively is set to a large
14884 number, such as most-positive-fixnum. */
14885 int slack = max (scroll_max, 10 * frame_line_height);
14886 int y_to_move = it.last_visible_y + slack;
14887
14888 /* Compute the distance from the scroll margin to PT or to
14889 the scroll limit, whichever comes first. This should
14890 include the height of the cursor line, to make that line
14891 fully visible. */
14892 move_it_to (&it, PT, -1, y_to_move,
14893 -1, MOVE_TO_POS | MOVE_TO_Y);
14894 dy = line_bottom_y (&it) - y0;
14895
14896 if (dy > scroll_max)
14897 return SCROLLING_FAILED;
14898
14899 if (dy > 0)
14900 scroll_down_p = 1;
14901 }
14902 }
14903
14904 if (scroll_down_p)
14905 {
14906 /* Point is in or below the bottom scroll margin, so move the
14907 window start down. If scrolling conservatively, move it just
14908 enough down to make point visible. If scroll_step is set,
14909 move it down by scroll_step. */
14910 if (arg_scroll_conservatively)
14911 amount_to_scroll
14912 = min (max (dy, frame_line_height),
14913 frame_line_height * arg_scroll_conservatively);
14914 else if (scroll_step || temp_scroll_step)
14915 amount_to_scroll = scroll_max;
14916 else
14917 {
14918 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14919 height = WINDOW_BOX_TEXT_HEIGHT (w);
14920 if (NUMBERP (aggressive))
14921 {
14922 double float_amount = XFLOATINT (aggressive) * height;
14923 int aggressive_scroll = float_amount;
14924 if (aggressive_scroll == 0 && float_amount > 0)
14925 aggressive_scroll = 1;
14926 /* Don't let point enter the scroll margin near top of
14927 the window. This could happen if the value of
14928 scroll_up_aggressively is too large and there are
14929 non-zero margins, because scroll_up_aggressively
14930 means put point that fraction of window height
14931 _from_the_bottom_margin_. */
14932 if (aggressive_scroll + 2*this_scroll_margin > height)
14933 aggressive_scroll = height - 2*this_scroll_margin;
14934 amount_to_scroll = dy + aggressive_scroll;
14935 }
14936 }
14937
14938 if (amount_to_scroll <= 0)
14939 return SCROLLING_FAILED;
14940
14941 start_display (&it, w, startp);
14942 if (arg_scroll_conservatively <= scroll_limit)
14943 move_it_vertically (&it, amount_to_scroll);
14944 else
14945 {
14946 /* Extra precision for users who set scroll-conservatively
14947 to a large number: make sure the amount we scroll
14948 the window start is never less than amount_to_scroll,
14949 which was computed as distance from window bottom to
14950 point. This matters when lines at window top and lines
14951 below window bottom have different height. */
14952 struct it it1;
14953 void *it1data = NULL;
14954 /* We use a temporary it1 because line_bottom_y can modify
14955 its argument, if it moves one line down; see there. */
14956 int start_y;
14957
14958 SAVE_IT (it1, it, it1data);
14959 start_y = line_bottom_y (&it1);
14960 do {
14961 RESTORE_IT (&it, &it, it1data);
14962 move_it_by_lines (&it, 1);
14963 SAVE_IT (it1, it, it1data);
14964 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14965 }
14966
14967 /* If STARTP is unchanged, move it down another screen line. */
14968 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14969 move_it_by_lines (&it, 1);
14970 startp = it.current.pos;
14971 }
14972 else
14973 {
14974 struct text_pos scroll_margin_pos = startp;
14975 int y_offset = 0;
14976
14977 /* See if point is inside the scroll margin at the top of the
14978 window. */
14979 if (this_scroll_margin)
14980 {
14981 int y_start;
14982
14983 start_display (&it, w, startp);
14984 y_start = it.current_y;
14985 move_it_vertically (&it, this_scroll_margin);
14986 scroll_margin_pos = it.current.pos;
14987 /* If we didn't move enough before hitting ZV, request
14988 additional amount of scroll, to move point out of the
14989 scroll margin. */
14990 if (IT_CHARPOS (it) == ZV
14991 && it.current_y - y_start < this_scroll_margin)
14992 y_offset = this_scroll_margin - (it.current_y - y_start);
14993 }
14994
14995 if (PT < CHARPOS (scroll_margin_pos))
14996 {
14997 /* Point is in the scroll margin at the top of the window or
14998 above what is displayed in the window. */
14999 int y0, y_to_move;
15000
15001 /* Compute the vertical distance from PT to the scroll
15002 margin position. Move as far as scroll_max allows, or
15003 one screenful, or 10 screen lines, whichever is largest.
15004 Give up if distance is greater than scroll_max or if we
15005 didn't reach the scroll margin position. */
15006 SET_TEXT_POS (pos, PT, PT_BYTE);
15007 start_display (&it, w, pos);
15008 y0 = it.current_y;
15009 y_to_move = max (it.last_visible_y,
15010 max (scroll_max, 10 * frame_line_height));
15011 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15012 y_to_move, -1,
15013 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15014 dy = it.current_y - y0;
15015 if (dy > scroll_max
15016 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15017 return SCROLLING_FAILED;
15018
15019 /* Additional scroll for when ZV was too close to point. */
15020 dy += y_offset;
15021
15022 /* Compute new window start. */
15023 start_display (&it, w, startp);
15024
15025 if (arg_scroll_conservatively)
15026 amount_to_scroll = max (dy, frame_line_height *
15027 max (scroll_step, temp_scroll_step));
15028 else if (scroll_step || temp_scroll_step)
15029 amount_to_scroll = scroll_max;
15030 else
15031 {
15032 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15033 height = WINDOW_BOX_TEXT_HEIGHT (w);
15034 if (NUMBERP (aggressive))
15035 {
15036 double float_amount = XFLOATINT (aggressive) * height;
15037 int aggressive_scroll = float_amount;
15038 if (aggressive_scroll == 0 && float_amount > 0)
15039 aggressive_scroll = 1;
15040 /* Don't let point enter the scroll margin near
15041 bottom of the window, if the value of
15042 scroll_down_aggressively happens to be too
15043 large. */
15044 if (aggressive_scroll + 2*this_scroll_margin > height)
15045 aggressive_scroll = height - 2*this_scroll_margin;
15046 amount_to_scroll = dy + aggressive_scroll;
15047 }
15048 }
15049
15050 if (amount_to_scroll <= 0)
15051 return SCROLLING_FAILED;
15052
15053 move_it_vertically_backward (&it, amount_to_scroll);
15054 startp = it.current.pos;
15055 }
15056 }
15057
15058 /* Run window scroll functions. */
15059 startp = run_window_scroll_functions (window, startp);
15060
15061 /* Display the window. Give up if new fonts are loaded, or if point
15062 doesn't appear. */
15063 if (!try_window (window, startp, 0))
15064 rc = SCROLLING_NEED_LARGER_MATRICES;
15065 else if (w->cursor.vpos < 0)
15066 {
15067 clear_glyph_matrix (w->desired_matrix);
15068 rc = SCROLLING_FAILED;
15069 }
15070 else
15071 {
15072 /* Maybe forget recorded base line for line number display. */
15073 if (!just_this_one_p
15074 || current_buffer->clip_changed
15075 || BEG_UNCHANGED < CHARPOS (startp))
15076 w->base_line_number = 0;
15077
15078 /* If cursor ends up on a partially visible line,
15079 treat that as being off the bottom of the screen. */
15080 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15081 /* It's possible that the cursor is on the first line of the
15082 buffer, which is partially obscured due to a vscroll
15083 (Bug#7537). In that case, avoid looping forever. */
15084 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15085 {
15086 clear_glyph_matrix (w->desired_matrix);
15087 ++extra_scroll_margin_lines;
15088 goto too_near_end;
15089 }
15090 rc = SCROLLING_SUCCESS;
15091 }
15092
15093 return rc;
15094 }
15095
15096
15097 /* Compute a suitable window start for window W if display of W starts
15098 on a continuation line. Value is non-zero if a new window start
15099 was computed.
15100
15101 The new window start will be computed, based on W's width, starting
15102 from the start of the continued line. It is the start of the
15103 screen line with the minimum distance from the old start W->start. */
15104
15105 static int
15106 compute_window_start_on_continuation_line (struct window *w)
15107 {
15108 struct text_pos pos, start_pos;
15109 int window_start_changed_p = 0;
15110
15111 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15112
15113 /* If window start is on a continuation line... Window start may be
15114 < BEGV in case there's invisible text at the start of the
15115 buffer (M-x rmail, for example). */
15116 if (CHARPOS (start_pos) > BEGV
15117 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15118 {
15119 struct it it;
15120 struct glyph_row *row;
15121
15122 /* Handle the case that the window start is out of range. */
15123 if (CHARPOS (start_pos) < BEGV)
15124 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15125 else if (CHARPOS (start_pos) > ZV)
15126 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15127
15128 /* Find the start of the continued line. This should be fast
15129 because find_newline is fast (newline cache). */
15130 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15131 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15132 row, DEFAULT_FACE_ID);
15133 reseat_at_previous_visible_line_start (&it);
15134
15135 /* If the line start is "too far" away from the window start,
15136 say it takes too much time to compute a new window start. */
15137 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15138 /* PXW: Do we need upper bounds here? */
15139 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15140 {
15141 int min_distance, distance;
15142
15143 /* Move forward by display lines to find the new window
15144 start. If window width was enlarged, the new start can
15145 be expected to be > the old start. If window width was
15146 decreased, the new window start will be < the old start.
15147 So, we're looking for the display line start with the
15148 minimum distance from the old window start. */
15149 pos = it.current.pos;
15150 min_distance = INFINITY;
15151 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15152 distance < min_distance)
15153 {
15154 min_distance = distance;
15155 pos = it.current.pos;
15156 if (it.line_wrap == WORD_WRAP)
15157 {
15158 /* Under WORD_WRAP, move_it_by_lines is likely to
15159 overshoot and stop not at the first, but the
15160 second character from the left margin. So in
15161 that case, we need a more tight control on the X
15162 coordinate of the iterator than move_it_by_lines
15163 promises in its contract. The method is to first
15164 go to the last (rightmost) visible character of a
15165 line, then move to the leftmost character on the
15166 next line in a separate call. */
15167 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15168 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15169 move_it_to (&it, ZV, 0,
15170 it.current_y + it.max_ascent + it.max_descent, -1,
15171 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15172 }
15173 else
15174 move_it_by_lines (&it, 1);
15175 }
15176
15177 /* Set the window start there. */
15178 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15179 window_start_changed_p = 1;
15180 }
15181 }
15182
15183 return window_start_changed_p;
15184 }
15185
15186
15187 /* Try cursor movement in case text has not changed in window WINDOW,
15188 with window start STARTP. Value is
15189
15190 CURSOR_MOVEMENT_SUCCESS if successful
15191
15192 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15193
15194 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15195 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15196 we want to scroll as if scroll-step were set to 1. See the code.
15197
15198 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15199 which case we have to abort this redisplay, and adjust matrices
15200 first. */
15201
15202 enum
15203 {
15204 CURSOR_MOVEMENT_SUCCESS,
15205 CURSOR_MOVEMENT_CANNOT_BE_USED,
15206 CURSOR_MOVEMENT_MUST_SCROLL,
15207 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15208 };
15209
15210 static int
15211 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15212 {
15213 struct window *w = XWINDOW (window);
15214 struct frame *f = XFRAME (w->frame);
15215 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15216
15217 #ifdef GLYPH_DEBUG
15218 if (inhibit_try_cursor_movement)
15219 return rc;
15220 #endif
15221
15222 /* Previously, there was a check for Lisp integer in the
15223 if-statement below. Now, this field is converted to
15224 ptrdiff_t, thus zero means invalid position in a buffer. */
15225 eassert (w->last_point > 0);
15226 /* Likewise there was a check whether window_end_vpos is nil or larger
15227 than the window. Now window_end_vpos is int and so never nil, but
15228 let's leave eassert to check whether it fits in the window. */
15229 eassert (w->window_end_vpos < w->current_matrix->nrows);
15230
15231 /* Handle case where text has not changed, only point, and it has
15232 not moved off the frame. */
15233 if (/* Point may be in this window. */
15234 PT >= CHARPOS (startp)
15235 /* Selective display hasn't changed. */
15236 && !current_buffer->clip_changed
15237 /* Function force-mode-line-update is used to force a thorough
15238 redisplay. It sets either windows_or_buffers_changed or
15239 update_mode_lines. So don't take a shortcut here for these
15240 cases. */
15241 && !update_mode_lines
15242 && !windows_or_buffers_changed
15243 && !f->cursor_type_changed
15244 && NILP (Vshow_trailing_whitespace)
15245 /* This code is not used for mini-buffer for the sake of the case
15246 of redisplaying to replace an echo area message; since in
15247 that case the mini-buffer contents per se are usually
15248 unchanged. This code is of no real use in the mini-buffer
15249 since the handling of this_line_start_pos, etc., in redisplay
15250 handles the same cases. */
15251 && !EQ (window, minibuf_window)
15252 && (FRAME_WINDOW_P (f)
15253 || !overlay_arrow_in_current_buffer_p ()))
15254 {
15255 int this_scroll_margin, top_scroll_margin;
15256 struct glyph_row *row = NULL;
15257 int frame_line_height = default_line_pixel_height (w);
15258 int window_total_lines
15259 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15260
15261 #ifdef GLYPH_DEBUG
15262 debug_method_add (w, "cursor movement");
15263 #endif
15264
15265 /* Scroll if point within this distance from the top or bottom
15266 of the window. This is a pixel value. */
15267 if (scroll_margin > 0)
15268 {
15269 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15270 this_scroll_margin *= frame_line_height;
15271 }
15272 else
15273 this_scroll_margin = 0;
15274
15275 top_scroll_margin = this_scroll_margin;
15276 if (WINDOW_WANTS_HEADER_LINE_P (w))
15277 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15278
15279 /* Start with the row the cursor was displayed during the last
15280 not paused redisplay. Give up if that row is not valid. */
15281 if (w->last_cursor_vpos < 0
15282 || w->last_cursor_vpos >= w->current_matrix->nrows)
15283 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15284 else
15285 {
15286 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15287 if (row->mode_line_p)
15288 ++row;
15289 if (!row->enabled_p)
15290 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15291 }
15292
15293 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15294 {
15295 int scroll_p = 0, must_scroll = 0;
15296 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15297
15298 if (PT > w->last_point)
15299 {
15300 /* Point has moved forward. */
15301 while (MATRIX_ROW_END_CHARPOS (row) < PT
15302 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15303 {
15304 eassert (row->enabled_p);
15305 ++row;
15306 }
15307
15308 /* If the end position of a row equals the start
15309 position of the next row, and PT is at that position,
15310 we would rather display cursor in the next line. */
15311 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15312 && MATRIX_ROW_END_CHARPOS (row) == PT
15313 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15314 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15315 && !cursor_row_p (row))
15316 ++row;
15317
15318 /* If within the scroll margin, scroll. Note that
15319 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15320 the next line would be drawn, and that
15321 this_scroll_margin can be zero. */
15322 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15323 || PT > MATRIX_ROW_END_CHARPOS (row)
15324 /* Line is completely visible last line in window
15325 and PT is to be set in the next line. */
15326 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15327 && PT == MATRIX_ROW_END_CHARPOS (row)
15328 && !row->ends_at_zv_p
15329 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15330 scroll_p = 1;
15331 }
15332 else if (PT < w->last_point)
15333 {
15334 /* Cursor has to be moved backward. Note that PT >=
15335 CHARPOS (startp) because of the outer if-statement. */
15336 while (!row->mode_line_p
15337 && (MATRIX_ROW_START_CHARPOS (row) > PT
15338 || (MATRIX_ROW_START_CHARPOS (row) == PT
15339 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15340 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15341 row > w->current_matrix->rows
15342 && (row-1)->ends_in_newline_from_string_p))))
15343 && (row->y > top_scroll_margin
15344 || CHARPOS (startp) == BEGV))
15345 {
15346 eassert (row->enabled_p);
15347 --row;
15348 }
15349
15350 /* Consider the following case: Window starts at BEGV,
15351 there is invisible, intangible text at BEGV, so that
15352 display starts at some point START > BEGV. It can
15353 happen that we are called with PT somewhere between
15354 BEGV and START. Try to handle that case. */
15355 if (row < w->current_matrix->rows
15356 || row->mode_line_p)
15357 {
15358 row = w->current_matrix->rows;
15359 if (row->mode_line_p)
15360 ++row;
15361 }
15362
15363 /* Due to newlines in overlay strings, we may have to
15364 skip forward over overlay strings. */
15365 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15366 && MATRIX_ROW_END_CHARPOS (row) == PT
15367 && !cursor_row_p (row))
15368 ++row;
15369
15370 /* If within the scroll margin, scroll. */
15371 if (row->y < top_scroll_margin
15372 && CHARPOS (startp) != BEGV)
15373 scroll_p = 1;
15374 }
15375 else
15376 {
15377 /* Cursor did not move. So don't scroll even if cursor line
15378 is partially visible, as it was so before. */
15379 rc = CURSOR_MOVEMENT_SUCCESS;
15380 }
15381
15382 if (PT < MATRIX_ROW_START_CHARPOS (row)
15383 || PT > MATRIX_ROW_END_CHARPOS (row))
15384 {
15385 /* if PT is not in the glyph row, give up. */
15386 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15387 must_scroll = 1;
15388 }
15389 else if (rc != CURSOR_MOVEMENT_SUCCESS
15390 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15391 {
15392 struct glyph_row *row1;
15393
15394 /* If rows are bidi-reordered and point moved, back up
15395 until we find a row that does not belong to a
15396 continuation line. This is because we must consider
15397 all rows of a continued line as candidates for the
15398 new cursor positioning, since row start and end
15399 positions change non-linearly with vertical position
15400 in such rows. */
15401 /* FIXME: Revisit this when glyph ``spilling'' in
15402 continuation lines' rows is implemented for
15403 bidi-reordered rows. */
15404 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15405 MATRIX_ROW_CONTINUATION_LINE_P (row);
15406 --row)
15407 {
15408 /* If we hit the beginning of the displayed portion
15409 without finding the first row of a continued
15410 line, give up. */
15411 if (row <= row1)
15412 {
15413 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15414 break;
15415 }
15416 eassert (row->enabled_p);
15417 }
15418 }
15419 if (must_scroll)
15420 ;
15421 else if (rc != CURSOR_MOVEMENT_SUCCESS
15422 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15423 /* Make sure this isn't a header line by any chance, since
15424 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15425 && !row->mode_line_p
15426 && make_cursor_line_fully_visible_p)
15427 {
15428 if (PT == MATRIX_ROW_END_CHARPOS (row)
15429 && !row->ends_at_zv_p
15430 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15431 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15432 else if (row->height > window_box_height (w))
15433 {
15434 /* If we end up in a partially visible line, let's
15435 make it fully visible, except when it's taller
15436 than the window, in which case we can't do much
15437 about it. */
15438 *scroll_step = 1;
15439 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15440 }
15441 else
15442 {
15443 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15444 if (!cursor_row_fully_visible_p (w, 0, 1))
15445 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15446 else
15447 rc = CURSOR_MOVEMENT_SUCCESS;
15448 }
15449 }
15450 else if (scroll_p)
15451 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15452 else if (rc != CURSOR_MOVEMENT_SUCCESS
15453 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15454 {
15455 /* With bidi-reordered rows, there could be more than
15456 one candidate row whose start and end positions
15457 occlude point. We need to let set_cursor_from_row
15458 find the best candidate. */
15459 /* FIXME: Revisit this when glyph ``spilling'' in
15460 continuation lines' rows is implemented for
15461 bidi-reordered rows. */
15462 int rv = 0;
15463
15464 do
15465 {
15466 int at_zv_p = 0, exact_match_p = 0;
15467
15468 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15469 && PT <= MATRIX_ROW_END_CHARPOS (row)
15470 && cursor_row_p (row))
15471 rv |= set_cursor_from_row (w, row, w->current_matrix,
15472 0, 0, 0, 0);
15473 /* As soon as we've found the exact match for point,
15474 or the first suitable row whose ends_at_zv_p flag
15475 is set, we are done. */
15476 at_zv_p =
15477 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15478 if (rv && !at_zv_p
15479 && w->cursor.hpos >= 0
15480 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15481 w->cursor.vpos))
15482 {
15483 struct glyph_row *candidate =
15484 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15485 struct glyph *g =
15486 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15487 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15488
15489 exact_match_p =
15490 (BUFFERP (g->object) && g->charpos == PT)
15491 || (INTEGERP (g->object)
15492 && (g->charpos == PT
15493 || (g->charpos == 0 && endpos - 1 == PT)));
15494 }
15495 if (rv && (at_zv_p || exact_match_p))
15496 {
15497 rc = CURSOR_MOVEMENT_SUCCESS;
15498 break;
15499 }
15500 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15501 break;
15502 ++row;
15503 }
15504 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15505 || row->continued_p)
15506 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15507 || (MATRIX_ROW_START_CHARPOS (row) == PT
15508 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15509 /* If we didn't find any candidate rows, or exited the
15510 loop before all the candidates were examined, signal
15511 to the caller that this method failed. */
15512 if (rc != CURSOR_MOVEMENT_SUCCESS
15513 && !(rv
15514 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15515 && !row->continued_p))
15516 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15517 else if (rv)
15518 rc = CURSOR_MOVEMENT_SUCCESS;
15519 }
15520 else
15521 {
15522 do
15523 {
15524 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15525 {
15526 rc = CURSOR_MOVEMENT_SUCCESS;
15527 break;
15528 }
15529 ++row;
15530 }
15531 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15532 && MATRIX_ROW_START_CHARPOS (row) == PT
15533 && cursor_row_p (row));
15534 }
15535 }
15536 }
15537
15538 return rc;
15539 }
15540
15541 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15542 static
15543 #endif
15544 void
15545 set_vertical_scroll_bar (struct window *w)
15546 {
15547 ptrdiff_t start, end, whole;
15548
15549 /* Calculate the start and end positions for the current window.
15550 At some point, it would be nice to choose between scrollbars
15551 which reflect the whole buffer size, with special markers
15552 indicating narrowing, and scrollbars which reflect only the
15553 visible region.
15554
15555 Note that mini-buffers sometimes aren't displaying any text. */
15556 if (!MINI_WINDOW_P (w)
15557 || (w == XWINDOW (minibuf_window)
15558 && NILP (echo_area_buffer[0])))
15559 {
15560 struct buffer *buf = XBUFFER (w->contents);
15561 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15562 start = marker_position (w->start) - BUF_BEGV (buf);
15563 /* I don't think this is guaranteed to be right. For the
15564 moment, we'll pretend it is. */
15565 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15566
15567 if (end < start)
15568 end = start;
15569 if (whole < (end - start))
15570 whole = end - start;
15571 }
15572 else
15573 start = end = whole = 0;
15574
15575 /* Indicate what this scroll bar ought to be displaying now. */
15576 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15577 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15578 (w, end - start, whole, start);
15579 }
15580
15581
15582 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15583 selected_window is redisplayed.
15584
15585 We can return without actually redisplaying the window if fonts has been
15586 changed on window's frame. In that case, redisplay_internal will retry. */
15587
15588 static void
15589 redisplay_window (Lisp_Object window, bool just_this_one_p)
15590 {
15591 struct window *w = XWINDOW (window);
15592 struct frame *f = XFRAME (w->frame);
15593 struct buffer *buffer = XBUFFER (w->contents);
15594 struct buffer *old = current_buffer;
15595 struct text_pos lpoint, opoint, startp;
15596 int update_mode_line;
15597 int tem;
15598 struct it it;
15599 /* Record it now because it's overwritten. */
15600 bool current_matrix_up_to_date_p = false;
15601 bool used_current_matrix_p = false;
15602 /* This is less strict than current_matrix_up_to_date_p.
15603 It indicates that the buffer contents and narrowing are unchanged. */
15604 bool buffer_unchanged_p = false;
15605 int temp_scroll_step = 0;
15606 ptrdiff_t count = SPECPDL_INDEX ();
15607 int rc;
15608 int centering_position = -1;
15609 int last_line_misfit = 0;
15610 ptrdiff_t beg_unchanged, end_unchanged;
15611 int frame_line_height;
15612
15613 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15614 opoint = lpoint;
15615
15616 #ifdef GLYPH_DEBUG
15617 *w->desired_matrix->method = 0;
15618 #endif
15619
15620 if (!just_this_one_p
15621 && REDISPLAY_SOME_P ()
15622 && !w->redisplay
15623 && !f->redisplay
15624 && !buffer->text->redisplay
15625 && BUF_PT (buffer) == w->last_point)
15626 return;
15627
15628 /* Make sure that both W's markers are valid. */
15629 eassert (XMARKER (w->start)->buffer == buffer);
15630 eassert (XMARKER (w->pointm)->buffer == buffer);
15631
15632 restart:
15633 reconsider_clip_changes (w);
15634 frame_line_height = default_line_pixel_height (w);
15635
15636 /* Has the mode line to be updated? */
15637 update_mode_line = (w->update_mode_line
15638 || update_mode_lines
15639 || buffer->clip_changed
15640 || buffer->prevent_redisplay_optimizations_p);
15641
15642 if (!just_this_one_p)
15643 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15644 cleverly elsewhere. */
15645 w->must_be_updated_p = true;
15646
15647 if (MINI_WINDOW_P (w))
15648 {
15649 if (w == XWINDOW (echo_area_window)
15650 && !NILP (echo_area_buffer[0]))
15651 {
15652 if (update_mode_line)
15653 /* We may have to update a tty frame's menu bar or a
15654 tool-bar. Example `M-x C-h C-h C-g'. */
15655 goto finish_menu_bars;
15656 else
15657 /* We've already displayed the echo area glyphs in this window. */
15658 goto finish_scroll_bars;
15659 }
15660 else if ((w != XWINDOW (minibuf_window)
15661 || minibuf_level == 0)
15662 /* When buffer is nonempty, redisplay window normally. */
15663 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15664 /* Quail displays non-mini buffers in minibuffer window.
15665 In that case, redisplay the window normally. */
15666 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15667 {
15668 /* W is a mini-buffer window, but it's not active, so clear
15669 it. */
15670 int yb = window_text_bottom_y (w);
15671 struct glyph_row *row;
15672 int y;
15673
15674 for (y = 0, row = w->desired_matrix->rows;
15675 y < yb;
15676 y += row->height, ++row)
15677 blank_row (w, row, y);
15678 goto finish_scroll_bars;
15679 }
15680
15681 clear_glyph_matrix (w->desired_matrix);
15682 }
15683
15684 /* Otherwise set up data on this window; select its buffer and point
15685 value. */
15686 /* Really select the buffer, for the sake of buffer-local
15687 variables. */
15688 set_buffer_internal_1 (XBUFFER (w->contents));
15689
15690 current_matrix_up_to_date_p
15691 = (w->window_end_valid
15692 && !current_buffer->clip_changed
15693 && !current_buffer->prevent_redisplay_optimizations_p
15694 && !window_outdated (w));
15695
15696 /* Run the window-bottom-change-functions
15697 if it is possible that the text on the screen has changed
15698 (either due to modification of the text, or any other reason). */
15699 if (!current_matrix_up_to_date_p
15700 && !NILP (Vwindow_text_change_functions))
15701 {
15702 safe_run_hooks (Qwindow_text_change_functions);
15703 goto restart;
15704 }
15705
15706 beg_unchanged = BEG_UNCHANGED;
15707 end_unchanged = END_UNCHANGED;
15708
15709 SET_TEXT_POS (opoint, PT, PT_BYTE);
15710
15711 specbind (Qinhibit_point_motion_hooks, Qt);
15712
15713 buffer_unchanged_p
15714 = (w->window_end_valid
15715 && !current_buffer->clip_changed
15716 && !window_outdated (w));
15717
15718 /* When windows_or_buffers_changed is non-zero, we can't rely
15719 on the window end being valid, so set it to zero there. */
15720 if (windows_or_buffers_changed)
15721 {
15722 /* If window starts on a continuation line, maybe adjust the
15723 window start in case the window's width changed. */
15724 if (XMARKER (w->start)->buffer == current_buffer)
15725 compute_window_start_on_continuation_line (w);
15726
15727 w->window_end_valid = false;
15728 /* If so, we also can't rely on current matrix
15729 and should not fool try_cursor_movement below. */
15730 current_matrix_up_to_date_p = false;
15731 }
15732
15733 /* Some sanity checks. */
15734 CHECK_WINDOW_END (w);
15735 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15736 emacs_abort ();
15737 if (BYTEPOS (opoint) < CHARPOS (opoint))
15738 emacs_abort ();
15739
15740 if (mode_line_update_needed (w))
15741 update_mode_line = 1;
15742
15743 /* Point refers normally to the selected window. For any other
15744 window, set up appropriate value. */
15745 if (!EQ (window, selected_window))
15746 {
15747 ptrdiff_t new_pt = marker_position (w->pointm);
15748 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15749 if (new_pt < BEGV)
15750 {
15751 new_pt = BEGV;
15752 new_pt_byte = BEGV_BYTE;
15753 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15754 }
15755 else if (new_pt > (ZV - 1))
15756 {
15757 new_pt = ZV;
15758 new_pt_byte = ZV_BYTE;
15759 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15760 }
15761
15762 /* We don't use SET_PT so that the point-motion hooks don't run. */
15763 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15764 }
15765
15766 /* If any of the character widths specified in the display table
15767 have changed, invalidate the width run cache. It's true that
15768 this may be a bit late to catch such changes, but the rest of
15769 redisplay goes (non-fatally) haywire when the display table is
15770 changed, so why should we worry about doing any better? */
15771 if (current_buffer->width_run_cache
15772 || (current_buffer->base_buffer
15773 && current_buffer->base_buffer->width_run_cache))
15774 {
15775 struct Lisp_Char_Table *disptab = buffer_display_table ();
15776
15777 if (! disptab_matches_widthtab
15778 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15779 {
15780 struct buffer *buf = current_buffer;
15781
15782 if (buf->base_buffer)
15783 buf = buf->base_buffer;
15784 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15785 recompute_width_table (current_buffer, disptab);
15786 }
15787 }
15788
15789 /* If window-start is screwed up, choose a new one. */
15790 if (XMARKER (w->start)->buffer != current_buffer)
15791 goto recenter;
15792
15793 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15794
15795 /* If someone specified a new starting point but did not insist,
15796 check whether it can be used. */
15797 if (w->optional_new_start
15798 && CHARPOS (startp) >= BEGV
15799 && CHARPOS (startp) <= ZV)
15800 {
15801 w->optional_new_start = 0;
15802 start_display (&it, w, startp);
15803 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15804 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15805 if (IT_CHARPOS (it) == PT)
15806 w->force_start = 1;
15807 /* IT may overshoot PT if text at PT is invisible. */
15808 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15809 w->force_start = 1;
15810 }
15811
15812 force_start:
15813
15814 /* Handle case where place to start displaying has been specified,
15815 unless the specified location is outside the accessible range. */
15816 if (w->force_start || window_frozen_p (w))
15817 {
15818 /* We set this later on if we have to adjust point. */
15819 int new_vpos = -1;
15820
15821 w->force_start = 0;
15822 w->vscroll = 0;
15823 w->window_end_valid = 0;
15824
15825 /* Forget any recorded base line for line number display. */
15826 if (!buffer_unchanged_p)
15827 w->base_line_number = 0;
15828
15829 /* Redisplay the mode line. Select the buffer properly for that.
15830 Also, run the hook window-scroll-functions
15831 because we have scrolled. */
15832 /* Note, we do this after clearing force_start because
15833 if there's an error, it is better to forget about force_start
15834 than to get into an infinite loop calling the hook functions
15835 and having them get more errors. */
15836 if (!update_mode_line
15837 || ! NILP (Vwindow_scroll_functions))
15838 {
15839 update_mode_line = 1;
15840 w->update_mode_line = 1;
15841 startp = run_window_scroll_functions (window, startp);
15842 }
15843
15844 if (CHARPOS (startp) < BEGV)
15845 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15846 else if (CHARPOS (startp) > ZV)
15847 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15848
15849 /* Redisplay, then check if cursor has been set during the
15850 redisplay. Give up if new fonts were loaded. */
15851 /* We used to issue a CHECK_MARGINS argument to try_window here,
15852 but this causes scrolling to fail when point begins inside
15853 the scroll margin (bug#148) -- cyd */
15854 if (!try_window (window, startp, 0))
15855 {
15856 w->force_start = 1;
15857 clear_glyph_matrix (w->desired_matrix);
15858 goto need_larger_matrices;
15859 }
15860
15861 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15862 {
15863 /* If point does not appear, try to move point so it does
15864 appear. The desired matrix has been built above, so we
15865 can use it here. */
15866 new_vpos = window_box_height (w) / 2;
15867 }
15868
15869 if (!cursor_row_fully_visible_p (w, 0, 0))
15870 {
15871 /* Point does appear, but on a line partly visible at end of window.
15872 Move it back to a fully-visible line. */
15873 new_vpos = window_box_height (w);
15874 }
15875 else if (w->cursor.vpos >= 0)
15876 {
15877 /* Some people insist on not letting point enter the scroll
15878 margin, even though this part handles windows that didn't
15879 scroll at all. */
15880 int window_total_lines
15881 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15882 int margin = min (scroll_margin, window_total_lines / 4);
15883 int pixel_margin = margin * frame_line_height;
15884 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15885
15886 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15887 below, which finds the row to move point to, advances by
15888 the Y coordinate of the _next_ row, see the definition of
15889 MATRIX_ROW_BOTTOM_Y. */
15890 if (w->cursor.vpos < margin + header_line)
15891 {
15892 w->cursor.vpos = -1;
15893 clear_glyph_matrix (w->desired_matrix);
15894 goto try_to_scroll;
15895 }
15896 else
15897 {
15898 int window_height = window_box_height (w);
15899
15900 if (header_line)
15901 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15902 if (w->cursor.y >= window_height - pixel_margin)
15903 {
15904 w->cursor.vpos = -1;
15905 clear_glyph_matrix (w->desired_matrix);
15906 goto try_to_scroll;
15907 }
15908 }
15909 }
15910
15911 /* If we need to move point for either of the above reasons,
15912 now actually do it. */
15913 if (new_vpos >= 0)
15914 {
15915 struct glyph_row *row;
15916
15917 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15918 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15919 ++row;
15920
15921 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15922 MATRIX_ROW_START_BYTEPOS (row));
15923
15924 if (w != XWINDOW (selected_window))
15925 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15926 else if (current_buffer == old)
15927 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15928
15929 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15930
15931 /* If we are highlighting the region, then we just changed
15932 the region, so redisplay to show it. */
15933 /* FIXME: We need to (re)run pre-redisplay-function! */
15934 /* if (markpos_of_region () >= 0)
15935 {
15936 clear_glyph_matrix (w->desired_matrix);
15937 if (!try_window (window, startp, 0))
15938 goto need_larger_matrices;
15939 }
15940 */
15941 }
15942
15943 #ifdef GLYPH_DEBUG
15944 debug_method_add (w, "forced window start");
15945 #endif
15946 goto done;
15947 }
15948
15949 /* Handle case where text has not changed, only point, and it has
15950 not moved off the frame, and we are not retrying after hscroll.
15951 (current_matrix_up_to_date_p is nonzero when retrying.) */
15952 if (current_matrix_up_to_date_p
15953 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15954 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15955 {
15956 switch (rc)
15957 {
15958 case CURSOR_MOVEMENT_SUCCESS:
15959 used_current_matrix_p = 1;
15960 goto done;
15961
15962 case CURSOR_MOVEMENT_MUST_SCROLL:
15963 goto try_to_scroll;
15964
15965 default:
15966 emacs_abort ();
15967 }
15968 }
15969 /* If current starting point was originally the beginning of a line
15970 but no longer is, find a new starting point. */
15971 else if (w->start_at_line_beg
15972 && !(CHARPOS (startp) <= BEGV
15973 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15974 {
15975 #ifdef GLYPH_DEBUG
15976 debug_method_add (w, "recenter 1");
15977 #endif
15978 goto recenter;
15979 }
15980
15981 /* Try scrolling with try_window_id. Value is > 0 if update has
15982 been done, it is -1 if we know that the same window start will
15983 not work. It is 0 if unsuccessful for some other reason. */
15984 else if ((tem = try_window_id (w)) != 0)
15985 {
15986 #ifdef GLYPH_DEBUG
15987 debug_method_add (w, "try_window_id %d", tem);
15988 #endif
15989
15990 if (f->fonts_changed)
15991 goto need_larger_matrices;
15992 if (tem > 0)
15993 goto done;
15994
15995 /* Otherwise try_window_id has returned -1 which means that we
15996 don't want the alternative below this comment to execute. */
15997 }
15998 else if (CHARPOS (startp) >= BEGV
15999 && CHARPOS (startp) <= ZV
16000 && PT >= CHARPOS (startp)
16001 && (CHARPOS (startp) < ZV
16002 /* Avoid starting at end of buffer. */
16003 || CHARPOS (startp) == BEGV
16004 || !window_outdated (w)))
16005 {
16006 int d1, d2, d3, d4, d5, d6;
16007
16008 /* If first window line is a continuation line, and window start
16009 is inside the modified region, but the first change is before
16010 current window start, we must select a new window start.
16011
16012 However, if this is the result of a down-mouse event (e.g. by
16013 extending the mouse-drag-overlay), we don't want to select a
16014 new window start, since that would change the position under
16015 the mouse, resulting in an unwanted mouse-movement rather
16016 than a simple mouse-click. */
16017 if (!w->start_at_line_beg
16018 && NILP (do_mouse_tracking)
16019 && CHARPOS (startp) > BEGV
16020 && CHARPOS (startp) > BEG + beg_unchanged
16021 && CHARPOS (startp) <= Z - end_unchanged
16022 /* Even if w->start_at_line_beg is nil, a new window may
16023 start at a line_beg, since that's how set_buffer_window
16024 sets it. So, we need to check the return value of
16025 compute_window_start_on_continuation_line. (See also
16026 bug#197). */
16027 && XMARKER (w->start)->buffer == current_buffer
16028 && compute_window_start_on_continuation_line (w)
16029 /* It doesn't make sense to force the window start like we
16030 do at label force_start if it is already known that point
16031 will not be visible in the resulting window, because
16032 doing so will move point from its correct position
16033 instead of scrolling the window to bring point into view.
16034 See bug#9324. */
16035 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16036 {
16037 w->force_start = 1;
16038 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16039 goto force_start;
16040 }
16041
16042 #ifdef GLYPH_DEBUG
16043 debug_method_add (w, "same window start");
16044 #endif
16045
16046 /* Try to redisplay starting at same place as before.
16047 If point has not moved off frame, accept the results. */
16048 if (!current_matrix_up_to_date_p
16049 /* Don't use try_window_reusing_current_matrix in this case
16050 because a window scroll function can have changed the
16051 buffer. */
16052 || !NILP (Vwindow_scroll_functions)
16053 || MINI_WINDOW_P (w)
16054 || !(used_current_matrix_p
16055 = try_window_reusing_current_matrix (w)))
16056 {
16057 IF_DEBUG (debug_method_add (w, "1"));
16058 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16059 /* -1 means we need to scroll.
16060 0 means we need new matrices, but fonts_changed
16061 is set in that case, so we will detect it below. */
16062 goto try_to_scroll;
16063 }
16064
16065 if (f->fonts_changed)
16066 goto need_larger_matrices;
16067
16068 if (w->cursor.vpos >= 0)
16069 {
16070 if (!just_this_one_p
16071 || current_buffer->clip_changed
16072 || BEG_UNCHANGED < CHARPOS (startp))
16073 /* Forget any recorded base line for line number display. */
16074 w->base_line_number = 0;
16075
16076 if (!cursor_row_fully_visible_p (w, 1, 0))
16077 {
16078 clear_glyph_matrix (w->desired_matrix);
16079 last_line_misfit = 1;
16080 }
16081 /* Drop through and scroll. */
16082 else
16083 goto done;
16084 }
16085 else
16086 clear_glyph_matrix (w->desired_matrix);
16087 }
16088
16089 try_to_scroll:
16090
16091 /* Redisplay the mode line. Select the buffer properly for that. */
16092 if (!update_mode_line)
16093 {
16094 update_mode_line = 1;
16095 w->update_mode_line = 1;
16096 }
16097
16098 /* Try to scroll by specified few lines. */
16099 if ((scroll_conservatively
16100 || emacs_scroll_step
16101 || temp_scroll_step
16102 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16103 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16104 && CHARPOS (startp) >= BEGV
16105 && CHARPOS (startp) <= ZV)
16106 {
16107 /* The function returns -1 if new fonts were loaded, 1 if
16108 successful, 0 if not successful. */
16109 int ss = try_scrolling (window, just_this_one_p,
16110 scroll_conservatively,
16111 emacs_scroll_step,
16112 temp_scroll_step, last_line_misfit);
16113 switch (ss)
16114 {
16115 case SCROLLING_SUCCESS:
16116 goto done;
16117
16118 case SCROLLING_NEED_LARGER_MATRICES:
16119 goto need_larger_matrices;
16120
16121 case SCROLLING_FAILED:
16122 break;
16123
16124 default:
16125 emacs_abort ();
16126 }
16127 }
16128
16129 /* Finally, just choose a place to start which positions point
16130 according to user preferences. */
16131
16132 recenter:
16133
16134 #ifdef GLYPH_DEBUG
16135 debug_method_add (w, "recenter");
16136 #endif
16137
16138 /* Forget any previously recorded base line for line number display. */
16139 if (!buffer_unchanged_p)
16140 w->base_line_number = 0;
16141
16142 /* Determine the window start relative to point. */
16143 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16144 it.current_y = it.last_visible_y;
16145 if (centering_position < 0)
16146 {
16147 int window_total_lines
16148 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16149 int margin =
16150 scroll_margin > 0
16151 ? min (scroll_margin, window_total_lines / 4)
16152 : 0;
16153 ptrdiff_t margin_pos = CHARPOS (startp);
16154 Lisp_Object aggressive;
16155 int scrolling_up;
16156
16157 /* If there is a scroll margin at the top of the window, find
16158 its character position. */
16159 if (margin
16160 /* Cannot call start_display if startp is not in the
16161 accessible region of the buffer. This can happen when we
16162 have just switched to a different buffer and/or changed
16163 its restriction. In that case, startp is initialized to
16164 the character position 1 (BEGV) because we did not yet
16165 have chance to display the buffer even once. */
16166 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16167 {
16168 struct it it1;
16169 void *it1data = NULL;
16170
16171 SAVE_IT (it1, it, it1data);
16172 start_display (&it1, w, startp);
16173 move_it_vertically (&it1, margin * frame_line_height);
16174 margin_pos = IT_CHARPOS (it1);
16175 RESTORE_IT (&it, &it, it1data);
16176 }
16177 scrolling_up = PT > margin_pos;
16178 aggressive =
16179 scrolling_up
16180 ? BVAR (current_buffer, scroll_up_aggressively)
16181 : BVAR (current_buffer, scroll_down_aggressively);
16182
16183 if (!MINI_WINDOW_P (w)
16184 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16185 {
16186 int pt_offset = 0;
16187
16188 /* Setting scroll-conservatively overrides
16189 scroll-*-aggressively. */
16190 if (!scroll_conservatively && NUMBERP (aggressive))
16191 {
16192 double float_amount = XFLOATINT (aggressive);
16193
16194 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16195 if (pt_offset == 0 && float_amount > 0)
16196 pt_offset = 1;
16197 if (pt_offset && margin > 0)
16198 margin -= 1;
16199 }
16200 /* Compute how much to move the window start backward from
16201 point so that point will be displayed where the user
16202 wants it. */
16203 if (scrolling_up)
16204 {
16205 centering_position = it.last_visible_y;
16206 if (pt_offset)
16207 centering_position -= pt_offset;
16208 centering_position -=
16209 frame_line_height * (1 + margin + (last_line_misfit != 0))
16210 + WINDOW_HEADER_LINE_HEIGHT (w);
16211 /* Don't let point enter the scroll margin near top of
16212 the window. */
16213 if (centering_position < margin * frame_line_height)
16214 centering_position = margin * frame_line_height;
16215 }
16216 else
16217 centering_position = margin * frame_line_height + pt_offset;
16218 }
16219 else
16220 /* Set the window start half the height of the window backward
16221 from point. */
16222 centering_position = window_box_height (w) / 2;
16223 }
16224 move_it_vertically_backward (&it, centering_position);
16225
16226 eassert (IT_CHARPOS (it) >= BEGV);
16227
16228 /* The function move_it_vertically_backward may move over more
16229 than the specified y-distance. If it->w is small, e.g. a
16230 mini-buffer window, we may end up in front of the window's
16231 display area. Start displaying at the start of the line
16232 containing PT in this case. */
16233 if (it.current_y <= 0)
16234 {
16235 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16236 move_it_vertically_backward (&it, 0);
16237 it.current_y = 0;
16238 }
16239
16240 it.current_x = it.hpos = 0;
16241
16242 /* Set the window start position here explicitly, to avoid an
16243 infinite loop in case the functions in window-scroll-functions
16244 get errors. */
16245 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16246
16247 /* Run scroll hooks. */
16248 startp = run_window_scroll_functions (window, it.current.pos);
16249
16250 /* Redisplay the window. */
16251 if (!current_matrix_up_to_date_p
16252 || windows_or_buffers_changed
16253 || f->cursor_type_changed
16254 /* Don't use try_window_reusing_current_matrix in this case
16255 because it can have changed the buffer. */
16256 || !NILP (Vwindow_scroll_functions)
16257 || !just_this_one_p
16258 || MINI_WINDOW_P (w)
16259 || !(used_current_matrix_p
16260 = try_window_reusing_current_matrix (w)))
16261 try_window (window, startp, 0);
16262
16263 /* If new fonts have been loaded (due to fontsets), give up. We
16264 have to start a new redisplay since we need to re-adjust glyph
16265 matrices. */
16266 if (f->fonts_changed)
16267 goto need_larger_matrices;
16268
16269 /* If cursor did not appear assume that the middle of the window is
16270 in the first line of the window. Do it again with the next line.
16271 (Imagine a window of height 100, displaying two lines of height
16272 60. Moving back 50 from it->last_visible_y will end in the first
16273 line.) */
16274 if (w->cursor.vpos < 0)
16275 {
16276 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16277 {
16278 clear_glyph_matrix (w->desired_matrix);
16279 move_it_by_lines (&it, 1);
16280 try_window (window, it.current.pos, 0);
16281 }
16282 else if (PT < IT_CHARPOS (it))
16283 {
16284 clear_glyph_matrix (w->desired_matrix);
16285 move_it_by_lines (&it, -1);
16286 try_window (window, it.current.pos, 0);
16287 }
16288 else
16289 {
16290 /* Not much we can do about it. */
16291 }
16292 }
16293
16294 /* Consider the following case: Window starts at BEGV, there is
16295 invisible, intangible text at BEGV, so that display starts at
16296 some point START > BEGV. It can happen that we are called with
16297 PT somewhere between BEGV and START. Try to handle that case. */
16298 if (w->cursor.vpos < 0)
16299 {
16300 struct glyph_row *row = w->current_matrix->rows;
16301 if (row->mode_line_p)
16302 ++row;
16303 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16304 }
16305
16306 if (!cursor_row_fully_visible_p (w, 0, 0))
16307 {
16308 /* If vscroll is enabled, disable it and try again. */
16309 if (w->vscroll)
16310 {
16311 w->vscroll = 0;
16312 clear_glyph_matrix (w->desired_matrix);
16313 goto recenter;
16314 }
16315
16316 /* Users who set scroll-conservatively to a large number want
16317 point just above/below the scroll margin. If we ended up
16318 with point's row partially visible, move the window start to
16319 make that row fully visible and out of the margin. */
16320 if (scroll_conservatively > SCROLL_LIMIT)
16321 {
16322 int window_total_lines
16323 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16324 int margin =
16325 scroll_margin > 0
16326 ? min (scroll_margin, window_total_lines / 4)
16327 : 0;
16328 int move_down = w->cursor.vpos >= window_total_lines / 2;
16329
16330 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16331 clear_glyph_matrix (w->desired_matrix);
16332 if (1 == try_window (window, it.current.pos,
16333 TRY_WINDOW_CHECK_MARGINS))
16334 goto done;
16335 }
16336
16337 /* If centering point failed to make the whole line visible,
16338 put point at the top instead. That has to make the whole line
16339 visible, if it can be done. */
16340 if (centering_position == 0)
16341 goto done;
16342
16343 clear_glyph_matrix (w->desired_matrix);
16344 centering_position = 0;
16345 goto recenter;
16346 }
16347
16348 done:
16349
16350 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16351 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16352 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16353
16354 /* Display the mode line, if we must. */
16355 if ((update_mode_line
16356 /* If window not full width, must redo its mode line
16357 if (a) the window to its side is being redone and
16358 (b) we do a frame-based redisplay. This is a consequence
16359 of how inverted lines are drawn in frame-based redisplay. */
16360 || (!just_this_one_p
16361 && !FRAME_WINDOW_P (f)
16362 && !WINDOW_FULL_WIDTH_P (w))
16363 /* Line number to display. */
16364 || w->base_line_pos > 0
16365 /* Column number is displayed and different from the one displayed. */
16366 || (w->column_number_displayed != -1
16367 && (w->column_number_displayed != current_column ())))
16368 /* This means that the window has a mode line. */
16369 && (WINDOW_WANTS_MODELINE_P (w)
16370 || WINDOW_WANTS_HEADER_LINE_P (w)))
16371 {
16372
16373 display_mode_lines (w);
16374
16375 /* If mode line height has changed, arrange for a thorough
16376 immediate redisplay using the correct mode line height. */
16377 if (WINDOW_WANTS_MODELINE_P (w)
16378 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16379 {
16380 f->fonts_changed = 1;
16381 w->mode_line_height = -1;
16382 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16383 = DESIRED_MODE_LINE_HEIGHT (w);
16384 }
16385
16386 /* If header line height has changed, arrange for a thorough
16387 immediate redisplay using the correct header line height. */
16388 if (WINDOW_WANTS_HEADER_LINE_P (w)
16389 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16390 {
16391 f->fonts_changed = 1;
16392 w->header_line_height = -1;
16393 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16394 = DESIRED_HEADER_LINE_HEIGHT (w);
16395 }
16396
16397 if (f->fonts_changed)
16398 goto need_larger_matrices;
16399 }
16400
16401 if (!line_number_displayed && w->base_line_pos != -1)
16402 {
16403 w->base_line_pos = 0;
16404 w->base_line_number = 0;
16405 }
16406
16407 finish_menu_bars:
16408
16409 /* When we reach a frame's selected window, redo the frame's menu bar. */
16410 if (update_mode_line
16411 && EQ (FRAME_SELECTED_WINDOW (f), window))
16412 {
16413 int redisplay_menu_p = 0;
16414
16415 if (FRAME_WINDOW_P (f))
16416 {
16417 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16418 || defined (HAVE_NS) || defined (USE_GTK)
16419 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16420 #else
16421 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16422 #endif
16423 }
16424 else
16425 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16426
16427 if (redisplay_menu_p)
16428 display_menu_bar (w);
16429
16430 #ifdef HAVE_WINDOW_SYSTEM
16431 if (FRAME_WINDOW_P (f))
16432 {
16433 #if defined (USE_GTK) || defined (HAVE_NS)
16434 if (FRAME_EXTERNAL_TOOL_BAR (f))
16435 redisplay_tool_bar (f);
16436 #else
16437 if (WINDOWP (f->tool_bar_window)
16438 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16439 || !NILP (Vauto_resize_tool_bars))
16440 && redisplay_tool_bar (f))
16441 ignore_mouse_drag_p = 1;
16442 #endif
16443 }
16444 #endif
16445 }
16446
16447 #ifdef HAVE_WINDOW_SYSTEM
16448 if (FRAME_WINDOW_P (f)
16449 && update_window_fringes (w, (just_this_one_p
16450 || (!used_current_matrix_p && !overlay_arrow_seen)
16451 || w->pseudo_window_p)))
16452 {
16453 update_begin (f);
16454 block_input ();
16455 if (draw_window_fringes (w, 1))
16456 {
16457 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16458 x_draw_right_divider (w);
16459 else
16460 x_draw_vertical_border (w);
16461 }
16462 unblock_input ();
16463 update_end (f);
16464 }
16465
16466 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16467 x_draw_bottom_divider (w);
16468 #endif /* HAVE_WINDOW_SYSTEM */
16469
16470 /* We go to this label, with fonts_changed set, if it is
16471 necessary to try again using larger glyph matrices.
16472 We have to redeem the scroll bar even in this case,
16473 because the loop in redisplay_internal expects that. */
16474 need_larger_matrices:
16475 ;
16476 finish_scroll_bars:
16477
16478 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16479 {
16480 /* Set the thumb's position and size. */
16481 set_vertical_scroll_bar (w);
16482
16483 /* Note that we actually used the scroll bar attached to this
16484 window, so it shouldn't be deleted at the end of redisplay. */
16485 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16486 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16487 }
16488
16489 /* Restore current_buffer and value of point in it. The window
16490 update may have changed the buffer, so first make sure `opoint'
16491 is still valid (Bug#6177). */
16492 if (CHARPOS (opoint) < BEGV)
16493 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16494 else if (CHARPOS (opoint) > ZV)
16495 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16496 else
16497 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16498
16499 set_buffer_internal_1 (old);
16500 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16501 shorter. This can be caused by log truncation in *Messages*. */
16502 if (CHARPOS (lpoint) <= ZV)
16503 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16504
16505 unbind_to (count, Qnil);
16506 }
16507
16508
16509 /* Build the complete desired matrix of WINDOW with a window start
16510 buffer position POS.
16511
16512 Value is 1 if successful. It is zero if fonts were loaded during
16513 redisplay which makes re-adjusting glyph matrices necessary, and -1
16514 if point would appear in the scroll margins.
16515 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16516 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16517 set in FLAGS.) */
16518
16519 int
16520 try_window (Lisp_Object window, struct text_pos pos, int flags)
16521 {
16522 struct window *w = XWINDOW (window);
16523 struct it it;
16524 struct glyph_row *last_text_row = NULL;
16525 struct frame *f = XFRAME (w->frame);
16526 int frame_line_height = default_line_pixel_height (w);
16527
16528 /* Make POS the new window start. */
16529 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16530
16531 /* Mark cursor position as unknown. No overlay arrow seen. */
16532 w->cursor.vpos = -1;
16533 overlay_arrow_seen = 0;
16534
16535 /* Initialize iterator and info to start at POS. */
16536 start_display (&it, w, pos);
16537
16538 /* Display all lines of W. */
16539 while (it.current_y < it.last_visible_y)
16540 {
16541 if (display_line (&it))
16542 last_text_row = it.glyph_row - 1;
16543 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16544 return 0;
16545 }
16546
16547 /* Don't let the cursor end in the scroll margins. */
16548 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16549 && !MINI_WINDOW_P (w))
16550 {
16551 int this_scroll_margin;
16552 int window_total_lines
16553 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16554
16555 if (scroll_margin > 0)
16556 {
16557 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16558 this_scroll_margin *= frame_line_height;
16559 }
16560 else
16561 this_scroll_margin = 0;
16562
16563 if ((w->cursor.y >= 0 /* not vscrolled */
16564 && w->cursor.y < this_scroll_margin
16565 && CHARPOS (pos) > BEGV
16566 && IT_CHARPOS (it) < ZV)
16567 /* rms: considering make_cursor_line_fully_visible_p here
16568 seems to give wrong results. We don't want to recenter
16569 when the last line is partly visible, we want to allow
16570 that case to be handled in the usual way. */
16571 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16572 {
16573 w->cursor.vpos = -1;
16574 clear_glyph_matrix (w->desired_matrix);
16575 return -1;
16576 }
16577 }
16578
16579 /* If bottom moved off end of frame, change mode line percentage. */
16580 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16581 w->update_mode_line = 1;
16582
16583 /* Set window_end_pos to the offset of the last character displayed
16584 on the window from the end of current_buffer. Set
16585 window_end_vpos to its row number. */
16586 if (last_text_row)
16587 {
16588 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16589 adjust_window_ends (w, last_text_row, 0);
16590 eassert
16591 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16592 w->window_end_vpos)));
16593 }
16594 else
16595 {
16596 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16597 w->window_end_pos = Z - ZV;
16598 w->window_end_vpos = 0;
16599 }
16600
16601 /* But that is not valid info until redisplay finishes. */
16602 w->window_end_valid = 0;
16603 return 1;
16604 }
16605
16606
16607 \f
16608 /************************************************************************
16609 Window redisplay reusing current matrix when buffer has not changed
16610 ************************************************************************/
16611
16612 /* Try redisplay of window W showing an unchanged buffer with a
16613 different window start than the last time it was displayed by
16614 reusing its current matrix. Value is non-zero if successful.
16615 W->start is the new window start. */
16616
16617 static int
16618 try_window_reusing_current_matrix (struct window *w)
16619 {
16620 struct frame *f = XFRAME (w->frame);
16621 struct glyph_row *bottom_row;
16622 struct it it;
16623 struct run run;
16624 struct text_pos start, new_start;
16625 int nrows_scrolled, i;
16626 struct glyph_row *last_text_row;
16627 struct glyph_row *last_reused_text_row;
16628 struct glyph_row *start_row;
16629 int start_vpos, min_y, max_y;
16630
16631 #ifdef GLYPH_DEBUG
16632 if (inhibit_try_window_reusing)
16633 return 0;
16634 #endif
16635
16636 if (/* This function doesn't handle terminal frames. */
16637 !FRAME_WINDOW_P (f)
16638 /* Don't try to reuse the display if windows have been split
16639 or such. */
16640 || windows_or_buffers_changed
16641 || f->cursor_type_changed)
16642 return 0;
16643
16644 /* Can't do this if showing trailing whitespace. */
16645 if (!NILP (Vshow_trailing_whitespace))
16646 return 0;
16647
16648 /* If top-line visibility has changed, give up. */
16649 if (WINDOW_WANTS_HEADER_LINE_P (w)
16650 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16651 return 0;
16652
16653 /* Give up if old or new display is scrolled vertically. We could
16654 make this function handle this, but right now it doesn't. */
16655 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16656 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16657 return 0;
16658
16659 /* The variable new_start now holds the new window start. The old
16660 start `start' can be determined from the current matrix. */
16661 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16662 start = start_row->minpos;
16663 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16664
16665 /* Clear the desired matrix for the display below. */
16666 clear_glyph_matrix (w->desired_matrix);
16667
16668 if (CHARPOS (new_start) <= CHARPOS (start))
16669 {
16670 /* Don't use this method if the display starts with an ellipsis
16671 displayed for invisible text. It's not easy to handle that case
16672 below, and it's certainly not worth the effort since this is
16673 not a frequent case. */
16674 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16675 return 0;
16676
16677 IF_DEBUG (debug_method_add (w, "twu1"));
16678
16679 /* Display up to a row that can be reused. The variable
16680 last_text_row is set to the last row displayed that displays
16681 text. Note that it.vpos == 0 if or if not there is a
16682 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16683 start_display (&it, w, new_start);
16684 w->cursor.vpos = -1;
16685 last_text_row = last_reused_text_row = NULL;
16686
16687 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16688 {
16689 /* If we have reached into the characters in the START row,
16690 that means the line boundaries have changed. So we
16691 can't start copying with the row START. Maybe it will
16692 work to start copying with the following row. */
16693 while (IT_CHARPOS (it) > CHARPOS (start))
16694 {
16695 /* Advance to the next row as the "start". */
16696 start_row++;
16697 start = start_row->minpos;
16698 /* If there are no more rows to try, or just one, give up. */
16699 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16700 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16701 || CHARPOS (start) == ZV)
16702 {
16703 clear_glyph_matrix (w->desired_matrix);
16704 return 0;
16705 }
16706
16707 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16708 }
16709 /* If we have reached alignment, we can copy the rest of the
16710 rows. */
16711 if (IT_CHARPOS (it) == CHARPOS (start)
16712 /* Don't accept "alignment" inside a display vector,
16713 since start_row could have started in the middle of
16714 that same display vector (thus their character
16715 positions match), and we have no way of telling if
16716 that is the case. */
16717 && it.current.dpvec_index < 0)
16718 break;
16719
16720 if (display_line (&it))
16721 last_text_row = it.glyph_row - 1;
16722
16723 }
16724
16725 /* A value of current_y < last_visible_y means that we stopped
16726 at the previous window start, which in turn means that we
16727 have at least one reusable row. */
16728 if (it.current_y < it.last_visible_y)
16729 {
16730 struct glyph_row *row;
16731
16732 /* IT.vpos always starts from 0; it counts text lines. */
16733 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16734
16735 /* Find PT if not already found in the lines displayed. */
16736 if (w->cursor.vpos < 0)
16737 {
16738 int dy = it.current_y - start_row->y;
16739
16740 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16741 row = row_containing_pos (w, PT, row, NULL, dy);
16742 if (row)
16743 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16744 dy, nrows_scrolled);
16745 else
16746 {
16747 clear_glyph_matrix (w->desired_matrix);
16748 return 0;
16749 }
16750 }
16751
16752 /* Scroll the display. Do it before the current matrix is
16753 changed. The problem here is that update has not yet
16754 run, i.e. part of the current matrix is not up to date.
16755 scroll_run_hook will clear the cursor, and use the
16756 current matrix to get the height of the row the cursor is
16757 in. */
16758 run.current_y = start_row->y;
16759 run.desired_y = it.current_y;
16760 run.height = it.last_visible_y - it.current_y;
16761
16762 if (run.height > 0 && run.current_y != run.desired_y)
16763 {
16764 update_begin (f);
16765 FRAME_RIF (f)->update_window_begin_hook (w);
16766 FRAME_RIF (f)->clear_window_mouse_face (w);
16767 FRAME_RIF (f)->scroll_run_hook (w, &run);
16768 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16769 update_end (f);
16770 }
16771
16772 /* Shift current matrix down by nrows_scrolled lines. */
16773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16774 rotate_matrix (w->current_matrix,
16775 start_vpos,
16776 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16777 nrows_scrolled);
16778
16779 /* Disable lines that must be updated. */
16780 for (i = 0; i < nrows_scrolled; ++i)
16781 (start_row + i)->enabled_p = false;
16782
16783 /* Re-compute Y positions. */
16784 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16785 max_y = it.last_visible_y;
16786 for (row = start_row + nrows_scrolled;
16787 row < bottom_row;
16788 ++row)
16789 {
16790 row->y = it.current_y;
16791 row->visible_height = row->height;
16792
16793 if (row->y < min_y)
16794 row->visible_height -= min_y - row->y;
16795 if (row->y + row->height > max_y)
16796 row->visible_height -= row->y + row->height - max_y;
16797 if (row->fringe_bitmap_periodic_p)
16798 row->redraw_fringe_bitmaps_p = 1;
16799
16800 it.current_y += row->height;
16801
16802 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16803 last_reused_text_row = row;
16804 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16805 break;
16806 }
16807
16808 /* Disable lines in the current matrix which are now
16809 below the window. */
16810 for (++row; row < bottom_row; ++row)
16811 row->enabled_p = row->mode_line_p = 0;
16812 }
16813
16814 /* Update window_end_pos etc.; last_reused_text_row is the last
16815 reused row from the current matrix containing text, if any.
16816 The value of last_text_row is the last displayed line
16817 containing text. */
16818 if (last_reused_text_row)
16819 adjust_window_ends (w, last_reused_text_row, 1);
16820 else if (last_text_row)
16821 adjust_window_ends (w, last_text_row, 0);
16822 else
16823 {
16824 /* This window must be completely empty. */
16825 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16826 w->window_end_pos = Z - ZV;
16827 w->window_end_vpos = 0;
16828 }
16829 w->window_end_valid = 0;
16830
16831 /* Update hint: don't try scrolling again in update_window. */
16832 w->desired_matrix->no_scrolling_p = 1;
16833
16834 #ifdef GLYPH_DEBUG
16835 debug_method_add (w, "try_window_reusing_current_matrix 1");
16836 #endif
16837 return 1;
16838 }
16839 else if (CHARPOS (new_start) > CHARPOS (start))
16840 {
16841 struct glyph_row *pt_row, *row;
16842 struct glyph_row *first_reusable_row;
16843 struct glyph_row *first_row_to_display;
16844 int dy;
16845 int yb = window_text_bottom_y (w);
16846
16847 /* Find the row starting at new_start, if there is one. Don't
16848 reuse a partially visible line at the end. */
16849 first_reusable_row = start_row;
16850 while (first_reusable_row->enabled_p
16851 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16852 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16853 < CHARPOS (new_start)))
16854 ++first_reusable_row;
16855
16856 /* Give up if there is no row to reuse. */
16857 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16858 || !first_reusable_row->enabled_p
16859 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16860 != CHARPOS (new_start)))
16861 return 0;
16862
16863 /* We can reuse fully visible rows beginning with
16864 first_reusable_row to the end of the window. Set
16865 first_row_to_display to the first row that cannot be reused.
16866 Set pt_row to the row containing point, if there is any. */
16867 pt_row = NULL;
16868 for (first_row_to_display = first_reusable_row;
16869 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16870 ++first_row_to_display)
16871 {
16872 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16873 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16874 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16875 && first_row_to_display->ends_at_zv_p
16876 && pt_row == NULL)))
16877 pt_row = first_row_to_display;
16878 }
16879
16880 /* Start displaying at the start of first_row_to_display. */
16881 eassert (first_row_to_display->y < yb);
16882 init_to_row_start (&it, w, first_row_to_display);
16883
16884 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16885 - start_vpos);
16886 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16887 - nrows_scrolled);
16888 it.current_y = (first_row_to_display->y - first_reusable_row->y
16889 + WINDOW_HEADER_LINE_HEIGHT (w));
16890
16891 /* Display lines beginning with first_row_to_display in the
16892 desired matrix. Set last_text_row to the last row displayed
16893 that displays text. */
16894 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16895 if (pt_row == NULL)
16896 w->cursor.vpos = -1;
16897 last_text_row = NULL;
16898 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16899 if (display_line (&it))
16900 last_text_row = it.glyph_row - 1;
16901
16902 /* If point is in a reused row, adjust y and vpos of the cursor
16903 position. */
16904 if (pt_row)
16905 {
16906 w->cursor.vpos -= nrows_scrolled;
16907 w->cursor.y -= first_reusable_row->y - start_row->y;
16908 }
16909
16910 /* Give up if point isn't in a row displayed or reused. (This
16911 also handles the case where w->cursor.vpos < nrows_scrolled
16912 after the calls to display_line, which can happen with scroll
16913 margins. See bug#1295.) */
16914 if (w->cursor.vpos < 0)
16915 {
16916 clear_glyph_matrix (w->desired_matrix);
16917 return 0;
16918 }
16919
16920 /* Scroll the display. */
16921 run.current_y = first_reusable_row->y;
16922 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16923 run.height = it.last_visible_y - run.current_y;
16924 dy = run.current_y - run.desired_y;
16925
16926 if (run.height)
16927 {
16928 update_begin (f);
16929 FRAME_RIF (f)->update_window_begin_hook (w);
16930 FRAME_RIF (f)->clear_window_mouse_face (w);
16931 FRAME_RIF (f)->scroll_run_hook (w, &run);
16932 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16933 update_end (f);
16934 }
16935
16936 /* Adjust Y positions of reused rows. */
16937 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16938 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16939 max_y = it.last_visible_y;
16940 for (row = first_reusable_row; row < first_row_to_display; ++row)
16941 {
16942 row->y -= dy;
16943 row->visible_height = row->height;
16944 if (row->y < min_y)
16945 row->visible_height -= min_y - row->y;
16946 if (row->y + row->height > max_y)
16947 row->visible_height -= row->y + row->height - max_y;
16948 if (row->fringe_bitmap_periodic_p)
16949 row->redraw_fringe_bitmaps_p = 1;
16950 }
16951
16952 /* Scroll the current matrix. */
16953 eassert (nrows_scrolled > 0);
16954 rotate_matrix (w->current_matrix,
16955 start_vpos,
16956 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16957 -nrows_scrolled);
16958
16959 /* Disable rows not reused. */
16960 for (row -= nrows_scrolled; row < bottom_row; ++row)
16961 row->enabled_p = false;
16962
16963 /* Point may have moved to a different line, so we cannot assume that
16964 the previous cursor position is valid; locate the correct row. */
16965 if (pt_row)
16966 {
16967 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16968 row < bottom_row
16969 && PT >= MATRIX_ROW_END_CHARPOS (row)
16970 && !row->ends_at_zv_p;
16971 row++)
16972 {
16973 w->cursor.vpos++;
16974 w->cursor.y = row->y;
16975 }
16976 if (row < bottom_row)
16977 {
16978 /* Can't simply scan the row for point with
16979 bidi-reordered glyph rows. Let set_cursor_from_row
16980 figure out where to put the cursor, and if it fails,
16981 give up. */
16982 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16983 {
16984 if (!set_cursor_from_row (w, row, w->current_matrix,
16985 0, 0, 0, 0))
16986 {
16987 clear_glyph_matrix (w->desired_matrix);
16988 return 0;
16989 }
16990 }
16991 else
16992 {
16993 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16994 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16995
16996 for (; glyph < end
16997 && (!BUFFERP (glyph->object)
16998 || glyph->charpos < PT);
16999 glyph++)
17000 {
17001 w->cursor.hpos++;
17002 w->cursor.x += glyph->pixel_width;
17003 }
17004 }
17005 }
17006 }
17007
17008 /* Adjust window end. A null value of last_text_row means that
17009 the window end is in reused rows which in turn means that
17010 only its vpos can have changed. */
17011 if (last_text_row)
17012 adjust_window_ends (w, last_text_row, 0);
17013 else
17014 w->window_end_vpos -= nrows_scrolled;
17015
17016 w->window_end_valid = 0;
17017 w->desired_matrix->no_scrolling_p = 1;
17018
17019 #ifdef GLYPH_DEBUG
17020 debug_method_add (w, "try_window_reusing_current_matrix 2");
17021 #endif
17022 return 1;
17023 }
17024
17025 return 0;
17026 }
17027
17028
17029 \f
17030 /************************************************************************
17031 Window redisplay reusing current matrix when buffer has changed
17032 ************************************************************************/
17033
17034 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17035 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17036 ptrdiff_t *, ptrdiff_t *);
17037 static struct glyph_row *
17038 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17039 struct glyph_row *);
17040
17041
17042 /* Return the last row in MATRIX displaying text. If row START is
17043 non-null, start searching with that row. IT gives the dimensions
17044 of the display. Value is null if matrix is empty; otherwise it is
17045 a pointer to the row found. */
17046
17047 static struct glyph_row *
17048 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17049 struct glyph_row *start)
17050 {
17051 struct glyph_row *row, *row_found;
17052
17053 /* Set row_found to the last row in IT->w's current matrix
17054 displaying text. The loop looks funny but think of partially
17055 visible lines. */
17056 row_found = NULL;
17057 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17058 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17059 {
17060 eassert (row->enabled_p);
17061 row_found = row;
17062 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17063 break;
17064 ++row;
17065 }
17066
17067 return row_found;
17068 }
17069
17070
17071 /* Return the last row in the current matrix of W that is not affected
17072 by changes at the start of current_buffer that occurred since W's
17073 current matrix was built. Value is null if no such row exists.
17074
17075 BEG_UNCHANGED us the number of characters unchanged at the start of
17076 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17077 first changed character in current_buffer. Characters at positions <
17078 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17079 when the current matrix was built. */
17080
17081 static struct glyph_row *
17082 find_last_unchanged_at_beg_row (struct window *w)
17083 {
17084 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17085 struct glyph_row *row;
17086 struct glyph_row *row_found = NULL;
17087 int yb = window_text_bottom_y (w);
17088
17089 /* Find the last row displaying unchanged text. */
17090 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17091 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17092 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17093 ++row)
17094 {
17095 if (/* If row ends before first_changed_pos, it is unchanged,
17096 except in some case. */
17097 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17098 /* When row ends in ZV and we write at ZV it is not
17099 unchanged. */
17100 && !row->ends_at_zv_p
17101 /* When first_changed_pos is the end of a continued line,
17102 row is not unchanged because it may be no longer
17103 continued. */
17104 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17105 && (row->continued_p
17106 || row->exact_window_width_line_p))
17107 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17108 needs to be recomputed, so don't consider this row as
17109 unchanged. This happens when the last line was
17110 bidi-reordered and was killed immediately before this
17111 redisplay cycle. In that case, ROW->end stores the
17112 buffer position of the first visual-order character of
17113 the killed text, which is now beyond ZV. */
17114 && CHARPOS (row->end.pos) <= ZV)
17115 row_found = row;
17116
17117 /* Stop if last visible row. */
17118 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17119 break;
17120 }
17121
17122 return row_found;
17123 }
17124
17125
17126 /* Find the first glyph row in the current matrix of W that is not
17127 affected by changes at the end of current_buffer since the
17128 time W's current matrix was built.
17129
17130 Return in *DELTA the number of chars by which buffer positions in
17131 unchanged text at the end of current_buffer must be adjusted.
17132
17133 Return in *DELTA_BYTES the corresponding number of bytes.
17134
17135 Value is null if no such row exists, i.e. all rows are affected by
17136 changes. */
17137
17138 static struct glyph_row *
17139 find_first_unchanged_at_end_row (struct window *w,
17140 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17141 {
17142 struct glyph_row *row;
17143 struct glyph_row *row_found = NULL;
17144
17145 *delta = *delta_bytes = 0;
17146
17147 /* Display must not have been paused, otherwise the current matrix
17148 is not up to date. */
17149 eassert (w->window_end_valid);
17150
17151 /* A value of window_end_pos >= END_UNCHANGED means that the window
17152 end is in the range of changed text. If so, there is no
17153 unchanged row at the end of W's current matrix. */
17154 if (w->window_end_pos >= END_UNCHANGED)
17155 return NULL;
17156
17157 /* Set row to the last row in W's current matrix displaying text. */
17158 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17159
17160 /* If matrix is entirely empty, no unchanged row exists. */
17161 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17162 {
17163 /* The value of row is the last glyph row in the matrix having a
17164 meaningful buffer position in it. The end position of row
17165 corresponds to window_end_pos. This allows us to translate
17166 buffer positions in the current matrix to current buffer
17167 positions for characters not in changed text. */
17168 ptrdiff_t Z_old =
17169 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17170 ptrdiff_t Z_BYTE_old =
17171 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17172 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17173 struct glyph_row *first_text_row
17174 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17175
17176 *delta = Z - Z_old;
17177 *delta_bytes = Z_BYTE - Z_BYTE_old;
17178
17179 /* Set last_unchanged_pos to the buffer position of the last
17180 character in the buffer that has not been changed. Z is the
17181 index + 1 of the last character in current_buffer, i.e. by
17182 subtracting END_UNCHANGED we get the index of the last
17183 unchanged character, and we have to add BEG to get its buffer
17184 position. */
17185 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17186 last_unchanged_pos_old = last_unchanged_pos - *delta;
17187
17188 /* Search backward from ROW for a row displaying a line that
17189 starts at a minimum position >= last_unchanged_pos_old. */
17190 for (; row > first_text_row; --row)
17191 {
17192 /* This used to abort, but it can happen.
17193 It is ok to just stop the search instead here. KFS. */
17194 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17195 break;
17196
17197 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17198 row_found = row;
17199 }
17200 }
17201
17202 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17203
17204 return row_found;
17205 }
17206
17207
17208 /* Make sure that glyph rows in the current matrix of window W
17209 reference the same glyph memory as corresponding rows in the
17210 frame's frame matrix. This function is called after scrolling W's
17211 current matrix on a terminal frame in try_window_id and
17212 try_window_reusing_current_matrix. */
17213
17214 static void
17215 sync_frame_with_window_matrix_rows (struct window *w)
17216 {
17217 struct frame *f = XFRAME (w->frame);
17218 struct glyph_row *window_row, *window_row_end, *frame_row;
17219
17220 /* Preconditions: W must be a leaf window and full-width. Its frame
17221 must have a frame matrix. */
17222 eassert (BUFFERP (w->contents));
17223 eassert (WINDOW_FULL_WIDTH_P (w));
17224 eassert (!FRAME_WINDOW_P (f));
17225
17226 /* If W is a full-width window, glyph pointers in W's current matrix
17227 have, by definition, to be the same as glyph pointers in the
17228 corresponding frame matrix. Note that frame matrices have no
17229 marginal areas (see build_frame_matrix). */
17230 window_row = w->current_matrix->rows;
17231 window_row_end = window_row + w->current_matrix->nrows;
17232 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17233 while (window_row < window_row_end)
17234 {
17235 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17236 struct glyph *end = window_row->glyphs[LAST_AREA];
17237
17238 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17239 frame_row->glyphs[TEXT_AREA] = start;
17240 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17241 frame_row->glyphs[LAST_AREA] = end;
17242
17243 /* Disable frame rows whose corresponding window rows have
17244 been disabled in try_window_id. */
17245 if (!window_row->enabled_p)
17246 frame_row->enabled_p = false;
17247
17248 ++window_row, ++frame_row;
17249 }
17250 }
17251
17252
17253 /* Find the glyph row in window W containing CHARPOS. Consider all
17254 rows between START and END (not inclusive). END null means search
17255 all rows to the end of the display area of W. Value is the row
17256 containing CHARPOS or null. */
17257
17258 struct glyph_row *
17259 row_containing_pos (struct window *w, ptrdiff_t charpos,
17260 struct glyph_row *start, struct glyph_row *end, int dy)
17261 {
17262 struct glyph_row *row = start;
17263 struct glyph_row *best_row = NULL;
17264 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17265 int last_y;
17266
17267 /* If we happen to start on a header-line, skip that. */
17268 if (row->mode_line_p)
17269 ++row;
17270
17271 if ((end && row >= end) || !row->enabled_p)
17272 return NULL;
17273
17274 last_y = window_text_bottom_y (w) - dy;
17275
17276 while (1)
17277 {
17278 /* Give up if we have gone too far. */
17279 if (end && row >= end)
17280 return NULL;
17281 /* This formerly returned if they were equal.
17282 I think that both quantities are of a "last plus one" type;
17283 if so, when they are equal, the row is within the screen. -- rms. */
17284 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17285 return NULL;
17286
17287 /* If it is in this row, return this row. */
17288 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17289 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17290 /* The end position of a row equals the start
17291 position of the next row. If CHARPOS is there, we
17292 would rather consider it displayed in the next
17293 line, except when this line ends in ZV. */
17294 && !row_for_charpos_p (row, charpos)))
17295 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17296 {
17297 struct glyph *g;
17298
17299 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17300 || (!best_row && !row->continued_p))
17301 return row;
17302 /* In bidi-reordered rows, there could be several rows whose
17303 edges surround CHARPOS, all of these rows belonging to
17304 the same continued line. We need to find the row which
17305 fits CHARPOS the best. */
17306 for (g = row->glyphs[TEXT_AREA];
17307 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17308 g++)
17309 {
17310 if (!STRINGP (g->object))
17311 {
17312 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17313 {
17314 mindif = eabs (g->charpos - charpos);
17315 best_row = row;
17316 /* Exact match always wins. */
17317 if (mindif == 0)
17318 return best_row;
17319 }
17320 }
17321 }
17322 }
17323 else if (best_row && !row->continued_p)
17324 return best_row;
17325 ++row;
17326 }
17327 }
17328
17329
17330 /* Try to redisplay window W by reusing its existing display. W's
17331 current matrix must be up to date when this function is called,
17332 i.e. window_end_valid must be nonzero.
17333
17334 Value is
17335
17336 1 if display has been updated
17337 0 if otherwise unsuccessful
17338 -1 if redisplay with same window start is known not to succeed
17339
17340 The following steps are performed:
17341
17342 1. Find the last row in the current matrix of W that is not
17343 affected by changes at the start of current_buffer. If no such row
17344 is found, give up.
17345
17346 2. Find the first row in W's current matrix that is not affected by
17347 changes at the end of current_buffer. Maybe there is no such row.
17348
17349 3. Display lines beginning with the row + 1 found in step 1 to the
17350 row found in step 2 or, if step 2 didn't find a row, to the end of
17351 the window.
17352
17353 4. If cursor is not known to appear on the window, give up.
17354
17355 5. If display stopped at the row found in step 2, scroll the
17356 display and current matrix as needed.
17357
17358 6. Maybe display some lines at the end of W, if we must. This can
17359 happen under various circumstances, like a partially visible line
17360 becoming fully visible, or because newly displayed lines are displayed
17361 in smaller font sizes.
17362
17363 7. Update W's window end information. */
17364
17365 static int
17366 try_window_id (struct window *w)
17367 {
17368 struct frame *f = XFRAME (w->frame);
17369 struct glyph_matrix *current_matrix = w->current_matrix;
17370 struct glyph_matrix *desired_matrix = w->desired_matrix;
17371 struct glyph_row *last_unchanged_at_beg_row;
17372 struct glyph_row *first_unchanged_at_end_row;
17373 struct glyph_row *row;
17374 struct glyph_row *bottom_row;
17375 int bottom_vpos;
17376 struct it it;
17377 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17378 int dvpos, dy;
17379 struct text_pos start_pos;
17380 struct run run;
17381 int first_unchanged_at_end_vpos = 0;
17382 struct glyph_row *last_text_row, *last_text_row_at_end;
17383 struct text_pos start;
17384 ptrdiff_t first_changed_charpos, last_changed_charpos;
17385
17386 #ifdef GLYPH_DEBUG
17387 if (inhibit_try_window_id)
17388 return 0;
17389 #endif
17390
17391 /* This is handy for debugging. */
17392 #if 0
17393 #define GIVE_UP(X) \
17394 do { \
17395 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17396 return 0; \
17397 } while (0)
17398 #else
17399 #define GIVE_UP(X) return 0
17400 #endif
17401
17402 SET_TEXT_POS_FROM_MARKER (start, w->start);
17403
17404 /* Don't use this for mini-windows because these can show
17405 messages and mini-buffers, and we don't handle that here. */
17406 if (MINI_WINDOW_P (w))
17407 GIVE_UP (1);
17408
17409 /* This flag is used to prevent redisplay optimizations. */
17410 if (windows_or_buffers_changed || f->cursor_type_changed)
17411 GIVE_UP (2);
17412
17413 /* Verify that narrowing has not changed.
17414 Also verify that we were not told to prevent redisplay optimizations.
17415 It would be nice to further
17416 reduce the number of cases where this prevents try_window_id. */
17417 if (current_buffer->clip_changed
17418 || current_buffer->prevent_redisplay_optimizations_p)
17419 GIVE_UP (3);
17420
17421 /* Window must either use window-based redisplay or be full width. */
17422 if (!FRAME_WINDOW_P (f)
17423 && (!FRAME_LINE_INS_DEL_OK (f)
17424 || !WINDOW_FULL_WIDTH_P (w)))
17425 GIVE_UP (4);
17426
17427 /* Give up if point is known NOT to appear in W. */
17428 if (PT < CHARPOS (start))
17429 GIVE_UP (5);
17430
17431 /* Another way to prevent redisplay optimizations. */
17432 if (w->last_modified == 0)
17433 GIVE_UP (6);
17434
17435 /* Verify that window is not hscrolled. */
17436 if (w->hscroll != 0)
17437 GIVE_UP (7);
17438
17439 /* Verify that display wasn't paused. */
17440 if (!w->window_end_valid)
17441 GIVE_UP (8);
17442
17443 /* Likewise if highlighting trailing whitespace. */
17444 if (!NILP (Vshow_trailing_whitespace))
17445 GIVE_UP (11);
17446
17447 /* Can't use this if overlay arrow position and/or string have
17448 changed. */
17449 if (overlay_arrows_changed_p ())
17450 GIVE_UP (12);
17451
17452 /* When word-wrap is on, adding a space to the first word of a
17453 wrapped line can change the wrap position, altering the line
17454 above it. It might be worthwhile to handle this more
17455 intelligently, but for now just redisplay from scratch. */
17456 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17457 GIVE_UP (21);
17458
17459 /* Under bidi reordering, adding or deleting a character in the
17460 beginning of a paragraph, before the first strong directional
17461 character, can change the base direction of the paragraph (unless
17462 the buffer specifies a fixed paragraph direction), which will
17463 require to redisplay the whole paragraph. It might be worthwhile
17464 to find the paragraph limits and widen the range of redisplayed
17465 lines to that, but for now just give up this optimization and
17466 redisplay from scratch. */
17467 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17468 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17469 GIVE_UP (22);
17470
17471 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17472 only if buffer has really changed. The reason is that the gap is
17473 initially at Z for freshly visited files. The code below would
17474 set end_unchanged to 0 in that case. */
17475 if (MODIFF > SAVE_MODIFF
17476 /* This seems to happen sometimes after saving a buffer. */
17477 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17478 {
17479 if (GPT - BEG < BEG_UNCHANGED)
17480 BEG_UNCHANGED = GPT - BEG;
17481 if (Z - GPT < END_UNCHANGED)
17482 END_UNCHANGED = Z - GPT;
17483 }
17484
17485 /* The position of the first and last character that has been changed. */
17486 first_changed_charpos = BEG + BEG_UNCHANGED;
17487 last_changed_charpos = Z - END_UNCHANGED;
17488
17489 /* If window starts after a line end, and the last change is in
17490 front of that newline, then changes don't affect the display.
17491 This case happens with stealth-fontification. Note that although
17492 the display is unchanged, glyph positions in the matrix have to
17493 be adjusted, of course. */
17494 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17495 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17496 && ((last_changed_charpos < CHARPOS (start)
17497 && CHARPOS (start) == BEGV)
17498 || (last_changed_charpos < CHARPOS (start) - 1
17499 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17500 {
17501 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17502 struct glyph_row *r0;
17503
17504 /* Compute how many chars/bytes have been added to or removed
17505 from the buffer. */
17506 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17507 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17508 Z_delta = Z - Z_old;
17509 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17510
17511 /* Give up if PT is not in the window. Note that it already has
17512 been checked at the start of try_window_id that PT is not in
17513 front of the window start. */
17514 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17515 GIVE_UP (13);
17516
17517 /* If window start is unchanged, we can reuse the whole matrix
17518 as is, after adjusting glyph positions. No need to compute
17519 the window end again, since its offset from Z hasn't changed. */
17520 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17521 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17522 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17523 /* PT must not be in a partially visible line. */
17524 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17525 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17526 {
17527 /* Adjust positions in the glyph matrix. */
17528 if (Z_delta || Z_delta_bytes)
17529 {
17530 struct glyph_row *r1
17531 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17532 increment_matrix_positions (w->current_matrix,
17533 MATRIX_ROW_VPOS (r0, current_matrix),
17534 MATRIX_ROW_VPOS (r1, current_matrix),
17535 Z_delta, Z_delta_bytes);
17536 }
17537
17538 /* Set the cursor. */
17539 row = row_containing_pos (w, PT, r0, NULL, 0);
17540 if (row)
17541 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17542 return 1;
17543 }
17544 }
17545
17546 /* Handle the case that changes are all below what is displayed in
17547 the window, and that PT is in the window. This shortcut cannot
17548 be taken if ZV is visible in the window, and text has been added
17549 there that is visible in the window. */
17550 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17551 /* ZV is not visible in the window, or there are no
17552 changes at ZV, actually. */
17553 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17554 || first_changed_charpos == last_changed_charpos))
17555 {
17556 struct glyph_row *r0;
17557
17558 /* Give up if PT is not in the window. Note that it already has
17559 been checked at the start of try_window_id that PT is not in
17560 front of the window start. */
17561 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17562 GIVE_UP (14);
17563
17564 /* If window start is unchanged, we can reuse the whole matrix
17565 as is, without changing glyph positions since no text has
17566 been added/removed in front of the window end. */
17567 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17568 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17569 /* PT must not be in a partially visible line. */
17570 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17571 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17572 {
17573 /* We have to compute the window end anew since text
17574 could have been added/removed after it. */
17575 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17576 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17577
17578 /* Set the cursor. */
17579 row = row_containing_pos (w, PT, r0, NULL, 0);
17580 if (row)
17581 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17582 return 2;
17583 }
17584 }
17585
17586 /* Give up if window start is in the changed area.
17587
17588 The condition used to read
17589
17590 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17591
17592 but why that was tested escapes me at the moment. */
17593 if (CHARPOS (start) >= first_changed_charpos
17594 && CHARPOS (start) <= last_changed_charpos)
17595 GIVE_UP (15);
17596
17597 /* Check that window start agrees with the start of the first glyph
17598 row in its current matrix. Check this after we know the window
17599 start is not in changed text, otherwise positions would not be
17600 comparable. */
17601 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17602 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17603 GIVE_UP (16);
17604
17605 /* Give up if the window ends in strings. Overlay strings
17606 at the end are difficult to handle, so don't try. */
17607 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17608 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17609 GIVE_UP (20);
17610
17611 /* Compute the position at which we have to start displaying new
17612 lines. Some of the lines at the top of the window might be
17613 reusable because they are not displaying changed text. Find the
17614 last row in W's current matrix not affected by changes at the
17615 start of current_buffer. Value is null if changes start in the
17616 first line of window. */
17617 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17618 if (last_unchanged_at_beg_row)
17619 {
17620 /* Avoid starting to display in the middle of a character, a TAB
17621 for instance. This is easier than to set up the iterator
17622 exactly, and it's not a frequent case, so the additional
17623 effort wouldn't really pay off. */
17624 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17625 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17626 && last_unchanged_at_beg_row > w->current_matrix->rows)
17627 --last_unchanged_at_beg_row;
17628
17629 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17630 GIVE_UP (17);
17631
17632 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17633 GIVE_UP (18);
17634 start_pos = it.current.pos;
17635
17636 /* Start displaying new lines in the desired matrix at the same
17637 vpos we would use in the current matrix, i.e. below
17638 last_unchanged_at_beg_row. */
17639 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17640 current_matrix);
17641 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17642 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17643
17644 eassert (it.hpos == 0 && it.current_x == 0);
17645 }
17646 else
17647 {
17648 /* There are no reusable lines at the start of the window.
17649 Start displaying in the first text line. */
17650 start_display (&it, w, start);
17651 it.vpos = it.first_vpos;
17652 start_pos = it.current.pos;
17653 }
17654
17655 /* Find the first row that is not affected by changes at the end of
17656 the buffer. Value will be null if there is no unchanged row, in
17657 which case we must redisplay to the end of the window. delta
17658 will be set to the value by which buffer positions beginning with
17659 first_unchanged_at_end_row have to be adjusted due to text
17660 changes. */
17661 first_unchanged_at_end_row
17662 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17663 IF_DEBUG (debug_delta = delta);
17664 IF_DEBUG (debug_delta_bytes = delta_bytes);
17665
17666 /* Set stop_pos to the buffer position up to which we will have to
17667 display new lines. If first_unchanged_at_end_row != NULL, this
17668 is the buffer position of the start of the line displayed in that
17669 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17670 that we don't stop at a buffer position. */
17671 stop_pos = 0;
17672 if (first_unchanged_at_end_row)
17673 {
17674 eassert (last_unchanged_at_beg_row == NULL
17675 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17676
17677 /* If this is a continuation line, move forward to the next one
17678 that isn't. Changes in lines above affect this line.
17679 Caution: this may move first_unchanged_at_end_row to a row
17680 not displaying text. */
17681 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17682 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17683 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17684 < it.last_visible_y))
17685 ++first_unchanged_at_end_row;
17686
17687 if (!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 = NULL;
17691 else
17692 {
17693 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17694 + delta);
17695 first_unchanged_at_end_vpos
17696 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17697 eassert (stop_pos >= Z - END_UNCHANGED);
17698 }
17699 }
17700 else if (last_unchanged_at_beg_row == NULL)
17701 GIVE_UP (19);
17702
17703
17704 #ifdef GLYPH_DEBUG
17705
17706 /* Either there is no unchanged row at the end, or the one we have
17707 now displays text. This is a necessary condition for the window
17708 end pos calculation at the end of this function. */
17709 eassert (first_unchanged_at_end_row == NULL
17710 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17711
17712 debug_last_unchanged_at_beg_vpos
17713 = (last_unchanged_at_beg_row
17714 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17715 : -1);
17716 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17717
17718 #endif /* GLYPH_DEBUG */
17719
17720
17721 /* Display new lines. Set last_text_row to the last new line
17722 displayed which has text on it, i.e. might end up as being the
17723 line where the window_end_vpos is. */
17724 w->cursor.vpos = -1;
17725 last_text_row = NULL;
17726 overlay_arrow_seen = 0;
17727 while (it.current_y < it.last_visible_y
17728 && !f->fonts_changed
17729 && (first_unchanged_at_end_row == NULL
17730 || IT_CHARPOS (it) < stop_pos))
17731 {
17732 if (display_line (&it))
17733 last_text_row = it.glyph_row - 1;
17734 }
17735
17736 if (f->fonts_changed)
17737 return -1;
17738
17739
17740 /* Compute differences in buffer positions, y-positions etc. for
17741 lines reused at the bottom of the window. Compute what we can
17742 scroll. */
17743 if (first_unchanged_at_end_row
17744 /* No lines reused because we displayed everything up to the
17745 bottom of the window. */
17746 && it.current_y < it.last_visible_y)
17747 {
17748 dvpos = (it.vpos
17749 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17750 current_matrix));
17751 dy = it.current_y - first_unchanged_at_end_row->y;
17752 run.current_y = first_unchanged_at_end_row->y;
17753 run.desired_y = run.current_y + dy;
17754 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17755 }
17756 else
17757 {
17758 delta = delta_bytes = dvpos = dy
17759 = run.current_y = run.desired_y = run.height = 0;
17760 first_unchanged_at_end_row = NULL;
17761 }
17762 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17763
17764
17765 /* Find the cursor if not already found. We have to decide whether
17766 PT will appear on this window (it sometimes doesn't, but this is
17767 not a very frequent case.) This decision has to be made before
17768 the current matrix is altered. A value of cursor.vpos < 0 means
17769 that PT is either in one of the lines beginning at
17770 first_unchanged_at_end_row or below the window. Don't care for
17771 lines that might be displayed later at the window end; as
17772 mentioned, this is not a frequent case. */
17773 if (w->cursor.vpos < 0)
17774 {
17775 /* Cursor in unchanged rows at the top? */
17776 if (PT < CHARPOS (start_pos)
17777 && last_unchanged_at_beg_row)
17778 {
17779 row = row_containing_pos (w, PT,
17780 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17781 last_unchanged_at_beg_row + 1, 0);
17782 if (row)
17783 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17784 }
17785
17786 /* Start from first_unchanged_at_end_row looking for PT. */
17787 else if (first_unchanged_at_end_row)
17788 {
17789 row = row_containing_pos (w, PT - delta,
17790 first_unchanged_at_end_row, NULL, 0);
17791 if (row)
17792 set_cursor_from_row (w, row, w->current_matrix, delta,
17793 delta_bytes, dy, dvpos);
17794 }
17795
17796 /* Give up if cursor was not found. */
17797 if (w->cursor.vpos < 0)
17798 {
17799 clear_glyph_matrix (w->desired_matrix);
17800 return -1;
17801 }
17802 }
17803
17804 /* Don't let the cursor end in the scroll margins. */
17805 {
17806 int this_scroll_margin, cursor_height;
17807 int frame_line_height = default_line_pixel_height (w);
17808 int window_total_lines
17809 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17810
17811 this_scroll_margin =
17812 max (0, min (scroll_margin, window_total_lines / 4));
17813 this_scroll_margin *= frame_line_height;
17814 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17815
17816 if ((w->cursor.y < this_scroll_margin
17817 && CHARPOS (start) > BEGV)
17818 /* Old redisplay didn't take scroll margin into account at the bottom,
17819 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17820 || (w->cursor.y + (make_cursor_line_fully_visible_p
17821 ? cursor_height + this_scroll_margin
17822 : 1)) > it.last_visible_y)
17823 {
17824 w->cursor.vpos = -1;
17825 clear_glyph_matrix (w->desired_matrix);
17826 return -1;
17827 }
17828 }
17829
17830 /* Scroll the display. Do it before changing the current matrix so
17831 that xterm.c doesn't get confused about where the cursor glyph is
17832 found. */
17833 if (dy && run.height)
17834 {
17835 update_begin (f);
17836
17837 if (FRAME_WINDOW_P (f))
17838 {
17839 FRAME_RIF (f)->update_window_begin_hook (w);
17840 FRAME_RIF (f)->clear_window_mouse_face (w);
17841 FRAME_RIF (f)->scroll_run_hook (w, &run);
17842 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17843 }
17844 else
17845 {
17846 /* Terminal frame. In this case, dvpos gives the number of
17847 lines to scroll by; dvpos < 0 means scroll up. */
17848 int from_vpos
17849 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17850 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17851 int end = (WINDOW_TOP_EDGE_LINE (w)
17852 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17853 + window_internal_height (w));
17854
17855 #if defined (HAVE_GPM) || defined (MSDOS)
17856 x_clear_window_mouse_face (w);
17857 #endif
17858 /* Perform the operation on the screen. */
17859 if (dvpos > 0)
17860 {
17861 /* Scroll last_unchanged_at_beg_row to the end of the
17862 window down dvpos lines. */
17863 set_terminal_window (f, end);
17864
17865 /* On dumb terminals delete dvpos lines at the end
17866 before inserting dvpos empty lines. */
17867 if (!FRAME_SCROLL_REGION_OK (f))
17868 ins_del_lines (f, end - dvpos, -dvpos);
17869
17870 /* Insert dvpos empty lines in front of
17871 last_unchanged_at_beg_row. */
17872 ins_del_lines (f, from, dvpos);
17873 }
17874 else if (dvpos < 0)
17875 {
17876 /* Scroll up last_unchanged_at_beg_vpos to the end of
17877 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17878 set_terminal_window (f, end);
17879
17880 /* Delete dvpos lines in front of
17881 last_unchanged_at_beg_vpos. ins_del_lines will set
17882 the cursor to the given vpos and emit |dvpos| delete
17883 line sequences. */
17884 ins_del_lines (f, from + dvpos, dvpos);
17885
17886 /* On a dumb terminal insert dvpos empty lines at the
17887 end. */
17888 if (!FRAME_SCROLL_REGION_OK (f))
17889 ins_del_lines (f, end + dvpos, -dvpos);
17890 }
17891
17892 set_terminal_window (f, 0);
17893 }
17894
17895 update_end (f);
17896 }
17897
17898 /* Shift reused rows of the current matrix to the right position.
17899 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17900 text. */
17901 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17902 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17903 if (dvpos < 0)
17904 {
17905 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17906 bottom_vpos, dvpos);
17907 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17908 bottom_vpos);
17909 }
17910 else if (dvpos > 0)
17911 {
17912 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17913 bottom_vpos, dvpos);
17914 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17915 first_unchanged_at_end_vpos + dvpos);
17916 }
17917
17918 /* For frame-based redisplay, make sure that current frame and window
17919 matrix are in sync with respect to glyph memory. */
17920 if (!FRAME_WINDOW_P (f))
17921 sync_frame_with_window_matrix_rows (w);
17922
17923 /* Adjust buffer positions in reused rows. */
17924 if (delta || delta_bytes)
17925 increment_matrix_positions (current_matrix,
17926 first_unchanged_at_end_vpos + dvpos,
17927 bottom_vpos, delta, delta_bytes);
17928
17929 /* Adjust Y positions. */
17930 if (dy)
17931 shift_glyph_matrix (w, current_matrix,
17932 first_unchanged_at_end_vpos + dvpos,
17933 bottom_vpos, dy);
17934
17935 if (first_unchanged_at_end_row)
17936 {
17937 first_unchanged_at_end_row += dvpos;
17938 if (first_unchanged_at_end_row->y >= it.last_visible_y
17939 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17940 first_unchanged_at_end_row = NULL;
17941 }
17942
17943 /* If scrolling up, there may be some lines to display at the end of
17944 the window. */
17945 last_text_row_at_end = NULL;
17946 if (dy < 0)
17947 {
17948 /* Scrolling up can leave for example a partially visible line
17949 at the end of the window to be redisplayed. */
17950 /* Set last_row to the glyph row in the current matrix where the
17951 window end line is found. It has been moved up or down in
17952 the matrix by dvpos. */
17953 int last_vpos = w->window_end_vpos + dvpos;
17954 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17955
17956 /* If last_row is the window end line, it should display text. */
17957 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17958
17959 /* If window end line was partially visible before, begin
17960 displaying at that line. Otherwise begin displaying with the
17961 line following it. */
17962 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17963 {
17964 init_to_row_start (&it, w, last_row);
17965 it.vpos = last_vpos;
17966 it.current_y = last_row->y;
17967 }
17968 else
17969 {
17970 init_to_row_end (&it, w, last_row);
17971 it.vpos = 1 + last_vpos;
17972 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17973 ++last_row;
17974 }
17975
17976 /* We may start in a continuation line. If so, we have to
17977 get the right continuation_lines_width and current_x. */
17978 it.continuation_lines_width = last_row->continuation_lines_width;
17979 it.hpos = it.current_x = 0;
17980
17981 /* Display the rest of the lines at the window end. */
17982 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17983 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17984 {
17985 /* Is it always sure that the display agrees with lines in
17986 the current matrix? I don't think so, so we mark rows
17987 displayed invalid in the current matrix by setting their
17988 enabled_p flag to zero. */
17989 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
17990 if (display_line (&it))
17991 last_text_row_at_end = it.glyph_row - 1;
17992 }
17993 }
17994
17995 /* Update window_end_pos and window_end_vpos. */
17996 if (first_unchanged_at_end_row && !last_text_row_at_end)
17997 {
17998 /* Window end line if one of the preserved rows from the current
17999 matrix. Set row to the last row displaying text in current
18000 matrix starting at first_unchanged_at_end_row, after
18001 scrolling. */
18002 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18003 row = find_last_row_displaying_text (w->current_matrix, &it,
18004 first_unchanged_at_end_row);
18005 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18006 adjust_window_ends (w, row, 1);
18007 eassert (w->window_end_bytepos >= 0);
18008 IF_DEBUG (debug_method_add (w, "A"));
18009 }
18010 else if (last_text_row_at_end)
18011 {
18012 adjust_window_ends (w, last_text_row_at_end, 0);
18013 eassert (w->window_end_bytepos >= 0);
18014 IF_DEBUG (debug_method_add (w, "B"));
18015 }
18016 else if (last_text_row)
18017 {
18018 /* We have displayed either to the end of the window or at the
18019 end of the window, i.e. the last row with text is to be found
18020 in the desired matrix. */
18021 adjust_window_ends (w, last_text_row, 0);
18022 eassert (w->window_end_bytepos >= 0);
18023 }
18024 else if (first_unchanged_at_end_row == NULL
18025 && last_text_row == NULL
18026 && last_text_row_at_end == NULL)
18027 {
18028 /* Displayed to end of window, but no line containing text was
18029 displayed. Lines were deleted at the end of the window. */
18030 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18031 int vpos = w->window_end_vpos;
18032 struct glyph_row *current_row = current_matrix->rows + vpos;
18033 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18034
18035 for (row = NULL;
18036 row == NULL && vpos >= first_vpos;
18037 --vpos, --current_row, --desired_row)
18038 {
18039 if (desired_row->enabled_p)
18040 {
18041 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18042 row = desired_row;
18043 }
18044 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18045 row = current_row;
18046 }
18047
18048 eassert (row != NULL);
18049 w->window_end_vpos = vpos + 1;
18050 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18051 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18052 eassert (w->window_end_bytepos >= 0);
18053 IF_DEBUG (debug_method_add (w, "C"));
18054 }
18055 else
18056 emacs_abort ();
18057
18058 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18059 debug_end_vpos = w->window_end_vpos));
18060
18061 /* Record that display has not been completed. */
18062 w->window_end_valid = 0;
18063 w->desired_matrix->no_scrolling_p = 1;
18064 return 3;
18065
18066 #undef GIVE_UP
18067 }
18068
18069
18070 \f
18071 /***********************************************************************
18072 More debugging support
18073 ***********************************************************************/
18074
18075 #ifdef GLYPH_DEBUG
18076
18077 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18078 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18079 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18080
18081
18082 /* Dump the contents of glyph matrix MATRIX on stderr.
18083
18084 GLYPHS 0 means don't show glyph contents.
18085 GLYPHS 1 means show glyphs in short form
18086 GLYPHS > 1 means show glyphs in long form. */
18087
18088 void
18089 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18090 {
18091 int i;
18092 for (i = 0; i < matrix->nrows; ++i)
18093 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18094 }
18095
18096
18097 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18098 the glyph row and area where the glyph comes from. */
18099
18100 void
18101 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18102 {
18103 if (glyph->type == CHAR_GLYPH
18104 || glyph->type == GLYPHLESS_GLYPH)
18105 {
18106 fprintf (stderr,
18107 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18108 glyph - row->glyphs[TEXT_AREA],
18109 (glyph->type == CHAR_GLYPH
18110 ? 'C'
18111 : 'G'),
18112 glyph->charpos,
18113 (BUFFERP (glyph->object)
18114 ? 'B'
18115 : (STRINGP (glyph->object)
18116 ? 'S'
18117 : (INTEGERP (glyph->object)
18118 ? '0'
18119 : '-'))),
18120 glyph->pixel_width,
18121 glyph->u.ch,
18122 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18123 ? glyph->u.ch
18124 : '.'),
18125 glyph->face_id,
18126 glyph->left_box_line_p,
18127 glyph->right_box_line_p);
18128 }
18129 else if (glyph->type == STRETCH_GLYPH)
18130 {
18131 fprintf (stderr,
18132 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18133 glyph - row->glyphs[TEXT_AREA],
18134 'S',
18135 glyph->charpos,
18136 (BUFFERP (glyph->object)
18137 ? 'B'
18138 : (STRINGP (glyph->object)
18139 ? 'S'
18140 : (INTEGERP (glyph->object)
18141 ? '0'
18142 : '-'))),
18143 glyph->pixel_width,
18144 0,
18145 ' ',
18146 glyph->face_id,
18147 glyph->left_box_line_p,
18148 glyph->right_box_line_p);
18149 }
18150 else if (glyph->type == IMAGE_GLYPH)
18151 {
18152 fprintf (stderr,
18153 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18154 glyph - row->glyphs[TEXT_AREA],
18155 'I',
18156 glyph->charpos,
18157 (BUFFERP (glyph->object)
18158 ? 'B'
18159 : (STRINGP (glyph->object)
18160 ? 'S'
18161 : (INTEGERP (glyph->object)
18162 ? '0'
18163 : '-'))),
18164 glyph->pixel_width,
18165 glyph->u.img_id,
18166 '.',
18167 glyph->face_id,
18168 glyph->left_box_line_p,
18169 glyph->right_box_line_p);
18170 }
18171 else if (glyph->type == COMPOSITE_GLYPH)
18172 {
18173 fprintf (stderr,
18174 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18175 glyph - row->glyphs[TEXT_AREA],
18176 '+',
18177 glyph->charpos,
18178 (BUFFERP (glyph->object)
18179 ? 'B'
18180 : (STRINGP (glyph->object)
18181 ? 'S'
18182 : (INTEGERP (glyph->object)
18183 ? '0'
18184 : '-'))),
18185 glyph->pixel_width,
18186 glyph->u.cmp.id);
18187 if (glyph->u.cmp.automatic)
18188 fprintf (stderr,
18189 "[%d-%d]",
18190 glyph->slice.cmp.from, glyph->slice.cmp.to);
18191 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18192 glyph->face_id,
18193 glyph->left_box_line_p,
18194 glyph->right_box_line_p);
18195 }
18196 }
18197
18198
18199 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18200 GLYPHS 0 means don't show glyph contents.
18201 GLYPHS 1 means show glyphs in short form
18202 GLYPHS > 1 means show glyphs in long form. */
18203
18204 void
18205 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18206 {
18207 if (glyphs != 1)
18208 {
18209 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18210 fprintf (stderr, "==============================================================================\n");
18211
18212 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18213 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18214 vpos,
18215 MATRIX_ROW_START_CHARPOS (row),
18216 MATRIX_ROW_END_CHARPOS (row),
18217 row->used[TEXT_AREA],
18218 row->contains_overlapping_glyphs_p,
18219 row->enabled_p,
18220 row->truncated_on_left_p,
18221 row->truncated_on_right_p,
18222 row->continued_p,
18223 MATRIX_ROW_CONTINUATION_LINE_P (row),
18224 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18225 row->ends_at_zv_p,
18226 row->fill_line_p,
18227 row->ends_in_middle_of_char_p,
18228 row->starts_in_middle_of_char_p,
18229 row->mouse_face_p,
18230 row->x,
18231 row->y,
18232 row->pixel_width,
18233 row->height,
18234 row->visible_height,
18235 row->ascent,
18236 row->phys_ascent);
18237 /* The next 3 lines should align to "Start" in the header. */
18238 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18239 row->end.overlay_string_index,
18240 row->continuation_lines_width);
18241 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18242 CHARPOS (row->start.string_pos),
18243 CHARPOS (row->end.string_pos));
18244 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18245 row->end.dpvec_index);
18246 }
18247
18248 if (glyphs > 1)
18249 {
18250 int area;
18251
18252 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18253 {
18254 struct glyph *glyph = row->glyphs[area];
18255 struct glyph *glyph_end = glyph + row->used[area];
18256
18257 /* Glyph for a line end in text. */
18258 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18259 ++glyph_end;
18260
18261 if (glyph < glyph_end)
18262 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18263
18264 for (; glyph < glyph_end; ++glyph)
18265 dump_glyph (row, glyph, area);
18266 }
18267 }
18268 else if (glyphs == 1)
18269 {
18270 int area;
18271
18272 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18273 {
18274 char *s = alloca (row->used[area] + 4);
18275 int i;
18276
18277 for (i = 0; i < row->used[area]; ++i)
18278 {
18279 struct glyph *glyph = row->glyphs[area] + i;
18280 if (i == row->used[area] - 1
18281 && area == TEXT_AREA
18282 && INTEGERP (glyph->object)
18283 && glyph->type == CHAR_GLYPH
18284 && glyph->u.ch == ' ')
18285 {
18286 strcpy (&s[i], "[\\n]");
18287 i += 4;
18288 }
18289 else if (glyph->type == CHAR_GLYPH
18290 && glyph->u.ch < 0x80
18291 && glyph->u.ch >= ' ')
18292 s[i] = glyph->u.ch;
18293 else
18294 s[i] = '.';
18295 }
18296
18297 s[i] = '\0';
18298 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18299 }
18300 }
18301 }
18302
18303
18304 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18305 Sdump_glyph_matrix, 0, 1, "p",
18306 doc: /* Dump the current matrix of the selected window to stderr.
18307 Shows contents of glyph row structures. With non-nil
18308 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18309 glyphs in short form, otherwise show glyphs in long form. */)
18310 (Lisp_Object glyphs)
18311 {
18312 struct window *w = XWINDOW (selected_window);
18313 struct buffer *buffer = XBUFFER (w->contents);
18314
18315 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18316 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18317 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18318 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18319 fprintf (stderr, "=============================================\n");
18320 dump_glyph_matrix (w->current_matrix,
18321 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18322 return Qnil;
18323 }
18324
18325
18326 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18327 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18328 (void)
18329 {
18330 struct frame *f = XFRAME (selected_frame);
18331 dump_glyph_matrix (f->current_matrix, 1);
18332 return Qnil;
18333 }
18334
18335
18336 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18337 doc: /* Dump glyph row ROW to stderr.
18338 GLYPH 0 means don't dump glyphs.
18339 GLYPH 1 means dump glyphs in short form.
18340 GLYPH > 1 or omitted means dump glyphs in long form. */)
18341 (Lisp_Object row, Lisp_Object glyphs)
18342 {
18343 struct glyph_matrix *matrix;
18344 EMACS_INT vpos;
18345
18346 CHECK_NUMBER (row);
18347 matrix = XWINDOW (selected_window)->current_matrix;
18348 vpos = XINT (row);
18349 if (vpos >= 0 && vpos < matrix->nrows)
18350 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18351 vpos,
18352 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18353 return Qnil;
18354 }
18355
18356
18357 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18358 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18359 GLYPH 0 means don't dump glyphs.
18360 GLYPH 1 means dump glyphs in short form.
18361 GLYPH > 1 or omitted means dump glyphs in long form.
18362
18363 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18364 do nothing. */)
18365 (Lisp_Object row, Lisp_Object glyphs)
18366 {
18367 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18368 struct frame *sf = SELECTED_FRAME ();
18369 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18370 EMACS_INT vpos;
18371
18372 CHECK_NUMBER (row);
18373 vpos = XINT (row);
18374 if (vpos >= 0 && vpos < m->nrows)
18375 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18376 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18377 #endif
18378 return Qnil;
18379 }
18380
18381
18382 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18383 doc: /* Toggle tracing of redisplay.
18384 With ARG, turn tracing on if and only if ARG is positive. */)
18385 (Lisp_Object arg)
18386 {
18387 if (NILP (arg))
18388 trace_redisplay_p = !trace_redisplay_p;
18389 else
18390 {
18391 arg = Fprefix_numeric_value (arg);
18392 trace_redisplay_p = XINT (arg) > 0;
18393 }
18394
18395 return Qnil;
18396 }
18397
18398
18399 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18400 doc: /* Like `format', but print result to stderr.
18401 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18402 (ptrdiff_t nargs, Lisp_Object *args)
18403 {
18404 Lisp_Object s = Fformat (nargs, args);
18405 fprintf (stderr, "%s", SDATA (s));
18406 return Qnil;
18407 }
18408
18409 #endif /* GLYPH_DEBUG */
18410
18411
18412 \f
18413 /***********************************************************************
18414 Building Desired Matrix Rows
18415 ***********************************************************************/
18416
18417 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18418 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18419
18420 static struct glyph_row *
18421 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18422 {
18423 struct frame *f = XFRAME (WINDOW_FRAME (w));
18424 struct buffer *buffer = XBUFFER (w->contents);
18425 struct buffer *old = current_buffer;
18426 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18427 int arrow_len = SCHARS (overlay_arrow_string);
18428 const unsigned char *arrow_end = arrow_string + arrow_len;
18429 const unsigned char *p;
18430 struct it it;
18431 bool multibyte_p;
18432 int n_glyphs_before;
18433
18434 set_buffer_temp (buffer);
18435 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18436 it.glyph_row->used[TEXT_AREA] = 0;
18437 SET_TEXT_POS (it.position, 0, 0);
18438
18439 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18440 p = arrow_string;
18441 while (p < arrow_end)
18442 {
18443 Lisp_Object face, ilisp;
18444
18445 /* Get the next character. */
18446 if (multibyte_p)
18447 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18448 else
18449 {
18450 it.c = it.char_to_display = *p, it.len = 1;
18451 if (! ASCII_CHAR_P (it.c))
18452 it.char_to_display = BYTE8_TO_CHAR (it.c);
18453 }
18454 p += it.len;
18455
18456 /* Get its face. */
18457 ilisp = make_number (p - arrow_string);
18458 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18459 it.face_id = compute_char_face (f, it.char_to_display, face);
18460
18461 /* Compute its width, get its glyphs. */
18462 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18463 SET_TEXT_POS (it.position, -1, -1);
18464 PRODUCE_GLYPHS (&it);
18465
18466 /* If this character doesn't fit any more in the line, we have
18467 to remove some glyphs. */
18468 if (it.current_x > it.last_visible_x)
18469 {
18470 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18471 break;
18472 }
18473 }
18474
18475 set_buffer_temp (old);
18476 return it.glyph_row;
18477 }
18478
18479
18480 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18481 glyphs to insert is determined by produce_special_glyphs. */
18482
18483 static void
18484 insert_left_trunc_glyphs (struct it *it)
18485 {
18486 struct it truncate_it;
18487 struct glyph *from, *end, *to, *toend;
18488
18489 eassert (!FRAME_WINDOW_P (it->f)
18490 || (!it->glyph_row->reversed_p
18491 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18492 || (it->glyph_row->reversed_p
18493 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18494
18495 /* Get the truncation glyphs. */
18496 truncate_it = *it;
18497 truncate_it.current_x = 0;
18498 truncate_it.face_id = DEFAULT_FACE_ID;
18499 truncate_it.glyph_row = &scratch_glyph_row;
18500 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18501 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18502 truncate_it.object = make_number (0);
18503 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18504
18505 /* Overwrite glyphs from IT with truncation glyphs. */
18506 if (!it->glyph_row->reversed_p)
18507 {
18508 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18509
18510 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18511 end = from + tused;
18512 to = it->glyph_row->glyphs[TEXT_AREA];
18513 toend = to + it->glyph_row->used[TEXT_AREA];
18514 if (FRAME_WINDOW_P (it->f))
18515 {
18516 /* On GUI frames, when variable-size fonts are displayed,
18517 the truncation glyphs may need more pixels than the row's
18518 glyphs they overwrite. We overwrite more glyphs to free
18519 enough screen real estate, and enlarge the stretch glyph
18520 on the right (see display_line), if there is one, to
18521 preserve the screen position of the truncation glyphs on
18522 the right. */
18523 int w = 0;
18524 struct glyph *g = to;
18525 short used;
18526
18527 /* The first glyph could be partially visible, in which case
18528 it->glyph_row->x will be negative. But we want the left
18529 truncation glyphs to be aligned at the left margin of the
18530 window, so we override the x coordinate at which the row
18531 will begin. */
18532 it->glyph_row->x = 0;
18533 while (g < toend && w < it->truncation_pixel_width)
18534 {
18535 w += g->pixel_width;
18536 ++g;
18537 }
18538 if (g - to - tused > 0)
18539 {
18540 memmove (to + tused, g, (toend - g) * sizeof(*g));
18541 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18542 }
18543 used = it->glyph_row->used[TEXT_AREA];
18544 if (it->glyph_row->truncated_on_right_p
18545 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18546 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18547 == STRETCH_GLYPH)
18548 {
18549 int extra = w - it->truncation_pixel_width;
18550
18551 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18552 }
18553 }
18554
18555 while (from < end)
18556 *to++ = *from++;
18557
18558 /* There may be padding glyphs left over. Overwrite them too. */
18559 if (!FRAME_WINDOW_P (it->f))
18560 {
18561 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18562 {
18563 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18564 while (from < end)
18565 *to++ = *from++;
18566 }
18567 }
18568
18569 if (to > toend)
18570 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18571 }
18572 else
18573 {
18574 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18575
18576 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18577 that back to front. */
18578 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18579 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18580 toend = it->glyph_row->glyphs[TEXT_AREA];
18581 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18582 if (FRAME_WINDOW_P (it->f))
18583 {
18584 int w = 0;
18585 struct glyph *g = to;
18586
18587 while (g >= toend && w < it->truncation_pixel_width)
18588 {
18589 w += g->pixel_width;
18590 --g;
18591 }
18592 if (to - g - tused > 0)
18593 to = g + tused;
18594 if (it->glyph_row->truncated_on_right_p
18595 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18596 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18597 {
18598 int extra = w - it->truncation_pixel_width;
18599
18600 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18601 }
18602 }
18603
18604 while (from >= end && to >= toend)
18605 *to-- = *from--;
18606 if (!FRAME_WINDOW_P (it->f))
18607 {
18608 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18609 {
18610 from =
18611 truncate_it.glyph_row->glyphs[TEXT_AREA]
18612 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18613 while (from >= end && to >= toend)
18614 *to-- = *from--;
18615 }
18616 }
18617 if (from >= end)
18618 {
18619 /* Need to free some room before prepending additional
18620 glyphs. */
18621 int move_by = from - end + 1;
18622 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18623 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18624
18625 for ( ; g >= g0; g--)
18626 g[move_by] = *g;
18627 while (from >= end)
18628 *to-- = *from--;
18629 it->glyph_row->used[TEXT_AREA] += move_by;
18630 }
18631 }
18632 }
18633
18634 /* Compute the hash code for ROW. */
18635 unsigned
18636 row_hash (struct glyph_row *row)
18637 {
18638 int area, k;
18639 unsigned hashval = 0;
18640
18641 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18642 for (k = 0; k < row->used[area]; ++k)
18643 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18644 + row->glyphs[area][k].u.val
18645 + row->glyphs[area][k].face_id
18646 + row->glyphs[area][k].padding_p
18647 + (row->glyphs[area][k].type << 2));
18648
18649 return hashval;
18650 }
18651
18652 /* Compute the pixel height and width of IT->glyph_row.
18653
18654 Most of the time, ascent and height of a display line will be equal
18655 to the max_ascent and max_height values of the display iterator
18656 structure. This is not the case if
18657
18658 1. We hit ZV without displaying anything. In this case, max_ascent
18659 and max_height will be zero.
18660
18661 2. We have some glyphs that don't contribute to the line height.
18662 (The glyph row flag contributes_to_line_height_p is for future
18663 pixmap extensions).
18664
18665 The first case is easily covered by using default values because in
18666 these cases, the line height does not really matter, except that it
18667 must not be zero. */
18668
18669 static void
18670 compute_line_metrics (struct it *it)
18671 {
18672 struct glyph_row *row = it->glyph_row;
18673
18674 if (FRAME_WINDOW_P (it->f))
18675 {
18676 int i, min_y, max_y;
18677
18678 /* The line may consist of one space only, that was added to
18679 place the cursor on it. If so, the row's height hasn't been
18680 computed yet. */
18681 if (row->height == 0)
18682 {
18683 if (it->max_ascent + it->max_descent == 0)
18684 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18685 row->ascent = it->max_ascent;
18686 row->height = it->max_ascent + it->max_descent;
18687 row->phys_ascent = it->max_phys_ascent;
18688 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18689 row->extra_line_spacing = it->max_extra_line_spacing;
18690 }
18691
18692 /* Compute the width of this line. */
18693 row->pixel_width = row->x;
18694 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18695 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18696
18697 eassert (row->pixel_width >= 0);
18698 eassert (row->ascent >= 0 && row->height > 0);
18699
18700 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18701 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18702
18703 /* If first line's physical ascent is larger than its logical
18704 ascent, use the physical ascent, and make the row taller.
18705 This makes accented characters fully visible. */
18706 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18707 && row->phys_ascent > row->ascent)
18708 {
18709 row->height += row->phys_ascent - row->ascent;
18710 row->ascent = row->phys_ascent;
18711 }
18712
18713 /* Compute how much of the line is visible. */
18714 row->visible_height = row->height;
18715
18716 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18717 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18718
18719 if (row->y < min_y)
18720 row->visible_height -= min_y - row->y;
18721 if (row->y + row->height > max_y)
18722 row->visible_height -= row->y + row->height - max_y;
18723 }
18724 else
18725 {
18726 row->pixel_width = row->used[TEXT_AREA];
18727 if (row->continued_p)
18728 row->pixel_width -= it->continuation_pixel_width;
18729 else if (row->truncated_on_right_p)
18730 row->pixel_width -= it->truncation_pixel_width;
18731 row->ascent = row->phys_ascent = 0;
18732 row->height = row->phys_height = row->visible_height = 1;
18733 row->extra_line_spacing = 0;
18734 }
18735
18736 /* Compute a hash code for this row. */
18737 row->hash = row_hash (row);
18738
18739 it->max_ascent = it->max_descent = 0;
18740 it->max_phys_ascent = it->max_phys_descent = 0;
18741 }
18742
18743
18744 /* Append one space to the glyph row of iterator IT if doing a
18745 window-based redisplay. The space has the same face as
18746 IT->face_id. Value is non-zero if a space was added.
18747
18748 This function is called to make sure that there is always one glyph
18749 at the end of a glyph row that the cursor can be set on under
18750 window-systems. (If there weren't such a glyph we would not know
18751 how wide and tall a box cursor should be displayed).
18752
18753 At the same time this space let's a nicely handle clearing to the
18754 end of the line if the row ends in italic text. */
18755
18756 static int
18757 append_space_for_newline (struct it *it, int default_face_p)
18758 {
18759 if (FRAME_WINDOW_P (it->f))
18760 {
18761 int n = it->glyph_row->used[TEXT_AREA];
18762
18763 if (it->glyph_row->glyphs[TEXT_AREA] + n
18764 < it->glyph_row->glyphs[1 + TEXT_AREA])
18765 {
18766 /* Save some values that must not be changed.
18767 Must save IT->c and IT->len because otherwise
18768 ITERATOR_AT_END_P wouldn't work anymore after
18769 append_space_for_newline has been called. */
18770 enum display_element_type saved_what = it->what;
18771 int saved_c = it->c, saved_len = it->len;
18772 int saved_char_to_display = it->char_to_display;
18773 int saved_x = it->current_x;
18774 int saved_face_id = it->face_id;
18775 int saved_box_end = it->end_of_box_run_p;
18776 struct text_pos saved_pos;
18777 Lisp_Object saved_object;
18778 struct face *face;
18779
18780 saved_object = it->object;
18781 saved_pos = it->position;
18782
18783 it->what = IT_CHARACTER;
18784 memset (&it->position, 0, sizeof it->position);
18785 it->object = make_number (0);
18786 it->c = it->char_to_display = ' ';
18787 it->len = 1;
18788
18789 /* If the default face was remapped, be sure to use the
18790 remapped face for the appended newline. */
18791 if (default_face_p)
18792 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18793 else if (it->face_before_selective_p)
18794 it->face_id = it->saved_face_id;
18795 face = FACE_FROM_ID (it->f, it->face_id);
18796 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18797 /* In R2L rows, we will prepend a stretch glyph that will
18798 have the end_of_box_run_p flag set for it, so there's no
18799 need for the appended newline glyph to have that flag
18800 set. */
18801 if (it->glyph_row->reversed_p
18802 /* But if the appended newline glyph goes all the way to
18803 the end of the row, there will be no stretch glyph,
18804 so leave the box flag set. */
18805 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18806 it->end_of_box_run_p = 0;
18807
18808 PRODUCE_GLYPHS (it);
18809
18810 it->override_ascent = -1;
18811 it->constrain_row_ascent_descent_p = 0;
18812 it->current_x = saved_x;
18813 it->object = saved_object;
18814 it->position = saved_pos;
18815 it->what = saved_what;
18816 it->face_id = saved_face_id;
18817 it->len = saved_len;
18818 it->c = saved_c;
18819 it->char_to_display = saved_char_to_display;
18820 it->end_of_box_run_p = saved_box_end;
18821 return 1;
18822 }
18823 }
18824
18825 return 0;
18826 }
18827
18828
18829 /* Extend the face of the last glyph in the text area of IT->glyph_row
18830 to the end of the display line. Called from display_line. If the
18831 glyph row is empty, add a space glyph to it so that we know the
18832 face to draw. Set the glyph row flag fill_line_p. If the glyph
18833 row is R2L, prepend a stretch glyph to cover the empty space to the
18834 left of the leftmost glyph. */
18835
18836 static void
18837 extend_face_to_end_of_line (struct it *it)
18838 {
18839 struct face *face, *default_face;
18840 struct frame *f = it->f;
18841
18842 /* If line is already filled, do nothing. Non window-system frames
18843 get a grace of one more ``pixel'' because their characters are
18844 1-``pixel'' wide, so they hit the equality too early. This grace
18845 is needed only for R2L rows that are not continued, to produce
18846 one extra blank where we could display the cursor. */
18847 if ((it->current_x >= it->last_visible_x
18848 + (!FRAME_WINDOW_P (f)
18849 && it->glyph_row->reversed_p
18850 && !it->glyph_row->continued_p))
18851 /* If the window has display margins, we will need to extend
18852 their face even if the text area is filled. */
18853 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18854 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
18855 return;
18856
18857 /* The default face, possibly remapped. */
18858 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18859
18860 /* Face extension extends the background and box of IT->face_id
18861 to the end of the line. If the background equals the background
18862 of the frame, we don't have to do anything. */
18863 if (it->face_before_selective_p)
18864 face = FACE_FROM_ID (f, it->saved_face_id);
18865 else
18866 face = FACE_FROM_ID (f, it->face_id);
18867
18868 if (FRAME_WINDOW_P (f)
18869 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18870 && face->box == FACE_NO_BOX
18871 && face->background == FRAME_BACKGROUND_PIXEL (f)
18872 #ifdef HAVE_WINDOW_SYSTEM
18873 && !face->stipple
18874 #endif
18875 && !it->glyph_row->reversed_p)
18876 return;
18877
18878 /* Set the glyph row flag indicating that the face of the last glyph
18879 in the text area has to be drawn to the end of the text area. */
18880 it->glyph_row->fill_line_p = 1;
18881
18882 /* If current character of IT is not ASCII, make sure we have the
18883 ASCII face. This will be automatically undone the next time
18884 get_next_display_element returns a multibyte character. Note
18885 that the character will always be single byte in unibyte
18886 text. */
18887 if (!ASCII_CHAR_P (it->c))
18888 {
18889 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18890 }
18891
18892 if (FRAME_WINDOW_P (f))
18893 {
18894 /* If the row is empty, add a space with the current face of IT,
18895 so that we know which face to draw. */
18896 if (it->glyph_row->used[TEXT_AREA] == 0)
18897 {
18898 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18899 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18900 it->glyph_row->used[TEXT_AREA] = 1;
18901 }
18902 /* Mode line and the header line don't have margins, and
18903 likewise the frame's tool-bar window, if there is any. */
18904 if (!(it->glyph_row->mode_line_p
18905 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18906 || (WINDOWP (f->tool_bar_window)
18907 && it->w == XWINDOW (f->tool_bar_window))
18908 #endif
18909 ))
18910 {
18911 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18912 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
18913 {
18914 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
18915 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
18916 default_face->id;
18917 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
18918 }
18919 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
18920 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
18921 {
18922 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
18923 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
18924 default_face->id;
18925 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
18926 }
18927 }
18928 #ifdef HAVE_WINDOW_SYSTEM
18929 if (it->glyph_row->reversed_p)
18930 {
18931 /* Prepend a stretch glyph to the row, such that the
18932 rightmost glyph will be drawn flushed all the way to the
18933 right margin of the window. The stretch glyph that will
18934 occupy the empty space, if any, to the left of the
18935 glyphs. */
18936 struct font *font = face->font ? face->font : FRAME_FONT (f);
18937 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18938 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18939 struct glyph *g;
18940 int row_width, stretch_ascent, stretch_width;
18941 struct text_pos saved_pos;
18942 int saved_face_id, saved_avoid_cursor, saved_box_start;
18943
18944 for (row_width = 0, g = row_start; g < row_end; g++)
18945 row_width += g->pixel_width;
18946 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18947 if (stretch_width > 0)
18948 {
18949 stretch_ascent =
18950 (((it->ascent + it->descent)
18951 * FONT_BASE (font)) / FONT_HEIGHT (font));
18952 saved_pos = it->position;
18953 memset (&it->position, 0, sizeof it->position);
18954 saved_avoid_cursor = it->avoid_cursor_p;
18955 it->avoid_cursor_p = 1;
18956 saved_face_id = it->face_id;
18957 saved_box_start = it->start_of_box_run_p;
18958 /* The last row's stretch glyph should get the default
18959 face, to avoid painting the rest of the window with
18960 the region face, if the region ends at ZV. */
18961 if (it->glyph_row->ends_at_zv_p)
18962 it->face_id = default_face->id;
18963 else
18964 it->face_id = face->id;
18965 it->start_of_box_run_p = 0;
18966 append_stretch_glyph (it, make_number (0), stretch_width,
18967 it->ascent + it->descent, stretch_ascent);
18968 it->position = saved_pos;
18969 it->avoid_cursor_p = saved_avoid_cursor;
18970 it->face_id = saved_face_id;
18971 it->start_of_box_run_p = saved_box_start;
18972 }
18973 }
18974 #endif /* HAVE_WINDOW_SYSTEM */
18975 }
18976 else
18977 {
18978 /* Save some values that must not be changed. */
18979 int saved_x = it->current_x;
18980 struct text_pos saved_pos;
18981 Lisp_Object saved_object;
18982 enum display_element_type saved_what = it->what;
18983 int saved_face_id = it->face_id;
18984
18985 saved_object = it->object;
18986 saved_pos = it->position;
18987
18988 it->what = IT_CHARACTER;
18989 memset (&it->position, 0, sizeof it->position);
18990 it->object = make_number (0);
18991 it->c = it->char_to_display = ' ';
18992 it->len = 1;
18993
18994 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
18995 && (it->glyph_row->used[LEFT_MARGIN_AREA]
18996 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
18997 && !it->glyph_row->mode_line_p
18998 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
18999 {
19000 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19001 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19002
19003 for (it->current_x = 0; g < e; g++)
19004 it->current_x += g->pixel_width;
19005
19006 it->area = LEFT_MARGIN_AREA;
19007 it->face_id = default_face->id;
19008 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19009 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19010 {
19011 PRODUCE_GLYPHS (it);
19012 /* term.c:produce_glyphs advances it->current_x only for
19013 TEXT_AREA. */
19014 it->current_x += it->pixel_width;
19015 }
19016
19017 it->current_x = saved_x;
19018 it->area = TEXT_AREA;
19019 }
19020
19021 /* The last row's blank glyphs should get the default face, to
19022 avoid painting the rest of the window with the region face,
19023 if the region ends at ZV. */
19024 if (it->glyph_row->ends_at_zv_p)
19025 it->face_id = default_face->id;
19026 else
19027 it->face_id = face->id;
19028 PRODUCE_GLYPHS (it);
19029
19030 while (it->current_x <= it->last_visible_x)
19031 PRODUCE_GLYPHS (it);
19032
19033 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19034 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19035 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19036 && !it->glyph_row->mode_line_p
19037 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19038 {
19039 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19040 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19041
19042 for ( ; g < e; g++)
19043 it->current_x += g->pixel_width;
19044
19045 it->area = RIGHT_MARGIN_AREA;
19046 it->face_id = default_face->id;
19047 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19048 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19049 {
19050 PRODUCE_GLYPHS (it);
19051 it->current_x += it->pixel_width;
19052 }
19053
19054 it->area = TEXT_AREA;
19055 }
19056
19057 /* Don't count these blanks really. It would let us insert a left
19058 truncation glyph below and make us set the cursor on them, maybe. */
19059 it->current_x = saved_x;
19060 it->object = saved_object;
19061 it->position = saved_pos;
19062 it->what = saved_what;
19063 it->face_id = saved_face_id;
19064 }
19065 }
19066
19067
19068 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19069 trailing whitespace. */
19070
19071 static int
19072 trailing_whitespace_p (ptrdiff_t charpos)
19073 {
19074 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19075 int c = 0;
19076
19077 while (bytepos < ZV_BYTE
19078 && (c = FETCH_CHAR (bytepos),
19079 c == ' ' || c == '\t'))
19080 ++bytepos;
19081
19082 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19083 {
19084 if (bytepos != PT_BYTE)
19085 return 1;
19086 }
19087 return 0;
19088 }
19089
19090
19091 /* Highlight trailing whitespace, if any, in ROW. */
19092
19093 static void
19094 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19095 {
19096 int used = row->used[TEXT_AREA];
19097
19098 if (used)
19099 {
19100 struct glyph *start = row->glyphs[TEXT_AREA];
19101 struct glyph *glyph = start + used - 1;
19102
19103 if (row->reversed_p)
19104 {
19105 /* Right-to-left rows need to be processed in the opposite
19106 direction, so swap the edge pointers. */
19107 glyph = start;
19108 start = row->glyphs[TEXT_AREA] + used - 1;
19109 }
19110
19111 /* Skip over glyphs inserted to display the cursor at the
19112 end of a line, for extending the face of the last glyph
19113 to the end of the line on terminals, and for truncation
19114 and continuation glyphs. */
19115 if (!row->reversed_p)
19116 {
19117 while (glyph >= start
19118 && glyph->type == CHAR_GLYPH
19119 && INTEGERP (glyph->object))
19120 --glyph;
19121 }
19122 else
19123 {
19124 while (glyph <= start
19125 && glyph->type == CHAR_GLYPH
19126 && INTEGERP (glyph->object))
19127 ++glyph;
19128 }
19129
19130 /* If last glyph is a space or stretch, and it's trailing
19131 whitespace, set the face of all trailing whitespace glyphs in
19132 IT->glyph_row to `trailing-whitespace'. */
19133 if ((row->reversed_p ? glyph <= start : glyph >= start)
19134 && BUFFERP (glyph->object)
19135 && (glyph->type == STRETCH_GLYPH
19136 || (glyph->type == CHAR_GLYPH
19137 && glyph->u.ch == ' '))
19138 && trailing_whitespace_p (glyph->charpos))
19139 {
19140 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19141 if (face_id < 0)
19142 return;
19143
19144 if (!row->reversed_p)
19145 {
19146 while (glyph >= start
19147 && BUFFERP (glyph->object)
19148 && (glyph->type == STRETCH_GLYPH
19149 || (glyph->type == CHAR_GLYPH
19150 && glyph->u.ch == ' ')))
19151 (glyph--)->face_id = face_id;
19152 }
19153 else
19154 {
19155 while (glyph <= start
19156 && BUFFERP (glyph->object)
19157 && (glyph->type == STRETCH_GLYPH
19158 || (glyph->type == CHAR_GLYPH
19159 && glyph->u.ch == ' ')))
19160 (glyph++)->face_id = face_id;
19161 }
19162 }
19163 }
19164 }
19165
19166
19167 /* Value is non-zero if glyph row ROW should be
19168 considered to hold the buffer position CHARPOS. */
19169
19170 static int
19171 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19172 {
19173 int result = 1;
19174
19175 if (charpos == CHARPOS (row->end.pos)
19176 || charpos == MATRIX_ROW_END_CHARPOS (row))
19177 {
19178 /* Suppose the row ends on a string.
19179 Unless the row is continued, that means it ends on a newline
19180 in the string. If it's anything other than a display string
19181 (e.g., a before-string from an overlay), we don't want the
19182 cursor there. (This heuristic seems to give the optimal
19183 behavior for the various types of multi-line strings.)
19184 One exception: if the string has `cursor' property on one of
19185 its characters, we _do_ want the cursor there. */
19186 if (CHARPOS (row->end.string_pos) >= 0)
19187 {
19188 if (row->continued_p)
19189 result = 1;
19190 else
19191 {
19192 /* Check for `display' property. */
19193 struct glyph *beg = row->glyphs[TEXT_AREA];
19194 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19195 struct glyph *glyph;
19196
19197 result = 0;
19198 for (glyph = end; glyph >= beg; --glyph)
19199 if (STRINGP (glyph->object))
19200 {
19201 Lisp_Object prop
19202 = Fget_char_property (make_number (charpos),
19203 Qdisplay, Qnil);
19204 result =
19205 (!NILP (prop)
19206 && display_prop_string_p (prop, glyph->object));
19207 /* If there's a `cursor' property on one of the
19208 string's characters, this row is a cursor row,
19209 even though this is not a display string. */
19210 if (!result)
19211 {
19212 Lisp_Object s = glyph->object;
19213
19214 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19215 {
19216 ptrdiff_t gpos = glyph->charpos;
19217
19218 if (!NILP (Fget_char_property (make_number (gpos),
19219 Qcursor, s)))
19220 {
19221 result = 1;
19222 break;
19223 }
19224 }
19225 }
19226 break;
19227 }
19228 }
19229 }
19230 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19231 {
19232 /* If the row ends in middle of a real character,
19233 and the line is continued, we want the cursor here.
19234 That's because CHARPOS (ROW->end.pos) would equal
19235 PT if PT is before the character. */
19236 if (!row->ends_in_ellipsis_p)
19237 result = row->continued_p;
19238 else
19239 /* If the row ends in an ellipsis, then
19240 CHARPOS (ROW->end.pos) will equal point after the
19241 invisible text. We want that position to be displayed
19242 after the ellipsis. */
19243 result = 0;
19244 }
19245 /* If the row ends at ZV, display the cursor at the end of that
19246 row instead of at the start of the row below. */
19247 else if (row->ends_at_zv_p)
19248 result = 1;
19249 else
19250 result = 0;
19251 }
19252
19253 return result;
19254 }
19255
19256 /* Value is non-zero if glyph row ROW should be
19257 used to hold the cursor. */
19258
19259 static int
19260 cursor_row_p (struct glyph_row *row)
19261 {
19262 return row_for_charpos_p (row, PT);
19263 }
19264
19265 \f
19266
19267 /* Push the property PROP so that it will be rendered at the current
19268 position in IT. Return 1 if PROP was successfully pushed, 0
19269 otherwise. Called from handle_line_prefix to handle the
19270 `line-prefix' and `wrap-prefix' properties. */
19271
19272 static int
19273 push_prefix_prop (struct it *it, Lisp_Object prop)
19274 {
19275 struct text_pos pos =
19276 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19277
19278 eassert (it->method == GET_FROM_BUFFER
19279 || it->method == GET_FROM_DISPLAY_VECTOR
19280 || it->method == GET_FROM_STRING);
19281
19282 /* We need to save the current buffer/string position, so it will be
19283 restored by pop_it, because iterate_out_of_display_property
19284 depends on that being set correctly, but some situations leave
19285 it->position not yet set when this function is called. */
19286 push_it (it, &pos);
19287
19288 if (STRINGP (prop))
19289 {
19290 if (SCHARS (prop) == 0)
19291 {
19292 pop_it (it);
19293 return 0;
19294 }
19295
19296 it->string = prop;
19297 it->string_from_prefix_prop_p = 1;
19298 it->multibyte_p = STRING_MULTIBYTE (it->string);
19299 it->current.overlay_string_index = -1;
19300 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19301 it->end_charpos = it->string_nchars = SCHARS (it->string);
19302 it->method = GET_FROM_STRING;
19303 it->stop_charpos = 0;
19304 it->prev_stop = 0;
19305 it->base_level_stop = 0;
19306
19307 /* Force paragraph direction to be that of the parent
19308 buffer/string. */
19309 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19310 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19311 else
19312 it->paragraph_embedding = L2R;
19313
19314 /* Set up the bidi iterator for this display string. */
19315 if (it->bidi_p)
19316 {
19317 it->bidi_it.string.lstring = it->string;
19318 it->bidi_it.string.s = NULL;
19319 it->bidi_it.string.schars = it->end_charpos;
19320 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19321 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19322 it->bidi_it.string.unibyte = !it->multibyte_p;
19323 it->bidi_it.w = it->w;
19324 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19325 }
19326 }
19327 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19328 {
19329 it->method = GET_FROM_STRETCH;
19330 it->object = prop;
19331 }
19332 #ifdef HAVE_WINDOW_SYSTEM
19333 else if (IMAGEP (prop))
19334 {
19335 it->what = IT_IMAGE;
19336 it->image_id = lookup_image (it->f, prop);
19337 it->method = GET_FROM_IMAGE;
19338 }
19339 #endif /* HAVE_WINDOW_SYSTEM */
19340 else
19341 {
19342 pop_it (it); /* bogus display property, give up */
19343 return 0;
19344 }
19345
19346 return 1;
19347 }
19348
19349 /* Return the character-property PROP at the current position in IT. */
19350
19351 static Lisp_Object
19352 get_it_property (struct it *it, Lisp_Object prop)
19353 {
19354 Lisp_Object position, object = it->object;
19355
19356 if (STRINGP (object))
19357 position = make_number (IT_STRING_CHARPOS (*it));
19358 else if (BUFFERP (object))
19359 {
19360 position = make_number (IT_CHARPOS (*it));
19361 object = it->window;
19362 }
19363 else
19364 return Qnil;
19365
19366 return Fget_char_property (position, prop, object);
19367 }
19368
19369 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19370
19371 static void
19372 handle_line_prefix (struct it *it)
19373 {
19374 Lisp_Object prefix;
19375
19376 if (it->continuation_lines_width > 0)
19377 {
19378 prefix = get_it_property (it, Qwrap_prefix);
19379 if (NILP (prefix))
19380 prefix = Vwrap_prefix;
19381 }
19382 else
19383 {
19384 prefix = get_it_property (it, Qline_prefix);
19385 if (NILP (prefix))
19386 prefix = Vline_prefix;
19387 }
19388 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19389 {
19390 /* If the prefix is wider than the window, and we try to wrap
19391 it, it would acquire its own wrap prefix, and so on till the
19392 iterator stack overflows. So, don't wrap the prefix. */
19393 it->line_wrap = TRUNCATE;
19394 it->avoid_cursor_p = 1;
19395 }
19396 }
19397
19398 \f
19399
19400 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19401 only for R2L lines from display_line and display_string, when they
19402 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19403 the line/string needs to be continued on the next glyph row. */
19404 static void
19405 unproduce_glyphs (struct it *it, int n)
19406 {
19407 struct glyph *glyph, *end;
19408
19409 eassert (it->glyph_row);
19410 eassert (it->glyph_row->reversed_p);
19411 eassert (it->area == TEXT_AREA);
19412 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19413
19414 if (n > it->glyph_row->used[TEXT_AREA])
19415 n = it->glyph_row->used[TEXT_AREA];
19416 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19417 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19418 for ( ; glyph < end; glyph++)
19419 glyph[-n] = *glyph;
19420 }
19421
19422 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19423 and ROW->maxpos. */
19424 static void
19425 find_row_edges (struct it *it, struct glyph_row *row,
19426 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19427 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19428 {
19429 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19430 lines' rows is implemented for bidi-reordered rows. */
19431
19432 /* ROW->minpos is the value of min_pos, the minimal buffer position
19433 we have in ROW, or ROW->start.pos if that is smaller. */
19434 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19435 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19436 else
19437 /* We didn't find buffer positions smaller than ROW->start, or
19438 didn't find _any_ valid buffer positions in any of the glyphs,
19439 so we must trust the iterator's computed positions. */
19440 row->minpos = row->start.pos;
19441 if (max_pos <= 0)
19442 {
19443 max_pos = CHARPOS (it->current.pos);
19444 max_bpos = BYTEPOS (it->current.pos);
19445 }
19446
19447 /* Here are the various use-cases for ending the row, and the
19448 corresponding values for ROW->maxpos:
19449
19450 Line ends in a newline from buffer eol_pos + 1
19451 Line is continued from buffer max_pos + 1
19452 Line is truncated on right it->current.pos
19453 Line ends in a newline from string max_pos + 1(*)
19454 (*) + 1 only when line ends in a forward scan
19455 Line is continued from string max_pos
19456 Line is continued from display vector max_pos
19457 Line is entirely from a string min_pos == max_pos
19458 Line is entirely from a display vector min_pos == max_pos
19459 Line that ends at ZV ZV
19460
19461 If you discover other use-cases, please add them here as
19462 appropriate. */
19463 if (row->ends_at_zv_p)
19464 row->maxpos = it->current.pos;
19465 else if (row->used[TEXT_AREA])
19466 {
19467 int seen_this_string = 0;
19468 struct glyph_row *r1 = row - 1;
19469
19470 /* Did we see the same display string on the previous row? */
19471 if (STRINGP (it->object)
19472 /* this is not the first row */
19473 && row > it->w->desired_matrix->rows
19474 /* previous row is not the header line */
19475 && !r1->mode_line_p
19476 /* previous row also ends in a newline from a string */
19477 && r1->ends_in_newline_from_string_p)
19478 {
19479 struct glyph *start, *end;
19480
19481 /* Search for the last glyph of the previous row that came
19482 from buffer or string. Depending on whether the row is
19483 L2R or R2L, we need to process it front to back or the
19484 other way round. */
19485 if (!r1->reversed_p)
19486 {
19487 start = r1->glyphs[TEXT_AREA];
19488 end = start + r1->used[TEXT_AREA];
19489 /* Glyphs inserted by redisplay have an integer (zero)
19490 as their object. */
19491 while (end > start
19492 && INTEGERP ((end - 1)->object)
19493 && (end - 1)->charpos <= 0)
19494 --end;
19495 if (end > start)
19496 {
19497 if (EQ ((end - 1)->object, it->object))
19498 seen_this_string = 1;
19499 }
19500 else
19501 /* If all the glyphs of the previous row were inserted
19502 by redisplay, it means the previous row was
19503 produced from a single newline, which is only
19504 possible if that newline came from the same string
19505 as the one which produced this ROW. */
19506 seen_this_string = 1;
19507 }
19508 else
19509 {
19510 end = r1->glyphs[TEXT_AREA] - 1;
19511 start = end + r1->used[TEXT_AREA];
19512 while (end < start
19513 && INTEGERP ((end + 1)->object)
19514 && (end + 1)->charpos <= 0)
19515 ++end;
19516 if (end < start)
19517 {
19518 if (EQ ((end + 1)->object, it->object))
19519 seen_this_string = 1;
19520 }
19521 else
19522 seen_this_string = 1;
19523 }
19524 }
19525 /* Take note of each display string that covers a newline only
19526 once, the first time we see it. This is for when a display
19527 string includes more than one newline in it. */
19528 if (row->ends_in_newline_from_string_p && !seen_this_string)
19529 {
19530 /* If we were scanning the buffer forward when we displayed
19531 the string, we want to account for at least one buffer
19532 position that belongs to this row (position covered by
19533 the display string), so that cursor positioning will
19534 consider this row as a candidate when point is at the end
19535 of the visual line represented by this row. This is not
19536 required when scanning back, because max_pos will already
19537 have a much larger value. */
19538 if (CHARPOS (row->end.pos) > max_pos)
19539 INC_BOTH (max_pos, max_bpos);
19540 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19541 }
19542 else if (CHARPOS (it->eol_pos) > 0)
19543 SET_TEXT_POS (row->maxpos,
19544 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19545 else if (row->continued_p)
19546 {
19547 /* If max_pos is different from IT's current position, it
19548 means IT->method does not belong to the display element
19549 at max_pos. However, it also means that the display
19550 element at max_pos was displayed in its entirety on this
19551 line, which is equivalent to saying that the next line
19552 starts at the next buffer position. */
19553 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19554 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19555 else
19556 {
19557 INC_BOTH (max_pos, max_bpos);
19558 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19559 }
19560 }
19561 else if (row->truncated_on_right_p)
19562 /* display_line already called reseat_at_next_visible_line_start,
19563 which puts the iterator at the beginning of the next line, in
19564 the logical order. */
19565 row->maxpos = it->current.pos;
19566 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19567 /* A line that is entirely from a string/image/stretch... */
19568 row->maxpos = row->minpos;
19569 else
19570 emacs_abort ();
19571 }
19572 else
19573 row->maxpos = it->current.pos;
19574 }
19575
19576 /* Construct the glyph row IT->glyph_row in the desired matrix of
19577 IT->w from text at the current position of IT. See dispextern.h
19578 for an overview of struct it. Value is non-zero if
19579 IT->glyph_row displays text, as opposed to a line displaying ZV
19580 only. */
19581
19582 static int
19583 display_line (struct it *it)
19584 {
19585 struct glyph_row *row = it->glyph_row;
19586 Lisp_Object overlay_arrow_string;
19587 struct it wrap_it;
19588 void *wrap_data = NULL;
19589 int may_wrap = 0, wrap_x IF_LINT (= 0);
19590 int wrap_row_used = -1;
19591 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19592 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19593 int wrap_row_extra_line_spacing IF_LINT (= 0);
19594 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19595 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19596 int cvpos;
19597 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19598 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19599
19600 /* We always start displaying at hpos zero even if hscrolled. */
19601 eassert (it->hpos == 0 && it->current_x == 0);
19602
19603 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19604 >= it->w->desired_matrix->nrows)
19605 {
19606 it->w->nrows_scale_factor++;
19607 it->f->fonts_changed = 1;
19608 return 0;
19609 }
19610
19611 /* Clear the result glyph row and enable it. */
19612 prepare_desired_row (row);
19613
19614 row->y = it->current_y;
19615 row->start = it->start;
19616 row->continuation_lines_width = it->continuation_lines_width;
19617 row->displays_text_p = 1;
19618 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19619 it->starts_in_middle_of_char_p = 0;
19620
19621 /* Arrange the overlays nicely for our purposes. Usually, we call
19622 display_line on only one line at a time, in which case this
19623 can't really hurt too much, or we call it on lines which appear
19624 one after another in the buffer, in which case all calls to
19625 recenter_overlay_lists but the first will be pretty cheap. */
19626 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19627
19628 /* Move over display elements that are not visible because we are
19629 hscrolled. This may stop at an x-position < IT->first_visible_x
19630 if the first glyph is partially visible or if we hit a line end. */
19631 if (it->current_x < it->first_visible_x)
19632 {
19633 enum move_it_result move_result;
19634
19635 this_line_min_pos = row->start.pos;
19636 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19637 MOVE_TO_POS | MOVE_TO_X);
19638 /* If we are under a large hscroll, move_it_in_display_line_to
19639 could hit the end of the line without reaching
19640 it->first_visible_x. Pretend that we did reach it. This is
19641 especially important on a TTY, where we will call
19642 extend_face_to_end_of_line, which needs to know how many
19643 blank glyphs to produce. */
19644 if (it->current_x < it->first_visible_x
19645 && (move_result == MOVE_NEWLINE_OR_CR
19646 || move_result == MOVE_POS_MATCH_OR_ZV))
19647 it->current_x = it->first_visible_x;
19648
19649 /* Record the smallest positions seen while we moved over
19650 display elements that are not visible. This is needed by
19651 redisplay_internal for optimizing the case where the cursor
19652 stays inside the same line. The rest of this function only
19653 considers positions that are actually displayed, so
19654 RECORD_MAX_MIN_POS will not otherwise record positions that
19655 are hscrolled to the left of the left edge of the window. */
19656 min_pos = CHARPOS (this_line_min_pos);
19657 min_bpos = BYTEPOS (this_line_min_pos);
19658 }
19659 else
19660 {
19661 /* We only do this when not calling `move_it_in_display_line_to'
19662 above, because move_it_in_display_line_to calls
19663 handle_line_prefix itself. */
19664 handle_line_prefix (it);
19665 }
19666
19667 /* Get the initial row height. This is either the height of the
19668 text hscrolled, if there is any, or zero. */
19669 row->ascent = it->max_ascent;
19670 row->height = it->max_ascent + it->max_descent;
19671 row->phys_ascent = it->max_phys_ascent;
19672 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19673 row->extra_line_spacing = it->max_extra_line_spacing;
19674
19675 /* Utility macro to record max and min buffer positions seen until now. */
19676 #define RECORD_MAX_MIN_POS(IT) \
19677 do \
19678 { \
19679 int composition_p = !STRINGP ((IT)->string) \
19680 && ((IT)->what == IT_COMPOSITION); \
19681 ptrdiff_t current_pos = \
19682 composition_p ? (IT)->cmp_it.charpos \
19683 : IT_CHARPOS (*(IT)); \
19684 ptrdiff_t current_bpos = \
19685 composition_p ? CHAR_TO_BYTE (current_pos) \
19686 : IT_BYTEPOS (*(IT)); \
19687 if (current_pos < min_pos) \
19688 { \
19689 min_pos = current_pos; \
19690 min_bpos = current_bpos; \
19691 } \
19692 if (IT_CHARPOS (*it) > max_pos) \
19693 { \
19694 max_pos = IT_CHARPOS (*it); \
19695 max_bpos = IT_BYTEPOS (*it); \
19696 } \
19697 } \
19698 while (0)
19699
19700 /* Loop generating characters. The loop is left with IT on the next
19701 character to display. */
19702 while (1)
19703 {
19704 int n_glyphs_before, hpos_before, x_before;
19705 int x, nglyphs;
19706 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19707
19708 /* Retrieve the next thing to display. Value is zero if end of
19709 buffer reached. */
19710 if (!get_next_display_element (it))
19711 {
19712 /* Maybe add a space at the end of this line that is used to
19713 display the cursor there under X. Set the charpos of the
19714 first glyph of blank lines not corresponding to any text
19715 to -1. */
19716 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19717 row->exact_window_width_line_p = 1;
19718 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19719 || row->used[TEXT_AREA] == 0)
19720 {
19721 row->glyphs[TEXT_AREA]->charpos = -1;
19722 row->displays_text_p = 0;
19723
19724 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19725 && (!MINI_WINDOW_P (it->w)
19726 || (minibuf_level && EQ (it->window, minibuf_window))))
19727 row->indicate_empty_line_p = 1;
19728 }
19729
19730 it->continuation_lines_width = 0;
19731 row->ends_at_zv_p = 1;
19732 /* A row that displays right-to-left text must always have
19733 its last face extended all the way to the end of line,
19734 even if this row ends in ZV, because we still write to
19735 the screen left to right. We also need to extend the
19736 last face if the default face is remapped to some
19737 different face, otherwise the functions that clear
19738 portions of the screen will clear with the default face's
19739 background color. */
19740 if (row->reversed_p
19741 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19742 extend_face_to_end_of_line (it);
19743 break;
19744 }
19745
19746 /* Now, get the metrics of what we want to display. This also
19747 generates glyphs in `row' (which is IT->glyph_row). */
19748 n_glyphs_before = row->used[TEXT_AREA];
19749 x = it->current_x;
19750
19751 /* Remember the line height so far in case the next element doesn't
19752 fit on the line. */
19753 if (it->line_wrap != TRUNCATE)
19754 {
19755 ascent = it->max_ascent;
19756 descent = it->max_descent;
19757 phys_ascent = it->max_phys_ascent;
19758 phys_descent = it->max_phys_descent;
19759
19760 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19761 {
19762 if (IT_DISPLAYING_WHITESPACE (it))
19763 may_wrap = 1;
19764 else if (may_wrap)
19765 {
19766 SAVE_IT (wrap_it, *it, wrap_data);
19767 wrap_x = x;
19768 wrap_row_used = row->used[TEXT_AREA];
19769 wrap_row_ascent = row->ascent;
19770 wrap_row_height = row->height;
19771 wrap_row_phys_ascent = row->phys_ascent;
19772 wrap_row_phys_height = row->phys_height;
19773 wrap_row_extra_line_spacing = row->extra_line_spacing;
19774 wrap_row_min_pos = min_pos;
19775 wrap_row_min_bpos = min_bpos;
19776 wrap_row_max_pos = max_pos;
19777 wrap_row_max_bpos = max_bpos;
19778 may_wrap = 0;
19779 }
19780 }
19781 }
19782
19783 PRODUCE_GLYPHS (it);
19784
19785 /* If this display element was in marginal areas, continue with
19786 the next one. */
19787 if (it->area != TEXT_AREA)
19788 {
19789 row->ascent = max (row->ascent, it->max_ascent);
19790 row->height = max (row->height, it->max_ascent + it->max_descent);
19791 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19792 row->phys_height = max (row->phys_height,
19793 it->max_phys_ascent + it->max_phys_descent);
19794 row->extra_line_spacing = max (row->extra_line_spacing,
19795 it->max_extra_line_spacing);
19796 set_iterator_to_next (it, 1);
19797 continue;
19798 }
19799
19800 /* Does the display element fit on the line? If we truncate
19801 lines, we should draw past the right edge of the window. If
19802 we don't truncate, we want to stop so that we can display the
19803 continuation glyph before the right margin. If lines are
19804 continued, there are two possible strategies for characters
19805 resulting in more than 1 glyph (e.g. tabs): Display as many
19806 glyphs as possible in this line and leave the rest for the
19807 continuation line, or display the whole element in the next
19808 line. Original redisplay did the former, so we do it also. */
19809 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19810 hpos_before = it->hpos;
19811 x_before = x;
19812
19813 if (/* Not a newline. */
19814 nglyphs > 0
19815 /* Glyphs produced fit entirely in the line. */
19816 && it->current_x < it->last_visible_x)
19817 {
19818 it->hpos += nglyphs;
19819 row->ascent = max (row->ascent, it->max_ascent);
19820 row->height = max (row->height, it->max_ascent + it->max_descent);
19821 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19822 row->phys_height = max (row->phys_height,
19823 it->max_phys_ascent + it->max_phys_descent);
19824 row->extra_line_spacing = max (row->extra_line_spacing,
19825 it->max_extra_line_spacing);
19826 if (it->current_x - it->pixel_width < it->first_visible_x)
19827 row->x = x - it->first_visible_x;
19828 /* Record the maximum and minimum buffer positions seen so
19829 far in glyphs that will be displayed by this row. */
19830 if (it->bidi_p)
19831 RECORD_MAX_MIN_POS (it);
19832 }
19833 else
19834 {
19835 int i, new_x;
19836 struct glyph *glyph;
19837
19838 for (i = 0; i < nglyphs; ++i, x = new_x)
19839 {
19840 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19841 new_x = x + glyph->pixel_width;
19842
19843 if (/* Lines are continued. */
19844 it->line_wrap != TRUNCATE
19845 && (/* Glyph doesn't fit on the line. */
19846 new_x > it->last_visible_x
19847 /* Or it fits exactly on a window system frame. */
19848 || (new_x == it->last_visible_x
19849 && FRAME_WINDOW_P (it->f)
19850 && (row->reversed_p
19851 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19852 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19853 {
19854 /* End of a continued line. */
19855
19856 if (it->hpos == 0
19857 || (new_x == it->last_visible_x
19858 && FRAME_WINDOW_P (it->f)
19859 && (row->reversed_p
19860 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19861 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19862 {
19863 /* Current glyph is the only one on the line or
19864 fits exactly on the line. We must continue
19865 the line because we can't draw the cursor
19866 after the glyph. */
19867 row->continued_p = 1;
19868 it->current_x = new_x;
19869 it->continuation_lines_width += new_x;
19870 ++it->hpos;
19871 if (i == nglyphs - 1)
19872 {
19873 /* If line-wrap is on, check if a previous
19874 wrap point was found. */
19875 if (wrap_row_used > 0
19876 /* Even if there is a previous wrap
19877 point, continue the line here as
19878 usual, if (i) the previous character
19879 was a space or tab AND (ii) the
19880 current character is not. */
19881 && (!may_wrap
19882 || IT_DISPLAYING_WHITESPACE (it)))
19883 goto back_to_wrap;
19884
19885 /* Record the maximum and minimum buffer
19886 positions seen so far in glyphs that will be
19887 displayed by this row. */
19888 if (it->bidi_p)
19889 RECORD_MAX_MIN_POS (it);
19890 set_iterator_to_next (it, 1);
19891 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19892 {
19893 if (!get_next_display_element (it))
19894 {
19895 row->exact_window_width_line_p = 1;
19896 it->continuation_lines_width = 0;
19897 row->continued_p = 0;
19898 row->ends_at_zv_p = 1;
19899 }
19900 else if (ITERATOR_AT_END_OF_LINE_P (it))
19901 {
19902 row->continued_p = 0;
19903 row->exact_window_width_line_p = 1;
19904 }
19905 }
19906 }
19907 else if (it->bidi_p)
19908 RECORD_MAX_MIN_POS (it);
19909 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19910 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19911 extend_face_to_end_of_line (it);
19912 }
19913 else if (CHAR_GLYPH_PADDING_P (*glyph)
19914 && !FRAME_WINDOW_P (it->f))
19915 {
19916 /* A padding glyph that doesn't fit on this line.
19917 This means the whole character doesn't fit
19918 on the line. */
19919 if (row->reversed_p)
19920 unproduce_glyphs (it, row->used[TEXT_AREA]
19921 - n_glyphs_before);
19922 row->used[TEXT_AREA] = n_glyphs_before;
19923
19924 /* Fill the rest of the row with continuation
19925 glyphs like in 20.x. */
19926 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19927 < row->glyphs[1 + TEXT_AREA])
19928 produce_special_glyphs (it, IT_CONTINUATION);
19929
19930 row->continued_p = 1;
19931 it->current_x = x_before;
19932 it->continuation_lines_width += x_before;
19933
19934 /* Restore the height to what it was before the
19935 element not fitting on the line. */
19936 it->max_ascent = ascent;
19937 it->max_descent = descent;
19938 it->max_phys_ascent = phys_ascent;
19939 it->max_phys_descent = phys_descent;
19940 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19941 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19942 extend_face_to_end_of_line (it);
19943 }
19944 else if (wrap_row_used > 0)
19945 {
19946 back_to_wrap:
19947 if (row->reversed_p)
19948 unproduce_glyphs (it,
19949 row->used[TEXT_AREA] - wrap_row_used);
19950 RESTORE_IT (it, &wrap_it, wrap_data);
19951 it->continuation_lines_width += wrap_x;
19952 row->used[TEXT_AREA] = wrap_row_used;
19953 row->ascent = wrap_row_ascent;
19954 row->height = wrap_row_height;
19955 row->phys_ascent = wrap_row_phys_ascent;
19956 row->phys_height = wrap_row_phys_height;
19957 row->extra_line_spacing = wrap_row_extra_line_spacing;
19958 min_pos = wrap_row_min_pos;
19959 min_bpos = wrap_row_min_bpos;
19960 max_pos = wrap_row_max_pos;
19961 max_bpos = wrap_row_max_bpos;
19962 row->continued_p = 1;
19963 row->ends_at_zv_p = 0;
19964 row->exact_window_width_line_p = 0;
19965 it->continuation_lines_width += x;
19966
19967 /* Make sure that a non-default face is extended
19968 up to the right margin of the window. */
19969 extend_face_to_end_of_line (it);
19970 }
19971 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19972 {
19973 /* A TAB that extends past the right edge of the
19974 window. This produces a single glyph on
19975 window system frames. We leave the glyph in
19976 this row and let it fill the row, but don't
19977 consume the TAB. */
19978 if ((row->reversed_p
19979 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19980 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19981 produce_special_glyphs (it, IT_CONTINUATION);
19982 it->continuation_lines_width += it->last_visible_x;
19983 row->ends_in_middle_of_char_p = 1;
19984 row->continued_p = 1;
19985 glyph->pixel_width = it->last_visible_x - x;
19986 it->starts_in_middle_of_char_p = 1;
19987 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19988 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
19989 extend_face_to_end_of_line (it);
19990 }
19991 else
19992 {
19993 /* Something other than a TAB that draws past
19994 the right edge of the window. Restore
19995 positions to values before the element. */
19996 if (row->reversed_p)
19997 unproduce_glyphs (it, row->used[TEXT_AREA]
19998 - (n_glyphs_before + i));
19999 row->used[TEXT_AREA] = n_glyphs_before + i;
20000
20001 /* Display continuation glyphs. */
20002 it->current_x = x_before;
20003 it->continuation_lines_width += x;
20004 if (!FRAME_WINDOW_P (it->f)
20005 || (row->reversed_p
20006 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20007 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20008 produce_special_glyphs (it, IT_CONTINUATION);
20009 row->continued_p = 1;
20010
20011 extend_face_to_end_of_line (it);
20012
20013 if (nglyphs > 1 && i > 0)
20014 {
20015 row->ends_in_middle_of_char_p = 1;
20016 it->starts_in_middle_of_char_p = 1;
20017 }
20018
20019 /* Restore the height to what it was before the
20020 element not fitting on the line. */
20021 it->max_ascent = ascent;
20022 it->max_descent = descent;
20023 it->max_phys_ascent = phys_ascent;
20024 it->max_phys_descent = phys_descent;
20025 }
20026
20027 break;
20028 }
20029 else if (new_x > it->first_visible_x)
20030 {
20031 /* Increment number of glyphs actually displayed. */
20032 ++it->hpos;
20033
20034 /* Record the maximum and minimum buffer positions
20035 seen so far in glyphs that will be displayed by
20036 this row. */
20037 if (it->bidi_p)
20038 RECORD_MAX_MIN_POS (it);
20039
20040 if (x < it->first_visible_x)
20041 /* Glyph is partially visible, i.e. row starts at
20042 negative X position. */
20043 row->x = x - it->first_visible_x;
20044 }
20045 else
20046 {
20047 /* Glyph is completely off the left margin of the
20048 window. This should not happen because of the
20049 move_it_in_display_line at the start of this
20050 function, unless the text display area of the
20051 window is empty. */
20052 eassert (it->first_visible_x <= it->last_visible_x);
20053 }
20054 }
20055 /* Even if this display element produced no glyphs at all,
20056 we want to record its position. */
20057 if (it->bidi_p && nglyphs == 0)
20058 RECORD_MAX_MIN_POS (it);
20059
20060 row->ascent = max (row->ascent, it->max_ascent);
20061 row->height = max (row->height, it->max_ascent + it->max_descent);
20062 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20063 row->phys_height = max (row->phys_height,
20064 it->max_phys_ascent + it->max_phys_descent);
20065 row->extra_line_spacing = max (row->extra_line_spacing,
20066 it->max_extra_line_spacing);
20067
20068 /* End of this display line if row is continued. */
20069 if (row->continued_p || row->ends_at_zv_p)
20070 break;
20071 }
20072
20073 at_end_of_line:
20074 /* Is this a line end? If yes, we're also done, after making
20075 sure that a non-default face is extended up to the right
20076 margin of the window. */
20077 if (ITERATOR_AT_END_OF_LINE_P (it))
20078 {
20079 int used_before = row->used[TEXT_AREA];
20080
20081 row->ends_in_newline_from_string_p = STRINGP (it->object);
20082
20083 /* Add a space at the end of the line that is used to
20084 display the cursor there. */
20085 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20086 append_space_for_newline (it, 0);
20087
20088 /* Extend the face to the end of the line. */
20089 extend_face_to_end_of_line (it);
20090
20091 /* Make sure we have the position. */
20092 if (used_before == 0)
20093 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20094
20095 /* Record the position of the newline, for use in
20096 find_row_edges. */
20097 it->eol_pos = it->current.pos;
20098
20099 /* Consume the line end. This skips over invisible lines. */
20100 set_iterator_to_next (it, 1);
20101 it->continuation_lines_width = 0;
20102 break;
20103 }
20104
20105 /* Proceed with next display element. Note that this skips
20106 over lines invisible because of selective display. */
20107 set_iterator_to_next (it, 1);
20108
20109 /* If we truncate lines, we are done when the last displayed
20110 glyphs reach past the right margin of the window. */
20111 if (it->line_wrap == TRUNCATE
20112 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20113 ? (it->current_x >= it->last_visible_x)
20114 : (it->current_x > it->last_visible_x)))
20115 {
20116 /* Maybe add truncation glyphs. */
20117 if (!FRAME_WINDOW_P (it->f)
20118 || (row->reversed_p
20119 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20120 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20121 {
20122 int i, n;
20123
20124 if (!row->reversed_p)
20125 {
20126 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20127 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20128 break;
20129 }
20130 else
20131 {
20132 for (i = 0; i < row->used[TEXT_AREA]; i++)
20133 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20134 break;
20135 /* Remove any padding glyphs at the front of ROW, to
20136 make room for the truncation glyphs we will be
20137 adding below. The loop below always inserts at
20138 least one truncation glyph, so also remove the
20139 last glyph added to ROW. */
20140 unproduce_glyphs (it, i + 1);
20141 /* Adjust i for the loop below. */
20142 i = row->used[TEXT_AREA] - (i + 1);
20143 }
20144
20145 it->current_x = x_before;
20146 if (!FRAME_WINDOW_P (it->f))
20147 {
20148 for (n = row->used[TEXT_AREA]; i < n; ++i)
20149 {
20150 row->used[TEXT_AREA] = i;
20151 produce_special_glyphs (it, IT_TRUNCATION);
20152 }
20153 }
20154 else
20155 {
20156 row->used[TEXT_AREA] = i;
20157 produce_special_glyphs (it, IT_TRUNCATION);
20158 }
20159 }
20160 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20161 {
20162 /* Don't truncate if we can overflow newline into fringe. */
20163 if (!get_next_display_element (it))
20164 {
20165 it->continuation_lines_width = 0;
20166 row->ends_at_zv_p = 1;
20167 row->exact_window_width_line_p = 1;
20168 break;
20169 }
20170 if (ITERATOR_AT_END_OF_LINE_P (it))
20171 {
20172 row->exact_window_width_line_p = 1;
20173 goto at_end_of_line;
20174 }
20175 it->current_x = x_before;
20176 }
20177
20178 row->truncated_on_right_p = 1;
20179 it->continuation_lines_width = 0;
20180 reseat_at_next_visible_line_start (it, 0);
20181 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20182 it->hpos = hpos_before;
20183 break;
20184 }
20185 }
20186
20187 if (wrap_data)
20188 bidi_unshelve_cache (wrap_data, 1);
20189
20190 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20191 at the left window margin. */
20192 if (it->first_visible_x
20193 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20194 {
20195 if (!FRAME_WINDOW_P (it->f)
20196 || (row->reversed_p
20197 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20198 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20199 insert_left_trunc_glyphs (it);
20200 row->truncated_on_left_p = 1;
20201 }
20202
20203 /* Remember the position at which this line ends.
20204
20205 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20206 cannot be before the call to find_row_edges below, since that is
20207 where these positions are determined. */
20208 row->end = it->current;
20209 if (!it->bidi_p)
20210 {
20211 row->minpos = row->start.pos;
20212 row->maxpos = row->end.pos;
20213 }
20214 else
20215 {
20216 /* ROW->minpos and ROW->maxpos must be the smallest and
20217 `1 + the largest' buffer positions in ROW. But if ROW was
20218 bidi-reordered, these two positions can be anywhere in the
20219 row, so we must determine them now. */
20220 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20221 }
20222
20223 /* If the start of this line is the overlay arrow-position, then
20224 mark this glyph row as the one containing the overlay arrow.
20225 This is clearly a mess with variable size fonts. It would be
20226 better to let it be displayed like cursors under X. */
20227 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20228 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20229 !NILP (overlay_arrow_string)))
20230 {
20231 /* Overlay arrow in window redisplay is a fringe bitmap. */
20232 if (STRINGP (overlay_arrow_string))
20233 {
20234 struct glyph_row *arrow_row
20235 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20236 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20237 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20238 struct glyph *p = row->glyphs[TEXT_AREA];
20239 struct glyph *p2, *end;
20240
20241 /* Copy the arrow glyphs. */
20242 while (glyph < arrow_end)
20243 *p++ = *glyph++;
20244
20245 /* Throw away padding glyphs. */
20246 p2 = p;
20247 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20248 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20249 ++p2;
20250 if (p2 > p)
20251 {
20252 while (p2 < end)
20253 *p++ = *p2++;
20254 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20255 }
20256 }
20257 else
20258 {
20259 eassert (INTEGERP (overlay_arrow_string));
20260 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20261 }
20262 overlay_arrow_seen = 1;
20263 }
20264
20265 /* Highlight trailing whitespace. */
20266 if (!NILP (Vshow_trailing_whitespace))
20267 highlight_trailing_whitespace (it->f, it->glyph_row);
20268
20269 /* Compute pixel dimensions of this line. */
20270 compute_line_metrics (it);
20271
20272 /* Implementation note: No changes in the glyphs of ROW or in their
20273 faces can be done past this point, because compute_line_metrics
20274 computes ROW's hash value and stores it within the glyph_row
20275 structure. */
20276
20277 /* Record whether this row ends inside an ellipsis. */
20278 row->ends_in_ellipsis_p
20279 = (it->method == GET_FROM_DISPLAY_VECTOR
20280 && it->ellipsis_p);
20281
20282 /* Save fringe bitmaps in this row. */
20283 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20284 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20285 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20286 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20287
20288 it->left_user_fringe_bitmap = 0;
20289 it->left_user_fringe_face_id = 0;
20290 it->right_user_fringe_bitmap = 0;
20291 it->right_user_fringe_face_id = 0;
20292
20293 /* Maybe set the cursor. */
20294 cvpos = it->w->cursor.vpos;
20295 if ((cvpos < 0
20296 /* In bidi-reordered rows, keep checking for proper cursor
20297 position even if one has been found already, because buffer
20298 positions in such rows change non-linearly with ROW->VPOS,
20299 when a line is continued. One exception: when we are at ZV,
20300 display cursor on the first suitable glyph row, since all
20301 the empty rows after that also have their position set to ZV. */
20302 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20303 lines' rows is implemented for bidi-reordered rows. */
20304 || (it->bidi_p
20305 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20306 && PT >= MATRIX_ROW_START_CHARPOS (row)
20307 && PT <= MATRIX_ROW_END_CHARPOS (row)
20308 && cursor_row_p (row))
20309 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20310
20311 /* Prepare for the next line. This line starts horizontally at (X
20312 HPOS) = (0 0). Vertical positions are incremented. As a
20313 convenience for the caller, IT->glyph_row is set to the next
20314 row to be used. */
20315 it->current_x = it->hpos = 0;
20316 it->current_y += row->height;
20317 SET_TEXT_POS (it->eol_pos, 0, 0);
20318 ++it->vpos;
20319 ++it->glyph_row;
20320 /* The next row should by default use the same value of the
20321 reversed_p flag as this one. set_iterator_to_next decides when
20322 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20323 the flag accordingly. */
20324 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20325 it->glyph_row->reversed_p = row->reversed_p;
20326 it->start = row->end;
20327 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20328
20329 #undef RECORD_MAX_MIN_POS
20330 }
20331
20332 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20333 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20334 doc: /* Return paragraph direction at point in BUFFER.
20335 Value is either `left-to-right' or `right-to-left'.
20336 If BUFFER is omitted or nil, it defaults to the current buffer.
20337
20338 Paragraph direction determines how the text in the paragraph is displayed.
20339 In left-to-right paragraphs, text begins at the left margin of the window
20340 and the reading direction is generally left to right. In right-to-left
20341 paragraphs, text begins at the right margin and is read from right to left.
20342
20343 See also `bidi-paragraph-direction'. */)
20344 (Lisp_Object buffer)
20345 {
20346 struct buffer *buf = current_buffer;
20347 struct buffer *old = buf;
20348
20349 if (! NILP (buffer))
20350 {
20351 CHECK_BUFFER (buffer);
20352 buf = XBUFFER (buffer);
20353 }
20354
20355 if (NILP (BVAR (buf, bidi_display_reordering))
20356 || NILP (BVAR (buf, enable_multibyte_characters))
20357 /* When we are loading loadup.el, the character property tables
20358 needed for bidi iteration are not yet available. */
20359 || !NILP (Vpurify_flag))
20360 return Qleft_to_right;
20361 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20362 return BVAR (buf, bidi_paragraph_direction);
20363 else
20364 {
20365 /* Determine the direction from buffer text. We could try to
20366 use current_matrix if it is up to date, but this seems fast
20367 enough as it is. */
20368 struct bidi_it itb;
20369 ptrdiff_t pos = BUF_PT (buf);
20370 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20371 int c;
20372 void *itb_data = bidi_shelve_cache ();
20373
20374 set_buffer_temp (buf);
20375 /* bidi_paragraph_init finds the base direction of the paragraph
20376 by searching forward from paragraph start. We need the base
20377 direction of the current or _previous_ paragraph, so we need
20378 to make sure we are within that paragraph. To that end, find
20379 the previous non-empty line. */
20380 if (pos >= ZV && pos > BEGV)
20381 DEC_BOTH (pos, bytepos);
20382 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20383 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20384 {
20385 while ((c = FETCH_BYTE (bytepos)) == '\n'
20386 || c == ' ' || c == '\t' || c == '\f')
20387 {
20388 if (bytepos <= BEGV_BYTE)
20389 break;
20390 bytepos--;
20391 pos--;
20392 }
20393 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20394 bytepos--;
20395 }
20396 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20397 itb.paragraph_dir = NEUTRAL_DIR;
20398 itb.string.s = NULL;
20399 itb.string.lstring = Qnil;
20400 itb.string.bufpos = 0;
20401 itb.string.unibyte = 0;
20402 /* We have no window to use here for ignoring window-specific
20403 overlays. Using NULL for window pointer will cause
20404 compute_display_string_pos to use the current buffer. */
20405 itb.w = NULL;
20406 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20407 bidi_unshelve_cache (itb_data, 0);
20408 set_buffer_temp (old);
20409 switch (itb.paragraph_dir)
20410 {
20411 case L2R:
20412 return Qleft_to_right;
20413 break;
20414 case R2L:
20415 return Qright_to_left;
20416 break;
20417 default:
20418 emacs_abort ();
20419 }
20420 }
20421 }
20422
20423 DEFUN ("move-point-visually", Fmove_point_visually,
20424 Smove_point_visually, 1, 1, 0,
20425 doc: /* Move point in the visual order in the specified DIRECTION.
20426 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20427 left.
20428
20429 Value is the new character position of point. */)
20430 (Lisp_Object direction)
20431 {
20432 struct window *w = XWINDOW (selected_window);
20433 struct buffer *b = XBUFFER (w->contents);
20434 struct glyph_row *row;
20435 int dir;
20436 Lisp_Object paragraph_dir;
20437
20438 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20439 (!(ROW)->continued_p \
20440 && INTEGERP ((GLYPH)->object) \
20441 && (GLYPH)->type == CHAR_GLYPH \
20442 && (GLYPH)->u.ch == ' ' \
20443 && (GLYPH)->charpos >= 0 \
20444 && !(GLYPH)->avoid_cursor_p)
20445
20446 CHECK_NUMBER (direction);
20447 dir = XINT (direction);
20448 if (dir > 0)
20449 dir = 1;
20450 else
20451 dir = -1;
20452
20453 /* If current matrix is up-to-date, we can use the information
20454 recorded in the glyphs, at least as long as the goal is on the
20455 screen. */
20456 if (w->window_end_valid
20457 && !windows_or_buffers_changed
20458 && b
20459 && !b->clip_changed
20460 && !b->prevent_redisplay_optimizations_p
20461 && !window_outdated (w)
20462 && w->cursor.vpos >= 0
20463 && w->cursor.vpos < w->current_matrix->nrows
20464 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20465 {
20466 struct glyph *g = row->glyphs[TEXT_AREA];
20467 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20468 struct glyph *gpt = g + w->cursor.hpos;
20469
20470 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20471 {
20472 if (BUFFERP (g->object) && g->charpos != PT)
20473 {
20474 SET_PT (g->charpos);
20475 w->cursor.vpos = -1;
20476 return make_number (PT);
20477 }
20478 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20479 {
20480 ptrdiff_t new_pos;
20481
20482 if (BUFFERP (gpt->object))
20483 {
20484 new_pos = PT;
20485 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20486 new_pos += (row->reversed_p ? -dir : dir);
20487 else
20488 new_pos -= (row->reversed_p ? -dir : dir);;
20489 }
20490 else if (BUFFERP (g->object))
20491 new_pos = g->charpos;
20492 else
20493 break;
20494 SET_PT (new_pos);
20495 w->cursor.vpos = -1;
20496 return make_number (PT);
20497 }
20498 else if (ROW_GLYPH_NEWLINE_P (row, g))
20499 {
20500 /* Glyphs inserted at the end of a non-empty line for
20501 positioning the cursor have zero charpos, so we must
20502 deduce the value of point by other means. */
20503 if (g->charpos > 0)
20504 SET_PT (g->charpos);
20505 else if (row->ends_at_zv_p && PT != ZV)
20506 SET_PT (ZV);
20507 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20508 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20509 else
20510 break;
20511 w->cursor.vpos = -1;
20512 return make_number (PT);
20513 }
20514 }
20515 if (g == e || INTEGERP (g->object))
20516 {
20517 if (row->truncated_on_left_p || row->truncated_on_right_p)
20518 goto simulate_display;
20519 if (!row->reversed_p)
20520 row += dir;
20521 else
20522 row -= dir;
20523 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20524 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20525 goto simulate_display;
20526
20527 if (dir > 0)
20528 {
20529 if (row->reversed_p && !row->continued_p)
20530 {
20531 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20532 w->cursor.vpos = -1;
20533 return make_number (PT);
20534 }
20535 g = row->glyphs[TEXT_AREA];
20536 e = g + row->used[TEXT_AREA];
20537 for ( ; g < e; g++)
20538 {
20539 if (BUFFERP (g->object)
20540 /* Empty lines have only one glyph, which stands
20541 for the newline, and whose charpos is the
20542 buffer position of the newline. */
20543 || ROW_GLYPH_NEWLINE_P (row, g)
20544 /* When the buffer ends in a newline, the line at
20545 EOB also has one glyph, but its charpos is -1. */
20546 || (row->ends_at_zv_p
20547 && !row->reversed_p
20548 && INTEGERP (g->object)
20549 && g->type == CHAR_GLYPH
20550 && g->u.ch == ' '))
20551 {
20552 if (g->charpos > 0)
20553 SET_PT (g->charpos);
20554 else if (!row->reversed_p
20555 && row->ends_at_zv_p
20556 && PT != ZV)
20557 SET_PT (ZV);
20558 else
20559 continue;
20560 w->cursor.vpos = -1;
20561 return make_number (PT);
20562 }
20563 }
20564 }
20565 else
20566 {
20567 if (!row->reversed_p && !row->continued_p)
20568 {
20569 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20570 w->cursor.vpos = -1;
20571 return make_number (PT);
20572 }
20573 e = row->glyphs[TEXT_AREA];
20574 g = e + row->used[TEXT_AREA] - 1;
20575 for ( ; g >= e; g--)
20576 {
20577 if (BUFFERP (g->object)
20578 || (ROW_GLYPH_NEWLINE_P (row, g)
20579 && g->charpos > 0)
20580 /* Empty R2L lines on GUI frames have the buffer
20581 position of the newline stored in the stretch
20582 glyph. */
20583 || g->type == STRETCH_GLYPH
20584 || (row->ends_at_zv_p
20585 && row->reversed_p
20586 && INTEGERP (g->object)
20587 && g->type == CHAR_GLYPH
20588 && g->u.ch == ' '))
20589 {
20590 if (g->charpos > 0)
20591 SET_PT (g->charpos);
20592 else if (row->reversed_p
20593 && row->ends_at_zv_p
20594 && PT != ZV)
20595 SET_PT (ZV);
20596 else
20597 continue;
20598 w->cursor.vpos = -1;
20599 return make_number (PT);
20600 }
20601 }
20602 }
20603 }
20604 }
20605
20606 simulate_display:
20607
20608 /* If we wind up here, we failed to move by using the glyphs, so we
20609 need to simulate display instead. */
20610
20611 if (b)
20612 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20613 else
20614 paragraph_dir = Qleft_to_right;
20615 if (EQ (paragraph_dir, Qright_to_left))
20616 dir = -dir;
20617 if (PT <= BEGV && dir < 0)
20618 xsignal0 (Qbeginning_of_buffer);
20619 else if (PT >= ZV && dir > 0)
20620 xsignal0 (Qend_of_buffer);
20621 else
20622 {
20623 struct text_pos pt;
20624 struct it it;
20625 int pt_x, target_x, pixel_width, pt_vpos;
20626 bool at_eol_p;
20627 bool overshoot_expected = false;
20628 bool target_is_eol_p = false;
20629
20630 /* Setup the arena. */
20631 SET_TEXT_POS (pt, PT, PT_BYTE);
20632 start_display (&it, w, pt);
20633
20634 if (it.cmp_it.id < 0
20635 && it.method == GET_FROM_STRING
20636 && it.area == TEXT_AREA
20637 && it.string_from_display_prop_p
20638 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20639 overshoot_expected = true;
20640
20641 /* Find the X coordinate of point. We start from the beginning
20642 of this or previous line to make sure we are before point in
20643 the logical order (since the move_it_* functions can only
20644 move forward). */
20645 reseat:
20646 reseat_at_previous_visible_line_start (&it);
20647 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20648 if (IT_CHARPOS (it) != PT)
20649 {
20650 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20651 -1, -1, -1, MOVE_TO_POS);
20652 /* If we missed point because the character there is
20653 displayed out of a display vector that has more than one
20654 glyph, retry expecting overshoot. */
20655 if (it.method == GET_FROM_DISPLAY_VECTOR
20656 && it.current.dpvec_index > 0
20657 && !overshoot_expected)
20658 {
20659 overshoot_expected = true;
20660 goto reseat;
20661 }
20662 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20663 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20664 }
20665 pt_x = it.current_x;
20666 pt_vpos = it.vpos;
20667 if (dir > 0 || overshoot_expected)
20668 {
20669 struct glyph_row *row = it.glyph_row;
20670
20671 /* When point is at beginning of line, we don't have
20672 information about the glyph there loaded into struct
20673 it. Calling get_next_display_element fixes that. */
20674 if (pt_x == 0)
20675 get_next_display_element (&it);
20676 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20677 it.glyph_row = NULL;
20678 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20679 it.glyph_row = row;
20680 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20681 it, lest it will become out of sync with it's buffer
20682 position. */
20683 it.current_x = pt_x;
20684 }
20685 else
20686 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20687 pixel_width = it.pixel_width;
20688 if (overshoot_expected && at_eol_p)
20689 pixel_width = 0;
20690 else if (pixel_width <= 0)
20691 pixel_width = 1;
20692
20693 /* If there's a display string (or something similar) at point,
20694 we are actually at the glyph to the left of point, so we need
20695 to correct the X coordinate. */
20696 if (overshoot_expected)
20697 {
20698 if (it.bidi_p)
20699 pt_x += pixel_width * it.bidi_it.scan_dir;
20700 else
20701 pt_x += pixel_width;
20702 }
20703
20704 /* Compute target X coordinate, either to the left or to the
20705 right of point. On TTY frames, all characters have the same
20706 pixel width of 1, so we can use that. On GUI frames we don't
20707 have an easy way of getting at the pixel width of the
20708 character to the left of point, so we use a different method
20709 of getting to that place. */
20710 if (dir > 0)
20711 target_x = pt_x + pixel_width;
20712 else
20713 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20714
20715 /* Target X coordinate could be one line above or below the line
20716 of point, in which case we need to adjust the target X
20717 coordinate. Also, if moving to the left, we need to begin at
20718 the left edge of the point's screen line. */
20719 if (dir < 0)
20720 {
20721 if (pt_x > 0)
20722 {
20723 start_display (&it, w, pt);
20724 reseat_at_previous_visible_line_start (&it);
20725 it.current_x = it.current_y = it.hpos = 0;
20726 if (pt_vpos != 0)
20727 move_it_by_lines (&it, pt_vpos);
20728 }
20729 else
20730 {
20731 move_it_by_lines (&it, -1);
20732 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20733 target_is_eol_p = true;
20734 }
20735 }
20736 else
20737 {
20738 if (at_eol_p
20739 || (target_x >= it.last_visible_x
20740 && it.line_wrap != TRUNCATE))
20741 {
20742 if (pt_x > 0)
20743 move_it_by_lines (&it, 0);
20744 move_it_by_lines (&it, 1);
20745 target_x = 0;
20746 }
20747 }
20748
20749 /* Move to the target X coordinate. */
20750 #ifdef HAVE_WINDOW_SYSTEM
20751 /* On GUI frames, as we don't know the X coordinate of the
20752 character to the left of point, moving point to the left
20753 requires walking, one grapheme cluster at a time, until we
20754 find ourself at a place immediately to the left of the
20755 character at point. */
20756 if (FRAME_WINDOW_P (it.f) && dir < 0)
20757 {
20758 struct text_pos new_pos;
20759 enum move_it_result rc = MOVE_X_REACHED;
20760
20761 if (it.current_x == 0)
20762 get_next_display_element (&it);
20763 if (it.what == IT_COMPOSITION)
20764 {
20765 new_pos.charpos = it.cmp_it.charpos;
20766 new_pos.bytepos = -1;
20767 }
20768 else
20769 new_pos = it.current.pos;
20770
20771 while (it.current_x + it.pixel_width <= target_x
20772 && rc == MOVE_X_REACHED)
20773 {
20774 int new_x = it.current_x + it.pixel_width;
20775
20776 /* For composed characters, we want the position of the
20777 first character in the grapheme cluster (usually, the
20778 composition's base character), whereas it.current
20779 might give us the position of the _last_ one, e.g. if
20780 the composition is rendered in reverse due to bidi
20781 reordering. */
20782 if (it.what == IT_COMPOSITION)
20783 {
20784 new_pos.charpos = it.cmp_it.charpos;
20785 new_pos.bytepos = -1;
20786 }
20787 else
20788 new_pos = it.current.pos;
20789 if (new_x == it.current_x)
20790 new_x++;
20791 rc = move_it_in_display_line_to (&it, ZV, new_x,
20792 MOVE_TO_POS | MOVE_TO_X);
20793 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20794 break;
20795 }
20796 /* The previous position we saw in the loop is the one we
20797 want. */
20798 if (new_pos.bytepos == -1)
20799 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20800 it.current.pos = new_pos;
20801 }
20802 else
20803 #endif
20804 if (it.current_x != target_x)
20805 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20806
20807 /* When lines are truncated, the above loop will stop at the
20808 window edge. But we want to get to the end of line, even if
20809 it is beyond the window edge; automatic hscroll will then
20810 scroll the window to show point as appropriate. */
20811 if (target_is_eol_p && it.line_wrap == TRUNCATE
20812 && get_next_display_element (&it))
20813 {
20814 struct text_pos new_pos = it.current.pos;
20815
20816 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20817 {
20818 set_iterator_to_next (&it, 0);
20819 if (it.method == GET_FROM_BUFFER)
20820 new_pos = it.current.pos;
20821 if (!get_next_display_element (&it))
20822 break;
20823 }
20824
20825 it.current.pos = new_pos;
20826 }
20827
20828 /* If we ended up in a display string that covers point, move to
20829 buffer position to the right in the visual order. */
20830 if (dir > 0)
20831 {
20832 while (IT_CHARPOS (it) == PT)
20833 {
20834 set_iterator_to_next (&it, 0);
20835 if (!get_next_display_element (&it))
20836 break;
20837 }
20838 }
20839
20840 /* Move point to that position. */
20841 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20842 }
20843
20844 return make_number (PT);
20845
20846 #undef ROW_GLYPH_NEWLINE_P
20847 }
20848
20849 \f
20850 /***********************************************************************
20851 Menu Bar
20852 ***********************************************************************/
20853
20854 /* Redisplay the menu bar in the frame for window W.
20855
20856 The menu bar of X frames that don't have X toolkit support is
20857 displayed in a special window W->frame->menu_bar_window.
20858
20859 The menu bar of terminal frames is treated specially as far as
20860 glyph matrices are concerned. Menu bar lines are not part of
20861 windows, so the update is done directly on the frame matrix rows
20862 for the menu bar. */
20863
20864 static void
20865 display_menu_bar (struct window *w)
20866 {
20867 struct frame *f = XFRAME (WINDOW_FRAME (w));
20868 struct it it;
20869 Lisp_Object items;
20870 int i;
20871
20872 /* Don't do all this for graphical frames. */
20873 #ifdef HAVE_NTGUI
20874 if (FRAME_W32_P (f))
20875 return;
20876 #endif
20877 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20878 if (FRAME_X_P (f))
20879 return;
20880 #endif
20881
20882 #ifdef HAVE_NS
20883 if (FRAME_NS_P (f))
20884 return;
20885 #endif /* HAVE_NS */
20886
20887 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20888 eassert (!FRAME_WINDOW_P (f));
20889 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20890 it.first_visible_x = 0;
20891 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20892 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20893 if (FRAME_WINDOW_P (f))
20894 {
20895 /* Menu bar lines are displayed in the desired matrix of the
20896 dummy window menu_bar_window. */
20897 struct window *menu_w;
20898 menu_w = XWINDOW (f->menu_bar_window);
20899 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20900 MENU_FACE_ID);
20901 it.first_visible_x = 0;
20902 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
20903 }
20904 else
20905 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20906 {
20907 /* This is a TTY frame, i.e. character hpos/vpos are used as
20908 pixel x/y. */
20909 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20910 MENU_FACE_ID);
20911 it.first_visible_x = 0;
20912 it.last_visible_x = FRAME_COLS (f);
20913 }
20914
20915 /* FIXME: This should be controlled by a user option. See the
20916 comments in redisplay_tool_bar and display_mode_line about
20917 this. */
20918 it.paragraph_embedding = L2R;
20919
20920 /* Clear all rows of the menu bar. */
20921 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20922 {
20923 struct glyph_row *row = it.glyph_row + i;
20924 clear_glyph_row (row);
20925 row->enabled_p = true;
20926 row->full_width_p = 1;
20927 }
20928
20929 /* Display all items of the menu bar. */
20930 items = FRAME_MENU_BAR_ITEMS (it.f);
20931 for (i = 0; i < ASIZE (items); i += 4)
20932 {
20933 Lisp_Object string;
20934
20935 /* Stop at nil string. */
20936 string = AREF (items, i + 1);
20937 if (NILP (string))
20938 break;
20939
20940 /* Remember where item was displayed. */
20941 ASET (items, i + 3, make_number (it.hpos));
20942
20943 /* Display the item, pad with one space. */
20944 if (it.current_x < it.last_visible_x)
20945 display_string (NULL, string, Qnil, 0, 0, &it,
20946 SCHARS (string) + 1, 0, 0, -1);
20947 }
20948
20949 /* Fill out the line with spaces. */
20950 if (it.current_x < it.last_visible_x)
20951 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20952
20953 /* Compute the total height of the lines. */
20954 compute_line_metrics (&it);
20955 }
20956
20957 /* Deep copy of a glyph row, including the glyphs. */
20958 static void
20959 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20960 {
20961 struct glyph *pointers[1 + LAST_AREA];
20962 int to_used = to->used[TEXT_AREA];
20963
20964 /* Save glyph pointers of TO. */
20965 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20966
20967 /* Do a structure assignment. */
20968 *to = *from;
20969
20970 /* Restore original glyph pointers of TO. */
20971 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20972
20973 /* Copy the glyphs. */
20974 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20975 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20976
20977 /* If we filled only part of the TO row, fill the rest with
20978 space_glyph (which will display as empty space). */
20979 if (to_used > from->used[TEXT_AREA])
20980 fill_up_frame_row_with_spaces (to, to_used);
20981 }
20982
20983 /* Display one menu item on a TTY, by overwriting the glyphs in the
20984 frame F's desired glyph matrix with glyphs produced from the menu
20985 item text. Called from term.c to display TTY drop-down menus one
20986 item at a time.
20987
20988 ITEM_TEXT is the menu item text as a C string.
20989
20990 FACE_ID is the face ID to be used for this menu item. FACE_ID
20991 could specify one of 3 faces: a face for an enabled item, a face
20992 for a disabled item, or a face for a selected item.
20993
20994 X and Y are coordinates of the first glyph in the frame's desired
20995 matrix to be overwritten by the menu item. Since this is a TTY, Y
20996 is the zero-based number of the glyph row and X is the zero-based
20997 glyph number in the row, starting from left, where to start
20998 displaying the item.
20999
21000 SUBMENU non-zero means this menu item drops down a submenu, which
21001 should be indicated by displaying a proper visual cue after the
21002 item text. */
21003
21004 void
21005 display_tty_menu_item (const char *item_text, int width, int face_id,
21006 int x, int y, int submenu)
21007 {
21008 struct it it;
21009 struct frame *f = SELECTED_FRAME ();
21010 struct window *w = XWINDOW (f->selected_window);
21011 int saved_used, saved_truncated, saved_width, saved_reversed;
21012 struct glyph_row *row;
21013 size_t item_len = strlen (item_text);
21014
21015 eassert (FRAME_TERMCAP_P (f));
21016
21017 /* Don't write beyond the matrix's last row. This can happen for
21018 TTY screens that are not high enough to show the entire menu.
21019 (This is actually a bit of defensive programming, as
21020 tty_menu_display already limits the number of menu items to one
21021 less than the number of screen lines.) */
21022 if (y >= f->desired_matrix->nrows)
21023 return;
21024
21025 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21026 it.first_visible_x = 0;
21027 it.last_visible_x = FRAME_COLS (f) - 1;
21028 row = it.glyph_row;
21029 /* Start with the row contents from the current matrix. */
21030 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21031 saved_width = row->full_width_p;
21032 row->full_width_p = 1;
21033 saved_reversed = row->reversed_p;
21034 row->reversed_p = 0;
21035 row->enabled_p = true;
21036
21037 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21038 desired face. */
21039 eassert (x < f->desired_matrix->matrix_w);
21040 it.current_x = it.hpos = x;
21041 it.current_y = it.vpos = y;
21042 saved_used = row->used[TEXT_AREA];
21043 saved_truncated = row->truncated_on_right_p;
21044 row->used[TEXT_AREA] = x;
21045 it.face_id = face_id;
21046 it.line_wrap = TRUNCATE;
21047
21048 /* FIXME: This should be controlled by a user option. See the
21049 comments in redisplay_tool_bar and display_mode_line about this.
21050 Also, if paragraph_embedding could ever be R2L, changes will be
21051 needed to avoid shifting to the right the row characters in
21052 term.c:append_glyph. */
21053 it.paragraph_embedding = L2R;
21054
21055 /* Pad with a space on the left. */
21056 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21057 width--;
21058 /* Display the menu item, pad with spaces to WIDTH. */
21059 if (submenu)
21060 {
21061 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21062 item_len, 0, FRAME_COLS (f) - 1, -1);
21063 width -= item_len;
21064 /* Indicate with " >" that there's a submenu. */
21065 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21066 FRAME_COLS (f) - 1, -1);
21067 }
21068 else
21069 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21070 width, 0, FRAME_COLS (f) - 1, -1);
21071
21072 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21073 row->truncated_on_right_p = saved_truncated;
21074 row->hash = row_hash (row);
21075 row->full_width_p = saved_width;
21076 row->reversed_p = saved_reversed;
21077 }
21078 \f
21079 /***********************************************************************
21080 Mode Line
21081 ***********************************************************************/
21082
21083 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21084 FORCE is non-zero, redisplay mode lines unconditionally.
21085 Otherwise, redisplay only mode lines that are garbaged. Value is
21086 the number of windows whose mode lines were redisplayed. */
21087
21088 static int
21089 redisplay_mode_lines (Lisp_Object window, bool force)
21090 {
21091 int nwindows = 0;
21092
21093 while (!NILP (window))
21094 {
21095 struct window *w = XWINDOW (window);
21096
21097 if (WINDOWP (w->contents))
21098 nwindows += redisplay_mode_lines (w->contents, force);
21099 else if (force
21100 || FRAME_GARBAGED_P (XFRAME (w->frame))
21101 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21102 {
21103 struct text_pos lpoint;
21104 struct buffer *old = current_buffer;
21105
21106 /* Set the window's buffer for the mode line display. */
21107 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21108 set_buffer_internal_1 (XBUFFER (w->contents));
21109
21110 /* Point refers normally to the selected window. For any
21111 other window, set up appropriate value. */
21112 if (!EQ (window, selected_window))
21113 {
21114 struct text_pos pt;
21115
21116 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21117 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21118 }
21119
21120 /* Display mode lines. */
21121 clear_glyph_matrix (w->desired_matrix);
21122 if (display_mode_lines (w))
21123 ++nwindows;
21124
21125 /* Restore old settings. */
21126 set_buffer_internal_1 (old);
21127 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21128 }
21129
21130 window = w->next;
21131 }
21132
21133 return nwindows;
21134 }
21135
21136
21137 /* Display the mode and/or header line of window W. Value is the
21138 sum number of mode lines and header lines displayed. */
21139
21140 static int
21141 display_mode_lines (struct window *w)
21142 {
21143 Lisp_Object old_selected_window = selected_window;
21144 Lisp_Object old_selected_frame = selected_frame;
21145 Lisp_Object new_frame = w->frame;
21146 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21147 int n = 0;
21148
21149 selected_frame = new_frame;
21150 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21151 or window's point, then we'd need select_window_1 here as well. */
21152 XSETWINDOW (selected_window, w);
21153 XFRAME (new_frame)->selected_window = selected_window;
21154
21155 /* These will be set while the mode line specs are processed. */
21156 line_number_displayed = 0;
21157 w->column_number_displayed = -1;
21158
21159 if (WINDOW_WANTS_MODELINE_P (w))
21160 {
21161 struct window *sel_w = XWINDOW (old_selected_window);
21162
21163 /* Select mode line face based on the real selected window. */
21164 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21165 BVAR (current_buffer, mode_line_format));
21166 ++n;
21167 }
21168
21169 if (WINDOW_WANTS_HEADER_LINE_P (w))
21170 {
21171 display_mode_line (w, HEADER_LINE_FACE_ID,
21172 BVAR (current_buffer, header_line_format));
21173 ++n;
21174 }
21175
21176 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21177 selected_frame = old_selected_frame;
21178 selected_window = old_selected_window;
21179 if (n > 0)
21180 w->must_be_updated_p = true;
21181 return n;
21182 }
21183
21184
21185 /* Display mode or header line of window W. FACE_ID specifies which
21186 line to display; it is either MODE_LINE_FACE_ID or
21187 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21188 display. Value is the pixel height of the mode/header line
21189 displayed. */
21190
21191 static int
21192 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21193 {
21194 struct it it;
21195 struct face *face;
21196 ptrdiff_t count = SPECPDL_INDEX ();
21197
21198 init_iterator (&it, w, -1, -1, NULL, face_id);
21199 /* Don't extend on a previously drawn mode-line.
21200 This may happen if called from pos_visible_p. */
21201 it.glyph_row->enabled_p = false;
21202 prepare_desired_row (it.glyph_row);
21203
21204 it.glyph_row->mode_line_p = 1;
21205
21206 /* FIXME: This should be controlled by a user option. But
21207 supporting such an option is not trivial, since the mode line is
21208 made up of many separate strings. */
21209 it.paragraph_embedding = L2R;
21210
21211 record_unwind_protect (unwind_format_mode_line,
21212 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21213
21214 mode_line_target = MODE_LINE_DISPLAY;
21215
21216 /* Temporarily make frame's keyboard the current kboard so that
21217 kboard-local variables in the mode_line_format will get the right
21218 values. */
21219 push_kboard (FRAME_KBOARD (it.f));
21220 record_unwind_save_match_data ();
21221 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21222 pop_kboard ();
21223
21224 unbind_to (count, Qnil);
21225
21226 /* Fill up with spaces. */
21227 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21228
21229 compute_line_metrics (&it);
21230 it.glyph_row->full_width_p = 1;
21231 it.glyph_row->continued_p = 0;
21232 it.glyph_row->truncated_on_left_p = 0;
21233 it.glyph_row->truncated_on_right_p = 0;
21234
21235 /* Make a 3D mode-line have a shadow at its right end. */
21236 face = FACE_FROM_ID (it.f, face_id);
21237 extend_face_to_end_of_line (&it);
21238 if (face->box != FACE_NO_BOX)
21239 {
21240 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21241 + it.glyph_row->used[TEXT_AREA] - 1);
21242 last->right_box_line_p = 1;
21243 }
21244
21245 return it.glyph_row->height;
21246 }
21247
21248 /* Move element ELT in LIST to the front of LIST.
21249 Return the updated list. */
21250
21251 static Lisp_Object
21252 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21253 {
21254 register Lisp_Object tail, prev;
21255 register Lisp_Object tem;
21256
21257 tail = list;
21258 prev = Qnil;
21259 while (CONSP (tail))
21260 {
21261 tem = XCAR (tail);
21262
21263 if (EQ (elt, tem))
21264 {
21265 /* Splice out the link TAIL. */
21266 if (NILP (prev))
21267 list = XCDR (tail);
21268 else
21269 Fsetcdr (prev, XCDR (tail));
21270
21271 /* Now make it the first. */
21272 Fsetcdr (tail, list);
21273 return tail;
21274 }
21275 else
21276 prev = tail;
21277 tail = XCDR (tail);
21278 QUIT;
21279 }
21280
21281 /* Not found--return unchanged LIST. */
21282 return list;
21283 }
21284
21285 /* Contribute ELT to the mode line for window IT->w. How it
21286 translates into text depends on its data type.
21287
21288 IT describes the display environment in which we display, as usual.
21289
21290 DEPTH is the depth in recursion. It is used to prevent
21291 infinite recursion here.
21292
21293 FIELD_WIDTH is the number of characters the display of ELT should
21294 occupy in the mode line, and PRECISION is the maximum number of
21295 characters to display from ELT's representation. See
21296 display_string for details.
21297
21298 Returns the hpos of the end of the text generated by ELT.
21299
21300 PROPS is a property list to add to any string we encounter.
21301
21302 If RISKY is nonzero, remove (disregard) any properties in any string
21303 we encounter, and ignore :eval and :propertize.
21304
21305 The global variable `mode_line_target' determines whether the
21306 output is passed to `store_mode_line_noprop',
21307 `store_mode_line_string', or `display_string'. */
21308
21309 static int
21310 display_mode_element (struct it *it, int depth, int field_width, int precision,
21311 Lisp_Object elt, Lisp_Object props, int risky)
21312 {
21313 int n = 0, field, prec;
21314 int literal = 0;
21315
21316 tail_recurse:
21317 if (depth > 100)
21318 elt = build_string ("*too-deep*");
21319
21320 depth++;
21321
21322 switch (XTYPE (elt))
21323 {
21324 case Lisp_String:
21325 {
21326 /* A string: output it and check for %-constructs within it. */
21327 unsigned char c;
21328 ptrdiff_t offset = 0;
21329
21330 if (SCHARS (elt) > 0
21331 && (!NILP (props) || risky))
21332 {
21333 Lisp_Object oprops, aelt;
21334 oprops = Ftext_properties_at (make_number (0), elt);
21335
21336 /* If the starting string's properties are not what
21337 we want, translate the string. Also, if the string
21338 is risky, do that anyway. */
21339
21340 if (NILP (Fequal (props, oprops)) || risky)
21341 {
21342 /* If the starting string has properties,
21343 merge the specified ones onto the existing ones. */
21344 if (! NILP (oprops) && !risky)
21345 {
21346 Lisp_Object tem;
21347
21348 oprops = Fcopy_sequence (oprops);
21349 tem = props;
21350 while (CONSP (tem))
21351 {
21352 oprops = Fplist_put (oprops, XCAR (tem),
21353 XCAR (XCDR (tem)));
21354 tem = XCDR (XCDR (tem));
21355 }
21356 props = oprops;
21357 }
21358
21359 aelt = Fassoc (elt, mode_line_proptrans_alist);
21360 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21361 {
21362 /* AELT is what we want. Move it to the front
21363 without consing. */
21364 elt = XCAR (aelt);
21365 mode_line_proptrans_alist
21366 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21367 }
21368 else
21369 {
21370 Lisp_Object tem;
21371
21372 /* If AELT has the wrong props, it is useless.
21373 so get rid of it. */
21374 if (! NILP (aelt))
21375 mode_line_proptrans_alist
21376 = Fdelq (aelt, mode_line_proptrans_alist);
21377
21378 elt = Fcopy_sequence (elt);
21379 Fset_text_properties (make_number (0), Flength (elt),
21380 props, elt);
21381 /* Add this item to mode_line_proptrans_alist. */
21382 mode_line_proptrans_alist
21383 = Fcons (Fcons (elt, props),
21384 mode_line_proptrans_alist);
21385 /* Truncate mode_line_proptrans_alist
21386 to at most 50 elements. */
21387 tem = Fnthcdr (make_number (50),
21388 mode_line_proptrans_alist);
21389 if (! NILP (tem))
21390 XSETCDR (tem, Qnil);
21391 }
21392 }
21393 }
21394
21395 offset = 0;
21396
21397 if (literal)
21398 {
21399 prec = precision - n;
21400 switch (mode_line_target)
21401 {
21402 case MODE_LINE_NOPROP:
21403 case MODE_LINE_TITLE:
21404 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21405 break;
21406 case MODE_LINE_STRING:
21407 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21408 break;
21409 case MODE_LINE_DISPLAY:
21410 n += display_string (NULL, elt, Qnil, 0, 0, it,
21411 0, prec, 0, STRING_MULTIBYTE (elt));
21412 break;
21413 }
21414
21415 break;
21416 }
21417
21418 /* Handle the non-literal case. */
21419
21420 while ((precision <= 0 || n < precision)
21421 && SREF (elt, offset) != 0
21422 && (mode_line_target != MODE_LINE_DISPLAY
21423 || it->current_x < it->last_visible_x))
21424 {
21425 ptrdiff_t last_offset = offset;
21426
21427 /* Advance to end of string or next format specifier. */
21428 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21429 ;
21430
21431 if (offset - 1 != last_offset)
21432 {
21433 ptrdiff_t nchars, nbytes;
21434
21435 /* Output to end of string or up to '%'. Field width
21436 is length of string. Don't output more than
21437 PRECISION allows us. */
21438 offset--;
21439
21440 prec = c_string_width (SDATA (elt) + last_offset,
21441 offset - last_offset, precision - n,
21442 &nchars, &nbytes);
21443
21444 switch (mode_line_target)
21445 {
21446 case MODE_LINE_NOPROP:
21447 case MODE_LINE_TITLE:
21448 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21449 break;
21450 case MODE_LINE_STRING:
21451 {
21452 ptrdiff_t bytepos = last_offset;
21453 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21454 ptrdiff_t endpos = (precision <= 0
21455 ? string_byte_to_char (elt, offset)
21456 : charpos + nchars);
21457
21458 n += store_mode_line_string (NULL,
21459 Fsubstring (elt, make_number (charpos),
21460 make_number (endpos)),
21461 0, 0, 0, Qnil);
21462 }
21463 break;
21464 case MODE_LINE_DISPLAY:
21465 {
21466 ptrdiff_t bytepos = last_offset;
21467 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21468
21469 if (precision <= 0)
21470 nchars = string_byte_to_char (elt, offset) - charpos;
21471 n += display_string (NULL, elt, Qnil, 0, charpos,
21472 it, 0, nchars, 0,
21473 STRING_MULTIBYTE (elt));
21474 }
21475 break;
21476 }
21477 }
21478 else /* c == '%' */
21479 {
21480 ptrdiff_t percent_position = offset;
21481
21482 /* Get the specified minimum width. Zero means
21483 don't pad. */
21484 field = 0;
21485 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21486 field = field * 10 + c - '0';
21487
21488 /* Don't pad beyond the total padding allowed. */
21489 if (field_width - n > 0 && field > field_width - n)
21490 field = field_width - n;
21491
21492 /* Note that either PRECISION <= 0 or N < PRECISION. */
21493 prec = precision - n;
21494
21495 if (c == 'M')
21496 n += display_mode_element (it, depth, field, prec,
21497 Vglobal_mode_string, props,
21498 risky);
21499 else if (c != 0)
21500 {
21501 bool multibyte;
21502 ptrdiff_t bytepos, charpos;
21503 const char *spec;
21504 Lisp_Object string;
21505
21506 bytepos = percent_position;
21507 charpos = (STRING_MULTIBYTE (elt)
21508 ? string_byte_to_char (elt, bytepos)
21509 : bytepos);
21510 spec = decode_mode_spec (it->w, c, field, &string);
21511 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21512
21513 switch (mode_line_target)
21514 {
21515 case MODE_LINE_NOPROP:
21516 case MODE_LINE_TITLE:
21517 n += store_mode_line_noprop (spec, field, prec);
21518 break;
21519 case MODE_LINE_STRING:
21520 {
21521 Lisp_Object tem = build_string (spec);
21522 props = Ftext_properties_at (make_number (charpos), elt);
21523 /* Should only keep face property in props */
21524 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21525 }
21526 break;
21527 case MODE_LINE_DISPLAY:
21528 {
21529 int nglyphs_before, nwritten;
21530
21531 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21532 nwritten = display_string (spec, string, elt,
21533 charpos, 0, it,
21534 field, prec, 0,
21535 multibyte);
21536
21537 /* Assign to the glyphs written above the
21538 string where the `%x' came from, position
21539 of the `%'. */
21540 if (nwritten > 0)
21541 {
21542 struct glyph *glyph
21543 = (it->glyph_row->glyphs[TEXT_AREA]
21544 + nglyphs_before);
21545 int i;
21546
21547 for (i = 0; i < nwritten; ++i)
21548 {
21549 glyph[i].object = elt;
21550 glyph[i].charpos = charpos;
21551 }
21552
21553 n += nwritten;
21554 }
21555 }
21556 break;
21557 }
21558 }
21559 else /* c == 0 */
21560 break;
21561 }
21562 }
21563 }
21564 break;
21565
21566 case Lisp_Symbol:
21567 /* A symbol: process the value of the symbol recursively
21568 as if it appeared here directly. Avoid error if symbol void.
21569 Special case: if value of symbol is a string, output the string
21570 literally. */
21571 {
21572 register Lisp_Object tem;
21573
21574 /* If the variable is not marked as risky to set
21575 then its contents are risky to use. */
21576 if (NILP (Fget (elt, Qrisky_local_variable)))
21577 risky = 1;
21578
21579 tem = Fboundp (elt);
21580 if (!NILP (tem))
21581 {
21582 tem = Fsymbol_value (elt);
21583 /* If value is a string, output that string literally:
21584 don't check for % within it. */
21585 if (STRINGP (tem))
21586 literal = 1;
21587
21588 if (!EQ (tem, elt))
21589 {
21590 /* Give up right away for nil or t. */
21591 elt = tem;
21592 goto tail_recurse;
21593 }
21594 }
21595 }
21596 break;
21597
21598 case Lisp_Cons:
21599 {
21600 register Lisp_Object car, tem;
21601
21602 /* A cons cell: five distinct cases.
21603 If first element is :eval or :propertize, do something special.
21604 If first element is a string or a cons, process all the elements
21605 and effectively concatenate them.
21606 If first element is a negative number, truncate displaying cdr to
21607 at most that many characters. If positive, pad (with spaces)
21608 to at least that many characters.
21609 If first element is a symbol, process the cadr or caddr recursively
21610 according to whether the symbol's value is non-nil or nil. */
21611 car = XCAR (elt);
21612 if (EQ (car, QCeval))
21613 {
21614 /* An element of the form (:eval FORM) means evaluate FORM
21615 and use the result as mode line elements. */
21616
21617 if (risky)
21618 break;
21619
21620 if (CONSP (XCDR (elt)))
21621 {
21622 Lisp_Object spec;
21623 spec = safe_eval (XCAR (XCDR (elt)));
21624 n += display_mode_element (it, depth, field_width - n,
21625 precision - n, spec, props,
21626 risky);
21627 }
21628 }
21629 else if (EQ (car, QCpropertize))
21630 {
21631 /* An element of the form (:propertize ELT PROPS...)
21632 means display ELT but applying properties PROPS. */
21633
21634 if (risky)
21635 break;
21636
21637 if (CONSP (XCDR (elt)))
21638 n += display_mode_element (it, depth, field_width - n,
21639 precision - n, XCAR (XCDR (elt)),
21640 XCDR (XCDR (elt)), risky);
21641 }
21642 else if (SYMBOLP (car))
21643 {
21644 tem = Fboundp (car);
21645 elt = XCDR (elt);
21646 if (!CONSP (elt))
21647 goto invalid;
21648 /* elt is now the cdr, and we know it is a cons cell.
21649 Use its car if CAR has a non-nil value. */
21650 if (!NILP (tem))
21651 {
21652 tem = Fsymbol_value (car);
21653 if (!NILP (tem))
21654 {
21655 elt = XCAR (elt);
21656 goto tail_recurse;
21657 }
21658 }
21659 /* Symbol's value is nil (or symbol is unbound)
21660 Get the cddr of the original list
21661 and if possible find the caddr and use that. */
21662 elt = XCDR (elt);
21663 if (NILP (elt))
21664 break;
21665 else if (!CONSP (elt))
21666 goto invalid;
21667 elt = XCAR (elt);
21668 goto tail_recurse;
21669 }
21670 else if (INTEGERP (car))
21671 {
21672 register int lim = XINT (car);
21673 elt = XCDR (elt);
21674 if (lim < 0)
21675 {
21676 /* Negative int means reduce maximum width. */
21677 if (precision <= 0)
21678 precision = -lim;
21679 else
21680 precision = min (precision, -lim);
21681 }
21682 else if (lim > 0)
21683 {
21684 /* Padding specified. Don't let it be more than
21685 current maximum. */
21686 if (precision > 0)
21687 lim = min (precision, lim);
21688
21689 /* If that's more padding than already wanted, queue it.
21690 But don't reduce padding already specified even if
21691 that is beyond the current truncation point. */
21692 field_width = max (lim, field_width);
21693 }
21694 goto tail_recurse;
21695 }
21696 else if (STRINGP (car) || CONSP (car))
21697 {
21698 Lisp_Object halftail = elt;
21699 int len = 0;
21700
21701 while (CONSP (elt)
21702 && (precision <= 0 || n < precision))
21703 {
21704 n += display_mode_element (it, depth,
21705 /* Do padding only after the last
21706 element in the list. */
21707 (! CONSP (XCDR (elt))
21708 ? field_width - n
21709 : 0),
21710 precision - n, XCAR (elt),
21711 props, risky);
21712 elt = XCDR (elt);
21713 len++;
21714 if ((len & 1) == 0)
21715 halftail = XCDR (halftail);
21716 /* Check for cycle. */
21717 if (EQ (halftail, elt))
21718 break;
21719 }
21720 }
21721 }
21722 break;
21723
21724 default:
21725 invalid:
21726 elt = build_string ("*invalid*");
21727 goto tail_recurse;
21728 }
21729
21730 /* Pad to FIELD_WIDTH. */
21731 if (field_width > 0 && n < field_width)
21732 {
21733 switch (mode_line_target)
21734 {
21735 case MODE_LINE_NOPROP:
21736 case MODE_LINE_TITLE:
21737 n += store_mode_line_noprop ("", field_width - n, 0);
21738 break;
21739 case MODE_LINE_STRING:
21740 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21741 break;
21742 case MODE_LINE_DISPLAY:
21743 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21744 0, 0, 0);
21745 break;
21746 }
21747 }
21748
21749 return n;
21750 }
21751
21752 /* Store a mode-line string element in mode_line_string_list.
21753
21754 If STRING is non-null, display that C string. Otherwise, the Lisp
21755 string LISP_STRING is displayed.
21756
21757 FIELD_WIDTH is the minimum number of output glyphs to produce.
21758 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21759 with spaces. FIELD_WIDTH <= 0 means don't pad.
21760
21761 PRECISION is the maximum number of characters to output from
21762 STRING. PRECISION <= 0 means don't truncate the string.
21763
21764 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21765 properties to the string.
21766
21767 PROPS are the properties to add to the string.
21768 The mode_line_string_face face property is always added to the string.
21769 */
21770
21771 static int
21772 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21773 int field_width, int precision, Lisp_Object props)
21774 {
21775 ptrdiff_t len;
21776 int n = 0;
21777
21778 if (string != NULL)
21779 {
21780 len = strlen (string);
21781 if (precision > 0 && len > precision)
21782 len = precision;
21783 lisp_string = make_string (string, len);
21784 if (NILP (props))
21785 props = mode_line_string_face_prop;
21786 else if (!NILP (mode_line_string_face))
21787 {
21788 Lisp_Object face = Fplist_get (props, Qface);
21789 props = Fcopy_sequence (props);
21790 if (NILP (face))
21791 face = mode_line_string_face;
21792 else
21793 face = list2 (face, mode_line_string_face);
21794 props = Fplist_put (props, Qface, face);
21795 }
21796 Fadd_text_properties (make_number (0), make_number (len),
21797 props, lisp_string);
21798 }
21799 else
21800 {
21801 len = XFASTINT (Flength (lisp_string));
21802 if (precision > 0 && len > precision)
21803 {
21804 len = precision;
21805 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21806 precision = -1;
21807 }
21808 if (!NILP (mode_line_string_face))
21809 {
21810 Lisp_Object face;
21811 if (NILP (props))
21812 props = Ftext_properties_at (make_number (0), lisp_string);
21813 face = Fplist_get (props, Qface);
21814 if (NILP (face))
21815 face = mode_line_string_face;
21816 else
21817 face = list2 (face, mode_line_string_face);
21818 props = list2 (Qface, face);
21819 if (copy_string)
21820 lisp_string = Fcopy_sequence (lisp_string);
21821 }
21822 if (!NILP (props))
21823 Fadd_text_properties (make_number (0), make_number (len),
21824 props, lisp_string);
21825 }
21826
21827 if (len > 0)
21828 {
21829 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21830 n += len;
21831 }
21832
21833 if (field_width > len)
21834 {
21835 field_width -= len;
21836 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21837 if (!NILP (props))
21838 Fadd_text_properties (make_number (0), make_number (field_width),
21839 props, lisp_string);
21840 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21841 n += field_width;
21842 }
21843
21844 return n;
21845 }
21846
21847
21848 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21849 1, 4, 0,
21850 doc: /* Format a string out of a mode line format specification.
21851 First arg FORMAT specifies the mode line format (see `mode-line-format'
21852 for details) to use.
21853
21854 By default, the format is evaluated for the currently selected window.
21855
21856 Optional second arg FACE specifies the face property to put on all
21857 characters for which no face is specified. The value nil means the
21858 default face. The value t means whatever face the window's mode line
21859 currently uses (either `mode-line' or `mode-line-inactive',
21860 depending on whether the window is the selected window or not).
21861 An integer value means the value string has no text
21862 properties.
21863
21864 Optional third and fourth args WINDOW and BUFFER specify the window
21865 and buffer to use as the context for the formatting (defaults
21866 are the selected window and the WINDOW's buffer). */)
21867 (Lisp_Object format, Lisp_Object face,
21868 Lisp_Object window, Lisp_Object buffer)
21869 {
21870 struct it it;
21871 int len;
21872 struct window *w;
21873 struct buffer *old_buffer = NULL;
21874 int face_id;
21875 int no_props = INTEGERP (face);
21876 ptrdiff_t count = SPECPDL_INDEX ();
21877 Lisp_Object str;
21878 int string_start = 0;
21879
21880 w = decode_any_window (window);
21881 XSETWINDOW (window, w);
21882
21883 if (NILP (buffer))
21884 buffer = w->contents;
21885 CHECK_BUFFER (buffer);
21886
21887 /* Make formatting the modeline a non-op when noninteractive, otherwise
21888 there will be problems later caused by a partially initialized frame. */
21889 if (NILP (format) || noninteractive)
21890 return empty_unibyte_string;
21891
21892 if (no_props)
21893 face = Qnil;
21894
21895 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21896 : EQ (face, Qt) ? (EQ (window, selected_window)
21897 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21898 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21899 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21900 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21901 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21902 : DEFAULT_FACE_ID;
21903
21904 old_buffer = current_buffer;
21905
21906 /* Save things including mode_line_proptrans_alist,
21907 and set that to nil so that we don't alter the outer value. */
21908 record_unwind_protect (unwind_format_mode_line,
21909 format_mode_line_unwind_data
21910 (XFRAME (WINDOW_FRAME (w)),
21911 old_buffer, selected_window, 1));
21912 mode_line_proptrans_alist = Qnil;
21913
21914 Fselect_window (window, Qt);
21915 set_buffer_internal_1 (XBUFFER (buffer));
21916
21917 init_iterator (&it, w, -1, -1, NULL, face_id);
21918
21919 if (no_props)
21920 {
21921 mode_line_target = MODE_LINE_NOPROP;
21922 mode_line_string_face_prop = Qnil;
21923 mode_line_string_list = Qnil;
21924 string_start = MODE_LINE_NOPROP_LEN (0);
21925 }
21926 else
21927 {
21928 mode_line_target = MODE_LINE_STRING;
21929 mode_line_string_list = Qnil;
21930 mode_line_string_face = face;
21931 mode_line_string_face_prop
21932 = NILP (face) ? Qnil : list2 (Qface, face);
21933 }
21934
21935 push_kboard (FRAME_KBOARD (it.f));
21936 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21937 pop_kboard ();
21938
21939 if (no_props)
21940 {
21941 len = MODE_LINE_NOPROP_LEN (string_start);
21942 str = make_string (mode_line_noprop_buf + string_start, len);
21943 }
21944 else
21945 {
21946 mode_line_string_list = Fnreverse (mode_line_string_list);
21947 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21948 empty_unibyte_string);
21949 }
21950
21951 unbind_to (count, Qnil);
21952 return str;
21953 }
21954
21955 /* Write a null-terminated, right justified decimal representation of
21956 the positive integer D to BUF using a minimal field width WIDTH. */
21957
21958 static void
21959 pint2str (register char *buf, register int width, register ptrdiff_t d)
21960 {
21961 register char *p = buf;
21962
21963 if (d <= 0)
21964 *p++ = '0';
21965 else
21966 {
21967 while (d > 0)
21968 {
21969 *p++ = d % 10 + '0';
21970 d /= 10;
21971 }
21972 }
21973
21974 for (width -= (int) (p - buf); width > 0; --width)
21975 *p++ = ' ';
21976 *p-- = '\0';
21977 while (p > buf)
21978 {
21979 d = *buf;
21980 *buf++ = *p;
21981 *p-- = d;
21982 }
21983 }
21984
21985 /* Write a null-terminated, right justified decimal and "human
21986 readable" representation of the nonnegative integer D to BUF using
21987 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21988
21989 static const char power_letter[] =
21990 {
21991 0, /* no letter */
21992 'k', /* kilo */
21993 'M', /* mega */
21994 'G', /* giga */
21995 'T', /* tera */
21996 'P', /* peta */
21997 'E', /* exa */
21998 'Z', /* zetta */
21999 'Y' /* yotta */
22000 };
22001
22002 static void
22003 pint2hrstr (char *buf, int width, ptrdiff_t d)
22004 {
22005 /* We aim to represent the nonnegative integer D as
22006 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22007 ptrdiff_t quotient = d;
22008 int remainder = 0;
22009 /* -1 means: do not use TENTHS. */
22010 int tenths = -1;
22011 int exponent = 0;
22012
22013 /* Length of QUOTIENT.TENTHS as a string. */
22014 int length;
22015
22016 char * psuffix;
22017 char * p;
22018
22019 if (quotient >= 1000)
22020 {
22021 /* Scale to the appropriate EXPONENT. */
22022 do
22023 {
22024 remainder = quotient % 1000;
22025 quotient /= 1000;
22026 exponent++;
22027 }
22028 while (quotient >= 1000);
22029
22030 /* Round to nearest and decide whether to use TENTHS or not. */
22031 if (quotient <= 9)
22032 {
22033 tenths = remainder / 100;
22034 if (remainder % 100 >= 50)
22035 {
22036 if (tenths < 9)
22037 tenths++;
22038 else
22039 {
22040 quotient++;
22041 if (quotient == 10)
22042 tenths = -1;
22043 else
22044 tenths = 0;
22045 }
22046 }
22047 }
22048 else
22049 if (remainder >= 500)
22050 {
22051 if (quotient < 999)
22052 quotient++;
22053 else
22054 {
22055 quotient = 1;
22056 exponent++;
22057 tenths = 0;
22058 }
22059 }
22060 }
22061
22062 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22063 if (tenths == -1 && quotient <= 99)
22064 if (quotient <= 9)
22065 length = 1;
22066 else
22067 length = 2;
22068 else
22069 length = 3;
22070 p = psuffix = buf + max (width, length);
22071
22072 /* Print EXPONENT. */
22073 *psuffix++ = power_letter[exponent];
22074 *psuffix = '\0';
22075
22076 /* Print TENTHS. */
22077 if (tenths >= 0)
22078 {
22079 *--p = '0' + tenths;
22080 *--p = '.';
22081 }
22082
22083 /* Print QUOTIENT. */
22084 do
22085 {
22086 int digit = quotient % 10;
22087 *--p = '0' + digit;
22088 }
22089 while ((quotient /= 10) != 0);
22090
22091 /* Print leading spaces. */
22092 while (buf < p)
22093 *--p = ' ';
22094 }
22095
22096 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22097 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22098 type of CODING_SYSTEM. Return updated pointer into BUF. */
22099
22100 static unsigned char invalid_eol_type[] = "(*invalid*)";
22101
22102 static char *
22103 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22104 {
22105 Lisp_Object val;
22106 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22107 const unsigned char *eol_str;
22108 int eol_str_len;
22109 /* The EOL conversion we are using. */
22110 Lisp_Object eoltype;
22111
22112 val = CODING_SYSTEM_SPEC (coding_system);
22113 eoltype = Qnil;
22114
22115 if (!VECTORP (val)) /* Not yet decided. */
22116 {
22117 *buf++ = multibyte ? '-' : ' ';
22118 if (eol_flag)
22119 eoltype = eol_mnemonic_undecided;
22120 /* Don't mention EOL conversion if it isn't decided. */
22121 }
22122 else
22123 {
22124 Lisp_Object attrs;
22125 Lisp_Object eolvalue;
22126
22127 attrs = AREF (val, 0);
22128 eolvalue = AREF (val, 2);
22129
22130 *buf++ = multibyte
22131 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22132 : ' ';
22133
22134 if (eol_flag)
22135 {
22136 /* The EOL conversion that is normal on this system. */
22137
22138 if (NILP (eolvalue)) /* Not yet decided. */
22139 eoltype = eol_mnemonic_undecided;
22140 else if (VECTORP (eolvalue)) /* Not yet decided. */
22141 eoltype = eol_mnemonic_undecided;
22142 else /* eolvalue is Qunix, Qdos, or Qmac. */
22143 eoltype = (EQ (eolvalue, Qunix)
22144 ? eol_mnemonic_unix
22145 : (EQ (eolvalue, Qdos) == 1
22146 ? eol_mnemonic_dos : eol_mnemonic_mac));
22147 }
22148 }
22149
22150 if (eol_flag)
22151 {
22152 /* Mention the EOL conversion if it is not the usual one. */
22153 if (STRINGP (eoltype))
22154 {
22155 eol_str = SDATA (eoltype);
22156 eol_str_len = SBYTES (eoltype);
22157 }
22158 else if (CHARACTERP (eoltype))
22159 {
22160 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22161 int c = XFASTINT (eoltype);
22162 eol_str_len = CHAR_STRING (c, tmp);
22163 eol_str = tmp;
22164 }
22165 else
22166 {
22167 eol_str = invalid_eol_type;
22168 eol_str_len = sizeof (invalid_eol_type) - 1;
22169 }
22170 memcpy (buf, eol_str, eol_str_len);
22171 buf += eol_str_len;
22172 }
22173
22174 return buf;
22175 }
22176
22177 /* Return a string for the output of a mode line %-spec for window W,
22178 generated by character C. FIELD_WIDTH > 0 means pad the string
22179 returned with spaces to that value. Return a Lisp string in
22180 *STRING if the resulting string is taken from that Lisp string.
22181
22182 Note we operate on the current buffer for most purposes. */
22183
22184 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22185
22186 static const char *
22187 decode_mode_spec (struct window *w, register int c, int field_width,
22188 Lisp_Object *string)
22189 {
22190 Lisp_Object obj;
22191 struct frame *f = XFRAME (WINDOW_FRAME (w));
22192 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22193 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22194 produce strings from numerical values, so limit preposterously
22195 large values of FIELD_WIDTH to avoid overrunning the buffer's
22196 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22197 bytes plus the terminating null. */
22198 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22199 struct buffer *b = current_buffer;
22200
22201 obj = Qnil;
22202 *string = Qnil;
22203
22204 switch (c)
22205 {
22206 case '*':
22207 if (!NILP (BVAR (b, read_only)))
22208 return "%";
22209 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22210 return "*";
22211 return "-";
22212
22213 case '+':
22214 /* This differs from %* only for a modified read-only buffer. */
22215 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22216 return "*";
22217 if (!NILP (BVAR (b, read_only)))
22218 return "%";
22219 return "-";
22220
22221 case '&':
22222 /* This differs from %* in ignoring read-only-ness. */
22223 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22224 return "*";
22225 return "-";
22226
22227 case '%':
22228 return "%";
22229
22230 case '[':
22231 {
22232 int i;
22233 char *p;
22234
22235 if (command_loop_level > 5)
22236 return "[[[... ";
22237 p = decode_mode_spec_buf;
22238 for (i = 0; i < command_loop_level; i++)
22239 *p++ = '[';
22240 *p = 0;
22241 return decode_mode_spec_buf;
22242 }
22243
22244 case ']':
22245 {
22246 int i;
22247 char *p;
22248
22249 if (command_loop_level > 5)
22250 return " ...]]]";
22251 p = decode_mode_spec_buf;
22252 for (i = 0; i < command_loop_level; i++)
22253 *p++ = ']';
22254 *p = 0;
22255 return decode_mode_spec_buf;
22256 }
22257
22258 case '-':
22259 {
22260 register int i;
22261
22262 /* Let lots_of_dashes be a string of infinite length. */
22263 if (mode_line_target == MODE_LINE_NOPROP
22264 || mode_line_target == MODE_LINE_STRING)
22265 return "--";
22266 if (field_width <= 0
22267 || field_width > sizeof (lots_of_dashes))
22268 {
22269 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22270 decode_mode_spec_buf[i] = '-';
22271 decode_mode_spec_buf[i] = '\0';
22272 return decode_mode_spec_buf;
22273 }
22274 else
22275 return lots_of_dashes;
22276 }
22277
22278 case 'b':
22279 obj = BVAR (b, name);
22280 break;
22281
22282 case 'c':
22283 /* %c and %l are ignored in `frame-title-format'.
22284 (In redisplay_internal, the frame title is drawn _before_ the
22285 windows are updated, so the stuff which depends on actual
22286 window contents (such as %l) may fail to render properly, or
22287 even crash emacs.) */
22288 if (mode_line_target == MODE_LINE_TITLE)
22289 return "";
22290 else
22291 {
22292 ptrdiff_t col = current_column ();
22293 w->column_number_displayed = col;
22294 pint2str (decode_mode_spec_buf, width, col);
22295 return decode_mode_spec_buf;
22296 }
22297
22298 case 'e':
22299 #ifndef SYSTEM_MALLOC
22300 {
22301 if (NILP (Vmemory_full))
22302 return "";
22303 else
22304 return "!MEM FULL! ";
22305 }
22306 #else
22307 return "";
22308 #endif
22309
22310 case 'F':
22311 /* %F displays the frame name. */
22312 if (!NILP (f->title))
22313 return SSDATA (f->title);
22314 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22315 return SSDATA (f->name);
22316 return "Emacs";
22317
22318 case 'f':
22319 obj = BVAR (b, filename);
22320 break;
22321
22322 case 'i':
22323 {
22324 ptrdiff_t size = ZV - BEGV;
22325 pint2str (decode_mode_spec_buf, width, size);
22326 return decode_mode_spec_buf;
22327 }
22328
22329 case 'I':
22330 {
22331 ptrdiff_t size = ZV - BEGV;
22332 pint2hrstr (decode_mode_spec_buf, width, size);
22333 return decode_mode_spec_buf;
22334 }
22335
22336 case 'l':
22337 {
22338 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22339 ptrdiff_t topline, nlines, height;
22340 ptrdiff_t junk;
22341
22342 /* %c and %l are ignored in `frame-title-format'. */
22343 if (mode_line_target == MODE_LINE_TITLE)
22344 return "";
22345
22346 startpos = marker_position (w->start);
22347 startpos_byte = marker_byte_position (w->start);
22348 height = WINDOW_TOTAL_LINES (w);
22349
22350 /* If we decided that this buffer isn't suitable for line numbers,
22351 don't forget that too fast. */
22352 if (w->base_line_pos == -1)
22353 goto no_value;
22354
22355 /* If the buffer is very big, don't waste time. */
22356 if (INTEGERP (Vline_number_display_limit)
22357 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22358 {
22359 w->base_line_pos = 0;
22360 w->base_line_number = 0;
22361 goto no_value;
22362 }
22363
22364 if (w->base_line_number > 0
22365 && w->base_line_pos > 0
22366 && w->base_line_pos <= startpos)
22367 {
22368 line = w->base_line_number;
22369 linepos = w->base_line_pos;
22370 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22371 }
22372 else
22373 {
22374 line = 1;
22375 linepos = BUF_BEGV (b);
22376 linepos_byte = BUF_BEGV_BYTE (b);
22377 }
22378
22379 /* Count lines from base line to window start position. */
22380 nlines = display_count_lines (linepos_byte,
22381 startpos_byte,
22382 startpos, &junk);
22383
22384 topline = nlines + line;
22385
22386 /* Determine a new base line, if the old one is too close
22387 or too far away, or if we did not have one.
22388 "Too close" means it's plausible a scroll-down would
22389 go back past it. */
22390 if (startpos == BUF_BEGV (b))
22391 {
22392 w->base_line_number = topline;
22393 w->base_line_pos = BUF_BEGV (b);
22394 }
22395 else if (nlines < height + 25 || nlines > height * 3 + 50
22396 || linepos == BUF_BEGV (b))
22397 {
22398 ptrdiff_t limit = BUF_BEGV (b);
22399 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22400 ptrdiff_t position;
22401 ptrdiff_t distance =
22402 (height * 2 + 30) * line_number_display_limit_width;
22403
22404 if (startpos - distance > limit)
22405 {
22406 limit = startpos - distance;
22407 limit_byte = CHAR_TO_BYTE (limit);
22408 }
22409
22410 nlines = display_count_lines (startpos_byte,
22411 limit_byte,
22412 - (height * 2 + 30),
22413 &position);
22414 /* If we couldn't find the lines we wanted within
22415 line_number_display_limit_width chars per line,
22416 give up on line numbers for this window. */
22417 if (position == limit_byte && limit == startpos - distance)
22418 {
22419 w->base_line_pos = -1;
22420 w->base_line_number = 0;
22421 goto no_value;
22422 }
22423
22424 w->base_line_number = topline - nlines;
22425 w->base_line_pos = BYTE_TO_CHAR (position);
22426 }
22427
22428 /* Now count lines from the start pos to point. */
22429 nlines = display_count_lines (startpos_byte,
22430 PT_BYTE, PT, &junk);
22431
22432 /* Record that we did display the line number. */
22433 line_number_displayed = 1;
22434
22435 /* Make the string to show. */
22436 pint2str (decode_mode_spec_buf, width, topline + nlines);
22437 return decode_mode_spec_buf;
22438 no_value:
22439 {
22440 char* p = decode_mode_spec_buf;
22441 int pad = width - 2;
22442 while (pad-- > 0)
22443 *p++ = ' ';
22444 *p++ = '?';
22445 *p++ = '?';
22446 *p = '\0';
22447 return decode_mode_spec_buf;
22448 }
22449 }
22450 break;
22451
22452 case 'm':
22453 obj = BVAR (b, mode_name);
22454 break;
22455
22456 case 'n':
22457 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22458 return " Narrow";
22459 break;
22460
22461 case 'p':
22462 {
22463 ptrdiff_t pos = marker_position (w->start);
22464 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22465
22466 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22467 {
22468 if (pos <= BUF_BEGV (b))
22469 return "All";
22470 else
22471 return "Bottom";
22472 }
22473 else if (pos <= BUF_BEGV (b))
22474 return "Top";
22475 else
22476 {
22477 if (total > 1000000)
22478 /* Do it differently for a large value, to avoid overflow. */
22479 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22480 else
22481 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22482 /* We can't normally display a 3-digit number,
22483 so get us a 2-digit number that is close. */
22484 if (total == 100)
22485 total = 99;
22486 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22487 return decode_mode_spec_buf;
22488 }
22489 }
22490
22491 /* Display percentage of size above the bottom of the screen. */
22492 case 'P':
22493 {
22494 ptrdiff_t toppos = marker_position (w->start);
22495 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22496 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22497
22498 if (botpos >= BUF_ZV (b))
22499 {
22500 if (toppos <= BUF_BEGV (b))
22501 return "All";
22502 else
22503 return "Bottom";
22504 }
22505 else
22506 {
22507 if (total > 1000000)
22508 /* Do it differently for a large value, to avoid overflow. */
22509 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22510 else
22511 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22512 /* We can't normally display a 3-digit number,
22513 so get us a 2-digit number that is close. */
22514 if (total == 100)
22515 total = 99;
22516 if (toppos <= BUF_BEGV (b))
22517 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22518 else
22519 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22520 return decode_mode_spec_buf;
22521 }
22522 }
22523
22524 case 's':
22525 /* status of process */
22526 obj = Fget_buffer_process (Fcurrent_buffer ());
22527 if (NILP (obj))
22528 return "no process";
22529 #ifndef MSDOS
22530 obj = Fsymbol_name (Fprocess_status (obj));
22531 #endif
22532 break;
22533
22534 case '@':
22535 {
22536 ptrdiff_t count = inhibit_garbage_collection ();
22537 Lisp_Object val = call1 (intern ("file-remote-p"),
22538 BVAR (current_buffer, directory));
22539 unbind_to (count, Qnil);
22540
22541 if (NILP (val))
22542 return "-";
22543 else
22544 return "@";
22545 }
22546
22547 case 'z':
22548 /* coding-system (not including end-of-line format) */
22549 case 'Z':
22550 /* coding-system (including end-of-line type) */
22551 {
22552 int eol_flag = (c == 'Z');
22553 char *p = decode_mode_spec_buf;
22554
22555 if (! FRAME_WINDOW_P (f))
22556 {
22557 /* No need to mention EOL here--the terminal never needs
22558 to do EOL conversion. */
22559 p = decode_mode_spec_coding (CODING_ID_NAME
22560 (FRAME_KEYBOARD_CODING (f)->id),
22561 p, 0);
22562 p = decode_mode_spec_coding (CODING_ID_NAME
22563 (FRAME_TERMINAL_CODING (f)->id),
22564 p, 0);
22565 }
22566 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22567 p, eol_flag);
22568
22569 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22570 #ifdef subprocesses
22571 obj = Fget_buffer_process (Fcurrent_buffer ());
22572 if (PROCESSP (obj))
22573 {
22574 p = decode_mode_spec_coding
22575 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22576 p = decode_mode_spec_coding
22577 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22578 }
22579 #endif /* subprocesses */
22580 #endif /* 0 */
22581 *p = 0;
22582 return decode_mode_spec_buf;
22583 }
22584 }
22585
22586 if (STRINGP (obj))
22587 {
22588 *string = obj;
22589 return SSDATA (obj);
22590 }
22591 else
22592 return "";
22593 }
22594
22595
22596 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22597 means count lines back from START_BYTE. But don't go beyond
22598 LIMIT_BYTE. Return the number of lines thus found (always
22599 nonnegative).
22600
22601 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22602 either the position COUNT lines after/before START_BYTE, if we
22603 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22604 COUNT lines. */
22605
22606 static ptrdiff_t
22607 display_count_lines (ptrdiff_t start_byte,
22608 ptrdiff_t limit_byte, ptrdiff_t count,
22609 ptrdiff_t *byte_pos_ptr)
22610 {
22611 register unsigned char *cursor;
22612 unsigned char *base;
22613
22614 register ptrdiff_t ceiling;
22615 register unsigned char *ceiling_addr;
22616 ptrdiff_t orig_count = count;
22617
22618 /* If we are not in selective display mode,
22619 check only for newlines. */
22620 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22621 && !INTEGERP (BVAR (current_buffer, selective_display)));
22622
22623 if (count > 0)
22624 {
22625 while (start_byte < limit_byte)
22626 {
22627 ceiling = BUFFER_CEILING_OF (start_byte);
22628 ceiling = min (limit_byte - 1, ceiling);
22629 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22630 base = (cursor = BYTE_POS_ADDR (start_byte));
22631
22632 do
22633 {
22634 if (selective_display)
22635 {
22636 while (*cursor != '\n' && *cursor != 015
22637 && ++cursor != ceiling_addr)
22638 continue;
22639 if (cursor == ceiling_addr)
22640 break;
22641 }
22642 else
22643 {
22644 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22645 if (! cursor)
22646 break;
22647 }
22648
22649 cursor++;
22650
22651 if (--count == 0)
22652 {
22653 start_byte += cursor - base;
22654 *byte_pos_ptr = start_byte;
22655 return orig_count;
22656 }
22657 }
22658 while (cursor < ceiling_addr);
22659
22660 start_byte += ceiling_addr - base;
22661 }
22662 }
22663 else
22664 {
22665 while (start_byte > limit_byte)
22666 {
22667 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22668 ceiling = max (limit_byte, ceiling);
22669 ceiling_addr = BYTE_POS_ADDR (ceiling);
22670 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22671 while (1)
22672 {
22673 if (selective_display)
22674 {
22675 while (--cursor >= ceiling_addr
22676 && *cursor != '\n' && *cursor != 015)
22677 continue;
22678 if (cursor < ceiling_addr)
22679 break;
22680 }
22681 else
22682 {
22683 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22684 if (! cursor)
22685 break;
22686 }
22687
22688 if (++count == 0)
22689 {
22690 start_byte += cursor - base + 1;
22691 *byte_pos_ptr = start_byte;
22692 /* When scanning backwards, we should
22693 not count the newline posterior to which we stop. */
22694 return - orig_count - 1;
22695 }
22696 }
22697 start_byte += ceiling_addr - base;
22698 }
22699 }
22700
22701 *byte_pos_ptr = limit_byte;
22702
22703 if (count < 0)
22704 return - orig_count + count;
22705 return orig_count - count;
22706
22707 }
22708
22709
22710 \f
22711 /***********************************************************************
22712 Displaying strings
22713 ***********************************************************************/
22714
22715 /* Display a NUL-terminated string, starting with index START.
22716
22717 If STRING is non-null, display that C string. Otherwise, the Lisp
22718 string LISP_STRING is displayed. There's a case that STRING is
22719 non-null and LISP_STRING is not nil. It means STRING is a string
22720 data of LISP_STRING. In that case, we display LISP_STRING while
22721 ignoring its text properties.
22722
22723 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22724 FACE_STRING. Display STRING or LISP_STRING with the face at
22725 FACE_STRING_POS in FACE_STRING:
22726
22727 Display the string in the environment given by IT, but use the
22728 standard display table, temporarily.
22729
22730 FIELD_WIDTH is the minimum number of output glyphs to produce.
22731 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22732 with spaces. If STRING has more characters, more than FIELD_WIDTH
22733 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22734
22735 PRECISION is the maximum number of characters to output from
22736 STRING. PRECISION < 0 means don't truncate the string.
22737
22738 This is roughly equivalent to printf format specifiers:
22739
22740 FIELD_WIDTH PRECISION PRINTF
22741 ----------------------------------------
22742 -1 -1 %s
22743 -1 10 %.10s
22744 10 -1 %10s
22745 20 10 %20.10s
22746
22747 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22748 display them, and < 0 means obey the current buffer's value of
22749 enable_multibyte_characters.
22750
22751 Value is the number of columns displayed. */
22752
22753 static int
22754 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22755 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22756 int field_width, int precision, int max_x, int multibyte)
22757 {
22758 int hpos_at_start = it->hpos;
22759 int saved_face_id = it->face_id;
22760 struct glyph_row *row = it->glyph_row;
22761 ptrdiff_t it_charpos;
22762
22763 /* Initialize the iterator IT for iteration over STRING beginning
22764 with index START. */
22765 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22766 precision, field_width, multibyte);
22767 if (string && STRINGP (lisp_string))
22768 /* LISP_STRING is the one returned by decode_mode_spec. We should
22769 ignore its text properties. */
22770 it->stop_charpos = it->end_charpos;
22771
22772 /* If displaying STRING, set up the face of the iterator from
22773 FACE_STRING, if that's given. */
22774 if (STRINGP (face_string))
22775 {
22776 ptrdiff_t endptr;
22777 struct face *face;
22778
22779 it->face_id
22780 = face_at_string_position (it->w, face_string, face_string_pos,
22781 0, &endptr, it->base_face_id, 0);
22782 face = FACE_FROM_ID (it->f, it->face_id);
22783 it->face_box_p = face->box != FACE_NO_BOX;
22784 }
22785
22786 /* Set max_x to the maximum allowed X position. Don't let it go
22787 beyond the right edge of the window. */
22788 if (max_x <= 0)
22789 max_x = it->last_visible_x;
22790 else
22791 max_x = min (max_x, it->last_visible_x);
22792
22793 /* Skip over display elements that are not visible. because IT->w is
22794 hscrolled. */
22795 if (it->current_x < it->first_visible_x)
22796 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22797 MOVE_TO_POS | MOVE_TO_X);
22798
22799 row->ascent = it->max_ascent;
22800 row->height = it->max_ascent + it->max_descent;
22801 row->phys_ascent = it->max_phys_ascent;
22802 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22803 row->extra_line_spacing = it->max_extra_line_spacing;
22804
22805 if (STRINGP (it->string))
22806 it_charpos = IT_STRING_CHARPOS (*it);
22807 else
22808 it_charpos = IT_CHARPOS (*it);
22809
22810 /* This condition is for the case that we are called with current_x
22811 past last_visible_x. */
22812 while (it->current_x < max_x)
22813 {
22814 int x_before, x, n_glyphs_before, i, nglyphs;
22815
22816 /* Get the next display element. */
22817 if (!get_next_display_element (it))
22818 break;
22819
22820 /* Produce glyphs. */
22821 x_before = it->current_x;
22822 n_glyphs_before = row->used[TEXT_AREA];
22823 PRODUCE_GLYPHS (it);
22824
22825 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22826 i = 0;
22827 x = x_before;
22828 while (i < nglyphs)
22829 {
22830 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22831
22832 if (it->line_wrap != TRUNCATE
22833 && x + glyph->pixel_width > max_x)
22834 {
22835 /* End of continued line or max_x reached. */
22836 if (CHAR_GLYPH_PADDING_P (*glyph))
22837 {
22838 /* A wide character is unbreakable. */
22839 if (row->reversed_p)
22840 unproduce_glyphs (it, row->used[TEXT_AREA]
22841 - n_glyphs_before);
22842 row->used[TEXT_AREA] = n_glyphs_before;
22843 it->current_x = x_before;
22844 }
22845 else
22846 {
22847 if (row->reversed_p)
22848 unproduce_glyphs (it, row->used[TEXT_AREA]
22849 - (n_glyphs_before + i));
22850 row->used[TEXT_AREA] = n_glyphs_before + i;
22851 it->current_x = x;
22852 }
22853 break;
22854 }
22855 else if (x + glyph->pixel_width >= it->first_visible_x)
22856 {
22857 /* Glyph is at least partially visible. */
22858 ++it->hpos;
22859 if (x < it->first_visible_x)
22860 row->x = x - it->first_visible_x;
22861 }
22862 else
22863 {
22864 /* Glyph is off the left margin of the display area.
22865 Should not happen. */
22866 emacs_abort ();
22867 }
22868
22869 row->ascent = max (row->ascent, it->max_ascent);
22870 row->height = max (row->height, it->max_ascent + it->max_descent);
22871 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22872 row->phys_height = max (row->phys_height,
22873 it->max_phys_ascent + it->max_phys_descent);
22874 row->extra_line_spacing = max (row->extra_line_spacing,
22875 it->max_extra_line_spacing);
22876 x += glyph->pixel_width;
22877 ++i;
22878 }
22879
22880 /* Stop if max_x reached. */
22881 if (i < nglyphs)
22882 break;
22883
22884 /* Stop at line ends. */
22885 if (ITERATOR_AT_END_OF_LINE_P (it))
22886 {
22887 it->continuation_lines_width = 0;
22888 break;
22889 }
22890
22891 set_iterator_to_next (it, 1);
22892 if (STRINGP (it->string))
22893 it_charpos = IT_STRING_CHARPOS (*it);
22894 else
22895 it_charpos = IT_CHARPOS (*it);
22896
22897 /* Stop if truncating at the right edge. */
22898 if (it->line_wrap == TRUNCATE
22899 && it->current_x >= it->last_visible_x)
22900 {
22901 /* Add truncation mark, but don't do it if the line is
22902 truncated at a padding space. */
22903 if (it_charpos < it->string_nchars)
22904 {
22905 if (!FRAME_WINDOW_P (it->f))
22906 {
22907 int ii, n;
22908
22909 if (it->current_x > it->last_visible_x)
22910 {
22911 if (!row->reversed_p)
22912 {
22913 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22914 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22915 break;
22916 }
22917 else
22918 {
22919 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22920 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22921 break;
22922 unproduce_glyphs (it, ii + 1);
22923 ii = row->used[TEXT_AREA] - (ii + 1);
22924 }
22925 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22926 {
22927 row->used[TEXT_AREA] = ii;
22928 produce_special_glyphs (it, IT_TRUNCATION);
22929 }
22930 }
22931 produce_special_glyphs (it, IT_TRUNCATION);
22932 }
22933 row->truncated_on_right_p = 1;
22934 }
22935 break;
22936 }
22937 }
22938
22939 /* Maybe insert a truncation at the left. */
22940 if (it->first_visible_x
22941 && it_charpos > 0)
22942 {
22943 if (!FRAME_WINDOW_P (it->f)
22944 || (row->reversed_p
22945 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22946 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22947 insert_left_trunc_glyphs (it);
22948 row->truncated_on_left_p = 1;
22949 }
22950
22951 it->face_id = saved_face_id;
22952
22953 /* Value is number of columns displayed. */
22954 return it->hpos - hpos_at_start;
22955 }
22956
22957
22958 \f
22959 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22960 appears as an element of LIST or as the car of an element of LIST.
22961 If PROPVAL is a list, compare each element against LIST in that
22962 way, and return 1/2 if any element of PROPVAL is found in LIST.
22963 Otherwise return 0. This function cannot quit.
22964 The return value is 2 if the text is invisible but with an ellipsis
22965 and 1 if it's invisible and without an ellipsis. */
22966
22967 int
22968 invisible_p (register Lisp_Object propval, Lisp_Object list)
22969 {
22970 register Lisp_Object tail, proptail;
22971
22972 for (tail = list; CONSP (tail); tail = XCDR (tail))
22973 {
22974 register Lisp_Object tem;
22975 tem = XCAR (tail);
22976 if (EQ (propval, tem))
22977 return 1;
22978 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22979 return NILP (XCDR (tem)) ? 1 : 2;
22980 }
22981
22982 if (CONSP (propval))
22983 {
22984 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22985 {
22986 Lisp_Object propelt;
22987 propelt = XCAR (proptail);
22988 for (tail = list; CONSP (tail); tail = XCDR (tail))
22989 {
22990 register Lisp_Object tem;
22991 tem = XCAR (tail);
22992 if (EQ (propelt, tem))
22993 return 1;
22994 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22995 return NILP (XCDR (tem)) ? 1 : 2;
22996 }
22997 }
22998 }
22999
23000 return 0;
23001 }
23002
23003 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23004 doc: /* Non-nil if the property makes the text invisible.
23005 POS-OR-PROP can be a marker or number, in which case it is taken to be
23006 a position in the current buffer and the value of the `invisible' property
23007 is checked; or it can be some other value, which is then presumed to be the
23008 value of the `invisible' property of the text of interest.
23009 The non-nil value returned can be t for truly invisible text or something
23010 else if the text is replaced by an ellipsis. */)
23011 (Lisp_Object pos_or_prop)
23012 {
23013 Lisp_Object prop
23014 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23015 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23016 : pos_or_prop);
23017 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23018 return (invis == 0 ? Qnil
23019 : invis == 1 ? Qt
23020 : make_number (invis));
23021 }
23022
23023 /* Calculate a width or height in pixels from a specification using
23024 the following elements:
23025
23026 SPEC ::=
23027 NUM - a (fractional) multiple of the default font width/height
23028 (NUM) - specifies exactly NUM pixels
23029 UNIT - a fixed number of pixels, see below.
23030 ELEMENT - size of a display element in pixels, see below.
23031 (NUM . SPEC) - equals NUM * SPEC
23032 (+ SPEC SPEC ...) - add pixel values
23033 (- SPEC SPEC ...) - subtract pixel values
23034 (- SPEC) - negate pixel value
23035
23036 NUM ::=
23037 INT or FLOAT - a number constant
23038 SYMBOL - use symbol's (buffer local) variable binding.
23039
23040 UNIT ::=
23041 in - pixels per inch *)
23042 mm - pixels per 1/1000 meter *)
23043 cm - pixels per 1/100 meter *)
23044 width - width of current font in pixels.
23045 height - height of current font in pixels.
23046
23047 *) using the ratio(s) defined in display-pixels-per-inch.
23048
23049 ELEMENT ::=
23050
23051 left-fringe - left fringe width in pixels
23052 right-fringe - right fringe width in pixels
23053
23054 left-margin - left margin width in pixels
23055 right-margin - right margin width in pixels
23056
23057 scroll-bar - scroll-bar area width in pixels
23058
23059 Examples:
23060
23061 Pixels corresponding to 5 inches:
23062 (5 . in)
23063
23064 Total width of non-text areas on left side of window (if scroll-bar is on left):
23065 '(space :width (+ left-fringe left-margin scroll-bar))
23066
23067 Align to first text column (in header line):
23068 '(space :align-to 0)
23069
23070 Align to middle of text area minus half the width of variable `my-image'
23071 containing a loaded image:
23072 '(space :align-to (0.5 . (- text my-image)))
23073
23074 Width of left margin minus width of 1 character in the default font:
23075 '(space :width (- left-margin 1))
23076
23077 Width of left margin minus width of 2 characters in the current font:
23078 '(space :width (- left-margin (2 . width)))
23079
23080 Center 1 character over left-margin (in header line):
23081 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23082
23083 Different ways to express width of left fringe plus left margin minus one pixel:
23084 '(space :width (- (+ left-fringe left-margin) (1)))
23085 '(space :width (+ left-fringe left-margin (- (1))))
23086 '(space :width (+ left-fringe left-margin (-1)))
23087
23088 */
23089
23090 static int
23091 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23092 struct font *font, int width_p, int *align_to)
23093 {
23094 double pixels;
23095
23096 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23097 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23098
23099 if (NILP (prop))
23100 return OK_PIXELS (0);
23101
23102 eassert (FRAME_LIVE_P (it->f));
23103
23104 if (SYMBOLP (prop))
23105 {
23106 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23107 {
23108 char *unit = SSDATA (SYMBOL_NAME (prop));
23109
23110 if (unit[0] == 'i' && unit[1] == 'n')
23111 pixels = 1.0;
23112 else if (unit[0] == 'm' && unit[1] == 'm')
23113 pixels = 25.4;
23114 else if (unit[0] == 'c' && unit[1] == 'm')
23115 pixels = 2.54;
23116 else
23117 pixels = 0;
23118 if (pixels > 0)
23119 {
23120 double ppi = (width_p ? FRAME_RES_X (it->f)
23121 : FRAME_RES_Y (it->f));
23122
23123 if (ppi > 0)
23124 return OK_PIXELS (ppi / pixels);
23125 return 0;
23126 }
23127 }
23128
23129 #ifdef HAVE_WINDOW_SYSTEM
23130 if (EQ (prop, Qheight))
23131 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23132 if (EQ (prop, Qwidth))
23133 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23134 #else
23135 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23136 return OK_PIXELS (1);
23137 #endif
23138
23139 if (EQ (prop, Qtext))
23140 return OK_PIXELS (width_p
23141 ? window_box_width (it->w, TEXT_AREA)
23142 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23143
23144 if (align_to && *align_to < 0)
23145 {
23146 *res = 0;
23147 if (EQ (prop, Qleft))
23148 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23149 if (EQ (prop, Qright))
23150 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23151 if (EQ (prop, Qcenter))
23152 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23153 + window_box_width (it->w, TEXT_AREA) / 2);
23154 if (EQ (prop, Qleft_fringe))
23155 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23156 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23157 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23158 if (EQ (prop, Qright_fringe))
23159 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23160 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23161 : window_box_right_offset (it->w, TEXT_AREA));
23162 if (EQ (prop, Qleft_margin))
23163 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23164 if (EQ (prop, Qright_margin))
23165 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23166 if (EQ (prop, Qscroll_bar))
23167 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23168 ? 0
23169 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23170 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23171 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23172 : 0)));
23173 }
23174 else
23175 {
23176 if (EQ (prop, Qleft_fringe))
23177 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23178 if (EQ (prop, Qright_fringe))
23179 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23180 if (EQ (prop, Qleft_margin))
23181 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23182 if (EQ (prop, Qright_margin))
23183 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23184 if (EQ (prop, Qscroll_bar))
23185 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23186 }
23187
23188 prop = buffer_local_value_1 (prop, it->w->contents);
23189 if (EQ (prop, Qunbound))
23190 prop = Qnil;
23191 }
23192
23193 if (INTEGERP (prop) || FLOATP (prop))
23194 {
23195 int base_unit = (width_p
23196 ? FRAME_COLUMN_WIDTH (it->f)
23197 : FRAME_LINE_HEIGHT (it->f));
23198 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23199 }
23200
23201 if (CONSP (prop))
23202 {
23203 Lisp_Object car = XCAR (prop);
23204 Lisp_Object cdr = XCDR (prop);
23205
23206 if (SYMBOLP (car))
23207 {
23208 #ifdef HAVE_WINDOW_SYSTEM
23209 if (FRAME_WINDOW_P (it->f)
23210 && valid_image_p (prop))
23211 {
23212 ptrdiff_t id = lookup_image (it->f, prop);
23213 struct image *img = IMAGE_FROM_ID (it->f, id);
23214
23215 return OK_PIXELS (width_p ? img->width : img->height);
23216 }
23217 #endif
23218 if (EQ (car, Qplus) || EQ (car, Qminus))
23219 {
23220 int first = 1;
23221 double px;
23222
23223 pixels = 0;
23224 while (CONSP (cdr))
23225 {
23226 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23227 font, width_p, align_to))
23228 return 0;
23229 if (first)
23230 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23231 else
23232 pixels += px;
23233 cdr = XCDR (cdr);
23234 }
23235 if (EQ (car, Qminus))
23236 pixels = -pixels;
23237 return OK_PIXELS (pixels);
23238 }
23239
23240 car = buffer_local_value_1 (car, it->w->contents);
23241 if (EQ (car, Qunbound))
23242 car = Qnil;
23243 }
23244
23245 if (INTEGERP (car) || FLOATP (car))
23246 {
23247 double fact;
23248 pixels = XFLOATINT (car);
23249 if (NILP (cdr))
23250 return OK_PIXELS (pixels);
23251 if (calc_pixel_width_or_height (&fact, it, cdr,
23252 font, width_p, align_to))
23253 return OK_PIXELS (pixels * fact);
23254 return 0;
23255 }
23256
23257 return 0;
23258 }
23259
23260 return 0;
23261 }
23262
23263 \f
23264 /***********************************************************************
23265 Glyph Display
23266 ***********************************************************************/
23267
23268 #ifdef HAVE_WINDOW_SYSTEM
23269
23270 #ifdef GLYPH_DEBUG
23271
23272 void
23273 dump_glyph_string (struct glyph_string *s)
23274 {
23275 fprintf (stderr, "glyph string\n");
23276 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23277 s->x, s->y, s->width, s->height);
23278 fprintf (stderr, " ybase = %d\n", s->ybase);
23279 fprintf (stderr, " hl = %d\n", s->hl);
23280 fprintf (stderr, " left overhang = %d, right = %d\n",
23281 s->left_overhang, s->right_overhang);
23282 fprintf (stderr, " nchars = %d\n", s->nchars);
23283 fprintf (stderr, " extends to end of line = %d\n",
23284 s->extends_to_end_of_line_p);
23285 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23286 fprintf (stderr, " bg width = %d\n", s->background_width);
23287 }
23288
23289 #endif /* GLYPH_DEBUG */
23290
23291 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23292 of XChar2b structures for S; it can't be allocated in
23293 init_glyph_string because it must be allocated via `alloca'. W
23294 is the window on which S is drawn. ROW and AREA are the glyph row
23295 and area within the row from which S is constructed. START is the
23296 index of the first glyph structure covered by S. HL is a
23297 face-override for drawing S. */
23298
23299 #ifdef HAVE_NTGUI
23300 #define OPTIONAL_HDC(hdc) HDC hdc,
23301 #define DECLARE_HDC(hdc) HDC hdc;
23302 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23303 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23304 #endif
23305
23306 #ifndef OPTIONAL_HDC
23307 #define OPTIONAL_HDC(hdc)
23308 #define DECLARE_HDC(hdc)
23309 #define ALLOCATE_HDC(hdc, f)
23310 #define RELEASE_HDC(hdc, f)
23311 #endif
23312
23313 static void
23314 init_glyph_string (struct glyph_string *s,
23315 OPTIONAL_HDC (hdc)
23316 XChar2b *char2b, struct window *w, struct glyph_row *row,
23317 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23318 {
23319 memset (s, 0, sizeof *s);
23320 s->w = w;
23321 s->f = XFRAME (w->frame);
23322 #ifdef HAVE_NTGUI
23323 s->hdc = hdc;
23324 #endif
23325 s->display = FRAME_X_DISPLAY (s->f);
23326 s->window = FRAME_X_WINDOW (s->f);
23327 s->char2b = char2b;
23328 s->hl = hl;
23329 s->row = row;
23330 s->area = area;
23331 s->first_glyph = row->glyphs[area] + start;
23332 s->height = row->height;
23333 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23334 s->ybase = s->y + row->ascent;
23335 }
23336
23337
23338 /* Append the list of glyph strings with head H and tail T to the list
23339 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23340
23341 static void
23342 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23343 struct glyph_string *h, struct glyph_string *t)
23344 {
23345 if (h)
23346 {
23347 if (*head)
23348 (*tail)->next = h;
23349 else
23350 *head = h;
23351 h->prev = *tail;
23352 *tail = t;
23353 }
23354 }
23355
23356
23357 /* Prepend the list of glyph strings with head H and tail T to the
23358 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23359 result. */
23360
23361 static void
23362 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23363 struct glyph_string *h, struct glyph_string *t)
23364 {
23365 if (h)
23366 {
23367 if (*head)
23368 (*head)->prev = t;
23369 else
23370 *tail = t;
23371 t->next = *head;
23372 *head = h;
23373 }
23374 }
23375
23376
23377 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23378 Set *HEAD and *TAIL to the resulting list. */
23379
23380 static void
23381 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23382 struct glyph_string *s)
23383 {
23384 s->next = s->prev = NULL;
23385 append_glyph_string_lists (head, tail, s, s);
23386 }
23387
23388
23389 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23390 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23391 make sure that X resources for the face returned are allocated.
23392 Value is a pointer to a realized face that is ready for display if
23393 DISPLAY_P is non-zero. */
23394
23395 static struct face *
23396 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23397 XChar2b *char2b, int display_p)
23398 {
23399 struct face *face = FACE_FROM_ID (f, face_id);
23400 unsigned code = 0;
23401
23402 if (face->font)
23403 {
23404 code = face->font->driver->encode_char (face->font, c);
23405
23406 if (code == FONT_INVALID_CODE)
23407 code = 0;
23408 }
23409 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23410
23411 /* Make sure X resources of the face are allocated. */
23412 #ifdef HAVE_X_WINDOWS
23413 if (display_p)
23414 #endif
23415 {
23416 eassert (face != NULL);
23417 PREPARE_FACE_FOR_DISPLAY (f, face);
23418 }
23419
23420 return face;
23421 }
23422
23423
23424 /* Get face and two-byte form of character glyph GLYPH on frame F.
23425 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23426 a pointer to a realized face that is ready for display. */
23427
23428 static struct face *
23429 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23430 XChar2b *char2b, int *two_byte_p)
23431 {
23432 struct face *face;
23433 unsigned code = 0;
23434
23435 eassert (glyph->type == CHAR_GLYPH);
23436 face = FACE_FROM_ID (f, glyph->face_id);
23437
23438 /* Make sure X resources of the face are allocated. */
23439 eassert (face != NULL);
23440 PREPARE_FACE_FOR_DISPLAY (f, face);
23441
23442 if (two_byte_p)
23443 *two_byte_p = 0;
23444
23445 if (face->font)
23446 {
23447 if (CHAR_BYTE8_P (glyph->u.ch))
23448 code = CHAR_TO_BYTE8 (glyph->u.ch);
23449 else
23450 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23451
23452 if (code == FONT_INVALID_CODE)
23453 code = 0;
23454 }
23455
23456 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23457 return face;
23458 }
23459
23460
23461 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23462 Return 1 if FONT has a glyph for C, otherwise return 0. */
23463
23464 static int
23465 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23466 {
23467 unsigned code;
23468
23469 if (CHAR_BYTE8_P (c))
23470 code = CHAR_TO_BYTE8 (c);
23471 else
23472 code = font->driver->encode_char (font, c);
23473
23474 if (code == FONT_INVALID_CODE)
23475 return 0;
23476 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23477 return 1;
23478 }
23479
23480
23481 /* Fill glyph string S with composition components specified by S->cmp.
23482
23483 BASE_FACE is the base face of the composition.
23484 S->cmp_from is the index of the first component for S.
23485
23486 OVERLAPS non-zero means S should draw the foreground only, and use
23487 its physical height for clipping. See also draw_glyphs.
23488
23489 Value is the index of a component not in S. */
23490
23491 static int
23492 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23493 int overlaps)
23494 {
23495 int i;
23496 /* For all glyphs of this composition, starting at the offset
23497 S->cmp_from, until we reach the end of the definition or encounter a
23498 glyph that requires the different face, add it to S. */
23499 struct face *face;
23500
23501 eassert (s);
23502
23503 s->for_overlaps = overlaps;
23504 s->face = NULL;
23505 s->font = NULL;
23506 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23507 {
23508 int c = COMPOSITION_GLYPH (s->cmp, i);
23509
23510 /* TAB in a composition means display glyphs with padding space
23511 on the left or right. */
23512 if (c != '\t')
23513 {
23514 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23515 -1, Qnil);
23516
23517 face = get_char_face_and_encoding (s->f, c, face_id,
23518 s->char2b + i, 1);
23519 if (face)
23520 {
23521 if (! s->face)
23522 {
23523 s->face = face;
23524 s->font = s->face->font;
23525 }
23526 else if (s->face != face)
23527 break;
23528 }
23529 }
23530 ++s->nchars;
23531 }
23532 s->cmp_to = i;
23533
23534 if (s->face == NULL)
23535 {
23536 s->face = base_face->ascii_face;
23537 s->font = s->face->font;
23538 }
23539
23540 /* All glyph strings for the same composition has the same width,
23541 i.e. the width set for the first component of the composition. */
23542 s->width = s->first_glyph->pixel_width;
23543
23544 /* If the specified font could not be loaded, use the frame's
23545 default font, but record the fact that we couldn't load it in
23546 the glyph string so that we can draw rectangles for the
23547 characters of the glyph string. */
23548 if (s->font == NULL)
23549 {
23550 s->font_not_found_p = 1;
23551 s->font = FRAME_FONT (s->f);
23552 }
23553
23554 /* Adjust base line for subscript/superscript text. */
23555 s->ybase += s->first_glyph->voffset;
23556
23557 /* This glyph string must always be drawn with 16-bit functions. */
23558 s->two_byte_p = 1;
23559
23560 return s->cmp_to;
23561 }
23562
23563 static int
23564 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23565 int start, int end, int overlaps)
23566 {
23567 struct glyph *glyph, *last;
23568 Lisp_Object lgstring;
23569 int i;
23570
23571 s->for_overlaps = overlaps;
23572 glyph = s->row->glyphs[s->area] + start;
23573 last = s->row->glyphs[s->area] + end;
23574 s->cmp_id = glyph->u.cmp.id;
23575 s->cmp_from = glyph->slice.cmp.from;
23576 s->cmp_to = glyph->slice.cmp.to + 1;
23577 s->face = FACE_FROM_ID (s->f, face_id);
23578 lgstring = composition_gstring_from_id (s->cmp_id);
23579 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23580 glyph++;
23581 while (glyph < last
23582 && glyph->u.cmp.automatic
23583 && glyph->u.cmp.id == s->cmp_id
23584 && s->cmp_to == glyph->slice.cmp.from)
23585 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23586
23587 for (i = s->cmp_from; i < s->cmp_to; i++)
23588 {
23589 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23590 unsigned code = LGLYPH_CODE (lglyph);
23591
23592 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23593 }
23594 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23595 return glyph - s->row->glyphs[s->area];
23596 }
23597
23598
23599 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23600 See the comment of fill_glyph_string for arguments.
23601 Value is the index of the first glyph not in S. */
23602
23603
23604 static int
23605 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23606 int start, int end, int overlaps)
23607 {
23608 struct glyph *glyph, *last;
23609 int voffset;
23610
23611 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23612 s->for_overlaps = overlaps;
23613 glyph = s->row->glyphs[s->area] + start;
23614 last = s->row->glyphs[s->area] + end;
23615 voffset = glyph->voffset;
23616 s->face = FACE_FROM_ID (s->f, face_id);
23617 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23618 s->nchars = 1;
23619 s->width = glyph->pixel_width;
23620 glyph++;
23621 while (glyph < last
23622 && glyph->type == GLYPHLESS_GLYPH
23623 && glyph->voffset == voffset
23624 && glyph->face_id == face_id)
23625 {
23626 s->nchars++;
23627 s->width += glyph->pixel_width;
23628 glyph++;
23629 }
23630 s->ybase += voffset;
23631 return glyph - s->row->glyphs[s->area];
23632 }
23633
23634
23635 /* Fill glyph string S from a sequence of character glyphs.
23636
23637 FACE_ID is the face id of the string. START is the index of the
23638 first glyph to consider, END is the index of the last + 1.
23639 OVERLAPS non-zero means S should draw the foreground only, and use
23640 its physical height for clipping. See also draw_glyphs.
23641
23642 Value is the index of the first glyph not in S. */
23643
23644 static int
23645 fill_glyph_string (struct glyph_string *s, int face_id,
23646 int start, int end, int overlaps)
23647 {
23648 struct glyph *glyph, *last;
23649 int voffset;
23650 int glyph_not_available_p;
23651
23652 eassert (s->f == XFRAME (s->w->frame));
23653 eassert (s->nchars == 0);
23654 eassert (start >= 0 && end > start);
23655
23656 s->for_overlaps = overlaps;
23657 glyph = s->row->glyphs[s->area] + start;
23658 last = s->row->glyphs[s->area] + end;
23659 voffset = glyph->voffset;
23660 s->padding_p = glyph->padding_p;
23661 glyph_not_available_p = glyph->glyph_not_available_p;
23662
23663 while (glyph < last
23664 && glyph->type == CHAR_GLYPH
23665 && glyph->voffset == voffset
23666 /* Same face id implies same font, nowadays. */
23667 && glyph->face_id == face_id
23668 && glyph->glyph_not_available_p == glyph_not_available_p)
23669 {
23670 int two_byte_p;
23671
23672 s->face = get_glyph_face_and_encoding (s->f, glyph,
23673 s->char2b + s->nchars,
23674 &two_byte_p);
23675 s->two_byte_p = two_byte_p;
23676 ++s->nchars;
23677 eassert (s->nchars <= end - start);
23678 s->width += glyph->pixel_width;
23679 if (glyph++->padding_p != s->padding_p)
23680 break;
23681 }
23682
23683 s->font = s->face->font;
23684
23685 /* If the specified font could not be loaded, use the frame's font,
23686 but record the fact that we couldn't load it in
23687 S->font_not_found_p so that we can draw rectangles for the
23688 characters of the glyph string. */
23689 if (s->font == NULL || glyph_not_available_p)
23690 {
23691 s->font_not_found_p = 1;
23692 s->font = FRAME_FONT (s->f);
23693 }
23694
23695 /* Adjust base line for subscript/superscript text. */
23696 s->ybase += voffset;
23697
23698 eassert (s->face && s->face->gc);
23699 return glyph - s->row->glyphs[s->area];
23700 }
23701
23702
23703 /* Fill glyph string S from image glyph S->first_glyph. */
23704
23705 static void
23706 fill_image_glyph_string (struct glyph_string *s)
23707 {
23708 eassert (s->first_glyph->type == IMAGE_GLYPH);
23709 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23710 eassert (s->img);
23711 s->slice = s->first_glyph->slice.img;
23712 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23713 s->font = s->face->font;
23714 s->width = s->first_glyph->pixel_width;
23715
23716 /* Adjust base line for subscript/superscript text. */
23717 s->ybase += s->first_glyph->voffset;
23718 }
23719
23720
23721 /* Fill glyph string S from a sequence of stretch glyphs.
23722
23723 START is the index of the first glyph to consider,
23724 END is the index of the last + 1.
23725
23726 Value is the index of the first glyph not in S. */
23727
23728 static int
23729 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23730 {
23731 struct glyph *glyph, *last;
23732 int voffset, face_id;
23733
23734 eassert (s->first_glyph->type == STRETCH_GLYPH);
23735
23736 glyph = s->row->glyphs[s->area] + start;
23737 last = s->row->glyphs[s->area] + end;
23738 face_id = glyph->face_id;
23739 s->face = FACE_FROM_ID (s->f, face_id);
23740 s->font = s->face->font;
23741 s->width = glyph->pixel_width;
23742 s->nchars = 1;
23743 voffset = glyph->voffset;
23744
23745 for (++glyph;
23746 (glyph < last
23747 && glyph->type == STRETCH_GLYPH
23748 && glyph->voffset == voffset
23749 && glyph->face_id == face_id);
23750 ++glyph)
23751 s->width += glyph->pixel_width;
23752
23753 /* Adjust base line for subscript/superscript text. */
23754 s->ybase += voffset;
23755
23756 /* The case that face->gc == 0 is handled when drawing the glyph
23757 string by calling PREPARE_FACE_FOR_DISPLAY. */
23758 eassert (s->face);
23759 return glyph - s->row->glyphs[s->area];
23760 }
23761
23762 static struct font_metrics *
23763 get_per_char_metric (struct font *font, XChar2b *char2b)
23764 {
23765 static struct font_metrics metrics;
23766 unsigned code;
23767
23768 if (! font)
23769 return NULL;
23770 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23771 if (code == FONT_INVALID_CODE)
23772 return NULL;
23773 font->driver->text_extents (font, &code, 1, &metrics);
23774 return &metrics;
23775 }
23776
23777 /* EXPORT for RIF:
23778 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23779 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23780 assumed to be zero. */
23781
23782 void
23783 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23784 {
23785 *left = *right = 0;
23786
23787 if (glyph->type == CHAR_GLYPH)
23788 {
23789 struct face *face;
23790 XChar2b char2b;
23791 struct font_metrics *pcm;
23792
23793 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23794 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23795 {
23796 if (pcm->rbearing > pcm->width)
23797 *right = pcm->rbearing - pcm->width;
23798 if (pcm->lbearing < 0)
23799 *left = -pcm->lbearing;
23800 }
23801 }
23802 else if (glyph->type == COMPOSITE_GLYPH)
23803 {
23804 if (! glyph->u.cmp.automatic)
23805 {
23806 struct composition *cmp = composition_table[glyph->u.cmp.id];
23807
23808 if (cmp->rbearing > cmp->pixel_width)
23809 *right = cmp->rbearing - cmp->pixel_width;
23810 if (cmp->lbearing < 0)
23811 *left = - cmp->lbearing;
23812 }
23813 else
23814 {
23815 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23816 struct font_metrics metrics;
23817
23818 composition_gstring_width (gstring, glyph->slice.cmp.from,
23819 glyph->slice.cmp.to + 1, &metrics);
23820 if (metrics.rbearing > metrics.width)
23821 *right = metrics.rbearing - metrics.width;
23822 if (metrics.lbearing < 0)
23823 *left = - metrics.lbearing;
23824 }
23825 }
23826 }
23827
23828
23829 /* Return the index of the first glyph preceding glyph string S that
23830 is overwritten by S because of S's left overhang. Value is -1
23831 if no glyphs are overwritten. */
23832
23833 static int
23834 left_overwritten (struct glyph_string *s)
23835 {
23836 int k;
23837
23838 if (s->left_overhang)
23839 {
23840 int x = 0, i;
23841 struct glyph *glyphs = s->row->glyphs[s->area];
23842 int first = s->first_glyph - glyphs;
23843
23844 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23845 x -= glyphs[i].pixel_width;
23846
23847 k = i + 1;
23848 }
23849 else
23850 k = -1;
23851
23852 return k;
23853 }
23854
23855
23856 /* Return the index of the first glyph preceding glyph string S that
23857 is overwriting S because of its right overhang. Value is -1 if no
23858 glyph in front of S overwrites S. */
23859
23860 static int
23861 left_overwriting (struct glyph_string *s)
23862 {
23863 int i, k, x;
23864 struct glyph *glyphs = s->row->glyphs[s->area];
23865 int first = s->first_glyph - glyphs;
23866
23867 k = -1;
23868 x = 0;
23869 for (i = first - 1; i >= 0; --i)
23870 {
23871 int left, right;
23872 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23873 if (x + right > 0)
23874 k = i;
23875 x -= glyphs[i].pixel_width;
23876 }
23877
23878 return k;
23879 }
23880
23881
23882 /* Return the index of the last glyph following glyph string S that is
23883 overwritten by S because of S's right overhang. Value is -1 if
23884 no such glyph is found. */
23885
23886 static int
23887 right_overwritten (struct glyph_string *s)
23888 {
23889 int k = -1;
23890
23891 if (s->right_overhang)
23892 {
23893 int x = 0, i;
23894 struct glyph *glyphs = s->row->glyphs[s->area];
23895 int first = (s->first_glyph - glyphs
23896 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23897 int end = s->row->used[s->area];
23898
23899 for (i = first; i < end && s->right_overhang > x; ++i)
23900 x += glyphs[i].pixel_width;
23901
23902 k = i;
23903 }
23904
23905 return k;
23906 }
23907
23908
23909 /* Return the index of the last glyph following glyph string S that
23910 overwrites S because of its left overhang. Value is negative
23911 if no such glyph is found. */
23912
23913 static int
23914 right_overwriting (struct glyph_string *s)
23915 {
23916 int i, k, x;
23917 int end = s->row->used[s->area];
23918 struct glyph *glyphs = s->row->glyphs[s->area];
23919 int first = (s->first_glyph - glyphs
23920 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23921
23922 k = -1;
23923 x = 0;
23924 for (i = first; i < end; ++i)
23925 {
23926 int left, right;
23927 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23928 if (x - left < 0)
23929 k = i;
23930 x += glyphs[i].pixel_width;
23931 }
23932
23933 return k;
23934 }
23935
23936
23937 /* Set background width of glyph string S. START is the index of the
23938 first glyph following S. LAST_X is the right-most x-position + 1
23939 in the drawing area. */
23940
23941 static void
23942 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23943 {
23944 /* If the face of this glyph string has to be drawn to the end of
23945 the drawing area, set S->extends_to_end_of_line_p. */
23946
23947 if (start == s->row->used[s->area]
23948 && ((s->row->fill_line_p
23949 && (s->hl == DRAW_NORMAL_TEXT
23950 || s->hl == DRAW_IMAGE_RAISED
23951 || s->hl == DRAW_IMAGE_SUNKEN))
23952 || s->hl == DRAW_MOUSE_FACE))
23953 s->extends_to_end_of_line_p = 1;
23954
23955 /* If S extends its face to the end of the line, set its
23956 background_width to the distance to the right edge of the drawing
23957 area. */
23958 if (s->extends_to_end_of_line_p)
23959 s->background_width = last_x - s->x + 1;
23960 else
23961 s->background_width = s->width;
23962 }
23963
23964
23965 /* Compute overhangs and x-positions for glyph string S and its
23966 predecessors, or successors. X is the starting x-position for S.
23967 BACKWARD_P non-zero means process predecessors. */
23968
23969 static void
23970 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23971 {
23972 if (backward_p)
23973 {
23974 while (s)
23975 {
23976 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23977 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23978 x -= s->width;
23979 s->x = x;
23980 s = s->prev;
23981 }
23982 }
23983 else
23984 {
23985 while (s)
23986 {
23987 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23988 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23989 s->x = x;
23990 x += s->width;
23991 s = s->next;
23992 }
23993 }
23994 }
23995
23996
23997
23998 /* The following macros are only called from draw_glyphs below.
23999 They reference the following parameters of that function directly:
24000 `w', `row', `area', and `overlap_p'
24001 as well as the following local variables:
24002 `s', `f', and `hdc' (in W32) */
24003
24004 #ifdef HAVE_NTGUI
24005 /* On W32, silently add local `hdc' variable to argument list of
24006 init_glyph_string. */
24007 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24008 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24009 #else
24010 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24011 init_glyph_string (s, char2b, w, row, area, start, hl)
24012 #endif
24013
24014 /* Add a glyph string for a stretch glyph to the list of strings
24015 between HEAD and TAIL. START is the index of the stretch glyph in
24016 row area AREA of glyph row ROW. END is the index of the last glyph
24017 in that glyph row area. X is the current output position assigned
24018 to the new glyph string constructed. HL overrides that face of the
24019 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24020 is the right-most x-position of the drawing area. */
24021
24022 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24023 and below -- keep them on one line. */
24024 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24025 do \
24026 { \
24027 s = alloca (sizeof *s); \
24028 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24029 START = fill_stretch_glyph_string (s, START, END); \
24030 append_glyph_string (&HEAD, &TAIL, s); \
24031 s->x = (X); \
24032 } \
24033 while (0)
24034
24035
24036 /* Add a glyph string for an image glyph to the list of strings
24037 between HEAD and TAIL. START is the index of the image glyph in
24038 row area AREA of glyph row ROW. END is the index of the last glyph
24039 in that glyph row area. X is the current output position assigned
24040 to the new glyph string constructed. HL overrides that face of the
24041 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24042 is the right-most x-position of the drawing area. */
24043
24044 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24045 do \
24046 { \
24047 s = alloca (sizeof *s); \
24048 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24049 fill_image_glyph_string (s); \
24050 append_glyph_string (&HEAD, &TAIL, s); \
24051 ++START; \
24052 s->x = (X); \
24053 } \
24054 while (0)
24055
24056
24057 /* Add a glyph string for a sequence of character glyphs to the list
24058 of strings between HEAD and TAIL. START is the index of the first
24059 glyph in row area AREA of glyph row ROW that is part of the new
24060 glyph string. END is the index of the last glyph in that glyph row
24061 area. X is the current output position assigned to the new glyph
24062 string constructed. HL overrides that face of the glyph; e.g. it
24063 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24064 right-most x-position of the drawing area. */
24065
24066 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24067 do \
24068 { \
24069 int face_id; \
24070 XChar2b *char2b; \
24071 \
24072 face_id = (row)->glyphs[area][START].face_id; \
24073 \
24074 s = alloca (sizeof *s); \
24075 char2b = alloca ((END - START) * sizeof *char2b); \
24076 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24077 append_glyph_string (&HEAD, &TAIL, s); \
24078 s->x = (X); \
24079 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24080 } \
24081 while (0)
24082
24083
24084 /* Add a glyph string for a composite sequence to the list of strings
24085 between HEAD and TAIL. START is the index of the first glyph in
24086 row area AREA of glyph row ROW that is part of the new glyph
24087 string. END is the index of the last glyph in that glyph row area.
24088 X is the current output position assigned to the new glyph string
24089 constructed. HL overrides that face of the glyph; e.g. it is
24090 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24091 x-position of the drawing area. */
24092
24093 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24094 do { \
24095 int face_id = (row)->glyphs[area][START].face_id; \
24096 struct face *base_face = FACE_FROM_ID (f, face_id); \
24097 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24098 struct composition *cmp = composition_table[cmp_id]; \
24099 XChar2b *char2b; \
24100 struct glyph_string *first_s = NULL; \
24101 int n; \
24102 \
24103 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24104 \
24105 /* Make glyph_strings for each glyph sequence that is drawable by \
24106 the same face, and append them to HEAD/TAIL. */ \
24107 for (n = 0; n < cmp->glyph_len;) \
24108 { \
24109 s = alloca (sizeof *s); \
24110 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24111 append_glyph_string (&(HEAD), &(TAIL), s); \
24112 s->cmp = cmp; \
24113 s->cmp_from = n; \
24114 s->x = (X); \
24115 if (n == 0) \
24116 first_s = s; \
24117 n = fill_composite_glyph_string (s, base_face, overlaps); \
24118 } \
24119 \
24120 ++START; \
24121 s = first_s; \
24122 } while (0)
24123
24124
24125 /* Add a glyph string for a glyph-string sequence to the list of strings
24126 between HEAD and TAIL. */
24127
24128 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24129 do { \
24130 int face_id; \
24131 XChar2b *char2b; \
24132 Lisp_Object gstring; \
24133 \
24134 face_id = (row)->glyphs[area][START].face_id; \
24135 gstring = (composition_gstring_from_id \
24136 ((row)->glyphs[area][START].u.cmp.id)); \
24137 s = alloca (sizeof *s); \
24138 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24139 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24140 append_glyph_string (&(HEAD), &(TAIL), s); \
24141 s->x = (X); \
24142 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24143 } while (0)
24144
24145
24146 /* Add a glyph string for a sequence of glyphless character's glyphs
24147 to the list of strings between HEAD and TAIL. The meanings of
24148 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24149
24150 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24151 do \
24152 { \
24153 int face_id; \
24154 \
24155 face_id = (row)->glyphs[area][START].face_id; \
24156 \
24157 s = alloca (sizeof *s); \
24158 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24159 append_glyph_string (&HEAD, &TAIL, s); \
24160 s->x = (X); \
24161 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24162 overlaps); \
24163 } \
24164 while (0)
24165
24166
24167 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24168 of AREA of glyph row ROW on window W between indices START and END.
24169 HL overrides the face for drawing glyph strings, e.g. it is
24170 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24171 x-positions of the drawing area.
24172
24173 This is an ugly monster macro construct because we must use alloca
24174 to allocate glyph strings (because draw_glyphs can be called
24175 asynchronously). */
24176
24177 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24178 do \
24179 { \
24180 HEAD = TAIL = NULL; \
24181 while (START < END) \
24182 { \
24183 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24184 switch (first_glyph->type) \
24185 { \
24186 case CHAR_GLYPH: \
24187 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24188 HL, X, LAST_X); \
24189 break; \
24190 \
24191 case COMPOSITE_GLYPH: \
24192 if (first_glyph->u.cmp.automatic) \
24193 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24194 HL, X, LAST_X); \
24195 else \
24196 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24197 HL, X, LAST_X); \
24198 break; \
24199 \
24200 case STRETCH_GLYPH: \
24201 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24202 HL, X, LAST_X); \
24203 break; \
24204 \
24205 case IMAGE_GLYPH: \
24206 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24207 HL, X, LAST_X); \
24208 break; \
24209 \
24210 case GLYPHLESS_GLYPH: \
24211 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24212 HL, X, LAST_X); \
24213 break; \
24214 \
24215 default: \
24216 emacs_abort (); \
24217 } \
24218 \
24219 if (s) \
24220 { \
24221 set_glyph_string_background_width (s, START, LAST_X); \
24222 (X) += s->width; \
24223 } \
24224 } \
24225 } while (0)
24226
24227
24228 /* Draw glyphs between START and END in AREA of ROW on window W,
24229 starting at x-position X. X is relative to AREA in W. HL is a
24230 face-override with the following meaning:
24231
24232 DRAW_NORMAL_TEXT draw normally
24233 DRAW_CURSOR draw in cursor face
24234 DRAW_MOUSE_FACE draw in mouse face.
24235 DRAW_INVERSE_VIDEO draw in mode line face
24236 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24237 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24238
24239 If OVERLAPS is non-zero, draw only the foreground of characters and
24240 clip to the physical height of ROW. Non-zero value also defines
24241 the overlapping part to be drawn:
24242
24243 OVERLAPS_PRED overlap with preceding rows
24244 OVERLAPS_SUCC overlap with succeeding rows
24245 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24246 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24247
24248 Value is the x-position reached, relative to AREA of W. */
24249
24250 static int
24251 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24252 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24253 enum draw_glyphs_face hl, int overlaps)
24254 {
24255 struct glyph_string *head, *tail;
24256 struct glyph_string *s;
24257 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24258 int i, j, x_reached, last_x, area_left = 0;
24259 struct frame *f = XFRAME (WINDOW_FRAME (w));
24260 DECLARE_HDC (hdc);
24261
24262 ALLOCATE_HDC (hdc, f);
24263
24264 /* Let's rather be paranoid than getting a SEGV. */
24265 end = min (end, row->used[area]);
24266 start = clip_to_bounds (0, start, end);
24267
24268 /* Translate X to frame coordinates. Set last_x to the right
24269 end of the drawing area. */
24270 if (row->full_width_p)
24271 {
24272 /* X is relative to the left edge of W, without scroll bars
24273 or fringes. */
24274 area_left = WINDOW_LEFT_EDGE_X (w);
24275 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24276 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24277 }
24278 else
24279 {
24280 area_left = window_box_left (w, area);
24281 last_x = area_left + window_box_width (w, area);
24282 }
24283 x += area_left;
24284
24285 /* Build a doubly-linked list of glyph_string structures between
24286 head and tail from what we have to draw. Note that the macro
24287 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24288 the reason we use a separate variable `i'. */
24289 i = start;
24290 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24291 if (tail)
24292 x_reached = tail->x + tail->background_width;
24293 else
24294 x_reached = x;
24295
24296 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24297 the row, redraw some glyphs in front or following the glyph
24298 strings built above. */
24299 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24300 {
24301 struct glyph_string *h, *t;
24302 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24303 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24304 int check_mouse_face = 0;
24305 int dummy_x = 0;
24306
24307 /* If mouse highlighting is on, we may need to draw adjacent
24308 glyphs using mouse-face highlighting. */
24309 if (area == TEXT_AREA && row->mouse_face_p
24310 && hlinfo->mouse_face_beg_row >= 0
24311 && hlinfo->mouse_face_end_row >= 0)
24312 {
24313 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24314
24315 if (row_vpos >= hlinfo->mouse_face_beg_row
24316 && row_vpos <= hlinfo->mouse_face_end_row)
24317 {
24318 check_mouse_face = 1;
24319 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24320 ? hlinfo->mouse_face_beg_col : 0;
24321 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24322 ? hlinfo->mouse_face_end_col
24323 : row->used[TEXT_AREA];
24324 }
24325 }
24326
24327 /* Compute overhangs for all glyph strings. */
24328 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24329 for (s = head; s; s = s->next)
24330 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24331
24332 /* Prepend glyph strings for glyphs in front of the first glyph
24333 string that are overwritten because of the first glyph
24334 string's left overhang. The background of all strings
24335 prepended must be drawn because the first glyph string
24336 draws over it. */
24337 i = left_overwritten (head);
24338 if (i >= 0)
24339 {
24340 enum draw_glyphs_face overlap_hl;
24341
24342 /* If this row contains mouse highlighting, attempt to draw
24343 the overlapped glyphs with the correct highlight. This
24344 code fails if the overlap encompasses more than one glyph
24345 and mouse-highlight spans only some of these glyphs.
24346 However, making it work perfectly involves a lot more
24347 code, and I don't know if the pathological case occurs in
24348 practice, so we'll stick to this for now. --- cyd */
24349 if (check_mouse_face
24350 && mouse_beg_col < start && mouse_end_col > i)
24351 overlap_hl = DRAW_MOUSE_FACE;
24352 else
24353 overlap_hl = DRAW_NORMAL_TEXT;
24354
24355 j = i;
24356 BUILD_GLYPH_STRINGS (j, start, h, t,
24357 overlap_hl, dummy_x, last_x);
24358 start = i;
24359 compute_overhangs_and_x (t, head->x, 1);
24360 prepend_glyph_string_lists (&head, &tail, h, t);
24361 clip_head = head;
24362 }
24363
24364 /* Prepend glyph strings for glyphs in front of the first glyph
24365 string that overwrite that glyph string because of their
24366 right overhang. For these strings, only the foreground must
24367 be drawn, because it draws over the glyph string at `head'.
24368 The background must not be drawn because this would overwrite
24369 right overhangs of preceding glyphs for which no glyph
24370 strings exist. */
24371 i = left_overwriting (head);
24372 if (i >= 0)
24373 {
24374 enum draw_glyphs_face overlap_hl;
24375
24376 if (check_mouse_face
24377 && mouse_beg_col < start && mouse_end_col > i)
24378 overlap_hl = DRAW_MOUSE_FACE;
24379 else
24380 overlap_hl = DRAW_NORMAL_TEXT;
24381
24382 clip_head = head;
24383 BUILD_GLYPH_STRINGS (i, start, h, t,
24384 overlap_hl, dummy_x, last_x);
24385 for (s = h; s; s = s->next)
24386 s->background_filled_p = 1;
24387 compute_overhangs_and_x (t, head->x, 1);
24388 prepend_glyph_string_lists (&head, &tail, h, t);
24389 }
24390
24391 /* Append glyphs strings for glyphs following the last glyph
24392 string tail that are overwritten by tail. The background of
24393 these strings has to be drawn because tail's foreground draws
24394 over it. */
24395 i = right_overwritten (tail);
24396 if (i >= 0)
24397 {
24398 enum draw_glyphs_face overlap_hl;
24399
24400 if (check_mouse_face
24401 && mouse_beg_col < i && mouse_end_col > end)
24402 overlap_hl = DRAW_MOUSE_FACE;
24403 else
24404 overlap_hl = DRAW_NORMAL_TEXT;
24405
24406 BUILD_GLYPH_STRINGS (end, i, h, t,
24407 overlap_hl, x, last_x);
24408 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24409 we don't have `end = i;' here. */
24410 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24411 append_glyph_string_lists (&head, &tail, h, t);
24412 clip_tail = tail;
24413 }
24414
24415 /* Append glyph strings for glyphs following the last glyph
24416 string tail that overwrite tail. The foreground of such
24417 glyphs has to be drawn because it writes into the background
24418 of tail. The background must not be drawn because it could
24419 paint over the foreground of following glyphs. */
24420 i = right_overwriting (tail);
24421 if (i >= 0)
24422 {
24423 enum draw_glyphs_face overlap_hl;
24424 if (check_mouse_face
24425 && mouse_beg_col < i && mouse_end_col > end)
24426 overlap_hl = DRAW_MOUSE_FACE;
24427 else
24428 overlap_hl = DRAW_NORMAL_TEXT;
24429
24430 clip_tail = tail;
24431 i++; /* We must include the Ith glyph. */
24432 BUILD_GLYPH_STRINGS (end, i, h, t,
24433 overlap_hl, x, last_x);
24434 for (s = h; s; s = s->next)
24435 s->background_filled_p = 1;
24436 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24437 append_glyph_string_lists (&head, &tail, h, t);
24438 }
24439 if (clip_head || clip_tail)
24440 for (s = head; s; s = s->next)
24441 {
24442 s->clip_head = clip_head;
24443 s->clip_tail = clip_tail;
24444 }
24445 }
24446
24447 /* Draw all strings. */
24448 for (s = head; s; s = s->next)
24449 FRAME_RIF (f)->draw_glyph_string (s);
24450
24451 #ifndef HAVE_NS
24452 /* When focus a sole frame and move horizontally, this sets on_p to 0
24453 causing a failure to erase prev cursor position. */
24454 if (area == TEXT_AREA
24455 && !row->full_width_p
24456 /* When drawing overlapping rows, only the glyph strings'
24457 foreground is drawn, which doesn't erase a cursor
24458 completely. */
24459 && !overlaps)
24460 {
24461 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24462 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24463 : (tail ? tail->x + tail->background_width : x));
24464 x0 -= area_left;
24465 x1 -= area_left;
24466
24467 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24468 row->y, MATRIX_ROW_BOTTOM_Y (row));
24469 }
24470 #endif
24471
24472 /* Value is the x-position up to which drawn, relative to AREA of W.
24473 This doesn't include parts drawn because of overhangs. */
24474 if (row->full_width_p)
24475 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24476 else
24477 x_reached -= area_left;
24478
24479 RELEASE_HDC (hdc, f);
24480
24481 return x_reached;
24482 }
24483
24484 /* Expand row matrix if too narrow. Don't expand if area
24485 is not present. */
24486
24487 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24488 { \
24489 if (!it->f->fonts_changed \
24490 && (it->glyph_row->glyphs[area] \
24491 < it->glyph_row->glyphs[area + 1])) \
24492 { \
24493 it->w->ncols_scale_factor++; \
24494 it->f->fonts_changed = 1; \
24495 } \
24496 }
24497
24498 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24499 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24500
24501 static void
24502 append_glyph (struct it *it)
24503 {
24504 struct glyph *glyph;
24505 enum glyph_row_area area = it->area;
24506
24507 eassert (it->glyph_row);
24508 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24509
24510 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24511 if (glyph < it->glyph_row->glyphs[area + 1])
24512 {
24513 /* If the glyph row is reversed, we need to prepend the glyph
24514 rather than append it. */
24515 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24516 {
24517 struct glyph *g;
24518
24519 /* Make room for the additional glyph. */
24520 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24521 g[1] = *g;
24522 glyph = it->glyph_row->glyphs[area];
24523 }
24524 glyph->charpos = CHARPOS (it->position);
24525 glyph->object = it->object;
24526 if (it->pixel_width > 0)
24527 {
24528 glyph->pixel_width = it->pixel_width;
24529 glyph->padding_p = 0;
24530 }
24531 else
24532 {
24533 /* Assure at least 1-pixel width. Otherwise, cursor can't
24534 be displayed correctly. */
24535 glyph->pixel_width = 1;
24536 glyph->padding_p = 1;
24537 }
24538 glyph->ascent = it->ascent;
24539 glyph->descent = it->descent;
24540 glyph->voffset = it->voffset;
24541 glyph->type = CHAR_GLYPH;
24542 glyph->avoid_cursor_p = it->avoid_cursor_p;
24543 glyph->multibyte_p = it->multibyte_p;
24544 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24545 {
24546 /* In R2L rows, the left and the right box edges need to be
24547 drawn in reverse direction. */
24548 glyph->right_box_line_p = it->start_of_box_run_p;
24549 glyph->left_box_line_p = it->end_of_box_run_p;
24550 }
24551 else
24552 {
24553 glyph->left_box_line_p = it->start_of_box_run_p;
24554 glyph->right_box_line_p = it->end_of_box_run_p;
24555 }
24556 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24557 || it->phys_descent > it->descent);
24558 glyph->glyph_not_available_p = it->glyph_not_available_p;
24559 glyph->face_id = it->face_id;
24560 glyph->u.ch = it->char_to_display;
24561 glyph->slice.img = null_glyph_slice;
24562 glyph->font_type = FONT_TYPE_UNKNOWN;
24563 if (it->bidi_p)
24564 {
24565 glyph->resolved_level = it->bidi_it.resolved_level;
24566 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24567 emacs_abort ();
24568 glyph->bidi_type = it->bidi_it.type;
24569 }
24570 else
24571 {
24572 glyph->resolved_level = 0;
24573 glyph->bidi_type = UNKNOWN_BT;
24574 }
24575 ++it->glyph_row->used[area];
24576 }
24577 else
24578 IT_EXPAND_MATRIX_WIDTH (it, area);
24579 }
24580
24581 /* Store one glyph for the composition IT->cmp_it.id in
24582 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24583 non-null. */
24584
24585 static void
24586 append_composite_glyph (struct it *it)
24587 {
24588 struct glyph *glyph;
24589 enum glyph_row_area area = it->area;
24590
24591 eassert (it->glyph_row);
24592
24593 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24594 if (glyph < it->glyph_row->glyphs[area + 1])
24595 {
24596 /* If the glyph row is reversed, we need to prepend the glyph
24597 rather than append it. */
24598 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24599 {
24600 struct glyph *g;
24601
24602 /* Make room for the new glyph. */
24603 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24604 g[1] = *g;
24605 glyph = it->glyph_row->glyphs[it->area];
24606 }
24607 glyph->charpos = it->cmp_it.charpos;
24608 glyph->object = it->object;
24609 glyph->pixel_width = it->pixel_width;
24610 glyph->ascent = it->ascent;
24611 glyph->descent = it->descent;
24612 glyph->voffset = it->voffset;
24613 glyph->type = COMPOSITE_GLYPH;
24614 if (it->cmp_it.ch < 0)
24615 {
24616 glyph->u.cmp.automatic = 0;
24617 glyph->u.cmp.id = it->cmp_it.id;
24618 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24619 }
24620 else
24621 {
24622 glyph->u.cmp.automatic = 1;
24623 glyph->u.cmp.id = it->cmp_it.id;
24624 glyph->slice.cmp.from = it->cmp_it.from;
24625 glyph->slice.cmp.to = it->cmp_it.to - 1;
24626 }
24627 glyph->avoid_cursor_p = it->avoid_cursor_p;
24628 glyph->multibyte_p = it->multibyte_p;
24629 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24630 {
24631 /* In R2L rows, the left and the right box edges need to be
24632 drawn in reverse direction. */
24633 glyph->right_box_line_p = it->start_of_box_run_p;
24634 glyph->left_box_line_p = it->end_of_box_run_p;
24635 }
24636 else
24637 {
24638 glyph->left_box_line_p = it->start_of_box_run_p;
24639 glyph->right_box_line_p = it->end_of_box_run_p;
24640 }
24641 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24642 || it->phys_descent > it->descent);
24643 glyph->padding_p = 0;
24644 glyph->glyph_not_available_p = 0;
24645 glyph->face_id = it->face_id;
24646 glyph->font_type = FONT_TYPE_UNKNOWN;
24647 if (it->bidi_p)
24648 {
24649 glyph->resolved_level = it->bidi_it.resolved_level;
24650 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24651 emacs_abort ();
24652 glyph->bidi_type = it->bidi_it.type;
24653 }
24654 ++it->glyph_row->used[area];
24655 }
24656 else
24657 IT_EXPAND_MATRIX_WIDTH (it, area);
24658 }
24659
24660
24661 /* Change IT->ascent and IT->height according to the setting of
24662 IT->voffset. */
24663
24664 static void
24665 take_vertical_position_into_account (struct it *it)
24666 {
24667 if (it->voffset)
24668 {
24669 if (it->voffset < 0)
24670 /* Increase the ascent so that we can display the text higher
24671 in the line. */
24672 it->ascent -= it->voffset;
24673 else
24674 /* Increase the descent so that we can display the text lower
24675 in the line. */
24676 it->descent += it->voffset;
24677 }
24678 }
24679
24680
24681 /* Produce glyphs/get display metrics for the image IT is loaded with.
24682 See the description of struct display_iterator in dispextern.h for
24683 an overview of struct display_iterator. */
24684
24685 static void
24686 produce_image_glyph (struct it *it)
24687 {
24688 struct image *img;
24689 struct face *face;
24690 int glyph_ascent, crop;
24691 struct glyph_slice slice;
24692
24693 eassert (it->what == IT_IMAGE);
24694
24695 face = FACE_FROM_ID (it->f, it->face_id);
24696 eassert (face);
24697 /* Make sure X resources of the face is loaded. */
24698 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24699
24700 if (it->image_id < 0)
24701 {
24702 /* Fringe bitmap. */
24703 it->ascent = it->phys_ascent = 0;
24704 it->descent = it->phys_descent = 0;
24705 it->pixel_width = 0;
24706 it->nglyphs = 0;
24707 return;
24708 }
24709
24710 img = IMAGE_FROM_ID (it->f, it->image_id);
24711 eassert (img);
24712 /* Make sure X resources of the image is loaded. */
24713 prepare_image_for_display (it->f, img);
24714
24715 slice.x = slice.y = 0;
24716 slice.width = img->width;
24717 slice.height = img->height;
24718
24719 if (INTEGERP (it->slice.x))
24720 slice.x = XINT (it->slice.x);
24721 else if (FLOATP (it->slice.x))
24722 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24723
24724 if (INTEGERP (it->slice.y))
24725 slice.y = XINT (it->slice.y);
24726 else if (FLOATP (it->slice.y))
24727 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24728
24729 if (INTEGERP (it->slice.width))
24730 slice.width = XINT (it->slice.width);
24731 else if (FLOATP (it->slice.width))
24732 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24733
24734 if (INTEGERP (it->slice.height))
24735 slice.height = XINT (it->slice.height);
24736 else if (FLOATP (it->slice.height))
24737 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24738
24739 if (slice.x >= img->width)
24740 slice.x = img->width;
24741 if (slice.y >= img->height)
24742 slice.y = img->height;
24743 if (slice.x + slice.width >= img->width)
24744 slice.width = img->width - slice.x;
24745 if (slice.y + slice.height > img->height)
24746 slice.height = img->height - slice.y;
24747
24748 if (slice.width == 0 || slice.height == 0)
24749 return;
24750
24751 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24752
24753 it->descent = slice.height - glyph_ascent;
24754 if (slice.y == 0)
24755 it->descent += img->vmargin;
24756 if (slice.y + slice.height == img->height)
24757 it->descent += img->vmargin;
24758 it->phys_descent = it->descent;
24759
24760 it->pixel_width = slice.width;
24761 if (slice.x == 0)
24762 it->pixel_width += img->hmargin;
24763 if (slice.x + slice.width == img->width)
24764 it->pixel_width += img->hmargin;
24765
24766 /* It's quite possible for images to have an ascent greater than
24767 their height, so don't get confused in that case. */
24768 if (it->descent < 0)
24769 it->descent = 0;
24770
24771 it->nglyphs = 1;
24772
24773 if (face->box != FACE_NO_BOX)
24774 {
24775 if (face->box_line_width > 0)
24776 {
24777 if (slice.y == 0)
24778 it->ascent += face->box_line_width;
24779 if (slice.y + slice.height == img->height)
24780 it->descent += face->box_line_width;
24781 }
24782
24783 if (it->start_of_box_run_p && slice.x == 0)
24784 it->pixel_width += eabs (face->box_line_width);
24785 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24786 it->pixel_width += eabs (face->box_line_width);
24787 }
24788
24789 take_vertical_position_into_account (it);
24790
24791 /* Automatically crop wide image glyphs at right edge so we can
24792 draw the cursor on same display row. */
24793 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24794 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24795 {
24796 it->pixel_width -= crop;
24797 slice.width -= crop;
24798 }
24799
24800 if (it->glyph_row)
24801 {
24802 struct glyph *glyph;
24803 enum glyph_row_area area = it->area;
24804
24805 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24806 if (glyph < it->glyph_row->glyphs[area + 1])
24807 {
24808 glyph->charpos = CHARPOS (it->position);
24809 glyph->object = it->object;
24810 glyph->pixel_width = it->pixel_width;
24811 glyph->ascent = glyph_ascent;
24812 glyph->descent = it->descent;
24813 glyph->voffset = it->voffset;
24814 glyph->type = IMAGE_GLYPH;
24815 glyph->avoid_cursor_p = it->avoid_cursor_p;
24816 glyph->multibyte_p = it->multibyte_p;
24817 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24818 {
24819 /* In R2L rows, the left and the right box edges need to be
24820 drawn in reverse direction. */
24821 glyph->right_box_line_p = it->start_of_box_run_p;
24822 glyph->left_box_line_p = it->end_of_box_run_p;
24823 }
24824 else
24825 {
24826 glyph->left_box_line_p = it->start_of_box_run_p;
24827 glyph->right_box_line_p = it->end_of_box_run_p;
24828 }
24829 glyph->overlaps_vertically_p = 0;
24830 glyph->padding_p = 0;
24831 glyph->glyph_not_available_p = 0;
24832 glyph->face_id = it->face_id;
24833 glyph->u.img_id = img->id;
24834 glyph->slice.img = slice;
24835 glyph->font_type = FONT_TYPE_UNKNOWN;
24836 if (it->bidi_p)
24837 {
24838 glyph->resolved_level = it->bidi_it.resolved_level;
24839 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24840 emacs_abort ();
24841 glyph->bidi_type = it->bidi_it.type;
24842 }
24843 ++it->glyph_row->used[area];
24844 }
24845 else
24846 IT_EXPAND_MATRIX_WIDTH (it, area);
24847 }
24848 }
24849
24850
24851 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24852 of the glyph, WIDTH and HEIGHT are the width and height of the
24853 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24854
24855 static void
24856 append_stretch_glyph (struct it *it, Lisp_Object object,
24857 int width, int height, int ascent)
24858 {
24859 struct glyph *glyph;
24860 enum glyph_row_area area = it->area;
24861
24862 eassert (ascent >= 0 && ascent <= height);
24863
24864 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24865 if (glyph < it->glyph_row->glyphs[area + 1])
24866 {
24867 /* If the glyph row is reversed, we need to prepend the glyph
24868 rather than append it. */
24869 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24870 {
24871 struct glyph *g;
24872
24873 /* Make room for the additional glyph. */
24874 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24875 g[1] = *g;
24876 glyph = it->glyph_row->glyphs[area];
24877 }
24878 glyph->charpos = CHARPOS (it->position);
24879 glyph->object = object;
24880 glyph->pixel_width = width;
24881 glyph->ascent = ascent;
24882 glyph->descent = height - ascent;
24883 glyph->voffset = it->voffset;
24884 glyph->type = STRETCH_GLYPH;
24885 glyph->avoid_cursor_p = it->avoid_cursor_p;
24886 glyph->multibyte_p = it->multibyte_p;
24887 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24888 {
24889 /* In R2L rows, the left and the right box edges need to be
24890 drawn in reverse direction. */
24891 glyph->right_box_line_p = it->start_of_box_run_p;
24892 glyph->left_box_line_p = it->end_of_box_run_p;
24893 }
24894 else
24895 {
24896 glyph->left_box_line_p = it->start_of_box_run_p;
24897 glyph->right_box_line_p = it->end_of_box_run_p;
24898 }
24899 glyph->overlaps_vertically_p = 0;
24900 glyph->padding_p = 0;
24901 glyph->glyph_not_available_p = 0;
24902 glyph->face_id = it->face_id;
24903 glyph->u.stretch.ascent = ascent;
24904 glyph->u.stretch.height = height;
24905 glyph->slice.img = null_glyph_slice;
24906 glyph->font_type = FONT_TYPE_UNKNOWN;
24907 if (it->bidi_p)
24908 {
24909 glyph->resolved_level = it->bidi_it.resolved_level;
24910 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24911 emacs_abort ();
24912 glyph->bidi_type = it->bidi_it.type;
24913 }
24914 else
24915 {
24916 glyph->resolved_level = 0;
24917 glyph->bidi_type = UNKNOWN_BT;
24918 }
24919 ++it->glyph_row->used[area];
24920 }
24921 else
24922 IT_EXPAND_MATRIX_WIDTH (it, area);
24923 }
24924
24925 #endif /* HAVE_WINDOW_SYSTEM */
24926
24927 /* Produce a stretch glyph for iterator IT. IT->object is the value
24928 of the glyph property displayed. The value must be a list
24929 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24930 being recognized:
24931
24932 1. `:width WIDTH' specifies that the space should be WIDTH *
24933 canonical char width wide. WIDTH may be an integer or floating
24934 point number.
24935
24936 2. `:relative-width FACTOR' specifies that the width of the stretch
24937 should be computed from the width of the first character having the
24938 `glyph' property, and should be FACTOR times that width.
24939
24940 3. `:align-to HPOS' specifies that the space should be wide enough
24941 to reach HPOS, a value in canonical character units.
24942
24943 Exactly one of the above pairs must be present.
24944
24945 4. `:height HEIGHT' specifies that the height of the stretch produced
24946 should be HEIGHT, measured in canonical character units.
24947
24948 5. `:relative-height FACTOR' specifies that the height of the
24949 stretch should be FACTOR times the height of the characters having
24950 the glyph property.
24951
24952 Either none or exactly one of 4 or 5 must be present.
24953
24954 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24955 of the stretch should be used for the ascent of the stretch.
24956 ASCENT must be in the range 0 <= ASCENT <= 100. */
24957
24958 void
24959 produce_stretch_glyph (struct it *it)
24960 {
24961 /* (space :width WIDTH :height HEIGHT ...) */
24962 Lisp_Object prop, plist;
24963 int width = 0, height = 0, align_to = -1;
24964 int zero_width_ok_p = 0;
24965 double tem;
24966 struct font *font = NULL;
24967
24968 #ifdef HAVE_WINDOW_SYSTEM
24969 int ascent = 0;
24970 int zero_height_ok_p = 0;
24971
24972 if (FRAME_WINDOW_P (it->f))
24973 {
24974 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24975 font = face->font ? face->font : FRAME_FONT (it->f);
24976 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24977 }
24978 #endif
24979
24980 /* List should start with `space'. */
24981 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24982 plist = XCDR (it->object);
24983
24984 /* Compute the width of the stretch. */
24985 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24986 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24987 {
24988 /* Absolute width `:width WIDTH' specified and valid. */
24989 zero_width_ok_p = 1;
24990 width = (int)tem;
24991 }
24992 #ifdef HAVE_WINDOW_SYSTEM
24993 else if (FRAME_WINDOW_P (it->f)
24994 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24995 {
24996 /* Relative width `:relative-width FACTOR' specified and valid.
24997 Compute the width of the characters having the `glyph'
24998 property. */
24999 struct it it2;
25000 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25001
25002 it2 = *it;
25003 if (it->multibyte_p)
25004 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25005 else
25006 {
25007 it2.c = it2.char_to_display = *p, it2.len = 1;
25008 if (! ASCII_CHAR_P (it2.c))
25009 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25010 }
25011
25012 it2.glyph_row = NULL;
25013 it2.what = IT_CHARACTER;
25014 x_produce_glyphs (&it2);
25015 width = NUMVAL (prop) * it2.pixel_width;
25016 }
25017 #endif /* HAVE_WINDOW_SYSTEM */
25018 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25019 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25020 {
25021 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25022 align_to = (align_to < 0
25023 ? 0
25024 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25025 else if (align_to < 0)
25026 align_to = window_box_left_offset (it->w, TEXT_AREA);
25027 width = max (0, (int)tem + align_to - it->current_x);
25028 zero_width_ok_p = 1;
25029 }
25030 else
25031 /* Nothing specified -> width defaults to canonical char width. */
25032 width = FRAME_COLUMN_WIDTH (it->f);
25033
25034 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25035 width = 1;
25036
25037 #ifdef HAVE_WINDOW_SYSTEM
25038 /* Compute height. */
25039 if (FRAME_WINDOW_P (it->f))
25040 {
25041 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25042 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25043 {
25044 height = (int)tem;
25045 zero_height_ok_p = 1;
25046 }
25047 else if (prop = Fplist_get (plist, QCrelative_height),
25048 NUMVAL (prop) > 0)
25049 height = FONT_HEIGHT (font) * NUMVAL (prop);
25050 else
25051 height = FONT_HEIGHT (font);
25052
25053 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25054 height = 1;
25055
25056 /* Compute percentage of height used for ascent. If
25057 `:ascent ASCENT' is present and valid, use that. Otherwise,
25058 derive the ascent from the font in use. */
25059 if (prop = Fplist_get (plist, QCascent),
25060 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25061 ascent = height * NUMVAL (prop) / 100.0;
25062 else if (!NILP (prop)
25063 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25064 ascent = min (max (0, (int)tem), height);
25065 else
25066 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25067 }
25068 else
25069 #endif /* HAVE_WINDOW_SYSTEM */
25070 height = 1;
25071
25072 if (width > 0 && it->line_wrap != TRUNCATE
25073 && it->current_x + width > it->last_visible_x)
25074 {
25075 width = it->last_visible_x - it->current_x;
25076 #ifdef HAVE_WINDOW_SYSTEM
25077 /* Subtract one more pixel from the stretch width, but only on
25078 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25079 width -= FRAME_WINDOW_P (it->f);
25080 #endif
25081 }
25082
25083 if (width > 0 && height > 0 && it->glyph_row)
25084 {
25085 Lisp_Object o_object = it->object;
25086 Lisp_Object object = it->stack[it->sp - 1].string;
25087 int n = width;
25088
25089 if (!STRINGP (object))
25090 object = it->w->contents;
25091 #ifdef HAVE_WINDOW_SYSTEM
25092 if (FRAME_WINDOW_P (it->f))
25093 append_stretch_glyph (it, object, width, height, ascent);
25094 else
25095 #endif
25096 {
25097 it->object = object;
25098 it->char_to_display = ' ';
25099 it->pixel_width = it->len = 1;
25100 while (n--)
25101 tty_append_glyph (it);
25102 it->object = o_object;
25103 }
25104 }
25105
25106 it->pixel_width = width;
25107 #ifdef HAVE_WINDOW_SYSTEM
25108 if (FRAME_WINDOW_P (it->f))
25109 {
25110 it->ascent = it->phys_ascent = ascent;
25111 it->descent = it->phys_descent = height - it->ascent;
25112 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25113 take_vertical_position_into_account (it);
25114 }
25115 else
25116 #endif
25117 it->nglyphs = width;
25118 }
25119
25120 /* Get information about special display element WHAT in an
25121 environment described by IT. WHAT is one of IT_TRUNCATION or
25122 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25123 non-null glyph_row member. This function ensures that fields like
25124 face_id, c, len of IT are left untouched. */
25125
25126 static void
25127 produce_special_glyphs (struct it *it, enum display_element_type what)
25128 {
25129 struct it temp_it;
25130 Lisp_Object gc;
25131 GLYPH glyph;
25132
25133 temp_it = *it;
25134 temp_it.object = make_number (0);
25135 memset (&temp_it.current, 0, sizeof temp_it.current);
25136
25137 if (what == IT_CONTINUATION)
25138 {
25139 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25140 if (it->bidi_it.paragraph_dir == R2L)
25141 SET_GLYPH_FROM_CHAR (glyph, '/');
25142 else
25143 SET_GLYPH_FROM_CHAR (glyph, '\\');
25144 if (it->dp
25145 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25146 {
25147 /* FIXME: Should we mirror GC for R2L lines? */
25148 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25149 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25150 }
25151 }
25152 else if (what == IT_TRUNCATION)
25153 {
25154 /* Truncation glyph. */
25155 SET_GLYPH_FROM_CHAR (glyph, '$');
25156 if (it->dp
25157 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25158 {
25159 /* FIXME: Should we mirror GC for R2L lines? */
25160 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25161 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25162 }
25163 }
25164 else
25165 emacs_abort ();
25166
25167 #ifdef HAVE_WINDOW_SYSTEM
25168 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25169 is turned off, we precede the truncation/continuation glyphs by a
25170 stretch glyph whose width is computed such that these special
25171 glyphs are aligned at the window margin, even when very different
25172 fonts are used in different glyph rows. */
25173 if (FRAME_WINDOW_P (temp_it.f)
25174 /* init_iterator calls this with it->glyph_row == NULL, and it
25175 wants only the pixel width of the truncation/continuation
25176 glyphs. */
25177 && temp_it.glyph_row
25178 /* insert_left_trunc_glyphs calls us at the beginning of the
25179 row, and it has its own calculation of the stretch glyph
25180 width. */
25181 && temp_it.glyph_row->used[TEXT_AREA] > 0
25182 && (temp_it.glyph_row->reversed_p
25183 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25184 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25185 {
25186 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25187
25188 if (stretch_width > 0)
25189 {
25190 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25191 struct font *font =
25192 face->font ? face->font : FRAME_FONT (temp_it.f);
25193 int stretch_ascent =
25194 (((temp_it.ascent + temp_it.descent)
25195 * FONT_BASE (font)) / FONT_HEIGHT (font));
25196
25197 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25198 temp_it.ascent + temp_it.descent,
25199 stretch_ascent);
25200 }
25201 }
25202 #endif
25203
25204 temp_it.dp = NULL;
25205 temp_it.what = IT_CHARACTER;
25206 temp_it.len = 1;
25207 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25208 temp_it.face_id = GLYPH_FACE (glyph);
25209 temp_it.len = CHAR_BYTES (temp_it.c);
25210
25211 PRODUCE_GLYPHS (&temp_it);
25212 it->pixel_width = temp_it.pixel_width;
25213 it->nglyphs = temp_it.pixel_width;
25214 }
25215
25216 #ifdef HAVE_WINDOW_SYSTEM
25217
25218 /* Calculate line-height and line-spacing properties.
25219 An integer value specifies explicit pixel value.
25220 A float value specifies relative value to current face height.
25221 A cons (float . face-name) specifies relative value to
25222 height of specified face font.
25223
25224 Returns height in pixels, or nil. */
25225
25226
25227 static Lisp_Object
25228 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25229 int boff, int override)
25230 {
25231 Lisp_Object face_name = Qnil;
25232 int ascent, descent, height;
25233
25234 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25235 return val;
25236
25237 if (CONSP (val))
25238 {
25239 face_name = XCAR (val);
25240 val = XCDR (val);
25241 if (!NUMBERP (val))
25242 val = make_number (1);
25243 if (NILP (face_name))
25244 {
25245 height = it->ascent + it->descent;
25246 goto scale;
25247 }
25248 }
25249
25250 if (NILP (face_name))
25251 {
25252 font = FRAME_FONT (it->f);
25253 boff = FRAME_BASELINE_OFFSET (it->f);
25254 }
25255 else if (EQ (face_name, Qt))
25256 {
25257 override = 0;
25258 }
25259 else
25260 {
25261 int face_id;
25262 struct face *face;
25263
25264 face_id = lookup_named_face (it->f, face_name, 0);
25265 if (face_id < 0)
25266 return make_number (-1);
25267
25268 face = FACE_FROM_ID (it->f, face_id);
25269 font = face->font;
25270 if (font == NULL)
25271 return make_number (-1);
25272 boff = font->baseline_offset;
25273 if (font->vertical_centering)
25274 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25275 }
25276
25277 ascent = FONT_BASE (font) + boff;
25278 descent = FONT_DESCENT (font) - boff;
25279
25280 if (override)
25281 {
25282 it->override_ascent = ascent;
25283 it->override_descent = descent;
25284 it->override_boff = boff;
25285 }
25286
25287 height = ascent + descent;
25288
25289 scale:
25290 if (FLOATP (val))
25291 height = (int)(XFLOAT_DATA (val) * height);
25292 else if (INTEGERP (val))
25293 height *= XINT (val);
25294
25295 return make_number (height);
25296 }
25297
25298
25299 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25300 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25301 and only if this is for a character for which no font was found.
25302
25303 If the display method (it->glyphless_method) is
25304 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25305 length of the acronym or the hexadecimal string, UPPER_XOFF and
25306 UPPER_YOFF are pixel offsets for the upper part of the string,
25307 LOWER_XOFF and LOWER_YOFF are for the lower part.
25308
25309 For the other display methods, LEN through LOWER_YOFF are zero. */
25310
25311 static void
25312 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25313 short upper_xoff, short upper_yoff,
25314 short lower_xoff, short lower_yoff)
25315 {
25316 struct glyph *glyph;
25317 enum glyph_row_area area = it->area;
25318
25319 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25320 if (glyph < it->glyph_row->glyphs[area + 1])
25321 {
25322 /* If the glyph row is reversed, we need to prepend the glyph
25323 rather than append it. */
25324 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25325 {
25326 struct glyph *g;
25327
25328 /* Make room for the additional glyph. */
25329 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25330 g[1] = *g;
25331 glyph = it->glyph_row->glyphs[area];
25332 }
25333 glyph->charpos = CHARPOS (it->position);
25334 glyph->object = it->object;
25335 glyph->pixel_width = it->pixel_width;
25336 glyph->ascent = it->ascent;
25337 glyph->descent = it->descent;
25338 glyph->voffset = it->voffset;
25339 glyph->type = GLYPHLESS_GLYPH;
25340 glyph->u.glyphless.method = it->glyphless_method;
25341 glyph->u.glyphless.for_no_font = for_no_font;
25342 glyph->u.glyphless.len = len;
25343 glyph->u.glyphless.ch = it->c;
25344 glyph->slice.glyphless.upper_xoff = upper_xoff;
25345 glyph->slice.glyphless.upper_yoff = upper_yoff;
25346 glyph->slice.glyphless.lower_xoff = lower_xoff;
25347 glyph->slice.glyphless.lower_yoff = lower_yoff;
25348 glyph->avoid_cursor_p = it->avoid_cursor_p;
25349 glyph->multibyte_p = it->multibyte_p;
25350 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25351 {
25352 /* In R2L rows, the left and the right box edges need to be
25353 drawn in reverse direction. */
25354 glyph->right_box_line_p = it->start_of_box_run_p;
25355 glyph->left_box_line_p = it->end_of_box_run_p;
25356 }
25357 else
25358 {
25359 glyph->left_box_line_p = it->start_of_box_run_p;
25360 glyph->right_box_line_p = it->end_of_box_run_p;
25361 }
25362 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25363 || it->phys_descent > it->descent);
25364 glyph->padding_p = 0;
25365 glyph->glyph_not_available_p = 0;
25366 glyph->face_id = face_id;
25367 glyph->font_type = FONT_TYPE_UNKNOWN;
25368 if (it->bidi_p)
25369 {
25370 glyph->resolved_level = it->bidi_it.resolved_level;
25371 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25372 emacs_abort ();
25373 glyph->bidi_type = it->bidi_it.type;
25374 }
25375 ++it->glyph_row->used[area];
25376 }
25377 else
25378 IT_EXPAND_MATRIX_WIDTH (it, area);
25379 }
25380
25381
25382 /* Produce a glyph for a glyphless character for iterator IT.
25383 IT->glyphless_method specifies which method to use for displaying
25384 the character. See the description of enum
25385 glyphless_display_method in dispextern.h for the detail.
25386
25387 FOR_NO_FONT is nonzero if and only if this is for a character for
25388 which no font was found. ACRONYM, if non-nil, is an acronym string
25389 for the character. */
25390
25391 static void
25392 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25393 {
25394 int face_id;
25395 struct face *face;
25396 struct font *font;
25397 int base_width, base_height, width, height;
25398 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25399 int len;
25400
25401 /* Get the metrics of the base font. We always refer to the current
25402 ASCII face. */
25403 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25404 font = face->font ? face->font : FRAME_FONT (it->f);
25405 it->ascent = FONT_BASE (font) + font->baseline_offset;
25406 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25407 base_height = it->ascent + it->descent;
25408 base_width = font->average_width;
25409
25410 face_id = merge_glyphless_glyph_face (it);
25411
25412 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25413 {
25414 it->pixel_width = THIN_SPACE_WIDTH;
25415 len = 0;
25416 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25417 }
25418 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25419 {
25420 width = CHAR_WIDTH (it->c);
25421 if (width == 0)
25422 width = 1;
25423 else if (width > 4)
25424 width = 4;
25425 it->pixel_width = base_width * width;
25426 len = 0;
25427 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25428 }
25429 else
25430 {
25431 char buf[7];
25432 const char *str;
25433 unsigned int code[6];
25434 int upper_len;
25435 int ascent, descent;
25436 struct font_metrics metrics_upper, metrics_lower;
25437
25438 face = FACE_FROM_ID (it->f, face_id);
25439 font = face->font ? face->font : FRAME_FONT (it->f);
25440 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25441
25442 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25443 {
25444 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25445 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25446 if (CONSP (acronym))
25447 acronym = XCAR (acronym);
25448 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25449 }
25450 else
25451 {
25452 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25453 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25454 str = buf;
25455 }
25456 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25457 code[len] = font->driver->encode_char (font, str[len]);
25458 upper_len = (len + 1) / 2;
25459 font->driver->text_extents (font, code, upper_len,
25460 &metrics_upper);
25461 font->driver->text_extents (font, code + upper_len, len - upper_len,
25462 &metrics_lower);
25463
25464
25465
25466 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25467 width = max (metrics_upper.width, metrics_lower.width) + 4;
25468 upper_xoff = upper_yoff = 2; /* the typical case */
25469 if (base_width >= width)
25470 {
25471 /* Align the upper to the left, the lower to the right. */
25472 it->pixel_width = base_width;
25473 lower_xoff = base_width - 2 - metrics_lower.width;
25474 }
25475 else
25476 {
25477 /* Center the shorter one. */
25478 it->pixel_width = width;
25479 if (metrics_upper.width >= metrics_lower.width)
25480 lower_xoff = (width - metrics_lower.width) / 2;
25481 else
25482 {
25483 /* FIXME: This code doesn't look right. It formerly was
25484 missing the "lower_xoff = 0;", which couldn't have
25485 been right since it left lower_xoff uninitialized. */
25486 lower_xoff = 0;
25487 upper_xoff = (width - metrics_upper.width) / 2;
25488 }
25489 }
25490
25491 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25492 top, bottom, and between upper and lower strings. */
25493 height = (metrics_upper.ascent + metrics_upper.descent
25494 + metrics_lower.ascent + metrics_lower.descent) + 5;
25495 /* Center vertically.
25496 H:base_height, D:base_descent
25497 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25498
25499 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25500 descent = D - H/2 + h/2;
25501 lower_yoff = descent - 2 - ld;
25502 upper_yoff = lower_yoff - la - 1 - ud; */
25503 ascent = - (it->descent - (base_height + height + 1) / 2);
25504 descent = it->descent - (base_height - height) / 2;
25505 lower_yoff = descent - 2 - metrics_lower.descent;
25506 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25507 - metrics_upper.descent);
25508 /* Don't make the height shorter than the base height. */
25509 if (height > base_height)
25510 {
25511 it->ascent = ascent;
25512 it->descent = descent;
25513 }
25514 }
25515
25516 it->phys_ascent = it->ascent;
25517 it->phys_descent = it->descent;
25518 if (it->glyph_row)
25519 append_glyphless_glyph (it, face_id, for_no_font, len,
25520 upper_xoff, upper_yoff,
25521 lower_xoff, lower_yoff);
25522 it->nglyphs = 1;
25523 take_vertical_position_into_account (it);
25524 }
25525
25526
25527 /* RIF:
25528 Produce glyphs/get display metrics for the display element IT is
25529 loaded with. See the description of struct it in dispextern.h
25530 for an overview of struct it. */
25531
25532 void
25533 x_produce_glyphs (struct it *it)
25534 {
25535 int extra_line_spacing = it->extra_line_spacing;
25536
25537 it->glyph_not_available_p = 0;
25538
25539 if (it->what == IT_CHARACTER)
25540 {
25541 XChar2b char2b;
25542 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25543 struct font *font = face->font;
25544 struct font_metrics *pcm = NULL;
25545 int boff; /* Baseline offset. */
25546
25547 if (font == NULL)
25548 {
25549 /* When no suitable font is found, display this character by
25550 the method specified in the first extra slot of
25551 Vglyphless_char_display. */
25552 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25553
25554 eassert (it->what == IT_GLYPHLESS);
25555 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25556 goto done;
25557 }
25558
25559 boff = font->baseline_offset;
25560 if (font->vertical_centering)
25561 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25562
25563 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25564 {
25565 int stretched_p;
25566
25567 it->nglyphs = 1;
25568
25569 if (it->override_ascent >= 0)
25570 {
25571 it->ascent = it->override_ascent;
25572 it->descent = it->override_descent;
25573 boff = it->override_boff;
25574 }
25575 else
25576 {
25577 it->ascent = FONT_BASE (font) + boff;
25578 it->descent = FONT_DESCENT (font) - boff;
25579 }
25580
25581 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25582 {
25583 pcm = get_per_char_metric (font, &char2b);
25584 if (pcm->width == 0
25585 && pcm->rbearing == 0 && pcm->lbearing == 0)
25586 pcm = NULL;
25587 }
25588
25589 if (pcm)
25590 {
25591 it->phys_ascent = pcm->ascent + boff;
25592 it->phys_descent = pcm->descent - boff;
25593 it->pixel_width = pcm->width;
25594 }
25595 else
25596 {
25597 it->glyph_not_available_p = 1;
25598 it->phys_ascent = it->ascent;
25599 it->phys_descent = it->descent;
25600 it->pixel_width = font->space_width;
25601 }
25602
25603 if (it->constrain_row_ascent_descent_p)
25604 {
25605 if (it->descent > it->max_descent)
25606 {
25607 it->ascent += it->descent - it->max_descent;
25608 it->descent = it->max_descent;
25609 }
25610 if (it->ascent > it->max_ascent)
25611 {
25612 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25613 it->ascent = it->max_ascent;
25614 }
25615 it->phys_ascent = min (it->phys_ascent, it->ascent);
25616 it->phys_descent = min (it->phys_descent, it->descent);
25617 extra_line_spacing = 0;
25618 }
25619
25620 /* If this is a space inside a region of text with
25621 `space-width' property, change its width. */
25622 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25623 if (stretched_p)
25624 it->pixel_width *= XFLOATINT (it->space_width);
25625
25626 /* If face has a box, add the box thickness to the character
25627 height. If character has a box line to the left and/or
25628 right, add the box line width to the character's width. */
25629 if (face->box != FACE_NO_BOX)
25630 {
25631 int thick = face->box_line_width;
25632
25633 if (thick > 0)
25634 {
25635 it->ascent += thick;
25636 it->descent += thick;
25637 }
25638 else
25639 thick = -thick;
25640
25641 if (it->start_of_box_run_p)
25642 it->pixel_width += thick;
25643 if (it->end_of_box_run_p)
25644 it->pixel_width += thick;
25645 }
25646
25647 /* If face has an overline, add the height of the overline
25648 (1 pixel) and a 1 pixel margin to the character height. */
25649 if (face->overline_p)
25650 it->ascent += overline_margin;
25651
25652 if (it->constrain_row_ascent_descent_p)
25653 {
25654 if (it->ascent > it->max_ascent)
25655 it->ascent = it->max_ascent;
25656 if (it->descent > it->max_descent)
25657 it->descent = it->max_descent;
25658 }
25659
25660 take_vertical_position_into_account (it);
25661
25662 /* If we have to actually produce glyphs, do it. */
25663 if (it->glyph_row)
25664 {
25665 if (stretched_p)
25666 {
25667 /* Translate a space with a `space-width' property
25668 into a stretch glyph. */
25669 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25670 / FONT_HEIGHT (font));
25671 append_stretch_glyph (it, it->object, it->pixel_width,
25672 it->ascent + it->descent, ascent);
25673 }
25674 else
25675 append_glyph (it);
25676
25677 /* If characters with lbearing or rbearing are displayed
25678 in this line, record that fact in a flag of the
25679 glyph row. This is used to optimize X output code. */
25680 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25681 it->glyph_row->contains_overlapping_glyphs_p = 1;
25682 }
25683 if (! stretched_p && it->pixel_width == 0)
25684 /* We assure that all visible glyphs have at least 1-pixel
25685 width. */
25686 it->pixel_width = 1;
25687 }
25688 else if (it->char_to_display == '\n')
25689 {
25690 /* A newline has no width, but we need the height of the
25691 line. But if previous part of the line sets a height,
25692 don't increase that height. */
25693
25694 Lisp_Object height;
25695 Lisp_Object total_height = Qnil;
25696
25697 it->override_ascent = -1;
25698 it->pixel_width = 0;
25699 it->nglyphs = 0;
25700
25701 height = get_it_property (it, Qline_height);
25702 /* Split (line-height total-height) list. */
25703 if (CONSP (height)
25704 && CONSP (XCDR (height))
25705 && NILP (XCDR (XCDR (height))))
25706 {
25707 total_height = XCAR (XCDR (height));
25708 height = XCAR (height);
25709 }
25710 height = calc_line_height_property (it, height, font, boff, 1);
25711
25712 if (it->override_ascent >= 0)
25713 {
25714 it->ascent = it->override_ascent;
25715 it->descent = it->override_descent;
25716 boff = it->override_boff;
25717 }
25718 else
25719 {
25720 it->ascent = FONT_BASE (font) + boff;
25721 it->descent = FONT_DESCENT (font) - boff;
25722 }
25723
25724 if (EQ (height, Qt))
25725 {
25726 if (it->descent > it->max_descent)
25727 {
25728 it->ascent += it->descent - it->max_descent;
25729 it->descent = it->max_descent;
25730 }
25731 if (it->ascent > it->max_ascent)
25732 {
25733 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25734 it->ascent = it->max_ascent;
25735 }
25736 it->phys_ascent = min (it->phys_ascent, it->ascent);
25737 it->phys_descent = min (it->phys_descent, it->descent);
25738 it->constrain_row_ascent_descent_p = 1;
25739 extra_line_spacing = 0;
25740 }
25741 else
25742 {
25743 Lisp_Object spacing;
25744
25745 it->phys_ascent = it->ascent;
25746 it->phys_descent = it->descent;
25747
25748 if ((it->max_ascent > 0 || it->max_descent > 0)
25749 && face->box != FACE_NO_BOX
25750 && face->box_line_width > 0)
25751 {
25752 it->ascent += face->box_line_width;
25753 it->descent += face->box_line_width;
25754 }
25755 if (!NILP (height)
25756 && XINT (height) > it->ascent + it->descent)
25757 it->ascent = XINT (height) - it->descent;
25758
25759 if (!NILP (total_height))
25760 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25761 else
25762 {
25763 spacing = get_it_property (it, Qline_spacing);
25764 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25765 }
25766 if (INTEGERP (spacing))
25767 {
25768 extra_line_spacing = XINT (spacing);
25769 if (!NILP (total_height))
25770 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25771 }
25772 }
25773 }
25774 else /* i.e. (it->char_to_display == '\t') */
25775 {
25776 if (font->space_width > 0)
25777 {
25778 int tab_width = it->tab_width * font->space_width;
25779 int x = it->current_x + it->continuation_lines_width;
25780 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25781
25782 /* If the distance from the current position to the next tab
25783 stop is less than a space character width, use the
25784 tab stop after that. */
25785 if (next_tab_x - x < font->space_width)
25786 next_tab_x += tab_width;
25787
25788 it->pixel_width = next_tab_x - x;
25789 it->nglyphs = 1;
25790 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25791 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25792
25793 if (it->glyph_row)
25794 {
25795 append_stretch_glyph (it, it->object, it->pixel_width,
25796 it->ascent + it->descent, it->ascent);
25797 }
25798 }
25799 else
25800 {
25801 it->pixel_width = 0;
25802 it->nglyphs = 1;
25803 }
25804 }
25805 }
25806 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25807 {
25808 /* A static composition.
25809
25810 Note: A composition is represented as one glyph in the
25811 glyph matrix. There are no padding glyphs.
25812
25813 Important note: pixel_width, ascent, and descent are the
25814 values of what is drawn by draw_glyphs (i.e. the values of
25815 the overall glyphs composed). */
25816 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25817 int boff; /* baseline offset */
25818 struct composition *cmp = composition_table[it->cmp_it.id];
25819 int glyph_len = cmp->glyph_len;
25820 struct font *font = face->font;
25821
25822 it->nglyphs = 1;
25823
25824 /* If we have not yet calculated pixel size data of glyphs of
25825 the composition for the current face font, calculate them
25826 now. Theoretically, we have to check all fonts for the
25827 glyphs, but that requires much time and memory space. So,
25828 here we check only the font of the first glyph. This may
25829 lead to incorrect display, but it's very rare, and C-l
25830 (recenter-top-bottom) can correct the display anyway. */
25831 if (! cmp->font || cmp->font != font)
25832 {
25833 /* Ascent and descent of the font of the first character
25834 of this composition (adjusted by baseline offset).
25835 Ascent and descent of overall glyphs should not be less
25836 than these, respectively. */
25837 int font_ascent, font_descent, font_height;
25838 /* Bounding box of the overall glyphs. */
25839 int leftmost, rightmost, lowest, highest;
25840 int lbearing, rbearing;
25841 int i, width, ascent, descent;
25842 int left_padded = 0, right_padded = 0;
25843 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25844 XChar2b char2b;
25845 struct font_metrics *pcm;
25846 int font_not_found_p;
25847 ptrdiff_t pos;
25848
25849 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25850 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25851 break;
25852 if (glyph_len < cmp->glyph_len)
25853 right_padded = 1;
25854 for (i = 0; i < glyph_len; i++)
25855 {
25856 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25857 break;
25858 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25859 }
25860 if (i > 0)
25861 left_padded = 1;
25862
25863 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25864 : IT_CHARPOS (*it));
25865 /* If no suitable font is found, use the default font. */
25866 font_not_found_p = font == NULL;
25867 if (font_not_found_p)
25868 {
25869 face = face->ascii_face;
25870 font = face->font;
25871 }
25872 boff = font->baseline_offset;
25873 if (font->vertical_centering)
25874 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25875 font_ascent = FONT_BASE (font) + boff;
25876 font_descent = FONT_DESCENT (font) - boff;
25877 font_height = FONT_HEIGHT (font);
25878
25879 cmp->font = font;
25880
25881 pcm = NULL;
25882 if (! font_not_found_p)
25883 {
25884 get_char_face_and_encoding (it->f, c, it->face_id,
25885 &char2b, 0);
25886 pcm = get_per_char_metric (font, &char2b);
25887 }
25888
25889 /* Initialize the bounding box. */
25890 if (pcm)
25891 {
25892 width = cmp->glyph_len > 0 ? pcm->width : 0;
25893 ascent = pcm->ascent;
25894 descent = pcm->descent;
25895 lbearing = pcm->lbearing;
25896 rbearing = pcm->rbearing;
25897 }
25898 else
25899 {
25900 width = cmp->glyph_len > 0 ? font->space_width : 0;
25901 ascent = FONT_BASE (font);
25902 descent = FONT_DESCENT (font);
25903 lbearing = 0;
25904 rbearing = width;
25905 }
25906
25907 rightmost = width;
25908 leftmost = 0;
25909 lowest = - descent + boff;
25910 highest = ascent + boff;
25911
25912 if (! font_not_found_p
25913 && font->default_ascent
25914 && CHAR_TABLE_P (Vuse_default_ascent)
25915 && !NILP (Faref (Vuse_default_ascent,
25916 make_number (it->char_to_display))))
25917 highest = font->default_ascent + boff;
25918
25919 /* Draw the first glyph at the normal position. It may be
25920 shifted to right later if some other glyphs are drawn
25921 at the left. */
25922 cmp->offsets[i * 2] = 0;
25923 cmp->offsets[i * 2 + 1] = boff;
25924 cmp->lbearing = lbearing;
25925 cmp->rbearing = rbearing;
25926
25927 /* Set cmp->offsets for the remaining glyphs. */
25928 for (i++; i < glyph_len; i++)
25929 {
25930 int left, right, btm, top;
25931 int ch = COMPOSITION_GLYPH (cmp, i);
25932 int face_id;
25933 struct face *this_face;
25934
25935 if (ch == '\t')
25936 ch = ' ';
25937 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25938 this_face = FACE_FROM_ID (it->f, face_id);
25939 font = this_face->font;
25940
25941 if (font == NULL)
25942 pcm = NULL;
25943 else
25944 {
25945 get_char_face_and_encoding (it->f, ch, face_id,
25946 &char2b, 0);
25947 pcm = get_per_char_metric (font, &char2b);
25948 }
25949 if (! pcm)
25950 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25951 else
25952 {
25953 width = pcm->width;
25954 ascent = pcm->ascent;
25955 descent = pcm->descent;
25956 lbearing = pcm->lbearing;
25957 rbearing = pcm->rbearing;
25958 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25959 {
25960 /* Relative composition with or without
25961 alternate chars. */
25962 left = (leftmost + rightmost - width) / 2;
25963 btm = - descent + boff;
25964 if (font->relative_compose
25965 && (! CHAR_TABLE_P (Vignore_relative_composition)
25966 || NILP (Faref (Vignore_relative_composition,
25967 make_number (ch)))))
25968 {
25969
25970 if (- descent >= font->relative_compose)
25971 /* One extra pixel between two glyphs. */
25972 btm = highest + 1;
25973 else if (ascent <= 0)
25974 /* One extra pixel between two glyphs. */
25975 btm = lowest - 1 - ascent - descent;
25976 }
25977 }
25978 else
25979 {
25980 /* A composition rule is specified by an integer
25981 value that encodes global and new reference
25982 points (GREF and NREF). GREF and NREF are
25983 specified by numbers as below:
25984
25985 0---1---2 -- ascent
25986 | |
25987 | |
25988 | |
25989 9--10--11 -- center
25990 | |
25991 ---3---4---5--- baseline
25992 | |
25993 6---7---8 -- descent
25994 */
25995 int rule = COMPOSITION_RULE (cmp, i);
25996 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25997
25998 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25999 grefx = gref % 3, nrefx = nref % 3;
26000 grefy = gref / 3, nrefy = nref / 3;
26001 if (xoff)
26002 xoff = font_height * (xoff - 128) / 256;
26003 if (yoff)
26004 yoff = font_height * (yoff - 128) / 256;
26005
26006 left = (leftmost
26007 + grefx * (rightmost - leftmost) / 2
26008 - nrefx * width / 2
26009 + xoff);
26010
26011 btm = ((grefy == 0 ? highest
26012 : grefy == 1 ? 0
26013 : grefy == 2 ? lowest
26014 : (highest + lowest) / 2)
26015 - (nrefy == 0 ? ascent + descent
26016 : nrefy == 1 ? descent - boff
26017 : nrefy == 2 ? 0
26018 : (ascent + descent) / 2)
26019 + yoff);
26020 }
26021
26022 cmp->offsets[i * 2] = left;
26023 cmp->offsets[i * 2 + 1] = btm + descent;
26024
26025 /* Update the bounding box of the overall glyphs. */
26026 if (width > 0)
26027 {
26028 right = left + width;
26029 if (left < leftmost)
26030 leftmost = left;
26031 if (right > rightmost)
26032 rightmost = right;
26033 }
26034 top = btm + descent + ascent;
26035 if (top > highest)
26036 highest = top;
26037 if (btm < lowest)
26038 lowest = btm;
26039
26040 if (cmp->lbearing > left + lbearing)
26041 cmp->lbearing = left + lbearing;
26042 if (cmp->rbearing < left + rbearing)
26043 cmp->rbearing = left + rbearing;
26044 }
26045 }
26046
26047 /* If there are glyphs whose x-offsets are negative,
26048 shift all glyphs to the right and make all x-offsets
26049 non-negative. */
26050 if (leftmost < 0)
26051 {
26052 for (i = 0; i < cmp->glyph_len; i++)
26053 cmp->offsets[i * 2] -= leftmost;
26054 rightmost -= leftmost;
26055 cmp->lbearing -= leftmost;
26056 cmp->rbearing -= leftmost;
26057 }
26058
26059 if (left_padded && cmp->lbearing < 0)
26060 {
26061 for (i = 0; i < cmp->glyph_len; i++)
26062 cmp->offsets[i * 2] -= cmp->lbearing;
26063 rightmost -= cmp->lbearing;
26064 cmp->rbearing -= cmp->lbearing;
26065 cmp->lbearing = 0;
26066 }
26067 if (right_padded && rightmost < cmp->rbearing)
26068 {
26069 rightmost = cmp->rbearing;
26070 }
26071
26072 cmp->pixel_width = rightmost;
26073 cmp->ascent = highest;
26074 cmp->descent = - lowest;
26075 if (cmp->ascent < font_ascent)
26076 cmp->ascent = font_ascent;
26077 if (cmp->descent < font_descent)
26078 cmp->descent = font_descent;
26079 }
26080
26081 if (it->glyph_row
26082 && (cmp->lbearing < 0
26083 || cmp->rbearing > cmp->pixel_width))
26084 it->glyph_row->contains_overlapping_glyphs_p = 1;
26085
26086 it->pixel_width = cmp->pixel_width;
26087 it->ascent = it->phys_ascent = cmp->ascent;
26088 it->descent = it->phys_descent = cmp->descent;
26089 if (face->box != FACE_NO_BOX)
26090 {
26091 int thick = face->box_line_width;
26092
26093 if (thick > 0)
26094 {
26095 it->ascent += thick;
26096 it->descent += thick;
26097 }
26098 else
26099 thick = - thick;
26100
26101 if (it->start_of_box_run_p)
26102 it->pixel_width += thick;
26103 if (it->end_of_box_run_p)
26104 it->pixel_width += thick;
26105 }
26106
26107 /* If face has an overline, add the height of the overline
26108 (1 pixel) and a 1 pixel margin to the character height. */
26109 if (face->overline_p)
26110 it->ascent += overline_margin;
26111
26112 take_vertical_position_into_account (it);
26113 if (it->ascent < 0)
26114 it->ascent = 0;
26115 if (it->descent < 0)
26116 it->descent = 0;
26117
26118 if (it->glyph_row && cmp->glyph_len > 0)
26119 append_composite_glyph (it);
26120 }
26121 else if (it->what == IT_COMPOSITION)
26122 {
26123 /* A dynamic (automatic) composition. */
26124 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26125 Lisp_Object gstring;
26126 struct font_metrics metrics;
26127
26128 it->nglyphs = 1;
26129
26130 gstring = composition_gstring_from_id (it->cmp_it.id);
26131 it->pixel_width
26132 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26133 &metrics);
26134 if (it->glyph_row
26135 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26136 it->glyph_row->contains_overlapping_glyphs_p = 1;
26137 it->ascent = it->phys_ascent = metrics.ascent;
26138 it->descent = it->phys_descent = metrics.descent;
26139 if (face->box != FACE_NO_BOX)
26140 {
26141 int thick = face->box_line_width;
26142
26143 if (thick > 0)
26144 {
26145 it->ascent += thick;
26146 it->descent += thick;
26147 }
26148 else
26149 thick = - thick;
26150
26151 if (it->start_of_box_run_p)
26152 it->pixel_width += thick;
26153 if (it->end_of_box_run_p)
26154 it->pixel_width += thick;
26155 }
26156 /* If face has an overline, add the height of the overline
26157 (1 pixel) and a 1 pixel margin to the character height. */
26158 if (face->overline_p)
26159 it->ascent += overline_margin;
26160 take_vertical_position_into_account (it);
26161 if (it->ascent < 0)
26162 it->ascent = 0;
26163 if (it->descent < 0)
26164 it->descent = 0;
26165
26166 if (it->glyph_row)
26167 append_composite_glyph (it);
26168 }
26169 else if (it->what == IT_GLYPHLESS)
26170 produce_glyphless_glyph (it, 0, Qnil);
26171 else if (it->what == IT_IMAGE)
26172 produce_image_glyph (it);
26173 else if (it->what == IT_STRETCH)
26174 produce_stretch_glyph (it);
26175
26176 done:
26177 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26178 because this isn't true for images with `:ascent 100'. */
26179 eassert (it->ascent >= 0 && it->descent >= 0);
26180 if (it->area == TEXT_AREA)
26181 it->current_x += it->pixel_width;
26182
26183 if (extra_line_spacing > 0)
26184 {
26185 it->descent += extra_line_spacing;
26186 if (extra_line_spacing > it->max_extra_line_spacing)
26187 it->max_extra_line_spacing = extra_line_spacing;
26188 }
26189
26190 it->max_ascent = max (it->max_ascent, it->ascent);
26191 it->max_descent = max (it->max_descent, it->descent);
26192 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26193 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26194 }
26195
26196 /* EXPORT for RIF:
26197 Output LEN glyphs starting at START at the nominal cursor position.
26198 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26199 being updated, and UPDATED_AREA is the area of that row being updated. */
26200
26201 void
26202 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26203 struct glyph *start, enum glyph_row_area updated_area, int len)
26204 {
26205 int x, hpos, chpos = w->phys_cursor.hpos;
26206
26207 eassert (updated_row);
26208 /* When the window is hscrolled, cursor hpos can legitimately be out
26209 of bounds, but we draw the cursor at the corresponding window
26210 margin in that case. */
26211 if (!updated_row->reversed_p && chpos < 0)
26212 chpos = 0;
26213 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26214 chpos = updated_row->used[TEXT_AREA] - 1;
26215
26216 block_input ();
26217
26218 /* Write glyphs. */
26219
26220 hpos = start - updated_row->glyphs[updated_area];
26221 x = draw_glyphs (w, w->output_cursor.x,
26222 updated_row, updated_area,
26223 hpos, hpos + len,
26224 DRAW_NORMAL_TEXT, 0);
26225
26226 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26227 if (updated_area == TEXT_AREA
26228 && w->phys_cursor_on_p
26229 && w->phys_cursor.vpos == w->output_cursor.vpos
26230 && chpos >= hpos
26231 && chpos < hpos + len)
26232 w->phys_cursor_on_p = 0;
26233
26234 unblock_input ();
26235
26236 /* Advance the output cursor. */
26237 w->output_cursor.hpos += len;
26238 w->output_cursor.x = x;
26239 }
26240
26241
26242 /* EXPORT for RIF:
26243 Insert LEN glyphs from START at the nominal cursor position. */
26244
26245 void
26246 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26247 struct glyph *start, enum glyph_row_area updated_area, int len)
26248 {
26249 struct frame *f;
26250 int line_height, shift_by_width, shifted_region_width;
26251 struct glyph_row *row;
26252 struct glyph *glyph;
26253 int frame_x, frame_y;
26254 ptrdiff_t hpos;
26255
26256 eassert (updated_row);
26257 block_input ();
26258 f = XFRAME (WINDOW_FRAME (w));
26259
26260 /* Get the height of the line we are in. */
26261 row = updated_row;
26262 line_height = row->height;
26263
26264 /* Get the width of the glyphs to insert. */
26265 shift_by_width = 0;
26266 for (glyph = start; glyph < start + len; ++glyph)
26267 shift_by_width += glyph->pixel_width;
26268
26269 /* Get the width of the region to shift right. */
26270 shifted_region_width = (window_box_width (w, updated_area)
26271 - w->output_cursor.x
26272 - shift_by_width);
26273
26274 /* Shift right. */
26275 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26276 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26277
26278 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26279 line_height, shift_by_width);
26280
26281 /* Write the glyphs. */
26282 hpos = start - row->glyphs[updated_area];
26283 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26284 hpos, hpos + len,
26285 DRAW_NORMAL_TEXT, 0);
26286
26287 /* Advance the output cursor. */
26288 w->output_cursor.hpos += len;
26289 w->output_cursor.x += shift_by_width;
26290 unblock_input ();
26291 }
26292
26293
26294 /* EXPORT for RIF:
26295 Erase the current text line from the nominal cursor position
26296 (inclusive) to pixel column TO_X (exclusive). The idea is that
26297 everything from TO_X onward is already erased.
26298
26299 TO_X is a pixel position relative to UPDATED_AREA of currently
26300 updated window W. TO_X == -1 means clear to the end of this area. */
26301
26302 void
26303 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26304 enum glyph_row_area updated_area, int to_x)
26305 {
26306 struct frame *f;
26307 int max_x, min_y, max_y;
26308 int from_x, from_y, to_y;
26309
26310 eassert (updated_row);
26311 f = XFRAME (w->frame);
26312
26313 if (updated_row->full_width_p)
26314 max_x = (WINDOW_PIXEL_WIDTH (w)
26315 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26316 else
26317 max_x = window_box_width (w, updated_area);
26318 max_y = window_text_bottom_y (w);
26319
26320 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26321 of window. For TO_X > 0, truncate to end of drawing area. */
26322 if (to_x == 0)
26323 return;
26324 else if (to_x < 0)
26325 to_x = max_x;
26326 else
26327 to_x = min (to_x, max_x);
26328
26329 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26330
26331 /* Notice if the cursor will be cleared by this operation. */
26332 if (!updated_row->full_width_p)
26333 notice_overwritten_cursor (w, updated_area,
26334 w->output_cursor.x, -1,
26335 updated_row->y,
26336 MATRIX_ROW_BOTTOM_Y (updated_row));
26337
26338 from_x = w->output_cursor.x;
26339
26340 /* Translate to frame coordinates. */
26341 if (updated_row->full_width_p)
26342 {
26343 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26344 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26345 }
26346 else
26347 {
26348 int area_left = window_box_left (w, updated_area);
26349 from_x += area_left;
26350 to_x += area_left;
26351 }
26352
26353 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26354 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26355 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26356
26357 /* Prevent inadvertently clearing to end of the X window. */
26358 if (to_x > from_x && to_y > from_y)
26359 {
26360 block_input ();
26361 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26362 to_x - from_x, to_y - from_y);
26363 unblock_input ();
26364 }
26365 }
26366
26367 #endif /* HAVE_WINDOW_SYSTEM */
26368
26369
26370 \f
26371 /***********************************************************************
26372 Cursor types
26373 ***********************************************************************/
26374
26375 /* Value is the internal representation of the specified cursor type
26376 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26377 of the bar cursor. */
26378
26379 static enum text_cursor_kinds
26380 get_specified_cursor_type (Lisp_Object arg, int *width)
26381 {
26382 enum text_cursor_kinds type;
26383
26384 if (NILP (arg))
26385 return NO_CURSOR;
26386
26387 if (EQ (arg, Qbox))
26388 return FILLED_BOX_CURSOR;
26389
26390 if (EQ (arg, Qhollow))
26391 return HOLLOW_BOX_CURSOR;
26392
26393 if (EQ (arg, Qbar))
26394 {
26395 *width = 2;
26396 return BAR_CURSOR;
26397 }
26398
26399 if (CONSP (arg)
26400 && EQ (XCAR (arg), Qbar)
26401 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26402 {
26403 *width = XINT (XCDR (arg));
26404 return BAR_CURSOR;
26405 }
26406
26407 if (EQ (arg, Qhbar))
26408 {
26409 *width = 2;
26410 return HBAR_CURSOR;
26411 }
26412
26413 if (CONSP (arg)
26414 && EQ (XCAR (arg), Qhbar)
26415 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26416 {
26417 *width = XINT (XCDR (arg));
26418 return HBAR_CURSOR;
26419 }
26420
26421 /* Treat anything unknown as "hollow box cursor".
26422 It was bad to signal an error; people have trouble fixing
26423 .Xdefaults with Emacs, when it has something bad in it. */
26424 type = HOLLOW_BOX_CURSOR;
26425
26426 return type;
26427 }
26428
26429 /* Set the default cursor types for specified frame. */
26430 void
26431 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26432 {
26433 int width = 1;
26434 Lisp_Object tem;
26435
26436 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26437 FRAME_CURSOR_WIDTH (f) = width;
26438
26439 /* By default, set up the blink-off state depending on the on-state. */
26440
26441 tem = Fassoc (arg, Vblink_cursor_alist);
26442 if (!NILP (tem))
26443 {
26444 FRAME_BLINK_OFF_CURSOR (f)
26445 = get_specified_cursor_type (XCDR (tem), &width);
26446 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26447 }
26448 else
26449 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26450
26451 /* Make sure the cursor gets redrawn. */
26452 f->cursor_type_changed = 1;
26453 }
26454
26455
26456 #ifdef HAVE_WINDOW_SYSTEM
26457
26458 /* Return the cursor we want to be displayed in window W. Return
26459 width of bar/hbar cursor through WIDTH arg. Return with
26460 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26461 (i.e. if the `system caret' should track this cursor).
26462
26463 In a mini-buffer window, we want the cursor only to appear if we
26464 are reading input from this window. For the selected window, we
26465 want the cursor type given by the frame parameter or buffer local
26466 setting of cursor-type. If explicitly marked off, draw no cursor.
26467 In all other cases, we want a hollow box cursor. */
26468
26469 static enum text_cursor_kinds
26470 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26471 int *active_cursor)
26472 {
26473 struct frame *f = XFRAME (w->frame);
26474 struct buffer *b = XBUFFER (w->contents);
26475 int cursor_type = DEFAULT_CURSOR;
26476 Lisp_Object alt_cursor;
26477 int non_selected = 0;
26478
26479 *active_cursor = 1;
26480
26481 /* Echo area */
26482 if (cursor_in_echo_area
26483 && FRAME_HAS_MINIBUF_P (f)
26484 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26485 {
26486 if (w == XWINDOW (echo_area_window))
26487 {
26488 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26489 {
26490 *width = FRAME_CURSOR_WIDTH (f);
26491 return FRAME_DESIRED_CURSOR (f);
26492 }
26493 else
26494 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26495 }
26496
26497 *active_cursor = 0;
26498 non_selected = 1;
26499 }
26500
26501 /* Detect a nonselected window or nonselected frame. */
26502 else if (w != XWINDOW (f->selected_window)
26503 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26504 {
26505 *active_cursor = 0;
26506
26507 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26508 return NO_CURSOR;
26509
26510 non_selected = 1;
26511 }
26512
26513 /* Never display a cursor in a window in which cursor-type is nil. */
26514 if (NILP (BVAR (b, cursor_type)))
26515 return NO_CURSOR;
26516
26517 /* Get the normal cursor type for this window. */
26518 if (EQ (BVAR (b, cursor_type), Qt))
26519 {
26520 cursor_type = FRAME_DESIRED_CURSOR (f);
26521 *width = FRAME_CURSOR_WIDTH (f);
26522 }
26523 else
26524 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26525
26526 /* Use cursor-in-non-selected-windows instead
26527 for non-selected window or frame. */
26528 if (non_selected)
26529 {
26530 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26531 if (!EQ (Qt, alt_cursor))
26532 return get_specified_cursor_type (alt_cursor, width);
26533 /* t means modify the normal cursor type. */
26534 if (cursor_type == FILLED_BOX_CURSOR)
26535 cursor_type = HOLLOW_BOX_CURSOR;
26536 else if (cursor_type == BAR_CURSOR && *width > 1)
26537 --*width;
26538 return cursor_type;
26539 }
26540
26541 /* Use normal cursor if not blinked off. */
26542 if (!w->cursor_off_p)
26543 {
26544 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26545 {
26546 if (cursor_type == FILLED_BOX_CURSOR)
26547 {
26548 /* Using a block cursor on large images can be very annoying.
26549 So use a hollow cursor for "large" images.
26550 If image is not transparent (no mask), also use hollow cursor. */
26551 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26552 if (img != NULL && IMAGEP (img->spec))
26553 {
26554 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26555 where N = size of default frame font size.
26556 This should cover most of the "tiny" icons people may use. */
26557 if (!img->mask
26558 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26559 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26560 cursor_type = HOLLOW_BOX_CURSOR;
26561 }
26562 }
26563 else if (cursor_type != NO_CURSOR)
26564 {
26565 /* Display current only supports BOX and HOLLOW cursors for images.
26566 So for now, unconditionally use a HOLLOW cursor when cursor is
26567 not a solid box cursor. */
26568 cursor_type = HOLLOW_BOX_CURSOR;
26569 }
26570 }
26571 return cursor_type;
26572 }
26573
26574 /* Cursor is blinked off, so determine how to "toggle" it. */
26575
26576 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26577 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26578 return get_specified_cursor_type (XCDR (alt_cursor), width);
26579
26580 /* Then see if frame has specified a specific blink off cursor type. */
26581 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26582 {
26583 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26584 return FRAME_BLINK_OFF_CURSOR (f);
26585 }
26586
26587 #if 0
26588 /* Some people liked having a permanently visible blinking cursor,
26589 while others had very strong opinions against it. So it was
26590 decided to remove it. KFS 2003-09-03 */
26591
26592 /* Finally perform built-in cursor blinking:
26593 filled box <-> hollow box
26594 wide [h]bar <-> narrow [h]bar
26595 narrow [h]bar <-> no cursor
26596 other type <-> no cursor */
26597
26598 if (cursor_type == FILLED_BOX_CURSOR)
26599 return HOLLOW_BOX_CURSOR;
26600
26601 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26602 {
26603 *width = 1;
26604 return cursor_type;
26605 }
26606 #endif
26607
26608 return NO_CURSOR;
26609 }
26610
26611
26612 /* Notice when the text cursor of window W has been completely
26613 overwritten by a drawing operation that outputs glyphs in AREA
26614 starting at X0 and ending at X1 in the line starting at Y0 and
26615 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26616 the rest of the line after X0 has been written. Y coordinates
26617 are window-relative. */
26618
26619 static void
26620 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26621 int x0, int x1, int y0, int y1)
26622 {
26623 int cx0, cx1, cy0, cy1;
26624 struct glyph_row *row;
26625
26626 if (!w->phys_cursor_on_p)
26627 return;
26628 if (area != TEXT_AREA)
26629 return;
26630
26631 if (w->phys_cursor.vpos < 0
26632 || w->phys_cursor.vpos >= w->current_matrix->nrows
26633 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26634 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26635 return;
26636
26637 if (row->cursor_in_fringe_p)
26638 {
26639 row->cursor_in_fringe_p = 0;
26640 draw_fringe_bitmap (w, row, row->reversed_p);
26641 w->phys_cursor_on_p = 0;
26642 return;
26643 }
26644
26645 cx0 = w->phys_cursor.x;
26646 cx1 = cx0 + w->phys_cursor_width;
26647 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26648 return;
26649
26650 /* The cursor image will be completely removed from the
26651 screen if the output area intersects the cursor area in
26652 y-direction. When we draw in [y0 y1[, and some part of
26653 the cursor is at y < y0, that part must have been drawn
26654 before. When scrolling, the cursor is erased before
26655 actually scrolling, so we don't come here. When not
26656 scrolling, the rows above the old cursor row must have
26657 changed, and in this case these rows must have written
26658 over the cursor image.
26659
26660 Likewise if part of the cursor is below y1, with the
26661 exception of the cursor being in the first blank row at
26662 the buffer and window end because update_text_area
26663 doesn't draw that row. (Except when it does, but
26664 that's handled in update_text_area.) */
26665
26666 cy0 = w->phys_cursor.y;
26667 cy1 = cy0 + w->phys_cursor_height;
26668 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26669 return;
26670
26671 w->phys_cursor_on_p = 0;
26672 }
26673
26674 #endif /* HAVE_WINDOW_SYSTEM */
26675
26676 \f
26677 /************************************************************************
26678 Mouse Face
26679 ************************************************************************/
26680
26681 #ifdef HAVE_WINDOW_SYSTEM
26682
26683 /* EXPORT for RIF:
26684 Fix the display of area AREA of overlapping row ROW in window W
26685 with respect to the overlapping part OVERLAPS. */
26686
26687 void
26688 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26689 enum glyph_row_area area, int overlaps)
26690 {
26691 int i, x;
26692
26693 block_input ();
26694
26695 x = 0;
26696 for (i = 0; i < row->used[area];)
26697 {
26698 if (row->glyphs[area][i].overlaps_vertically_p)
26699 {
26700 int start = i, start_x = x;
26701
26702 do
26703 {
26704 x += row->glyphs[area][i].pixel_width;
26705 ++i;
26706 }
26707 while (i < row->used[area]
26708 && row->glyphs[area][i].overlaps_vertically_p);
26709
26710 draw_glyphs (w, start_x, row, area,
26711 start, i,
26712 DRAW_NORMAL_TEXT, overlaps);
26713 }
26714 else
26715 {
26716 x += row->glyphs[area][i].pixel_width;
26717 ++i;
26718 }
26719 }
26720
26721 unblock_input ();
26722 }
26723
26724
26725 /* EXPORT:
26726 Draw the cursor glyph of window W in glyph row ROW. See the
26727 comment of draw_glyphs for the meaning of HL. */
26728
26729 void
26730 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26731 enum draw_glyphs_face hl)
26732 {
26733 /* If cursor hpos is out of bounds, don't draw garbage. This can
26734 happen in mini-buffer windows when switching between echo area
26735 glyphs and mini-buffer. */
26736 if ((row->reversed_p
26737 ? (w->phys_cursor.hpos >= 0)
26738 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26739 {
26740 int on_p = w->phys_cursor_on_p;
26741 int x1;
26742 int hpos = w->phys_cursor.hpos;
26743
26744 /* When the window is hscrolled, cursor hpos can legitimately be
26745 out of bounds, but we draw the cursor at the corresponding
26746 window margin in that case. */
26747 if (!row->reversed_p && hpos < 0)
26748 hpos = 0;
26749 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26750 hpos = row->used[TEXT_AREA] - 1;
26751
26752 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26753 hl, 0);
26754 w->phys_cursor_on_p = on_p;
26755
26756 if (hl == DRAW_CURSOR)
26757 w->phys_cursor_width = x1 - w->phys_cursor.x;
26758 /* When we erase the cursor, and ROW is overlapped by other
26759 rows, make sure that these overlapping parts of other rows
26760 are redrawn. */
26761 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26762 {
26763 w->phys_cursor_width = x1 - w->phys_cursor.x;
26764
26765 if (row > w->current_matrix->rows
26766 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26767 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26768 OVERLAPS_ERASED_CURSOR);
26769
26770 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26771 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26772 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26773 OVERLAPS_ERASED_CURSOR);
26774 }
26775 }
26776 }
26777
26778
26779 /* Erase the image of a cursor of window W from the screen. */
26780
26781 #ifndef HAVE_NTGUI
26782 static
26783 #endif
26784 void
26785 erase_phys_cursor (struct window *w)
26786 {
26787 struct frame *f = XFRAME (w->frame);
26788 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26789 int hpos = w->phys_cursor.hpos;
26790 int vpos = w->phys_cursor.vpos;
26791 int mouse_face_here_p = 0;
26792 struct glyph_matrix *active_glyphs = w->current_matrix;
26793 struct glyph_row *cursor_row;
26794 struct glyph *cursor_glyph;
26795 enum draw_glyphs_face hl;
26796
26797 /* No cursor displayed or row invalidated => nothing to do on the
26798 screen. */
26799 if (w->phys_cursor_type == NO_CURSOR)
26800 goto mark_cursor_off;
26801
26802 /* VPOS >= active_glyphs->nrows means that window has been resized.
26803 Don't bother to erase the cursor. */
26804 if (vpos >= active_glyphs->nrows)
26805 goto mark_cursor_off;
26806
26807 /* If row containing cursor is marked invalid, there is nothing we
26808 can do. */
26809 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26810 if (!cursor_row->enabled_p)
26811 goto mark_cursor_off;
26812
26813 /* If line spacing is > 0, old cursor may only be partially visible in
26814 window after split-window. So adjust visible height. */
26815 cursor_row->visible_height = min (cursor_row->visible_height,
26816 window_text_bottom_y (w) - cursor_row->y);
26817
26818 /* If row is completely invisible, don't attempt to delete a cursor which
26819 isn't there. This can happen if cursor is at top of a window, and
26820 we switch to a buffer with a header line in that window. */
26821 if (cursor_row->visible_height <= 0)
26822 goto mark_cursor_off;
26823
26824 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26825 if (cursor_row->cursor_in_fringe_p)
26826 {
26827 cursor_row->cursor_in_fringe_p = 0;
26828 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26829 goto mark_cursor_off;
26830 }
26831
26832 /* This can happen when the new row is shorter than the old one.
26833 In this case, either draw_glyphs or clear_end_of_line
26834 should have cleared the cursor. Note that we wouldn't be
26835 able to erase the cursor in this case because we don't have a
26836 cursor glyph at hand. */
26837 if ((cursor_row->reversed_p
26838 ? (w->phys_cursor.hpos < 0)
26839 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26840 goto mark_cursor_off;
26841
26842 /* When the window is hscrolled, cursor hpos can legitimately be out
26843 of bounds, but we draw the cursor at the corresponding window
26844 margin in that case. */
26845 if (!cursor_row->reversed_p && hpos < 0)
26846 hpos = 0;
26847 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26848 hpos = cursor_row->used[TEXT_AREA] - 1;
26849
26850 /* If the cursor is in the mouse face area, redisplay that when
26851 we clear the cursor. */
26852 if (! NILP (hlinfo->mouse_face_window)
26853 && coords_in_mouse_face_p (w, hpos, vpos)
26854 /* Don't redraw the cursor's spot in mouse face if it is at the
26855 end of a line (on a newline). The cursor appears there, but
26856 mouse highlighting does not. */
26857 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26858 mouse_face_here_p = 1;
26859
26860 /* Maybe clear the display under the cursor. */
26861 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26862 {
26863 int x, y, left_x;
26864 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26865 int width;
26866
26867 cursor_glyph = get_phys_cursor_glyph (w);
26868 if (cursor_glyph == NULL)
26869 goto mark_cursor_off;
26870
26871 width = cursor_glyph->pixel_width;
26872 left_x = window_box_left_offset (w, TEXT_AREA);
26873 x = w->phys_cursor.x;
26874 if (x < left_x)
26875 width -= left_x - x;
26876 width = min (width, window_box_width (w, TEXT_AREA) - x);
26877 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26878 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26879
26880 if (width > 0)
26881 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26882 }
26883
26884 /* Erase the cursor by redrawing the character underneath it. */
26885 if (mouse_face_here_p)
26886 hl = DRAW_MOUSE_FACE;
26887 else
26888 hl = DRAW_NORMAL_TEXT;
26889 draw_phys_cursor_glyph (w, cursor_row, hl);
26890
26891 mark_cursor_off:
26892 w->phys_cursor_on_p = 0;
26893 w->phys_cursor_type = NO_CURSOR;
26894 }
26895
26896
26897 /* EXPORT:
26898 Display or clear cursor of window W. If ON is zero, clear the
26899 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26900 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26901
26902 void
26903 display_and_set_cursor (struct window *w, bool on,
26904 int hpos, int vpos, int x, int y)
26905 {
26906 struct frame *f = XFRAME (w->frame);
26907 int new_cursor_type;
26908 int new_cursor_width;
26909 int active_cursor;
26910 struct glyph_row *glyph_row;
26911 struct glyph *glyph;
26912
26913 /* This is pointless on invisible frames, and dangerous on garbaged
26914 windows and frames; in the latter case, the frame or window may
26915 be in the midst of changing its size, and x and y may be off the
26916 window. */
26917 if (! FRAME_VISIBLE_P (f)
26918 || FRAME_GARBAGED_P (f)
26919 || vpos >= w->current_matrix->nrows
26920 || hpos >= w->current_matrix->matrix_w)
26921 return;
26922
26923 /* If cursor is off and we want it off, return quickly. */
26924 if (!on && !w->phys_cursor_on_p)
26925 return;
26926
26927 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26928 /* If cursor row is not enabled, we don't really know where to
26929 display the cursor. */
26930 if (!glyph_row->enabled_p)
26931 {
26932 w->phys_cursor_on_p = 0;
26933 return;
26934 }
26935
26936 glyph = NULL;
26937 if (!glyph_row->exact_window_width_line_p
26938 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26939 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26940
26941 eassert (input_blocked_p ());
26942
26943 /* Set new_cursor_type to the cursor we want to be displayed. */
26944 new_cursor_type = get_window_cursor_type (w, glyph,
26945 &new_cursor_width, &active_cursor);
26946
26947 /* If cursor is currently being shown and we don't want it to be or
26948 it is in the wrong place, or the cursor type is not what we want,
26949 erase it. */
26950 if (w->phys_cursor_on_p
26951 && (!on
26952 || w->phys_cursor.x != x
26953 || w->phys_cursor.y != y
26954 || new_cursor_type != w->phys_cursor_type
26955 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26956 && new_cursor_width != w->phys_cursor_width)))
26957 erase_phys_cursor (w);
26958
26959 /* Don't check phys_cursor_on_p here because that flag is only set
26960 to zero in some cases where we know that the cursor has been
26961 completely erased, to avoid the extra work of erasing the cursor
26962 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26963 still not be visible, or it has only been partly erased. */
26964 if (on)
26965 {
26966 w->phys_cursor_ascent = glyph_row->ascent;
26967 w->phys_cursor_height = glyph_row->height;
26968
26969 /* Set phys_cursor_.* before x_draw_.* is called because some
26970 of them may need the information. */
26971 w->phys_cursor.x = x;
26972 w->phys_cursor.y = glyph_row->y;
26973 w->phys_cursor.hpos = hpos;
26974 w->phys_cursor.vpos = vpos;
26975 }
26976
26977 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26978 new_cursor_type, new_cursor_width,
26979 on, active_cursor);
26980 }
26981
26982
26983 /* Switch the display of W's cursor on or off, according to the value
26984 of ON. */
26985
26986 static void
26987 update_window_cursor (struct window *w, bool on)
26988 {
26989 /* Don't update cursor in windows whose frame is in the process
26990 of being deleted. */
26991 if (w->current_matrix)
26992 {
26993 int hpos = w->phys_cursor.hpos;
26994 int vpos = w->phys_cursor.vpos;
26995 struct glyph_row *row;
26996
26997 if (vpos >= w->current_matrix->nrows
26998 || hpos >= w->current_matrix->matrix_w)
26999 return;
27000
27001 row = MATRIX_ROW (w->current_matrix, vpos);
27002
27003 /* When the window is hscrolled, cursor hpos can legitimately be
27004 out of bounds, but we draw the cursor at the corresponding
27005 window margin in that case. */
27006 if (!row->reversed_p && hpos < 0)
27007 hpos = 0;
27008 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27009 hpos = row->used[TEXT_AREA] - 1;
27010
27011 block_input ();
27012 display_and_set_cursor (w, on, hpos, vpos,
27013 w->phys_cursor.x, w->phys_cursor.y);
27014 unblock_input ();
27015 }
27016 }
27017
27018
27019 /* Call update_window_cursor with parameter ON_P on all leaf windows
27020 in the window tree rooted at W. */
27021
27022 static void
27023 update_cursor_in_window_tree (struct window *w, bool on_p)
27024 {
27025 while (w)
27026 {
27027 if (WINDOWP (w->contents))
27028 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27029 else
27030 update_window_cursor (w, on_p);
27031
27032 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27033 }
27034 }
27035
27036
27037 /* EXPORT:
27038 Display the cursor on window W, or clear it, according to ON_P.
27039 Don't change the cursor's position. */
27040
27041 void
27042 x_update_cursor (struct frame *f, bool on_p)
27043 {
27044 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27045 }
27046
27047
27048 /* EXPORT:
27049 Clear the cursor of window W to background color, and mark the
27050 cursor as not shown. This is used when the text where the cursor
27051 is about to be rewritten. */
27052
27053 void
27054 x_clear_cursor (struct window *w)
27055 {
27056 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27057 update_window_cursor (w, 0);
27058 }
27059
27060 #endif /* HAVE_WINDOW_SYSTEM */
27061
27062 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27063 and MSDOS. */
27064 static void
27065 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27066 int start_hpos, int end_hpos,
27067 enum draw_glyphs_face draw)
27068 {
27069 #ifdef HAVE_WINDOW_SYSTEM
27070 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27071 {
27072 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27073 return;
27074 }
27075 #endif
27076 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27077 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27078 #endif
27079 }
27080
27081 /* Display the active region described by mouse_face_* according to DRAW. */
27082
27083 static void
27084 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27085 {
27086 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27087 struct frame *f = XFRAME (WINDOW_FRAME (w));
27088
27089 if (/* If window is in the process of being destroyed, don't bother
27090 to do anything. */
27091 w->current_matrix != NULL
27092 /* Don't update mouse highlight if hidden */
27093 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27094 /* Recognize when we are called to operate on rows that don't exist
27095 anymore. This can happen when a window is split. */
27096 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27097 {
27098 int phys_cursor_on_p = w->phys_cursor_on_p;
27099 struct glyph_row *row, *first, *last;
27100
27101 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27102 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27103
27104 for (row = first; row <= last && row->enabled_p; ++row)
27105 {
27106 int start_hpos, end_hpos, start_x;
27107
27108 /* For all but the first row, the highlight starts at column 0. */
27109 if (row == first)
27110 {
27111 /* R2L rows have BEG and END in reversed order, but the
27112 screen drawing geometry is always left to right. So
27113 we need to mirror the beginning and end of the
27114 highlighted area in R2L rows. */
27115 if (!row->reversed_p)
27116 {
27117 start_hpos = hlinfo->mouse_face_beg_col;
27118 start_x = hlinfo->mouse_face_beg_x;
27119 }
27120 else if (row == last)
27121 {
27122 start_hpos = hlinfo->mouse_face_end_col;
27123 start_x = hlinfo->mouse_face_end_x;
27124 }
27125 else
27126 {
27127 start_hpos = 0;
27128 start_x = 0;
27129 }
27130 }
27131 else if (row->reversed_p && row == last)
27132 {
27133 start_hpos = hlinfo->mouse_face_end_col;
27134 start_x = hlinfo->mouse_face_end_x;
27135 }
27136 else
27137 {
27138 start_hpos = 0;
27139 start_x = 0;
27140 }
27141
27142 if (row == last)
27143 {
27144 if (!row->reversed_p)
27145 end_hpos = hlinfo->mouse_face_end_col;
27146 else if (row == first)
27147 end_hpos = hlinfo->mouse_face_beg_col;
27148 else
27149 {
27150 end_hpos = row->used[TEXT_AREA];
27151 if (draw == DRAW_NORMAL_TEXT)
27152 row->fill_line_p = 1; /* Clear to end of line */
27153 }
27154 }
27155 else if (row->reversed_p && row == first)
27156 end_hpos = hlinfo->mouse_face_beg_col;
27157 else
27158 {
27159 end_hpos = row->used[TEXT_AREA];
27160 if (draw == DRAW_NORMAL_TEXT)
27161 row->fill_line_p = 1; /* Clear to end of line */
27162 }
27163
27164 if (end_hpos > start_hpos)
27165 {
27166 draw_row_with_mouse_face (w, start_x, row,
27167 start_hpos, end_hpos, draw);
27168
27169 row->mouse_face_p
27170 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27171 }
27172 }
27173
27174 #ifdef HAVE_WINDOW_SYSTEM
27175 /* When we've written over the cursor, arrange for it to
27176 be displayed again. */
27177 if (FRAME_WINDOW_P (f)
27178 && phys_cursor_on_p && !w->phys_cursor_on_p)
27179 {
27180 int hpos = w->phys_cursor.hpos;
27181
27182 /* When the window is hscrolled, cursor hpos can legitimately be
27183 out of bounds, but we draw the cursor at the corresponding
27184 window margin in that case. */
27185 if (!row->reversed_p && hpos < 0)
27186 hpos = 0;
27187 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27188 hpos = row->used[TEXT_AREA] - 1;
27189
27190 block_input ();
27191 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27192 w->phys_cursor.x, w->phys_cursor.y);
27193 unblock_input ();
27194 }
27195 #endif /* HAVE_WINDOW_SYSTEM */
27196 }
27197
27198 #ifdef HAVE_WINDOW_SYSTEM
27199 /* Change the mouse cursor. */
27200 if (FRAME_WINDOW_P (f))
27201 {
27202 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27203 if (draw == DRAW_NORMAL_TEXT
27204 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27205 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27206 else
27207 #endif
27208 if (draw == DRAW_MOUSE_FACE)
27209 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27210 else
27211 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27212 }
27213 #endif /* HAVE_WINDOW_SYSTEM */
27214 }
27215
27216 /* EXPORT:
27217 Clear out the mouse-highlighted active region.
27218 Redraw it un-highlighted first. Value is non-zero if mouse
27219 face was actually drawn unhighlighted. */
27220
27221 int
27222 clear_mouse_face (Mouse_HLInfo *hlinfo)
27223 {
27224 int cleared = 0;
27225
27226 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27227 {
27228 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27229 cleared = 1;
27230 }
27231
27232 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27233 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27234 hlinfo->mouse_face_window = Qnil;
27235 hlinfo->mouse_face_overlay = Qnil;
27236 return cleared;
27237 }
27238
27239 /* Return true if the coordinates HPOS and VPOS on windows W are
27240 within the mouse face on that window. */
27241 static bool
27242 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27243 {
27244 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27245
27246 /* Quickly resolve the easy cases. */
27247 if (!(WINDOWP (hlinfo->mouse_face_window)
27248 && XWINDOW (hlinfo->mouse_face_window) == w))
27249 return false;
27250 if (vpos < hlinfo->mouse_face_beg_row
27251 || vpos > hlinfo->mouse_face_end_row)
27252 return false;
27253 if (vpos > hlinfo->mouse_face_beg_row
27254 && vpos < hlinfo->mouse_face_end_row)
27255 return true;
27256
27257 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27258 {
27259 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27260 {
27261 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27262 return true;
27263 }
27264 else if ((vpos == hlinfo->mouse_face_beg_row
27265 && hpos >= hlinfo->mouse_face_beg_col)
27266 || (vpos == hlinfo->mouse_face_end_row
27267 && hpos < hlinfo->mouse_face_end_col))
27268 return true;
27269 }
27270 else
27271 {
27272 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27273 {
27274 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27275 return true;
27276 }
27277 else if ((vpos == hlinfo->mouse_face_beg_row
27278 && hpos <= hlinfo->mouse_face_beg_col)
27279 || (vpos == hlinfo->mouse_face_end_row
27280 && hpos > hlinfo->mouse_face_end_col))
27281 return true;
27282 }
27283 return false;
27284 }
27285
27286
27287 /* EXPORT:
27288 True if physical cursor of window W is within mouse face. */
27289
27290 bool
27291 cursor_in_mouse_face_p (struct window *w)
27292 {
27293 int hpos = w->phys_cursor.hpos;
27294 int vpos = w->phys_cursor.vpos;
27295 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27296
27297 /* When the window is hscrolled, cursor hpos can legitimately be out
27298 of bounds, but we draw the cursor at the corresponding window
27299 margin in that case. */
27300 if (!row->reversed_p && hpos < 0)
27301 hpos = 0;
27302 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27303 hpos = row->used[TEXT_AREA] - 1;
27304
27305 return coords_in_mouse_face_p (w, hpos, vpos);
27306 }
27307
27308
27309 \f
27310 /* Find the glyph rows START_ROW and END_ROW of window W that display
27311 characters between buffer positions START_CHARPOS and END_CHARPOS
27312 (excluding END_CHARPOS). DISP_STRING is a display string that
27313 covers these buffer positions. This is similar to
27314 row_containing_pos, but is more accurate when bidi reordering makes
27315 buffer positions change non-linearly with glyph rows. */
27316 static void
27317 rows_from_pos_range (struct window *w,
27318 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27319 Lisp_Object disp_string,
27320 struct glyph_row **start, struct glyph_row **end)
27321 {
27322 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27323 int last_y = window_text_bottom_y (w);
27324 struct glyph_row *row;
27325
27326 *start = NULL;
27327 *end = NULL;
27328
27329 while (!first->enabled_p
27330 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27331 first++;
27332
27333 /* Find the START row. */
27334 for (row = first;
27335 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27336 row++)
27337 {
27338 /* A row can potentially be the START row if the range of the
27339 characters it displays intersects the range
27340 [START_CHARPOS..END_CHARPOS). */
27341 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27342 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27343 /* See the commentary in row_containing_pos, for the
27344 explanation of the complicated way to check whether
27345 some position is beyond the end of the characters
27346 displayed by a row. */
27347 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27348 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27349 && !row->ends_at_zv_p
27350 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27351 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27352 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27353 && !row->ends_at_zv_p
27354 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27355 {
27356 /* Found a candidate row. Now make sure at least one of the
27357 glyphs it displays has a charpos from the range
27358 [START_CHARPOS..END_CHARPOS).
27359
27360 This is not obvious because bidi reordering could make
27361 buffer positions of a row be 1,2,3,102,101,100, and if we
27362 want to highlight characters in [50..60), we don't want
27363 this row, even though [50..60) does intersect [1..103),
27364 the range of character positions given by the row's start
27365 and end positions. */
27366 struct glyph *g = row->glyphs[TEXT_AREA];
27367 struct glyph *e = g + row->used[TEXT_AREA];
27368
27369 while (g < e)
27370 {
27371 if (((BUFFERP (g->object) || INTEGERP (g->object))
27372 && start_charpos <= g->charpos && g->charpos < end_charpos)
27373 /* A glyph that comes from DISP_STRING is by
27374 definition to be highlighted. */
27375 || EQ (g->object, disp_string))
27376 *start = row;
27377 g++;
27378 }
27379 if (*start)
27380 break;
27381 }
27382 }
27383
27384 /* Find the END row. */
27385 if (!*start
27386 /* If the last row is partially visible, start looking for END
27387 from that row, instead of starting from FIRST. */
27388 && !(row->enabled_p
27389 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27390 row = first;
27391 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27392 {
27393 struct glyph_row *next = row + 1;
27394 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27395
27396 if (!next->enabled_p
27397 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27398 /* The first row >= START whose range of displayed characters
27399 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27400 is the row END + 1. */
27401 || (start_charpos < next_start
27402 && end_charpos < next_start)
27403 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27404 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27405 && !next->ends_at_zv_p
27406 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27407 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27408 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27409 && !next->ends_at_zv_p
27410 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27411 {
27412 *end = row;
27413 break;
27414 }
27415 else
27416 {
27417 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27418 but none of the characters it displays are in the range, it is
27419 also END + 1. */
27420 struct glyph *g = next->glyphs[TEXT_AREA];
27421 struct glyph *s = g;
27422 struct glyph *e = g + next->used[TEXT_AREA];
27423
27424 while (g < e)
27425 {
27426 if (((BUFFERP (g->object) || INTEGERP (g->object))
27427 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27428 /* If the buffer position of the first glyph in
27429 the row is equal to END_CHARPOS, it means
27430 the last character to be highlighted is the
27431 newline of ROW, and we must consider NEXT as
27432 END, not END+1. */
27433 || (((!next->reversed_p && g == s)
27434 || (next->reversed_p && g == e - 1))
27435 && (g->charpos == end_charpos
27436 /* Special case for when NEXT is an
27437 empty line at ZV. */
27438 || (g->charpos == -1
27439 && !row->ends_at_zv_p
27440 && next_start == end_charpos)))))
27441 /* A glyph that comes from DISP_STRING is by
27442 definition to be highlighted. */
27443 || EQ (g->object, disp_string))
27444 break;
27445 g++;
27446 }
27447 if (g == e)
27448 {
27449 *end = row;
27450 break;
27451 }
27452 /* The first row that ends at ZV must be the last to be
27453 highlighted. */
27454 else if (next->ends_at_zv_p)
27455 {
27456 *end = next;
27457 break;
27458 }
27459 }
27460 }
27461 }
27462
27463 /* This function sets the mouse_face_* elements of HLINFO, assuming
27464 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27465 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27466 for the overlay or run of text properties specifying the mouse
27467 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27468 before-string and after-string that must also be highlighted.
27469 DISP_STRING, if non-nil, is a display string that may cover some
27470 or all of the highlighted text. */
27471
27472 static void
27473 mouse_face_from_buffer_pos (Lisp_Object window,
27474 Mouse_HLInfo *hlinfo,
27475 ptrdiff_t mouse_charpos,
27476 ptrdiff_t start_charpos,
27477 ptrdiff_t end_charpos,
27478 Lisp_Object before_string,
27479 Lisp_Object after_string,
27480 Lisp_Object disp_string)
27481 {
27482 struct window *w = XWINDOW (window);
27483 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27484 struct glyph_row *r1, *r2;
27485 struct glyph *glyph, *end;
27486 ptrdiff_t ignore, pos;
27487 int x;
27488
27489 eassert (NILP (disp_string) || STRINGP (disp_string));
27490 eassert (NILP (before_string) || STRINGP (before_string));
27491 eassert (NILP (after_string) || STRINGP (after_string));
27492
27493 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27494 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27495 if (r1 == NULL)
27496 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27497 /* If the before-string or display-string contains newlines,
27498 rows_from_pos_range skips to its last row. Move back. */
27499 if (!NILP (before_string) || !NILP (disp_string))
27500 {
27501 struct glyph_row *prev;
27502 while ((prev = r1 - 1, prev >= first)
27503 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27504 && prev->used[TEXT_AREA] > 0)
27505 {
27506 struct glyph *beg = prev->glyphs[TEXT_AREA];
27507 glyph = beg + prev->used[TEXT_AREA];
27508 while (--glyph >= beg && INTEGERP (glyph->object));
27509 if (glyph < beg
27510 || !(EQ (glyph->object, before_string)
27511 || EQ (glyph->object, disp_string)))
27512 break;
27513 r1 = prev;
27514 }
27515 }
27516 if (r2 == NULL)
27517 {
27518 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27519 hlinfo->mouse_face_past_end = 1;
27520 }
27521 else if (!NILP (after_string))
27522 {
27523 /* If the after-string has newlines, advance to its last row. */
27524 struct glyph_row *next;
27525 struct glyph_row *last
27526 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27527
27528 for (next = r2 + 1;
27529 next <= last
27530 && next->used[TEXT_AREA] > 0
27531 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27532 ++next)
27533 r2 = next;
27534 }
27535 /* The rest of the display engine assumes that mouse_face_beg_row is
27536 either above mouse_face_end_row or identical to it. But with
27537 bidi-reordered continued lines, the row for START_CHARPOS could
27538 be below the row for END_CHARPOS. If so, swap the rows and store
27539 them in correct order. */
27540 if (r1->y > r2->y)
27541 {
27542 struct glyph_row *tem = r2;
27543
27544 r2 = r1;
27545 r1 = tem;
27546 }
27547
27548 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27549 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27550
27551 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27552 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27553 could be anywhere in the row and in any order. The strategy
27554 below is to find the leftmost and the rightmost glyph that
27555 belongs to either of these 3 strings, or whose position is
27556 between START_CHARPOS and END_CHARPOS, and highlight all the
27557 glyphs between those two. This may cover more than just the text
27558 between START_CHARPOS and END_CHARPOS if the range of characters
27559 strides the bidi level boundary, e.g. if the beginning is in R2L
27560 text while the end is in L2R text or vice versa. */
27561 if (!r1->reversed_p)
27562 {
27563 /* This row is in a left to right paragraph. Scan it left to
27564 right. */
27565 glyph = r1->glyphs[TEXT_AREA];
27566 end = glyph + r1->used[TEXT_AREA];
27567 x = r1->x;
27568
27569 /* Skip truncation glyphs at the start of the glyph row. */
27570 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27571 for (; glyph < end
27572 && INTEGERP (glyph->object)
27573 && glyph->charpos < 0;
27574 ++glyph)
27575 x += glyph->pixel_width;
27576
27577 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27578 or DISP_STRING, and the first glyph from buffer whose
27579 position is between START_CHARPOS and END_CHARPOS. */
27580 for (; glyph < end
27581 && !INTEGERP (glyph->object)
27582 && !EQ (glyph->object, disp_string)
27583 && !(BUFFERP (glyph->object)
27584 && (glyph->charpos >= start_charpos
27585 && glyph->charpos < end_charpos));
27586 ++glyph)
27587 {
27588 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27589 are present at buffer positions between START_CHARPOS and
27590 END_CHARPOS, or if they come from an overlay. */
27591 if (EQ (glyph->object, before_string))
27592 {
27593 pos = string_buffer_position (before_string,
27594 start_charpos);
27595 /* If pos == 0, it means before_string came from an
27596 overlay, not from a buffer position. */
27597 if (!pos || (pos >= start_charpos && pos < end_charpos))
27598 break;
27599 }
27600 else if (EQ (glyph->object, after_string))
27601 {
27602 pos = string_buffer_position (after_string, end_charpos);
27603 if (!pos || (pos >= start_charpos && pos < end_charpos))
27604 break;
27605 }
27606 x += glyph->pixel_width;
27607 }
27608 hlinfo->mouse_face_beg_x = x;
27609 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27610 }
27611 else
27612 {
27613 /* This row is in a right to left paragraph. Scan it right to
27614 left. */
27615 struct glyph *g;
27616
27617 end = r1->glyphs[TEXT_AREA] - 1;
27618 glyph = end + r1->used[TEXT_AREA];
27619
27620 /* Skip truncation glyphs at the start of the glyph row. */
27621 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27622 for (; glyph > end
27623 && INTEGERP (glyph->object)
27624 && glyph->charpos < 0;
27625 --glyph)
27626 ;
27627
27628 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27629 or DISP_STRING, and the first glyph from buffer whose
27630 position is between START_CHARPOS and END_CHARPOS. */
27631 for (; glyph > end
27632 && !INTEGERP (glyph->object)
27633 && !EQ (glyph->object, disp_string)
27634 && !(BUFFERP (glyph->object)
27635 && (glyph->charpos >= start_charpos
27636 && glyph->charpos < end_charpos));
27637 --glyph)
27638 {
27639 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27640 are present at buffer positions between START_CHARPOS and
27641 END_CHARPOS, or if they come from an overlay. */
27642 if (EQ (glyph->object, before_string))
27643 {
27644 pos = string_buffer_position (before_string, start_charpos);
27645 /* If pos == 0, it means before_string came from an
27646 overlay, not from a buffer position. */
27647 if (!pos || (pos >= start_charpos && pos < end_charpos))
27648 break;
27649 }
27650 else if (EQ (glyph->object, after_string))
27651 {
27652 pos = string_buffer_position (after_string, end_charpos);
27653 if (!pos || (pos >= start_charpos && pos < end_charpos))
27654 break;
27655 }
27656 }
27657
27658 glyph++; /* first glyph to the right of the highlighted area */
27659 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27660 x += g->pixel_width;
27661 hlinfo->mouse_face_beg_x = x;
27662 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27663 }
27664
27665 /* If the highlight ends in a different row, compute GLYPH and END
27666 for the end row. Otherwise, reuse the values computed above for
27667 the row where the highlight begins. */
27668 if (r2 != r1)
27669 {
27670 if (!r2->reversed_p)
27671 {
27672 glyph = r2->glyphs[TEXT_AREA];
27673 end = glyph + r2->used[TEXT_AREA];
27674 x = r2->x;
27675 }
27676 else
27677 {
27678 end = r2->glyphs[TEXT_AREA] - 1;
27679 glyph = end + r2->used[TEXT_AREA];
27680 }
27681 }
27682
27683 if (!r2->reversed_p)
27684 {
27685 /* Skip truncation and continuation glyphs near the end of the
27686 row, and also blanks and stretch glyphs inserted by
27687 extend_face_to_end_of_line. */
27688 while (end > glyph
27689 && INTEGERP ((end - 1)->object))
27690 --end;
27691 /* Scan the rest of the glyph row from the end, looking for the
27692 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27693 DISP_STRING, or whose position is between START_CHARPOS
27694 and END_CHARPOS */
27695 for (--end;
27696 end > glyph
27697 && !INTEGERP (end->object)
27698 && !EQ (end->object, disp_string)
27699 && !(BUFFERP (end->object)
27700 && (end->charpos >= start_charpos
27701 && end->charpos < end_charpos));
27702 --end)
27703 {
27704 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27705 are present at buffer positions between START_CHARPOS and
27706 END_CHARPOS, or if they come from an overlay. */
27707 if (EQ (end->object, before_string))
27708 {
27709 pos = string_buffer_position (before_string, start_charpos);
27710 if (!pos || (pos >= start_charpos && pos < end_charpos))
27711 break;
27712 }
27713 else if (EQ (end->object, after_string))
27714 {
27715 pos = string_buffer_position (after_string, end_charpos);
27716 if (!pos || (pos >= start_charpos && pos < end_charpos))
27717 break;
27718 }
27719 }
27720 /* Find the X coordinate of the last glyph to be highlighted. */
27721 for (; glyph <= end; ++glyph)
27722 x += glyph->pixel_width;
27723
27724 hlinfo->mouse_face_end_x = x;
27725 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27726 }
27727 else
27728 {
27729 /* Skip truncation and continuation glyphs near the end of the
27730 row, and also blanks and stretch glyphs inserted by
27731 extend_face_to_end_of_line. */
27732 x = r2->x;
27733 end++;
27734 while (end < glyph
27735 && INTEGERP (end->object))
27736 {
27737 x += end->pixel_width;
27738 ++end;
27739 }
27740 /* Scan the rest of the glyph row from the end, looking for the
27741 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27742 DISP_STRING, or whose position is between START_CHARPOS
27743 and END_CHARPOS */
27744 for ( ;
27745 end < glyph
27746 && !INTEGERP (end->object)
27747 && !EQ (end->object, disp_string)
27748 && !(BUFFERP (end->object)
27749 && (end->charpos >= start_charpos
27750 && end->charpos < end_charpos));
27751 ++end)
27752 {
27753 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27754 are present at buffer positions between START_CHARPOS and
27755 END_CHARPOS, or if they come from an overlay. */
27756 if (EQ (end->object, before_string))
27757 {
27758 pos = string_buffer_position (before_string, start_charpos);
27759 if (!pos || (pos >= start_charpos && pos < end_charpos))
27760 break;
27761 }
27762 else if (EQ (end->object, after_string))
27763 {
27764 pos = string_buffer_position (after_string, end_charpos);
27765 if (!pos || (pos >= start_charpos && pos < end_charpos))
27766 break;
27767 }
27768 x += end->pixel_width;
27769 }
27770 /* If we exited the above loop because we arrived at the last
27771 glyph of the row, and its buffer position is still not in
27772 range, it means the last character in range is the preceding
27773 newline. Bump the end column and x values to get past the
27774 last glyph. */
27775 if (end == glyph
27776 && BUFFERP (end->object)
27777 && (end->charpos < start_charpos
27778 || end->charpos >= end_charpos))
27779 {
27780 x += end->pixel_width;
27781 ++end;
27782 }
27783 hlinfo->mouse_face_end_x = x;
27784 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27785 }
27786
27787 hlinfo->mouse_face_window = window;
27788 hlinfo->mouse_face_face_id
27789 = face_at_buffer_position (w, mouse_charpos, &ignore,
27790 mouse_charpos + 1,
27791 !hlinfo->mouse_face_hidden, -1);
27792 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27793 }
27794
27795 /* The following function is not used anymore (replaced with
27796 mouse_face_from_string_pos), but I leave it here for the time
27797 being, in case someone would. */
27798
27799 #if 0 /* not used */
27800
27801 /* Find the position of the glyph for position POS in OBJECT in
27802 window W's current matrix, and return in *X, *Y the pixel
27803 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27804
27805 RIGHT_P non-zero means return the position of the right edge of the
27806 glyph, RIGHT_P zero means return the left edge position.
27807
27808 If no glyph for POS exists in the matrix, return the position of
27809 the glyph with the next smaller position that is in the matrix, if
27810 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27811 exists in the matrix, return the position of the glyph with the
27812 next larger position in OBJECT.
27813
27814 Value is non-zero if a glyph was found. */
27815
27816 static int
27817 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27818 int *hpos, int *vpos, int *x, int *y, int right_p)
27819 {
27820 int yb = window_text_bottom_y (w);
27821 struct glyph_row *r;
27822 struct glyph *best_glyph = NULL;
27823 struct glyph_row *best_row = NULL;
27824 int best_x = 0;
27825
27826 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27827 r->enabled_p && r->y < yb;
27828 ++r)
27829 {
27830 struct glyph *g = r->glyphs[TEXT_AREA];
27831 struct glyph *e = g + r->used[TEXT_AREA];
27832 int gx;
27833
27834 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27835 if (EQ (g->object, object))
27836 {
27837 if (g->charpos == pos)
27838 {
27839 best_glyph = g;
27840 best_x = gx;
27841 best_row = r;
27842 goto found;
27843 }
27844 else if (best_glyph == NULL
27845 || ((eabs (g->charpos - pos)
27846 < eabs (best_glyph->charpos - pos))
27847 && (right_p
27848 ? g->charpos < pos
27849 : g->charpos > pos)))
27850 {
27851 best_glyph = g;
27852 best_x = gx;
27853 best_row = r;
27854 }
27855 }
27856 }
27857
27858 found:
27859
27860 if (best_glyph)
27861 {
27862 *x = best_x;
27863 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27864
27865 if (right_p)
27866 {
27867 *x += best_glyph->pixel_width;
27868 ++*hpos;
27869 }
27870
27871 *y = best_row->y;
27872 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27873 }
27874
27875 return best_glyph != NULL;
27876 }
27877 #endif /* not used */
27878
27879 /* Find the positions of the first and the last glyphs in window W's
27880 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27881 (assumed to be a string), and return in HLINFO's mouse_face_*
27882 members the pixel and column/row coordinates of those glyphs. */
27883
27884 static void
27885 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27886 Lisp_Object object,
27887 ptrdiff_t startpos, ptrdiff_t endpos)
27888 {
27889 int yb = window_text_bottom_y (w);
27890 struct glyph_row *r;
27891 struct glyph *g, *e;
27892 int gx;
27893 int found = 0;
27894
27895 /* Find the glyph row with at least one position in the range
27896 [STARTPOS..ENDPOS), and the first glyph in that row whose
27897 position belongs to that range. */
27898 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27899 r->enabled_p && r->y < yb;
27900 ++r)
27901 {
27902 if (!r->reversed_p)
27903 {
27904 g = r->glyphs[TEXT_AREA];
27905 e = g + r->used[TEXT_AREA];
27906 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27907 if (EQ (g->object, object)
27908 && startpos <= g->charpos && g->charpos < endpos)
27909 {
27910 hlinfo->mouse_face_beg_row
27911 = MATRIX_ROW_VPOS (r, w->current_matrix);
27912 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27913 hlinfo->mouse_face_beg_x = gx;
27914 found = 1;
27915 break;
27916 }
27917 }
27918 else
27919 {
27920 struct glyph *g1;
27921
27922 e = r->glyphs[TEXT_AREA];
27923 g = e + r->used[TEXT_AREA];
27924 for ( ; g > e; --g)
27925 if (EQ ((g-1)->object, object)
27926 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27927 {
27928 hlinfo->mouse_face_beg_row
27929 = MATRIX_ROW_VPOS (r, w->current_matrix);
27930 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27931 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27932 gx += g1->pixel_width;
27933 hlinfo->mouse_face_beg_x = gx;
27934 found = 1;
27935 break;
27936 }
27937 }
27938 if (found)
27939 break;
27940 }
27941
27942 if (!found)
27943 return;
27944
27945 /* Starting with the next row, look for the first row which does NOT
27946 include any glyphs whose positions are in the range. */
27947 for (++r; r->enabled_p && r->y < yb; ++r)
27948 {
27949 g = r->glyphs[TEXT_AREA];
27950 e = g + r->used[TEXT_AREA];
27951 found = 0;
27952 for ( ; g < e; ++g)
27953 if (EQ (g->object, object)
27954 && startpos <= g->charpos && g->charpos < endpos)
27955 {
27956 found = 1;
27957 break;
27958 }
27959 if (!found)
27960 break;
27961 }
27962
27963 /* The highlighted region ends on the previous row. */
27964 r--;
27965
27966 /* Set the end row. */
27967 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27968
27969 /* Compute and set the end column and the end column's horizontal
27970 pixel coordinate. */
27971 if (!r->reversed_p)
27972 {
27973 g = r->glyphs[TEXT_AREA];
27974 e = g + r->used[TEXT_AREA];
27975 for ( ; e > g; --e)
27976 if (EQ ((e-1)->object, object)
27977 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27978 break;
27979 hlinfo->mouse_face_end_col = e - g;
27980
27981 for (gx = r->x; g < e; ++g)
27982 gx += g->pixel_width;
27983 hlinfo->mouse_face_end_x = gx;
27984 }
27985 else
27986 {
27987 e = r->glyphs[TEXT_AREA];
27988 g = e + r->used[TEXT_AREA];
27989 for (gx = r->x ; e < g; ++e)
27990 {
27991 if (EQ (e->object, object)
27992 && startpos <= e->charpos && e->charpos < endpos)
27993 break;
27994 gx += e->pixel_width;
27995 }
27996 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27997 hlinfo->mouse_face_end_x = gx;
27998 }
27999 }
28000
28001 #ifdef HAVE_WINDOW_SYSTEM
28002
28003 /* See if position X, Y is within a hot-spot of an image. */
28004
28005 static int
28006 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28007 {
28008 if (!CONSP (hot_spot))
28009 return 0;
28010
28011 if (EQ (XCAR (hot_spot), Qrect))
28012 {
28013 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28014 Lisp_Object rect = XCDR (hot_spot);
28015 Lisp_Object tem;
28016 if (!CONSP (rect))
28017 return 0;
28018 if (!CONSP (XCAR (rect)))
28019 return 0;
28020 if (!CONSP (XCDR (rect)))
28021 return 0;
28022 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28023 return 0;
28024 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28025 return 0;
28026 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28027 return 0;
28028 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28029 return 0;
28030 return 1;
28031 }
28032 else if (EQ (XCAR (hot_spot), Qcircle))
28033 {
28034 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28035 Lisp_Object circ = XCDR (hot_spot);
28036 Lisp_Object lr, lx0, ly0;
28037 if (CONSP (circ)
28038 && CONSP (XCAR (circ))
28039 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28040 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28041 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28042 {
28043 double r = XFLOATINT (lr);
28044 double dx = XINT (lx0) - x;
28045 double dy = XINT (ly0) - y;
28046 return (dx * dx + dy * dy <= r * r);
28047 }
28048 }
28049 else if (EQ (XCAR (hot_spot), Qpoly))
28050 {
28051 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28052 if (VECTORP (XCDR (hot_spot)))
28053 {
28054 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28055 Lisp_Object *poly = v->contents;
28056 ptrdiff_t n = v->header.size;
28057 ptrdiff_t i;
28058 int inside = 0;
28059 Lisp_Object lx, ly;
28060 int x0, y0;
28061
28062 /* Need an even number of coordinates, and at least 3 edges. */
28063 if (n < 6 || n & 1)
28064 return 0;
28065
28066 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28067 If count is odd, we are inside polygon. Pixels on edges
28068 may or may not be included depending on actual geometry of the
28069 polygon. */
28070 if ((lx = poly[n-2], !INTEGERP (lx))
28071 || (ly = poly[n-1], !INTEGERP (lx)))
28072 return 0;
28073 x0 = XINT (lx), y0 = XINT (ly);
28074 for (i = 0; i < n; i += 2)
28075 {
28076 int x1 = x0, y1 = y0;
28077 if ((lx = poly[i], !INTEGERP (lx))
28078 || (ly = poly[i+1], !INTEGERP (ly)))
28079 return 0;
28080 x0 = XINT (lx), y0 = XINT (ly);
28081
28082 /* Does this segment cross the X line? */
28083 if (x0 >= x)
28084 {
28085 if (x1 >= x)
28086 continue;
28087 }
28088 else if (x1 < x)
28089 continue;
28090 if (y > y0 && y > y1)
28091 continue;
28092 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28093 inside = !inside;
28094 }
28095 return inside;
28096 }
28097 }
28098 return 0;
28099 }
28100
28101 Lisp_Object
28102 find_hot_spot (Lisp_Object map, int x, int y)
28103 {
28104 while (CONSP (map))
28105 {
28106 if (CONSP (XCAR (map))
28107 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28108 return XCAR (map);
28109 map = XCDR (map);
28110 }
28111
28112 return Qnil;
28113 }
28114
28115 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28116 3, 3, 0,
28117 doc: /* Lookup in image map MAP coordinates X and Y.
28118 An image map is an alist where each element has the format (AREA ID PLIST).
28119 An AREA is specified as either a rectangle, a circle, or a polygon:
28120 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28121 pixel coordinates of the upper left and bottom right corners.
28122 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28123 and the radius of the circle; r may be a float or integer.
28124 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28125 vector describes one corner in the polygon.
28126 Returns the alist element for the first matching AREA in MAP. */)
28127 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28128 {
28129 if (NILP (map))
28130 return Qnil;
28131
28132 CHECK_NUMBER (x);
28133 CHECK_NUMBER (y);
28134
28135 return find_hot_spot (map,
28136 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28137 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28138 }
28139
28140
28141 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28142 static void
28143 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28144 {
28145 /* Do not change cursor shape while dragging mouse. */
28146 if (!NILP (do_mouse_tracking))
28147 return;
28148
28149 if (!NILP (pointer))
28150 {
28151 if (EQ (pointer, Qarrow))
28152 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28153 else if (EQ (pointer, Qhand))
28154 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28155 else if (EQ (pointer, Qtext))
28156 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28157 else if (EQ (pointer, intern ("hdrag")))
28158 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28159 else if (EQ (pointer, intern ("nhdrag")))
28160 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28161 #ifdef HAVE_X_WINDOWS
28162 else if (EQ (pointer, intern ("vdrag")))
28163 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28164 #endif
28165 else if (EQ (pointer, intern ("hourglass")))
28166 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28167 else if (EQ (pointer, Qmodeline))
28168 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28169 else
28170 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28171 }
28172
28173 if (cursor != No_Cursor)
28174 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28175 }
28176
28177 #endif /* HAVE_WINDOW_SYSTEM */
28178
28179 /* Take proper action when mouse has moved to the mode or header line
28180 or marginal area AREA of window W, x-position X and y-position Y.
28181 X is relative to the start of the text display area of W, so the
28182 width of bitmap areas and scroll bars must be subtracted to get a
28183 position relative to the start of the mode line. */
28184
28185 static void
28186 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28187 enum window_part area)
28188 {
28189 struct window *w = XWINDOW (window);
28190 struct frame *f = XFRAME (w->frame);
28191 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28192 #ifdef HAVE_WINDOW_SYSTEM
28193 Display_Info *dpyinfo;
28194 #endif
28195 Cursor cursor = No_Cursor;
28196 Lisp_Object pointer = Qnil;
28197 int dx, dy, width, height;
28198 ptrdiff_t charpos;
28199 Lisp_Object string, object = Qnil;
28200 Lisp_Object pos IF_LINT (= Qnil), help;
28201
28202 Lisp_Object mouse_face;
28203 int original_x_pixel = x;
28204 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28205 struct glyph_row *row IF_LINT (= 0);
28206
28207 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28208 {
28209 int x0;
28210 struct glyph *end;
28211
28212 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28213 returns them in row/column units! */
28214 string = mode_line_string (w, area, &x, &y, &charpos,
28215 &object, &dx, &dy, &width, &height);
28216
28217 row = (area == ON_MODE_LINE
28218 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28219 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28220
28221 /* Find the glyph under the mouse pointer. */
28222 if (row->mode_line_p && row->enabled_p)
28223 {
28224 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28225 end = glyph + row->used[TEXT_AREA];
28226
28227 for (x0 = original_x_pixel;
28228 glyph < end && x0 >= glyph->pixel_width;
28229 ++glyph)
28230 x0 -= glyph->pixel_width;
28231
28232 if (glyph >= end)
28233 glyph = NULL;
28234 }
28235 }
28236 else
28237 {
28238 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28239 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28240 returns them in row/column units! */
28241 string = marginal_area_string (w, area, &x, &y, &charpos,
28242 &object, &dx, &dy, &width, &height);
28243 }
28244
28245 help = Qnil;
28246
28247 #ifdef HAVE_WINDOW_SYSTEM
28248 if (IMAGEP (object))
28249 {
28250 Lisp_Object image_map, hotspot;
28251 if ((image_map = Fplist_get (XCDR (object), QCmap),
28252 !NILP (image_map))
28253 && (hotspot = find_hot_spot (image_map, dx, dy),
28254 CONSP (hotspot))
28255 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28256 {
28257 Lisp_Object plist;
28258
28259 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28260 If so, we could look for mouse-enter, mouse-leave
28261 properties in PLIST (and do something...). */
28262 hotspot = XCDR (hotspot);
28263 if (CONSP (hotspot)
28264 && (plist = XCAR (hotspot), CONSP (plist)))
28265 {
28266 pointer = Fplist_get (plist, Qpointer);
28267 if (NILP (pointer))
28268 pointer = Qhand;
28269 help = Fplist_get (plist, Qhelp_echo);
28270 if (!NILP (help))
28271 {
28272 help_echo_string = help;
28273 XSETWINDOW (help_echo_window, w);
28274 help_echo_object = w->contents;
28275 help_echo_pos = charpos;
28276 }
28277 }
28278 }
28279 if (NILP (pointer))
28280 pointer = Fplist_get (XCDR (object), QCpointer);
28281 }
28282 #endif /* HAVE_WINDOW_SYSTEM */
28283
28284 if (STRINGP (string))
28285 pos = make_number (charpos);
28286
28287 /* Set the help text and mouse pointer. If the mouse is on a part
28288 of the mode line without any text (e.g. past the right edge of
28289 the mode line text), use the default help text and pointer. */
28290 if (STRINGP (string) || area == ON_MODE_LINE)
28291 {
28292 /* Arrange to display the help by setting the global variables
28293 help_echo_string, help_echo_object, and help_echo_pos. */
28294 if (NILP (help))
28295 {
28296 if (STRINGP (string))
28297 help = Fget_text_property (pos, Qhelp_echo, string);
28298
28299 if (!NILP (help))
28300 {
28301 help_echo_string = help;
28302 XSETWINDOW (help_echo_window, w);
28303 help_echo_object = string;
28304 help_echo_pos = charpos;
28305 }
28306 else if (area == ON_MODE_LINE)
28307 {
28308 Lisp_Object default_help
28309 = buffer_local_value_1 (Qmode_line_default_help_echo,
28310 w->contents);
28311
28312 if (STRINGP (default_help))
28313 {
28314 help_echo_string = default_help;
28315 XSETWINDOW (help_echo_window, w);
28316 help_echo_object = Qnil;
28317 help_echo_pos = -1;
28318 }
28319 }
28320 }
28321
28322 #ifdef HAVE_WINDOW_SYSTEM
28323 /* Change the mouse pointer according to what is under it. */
28324 if (FRAME_WINDOW_P (f))
28325 {
28326 dpyinfo = FRAME_DISPLAY_INFO (f);
28327 if (STRINGP (string))
28328 {
28329 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28330
28331 if (NILP (pointer))
28332 pointer = Fget_text_property (pos, Qpointer, string);
28333
28334 /* Change the mouse pointer according to what is under X/Y. */
28335 if (NILP (pointer)
28336 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28337 {
28338 Lisp_Object map;
28339 map = Fget_text_property (pos, Qlocal_map, string);
28340 if (!KEYMAPP (map))
28341 map = Fget_text_property (pos, Qkeymap, string);
28342 if (!KEYMAPP (map))
28343 cursor = dpyinfo->vertical_scroll_bar_cursor;
28344 }
28345 }
28346 else
28347 /* Default mode-line pointer. */
28348 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28349 }
28350 #endif
28351 }
28352
28353 /* Change the mouse face according to what is under X/Y. */
28354 if (STRINGP (string))
28355 {
28356 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28357 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28358 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28359 && glyph)
28360 {
28361 Lisp_Object b, e;
28362
28363 struct glyph * tmp_glyph;
28364
28365 int gpos;
28366 int gseq_length;
28367 int total_pixel_width;
28368 ptrdiff_t begpos, endpos, ignore;
28369
28370 int vpos, hpos;
28371
28372 b = Fprevious_single_property_change (make_number (charpos + 1),
28373 Qmouse_face, string, Qnil);
28374 if (NILP (b))
28375 begpos = 0;
28376 else
28377 begpos = XINT (b);
28378
28379 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28380 if (NILP (e))
28381 endpos = SCHARS (string);
28382 else
28383 endpos = XINT (e);
28384
28385 /* Calculate the glyph position GPOS of GLYPH in the
28386 displayed string, relative to the beginning of the
28387 highlighted part of the string.
28388
28389 Note: GPOS is different from CHARPOS. CHARPOS is the
28390 position of GLYPH in the internal string object. A mode
28391 line string format has structures which are converted to
28392 a flattened string by the Emacs Lisp interpreter. The
28393 internal string is an element of those structures. The
28394 displayed string is the flattened string. */
28395 tmp_glyph = row_start_glyph;
28396 while (tmp_glyph < glyph
28397 && (!(EQ (tmp_glyph->object, glyph->object)
28398 && begpos <= tmp_glyph->charpos
28399 && tmp_glyph->charpos < endpos)))
28400 tmp_glyph++;
28401 gpos = glyph - tmp_glyph;
28402
28403 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28404 the highlighted part of the displayed string to which
28405 GLYPH belongs. Note: GSEQ_LENGTH is different from
28406 SCHARS (STRING), because the latter returns the length of
28407 the internal string. */
28408 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28409 tmp_glyph > glyph
28410 && (!(EQ (tmp_glyph->object, glyph->object)
28411 && begpos <= tmp_glyph->charpos
28412 && tmp_glyph->charpos < endpos));
28413 tmp_glyph--)
28414 ;
28415 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28416
28417 /* Calculate the total pixel width of all the glyphs between
28418 the beginning of the highlighted area and GLYPH. */
28419 total_pixel_width = 0;
28420 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28421 total_pixel_width += tmp_glyph->pixel_width;
28422
28423 /* Pre calculation of re-rendering position. Note: X is in
28424 column units here, after the call to mode_line_string or
28425 marginal_area_string. */
28426 hpos = x - gpos;
28427 vpos = (area == ON_MODE_LINE
28428 ? (w->current_matrix)->nrows - 1
28429 : 0);
28430
28431 /* If GLYPH's position is included in the region that is
28432 already drawn in mouse face, we have nothing to do. */
28433 if ( EQ (window, hlinfo->mouse_face_window)
28434 && (!row->reversed_p
28435 ? (hlinfo->mouse_face_beg_col <= hpos
28436 && hpos < hlinfo->mouse_face_end_col)
28437 /* In R2L rows we swap BEG and END, see below. */
28438 : (hlinfo->mouse_face_end_col <= hpos
28439 && hpos < hlinfo->mouse_face_beg_col))
28440 && hlinfo->mouse_face_beg_row == vpos )
28441 return;
28442
28443 if (clear_mouse_face (hlinfo))
28444 cursor = No_Cursor;
28445
28446 if (!row->reversed_p)
28447 {
28448 hlinfo->mouse_face_beg_col = hpos;
28449 hlinfo->mouse_face_beg_x = original_x_pixel
28450 - (total_pixel_width + dx);
28451 hlinfo->mouse_face_end_col = hpos + gseq_length;
28452 hlinfo->mouse_face_end_x = 0;
28453 }
28454 else
28455 {
28456 /* In R2L rows, show_mouse_face expects BEG and END
28457 coordinates to be swapped. */
28458 hlinfo->mouse_face_end_col = hpos;
28459 hlinfo->mouse_face_end_x = original_x_pixel
28460 - (total_pixel_width + dx);
28461 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28462 hlinfo->mouse_face_beg_x = 0;
28463 }
28464
28465 hlinfo->mouse_face_beg_row = vpos;
28466 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28467 hlinfo->mouse_face_past_end = 0;
28468 hlinfo->mouse_face_window = window;
28469
28470 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28471 charpos,
28472 0, &ignore,
28473 glyph->face_id,
28474 1);
28475 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28476
28477 if (NILP (pointer))
28478 pointer = Qhand;
28479 }
28480 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28481 clear_mouse_face (hlinfo);
28482 }
28483 #ifdef HAVE_WINDOW_SYSTEM
28484 if (FRAME_WINDOW_P (f))
28485 define_frame_cursor1 (f, cursor, pointer);
28486 #endif
28487 }
28488
28489
28490 /* EXPORT:
28491 Take proper action when the mouse has moved to position X, Y on
28492 frame F with regards to highlighting portions of display that have
28493 mouse-face properties. Also de-highlight portions of display where
28494 the mouse was before, set the mouse pointer shape as appropriate
28495 for the mouse coordinates, and activate help echo (tooltips).
28496 X and Y can be negative or out of range. */
28497
28498 void
28499 note_mouse_highlight (struct frame *f, int x, int y)
28500 {
28501 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28502 enum window_part part = ON_NOTHING;
28503 Lisp_Object window;
28504 struct window *w;
28505 Cursor cursor = No_Cursor;
28506 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28507 struct buffer *b;
28508
28509 /* When a menu is active, don't highlight because this looks odd. */
28510 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28511 if (popup_activated ())
28512 return;
28513 #endif
28514
28515 if (!f->glyphs_initialized_p
28516 || f->pointer_invisible)
28517 return;
28518
28519 hlinfo->mouse_face_mouse_x = x;
28520 hlinfo->mouse_face_mouse_y = y;
28521 hlinfo->mouse_face_mouse_frame = f;
28522
28523 if (hlinfo->mouse_face_defer)
28524 return;
28525
28526 /* Which window is that in? */
28527 window = window_from_coordinates (f, x, y, &part, 1);
28528
28529 /* If displaying active text in another window, clear that. */
28530 if (! EQ (window, hlinfo->mouse_face_window)
28531 /* Also clear if we move out of text area in same window. */
28532 || (!NILP (hlinfo->mouse_face_window)
28533 && !NILP (window)
28534 && part != ON_TEXT
28535 && part != ON_MODE_LINE
28536 && part != ON_HEADER_LINE))
28537 clear_mouse_face (hlinfo);
28538
28539 /* Not on a window -> return. */
28540 if (!WINDOWP (window))
28541 return;
28542
28543 /* Reset help_echo_string. It will get recomputed below. */
28544 help_echo_string = Qnil;
28545
28546 /* Convert to window-relative pixel coordinates. */
28547 w = XWINDOW (window);
28548 frame_to_window_pixel_xy (w, &x, &y);
28549
28550 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28551 /* Handle tool-bar window differently since it doesn't display a
28552 buffer. */
28553 if (EQ (window, f->tool_bar_window))
28554 {
28555 note_tool_bar_highlight (f, x, y);
28556 return;
28557 }
28558 #endif
28559
28560 /* Mouse is on the mode, header line or margin? */
28561 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28562 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28563 {
28564 note_mode_line_or_margin_highlight (window, x, y, part);
28565 return;
28566 }
28567
28568 #ifdef HAVE_WINDOW_SYSTEM
28569 if (part == ON_VERTICAL_BORDER)
28570 {
28571 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28572 help_echo_string = build_string ("drag-mouse-1: resize");
28573 }
28574 else if (part == ON_RIGHT_DIVIDER)
28575 {
28576 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28577 help_echo_string = build_string ("drag-mouse-1: resize");
28578 }
28579 else if (part == ON_BOTTOM_DIVIDER)
28580 {
28581 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28582 help_echo_string = build_string ("drag-mouse-1: resize");
28583 }
28584 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28585 || part == ON_SCROLL_BAR)
28586 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28587 else
28588 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28589 #endif
28590
28591 /* Are we in a window whose display is up to date?
28592 And verify the buffer's text has not changed. */
28593 b = XBUFFER (w->contents);
28594 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28595 {
28596 int hpos, vpos, dx, dy, area = LAST_AREA;
28597 ptrdiff_t pos;
28598 struct glyph *glyph;
28599 Lisp_Object object;
28600 Lisp_Object mouse_face = Qnil, position;
28601 Lisp_Object *overlay_vec = NULL;
28602 ptrdiff_t i, noverlays;
28603 struct buffer *obuf;
28604 ptrdiff_t obegv, ozv;
28605 int same_region;
28606
28607 /* Find the glyph under X/Y. */
28608 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28609
28610 #ifdef HAVE_WINDOW_SYSTEM
28611 /* Look for :pointer property on image. */
28612 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28613 {
28614 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28615 if (img != NULL && IMAGEP (img->spec))
28616 {
28617 Lisp_Object image_map, hotspot;
28618 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28619 !NILP (image_map))
28620 && (hotspot = find_hot_spot (image_map,
28621 glyph->slice.img.x + dx,
28622 glyph->slice.img.y + dy),
28623 CONSP (hotspot))
28624 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28625 {
28626 Lisp_Object plist;
28627
28628 /* Could check XCAR (hotspot) to see if we enter/leave
28629 this hot-spot.
28630 If so, we could look for mouse-enter, mouse-leave
28631 properties in PLIST (and do something...). */
28632 hotspot = XCDR (hotspot);
28633 if (CONSP (hotspot)
28634 && (plist = XCAR (hotspot), CONSP (plist)))
28635 {
28636 pointer = Fplist_get (plist, Qpointer);
28637 if (NILP (pointer))
28638 pointer = Qhand;
28639 help_echo_string = Fplist_get (plist, Qhelp_echo);
28640 if (!NILP (help_echo_string))
28641 {
28642 help_echo_window = window;
28643 help_echo_object = glyph->object;
28644 help_echo_pos = glyph->charpos;
28645 }
28646 }
28647 }
28648 if (NILP (pointer))
28649 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28650 }
28651 }
28652 #endif /* HAVE_WINDOW_SYSTEM */
28653
28654 /* Clear mouse face if X/Y not over text. */
28655 if (glyph == NULL
28656 || area != TEXT_AREA
28657 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28658 /* Glyph's OBJECT is an integer for glyphs inserted by the
28659 display engine for its internal purposes, like truncation
28660 and continuation glyphs and blanks beyond the end of
28661 line's text on text terminals. If we are over such a
28662 glyph, we are not over any text. */
28663 || INTEGERP (glyph->object)
28664 /* R2L rows have a stretch glyph at their front, which
28665 stands for no text, whereas L2R rows have no glyphs at
28666 all beyond the end of text. Treat such stretch glyphs
28667 like we do with NULL glyphs in L2R rows. */
28668 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28669 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28670 && glyph->type == STRETCH_GLYPH
28671 && glyph->avoid_cursor_p))
28672 {
28673 if (clear_mouse_face (hlinfo))
28674 cursor = No_Cursor;
28675 #ifdef HAVE_WINDOW_SYSTEM
28676 if (FRAME_WINDOW_P (f) && NILP (pointer))
28677 {
28678 if (area != TEXT_AREA)
28679 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28680 else
28681 pointer = Vvoid_text_area_pointer;
28682 }
28683 #endif
28684 goto set_cursor;
28685 }
28686
28687 pos = glyph->charpos;
28688 object = glyph->object;
28689 if (!STRINGP (object) && !BUFFERP (object))
28690 goto set_cursor;
28691
28692 /* If we get an out-of-range value, return now; avoid an error. */
28693 if (BUFFERP (object) && pos > BUF_Z (b))
28694 goto set_cursor;
28695
28696 /* Make the window's buffer temporarily current for
28697 overlays_at and compute_char_face. */
28698 obuf = current_buffer;
28699 current_buffer = b;
28700 obegv = BEGV;
28701 ozv = ZV;
28702 BEGV = BEG;
28703 ZV = Z;
28704
28705 /* Is this char mouse-active or does it have help-echo? */
28706 position = make_number (pos);
28707
28708 if (BUFFERP (object))
28709 {
28710 /* Put all the overlays we want in a vector in overlay_vec. */
28711 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28712 /* Sort overlays into increasing priority order. */
28713 noverlays = sort_overlays (overlay_vec, noverlays, w);
28714 }
28715 else
28716 noverlays = 0;
28717
28718 if (NILP (Vmouse_highlight))
28719 {
28720 clear_mouse_face (hlinfo);
28721 goto check_help_echo;
28722 }
28723
28724 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28725
28726 if (same_region)
28727 cursor = No_Cursor;
28728
28729 /* Check mouse-face highlighting. */
28730 if (! same_region
28731 /* If there exists an overlay with mouse-face overlapping
28732 the one we are currently highlighting, we have to
28733 check if we enter the overlapping overlay, and then
28734 highlight only that. */
28735 || (OVERLAYP (hlinfo->mouse_face_overlay)
28736 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28737 {
28738 /* Find the highest priority overlay with a mouse-face. */
28739 Lisp_Object overlay = Qnil;
28740 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28741 {
28742 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28743 if (!NILP (mouse_face))
28744 overlay = overlay_vec[i];
28745 }
28746
28747 /* If we're highlighting the same overlay as before, there's
28748 no need to do that again. */
28749 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28750 goto check_help_echo;
28751 hlinfo->mouse_face_overlay = overlay;
28752
28753 /* Clear the display of the old active region, if any. */
28754 if (clear_mouse_face (hlinfo))
28755 cursor = No_Cursor;
28756
28757 /* If no overlay applies, get a text property. */
28758 if (NILP (overlay))
28759 mouse_face = Fget_text_property (position, Qmouse_face, object);
28760
28761 /* Next, compute the bounds of the mouse highlighting and
28762 display it. */
28763 if (!NILP (mouse_face) && STRINGP (object))
28764 {
28765 /* The mouse-highlighting comes from a display string
28766 with a mouse-face. */
28767 Lisp_Object s, e;
28768 ptrdiff_t ignore;
28769
28770 s = Fprevious_single_property_change
28771 (make_number (pos + 1), Qmouse_face, object, Qnil);
28772 e = Fnext_single_property_change
28773 (position, Qmouse_face, object, Qnil);
28774 if (NILP (s))
28775 s = make_number (0);
28776 if (NILP (e))
28777 e = make_number (SCHARS (object));
28778 mouse_face_from_string_pos (w, hlinfo, object,
28779 XINT (s), XINT (e));
28780 hlinfo->mouse_face_past_end = 0;
28781 hlinfo->mouse_face_window = window;
28782 hlinfo->mouse_face_face_id
28783 = face_at_string_position (w, object, pos, 0, &ignore,
28784 glyph->face_id, 1);
28785 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28786 cursor = No_Cursor;
28787 }
28788 else
28789 {
28790 /* The mouse-highlighting, if any, comes from an overlay
28791 or text property in the buffer. */
28792 Lisp_Object buffer IF_LINT (= Qnil);
28793 Lisp_Object disp_string IF_LINT (= Qnil);
28794
28795 if (STRINGP (object))
28796 {
28797 /* If we are on a display string with no mouse-face,
28798 check if the text under it has one. */
28799 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28800 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28801 pos = string_buffer_position (object, start);
28802 if (pos > 0)
28803 {
28804 mouse_face = get_char_property_and_overlay
28805 (make_number (pos), Qmouse_face, w->contents, &overlay);
28806 buffer = w->contents;
28807 disp_string = object;
28808 }
28809 }
28810 else
28811 {
28812 buffer = object;
28813 disp_string = Qnil;
28814 }
28815
28816 if (!NILP (mouse_face))
28817 {
28818 Lisp_Object before, after;
28819 Lisp_Object before_string, after_string;
28820 /* To correctly find the limits of mouse highlight
28821 in a bidi-reordered buffer, we must not use the
28822 optimization of limiting the search in
28823 previous-single-property-change and
28824 next-single-property-change, because
28825 rows_from_pos_range needs the real start and end
28826 positions to DTRT in this case. That's because
28827 the first row visible in a window does not
28828 necessarily display the character whose position
28829 is the smallest. */
28830 Lisp_Object lim1
28831 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28832 ? Fmarker_position (w->start)
28833 : Qnil;
28834 Lisp_Object lim2
28835 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28836 ? make_number (BUF_Z (XBUFFER (buffer))
28837 - w->window_end_pos)
28838 : Qnil;
28839
28840 if (NILP (overlay))
28841 {
28842 /* Handle the text property case. */
28843 before = Fprevious_single_property_change
28844 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28845 after = Fnext_single_property_change
28846 (make_number (pos), Qmouse_face, buffer, lim2);
28847 before_string = after_string = Qnil;
28848 }
28849 else
28850 {
28851 /* Handle the overlay case. */
28852 before = Foverlay_start (overlay);
28853 after = Foverlay_end (overlay);
28854 before_string = Foverlay_get (overlay, Qbefore_string);
28855 after_string = Foverlay_get (overlay, Qafter_string);
28856
28857 if (!STRINGP (before_string)) before_string = Qnil;
28858 if (!STRINGP (after_string)) after_string = Qnil;
28859 }
28860
28861 mouse_face_from_buffer_pos (window, hlinfo, pos,
28862 NILP (before)
28863 ? 1
28864 : XFASTINT (before),
28865 NILP (after)
28866 ? BUF_Z (XBUFFER (buffer))
28867 : XFASTINT (after),
28868 before_string, after_string,
28869 disp_string);
28870 cursor = No_Cursor;
28871 }
28872 }
28873 }
28874
28875 check_help_echo:
28876
28877 /* Look for a `help-echo' property. */
28878 if (NILP (help_echo_string)) {
28879 Lisp_Object help, overlay;
28880
28881 /* Check overlays first. */
28882 help = overlay = Qnil;
28883 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28884 {
28885 overlay = overlay_vec[i];
28886 help = Foverlay_get (overlay, Qhelp_echo);
28887 }
28888
28889 if (!NILP (help))
28890 {
28891 help_echo_string = help;
28892 help_echo_window = window;
28893 help_echo_object = overlay;
28894 help_echo_pos = pos;
28895 }
28896 else
28897 {
28898 Lisp_Object obj = glyph->object;
28899 ptrdiff_t charpos = glyph->charpos;
28900
28901 /* Try text properties. */
28902 if (STRINGP (obj)
28903 && charpos >= 0
28904 && charpos < SCHARS (obj))
28905 {
28906 help = Fget_text_property (make_number (charpos),
28907 Qhelp_echo, obj);
28908 if (NILP (help))
28909 {
28910 /* If the string itself doesn't specify a help-echo,
28911 see if the buffer text ``under'' it does. */
28912 struct glyph_row *r
28913 = MATRIX_ROW (w->current_matrix, vpos);
28914 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28915 ptrdiff_t p = string_buffer_position (obj, start);
28916 if (p > 0)
28917 {
28918 help = Fget_char_property (make_number (p),
28919 Qhelp_echo, w->contents);
28920 if (!NILP (help))
28921 {
28922 charpos = p;
28923 obj = w->contents;
28924 }
28925 }
28926 }
28927 }
28928 else if (BUFFERP (obj)
28929 && charpos >= BEGV
28930 && charpos < ZV)
28931 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28932 obj);
28933
28934 if (!NILP (help))
28935 {
28936 help_echo_string = help;
28937 help_echo_window = window;
28938 help_echo_object = obj;
28939 help_echo_pos = charpos;
28940 }
28941 }
28942 }
28943
28944 #ifdef HAVE_WINDOW_SYSTEM
28945 /* Look for a `pointer' property. */
28946 if (FRAME_WINDOW_P (f) && NILP (pointer))
28947 {
28948 /* Check overlays first. */
28949 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28950 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28951
28952 if (NILP (pointer))
28953 {
28954 Lisp_Object obj = glyph->object;
28955 ptrdiff_t charpos = glyph->charpos;
28956
28957 /* Try text properties. */
28958 if (STRINGP (obj)
28959 && charpos >= 0
28960 && charpos < SCHARS (obj))
28961 {
28962 pointer = Fget_text_property (make_number (charpos),
28963 Qpointer, obj);
28964 if (NILP (pointer))
28965 {
28966 /* If the string itself doesn't specify a pointer,
28967 see if the buffer text ``under'' it does. */
28968 struct glyph_row *r
28969 = MATRIX_ROW (w->current_matrix, vpos);
28970 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28971 ptrdiff_t p = string_buffer_position (obj, start);
28972 if (p > 0)
28973 pointer = Fget_char_property (make_number (p),
28974 Qpointer, w->contents);
28975 }
28976 }
28977 else if (BUFFERP (obj)
28978 && charpos >= BEGV
28979 && charpos < ZV)
28980 pointer = Fget_text_property (make_number (charpos),
28981 Qpointer, obj);
28982 }
28983 }
28984 #endif /* HAVE_WINDOW_SYSTEM */
28985
28986 BEGV = obegv;
28987 ZV = ozv;
28988 current_buffer = obuf;
28989 }
28990
28991 set_cursor:
28992
28993 #ifdef HAVE_WINDOW_SYSTEM
28994 if (FRAME_WINDOW_P (f))
28995 define_frame_cursor1 (f, cursor, pointer);
28996 #else
28997 /* This is here to prevent a compiler error, about "label at end of
28998 compound statement". */
28999 return;
29000 #endif
29001 }
29002
29003
29004 /* EXPORT for RIF:
29005 Clear any mouse-face on window W. This function is part of the
29006 redisplay interface, and is called from try_window_id and similar
29007 functions to ensure the mouse-highlight is off. */
29008
29009 void
29010 x_clear_window_mouse_face (struct window *w)
29011 {
29012 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29013 Lisp_Object window;
29014
29015 block_input ();
29016 XSETWINDOW (window, w);
29017 if (EQ (window, hlinfo->mouse_face_window))
29018 clear_mouse_face (hlinfo);
29019 unblock_input ();
29020 }
29021
29022
29023 /* EXPORT:
29024 Just discard the mouse face information for frame F, if any.
29025 This is used when the size of F is changed. */
29026
29027 void
29028 cancel_mouse_face (struct frame *f)
29029 {
29030 Lisp_Object window;
29031 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29032
29033 window = hlinfo->mouse_face_window;
29034 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29035 reset_mouse_highlight (hlinfo);
29036 }
29037
29038
29039 \f
29040 /***********************************************************************
29041 Exposure Events
29042 ***********************************************************************/
29043
29044 #ifdef HAVE_WINDOW_SYSTEM
29045
29046 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29047 which intersects rectangle R. R is in window-relative coordinates. */
29048
29049 static void
29050 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29051 enum glyph_row_area area)
29052 {
29053 struct glyph *first = row->glyphs[area];
29054 struct glyph *end = row->glyphs[area] + row->used[area];
29055 struct glyph *last;
29056 int first_x, start_x, x;
29057
29058 if (area == TEXT_AREA && row->fill_line_p)
29059 /* If row extends face to end of line write the whole line. */
29060 draw_glyphs (w, 0, row, area,
29061 0, row->used[area],
29062 DRAW_NORMAL_TEXT, 0);
29063 else
29064 {
29065 /* Set START_X to the window-relative start position for drawing glyphs of
29066 AREA. The first glyph of the text area can be partially visible.
29067 The first glyphs of other areas cannot. */
29068 start_x = window_box_left_offset (w, area);
29069 x = start_x;
29070 if (area == TEXT_AREA)
29071 x += row->x;
29072
29073 /* Find the first glyph that must be redrawn. */
29074 while (first < end
29075 && x + first->pixel_width < r->x)
29076 {
29077 x += first->pixel_width;
29078 ++first;
29079 }
29080
29081 /* Find the last one. */
29082 last = first;
29083 first_x = x;
29084 while (last < end
29085 && x < r->x + r->width)
29086 {
29087 x += last->pixel_width;
29088 ++last;
29089 }
29090
29091 /* Repaint. */
29092 if (last > first)
29093 draw_glyphs (w, first_x - start_x, row, area,
29094 first - row->glyphs[area], last - row->glyphs[area],
29095 DRAW_NORMAL_TEXT, 0);
29096 }
29097 }
29098
29099
29100 /* Redraw the parts of the glyph row ROW on window W intersecting
29101 rectangle R. R is in window-relative coordinates. Value is
29102 non-zero if mouse-face was overwritten. */
29103
29104 static int
29105 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29106 {
29107 eassert (row->enabled_p);
29108
29109 if (row->mode_line_p || w->pseudo_window_p)
29110 draw_glyphs (w, 0, row, TEXT_AREA,
29111 0, row->used[TEXT_AREA],
29112 DRAW_NORMAL_TEXT, 0);
29113 else
29114 {
29115 if (row->used[LEFT_MARGIN_AREA])
29116 expose_area (w, row, r, LEFT_MARGIN_AREA);
29117 if (row->used[TEXT_AREA])
29118 expose_area (w, row, r, TEXT_AREA);
29119 if (row->used[RIGHT_MARGIN_AREA])
29120 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29121 draw_row_fringe_bitmaps (w, row);
29122 }
29123
29124 return row->mouse_face_p;
29125 }
29126
29127
29128 /* Redraw those parts of glyphs rows during expose event handling that
29129 overlap other rows. Redrawing of an exposed line writes over parts
29130 of lines overlapping that exposed line; this function fixes that.
29131
29132 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29133 row in W's current matrix that is exposed and overlaps other rows.
29134 LAST_OVERLAPPING_ROW is the last such row. */
29135
29136 static void
29137 expose_overlaps (struct window *w,
29138 struct glyph_row *first_overlapping_row,
29139 struct glyph_row *last_overlapping_row,
29140 XRectangle *r)
29141 {
29142 struct glyph_row *row;
29143
29144 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29145 if (row->overlapping_p)
29146 {
29147 eassert (row->enabled_p && !row->mode_line_p);
29148
29149 row->clip = r;
29150 if (row->used[LEFT_MARGIN_AREA])
29151 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29152
29153 if (row->used[TEXT_AREA])
29154 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29155
29156 if (row->used[RIGHT_MARGIN_AREA])
29157 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29158 row->clip = NULL;
29159 }
29160 }
29161
29162
29163 /* Return non-zero if W's cursor intersects rectangle R. */
29164
29165 static int
29166 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29167 {
29168 XRectangle cr, result;
29169 struct glyph *cursor_glyph;
29170 struct glyph_row *row;
29171
29172 if (w->phys_cursor.vpos >= 0
29173 && w->phys_cursor.vpos < w->current_matrix->nrows
29174 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29175 row->enabled_p)
29176 && row->cursor_in_fringe_p)
29177 {
29178 /* Cursor is in the fringe. */
29179 cr.x = window_box_right_offset (w,
29180 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29181 ? RIGHT_MARGIN_AREA
29182 : TEXT_AREA));
29183 cr.y = row->y;
29184 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29185 cr.height = row->height;
29186 return x_intersect_rectangles (&cr, r, &result);
29187 }
29188
29189 cursor_glyph = get_phys_cursor_glyph (w);
29190 if (cursor_glyph)
29191 {
29192 /* r is relative to W's box, but w->phys_cursor.x is relative
29193 to left edge of W's TEXT area. Adjust it. */
29194 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29195 cr.y = w->phys_cursor.y;
29196 cr.width = cursor_glyph->pixel_width;
29197 cr.height = w->phys_cursor_height;
29198 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29199 I assume the effect is the same -- and this is portable. */
29200 return x_intersect_rectangles (&cr, r, &result);
29201 }
29202 /* If we don't understand the format, pretend we're not in the hot-spot. */
29203 return 0;
29204 }
29205
29206
29207 /* EXPORT:
29208 Draw a vertical window border to the right of window W if W doesn't
29209 have vertical scroll bars. */
29210
29211 void
29212 x_draw_vertical_border (struct window *w)
29213 {
29214 struct frame *f = XFRAME (WINDOW_FRAME (w));
29215
29216 /* We could do better, if we knew what type of scroll-bar the adjacent
29217 windows (on either side) have... But we don't :-(
29218 However, I think this works ok. ++KFS 2003-04-25 */
29219
29220 /* Redraw borders between horizontally adjacent windows. Don't
29221 do it for frames with vertical scroll bars because either the
29222 right scroll bar of a window, or the left scroll bar of its
29223 neighbor will suffice as a border. */
29224 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29225 return;
29226
29227 /* Note: It is necessary to redraw both the left and the right
29228 borders, for when only this single window W is being
29229 redisplayed. */
29230 if (!WINDOW_RIGHTMOST_P (w)
29231 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29232 {
29233 int x0, x1, y0, y1;
29234
29235 window_box_edges (w, &x0, &y0, &x1, &y1);
29236 y1 -= 1;
29237
29238 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29239 x1 -= 1;
29240
29241 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29242 }
29243
29244 if (!WINDOW_LEFTMOST_P (w)
29245 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29246 {
29247 int x0, x1, y0, y1;
29248
29249 window_box_edges (w, &x0, &y0, &x1, &y1);
29250 y1 -= 1;
29251
29252 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29253 x0 -= 1;
29254
29255 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29256 }
29257 }
29258
29259
29260 /* Draw window dividers for window W. */
29261
29262 void
29263 x_draw_right_divider (struct window *w)
29264 {
29265 struct frame *f = WINDOW_XFRAME (w);
29266
29267 if (w->mini || w->pseudo_window_p)
29268 return;
29269 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29270 {
29271 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29272 int x1 = WINDOW_RIGHT_EDGE_X (w);
29273 int y0 = WINDOW_TOP_EDGE_Y (w);
29274 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29275
29276 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29277 }
29278 }
29279
29280 static void
29281 x_draw_bottom_divider (struct window *w)
29282 {
29283 struct frame *f = XFRAME (WINDOW_FRAME (w));
29284
29285 if (w->mini || w->pseudo_window_p)
29286 return;
29287 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29288 {
29289 int x0 = WINDOW_LEFT_EDGE_X (w);
29290 int x1 = WINDOW_RIGHT_EDGE_X (w);
29291 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29292 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29293
29294 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29295 }
29296 }
29297
29298 /* Redraw the part of window W intersection rectangle FR. Pixel
29299 coordinates in FR are frame-relative. Call this function with
29300 input blocked. Value is non-zero if the exposure overwrites
29301 mouse-face. */
29302
29303 static int
29304 expose_window (struct window *w, XRectangle *fr)
29305 {
29306 struct frame *f = XFRAME (w->frame);
29307 XRectangle wr, r;
29308 int mouse_face_overwritten_p = 0;
29309
29310 /* If window is not yet fully initialized, do nothing. This can
29311 happen when toolkit scroll bars are used and a window is split.
29312 Reconfiguring the scroll bar will generate an expose for a newly
29313 created window. */
29314 if (w->current_matrix == NULL)
29315 return 0;
29316
29317 /* When we're currently updating the window, display and current
29318 matrix usually don't agree. Arrange for a thorough display
29319 later. */
29320 if (w->must_be_updated_p)
29321 {
29322 SET_FRAME_GARBAGED (f);
29323 return 0;
29324 }
29325
29326 /* Frame-relative pixel rectangle of W. */
29327 wr.x = WINDOW_LEFT_EDGE_X (w);
29328 wr.y = WINDOW_TOP_EDGE_Y (w);
29329 wr.width = WINDOW_PIXEL_WIDTH (w);
29330 wr.height = WINDOW_PIXEL_HEIGHT (w);
29331
29332 if (x_intersect_rectangles (fr, &wr, &r))
29333 {
29334 int yb = window_text_bottom_y (w);
29335 struct glyph_row *row;
29336 int cursor_cleared_p, phys_cursor_on_p;
29337 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29338
29339 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29340 r.x, r.y, r.width, r.height));
29341
29342 /* Convert to window coordinates. */
29343 r.x -= WINDOW_LEFT_EDGE_X (w);
29344 r.y -= WINDOW_TOP_EDGE_Y (w);
29345
29346 /* Turn off the cursor. */
29347 if (!w->pseudo_window_p
29348 && phys_cursor_in_rect_p (w, &r))
29349 {
29350 x_clear_cursor (w);
29351 cursor_cleared_p = 1;
29352 }
29353 else
29354 cursor_cleared_p = 0;
29355
29356 /* If the row containing the cursor extends face to end of line,
29357 then expose_area might overwrite the cursor outside the
29358 rectangle and thus notice_overwritten_cursor might clear
29359 w->phys_cursor_on_p. We remember the original value and
29360 check later if it is changed. */
29361 phys_cursor_on_p = w->phys_cursor_on_p;
29362
29363 /* Update lines intersecting rectangle R. */
29364 first_overlapping_row = last_overlapping_row = NULL;
29365 for (row = w->current_matrix->rows;
29366 row->enabled_p;
29367 ++row)
29368 {
29369 int y0 = row->y;
29370 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29371
29372 if ((y0 >= r.y && y0 < r.y + r.height)
29373 || (y1 > r.y && y1 < r.y + r.height)
29374 || (r.y >= y0 && r.y < y1)
29375 || (r.y + r.height > y0 && r.y + r.height < y1))
29376 {
29377 /* A header line may be overlapping, but there is no need
29378 to fix overlapping areas for them. KFS 2005-02-12 */
29379 if (row->overlapping_p && !row->mode_line_p)
29380 {
29381 if (first_overlapping_row == NULL)
29382 first_overlapping_row = row;
29383 last_overlapping_row = row;
29384 }
29385
29386 row->clip = fr;
29387 if (expose_line (w, row, &r))
29388 mouse_face_overwritten_p = 1;
29389 row->clip = NULL;
29390 }
29391 else if (row->overlapping_p)
29392 {
29393 /* We must redraw a row overlapping the exposed area. */
29394 if (y0 < r.y
29395 ? y0 + row->phys_height > r.y
29396 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29397 {
29398 if (first_overlapping_row == NULL)
29399 first_overlapping_row = row;
29400 last_overlapping_row = row;
29401 }
29402 }
29403
29404 if (y1 >= yb)
29405 break;
29406 }
29407
29408 /* Display the mode line if there is one. */
29409 if (WINDOW_WANTS_MODELINE_P (w)
29410 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29411 row->enabled_p)
29412 && row->y < r.y + r.height)
29413 {
29414 if (expose_line (w, row, &r))
29415 mouse_face_overwritten_p = 1;
29416 }
29417
29418 if (!w->pseudo_window_p)
29419 {
29420 /* Fix the display of overlapping rows. */
29421 if (first_overlapping_row)
29422 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29423 fr);
29424
29425 /* Draw border between windows. */
29426 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29427 x_draw_right_divider (w);
29428 else
29429 x_draw_vertical_border (w);
29430
29431 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29432 x_draw_bottom_divider (w);
29433
29434 /* Turn the cursor on again. */
29435 if (cursor_cleared_p
29436 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29437 update_window_cursor (w, 1);
29438 }
29439 }
29440
29441 return mouse_face_overwritten_p;
29442 }
29443
29444
29445
29446 /* Redraw (parts) of all windows in the window tree rooted at W that
29447 intersect R. R contains frame pixel coordinates. Value is
29448 non-zero if the exposure overwrites mouse-face. */
29449
29450 static int
29451 expose_window_tree (struct window *w, XRectangle *r)
29452 {
29453 struct frame *f = XFRAME (w->frame);
29454 int mouse_face_overwritten_p = 0;
29455
29456 while (w && !FRAME_GARBAGED_P (f))
29457 {
29458 if (WINDOWP (w->contents))
29459 mouse_face_overwritten_p
29460 |= expose_window_tree (XWINDOW (w->contents), r);
29461 else
29462 mouse_face_overwritten_p |= expose_window (w, r);
29463
29464 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29465 }
29466
29467 return mouse_face_overwritten_p;
29468 }
29469
29470
29471 /* EXPORT:
29472 Redisplay an exposed area of frame F. X and Y are the upper-left
29473 corner of the exposed rectangle. W and H are width and height of
29474 the exposed area. All are pixel values. W or H zero means redraw
29475 the entire frame. */
29476
29477 void
29478 expose_frame (struct frame *f, int x, int y, int w, int h)
29479 {
29480 XRectangle r;
29481 int mouse_face_overwritten_p = 0;
29482
29483 TRACE ((stderr, "expose_frame "));
29484
29485 /* No need to redraw if frame will be redrawn soon. */
29486 if (FRAME_GARBAGED_P (f))
29487 {
29488 TRACE ((stderr, " garbaged\n"));
29489 return;
29490 }
29491
29492 /* If basic faces haven't been realized yet, there is no point in
29493 trying to redraw anything. This can happen when we get an expose
29494 event while Emacs is starting, e.g. by moving another window. */
29495 if (FRAME_FACE_CACHE (f) == NULL
29496 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29497 {
29498 TRACE ((stderr, " no faces\n"));
29499 return;
29500 }
29501
29502 if (w == 0 || h == 0)
29503 {
29504 r.x = r.y = 0;
29505 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29506 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29507 }
29508 else
29509 {
29510 r.x = x;
29511 r.y = y;
29512 r.width = w;
29513 r.height = h;
29514 }
29515
29516 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29517 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29518
29519 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29520 if (WINDOWP (f->tool_bar_window))
29521 mouse_face_overwritten_p
29522 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29523 #endif
29524
29525 #ifdef HAVE_X_WINDOWS
29526 #ifndef MSDOS
29527 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29528 if (WINDOWP (f->menu_bar_window))
29529 mouse_face_overwritten_p
29530 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29531 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29532 #endif
29533 #endif
29534
29535 /* Some window managers support a focus-follows-mouse style with
29536 delayed raising of frames. Imagine a partially obscured frame,
29537 and moving the mouse into partially obscured mouse-face on that
29538 frame. The visible part of the mouse-face will be highlighted,
29539 then the WM raises the obscured frame. With at least one WM, KDE
29540 2.1, Emacs is not getting any event for the raising of the frame
29541 (even tried with SubstructureRedirectMask), only Expose events.
29542 These expose events will draw text normally, i.e. not
29543 highlighted. Which means we must redo the highlight here.
29544 Subsume it under ``we love X''. --gerd 2001-08-15 */
29545 /* Included in Windows version because Windows most likely does not
29546 do the right thing if any third party tool offers
29547 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29548 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29549 {
29550 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29551 if (f == hlinfo->mouse_face_mouse_frame)
29552 {
29553 int mouse_x = hlinfo->mouse_face_mouse_x;
29554 int mouse_y = hlinfo->mouse_face_mouse_y;
29555 clear_mouse_face (hlinfo);
29556 note_mouse_highlight (f, mouse_x, mouse_y);
29557 }
29558 }
29559 }
29560
29561
29562 /* EXPORT:
29563 Determine the intersection of two rectangles R1 and R2. Return
29564 the intersection in *RESULT. Value is non-zero if RESULT is not
29565 empty. */
29566
29567 int
29568 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29569 {
29570 XRectangle *left, *right;
29571 XRectangle *upper, *lower;
29572 int intersection_p = 0;
29573
29574 /* Rearrange so that R1 is the left-most rectangle. */
29575 if (r1->x < r2->x)
29576 left = r1, right = r2;
29577 else
29578 left = r2, right = r1;
29579
29580 /* X0 of the intersection is right.x0, if this is inside R1,
29581 otherwise there is no intersection. */
29582 if (right->x <= left->x + left->width)
29583 {
29584 result->x = right->x;
29585
29586 /* The right end of the intersection is the minimum of
29587 the right ends of left and right. */
29588 result->width = (min (left->x + left->width, right->x + right->width)
29589 - result->x);
29590
29591 /* Same game for Y. */
29592 if (r1->y < r2->y)
29593 upper = r1, lower = r2;
29594 else
29595 upper = r2, lower = r1;
29596
29597 /* The upper end of the intersection is lower.y0, if this is inside
29598 of upper. Otherwise, there is no intersection. */
29599 if (lower->y <= upper->y + upper->height)
29600 {
29601 result->y = lower->y;
29602
29603 /* The lower end of the intersection is the minimum of the lower
29604 ends of upper and lower. */
29605 result->height = (min (lower->y + lower->height,
29606 upper->y + upper->height)
29607 - result->y);
29608 intersection_p = 1;
29609 }
29610 }
29611
29612 return intersection_p;
29613 }
29614
29615 #endif /* HAVE_WINDOW_SYSTEM */
29616
29617 \f
29618 /***********************************************************************
29619 Initialization
29620 ***********************************************************************/
29621
29622 void
29623 syms_of_xdisp (void)
29624 {
29625 Vwith_echo_area_save_vector = Qnil;
29626 staticpro (&Vwith_echo_area_save_vector);
29627
29628 Vmessage_stack = Qnil;
29629 staticpro (&Vmessage_stack);
29630
29631 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29632 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29633
29634 message_dolog_marker1 = Fmake_marker ();
29635 staticpro (&message_dolog_marker1);
29636 message_dolog_marker2 = Fmake_marker ();
29637 staticpro (&message_dolog_marker2);
29638 message_dolog_marker3 = Fmake_marker ();
29639 staticpro (&message_dolog_marker3);
29640
29641 #ifdef GLYPH_DEBUG
29642 defsubr (&Sdump_frame_glyph_matrix);
29643 defsubr (&Sdump_glyph_matrix);
29644 defsubr (&Sdump_glyph_row);
29645 defsubr (&Sdump_tool_bar_row);
29646 defsubr (&Strace_redisplay);
29647 defsubr (&Strace_to_stderr);
29648 #endif
29649 #ifdef HAVE_WINDOW_SYSTEM
29650 defsubr (&Stool_bar_height);
29651 defsubr (&Slookup_image_map);
29652 #endif
29653 defsubr (&Sline_pixel_height);
29654 defsubr (&Sformat_mode_line);
29655 defsubr (&Sinvisible_p);
29656 defsubr (&Scurrent_bidi_paragraph_direction);
29657 defsubr (&Swindow_text_pixel_size);
29658 defsubr (&Smove_point_visually);
29659
29660 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29661 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29662 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29663 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29664 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29665 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29666 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29667 DEFSYM (Qeval, "eval");
29668 DEFSYM (QCdata, ":data");
29669 DEFSYM (Qdisplay, "display");
29670 DEFSYM (Qspace_width, "space-width");
29671 DEFSYM (Qraise, "raise");
29672 DEFSYM (Qslice, "slice");
29673 DEFSYM (Qspace, "space");
29674 DEFSYM (Qmargin, "margin");
29675 DEFSYM (Qpointer, "pointer");
29676 DEFSYM (Qleft_margin, "left-margin");
29677 DEFSYM (Qright_margin, "right-margin");
29678 DEFSYM (Qcenter, "center");
29679 DEFSYM (Qline_height, "line-height");
29680 DEFSYM (QCalign_to, ":align-to");
29681 DEFSYM (QCrelative_width, ":relative-width");
29682 DEFSYM (QCrelative_height, ":relative-height");
29683 DEFSYM (QCeval, ":eval");
29684 DEFSYM (QCpropertize, ":propertize");
29685 DEFSYM (QCfile, ":file");
29686 DEFSYM (Qfontified, "fontified");
29687 DEFSYM (Qfontification_functions, "fontification-functions");
29688 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29689 DEFSYM (Qescape_glyph, "escape-glyph");
29690 DEFSYM (Qnobreak_space, "nobreak-space");
29691 DEFSYM (Qimage, "image");
29692 DEFSYM (Qtext, "text");
29693 DEFSYM (Qboth, "both");
29694 DEFSYM (Qboth_horiz, "both-horiz");
29695 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29696 DEFSYM (QCmap, ":map");
29697 DEFSYM (QCpointer, ":pointer");
29698 DEFSYM (Qrect, "rect");
29699 DEFSYM (Qcircle, "circle");
29700 DEFSYM (Qpoly, "poly");
29701 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29702 DEFSYM (Qgrow_only, "grow-only");
29703 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29704 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29705 DEFSYM (Qposition, "position");
29706 DEFSYM (Qbuffer_position, "buffer-position");
29707 DEFSYM (Qobject, "object");
29708 DEFSYM (Qbar, "bar");
29709 DEFSYM (Qhbar, "hbar");
29710 DEFSYM (Qbox, "box");
29711 DEFSYM (Qhollow, "hollow");
29712 DEFSYM (Qhand, "hand");
29713 DEFSYM (Qarrow, "arrow");
29714 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29715
29716 list_of_error = list1 (list2 (intern_c_string ("error"),
29717 intern_c_string ("void-variable")));
29718 staticpro (&list_of_error);
29719
29720 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29721 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29722 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29723 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29724
29725 echo_buffer[0] = echo_buffer[1] = Qnil;
29726 staticpro (&echo_buffer[0]);
29727 staticpro (&echo_buffer[1]);
29728
29729 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29730 staticpro (&echo_area_buffer[0]);
29731 staticpro (&echo_area_buffer[1]);
29732
29733 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29734 staticpro (&Vmessages_buffer_name);
29735
29736 mode_line_proptrans_alist = Qnil;
29737 staticpro (&mode_line_proptrans_alist);
29738 mode_line_string_list = Qnil;
29739 staticpro (&mode_line_string_list);
29740 mode_line_string_face = Qnil;
29741 staticpro (&mode_line_string_face);
29742 mode_line_string_face_prop = Qnil;
29743 staticpro (&mode_line_string_face_prop);
29744 Vmode_line_unwind_vector = Qnil;
29745 staticpro (&Vmode_line_unwind_vector);
29746
29747 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29748
29749 help_echo_string = Qnil;
29750 staticpro (&help_echo_string);
29751 help_echo_object = Qnil;
29752 staticpro (&help_echo_object);
29753 help_echo_window = Qnil;
29754 staticpro (&help_echo_window);
29755 previous_help_echo_string = Qnil;
29756 staticpro (&previous_help_echo_string);
29757 help_echo_pos = -1;
29758
29759 DEFSYM (Qright_to_left, "right-to-left");
29760 DEFSYM (Qleft_to_right, "left-to-right");
29761
29762 #ifdef HAVE_WINDOW_SYSTEM
29763 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29764 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29765 For example, if a block cursor is over a tab, it will be drawn as
29766 wide as that tab on the display. */);
29767 x_stretch_cursor_p = 0;
29768 #endif
29769
29770 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29771 doc: /* Non-nil means highlight trailing whitespace.
29772 The face used for trailing whitespace is `trailing-whitespace'. */);
29773 Vshow_trailing_whitespace = Qnil;
29774
29775 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29776 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29777 If the value is t, Emacs highlights non-ASCII chars which have the
29778 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29779 or `escape-glyph' face respectively.
29780
29781 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29782 U+2011 (non-breaking hyphen) are affected.
29783
29784 Any other non-nil value means to display these characters as a escape
29785 glyph followed by an ordinary space or hyphen.
29786
29787 A value of nil means no special handling of these characters. */);
29788 Vnobreak_char_display = Qt;
29789
29790 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29791 doc: /* The pointer shape to show in void text areas.
29792 A value of nil means to show the text pointer. Other options are `arrow',
29793 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29794 Vvoid_text_area_pointer = Qarrow;
29795
29796 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29797 doc: /* Non-nil means don't actually do any redisplay.
29798 This is used for internal purposes. */);
29799 Vinhibit_redisplay = Qnil;
29800
29801 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29802 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29803 Vglobal_mode_string = Qnil;
29804
29805 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29806 doc: /* Marker for where to display an arrow on top of the buffer text.
29807 This must be the beginning of a line in order to work.
29808 See also `overlay-arrow-string'. */);
29809 Voverlay_arrow_position = Qnil;
29810
29811 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29812 doc: /* String to display as an arrow in non-window frames.
29813 See also `overlay-arrow-position'. */);
29814 Voverlay_arrow_string = build_pure_c_string ("=>");
29815
29816 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29817 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29818 The symbols on this list are examined during redisplay to determine
29819 where to display overlay arrows. */);
29820 Voverlay_arrow_variable_list
29821 = list1 (intern_c_string ("overlay-arrow-position"));
29822
29823 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29824 doc: /* The number of lines to try scrolling a window by when point moves out.
29825 If that fails to bring point back on frame, point is centered instead.
29826 If this is zero, point is always centered after it moves off frame.
29827 If you want scrolling to always be a line at a time, you should set
29828 `scroll-conservatively' to a large value rather than set this to 1. */);
29829
29830 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29831 doc: /* Scroll up to this many lines, to bring point back on screen.
29832 If point moves off-screen, redisplay will scroll by up to
29833 `scroll-conservatively' lines in order to bring point just barely
29834 onto the screen again. If that cannot be done, then redisplay
29835 recenters point as usual.
29836
29837 If the value is greater than 100, redisplay will never recenter point,
29838 but will always scroll just enough text to bring point into view, even
29839 if you move far away.
29840
29841 A value of zero means always recenter point if it moves off screen. */);
29842 scroll_conservatively = 0;
29843
29844 DEFVAR_INT ("scroll-margin", scroll_margin,
29845 doc: /* Number of lines of margin at the top and bottom of a window.
29846 Recenter the window whenever point gets within this many lines
29847 of the top or bottom of the window. */);
29848 scroll_margin = 0;
29849
29850 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29851 doc: /* Pixels per inch value for non-window system displays.
29852 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29853 Vdisplay_pixels_per_inch = make_float (72.0);
29854
29855 #ifdef GLYPH_DEBUG
29856 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29857 #endif
29858
29859 DEFVAR_LISP ("truncate-partial-width-windows",
29860 Vtruncate_partial_width_windows,
29861 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29862 For an integer value, truncate lines in each window narrower than the
29863 full frame width, provided the window width is less than that integer;
29864 otherwise, respect the value of `truncate-lines'.
29865
29866 For any other non-nil value, truncate lines in all windows that do
29867 not span the full frame width.
29868
29869 A value of nil means to respect the value of `truncate-lines'.
29870
29871 If `word-wrap' is enabled, you might want to reduce this. */);
29872 Vtruncate_partial_width_windows = make_number (50);
29873
29874 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29875 doc: /* Maximum buffer size for which line number should be displayed.
29876 If the buffer is bigger than this, the line number does not appear
29877 in the mode line. A value of nil means no limit. */);
29878 Vline_number_display_limit = Qnil;
29879
29880 DEFVAR_INT ("line-number-display-limit-width",
29881 line_number_display_limit_width,
29882 doc: /* Maximum line width (in characters) for line number display.
29883 If the average length of the lines near point is bigger than this, then the
29884 line number may be omitted from the mode line. */);
29885 line_number_display_limit_width = 200;
29886
29887 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29888 doc: /* Non-nil means highlight region even in nonselected windows. */);
29889 highlight_nonselected_windows = 0;
29890
29891 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29892 doc: /* Non-nil if more than one frame is visible on this display.
29893 Minibuffer-only frames don't count, but iconified frames do.
29894 This variable is not guaranteed to be accurate except while processing
29895 `frame-title-format' and `icon-title-format'. */);
29896
29897 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29898 doc: /* Template for displaying the title bar of visible frames.
29899 \(Assuming the window manager supports this feature.)
29900
29901 This variable has the same structure as `mode-line-format', except that
29902 the %c and %l constructs are ignored. It is used only on frames for
29903 which no explicit name has been set \(see `modify-frame-parameters'). */);
29904
29905 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29906 doc: /* Template for displaying the title bar of an iconified frame.
29907 \(Assuming the window manager supports this feature.)
29908 This variable has the same structure as `mode-line-format' (which see),
29909 and is used only on frames for which no explicit name has been set
29910 \(see `modify-frame-parameters'). */);
29911 Vicon_title_format
29912 = Vframe_title_format
29913 = listn (CONSTYPE_PURE, 3,
29914 intern_c_string ("multiple-frames"),
29915 build_pure_c_string ("%b"),
29916 listn (CONSTYPE_PURE, 4,
29917 empty_unibyte_string,
29918 intern_c_string ("invocation-name"),
29919 build_pure_c_string ("@"),
29920 intern_c_string ("system-name")));
29921
29922 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29923 doc: /* Maximum number of lines to keep in the message log buffer.
29924 If nil, disable message logging. If t, log messages but don't truncate
29925 the buffer when it becomes large. */);
29926 Vmessage_log_max = make_number (1000);
29927
29928 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29929 doc: /* Functions called before redisplay, if window sizes have changed.
29930 The value should be a list of functions that take one argument.
29931 Just before redisplay, for each frame, if any of its windows have changed
29932 size since the last redisplay, or have been split or deleted,
29933 all the functions in the list are called, with the frame as argument. */);
29934 Vwindow_size_change_functions = Qnil;
29935
29936 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29937 doc: /* List of functions to call before redisplaying a window with scrolling.
29938 Each function is called with two arguments, the window and its new
29939 display-start position. Note that these functions are also called by
29940 `set-window-buffer'. Also note that the value of `window-end' is not
29941 valid when these functions are called.
29942
29943 Warning: Do not use this feature to alter the way the window
29944 is scrolled. It is not designed for that, and such use probably won't
29945 work. */);
29946 Vwindow_scroll_functions = Qnil;
29947
29948 DEFVAR_LISP ("window-text-change-functions",
29949 Vwindow_text_change_functions,
29950 doc: /* Functions to call in redisplay when text in the window might change. */);
29951 Vwindow_text_change_functions = Qnil;
29952
29953 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29954 doc: /* Functions called when redisplay of a window reaches the end trigger.
29955 Each function is called with two arguments, the window and the end trigger value.
29956 See `set-window-redisplay-end-trigger'. */);
29957 Vredisplay_end_trigger_functions = Qnil;
29958
29959 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29960 doc: /* Non-nil means autoselect window with mouse pointer.
29961 If nil, do not autoselect windows.
29962 A positive number means delay autoselection by that many seconds: a
29963 window is autoselected only after the mouse has remained in that
29964 window for the duration of the delay.
29965 A negative number has a similar effect, but causes windows to be
29966 autoselected only after the mouse has stopped moving. \(Because of
29967 the way Emacs compares mouse events, you will occasionally wait twice
29968 that time before the window gets selected.\)
29969 Any other value means to autoselect window instantaneously when the
29970 mouse pointer enters it.
29971
29972 Autoselection selects the minibuffer only if it is active, and never
29973 unselects the minibuffer if it is active.
29974
29975 When customizing this variable make sure that the actual value of
29976 `focus-follows-mouse' matches the behavior of your window manager. */);
29977 Vmouse_autoselect_window = Qnil;
29978
29979 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29980 doc: /* Non-nil means automatically resize tool-bars.
29981 This dynamically changes the tool-bar's height to the minimum height
29982 that is needed to make all tool-bar items visible.
29983 If value is `grow-only', the tool-bar's height is only increased
29984 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29985 Vauto_resize_tool_bars = Qt;
29986
29987 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29988 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29989 auto_raise_tool_bar_buttons_p = 1;
29990
29991 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29992 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29993 make_cursor_line_fully_visible_p = 1;
29994
29995 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29996 doc: /* Border below tool-bar in pixels.
29997 If an integer, use it as the height of the border.
29998 If it is one of `internal-border-width' or `border-width', use the
29999 value of the corresponding frame parameter.
30000 Otherwise, no border is added below the tool-bar. */);
30001 Vtool_bar_border = Qinternal_border_width;
30002
30003 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30004 doc: /* Margin around tool-bar buttons in pixels.
30005 If an integer, use that for both horizontal and vertical margins.
30006 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30007 HORZ specifying the horizontal margin, and VERT specifying the
30008 vertical margin. */);
30009 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30010
30011 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30012 doc: /* Relief thickness of tool-bar buttons. */);
30013 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30014
30015 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30016 doc: /* Tool bar style to use.
30017 It can be one of
30018 image - show images only
30019 text - show text only
30020 both - show both, text below image
30021 both-horiz - show text to the right of the image
30022 text-image-horiz - show text to the left of the image
30023 any other - use system default or image if no system default.
30024
30025 This variable only affects the GTK+ toolkit version of Emacs. */);
30026 Vtool_bar_style = Qnil;
30027
30028 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30029 doc: /* Maximum number of characters a label can have to be shown.
30030 The tool bar style must also show labels for this to have any effect, see
30031 `tool-bar-style'. */);
30032 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30033
30034 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30035 doc: /* List of functions to call to fontify regions of text.
30036 Each function is called with one argument POS. Functions must
30037 fontify a region starting at POS in the current buffer, and give
30038 fontified regions the property `fontified'. */);
30039 Vfontification_functions = Qnil;
30040 Fmake_variable_buffer_local (Qfontification_functions);
30041
30042 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30043 unibyte_display_via_language_environment,
30044 doc: /* Non-nil means display unibyte text according to language environment.
30045 Specifically, this means that raw bytes in the range 160-255 decimal
30046 are displayed by converting them to the equivalent multibyte characters
30047 according to the current language environment. As a result, they are
30048 displayed according to the current fontset.
30049
30050 Note that this variable affects only how these bytes are displayed,
30051 but does not change the fact they are interpreted as raw bytes. */);
30052 unibyte_display_via_language_environment = 0;
30053
30054 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30055 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30056 If a float, it specifies a fraction of the mini-window frame's height.
30057 If an integer, it specifies a number of lines. */);
30058 Vmax_mini_window_height = make_float (0.25);
30059
30060 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30061 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30062 A value of nil means don't automatically resize mini-windows.
30063 A value of t means resize them to fit the text displayed in them.
30064 A value of `grow-only', the default, means let mini-windows grow only;
30065 they return to their normal size when the minibuffer is closed, or the
30066 echo area becomes empty. */);
30067 Vresize_mini_windows = Qgrow_only;
30068
30069 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30070 doc: /* Alist specifying how to blink the cursor off.
30071 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30072 `cursor-type' frame-parameter or variable equals ON-STATE,
30073 comparing using `equal', Emacs uses OFF-STATE to specify
30074 how to blink it off. ON-STATE and OFF-STATE are values for
30075 the `cursor-type' frame parameter.
30076
30077 If a frame's ON-STATE has no entry in this list,
30078 the frame's other specifications determine how to blink the cursor off. */);
30079 Vblink_cursor_alist = Qnil;
30080
30081 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30082 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30083 If non-nil, windows are automatically scrolled horizontally to make
30084 point visible. */);
30085 automatic_hscrolling_p = 1;
30086 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30087
30088 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30089 doc: /* How many columns away from the window edge point is allowed to get
30090 before automatic hscrolling will horizontally scroll the window. */);
30091 hscroll_margin = 5;
30092
30093 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30094 doc: /* How many columns to scroll the window when point gets too close to the edge.
30095 When point is less than `hscroll-margin' columns from the window
30096 edge, automatic hscrolling will scroll the window by the amount of columns
30097 determined by this variable. If its value is a positive integer, scroll that
30098 many columns. If it's a positive floating-point number, it specifies the
30099 fraction of the window's width to scroll. If it's nil or zero, point will be
30100 centered horizontally after the scroll. Any other value, including negative
30101 numbers, are treated as if the value were zero.
30102
30103 Automatic hscrolling always moves point outside the scroll margin, so if
30104 point was more than scroll step columns inside the margin, the window will
30105 scroll more than the value given by the scroll step.
30106
30107 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30108 and `scroll-right' overrides this variable's effect. */);
30109 Vhscroll_step = make_number (0);
30110
30111 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30112 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30113 Bind this around calls to `message' to let it take effect. */);
30114 message_truncate_lines = 0;
30115
30116 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30117 doc: /* Normal hook run to update the menu bar definitions.
30118 Redisplay runs this hook before it redisplays the menu bar.
30119 This is used to update submenus such as Buffers,
30120 whose contents depend on various data. */);
30121 Vmenu_bar_update_hook = Qnil;
30122
30123 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30124 doc: /* Frame for which we are updating a menu.
30125 The enable predicate for a menu binding should check this variable. */);
30126 Vmenu_updating_frame = Qnil;
30127
30128 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30129 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30130 inhibit_menubar_update = 0;
30131
30132 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30133 doc: /* Prefix prepended to all continuation lines at display time.
30134 The value may be a string, an image, or a stretch-glyph; it is
30135 interpreted in the same way as the value of a `display' text property.
30136
30137 This variable is overridden by any `wrap-prefix' text or overlay
30138 property.
30139
30140 To add a prefix to non-continuation lines, use `line-prefix'. */);
30141 Vwrap_prefix = Qnil;
30142 DEFSYM (Qwrap_prefix, "wrap-prefix");
30143 Fmake_variable_buffer_local (Qwrap_prefix);
30144
30145 DEFVAR_LISP ("line-prefix", Vline_prefix,
30146 doc: /* Prefix prepended to all non-continuation lines at display time.
30147 The value may be a string, an image, or a stretch-glyph; it is
30148 interpreted in the same way as the value of a `display' text property.
30149
30150 This variable is overridden by any `line-prefix' text or overlay
30151 property.
30152
30153 To add a prefix to continuation lines, use `wrap-prefix'. */);
30154 Vline_prefix = Qnil;
30155 DEFSYM (Qline_prefix, "line-prefix");
30156 Fmake_variable_buffer_local (Qline_prefix);
30157
30158 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30159 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30160 inhibit_eval_during_redisplay = 0;
30161
30162 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30163 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30164 inhibit_free_realized_faces = 0;
30165
30166 #ifdef GLYPH_DEBUG
30167 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30168 doc: /* Inhibit try_window_id display optimization. */);
30169 inhibit_try_window_id = 0;
30170
30171 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30172 doc: /* Inhibit try_window_reusing display optimization. */);
30173 inhibit_try_window_reusing = 0;
30174
30175 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30176 doc: /* Inhibit try_cursor_movement display optimization. */);
30177 inhibit_try_cursor_movement = 0;
30178 #endif /* GLYPH_DEBUG */
30179
30180 DEFVAR_INT ("overline-margin", overline_margin,
30181 doc: /* Space between overline and text, in pixels.
30182 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30183 margin to the character height. */);
30184 overline_margin = 2;
30185
30186 DEFVAR_INT ("underline-minimum-offset",
30187 underline_minimum_offset,
30188 doc: /* Minimum distance between baseline and underline.
30189 This can improve legibility of underlined text at small font sizes,
30190 particularly when using variable `x-use-underline-position-properties'
30191 with fonts that specify an UNDERLINE_POSITION relatively close to the
30192 baseline. The default value is 1. */);
30193 underline_minimum_offset = 1;
30194
30195 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30196 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30197 This feature only works when on a window system that can change
30198 cursor shapes. */);
30199 display_hourglass_p = 1;
30200
30201 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30202 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30203 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30204
30205 #ifdef HAVE_WINDOW_SYSTEM
30206 hourglass_atimer = NULL;
30207 hourglass_shown_p = 0;
30208 #endif /* HAVE_WINDOW_SYSTEM */
30209
30210 DEFSYM (Qglyphless_char, "glyphless-char");
30211 DEFSYM (Qhex_code, "hex-code");
30212 DEFSYM (Qempty_box, "empty-box");
30213 DEFSYM (Qthin_space, "thin-space");
30214 DEFSYM (Qzero_width, "zero-width");
30215
30216 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30217 doc: /* Function run just before redisplay.
30218 It is called with one argument, which is the set of windows that are to
30219 be redisplayed. This set can be nil (meaning, only the selected window),
30220 or t (meaning all windows). */);
30221 Vpre_redisplay_function = intern ("ignore");
30222
30223 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30224 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30225
30226 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30227 doc: /* Char-table defining glyphless characters.
30228 Each element, if non-nil, should be one of the following:
30229 an ASCII acronym string: display this string in a box
30230 `hex-code': display the hexadecimal code of a character in a box
30231 `empty-box': display as an empty box
30232 `thin-space': display as 1-pixel width space
30233 `zero-width': don't display
30234 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30235 display method for graphical terminals and text terminals respectively.
30236 GRAPHICAL and TEXT should each have one of the values listed above.
30237
30238 The char-table has one extra slot to control the display of a character for
30239 which no font is found. This slot only takes effect on graphical terminals.
30240 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30241 `thin-space'. The default is `empty-box'.
30242
30243 If a character has a non-nil entry in an active display table, the
30244 display table takes effect; in this case, Emacs does not consult
30245 `glyphless-char-display' at all. */);
30246 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30247 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30248 Qempty_box);
30249
30250 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30251 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30252 Vdebug_on_message = Qnil;
30253
30254 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30255 doc: /* */);
30256 Vredisplay__all_windows_cause
30257 = Fmake_vector (make_number (100), make_number (0));
30258
30259 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30260 doc: /* */);
30261 Vredisplay__mode_lines_cause
30262 = Fmake_vector (make_number (100), make_number (0));
30263 }
30264
30265
30266 /* Initialize this module when Emacs starts. */
30267
30268 void
30269 init_xdisp (void)
30270 {
30271 CHARPOS (this_line_start_pos) = 0;
30272
30273 if (!noninteractive)
30274 {
30275 struct window *m = XWINDOW (minibuf_window);
30276 Lisp_Object frame = m->frame;
30277 struct frame *f = XFRAME (frame);
30278 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30279 struct window *r = XWINDOW (root);
30280 int i;
30281
30282 echo_area_window = minibuf_window;
30283
30284 r->top_line = FRAME_TOP_MARGIN (f);
30285 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30286 r->total_cols = FRAME_COLS (f);
30287 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30288 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30289 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30290
30291 m->top_line = FRAME_LINES (f) - 1;
30292 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30293 m->total_cols = FRAME_COLS (f);
30294 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30295 m->total_lines = 1;
30296 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30297
30298 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30299 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30300 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30301
30302 /* The default ellipsis glyphs `...'. */
30303 for (i = 0; i < 3; ++i)
30304 default_invis_vector[i] = make_number ('.');
30305 }
30306
30307 {
30308 /* Allocate the buffer for frame titles.
30309 Also used for `format-mode-line'. */
30310 int size = 100;
30311 mode_line_noprop_buf = xmalloc (size);
30312 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30313 mode_line_noprop_ptr = mode_line_noprop_buf;
30314 mode_line_target = MODE_LINE_DISPLAY;
30315 }
30316
30317 help_echo_showing_p = 0;
30318 }
30319
30320 #ifdef HAVE_WINDOW_SYSTEM
30321
30322 /* Platform-independent portion of hourglass implementation. */
30323
30324 /* Cancel a currently active hourglass timer, and start a new one. */
30325 void
30326 start_hourglass (void)
30327 {
30328 struct timespec delay;
30329
30330 cancel_hourglass ();
30331
30332 if (INTEGERP (Vhourglass_delay)
30333 && XINT (Vhourglass_delay) > 0)
30334 delay = make_timespec (min (XINT (Vhourglass_delay),
30335 TYPE_MAXIMUM (time_t)),
30336 0);
30337 else if (FLOATP (Vhourglass_delay)
30338 && XFLOAT_DATA (Vhourglass_delay) > 0)
30339 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30340 else
30341 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30342
30343 #ifdef HAVE_NTGUI
30344 {
30345 extern void w32_note_current_window (void);
30346 w32_note_current_window ();
30347 }
30348 #endif /* HAVE_NTGUI */
30349
30350 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30351 show_hourglass, NULL);
30352 }
30353
30354
30355 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30356 shown. */
30357 void
30358 cancel_hourglass (void)
30359 {
30360 if (hourglass_atimer)
30361 {
30362 cancel_atimer (hourglass_atimer);
30363 hourglass_atimer = NULL;
30364 }
30365
30366 if (hourglass_shown_p)
30367 hide_hourglass ();
30368 }
30369
30370 #endif /* HAVE_WINDOW_SYSTEM */