Minor integer overflow fixes.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
304
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
308
309 #define INFINITY 10000000
310
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
323
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
326
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
330
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
333
334 static Lisp_Object Qfontification_functions;
335
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
339
340 /* Non-nil means don't actually do any redisplay. */
341
342 Lisp_Object Qinhibit_redisplay;
343
344 /* Names of text properties relevant for redisplay. */
345
346 Lisp_Object Qdisplay;
347
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
356
357 #ifdef HAVE_WINDOW_SYSTEM
358
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
361
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
369
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
373
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
377
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
389
390 /* Name of the face used to highlight trailing whitespace. */
391
392 static Lisp_Object Qtrailing_whitespace;
393
394 /* Name and number of the face used to highlight escape glyphs. */
395
396 static Lisp_Object Qescape_glyph;
397
398 /* Name and number of the face used to highlight non-breaking spaces. */
399
400 static Lisp_Object Qnobreak_space;
401
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
404
405 Lisp_Object Qimage;
406
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
411
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
414
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
417
418 bool noninteractive_need_newline;
419
420 /* Non-zero means print newline to message log before next message. */
421
422 static bool message_log_need_newline;
423
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
430 \f
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
435
436 static struct text_pos this_line_start_pos;
437
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
440
441 static struct text_pos this_line_end_pos;
442
443 /* The vertical positions and the height of this line. */
444
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
448
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
451
452 static int this_line_start_x;
453
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
457
458 static struct text_pos this_line_min_pos;
459
460 /* Buffer that this_line_.* variables are referring to. */
461
462 static struct buffer *this_line_buffer;
463
464
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
469
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
471
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
474
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
476
477 Lisp_Object Qmenu_bar_update_hook;
478
479 /* Nonzero if an overlay arrow has been displayed in this window. */
480
481 static bool overlay_arrow_seen;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
490
491 Lisp_Object echo_area_window;
492
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
497
498 static Lisp_Object Vmessage_stack;
499
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
502
503 static bool message_enable_multibyte;
504
505 /* Nonzero if we should redraw the mode lines on the next redisplay.
506 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
507 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
508 (the number used is then only used to track down the cause for this
509 full-redisplay). */
510
511 int update_mode_lines;
512
513 /* Nonzero if window sizes or contents other than selected-window have changed
514 since last redisplay that finished.
515 If it has value REDISPLAY_SOME, then only redisplay the windows where
516 the `redisplay' bit has been set. Otherwise, redisplay all windows
517 (the number used is then only used to track down the cause for this
518 full-redisplay). */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero after display_mode_line if %l was used and it displayed a
523 line number. */
524
525 static bool line_number_displayed;
526
527 /* The name of the *Messages* buffer, a string. */
528
529 static Lisp_Object Vmessages_buffer_name;
530
531 /* Current, index 0, and last displayed echo area message. Either
532 buffers from echo_buffers, or nil to indicate no message. */
533
534 Lisp_Object echo_area_buffer[2];
535
536 /* The buffers referenced from echo_area_buffer. */
537
538 static Lisp_Object echo_buffer[2];
539
540 /* A vector saved used in with_area_buffer to reduce consing. */
541
542 static Lisp_Object Vwith_echo_area_save_vector;
543
544 /* Non-zero means display_echo_area should display the last echo area
545 message again. Set by redisplay_preserve_echo_area. */
546
547 static bool display_last_displayed_message_p;
548
549 /* Nonzero if echo area is being used by print; zero if being used by
550 message. */
551
552 static bool message_buf_print;
553
554 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
555
556 static Lisp_Object Qinhibit_menubar_update;
557 static Lisp_Object Qmessage_truncate_lines;
558
559 /* Set to 1 in clear_message to make redisplay_internal aware
560 of an emptied echo area. */
561
562 static bool message_cleared_p;
563
564 /* A scratch glyph row with contents used for generating truncation
565 glyphs. Also used in direct_output_for_insert. */
566
567 #define MAX_SCRATCH_GLYPHS 100
568 static struct glyph_row scratch_glyph_row;
569 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
570
571 /* Ascent and height of the last line processed by move_it_to. */
572
573 static int last_max_ascent, last_height;
574
575 /* Non-zero if there's a help-echo in the echo area. */
576
577 bool help_echo_showing_p;
578
579 /* The maximum distance to look ahead for text properties. Values
580 that are too small let us call compute_char_face and similar
581 functions too often which is expensive. Values that are too large
582 let us call compute_char_face and alike too often because we
583 might not be interested in text properties that far away. */
584
585 #define TEXT_PROP_DISTANCE_LIMIT 100
586
587 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
588 iterator state and later restore it. This is needed because the
589 bidi iterator on bidi.c keeps a stacked cache of its states, which
590 is really a singleton. When we use scratch iterator objects to
591 move around the buffer, we can cause the bidi cache to be pushed or
592 popped, and therefore we need to restore the cache state when we
593 return to the original iterator. */
594 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
595 do { \
596 if (CACHE) \
597 bidi_unshelve_cache (CACHE, 1); \
598 ITCOPY = ITORIG; \
599 CACHE = bidi_shelve_cache (); \
600 } while (0)
601
602 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
603 do { \
604 if (pITORIG != pITCOPY) \
605 *(pITORIG) = *(pITCOPY); \
606 bidi_unshelve_cache (CACHE, 0); \
607 CACHE = NULL; \
608 } while (0)
609
610 /* Functions to mark elements as needing redisplay. */
611 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
612
613 void
614 redisplay_other_windows (void)
615 {
616 if (!windows_or_buffers_changed)
617 windows_or_buffers_changed = REDISPLAY_SOME;
618 }
619
620 void
621 wset_redisplay (struct window *w)
622 {
623 redisplay_other_windows ();
624 w->redisplay = true;
625 }
626
627 void
628 fset_redisplay (struct frame *f)
629 {
630 redisplay_other_windows ();
631 f->redisplay = true;
632 }
633
634 void
635 bset_redisplay (struct buffer *b)
636 {
637 int count = buffer_window_count (b);
638 if (count > 0)
639 {
640 /* ... it's visible in other window than selected, */
641 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
642 redisplay_other_windows ();
643 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
644 so that if we later set windows_or_buffers_changed, this buffer will
645 not be omitted. */
646 b->text->redisplay = true;
647 }
648 }
649
650 void
651 bset_update_mode_line (struct buffer *b)
652 {
653 if (!update_mode_lines)
654 update_mode_lines = REDISPLAY_SOME;
655 b->text->redisplay = true;
656 }
657
658 #ifdef GLYPH_DEBUG
659
660 /* Non-zero means print traces of redisplay if compiled with
661 GLYPH_DEBUG defined. */
662
663 int trace_redisplay_p;
664
665 #endif /* GLYPH_DEBUG */
666
667 #ifdef DEBUG_TRACE_MOVE
668 /* Non-zero means trace with TRACE_MOVE to stderr. */
669 int trace_move;
670
671 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
672 #else
673 #define TRACE_MOVE(x) (void) 0
674 #endif
675
676 static Lisp_Object Qauto_hscroll_mode;
677
678 /* Buffer being redisplayed -- for redisplay_window_error. */
679
680 static struct buffer *displayed_buffer;
681
682 /* Value returned from text property handlers (see below). */
683
684 enum prop_handled
685 {
686 HANDLED_NORMALLY,
687 HANDLED_RECOMPUTE_PROPS,
688 HANDLED_OVERLAY_STRING_CONSUMED,
689 HANDLED_RETURN
690 };
691
692 /* A description of text properties that redisplay is interested
693 in. */
694
695 struct props
696 {
697 /* The name of the property. */
698 Lisp_Object *name;
699
700 /* A unique index for the property. */
701 enum prop_idx idx;
702
703 /* A handler function called to set up iterator IT from the property
704 at IT's current position. Value is used to steer handle_stop. */
705 enum prop_handled (*handler) (struct it *it);
706 };
707
708 static enum prop_handled handle_face_prop (struct it *);
709 static enum prop_handled handle_invisible_prop (struct it *);
710 static enum prop_handled handle_display_prop (struct it *);
711 static enum prop_handled handle_composition_prop (struct it *);
712 static enum prop_handled handle_overlay_change (struct it *);
713 static enum prop_handled handle_fontified_prop (struct it *);
714
715 /* Properties handled by iterators. */
716
717 static struct props it_props[] =
718 {
719 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
720 /* Handle `face' before `display' because some sub-properties of
721 `display' need to know the face. */
722 {&Qface, FACE_PROP_IDX, handle_face_prop},
723 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
724 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
725 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
726 {NULL, 0, NULL}
727 };
728
729 /* Value is the position described by X. If X is a marker, value is
730 the marker_position of X. Otherwise, value is X. */
731
732 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
733
734 /* Enumeration returned by some move_it_.* functions internally. */
735
736 enum move_it_result
737 {
738 /* Not used. Undefined value. */
739 MOVE_UNDEFINED,
740
741 /* Move ended at the requested buffer position or ZV. */
742 MOVE_POS_MATCH_OR_ZV,
743
744 /* Move ended at the requested X pixel position. */
745 MOVE_X_REACHED,
746
747 /* Move within a line ended at the end of a line that must be
748 continued. */
749 MOVE_LINE_CONTINUED,
750
751 /* Move within a line ended at the end of a line that would
752 be displayed truncated. */
753 MOVE_LINE_TRUNCATED,
754
755 /* Move within a line ended at a line end. */
756 MOVE_NEWLINE_OR_CR
757 };
758
759 /* This counter is used to clear the face cache every once in a while
760 in redisplay_internal. It is incremented for each redisplay.
761 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
762 cleared. */
763
764 #define CLEAR_FACE_CACHE_COUNT 500
765 static int clear_face_cache_count;
766
767 /* Similarly for the image cache. */
768
769 #ifdef HAVE_WINDOW_SYSTEM
770 #define CLEAR_IMAGE_CACHE_COUNT 101
771 static int clear_image_cache_count;
772
773 /* Null glyph slice */
774 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
775 #endif
776
777 /* True while redisplay_internal is in progress. */
778
779 bool redisplaying_p;
780
781 static Lisp_Object Qinhibit_free_realized_faces;
782 static Lisp_Object Qmode_line_default_help_echo;
783
784 /* If a string, XTread_socket generates an event to display that string.
785 (The display is done in read_char.) */
786
787 Lisp_Object help_echo_string;
788 Lisp_Object help_echo_window;
789 Lisp_Object help_echo_object;
790 ptrdiff_t help_echo_pos;
791
792 /* Temporary variable for XTread_socket. */
793
794 Lisp_Object previous_help_echo_string;
795
796 /* Platform-independent portion of hourglass implementation. */
797
798 #ifdef HAVE_WINDOW_SYSTEM
799
800 /* Non-zero means an hourglass cursor is currently shown. */
801 bool hourglass_shown_p;
802
803 /* If non-null, an asynchronous timer that, when it expires, displays
804 an hourglass cursor on all frames. */
805 struct atimer *hourglass_atimer;
806
807 #endif /* HAVE_WINDOW_SYSTEM */
808
809 /* Name of the face used to display glyphless characters. */
810 static Lisp_Object Qglyphless_char;
811
812 /* Symbol for the purpose of Vglyphless_char_display. */
813 static Lisp_Object Qglyphless_char_display;
814
815 /* Method symbols for Vglyphless_char_display. */
816 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
817
818 /* Default number of seconds to wait before displaying an hourglass
819 cursor. */
820 #define DEFAULT_HOURGLASS_DELAY 1
821
822 #ifdef HAVE_WINDOW_SYSTEM
823
824 /* Default pixel width of `thin-space' display method. */
825 #define THIN_SPACE_WIDTH 1
826
827 #endif /* HAVE_WINDOW_SYSTEM */
828
829 /* Function prototypes. */
830
831 static void setup_for_ellipsis (struct it *, int);
832 static void set_iterator_to_next (struct it *, int);
833 static void mark_window_display_accurate_1 (struct window *, int);
834 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
835 static int display_prop_string_p (Lisp_Object, Lisp_Object);
836 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
837 static int cursor_row_p (struct glyph_row *);
838 static int redisplay_mode_lines (Lisp_Object, bool);
839 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
840
841 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
842
843 static void handle_line_prefix (struct it *);
844
845 static void pint2str (char *, int, ptrdiff_t);
846 static void pint2hrstr (char *, int, ptrdiff_t);
847 static struct text_pos run_window_scroll_functions (Lisp_Object,
848 struct text_pos);
849 static int text_outside_line_unchanged_p (struct window *,
850 ptrdiff_t, ptrdiff_t);
851 static void store_mode_line_noprop_char (char);
852 static int store_mode_line_noprop (const char *, int, int);
853 static void handle_stop (struct it *);
854 static void handle_stop_backwards (struct it *, ptrdiff_t);
855 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
856 static void ensure_echo_area_buffers (void);
857 static void unwind_with_echo_area_buffer (Lisp_Object);
858 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
859 static int with_echo_area_buffer (struct window *, int,
860 int (*) (ptrdiff_t, Lisp_Object),
861 ptrdiff_t, Lisp_Object);
862 static void clear_garbaged_frames (void);
863 static int current_message_1 (ptrdiff_t, Lisp_Object);
864 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
865 static void set_message (Lisp_Object);
866 static int set_message_1 (ptrdiff_t, Lisp_Object);
867 static int display_echo_area (struct window *);
868 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
869 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
870 static void unwind_redisplay (void);
871 static int string_char_and_length (const unsigned char *, int *);
872 static struct text_pos display_prop_end (struct it *, Lisp_Object,
873 struct text_pos);
874 static int compute_window_start_on_continuation_line (struct window *);
875 static void insert_left_trunc_glyphs (struct it *);
876 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
877 Lisp_Object);
878 static void extend_face_to_end_of_line (struct it *);
879 static int append_space_for_newline (struct it *, int);
880 static int cursor_row_fully_visible_p (struct window *, int, int);
881 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
882 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
883 static int trailing_whitespace_p (ptrdiff_t);
884 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
885 static void push_it (struct it *, struct text_pos *);
886 static void iterate_out_of_display_property (struct it *);
887 static void pop_it (struct it *);
888 static void sync_frame_with_window_matrix_rows (struct window *);
889 static void redisplay_internal (void);
890 static int echo_area_display (int);
891 static void redisplay_windows (Lisp_Object);
892 static void redisplay_window (Lisp_Object, bool);
893 static Lisp_Object redisplay_window_error (Lisp_Object);
894 static Lisp_Object redisplay_window_0 (Lisp_Object);
895 static Lisp_Object redisplay_window_1 (Lisp_Object);
896 static int set_cursor_from_row (struct window *, struct glyph_row *,
897 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
898 int, int);
899 static int update_menu_bar (struct frame *, int, int);
900 static int try_window_reusing_current_matrix (struct window *);
901 static int try_window_id (struct window *);
902 static int display_line (struct it *);
903 static int display_mode_lines (struct window *);
904 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
905 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
906 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
907 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
908 static void display_menu_bar (struct window *);
909 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
910 ptrdiff_t *);
911 static int display_string (const char *, Lisp_Object, Lisp_Object,
912 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
913 static void compute_line_metrics (struct it *);
914 static void run_redisplay_end_trigger_hook (struct it *);
915 static int get_overlay_strings (struct it *, ptrdiff_t);
916 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
917 static void next_overlay_string (struct it *);
918 static void reseat (struct it *, struct text_pos, int);
919 static void reseat_1 (struct it *, struct text_pos, int);
920 static void back_to_previous_visible_line_start (struct it *);
921 static void reseat_at_next_visible_line_start (struct it *, int);
922 static int next_element_from_ellipsis (struct it *);
923 static int next_element_from_display_vector (struct it *);
924 static int next_element_from_string (struct it *);
925 static int next_element_from_c_string (struct it *);
926 static int next_element_from_buffer (struct it *);
927 static int next_element_from_composition (struct it *);
928 static int next_element_from_image (struct it *);
929 static int next_element_from_stretch (struct it *);
930 static void load_overlay_strings (struct it *, ptrdiff_t);
931 static int init_from_display_pos (struct it *, struct window *,
932 struct display_pos *);
933 static void reseat_to_string (struct it *, const char *,
934 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
935 static int get_next_display_element (struct it *);
936 static enum move_it_result
937 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
938 enum move_operation_enum);
939 static void get_visually_first_element (struct it *);
940 static void init_to_row_start (struct it *, struct window *,
941 struct glyph_row *);
942 static int init_to_row_end (struct it *, struct window *,
943 struct glyph_row *);
944 static void back_to_previous_line_start (struct it *);
945 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
946 static struct text_pos string_pos_nchars_ahead (struct text_pos,
947 Lisp_Object, ptrdiff_t);
948 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
949 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
950 static ptrdiff_t number_of_chars (const char *, bool);
951 static void compute_stop_pos (struct it *);
952 static void compute_string_pos (struct text_pos *, struct text_pos,
953 Lisp_Object);
954 static int face_before_or_after_it_pos (struct it *, int);
955 static ptrdiff_t next_overlay_change (ptrdiff_t);
956 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
957 Lisp_Object, struct text_pos *, ptrdiff_t, int);
958 static int handle_single_display_spec (struct it *, Lisp_Object,
959 Lisp_Object, Lisp_Object,
960 struct text_pos *, ptrdiff_t, int, int);
961 static int underlying_face_id (struct it *);
962 static int in_ellipses_for_invisible_text_p (struct display_pos *,
963 struct window *);
964
965 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
966 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
967
968 #ifdef HAVE_WINDOW_SYSTEM
969
970 static void x_consider_frame_title (Lisp_Object);
971 static void update_tool_bar (struct frame *, int);
972 static int redisplay_tool_bar (struct frame *);
973 static void x_draw_bottom_divider (struct window *w);
974 static void notice_overwritten_cursor (struct window *,
975 enum glyph_row_area,
976 int, int, int, int);
977 static void append_stretch_glyph (struct it *, Lisp_Object,
978 int, int, int);
979
980
981 #endif /* HAVE_WINDOW_SYSTEM */
982
983 static void produce_special_glyphs (struct it *, enum display_element_type);
984 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
985 static int coords_in_mouse_face_p (struct window *, int, int);
986
987
988 \f
989 /***********************************************************************
990 Window display dimensions
991 ***********************************************************************/
992
993 /* Return the bottom boundary y-position for text lines in window W.
994 This is the first y position at which a line cannot start.
995 It is relative to the top of the window.
996
997 This is the height of W minus the height of a mode line, if any. */
998
999 int
1000 window_text_bottom_y (struct window *w)
1001 {
1002 int height = WINDOW_PIXEL_HEIGHT (w);
1003
1004 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1005
1006 if (WINDOW_WANTS_MODELINE_P (w))
1007 height -= CURRENT_MODE_LINE_HEIGHT (w);
1008
1009 return height;
1010 }
1011
1012 /* Return the pixel width of display area AREA of window W.
1013 ANY_AREA means return the total width of W, not including
1014 fringes to the left and right of the window. */
1015
1016 int
1017 window_box_width (struct window *w, enum glyph_row_area area)
1018 {
1019 int pixels = w->pixel_width;
1020
1021 if (!w->pseudo_window_p)
1022 {
1023 pixels -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1024 pixels -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1025
1026 if (area == TEXT_AREA)
1027 pixels -= (WINDOW_MARGINS_WIDTH (w)
1028 + WINDOW_FRINGES_WIDTH (w));
1029 else if (area == LEFT_MARGIN_AREA)
1030 pixels = WINDOW_LEFT_MARGIN_WIDTH (w);
1031 else if (area == RIGHT_MARGIN_AREA)
1032 pixels = WINDOW_RIGHT_MARGIN_WIDTH (w);
1033 }
1034
1035 return pixels;
1036 }
1037
1038
1039 /* Return the pixel height of the display area of window W, not
1040 including mode lines of W, if any. */
1041
1042 int
1043 window_box_height (struct window *w)
1044 {
1045 struct frame *f = XFRAME (w->frame);
1046 int height = WINDOW_PIXEL_HEIGHT (w);
1047
1048 eassert (height >= 0);
1049
1050 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1051
1052 /* Note: the code below that determines the mode-line/header-line
1053 height is essentially the same as that contained in the macro
1054 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1055 the appropriate glyph row has its `mode_line_p' flag set,
1056 and if it doesn't, uses estimate_mode_line_height instead. */
1057
1058 if (WINDOW_WANTS_MODELINE_P (w))
1059 {
1060 struct glyph_row *ml_row
1061 = (w->current_matrix && w->current_matrix->rows
1062 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1063 : 0);
1064 if (ml_row && ml_row->mode_line_p)
1065 height -= ml_row->height;
1066 else
1067 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1068 }
1069
1070 if (WINDOW_WANTS_HEADER_LINE_P (w))
1071 {
1072 struct glyph_row *hl_row
1073 = (w->current_matrix && w->current_matrix->rows
1074 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1075 : 0);
1076 if (hl_row && hl_row->mode_line_p)
1077 height -= hl_row->height;
1078 else
1079 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1080 }
1081
1082 /* With a very small font and a mode-line that's taller than
1083 default, we might end up with a negative height. */
1084 return max (0, height);
1085 }
1086
1087 /* Return the window-relative coordinate of the left edge of display
1088 area AREA of window W. ANY_AREA means return the left edge of the
1089 whole window, to the right of the left fringe of W. */
1090
1091 int
1092 window_box_left_offset (struct window *w, enum glyph_row_area area)
1093 {
1094 int x;
1095
1096 if (w->pseudo_window_p)
1097 return 0;
1098
1099 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1100
1101 if (area == TEXT_AREA)
1102 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1103 + window_box_width (w, LEFT_MARGIN_AREA));
1104 else if (area == RIGHT_MARGIN_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA)
1107 + window_box_width (w, TEXT_AREA)
1108 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1109 ? 0
1110 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1111 else if (area == LEFT_MARGIN_AREA
1112 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1113 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1114
1115 return x;
1116 }
1117
1118
1119 /* Return the window-relative coordinate of the right edge of display
1120 area AREA of window W. ANY_AREA means return the right edge of the
1121 whole window, to the left of the right fringe of W. */
1122
1123 int
1124 window_box_right_offset (struct window *w, enum glyph_row_area area)
1125 {
1126 return window_box_left_offset (w, area) + window_box_width (w, area);
1127 }
1128
1129 /* Return the frame-relative coordinate of the left edge of display
1130 area AREA of window W. ANY_AREA means return the left edge of the
1131 whole window, to the right of the left fringe of W. */
1132
1133 int
1134 window_box_left (struct window *w, enum glyph_row_area area)
1135 {
1136 struct frame *f = XFRAME (w->frame);
1137 int x;
1138
1139 if (w->pseudo_window_p)
1140 return FRAME_INTERNAL_BORDER_WIDTH (f);
1141
1142 x = (WINDOW_LEFT_EDGE_X (w)
1143 + window_box_left_offset (w, area));
1144
1145 return x;
1146 }
1147
1148
1149 /* Return the frame-relative coordinate of the right edge of display
1150 area AREA of window W. ANY_AREA means return the right edge of the
1151 whole window, to the left of the right fringe of W. */
1152
1153 int
1154 window_box_right (struct window *w, enum glyph_row_area area)
1155 {
1156 return window_box_left (w, area) + window_box_width (w, area);
1157 }
1158
1159 /* Get the bounding box of the display area AREA of window W, without
1160 mode lines, in frame-relative coordinates. ANY_AREA means the
1161 whole window, not including the left and right fringes of
1162 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1163 coordinates of the upper-left corner of the box. Return in
1164 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1165
1166 void
1167 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1168 int *box_y, int *box_width, int *box_height)
1169 {
1170 if (box_width)
1171 *box_width = window_box_width (w, area);
1172 if (box_height)
1173 *box_height = window_box_height (w);
1174 if (box_x)
1175 *box_x = window_box_left (w, area);
1176 if (box_y)
1177 {
1178 *box_y = WINDOW_TOP_EDGE_Y (w);
1179 if (WINDOW_WANTS_HEADER_LINE_P (w))
1180 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1181 }
1182 }
1183
1184 #ifdef HAVE_WINDOW_SYSTEM
1185
1186 /* Get the bounding box of the display area AREA of window W, without
1187 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1188 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1189 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1190 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1191 box. */
1192
1193 static void
1194 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1195 int *bottom_right_x, int *bottom_right_y)
1196 {
1197 window_box (w, ANY_AREA, top_left_x, top_left_y,
1198 bottom_right_x, bottom_right_y);
1199 *bottom_right_x += *top_left_x;
1200 *bottom_right_y += *top_left_y;
1201 }
1202
1203 #endif /* HAVE_WINDOW_SYSTEM */
1204
1205 /***********************************************************************
1206 Utilities
1207 ***********************************************************************/
1208
1209 /* Return the bottom y-position of the line the iterator IT is in.
1210 This can modify IT's settings. */
1211
1212 int
1213 line_bottom_y (struct it *it)
1214 {
1215 int line_height = it->max_ascent + it->max_descent;
1216 int line_top_y = it->current_y;
1217
1218 if (line_height == 0)
1219 {
1220 if (last_height)
1221 line_height = last_height;
1222 else if (IT_CHARPOS (*it) < ZV)
1223 {
1224 move_it_by_lines (it, 1);
1225 line_height = (it->max_ascent || it->max_descent
1226 ? it->max_ascent + it->max_descent
1227 : last_height);
1228 }
1229 else
1230 {
1231 struct glyph_row *row = it->glyph_row;
1232
1233 /* Use the default character height. */
1234 it->glyph_row = NULL;
1235 it->what = IT_CHARACTER;
1236 it->c = ' ';
1237 it->len = 1;
1238 PRODUCE_GLYPHS (it);
1239 line_height = it->ascent + it->descent;
1240 it->glyph_row = row;
1241 }
1242 }
1243
1244 return line_top_y + line_height;
1245 }
1246
1247 DEFUN ("line-pixel-height", Fline_pixel_height,
1248 Sline_pixel_height, 0, 0, 0,
1249 doc: /* Return height in pixels of text line in the selected window.
1250
1251 Value is the height in pixels of the line at point. */)
1252 (void)
1253 {
1254 struct it it;
1255 struct text_pos pt;
1256 struct window *w = XWINDOW (selected_window);
1257
1258 SET_TEXT_POS (pt, PT, PT_BYTE);
1259 start_display (&it, w, pt);
1260 it.vpos = it.current_y = 0;
1261 last_height = 0;
1262 return make_number (line_bottom_y (&it));
1263 }
1264
1265 /* Return the default pixel height of text lines in window W. The
1266 value is the canonical height of the W frame's default font, plus
1267 any extra space required by the line-spacing variable or frame
1268 parameter.
1269
1270 Implementation note: this ignores any line-spacing text properties
1271 put on the newline characters. This is because those properties
1272 only affect the _screen_ line ending in the newline (i.e., in a
1273 continued line, only the last screen line will be affected), which
1274 means only a small number of lines in a buffer can ever use this
1275 feature. Since this function is used to compute the default pixel
1276 equivalent of text lines in a window, we can safely ignore those
1277 few lines. For the same reasons, we ignore the line-height
1278 properties. */
1279 int
1280 default_line_pixel_height (struct window *w)
1281 {
1282 struct frame *f = WINDOW_XFRAME (w);
1283 int height = FRAME_LINE_HEIGHT (f);
1284
1285 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1286 {
1287 struct buffer *b = XBUFFER (w->contents);
1288 Lisp_Object val = BVAR (b, extra_line_spacing);
1289
1290 if (NILP (val))
1291 val = BVAR (&buffer_defaults, extra_line_spacing);
1292 if (!NILP (val))
1293 {
1294 if (RANGED_INTEGERP (0, val, INT_MAX))
1295 height += XFASTINT (val);
1296 else if (FLOATP (val))
1297 {
1298 int addon = XFLOAT_DATA (val) * height + 0.5;
1299
1300 if (addon >= 0)
1301 height += addon;
1302 }
1303 }
1304 else
1305 height += f->extra_line_spacing;
1306 }
1307
1308 return height;
1309 }
1310
1311 /* Subroutine of pos_visible_p below. Extracts a display string, if
1312 any, from the display spec given as its argument. */
1313 static Lisp_Object
1314 string_from_display_spec (Lisp_Object spec)
1315 {
1316 if (CONSP (spec))
1317 {
1318 while (CONSP (spec))
1319 {
1320 if (STRINGP (XCAR (spec)))
1321 return XCAR (spec);
1322 spec = XCDR (spec);
1323 }
1324 }
1325 else if (VECTORP (spec))
1326 {
1327 ptrdiff_t i;
1328
1329 for (i = 0; i < ASIZE (spec); i++)
1330 {
1331 if (STRINGP (AREF (spec, i)))
1332 return AREF (spec, i);
1333 }
1334 return Qnil;
1335 }
1336
1337 return spec;
1338 }
1339
1340
1341 /* Limit insanely large values of W->hscroll on frame F to the largest
1342 value that will still prevent first_visible_x and last_visible_x of
1343 'struct it' from overflowing an int. */
1344 static int
1345 window_hscroll_limited (struct window *w, struct frame *f)
1346 {
1347 ptrdiff_t window_hscroll = w->hscroll;
1348 int window_text_width = window_box_width (w, TEXT_AREA);
1349 int colwidth = FRAME_COLUMN_WIDTH (f);
1350
1351 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1352 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1353
1354 return window_hscroll;
1355 }
1356
1357 /* Return 1 if position CHARPOS is visible in window W.
1358 CHARPOS < 0 means return info about WINDOW_END position.
1359 If visible, set *X and *Y to pixel coordinates of top left corner.
1360 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1361 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1362
1363 int
1364 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1365 int *rtop, int *rbot, int *rowh, int *vpos)
1366 {
1367 struct it it;
1368 void *itdata = bidi_shelve_cache ();
1369 struct text_pos top;
1370 int visible_p = 0;
1371 struct buffer *old_buffer = NULL;
1372
1373 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1374 return visible_p;
1375
1376 if (XBUFFER (w->contents) != current_buffer)
1377 {
1378 old_buffer = current_buffer;
1379 set_buffer_internal_1 (XBUFFER (w->contents));
1380 }
1381
1382 SET_TEXT_POS_FROM_MARKER (top, w->start);
1383 /* Scrolling a minibuffer window via scroll bar when the echo area
1384 shows long text sometimes resets the minibuffer contents behind
1385 our backs. */
1386 if (CHARPOS (top) > ZV)
1387 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1388
1389 /* Compute exact mode line heights. */
1390 if (WINDOW_WANTS_MODELINE_P (w))
1391 w->mode_line_height
1392 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1393 BVAR (current_buffer, mode_line_format));
1394
1395 if (WINDOW_WANTS_HEADER_LINE_P (w))
1396 w->header_line_height
1397 = display_mode_line (w, HEADER_LINE_FACE_ID,
1398 BVAR (current_buffer, header_line_format));
1399
1400 start_display (&it, w, top);
1401 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1402 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1403
1404 if (charpos >= 0
1405 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1406 && IT_CHARPOS (it) >= charpos)
1407 /* When scanning backwards under bidi iteration, move_it_to
1408 stops at or _before_ CHARPOS, because it stops at or to
1409 the _right_ of the character at CHARPOS. */
1410 || (it.bidi_p && it.bidi_it.scan_dir == -1
1411 && IT_CHARPOS (it) <= charpos)))
1412 {
1413 /* We have reached CHARPOS, or passed it. How the call to
1414 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1415 or covered by a display property, move_it_to stops at the end
1416 of the invisible text, to the right of CHARPOS. (ii) If
1417 CHARPOS is in a display vector, move_it_to stops on its last
1418 glyph. */
1419 int top_x = it.current_x;
1420 int top_y = it.current_y;
1421 /* Calling line_bottom_y may change it.method, it.position, etc. */
1422 enum it_method it_method = it.method;
1423 int bottom_y = (last_height = 0, line_bottom_y (&it));
1424 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1425
1426 if (top_y < window_top_y)
1427 visible_p = bottom_y > window_top_y;
1428 else if (top_y < it.last_visible_y)
1429 visible_p = true;
1430 if (bottom_y >= it.last_visible_y
1431 && it.bidi_p && it.bidi_it.scan_dir == -1
1432 && IT_CHARPOS (it) < charpos)
1433 {
1434 /* When the last line of the window is scanned backwards
1435 under bidi iteration, we could be duped into thinking
1436 that we have passed CHARPOS, when in fact move_it_to
1437 simply stopped short of CHARPOS because it reached
1438 last_visible_y. To see if that's what happened, we call
1439 move_it_to again with a slightly larger vertical limit,
1440 and see if it actually moved vertically; if it did, we
1441 didn't really reach CHARPOS, which is beyond window end. */
1442 struct it save_it = it;
1443 /* Why 10? because we don't know how many canonical lines
1444 will the height of the next line(s) be. So we guess. */
1445 int ten_more_lines = 10 * default_line_pixel_height (w);
1446
1447 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1448 MOVE_TO_POS | MOVE_TO_Y);
1449 if (it.current_y > top_y)
1450 visible_p = 0;
1451
1452 it = save_it;
1453 }
1454 if (visible_p)
1455 {
1456 if (it_method == GET_FROM_DISPLAY_VECTOR)
1457 {
1458 /* We stopped on the last glyph of a display vector.
1459 Try and recompute. Hack alert! */
1460 if (charpos < 2 || top.charpos >= charpos)
1461 top_x = it.glyph_row->x;
1462 else
1463 {
1464 struct it it2, it2_prev;
1465 /* The idea is to get to the previous buffer
1466 position, consume the character there, and use
1467 the pixel coordinates we get after that. But if
1468 the previous buffer position is also displayed
1469 from a display vector, we need to consume all of
1470 the glyphs from that display vector. */
1471 start_display (&it2, w, top);
1472 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1473 /* If we didn't get to CHARPOS - 1, there's some
1474 replacing display property at that position, and
1475 we stopped after it. That is exactly the place
1476 whose coordinates we want. */
1477 if (IT_CHARPOS (it2) != charpos - 1)
1478 it2_prev = it2;
1479 else
1480 {
1481 /* Iterate until we get out of the display
1482 vector that displays the character at
1483 CHARPOS - 1. */
1484 do {
1485 get_next_display_element (&it2);
1486 PRODUCE_GLYPHS (&it2);
1487 it2_prev = it2;
1488 set_iterator_to_next (&it2, 1);
1489 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1490 && IT_CHARPOS (it2) < charpos);
1491 }
1492 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1493 || it2_prev.current_x > it2_prev.last_visible_x)
1494 top_x = it.glyph_row->x;
1495 else
1496 {
1497 top_x = it2_prev.current_x;
1498 top_y = it2_prev.current_y;
1499 }
1500 }
1501 }
1502 else if (IT_CHARPOS (it) != charpos)
1503 {
1504 Lisp_Object cpos = make_number (charpos);
1505 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1506 Lisp_Object string = string_from_display_spec (spec);
1507 struct text_pos tpos;
1508 int replacing_spec_p;
1509 bool newline_in_string
1510 = (STRINGP (string)
1511 && memchr (SDATA (string), '\n', SBYTES (string)));
1512
1513 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1514 replacing_spec_p
1515 = (!NILP (spec)
1516 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1517 charpos, FRAME_WINDOW_P (it.f)));
1518 /* The tricky code below is needed because there's a
1519 discrepancy between move_it_to and how we set cursor
1520 when PT is at the beginning of a portion of text
1521 covered by a display property or an overlay with a
1522 display property, or the display line ends in a
1523 newline from a display string. move_it_to will stop
1524 _after_ such display strings, whereas
1525 set_cursor_from_row conspires with cursor_row_p to
1526 place the cursor on the first glyph produced from the
1527 display string. */
1528
1529 /* We have overshoot PT because it is covered by a
1530 display property that replaces the text it covers.
1531 If the string includes embedded newlines, we are also
1532 in the wrong display line. Backtrack to the correct
1533 line, where the display property begins. */
1534 if (replacing_spec_p)
1535 {
1536 Lisp_Object startpos, endpos;
1537 EMACS_INT start, end;
1538 struct it it3;
1539 int it3_moved;
1540
1541 /* Find the first and the last buffer positions
1542 covered by the display string. */
1543 endpos =
1544 Fnext_single_char_property_change (cpos, Qdisplay,
1545 Qnil, Qnil);
1546 startpos =
1547 Fprevious_single_char_property_change (endpos, Qdisplay,
1548 Qnil, Qnil);
1549 start = XFASTINT (startpos);
1550 end = XFASTINT (endpos);
1551 /* Move to the last buffer position before the
1552 display property. */
1553 start_display (&it3, w, top);
1554 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1555 /* Move forward one more line if the position before
1556 the display string is a newline or if it is the
1557 rightmost character on a line that is
1558 continued or word-wrapped. */
1559 if (it3.method == GET_FROM_BUFFER
1560 && (it3.c == '\n'
1561 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1562 move_it_by_lines (&it3, 1);
1563 else if (move_it_in_display_line_to (&it3, -1,
1564 it3.current_x
1565 + it3.pixel_width,
1566 MOVE_TO_X)
1567 == MOVE_LINE_CONTINUED)
1568 {
1569 move_it_by_lines (&it3, 1);
1570 /* When we are under word-wrap, the #$@%!
1571 move_it_by_lines moves 2 lines, so we need to
1572 fix that up. */
1573 if (it3.line_wrap == WORD_WRAP)
1574 move_it_by_lines (&it3, -1);
1575 }
1576
1577 /* Record the vertical coordinate of the display
1578 line where we wound up. */
1579 top_y = it3.current_y;
1580 if (it3.bidi_p)
1581 {
1582 /* When characters are reordered for display,
1583 the character displayed to the left of the
1584 display string could be _after_ the display
1585 property in the logical order. Use the
1586 smallest vertical position of these two. */
1587 start_display (&it3, w, top);
1588 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1589 if (it3.current_y < top_y)
1590 top_y = it3.current_y;
1591 }
1592 /* Move from the top of the window to the beginning
1593 of the display line where the display string
1594 begins. */
1595 start_display (&it3, w, top);
1596 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1597 /* If it3_moved stays zero after the 'while' loop
1598 below, that means we already were at a newline
1599 before the loop (e.g., the display string begins
1600 with a newline), so we don't need to (and cannot)
1601 inspect the glyphs of it3.glyph_row, because
1602 PRODUCE_GLYPHS will not produce anything for a
1603 newline, and thus it3.glyph_row stays at its
1604 stale content it got at top of the window. */
1605 it3_moved = 0;
1606 /* Finally, advance the iterator until we hit the
1607 first display element whose character position is
1608 CHARPOS, or until the first newline from the
1609 display string, which signals the end of the
1610 display line. */
1611 while (get_next_display_element (&it3))
1612 {
1613 PRODUCE_GLYPHS (&it3);
1614 if (IT_CHARPOS (it3) == charpos
1615 || ITERATOR_AT_END_OF_LINE_P (&it3))
1616 break;
1617 it3_moved = 1;
1618 set_iterator_to_next (&it3, 0);
1619 }
1620 top_x = it3.current_x - it3.pixel_width;
1621 /* Normally, we would exit the above loop because we
1622 found the display element whose character
1623 position is CHARPOS. For the contingency that we
1624 didn't, and stopped at the first newline from the
1625 display string, move back over the glyphs
1626 produced from the string, until we find the
1627 rightmost glyph not from the string. */
1628 if (it3_moved
1629 && newline_in_string
1630 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1631 {
1632 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1633 + it3.glyph_row->used[TEXT_AREA];
1634
1635 while (EQ ((g - 1)->object, string))
1636 {
1637 --g;
1638 top_x -= g->pixel_width;
1639 }
1640 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1641 + it3.glyph_row->used[TEXT_AREA]);
1642 }
1643 }
1644 }
1645
1646 *x = top_x;
1647 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1648 *rtop = max (0, window_top_y - top_y);
1649 *rbot = max (0, bottom_y - it.last_visible_y);
1650 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1651 - max (top_y, window_top_y)));
1652 *vpos = it.vpos;
1653 }
1654 }
1655 else
1656 {
1657 /* We were asked to provide info about WINDOW_END. */
1658 struct it it2;
1659 void *it2data = NULL;
1660
1661 SAVE_IT (it2, it, it2data);
1662 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1663 move_it_by_lines (&it, 1);
1664 if (charpos < IT_CHARPOS (it)
1665 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1666 {
1667 visible_p = true;
1668 RESTORE_IT (&it2, &it2, it2data);
1669 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1670 *x = it2.current_x;
1671 *y = it2.current_y + it2.max_ascent - it2.ascent;
1672 *rtop = max (0, -it2.current_y);
1673 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1674 - it.last_visible_y));
1675 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1676 it.last_visible_y)
1677 - max (it2.current_y,
1678 WINDOW_HEADER_LINE_HEIGHT (w))));
1679 *vpos = it2.vpos;
1680 }
1681 else
1682 bidi_unshelve_cache (it2data, 1);
1683 }
1684 bidi_unshelve_cache (itdata, 0);
1685
1686 if (old_buffer)
1687 set_buffer_internal_1 (old_buffer);
1688
1689 if (visible_p && w->hscroll > 0)
1690 *x -=
1691 window_hscroll_limited (w, WINDOW_XFRAME (w))
1692 * WINDOW_FRAME_COLUMN_WIDTH (w);
1693
1694 #if 0
1695 /* Debugging code. */
1696 if (visible_p)
1697 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1698 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1699 else
1700 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1701 #endif
1702
1703 return visible_p;
1704 }
1705
1706
1707 /* Return the next character from STR. Return in *LEN the length of
1708 the character. This is like STRING_CHAR_AND_LENGTH but never
1709 returns an invalid character. If we find one, we return a `?', but
1710 with the length of the invalid character. */
1711
1712 static int
1713 string_char_and_length (const unsigned char *str, int *len)
1714 {
1715 int c;
1716
1717 c = STRING_CHAR_AND_LENGTH (str, *len);
1718 if (!CHAR_VALID_P (c))
1719 /* We may not change the length here because other places in Emacs
1720 don't use this function, i.e. they silently accept invalid
1721 characters. */
1722 c = '?';
1723
1724 return c;
1725 }
1726
1727
1728
1729 /* Given a position POS containing a valid character and byte position
1730 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1731
1732 static struct text_pos
1733 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1734 {
1735 eassert (STRINGP (string) && nchars >= 0);
1736
1737 if (STRING_MULTIBYTE (string))
1738 {
1739 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1740 int len;
1741
1742 while (nchars--)
1743 {
1744 string_char_and_length (p, &len);
1745 p += len;
1746 CHARPOS (pos) += 1;
1747 BYTEPOS (pos) += len;
1748 }
1749 }
1750 else
1751 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1752
1753 return pos;
1754 }
1755
1756
1757 /* Value is the text position, i.e. character and byte position,
1758 for character position CHARPOS in STRING. */
1759
1760 static struct text_pos
1761 string_pos (ptrdiff_t charpos, Lisp_Object string)
1762 {
1763 struct text_pos pos;
1764 eassert (STRINGP (string));
1765 eassert (charpos >= 0);
1766 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1767 return pos;
1768 }
1769
1770
1771 /* Value is a text position, i.e. character and byte position, for
1772 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1773 means recognize multibyte characters. */
1774
1775 static struct text_pos
1776 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1777 {
1778 struct text_pos pos;
1779
1780 eassert (s != NULL);
1781 eassert (charpos >= 0);
1782
1783 if (multibyte_p)
1784 {
1785 int len;
1786
1787 SET_TEXT_POS (pos, 0, 0);
1788 while (charpos--)
1789 {
1790 string_char_and_length ((const unsigned char *) s, &len);
1791 s += len;
1792 CHARPOS (pos) += 1;
1793 BYTEPOS (pos) += len;
1794 }
1795 }
1796 else
1797 SET_TEXT_POS (pos, charpos, charpos);
1798
1799 return pos;
1800 }
1801
1802
1803 /* Value is the number of characters in C string S. MULTIBYTE_P
1804 non-zero means recognize multibyte characters. */
1805
1806 static ptrdiff_t
1807 number_of_chars (const char *s, bool multibyte_p)
1808 {
1809 ptrdiff_t nchars;
1810
1811 if (multibyte_p)
1812 {
1813 ptrdiff_t rest = strlen (s);
1814 int len;
1815 const unsigned char *p = (const unsigned char *) s;
1816
1817 for (nchars = 0; rest > 0; ++nchars)
1818 {
1819 string_char_and_length (p, &len);
1820 rest -= len, p += len;
1821 }
1822 }
1823 else
1824 nchars = strlen (s);
1825
1826 return nchars;
1827 }
1828
1829
1830 /* Compute byte position NEWPOS->bytepos corresponding to
1831 NEWPOS->charpos. POS is a known position in string STRING.
1832 NEWPOS->charpos must be >= POS.charpos. */
1833
1834 static void
1835 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1836 {
1837 eassert (STRINGP (string));
1838 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1839
1840 if (STRING_MULTIBYTE (string))
1841 *newpos = string_pos_nchars_ahead (pos, string,
1842 CHARPOS (*newpos) - CHARPOS (pos));
1843 else
1844 BYTEPOS (*newpos) = CHARPOS (*newpos);
1845 }
1846
1847 /* EXPORT:
1848 Return an estimation of the pixel height of mode or header lines on
1849 frame F. FACE_ID specifies what line's height to estimate. */
1850
1851 int
1852 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1853 {
1854 #ifdef HAVE_WINDOW_SYSTEM
1855 if (FRAME_WINDOW_P (f))
1856 {
1857 int height = FONT_HEIGHT (FRAME_FONT (f));
1858
1859 /* This function is called so early when Emacs starts that the face
1860 cache and mode line face are not yet initialized. */
1861 if (FRAME_FACE_CACHE (f))
1862 {
1863 struct face *face = FACE_FROM_ID (f, face_id);
1864 if (face)
1865 {
1866 if (face->font)
1867 height = FONT_HEIGHT (face->font);
1868 if (face->box_line_width > 0)
1869 height += 2 * face->box_line_width;
1870 }
1871 }
1872
1873 return height;
1874 }
1875 #endif
1876
1877 return 1;
1878 }
1879
1880 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1881 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1882 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1883 not force the value into range. */
1884
1885 void
1886 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1887 int *x, int *y, NativeRectangle *bounds, int noclip)
1888 {
1889
1890 #ifdef HAVE_WINDOW_SYSTEM
1891 if (FRAME_WINDOW_P (f))
1892 {
1893 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1894 even for negative values. */
1895 if (pix_x < 0)
1896 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1897 if (pix_y < 0)
1898 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1899
1900 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1901 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1902
1903 if (bounds)
1904 STORE_NATIVE_RECT (*bounds,
1905 FRAME_COL_TO_PIXEL_X (f, pix_x),
1906 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1907 FRAME_COLUMN_WIDTH (f) - 1,
1908 FRAME_LINE_HEIGHT (f) - 1);
1909
1910 /* PXW: Should we clip pixels before converting to columns/lines? */
1911 if (!noclip)
1912 {
1913 if (pix_x < 0)
1914 pix_x = 0;
1915 else if (pix_x > FRAME_TOTAL_COLS (f))
1916 pix_x = FRAME_TOTAL_COLS (f);
1917
1918 if (pix_y < 0)
1919 pix_y = 0;
1920 else if (pix_y > FRAME_LINES (f))
1921 pix_y = FRAME_LINES (f);
1922 }
1923 }
1924 #endif
1925
1926 *x = pix_x;
1927 *y = pix_y;
1928 }
1929
1930
1931 /* Find the glyph under window-relative coordinates X/Y in window W.
1932 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1933 strings. Return in *HPOS and *VPOS the row and column number of
1934 the glyph found. Return in *AREA the glyph area containing X.
1935 Value is a pointer to the glyph found or null if X/Y is not on
1936 text, or we can't tell because W's current matrix is not up to
1937 date. */
1938
1939 static struct glyph *
1940 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1941 int *dx, int *dy, int *area)
1942 {
1943 struct glyph *glyph, *end;
1944 struct glyph_row *row = NULL;
1945 int x0, i;
1946
1947 /* Find row containing Y. Give up if some row is not enabled. */
1948 for (i = 0; i < w->current_matrix->nrows; ++i)
1949 {
1950 row = MATRIX_ROW (w->current_matrix, i);
1951 if (!row->enabled_p)
1952 return NULL;
1953 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1954 break;
1955 }
1956
1957 *vpos = i;
1958 *hpos = 0;
1959
1960 /* Give up if Y is not in the window. */
1961 if (i == w->current_matrix->nrows)
1962 return NULL;
1963
1964 /* Get the glyph area containing X. */
1965 if (w->pseudo_window_p)
1966 {
1967 *area = TEXT_AREA;
1968 x0 = 0;
1969 }
1970 else
1971 {
1972 if (x < window_box_left_offset (w, TEXT_AREA))
1973 {
1974 *area = LEFT_MARGIN_AREA;
1975 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1976 }
1977 else if (x < window_box_right_offset (w, TEXT_AREA))
1978 {
1979 *area = TEXT_AREA;
1980 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1981 }
1982 else
1983 {
1984 *area = RIGHT_MARGIN_AREA;
1985 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1986 }
1987 }
1988
1989 /* Find glyph containing X. */
1990 glyph = row->glyphs[*area];
1991 end = glyph + row->used[*area];
1992 x -= x0;
1993 while (glyph < end && x >= glyph->pixel_width)
1994 {
1995 x -= glyph->pixel_width;
1996 ++glyph;
1997 }
1998
1999 if (glyph == end)
2000 return NULL;
2001
2002 if (dx)
2003 {
2004 *dx = x;
2005 *dy = y - (row->y + row->ascent - glyph->ascent);
2006 }
2007
2008 *hpos = glyph - row->glyphs[*area];
2009 return glyph;
2010 }
2011
2012 /* Convert frame-relative x/y to coordinates relative to window W.
2013 Takes pseudo-windows into account. */
2014
2015 static void
2016 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2017 {
2018 if (w->pseudo_window_p)
2019 {
2020 /* A pseudo-window is always full-width, and starts at the
2021 left edge of the frame, plus a frame border. */
2022 struct frame *f = XFRAME (w->frame);
2023 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2024 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2025 }
2026 else
2027 {
2028 *x -= WINDOW_LEFT_EDGE_X (w);
2029 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2030 }
2031 }
2032
2033 #ifdef HAVE_WINDOW_SYSTEM
2034
2035 /* EXPORT:
2036 Return in RECTS[] at most N clipping rectangles for glyph string S.
2037 Return the number of stored rectangles. */
2038
2039 int
2040 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2041 {
2042 XRectangle r;
2043
2044 if (n <= 0)
2045 return 0;
2046
2047 if (s->row->full_width_p)
2048 {
2049 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2050 r.x = WINDOW_LEFT_EDGE_X (s->w);
2051 if (s->row->mode_line_p)
2052 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2053 else
2054 r.width = WINDOW_PIXEL_WIDTH (s->w);
2055
2056 /* Unless displaying a mode or menu bar line, which are always
2057 fully visible, clip to the visible part of the row. */
2058 if (s->w->pseudo_window_p)
2059 r.height = s->row->visible_height;
2060 else
2061 r.height = s->height;
2062 }
2063 else
2064 {
2065 /* This is a text line that may be partially visible. */
2066 r.x = window_box_left (s->w, s->area);
2067 r.width = window_box_width (s->w, s->area);
2068 r.height = s->row->visible_height;
2069 }
2070
2071 if (s->clip_head)
2072 if (r.x < s->clip_head->x)
2073 {
2074 if (r.width >= s->clip_head->x - r.x)
2075 r.width -= s->clip_head->x - r.x;
2076 else
2077 r.width = 0;
2078 r.x = s->clip_head->x;
2079 }
2080 if (s->clip_tail)
2081 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2082 {
2083 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2084 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2085 else
2086 r.width = 0;
2087 }
2088
2089 /* If S draws overlapping rows, it's sufficient to use the top and
2090 bottom of the window for clipping because this glyph string
2091 intentionally draws over other lines. */
2092 if (s->for_overlaps)
2093 {
2094 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2095 r.height = window_text_bottom_y (s->w) - r.y;
2096
2097 /* Alas, the above simple strategy does not work for the
2098 environments with anti-aliased text: if the same text is
2099 drawn onto the same place multiple times, it gets thicker.
2100 If the overlap we are processing is for the erased cursor, we
2101 take the intersection with the rectangle of the cursor. */
2102 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2103 {
2104 XRectangle rc, r_save = r;
2105
2106 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2107 rc.y = s->w->phys_cursor.y;
2108 rc.width = s->w->phys_cursor_width;
2109 rc.height = s->w->phys_cursor_height;
2110
2111 x_intersect_rectangles (&r_save, &rc, &r);
2112 }
2113 }
2114 else
2115 {
2116 /* Don't use S->y for clipping because it doesn't take partially
2117 visible lines into account. For example, it can be negative for
2118 partially visible lines at the top of a window. */
2119 if (!s->row->full_width_p
2120 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2121 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2122 else
2123 r.y = max (0, s->row->y);
2124 }
2125
2126 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2127
2128 /* If drawing the cursor, don't let glyph draw outside its
2129 advertised boundaries. Cleartype does this under some circumstances. */
2130 if (s->hl == DRAW_CURSOR)
2131 {
2132 struct glyph *glyph = s->first_glyph;
2133 int height, max_y;
2134
2135 if (s->x > r.x)
2136 {
2137 r.width -= s->x - r.x;
2138 r.x = s->x;
2139 }
2140 r.width = min (r.width, glyph->pixel_width);
2141
2142 /* If r.y is below window bottom, ensure that we still see a cursor. */
2143 height = min (glyph->ascent + glyph->descent,
2144 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2145 max_y = window_text_bottom_y (s->w) - height;
2146 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2147 if (s->ybase - glyph->ascent > max_y)
2148 {
2149 r.y = max_y;
2150 r.height = height;
2151 }
2152 else
2153 {
2154 /* Don't draw cursor glyph taller than our actual glyph. */
2155 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2156 if (height < r.height)
2157 {
2158 max_y = r.y + r.height;
2159 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2160 r.height = min (max_y - r.y, height);
2161 }
2162 }
2163 }
2164
2165 if (s->row->clip)
2166 {
2167 XRectangle r_save = r;
2168
2169 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2170 r.width = 0;
2171 }
2172
2173 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2174 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2175 {
2176 #ifdef CONVERT_FROM_XRECT
2177 CONVERT_FROM_XRECT (r, *rects);
2178 #else
2179 *rects = r;
2180 #endif
2181 return 1;
2182 }
2183 else
2184 {
2185 /* If we are processing overlapping and allowed to return
2186 multiple clipping rectangles, we exclude the row of the glyph
2187 string from the clipping rectangle. This is to avoid drawing
2188 the same text on the environment with anti-aliasing. */
2189 #ifdef CONVERT_FROM_XRECT
2190 XRectangle rs[2];
2191 #else
2192 XRectangle *rs = rects;
2193 #endif
2194 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2195
2196 if (s->for_overlaps & OVERLAPS_PRED)
2197 {
2198 rs[i] = r;
2199 if (r.y + r.height > row_y)
2200 {
2201 if (r.y < row_y)
2202 rs[i].height = row_y - r.y;
2203 else
2204 rs[i].height = 0;
2205 }
2206 i++;
2207 }
2208 if (s->for_overlaps & OVERLAPS_SUCC)
2209 {
2210 rs[i] = r;
2211 if (r.y < row_y + s->row->visible_height)
2212 {
2213 if (r.y + r.height > row_y + s->row->visible_height)
2214 {
2215 rs[i].y = row_y + s->row->visible_height;
2216 rs[i].height = r.y + r.height - rs[i].y;
2217 }
2218 else
2219 rs[i].height = 0;
2220 }
2221 i++;
2222 }
2223
2224 n = i;
2225 #ifdef CONVERT_FROM_XRECT
2226 for (i = 0; i < n; i++)
2227 CONVERT_FROM_XRECT (rs[i], rects[i]);
2228 #endif
2229 return n;
2230 }
2231 }
2232
2233 /* EXPORT:
2234 Return in *NR the clipping rectangle for glyph string S. */
2235
2236 void
2237 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2238 {
2239 get_glyph_string_clip_rects (s, nr, 1);
2240 }
2241
2242
2243 /* EXPORT:
2244 Return the position and height of the phys cursor in window W.
2245 Set w->phys_cursor_width to width of phys cursor.
2246 */
2247
2248 void
2249 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2250 struct glyph *glyph, int *xp, int *yp, int *heightp)
2251 {
2252 struct frame *f = XFRAME (WINDOW_FRAME (w));
2253 int x, y, wd, h, h0, y0;
2254
2255 /* Compute the width of the rectangle to draw. If on a stretch
2256 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2257 rectangle as wide as the glyph, but use a canonical character
2258 width instead. */
2259 wd = glyph->pixel_width - 1;
2260 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2261 wd++; /* Why? */
2262 #endif
2263
2264 x = w->phys_cursor.x;
2265 if (x < 0)
2266 {
2267 wd += x;
2268 x = 0;
2269 }
2270
2271 if (glyph->type == STRETCH_GLYPH
2272 && !x_stretch_cursor_p)
2273 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2274 w->phys_cursor_width = wd;
2275
2276 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2277
2278 /* If y is below window bottom, ensure that we still see a cursor. */
2279 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2280
2281 h = max (h0, glyph->ascent + glyph->descent);
2282 h0 = min (h0, glyph->ascent + glyph->descent);
2283
2284 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2285 if (y < y0)
2286 {
2287 h = max (h - (y0 - y) + 1, h0);
2288 y = y0 - 1;
2289 }
2290 else
2291 {
2292 y0 = window_text_bottom_y (w) - h0;
2293 if (y > y0)
2294 {
2295 h += y - y0;
2296 y = y0;
2297 }
2298 }
2299
2300 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2301 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2302 *heightp = h;
2303 }
2304
2305 /*
2306 * Remember which glyph the mouse is over.
2307 */
2308
2309 void
2310 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2311 {
2312 Lisp_Object window;
2313 struct window *w;
2314 struct glyph_row *r, *gr, *end_row;
2315 enum window_part part;
2316 enum glyph_row_area area;
2317 int x, y, width, height;
2318
2319 /* Try to determine frame pixel position and size of the glyph under
2320 frame pixel coordinates X/Y on frame F. */
2321
2322 if (window_resize_pixelwise)
2323 {
2324 width = height = 1;
2325 goto virtual_glyph;
2326 }
2327 else if (!f->glyphs_initialized_p
2328 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2329 NILP (window)))
2330 {
2331 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2332 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2333 goto virtual_glyph;
2334 }
2335
2336 w = XWINDOW (window);
2337 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2338 height = WINDOW_FRAME_LINE_HEIGHT (w);
2339
2340 x = window_relative_x_coord (w, part, gx);
2341 y = gy - WINDOW_TOP_EDGE_Y (w);
2342
2343 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2344 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2345
2346 if (w->pseudo_window_p)
2347 {
2348 area = TEXT_AREA;
2349 part = ON_MODE_LINE; /* Don't adjust margin. */
2350 goto text_glyph;
2351 }
2352
2353 switch (part)
2354 {
2355 case ON_LEFT_MARGIN:
2356 area = LEFT_MARGIN_AREA;
2357 goto text_glyph;
2358
2359 case ON_RIGHT_MARGIN:
2360 area = RIGHT_MARGIN_AREA;
2361 goto text_glyph;
2362
2363 case ON_HEADER_LINE:
2364 case ON_MODE_LINE:
2365 gr = (part == ON_HEADER_LINE
2366 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2367 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2368 gy = gr->y;
2369 area = TEXT_AREA;
2370 goto text_glyph_row_found;
2371
2372 case ON_TEXT:
2373 area = TEXT_AREA;
2374
2375 text_glyph:
2376 gr = 0; gy = 0;
2377 for (; r <= end_row && r->enabled_p; ++r)
2378 if (r->y + r->height > y)
2379 {
2380 gr = r; gy = r->y;
2381 break;
2382 }
2383
2384 text_glyph_row_found:
2385 if (gr && gy <= y)
2386 {
2387 struct glyph *g = gr->glyphs[area];
2388 struct glyph *end = g + gr->used[area];
2389
2390 height = gr->height;
2391 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2392 if (gx + g->pixel_width > x)
2393 break;
2394
2395 if (g < end)
2396 {
2397 if (g->type == IMAGE_GLYPH)
2398 {
2399 /* Don't remember when mouse is over image, as
2400 image may have hot-spots. */
2401 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2402 return;
2403 }
2404 width = g->pixel_width;
2405 }
2406 else
2407 {
2408 /* Use nominal char spacing at end of line. */
2409 x -= gx;
2410 gx += (x / width) * width;
2411 }
2412
2413 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2414 gx += window_box_left_offset (w, area);
2415 }
2416 else
2417 {
2418 /* Use nominal line height at end of window. */
2419 gx = (x / width) * width;
2420 y -= gy;
2421 gy += (y / height) * height;
2422 }
2423 break;
2424
2425 case ON_LEFT_FRINGE:
2426 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2427 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2428 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2429 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2430 goto row_glyph;
2431
2432 case ON_RIGHT_FRINGE:
2433 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2434 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2435 : window_box_right_offset (w, TEXT_AREA));
2436 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2437 goto row_glyph;
2438
2439 case ON_SCROLL_BAR:
2440 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2441 ? 0
2442 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2443 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2444 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2445 : 0)));
2446 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2447
2448 row_glyph:
2449 gr = 0, gy = 0;
2450 for (; r <= end_row && r->enabled_p; ++r)
2451 if (r->y + r->height > y)
2452 {
2453 gr = r; gy = r->y;
2454 break;
2455 }
2456
2457 if (gr && gy <= y)
2458 height = gr->height;
2459 else
2460 {
2461 /* Use nominal line height at end of window. */
2462 y -= gy;
2463 gy += (y / height) * height;
2464 }
2465 break;
2466
2467 default:
2468 ;
2469 virtual_glyph:
2470 /* If there is no glyph under the mouse, then we divide the screen
2471 into a grid of the smallest glyph in the frame, and use that
2472 as our "glyph". */
2473
2474 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2475 round down even for negative values. */
2476 if (gx < 0)
2477 gx -= width - 1;
2478 if (gy < 0)
2479 gy -= height - 1;
2480
2481 gx = (gx / width) * width;
2482 gy = (gy / height) * height;
2483
2484 goto store_rect;
2485 }
2486
2487 gx += WINDOW_LEFT_EDGE_X (w);
2488 gy += WINDOW_TOP_EDGE_Y (w);
2489
2490 store_rect:
2491 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2492
2493 /* Visible feedback for debugging. */
2494 #if 0
2495 #if HAVE_X_WINDOWS
2496 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2497 f->output_data.x->normal_gc,
2498 gx, gy, width, height);
2499 #endif
2500 #endif
2501 }
2502
2503
2504 #endif /* HAVE_WINDOW_SYSTEM */
2505
2506 static void
2507 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2508 {
2509 eassert (w);
2510 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2511 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2512 w->window_end_vpos
2513 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2514 }
2515
2516 /***********************************************************************
2517 Lisp form evaluation
2518 ***********************************************************************/
2519
2520 /* Error handler for safe_eval and safe_call. */
2521
2522 static Lisp_Object
2523 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2524 {
2525 add_to_log ("Error during redisplay: %S signaled %S",
2526 Flist (nargs, args), arg);
2527 return Qnil;
2528 }
2529
2530 /* Call function FUNC with the rest of NARGS - 1 arguments
2531 following. Return the result, or nil if something went
2532 wrong. Prevent redisplay during the evaluation. */
2533
2534 Lisp_Object
2535 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2536 {
2537 Lisp_Object val;
2538
2539 if (inhibit_eval_during_redisplay)
2540 val = Qnil;
2541 else
2542 {
2543 va_list ap;
2544 ptrdiff_t i;
2545 ptrdiff_t count = SPECPDL_INDEX ();
2546 struct gcpro gcpro1;
2547 Lisp_Object *args = alloca (nargs * word_size);
2548
2549 args[0] = func;
2550 va_start (ap, func);
2551 for (i = 1; i < nargs; i++)
2552 args[i] = va_arg (ap, Lisp_Object);
2553 va_end (ap);
2554
2555 GCPRO1 (args[0]);
2556 gcpro1.nvars = nargs;
2557 specbind (Qinhibit_redisplay, Qt);
2558 /* Use Qt to ensure debugger does not run,
2559 so there is no possibility of wanting to redisplay. */
2560 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2561 safe_eval_handler);
2562 UNGCPRO;
2563 val = unbind_to (count, val);
2564 }
2565
2566 return val;
2567 }
2568
2569
2570 /* Call function FN with one argument ARG.
2571 Return the result, or nil if something went wrong. */
2572
2573 Lisp_Object
2574 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2575 {
2576 return safe_call (2, fn, arg);
2577 }
2578
2579 static Lisp_Object Qeval;
2580
2581 Lisp_Object
2582 safe_eval (Lisp_Object sexpr)
2583 {
2584 return safe_call1 (Qeval, sexpr);
2585 }
2586
2587 /* Call function FN with two arguments ARG1 and ARG2.
2588 Return the result, or nil if something went wrong. */
2589
2590 Lisp_Object
2591 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2592 {
2593 return safe_call (3, fn, arg1, arg2);
2594 }
2595
2596
2597 \f
2598 /***********************************************************************
2599 Debugging
2600 ***********************************************************************/
2601
2602 #if 0
2603
2604 /* Define CHECK_IT to perform sanity checks on iterators.
2605 This is for debugging. It is too slow to do unconditionally. */
2606
2607 static void
2608 check_it (struct it *it)
2609 {
2610 if (it->method == GET_FROM_STRING)
2611 {
2612 eassert (STRINGP (it->string));
2613 eassert (IT_STRING_CHARPOS (*it) >= 0);
2614 }
2615 else
2616 {
2617 eassert (IT_STRING_CHARPOS (*it) < 0);
2618 if (it->method == GET_FROM_BUFFER)
2619 {
2620 /* Check that character and byte positions agree. */
2621 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2622 }
2623 }
2624
2625 if (it->dpvec)
2626 eassert (it->current.dpvec_index >= 0);
2627 else
2628 eassert (it->current.dpvec_index < 0);
2629 }
2630
2631 #define CHECK_IT(IT) check_it ((IT))
2632
2633 #else /* not 0 */
2634
2635 #define CHECK_IT(IT) (void) 0
2636
2637 #endif /* not 0 */
2638
2639
2640 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2641
2642 /* Check that the window end of window W is what we expect it
2643 to be---the last row in the current matrix displaying text. */
2644
2645 static void
2646 check_window_end (struct window *w)
2647 {
2648 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2649 {
2650 struct glyph_row *row;
2651 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2652 !row->enabled_p
2653 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2654 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2655 }
2656 }
2657
2658 #define CHECK_WINDOW_END(W) check_window_end ((W))
2659
2660 #else
2661
2662 #define CHECK_WINDOW_END(W) (void) 0
2663
2664 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2665
2666 /***********************************************************************
2667 Iterator initialization
2668 ***********************************************************************/
2669
2670 /* Initialize IT for displaying current_buffer in window W, starting
2671 at character position CHARPOS. CHARPOS < 0 means that no buffer
2672 position is specified which is useful when the iterator is assigned
2673 a position later. BYTEPOS is the byte position corresponding to
2674 CHARPOS.
2675
2676 If ROW is not null, calls to produce_glyphs with IT as parameter
2677 will produce glyphs in that row.
2678
2679 BASE_FACE_ID is the id of a base face to use. It must be one of
2680 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2681 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2682 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2683
2684 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2685 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2686 will be initialized to use the corresponding mode line glyph row of
2687 the desired matrix of W. */
2688
2689 void
2690 init_iterator (struct it *it, struct window *w,
2691 ptrdiff_t charpos, ptrdiff_t bytepos,
2692 struct glyph_row *row, enum face_id base_face_id)
2693 {
2694 enum face_id remapped_base_face_id = base_face_id;
2695
2696 /* Some precondition checks. */
2697 eassert (w != NULL && it != NULL);
2698 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2699 && charpos <= ZV));
2700
2701 /* If face attributes have been changed since the last redisplay,
2702 free realized faces now because they depend on face definitions
2703 that might have changed. Don't free faces while there might be
2704 desired matrices pending which reference these faces. */
2705 if (face_change_count && !inhibit_free_realized_faces)
2706 {
2707 face_change_count = 0;
2708 free_all_realized_faces (Qnil);
2709 }
2710
2711 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2712 if (! NILP (Vface_remapping_alist))
2713 remapped_base_face_id
2714 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2715
2716 /* Use one of the mode line rows of W's desired matrix if
2717 appropriate. */
2718 if (row == NULL)
2719 {
2720 if (base_face_id == MODE_LINE_FACE_ID
2721 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2722 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2723 else if (base_face_id == HEADER_LINE_FACE_ID)
2724 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2725 }
2726
2727 /* Clear IT. */
2728 memset (it, 0, sizeof *it);
2729 it->current.overlay_string_index = -1;
2730 it->current.dpvec_index = -1;
2731 it->base_face_id = remapped_base_face_id;
2732 it->string = Qnil;
2733 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2734 it->paragraph_embedding = L2R;
2735 it->bidi_it.string.lstring = Qnil;
2736 it->bidi_it.string.s = NULL;
2737 it->bidi_it.string.bufpos = 0;
2738 it->bidi_it.w = w;
2739
2740 /* The window in which we iterate over current_buffer: */
2741 XSETWINDOW (it->window, w);
2742 it->w = w;
2743 it->f = XFRAME (w->frame);
2744
2745 it->cmp_it.id = -1;
2746
2747 /* Extra space between lines (on window systems only). */
2748 if (base_face_id == DEFAULT_FACE_ID
2749 && FRAME_WINDOW_P (it->f))
2750 {
2751 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2752 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2753 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2754 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2755 * FRAME_LINE_HEIGHT (it->f));
2756 else if (it->f->extra_line_spacing > 0)
2757 it->extra_line_spacing = it->f->extra_line_spacing;
2758 it->max_extra_line_spacing = 0;
2759 }
2760
2761 /* If realized faces have been removed, e.g. because of face
2762 attribute changes of named faces, recompute them. When running
2763 in batch mode, the face cache of the initial frame is null. If
2764 we happen to get called, make a dummy face cache. */
2765 if (FRAME_FACE_CACHE (it->f) == NULL)
2766 init_frame_faces (it->f);
2767 if (FRAME_FACE_CACHE (it->f)->used == 0)
2768 recompute_basic_faces (it->f);
2769
2770 /* Current value of the `slice', `space-width', and 'height' properties. */
2771 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2772 it->space_width = Qnil;
2773 it->font_height = Qnil;
2774 it->override_ascent = -1;
2775
2776 /* Are control characters displayed as `^C'? */
2777 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2778
2779 /* -1 means everything between a CR and the following line end
2780 is invisible. >0 means lines indented more than this value are
2781 invisible. */
2782 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2783 ? (clip_to_bounds
2784 (-1, XINT (BVAR (current_buffer, selective_display)),
2785 PTRDIFF_MAX))
2786 : (!NILP (BVAR (current_buffer, selective_display))
2787 ? -1 : 0));
2788 it->selective_display_ellipsis_p
2789 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2790
2791 /* Display table to use. */
2792 it->dp = window_display_table (w);
2793
2794 /* Are multibyte characters enabled in current_buffer? */
2795 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2796
2797 /* Get the position at which the redisplay_end_trigger hook should
2798 be run, if it is to be run at all. */
2799 if (MARKERP (w->redisplay_end_trigger)
2800 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2801 it->redisplay_end_trigger_charpos
2802 = marker_position (w->redisplay_end_trigger);
2803 else if (INTEGERP (w->redisplay_end_trigger))
2804 it->redisplay_end_trigger_charpos =
2805 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2806
2807 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2808
2809 /* Are lines in the display truncated? */
2810 if (base_face_id != DEFAULT_FACE_ID
2811 || it->w->hscroll
2812 || (! WINDOW_FULL_WIDTH_P (it->w)
2813 && ((!NILP (Vtruncate_partial_width_windows)
2814 && !INTEGERP (Vtruncate_partial_width_windows))
2815 || (INTEGERP (Vtruncate_partial_width_windows)
2816 /* PXW: Shall we do something about this? */
2817 && (WINDOW_TOTAL_COLS (it->w)
2818 < XINT (Vtruncate_partial_width_windows))))))
2819 it->line_wrap = TRUNCATE;
2820 else if (NILP (BVAR (current_buffer, truncate_lines)))
2821 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2822 ? WINDOW_WRAP : WORD_WRAP;
2823 else
2824 it->line_wrap = TRUNCATE;
2825
2826 /* Get dimensions of truncation and continuation glyphs. These are
2827 displayed as fringe bitmaps under X, but we need them for such
2828 frames when the fringes are turned off. But leave the dimensions
2829 zero for tooltip frames, as these glyphs look ugly there and also
2830 sabotage calculations of tooltip dimensions in x-show-tip. */
2831 #ifdef HAVE_WINDOW_SYSTEM
2832 if (!(FRAME_WINDOW_P (it->f)
2833 && FRAMEP (tip_frame)
2834 && it->f == XFRAME (tip_frame)))
2835 #endif
2836 {
2837 if (it->line_wrap == TRUNCATE)
2838 {
2839 /* We will need the truncation glyph. */
2840 eassert (it->glyph_row == NULL);
2841 produce_special_glyphs (it, IT_TRUNCATION);
2842 it->truncation_pixel_width = it->pixel_width;
2843 }
2844 else
2845 {
2846 /* We will need the continuation glyph. */
2847 eassert (it->glyph_row == NULL);
2848 produce_special_glyphs (it, IT_CONTINUATION);
2849 it->continuation_pixel_width = it->pixel_width;
2850 }
2851 }
2852
2853 /* Reset these values to zero because the produce_special_glyphs
2854 above has changed them. */
2855 it->pixel_width = it->ascent = it->descent = 0;
2856 it->phys_ascent = it->phys_descent = 0;
2857
2858 /* Set this after getting the dimensions of truncation and
2859 continuation glyphs, so that we don't produce glyphs when calling
2860 produce_special_glyphs, above. */
2861 it->glyph_row = row;
2862 it->area = TEXT_AREA;
2863
2864 /* Forget any previous info about this row being reversed. */
2865 if (it->glyph_row)
2866 it->glyph_row->reversed_p = 0;
2867
2868 /* Get the dimensions of the display area. The display area
2869 consists of the visible window area plus a horizontally scrolled
2870 part to the left of the window. All x-values are relative to the
2871 start of this total display area. */
2872 if (base_face_id != DEFAULT_FACE_ID)
2873 {
2874 /* Mode lines, menu bar in terminal frames. */
2875 it->first_visible_x = 0;
2876 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2877 }
2878 else
2879 {
2880 it->first_visible_x
2881 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2882 it->last_visible_x = (it->first_visible_x
2883 + window_box_width (w, TEXT_AREA));
2884
2885 /* If we truncate lines, leave room for the truncation glyph(s) at
2886 the right margin. Otherwise, leave room for the continuation
2887 glyph(s). Done only if the window has no fringes. Since we
2888 don't know at this point whether there will be any R2L lines in
2889 the window, we reserve space for truncation/continuation glyphs
2890 even if only one of the fringes is absent. */
2891 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2892 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2893 {
2894 if (it->line_wrap == TRUNCATE)
2895 it->last_visible_x -= it->truncation_pixel_width;
2896 else
2897 it->last_visible_x -= it->continuation_pixel_width;
2898 }
2899
2900 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2901 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2902 }
2903
2904 /* Leave room for a border glyph. */
2905 if (!FRAME_WINDOW_P (it->f)
2906 && !WINDOW_RIGHTMOST_P (it->w))
2907 it->last_visible_x -= 1;
2908
2909 it->last_visible_y = window_text_bottom_y (w);
2910
2911 /* For mode lines and alike, arrange for the first glyph having a
2912 left box line if the face specifies a box. */
2913 if (base_face_id != DEFAULT_FACE_ID)
2914 {
2915 struct face *face;
2916
2917 it->face_id = remapped_base_face_id;
2918
2919 /* If we have a boxed mode line, make the first character appear
2920 with a left box line. */
2921 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2922 if (face->box != FACE_NO_BOX)
2923 it->start_of_box_run_p = true;
2924 }
2925
2926 /* If a buffer position was specified, set the iterator there,
2927 getting overlays and face properties from that position. */
2928 if (charpos >= BUF_BEG (current_buffer))
2929 {
2930 it->end_charpos = ZV;
2931 eassert (charpos == BYTE_TO_CHAR (bytepos));
2932 IT_CHARPOS (*it) = charpos;
2933 IT_BYTEPOS (*it) = bytepos;
2934
2935 /* We will rely on `reseat' to set this up properly, via
2936 handle_face_prop. */
2937 it->face_id = it->base_face_id;
2938
2939 it->start = it->current;
2940 /* Do we need to reorder bidirectional text? Not if this is a
2941 unibyte buffer: by definition, none of the single-byte
2942 characters are strong R2L, so no reordering is needed. And
2943 bidi.c doesn't support unibyte buffers anyway. Also, don't
2944 reorder while we are loading loadup.el, since the tables of
2945 character properties needed for reordering are not yet
2946 available. */
2947 it->bidi_p =
2948 NILP (Vpurify_flag)
2949 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2950 && it->multibyte_p;
2951
2952 /* If we are to reorder bidirectional text, init the bidi
2953 iterator. */
2954 if (it->bidi_p)
2955 {
2956 /* Note the paragraph direction that this buffer wants to
2957 use. */
2958 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2959 Qleft_to_right))
2960 it->paragraph_embedding = L2R;
2961 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2962 Qright_to_left))
2963 it->paragraph_embedding = R2L;
2964 else
2965 it->paragraph_embedding = NEUTRAL_DIR;
2966 bidi_unshelve_cache (NULL, 0);
2967 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2968 &it->bidi_it);
2969 }
2970
2971 /* Compute faces etc. */
2972 reseat (it, it->current.pos, 1);
2973 }
2974
2975 CHECK_IT (it);
2976 }
2977
2978
2979 /* Initialize IT for the display of window W with window start POS. */
2980
2981 void
2982 start_display (struct it *it, struct window *w, struct text_pos pos)
2983 {
2984 struct glyph_row *row;
2985 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2986
2987 row = w->desired_matrix->rows + first_vpos;
2988 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2989 it->first_vpos = first_vpos;
2990
2991 /* Don't reseat to previous visible line start if current start
2992 position is in a string or image. */
2993 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2994 {
2995 int start_at_line_beg_p;
2996 int first_y = it->current_y;
2997
2998 /* If window start is not at a line start, skip forward to POS to
2999 get the correct continuation lines width. */
3000 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3001 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3002 if (!start_at_line_beg_p)
3003 {
3004 int new_x;
3005
3006 reseat_at_previous_visible_line_start (it);
3007 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3008
3009 new_x = it->current_x + it->pixel_width;
3010
3011 /* If lines are continued, this line may end in the middle
3012 of a multi-glyph character (e.g. a control character
3013 displayed as \003, or in the middle of an overlay
3014 string). In this case move_it_to above will not have
3015 taken us to the start of the continuation line but to the
3016 end of the continued line. */
3017 if (it->current_x > 0
3018 && it->line_wrap != TRUNCATE /* Lines are continued. */
3019 && (/* And glyph doesn't fit on the line. */
3020 new_x > it->last_visible_x
3021 /* Or it fits exactly and we're on a window
3022 system frame. */
3023 || (new_x == it->last_visible_x
3024 && FRAME_WINDOW_P (it->f)
3025 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3026 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3027 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3028 {
3029 if ((it->current.dpvec_index >= 0
3030 || it->current.overlay_string_index >= 0)
3031 /* If we are on a newline from a display vector or
3032 overlay string, then we are already at the end of
3033 a screen line; no need to go to the next line in
3034 that case, as this line is not really continued.
3035 (If we do go to the next line, C-e will not DTRT.) */
3036 && it->c != '\n')
3037 {
3038 set_iterator_to_next (it, 1);
3039 move_it_in_display_line_to (it, -1, -1, 0);
3040 }
3041
3042 it->continuation_lines_width += it->current_x;
3043 }
3044 /* If the character at POS is displayed via a display
3045 vector, move_it_to above stops at the final glyph of
3046 IT->dpvec. To make the caller redisplay that character
3047 again (a.k.a. start at POS), we need to reset the
3048 dpvec_index to the beginning of IT->dpvec. */
3049 else if (it->current.dpvec_index >= 0)
3050 it->current.dpvec_index = 0;
3051
3052 /* We're starting a new display line, not affected by the
3053 height of the continued line, so clear the appropriate
3054 fields in the iterator structure. */
3055 it->max_ascent = it->max_descent = 0;
3056 it->max_phys_ascent = it->max_phys_descent = 0;
3057
3058 it->current_y = first_y;
3059 it->vpos = 0;
3060 it->current_x = it->hpos = 0;
3061 }
3062 }
3063 }
3064
3065
3066 /* Return 1 if POS is a position in ellipses displayed for invisible
3067 text. W is the window we display, for text property lookup. */
3068
3069 static int
3070 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3071 {
3072 Lisp_Object prop, window;
3073 int ellipses_p = 0;
3074 ptrdiff_t charpos = CHARPOS (pos->pos);
3075
3076 /* If POS specifies a position in a display vector, this might
3077 be for an ellipsis displayed for invisible text. We won't
3078 get the iterator set up for delivering that ellipsis unless
3079 we make sure that it gets aware of the invisible text. */
3080 if (pos->dpvec_index >= 0
3081 && pos->overlay_string_index < 0
3082 && CHARPOS (pos->string_pos) < 0
3083 && charpos > BEGV
3084 && (XSETWINDOW (window, w),
3085 prop = Fget_char_property (make_number (charpos),
3086 Qinvisible, window),
3087 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3088 {
3089 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3090 window);
3091 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3092 }
3093
3094 return ellipses_p;
3095 }
3096
3097
3098 /* Initialize IT for stepping through current_buffer in window W,
3099 starting at position POS that includes overlay string and display
3100 vector/ control character translation position information. Value
3101 is zero if there are overlay strings with newlines at POS. */
3102
3103 static int
3104 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3105 {
3106 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3107 int i, overlay_strings_with_newlines = 0;
3108
3109 /* If POS specifies a position in a display vector, this might
3110 be for an ellipsis displayed for invisible text. We won't
3111 get the iterator set up for delivering that ellipsis unless
3112 we make sure that it gets aware of the invisible text. */
3113 if (in_ellipses_for_invisible_text_p (pos, w))
3114 {
3115 --charpos;
3116 bytepos = 0;
3117 }
3118
3119 /* Keep in mind: the call to reseat in init_iterator skips invisible
3120 text, so we might end up at a position different from POS. This
3121 is only a problem when POS is a row start after a newline and an
3122 overlay starts there with an after-string, and the overlay has an
3123 invisible property. Since we don't skip invisible text in
3124 display_line and elsewhere immediately after consuming the
3125 newline before the row start, such a POS will not be in a string,
3126 but the call to init_iterator below will move us to the
3127 after-string. */
3128 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3129
3130 /* This only scans the current chunk -- it should scan all chunks.
3131 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3132 to 16 in 22.1 to make this a lesser problem. */
3133 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3134 {
3135 const char *s = SSDATA (it->overlay_strings[i]);
3136 const char *e = s + SBYTES (it->overlay_strings[i]);
3137
3138 while (s < e && *s != '\n')
3139 ++s;
3140
3141 if (s < e)
3142 {
3143 overlay_strings_with_newlines = 1;
3144 break;
3145 }
3146 }
3147
3148 /* If position is within an overlay string, set up IT to the right
3149 overlay string. */
3150 if (pos->overlay_string_index >= 0)
3151 {
3152 int relative_index;
3153
3154 /* If the first overlay string happens to have a `display'
3155 property for an image, the iterator will be set up for that
3156 image, and we have to undo that setup first before we can
3157 correct the overlay string index. */
3158 if (it->method == GET_FROM_IMAGE)
3159 pop_it (it);
3160
3161 /* We already have the first chunk of overlay strings in
3162 IT->overlay_strings. Load more until the one for
3163 pos->overlay_string_index is in IT->overlay_strings. */
3164 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3165 {
3166 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3167 it->current.overlay_string_index = 0;
3168 while (n--)
3169 {
3170 load_overlay_strings (it, 0);
3171 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3172 }
3173 }
3174
3175 it->current.overlay_string_index = pos->overlay_string_index;
3176 relative_index = (it->current.overlay_string_index
3177 % OVERLAY_STRING_CHUNK_SIZE);
3178 it->string = it->overlay_strings[relative_index];
3179 eassert (STRINGP (it->string));
3180 it->current.string_pos = pos->string_pos;
3181 it->method = GET_FROM_STRING;
3182 it->end_charpos = SCHARS (it->string);
3183 /* Set up the bidi iterator for this overlay string. */
3184 if (it->bidi_p)
3185 {
3186 it->bidi_it.string.lstring = it->string;
3187 it->bidi_it.string.s = NULL;
3188 it->bidi_it.string.schars = SCHARS (it->string);
3189 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3190 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3191 it->bidi_it.string.unibyte = !it->multibyte_p;
3192 it->bidi_it.w = it->w;
3193 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3194 FRAME_WINDOW_P (it->f), &it->bidi_it);
3195
3196 /* Synchronize the state of the bidi iterator with
3197 pos->string_pos. For any string position other than
3198 zero, this will be done automagically when we resume
3199 iteration over the string and get_visually_first_element
3200 is called. But if string_pos is zero, and the string is
3201 to be reordered for display, we need to resync manually,
3202 since it could be that the iteration state recorded in
3203 pos ended at string_pos of 0 moving backwards in string. */
3204 if (CHARPOS (pos->string_pos) == 0)
3205 {
3206 get_visually_first_element (it);
3207 if (IT_STRING_CHARPOS (*it) != 0)
3208 do {
3209 /* Paranoia. */
3210 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3211 bidi_move_to_visually_next (&it->bidi_it);
3212 } while (it->bidi_it.charpos != 0);
3213 }
3214 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3215 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3216 }
3217 }
3218
3219 if (CHARPOS (pos->string_pos) >= 0)
3220 {
3221 /* Recorded position is not in an overlay string, but in another
3222 string. This can only be a string from a `display' property.
3223 IT should already be filled with that string. */
3224 it->current.string_pos = pos->string_pos;
3225 eassert (STRINGP (it->string));
3226 if (it->bidi_p)
3227 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3228 FRAME_WINDOW_P (it->f), &it->bidi_it);
3229 }
3230
3231 /* Restore position in display vector translations, control
3232 character translations or ellipses. */
3233 if (pos->dpvec_index >= 0)
3234 {
3235 if (it->dpvec == NULL)
3236 get_next_display_element (it);
3237 eassert (it->dpvec && it->current.dpvec_index == 0);
3238 it->current.dpvec_index = pos->dpvec_index;
3239 }
3240
3241 CHECK_IT (it);
3242 return !overlay_strings_with_newlines;
3243 }
3244
3245
3246 /* Initialize IT for stepping through current_buffer in window W
3247 starting at ROW->start. */
3248
3249 static void
3250 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3251 {
3252 init_from_display_pos (it, w, &row->start);
3253 it->start = row->start;
3254 it->continuation_lines_width = row->continuation_lines_width;
3255 CHECK_IT (it);
3256 }
3257
3258
3259 /* Initialize IT for stepping through current_buffer in window W
3260 starting in the line following ROW, i.e. starting at ROW->end.
3261 Value is zero if there are overlay strings with newlines at ROW's
3262 end position. */
3263
3264 static int
3265 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3266 {
3267 int success = 0;
3268
3269 if (init_from_display_pos (it, w, &row->end))
3270 {
3271 if (row->continued_p)
3272 it->continuation_lines_width
3273 = row->continuation_lines_width + row->pixel_width;
3274 CHECK_IT (it);
3275 success = 1;
3276 }
3277
3278 return success;
3279 }
3280
3281
3282
3283 \f
3284 /***********************************************************************
3285 Text properties
3286 ***********************************************************************/
3287
3288 /* Called when IT reaches IT->stop_charpos. Handle text property and
3289 overlay changes. Set IT->stop_charpos to the next position where
3290 to stop. */
3291
3292 static void
3293 handle_stop (struct it *it)
3294 {
3295 enum prop_handled handled;
3296 int handle_overlay_change_p;
3297 struct props *p;
3298
3299 it->dpvec = NULL;
3300 it->current.dpvec_index = -1;
3301 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3302 it->ignore_overlay_strings_at_pos_p = 0;
3303 it->ellipsis_p = 0;
3304
3305 /* Use face of preceding text for ellipsis (if invisible) */
3306 if (it->selective_display_ellipsis_p)
3307 it->saved_face_id = it->face_id;
3308
3309 do
3310 {
3311 handled = HANDLED_NORMALLY;
3312
3313 /* Call text property handlers. */
3314 for (p = it_props; p->handler; ++p)
3315 {
3316 handled = p->handler (it);
3317
3318 if (handled == HANDLED_RECOMPUTE_PROPS)
3319 break;
3320 else if (handled == HANDLED_RETURN)
3321 {
3322 /* We still want to show before and after strings from
3323 overlays even if the actual buffer text is replaced. */
3324 if (!handle_overlay_change_p
3325 || it->sp > 1
3326 /* Don't call get_overlay_strings_1 if we already
3327 have overlay strings loaded, because doing so
3328 will load them again and push the iterator state
3329 onto the stack one more time, which is not
3330 expected by the rest of the code that processes
3331 overlay strings. */
3332 || (it->current.overlay_string_index < 0
3333 ? !get_overlay_strings_1 (it, 0, 0)
3334 : 0))
3335 {
3336 if (it->ellipsis_p)
3337 setup_for_ellipsis (it, 0);
3338 /* When handling a display spec, we might load an
3339 empty string. In that case, discard it here. We
3340 used to discard it in handle_single_display_spec,
3341 but that causes get_overlay_strings_1, above, to
3342 ignore overlay strings that we must check. */
3343 if (STRINGP (it->string) && !SCHARS (it->string))
3344 pop_it (it);
3345 return;
3346 }
3347 else if (STRINGP (it->string) && !SCHARS (it->string))
3348 pop_it (it);
3349 else
3350 {
3351 it->ignore_overlay_strings_at_pos_p = true;
3352 it->string_from_display_prop_p = 0;
3353 it->from_disp_prop_p = 0;
3354 handle_overlay_change_p = 0;
3355 }
3356 handled = HANDLED_RECOMPUTE_PROPS;
3357 break;
3358 }
3359 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3360 handle_overlay_change_p = 0;
3361 }
3362
3363 if (handled != HANDLED_RECOMPUTE_PROPS)
3364 {
3365 /* Don't check for overlay strings below when set to deliver
3366 characters from a display vector. */
3367 if (it->method == GET_FROM_DISPLAY_VECTOR)
3368 handle_overlay_change_p = 0;
3369
3370 /* Handle overlay changes.
3371 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3372 if it finds overlays. */
3373 if (handle_overlay_change_p)
3374 handled = handle_overlay_change (it);
3375 }
3376
3377 if (it->ellipsis_p)
3378 {
3379 setup_for_ellipsis (it, 0);
3380 break;
3381 }
3382 }
3383 while (handled == HANDLED_RECOMPUTE_PROPS);
3384
3385 /* Determine where to stop next. */
3386 if (handled == HANDLED_NORMALLY)
3387 compute_stop_pos (it);
3388 }
3389
3390
3391 /* Compute IT->stop_charpos from text property and overlay change
3392 information for IT's current position. */
3393
3394 static void
3395 compute_stop_pos (struct it *it)
3396 {
3397 register INTERVAL iv, next_iv;
3398 Lisp_Object object, limit, position;
3399 ptrdiff_t charpos, bytepos;
3400
3401 if (STRINGP (it->string))
3402 {
3403 /* Strings are usually short, so don't limit the search for
3404 properties. */
3405 it->stop_charpos = it->end_charpos;
3406 object = it->string;
3407 limit = Qnil;
3408 charpos = IT_STRING_CHARPOS (*it);
3409 bytepos = IT_STRING_BYTEPOS (*it);
3410 }
3411 else
3412 {
3413 ptrdiff_t pos;
3414
3415 /* If end_charpos is out of range for some reason, such as a
3416 misbehaving display function, rationalize it (Bug#5984). */
3417 if (it->end_charpos > ZV)
3418 it->end_charpos = ZV;
3419 it->stop_charpos = it->end_charpos;
3420
3421 /* If next overlay change is in front of the current stop pos
3422 (which is IT->end_charpos), stop there. Note: value of
3423 next_overlay_change is point-max if no overlay change
3424 follows. */
3425 charpos = IT_CHARPOS (*it);
3426 bytepos = IT_BYTEPOS (*it);
3427 pos = next_overlay_change (charpos);
3428 if (pos < it->stop_charpos)
3429 it->stop_charpos = pos;
3430
3431 /* Set up variables for computing the stop position from text
3432 property changes. */
3433 XSETBUFFER (object, current_buffer);
3434 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3435 }
3436
3437 /* Get the interval containing IT's position. Value is a null
3438 interval if there isn't such an interval. */
3439 position = make_number (charpos);
3440 iv = validate_interval_range (object, &position, &position, 0);
3441 if (iv)
3442 {
3443 Lisp_Object values_here[LAST_PROP_IDX];
3444 struct props *p;
3445
3446 /* Get properties here. */
3447 for (p = it_props; p->handler; ++p)
3448 values_here[p->idx] = textget (iv->plist, *p->name);
3449
3450 /* Look for an interval following iv that has different
3451 properties. */
3452 for (next_iv = next_interval (iv);
3453 (next_iv
3454 && (NILP (limit)
3455 || XFASTINT (limit) > next_iv->position));
3456 next_iv = next_interval (next_iv))
3457 {
3458 for (p = it_props; p->handler; ++p)
3459 {
3460 Lisp_Object new_value;
3461
3462 new_value = textget (next_iv->plist, *p->name);
3463 if (!EQ (values_here[p->idx], new_value))
3464 break;
3465 }
3466
3467 if (p->handler)
3468 break;
3469 }
3470
3471 if (next_iv)
3472 {
3473 if (INTEGERP (limit)
3474 && next_iv->position >= XFASTINT (limit))
3475 /* No text property change up to limit. */
3476 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3477 else
3478 /* Text properties change in next_iv. */
3479 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3480 }
3481 }
3482
3483 if (it->cmp_it.id < 0)
3484 {
3485 ptrdiff_t stoppos = it->end_charpos;
3486
3487 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3488 stoppos = -1;
3489 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3490 stoppos, it->string);
3491 }
3492
3493 eassert (STRINGP (it->string)
3494 || (it->stop_charpos >= BEGV
3495 && it->stop_charpos >= IT_CHARPOS (*it)));
3496 }
3497
3498
3499 /* Return the position of the next overlay change after POS in
3500 current_buffer. Value is point-max if no overlay change
3501 follows. This is like `next-overlay-change' but doesn't use
3502 xmalloc. */
3503
3504 static ptrdiff_t
3505 next_overlay_change (ptrdiff_t pos)
3506 {
3507 ptrdiff_t i, noverlays;
3508 ptrdiff_t endpos;
3509 Lisp_Object *overlays;
3510
3511 /* Get all overlays at the given position. */
3512 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3513
3514 /* If any of these overlays ends before endpos,
3515 use its ending point instead. */
3516 for (i = 0; i < noverlays; ++i)
3517 {
3518 Lisp_Object oend;
3519 ptrdiff_t oendpos;
3520
3521 oend = OVERLAY_END (overlays[i]);
3522 oendpos = OVERLAY_POSITION (oend);
3523 endpos = min (endpos, oendpos);
3524 }
3525
3526 return endpos;
3527 }
3528
3529 /* How many characters forward to search for a display property or
3530 display string. Searching too far forward makes the bidi display
3531 sluggish, especially in small windows. */
3532 #define MAX_DISP_SCAN 250
3533
3534 /* Return the character position of a display string at or after
3535 position specified by POSITION. If no display string exists at or
3536 after POSITION, return ZV. A display string is either an overlay
3537 with `display' property whose value is a string, or a `display'
3538 text property whose value is a string. STRING is data about the
3539 string to iterate; if STRING->lstring is nil, we are iterating a
3540 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3541 on a GUI frame. DISP_PROP is set to zero if we searched
3542 MAX_DISP_SCAN characters forward without finding any display
3543 strings, non-zero otherwise. It is set to 2 if the display string
3544 uses any kind of `(space ...)' spec that will produce a stretch of
3545 white space in the text area. */
3546 ptrdiff_t
3547 compute_display_string_pos (struct text_pos *position,
3548 struct bidi_string_data *string,
3549 struct window *w,
3550 int frame_window_p, int *disp_prop)
3551 {
3552 /* OBJECT = nil means current buffer. */
3553 Lisp_Object object, object1;
3554 Lisp_Object pos, spec, limpos;
3555 int string_p = (string && (STRINGP (string->lstring) || string->s));
3556 ptrdiff_t eob = string_p ? string->schars : ZV;
3557 ptrdiff_t begb = string_p ? 0 : BEGV;
3558 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3559 ptrdiff_t lim =
3560 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3561 struct text_pos tpos;
3562 int rv = 0;
3563
3564 if (string && STRINGP (string->lstring))
3565 object1 = object = string->lstring;
3566 else if (w && !string_p)
3567 {
3568 XSETWINDOW (object, w);
3569 object1 = Qnil;
3570 }
3571 else
3572 object1 = object = Qnil;
3573
3574 *disp_prop = 1;
3575
3576 if (charpos >= eob
3577 /* We don't support display properties whose values are strings
3578 that have display string properties. */
3579 || string->from_disp_str
3580 /* C strings cannot have display properties. */
3581 || (string->s && !STRINGP (object)))
3582 {
3583 *disp_prop = 0;
3584 return eob;
3585 }
3586
3587 /* If the character at CHARPOS is where the display string begins,
3588 return CHARPOS. */
3589 pos = make_number (charpos);
3590 if (STRINGP (object))
3591 bufpos = string->bufpos;
3592 else
3593 bufpos = charpos;
3594 tpos = *position;
3595 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3596 && (charpos <= begb
3597 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3598 object),
3599 spec))
3600 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3601 frame_window_p)))
3602 {
3603 if (rv == 2)
3604 *disp_prop = 2;
3605 return charpos;
3606 }
3607
3608 /* Look forward for the first character with a `display' property
3609 that will replace the underlying text when displayed. */
3610 limpos = make_number (lim);
3611 do {
3612 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3613 CHARPOS (tpos) = XFASTINT (pos);
3614 if (CHARPOS (tpos) >= lim)
3615 {
3616 *disp_prop = 0;
3617 break;
3618 }
3619 if (STRINGP (object))
3620 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3621 else
3622 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3623 spec = Fget_char_property (pos, Qdisplay, object);
3624 if (!STRINGP (object))
3625 bufpos = CHARPOS (tpos);
3626 } while (NILP (spec)
3627 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3628 bufpos, frame_window_p)));
3629 if (rv == 2)
3630 *disp_prop = 2;
3631
3632 return CHARPOS (tpos);
3633 }
3634
3635 /* Return the character position of the end of the display string that
3636 started at CHARPOS. If there's no display string at CHARPOS,
3637 return -1. A display string is either an overlay with `display'
3638 property whose value is a string or a `display' text property whose
3639 value is a string. */
3640 ptrdiff_t
3641 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3642 {
3643 /* OBJECT = nil means current buffer. */
3644 Lisp_Object object =
3645 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3646 Lisp_Object pos = make_number (charpos);
3647 ptrdiff_t eob =
3648 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3649
3650 if (charpos >= eob || (string->s && !STRINGP (object)))
3651 return eob;
3652
3653 /* It could happen that the display property or overlay was removed
3654 since we found it in compute_display_string_pos above. One way
3655 this can happen is if JIT font-lock was called (through
3656 handle_fontified_prop), and jit-lock-functions remove text
3657 properties or overlays from the portion of buffer that includes
3658 CHARPOS. Muse mode is known to do that, for example. In this
3659 case, we return -1 to the caller, to signal that no display
3660 string is actually present at CHARPOS. See bidi_fetch_char for
3661 how this is handled.
3662
3663 An alternative would be to never look for display properties past
3664 it->stop_charpos. But neither compute_display_string_pos nor
3665 bidi_fetch_char that calls it know or care where the next
3666 stop_charpos is. */
3667 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3668 return -1;
3669
3670 /* Look forward for the first character where the `display' property
3671 changes. */
3672 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3673
3674 return XFASTINT (pos);
3675 }
3676
3677
3678 \f
3679 /***********************************************************************
3680 Fontification
3681 ***********************************************************************/
3682
3683 /* Handle changes in the `fontified' property of the current buffer by
3684 calling hook functions from Qfontification_functions to fontify
3685 regions of text. */
3686
3687 static enum prop_handled
3688 handle_fontified_prop (struct it *it)
3689 {
3690 Lisp_Object prop, pos;
3691 enum prop_handled handled = HANDLED_NORMALLY;
3692
3693 if (!NILP (Vmemory_full))
3694 return handled;
3695
3696 /* Get the value of the `fontified' property at IT's current buffer
3697 position. (The `fontified' property doesn't have a special
3698 meaning in strings.) If the value is nil, call functions from
3699 Qfontification_functions. */
3700 if (!STRINGP (it->string)
3701 && it->s == NULL
3702 && !NILP (Vfontification_functions)
3703 && !NILP (Vrun_hooks)
3704 && (pos = make_number (IT_CHARPOS (*it)),
3705 prop = Fget_char_property (pos, Qfontified, Qnil),
3706 /* Ignore the special cased nil value always present at EOB since
3707 no amount of fontifying will be able to change it. */
3708 NILP (prop) && IT_CHARPOS (*it) < Z))
3709 {
3710 ptrdiff_t count = SPECPDL_INDEX ();
3711 Lisp_Object val;
3712 struct buffer *obuf = current_buffer;
3713 ptrdiff_t begv = BEGV, zv = ZV;
3714 bool old_clip_changed = current_buffer->clip_changed;
3715
3716 val = Vfontification_functions;
3717 specbind (Qfontification_functions, Qnil);
3718
3719 eassert (it->end_charpos == ZV);
3720
3721 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3722 safe_call1 (val, pos);
3723 else
3724 {
3725 Lisp_Object fns, fn;
3726 struct gcpro gcpro1, gcpro2;
3727
3728 fns = Qnil;
3729 GCPRO2 (val, fns);
3730
3731 for (; CONSP (val); val = XCDR (val))
3732 {
3733 fn = XCAR (val);
3734
3735 if (EQ (fn, Qt))
3736 {
3737 /* A value of t indicates this hook has a local
3738 binding; it means to run the global binding too.
3739 In a global value, t should not occur. If it
3740 does, we must ignore it to avoid an endless
3741 loop. */
3742 for (fns = Fdefault_value (Qfontification_functions);
3743 CONSP (fns);
3744 fns = XCDR (fns))
3745 {
3746 fn = XCAR (fns);
3747 if (!EQ (fn, Qt))
3748 safe_call1 (fn, pos);
3749 }
3750 }
3751 else
3752 safe_call1 (fn, pos);
3753 }
3754
3755 UNGCPRO;
3756 }
3757
3758 unbind_to (count, Qnil);
3759
3760 /* Fontification functions routinely call `save-restriction'.
3761 Normally, this tags clip_changed, which can confuse redisplay
3762 (see discussion in Bug#6671). Since we don't perform any
3763 special handling of fontification changes in the case where
3764 `save-restriction' isn't called, there's no point doing so in
3765 this case either. So, if the buffer's restrictions are
3766 actually left unchanged, reset clip_changed. */
3767 if (obuf == current_buffer)
3768 {
3769 if (begv == BEGV && zv == ZV)
3770 current_buffer->clip_changed = old_clip_changed;
3771 }
3772 /* There isn't much we can reasonably do to protect against
3773 misbehaving fontification, but here's a fig leaf. */
3774 else if (BUFFER_LIVE_P (obuf))
3775 set_buffer_internal_1 (obuf);
3776
3777 /* The fontification code may have added/removed text.
3778 It could do even a lot worse, but let's at least protect against
3779 the most obvious case where only the text past `pos' gets changed',
3780 as is/was done in grep.el where some escapes sequences are turned
3781 into face properties (bug#7876). */
3782 it->end_charpos = ZV;
3783
3784 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3785 something. This avoids an endless loop if they failed to
3786 fontify the text for which reason ever. */
3787 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3788 handled = HANDLED_RECOMPUTE_PROPS;
3789 }
3790
3791 return handled;
3792 }
3793
3794
3795 \f
3796 /***********************************************************************
3797 Faces
3798 ***********************************************************************/
3799
3800 /* Set up iterator IT from face properties at its current position.
3801 Called from handle_stop. */
3802
3803 static enum prop_handled
3804 handle_face_prop (struct it *it)
3805 {
3806 int new_face_id;
3807 ptrdiff_t next_stop;
3808
3809 if (!STRINGP (it->string))
3810 {
3811 new_face_id
3812 = face_at_buffer_position (it->w,
3813 IT_CHARPOS (*it),
3814 &next_stop,
3815 (IT_CHARPOS (*it)
3816 + TEXT_PROP_DISTANCE_LIMIT),
3817 0, it->base_face_id);
3818
3819 /* Is this a start of a run of characters with box face?
3820 Caveat: this can be called for a freshly initialized
3821 iterator; face_id is -1 in this case. We know that the new
3822 face will not change until limit, i.e. if the new face has a
3823 box, all characters up to limit will have one. But, as
3824 usual, we don't know whether limit is really the end. */
3825 if (new_face_id != it->face_id)
3826 {
3827 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3828 /* If it->face_id is -1, old_face below will be NULL, see
3829 the definition of FACE_FROM_ID. This will happen if this
3830 is the initial call that gets the face. */
3831 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3832
3833 /* If the value of face_id of the iterator is -1, we have to
3834 look in front of IT's position and see whether there is a
3835 face there that's different from new_face_id. */
3836 if (!old_face && IT_CHARPOS (*it) > BEG)
3837 {
3838 int prev_face_id = face_before_it_pos (it);
3839
3840 old_face = FACE_FROM_ID (it->f, prev_face_id);
3841 }
3842
3843 /* If the new face has a box, but the old face does not,
3844 this is the start of a run of characters with box face,
3845 i.e. this character has a shadow on the left side. */
3846 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3847 && (old_face == NULL || !old_face->box));
3848 it->face_box_p = new_face->box != FACE_NO_BOX;
3849 }
3850 }
3851 else
3852 {
3853 int base_face_id;
3854 ptrdiff_t bufpos;
3855 int i;
3856 Lisp_Object from_overlay
3857 = (it->current.overlay_string_index >= 0
3858 ? it->string_overlays[it->current.overlay_string_index
3859 % OVERLAY_STRING_CHUNK_SIZE]
3860 : Qnil);
3861
3862 /* See if we got to this string directly or indirectly from
3863 an overlay property. That includes the before-string or
3864 after-string of an overlay, strings in display properties
3865 provided by an overlay, their text properties, etc.
3866
3867 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3868 if (! NILP (from_overlay))
3869 for (i = it->sp - 1; i >= 0; i--)
3870 {
3871 if (it->stack[i].current.overlay_string_index >= 0)
3872 from_overlay
3873 = it->string_overlays[it->stack[i].current.overlay_string_index
3874 % OVERLAY_STRING_CHUNK_SIZE];
3875 else if (! NILP (it->stack[i].from_overlay))
3876 from_overlay = it->stack[i].from_overlay;
3877
3878 if (!NILP (from_overlay))
3879 break;
3880 }
3881
3882 if (! NILP (from_overlay))
3883 {
3884 bufpos = IT_CHARPOS (*it);
3885 /* For a string from an overlay, the base face depends
3886 only on text properties and ignores overlays. */
3887 base_face_id
3888 = face_for_overlay_string (it->w,
3889 IT_CHARPOS (*it),
3890 &next_stop,
3891 (IT_CHARPOS (*it)
3892 + TEXT_PROP_DISTANCE_LIMIT),
3893 0,
3894 from_overlay);
3895 }
3896 else
3897 {
3898 bufpos = 0;
3899
3900 /* For strings from a `display' property, use the face at
3901 IT's current buffer position as the base face to merge
3902 with, so that overlay strings appear in the same face as
3903 surrounding text, unless they specify their own faces.
3904 For strings from wrap-prefix and line-prefix properties,
3905 use the default face, possibly remapped via
3906 Vface_remapping_alist. */
3907 base_face_id = it->string_from_prefix_prop_p
3908 ? (!NILP (Vface_remapping_alist)
3909 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3910 : DEFAULT_FACE_ID)
3911 : underlying_face_id (it);
3912 }
3913
3914 new_face_id = face_at_string_position (it->w,
3915 it->string,
3916 IT_STRING_CHARPOS (*it),
3917 bufpos,
3918 &next_stop,
3919 base_face_id, 0);
3920
3921 /* Is this a start of a run of characters with box? Caveat:
3922 this can be called for a freshly allocated iterator; face_id
3923 is -1 is this case. We know that the new face will not
3924 change until the next check pos, i.e. if the new face has a
3925 box, all characters up to that position will have a
3926 box. But, as usual, we don't know whether that position
3927 is really the end. */
3928 if (new_face_id != it->face_id)
3929 {
3930 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3931 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3932
3933 /* If new face has a box but old face hasn't, this is the
3934 start of a run of characters with box, i.e. it has a
3935 shadow on the left side. */
3936 it->start_of_box_run_p
3937 = new_face->box && (old_face == NULL || !old_face->box);
3938 it->face_box_p = new_face->box != FACE_NO_BOX;
3939 }
3940 }
3941
3942 it->face_id = new_face_id;
3943 return HANDLED_NORMALLY;
3944 }
3945
3946
3947 /* Return the ID of the face ``underlying'' IT's current position,
3948 which is in a string. If the iterator is associated with a
3949 buffer, return the face at IT's current buffer position.
3950 Otherwise, use the iterator's base_face_id. */
3951
3952 static int
3953 underlying_face_id (struct it *it)
3954 {
3955 int face_id = it->base_face_id, i;
3956
3957 eassert (STRINGP (it->string));
3958
3959 for (i = it->sp - 1; i >= 0; --i)
3960 if (NILP (it->stack[i].string))
3961 face_id = it->stack[i].face_id;
3962
3963 return face_id;
3964 }
3965
3966
3967 /* Compute the face one character before or after the current position
3968 of IT, in the visual order. BEFORE_P non-zero means get the face
3969 in front (to the left in L2R paragraphs, to the right in R2L
3970 paragraphs) of IT's screen position. Value is the ID of the face. */
3971
3972 static int
3973 face_before_or_after_it_pos (struct it *it, int before_p)
3974 {
3975 int face_id, limit;
3976 ptrdiff_t next_check_charpos;
3977 struct it it_copy;
3978 void *it_copy_data = NULL;
3979
3980 eassert (it->s == NULL);
3981
3982 if (STRINGP (it->string))
3983 {
3984 ptrdiff_t bufpos, charpos;
3985 int base_face_id;
3986
3987 /* No face change past the end of the string (for the case
3988 we are padding with spaces). No face change before the
3989 string start. */
3990 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3991 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3992 return it->face_id;
3993
3994 if (!it->bidi_p)
3995 {
3996 /* Set charpos to the position before or after IT's current
3997 position, in the logical order, which in the non-bidi
3998 case is the same as the visual order. */
3999 if (before_p)
4000 charpos = IT_STRING_CHARPOS (*it) - 1;
4001 else if (it->what == IT_COMPOSITION)
4002 /* For composition, we must check the character after the
4003 composition. */
4004 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4005 else
4006 charpos = IT_STRING_CHARPOS (*it) + 1;
4007 }
4008 else
4009 {
4010 if (before_p)
4011 {
4012 /* With bidi iteration, the character before the current
4013 in the visual order cannot be found by simple
4014 iteration, because "reverse" reordering is not
4015 supported. Instead, we need to use the move_it_*
4016 family of functions. */
4017 /* Ignore face changes before the first visible
4018 character on this display line. */
4019 if (it->current_x <= it->first_visible_x)
4020 return it->face_id;
4021 SAVE_IT (it_copy, *it, it_copy_data);
4022 /* Implementation note: Since move_it_in_display_line
4023 works in the iterator geometry, and thinks the first
4024 character is always the leftmost, even in R2L lines,
4025 we don't need to distinguish between the R2L and L2R
4026 cases here. */
4027 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4028 it_copy.current_x - 1, MOVE_TO_X);
4029 charpos = IT_STRING_CHARPOS (it_copy);
4030 RESTORE_IT (it, it, it_copy_data);
4031 }
4032 else
4033 {
4034 /* Set charpos to the string position of the character
4035 that comes after IT's current position in the visual
4036 order. */
4037 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4038
4039 it_copy = *it;
4040 while (n--)
4041 bidi_move_to_visually_next (&it_copy.bidi_it);
4042
4043 charpos = it_copy.bidi_it.charpos;
4044 }
4045 }
4046 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4047
4048 if (it->current.overlay_string_index >= 0)
4049 bufpos = IT_CHARPOS (*it);
4050 else
4051 bufpos = 0;
4052
4053 base_face_id = underlying_face_id (it);
4054
4055 /* Get the face for ASCII, or unibyte. */
4056 face_id = face_at_string_position (it->w,
4057 it->string,
4058 charpos,
4059 bufpos,
4060 &next_check_charpos,
4061 base_face_id, 0);
4062
4063 /* Correct the face for charsets different from ASCII. Do it
4064 for the multibyte case only. The face returned above is
4065 suitable for unibyte text if IT->string is unibyte. */
4066 if (STRING_MULTIBYTE (it->string))
4067 {
4068 struct text_pos pos1 = string_pos (charpos, it->string);
4069 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4070 int c, len;
4071 struct face *face = FACE_FROM_ID (it->f, face_id);
4072
4073 c = string_char_and_length (p, &len);
4074 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4075 }
4076 }
4077 else
4078 {
4079 struct text_pos pos;
4080
4081 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4082 || (IT_CHARPOS (*it) <= BEGV && before_p))
4083 return it->face_id;
4084
4085 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4086 pos = it->current.pos;
4087
4088 if (!it->bidi_p)
4089 {
4090 if (before_p)
4091 DEC_TEXT_POS (pos, it->multibyte_p);
4092 else
4093 {
4094 if (it->what == IT_COMPOSITION)
4095 {
4096 /* For composition, we must check the position after
4097 the composition. */
4098 pos.charpos += it->cmp_it.nchars;
4099 pos.bytepos += it->len;
4100 }
4101 else
4102 INC_TEXT_POS (pos, it->multibyte_p);
4103 }
4104 }
4105 else
4106 {
4107 if (before_p)
4108 {
4109 /* With bidi iteration, the character before the current
4110 in the visual order cannot be found by simple
4111 iteration, because "reverse" reordering is not
4112 supported. Instead, we need to use the move_it_*
4113 family of functions. */
4114 /* Ignore face changes before the first visible
4115 character on this display line. */
4116 if (it->current_x <= it->first_visible_x)
4117 return it->face_id;
4118 SAVE_IT (it_copy, *it, it_copy_data);
4119 /* Implementation note: Since move_it_in_display_line
4120 works in the iterator geometry, and thinks the first
4121 character is always the leftmost, even in R2L lines,
4122 we don't need to distinguish between the R2L and L2R
4123 cases here. */
4124 move_it_in_display_line (&it_copy, ZV,
4125 it_copy.current_x - 1, MOVE_TO_X);
4126 pos = it_copy.current.pos;
4127 RESTORE_IT (it, it, it_copy_data);
4128 }
4129 else
4130 {
4131 /* Set charpos to the buffer position of the character
4132 that comes after IT's current position in the visual
4133 order. */
4134 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4135
4136 it_copy = *it;
4137 while (n--)
4138 bidi_move_to_visually_next (&it_copy.bidi_it);
4139
4140 SET_TEXT_POS (pos,
4141 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4142 }
4143 }
4144 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4145
4146 /* Determine face for CHARSET_ASCII, or unibyte. */
4147 face_id = face_at_buffer_position (it->w,
4148 CHARPOS (pos),
4149 &next_check_charpos,
4150 limit, 0, -1);
4151
4152 /* Correct the face for charsets different from ASCII. Do it
4153 for the multibyte case only. The face returned above is
4154 suitable for unibyte text if current_buffer is unibyte. */
4155 if (it->multibyte_p)
4156 {
4157 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4158 struct face *face = FACE_FROM_ID (it->f, face_id);
4159 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4160 }
4161 }
4162
4163 return face_id;
4164 }
4165
4166
4167 \f
4168 /***********************************************************************
4169 Invisible text
4170 ***********************************************************************/
4171
4172 /* Set up iterator IT from invisible properties at its current
4173 position. Called from handle_stop. */
4174
4175 static enum prop_handled
4176 handle_invisible_prop (struct it *it)
4177 {
4178 enum prop_handled handled = HANDLED_NORMALLY;
4179 int invis_p;
4180 Lisp_Object prop;
4181
4182 if (STRINGP (it->string))
4183 {
4184 Lisp_Object end_charpos, limit, charpos;
4185
4186 /* Get the value of the invisible text property at the
4187 current position. Value will be nil if there is no such
4188 property. */
4189 charpos = make_number (IT_STRING_CHARPOS (*it));
4190 prop = Fget_text_property (charpos, Qinvisible, it->string);
4191 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4192
4193 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4194 {
4195 /* Record whether we have to display an ellipsis for the
4196 invisible text. */
4197 int display_ellipsis_p = (invis_p == 2);
4198 ptrdiff_t len, endpos;
4199
4200 handled = HANDLED_RECOMPUTE_PROPS;
4201
4202 /* Get the position at which the next visible text can be
4203 found in IT->string, if any. */
4204 endpos = len = SCHARS (it->string);
4205 XSETINT (limit, len);
4206 do
4207 {
4208 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4209 it->string, limit);
4210 if (INTEGERP (end_charpos))
4211 {
4212 endpos = XFASTINT (end_charpos);
4213 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4214 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4215 if (invis_p == 2)
4216 display_ellipsis_p = true;
4217 }
4218 }
4219 while (invis_p && endpos < len);
4220
4221 if (display_ellipsis_p)
4222 it->ellipsis_p = true;
4223
4224 if (endpos < len)
4225 {
4226 /* Text at END_CHARPOS is visible. Move IT there. */
4227 struct text_pos old;
4228 ptrdiff_t oldpos;
4229
4230 old = it->current.string_pos;
4231 oldpos = CHARPOS (old);
4232 if (it->bidi_p)
4233 {
4234 if (it->bidi_it.first_elt
4235 && it->bidi_it.charpos < SCHARS (it->string))
4236 bidi_paragraph_init (it->paragraph_embedding,
4237 &it->bidi_it, 1);
4238 /* Bidi-iterate out of the invisible text. */
4239 do
4240 {
4241 bidi_move_to_visually_next (&it->bidi_it);
4242 }
4243 while (oldpos <= it->bidi_it.charpos
4244 && it->bidi_it.charpos < endpos);
4245
4246 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4247 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4248 if (IT_CHARPOS (*it) >= endpos)
4249 it->prev_stop = endpos;
4250 }
4251 else
4252 {
4253 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4254 compute_string_pos (&it->current.string_pos, old, it->string);
4255 }
4256 }
4257 else
4258 {
4259 /* The rest of the string is invisible. If this is an
4260 overlay string, proceed with the next overlay string
4261 or whatever comes and return a character from there. */
4262 if (it->current.overlay_string_index >= 0
4263 && !display_ellipsis_p)
4264 {
4265 next_overlay_string (it);
4266 /* Don't check for overlay strings when we just
4267 finished processing them. */
4268 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4269 }
4270 else
4271 {
4272 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4273 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4274 }
4275 }
4276 }
4277 }
4278 else
4279 {
4280 ptrdiff_t newpos, next_stop, start_charpos, tem;
4281 Lisp_Object pos, overlay;
4282
4283 /* First of all, is there invisible text at this position? */
4284 tem = start_charpos = IT_CHARPOS (*it);
4285 pos = make_number (tem);
4286 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4287 &overlay);
4288 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4289
4290 /* If we are on invisible text, skip over it. */
4291 if (invis_p && start_charpos < it->end_charpos)
4292 {
4293 /* Record whether we have to display an ellipsis for the
4294 invisible text. */
4295 int display_ellipsis_p = invis_p == 2;
4296
4297 handled = HANDLED_RECOMPUTE_PROPS;
4298
4299 /* Loop skipping over invisible text. The loop is left at
4300 ZV or with IT on the first char being visible again. */
4301 do
4302 {
4303 /* Try to skip some invisible text. Return value is the
4304 position reached which can be equal to where we start
4305 if there is nothing invisible there. This skips both
4306 over invisible text properties and overlays with
4307 invisible property. */
4308 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4309
4310 /* If we skipped nothing at all we weren't at invisible
4311 text in the first place. If everything to the end of
4312 the buffer was skipped, end the loop. */
4313 if (newpos == tem || newpos >= ZV)
4314 invis_p = 0;
4315 else
4316 {
4317 /* We skipped some characters but not necessarily
4318 all there are. Check if we ended up on visible
4319 text. Fget_char_property returns the property of
4320 the char before the given position, i.e. if we
4321 get invis_p = 0, this means that the char at
4322 newpos is visible. */
4323 pos = make_number (newpos);
4324 prop = Fget_char_property (pos, Qinvisible, it->window);
4325 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4326 }
4327
4328 /* If we ended up on invisible text, proceed to
4329 skip starting with next_stop. */
4330 if (invis_p)
4331 tem = next_stop;
4332
4333 /* If there are adjacent invisible texts, don't lose the
4334 second one's ellipsis. */
4335 if (invis_p == 2)
4336 display_ellipsis_p = true;
4337 }
4338 while (invis_p);
4339
4340 /* The position newpos is now either ZV or on visible text. */
4341 if (it->bidi_p)
4342 {
4343 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4344 int on_newline
4345 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4346 int after_newline
4347 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4348
4349 /* If the invisible text ends on a newline or on a
4350 character after a newline, we can avoid the costly,
4351 character by character, bidi iteration to NEWPOS, and
4352 instead simply reseat the iterator there. That's
4353 because all bidi reordering information is tossed at
4354 the newline. This is a big win for modes that hide
4355 complete lines, like Outline, Org, etc. */
4356 if (on_newline || after_newline)
4357 {
4358 struct text_pos tpos;
4359 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4360
4361 SET_TEXT_POS (tpos, newpos, bpos);
4362 reseat_1 (it, tpos, 0);
4363 /* If we reseat on a newline/ZV, we need to prep the
4364 bidi iterator for advancing to the next character
4365 after the newline/EOB, keeping the current paragraph
4366 direction (so that PRODUCE_GLYPHS does TRT wrt
4367 prepending/appending glyphs to a glyph row). */
4368 if (on_newline)
4369 {
4370 it->bidi_it.first_elt = 0;
4371 it->bidi_it.paragraph_dir = pdir;
4372 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4373 it->bidi_it.nchars = 1;
4374 it->bidi_it.ch_len = 1;
4375 }
4376 }
4377 else /* Must use the slow method. */
4378 {
4379 /* With bidi iteration, the region of invisible text
4380 could start and/or end in the middle of a
4381 non-base embedding level. Therefore, we need to
4382 skip invisible text using the bidi iterator,
4383 starting at IT's current position, until we find
4384 ourselves outside of the invisible text.
4385 Skipping invisible text _after_ bidi iteration
4386 avoids affecting the visual order of the
4387 displayed text when invisible properties are
4388 added or removed. */
4389 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4390 {
4391 /* If we were `reseat'ed to a new paragraph,
4392 determine the paragraph base direction. We
4393 need to do it now because
4394 next_element_from_buffer may not have a
4395 chance to do it, if we are going to skip any
4396 text at the beginning, which resets the
4397 FIRST_ELT flag. */
4398 bidi_paragraph_init (it->paragraph_embedding,
4399 &it->bidi_it, 1);
4400 }
4401 do
4402 {
4403 bidi_move_to_visually_next (&it->bidi_it);
4404 }
4405 while (it->stop_charpos <= it->bidi_it.charpos
4406 && it->bidi_it.charpos < newpos);
4407 IT_CHARPOS (*it) = it->bidi_it.charpos;
4408 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4409 /* If we overstepped NEWPOS, record its position in
4410 the iterator, so that we skip invisible text if
4411 later the bidi iteration lands us in the
4412 invisible region again. */
4413 if (IT_CHARPOS (*it) >= newpos)
4414 it->prev_stop = newpos;
4415 }
4416 }
4417 else
4418 {
4419 IT_CHARPOS (*it) = newpos;
4420 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4421 }
4422
4423 /* If there are before-strings at the start of invisible
4424 text, and the text is invisible because of a text
4425 property, arrange to show before-strings because 20.x did
4426 it that way. (If the text is invisible because of an
4427 overlay property instead of a text property, this is
4428 already handled in the overlay code.) */
4429 if (NILP (overlay)
4430 && get_overlay_strings (it, it->stop_charpos))
4431 {
4432 handled = HANDLED_RECOMPUTE_PROPS;
4433 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4434 }
4435 else if (display_ellipsis_p)
4436 {
4437 /* Make sure that the glyphs of the ellipsis will get
4438 correct `charpos' values. If we would not update
4439 it->position here, the glyphs would belong to the
4440 last visible character _before_ the invisible
4441 text, which confuses `set_cursor_from_row'.
4442
4443 We use the last invisible position instead of the
4444 first because this way the cursor is always drawn on
4445 the first "." of the ellipsis, whenever PT is inside
4446 the invisible text. Otherwise the cursor would be
4447 placed _after_ the ellipsis when the point is after the
4448 first invisible character. */
4449 if (!STRINGP (it->object))
4450 {
4451 it->position.charpos = newpos - 1;
4452 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4453 }
4454 it->ellipsis_p = true;
4455 /* Let the ellipsis display before
4456 considering any properties of the following char.
4457 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4458 handled = HANDLED_RETURN;
4459 }
4460 }
4461 }
4462
4463 return handled;
4464 }
4465
4466
4467 /* Make iterator IT return `...' next.
4468 Replaces LEN characters from buffer. */
4469
4470 static void
4471 setup_for_ellipsis (struct it *it, int len)
4472 {
4473 /* Use the display table definition for `...'. Invalid glyphs
4474 will be handled by the method returning elements from dpvec. */
4475 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4476 {
4477 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4478 it->dpvec = v->contents;
4479 it->dpend = v->contents + v->header.size;
4480 }
4481 else
4482 {
4483 /* Default `...'. */
4484 it->dpvec = default_invis_vector;
4485 it->dpend = default_invis_vector + 3;
4486 }
4487
4488 it->dpvec_char_len = len;
4489 it->current.dpvec_index = 0;
4490 it->dpvec_face_id = -1;
4491
4492 /* Remember the current face id in case glyphs specify faces.
4493 IT's face is restored in set_iterator_to_next.
4494 saved_face_id was set to preceding char's face in handle_stop. */
4495 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4496 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4497
4498 it->method = GET_FROM_DISPLAY_VECTOR;
4499 it->ellipsis_p = true;
4500 }
4501
4502
4503 \f
4504 /***********************************************************************
4505 'display' property
4506 ***********************************************************************/
4507
4508 /* Set up iterator IT from `display' property at its current position.
4509 Called from handle_stop.
4510 We return HANDLED_RETURN if some part of the display property
4511 overrides the display of the buffer text itself.
4512 Otherwise we return HANDLED_NORMALLY. */
4513
4514 static enum prop_handled
4515 handle_display_prop (struct it *it)
4516 {
4517 Lisp_Object propval, object, overlay;
4518 struct text_pos *position;
4519 ptrdiff_t bufpos;
4520 /* Nonzero if some property replaces the display of the text itself. */
4521 int display_replaced_p = 0;
4522
4523 if (STRINGP (it->string))
4524 {
4525 object = it->string;
4526 position = &it->current.string_pos;
4527 bufpos = CHARPOS (it->current.pos);
4528 }
4529 else
4530 {
4531 XSETWINDOW (object, it->w);
4532 position = &it->current.pos;
4533 bufpos = CHARPOS (*position);
4534 }
4535
4536 /* Reset those iterator values set from display property values. */
4537 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4538 it->space_width = Qnil;
4539 it->font_height = Qnil;
4540 it->voffset = 0;
4541
4542 /* We don't support recursive `display' properties, i.e. string
4543 values that have a string `display' property, that have a string
4544 `display' property etc. */
4545 if (!it->string_from_display_prop_p)
4546 it->area = TEXT_AREA;
4547
4548 propval = get_char_property_and_overlay (make_number (position->charpos),
4549 Qdisplay, object, &overlay);
4550 if (NILP (propval))
4551 return HANDLED_NORMALLY;
4552 /* Now OVERLAY is the overlay that gave us this property, or nil
4553 if it was a text property. */
4554
4555 if (!STRINGP (it->string))
4556 object = it->w->contents;
4557
4558 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4559 position, bufpos,
4560 FRAME_WINDOW_P (it->f));
4561
4562 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4563 }
4564
4565 /* Subroutine of handle_display_prop. Returns non-zero if the display
4566 specification in SPEC is a replacing specification, i.e. it would
4567 replace the text covered by `display' property with something else,
4568 such as an image or a display string. If SPEC includes any kind or
4569 `(space ...) specification, the value is 2; this is used by
4570 compute_display_string_pos, which see.
4571
4572 See handle_single_display_spec for documentation of arguments.
4573 frame_window_p is non-zero if the window being redisplayed is on a
4574 GUI frame; this argument is used only if IT is NULL, see below.
4575
4576 IT can be NULL, if this is called by the bidi reordering code
4577 through compute_display_string_pos, which see. In that case, this
4578 function only examines SPEC, but does not otherwise "handle" it, in
4579 the sense that it doesn't set up members of IT from the display
4580 spec. */
4581 static int
4582 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4583 Lisp_Object overlay, struct text_pos *position,
4584 ptrdiff_t bufpos, int frame_window_p)
4585 {
4586 int replacing_p = 0;
4587 int rv;
4588
4589 if (CONSP (spec)
4590 /* Simple specifications. */
4591 && !EQ (XCAR (spec), Qimage)
4592 && !EQ (XCAR (spec), Qspace)
4593 && !EQ (XCAR (spec), Qwhen)
4594 && !EQ (XCAR (spec), Qslice)
4595 && !EQ (XCAR (spec), Qspace_width)
4596 && !EQ (XCAR (spec), Qheight)
4597 && !EQ (XCAR (spec), Qraise)
4598 /* Marginal area specifications. */
4599 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4600 && !EQ (XCAR (spec), Qleft_fringe)
4601 && !EQ (XCAR (spec), Qright_fringe)
4602 && !NILP (XCAR (spec)))
4603 {
4604 for (; CONSP (spec); spec = XCDR (spec))
4605 {
4606 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4607 overlay, position, bufpos,
4608 replacing_p, frame_window_p)))
4609 {
4610 replacing_p = rv;
4611 /* If some text in a string is replaced, `position' no
4612 longer points to the position of `object'. */
4613 if (!it || STRINGP (object))
4614 break;
4615 }
4616 }
4617 }
4618 else if (VECTORP (spec))
4619 {
4620 ptrdiff_t i;
4621 for (i = 0; i < ASIZE (spec); ++i)
4622 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4623 overlay, position, bufpos,
4624 replacing_p, frame_window_p)))
4625 {
4626 replacing_p = rv;
4627 /* If some text in a string is replaced, `position' no
4628 longer points to the position of `object'. */
4629 if (!it || STRINGP (object))
4630 break;
4631 }
4632 }
4633 else
4634 {
4635 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4636 position, bufpos, 0,
4637 frame_window_p)))
4638 replacing_p = rv;
4639 }
4640
4641 return replacing_p;
4642 }
4643
4644 /* Value is the position of the end of the `display' property starting
4645 at START_POS in OBJECT. */
4646
4647 static struct text_pos
4648 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4649 {
4650 Lisp_Object end;
4651 struct text_pos end_pos;
4652
4653 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4654 Qdisplay, object, Qnil);
4655 CHARPOS (end_pos) = XFASTINT (end);
4656 if (STRINGP (object))
4657 compute_string_pos (&end_pos, start_pos, it->string);
4658 else
4659 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4660
4661 return end_pos;
4662 }
4663
4664
4665 /* Set up IT from a single `display' property specification SPEC. OBJECT
4666 is the object in which the `display' property was found. *POSITION
4667 is the position in OBJECT at which the `display' property was found.
4668 BUFPOS is the buffer position of OBJECT (different from POSITION if
4669 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4670 previously saw a display specification which already replaced text
4671 display with something else, for example an image; we ignore such
4672 properties after the first one has been processed.
4673
4674 OVERLAY is the overlay this `display' property came from,
4675 or nil if it was a text property.
4676
4677 If SPEC is a `space' or `image' specification, and in some other
4678 cases too, set *POSITION to the position where the `display'
4679 property ends.
4680
4681 If IT is NULL, only examine the property specification in SPEC, but
4682 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4683 is intended to be displayed in a window on a GUI frame.
4684
4685 Value is non-zero if something was found which replaces the display
4686 of buffer or string text. */
4687
4688 static int
4689 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4690 Lisp_Object overlay, struct text_pos *position,
4691 ptrdiff_t bufpos, int display_replaced_p,
4692 int frame_window_p)
4693 {
4694 Lisp_Object form;
4695 Lisp_Object location, value;
4696 struct text_pos start_pos = *position;
4697 int valid_p;
4698
4699 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4700 If the result is non-nil, use VALUE instead of SPEC. */
4701 form = Qt;
4702 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4703 {
4704 spec = XCDR (spec);
4705 if (!CONSP (spec))
4706 return 0;
4707 form = XCAR (spec);
4708 spec = XCDR (spec);
4709 }
4710
4711 if (!NILP (form) && !EQ (form, Qt))
4712 {
4713 ptrdiff_t count = SPECPDL_INDEX ();
4714 struct gcpro gcpro1;
4715
4716 /* Bind `object' to the object having the `display' property, a
4717 buffer or string. Bind `position' to the position in the
4718 object where the property was found, and `buffer-position'
4719 to the current position in the buffer. */
4720
4721 if (NILP (object))
4722 XSETBUFFER (object, current_buffer);
4723 specbind (Qobject, object);
4724 specbind (Qposition, make_number (CHARPOS (*position)));
4725 specbind (Qbuffer_position, make_number (bufpos));
4726 GCPRO1 (form);
4727 form = safe_eval (form);
4728 UNGCPRO;
4729 unbind_to (count, Qnil);
4730 }
4731
4732 if (NILP (form))
4733 return 0;
4734
4735 /* Handle `(height HEIGHT)' specifications. */
4736 if (CONSP (spec)
4737 && EQ (XCAR (spec), Qheight)
4738 && CONSP (XCDR (spec)))
4739 {
4740 if (it)
4741 {
4742 if (!FRAME_WINDOW_P (it->f))
4743 return 0;
4744
4745 it->font_height = XCAR (XCDR (spec));
4746 if (!NILP (it->font_height))
4747 {
4748 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4749 int new_height = -1;
4750
4751 if (CONSP (it->font_height)
4752 && (EQ (XCAR (it->font_height), Qplus)
4753 || EQ (XCAR (it->font_height), Qminus))
4754 && CONSP (XCDR (it->font_height))
4755 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4756 {
4757 /* `(+ N)' or `(- N)' where N is an integer. */
4758 int steps = XINT (XCAR (XCDR (it->font_height)));
4759 if (EQ (XCAR (it->font_height), Qplus))
4760 steps = - steps;
4761 it->face_id = smaller_face (it->f, it->face_id, steps);
4762 }
4763 else if (FUNCTIONP (it->font_height))
4764 {
4765 /* Call function with current height as argument.
4766 Value is the new height. */
4767 Lisp_Object height;
4768 height = safe_call1 (it->font_height,
4769 face->lface[LFACE_HEIGHT_INDEX]);
4770 if (NUMBERP (height))
4771 new_height = XFLOATINT (height);
4772 }
4773 else if (NUMBERP (it->font_height))
4774 {
4775 /* Value is a multiple of the canonical char height. */
4776 struct face *f;
4777
4778 f = FACE_FROM_ID (it->f,
4779 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4780 new_height = (XFLOATINT (it->font_height)
4781 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4782 }
4783 else
4784 {
4785 /* Evaluate IT->font_height with `height' bound to the
4786 current specified height to get the new height. */
4787 ptrdiff_t count = SPECPDL_INDEX ();
4788
4789 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4790 value = safe_eval (it->font_height);
4791 unbind_to (count, Qnil);
4792
4793 if (NUMBERP (value))
4794 new_height = XFLOATINT (value);
4795 }
4796
4797 if (new_height > 0)
4798 it->face_id = face_with_height (it->f, it->face_id, new_height);
4799 }
4800 }
4801
4802 return 0;
4803 }
4804
4805 /* Handle `(space-width WIDTH)'. */
4806 if (CONSP (spec)
4807 && EQ (XCAR (spec), Qspace_width)
4808 && CONSP (XCDR (spec)))
4809 {
4810 if (it)
4811 {
4812 if (!FRAME_WINDOW_P (it->f))
4813 return 0;
4814
4815 value = XCAR (XCDR (spec));
4816 if (NUMBERP (value) && XFLOATINT (value) > 0)
4817 it->space_width = value;
4818 }
4819
4820 return 0;
4821 }
4822
4823 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4824 if (CONSP (spec)
4825 && EQ (XCAR (spec), Qslice))
4826 {
4827 Lisp_Object tem;
4828
4829 if (it)
4830 {
4831 if (!FRAME_WINDOW_P (it->f))
4832 return 0;
4833
4834 if (tem = XCDR (spec), CONSP (tem))
4835 {
4836 it->slice.x = XCAR (tem);
4837 if (tem = XCDR (tem), CONSP (tem))
4838 {
4839 it->slice.y = XCAR (tem);
4840 if (tem = XCDR (tem), CONSP (tem))
4841 {
4842 it->slice.width = XCAR (tem);
4843 if (tem = XCDR (tem), CONSP (tem))
4844 it->slice.height = XCAR (tem);
4845 }
4846 }
4847 }
4848 }
4849
4850 return 0;
4851 }
4852
4853 /* Handle `(raise FACTOR)'. */
4854 if (CONSP (spec)
4855 && EQ (XCAR (spec), Qraise)
4856 && CONSP (XCDR (spec)))
4857 {
4858 if (it)
4859 {
4860 if (!FRAME_WINDOW_P (it->f))
4861 return 0;
4862
4863 #ifdef HAVE_WINDOW_SYSTEM
4864 value = XCAR (XCDR (spec));
4865 if (NUMBERP (value))
4866 {
4867 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4868 it->voffset = - (XFLOATINT (value)
4869 * (FONT_HEIGHT (face->font)));
4870 }
4871 #endif /* HAVE_WINDOW_SYSTEM */
4872 }
4873
4874 return 0;
4875 }
4876
4877 /* Don't handle the other kinds of display specifications
4878 inside a string that we got from a `display' property. */
4879 if (it && it->string_from_display_prop_p)
4880 return 0;
4881
4882 /* Characters having this form of property are not displayed, so
4883 we have to find the end of the property. */
4884 if (it)
4885 {
4886 start_pos = *position;
4887 *position = display_prop_end (it, object, start_pos);
4888 }
4889 value = Qnil;
4890
4891 /* Stop the scan at that end position--we assume that all
4892 text properties change there. */
4893 if (it)
4894 it->stop_charpos = position->charpos;
4895
4896 /* Handle `(left-fringe BITMAP [FACE])'
4897 and `(right-fringe BITMAP [FACE])'. */
4898 if (CONSP (spec)
4899 && (EQ (XCAR (spec), Qleft_fringe)
4900 || EQ (XCAR (spec), Qright_fringe))
4901 && CONSP (XCDR (spec)))
4902 {
4903 int fringe_bitmap;
4904
4905 if (it)
4906 {
4907 if (!FRAME_WINDOW_P (it->f))
4908 /* If we return here, POSITION has been advanced
4909 across the text with this property. */
4910 {
4911 /* Synchronize the bidi iterator with POSITION. This is
4912 needed because we are not going to push the iterator
4913 on behalf of this display property, so there will be
4914 no pop_it call to do this synchronization for us. */
4915 if (it->bidi_p)
4916 {
4917 it->position = *position;
4918 iterate_out_of_display_property (it);
4919 *position = it->position;
4920 }
4921 return 1;
4922 }
4923 }
4924 else if (!frame_window_p)
4925 return 1;
4926
4927 #ifdef HAVE_WINDOW_SYSTEM
4928 value = XCAR (XCDR (spec));
4929 if (!SYMBOLP (value)
4930 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4931 /* If we return here, POSITION has been advanced
4932 across the text with this property. */
4933 {
4934 if (it && it->bidi_p)
4935 {
4936 it->position = *position;
4937 iterate_out_of_display_property (it);
4938 *position = it->position;
4939 }
4940 return 1;
4941 }
4942
4943 if (it)
4944 {
4945 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4946
4947 if (CONSP (XCDR (XCDR (spec))))
4948 {
4949 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4950 int face_id2 = lookup_derived_face (it->f, face_name,
4951 FRINGE_FACE_ID, 0);
4952 if (face_id2 >= 0)
4953 face_id = face_id2;
4954 }
4955
4956 /* Save current settings of IT so that we can restore them
4957 when we are finished with the glyph property value. */
4958 push_it (it, position);
4959
4960 it->area = TEXT_AREA;
4961 it->what = IT_IMAGE;
4962 it->image_id = -1; /* no image */
4963 it->position = start_pos;
4964 it->object = NILP (object) ? it->w->contents : object;
4965 it->method = GET_FROM_IMAGE;
4966 it->from_overlay = Qnil;
4967 it->face_id = face_id;
4968 it->from_disp_prop_p = true;
4969
4970 /* Say that we haven't consumed the characters with
4971 `display' property yet. The call to pop_it in
4972 set_iterator_to_next will clean this up. */
4973 *position = start_pos;
4974
4975 if (EQ (XCAR (spec), Qleft_fringe))
4976 {
4977 it->left_user_fringe_bitmap = fringe_bitmap;
4978 it->left_user_fringe_face_id = face_id;
4979 }
4980 else
4981 {
4982 it->right_user_fringe_bitmap = fringe_bitmap;
4983 it->right_user_fringe_face_id = face_id;
4984 }
4985 }
4986 #endif /* HAVE_WINDOW_SYSTEM */
4987 return 1;
4988 }
4989
4990 /* Prepare to handle `((margin left-margin) ...)',
4991 `((margin right-margin) ...)' and `((margin nil) ...)'
4992 prefixes for display specifications. */
4993 location = Qunbound;
4994 if (CONSP (spec) && CONSP (XCAR (spec)))
4995 {
4996 Lisp_Object tem;
4997
4998 value = XCDR (spec);
4999 if (CONSP (value))
5000 value = XCAR (value);
5001
5002 tem = XCAR (spec);
5003 if (EQ (XCAR (tem), Qmargin)
5004 && (tem = XCDR (tem),
5005 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5006 (NILP (tem)
5007 || EQ (tem, Qleft_margin)
5008 || EQ (tem, Qright_margin))))
5009 location = tem;
5010 }
5011
5012 if (EQ (location, Qunbound))
5013 {
5014 location = Qnil;
5015 value = spec;
5016 }
5017
5018 /* After this point, VALUE is the property after any
5019 margin prefix has been stripped. It must be a string,
5020 an image specification, or `(space ...)'.
5021
5022 LOCATION specifies where to display: `left-margin',
5023 `right-margin' or nil. */
5024
5025 valid_p = (STRINGP (value)
5026 #ifdef HAVE_WINDOW_SYSTEM
5027 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5028 && valid_image_p (value))
5029 #endif /* not HAVE_WINDOW_SYSTEM */
5030 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5031
5032 if (valid_p && !display_replaced_p)
5033 {
5034 int retval = 1;
5035
5036 if (!it)
5037 {
5038 /* Callers need to know whether the display spec is any kind
5039 of `(space ...)' spec that is about to affect text-area
5040 display. */
5041 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5042 retval = 2;
5043 return retval;
5044 }
5045
5046 /* Save current settings of IT so that we can restore them
5047 when we are finished with the glyph property value. */
5048 push_it (it, position);
5049 it->from_overlay = overlay;
5050 it->from_disp_prop_p = true;
5051
5052 if (NILP (location))
5053 it->area = TEXT_AREA;
5054 else if (EQ (location, Qleft_margin))
5055 it->area = LEFT_MARGIN_AREA;
5056 else
5057 it->area = RIGHT_MARGIN_AREA;
5058
5059 if (STRINGP (value))
5060 {
5061 it->string = value;
5062 it->multibyte_p = STRING_MULTIBYTE (it->string);
5063 it->current.overlay_string_index = -1;
5064 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5065 it->end_charpos = it->string_nchars = SCHARS (it->string);
5066 it->method = GET_FROM_STRING;
5067 it->stop_charpos = 0;
5068 it->prev_stop = 0;
5069 it->base_level_stop = 0;
5070 it->string_from_display_prop_p = true;
5071 /* Say that we haven't consumed the characters with
5072 `display' property yet. The call to pop_it in
5073 set_iterator_to_next will clean this up. */
5074 if (BUFFERP (object))
5075 *position = start_pos;
5076
5077 /* Force paragraph direction to be that of the parent
5078 object. If the parent object's paragraph direction is
5079 not yet determined, default to L2R. */
5080 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5081 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5082 else
5083 it->paragraph_embedding = L2R;
5084
5085 /* Set up the bidi iterator for this display string. */
5086 if (it->bidi_p)
5087 {
5088 it->bidi_it.string.lstring = it->string;
5089 it->bidi_it.string.s = NULL;
5090 it->bidi_it.string.schars = it->end_charpos;
5091 it->bidi_it.string.bufpos = bufpos;
5092 it->bidi_it.string.from_disp_str = 1;
5093 it->bidi_it.string.unibyte = !it->multibyte_p;
5094 it->bidi_it.w = it->w;
5095 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5096 }
5097 }
5098 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5099 {
5100 it->method = GET_FROM_STRETCH;
5101 it->object = value;
5102 *position = it->position = start_pos;
5103 retval = 1 + (it->area == TEXT_AREA);
5104 }
5105 #ifdef HAVE_WINDOW_SYSTEM
5106 else
5107 {
5108 it->what = IT_IMAGE;
5109 it->image_id = lookup_image (it->f, value);
5110 it->position = start_pos;
5111 it->object = NILP (object) ? it->w->contents : object;
5112 it->method = GET_FROM_IMAGE;
5113
5114 /* Say that we haven't consumed the characters with
5115 `display' property yet. The call to pop_it in
5116 set_iterator_to_next will clean this up. */
5117 *position = start_pos;
5118 }
5119 #endif /* HAVE_WINDOW_SYSTEM */
5120
5121 return retval;
5122 }
5123
5124 /* Invalid property or property not supported. Restore
5125 POSITION to what it was before. */
5126 *position = start_pos;
5127 return 0;
5128 }
5129
5130 /* Check if PROP is a display property value whose text should be
5131 treated as intangible. OVERLAY is the overlay from which PROP
5132 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5133 specify the buffer position covered by PROP. */
5134
5135 int
5136 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5137 ptrdiff_t charpos, ptrdiff_t bytepos)
5138 {
5139 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5140 struct text_pos position;
5141
5142 SET_TEXT_POS (position, charpos, bytepos);
5143 return handle_display_spec (NULL, prop, Qnil, overlay,
5144 &position, charpos, frame_window_p);
5145 }
5146
5147
5148 /* Return 1 if PROP is a display sub-property value containing STRING.
5149
5150 Implementation note: this and the following function are really
5151 special cases of handle_display_spec and
5152 handle_single_display_spec, and should ideally use the same code.
5153 Until they do, these two pairs must be consistent and must be
5154 modified in sync. */
5155
5156 static int
5157 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5158 {
5159 if (EQ (string, prop))
5160 return 1;
5161
5162 /* Skip over `when FORM'. */
5163 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5164 {
5165 prop = XCDR (prop);
5166 if (!CONSP (prop))
5167 return 0;
5168 /* Actually, the condition following `when' should be eval'ed,
5169 like handle_single_display_spec does, and we should return
5170 zero if it evaluates to nil. However, this function is
5171 called only when the buffer was already displayed and some
5172 glyph in the glyph matrix was found to come from a display
5173 string. Therefore, the condition was already evaluated, and
5174 the result was non-nil, otherwise the display string wouldn't
5175 have been displayed and we would have never been called for
5176 this property. Thus, we can skip the evaluation and assume
5177 its result is non-nil. */
5178 prop = XCDR (prop);
5179 }
5180
5181 if (CONSP (prop))
5182 /* Skip over `margin LOCATION'. */
5183 if (EQ (XCAR (prop), Qmargin))
5184 {
5185 prop = XCDR (prop);
5186 if (!CONSP (prop))
5187 return 0;
5188
5189 prop = XCDR (prop);
5190 if (!CONSP (prop))
5191 return 0;
5192 }
5193
5194 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5195 }
5196
5197
5198 /* Return 1 if STRING appears in the `display' property PROP. */
5199
5200 static int
5201 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5202 {
5203 if (CONSP (prop)
5204 && !EQ (XCAR (prop), Qwhen)
5205 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5206 {
5207 /* A list of sub-properties. */
5208 while (CONSP (prop))
5209 {
5210 if (single_display_spec_string_p (XCAR (prop), string))
5211 return 1;
5212 prop = XCDR (prop);
5213 }
5214 }
5215 else if (VECTORP (prop))
5216 {
5217 /* A vector of sub-properties. */
5218 ptrdiff_t i;
5219 for (i = 0; i < ASIZE (prop); ++i)
5220 if (single_display_spec_string_p (AREF (prop, i), string))
5221 return 1;
5222 }
5223 else
5224 return single_display_spec_string_p (prop, string);
5225
5226 return 0;
5227 }
5228
5229 /* Look for STRING in overlays and text properties in the current
5230 buffer, between character positions FROM and TO (excluding TO).
5231 BACK_P non-zero means look back (in this case, TO is supposed to be
5232 less than FROM).
5233 Value is the first character position where STRING was found, or
5234 zero if it wasn't found before hitting TO.
5235
5236 This function may only use code that doesn't eval because it is
5237 called asynchronously from note_mouse_highlight. */
5238
5239 static ptrdiff_t
5240 string_buffer_position_lim (Lisp_Object string,
5241 ptrdiff_t from, ptrdiff_t to, int back_p)
5242 {
5243 Lisp_Object limit, prop, pos;
5244 int found = 0;
5245
5246 pos = make_number (max (from, BEGV));
5247
5248 if (!back_p) /* looking forward */
5249 {
5250 limit = make_number (min (to, ZV));
5251 while (!found && !EQ (pos, limit))
5252 {
5253 prop = Fget_char_property (pos, Qdisplay, Qnil);
5254 if (!NILP (prop) && display_prop_string_p (prop, string))
5255 found = 1;
5256 else
5257 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5258 limit);
5259 }
5260 }
5261 else /* looking back */
5262 {
5263 limit = make_number (max (to, BEGV));
5264 while (!found && !EQ (pos, limit))
5265 {
5266 prop = Fget_char_property (pos, Qdisplay, Qnil);
5267 if (!NILP (prop) && display_prop_string_p (prop, string))
5268 found = 1;
5269 else
5270 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5271 limit);
5272 }
5273 }
5274
5275 return found ? XINT (pos) : 0;
5276 }
5277
5278 /* Determine which buffer position in current buffer STRING comes from.
5279 AROUND_CHARPOS is an approximate position where it could come from.
5280 Value is the buffer position or 0 if it couldn't be determined.
5281
5282 This function is necessary because we don't record buffer positions
5283 in glyphs generated from strings (to keep struct glyph small).
5284 This function may only use code that doesn't eval because it is
5285 called asynchronously from note_mouse_highlight. */
5286
5287 static ptrdiff_t
5288 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5289 {
5290 const int MAX_DISTANCE = 1000;
5291 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5292 around_charpos + MAX_DISTANCE,
5293 0);
5294
5295 if (!found)
5296 found = string_buffer_position_lim (string, around_charpos,
5297 around_charpos - MAX_DISTANCE, 1);
5298 return found;
5299 }
5300
5301
5302 \f
5303 /***********************************************************************
5304 `composition' property
5305 ***********************************************************************/
5306
5307 /* Set up iterator IT from `composition' property at its current
5308 position. Called from handle_stop. */
5309
5310 static enum prop_handled
5311 handle_composition_prop (struct it *it)
5312 {
5313 Lisp_Object prop, string;
5314 ptrdiff_t pos, pos_byte, start, end;
5315
5316 if (STRINGP (it->string))
5317 {
5318 unsigned char *s;
5319
5320 pos = IT_STRING_CHARPOS (*it);
5321 pos_byte = IT_STRING_BYTEPOS (*it);
5322 string = it->string;
5323 s = SDATA (string) + pos_byte;
5324 it->c = STRING_CHAR (s);
5325 }
5326 else
5327 {
5328 pos = IT_CHARPOS (*it);
5329 pos_byte = IT_BYTEPOS (*it);
5330 string = Qnil;
5331 it->c = FETCH_CHAR (pos_byte);
5332 }
5333
5334 /* If there's a valid composition and point is not inside of the
5335 composition (in the case that the composition is from the current
5336 buffer), draw a glyph composed from the composition components. */
5337 if (find_composition (pos, -1, &start, &end, &prop, string)
5338 && composition_valid_p (start, end, prop)
5339 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5340 {
5341 if (start < pos)
5342 /* As we can't handle this situation (perhaps font-lock added
5343 a new composition), we just return here hoping that next
5344 redisplay will detect this composition much earlier. */
5345 return HANDLED_NORMALLY;
5346 if (start != pos)
5347 {
5348 if (STRINGP (it->string))
5349 pos_byte = string_char_to_byte (it->string, start);
5350 else
5351 pos_byte = CHAR_TO_BYTE (start);
5352 }
5353 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5354 prop, string);
5355
5356 if (it->cmp_it.id >= 0)
5357 {
5358 it->cmp_it.ch = -1;
5359 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5360 it->cmp_it.nglyphs = -1;
5361 }
5362 }
5363
5364 return HANDLED_NORMALLY;
5365 }
5366
5367
5368 \f
5369 /***********************************************************************
5370 Overlay strings
5371 ***********************************************************************/
5372
5373 /* The following structure is used to record overlay strings for
5374 later sorting in load_overlay_strings. */
5375
5376 struct overlay_entry
5377 {
5378 Lisp_Object overlay;
5379 Lisp_Object string;
5380 EMACS_INT priority;
5381 int after_string_p;
5382 };
5383
5384
5385 /* Set up iterator IT from overlay strings at its current position.
5386 Called from handle_stop. */
5387
5388 static enum prop_handled
5389 handle_overlay_change (struct it *it)
5390 {
5391 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5392 return HANDLED_RECOMPUTE_PROPS;
5393 else
5394 return HANDLED_NORMALLY;
5395 }
5396
5397
5398 /* Set up the next overlay string for delivery by IT, if there is an
5399 overlay string to deliver. Called by set_iterator_to_next when the
5400 end of the current overlay string is reached. If there are more
5401 overlay strings to display, IT->string and
5402 IT->current.overlay_string_index are set appropriately here.
5403 Otherwise IT->string is set to nil. */
5404
5405 static void
5406 next_overlay_string (struct it *it)
5407 {
5408 ++it->current.overlay_string_index;
5409 if (it->current.overlay_string_index == it->n_overlay_strings)
5410 {
5411 /* No more overlay strings. Restore IT's settings to what
5412 they were before overlay strings were processed, and
5413 continue to deliver from current_buffer. */
5414
5415 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5416 pop_it (it);
5417 eassert (it->sp > 0
5418 || (NILP (it->string)
5419 && it->method == GET_FROM_BUFFER
5420 && it->stop_charpos >= BEGV
5421 && it->stop_charpos <= it->end_charpos));
5422 it->current.overlay_string_index = -1;
5423 it->n_overlay_strings = 0;
5424 it->overlay_strings_charpos = -1;
5425 /* If there's an empty display string on the stack, pop the
5426 stack, to resync the bidi iterator with IT's position. Such
5427 empty strings are pushed onto the stack in
5428 get_overlay_strings_1. */
5429 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5430 pop_it (it);
5431
5432 /* If we're at the end of the buffer, record that we have
5433 processed the overlay strings there already, so that
5434 next_element_from_buffer doesn't try it again. */
5435 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5436 it->overlay_strings_at_end_processed_p = true;
5437 }
5438 else
5439 {
5440 /* There are more overlay strings to process. If
5441 IT->current.overlay_string_index has advanced to a position
5442 where we must load IT->overlay_strings with more strings, do
5443 it. We must load at the IT->overlay_strings_charpos where
5444 IT->n_overlay_strings was originally computed; when invisible
5445 text is present, this might not be IT_CHARPOS (Bug#7016). */
5446 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5447
5448 if (it->current.overlay_string_index && i == 0)
5449 load_overlay_strings (it, it->overlay_strings_charpos);
5450
5451 /* Initialize IT to deliver display elements from the overlay
5452 string. */
5453 it->string = it->overlay_strings[i];
5454 it->multibyte_p = STRING_MULTIBYTE (it->string);
5455 SET_TEXT_POS (it->current.string_pos, 0, 0);
5456 it->method = GET_FROM_STRING;
5457 it->stop_charpos = 0;
5458 it->end_charpos = SCHARS (it->string);
5459 if (it->cmp_it.stop_pos >= 0)
5460 it->cmp_it.stop_pos = 0;
5461 it->prev_stop = 0;
5462 it->base_level_stop = 0;
5463
5464 /* Set up the bidi iterator for this overlay string. */
5465 if (it->bidi_p)
5466 {
5467 it->bidi_it.string.lstring = it->string;
5468 it->bidi_it.string.s = NULL;
5469 it->bidi_it.string.schars = SCHARS (it->string);
5470 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5471 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5472 it->bidi_it.string.unibyte = !it->multibyte_p;
5473 it->bidi_it.w = it->w;
5474 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5475 }
5476 }
5477
5478 CHECK_IT (it);
5479 }
5480
5481
5482 /* Compare two overlay_entry structures E1 and E2. Used as a
5483 comparison function for qsort in load_overlay_strings. Overlay
5484 strings for the same position are sorted so that
5485
5486 1. All after-strings come in front of before-strings, except
5487 when they come from the same overlay.
5488
5489 2. Within after-strings, strings are sorted so that overlay strings
5490 from overlays with higher priorities come first.
5491
5492 2. Within before-strings, strings are sorted so that overlay
5493 strings from overlays with higher priorities come last.
5494
5495 Value is analogous to strcmp. */
5496
5497
5498 static int
5499 compare_overlay_entries (const void *e1, const void *e2)
5500 {
5501 struct overlay_entry const *entry1 = e1;
5502 struct overlay_entry const *entry2 = e2;
5503 int result;
5504
5505 if (entry1->after_string_p != entry2->after_string_p)
5506 {
5507 /* Let after-strings appear in front of before-strings if
5508 they come from different overlays. */
5509 if (EQ (entry1->overlay, entry2->overlay))
5510 result = entry1->after_string_p ? 1 : -1;
5511 else
5512 result = entry1->after_string_p ? -1 : 1;
5513 }
5514 else if (entry1->priority != entry2->priority)
5515 {
5516 if (entry1->after_string_p)
5517 /* After-strings sorted in order of decreasing priority. */
5518 result = entry2->priority < entry1->priority ? -1 : 1;
5519 else
5520 /* Before-strings sorted in order of increasing priority. */
5521 result = entry1->priority < entry2->priority ? -1 : 1;
5522 }
5523 else
5524 result = 0;
5525
5526 return result;
5527 }
5528
5529
5530 /* Load the vector IT->overlay_strings with overlay strings from IT's
5531 current buffer position, or from CHARPOS if that is > 0. Set
5532 IT->n_overlays to the total number of overlay strings found.
5533
5534 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5535 a time. On entry into load_overlay_strings,
5536 IT->current.overlay_string_index gives the number of overlay
5537 strings that have already been loaded by previous calls to this
5538 function.
5539
5540 IT->add_overlay_start contains an additional overlay start
5541 position to consider for taking overlay strings from, if non-zero.
5542 This position comes into play when the overlay has an `invisible'
5543 property, and both before and after-strings. When we've skipped to
5544 the end of the overlay, because of its `invisible' property, we
5545 nevertheless want its before-string to appear.
5546 IT->add_overlay_start will contain the overlay start position
5547 in this case.
5548
5549 Overlay strings are sorted so that after-string strings come in
5550 front of before-string strings. Within before and after-strings,
5551 strings are sorted by overlay priority. See also function
5552 compare_overlay_entries. */
5553
5554 static void
5555 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5556 {
5557 Lisp_Object overlay, window, str, invisible;
5558 struct Lisp_Overlay *ov;
5559 ptrdiff_t start, end;
5560 ptrdiff_t size = 20;
5561 ptrdiff_t n = 0, i, j;
5562 int invis_p;
5563 struct overlay_entry *entries = alloca (size * sizeof *entries);
5564 USE_SAFE_ALLOCA;
5565
5566 if (charpos <= 0)
5567 charpos = IT_CHARPOS (*it);
5568
5569 /* Append the overlay string STRING of overlay OVERLAY to vector
5570 `entries' which has size `size' and currently contains `n'
5571 elements. AFTER_P non-zero means STRING is an after-string of
5572 OVERLAY. */
5573 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5574 do \
5575 { \
5576 Lisp_Object priority; \
5577 \
5578 if (n == size) \
5579 { \
5580 struct overlay_entry *old = entries; \
5581 SAFE_NALLOCA (entries, 2, size); \
5582 memcpy (entries, old, size * sizeof *entries); \
5583 size *= 2; \
5584 } \
5585 \
5586 entries[n].string = (STRING); \
5587 entries[n].overlay = (OVERLAY); \
5588 priority = Foverlay_get ((OVERLAY), Qpriority); \
5589 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5590 entries[n].after_string_p = (AFTER_P); \
5591 ++n; \
5592 } \
5593 while (0)
5594
5595 /* Process overlay before the overlay center. */
5596 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5597 {
5598 XSETMISC (overlay, ov);
5599 eassert (OVERLAYP (overlay));
5600 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5601 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5602
5603 if (end < charpos)
5604 break;
5605
5606 /* Skip this overlay if it doesn't start or end at IT's current
5607 position. */
5608 if (end != charpos && start != charpos)
5609 continue;
5610
5611 /* Skip this overlay if it doesn't apply to IT->w. */
5612 window = Foverlay_get (overlay, Qwindow);
5613 if (WINDOWP (window) && XWINDOW (window) != it->w)
5614 continue;
5615
5616 /* If the text ``under'' the overlay is invisible, both before-
5617 and after-strings from this overlay are visible; start and
5618 end position are indistinguishable. */
5619 invisible = Foverlay_get (overlay, Qinvisible);
5620 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5621
5622 /* If overlay has a non-empty before-string, record it. */
5623 if ((start == charpos || (end == charpos && invis_p))
5624 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5625 && SCHARS (str))
5626 RECORD_OVERLAY_STRING (overlay, str, 0);
5627
5628 /* If overlay has a non-empty after-string, record it. */
5629 if ((end == charpos || (start == charpos && invis_p))
5630 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5631 && SCHARS (str))
5632 RECORD_OVERLAY_STRING (overlay, str, 1);
5633 }
5634
5635 /* Process overlays after the overlay center. */
5636 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5637 {
5638 XSETMISC (overlay, ov);
5639 eassert (OVERLAYP (overlay));
5640 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5641 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5642
5643 if (start > charpos)
5644 break;
5645
5646 /* Skip this overlay if it doesn't start or end at IT's current
5647 position. */
5648 if (end != charpos && start != charpos)
5649 continue;
5650
5651 /* Skip this overlay if it doesn't apply to IT->w. */
5652 window = Foverlay_get (overlay, Qwindow);
5653 if (WINDOWP (window) && XWINDOW (window) != it->w)
5654 continue;
5655
5656 /* If the text ``under'' the overlay is invisible, it has a zero
5657 dimension, and both before- and after-strings apply. */
5658 invisible = Foverlay_get (overlay, Qinvisible);
5659 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5660
5661 /* If overlay has a non-empty before-string, record it. */
5662 if ((start == charpos || (end == charpos && invis_p))
5663 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5664 && SCHARS (str))
5665 RECORD_OVERLAY_STRING (overlay, str, 0);
5666
5667 /* If overlay has a non-empty after-string, record it. */
5668 if ((end == charpos || (start == charpos && invis_p))
5669 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5670 && SCHARS (str))
5671 RECORD_OVERLAY_STRING (overlay, str, 1);
5672 }
5673
5674 #undef RECORD_OVERLAY_STRING
5675
5676 /* Sort entries. */
5677 if (n > 1)
5678 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5679
5680 /* Record number of overlay strings, and where we computed it. */
5681 it->n_overlay_strings = n;
5682 it->overlay_strings_charpos = charpos;
5683
5684 /* IT->current.overlay_string_index is the number of overlay strings
5685 that have already been consumed by IT. Copy some of the
5686 remaining overlay strings to IT->overlay_strings. */
5687 i = 0;
5688 j = it->current.overlay_string_index;
5689 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5690 {
5691 it->overlay_strings[i] = entries[j].string;
5692 it->string_overlays[i++] = entries[j++].overlay;
5693 }
5694
5695 CHECK_IT (it);
5696 SAFE_FREE ();
5697 }
5698
5699
5700 /* Get the first chunk of overlay strings at IT's current buffer
5701 position, or at CHARPOS if that is > 0. Value is non-zero if at
5702 least one overlay string was found. */
5703
5704 static int
5705 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5706 {
5707 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5708 process. This fills IT->overlay_strings with strings, and sets
5709 IT->n_overlay_strings to the total number of strings to process.
5710 IT->pos.overlay_string_index has to be set temporarily to zero
5711 because load_overlay_strings needs this; it must be set to -1
5712 when no overlay strings are found because a zero value would
5713 indicate a position in the first overlay string. */
5714 it->current.overlay_string_index = 0;
5715 load_overlay_strings (it, charpos);
5716
5717 /* If we found overlay strings, set up IT to deliver display
5718 elements from the first one. Otherwise set up IT to deliver
5719 from current_buffer. */
5720 if (it->n_overlay_strings)
5721 {
5722 /* Make sure we know settings in current_buffer, so that we can
5723 restore meaningful values when we're done with the overlay
5724 strings. */
5725 if (compute_stop_p)
5726 compute_stop_pos (it);
5727 eassert (it->face_id >= 0);
5728
5729 /* Save IT's settings. They are restored after all overlay
5730 strings have been processed. */
5731 eassert (!compute_stop_p || it->sp == 0);
5732
5733 /* When called from handle_stop, there might be an empty display
5734 string loaded. In that case, don't bother saving it. But
5735 don't use this optimization with the bidi iterator, since we
5736 need the corresponding pop_it call to resync the bidi
5737 iterator's position with IT's position, after we are done
5738 with the overlay strings. (The corresponding call to pop_it
5739 in case of an empty display string is in
5740 next_overlay_string.) */
5741 if (!(!it->bidi_p
5742 && STRINGP (it->string) && !SCHARS (it->string)))
5743 push_it (it, NULL);
5744
5745 /* Set up IT to deliver display elements from the first overlay
5746 string. */
5747 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5748 it->string = it->overlay_strings[0];
5749 it->from_overlay = Qnil;
5750 it->stop_charpos = 0;
5751 eassert (STRINGP (it->string));
5752 it->end_charpos = SCHARS (it->string);
5753 it->prev_stop = 0;
5754 it->base_level_stop = 0;
5755 it->multibyte_p = STRING_MULTIBYTE (it->string);
5756 it->method = GET_FROM_STRING;
5757 it->from_disp_prop_p = 0;
5758
5759 /* Force paragraph direction to be that of the parent
5760 buffer. */
5761 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5762 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5763 else
5764 it->paragraph_embedding = L2R;
5765
5766 /* Set up the bidi iterator for this overlay string. */
5767 if (it->bidi_p)
5768 {
5769 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5770
5771 it->bidi_it.string.lstring = it->string;
5772 it->bidi_it.string.s = NULL;
5773 it->bidi_it.string.schars = SCHARS (it->string);
5774 it->bidi_it.string.bufpos = pos;
5775 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5776 it->bidi_it.string.unibyte = !it->multibyte_p;
5777 it->bidi_it.w = it->w;
5778 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5779 }
5780 return 1;
5781 }
5782
5783 it->current.overlay_string_index = -1;
5784 return 0;
5785 }
5786
5787 static int
5788 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5789 {
5790 it->string = Qnil;
5791 it->method = GET_FROM_BUFFER;
5792
5793 (void) get_overlay_strings_1 (it, charpos, 1);
5794
5795 CHECK_IT (it);
5796
5797 /* Value is non-zero if we found at least one overlay string. */
5798 return STRINGP (it->string);
5799 }
5800
5801
5802 \f
5803 /***********************************************************************
5804 Saving and restoring state
5805 ***********************************************************************/
5806
5807 /* Save current settings of IT on IT->stack. Called, for example,
5808 before setting up IT for an overlay string, to be able to restore
5809 IT's settings to what they were after the overlay string has been
5810 processed. If POSITION is non-NULL, it is the position to save on
5811 the stack instead of IT->position. */
5812
5813 static void
5814 push_it (struct it *it, struct text_pos *position)
5815 {
5816 struct iterator_stack_entry *p;
5817
5818 eassert (it->sp < IT_STACK_SIZE);
5819 p = it->stack + it->sp;
5820
5821 p->stop_charpos = it->stop_charpos;
5822 p->prev_stop = it->prev_stop;
5823 p->base_level_stop = it->base_level_stop;
5824 p->cmp_it = it->cmp_it;
5825 eassert (it->face_id >= 0);
5826 p->face_id = it->face_id;
5827 p->string = it->string;
5828 p->method = it->method;
5829 p->from_overlay = it->from_overlay;
5830 switch (p->method)
5831 {
5832 case GET_FROM_IMAGE:
5833 p->u.image.object = it->object;
5834 p->u.image.image_id = it->image_id;
5835 p->u.image.slice = it->slice;
5836 break;
5837 case GET_FROM_STRETCH:
5838 p->u.stretch.object = it->object;
5839 break;
5840 }
5841 p->position = position ? *position : it->position;
5842 p->current = it->current;
5843 p->end_charpos = it->end_charpos;
5844 p->string_nchars = it->string_nchars;
5845 p->area = it->area;
5846 p->multibyte_p = it->multibyte_p;
5847 p->avoid_cursor_p = it->avoid_cursor_p;
5848 p->space_width = it->space_width;
5849 p->font_height = it->font_height;
5850 p->voffset = it->voffset;
5851 p->string_from_display_prop_p = it->string_from_display_prop_p;
5852 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5853 p->display_ellipsis_p = 0;
5854 p->line_wrap = it->line_wrap;
5855 p->bidi_p = it->bidi_p;
5856 p->paragraph_embedding = it->paragraph_embedding;
5857 p->from_disp_prop_p = it->from_disp_prop_p;
5858 ++it->sp;
5859
5860 /* Save the state of the bidi iterator as well. */
5861 if (it->bidi_p)
5862 bidi_push_it (&it->bidi_it);
5863 }
5864
5865 static void
5866 iterate_out_of_display_property (struct it *it)
5867 {
5868 int buffer_p = !STRINGP (it->string);
5869 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5870 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5871
5872 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5873
5874 /* Maybe initialize paragraph direction. If we are at the beginning
5875 of a new paragraph, next_element_from_buffer may not have a
5876 chance to do that. */
5877 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5878 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5879 /* prev_stop can be zero, so check against BEGV as well. */
5880 while (it->bidi_it.charpos >= bob
5881 && it->prev_stop <= it->bidi_it.charpos
5882 && it->bidi_it.charpos < CHARPOS (it->position)
5883 && it->bidi_it.charpos < eob)
5884 bidi_move_to_visually_next (&it->bidi_it);
5885 /* Record the stop_pos we just crossed, for when we cross it
5886 back, maybe. */
5887 if (it->bidi_it.charpos > CHARPOS (it->position))
5888 it->prev_stop = CHARPOS (it->position);
5889 /* If we ended up not where pop_it put us, resync IT's
5890 positional members with the bidi iterator. */
5891 if (it->bidi_it.charpos != CHARPOS (it->position))
5892 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5893 if (buffer_p)
5894 it->current.pos = it->position;
5895 else
5896 it->current.string_pos = it->position;
5897 }
5898
5899 /* Restore IT's settings from IT->stack. Called, for example, when no
5900 more overlay strings must be processed, and we return to delivering
5901 display elements from a buffer, or when the end of a string from a
5902 `display' property is reached and we return to delivering display
5903 elements from an overlay string, or from a buffer. */
5904
5905 static void
5906 pop_it (struct it *it)
5907 {
5908 struct iterator_stack_entry *p;
5909 int from_display_prop = it->from_disp_prop_p;
5910
5911 eassert (it->sp > 0);
5912 --it->sp;
5913 p = it->stack + it->sp;
5914 it->stop_charpos = p->stop_charpos;
5915 it->prev_stop = p->prev_stop;
5916 it->base_level_stop = p->base_level_stop;
5917 it->cmp_it = p->cmp_it;
5918 it->face_id = p->face_id;
5919 it->current = p->current;
5920 it->position = p->position;
5921 it->string = p->string;
5922 it->from_overlay = p->from_overlay;
5923 if (NILP (it->string))
5924 SET_TEXT_POS (it->current.string_pos, -1, -1);
5925 it->method = p->method;
5926 switch (it->method)
5927 {
5928 case GET_FROM_IMAGE:
5929 it->image_id = p->u.image.image_id;
5930 it->object = p->u.image.object;
5931 it->slice = p->u.image.slice;
5932 break;
5933 case GET_FROM_STRETCH:
5934 it->object = p->u.stretch.object;
5935 break;
5936 case GET_FROM_BUFFER:
5937 it->object = it->w->contents;
5938 break;
5939 case GET_FROM_STRING:
5940 it->object = it->string;
5941 break;
5942 case GET_FROM_DISPLAY_VECTOR:
5943 if (it->s)
5944 it->method = GET_FROM_C_STRING;
5945 else if (STRINGP (it->string))
5946 it->method = GET_FROM_STRING;
5947 else
5948 {
5949 it->method = GET_FROM_BUFFER;
5950 it->object = it->w->contents;
5951 }
5952 }
5953 it->end_charpos = p->end_charpos;
5954 it->string_nchars = p->string_nchars;
5955 it->area = p->area;
5956 it->multibyte_p = p->multibyte_p;
5957 it->avoid_cursor_p = p->avoid_cursor_p;
5958 it->space_width = p->space_width;
5959 it->font_height = p->font_height;
5960 it->voffset = p->voffset;
5961 it->string_from_display_prop_p = p->string_from_display_prop_p;
5962 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5963 it->line_wrap = p->line_wrap;
5964 it->bidi_p = p->bidi_p;
5965 it->paragraph_embedding = p->paragraph_embedding;
5966 it->from_disp_prop_p = p->from_disp_prop_p;
5967 if (it->bidi_p)
5968 {
5969 bidi_pop_it (&it->bidi_it);
5970 /* Bidi-iterate until we get out of the portion of text, if any,
5971 covered by a `display' text property or by an overlay with
5972 `display' property. (We cannot just jump there, because the
5973 internal coherency of the bidi iterator state can not be
5974 preserved across such jumps.) We also must determine the
5975 paragraph base direction if the overlay we just processed is
5976 at the beginning of a new paragraph. */
5977 if (from_display_prop
5978 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5979 iterate_out_of_display_property (it);
5980
5981 eassert ((BUFFERP (it->object)
5982 && IT_CHARPOS (*it) == it->bidi_it.charpos
5983 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5984 || (STRINGP (it->object)
5985 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5986 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5987 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5988 }
5989 }
5990
5991
5992 \f
5993 /***********************************************************************
5994 Moving over lines
5995 ***********************************************************************/
5996
5997 /* Set IT's current position to the previous line start. */
5998
5999 static void
6000 back_to_previous_line_start (struct it *it)
6001 {
6002 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6003
6004 DEC_BOTH (cp, bp);
6005 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6006 }
6007
6008
6009 /* Move IT to the next line start.
6010
6011 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6012 we skipped over part of the text (as opposed to moving the iterator
6013 continuously over the text). Otherwise, don't change the value
6014 of *SKIPPED_P.
6015
6016 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6017 iterator on the newline, if it was found.
6018
6019 Newlines may come from buffer text, overlay strings, or strings
6020 displayed via the `display' property. That's the reason we can't
6021 simply use find_newline_no_quit.
6022
6023 Note that this function may not skip over invisible text that is so
6024 because of text properties and immediately follows a newline. If
6025 it would, function reseat_at_next_visible_line_start, when called
6026 from set_iterator_to_next, would effectively make invisible
6027 characters following a newline part of the wrong glyph row, which
6028 leads to wrong cursor motion. */
6029
6030 static int
6031 forward_to_next_line_start (struct it *it, int *skipped_p,
6032 struct bidi_it *bidi_it_prev)
6033 {
6034 ptrdiff_t old_selective;
6035 int newline_found_p, n;
6036 const int MAX_NEWLINE_DISTANCE = 500;
6037
6038 /* If already on a newline, just consume it to avoid unintended
6039 skipping over invisible text below. */
6040 if (it->what == IT_CHARACTER
6041 && it->c == '\n'
6042 && CHARPOS (it->position) == IT_CHARPOS (*it))
6043 {
6044 if (it->bidi_p && bidi_it_prev)
6045 *bidi_it_prev = it->bidi_it;
6046 set_iterator_to_next (it, 0);
6047 it->c = 0;
6048 return 1;
6049 }
6050
6051 /* Don't handle selective display in the following. It's (a)
6052 unnecessary because it's done by the caller, and (b) leads to an
6053 infinite recursion because next_element_from_ellipsis indirectly
6054 calls this function. */
6055 old_selective = it->selective;
6056 it->selective = 0;
6057
6058 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6059 from buffer text. */
6060 for (n = newline_found_p = 0;
6061 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6062 n += STRINGP (it->string) ? 0 : 1)
6063 {
6064 if (!get_next_display_element (it))
6065 return 0;
6066 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6067 if (newline_found_p && it->bidi_p && bidi_it_prev)
6068 *bidi_it_prev = it->bidi_it;
6069 set_iterator_to_next (it, 0);
6070 }
6071
6072 /* If we didn't find a newline near enough, see if we can use a
6073 short-cut. */
6074 if (!newline_found_p)
6075 {
6076 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6077 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6078 1, &bytepos);
6079 Lisp_Object pos;
6080
6081 eassert (!STRINGP (it->string));
6082
6083 /* If there isn't any `display' property in sight, and no
6084 overlays, we can just use the position of the newline in
6085 buffer text. */
6086 if (it->stop_charpos >= limit
6087 || ((pos = Fnext_single_property_change (make_number (start),
6088 Qdisplay, Qnil,
6089 make_number (limit)),
6090 NILP (pos))
6091 && next_overlay_change (start) == ZV))
6092 {
6093 if (!it->bidi_p)
6094 {
6095 IT_CHARPOS (*it) = limit;
6096 IT_BYTEPOS (*it) = bytepos;
6097 }
6098 else
6099 {
6100 struct bidi_it bprev;
6101
6102 /* Help bidi.c avoid expensive searches for display
6103 properties and overlays, by telling it that there are
6104 none up to `limit'. */
6105 if (it->bidi_it.disp_pos < limit)
6106 {
6107 it->bidi_it.disp_pos = limit;
6108 it->bidi_it.disp_prop = 0;
6109 }
6110 do {
6111 bprev = it->bidi_it;
6112 bidi_move_to_visually_next (&it->bidi_it);
6113 } while (it->bidi_it.charpos != limit);
6114 IT_CHARPOS (*it) = limit;
6115 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6116 if (bidi_it_prev)
6117 *bidi_it_prev = bprev;
6118 }
6119 *skipped_p = newline_found_p = true;
6120 }
6121 else
6122 {
6123 while (get_next_display_element (it)
6124 && !newline_found_p)
6125 {
6126 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6127 if (newline_found_p && it->bidi_p && bidi_it_prev)
6128 *bidi_it_prev = it->bidi_it;
6129 set_iterator_to_next (it, 0);
6130 }
6131 }
6132 }
6133
6134 it->selective = old_selective;
6135 return newline_found_p;
6136 }
6137
6138
6139 /* Set IT's current position to the previous visible line start. Skip
6140 invisible text that is so either due to text properties or due to
6141 selective display. Caution: this does not change IT->current_x and
6142 IT->hpos. */
6143
6144 static void
6145 back_to_previous_visible_line_start (struct it *it)
6146 {
6147 while (IT_CHARPOS (*it) > BEGV)
6148 {
6149 back_to_previous_line_start (it);
6150
6151 if (IT_CHARPOS (*it) <= BEGV)
6152 break;
6153
6154 /* If selective > 0, then lines indented more than its value are
6155 invisible. */
6156 if (it->selective > 0
6157 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6158 it->selective))
6159 continue;
6160
6161 /* Check the newline before point for invisibility. */
6162 {
6163 Lisp_Object prop;
6164 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6165 Qinvisible, it->window);
6166 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6167 continue;
6168 }
6169
6170 if (IT_CHARPOS (*it) <= BEGV)
6171 break;
6172
6173 {
6174 struct it it2;
6175 void *it2data = NULL;
6176 ptrdiff_t pos;
6177 ptrdiff_t beg, end;
6178 Lisp_Object val, overlay;
6179
6180 SAVE_IT (it2, *it, it2data);
6181
6182 /* If newline is part of a composition, continue from start of composition */
6183 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6184 && beg < IT_CHARPOS (*it))
6185 goto replaced;
6186
6187 /* If newline is replaced by a display property, find start of overlay
6188 or interval and continue search from that point. */
6189 pos = --IT_CHARPOS (it2);
6190 --IT_BYTEPOS (it2);
6191 it2.sp = 0;
6192 bidi_unshelve_cache (NULL, 0);
6193 it2.string_from_display_prop_p = 0;
6194 it2.from_disp_prop_p = 0;
6195 if (handle_display_prop (&it2) == HANDLED_RETURN
6196 && !NILP (val = get_char_property_and_overlay
6197 (make_number (pos), Qdisplay, Qnil, &overlay))
6198 && (OVERLAYP (overlay)
6199 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6200 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6201 {
6202 RESTORE_IT (it, it, it2data);
6203 goto replaced;
6204 }
6205
6206 /* Newline is not replaced by anything -- so we are done. */
6207 RESTORE_IT (it, it, it2data);
6208 break;
6209
6210 replaced:
6211 if (beg < BEGV)
6212 beg = BEGV;
6213 IT_CHARPOS (*it) = beg;
6214 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6215 }
6216 }
6217
6218 it->continuation_lines_width = 0;
6219
6220 eassert (IT_CHARPOS (*it) >= BEGV);
6221 eassert (IT_CHARPOS (*it) == BEGV
6222 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6223 CHECK_IT (it);
6224 }
6225
6226
6227 /* Reseat iterator IT at the previous visible line start. Skip
6228 invisible text that is so either due to text properties or due to
6229 selective display. At the end, update IT's overlay information,
6230 face information etc. */
6231
6232 void
6233 reseat_at_previous_visible_line_start (struct it *it)
6234 {
6235 back_to_previous_visible_line_start (it);
6236 reseat (it, it->current.pos, 1);
6237 CHECK_IT (it);
6238 }
6239
6240
6241 /* Reseat iterator IT on the next visible line start in the current
6242 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6243 preceding the line start. Skip over invisible text that is so
6244 because of selective display. Compute faces, overlays etc at the
6245 new position. Note that this function does not skip over text that
6246 is invisible because of text properties. */
6247
6248 static void
6249 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6250 {
6251 int newline_found_p, skipped_p = 0;
6252 struct bidi_it bidi_it_prev;
6253
6254 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6255
6256 /* Skip over lines that are invisible because they are indented
6257 more than the value of IT->selective. */
6258 if (it->selective > 0)
6259 while (IT_CHARPOS (*it) < ZV
6260 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6261 it->selective))
6262 {
6263 eassert (IT_BYTEPOS (*it) == BEGV
6264 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6265 newline_found_p =
6266 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6267 }
6268
6269 /* Position on the newline if that's what's requested. */
6270 if (on_newline_p && newline_found_p)
6271 {
6272 if (STRINGP (it->string))
6273 {
6274 if (IT_STRING_CHARPOS (*it) > 0)
6275 {
6276 if (!it->bidi_p)
6277 {
6278 --IT_STRING_CHARPOS (*it);
6279 --IT_STRING_BYTEPOS (*it);
6280 }
6281 else
6282 {
6283 /* We need to restore the bidi iterator to the state
6284 it had on the newline, and resync the IT's
6285 position with that. */
6286 it->bidi_it = bidi_it_prev;
6287 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6288 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6289 }
6290 }
6291 }
6292 else if (IT_CHARPOS (*it) > BEGV)
6293 {
6294 if (!it->bidi_p)
6295 {
6296 --IT_CHARPOS (*it);
6297 --IT_BYTEPOS (*it);
6298 }
6299 else
6300 {
6301 /* We need to restore the bidi iterator to the state it
6302 had on the newline and resync IT with that. */
6303 it->bidi_it = bidi_it_prev;
6304 IT_CHARPOS (*it) = it->bidi_it.charpos;
6305 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6306 }
6307 reseat (it, it->current.pos, 0);
6308 }
6309 }
6310 else if (skipped_p)
6311 reseat (it, it->current.pos, 0);
6312
6313 CHECK_IT (it);
6314 }
6315
6316
6317 \f
6318 /***********************************************************************
6319 Changing an iterator's position
6320 ***********************************************************************/
6321
6322 /* Change IT's current position to POS in current_buffer. If FORCE_P
6323 is non-zero, always check for text properties at the new position.
6324 Otherwise, text properties are only looked up if POS >=
6325 IT->check_charpos of a property. */
6326
6327 static void
6328 reseat (struct it *it, struct text_pos pos, int force_p)
6329 {
6330 ptrdiff_t original_pos = IT_CHARPOS (*it);
6331
6332 reseat_1 (it, pos, 0);
6333
6334 /* Determine where to check text properties. Avoid doing it
6335 where possible because text property lookup is very expensive. */
6336 if (force_p
6337 || CHARPOS (pos) > it->stop_charpos
6338 || CHARPOS (pos) < original_pos)
6339 {
6340 if (it->bidi_p)
6341 {
6342 /* For bidi iteration, we need to prime prev_stop and
6343 base_level_stop with our best estimations. */
6344 /* Implementation note: Of course, POS is not necessarily a
6345 stop position, so assigning prev_pos to it is a lie; we
6346 should have called compute_stop_backwards. However, if
6347 the current buffer does not include any R2L characters,
6348 that call would be a waste of cycles, because the
6349 iterator will never move back, and thus never cross this
6350 "fake" stop position. So we delay that backward search
6351 until the time we really need it, in next_element_from_buffer. */
6352 if (CHARPOS (pos) != it->prev_stop)
6353 it->prev_stop = CHARPOS (pos);
6354 if (CHARPOS (pos) < it->base_level_stop)
6355 it->base_level_stop = 0; /* meaning it's unknown */
6356 handle_stop (it);
6357 }
6358 else
6359 {
6360 handle_stop (it);
6361 it->prev_stop = it->base_level_stop = 0;
6362 }
6363
6364 }
6365
6366 CHECK_IT (it);
6367 }
6368
6369
6370 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6371 IT->stop_pos to POS, also. */
6372
6373 static void
6374 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6375 {
6376 /* Don't call this function when scanning a C string. */
6377 eassert (it->s == NULL);
6378
6379 /* POS must be a reasonable value. */
6380 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6381
6382 it->current.pos = it->position = pos;
6383 it->end_charpos = ZV;
6384 it->dpvec = NULL;
6385 it->current.dpvec_index = -1;
6386 it->current.overlay_string_index = -1;
6387 IT_STRING_CHARPOS (*it) = -1;
6388 IT_STRING_BYTEPOS (*it) = -1;
6389 it->string = Qnil;
6390 it->method = GET_FROM_BUFFER;
6391 it->object = it->w->contents;
6392 it->area = TEXT_AREA;
6393 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6394 it->sp = 0;
6395 it->string_from_display_prop_p = 0;
6396 it->string_from_prefix_prop_p = 0;
6397
6398 it->from_disp_prop_p = 0;
6399 it->face_before_selective_p = 0;
6400 if (it->bidi_p)
6401 {
6402 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6403 &it->bidi_it);
6404 bidi_unshelve_cache (NULL, 0);
6405 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6406 it->bidi_it.string.s = NULL;
6407 it->bidi_it.string.lstring = Qnil;
6408 it->bidi_it.string.bufpos = 0;
6409 it->bidi_it.string.unibyte = 0;
6410 it->bidi_it.w = it->w;
6411 }
6412
6413 if (set_stop_p)
6414 {
6415 it->stop_charpos = CHARPOS (pos);
6416 it->base_level_stop = CHARPOS (pos);
6417 }
6418 /* This make the information stored in it->cmp_it invalidate. */
6419 it->cmp_it.id = -1;
6420 }
6421
6422
6423 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6424 If S is non-null, it is a C string to iterate over. Otherwise,
6425 STRING gives a Lisp string to iterate over.
6426
6427 If PRECISION > 0, don't return more then PRECISION number of
6428 characters from the string.
6429
6430 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6431 characters have been returned. FIELD_WIDTH < 0 means an infinite
6432 field width.
6433
6434 MULTIBYTE = 0 means disable processing of multibyte characters,
6435 MULTIBYTE > 0 means enable it,
6436 MULTIBYTE < 0 means use IT->multibyte_p.
6437
6438 IT must be initialized via a prior call to init_iterator before
6439 calling this function. */
6440
6441 static void
6442 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6443 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6444 int multibyte)
6445 {
6446 /* No text property checks performed by default, but see below. */
6447 it->stop_charpos = -1;
6448
6449 /* Set iterator position and end position. */
6450 memset (&it->current, 0, sizeof it->current);
6451 it->current.overlay_string_index = -1;
6452 it->current.dpvec_index = -1;
6453 eassert (charpos >= 0);
6454
6455 /* If STRING is specified, use its multibyteness, otherwise use the
6456 setting of MULTIBYTE, if specified. */
6457 if (multibyte >= 0)
6458 it->multibyte_p = multibyte > 0;
6459
6460 /* Bidirectional reordering of strings is controlled by the default
6461 value of bidi-display-reordering. Don't try to reorder while
6462 loading loadup.el, as the necessary character property tables are
6463 not yet available. */
6464 it->bidi_p =
6465 NILP (Vpurify_flag)
6466 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6467
6468 if (s == NULL)
6469 {
6470 eassert (STRINGP (string));
6471 it->string = string;
6472 it->s = NULL;
6473 it->end_charpos = it->string_nchars = SCHARS (string);
6474 it->method = GET_FROM_STRING;
6475 it->current.string_pos = string_pos (charpos, string);
6476
6477 if (it->bidi_p)
6478 {
6479 it->bidi_it.string.lstring = string;
6480 it->bidi_it.string.s = NULL;
6481 it->bidi_it.string.schars = it->end_charpos;
6482 it->bidi_it.string.bufpos = 0;
6483 it->bidi_it.string.from_disp_str = 0;
6484 it->bidi_it.string.unibyte = !it->multibyte_p;
6485 it->bidi_it.w = it->w;
6486 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6487 FRAME_WINDOW_P (it->f), &it->bidi_it);
6488 }
6489 }
6490 else
6491 {
6492 it->s = (const unsigned char *) s;
6493 it->string = Qnil;
6494
6495 /* Note that we use IT->current.pos, not it->current.string_pos,
6496 for displaying C strings. */
6497 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6498 if (it->multibyte_p)
6499 {
6500 it->current.pos = c_string_pos (charpos, s, 1);
6501 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6502 }
6503 else
6504 {
6505 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6506 it->end_charpos = it->string_nchars = strlen (s);
6507 }
6508
6509 if (it->bidi_p)
6510 {
6511 it->bidi_it.string.lstring = Qnil;
6512 it->bidi_it.string.s = (const unsigned char *) s;
6513 it->bidi_it.string.schars = it->end_charpos;
6514 it->bidi_it.string.bufpos = 0;
6515 it->bidi_it.string.from_disp_str = 0;
6516 it->bidi_it.string.unibyte = !it->multibyte_p;
6517 it->bidi_it.w = it->w;
6518 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6519 &it->bidi_it);
6520 }
6521 it->method = GET_FROM_C_STRING;
6522 }
6523
6524 /* PRECISION > 0 means don't return more than PRECISION characters
6525 from the string. */
6526 if (precision > 0 && it->end_charpos - charpos > precision)
6527 {
6528 it->end_charpos = it->string_nchars = charpos + precision;
6529 if (it->bidi_p)
6530 it->bidi_it.string.schars = it->end_charpos;
6531 }
6532
6533 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6534 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6535 FIELD_WIDTH < 0 means infinite field width. This is useful for
6536 padding with `-' at the end of a mode line. */
6537 if (field_width < 0)
6538 field_width = INFINITY;
6539 /* Implementation note: We deliberately don't enlarge
6540 it->bidi_it.string.schars here to fit it->end_charpos, because
6541 the bidi iterator cannot produce characters out of thin air. */
6542 if (field_width > it->end_charpos - charpos)
6543 it->end_charpos = charpos + field_width;
6544
6545 /* Use the standard display table for displaying strings. */
6546 if (DISP_TABLE_P (Vstandard_display_table))
6547 it->dp = XCHAR_TABLE (Vstandard_display_table);
6548
6549 it->stop_charpos = charpos;
6550 it->prev_stop = charpos;
6551 it->base_level_stop = 0;
6552 if (it->bidi_p)
6553 {
6554 it->bidi_it.first_elt = 1;
6555 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6556 it->bidi_it.disp_pos = -1;
6557 }
6558 if (s == NULL && it->multibyte_p)
6559 {
6560 ptrdiff_t endpos = SCHARS (it->string);
6561 if (endpos > it->end_charpos)
6562 endpos = it->end_charpos;
6563 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6564 it->string);
6565 }
6566 CHECK_IT (it);
6567 }
6568
6569
6570 \f
6571 /***********************************************************************
6572 Iteration
6573 ***********************************************************************/
6574
6575 /* Map enum it_method value to corresponding next_element_from_* function. */
6576
6577 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6578 {
6579 next_element_from_buffer,
6580 next_element_from_display_vector,
6581 next_element_from_string,
6582 next_element_from_c_string,
6583 next_element_from_image,
6584 next_element_from_stretch
6585 };
6586
6587 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6588
6589
6590 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6591 (possibly with the following characters). */
6592
6593 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6594 ((IT)->cmp_it.id >= 0 \
6595 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6596 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6597 END_CHARPOS, (IT)->w, \
6598 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6599 (IT)->string)))
6600
6601
6602 /* Lookup the char-table Vglyphless_char_display for character C (-1
6603 if we want information for no-font case), and return the display
6604 method symbol. By side-effect, update it->what and
6605 it->glyphless_method. This function is called from
6606 get_next_display_element for each character element, and from
6607 x_produce_glyphs when no suitable font was found. */
6608
6609 Lisp_Object
6610 lookup_glyphless_char_display (int c, struct it *it)
6611 {
6612 Lisp_Object glyphless_method = Qnil;
6613
6614 if (CHAR_TABLE_P (Vglyphless_char_display)
6615 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6616 {
6617 if (c >= 0)
6618 {
6619 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6620 if (CONSP (glyphless_method))
6621 glyphless_method = FRAME_WINDOW_P (it->f)
6622 ? XCAR (glyphless_method)
6623 : XCDR (glyphless_method);
6624 }
6625 else
6626 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6627 }
6628
6629 retry:
6630 if (NILP (glyphless_method))
6631 {
6632 if (c >= 0)
6633 /* The default is to display the character by a proper font. */
6634 return Qnil;
6635 /* The default for the no-font case is to display an empty box. */
6636 glyphless_method = Qempty_box;
6637 }
6638 if (EQ (glyphless_method, Qzero_width))
6639 {
6640 if (c >= 0)
6641 return glyphless_method;
6642 /* This method can't be used for the no-font case. */
6643 glyphless_method = Qempty_box;
6644 }
6645 if (EQ (glyphless_method, Qthin_space))
6646 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6647 else if (EQ (glyphless_method, Qempty_box))
6648 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6649 else if (EQ (glyphless_method, Qhex_code))
6650 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6651 else if (STRINGP (glyphless_method))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6653 else
6654 {
6655 /* Invalid value. We use the default method. */
6656 glyphless_method = Qnil;
6657 goto retry;
6658 }
6659 it->what = IT_GLYPHLESS;
6660 return glyphless_method;
6661 }
6662
6663 /* Merge escape glyph face and cache the result. */
6664
6665 static struct frame *last_escape_glyph_frame = NULL;
6666 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6667 static int last_escape_glyph_merged_face_id = 0;
6668
6669 static int
6670 merge_escape_glyph_face (struct it *it)
6671 {
6672 int face_id;
6673
6674 if (it->f == last_escape_glyph_frame
6675 && it->face_id == last_escape_glyph_face_id)
6676 face_id = last_escape_glyph_merged_face_id;
6677 else
6678 {
6679 /* Merge the `escape-glyph' face into the current face. */
6680 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6681 last_escape_glyph_frame = it->f;
6682 last_escape_glyph_face_id = it->face_id;
6683 last_escape_glyph_merged_face_id = face_id;
6684 }
6685 return face_id;
6686 }
6687
6688 /* Likewise for glyphless glyph face. */
6689
6690 static struct frame *last_glyphless_glyph_frame = NULL;
6691 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6692 static int last_glyphless_glyph_merged_face_id = 0;
6693
6694 int
6695 merge_glyphless_glyph_face (struct it *it)
6696 {
6697 int face_id;
6698
6699 if (it->f == last_glyphless_glyph_frame
6700 && it->face_id == last_glyphless_glyph_face_id)
6701 face_id = last_glyphless_glyph_merged_face_id;
6702 else
6703 {
6704 /* Merge the `glyphless-char' face into the current face. */
6705 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6706 last_glyphless_glyph_frame = it->f;
6707 last_glyphless_glyph_face_id = it->face_id;
6708 last_glyphless_glyph_merged_face_id = face_id;
6709 }
6710 return face_id;
6711 }
6712
6713 /* Load IT's display element fields with information about the next
6714 display element from the current position of IT. Value is zero if
6715 end of buffer (or C string) is reached. */
6716
6717 static int
6718 get_next_display_element (struct it *it)
6719 {
6720 /* Non-zero means that we found a display element. Zero means that
6721 we hit the end of what we iterate over. Performance note: the
6722 function pointer `method' used here turns out to be faster than
6723 using a sequence of if-statements. */
6724 int success_p;
6725
6726 get_next:
6727 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6728
6729 if (it->what == IT_CHARACTER)
6730 {
6731 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6732 and only if (a) the resolved directionality of that character
6733 is R..." */
6734 /* FIXME: Do we need an exception for characters from display
6735 tables? */
6736 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6737 it->c = bidi_mirror_char (it->c);
6738 /* Map via display table or translate control characters.
6739 IT->c, IT->len etc. have been set to the next character by
6740 the function call above. If we have a display table, and it
6741 contains an entry for IT->c, translate it. Don't do this if
6742 IT->c itself comes from a display table, otherwise we could
6743 end up in an infinite recursion. (An alternative could be to
6744 count the recursion depth of this function and signal an
6745 error when a certain maximum depth is reached.) Is it worth
6746 it? */
6747 if (success_p && it->dpvec == NULL)
6748 {
6749 Lisp_Object dv;
6750 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6751 int nonascii_space_p = 0;
6752 int nonascii_hyphen_p = 0;
6753 int c = it->c; /* This is the character to display. */
6754
6755 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6756 {
6757 eassert (SINGLE_BYTE_CHAR_P (c));
6758 if (unibyte_display_via_language_environment)
6759 {
6760 c = DECODE_CHAR (unibyte, c);
6761 if (c < 0)
6762 c = BYTE8_TO_CHAR (it->c);
6763 }
6764 else
6765 c = BYTE8_TO_CHAR (it->c);
6766 }
6767
6768 if (it->dp
6769 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6770 VECTORP (dv)))
6771 {
6772 struct Lisp_Vector *v = XVECTOR (dv);
6773
6774 /* Return the first character from the display table
6775 entry, if not empty. If empty, don't display the
6776 current character. */
6777 if (v->header.size)
6778 {
6779 it->dpvec_char_len = it->len;
6780 it->dpvec = v->contents;
6781 it->dpend = v->contents + v->header.size;
6782 it->current.dpvec_index = 0;
6783 it->dpvec_face_id = -1;
6784 it->saved_face_id = it->face_id;
6785 it->method = GET_FROM_DISPLAY_VECTOR;
6786 it->ellipsis_p = 0;
6787 }
6788 else
6789 {
6790 set_iterator_to_next (it, 0);
6791 }
6792 goto get_next;
6793 }
6794
6795 if (! NILP (lookup_glyphless_char_display (c, it)))
6796 {
6797 if (it->what == IT_GLYPHLESS)
6798 goto done;
6799 /* Don't display this character. */
6800 set_iterator_to_next (it, 0);
6801 goto get_next;
6802 }
6803
6804 /* If `nobreak-char-display' is non-nil, we display
6805 non-ASCII spaces and hyphens specially. */
6806 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6807 {
6808 if (c == 0xA0)
6809 nonascii_space_p = true;
6810 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6811 nonascii_hyphen_p = true;
6812 }
6813
6814 /* Translate control characters into `\003' or `^C' form.
6815 Control characters coming from a display table entry are
6816 currently not translated because we use IT->dpvec to hold
6817 the translation. This could easily be changed but I
6818 don't believe that it is worth doing.
6819
6820 The characters handled by `nobreak-char-display' must be
6821 translated too.
6822
6823 Non-printable characters and raw-byte characters are also
6824 translated to octal form. */
6825 if (((c < ' ' || c == 127) /* ASCII control chars. */
6826 ? (it->area != TEXT_AREA
6827 /* In mode line, treat \n, \t like other crl chars. */
6828 || (c != '\t'
6829 && it->glyph_row
6830 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6831 || (c != '\n' && c != '\t'))
6832 : (nonascii_space_p
6833 || nonascii_hyphen_p
6834 || CHAR_BYTE8_P (c)
6835 || ! CHAR_PRINTABLE_P (c))))
6836 {
6837 /* C is a control character, non-ASCII space/hyphen,
6838 raw-byte, or a non-printable character which must be
6839 displayed either as '\003' or as `^C' where the '\\'
6840 and '^' can be defined in the display table. Fill
6841 IT->ctl_chars with glyphs for what we have to
6842 display. Then, set IT->dpvec to these glyphs. */
6843 Lisp_Object gc;
6844 int ctl_len;
6845 int face_id;
6846 int lface_id = 0;
6847 int escape_glyph;
6848
6849 /* Handle control characters with ^. */
6850
6851 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6852 {
6853 int g;
6854
6855 g = '^'; /* default glyph for Control */
6856 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6857 if (it->dp
6858 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6859 {
6860 g = GLYPH_CODE_CHAR (gc);
6861 lface_id = GLYPH_CODE_FACE (gc);
6862 }
6863
6864 face_id = (lface_id
6865 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6866 : merge_escape_glyph_face (it));
6867
6868 XSETINT (it->ctl_chars[0], g);
6869 XSETINT (it->ctl_chars[1], c ^ 0100);
6870 ctl_len = 2;
6871 goto display_control;
6872 }
6873
6874 /* Handle non-ascii space in the mode where it only gets
6875 highlighting. */
6876
6877 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6878 {
6879 /* Merge `nobreak-space' into the current face. */
6880 face_id = merge_faces (it->f, Qnobreak_space, 0,
6881 it->face_id);
6882 XSETINT (it->ctl_chars[0], ' ');
6883 ctl_len = 1;
6884 goto display_control;
6885 }
6886
6887 /* Handle sequences that start with the "escape glyph". */
6888
6889 /* the default escape glyph is \. */
6890 escape_glyph = '\\';
6891
6892 if (it->dp
6893 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6894 {
6895 escape_glyph = GLYPH_CODE_CHAR (gc);
6896 lface_id = GLYPH_CODE_FACE (gc);
6897 }
6898
6899 face_id = (lface_id
6900 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6901 : merge_escape_glyph_face (it));
6902
6903 /* Draw non-ASCII hyphen with just highlighting: */
6904
6905 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6906 {
6907 XSETINT (it->ctl_chars[0], '-');
6908 ctl_len = 1;
6909 goto display_control;
6910 }
6911
6912 /* Draw non-ASCII space/hyphen with escape glyph: */
6913
6914 if (nonascii_space_p || nonascii_hyphen_p)
6915 {
6916 XSETINT (it->ctl_chars[0], escape_glyph);
6917 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6918 ctl_len = 2;
6919 goto display_control;
6920 }
6921
6922 {
6923 char str[10];
6924 int len, i;
6925
6926 if (CHAR_BYTE8_P (c))
6927 /* Display \200 instead of \17777600. */
6928 c = CHAR_TO_BYTE8 (c);
6929 len = sprintf (str, "%03o", c);
6930
6931 XSETINT (it->ctl_chars[0], escape_glyph);
6932 for (i = 0; i < len; i++)
6933 XSETINT (it->ctl_chars[i + 1], str[i]);
6934 ctl_len = len + 1;
6935 }
6936
6937 display_control:
6938 /* Set up IT->dpvec and return first character from it. */
6939 it->dpvec_char_len = it->len;
6940 it->dpvec = it->ctl_chars;
6941 it->dpend = it->dpvec + ctl_len;
6942 it->current.dpvec_index = 0;
6943 it->dpvec_face_id = face_id;
6944 it->saved_face_id = it->face_id;
6945 it->method = GET_FROM_DISPLAY_VECTOR;
6946 it->ellipsis_p = 0;
6947 goto get_next;
6948 }
6949 it->char_to_display = c;
6950 }
6951 else if (success_p)
6952 {
6953 it->char_to_display = it->c;
6954 }
6955 }
6956
6957 #ifdef HAVE_WINDOW_SYSTEM
6958 /* Adjust face id for a multibyte character. There are no multibyte
6959 character in unibyte text. */
6960 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6961 && it->multibyte_p
6962 && success_p
6963 && FRAME_WINDOW_P (it->f))
6964 {
6965 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6966
6967 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6968 {
6969 /* Automatic composition with glyph-string. */
6970 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6971
6972 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6973 }
6974 else
6975 {
6976 ptrdiff_t pos = (it->s ? -1
6977 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6978 : IT_CHARPOS (*it));
6979 int c;
6980
6981 if (it->what == IT_CHARACTER)
6982 c = it->char_to_display;
6983 else
6984 {
6985 struct composition *cmp = composition_table[it->cmp_it.id];
6986 int i;
6987
6988 c = ' ';
6989 for (i = 0; i < cmp->glyph_len; i++)
6990 /* TAB in a composition means display glyphs with
6991 padding space on the left or right. */
6992 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6993 break;
6994 }
6995 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6996 }
6997 }
6998 #endif /* HAVE_WINDOW_SYSTEM */
6999
7000 done:
7001 /* Is this character the last one of a run of characters with
7002 box? If yes, set IT->end_of_box_run_p to 1. */
7003 if (it->face_box_p
7004 && it->s == NULL)
7005 {
7006 if (it->method == GET_FROM_STRING && it->sp)
7007 {
7008 int face_id = underlying_face_id (it);
7009 struct face *face = FACE_FROM_ID (it->f, face_id);
7010
7011 if (face)
7012 {
7013 if (face->box == FACE_NO_BOX)
7014 {
7015 /* If the box comes from face properties in a
7016 display string, check faces in that string. */
7017 int string_face_id = face_after_it_pos (it);
7018 it->end_of_box_run_p
7019 = (FACE_FROM_ID (it->f, string_face_id)->box
7020 == FACE_NO_BOX);
7021 }
7022 /* Otherwise, the box comes from the underlying face.
7023 If this is the last string character displayed, check
7024 the next buffer location. */
7025 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7026 && (it->current.overlay_string_index
7027 == it->n_overlay_strings - 1))
7028 {
7029 ptrdiff_t ignore;
7030 int next_face_id;
7031 struct text_pos pos = it->current.pos;
7032 INC_TEXT_POS (pos, it->multibyte_p);
7033
7034 next_face_id = face_at_buffer_position
7035 (it->w, CHARPOS (pos), &ignore,
7036 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7037 -1);
7038 it->end_of_box_run_p
7039 = (FACE_FROM_ID (it->f, next_face_id)->box
7040 == FACE_NO_BOX);
7041 }
7042 }
7043 }
7044 /* next_element_from_display_vector sets this flag according to
7045 faces of the display vector glyphs, see there. */
7046 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7047 {
7048 int face_id = face_after_it_pos (it);
7049 it->end_of_box_run_p
7050 = (face_id != it->face_id
7051 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7052 }
7053 }
7054 /* If we reached the end of the object we've been iterating (e.g., a
7055 display string or an overlay string), and there's something on
7056 IT->stack, proceed with what's on the stack. It doesn't make
7057 sense to return zero if there's unprocessed stuff on the stack,
7058 because otherwise that stuff will never be displayed. */
7059 if (!success_p && it->sp > 0)
7060 {
7061 set_iterator_to_next (it, 0);
7062 success_p = get_next_display_element (it);
7063 }
7064
7065 /* Value is 0 if end of buffer or string reached. */
7066 return success_p;
7067 }
7068
7069
7070 /* Move IT to the next display element.
7071
7072 RESEAT_P non-zero means if called on a newline in buffer text,
7073 skip to the next visible line start.
7074
7075 Functions get_next_display_element and set_iterator_to_next are
7076 separate because I find this arrangement easier to handle than a
7077 get_next_display_element function that also increments IT's
7078 position. The way it is we can first look at an iterator's current
7079 display element, decide whether it fits on a line, and if it does,
7080 increment the iterator position. The other way around we probably
7081 would either need a flag indicating whether the iterator has to be
7082 incremented the next time, or we would have to implement a
7083 decrement position function which would not be easy to write. */
7084
7085 void
7086 set_iterator_to_next (struct it *it, int reseat_p)
7087 {
7088 /* Reset flags indicating start and end of a sequence of characters
7089 with box. Reset them at the start of this function because
7090 moving the iterator to a new position might set them. */
7091 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7092
7093 switch (it->method)
7094 {
7095 case GET_FROM_BUFFER:
7096 /* The current display element of IT is a character from
7097 current_buffer. Advance in the buffer, and maybe skip over
7098 invisible lines that are so because of selective display. */
7099 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7100 reseat_at_next_visible_line_start (it, 0);
7101 else if (it->cmp_it.id >= 0)
7102 {
7103 /* We are currently getting glyphs from a composition. */
7104 int i;
7105
7106 if (! it->bidi_p)
7107 {
7108 IT_CHARPOS (*it) += it->cmp_it.nchars;
7109 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7110 if (it->cmp_it.to < it->cmp_it.nglyphs)
7111 {
7112 it->cmp_it.from = it->cmp_it.to;
7113 }
7114 else
7115 {
7116 it->cmp_it.id = -1;
7117 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7118 IT_BYTEPOS (*it),
7119 it->end_charpos, Qnil);
7120 }
7121 }
7122 else if (! it->cmp_it.reversed_p)
7123 {
7124 /* Composition created while scanning forward. */
7125 /* Update IT's char/byte positions to point to the first
7126 character of the next grapheme cluster, or to the
7127 character visually after the current composition. */
7128 for (i = 0; i < it->cmp_it.nchars; i++)
7129 bidi_move_to_visually_next (&it->bidi_it);
7130 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7131 IT_CHARPOS (*it) = it->bidi_it.charpos;
7132
7133 if (it->cmp_it.to < it->cmp_it.nglyphs)
7134 {
7135 /* Proceed to the next grapheme cluster. */
7136 it->cmp_it.from = it->cmp_it.to;
7137 }
7138 else
7139 {
7140 /* No more grapheme clusters in this composition.
7141 Find the next stop position. */
7142 ptrdiff_t stop = it->end_charpos;
7143 if (it->bidi_it.scan_dir < 0)
7144 /* Now we are scanning backward and don't know
7145 where to stop. */
7146 stop = -1;
7147 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7148 IT_BYTEPOS (*it), stop, Qnil);
7149 }
7150 }
7151 else
7152 {
7153 /* Composition created while scanning backward. */
7154 /* Update IT's char/byte positions to point to the last
7155 character of the previous grapheme cluster, or the
7156 character visually after the current composition. */
7157 for (i = 0; i < it->cmp_it.nchars; i++)
7158 bidi_move_to_visually_next (&it->bidi_it);
7159 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7160 IT_CHARPOS (*it) = it->bidi_it.charpos;
7161 if (it->cmp_it.from > 0)
7162 {
7163 /* Proceed to the previous grapheme cluster. */
7164 it->cmp_it.to = it->cmp_it.from;
7165 }
7166 else
7167 {
7168 /* No more grapheme clusters in this composition.
7169 Find the next stop position. */
7170 ptrdiff_t stop = it->end_charpos;
7171 if (it->bidi_it.scan_dir < 0)
7172 /* Now we are scanning backward and don't know
7173 where to stop. */
7174 stop = -1;
7175 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7176 IT_BYTEPOS (*it), stop, Qnil);
7177 }
7178 }
7179 }
7180 else
7181 {
7182 eassert (it->len != 0);
7183
7184 if (!it->bidi_p)
7185 {
7186 IT_BYTEPOS (*it) += it->len;
7187 IT_CHARPOS (*it) += 1;
7188 }
7189 else
7190 {
7191 int prev_scan_dir = it->bidi_it.scan_dir;
7192 /* If this is a new paragraph, determine its base
7193 direction (a.k.a. its base embedding level). */
7194 if (it->bidi_it.new_paragraph)
7195 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7196 bidi_move_to_visually_next (&it->bidi_it);
7197 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7198 IT_CHARPOS (*it) = it->bidi_it.charpos;
7199 if (prev_scan_dir != it->bidi_it.scan_dir)
7200 {
7201 /* As the scan direction was changed, we must
7202 re-compute the stop position for composition. */
7203 ptrdiff_t stop = it->end_charpos;
7204 if (it->bidi_it.scan_dir < 0)
7205 stop = -1;
7206 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7207 IT_BYTEPOS (*it), stop, Qnil);
7208 }
7209 }
7210 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7211 }
7212 break;
7213
7214 case GET_FROM_C_STRING:
7215 /* Current display element of IT is from a C string. */
7216 if (!it->bidi_p
7217 /* If the string position is beyond string's end, it means
7218 next_element_from_c_string is padding the string with
7219 blanks, in which case we bypass the bidi iterator,
7220 because it cannot deal with such virtual characters. */
7221 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7222 {
7223 IT_BYTEPOS (*it) += it->len;
7224 IT_CHARPOS (*it) += 1;
7225 }
7226 else
7227 {
7228 bidi_move_to_visually_next (&it->bidi_it);
7229 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7230 IT_CHARPOS (*it) = it->bidi_it.charpos;
7231 }
7232 break;
7233
7234 case GET_FROM_DISPLAY_VECTOR:
7235 /* Current display element of IT is from a display table entry.
7236 Advance in the display table definition. Reset it to null if
7237 end reached, and continue with characters from buffers/
7238 strings. */
7239 ++it->current.dpvec_index;
7240
7241 /* Restore face of the iterator to what they were before the
7242 display vector entry (these entries may contain faces). */
7243 it->face_id = it->saved_face_id;
7244
7245 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7246 {
7247 int recheck_faces = it->ellipsis_p;
7248
7249 if (it->s)
7250 it->method = GET_FROM_C_STRING;
7251 else if (STRINGP (it->string))
7252 it->method = GET_FROM_STRING;
7253 else
7254 {
7255 it->method = GET_FROM_BUFFER;
7256 it->object = it->w->contents;
7257 }
7258
7259 it->dpvec = NULL;
7260 it->current.dpvec_index = -1;
7261
7262 /* Skip over characters which were displayed via IT->dpvec. */
7263 if (it->dpvec_char_len < 0)
7264 reseat_at_next_visible_line_start (it, 1);
7265 else if (it->dpvec_char_len > 0)
7266 {
7267 if (it->method == GET_FROM_STRING
7268 && it->current.overlay_string_index >= 0
7269 && it->n_overlay_strings > 0)
7270 it->ignore_overlay_strings_at_pos_p = true;
7271 it->len = it->dpvec_char_len;
7272 set_iterator_to_next (it, reseat_p);
7273 }
7274
7275 /* Maybe recheck faces after display vector. */
7276 if (recheck_faces)
7277 it->stop_charpos = IT_CHARPOS (*it);
7278 }
7279 break;
7280
7281 case GET_FROM_STRING:
7282 /* Current display element is a character from a Lisp string. */
7283 eassert (it->s == NULL && STRINGP (it->string));
7284 /* Don't advance past string end. These conditions are true
7285 when set_iterator_to_next is called at the end of
7286 get_next_display_element, in which case the Lisp string is
7287 already exhausted, and all we want is pop the iterator
7288 stack. */
7289 if (it->current.overlay_string_index >= 0)
7290 {
7291 /* This is an overlay string, so there's no padding with
7292 spaces, and the number of characters in the string is
7293 where the string ends. */
7294 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7295 goto consider_string_end;
7296 }
7297 else
7298 {
7299 /* Not an overlay string. There could be padding, so test
7300 against it->end_charpos. */
7301 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7302 goto consider_string_end;
7303 }
7304 if (it->cmp_it.id >= 0)
7305 {
7306 int i;
7307
7308 if (! it->bidi_p)
7309 {
7310 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7311 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7312 if (it->cmp_it.to < it->cmp_it.nglyphs)
7313 it->cmp_it.from = it->cmp_it.to;
7314 else
7315 {
7316 it->cmp_it.id = -1;
7317 composition_compute_stop_pos (&it->cmp_it,
7318 IT_STRING_CHARPOS (*it),
7319 IT_STRING_BYTEPOS (*it),
7320 it->end_charpos, it->string);
7321 }
7322 }
7323 else if (! it->cmp_it.reversed_p)
7324 {
7325 for (i = 0; i < it->cmp_it.nchars; i++)
7326 bidi_move_to_visually_next (&it->bidi_it);
7327 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7328 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7329
7330 if (it->cmp_it.to < it->cmp_it.nglyphs)
7331 it->cmp_it.from = it->cmp_it.to;
7332 else
7333 {
7334 ptrdiff_t stop = it->end_charpos;
7335 if (it->bidi_it.scan_dir < 0)
7336 stop = -1;
7337 composition_compute_stop_pos (&it->cmp_it,
7338 IT_STRING_CHARPOS (*it),
7339 IT_STRING_BYTEPOS (*it), stop,
7340 it->string);
7341 }
7342 }
7343 else
7344 {
7345 for (i = 0; i < it->cmp_it.nchars; i++)
7346 bidi_move_to_visually_next (&it->bidi_it);
7347 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7348 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7349 if (it->cmp_it.from > 0)
7350 it->cmp_it.to = it->cmp_it.from;
7351 else
7352 {
7353 ptrdiff_t stop = it->end_charpos;
7354 if (it->bidi_it.scan_dir < 0)
7355 stop = -1;
7356 composition_compute_stop_pos (&it->cmp_it,
7357 IT_STRING_CHARPOS (*it),
7358 IT_STRING_BYTEPOS (*it), stop,
7359 it->string);
7360 }
7361 }
7362 }
7363 else
7364 {
7365 if (!it->bidi_p
7366 /* If the string position is beyond string's end, it
7367 means next_element_from_string is padding the string
7368 with blanks, in which case we bypass the bidi
7369 iterator, because it cannot deal with such virtual
7370 characters. */
7371 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7372 {
7373 IT_STRING_BYTEPOS (*it) += it->len;
7374 IT_STRING_CHARPOS (*it) += 1;
7375 }
7376 else
7377 {
7378 int prev_scan_dir = it->bidi_it.scan_dir;
7379
7380 bidi_move_to_visually_next (&it->bidi_it);
7381 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7382 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7383 if (prev_scan_dir != it->bidi_it.scan_dir)
7384 {
7385 ptrdiff_t stop = it->end_charpos;
7386
7387 if (it->bidi_it.scan_dir < 0)
7388 stop = -1;
7389 composition_compute_stop_pos (&it->cmp_it,
7390 IT_STRING_CHARPOS (*it),
7391 IT_STRING_BYTEPOS (*it), stop,
7392 it->string);
7393 }
7394 }
7395 }
7396
7397 consider_string_end:
7398
7399 if (it->current.overlay_string_index >= 0)
7400 {
7401 /* IT->string is an overlay string. Advance to the
7402 next, if there is one. */
7403 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7404 {
7405 it->ellipsis_p = 0;
7406 next_overlay_string (it);
7407 if (it->ellipsis_p)
7408 setup_for_ellipsis (it, 0);
7409 }
7410 }
7411 else
7412 {
7413 /* IT->string is not an overlay string. If we reached
7414 its end, and there is something on IT->stack, proceed
7415 with what is on the stack. This can be either another
7416 string, this time an overlay string, or a buffer. */
7417 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7418 && it->sp > 0)
7419 {
7420 pop_it (it);
7421 if (it->method == GET_FROM_STRING)
7422 goto consider_string_end;
7423 }
7424 }
7425 break;
7426
7427 case GET_FROM_IMAGE:
7428 case GET_FROM_STRETCH:
7429 /* The position etc with which we have to proceed are on
7430 the stack. The position may be at the end of a string,
7431 if the `display' property takes up the whole string. */
7432 eassert (it->sp > 0);
7433 pop_it (it);
7434 if (it->method == GET_FROM_STRING)
7435 goto consider_string_end;
7436 break;
7437
7438 default:
7439 /* There are no other methods defined, so this should be a bug. */
7440 emacs_abort ();
7441 }
7442
7443 eassert (it->method != GET_FROM_STRING
7444 || (STRINGP (it->string)
7445 && IT_STRING_CHARPOS (*it) >= 0));
7446 }
7447
7448 /* Load IT's display element fields with information about the next
7449 display element which comes from a display table entry or from the
7450 result of translating a control character to one of the forms `^C'
7451 or `\003'.
7452
7453 IT->dpvec holds the glyphs to return as characters.
7454 IT->saved_face_id holds the face id before the display vector--it
7455 is restored into IT->face_id in set_iterator_to_next. */
7456
7457 static int
7458 next_element_from_display_vector (struct it *it)
7459 {
7460 Lisp_Object gc;
7461 int prev_face_id = it->face_id;
7462 int next_face_id;
7463
7464 /* Precondition. */
7465 eassert (it->dpvec && it->current.dpvec_index >= 0);
7466
7467 it->face_id = it->saved_face_id;
7468
7469 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7470 That seemed totally bogus - so I changed it... */
7471 gc = it->dpvec[it->current.dpvec_index];
7472
7473 if (GLYPH_CODE_P (gc))
7474 {
7475 struct face *this_face, *prev_face, *next_face;
7476
7477 it->c = GLYPH_CODE_CHAR (gc);
7478 it->len = CHAR_BYTES (it->c);
7479
7480 /* The entry may contain a face id to use. Such a face id is
7481 the id of a Lisp face, not a realized face. A face id of
7482 zero means no face is specified. */
7483 if (it->dpvec_face_id >= 0)
7484 it->face_id = it->dpvec_face_id;
7485 else
7486 {
7487 int lface_id = GLYPH_CODE_FACE (gc);
7488 if (lface_id > 0)
7489 it->face_id = merge_faces (it->f, Qt, lface_id,
7490 it->saved_face_id);
7491 }
7492
7493 /* Glyphs in the display vector could have the box face, so we
7494 need to set the related flags in the iterator, as
7495 appropriate. */
7496 this_face = FACE_FROM_ID (it->f, it->face_id);
7497 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7498
7499 /* Is this character the first character of a box-face run? */
7500 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7501 && (!prev_face
7502 || prev_face->box == FACE_NO_BOX));
7503
7504 /* For the last character of the box-face run, we need to look
7505 either at the next glyph from the display vector, or at the
7506 face we saw before the display vector. */
7507 next_face_id = it->saved_face_id;
7508 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7509 {
7510 if (it->dpvec_face_id >= 0)
7511 next_face_id = it->dpvec_face_id;
7512 else
7513 {
7514 int lface_id =
7515 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7516
7517 if (lface_id > 0)
7518 next_face_id = merge_faces (it->f, Qt, lface_id,
7519 it->saved_face_id);
7520 }
7521 }
7522 next_face = FACE_FROM_ID (it->f, next_face_id);
7523 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7524 && (!next_face
7525 || next_face->box == FACE_NO_BOX));
7526 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7527 }
7528 else
7529 /* Display table entry is invalid. Return a space. */
7530 it->c = ' ', it->len = 1;
7531
7532 /* Don't change position and object of the iterator here. They are
7533 still the values of the character that had this display table
7534 entry or was translated, and that's what we want. */
7535 it->what = IT_CHARACTER;
7536 return 1;
7537 }
7538
7539 /* Get the first element of string/buffer in the visual order, after
7540 being reseated to a new position in a string or a buffer. */
7541 static void
7542 get_visually_first_element (struct it *it)
7543 {
7544 int string_p = STRINGP (it->string) || it->s;
7545 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7546 ptrdiff_t bob = (string_p ? 0 : BEGV);
7547
7548 if (STRINGP (it->string))
7549 {
7550 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7551 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7552 }
7553 else
7554 {
7555 it->bidi_it.charpos = IT_CHARPOS (*it);
7556 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7557 }
7558
7559 if (it->bidi_it.charpos == eob)
7560 {
7561 /* Nothing to do, but reset the FIRST_ELT flag, like
7562 bidi_paragraph_init does, because we are not going to
7563 call it. */
7564 it->bidi_it.first_elt = 0;
7565 }
7566 else if (it->bidi_it.charpos == bob
7567 || (!string_p
7568 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7569 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7570 {
7571 /* If we are at the beginning of a line/string, we can produce
7572 the next element right away. */
7573 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7574 bidi_move_to_visually_next (&it->bidi_it);
7575 }
7576 else
7577 {
7578 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7579
7580 /* We need to prime the bidi iterator starting at the line's or
7581 string's beginning, before we will be able to produce the
7582 next element. */
7583 if (string_p)
7584 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7585 else
7586 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7587 IT_BYTEPOS (*it), -1,
7588 &it->bidi_it.bytepos);
7589 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7590 do
7591 {
7592 /* Now return to buffer/string position where we were asked
7593 to get the next display element, and produce that. */
7594 bidi_move_to_visually_next (&it->bidi_it);
7595 }
7596 while (it->bidi_it.bytepos != orig_bytepos
7597 && it->bidi_it.charpos < eob);
7598 }
7599
7600 /* Adjust IT's position information to where we ended up. */
7601 if (STRINGP (it->string))
7602 {
7603 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7604 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7605 }
7606 else
7607 {
7608 IT_CHARPOS (*it) = it->bidi_it.charpos;
7609 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7610 }
7611
7612 if (STRINGP (it->string) || !it->s)
7613 {
7614 ptrdiff_t stop, charpos, bytepos;
7615
7616 if (STRINGP (it->string))
7617 {
7618 eassert (!it->s);
7619 stop = SCHARS (it->string);
7620 if (stop > it->end_charpos)
7621 stop = it->end_charpos;
7622 charpos = IT_STRING_CHARPOS (*it);
7623 bytepos = IT_STRING_BYTEPOS (*it);
7624 }
7625 else
7626 {
7627 stop = it->end_charpos;
7628 charpos = IT_CHARPOS (*it);
7629 bytepos = IT_BYTEPOS (*it);
7630 }
7631 if (it->bidi_it.scan_dir < 0)
7632 stop = -1;
7633 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7634 it->string);
7635 }
7636 }
7637
7638 /* Load IT with the next display element from Lisp string IT->string.
7639 IT->current.string_pos is the current position within the string.
7640 If IT->current.overlay_string_index >= 0, the Lisp string is an
7641 overlay string. */
7642
7643 static int
7644 next_element_from_string (struct it *it)
7645 {
7646 struct text_pos position;
7647
7648 eassert (STRINGP (it->string));
7649 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7650 eassert (IT_STRING_CHARPOS (*it) >= 0);
7651 position = it->current.string_pos;
7652
7653 /* With bidi reordering, the character to display might not be the
7654 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7655 that we were reseat()ed to a new string, whose paragraph
7656 direction is not known. */
7657 if (it->bidi_p && it->bidi_it.first_elt)
7658 {
7659 get_visually_first_element (it);
7660 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7661 }
7662
7663 /* Time to check for invisible text? */
7664 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7665 {
7666 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7667 {
7668 if (!(!it->bidi_p
7669 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7670 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7671 {
7672 /* With bidi non-linear iteration, we could find
7673 ourselves far beyond the last computed stop_charpos,
7674 with several other stop positions in between that we
7675 missed. Scan them all now, in buffer's logical
7676 order, until we find and handle the last stop_charpos
7677 that precedes our current position. */
7678 handle_stop_backwards (it, it->stop_charpos);
7679 return GET_NEXT_DISPLAY_ELEMENT (it);
7680 }
7681 else
7682 {
7683 if (it->bidi_p)
7684 {
7685 /* Take note of the stop position we just moved
7686 across, for when we will move back across it. */
7687 it->prev_stop = it->stop_charpos;
7688 /* If we are at base paragraph embedding level, take
7689 note of the last stop position seen at this
7690 level. */
7691 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7692 it->base_level_stop = it->stop_charpos;
7693 }
7694 handle_stop (it);
7695
7696 /* Since a handler may have changed IT->method, we must
7697 recurse here. */
7698 return GET_NEXT_DISPLAY_ELEMENT (it);
7699 }
7700 }
7701 else if (it->bidi_p
7702 /* If we are before prev_stop, we may have overstepped
7703 on our way backwards a stop_pos, and if so, we need
7704 to handle that stop_pos. */
7705 && IT_STRING_CHARPOS (*it) < it->prev_stop
7706 /* We can sometimes back up for reasons that have nothing
7707 to do with bidi reordering. E.g., compositions. The
7708 code below is only needed when we are above the base
7709 embedding level, so test for that explicitly. */
7710 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7711 {
7712 /* If we lost track of base_level_stop, we have no better
7713 place for handle_stop_backwards to start from than string
7714 beginning. This happens, e.g., when we were reseated to
7715 the previous screenful of text by vertical-motion. */
7716 if (it->base_level_stop <= 0
7717 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7718 it->base_level_stop = 0;
7719 handle_stop_backwards (it, it->base_level_stop);
7720 return GET_NEXT_DISPLAY_ELEMENT (it);
7721 }
7722 }
7723
7724 if (it->current.overlay_string_index >= 0)
7725 {
7726 /* Get the next character from an overlay string. In overlay
7727 strings, there is no field width or padding with spaces to
7728 do. */
7729 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7730 {
7731 it->what = IT_EOB;
7732 return 0;
7733 }
7734 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7735 IT_STRING_BYTEPOS (*it),
7736 it->bidi_it.scan_dir < 0
7737 ? -1
7738 : SCHARS (it->string))
7739 && next_element_from_composition (it))
7740 {
7741 return 1;
7742 }
7743 else if (STRING_MULTIBYTE (it->string))
7744 {
7745 const unsigned char *s = (SDATA (it->string)
7746 + IT_STRING_BYTEPOS (*it));
7747 it->c = string_char_and_length (s, &it->len);
7748 }
7749 else
7750 {
7751 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7752 it->len = 1;
7753 }
7754 }
7755 else
7756 {
7757 /* Get the next character from a Lisp string that is not an
7758 overlay string. Such strings come from the mode line, for
7759 example. We may have to pad with spaces, or truncate the
7760 string. See also next_element_from_c_string. */
7761 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7762 {
7763 it->what = IT_EOB;
7764 return 0;
7765 }
7766 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7767 {
7768 /* Pad with spaces. */
7769 it->c = ' ', it->len = 1;
7770 CHARPOS (position) = BYTEPOS (position) = -1;
7771 }
7772 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7773 IT_STRING_BYTEPOS (*it),
7774 it->bidi_it.scan_dir < 0
7775 ? -1
7776 : it->string_nchars)
7777 && next_element_from_composition (it))
7778 {
7779 return 1;
7780 }
7781 else if (STRING_MULTIBYTE (it->string))
7782 {
7783 const unsigned char *s = (SDATA (it->string)
7784 + IT_STRING_BYTEPOS (*it));
7785 it->c = string_char_and_length (s, &it->len);
7786 }
7787 else
7788 {
7789 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7790 it->len = 1;
7791 }
7792 }
7793
7794 /* Record what we have and where it came from. */
7795 it->what = IT_CHARACTER;
7796 it->object = it->string;
7797 it->position = position;
7798 return 1;
7799 }
7800
7801
7802 /* Load IT with next display element from C string IT->s.
7803 IT->string_nchars is the maximum number of characters to return
7804 from the string. IT->end_charpos may be greater than
7805 IT->string_nchars when this function is called, in which case we
7806 may have to return padding spaces. Value is zero if end of string
7807 reached, including padding spaces. */
7808
7809 static int
7810 next_element_from_c_string (struct it *it)
7811 {
7812 bool success_p = true;
7813
7814 eassert (it->s);
7815 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7816 it->what = IT_CHARACTER;
7817 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7818 it->object = Qnil;
7819
7820 /* With bidi reordering, the character to display might not be the
7821 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7822 we were reseated to a new string, whose paragraph direction is
7823 not known. */
7824 if (it->bidi_p && it->bidi_it.first_elt)
7825 get_visually_first_element (it);
7826
7827 /* IT's position can be greater than IT->string_nchars in case a
7828 field width or precision has been specified when the iterator was
7829 initialized. */
7830 if (IT_CHARPOS (*it) >= it->end_charpos)
7831 {
7832 /* End of the game. */
7833 it->what = IT_EOB;
7834 success_p = 0;
7835 }
7836 else if (IT_CHARPOS (*it) >= it->string_nchars)
7837 {
7838 /* Pad with spaces. */
7839 it->c = ' ', it->len = 1;
7840 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7841 }
7842 else if (it->multibyte_p)
7843 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7844 else
7845 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7846
7847 return success_p;
7848 }
7849
7850
7851 /* Set up IT to return characters from an ellipsis, if appropriate.
7852 The definition of the ellipsis glyphs may come from a display table
7853 entry. This function fills IT with the first glyph from the
7854 ellipsis if an ellipsis is to be displayed. */
7855
7856 static int
7857 next_element_from_ellipsis (struct it *it)
7858 {
7859 if (it->selective_display_ellipsis_p)
7860 setup_for_ellipsis (it, it->len);
7861 else
7862 {
7863 /* The face at the current position may be different from the
7864 face we find after the invisible text. Remember what it
7865 was in IT->saved_face_id, and signal that it's there by
7866 setting face_before_selective_p. */
7867 it->saved_face_id = it->face_id;
7868 it->method = GET_FROM_BUFFER;
7869 it->object = it->w->contents;
7870 reseat_at_next_visible_line_start (it, 1);
7871 it->face_before_selective_p = true;
7872 }
7873
7874 return GET_NEXT_DISPLAY_ELEMENT (it);
7875 }
7876
7877
7878 /* Deliver an image display element. The iterator IT is already
7879 filled with image information (done in handle_display_prop). Value
7880 is always 1. */
7881
7882
7883 static int
7884 next_element_from_image (struct it *it)
7885 {
7886 it->what = IT_IMAGE;
7887 it->ignore_overlay_strings_at_pos_p = 0;
7888 return 1;
7889 }
7890
7891
7892 /* Fill iterator IT with next display element from a stretch glyph
7893 property. IT->object is the value of the text property. Value is
7894 always 1. */
7895
7896 static int
7897 next_element_from_stretch (struct it *it)
7898 {
7899 it->what = IT_STRETCH;
7900 return 1;
7901 }
7902
7903 /* Scan backwards from IT's current position until we find a stop
7904 position, or until BEGV. This is called when we find ourself
7905 before both the last known prev_stop and base_level_stop while
7906 reordering bidirectional text. */
7907
7908 static void
7909 compute_stop_pos_backwards (struct it *it)
7910 {
7911 const int SCAN_BACK_LIMIT = 1000;
7912 struct text_pos pos;
7913 struct display_pos save_current = it->current;
7914 struct text_pos save_position = it->position;
7915 ptrdiff_t charpos = IT_CHARPOS (*it);
7916 ptrdiff_t where_we_are = charpos;
7917 ptrdiff_t save_stop_pos = it->stop_charpos;
7918 ptrdiff_t save_end_pos = it->end_charpos;
7919
7920 eassert (NILP (it->string) && !it->s);
7921 eassert (it->bidi_p);
7922 it->bidi_p = 0;
7923 do
7924 {
7925 it->end_charpos = min (charpos + 1, ZV);
7926 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7927 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7928 reseat_1 (it, pos, 0);
7929 compute_stop_pos (it);
7930 /* We must advance forward, right? */
7931 if (it->stop_charpos <= charpos)
7932 emacs_abort ();
7933 }
7934 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7935
7936 if (it->stop_charpos <= where_we_are)
7937 it->prev_stop = it->stop_charpos;
7938 else
7939 it->prev_stop = BEGV;
7940 it->bidi_p = true;
7941 it->current = save_current;
7942 it->position = save_position;
7943 it->stop_charpos = save_stop_pos;
7944 it->end_charpos = save_end_pos;
7945 }
7946
7947 /* Scan forward from CHARPOS in the current buffer/string, until we
7948 find a stop position > current IT's position. Then handle the stop
7949 position before that. This is called when we bump into a stop
7950 position while reordering bidirectional text. CHARPOS should be
7951 the last previously processed stop_pos (or BEGV/0, if none were
7952 processed yet) whose position is less that IT's current
7953 position. */
7954
7955 static void
7956 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7957 {
7958 int bufp = !STRINGP (it->string);
7959 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7960 struct display_pos save_current = it->current;
7961 struct text_pos save_position = it->position;
7962 struct text_pos pos1;
7963 ptrdiff_t next_stop;
7964
7965 /* Scan in strict logical order. */
7966 eassert (it->bidi_p);
7967 it->bidi_p = 0;
7968 do
7969 {
7970 it->prev_stop = charpos;
7971 if (bufp)
7972 {
7973 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7974 reseat_1 (it, pos1, 0);
7975 }
7976 else
7977 it->current.string_pos = string_pos (charpos, it->string);
7978 compute_stop_pos (it);
7979 /* We must advance forward, right? */
7980 if (it->stop_charpos <= it->prev_stop)
7981 emacs_abort ();
7982 charpos = it->stop_charpos;
7983 }
7984 while (charpos <= where_we_are);
7985
7986 it->bidi_p = true;
7987 it->current = save_current;
7988 it->position = save_position;
7989 next_stop = it->stop_charpos;
7990 it->stop_charpos = it->prev_stop;
7991 handle_stop (it);
7992 it->stop_charpos = next_stop;
7993 }
7994
7995 /* Load IT with the next display element from current_buffer. Value
7996 is zero if end of buffer reached. IT->stop_charpos is the next
7997 position at which to stop and check for text properties or buffer
7998 end. */
7999
8000 static int
8001 next_element_from_buffer (struct it *it)
8002 {
8003 bool success_p = true;
8004
8005 eassert (IT_CHARPOS (*it) >= BEGV);
8006 eassert (NILP (it->string) && !it->s);
8007 eassert (!it->bidi_p
8008 || (EQ (it->bidi_it.string.lstring, Qnil)
8009 && it->bidi_it.string.s == NULL));
8010
8011 /* With bidi reordering, the character to display might not be the
8012 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8013 we were reseat()ed to a new buffer position, which is potentially
8014 a different paragraph. */
8015 if (it->bidi_p && it->bidi_it.first_elt)
8016 {
8017 get_visually_first_element (it);
8018 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8019 }
8020
8021 if (IT_CHARPOS (*it) >= it->stop_charpos)
8022 {
8023 if (IT_CHARPOS (*it) >= it->end_charpos)
8024 {
8025 int overlay_strings_follow_p;
8026
8027 /* End of the game, except when overlay strings follow that
8028 haven't been returned yet. */
8029 if (it->overlay_strings_at_end_processed_p)
8030 overlay_strings_follow_p = 0;
8031 else
8032 {
8033 it->overlay_strings_at_end_processed_p = true;
8034 overlay_strings_follow_p = get_overlay_strings (it, 0);
8035 }
8036
8037 if (overlay_strings_follow_p)
8038 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8039 else
8040 {
8041 it->what = IT_EOB;
8042 it->position = it->current.pos;
8043 success_p = 0;
8044 }
8045 }
8046 else if (!(!it->bidi_p
8047 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8048 || IT_CHARPOS (*it) == it->stop_charpos))
8049 {
8050 /* With bidi non-linear iteration, we could find ourselves
8051 far beyond the last computed stop_charpos, with several
8052 other stop positions in between that we missed. Scan
8053 them all now, in buffer's logical order, until we find
8054 and handle the last stop_charpos that precedes our
8055 current position. */
8056 handle_stop_backwards (it, it->stop_charpos);
8057 return GET_NEXT_DISPLAY_ELEMENT (it);
8058 }
8059 else
8060 {
8061 if (it->bidi_p)
8062 {
8063 /* Take note of the stop position we just moved across,
8064 for when we will move back across it. */
8065 it->prev_stop = it->stop_charpos;
8066 /* If we are at base paragraph embedding level, take
8067 note of the last stop position seen at this
8068 level. */
8069 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8070 it->base_level_stop = it->stop_charpos;
8071 }
8072 handle_stop (it);
8073 return GET_NEXT_DISPLAY_ELEMENT (it);
8074 }
8075 }
8076 else if (it->bidi_p
8077 /* If we are before prev_stop, we may have overstepped on
8078 our way backwards a stop_pos, and if so, we need to
8079 handle that stop_pos. */
8080 && IT_CHARPOS (*it) < it->prev_stop
8081 /* We can sometimes back up for reasons that have nothing
8082 to do with bidi reordering. E.g., compositions. The
8083 code below is only needed when we are above the base
8084 embedding level, so test for that explicitly. */
8085 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8086 {
8087 if (it->base_level_stop <= 0
8088 || IT_CHARPOS (*it) < it->base_level_stop)
8089 {
8090 /* If we lost track of base_level_stop, we need to find
8091 prev_stop by looking backwards. This happens, e.g., when
8092 we were reseated to the previous screenful of text by
8093 vertical-motion. */
8094 it->base_level_stop = BEGV;
8095 compute_stop_pos_backwards (it);
8096 handle_stop_backwards (it, it->prev_stop);
8097 }
8098 else
8099 handle_stop_backwards (it, it->base_level_stop);
8100 return GET_NEXT_DISPLAY_ELEMENT (it);
8101 }
8102 else
8103 {
8104 /* No face changes, overlays etc. in sight, so just return a
8105 character from current_buffer. */
8106 unsigned char *p;
8107 ptrdiff_t stop;
8108
8109 /* Maybe run the redisplay end trigger hook. Performance note:
8110 This doesn't seem to cost measurable time. */
8111 if (it->redisplay_end_trigger_charpos
8112 && it->glyph_row
8113 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8114 run_redisplay_end_trigger_hook (it);
8115
8116 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8117 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8118 stop)
8119 && next_element_from_composition (it))
8120 {
8121 return 1;
8122 }
8123
8124 /* Get the next character, maybe multibyte. */
8125 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8126 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8127 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8128 else
8129 it->c = *p, it->len = 1;
8130
8131 /* Record what we have and where it came from. */
8132 it->what = IT_CHARACTER;
8133 it->object = it->w->contents;
8134 it->position = it->current.pos;
8135
8136 /* Normally we return the character found above, except when we
8137 really want to return an ellipsis for selective display. */
8138 if (it->selective)
8139 {
8140 if (it->c == '\n')
8141 {
8142 /* A value of selective > 0 means hide lines indented more
8143 than that number of columns. */
8144 if (it->selective > 0
8145 && IT_CHARPOS (*it) + 1 < ZV
8146 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8147 IT_BYTEPOS (*it) + 1,
8148 it->selective))
8149 {
8150 success_p = next_element_from_ellipsis (it);
8151 it->dpvec_char_len = -1;
8152 }
8153 }
8154 else if (it->c == '\r' && it->selective == -1)
8155 {
8156 /* A value of selective == -1 means that everything from the
8157 CR to the end of the line is invisible, with maybe an
8158 ellipsis displayed for it. */
8159 success_p = next_element_from_ellipsis (it);
8160 it->dpvec_char_len = -1;
8161 }
8162 }
8163 }
8164
8165 /* Value is zero if end of buffer reached. */
8166 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8167 return success_p;
8168 }
8169
8170
8171 /* Run the redisplay end trigger hook for IT. */
8172
8173 static void
8174 run_redisplay_end_trigger_hook (struct it *it)
8175 {
8176 Lisp_Object args[3];
8177
8178 /* IT->glyph_row should be non-null, i.e. we should be actually
8179 displaying something, or otherwise we should not run the hook. */
8180 eassert (it->glyph_row);
8181
8182 /* Set up hook arguments. */
8183 args[0] = Qredisplay_end_trigger_functions;
8184 args[1] = it->window;
8185 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8186 it->redisplay_end_trigger_charpos = 0;
8187
8188 /* Since we are *trying* to run these functions, don't try to run
8189 them again, even if they get an error. */
8190 wset_redisplay_end_trigger (it->w, Qnil);
8191 Frun_hook_with_args (3, args);
8192
8193 /* Notice if it changed the face of the character we are on. */
8194 handle_face_prop (it);
8195 }
8196
8197
8198 /* Deliver a composition display element. Unlike the other
8199 next_element_from_XXX, this function is not registered in the array
8200 get_next_element[]. It is called from next_element_from_buffer and
8201 next_element_from_string when necessary. */
8202
8203 static int
8204 next_element_from_composition (struct it *it)
8205 {
8206 it->what = IT_COMPOSITION;
8207 it->len = it->cmp_it.nbytes;
8208 if (STRINGP (it->string))
8209 {
8210 if (it->c < 0)
8211 {
8212 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8213 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8214 return 0;
8215 }
8216 it->position = it->current.string_pos;
8217 it->object = it->string;
8218 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8219 IT_STRING_BYTEPOS (*it), it->string);
8220 }
8221 else
8222 {
8223 if (it->c < 0)
8224 {
8225 IT_CHARPOS (*it) += it->cmp_it.nchars;
8226 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8227 if (it->bidi_p)
8228 {
8229 if (it->bidi_it.new_paragraph)
8230 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8231 /* Resync the bidi iterator with IT's new position.
8232 FIXME: this doesn't support bidirectional text. */
8233 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8234 bidi_move_to_visually_next (&it->bidi_it);
8235 }
8236 return 0;
8237 }
8238 it->position = it->current.pos;
8239 it->object = it->w->contents;
8240 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8241 IT_BYTEPOS (*it), Qnil);
8242 }
8243 return 1;
8244 }
8245
8246
8247 \f
8248 /***********************************************************************
8249 Moving an iterator without producing glyphs
8250 ***********************************************************************/
8251
8252 /* Check if iterator is at a position corresponding to a valid buffer
8253 position after some move_it_ call. */
8254
8255 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8256 ((it)->method == GET_FROM_STRING \
8257 ? IT_STRING_CHARPOS (*it) == 0 \
8258 : 1)
8259
8260
8261 /* Move iterator IT to a specified buffer or X position within one
8262 line on the display without producing glyphs.
8263
8264 OP should be a bit mask including some or all of these bits:
8265 MOVE_TO_X: Stop upon reaching x-position TO_X.
8266 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8267 Regardless of OP's value, stop upon reaching the end of the display line.
8268
8269 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8270 This means, in particular, that TO_X includes window's horizontal
8271 scroll amount.
8272
8273 The return value has several possible values that
8274 say what condition caused the scan to stop:
8275
8276 MOVE_POS_MATCH_OR_ZV
8277 - when TO_POS or ZV was reached.
8278
8279 MOVE_X_REACHED
8280 -when TO_X was reached before TO_POS or ZV were reached.
8281
8282 MOVE_LINE_CONTINUED
8283 - when we reached the end of the display area and the line must
8284 be continued.
8285
8286 MOVE_LINE_TRUNCATED
8287 - when we reached the end of the display area and the line is
8288 truncated.
8289
8290 MOVE_NEWLINE_OR_CR
8291 - when we stopped at a line end, i.e. a newline or a CR and selective
8292 display is on. */
8293
8294 static enum move_it_result
8295 move_it_in_display_line_to (struct it *it,
8296 ptrdiff_t to_charpos, int to_x,
8297 enum move_operation_enum op)
8298 {
8299 enum move_it_result result = MOVE_UNDEFINED;
8300 struct glyph_row *saved_glyph_row;
8301 struct it wrap_it, atpos_it, atx_it, ppos_it;
8302 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8303 void *ppos_data = NULL;
8304 int may_wrap = 0;
8305 enum it_method prev_method = it->method;
8306 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8307 int saw_smaller_pos = prev_pos < to_charpos;
8308
8309 /* Don't produce glyphs in produce_glyphs. */
8310 saved_glyph_row = it->glyph_row;
8311 it->glyph_row = NULL;
8312
8313 /* Use wrap_it to save a copy of IT wherever a word wrap could
8314 occur. Use atpos_it to save a copy of IT at the desired buffer
8315 position, if found, so that we can scan ahead and check if the
8316 word later overshoots the window edge. Use atx_it similarly, for
8317 pixel positions. */
8318 wrap_it.sp = -1;
8319 atpos_it.sp = -1;
8320 atx_it.sp = -1;
8321
8322 /* Use ppos_it under bidi reordering to save a copy of IT for the
8323 position > CHARPOS that is the closest to CHARPOS. We restore
8324 that position in IT when we have scanned the entire display line
8325 without finding a match for CHARPOS and all the character
8326 positions are greater than CHARPOS. */
8327 if (it->bidi_p)
8328 {
8329 SAVE_IT (ppos_it, *it, ppos_data);
8330 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8331 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8332 SAVE_IT (ppos_it, *it, ppos_data);
8333 }
8334
8335 #define BUFFER_POS_REACHED_P() \
8336 ((op & MOVE_TO_POS) != 0 \
8337 && BUFFERP (it->object) \
8338 && (IT_CHARPOS (*it) == to_charpos \
8339 || ((!it->bidi_p \
8340 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8341 && IT_CHARPOS (*it) > to_charpos) \
8342 || (it->what == IT_COMPOSITION \
8343 && ((IT_CHARPOS (*it) > to_charpos \
8344 && to_charpos >= it->cmp_it.charpos) \
8345 || (IT_CHARPOS (*it) < to_charpos \
8346 && to_charpos <= it->cmp_it.charpos)))) \
8347 && (it->method == GET_FROM_BUFFER \
8348 || (it->method == GET_FROM_DISPLAY_VECTOR \
8349 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8350
8351 /* If there's a line-/wrap-prefix, handle it. */
8352 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8353 && it->current_y < it->last_visible_y)
8354 handle_line_prefix (it);
8355
8356 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8357 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8358
8359 while (1)
8360 {
8361 int x, i, ascent = 0, descent = 0;
8362
8363 /* Utility macro to reset an iterator with x, ascent, and descent. */
8364 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8365 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8366 (IT)->max_descent = descent)
8367
8368 /* Stop if we move beyond TO_CHARPOS (after an image or a
8369 display string or stretch glyph). */
8370 if ((op & MOVE_TO_POS) != 0
8371 && BUFFERP (it->object)
8372 && it->method == GET_FROM_BUFFER
8373 && (((!it->bidi_p
8374 /* When the iterator is at base embedding level, we
8375 are guaranteed that characters are delivered for
8376 display in strictly increasing order of their
8377 buffer positions. */
8378 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8379 && IT_CHARPOS (*it) > to_charpos)
8380 || (it->bidi_p
8381 && (prev_method == GET_FROM_IMAGE
8382 || prev_method == GET_FROM_STRETCH
8383 || prev_method == GET_FROM_STRING)
8384 /* Passed TO_CHARPOS from left to right. */
8385 && ((prev_pos < to_charpos
8386 && IT_CHARPOS (*it) > to_charpos)
8387 /* Passed TO_CHARPOS from right to left. */
8388 || (prev_pos > to_charpos
8389 && IT_CHARPOS (*it) < to_charpos)))))
8390 {
8391 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8392 {
8393 result = MOVE_POS_MATCH_OR_ZV;
8394 break;
8395 }
8396 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8397 /* If wrap_it is valid, the current position might be in a
8398 word that is wrapped. So, save the iterator in
8399 atpos_it and continue to see if wrapping happens. */
8400 SAVE_IT (atpos_it, *it, atpos_data);
8401 }
8402
8403 /* Stop when ZV reached.
8404 We used to stop here when TO_CHARPOS reached as well, but that is
8405 too soon if this glyph does not fit on this line. So we handle it
8406 explicitly below. */
8407 if (!get_next_display_element (it))
8408 {
8409 result = MOVE_POS_MATCH_OR_ZV;
8410 break;
8411 }
8412
8413 if (it->line_wrap == TRUNCATE)
8414 {
8415 if (BUFFER_POS_REACHED_P ())
8416 {
8417 result = MOVE_POS_MATCH_OR_ZV;
8418 break;
8419 }
8420 }
8421 else
8422 {
8423 if (it->line_wrap == WORD_WRAP)
8424 {
8425 if (IT_DISPLAYING_WHITESPACE (it))
8426 may_wrap = 1;
8427 else if (may_wrap)
8428 {
8429 /* We have reached a glyph that follows one or more
8430 whitespace characters. If the position is
8431 already found, we are done. */
8432 if (atpos_it.sp >= 0)
8433 {
8434 RESTORE_IT (it, &atpos_it, atpos_data);
8435 result = MOVE_POS_MATCH_OR_ZV;
8436 goto done;
8437 }
8438 if (atx_it.sp >= 0)
8439 {
8440 RESTORE_IT (it, &atx_it, atx_data);
8441 result = MOVE_X_REACHED;
8442 goto done;
8443 }
8444 /* Otherwise, we can wrap here. */
8445 SAVE_IT (wrap_it, *it, wrap_data);
8446 may_wrap = 0;
8447 }
8448 }
8449 }
8450
8451 /* Remember the line height for the current line, in case
8452 the next element doesn't fit on the line. */
8453 ascent = it->max_ascent;
8454 descent = it->max_descent;
8455
8456 /* The call to produce_glyphs will get the metrics of the
8457 display element IT is loaded with. Record the x-position
8458 before this display element, in case it doesn't fit on the
8459 line. */
8460 x = it->current_x;
8461
8462 PRODUCE_GLYPHS (it);
8463
8464 if (it->area != TEXT_AREA)
8465 {
8466 prev_method = it->method;
8467 if (it->method == GET_FROM_BUFFER)
8468 prev_pos = IT_CHARPOS (*it);
8469 set_iterator_to_next (it, 1);
8470 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8471 SET_TEXT_POS (this_line_min_pos,
8472 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8473 if (it->bidi_p
8474 && (op & MOVE_TO_POS)
8475 && IT_CHARPOS (*it) > to_charpos
8476 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8477 SAVE_IT (ppos_it, *it, ppos_data);
8478 continue;
8479 }
8480
8481 /* The number of glyphs we get back in IT->nglyphs will normally
8482 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8483 character on a terminal frame, or (iii) a line end. For the
8484 second case, IT->nglyphs - 1 padding glyphs will be present.
8485 (On X frames, there is only one glyph produced for a
8486 composite character.)
8487
8488 The behavior implemented below means, for continuation lines,
8489 that as many spaces of a TAB as fit on the current line are
8490 displayed there. For terminal frames, as many glyphs of a
8491 multi-glyph character are displayed in the current line, too.
8492 This is what the old redisplay code did, and we keep it that
8493 way. Under X, the whole shape of a complex character must
8494 fit on the line or it will be completely displayed in the
8495 next line.
8496
8497 Note that both for tabs and padding glyphs, all glyphs have
8498 the same width. */
8499 if (it->nglyphs)
8500 {
8501 /* More than one glyph or glyph doesn't fit on line. All
8502 glyphs have the same width. */
8503 int single_glyph_width = it->pixel_width / it->nglyphs;
8504 int new_x;
8505 int x_before_this_char = x;
8506 int hpos_before_this_char = it->hpos;
8507
8508 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8509 {
8510 new_x = x + single_glyph_width;
8511
8512 /* We want to leave anything reaching TO_X to the caller. */
8513 if ((op & MOVE_TO_X) && new_x > to_x)
8514 {
8515 if (BUFFER_POS_REACHED_P ())
8516 {
8517 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8518 goto buffer_pos_reached;
8519 if (atpos_it.sp < 0)
8520 {
8521 SAVE_IT (atpos_it, *it, atpos_data);
8522 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8523 }
8524 }
8525 else
8526 {
8527 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8528 {
8529 it->current_x = x;
8530 result = MOVE_X_REACHED;
8531 break;
8532 }
8533 if (atx_it.sp < 0)
8534 {
8535 SAVE_IT (atx_it, *it, atx_data);
8536 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8537 }
8538 }
8539 }
8540
8541 if (/* Lines are continued. */
8542 it->line_wrap != TRUNCATE
8543 && (/* And glyph doesn't fit on the line. */
8544 new_x > it->last_visible_x
8545 /* Or it fits exactly and we're on a window
8546 system frame. */
8547 || (new_x == it->last_visible_x
8548 && FRAME_WINDOW_P (it->f)
8549 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8550 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8551 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8552 {
8553 if (/* IT->hpos == 0 means the very first glyph
8554 doesn't fit on the line, e.g. a wide image. */
8555 it->hpos == 0
8556 || (new_x == it->last_visible_x
8557 && FRAME_WINDOW_P (it->f)))
8558 {
8559 ++it->hpos;
8560 it->current_x = new_x;
8561
8562 /* The character's last glyph just barely fits
8563 in this row. */
8564 if (i == it->nglyphs - 1)
8565 {
8566 /* If this is the destination position,
8567 return a position *before* it in this row,
8568 now that we know it fits in this row. */
8569 if (BUFFER_POS_REACHED_P ())
8570 {
8571 if (it->line_wrap != WORD_WRAP
8572 || wrap_it.sp < 0)
8573 {
8574 it->hpos = hpos_before_this_char;
8575 it->current_x = x_before_this_char;
8576 result = MOVE_POS_MATCH_OR_ZV;
8577 break;
8578 }
8579 if (it->line_wrap == WORD_WRAP
8580 && atpos_it.sp < 0)
8581 {
8582 SAVE_IT (atpos_it, *it, atpos_data);
8583 atpos_it.current_x = x_before_this_char;
8584 atpos_it.hpos = hpos_before_this_char;
8585 }
8586 }
8587
8588 prev_method = it->method;
8589 if (it->method == GET_FROM_BUFFER)
8590 prev_pos = IT_CHARPOS (*it);
8591 set_iterator_to_next (it, 1);
8592 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8593 SET_TEXT_POS (this_line_min_pos,
8594 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8595 /* On graphical terminals, newlines may
8596 "overflow" into the fringe if
8597 overflow-newline-into-fringe is non-nil.
8598 On text terminals, and on graphical
8599 terminals with no right margin, newlines
8600 may overflow into the last glyph on the
8601 display line.*/
8602 if (!FRAME_WINDOW_P (it->f)
8603 || ((it->bidi_p
8604 && it->bidi_it.paragraph_dir == R2L)
8605 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8606 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8607 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8608 {
8609 if (!get_next_display_element (it))
8610 {
8611 result = MOVE_POS_MATCH_OR_ZV;
8612 break;
8613 }
8614 if (BUFFER_POS_REACHED_P ())
8615 {
8616 if (ITERATOR_AT_END_OF_LINE_P (it))
8617 result = MOVE_POS_MATCH_OR_ZV;
8618 else
8619 result = MOVE_LINE_CONTINUED;
8620 break;
8621 }
8622 if (ITERATOR_AT_END_OF_LINE_P (it)
8623 && (it->line_wrap != WORD_WRAP
8624 || wrap_it.sp < 0))
8625 {
8626 result = MOVE_NEWLINE_OR_CR;
8627 break;
8628 }
8629 }
8630 }
8631 }
8632 else
8633 IT_RESET_X_ASCENT_DESCENT (it);
8634
8635 if (wrap_it.sp >= 0)
8636 {
8637 RESTORE_IT (it, &wrap_it, wrap_data);
8638 atpos_it.sp = -1;
8639 atx_it.sp = -1;
8640 }
8641
8642 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8643 IT_CHARPOS (*it)));
8644 result = MOVE_LINE_CONTINUED;
8645 break;
8646 }
8647
8648 if (BUFFER_POS_REACHED_P ())
8649 {
8650 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8651 goto buffer_pos_reached;
8652 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8653 {
8654 SAVE_IT (atpos_it, *it, atpos_data);
8655 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8656 }
8657 }
8658
8659 if (new_x > it->first_visible_x)
8660 {
8661 /* Glyph is visible. Increment number of glyphs that
8662 would be displayed. */
8663 ++it->hpos;
8664 }
8665 }
8666
8667 if (result != MOVE_UNDEFINED)
8668 break;
8669 }
8670 else if (BUFFER_POS_REACHED_P ())
8671 {
8672 buffer_pos_reached:
8673 IT_RESET_X_ASCENT_DESCENT (it);
8674 result = MOVE_POS_MATCH_OR_ZV;
8675 break;
8676 }
8677 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8678 {
8679 /* Stop when TO_X specified and reached. This check is
8680 necessary here because of lines consisting of a line end,
8681 only. The line end will not produce any glyphs and we
8682 would never get MOVE_X_REACHED. */
8683 eassert (it->nglyphs == 0);
8684 result = MOVE_X_REACHED;
8685 break;
8686 }
8687
8688 /* Is this a line end? If yes, we're done. */
8689 if (ITERATOR_AT_END_OF_LINE_P (it))
8690 {
8691 /* If we are past TO_CHARPOS, but never saw any character
8692 positions smaller than TO_CHARPOS, return
8693 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8694 did. */
8695 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8696 {
8697 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8698 {
8699 if (IT_CHARPOS (ppos_it) < ZV)
8700 {
8701 RESTORE_IT (it, &ppos_it, ppos_data);
8702 result = MOVE_POS_MATCH_OR_ZV;
8703 }
8704 else
8705 goto buffer_pos_reached;
8706 }
8707 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8708 && IT_CHARPOS (*it) > to_charpos)
8709 goto buffer_pos_reached;
8710 else
8711 result = MOVE_NEWLINE_OR_CR;
8712 }
8713 else
8714 result = MOVE_NEWLINE_OR_CR;
8715 break;
8716 }
8717
8718 prev_method = it->method;
8719 if (it->method == GET_FROM_BUFFER)
8720 prev_pos = IT_CHARPOS (*it);
8721 /* The current display element has been consumed. Advance
8722 to the next. */
8723 set_iterator_to_next (it, 1);
8724 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8725 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8726 if (IT_CHARPOS (*it) < to_charpos)
8727 saw_smaller_pos = 1;
8728 if (it->bidi_p
8729 && (op & MOVE_TO_POS)
8730 && IT_CHARPOS (*it) >= to_charpos
8731 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8732 SAVE_IT (ppos_it, *it, ppos_data);
8733
8734 /* Stop if lines are truncated and IT's current x-position is
8735 past the right edge of the window now. */
8736 if (it->line_wrap == TRUNCATE
8737 && it->current_x >= it->last_visible_x)
8738 {
8739 if (!FRAME_WINDOW_P (it->f)
8740 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8741 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8742 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8743 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8744 {
8745 int at_eob_p = 0;
8746
8747 if ((at_eob_p = !get_next_display_element (it))
8748 || BUFFER_POS_REACHED_P ()
8749 /* If we are past TO_CHARPOS, but never saw any
8750 character positions smaller than TO_CHARPOS,
8751 return MOVE_POS_MATCH_OR_ZV, like the
8752 unidirectional display did. */
8753 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8754 && !saw_smaller_pos
8755 && IT_CHARPOS (*it) > to_charpos))
8756 {
8757 if (it->bidi_p
8758 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8759 RESTORE_IT (it, &ppos_it, ppos_data);
8760 result = MOVE_POS_MATCH_OR_ZV;
8761 break;
8762 }
8763 if (ITERATOR_AT_END_OF_LINE_P (it))
8764 {
8765 result = MOVE_NEWLINE_OR_CR;
8766 break;
8767 }
8768 }
8769 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8770 && !saw_smaller_pos
8771 && IT_CHARPOS (*it) > to_charpos)
8772 {
8773 if (IT_CHARPOS (ppos_it) < ZV)
8774 RESTORE_IT (it, &ppos_it, ppos_data);
8775 result = MOVE_POS_MATCH_OR_ZV;
8776 break;
8777 }
8778 result = MOVE_LINE_TRUNCATED;
8779 break;
8780 }
8781 #undef IT_RESET_X_ASCENT_DESCENT
8782 }
8783
8784 #undef BUFFER_POS_REACHED_P
8785
8786 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8787 restore the saved iterator. */
8788 if (atpos_it.sp >= 0)
8789 RESTORE_IT (it, &atpos_it, atpos_data);
8790 else if (atx_it.sp >= 0)
8791 RESTORE_IT (it, &atx_it, atx_data);
8792
8793 done:
8794
8795 if (atpos_data)
8796 bidi_unshelve_cache (atpos_data, 1);
8797 if (atx_data)
8798 bidi_unshelve_cache (atx_data, 1);
8799 if (wrap_data)
8800 bidi_unshelve_cache (wrap_data, 1);
8801 if (ppos_data)
8802 bidi_unshelve_cache (ppos_data, 1);
8803
8804 /* Restore the iterator settings altered at the beginning of this
8805 function. */
8806 it->glyph_row = saved_glyph_row;
8807 return result;
8808 }
8809
8810 /* For external use. */
8811 void
8812 move_it_in_display_line (struct it *it,
8813 ptrdiff_t to_charpos, int to_x,
8814 enum move_operation_enum op)
8815 {
8816 if (it->line_wrap == WORD_WRAP
8817 && (op & MOVE_TO_X))
8818 {
8819 struct it save_it;
8820 void *save_data = NULL;
8821 int skip;
8822
8823 SAVE_IT (save_it, *it, save_data);
8824 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8825 /* When word-wrap is on, TO_X may lie past the end
8826 of a wrapped line. Then it->current is the
8827 character on the next line, so backtrack to the
8828 space before the wrap point. */
8829 if (skip == MOVE_LINE_CONTINUED)
8830 {
8831 int prev_x = max (it->current_x - 1, 0);
8832 RESTORE_IT (it, &save_it, save_data);
8833 move_it_in_display_line_to
8834 (it, -1, prev_x, MOVE_TO_X);
8835 }
8836 else
8837 bidi_unshelve_cache (save_data, 1);
8838 }
8839 else
8840 move_it_in_display_line_to (it, to_charpos, to_x, op);
8841 }
8842
8843
8844 /* Move IT forward until it satisfies one or more of the criteria in
8845 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8846
8847 OP is a bit-mask that specifies where to stop, and in particular,
8848 which of those four position arguments makes a difference. See the
8849 description of enum move_operation_enum.
8850
8851 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8852 screen line, this function will set IT to the next position that is
8853 displayed to the right of TO_CHARPOS on the screen.
8854
8855 Return the maximum pixel length of any line scanned but never more
8856 than it.last_visible_x. */
8857
8858 int
8859 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8860 {
8861 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8862 int line_height, line_start_x = 0, reached = 0;
8863 int max_current_x = 0;
8864 void *backup_data = NULL;
8865
8866 for (;;)
8867 {
8868 if (op & MOVE_TO_VPOS)
8869 {
8870 /* If no TO_CHARPOS and no TO_X specified, stop at the
8871 start of the line TO_VPOS. */
8872 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8873 {
8874 if (it->vpos == to_vpos)
8875 {
8876 reached = 1;
8877 break;
8878 }
8879 else
8880 skip = move_it_in_display_line_to (it, -1, -1, 0);
8881 }
8882 else
8883 {
8884 /* TO_VPOS >= 0 means stop at TO_X in the line at
8885 TO_VPOS, or at TO_POS, whichever comes first. */
8886 if (it->vpos == to_vpos)
8887 {
8888 reached = 2;
8889 break;
8890 }
8891
8892 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8893
8894 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8895 {
8896 reached = 3;
8897 break;
8898 }
8899 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8900 {
8901 /* We have reached TO_X but not in the line we want. */
8902 skip = move_it_in_display_line_to (it, to_charpos,
8903 -1, MOVE_TO_POS);
8904 if (skip == MOVE_POS_MATCH_OR_ZV)
8905 {
8906 reached = 4;
8907 break;
8908 }
8909 }
8910 }
8911 }
8912 else if (op & MOVE_TO_Y)
8913 {
8914 struct it it_backup;
8915
8916 if (it->line_wrap == WORD_WRAP)
8917 SAVE_IT (it_backup, *it, backup_data);
8918
8919 /* TO_Y specified means stop at TO_X in the line containing
8920 TO_Y---or at TO_CHARPOS if this is reached first. The
8921 problem is that we can't really tell whether the line
8922 contains TO_Y before we have completely scanned it, and
8923 this may skip past TO_X. What we do is to first scan to
8924 TO_X.
8925
8926 If TO_X is not specified, use a TO_X of zero. The reason
8927 is to make the outcome of this function more predictable.
8928 If we didn't use TO_X == 0, we would stop at the end of
8929 the line which is probably not what a caller would expect
8930 to happen. */
8931 skip = move_it_in_display_line_to
8932 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8933 (MOVE_TO_X | (op & MOVE_TO_POS)));
8934
8935 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8936 if (skip == MOVE_POS_MATCH_OR_ZV)
8937 reached = 5;
8938 else if (skip == MOVE_X_REACHED)
8939 {
8940 /* If TO_X was reached, we want to know whether TO_Y is
8941 in the line. We know this is the case if the already
8942 scanned glyphs make the line tall enough. Otherwise,
8943 we must check by scanning the rest of the line. */
8944 line_height = it->max_ascent + it->max_descent;
8945 if (to_y >= it->current_y
8946 && to_y < it->current_y + line_height)
8947 {
8948 reached = 6;
8949 break;
8950 }
8951 SAVE_IT (it_backup, *it, backup_data);
8952 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8953 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8954 op & MOVE_TO_POS);
8955 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8956 line_height = it->max_ascent + it->max_descent;
8957 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8958
8959 if (to_y >= it->current_y
8960 && to_y < it->current_y + line_height)
8961 {
8962 /* If TO_Y is in this line and TO_X was reached
8963 above, we scanned too far. We have to restore
8964 IT's settings to the ones before skipping. But
8965 keep the more accurate values of max_ascent and
8966 max_descent we've found while skipping the rest
8967 of the line, for the sake of callers, such as
8968 pos_visible_p, that need to know the line
8969 height. */
8970 int max_ascent = it->max_ascent;
8971 int max_descent = it->max_descent;
8972
8973 RESTORE_IT (it, &it_backup, backup_data);
8974 it->max_ascent = max_ascent;
8975 it->max_descent = max_descent;
8976 reached = 6;
8977 }
8978 else
8979 {
8980 skip = skip2;
8981 if (skip == MOVE_POS_MATCH_OR_ZV)
8982 reached = 7;
8983 }
8984 }
8985 else
8986 {
8987 /* Check whether TO_Y is in this line. */
8988 line_height = it->max_ascent + it->max_descent;
8989 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8990
8991 if (to_y >= it->current_y
8992 && to_y < it->current_y + line_height)
8993 {
8994 if (to_y > it->current_y)
8995 max_current_x = max (it->current_x, max_current_x);
8996
8997 /* When word-wrap is on, TO_X may lie past the end
8998 of a wrapped line. Then it->current is the
8999 character on the next line, so backtrack to the
9000 space before the wrap point. */
9001 if (skip == MOVE_LINE_CONTINUED
9002 && it->line_wrap == WORD_WRAP)
9003 {
9004 int prev_x = max (it->current_x - 1, 0);
9005 RESTORE_IT (it, &it_backup, backup_data);
9006 skip = move_it_in_display_line_to
9007 (it, -1, prev_x, MOVE_TO_X);
9008 }
9009
9010 reached = 6;
9011 }
9012 }
9013
9014 if (reached)
9015 {
9016 max_current_x = max (it->current_x, max_current_x);
9017 break;
9018 }
9019 }
9020 else if (BUFFERP (it->object)
9021 && (it->method == GET_FROM_BUFFER
9022 || it->method == GET_FROM_STRETCH)
9023 && IT_CHARPOS (*it) >= to_charpos
9024 /* Under bidi iteration, a call to set_iterator_to_next
9025 can scan far beyond to_charpos if the initial
9026 portion of the next line needs to be reordered. In
9027 that case, give move_it_in_display_line_to another
9028 chance below. */
9029 && !(it->bidi_p
9030 && it->bidi_it.scan_dir == -1))
9031 skip = MOVE_POS_MATCH_OR_ZV;
9032 else
9033 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9034
9035 switch (skip)
9036 {
9037 case MOVE_POS_MATCH_OR_ZV:
9038 max_current_x = max (it->current_x, max_current_x);
9039 reached = 8;
9040 goto out;
9041
9042 case MOVE_NEWLINE_OR_CR:
9043 max_current_x = max (it->current_x, max_current_x);
9044 set_iterator_to_next (it, 1);
9045 it->continuation_lines_width = 0;
9046 break;
9047
9048 case MOVE_LINE_TRUNCATED:
9049 max_current_x = it->last_visible_x;
9050 it->continuation_lines_width = 0;
9051 reseat_at_next_visible_line_start (it, 0);
9052 if ((op & MOVE_TO_POS) != 0
9053 && IT_CHARPOS (*it) > to_charpos)
9054 {
9055 reached = 9;
9056 goto out;
9057 }
9058 break;
9059
9060 case MOVE_LINE_CONTINUED:
9061 max_current_x = it->last_visible_x;
9062 /* For continued lines ending in a tab, some of the glyphs
9063 associated with the tab are displayed on the current
9064 line. Since it->current_x does not include these glyphs,
9065 we use it->last_visible_x instead. */
9066 if (it->c == '\t')
9067 {
9068 it->continuation_lines_width += it->last_visible_x;
9069 /* When moving by vpos, ensure that the iterator really
9070 advances to the next line (bug#847, bug#969). Fixme:
9071 do we need to do this in other circumstances? */
9072 if (it->current_x != it->last_visible_x
9073 && (op & MOVE_TO_VPOS)
9074 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9075 {
9076 line_start_x = it->current_x + it->pixel_width
9077 - it->last_visible_x;
9078 set_iterator_to_next (it, 0);
9079 }
9080 }
9081 else
9082 it->continuation_lines_width += it->current_x;
9083 break;
9084
9085 default:
9086 emacs_abort ();
9087 }
9088
9089 /* Reset/increment for the next run. */
9090 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9091 it->current_x = line_start_x;
9092 line_start_x = 0;
9093 it->hpos = 0;
9094 it->current_y += it->max_ascent + it->max_descent;
9095 ++it->vpos;
9096 last_height = it->max_ascent + it->max_descent;
9097 last_max_ascent = it->max_ascent;
9098 it->max_ascent = it->max_descent = 0;
9099 }
9100
9101 out:
9102
9103 /* On text terminals, we may stop at the end of a line in the middle
9104 of a multi-character glyph. If the glyph itself is continued,
9105 i.e. it is actually displayed on the next line, don't treat this
9106 stopping point as valid; move to the next line instead (unless
9107 that brings us offscreen). */
9108 if (!FRAME_WINDOW_P (it->f)
9109 && op & MOVE_TO_POS
9110 && IT_CHARPOS (*it) == to_charpos
9111 && it->what == IT_CHARACTER
9112 && it->nglyphs > 1
9113 && it->line_wrap == WINDOW_WRAP
9114 && it->current_x == it->last_visible_x - 1
9115 && it->c != '\n'
9116 && it->c != '\t'
9117 && it->vpos < it->w->window_end_vpos)
9118 {
9119 it->continuation_lines_width += it->current_x;
9120 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9121 it->current_y += it->max_ascent + it->max_descent;
9122 ++it->vpos;
9123 last_height = it->max_ascent + it->max_descent;
9124 last_max_ascent = it->max_ascent;
9125 }
9126
9127 if (backup_data)
9128 bidi_unshelve_cache (backup_data, 1);
9129
9130 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9131
9132 return max_current_x;
9133 }
9134
9135
9136 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9137
9138 If DY > 0, move IT backward at least that many pixels. DY = 0
9139 means move IT backward to the preceding line start or BEGV. This
9140 function may move over more than DY pixels if IT->current_y - DY
9141 ends up in the middle of a line; in this case IT->current_y will be
9142 set to the top of the line moved to. */
9143
9144 void
9145 move_it_vertically_backward (struct it *it, int dy)
9146 {
9147 int nlines, h;
9148 struct it it2, it3;
9149 void *it2data = NULL, *it3data = NULL;
9150 ptrdiff_t start_pos;
9151 int nchars_per_row
9152 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9153 ptrdiff_t pos_limit;
9154
9155 move_further_back:
9156 eassert (dy >= 0);
9157
9158 start_pos = IT_CHARPOS (*it);
9159
9160 /* Estimate how many newlines we must move back. */
9161 nlines = max (1, dy / default_line_pixel_height (it->w));
9162 if (it->line_wrap == TRUNCATE)
9163 pos_limit = BEGV;
9164 else
9165 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9166
9167 /* Set the iterator's position that many lines back. But don't go
9168 back more than NLINES full screen lines -- this wins a day with
9169 buffers which have very long lines. */
9170 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9171 back_to_previous_visible_line_start (it);
9172
9173 /* Reseat the iterator here. When moving backward, we don't want
9174 reseat to skip forward over invisible text, set up the iterator
9175 to deliver from overlay strings at the new position etc. So,
9176 use reseat_1 here. */
9177 reseat_1 (it, it->current.pos, 1);
9178
9179 /* We are now surely at a line start. */
9180 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9181 reordering is in effect. */
9182 it->continuation_lines_width = 0;
9183
9184 /* Move forward and see what y-distance we moved. First move to the
9185 start of the next line so that we get its height. We need this
9186 height to be able to tell whether we reached the specified
9187 y-distance. */
9188 SAVE_IT (it2, *it, it2data);
9189 it2.max_ascent = it2.max_descent = 0;
9190 do
9191 {
9192 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9193 MOVE_TO_POS | MOVE_TO_VPOS);
9194 }
9195 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9196 /* If we are in a display string which starts at START_POS,
9197 and that display string includes a newline, and we are
9198 right after that newline (i.e. at the beginning of a
9199 display line), exit the loop, because otherwise we will
9200 infloop, since move_it_to will see that it is already at
9201 START_POS and will not move. */
9202 || (it2.method == GET_FROM_STRING
9203 && IT_CHARPOS (it2) == start_pos
9204 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9205 eassert (IT_CHARPOS (*it) >= BEGV);
9206 SAVE_IT (it3, it2, it3data);
9207
9208 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9209 eassert (IT_CHARPOS (*it) >= BEGV);
9210 /* H is the actual vertical distance from the position in *IT
9211 and the starting position. */
9212 h = it2.current_y - it->current_y;
9213 /* NLINES is the distance in number of lines. */
9214 nlines = it2.vpos - it->vpos;
9215
9216 /* Correct IT's y and vpos position
9217 so that they are relative to the starting point. */
9218 it->vpos -= nlines;
9219 it->current_y -= h;
9220
9221 if (dy == 0)
9222 {
9223 /* DY == 0 means move to the start of the screen line. The
9224 value of nlines is > 0 if continuation lines were involved,
9225 or if the original IT position was at start of a line. */
9226 RESTORE_IT (it, it, it2data);
9227 if (nlines > 0)
9228 move_it_by_lines (it, nlines);
9229 /* The above code moves us to some position NLINES down,
9230 usually to its first glyph (leftmost in an L2R line), but
9231 that's not necessarily the start of the line, under bidi
9232 reordering. We want to get to the character position
9233 that is immediately after the newline of the previous
9234 line. */
9235 if (it->bidi_p
9236 && !it->continuation_lines_width
9237 && !STRINGP (it->string)
9238 && IT_CHARPOS (*it) > BEGV
9239 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9240 {
9241 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9242
9243 DEC_BOTH (cp, bp);
9244 cp = find_newline_no_quit (cp, bp, -1, NULL);
9245 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9246 }
9247 bidi_unshelve_cache (it3data, 1);
9248 }
9249 else
9250 {
9251 /* The y-position we try to reach, relative to *IT.
9252 Note that H has been subtracted in front of the if-statement. */
9253 int target_y = it->current_y + h - dy;
9254 int y0 = it3.current_y;
9255 int y1;
9256 int line_height;
9257
9258 RESTORE_IT (&it3, &it3, it3data);
9259 y1 = line_bottom_y (&it3);
9260 line_height = y1 - y0;
9261 RESTORE_IT (it, it, it2data);
9262 /* If we did not reach target_y, try to move further backward if
9263 we can. If we moved too far backward, try to move forward. */
9264 if (target_y < it->current_y
9265 /* This is heuristic. In a window that's 3 lines high, with
9266 a line height of 13 pixels each, recentering with point
9267 on the bottom line will try to move -39/2 = 19 pixels
9268 backward. Try to avoid moving into the first line. */
9269 && (it->current_y - target_y
9270 > min (window_box_height (it->w), line_height * 2 / 3))
9271 && IT_CHARPOS (*it) > BEGV)
9272 {
9273 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9274 target_y - it->current_y));
9275 dy = it->current_y - target_y;
9276 goto move_further_back;
9277 }
9278 else if (target_y >= it->current_y + line_height
9279 && IT_CHARPOS (*it) < ZV)
9280 {
9281 /* Should move forward by at least one line, maybe more.
9282
9283 Note: Calling move_it_by_lines can be expensive on
9284 terminal frames, where compute_motion is used (via
9285 vmotion) to do the job, when there are very long lines
9286 and truncate-lines is nil. That's the reason for
9287 treating terminal frames specially here. */
9288
9289 if (!FRAME_WINDOW_P (it->f))
9290 move_it_vertically (it, target_y - (it->current_y + line_height));
9291 else
9292 {
9293 do
9294 {
9295 move_it_by_lines (it, 1);
9296 }
9297 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9298 }
9299 }
9300 }
9301 }
9302
9303
9304 /* Move IT by a specified amount of pixel lines DY. DY negative means
9305 move backwards. DY = 0 means move to start of screen line. At the
9306 end, IT will be on the start of a screen line. */
9307
9308 void
9309 move_it_vertically (struct it *it, int dy)
9310 {
9311 if (dy <= 0)
9312 move_it_vertically_backward (it, -dy);
9313 else
9314 {
9315 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9316 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9317 MOVE_TO_POS | MOVE_TO_Y);
9318 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9319
9320 /* If buffer ends in ZV without a newline, move to the start of
9321 the line to satisfy the post-condition. */
9322 if (IT_CHARPOS (*it) == ZV
9323 && ZV > BEGV
9324 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9325 move_it_by_lines (it, 0);
9326 }
9327 }
9328
9329
9330 /* Move iterator IT past the end of the text line it is in. */
9331
9332 void
9333 move_it_past_eol (struct it *it)
9334 {
9335 enum move_it_result rc;
9336
9337 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9338 if (rc == MOVE_NEWLINE_OR_CR)
9339 set_iterator_to_next (it, 0);
9340 }
9341
9342
9343 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9344 negative means move up. DVPOS == 0 means move to the start of the
9345 screen line.
9346
9347 Optimization idea: If we would know that IT->f doesn't use
9348 a face with proportional font, we could be faster for
9349 truncate-lines nil. */
9350
9351 void
9352 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9353 {
9354
9355 /* The commented-out optimization uses vmotion on terminals. This
9356 gives bad results, because elements like it->what, on which
9357 callers such as pos_visible_p rely, aren't updated. */
9358 /* struct position pos;
9359 if (!FRAME_WINDOW_P (it->f))
9360 {
9361 struct text_pos textpos;
9362
9363 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9364 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9365 reseat (it, textpos, 1);
9366 it->vpos += pos.vpos;
9367 it->current_y += pos.vpos;
9368 }
9369 else */
9370
9371 if (dvpos == 0)
9372 {
9373 /* DVPOS == 0 means move to the start of the screen line. */
9374 move_it_vertically_backward (it, 0);
9375 /* Let next call to line_bottom_y calculate real line height. */
9376 last_height = 0;
9377 }
9378 else if (dvpos > 0)
9379 {
9380 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9381 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9382 {
9383 /* Only move to the next buffer position if we ended up in a
9384 string from display property, not in an overlay string
9385 (before-string or after-string). That is because the
9386 latter don't conceal the underlying buffer position, so
9387 we can ask to move the iterator to the exact position we
9388 are interested in. Note that, even if we are already at
9389 IT_CHARPOS (*it), the call below is not a no-op, as it
9390 will detect that we are at the end of the string, pop the
9391 iterator, and compute it->current_x and it->hpos
9392 correctly. */
9393 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9394 -1, -1, -1, MOVE_TO_POS);
9395 }
9396 }
9397 else
9398 {
9399 struct it it2;
9400 void *it2data = NULL;
9401 ptrdiff_t start_charpos, i;
9402 int nchars_per_row
9403 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9404 ptrdiff_t pos_limit;
9405
9406 /* Start at the beginning of the screen line containing IT's
9407 position. This may actually move vertically backwards,
9408 in case of overlays, so adjust dvpos accordingly. */
9409 dvpos += it->vpos;
9410 move_it_vertically_backward (it, 0);
9411 dvpos -= it->vpos;
9412
9413 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9414 screen lines, and reseat the iterator there. */
9415 start_charpos = IT_CHARPOS (*it);
9416 if (it->line_wrap == TRUNCATE)
9417 pos_limit = BEGV;
9418 else
9419 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9420 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9421 back_to_previous_visible_line_start (it);
9422 reseat (it, it->current.pos, 1);
9423
9424 /* Move further back if we end up in a string or an image. */
9425 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9426 {
9427 /* First try to move to start of display line. */
9428 dvpos += it->vpos;
9429 move_it_vertically_backward (it, 0);
9430 dvpos -= it->vpos;
9431 if (IT_POS_VALID_AFTER_MOVE_P (it))
9432 break;
9433 /* If start of line is still in string or image,
9434 move further back. */
9435 back_to_previous_visible_line_start (it);
9436 reseat (it, it->current.pos, 1);
9437 dvpos--;
9438 }
9439
9440 it->current_x = it->hpos = 0;
9441
9442 /* Above call may have moved too far if continuation lines
9443 are involved. Scan forward and see if it did. */
9444 SAVE_IT (it2, *it, it2data);
9445 it2.vpos = it2.current_y = 0;
9446 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9447 it->vpos -= it2.vpos;
9448 it->current_y -= it2.current_y;
9449 it->current_x = it->hpos = 0;
9450
9451 /* If we moved too far back, move IT some lines forward. */
9452 if (it2.vpos > -dvpos)
9453 {
9454 int delta = it2.vpos + dvpos;
9455
9456 RESTORE_IT (&it2, &it2, it2data);
9457 SAVE_IT (it2, *it, it2data);
9458 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9459 /* Move back again if we got too far ahead. */
9460 if (IT_CHARPOS (*it) >= start_charpos)
9461 RESTORE_IT (it, &it2, it2data);
9462 else
9463 bidi_unshelve_cache (it2data, 1);
9464 }
9465 else
9466 RESTORE_IT (it, it, it2data);
9467 }
9468 }
9469
9470 /* Return 1 if IT points into the middle of a display vector. */
9471
9472 int
9473 in_display_vector_p (struct it *it)
9474 {
9475 return (it->method == GET_FROM_DISPLAY_VECTOR
9476 && it->current.dpvec_index > 0
9477 && it->dpvec + it->current.dpvec_index != it->dpend);
9478 }
9479
9480 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9481 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9482 WINDOW must be a live window and defaults to the selected one. The
9483 return value is a cons of the maximum pixel-width of any text line and
9484 the maximum pixel-height of all text lines.
9485
9486 The optional argument FROM, if non-nil, specifies the first text
9487 position and defaults to the minimum accessible position of the buffer.
9488 If FROM is t, use the minimum accessible position that is not a newline
9489 character. TO, if non-nil, specifies the last text position and
9490 defaults to the maximum accessible position of the buffer. If TO is t,
9491 use the maximum accessible position that is not a newline character.
9492
9493 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9494 width that can be returned. X_LIMIT nil or omitted, means to use the
9495 pixel-width of WINDOW's body; use this if you do not intend to change
9496 the width of WINDOW. Use the maximum width WINDOW may assume if you
9497 intend to change WINDOW's width.
9498
9499 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9500 height that can be returned. Text lines whose y-coordinate is beyond
9501 Y_LIMIT are ignored. Since calculating the text height of a large
9502 buffer can take some time, it makes sense to specify this argument if
9503 the size of the buffer is unknown.
9504
9505 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9506 include the height of the mode- or header-line of WINDOW in the return
9507 value. If it is either the symbol `mode-line' or `header-line', include
9508 only the height of that line, if present, in the return value. If t,
9509 include the height of any of these lines in the return value. */)
9510 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9511 Lisp_Object mode_and_header_line)
9512 {
9513 struct window *w = decode_live_window (window);
9514 Lisp_Object buf;
9515 struct buffer *b;
9516 struct it it;
9517 struct buffer *old_buffer = NULL;
9518 ptrdiff_t start, end, pos;
9519 struct text_pos startp;
9520 void *itdata = NULL;
9521 int c, max_y = -1, x = 0, y = 0;
9522
9523 buf = w->contents;
9524 CHECK_BUFFER (buf);
9525 b = XBUFFER (buf);
9526
9527 if (b != current_buffer)
9528 {
9529 old_buffer = current_buffer;
9530 set_buffer_internal (b);
9531 }
9532
9533 if (NILP (from))
9534 start = BEGV;
9535 else if (EQ (from, Qt))
9536 {
9537 start = pos = BEGV;
9538 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9539 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9540 start = pos;
9541 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9542 start = pos;
9543 }
9544 else
9545 {
9546 CHECK_NUMBER_COERCE_MARKER (from);
9547 start = min (max (XINT (from), BEGV), ZV);
9548 }
9549
9550 if (NILP (to))
9551 end = ZV;
9552 else if (EQ (to, Qt))
9553 {
9554 end = pos = ZV;
9555 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9556 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9557 end = pos;
9558 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9559 end = pos;
9560 }
9561 else
9562 {
9563 CHECK_NUMBER_COERCE_MARKER (to);
9564 end = max (start, min (XINT (to), ZV));
9565 }
9566
9567 if (!NILP (y_limit))
9568 {
9569 CHECK_NUMBER (y_limit);
9570 max_y = min (XINT (y_limit), INT_MAX);
9571 }
9572
9573 itdata = bidi_shelve_cache ();
9574 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9575 start_display (&it, w, startp);
9576
9577 /** move_it_vertically_backward (&it, 0); **/
9578 if (NILP (x_limit))
9579 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9580 else
9581 {
9582 CHECK_NUMBER (x_limit);
9583 it.last_visible_x = min (XINT (x_limit), INFINITY);
9584 /* Actually, we never want move_it_to stop at to_x. But to make
9585 sure that move_it_in_display_line_to always moves far enough,
9586 we set it to INT_MAX and specify MOVE_TO_X. */
9587 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9588 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9589 }
9590
9591 if (start == end)
9592 y = it.current_y;
9593 else
9594 {
9595 /* Count last line. */
9596 last_height = 0;
9597 y = line_bottom_y (&it); /* - y; */
9598 }
9599
9600 if (!EQ (mode_and_header_line, Qheader_line)
9601 && !EQ (mode_and_header_line, Qt))
9602 /* Do not count the header-line which was counted automatically by
9603 start_display. */
9604 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9605
9606 if (EQ (mode_and_header_line, Qmode_line)
9607 || EQ (mode_and_header_line, Qt))
9608 /* Do count the mode-line which is not included automatically by
9609 start_display. */
9610 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9611
9612 bidi_unshelve_cache (itdata, 0);
9613
9614 if (old_buffer)
9615 set_buffer_internal (old_buffer);
9616
9617 return Fcons (make_number (x), make_number (y));
9618 }
9619 \f
9620 /***********************************************************************
9621 Messages
9622 ***********************************************************************/
9623
9624
9625 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9626 to *Messages*. */
9627
9628 void
9629 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9630 {
9631 Lisp_Object args[3];
9632 Lisp_Object msg, fmt;
9633 char *buffer;
9634 ptrdiff_t len;
9635 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9636 USE_SAFE_ALLOCA;
9637
9638 fmt = msg = Qnil;
9639 GCPRO4 (fmt, msg, arg1, arg2);
9640
9641 args[0] = fmt = build_string (format);
9642 args[1] = arg1;
9643 args[2] = arg2;
9644 msg = Fformat (3, args);
9645
9646 len = SBYTES (msg) + 1;
9647 buffer = SAFE_ALLOCA (len);
9648 memcpy (buffer, SDATA (msg), len);
9649
9650 message_dolog (buffer, len - 1, 1, 0);
9651 SAFE_FREE ();
9652
9653 UNGCPRO;
9654 }
9655
9656
9657 /* Output a newline in the *Messages* buffer if "needs" one. */
9658
9659 void
9660 message_log_maybe_newline (void)
9661 {
9662 if (message_log_need_newline)
9663 message_dolog ("", 0, 1, 0);
9664 }
9665
9666
9667 /* Add a string M of length NBYTES to the message log, optionally
9668 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9669 true, means interpret the contents of M as multibyte. This
9670 function calls low-level routines in order to bypass text property
9671 hooks, etc. which might not be safe to run.
9672
9673 This may GC (insert may run before/after change hooks),
9674 so the buffer M must NOT point to a Lisp string. */
9675
9676 void
9677 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9678 {
9679 const unsigned char *msg = (const unsigned char *) m;
9680
9681 if (!NILP (Vmemory_full))
9682 return;
9683
9684 if (!NILP (Vmessage_log_max))
9685 {
9686 struct buffer *oldbuf;
9687 Lisp_Object oldpoint, oldbegv, oldzv;
9688 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9689 ptrdiff_t point_at_end = 0;
9690 ptrdiff_t zv_at_end = 0;
9691 Lisp_Object old_deactivate_mark;
9692 struct gcpro gcpro1;
9693
9694 old_deactivate_mark = Vdeactivate_mark;
9695 oldbuf = current_buffer;
9696
9697 /* Ensure the Messages buffer exists, and switch to it.
9698 If we created it, set the major-mode. */
9699 {
9700 int newbuffer = 0;
9701 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9702
9703 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9704
9705 if (newbuffer
9706 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9707 call0 (intern ("messages-buffer-mode"));
9708 }
9709
9710 bset_undo_list (current_buffer, Qt);
9711 bset_cache_long_scans (current_buffer, Qnil);
9712
9713 oldpoint = message_dolog_marker1;
9714 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9715 oldbegv = message_dolog_marker2;
9716 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9717 oldzv = message_dolog_marker3;
9718 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9719 GCPRO1 (old_deactivate_mark);
9720
9721 if (PT == Z)
9722 point_at_end = 1;
9723 if (ZV == Z)
9724 zv_at_end = 1;
9725
9726 BEGV = BEG;
9727 BEGV_BYTE = BEG_BYTE;
9728 ZV = Z;
9729 ZV_BYTE = Z_BYTE;
9730 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9731
9732 /* Insert the string--maybe converting multibyte to single byte
9733 or vice versa, so that all the text fits the buffer. */
9734 if (multibyte
9735 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9736 {
9737 ptrdiff_t i;
9738 int c, char_bytes;
9739 char work[1];
9740
9741 /* Convert a multibyte string to single-byte
9742 for the *Message* buffer. */
9743 for (i = 0; i < nbytes; i += char_bytes)
9744 {
9745 c = string_char_and_length (msg + i, &char_bytes);
9746 work[0] = (ASCII_CHAR_P (c)
9747 ? c
9748 : multibyte_char_to_unibyte (c));
9749 insert_1_both (work, 1, 1, 1, 0, 0);
9750 }
9751 }
9752 else if (! multibyte
9753 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9754 {
9755 ptrdiff_t i;
9756 int c, char_bytes;
9757 unsigned char str[MAX_MULTIBYTE_LENGTH];
9758 /* Convert a single-byte string to multibyte
9759 for the *Message* buffer. */
9760 for (i = 0; i < nbytes; i++)
9761 {
9762 c = msg[i];
9763 MAKE_CHAR_MULTIBYTE (c);
9764 char_bytes = CHAR_STRING (c, str);
9765 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9766 }
9767 }
9768 else if (nbytes)
9769 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9770
9771 if (nlflag)
9772 {
9773 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9774 printmax_t dups;
9775
9776 insert_1_both ("\n", 1, 1, 1, 0, 0);
9777
9778 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9779 this_bol = PT;
9780 this_bol_byte = PT_BYTE;
9781
9782 /* See if this line duplicates the previous one.
9783 If so, combine duplicates. */
9784 if (this_bol > BEG)
9785 {
9786 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9787 prev_bol = PT;
9788 prev_bol_byte = PT_BYTE;
9789
9790 dups = message_log_check_duplicate (prev_bol_byte,
9791 this_bol_byte);
9792 if (dups)
9793 {
9794 del_range_both (prev_bol, prev_bol_byte,
9795 this_bol, this_bol_byte, 0);
9796 if (dups > 1)
9797 {
9798 char dupstr[sizeof " [ times]"
9799 + INT_STRLEN_BOUND (printmax_t)];
9800
9801 /* If you change this format, don't forget to also
9802 change message_log_check_duplicate. */
9803 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9805 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9806 }
9807 }
9808 }
9809
9810 /* If we have more than the desired maximum number of lines
9811 in the *Messages* buffer now, delete the oldest ones.
9812 This is safe because we don't have undo in this buffer. */
9813
9814 if (NATNUMP (Vmessage_log_max))
9815 {
9816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9817 -XFASTINT (Vmessage_log_max) - 1, 0);
9818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9819 }
9820 }
9821 BEGV = marker_position (oldbegv);
9822 BEGV_BYTE = marker_byte_position (oldbegv);
9823
9824 if (zv_at_end)
9825 {
9826 ZV = Z;
9827 ZV_BYTE = Z_BYTE;
9828 }
9829 else
9830 {
9831 ZV = marker_position (oldzv);
9832 ZV_BYTE = marker_byte_position (oldzv);
9833 }
9834
9835 if (point_at_end)
9836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9837 else
9838 /* We can't do Fgoto_char (oldpoint) because it will run some
9839 Lisp code. */
9840 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9841 marker_byte_position (oldpoint));
9842
9843 UNGCPRO;
9844 unchain_marker (XMARKER (oldpoint));
9845 unchain_marker (XMARKER (oldbegv));
9846 unchain_marker (XMARKER (oldzv));
9847
9848 /* We called insert_1_both above with its 5th argument (PREPARE)
9849 zero, which prevents insert_1_both from calling
9850 prepare_to_modify_buffer, which in turns prevents us from
9851 incrementing windows_or_buffers_changed even if *Messages* is
9852 shown in some window. So we must manually set
9853 windows_or_buffers_changed here to make up for that. */
9854 windows_or_buffers_changed = old_windows_or_buffers_changed;
9855 bset_redisplay (current_buffer);
9856
9857 set_buffer_internal (oldbuf);
9858
9859 message_log_need_newline = !nlflag;
9860 Vdeactivate_mark = old_deactivate_mark;
9861 }
9862 }
9863
9864
9865 /* We are at the end of the buffer after just having inserted a newline.
9866 (Note: We depend on the fact we won't be crossing the gap.)
9867 Check to see if the most recent message looks a lot like the previous one.
9868 Return 0 if different, 1 if the new one should just replace it, or a
9869 value N > 1 if we should also append " [N times]". */
9870
9871 static intmax_t
9872 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9873 {
9874 ptrdiff_t i;
9875 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9876 int seen_dots = 0;
9877 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9878 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9879
9880 for (i = 0; i < len; i++)
9881 {
9882 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9883 seen_dots = 1;
9884 if (p1[i] != p2[i])
9885 return seen_dots;
9886 }
9887 p1 += len;
9888 if (*p1 == '\n')
9889 return 2;
9890 if (*p1++ == ' ' && *p1++ == '[')
9891 {
9892 char *pend;
9893 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9894 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9895 return n + 1;
9896 }
9897 return 0;
9898 }
9899 \f
9900
9901 /* Display an echo area message M with a specified length of NBYTES
9902 bytes. The string may include null characters. If M is not a
9903 string, clear out any existing message, and let the mini-buffer
9904 text show through.
9905
9906 This function cancels echoing. */
9907
9908 void
9909 message3 (Lisp_Object m)
9910 {
9911 struct gcpro gcpro1;
9912
9913 GCPRO1 (m);
9914 clear_message (true, true);
9915 cancel_echoing ();
9916
9917 /* First flush out any partial line written with print. */
9918 message_log_maybe_newline ();
9919 if (STRINGP (m))
9920 {
9921 ptrdiff_t nbytes = SBYTES (m);
9922 bool multibyte = STRING_MULTIBYTE (m);
9923 USE_SAFE_ALLOCA;
9924 char *buffer = SAFE_ALLOCA (nbytes);
9925 memcpy (buffer, SDATA (m), nbytes);
9926 message_dolog (buffer, nbytes, 1, multibyte);
9927 SAFE_FREE ();
9928 }
9929 message3_nolog (m);
9930
9931 UNGCPRO;
9932 }
9933
9934
9935 /* The non-logging version of message3.
9936 This does not cancel echoing, because it is used for echoing.
9937 Perhaps we need to make a separate function for echoing
9938 and make this cancel echoing. */
9939
9940 void
9941 message3_nolog (Lisp_Object m)
9942 {
9943 struct frame *sf = SELECTED_FRAME ();
9944
9945 if (FRAME_INITIAL_P (sf))
9946 {
9947 if (noninteractive_need_newline)
9948 putc ('\n', stderr);
9949 noninteractive_need_newline = 0;
9950 if (STRINGP (m))
9951 {
9952 Lisp_Object s = ENCODE_SYSTEM (m);
9953
9954 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9955 }
9956 if (cursor_in_echo_area == 0)
9957 fprintf (stderr, "\n");
9958 fflush (stderr);
9959 }
9960 /* Error messages get reported properly by cmd_error, so this must be just an
9961 informative message; if the frame hasn't really been initialized yet, just
9962 toss it. */
9963 else if (INTERACTIVE && sf->glyphs_initialized_p)
9964 {
9965 /* Get the frame containing the mini-buffer
9966 that the selected frame is using. */
9967 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9968 Lisp_Object frame = XWINDOW (mini_window)->frame;
9969 struct frame *f = XFRAME (frame);
9970
9971 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9972 Fmake_frame_visible (frame);
9973
9974 if (STRINGP (m) && SCHARS (m) > 0)
9975 {
9976 set_message (m);
9977 if (minibuffer_auto_raise)
9978 Fraise_frame (frame);
9979 /* Assume we are not echoing.
9980 (If we are, echo_now will override this.) */
9981 echo_message_buffer = Qnil;
9982 }
9983 else
9984 clear_message (true, true);
9985
9986 do_pending_window_change (0);
9987 echo_area_display (1);
9988 do_pending_window_change (0);
9989 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9990 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9991 }
9992 }
9993
9994
9995 /* Display a null-terminated echo area message M. If M is 0, clear
9996 out any existing message, and let the mini-buffer text show through.
9997
9998 The buffer M must continue to exist until after the echo area gets
9999 cleared or some other message gets displayed there. Do not pass
10000 text that is stored in a Lisp string. Do not pass text in a buffer
10001 that was alloca'd. */
10002
10003 void
10004 message1 (const char *m)
10005 {
10006 message3 (m ? build_unibyte_string (m) : Qnil);
10007 }
10008
10009
10010 /* The non-logging counterpart of message1. */
10011
10012 void
10013 message1_nolog (const char *m)
10014 {
10015 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10016 }
10017
10018 /* Display a message M which contains a single %s
10019 which gets replaced with STRING. */
10020
10021 void
10022 message_with_string (const char *m, Lisp_Object string, int log)
10023 {
10024 CHECK_STRING (string);
10025
10026 if (noninteractive)
10027 {
10028 if (m)
10029 {
10030 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10031 String whose data pointer might be passed to us in M. So
10032 we use a local copy. */
10033 char *fmt = xstrdup (m);
10034
10035 if (noninteractive_need_newline)
10036 putc ('\n', stderr);
10037 noninteractive_need_newline = 0;
10038 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10039 if (!cursor_in_echo_area)
10040 fprintf (stderr, "\n");
10041 fflush (stderr);
10042 xfree (fmt);
10043 }
10044 }
10045 else if (INTERACTIVE)
10046 {
10047 /* The frame whose minibuffer we're going to display the message on.
10048 It may be larger than the selected frame, so we need
10049 to use its buffer, not the selected frame's buffer. */
10050 Lisp_Object mini_window;
10051 struct frame *f, *sf = SELECTED_FRAME ();
10052
10053 /* Get the frame containing the minibuffer
10054 that the selected frame is using. */
10055 mini_window = FRAME_MINIBUF_WINDOW (sf);
10056 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10057
10058 /* Error messages get reported properly by cmd_error, so this must be
10059 just an informative message; if the frame hasn't really been
10060 initialized yet, just toss it. */
10061 if (f->glyphs_initialized_p)
10062 {
10063 Lisp_Object args[2], msg;
10064 struct gcpro gcpro1, gcpro2;
10065
10066 args[0] = build_string (m);
10067 args[1] = msg = string;
10068 GCPRO2 (args[0], msg);
10069 gcpro1.nvars = 2;
10070
10071 msg = Fformat (2, args);
10072
10073 if (log)
10074 message3 (msg);
10075 else
10076 message3_nolog (msg);
10077
10078 UNGCPRO;
10079
10080 /* Print should start at the beginning of the message
10081 buffer next time. */
10082 message_buf_print = 0;
10083 }
10084 }
10085 }
10086
10087
10088 /* Dump an informative message to the minibuf. If M is 0, clear out
10089 any existing message, and let the mini-buffer text show through. */
10090
10091 static void
10092 vmessage (const char *m, va_list ap)
10093 {
10094 if (noninteractive)
10095 {
10096 if (m)
10097 {
10098 if (noninteractive_need_newline)
10099 putc ('\n', stderr);
10100 noninteractive_need_newline = 0;
10101 vfprintf (stderr, m, ap);
10102 if (cursor_in_echo_area == 0)
10103 fprintf (stderr, "\n");
10104 fflush (stderr);
10105 }
10106 }
10107 else if (INTERACTIVE)
10108 {
10109 /* The frame whose mini-buffer we're going to display the message
10110 on. It may be larger than the selected frame, so we need to
10111 use its buffer, not the selected frame's buffer. */
10112 Lisp_Object mini_window;
10113 struct frame *f, *sf = SELECTED_FRAME ();
10114
10115 /* Get the frame containing the mini-buffer
10116 that the selected frame is using. */
10117 mini_window = FRAME_MINIBUF_WINDOW (sf);
10118 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10119
10120 /* Error messages get reported properly by cmd_error, so this must be
10121 just an informative message; if the frame hasn't really been
10122 initialized yet, just toss it. */
10123 if (f->glyphs_initialized_p)
10124 {
10125 if (m)
10126 {
10127 ptrdiff_t len;
10128 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10129 char *message_buf = alloca (maxsize + 1);
10130
10131 len = doprnt (message_buf, maxsize, m, 0, ap);
10132
10133 message3 (make_string (message_buf, len));
10134 }
10135 else
10136 message1 (0);
10137
10138 /* Print should start at the beginning of the message
10139 buffer next time. */
10140 message_buf_print = 0;
10141 }
10142 }
10143 }
10144
10145 void
10146 message (const char *m, ...)
10147 {
10148 va_list ap;
10149 va_start (ap, m);
10150 vmessage (m, ap);
10151 va_end (ap);
10152 }
10153
10154
10155 #if 0
10156 /* The non-logging version of message. */
10157
10158 void
10159 message_nolog (const char *m, ...)
10160 {
10161 Lisp_Object old_log_max;
10162 va_list ap;
10163 va_start (ap, m);
10164 old_log_max = Vmessage_log_max;
10165 Vmessage_log_max = Qnil;
10166 vmessage (m, ap);
10167 Vmessage_log_max = old_log_max;
10168 va_end (ap);
10169 }
10170 #endif
10171
10172
10173 /* Display the current message in the current mini-buffer. This is
10174 only called from error handlers in process.c, and is not time
10175 critical. */
10176
10177 void
10178 update_echo_area (void)
10179 {
10180 if (!NILP (echo_area_buffer[0]))
10181 {
10182 Lisp_Object string;
10183 string = Fcurrent_message ();
10184 message3 (string);
10185 }
10186 }
10187
10188
10189 /* Make sure echo area buffers in `echo_buffers' are live.
10190 If they aren't, make new ones. */
10191
10192 static void
10193 ensure_echo_area_buffers (void)
10194 {
10195 int i;
10196
10197 for (i = 0; i < 2; ++i)
10198 if (!BUFFERP (echo_buffer[i])
10199 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10200 {
10201 char name[30];
10202 Lisp_Object old_buffer;
10203 int j;
10204
10205 old_buffer = echo_buffer[i];
10206 echo_buffer[i] = Fget_buffer_create
10207 (make_formatted_string (name, " *Echo Area %d*", i));
10208 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10209 /* to force word wrap in echo area -
10210 it was decided to postpone this*/
10211 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10212
10213 for (j = 0; j < 2; ++j)
10214 if (EQ (old_buffer, echo_area_buffer[j]))
10215 echo_area_buffer[j] = echo_buffer[i];
10216 }
10217 }
10218
10219
10220 /* Call FN with args A1..A2 with either the current or last displayed
10221 echo_area_buffer as current buffer.
10222
10223 WHICH zero means use the current message buffer
10224 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10225 from echo_buffer[] and clear it.
10226
10227 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10228 suitable buffer from echo_buffer[] and clear it.
10229
10230 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10231 that the current message becomes the last displayed one, make
10232 choose a suitable buffer for echo_area_buffer[0], and clear it.
10233
10234 Value is what FN returns. */
10235
10236 static int
10237 with_echo_area_buffer (struct window *w, int which,
10238 int (*fn) (ptrdiff_t, Lisp_Object),
10239 ptrdiff_t a1, Lisp_Object a2)
10240 {
10241 Lisp_Object buffer;
10242 int this_one, the_other, clear_buffer_p, rc;
10243 ptrdiff_t count = SPECPDL_INDEX ();
10244
10245 /* If buffers aren't live, make new ones. */
10246 ensure_echo_area_buffers ();
10247
10248 clear_buffer_p = 0;
10249
10250 if (which == 0)
10251 this_one = 0, the_other = 1;
10252 else if (which > 0)
10253 this_one = 1, the_other = 0;
10254 else
10255 {
10256 this_one = 0, the_other = 1;
10257 clear_buffer_p = true;
10258
10259 /* We need a fresh one in case the current echo buffer equals
10260 the one containing the last displayed echo area message. */
10261 if (!NILP (echo_area_buffer[this_one])
10262 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10263 echo_area_buffer[this_one] = Qnil;
10264 }
10265
10266 /* Choose a suitable buffer from echo_buffer[] is we don't
10267 have one. */
10268 if (NILP (echo_area_buffer[this_one]))
10269 {
10270 echo_area_buffer[this_one]
10271 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10272 ? echo_buffer[the_other]
10273 : echo_buffer[this_one]);
10274 clear_buffer_p = true;
10275 }
10276
10277 buffer = echo_area_buffer[this_one];
10278
10279 /* Don't get confused by reusing the buffer used for echoing
10280 for a different purpose. */
10281 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10282 cancel_echoing ();
10283
10284 record_unwind_protect (unwind_with_echo_area_buffer,
10285 with_echo_area_buffer_unwind_data (w));
10286
10287 /* Make the echo area buffer current. Note that for display
10288 purposes, it is not necessary that the displayed window's buffer
10289 == current_buffer, except for text property lookup. So, let's
10290 only set that buffer temporarily here without doing a full
10291 Fset_window_buffer. We must also change w->pointm, though,
10292 because otherwise an assertions in unshow_buffer fails, and Emacs
10293 aborts. */
10294 set_buffer_internal_1 (XBUFFER (buffer));
10295 if (w)
10296 {
10297 wset_buffer (w, buffer);
10298 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10299 }
10300
10301 bset_undo_list (current_buffer, Qt);
10302 bset_read_only (current_buffer, Qnil);
10303 specbind (Qinhibit_read_only, Qt);
10304 specbind (Qinhibit_modification_hooks, Qt);
10305
10306 if (clear_buffer_p && Z > BEG)
10307 del_range (BEG, Z);
10308
10309 eassert (BEGV >= BEG);
10310 eassert (ZV <= Z && ZV >= BEGV);
10311
10312 rc = fn (a1, a2);
10313
10314 eassert (BEGV >= BEG);
10315 eassert (ZV <= Z && ZV >= BEGV);
10316
10317 unbind_to (count, Qnil);
10318 return rc;
10319 }
10320
10321
10322 /* Save state that should be preserved around the call to the function
10323 FN called in with_echo_area_buffer. */
10324
10325 static Lisp_Object
10326 with_echo_area_buffer_unwind_data (struct window *w)
10327 {
10328 int i = 0;
10329 Lisp_Object vector, tmp;
10330
10331 /* Reduce consing by keeping one vector in
10332 Vwith_echo_area_save_vector. */
10333 vector = Vwith_echo_area_save_vector;
10334 Vwith_echo_area_save_vector = Qnil;
10335
10336 if (NILP (vector))
10337 vector = Fmake_vector (make_number (9), Qnil);
10338
10339 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10340 ASET (vector, i, Vdeactivate_mark); ++i;
10341 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10342
10343 if (w)
10344 {
10345 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10346 ASET (vector, i, w->contents); ++i;
10347 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10348 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10349 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10350 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10351 }
10352 else
10353 {
10354 int end = i + 6;
10355 for (; i < end; ++i)
10356 ASET (vector, i, Qnil);
10357 }
10358
10359 eassert (i == ASIZE (vector));
10360 return vector;
10361 }
10362
10363
10364 /* Restore global state from VECTOR which was created by
10365 with_echo_area_buffer_unwind_data. */
10366
10367 static void
10368 unwind_with_echo_area_buffer (Lisp_Object vector)
10369 {
10370 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10371 Vdeactivate_mark = AREF (vector, 1);
10372 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10373
10374 if (WINDOWP (AREF (vector, 3)))
10375 {
10376 struct window *w;
10377 Lisp_Object buffer;
10378
10379 w = XWINDOW (AREF (vector, 3));
10380 buffer = AREF (vector, 4);
10381
10382 wset_buffer (w, buffer);
10383 set_marker_both (w->pointm, buffer,
10384 XFASTINT (AREF (vector, 5)),
10385 XFASTINT (AREF (vector, 6)));
10386 set_marker_both (w->start, buffer,
10387 XFASTINT (AREF (vector, 7)),
10388 XFASTINT (AREF (vector, 8)));
10389 }
10390
10391 Vwith_echo_area_save_vector = vector;
10392 }
10393
10394
10395 /* Set up the echo area for use by print functions. MULTIBYTE_P
10396 non-zero means we will print multibyte. */
10397
10398 void
10399 setup_echo_area_for_printing (int multibyte_p)
10400 {
10401 /* If we can't find an echo area any more, exit. */
10402 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10403 Fkill_emacs (Qnil);
10404
10405 ensure_echo_area_buffers ();
10406
10407 if (!message_buf_print)
10408 {
10409 /* A message has been output since the last time we printed.
10410 Choose a fresh echo area buffer. */
10411 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10412 echo_area_buffer[0] = echo_buffer[1];
10413 else
10414 echo_area_buffer[0] = echo_buffer[0];
10415
10416 /* Switch to that buffer and clear it. */
10417 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10418 bset_truncate_lines (current_buffer, Qnil);
10419
10420 if (Z > BEG)
10421 {
10422 ptrdiff_t count = SPECPDL_INDEX ();
10423 specbind (Qinhibit_read_only, Qt);
10424 /* Note that undo recording is always disabled. */
10425 del_range (BEG, Z);
10426 unbind_to (count, Qnil);
10427 }
10428 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10429
10430 /* Set up the buffer for the multibyteness we need. */
10431 if (multibyte_p
10432 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10433 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10434
10435 /* Raise the frame containing the echo area. */
10436 if (minibuffer_auto_raise)
10437 {
10438 struct frame *sf = SELECTED_FRAME ();
10439 Lisp_Object mini_window;
10440 mini_window = FRAME_MINIBUF_WINDOW (sf);
10441 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10442 }
10443
10444 message_log_maybe_newline ();
10445 message_buf_print = 1;
10446 }
10447 else
10448 {
10449 if (NILP (echo_area_buffer[0]))
10450 {
10451 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10452 echo_area_buffer[0] = echo_buffer[1];
10453 else
10454 echo_area_buffer[0] = echo_buffer[0];
10455 }
10456
10457 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10458 {
10459 /* Someone switched buffers between print requests. */
10460 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10461 bset_truncate_lines (current_buffer, Qnil);
10462 }
10463 }
10464 }
10465
10466
10467 /* Display an echo area message in window W. Value is non-zero if W's
10468 height is changed. If display_last_displayed_message_p is
10469 non-zero, display the message that was last displayed, otherwise
10470 display the current message. */
10471
10472 static int
10473 display_echo_area (struct window *w)
10474 {
10475 int i, no_message_p, window_height_changed_p;
10476
10477 /* Temporarily disable garbage collections while displaying the echo
10478 area. This is done because a GC can print a message itself.
10479 That message would modify the echo area buffer's contents while a
10480 redisplay of the buffer is going on, and seriously confuse
10481 redisplay. */
10482 ptrdiff_t count = inhibit_garbage_collection ();
10483
10484 /* If there is no message, we must call display_echo_area_1
10485 nevertheless because it resizes the window. But we will have to
10486 reset the echo_area_buffer in question to nil at the end because
10487 with_echo_area_buffer will sets it to an empty buffer. */
10488 i = display_last_displayed_message_p ? 1 : 0;
10489 no_message_p = NILP (echo_area_buffer[i]);
10490
10491 window_height_changed_p
10492 = with_echo_area_buffer (w, display_last_displayed_message_p,
10493 display_echo_area_1,
10494 (intptr_t) w, Qnil);
10495
10496 if (no_message_p)
10497 echo_area_buffer[i] = Qnil;
10498
10499 unbind_to (count, Qnil);
10500 return window_height_changed_p;
10501 }
10502
10503
10504 /* Helper for display_echo_area. Display the current buffer which
10505 contains the current echo area message in window W, a mini-window,
10506 a pointer to which is passed in A1. A2..A4 are currently not used.
10507 Change the height of W so that all of the message is displayed.
10508 Value is non-zero if height of W was changed. */
10509
10510 static int
10511 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10512 {
10513 intptr_t i1 = a1;
10514 struct window *w = (struct window *) i1;
10515 Lisp_Object window;
10516 struct text_pos start;
10517 int window_height_changed_p = 0;
10518
10519 /* Do this before displaying, so that we have a large enough glyph
10520 matrix for the display. If we can't get enough space for the
10521 whole text, display the last N lines. That works by setting w->start. */
10522 window_height_changed_p = resize_mini_window (w, 0);
10523
10524 /* Use the starting position chosen by resize_mini_window. */
10525 SET_TEXT_POS_FROM_MARKER (start, w->start);
10526
10527 /* Display. */
10528 clear_glyph_matrix (w->desired_matrix);
10529 XSETWINDOW (window, w);
10530 try_window (window, start, 0);
10531
10532 return window_height_changed_p;
10533 }
10534
10535
10536 /* Resize the echo area window to exactly the size needed for the
10537 currently displayed message, if there is one. If a mini-buffer
10538 is active, don't shrink it. */
10539
10540 void
10541 resize_echo_area_exactly (void)
10542 {
10543 if (BUFFERP (echo_area_buffer[0])
10544 && WINDOWP (echo_area_window))
10545 {
10546 struct window *w = XWINDOW (echo_area_window);
10547 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10548 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10549 (intptr_t) w, resize_exactly);
10550 if (resized_p)
10551 {
10552 windows_or_buffers_changed = 42;
10553 update_mode_lines = 30;
10554 redisplay_internal ();
10555 }
10556 }
10557 }
10558
10559
10560 /* Callback function for with_echo_area_buffer, when used from
10561 resize_echo_area_exactly. A1 contains a pointer to the window to
10562 resize, EXACTLY non-nil means resize the mini-window exactly to the
10563 size of the text displayed. A3 and A4 are not used. Value is what
10564 resize_mini_window returns. */
10565
10566 static int
10567 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10568 {
10569 intptr_t i1 = a1;
10570 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10571 }
10572
10573
10574 /* Resize mini-window W to fit the size of its contents. EXACT_P
10575 means size the window exactly to the size needed. Otherwise, it's
10576 only enlarged until W's buffer is empty.
10577
10578 Set W->start to the right place to begin display. If the whole
10579 contents fit, start at the beginning. Otherwise, start so as
10580 to make the end of the contents appear. This is particularly
10581 important for y-or-n-p, but seems desirable generally.
10582
10583 Value is non-zero if the window height has been changed. */
10584
10585 int
10586 resize_mini_window (struct window *w, int exact_p)
10587 {
10588 struct frame *f = XFRAME (w->frame);
10589 int window_height_changed_p = 0;
10590
10591 eassert (MINI_WINDOW_P (w));
10592
10593 /* By default, start display at the beginning. */
10594 set_marker_both (w->start, w->contents,
10595 BUF_BEGV (XBUFFER (w->contents)),
10596 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10597
10598 /* Don't resize windows while redisplaying a window; it would
10599 confuse redisplay functions when the size of the window they are
10600 displaying changes from under them. Such a resizing can happen,
10601 for instance, when which-func prints a long message while
10602 we are running fontification-functions. We're running these
10603 functions with safe_call which binds inhibit-redisplay to t. */
10604 if (!NILP (Vinhibit_redisplay))
10605 return 0;
10606
10607 /* Nil means don't try to resize. */
10608 if (NILP (Vresize_mini_windows)
10609 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10610 return 0;
10611
10612 if (!FRAME_MINIBUF_ONLY_P (f))
10613 {
10614 struct it it;
10615 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10616 + WINDOW_PIXEL_HEIGHT (w));
10617 int unit = FRAME_LINE_HEIGHT (f);
10618 int height, max_height;
10619 struct text_pos start;
10620 struct buffer *old_current_buffer = NULL;
10621
10622 if (current_buffer != XBUFFER (w->contents))
10623 {
10624 old_current_buffer = current_buffer;
10625 set_buffer_internal (XBUFFER (w->contents));
10626 }
10627
10628 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10629
10630 /* Compute the max. number of lines specified by the user. */
10631 if (FLOATP (Vmax_mini_window_height))
10632 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10633 else if (INTEGERP (Vmax_mini_window_height))
10634 max_height = XINT (Vmax_mini_window_height) * unit;
10635 else
10636 max_height = total_height / 4;
10637
10638 /* Correct that max. height if it's bogus. */
10639 max_height = clip_to_bounds (unit, max_height, total_height);
10640
10641 /* Find out the height of the text in the window. */
10642 if (it.line_wrap == TRUNCATE)
10643 height = unit;
10644 else
10645 {
10646 last_height = 0;
10647 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10648 if (it.max_ascent == 0 && it.max_descent == 0)
10649 height = it.current_y + last_height;
10650 else
10651 height = it.current_y + it.max_ascent + it.max_descent;
10652 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10653 }
10654
10655 /* Compute a suitable window start. */
10656 if (height > max_height)
10657 {
10658 height = max_height;
10659 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10660 move_it_vertically_backward (&it, height);
10661 start = it.current.pos;
10662 }
10663 else
10664 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10665 SET_MARKER_FROM_TEXT_POS (w->start, start);
10666
10667 if (EQ (Vresize_mini_windows, Qgrow_only))
10668 {
10669 /* Let it grow only, until we display an empty message, in which
10670 case the window shrinks again. */
10671 if (height > WINDOW_PIXEL_HEIGHT (w))
10672 {
10673 int old_height = WINDOW_PIXEL_HEIGHT (w);
10674
10675 FRAME_WINDOWS_FROZEN (f) = 1;
10676 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10677 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10678 }
10679 else if (height < WINDOW_PIXEL_HEIGHT (w)
10680 && (exact_p || BEGV == ZV))
10681 {
10682 int old_height = WINDOW_PIXEL_HEIGHT (w);
10683
10684 FRAME_WINDOWS_FROZEN (f) = 0;
10685 shrink_mini_window (w, 1);
10686 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10687 }
10688 }
10689 else
10690 {
10691 /* Always resize to exact size needed. */
10692 if (height > WINDOW_PIXEL_HEIGHT (w))
10693 {
10694 int old_height = WINDOW_PIXEL_HEIGHT (w);
10695
10696 FRAME_WINDOWS_FROZEN (f) = 1;
10697 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10698 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10699 }
10700 else if (height < WINDOW_PIXEL_HEIGHT (w))
10701 {
10702 int old_height = WINDOW_PIXEL_HEIGHT (w);
10703
10704 FRAME_WINDOWS_FROZEN (f) = 0;
10705 shrink_mini_window (w, 1);
10706
10707 if (height)
10708 {
10709 FRAME_WINDOWS_FROZEN (f) = 1;
10710 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10711 }
10712
10713 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10714 }
10715 }
10716
10717 if (old_current_buffer)
10718 set_buffer_internal (old_current_buffer);
10719 }
10720
10721 return window_height_changed_p;
10722 }
10723
10724
10725 /* Value is the current message, a string, or nil if there is no
10726 current message. */
10727
10728 Lisp_Object
10729 current_message (void)
10730 {
10731 Lisp_Object msg;
10732
10733 if (!BUFFERP (echo_area_buffer[0]))
10734 msg = Qnil;
10735 else
10736 {
10737 with_echo_area_buffer (0, 0, current_message_1,
10738 (intptr_t) &msg, Qnil);
10739 if (NILP (msg))
10740 echo_area_buffer[0] = Qnil;
10741 }
10742
10743 return msg;
10744 }
10745
10746
10747 static int
10748 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10749 {
10750 intptr_t i1 = a1;
10751 Lisp_Object *msg = (Lisp_Object *) i1;
10752
10753 if (Z > BEG)
10754 *msg = make_buffer_string (BEG, Z, 1);
10755 else
10756 *msg = Qnil;
10757 return 0;
10758 }
10759
10760
10761 /* Push the current message on Vmessage_stack for later restoration
10762 by restore_message. Value is non-zero if the current message isn't
10763 empty. This is a relatively infrequent operation, so it's not
10764 worth optimizing. */
10765
10766 bool
10767 push_message (void)
10768 {
10769 Lisp_Object msg = current_message ();
10770 Vmessage_stack = Fcons (msg, Vmessage_stack);
10771 return STRINGP (msg);
10772 }
10773
10774
10775 /* Restore message display from the top of Vmessage_stack. */
10776
10777 void
10778 restore_message (void)
10779 {
10780 eassert (CONSP (Vmessage_stack));
10781 message3_nolog (XCAR (Vmessage_stack));
10782 }
10783
10784
10785 /* Handler for unwind-protect calling pop_message. */
10786
10787 void
10788 pop_message_unwind (void)
10789 {
10790 /* Pop the top-most entry off Vmessage_stack. */
10791 eassert (CONSP (Vmessage_stack));
10792 Vmessage_stack = XCDR (Vmessage_stack);
10793 }
10794
10795
10796 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10797 exits. If the stack is not empty, we have a missing pop_message
10798 somewhere. */
10799
10800 void
10801 check_message_stack (void)
10802 {
10803 if (!NILP (Vmessage_stack))
10804 emacs_abort ();
10805 }
10806
10807
10808 /* Truncate to NCHARS what will be displayed in the echo area the next
10809 time we display it---but don't redisplay it now. */
10810
10811 void
10812 truncate_echo_area (ptrdiff_t nchars)
10813 {
10814 if (nchars == 0)
10815 echo_area_buffer[0] = Qnil;
10816 else if (!noninteractive
10817 && INTERACTIVE
10818 && !NILP (echo_area_buffer[0]))
10819 {
10820 struct frame *sf = SELECTED_FRAME ();
10821 /* Error messages get reported properly by cmd_error, so this must be
10822 just an informative message; if the frame hasn't really been
10823 initialized yet, just toss it. */
10824 if (sf->glyphs_initialized_p)
10825 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10826 }
10827 }
10828
10829
10830 /* Helper function for truncate_echo_area. Truncate the current
10831 message to at most NCHARS characters. */
10832
10833 static int
10834 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10835 {
10836 if (BEG + nchars < Z)
10837 del_range (BEG + nchars, Z);
10838 if (Z == BEG)
10839 echo_area_buffer[0] = Qnil;
10840 return 0;
10841 }
10842
10843 /* Set the current message to STRING. */
10844
10845 static void
10846 set_message (Lisp_Object string)
10847 {
10848 eassert (STRINGP (string));
10849
10850 message_enable_multibyte = STRING_MULTIBYTE (string);
10851
10852 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10853 message_buf_print = 0;
10854 help_echo_showing_p = 0;
10855
10856 if (STRINGP (Vdebug_on_message)
10857 && STRINGP (string)
10858 && fast_string_match (Vdebug_on_message, string) >= 0)
10859 call_debugger (list2 (Qerror, string));
10860 }
10861
10862
10863 /* Helper function for set_message. First argument is ignored and second
10864 argument has the same meaning as for set_message.
10865 This function is called with the echo area buffer being current. */
10866
10867 static int
10868 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10869 {
10870 eassert (STRINGP (string));
10871
10872 /* Change multibyteness of the echo buffer appropriately. */
10873 if (message_enable_multibyte
10874 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10875 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10876
10877 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10878 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10879 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10880
10881 /* Insert new message at BEG. */
10882 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10883
10884 /* This function takes care of single/multibyte conversion.
10885 We just have to ensure that the echo area buffer has the right
10886 setting of enable_multibyte_characters. */
10887 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10888
10889 return 0;
10890 }
10891
10892
10893 /* Clear messages. CURRENT_P non-zero means clear the current
10894 message. LAST_DISPLAYED_P non-zero means clear the message
10895 last displayed. */
10896
10897 void
10898 clear_message (bool current_p, bool last_displayed_p)
10899 {
10900 if (current_p)
10901 {
10902 echo_area_buffer[0] = Qnil;
10903 message_cleared_p = true;
10904 }
10905
10906 if (last_displayed_p)
10907 echo_area_buffer[1] = Qnil;
10908
10909 message_buf_print = 0;
10910 }
10911
10912 /* Clear garbaged frames.
10913
10914 This function is used where the old redisplay called
10915 redraw_garbaged_frames which in turn called redraw_frame which in
10916 turn called clear_frame. The call to clear_frame was a source of
10917 flickering. I believe a clear_frame is not necessary. It should
10918 suffice in the new redisplay to invalidate all current matrices,
10919 and ensure a complete redisplay of all windows. */
10920
10921 static void
10922 clear_garbaged_frames (void)
10923 {
10924 if (frame_garbaged)
10925 {
10926 Lisp_Object tail, frame;
10927
10928 FOR_EACH_FRAME (tail, frame)
10929 {
10930 struct frame *f = XFRAME (frame);
10931
10932 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10933 {
10934 if (f->resized_p)
10935 redraw_frame (f);
10936 else
10937 clear_current_matrices (f);
10938 fset_redisplay (f);
10939 f->garbaged = false;
10940 f->resized_p = false;
10941 }
10942 }
10943
10944 frame_garbaged = false;
10945 }
10946 }
10947
10948
10949 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10950 is non-zero update selected_frame. Value is non-zero if the
10951 mini-windows height has been changed. */
10952
10953 static int
10954 echo_area_display (int update_frame_p)
10955 {
10956 Lisp_Object mini_window;
10957 struct window *w;
10958 struct frame *f;
10959 int window_height_changed_p = 0;
10960 struct frame *sf = SELECTED_FRAME ();
10961
10962 mini_window = FRAME_MINIBUF_WINDOW (sf);
10963 w = XWINDOW (mini_window);
10964 f = XFRAME (WINDOW_FRAME (w));
10965
10966 /* Don't display if frame is invisible or not yet initialized. */
10967 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10968 return 0;
10969
10970 #ifdef HAVE_WINDOW_SYSTEM
10971 /* When Emacs starts, selected_frame may be the initial terminal
10972 frame. If we let this through, a message would be displayed on
10973 the terminal. */
10974 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10975 return 0;
10976 #endif /* HAVE_WINDOW_SYSTEM */
10977
10978 /* Redraw garbaged frames. */
10979 clear_garbaged_frames ();
10980
10981 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10982 {
10983 echo_area_window = mini_window;
10984 window_height_changed_p = display_echo_area (w);
10985 w->must_be_updated_p = true;
10986
10987 /* Update the display, unless called from redisplay_internal.
10988 Also don't update the screen during redisplay itself. The
10989 update will happen at the end of redisplay, and an update
10990 here could cause confusion. */
10991 if (update_frame_p && !redisplaying_p)
10992 {
10993 int n = 0;
10994
10995 /* If the display update has been interrupted by pending
10996 input, update mode lines in the frame. Due to the
10997 pending input, it might have been that redisplay hasn't
10998 been called, so that mode lines above the echo area are
10999 garbaged. This looks odd, so we prevent it here. */
11000 if (!display_completed)
11001 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11002
11003 if (window_height_changed_p
11004 /* Don't do this if Emacs is shutting down. Redisplay
11005 needs to run hooks. */
11006 && !NILP (Vrun_hooks))
11007 {
11008 /* Must update other windows. Likewise as in other
11009 cases, don't let this update be interrupted by
11010 pending input. */
11011 ptrdiff_t count = SPECPDL_INDEX ();
11012 specbind (Qredisplay_dont_pause, Qt);
11013 windows_or_buffers_changed = 44;
11014 redisplay_internal ();
11015 unbind_to (count, Qnil);
11016 }
11017 else if (FRAME_WINDOW_P (f) && n == 0)
11018 {
11019 /* Window configuration is the same as before.
11020 Can do with a display update of the echo area,
11021 unless we displayed some mode lines. */
11022 update_single_window (w, 1);
11023 flush_frame (f);
11024 }
11025 else
11026 update_frame (f, 1, 1);
11027
11028 /* If cursor is in the echo area, make sure that the next
11029 redisplay displays the minibuffer, so that the cursor will
11030 be replaced with what the minibuffer wants. */
11031 if (cursor_in_echo_area)
11032 wset_redisplay (XWINDOW (mini_window));
11033 }
11034 }
11035 else if (!EQ (mini_window, selected_window))
11036 wset_redisplay (XWINDOW (mini_window));
11037
11038 /* Last displayed message is now the current message. */
11039 echo_area_buffer[1] = echo_area_buffer[0];
11040 /* Inform read_char that we're not echoing. */
11041 echo_message_buffer = Qnil;
11042
11043 /* Prevent redisplay optimization in redisplay_internal by resetting
11044 this_line_start_pos. This is done because the mini-buffer now
11045 displays the message instead of its buffer text. */
11046 if (EQ (mini_window, selected_window))
11047 CHARPOS (this_line_start_pos) = 0;
11048
11049 return window_height_changed_p;
11050 }
11051
11052 /* Nonzero if W's buffer was changed but not saved. */
11053
11054 static int
11055 window_buffer_changed (struct window *w)
11056 {
11057 struct buffer *b = XBUFFER (w->contents);
11058
11059 eassert (BUFFER_LIVE_P (b));
11060
11061 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11062 }
11063
11064 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11065
11066 static int
11067 mode_line_update_needed (struct window *w)
11068 {
11069 return (w->column_number_displayed != -1
11070 && !(PT == w->last_point && !window_outdated (w))
11071 && (w->column_number_displayed != current_column ()));
11072 }
11073
11074 /* Nonzero if window start of W is frozen and may not be changed during
11075 redisplay. */
11076
11077 static bool
11078 window_frozen_p (struct window *w)
11079 {
11080 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11081 {
11082 Lisp_Object window;
11083
11084 XSETWINDOW (window, w);
11085 if (MINI_WINDOW_P (w))
11086 return 0;
11087 else if (EQ (window, selected_window))
11088 return 0;
11089 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11090 && EQ (window, Vminibuf_scroll_window))
11091 /* This special window can't be frozen too. */
11092 return 0;
11093 else
11094 return 1;
11095 }
11096 return 0;
11097 }
11098
11099 /***********************************************************************
11100 Mode Lines and Frame Titles
11101 ***********************************************************************/
11102
11103 /* A buffer for constructing non-propertized mode-line strings and
11104 frame titles in it; allocated from the heap in init_xdisp and
11105 resized as needed in store_mode_line_noprop_char. */
11106
11107 static char *mode_line_noprop_buf;
11108
11109 /* The buffer's end, and a current output position in it. */
11110
11111 static char *mode_line_noprop_buf_end;
11112 static char *mode_line_noprop_ptr;
11113
11114 #define MODE_LINE_NOPROP_LEN(start) \
11115 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11116
11117 static enum {
11118 MODE_LINE_DISPLAY = 0,
11119 MODE_LINE_TITLE,
11120 MODE_LINE_NOPROP,
11121 MODE_LINE_STRING
11122 } mode_line_target;
11123
11124 /* Alist that caches the results of :propertize.
11125 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11126 static Lisp_Object mode_line_proptrans_alist;
11127
11128 /* List of strings making up the mode-line. */
11129 static Lisp_Object mode_line_string_list;
11130
11131 /* Base face property when building propertized mode line string. */
11132 static Lisp_Object mode_line_string_face;
11133 static Lisp_Object mode_line_string_face_prop;
11134
11135
11136 /* Unwind data for mode line strings */
11137
11138 static Lisp_Object Vmode_line_unwind_vector;
11139
11140 static Lisp_Object
11141 format_mode_line_unwind_data (struct frame *target_frame,
11142 struct buffer *obuf,
11143 Lisp_Object owin,
11144 int save_proptrans)
11145 {
11146 Lisp_Object vector, tmp;
11147
11148 /* Reduce consing by keeping one vector in
11149 Vwith_echo_area_save_vector. */
11150 vector = Vmode_line_unwind_vector;
11151 Vmode_line_unwind_vector = Qnil;
11152
11153 if (NILP (vector))
11154 vector = Fmake_vector (make_number (10), Qnil);
11155
11156 ASET (vector, 0, make_number (mode_line_target));
11157 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11158 ASET (vector, 2, mode_line_string_list);
11159 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11160 ASET (vector, 4, mode_line_string_face);
11161 ASET (vector, 5, mode_line_string_face_prop);
11162
11163 if (obuf)
11164 XSETBUFFER (tmp, obuf);
11165 else
11166 tmp = Qnil;
11167 ASET (vector, 6, tmp);
11168 ASET (vector, 7, owin);
11169 if (target_frame)
11170 {
11171 /* Similarly to `with-selected-window', if the operation selects
11172 a window on another frame, we must restore that frame's
11173 selected window, and (for a tty) the top-frame. */
11174 ASET (vector, 8, target_frame->selected_window);
11175 if (FRAME_TERMCAP_P (target_frame))
11176 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11177 }
11178
11179 return vector;
11180 }
11181
11182 static void
11183 unwind_format_mode_line (Lisp_Object vector)
11184 {
11185 Lisp_Object old_window = AREF (vector, 7);
11186 Lisp_Object target_frame_window = AREF (vector, 8);
11187 Lisp_Object old_top_frame = AREF (vector, 9);
11188
11189 mode_line_target = XINT (AREF (vector, 0));
11190 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11191 mode_line_string_list = AREF (vector, 2);
11192 if (! EQ (AREF (vector, 3), Qt))
11193 mode_line_proptrans_alist = AREF (vector, 3);
11194 mode_line_string_face = AREF (vector, 4);
11195 mode_line_string_face_prop = AREF (vector, 5);
11196
11197 /* Select window before buffer, since it may change the buffer. */
11198 if (!NILP (old_window))
11199 {
11200 /* If the operation that we are unwinding had selected a window
11201 on a different frame, reset its frame-selected-window. For a
11202 text terminal, reset its top-frame if necessary. */
11203 if (!NILP (target_frame_window))
11204 {
11205 Lisp_Object frame
11206 = WINDOW_FRAME (XWINDOW (target_frame_window));
11207
11208 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11209 Fselect_window (target_frame_window, Qt);
11210
11211 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11212 Fselect_frame (old_top_frame, Qt);
11213 }
11214
11215 Fselect_window (old_window, Qt);
11216 }
11217
11218 if (!NILP (AREF (vector, 6)))
11219 {
11220 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11221 ASET (vector, 6, Qnil);
11222 }
11223
11224 Vmode_line_unwind_vector = vector;
11225 }
11226
11227
11228 /* Store a single character C for the frame title in mode_line_noprop_buf.
11229 Re-allocate mode_line_noprop_buf if necessary. */
11230
11231 static void
11232 store_mode_line_noprop_char (char c)
11233 {
11234 /* If output position has reached the end of the allocated buffer,
11235 increase the buffer's size. */
11236 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11237 {
11238 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11239 ptrdiff_t size = len;
11240 mode_line_noprop_buf =
11241 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11242 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11243 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11244 }
11245
11246 *mode_line_noprop_ptr++ = c;
11247 }
11248
11249
11250 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11251 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11252 characters that yield more columns than PRECISION; PRECISION <= 0
11253 means copy the whole string. Pad with spaces until FIELD_WIDTH
11254 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11255 pad. Called from display_mode_element when it is used to build a
11256 frame title. */
11257
11258 static int
11259 store_mode_line_noprop (const char *string, int field_width, int precision)
11260 {
11261 const unsigned char *str = (const unsigned char *) string;
11262 int n = 0;
11263 ptrdiff_t dummy, nbytes;
11264
11265 /* Copy at most PRECISION chars from STR. */
11266 nbytes = strlen (string);
11267 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11268 while (nbytes--)
11269 store_mode_line_noprop_char (*str++);
11270
11271 /* Fill up with spaces until FIELD_WIDTH reached. */
11272 while (field_width > 0
11273 && n < field_width)
11274 {
11275 store_mode_line_noprop_char (' ');
11276 ++n;
11277 }
11278
11279 return n;
11280 }
11281
11282 /***********************************************************************
11283 Frame Titles
11284 ***********************************************************************/
11285
11286 #ifdef HAVE_WINDOW_SYSTEM
11287
11288 /* Set the title of FRAME, if it has changed. The title format is
11289 Vicon_title_format if FRAME is iconified, otherwise it is
11290 frame_title_format. */
11291
11292 static void
11293 x_consider_frame_title (Lisp_Object frame)
11294 {
11295 struct frame *f = XFRAME (frame);
11296
11297 if (FRAME_WINDOW_P (f)
11298 || FRAME_MINIBUF_ONLY_P (f)
11299 || f->explicit_name)
11300 {
11301 /* Do we have more than one visible frame on this X display? */
11302 Lisp_Object tail, other_frame, fmt;
11303 ptrdiff_t title_start;
11304 char *title;
11305 ptrdiff_t len;
11306 struct it it;
11307 ptrdiff_t count = SPECPDL_INDEX ();
11308
11309 FOR_EACH_FRAME (tail, other_frame)
11310 {
11311 struct frame *tf = XFRAME (other_frame);
11312
11313 if (tf != f
11314 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11315 && !FRAME_MINIBUF_ONLY_P (tf)
11316 && !EQ (other_frame, tip_frame)
11317 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11318 break;
11319 }
11320
11321 /* Set global variable indicating that multiple frames exist. */
11322 multiple_frames = CONSP (tail);
11323
11324 /* Switch to the buffer of selected window of the frame. Set up
11325 mode_line_target so that display_mode_element will output into
11326 mode_line_noprop_buf; then display the title. */
11327 record_unwind_protect (unwind_format_mode_line,
11328 format_mode_line_unwind_data
11329 (f, current_buffer, selected_window, 0));
11330
11331 Fselect_window (f->selected_window, Qt);
11332 set_buffer_internal_1
11333 (XBUFFER (XWINDOW (f->selected_window)->contents));
11334 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11335
11336 mode_line_target = MODE_LINE_TITLE;
11337 title_start = MODE_LINE_NOPROP_LEN (0);
11338 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11339 NULL, DEFAULT_FACE_ID);
11340 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11341 len = MODE_LINE_NOPROP_LEN (title_start);
11342 title = mode_line_noprop_buf + title_start;
11343 unbind_to (count, Qnil);
11344
11345 /* Set the title only if it's changed. This avoids consing in
11346 the common case where it hasn't. (If it turns out that we've
11347 already wasted too much time by walking through the list with
11348 display_mode_element, then we might need to optimize at a
11349 higher level than this.) */
11350 if (! STRINGP (f->name)
11351 || SBYTES (f->name) != len
11352 || memcmp (title, SDATA (f->name), len) != 0)
11353 x_implicitly_set_name (f, make_string (title, len), Qnil);
11354 }
11355 }
11356
11357 #endif /* not HAVE_WINDOW_SYSTEM */
11358
11359 \f
11360 /***********************************************************************
11361 Menu Bars
11362 ***********************************************************************/
11363
11364 /* Non-zero if we will not redisplay all visible windows. */
11365 #define REDISPLAY_SOME_P() \
11366 ((windows_or_buffers_changed == 0 \
11367 || windows_or_buffers_changed == REDISPLAY_SOME) \
11368 && (update_mode_lines == 0 \
11369 || update_mode_lines == REDISPLAY_SOME))
11370
11371 /* Prepare for redisplay by updating menu-bar item lists when
11372 appropriate. This can call eval. */
11373
11374 static void
11375 prepare_menu_bars (void)
11376 {
11377 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11378 bool some_windows = REDISPLAY_SOME_P ();
11379 struct gcpro gcpro1, gcpro2;
11380 Lisp_Object tooltip_frame;
11381
11382 #ifdef HAVE_WINDOW_SYSTEM
11383 tooltip_frame = tip_frame;
11384 #else
11385 tooltip_frame = Qnil;
11386 #endif
11387
11388 if (FUNCTIONP (Vpre_redisplay_function))
11389 {
11390 Lisp_Object windows = all_windows ? Qt : Qnil;
11391 if (all_windows && some_windows)
11392 {
11393 Lisp_Object ws = window_list ();
11394 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11395 {
11396 Lisp_Object this = XCAR (ws);
11397 struct window *w = XWINDOW (this);
11398 if (w->redisplay
11399 || XFRAME (w->frame)->redisplay
11400 || XBUFFER (w->contents)->text->redisplay)
11401 {
11402 windows = Fcons (this, windows);
11403 }
11404 }
11405 }
11406 safe_call1 (Vpre_redisplay_function, windows);
11407 }
11408
11409 /* Update all frame titles based on their buffer names, etc. We do
11410 this before the menu bars so that the buffer-menu will show the
11411 up-to-date frame titles. */
11412 #ifdef HAVE_WINDOW_SYSTEM
11413 if (all_windows)
11414 {
11415 Lisp_Object tail, frame;
11416
11417 FOR_EACH_FRAME (tail, frame)
11418 {
11419 struct frame *f = XFRAME (frame);
11420 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11421 if (some_windows
11422 && !f->redisplay
11423 && !w->redisplay
11424 && !XBUFFER (w->contents)->text->redisplay)
11425 continue;
11426
11427 if (!EQ (frame, tooltip_frame)
11428 && (FRAME_ICONIFIED_P (f)
11429 || FRAME_VISIBLE_P (f) == 1
11430 /* Exclude TTY frames that are obscured because they
11431 are not the top frame on their console. This is
11432 because x_consider_frame_title actually switches
11433 to the frame, which for TTY frames means it is
11434 marked as garbaged, and will be completely
11435 redrawn on the next redisplay cycle. This causes
11436 TTY frames to be completely redrawn, when there
11437 are more than one of them, even though nothing
11438 should be changed on display. */
11439 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11440 x_consider_frame_title (frame);
11441 }
11442 }
11443 #endif /* HAVE_WINDOW_SYSTEM */
11444
11445 /* Update the menu bar item lists, if appropriate. This has to be
11446 done before any actual redisplay or generation of display lines. */
11447
11448 if (all_windows)
11449 {
11450 Lisp_Object tail, frame;
11451 ptrdiff_t count = SPECPDL_INDEX ();
11452 /* 1 means that update_menu_bar has run its hooks
11453 so any further calls to update_menu_bar shouldn't do so again. */
11454 int menu_bar_hooks_run = 0;
11455
11456 record_unwind_save_match_data ();
11457
11458 FOR_EACH_FRAME (tail, frame)
11459 {
11460 struct frame *f = XFRAME (frame);
11461 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11462
11463 /* Ignore tooltip frame. */
11464 if (EQ (frame, tooltip_frame))
11465 continue;
11466
11467 if (some_windows
11468 && !f->redisplay
11469 && !w->redisplay
11470 && !XBUFFER (w->contents)->text->redisplay)
11471 continue;
11472
11473 /* If a window on this frame changed size, report that to
11474 the user and clear the size-change flag. */
11475 if (FRAME_WINDOW_SIZES_CHANGED (f))
11476 {
11477 Lisp_Object functions;
11478
11479 /* Clear flag first in case we get an error below. */
11480 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11481 functions = Vwindow_size_change_functions;
11482 GCPRO2 (tail, functions);
11483
11484 while (CONSP (functions))
11485 {
11486 if (!EQ (XCAR (functions), Qt))
11487 call1 (XCAR (functions), frame);
11488 functions = XCDR (functions);
11489 }
11490 UNGCPRO;
11491 }
11492
11493 GCPRO1 (tail);
11494 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11495 #ifdef HAVE_WINDOW_SYSTEM
11496 update_tool_bar (f, 0);
11497 #endif
11498 #ifdef HAVE_NS
11499 if (windows_or_buffers_changed
11500 && FRAME_NS_P (f))
11501 ns_set_doc_edited
11502 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11503 #endif
11504 UNGCPRO;
11505 }
11506
11507 unbind_to (count, Qnil);
11508 }
11509 else
11510 {
11511 struct frame *sf = SELECTED_FRAME ();
11512 update_menu_bar (sf, 1, 0);
11513 #ifdef HAVE_WINDOW_SYSTEM
11514 update_tool_bar (sf, 1);
11515 #endif
11516 }
11517 }
11518
11519
11520 /* Update the menu bar item list for frame F. This has to be done
11521 before we start to fill in any display lines, because it can call
11522 eval.
11523
11524 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11525
11526 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11527 already ran the menu bar hooks for this redisplay, so there
11528 is no need to run them again. The return value is the
11529 updated value of this flag, to pass to the next call. */
11530
11531 static int
11532 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11533 {
11534 Lisp_Object window;
11535 register struct window *w;
11536
11537 /* If called recursively during a menu update, do nothing. This can
11538 happen when, for instance, an activate-menubar-hook causes a
11539 redisplay. */
11540 if (inhibit_menubar_update)
11541 return hooks_run;
11542
11543 window = FRAME_SELECTED_WINDOW (f);
11544 w = XWINDOW (window);
11545
11546 if (FRAME_WINDOW_P (f)
11547 ?
11548 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11549 || defined (HAVE_NS) || defined (USE_GTK)
11550 FRAME_EXTERNAL_MENU_BAR (f)
11551 #else
11552 FRAME_MENU_BAR_LINES (f) > 0
11553 #endif
11554 : FRAME_MENU_BAR_LINES (f) > 0)
11555 {
11556 /* If the user has switched buffers or windows, we need to
11557 recompute to reflect the new bindings. But we'll
11558 recompute when update_mode_lines is set too; that means
11559 that people can use force-mode-line-update to request
11560 that the menu bar be recomputed. The adverse effect on
11561 the rest of the redisplay algorithm is about the same as
11562 windows_or_buffers_changed anyway. */
11563 if (windows_or_buffers_changed
11564 /* This used to test w->update_mode_line, but we believe
11565 there is no need to recompute the menu in that case. */
11566 || update_mode_lines
11567 || window_buffer_changed (w))
11568 {
11569 struct buffer *prev = current_buffer;
11570 ptrdiff_t count = SPECPDL_INDEX ();
11571
11572 specbind (Qinhibit_menubar_update, Qt);
11573
11574 set_buffer_internal_1 (XBUFFER (w->contents));
11575 if (save_match_data)
11576 record_unwind_save_match_data ();
11577 if (NILP (Voverriding_local_map_menu_flag))
11578 {
11579 specbind (Qoverriding_terminal_local_map, Qnil);
11580 specbind (Qoverriding_local_map, Qnil);
11581 }
11582
11583 if (!hooks_run)
11584 {
11585 /* Run the Lucid hook. */
11586 safe_run_hooks (Qactivate_menubar_hook);
11587
11588 /* If it has changed current-menubar from previous value,
11589 really recompute the menu-bar from the value. */
11590 if (! NILP (Vlucid_menu_bar_dirty_flag))
11591 call0 (Qrecompute_lucid_menubar);
11592
11593 safe_run_hooks (Qmenu_bar_update_hook);
11594
11595 hooks_run = 1;
11596 }
11597
11598 XSETFRAME (Vmenu_updating_frame, f);
11599 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11600
11601 /* Redisplay the menu bar in case we changed it. */
11602 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11603 || defined (HAVE_NS) || defined (USE_GTK)
11604 if (FRAME_WINDOW_P (f))
11605 {
11606 #if defined (HAVE_NS)
11607 /* All frames on Mac OS share the same menubar. So only
11608 the selected frame should be allowed to set it. */
11609 if (f == SELECTED_FRAME ())
11610 #endif
11611 set_frame_menubar (f, 0, 0);
11612 }
11613 else
11614 /* On a terminal screen, the menu bar is an ordinary screen
11615 line, and this makes it get updated. */
11616 w->update_mode_line = 1;
11617 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11618 /* In the non-toolkit version, the menu bar is an ordinary screen
11619 line, and this makes it get updated. */
11620 w->update_mode_line = 1;
11621 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11622
11623 unbind_to (count, Qnil);
11624 set_buffer_internal_1 (prev);
11625 }
11626 }
11627
11628 return hooks_run;
11629 }
11630
11631 /***********************************************************************
11632 Tool-bars
11633 ***********************************************************************/
11634
11635 #ifdef HAVE_WINDOW_SYSTEM
11636
11637 /* Tool-bar item index of the item on which a mouse button was pressed
11638 or -1. */
11639
11640 int last_tool_bar_item;
11641
11642 /* Select `frame' temporarily without running all the code in
11643 do_switch_frame.
11644 FIXME: Maybe do_switch_frame should be trimmed down similarly
11645 when `norecord' is set. */
11646 static void
11647 fast_set_selected_frame (Lisp_Object frame)
11648 {
11649 if (!EQ (selected_frame, frame))
11650 {
11651 selected_frame = frame;
11652 selected_window = XFRAME (frame)->selected_window;
11653 }
11654 }
11655
11656 /* Update the tool-bar item list for frame F. This has to be done
11657 before we start to fill in any display lines. Called from
11658 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11659 and restore it here. */
11660
11661 static void
11662 update_tool_bar (struct frame *f, int save_match_data)
11663 {
11664 #if defined (USE_GTK) || defined (HAVE_NS)
11665 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11666 #else
11667 int do_update = (WINDOWP (f->tool_bar_window)
11668 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11669 #endif
11670
11671 if (do_update)
11672 {
11673 Lisp_Object window;
11674 struct window *w;
11675
11676 window = FRAME_SELECTED_WINDOW (f);
11677 w = XWINDOW (window);
11678
11679 /* If the user has switched buffers or windows, we need to
11680 recompute to reflect the new bindings. But we'll
11681 recompute when update_mode_lines is set too; that means
11682 that people can use force-mode-line-update to request
11683 that the menu bar be recomputed. The adverse effect on
11684 the rest of the redisplay algorithm is about the same as
11685 windows_or_buffers_changed anyway. */
11686 if (windows_or_buffers_changed
11687 || w->update_mode_line
11688 || update_mode_lines
11689 || window_buffer_changed (w))
11690 {
11691 struct buffer *prev = current_buffer;
11692 ptrdiff_t count = SPECPDL_INDEX ();
11693 Lisp_Object frame, new_tool_bar;
11694 int new_n_tool_bar;
11695 struct gcpro gcpro1;
11696
11697 /* Set current_buffer to the buffer of the selected
11698 window of the frame, so that we get the right local
11699 keymaps. */
11700 set_buffer_internal_1 (XBUFFER (w->contents));
11701
11702 /* Save match data, if we must. */
11703 if (save_match_data)
11704 record_unwind_save_match_data ();
11705
11706 /* Make sure that we don't accidentally use bogus keymaps. */
11707 if (NILP (Voverriding_local_map_menu_flag))
11708 {
11709 specbind (Qoverriding_terminal_local_map, Qnil);
11710 specbind (Qoverriding_local_map, Qnil);
11711 }
11712
11713 GCPRO1 (new_tool_bar);
11714
11715 /* We must temporarily set the selected frame to this frame
11716 before calling tool_bar_items, because the calculation of
11717 the tool-bar keymap uses the selected frame (see
11718 `tool-bar-make-keymap' in tool-bar.el). */
11719 eassert (EQ (selected_window,
11720 /* Since we only explicitly preserve selected_frame,
11721 check that selected_window would be redundant. */
11722 XFRAME (selected_frame)->selected_window));
11723 record_unwind_protect (fast_set_selected_frame, selected_frame);
11724 XSETFRAME (frame, f);
11725 fast_set_selected_frame (frame);
11726
11727 /* Build desired tool-bar items from keymaps. */
11728 new_tool_bar
11729 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11730 &new_n_tool_bar);
11731
11732 /* Redisplay the tool-bar if we changed it. */
11733 if (new_n_tool_bar != f->n_tool_bar_items
11734 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11735 {
11736 /* Redisplay that happens asynchronously due to an expose event
11737 may access f->tool_bar_items. Make sure we update both
11738 variables within BLOCK_INPUT so no such event interrupts. */
11739 block_input ();
11740 fset_tool_bar_items (f, new_tool_bar);
11741 f->n_tool_bar_items = new_n_tool_bar;
11742 w->update_mode_line = 1;
11743 unblock_input ();
11744 }
11745
11746 UNGCPRO;
11747
11748 unbind_to (count, Qnil);
11749 set_buffer_internal_1 (prev);
11750 }
11751 }
11752 }
11753
11754 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11755
11756 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11757 F's desired tool-bar contents. F->tool_bar_items must have
11758 been set up previously by calling prepare_menu_bars. */
11759
11760 static void
11761 build_desired_tool_bar_string (struct frame *f)
11762 {
11763 int i, size, size_needed;
11764 struct gcpro gcpro1, gcpro2, gcpro3;
11765 Lisp_Object image, plist, props;
11766
11767 image = plist = props = Qnil;
11768 GCPRO3 (image, plist, props);
11769
11770 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11771 Otherwise, make a new string. */
11772
11773 /* The size of the string we might be able to reuse. */
11774 size = (STRINGP (f->desired_tool_bar_string)
11775 ? SCHARS (f->desired_tool_bar_string)
11776 : 0);
11777
11778 /* We need one space in the string for each image. */
11779 size_needed = f->n_tool_bar_items;
11780
11781 /* Reuse f->desired_tool_bar_string, if possible. */
11782 if (size < size_needed || NILP (f->desired_tool_bar_string))
11783 fset_desired_tool_bar_string
11784 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11785 else
11786 {
11787 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11788 Fremove_text_properties (make_number (0), make_number (size),
11789 props, f->desired_tool_bar_string);
11790 }
11791
11792 /* Put a `display' property on the string for the images to display,
11793 put a `menu_item' property on tool-bar items with a value that
11794 is the index of the item in F's tool-bar item vector. */
11795 for (i = 0; i < f->n_tool_bar_items; ++i)
11796 {
11797 #define PROP(IDX) \
11798 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11799
11800 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11801 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11802 int hmargin, vmargin, relief, idx, end;
11803
11804 /* If image is a vector, choose the image according to the
11805 button state. */
11806 image = PROP (TOOL_BAR_ITEM_IMAGES);
11807 if (VECTORP (image))
11808 {
11809 if (enabled_p)
11810 idx = (selected_p
11811 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11812 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11813 else
11814 idx = (selected_p
11815 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11816 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11817
11818 eassert (ASIZE (image) >= idx);
11819 image = AREF (image, idx);
11820 }
11821 else
11822 idx = -1;
11823
11824 /* Ignore invalid image specifications. */
11825 if (!valid_image_p (image))
11826 continue;
11827
11828 /* Display the tool-bar button pressed, or depressed. */
11829 plist = Fcopy_sequence (XCDR (image));
11830
11831 /* Compute margin and relief to draw. */
11832 relief = (tool_bar_button_relief >= 0
11833 ? tool_bar_button_relief
11834 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11835 hmargin = vmargin = relief;
11836
11837 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11838 INT_MAX - max (hmargin, vmargin)))
11839 {
11840 hmargin += XFASTINT (Vtool_bar_button_margin);
11841 vmargin += XFASTINT (Vtool_bar_button_margin);
11842 }
11843 else if (CONSP (Vtool_bar_button_margin))
11844 {
11845 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11846 INT_MAX - hmargin))
11847 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11848
11849 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11850 INT_MAX - vmargin))
11851 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11852 }
11853
11854 if (auto_raise_tool_bar_buttons_p)
11855 {
11856 /* Add a `:relief' property to the image spec if the item is
11857 selected. */
11858 if (selected_p)
11859 {
11860 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11861 hmargin -= relief;
11862 vmargin -= relief;
11863 }
11864 }
11865 else
11866 {
11867 /* If image is selected, display it pressed, i.e. with a
11868 negative relief. If it's not selected, display it with a
11869 raised relief. */
11870 plist = Fplist_put (plist, QCrelief,
11871 (selected_p
11872 ? make_number (-relief)
11873 : make_number (relief)));
11874 hmargin -= relief;
11875 vmargin -= relief;
11876 }
11877
11878 /* Put a margin around the image. */
11879 if (hmargin || vmargin)
11880 {
11881 if (hmargin == vmargin)
11882 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11883 else
11884 plist = Fplist_put (plist, QCmargin,
11885 Fcons (make_number (hmargin),
11886 make_number (vmargin)));
11887 }
11888
11889 /* If button is not enabled, and we don't have special images
11890 for the disabled state, make the image appear disabled by
11891 applying an appropriate algorithm to it. */
11892 if (!enabled_p && idx < 0)
11893 plist = Fplist_put (plist, QCconversion, Qdisabled);
11894
11895 /* Put a `display' text property on the string for the image to
11896 display. Put a `menu-item' property on the string that gives
11897 the start of this item's properties in the tool-bar items
11898 vector. */
11899 image = Fcons (Qimage, plist);
11900 props = list4 (Qdisplay, image,
11901 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11902
11903 /* Let the last image hide all remaining spaces in the tool bar
11904 string. The string can be longer than needed when we reuse a
11905 previous string. */
11906 if (i + 1 == f->n_tool_bar_items)
11907 end = SCHARS (f->desired_tool_bar_string);
11908 else
11909 end = i + 1;
11910 Fadd_text_properties (make_number (i), make_number (end),
11911 props, f->desired_tool_bar_string);
11912 #undef PROP
11913 }
11914
11915 UNGCPRO;
11916 }
11917
11918
11919 /* Display one line of the tool-bar of frame IT->f.
11920
11921 HEIGHT specifies the desired height of the tool-bar line.
11922 If the actual height of the glyph row is less than HEIGHT, the
11923 row's height is increased to HEIGHT, and the icons are centered
11924 vertically in the new height.
11925
11926 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11927 count a final empty row in case the tool-bar width exactly matches
11928 the window width.
11929 */
11930
11931 static void
11932 display_tool_bar_line (struct it *it, int height)
11933 {
11934 struct glyph_row *row = it->glyph_row;
11935 int max_x = it->last_visible_x;
11936 struct glyph *last;
11937
11938 prepare_desired_row (row);
11939 row->y = it->current_y;
11940
11941 /* Note that this isn't made use of if the face hasn't a box,
11942 so there's no need to check the face here. */
11943 it->start_of_box_run_p = 1;
11944
11945 while (it->current_x < max_x)
11946 {
11947 int x, n_glyphs_before, i, nglyphs;
11948 struct it it_before;
11949
11950 /* Get the next display element. */
11951 if (!get_next_display_element (it))
11952 {
11953 /* Don't count empty row if we are counting needed tool-bar lines. */
11954 if (height < 0 && !it->hpos)
11955 return;
11956 break;
11957 }
11958
11959 /* Produce glyphs. */
11960 n_glyphs_before = row->used[TEXT_AREA];
11961 it_before = *it;
11962
11963 PRODUCE_GLYPHS (it);
11964
11965 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11966 i = 0;
11967 x = it_before.current_x;
11968 while (i < nglyphs)
11969 {
11970 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11971
11972 if (x + glyph->pixel_width > max_x)
11973 {
11974 /* Glyph doesn't fit on line. Backtrack. */
11975 row->used[TEXT_AREA] = n_glyphs_before;
11976 *it = it_before;
11977 /* If this is the only glyph on this line, it will never fit on the
11978 tool-bar, so skip it. But ensure there is at least one glyph,
11979 so we don't accidentally disable the tool-bar. */
11980 if (n_glyphs_before == 0
11981 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11982 break;
11983 goto out;
11984 }
11985
11986 ++it->hpos;
11987 x += glyph->pixel_width;
11988 ++i;
11989 }
11990
11991 /* Stop at line end. */
11992 if (ITERATOR_AT_END_OF_LINE_P (it))
11993 break;
11994
11995 set_iterator_to_next (it, 1);
11996 }
11997
11998 out:;
11999
12000 row->displays_text_p = row->used[TEXT_AREA] != 0;
12001
12002 /* Use default face for the border below the tool bar.
12003
12004 FIXME: When auto-resize-tool-bars is grow-only, there is
12005 no additional border below the possibly empty tool-bar lines.
12006 So to make the extra empty lines look "normal", we have to
12007 use the tool-bar face for the border too. */
12008 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12009 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12010 it->face_id = DEFAULT_FACE_ID;
12011
12012 extend_face_to_end_of_line (it);
12013 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12014 last->right_box_line_p = 1;
12015 if (last == row->glyphs[TEXT_AREA])
12016 last->left_box_line_p = 1;
12017
12018 /* Make line the desired height and center it vertically. */
12019 if ((height -= it->max_ascent + it->max_descent) > 0)
12020 {
12021 /* Don't add more than one line height. */
12022 height %= FRAME_LINE_HEIGHT (it->f);
12023 it->max_ascent += height / 2;
12024 it->max_descent += (height + 1) / 2;
12025 }
12026
12027 compute_line_metrics (it);
12028
12029 /* If line is empty, make it occupy the rest of the tool-bar. */
12030 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12031 {
12032 row->height = row->phys_height = it->last_visible_y - row->y;
12033 row->visible_height = row->height;
12034 row->ascent = row->phys_ascent = 0;
12035 row->extra_line_spacing = 0;
12036 }
12037
12038 row->full_width_p = 1;
12039 row->continued_p = 0;
12040 row->truncated_on_left_p = 0;
12041 row->truncated_on_right_p = 0;
12042
12043 it->current_x = it->hpos = 0;
12044 it->current_y += row->height;
12045 ++it->vpos;
12046 ++it->glyph_row;
12047 }
12048
12049
12050 /* Max tool-bar height. Basically, this is what makes all other windows
12051 disappear when the frame gets too small. Rethink this! */
12052
12053 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12054 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12055
12056 /* Value is the number of screen lines needed to make all tool-bar
12057 items of frame F visible. The number of actual rows needed is
12058 returned in *N_ROWS if non-NULL. */
12059
12060 static int
12061 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12062 {
12063 struct window *w = XWINDOW (f->tool_bar_window);
12064 struct it it;
12065 /* tool_bar_height is called from redisplay_tool_bar after building
12066 the desired matrix, so use (unused) mode-line row as temporary row to
12067 avoid destroying the first tool-bar row. */
12068 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12069
12070 /* Initialize an iterator for iteration over
12071 F->desired_tool_bar_string in the tool-bar window of frame F. */
12072 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12073 it.first_visible_x = 0;
12074 /* PXW: Use FRAME_PIXEL_WIDTH (f) here? */
12075 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12076 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12077 it.paragraph_embedding = L2R;
12078
12079 while (!ITERATOR_AT_END_P (&it))
12080 {
12081 clear_glyph_row (temp_row);
12082 it.glyph_row = temp_row;
12083 display_tool_bar_line (&it, -1);
12084 }
12085 clear_glyph_row (temp_row);
12086
12087 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12088 if (n_rows)
12089 *n_rows = it.vpos > 0 ? it.vpos : -1;
12090
12091 if (pixelwise)
12092 return it.current_y;
12093 else
12094 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12095 }
12096
12097 #endif /* !USE_GTK && !HAVE_NS */
12098
12099 #if defined USE_GTK || defined HAVE_NS
12100 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12101 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12102 #endif
12103
12104 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12105 0, 2, 0,
12106 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12107 If FRAME is nil or omitted, use the selected frame. Optional argument
12108 PIXELWISE non-nil means return the height of the tool bar inpixels. */)
12109 (Lisp_Object frame, Lisp_Object pixelwise)
12110 {
12111 int height = 0;
12112
12113 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12114 struct frame *f = decode_any_frame (frame);
12115
12116 if (WINDOWP (f->tool_bar_window)
12117 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12118 {
12119 update_tool_bar (f, 1);
12120 if (f->n_tool_bar_items)
12121 {
12122 build_desired_tool_bar_string (f);
12123 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12124 }
12125 }
12126 #endif
12127
12128 return make_number (height);
12129 }
12130
12131
12132 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12133 height should be changed. */
12134
12135 static int
12136 redisplay_tool_bar (struct frame *f)
12137 {
12138 #if defined (USE_GTK) || defined (HAVE_NS)
12139
12140 if (FRAME_EXTERNAL_TOOL_BAR (f))
12141 update_frame_tool_bar (f);
12142 return 0;
12143
12144 #else /* !USE_GTK && !HAVE_NS */
12145
12146 struct window *w;
12147 struct it it;
12148 struct glyph_row *row;
12149
12150 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12151 do anything. This means you must start with tool-bar-lines
12152 non-zero to get the auto-sizing effect. Or in other words, you
12153 can turn off tool-bars by specifying tool-bar-lines zero. */
12154 if (!WINDOWP (f->tool_bar_window)
12155 || (w = XWINDOW (f->tool_bar_window),
12156 WINDOW_PIXEL_HEIGHT (w) == 0))
12157 return 0;
12158
12159 /* Set up an iterator for the tool-bar window. */
12160 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12161 it.first_visible_x = 0;
12162 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12163 row = it.glyph_row;
12164
12165 /* Build a string that represents the contents of the tool-bar. */
12166 build_desired_tool_bar_string (f);
12167 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12168 /* FIXME: This should be controlled by a user option. But it
12169 doesn't make sense to have an R2L tool bar if the menu bar cannot
12170 be drawn also R2L, and making the menu bar R2L is tricky due
12171 toolkit-specific code that implements it. If an R2L tool bar is
12172 ever supported, display_tool_bar_line should also be augmented to
12173 call unproduce_glyphs like display_line and display_string
12174 do. */
12175 it.paragraph_embedding = L2R;
12176
12177 if (f->n_tool_bar_rows == 0)
12178 {
12179 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12180
12181 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12182 {
12183 Lisp_Object frame;
12184 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12185 / FRAME_LINE_HEIGHT (f));
12186
12187 XSETFRAME (frame, f);
12188 Fmodify_frame_parameters (frame,
12189 list1 (Fcons (Qtool_bar_lines,
12190 make_number (new_lines))));
12191 /* Always do that now. */
12192 clear_glyph_matrix (w->desired_matrix);
12193 f->fonts_changed = 1;
12194 return 1;
12195 }
12196 }
12197
12198 /* Display as many lines as needed to display all tool-bar items. */
12199
12200 if (f->n_tool_bar_rows > 0)
12201 {
12202 int border, rows, height, extra;
12203
12204 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12205 border = XINT (Vtool_bar_border);
12206 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12207 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12208 else if (EQ (Vtool_bar_border, Qborder_width))
12209 border = f->border_width;
12210 else
12211 border = 0;
12212 if (border < 0)
12213 border = 0;
12214
12215 rows = f->n_tool_bar_rows;
12216 height = max (1, (it.last_visible_y - border) / rows);
12217 extra = it.last_visible_y - border - height * rows;
12218
12219 while (it.current_y < it.last_visible_y)
12220 {
12221 int h = 0;
12222 if (extra > 0 && rows-- > 0)
12223 {
12224 h = (extra + rows - 1) / rows;
12225 extra -= h;
12226 }
12227 display_tool_bar_line (&it, height + h);
12228 }
12229 }
12230 else
12231 {
12232 while (it.current_y < it.last_visible_y)
12233 display_tool_bar_line (&it, 0);
12234 }
12235
12236 /* It doesn't make much sense to try scrolling in the tool-bar
12237 window, so don't do it. */
12238 w->desired_matrix->no_scrolling_p = 1;
12239 w->must_be_updated_p = 1;
12240
12241 if (!NILP (Vauto_resize_tool_bars))
12242 {
12243 /* Do we really allow the toolbar to occupy the whole frame? */
12244 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12245 int change_height_p = 0;
12246
12247 /* If we couldn't display everything, change the tool-bar's
12248 height if there is room for more. */
12249 if (IT_STRING_CHARPOS (it) < it.end_charpos
12250 && it.current_y < max_tool_bar_height)
12251 change_height_p = 1;
12252
12253 row = it.glyph_row - 1;
12254
12255 /* If there are blank lines at the end, except for a partially
12256 visible blank line at the end that is smaller than
12257 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12258 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12259 && row->height >= FRAME_LINE_HEIGHT (f))
12260 change_height_p = 1;
12261
12262 /* If row displays tool-bar items, but is partially visible,
12263 change the tool-bar's height. */
12264 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12265 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12266 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12267 change_height_p = 1;
12268
12269 /* Resize windows as needed by changing the `tool-bar-lines'
12270 frame parameter. */
12271 if (change_height_p)
12272 {
12273 Lisp_Object frame;
12274 int nrows;
12275 int new_height = tool_bar_height (f, &nrows, 1);
12276
12277 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12278 && !f->minimize_tool_bar_window_p)
12279 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12280 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12281 f->minimize_tool_bar_window_p = 0;
12282
12283 if (change_height_p)
12284 {
12285 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12286 / FRAME_LINE_HEIGHT (f));
12287
12288 XSETFRAME (frame, f);
12289 Fmodify_frame_parameters (frame,
12290 list1 (Fcons (Qtool_bar_lines,
12291 make_number (new_lines))));
12292 /* Always do that now. */
12293 clear_glyph_matrix (w->desired_matrix);
12294 f->n_tool_bar_rows = nrows;
12295 f->fonts_changed = 1;
12296 return 1;
12297 }
12298 }
12299 }
12300
12301 f->minimize_tool_bar_window_p = 0;
12302 return 0;
12303
12304 #endif /* USE_GTK || HAVE_NS */
12305 }
12306
12307 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12308
12309 /* Get information about the tool-bar item which is displayed in GLYPH
12310 on frame F. Return in *PROP_IDX the index where tool-bar item
12311 properties start in F->tool_bar_items. Value is zero if
12312 GLYPH doesn't display a tool-bar item. */
12313
12314 static int
12315 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12316 {
12317 Lisp_Object prop;
12318 int success_p;
12319 int charpos;
12320
12321 /* This function can be called asynchronously, which means we must
12322 exclude any possibility that Fget_text_property signals an
12323 error. */
12324 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12325 charpos = max (0, charpos);
12326
12327 /* Get the text property `menu-item' at pos. The value of that
12328 property is the start index of this item's properties in
12329 F->tool_bar_items. */
12330 prop = Fget_text_property (make_number (charpos),
12331 Qmenu_item, f->current_tool_bar_string);
12332 if (INTEGERP (prop))
12333 {
12334 *prop_idx = XINT (prop);
12335 success_p = 1;
12336 }
12337 else
12338 success_p = 0;
12339
12340 return success_p;
12341 }
12342
12343 \f
12344 /* Get information about the tool-bar item at position X/Y on frame F.
12345 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12346 the current matrix of the tool-bar window of F, or NULL if not
12347 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12348 item in F->tool_bar_items. Value is
12349
12350 -1 if X/Y is not on a tool-bar item
12351 0 if X/Y is on the same item that was highlighted before.
12352 1 otherwise. */
12353
12354 static int
12355 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12356 int *hpos, int *vpos, int *prop_idx)
12357 {
12358 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12359 struct window *w = XWINDOW (f->tool_bar_window);
12360 int area;
12361
12362 /* Find the glyph under X/Y. */
12363 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12364 if (*glyph == NULL)
12365 return -1;
12366
12367 /* Get the start of this tool-bar item's properties in
12368 f->tool_bar_items. */
12369 if (!tool_bar_item_info (f, *glyph, prop_idx))
12370 return -1;
12371
12372 /* Is mouse on the highlighted item? */
12373 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12374 && *vpos >= hlinfo->mouse_face_beg_row
12375 && *vpos <= hlinfo->mouse_face_end_row
12376 && (*vpos > hlinfo->mouse_face_beg_row
12377 || *hpos >= hlinfo->mouse_face_beg_col)
12378 && (*vpos < hlinfo->mouse_face_end_row
12379 || *hpos < hlinfo->mouse_face_end_col
12380 || hlinfo->mouse_face_past_end))
12381 return 0;
12382
12383 return 1;
12384 }
12385
12386
12387 /* EXPORT:
12388 Handle mouse button event on the tool-bar of frame F, at
12389 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12390 0 for button release. MODIFIERS is event modifiers for button
12391 release. */
12392
12393 void
12394 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12395 int modifiers)
12396 {
12397 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12398 struct window *w = XWINDOW (f->tool_bar_window);
12399 int hpos, vpos, prop_idx;
12400 struct glyph *glyph;
12401 Lisp_Object enabled_p;
12402 int ts;
12403
12404 /* If not on the highlighted tool-bar item, and mouse-highlight is
12405 non-nil, return. This is so we generate the tool-bar button
12406 click only when the mouse button is released on the same item as
12407 where it was pressed. However, when mouse-highlight is disabled,
12408 generate the click when the button is released regardless of the
12409 highlight, since tool-bar items are not highlighted in that
12410 case. */
12411 frame_to_window_pixel_xy (w, &x, &y);
12412 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12413 if (ts == -1
12414 || (ts != 0 && !NILP (Vmouse_highlight)))
12415 return;
12416
12417 /* When mouse-highlight is off, generate the click for the item
12418 where the button was pressed, disregarding where it was
12419 released. */
12420 if (NILP (Vmouse_highlight) && !down_p)
12421 prop_idx = last_tool_bar_item;
12422
12423 /* If item is disabled, do nothing. */
12424 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12425 if (NILP (enabled_p))
12426 return;
12427
12428 if (down_p)
12429 {
12430 /* Show item in pressed state. */
12431 if (!NILP (Vmouse_highlight))
12432 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12433 last_tool_bar_item = prop_idx;
12434 }
12435 else
12436 {
12437 Lisp_Object key, frame;
12438 struct input_event event;
12439 EVENT_INIT (event);
12440
12441 /* Show item in released state. */
12442 if (!NILP (Vmouse_highlight))
12443 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12444
12445 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12446
12447 XSETFRAME (frame, f);
12448 event.kind = TOOL_BAR_EVENT;
12449 event.frame_or_window = frame;
12450 event.arg = frame;
12451 kbd_buffer_store_event (&event);
12452
12453 event.kind = TOOL_BAR_EVENT;
12454 event.frame_or_window = frame;
12455 event.arg = key;
12456 event.modifiers = modifiers;
12457 kbd_buffer_store_event (&event);
12458 last_tool_bar_item = -1;
12459 }
12460 }
12461
12462
12463 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12464 tool-bar window-relative coordinates X/Y. Called from
12465 note_mouse_highlight. */
12466
12467 static void
12468 note_tool_bar_highlight (struct frame *f, int x, int y)
12469 {
12470 Lisp_Object window = f->tool_bar_window;
12471 struct window *w = XWINDOW (window);
12472 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12473 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12474 int hpos, vpos;
12475 struct glyph *glyph;
12476 struct glyph_row *row;
12477 int i;
12478 Lisp_Object enabled_p;
12479 int prop_idx;
12480 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12481 int mouse_down_p, rc;
12482
12483 /* Function note_mouse_highlight is called with negative X/Y
12484 values when mouse moves outside of the frame. */
12485 if (x <= 0 || y <= 0)
12486 {
12487 clear_mouse_face (hlinfo);
12488 return;
12489 }
12490
12491 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12492 if (rc < 0)
12493 {
12494 /* Not on tool-bar item. */
12495 clear_mouse_face (hlinfo);
12496 return;
12497 }
12498 else if (rc == 0)
12499 /* On same tool-bar item as before. */
12500 goto set_help_echo;
12501
12502 clear_mouse_face (hlinfo);
12503
12504 /* Mouse is down, but on different tool-bar item? */
12505 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12506 && f == dpyinfo->last_mouse_frame);
12507
12508 if (mouse_down_p
12509 && last_tool_bar_item != prop_idx)
12510 return;
12511
12512 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12513
12514 /* If tool-bar item is not enabled, don't highlight it. */
12515 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12516 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12517 {
12518 /* Compute the x-position of the glyph. In front and past the
12519 image is a space. We include this in the highlighted area. */
12520 row = MATRIX_ROW (w->current_matrix, vpos);
12521 for (i = x = 0; i < hpos; ++i)
12522 x += row->glyphs[TEXT_AREA][i].pixel_width;
12523
12524 /* Record this as the current active region. */
12525 hlinfo->mouse_face_beg_col = hpos;
12526 hlinfo->mouse_face_beg_row = vpos;
12527 hlinfo->mouse_face_beg_x = x;
12528 hlinfo->mouse_face_past_end = 0;
12529
12530 hlinfo->mouse_face_end_col = hpos + 1;
12531 hlinfo->mouse_face_end_row = vpos;
12532 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12533 hlinfo->mouse_face_window = window;
12534 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12535
12536 /* Display it as active. */
12537 show_mouse_face (hlinfo, draw);
12538 }
12539
12540 set_help_echo:
12541
12542 /* Set help_echo_string to a help string to display for this tool-bar item.
12543 XTread_socket does the rest. */
12544 help_echo_object = help_echo_window = Qnil;
12545 help_echo_pos = -1;
12546 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12547 if (NILP (help_echo_string))
12548 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12549 }
12550
12551 #endif /* !USE_GTK && !HAVE_NS */
12552
12553 #endif /* HAVE_WINDOW_SYSTEM */
12554
12555
12556 \f
12557 /************************************************************************
12558 Horizontal scrolling
12559 ************************************************************************/
12560
12561 static int hscroll_window_tree (Lisp_Object);
12562 static int hscroll_windows (Lisp_Object);
12563
12564 /* For all leaf windows in the window tree rooted at WINDOW, set their
12565 hscroll value so that PT is (i) visible in the window, and (ii) so
12566 that it is not within a certain margin at the window's left and
12567 right border. Value is non-zero if any window's hscroll has been
12568 changed. */
12569
12570 static int
12571 hscroll_window_tree (Lisp_Object window)
12572 {
12573 int hscrolled_p = 0;
12574 int hscroll_relative_p = FLOATP (Vhscroll_step);
12575 int hscroll_step_abs = 0;
12576 double hscroll_step_rel = 0;
12577
12578 if (hscroll_relative_p)
12579 {
12580 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12581 if (hscroll_step_rel < 0)
12582 {
12583 hscroll_relative_p = 0;
12584 hscroll_step_abs = 0;
12585 }
12586 }
12587 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12588 {
12589 hscroll_step_abs = XINT (Vhscroll_step);
12590 if (hscroll_step_abs < 0)
12591 hscroll_step_abs = 0;
12592 }
12593 else
12594 hscroll_step_abs = 0;
12595
12596 while (WINDOWP (window))
12597 {
12598 struct window *w = XWINDOW (window);
12599
12600 if (WINDOWP (w->contents))
12601 hscrolled_p |= hscroll_window_tree (w->contents);
12602 else if (w->cursor.vpos >= 0)
12603 {
12604 int h_margin;
12605 int text_area_width;
12606 struct glyph_row *current_cursor_row
12607 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12608 struct glyph_row *desired_cursor_row
12609 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12610 struct glyph_row *cursor_row
12611 = (desired_cursor_row->enabled_p
12612 ? desired_cursor_row
12613 : current_cursor_row);
12614 int row_r2l_p = cursor_row->reversed_p;
12615
12616 text_area_width = window_box_width (w, TEXT_AREA);
12617
12618 /* Scroll when cursor is inside this scroll margin. */
12619 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12620
12621 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12622 /* For left-to-right rows, hscroll when cursor is either
12623 (i) inside the right hscroll margin, or (ii) if it is
12624 inside the left margin and the window is already
12625 hscrolled. */
12626 && ((!row_r2l_p
12627 && ((w->hscroll
12628 && w->cursor.x <= h_margin)
12629 || (cursor_row->enabled_p
12630 && cursor_row->truncated_on_right_p
12631 && (w->cursor.x >= text_area_width - h_margin))))
12632 /* For right-to-left rows, the logic is similar,
12633 except that rules for scrolling to left and right
12634 are reversed. E.g., if cursor.x <= h_margin, we
12635 need to hscroll "to the right" unconditionally,
12636 and that will scroll the screen to the left so as
12637 to reveal the next portion of the row. */
12638 || (row_r2l_p
12639 && ((cursor_row->enabled_p
12640 /* FIXME: It is confusing to set the
12641 truncated_on_right_p flag when R2L rows
12642 are actually truncated on the left. */
12643 && cursor_row->truncated_on_right_p
12644 && w->cursor.x <= h_margin)
12645 || (w->hscroll
12646 && (w->cursor.x >= text_area_width - h_margin))))))
12647 {
12648 struct it it;
12649 ptrdiff_t hscroll;
12650 struct buffer *saved_current_buffer;
12651 ptrdiff_t pt;
12652 int wanted_x;
12653
12654 /* Find point in a display of infinite width. */
12655 saved_current_buffer = current_buffer;
12656 current_buffer = XBUFFER (w->contents);
12657
12658 if (w == XWINDOW (selected_window))
12659 pt = PT;
12660 else
12661 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12662
12663 /* Move iterator to pt starting at cursor_row->start in
12664 a line with infinite width. */
12665 init_to_row_start (&it, w, cursor_row);
12666 it.last_visible_x = INFINITY;
12667 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12668 current_buffer = saved_current_buffer;
12669
12670 /* Position cursor in window. */
12671 if (!hscroll_relative_p && hscroll_step_abs == 0)
12672 hscroll = max (0, (it.current_x
12673 - (ITERATOR_AT_END_OF_LINE_P (&it)
12674 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12675 : (text_area_width / 2))))
12676 / FRAME_COLUMN_WIDTH (it.f);
12677 else if ((!row_r2l_p
12678 && w->cursor.x >= text_area_width - h_margin)
12679 || (row_r2l_p && w->cursor.x <= h_margin))
12680 {
12681 if (hscroll_relative_p)
12682 wanted_x = text_area_width * (1 - hscroll_step_rel)
12683 - h_margin;
12684 else
12685 wanted_x = text_area_width
12686 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12687 - h_margin;
12688 hscroll
12689 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12690 }
12691 else
12692 {
12693 if (hscroll_relative_p)
12694 wanted_x = text_area_width * hscroll_step_rel
12695 + h_margin;
12696 else
12697 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12698 + h_margin;
12699 hscroll
12700 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12701 }
12702 hscroll = max (hscroll, w->min_hscroll);
12703
12704 /* Don't prevent redisplay optimizations if hscroll
12705 hasn't changed, as it will unnecessarily slow down
12706 redisplay. */
12707 if (w->hscroll != hscroll)
12708 {
12709 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12710 w->hscroll = hscroll;
12711 hscrolled_p = 1;
12712 }
12713 }
12714 }
12715
12716 window = w->next;
12717 }
12718
12719 /* Value is non-zero if hscroll of any leaf window has been changed. */
12720 return hscrolled_p;
12721 }
12722
12723
12724 /* Set hscroll so that cursor is visible and not inside horizontal
12725 scroll margins for all windows in the tree rooted at WINDOW. See
12726 also hscroll_window_tree above. Value is non-zero if any window's
12727 hscroll has been changed. If it has, desired matrices on the frame
12728 of WINDOW are cleared. */
12729
12730 static int
12731 hscroll_windows (Lisp_Object window)
12732 {
12733 int hscrolled_p = hscroll_window_tree (window);
12734 if (hscrolled_p)
12735 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12736 return hscrolled_p;
12737 }
12738
12739
12740 \f
12741 /************************************************************************
12742 Redisplay
12743 ************************************************************************/
12744
12745 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12746 to a non-zero value. This is sometimes handy to have in a debugger
12747 session. */
12748
12749 #ifdef GLYPH_DEBUG
12750
12751 /* First and last unchanged row for try_window_id. */
12752
12753 static int debug_first_unchanged_at_end_vpos;
12754 static int debug_last_unchanged_at_beg_vpos;
12755
12756 /* Delta vpos and y. */
12757
12758 static int debug_dvpos, debug_dy;
12759
12760 /* Delta in characters and bytes for try_window_id. */
12761
12762 static ptrdiff_t debug_delta, debug_delta_bytes;
12763
12764 /* Values of window_end_pos and window_end_vpos at the end of
12765 try_window_id. */
12766
12767 static ptrdiff_t debug_end_vpos;
12768
12769 /* Append a string to W->desired_matrix->method. FMT is a printf
12770 format string. If trace_redisplay_p is non-zero also printf the
12771 resulting string to stderr. */
12772
12773 static void debug_method_add (struct window *, char const *, ...)
12774 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12775
12776 static void
12777 debug_method_add (struct window *w, char const *fmt, ...)
12778 {
12779 void *ptr = w;
12780 char *method = w->desired_matrix->method;
12781 int len = strlen (method);
12782 int size = sizeof w->desired_matrix->method;
12783 int remaining = size - len - 1;
12784 va_list ap;
12785
12786 if (len && remaining)
12787 {
12788 method[len] = '|';
12789 --remaining, ++len;
12790 }
12791
12792 va_start (ap, fmt);
12793 vsnprintf (method + len, remaining + 1, fmt, ap);
12794 va_end (ap);
12795
12796 if (trace_redisplay_p)
12797 fprintf (stderr, "%p (%s): %s\n",
12798 ptr,
12799 ((BUFFERP (w->contents)
12800 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12801 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12802 : "no buffer"),
12803 method + len);
12804 }
12805
12806 #endif /* GLYPH_DEBUG */
12807
12808
12809 /* Value is non-zero if all changes in window W, which displays
12810 current_buffer, are in the text between START and END. START is a
12811 buffer position, END is given as a distance from Z. Used in
12812 redisplay_internal for display optimization. */
12813
12814 static int
12815 text_outside_line_unchanged_p (struct window *w,
12816 ptrdiff_t start, ptrdiff_t end)
12817 {
12818 int unchanged_p = 1;
12819
12820 /* If text or overlays have changed, see where. */
12821 if (window_outdated (w))
12822 {
12823 /* Gap in the line? */
12824 if (GPT < start || Z - GPT < end)
12825 unchanged_p = 0;
12826
12827 /* Changes start in front of the line, or end after it? */
12828 if (unchanged_p
12829 && (BEG_UNCHANGED < start - 1
12830 || END_UNCHANGED < end))
12831 unchanged_p = 0;
12832
12833 /* If selective display, can't optimize if changes start at the
12834 beginning of the line. */
12835 if (unchanged_p
12836 && INTEGERP (BVAR (current_buffer, selective_display))
12837 && XINT (BVAR (current_buffer, selective_display)) > 0
12838 && (BEG_UNCHANGED < start || GPT <= start))
12839 unchanged_p = 0;
12840
12841 /* If there are overlays at the start or end of the line, these
12842 may have overlay strings with newlines in them. A change at
12843 START, for instance, may actually concern the display of such
12844 overlay strings as well, and they are displayed on different
12845 lines. So, quickly rule out this case. (For the future, it
12846 might be desirable to implement something more telling than
12847 just BEG/END_UNCHANGED.) */
12848 if (unchanged_p)
12849 {
12850 if (BEG + BEG_UNCHANGED == start
12851 && overlay_touches_p (start))
12852 unchanged_p = 0;
12853 if (END_UNCHANGED == end
12854 && overlay_touches_p (Z - end))
12855 unchanged_p = 0;
12856 }
12857
12858 /* Under bidi reordering, adding or deleting a character in the
12859 beginning of a paragraph, before the first strong directional
12860 character, can change the base direction of the paragraph (unless
12861 the buffer specifies a fixed paragraph direction), which will
12862 require to redisplay the whole paragraph. It might be worthwhile
12863 to find the paragraph limits and widen the range of redisplayed
12864 lines to that, but for now just give up this optimization. */
12865 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12866 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12867 unchanged_p = 0;
12868 }
12869
12870 return unchanged_p;
12871 }
12872
12873
12874 /* Do a frame update, taking possible shortcuts into account. This is
12875 the main external entry point for redisplay.
12876
12877 If the last redisplay displayed an echo area message and that message
12878 is no longer requested, we clear the echo area or bring back the
12879 mini-buffer if that is in use. */
12880
12881 void
12882 redisplay (void)
12883 {
12884 redisplay_internal ();
12885 }
12886
12887
12888 static Lisp_Object
12889 overlay_arrow_string_or_property (Lisp_Object var)
12890 {
12891 Lisp_Object val;
12892
12893 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12894 return val;
12895
12896 return Voverlay_arrow_string;
12897 }
12898
12899 /* Return 1 if there are any overlay-arrows in current_buffer. */
12900 static int
12901 overlay_arrow_in_current_buffer_p (void)
12902 {
12903 Lisp_Object vlist;
12904
12905 for (vlist = Voverlay_arrow_variable_list;
12906 CONSP (vlist);
12907 vlist = XCDR (vlist))
12908 {
12909 Lisp_Object var = XCAR (vlist);
12910 Lisp_Object val;
12911
12912 if (!SYMBOLP (var))
12913 continue;
12914 val = find_symbol_value (var);
12915 if (MARKERP (val)
12916 && current_buffer == XMARKER (val)->buffer)
12917 return 1;
12918 }
12919 return 0;
12920 }
12921
12922
12923 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12924 has changed. */
12925
12926 static int
12927 overlay_arrows_changed_p (void)
12928 {
12929 Lisp_Object vlist;
12930
12931 for (vlist = Voverlay_arrow_variable_list;
12932 CONSP (vlist);
12933 vlist = XCDR (vlist))
12934 {
12935 Lisp_Object var = XCAR (vlist);
12936 Lisp_Object val, pstr;
12937
12938 if (!SYMBOLP (var))
12939 continue;
12940 val = find_symbol_value (var);
12941 if (!MARKERP (val))
12942 continue;
12943 if (! EQ (COERCE_MARKER (val),
12944 Fget (var, Qlast_arrow_position))
12945 || ! (pstr = overlay_arrow_string_or_property (var),
12946 EQ (pstr, Fget (var, Qlast_arrow_string))))
12947 return 1;
12948 }
12949 return 0;
12950 }
12951
12952 /* Mark overlay arrows to be updated on next redisplay. */
12953
12954 static void
12955 update_overlay_arrows (int up_to_date)
12956 {
12957 Lisp_Object vlist;
12958
12959 for (vlist = Voverlay_arrow_variable_list;
12960 CONSP (vlist);
12961 vlist = XCDR (vlist))
12962 {
12963 Lisp_Object var = XCAR (vlist);
12964
12965 if (!SYMBOLP (var))
12966 continue;
12967
12968 if (up_to_date > 0)
12969 {
12970 Lisp_Object val = find_symbol_value (var);
12971 Fput (var, Qlast_arrow_position,
12972 COERCE_MARKER (val));
12973 Fput (var, Qlast_arrow_string,
12974 overlay_arrow_string_or_property (var));
12975 }
12976 else if (up_to_date < 0
12977 || !NILP (Fget (var, Qlast_arrow_position)))
12978 {
12979 Fput (var, Qlast_arrow_position, Qt);
12980 Fput (var, Qlast_arrow_string, Qt);
12981 }
12982 }
12983 }
12984
12985
12986 /* Return overlay arrow string to display at row.
12987 Return integer (bitmap number) for arrow bitmap in left fringe.
12988 Return nil if no overlay arrow. */
12989
12990 static Lisp_Object
12991 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12992 {
12993 Lisp_Object vlist;
12994
12995 for (vlist = Voverlay_arrow_variable_list;
12996 CONSP (vlist);
12997 vlist = XCDR (vlist))
12998 {
12999 Lisp_Object var = XCAR (vlist);
13000 Lisp_Object val;
13001
13002 if (!SYMBOLP (var))
13003 continue;
13004
13005 val = find_symbol_value (var);
13006
13007 if (MARKERP (val)
13008 && current_buffer == XMARKER (val)->buffer
13009 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13010 {
13011 if (FRAME_WINDOW_P (it->f)
13012 /* FIXME: if ROW->reversed_p is set, this should test
13013 the right fringe, not the left one. */
13014 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13015 {
13016 #ifdef HAVE_WINDOW_SYSTEM
13017 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13018 {
13019 int fringe_bitmap;
13020 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13021 return make_number (fringe_bitmap);
13022 }
13023 #endif
13024 return make_number (-1); /* Use default arrow bitmap. */
13025 }
13026 return overlay_arrow_string_or_property (var);
13027 }
13028 }
13029
13030 return Qnil;
13031 }
13032
13033 /* Return 1 if point moved out of or into a composition. Otherwise
13034 return 0. PREV_BUF and PREV_PT are the last point buffer and
13035 position. BUF and PT are the current point buffer and position. */
13036
13037 static int
13038 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13039 struct buffer *buf, ptrdiff_t pt)
13040 {
13041 ptrdiff_t start, end;
13042 Lisp_Object prop;
13043 Lisp_Object buffer;
13044
13045 XSETBUFFER (buffer, buf);
13046 /* Check a composition at the last point if point moved within the
13047 same buffer. */
13048 if (prev_buf == buf)
13049 {
13050 if (prev_pt == pt)
13051 /* Point didn't move. */
13052 return 0;
13053
13054 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13055 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13056 && composition_valid_p (start, end, prop)
13057 && start < prev_pt && end > prev_pt)
13058 /* The last point was within the composition. Return 1 iff
13059 point moved out of the composition. */
13060 return (pt <= start || pt >= end);
13061 }
13062
13063 /* Check a composition at the current point. */
13064 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13065 && find_composition (pt, -1, &start, &end, &prop, buffer)
13066 && composition_valid_p (start, end, prop)
13067 && start < pt && end > pt);
13068 }
13069
13070 /* Reconsider the clip changes of buffer which is displayed in W. */
13071
13072 static void
13073 reconsider_clip_changes (struct window *w)
13074 {
13075 struct buffer *b = XBUFFER (w->contents);
13076
13077 if (b->clip_changed
13078 && w->window_end_valid
13079 && w->current_matrix->buffer == b
13080 && w->current_matrix->zv == BUF_ZV (b)
13081 && w->current_matrix->begv == BUF_BEGV (b))
13082 b->clip_changed = 0;
13083
13084 /* If display wasn't paused, and W is not a tool bar window, see if
13085 point has been moved into or out of a composition. In that case,
13086 we set b->clip_changed to 1 to force updating the screen. If
13087 b->clip_changed has already been set to 1, we can skip this
13088 check. */
13089 if (!b->clip_changed && w->window_end_valid)
13090 {
13091 ptrdiff_t pt = (w == XWINDOW (selected_window)
13092 ? PT : marker_position (w->pointm));
13093
13094 if ((w->current_matrix->buffer != b || pt != w->last_point)
13095 && check_point_in_composition (w->current_matrix->buffer,
13096 w->last_point, b, pt))
13097 b->clip_changed = 1;
13098 }
13099 }
13100
13101 static void
13102 propagate_buffer_redisplay (void)
13103 { /* Resetting b->text->redisplay is problematic!
13104 We can't just reset it in the case that some window that displays
13105 it has not been redisplayed; and such a window can stay
13106 unredisplayed for a long time if it's currently invisible.
13107 But we do want to reset it at the end of redisplay otherwise
13108 its displayed windows will keep being redisplayed over and over
13109 again.
13110 So we copy all b->text->redisplay flags up to their windows here,
13111 such that mark_window_display_accurate can safely reset
13112 b->text->redisplay. */
13113 Lisp_Object ws = window_list ();
13114 for (; CONSP (ws); ws = XCDR (ws))
13115 {
13116 struct window *thisw = XWINDOW (XCAR (ws));
13117 struct buffer *thisb = XBUFFER (thisw->contents);
13118 if (thisb->text->redisplay)
13119 thisw->redisplay = true;
13120 }
13121 }
13122
13123 #define STOP_POLLING \
13124 do { if (! polling_stopped_here) stop_polling (); \
13125 polling_stopped_here = 1; } while (0)
13126
13127 #define RESUME_POLLING \
13128 do { if (polling_stopped_here) start_polling (); \
13129 polling_stopped_here = 0; } while (0)
13130
13131
13132 /* Perhaps in the future avoid recentering windows if it
13133 is not necessary; currently that causes some problems. */
13134
13135 static void
13136 redisplay_internal (void)
13137 {
13138 struct window *w = XWINDOW (selected_window);
13139 struct window *sw;
13140 struct frame *fr;
13141 int pending;
13142 bool must_finish = 0, match_p;
13143 struct text_pos tlbufpos, tlendpos;
13144 int number_of_visible_frames;
13145 ptrdiff_t count;
13146 struct frame *sf;
13147 int polling_stopped_here = 0;
13148 Lisp_Object tail, frame;
13149
13150 /* True means redisplay has to consider all windows on all
13151 frames. False, only selected_window is considered. */
13152 bool consider_all_windows_p;
13153
13154 /* True means redisplay has to redisplay the miniwindow. */
13155 bool update_miniwindow_p = false;
13156
13157 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13158
13159 /* No redisplay if running in batch mode or frame is not yet fully
13160 initialized, or redisplay is explicitly turned off by setting
13161 Vinhibit_redisplay. */
13162 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13163 || !NILP (Vinhibit_redisplay))
13164 return;
13165
13166 /* Don't examine these until after testing Vinhibit_redisplay.
13167 When Emacs is shutting down, perhaps because its connection to
13168 X has dropped, we should not look at them at all. */
13169 fr = XFRAME (w->frame);
13170 sf = SELECTED_FRAME ();
13171
13172 if (!fr->glyphs_initialized_p)
13173 return;
13174
13175 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13176 if (popup_activated ())
13177 return;
13178 #endif
13179
13180 /* I don't think this happens but let's be paranoid. */
13181 if (redisplaying_p)
13182 return;
13183
13184 /* Record a function that clears redisplaying_p
13185 when we leave this function. */
13186 count = SPECPDL_INDEX ();
13187 record_unwind_protect_void (unwind_redisplay);
13188 redisplaying_p = 1;
13189 specbind (Qinhibit_free_realized_faces, Qnil);
13190
13191 /* Record this function, so it appears on the profiler's backtraces. */
13192 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13193
13194 FOR_EACH_FRAME (tail, frame)
13195 XFRAME (frame)->already_hscrolled_p = 0;
13196
13197 retry:
13198 /* Remember the currently selected window. */
13199 sw = w;
13200
13201 pending = 0;
13202 last_escape_glyph_frame = NULL;
13203 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13204 last_glyphless_glyph_frame = NULL;
13205 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13206
13207 /* If face_change_count is non-zero, init_iterator will free all
13208 realized faces, which includes the faces referenced from current
13209 matrices. So, we can't reuse current matrices in this case. */
13210 if (face_change_count)
13211 windows_or_buffers_changed = 47;
13212
13213 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13214 && FRAME_TTY (sf)->previous_frame != sf)
13215 {
13216 /* Since frames on a single ASCII terminal share the same
13217 display area, displaying a different frame means redisplay
13218 the whole thing. */
13219 SET_FRAME_GARBAGED (sf);
13220 #ifndef DOS_NT
13221 set_tty_color_mode (FRAME_TTY (sf), sf);
13222 #endif
13223 FRAME_TTY (sf)->previous_frame = sf;
13224 }
13225
13226 /* Set the visible flags for all frames. Do this before checking for
13227 resized or garbaged frames; they want to know if their frames are
13228 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13229 number_of_visible_frames = 0;
13230
13231 FOR_EACH_FRAME (tail, frame)
13232 {
13233 struct frame *f = XFRAME (frame);
13234
13235 if (FRAME_VISIBLE_P (f))
13236 {
13237 ++number_of_visible_frames;
13238 /* Adjust matrices for visible frames only. */
13239 if (f->fonts_changed)
13240 {
13241 adjust_frame_glyphs (f);
13242 f->fonts_changed = 0;
13243 }
13244 /* If cursor type has been changed on the frame
13245 other than selected, consider all frames. */
13246 if (f != sf && f->cursor_type_changed)
13247 update_mode_lines = 31;
13248 }
13249 clear_desired_matrices (f);
13250 }
13251
13252 /* Notice any pending interrupt request to change frame size. */
13253 do_pending_window_change (1);
13254
13255 /* do_pending_window_change could change the selected_window due to
13256 frame resizing which makes the selected window too small. */
13257 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13258 sw = w;
13259
13260 /* Clear frames marked as garbaged. */
13261 clear_garbaged_frames ();
13262
13263 /* Build menubar and tool-bar items. */
13264 if (NILP (Vmemory_full))
13265 prepare_menu_bars ();
13266
13267 reconsider_clip_changes (w);
13268
13269 /* In most cases selected window displays current buffer. */
13270 match_p = XBUFFER (w->contents) == current_buffer;
13271 if (match_p)
13272 {
13273 /* Detect case that we need to write or remove a star in the mode line. */
13274 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13275 w->update_mode_line = 1;
13276
13277 if (mode_line_update_needed (w))
13278 w->update_mode_line = 1;
13279 }
13280
13281 /* Normally the message* functions will have already displayed and
13282 updated the echo area, but the frame may have been trashed, or
13283 the update may have been preempted, so display the echo area
13284 again here. Checking message_cleared_p captures the case that
13285 the echo area should be cleared. */
13286 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13287 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13288 || (message_cleared_p
13289 && minibuf_level == 0
13290 /* If the mini-window is currently selected, this means the
13291 echo-area doesn't show through. */
13292 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13293 {
13294 int window_height_changed_p = echo_area_display (0);
13295
13296 if (message_cleared_p)
13297 update_miniwindow_p = true;
13298
13299 must_finish = 1;
13300
13301 /* If we don't display the current message, don't clear the
13302 message_cleared_p flag, because, if we did, we wouldn't clear
13303 the echo area in the next redisplay which doesn't preserve
13304 the echo area. */
13305 if (!display_last_displayed_message_p)
13306 message_cleared_p = 0;
13307
13308 if (window_height_changed_p)
13309 {
13310 windows_or_buffers_changed = 50;
13311
13312 /* If window configuration was changed, frames may have been
13313 marked garbaged. Clear them or we will experience
13314 surprises wrt scrolling. */
13315 clear_garbaged_frames ();
13316 }
13317 }
13318 else if (EQ (selected_window, minibuf_window)
13319 && (current_buffer->clip_changed || window_outdated (w))
13320 && resize_mini_window (w, 0))
13321 {
13322 /* Resized active mini-window to fit the size of what it is
13323 showing if its contents might have changed. */
13324 must_finish = 1;
13325
13326 /* If window configuration was changed, frames may have been
13327 marked garbaged. Clear them or we will experience
13328 surprises wrt scrolling. */
13329 clear_garbaged_frames ();
13330 }
13331
13332 if (windows_or_buffers_changed && !update_mode_lines)
13333 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13334 only the windows's contents needs to be refreshed, or whether the
13335 mode-lines also need a refresh. */
13336 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13337 ? REDISPLAY_SOME : 32);
13338
13339 /* If specs for an arrow have changed, do thorough redisplay
13340 to ensure we remove any arrow that should no longer exist. */
13341 if (overlay_arrows_changed_p ())
13342 /* Apparently, this is the only case where we update other windows,
13343 without updating other mode-lines. */
13344 windows_or_buffers_changed = 49;
13345
13346 consider_all_windows_p = (update_mode_lines
13347 || windows_or_buffers_changed);
13348
13349 #define AINC(a,i) \
13350 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13351 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13352
13353 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13354 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13355
13356 /* Optimize the case that only the line containing the cursor in the
13357 selected window has changed. Variables starting with this_ are
13358 set in display_line and record information about the line
13359 containing the cursor. */
13360 tlbufpos = this_line_start_pos;
13361 tlendpos = this_line_end_pos;
13362 if (!consider_all_windows_p
13363 && CHARPOS (tlbufpos) > 0
13364 && !w->update_mode_line
13365 && !current_buffer->clip_changed
13366 && !current_buffer->prevent_redisplay_optimizations_p
13367 && FRAME_VISIBLE_P (XFRAME (w->frame))
13368 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13369 && !XFRAME (w->frame)->cursor_type_changed
13370 /* Make sure recorded data applies to current buffer, etc. */
13371 && this_line_buffer == current_buffer
13372 && match_p
13373 && !w->force_start
13374 && !w->optional_new_start
13375 /* Point must be on the line that we have info recorded about. */
13376 && PT >= CHARPOS (tlbufpos)
13377 && PT <= Z - CHARPOS (tlendpos)
13378 /* All text outside that line, including its final newline,
13379 must be unchanged. */
13380 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13381 CHARPOS (tlendpos)))
13382 {
13383 if (CHARPOS (tlbufpos) > BEGV
13384 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13385 && (CHARPOS (tlbufpos) == ZV
13386 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13387 /* Former continuation line has disappeared by becoming empty. */
13388 goto cancel;
13389 else if (window_outdated (w) || MINI_WINDOW_P (w))
13390 {
13391 /* We have to handle the case of continuation around a
13392 wide-column character (see the comment in indent.c around
13393 line 1340).
13394
13395 For instance, in the following case:
13396
13397 -------- Insert --------
13398 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13399 J_I_ ==> J_I_ `^^' are cursors.
13400 ^^ ^^
13401 -------- --------
13402
13403 As we have to redraw the line above, we cannot use this
13404 optimization. */
13405
13406 struct it it;
13407 int line_height_before = this_line_pixel_height;
13408
13409 /* Note that start_display will handle the case that the
13410 line starting at tlbufpos is a continuation line. */
13411 start_display (&it, w, tlbufpos);
13412
13413 /* Implementation note: It this still necessary? */
13414 if (it.current_x != this_line_start_x)
13415 goto cancel;
13416
13417 TRACE ((stderr, "trying display optimization 1\n"));
13418 w->cursor.vpos = -1;
13419 overlay_arrow_seen = 0;
13420 it.vpos = this_line_vpos;
13421 it.current_y = this_line_y;
13422 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13423 display_line (&it);
13424
13425 /* If line contains point, is not continued,
13426 and ends at same distance from eob as before, we win. */
13427 if (w->cursor.vpos >= 0
13428 /* Line is not continued, otherwise this_line_start_pos
13429 would have been set to 0 in display_line. */
13430 && CHARPOS (this_line_start_pos)
13431 /* Line ends as before. */
13432 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13433 /* Line has same height as before. Otherwise other lines
13434 would have to be shifted up or down. */
13435 && this_line_pixel_height == line_height_before)
13436 {
13437 /* If this is not the window's last line, we must adjust
13438 the charstarts of the lines below. */
13439 if (it.current_y < it.last_visible_y)
13440 {
13441 struct glyph_row *row
13442 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13443 ptrdiff_t delta, delta_bytes;
13444
13445 /* We used to distinguish between two cases here,
13446 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13447 when the line ends in a newline or the end of the
13448 buffer's accessible portion. But both cases did
13449 the same, so they were collapsed. */
13450 delta = (Z
13451 - CHARPOS (tlendpos)
13452 - MATRIX_ROW_START_CHARPOS (row));
13453 delta_bytes = (Z_BYTE
13454 - BYTEPOS (tlendpos)
13455 - MATRIX_ROW_START_BYTEPOS (row));
13456
13457 increment_matrix_positions (w->current_matrix,
13458 this_line_vpos + 1,
13459 w->current_matrix->nrows,
13460 delta, delta_bytes);
13461 }
13462
13463 /* If this row displays text now but previously didn't,
13464 or vice versa, w->window_end_vpos may have to be
13465 adjusted. */
13466 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13467 {
13468 if (w->window_end_vpos < this_line_vpos)
13469 w->window_end_vpos = this_line_vpos;
13470 }
13471 else if (w->window_end_vpos == this_line_vpos
13472 && this_line_vpos > 0)
13473 w->window_end_vpos = this_line_vpos - 1;
13474 w->window_end_valid = 0;
13475
13476 /* Update hint: No need to try to scroll in update_window. */
13477 w->desired_matrix->no_scrolling_p = 1;
13478
13479 #ifdef GLYPH_DEBUG
13480 *w->desired_matrix->method = 0;
13481 debug_method_add (w, "optimization 1");
13482 #endif
13483 #ifdef HAVE_WINDOW_SYSTEM
13484 update_window_fringes (w, 0);
13485 #endif
13486 goto update;
13487 }
13488 else
13489 goto cancel;
13490 }
13491 else if (/* Cursor position hasn't changed. */
13492 PT == w->last_point
13493 /* Make sure the cursor was last displayed
13494 in this window. Otherwise we have to reposition it. */
13495
13496 /* PXW: Must be converted to pixels, probably. */
13497 && 0 <= w->cursor.vpos
13498 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13499 {
13500 if (!must_finish)
13501 {
13502 do_pending_window_change (1);
13503 /* If selected_window changed, redisplay again. */
13504 if (WINDOWP (selected_window)
13505 && (w = XWINDOW (selected_window)) != sw)
13506 goto retry;
13507
13508 /* We used to always goto end_of_redisplay here, but this
13509 isn't enough if we have a blinking cursor. */
13510 if (w->cursor_off_p == w->last_cursor_off_p)
13511 goto end_of_redisplay;
13512 }
13513 goto update;
13514 }
13515 /* If highlighting the region, or if the cursor is in the echo area,
13516 then we can't just move the cursor. */
13517 else if (NILP (Vshow_trailing_whitespace)
13518 && !cursor_in_echo_area)
13519 {
13520 struct it it;
13521 struct glyph_row *row;
13522
13523 /* Skip from tlbufpos to PT and see where it is. Note that
13524 PT may be in invisible text. If so, we will end at the
13525 next visible position. */
13526 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13527 NULL, DEFAULT_FACE_ID);
13528 it.current_x = this_line_start_x;
13529 it.current_y = this_line_y;
13530 it.vpos = this_line_vpos;
13531
13532 /* The call to move_it_to stops in front of PT, but
13533 moves over before-strings. */
13534 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13535
13536 if (it.vpos == this_line_vpos
13537 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13538 row->enabled_p))
13539 {
13540 eassert (this_line_vpos == it.vpos);
13541 eassert (this_line_y == it.current_y);
13542 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13543 #ifdef GLYPH_DEBUG
13544 *w->desired_matrix->method = 0;
13545 debug_method_add (w, "optimization 3");
13546 #endif
13547 goto update;
13548 }
13549 else
13550 goto cancel;
13551 }
13552
13553 cancel:
13554 /* Text changed drastically or point moved off of line. */
13555 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13556 }
13557
13558 CHARPOS (this_line_start_pos) = 0;
13559 ++clear_face_cache_count;
13560 #ifdef HAVE_WINDOW_SYSTEM
13561 ++clear_image_cache_count;
13562 #endif
13563
13564 /* Build desired matrices, and update the display. If
13565 consider_all_windows_p is non-zero, do it for all windows on all
13566 frames. Otherwise do it for selected_window, only. */
13567
13568 if (consider_all_windows_p)
13569 {
13570 FOR_EACH_FRAME (tail, frame)
13571 XFRAME (frame)->updated_p = 0;
13572
13573 propagate_buffer_redisplay ();
13574
13575 FOR_EACH_FRAME (tail, frame)
13576 {
13577 struct frame *f = XFRAME (frame);
13578
13579 /* We don't have to do anything for unselected terminal
13580 frames. */
13581 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13582 && !EQ (FRAME_TTY (f)->top_frame, frame))
13583 continue;
13584
13585 retry_frame:
13586
13587 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13588 {
13589 bool gcscrollbars
13590 /* Only GC scrollbars when we redisplay the whole frame. */
13591 = f->redisplay || !REDISPLAY_SOME_P ();
13592 /* Mark all the scroll bars to be removed; we'll redeem
13593 the ones we want when we redisplay their windows. */
13594 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13595 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13596
13597 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13598 redisplay_windows (FRAME_ROOT_WINDOW (f));
13599 /* Remember that the invisible frames need to be redisplayed next
13600 time they're visible. */
13601 else if (!REDISPLAY_SOME_P ())
13602 f->redisplay = true;
13603
13604 /* The X error handler may have deleted that frame. */
13605 if (!FRAME_LIVE_P (f))
13606 continue;
13607
13608 /* Any scroll bars which redisplay_windows should have
13609 nuked should now go away. */
13610 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13611 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13612
13613 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13614 {
13615 /* If fonts changed on visible frame, display again. */
13616 if (f->fonts_changed)
13617 {
13618 adjust_frame_glyphs (f);
13619 f->fonts_changed = 0;
13620 goto retry_frame;
13621 }
13622
13623 /* See if we have to hscroll. */
13624 if (!f->already_hscrolled_p)
13625 {
13626 f->already_hscrolled_p = 1;
13627 if (hscroll_windows (f->root_window))
13628 goto retry_frame;
13629 }
13630
13631 /* Prevent various kinds of signals during display
13632 update. stdio is not robust about handling
13633 signals, which can cause an apparent I/O error. */
13634 if (interrupt_input)
13635 unrequest_sigio ();
13636 STOP_POLLING;
13637
13638 pending |= update_frame (f, 0, 0);
13639 f->cursor_type_changed = 0;
13640 f->updated_p = 1;
13641 }
13642 }
13643 }
13644
13645 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13646
13647 if (!pending)
13648 {
13649 /* Do the mark_window_display_accurate after all windows have
13650 been redisplayed because this call resets flags in buffers
13651 which are needed for proper redisplay. */
13652 FOR_EACH_FRAME (tail, frame)
13653 {
13654 struct frame *f = XFRAME (frame);
13655 if (f->updated_p)
13656 {
13657 f->redisplay = false;
13658 mark_window_display_accurate (f->root_window, 1);
13659 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13660 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13661 }
13662 }
13663 }
13664 }
13665 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13666 {
13667 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13668 struct frame *mini_frame;
13669
13670 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13671 /* Use list_of_error, not Qerror, so that
13672 we catch only errors and don't run the debugger. */
13673 internal_condition_case_1 (redisplay_window_1, selected_window,
13674 list_of_error,
13675 redisplay_window_error);
13676 if (update_miniwindow_p)
13677 internal_condition_case_1 (redisplay_window_1, mini_window,
13678 list_of_error,
13679 redisplay_window_error);
13680
13681 /* Compare desired and current matrices, perform output. */
13682
13683 update:
13684 /* If fonts changed, display again. */
13685 if (sf->fonts_changed)
13686 goto retry;
13687
13688 /* Prevent various kinds of signals during display update.
13689 stdio is not robust about handling signals,
13690 which can cause an apparent I/O error. */
13691 if (interrupt_input)
13692 unrequest_sigio ();
13693 STOP_POLLING;
13694
13695 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13696 {
13697 if (hscroll_windows (selected_window))
13698 goto retry;
13699
13700 XWINDOW (selected_window)->must_be_updated_p = true;
13701 pending = update_frame (sf, 0, 0);
13702 sf->cursor_type_changed = 0;
13703 }
13704
13705 /* We may have called echo_area_display at the top of this
13706 function. If the echo area is on another frame, that may
13707 have put text on a frame other than the selected one, so the
13708 above call to update_frame would not have caught it. Catch
13709 it here. */
13710 mini_window = FRAME_MINIBUF_WINDOW (sf);
13711 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13712
13713 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13714 {
13715 XWINDOW (mini_window)->must_be_updated_p = true;
13716 pending |= update_frame (mini_frame, 0, 0);
13717 mini_frame->cursor_type_changed = 0;
13718 if (!pending && hscroll_windows (mini_window))
13719 goto retry;
13720 }
13721 }
13722
13723 /* If display was paused because of pending input, make sure we do a
13724 thorough update the next time. */
13725 if (pending)
13726 {
13727 /* Prevent the optimization at the beginning of
13728 redisplay_internal that tries a single-line update of the
13729 line containing the cursor in the selected window. */
13730 CHARPOS (this_line_start_pos) = 0;
13731
13732 /* Let the overlay arrow be updated the next time. */
13733 update_overlay_arrows (0);
13734
13735 /* If we pause after scrolling, some rows in the current
13736 matrices of some windows are not valid. */
13737 if (!WINDOW_FULL_WIDTH_P (w)
13738 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13739 update_mode_lines = 36;
13740 }
13741 else
13742 {
13743 if (!consider_all_windows_p)
13744 {
13745 /* This has already been done above if
13746 consider_all_windows_p is set. */
13747 if (XBUFFER (w->contents)->text->redisplay
13748 && buffer_window_count (XBUFFER (w->contents)) > 1)
13749 /* This can happen if b->text->redisplay was set during
13750 jit-lock. */
13751 propagate_buffer_redisplay ();
13752 mark_window_display_accurate_1 (w, 1);
13753
13754 /* Say overlay arrows are up to date. */
13755 update_overlay_arrows (1);
13756
13757 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13758 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13759 }
13760
13761 update_mode_lines = 0;
13762 windows_or_buffers_changed = 0;
13763 }
13764
13765 /* Start SIGIO interrupts coming again. Having them off during the
13766 code above makes it less likely one will discard output, but not
13767 impossible, since there might be stuff in the system buffer here.
13768 But it is much hairier to try to do anything about that. */
13769 if (interrupt_input)
13770 request_sigio ();
13771 RESUME_POLLING;
13772
13773 /* If a frame has become visible which was not before, redisplay
13774 again, so that we display it. Expose events for such a frame
13775 (which it gets when becoming visible) don't call the parts of
13776 redisplay constructing glyphs, so simply exposing a frame won't
13777 display anything in this case. So, we have to display these
13778 frames here explicitly. */
13779 if (!pending)
13780 {
13781 int new_count = 0;
13782
13783 FOR_EACH_FRAME (tail, frame)
13784 {
13785 if (XFRAME (frame)->visible)
13786 new_count++;
13787 }
13788
13789 if (new_count != number_of_visible_frames)
13790 windows_or_buffers_changed = 52;
13791 }
13792
13793 /* Change frame size now if a change is pending. */
13794 do_pending_window_change (1);
13795
13796 /* If we just did a pending size change, or have additional
13797 visible frames, or selected_window changed, redisplay again. */
13798 if ((windows_or_buffers_changed && !pending)
13799 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13800 goto retry;
13801
13802 /* Clear the face and image caches.
13803
13804 We used to do this only if consider_all_windows_p. But the cache
13805 needs to be cleared if a timer creates images in the current
13806 buffer (e.g. the test case in Bug#6230). */
13807
13808 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13809 {
13810 clear_face_cache (0);
13811 clear_face_cache_count = 0;
13812 }
13813
13814 #ifdef HAVE_WINDOW_SYSTEM
13815 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13816 {
13817 clear_image_caches (Qnil);
13818 clear_image_cache_count = 0;
13819 }
13820 #endif /* HAVE_WINDOW_SYSTEM */
13821
13822 end_of_redisplay:
13823 if (interrupt_input && interrupts_deferred)
13824 request_sigio ();
13825
13826 unbind_to (count, Qnil);
13827 RESUME_POLLING;
13828 }
13829
13830
13831 /* Redisplay, but leave alone any recent echo area message unless
13832 another message has been requested in its place.
13833
13834 This is useful in situations where you need to redisplay but no
13835 user action has occurred, making it inappropriate for the message
13836 area to be cleared. See tracking_off and
13837 wait_reading_process_output for examples of these situations.
13838
13839 FROM_WHERE is an integer saying from where this function was
13840 called. This is useful for debugging. */
13841
13842 void
13843 redisplay_preserve_echo_area (int from_where)
13844 {
13845 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13846
13847 if (!NILP (echo_area_buffer[1]))
13848 {
13849 /* We have a previously displayed message, but no current
13850 message. Redisplay the previous message. */
13851 display_last_displayed_message_p = 1;
13852 redisplay_internal ();
13853 display_last_displayed_message_p = 0;
13854 }
13855 else
13856 redisplay_internal ();
13857
13858 flush_frame (SELECTED_FRAME ());
13859 }
13860
13861
13862 /* Function registered with record_unwind_protect in redisplay_internal. */
13863
13864 static void
13865 unwind_redisplay (void)
13866 {
13867 redisplaying_p = 0;
13868 }
13869
13870
13871 /* Mark the display of leaf window W as accurate or inaccurate.
13872 If ACCURATE_P is non-zero mark display of W as accurate. If
13873 ACCURATE_P is zero, arrange for W to be redisplayed the next
13874 time redisplay_internal is called. */
13875
13876 static void
13877 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13878 {
13879 struct buffer *b = XBUFFER (w->contents);
13880
13881 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13882 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13883 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13884
13885 if (accurate_p)
13886 {
13887 b->clip_changed = false;
13888 b->prevent_redisplay_optimizations_p = false;
13889 eassert (buffer_window_count (b) > 0);
13890 /* Resetting b->text->redisplay is problematic!
13891 In order to make it safer to do it here, redisplay_internal must
13892 have copied all b->text->redisplay to their respective windows. */
13893 b->text->redisplay = false;
13894
13895 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13896 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13897 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13898 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13899
13900 w->current_matrix->buffer = b;
13901 w->current_matrix->begv = BUF_BEGV (b);
13902 w->current_matrix->zv = BUF_ZV (b);
13903
13904 w->last_cursor_vpos = w->cursor.vpos;
13905 w->last_cursor_off_p = w->cursor_off_p;
13906
13907 if (w == XWINDOW (selected_window))
13908 w->last_point = BUF_PT (b);
13909 else
13910 w->last_point = marker_position (w->pointm);
13911
13912 w->window_end_valid = true;
13913 w->update_mode_line = false;
13914 }
13915
13916 w->redisplay = !accurate_p;
13917 }
13918
13919
13920 /* Mark the display of windows in the window tree rooted at WINDOW as
13921 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13922 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13923 be redisplayed the next time redisplay_internal is called. */
13924
13925 void
13926 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13927 {
13928 struct window *w;
13929
13930 for (; !NILP (window); window = w->next)
13931 {
13932 w = XWINDOW (window);
13933 if (WINDOWP (w->contents))
13934 mark_window_display_accurate (w->contents, accurate_p);
13935 else
13936 mark_window_display_accurate_1 (w, accurate_p);
13937 }
13938
13939 if (accurate_p)
13940 update_overlay_arrows (1);
13941 else
13942 /* Force a thorough redisplay the next time by setting
13943 last_arrow_position and last_arrow_string to t, which is
13944 unequal to any useful value of Voverlay_arrow_... */
13945 update_overlay_arrows (-1);
13946 }
13947
13948
13949 /* Return value in display table DP (Lisp_Char_Table *) for character
13950 C. Since a display table doesn't have any parent, we don't have to
13951 follow parent. Do not call this function directly but use the
13952 macro DISP_CHAR_VECTOR. */
13953
13954 Lisp_Object
13955 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13956 {
13957 Lisp_Object val;
13958
13959 if (ASCII_CHAR_P (c))
13960 {
13961 val = dp->ascii;
13962 if (SUB_CHAR_TABLE_P (val))
13963 val = XSUB_CHAR_TABLE (val)->contents[c];
13964 }
13965 else
13966 {
13967 Lisp_Object table;
13968
13969 XSETCHAR_TABLE (table, dp);
13970 val = char_table_ref (table, c);
13971 }
13972 if (NILP (val))
13973 val = dp->defalt;
13974 return val;
13975 }
13976
13977
13978 \f
13979 /***********************************************************************
13980 Window Redisplay
13981 ***********************************************************************/
13982
13983 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13984
13985 static void
13986 redisplay_windows (Lisp_Object window)
13987 {
13988 while (!NILP (window))
13989 {
13990 struct window *w = XWINDOW (window);
13991
13992 if (WINDOWP (w->contents))
13993 redisplay_windows (w->contents);
13994 else if (BUFFERP (w->contents))
13995 {
13996 displayed_buffer = XBUFFER (w->contents);
13997 /* Use list_of_error, not Qerror, so that
13998 we catch only errors and don't run the debugger. */
13999 internal_condition_case_1 (redisplay_window_0, window,
14000 list_of_error,
14001 redisplay_window_error);
14002 }
14003
14004 window = w->next;
14005 }
14006 }
14007
14008 static Lisp_Object
14009 redisplay_window_error (Lisp_Object ignore)
14010 {
14011 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14012 return Qnil;
14013 }
14014
14015 static Lisp_Object
14016 redisplay_window_0 (Lisp_Object window)
14017 {
14018 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14019 redisplay_window (window, false);
14020 return Qnil;
14021 }
14022
14023 static Lisp_Object
14024 redisplay_window_1 (Lisp_Object window)
14025 {
14026 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14027 redisplay_window (window, true);
14028 return Qnil;
14029 }
14030 \f
14031
14032 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14033 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14034 which positions recorded in ROW differ from current buffer
14035 positions.
14036
14037 Return 0 if cursor is not on this row, 1 otherwise. */
14038
14039 static int
14040 set_cursor_from_row (struct window *w, struct glyph_row *row,
14041 struct glyph_matrix *matrix,
14042 ptrdiff_t delta, ptrdiff_t delta_bytes,
14043 int dy, int dvpos)
14044 {
14045 struct glyph *glyph = row->glyphs[TEXT_AREA];
14046 struct glyph *end = glyph + row->used[TEXT_AREA];
14047 struct glyph *cursor = NULL;
14048 /* The last known character position in row. */
14049 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14050 int x = row->x;
14051 ptrdiff_t pt_old = PT - delta;
14052 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14053 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14054 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14055 /* A glyph beyond the edge of TEXT_AREA which we should never
14056 touch. */
14057 struct glyph *glyphs_end = end;
14058 /* Non-zero means we've found a match for cursor position, but that
14059 glyph has the avoid_cursor_p flag set. */
14060 int match_with_avoid_cursor = 0;
14061 /* Non-zero means we've seen at least one glyph that came from a
14062 display string. */
14063 int string_seen = 0;
14064 /* Largest and smallest buffer positions seen so far during scan of
14065 glyph row. */
14066 ptrdiff_t bpos_max = pos_before;
14067 ptrdiff_t bpos_min = pos_after;
14068 /* Last buffer position covered by an overlay string with an integer
14069 `cursor' property. */
14070 ptrdiff_t bpos_covered = 0;
14071 /* Non-zero means the display string on which to display the cursor
14072 comes from a text property, not from an overlay. */
14073 int string_from_text_prop = 0;
14074
14075 /* Don't even try doing anything if called for a mode-line or
14076 header-line row, since the rest of the code isn't prepared to
14077 deal with such calamities. */
14078 eassert (!row->mode_line_p);
14079 if (row->mode_line_p)
14080 return 0;
14081
14082 /* Skip over glyphs not having an object at the start and the end of
14083 the row. These are special glyphs like truncation marks on
14084 terminal frames. */
14085 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14086 {
14087 if (!row->reversed_p)
14088 {
14089 while (glyph < end
14090 && INTEGERP (glyph->object)
14091 && glyph->charpos < 0)
14092 {
14093 x += glyph->pixel_width;
14094 ++glyph;
14095 }
14096 while (end > glyph
14097 && INTEGERP ((end - 1)->object)
14098 /* CHARPOS is zero for blanks and stretch glyphs
14099 inserted by extend_face_to_end_of_line. */
14100 && (end - 1)->charpos <= 0)
14101 --end;
14102 glyph_before = glyph - 1;
14103 glyph_after = end;
14104 }
14105 else
14106 {
14107 struct glyph *g;
14108
14109 /* If the glyph row is reversed, we need to process it from back
14110 to front, so swap the edge pointers. */
14111 glyphs_end = end = glyph - 1;
14112 glyph += row->used[TEXT_AREA] - 1;
14113
14114 while (glyph > end + 1
14115 && INTEGERP (glyph->object)
14116 && glyph->charpos < 0)
14117 {
14118 --glyph;
14119 x -= glyph->pixel_width;
14120 }
14121 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14122 --glyph;
14123 /* By default, in reversed rows we put the cursor on the
14124 rightmost (first in the reading order) glyph. */
14125 for (g = end + 1; g < glyph; g++)
14126 x += g->pixel_width;
14127 while (end < glyph
14128 && INTEGERP ((end + 1)->object)
14129 && (end + 1)->charpos <= 0)
14130 ++end;
14131 glyph_before = glyph + 1;
14132 glyph_after = end;
14133 }
14134 }
14135 else if (row->reversed_p)
14136 {
14137 /* In R2L rows that don't display text, put the cursor on the
14138 rightmost glyph. Case in point: an empty last line that is
14139 part of an R2L paragraph. */
14140 cursor = end - 1;
14141 /* Avoid placing the cursor on the last glyph of the row, where
14142 on terminal frames we hold the vertical border between
14143 adjacent windows. */
14144 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14145 && !WINDOW_RIGHTMOST_P (w)
14146 && cursor == row->glyphs[LAST_AREA] - 1)
14147 cursor--;
14148 x = -1; /* will be computed below, at label compute_x */
14149 }
14150
14151 /* Step 1: Try to find the glyph whose character position
14152 corresponds to point. If that's not possible, find 2 glyphs
14153 whose character positions are the closest to point, one before
14154 point, the other after it. */
14155 if (!row->reversed_p)
14156 while (/* not marched to end of glyph row */
14157 glyph < end
14158 /* glyph was not inserted by redisplay for internal purposes */
14159 && !INTEGERP (glyph->object))
14160 {
14161 if (BUFFERP (glyph->object))
14162 {
14163 ptrdiff_t dpos = glyph->charpos - pt_old;
14164
14165 if (glyph->charpos > bpos_max)
14166 bpos_max = glyph->charpos;
14167 if (glyph->charpos < bpos_min)
14168 bpos_min = glyph->charpos;
14169 if (!glyph->avoid_cursor_p)
14170 {
14171 /* If we hit point, we've found the glyph on which to
14172 display the cursor. */
14173 if (dpos == 0)
14174 {
14175 match_with_avoid_cursor = 0;
14176 break;
14177 }
14178 /* See if we've found a better approximation to
14179 POS_BEFORE or to POS_AFTER. */
14180 if (0 > dpos && dpos > pos_before - pt_old)
14181 {
14182 pos_before = glyph->charpos;
14183 glyph_before = glyph;
14184 }
14185 else if (0 < dpos && dpos < pos_after - pt_old)
14186 {
14187 pos_after = glyph->charpos;
14188 glyph_after = glyph;
14189 }
14190 }
14191 else if (dpos == 0)
14192 match_with_avoid_cursor = 1;
14193 }
14194 else if (STRINGP (glyph->object))
14195 {
14196 Lisp_Object chprop;
14197 ptrdiff_t glyph_pos = glyph->charpos;
14198
14199 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14200 glyph->object);
14201 if (!NILP (chprop))
14202 {
14203 /* If the string came from a `display' text property,
14204 look up the buffer position of that property and
14205 use that position to update bpos_max, as if we
14206 actually saw such a position in one of the row's
14207 glyphs. This helps with supporting integer values
14208 of `cursor' property on the display string in
14209 situations where most or all of the row's buffer
14210 text is completely covered by display properties,
14211 so that no glyph with valid buffer positions is
14212 ever seen in the row. */
14213 ptrdiff_t prop_pos =
14214 string_buffer_position_lim (glyph->object, pos_before,
14215 pos_after, 0);
14216
14217 if (prop_pos >= pos_before)
14218 bpos_max = prop_pos - 1;
14219 }
14220 if (INTEGERP (chprop))
14221 {
14222 bpos_covered = bpos_max + XINT (chprop);
14223 /* If the `cursor' property covers buffer positions up
14224 to and including point, we should display cursor on
14225 this glyph. Note that, if a `cursor' property on one
14226 of the string's characters has an integer value, we
14227 will break out of the loop below _before_ we get to
14228 the position match above. IOW, integer values of
14229 the `cursor' property override the "exact match for
14230 point" strategy of positioning the cursor. */
14231 /* Implementation note: bpos_max == pt_old when, e.g.,
14232 we are in an empty line, where bpos_max is set to
14233 MATRIX_ROW_START_CHARPOS, see above. */
14234 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14235 {
14236 cursor = glyph;
14237 break;
14238 }
14239 }
14240
14241 string_seen = 1;
14242 }
14243 x += glyph->pixel_width;
14244 ++glyph;
14245 }
14246 else if (glyph > end) /* row is reversed */
14247 while (!INTEGERP (glyph->object))
14248 {
14249 if (BUFFERP (glyph->object))
14250 {
14251 ptrdiff_t dpos = glyph->charpos - pt_old;
14252
14253 if (glyph->charpos > bpos_max)
14254 bpos_max = glyph->charpos;
14255 if (glyph->charpos < bpos_min)
14256 bpos_min = glyph->charpos;
14257 if (!glyph->avoid_cursor_p)
14258 {
14259 if (dpos == 0)
14260 {
14261 match_with_avoid_cursor = 0;
14262 break;
14263 }
14264 if (0 > dpos && dpos > pos_before - pt_old)
14265 {
14266 pos_before = glyph->charpos;
14267 glyph_before = glyph;
14268 }
14269 else if (0 < dpos && dpos < pos_after - pt_old)
14270 {
14271 pos_after = glyph->charpos;
14272 glyph_after = glyph;
14273 }
14274 }
14275 else if (dpos == 0)
14276 match_with_avoid_cursor = 1;
14277 }
14278 else if (STRINGP (glyph->object))
14279 {
14280 Lisp_Object chprop;
14281 ptrdiff_t glyph_pos = glyph->charpos;
14282
14283 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14284 glyph->object);
14285 if (!NILP (chprop))
14286 {
14287 ptrdiff_t prop_pos =
14288 string_buffer_position_lim (glyph->object, pos_before,
14289 pos_after, 0);
14290
14291 if (prop_pos >= pos_before)
14292 bpos_max = prop_pos - 1;
14293 }
14294 if (INTEGERP (chprop))
14295 {
14296 bpos_covered = bpos_max + XINT (chprop);
14297 /* If the `cursor' property covers buffer positions up
14298 to and including point, we should display cursor on
14299 this glyph. */
14300 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14301 {
14302 cursor = glyph;
14303 break;
14304 }
14305 }
14306 string_seen = 1;
14307 }
14308 --glyph;
14309 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14310 {
14311 x--; /* can't use any pixel_width */
14312 break;
14313 }
14314 x -= glyph->pixel_width;
14315 }
14316
14317 /* Step 2: If we didn't find an exact match for point, we need to
14318 look for a proper place to put the cursor among glyphs between
14319 GLYPH_BEFORE and GLYPH_AFTER. */
14320 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14321 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14322 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14323 {
14324 /* An empty line has a single glyph whose OBJECT is zero and
14325 whose CHARPOS is the position of a newline on that line.
14326 Note that on a TTY, there are more glyphs after that, which
14327 were produced by extend_face_to_end_of_line, but their
14328 CHARPOS is zero or negative. */
14329 int empty_line_p =
14330 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14331 && INTEGERP (glyph->object) && glyph->charpos > 0
14332 /* On a TTY, continued and truncated rows also have a glyph at
14333 their end whose OBJECT is zero and whose CHARPOS is
14334 positive (the continuation and truncation glyphs), but such
14335 rows are obviously not "empty". */
14336 && !(row->continued_p || row->truncated_on_right_p);
14337
14338 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14339 {
14340 ptrdiff_t ellipsis_pos;
14341
14342 /* Scan back over the ellipsis glyphs. */
14343 if (!row->reversed_p)
14344 {
14345 ellipsis_pos = (glyph - 1)->charpos;
14346 while (glyph > row->glyphs[TEXT_AREA]
14347 && (glyph - 1)->charpos == ellipsis_pos)
14348 glyph--, x -= glyph->pixel_width;
14349 /* That loop always goes one position too far, including
14350 the glyph before the ellipsis. So scan forward over
14351 that one. */
14352 x += glyph->pixel_width;
14353 glyph++;
14354 }
14355 else /* row is reversed */
14356 {
14357 ellipsis_pos = (glyph + 1)->charpos;
14358 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14359 && (glyph + 1)->charpos == ellipsis_pos)
14360 glyph++, x += glyph->pixel_width;
14361 x -= glyph->pixel_width;
14362 glyph--;
14363 }
14364 }
14365 else if (match_with_avoid_cursor)
14366 {
14367 cursor = glyph_after;
14368 x = -1;
14369 }
14370 else if (string_seen)
14371 {
14372 int incr = row->reversed_p ? -1 : +1;
14373
14374 /* Need to find the glyph that came out of a string which is
14375 present at point. That glyph is somewhere between
14376 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14377 positioned between POS_BEFORE and POS_AFTER in the
14378 buffer. */
14379 struct glyph *start, *stop;
14380 ptrdiff_t pos = pos_before;
14381
14382 x = -1;
14383
14384 /* If the row ends in a newline from a display string,
14385 reordering could have moved the glyphs belonging to the
14386 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14387 in this case we extend the search to the last glyph in
14388 the row that was not inserted by redisplay. */
14389 if (row->ends_in_newline_from_string_p)
14390 {
14391 glyph_after = end;
14392 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14393 }
14394
14395 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14396 correspond to POS_BEFORE and POS_AFTER, respectively. We
14397 need START and STOP in the order that corresponds to the
14398 row's direction as given by its reversed_p flag. If the
14399 directionality of characters between POS_BEFORE and
14400 POS_AFTER is the opposite of the row's base direction,
14401 these characters will have been reordered for display,
14402 and we need to reverse START and STOP. */
14403 if (!row->reversed_p)
14404 {
14405 start = min (glyph_before, glyph_after);
14406 stop = max (glyph_before, glyph_after);
14407 }
14408 else
14409 {
14410 start = max (glyph_before, glyph_after);
14411 stop = min (glyph_before, glyph_after);
14412 }
14413 for (glyph = start + incr;
14414 row->reversed_p ? glyph > stop : glyph < stop; )
14415 {
14416
14417 /* Any glyphs that come from the buffer are here because
14418 of bidi reordering. Skip them, and only pay
14419 attention to glyphs that came from some string. */
14420 if (STRINGP (glyph->object))
14421 {
14422 Lisp_Object str;
14423 ptrdiff_t tem;
14424 /* If the display property covers the newline, we
14425 need to search for it one position farther. */
14426 ptrdiff_t lim = pos_after
14427 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14428
14429 string_from_text_prop = 0;
14430 str = glyph->object;
14431 tem = string_buffer_position_lim (str, pos, lim, 0);
14432 if (tem == 0 /* from overlay */
14433 || pos <= tem)
14434 {
14435 /* If the string from which this glyph came is
14436 found in the buffer at point, or at position
14437 that is closer to point than pos_after, then
14438 we've found the glyph we've been looking for.
14439 If it comes from an overlay (tem == 0), and
14440 it has the `cursor' property on one of its
14441 glyphs, record that glyph as a candidate for
14442 displaying the cursor. (As in the
14443 unidirectional version, we will display the
14444 cursor on the last candidate we find.) */
14445 if (tem == 0
14446 || tem == pt_old
14447 || (tem - pt_old > 0 && tem < pos_after))
14448 {
14449 /* The glyphs from this string could have
14450 been reordered. Find the one with the
14451 smallest string position. Or there could
14452 be a character in the string with the
14453 `cursor' property, which means display
14454 cursor on that character's glyph. */
14455 ptrdiff_t strpos = glyph->charpos;
14456
14457 if (tem)
14458 {
14459 cursor = glyph;
14460 string_from_text_prop = 1;
14461 }
14462 for ( ;
14463 (row->reversed_p ? glyph > stop : glyph < stop)
14464 && EQ (glyph->object, str);
14465 glyph += incr)
14466 {
14467 Lisp_Object cprop;
14468 ptrdiff_t gpos = glyph->charpos;
14469
14470 cprop = Fget_char_property (make_number (gpos),
14471 Qcursor,
14472 glyph->object);
14473 if (!NILP (cprop))
14474 {
14475 cursor = glyph;
14476 break;
14477 }
14478 if (tem && glyph->charpos < strpos)
14479 {
14480 strpos = glyph->charpos;
14481 cursor = glyph;
14482 }
14483 }
14484
14485 if (tem == pt_old
14486 || (tem - pt_old > 0 && tem < pos_after))
14487 goto compute_x;
14488 }
14489 if (tem)
14490 pos = tem + 1; /* don't find previous instances */
14491 }
14492 /* This string is not what we want; skip all of the
14493 glyphs that came from it. */
14494 while ((row->reversed_p ? glyph > stop : glyph < stop)
14495 && EQ (glyph->object, str))
14496 glyph += incr;
14497 }
14498 else
14499 glyph += incr;
14500 }
14501
14502 /* If we reached the end of the line, and END was from a string,
14503 the cursor is not on this line. */
14504 if (cursor == NULL
14505 && (row->reversed_p ? glyph <= end : glyph >= end)
14506 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14507 && STRINGP (end->object)
14508 && row->continued_p)
14509 return 0;
14510 }
14511 /* A truncated row may not include PT among its character positions.
14512 Setting the cursor inside the scroll margin will trigger
14513 recalculation of hscroll in hscroll_window_tree. But if a
14514 display string covers point, defer to the string-handling
14515 code below to figure this out. */
14516 else if (row->truncated_on_left_p && pt_old < bpos_min)
14517 {
14518 cursor = glyph_before;
14519 x = -1;
14520 }
14521 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14522 /* Zero-width characters produce no glyphs. */
14523 || (!empty_line_p
14524 && (row->reversed_p
14525 ? glyph_after > glyphs_end
14526 : glyph_after < glyphs_end)))
14527 {
14528 cursor = glyph_after;
14529 x = -1;
14530 }
14531 }
14532
14533 compute_x:
14534 if (cursor != NULL)
14535 glyph = cursor;
14536 else if (glyph == glyphs_end
14537 && pos_before == pos_after
14538 && STRINGP ((row->reversed_p
14539 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14540 : row->glyphs[TEXT_AREA])->object))
14541 {
14542 /* If all the glyphs of this row came from strings, put the
14543 cursor on the first glyph of the row. This avoids having the
14544 cursor outside of the text area in this very rare and hard
14545 use case. */
14546 glyph =
14547 row->reversed_p
14548 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14549 : row->glyphs[TEXT_AREA];
14550 }
14551 if (x < 0)
14552 {
14553 struct glyph *g;
14554
14555 /* Need to compute x that corresponds to GLYPH. */
14556 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14557 {
14558 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14559 emacs_abort ();
14560 x += g->pixel_width;
14561 }
14562 }
14563
14564 /* ROW could be part of a continued line, which, under bidi
14565 reordering, might have other rows whose start and end charpos
14566 occlude point. Only set w->cursor if we found a better
14567 approximation to the cursor position than we have from previously
14568 examined candidate rows belonging to the same continued line. */
14569 if (/* We already have a candidate row. */
14570 w->cursor.vpos >= 0
14571 /* That candidate is not the row we are processing. */
14572 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14573 /* Make sure cursor.vpos specifies a row whose start and end
14574 charpos occlude point, and it is valid candidate for being a
14575 cursor-row. This is because some callers of this function
14576 leave cursor.vpos at the row where the cursor was displayed
14577 during the last redisplay cycle. */
14578 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14579 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14580 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14581 {
14582 struct glyph *g1
14583 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14584
14585 /* Don't consider glyphs that are outside TEXT_AREA. */
14586 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14587 return 0;
14588 /* Keep the candidate whose buffer position is the closest to
14589 point or has the `cursor' property. */
14590 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14591 w->cursor.hpos >= 0
14592 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14593 && ((BUFFERP (g1->object)
14594 && (g1->charpos == pt_old /* An exact match always wins. */
14595 || (BUFFERP (glyph->object)
14596 && eabs (g1->charpos - pt_old)
14597 < eabs (glyph->charpos - pt_old))))
14598 /* Previous candidate is a glyph from a string that has
14599 a non-nil `cursor' property. */
14600 || (STRINGP (g1->object)
14601 && (!NILP (Fget_char_property (make_number (g1->charpos),
14602 Qcursor, g1->object))
14603 /* Previous candidate is from the same display
14604 string as this one, and the display string
14605 came from a text property. */
14606 || (EQ (g1->object, glyph->object)
14607 && string_from_text_prop)
14608 /* this candidate is from newline and its
14609 position is not an exact match */
14610 || (INTEGERP (glyph->object)
14611 && glyph->charpos != pt_old)))))
14612 return 0;
14613 /* If this candidate gives an exact match, use that. */
14614 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14615 /* If this candidate is a glyph created for the
14616 terminating newline of a line, and point is on that
14617 newline, it wins because it's an exact match. */
14618 || (!row->continued_p
14619 && INTEGERP (glyph->object)
14620 && glyph->charpos == 0
14621 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14622 /* Otherwise, keep the candidate that comes from a row
14623 spanning less buffer positions. This may win when one or
14624 both candidate positions are on glyphs that came from
14625 display strings, for which we cannot compare buffer
14626 positions. */
14627 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14628 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14629 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14630 return 0;
14631 }
14632 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14633 w->cursor.x = x;
14634 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14635 w->cursor.y = row->y + dy;
14636
14637 if (w == XWINDOW (selected_window))
14638 {
14639 if (!row->continued_p
14640 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14641 && row->x == 0)
14642 {
14643 this_line_buffer = XBUFFER (w->contents);
14644
14645 CHARPOS (this_line_start_pos)
14646 = MATRIX_ROW_START_CHARPOS (row) + delta;
14647 BYTEPOS (this_line_start_pos)
14648 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14649
14650 CHARPOS (this_line_end_pos)
14651 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14652 BYTEPOS (this_line_end_pos)
14653 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14654
14655 this_line_y = w->cursor.y;
14656 this_line_pixel_height = row->height;
14657 this_line_vpos = w->cursor.vpos;
14658 this_line_start_x = row->x;
14659 }
14660 else
14661 CHARPOS (this_line_start_pos) = 0;
14662 }
14663
14664 return 1;
14665 }
14666
14667
14668 /* Run window scroll functions, if any, for WINDOW with new window
14669 start STARTP. Sets the window start of WINDOW to that position.
14670
14671 We assume that the window's buffer is really current. */
14672
14673 static struct text_pos
14674 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14675 {
14676 struct window *w = XWINDOW (window);
14677 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14678
14679 eassert (current_buffer == XBUFFER (w->contents));
14680
14681 if (!NILP (Vwindow_scroll_functions))
14682 {
14683 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14684 make_number (CHARPOS (startp)));
14685 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14686 /* In case the hook functions switch buffers. */
14687 set_buffer_internal (XBUFFER (w->contents));
14688 }
14689
14690 return startp;
14691 }
14692
14693
14694 /* Make sure the line containing the cursor is fully visible.
14695 A value of 1 means there is nothing to be done.
14696 (Either the line is fully visible, or it cannot be made so,
14697 or we cannot tell.)
14698
14699 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14700 is higher than window.
14701
14702 A value of 0 means the caller should do scrolling
14703 as if point had gone off the screen. */
14704
14705 static int
14706 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14707 {
14708 struct glyph_matrix *matrix;
14709 struct glyph_row *row;
14710 int window_height;
14711
14712 if (!make_cursor_line_fully_visible_p)
14713 return 1;
14714
14715 /* It's not always possible to find the cursor, e.g, when a window
14716 is full of overlay strings. Don't do anything in that case. */
14717 if (w->cursor.vpos < 0)
14718 return 1;
14719
14720 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14721 row = MATRIX_ROW (matrix, w->cursor.vpos);
14722
14723 /* If the cursor row is not partially visible, there's nothing to do. */
14724 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14725 return 1;
14726
14727 /* If the row the cursor is in is taller than the window's height,
14728 it's not clear what to do, so do nothing. */
14729 window_height = window_box_height (w);
14730 if (row->height >= window_height)
14731 {
14732 if (!force_p || MINI_WINDOW_P (w)
14733 || w->vscroll || w->cursor.vpos == 0)
14734 return 1;
14735 }
14736 return 0;
14737 }
14738
14739
14740 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14741 non-zero means only WINDOW is redisplayed in redisplay_internal.
14742 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14743 in redisplay_window to bring a partially visible line into view in
14744 the case that only the cursor has moved.
14745
14746 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14747 last screen line's vertical height extends past the end of the screen.
14748
14749 Value is
14750
14751 1 if scrolling succeeded
14752
14753 0 if scrolling didn't find point.
14754
14755 -1 if new fonts have been loaded so that we must interrupt
14756 redisplay, adjust glyph matrices, and try again. */
14757
14758 enum
14759 {
14760 SCROLLING_SUCCESS,
14761 SCROLLING_FAILED,
14762 SCROLLING_NEED_LARGER_MATRICES
14763 };
14764
14765 /* If scroll-conservatively is more than this, never recenter.
14766
14767 If you change this, don't forget to update the doc string of
14768 `scroll-conservatively' and the Emacs manual. */
14769 #define SCROLL_LIMIT 100
14770
14771 static int
14772 try_scrolling (Lisp_Object window, int just_this_one_p,
14773 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14774 int temp_scroll_step, int last_line_misfit)
14775 {
14776 struct window *w = XWINDOW (window);
14777 struct frame *f = XFRAME (w->frame);
14778 struct text_pos pos, startp;
14779 struct it it;
14780 int this_scroll_margin, scroll_max, rc, height;
14781 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14782 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14783 Lisp_Object aggressive;
14784 /* We will never try scrolling more than this number of lines. */
14785 int scroll_limit = SCROLL_LIMIT;
14786 int frame_line_height = default_line_pixel_height (w);
14787 int window_total_lines
14788 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14789
14790 #ifdef GLYPH_DEBUG
14791 debug_method_add (w, "try_scrolling");
14792 #endif
14793
14794 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14795
14796 /* Compute scroll margin height in pixels. We scroll when point is
14797 within this distance from the top or bottom of the window. */
14798 if (scroll_margin > 0)
14799 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14800 * frame_line_height;
14801 else
14802 this_scroll_margin = 0;
14803
14804 /* Force arg_scroll_conservatively to have a reasonable value, to
14805 avoid scrolling too far away with slow move_it_* functions. Note
14806 that the user can supply scroll-conservatively equal to
14807 `most-positive-fixnum', which can be larger than INT_MAX. */
14808 if (arg_scroll_conservatively > scroll_limit)
14809 {
14810 arg_scroll_conservatively = scroll_limit + 1;
14811 scroll_max = scroll_limit * frame_line_height;
14812 }
14813 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14814 /* Compute how much we should try to scroll maximally to bring
14815 point into view. */
14816 scroll_max = (max (scroll_step,
14817 max (arg_scroll_conservatively, temp_scroll_step))
14818 * frame_line_height);
14819 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14820 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14821 /* We're trying to scroll because of aggressive scrolling but no
14822 scroll_step is set. Choose an arbitrary one. */
14823 scroll_max = 10 * frame_line_height;
14824 else
14825 scroll_max = 0;
14826
14827 too_near_end:
14828
14829 /* Decide whether to scroll down. */
14830 if (PT > CHARPOS (startp))
14831 {
14832 int scroll_margin_y;
14833
14834 /* Compute the pixel ypos of the scroll margin, then move IT to
14835 either that ypos or PT, whichever comes first. */
14836 start_display (&it, w, startp);
14837 scroll_margin_y = it.last_visible_y - this_scroll_margin
14838 - frame_line_height * extra_scroll_margin_lines;
14839 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14840 (MOVE_TO_POS | MOVE_TO_Y));
14841
14842 if (PT > CHARPOS (it.current.pos))
14843 {
14844 int y0 = line_bottom_y (&it);
14845 /* Compute how many pixels below window bottom to stop searching
14846 for PT. This avoids costly search for PT that is far away if
14847 the user limited scrolling by a small number of lines, but
14848 always finds PT if scroll_conservatively is set to a large
14849 number, such as most-positive-fixnum. */
14850 int slack = max (scroll_max, 10 * frame_line_height);
14851 int y_to_move = it.last_visible_y + slack;
14852
14853 /* Compute the distance from the scroll margin to PT or to
14854 the scroll limit, whichever comes first. This should
14855 include the height of the cursor line, to make that line
14856 fully visible. */
14857 move_it_to (&it, PT, -1, y_to_move,
14858 -1, MOVE_TO_POS | MOVE_TO_Y);
14859 dy = line_bottom_y (&it) - y0;
14860
14861 if (dy > scroll_max)
14862 return SCROLLING_FAILED;
14863
14864 if (dy > 0)
14865 scroll_down_p = 1;
14866 }
14867 }
14868
14869 if (scroll_down_p)
14870 {
14871 /* Point is in or below the bottom scroll margin, so move the
14872 window start down. If scrolling conservatively, move it just
14873 enough down to make point visible. If scroll_step is set,
14874 move it down by scroll_step. */
14875 if (arg_scroll_conservatively)
14876 amount_to_scroll
14877 = min (max (dy, frame_line_height),
14878 frame_line_height * arg_scroll_conservatively);
14879 else if (scroll_step || temp_scroll_step)
14880 amount_to_scroll = scroll_max;
14881 else
14882 {
14883 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14884 height = WINDOW_BOX_TEXT_HEIGHT (w);
14885 if (NUMBERP (aggressive))
14886 {
14887 double float_amount = XFLOATINT (aggressive) * height;
14888 int aggressive_scroll = float_amount;
14889 if (aggressive_scroll == 0 && float_amount > 0)
14890 aggressive_scroll = 1;
14891 /* Don't let point enter the scroll margin near top of
14892 the window. This could happen if the value of
14893 scroll_up_aggressively is too large and there are
14894 non-zero margins, because scroll_up_aggressively
14895 means put point that fraction of window height
14896 _from_the_bottom_margin_. */
14897 if (aggressive_scroll + 2*this_scroll_margin > height)
14898 aggressive_scroll = height - 2*this_scroll_margin;
14899 amount_to_scroll = dy + aggressive_scroll;
14900 }
14901 }
14902
14903 if (amount_to_scroll <= 0)
14904 return SCROLLING_FAILED;
14905
14906 start_display (&it, w, startp);
14907 if (arg_scroll_conservatively <= scroll_limit)
14908 move_it_vertically (&it, amount_to_scroll);
14909 else
14910 {
14911 /* Extra precision for users who set scroll-conservatively
14912 to a large number: make sure the amount we scroll
14913 the window start is never less than amount_to_scroll,
14914 which was computed as distance from window bottom to
14915 point. This matters when lines at window top and lines
14916 below window bottom have different height. */
14917 struct it it1;
14918 void *it1data = NULL;
14919 /* We use a temporary it1 because line_bottom_y can modify
14920 its argument, if it moves one line down; see there. */
14921 int start_y;
14922
14923 SAVE_IT (it1, it, it1data);
14924 start_y = line_bottom_y (&it1);
14925 do {
14926 RESTORE_IT (&it, &it, it1data);
14927 move_it_by_lines (&it, 1);
14928 SAVE_IT (it1, it, it1data);
14929 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14930 }
14931
14932 /* If STARTP is unchanged, move it down another screen line. */
14933 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14934 move_it_by_lines (&it, 1);
14935 startp = it.current.pos;
14936 }
14937 else
14938 {
14939 struct text_pos scroll_margin_pos = startp;
14940 int y_offset = 0;
14941
14942 /* See if point is inside the scroll margin at the top of the
14943 window. */
14944 if (this_scroll_margin)
14945 {
14946 int y_start;
14947
14948 start_display (&it, w, startp);
14949 y_start = it.current_y;
14950 move_it_vertically (&it, this_scroll_margin);
14951 scroll_margin_pos = it.current.pos;
14952 /* If we didn't move enough before hitting ZV, request
14953 additional amount of scroll, to move point out of the
14954 scroll margin. */
14955 if (IT_CHARPOS (it) == ZV
14956 && it.current_y - y_start < this_scroll_margin)
14957 y_offset = this_scroll_margin - (it.current_y - y_start);
14958 }
14959
14960 if (PT < CHARPOS (scroll_margin_pos))
14961 {
14962 /* Point is in the scroll margin at the top of the window or
14963 above what is displayed in the window. */
14964 int y0, y_to_move;
14965
14966 /* Compute the vertical distance from PT to the scroll
14967 margin position. Move as far as scroll_max allows, or
14968 one screenful, or 10 screen lines, whichever is largest.
14969 Give up if distance is greater than scroll_max or if we
14970 didn't reach the scroll margin position. */
14971 SET_TEXT_POS (pos, PT, PT_BYTE);
14972 start_display (&it, w, pos);
14973 y0 = it.current_y;
14974 y_to_move = max (it.last_visible_y,
14975 max (scroll_max, 10 * frame_line_height));
14976 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14977 y_to_move, -1,
14978 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14979 dy = it.current_y - y0;
14980 if (dy > scroll_max
14981 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14982 return SCROLLING_FAILED;
14983
14984 /* Additional scroll for when ZV was too close to point. */
14985 dy += y_offset;
14986
14987 /* Compute new window start. */
14988 start_display (&it, w, startp);
14989
14990 if (arg_scroll_conservatively)
14991 amount_to_scroll = max (dy, frame_line_height *
14992 max (scroll_step, temp_scroll_step));
14993 else if (scroll_step || temp_scroll_step)
14994 amount_to_scroll = scroll_max;
14995 else
14996 {
14997 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14998 height = WINDOW_BOX_TEXT_HEIGHT (w);
14999 if (NUMBERP (aggressive))
15000 {
15001 double float_amount = XFLOATINT (aggressive) * height;
15002 int aggressive_scroll = float_amount;
15003 if (aggressive_scroll == 0 && float_amount > 0)
15004 aggressive_scroll = 1;
15005 /* Don't let point enter the scroll margin near
15006 bottom of the window, if the value of
15007 scroll_down_aggressively happens to be too
15008 large. */
15009 if (aggressive_scroll + 2*this_scroll_margin > height)
15010 aggressive_scroll = height - 2*this_scroll_margin;
15011 amount_to_scroll = dy + aggressive_scroll;
15012 }
15013 }
15014
15015 if (amount_to_scroll <= 0)
15016 return SCROLLING_FAILED;
15017
15018 move_it_vertically_backward (&it, amount_to_scroll);
15019 startp = it.current.pos;
15020 }
15021 }
15022
15023 /* Run window scroll functions. */
15024 startp = run_window_scroll_functions (window, startp);
15025
15026 /* Display the window. Give up if new fonts are loaded, or if point
15027 doesn't appear. */
15028 if (!try_window (window, startp, 0))
15029 rc = SCROLLING_NEED_LARGER_MATRICES;
15030 else if (w->cursor.vpos < 0)
15031 {
15032 clear_glyph_matrix (w->desired_matrix);
15033 rc = SCROLLING_FAILED;
15034 }
15035 else
15036 {
15037 /* Maybe forget recorded base line for line number display. */
15038 if (!just_this_one_p
15039 || current_buffer->clip_changed
15040 || BEG_UNCHANGED < CHARPOS (startp))
15041 w->base_line_number = 0;
15042
15043 /* If cursor ends up on a partially visible line,
15044 treat that as being off the bottom of the screen. */
15045 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15046 /* It's possible that the cursor is on the first line of the
15047 buffer, which is partially obscured due to a vscroll
15048 (Bug#7537). In that case, avoid looping forever. */
15049 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15050 {
15051 clear_glyph_matrix (w->desired_matrix);
15052 ++extra_scroll_margin_lines;
15053 goto too_near_end;
15054 }
15055 rc = SCROLLING_SUCCESS;
15056 }
15057
15058 return rc;
15059 }
15060
15061
15062 /* Compute a suitable window start for window W if display of W starts
15063 on a continuation line. Value is non-zero if a new window start
15064 was computed.
15065
15066 The new window start will be computed, based on W's width, starting
15067 from the start of the continued line. It is the start of the
15068 screen line with the minimum distance from the old start W->start. */
15069
15070 static int
15071 compute_window_start_on_continuation_line (struct window *w)
15072 {
15073 struct text_pos pos, start_pos;
15074 int window_start_changed_p = 0;
15075
15076 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15077
15078 /* If window start is on a continuation line... Window start may be
15079 < BEGV in case there's invisible text at the start of the
15080 buffer (M-x rmail, for example). */
15081 if (CHARPOS (start_pos) > BEGV
15082 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15083 {
15084 struct it it;
15085 struct glyph_row *row;
15086
15087 /* Handle the case that the window start is out of range. */
15088 if (CHARPOS (start_pos) < BEGV)
15089 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15090 else if (CHARPOS (start_pos) > ZV)
15091 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15092
15093 /* Find the start of the continued line. This should be fast
15094 because find_newline is fast (newline cache). */
15095 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15096 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15097 row, DEFAULT_FACE_ID);
15098 reseat_at_previous_visible_line_start (&it);
15099
15100 /* If the line start is "too far" away from the window start,
15101 say it takes too much time to compute a new window start. */
15102 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15103 /* PXW: Do we need upper bounds here? */
15104 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15105 {
15106 int min_distance, distance;
15107
15108 /* Move forward by display lines to find the new window
15109 start. If window width was enlarged, the new start can
15110 be expected to be > the old start. If window width was
15111 decreased, the new window start will be < the old start.
15112 So, we're looking for the display line start with the
15113 minimum distance from the old window start. */
15114 pos = it.current.pos;
15115 min_distance = INFINITY;
15116 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15117 distance < min_distance)
15118 {
15119 min_distance = distance;
15120 pos = it.current.pos;
15121 if (it.line_wrap == WORD_WRAP)
15122 {
15123 /* Under WORD_WRAP, move_it_by_lines is likely to
15124 overshoot and stop not at the first, but the
15125 second character from the left margin. So in
15126 that case, we need a more tight control on the X
15127 coordinate of the iterator than move_it_by_lines
15128 promises in its contract. The method is to first
15129 go to the last (rightmost) visible character of a
15130 line, then move to the leftmost character on the
15131 next line in a separate call. */
15132 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15133 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15134 move_it_to (&it, ZV, 0,
15135 it.current_y + it.max_ascent + it.max_descent, -1,
15136 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15137 }
15138 else
15139 move_it_by_lines (&it, 1);
15140 }
15141
15142 /* Set the window start there. */
15143 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15144 window_start_changed_p = 1;
15145 }
15146 }
15147
15148 return window_start_changed_p;
15149 }
15150
15151
15152 /* Try cursor movement in case text has not changed in window WINDOW,
15153 with window start STARTP. Value is
15154
15155 CURSOR_MOVEMENT_SUCCESS if successful
15156
15157 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15158
15159 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15160 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15161 we want to scroll as if scroll-step were set to 1. See the code.
15162
15163 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15164 which case we have to abort this redisplay, and adjust matrices
15165 first. */
15166
15167 enum
15168 {
15169 CURSOR_MOVEMENT_SUCCESS,
15170 CURSOR_MOVEMENT_CANNOT_BE_USED,
15171 CURSOR_MOVEMENT_MUST_SCROLL,
15172 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15173 };
15174
15175 static int
15176 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15177 {
15178 struct window *w = XWINDOW (window);
15179 struct frame *f = XFRAME (w->frame);
15180 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15181
15182 #ifdef GLYPH_DEBUG
15183 if (inhibit_try_cursor_movement)
15184 return rc;
15185 #endif
15186
15187 /* Previously, there was a check for Lisp integer in the
15188 if-statement below. Now, this field is converted to
15189 ptrdiff_t, thus zero means invalid position in a buffer. */
15190 eassert (w->last_point > 0);
15191 /* Likewise there was a check whether window_end_vpos is nil or larger
15192 than the window. Now window_end_vpos is int and so never nil, but
15193 let's leave eassert to check whether it fits in the window. */
15194 eassert (w->window_end_vpos < w->current_matrix->nrows);
15195
15196 /* Handle case where text has not changed, only point, and it has
15197 not moved off the frame. */
15198 if (/* Point may be in this window. */
15199 PT >= CHARPOS (startp)
15200 /* Selective display hasn't changed. */
15201 && !current_buffer->clip_changed
15202 /* Function force-mode-line-update is used to force a thorough
15203 redisplay. It sets either windows_or_buffers_changed or
15204 update_mode_lines. So don't take a shortcut here for these
15205 cases. */
15206 && !update_mode_lines
15207 && !windows_or_buffers_changed
15208 && !f->cursor_type_changed
15209 && NILP (Vshow_trailing_whitespace)
15210 /* This code is not used for mini-buffer for the sake of the case
15211 of redisplaying to replace an echo area message; since in
15212 that case the mini-buffer contents per se are usually
15213 unchanged. This code is of no real use in the mini-buffer
15214 since the handling of this_line_start_pos, etc., in redisplay
15215 handles the same cases. */
15216 && !EQ (window, minibuf_window)
15217 && (FRAME_WINDOW_P (f)
15218 || !overlay_arrow_in_current_buffer_p ()))
15219 {
15220 int this_scroll_margin, top_scroll_margin;
15221 struct glyph_row *row = NULL;
15222 int frame_line_height = default_line_pixel_height (w);
15223 int window_total_lines
15224 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15225
15226 #ifdef GLYPH_DEBUG
15227 debug_method_add (w, "cursor movement");
15228 #endif
15229
15230 /* Scroll if point within this distance from the top or bottom
15231 of the window. This is a pixel value. */
15232 if (scroll_margin > 0)
15233 {
15234 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15235 this_scroll_margin *= frame_line_height;
15236 }
15237 else
15238 this_scroll_margin = 0;
15239
15240 top_scroll_margin = this_scroll_margin;
15241 if (WINDOW_WANTS_HEADER_LINE_P (w))
15242 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15243
15244 /* Start with the row the cursor was displayed during the last
15245 not paused redisplay. Give up if that row is not valid. */
15246 if (w->last_cursor_vpos < 0
15247 || w->last_cursor_vpos >= w->current_matrix->nrows)
15248 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15249 else
15250 {
15251 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15252 if (row->mode_line_p)
15253 ++row;
15254 if (!row->enabled_p)
15255 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15256 }
15257
15258 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15259 {
15260 int scroll_p = 0, must_scroll = 0;
15261 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15262
15263 if (PT > w->last_point)
15264 {
15265 /* Point has moved forward. */
15266 while (MATRIX_ROW_END_CHARPOS (row) < PT
15267 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15268 {
15269 eassert (row->enabled_p);
15270 ++row;
15271 }
15272
15273 /* If the end position of a row equals the start
15274 position of the next row, and PT is at that position,
15275 we would rather display cursor in the next line. */
15276 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15277 && MATRIX_ROW_END_CHARPOS (row) == PT
15278 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15279 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15280 && !cursor_row_p (row))
15281 ++row;
15282
15283 /* If within the scroll margin, scroll. Note that
15284 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15285 the next line would be drawn, and that
15286 this_scroll_margin can be zero. */
15287 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15288 || PT > MATRIX_ROW_END_CHARPOS (row)
15289 /* Line is completely visible last line in window
15290 and PT is to be set in the next line. */
15291 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15292 && PT == MATRIX_ROW_END_CHARPOS (row)
15293 && !row->ends_at_zv_p
15294 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15295 scroll_p = 1;
15296 }
15297 else if (PT < w->last_point)
15298 {
15299 /* Cursor has to be moved backward. Note that PT >=
15300 CHARPOS (startp) because of the outer if-statement. */
15301 while (!row->mode_line_p
15302 && (MATRIX_ROW_START_CHARPOS (row) > PT
15303 || (MATRIX_ROW_START_CHARPOS (row) == PT
15304 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15305 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15306 row > w->current_matrix->rows
15307 && (row-1)->ends_in_newline_from_string_p))))
15308 && (row->y > top_scroll_margin
15309 || CHARPOS (startp) == BEGV))
15310 {
15311 eassert (row->enabled_p);
15312 --row;
15313 }
15314
15315 /* Consider the following case: Window starts at BEGV,
15316 there is invisible, intangible text at BEGV, so that
15317 display starts at some point START > BEGV. It can
15318 happen that we are called with PT somewhere between
15319 BEGV and START. Try to handle that case. */
15320 if (row < w->current_matrix->rows
15321 || row->mode_line_p)
15322 {
15323 row = w->current_matrix->rows;
15324 if (row->mode_line_p)
15325 ++row;
15326 }
15327
15328 /* Due to newlines in overlay strings, we may have to
15329 skip forward over overlay strings. */
15330 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15331 && MATRIX_ROW_END_CHARPOS (row) == PT
15332 && !cursor_row_p (row))
15333 ++row;
15334
15335 /* If within the scroll margin, scroll. */
15336 if (row->y < top_scroll_margin
15337 && CHARPOS (startp) != BEGV)
15338 scroll_p = 1;
15339 }
15340 else
15341 {
15342 /* Cursor did not move. So don't scroll even if cursor line
15343 is partially visible, as it was so before. */
15344 rc = CURSOR_MOVEMENT_SUCCESS;
15345 }
15346
15347 if (PT < MATRIX_ROW_START_CHARPOS (row)
15348 || PT > MATRIX_ROW_END_CHARPOS (row))
15349 {
15350 /* if PT is not in the glyph row, give up. */
15351 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15352 must_scroll = 1;
15353 }
15354 else if (rc != CURSOR_MOVEMENT_SUCCESS
15355 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15356 {
15357 struct glyph_row *row1;
15358
15359 /* If rows are bidi-reordered and point moved, back up
15360 until we find a row that does not belong to a
15361 continuation line. This is because we must consider
15362 all rows of a continued line as candidates for the
15363 new cursor positioning, since row start and end
15364 positions change non-linearly with vertical position
15365 in such rows. */
15366 /* FIXME: Revisit this when glyph ``spilling'' in
15367 continuation lines' rows is implemented for
15368 bidi-reordered rows. */
15369 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15370 MATRIX_ROW_CONTINUATION_LINE_P (row);
15371 --row)
15372 {
15373 /* If we hit the beginning of the displayed portion
15374 without finding the first row of a continued
15375 line, give up. */
15376 if (row <= row1)
15377 {
15378 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15379 break;
15380 }
15381 eassert (row->enabled_p);
15382 }
15383 }
15384 if (must_scroll)
15385 ;
15386 else if (rc != CURSOR_MOVEMENT_SUCCESS
15387 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15388 /* Make sure this isn't a header line by any chance, since
15389 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15390 && !row->mode_line_p
15391 && make_cursor_line_fully_visible_p)
15392 {
15393 if (PT == MATRIX_ROW_END_CHARPOS (row)
15394 && !row->ends_at_zv_p
15395 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15396 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15397 else if (row->height > window_box_height (w))
15398 {
15399 /* If we end up in a partially visible line, let's
15400 make it fully visible, except when it's taller
15401 than the window, in which case we can't do much
15402 about it. */
15403 *scroll_step = 1;
15404 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15405 }
15406 else
15407 {
15408 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15409 if (!cursor_row_fully_visible_p (w, 0, 1))
15410 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15411 else
15412 rc = CURSOR_MOVEMENT_SUCCESS;
15413 }
15414 }
15415 else if (scroll_p)
15416 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15417 else if (rc != CURSOR_MOVEMENT_SUCCESS
15418 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15419 {
15420 /* With bidi-reordered rows, there could be more than
15421 one candidate row whose start and end positions
15422 occlude point. We need to let set_cursor_from_row
15423 find the best candidate. */
15424 /* FIXME: Revisit this when glyph ``spilling'' in
15425 continuation lines' rows is implemented for
15426 bidi-reordered rows. */
15427 int rv = 0;
15428
15429 do
15430 {
15431 int at_zv_p = 0, exact_match_p = 0;
15432
15433 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15434 && PT <= MATRIX_ROW_END_CHARPOS (row)
15435 && cursor_row_p (row))
15436 rv |= set_cursor_from_row (w, row, w->current_matrix,
15437 0, 0, 0, 0);
15438 /* As soon as we've found the exact match for point,
15439 or the first suitable row whose ends_at_zv_p flag
15440 is set, we are done. */
15441 at_zv_p =
15442 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15443 if (rv && !at_zv_p
15444 && w->cursor.hpos >= 0
15445 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15446 w->cursor.vpos))
15447 {
15448 struct glyph_row *candidate =
15449 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15450 struct glyph *g =
15451 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15452 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15453
15454 exact_match_p =
15455 (BUFFERP (g->object) && g->charpos == PT)
15456 || (INTEGERP (g->object)
15457 && (g->charpos == PT
15458 || (g->charpos == 0 && endpos - 1 == PT)));
15459 }
15460 if (rv && (at_zv_p || exact_match_p))
15461 {
15462 rc = CURSOR_MOVEMENT_SUCCESS;
15463 break;
15464 }
15465 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15466 break;
15467 ++row;
15468 }
15469 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15470 || row->continued_p)
15471 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15472 || (MATRIX_ROW_START_CHARPOS (row) == PT
15473 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15474 /* If we didn't find any candidate rows, or exited the
15475 loop before all the candidates were examined, signal
15476 to the caller that this method failed. */
15477 if (rc != CURSOR_MOVEMENT_SUCCESS
15478 && !(rv
15479 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15480 && !row->continued_p))
15481 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15482 else if (rv)
15483 rc = CURSOR_MOVEMENT_SUCCESS;
15484 }
15485 else
15486 {
15487 do
15488 {
15489 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15490 {
15491 rc = CURSOR_MOVEMENT_SUCCESS;
15492 break;
15493 }
15494 ++row;
15495 }
15496 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15497 && MATRIX_ROW_START_CHARPOS (row) == PT
15498 && cursor_row_p (row));
15499 }
15500 }
15501 }
15502
15503 return rc;
15504 }
15505
15506 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15507 static
15508 #endif
15509 void
15510 set_vertical_scroll_bar (struct window *w)
15511 {
15512 ptrdiff_t start, end, whole;
15513
15514 /* Calculate the start and end positions for the current window.
15515 At some point, it would be nice to choose between scrollbars
15516 which reflect the whole buffer size, with special markers
15517 indicating narrowing, and scrollbars which reflect only the
15518 visible region.
15519
15520 Note that mini-buffers sometimes aren't displaying any text. */
15521 if (!MINI_WINDOW_P (w)
15522 || (w == XWINDOW (minibuf_window)
15523 && NILP (echo_area_buffer[0])))
15524 {
15525 struct buffer *buf = XBUFFER (w->contents);
15526 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15527 start = marker_position (w->start) - BUF_BEGV (buf);
15528 /* I don't think this is guaranteed to be right. For the
15529 moment, we'll pretend it is. */
15530 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15531
15532 if (end < start)
15533 end = start;
15534 if (whole < (end - start))
15535 whole = end - start;
15536 }
15537 else
15538 start = end = whole = 0;
15539
15540 /* Indicate what this scroll bar ought to be displaying now. */
15541 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15542 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15543 (w, end - start, whole, start);
15544 }
15545
15546
15547 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15548 selected_window is redisplayed.
15549
15550 We can return without actually redisplaying the window if fonts has been
15551 changed on window's frame. In that case, redisplay_internal will retry. */
15552
15553 static void
15554 redisplay_window (Lisp_Object window, bool just_this_one_p)
15555 {
15556 struct window *w = XWINDOW (window);
15557 struct frame *f = XFRAME (w->frame);
15558 struct buffer *buffer = XBUFFER (w->contents);
15559 struct buffer *old = current_buffer;
15560 struct text_pos lpoint, opoint, startp;
15561 int update_mode_line;
15562 int tem;
15563 struct it it;
15564 /* Record it now because it's overwritten. */
15565 bool current_matrix_up_to_date_p = false;
15566 bool used_current_matrix_p = false;
15567 /* This is less strict than current_matrix_up_to_date_p.
15568 It indicates that the buffer contents and narrowing are unchanged. */
15569 bool buffer_unchanged_p = false;
15570 int temp_scroll_step = 0;
15571 ptrdiff_t count = SPECPDL_INDEX ();
15572 int rc;
15573 int centering_position = -1;
15574 int last_line_misfit = 0;
15575 ptrdiff_t beg_unchanged, end_unchanged;
15576 int frame_line_height;
15577
15578 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15579 opoint = lpoint;
15580
15581 #ifdef GLYPH_DEBUG
15582 *w->desired_matrix->method = 0;
15583 #endif
15584
15585 if (!just_this_one_p
15586 && REDISPLAY_SOME_P ()
15587 && !w->redisplay
15588 && !f->redisplay
15589 && !buffer->text->redisplay)
15590 return;
15591
15592 /* Make sure that both W's markers are valid. */
15593 eassert (XMARKER (w->start)->buffer == buffer);
15594 eassert (XMARKER (w->pointm)->buffer == buffer);
15595
15596 restart:
15597 reconsider_clip_changes (w);
15598 frame_line_height = default_line_pixel_height (w);
15599
15600 /* Has the mode line to be updated? */
15601 update_mode_line = (w->update_mode_line
15602 || update_mode_lines
15603 || buffer->clip_changed
15604 || buffer->prevent_redisplay_optimizations_p);
15605
15606 if (!just_this_one_p)
15607 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15608 cleverly elsewhere. */
15609 w->must_be_updated_p = true;
15610
15611 if (MINI_WINDOW_P (w))
15612 {
15613 if (w == XWINDOW (echo_area_window)
15614 && !NILP (echo_area_buffer[0]))
15615 {
15616 if (update_mode_line)
15617 /* We may have to update a tty frame's menu bar or a
15618 tool-bar. Example `M-x C-h C-h C-g'. */
15619 goto finish_menu_bars;
15620 else
15621 /* We've already displayed the echo area glyphs in this window. */
15622 goto finish_scroll_bars;
15623 }
15624 else if ((w != XWINDOW (minibuf_window)
15625 || minibuf_level == 0)
15626 /* When buffer is nonempty, redisplay window normally. */
15627 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15628 /* Quail displays non-mini buffers in minibuffer window.
15629 In that case, redisplay the window normally. */
15630 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15631 {
15632 /* W is a mini-buffer window, but it's not active, so clear
15633 it. */
15634 int yb = window_text_bottom_y (w);
15635 struct glyph_row *row;
15636 int y;
15637
15638 for (y = 0, row = w->desired_matrix->rows;
15639 y < yb;
15640 y += row->height, ++row)
15641 blank_row (w, row, y);
15642 goto finish_scroll_bars;
15643 }
15644
15645 clear_glyph_matrix (w->desired_matrix);
15646 }
15647
15648 /* Otherwise set up data on this window; select its buffer and point
15649 value. */
15650 /* Really select the buffer, for the sake of buffer-local
15651 variables. */
15652 set_buffer_internal_1 (XBUFFER (w->contents));
15653
15654 current_matrix_up_to_date_p
15655 = (w->window_end_valid
15656 && !current_buffer->clip_changed
15657 && !current_buffer->prevent_redisplay_optimizations_p
15658 && !window_outdated (w));
15659
15660 /* Run the window-bottom-change-functions
15661 if it is possible that the text on the screen has changed
15662 (either due to modification of the text, or any other reason). */
15663 if (!current_matrix_up_to_date_p
15664 && !NILP (Vwindow_text_change_functions))
15665 {
15666 safe_run_hooks (Qwindow_text_change_functions);
15667 goto restart;
15668 }
15669
15670 beg_unchanged = BEG_UNCHANGED;
15671 end_unchanged = END_UNCHANGED;
15672
15673 SET_TEXT_POS (opoint, PT, PT_BYTE);
15674
15675 specbind (Qinhibit_point_motion_hooks, Qt);
15676
15677 buffer_unchanged_p
15678 = (w->window_end_valid
15679 && !current_buffer->clip_changed
15680 && !window_outdated (w));
15681
15682 /* When windows_or_buffers_changed is non-zero, we can't rely
15683 on the window end being valid, so set it to zero there. */
15684 if (windows_or_buffers_changed)
15685 {
15686 /* If window starts on a continuation line, maybe adjust the
15687 window start in case the window's width changed. */
15688 if (XMARKER (w->start)->buffer == current_buffer)
15689 compute_window_start_on_continuation_line (w);
15690
15691 w->window_end_valid = false;
15692 /* If so, we also can't rely on current matrix
15693 and should not fool try_cursor_movement below. */
15694 current_matrix_up_to_date_p = false;
15695 }
15696
15697 /* Some sanity checks. */
15698 CHECK_WINDOW_END (w);
15699 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15700 emacs_abort ();
15701 if (BYTEPOS (opoint) < CHARPOS (opoint))
15702 emacs_abort ();
15703
15704 if (mode_line_update_needed (w))
15705 update_mode_line = 1;
15706
15707 /* Point refers normally to the selected window. For any other
15708 window, set up appropriate value. */
15709 if (!EQ (window, selected_window))
15710 {
15711 ptrdiff_t new_pt = marker_position (w->pointm);
15712 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15713 if (new_pt < BEGV)
15714 {
15715 new_pt = BEGV;
15716 new_pt_byte = BEGV_BYTE;
15717 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15718 }
15719 else if (new_pt > (ZV - 1))
15720 {
15721 new_pt = ZV;
15722 new_pt_byte = ZV_BYTE;
15723 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15724 }
15725
15726 /* We don't use SET_PT so that the point-motion hooks don't run. */
15727 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15728 }
15729
15730 /* If any of the character widths specified in the display table
15731 have changed, invalidate the width run cache. It's true that
15732 this may be a bit late to catch such changes, but the rest of
15733 redisplay goes (non-fatally) haywire when the display table is
15734 changed, so why should we worry about doing any better? */
15735 if (current_buffer->width_run_cache)
15736 {
15737 struct Lisp_Char_Table *disptab = buffer_display_table ();
15738
15739 if (! disptab_matches_widthtab
15740 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15741 {
15742 invalidate_region_cache (current_buffer,
15743 current_buffer->width_run_cache,
15744 BEG, Z);
15745 recompute_width_table (current_buffer, disptab);
15746 }
15747 }
15748
15749 /* If window-start is screwed up, choose a new one. */
15750 if (XMARKER (w->start)->buffer != current_buffer)
15751 goto recenter;
15752
15753 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15754
15755 /* If someone specified a new starting point but did not insist,
15756 check whether it can be used. */
15757 if (w->optional_new_start
15758 && CHARPOS (startp) >= BEGV
15759 && CHARPOS (startp) <= ZV)
15760 {
15761 w->optional_new_start = 0;
15762 start_display (&it, w, startp);
15763 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15764 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15765 if (IT_CHARPOS (it) == PT)
15766 w->force_start = 1;
15767 /* IT may overshoot PT if text at PT is invisible. */
15768 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15769 w->force_start = 1;
15770 }
15771
15772 force_start:
15773
15774 /* Handle case where place to start displaying has been specified,
15775 unless the specified location is outside the accessible range. */
15776 if (w->force_start || window_frozen_p (w))
15777 {
15778 /* We set this later on if we have to adjust point. */
15779 int new_vpos = -1;
15780
15781 w->force_start = 0;
15782 w->vscroll = 0;
15783 w->window_end_valid = 0;
15784
15785 /* Forget any recorded base line for line number display. */
15786 if (!buffer_unchanged_p)
15787 w->base_line_number = 0;
15788
15789 /* Redisplay the mode line. Select the buffer properly for that.
15790 Also, run the hook window-scroll-functions
15791 because we have scrolled. */
15792 /* Note, we do this after clearing force_start because
15793 if there's an error, it is better to forget about force_start
15794 than to get into an infinite loop calling the hook functions
15795 and having them get more errors. */
15796 if (!update_mode_line
15797 || ! NILP (Vwindow_scroll_functions))
15798 {
15799 update_mode_line = 1;
15800 w->update_mode_line = 1;
15801 startp = run_window_scroll_functions (window, startp);
15802 }
15803
15804 if (CHARPOS (startp) < BEGV)
15805 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15806 else if (CHARPOS (startp) > ZV)
15807 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15808
15809 /* Redisplay, then check if cursor has been set during the
15810 redisplay. Give up if new fonts were loaded. */
15811 /* We used to issue a CHECK_MARGINS argument to try_window here,
15812 but this causes scrolling to fail when point begins inside
15813 the scroll margin (bug#148) -- cyd */
15814 if (!try_window (window, startp, 0))
15815 {
15816 w->force_start = 1;
15817 clear_glyph_matrix (w->desired_matrix);
15818 goto need_larger_matrices;
15819 }
15820
15821 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15822 {
15823 /* If point does not appear, try to move point so it does
15824 appear. The desired matrix has been built above, so we
15825 can use it here. */
15826 new_vpos = window_box_height (w) / 2;
15827 }
15828
15829 if (!cursor_row_fully_visible_p (w, 0, 0))
15830 {
15831 /* Point does appear, but on a line partly visible at end of window.
15832 Move it back to a fully-visible line. */
15833 new_vpos = window_box_height (w);
15834 }
15835 else if (w->cursor.vpos >= 0)
15836 {
15837 /* Some people insist on not letting point enter the scroll
15838 margin, even though this part handles windows that didn't
15839 scroll at all. */
15840 int window_total_lines
15841 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15842 int margin = min (scroll_margin, window_total_lines / 4);
15843 int pixel_margin = margin * frame_line_height;
15844 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15845
15846 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15847 below, which finds the row to move point to, advances by
15848 the Y coordinate of the _next_ row, see the definition of
15849 MATRIX_ROW_BOTTOM_Y. */
15850 if (w->cursor.vpos < margin + header_line)
15851 {
15852 w->cursor.vpos = -1;
15853 clear_glyph_matrix (w->desired_matrix);
15854 goto try_to_scroll;
15855 }
15856 else
15857 {
15858 int window_height = window_box_height (w);
15859
15860 if (header_line)
15861 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15862 if (w->cursor.y >= window_height - pixel_margin)
15863 {
15864 w->cursor.vpos = -1;
15865 clear_glyph_matrix (w->desired_matrix);
15866 goto try_to_scroll;
15867 }
15868 }
15869 }
15870
15871 /* If we need to move point for either of the above reasons,
15872 now actually do it. */
15873 if (new_vpos >= 0)
15874 {
15875 struct glyph_row *row;
15876
15877 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15878 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15879 ++row;
15880
15881 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15882 MATRIX_ROW_START_BYTEPOS (row));
15883
15884 if (w != XWINDOW (selected_window))
15885 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15886 else if (current_buffer == old)
15887 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15888
15889 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15890
15891 /* If we are highlighting the region, then we just changed
15892 the region, so redisplay to show it. */
15893 /* FIXME: We need to (re)run pre-redisplay-function! */
15894 /* if (markpos_of_region () >= 0)
15895 {
15896 clear_glyph_matrix (w->desired_matrix);
15897 if (!try_window (window, startp, 0))
15898 goto need_larger_matrices;
15899 }
15900 */
15901 }
15902
15903 #ifdef GLYPH_DEBUG
15904 debug_method_add (w, "forced window start");
15905 #endif
15906 goto done;
15907 }
15908
15909 /* Handle case where text has not changed, only point, and it has
15910 not moved off the frame, and we are not retrying after hscroll.
15911 (current_matrix_up_to_date_p is nonzero when retrying.) */
15912 if (current_matrix_up_to_date_p
15913 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15914 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15915 {
15916 switch (rc)
15917 {
15918 case CURSOR_MOVEMENT_SUCCESS:
15919 used_current_matrix_p = 1;
15920 goto done;
15921
15922 case CURSOR_MOVEMENT_MUST_SCROLL:
15923 goto try_to_scroll;
15924
15925 default:
15926 emacs_abort ();
15927 }
15928 }
15929 /* If current starting point was originally the beginning of a line
15930 but no longer is, find a new starting point. */
15931 else if (w->start_at_line_beg
15932 && !(CHARPOS (startp) <= BEGV
15933 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15934 {
15935 #ifdef GLYPH_DEBUG
15936 debug_method_add (w, "recenter 1");
15937 #endif
15938 goto recenter;
15939 }
15940
15941 /* Try scrolling with try_window_id. Value is > 0 if update has
15942 been done, it is -1 if we know that the same window start will
15943 not work. It is 0 if unsuccessful for some other reason. */
15944 else if ((tem = try_window_id (w)) != 0)
15945 {
15946 #ifdef GLYPH_DEBUG
15947 debug_method_add (w, "try_window_id %d", tem);
15948 #endif
15949
15950 if (f->fonts_changed)
15951 goto need_larger_matrices;
15952 if (tem > 0)
15953 goto done;
15954
15955 /* Otherwise try_window_id has returned -1 which means that we
15956 don't want the alternative below this comment to execute. */
15957 }
15958 else if (CHARPOS (startp) >= BEGV
15959 && CHARPOS (startp) <= ZV
15960 && PT >= CHARPOS (startp)
15961 && (CHARPOS (startp) < ZV
15962 /* Avoid starting at end of buffer. */
15963 || CHARPOS (startp) == BEGV
15964 || !window_outdated (w)))
15965 {
15966 int d1, d2, d3, d4, d5, d6;
15967
15968 /* If first window line is a continuation line, and window start
15969 is inside the modified region, but the first change is before
15970 current window start, we must select a new window start.
15971
15972 However, if this is the result of a down-mouse event (e.g. by
15973 extending the mouse-drag-overlay), we don't want to select a
15974 new window start, since that would change the position under
15975 the mouse, resulting in an unwanted mouse-movement rather
15976 than a simple mouse-click. */
15977 if (!w->start_at_line_beg
15978 && NILP (do_mouse_tracking)
15979 && CHARPOS (startp) > BEGV
15980 && CHARPOS (startp) > BEG + beg_unchanged
15981 && CHARPOS (startp) <= Z - end_unchanged
15982 /* Even if w->start_at_line_beg is nil, a new window may
15983 start at a line_beg, since that's how set_buffer_window
15984 sets it. So, we need to check the return value of
15985 compute_window_start_on_continuation_line. (See also
15986 bug#197). */
15987 && XMARKER (w->start)->buffer == current_buffer
15988 && compute_window_start_on_continuation_line (w)
15989 /* It doesn't make sense to force the window start like we
15990 do at label force_start if it is already known that point
15991 will not be visible in the resulting window, because
15992 doing so will move point from its correct position
15993 instead of scrolling the window to bring point into view.
15994 See bug#9324. */
15995 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15996 {
15997 w->force_start = 1;
15998 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15999 goto force_start;
16000 }
16001
16002 #ifdef GLYPH_DEBUG
16003 debug_method_add (w, "same window start");
16004 #endif
16005
16006 /* Try to redisplay starting at same place as before.
16007 If point has not moved off frame, accept the results. */
16008 if (!current_matrix_up_to_date_p
16009 /* Don't use try_window_reusing_current_matrix in this case
16010 because a window scroll function can have changed the
16011 buffer. */
16012 || !NILP (Vwindow_scroll_functions)
16013 || MINI_WINDOW_P (w)
16014 || !(used_current_matrix_p
16015 = try_window_reusing_current_matrix (w)))
16016 {
16017 IF_DEBUG (debug_method_add (w, "1"));
16018 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16019 /* -1 means we need to scroll.
16020 0 means we need new matrices, but fonts_changed
16021 is set in that case, so we will detect it below. */
16022 goto try_to_scroll;
16023 }
16024
16025 if (f->fonts_changed)
16026 goto need_larger_matrices;
16027
16028 if (w->cursor.vpos >= 0)
16029 {
16030 if (!just_this_one_p
16031 || current_buffer->clip_changed
16032 || BEG_UNCHANGED < CHARPOS (startp))
16033 /* Forget any recorded base line for line number display. */
16034 w->base_line_number = 0;
16035
16036 if (!cursor_row_fully_visible_p (w, 1, 0))
16037 {
16038 clear_glyph_matrix (w->desired_matrix);
16039 last_line_misfit = 1;
16040 }
16041 /* Drop through and scroll. */
16042 else
16043 goto done;
16044 }
16045 else
16046 clear_glyph_matrix (w->desired_matrix);
16047 }
16048
16049 try_to_scroll:
16050
16051 /* Redisplay the mode line. Select the buffer properly for that. */
16052 if (!update_mode_line)
16053 {
16054 update_mode_line = 1;
16055 w->update_mode_line = 1;
16056 }
16057
16058 /* Try to scroll by specified few lines. */
16059 if ((scroll_conservatively
16060 || emacs_scroll_step
16061 || temp_scroll_step
16062 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16063 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16064 && CHARPOS (startp) >= BEGV
16065 && CHARPOS (startp) <= ZV)
16066 {
16067 /* The function returns -1 if new fonts were loaded, 1 if
16068 successful, 0 if not successful. */
16069 int ss = try_scrolling (window, just_this_one_p,
16070 scroll_conservatively,
16071 emacs_scroll_step,
16072 temp_scroll_step, last_line_misfit);
16073 switch (ss)
16074 {
16075 case SCROLLING_SUCCESS:
16076 goto done;
16077
16078 case SCROLLING_NEED_LARGER_MATRICES:
16079 goto need_larger_matrices;
16080
16081 case SCROLLING_FAILED:
16082 break;
16083
16084 default:
16085 emacs_abort ();
16086 }
16087 }
16088
16089 /* Finally, just choose a place to start which positions point
16090 according to user preferences. */
16091
16092 recenter:
16093
16094 #ifdef GLYPH_DEBUG
16095 debug_method_add (w, "recenter");
16096 #endif
16097
16098 /* Forget any previously recorded base line for line number display. */
16099 if (!buffer_unchanged_p)
16100 w->base_line_number = 0;
16101
16102 /* Determine the window start relative to point. */
16103 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16104 it.current_y = it.last_visible_y;
16105 if (centering_position < 0)
16106 {
16107 int window_total_lines
16108 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16109 int margin =
16110 scroll_margin > 0
16111 ? min (scroll_margin, window_total_lines / 4)
16112 : 0;
16113 ptrdiff_t margin_pos = CHARPOS (startp);
16114 Lisp_Object aggressive;
16115 int scrolling_up;
16116
16117 /* If there is a scroll margin at the top of the window, find
16118 its character position. */
16119 if (margin
16120 /* Cannot call start_display if startp is not in the
16121 accessible region of the buffer. This can happen when we
16122 have just switched to a different buffer and/or changed
16123 its restriction. In that case, startp is initialized to
16124 the character position 1 (BEGV) because we did not yet
16125 have chance to display the buffer even once. */
16126 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16127 {
16128 struct it it1;
16129 void *it1data = NULL;
16130
16131 SAVE_IT (it1, it, it1data);
16132 start_display (&it1, w, startp);
16133 move_it_vertically (&it1, margin * frame_line_height);
16134 margin_pos = IT_CHARPOS (it1);
16135 RESTORE_IT (&it, &it, it1data);
16136 }
16137 scrolling_up = PT > margin_pos;
16138 aggressive =
16139 scrolling_up
16140 ? BVAR (current_buffer, scroll_up_aggressively)
16141 : BVAR (current_buffer, scroll_down_aggressively);
16142
16143 if (!MINI_WINDOW_P (w)
16144 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16145 {
16146 int pt_offset = 0;
16147
16148 /* Setting scroll-conservatively overrides
16149 scroll-*-aggressively. */
16150 if (!scroll_conservatively && NUMBERP (aggressive))
16151 {
16152 double float_amount = XFLOATINT (aggressive);
16153
16154 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16155 if (pt_offset == 0 && float_amount > 0)
16156 pt_offset = 1;
16157 if (pt_offset && margin > 0)
16158 margin -= 1;
16159 }
16160 /* Compute how much to move the window start backward from
16161 point so that point will be displayed where the user
16162 wants it. */
16163 if (scrolling_up)
16164 {
16165 centering_position = it.last_visible_y;
16166 if (pt_offset)
16167 centering_position -= pt_offset;
16168 centering_position -=
16169 frame_line_height * (1 + margin + (last_line_misfit != 0))
16170 + WINDOW_HEADER_LINE_HEIGHT (w);
16171 /* Don't let point enter the scroll margin near top of
16172 the window. */
16173 if (centering_position < margin * frame_line_height)
16174 centering_position = margin * frame_line_height;
16175 }
16176 else
16177 centering_position = margin * frame_line_height + pt_offset;
16178 }
16179 else
16180 /* Set the window start half the height of the window backward
16181 from point. */
16182 centering_position = window_box_height (w) / 2;
16183 }
16184 move_it_vertically_backward (&it, centering_position);
16185
16186 eassert (IT_CHARPOS (it) >= BEGV);
16187
16188 /* The function move_it_vertically_backward may move over more
16189 than the specified y-distance. If it->w is small, e.g. a
16190 mini-buffer window, we may end up in front of the window's
16191 display area. Start displaying at the start of the line
16192 containing PT in this case. */
16193 if (it.current_y <= 0)
16194 {
16195 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16196 move_it_vertically_backward (&it, 0);
16197 it.current_y = 0;
16198 }
16199
16200 it.current_x = it.hpos = 0;
16201
16202 /* Set the window start position here explicitly, to avoid an
16203 infinite loop in case the functions in window-scroll-functions
16204 get errors. */
16205 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16206
16207 /* Run scroll hooks. */
16208 startp = run_window_scroll_functions (window, it.current.pos);
16209
16210 /* Redisplay the window. */
16211 if (!current_matrix_up_to_date_p
16212 || windows_or_buffers_changed
16213 || f->cursor_type_changed
16214 /* Don't use try_window_reusing_current_matrix in this case
16215 because it can have changed the buffer. */
16216 || !NILP (Vwindow_scroll_functions)
16217 || !just_this_one_p
16218 || MINI_WINDOW_P (w)
16219 || !(used_current_matrix_p
16220 = try_window_reusing_current_matrix (w)))
16221 try_window (window, startp, 0);
16222
16223 /* If new fonts have been loaded (due to fontsets), give up. We
16224 have to start a new redisplay since we need to re-adjust glyph
16225 matrices. */
16226 if (f->fonts_changed)
16227 goto need_larger_matrices;
16228
16229 /* If cursor did not appear assume that the middle of the window is
16230 in the first line of the window. Do it again with the next line.
16231 (Imagine a window of height 100, displaying two lines of height
16232 60. Moving back 50 from it->last_visible_y will end in the first
16233 line.) */
16234 if (w->cursor.vpos < 0)
16235 {
16236 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16237 {
16238 clear_glyph_matrix (w->desired_matrix);
16239 move_it_by_lines (&it, 1);
16240 try_window (window, it.current.pos, 0);
16241 }
16242 else if (PT < IT_CHARPOS (it))
16243 {
16244 clear_glyph_matrix (w->desired_matrix);
16245 move_it_by_lines (&it, -1);
16246 try_window (window, it.current.pos, 0);
16247 }
16248 else
16249 {
16250 /* Not much we can do about it. */
16251 }
16252 }
16253
16254 /* Consider the following case: Window starts at BEGV, there is
16255 invisible, intangible text at BEGV, so that display starts at
16256 some point START > BEGV. It can happen that we are called with
16257 PT somewhere between BEGV and START. Try to handle that case. */
16258 if (w->cursor.vpos < 0)
16259 {
16260 struct glyph_row *row = w->current_matrix->rows;
16261 if (row->mode_line_p)
16262 ++row;
16263 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16264 }
16265
16266 if (!cursor_row_fully_visible_p (w, 0, 0))
16267 {
16268 /* If vscroll is enabled, disable it and try again. */
16269 if (w->vscroll)
16270 {
16271 w->vscroll = 0;
16272 clear_glyph_matrix (w->desired_matrix);
16273 goto recenter;
16274 }
16275
16276 /* Users who set scroll-conservatively to a large number want
16277 point just above/below the scroll margin. If we ended up
16278 with point's row partially visible, move the window start to
16279 make that row fully visible and out of the margin. */
16280 if (scroll_conservatively > SCROLL_LIMIT)
16281 {
16282 int window_total_lines
16283 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16284 int margin =
16285 scroll_margin > 0
16286 ? min (scroll_margin, window_total_lines / 4)
16287 : 0;
16288 int move_down = w->cursor.vpos >= window_total_lines / 2;
16289
16290 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16291 clear_glyph_matrix (w->desired_matrix);
16292 if (1 == try_window (window, it.current.pos,
16293 TRY_WINDOW_CHECK_MARGINS))
16294 goto done;
16295 }
16296
16297 /* If centering point failed to make the whole line visible,
16298 put point at the top instead. That has to make the whole line
16299 visible, if it can be done. */
16300 if (centering_position == 0)
16301 goto done;
16302
16303 clear_glyph_matrix (w->desired_matrix);
16304 centering_position = 0;
16305 goto recenter;
16306 }
16307
16308 done:
16309
16310 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16311 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16312 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16313
16314 /* Display the mode line, if we must. */
16315 if ((update_mode_line
16316 /* If window not full width, must redo its mode line
16317 if (a) the window to its side is being redone and
16318 (b) we do a frame-based redisplay. This is a consequence
16319 of how inverted lines are drawn in frame-based redisplay. */
16320 || (!just_this_one_p
16321 && !FRAME_WINDOW_P (f)
16322 && !WINDOW_FULL_WIDTH_P (w))
16323 /* Line number to display. */
16324 || w->base_line_pos > 0
16325 /* Column number is displayed and different from the one displayed. */
16326 || (w->column_number_displayed != -1
16327 && (w->column_number_displayed != current_column ())))
16328 /* This means that the window has a mode line. */
16329 && (WINDOW_WANTS_MODELINE_P (w)
16330 || WINDOW_WANTS_HEADER_LINE_P (w)))
16331 {
16332
16333 display_mode_lines (w);
16334
16335 /* If mode line height has changed, arrange for a thorough
16336 immediate redisplay using the correct mode line height. */
16337 if (WINDOW_WANTS_MODELINE_P (w)
16338 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16339 {
16340 f->fonts_changed = 1;
16341 w->mode_line_height = -1;
16342 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16343 = DESIRED_MODE_LINE_HEIGHT (w);
16344 }
16345
16346 /* If header line height has changed, arrange for a thorough
16347 immediate redisplay using the correct header line height. */
16348 if (WINDOW_WANTS_HEADER_LINE_P (w)
16349 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16350 {
16351 f->fonts_changed = 1;
16352 w->header_line_height = -1;
16353 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16354 = DESIRED_HEADER_LINE_HEIGHT (w);
16355 }
16356
16357 if (f->fonts_changed)
16358 goto need_larger_matrices;
16359 }
16360
16361 if (!line_number_displayed && w->base_line_pos != -1)
16362 {
16363 w->base_line_pos = 0;
16364 w->base_line_number = 0;
16365 }
16366
16367 finish_menu_bars:
16368
16369 /* When we reach a frame's selected window, redo the frame's menu bar. */
16370 if (update_mode_line
16371 && EQ (FRAME_SELECTED_WINDOW (f), window))
16372 {
16373 int redisplay_menu_p = 0;
16374
16375 if (FRAME_WINDOW_P (f))
16376 {
16377 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16378 || defined (HAVE_NS) || defined (USE_GTK)
16379 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16380 #else
16381 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16382 #endif
16383 }
16384 else
16385 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16386
16387 if (redisplay_menu_p)
16388 display_menu_bar (w);
16389
16390 #ifdef HAVE_WINDOW_SYSTEM
16391 if (FRAME_WINDOW_P (f))
16392 {
16393 #if defined (USE_GTK) || defined (HAVE_NS)
16394 if (FRAME_EXTERNAL_TOOL_BAR (f))
16395 redisplay_tool_bar (f);
16396 #else
16397 if (WINDOWP (f->tool_bar_window)
16398 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16399 || !NILP (Vauto_resize_tool_bars))
16400 && redisplay_tool_bar (f))
16401 ignore_mouse_drag_p = 1;
16402 #endif
16403 }
16404 #endif
16405 }
16406
16407 #ifdef HAVE_WINDOW_SYSTEM
16408 if (FRAME_WINDOW_P (f)
16409 && update_window_fringes (w, (just_this_one_p
16410 || (!used_current_matrix_p && !overlay_arrow_seen)
16411 || w->pseudo_window_p)))
16412 {
16413 update_begin (f);
16414 block_input ();
16415 if (draw_window_fringes (w, 1))
16416 {
16417 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16418 x_draw_right_divider (w);
16419 else
16420 x_draw_vertical_border (w);
16421 }
16422 unblock_input ();
16423 update_end (f);
16424 }
16425
16426 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16427 x_draw_bottom_divider (w);
16428 #endif /* HAVE_WINDOW_SYSTEM */
16429
16430 /* We go to this label, with fonts_changed set, if it is
16431 necessary to try again using larger glyph matrices.
16432 We have to redeem the scroll bar even in this case,
16433 because the loop in redisplay_internal expects that. */
16434 need_larger_matrices:
16435 ;
16436 finish_scroll_bars:
16437
16438 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16439 {
16440 /* Set the thumb's position and size. */
16441 set_vertical_scroll_bar (w);
16442
16443 /* Note that we actually used the scroll bar attached to this
16444 window, so it shouldn't be deleted at the end of redisplay. */
16445 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16446 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16447 }
16448
16449 /* Restore current_buffer and value of point in it. The window
16450 update may have changed the buffer, so first make sure `opoint'
16451 is still valid (Bug#6177). */
16452 if (CHARPOS (opoint) < BEGV)
16453 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16454 else if (CHARPOS (opoint) > ZV)
16455 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16456 else
16457 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16458
16459 set_buffer_internal_1 (old);
16460 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16461 shorter. This can be caused by log truncation in *Messages*. */
16462 if (CHARPOS (lpoint) <= ZV)
16463 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16464
16465 unbind_to (count, Qnil);
16466 }
16467
16468
16469 /* Build the complete desired matrix of WINDOW with a window start
16470 buffer position POS.
16471
16472 Value is 1 if successful. It is zero if fonts were loaded during
16473 redisplay which makes re-adjusting glyph matrices necessary, and -1
16474 if point would appear in the scroll margins.
16475 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16476 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16477 set in FLAGS.) */
16478
16479 int
16480 try_window (Lisp_Object window, struct text_pos pos, int flags)
16481 {
16482 struct window *w = XWINDOW (window);
16483 struct it it;
16484 struct glyph_row *last_text_row = NULL;
16485 struct frame *f = XFRAME (w->frame);
16486 int frame_line_height = default_line_pixel_height (w);
16487
16488 /* Make POS the new window start. */
16489 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16490
16491 /* Mark cursor position as unknown. No overlay arrow seen. */
16492 w->cursor.vpos = -1;
16493 overlay_arrow_seen = 0;
16494
16495 /* Initialize iterator and info to start at POS. */
16496 start_display (&it, w, pos);
16497
16498 /* Display all lines of W. */
16499 while (it.current_y < it.last_visible_y)
16500 {
16501 if (display_line (&it))
16502 last_text_row = it.glyph_row - 1;
16503 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16504 return 0;
16505 }
16506
16507 /* Don't let the cursor end in the scroll margins. */
16508 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16509 && !MINI_WINDOW_P (w))
16510 {
16511 int this_scroll_margin;
16512 int window_total_lines
16513 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16514
16515 if (scroll_margin > 0)
16516 {
16517 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16518 this_scroll_margin *= frame_line_height;
16519 }
16520 else
16521 this_scroll_margin = 0;
16522
16523 if ((w->cursor.y >= 0 /* not vscrolled */
16524 && w->cursor.y < this_scroll_margin
16525 && CHARPOS (pos) > BEGV
16526 && IT_CHARPOS (it) < ZV)
16527 /* rms: considering make_cursor_line_fully_visible_p here
16528 seems to give wrong results. We don't want to recenter
16529 when the last line is partly visible, we want to allow
16530 that case to be handled in the usual way. */
16531 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16532 {
16533 w->cursor.vpos = -1;
16534 clear_glyph_matrix (w->desired_matrix);
16535 return -1;
16536 }
16537 }
16538
16539 /* If bottom moved off end of frame, change mode line percentage. */
16540 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16541 w->update_mode_line = 1;
16542
16543 /* Set window_end_pos to the offset of the last character displayed
16544 on the window from the end of current_buffer. Set
16545 window_end_vpos to its row number. */
16546 if (last_text_row)
16547 {
16548 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16549 adjust_window_ends (w, last_text_row, 0);
16550 eassert
16551 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16552 w->window_end_vpos)));
16553 }
16554 else
16555 {
16556 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16557 w->window_end_pos = Z - ZV;
16558 w->window_end_vpos = 0;
16559 }
16560
16561 /* But that is not valid info until redisplay finishes. */
16562 w->window_end_valid = 0;
16563 return 1;
16564 }
16565
16566
16567 \f
16568 /************************************************************************
16569 Window redisplay reusing current matrix when buffer has not changed
16570 ************************************************************************/
16571
16572 /* Try redisplay of window W showing an unchanged buffer with a
16573 different window start than the last time it was displayed by
16574 reusing its current matrix. Value is non-zero if successful.
16575 W->start is the new window start. */
16576
16577 static int
16578 try_window_reusing_current_matrix (struct window *w)
16579 {
16580 struct frame *f = XFRAME (w->frame);
16581 struct glyph_row *bottom_row;
16582 struct it it;
16583 struct run run;
16584 struct text_pos start, new_start;
16585 int nrows_scrolled, i;
16586 struct glyph_row *last_text_row;
16587 struct glyph_row *last_reused_text_row;
16588 struct glyph_row *start_row;
16589 int start_vpos, min_y, max_y;
16590
16591 #ifdef GLYPH_DEBUG
16592 if (inhibit_try_window_reusing)
16593 return 0;
16594 #endif
16595
16596 if (/* This function doesn't handle terminal frames. */
16597 !FRAME_WINDOW_P (f)
16598 /* Don't try to reuse the display if windows have been split
16599 or such. */
16600 || windows_or_buffers_changed
16601 || f->cursor_type_changed)
16602 return 0;
16603
16604 /* Can't do this if showing trailing whitespace. */
16605 if (!NILP (Vshow_trailing_whitespace))
16606 return 0;
16607
16608 /* If top-line visibility has changed, give up. */
16609 if (WINDOW_WANTS_HEADER_LINE_P (w)
16610 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16611 return 0;
16612
16613 /* Give up if old or new display is scrolled vertically. We could
16614 make this function handle this, but right now it doesn't. */
16615 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16616 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16617 return 0;
16618
16619 /* The variable new_start now holds the new window start. The old
16620 start `start' can be determined from the current matrix. */
16621 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16622 start = start_row->minpos;
16623 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16624
16625 /* Clear the desired matrix for the display below. */
16626 clear_glyph_matrix (w->desired_matrix);
16627
16628 if (CHARPOS (new_start) <= CHARPOS (start))
16629 {
16630 /* Don't use this method if the display starts with an ellipsis
16631 displayed for invisible text. It's not easy to handle that case
16632 below, and it's certainly not worth the effort since this is
16633 not a frequent case. */
16634 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16635 return 0;
16636
16637 IF_DEBUG (debug_method_add (w, "twu1"));
16638
16639 /* Display up to a row that can be reused. The variable
16640 last_text_row is set to the last row displayed that displays
16641 text. Note that it.vpos == 0 if or if not there is a
16642 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16643 start_display (&it, w, new_start);
16644 w->cursor.vpos = -1;
16645 last_text_row = last_reused_text_row = NULL;
16646
16647 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16648 {
16649 /* If we have reached into the characters in the START row,
16650 that means the line boundaries have changed. So we
16651 can't start copying with the row START. Maybe it will
16652 work to start copying with the following row. */
16653 while (IT_CHARPOS (it) > CHARPOS (start))
16654 {
16655 /* Advance to the next row as the "start". */
16656 start_row++;
16657 start = start_row->minpos;
16658 /* If there are no more rows to try, or just one, give up. */
16659 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16660 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16661 || CHARPOS (start) == ZV)
16662 {
16663 clear_glyph_matrix (w->desired_matrix);
16664 return 0;
16665 }
16666
16667 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16668 }
16669 /* If we have reached alignment, we can copy the rest of the
16670 rows. */
16671 if (IT_CHARPOS (it) == CHARPOS (start)
16672 /* Don't accept "alignment" inside a display vector,
16673 since start_row could have started in the middle of
16674 that same display vector (thus their character
16675 positions match), and we have no way of telling if
16676 that is the case. */
16677 && it.current.dpvec_index < 0)
16678 break;
16679
16680 if (display_line (&it))
16681 last_text_row = it.glyph_row - 1;
16682
16683 }
16684
16685 /* A value of current_y < last_visible_y means that we stopped
16686 at the previous window start, which in turn means that we
16687 have at least one reusable row. */
16688 if (it.current_y < it.last_visible_y)
16689 {
16690 struct glyph_row *row;
16691
16692 /* IT.vpos always starts from 0; it counts text lines. */
16693 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16694
16695 /* Find PT if not already found in the lines displayed. */
16696 if (w->cursor.vpos < 0)
16697 {
16698 int dy = it.current_y - start_row->y;
16699
16700 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16701 row = row_containing_pos (w, PT, row, NULL, dy);
16702 if (row)
16703 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16704 dy, nrows_scrolled);
16705 else
16706 {
16707 clear_glyph_matrix (w->desired_matrix);
16708 return 0;
16709 }
16710 }
16711
16712 /* Scroll the display. Do it before the current matrix is
16713 changed. The problem here is that update has not yet
16714 run, i.e. part of the current matrix is not up to date.
16715 scroll_run_hook will clear the cursor, and use the
16716 current matrix to get the height of the row the cursor is
16717 in. */
16718 run.current_y = start_row->y;
16719 run.desired_y = it.current_y;
16720 run.height = it.last_visible_y - it.current_y;
16721
16722 if (run.height > 0 && run.current_y != run.desired_y)
16723 {
16724 update_begin (f);
16725 FRAME_RIF (f)->update_window_begin_hook (w);
16726 FRAME_RIF (f)->clear_window_mouse_face (w);
16727 FRAME_RIF (f)->scroll_run_hook (w, &run);
16728 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16729 update_end (f);
16730 }
16731
16732 /* Shift current matrix down by nrows_scrolled lines. */
16733 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16734 rotate_matrix (w->current_matrix,
16735 start_vpos,
16736 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16737 nrows_scrolled);
16738
16739 /* Disable lines that must be updated. */
16740 for (i = 0; i < nrows_scrolled; ++i)
16741 (start_row + i)->enabled_p = 0;
16742
16743 /* Re-compute Y positions. */
16744 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16745 max_y = it.last_visible_y;
16746 for (row = start_row + nrows_scrolled;
16747 row < bottom_row;
16748 ++row)
16749 {
16750 row->y = it.current_y;
16751 row->visible_height = row->height;
16752
16753 if (row->y < min_y)
16754 row->visible_height -= min_y - row->y;
16755 if (row->y + row->height > max_y)
16756 row->visible_height -= row->y + row->height - max_y;
16757 if (row->fringe_bitmap_periodic_p)
16758 row->redraw_fringe_bitmaps_p = 1;
16759
16760 it.current_y += row->height;
16761
16762 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16763 last_reused_text_row = row;
16764 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16765 break;
16766 }
16767
16768 /* Disable lines in the current matrix which are now
16769 below the window. */
16770 for (++row; row < bottom_row; ++row)
16771 row->enabled_p = row->mode_line_p = 0;
16772 }
16773
16774 /* Update window_end_pos etc.; last_reused_text_row is the last
16775 reused row from the current matrix containing text, if any.
16776 The value of last_text_row is the last displayed line
16777 containing text. */
16778 if (last_reused_text_row)
16779 adjust_window_ends (w, last_reused_text_row, 1);
16780 else if (last_text_row)
16781 adjust_window_ends (w, last_text_row, 0);
16782 else
16783 {
16784 /* This window must be completely empty. */
16785 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16786 w->window_end_pos = Z - ZV;
16787 w->window_end_vpos = 0;
16788 }
16789 w->window_end_valid = 0;
16790
16791 /* Update hint: don't try scrolling again in update_window. */
16792 w->desired_matrix->no_scrolling_p = 1;
16793
16794 #ifdef GLYPH_DEBUG
16795 debug_method_add (w, "try_window_reusing_current_matrix 1");
16796 #endif
16797 return 1;
16798 }
16799 else if (CHARPOS (new_start) > CHARPOS (start))
16800 {
16801 struct glyph_row *pt_row, *row;
16802 struct glyph_row *first_reusable_row;
16803 struct glyph_row *first_row_to_display;
16804 int dy;
16805 int yb = window_text_bottom_y (w);
16806
16807 /* Find the row starting at new_start, if there is one. Don't
16808 reuse a partially visible line at the end. */
16809 first_reusable_row = start_row;
16810 while (first_reusable_row->enabled_p
16811 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16812 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16813 < CHARPOS (new_start)))
16814 ++first_reusable_row;
16815
16816 /* Give up if there is no row to reuse. */
16817 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16818 || !first_reusable_row->enabled_p
16819 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16820 != CHARPOS (new_start)))
16821 return 0;
16822
16823 /* We can reuse fully visible rows beginning with
16824 first_reusable_row to the end of the window. Set
16825 first_row_to_display to the first row that cannot be reused.
16826 Set pt_row to the row containing point, if there is any. */
16827 pt_row = NULL;
16828 for (first_row_to_display = first_reusable_row;
16829 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16830 ++first_row_to_display)
16831 {
16832 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16833 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16834 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16835 && first_row_to_display->ends_at_zv_p
16836 && pt_row == NULL)))
16837 pt_row = first_row_to_display;
16838 }
16839
16840 /* Start displaying at the start of first_row_to_display. */
16841 eassert (first_row_to_display->y < yb);
16842 init_to_row_start (&it, w, first_row_to_display);
16843
16844 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16845 - start_vpos);
16846 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16847 - nrows_scrolled);
16848 it.current_y = (first_row_to_display->y - first_reusable_row->y
16849 + WINDOW_HEADER_LINE_HEIGHT (w));
16850
16851 /* Display lines beginning with first_row_to_display in the
16852 desired matrix. Set last_text_row to the last row displayed
16853 that displays text. */
16854 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16855 if (pt_row == NULL)
16856 w->cursor.vpos = -1;
16857 last_text_row = NULL;
16858 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16859 if (display_line (&it))
16860 last_text_row = it.glyph_row - 1;
16861
16862 /* If point is in a reused row, adjust y and vpos of the cursor
16863 position. */
16864 if (pt_row)
16865 {
16866 w->cursor.vpos -= nrows_scrolled;
16867 w->cursor.y -= first_reusable_row->y - start_row->y;
16868 }
16869
16870 /* Give up if point isn't in a row displayed or reused. (This
16871 also handles the case where w->cursor.vpos < nrows_scrolled
16872 after the calls to display_line, which can happen with scroll
16873 margins. See bug#1295.) */
16874 if (w->cursor.vpos < 0)
16875 {
16876 clear_glyph_matrix (w->desired_matrix);
16877 return 0;
16878 }
16879
16880 /* Scroll the display. */
16881 run.current_y = first_reusable_row->y;
16882 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16883 run.height = it.last_visible_y - run.current_y;
16884 dy = run.current_y - run.desired_y;
16885
16886 if (run.height)
16887 {
16888 update_begin (f);
16889 FRAME_RIF (f)->update_window_begin_hook (w);
16890 FRAME_RIF (f)->clear_window_mouse_face (w);
16891 FRAME_RIF (f)->scroll_run_hook (w, &run);
16892 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16893 update_end (f);
16894 }
16895
16896 /* Adjust Y positions of reused rows. */
16897 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16898 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16899 max_y = it.last_visible_y;
16900 for (row = first_reusable_row; row < first_row_to_display; ++row)
16901 {
16902 row->y -= dy;
16903 row->visible_height = row->height;
16904 if (row->y < min_y)
16905 row->visible_height -= min_y - row->y;
16906 if (row->y + row->height > max_y)
16907 row->visible_height -= row->y + row->height - max_y;
16908 if (row->fringe_bitmap_periodic_p)
16909 row->redraw_fringe_bitmaps_p = 1;
16910 }
16911
16912 /* Scroll the current matrix. */
16913 eassert (nrows_scrolled > 0);
16914 rotate_matrix (w->current_matrix,
16915 start_vpos,
16916 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16917 -nrows_scrolled);
16918
16919 /* Disable rows not reused. */
16920 for (row -= nrows_scrolled; row < bottom_row; ++row)
16921 row->enabled_p = 0;
16922
16923 /* Point may have moved to a different line, so we cannot assume that
16924 the previous cursor position is valid; locate the correct row. */
16925 if (pt_row)
16926 {
16927 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16928 row < bottom_row
16929 && PT >= MATRIX_ROW_END_CHARPOS (row)
16930 && !row->ends_at_zv_p;
16931 row++)
16932 {
16933 w->cursor.vpos++;
16934 w->cursor.y = row->y;
16935 }
16936 if (row < bottom_row)
16937 {
16938 /* Can't simply scan the row for point with
16939 bidi-reordered glyph rows. Let set_cursor_from_row
16940 figure out where to put the cursor, and if it fails,
16941 give up. */
16942 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16943 {
16944 if (!set_cursor_from_row (w, row, w->current_matrix,
16945 0, 0, 0, 0))
16946 {
16947 clear_glyph_matrix (w->desired_matrix);
16948 return 0;
16949 }
16950 }
16951 else
16952 {
16953 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16954 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16955
16956 for (; glyph < end
16957 && (!BUFFERP (glyph->object)
16958 || glyph->charpos < PT);
16959 glyph++)
16960 {
16961 w->cursor.hpos++;
16962 w->cursor.x += glyph->pixel_width;
16963 }
16964 }
16965 }
16966 }
16967
16968 /* Adjust window end. A null value of last_text_row means that
16969 the window end is in reused rows which in turn means that
16970 only its vpos can have changed. */
16971 if (last_text_row)
16972 adjust_window_ends (w, last_text_row, 0);
16973 else
16974 w->window_end_vpos -= nrows_scrolled;
16975
16976 w->window_end_valid = 0;
16977 w->desired_matrix->no_scrolling_p = 1;
16978
16979 #ifdef GLYPH_DEBUG
16980 debug_method_add (w, "try_window_reusing_current_matrix 2");
16981 #endif
16982 return 1;
16983 }
16984
16985 return 0;
16986 }
16987
16988
16989 \f
16990 /************************************************************************
16991 Window redisplay reusing current matrix when buffer has changed
16992 ************************************************************************/
16993
16994 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16995 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16996 ptrdiff_t *, ptrdiff_t *);
16997 static struct glyph_row *
16998 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16999 struct glyph_row *);
17000
17001
17002 /* Return the last row in MATRIX displaying text. If row START is
17003 non-null, start searching with that row. IT gives the dimensions
17004 of the display. Value is null if matrix is empty; otherwise it is
17005 a pointer to the row found. */
17006
17007 static struct glyph_row *
17008 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17009 struct glyph_row *start)
17010 {
17011 struct glyph_row *row, *row_found;
17012
17013 /* Set row_found to the last row in IT->w's current matrix
17014 displaying text. The loop looks funny but think of partially
17015 visible lines. */
17016 row_found = NULL;
17017 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17018 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17019 {
17020 eassert (row->enabled_p);
17021 row_found = row;
17022 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17023 break;
17024 ++row;
17025 }
17026
17027 return row_found;
17028 }
17029
17030
17031 /* Return the last row in the current matrix of W that is not affected
17032 by changes at the start of current_buffer that occurred since W's
17033 current matrix was built. Value is null if no such row exists.
17034
17035 BEG_UNCHANGED us the number of characters unchanged at the start of
17036 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17037 first changed character in current_buffer. Characters at positions <
17038 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17039 when the current matrix was built. */
17040
17041 static struct glyph_row *
17042 find_last_unchanged_at_beg_row (struct window *w)
17043 {
17044 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17045 struct glyph_row *row;
17046 struct glyph_row *row_found = NULL;
17047 int yb = window_text_bottom_y (w);
17048
17049 /* Find the last row displaying unchanged text. */
17050 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17051 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17052 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17053 ++row)
17054 {
17055 if (/* If row ends before first_changed_pos, it is unchanged,
17056 except in some case. */
17057 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17058 /* When row ends in ZV and we write at ZV it is not
17059 unchanged. */
17060 && !row->ends_at_zv_p
17061 /* When first_changed_pos is the end of a continued line,
17062 row is not unchanged because it may be no longer
17063 continued. */
17064 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17065 && (row->continued_p
17066 || row->exact_window_width_line_p))
17067 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17068 needs to be recomputed, so don't consider this row as
17069 unchanged. This happens when the last line was
17070 bidi-reordered and was killed immediately before this
17071 redisplay cycle. In that case, ROW->end stores the
17072 buffer position of the first visual-order character of
17073 the killed text, which is now beyond ZV. */
17074 && CHARPOS (row->end.pos) <= ZV)
17075 row_found = row;
17076
17077 /* Stop if last visible row. */
17078 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17079 break;
17080 }
17081
17082 return row_found;
17083 }
17084
17085
17086 /* Find the first glyph row in the current matrix of W that is not
17087 affected by changes at the end of current_buffer since the
17088 time W's current matrix was built.
17089
17090 Return in *DELTA the number of chars by which buffer positions in
17091 unchanged text at the end of current_buffer must be adjusted.
17092
17093 Return in *DELTA_BYTES the corresponding number of bytes.
17094
17095 Value is null if no such row exists, i.e. all rows are affected by
17096 changes. */
17097
17098 static struct glyph_row *
17099 find_first_unchanged_at_end_row (struct window *w,
17100 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17101 {
17102 struct glyph_row *row;
17103 struct glyph_row *row_found = NULL;
17104
17105 *delta = *delta_bytes = 0;
17106
17107 /* Display must not have been paused, otherwise the current matrix
17108 is not up to date. */
17109 eassert (w->window_end_valid);
17110
17111 /* A value of window_end_pos >= END_UNCHANGED means that the window
17112 end is in the range of changed text. If so, there is no
17113 unchanged row at the end of W's current matrix. */
17114 if (w->window_end_pos >= END_UNCHANGED)
17115 return NULL;
17116
17117 /* Set row to the last row in W's current matrix displaying text. */
17118 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17119
17120 /* If matrix is entirely empty, no unchanged row exists. */
17121 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17122 {
17123 /* The value of row is the last glyph row in the matrix having a
17124 meaningful buffer position in it. The end position of row
17125 corresponds to window_end_pos. This allows us to translate
17126 buffer positions in the current matrix to current buffer
17127 positions for characters not in changed text. */
17128 ptrdiff_t Z_old =
17129 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17130 ptrdiff_t Z_BYTE_old =
17131 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17132 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17133 struct glyph_row *first_text_row
17134 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17135
17136 *delta = Z - Z_old;
17137 *delta_bytes = Z_BYTE - Z_BYTE_old;
17138
17139 /* Set last_unchanged_pos to the buffer position of the last
17140 character in the buffer that has not been changed. Z is the
17141 index + 1 of the last character in current_buffer, i.e. by
17142 subtracting END_UNCHANGED we get the index of the last
17143 unchanged character, and we have to add BEG to get its buffer
17144 position. */
17145 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17146 last_unchanged_pos_old = last_unchanged_pos - *delta;
17147
17148 /* Search backward from ROW for a row displaying a line that
17149 starts at a minimum position >= last_unchanged_pos_old. */
17150 for (; row > first_text_row; --row)
17151 {
17152 /* This used to abort, but it can happen.
17153 It is ok to just stop the search instead here. KFS. */
17154 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17155 break;
17156
17157 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17158 row_found = row;
17159 }
17160 }
17161
17162 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17163
17164 return row_found;
17165 }
17166
17167
17168 /* Make sure that glyph rows in the current matrix of window W
17169 reference the same glyph memory as corresponding rows in the
17170 frame's frame matrix. This function is called after scrolling W's
17171 current matrix on a terminal frame in try_window_id and
17172 try_window_reusing_current_matrix. */
17173
17174 static void
17175 sync_frame_with_window_matrix_rows (struct window *w)
17176 {
17177 struct frame *f = XFRAME (w->frame);
17178 struct glyph_row *window_row, *window_row_end, *frame_row;
17179
17180 /* Preconditions: W must be a leaf window and full-width. Its frame
17181 must have a frame matrix. */
17182 eassert (BUFFERP (w->contents));
17183 eassert (WINDOW_FULL_WIDTH_P (w));
17184 eassert (!FRAME_WINDOW_P (f));
17185
17186 /* If W is a full-width window, glyph pointers in W's current matrix
17187 have, by definition, to be the same as glyph pointers in the
17188 corresponding frame matrix. Note that frame matrices have no
17189 marginal areas (see build_frame_matrix). */
17190 window_row = w->current_matrix->rows;
17191 window_row_end = window_row + w->current_matrix->nrows;
17192 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17193 while (window_row < window_row_end)
17194 {
17195 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17196 struct glyph *end = window_row->glyphs[LAST_AREA];
17197
17198 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17199 frame_row->glyphs[TEXT_AREA] = start;
17200 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17201 frame_row->glyphs[LAST_AREA] = end;
17202
17203 /* Disable frame rows whose corresponding window rows have
17204 been disabled in try_window_id. */
17205 if (!window_row->enabled_p)
17206 frame_row->enabled_p = 0;
17207
17208 ++window_row, ++frame_row;
17209 }
17210 }
17211
17212
17213 /* Find the glyph row in window W containing CHARPOS. Consider all
17214 rows between START and END (not inclusive). END null means search
17215 all rows to the end of the display area of W. Value is the row
17216 containing CHARPOS or null. */
17217
17218 struct glyph_row *
17219 row_containing_pos (struct window *w, ptrdiff_t charpos,
17220 struct glyph_row *start, struct glyph_row *end, int dy)
17221 {
17222 struct glyph_row *row = start;
17223 struct glyph_row *best_row = NULL;
17224 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17225 int last_y;
17226
17227 /* If we happen to start on a header-line, skip that. */
17228 if (row->mode_line_p)
17229 ++row;
17230
17231 if ((end && row >= end) || !row->enabled_p)
17232 return NULL;
17233
17234 last_y = window_text_bottom_y (w) - dy;
17235
17236 while (1)
17237 {
17238 /* Give up if we have gone too far. */
17239 if (end && row >= end)
17240 return NULL;
17241 /* This formerly returned if they were equal.
17242 I think that both quantities are of a "last plus one" type;
17243 if so, when they are equal, the row is within the screen. -- rms. */
17244 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17245 return NULL;
17246
17247 /* If it is in this row, return this row. */
17248 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17249 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17250 /* The end position of a row equals the start
17251 position of the next row. If CHARPOS is there, we
17252 would rather consider it displayed in the next
17253 line, except when this line ends in ZV. */
17254 && !row_for_charpos_p (row, charpos)))
17255 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17256 {
17257 struct glyph *g;
17258
17259 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17260 || (!best_row && !row->continued_p))
17261 return row;
17262 /* In bidi-reordered rows, there could be several rows whose
17263 edges surround CHARPOS, all of these rows belonging to
17264 the same continued line. We need to find the row which
17265 fits CHARPOS the best. */
17266 for (g = row->glyphs[TEXT_AREA];
17267 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17268 g++)
17269 {
17270 if (!STRINGP (g->object))
17271 {
17272 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17273 {
17274 mindif = eabs (g->charpos - charpos);
17275 best_row = row;
17276 /* Exact match always wins. */
17277 if (mindif == 0)
17278 return best_row;
17279 }
17280 }
17281 }
17282 }
17283 else if (best_row && !row->continued_p)
17284 return best_row;
17285 ++row;
17286 }
17287 }
17288
17289
17290 /* Try to redisplay window W by reusing its existing display. W's
17291 current matrix must be up to date when this function is called,
17292 i.e. window_end_valid must be nonzero.
17293
17294 Value is
17295
17296 1 if display has been updated
17297 0 if otherwise unsuccessful
17298 -1 if redisplay with same window start is known not to succeed
17299
17300 The following steps are performed:
17301
17302 1. Find the last row in the current matrix of W that is not
17303 affected by changes at the start of current_buffer. If no such row
17304 is found, give up.
17305
17306 2. Find the first row in W's current matrix that is not affected by
17307 changes at the end of current_buffer. Maybe there is no such row.
17308
17309 3. Display lines beginning with the row + 1 found in step 1 to the
17310 row found in step 2 or, if step 2 didn't find a row, to the end of
17311 the window.
17312
17313 4. If cursor is not known to appear on the window, give up.
17314
17315 5. If display stopped at the row found in step 2, scroll the
17316 display and current matrix as needed.
17317
17318 6. Maybe display some lines at the end of W, if we must. This can
17319 happen under various circumstances, like a partially visible line
17320 becoming fully visible, or because newly displayed lines are displayed
17321 in smaller font sizes.
17322
17323 7. Update W's window end information. */
17324
17325 static int
17326 try_window_id (struct window *w)
17327 {
17328 struct frame *f = XFRAME (w->frame);
17329 struct glyph_matrix *current_matrix = w->current_matrix;
17330 struct glyph_matrix *desired_matrix = w->desired_matrix;
17331 struct glyph_row *last_unchanged_at_beg_row;
17332 struct glyph_row *first_unchanged_at_end_row;
17333 struct glyph_row *row;
17334 struct glyph_row *bottom_row;
17335 int bottom_vpos;
17336 struct it it;
17337 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17338 int dvpos, dy;
17339 struct text_pos start_pos;
17340 struct run run;
17341 int first_unchanged_at_end_vpos = 0;
17342 struct glyph_row *last_text_row, *last_text_row_at_end;
17343 struct text_pos start;
17344 ptrdiff_t first_changed_charpos, last_changed_charpos;
17345
17346 #ifdef GLYPH_DEBUG
17347 if (inhibit_try_window_id)
17348 return 0;
17349 #endif
17350
17351 /* This is handy for debugging. */
17352 #if 0
17353 #define GIVE_UP(X) \
17354 do { \
17355 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17356 return 0; \
17357 } while (0)
17358 #else
17359 #define GIVE_UP(X) return 0
17360 #endif
17361
17362 SET_TEXT_POS_FROM_MARKER (start, w->start);
17363
17364 /* Don't use this for mini-windows because these can show
17365 messages and mini-buffers, and we don't handle that here. */
17366 if (MINI_WINDOW_P (w))
17367 GIVE_UP (1);
17368
17369 /* This flag is used to prevent redisplay optimizations. */
17370 if (windows_or_buffers_changed || f->cursor_type_changed)
17371 GIVE_UP (2);
17372
17373 /* Verify that narrowing has not changed.
17374 Also verify that we were not told to prevent redisplay optimizations.
17375 It would be nice to further
17376 reduce the number of cases where this prevents try_window_id. */
17377 if (current_buffer->clip_changed
17378 || current_buffer->prevent_redisplay_optimizations_p)
17379 GIVE_UP (3);
17380
17381 /* Window must either use window-based redisplay or be full width. */
17382 if (!FRAME_WINDOW_P (f)
17383 && (!FRAME_LINE_INS_DEL_OK (f)
17384 || !WINDOW_FULL_WIDTH_P (w)))
17385 GIVE_UP (4);
17386
17387 /* Give up if point is known NOT to appear in W. */
17388 if (PT < CHARPOS (start))
17389 GIVE_UP (5);
17390
17391 /* Another way to prevent redisplay optimizations. */
17392 if (w->last_modified == 0)
17393 GIVE_UP (6);
17394
17395 /* Verify that window is not hscrolled. */
17396 if (w->hscroll != 0)
17397 GIVE_UP (7);
17398
17399 /* Verify that display wasn't paused. */
17400 if (!w->window_end_valid)
17401 GIVE_UP (8);
17402
17403 /* Likewise if highlighting trailing whitespace. */
17404 if (!NILP (Vshow_trailing_whitespace))
17405 GIVE_UP (11);
17406
17407 /* Can't use this if overlay arrow position and/or string have
17408 changed. */
17409 if (overlay_arrows_changed_p ())
17410 GIVE_UP (12);
17411
17412 /* When word-wrap is on, adding a space to the first word of a
17413 wrapped line can change the wrap position, altering the line
17414 above it. It might be worthwhile to handle this more
17415 intelligently, but for now just redisplay from scratch. */
17416 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17417 GIVE_UP (21);
17418
17419 /* Under bidi reordering, adding or deleting a character in the
17420 beginning of a paragraph, before the first strong directional
17421 character, can change the base direction of the paragraph (unless
17422 the buffer specifies a fixed paragraph direction), which will
17423 require to redisplay the whole paragraph. It might be worthwhile
17424 to find the paragraph limits and widen the range of redisplayed
17425 lines to that, but for now just give up this optimization and
17426 redisplay from scratch. */
17427 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17428 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17429 GIVE_UP (22);
17430
17431 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17432 only if buffer has really changed. The reason is that the gap is
17433 initially at Z for freshly visited files. The code below would
17434 set end_unchanged to 0 in that case. */
17435 if (MODIFF > SAVE_MODIFF
17436 /* This seems to happen sometimes after saving a buffer. */
17437 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17438 {
17439 if (GPT - BEG < BEG_UNCHANGED)
17440 BEG_UNCHANGED = GPT - BEG;
17441 if (Z - GPT < END_UNCHANGED)
17442 END_UNCHANGED = Z - GPT;
17443 }
17444
17445 /* The position of the first and last character that has been changed. */
17446 first_changed_charpos = BEG + BEG_UNCHANGED;
17447 last_changed_charpos = Z - END_UNCHANGED;
17448
17449 /* If window starts after a line end, and the last change is in
17450 front of that newline, then changes don't affect the display.
17451 This case happens with stealth-fontification. Note that although
17452 the display is unchanged, glyph positions in the matrix have to
17453 be adjusted, of course. */
17454 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17455 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17456 && ((last_changed_charpos < CHARPOS (start)
17457 && CHARPOS (start) == BEGV)
17458 || (last_changed_charpos < CHARPOS (start) - 1
17459 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17460 {
17461 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17462 struct glyph_row *r0;
17463
17464 /* Compute how many chars/bytes have been added to or removed
17465 from the buffer. */
17466 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17467 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17468 Z_delta = Z - Z_old;
17469 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17470
17471 /* Give up if PT is not in the window. Note that it already has
17472 been checked at the start of try_window_id that PT is not in
17473 front of the window start. */
17474 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17475 GIVE_UP (13);
17476
17477 /* If window start is unchanged, we can reuse the whole matrix
17478 as is, after adjusting glyph positions. No need to compute
17479 the window end again, since its offset from Z hasn't changed. */
17480 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17481 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17482 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17483 /* PT must not be in a partially visible line. */
17484 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17485 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17486 {
17487 /* Adjust positions in the glyph matrix. */
17488 if (Z_delta || Z_delta_bytes)
17489 {
17490 struct glyph_row *r1
17491 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17492 increment_matrix_positions (w->current_matrix,
17493 MATRIX_ROW_VPOS (r0, current_matrix),
17494 MATRIX_ROW_VPOS (r1, current_matrix),
17495 Z_delta, Z_delta_bytes);
17496 }
17497
17498 /* Set the cursor. */
17499 row = row_containing_pos (w, PT, r0, NULL, 0);
17500 if (row)
17501 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17502 return 1;
17503 }
17504 }
17505
17506 /* Handle the case that changes are all below what is displayed in
17507 the window, and that PT is in the window. This shortcut cannot
17508 be taken if ZV is visible in the window, and text has been added
17509 there that is visible in the window. */
17510 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17511 /* ZV is not visible in the window, or there are no
17512 changes at ZV, actually. */
17513 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17514 || first_changed_charpos == last_changed_charpos))
17515 {
17516 struct glyph_row *r0;
17517
17518 /* Give up if PT is not in the window. Note that it already has
17519 been checked at the start of try_window_id that PT is not in
17520 front of the window start. */
17521 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17522 GIVE_UP (14);
17523
17524 /* If window start is unchanged, we can reuse the whole matrix
17525 as is, without changing glyph positions since no text has
17526 been added/removed in front of the window end. */
17527 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17528 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17529 /* PT must not be in a partially visible line. */
17530 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17531 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17532 {
17533 /* We have to compute the window end anew since text
17534 could have been added/removed after it. */
17535 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17536 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
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 2;
17543 }
17544 }
17545
17546 /* Give up if window start is in the changed area.
17547
17548 The condition used to read
17549
17550 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17551
17552 but why that was tested escapes me at the moment. */
17553 if (CHARPOS (start) >= first_changed_charpos
17554 && CHARPOS (start) <= last_changed_charpos)
17555 GIVE_UP (15);
17556
17557 /* Check that window start agrees with the start of the first glyph
17558 row in its current matrix. Check this after we know the window
17559 start is not in changed text, otherwise positions would not be
17560 comparable. */
17561 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17562 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17563 GIVE_UP (16);
17564
17565 /* Give up if the window ends in strings. Overlay strings
17566 at the end are difficult to handle, so don't try. */
17567 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17568 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17569 GIVE_UP (20);
17570
17571 /* Compute the position at which we have to start displaying new
17572 lines. Some of the lines at the top of the window might be
17573 reusable because they are not displaying changed text. Find the
17574 last row in W's current matrix not affected by changes at the
17575 start of current_buffer. Value is null if changes start in the
17576 first line of window. */
17577 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17578 if (last_unchanged_at_beg_row)
17579 {
17580 /* Avoid starting to display in the middle of a character, a TAB
17581 for instance. This is easier than to set up the iterator
17582 exactly, and it's not a frequent case, so the additional
17583 effort wouldn't really pay off. */
17584 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17585 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17586 && last_unchanged_at_beg_row > w->current_matrix->rows)
17587 --last_unchanged_at_beg_row;
17588
17589 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17590 GIVE_UP (17);
17591
17592 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17593 GIVE_UP (18);
17594 start_pos = it.current.pos;
17595
17596 /* Start displaying new lines in the desired matrix at the same
17597 vpos we would use in the current matrix, i.e. below
17598 last_unchanged_at_beg_row. */
17599 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17600 current_matrix);
17601 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17602 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17603
17604 eassert (it.hpos == 0 && it.current_x == 0);
17605 }
17606 else
17607 {
17608 /* There are no reusable lines at the start of the window.
17609 Start displaying in the first text line. */
17610 start_display (&it, w, start);
17611 it.vpos = it.first_vpos;
17612 start_pos = it.current.pos;
17613 }
17614
17615 /* Find the first row that is not affected by changes at the end of
17616 the buffer. Value will be null if there is no unchanged row, in
17617 which case we must redisplay to the end of the window. delta
17618 will be set to the value by which buffer positions beginning with
17619 first_unchanged_at_end_row have to be adjusted due to text
17620 changes. */
17621 first_unchanged_at_end_row
17622 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17623 IF_DEBUG (debug_delta = delta);
17624 IF_DEBUG (debug_delta_bytes = delta_bytes);
17625
17626 /* Set stop_pos to the buffer position up to which we will have to
17627 display new lines. If first_unchanged_at_end_row != NULL, this
17628 is the buffer position of the start of the line displayed in that
17629 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17630 that we don't stop at a buffer position. */
17631 stop_pos = 0;
17632 if (first_unchanged_at_end_row)
17633 {
17634 eassert (last_unchanged_at_beg_row == NULL
17635 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17636
17637 /* If this is a continuation line, move forward to the next one
17638 that isn't. Changes in lines above affect this line.
17639 Caution: this may move first_unchanged_at_end_row to a row
17640 not displaying text. */
17641 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17642 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17643 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17644 < it.last_visible_y))
17645 ++first_unchanged_at_end_row;
17646
17647 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17648 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17649 >= it.last_visible_y))
17650 first_unchanged_at_end_row = NULL;
17651 else
17652 {
17653 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17654 + delta);
17655 first_unchanged_at_end_vpos
17656 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17657 eassert (stop_pos >= Z - END_UNCHANGED);
17658 }
17659 }
17660 else if (last_unchanged_at_beg_row == NULL)
17661 GIVE_UP (19);
17662
17663
17664 #ifdef GLYPH_DEBUG
17665
17666 /* Either there is no unchanged row at the end, or the one we have
17667 now displays text. This is a necessary condition for the window
17668 end pos calculation at the end of this function. */
17669 eassert (first_unchanged_at_end_row == NULL
17670 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17671
17672 debug_last_unchanged_at_beg_vpos
17673 = (last_unchanged_at_beg_row
17674 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17675 : -1);
17676 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17677
17678 #endif /* GLYPH_DEBUG */
17679
17680
17681 /* Display new lines. Set last_text_row to the last new line
17682 displayed which has text on it, i.e. might end up as being the
17683 line where the window_end_vpos is. */
17684 w->cursor.vpos = -1;
17685 last_text_row = NULL;
17686 overlay_arrow_seen = 0;
17687 while (it.current_y < it.last_visible_y
17688 && !f->fonts_changed
17689 && (first_unchanged_at_end_row == NULL
17690 || IT_CHARPOS (it) < stop_pos))
17691 {
17692 if (display_line (&it))
17693 last_text_row = it.glyph_row - 1;
17694 }
17695
17696 if (f->fonts_changed)
17697 return -1;
17698
17699
17700 /* Compute differences in buffer positions, y-positions etc. for
17701 lines reused at the bottom of the window. Compute what we can
17702 scroll. */
17703 if (first_unchanged_at_end_row
17704 /* No lines reused because we displayed everything up to the
17705 bottom of the window. */
17706 && it.current_y < it.last_visible_y)
17707 {
17708 dvpos = (it.vpos
17709 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17710 current_matrix));
17711 dy = it.current_y - first_unchanged_at_end_row->y;
17712 run.current_y = first_unchanged_at_end_row->y;
17713 run.desired_y = run.current_y + dy;
17714 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17715 }
17716 else
17717 {
17718 delta = delta_bytes = dvpos = dy
17719 = run.current_y = run.desired_y = run.height = 0;
17720 first_unchanged_at_end_row = NULL;
17721 }
17722 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17723
17724
17725 /* Find the cursor if not already found. We have to decide whether
17726 PT will appear on this window (it sometimes doesn't, but this is
17727 not a very frequent case.) This decision has to be made before
17728 the current matrix is altered. A value of cursor.vpos < 0 means
17729 that PT is either in one of the lines beginning at
17730 first_unchanged_at_end_row or below the window. Don't care for
17731 lines that might be displayed later at the window end; as
17732 mentioned, this is not a frequent case. */
17733 if (w->cursor.vpos < 0)
17734 {
17735 /* Cursor in unchanged rows at the top? */
17736 if (PT < CHARPOS (start_pos)
17737 && last_unchanged_at_beg_row)
17738 {
17739 row = row_containing_pos (w, PT,
17740 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17741 last_unchanged_at_beg_row + 1, 0);
17742 if (row)
17743 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17744 }
17745
17746 /* Start from first_unchanged_at_end_row looking for PT. */
17747 else if (first_unchanged_at_end_row)
17748 {
17749 row = row_containing_pos (w, PT - delta,
17750 first_unchanged_at_end_row, NULL, 0);
17751 if (row)
17752 set_cursor_from_row (w, row, w->current_matrix, delta,
17753 delta_bytes, dy, dvpos);
17754 }
17755
17756 /* Give up if cursor was not found. */
17757 if (w->cursor.vpos < 0)
17758 {
17759 clear_glyph_matrix (w->desired_matrix);
17760 return -1;
17761 }
17762 }
17763
17764 /* Don't let the cursor end in the scroll margins. */
17765 {
17766 int this_scroll_margin, cursor_height;
17767 int frame_line_height = default_line_pixel_height (w);
17768 int window_total_lines
17769 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17770
17771 this_scroll_margin =
17772 max (0, min (scroll_margin, window_total_lines / 4));
17773 this_scroll_margin *= frame_line_height;
17774 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17775
17776 if ((w->cursor.y < this_scroll_margin
17777 && CHARPOS (start) > BEGV)
17778 /* Old redisplay didn't take scroll margin into account at the bottom,
17779 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17780 || (w->cursor.y + (make_cursor_line_fully_visible_p
17781 ? cursor_height + this_scroll_margin
17782 : 1)) > it.last_visible_y)
17783 {
17784 w->cursor.vpos = -1;
17785 clear_glyph_matrix (w->desired_matrix);
17786 return -1;
17787 }
17788 }
17789
17790 /* Scroll the display. Do it before changing the current matrix so
17791 that xterm.c doesn't get confused about where the cursor glyph is
17792 found. */
17793 if (dy && run.height)
17794 {
17795 update_begin (f);
17796
17797 if (FRAME_WINDOW_P (f))
17798 {
17799 FRAME_RIF (f)->update_window_begin_hook (w);
17800 FRAME_RIF (f)->clear_window_mouse_face (w);
17801 FRAME_RIF (f)->scroll_run_hook (w, &run);
17802 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17803 }
17804 else
17805 {
17806 /* Terminal frame. In this case, dvpos gives the number of
17807 lines to scroll by; dvpos < 0 means scroll up. */
17808 int from_vpos
17809 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17810 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17811 int end = (WINDOW_TOP_EDGE_LINE (w)
17812 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17813 + window_internal_height (w));
17814
17815 #if defined (HAVE_GPM) || defined (MSDOS)
17816 x_clear_window_mouse_face (w);
17817 #endif
17818 /* Perform the operation on the screen. */
17819 if (dvpos > 0)
17820 {
17821 /* Scroll last_unchanged_at_beg_row to the end of the
17822 window down dvpos lines. */
17823 set_terminal_window (f, end);
17824
17825 /* On dumb terminals delete dvpos lines at the end
17826 before inserting dvpos empty lines. */
17827 if (!FRAME_SCROLL_REGION_OK (f))
17828 ins_del_lines (f, end - dvpos, -dvpos);
17829
17830 /* Insert dvpos empty lines in front of
17831 last_unchanged_at_beg_row. */
17832 ins_del_lines (f, from, dvpos);
17833 }
17834 else if (dvpos < 0)
17835 {
17836 /* Scroll up last_unchanged_at_beg_vpos to the end of
17837 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17838 set_terminal_window (f, end);
17839
17840 /* Delete dvpos lines in front of
17841 last_unchanged_at_beg_vpos. ins_del_lines will set
17842 the cursor to the given vpos and emit |dvpos| delete
17843 line sequences. */
17844 ins_del_lines (f, from + dvpos, dvpos);
17845
17846 /* On a dumb terminal insert dvpos empty lines at the
17847 end. */
17848 if (!FRAME_SCROLL_REGION_OK (f))
17849 ins_del_lines (f, end + dvpos, -dvpos);
17850 }
17851
17852 set_terminal_window (f, 0);
17853 }
17854
17855 update_end (f);
17856 }
17857
17858 /* Shift reused rows of the current matrix to the right position.
17859 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17860 text. */
17861 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17862 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17863 if (dvpos < 0)
17864 {
17865 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17866 bottom_vpos, dvpos);
17867 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17868 bottom_vpos);
17869 }
17870 else if (dvpos > 0)
17871 {
17872 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17873 bottom_vpos, dvpos);
17874 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17875 first_unchanged_at_end_vpos + dvpos);
17876 }
17877
17878 /* For frame-based redisplay, make sure that current frame and window
17879 matrix are in sync with respect to glyph memory. */
17880 if (!FRAME_WINDOW_P (f))
17881 sync_frame_with_window_matrix_rows (w);
17882
17883 /* Adjust buffer positions in reused rows. */
17884 if (delta || delta_bytes)
17885 increment_matrix_positions (current_matrix,
17886 first_unchanged_at_end_vpos + dvpos,
17887 bottom_vpos, delta, delta_bytes);
17888
17889 /* Adjust Y positions. */
17890 if (dy)
17891 shift_glyph_matrix (w, current_matrix,
17892 first_unchanged_at_end_vpos + dvpos,
17893 bottom_vpos, dy);
17894
17895 if (first_unchanged_at_end_row)
17896 {
17897 first_unchanged_at_end_row += dvpos;
17898 if (first_unchanged_at_end_row->y >= it.last_visible_y
17899 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17900 first_unchanged_at_end_row = NULL;
17901 }
17902
17903 /* If scrolling up, there may be some lines to display at the end of
17904 the window. */
17905 last_text_row_at_end = NULL;
17906 if (dy < 0)
17907 {
17908 /* Scrolling up can leave for example a partially visible line
17909 at the end of the window to be redisplayed. */
17910 /* Set last_row to the glyph row in the current matrix where the
17911 window end line is found. It has been moved up or down in
17912 the matrix by dvpos. */
17913 int last_vpos = w->window_end_vpos + dvpos;
17914 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17915
17916 /* If last_row is the window end line, it should display text. */
17917 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17918
17919 /* If window end line was partially visible before, begin
17920 displaying at that line. Otherwise begin displaying with the
17921 line following it. */
17922 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17923 {
17924 init_to_row_start (&it, w, last_row);
17925 it.vpos = last_vpos;
17926 it.current_y = last_row->y;
17927 }
17928 else
17929 {
17930 init_to_row_end (&it, w, last_row);
17931 it.vpos = 1 + last_vpos;
17932 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17933 ++last_row;
17934 }
17935
17936 /* We may start in a continuation line. If so, we have to
17937 get the right continuation_lines_width and current_x. */
17938 it.continuation_lines_width = last_row->continuation_lines_width;
17939 it.hpos = it.current_x = 0;
17940
17941 /* Display the rest of the lines at the window end. */
17942 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17943 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17944 {
17945 /* Is it always sure that the display agrees with lines in
17946 the current matrix? I don't think so, so we mark rows
17947 displayed invalid in the current matrix by setting their
17948 enabled_p flag to zero. */
17949 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17950 if (display_line (&it))
17951 last_text_row_at_end = it.glyph_row - 1;
17952 }
17953 }
17954
17955 /* Update window_end_pos and window_end_vpos. */
17956 if (first_unchanged_at_end_row && !last_text_row_at_end)
17957 {
17958 /* Window end line if one of the preserved rows from the current
17959 matrix. Set row to the last row displaying text in current
17960 matrix starting at first_unchanged_at_end_row, after
17961 scrolling. */
17962 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17963 row = find_last_row_displaying_text (w->current_matrix, &it,
17964 first_unchanged_at_end_row);
17965 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17966 adjust_window_ends (w, row, 1);
17967 eassert (w->window_end_bytepos >= 0);
17968 IF_DEBUG (debug_method_add (w, "A"));
17969 }
17970 else if (last_text_row_at_end)
17971 {
17972 adjust_window_ends (w, last_text_row_at_end, 0);
17973 eassert (w->window_end_bytepos >= 0);
17974 IF_DEBUG (debug_method_add (w, "B"));
17975 }
17976 else if (last_text_row)
17977 {
17978 /* We have displayed either to the end of the window or at the
17979 end of the window, i.e. the last row with text is to be found
17980 in the desired matrix. */
17981 adjust_window_ends (w, last_text_row, 0);
17982 eassert (w->window_end_bytepos >= 0);
17983 }
17984 else if (first_unchanged_at_end_row == NULL
17985 && last_text_row == NULL
17986 && last_text_row_at_end == NULL)
17987 {
17988 /* Displayed to end of window, but no line containing text was
17989 displayed. Lines were deleted at the end of the window. */
17990 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17991 int vpos = w->window_end_vpos;
17992 struct glyph_row *current_row = current_matrix->rows + vpos;
17993 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17994
17995 for (row = NULL;
17996 row == NULL && vpos >= first_vpos;
17997 --vpos, --current_row, --desired_row)
17998 {
17999 if (desired_row->enabled_p)
18000 {
18001 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18002 row = desired_row;
18003 }
18004 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18005 row = current_row;
18006 }
18007
18008 eassert (row != NULL);
18009 w->window_end_vpos = vpos + 1;
18010 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18011 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18012 eassert (w->window_end_bytepos >= 0);
18013 IF_DEBUG (debug_method_add (w, "C"));
18014 }
18015 else
18016 emacs_abort ();
18017
18018 IF_DEBUG (debug_end_pos = w->window_end_pos;
18019 debug_end_vpos = w->window_end_vpos);
18020
18021 /* Record that display has not been completed. */
18022 w->window_end_valid = 0;
18023 w->desired_matrix->no_scrolling_p = 1;
18024 return 3;
18025
18026 #undef GIVE_UP
18027 }
18028
18029
18030 \f
18031 /***********************************************************************
18032 More debugging support
18033 ***********************************************************************/
18034
18035 #ifdef GLYPH_DEBUG
18036
18037 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18038 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18039 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18040
18041
18042 /* Dump the contents of glyph matrix MATRIX on stderr.
18043
18044 GLYPHS 0 means don't show glyph contents.
18045 GLYPHS 1 means show glyphs in short form
18046 GLYPHS > 1 means show glyphs in long form. */
18047
18048 void
18049 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18050 {
18051 int i;
18052 for (i = 0; i < matrix->nrows; ++i)
18053 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18054 }
18055
18056
18057 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18058 the glyph row and area where the glyph comes from. */
18059
18060 void
18061 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18062 {
18063 if (glyph->type == CHAR_GLYPH
18064 || glyph->type == GLYPHLESS_GLYPH)
18065 {
18066 fprintf (stderr,
18067 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18068 glyph - row->glyphs[TEXT_AREA],
18069 (glyph->type == CHAR_GLYPH
18070 ? 'C'
18071 : 'G'),
18072 glyph->charpos,
18073 (BUFFERP (glyph->object)
18074 ? 'B'
18075 : (STRINGP (glyph->object)
18076 ? 'S'
18077 : (INTEGERP (glyph->object)
18078 ? '0'
18079 : '-'))),
18080 glyph->pixel_width,
18081 glyph->u.ch,
18082 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18083 ? glyph->u.ch
18084 : '.'),
18085 glyph->face_id,
18086 glyph->left_box_line_p,
18087 glyph->right_box_line_p);
18088 }
18089 else if (glyph->type == STRETCH_GLYPH)
18090 {
18091 fprintf (stderr,
18092 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18093 glyph - row->glyphs[TEXT_AREA],
18094 'S',
18095 glyph->charpos,
18096 (BUFFERP (glyph->object)
18097 ? 'B'
18098 : (STRINGP (glyph->object)
18099 ? 'S'
18100 : (INTEGERP (glyph->object)
18101 ? '0'
18102 : '-'))),
18103 glyph->pixel_width,
18104 0,
18105 ' ',
18106 glyph->face_id,
18107 glyph->left_box_line_p,
18108 glyph->right_box_line_p);
18109 }
18110 else if (glyph->type == IMAGE_GLYPH)
18111 {
18112 fprintf (stderr,
18113 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18114 glyph - row->glyphs[TEXT_AREA],
18115 'I',
18116 glyph->charpos,
18117 (BUFFERP (glyph->object)
18118 ? 'B'
18119 : (STRINGP (glyph->object)
18120 ? 'S'
18121 : (INTEGERP (glyph->object)
18122 ? '0'
18123 : '-'))),
18124 glyph->pixel_width,
18125 glyph->u.img_id,
18126 '.',
18127 glyph->face_id,
18128 glyph->left_box_line_p,
18129 glyph->right_box_line_p);
18130 }
18131 else if (glyph->type == COMPOSITE_GLYPH)
18132 {
18133 fprintf (stderr,
18134 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18135 glyph - row->glyphs[TEXT_AREA],
18136 '+',
18137 glyph->charpos,
18138 (BUFFERP (glyph->object)
18139 ? 'B'
18140 : (STRINGP (glyph->object)
18141 ? 'S'
18142 : (INTEGERP (glyph->object)
18143 ? '0'
18144 : '-'))),
18145 glyph->pixel_width,
18146 glyph->u.cmp.id);
18147 if (glyph->u.cmp.automatic)
18148 fprintf (stderr,
18149 "[%d-%d]",
18150 glyph->slice.cmp.from, glyph->slice.cmp.to);
18151 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18152 glyph->face_id,
18153 glyph->left_box_line_p,
18154 glyph->right_box_line_p);
18155 }
18156 }
18157
18158
18159 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18160 GLYPHS 0 means don't show glyph contents.
18161 GLYPHS 1 means show glyphs in short form
18162 GLYPHS > 1 means show glyphs in long form. */
18163
18164 void
18165 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18166 {
18167 if (glyphs != 1)
18168 {
18169 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18170 fprintf (stderr, "==============================================================================\n");
18171
18172 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18173 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18174 vpos,
18175 MATRIX_ROW_START_CHARPOS (row),
18176 MATRIX_ROW_END_CHARPOS (row),
18177 row->used[TEXT_AREA],
18178 row->contains_overlapping_glyphs_p,
18179 row->enabled_p,
18180 row->truncated_on_left_p,
18181 row->truncated_on_right_p,
18182 row->continued_p,
18183 MATRIX_ROW_CONTINUATION_LINE_P (row),
18184 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18185 row->ends_at_zv_p,
18186 row->fill_line_p,
18187 row->ends_in_middle_of_char_p,
18188 row->starts_in_middle_of_char_p,
18189 row->mouse_face_p,
18190 row->x,
18191 row->y,
18192 row->pixel_width,
18193 row->height,
18194 row->visible_height,
18195 row->ascent,
18196 row->phys_ascent);
18197 /* The next 3 lines should align to "Start" in the header. */
18198 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18199 row->end.overlay_string_index,
18200 row->continuation_lines_width);
18201 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18202 CHARPOS (row->start.string_pos),
18203 CHARPOS (row->end.string_pos));
18204 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18205 row->end.dpvec_index);
18206 }
18207
18208 if (glyphs > 1)
18209 {
18210 int area;
18211
18212 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18213 {
18214 struct glyph *glyph = row->glyphs[area];
18215 struct glyph *glyph_end = glyph + row->used[area];
18216
18217 /* Glyph for a line end in text. */
18218 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18219 ++glyph_end;
18220
18221 if (glyph < glyph_end)
18222 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18223
18224 for (; glyph < glyph_end; ++glyph)
18225 dump_glyph (row, glyph, area);
18226 }
18227 }
18228 else if (glyphs == 1)
18229 {
18230 int area;
18231
18232 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18233 {
18234 char *s = alloca (row->used[area] + 4);
18235 int i;
18236
18237 for (i = 0; i < row->used[area]; ++i)
18238 {
18239 struct glyph *glyph = row->glyphs[area] + i;
18240 if (i == row->used[area] - 1
18241 && area == TEXT_AREA
18242 && INTEGERP (glyph->object)
18243 && glyph->type == CHAR_GLYPH
18244 && glyph->u.ch == ' ')
18245 {
18246 strcpy (&s[i], "[\\n]");
18247 i += 4;
18248 }
18249 else if (glyph->type == CHAR_GLYPH
18250 && glyph->u.ch < 0x80
18251 && glyph->u.ch >= ' ')
18252 s[i] = glyph->u.ch;
18253 else
18254 s[i] = '.';
18255 }
18256
18257 s[i] = '\0';
18258 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18259 }
18260 }
18261 }
18262
18263
18264 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18265 Sdump_glyph_matrix, 0, 1, "p",
18266 doc: /* Dump the current matrix of the selected window to stderr.
18267 Shows contents of glyph row structures. With non-nil
18268 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18269 glyphs in short form, otherwise show glyphs in long form. */)
18270 (Lisp_Object glyphs)
18271 {
18272 struct window *w = XWINDOW (selected_window);
18273 struct buffer *buffer = XBUFFER (w->contents);
18274
18275 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18276 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18277 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18278 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18279 fprintf (stderr, "=============================================\n");
18280 dump_glyph_matrix (w->current_matrix,
18281 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18282 return Qnil;
18283 }
18284
18285
18286 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18287 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18288 (void)
18289 {
18290 struct frame *f = XFRAME (selected_frame);
18291 dump_glyph_matrix (f->current_matrix, 1);
18292 return Qnil;
18293 }
18294
18295
18296 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18297 doc: /* Dump glyph row ROW to stderr.
18298 GLYPH 0 means don't dump glyphs.
18299 GLYPH 1 means dump glyphs in short form.
18300 GLYPH > 1 or omitted means dump glyphs in long form. */)
18301 (Lisp_Object row, Lisp_Object glyphs)
18302 {
18303 struct glyph_matrix *matrix;
18304 EMACS_INT vpos;
18305
18306 CHECK_NUMBER (row);
18307 matrix = XWINDOW (selected_window)->current_matrix;
18308 vpos = XINT (row);
18309 if (vpos >= 0 && vpos < matrix->nrows)
18310 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18311 vpos,
18312 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18313 return Qnil;
18314 }
18315
18316
18317 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18318 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18319 GLYPH 0 means don't dump glyphs.
18320 GLYPH 1 means dump glyphs in short form.
18321 GLYPH > 1 or omitted means dump glyphs in long form.
18322
18323 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18324 do nothing. */)
18325 (Lisp_Object row, Lisp_Object glyphs)
18326 {
18327 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18328 struct frame *sf = SELECTED_FRAME ();
18329 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18330 EMACS_INT vpos;
18331
18332 CHECK_NUMBER (row);
18333 vpos = XINT (row);
18334 if (vpos >= 0 && vpos < m->nrows)
18335 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18336 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18337 #endif
18338 return Qnil;
18339 }
18340
18341
18342 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18343 doc: /* Toggle tracing of redisplay.
18344 With ARG, turn tracing on if and only if ARG is positive. */)
18345 (Lisp_Object arg)
18346 {
18347 if (NILP (arg))
18348 trace_redisplay_p = !trace_redisplay_p;
18349 else
18350 {
18351 arg = Fprefix_numeric_value (arg);
18352 trace_redisplay_p = XINT (arg) > 0;
18353 }
18354
18355 return Qnil;
18356 }
18357
18358
18359 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18360 doc: /* Like `format', but print result to stderr.
18361 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18362 (ptrdiff_t nargs, Lisp_Object *args)
18363 {
18364 Lisp_Object s = Fformat (nargs, args);
18365 fprintf (stderr, "%s", SDATA (s));
18366 return Qnil;
18367 }
18368
18369 #endif /* GLYPH_DEBUG */
18370
18371
18372 \f
18373 /***********************************************************************
18374 Building Desired Matrix Rows
18375 ***********************************************************************/
18376
18377 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18378 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18379
18380 static struct glyph_row *
18381 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18382 {
18383 struct frame *f = XFRAME (WINDOW_FRAME (w));
18384 struct buffer *buffer = XBUFFER (w->contents);
18385 struct buffer *old = current_buffer;
18386 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18387 int arrow_len = SCHARS (overlay_arrow_string);
18388 const unsigned char *arrow_end = arrow_string + arrow_len;
18389 const unsigned char *p;
18390 struct it it;
18391 bool multibyte_p;
18392 int n_glyphs_before;
18393
18394 set_buffer_temp (buffer);
18395 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18396 it.glyph_row->used[TEXT_AREA] = 0;
18397 SET_TEXT_POS (it.position, 0, 0);
18398
18399 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18400 p = arrow_string;
18401 while (p < arrow_end)
18402 {
18403 Lisp_Object face, ilisp;
18404
18405 /* Get the next character. */
18406 if (multibyte_p)
18407 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18408 else
18409 {
18410 it.c = it.char_to_display = *p, it.len = 1;
18411 if (! ASCII_CHAR_P (it.c))
18412 it.char_to_display = BYTE8_TO_CHAR (it.c);
18413 }
18414 p += it.len;
18415
18416 /* Get its face. */
18417 ilisp = make_number (p - arrow_string);
18418 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18419 it.face_id = compute_char_face (f, it.char_to_display, face);
18420
18421 /* Compute its width, get its glyphs. */
18422 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18423 SET_TEXT_POS (it.position, -1, -1);
18424 PRODUCE_GLYPHS (&it);
18425
18426 /* If this character doesn't fit any more in the line, we have
18427 to remove some glyphs. */
18428 if (it.current_x > it.last_visible_x)
18429 {
18430 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18431 break;
18432 }
18433 }
18434
18435 set_buffer_temp (old);
18436 return it.glyph_row;
18437 }
18438
18439
18440 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18441 glyphs to insert is determined by produce_special_glyphs. */
18442
18443 static void
18444 insert_left_trunc_glyphs (struct it *it)
18445 {
18446 struct it truncate_it;
18447 struct glyph *from, *end, *to, *toend;
18448
18449 eassert (!FRAME_WINDOW_P (it->f)
18450 || (!it->glyph_row->reversed_p
18451 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18452 || (it->glyph_row->reversed_p
18453 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18454
18455 /* Get the truncation glyphs. */
18456 truncate_it = *it;
18457 truncate_it.current_x = 0;
18458 truncate_it.face_id = DEFAULT_FACE_ID;
18459 truncate_it.glyph_row = &scratch_glyph_row;
18460 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18461 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18462 truncate_it.object = make_number (0);
18463 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18464
18465 /* Overwrite glyphs from IT with truncation glyphs. */
18466 if (!it->glyph_row->reversed_p)
18467 {
18468 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18469
18470 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18471 end = from + tused;
18472 to = it->glyph_row->glyphs[TEXT_AREA];
18473 toend = to + it->glyph_row->used[TEXT_AREA];
18474 if (FRAME_WINDOW_P (it->f))
18475 {
18476 /* On GUI frames, when variable-size fonts are displayed,
18477 the truncation glyphs may need more pixels than the row's
18478 glyphs they overwrite. We overwrite more glyphs to free
18479 enough screen real estate, and enlarge the stretch glyph
18480 on the right (see display_line), if there is one, to
18481 preserve the screen position of the truncation glyphs on
18482 the right. */
18483 int w = 0;
18484 struct glyph *g = to;
18485 short used;
18486
18487 /* The first glyph could be partially visible, in which case
18488 it->glyph_row->x will be negative. But we want the left
18489 truncation glyphs to be aligned at the left margin of the
18490 window, so we override the x coordinate at which the row
18491 will begin. */
18492 it->glyph_row->x = 0;
18493 while (g < toend && w < it->truncation_pixel_width)
18494 {
18495 w += g->pixel_width;
18496 ++g;
18497 }
18498 if (g - to - tused > 0)
18499 {
18500 memmove (to + tused, g, (toend - g) * sizeof(*g));
18501 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18502 }
18503 used = it->glyph_row->used[TEXT_AREA];
18504 if (it->glyph_row->truncated_on_right_p
18505 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18506 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18507 == STRETCH_GLYPH)
18508 {
18509 int extra = w - it->truncation_pixel_width;
18510
18511 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18512 }
18513 }
18514
18515 while (from < end)
18516 *to++ = *from++;
18517
18518 /* There may be padding glyphs left over. Overwrite them too. */
18519 if (!FRAME_WINDOW_P (it->f))
18520 {
18521 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18522 {
18523 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18524 while (from < end)
18525 *to++ = *from++;
18526 }
18527 }
18528
18529 if (to > toend)
18530 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18531 }
18532 else
18533 {
18534 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18535
18536 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18537 that back to front. */
18538 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18539 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18540 toend = it->glyph_row->glyphs[TEXT_AREA];
18541 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18542 if (FRAME_WINDOW_P (it->f))
18543 {
18544 int w = 0;
18545 struct glyph *g = to;
18546
18547 while (g >= toend && w < it->truncation_pixel_width)
18548 {
18549 w += g->pixel_width;
18550 --g;
18551 }
18552 if (to - g - tused > 0)
18553 to = g + tused;
18554 if (it->glyph_row->truncated_on_right_p
18555 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18556 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18557 {
18558 int extra = w - it->truncation_pixel_width;
18559
18560 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18561 }
18562 }
18563
18564 while (from >= end && to >= toend)
18565 *to-- = *from--;
18566 if (!FRAME_WINDOW_P (it->f))
18567 {
18568 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18569 {
18570 from =
18571 truncate_it.glyph_row->glyphs[TEXT_AREA]
18572 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18573 while (from >= end && to >= toend)
18574 *to-- = *from--;
18575 }
18576 }
18577 if (from >= end)
18578 {
18579 /* Need to free some room before prepending additional
18580 glyphs. */
18581 int move_by = from - end + 1;
18582 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18583 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18584
18585 for ( ; g >= g0; g--)
18586 g[move_by] = *g;
18587 while (from >= end)
18588 *to-- = *from--;
18589 it->glyph_row->used[TEXT_AREA] += move_by;
18590 }
18591 }
18592 }
18593
18594 /* Compute the hash code for ROW. */
18595 unsigned
18596 row_hash (struct glyph_row *row)
18597 {
18598 int area, k;
18599 unsigned hashval = 0;
18600
18601 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18602 for (k = 0; k < row->used[area]; ++k)
18603 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18604 + row->glyphs[area][k].u.val
18605 + row->glyphs[area][k].face_id
18606 + row->glyphs[area][k].padding_p
18607 + (row->glyphs[area][k].type << 2));
18608
18609 return hashval;
18610 }
18611
18612 /* Compute the pixel height and width of IT->glyph_row.
18613
18614 Most of the time, ascent and height of a display line will be equal
18615 to the max_ascent and max_height values of the display iterator
18616 structure. This is not the case if
18617
18618 1. We hit ZV without displaying anything. In this case, max_ascent
18619 and max_height will be zero.
18620
18621 2. We have some glyphs that don't contribute to the line height.
18622 (The glyph row flag contributes_to_line_height_p is for future
18623 pixmap extensions).
18624
18625 The first case is easily covered by using default values because in
18626 these cases, the line height does not really matter, except that it
18627 must not be zero. */
18628
18629 static void
18630 compute_line_metrics (struct it *it)
18631 {
18632 struct glyph_row *row = it->glyph_row;
18633
18634 if (FRAME_WINDOW_P (it->f))
18635 {
18636 int i, min_y, max_y;
18637
18638 /* The line may consist of one space only, that was added to
18639 place the cursor on it. If so, the row's height hasn't been
18640 computed yet. */
18641 if (row->height == 0)
18642 {
18643 if (it->max_ascent + it->max_descent == 0)
18644 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18645 row->ascent = it->max_ascent;
18646 row->height = it->max_ascent + it->max_descent;
18647 row->phys_ascent = it->max_phys_ascent;
18648 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18649 row->extra_line_spacing = it->max_extra_line_spacing;
18650 }
18651
18652 /* Compute the width of this line. */
18653 row->pixel_width = row->x;
18654 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18655 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18656
18657 eassert (row->pixel_width >= 0);
18658 eassert (row->ascent >= 0 && row->height > 0);
18659
18660 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18661 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18662
18663 /* If first line's physical ascent is larger than its logical
18664 ascent, use the physical ascent, and make the row taller.
18665 This makes accented characters fully visible. */
18666 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18667 && row->phys_ascent > row->ascent)
18668 {
18669 row->height += row->phys_ascent - row->ascent;
18670 row->ascent = row->phys_ascent;
18671 }
18672
18673 /* Compute how much of the line is visible. */
18674 row->visible_height = row->height;
18675
18676 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18677 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18678
18679 if (row->y < min_y)
18680 row->visible_height -= min_y - row->y;
18681 if (row->y + row->height > max_y)
18682 row->visible_height -= row->y + row->height - max_y;
18683 }
18684 else
18685 {
18686 row->pixel_width = row->used[TEXT_AREA];
18687 if (row->continued_p)
18688 row->pixel_width -= it->continuation_pixel_width;
18689 else if (row->truncated_on_right_p)
18690 row->pixel_width -= it->truncation_pixel_width;
18691 row->ascent = row->phys_ascent = 0;
18692 row->height = row->phys_height = row->visible_height = 1;
18693 row->extra_line_spacing = 0;
18694 }
18695
18696 /* Compute a hash code for this row. */
18697 row->hash = row_hash (row);
18698
18699 it->max_ascent = it->max_descent = 0;
18700 it->max_phys_ascent = it->max_phys_descent = 0;
18701 }
18702
18703
18704 /* Append one space to the glyph row of iterator IT if doing a
18705 window-based redisplay. The space has the same face as
18706 IT->face_id. Value is non-zero if a space was added.
18707
18708 This function is called to make sure that there is always one glyph
18709 at the end of a glyph row that the cursor can be set on under
18710 window-systems. (If there weren't such a glyph we would not know
18711 how wide and tall a box cursor should be displayed).
18712
18713 At the same time this space let's a nicely handle clearing to the
18714 end of the line if the row ends in italic text. */
18715
18716 static int
18717 append_space_for_newline (struct it *it, int default_face_p)
18718 {
18719 if (FRAME_WINDOW_P (it->f))
18720 {
18721 int n = it->glyph_row->used[TEXT_AREA];
18722
18723 if (it->glyph_row->glyphs[TEXT_AREA] + n
18724 < it->glyph_row->glyphs[1 + TEXT_AREA])
18725 {
18726 /* Save some values that must not be changed.
18727 Must save IT->c and IT->len because otherwise
18728 ITERATOR_AT_END_P wouldn't work anymore after
18729 append_space_for_newline has been called. */
18730 enum display_element_type saved_what = it->what;
18731 int saved_c = it->c, saved_len = it->len;
18732 int saved_char_to_display = it->char_to_display;
18733 int saved_x = it->current_x;
18734 int saved_face_id = it->face_id;
18735 int saved_box_end = it->end_of_box_run_p;
18736 struct text_pos saved_pos;
18737 Lisp_Object saved_object;
18738 struct face *face;
18739
18740 saved_object = it->object;
18741 saved_pos = it->position;
18742
18743 it->what = IT_CHARACTER;
18744 memset (&it->position, 0, sizeof it->position);
18745 it->object = make_number (0);
18746 it->c = it->char_to_display = ' ';
18747 it->len = 1;
18748
18749 /* If the default face was remapped, be sure to use the
18750 remapped face for the appended newline. */
18751 if (default_face_p)
18752 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18753 else if (it->face_before_selective_p)
18754 it->face_id = it->saved_face_id;
18755 face = FACE_FROM_ID (it->f, it->face_id);
18756 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18757 /* In R2L rows, we will prepend a stretch glyph that will
18758 have the end_of_box_run_p flag set for it, so there's no
18759 need for the appended newline glyph to have that flag
18760 set. */
18761 if (it->glyph_row->reversed_p
18762 /* But if the appended newline glyph goes all the way to
18763 the end of the row, there will be no stretch glyph,
18764 so leave the box flag set. */
18765 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18766 it->end_of_box_run_p = 0;
18767
18768 PRODUCE_GLYPHS (it);
18769
18770 it->override_ascent = -1;
18771 it->constrain_row_ascent_descent_p = 0;
18772 it->current_x = saved_x;
18773 it->object = saved_object;
18774 it->position = saved_pos;
18775 it->what = saved_what;
18776 it->face_id = saved_face_id;
18777 it->len = saved_len;
18778 it->c = saved_c;
18779 it->char_to_display = saved_char_to_display;
18780 it->end_of_box_run_p = saved_box_end;
18781 return 1;
18782 }
18783 }
18784
18785 return 0;
18786 }
18787
18788
18789 /* Extend the face of the last glyph in the text area of IT->glyph_row
18790 to the end of the display line. Called from display_line. If the
18791 glyph row is empty, add a space glyph to it so that we know the
18792 face to draw. Set the glyph row flag fill_line_p. If the glyph
18793 row is R2L, prepend a stretch glyph to cover the empty space to the
18794 left of the leftmost glyph. */
18795
18796 static void
18797 extend_face_to_end_of_line (struct it *it)
18798 {
18799 struct face *face, *default_face;
18800 struct frame *f = it->f;
18801
18802 /* If line is already filled, do nothing. Non window-system frames
18803 get a grace of one more ``pixel'' because their characters are
18804 1-``pixel'' wide, so they hit the equality too early. This grace
18805 is needed only for R2L rows that are not continued, to produce
18806 one extra blank where we could display the cursor. */
18807 if (it->current_x >= it->last_visible_x
18808 + (!FRAME_WINDOW_P (f)
18809 && it->glyph_row->reversed_p
18810 && !it->glyph_row->continued_p))
18811 return;
18812
18813 /* The default face, possibly remapped. */
18814 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18815
18816 /* Face extension extends the background and box of IT->face_id
18817 to the end of the line. If the background equals the background
18818 of the frame, we don't have to do anything. */
18819 if (it->face_before_selective_p)
18820 face = FACE_FROM_ID (f, it->saved_face_id);
18821 else
18822 face = FACE_FROM_ID (f, it->face_id);
18823
18824 if (FRAME_WINDOW_P (f)
18825 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18826 && face->box == FACE_NO_BOX
18827 && face->background == FRAME_BACKGROUND_PIXEL (f)
18828 #ifdef HAVE_WINDOW_SYSTEM
18829 && !face->stipple
18830 #endif
18831 && !it->glyph_row->reversed_p)
18832 return;
18833
18834 /* Set the glyph row flag indicating that the face of the last glyph
18835 in the text area has to be drawn to the end of the text area. */
18836 it->glyph_row->fill_line_p = 1;
18837
18838 /* If current character of IT is not ASCII, make sure we have the
18839 ASCII face. This will be automatically undone the next time
18840 get_next_display_element returns a multibyte character. Note
18841 that the character will always be single byte in unibyte
18842 text. */
18843 if (!ASCII_CHAR_P (it->c))
18844 {
18845 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18846 }
18847
18848 if (FRAME_WINDOW_P (f))
18849 {
18850 /* If the row is empty, add a space with the current face of IT,
18851 so that we know which face to draw. */
18852 if (it->glyph_row->used[TEXT_AREA] == 0)
18853 {
18854 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18855 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18856 it->glyph_row->used[TEXT_AREA] = 1;
18857 }
18858 #ifdef HAVE_WINDOW_SYSTEM
18859 if (it->glyph_row->reversed_p)
18860 {
18861 /* Prepend a stretch glyph to the row, such that the
18862 rightmost glyph will be drawn flushed all the way to the
18863 right margin of the window. The stretch glyph that will
18864 occupy the empty space, if any, to the left of the
18865 glyphs. */
18866 struct font *font = face->font ? face->font : FRAME_FONT (f);
18867 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18868 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18869 struct glyph *g;
18870 int row_width, stretch_ascent, stretch_width;
18871 struct text_pos saved_pos;
18872 int saved_face_id, saved_avoid_cursor, saved_box_start;
18873
18874 for (row_width = 0, g = row_start; g < row_end; g++)
18875 row_width += g->pixel_width;
18876 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18877 if (stretch_width > 0)
18878 {
18879 stretch_ascent =
18880 (((it->ascent + it->descent)
18881 * FONT_BASE (font)) / FONT_HEIGHT (font));
18882 saved_pos = it->position;
18883 memset (&it->position, 0, sizeof it->position);
18884 saved_avoid_cursor = it->avoid_cursor_p;
18885 it->avoid_cursor_p = 1;
18886 saved_face_id = it->face_id;
18887 saved_box_start = it->start_of_box_run_p;
18888 /* The last row's stretch glyph should get the default
18889 face, to avoid painting the rest of the window with
18890 the region face, if the region ends at ZV. */
18891 if (it->glyph_row->ends_at_zv_p)
18892 it->face_id = default_face->id;
18893 else
18894 it->face_id = face->id;
18895 it->start_of_box_run_p = 0;
18896 append_stretch_glyph (it, make_number (0), stretch_width,
18897 it->ascent + it->descent, stretch_ascent);
18898 it->position = saved_pos;
18899 it->avoid_cursor_p = saved_avoid_cursor;
18900 it->face_id = saved_face_id;
18901 it->start_of_box_run_p = saved_box_start;
18902 }
18903 }
18904 #endif /* HAVE_WINDOW_SYSTEM */
18905 }
18906 else
18907 {
18908 /* Save some values that must not be changed. */
18909 int saved_x = it->current_x;
18910 struct text_pos saved_pos;
18911 Lisp_Object saved_object;
18912 enum display_element_type saved_what = it->what;
18913 int saved_face_id = it->face_id;
18914
18915 saved_object = it->object;
18916 saved_pos = it->position;
18917
18918 it->what = IT_CHARACTER;
18919 memset (&it->position, 0, sizeof it->position);
18920 it->object = make_number (0);
18921 it->c = it->char_to_display = ' ';
18922 it->len = 1;
18923 /* The last row's blank glyphs should get the default face, to
18924 avoid painting the rest of the window with the region face,
18925 if the region ends at ZV. */
18926 if (it->glyph_row->ends_at_zv_p)
18927 it->face_id = default_face->id;
18928 else
18929 it->face_id = face->id;
18930
18931 PRODUCE_GLYPHS (it);
18932
18933 while (it->current_x <= it->last_visible_x)
18934 PRODUCE_GLYPHS (it);
18935
18936 /* Don't count these blanks really. It would let us insert a left
18937 truncation glyph below and make us set the cursor on them, maybe. */
18938 it->current_x = saved_x;
18939 it->object = saved_object;
18940 it->position = saved_pos;
18941 it->what = saved_what;
18942 it->face_id = saved_face_id;
18943 }
18944 }
18945
18946
18947 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18948 trailing whitespace. */
18949
18950 static int
18951 trailing_whitespace_p (ptrdiff_t charpos)
18952 {
18953 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18954 int c = 0;
18955
18956 while (bytepos < ZV_BYTE
18957 && (c = FETCH_CHAR (bytepos),
18958 c == ' ' || c == '\t'))
18959 ++bytepos;
18960
18961 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18962 {
18963 if (bytepos != PT_BYTE)
18964 return 1;
18965 }
18966 return 0;
18967 }
18968
18969
18970 /* Highlight trailing whitespace, if any, in ROW. */
18971
18972 static void
18973 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18974 {
18975 int used = row->used[TEXT_AREA];
18976
18977 if (used)
18978 {
18979 struct glyph *start = row->glyphs[TEXT_AREA];
18980 struct glyph *glyph = start + used - 1;
18981
18982 if (row->reversed_p)
18983 {
18984 /* Right-to-left rows need to be processed in the opposite
18985 direction, so swap the edge pointers. */
18986 glyph = start;
18987 start = row->glyphs[TEXT_AREA] + used - 1;
18988 }
18989
18990 /* Skip over glyphs inserted to display the cursor at the
18991 end of a line, for extending the face of the last glyph
18992 to the end of the line on terminals, and for truncation
18993 and continuation glyphs. */
18994 if (!row->reversed_p)
18995 {
18996 while (glyph >= start
18997 && glyph->type == CHAR_GLYPH
18998 && INTEGERP (glyph->object))
18999 --glyph;
19000 }
19001 else
19002 {
19003 while (glyph <= start
19004 && glyph->type == CHAR_GLYPH
19005 && INTEGERP (glyph->object))
19006 ++glyph;
19007 }
19008
19009 /* If last glyph is a space or stretch, and it's trailing
19010 whitespace, set the face of all trailing whitespace glyphs in
19011 IT->glyph_row to `trailing-whitespace'. */
19012 if ((row->reversed_p ? glyph <= start : glyph >= start)
19013 && BUFFERP (glyph->object)
19014 && (glyph->type == STRETCH_GLYPH
19015 || (glyph->type == CHAR_GLYPH
19016 && glyph->u.ch == ' '))
19017 && trailing_whitespace_p (glyph->charpos))
19018 {
19019 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19020 if (face_id < 0)
19021 return;
19022
19023 if (!row->reversed_p)
19024 {
19025 while (glyph >= start
19026 && BUFFERP (glyph->object)
19027 && (glyph->type == STRETCH_GLYPH
19028 || (glyph->type == CHAR_GLYPH
19029 && glyph->u.ch == ' ')))
19030 (glyph--)->face_id = face_id;
19031 }
19032 else
19033 {
19034 while (glyph <= start
19035 && BUFFERP (glyph->object)
19036 && (glyph->type == STRETCH_GLYPH
19037 || (glyph->type == CHAR_GLYPH
19038 && glyph->u.ch == ' ')))
19039 (glyph++)->face_id = face_id;
19040 }
19041 }
19042 }
19043 }
19044
19045
19046 /* Value is non-zero if glyph row ROW should be
19047 considered to hold the buffer position CHARPOS. */
19048
19049 static int
19050 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19051 {
19052 int result = 1;
19053
19054 if (charpos == CHARPOS (row->end.pos)
19055 || charpos == MATRIX_ROW_END_CHARPOS (row))
19056 {
19057 /* Suppose the row ends on a string.
19058 Unless the row is continued, that means it ends on a newline
19059 in the string. If it's anything other than a display string
19060 (e.g., a before-string from an overlay), we don't want the
19061 cursor there. (This heuristic seems to give the optimal
19062 behavior for the various types of multi-line strings.)
19063 One exception: if the string has `cursor' property on one of
19064 its characters, we _do_ want the cursor there. */
19065 if (CHARPOS (row->end.string_pos) >= 0)
19066 {
19067 if (row->continued_p)
19068 result = 1;
19069 else
19070 {
19071 /* Check for `display' property. */
19072 struct glyph *beg = row->glyphs[TEXT_AREA];
19073 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19074 struct glyph *glyph;
19075
19076 result = 0;
19077 for (glyph = end; glyph >= beg; --glyph)
19078 if (STRINGP (glyph->object))
19079 {
19080 Lisp_Object prop
19081 = Fget_char_property (make_number (charpos),
19082 Qdisplay, Qnil);
19083 result =
19084 (!NILP (prop)
19085 && display_prop_string_p (prop, glyph->object));
19086 /* If there's a `cursor' property on one of the
19087 string's characters, this row is a cursor row,
19088 even though this is not a display string. */
19089 if (!result)
19090 {
19091 Lisp_Object s = glyph->object;
19092
19093 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19094 {
19095 ptrdiff_t gpos = glyph->charpos;
19096
19097 if (!NILP (Fget_char_property (make_number (gpos),
19098 Qcursor, s)))
19099 {
19100 result = 1;
19101 break;
19102 }
19103 }
19104 }
19105 break;
19106 }
19107 }
19108 }
19109 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19110 {
19111 /* If the row ends in middle of a real character,
19112 and the line is continued, we want the cursor here.
19113 That's because CHARPOS (ROW->end.pos) would equal
19114 PT if PT is before the character. */
19115 if (!row->ends_in_ellipsis_p)
19116 result = row->continued_p;
19117 else
19118 /* If the row ends in an ellipsis, then
19119 CHARPOS (ROW->end.pos) will equal point after the
19120 invisible text. We want that position to be displayed
19121 after the ellipsis. */
19122 result = 0;
19123 }
19124 /* If the row ends at ZV, display the cursor at the end of that
19125 row instead of at the start of the row below. */
19126 else if (row->ends_at_zv_p)
19127 result = 1;
19128 else
19129 result = 0;
19130 }
19131
19132 return result;
19133 }
19134
19135 /* Value is non-zero if glyph row ROW should be
19136 used to hold the cursor. */
19137
19138 static int
19139 cursor_row_p (struct glyph_row *row)
19140 {
19141 return row_for_charpos_p (row, PT);
19142 }
19143
19144 \f
19145
19146 /* Push the property PROP so that it will be rendered at the current
19147 position in IT. Return 1 if PROP was successfully pushed, 0
19148 otherwise. Called from handle_line_prefix to handle the
19149 `line-prefix' and `wrap-prefix' properties. */
19150
19151 static int
19152 push_prefix_prop (struct it *it, Lisp_Object prop)
19153 {
19154 struct text_pos pos =
19155 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19156
19157 eassert (it->method == GET_FROM_BUFFER
19158 || it->method == GET_FROM_DISPLAY_VECTOR
19159 || it->method == GET_FROM_STRING);
19160
19161 /* We need to save the current buffer/string position, so it will be
19162 restored by pop_it, because iterate_out_of_display_property
19163 depends on that being set correctly, but some situations leave
19164 it->position not yet set when this function is called. */
19165 push_it (it, &pos);
19166
19167 if (STRINGP (prop))
19168 {
19169 if (SCHARS (prop) == 0)
19170 {
19171 pop_it (it);
19172 return 0;
19173 }
19174
19175 it->string = prop;
19176 it->string_from_prefix_prop_p = 1;
19177 it->multibyte_p = STRING_MULTIBYTE (it->string);
19178 it->current.overlay_string_index = -1;
19179 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19180 it->end_charpos = it->string_nchars = SCHARS (it->string);
19181 it->method = GET_FROM_STRING;
19182 it->stop_charpos = 0;
19183 it->prev_stop = 0;
19184 it->base_level_stop = 0;
19185
19186 /* Force paragraph direction to be that of the parent
19187 buffer/string. */
19188 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19189 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19190 else
19191 it->paragraph_embedding = L2R;
19192
19193 /* Set up the bidi iterator for this display string. */
19194 if (it->bidi_p)
19195 {
19196 it->bidi_it.string.lstring = it->string;
19197 it->bidi_it.string.s = NULL;
19198 it->bidi_it.string.schars = it->end_charpos;
19199 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19200 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19201 it->bidi_it.string.unibyte = !it->multibyte_p;
19202 it->bidi_it.w = it->w;
19203 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19204 }
19205 }
19206 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19207 {
19208 it->method = GET_FROM_STRETCH;
19209 it->object = prop;
19210 }
19211 #ifdef HAVE_WINDOW_SYSTEM
19212 else if (IMAGEP (prop))
19213 {
19214 it->what = IT_IMAGE;
19215 it->image_id = lookup_image (it->f, prop);
19216 it->method = GET_FROM_IMAGE;
19217 }
19218 #endif /* HAVE_WINDOW_SYSTEM */
19219 else
19220 {
19221 pop_it (it); /* bogus display property, give up */
19222 return 0;
19223 }
19224
19225 return 1;
19226 }
19227
19228 /* Return the character-property PROP at the current position in IT. */
19229
19230 static Lisp_Object
19231 get_it_property (struct it *it, Lisp_Object prop)
19232 {
19233 Lisp_Object position, object = it->object;
19234
19235 if (STRINGP (object))
19236 position = make_number (IT_STRING_CHARPOS (*it));
19237 else if (BUFFERP (object))
19238 {
19239 position = make_number (IT_CHARPOS (*it));
19240 object = it->window;
19241 }
19242 else
19243 return Qnil;
19244
19245 return Fget_char_property (position, prop, object);
19246 }
19247
19248 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19249
19250 static void
19251 handle_line_prefix (struct it *it)
19252 {
19253 Lisp_Object prefix;
19254
19255 if (it->continuation_lines_width > 0)
19256 {
19257 prefix = get_it_property (it, Qwrap_prefix);
19258 if (NILP (prefix))
19259 prefix = Vwrap_prefix;
19260 }
19261 else
19262 {
19263 prefix = get_it_property (it, Qline_prefix);
19264 if (NILP (prefix))
19265 prefix = Vline_prefix;
19266 }
19267 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19268 {
19269 /* If the prefix is wider than the window, and we try to wrap
19270 it, it would acquire its own wrap prefix, and so on till the
19271 iterator stack overflows. So, don't wrap the prefix. */
19272 it->line_wrap = TRUNCATE;
19273 it->avoid_cursor_p = 1;
19274 }
19275 }
19276
19277 \f
19278
19279 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19280 only for R2L lines from display_line and display_string, when they
19281 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19282 the line/string needs to be continued on the next glyph row. */
19283 static void
19284 unproduce_glyphs (struct it *it, int n)
19285 {
19286 struct glyph *glyph, *end;
19287
19288 eassert (it->glyph_row);
19289 eassert (it->glyph_row->reversed_p);
19290 eassert (it->area == TEXT_AREA);
19291 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19292
19293 if (n > it->glyph_row->used[TEXT_AREA])
19294 n = it->glyph_row->used[TEXT_AREA];
19295 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19296 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19297 for ( ; glyph < end; glyph++)
19298 glyph[-n] = *glyph;
19299 }
19300
19301 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19302 and ROW->maxpos. */
19303 static void
19304 find_row_edges (struct it *it, struct glyph_row *row,
19305 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19306 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19307 {
19308 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19309 lines' rows is implemented for bidi-reordered rows. */
19310
19311 /* ROW->minpos is the value of min_pos, the minimal buffer position
19312 we have in ROW, or ROW->start.pos if that is smaller. */
19313 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19314 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19315 else
19316 /* We didn't find buffer positions smaller than ROW->start, or
19317 didn't find _any_ valid buffer positions in any of the glyphs,
19318 so we must trust the iterator's computed positions. */
19319 row->minpos = row->start.pos;
19320 if (max_pos <= 0)
19321 {
19322 max_pos = CHARPOS (it->current.pos);
19323 max_bpos = BYTEPOS (it->current.pos);
19324 }
19325
19326 /* Here are the various use-cases for ending the row, and the
19327 corresponding values for ROW->maxpos:
19328
19329 Line ends in a newline from buffer eol_pos + 1
19330 Line is continued from buffer max_pos + 1
19331 Line is truncated on right it->current.pos
19332 Line ends in a newline from string max_pos + 1(*)
19333 (*) + 1 only when line ends in a forward scan
19334 Line is continued from string max_pos
19335 Line is continued from display vector max_pos
19336 Line is entirely from a string min_pos == max_pos
19337 Line is entirely from a display vector min_pos == max_pos
19338 Line that ends at ZV ZV
19339
19340 If you discover other use-cases, please add them here as
19341 appropriate. */
19342 if (row->ends_at_zv_p)
19343 row->maxpos = it->current.pos;
19344 else if (row->used[TEXT_AREA])
19345 {
19346 int seen_this_string = 0;
19347 struct glyph_row *r1 = row - 1;
19348
19349 /* Did we see the same display string on the previous row? */
19350 if (STRINGP (it->object)
19351 /* this is not the first row */
19352 && row > it->w->desired_matrix->rows
19353 /* previous row is not the header line */
19354 && !r1->mode_line_p
19355 /* previous row also ends in a newline from a string */
19356 && r1->ends_in_newline_from_string_p)
19357 {
19358 struct glyph *start, *end;
19359
19360 /* Search for the last glyph of the previous row that came
19361 from buffer or string. Depending on whether the row is
19362 L2R or R2L, we need to process it front to back or the
19363 other way round. */
19364 if (!r1->reversed_p)
19365 {
19366 start = r1->glyphs[TEXT_AREA];
19367 end = start + r1->used[TEXT_AREA];
19368 /* Glyphs inserted by redisplay have an integer (zero)
19369 as their object. */
19370 while (end > start
19371 && INTEGERP ((end - 1)->object)
19372 && (end - 1)->charpos <= 0)
19373 --end;
19374 if (end > start)
19375 {
19376 if (EQ ((end - 1)->object, it->object))
19377 seen_this_string = 1;
19378 }
19379 else
19380 /* If all the glyphs of the previous row were inserted
19381 by redisplay, it means the previous row was
19382 produced from a single newline, which is only
19383 possible if that newline came from the same string
19384 as the one which produced this ROW. */
19385 seen_this_string = 1;
19386 }
19387 else
19388 {
19389 end = r1->glyphs[TEXT_AREA] - 1;
19390 start = end + r1->used[TEXT_AREA];
19391 while (end < start
19392 && INTEGERP ((end + 1)->object)
19393 && (end + 1)->charpos <= 0)
19394 ++end;
19395 if (end < start)
19396 {
19397 if (EQ ((end + 1)->object, it->object))
19398 seen_this_string = 1;
19399 }
19400 else
19401 seen_this_string = 1;
19402 }
19403 }
19404 /* Take note of each display string that covers a newline only
19405 once, the first time we see it. This is for when a display
19406 string includes more than one newline in it. */
19407 if (row->ends_in_newline_from_string_p && !seen_this_string)
19408 {
19409 /* If we were scanning the buffer forward when we displayed
19410 the string, we want to account for at least one buffer
19411 position that belongs to this row (position covered by
19412 the display string), so that cursor positioning will
19413 consider this row as a candidate when point is at the end
19414 of the visual line represented by this row. This is not
19415 required when scanning back, because max_pos will already
19416 have a much larger value. */
19417 if (CHARPOS (row->end.pos) > max_pos)
19418 INC_BOTH (max_pos, max_bpos);
19419 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19420 }
19421 else if (CHARPOS (it->eol_pos) > 0)
19422 SET_TEXT_POS (row->maxpos,
19423 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19424 else if (row->continued_p)
19425 {
19426 /* If max_pos is different from IT's current position, it
19427 means IT->method does not belong to the display element
19428 at max_pos. However, it also means that the display
19429 element at max_pos was displayed in its entirety on this
19430 line, which is equivalent to saying that the next line
19431 starts at the next buffer position. */
19432 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19433 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19434 else
19435 {
19436 INC_BOTH (max_pos, max_bpos);
19437 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19438 }
19439 }
19440 else if (row->truncated_on_right_p)
19441 /* display_line already called reseat_at_next_visible_line_start,
19442 which puts the iterator at the beginning of the next line, in
19443 the logical order. */
19444 row->maxpos = it->current.pos;
19445 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19446 /* A line that is entirely from a string/image/stretch... */
19447 row->maxpos = row->minpos;
19448 else
19449 emacs_abort ();
19450 }
19451 else
19452 row->maxpos = it->current.pos;
19453 }
19454
19455 /* Construct the glyph row IT->glyph_row in the desired matrix of
19456 IT->w from text at the current position of IT. See dispextern.h
19457 for an overview of struct it. Value is non-zero if
19458 IT->glyph_row displays text, as opposed to a line displaying ZV
19459 only. */
19460
19461 static int
19462 display_line (struct it *it)
19463 {
19464 struct glyph_row *row = it->glyph_row;
19465 Lisp_Object overlay_arrow_string;
19466 struct it wrap_it;
19467 void *wrap_data = NULL;
19468 int may_wrap = 0, wrap_x IF_LINT (= 0);
19469 int wrap_row_used = -1;
19470 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19471 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19472 int wrap_row_extra_line_spacing IF_LINT (= 0);
19473 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19474 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19475 int cvpos;
19476 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19477 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19478
19479 /* We always start displaying at hpos zero even if hscrolled. */
19480 eassert (it->hpos == 0 && it->current_x == 0);
19481
19482 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19483 >= it->w->desired_matrix->nrows)
19484 {
19485 it->w->nrows_scale_factor++;
19486 it->f->fonts_changed = 1;
19487 return 0;
19488 }
19489
19490 /* Clear the result glyph row and enable it. */
19491 prepare_desired_row (row);
19492
19493 row->y = it->current_y;
19494 row->start = it->start;
19495 row->continuation_lines_width = it->continuation_lines_width;
19496 row->displays_text_p = 1;
19497 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19498 it->starts_in_middle_of_char_p = 0;
19499
19500 /* Arrange the overlays nicely for our purposes. Usually, we call
19501 display_line on only one line at a time, in which case this
19502 can't really hurt too much, or we call it on lines which appear
19503 one after another in the buffer, in which case all calls to
19504 recenter_overlay_lists but the first will be pretty cheap. */
19505 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19506
19507 /* Move over display elements that are not visible because we are
19508 hscrolled. This may stop at an x-position < IT->first_visible_x
19509 if the first glyph is partially visible or if we hit a line end. */
19510 if (it->current_x < it->first_visible_x)
19511 {
19512 enum move_it_result move_result;
19513
19514 this_line_min_pos = row->start.pos;
19515 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19516 MOVE_TO_POS | MOVE_TO_X);
19517 /* If we are under a large hscroll, move_it_in_display_line_to
19518 could hit the end of the line without reaching
19519 it->first_visible_x. Pretend that we did reach it. This is
19520 especially important on a TTY, where we will call
19521 extend_face_to_end_of_line, which needs to know how many
19522 blank glyphs to produce. */
19523 if (it->current_x < it->first_visible_x
19524 && (move_result == MOVE_NEWLINE_OR_CR
19525 || move_result == MOVE_POS_MATCH_OR_ZV))
19526 it->current_x = it->first_visible_x;
19527
19528 /* Record the smallest positions seen while we moved over
19529 display elements that are not visible. This is needed by
19530 redisplay_internal for optimizing the case where the cursor
19531 stays inside the same line. The rest of this function only
19532 considers positions that are actually displayed, so
19533 RECORD_MAX_MIN_POS will not otherwise record positions that
19534 are hscrolled to the left of the left edge of the window. */
19535 min_pos = CHARPOS (this_line_min_pos);
19536 min_bpos = BYTEPOS (this_line_min_pos);
19537 }
19538 else
19539 {
19540 /* We only do this when not calling `move_it_in_display_line_to'
19541 above, because move_it_in_display_line_to calls
19542 handle_line_prefix itself. */
19543 handle_line_prefix (it);
19544 }
19545
19546 /* Get the initial row height. This is either the height of the
19547 text hscrolled, if there is any, or zero. */
19548 row->ascent = it->max_ascent;
19549 row->height = it->max_ascent + it->max_descent;
19550 row->phys_ascent = it->max_phys_ascent;
19551 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19552 row->extra_line_spacing = it->max_extra_line_spacing;
19553
19554 /* Utility macro to record max and min buffer positions seen until now. */
19555 #define RECORD_MAX_MIN_POS(IT) \
19556 do \
19557 { \
19558 int composition_p = !STRINGP ((IT)->string) \
19559 && ((IT)->what == IT_COMPOSITION); \
19560 ptrdiff_t current_pos = \
19561 composition_p ? (IT)->cmp_it.charpos \
19562 : IT_CHARPOS (*(IT)); \
19563 ptrdiff_t current_bpos = \
19564 composition_p ? CHAR_TO_BYTE (current_pos) \
19565 : IT_BYTEPOS (*(IT)); \
19566 if (current_pos < min_pos) \
19567 { \
19568 min_pos = current_pos; \
19569 min_bpos = current_bpos; \
19570 } \
19571 if (IT_CHARPOS (*it) > max_pos) \
19572 { \
19573 max_pos = IT_CHARPOS (*it); \
19574 max_bpos = IT_BYTEPOS (*it); \
19575 } \
19576 } \
19577 while (0)
19578
19579 /* Loop generating characters. The loop is left with IT on the next
19580 character to display. */
19581 while (1)
19582 {
19583 int n_glyphs_before, hpos_before, x_before;
19584 int x, nglyphs;
19585 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19586
19587 /* Retrieve the next thing to display. Value is zero if end of
19588 buffer reached. */
19589 if (!get_next_display_element (it))
19590 {
19591 /* Maybe add a space at the end of this line that is used to
19592 display the cursor there under X. Set the charpos of the
19593 first glyph of blank lines not corresponding to any text
19594 to -1. */
19595 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19596 row->exact_window_width_line_p = 1;
19597 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19598 || row->used[TEXT_AREA] == 0)
19599 {
19600 row->glyphs[TEXT_AREA]->charpos = -1;
19601 row->displays_text_p = 0;
19602
19603 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19604 && (!MINI_WINDOW_P (it->w)
19605 || (minibuf_level && EQ (it->window, minibuf_window))))
19606 row->indicate_empty_line_p = 1;
19607 }
19608
19609 it->continuation_lines_width = 0;
19610 row->ends_at_zv_p = 1;
19611 /* A row that displays right-to-left text must always have
19612 its last face extended all the way to the end of line,
19613 even if this row ends in ZV, because we still write to
19614 the screen left to right. We also need to extend the
19615 last face if the default face is remapped to some
19616 different face, otherwise the functions that clear
19617 portions of the screen will clear with the default face's
19618 background color. */
19619 if (row->reversed_p
19620 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19621 extend_face_to_end_of_line (it);
19622 break;
19623 }
19624
19625 /* Now, get the metrics of what we want to display. This also
19626 generates glyphs in `row' (which is IT->glyph_row). */
19627 n_glyphs_before = row->used[TEXT_AREA];
19628 x = it->current_x;
19629
19630 /* Remember the line height so far in case the next element doesn't
19631 fit on the line. */
19632 if (it->line_wrap != TRUNCATE)
19633 {
19634 ascent = it->max_ascent;
19635 descent = it->max_descent;
19636 phys_ascent = it->max_phys_ascent;
19637 phys_descent = it->max_phys_descent;
19638
19639 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19640 {
19641 if (IT_DISPLAYING_WHITESPACE (it))
19642 may_wrap = 1;
19643 else if (may_wrap)
19644 {
19645 SAVE_IT (wrap_it, *it, wrap_data);
19646 wrap_x = x;
19647 wrap_row_used = row->used[TEXT_AREA];
19648 wrap_row_ascent = row->ascent;
19649 wrap_row_height = row->height;
19650 wrap_row_phys_ascent = row->phys_ascent;
19651 wrap_row_phys_height = row->phys_height;
19652 wrap_row_extra_line_spacing = row->extra_line_spacing;
19653 wrap_row_min_pos = min_pos;
19654 wrap_row_min_bpos = min_bpos;
19655 wrap_row_max_pos = max_pos;
19656 wrap_row_max_bpos = max_bpos;
19657 may_wrap = 0;
19658 }
19659 }
19660 }
19661
19662 PRODUCE_GLYPHS (it);
19663
19664 /* If this display element was in marginal areas, continue with
19665 the next one. */
19666 if (it->area != TEXT_AREA)
19667 {
19668 row->ascent = max (row->ascent, it->max_ascent);
19669 row->height = max (row->height, it->max_ascent + it->max_descent);
19670 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19671 row->phys_height = max (row->phys_height,
19672 it->max_phys_ascent + it->max_phys_descent);
19673 row->extra_line_spacing = max (row->extra_line_spacing,
19674 it->max_extra_line_spacing);
19675 set_iterator_to_next (it, 1);
19676 continue;
19677 }
19678
19679 /* Does the display element fit on the line? If we truncate
19680 lines, we should draw past the right edge of the window. If
19681 we don't truncate, we want to stop so that we can display the
19682 continuation glyph before the right margin. If lines are
19683 continued, there are two possible strategies for characters
19684 resulting in more than 1 glyph (e.g. tabs): Display as many
19685 glyphs as possible in this line and leave the rest for the
19686 continuation line, or display the whole element in the next
19687 line. Original redisplay did the former, so we do it also. */
19688 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19689 hpos_before = it->hpos;
19690 x_before = x;
19691
19692 if (/* Not a newline. */
19693 nglyphs > 0
19694 /* Glyphs produced fit entirely in the line. */
19695 && it->current_x < it->last_visible_x)
19696 {
19697 it->hpos += nglyphs;
19698 row->ascent = max (row->ascent, it->max_ascent);
19699 row->height = max (row->height, it->max_ascent + it->max_descent);
19700 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19701 row->phys_height = max (row->phys_height,
19702 it->max_phys_ascent + it->max_phys_descent);
19703 row->extra_line_spacing = max (row->extra_line_spacing,
19704 it->max_extra_line_spacing);
19705 if (it->current_x - it->pixel_width < it->first_visible_x)
19706 row->x = x - it->first_visible_x;
19707 /* Record the maximum and minimum buffer positions seen so
19708 far in glyphs that will be displayed by this row. */
19709 if (it->bidi_p)
19710 RECORD_MAX_MIN_POS (it);
19711 }
19712 else
19713 {
19714 int i, new_x;
19715 struct glyph *glyph;
19716
19717 for (i = 0; i < nglyphs; ++i, x = new_x)
19718 {
19719 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19720 new_x = x + glyph->pixel_width;
19721
19722 if (/* Lines are continued. */
19723 it->line_wrap != TRUNCATE
19724 && (/* Glyph doesn't fit on the line. */
19725 new_x > it->last_visible_x
19726 /* Or it fits exactly on a window system frame. */
19727 || (new_x == it->last_visible_x
19728 && FRAME_WINDOW_P (it->f)
19729 && (row->reversed_p
19730 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19731 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19732 {
19733 /* End of a continued line. */
19734
19735 if (it->hpos == 0
19736 || (new_x == it->last_visible_x
19737 && FRAME_WINDOW_P (it->f)
19738 && (row->reversed_p
19739 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19740 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19741 {
19742 /* Current glyph is the only one on the line or
19743 fits exactly on the line. We must continue
19744 the line because we can't draw the cursor
19745 after the glyph. */
19746 row->continued_p = 1;
19747 it->current_x = new_x;
19748 it->continuation_lines_width += new_x;
19749 ++it->hpos;
19750 if (i == nglyphs - 1)
19751 {
19752 /* If line-wrap is on, check if a previous
19753 wrap point was found. */
19754 if (wrap_row_used > 0
19755 /* Even if there is a previous wrap
19756 point, continue the line here as
19757 usual, if (i) the previous character
19758 was a space or tab AND (ii) the
19759 current character is not. */
19760 && (!may_wrap
19761 || IT_DISPLAYING_WHITESPACE (it)))
19762 goto back_to_wrap;
19763
19764 /* Record the maximum and minimum buffer
19765 positions seen so far in glyphs that will be
19766 displayed by this row. */
19767 if (it->bidi_p)
19768 RECORD_MAX_MIN_POS (it);
19769 set_iterator_to_next (it, 1);
19770 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19771 {
19772 if (!get_next_display_element (it))
19773 {
19774 row->exact_window_width_line_p = 1;
19775 it->continuation_lines_width = 0;
19776 row->continued_p = 0;
19777 row->ends_at_zv_p = 1;
19778 }
19779 else if (ITERATOR_AT_END_OF_LINE_P (it))
19780 {
19781 row->continued_p = 0;
19782 row->exact_window_width_line_p = 1;
19783 }
19784 }
19785 }
19786 else if (it->bidi_p)
19787 RECORD_MAX_MIN_POS (it);
19788 }
19789 else if (CHAR_GLYPH_PADDING_P (*glyph)
19790 && !FRAME_WINDOW_P (it->f))
19791 {
19792 /* A padding glyph that doesn't fit on this line.
19793 This means the whole character doesn't fit
19794 on the line. */
19795 if (row->reversed_p)
19796 unproduce_glyphs (it, row->used[TEXT_AREA]
19797 - n_glyphs_before);
19798 row->used[TEXT_AREA] = n_glyphs_before;
19799
19800 /* Fill the rest of the row with continuation
19801 glyphs like in 20.x. */
19802 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19803 < row->glyphs[1 + TEXT_AREA])
19804 produce_special_glyphs (it, IT_CONTINUATION);
19805
19806 row->continued_p = 1;
19807 it->current_x = x_before;
19808 it->continuation_lines_width += x_before;
19809
19810 /* Restore the height to what it was before the
19811 element not fitting on the line. */
19812 it->max_ascent = ascent;
19813 it->max_descent = descent;
19814 it->max_phys_ascent = phys_ascent;
19815 it->max_phys_descent = phys_descent;
19816 }
19817 else if (wrap_row_used > 0)
19818 {
19819 back_to_wrap:
19820 if (row->reversed_p)
19821 unproduce_glyphs (it,
19822 row->used[TEXT_AREA] - wrap_row_used);
19823 RESTORE_IT (it, &wrap_it, wrap_data);
19824 it->continuation_lines_width += wrap_x;
19825 row->used[TEXT_AREA] = wrap_row_used;
19826 row->ascent = wrap_row_ascent;
19827 row->height = wrap_row_height;
19828 row->phys_ascent = wrap_row_phys_ascent;
19829 row->phys_height = wrap_row_phys_height;
19830 row->extra_line_spacing = wrap_row_extra_line_spacing;
19831 min_pos = wrap_row_min_pos;
19832 min_bpos = wrap_row_min_bpos;
19833 max_pos = wrap_row_max_pos;
19834 max_bpos = wrap_row_max_bpos;
19835 row->continued_p = 1;
19836 row->ends_at_zv_p = 0;
19837 row->exact_window_width_line_p = 0;
19838 it->continuation_lines_width += x;
19839
19840 /* Make sure that a non-default face is extended
19841 up to the right margin of the window. */
19842 extend_face_to_end_of_line (it);
19843 }
19844 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19845 {
19846 /* A TAB that extends past the right edge of the
19847 window. This produces a single glyph on
19848 window system frames. We leave the glyph in
19849 this row and let it fill the row, but don't
19850 consume the TAB. */
19851 if ((row->reversed_p
19852 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19853 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19854 produce_special_glyphs (it, IT_CONTINUATION);
19855 it->continuation_lines_width += it->last_visible_x;
19856 row->ends_in_middle_of_char_p = 1;
19857 row->continued_p = 1;
19858 glyph->pixel_width = it->last_visible_x - x;
19859 it->starts_in_middle_of_char_p = 1;
19860 }
19861 else
19862 {
19863 /* Something other than a TAB that draws past
19864 the right edge of the window. Restore
19865 positions to values before the element. */
19866 if (row->reversed_p)
19867 unproduce_glyphs (it, row->used[TEXT_AREA]
19868 - (n_glyphs_before + i));
19869 row->used[TEXT_AREA] = n_glyphs_before + i;
19870
19871 /* Display continuation glyphs. */
19872 it->current_x = x_before;
19873 it->continuation_lines_width += x;
19874 if (!FRAME_WINDOW_P (it->f)
19875 || (row->reversed_p
19876 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19877 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19878 produce_special_glyphs (it, IT_CONTINUATION);
19879 row->continued_p = 1;
19880
19881 extend_face_to_end_of_line (it);
19882
19883 if (nglyphs > 1 && i > 0)
19884 {
19885 row->ends_in_middle_of_char_p = 1;
19886 it->starts_in_middle_of_char_p = 1;
19887 }
19888
19889 /* Restore the height to what it was before the
19890 element not fitting on the line. */
19891 it->max_ascent = ascent;
19892 it->max_descent = descent;
19893 it->max_phys_ascent = phys_ascent;
19894 it->max_phys_descent = phys_descent;
19895 }
19896
19897 break;
19898 }
19899 else if (new_x > it->first_visible_x)
19900 {
19901 /* Increment number of glyphs actually displayed. */
19902 ++it->hpos;
19903
19904 /* Record the maximum and minimum buffer positions
19905 seen so far in glyphs that will be displayed by
19906 this row. */
19907 if (it->bidi_p)
19908 RECORD_MAX_MIN_POS (it);
19909
19910 if (x < it->first_visible_x)
19911 /* Glyph is partially visible, i.e. row starts at
19912 negative X position. */
19913 row->x = x - it->first_visible_x;
19914 }
19915 else
19916 {
19917 /* Glyph is completely off the left margin of the
19918 window. This should not happen because of the
19919 move_it_in_display_line at the start of this
19920 function, unless the text display area of the
19921 window is empty. */
19922 eassert (it->first_visible_x <= it->last_visible_x);
19923 }
19924 }
19925 /* Even if this display element produced no glyphs at all,
19926 we want to record its position. */
19927 if (it->bidi_p && nglyphs == 0)
19928 RECORD_MAX_MIN_POS (it);
19929
19930 row->ascent = max (row->ascent, it->max_ascent);
19931 row->height = max (row->height, it->max_ascent + it->max_descent);
19932 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19933 row->phys_height = max (row->phys_height,
19934 it->max_phys_ascent + it->max_phys_descent);
19935 row->extra_line_spacing = max (row->extra_line_spacing,
19936 it->max_extra_line_spacing);
19937
19938 /* End of this display line if row is continued. */
19939 if (row->continued_p || row->ends_at_zv_p)
19940 break;
19941 }
19942
19943 at_end_of_line:
19944 /* Is this a line end? If yes, we're also done, after making
19945 sure that a non-default face is extended up to the right
19946 margin of the window. */
19947 if (ITERATOR_AT_END_OF_LINE_P (it))
19948 {
19949 int used_before = row->used[TEXT_AREA];
19950
19951 row->ends_in_newline_from_string_p = STRINGP (it->object);
19952
19953 /* Add a space at the end of the line that is used to
19954 display the cursor there. */
19955 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19956 append_space_for_newline (it, 0);
19957
19958 /* Extend the face to the end of the line. */
19959 extend_face_to_end_of_line (it);
19960
19961 /* Make sure we have the position. */
19962 if (used_before == 0)
19963 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19964
19965 /* Record the position of the newline, for use in
19966 find_row_edges. */
19967 it->eol_pos = it->current.pos;
19968
19969 /* Consume the line end. This skips over invisible lines. */
19970 set_iterator_to_next (it, 1);
19971 it->continuation_lines_width = 0;
19972 break;
19973 }
19974
19975 /* Proceed with next display element. Note that this skips
19976 over lines invisible because of selective display. */
19977 set_iterator_to_next (it, 1);
19978
19979 /* If we truncate lines, we are done when the last displayed
19980 glyphs reach past the right margin of the window. */
19981 if (it->line_wrap == TRUNCATE
19982 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19983 ? (it->current_x >= it->last_visible_x)
19984 : (it->current_x > it->last_visible_x)))
19985 {
19986 /* Maybe add truncation glyphs. */
19987 if (!FRAME_WINDOW_P (it->f)
19988 || (row->reversed_p
19989 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19990 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19991 {
19992 int i, n;
19993
19994 if (!row->reversed_p)
19995 {
19996 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19997 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19998 break;
19999 }
20000 else
20001 {
20002 for (i = 0; i < row->used[TEXT_AREA]; i++)
20003 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20004 break;
20005 /* Remove any padding glyphs at the front of ROW, to
20006 make room for the truncation glyphs we will be
20007 adding below. The loop below always inserts at
20008 least one truncation glyph, so also remove the
20009 last glyph added to ROW. */
20010 unproduce_glyphs (it, i + 1);
20011 /* Adjust i for the loop below. */
20012 i = row->used[TEXT_AREA] - (i + 1);
20013 }
20014
20015 it->current_x = x_before;
20016 if (!FRAME_WINDOW_P (it->f))
20017 {
20018 for (n = row->used[TEXT_AREA]; i < n; ++i)
20019 {
20020 row->used[TEXT_AREA] = i;
20021 produce_special_glyphs (it, IT_TRUNCATION);
20022 }
20023 }
20024 else
20025 {
20026 row->used[TEXT_AREA] = i;
20027 produce_special_glyphs (it, IT_TRUNCATION);
20028 }
20029 }
20030 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20031 {
20032 /* Don't truncate if we can overflow newline into fringe. */
20033 if (!get_next_display_element (it))
20034 {
20035 it->continuation_lines_width = 0;
20036 row->ends_at_zv_p = 1;
20037 row->exact_window_width_line_p = 1;
20038 break;
20039 }
20040 if (ITERATOR_AT_END_OF_LINE_P (it))
20041 {
20042 row->exact_window_width_line_p = 1;
20043 goto at_end_of_line;
20044 }
20045 it->current_x = x_before;
20046 }
20047
20048 row->truncated_on_right_p = 1;
20049 it->continuation_lines_width = 0;
20050 reseat_at_next_visible_line_start (it, 0);
20051 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20052 it->hpos = hpos_before;
20053 break;
20054 }
20055 }
20056
20057 if (wrap_data)
20058 bidi_unshelve_cache (wrap_data, 1);
20059
20060 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20061 at the left window margin. */
20062 if (it->first_visible_x
20063 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20064 {
20065 if (!FRAME_WINDOW_P (it->f)
20066 || (row->reversed_p
20067 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20068 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20069 insert_left_trunc_glyphs (it);
20070 row->truncated_on_left_p = 1;
20071 }
20072
20073 /* Remember the position at which this line ends.
20074
20075 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20076 cannot be before the call to find_row_edges below, since that is
20077 where these positions are determined. */
20078 row->end = it->current;
20079 if (!it->bidi_p)
20080 {
20081 row->minpos = row->start.pos;
20082 row->maxpos = row->end.pos;
20083 }
20084 else
20085 {
20086 /* ROW->minpos and ROW->maxpos must be the smallest and
20087 `1 + the largest' buffer positions in ROW. But if ROW was
20088 bidi-reordered, these two positions can be anywhere in the
20089 row, so we must determine them now. */
20090 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20091 }
20092
20093 /* If the start of this line is the overlay arrow-position, then
20094 mark this glyph row as the one containing the overlay arrow.
20095 This is clearly a mess with variable size fonts. It would be
20096 better to let it be displayed like cursors under X. */
20097 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20098 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20099 !NILP (overlay_arrow_string)))
20100 {
20101 /* Overlay arrow in window redisplay is a fringe bitmap. */
20102 if (STRINGP (overlay_arrow_string))
20103 {
20104 struct glyph_row *arrow_row
20105 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20106 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20107 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20108 struct glyph *p = row->glyphs[TEXT_AREA];
20109 struct glyph *p2, *end;
20110
20111 /* Copy the arrow glyphs. */
20112 while (glyph < arrow_end)
20113 *p++ = *glyph++;
20114
20115 /* Throw away padding glyphs. */
20116 p2 = p;
20117 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20118 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20119 ++p2;
20120 if (p2 > p)
20121 {
20122 while (p2 < end)
20123 *p++ = *p2++;
20124 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20125 }
20126 }
20127 else
20128 {
20129 eassert (INTEGERP (overlay_arrow_string));
20130 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20131 }
20132 overlay_arrow_seen = 1;
20133 }
20134
20135 /* Highlight trailing whitespace. */
20136 if (!NILP (Vshow_trailing_whitespace))
20137 highlight_trailing_whitespace (it->f, it->glyph_row);
20138
20139 /* Compute pixel dimensions of this line. */
20140 compute_line_metrics (it);
20141
20142 /* Implementation note: No changes in the glyphs of ROW or in their
20143 faces can be done past this point, because compute_line_metrics
20144 computes ROW's hash value and stores it within the glyph_row
20145 structure. */
20146
20147 /* Record whether this row ends inside an ellipsis. */
20148 row->ends_in_ellipsis_p
20149 = (it->method == GET_FROM_DISPLAY_VECTOR
20150 && it->ellipsis_p);
20151
20152 /* Save fringe bitmaps in this row. */
20153 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20154 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20155 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20156 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20157
20158 it->left_user_fringe_bitmap = 0;
20159 it->left_user_fringe_face_id = 0;
20160 it->right_user_fringe_bitmap = 0;
20161 it->right_user_fringe_face_id = 0;
20162
20163 /* Maybe set the cursor. */
20164 cvpos = it->w->cursor.vpos;
20165 if ((cvpos < 0
20166 /* In bidi-reordered rows, keep checking for proper cursor
20167 position even if one has been found already, because buffer
20168 positions in such rows change non-linearly with ROW->VPOS,
20169 when a line is continued. One exception: when we are at ZV,
20170 display cursor on the first suitable glyph row, since all
20171 the empty rows after that also have their position set to ZV. */
20172 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20173 lines' rows is implemented for bidi-reordered rows. */
20174 || (it->bidi_p
20175 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20176 && PT >= MATRIX_ROW_START_CHARPOS (row)
20177 && PT <= MATRIX_ROW_END_CHARPOS (row)
20178 && cursor_row_p (row))
20179 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20180
20181 /* Prepare for the next line. This line starts horizontally at (X
20182 HPOS) = (0 0). Vertical positions are incremented. As a
20183 convenience for the caller, IT->glyph_row is set to the next
20184 row to be used. */
20185 it->current_x = it->hpos = 0;
20186 it->current_y += row->height;
20187 SET_TEXT_POS (it->eol_pos, 0, 0);
20188 ++it->vpos;
20189 ++it->glyph_row;
20190 /* The next row should by default use the same value of the
20191 reversed_p flag as this one. set_iterator_to_next decides when
20192 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20193 the flag accordingly. */
20194 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20195 it->glyph_row->reversed_p = row->reversed_p;
20196 it->start = row->end;
20197 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20198
20199 #undef RECORD_MAX_MIN_POS
20200 }
20201
20202 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20203 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20204 doc: /* Return paragraph direction at point in BUFFER.
20205 Value is either `left-to-right' or `right-to-left'.
20206 If BUFFER is omitted or nil, it defaults to the current buffer.
20207
20208 Paragraph direction determines how the text in the paragraph is displayed.
20209 In left-to-right paragraphs, text begins at the left margin of the window
20210 and the reading direction is generally left to right. In right-to-left
20211 paragraphs, text begins at the right margin and is read from right to left.
20212
20213 See also `bidi-paragraph-direction'. */)
20214 (Lisp_Object buffer)
20215 {
20216 struct buffer *buf = current_buffer;
20217 struct buffer *old = buf;
20218
20219 if (! NILP (buffer))
20220 {
20221 CHECK_BUFFER (buffer);
20222 buf = XBUFFER (buffer);
20223 }
20224
20225 if (NILP (BVAR (buf, bidi_display_reordering))
20226 || NILP (BVAR (buf, enable_multibyte_characters))
20227 /* When we are loading loadup.el, the character property tables
20228 needed for bidi iteration are not yet available. */
20229 || !NILP (Vpurify_flag))
20230 return Qleft_to_right;
20231 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20232 return BVAR (buf, bidi_paragraph_direction);
20233 else
20234 {
20235 /* Determine the direction from buffer text. We could try to
20236 use current_matrix if it is up to date, but this seems fast
20237 enough as it is. */
20238 struct bidi_it itb;
20239 ptrdiff_t pos = BUF_PT (buf);
20240 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20241 int c;
20242 void *itb_data = bidi_shelve_cache ();
20243
20244 set_buffer_temp (buf);
20245 /* bidi_paragraph_init finds the base direction of the paragraph
20246 by searching forward from paragraph start. We need the base
20247 direction of the current or _previous_ paragraph, so we need
20248 to make sure we are within that paragraph. To that end, find
20249 the previous non-empty line. */
20250 if (pos >= ZV && pos > BEGV)
20251 DEC_BOTH (pos, bytepos);
20252 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20253 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20254 {
20255 while ((c = FETCH_BYTE (bytepos)) == '\n'
20256 || c == ' ' || c == '\t' || c == '\f')
20257 {
20258 if (bytepos <= BEGV_BYTE)
20259 break;
20260 bytepos--;
20261 pos--;
20262 }
20263 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20264 bytepos--;
20265 }
20266 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20267 itb.paragraph_dir = NEUTRAL_DIR;
20268 itb.string.s = NULL;
20269 itb.string.lstring = Qnil;
20270 itb.string.bufpos = 0;
20271 itb.string.unibyte = 0;
20272 /* We have no window to use here for ignoring window-specific
20273 overlays. Using NULL for window pointer will cause
20274 compute_display_string_pos to use the current buffer. */
20275 itb.w = NULL;
20276 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20277 bidi_unshelve_cache (itb_data, 0);
20278 set_buffer_temp (old);
20279 switch (itb.paragraph_dir)
20280 {
20281 case L2R:
20282 return Qleft_to_right;
20283 break;
20284 case R2L:
20285 return Qright_to_left;
20286 break;
20287 default:
20288 emacs_abort ();
20289 }
20290 }
20291 }
20292
20293 DEFUN ("move-point-visually", Fmove_point_visually,
20294 Smove_point_visually, 1, 1, 0,
20295 doc: /* Move point in the visual order in the specified DIRECTION.
20296 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20297 left.
20298
20299 Value is the new character position of point. */)
20300 (Lisp_Object direction)
20301 {
20302 struct window *w = XWINDOW (selected_window);
20303 struct buffer *b = XBUFFER (w->contents);
20304 struct glyph_row *row;
20305 int dir;
20306 Lisp_Object paragraph_dir;
20307
20308 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20309 (!(ROW)->continued_p \
20310 && INTEGERP ((GLYPH)->object) \
20311 && (GLYPH)->type == CHAR_GLYPH \
20312 && (GLYPH)->u.ch == ' ' \
20313 && (GLYPH)->charpos >= 0 \
20314 && !(GLYPH)->avoid_cursor_p)
20315
20316 CHECK_NUMBER (direction);
20317 dir = XINT (direction);
20318 if (dir > 0)
20319 dir = 1;
20320 else
20321 dir = -1;
20322
20323 /* If current matrix is up-to-date, we can use the information
20324 recorded in the glyphs, at least as long as the goal is on the
20325 screen. */
20326 if (w->window_end_valid
20327 && !windows_or_buffers_changed
20328 && b
20329 && !b->clip_changed
20330 && !b->prevent_redisplay_optimizations_p
20331 && !window_outdated (w)
20332 && w->cursor.vpos >= 0
20333 && w->cursor.vpos < w->current_matrix->nrows
20334 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20335 {
20336 struct glyph *g = row->glyphs[TEXT_AREA];
20337 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20338 struct glyph *gpt = g + w->cursor.hpos;
20339
20340 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20341 {
20342 if (BUFFERP (g->object) && g->charpos != PT)
20343 {
20344 SET_PT (g->charpos);
20345 w->cursor.vpos = -1;
20346 return make_number (PT);
20347 }
20348 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20349 {
20350 ptrdiff_t new_pos;
20351
20352 if (BUFFERP (gpt->object))
20353 {
20354 new_pos = PT;
20355 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20356 new_pos += (row->reversed_p ? -dir : dir);
20357 else
20358 new_pos -= (row->reversed_p ? -dir : dir);;
20359 }
20360 else if (BUFFERP (g->object))
20361 new_pos = g->charpos;
20362 else
20363 break;
20364 SET_PT (new_pos);
20365 w->cursor.vpos = -1;
20366 return make_number (PT);
20367 }
20368 else if (ROW_GLYPH_NEWLINE_P (row, g))
20369 {
20370 /* Glyphs inserted at the end of a non-empty line for
20371 positioning the cursor have zero charpos, so we must
20372 deduce the value of point by other means. */
20373 if (g->charpos > 0)
20374 SET_PT (g->charpos);
20375 else if (row->ends_at_zv_p && PT != ZV)
20376 SET_PT (ZV);
20377 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20378 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20379 else
20380 break;
20381 w->cursor.vpos = -1;
20382 return make_number (PT);
20383 }
20384 }
20385 if (g == e || INTEGERP (g->object))
20386 {
20387 if (row->truncated_on_left_p || row->truncated_on_right_p)
20388 goto simulate_display;
20389 if (!row->reversed_p)
20390 row += dir;
20391 else
20392 row -= dir;
20393 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20394 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20395 goto simulate_display;
20396
20397 if (dir > 0)
20398 {
20399 if (row->reversed_p && !row->continued_p)
20400 {
20401 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20402 w->cursor.vpos = -1;
20403 return make_number (PT);
20404 }
20405 g = row->glyphs[TEXT_AREA];
20406 e = g + row->used[TEXT_AREA];
20407 for ( ; g < e; g++)
20408 {
20409 if (BUFFERP (g->object)
20410 /* Empty lines have only one glyph, which stands
20411 for the newline, and whose charpos is the
20412 buffer position of the newline. */
20413 || ROW_GLYPH_NEWLINE_P (row, g)
20414 /* When the buffer ends in a newline, the line at
20415 EOB also has one glyph, but its charpos is -1. */
20416 || (row->ends_at_zv_p
20417 && !row->reversed_p
20418 && INTEGERP (g->object)
20419 && g->type == CHAR_GLYPH
20420 && g->u.ch == ' '))
20421 {
20422 if (g->charpos > 0)
20423 SET_PT (g->charpos);
20424 else if (!row->reversed_p
20425 && row->ends_at_zv_p
20426 && PT != ZV)
20427 SET_PT (ZV);
20428 else
20429 continue;
20430 w->cursor.vpos = -1;
20431 return make_number (PT);
20432 }
20433 }
20434 }
20435 else
20436 {
20437 if (!row->reversed_p && !row->continued_p)
20438 {
20439 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20440 w->cursor.vpos = -1;
20441 return make_number (PT);
20442 }
20443 e = row->glyphs[TEXT_AREA];
20444 g = e + row->used[TEXT_AREA] - 1;
20445 for ( ; g >= e; g--)
20446 {
20447 if (BUFFERP (g->object)
20448 || (ROW_GLYPH_NEWLINE_P (row, g)
20449 && g->charpos > 0)
20450 /* Empty R2L lines on GUI frames have the buffer
20451 position of the newline stored in the stretch
20452 glyph. */
20453 || g->type == STRETCH_GLYPH
20454 || (row->ends_at_zv_p
20455 && row->reversed_p
20456 && INTEGERP (g->object)
20457 && g->type == CHAR_GLYPH
20458 && g->u.ch == ' '))
20459 {
20460 if (g->charpos > 0)
20461 SET_PT (g->charpos);
20462 else if (row->reversed_p
20463 && row->ends_at_zv_p
20464 && PT != ZV)
20465 SET_PT (ZV);
20466 else
20467 continue;
20468 w->cursor.vpos = -1;
20469 return make_number (PT);
20470 }
20471 }
20472 }
20473 }
20474 }
20475
20476 simulate_display:
20477
20478 /* If we wind up here, we failed to move by using the glyphs, so we
20479 need to simulate display instead. */
20480
20481 if (b)
20482 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20483 else
20484 paragraph_dir = Qleft_to_right;
20485 if (EQ (paragraph_dir, Qright_to_left))
20486 dir = -dir;
20487 if (PT <= BEGV && dir < 0)
20488 xsignal0 (Qbeginning_of_buffer);
20489 else if (PT >= ZV && dir > 0)
20490 xsignal0 (Qend_of_buffer);
20491 else
20492 {
20493 struct text_pos pt;
20494 struct it it;
20495 int pt_x, target_x, pixel_width, pt_vpos;
20496 bool at_eol_p;
20497 bool overshoot_expected = false;
20498 bool target_is_eol_p = false;
20499
20500 /* Setup the arena. */
20501 SET_TEXT_POS (pt, PT, PT_BYTE);
20502 start_display (&it, w, pt);
20503
20504 if (it.cmp_it.id < 0
20505 && it.method == GET_FROM_STRING
20506 && it.area == TEXT_AREA
20507 && it.string_from_display_prop_p
20508 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20509 overshoot_expected = true;
20510
20511 /* Find the X coordinate of point. We start from the beginning
20512 of this or previous line to make sure we are before point in
20513 the logical order (since the move_it_* functions can only
20514 move forward). */
20515 reseat_at_previous_visible_line_start (&it);
20516 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20517 if (IT_CHARPOS (it) != PT)
20518 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20519 -1, -1, -1, MOVE_TO_POS);
20520 pt_x = it.current_x;
20521 pt_vpos = it.vpos;
20522 if (dir > 0 || overshoot_expected)
20523 {
20524 struct glyph_row *row = it.glyph_row;
20525
20526 /* When point is at beginning of line, we don't have
20527 information about the glyph there loaded into struct
20528 it. Calling get_next_display_element fixes that. */
20529 if (pt_x == 0)
20530 get_next_display_element (&it);
20531 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20532 it.glyph_row = NULL;
20533 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20534 it.glyph_row = row;
20535 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20536 it, lest it will become out of sync with it's buffer
20537 position. */
20538 it.current_x = pt_x;
20539 }
20540 else
20541 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20542 pixel_width = it.pixel_width;
20543 if (overshoot_expected && at_eol_p)
20544 pixel_width = 0;
20545 else if (pixel_width <= 0)
20546 pixel_width = 1;
20547
20548 /* If there's a display string at point, we are actually at the
20549 glyph to the left of point, so we need to correct the X
20550 coordinate. */
20551 if (overshoot_expected)
20552 pt_x += pixel_width;
20553
20554 /* Compute target X coordinate, either to the left or to the
20555 right of point. On TTY frames, all characters have the same
20556 pixel width of 1, so we can use that. On GUI frames we don't
20557 have an easy way of getting at the pixel width of the
20558 character to the left of point, so we use a different method
20559 of getting to that place. */
20560 if (dir > 0)
20561 target_x = pt_x + pixel_width;
20562 else
20563 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20564
20565 /* Target X coordinate could be one line above or below the line
20566 of point, in which case we need to adjust the target X
20567 coordinate. Also, if moving to the left, we need to begin at
20568 the left edge of the point's screen line. */
20569 if (dir < 0)
20570 {
20571 if (pt_x > 0)
20572 {
20573 start_display (&it, w, pt);
20574 reseat_at_previous_visible_line_start (&it);
20575 it.current_x = it.current_y = it.hpos = 0;
20576 if (pt_vpos != 0)
20577 move_it_by_lines (&it, pt_vpos);
20578 }
20579 else
20580 {
20581 move_it_by_lines (&it, -1);
20582 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20583 target_is_eol_p = true;
20584 }
20585 }
20586 else
20587 {
20588 if (at_eol_p
20589 || (target_x >= it.last_visible_x
20590 && it.line_wrap != TRUNCATE))
20591 {
20592 if (pt_x > 0)
20593 move_it_by_lines (&it, 0);
20594 move_it_by_lines (&it, 1);
20595 target_x = 0;
20596 }
20597 }
20598
20599 /* Move to the target X coordinate. */
20600 #ifdef HAVE_WINDOW_SYSTEM
20601 /* On GUI frames, as we don't know the X coordinate of the
20602 character to the left of point, moving point to the left
20603 requires walking, one grapheme cluster at a time, until we
20604 find ourself at a place immediately to the left of the
20605 character at point. */
20606 if (FRAME_WINDOW_P (it.f) && dir < 0)
20607 {
20608 struct text_pos new_pos = it.current.pos;
20609 enum move_it_result rc = MOVE_X_REACHED;
20610
20611 while (it.current_x + it.pixel_width <= target_x
20612 && rc == MOVE_X_REACHED)
20613 {
20614 int new_x = it.current_x + it.pixel_width;
20615
20616 new_pos = it.current.pos;
20617 if (new_x == it.current_x)
20618 new_x++;
20619 rc = move_it_in_display_line_to (&it, ZV, new_x,
20620 MOVE_TO_POS | MOVE_TO_X);
20621 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20622 break;
20623 }
20624 /* If we ended up on a composed character inside
20625 bidi-reordered text (e.g., Hebrew text with diacritics),
20626 the iterator gives us the buffer position of the last (in
20627 logical order) character of the composed grapheme cluster,
20628 which is not what we want. So we cheat: we compute the
20629 character position of the character that follows (in the
20630 logical order) the one where the above loop stopped. That
20631 character will appear on display to the left of point. */
20632 if (it.bidi_p
20633 && it.bidi_it.scan_dir == -1
20634 && new_pos.charpos - IT_CHARPOS (it) > 1)
20635 {
20636 new_pos.charpos = IT_CHARPOS (it) + 1;
20637 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20638 }
20639 it.current.pos = new_pos;
20640 }
20641 else
20642 #endif
20643 if (it.current_x != target_x)
20644 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20645
20646 /* When lines are truncated, the above loop will stop at the
20647 window edge. But we want to get to the end of line, even if
20648 it is beyond the window edge; automatic hscroll will then
20649 scroll the window to show point as appropriate. */
20650 if (target_is_eol_p && it.line_wrap == TRUNCATE
20651 && get_next_display_element (&it))
20652 {
20653 struct text_pos new_pos = it.current.pos;
20654
20655 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20656 {
20657 set_iterator_to_next (&it, 0);
20658 if (it.method == GET_FROM_BUFFER)
20659 new_pos = it.current.pos;
20660 if (!get_next_display_element (&it))
20661 break;
20662 }
20663
20664 it.current.pos = new_pos;
20665 }
20666
20667 /* If we ended up in a display string that covers point, move to
20668 buffer position to the right in the visual order. */
20669 if (dir > 0)
20670 {
20671 while (IT_CHARPOS (it) == PT)
20672 {
20673 set_iterator_to_next (&it, 0);
20674 if (!get_next_display_element (&it))
20675 break;
20676 }
20677 }
20678
20679 /* Move point to that position. */
20680 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20681 }
20682
20683 return make_number (PT);
20684
20685 #undef ROW_GLYPH_NEWLINE_P
20686 }
20687
20688 \f
20689 /***********************************************************************
20690 Menu Bar
20691 ***********************************************************************/
20692
20693 /* Redisplay the menu bar in the frame for window W.
20694
20695 The menu bar of X frames that don't have X toolkit support is
20696 displayed in a special window W->frame->menu_bar_window.
20697
20698 The menu bar of terminal frames is treated specially as far as
20699 glyph matrices are concerned. Menu bar lines are not part of
20700 windows, so the update is done directly on the frame matrix rows
20701 for the menu bar. */
20702
20703 static void
20704 display_menu_bar (struct window *w)
20705 {
20706 struct frame *f = XFRAME (WINDOW_FRAME (w));
20707 struct it it;
20708 Lisp_Object items;
20709 int i;
20710
20711 /* Don't do all this for graphical frames. */
20712 #ifdef HAVE_NTGUI
20713 if (FRAME_W32_P (f))
20714 return;
20715 #endif
20716 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20717 if (FRAME_X_P (f))
20718 return;
20719 #endif
20720
20721 #ifdef HAVE_NS
20722 if (FRAME_NS_P (f))
20723 return;
20724 #endif /* HAVE_NS */
20725
20726 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20727 eassert (!FRAME_WINDOW_P (f));
20728 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20729 it.first_visible_x = 0;
20730 /* PXW: Use FRAME_PIXEL_WIDTH (f) here? */
20731 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20732 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20733 if (FRAME_WINDOW_P (f))
20734 {
20735 /* Menu bar lines are displayed in the desired matrix of the
20736 dummy window menu_bar_window. */
20737 struct window *menu_w;
20738 menu_w = XWINDOW (f->menu_bar_window);
20739 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20740 MENU_FACE_ID);
20741 it.first_visible_x = 0;
20742 /* PXW: Use FRAME_PIXEL_WIDTH (f) here? */
20743 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20744 }
20745 else
20746 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20747 {
20748 /* This is a TTY frame, i.e. character hpos/vpos are used as
20749 pixel x/y. */
20750 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20751 MENU_FACE_ID);
20752 it.first_visible_x = 0;
20753 it.last_visible_x = FRAME_COLS (f);
20754 }
20755
20756 /* FIXME: This should be controlled by a user option. See the
20757 comments in redisplay_tool_bar and display_mode_line about
20758 this. */
20759 it.paragraph_embedding = L2R;
20760
20761 /* Clear all rows of the menu bar. */
20762 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20763 {
20764 struct glyph_row *row = it.glyph_row + i;
20765 clear_glyph_row (row);
20766 row->enabled_p = 1;
20767 row->full_width_p = 1;
20768 }
20769
20770 /* Display all items of the menu bar. */
20771 items = FRAME_MENU_BAR_ITEMS (it.f);
20772 for (i = 0; i < ASIZE (items); i += 4)
20773 {
20774 Lisp_Object string;
20775
20776 /* Stop at nil string. */
20777 string = AREF (items, i + 1);
20778 if (NILP (string))
20779 break;
20780
20781 /* Remember where item was displayed. */
20782 ASET (items, i + 3, make_number (it.hpos));
20783
20784 /* Display the item, pad with one space. */
20785 if (it.current_x < it.last_visible_x)
20786 display_string (NULL, string, Qnil, 0, 0, &it,
20787 SCHARS (string) + 1, 0, 0, -1);
20788 }
20789
20790 /* Fill out the line with spaces. */
20791 if (it.current_x < it.last_visible_x)
20792 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20793
20794 /* Compute the total height of the lines. */
20795 compute_line_metrics (&it);
20796 }
20797
20798 /* Deep copy of a glyph row, including the glyphs. */
20799 static void
20800 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20801 {
20802 struct glyph *pointers[1 + LAST_AREA];
20803 int to_used = to->used[TEXT_AREA];
20804
20805 /* Save glyph pointers of TO. */
20806 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20807
20808 /* Do a structure assignment. */
20809 *to = *from;
20810
20811 /* Restore original glyph pointers of TO. */
20812 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20813
20814 /* Copy the glyphs. */
20815 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20816 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20817
20818 /* If we filled only part of the TO row, fill the rest with
20819 space_glyph (which will display as empty space). */
20820 if (to_used > from->used[TEXT_AREA])
20821 fill_up_frame_row_with_spaces (to, to_used);
20822 }
20823
20824 /* Display one menu item on a TTY, by overwriting the glyphs in the
20825 frame F's desired glyph matrix with glyphs produced from the menu
20826 item text. Called from term.c to display TTY drop-down menus one
20827 item at a time.
20828
20829 ITEM_TEXT is the menu item text as a C string.
20830
20831 FACE_ID is the face ID to be used for this menu item. FACE_ID
20832 could specify one of 3 faces: a face for an enabled item, a face
20833 for a disabled item, or a face for a selected item.
20834
20835 X and Y are coordinates of the first glyph in the frame's desired
20836 matrix to be overwritten by the menu item. Since this is a TTY, Y
20837 is the zero-based number of the glyph row and X is the zero-based
20838 glyph number in the row, starting from left, where to start
20839 displaying the item.
20840
20841 SUBMENU non-zero means this menu item drops down a submenu, which
20842 should be indicated by displaying a proper visual cue after the
20843 item text. */
20844
20845 void
20846 display_tty_menu_item (const char *item_text, int width, int face_id,
20847 int x, int y, int submenu)
20848 {
20849 struct it it;
20850 struct frame *f = SELECTED_FRAME ();
20851 struct window *w = XWINDOW (f->selected_window);
20852 int saved_used, saved_truncated, saved_width, saved_reversed;
20853 struct glyph_row *row;
20854 size_t item_len = strlen (item_text);
20855
20856 eassert (FRAME_TERMCAP_P (f));
20857
20858 /* Don't write beyond the matrix's last row. This can happen for
20859 TTY screens that are not high enough to show the entire menu.
20860 (This is actually a bit of defensive programming, as
20861 tty_menu_display already limits the number of menu items to one
20862 less than the number of screen lines.) */
20863 if (y >= f->desired_matrix->nrows)
20864 return;
20865
20866 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20867 it.first_visible_x = 0;
20868 it.last_visible_x = FRAME_COLS (f) - 1;
20869 row = it.glyph_row;
20870 /* Start with the row contents from the current matrix. */
20871 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20872 saved_width = row->full_width_p;
20873 row->full_width_p = 1;
20874 saved_reversed = row->reversed_p;
20875 row->reversed_p = 0;
20876 row->enabled_p = 1;
20877
20878 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20879 desired face. */
20880 eassert (x < f->desired_matrix->matrix_w);
20881 it.current_x = it.hpos = x;
20882 it.current_y = it.vpos = y;
20883 saved_used = row->used[TEXT_AREA];
20884 saved_truncated = row->truncated_on_right_p;
20885 row->used[TEXT_AREA] = x;
20886 it.face_id = face_id;
20887 it.line_wrap = TRUNCATE;
20888
20889 /* FIXME: This should be controlled by a user option. See the
20890 comments in redisplay_tool_bar and display_mode_line about this.
20891 Also, if paragraph_embedding could ever be R2L, changes will be
20892 needed to avoid shifting to the right the row characters in
20893 term.c:append_glyph. */
20894 it.paragraph_embedding = L2R;
20895
20896 /* Pad with a space on the left. */
20897 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20898 width--;
20899 /* Display the menu item, pad with spaces to WIDTH. */
20900 if (submenu)
20901 {
20902 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20903 item_len, 0, FRAME_COLS (f) - 1, -1);
20904 width -= item_len;
20905 /* Indicate with " >" that there's a submenu. */
20906 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20907 FRAME_COLS (f) - 1, -1);
20908 }
20909 else
20910 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20911 width, 0, FRAME_COLS (f) - 1, -1);
20912
20913 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20914 row->truncated_on_right_p = saved_truncated;
20915 row->hash = row_hash (row);
20916 row->full_width_p = saved_width;
20917 row->reversed_p = saved_reversed;
20918 }
20919 \f
20920 /***********************************************************************
20921 Mode Line
20922 ***********************************************************************/
20923
20924 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20925 FORCE is non-zero, redisplay mode lines unconditionally.
20926 Otherwise, redisplay only mode lines that are garbaged. Value is
20927 the number of windows whose mode lines were redisplayed. */
20928
20929 static int
20930 redisplay_mode_lines (Lisp_Object window, bool force)
20931 {
20932 int nwindows = 0;
20933
20934 while (!NILP (window))
20935 {
20936 struct window *w = XWINDOW (window);
20937
20938 if (WINDOWP (w->contents))
20939 nwindows += redisplay_mode_lines (w->contents, force);
20940 else if (force
20941 || FRAME_GARBAGED_P (XFRAME (w->frame))
20942 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20943 {
20944 struct text_pos lpoint;
20945 struct buffer *old = current_buffer;
20946
20947 /* Set the window's buffer for the mode line display. */
20948 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20949 set_buffer_internal_1 (XBUFFER (w->contents));
20950
20951 /* Point refers normally to the selected window. For any
20952 other window, set up appropriate value. */
20953 if (!EQ (window, selected_window))
20954 {
20955 struct text_pos pt;
20956
20957 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20958 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20959 }
20960
20961 /* Display mode lines. */
20962 clear_glyph_matrix (w->desired_matrix);
20963 if (display_mode_lines (w))
20964 ++nwindows;
20965
20966 /* Restore old settings. */
20967 set_buffer_internal_1 (old);
20968 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20969 }
20970
20971 window = w->next;
20972 }
20973
20974 return nwindows;
20975 }
20976
20977
20978 /* Display the mode and/or header line of window W. Value is the
20979 sum number of mode lines and header lines displayed. */
20980
20981 static int
20982 display_mode_lines (struct window *w)
20983 {
20984 Lisp_Object old_selected_window = selected_window;
20985 Lisp_Object old_selected_frame = selected_frame;
20986 Lisp_Object new_frame = w->frame;
20987 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20988 int n = 0;
20989
20990 selected_frame = new_frame;
20991 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20992 or window's point, then we'd need select_window_1 here as well. */
20993 XSETWINDOW (selected_window, w);
20994 XFRAME (new_frame)->selected_window = selected_window;
20995
20996 /* These will be set while the mode line specs are processed. */
20997 line_number_displayed = 0;
20998 w->column_number_displayed = -1;
20999
21000 if (WINDOW_WANTS_MODELINE_P (w))
21001 {
21002 struct window *sel_w = XWINDOW (old_selected_window);
21003
21004 /* Select mode line face based on the real selected window. */
21005 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21006 BVAR (current_buffer, mode_line_format));
21007 ++n;
21008 }
21009
21010 if (WINDOW_WANTS_HEADER_LINE_P (w))
21011 {
21012 display_mode_line (w, HEADER_LINE_FACE_ID,
21013 BVAR (current_buffer, header_line_format));
21014 ++n;
21015 }
21016
21017 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21018 selected_frame = old_selected_frame;
21019 selected_window = old_selected_window;
21020 if (n > 0)
21021 w->must_be_updated_p = true;
21022 return n;
21023 }
21024
21025
21026 /* Display mode or header line of window W. FACE_ID specifies which
21027 line to display; it is either MODE_LINE_FACE_ID or
21028 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21029 display. Value is the pixel height of the mode/header line
21030 displayed. */
21031
21032 static int
21033 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21034 {
21035 struct it it;
21036 struct face *face;
21037 ptrdiff_t count = SPECPDL_INDEX ();
21038
21039 init_iterator (&it, w, -1, -1, NULL, face_id);
21040 /* Don't extend on a previously drawn mode-line.
21041 This may happen if called from pos_visible_p. */
21042 it.glyph_row->enabled_p = 0;
21043 prepare_desired_row (it.glyph_row);
21044
21045 it.glyph_row->mode_line_p = 1;
21046
21047 /* FIXME: This should be controlled by a user option. But
21048 supporting such an option is not trivial, since the mode line is
21049 made up of many separate strings. */
21050 it.paragraph_embedding = L2R;
21051
21052 record_unwind_protect (unwind_format_mode_line,
21053 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21054
21055 mode_line_target = MODE_LINE_DISPLAY;
21056
21057 /* Temporarily make frame's keyboard the current kboard so that
21058 kboard-local variables in the mode_line_format will get the right
21059 values. */
21060 push_kboard (FRAME_KBOARD (it.f));
21061 record_unwind_save_match_data ();
21062 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21063 pop_kboard ();
21064
21065 unbind_to (count, Qnil);
21066
21067 /* Fill up with spaces. */
21068 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21069
21070 compute_line_metrics (&it);
21071 it.glyph_row->full_width_p = 1;
21072 it.glyph_row->continued_p = 0;
21073 it.glyph_row->truncated_on_left_p = 0;
21074 it.glyph_row->truncated_on_right_p = 0;
21075
21076 /* Make a 3D mode-line have a shadow at its right end. */
21077 face = FACE_FROM_ID (it.f, face_id);
21078 extend_face_to_end_of_line (&it);
21079 if (face->box != FACE_NO_BOX)
21080 {
21081 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21082 + it.glyph_row->used[TEXT_AREA] - 1);
21083 last->right_box_line_p = 1;
21084 }
21085
21086 return it.glyph_row->height;
21087 }
21088
21089 /* Move element ELT in LIST to the front of LIST.
21090 Return the updated list. */
21091
21092 static Lisp_Object
21093 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21094 {
21095 register Lisp_Object tail, prev;
21096 register Lisp_Object tem;
21097
21098 tail = list;
21099 prev = Qnil;
21100 while (CONSP (tail))
21101 {
21102 tem = XCAR (tail);
21103
21104 if (EQ (elt, tem))
21105 {
21106 /* Splice out the link TAIL. */
21107 if (NILP (prev))
21108 list = XCDR (tail);
21109 else
21110 Fsetcdr (prev, XCDR (tail));
21111
21112 /* Now make it the first. */
21113 Fsetcdr (tail, list);
21114 return tail;
21115 }
21116 else
21117 prev = tail;
21118 tail = XCDR (tail);
21119 QUIT;
21120 }
21121
21122 /* Not found--return unchanged LIST. */
21123 return list;
21124 }
21125
21126 /* Contribute ELT to the mode line for window IT->w. How it
21127 translates into text depends on its data type.
21128
21129 IT describes the display environment in which we display, as usual.
21130
21131 DEPTH is the depth in recursion. It is used to prevent
21132 infinite recursion here.
21133
21134 FIELD_WIDTH is the number of characters the display of ELT should
21135 occupy in the mode line, and PRECISION is the maximum number of
21136 characters to display from ELT's representation. See
21137 display_string for details.
21138
21139 Returns the hpos of the end of the text generated by ELT.
21140
21141 PROPS is a property list to add to any string we encounter.
21142
21143 If RISKY is nonzero, remove (disregard) any properties in any string
21144 we encounter, and ignore :eval and :propertize.
21145
21146 The global variable `mode_line_target' determines whether the
21147 output is passed to `store_mode_line_noprop',
21148 `store_mode_line_string', or `display_string'. */
21149
21150 static int
21151 display_mode_element (struct it *it, int depth, int field_width, int precision,
21152 Lisp_Object elt, Lisp_Object props, int risky)
21153 {
21154 int n = 0, field, prec;
21155 int literal = 0;
21156
21157 tail_recurse:
21158 if (depth > 100)
21159 elt = build_string ("*too-deep*");
21160
21161 depth++;
21162
21163 switch (XTYPE (elt))
21164 {
21165 case Lisp_String:
21166 {
21167 /* A string: output it and check for %-constructs within it. */
21168 unsigned char c;
21169 ptrdiff_t offset = 0;
21170
21171 if (SCHARS (elt) > 0
21172 && (!NILP (props) || risky))
21173 {
21174 Lisp_Object oprops, aelt;
21175 oprops = Ftext_properties_at (make_number (0), elt);
21176
21177 /* If the starting string's properties are not what
21178 we want, translate the string. Also, if the string
21179 is risky, do that anyway. */
21180
21181 if (NILP (Fequal (props, oprops)) || risky)
21182 {
21183 /* If the starting string has properties,
21184 merge the specified ones onto the existing ones. */
21185 if (! NILP (oprops) && !risky)
21186 {
21187 Lisp_Object tem;
21188
21189 oprops = Fcopy_sequence (oprops);
21190 tem = props;
21191 while (CONSP (tem))
21192 {
21193 oprops = Fplist_put (oprops, XCAR (tem),
21194 XCAR (XCDR (tem)));
21195 tem = XCDR (XCDR (tem));
21196 }
21197 props = oprops;
21198 }
21199
21200 aelt = Fassoc (elt, mode_line_proptrans_alist);
21201 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21202 {
21203 /* AELT is what we want. Move it to the front
21204 without consing. */
21205 elt = XCAR (aelt);
21206 mode_line_proptrans_alist
21207 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21208 }
21209 else
21210 {
21211 Lisp_Object tem;
21212
21213 /* If AELT has the wrong props, it is useless.
21214 so get rid of it. */
21215 if (! NILP (aelt))
21216 mode_line_proptrans_alist
21217 = Fdelq (aelt, mode_line_proptrans_alist);
21218
21219 elt = Fcopy_sequence (elt);
21220 Fset_text_properties (make_number (0), Flength (elt),
21221 props, elt);
21222 /* Add this item to mode_line_proptrans_alist. */
21223 mode_line_proptrans_alist
21224 = Fcons (Fcons (elt, props),
21225 mode_line_proptrans_alist);
21226 /* Truncate mode_line_proptrans_alist
21227 to at most 50 elements. */
21228 tem = Fnthcdr (make_number (50),
21229 mode_line_proptrans_alist);
21230 if (! NILP (tem))
21231 XSETCDR (tem, Qnil);
21232 }
21233 }
21234 }
21235
21236 offset = 0;
21237
21238 if (literal)
21239 {
21240 prec = precision - n;
21241 switch (mode_line_target)
21242 {
21243 case MODE_LINE_NOPROP:
21244 case MODE_LINE_TITLE:
21245 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21246 break;
21247 case MODE_LINE_STRING:
21248 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21249 break;
21250 case MODE_LINE_DISPLAY:
21251 n += display_string (NULL, elt, Qnil, 0, 0, it,
21252 0, prec, 0, STRING_MULTIBYTE (elt));
21253 break;
21254 }
21255
21256 break;
21257 }
21258
21259 /* Handle the non-literal case. */
21260
21261 while ((precision <= 0 || n < precision)
21262 && SREF (elt, offset) != 0
21263 && (mode_line_target != MODE_LINE_DISPLAY
21264 || it->current_x < it->last_visible_x))
21265 {
21266 ptrdiff_t last_offset = offset;
21267
21268 /* Advance to end of string or next format specifier. */
21269 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21270 ;
21271
21272 if (offset - 1 != last_offset)
21273 {
21274 ptrdiff_t nchars, nbytes;
21275
21276 /* Output to end of string or up to '%'. Field width
21277 is length of string. Don't output more than
21278 PRECISION allows us. */
21279 offset--;
21280
21281 prec = c_string_width (SDATA (elt) + last_offset,
21282 offset - last_offset, precision - n,
21283 &nchars, &nbytes);
21284
21285 switch (mode_line_target)
21286 {
21287 case MODE_LINE_NOPROP:
21288 case MODE_LINE_TITLE:
21289 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21290 break;
21291 case MODE_LINE_STRING:
21292 {
21293 ptrdiff_t bytepos = last_offset;
21294 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21295 ptrdiff_t endpos = (precision <= 0
21296 ? string_byte_to_char (elt, offset)
21297 : charpos + nchars);
21298
21299 n += store_mode_line_string (NULL,
21300 Fsubstring (elt, make_number (charpos),
21301 make_number (endpos)),
21302 0, 0, 0, Qnil);
21303 }
21304 break;
21305 case MODE_LINE_DISPLAY:
21306 {
21307 ptrdiff_t bytepos = last_offset;
21308 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21309
21310 if (precision <= 0)
21311 nchars = string_byte_to_char (elt, offset) - charpos;
21312 n += display_string (NULL, elt, Qnil, 0, charpos,
21313 it, 0, nchars, 0,
21314 STRING_MULTIBYTE (elt));
21315 }
21316 break;
21317 }
21318 }
21319 else /* c == '%' */
21320 {
21321 ptrdiff_t percent_position = offset;
21322
21323 /* Get the specified minimum width. Zero means
21324 don't pad. */
21325 field = 0;
21326 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21327 field = field * 10 + c - '0';
21328
21329 /* Don't pad beyond the total padding allowed. */
21330 if (field_width - n > 0 && field > field_width - n)
21331 field = field_width - n;
21332
21333 /* Note that either PRECISION <= 0 or N < PRECISION. */
21334 prec = precision - n;
21335
21336 if (c == 'M')
21337 n += display_mode_element (it, depth, field, prec,
21338 Vglobal_mode_string, props,
21339 risky);
21340 else if (c != 0)
21341 {
21342 bool multibyte;
21343 ptrdiff_t bytepos, charpos;
21344 const char *spec;
21345 Lisp_Object string;
21346
21347 bytepos = percent_position;
21348 charpos = (STRING_MULTIBYTE (elt)
21349 ? string_byte_to_char (elt, bytepos)
21350 : bytepos);
21351 spec = decode_mode_spec (it->w, c, field, &string);
21352 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21353
21354 switch (mode_line_target)
21355 {
21356 case MODE_LINE_NOPROP:
21357 case MODE_LINE_TITLE:
21358 n += store_mode_line_noprop (spec, field, prec);
21359 break;
21360 case MODE_LINE_STRING:
21361 {
21362 Lisp_Object tem = build_string (spec);
21363 props = Ftext_properties_at (make_number (charpos), elt);
21364 /* Should only keep face property in props */
21365 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21366 }
21367 break;
21368 case MODE_LINE_DISPLAY:
21369 {
21370 int nglyphs_before, nwritten;
21371
21372 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21373 nwritten = display_string (spec, string, elt,
21374 charpos, 0, it,
21375 field, prec, 0,
21376 multibyte);
21377
21378 /* Assign to the glyphs written above the
21379 string where the `%x' came from, position
21380 of the `%'. */
21381 if (nwritten > 0)
21382 {
21383 struct glyph *glyph
21384 = (it->glyph_row->glyphs[TEXT_AREA]
21385 + nglyphs_before);
21386 int i;
21387
21388 for (i = 0; i < nwritten; ++i)
21389 {
21390 glyph[i].object = elt;
21391 glyph[i].charpos = charpos;
21392 }
21393
21394 n += nwritten;
21395 }
21396 }
21397 break;
21398 }
21399 }
21400 else /* c == 0 */
21401 break;
21402 }
21403 }
21404 }
21405 break;
21406
21407 case Lisp_Symbol:
21408 /* A symbol: process the value of the symbol recursively
21409 as if it appeared here directly. Avoid error if symbol void.
21410 Special case: if value of symbol is a string, output the string
21411 literally. */
21412 {
21413 register Lisp_Object tem;
21414
21415 /* If the variable is not marked as risky to set
21416 then its contents are risky to use. */
21417 if (NILP (Fget (elt, Qrisky_local_variable)))
21418 risky = 1;
21419
21420 tem = Fboundp (elt);
21421 if (!NILP (tem))
21422 {
21423 tem = Fsymbol_value (elt);
21424 /* If value is a string, output that string literally:
21425 don't check for % within it. */
21426 if (STRINGP (tem))
21427 literal = 1;
21428
21429 if (!EQ (tem, elt))
21430 {
21431 /* Give up right away for nil or t. */
21432 elt = tem;
21433 goto tail_recurse;
21434 }
21435 }
21436 }
21437 break;
21438
21439 case Lisp_Cons:
21440 {
21441 register Lisp_Object car, tem;
21442
21443 /* A cons cell: five distinct cases.
21444 If first element is :eval or :propertize, do something special.
21445 If first element is a string or a cons, process all the elements
21446 and effectively concatenate them.
21447 If first element is a negative number, truncate displaying cdr to
21448 at most that many characters. If positive, pad (with spaces)
21449 to at least that many characters.
21450 If first element is a symbol, process the cadr or caddr recursively
21451 according to whether the symbol's value is non-nil or nil. */
21452 car = XCAR (elt);
21453 if (EQ (car, QCeval))
21454 {
21455 /* An element of the form (:eval FORM) means evaluate FORM
21456 and use the result as mode line elements. */
21457
21458 if (risky)
21459 break;
21460
21461 if (CONSP (XCDR (elt)))
21462 {
21463 Lisp_Object spec;
21464 spec = safe_eval (XCAR (XCDR (elt)));
21465 n += display_mode_element (it, depth, field_width - n,
21466 precision - n, spec, props,
21467 risky);
21468 }
21469 }
21470 else if (EQ (car, QCpropertize))
21471 {
21472 /* An element of the form (:propertize ELT PROPS...)
21473 means display ELT but applying properties PROPS. */
21474
21475 if (risky)
21476 break;
21477
21478 if (CONSP (XCDR (elt)))
21479 n += display_mode_element (it, depth, field_width - n,
21480 precision - n, XCAR (XCDR (elt)),
21481 XCDR (XCDR (elt)), risky);
21482 }
21483 else if (SYMBOLP (car))
21484 {
21485 tem = Fboundp (car);
21486 elt = XCDR (elt);
21487 if (!CONSP (elt))
21488 goto invalid;
21489 /* elt is now the cdr, and we know it is a cons cell.
21490 Use its car if CAR has a non-nil value. */
21491 if (!NILP (tem))
21492 {
21493 tem = Fsymbol_value (car);
21494 if (!NILP (tem))
21495 {
21496 elt = XCAR (elt);
21497 goto tail_recurse;
21498 }
21499 }
21500 /* Symbol's value is nil (or symbol is unbound)
21501 Get the cddr of the original list
21502 and if possible find the caddr and use that. */
21503 elt = XCDR (elt);
21504 if (NILP (elt))
21505 break;
21506 else if (!CONSP (elt))
21507 goto invalid;
21508 elt = XCAR (elt);
21509 goto tail_recurse;
21510 }
21511 else if (INTEGERP (car))
21512 {
21513 register int lim = XINT (car);
21514 elt = XCDR (elt);
21515 if (lim < 0)
21516 {
21517 /* Negative int means reduce maximum width. */
21518 if (precision <= 0)
21519 precision = -lim;
21520 else
21521 precision = min (precision, -lim);
21522 }
21523 else if (lim > 0)
21524 {
21525 /* Padding specified. Don't let it be more than
21526 current maximum. */
21527 if (precision > 0)
21528 lim = min (precision, lim);
21529
21530 /* If that's more padding than already wanted, queue it.
21531 But don't reduce padding already specified even if
21532 that is beyond the current truncation point. */
21533 field_width = max (lim, field_width);
21534 }
21535 goto tail_recurse;
21536 }
21537 else if (STRINGP (car) || CONSP (car))
21538 {
21539 Lisp_Object halftail = elt;
21540 int len = 0;
21541
21542 while (CONSP (elt)
21543 && (precision <= 0 || n < precision))
21544 {
21545 n += display_mode_element (it, depth,
21546 /* Do padding only after the last
21547 element in the list. */
21548 (! CONSP (XCDR (elt))
21549 ? field_width - n
21550 : 0),
21551 precision - n, XCAR (elt),
21552 props, risky);
21553 elt = XCDR (elt);
21554 len++;
21555 if ((len & 1) == 0)
21556 halftail = XCDR (halftail);
21557 /* Check for cycle. */
21558 if (EQ (halftail, elt))
21559 break;
21560 }
21561 }
21562 }
21563 break;
21564
21565 default:
21566 invalid:
21567 elt = build_string ("*invalid*");
21568 goto tail_recurse;
21569 }
21570
21571 /* Pad to FIELD_WIDTH. */
21572 if (field_width > 0 && n < field_width)
21573 {
21574 switch (mode_line_target)
21575 {
21576 case MODE_LINE_NOPROP:
21577 case MODE_LINE_TITLE:
21578 n += store_mode_line_noprop ("", field_width - n, 0);
21579 break;
21580 case MODE_LINE_STRING:
21581 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21582 break;
21583 case MODE_LINE_DISPLAY:
21584 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21585 0, 0, 0);
21586 break;
21587 }
21588 }
21589
21590 return n;
21591 }
21592
21593 /* Store a mode-line string element in mode_line_string_list.
21594
21595 If STRING is non-null, display that C string. Otherwise, the Lisp
21596 string LISP_STRING is displayed.
21597
21598 FIELD_WIDTH is the minimum number of output glyphs to produce.
21599 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21600 with spaces. FIELD_WIDTH <= 0 means don't pad.
21601
21602 PRECISION is the maximum number of characters to output from
21603 STRING. PRECISION <= 0 means don't truncate the string.
21604
21605 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21606 properties to the string.
21607
21608 PROPS are the properties to add to the string.
21609 The mode_line_string_face face property is always added to the string.
21610 */
21611
21612 static int
21613 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21614 int field_width, int precision, Lisp_Object props)
21615 {
21616 ptrdiff_t len;
21617 int n = 0;
21618
21619 if (string != NULL)
21620 {
21621 len = strlen (string);
21622 if (precision > 0 && len > precision)
21623 len = precision;
21624 lisp_string = make_string (string, len);
21625 if (NILP (props))
21626 props = mode_line_string_face_prop;
21627 else if (!NILP (mode_line_string_face))
21628 {
21629 Lisp_Object face = Fplist_get (props, Qface);
21630 props = Fcopy_sequence (props);
21631 if (NILP (face))
21632 face = mode_line_string_face;
21633 else
21634 face = list2 (face, mode_line_string_face);
21635 props = Fplist_put (props, Qface, face);
21636 }
21637 Fadd_text_properties (make_number (0), make_number (len),
21638 props, lisp_string);
21639 }
21640 else
21641 {
21642 len = XFASTINT (Flength (lisp_string));
21643 if (precision > 0 && len > precision)
21644 {
21645 len = precision;
21646 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21647 precision = -1;
21648 }
21649 if (!NILP (mode_line_string_face))
21650 {
21651 Lisp_Object face;
21652 if (NILP (props))
21653 props = Ftext_properties_at (make_number (0), lisp_string);
21654 face = Fplist_get (props, Qface);
21655 if (NILP (face))
21656 face = mode_line_string_face;
21657 else
21658 face = list2 (face, mode_line_string_face);
21659 props = list2 (Qface, face);
21660 if (copy_string)
21661 lisp_string = Fcopy_sequence (lisp_string);
21662 }
21663 if (!NILP (props))
21664 Fadd_text_properties (make_number (0), make_number (len),
21665 props, lisp_string);
21666 }
21667
21668 if (len > 0)
21669 {
21670 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21671 n += len;
21672 }
21673
21674 if (field_width > len)
21675 {
21676 field_width -= len;
21677 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21678 if (!NILP (props))
21679 Fadd_text_properties (make_number (0), make_number (field_width),
21680 props, lisp_string);
21681 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21682 n += field_width;
21683 }
21684
21685 return n;
21686 }
21687
21688
21689 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21690 1, 4, 0,
21691 doc: /* Format a string out of a mode line format specification.
21692 First arg FORMAT specifies the mode line format (see `mode-line-format'
21693 for details) to use.
21694
21695 By default, the format is evaluated for the currently selected window.
21696
21697 Optional second arg FACE specifies the face property to put on all
21698 characters for which no face is specified. The value nil means the
21699 default face. The value t means whatever face the window's mode line
21700 currently uses (either `mode-line' or `mode-line-inactive',
21701 depending on whether the window is the selected window or not).
21702 An integer value means the value string has no text
21703 properties.
21704
21705 Optional third and fourth args WINDOW and BUFFER specify the window
21706 and buffer to use as the context for the formatting (defaults
21707 are the selected window and the WINDOW's buffer). */)
21708 (Lisp_Object format, Lisp_Object face,
21709 Lisp_Object window, Lisp_Object buffer)
21710 {
21711 struct it it;
21712 int len;
21713 struct window *w;
21714 struct buffer *old_buffer = NULL;
21715 int face_id;
21716 int no_props = INTEGERP (face);
21717 ptrdiff_t count = SPECPDL_INDEX ();
21718 Lisp_Object str;
21719 int string_start = 0;
21720
21721 w = decode_any_window (window);
21722 XSETWINDOW (window, w);
21723
21724 if (NILP (buffer))
21725 buffer = w->contents;
21726 CHECK_BUFFER (buffer);
21727
21728 /* Make formatting the modeline a non-op when noninteractive, otherwise
21729 there will be problems later caused by a partially initialized frame. */
21730 if (NILP (format) || noninteractive)
21731 return empty_unibyte_string;
21732
21733 if (no_props)
21734 face = Qnil;
21735
21736 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21737 : EQ (face, Qt) ? (EQ (window, selected_window)
21738 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21739 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21740 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21741 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21742 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21743 : DEFAULT_FACE_ID;
21744
21745 old_buffer = current_buffer;
21746
21747 /* Save things including mode_line_proptrans_alist,
21748 and set that to nil so that we don't alter the outer value. */
21749 record_unwind_protect (unwind_format_mode_line,
21750 format_mode_line_unwind_data
21751 (XFRAME (WINDOW_FRAME (w)),
21752 old_buffer, selected_window, 1));
21753 mode_line_proptrans_alist = Qnil;
21754
21755 Fselect_window (window, Qt);
21756 set_buffer_internal_1 (XBUFFER (buffer));
21757
21758 init_iterator (&it, w, -1, -1, NULL, face_id);
21759
21760 if (no_props)
21761 {
21762 mode_line_target = MODE_LINE_NOPROP;
21763 mode_line_string_face_prop = Qnil;
21764 mode_line_string_list = Qnil;
21765 string_start = MODE_LINE_NOPROP_LEN (0);
21766 }
21767 else
21768 {
21769 mode_line_target = MODE_LINE_STRING;
21770 mode_line_string_list = Qnil;
21771 mode_line_string_face = face;
21772 mode_line_string_face_prop
21773 = NILP (face) ? Qnil : list2 (Qface, face);
21774 }
21775
21776 push_kboard (FRAME_KBOARD (it.f));
21777 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21778 pop_kboard ();
21779
21780 if (no_props)
21781 {
21782 len = MODE_LINE_NOPROP_LEN (string_start);
21783 str = make_string (mode_line_noprop_buf + string_start, len);
21784 }
21785 else
21786 {
21787 mode_line_string_list = Fnreverse (mode_line_string_list);
21788 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21789 empty_unibyte_string);
21790 }
21791
21792 unbind_to (count, Qnil);
21793 return str;
21794 }
21795
21796 /* Write a null-terminated, right justified decimal representation of
21797 the positive integer D to BUF using a minimal field width WIDTH. */
21798
21799 static void
21800 pint2str (register char *buf, register int width, register ptrdiff_t d)
21801 {
21802 register char *p = buf;
21803
21804 if (d <= 0)
21805 *p++ = '0';
21806 else
21807 {
21808 while (d > 0)
21809 {
21810 *p++ = d % 10 + '0';
21811 d /= 10;
21812 }
21813 }
21814
21815 for (width -= (int) (p - buf); width > 0; --width)
21816 *p++ = ' ';
21817 *p-- = '\0';
21818 while (p > buf)
21819 {
21820 d = *buf;
21821 *buf++ = *p;
21822 *p-- = d;
21823 }
21824 }
21825
21826 /* Write a null-terminated, right justified decimal and "human
21827 readable" representation of the nonnegative integer D to BUF using
21828 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21829
21830 static const char power_letter[] =
21831 {
21832 0, /* no letter */
21833 'k', /* kilo */
21834 'M', /* mega */
21835 'G', /* giga */
21836 'T', /* tera */
21837 'P', /* peta */
21838 'E', /* exa */
21839 'Z', /* zetta */
21840 'Y' /* yotta */
21841 };
21842
21843 static void
21844 pint2hrstr (char *buf, int width, ptrdiff_t d)
21845 {
21846 /* We aim to represent the nonnegative integer D as
21847 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21848 ptrdiff_t quotient = d;
21849 int remainder = 0;
21850 /* -1 means: do not use TENTHS. */
21851 int tenths = -1;
21852 int exponent = 0;
21853
21854 /* Length of QUOTIENT.TENTHS as a string. */
21855 int length;
21856
21857 char * psuffix;
21858 char * p;
21859
21860 if (quotient >= 1000)
21861 {
21862 /* Scale to the appropriate EXPONENT. */
21863 do
21864 {
21865 remainder = quotient % 1000;
21866 quotient /= 1000;
21867 exponent++;
21868 }
21869 while (quotient >= 1000);
21870
21871 /* Round to nearest and decide whether to use TENTHS or not. */
21872 if (quotient <= 9)
21873 {
21874 tenths = remainder / 100;
21875 if (remainder % 100 >= 50)
21876 {
21877 if (tenths < 9)
21878 tenths++;
21879 else
21880 {
21881 quotient++;
21882 if (quotient == 10)
21883 tenths = -1;
21884 else
21885 tenths = 0;
21886 }
21887 }
21888 }
21889 else
21890 if (remainder >= 500)
21891 {
21892 if (quotient < 999)
21893 quotient++;
21894 else
21895 {
21896 quotient = 1;
21897 exponent++;
21898 tenths = 0;
21899 }
21900 }
21901 }
21902
21903 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21904 if (tenths == -1 && quotient <= 99)
21905 if (quotient <= 9)
21906 length = 1;
21907 else
21908 length = 2;
21909 else
21910 length = 3;
21911 p = psuffix = buf + max (width, length);
21912
21913 /* Print EXPONENT. */
21914 *psuffix++ = power_letter[exponent];
21915 *psuffix = '\0';
21916
21917 /* Print TENTHS. */
21918 if (tenths >= 0)
21919 {
21920 *--p = '0' + tenths;
21921 *--p = '.';
21922 }
21923
21924 /* Print QUOTIENT. */
21925 do
21926 {
21927 int digit = quotient % 10;
21928 *--p = '0' + digit;
21929 }
21930 while ((quotient /= 10) != 0);
21931
21932 /* Print leading spaces. */
21933 while (buf < p)
21934 *--p = ' ';
21935 }
21936
21937 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21938 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21939 type of CODING_SYSTEM. Return updated pointer into BUF. */
21940
21941 static unsigned char invalid_eol_type[] = "(*invalid*)";
21942
21943 static char *
21944 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21945 {
21946 Lisp_Object val;
21947 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21948 const unsigned char *eol_str;
21949 int eol_str_len;
21950 /* The EOL conversion we are using. */
21951 Lisp_Object eoltype;
21952
21953 val = CODING_SYSTEM_SPEC (coding_system);
21954 eoltype = Qnil;
21955
21956 if (!VECTORP (val)) /* Not yet decided. */
21957 {
21958 *buf++ = multibyte ? '-' : ' ';
21959 if (eol_flag)
21960 eoltype = eol_mnemonic_undecided;
21961 /* Don't mention EOL conversion if it isn't decided. */
21962 }
21963 else
21964 {
21965 Lisp_Object attrs;
21966 Lisp_Object eolvalue;
21967
21968 attrs = AREF (val, 0);
21969 eolvalue = AREF (val, 2);
21970
21971 *buf++ = multibyte
21972 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21973 : ' ';
21974
21975 if (eol_flag)
21976 {
21977 /* The EOL conversion that is normal on this system. */
21978
21979 if (NILP (eolvalue)) /* Not yet decided. */
21980 eoltype = eol_mnemonic_undecided;
21981 else if (VECTORP (eolvalue)) /* Not yet decided. */
21982 eoltype = eol_mnemonic_undecided;
21983 else /* eolvalue is Qunix, Qdos, or Qmac. */
21984 eoltype = (EQ (eolvalue, Qunix)
21985 ? eol_mnemonic_unix
21986 : (EQ (eolvalue, Qdos) == 1
21987 ? eol_mnemonic_dos : eol_mnemonic_mac));
21988 }
21989 }
21990
21991 if (eol_flag)
21992 {
21993 /* Mention the EOL conversion if it is not the usual one. */
21994 if (STRINGP (eoltype))
21995 {
21996 eol_str = SDATA (eoltype);
21997 eol_str_len = SBYTES (eoltype);
21998 }
21999 else if (CHARACTERP (eoltype))
22000 {
22001 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22002 int c = XFASTINT (eoltype);
22003 eol_str_len = CHAR_STRING (c, tmp);
22004 eol_str = tmp;
22005 }
22006 else
22007 {
22008 eol_str = invalid_eol_type;
22009 eol_str_len = sizeof (invalid_eol_type) - 1;
22010 }
22011 memcpy (buf, eol_str, eol_str_len);
22012 buf += eol_str_len;
22013 }
22014
22015 return buf;
22016 }
22017
22018 /* Return a string for the output of a mode line %-spec for window W,
22019 generated by character C. FIELD_WIDTH > 0 means pad the string
22020 returned with spaces to that value. Return a Lisp string in
22021 *STRING if the resulting string is taken from that Lisp string.
22022
22023 Note we operate on the current buffer for most purposes. */
22024
22025 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22026
22027 static const char *
22028 decode_mode_spec (struct window *w, register int c, int field_width,
22029 Lisp_Object *string)
22030 {
22031 Lisp_Object obj;
22032 struct frame *f = XFRAME (WINDOW_FRAME (w));
22033 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22034 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22035 produce strings from numerical values, so limit preposterously
22036 large values of FIELD_WIDTH to avoid overrunning the buffer's
22037 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22038 bytes plus the terminating null. */
22039 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22040 struct buffer *b = current_buffer;
22041
22042 obj = Qnil;
22043 *string = Qnil;
22044
22045 switch (c)
22046 {
22047 case '*':
22048 if (!NILP (BVAR (b, read_only)))
22049 return "%";
22050 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22051 return "*";
22052 return "-";
22053
22054 case '+':
22055 /* This differs from %* only for a modified read-only buffer. */
22056 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22057 return "*";
22058 if (!NILP (BVAR (b, read_only)))
22059 return "%";
22060 return "-";
22061
22062 case '&':
22063 /* This differs from %* in ignoring read-only-ness. */
22064 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22065 return "*";
22066 return "-";
22067
22068 case '%':
22069 return "%";
22070
22071 case '[':
22072 {
22073 int i;
22074 char *p;
22075
22076 if (command_loop_level > 5)
22077 return "[[[... ";
22078 p = decode_mode_spec_buf;
22079 for (i = 0; i < command_loop_level; i++)
22080 *p++ = '[';
22081 *p = 0;
22082 return decode_mode_spec_buf;
22083 }
22084
22085 case ']':
22086 {
22087 int i;
22088 char *p;
22089
22090 if (command_loop_level > 5)
22091 return " ...]]]";
22092 p = decode_mode_spec_buf;
22093 for (i = 0; i < command_loop_level; i++)
22094 *p++ = ']';
22095 *p = 0;
22096 return decode_mode_spec_buf;
22097 }
22098
22099 case '-':
22100 {
22101 register int i;
22102
22103 /* Let lots_of_dashes be a string of infinite length. */
22104 if (mode_line_target == MODE_LINE_NOPROP
22105 || mode_line_target == MODE_LINE_STRING)
22106 return "--";
22107 if (field_width <= 0
22108 || field_width > sizeof (lots_of_dashes))
22109 {
22110 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22111 decode_mode_spec_buf[i] = '-';
22112 decode_mode_spec_buf[i] = '\0';
22113 return decode_mode_spec_buf;
22114 }
22115 else
22116 return lots_of_dashes;
22117 }
22118
22119 case 'b':
22120 obj = BVAR (b, name);
22121 break;
22122
22123 case 'c':
22124 /* %c and %l are ignored in `frame-title-format'.
22125 (In redisplay_internal, the frame title is drawn _before_ the
22126 windows are updated, so the stuff which depends on actual
22127 window contents (such as %l) may fail to render properly, or
22128 even crash emacs.) */
22129 if (mode_line_target == MODE_LINE_TITLE)
22130 return "";
22131 else
22132 {
22133 ptrdiff_t col = current_column ();
22134 w->column_number_displayed = col;
22135 pint2str (decode_mode_spec_buf, width, col);
22136 return decode_mode_spec_buf;
22137 }
22138
22139 case 'e':
22140 #ifndef SYSTEM_MALLOC
22141 {
22142 if (NILP (Vmemory_full))
22143 return "";
22144 else
22145 return "!MEM FULL! ";
22146 }
22147 #else
22148 return "";
22149 #endif
22150
22151 case 'F':
22152 /* %F displays the frame name. */
22153 if (!NILP (f->title))
22154 return SSDATA (f->title);
22155 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22156 return SSDATA (f->name);
22157 return "Emacs";
22158
22159 case 'f':
22160 obj = BVAR (b, filename);
22161 break;
22162
22163 case 'i':
22164 {
22165 ptrdiff_t size = ZV - BEGV;
22166 pint2str (decode_mode_spec_buf, width, size);
22167 return decode_mode_spec_buf;
22168 }
22169
22170 case 'I':
22171 {
22172 ptrdiff_t size = ZV - BEGV;
22173 pint2hrstr (decode_mode_spec_buf, width, size);
22174 return decode_mode_spec_buf;
22175 }
22176
22177 case 'l':
22178 {
22179 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22180 ptrdiff_t topline, nlines, height;
22181 ptrdiff_t junk;
22182
22183 /* %c and %l are ignored in `frame-title-format'. */
22184 if (mode_line_target == MODE_LINE_TITLE)
22185 return "";
22186
22187 startpos = marker_position (w->start);
22188 startpos_byte = marker_byte_position (w->start);
22189 height = WINDOW_TOTAL_LINES (w);
22190
22191 /* If we decided that this buffer isn't suitable for line numbers,
22192 don't forget that too fast. */
22193 if (w->base_line_pos == -1)
22194 goto no_value;
22195
22196 /* If the buffer is very big, don't waste time. */
22197 if (INTEGERP (Vline_number_display_limit)
22198 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22199 {
22200 w->base_line_pos = 0;
22201 w->base_line_number = 0;
22202 goto no_value;
22203 }
22204
22205 if (w->base_line_number > 0
22206 && w->base_line_pos > 0
22207 && w->base_line_pos <= startpos)
22208 {
22209 line = w->base_line_number;
22210 linepos = w->base_line_pos;
22211 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22212 }
22213 else
22214 {
22215 line = 1;
22216 linepos = BUF_BEGV (b);
22217 linepos_byte = BUF_BEGV_BYTE (b);
22218 }
22219
22220 /* Count lines from base line to window start position. */
22221 nlines = display_count_lines (linepos_byte,
22222 startpos_byte,
22223 startpos, &junk);
22224
22225 topline = nlines + line;
22226
22227 /* Determine a new base line, if the old one is too close
22228 or too far away, or if we did not have one.
22229 "Too close" means it's plausible a scroll-down would
22230 go back past it. */
22231 if (startpos == BUF_BEGV (b))
22232 {
22233 w->base_line_number = topline;
22234 w->base_line_pos = BUF_BEGV (b);
22235 }
22236 else if (nlines < height + 25 || nlines > height * 3 + 50
22237 || linepos == BUF_BEGV (b))
22238 {
22239 ptrdiff_t limit = BUF_BEGV (b);
22240 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22241 ptrdiff_t position;
22242 ptrdiff_t distance =
22243 (height * 2 + 30) * line_number_display_limit_width;
22244
22245 if (startpos - distance > limit)
22246 {
22247 limit = startpos - distance;
22248 limit_byte = CHAR_TO_BYTE (limit);
22249 }
22250
22251 nlines = display_count_lines (startpos_byte,
22252 limit_byte,
22253 - (height * 2 + 30),
22254 &position);
22255 /* If we couldn't find the lines we wanted within
22256 line_number_display_limit_width chars per line,
22257 give up on line numbers for this window. */
22258 if (position == limit_byte && limit == startpos - distance)
22259 {
22260 w->base_line_pos = -1;
22261 w->base_line_number = 0;
22262 goto no_value;
22263 }
22264
22265 w->base_line_number = topline - nlines;
22266 w->base_line_pos = BYTE_TO_CHAR (position);
22267 }
22268
22269 /* Now count lines from the start pos to point. */
22270 nlines = display_count_lines (startpos_byte,
22271 PT_BYTE, PT, &junk);
22272
22273 /* Record that we did display the line number. */
22274 line_number_displayed = 1;
22275
22276 /* Make the string to show. */
22277 pint2str (decode_mode_spec_buf, width, topline + nlines);
22278 return decode_mode_spec_buf;
22279 no_value:
22280 {
22281 char* p = decode_mode_spec_buf;
22282 int pad = width - 2;
22283 while (pad-- > 0)
22284 *p++ = ' ';
22285 *p++ = '?';
22286 *p++ = '?';
22287 *p = '\0';
22288 return decode_mode_spec_buf;
22289 }
22290 }
22291 break;
22292
22293 case 'm':
22294 obj = BVAR (b, mode_name);
22295 break;
22296
22297 case 'n':
22298 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22299 return " Narrow";
22300 break;
22301
22302 case 'p':
22303 {
22304 ptrdiff_t pos = marker_position (w->start);
22305 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22306
22307 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22308 {
22309 if (pos <= BUF_BEGV (b))
22310 return "All";
22311 else
22312 return "Bottom";
22313 }
22314 else if (pos <= BUF_BEGV (b))
22315 return "Top";
22316 else
22317 {
22318 if (total > 1000000)
22319 /* Do it differently for a large value, to avoid overflow. */
22320 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22321 else
22322 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22323 /* We can't normally display a 3-digit number,
22324 so get us a 2-digit number that is close. */
22325 if (total == 100)
22326 total = 99;
22327 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22328 return decode_mode_spec_buf;
22329 }
22330 }
22331
22332 /* Display percentage of size above the bottom of the screen. */
22333 case 'P':
22334 {
22335 ptrdiff_t toppos = marker_position (w->start);
22336 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22337 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22338
22339 if (botpos >= BUF_ZV (b))
22340 {
22341 if (toppos <= BUF_BEGV (b))
22342 return "All";
22343 else
22344 return "Bottom";
22345 }
22346 else
22347 {
22348 if (total > 1000000)
22349 /* Do it differently for a large value, to avoid overflow. */
22350 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22351 else
22352 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22353 /* We can't normally display a 3-digit number,
22354 so get us a 2-digit number that is close. */
22355 if (total == 100)
22356 total = 99;
22357 if (toppos <= BUF_BEGV (b))
22358 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22359 else
22360 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22361 return decode_mode_spec_buf;
22362 }
22363 }
22364
22365 case 's':
22366 /* status of process */
22367 obj = Fget_buffer_process (Fcurrent_buffer ());
22368 if (NILP (obj))
22369 return "no process";
22370 #ifndef MSDOS
22371 obj = Fsymbol_name (Fprocess_status (obj));
22372 #endif
22373 break;
22374
22375 case '@':
22376 {
22377 ptrdiff_t count = inhibit_garbage_collection ();
22378 Lisp_Object val = call1 (intern ("file-remote-p"),
22379 BVAR (current_buffer, directory));
22380 unbind_to (count, Qnil);
22381
22382 if (NILP (val))
22383 return "-";
22384 else
22385 return "@";
22386 }
22387
22388 case 'z':
22389 /* coding-system (not including end-of-line format) */
22390 case 'Z':
22391 /* coding-system (including end-of-line type) */
22392 {
22393 int eol_flag = (c == 'Z');
22394 char *p = decode_mode_spec_buf;
22395
22396 if (! FRAME_WINDOW_P (f))
22397 {
22398 /* No need to mention EOL here--the terminal never needs
22399 to do EOL conversion. */
22400 p = decode_mode_spec_coding (CODING_ID_NAME
22401 (FRAME_KEYBOARD_CODING (f)->id),
22402 p, 0);
22403 p = decode_mode_spec_coding (CODING_ID_NAME
22404 (FRAME_TERMINAL_CODING (f)->id),
22405 p, 0);
22406 }
22407 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22408 p, eol_flag);
22409
22410 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22411 #ifdef subprocesses
22412 obj = Fget_buffer_process (Fcurrent_buffer ());
22413 if (PROCESSP (obj))
22414 {
22415 p = decode_mode_spec_coding
22416 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22417 p = decode_mode_spec_coding
22418 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22419 }
22420 #endif /* subprocesses */
22421 #endif /* 0 */
22422 *p = 0;
22423 return decode_mode_spec_buf;
22424 }
22425 }
22426
22427 if (STRINGP (obj))
22428 {
22429 *string = obj;
22430 return SSDATA (obj);
22431 }
22432 else
22433 return "";
22434 }
22435
22436
22437 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22438 means count lines back from START_BYTE. But don't go beyond
22439 LIMIT_BYTE. Return the number of lines thus found (always
22440 nonnegative).
22441
22442 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22443 either the position COUNT lines after/before START_BYTE, if we
22444 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22445 COUNT lines. */
22446
22447 static ptrdiff_t
22448 display_count_lines (ptrdiff_t start_byte,
22449 ptrdiff_t limit_byte, ptrdiff_t count,
22450 ptrdiff_t *byte_pos_ptr)
22451 {
22452 register unsigned char *cursor;
22453 unsigned char *base;
22454
22455 register ptrdiff_t ceiling;
22456 register unsigned char *ceiling_addr;
22457 ptrdiff_t orig_count = count;
22458
22459 /* If we are not in selective display mode,
22460 check only for newlines. */
22461 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22462 && !INTEGERP (BVAR (current_buffer, selective_display)));
22463
22464 if (count > 0)
22465 {
22466 while (start_byte < limit_byte)
22467 {
22468 ceiling = BUFFER_CEILING_OF (start_byte);
22469 ceiling = min (limit_byte - 1, ceiling);
22470 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22471 base = (cursor = BYTE_POS_ADDR (start_byte));
22472
22473 do
22474 {
22475 if (selective_display)
22476 {
22477 while (*cursor != '\n' && *cursor != 015
22478 && ++cursor != ceiling_addr)
22479 continue;
22480 if (cursor == ceiling_addr)
22481 break;
22482 }
22483 else
22484 {
22485 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22486 if (! cursor)
22487 break;
22488 }
22489
22490 cursor++;
22491
22492 if (--count == 0)
22493 {
22494 start_byte += cursor - base;
22495 *byte_pos_ptr = start_byte;
22496 return orig_count;
22497 }
22498 }
22499 while (cursor < ceiling_addr);
22500
22501 start_byte += ceiling_addr - base;
22502 }
22503 }
22504 else
22505 {
22506 while (start_byte > limit_byte)
22507 {
22508 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22509 ceiling = max (limit_byte, ceiling);
22510 ceiling_addr = BYTE_POS_ADDR (ceiling);
22511 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22512 while (1)
22513 {
22514 if (selective_display)
22515 {
22516 while (--cursor >= ceiling_addr
22517 && *cursor != '\n' && *cursor != 015)
22518 continue;
22519 if (cursor < ceiling_addr)
22520 break;
22521 }
22522 else
22523 {
22524 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22525 if (! cursor)
22526 break;
22527 }
22528
22529 if (++count == 0)
22530 {
22531 start_byte += cursor - base + 1;
22532 *byte_pos_ptr = start_byte;
22533 /* When scanning backwards, we should
22534 not count the newline posterior to which we stop. */
22535 return - orig_count - 1;
22536 }
22537 }
22538 start_byte += ceiling_addr - base;
22539 }
22540 }
22541
22542 *byte_pos_ptr = limit_byte;
22543
22544 if (count < 0)
22545 return - orig_count + count;
22546 return orig_count - count;
22547
22548 }
22549
22550
22551 \f
22552 /***********************************************************************
22553 Displaying strings
22554 ***********************************************************************/
22555
22556 /* Display a NUL-terminated string, starting with index START.
22557
22558 If STRING is non-null, display that C string. Otherwise, the Lisp
22559 string LISP_STRING is displayed. There's a case that STRING is
22560 non-null and LISP_STRING is not nil. It means STRING is a string
22561 data of LISP_STRING. In that case, we display LISP_STRING while
22562 ignoring its text properties.
22563
22564 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22565 FACE_STRING. Display STRING or LISP_STRING with the face at
22566 FACE_STRING_POS in FACE_STRING:
22567
22568 Display the string in the environment given by IT, but use the
22569 standard display table, temporarily.
22570
22571 FIELD_WIDTH is the minimum number of output glyphs to produce.
22572 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22573 with spaces. If STRING has more characters, more than FIELD_WIDTH
22574 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22575
22576 PRECISION is the maximum number of characters to output from
22577 STRING. PRECISION < 0 means don't truncate the string.
22578
22579 This is roughly equivalent to printf format specifiers:
22580
22581 FIELD_WIDTH PRECISION PRINTF
22582 ----------------------------------------
22583 -1 -1 %s
22584 -1 10 %.10s
22585 10 -1 %10s
22586 20 10 %20.10s
22587
22588 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22589 display them, and < 0 means obey the current buffer's value of
22590 enable_multibyte_characters.
22591
22592 Value is the number of columns displayed. */
22593
22594 static int
22595 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22596 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22597 int field_width, int precision, int max_x, int multibyte)
22598 {
22599 int hpos_at_start = it->hpos;
22600 int saved_face_id = it->face_id;
22601 struct glyph_row *row = it->glyph_row;
22602 ptrdiff_t it_charpos;
22603
22604 /* Initialize the iterator IT for iteration over STRING beginning
22605 with index START. */
22606 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22607 precision, field_width, multibyte);
22608 if (string && STRINGP (lisp_string))
22609 /* LISP_STRING is the one returned by decode_mode_spec. We should
22610 ignore its text properties. */
22611 it->stop_charpos = it->end_charpos;
22612
22613 /* If displaying STRING, set up the face of the iterator from
22614 FACE_STRING, if that's given. */
22615 if (STRINGP (face_string))
22616 {
22617 ptrdiff_t endptr;
22618 struct face *face;
22619
22620 it->face_id
22621 = face_at_string_position (it->w, face_string, face_string_pos,
22622 0, &endptr, it->base_face_id, 0);
22623 face = FACE_FROM_ID (it->f, it->face_id);
22624 it->face_box_p = face->box != FACE_NO_BOX;
22625 }
22626
22627 /* Set max_x to the maximum allowed X position. Don't let it go
22628 beyond the right edge of the window. */
22629 if (max_x <= 0)
22630 max_x = it->last_visible_x;
22631 else
22632 max_x = min (max_x, it->last_visible_x);
22633
22634 /* Skip over display elements that are not visible. because IT->w is
22635 hscrolled. */
22636 if (it->current_x < it->first_visible_x)
22637 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22638 MOVE_TO_POS | MOVE_TO_X);
22639
22640 row->ascent = it->max_ascent;
22641 row->height = it->max_ascent + it->max_descent;
22642 row->phys_ascent = it->max_phys_ascent;
22643 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22644 row->extra_line_spacing = it->max_extra_line_spacing;
22645
22646 if (STRINGP (it->string))
22647 it_charpos = IT_STRING_CHARPOS (*it);
22648 else
22649 it_charpos = IT_CHARPOS (*it);
22650
22651 /* This condition is for the case that we are called with current_x
22652 past last_visible_x. */
22653 while (it->current_x < max_x)
22654 {
22655 int x_before, x, n_glyphs_before, i, nglyphs;
22656
22657 /* Get the next display element. */
22658 if (!get_next_display_element (it))
22659 break;
22660
22661 /* Produce glyphs. */
22662 x_before = it->current_x;
22663 n_glyphs_before = row->used[TEXT_AREA];
22664 PRODUCE_GLYPHS (it);
22665
22666 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22667 i = 0;
22668 x = x_before;
22669 while (i < nglyphs)
22670 {
22671 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22672
22673 if (it->line_wrap != TRUNCATE
22674 && x + glyph->pixel_width > max_x)
22675 {
22676 /* End of continued line or max_x reached. */
22677 if (CHAR_GLYPH_PADDING_P (*glyph))
22678 {
22679 /* A wide character is unbreakable. */
22680 if (row->reversed_p)
22681 unproduce_glyphs (it, row->used[TEXT_AREA]
22682 - n_glyphs_before);
22683 row->used[TEXT_AREA] = n_glyphs_before;
22684 it->current_x = x_before;
22685 }
22686 else
22687 {
22688 if (row->reversed_p)
22689 unproduce_glyphs (it, row->used[TEXT_AREA]
22690 - (n_glyphs_before + i));
22691 row->used[TEXT_AREA] = n_glyphs_before + i;
22692 it->current_x = x;
22693 }
22694 break;
22695 }
22696 else if (x + glyph->pixel_width >= it->first_visible_x)
22697 {
22698 /* Glyph is at least partially visible. */
22699 ++it->hpos;
22700 if (x < it->first_visible_x)
22701 row->x = x - it->first_visible_x;
22702 }
22703 else
22704 {
22705 /* Glyph is off the left margin of the display area.
22706 Should not happen. */
22707 emacs_abort ();
22708 }
22709
22710 row->ascent = max (row->ascent, it->max_ascent);
22711 row->height = max (row->height, it->max_ascent + it->max_descent);
22712 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22713 row->phys_height = max (row->phys_height,
22714 it->max_phys_ascent + it->max_phys_descent);
22715 row->extra_line_spacing = max (row->extra_line_spacing,
22716 it->max_extra_line_spacing);
22717 x += glyph->pixel_width;
22718 ++i;
22719 }
22720
22721 /* Stop if max_x reached. */
22722 if (i < nglyphs)
22723 break;
22724
22725 /* Stop at line ends. */
22726 if (ITERATOR_AT_END_OF_LINE_P (it))
22727 {
22728 it->continuation_lines_width = 0;
22729 break;
22730 }
22731
22732 set_iterator_to_next (it, 1);
22733 if (STRINGP (it->string))
22734 it_charpos = IT_STRING_CHARPOS (*it);
22735 else
22736 it_charpos = IT_CHARPOS (*it);
22737
22738 /* Stop if truncating at the right edge. */
22739 if (it->line_wrap == TRUNCATE
22740 && it->current_x >= it->last_visible_x)
22741 {
22742 /* Add truncation mark, but don't do it if the line is
22743 truncated at a padding space. */
22744 if (it_charpos < it->string_nchars)
22745 {
22746 if (!FRAME_WINDOW_P (it->f))
22747 {
22748 int ii, n;
22749
22750 if (it->current_x > it->last_visible_x)
22751 {
22752 if (!row->reversed_p)
22753 {
22754 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22755 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22756 break;
22757 }
22758 else
22759 {
22760 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22761 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22762 break;
22763 unproduce_glyphs (it, ii + 1);
22764 ii = row->used[TEXT_AREA] - (ii + 1);
22765 }
22766 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22767 {
22768 row->used[TEXT_AREA] = ii;
22769 produce_special_glyphs (it, IT_TRUNCATION);
22770 }
22771 }
22772 produce_special_glyphs (it, IT_TRUNCATION);
22773 }
22774 row->truncated_on_right_p = 1;
22775 }
22776 break;
22777 }
22778 }
22779
22780 /* Maybe insert a truncation at the left. */
22781 if (it->first_visible_x
22782 && it_charpos > 0)
22783 {
22784 if (!FRAME_WINDOW_P (it->f)
22785 || (row->reversed_p
22786 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22787 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22788 insert_left_trunc_glyphs (it);
22789 row->truncated_on_left_p = 1;
22790 }
22791
22792 it->face_id = saved_face_id;
22793
22794 /* Value is number of columns displayed. */
22795 return it->hpos - hpos_at_start;
22796 }
22797
22798
22799 \f
22800 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22801 appears as an element of LIST or as the car of an element of LIST.
22802 If PROPVAL is a list, compare each element against LIST in that
22803 way, and return 1/2 if any element of PROPVAL is found in LIST.
22804 Otherwise return 0. This function cannot quit.
22805 The return value is 2 if the text is invisible but with an ellipsis
22806 and 1 if it's invisible and without an ellipsis. */
22807
22808 int
22809 invisible_p (register Lisp_Object propval, Lisp_Object list)
22810 {
22811 register Lisp_Object tail, proptail;
22812
22813 for (tail = list; CONSP (tail); tail = XCDR (tail))
22814 {
22815 register Lisp_Object tem;
22816 tem = XCAR (tail);
22817 if (EQ (propval, tem))
22818 return 1;
22819 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22820 return NILP (XCDR (tem)) ? 1 : 2;
22821 }
22822
22823 if (CONSP (propval))
22824 {
22825 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22826 {
22827 Lisp_Object propelt;
22828 propelt = XCAR (proptail);
22829 for (tail = list; CONSP (tail); tail = XCDR (tail))
22830 {
22831 register Lisp_Object tem;
22832 tem = XCAR (tail);
22833 if (EQ (propelt, tem))
22834 return 1;
22835 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22836 return NILP (XCDR (tem)) ? 1 : 2;
22837 }
22838 }
22839 }
22840
22841 return 0;
22842 }
22843
22844 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22845 doc: /* Non-nil if the property makes the text invisible.
22846 POS-OR-PROP can be a marker or number, in which case it is taken to be
22847 a position in the current buffer and the value of the `invisible' property
22848 is checked; or it can be some other value, which is then presumed to be the
22849 value of the `invisible' property of the text of interest.
22850 The non-nil value returned can be t for truly invisible text or something
22851 else if the text is replaced by an ellipsis. */)
22852 (Lisp_Object pos_or_prop)
22853 {
22854 Lisp_Object prop
22855 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22856 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22857 : pos_or_prop);
22858 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22859 return (invis == 0 ? Qnil
22860 : invis == 1 ? Qt
22861 : make_number (invis));
22862 }
22863
22864 /* Calculate a width or height in pixels from a specification using
22865 the following elements:
22866
22867 SPEC ::=
22868 NUM - a (fractional) multiple of the default font width/height
22869 (NUM) - specifies exactly NUM pixels
22870 UNIT - a fixed number of pixels, see below.
22871 ELEMENT - size of a display element in pixels, see below.
22872 (NUM . SPEC) - equals NUM * SPEC
22873 (+ SPEC SPEC ...) - add pixel values
22874 (- SPEC SPEC ...) - subtract pixel values
22875 (- SPEC) - negate pixel value
22876
22877 NUM ::=
22878 INT or FLOAT - a number constant
22879 SYMBOL - use symbol's (buffer local) variable binding.
22880
22881 UNIT ::=
22882 in - pixels per inch *)
22883 mm - pixels per 1/1000 meter *)
22884 cm - pixels per 1/100 meter *)
22885 width - width of current font in pixels.
22886 height - height of current font in pixels.
22887
22888 *) using the ratio(s) defined in display-pixels-per-inch.
22889
22890 ELEMENT ::=
22891
22892 left-fringe - left fringe width in pixels
22893 right-fringe - right fringe width in pixels
22894
22895 left-margin - left margin width in pixels
22896 right-margin - right margin width in pixels
22897
22898 scroll-bar - scroll-bar area width in pixels
22899
22900 Examples:
22901
22902 Pixels corresponding to 5 inches:
22903 (5 . in)
22904
22905 Total width of non-text areas on left side of window (if scroll-bar is on left):
22906 '(space :width (+ left-fringe left-margin scroll-bar))
22907
22908 Align to first text column (in header line):
22909 '(space :align-to 0)
22910
22911 Align to middle of text area minus half the width of variable `my-image'
22912 containing a loaded image:
22913 '(space :align-to (0.5 . (- text my-image)))
22914
22915 Width of left margin minus width of 1 character in the default font:
22916 '(space :width (- left-margin 1))
22917
22918 Width of left margin minus width of 2 characters in the current font:
22919 '(space :width (- left-margin (2 . width)))
22920
22921 Center 1 character over left-margin (in header line):
22922 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22923
22924 Different ways to express width of left fringe plus left margin minus one pixel:
22925 '(space :width (- (+ left-fringe left-margin) (1)))
22926 '(space :width (+ left-fringe left-margin (- (1))))
22927 '(space :width (+ left-fringe left-margin (-1)))
22928
22929 */
22930
22931 static int
22932 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22933 struct font *font, int width_p, int *align_to)
22934 {
22935 double pixels;
22936
22937 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22938 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22939
22940 if (NILP (prop))
22941 return OK_PIXELS (0);
22942
22943 eassert (FRAME_LIVE_P (it->f));
22944
22945 if (SYMBOLP (prop))
22946 {
22947 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22948 {
22949 char *unit = SSDATA (SYMBOL_NAME (prop));
22950
22951 if (unit[0] == 'i' && unit[1] == 'n')
22952 pixels = 1.0;
22953 else if (unit[0] == 'm' && unit[1] == 'm')
22954 pixels = 25.4;
22955 else if (unit[0] == 'c' && unit[1] == 'm')
22956 pixels = 2.54;
22957 else
22958 pixels = 0;
22959 if (pixels > 0)
22960 {
22961 double ppi = (width_p ? FRAME_RES_X (it->f)
22962 : FRAME_RES_Y (it->f));
22963
22964 if (ppi > 0)
22965 return OK_PIXELS (ppi / pixels);
22966 return 0;
22967 }
22968 }
22969
22970 #ifdef HAVE_WINDOW_SYSTEM
22971 if (EQ (prop, Qheight))
22972 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22973 if (EQ (prop, Qwidth))
22974 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22975 #else
22976 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22977 return OK_PIXELS (1);
22978 #endif
22979
22980 if (EQ (prop, Qtext))
22981 return OK_PIXELS (width_p
22982 ? window_box_width (it->w, TEXT_AREA)
22983 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22984
22985 if (align_to && *align_to < 0)
22986 {
22987 *res = 0;
22988 if (EQ (prop, Qleft))
22989 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22990 if (EQ (prop, Qright))
22991 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22992 if (EQ (prop, Qcenter))
22993 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22994 + window_box_width (it->w, TEXT_AREA) / 2);
22995 if (EQ (prop, Qleft_fringe))
22996 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22997 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22998 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22999 if (EQ (prop, Qright_fringe))
23000 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23001 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23002 : window_box_right_offset (it->w, TEXT_AREA));
23003 if (EQ (prop, Qleft_margin))
23004 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23005 if (EQ (prop, Qright_margin))
23006 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23007 if (EQ (prop, Qscroll_bar))
23008 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23009 ? 0
23010 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23011 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23012 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23013 : 0)));
23014 }
23015 else
23016 {
23017 if (EQ (prop, Qleft_fringe))
23018 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23019 if (EQ (prop, Qright_fringe))
23020 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23021 if (EQ (prop, Qleft_margin))
23022 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23023 if (EQ (prop, Qright_margin))
23024 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23025 if (EQ (prop, Qscroll_bar))
23026 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23027 }
23028
23029 prop = buffer_local_value_1 (prop, it->w->contents);
23030 if (EQ (prop, Qunbound))
23031 prop = Qnil;
23032 }
23033
23034 if (INTEGERP (prop) || FLOATP (prop))
23035 {
23036 int base_unit = (width_p
23037 ? FRAME_COLUMN_WIDTH (it->f)
23038 : FRAME_LINE_HEIGHT (it->f));
23039 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23040 }
23041
23042 if (CONSP (prop))
23043 {
23044 Lisp_Object car = XCAR (prop);
23045 Lisp_Object cdr = XCDR (prop);
23046
23047 if (SYMBOLP (car))
23048 {
23049 #ifdef HAVE_WINDOW_SYSTEM
23050 if (FRAME_WINDOW_P (it->f)
23051 && valid_image_p (prop))
23052 {
23053 ptrdiff_t id = lookup_image (it->f, prop);
23054 struct image *img = IMAGE_FROM_ID (it->f, id);
23055
23056 return OK_PIXELS (width_p ? img->width : img->height);
23057 }
23058 #endif
23059 if (EQ (car, Qplus) || EQ (car, Qminus))
23060 {
23061 int first = 1;
23062 double px;
23063
23064 pixels = 0;
23065 while (CONSP (cdr))
23066 {
23067 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23068 font, width_p, align_to))
23069 return 0;
23070 if (first)
23071 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23072 else
23073 pixels += px;
23074 cdr = XCDR (cdr);
23075 }
23076 if (EQ (car, Qminus))
23077 pixels = -pixels;
23078 return OK_PIXELS (pixels);
23079 }
23080
23081 car = buffer_local_value_1 (car, it->w->contents);
23082 if (EQ (car, Qunbound))
23083 car = Qnil;
23084 }
23085
23086 if (INTEGERP (car) || FLOATP (car))
23087 {
23088 double fact;
23089 pixels = XFLOATINT (car);
23090 if (NILP (cdr))
23091 return OK_PIXELS (pixels);
23092 if (calc_pixel_width_or_height (&fact, it, cdr,
23093 font, width_p, align_to))
23094 return OK_PIXELS (pixels * fact);
23095 return 0;
23096 }
23097
23098 return 0;
23099 }
23100
23101 return 0;
23102 }
23103
23104 \f
23105 /***********************************************************************
23106 Glyph Display
23107 ***********************************************************************/
23108
23109 #ifdef HAVE_WINDOW_SYSTEM
23110
23111 #ifdef GLYPH_DEBUG
23112
23113 void
23114 dump_glyph_string (struct glyph_string *s)
23115 {
23116 fprintf (stderr, "glyph string\n");
23117 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23118 s->x, s->y, s->width, s->height);
23119 fprintf (stderr, " ybase = %d\n", s->ybase);
23120 fprintf (stderr, " hl = %d\n", s->hl);
23121 fprintf (stderr, " left overhang = %d, right = %d\n",
23122 s->left_overhang, s->right_overhang);
23123 fprintf (stderr, " nchars = %d\n", s->nchars);
23124 fprintf (stderr, " extends to end of line = %d\n",
23125 s->extends_to_end_of_line_p);
23126 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23127 fprintf (stderr, " bg width = %d\n", s->background_width);
23128 }
23129
23130 #endif /* GLYPH_DEBUG */
23131
23132 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23133 of XChar2b structures for S; it can't be allocated in
23134 init_glyph_string because it must be allocated via `alloca'. W
23135 is the window on which S is drawn. ROW and AREA are the glyph row
23136 and area within the row from which S is constructed. START is the
23137 index of the first glyph structure covered by S. HL is a
23138 face-override for drawing S. */
23139
23140 #ifdef HAVE_NTGUI
23141 #define OPTIONAL_HDC(hdc) HDC hdc,
23142 #define DECLARE_HDC(hdc) HDC hdc;
23143 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23144 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23145 #endif
23146
23147 #ifndef OPTIONAL_HDC
23148 #define OPTIONAL_HDC(hdc)
23149 #define DECLARE_HDC(hdc)
23150 #define ALLOCATE_HDC(hdc, f)
23151 #define RELEASE_HDC(hdc, f)
23152 #endif
23153
23154 static void
23155 init_glyph_string (struct glyph_string *s,
23156 OPTIONAL_HDC (hdc)
23157 XChar2b *char2b, struct window *w, struct glyph_row *row,
23158 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23159 {
23160 memset (s, 0, sizeof *s);
23161 s->w = w;
23162 s->f = XFRAME (w->frame);
23163 #ifdef HAVE_NTGUI
23164 s->hdc = hdc;
23165 #endif
23166 s->display = FRAME_X_DISPLAY (s->f);
23167 s->window = FRAME_X_WINDOW (s->f);
23168 s->char2b = char2b;
23169 s->hl = hl;
23170 s->row = row;
23171 s->area = area;
23172 s->first_glyph = row->glyphs[area] + start;
23173 s->height = row->height;
23174 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23175 s->ybase = s->y + row->ascent;
23176 }
23177
23178
23179 /* Append the list of glyph strings with head H and tail T to the list
23180 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23181
23182 static void
23183 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23184 struct glyph_string *h, struct glyph_string *t)
23185 {
23186 if (h)
23187 {
23188 if (*head)
23189 (*tail)->next = h;
23190 else
23191 *head = h;
23192 h->prev = *tail;
23193 *tail = t;
23194 }
23195 }
23196
23197
23198 /* Prepend the list of glyph strings with head H and tail T to the
23199 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23200 result. */
23201
23202 static void
23203 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23204 struct glyph_string *h, struct glyph_string *t)
23205 {
23206 if (h)
23207 {
23208 if (*head)
23209 (*head)->prev = t;
23210 else
23211 *tail = t;
23212 t->next = *head;
23213 *head = h;
23214 }
23215 }
23216
23217
23218 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23219 Set *HEAD and *TAIL to the resulting list. */
23220
23221 static void
23222 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23223 struct glyph_string *s)
23224 {
23225 s->next = s->prev = NULL;
23226 append_glyph_string_lists (head, tail, s, s);
23227 }
23228
23229
23230 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23231 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23232 make sure that X resources for the face returned are allocated.
23233 Value is a pointer to a realized face that is ready for display if
23234 DISPLAY_P is non-zero. */
23235
23236 static struct face *
23237 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23238 XChar2b *char2b, int display_p)
23239 {
23240 struct face *face = FACE_FROM_ID (f, face_id);
23241 unsigned code = 0;
23242
23243 if (face->font)
23244 {
23245 code = face->font->driver->encode_char (face->font, c);
23246
23247 if (code == FONT_INVALID_CODE)
23248 code = 0;
23249 }
23250 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23251
23252 /* Make sure X resources of the face are allocated. */
23253 #ifdef HAVE_X_WINDOWS
23254 if (display_p)
23255 #endif
23256 {
23257 eassert (face != NULL);
23258 PREPARE_FACE_FOR_DISPLAY (f, face);
23259 }
23260
23261 return face;
23262 }
23263
23264
23265 /* Get face and two-byte form of character glyph GLYPH on frame F.
23266 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23267 a pointer to a realized face that is ready for display. */
23268
23269 static struct face *
23270 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23271 XChar2b *char2b, int *two_byte_p)
23272 {
23273 struct face *face;
23274 unsigned code = 0;
23275
23276 eassert (glyph->type == CHAR_GLYPH);
23277 face = FACE_FROM_ID (f, glyph->face_id);
23278
23279 /* Make sure X resources of the face are allocated. */
23280 eassert (face != NULL);
23281 PREPARE_FACE_FOR_DISPLAY (f, face);
23282
23283 if (two_byte_p)
23284 *two_byte_p = 0;
23285
23286 if (face->font)
23287 {
23288 if (CHAR_BYTE8_P (glyph->u.ch))
23289 code = CHAR_TO_BYTE8 (glyph->u.ch);
23290 else
23291 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23292
23293 if (code == FONT_INVALID_CODE)
23294 code = 0;
23295 }
23296
23297 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23298 return face;
23299 }
23300
23301
23302 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23303 Return 1 if FONT has a glyph for C, otherwise return 0. */
23304
23305 static int
23306 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23307 {
23308 unsigned code;
23309
23310 if (CHAR_BYTE8_P (c))
23311 code = CHAR_TO_BYTE8 (c);
23312 else
23313 code = font->driver->encode_char (font, c);
23314
23315 if (code == FONT_INVALID_CODE)
23316 return 0;
23317 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23318 return 1;
23319 }
23320
23321
23322 /* Fill glyph string S with composition components specified by S->cmp.
23323
23324 BASE_FACE is the base face of the composition.
23325 S->cmp_from is the index of the first component for S.
23326
23327 OVERLAPS non-zero means S should draw the foreground only, and use
23328 its physical height for clipping. See also draw_glyphs.
23329
23330 Value is the index of a component not in S. */
23331
23332 static int
23333 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23334 int overlaps)
23335 {
23336 int i;
23337 /* For all glyphs of this composition, starting at the offset
23338 S->cmp_from, until we reach the end of the definition or encounter a
23339 glyph that requires the different face, add it to S. */
23340 struct face *face;
23341
23342 eassert (s);
23343
23344 s->for_overlaps = overlaps;
23345 s->face = NULL;
23346 s->font = NULL;
23347 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23348 {
23349 int c = COMPOSITION_GLYPH (s->cmp, i);
23350
23351 /* TAB in a composition means display glyphs with padding space
23352 on the left or right. */
23353 if (c != '\t')
23354 {
23355 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23356 -1, Qnil);
23357
23358 face = get_char_face_and_encoding (s->f, c, face_id,
23359 s->char2b + i, 1);
23360 if (face)
23361 {
23362 if (! s->face)
23363 {
23364 s->face = face;
23365 s->font = s->face->font;
23366 }
23367 else if (s->face != face)
23368 break;
23369 }
23370 }
23371 ++s->nchars;
23372 }
23373 s->cmp_to = i;
23374
23375 if (s->face == NULL)
23376 {
23377 s->face = base_face->ascii_face;
23378 s->font = s->face->font;
23379 }
23380
23381 /* All glyph strings for the same composition has the same width,
23382 i.e. the width set for the first component of the composition. */
23383 s->width = s->first_glyph->pixel_width;
23384
23385 /* If the specified font could not be loaded, use the frame's
23386 default font, but record the fact that we couldn't load it in
23387 the glyph string so that we can draw rectangles for the
23388 characters of the glyph string. */
23389 if (s->font == NULL)
23390 {
23391 s->font_not_found_p = 1;
23392 s->font = FRAME_FONT (s->f);
23393 }
23394
23395 /* Adjust base line for subscript/superscript text. */
23396 s->ybase += s->first_glyph->voffset;
23397
23398 /* This glyph string must always be drawn with 16-bit functions. */
23399 s->two_byte_p = 1;
23400
23401 return s->cmp_to;
23402 }
23403
23404 static int
23405 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23406 int start, int end, int overlaps)
23407 {
23408 struct glyph *glyph, *last;
23409 Lisp_Object lgstring;
23410 int i;
23411
23412 s->for_overlaps = overlaps;
23413 glyph = s->row->glyphs[s->area] + start;
23414 last = s->row->glyphs[s->area] + end;
23415 s->cmp_id = glyph->u.cmp.id;
23416 s->cmp_from = glyph->slice.cmp.from;
23417 s->cmp_to = glyph->slice.cmp.to + 1;
23418 s->face = FACE_FROM_ID (s->f, face_id);
23419 lgstring = composition_gstring_from_id (s->cmp_id);
23420 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23421 glyph++;
23422 while (glyph < last
23423 && glyph->u.cmp.automatic
23424 && glyph->u.cmp.id == s->cmp_id
23425 && s->cmp_to == glyph->slice.cmp.from)
23426 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23427
23428 for (i = s->cmp_from; i < s->cmp_to; i++)
23429 {
23430 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23431 unsigned code = LGLYPH_CODE (lglyph);
23432
23433 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23434 }
23435 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23436 return glyph - s->row->glyphs[s->area];
23437 }
23438
23439
23440 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23441 See the comment of fill_glyph_string for arguments.
23442 Value is the index of the first glyph not in S. */
23443
23444
23445 static int
23446 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23447 int start, int end, int overlaps)
23448 {
23449 struct glyph *glyph, *last;
23450 int voffset;
23451
23452 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23453 s->for_overlaps = overlaps;
23454 glyph = s->row->glyphs[s->area] + start;
23455 last = s->row->glyphs[s->area] + end;
23456 voffset = glyph->voffset;
23457 s->face = FACE_FROM_ID (s->f, face_id);
23458 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23459 s->nchars = 1;
23460 s->width = glyph->pixel_width;
23461 glyph++;
23462 while (glyph < last
23463 && glyph->type == GLYPHLESS_GLYPH
23464 && glyph->voffset == voffset
23465 && glyph->face_id == face_id)
23466 {
23467 s->nchars++;
23468 s->width += glyph->pixel_width;
23469 glyph++;
23470 }
23471 s->ybase += voffset;
23472 return glyph - s->row->glyphs[s->area];
23473 }
23474
23475
23476 /* Fill glyph string S from a sequence of character glyphs.
23477
23478 FACE_ID is the face id of the string. START is the index of the
23479 first glyph to consider, END is the index of the last + 1.
23480 OVERLAPS non-zero means S should draw the foreground only, and use
23481 its physical height for clipping. See also draw_glyphs.
23482
23483 Value is the index of the first glyph not in S. */
23484
23485 static int
23486 fill_glyph_string (struct glyph_string *s, int face_id,
23487 int start, int end, int overlaps)
23488 {
23489 struct glyph *glyph, *last;
23490 int voffset;
23491 int glyph_not_available_p;
23492
23493 eassert (s->f == XFRAME (s->w->frame));
23494 eassert (s->nchars == 0);
23495 eassert (start >= 0 && end > start);
23496
23497 s->for_overlaps = overlaps;
23498 glyph = s->row->glyphs[s->area] + start;
23499 last = s->row->glyphs[s->area] + end;
23500 voffset = glyph->voffset;
23501 s->padding_p = glyph->padding_p;
23502 glyph_not_available_p = glyph->glyph_not_available_p;
23503
23504 while (glyph < last
23505 && glyph->type == CHAR_GLYPH
23506 && glyph->voffset == voffset
23507 /* Same face id implies same font, nowadays. */
23508 && glyph->face_id == face_id
23509 && glyph->glyph_not_available_p == glyph_not_available_p)
23510 {
23511 int two_byte_p;
23512
23513 s->face = get_glyph_face_and_encoding (s->f, glyph,
23514 s->char2b + s->nchars,
23515 &two_byte_p);
23516 s->two_byte_p = two_byte_p;
23517 ++s->nchars;
23518 eassert (s->nchars <= end - start);
23519 s->width += glyph->pixel_width;
23520 if (glyph++->padding_p != s->padding_p)
23521 break;
23522 }
23523
23524 s->font = s->face->font;
23525
23526 /* If the specified font could not be loaded, use the frame's font,
23527 but record the fact that we couldn't load it in
23528 S->font_not_found_p so that we can draw rectangles for the
23529 characters of the glyph string. */
23530 if (s->font == NULL || glyph_not_available_p)
23531 {
23532 s->font_not_found_p = 1;
23533 s->font = FRAME_FONT (s->f);
23534 }
23535
23536 /* Adjust base line for subscript/superscript text. */
23537 s->ybase += voffset;
23538
23539 eassert (s->face && s->face->gc);
23540 return glyph - s->row->glyphs[s->area];
23541 }
23542
23543
23544 /* Fill glyph string S from image glyph S->first_glyph. */
23545
23546 static void
23547 fill_image_glyph_string (struct glyph_string *s)
23548 {
23549 eassert (s->first_glyph->type == IMAGE_GLYPH);
23550 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23551 eassert (s->img);
23552 s->slice = s->first_glyph->slice.img;
23553 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23554 s->font = s->face->font;
23555 s->width = s->first_glyph->pixel_width;
23556
23557 /* Adjust base line for subscript/superscript text. */
23558 s->ybase += s->first_glyph->voffset;
23559 }
23560
23561
23562 /* Fill glyph string S from a sequence of stretch glyphs.
23563
23564 START is the index of the first glyph to consider,
23565 END is the index of the last + 1.
23566
23567 Value is the index of the first glyph not in S. */
23568
23569 static int
23570 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23571 {
23572 struct glyph *glyph, *last;
23573 int voffset, face_id;
23574
23575 eassert (s->first_glyph->type == STRETCH_GLYPH);
23576
23577 glyph = s->row->glyphs[s->area] + start;
23578 last = s->row->glyphs[s->area] + end;
23579 face_id = glyph->face_id;
23580 s->face = FACE_FROM_ID (s->f, face_id);
23581 s->font = s->face->font;
23582 s->width = glyph->pixel_width;
23583 s->nchars = 1;
23584 voffset = glyph->voffset;
23585
23586 for (++glyph;
23587 (glyph < last
23588 && glyph->type == STRETCH_GLYPH
23589 && glyph->voffset == voffset
23590 && glyph->face_id == face_id);
23591 ++glyph)
23592 s->width += glyph->pixel_width;
23593
23594 /* Adjust base line for subscript/superscript text. */
23595 s->ybase += voffset;
23596
23597 /* The case that face->gc == 0 is handled when drawing the glyph
23598 string by calling PREPARE_FACE_FOR_DISPLAY. */
23599 eassert (s->face);
23600 return glyph - s->row->glyphs[s->area];
23601 }
23602
23603 static struct font_metrics *
23604 get_per_char_metric (struct font *font, XChar2b *char2b)
23605 {
23606 static struct font_metrics metrics;
23607 unsigned code;
23608
23609 if (! font)
23610 return NULL;
23611 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23612 if (code == FONT_INVALID_CODE)
23613 return NULL;
23614 font->driver->text_extents (font, &code, 1, &metrics);
23615 return &metrics;
23616 }
23617
23618 /* EXPORT for RIF:
23619 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23620 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23621 assumed to be zero. */
23622
23623 void
23624 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23625 {
23626 *left = *right = 0;
23627
23628 if (glyph->type == CHAR_GLYPH)
23629 {
23630 struct face *face;
23631 XChar2b char2b;
23632 struct font_metrics *pcm;
23633
23634 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23635 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23636 {
23637 if (pcm->rbearing > pcm->width)
23638 *right = pcm->rbearing - pcm->width;
23639 if (pcm->lbearing < 0)
23640 *left = -pcm->lbearing;
23641 }
23642 }
23643 else if (glyph->type == COMPOSITE_GLYPH)
23644 {
23645 if (! glyph->u.cmp.automatic)
23646 {
23647 struct composition *cmp = composition_table[glyph->u.cmp.id];
23648
23649 if (cmp->rbearing > cmp->pixel_width)
23650 *right = cmp->rbearing - cmp->pixel_width;
23651 if (cmp->lbearing < 0)
23652 *left = - cmp->lbearing;
23653 }
23654 else
23655 {
23656 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23657 struct font_metrics metrics;
23658
23659 composition_gstring_width (gstring, glyph->slice.cmp.from,
23660 glyph->slice.cmp.to + 1, &metrics);
23661 if (metrics.rbearing > metrics.width)
23662 *right = metrics.rbearing - metrics.width;
23663 if (metrics.lbearing < 0)
23664 *left = - metrics.lbearing;
23665 }
23666 }
23667 }
23668
23669
23670 /* Return the index of the first glyph preceding glyph string S that
23671 is overwritten by S because of S's left overhang. Value is -1
23672 if no glyphs are overwritten. */
23673
23674 static int
23675 left_overwritten (struct glyph_string *s)
23676 {
23677 int k;
23678
23679 if (s->left_overhang)
23680 {
23681 int x = 0, i;
23682 struct glyph *glyphs = s->row->glyphs[s->area];
23683 int first = s->first_glyph - glyphs;
23684
23685 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23686 x -= glyphs[i].pixel_width;
23687
23688 k = i + 1;
23689 }
23690 else
23691 k = -1;
23692
23693 return k;
23694 }
23695
23696
23697 /* Return the index of the first glyph preceding glyph string S that
23698 is overwriting S because of its right overhang. Value is -1 if no
23699 glyph in front of S overwrites S. */
23700
23701 static int
23702 left_overwriting (struct glyph_string *s)
23703 {
23704 int i, k, x;
23705 struct glyph *glyphs = s->row->glyphs[s->area];
23706 int first = s->first_glyph - glyphs;
23707
23708 k = -1;
23709 x = 0;
23710 for (i = first - 1; i >= 0; --i)
23711 {
23712 int left, right;
23713 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23714 if (x + right > 0)
23715 k = i;
23716 x -= glyphs[i].pixel_width;
23717 }
23718
23719 return k;
23720 }
23721
23722
23723 /* Return the index of the last glyph following glyph string S that is
23724 overwritten by S because of S's right overhang. Value is -1 if
23725 no such glyph is found. */
23726
23727 static int
23728 right_overwritten (struct glyph_string *s)
23729 {
23730 int k = -1;
23731
23732 if (s->right_overhang)
23733 {
23734 int x = 0, i;
23735 struct glyph *glyphs = s->row->glyphs[s->area];
23736 int first = (s->first_glyph - glyphs
23737 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23738 int end = s->row->used[s->area];
23739
23740 for (i = first; i < end && s->right_overhang > x; ++i)
23741 x += glyphs[i].pixel_width;
23742
23743 k = i;
23744 }
23745
23746 return k;
23747 }
23748
23749
23750 /* Return the index of the last glyph following glyph string S that
23751 overwrites S because of its left overhang. Value is negative
23752 if no such glyph is found. */
23753
23754 static int
23755 right_overwriting (struct glyph_string *s)
23756 {
23757 int i, k, x;
23758 int end = s->row->used[s->area];
23759 struct glyph *glyphs = s->row->glyphs[s->area];
23760 int first = (s->first_glyph - glyphs
23761 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23762
23763 k = -1;
23764 x = 0;
23765 for (i = first; i < end; ++i)
23766 {
23767 int left, right;
23768 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23769 if (x - left < 0)
23770 k = i;
23771 x += glyphs[i].pixel_width;
23772 }
23773
23774 return k;
23775 }
23776
23777
23778 /* Set background width of glyph string S. START is the index of the
23779 first glyph following S. LAST_X is the right-most x-position + 1
23780 in the drawing area. */
23781
23782 static void
23783 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23784 {
23785 /* If the face of this glyph string has to be drawn to the end of
23786 the drawing area, set S->extends_to_end_of_line_p. */
23787
23788 if (start == s->row->used[s->area]
23789 && s->area == TEXT_AREA
23790 && ((s->row->fill_line_p
23791 && (s->hl == DRAW_NORMAL_TEXT
23792 || s->hl == DRAW_IMAGE_RAISED
23793 || s->hl == DRAW_IMAGE_SUNKEN))
23794 || s->hl == DRAW_MOUSE_FACE))
23795 s->extends_to_end_of_line_p = 1;
23796
23797 /* If S extends its face to the end of the line, set its
23798 background_width to the distance to the right edge of the drawing
23799 area. */
23800 if (s->extends_to_end_of_line_p)
23801 s->background_width = last_x - s->x + 1;
23802 else
23803 s->background_width = s->width;
23804 }
23805
23806
23807 /* Compute overhangs and x-positions for glyph string S and its
23808 predecessors, or successors. X is the starting x-position for S.
23809 BACKWARD_P non-zero means process predecessors. */
23810
23811 static void
23812 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23813 {
23814 if (backward_p)
23815 {
23816 while (s)
23817 {
23818 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23819 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23820 x -= s->width;
23821 s->x = x;
23822 s = s->prev;
23823 }
23824 }
23825 else
23826 {
23827 while (s)
23828 {
23829 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23830 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23831 s->x = x;
23832 x += s->width;
23833 s = s->next;
23834 }
23835 }
23836 }
23837
23838
23839
23840 /* The following macros are only called from draw_glyphs below.
23841 They reference the following parameters of that function directly:
23842 `w', `row', `area', and `overlap_p'
23843 as well as the following local variables:
23844 `s', `f', and `hdc' (in W32) */
23845
23846 #ifdef HAVE_NTGUI
23847 /* On W32, silently add local `hdc' variable to argument list of
23848 init_glyph_string. */
23849 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23850 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23851 #else
23852 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23853 init_glyph_string (s, char2b, w, row, area, start, hl)
23854 #endif
23855
23856 /* Add a glyph string for a stretch glyph to the list of strings
23857 between HEAD and TAIL. START is the index of the stretch glyph in
23858 row area AREA of glyph row ROW. END is the index of the last glyph
23859 in that glyph row area. X is the current output position assigned
23860 to the new glyph string constructed. HL overrides that face of the
23861 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23862 is the right-most x-position of the drawing area. */
23863
23864 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23865 and below -- keep them on one line. */
23866 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23867 do \
23868 { \
23869 s = alloca (sizeof *s); \
23870 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23871 START = fill_stretch_glyph_string (s, START, END); \
23872 append_glyph_string (&HEAD, &TAIL, s); \
23873 s->x = (X); \
23874 } \
23875 while (0)
23876
23877
23878 /* Add a glyph string for an image glyph to the list of strings
23879 between HEAD and TAIL. START is the index of the image glyph in
23880 row area AREA of glyph row ROW. END is the index of the last glyph
23881 in that glyph row area. X is the current output position assigned
23882 to the new glyph string constructed. HL overrides that face of the
23883 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23884 is the right-most x-position of the drawing area. */
23885
23886 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23887 do \
23888 { \
23889 s = alloca (sizeof *s); \
23890 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23891 fill_image_glyph_string (s); \
23892 append_glyph_string (&HEAD, &TAIL, s); \
23893 ++START; \
23894 s->x = (X); \
23895 } \
23896 while (0)
23897
23898
23899 /* Add a glyph string for a sequence of character glyphs to the list
23900 of strings between HEAD and TAIL. START is the index of the first
23901 glyph in row area AREA of glyph row ROW that is part of the new
23902 glyph string. END is the index of the last glyph in that glyph row
23903 area. X is the current output position assigned to the new glyph
23904 string constructed. HL overrides that face of the glyph; e.g. it
23905 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23906 right-most x-position of the drawing area. */
23907
23908 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23909 do \
23910 { \
23911 int face_id; \
23912 XChar2b *char2b; \
23913 \
23914 face_id = (row)->glyphs[area][START].face_id; \
23915 \
23916 s = alloca (sizeof *s); \
23917 char2b = alloca ((END - START) * sizeof *char2b); \
23918 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23919 append_glyph_string (&HEAD, &TAIL, s); \
23920 s->x = (X); \
23921 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23922 } \
23923 while (0)
23924
23925
23926 /* Add a glyph string for a composite sequence to the list of strings
23927 between HEAD and TAIL. START is the index of the first glyph in
23928 row area AREA of glyph row ROW that is part of the new glyph
23929 string. END is the index of the last glyph in that glyph row area.
23930 X is the current output position assigned to the new glyph string
23931 constructed. HL overrides that face of the glyph; e.g. it is
23932 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23933 x-position of the drawing area. */
23934
23935 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23936 do { \
23937 int face_id = (row)->glyphs[area][START].face_id; \
23938 struct face *base_face = FACE_FROM_ID (f, face_id); \
23939 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23940 struct composition *cmp = composition_table[cmp_id]; \
23941 XChar2b *char2b; \
23942 struct glyph_string *first_s = NULL; \
23943 int n; \
23944 \
23945 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23946 \
23947 /* Make glyph_strings for each glyph sequence that is drawable by \
23948 the same face, and append them to HEAD/TAIL. */ \
23949 for (n = 0; n < cmp->glyph_len;) \
23950 { \
23951 s = alloca (sizeof *s); \
23952 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23953 append_glyph_string (&(HEAD), &(TAIL), s); \
23954 s->cmp = cmp; \
23955 s->cmp_from = n; \
23956 s->x = (X); \
23957 if (n == 0) \
23958 first_s = s; \
23959 n = fill_composite_glyph_string (s, base_face, overlaps); \
23960 } \
23961 \
23962 ++START; \
23963 s = first_s; \
23964 } while (0)
23965
23966
23967 /* Add a glyph string for a glyph-string sequence to the list of strings
23968 between HEAD and TAIL. */
23969
23970 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23971 do { \
23972 int face_id; \
23973 XChar2b *char2b; \
23974 Lisp_Object gstring; \
23975 \
23976 face_id = (row)->glyphs[area][START].face_id; \
23977 gstring = (composition_gstring_from_id \
23978 ((row)->glyphs[area][START].u.cmp.id)); \
23979 s = alloca (sizeof *s); \
23980 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23981 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23982 append_glyph_string (&(HEAD), &(TAIL), s); \
23983 s->x = (X); \
23984 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23985 } while (0)
23986
23987
23988 /* Add a glyph string for a sequence of glyphless character's glyphs
23989 to the list of strings between HEAD and TAIL. The meanings of
23990 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23991
23992 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23993 do \
23994 { \
23995 int face_id; \
23996 \
23997 face_id = (row)->glyphs[area][START].face_id; \
23998 \
23999 s = alloca (sizeof *s); \
24000 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24001 append_glyph_string (&HEAD, &TAIL, s); \
24002 s->x = (X); \
24003 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24004 overlaps); \
24005 } \
24006 while (0)
24007
24008
24009 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24010 of AREA of glyph row ROW on window W between indices START and END.
24011 HL overrides the face for drawing glyph strings, e.g. it is
24012 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24013 x-positions of the drawing area.
24014
24015 This is an ugly monster macro construct because we must use alloca
24016 to allocate glyph strings (because draw_glyphs can be called
24017 asynchronously). */
24018
24019 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24020 do \
24021 { \
24022 HEAD = TAIL = NULL; \
24023 while (START < END) \
24024 { \
24025 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24026 switch (first_glyph->type) \
24027 { \
24028 case CHAR_GLYPH: \
24029 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24030 HL, X, LAST_X); \
24031 break; \
24032 \
24033 case COMPOSITE_GLYPH: \
24034 if (first_glyph->u.cmp.automatic) \
24035 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24036 HL, X, LAST_X); \
24037 else \
24038 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24039 HL, X, LAST_X); \
24040 break; \
24041 \
24042 case STRETCH_GLYPH: \
24043 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24044 HL, X, LAST_X); \
24045 break; \
24046 \
24047 case IMAGE_GLYPH: \
24048 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24049 HL, X, LAST_X); \
24050 break; \
24051 \
24052 case GLYPHLESS_GLYPH: \
24053 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24054 HL, X, LAST_X); \
24055 break; \
24056 \
24057 default: \
24058 emacs_abort (); \
24059 } \
24060 \
24061 if (s) \
24062 { \
24063 set_glyph_string_background_width (s, START, LAST_X); \
24064 (X) += s->width; \
24065 } \
24066 } \
24067 } while (0)
24068
24069
24070 /* Draw glyphs between START and END in AREA of ROW on window W,
24071 starting at x-position X. X is relative to AREA in W. HL is a
24072 face-override with the following meaning:
24073
24074 DRAW_NORMAL_TEXT draw normally
24075 DRAW_CURSOR draw in cursor face
24076 DRAW_MOUSE_FACE draw in mouse face.
24077 DRAW_INVERSE_VIDEO draw in mode line face
24078 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24079 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24080
24081 If OVERLAPS is non-zero, draw only the foreground of characters and
24082 clip to the physical height of ROW. Non-zero value also defines
24083 the overlapping part to be drawn:
24084
24085 OVERLAPS_PRED overlap with preceding rows
24086 OVERLAPS_SUCC overlap with succeeding rows
24087 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24088 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24089
24090 Value is the x-position reached, relative to AREA of W. */
24091
24092 static int
24093 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24094 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24095 enum draw_glyphs_face hl, int overlaps)
24096 {
24097 struct glyph_string *head, *tail;
24098 struct glyph_string *s;
24099 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24100 int i, j, x_reached, last_x, area_left = 0;
24101 struct frame *f = XFRAME (WINDOW_FRAME (w));
24102 DECLARE_HDC (hdc);
24103
24104 ALLOCATE_HDC (hdc, f);
24105
24106 /* Let's rather be paranoid than getting a SEGV. */
24107 end = min (end, row->used[area]);
24108 start = clip_to_bounds (0, start, end);
24109
24110 /* Translate X to frame coordinates. Set last_x to the right
24111 end of the drawing area. */
24112 if (row->full_width_p)
24113 {
24114 /* X is relative to the left edge of W, without scroll bars
24115 or fringes. */
24116 area_left = WINDOW_LEFT_EDGE_X (w);
24117 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24118 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24119 }
24120 else
24121 {
24122 area_left = window_box_left (w, area);
24123 last_x = area_left + window_box_width (w, area);
24124 }
24125 x += area_left;
24126
24127 /* Build a doubly-linked list of glyph_string structures between
24128 head and tail from what we have to draw. Note that the macro
24129 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24130 the reason we use a separate variable `i'. */
24131 i = start;
24132 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24133 if (tail)
24134 x_reached = tail->x + tail->background_width;
24135 else
24136 x_reached = x;
24137
24138 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24139 the row, redraw some glyphs in front or following the glyph
24140 strings built above. */
24141 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24142 {
24143 struct glyph_string *h, *t;
24144 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24145 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24146 int check_mouse_face = 0;
24147 int dummy_x = 0;
24148
24149 /* If mouse highlighting is on, we may need to draw adjacent
24150 glyphs using mouse-face highlighting. */
24151 if (area == TEXT_AREA && row->mouse_face_p
24152 && hlinfo->mouse_face_beg_row >= 0
24153 && hlinfo->mouse_face_end_row >= 0)
24154 {
24155 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24156
24157 if (row_vpos >= hlinfo->mouse_face_beg_row
24158 && row_vpos <= hlinfo->mouse_face_end_row)
24159 {
24160 check_mouse_face = 1;
24161 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24162 ? hlinfo->mouse_face_beg_col : 0;
24163 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24164 ? hlinfo->mouse_face_end_col
24165 : row->used[TEXT_AREA];
24166 }
24167 }
24168
24169 /* Compute overhangs for all glyph strings. */
24170 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24171 for (s = head; s; s = s->next)
24172 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24173
24174 /* Prepend glyph strings for glyphs in front of the first glyph
24175 string that are overwritten because of the first glyph
24176 string's left overhang. The background of all strings
24177 prepended must be drawn because the first glyph string
24178 draws over it. */
24179 i = left_overwritten (head);
24180 if (i >= 0)
24181 {
24182 enum draw_glyphs_face overlap_hl;
24183
24184 /* If this row contains mouse highlighting, attempt to draw
24185 the overlapped glyphs with the correct highlight. This
24186 code fails if the overlap encompasses more than one glyph
24187 and mouse-highlight spans only some of these glyphs.
24188 However, making it work perfectly involves a lot more
24189 code, and I don't know if the pathological case occurs in
24190 practice, so we'll stick to this for now. --- cyd */
24191 if (check_mouse_face
24192 && mouse_beg_col < start && mouse_end_col > i)
24193 overlap_hl = DRAW_MOUSE_FACE;
24194 else
24195 overlap_hl = DRAW_NORMAL_TEXT;
24196
24197 j = i;
24198 BUILD_GLYPH_STRINGS (j, start, h, t,
24199 overlap_hl, dummy_x, last_x);
24200 start = i;
24201 compute_overhangs_and_x (t, head->x, 1);
24202 prepend_glyph_string_lists (&head, &tail, h, t);
24203 clip_head = head;
24204 }
24205
24206 /* Prepend glyph strings for glyphs in front of the first glyph
24207 string that overwrite that glyph string because of their
24208 right overhang. For these strings, only the foreground must
24209 be drawn, because it draws over the glyph string at `head'.
24210 The background must not be drawn because this would overwrite
24211 right overhangs of preceding glyphs for which no glyph
24212 strings exist. */
24213 i = left_overwriting (head);
24214 if (i >= 0)
24215 {
24216 enum draw_glyphs_face overlap_hl;
24217
24218 if (check_mouse_face
24219 && mouse_beg_col < start && mouse_end_col > i)
24220 overlap_hl = DRAW_MOUSE_FACE;
24221 else
24222 overlap_hl = DRAW_NORMAL_TEXT;
24223
24224 clip_head = head;
24225 BUILD_GLYPH_STRINGS (i, start, h, t,
24226 overlap_hl, dummy_x, last_x);
24227 for (s = h; s; s = s->next)
24228 s->background_filled_p = 1;
24229 compute_overhangs_and_x (t, head->x, 1);
24230 prepend_glyph_string_lists (&head, &tail, h, t);
24231 }
24232
24233 /* Append glyphs strings for glyphs following the last glyph
24234 string tail that are overwritten by tail. The background of
24235 these strings has to be drawn because tail's foreground draws
24236 over it. */
24237 i = right_overwritten (tail);
24238 if (i >= 0)
24239 {
24240 enum draw_glyphs_face overlap_hl;
24241
24242 if (check_mouse_face
24243 && mouse_beg_col < i && mouse_end_col > end)
24244 overlap_hl = DRAW_MOUSE_FACE;
24245 else
24246 overlap_hl = DRAW_NORMAL_TEXT;
24247
24248 BUILD_GLYPH_STRINGS (end, i, h, t,
24249 overlap_hl, x, last_x);
24250 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24251 we don't have `end = i;' here. */
24252 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24253 append_glyph_string_lists (&head, &tail, h, t);
24254 clip_tail = tail;
24255 }
24256
24257 /* Append glyph strings for glyphs following the last glyph
24258 string tail that overwrite tail. The foreground of such
24259 glyphs has to be drawn because it writes into the background
24260 of tail. The background must not be drawn because it could
24261 paint over the foreground of following glyphs. */
24262 i = right_overwriting (tail);
24263 if (i >= 0)
24264 {
24265 enum draw_glyphs_face overlap_hl;
24266 if (check_mouse_face
24267 && mouse_beg_col < i && mouse_end_col > end)
24268 overlap_hl = DRAW_MOUSE_FACE;
24269 else
24270 overlap_hl = DRAW_NORMAL_TEXT;
24271
24272 clip_tail = tail;
24273 i++; /* We must include the Ith glyph. */
24274 BUILD_GLYPH_STRINGS (end, i, h, t,
24275 overlap_hl, x, last_x);
24276 for (s = h; s; s = s->next)
24277 s->background_filled_p = 1;
24278 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24279 append_glyph_string_lists (&head, &tail, h, t);
24280 }
24281 if (clip_head || clip_tail)
24282 for (s = head; s; s = s->next)
24283 {
24284 s->clip_head = clip_head;
24285 s->clip_tail = clip_tail;
24286 }
24287 }
24288
24289 /* Draw all strings. */
24290 for (s = head; s; s = s->next)
24291 FRAME_RIF (f)->draw_glyph_string (s);
24292
24293 #ifndef HAVE_NS
24294 /* When focus a sole frame and move horizontally, this sets on_p to 0
24295 causing a failure to erase prev cursor position. */
24296 if (area == TEXT_AREA
24297 && !row->full_width_p
24298 /* When drawing overlapping rows, only the glyph strings'
24299 foreground is drawn, which doesn't erase a cursor
24300 completely. */
24301 && !overlaps)
24302 {
24303 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24304 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24305 : (tail ? tail->x + tail->background_width : x));
24306 x0 -= area_left;
24307 x1 -= area_left;
24308
24309 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24310 row->y, MATRIX_ROW_BOTTOM_Y (row));
24311 }
24312 #endif
24313
24314 /* Value is the x-position up to which drawn, relative to AREA of W.
24315 This doesn't include parts drawn because of overhangs. */
24316 if (row->full_width_p)
24317 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24318 else
24319 x_reached -= area_left;
24320
24321 RELEASE_HDC (hdc, f);
24322
24323 return x_reached;
24324 }
24325
24326 /* Expand row matrix if too narrow. Don't expand if area
24327 is not present. */
24328
24329 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24330 { \
24331 if (!it->f->fonts_changed \
24332 && (it->glyph_row->glyphs[area] \
24333 < it->glyph_row->glyphs[area + 1])) \
24334 { \
24335 it->w->ncols_scale_factor++; \
24336 it->f->fonts_changed = 1; \
24337 } \
24338 }
24339
24340 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24341 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24342
24343 static void
24344 append_glyph (struct it *it)
24345 {
24346 struct glyph *glyph;
24347 enum glyph_row_area area = it->area;
24348
24349 eassert (it->glyph_row);
24350 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24351
24352 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24353 if (glyph < it->glyph_row->glyphs[area + 1])
24354 {
24355 /* If the glyph row is reversed, we need to prepend the glyph
24356 rather than append it. */
24357 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24358 {
24359 struct glyph *g;
24360
24361 /* Make room for the additional glyph. */
24362 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24363 g[1] = *g;
24364 glyph = it->glyph_row->glyphs[area];
24365 }
24366 glyph->charpos = CHARPOS (it->position);
24367 glyph->object = it->object;
24368 if (it->pixel_width > 0)
24369 {
24370 glyph->pixel_width = it->pixel_width;
24371 glyph->padding_p = 0;
24372 }
24373 else
24374 {
24375 /* Assure at least 1-pixel width. Otherwise, cursor can't
24376 be displayed correctly. */
24377 glyph->pixel_width = 1;
24378 glyph->padding_p = 1;
24379 }
24380 glyph->ascent = it->ascent;
24381 glyph->descent = it->descent;
24382 glyph->voffset = it->voffset;
24383 glyph->type = CHAR_GLYPH;
24384 glyph->avoid_cursor_p = it->avoid_cursor_p;
24385 glyph->multibyte_p = it->multibyte_p;
24386 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24387 {
24388 /* In R2L rows, the left and the right box edges need to be
24389 drawn in reverse direction. */
24390 glyph->right_box_line_p = it->start_of_box_run_p;
24391 glyph->left_box_line_p = it->end_of_box_run_p;
24392 }
24393 else
24394 {
24395 glyph->left_box_line_p = it->start_of_box_run_p;
24396 glyph->right_box_line_p = it->end_of_box_run_p;
24397 }
24398 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24399 || it->phys_descent > it->descent);
24400 glyph->glyph_not_available_p = it->glyph_not_available_p;
24401 glyph->face_id = it->face_id;
24402 glyph->u.ch = it->char_to_display;
24403 glyph->slice.img = null_glyph_slice;
24404 glyph->font_type = FONT_TYPE_UNKNOWN;
24405 if (it->bidi_p)
24406 {
24407 glyph->resolved_level = it->bidi_it.resolved_level;
24408 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24409 emacs_abort ();
24410 glyph->bidi_type = it->bidi_it.type;
24411 }
24412 else
24413 {
24414 glyph->resolved_level = 0;
24415 glyph->bidi_type = UNKNOWN_BT;
24416 }
24417 ++it->glyph_row->used[area];
24418 }
24419 else
24420 IT_EXPAND_MATRIX_WIDTH (it, area);
24421 }
24422
24423 /* Store one glyph for the composition IT->cmp_it.id in
24424 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24425 non-null. */
24426
24427 static void
24428 append_composite_glyph (struct it *it)
24429 {
24430 struct glyph *glyph;
24431 enum glyph_row_area area = it->area;
24432
24433 eassert (it->glyph_row);
24434
24435 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24436 if (glyph < it->glyph_row->glyphs[area + 1])
24437 {
24438 /* If the glyph row is reversed, we need to prepend the glyph
24439 rather than append it. */
24440 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24441 {
24442 struct glyph *g;
24443
24444 /* Make room for the new glyph. */
24445 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24446 g[1] = *g;
24447 glyph = it->glyph_row->glyphs[it->area];
24448 }
24449 glyph->charpos = it->cmp_it.charpos;
24450 glyph->object = it->object;
24451 glyph->pixel_width = it->pixel_width;
24452 glyph->ascent = it->ascent;
24453 glyph->descent = it->descent;
24454 glyph->voffset = it->voffset;
24455 glyph->type = COMPOSITE_GLYPH;
24456 if (it->cmp_it.ch < 0)
24457 {
24458 glyph->u.cmp.automatic = 0;
24459 glyph->u.cmp.id = it->cmp_it.id;
24460 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24461 }
24462 else
24463 {
24464 glyph->u.cmp.automatic = 1;
24465 glyph->u.cmp.id = it->cmp_it.id;
24466 glyph->slice.cmp.from = it->cmp_it.from;
24467 glyph->slice.cmp.to = it->cmp_it.to - 1;
24468 }
24469 glyph->avoid_cursor_p = it->avoid_cursor_p;
24470 glyph->multibyte_p = it->multibyte_p;
24471 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24472 {
24473 /* In R2L rows, the left and the right box edges need to be
24474 drawn in reverse direction. */
24475 glyph->right_box_line_p = it->start_of_box_run_p;
24476 glyph->left_box_line_p = it->end_of_box_run_p;
24477 }
24478 else
24479 {
24480 glyph->left_box_line_p = it->start_of_box_run_p;
24481 glyph->right_box_line_p = it->end_of_box_run_p;
24482 }
24483 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24484 || it->phys_descent > it->descent);
24485 glyph->padding_p = 0;
24486 glyph->glyph_not_available_p = 0;
24487 glyph->face_id = it->face_id;
24488 glyph->font_type = FONT_TYPE_UNKNOWN;
24489 if (it->bidi_p)
24490 {
24491 glyph->resolved_level = it->bidi_it.resolved_level;
24492 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24493 emacs_abort ();
24494 glyph->bidi_type = it->bidi_it.type;
24495 }
24496 ++it->glyph_row->used[area];
24497 }
24498 else
24499 IT_EXPAND_MATRIX_WIDTH (it, area);
24500 }
24501
24502
24503 /* Change IT->ascent and IT->height according to the setting of
24504 IT->voffset. */
24505
24506 static void
24507 take_vertical_position_into_account (struct it *it)
24508 {
24509 if (it->voffset)
24510 {
24511 if (it->voffset < 0)
24512 /* Increase the ascent so that we can display the text higher
24513 in the line. */
24514 it->ascent -= it->voffset;
24515 else
24516 /* Increase the descent so that we can display the text lower
24517 in the line. */
24518 it->descent += it->voffset;
24519 }
24520 }
24521
24522
24523 /* Produce glyphs/get display metrics for the image IT is loaded with.
24524 See the description of struct display_iterator in dispextern.h for
24525 an overview of struct display_iterator. */
24526
24527 static void
24528 produce_image_glyph (struct it *it)
24529 {
24530 struct image *img;
24531 struct face *face;
24532 int glyph_ascent, crop;
24533 struct glyph_slice slice;
24534
24535 eassert (it->what == IT_IMAGE);
24536
24537 face = FACE_FROM_ID (it->f, it->face_id);
24538 eassert (face);
24539 /* Make sure X resources of the face is loaded. */
24540 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24541
24542 if (it->image_id < 0)
24543 {
24544 /* Fringe bitmap. */
24545 it->ascent = it->phys_ascent = 0;
24546 it->descent = it->phys_descent = 0;
24547 it->pixel_width = 0;
24548 it->nglyphs = 0;
24549 return;
24550 }
24551
24552 img = IMAGE_FROM_ID (it->f, it->image_id);
24553 eassert (img);
24554 /* Make sure X resources of the image is loaded. */
24555 prepare_image_for_display (it->f, img);
24556
24557 slice.x = slice.y = 0;
24558 slice.width = img->width;
24559 slice.height = img->height;
24560
24561 if (INTEGERP (it->slice.x))
24562 slice.x = XINT (it->slice.x);
24563 else if (FLOATP (it->slice.x))
24564 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24565
24566 if (INTEGERP (it->slice.y))
24567 slice.y = XINT (it->slice.y);
24568 else if (FLOATP (it->slice.y))
24569 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24570
24571 if (INTEGERP (it->slice.width))
24572 slice.width = XINT (it->slice.width);
24573 else if (FLOATP (it->slice.width))
24574 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24575
24576 if (INTEGERP (it->slice.height))
24577 slice.height = XINT (it->slice.height);
24578 else if (FLOATP (it->slice.height))
24579 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24580
24581 if (slice.x >= img->width)
24582 slice.x = img->width;
24583 if (slice.y >= img->height)
24584 slice.y = img->height;
24585 if (slice.x + slice.width >= img->width)
24586 slice.width = img->width - slice.x;
24587 if (slice.y + slice.height > img->height)
24588 slice.height = img->height - slice.y;
24589
24590 if (slice.width == 0 || slice.height == 0)
24591 return;
24592
24593 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24594
24595 it->descent = slice.height - glyph_ascent;
24596 if (slice.y == 0)
24597 it->descent += img->vmargin;
24598 if (slice.y + slice.height == img->height)
24599 it->descent += img->vmargin;
24600 it->phys_descent = it->descent;
24601
24602 it->pixel_width = slice.width;
24603 if (slice.x == 0)
24604 it->pixel_width += img->hmargin;
24605 if (slice.x + slice.width == img->width)
24606 it->pixel_width += img->hmargin;
24607
24608 /* It's quite possible for images to have an ascent greater than
24609 their height, so don't get confused in that case. */
24610 if (it->descent < 0)
24611 it->descent = 0;
24612
24613 it->nglyphs = 1;
24614
24615 if (face->box != FACE_NO_BOX)
24616 {
24617 if (face->box_line_width > 0)
24618 {
24619 if (slice.y == 0)
24620 it->ascent += face->box_line_width;
24621 if (slice.y + slice.height == img->height)
24622 it->descent += face->box_line_width;
24623 }
24624
24625 if (it->start_of_box_run_p && slice.x == 0)
24626 it->pixel_width += eabs (face->box_line_width);
24627 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24628 it->pixel_width += eabs (face->box_line_width);
24629 }
24630
24631 take_vertical_position_into_account (it);
24632
24633 /* Automatically crop wide image glyphs at right edge so we can
24634 draw the cursor on same display row. */
24635 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24636 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24637 {
24638 it->pixel_width -= crop;
24639 slice.width -= crop;
24640 }
24641
24642 if (it->glyph_row)
24643 {
24644 struct glyph *glyph;
24645 enum glyph_row_area area = it->area;
24646
24647 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24648 if (glyph < it->glyph_row->glyphs[area + 1])
24649 {
24650 glyph->charpos = CHARPOS (it->position);
24651 glyph->object = it->object;
24652 glyph->pixel_width = it->pixel_width;
24653 glyph->ascent = glyph_ascent;
24654 glyph->descent = it->descent;
24655 glyph->voffset = it->voffset;
24656 glyph->type = IMAGE_GLYPH;
24657 glyph->avoid_cursor_p = it->avoid_cursor_p;
24658 glyph->multibyte_p = it->multibyte_p;
24659 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24660 {
24661 /* In R2L rows, the left and the right box edges need to be
24662 drawn in reverse direction. */
24663 glyph->right_box_line_p = it->start_of_box_run_p;
24664 glyph->left_box_line_p = it->end_of_box_run_p;
24665 }
24666 else
24667 {
24668 glyph->left_box_line_p = it->start_of_box_run_p;
24669 glyph->right_box_line_p = it->end_of_box_run_p;
24670 }
24671 glyph->overlaps_vertically_p = 0;
24672 glyph->padding_p = 0;
24673 glyph->glyph_not_available_p = 0;
24674 glyph->face_id = it->face_id;
24675 glyph->u.img_id = img->id;
24676 glyph->slice.img = slice;
24677 glyph->font_type = FONT_TYPE_UNKNOWN;
24678 if (it->bidi_p)
24679 {
24680 glyph->resolved_level = it->bidi_it.resolved_level;
24681 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24682 emacs_abort ();
24683 glyph->bidi_type = it->bidi_it.type;
24684 }
24685 ++it->glyph_row->used[area];
24686 }
24687 else
24688 IT_EXPAND_MATRIX_WIDTH (it, area);
24689 }
24690 }
24691
24692
24693 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24694 of the glyph, WIDTH and HEIGHT are the width and height of the
24695 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24696
24697 static void
24698 append_stretch_glyph (struct it *it, Lisp_Object object,
24699 int width, int height, int ascent)
24700 {
24701 struct glyph *glyph;
24702 enum glyph_row_area area = it->area;
24703
24704 eassert (ascent >= 0 && ascent <= height);
24705
24706 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24707 if (glyph < it->glyph_row->glyphs[area + 1])
24708 {
24709 /* If the glyph row is reversed, we need to prepend the glyph
24710 rather than append it. */
24711 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24712 {
24713 struct glyph *g;
24714
24715 /* Make room for the additional glyph. */
24716 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24717 g[1] = *g;
24718 glyph = it->glyph_row->glyphs[area];
24719 }
24720 glyph->charpos = CHARPOS (it->position);
24721 glyph->object = object;
24722 glyph->pixel_width = width;
24723 glyph->ascent = ascent;
24724 glyph->descent = height - ascent;
24725 glyph->voffset = it->voffset;
24726 glyph->type = STRETCH_GLYPH;
24727 glyph->avoid_cursor_p = it->avoid_cursor_p;
24728 glyph->multibyte_p = it->multibyte_p;
24729 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24730 {
24731 /* In R2L rows, the left and the right box edges need to be
24732 drawn in reverse direction. */
24733 glyph->right_box_line_p = it->start_of_box_run_p;
24734 glyph->left_box_line_p = it->end_of_box_run_p;
24735 }
24736 else
24737 {
24738 glyph->left_box_line_p = it->start_of_box_run_p;
24739 glyph->right_box_line_p = it->end_of_box_run_p;
24740 }
24741 glyph->overlaps_vertically_p = 0;
24742 glyph->padding_p = 0;
24743 glyph->glyph_not_available_p = 0;
24744 glyph->face_id = it->face_id;
24745 glyph->u.stretch.ascent = ascent;
24746 glyph->u.stretch.height = height;
24747 glyph->slice.img = null_glyph_slice;
24748 glyph->font_type = FONT_TYPE_UNKNOWN;
24749 if (it->bidi_p)
24750 {
24751 glyph->resolved_level = it->bidi_it.resolved_level;
24752 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24753 emacs_abort ();
24754 glyph->bidi_type = it->bidi_it.type;
24755 }
24756 else
24757 {
24758 glyph->resolved_level = 0;
24759 glyph->bidi_type = UNKNOWN_BT;
24760 }
24761 ++it->glyph_row->used[area];
24762 }
24763 else
24764 IT_EXPAND_MATRIX_WIDTH (it, area);
24765 }
24766
24767 #endif /* HAVE_WINDOW_SYSTEM */
24768
24769 /* Produce a stretch glyph for iterator IT. IT->object is the value
24770 of the glyph property displayed. The value must be a list
24771 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24772 being recognized:
24773
24774 1. `:width WIDTH' specifies that the space should be WIDTH *
24775 canonical char width wide. WIDTH may be an integer or floating
24776 point number.
24777
24778 2. `:relative-width FACTOR' specifies that the width of the stretch
24779 should be computed from the width of the first character having the
24780 `glyph' property, and should be FACTOR times that width.
24781
24782 3. `:align-to HPOS' specifies that the space should be wide enough
24783 to reach HPOS, a value in canonical character units.
24784
24785 Exactly one of the above pairs must be present.
24786
24787 4. `:height HEIGHT' specifies that the height of the stretch produced
24788 should be HEIGHT, measured in canonical character units.
24789
24790 5. `:relative-height FACTOR' specifies that the height of the
24791 stretch should be FACTOR times the height of the characters having
24792 the glyph property.
24793
24794 Either none or exactly one of 4 or 5 must be present.
24795
24796 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24797 of the stretch should be used for the ascent of the stretch.
24798 ASCENT must be in the range 0 <= ASCENT <= 100. */
24799
24800 void
24801 produce_stretch_glyph (struct it *it)
24802 {
24803 /* (space :width WIDTH :height HEIGHT ...) */
24804 Lisp_Object prop, plist;
24805 int width = 0, height = 0, align_to = -1;
24806 int zero_width_ok_p = 0;
24807 double tem;
24808 struct font *font = NULL;
24809
24810 #ifdef HAVE_WINDOW_SYSTEM
24811 int ascent = 0;
24812 int zero_height_ok_p = 0;
24813
24814 if (FRAME_WINDOW_P (it->f))
24815 {
24816 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24817 font = face->font ? face->font : FRAME_FONT (it->f);
24818 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24819 }
24820 #endif
24821
24822 /* List should start with `space'. */
24823 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24824 plist = XCDR (it->object);
24825
24826 /* Compute the width of the stretch. */
24827 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24828 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24829 {
24830 /* Absolute width `:width WIDTH' specified and valid. */
24831 zero_width_ok_p = 1;
24832 width = (int)tem;
24833 }
24834 #ifdef HAVE_WINDOW_SYSTEM
24835 else if (FRAME_WINDOW_P (it->f)
24836 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24837 {
24838 /* Relative width `:relative-width FACTOR' specified and valid.
24839 Compute the width of the characters having the `glyph'
24840 property. */
24841 struct it it2;
24842 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24843
24844 it2 = *it;
24845 if (it->multibyte_p)
24846 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24847 else
24848 {
24849 it2.c = it2.char_to_display = *p, it2.len = 1;
24850 if (! ASCII_CHAR_P (it2.c))
24851 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24852 }
24853
24854 it2.glyph_row = NULL;
24855 it2.what = IT_CHARACTER;
24856 x_produce_glyphs (&it2);
24857 width = NUMVAL (prop) * it2.pixel_width;
24858 }
24859 #endif /* HAVE_WINDOW_SYSTEM */
24860 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24861 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24862 {
24863 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24864 align_to = (align_to < 0
24865 ? 0
24866 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24867 else if (align_to < 0)
24868 align_to = window_box_left_offset (it->w, TEXT_AREA);
24869 width = max (0, (int)tem + align_to - it->current_x);
24870 zero_width_ok_p = 1;
24871 }
24872 else
24873 /* Nothing specified -> width defaults to canonical char width. */
24874 width = FRAME_COLUMN_WIDTH (it->f);
24875
24876 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24877 width = 1;
24878
24879 #ifdef HAVE_WINDOW_SYSTEM
24880 /* Compute height. */
24881 if (FRAME_WINDOW_P (it->f))
24882 {
24883 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24884 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24885 {
24886 height = (int)tem;
24887 zero_height_ok_p = 1;
24888 }
24889 else if (prop = Fplist_get (plist, QCrelative_height),
24890 NUMVAL (prop) > 0)
24891 height = FONT_HEIGHT (font) * NUMVAL (prop);
24892 else
24893 height = FONT_HEIGHT (font);
24894
24895 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24896 height = 1;
24897
24898 /* Compute percentage of height used for ascent. If
24899 `:ascent ASCENT' is present and valid, use that. Otherwise,
24900 derive the ascent from the font in use. */
24901 if (prop = Fplist_get (plist, QCascent),
24902 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24903 ascent = height * NUMVAL (prop) / 100.0;
24904 else if (!NILP (prop)
24905 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24906 ascent = min (max (0, (int)tem), height);
24907 else
24908 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24909 }
24910 else
24911 #endif /* HAVE_WINDOW_SYSTEM */
24912 height = 1;
24913
24914 if (width > 0 && it->line_wrap != TRUNCATE
24915 && it->current_x + width > it->last_visible_x)
24916 {
24917 width = it->last_visible_x - it->current_x;
24918 #ifdef HAVE_WINDOW_SYSTEM
24919 /* Subtract one more pixel from the stretch width, but only on
24920 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24921 width -= FRAME_WINDOW_P (it->f);
24922 #endif
24923 }
24924
24925 if (width > 0 && height > 0 && it->glyph_row)
24926 {
24927 Lisp_Object o_object = it->object;
24928 Lisp_Object object = it->stack[it->sp - 1].string;
24929 int n = width;
24930
24931 if (!STRINGP (object))
24932 object = it->w->contents;
24933 #ifdef HAVE_WINDOW_SYSTEM
24934 if (FRAME_WINDOW_P (it->f))
24935 append_stretch_glyph (it, object, width, height, ascent);
24936 else
24937 #endif
24938 {
24939 it->object = object;
24940 it->char_to_display = ' ';
24941 it->pixel_width = it->len = 1;
24942 while (n--)
24943 tty_append_glyph (it);
24944 it->object = o_object;
24945 }
24946 }
24947
24948 it->pixel_width = width;
24949 #ifdef HAVE_WINDOW_SYSTEM
24950 if (FRAME_WINDOW_P (it->f))
24951 {
24952 it->ascent = it->phys_ascent = ascent;
24953 it->descent = it->phys_descent = height - it->ascent;
24954 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24955 take_vertical_position_into_account (it);
24956 }
24957 else
24958 #endif
24959 it->nglyphs = width;
24960 }
24961
24962 /* Get information about special display element WHAT in an
24963 environment described by IT. WHAT is one of IT_TRUNCATION or
24964 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24965 non-null glyph_row member. This function ensures that fields like
24966 face_id, c, len of IT are left untouched. */
24967
24968 static void
24969 produce_special_glyphs (struct it *it, enum display_element_type what)
24970 {
24971 struct it temp_it;
24972 Lisp_Object gc;
24973 GLYPH glyph;
24974
24975 temp_it = *it;
24976 temp_it.object = make_number (0);
24977 memset (&temp_it.current, 0, sizeof temp_it.current);
24978
24979 if (what == IT_CONTINUATION)
24980 {
24981 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24982 if (it->bidi_it.paragraph_dir == R2L)
24983 SET_GLYPH_FROM_CHAR (glyph, '/');
24984 else
24985 SET_GLYPH_FROM_CHAR (glyph, '\\');
24986 if (it->dp
24987 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24988 {
24989 /* FIXME: Should we mirror GC for R2L lines? */
24990 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24991 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24992 }
24993 }
24994 else if (what == IT_TRUNCATION)
24995 {
24996 /* Truncation glyph. */
24997 SET_GLYPH_FROM_CHAR (glyph, '$');
24998 if (it->dp
24999 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25000 {
25001 /* FIXME: Should we mirror GC for R2L lines? */
25002 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25003 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25004 }
25005 }
25006 else
25007 emacs_abort ();
25008
25009 #ifdef HAVE_WINDOW_SYSTEM
25010 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25011 is turned off, we precede the truncation/continuation glyphs by a
25012 stretch glyph whose width is computed such that these special
25013 glyphs are aligned at the window margin, even when very different
25014 fonts are used in different glyph rows. */
25015 if (FRAME_WINDOW_P (temp_it.f)
25016 /* init_iterator calls this with it->glyph_row == NULL, and it
25017 wants only the pixel width of the truncation/continuation
25018 glyphs. */
25019 && temp_it.glyph_row
25020 /* insert_left_trunc_glyphs calls us at the beginning of the
25021 row, and it has its own calculation of the stretch glyph
25022 width. */
25023 && temp_it.glyph_row->used[TEXT_AREA] > 0
25024 && (temp_it.glyph_row->reversed_p
25025 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25026 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25027 {
25028 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25029
25030 if (stretch_width > 0)
25031 {
25032 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25033 struct font *font =
25034 face->font ? face->font : FRAME_FONT (temp_it.f);
25035 int stretch_ascent =
25036 (((temp_it.ascent + temp_it.descent)
25037 * FONT_BASE (font)) / FONT_HEIGHT (font));
25038
25039 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25040 temp_it.ascent + temp_it.descent,
25041 stretch_ascent);
25042 }
25043 }
25044 #endif
25045
25046 temp_it.dp = NULL;
25047 temp_it.what = IT_CHARACTER;
25048 temp_it.len = 1;
25049 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25050 temp_it.face_id = GLYPH_FACE (glyph);
25051 temp_it.len = CHAR_BYTES (temp_it.c);
25052
25053 PRODUCE_GLYPHS (&temp_it);
25054 it->pixel_width = temp_it.pixel_width;
25055 it->nglyphs = temp_it.pixel_width;
25056 }
25057
25058 #ifdef HAVE_WINDOW_SYSTEM
25059
25060 /* Calculate line-height and line-spacing properties.
25061 An integer value specifies explicit pixel value.
25062 A float value specifies relative value to current face height.
25063 A cons (float . face-name) specifies relative value to
25064 height of specified face font.
25065
25066 Returns height in pixels, or nil. */
25067
25068
25069 static Lisp_Object
25070 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25071 int boff, int override)
25072 {
25073 Lisp_Object face_name = Qnil;
25074 int ascent, descent, height;
25075
25076 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25077 return val;
25078
25079 if (CONSP (val))
25080 {
25081 face_name = XCAR (val);
25082 val = XCDR (val);
25083 if (!NUMBERP (val))
25084 val = make_number (1);
25085 if (NILP (face_name))
25086 {
25087 height = it->ascent + it->descent;
25088 goto scale;
25089 }
25090 }
25091
25092 if (NILP (face_name))
25093 {
25094 font = FRAME_FONT (it->f);
25095 boff = FRAME_BASELINE_OFFSET (it->f);
25096 }
25097 else if (EQ (face_name, Qt))
25098 {
25099 override = 0;
25100 }
25101 else
25102 {
25103 int face_id;
25104 struct face *face;
25105
25106 face_id = lookup_named_face (it->f, face_name, 0);
25107 if (face_id < 0)
25108 return make_number (-1);
25109
25110 face = FACE_FROM_ID (it->f, face_id);
25111 font = face->font;
25112 if (font == NULL)
25113 return make_number (-1);
25114 boff = font->baseline_offset;
25115 if (font->vertical_centering)
25116 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25117 }
25118
25119 ascent = FONT_BASE (font) + boff;
25120 descent = FONT_DESCENT (font) - boff;
25121
25122 if (override)
25123 {
25124 it->override_ascent = ascent;
25125 it->override_descent = descent;
25126 it->override_boff = boff;
25127 }
25128
25129 height = ascent + descent;
25130
25131 scale:
25132 if (FLOATP (val))
25133 height = (int)(XFLOAT_DATA (val) * height);
25134 else if (INTEGERP (val))
25135 height *= XINT (val);
25136
25137 return make_number (height);
25138 }
25139
25140
25141 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25142 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25143 and only if this is for a character for which no font was found.
25144
25145 If the display method (it->glyphless_method) is
25146 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25147 length of the acronym or the hexadecimal string, UPPER_XOFF and
25148 UPPER_YOFF are pixel offsets for the upper part of the string,
25149 LOWER_XOFF and LOWER_YOFF are for the lower part.
25150
25151 For the other display methods, LEN through LOWER_YOFF are zero. */
25152
25153 static void
25154 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25155 short upper_xoff, short upper_yoff,
25156 short lower_xoff, short lower_yoff)
25157 {
25158 struct glyph *glyph;
25159 enum glyph_row_area area = it->area;
25160
25161 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25162 if (glyph < it->glyph_row->glyphs[area + 1])
25163 {
25164 /* If the glyph row is reversed, we need to prepend the glyph
25165 rather than append it. */
25166 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25167 {
25168 struct glyph *g;
25169
25170 /* Make room for the additional glyph. */
25171 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25172 g[1] = *g;
25173 glyph = it->glyph_row->glyphs[area];
25174 }
25175 glyph->charpos = CHARPOS (it->position);
25176 glyph->object = it->object;
25177 glyph->pixel_width = it->pixel_width;
25178 glyph->ascent = it->ascent;
25179 glyph->descent = it->descent;
25180 glyph->voffset = it->voffset;
25181 glyph->type = GLYPHLESS_GLYPH;
25182 glyph->u.glyphless.method = it->glyphless_method;
25183 glyph->u.glyphless.for_no_font = for_no_font;
25184 glyph->u.glyphless.len = len;
25185 glyph->u.glyphless.ch = it->c;
25186 glyph->slice.glyphless.upper_xoff = upper_xoff;
25187 glyph->slice.glyphless.upper_yoff = upper_yoff;
25188 glyph->slice.glyphless.lower_xoff = lower_xoff;
25189 glyph->slice.glyphless.lower_yoff = lower_yoff;
25190 glyph->avoid_cursor_p = it->avoid_cursor_p;
25191 glyph->multibyte_p = it->multibyte_p;
25192 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25193 {
25194 /* In R2L rows, the left and the right box edges need to be
25195 drawn in reverse direction. */
25196 glyph->right_box_line_p = it->start_of_box_run_p;
25197 glyph->left_box_line_p = it->end_of_box_run_p;
25198 }
25199 else
25200 {
25201 glyph->left_box_line_p = it->start_of_box_run_p;
25202 glyph->right_box_line_p = it->end_of_box_run_p;
25203 }
25204 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25205 || it->phys_descent > it->descent);
25206 glyph->padding_p = 0;
25207 glyph->glyph_not_available_p = 0;
25208 glyph->face_id = face_id;
25209 glyph->font_type = FONT_TYPE_UNKNOWN;
25210 if (it->bidi_p)
25211 {
25212 glyph->resolved_level = it->bidi_it.resolved_level;
25213 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25214 emacs_abort ();
25215 glyph->bidi_type = it->bidi_it.type;
25216 }
25217 ++it->glyph_row->used[area];
25218 }
25219 else
25220 IT_EXPAND_MATRIX_WIDTH (it, area);
25221 }
25222
25223
25224 /* Produce a glyph for a glyphless character for iterator IT.
25225 IT->glyphless_method specifies which method to use for displaying
25226 the character. See the description of enum
25227 glyphless_display_method in dispextern.h for the detail.
25228
25229 FOR_NO_FONT is nonzero if and only if this is for a character for
25230 which no font was found. ACRONYM, if non-nil, is an acronym string
25231 for the character. */
25232
25233 static void
25234 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25235 {
25236 int face_id;
25237 struct face *face;
25238 struct font *font;
25239 int base_width, base_height, width, height;
25240 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25241 int len;
25242
25243 /* Get the metrics of the base font. We always refer to the current
25244 ASCII face. */
25245 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25246 font = face->font ? face->font : FRAME_FONT (it->f);
25247 it->ascent = FONT_BASE (font) + font->baseline_offset;
25248 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25249 base_height = it->ascent + it->descent;
25250 base_width = font->average_width;
25251
25252 face_id = merge_glyphless_glyph_face (it);
25253
25254 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25255 {
25256 it->pixel_width = THIN_SPACE_WIDTH;
25257 len = 0;
25258 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25259 }
25260 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25261 {
25262 width = CHAR_WIDTH (it->c);
25263 if (width == 0)
25264 width = 1;
25265 else if (width > 4)
25266 width = 4;
25267 it->pixel_width = base_width * width;
25268 len = 0;
25269 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25270 }
25271 else
25272 {
25273 char buf[7];
25274 const char *str;
25275 unsigned int code[6];
25276 int upper_len;
25277 int ascent, descent;
25278 struct font_metrics metrics_upper, metrics_lower;
25279
25280 face = FACE_FROM_ID (it->f, face_id);
25281 font = face->font ? face->font : FRAME_FONT (it->f);
25282 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25283
25284 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25285 {
25286 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25287 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25288 if (CONSP (acronym))
25289 acronym = XCAR (acronym);
25290 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25291 }
25292 else
25293 {
25294 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25295 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25296 str = buf;
25297 }
25298 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25299 code[len] = font->driver->encode_char (font, str[len]);
25300 upper_len = (len + 1) / 2;
25301 font->driver->text_extents (font, code, upper_len,
25302 &metrics_upper);
25303 font->driver->text_extents (font, code + upper_len, len - upper_len,
25304 &metrics_lower);
25305
25306
25307
25308 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25309 width = max (metrics_upper.width, metrics_lower.width) + 4;
25310 upper_xoff = upper_yoff = 2; /* the typical case */
25311 if (base_width >= width)
25312 {
25313 /* Align the upper to the left, the lower to the right. */
25314 it->pixel_width = base_width;
25315 lower_xoff = base_width - 2 - metrics_lower.width;
25316 }
25317 else
25318 {
25319 /* Center the shorter one. */
25320 it->pixel_width = width;
25321 if (metrics_upper.width >= metrics_lower.width)
25322 lower_xoff = (width - metrics_lower.width) / 2;
25323 else
25324 {
25325 /* FIXME: This code doesn't look right. It formerly was
25326 missing the "lower_xoff = 0;", which couldn't have
25327 been right since it left lower_xoff uninitialized. */
25328 lower_xoff = 0;
25329 upper_xoff = (width - metrics_upper.width) / 2;
25330 }
25331 }
25332
25333 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25334 top, bottom, and between upper and lower strings. */
25335 height = (metrics_upper.ascent + metrics_upper.descent
25336 + metrics_lower.ascent + metrics_lower.descent) + 5;
25337 /* Center vertically.
25338 H:base_height, D:base_descent
25339 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25340
25341 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25342 descent = D - H/2 + h/2;
25343 lower_yoff = descent - 2 - ld;
25344 upper_yoff = lower_yoff - la - 1 - ud; */
25345 ascent = - (it->descent - (base_height + height + 1) / 2);
25346 descent = it->descent - (base_height - height) / 2;
25347 lower_yoff = descent - 2 - metrics_lower.descent;
25348 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25349 - metrics_upper.descent);
25350 /* Don't make the height shorter than the base height. */
25351 if (height > base_height)
25352 {
25353 it->ascent = ascent;
25354 it->descent = descent;
25355 }
25356 }
25357
25358 it->phys_ascent = it->ascent;
25359 it->phys_descent = it->descent;
25360 if (it->glyph_row)
25361 append_glyphless_glyph (it, face_id, for_no_font, len,
25362 upper_xoff, upper_yoff,
25363 lower_xoff, lower_yoff);
25364 it->nglyphs = 1;
25365 take_vertical_position_into_account (it);
25366 }
25367
25368
25369 /* RIF:
25370 Produce glyphs/get display metrics for the display element IT is
25371 loaded with. See the description of struct it in dispextern.h
25372 for an overview of struct it. */
25373
25374 void
25375 x_produce_glyphs (struct it *it)
25376 {
25377 int extra_line_spacing = it->extra_line_spacing;
25378
25379 it->glyph_not_available_p = 0;
25380
25381 if (it->what == IT_CHARACTER)
25382 {
25383 XChar2b char2b;
25384 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25385 struct font *font = face->font;
25386 struct font_metrics *pcm = NULL;
25387 int boff; /* Baseline offset. */
25388
25389 if (font == NULL)
25390 {
25391 /* When no suitable font is found, display this character by
25392 the method specified in the first extra slot of
25393 Vglyphless_char_display. */
25394 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25395
25396 eassert (it->what == IT_GLYPHLESS);
25397 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25398 goto done;
25399 }
25400
25401 boff = font->baseline_offset;
25402 if (font->vertical_centering)
25403 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25404
25405 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25406 {
25407 int stretched_p;
25408
25409 it->nglyphs = 1;
25410
25411 if (it->override_ascent >= 0)
25412 {
25413 it->ascent = it->override_ascent;
25414 it->descent = it->override_descent;
25415 boff = it->override_boff;
25416 }
25417 else
25418 {
25419 it->ascent = FONT_BASE (font) + boff;
25420 it->descent = FONT_DESCENT (font) - boff;
25421 }
25422
25423 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25424 {
25425 pcm = get_per_char_metric (font, &char2b);
25426 if (pcm->width == 0
25427 && pcm->rbearing == 0 && pcm->lbearing == 0)
25428 pcm = NULL;
25429 }
25430
25431 if (pcm)
25432 {
25433 it->phys_ascent = pcm->ascent + boff;
25434 it->phys_descent = pcm->descent - boff;
25435 it->pixel_width = pcm->width;
25436 }
25437 else
25438 {
25439 it->glyph_not_available_p = 1;
25440 it->phys_ascent = it->ascent;
25441 it->phys_descent = it->descent;
25442 it->pixel_width = font->space_width;
25443 }
25444
25445 if (it->constrain_row_ascent_descent_p)
25446 {
25447 if (it->descent > it->max_descent)
25448 {
25449 it->ascent += it->descent - it->max_descent;
25450 it->descent = it->max_descent;
25451 }
25452 if (it->ascent > it->max_ascent)
25453 {
25454 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25455 it->ascent = it->max_ascent;
25456 }
25457 it->phys_ascent = min (it->phys_ascent, it->ascent);
25458 it->phys_descent = min (it->phys_descent, it->descent);
25459 extra_line_spacing = 0;
25460 }
25461
25462 /* If this is a space inside a region of text with
25463 `space-width' property, change its width. */
25464 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25465 if (stretched_p)
25466 it->pixel_width *= XFLOATINT (it->space_width);
25467
25468 /* If face has a box, add the box thickness to the character
25469 height. If character has a box line to the left and/or
25470 right, add the box line width to the character's width. */
25471 if (face->box != FACE_NO_BOX)
25472 {
25473 int thick = face->box_line_width;
25474
25475 if (thick > 0)
25476 {
25477 it->ascent += thick;
25478 it->descent += thick;
25479 }
25480 else
25481 thick = -thick;
25482
25483 if (it->start_of_box_run_p)
25484 it->pixel_width += thick;
25485 if (it->end_of_box_run_p)
25486 it->pixel_width += thick;
25487 }
25488
25489 /* If face has an overline, add the height of the overline
25490 (1 pixel) and a 1 pixel margin to the character height. */
25491 if (face->overline_p)
25492 it->ascent += overline_margin;
25493
25494 if (it->constrain_row_ascent_descent_p)
25495 {
25496 if (it->ascent > it->max_ascent)
25497 it->ascent = it->max_ascent;
25498 if (it->descent > it->max_descent)
25499 it->descent = it->max_descent;
25500 }
25501
25502 take_vertical_position_into_account (it);
25503
25504 /* If we have to actually produce glyphs, do it. */
25505 if (it->glyph_row)
25506 {
25507 if (stretched_p)
25508 {
25509 /* Translate a space with a `space-width' property
25510 into a stretch glyph. */
25511 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25512 / FONT_HEIGHT (font));
25513 append_stretch_glyph (it, it->object, it->pixel_width,
25514 it->ascent + it->descent, ascent);
25515 }
25516 else
25517 append_glyph (it);
25518
25519 /* If characters with lbearing or rbearing are displayed
25520 in this line, record that fact in a flag of the
25521 glyph row. This is used to optimize X output code. */
25522 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25523 it->glyph_row->contains_overlapping_glyphs_p = 1;
25524 }
25525 if (! stretched_p && it->pixel_width == 0)
25526 /* We assure that all visible glyphs have at least 1-pixel
25527 width. */
25528 it->pixel_width = 1;
25529 }
25530 else if (it->char_to_display == '\n')
25531 {
25532 /* A newline has no width, but we need the height of the
25533 line. But if previous part of the line sets a height,
25534 don't increase that height. */
25535
25536 Lisp_Object height;
25537 Lisp_Object total_height = Qnil;
25538
25539 it->override_ascent = -1;
25540 it->pixel_width = 0;
25541 it->nglyphs = 0;
25542
25543 height = get_it_property (it, Qline_height);
25544 /* Split (line-height total-height) list. */
25545 if (CONSP (height)
25546 && CONSP (XCDR (height))
25547 && NILP (XCDR (XCDR (height))))
25548 {
25549 total_height = XCAR (XCDR (height));
25550 height = XCAR (height);
25551 }
25552 height = calc_line_height_property (it, height, font, boff, 1);
25553
25554 if (it->override_ascent >= 0)
25555 {
25556 it->ascent = it->override_ascent;
25557 it->descent = it->override_descent;
25558 boff = it->override_boff;
25559 }
25560 else
25561 {
25562 it->ascent = FONT_BASE (font) + boff;
25563 it->descent = FONT_DESCENT (font) - boff;
25564 }
25565
25566 if (EQ (height, Qt))
25567 {
25568 if (it->descent > it->max_descent)
25569 {
25570 it->ascent += it->descent - it->max_descent;
25571 it->descent = it->max_descent;
25572 }
25573 if (it->ascent > it->max_ascent)
25574 {
25575 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25576 it->ascent = it->max_ascent;
25577 }
25578 it->phys_ascent = min (it->phys_ascent, it->ascent);
25579 it->phys_descent = min (it->phys_descent, it->descent);
25580 it->constrain_row_ascent_descent_p = 1;
25581 extra_line_spacing = 0;
25582 }
25583 else
25584 {
25585 Lisp_Object spacing;
25586
25587 it->phys_ascent = it->ascent;
25588 it->phys_descent = it->descent;
25589
25590 if ((it->max_ascent > 0 || it->max_descent > 0)
25591 && face->box != FACE_NO_BOX
25592 && face->box_line_width > 0)
25593 {
25594 it->ascent += face->box_line_width;
25595 it->descent += face->box_line_width;
25596 }
25597 if (!NILP (height)
25598 && XINT (height) > it->ascent + it->descent)
25599 it->ascent = XINT (height) - it->descent;
25600
25601 if (!NILP (total_height))
25602 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25603 else
25604 {
25605 spacing = get_it_property (it, Qline_spacing);
25606 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25607 }
25608 if (INTEGERP (spacing))
25609 {
25610 extra_line_spacing = XINT (spacing);
25611 if (!NILP (total_height))
25612 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25613 }
25614 }
25615 }
25616 else /* i.e. (it->char_to_display == '\t') */
25617 {
25618 if (font->space_width > 0)
25619 {
25620 int tab_width = it->tab_width * font->space_width;
25621 int x = it->current_x + it->continuation_lines_width;
25622 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25623
25624 /* If the distance from the current position to the next tab
25625 stop is less than a space character width, use the
25626 tab stop after that. */
25627 if (next_tab_x - x < font->space_width)
25628 next_tab_x += tab_width;
25629
25630 it->pixel_width = next_tab_x - x;
25631 it->nglyphs = 1;
25632 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25633 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25634
25635 if (it->glyph_row)
25636 {
25637 append_stretch_glyph (it, it->object, it->pixel_width,
25638 it->ascent + it->descent, it->ascent);
25639 }
25640 }
25641 else
25642 {
25643 it->pixel_width = 0;
25644 it->nglyphs = 1;
25645 }
25646 }
25647 }
25648 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25649 {
25650 /* A static composition.
25651
25652 Note: A composition is represented as one glyph in the
25653 glyph matrix. There are no padding glyphs.
25654
25655 Important note: pixel_width, ascent, and descent are the
25656 values of what is drawn by draw_glyphs (i.e. the values of
25657 the overall glyphs composed). */
25658 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25659 int boff; /* baseline offset */
25660 struct composition *cmp = composition_table[it->cmp_it.id];
25661 int glyph_len = cmp->glyph_len;
25662 struct font *font = face->font;
25663
25664 it->nglyphs = 1;
25665
25666 /* If we have not yet calculated pixel size data of glyphs of
25667 the composition for the current face font, calculate them
25668 now. Theoretically, we have to check all fonts for the
25669 glyphs, but that requires much time and memory space. So,
25670 here we check only the font of the first glyph. This may
25671 lead to incorrect display, but it's very rare, and C-l
25672 (recenter-top-bottom) can correct the display anyway. */
25673 if (! cmp->font || cmp->font != font)
25674 {
25675 /* Ascent and descent of the font of the first character
25676 of this composition (adjusted by baseline offset).
25677 Ascent and descent of overall glyphs should not be less
25678 than these, respectively. */
25679 int font_ascent, font_descent, font_height;
25680 /* Bounding box of the overall glyphs. */
25681 int leftmost, rightmost, lowest, highest;
25682 int lbearing, rbearing;
25683 int i, width, ascent, descent;
25684 int left_padded = 0, right_padded = 0;
25685 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25686 XChar2b char2b;
25687 struct font_metrics *pcm;
25688 int font_not_found_p;
25689 ptrdiff_t pos;
25690
25691 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25692 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25693 break;
25694 if (glyph_len < cmp->glyph_len)
25695 right_padded = 1;
25696 for (i = 0; i < glyph_len; i++)
25697 {
25698 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25699 break;
25700 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25701 }
25702 if (i > 0)
25703 left_padded = 1;
25704
25705 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25706 : IT_CHARPOS (*it));
25707 /* If no suitable font is found, use the default font. */
25708 font_not_found_p = font == NULL;
25709 if (font_not_found_p)
25710 {
25711 face = face->ascii_face;
25712 font = face->font;
25713 }
25714 boff = font->baseline_offset;
25715 if (font->vertical_centering)
25716 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25717 font_ascent = FONT_BASE (font) + boff;
25718 font_descent = FONT_DESCENT (font) - boff;
25719 font_height = FONT_HEIGHT (font);
25720
25721 cmp->font = font;
25722
25723 pcm = NULL;
25724 if (! font_not_found_p)
25725 {
25726 get_char_face_and_encoding (it->f, c, it->face_id,
25727 &char2b, 0);
25728 pcm = get_per_char_metric (font, &char2b);
25729 }
25730
25731 /* Initialize the bounding box. */
25732 if (pcm)
25733 {
25734 width = cmp->glyph_len > 0 ? pcm->width : 0;
25735 ascent = pcm->ascent;
25736 descent = pcm->descent;
25737 lbearing = pcm->lbearing;
25738 rbearing = pcm->rbearing;
25739 }
25740 else
25741 {
25742 width = cmp->glyph_len > 0 ? font->space_width : 0;
25743 ascent = FONT_BASE (font);
25744 descent = FONT_DESCENT (font);
25745 lbearing = 0;
25746 rbearing = width;
25747 }
25748
25749 rightmost = width;
25750 leftmost = 0;
25751 lowest = - descent + boff;
25752 highest = ascent + boff;
25753
25754 if (! font_not_found_p
25755 && font->default_ascent
25756 && CHAR_TABLE_P (Vuse_default_ascent)
25757 && !NILP (Faref (Vuse_default_ascent,
25758 make_number (it->char_to_display))))
25759 highest = font->default_ascent + boff;
25760
25761 /* Draw the first glyph at the normal position. It may be
25762 shifted to right later if some other glyphs are drawn
25763 at the left. */
25764 cmp->offsets[i * 2] = 0;
25765 cmp->offsets[i * 2 + 1] = boff;
25766 cmp->lbearing = lbearing;
25767 cmp->rbearing = rbearing;
25768
25769 /* Set cmp->offsets for the remaining glyphs. */
25770 for (i++; i < glyph_len; i++)
25771 {
25772 int left, right, btm, top;
25773 int ch = COMPOSITION_GLYPH (cmp, i);
25774 int face_id;
25775 struct face *this_face;
25776
25777 if (ch == '\t')
25778 ch = ' ';
25779 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25780 this_face = FACE_FROM_ID (it->f, face_id);
25781 font = this_face->font;
25782
25783 if (font == NULL)
25784 pcm = NULL;
25785 else
25786 {
25787 get_char_face_and_encoding (it->f, ch, face_id,
25788 &char2b, 0);
25789 pcm = get_per_char_metric (font, &char2b);
25790 }
25791 if (! pcm)
25792 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25793 else
25794 {
25795 width = pcm->width;
25796 ascent = pcm->ascent;
25797 descent = pcm->descent;
25798 lbearing = pcm->lbearing;
25799 rbearing = pcm->rbearing;
25800 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25801 {
25802 /* Relative composition with or without
25803 alternate chars. */
25804 left = (leftmost + rightmost - width) / 2;
25805 btm = - descent + boff;
25806 if (font->relative_compose
25807 && (! CHAR_TABLE_P (Vignore_relative_composition)
25808 || NILP (Faref (Vignore_relative_composition,
25809 make_number (ch)))))
25810 {
25811
25812 if (- descent >= font->relative_compose)
25813 /* One extra pixel between two glyphs. */
25814 btm = highest + 1;
25815 else if (ascent <= 0)
25816 /* One extra pixel between two glyphs. */
25817 btm = lowest - 1 - ascent - descent;
25818 }
25819 }
25820 else
25821 {
25822 /* A composition rule is specified by an integer
25823 value that encodes global and new reference
25824 points (GREF and NREF). GREF and NREF are
25825 specified by numbers as below:
25826
25827 0---1---2 -- ascent
25828 | |
25829 | |
25830 | |
25831 9--10--11 -- center
25832 | |
25833 ---3---4---5--- baseline
25834 | |
25835 6---7---8 -- descent
25836 */
25837 int rule = COMPOSITION_RULE (cmp, i);
25838 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25839
25840 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25841 grefx = gref % 3, nrefx = nref % 3;
25842 grefy = gref / 3, nrefy = nref / 3;
25843 if (xoff)
25844 xoff = font_height * (xoff - 128) / 256;
25845 if (yoff)
25846 yoff = font_height * (yoff - 128) / 256;
25847
25848 left = (leftmost
25849 + grefx * (rightmost - leftmost) / 2
25850 - nrefx * width / 2
25851 + xoff);
25852
25853 btm = ((grefy == 0 ? highest
25854 : grefy == 1 ? 0
25855 : grefy == 2 ? lowest
25856 : (highest + lowest) / 2)
25857 - (nrefy == 0 ? ascent + descent
25858 : nrefy == 1 ? descent - boff
25859 : nrefy == 2 ? 0
25860 : (ascent + descent) / 2)
25861 + yoff);
25862 }
25863
25864 cmp->offsets[i * 2] = left;
25865 cmp->offsets[i * 2 + 1] = btm + descent;
25866
25867 /* Update the bounding box of the overall glyphs. */
25868 if (width > 0)
25869 {
25870 right = left + width;
25871 if (left < leftmost)
25872 leftmost = left;
25873 if (right > rightmost)
25874 rightmost = right;
25875 }
25876 top = btm + descent + ascent;
25877 if (top > highest)
25878 highest = top;
25879 if (btm < lowest)
25880 lowest = btm;
25881
25882 if (cmp->lbearing > left + lbearing)
25883 cmp->lbearing = left + lbearing;
25884 if (cmp->rbearing < left + rbearing)
25885 cmp->rbearing = left + rbearing;
25886 }
25887 }
25888
25889 /* If there are glyphs whose x-offsets are negative,
25890 shift all glyphs to the right and make all x-offsets
25891 non-negative. */
25892 if (leftmost < 0)
25893 {
25894 for (i = 0; i < cmp->glyph_len; i++)
25895 cmp->offsets[i * 2] -= leftmost;
25896 rightmost -= leftmost;
25897 cmp->lbearing -= leftmost;
25898 cmp->rbearing -= leftmost;
25899 }
25900
25901 if (left_padded && cmp->lbearing < 0)
25902 {
25903 for (i = 0; i < cmp->glyph_len; i++)
25904 cmp->offsets[i * 2] -= cmp->lbearing;
25905 rightmost -= cmp->lbearing;
25906 cmp->rbearing -= cmp->lbearing;
25907 cmp->lbearing = 0;
25908 }
25909 if (right_padded && rightmost < cmp->rbearing)
25910 {
25911 rightmost = cmp->rbearing;
25912 }
25913
25914 cmp->pixel_width = rightmost;
25915 cmp->ascent = highest;
25916 cmp->descent = - lowest;
25917 if (cmp->ascent < font_ascent)
25918 cmp->ascent = font_ascent;
25919 if (cmp->descent < font_descent)
25920 cmp->descent = font_descent;
25921 }
25922
25923 if (it->glyph_row
25924 && (cmp->lbearing < 0
25925 || cmp->rbearing > cmp->pixel_width))
25926 it->glyph_row->contains_overlapping_glyphs_p = 1;
25927
25928 it->pixel_width = cmp->pixel_width;
25929 it->ascent = it->phys_ascent = cmp->ascent;
25930 it->descent = it->phys_descent = cmp->descent;
25931 if (face->box != FACE_NO_BOX)
25932 {
25933 int thick = face->box_line_width;
25934
25935 if (thick > 0)
25936 {
25937 it->ascent += thick;
25938 it->descent += thick;
25939 }
25940 else
25941 thick = - thick;
25942
25943 if (it->start_of_box_run_p)
25944 it->pixel_width += thick;
25945 if (it->end_of_box_run_p)
25946 it->pixel_width += thick;
25947 }
25948
25949 /* If face has an overline, add the height of the overline
25950 (1 pixel) and a 1 pixel margin to the character height. */
25951 if (face->overline_p)
25952 it->ascent += overline_margin;
25953
25954 take_vertical_position_into_account (it);
25955 if (it->ascent < 0)
25956 it->ascent = 0;
25957 if (it->descent < 0)
25958 it->descent = 0;
25959
25960 if (it->glyph_row && cmp->glyph_len > 0)
25961 append_composite_glyph (it);
25962 }
25963 else if (it->what == IT_COMPOSITION)
25964 {
25965 /* A dynamic (automatic) composition. */
25966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25967 Lisp_Object gstring;
25968 struct font_metrics metrics;
25969
25970 it->nglyphs = 1;
25971
25972 gstring = composition_gstring_from_id (it->cmp_it.id);
25973 it->pixel_width
25974 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25975 &metrics);
25976 if (it->glyph_row
25977 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25978 it->glyph_row->contains_overlapping_glyphs_p = 1;
25979 it->ascent = it->phys_ascent = metrics.ascent;
25980 it->descent = it->phys_descent = metrics.descent;
25981 if (face->box != FACE_NO_BOX)
25982 {
25983 int thick = face->box_line_width;
25984
25985 if (thick > 0)
25986 {
25987 it->ascent += thick;
25988 it->descent += thick;
25989 }
25990 else
25991 thick = - thick;
25992
25993 if (it->start_of_box_run_p)
25994 it->pixel_width += thick;
25995 if (it->end_of_box_run_p)
25996 it->pixel_width += thick;
25997 }
25998 /* If face has an overline, add the height of the overline
25999 (1 pixel) and a 1 pixel margin to the character height. */
26000 if (face->overline_p)
26001 it->ascent += overline_margin;
26002 take_vertical_position_into_account (it);
26003 if (it->ascent < 0)
26004 it->ascent = 0;
26005 if (it->descent < 0)
26006 it->descent = 0;
26007
26008 if (it->glyph_row)
26009 append_composite_glyph (it);
26010 }
26011 else if (it->what == IT_GLYPHLESS)
26012 produce_glyphless_glyph (it, 0, Qnil);
26013 else if (it->what == IT_IMAGE)
26014 produce_image_glyph (it);
26015 else if (it->what == IT_STRETCH)
26016 produce_stretch_glyph (it);
26017
26018 done:
26019 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26020 because this isn't true for images with `:ascent 100'. */
26021 eassert (it->ascent >= 0 && it->descent >= 0);
26022 if (it->area == TEXT_AREA)
26023 it->current_x += it->pixel_width;
26024
26025 if (extra_line_spacing > 0)
26026 {
26027 it->descent += extra_line_spacing;
26028 if (extra_line_spacing > it->max_extra_line_spacing)
26029 it->max_extra_line_spacing = extra_line_spacing;
26030 }
26031
26032 it->max_ascent = max (it->max_ascent, it->ascent);
26033 it->max_descent = max (it->max_descent, it->descent);
26034 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26035 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26036 }
26037
26038 /* EXPORT for RIF:
26039 Output LEN glyphs starting at START at the nominal cursor position.
26040 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26041 being updated, and UPDATED_AREA is the area of that row being updated. */
26042
26043 void
26044 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26045 struct glyph *start, enum glyph_row_area updated_area, int len)
26046 {
26047 int x, hpos, chpos = w->phys_cursor.hpos;
26048
26049 eassert (updated_row);
26050 /* When the window is hscrolled, cursor hpos can legitimately be out
26051 of bounds, but we draw the cursor at the corresponding window
26052 margin in that case. */
26053 if (!updated_row->reversed_p && chpos < 0)
26054 chpos = 0;
26055 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26056 chpos = updated_row->used[TEXT_AREA] - 1;
26057
26058 block_input ();
26059
26060 /* Write glyphs. */
26061
26062 hpos = start - updated_row->glyphs[updated_area];
26063 x = draw_glyphs (w, w->output_cursor.x,
26064 updated_row, updated_area,
26065 hpos, hpos + len,
26066 DRAW_NORMAL_TEXT, 0);
26067
26068 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26069 if (updated_area == TEXT_AREA
26070 && w->phys_cursor_on_p
26071 && w->phys_cursor.vpos == w->output_cursor.vpos
26072 && chpos >= hpos
26073 && chpos < hpos + len)
26074 w->phys_cursor_on_p = 0;
26075
26076 unblock_input ();
26077
26078 /* Advance the output cursor. */
26079 w->output_cursor.hpos += len;
26080 w->output_cursor.x = x;
26081 }
26082
26083
26084 /* EXPORT for RIF:
26085 Insert LEN glyphs from START at the nominal cursor position. */
26086
26087 void
26088 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26089 struct glyph *start, enum glyph_row_area updated_area, int len)
26090 {
26091 struct frame *f;
26092 int line_height, shift_by_width, shifted_region_width;
26093 struct glyph_row *row;
26094 struct glyph *glyph;
26095 int frame_x, frame_y;
26096 ptrdiff_t hpos;
26097
26098 eassert (updated_row);
26099 block_input ();
26100 f = XFRAME (WINDOW_FRAME (w));
26101
26102 /* Get the height of the line we are in. */
26103 row = updated_row;
26104 line_height = row->height;
26105
26106 /* Get the width of the glyphs to insert. */
26107 shift_by_width = 0;
26108 for (glyph = start; glyph < start + len; ++glyph)
26109 shift_by_width += glyph->pixel_width;
26110
26111 /* Get the width of the region to shift right. */
26112 shifted_region_width = (window_box_width (w, updated_area)
26113 - w->output_cursor.x
26114 - shift_by_width);
26115
26116 /* Shift right. */
26117 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26118 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26119
26120 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26121 line_height, shift_by_width);
26122
26123 /* Write the glyphs. */
26124 hpos = start - row->glyphs[updated_area];
26125 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26126 hpos, hpos + len,
26127 DRAW_NORMAL_TEXT, 0);
26128
26129 /* Advance the output cursor. */
26130 w->output_cursor.hpos += len;
26131 w->output_cursor.x += shift_by_width;
26132 unblock_input ();
26133 }
26134
26135
26136 /* EXPORT for RIF:
26137 Erase the current text line from the nominal cursor position
26138 (inclusive) to pixel column TO_X (exclusive). The idea is that
26139 everything from TO_X onward is already erased.
26140
26141 TO_X is a pixel position relative to UPDATED_AREA of currently
26142 updated window W. TO_X == -1 means clear to the end of this area. */
26143
26144 void
26145 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26146 enum glyph_row_area updated_area, int to_x)
26147 {
26148 struct frame *f;
26149 int max_x, min_y, max_y;
26150 int from_x, from_y, to_y;
26151
26152 eassert (updated_row);
26153 f = XFRAME (w->frame);
26154
26155 if (updated_row->full_width_p)
26156 max_x = (WINDOW_PIXEL_WIDTH (w)
26157 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26158 else
26159 max_x = window_box_width (w, updated_area);
26160 max_y = window_text_bottom_y (w);
26161
26162 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26163 of window. For TO_X > 0, truncate to end of drawing area. */
26164 if (to_x == 0)
26165 return;
26166 else if (to_x < 0)
26167 to_x = max_x;
26168 else
26169 to_x = min (to_x, max_x);
26170
26171 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26172
26173 /* Notice if the cursor will be cleared by this operation. */
26174 if (!updated_row->full_width_p)
26175 notice_overwritten_cursor (w, updated_area,
26176 w->output_cursor.x, -1,
26177 updated_row->y,
26178 MATRIX_ROW_BOTTOM_Y (updated_row));
26179
26180 from_x = w->output_cursor.x;
26181
26182 /* Translate to frame coordinates. */
26183 if (updated_row->full_width_p)
26184 {
26185 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26186 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26187 }
26188 else
26189 {
26190 int area_left = window_box_left (w, updated_area);
26191 from_x += area_left;
26192 to_x += area_left;
26193 }
26194
26195 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26196 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26197 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26198
26199 /* Prevent inadvertently clearing to end of the X window. */
26200 if (to_x > from_x && to_y > from_y)
26201 {
26202 block_input ();
26203 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26204 to_x - from_x, to_y - from_y);
26205 unblock_input ();
26206 }
26207 }
26208
26209 #endif /* HAVE_WINDOW_SYSTEM */
26210
26211
26212 \f
26213 /***********************************************************************
26214 Cursor types
26215 ***********************************************************************/
26216
26217 /* Value is the internal representation of the specified cursor type
26218 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26219 of the bar cursor. */
26220
26221 static enum text_cursor_kinds
26222 get_specified_cursor_type (Lisp_Object arg, int *width)
26223 {
26224 enum text_cursor_kinds type;
26225
26226 if (NILP (arg))
26227 return NO_CURSOR;
26228
26229 if (EQ (arg, Qbox))
26230 return FILLED_BOX_CURSOR;
26231
26232 if (EQ (arg, Qhollow))
26233 return HOLLOW_BOX_CURSOR;
26234
26235 if (EQ (arg, Qbar))
26236 {
26237 *width = 2;
26238 return BAR_CURSOR;
26239 }
26240
26241 if (CONSP (arg)
26242 && EQ (XCAR (arg), Qbar)
26243 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26244 {
26245 *width = XINT (XCDR (arg));
26246 return BAR_CURSOR;
26247 }
26248
26249 if (EQ (arg, Qhbar))
26250 {
26251 *width = 2;
26252 return HBAR_CURSOR;
26253 }
26254
26255 if (CONSP (arg)
26256 && EQ (XCAR (arg), Qhbar)
26257 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26258 {
26259 *width = XINT (XCDR (arg));
26260 return HBAR_CURSOR;
26261 }
26262
26263 /* Treat anything unknown as "hollow box cursor".
26264 It was bad to signal an error; people have trouble fixing
26265 .Xdefaults with Emacs, when it has something bad in it. */
26266 type = HOLLOW_BOX_CURSOR;
26267
26268 return type;
26269 }
26270
26271 /* Set the default cursor types for specified frame. */
26272 void
26273 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26274 {
26275 int width = 1;
26276 Lisp_Object tem;
26277
26278 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26279 FRAME_CURSOR_WIDTH (f) = width;
26280
26281 /* By default, set up the blink-off state depending on the on-state. */
26282
26283 tem = Fassoc (arg, Vblink_cursor_alist);
26284 if (!NILP (tem))
26285 {
26286 FRAME_BLINK_OFF_CURSOR (f)
26287 = get_specified_cursor_type (XCDR (tem), &width);
26288 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26289 }
26290 else
26291 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26292
26293 /* Make sure the cursor gets redrawn. */
26294 f->cursor_type_changed = 1;
26295 }
26296
26297
26298 #ifdef HAVE_WINDOW_SYSTEM
26299
26300 /* Return the cursor we want to be displayed in window W. Return
26301 width of bar/hbar cursor through WIDTH arg. Return with
26302 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26303 (i.e. if the `system caret' should track this cursor).
26304
26305 In a mini-buffer window, we want the cursor only to appear if we
26306 are reading input from this window. For the selected window, we
26307 want the cursor type given by the frame parameter or buffer local
26308 setting of cursor-type. If explicitly marked off, draw no cursor.
26309 In all other cases, we want a hollow box cursor. */
26310
26311 static enum text_cursor_kinds
26312 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26313 int *active_cursor)
26314 {
26315 struct frame *f = XFRAME (w->frame);
26316 struct buffer *b = XBUFFER (w->contents);
26317 int cursor_type = DEFAULT_CURSOR;
26318 Lisp_Object alt_cursor;
26319 int non_selected = 0;
26320
26321 *active_cursor = 1;
26322
26323 /* Echo area */
26324 if (cursor_in_echo_area
26325 && FRAME_HAS_MINIBUF_P (f)
26326 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26327 {
26328 if (w == XWINDOW (echo_area_window))
26329 {
26330 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26331 {
26332 *width = FRAME_CURSOR_WIDTH (f);
26333 return FRAME_DESIRED_CURSOR (f);
26334 }
26335 else
26336 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26337 }
26338
26339 *active_cursor = 0;
26340 non_selected = 1;
26341 }
26342
26343 /* Detect a nonselected window or nonselected frame. */
26344 else if (w != XWINDOW (f->selected_window)
26345 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26346 {
26347 *active_cursor = 0;
26348
26349 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26350 return NO_CURSOR;
26351
26352 non_selected = 1;
26353 }
26354
26355 /* Never display a cursor in a window in which cursor-type is nil. */
26356 if (NILP (BVAR (b, cursor_type)))
26357 return NO_CURSOR;
26358
26359 /* Get the normal cursor type for this window. */
26360 if (EQ (BVAR (b, cursor_type), Qt))
26361 {
26362 cursor_type = FRAME_DESIRED_CURSOR (f);
26363 *width = FRAME_CURSOR_WIDTH (f);
26364 }
26365 else
26366 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26367
26368 /* Use cursor-in-non-selected-windows instead
26369 for non-selected window or frame. */
26370 if (non_selected)
26371 {
26372 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26373 if (!EQ (Qt, alt_cursor))
26374 return get_specified_cursor_type (alt_cursor, width);
26375 /* t means modify the normal cursor type. */
26376 if (cursor_type == FILLED_BOX_CURSOR)
26377 cursor_type = HOLLOW_BOX_CURSOR;
26378 else if (cursor_type == BAR_CURSOR && *width > 1)
26379 --*width;
26380 return cursor_type;
26381 }
26382
26383 /* Use normal cursor if not blinked off. */
26384 if (!w->cursor_off_p)
26385 {
26386 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26387 {
26388 if (cursor_type == FILLED_BOX_CURSOR)
26389 {
26390 /* Using a block cursor on large images can be very annoying.
26391 So use a hollow cursor for "large" images.
26392 If image is not transparent (no mask), also use hollow cursor. */
26393 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26394 if (img != NULL && IMAGEP (img->spec))
26395 {
26396 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26397 where N = size of default frame font size.
26398 This should cover most of the "tiny" icons people may use. */
26399 if (!img->mask
26400 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26401 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26402 cursor_type = HOLLOW_BOX_CURSOR;
26403 }
26404 }
26405 else if (cursor_type != NO_CURSOR)
26406 {
26407 /* Display current only supports BOX and HOLLOW cursors for images.
26408 So for now, unconditionally use a HOLLOW cursor when cursor is
26409 not a solid box cursor. */
26410 cursor_type = HOLLOW_BOX_CURSOR;
26411 }
26412 }
26413 return cursor_type;
26414 }
26415
26416 /* Cursor is blinked off, so determine how to "toggle" it. */
26417
26418 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26419 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26420 return get_specified_cursor_type (XCDR (alt_cursor), width);
26421
26422 /* Then see if frame has specified a specific blink off cursor type. */
26423 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26424 {
26425 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26426 return FRAME_BLINK_OFF_CURSOR (f);
26427 }
26428
26429 #if 0
26430 /* Some people liked having a permanently visible blinking cursor,
26431 while others had very strong opinions against it. So it was
26432 decided to remove it. KFS 2003-09-03 */
26433
26434 /* Finally perform built-in cursor blinking:
26435 filled box <-> hollow box
26436 wide [h]bar <-> narrow [h]bar
26437 narrow [h]bar <-> no cursor
26438 other type <-> no cursor */
26439
26440 if (cursor_type == FILLED_BOX_CURSOR)
26441 return HOLLOW_BOX_CURSOR;
26442
26443 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26444 {
26445 *width = 1;
26446 return cursor_type;
26447 }
26448 #endif
26449
26450 return NO_CURSOR;
26451 }
26452
26453
26454 /* Notice when the text cursor of window W has been completely
26455 overwritten by a drawing operation that outputs glyphs in AREA
26456 starting at X0 and ending at X1 in the line starting at Y0 and
26457 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26458 the rest of the line after X0 has been written. Y coordinates
26459 are window-relative. */
26460
26461 static void
26462 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26463 int x0, int x1, int y0, int y1)
26464 {
26465 int cx0, cx1, cy0, cy1;
26466 struct glyph_row *row;
26467
26468 if (!w->phys_cursor_on_p)
26469 return;
26470 if (area != TEXT_AREA)
26471 return;
26472
26473 if (w->phys_cursor.vpos < 0
26474 || w->phys_cursor.vpos >= w->current_matrix->nrows
26475 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26476 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26477 return;
26478
26479 if (row->cursor_in_fringe_p)
26480 {
26481 row->cursor_in_fringe_p = 0;
26482 draw_fringe_bitmap (w, row, row->reversed_p);
26483 w->phys_cursor_on_p = 0;
26484 return;
26485 }
26486
26487 cx0 = w->phys_cursor.x;
26488 cx1 = cx0 + w->phys_cursor_width;
26489 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26490 return;
26491
26492 /* The cursor image will be completely removed from the
26493 screen if the output area intersects the cursor area in
26494 y-direction. When we draw in [y0 y1[, and some part of
26495 the cursor is at y < y0, that part must have been drawn
26496 before. When scrolling, the cursor is erased before
26497 actually scrolling, so we don't come here. When not
26498 scrolling, the rows above the old cursor row must have
26499 changed, and in this case these rows must have written
26500 over the cursor image.
26501
26502 Likewise if part of the cursor is below y1, with the
26503 exception of the cursor being in the first blank row at
26504 the buffer and window end because update_text_area
26505 doesn't draw that row. (Except when it does, but
26506 that's handled in update_text_area.) */
26507
26508 cy0 = w->phys_cursor.y;
26509 cy1 = cy0 + w->phys_cursor_height;
26510 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26511 return;
26512
26513 w->phys_cursor_on_p = 0;
26514 }
26515
26516 #endif /* HAVE_WINDOW_SYSTEM */
26517
26518 \f
26519 /************************************************************************
26520 Mouse Face
26521 ************************************************************************/
26522
26523 #ifdef HAVE_WINDOW_SYSTEM
26524
26525 /* EXPORT for RIF:
26526 Fix the display of area AREA of overlapping row ROW in window W
26527 with respect to the overlapping part OVERLAPS. */
26528
26529 void
26530 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26531 enum glyph_row_area area, int overlaps)
26532 {
26533 int i, x;
26534
26535 block_input ();
26536
26537 x = 0;
26538 for (i = 0; i < row->used[area];)
26539 {
26540 if (row->glyphs[area][i].overlaps_vertically_p)
26541 {
26542 int start = i, start_x = x;
26543
26544 do
26545 {
26546 x += row->glyphs[area][i].pixel_width;
26547 ++i;
26548 }
26549 while (i < row->used[area]
26550 && row->glyphs[area][i].overlaps_vertically_p);
26551
26552 draw_glyphs (w, start_x, row, area,
26553 start, i,
26554 DRAW_NORMAL_TEXT, overlaps);
26555 }
26556 else
26557 {
26558 x += row->glyphs[area][i].pixel_width;
26559 ++i;
26560 }
26561 }
26562
26563 unblock_input ();
26564 }
26565
26566
26567 /* EXPORT:
26568 Draw the cursor glyph of window W in glyph row ROW. See the
26569 comment of draw_glyphs for the meaning of HL. */
26570
26571 void
26572 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26573 enum draw_glyphs_face hl)
26574 {
26575 /* If cursor hpos is out of bounds, don't draw garbage. This can
26576 happen in mini-buffer windows when switching between echo area
26577 glyphs and mini-buffer. */
26578 if ((row->reversed_p
26579 ? (w->phys_cursor.hpos >= 0)
26580 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26581 {
26582 int on_p = w->phys_cursor_on_p;
26583 int x1;
26584 int hpos = w->phys_cursor.hpos;
26585
26586 /* When the window is hscrolled, cursor hpos can legitimately be
26587 out of bounds, but we draw the cursor at the corresponding
26588 window margin in that case. */
26589 if (!row->reversed_p && hpos < 0)
26590 hpos = 0;
26591 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26592 hpos = row->used[TEXT_AREA] - 1;
26593
26594 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26595 hl, 0);
26596 w->phys_cursor_on_p = on_p;
26597
26598 if (hl == DRAW_CURSOR)
26599 w->phys_cursor_width = x1 - w->phys_cursor.x;
26600 /* When we erase the cursor, and ROW is overlapped by other
26601 rows, make sure that these overlapping parts of other rows
26602 are redrawn. */
26603 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26604 {
26605 w->phys_cursor_width = x1 - w->phys_cursor.x;
26606
26607 if (row > w->current_matrix->rows
26608 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26609 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26610 OVERLAPS_ERASED_CURSOR);
26611
26612 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26613 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26614 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26615 OVERLAPS_ERASED_CURSOR);
26616 }
26617 }
26618 }
26619
26620
26621 /* Erase the image of a cursor of window W from the screen. */
26622
26623 #ifndef HAVE_NTGUI
26624 static
26625 #endif
26626 void
26627 erase_phys_cursor (struct window *w)
26628 {
26629 struct frame *f = XFRAME (w->frame);
26630 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26631 int hpos = w->phys_cursor.hpos;
26632 int vpos = w->phys_cursor.vpos;
26633 int mouse_face_here_p = 0;
26634 struct glyph_matrix *active_glyphs = w->current_matrix;
26635 struct glyph_row *cursor_row;
26636 struct glyph *cursor_glyph;
26637 enum draw_glyphs_face hl;
26638
26639 /* No cursor displayed or row invalidated => nothing to do on the
26640 screen. */
26641 if (w->phys_cursor_type == NO_CURSOR)
26642 goto mark_cursor_off;
26643
26644 /* VPOS >= active_glyphs->nrows means that window has been resized.
26645 Don't bother to erase the cursor. */
26646 if (vpos >= active_glyphs->nrows)
26647 goto mark_cursor_off;
26648
26649 /* If row containing cursor is marked invalid, there is nothing we
26650 can do. */
26651 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26652 if (!cursor_row->enabled_p)
26653 goto mark_cursor_off;
26654
26655 /* If line spacing is > 0, old cursor may only be partially visible in
26656 window after split-window. So adjust visible height. */
26657 cursor_row->visible_height = min (cursor_row->visible_height,
26658 window_text_bottom_y (w) - cursor_row->y);
26659
26660 /* If row is completely invisible, don't attempt to delete a cursor which
26661 isn't there. This can happen if cursor is at top of a window, and
26662 we switch to a buffer with a header line in that window. */
26663 if (cursor_row->visible_height <= 0)
26664 goto mark_cursor_off;
26665
26666 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26667 if (cursor_row->cursor_in_fringe_p)
26668 {
26669 cursor_row->cursor_in_fringe_p = 0;
26670 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26671 goto mark_cursor_off;
26672 }
26673
26674 /* This can happen when the new row is shorter than the old one.
26675 In this case, either draw_glyphs or clear_end_of_line
26676 should have cleared the cursor. Note that we wouldn't be
26677 able to erase the cursor in this case because we don't have a
26678 cursor glyph at hand. */
26679 if ((cursor_row->reversed_p
26680 ? (w->phys_cursor.hpos < 0)
26681 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26682 goto mark_cursor_off;
26683
26684 /* When the window is hscrolled, cursor hpos can legitimately be out
26685 of bounds, but we draw the cursor at the corresponding window
26686 margin in that case. */
26687 if (!cursor_row->reversed_p && hpos < 0)
26688 hpos = 0;
26689 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26690 hpos = cursor_row->used[TEXT_AREA] - 1;
26691
26692 /* If the cursor is in the mouse face area, redisplay that when
26693 we clear the cursor. */
26694 if (! NILP (hlinfo->mouse_face_window)
26695 && coords_in_mouse_face_p (w, hpos, vpos)
26696 /* Don't redraw the cursor's spot in mouse face if it is at the
26697 end of a line (on a newline). The cursor appears there, but
26698 mouse highlighting does not. */
26699 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26700 mouse_face_here_p = 1;
26701
26702 /* Maybe clear the display under the cursor. */
26703 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26704 {
26705 int x, y, left_x;
26706 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26707 int width;
26708
26709 cursor_glyph = get_phys_cursor_glyph (w);
26710 if (cursor_glyph == NULL)
26711 goto mark_cursor_off;
26712
26713 width = cursor_glyph->pixel_width;
26714 left_x = window_box_left_offset (w, TEXT_AREA);
26715 x = w->phys_cursor.x;
26716 if (x < left_x)
26717 width -= left_x - x;
26718 width = min (width, window_box_width (w, TEXT_AREA) - x);
26719 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26720 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26721
26722 if (width > 0)
26723 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26724 }
26725
26726 /* Erase the cursor by redrawing the character underneath it. */
26727 if (mouse_face_here_p)
26728 hl = DRAW_MOUSE_FACE;
26729 else
26730 hl = DRAW_NORMAL_TEXT;
26731 draw_phys_cursor_glyph (w, cursor_row, hl);
26732
26733 mark_cursor_off:
26734 w->phys_cursor_on_p = 0;
26735 w->phys_cursor_type = NO_CURSOR;
26736 }
26737
26738
26739 /* EXPORT:
26740 Display or clear cursor of window W. If ON is zero, clear the
26741 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26742 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26743
26744 void
26745 display_and_set_cursor (struct window *w, bool on,
26746 int hpos, int vpos, int x, int y)
26747 {
26748 struct frame *f = XFRAME (w->frame);
26749 int new_cursor_type;
26750 int new_cursor_width;
26751 int active_cursor;
26752 struct glyph_row *glyph_row;
26753 struct glyph *glyph;
26754
26755 /* This is pointless on invisible frames, and dangerous on garbaged
26756 windows and frames; in the latter case, the frame or window may
26757 be in the midst of changing its size, and x and y may be off the
26758 window. */
26759 if (! FRAME_VISIBLE_P (f)
26760 || FRAME_GARBAGED_P (f)
26761 || vpos >= w->current_matrix->nrows
26762 || hpos >= w->current_matrix->matrix_w)
26763 return;
26764
26765 /* If cursor is off and we want it off, return quickly. */
26766 if (!on && !w->phys_cursor_on_p)
26767 return;
26768
26769 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26770 /* If cursor row is not enabled, we don't really know where to
26771 display the cursor. */
26772 if (!glyph_row->enabled_p)
26773 {
26774 w->phys_cursor_on_p = 0;
26775 return;
26776 }
26777
26778 glyph = NULL;
26779 if (!glyph_row->exact_window_width_line_p
26780 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26781 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26782
26783 eassert (input_blocked_p ());
26784
26785 /* Set new_cursor_type to the cursor we want to be displayed. */
26786 new_cursor_type = get_window_cursor_type (w, glyph,
26787 &new_cursor_width, &active_cursor);
26788
26789 /* If cursor is currently being shown and we don't want it to be or
26790 it is in the wrong place, or the cursor type is not what we want,
26791 erase it. */
26792 if (w->phys_cursor_on_p
26793 && (!on
26794 || w->phys_cursor.x != x
26795 || w->phys_cursor.y != y
26796 || new_cursor_type != w->phys_cursor_type
26797 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26798 && new_cursor_width != w->phys_cursor_width)))
26799 erase_phys_cursor (w);
26800
26801 /* Don't check phys_cursor_on_p here because that flag is only set
26802 to zero in some cases where we know that the cursor has been
26803 completely erased, to avoid the extra work of erasing the cursor
26804 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26805 still not be visible, or it has only been partly erased. */
26806 if (on)
26807 {
26808 w->phys_cursor_ascent = glyph_row->ascent;
26809 w->phys_cursor_height = glyph_row->height;
26810
26811 /* Set phys_cursor_.* before x_draw_.* is called because some
26812 of them may need the information. */
26813 w->phys_cursor.x = x;
26814 w->phys_cursor.y = glyph_row->y;
26815 w->phys_cursor.hpos = hpos;
26816 w->phys_cursor.vpos = vpos;
26817 }
26818
26819 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26820 new_cursor_type, new_cursor_width,
26821 on, active_cursor);
26822 }
26823
26824
26825 /* Switch the display of W's cursor on or off, according to the value
26826 of ON. */
26827
26828 static void
26829 update_window_cursor (struct window *w, bool on)
26830 {
26831 /* Don't update cursor in windows whose frame is in the process
26832 of being deleted. */
26833 if (w->current_matrix)
26834 {
26835 int hpos = w->phys_cursor.hpos;
26836 int vpos = w->phys_cursor.vpos;
26837 struct glyph_row *row;
26838
26839 if (vpos >= w->current_matrix->nrows
26840 || hpos >= w->current_matrix->matrix_w)
26841 return;
26842
26843 row = MATRIX_ROW (w->current_matrix, vpos);
26844
26845 /* When the window is hscrolled, cursor hpos can legitimately be
26846 out of bounds, but we draw the cursor at the corresponding
26847 window margin in that case. */
26848 if (!row->reversed_p && hpos < 0)
26849 hpos = 0;
26850 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26851 hpos = row->used[TEXT_AREA] - 1;
26852
26853 block_input ();
26854 display_and_set_cursor (w, on, hpos, vpos,
26855 w->phys_cursor.x, w->phys_cursor.y);
26856 unblock_input ();
26857 }
26858 }
26859
26860
26861 /* Call update_window_cursor with parameter ON_P on all leaf windows
26862 in the window tree rooted at W. */
26863
26864 static void
26865 update_cursor_in_window_tree (struct window *w, bool on_p)
26866 {
26867 while (w)
26868 {
26869 if (WINDOWP (w->contents))
26870 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26871 else
26872 update_window_cursor (w, on_p);
26873
26874 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26875 }
26876 }
26877
26878
26879 /* EXPORT:
26880 Display the cursor on window W, or clear it, according to ON_P.
26881 Don't change the cursor's position. */
26882
26883 void
26884 x_update_cursor (struct frame *f, bool on_p)
26885 {
26886 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26887 }
26888
26889
26890 /* EXPORT:
26891 Clear the cursor of window W to background color, and mark the
26892 cursor as not shown. This is used when the text where the cursor
26893 is about to be rewritten. */
26894
26895 void
26896 x_clear_cursor (struct window *w)
26897 {
26898 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26899 update_window_cursor (w, 0);
26900 }
26901
26902 #endif /* HAVE_WINDOW_SYSTEM */
26903
26904 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26905 and MSDOS. */
26906 static void
26907 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26908 int start_hpos, int end_hpos,
26909 enum draw_glyphs_face draw)
26910 {
26911 #ifdef HAVE_WINDOW_SYSTEM
26912 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26913 {
26914 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26915 return;
26916 }
26917 #endif
26918 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26919 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26920 #endif
26921 }
26922
26923 /* Display the active region described by mouse_face_* according to DRAW. */
26924
26925 static void
26926 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26927 {
26928 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26929 struct frame *f = XFRAME (WINDOW_FRAME (w));
26930
26931 if (/* If window is in the process of being destroyed, don't bother
26932 to do anything. */
26933 w->current_matrix != NULL
26934 /* Don't update mouse highlight if hidden */
26935 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26936 /* Recognize when we are called to operate on rows that don't exist
26937 anymore. This can happen when a window is split. */
26938 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26939 {
26940 int phys_cursor_on_p = w->phys_cursor_on_p;
26941 struct glyph_row *row, *first, *last;
26942
26943 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26944 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26945
26946 for (row = first; row <= last && row->enabled_p; ++row)
26947 {
26948 int start_hpos, end_hpos, start_x;
26949
26950 /* For all but the first row, the highlight starts at column 0. */
26951 if (row == first)
26952 {
26953 /* R2L rows have BEG and END in reversed order, but the
26954 screen drawing geometry is always left to right. So
26955 we need to mirror the beginning and end of the
26956 highlighted area in R2L rows. */
26957 if (!row->reversed_p)
26958 {
26959 start_hpos = hlinfo->mouse_face_beg_col;
26960 start_x = hlinfo->mouse_face_beg_x;
26961 }
26962 else if (row == last)
26963 {
26964 start_hpos = hlinfo->mouse_face_end_col;
26965 start_x = hlinfo->mouse_face_end_x;
26966 }
26967 else
26968 {
26969 start_hpos = 0;
26970 start_x = 0;
26971 }
26972 }
26973 else if (row->reversed_p && row == last)
26974 {
26975 start_hpos = hlinfo->mouse_face_end_col;
26976 start_x = hlinfo->mouse_face_end_x;
26977 }
26978 else
26979 {
26980 start_hpos = 0;
26981 start_x = 0;
26982 }
26983
26984 if (row == last)
26985 {
26986 if (!row->reversed_p)
26987 end_hpos = hlinfo->mouse_face_end_col;
26988 else if (row == first)
26989 end_hpos = hlinfo->mouse_face_beg_col;
26990 else
26991 {
26992 end_hpos = row->used[TEXT_AREA];
26993 if (draw == DRAW_NORMAL_TEXT)
26994 row->fill_line_p = 1; /* Clear to end of line */
26995 }
26996 }
26997 else if (row->reversed_p && row == first)
26998 end_hpos = hlinfo->mouse_face_beg_col;
26999 else
27000 {
27001 end_hpos = row->used[TEXT_AREA];
27002 if (draw == DRAW_NORMAL_TEXT)
27003 row->fill_line_p = 1; /* Clear to end of line */
27004 }
27005
27006 if (end_hpos > start_hpos)
27007 {
27008 draw_row_with_mouse_face (w, start_x, row,
27009 start_hpos, end_hpos, draw);
27010
27011 row->mouse_face_p
27012 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27013 }
27014 }
27015
27016 #ifdef HAVE_WINDOW_SYSTEM
27017 /* When we've written over the cursor, arrange for it to
27018 be displayed again. */
27019 if (FRAME_WINDOW_P (f)
27020 && phys_cursor_on_p && !w->phys_cursor_on_p)
27021 {
27022 int hpos = w->phys_cursor.hpos;
27023
27024 /* When the window is hscrolled, cursor hpos can legitimately be
27025 out of bounds, but we draw the cursor at the corresponding
27026 window margin in that case. */
27027 if (!row->reversed_p && hpos < 0)
27028 hpos = 0;
27029 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27030 hpos = row->used[TEXT_AREA] - 1;
27031
27032 block_input ();
27033 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27034 w->phys_cursor.x, w->phys_cursor.y);
27035 unblock_input ();
27036 }
27037 #endif /* HAVE_WINDOW_SYSTEM */
27038 }
27039
27040 #ifdef HAVE_WINDOW_SYSTEM
27041 /* Change the mouse cursor. */
27042 if (FRAME_WINDOW_P (f))
27043 {
27044 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27045 if (draw == DRAW_NORMAL_TEXT
27046 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27047 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27048 else
27049 #endif
27050 if (draw == DRAW_MOUSE_FACE)
27051 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27052 else
27053 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27054 }
27055 #endif /* HAVE_WINDOW_SYSTEM */
27056 }
27057
27058 /* EXPORT:
27059 Clear out the mouse-highlighted active region.
27060 Redraw it un-highlighted first. Value is non-zero if mouse
27061 face was actually drawn unhighlighted. */
27062
27063 int
27064 clear_mouse_face (Mouse_HLInfo *hlinfo)
27065 {
27066 int cleared = 0;
27067
27068 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27069 {
27070 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27071 cleared = 1;
27072 }
27073
27074 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27075 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27076 hlinfo->mouse_face_window = Qnil;
27077 hlinfo->mouse_face_overlay = Qnil;
27078 return cleared;
27079 }
27080
27081 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
27082 within the mouse face on that window. */
27083 static int
27084 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27085 {
27086 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27087
27088 /* Quickly resolve the easy cases. */
27089 if (!(WINDOWP (hlinfo->mouse_face_window)
27090 && XWINDOW (hlinfo->mouse_face_window) == w))
27091 return 0;
27092 if (vpos < hlinfo->mouse_face_beg_row
27093 || vpos > hlinfo->mouse_face_end_row)
27094 return 0;
27095 if (vpos > hlinfo->mouse_face_beg_row
27096 && vpos < hlinfo->mouse_face_end_row)
27097 return 1;
27098
27099 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27100 {
27101 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27102 {
27103 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27104 return 1;
27105 }
27106 else if ((vpos == hlinfo->mouse_face_beg_row
27107 && hpos >= hlinfo->mouse_face_beg_col)
27108 || (vpos == hlinfo->mouse_face_end_row
27109 && hpos < hlinfo->mouse_face_end_col))
27110 return 1;
27111 }
27112 else
27113 {
27114 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27115 {
27116 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27117 return 1;
27118 }
27119 else if ((vpos == hlinfo->mouse_face_beg_row
27120 && hpos <= hlinfo->mouse_face_beg_col)
27121 || (vpos == hlinfo->mouse_face_end_row
27122 && hpos > hlinfo->mouse_face_end_col))
27123 return 1;
27124 }
27125 return 0;
27126 }
27127
27128
27129 /* EXPORT:
27130 Non-zero if physical cursor of window W is within mouse face. */
27131
27132 int
27133 cursor_in_mouse_face_p (struct window *w)
27134 {
27135 int hpos = w->phys_cursor.hpos;
27136 int vpos = w->phys_cursor.vpos;
27137 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27138
27139 /* When the window is hscrolled, cursor hpos can legitimately be out
27140 of bounds, but we draw the cursor at the corresponding window
27141 margin in that case. */
27142 if (!row->reversed_p && hpos < 0)
27143 hpos = 0;
27144 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27145 hpos = row->used[TEXT_AREA] - 1;
27146
27147 return coords_in_mouse_face_p (w, hpos, vpos);
27148 }
27149
27150
27151 \f
27152 /* Find the glyph rows START_ROW and END_ROW of window W that display
27153 characters between buffer positions START_CHARPOS and END_CHARPOS
27154 (excluding END_CHARPOS). DISP_STRING is a display string that
27155 covers these buffer positions. This is similar to
27156 row_containing_pos, but is more accurate when bidi reordering makes
27157 buffer positions change non-linearly with glyph rows. */
27158 static void
27159 rows_from_pos_range (struct window *w,
27160 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27161 Lisp_Object disp_string,
27162 struct glyph_row **start, struct glyph_row **end)
27163 {
27164 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27165 int last_y = window_text_bottom_y (w);
27166 struct glyph_row *row;
27167
27168 *start = NULL;
27169 *end = NULL;
27170
27171 while (!first->enabled_p
27172 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27173 first++;
27174
27175 /* Find the START row. */
27176 for (row = first;
27177 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27178 row++)
27179 {
27180 /* A row can potentially be the START row if the range of the
27181 characters it displays intersects the range
27182 [START_CHARPOS..END_CHARPOS). */
27183 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27184 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27185 /* See the commentary in row_containing_pos, for the
27186 explanation of the complicated way to check whether
27187 some position is beyond the end of the characters
27188 displayed by a row. */
27189 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27190 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27191 && !row->ends_at_zv_p
27192 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27193 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27194 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27195 && !row->ends_at_zv_p
27196 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27197 {
27198 /* Found a candidate row. Now make sure at least one of the
27199 glyphs it displays has a charpos from the range
27200 [START_CHARPOS..END_CHARPOS).
27201
27202 This is not obvious because bidi reordering could make
27203 buffer positions of a row be 1,2,3,102,101,100, and if we
27204 want to highlight characters in [50..60), we don't want
27205 this row, even though [50..60) does intersect [1..103),
27206 the range of character positions given by the row's start
27207 and end positions. */
27208 struct glyph *g = row->glyphs[TEXT_AREA];
27209 struct glyph *e = g + row->used[TEXT_AREA];
27210
27211 while (g < e)
27212 {
27213 if (((BUFFERP (g->object) || INTEGERP (g->object))
27214 && start_charpos <= g->charpos && g->charpos < end_charpos)
27215 /* A glyph that comes from DISP_STRING is by
27216 definition to be highlighted. */
27217 || EQ (g->object, disp_string))
27218 *start = row;
27219 g++;
27220 }
27221 if (*start)
27222 break;
27223 }
27224 }
27225
27226 /* Find the END row. */
27227 if (!*start
27228 /* If the last row is partially visible, start looking for END
27229 from that row, instead of starting from FIRST. */
27230 && !(row->enabled_p
27231 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27232 row = first;
27233 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27234 {
27235 struct glyph_row *next = row + 1;
27236 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27237
27238 if (!next->enabled_p
27239 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27240 /* The first row >= START whose range of displayed characters
27241 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27242 is the row END + 1. */
27243 || (start_charpos < next_start
27244 && end_charpos < next_start)
27245 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27246 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27247 && !next->ends_at_zv_p
27248 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27249 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27250 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27251 && !next->ends_at_zv_p
27252 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27253 {
27254 *end = row;
27255 break;
27256 }
27257 else
27258 {
27259 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27260 but none of the characters it displays are in the range, it is
27261 also END + 1. */
27262 struct glyph *g = next->glyphs[TEXT_AREA];
27263 struct glyph *s = g;
27264 struct glyph *e = g + next->used[TEXT_AREA];
27265
27266 while (g < e)
27267 {
27268 if (((BUFFERP (g->object) || INTEGERP (g->object))
27269 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27270 /* If the buffer position of the first glyph in
27271 the row is equal to END_CHARPOS, it means
27272 the last character to be highlighted is the
27273 newline of ROW, and we must consider NEXT as
27274 END, not END+1. */
27275 || (((!next->reversed_p && g == s)
27276 || (next->reversed_p && g == e - 1))
27277 && (g->charpos == end_charpos
27278 /* Special case for when NEXT is an
27279 empty line at ZV. */
27280 || (g->charpos == -1
27281 && !row->ends_at_zv_p
27282 && next_start == end_charpos)))))
27283 /* A glyph that comes from DISP_STRING is by
27284 definition to be highlighted. */
27285 || EQ (g->object, disp_string))
27286 break;
27287 g++;
27288 }
27289 if (g == e)
27290 {
27291 *end = row;
27292 break;
27293 }
27294 /* The first row that ends at ZV must be the last to be
27295 highlighted. */
27296 else if (next->ends_at_zv_p)
27297 {
27298 *end = next;
27299 break;
27300 }
27301 }
27302 }
27303 }
27304
27305 /* This function sets the mouse_face_* elements of HLINFO, assuming
27306 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27307 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27308 for the overlay or run of text properties specifying the mouse
27309 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27310 before-string and after-string that must also be highlighted.
27311 DISP_STRING, if non-nil, is a display string that may cover some
27312 or all of the highlighted text. */
27313
27314 static void
27315 mouse_face_from_buffer_pos (Lisp_Object window,
27316 Mouse_HLInfo *hlinfo,
27317 ptrdiff_t mouse_charpos,
27318 ptrdiff_t start_charpos,
27319 ptrdiff_t end_charpos,
27320 Lisp_Object before_string,
27321 Lisp_Object after_string,
27322 Lisp_Object disp_string)
27323 {
27324 struct window *w = XWINDOW (window);
27325 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27326 struct glyph_row *r1, *r2;
27327 struct glyph *glyph, *end;
27328 ptrdiff_t ignore, pos;
27329 int x;
27330
27331 eassert (NILP (disp_string) || STRINGP (disp_string));
27332 eassert (NILP (before_string) || STRINGP (before_string));
27333 eassert (NILP (after_string) || STRINGP (after_string));
27334
27335 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27336 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27337 if (r1 == NULL)
27338 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27339 /* If the before-string or display-string contains newlines,
27340 rows_from_pos_range skips to its last row. Move back. */
27341 if (!NILP (before_string) || !NILP (disp_string))
27342 {
27343 struct glyph_row *prev;
27344 while ((prev = r1 - 1, prev >= first)
27345 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27346 && prev->used[TEXT_AREA] > 0)
27347 {
27348 struct glyph *beg = prev->glyphs[TEXT_AREA];
27349 glyph = beg + prev->used[TEXT_AREA];
27350 while (--glyph >= beg && INTEGERP (glyph->object));
27351 if (glyph < beg
27352 || !(EQ (glyph->object, before_string)
27353 || EQ (glyph->object, disp_string)))
27354 break;
27355 r1 = prev;
27356 }
27357 }
27358 if (r2 == NULL)
27359 {
27360 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27361 hlinfo->mouse_face_past_end = 1;
27362 }
27363 else if (!NILP (after_string))
27364 {
27365 /* If the after-string has newlines, advance to its last row. */
27366 struct glyph_row *next;
27367 struct glyph_row *last
27368 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27369
27370 for (next = r2 + 1;
27371 next <= last
27372 && next->used[TEXT_AREA] > 0
27373 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27374 ++next)
27375 r2 = next;
27376 }
27377 /* The rest of the display engine assumes that mouse_face_beg_row is
27378 either above mouse_face_end_row or identical to it. But with
27379 bidi-reordered continued lines, the row for START_CHARPOS could
27380 be below the row for END_CHARPOS. If so, swap the rows and store
27381 them in correct order. */
27382 if (r1->y > r2->y)
27383 {
27384 struct glyph_row *tem = r2;
27385
27386 r2 = r1;
27387 r1 = tem;
27388 }
27389
27390 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27391 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27392
27393 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27394 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27395 could be anywhere in the row and in any order. The strategy
27396 below is to find the leftmost and the rightmost glyph that
27397 belongs to either of these 3 strings, or whose position is
27398 between START_CHARPOS and END_CHARPOS, and highlight all the
27399 glyphs between those two. This may cover more than just the text
27400 between START_CHARPOS and END_CHARPOS if the range of characters
27401 strides the bidi level boundary, e.g. if the beginning is in R2L
27402 text while the end is in L2R text or vice versa. */
27403 if (!r1->reversed_p)
27404 {
27405 /* This row is in a left to right paragraph. Scan it left to
27406 right. */
27407 glyph = r1->glyphs[TEXT_AREA];
27408 end = glyph + r1->used[TEXT_AREA];
27409 x = r1->x;
27410
27411 /* Skip truncation glyphs at the start of the glyph row. */
27412 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27413 for (; glyph < end
27414 && INTEGERP (glyph->object)
27415 && glyph->charpos < 0;
27416 ++glyph)
27417 x += glyph->pixel_width;
27418
27419 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27420 or DISP_STRING, and the first glyph from buffer whose
27421 position is between START_CHARPOS and END_CHARPOS. */
27422 for (; glyph < end
27423 && !INTEGERP (glyph->object)
27424 && !EQ (glyph->object, disp_string)
27425 && !(BUFFERP (glyph->object)
27426 && (glyph->charpos >= start_charpos
27427 && glyph->charpos < end_charpos));
27428 ++glyph)
27429 {
27430 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27431 are present at buffer positions between START_CHARPOS and
27432 END_CHARPOS, or if they come from an overlay. */
27433 if (EQ (glyph->object, before_string))
27434 {
27435 pos = string_buffer_position (before_string,
27436 start_charpos);
27437 /* If pos == 0, it means before_string came from an
27438 overlay, not from a buffer position. */
27439 if (!pos || (pos >= start_charpos && pos < end_charpos))
27440 break;
27441 }
27442 else if (EQ (glyph->object, after_string))
27443 {
27444 pos = string_buffer_position (after_string, end_charpos);
27445 if (!pos || (pos >= start_charpos && pos < end_charpos))
27446 break;
27447 }
27448 x += glyph->pixel_width;
27449 }
27450 hlinfo->mouse_face_beg_x = x;
27451 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27452 }
27453 else
27454 {
27455 /* This row is in a right to left paragraph. Scan it right to
27456 left. */
27457 struct glyph *g;
27458
27459 end = r1->glyphs[TEXT_AREA] - 1;
27460 glyph = end + r1->used[TEXT_AREA];
27461
27462 /* Skip truncation glyphs at the start of the glyph row. */
27463 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27464 for (; glyph > end
27465 && INTEGERP (glyph->object)
27466 && glyph->charpos < 0;
27467 --glyph)
27468 ;
27469
27470 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27471 or DISP_STRING, and the first glyph from buffer whose
27472 position is between START_CHARPOS and END_CHARPOS. */
27473 for (; glyph > end
27474 && !INTEGERP (glyph->object)
27475 && !EQ (glyph->object, disp_string)
27476 && !(BUFFERP (glyph->object)
27477 && (glyph->charpos >= start_charpos
27478 && glyph->charpos < end_charpos));
27479 --glyph)
27480 {
27481 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27482 are present at buffer positions between START_CHARPOS and
27483 END_CHARPOS, or if they come from an overlay. */
27484 if (EQ (glyph->object, before_string))
27485 {
27486 pos = string_buffer_position (before_string, start_charpos);
27487 /* If pos == 0, it means before_string came from an
27488 overlay, not from a buffer position. */
27489 if (!pos || (pos >= start_charpos && pos < end_charpos))
27490 break;
27491 }
27492 else if (EQ (glyph->object, after_string))
27493 {
27494 pos = string_buffer_position (after_string, end_charpos);
27495 if (!pos || (pos >= start_charpos && pos < end_charpos))
27496 break;
27497 }
27498 }
27499
27500 glyph++; /* first glyph to the right of the highlighted area */
27501 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27502 x += g->pixel_width;
27503 hlinfo->mouse_face_beg_x = x;
27504 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27505 }
27506
27507 /* If the highlight ends in a different row, compute GLYPH and END
27508 for the end row. Otherwise, reuse the values computed above for
27509 the row where the highlight begins. */
27510 if (r2 != r1)
27511 {
27512 if (!r2->reversed_p)
27513 {
27514 glyph = r2->glyphs[TEXT_AREA];
27515 end = glyph + r2->used[TEXT_AREA];
27516 x = r2->x;
27517 }
27518 else
27519 {
27520 end = r2->glyphs[TEXT_AREA] - 1;
27521 glyph = end + r2->used[TEXT_AREA];
27522 }
27523 }
27524
27525 if (!r2->reversed_p)
27526 {
27527 /* Skip truncation and continuation glyphs near the end of the
27528 row, and also blanks and stretch glyphs inserted by
27529 extend_face_to_end_of_line. */
27530 while (end > glyph
27531 && INTEGERP ((end - 1)->object))
27532 --end;
27533 /* Scan the rest of the glyph row from the end, looking for the
27534 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27535 DISP_STRING, or whose position is between START_CHARPOS
27536 and END_CHARPOS */
27537 for (--end;
27538 end > glyph
27539 && !INTEGERP (end->object)
27540 && !EQ (end->object, disp_string)
27541 && !(BUFFERP (end->object)
27542 && (end->charpos >= start_charpos
27543 && end->charpos < end_charpos));
27544 --end)
27545 {
27546 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27547 are present at buffer positions between START_CHARPOS and
27548 END_CHARPOS, or if they come from an overlay. */
27549 if (EQ (end->object, before_string))
27550 {
27551 pos = string_buffer_position (before_string, start_charpos);
27552 if (!pos || (pos >= start_charpos && pos < end_charpos))
27553 break;
27554 }
27555 else if (EQ (end->object, after_string))
27556 {
27557 pos = string_buffer_position (after_string, end_charpos);
27558 if (!pos || (pos >= start_charpos && pos < end_charpos))
27559 break;
27560 }
27561 }
27562 /* Find the X coordinate of the last glyph to be highlighted. */
27563 for (; glyph <= end; ++glyph)
27564 x += glyph->pixel_width;
27565
27566 hlinfo->mouse_face_end_x = x;
27567 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27568 }
27569 else
27570 {
27571 /* Skip truncation and continuation glyphs near the end of the
27572 row, and also blanks and stretch glyphs inserted by
27573 extend_face_to_end_of_line. */
27574 x = r2->x;
27575 end++;
27576 while (end < glyph
27577 && INTEGERP (end->object))
27578 {
27579 x += end->pixel_width;
27580 ++end;
27581 }
27582 /* Scan the rest of the glyph row from the end, looking for the
27583 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27584 DISP_STRING, or whose position is between START_CHARPOS
27585 and END_CHARPOS */
27586 for ( ;
27587 end < glyph
27588 && !INTEGERP (end->object)
27589 && !EQ (end->object, disp_string)
27590 && !(BUFFERP (end->object)
27591 && (end->charpos >= start_charpos
27592 && end->charpos < end_charpos));
27593 ++end)
27594 {
27595 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27596 are present at buffer positions between START_CHARPOS and
27597 END_CHARPOS, or if they come from an overlay. */
27598 if (EQ (end->object, before_string))
27599 {
27600 pos = string_buffer_position (before_string, start_charpos);
27601 if (!pos || (pos >= start_charpos && pos < end_charpos))
27602 break;
27603 }
27604 else if (EQ (end->object, after_string))
27605 {
27606 pos = string_buffer_position (after_string, end_charpos);
27607 if (!pos || (pos >= start_charpos && pos < end_charpos))
27608 break;
27609 }
27610 x += end->pixel_width;
27611 }
27612 /* If we exited the above loop because we arrived at the last
27613 glyph of the row, and its buffer position is still not in
27614 range, it means the last character in range is the preceding
27615 newline. Bump the end column and x values to get past the
27616 last glyph. */
27617 if (end == glyph
27618 && BUFFERP (end->object)
27619 && (end->charpos < start_charpos
27620 || end->charpos >= end_charpos))
27621 {
27622 x += end->pixel_width;
27623 ++end;
27624 }
27625 hlinfo->mouse_face_end_x = x;
27626 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27627 }
27628
27629 hlinfo->mouse_face_window = window;
27630 hlinfo->mouse_face_face_id
27631 = face_at_buffer_position (w, mouse_charpos, &ignore,
27632 mouse_charpos + 1,
27633 !hlinfo->mouse_face_hidden, -1);
27634 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27635 }
27636
27637 /* The following function is not used anymore (replaced with
27638 mouse_face_from_string_pos), but I leave it here for the time
27639 being, in case someone would. */
27640
27641 #if 0 /* not used */
27642
27643 /* Find the position of the glyph for position POS in OBJECT in
27644 window W's current matrix, and return in *X, *Y the pixel
27645 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27646
27647 RIGHT_P non-zero means return the position of the right edge of the
27648 glyph, RIGHT_P zero means return the left edge position.
27649
27650 If no glyph for POS exists in the matrix, return the position of
27651 the glyph with the next smaller position that is in the matrix, if
27652 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27653 exists in the matrix, return the position of the glyph with the
27654 next larger position in OBJECT.
27655
27656 Value is non-zero if a glyph was found. */
27657
27658 static int
27659 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27660 int *hpos, int *vpos, int *x, int *y, int right_p)
27661 {
27662 int yb = window_text_bottom_y (w);
27663 struct glyph_row *r;
27664 struct glyph *best_glyph = NULL;
27665 struct glyph_row *best_row = NULL;
27666 int best_x = 0;
27667
27668 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27669 r->enabled_p && r->y < yb;
27670 ++r)
27671 {
27672 struct glyph *g = r->glyphs[TEXT_AREA];
27673 struct glyph *e = g + r->used[TEXT_AREA];
27674 int gx;
27675
27676 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27677 if (EQ (g->object, object))
27678 {
27679 if (g->charpos == pos)
27680 {
27681 best_glyph = g;
27682 best_x = gx;
27683 best_row = r;
27684 goto found;
27685 }
27686 else if (best_glyph == NULL
27687 || ((eabs (g->charpos - pos)
27688 < eabs (best_glyph->charpos - pos))
27689 && (right_p
27690 ? g->charpos < pos
27691 : g->charpos > pos)))
27692 {
27693 best_glyph = g;
27694 best_x = gx;
27695 best_row = r;
27696 }
27697 }
27698 }
27699
27700 found:
27701
27702 if (best_glyph)
27703 {
27704 *x = best_x;
27705 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27706
27707 if (right_p)
27708 {
27709 *x += best_glyph->pixel_width;
27710 ++*hpos;
27711 }
27712
27713 *y = best_row->y;
27714 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27715 }
27716
27717 return best_glyph != NULL;
27718 }
27719 #endif /* not used */
27720
27721 /* Find the positions of the first and the last glyphs in window W's
27722 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27723 (assumed to be a string), and return in HLINFO's mouse_face_*
27724 members the pixel and column/row coordinates of those glyphs. */
27725
27726 static void
27727 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27728 Lisp_Object object,
27729 ptrdiff_t startpos, ptrdiff_t endpos)
27730 {
27731 int yb = window_text_bottom_y (w);
27732 struct glyph_row *r;
27733 struct glyph *g, *e;
27734 int gx;
27735 int found = 0;
27736
27737 /* Find the glyph row with at least one position in the range
27738 [STARTPOS..ENDPOS), and the first glyph in that row whose
27739 position belongs to that range. */
27740 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27741 r->enabled_p && r->y < yb;
27742 ++r)
27743 {
27744 if (!r->reversed_p)
27745 {
27746 g = r->glyphs[TEXT_AREA];
27747 e = g + r->used[TEXT_AREA];
27748 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27749 if (EQ (g->object, object)
27750 && startpos <= g->charpos && g->charpos < endpos)
27751 {
27752 hlinfo->mouse_face_beg_row
27753 = MATRIX_ROW_VPOS (r, w->current_matrix);
27754 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27755 hlinfo->mouse_face_beg_x = gx;
27756 found = 1;
27757 break;
27758 }
27759 }
27760 else
27761 {
27762 struct glyph *g1;
27763
27764 e = r->glyphs[TEXT_AREA];
27765 g = e + r->used[TEXT_AREA];
27766 for ( ; g > e; --g)
27767 if (EQ ((g-1)->object, object)
27768 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27769 {
27770 hlinfo->mouse_face_beg_row
27771 = MATRIX_ROW_VPOS (r, w->current_matrix);
27772 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27773 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27774 gx += g1->pixel_width;
27775 hlinfo->mouse_face_beg_x = gx;
27776 found = 1;
27777 break;
27778 }
27779 }
27780 if (found)
27781 break;
27782 }
27783
27784 if (!found)
27785 return;
27786
27787 /* Starting with the next row, look for the first row which does NOT
27788 include any glyphs whose positions are in the range. */
27789 for (++r; r->enabled_p && r->y < yb; ++r)
27790 {
27791 g = r->glyphs[TEXT_AREA];
27792 e = g + r->used[TEXT_AREA];
27793 found = 0;
27794 for ( ; g < e; ++g)
27795 if (EQ (g->object, object)
27796 && startpos <= g->charpos && g->charpos < endpos)
27797 {
27798 found = 1;
27799 break;
27800 }
27801 if (!found)
27802 break;
27803 }
27804
27805 /* The highlighted region ends on the previous row. */
27806 r--;
27807
27808 /* Set the end row. */
27809 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27810
27811 /* Compute and set the end column and the end column's horizontal
27812 pixel coordinate. */
27813 if (!r->reversed_p)
27814 {
27815 g = r->glyphs[TEXT_AREA];
27816 e = g + r->used[TEXT_AREA];
27817 for ( ; e > g; --e)
27818 if (EQ ((e-1)->object, object)
27819 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27820 break;
27821 hlinfo->mouse_face_end_col = e - g;
27822
27823 for (gx = r->x; g < e; ++g)
27824 gx += g->pixel_width;
27825 hlinfo->mouse_face_end_x = gx;
27826 }
27827 else
27828 {
27829 e = r->glyphs[TEXT_AREA];
27830 g = e + r->used[TEXT_AREA];
27831 for (gx = r->x ; e < g; ++e)
27832 {
27833 if (EQ (e->object, object)
27834 && startpos <= e->charpos && e->charpos < endpos)
27835 break;
27836 gx += e->pixel_width;
27837 }
27838 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27839 hlinfo->mouse_face_end_x = gx;
27840 }
27841 }
27842
27843 #ifdef HAVE_WINDOW_SYSTEM
27844
27845 /* See if position X, Y is within a hot-spot of an image. */
27846
27847 static int
27848 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27849 {
27850 if (!CONSP (hot_spot))
27851 return 0;
27852
27853 if (EQ (XCAR (hot_spot), Qrect))
27854 {
27855 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27856 Lisp_Object rect = XCDR (hot_spot);
27857 Lisp_Object tem;
27858 if (!CONSP (rect))
27859 return 0;
27860 if (!CONSP (XCAR (rect)))
27861 return 0;
27862 if (!CONSP (XCDR (rect)))
27863 return 0;
27864 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27865 return 0;
27866 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27867 return 0;
27868 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27869 return 0;
27870 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27871 return 0;
27872 return 1;
27873 }
27874 else if (EQ (XCAR (hot_spot), Qcircle))
27875 {
27876 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27877 Lisp_Object circ = XCDR (hot_spot);
27878 Lisp_Object lr, lx0, ly0;
27879 if (CONSP (circ)
27880 && CONSP (XCAR (circ))
27881 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27882 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27883 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27884 {
27885 double r = XFLOATINT (lr);
27886 double dx = XINT (lx0) - x;
27887 double dy = XINT (ly0) - y;
27888 return (dx * dx + dy * dy <= r * r);
27889 }
27890 }
27891 else if (EQ (XCAR (hot_spot), Qpoly))
27892 {
27893 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27894 if (VECTORP (XCDR (hot_spot)))
27895 {
27896 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27897 Lisp_Object *poly = v->contents;
27898 ptrdiff_t n = v->header.size;
27899 ptrdiff_t i;
27900 int inside = 0;
27901 Lisp_Object lx, ly;
27902 int x0, y0;
27903
27904 /* Need an even number of coordinates, and at least 3 edges. */
27905 if (n < 6 || n & 1)
27906 return 0;
27907
27908 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27909 If count is odd, we are inside polygon. Pixels on edges
27910 may or may not be included depending on actual geometry of the
27911 polygon. */
27912 if ((lx = poly[n-2], !INTEGERP (lx))
27913 || (ly = poly[n-1], !INTEGERP (lx)))
27914 return 0;
27915 x0 = XINT (lx), y0 = XINT (ly);
27916 for (i = 0; i < n; i += 2)
27917 {
27918 int x1 = x0, y1 = y0;
27919 if ((lx = poly[i], !INTEGERP (lx))
27920 || (ly = poly[i+1], !INTEGERP (ly)))
27921 return 0;
27922 x0 = XINT (lx), y0 = XINT (ly);
27923
27924 /* Does this segment cross the X line? */
27925 if (x0 >= x)
27926 {
27927 if (x1 >= x)
27928 continue;
27929 }
27930 else if (x1 < x)
27931 continue;
27932 if (y > y0 && y > y1)
27933 continue;
27934 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27935 inside = !inside;
27936 }
27937 return inside;
27938 }
27939 }
27940 return 0;
27941 }
27942
27943 Lisp_Object
27944 find_hot_spot (Lisp_Object map, int x, int y)
27945 {
27946 while (CONSP (map))
27947 {
27948 if (CONSP (XCAR (map))
27949 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27950 return XCAR (map);
27951 map = XCDR (map);
27952 }
27953
27954 return Qnil;
27955 }
27956
27957 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27958 3, 3, 0,
27959 doc: /* Lookup in image map MAP coordinates X and Y.
27960 An image map is an alist where each element has the format (AREA ID PLIST).
27961 An AREA is specified as either a rectangle, a circle, or a polygon:
27962 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27963 pixel coordinates of the upper left and bottom right corners.
27964 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27965 and the radius of the circle; r may be a float or integer.
27966 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27967 vector describes one corner in the polygon.
27968 Returns the alist element for the first matching AREA in MAP. */)
27969 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27970 {
27971 if (NILP (map))
27972 return Qnil;
27973
27974 CHECK_NUMBER (x);
27975 CHECK_NUMBER (y);
27976
27977 return find_hot_spot (map,
27978 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27979 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27980 }
27981
27982
27983 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27984 static void
27985 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27986 {
27987 /* Do not change cursor shape while dragging mouse. */
27988 if (!NILP (do_mouse_tracking))
27989 return;
27990
27991 if (!NILP (pointer))
27992 {
27993 if (EQ (pointer, Qarrow))
27994 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27995 else if (EQ (pointer, Qhand))
27996 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27997 else if (EQ (pointer, Qtext))
27998 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27999 else if (EQ (pointer, intern ("hdrag")))
28000 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28001 else if (EQ (pointer, intern ("nhdrag")))
28002 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28003 #ifdef HAVE_X_WINDOWS
28004 else if (EQ (pointer, intern ("vdrag")))
28005 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28006 #endif
28007 else if (EQ (pointer, intern ("hourglass")))
28008 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28009 else if (EQ (pointer, Qmodeline))
28010 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28011 else
28012 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28013 }
28014
28015 if (cursor != No_Cursor)
28016 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28017 }
28018
28019 #endif /* HAVE_WINDOW_SYSTEM */
28020
28021 /* Take proper action when mouse has moved to the mode or header line
28022 or marginal area AREA of window W, x-position X and y-position Y.
28023 X is relative to the start of the text display area of W, so the
28024 width of bitmap areas and scroll bars must be subtracted to get a
28025 position relative to the start of the mode line. */
28026
28027 static void
28028 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28029 enum window_part area)
28030 {
28031 struct window *w = XWINDOW (window);
28032 struct frame *f = XFRAME (w->frame);
28033 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28034 #ifdef HAVE_WINDOW_SYSTEM
28035 Display_Info *dpyinfo;
28036 #endif
28037 Cursor cursor = No_Cursor;
28038 Lisp_Object pointer = Qnil;
28039 int dx, dy, width, height;
28040 ptrdiff_t charpos;
28041 Lisp_Object string, object = Qnil;
28042 Lisp_Object pos IF_LINT (= Qnil), help;
28043
28044 Lisp_Object mouse_face;
28045 int original_x_pixel = x;
28046 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28047 struct glyph_row *row IF_LINT (= 0);
28048
28049 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28050 {
28051 int x0;
28052 struct glyph *end;
28053
28054 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28055 returns them in row/column units! */
28056 string = mode_line_string (w, area, &x, &y, &charpos,
28057 &object, &dx, &dy, &width, &height);
28058
28059 row = (area == ON_MODE_LINE
28060 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28061 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28062
28063 /* Find the glyph under the mouse pointer. */
28064 if (row->mode_line_p && row->enabled_p)
28065 {
28066 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28067 end = glyph + row->used[TEXT_AREA];
28068
28069 for (x0 = original_x_pixel;
28070 glyph < end && x0 >= glyph->pixel_width;
28071 ++glyph)
28072 x0 -= glyph->pixel_width;
28073
28074 if (glyph >= end)
28075 glyph = NULL;
28076 }
28077 }
28078 else
28079 {
28080 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28081 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28082 returns them in row/column units! */
28083 string = marginal_area_string (w, area, &x, &y, &charpos,
28084 &object, &dx, &dy, &width, &height);
28085 }
28086
28087 help = Qnil;
28088
28089 #ifdef HAVE_WINDOW_SYSTEM
28090 if (IMAGEP (object))
28091 {
28092 Lisp_Object image_map, hotspot;
28093 if ((image_map = Fplist_get (XCDR (object), QCmap),
28094 !NILP (image_map))
28095 && (hotspot = find_hot_spot (image_map, dx, dy),
28096 CONSP (hotspot))
28097 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28098 {
28099 Lisp_Object plist;
28100
28101 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28102 If so, we could look for mouse-enter, mouse-leave
28103 properties in PLIST (and do something...). */
28104 hotspot = XCDR (hotspot);
28105 if (CONSP (hotspot)
28106 && (plist = XCAR (hotspot), CONSP (plist)))
28107 {
28108 pointer = Fplist_get (plist, Qpointer);
28109 if (NILP (pointer))
28110 pointer = Qhand;
28111 help = Fplist_get (plist, Qhelp_echo);
28112 if (!NILP (help))
28113 {
28114 help_echo_string = help;
28115 XSETWINDOW (help_echo_window, w);
28116 help_echo_object = w->contents;
28117 help_echo_pos = charpos;
28118 }
28119 }
28120 }
28121 if (NILP (pointer))
28122 pointer = Fplist_get (XCDR (object), QCpointer);
28123 }
28124 #endif /* HAVE_WINDOW_SYSTEM */
28125
28126 if (STRINGP (string))
28127 pos = make_number (charpos);
28128
28129 /* Set the help text and mouse pointer. If the mouse is on a part
28130 of the mode line without any text (e.g. past the right edge of
28131 the mode line text), use the default help text and pointer. */
28132 if (STRINGP (string) || area == ON_MODE_LINE)
28133 {
28134 /* Arrange to display the help by setting the global variables
28135 help_echo_string, help_echo_object, and help_echo_pos. */
28136 if (NILP (help))
28137 {
28138 if (STRINGP (string))
28139 help = Fget_text_property (pos, Qhelp_echo, string);
28140
28141 if (!NILP (help))
28142 {
28143 help_echo_string = help;
28144 XSETWINDOW (help_echo_window, w);
28145 help_echo_object = string;
28146 help_echo_pos = charpos;
28147 }
28148 else if (area == ON_MODE_LINE)
28149 {
28150 Lisp_Object default_help
28151 = buffer_local_value_1 (Qmode_line_default_help_echo,
28152 w->contents);
28153
28154 if (STRINGP (default_help))
28155 {
28156 help_echo_string = default_help;
28157 XSETWINDOW (help_echo_window, w);
28158 help_echo_object = Qnil;
28159 help_echo_pos = -1;
28160 }
28161 }
28162 }
28163
28164 #ifdef HAVE_WINDOW_SYSTEM
28165 /* Change the mouse pointer according to what is under it. */
28166 if (FRAME_WINDOW_P (f))
28167 {
28168 dpyinfo = FRAME_DISPLAY_INFO (f);
28169 if (STRINGP (string))
28170 {
28171 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28172
28173 if (NILP (pointer))
28174 pointer = Fget_text_property (pos, Qpointer, string);
28175
28176 /* Change the mouse pointer according to what is under X/Y. */
28177 if (NILP (pointer)
28178 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28179 {
28180 Lisp_Object map;
28181 map = Fget_text_property (pos, Qlocal_map, string);
28182 if (!KEYMAPP (map))
28183 map = Fget_text_property (pos, Qkeymap, string);
28184 if (!KEYMAPP (map))
28185 cursor = dpyinfo->vertical_scroll_bar_cursor;
28186 }
28187 }
28188 else
28189 /* Default mode-line pointer. */
28190 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28191 }
28192 #endif
28193 }
28194
28195 /* Change the mouse face according to what is under X/Y. */
28196 if (STRINGP (string))
28197 {
28198 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28199 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28200 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28201 && glyph)
28202 {
28203 Lisp_Object b, e;
28204
28205 struct glyph * tmp_glyph;
28206
28207 int gpos;
28208 int gseq_length;
28209 int total_pixel_width;
28210 ptrdiff_t begpos, endpos, ignore;
28211
28212 int vpos, hpos;
28213
28214 b = Fprevious_single_property_change (make_number (charpos + 1),
28215 Qmouse_face, string, Qnil);
28216 if (NILP (b))
28217 begpos = 0;
28218 else
28219 begpos = XINT (b);
28220
28221 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28222 if (NILP (e))
28223 endpos = SCHARS (string);
28224 else
28225 endpos = XINT (e);
28226
28227 /* Calculate the glyph position GPOS of GLYPH in the
28228 displayed string, relative to the beginning of the
28229 highlighted part of the string.
28230
28231 Note: GPOS is different from CHARPOS. CHARPOS is the
28232 position of GLYPH in the internal string object. A mode
28233 line string format has structures which are converted to
28234 a flattened string by the Emacs Lisp interpreter. The
28235 internal string is an element of those structures. The
28236 displayed string is the flattened string. */
28237 tmp_glyph = row_start_glyph;
28238 while (tmp_glyph < glyph
28239 && (!(EQ (tmp_glyph->object, glyph->object)
28240 && begpos <= tmp_glyph->charpos
28241 && tmp_glyph->charpos < endpos)))
28242 tmp_glyph++;
28243 gpos = glyph - tmp_glyph;
28244
28245 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28246 the highlighted part of the displayed string to which
28247 GLYPH belongs. Note: GSEQ_LENGTH is different from
28248 SCHARS (STRING), because the latter returns the length of
28249 the internal string. */
28250 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28251 tmp_glyph > glyph
28252 && (!(EQ (tmp_glyph->object, glyph->object)
28253 && begpos <= tmp_glyph->charpos
28254 && tmp_glyph->charpos < endpos));
28255 tmp_glyph--)
28256 ;
28257 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28258
28259 /* Calculate the total pixel width of all the glyphs between
28260 the beginning of the highlighted area and GLYPH. */
28261 total_pixel_width = 0;
28262 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28263 total_pixel_width += tmp_glyph->pixel_width;
28264
28265 /* Pre calculation of re-rendering position. Note: X is in
28266 column units here, after the call to mode_line_string or
28267 marginal_area_string. */
28268 hpos = x - gpos;
28269 vpos = (area == ON_MODE_LINE
28270 ? (w->current_matrix)->nrows - 1
28271 : 0);
28272
28273 /* If GLYPH's position is included in the region that is
28274 already drawn in mouse face, we have nothing to do. */
28275 if ( EQ (window, hlinfo->mouse_face_window)
28276 && (!row->reversed_p
28277 ? (hlinfo->mouse_face_beg_col <= hpos
28278 && hpos < hlinfo->mouse_face_end_col)
28279 /* In R2L rows we swap BEG and END, see below. */
28280 : (hlinfo->mouse_face_end_col <= hpos
28281 && hpos < hlinfo->mouse_face_beg_col))
28282 && hlinfo->mouse_face_beg_row == vpos )
28283 return;
28284
28285 if (clear_mouse_face (hlinfo))
28286 cursor = No_Cursor;
28287
28288 if (!row->reversed_p)
28289 {
28290 hlinfo->mouse_face_beg_col = hpos;
28291 hlinfo->mouse_face_beg_x = original_x_pixel
28292 - (total_pixel_width + dx);
28293 hlinfo->mouse_face_end_col = hpos + gseq_length;
28294 hlinfo->mouse_face_end_x = 0;
28295 }
28296 else
28297 {
28298 /* In R2L rows, show_mouse_face expects BEG and END
28299 coordinates to be swapped. */
28300 hlinfo->mouse_face_end_col = hpos;
28301 hlinfo->mouse_face_end_x = original_x_pixel
28302 - (total_pixel_width + dx);
28303 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28304 hlinfo->mouse_face_beg_x = 0;
28305 }
28306
28307 hlinfo->mouse_face_beg_row = vpos;
28308 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28309 hlinfo->mouse_face_past_end = 0;
28310 hlinfo->mouse_face_window = window;
28311
28312 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28313 charpos,
28314 0, &ignore,
28315 glyph->face_id,
28316 1);
28317 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28318
28319 if (NILP (pointer))
28320 pointer = Qhand;
28321 }
28322 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28323 clear_mouse_face (hlinfo);
28324 }
28325 #ifdef HAVE_WINDOW_SYSTEM
28326 if (FRAME_WINDOW_P (f))
28327 define_frame_cursor1 (f, cursor, pointer);
28328 #endif
28329 }
28330
28331
28332 /* EXPORT:
28333 Take proper action when the mouse has moved to position X, Y on
28334 frame F with regards to highlighting portions of display that have
28335 mouse-face properties. Also de-highlight portions of display where
28336 the mouse was before, set the mouse pointer shape as appropriate
28337 for the mouse coordinates, and activate help echo (tooltips).
28338 X and Y can be negative or out of range. */
28339
28340 void
28341 note_mouse_highlight (struct frame *f, int x, int y)
28342 {
28343 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28344 enum window_part part = ON_NOTHING;
28345 Lisp_Object window;
28346 struct window *w;
28347 Cursor cursor = No_Cursor;
28348 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28349 struct buffer *b;
28350
28351 /* When a menu is active, don't highlight because this looks odd. */
28352 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28353 if (popup_activated ())
28354 return;
28355 #endif
28356
28357 if (!f->glyphs_initialized_p
28358 || f->pointer_invisible)
28359 return;
28360
28361 hlinfo->mouse_face_mouse_x = x;
28362 hlinfo->mouse_face_mouse_y = y;
28363 hlinfo->mouse_face_mouse_frame = f;
28364
28365 if (hlinfo->mouse_face_defer)
28366 return;
28367
28368 /* Which window is that in? */
28369 window = window_from_coordinates (f, x, y, &part, 1);
28370
28371 /* If displaying active text in another window, clear that. */
28372 if (! EQ (window, hlinfo->mouse_face_window)
28373 /* Also clear if we move out of text area in same window. */
28374 || (!NILP (hlinfo->mouse_face_window)
28375 && !NILP (window)
28376 && part != ON_TEXT
28377 && part != ON_MODE_LINE
28378 && part != ON_HEADER_LINE))
28379 clear_mouse_face (hlinfo);
28380
28381 /* Not on a window -> return. */
28382 if (!WINDOWP (window))
28383 return;
28384
28385 /* Reset help_echo_string. It will get recomputed below. */
28386 help_echo_string = Qnil;
28387
28388 /* Convert to window-relative pixel coordinates. */
28389 w = XWINDOW (window);
28390 frame_to_window_pixel_xy (w, &x, &y);
28391
28392 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28393 /* Handle tool-bar window differently since it doesn't display a
28394 buffer. */
28395 if (EQ (window, f->tool_bar_window))
28396 {
28397 note_tool_bar_highlight (f, x, y);
28398 return;
28399 }
28400 #endif
28401
28402 /* Mouse is on the mode, header line or margin? */
28403 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28404 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28405 {
28406 note_mode_line_or_margin_highlight (window, x, y, part);
28407 return;
28408 }
28409
28410 #ifdef HAVE_WINDOW_SYSTEM
28411 if (part == ON_VERTICAL_BORDER)
28412 {
28413 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28414 help_echo_string = build_string ("drag-mouse-1: resize");
28415 }
28416 else if (part == ON_RIGHT_DIVIDER)
28417 {
28418 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28419 help_echo_string = build_string ("drag-mouse-1: resize");
28420 }
28421 else if (part == ON_BOTTOM_DIVIDER)
28422 {
28423 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28424 help_echo_string = build_string ("drag-mouse-1: resize");
28425 }
28426 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28427 || part == ON_SCROLL_BAR)
28428 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28429 else
28430 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28431 #endif
28432
28433 /* Are we in a window whose display is up to date?
28434 And verify the buffer's text has not changed. */
28435 b = XBUFFER (w->contents);
28436 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28437 {
28438 int hpos, vpos, dx, dy, area = LAST_AREA;
28439 ptrdiff_t pos;
28440 struct glyph *glyph;
28441 Lisp_Object object;
28442 Lisp_Object mouse_face = Qnil, position;
28443 Lisp_Object *overlay_vec = NULL;
28444 ptrdiff_t i, noverlays;
28445 struct buffer *obuf;
28446 ptrdiff_t obegv, ozv;
28447 int same_region;
28448
28449 /* Find the glyph under X/Y. */
28450 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28451
28452 #ifdef HAVE_WINDOW_SYSTEM
28453 /* Look for :pointer property on image. */
28454 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28455 {
28456 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28457 if (img != NULL && IMAGEP (img->spec))
28458 {
28459 Lisp_Object image_map, hotspot;
28460 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28461 !NILP (image_map))
28462 && (hotspot = find_hot_spot (image_map,
28463 glyph->slice.img.x + dx,
28464 glyph->slice.img.y + dy),
28465 CONSP (hotspot))
28466 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28467 {
28468 Lisp_Object plist;
28469
28470 /* Could check XCAR (hotspot) to see if we enter/leave
28471 this hot-spot.
28472 If so, we could look for mouse-enter, mouse-leave
28473 properties in PLIST (and do something...). */
28474 hotspot = XCDR (hotspot);
28475 if (CONSP (hotspot)
28476 && (plist = XCAR (hotspot), CONSP (plist)))
28477 {
28478 pointer = Fplist_get (plist, Qpointer);
28479 if (NILP (pointer))
28480 pointer = Qhand;
28481 help_echo_string = Fplist_get (plist, Qhelp_echo);
28482 if (!NILP (help_echo_string))
28483 {
28484 help_echo_window = window;
28485 help_echo_object = glyph->object;
28486 help_echo_pos = glyph->charpos;
28487 }
28488 }
28489 }
28490 if (NILP (pointer))
28491 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28492 }
28493 }
28494 #endif /* HAVE_WINDOW_SYSTEM */
28495
28496 /* Clear mouse face if X/Y not over text. */
28497 if (glyph == NULL
28498 || area != TEXT_AREA
28499 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28500 /* Glyph's OBJECT is an integer for glyphs inserted by the
28501 display engine for its internal purposes, like truncation
28502 and continuation glyphs and blanks beyond the end of
28503 line's text on text terminals. If we are over such a
28504 glyph, we are not over any text. */
28505 || INTEGERP (glyph->object)
28506 /* R2L rows have a stretch glyph at their front, which
28507 stands for no text, whereas L2R rows have no glyphs at
28508 all beyond the end of text. Treat such stretch glyphs
28509 like we do with NULL glyphs in L2R rows. */
28510 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28511 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28512 && glyph->type == STRETCH_GLYPH
28513 && glyph->avoid_cursor_p))
28514 {
28515 if (clear_mouse_face (hlinfo))
28516 cursor = No_Cursor;
28517 #ifdef HAVE_WINDOW_SYSTEM
28518 if (FRAME_WINDOW_P (f) && NILP (pointer))
28519 {
28520 if (area != TEXT_AREA)
28521 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28522 else
28523 pointer = Vvoid_text_area_pointer;
28524 }
28525 #endif
28526 goto set_cursor;
28527 }
28528
28529 pos = glyph->charpos;
28530 object = glyph->object;
28531 if (!STRINGP (object) && !BUFFERP (object))
28532 goto set_cursor;
28533
28534 /* If we get an out-of-range value, return now; avoid an error. */
28535 if (BUFFERP (object) && pos > BUF_Z (b))
28536 goto set_cursor;
28537
28538 /* Make the window's buffer temporarily current for
28539 overlays_at and compute_char_face. */
28540 obuf = current_buffer;
28541 current_buffer = b;
28542 obegv = BEGV;
28543 ozv = ZV;
28544 BEGV = BEG;
28545 ZV = Z;
28546
28547 /* Is this char mouse-active or does it have help-echo? */
28548 position = make_number (pos);
28549
28550 if (BUFFERP (object))
28551 {
28552 /* Put all the overlays we want in a vector in overlay_vec. */
28553 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28554 /* Sort overlays into increasing priority order. */
28555 noverlays = sort_overlays (overlay_vec, noverlays, w);
28556 }
28557 else
28558 noverlays = 0;
28559
28560 if (NILP (Vmouse_highlight))
28561 {
28562 clear_mouse_face (hlinfo);
28563 goto check_help_echo;
28564 }
28565
28566 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28567
28568 if (same_region)
28569 cursor = No_Cursor;
28570
28571 /* Check mouse-face highlighting. */
28572 if (! same_region
28573 /* If there exists an overlay with mouse-face overlapping
28574 the one we are currently highlighting, we have to
28575 check if we enter the overlapping overlay, and then
28576 highlight only that. */
28577 || (OVERLAYP (hlinfo->mouse_face_overlay)
28578 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28579 {
28580 /* Find the highest priority overlay with a mouse-face. */
28581 Lisp_Object overlay = Qnil;
28582 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28583 {
28584 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28585 if (!NILP (mouse_face))
28586 overlay = overlay_vec[i];
28587 }
28588
28589 /* If we're highlighting the same overlay as before, there's
28590 no need to do that again. */
28591 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28592 goto check_help_echo;
28593 hlinfo->mouse_face_overlay = overlay;
28594
28595 /* Clear the display of the old active region, if any. */
28596 if (clear_mouse_face (hlinfo))
28597 cursor = No_Cursor;
28598
28599 /* If no overlay applies, get a text property. */
28600 if (NILP (overlay))
28601 mouse_face = Fget_text_property (position, Qmouse_face, object);
28602
28603 /* Next, compute the bounds of the mouse highlighting and
28604 display it. */
28605 if (!NILP (mouse_face) && STRINGP (object))
28606 {
28607 /* The mouse-highlighting comes from a display string
28608 with a mouse-face. */
28609 Lisp_Object s, e;
28610 ptrdiff_t ignore;
28611
28612 s = Fprevious_single_property_change
28613 (make_number (pos + 1), Qmouse_face, object, Qnil);
28614 e = Fnext_single_property_change
28615 (position, Qmouse_face, object, Qnil);
28616 if (NILP (s))
28617 s = make_number (0);
28618 if (NILP (e))
28619 e = make_number (SCHARS (object));
28620 mouse_face_from_string_pos (w, hlinfo, object,
28621 XINT (s), XINT (e));
28622 hlinfo->mouse_face_past_end = 0;
28623 hlinfo->mouse_face_window = window;
28624 hlinfo->mouse_face_face_id
28625 = face_at_string_position (w, object, pos, 0, &ignore,
28626 glyph->face_id, 1);
28627 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28628 cursor = No_Cursor;
28629 }
28630 else
28631 {
28632 /* The mouse-highlighting, if any, comes from an overlay
28633 or text property in the buffer. */
28634 Lisp_Object buffer IF_LINT (= Qnil);
28635 Lisp_Object disp_string IF_LINT (= Qnil);
28636
28637 if (STRINGP (object))
28638 {
28639 /* If we are on a display string with no mouse-face,
28640 check if the text under it has one. */
28641 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28642 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28643 pos = string_buffer_position (object, start);
28644 if (pos > 0)
28645 {
28646 mouse_face = get_char_property_and_overlay
28647 (make_number (pos), Qmouse_face, w->contents, &overlay);
28648 buffer = w->contents;
28649 disp_string = object;
28650 }
28651 }
28652 else
28653 {
28654 buffer = object;
28655 disp_string = Qnil;
28656 }
28657
28658 if (!NILP (mouse_face))
28659 {
28660 Lisp_Object before, after;
28661 Lisp_Object before_string, after_string;
28662 /* To correctly find the limits of mouse highlight
28663 in a bidi-reordered buffer, we must not use the
28664 optimization of limiting the search in
28665 previous-single-property-change and
28666 next-single-property-change, because
28667 rows_from_pos_range needs the real start and end
28668 positions to DTRT in this case. That's because
28669 the first row visible in a window does not
28670 necessarily display the character whose position
28671 is the smallest. */
28672 Lisp_Object lim1
28673 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28674 ? Fmarker_position (w->start)
28675 : Qnil;
28676 Lisp_Object lim2
28677 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28678 ? make_number (BUF_Z (XBUFFER (buffer))
28679 - w->window_end_pos)
28680 : Qnil;
28681
28682 if (NILP (overlay))
28683 {
28684 /* Handle the text property case. */
28685 before = Fprevious_single_property_change
28686 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28687 after = Fnext_single_property_change
28688 (make_number (pos), Qmouse_face, buffer, lim2);
28689 before_string = after_string = Qnil;
28690 }
28691 else
28692 {
28693 /* Handle the overlay case. */
28694 before = Foverlay_start (overlay);
28695 after = Foverlay_end (overlay);
28696 before_string = Foverlay_get (overlay, Qbefore_string);
28697 after_string = Foverlay_get (overlay, Qafter_string);
28698
28699 if (!STRINGP (before_string)) before_string = Qnil;
28700 if (!STRINGP (after_string)) after_string = Qnil;
28701 }
28702
28703 mouse_face_from_buffer_pos (window, hlinfo, pos,
28704 NILP (before)
28705 ? 1
28706 : XFASTINT (before),
28707 NILP (after)
28708 ? BUF_Z (XBUFFER (buffer))
28709 : XFASTINT (after),
28710 before_string, after_string,
28711 disp_string);
28712 cursor = No_Cursor;
28713 }
28714 }
28715 }
28716
28717 check_help_echo:
28718
28719 /* Look for a `help-echo' property. */
28720 if (NILP (help_echo_string)) {
28721 Lisp_Object help, overlay;
28722
28723 /* Check overlays first. */
28724 help = overlay = Qnil;
28725 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28726 {
28727 overlay = overlay_vec[i];
28728 help = Foverlay_get (overlay, Qhelp_echo);
28729 }
28730
28731 if (!NILP (help))
28732 {
28733 help_echo_string = help;
28734 help_echo_window = window;
28735 help_echo_object = overlay;
28736 help_echo_pos = pos;
28737 }
28738 else
28739 {
28740 Lisp_Object obj = glyph->object;
28741 ptrdiff_t charpos = glyph->charpos;
28742
28743 /* Try text properties. */
28744 if (STRINGP (obj)
28745 && charpos >= 0
28746 && charpos < SCHARS (obj))
28747 {
28748 help = Fget_text_property (make_number (charpos),
28749 Qhelp_echo, obj);
28750 if (NILP (help))
28751 {
28752 /* If the string itself doesn't specify a help-echo,
28753 see if the buffer text ``under'' it does. */
28754 struct glyph_row *r
28755 = MATRIX_ROW (w->current_matrix, vpos);
28756 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28757 ptrdiff_t p = string_buffer_position (obj, start);
28758 if (p > 0)
28759 {
28760 help = Fget_char_property (make_number (p),
28761 Qhelp_echo, w->contents);
28762 if (!NILP (help))
28763 {
28764 charpos = p;
28765 obj = w->contents;
28766 }
28767 }
28768 }
28769 }
28770 else if (BUFFERP (obj)
28771 && charpos >= BEGV
28772 && charpos < ZV)
28773 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28774 obj);
28775
28776 if (!NILP (help))
28777 {
28778 help_echo_string = help;
28779 help_echo_window = window;
28780 help_echo_object = obj;
28781 help_echo_pos = charpos;
28782 }
28783 }
28784 }
28785
28786 #ifdef HAVE_WINDOW_SYSTEM
28787 /* Look for a `pointer' property. */
28788 if (FRAME_WINDOW_P (f) && NILP (pointer))
28789 {
28790 /* Check overlays first. */
28791 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28792 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28793
28794 if (NILP (pointer))
28795 {
28796 Lisp_Object obj = glyph->object;
28797 ptrdiff_t charpos = glyph->charpos;
28798
28799 /* Try text properties. */
28800 if (STRINGP (obj)
28801 && charpos >= 0
28802 && charpos < SCHARS (obj))
28803 {
28804 pointer = Fget_text_property (make_number (charpos),
28805 Qpointer, obj);
28806 if (NILP (pointer))
28807 {
28808 /* If the string itself doesn't specify a pointer,
28809 see if the buffer text ``under'' it does. */
28810 struct glyph_row *r
28811 = MATRIX_ROW (w->current_matrix, vpos);
28812 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28813 ptrdiff_t p = string_buffer_position (obj, start);
28814 if (p > 0)
28815 pointer = Fget_char_property (make_number (p),
28816 Qpointer, w->contents);
28817 }
28818 }
28819 else if (BUFFERP (obj)
28820 && charpos >= BEGV
28821 && charpos < ZV)
28822 pointer = Fget_text_property (make_number (charpos),
28823 Qpointer, obj);
28824 }
28825 }
28826 #endif /* HAVE_WINDOW_SYSTEM */
28827
28828 BEGV = obegv;
28829 ZV = ozv;
28830 current_buffer = obuf;
28831 }
28832
28833 set_cursor:
28834
28835 #ifdef HAVE_WINDOW_SYSTEM
28836 if (FRAME_WINDOW_P (f))
28837 define_frame_cursor1 (f, cursor, pointer);
28838 #else
28839 /* This is here to prevent a compiler error, about "label at end of
28840 compound statement". */
28841 return;
28842 #endif
28843 }
28844
28845
28846 /* EXPORT for RIF:
28847 Clear any mouse-face on window W. This function is part of the
28848 redisplay interface, and is called from try_window_id and similar
28849 functions to ensure the mouse-highlight is off. */
28850
28851 void
28852 x_clear_window_mouse_face (struct window *w)
28853 {
28854 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28855 Lisp_Object window;
28856
28857 block_input ();
28858 XSETWINDOW (window, w);
28859 if (EQ (window, hlinfo->mouse_face_window))
28860 clear_mouse_face (hlinfo);
28861 unblock_input ();
28862 }
28863
28864
28865 /* EXPORT:
28866 Just discard the mouse face information for frame F, if any.
28867 This is used when the size of F is changed. */
28868
28869 void
28870 cancel_mouse_face (struct frame *f)
28871 {
28872 Lisp_Object window;
28873 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28874
28875 window = hlinfo->mouse_face_window;
28876 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28877 reset_mouse_highlight (hlinfo);
28878 }
28879
28880
28881 \f
28882 /***********************************************************************
28883 Exposure Events
28884 ***********************************************************************/
28885
28886 #ifdef HAVE_WINDOW_SYSTEM
28887
28888 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28889 which intersects rectangle R. R is in window-relative coordinates. */
28890
28891 static void
28892 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28893 enum glyph_row_area area)
28894 {
28895 struct glyph *first = row->glyphs[area];
28896 struct glyph *end = row->glyphs[area] + row->used[area];
28897 struct glyph *last;
28898 int first_x, start_x, x;
28899
28900 if (area == TEXT_AREA && row->fill_line_p)
28901 /* If row extends face to end of line write the whole line. */
28902 draw_glyphs (w, 0, row, area,
28903 0, row->used[area],
28904 DRAW_NORMAL_TEXT, 0);
28905 else
28906 {
28907 /* Set START_X to the window-relative start position for drawing glyphs of
28908 AREA. The first glyph of the text area can be partially visible.
28909 The first glyphs of other areas cannot. */
28910 start_x = window_box_left_offset (w, area);
28911 x = start_x;
28912 if (area == TEXT_AREA)
28913 x += row->x;
28914
28915 /* Find the first glyph that must be redrawn. */
28916 while (first < end
28917 && x + first->pixel_width < r->x)
28918 {
28919 x += first->pixel_width;
28920 ++first;
28921 }
28922
28923 /* Find the last one. */
28924 last = first;
28925 first_x = x;
28926 while (last < end
28927 && x < r->x + r->width)
28928 {
28929 x += last->pixel_width;
28930 ++last;
28931 }
28932
28933 /* Repaint. */
28934 if (last > first)
28935 draw_glyphs (w, first_x - start_x, row, area,
28936 first - row->glyphs[area], last - row->glyphs[area],
28937 DRAW_NORMAL_TEXT, 0);
28938 }
28939 }
28940
28941
28942 /* Redraw the parts of the glyph row ROW on window W intersecting
28943 rectangle R. R is in window-relative coordinates. Value is
28944 non-zero if mouse-face was overwritten. */
28945
28946 static int
28947 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28948 {
28949 eassert (row->enabled_p);
28950
28951 if (row->mode_line_p || w->pseudo_window_p)
28952 draw_glyphs (w, 0, row, TEXT_AREA,
28953 0, row->used[TEXT_AREA],
28954 DRAW_NORMAL_TEXT, 0);
28955 else
28956 {
28957 if (row->used[LEFT_MARGIN_AREA])
28958 expose_area (w, row, r, LEFT_MARGIN_AREA);
28959 if (row->used[TEXT_AREA])
28960 expose_area (w, row, r, TEXT_AREA);
28961 if (row->used[RIGHT_MARGIN_AREA])
28962 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28963 draw_row_fringe_bitmaps (w, row);
28964 }
28965
28966 return row->mouse_face_p;
28967 }
28968
28969
28970 /* Redraw those parts of glyphs rows during expose event handling that
28971 overlap other rows. Redrawing of an exposed line writes over parts
28972 of lines overlapping that exposed line; this function fixes that.
28973
28974 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28975 row in W's current matrix that is exposed and overlaps other rows.
28976 LAST_OVERLAPPING_ROW is the last such row. */
28977
28978 static void
28979 expose_overlaps (struct window *w,
28980 struct glyph_row *first_overlapping_row,
28981 struct glyph_row *last_overlapping_row,
28982 XRectangle *r)
28983 {
28984 struct glyph_row *row;
28985
28986 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28987 if (row->overlapping_p)
28988 {
28989 eassert (row->enabled_p && !row->mode_line_p);
28990
28991 row->clip = r;
28992 if (row->used[LEFT_MARGIN_AREA])
28993 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28994
28995 if (row->used[TEXT_AREA])
28996 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28997
28998 if (row->used[RIGHT_MARGIN_AREA])
28999 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29000 row->clip = NULL;
29001 }
29002 }
29003
29004
29005 /* Return non-zero if W's cursor intersects rectangle R. */
29006
29007 static int
29008 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29009 {
29010 XRectangle cr, result;
29011 struct glyph *cursor_glyph;
29012 struct glyph_row *row;
29013
29014 if (w->phys_cursor.vpos >= 0
29015 && w->phys_cursor.vpos < w->current_matrix->nrows
29016 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29017 row->enabled_p)
29018 && row->cursor_in_fringe_p)
29019 {
29020 /* Cursor is in the fringe. */
29021 cr.x = window_box_right_offset (w,
29022 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29023 ? RIGHT_MARGIN_AREA
29024 : TEXT_AREA));
29025 cr.y = row->y;
29026 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29027 cr.height = row->height;
29028 return x_intersect_rectangles (&cr, r, &result);
29029 }
29030
29031 cursor_glyph = get_phys_cursor_glyph (w);
29032 if (cursor_glyph)
29033 {
29034 /* r is relative to W's box, but w->phys_cursor.x is relative
29035 to left edge of W's TEXT area. Adjust it. */
29036 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29037 cr.y = w->phys_cursor.y;
29038 cr.width = cursor_glyph->pixel_width;
29039 cr.height = w->phys_cursor_height;
29040 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29041 I assume the effect is the same -- and this is portable. */
29042 return x_intersect_rectangles (&cr, r, &result);
29043 }
29044 /* If we don't understand the format, pretend we're not in the hot-spot. */
29045 return 0;
29046 }
29047
29048
29049 /* EXPORT:
29050 Draw a vertical window border to the right of window W if W doesn't
29051 have vertical scroll bars. */
29052
29053 void
29054 x_draw_vertical_border (struct window *w)
29055 {
29056 struct frame *f = XFRAME (WINDOW_FRAME (w));
29057
29058 /* We could do better, if we knew what type of scroll-bar the adjacent
29059 windows (on either side) have... But we don't :-(
29060 However, I think this works ok. ++KFS 2003-04-25 */
29061
29062 /* Redraw borders between horizontally adjacent windows. Don't
29063 do it for frames with vertical scroll bars because either the
29064 right scroll bar of a window, or the left scroll bar of its
29065 neighbor will suffice as a border. */
29066 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29067 return;
29068
29069 /* Note: It is necessary to redraw both the left and the right
29070 borders, for when only this single window W is being
29071 redisplayed. */
29072 if (!WINDOW_RIGHTMOST_P (w)
29073 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29074 {
29075 int x0, x1, y0, y1;
29076
29077 window_box_edges (w, &x0, &y0, &x1, &y1);
29078 y1 -= 1;
29079
29080 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29081 x1 -= 1;
29082
29083 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29084 }
29085
29086 if (!WINDOW_LEFTMOST_P (w)
29087 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29088 {
29089 int x0, x1, y0, y1;
29090
29091 window_box_edges (w, &x0, &y0, &x1, &y1);
29092 y1 -= 1;
29093
29094 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29095 x0 -= 1;
29096
29097 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29098 }
29099 }
29100
29101
29102 /* Draw window dividers for window W. */
29103
29104 void
29105 x_draw_right_divider (struct window *w)
29106 {
29107 struct frame *f = WINDOW_XFRAME (w);
29108
29109 if (w->mini || w->pseudo_window_p)
29110 return;
29111 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29112 {
29113 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29114 int x1 = WINDOW_RIGHT_EDGE_X (w);
29115 int y0 = WINDOW_TOP_EDGE_Y (w);
29116 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29117
29118 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29119 }
29120 }
29121
29122 static void
29123 x_draw_bottom_divider (struct window *w)
29124 {
29125 struct frame *f = XFRAME (WINDOW_FRAME (w));
29126
29127 if (w->mini || w->pseudo_window_p)
29128 return;
29129 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29130 {
29131 int x0 = WINDOW_LEFT_EDGE_X (w);
29132 int x1 = WINDOW_RIGHT_EDGE_X (w);
29133 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29134 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29135
29136 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29137 }
29138 }
29139
29140 /* Redraw the part of window W intersection rectangle FR. Pixel
29141 coordinates in FR are frame-relative. Call this function with
29142 input blocked. Value is non-zero if the exposure overwrites
29143 mouse-face. */
29144
29145 static int
29146 expose_window (struct window *w, XRectangle *fr)
29147 {
29148 struct frame *f = XFRAME (w->frame);
29149 XRectangle wr, r;
29150 int mouse_face_overwritten_p = 0;
29151
29152 /* If window is not yet fully initialized, do nothing. This can
29153 happen when toolkit scroll bars are used and a window is split.
29154 Reconfiguring the scroll bar will generate an expose for a newly
29155 created window. */
29156 if (w->current_matrix == NULL)
29157 return 0;
29158
29159 /* When we're currently updating the window, display and current
29160 matrix usually don't agree. Arrange for a thorough display
29161 later. */
29162 if (w->must_be_updated_p)
29163 {
29164 SET_FRAME_GARBAGED (f);
29165 return 0;
29166 }
29167
29168 /* Frame-relative pixel rectangle of W. */
29169 wr.x = WINDOW_LEFT_EDGE_X (w);
29170 wr.y = WINDOW_TOP_EDGE_Y (w);
29171 wr.width = WINDOW_PIXEL_WIDTH (w);
29172 wr.height = WINDOW_PIXEL_HEIGHT (w);
29173
29174 if (x_intersect_rectangles (fr, &wr, &r))
29175 {
29176 int yb = window_text_bottom_y (w);
29177 struct glyph_row *row;
29178 int cursor_cleared_p, phys_cursor_on_p;
29179 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29180
29181 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29182 r.x, r.y, r.width, r.height));
29183
29184 /* Convert to window coordinates. */
29185 r.x -= WINDOW_LEFT_EDGE_X (w);
29186 r.y -= WINDOW_TOP_EDGE_Y (w);
29187
29188 /* Turn off the cursor. */
29189 if (!w->pseudo_window_p
29190 && phys_cursor_in_rect_p (w, &r))
29191 {
29192 x_clear_cursor (w);
29193 cursor_cleared_p = 1;
29194 }
29195 else
29196 cursor_cleared_p = 0;
29197
29198 /* If the row containing the cursor extends face to end of line,
29199 then expose_area might overwrite the cursor outside the
29200 rectangle and thus notice_overwritten_cursor might clear
29201 w->phys_cursor_on_p. We remember the original value and
29202 check later if it is changed. */
29203 phys_cursor_on_p = w->phys_cursor_on_p;
29204
29205 /* Update lines intersecting rectangle R. */
29206 first_overlapping_row = last_overlapping_row = NULL;
29207 for (row = w->current_matrix->rows;
29208 row->enabled_p;
29209 ++row)
29210 {
29211 int y0 = row->y;
29212 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29213
29214 if ((y0 >= r.y && y0 < r.y + r.height)
29215 || (y1 > r.y && y1 < r.y + r.height)
29216 || (r.y >= y0 && r.y < y1)
29217 || (r.y + r.height > y0 && r.y + r.height < y1))
29218 {
29219 /* A header line may be overlapping, but there is no need
29220 to fix overlapping areas for them. KFS 2005-02-12 */
29221 if (row->overlapping_p && !row->mode_line_p)
29222 {
29223 if (first_overlapping_row == NULL)
29224 first_overlapping_row = row;
29225 last_overlapping_row = row;
29226 }
29227
29228 row->clip = fr;
29229 if (expose_line (w, row, &r))
29230 mouse_face_overwritten_p = 1;
29231 row->clip = NULL;
29232 }
29233 else if (row->overlapping_p)
29234 {
29235 /* We must redraw a row overlapping the exposed area. */
29236 if (y0 < r.y
29237 ? y0 + row->phys_height > r.y
29238 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29239 {
29240 if (first_overlapping_row == NULL)
29241 first_overlapping_row = row;
29242 last_overlapping_row = row;
29243 }
29244 }
29245
29246 if (y1 >= yb)
29247 break;
29248 }
29249
29250 /* Display the mode line if there is one. */
29251 if (WINDOW_WANTS_MODELINE_P (w)
29252 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29253 row->enabled_p)
29254 && row->y < r.y + r.height)
29255 {
29256 if (expose_line (w, row, &r))
29257 mouse_face_overwritten_p = 1;
29258 }
29259
29260 if (!w->pseudo_window_p)
29261 {
29262 /* Fix the display of overlapping rows. */
29263 if (first_overlapping_row)
29264 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29265 fr);
29266
29267 /* Draw border between windows. */
29268 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29269 x_draw_right_divider (w);
29270 else
29271 x_draw_vertical_border (w);
29272
29273 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29274 x_draw_bottom_divider (w);
29275
29276 /* Turn the cursor on again. */
29277 if (cursor_cleared_p
29278 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29279 update_window_cursor (w, 1);
29280 }
29281 }
29282
29283 return mouse_face_overwritten_p;
29284 }
29285
29286
29287
29288 /* Redraw (parts) of all windows in the window tree rooted at W that
29289 intersect R. R contains frame pixel coordinates. Value is
29290 non-zero if the exposure overwrites mouse-face. */
29291
29292 static int
29293 expose_window_tree (struct window *w, XRectangle *r)
29294 {
29295 struct frame *f = XFRAME (w->frame);
29296 int mouse_face_overwritten_p = 0;
29297
29298 while (w && !FRAME_GARBAGED_P (f))
29299 {
29300 if (WINDOWP (w->contents))
29301 mouse_face_overwritten_p
29302 |= expose_window_tree (XWINDOW (w->contents), r);
29303 else
29304 mouse_face_overwritten_p |= expose_window (w, r);
29305
29306 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29307 }
29308
29309 return mouse_face_overwritten_p;
29310 }
29311
29312
29313 /* EXPORT:
29314 Redisplay an exposed area of frame F. X and Y are the upper-left
29315 corner of the exposed rectangle. W and H are width and height of
29316 the exposed area. All are pixel values. W or H zero means redraw
29317 the entire frame. */
29318
29319 void
29320 expose_frame (struct frame *f, int x, int y, int w, int h)
29321 {
29322 XRectangle r;
29323 int mouse_face_overwritten_p = 0;
29324
29325 TRACE ((stderr, "expose_frame "));
29326
29327 /* No need to redraw if frame will be redrawn soon. */
29328 if (FRAME_GARBAGED_P (f))
29329 {
29330 TRACE ((stderr, " garbaged\n"));
29331 return;
29332 }
29333
29334 /* If basic faces haven't been realized yet, there is no point in
29335 trying to redraw anything. This can happen when we get an expose
29336 event while Emacs is starting, e.g. by moving another window. */
29337 if (FRAME_FACE_CACHE (f) == NULL
29338 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29339 {
29340 TRACE ((stderr, " no faces\n"));
29341 return;
29342 }
29343
29344 if (w == 0 || h == 0)
29345 {
29346 r.x = r.y = 0;
29347 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29348 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29349 }
29350 else
29351 {
29352 r.x = x;
29353 r.y = y;
29354 r.width = w;
29355 r.height = h;
29356 }
29357
29358 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29359 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29360
29361 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29362 if (WINDOWP (f->tool_bar_window))
29363 mouse_face_overwritten_p
29364 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29365 #endif
29366
29367 #ifdef HAVE_X_WINDOWS
29368 #ifndef MSDOS
29369 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29370 if (WINDOWP (f->menu_bar_window))
29371 mouse_face_overwritten_p
29372 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29373 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29374 #endif
29375 #endif
29376
29377 /* Some window managers support a focus-follows-mouse style with
29378 delayed raising of frames. Imagine a partially obscured frame,
29379 and moving the mouse into partially obscured mouse-face on that
29380 frame. The visible part of the mouse-face will be highlighted,
29381 then the WM raises the obscured frame. With at least one WM, KDE
29382 2.1, Emacs is not getting any event for the raising of the frame
29383 (even tried with SubstructureRedirectMask), only Expose events.
29384 These expose events will draw text normally, i.e. not
29385 highlighted. Which means we must redo the highlight here.
29386 Subsume it under ``we love X''. --gerd 2001-08-15 */
29387 /* Included in Windows version because Windows most likely does not
29388 do the right thing if any third party tool offers
29389 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29390 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29391 {
29392 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29393 if (f == hlinfo->mouse_face_mouse_frame)
29394 {
29395 int mouse_x = hlinfo->mouse_face_mouse_x;
29396 int mouse_y = hlinfo->mouse_face_mouse_y;
29397 clear_mouse_face (hlinfo);
29398 note_mouse_highlight (f, mouse_x, mouse_y);
29399 }
29400 }
29401 }
29402
29403
29404 /* EXPORT:
29405 Determine the intersection of two rectangles R1 and R2. Return
29406 the intersection in *RESULT. Value is non-zero if RESULT is not
29407 empty. */
29408
29409 int
29410 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29411 {
29412 XRectangle *left, *right;
29413 XRectangle *upper, *lower;
29414 int intersection_p = 0;
29415
29416 /* Rearrange so that R1 is the left-most rectangle. */
29417 if (r1->x < r2->x)
29418 left = r1, right = r2;
29419 else
29420 left = r2, right = r1;
29421
29422 /* X0 of the intersection is right.x0, if this is inside R1,
29423 otherwise there is no intersection. */
29424 if (right->x <= left->x + left->width)
29425 {
29426 result->x = right->x;
29427
29428 /* The right end of the intersection is the minimum of
29429 the right ends of left and right. */
29430 result->width = (min (left->x + left->width, right->x + right->width)
29431 - result->x);
29432
29433 /* Same game for Y. */
29434 if (r1->y < r2->y)
29435 upper = r1, lower = r2;
29436 else
29437 upper = r2, lower = r1;
29438
29439 /* The upper end of the intersection is lower.y0, if this is inside
29440 of upper. Otherwise, there is no intersection. */
29441 if (lower->y <= upper->y + upper->height)
29442 {
29443 result->y = lower->y;
29444
29445 /* The lower end of the intersection is the minimum of the lower
29446 ends of upper and lower. */
29447 result->height = (min (lower->y + lower->height,
29448 upper->y + upper->height)
29449 - result->y);
29450 intersection_p = 1;
29451 }
29452 }
29453
29454 return intersection_p;
29455 }
29456
29457 #endif /* HAVE_WINDOW_SYSTEM */
29458
29459 \f
29460 /***********************************************************************
29461 Initialization
29462 ***********************************************************************/
29463
29464 void
29465 syms_of_xdisp (void)
29466 {
29467 Vwith_echo_area_save_vector = Qnil;
29468 staticpro (&Vwith_echo_area_save_vector);
29469
29470 Vmessage_stack = Qnil;
29471 staticpro (&Vmessage_stack);
29472
29473 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29474 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29475
29476 message_dolog_marker1 = Fmake_marker ();
29477 staticpro (&message_dolog_marker1);
29478 message_dolog_marker2 = Fmake_marker ();
29479 staticpro (&message_dolog_marker2);
29480 message_dolog_marker3 = Fmake_marker ();
29481 staticpro (&message_dolog_marker3);
29482
29483 #ifdef GLYPH_DEBUG
29484 defsubr (&Sdump_frame_glyph_matrix);
29485 defsubr (&Sdump_glyph_matrix);
29486 defsubr (&Sdump_glyph_row);
29487 defsubr (&Sdump_tool_bar_row);
29488 defsubr (&Strace_redisplay);
29489 defsubr (&Strace_to_stderr);
29490 #endif
29491 #ifdef HAVE_WINDOW_SYSTEM
29492 defsubr (&Stool_bar_height);
29493 defsubr (&Slookup_image_map);
29494 #endif
29495 defsubr (&Sline_pixel_height);
29496 defsubr (&Sformat_mode_line);
29497 defsubr (&Sinvisible_p);
29498 defsubr (&Scurrent_bidi_paragraph_direction);
29499 defsubr (&Swindow_text_pixel_size);
29500 defsubr (&Smove_point_visually);
29501
29502 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29503 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29504 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29505 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29506 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29507 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29508 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29509 DEFSYM (Qeval, "eval");
29510 DEFSYM (QCdata, ":data");
29511 DEFSYM (Qdisplay, "display");
29512 DEFSYM (Qspace_width, "space-width");
29513 DEFSYM (Qraise, "raise");
29514 DEFSYM (Qslice, "slice");
29515 DEFSYM (Qspace, "space");
29516 DEFSYM (Qmargin, "margin");
29517 DEFSYM (Qpointer, "pointer");
29518 DEFSYM (Qleft_margin, "left-margin");
29519 DEFSYM (Qright_margin, "right-margin");
29520 DEFSYM (Qcenter, "center");
29521 DEFSYM (Qline_height, "line-height");
29522 DEFSYM (QCalign_to, ":align-to");
29523 DEFSYM (QCrelative_width, ":relative-width");
29524 DEFSYM (QCrelative_height, ":relative-height");
29525 DEFSYM (QCeval, ":eval");
29526 DEFSYM (QCpropertize, ":propertize");
29527 DEFSYM (QCfile, ":file");
29528 DEFSYM (Qfontified, "fontified");
29529 DEFSYM (Qfontification_functions, "fontification-functions");
29530 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29531 DEFSYM (Qescape_glyph, "escape-glyph");
29532 DEFSYM (Qnobreak_space, "nobreak-space");
29533 DEFSYM (Qimage, "image");
29534 DEFSYM (Qtext, "text");
29535 DEFSYM (Qboth, "both");
29536 DEFSYM (Qboth_horiz, "both-horiz");
29537 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29538 DEFSYM (QCmap, ":map");
29539 DEFSYM (QCpointer, ":pointer");
29540 DEFSYM (Qrect, "rect");
29541 DEFSYM (Qcircle, "circle");
29542 DEFSYM (Qpoly, "poly");
29543 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29544 DEFSYM (Qgrow_only, "grow-only");
29545 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29546 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29547 DEFSYM (Qposition, "position");
29548 DEFSYM (Qbuffer_position, "buffer-position");
29549 DEFSYM (Qobject, "object");
29550 DEFSYM (Qbar, "bar");
29551 DEFSYM (Qhbar, "hbar");
29552 DEFSYM (Qbox, "box");
29553 DEFSYM (Qhollow, "hollow");
29554 DEFSYM (Qhand, "hand");
29555 DEFSYM (Qarrow, "arrow");
29556 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29557
29558 list_of_error = list1 (list2 (intern_c_string ("error"),
29559 intern_c_string ("void-variable")));
29560 staticpro (&list_of_error);
29561
29562 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29563 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29564 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29565 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29566
29567 echo_buffer[0] = echo_buffer[1] = Qnil;
29568 staticpro (&echo_buffer[0]);
29569 staticpro (&echo_buffer[1]);
29570
29571 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29572 staticpro (&echo_area_buffer[0]);
29573 staticpro (&echo_area_buffer[1]);
29574
29575 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29576 staticpro (&Vmessages_buffer_name);
29577
29578 mode_line_proptrans_alist = Qnil;
29579 staticpro (&mode_line_proptrans_alist);
29580 mode_line_string_list = Qnil;
29581 staticpro (&mode_line_string_list);
29582 mode_line_string_face = Qnil;
29583 staticpro (&mode_line_string_face);
29584 mode_line_string_face_prop = Qnil;
29585 staticpro (&mode_line_string_face_prop);
29586 Vmode_line_unwind_vector = Qnil;
29587 staticpro (&Vmode_line_unwind_vector);
29588
29589 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29590
29591 help_echo_string = Qnil;
29592 staticpro (&help_echo_string);
29593 help_echo_object = Qnil;
29594 staticpro (&help_echo_object);
29595 help_echo_window = Qnil;
29596 staticpro (&help_echo_window);
29597 previous_help_echo_string = Qnil;
29598 staticpro (&previous_help_echo_string);
29599 help_echo_pos = -1;
29600
29601 DEFSYM (Qright_to_left, "right-to-left");
29602 DEFSYM (Qleft_to_right, "left-to-right");
29603
29604 #ifdef HAVE_WINDOW_SYSTEM
29605 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29606 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29607 For example, if a block cursor is over a tab, it will be drawn as
29608 wide as that tab on the display. */);
29609 x_stretch_cursor_p = 0;
29610 #endif
29611
29612 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29613 doc: /* Non-nil means highlight trailing whitespace.
29614 The face used for trailing whitespace is `trailing-whitespace'. */);
29615 Vshow_trailing_whitespace = Qnil;
29616
29617 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29618 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29619 If the value is t, Emacs highlights non-ASCII chars which have the
29620 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29621 or `escape-glyph' face respectively.
29622
29623 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29624 U+2011 (non-breaking hyphen) are affected.
29625
29626 Any other non-nil value means to display these characters as a escape
29627 glyph followed by an ordinary space or hyphen.
29628
29629 A value of nil means no special handling of these characters. */);
29630 Vnobreak_char_display = Qt;
29631
29632 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29633 doc: /* The pointer shape to show in void text areas.
29634 A value of nil means to show the text pointer. Other options are `arrow',
29635 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29636 Vvoid_text_area_pointer = Qarrow;
29637
29638 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29639 doc: /* Non-nil means don't actually do any redisplay.
29640 This is used for internal purposes. */);
29641 Vinhibit_redisplay = Qnil;
29642
29643 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29644 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29645 Vglobal_mode_string = Qnil;
29646
29647 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29648 doc: /* Marker for where to display an arrow on top of the buffer text.
29649 This must be the beginning of a line in order to work.
29650 See also `overlay-arrow-string'. */);
29651 Voverlay_arrow_position = Qnil;
29652
29653 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29654 doc: /* String to display as an arrow in non-window frames.
29655 See also `overlay-arrow-position'. */);
29656 Voverlay_arrow_string = build_pure_c_string ("=>");
29657
29658 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29659 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29660 The symbols on this list are examined during redisplay to determine
29661 where to display overlay arrows. */);
29662 Voverlay_arrow_variable_list
29663 = list1 (intern_c_string ("overlay-arrow-position"));
29664
29665 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29666 doc: /* The number of lines to try scrolling a window by when point moves out.
29667 If that fails to bring point back on frame, point is centered instead.
29668 If this is zero, point is always centered after it moves off frame.
29669 If you want scrolling to always be a line at a time, you should set
29670 `scroll-conservatively' to a large value rather than set this to 1. */);
29671
29672 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29673 doc: /* Scroll up to this many lines, to bring point back on screen.
29674 If point moves off-screen, redisplay will scroll by up to
29675 `scroll-conservatively' lines in order to bring point just barely
29676 onto the screen again. If that cannot be done, then redisplay
29677 recenters point as usual.
29678
29679 If the value is greater than 100, redisplay will never recenter point,
29680 but will always scroll just enough text to bring point into view, even
29681 if you move far away.
29682
29683 A value of zero means always recenter point if it moves off screen. */);
29684 scroll_conservatively = 0;
29685
29686 DEFVAR_INT ("scroll-margin", scroll_margin,
29687 doc: /* Number of lines of margin at the top and bottom of a window.
29688 Recenter the window whenever point gets within this many lines
29689 of the top or bottom of the window. */);
29690 scroll_margin = 0;
29691
29692 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29693 doc: /* Pixels per inch value for non-window system displays.
29694 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29695 Vdisplay_pixels_per_inch = make_float (72.0);
29696
29697 #ifdef GLYPH_DEBUG
29698 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29699 #endif
29700
29701 DEFVAR_LISP ("truncate-partial-width-windows",
29702 Vtruncate_partial_width_windows,
29703 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29704 For an integer value, truncate lines in each window narrower than the
29705 full frame width, provided the window width is less than that integer;
29706 otherwise, respect the value of `truncate-lines'.
29707
29708 For any other non-nil value, truncate lines in all windows that do
29709 not span the full frame width.
29710
29711 A value of nil means to respect the value of `truncate-lines'.
29712
29713 If `word-wrap' is enabled, you might want to reduce this. */);
29714 Vtruncate_partial_width_windows = make_number (50);
29715
29716 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29717 doc: /* Maximum buffer size for which line number should be displayed.
29718 If the buffer is bigger than this, the line number does not appear
29719 in the mode line. A value of nil means no limit. */);
29720 Vline_number_display_limit = Qnil;
29721
29722 DEFVAR_INT ("line-number-display-limit-width",
29723 line_number_display_limit_width,
29724 doc: /* Maximum line width (in characters) for line number display.
29725 If the average length of the lines near point is bigger than this, then the
29726 line number may be omitted from the mode line. */);
29727 line_number_display_limit_width = 200;
29728
29729 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29730 doc: /* Non-nil means highlight region even in nonselected windows. */);
29731 highlight_nonselected_windows = 0;
29732
29733 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29734 doc: /* Non-nil if more than one frame is visible on this display.
29735 Minibuffer-only frames don't count, but iconified frames do.
29736 This variable is not guaranteed to be accurate except while processing
29737 `frame-title-format' and `icon-title-format'. */);
29738
29739 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29740 doc: /* Template for displaying the title bar of visible frames.
29741 \(Assuming the window manager supports this feature.)
29742
29743 This variable has the same structure as `mode-line-format', except that
29744 the %c and %l constructs are ignored. It is used only on frames for
29745 which no explicit name has been set \(see `modify-frame-parameters'). */);
29746
29747 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29748 doc: /* Template for displaying the title bar of an iconified frame.
29749 \(Assuming the window manager supports this feature.)
29750 This variable has the same structure as `mode-line-format' (which see),
29751 and is used only on frames for which no explicit name has been set
29752 \(see `modify-frame-parameters'). */);
29753 Vicon_title_format
29754 = Vframe_title_format
29755 = listn (CONSTYPE_PURE, 3,
29756 intern_c_string ("multiple-frames"),
29757 build_pure_c_string ("%b"),
29758 listn (CONSTYPE_PURE, 4,
29759 empty_unibyte_string,
29760 intern_c_string ("invocation-name"),
29761 build_pure_c_string ("@"),
29762 intern_c_string ("system-name")));
29763
29764 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29765 doc: /* Maximum number of lines to keep in the message log buffer.
29766 If nil, disable message logging. If t, log messages but don't truncate
29767 the buffer when it becomes large. */);
29768 Vmessage_log_max = make_number (1000);
29769
29770 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29771 doc: /* Functions called before redisplay, if window sizes have changed.
29772 The value should be a list of functions that take one argument.
29773 Just before redisplay, for each frame, if any of its windows have changed
29774 size since the last redisplay, or have been split or deleted,
29775 all the functions in the list are called, with the frame as argument. */);
29776 Vwindow_size_change_functions = Qnil;
29777
29778 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29779 doc: /* List of functions to call before redisplaying a window with scrolling.
29780 Each function is called with two arguments, the window and its new
29781 display-start position. Note that these functions are also called by
29782 `set-window-buffer'. Also note that the value of `window-end' is not
29783 valid when these functions are called.
29784
29785 Warning: Do not use this feature to alter the way the window
29786 is scrolled. It is not designed for that, and such use probably won't
29787 work. */);
29788 Vwindow_scroll_functions = Qnil;
29789
29790 DEFVAR_LISP ("window-text-change-functions",
29791 Vwindow_text_change_functions,
29792 doc: /* Functions to call in redisplay when text in the window might change. */);
29793 Vwindow_text_change_functions = Qnil;
29794
29795 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29796 doc: /* Functions called when redisplay of a window reaches the end trigger.
29797 Each function is called with two arguments, the window and the end trigger value.
29798 See `set-window-redisplay-end-trigger'. */);
29799 Vredisplay_end_trigger_functions = Qnil;
29800
29801 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29802 doc: /* Non-nil means autoselect window with mouse pointer.
29803 If nil, do not autoselect windows.
29804 A positive number means delay autoselection by that many seconds: a
29805 window is autoselected only after the mouse has remained in that
29806 window for the duration of the delay.
29807 A negative number has a similar effect, but causes windows to be
29808 autoselected only after the mouse has stopped moving. \(Because of
29809 the way Emacs compares mouse events, you will occasionally wait twice
29810 that time before the window gets selected.\)
29811 Any other value means to autoselect window instantaneously when the
29812 mouse pointer enters it.
29813
29814 Autoselection selects the minibuffer only if it is active, and never
29815 unselects the minibuffer if it is active.
29816
29817 When customizing this variable make sure that the actual value of
29818 `focus-follows-mouse' matches the behavior of your window manager. */);
29819 Vmouse_autoselect_window = Qnil;
29820
29821 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29822 doc: /* Non-nil means automatically resize tool-bars.
29823 This dynamically changes the tool-bar's height to the minimum height
29824 that is needed to make all tool-bar items visible.
29825 If value is `grow-only', the tool-bar's height is only increased
29826 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29827 Vauto_resize_tool_bars = Qt;
29828
29829 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29830 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29831 auto_raise_tool_bar_buttons_p = 1;
29832
29833 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29834 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29835 make_cursor_line_fully_visible_p = 1;
29836
29837 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29838 doc: /* Border below tool-bar in pixels.
29839 If an integer, use it as the height of the border.
29840 If it is one of `internal-border-width' or `border-width', use the
29841 value of the corresponding frame parameter.
29842 Otherwise, no border is added below the tool-bar. */);
29843 Vtool_bar_border = Qinternal_border_width;
29844
29845 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29846 doc: /* Margin around tool-bar buttons in pixels.
29847 If an integer, use that for both horizontal and vertical margins.
29848 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29849 HORZ specifying the horizontal margin, and VERT specifying the
29850 vertical margin. */);
29851 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29852
29853 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29854 doc: /* Relief thickness of tool-bar buttons. */);
29855 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29856
29857 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29858 doc: /* Tool bar style to use.
29859 It can be one of
29860 image - show images only
29861 text - show text only
29862 both - show both, text below image
29863 both-horiz - show text to the right of the image
29864 text-image-horiz - show text to the left of the image
29865 any other - use system default or image if no system default.
29866
29867 This variable only affects the GTK+ toolkit version of Emacs. */);
29868 Vtool_bar_style = Qnil;
29869
29870 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29871 doc: /* Maximum number of characters a label can have to be shown.
29872 The tool bar style must also show labels for this to have any effect, see
29873 `tool-bar-style'. */);
29874 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29875
29876 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29877 doc: /* List of functions to call to fontify regions of text.
29878 Each function is called with one argument POS. Functions must
29879 fontify a region starting at POS in the current buffer, and give
29880 fontified regions the property `fontified'. */);
29881 Vfontification_functions = Qnil;
29882 Fmake_variable_buffer_local (Qfontification_functions);
29883
29884 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29885 unibyte_display_via_language_environment,
29886 doc: /* Non-nil means display unibyte text according to language environment.
29887 Specifically, this means that raw bytes in the range 160-255 decimal
29888 are displayed by converting them to the equivalent multibyte characters
29889 according to the current language environment. As a result, they are
29890 displayed according to the current fontset.
29891
29892 Note that this variable affects only how these bytes are displayed,
29893 but does not change the fact they are interpreted as raw bytes. */);
29894 unibyte_display_via_language_environment = 0;
29895
29896 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29897 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29898 If a float, it specifies a fraction of the mini-window frame's height.
29899 If an integer, it specifies a number of lines. */);
29900 Vmax_mini_window_height = make_float (0.25);
29901
29902 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29903 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29904 A value of nil means don't automatically resize mini-windows.
29905 A value of t means resize them to fit the text displayed in them.
29906 A value of `grow-only', the default, means let mini-windows grow only;
29907 they return to their normal size when the minibuffer is closed, or the
29908 echo area becomes empty. */);
29909 Vresize_mini_windows = Qgrow_only;
29910
29911 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29912 doc: /* Alist specifying how to blink the cursor off.
29913 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29914 `cursor-type' frame-parameter or variable equals ON-STATE,
29915 comparing using `equal', Emacs uses OFF-STATE to specify
29916 how to blink it off. ON-STATE and OFF-STATE are values for
29917 the `cursor-type' frame parameter.
29918
29919 If a frame's ON-STATE has no entry in this list,
29920 the frame's other specifications determine how to blink the cursor off. */);
29921 Vblink_cursor_alist = Qnil;
29922
29923 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29924 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29925 If non-nil, windows are automatically scrolled horizontally to make
29926 point visible. */);
29927 automatic_hscrolling_p = 1;
29928 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29929
29930 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29931 doc: /* How many columns away from the window edge point is allowed to get
29932 before automatic hscrolling will horizontally scroll the window. */);
29933 hscroll_margin = 5;
29934
29935 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29936 doc: /* How many columns to scroll the window when point gets too close to the edge.
29937 When point is less than `hscroll-margin' columns from the window
29938 edge, automatic hscrolling will scroll the window by the amount of columns
29939 determined by this variable. If its value is a positive integer, scroll that
29940 many columns. If it's a positive floating-point number, it specifies the
29941 fraction of the window's width to scroll. If it's nil or zero, point will be
29942 centered horizontally after the scroll. Any other value, including negative
29943 numbers, are treated as if the value were zero.
29944
29945 Automatic hscrolling always moves point outside the scroll margin, so if
29946 point was more than scroll step columns inside the margin, the window will
29947 scroll more than the value given by the scroll step.
29948
29949 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29950 and `scroll-right' overrides this variable's effect. */);
29951 Vhscroll_step = make_number (0);
29952
29953 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29954 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29955 Bind this around calls to `message' to let it take effect. */);
29956 message_truncate_lines = 0;
29957
29958 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29959 doc: /* Normal hook run to update the menu bar definitions.
29960 Redisplay runs this hook before it redisplays the menu bar.
29961 This is used to update submenus such as Buffers,
29962 whose contents depend on various data. */);
29963 Vmenu_bar_update_hook = Qnil;
29964
29965 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29966 doc: /* Frame for which we are updating a menu.
29967 The enable predicate for a menu binding should check this variable. */);
29968 Vmenu_updating_frame = Qnil;
29969
29970 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29971 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29972 inhibit_menubar_update = 0;
29973
29974 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29975 doc: /* Prefix prepended to all continuation lines at display time.
29976 The value may be a string, an image, or a stretch-glyph; it is
29977 interpreted in the same way as the value of a `display' text property.
29978
29979 This variable is overridden by any `wrap-prefix' text or overlay
29980 property.
29981
29982 To add a prefix to non-continuation lines, use `line-prefix'. */);
29983 Vwrap_prefix = Qnil;
29984 DEFSYM (Qwrap_prefix, "wrap-prefix");
29985 Fmake_variable_buffer_local (Qwrap_prefix);
29986
29987 DEFVAR_LISP ("line-prefix", Vline_prefix,
29988 doc: /* Prefix prepended to all non-continuation lines at display time.
29989 The value may be a string, an image, or a stretch-glyph; it is
29990 interpreted in the same way as the value of a `display' text property.
29991
29992 This variable is overridden by any `line-prefix' text or overlay
29993 property.
29994
29995 To add a prefix to continuation lines, use `wrap-prefix'. */);
29996 Vline_prefix = Qnil;
29997 DEFSYM (Qline_prefix, "line-prefix");
29998 Fmake_variable_buffer_local (Qline_prefix);
29999
30000 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30001 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30002 inhibit_eval_during_redisplay = 0;
30003
30004 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30005 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30006 inhibit_free_realized_faces = 0;
30007
30008 #ifdef GLYPH_DEBUG
30009 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30010 doc: /* Inhibit try_window_id display optimization. */);
30011 inhibit_try_window_id = 0;
30012
30013 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30014 doc: /* Inhibit try_window_reusing display optimization. */);
30015 inhibit_try_window_reusing = 0;
30016
30017 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30018 doc: /* Inhibit try_cursor_movement display optimization. */);
30019 inhibit_try_cursor_movement = 0;
30020 #endif /* GLYPH_DEBUG */
30021
30022 DEFVAR_INT ("overline-margin", overline_margin,
30023 doc: /* Space between overline and text, in pixels.
30024 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30025 margin to the character height. */);
30026 overline_margin = 2;
30027
30028 DEFVAR_INT ("underline-minimum-offset",
30029 underline_minimum_offset,
30030 doc: /* Minimum distance between baseline and underline.
30031 This can improve legibility of underlined text at small font sizes,
30032 particularly when using variable `x-use-underline-position-properties'
30033 with fonts that specify an UNDERLINE_POSITION relatively close to the
30034 baseline. The default value is 1. */);
30035 underline_minimum_offset = 1;
30036
30037 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30038 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30039 This feature only works when on a window system that can change
30040 cursor shapes. */);
30041 display_hourglass_p = 1;
30042
30043 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30044 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30045 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30046
30047 #ifdef HAVE_WINDOW_SYSTEM
30048 hourglass_atimer = NULL;
30049 hourglass_shown_p = 0;
30050 #endif /* HAVE_WINDOW_SYSTEM */
30051
30052 DEFSYM (Qglyphless_char, "glyphless-char");
30053 DEFSYM (Qhex_code, "hex-code");
30054 DEFSYM (Qempty_box, "empty-box");
30055 DEFSYM (Qthin_space, "thin-space");
30056 DEFSYM (Qzero_width, "zero-width");
30057
30058 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30059 doc: /* Function run just before redisplay.
30060 It is called with one argument, which is the set of windows that are to
30061 be redisplayed. This set can be nil (meaning, only the selected window),
30062 or t (meaning all windows). */);
30063 Vpre_redisplay_function = intern ("ignore");
30064
30065 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30066 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30067
30068 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30069 doc: /* Char-table defining glyphless characters.
30070 Each element, if non-nil, should be one of the following:
30071 an ASCII acronym string: display this string in a box
30072 `hex-code': display the hexadecimal code of a character in a box
30073 `empty-box': display as an empty box
30074 `thin-space': display as 1-pixel width space
30075 `zero-width': don't display
30076 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30077 display method for graphical terminals and text terminals respectively.
30078 GRAPHICAL and TEXT should each have one of the values listed above.
30079
30080 The char-table has one extra slot to control the display of a character for
30081 which no font is found. This slot only takes effect on graphical terminals.
30082 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30083 `thin-space'. The default is `empty-box'.
30084
30085 If a character has a non-nil entry in an active display table, the
30086 display table takes effect; in this case, Emacs does not consult
30087 `glyphless-char-display' at all. */);
30088 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30089 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30090 Qempty_box);
30091
30092 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30093 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30094 Vdebug_on_message = Qnil;
30095
30096 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30097 doc: /* */);
30098 Vredisplay__all_windows_cause
30099 = Fmake_vector (make_number (100), make_number (0));
30100
30101 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30102 doc: /* */);
30103 Vredisplay__mode_lines_cause
30104 = Fmake_vector (make_number (100), make_number (0));
30105 }
30106
30107
30108 /* Initialize this module when Emacs starts. */
30109
30110 void
30111 init_xdisp (void)
30112 {
30113 CHARPOS (this_line_start_pos) = 0;
30114
30115 if (!noninteractive)
30116 {
30117 struct window *m = XWINDOW (minibuf_window);
30118 Lisp_Object frame = m->frame;
30119 struct frame *f = XFRAME (frame);
30120 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30121 struct window *r = XWINDOW (root);
30122 int i;
30123
30124 echo_area_window = minibuf_window;
30125
30126 r->top_line = FRAME_TOP_MARGIN (f);
30127 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30128 r->total_cols = FRAME_COLS (f);
30129 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30130 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30131 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30132
30133 m->top_line = FRAME_LINES (f) - 1;
30134 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30135 m->total_cols = FRAME_COLS (f);
30136 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30137 m->total_lines = 1;
30138 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30139
30140 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30141 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30142 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30143
30144 /* The default ellipsis glyphs `...'. */
30145 for (i = 0; i < 3; ++i)
30146 default_invis_vector[i] = make_number ('.');
30147 }
30148
30149 {
30150 /* Allocate the buffer for frame titles.
30151 Also used for `format-mode-line'. */
30152 int size = 100;
30153 mode_line_noprop_buf = xmalloc (size);
30154 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30155 mode_line_noprop_ptr = mode_line_noprop_buf;
30156 mode_line_target = MODE_LINE_DISPLAY;
30157 }
30158
30159 help_echo_showing_p = 0;
30160 }
30161
30162 #ifdef HAVE_WINDOW_SYSTEM
30163
30164 /* Platform-independent portion of hourglass implementation. */
30165
30166 /* Cancel a currently active hourglass timer, and start a new one. */
30167 void
30168 start_hourglass (void)
30169 {
30170 struct timespec delay;
30171
30172 cancel_hourglass ();
30173
30174 if (INTEGERP (Vhourglass_delay)
30175 && XINT (Vhourglass_delay) > 0)
30176 delay = make_timespec (min (XINT (Vhourglass_delay),
30177 TYPE_MAXIMUM (time_t)),
30178 0);
30179 else if (FLOATP (Vhourglass_delay)
30180 && XFLOAT_DATA (Vhourglass_delay) > 0)
30181 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30182 else
30183 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30184
30185 #ifdef HAVE_NTGUI
30186 {
30187 extern void w32_note_current_window (void);
30188 w32_note_current_window ();
30189 }
30190 #endif /* HAVE_NTGUI */
30191
30192 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30193 show_hourglass, NULL);
30194 }
30195
30196
30197 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30198 shown. */
30199 void
30200 cancel_hourglass (void)
30201 {
30202 if (hourglass_atimer)
30203 {
30204 cancel_atimer (hourglass_atimer);
30205 hourglass_atimer = NULL;
30206 }
30207
30208 if (hourglass_shown_p)
30209 hide_hourglass ();
30210 }
30211
30212 #endif /* HAVE_WINDOW_SYSTEM */