Support resizing frames and windows pixelwise.
[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 notice_overwritten_cursor (struct window *,
974 enum glyph_row_area,
975 int, int, int, int);
976 static void append_stretch_glyph (struct it *, Lisp_Object,
977 int, int, int);
978
979
980 #endif /* HAVE_WINDOW_SYSTEM */
981
982 static void produce_special_glyphs (struct it *, enum display_element_type);
983 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
984 static int coords_in_mouse_face_p (struct window *, int, int);
985
986
987 \f
988 /***********************************************************************
989 Window display dimensions
990 ***********************************************************************/
991
992 /* Return the bottom boundary y-position for text lines in window W.
993 This is the first y position at which a line cannot start.
994 It is relative to the top of the window.
995
996 This is the height of W minus the height of a mode line, if any. */
997
998 int
999 window_text_bottom_y (struct window *w)
1000 {
1001 int height = WINDOW_PIXEL_HEIGHT (w);
1002
1003 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1004
1005 if (WINDOW_WANTS_MODELINE_P (w))
1006 height -= CURRENT_MODE_LINE_HEIGHT (w);
1007
1008 return height;
1009 }
1010
1011 /* Return the pixel width of display area AREA of window W.
1012 ANY_AREA means return the total width of W, not including
1013 fringes to the left and right of the window. */
1014
1015 int
1016 window_box_width (struct window *w, enum glyph_row_area area)
1017 {
1018 int pixels = w->pixel_width;
1019
1020 if (!w->pseudo_window_p)
1021 {
1022 pixels -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1023 pixels -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1024
1025 if (area == TEXT_AREA)
1026 pixels -= (WINDOW_MARGINS_WIDTH (w)
1027 + WINDOW_FRINGES_WIDTH (w));
1028 else if (area == LEFT_MARGIN_AREA)
1029 pixels = WINDOW_LEFT_MARGIN_WIDTH (w);
1030 else if (area == RIGHT_MARGIN_AREA)
1031 pixels = WINDOW_RIGHT_MARGIN_WIDTH (w);
1032 }
1033
1034 return pixels;
1035 }
1036
1037
1038 /* Return the pixel height of the display area of window W, not
1039 including mode lines of W, if any. */
1040
1041 int
1042 window_box_height (struct window *w)
1043 {
1044 struct frame *f = XFRAME (w->frame);
1045 int height = WINDOW_PIXEL_HEIGHT (w);
1046
1047 eassert (height >= 0);
1048
1049 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1050
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1056
1057 if (WINDOW_WANTS_MODELINE_P (w))
1058 {
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1067 }
1068
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 {
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1079 }
1080
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
1084 }
1085
1086 /* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. ANY_AREA means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1089
1090 int
1091 window_box_left_offset (struct window *w, enum glyph_row_area area)
1092 {
1093 int x;
1094
1095 if (w->pseudo_window_p)
1096 return 0;
1097
1098 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1099
1100 if (area == TEXT_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA));
1103 else if (area == RIGHT_MARGIN_AREA)
1104 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1105 + window_box_width (w, LEFT_MARGIN_AREA)
1106 + window_box_width (w, TEXT_AREA)
1107 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1108 ? 0
1109 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1110 else if (area == LEFT_MARGIN_AREA
1111 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1112 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1113
1114 return x;
1115 }
1116
1117
1118 /* Return the window-relative coordinate of the right edge of display
1119 area AREA of window W. ANY_AREA means return the right edge of the
1120 whole window, to the left of the right fringe of W. */
1121
1122 int
1123 window_box_right_offset (struct window *w, enum glyph_row_area area)
1124 {
1125 return window_box_left_offset (w, area) + window_box_width (w, area);
1126 }
1127
1128 /* Return the frame-relative coordinate of the left edge of display
1129 area AREA of window W. ANY_AREA means return the left edge of the
1130 whole window, to the right of the left fringe of W. */
1131
1132 int
1133 window_box_left (struct window *w, enum glyph_row_area area)
1134 {
1135 struct frame *f = XFRAME (w->frame);
1136 int x;
1137
1138 if (w->pseudo_window_p)
1139 return FRAME_INTERNAL_BORDER_WIDTH (f);
1140
1141 x = (WINDOW_LEFT_EDGE_X (w)
1142 + window_box_left_offset (w, area));
1143
1144 return x;
1145 }
1146
1147
1148 /* Return the frame-relative coordinate of the right edge of display
1149 area AREA of window W. ANY_AREA means return the right edge of the
1150 whole window, to the left of the right fringe of W. */
1151
1152 int
1153 window_box_right (struct window *w, enum glyph_row_area area)
1154 {
1155 return window_box_left (w, area) + window_box_width (w, area);
1156 }
1157
1158 /* Get the bounding box of the display area AREA of window W, without
1159 mode lines, in frame-relative coordinates. ANY_AREA means the
1160 whole window, not including the left and right fringes of
1161 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1162 coordinates of the upper-left corner of the box. Return in
1163 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1164
1165 void
1166 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1167 int *box_y, int *box_width, int *box_height)
1168 {
1169 if (box_width)
1170 *box_width = window_box_width (w, area);
1171 if (box_height)
1172 *box_height = window_box_height (w);
1173 if (box_x)
1174 *box_x = window_box_left (w, area);
1175 if (box_y)
1176 {
1177 *box_y = WINDOW_TOP_EDGE_Y (w);
1178 if (WINDOW_WANTS_HEADER_LINE_P (w))
1179 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1180 }
1181 }
1182
1183 #ifdef HAVE_WINDOW_SYSTEM
1184
1185 /* Get the bounding box of the display area AREA of window W, without
1186 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1187 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1188 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1189 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1190 box. */
1191
1192 static void
1193 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1194 int *bottom_right_x, int *bottom_right_y)
1195 {
1196 window_box (w, ANY_AREA, top_left_x, top_left_y,
1197 bottom_right_x, bottom_right_y);
1198 *bottom_right_x += *top_left_x;
1199 *bottom_right_y += *top_left_y;
1200 }
1201
1202 #endif /* HAVE_WINDOW_SYSTEM */
1203
1204 /***********************************************************************
1205 Utilities
1206 ***********************************************************************/
1207
1208 /* Return the bottom y-position of the line the iterator IT is in.
1209 This can modify IT's settings. */
1210
1211 int
1212 line_bottom_y (struct it *it)
1213 {
1214 int line_height = it->max_ascent + it->max_descent;
1215 int line_top_y = it->current_y;
1216
1217 if (line_height == 0)
1218 {
1219 if (last_height)
1220 line_height = last_height;
1221 else if (IT_CHARPOS (*it) < ZV)
1222 {
1223 move_it_by_lines (it, 1);
1224 line_height = (it->max_ascent || it->max_descent
1225 ? it->max_ascent + it->max_descent
1226 : last_height);
1227 }
1228 else
1229 {
1230 struct glyph_row *row = it->glyph_row;
1231
1232 /* Use the default character height. */
1233 it->glyph_row = NULL;
1234 it->what = IT_CHARACTER;
1235 it->c = ' ';
1236 it->len = 1;
1237 PRODUCE_GLYPHS (it);
1238 line_height = it->ascent + it->descent;
1239 it->glyph_row = row;
1240 }
1241 }
1242
1243 return line_top_y + line_height;
1244 }
1245
1246 DEFUN ("line-pixel-height", Fline_pixel_height,
1247 Sline_pixel_height, 0, 0, 0,
1248 doc: /* Return height in pixels of text line in the selected window.
1249
1250 Value is the height in pixels of the line at point. */)
1251 (void)
1252 {
1253 struct it it;
1254 struct text_pos pt;
1255 struct window *w = XWINDOW (selected_window);
1256
1257 SET_TEXT_POS (pt, PT, PT_BYTE);
1258 start_display (&it, w, pt);
1259 it.vpos = it.current_y = 0;
1260 last_height = 0;
1261 return make_number (line_bottom_y (&it));
1262 }
1263
1264 /* Return the default pixel height of text lines in window W. The
1265 value is the canonical height of the W frame's default font, plus
1266 any extra space required by the line-spacing variable or frame
1267 parameter.
1268
1269 Implementation note: this ignores any line-spacing text properties
1270 put on the newline characters. This is because those properties
1271 only affect the _screen_ line ending in the newline (i.e., in a
1272 continued line, only the last screen line will be affected), which
1273 means only a small number of lines in a buffer can ever use this
1274 feature. Since this function is used to compute the default pixel
1275 equivalent of text lines in a window, we can safely ignore those
1276 few lines. For the same reasons, we ignore the line-height
1277 properties. */
1278 int
1279 default_line_pixel_height (struct window *w)
1280 {
1281 struct frame *f = WINDOW_XFRAME (w);
1282 int height = FRAME_LINE_HEIGHT (f);
1283
1284 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1285 {
1286 struct buffer *b = XBUFFER (w->contents);
1287 Lisp_Object val = BVAR (b, extra_line_spacing);
1288
1289 if (NILP (val))
1290 val = BVAR (&buffer_defaults, extra_line_spacing);
1291 if (!NILP (val))
1292 {
1293 if (RANGED_INTEGERP (0, val, INT_MAX))
1294 height += XFASTINT (val);
1295 else if (FLOATP (val))
1296 {
1297 int addon = XFLOAT_DATA (val) * height + 0.5;
1298
1299 if (addon >= 0)
1300 height += addon;
1301 }
1302 }
1303 else
1304 height += f->extra_line_spacing;
1305 }
1306
1307 return height;
1308 }
1309
1310 /* Subroutine of pos_visible_p below. Extracts a display string, if
1311 any, from the display spec given as its argument. */
1312 static Lisp_Object
1313 string_from_display_spec (Lisp_Object spec)
1314 {
1315 if (CONSP (spec))
1316 {
1317 while (CONSP (spec))
1318 {
1319 if (STRINGP (XCAR (spec)))
1320 return XCAR (spec);
1321 spec = XCDR (spec);
1322 }
1323 }
1324 else if (VECTORP (spec))
1325 {
1326 ptrdiff_t i;
1327
1328 for (i = 0; i < ASIZE (spec); i++)
1329 {
1330 if (STRINGP (AREF (spec, i)))
1331 return AREF (spec, i);
1332 }
1333 return Qnil;
1334 }
1335
1336 return spec;
1337 }
1338
1339
1340 /* Limit insanely large values of W->hscroll on frame F to the largest
1341 value that will still prevent first_visible_x and last_visible_x of
1342 'struct it' from overflowing an int. */
1343 static int
1344 window_hscroll_limited (struct window *w, struct frame *f)
1345 {
1346 ptrdiff_t window_hscroll = w->hscroll;
1347 int window_text_width = window_box_width (w, TEXT_AREA);
1348 int colwidth = FRAME_COLUMN_WIDTH (f);
1349
1350 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1351 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1352
1353 return window_hscroll;
1354 }
1355
1356 /* Return 1 if position CHARPOS is visible in window W.
1357 CHARPOS < 0 means return info about WINDOW_END position.
1358 If visible, set *X and *Y to pixel coordinates of top left corner.
1359 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1360 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1361
1362 int
1363 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1364 int *rtop, int *rbot, int *rowh, int *vpos)
1365 {
1366 struct it it;
1367 void *itdata = bidi_shelve_cache ();
1368 struct text_pos top;
1369 int visible_p = 0;
1370 struct buffer *old_buffer = NULL;
1371
1372 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1373 return visible_p;
1374
1375 if (XBUFFER (w->contents) != current_buffer)
1376 {
1377 old_buffer = current_buffer;
1378 set_buffer_internal_1 (XBUFFER (w->contents));
1379 }
1380
1381 SET_TEXT_POS_FROM_MARKER (top, w->start);
1382 /* Scrolling a minibuffer window via scroll bar when the echo area
1383 shows long text sometimes resets the minibuffer contents behind
1384 our backs. */
1385 if (CHARPOS (top) > ZV)
1386 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1387
1388 /* Compute exact mode line heights. */
1389 if (WINDOW_WANTS_MODELINE_P (w))
1390 w->mode_line_height
1391 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1392 BVAR (current_buffer, mode_line_format));
1393
1394 if (WINDOW_WANTS_HEADER_LINE_P (w))
1395 w->header_line_height
1396 = display_mode_line (w, HEADER_LINE_FACE_ID,
1397 BVAR (current_buffer, header_line_format));
1398
1399 start_display (&it, w, top);
1400 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1401 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1402
1403 if (charpos >= 0
1404 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1405 && IT_CHARPOS (it) >= charpos)
1406 /* When scanning backwards under bidi iteration, move_it_to
1407 stops at or _before_ CHARPOS, because it stops at or to
1408 the _right_ of the character at CHARPOS. */
1409 || (it.bidi_p && it.bidi_it.scan_dir == -1
1410 && IT_CHARPOS (it) <= charpos)))
1411 {
1412 /* We have reached CHARPOS, or passed it. How the call to
1413 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1414 or covered by a display property, move_it_to stops at the end
1415 of the invisible text, to the right of CHARPOS. (ii) If
1416 CHARPOS is in a display vector, move_it_to stops on its last
1417 glyph. */
1418 int top_x = it.current_x;
1419 int top_y = it.current_y;
1420 /* Calling line_bottom_y may change it.method, it.position, etc. */
1421 enum it_method it_method = it.method;
1422 int bottom_y = (last_height = 0, line_bottom_y (&it));
1423 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1424
1425 if (top_y < window_top_y)
1426 visible_p = bottom_y > window_top_y;
1427 else if (top_y < it.last_visible_y)
1428 visible_p = true;
1429 if (bottom_y >= it.last_visible_y
1430 && it.bidi_p && it.bidi_it.scan_dir == -1
1431 && IT_CHARPOS (it) < charpos)
1432 {
1433 /* When the last line of the window is scanned backwards
1434 under bidi iteration, we could be duped into thinking
1435 that we have passed CHARPOS, when in fact move_it_to
1436 simply stopped short of CHARPOS because it reached
1437 last_visible_y. To see if that's what happened, we call
1438 move_it_to again with a slightly larger vertical limit,
1439 and see if it actually moved vertically; if it did, we
1440 didn't really reach CHARPOS, which is beyond window end. */
1441 struct it save_it = it;
1442 /* Why 10? because we don't know how many canonical lines
1443 will the height of the next line(s) be. So we guess. */
1444 int ten_more_lines = 10 * default_line_pixel_height (w);
1445
1446 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1447 MOVE_TO_POS | MOVE_TO_Y);
1448 if (it.current_y > top_y)
1449 visible_p = 0;
1450
1451 it = save_it;
1452 }
1453 if (visible_p)
1454 {
1455 if (it_method == GET_FROM_DISPLAY_VECTOR)
1456 {
1457 /* We stopped on the last glyph of a display vector.
1458 Try and recompute. Hack alert! */
1459 if (charpos < 2 || top.charpos >= charpos)
1460 top_x = it.glyph_row->x;
1461 else
1462 {
1463 struct it it2, it2_prev;
1464 /* The idea is to get to the previous buffer
1465 position, consume the character there, and use
1466 the pixel coordinates we get after that. But if
1467 the previous buffer position is also displayed
1468 from a display vector, we need to consume all of
1469 the glyphs from that display vector. */
1470 start_display (&it2, w, top);
1471 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1472 /* If we didn't get to CHARPOS - 1, there's some
1473 replacing display property at that position, and
1474 we stopped after it. That is exactly the place
1475 whose coordinates we want. */
1476 if (IT_CHARPOS (it2) != charpos - 1)
1477 it2_prev = it2;
1478 else
1479 {
1480 /* Iterate until we get out of the display
1481 vector that displays the character at
1482 CHARPOS - 1. */
1483 do {
1484 get_next_display_element (&it2);
1485 PRODUCE_GLYPHS (&it2);
1486 it2_prev = it2;
1487 set_iterator_to_next (&it2, 1);
1488 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1489 && IT_CHARPOS (it2) < charpos);
1490 }
1491 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1492 || it2_prev.current_x > it2_prev.last_visible_x)
1493 top_x = it.glyph_row->x;
1494 else
1495 {
1496 top_x = it2_prev.current_x;
1497 top_y = it2_prev.current_y;
1498 }
1499 }
1500 }
1501 else if (IT_CHARPOS (it) != charpos)
1502 {
1503 Lisp_Object cpos = make_number (charpos);
1504 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1505 Lisp_Object string = string_from_display_spec (spec);
1506 struct text_pos tpos;
1507 int replacing_spec_p;
1508 bool newline_in_string
1509 = (STRINGP (string)
1510 && memchr (SDATA (string), '\n', SBYTES (string)));
1511
1512 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1513 replacing_spec_p
1514 = (!NILP (spec)
1515 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1516 charpos, FRAME_WINDOW_P (it.f)));
1517 /* The tricky code below is needed because there's a
1518 discrepancy between move_it_to and how we set cursor
1519 when PT is at the beginning of a portion of text
1520 covered by a display property or an overlay with a
1521 display property, or the display line ends in a
1522 newline from a display string. move_it_to will stop
1523 _after_ such display strings, whereas
1524 set_cursor_from_row conspires with cursor_row_p to
1525 place the cursor on the first glyph produced from the
1526 display string. */
1527
1528 /* We have overshoot PT because it is covered by a
1529 display property that replaces the text it covers.
1530 If the string includes embedded newlines, we are also
1531 in the wrong display line. Backtrack to the correct
1532 line, where the display property begins. */
1533 if (replacing_spec_p)
1534 {
1535 Lisp_Object startpos, endpos;
1536 EMACS_INT start, end;
1537 struct it it3;
1538 int it3_moved;
1539
1540 /* Find the first and the last buffer positions
1541 covered by the display string. */
1542 endpos =
1543 Fnext_single_char_property_change (cpos, Qdisplay,
1544 Qnil, Qnil);
1545 startpos =
1546 Fprevious_single_char_property_change (endpos, Qdisplay,
1547 Qnil, Qnil);
1548 start = XFASTINT (startpos);
1549 end = XFASTINT (endpos);
1550 /* Move to the last buffer position before the
1551 display property. */
1552 start_display (&it3, w, top);
1553 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1554 /* Move forward one more line if the position before
1555 the display string is a newline or if it is the
1556 rightmost character on a line that is
1557 continued or word-wrapped. */
1558 if (it3.method == GET_FROM_BUFFER
1559 && (it3.c == '\n'
1560 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1561 move_it_by_lines (&it3, 1);
1562 else if (move_it_in_display_line_to (&it3, -1,
1563 it3.current_x
1564 + it3.pixel_width,
1565 MOVE_TO_X)
1566 == MOVE_LINE_CONTINUED)
1567 {
1568 move_it_by_lines (&it3, 1);
1569 /* When we are under word-wrap, the #$@%!
1570 move_it_by_lines moves 2 lines, so we need to
1571 fix that up. */
1572 if (it3.line_wrap == WORD_WRAP)
1573 move_it_by_lines (&it3, -1);
1574 }
1575
1576 /* Record the vertical coordinate of the display
1577 line where we wound up. */
1578 top_y = it3.current_y;
1579 if (it3.bidi_p)
1580 {
1581 /* When characters are reordered for display,
1582 the character displayed to the left of the
1583 display string could be _after_ the display
1584 property in the logical order. Use the
1585 smallest vertical position of these two. */
1586 start_display (&it3, w, top);
1587 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1588 if (it3.current_y < top_y)
1589 top_y = it3.current_y;
1590 }
1591 /* Move from the top of the window to the beginning
1592 of the display line where the display string
1593 begins. */
1594 start_display (&it3, w, top);
1595 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1596 /* If it3_moved stays zero after the 'while' loop
1597 below, that means we already were at a newline
1598 before the loop (e.g., the display string begins
1599 with a newline), so we don't need to (and cannot)
1600 inspect the glyphs of it3.glyph_row, because
1601 PRODUCE_GLYPHS will not produce anything for a
1602 newline, and thus it3.glyph_row stays at its
1603 stale content it got at top of the window. */
1604 it3_moved = 0;
1605 /* Finally, advance the iterator until we hit the
1606 first display element whose character position is
1607 CHARPOS, or until the first newline from the
1608 display string, which signals the end of the
1609 display line. */
1610 while (get_next_display_element (&it3))
1611 {
1612 PRODUCE_GLYPHS (&it3);
1613 if (IT_CHARPOS (it3) == charpos
1614 || ITERATOR_AT_END_OF_LINE_P (&it3))
1615 break;
1616 it3_moved = 1;
1617 set_iterator_to_next (&it3, 0);
1618 }
1619 top_x = it3.current_x - it3.pixel_width;
1620 /* Normally, we would exit the above loop because we
1621 found the display element whose character
1622 position is CHARPOS. For the contingency that we
1623 didn't, and stopped at the first newline from the
1624 display string, move back over the glyphs
1625 produced from the string, until we find the
1626 rightmost glyph not from the string. */
1627 if (it3_moved
1628 && newline_in_string
1629 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1630 {
1631 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1632 + it3.glyph_row->used[TEXT_AREA];
1633
1634 while (EQ ((g - 1)->object, string))
1635 {
1636 --g;
1637 top_x -= g->pixel_width;
1638 }
1639 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1640 + it3.glyph_row->used[TEXT_AREA]);
1641 }
1642 }
1643 }
1644
1645 *x = top_x;
1646 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1647 *rtop = max (0, window_top_y - top_y);
1648 *rbot = max (0, bottom_y - it.last_visible_y);
1649 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1650 - max (top_y, window_top_y)));
1651 *vpos = it.vpos;
1652 }
1653 }
1654 else
1655 {
1656 /* We were asked to provide info about WINDOW_END. */
1657 struct it it2;
1658 void *it2data = NULL;
1659
1660 SAVE_IT (it2, it, it2data);
1661 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1662 move_it_by_lines (&it, 1);
1663 if (charpos < IT_CHARPOS (it)
1664 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1665 {
1666 visible_p = true;
1667 RESTORE_IT (&it2, &it2, it2data);
1668 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1669 *x = it2.current_x;
1670 *y = it2.current_y + it2.max_ascent - it2.ascent;
1671 *rtop = max (0, -it2.current_y);
1672 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1673 - it.last_visible_y));
1674 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1675 it.last_visible_y)
1676 - max (it2.current_y,
1677 WINDOW_HEADER_LINE_HEIGHT (w))));
1678 *vpos = it2.vpos;
1679 }
1680 else
1681 bidi_unshelve_cache (it2data, 1);
1682 }
1683 bidi_unshelve_cache (itdata, 0);
1684
1685 if (old_buffer)
1686 set_buffer_internal_1 (old_buffer);
1687
1688 if (visible_p && w->hscroll > 0)
1689 *x -=
1690 window_hscroll_limited (w, WINDOW_XFRAME (w))
1691 * WINDOW_FRAME_COLUMN_WIDTH (w);
1692
1693 #if 0
1694 /* Debugging code. */
1695 if (visible_p)
1696 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1697 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1698 else
1699 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1700 #endif
1701
1702 return visible_p;
1703 }
1704
1705
1706 /* Return the next character from STR. Return in *LEN the length of
1707 the character. This is like STRING_CHAR_AND_LENGTH but never
1708 returns an invalid character. If we find one, we return a `?', but
1709 with the length of the invalid character. */
1710
1711 static int
1712 string_char_and_length (const unsigned char *str, int *len)
1713 {
1714 int c;
1715
1716 c = STRING_CHAR_AND_LENGTH (str, *len);
1717 if (!CHAR_VALID_P (c))
1718 /* We may not change the length here because other places in Emacs
1719 don't use this function, i.e. they silently accept invalid
1720 characters. */
1721 c = '?';
1722
1723 return c;
1724 }
1725
1726
1727
1728 /* Given a position POS containing a valid character and byte position
1729 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1730
1731 static struct text_pos
1732 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1733 {
1734 eassert (STRINGP (string) && nchars >= 0);
1735
1736 if (STRING_MULTIBYTE (string))
1737 {
1738 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1739 int len;
1740
1741 while (nchars--)
1742 {
1743 string_char_and_length (p, &len);
1744 p += len;
1745 CHARPOS (pos) += 1;
1746 BYTEPOS (pos) += len;
1747 }
1748 }
1749 else
1750 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1751
1752 return pos;
1753 }
1754
1755
1756 /* Value is the text position, i.e. character and byte position,
1757 for character position CHARPOS in STRING. */
1758
1759 static struct text_pos
1760 string_pos (ptrdiff_t charpos, Lisp_Object string)
1761 {
1762 struct text_pos pos;
1763 eassert (STRINGP (string));
1764 eassert (charpos >= 0);
1765 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1766 return pos;
1767 }
1768
1769
1770 /* Value is a text position, i.e. character and byte position, for
1771 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1772 means recognize multibyte characters. */
1773
1774 static struct text_pos
1775 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1776 {
1777 struct text_pos pos;
1778
1779 eassert (s != NULL);
1780 eassert (charpos >= 0);
1781
1782 if (multibyte_p)
1783 {
1784 int len;
1785
1786 SET_TEXT_POS (pos, 0, 0);
1787 while (charpos--)
1788 {
1789 string_char_and_length ((const unsigned char *) s, &len);
1790 s += len;
1791 CHARPOS (pos) += 1;
1792 BYTEPOS (pos) += len;
1793 }
1794 }
1795 else
1796 SET_TEXT_POS (pos, charpos, charpos);
1797
1798 return pos;
1799 }
1800
1801
1802 /* Value is the number of characters in C string S. MULTIBYTE_P
1803 non-zero means recognize multibyte characters. */
1804
1805 static ptrdiff_t
1806 number_of_chars (const char *s, bool multibyte_p)
1807 {
1808 ptrdiff_t nchars;
1809
1810 if (multibyte_p)
1811 {
1812 ptrdiff_t rest = strlen (s);
1813 int len;
1814 const unsigned char *p = (const unsigned char *) s;
1815
1816 for (nchars = 0; rest > 0; ++nchars)
1817 {
1818 string_char_and_length (p, &len);
1819 rest -= len, p += len;
1820 }
1821 }
1822 else
1823 nchars = strlen (s);
1824
1825 return nchars;
1826 }
1827
1828
1829 /* Compute byte position NEWPOS->bytepos corresponding to
1830 NEWPOS->charpos. POS is a known position in string STRING.
1831 NEWPOS->charpos must be >= POS.charpos. */
1832
1833 static void
1834 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1835 {
1836 eassert (STRINGP (string));
1837 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1838
1839 if (STRING_MULTIBYTE (string))
1840 *newpos = string_pos_nchars_ahead (pos, string,
1841 CHARPOS (*newpos) - CHARPOS (pos));
1842 else
1843 BYTEPOS (*newpos) = CHARPOS (*newpos);
1844 }
1845
1846 /* EXPORT:
1847 Return an estimation of the pixel height of mode or header lines on
1848 frame F. FACE_ID specifies what line's height to estimate. */
1849
1850 int
1851 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1852 {
1853 #ifdef HAVE_WINDOW_SYSTEM
1854 if (FRAME_WINDOW_P (f))
1855 {
1856 int height = FONT_HEIGHT (FRAME_FONT (f));
1857
1858 /* This function is called so early when Emacs starts that the face
1859 cache and mode line face are not yet initialized. */
1860 if (FRAME_FACE_CACHE (f))
1861 {
1862 struct face *face = FACE_FROM_ID (f, face_id);
1863 if (face)
1864 {
1865 if (face->font)
1866 height = FONT_HEIGHT (face->font);
1867 if (face->box_line_width > 0)
1868 height += 2 * face->box_line_width;
1869 }
1870 }
1871
1872 return height;
1873 }
1874 #endif
1875
1876 return 1;
1877 }
1878
1879 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1880 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1881 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1882 not force the value into range. */
1883
1884 void
1885 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1886 int *x, int *y, NativeRectangle *bounds, int noclip)
1887 {
1888
1889 #ifdef HAVE_WINDOW_SYSTEM
1890 if (FRAME_WINDOW_P (f))
1891 {
1892 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1893 even for negative values. */
1894 if (pix_x < 0)
1895 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1896 if (pix_y < 0)
1897 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1898
1899 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1900 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1901
1902 if (bounds)
1903 STORE_NATIVE_RECT (*bounds,
1904 FRAME_COL_TO_PIXEL_X (f, pix_x),
1905 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1906 FRAME_COLUMN_WIDTH (f) - 1,
1907 FRAME_LINE_HEIGHT (f) - 1);
1908
1909 /* PXW: Should we clip pixelized before converting to
1910 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 pixelwise:
2488 gx += WINDOW_LEFT_EDGE_X (w);
2489 gy += WINDOW_TOP_EDGE_Y (w);
2490
2491 store_rect:
2492 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2493
2494 /* Visible feedback for debugging. */
2495 #if 0
2496 #if HAVE_X_WINDOWS
2497 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2498 f->output_data.x->normal_gc,
2499 gx, gy, width, height);
2500 #endif
2501 #endif
2502 }
2503
2504
2505 #endif /* HAVE_WINDOW_SYSTEM */
2506
2507 static void
2508 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2509 {
2510 eassert (w);
2511 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2512 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2513 w->window_end_vpos
2514 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2515 }
2516
2517 /***********************************************************************
2518 Lisp form evaluation
2519 ***********************************************************************/
2520
2521 /* Error handler for safe_eval and safe_call. */
2522
2523 static Lisp_Object
2524 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2525 {
2526 add_to_log ("Error during redisplay: %S signaled %S",
2527 Flist (nargs, args), arg);
2528 return Qnil;
2529 }
2530
2531 /* Call function FUNC with the rest of NARGS - 1 arguments
2532 following. Return the result, or nil if something went
2533 wrong. Prevent redisplay during the evaluation. */
2534
2535 Lisp_Object
2536 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2537 {
2538 Lisp_Object val;
2539
2540 if (inhibit_eval_during_redisplay)
2541 val = Qnil;
2542 else
2543 {
2544 va_list ap;
2545 ptrdiff_t i;
2546 ptrdiff_t count = SPECPDL_INDEX ();
2547 struct gcpro gcpro1;
2548 Lisp_Object *args = alloca (nargs * word_size);
2549
2550 args[0] = func;
2551 va_start (ap, func);
2552 for (i = 1; i < nargs; i++)
2553 args[i] = va_arg (ap, Lisp_Object);
2554 va_end (ap);
2555
2556 GCPRO1 (args[0]);
2557 gcpro1.nvars = nargs;
2558 specbind (Qinhibit_redisplay, Qt);
2559 /* Use Qt to ensure debugger does not run,
2560 so there is no possibility of wanting to redisplay. */
2561 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2562 safe_eval_handler);
2563 UNGCPRO;
2564 val = unbind_to (count, val);
2565 }
2566
2567 return val;
2568 }
2569
2570
2571 /* Call function FN with one argument ARG.
2572 Return the result, or nil if something went wrong. */
2573
2574 Lisp_Object
2575 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2576 {
2577 return safe_call (2, fn, arg);
2578 }
2579
2580 static Lisp_Object Qeval;
2581
2582 Lisp_Object
2583 safe_eval (Lisp_Object sexpr)
2584 {
2585 return safe_call1 (Qeval, sexpr);
2586 }
2587
2588 /* Call function FN with two arguments ARG1 and ARG2.
2589 Return the result, or nil if something went wrong. */
2590
2591 Lisp_Object
2592 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2593 {
2594 return safe_call (3, fn, arg1, arg2);
2595 }
2596
2597
2598 \f
2599 /***********************************************************************
2600 Debugging
2601 ***********************************************************************/
2602
2603 #if 0
2604
2605 /* Define CHECK_IT to perform sanity checks on iterators.
2606 This is for debugging. It is too slow to do unconditionally. */
2607
2608 static void
2609 check_it (struct it *it)
2610 {
2611 if (it->method == GET_FROM_STRING)
2612 {
2613 eassert (STRINGP (it->string));
2614 eassert (IT_STRING_CHARPOS (*it) >= 0);
2615 }
2616 else
2617 {
2618 eassert (IT_STRING_CHARPOS (*it) < 0);
2619 if (it->method == GET_FROM_BUFFER)
2620 {
2621 /* Check that character and byte positions agree. */
2622 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2623 }
2624 }
2625
2626 if (it->dpvec)
2627 eassert (it->current.dpvec_index >= 0);
2628 else
2629 eassert (it->current.dpvec_index < 0);
2630 }
2631
2632 #define CHECK_IT(IT) check_it ((IT))
2633
2634 #else /* not 0 */
2635
2636 #define CHECK_IT(IT) (void) 0
2637
2638 #endif /* not 0 */
2639
2640
2641 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2642
2643 /* Check that the window end of window W is what we expect it
2644 to be---the last row in the current matrix displaying text. */
2645
2646 static void
2647 check_window_end (struct window *w)
2648 {
2649 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2650 {
2651 struct glyph_row *row;
2652 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2653 !row->enabled_p
2654 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2655 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2656 }
2657 }
2658
2659 #define CHECK_WINDOW_END(W) check_window_end ((W))
2660
2661 #else
2662
2663 #define CHECK_WINDOW_END(W) (void) 0
2664
2665 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2666
2667 /***********************************************************************
2668 Iterator initialization
2669 ***********************************************************************/
2670
2671 /* Initialize IT for displaying current_buffer in window W, starting
2672 at character position CHARPOS. CHARPOS < 0 means that no buffer
2673 position is specified which is useful when the iterator is assigned
2674 a position later. BYTEPOS is the byte position corresponding to
2675 CHARPOS.
2676
2677 If ROW is not null, calls to produce_glyphs with IT as parameter
2678 will produce glyphs in that row.
2679
2680 BASE_FACE_ID is the id of a base face to use. It must be one of
2681 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2682 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2683 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2684
2685 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2686 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2687 will be initialized to use the corresponding mode line glyph row of
2688 the desired matrix of W. */
2689
2690 void
2691 init_iterator (struct it *it, struct window *w,
2692 ptrdiff_t charpos, ptrdiff_t bytepos,
2693 struct glyph_row *row, enum face_id base_face_id)
2694 {
2695 enum face_id remapped_base_face_id = base_face_id;
2696
2697 /* Some precondition checks. */
2698 eassert (w != NULL && it != NULL);
2699 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2700 && charpos <= ZV));
2701
2702 /* If face attributes have been changed since the last redisplay,
2703 free realized faces now because they depend on face definitions
2704 that might have changed. Don't free faces while there might be
2705 desired matrices pending which reference these faces. */
2706 if (face_change_count && !inhibit_free_realized_faces)
2707 {
2708 face_change_count = 0;
2709 free_all_realized_faces (Qnil);
2710 }
2711
2712 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2713 if (! NILP (Vface_remapping_alist))
2714 remapped_base_face_id
2715 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2716
2717 /* Use one of the mode line rows of W's desired matrix if
2718 appropriate. */
2719 if (row == NULL)
2720 {
2721 if (base_face_id == MODE_LINE_FACE_ID
2722 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2723 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2724 else if (base_face_id == HEADER_LINE_FACE_ID)
2725 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2726 }
2727
2728 /* Clear IT. */
2729 memset (it, 0, sizeof *it);
2730 it->current.overlay_string_index = -1;
2731 it->current.dpvec_index = -1;
2732 it->base_face_id = remapped_base_face_id;
2733 it->string = Qnil;
2734 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2735 it->paragraph_embedding = L2R;
2736 it->bidi_it.string.lstring = Qnil;
2737 it->bidi_it.string.s = NULL;
2738 it->bidi_it.string.bufpos = 0;
2739 it->bidi_it.w = w;
2740
2741 /* The window in which we iterate over current_buffer: */
2742 XSETWINDOW (it->window, w);
2743 it->w = w;
2744 it->f = XFRAME (w->frame);
2745
2746 it->cmp_it.id = -1;
2747
2748 /* Extra space between lines (on window systems only). */
2749 if (base_face_id == DEFAULT_FACE_ID
2750 && FRAME_WINDOW_P (it->f))
2751 {
2752 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2753 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2754 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2755 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2756 * FRAME_LINE_HEIGHT (it->f));
2757 else if (it->f->extra_line_spacing > 0)
2758 it->extra_line_spacing = it->f->extra_line_spacing;
2759 it->max_extra_line_spacing = 0;
2760 }
2761
2762 /* If realized faces have been removed, e.g. because of face
2763 attribute changes of named faces, recompute them. When running
2764 in batch mode, the face cache of the initial frame is null. If
2765 we happen to get called, make a dummy face cache. */
2766 if (FRAME_FACE_CACHE (it->f) == NULL)
2767 init_frame_faces (it->f);
2768 if (FRAME_FACE_CACHE (it->f)->used == 0)
2769 recompute_basic_faces (it->f);
2770
2771 /* Current value of the `slice', `space-width', and 'height' properties. */
2772 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2773 it->space_width = Qnil;
2774 it->font_height = Qnil;
2775 it->override_ascent = -1;
2776
2777 /* Are control characters displayed as `^C'? */
2778 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2779
2780 /* -1 means everything between a CR and the following line end
2781 is invisible. >0 means lines indented more than this value are
2782 invisible. */
2783 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2784 ? (clip_to_bounds
2785 (-1, XINT (BVAR (current_buffer, selective_display)),
2786 PTRDIFF_MAX))
2787 : (!NILP (BVAR (current_buffer, selective_display))
2788 ? -1 : 0));
2789 it->selective_display_ellipsis_p
2790 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2791
2792 /* Display table to use. */
2793 it->dp = window_display_table (w);
2794
2795 /* Are multibyte characters enabled in current_buffer? */
2796 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2797
2798 /* Get the position at which the redisplay_end_trigger hook should
2799 be run, if it is to be run at all. */
2800 if (MARKERP (w->redisplay_end_trigger)
2801 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2802 it->redisplay_end_trigger_charpos
2803 = marker_position (w->redisplay_end_trigger);
2804 else if (INTEGERP (w->redisplay_end_trigger))
2805 it->redisplay_end_trigger_charpos =
2806 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2807
2808 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2809
2810 /* Are lines in the display truncated? */
2811 if (base_face_id != DEFAULT_FACE_ID
2812 || it->w->hscroll
2813 || (! WINDOW_FULL_WIDTH_P (it->w)
2814 && ((!NILP (Vtruncate_partial_width_windows)
2815 && !INTEGERP (Vtruncate_partial_width_windows))
2816 || (INTEGERP (Vtruncate_partial_width_windows)
2817 /* PXW: Shall we do something about this ? */
2818 && (WINDOW_TOTAL_COLS (it->w)
2819 < XINT (Vtruncate_partial_width_windows))))))
2820 it->line_wrap = TRUNCATE;
2821 else if (NILP (BVAR (current_buffer, truncate_lines)))
2822 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2823 ? WINDOW_WRAP : WORD_WRAP;
2824 else
2825 it->line_wrap = TRUNCATE;
2826
2827 /* Get dimensions of truncation and continuation glyphs. These are
2828 displayed as fringe bitmaps under X, but we need them for such
2829 frames when the fringes are turned off. But leave the dimensions
2830 zero for tooltip frames, as these glyphs look ugly there and also
2831 sabotage calculations of tooltip dimensions in x-show-tip. */
2832 #ifdef HAVE_WINDOW_SYSTEM
2833 if (!(FRAME_WINDOW_P (it->f)
2834 && FRAMEP (tip_frame)
2835 && it->f == XFRAME (tip_frame)))
2836 #endif
2837 {
2838 if (it->line_wrap == TRUNCATE)
2839 {
2840 /* We will need the truncation glyph. */
2841 eassert (it->glyph_row == NULL);
2842 produce_special_glyphs (it, IT_TRUNCATION);
2843 it->truncation_pixel_width = it->pixel_width;
2844 }
2845 else
2846 {
2847 /* We will need the continuation glyph. */
2848 eassert (it->glyph_row == NULL);
2849 produce_special_glyphs (it, IT_CONTINUATION);
2850 it->continuation_pixel_width = it->pixel_width;
2851 }
2852 }
2853
2854 /* Reset these values to zero because the produce_special_glyphs
2855 above has changed them. */
2856 it->pixel_width = it->ascent = it->descent = 0;
2857 it->phys_ascent = it->phys_descent = 0;
2858
2859 /* Set this after getting the dimensions of truncation and
2860 continuation glyphs, so that we don't produce glyphs when calling
2861 produce_special_glyphs, above. */
2862 it->glyph_row = row;
2863 it->area = TEXT_AREA;
2864
2865 /* Forget any previous info about this row being reversed. */
2866 if (it->glyph_row)
2867 it->glyph_row->reversed_p = 0;
2868
2869 /* Get the dimensions of the display area. The display area
2870 consists of the visible window area plus a horizontally scrolled
2871 part to the left of the window. All x-values are relative to the
2872 start of this total display area. */
2873 if (base_face_id != DEFAULT_FACE_ID)
2874 {
2875 /* Mode lines, menu bar in terminal frames. */
2876 it->first_visible_x = 0;
2877 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2878 }
2879 else
2880 {
2881 it->first_visible_x
2882 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2883 it->last_visible_x = (it->first_visible_x
2884 + window_box_width (w, TEXT_AREA));
2885
2886 /* If we truncate lines, leave room for the truncation glyph(s) at
2887 the right margin. Otherwise, leave room for the continuation
2888 glyph(s). Done only if the window has no fringes. Since we
2889 don't know at this point whether there will be any R2L lines in
2890 the window, we reserve space for truncation/continuation glyphs
2891 even if only one of the fringes is absent. */
2892 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2893 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2894 {
2895 if (it->line_wrap == TRUNCATE)
2896 it->last_visible_x -= it->truncation_pixel_width;
2897 else
2898 it->last_visible_x -= it->continuation_pixel_width;
2899 }
2900
2901 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2902 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2903 }
2904
2905 /* Leave room for a border glyph. */
2906 if (!FRAME_WINDOW_P (it->f)
2907 && !WINDOW_RIGHTMOST_P (it->w))
2908 it->last_visible_x -= 1;
2909
2910 it->last_visible_y = window_text_bottom_y (w);
2911
2912 /* For mode lines and alike, arrange for the first glyph having a
2913 left box line if the face specifies a box. */
2914 if (base_face_id != DEFAULT_FACE_ID)
2915 {
2916 struct face *face;
2917
2918 it->face_id = remapped_base_face_id;
2919
2920 /* If we have a boxed mode line, make the first character appear
2921 with a left box line. */
2922 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2923 if (face->box != FACE_NO_BOX)
2924 it->start_of_box_run_p = true;
2925 }
2926
2927 /* If a buffer position was specified, set the iterator there,
2928 getting overlays and face properties from that position. */
2929 if (charpos >= BUF_BEG (current_buffer))
2930 {
2931 it->end_charpos = ZV;
2932 eassert (charpos == BYTE_TO_CHAR (bytepos));
2933 IT_CHARPOS (*it) = charpos;
2934 IT_BYTEPOS (*it) = bytepos;
2935
2936 /* We will rely on `reseat' to set this up properly, via
2937 handle_face_prop. */
2938 it->face_id = it->base_face_id;
2939
2940 it->start = it->current;
2941 /* Do we need to reorder bidirectional text? Not if this is a
2942 unibyte buffer: by definition, none of the single-byte
2943 characters are strong R2L, so no reordering is needed. And
2944 bidi.c doesn't support unibyte buffers anyway. Also, don't
2945 reorder while we are loading loadup.el, since the tables of
2946 character properties needed for reordering are not yet
2947 available. */
2948 it->bidi_p =
2949 NILP (Vpurify_flag)
2950 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2951 && it->multibyte_p;
2952
2953 /* If we are to reorder bidirectional text, init the bidi
2954 iterator. */
2955 if (it->bidi_p)
2956 {
2957 /* Note the paragraph direction that this buffer wants to
2958 use. */
2959 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2960 Qleft_to_right))
2961 it->paragraph_embedding = L2R;
2962 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2963 Qright_to_left))
2964 it->paragraph_embedding = R2L;
2965 else
2966 it->paragraph_embedding = NEUTRAL_DIR;
2967 bidi_unshelve_cache (NULL, 0);
2968 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2969 &it->bidi_it);
2970 }
2971
2972 /* Compute faces etc. */
2973 reseat (it, it->current.pos, 1);
2974 }
2975
2976 CHECK_IT (it);
2977 }
2978
2979
2980 /* Initialize IT for the display of window W with window start POS. */
2981
2982 void
2983 start_display (struct it *it, struct window *w, struct text_pos pos)
2984 {
2985 struct glyph_row *row;
2986 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2987
2988 row = w->desired_matrix->rows + first_vpos;
2989 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2990 it->first_vpos = first_vpos;
2991
2992 /* Don't reseat to previous visible line start if current start
2993 position is in a string or image. */
2994 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2995 {
2996 int start_at_line_beg_p;
2997 int first_y = it->current_y;
2998
2999 /* If window start is not at a line start, skip forward to POS to
3000 get the correct continuation lines width. */
3001 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3002 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3003 if (!start_at_line_beg_p)
3004 {
3005 int new_x;
3006
3007 reseat_at_previous_visible_line_start (it);
3008 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3009
3010 new_x = it->current_x + it->pixel_width;
3011
3012 /* If lines are continued, this line may end in the middle
3013 of a multi-glyph character (e.g. a control character
3014 displayed as \003, or in the middle of an overlay
3015 string). In this case move_it_to above will not have
3016 taken us to the start of the continuation line but to the
3017 end of the continued line. */
3018 if (it->current_x > 0
3019 && it->line_wrap != TRUNCATE /* Lines are continued. */
3020 && (/* And glyph doesn't fit on the line. */
3021 new_x > it->last_visible_x
3022 /* Or it fits exactly and we're on a window
3023 system frame. */
3024 || (new_x == it->last_visible_x
3025 && FRAME_WINDOW_P (it->f)
3026 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3027 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3028 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3029 {
3030 if ((it->current.dpvec_index >= 0
3031 || it->current.overlay_string_index >= 0)
3032 /* If we are on a newline from a display vector or
3033 overlay string, then we are already at the end of
3034 a screen line; no need to go to the next line in
3035 that case, as this line is not really continued.
3036 (If we do go to the next line, C-e will not DTRT.) */
3037 && it->c != '\n')
3038 {
3039 set_iterator_to_next (it, 1);
3040 move_it_in_display_line_to (it, -1, -1, 0);
3041 }
3042
3043 it->continuation_lines_width += it->current_x;
3044 }
3045 /* If the character at POS is displayed via a display
3046 vector, move_it_to above stops at the final glyph of
3047 IT->dpvec. To make the caller redisplay that character
3048 again (a.k.a. start at POS), we need to reset the
3049 dpvec_index to the beginning of IT->dpvec. */
3050 else if (it->current.dpvec_index >= 0)
3051 it->current.dpvec_index = 0;
3052
3053 /* We're starting a new display line, not affected by the
3054 height of the continued line, so clear the appropriate
3055 fields in the iterator structure. */
3056 it->max_ascent = it->max_descent = 0;
3057 it->max_phys_ascent = it->max_phys_descent = 0;
3058
3059 it->current_y = first_y;
3060 it->vpos = 0;
3061 it->current_x = it->hpos = 0;
3062 }
3063 }
3064 }
3065
3066
3067 /* Return 1 if POS is a position in ellipses displayed for invisible
3068 text. W is the window we display, for text property lookup. */
3069
3070 static int
3071 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3072 {
3073 Lisp_Object prop, window;
3074 int ellipses_p = 0;
3075 ptrdiff_t charpos = CHARPOS (pos->pos);
3076
3077 /* If POS specifies a position in a display vector, this might
3078 be for an ellipsis displayed for invisible text. We won't
3079 get the iterator set up for delivering that ellipsis unless
3080 we make sure that it gets aware of the invisible text. */
3081 if (pos->dpvec_index >= 0
3082 && pos->overlay_string_index < 0
3083 && CHARPOS (pos->string_pos) < 0
3084 && charpos > BEGV
3085 && (XSETWINDOW (window, w),
3086 prop = Fget_char_property (make_number (charpos),
3087 Qinvisible, window),
3088 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3089 {
3090 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3091 window);
3092 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3093 }
3094
3095 return ellipses_p;
3096 }
3097
3098
3099 /* Initialize IT for stepping through current_buffer in window W,
3100 starting at position POS that includes overlay string and display
3101 vector/ control character translation position information. Value
3102 is zero if there are overlay strings with newlines at POS. */
3103
3104 static int
3105 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3106 {
3107 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3108 int i, overlay_strings_with_newlines = 0;
3109
3110 /* If POS specifies a position in a display vector, this might
3111 be for an ellipsis displayed for invisible text. We won't
3112 get the iterator set up for delivering that ellipsis unless
3113 we make sure that it gets aware of the invisible text. */
3114 if (in_ellipses_for_invisible_text_p (pos, w))
3115 {
3116 --charpos;
3117 bytepos = 0;
3118 }
3119
3120 /* Keep in mind: the call to reseat in init_iterator skips invisible
3121 text, so we might end up at a position different from POS. This
3122 is only a problem when POS is a row start after a newline and an
3123 overlay starts there with an after-string, and the overlay has an
3124 invisible property. Since we don't skip invisible text in
3125 display_line and elsewhere immediately after consuming the
3126 newline before the row start, such a POS will not be in a string,
3127 but the call to init_iterator below will move us to the
3128 after-string. */
3129 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3130
3131 /* This only scans the current chunk -- it should scan all chunks.
3132 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3133 to 16 in 22.1 to make this a lesser problem. */
3134 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3135 {
3136 const char *s = SSDATA (it->overlay_strings[i]);
3137 const char *e = s + SBYTES (it->overlay_strings[i]);
3138
3139 while (s < e && *s != '\n')
3140 ++s;
3141
3142 if (s < e)
3143 {
3144 overlay_strings_with_newlines = 1;
3145 break;
3146 }
3147 }
3148
3149 /* If position is within an overlay string, set up IT to the right
3150 overlay string. */
3151 if (pos->overlay_string_index >= 0)
3152 {
3153 int relative_index;
3154
3155 /* If the first overlay string happens to have a `display'
3156 property for an image, the iterator will be set up for that
3157 image, and we have to undo that setup first before we can
3158 correct the overlay string index. */
3159 if (it->method == GET_FROM_IMAGE)
3160 pop_it (it);
3161
3162 /* We already have the first chunk of overlay strings in
3163 IT->overlay_strings. Load more until the one for
3164 pos->overlay_string_index is in IT->overlay_strings. */
3165 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3166 {
3167 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3168 it->current.overlay_string_index = 0;
3169 while (n--)
3170 {
3171 load_overlay_strings (it, 0);
3172 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3173 }
3174 }
3175
3176 it->current.overlay_string_index = pos->overlay_string_index;
3177 relative_index = (it->current.overlay_string_index
3178 % OVERLAY_STRING_CHUNK_SIZE);
3179 it->string = it->overlay_strings[relative_index];
3180 eassert (STRINGP (it->string));
3181 it->current.string_pos = pos->string_pos;
3182 it->method = GET_FROM_STRING;
3183 it->end_charpos = SCHARS (it->string);
3184 /* Set up the bidi iterator for this overlay string. */
3185 if (it->bidi_p)
3186 {
3187 it->bidi_it.string.lstring = it->string;
3188 it->bidi_it.string.s = NULL;
3189 it->bidi_it.string.schars = SCHARS (it->string);
3190 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3191 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3192 it->bidi_it.string.unibyte = !it->multibyte_p;
3193 it->bidi_it.w = it->w;
3194 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3195 FRAME_WINDOW_P (it->f), &it->bidi_it);
3196
3197 /* Synchronize the state of the bidi iterator with
3198 pos->string_pos. For any string position other than
3199 zero, this will be done automagically when we resume
3200 iteration over the string and get_visually_first_element
3201 is called. But if string_pos is zero, and the string is
3202 to be reordered for display, we need to resync manually,
3203 since it could be that the iteration state recorded in
3204 pos ended at string_pos of 0 moving backwards in string. */
3205 if (CHARPOS (pos->string_pos) == 0)
3206 {
3207 get_visually_first_element (it);
3208 if (IT_STRING_CHARPOS (*it) != 0)
3209 do {
3210 /* Paranoia. */
3211 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3212 bidi_move_to_visually_next (&it->bidi_it);
3213 } while (it->bidi_it.charpos != 0);
3214 }
3215 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3216 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3217 }
3218 }
3219
3220 if (CHARPOS (pos->string_pos) >= 0)
3221 {
3222 /* Recorded position is not in an overlay string, but in another
3223 string. This can only be a string from a `display' property.
3224 IT should already be filled with that string. */
3225 it->current.string_pos = pos->string_pos;
3226 eassert (STRINGP (it->string));
3227 if (it->bidi_p)
3228 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3229 FRAME_WINDOW_P (it->f), &it->bidi_it);
3230 }
3231
3232 /* Restore position in display vector translations, control
3233 character translations or ellipses. */
3234 if (pos->dpvec_index >= 0)
3235 {
3236 if (it->dpvec == NULL)
3237 get_next_display_element (it);
3238 eassert (it->dpvec && it->current.dpvec_index == 0);
3239 it->current.dpvec_index = pos->dpvec_index;
3240 }
3241
3242 CHECK_IT (it);
3243 return !overlay_strings_with_newlines;
3244 }
3245
3246
3247 /* Initialize IT for stepping through current_buffer in window W
3248 starting at ROW->start. */
3249
3250 static void
3251 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3252 {
3253 init_from_display_pos (it, w, &row->start);
3254 it->start = row->start;
3255 it->continuation_lines_width = row->continuation_lines_width;
3256 CHECK_IT (it);
3257 }
3258
3259
3260 /* Initialize IT for stepping through current_buffer in window W
3261 starting in the line following ROW, i.e. starting at ROW->end.
3262 Value is zero if there are overlay strings with newlines at ROW's
3263 end position. */
3264
3265 static int
3266 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3267 {
3268 int success = 0;
3269
3270 if (init_from_display_pos (it, w, &row->end))
3271 {
3272 if (row->continued_p)
3273 it->continuation_lines_width
3274 = row->continuation_lines_width + row->pixel_width;
3275 CHECK_IT (it);
3276 success = 1;
3277 }
3278
3279 return success;
3280 }
3281
3282
3283
3284 \f
3285 /***********************************************************************
3286 Text properties
3287 ***********************************************************************/
3288
3289 /* Called when IT reaches IT->stop_charpos. Handle text property and
3290 overlay changes. Set IT->stop_charpos to the next position where
3291 to stop. */
3292
3293 static void
3294 handle_stop (struct it *it)
3295 {
3296 enum prop_handled handled;
3297 int handle_overlay_change_p;
3298 struct props *p;
3299
3300 it->dpvec = NULL;
3301 it->current.dpvec_index = -1;
3302 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3303 it->ignore_overlay_strings_at_pos_p = 0;
3304 it->ellipsis_p = 0;
3305
3306 /* Use face of preceding text for ellipsis (if invisible) */
3307 if (it->selective_display_ellipsis_p)
3308 it->saved_face_id = it->face_id;
3309
3310 do
3311 {
3312 handled = HANDLED_NORMALLY;
3313
3314 /* Call text property handlers. */
3315 for (p = it_props; p->handler; ++p)
3316 {
3317 handled = p->handler (it);
3318
3319 if (handled == HANDLED_RECOMPUTE_PROPS)
3320 break;
3321 else if (handled == HANDLED_RETURN)
3322 {
3323 /* We still want to show before and after strings from
3324 overlays even if the actual buffer text is replaced. */
3325 if (!handle_overlay_change_p
3326 || it->sp > 1
3327 /* Don't call get_overlay_strings_1 if we already
3328 have overlay strings loaded, because doing so
3329 will load them again and push the iterator state
3330 onto the stack one more time, which is not
3331 expected by the rest of the code that processes
3332 overlay strings. */
3333 || (it->current.overlay_string_index < 0
3334 ? !get_overlay_strings_1 (it, 0, 0)
3335 : 0))
3336 {
3337 if (it->ellipsis_p)
3338 setup_for_ellipsis (it, 0);
3339 /* When handling a display spec, we might load an
3340 empty string. In that case, discard it here. We
3341 used to discard it in handle_single_display_spec,
3342 but that causes get_overlay_strings_1, above, to
3343 ignore overlay strings that we must check. */
3344 if (STRINGP (it->string) && !SCHARS (it->string))
3345 pop_it (it);
3346 return;
3347 }
3348 else if (STRINGP (it->string) && !SCHARS (it->string))
3349 pop_it (it);
3350 else
3351 {
3352 it->ignore_overlay_strings_at_pos_p = true;
3353 it->string_from_display_prop_p = 0;
3354 it->from_disp_prop_p = 0;
3355 handle_overlay_change_p = 0;
3356 }
3357 handled = HANDLED_RECOMPUTE_PROPS;
3358 break;
3359 }
3360 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3361 handle_overlay_change_p = 0;
3362 }
3363
3364 if (handled != HANDLED_RECOMPUTE_PROPS)
3365 {
3366 /* Don't check for overlay strings below when set to deliver
3367 characters from a display vector. */
3368 if (it->method == GET_FROM_DISPLAY_VECTOR)
3369 handle_overlay_change_p = 0;
3370
3371 /* Handle overlay changes.
3372 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3373 if it finds overlays. */
3374 if (handle_overlay_change_p)
3375 handled = handle_overlay_change (it);
3376 }
3377
3378 if (it->ellipsis_p)
3379 {
3380 setup_for_ellipsis (it, 0);
3381 break;
3382 }
3383 }
3384 while (handled == HANDLED_RECOMPUTE_PROPS);
3385
3386 /* Determine where to stop next. */
3387 if (handled == HANDLED_NORMALLY)
3388 compute_stop_pos (it);
3389 }
3390
3391
3392 /* Compute IT->stop_charpos from text property and overlay change
3393 information for IT's current position. */
3394
3395 static void
3396 compute_stop_pos (struct it *it)
3397 {
3398 register INTERVAL iv, next_iv;
3399 Lisp_Object object, limit, position;
3400 ptrdiff_t charpos, bytepos;
3401
3402 if (STRINGP (it->string))
3403 {
3404 /* Strings are usually short, so don't limit the search for
3405 properties. */
3406 it->stop_charpos = it->end_charpos;
3407 object = it->string;
3408 limit = Qnil;
3409 charpos = IT_STRING_CHARPOS (*it);
3410 bytepos = IT_STRING_BYTEPOS (*it);
3411 }
3412 else
3413 {
3414 ptrdiff_t pos;
3415
3416 /* If end_charpos is out of range for some reason, such as a
3417 misbehaving display function, rationalize it (Bug#5984). */
3418 if (it->end_charpos > ZV)
3419 it->end_charpos = ZV;
3420 it->stop_charpos = it->end_charpos;
3421
3422 /* If next overlay change is in front of the current stop pos
3423 (which is IT->end_charpos), stop there. Note: value of
3424 next_overlay_change is point-max if no overlay change
3425 follows. */
3426 charpos = IT_CHARPOS (*it);
3427 bytepos = IT_BYTEPOS (*it);
3428 pos = next_overlay_change (charpos);
3429 if (pos < it->stop_charpos)
3430 it->stop_charpos = pos;
3431
3432 /* Set up variables for computing the stop position from text
3433 property changes. */
3434 XSETBUFFER (object, current_buffer);
3435 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3436 }
3437
3438 /* Get the interval containing IT's position. Value is a null
3439 interval if there isn't such an interval. */
3440 position = make_number (charpos);
3441 iv = validate_interval_range (object, &position, &position, 0);
3442 if (iv)
3443 {
3444 Lisp_Object values_here[LAST_PROP_IDX];
3445 struct props *p;
3446
3447 /* Get properties here. */
3448 for (p = it_props; p->handler; ++p)
3449 values_here[p->idx] = textget (iv->plist, *p->name);
3450
3451 /* Look for an interval following iv that has different
3452 properties. */
3453 for (next_iv = next_interval (iv);
3454 (next_iv
3455 && (NILP (limit)
3456 || XFASTINT (limit) > next_iv->position));
3457 next_iv = next_interval (next_iv))
3458 {
3459 for (p = it_props; p->handler; ++p)
3460 {
3461 Lisp_Object new_value;
3462
3463 new_value = textget (next_iv->plist, *p->name);
3464 if (!EQ (values_here[p->idx], new_value))
3465 break;
3466 }
3467
3468 if (p->handler)
3469 break;
3470 }
3471
3472 if (next_iv)
3473 {
3474 if (INTEGERP (limit)
3475 && next_iv->position >= XFASTINT (limit))
3476 /* No text property change up to limit. */
3477 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3478 else
3479 /* Text properties change in next_iv. */
3480 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3481 }
3482 }
3483
3484 if (it->cmp_it.id < 0)
3485 {
3486 ptrdiff_t stoppos = it->end_charpos;
3487
3488 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3489 stoppos = -1;
3490 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3491 stoppos, it->string);
3492 }
3493
3494 eassert (STRINGP (it->string)
3495 || (it->stop_charpos >= BEGV
3496 && it->stop_charpos >= IT_CHARPOS (*it)));
3497 }
3498
3499
3500 /* Return the position of the next overlay change after POS in
3501 current_buffer. Value is point-max if no overlay change
3502 follows. This is like `next-overlay-change' but doesn't use
3503 xmalloc. */
3504
3505 static ptrdiff_t
3506 next_overlay_change (ptrdiff_t pos)
3507 {
3508 ptrdiff_t i, noverlays;
3509 ptrdiff_t endpos;
3510 Lisp_Object *overlays;
3511
3512 /* Get all overlays at the given position. */
3513 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3514
3515 /* If any of these overlays ends before endpos,
3516 use its ending point instead. */
3517 for (i = 0; i < noverlays; ++i)
3518 {
3519 Lisp_Object oend;
3520 ptrdiff_t oendpos;
3521
3522 oend = OVERLAY_END (overlays[i]);
3523 oendpos = OVERLAY_POSITION (oend);
3524 endpos = min (endpos, oendpos);
3525 }
3526
3527 return endpos;
3528 }
3529
3530 /* How many characters forward to search for a display property or
3531 display string. Searching too far forward makes the bidi display
3532 sluggish, especially in small windows. */
3533 #define MAX_DISP_SCAN 250
3534
3535 /* Return the character position of a display string at or after
3536 position specified by POSITION. If no display string exists at or
3537 after POSITION, return ZV. A display string is either an overlay
3538 with `display' property whose value is a string, or a `display'
3539 text property whose value is a string. STRING is data about the
3540 string to iterate; if STRING->lstring is nil, we are iterating a
3541 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3542 on a GUI frame. DISP_PROP is set to zero if we searched
3543 MAX_DISP_SCAN characters forward without finding any display
3544 strings, non-zero otherwise. It is set to 2 if the display string
3545 uses any kind of `(space ...)' spec that will produce a stretch of
3546 white space in the text area. */
3547 ptrdiff_t
3548 compute_display_string_pos (struct text_pos *position,
3549 struct bidi_string_data *string,
3550 struct window *w,
3551 int frame_window_p, int *disp_prop)
3552 {
3553 /* OBJECT = nil means current buffer. */
3554 Lisp_Object object, object1;
3555 Lisp_Object pos, spec, limpos;
3556 int string_p = (string && (STRINGP (string->lstring) || string->s));
3557 ptrdiff_t eob = string_p ? string->schars : ZV;
3558 ptrdiff_t begb = string_p ? 0 : BEGV;
3559 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3560 ptrdiff_t lim =
3561 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3562 struct text_pos tpos;
3563 int rv = 0;
3564
3565 if (string && STRINGP (string->lstring))
3566 object1 = object = string->lstring;
3567 else if (w && !string_p)
3568 {
3569 XSETWINDOW (object, w);
3570 object1 = Qnil;
3571 }
3572 else
3573 object1 = object = Qnil;
3574
3575 *disp_prop = 1;
3576
3577 if (charpos >= eob
3578 /* We don't support display properties whose values are strings
3579 that have display string properties. */
3580 || string->from_disp_str
3581 /* C strings cannot have display properties. */
3582 || (string->s && !STRINGP (object)))
3583 {
3584 *disp_prop = 0;
3585 return eob;
3586 }
3587
3588 /* If the character at CHARPOS is where the display string begins,
3589 return CHARPOS. */
3590 pos = make_number (charpos);
3591 if (STRINGP (object))
3592 bufpos = string->bufpos;
3593 else
3594 bufpos = charpos;
3595 tpos = *position;
3596 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3597 && (charpos <= begb
3598 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3599 object),
3600 spec))
3601 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3602 frame_window_p)))
3603 {
3604 if (rv == 2)
3605 *disp_prop = 2;
3606 return charpos;
3607 }
3608
3609 /* Look forward for the first character with a `display' property
3610 that will replace the underlying text when displayed. */
3611 limpos = make_number (lim);
3612 do {
3613 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3614 CHARPOS (tpos) = XFASTINT (pos);
3615 if (CHARPOS (tpos) >= lim)
3616 {
3617 *disp_prop = 0;
3618 break;
3619 }
3620 if (STRINGP (object))
3621 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3622 else
3623 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3624 spec = Fget_char_property (pos, Qdisplay, object);
3625 if (!STRINGP (object))
3626 bufpos = CHARPOS (tpos);
3627 } while (NILP (spec)
3628 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3629 bufpos, frame_window_p)));
3630 if (rv == 2)
3631 *disp_prop = 2;
3632
3633 return CHARPOS (tpos);
3634 }
3635
3636 /* Return the character position of the end of the display string that
3637 started at CHARPOS. If there's no display string at CHARPOS,
3638 return -1. A display string is either an overlay with `display'
3639 property whose value is a string or a `display' text property whose
3640 value is a string. */
3641 ptrdiff_t
3642 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3643 {
3644 /* OBJECT = nil means current buffer. */
3645 Lisp_Object object =
3646 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3647 Lisp_Object pos = make_number (charpos);
3648 ptrdiff_t eob =
3649 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3650
3651 if (charpos >= eob || (string->s && !STRINGP (object)))
3652 return eob;
3653
3654 /* It could happen that the display property or overlay was removed
3655 since we found it in compute_display_string_pos above. One way
3656 this can happen is if JIT font-lock was called (through
3657 handle_fontified_prop), and jit-lock-functions remove text
3658 properties or overlays from the portion of buffer that includes
3659 CHARPOS. Muse mode is known to do that, for example. In this
3660 case, we return -1 to the caller, to signal that no display
3661 string is actually present at CHARPOS. See bidi_fetch_char for
3662 how this is handled.
3663
3664 An alternative would be to never look for display properties past
3665 it->stop_charpos. But neither compute_display_string_pos nor
3666 bidi_fetch_char that calls it know or care where the next
3667 stop_charpos is. */
3668 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3669 return -1;
3670
3671 /* Look forward for the first character where the `display' property
3672 changes. */
3673 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3674
3675 return XFASTINT (pos);
3676 }
3677
3678
3679 \f
3680 /***********************************************************************
3681 Fontification
3682 ***********************************************************************/
3683
3684 /* Handle changes in the `fontified' property of the current buffer by
3685 calling hook functions from Qfontification_functions to fontify
3686 regions of text. */
3687
3688 static enum prop_handled
3689 handle_fontified_prop (struct it *it)
3690 {
3691 Lisp_Object prop, pos;
3692 enum prop_handled handled = HANDLED_NORMALLY;
3693
3694 if (!NILP (Vmemory_full))
3695 return handled;
3696
3697 /* Get the value of the `fontified' property at IT's current buffer
3698 position. (The `fontified' property doesn't have a special
3699 meaning in strings.) If the value is nil, call functions from
3700 Qfontification_functions. */
3701 if (!STRINGP (it->string)
3702 && it->s == NULL
3703 && !NILP (Vfontification_functions)
3704 && !NILP (Vrun_hooks)
3705 && (pos = make_number (IT_CHARPOS (*it)),
3706 prop = Fget_char_property (pos, Qfontified, Qnil),
3707 /* Ignore the special cased nil value always present at EOB since
3708 no amount of fontifying will be able to change it. */
3709 NILP (prop) && IT_CHARPOS (*it) < Z))
3710 {
3711 ptrdiff_t count = SPECPDL_INDEX ();
3712 Lisp_Object val;
3713 struct buffer *obuf = current_buffer;
3714 ptrdiff_t begv = BEGV, zv = ZV;
3715 bool old_clip_changed = current_buffer->clip_changed;
3716
3717 val = Vfontification_functions;
3718 specbind (Qfontification_functions, Qnil);
3719
3720 eassert (it->end_charpos == ZV);
3721
3722 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3723 safe_call1 (val, pos);
3724 else
3725 {
3726 Lisp_Object fns, fn;
3727 struct gcpro gcpro1, gcpro2;
3728
3729 fns = Qnil;
3730 GCPRO2 (val, fns);
3731
3732 for (; CONSP (val); val = XCDR (val))
3733 {
3734 fn = XCAR (val);
3735
3736 if (EQ (fn, Qt))
3737 {
3738 /* A value of t indicates this hook has a local
3739 binding; it means to run the global binding too.
3740 In a global value, t should not occur. If it
3741 does, we must ignore it to avoid an endless
3742 loop. */
3743 for (fns = Fdefault_value (Qfontification_functions);
3744 CONSP (fns);
3745 fns = XCDR (fns))
3746 {
3747 fn = XCAR (fns);
3748 if (!EQ (fn, Qt))
3749 safe_call1 (fn, pos);
3750 }
3751 }
3752 else
3753 safe_call1 (fn, pos);
3754 }
3755
3756 UNGCPRO;
3757 }
3758
3759 unbind_to (count, Qnil);
3760
3761 /* Fontification functions routinely call `save-restriction'.
3762 Normally, this tags clip_changed, which can confuse redisplay
3763 (see discussion in Bug#6671). Since we don't perform any
3764 special handling of fontification changes in the case where
3765 `save-restriction' isn't called, there's no point doing so in
3766 this case either. So, if the buffer's restrictions are
3767 actually left unchanged, reset clip_changed. */
3768 if (obuf == current_buffer)
3769 {
3770 if (begv == BEGV && zv == ZV)
3771 current_buffer->clip_changed = old_clip_changed;
3772 }
3773 /* There isn't much we can reasonably do to protect against
3774 misbehaving fontification, but here's a fig leaf. */
3775 else if (BUFFER_LIVE_P (obuf))
3776 set_buffer_internal_1 (obuf);
3777
3778 /* The fontification code may have added/removed text.
3779 It could do even a lot worse, but let's at least protect against
3780 the most obvious case where only the text past `pos' gets changed',
3781 as is/was done in grep.el where some escapes sequences are turned
3782 into face properties (bug#7876). */
3783 it->end_charpos = ZV;
3784
3785 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3786 something. This avoids an endless loop if they failed to
3787 fontify the text for which reason ever. */
3788 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3789 handled = HANDLED_RECOMPUTE_PROPS;
3790 }
3791
3792 return handled;
3793 }
3794
3795
3796 \f
3797 /***********************************************************************
3798 Faces
3799 ***********************************************************************/
3800
3801 /* Set up iterator IT from face properties at its current position.
3802 Called from handle_stop. */
3803
3804 static enum prop_handled
3805 handle_face_prop (struct it *it)
3806 {
3807 int new_face_id;
3808 ptrdiff_t next_stop;
3809
3810 if (!STRINGP (it->string))
3811 {
3812 new_face_id
3813 = face_at_buffer_position (it->w,
3814 IT_CHARPOS (*it),
3815 &next_stop,
3816 (IT_CHARPOS (*it)
3817 + TEXT_PROP_DISTANCE_LIMIT),
3818 0, it->base_face_id);
3819
3820 /* Is this a start of a run of characters with box face?
3821 Caveat: this can be called for a freshly initialized
3822 iterator; face_id is -1 in this case. We know that the new
3823 face will not change until limit, i.e. if the new face has a
3824 box, all characters up to limit will have one. But, as
3825 usual, we don't know whether limit is really the end. */
3826 if (new_face_id != it->face_id)
3827 {
3828 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3829 /* If it->face_id is -1, old_face below will be NULL, see
3830 the definition of FACE_FROM_ID. This will happen if this
3831 is the initial call that gets the face. */
3832 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3833
3834 /* If the value of face_id of the iterator is -1, we have to
3835 look in front of IT's position and see whether there is a
3836 face there that's different from new_face_id. */
3837 if (!old_face && IT_CHARPOS (*it) > BEG)
3838 {
3839 int prev_face_id = face_before_it_pos (it);
3840
3841 old_face = FACE_FROM_ID (it->f, prev_face_id);
3842 }
3843
3844 /* If the new face has a box, but the old face does not,
3845 this is the start of a run of characters with box face,
3846 i.e. this character has a shadow on the left side. */
3847 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3848 && (old_face == NULL || !old_face->box));
3849 it->face_box_p = new_face->box != FACE_NO_BOX;
3850 }
3851 }
3852 else
3853 {
3854 int base_face_id;
3855 ptrdiff_t bufpos;
3856 int i;
3857 Lisp_Object from_overlay
3858 = (it->current.overlay_string_index >= 0
3859 ? it->string_overlays[it->current.overlay_string_index
3860 % OVERLAY_STRING_CHUNK_SIZE]
3861 : Qnil);
3862
3863 /* See if we got to this string directly or indirectly from
3864 an overlay property. That includes the before-string or
3865 after-string of an overlay, strings in display properties
3866 provided by an overlay, their text properties, etc.
3867
3868 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3869 if (! NILP (from_overlay))
3870 for (i = it->sp - 1; i >= 0; i--)
3871 {
3872 if (it->stack[i].current.overlay_string_index >= 0)
3873 from_overlay
3874 = it->string_overlays[it->stack[i].current.overlay_string_index
3875 % OVERLAY_STRING_CHUNK_SIZE];
3876 else if (! NILP (it->stack[i].from_overlay))
3877 from_overlay = it->stack[i].from_overlay;
3878
3879 if (!NILP (from_overlay))
3880 break;
3881 }
3882
3883 if (! NILP (from_overlay))
3884 {
3885 bufpos = IT_CHARPOS (*it);
3886 /* For a string from an overlay, the base face depends
3887 only on text properties and ignores overlays. */
3888 base_face_id
3889 = face_for_overlay_string (it->w,
3890 IT_CHARPOS (*it),
3891 &next_stop,
3892 (IT_CHARPOS (*it)
3893 + TEXT_PROP_DISTANCE_LIMIT),
3894 0,
3895 from_overlay);
3896 }
3897 else
3898 {
3899 bufpos = 0;
3900
3901 /* For strings from a `display' property, use the face at
3902 IT's current buffer position as the base face to merge
3903 with, so that overlay strings appear in the same face as
3904 surrounding text, unless they specify their own faces.
3905 For strings from wrap-prefix and line-prefix properties,
3906 use the default face, possibly remapped via
3907 Vface_remapping_alist. */
3908 base_face_id = it->string_from_prefix_prop_p
3909 ? (!NILP (Vface_remapping_alist)
3910 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3911 : DEFAULT_FACE_ID)
3912 : underlying_face_id (it);
3913 }
3914
3915 new_face_id = face_at_string_position (it->w,
3916 it->string,
3917 IT_STRING_CHARPOS (*it),
3918 bufpos,
3919 &next_stop,
3920 base_face_id, 0);
3921
3922 /* Is this a start of a run of characters with box? Caveat:
3923 this can be called for a freshly allocated iterator; face_id
3924 is -1 is this case. We know that the new face will not
3925 change until the next check pos, i.e. if the new face has a
3926 box, all characters up to that position will have a
3927 box. But, as usual, we don't know whether that position
3928 is really the end. */
3929 if (new_face_id != it->face_id)
3930 {
3931 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3932 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3933
3934 /* If new face has a box but old face hasn't, this is the
3935 start of a run of characters with box, i.e. it has a
3936 shadow on the left side. */
3937 it->start_of_box_run_p
3938 = new_face->box && (old_face == NULL || !old_face->box);
3939 it->face_box_p = new_face->box != FACE_NO_BOX;
3940 }
3941 }
3942
3943 it->face_id = new_face_id;
3944 return HANDLED_NORMALLY;
3945 }
3946
3947
3948 /* Return the ID of the face ``underlying'' IT's current position,
3949 which is in a string. If the iterator is associated with a
3950 buffer, return the face at IT's current buffer position.
3951 Otherwise, use the iterator's base_face_id. */
3952
3953 static int
3954 underlying_face_id (struct it *it)
3955 {
3956 int face_id = it->base_face_id, i;
3957
3958 eassert (STRINGP (it->string));
3959
3960 for (i = it->sp - 1; i >= 0; --i)
3961 if (NILP (it->stack[i].string))
3962 face_id = it->stack[i].face_id;
3963
3964 return face_id;
3965 }
3966
3967
3968 /* Compute the face one character before or after the current position
3969 of IT, in the visual order. BEFORE_P non-zero means get the face
3970 in front (to the left in L2R paragraphs, to the right in R2L
3971 paragraphs) of IT's screen position. Value is the ID of the face. */
3972
3973 static int
3974 face_before_or_after_it_pos (struct it *it, int before_p)
3975 {
3976 int face_id, limit;
3977 ptrdiff_t next_check_charpos;
3978 struct it it_copy;
3979 void *it_copy_data = NULL;
3980
3981 eassert (it->s == NULL);
3982
3983 if (STRINGP (it->string))
3984 {
3985 ptrdiff_t bufpos, charpos;
3986 int base_face_id;
3987
3988 /* No face change past the end of the string (for the case
3989 we are padding with spaces). No face change before the
3990 string start. */
3991 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3992 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3993 return it->face_id;
3994
3995 if (!it->bidi_p)
3996 {
3997 /* Set charpos to the position before or after IT's current
3998 position, in the logical order, which in the non-bidi
3999 case is the same as the visual order. */
4000 if (before_p)
4001 charpos = IT_STRING_CHARPOS (*it) - 1;
4002 else if (it->what == IT_COMPOSITION)
4003 /* For composition, we must check the character after the
4004 composition. */
4005 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4006 else
4007 charpos = IT_STRING_CHARPOS (*it) + 1;
4008 }
4009 else
4010 {
4011 if (before_p)
4012 {
4013 /* With bidi iteration, the character before the current
4014 in the visual order cannot be found by simple
4015 iteration, because "reverse" reordering is not
4016 supported. Instead, we need to use the move_it_*
4017 family of functions. */
4018 /* Ignore face changes before the first visible
4019 character on this display line. */
4020 if (it->current_x <= it->first_visible_x)
4021 return it->face_id;
4022 SAVE_IT (it_copy, *it, it_copy_data);
4023 /* Implementation note: Since move_it_in_display_line
4024 works in the iterator geometry, and thinks the first
4025 character is always the leftmost, even in R2L lines,
4026 we don't need to distinguish between the R2L and L2R
4027 cases here. */
4028 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4029 it_copy.current_x - 1, MOVE_TO_X);
4030 charpos = IT_STRING_CHARPOS (it_copy);
4031 RESTORE_IT (it, it, it_copy_data);
4032 }
4033 else
4034 {
4035 /* Set charpos to the string position of the character
4036 that comes after IT's current position in the visual
4037 order. */
4038 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4039
4040 it_copy = *it;
4041 while (n--)
4042 bidi_move_to_visually_next (&it_copy.bidi_it);
4043
4044 charpos = it_copy.bidi_it.charpos;
4045 }
4046 }
4047 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4048
4049 if (it->current.overlay_string_index >= 0)
4050 bufpos = IT_CHARPOS (*it);
4051 else
4052 bufpos = 0;
4053
4054 base_face_id = underlying_face_id (it);
4055
4056 /* Get the face for ASCII, or unibyte. */
4057 face_id = face_at_string_position (it->w,
4058 it->string,
4059 charpos,
4060 bufpos,
4061 &next_check_charpos,
4062 base_face_id, 0);
4063
4064 /* Correct the face for charsets different from ASCII. Do it
4065 for the multibyte case only. The face returned above is
4066 suitable for unibyte text if IT->string is unibyte. */
4067 if (STRING_MULTIBYTE (it->string))
4068 {
4069 struct text_pos pos1 = string_pos (charpos, it->string);
4070 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4071 int c, len;
4072 struct face *face = FACE_FROM_ID (it->f, face_id);
4073
4074 c = string_char_and_length (p, &len);
4075 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4076 }
4077 }
4078 else
4079 {
4080 struct text_pos pos;
4081
4082 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4083 || (IT_CHARPOS (*it) <= BEGV && before_p))
4084 return it->face_id;
4085
4086 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4087 pos = it->current.pos;
4088
4089 if (!it->bidi_p)
4090 {
4091 if (before_p)
4092 DEC_TEXT_POS (pos, it->multibyte_p);
4093 else
4094 {
4095 if (it->what == IT_COMPOSITION)
4096 {
4097 /* For composition, we must check the position after
4098 the composition. */
4099 pos.charpos += it->cmp_it.nchars;
4100 pos.bytepos += it->len;
4101 }
4102 else
4103 INC_TEXT_POS (pos, it->multibyte_p);
4104 }
4105 }
4106 else
4107 {
4108 if (before_p)
4109 {
4110 /* With bidi iteration, the character before the current
4111 in the visual order cannot be found by simple
4112 iteration, because "reverse" reordering is not
4113 supported. Instead, we need to use the move_it_*
4114 family of functions. */
4115 /* Ignore face changes before the first visible
4116 character on this display line. */
4117 if (it->current_x <= it->first_visible_x)
4118 return it->face_id;
4119 SAVE_IT (it_copy, *it, it_copy_data);
4120 /* Implementation note: Since move_it_in_display_line
4121 works in the iterator geometry, and thinks the first
4122 character is always the leftmost, even in R2L lines,
4123 we don't need to distinguish between the R2L and L2R
4124 cases here. */
4125 move_it_in_display_line (&it_copy, ZV,
4126 it_copy.current_x - 1, MOVE_TO_X);
4127 pos = it_copy.current.pos;
4128 RESTORE_IT (it, it, it_copy_data);
4129 }
4130 else
4131 {
4132 /* Set charpos to the buffer position of the character
4133 that comes after IT's current position in the visual
4134 order. */
4135 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4136
4137 it_copy = *it;
4138 while (n--)
4139 bidi_move_to_visually_next (&it_copy.bidi_it);
4140
4141 SET_TEXT_POS (pos,
4142 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4143 }
4144 }
4145 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4146
4147 /* Determine face for CHARSET_ASCII, or unibyte. */
4148 face_id = face_at_buffer_position (it->w,
4149 CHARPOS (pos),
4150 &next_check_charpos,
4151 limit, 0, -1);
4152
4153 /* Correct the face for charsets different from ASCII. Do it
4154 for the multibyte case only. The face returned above is
4155 suitable for unibyte text if current_buffer is unibyte. */
4156 if (it->multibyte_p)
4157 {
4158 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4159 struct face *face = FACE_FROM_ID (it->f, face_id);
4160 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4161 }
4162 }
4163
4164 return face_id;
4165 }
4166
4167
4168 \f
4169 /***********************************************************************
4170 Invisible text
4171 ***********************************************************************/
4172
4173 /* Set up iterator IT from invisible properties at its current
4174 position. Called from handle_stop. */
4175
4176 static enum prop_handled
4177 handle_invisible_prop (struct it *it)
4178 {
4179 enum prop_handled handled = HANDLED_NORMALLY;
4180 int invis_p;
4181 Lisp_Object prop;
4182
4183 if (STRINGP (it->string))
4184 {
4185 Lisp_Object end_charpos, limit, charpos;
4186
4187 /* Get the value of the invisible text property at the
4188 current position. Value will be nil if there is no such
4189 property. */
4190 charpos = make_number (IT_STRING_CHARPOS (*it));
4191 prop = Fget_text_property (charpos, Qinvisible, it->string);
4192 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4193
4194 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4195 {
4196 /* Record whether we have to display an ellipsis for the
4197 invisible text. */
4198 int display_ellipsis_p = (invis_p == 2);
4199 ptrdiff_t len, endpos;
4200
4201 handled = HANDLED_RECOMPUTE_PROPS;
4202
4203 /* Get the position at which the next visible text can be
4204 found in IT->string, if any. */
4205 endpos = len = SCHARS (it->string);
4206 XSETINT (limit, len);
4207 do
4208 {
4209 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4210 it->string, limit);
4211 if (INTEGERP (end_charpos))
4212 {
4213 endpos = XFASTINT (end_charpos);
4214 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4215 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4216 if (invis_p == 2)
4217 display_ellipsis_p = true;
4218 }
4219 }
4220 while (invis_p && endpos < len);
4221
4222 if (display_ellipsis_p)
4223 it->ellipsis_p = true;
4224
4225 if (endpos < len)
4226 {
4227 /* Text at END_CHARPOS is visible. Move IT there. */
4228 struct text_pos old;
4229 ptrdiff_t oldpos;
4230
4231 old = it->current.string_pos;
4232 oldpos = CHARPOS (old);
4233 if (it->bidi_p)
4234 {
4235 if (it->bidi_it.first_elt
4236 && it->bidi_it.charpos < SCHARS (it->string))
4237 bidi_paragraph_init (it->paragraph_embedding,
4238 &it->bidi_it, 1);
4239 /* Bidi-iterate out of the invisible text. */
4240 do
4241 {
4242 bidi_move_to_visually_next (&it->bidi_it);
4243 }
4244 while (oldpos <= it->bidi_it.charpos
4245 && it->bidi_it.charpos < endpos);
4246
4247 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4248 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4249 if (IT_CHARPOS (*it) >= endpos)
4250 it->prev_stop = endpos;
4251 }
4252 else
4253 {
4254 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4255 compute_string_pos (&it->current.string_pos, old, it->string);
4256 }
4257 }
4258 else
4259 {
4260 /* The rest of the string is invisible. If this is an
4261 overlay string, proceed with the next overlay string
4262 or whatever comes and return a character from there. */
4263 if (it->current.overlay_string_index >= 0
4264 && !display_ellipsis_p)
4265 {
4266 next_overlay_string (it);
4267 /* Don't check for overlay strings when we just
4268 finished processing them. */
4269 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4270 }
4271 else
4272 {
4273 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4274 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4275 }
4276 }
4277 }
4278 }
4279 else
4280 {
4281 ptrdiff_t newpos, next_stop, start_charpos, tem;
4282 Lisp_Object pos, overlay;
4283
4284 /* First of all, is there invisible text at this position? */
4285 tem = start_charpos = IT_CHARPOS (*it);
4286 pos = make_number (tem);
4287 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4288 &overlay);
4289 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4290
4291 /* If we are on invisible text, skip over it. */
4292 if (invis_p && start_charpos < it->end_charpos)
4293 {
4294 /* Record whether we have to display an ellipsis for the
4295 invisible text. */
4296 int display_ellipsis_p = invis_p == 2;
4297
4298 handled = HANDLED_RECOMPUTE_PROPS;
4299
4300 /* Loop skipping over invisible text. The loop is left at
4301 ZV or with IT on the first char being visible again. */
4302 do
4303 {
4304 /* Try to skip some invisible text. Return value is the
4305 position reached which can be equal to where we start
4306 if there is nothing invisible there. This skips both
4307 over invisible text properties and overlays with
4308 invisible property. */
4309 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4310
4311 /* If we skipped nothing at all we weren't at invisible
4312 text in the first place. If everything to the end of
4313 the buffer was skipped, end the loop. */
4314 if (newpos == tem || newpos >= ZV)
4315 invis_p = 0;
4316 else
4317 {
4318 /* We skipped some characters but not necessarily
4319 all there are. Check if we ended up on visible
4320 text. Fget_char_property returns the property of
4321 the char before the given position, i.e. if we
4322 get invis_p = 0, this means that the char at
4323 newpos is visible. */
4324 pos = make_number (newpos);
4325 prop = Fget_char_property (pos, Qinvisible, it->window);
4326 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4327 }
4328
4329 /* If we ended up on invisible text, proceed to
4330 skip starting with next_stop. */
4331 if (invis_p)
4332 tem = next_stop;
4333
4334 /* If there are adjacent invisible texts, don't lose the
4335 second one's ellipsis. */
4336 if (invis_p == 2)
4337 display_ellipsis_p = true;
4338 }
4339 while (invis_p);
4340
4341 /* The position newpos is now either ZV or on visible text. */
4342 if (it->bidi_p)
4343 {
4344 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4345 int on_newline
4346 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4347 int after_newline
4348 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4349
4350 /* If the invisible text ends on a newline or on a
4351 character after a newline, we can avoid the costly,
4352 character by character, bidi iteration to NEWPOS, and
4353 instead simply reseat the iterator there. That's
4354 because all bidi reordering information is tossed at
4355 the newline. This is a big win for modes that hide
4356 complete lines, like Outline, Org, etc. */
4357 if (on_newline || after_newline)
4358 {
4359 struct text_pos tpos;
4360 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4361
4362 SET_TEXT_POS (tpos, newpos, bpos);
4363 reseat_1 (it, tpos, 0);
4364 /* If we reseat on a newline/ZV, we need to prep the
4365 bidi iterator for advancing to the next character
4366 after the newline/EOB, keeping the current paragraph
4367 direction (so that PRODUCE_GLYPHS does TRT wrt
4368 prepending/appending glyphs to a glyph row). */
4369 if (on_newline)
4370 {
4371 it->bidi_it.first_elt = 0;
4372 it->bidi_it.paragraph_dir = pdir;
4373 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4374 it->bidi_it.nchars = 1;
4375 it->bidi_it.ch_len = 1;
4376 }
4377 }
4378 else /* Must use the slow method. */
4379 {
4380 /* With bidi iteration, the region of invisible text
4381 could start and/or end in the middle of a
4382 non-base embedding level. Therefore, we need to
4383 skip invisible text using the bidi iterator,
4384 starting at IT's current position, until we find
4385 ourselves outside of the invisible text.
4386 Skipping invisible text _after_ bidi iteration
4387 avoids affecting the visual order of the
4388 displayed text when invisible properties are
4389 added or removed. */
4390 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4391 {
4392 /* If we were `reseat'ed to a new paragraph,
4393 determine the paragraph base direction. We
4394 need to do it now because
4395 next_element_from_buffer may not have a
4396 chance to do it, if we are going to skip any
4397 text at the beginning, which resets the
4398 FIRST_ELT flag. */
4399 bidi_paragraph_init (it->paragraph_embedding,
4400 &it->bidi_it, 1);
4401 }
4402 do
4403 {
4404 bidi_move_to_visually_next (&it->bidi_it);
4405 }
4406 while (it->stop_charpos <= it->bidi_it.charpos
4407 && it->bidi_it.charpos < newpos);
4408 IT_CHARPOS (*it) = it->bidi_it.charpos;
4409 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4410 /* If we overstepped NEWPOS, record its position in
4411 the iterator, so that we skip invisible text if
4412 later the bidi iteration lands us in the
4413 invisible region again. */
4414 if (IT_CHARPOS (*it) >= newpos)
4415 it->prev_stop = newpos;
4416 }
4417 }
4418 else
4419 {
4420 IT_CHARPOS (*it) = newpos;
4421 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4422 }
4423
4424 /* If there are before-strings at the start of invisible
4425 text, and the text is invisible because of a text
4426 property, arrange to show before-strings because 20.x did
4427 it that way. (If the text is invisible because of an
4428 overlay property instead of a text property, this is
4429 already handled in the overlay code.) */
4430 if (NILP (overlay)
4431 && get_overlay_strings (it, it->stop_charpos))
4432 {
4433 handled = HANDLED_RECOMPUTE_PROPS;
4434 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4435 }
4436 else if (display_ellipsis_p)
4437 {
4438 /* Make sure that the glyphs of the ellipsis will get
4439 correct `charpos' values. If we would not update
4440 it->position here, the glyphs would belong to the
4441 last visible character _before_ the invisible
4442 text, which confuses `set_cursor_from_row'.
4443
4444 We use the last invisible position instead of the
4445 first because this way the cursor is always drawn on
4446 the first "." of the ellipsis, whenever PT is inside
4447 the invisible text. Otherwise the cursor would be
4448 placed _after_ the ellipsis when the point is after the
4449 first invisible character. */
4450 if (!STRINGP (it->object))
4451 {
4452 it->position.charpos = newpos - 1;
4453 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4454 }
4455 it->ellipsis_p = true;
4456 /* Let the ellipsis display before
4457 considering any properties of the following char.
4458 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4459 handled = HANDLED_RETURN;
4460 }
4461 }
4462 }
4463
4464 return handled;
4465 }
4466
4467
4468 /* Make iterator IT return `...' next.
4469 Replaces LEN characters from buffer. */
4470
4471 static void
4472 setup_for_ellipsis (struct it *it, int len)
4473 {
4474 /* Use the display table definition for `...'. Invalid glyphs
4475 will be handled by the method returning elements from dpvec. */
4476 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4477 {
4478 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4479 it->dpvec = v->contents;
4480 it->dpend = v->contents + v->header.size;
4481 }
4482 else
4483 {
4484 /* Default `...'. */
4485 it->dpvec = default_invis_vector;
4486 it->dpend = default_invis_vector + 3;
4487 }
4488
4489 it->dpvec_char_len = len;
4490 it->current.dpvec_index = 0;
4491 it->dpvec_face_id = -1;
4492
4493 /* Remember the current face id in case glyphs specify faces.
4494 IT's face is restored in set_iterator_to_next.
4495 saved_face_id was set to preceding char's face in handle_stop. */
4496 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4497 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4498
4499 it->method = GET_FROM_DISPLAY_VECTOR;
4500 it->ellipsis_p = true;
4501 }
4502
4503
4504 \f
4505 /***********************************************************************
4506 'display' property
4507 ***********************************************************************/
4508
4509 /* Set up iterator IT from `display' property at its current position.
4510 Called from handle_stop.
4511 We return HANDLED_RETURN if some part of the display property
4512 overrides the display of the buffer text itself.
4513 Otherwise we return HANDLED_NORMALLY. */
4514
4515 static enum prop_handled
4516 handle_display_prop (struct it *it)
4517 {
4518 Lisp_Object propval, object, overlay;
4519 struct text_pos *position;
4520 ptrdiff_t bufpos;
4521 /* Nonzero if some property replaces the display of the text itself. */
4522 int display_replaced_p = 0;
4523
4524 if (STRINGP (it->string))
4525 {
4526 object = it->string;
4527 position = &it->current.string_pos;
4528 bufpos = CHARPOS (it->current.pos);
4529 }
4530 else
4531 {
4532 XSETWINDOW (object, it->w);
4533 position = &it->current.pos;
4534 bufpos = CHARPOS (*position);
4535 }
4536
4537 /* Reset those iterator values set from display property values. */
4538 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4539 it->space_width = Qnil;
4540 it->font_height = Qnil;
4541 it->voffset = 0;
4542
4543 /* We don't support recursive `display' properties, i.e. string
4544 values that have a string `display' property, that have a string
4545 `display' property etc. */
4546 if (!it->string_from_display_prop_p)
4547 it->area = TEXT_AREA;
4548
4549 propval = get_char_property_and_overlay (make_number (position->charpos),
4550 Qdisplay, object, &overlay);
4551 if (NILP (propval))
4552 return HANDLED_NORMALLY;
4553 /* Now OVERLAY is the overlay that gave us this property, or nil
4554 if it was a text property. */
4555
4556 if (!STRINGP (it->string))
4557 object = it->w->contents;
4558
4559 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4560 position, bufpos,
4561 FRAME_WINDOW_P (it->f));
4562
4563 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4564 }
4565
4566 /* Subroutine of handle_display_prop. Returns non-zero if the display
4567 specification in SPEC is a replacing specification, i.e. it would
4568 replace the text covered by `display' property with something else,
4569 such as an image or a display string. If SPEC includes any kind or
4570 `(space ...) specification, the value is 2; this is used by
4571 compute_display_string_pos, which see.
4572
4573 See handle_single_display_spec for documentation of arguments.
4574 frame_window_p is non-zero if the window being redisplayed is on a
4575 GUI frame; this argument is used only if IT is NULL, see below.
4576
4577 IT can be NULL, if this is called by the bidi reordering code
4578 through compute_display_string_pos, which see. In that case, this
4579 function only examines SPEC, but does not otherwise "handle" it, in
4580 the sense that it doesn't set up members of IT from the display
4581 spec. */
4582 static int
4583 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4584 Lisp_Object overlay, struct text_pos *position,
4585 ptrdiff_t bufpos, int frame_window_p)
4586 {
4587 int replacing_p = 0;
4588 int rv;
4589
4590 if (CONSP (spec)
4591 /* Simple specifications. */
4592 && !EQ (XCAR (spec), Qimage)
4593 && !EQ (XCAR (spec), Qspace)
4594 && !EQ (XCAR (spec), Qwhen)
4595 && !EQ (XCAR (spec), Qslice)
4596 && !EQ (XCAR (spec), Qspace_width)
4597 && !EQ (XCAR (spec), Qheight)
4598 && !EQ (XCAR (spec), Qraise)
4599 /* Marginal area specifications. */
4600 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4601 && !EQ (XCAR (spec), Qleft_fringe)
4602 && !EQ (XCAR (spec), Qright_fringe)
4603 && !NILP (XCAR (spec)))
4604 {
4605 for (; CONSP (spec); spec = XCDR (spec))
4606 {
4607 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4608 overlay, position, bufpos,
4609 replacing_p, frame_window_p)))
4610 {
4611 replacing_p = rv;
4612 /* If some text in a string is replaced, `position' no
4613 longer points to the position of `object'. */
4614 if (!it || STRINGP (object))
4615 break;
4616 }
4617 }
4618 }
4619 else if (VECTORP (spec))
4620 {
4621 ptrdiff_t i;
4622 for (i = 0; i < ASIZE (spec); ++i)
4623 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4624 overlay, position, bufpos,
4625 replacing_p, frame_window_p)))
4626 {
4627 replacing_p = rv;
4628 /* If some text in a string is replaced, `position' no
4629 longer points to the position of `object'. */
4630 if (!it || STRINGP (object))
4631 break;
4632 }
4633 }
4634 else
4635 {
4636 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4637 position, bufpos, 0,
4638 frame_window_p)))
4639 replacing_p = rv;
4640 }
4641
4642 return replacing_p;
4643 }
4644
4645 /* Value is the position of the end of the `display' property starting
4646 at START_POS in OBJECT. */
4647
4648 static struct text_pos
4649 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4650 {
4651 Lisp_Object end;
4652 struct text_pos end_pos;
4653
4654 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4655 Qdisplay, object, Qnil);
4656 CHARPOS (end_pos) = XFASTINT (end);
4657 if (STRINGP (object))
4658 compute_string_pos (&end_pos, start_pos, it->string);
4659 else
4660 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4661
4662 return end_pos;
4663 }
4664
4665
4666 /* Set up IT from a single `display' property specification SPEC. OBJECT
4667 is the object in which the `display' property was found. *POSITION
4668 is the position in OBJECT at which the `display' property was found.
4669 BUFPOS is the buffer position of OBJECT (different from POSITION if
4670 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4671 previously saw a display specification which already replaced text
4672 display with something else, for example an image; we ignore such
4673 properties after the first one has been processed.
4674
4675 OVERLAY is the overlay this `display' property came from,
4676 or nil if it was a text property.
4677
4678 If SPEC is a `space' or `image' specification, and in some other
4679 cases too, set *POSITION to the position where the `display'
4680 property ends.
4681
4682 If IT is NULL, only examine the property specification in SPEC, but
4683 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4684 is intended to be displayed in a window on a GUI frame.
4685
4686 Value is non-zero if something was found which replaces the display
4687 of buffer or string text. */
4688
4689 static int
4690 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4691 Lisp_Object overlay, struct text_pos *position,
4692 ptrdiff_t bufpos, int display_replaced_p,
4693 int frame_window_p)
4694 {
4695 Lisp_Object form;
4696 Lisp_Object location, value;
4697 struct text_pos start_pos = *position;
4698 int valid_p;
4699
4700 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4701 If the result is non-nil, use VALUE instead of SPEC. */
4702 form = Qt;
4703 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4704 {
4705 spec = XCDR (spec);
4706 if (!CONSP (spec))
4707 return 0;
4708 form = XCAR (spec);
4709 spec = XCDR (spec);
4710 }
4711
4712 if (!NILP (form) && !EQ (form, Qt))
4713 {
4714 ptrdiff_t count = SPECPDL_INDEX ();
4715 struct gcpro gcpro1;
4716
4717 /* Bind `object' to the object having the `display' property, a
4718 buffer or string. Bind `position' to the position in the
4719 object where the property was found, and `buffer-position'
4720 to the current position in the buffer. */
4721
4722 if (NILP (object))
4723 XSETBUFFER (object, current_buffer);
4724 specbind (Qobject, object);
4725 specbind (Qposition, make_number (CHARPOS (*position)));
4726 specbind (Qbuffer_position, make_number (bufpos));
4727 GCPRO1 (form);
4728 form = safe_eval (form);
4729 UNGCPRO;
4730 unbind_to (count, Qnil);
4731 }
4732
4733 if (NILP (form))
4734 return 0;
4735
4736 /* Handle `(height HEIGHT)' specifications. */
4737 if (CONSP (spec)
4738 && EQ (XCAR (spec), Qheight)
4739 && CONSP (XCDR (spec)))
4740 {
4741 if (it)
4742 {
4743 if (!FRAME_WINDOW_P (it->f))
4744 return 0;
4745
4746 it->font_height = XCAR (XCDR (spec));
4747 if (!NILP (it->font_height))
4748 {
4749 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4750 int new_height = -1;
4751
4752 if (CONSP (it->font_height)
4753 && (EQ (XCAR (it->font_height), Qplus)
4754 || EQ (XCAR (it->font_height), Qminus))
4755 && CONSP (XCDR (it->font_height))
4756 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4757 {
4758 /* `(+ N)' or `(- N)' where N is an integer. */
4759 int steps = XINT (XCAR (XCDR (it->font_height)));
4760 if (EQ (XCAR (it->font_height), Qplus))
4761 steps = - steps;
4762 it->face_id = smaller_face (it->f, it->face_id, steps);
4763 }
4764 else if (FUNCTIONP (it->font_height))
4765 {
4766 /* Call function with current height as argument.
4767 Value is the new height. */
4768 Lisp_Object height;
4769 height = safe_call1 (it->font_height,
4770 face->lface[LFACE_HEIGHT_INDEX]);
4771 if (NUMBERP (height))
4772 new_height = XFLOATINT (height);
4773 }
4774 else if (NUMBERP (it->font_height))
4775 {
4776 /* Value is a multiple of the canonical char height. */
4777 struct face *f;
4778
4779 f = FACE_FROM_ID (it->f,
4780 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4781 new_height = (XFLOATINT (it->font_height)
4782 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4783 }
4784 else
4785 {
4786 /* Evaluate IT->font_height with `height' bound to the
4787 current specified height to get the new height. */
4788 ptrdiff_t count = SPECPDL_INDEX ();
4789
4790 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4791 value = safe_eval (it->font_height);
4792 unbind_to (count, Qnil);
4793
4794 if (NUMBERP (value))
4795 new_height = XFLOATINT (value);
4796 }
4797
4798 if (new_height > 0)
4799 it->face_id = face_with_height (it->f, it->face_id, new_height);
4800 }
4801 }
4802
4803 return 0;
4804 }
4805
4806 /* Handle `(space-width WIDTH)'. */
4807 if (CONSP (spec)
4808 && EQ (XCAR (spec), Qspace_width)
4809 && CONSP (XCDR (spec)))
4810 {
4811 if (it)
4812 {
4813 if (!FRAME_WINDOW_P (it->f))
4814 return 0;
4815
4816 value = XCAR (XCDR (spec));
4817 if (NUMBERP (value) && XFLOATINT (value) > 0)
4818 it->space_width = value;
4819 }
4820
4821 return 0;
4822 }
4823
4824 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4825 if (CONSP (spec)
4826 && EQ (XCAR (spec), Qslice))
4827 {
4828 Lisp_Object tem;
4829
4830 if (it)
4831 {
4832 if (!FRAME_WINDOW_P (it->f))
4833 return 0;
4834
4835 if (tem = XCDR (spec), CONSP (tem))
4836 {
4837 it->slice.x = XCAR (tem);
4838 if (tem = XCDR (tem), CONSP (tem))
4839 {
4840 it->slice.y = XCAR (tem);
4841 if (tem = XCDR (tem), CONSP (tem))
4842 {
4843 it->slice.width = XCAR (tem);
4844 if (tem = XCDR (tem), CONSP (tem))
4845 it->slice.height = XCAR (tem);
4846 }
4847 }
4848 }
4849 }
4850
4851 return 0;
4852 }
4853
4854 /* Handle `(raise FACTOR)'. */
4855 if (CONSP (spec)
4856 && EQ (XCAR (spec), Qraise)
4857 && CONSP (XCDR (spec)))
4858 {
4859 if (it)
4860 {
4861 if (!FRAME_WINDOW_P (it->f))
4862 return 0;
4863
4864 #ifdef HAVE_WINDOW_SYSTEM
4865 value = XCAR (XCDR (spec));
4866 if (NUMBERP (value))
4867 {
4868 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4869 it->voffset = - (XFLOATINT (value)
4870 * (FONT_HEIGHT (face->font)));
4871 }
4872 #endif /* HAVE_WINDOW_SYSTEM */
4873 }
4874
4875 return 0;
4876 }
4877
4878 /* Don't handle the other kinds of display specifications
4879 inside a string that we got from a `display' property. */
4880 if (it && it->string_from_display_prop_p)
4881 return 0;
4882
4883 /* Characters having this form of property are not displayed, so
4884 we have to find the end of the property. */
4885 if (it)
4886 {
4887 start_pos = *position;
4888 *position = display_prop_end (it, object, start_pos);
4889 }
4890 value = Qnil;
4891
4892 /* Stop the scan at that end position--we assume that all
4893 text properties change there. */
4894 if (it)
4895 it->stop_charpos = position->charpos;
4896
4897 /* Handle `(left-fringe BITMAP [FACE])'
4898 and `(right-fringe BITMAP [FACE])'. */
4899 if (CONSP (spec)
4900 && (EQ (XCAR (spec), Qleft_fringe)
4901 || EQ (XCAR (spec), Qright_fringe))
4902 && CONSP (XCDR (spec)))
4903 {
4904 int fringe_bitmap;
4905
4906 if (it)
4907 {
4908 if (!FRAME_WINDOW_P (it->f))
4909 /* If we return here, POSITION has been advanced
4910 across the text with this property. */
4911 {
4912 /* Synchronize the bidi iterator with POSITION. This is
4913 needed because we are not going to push the iterator
4914 on behalf of this display property, so there will be
4915 no pop_it call to do this synchronization for us. */
4916 if (it->bidi_p)
4917 {
4918 it->position = *position;
4919 iterate_out_of_display_property (it);
4920 *position = it->position;
4921 }
4922 return 1;
4923 }
4924 }
4925 else if (!frame_window_p)
4926 return 1;
4927
4928 #ifdef HAVE_WINDOW_SYSTEM
4929 value = XCAR (XCDR (spec));
4930 if (!SYMBOLP (value)
4931 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4932 /* If we return here, POSITION has been advanced
4933 across the text with this property. */
4934 {
4935 if (it && it->bidi_p)
4936 {
4937 it->position = *position;
4938 iterate_out_of_display_property (it);
4939 *position = it->position;
4940 }
4941 return 1;
4942 }
4943
4944 if (it)
4945 {
4946 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4947
4948 if (CONSP (XCDR (XCDR (spec))))
4949 {
4950 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4951 int face_id2 = lookup_derived_face (it->f, face_name,
4952 FRINGE_FACE_ID, 0);
4953 if (face_id2 >= 0)
4954 face_id = face_id2;
4955 }
4956
4957 /* Save current settings of IT so that we can restore them
4958 when we are finished with the glyph property value. */
4959 push_it (it, position);
4960
4961 it->area = TEXT_AREA;
4962 it->what = IT_IMAGE;
4963 it->image_id = -1; /* no image */
4964 it->position = start_pos;
4965 it->object = NILP (object) ? it->w->contents : object;
4966 it->method = GET_FROM_IMAGE;
4967 it->from_overlay = Qnil;
4968 it->face_id = face_id;
4969 it->from_disp_prop_p = true;
4970
4971 /* Say that we haven't consumed the characters with
4972 `display' property yet. The call to pop_it in
4973 set_iterator_to_next will clean this up. */
4974 *position = start_pos;
4975
4976 if (EQ (XCAR (spec), Qleft_fringe))
4977 {
4978 it->left_user_fringe_bitmap = fringe_bitmap;
4979 it->left_user_fringe_face_id = face_id;
4980 }
4981 else
4982 {
4983 it->right_user_fringe_bitmap = fringe_bitmap;
4984 it->right_user_fringe_face_id = face_id;
4985 }
4986 }
4987 #endif /* HAVE_WINDOW_SYSTEM */
4988 return 1;
4989 }
4990
4991 /* Prepare to handle `((margin left-margin) ...)',
4992 `((margin right-margin) ...)' and `((margin nil) ...)'
4993 prefixes for display specifications. */
4994 location = Qunbound;
4995 if (CONSP (spec) && CONSP (XCAR (spec)))
4996 {
4997 Lisp_Object tem;
4998
4999 value = XCDR (spec);
5000 if (CONSP (value))
5001 value = XCAR (value);
5002
5003 tem = XCAR (spec);
5004 if (EQ (XCAR (tem), Qmargin)
5005 && (tem = XCDR (tem),
5006 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5007 (NILP (tem)
5008 || EQ (tem, Qleft_margin)
5009 || EQ (tem, Qright_margin))))
5010 location = tem;
5011 }
5012
5013 if (EQ (location, Qunbound))
5014 {
5015 location = Qnil;
5016 value = spec;
5017 }
5018
5019 /* After this point, VALUE is the property after any
5020 margin prefix has been stripped. It must be a string,
5021 an image specification, or `(space ...)'.
5022
5023 LOCATION specifies where to display: `left-margin',
5024 `right-margin' or nil. */
5025
5026 valid_p = (STRINGP (value)
5027 #ifdef HAVE_WINDOW_SYSTEM
5028 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5029 && valid_image_p (value))
5030 #endif /* not HAVE_WINDOW_SYSTEM */
5031 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5032
5033 if (valid_p && !display_replaced_p)
5034 {
5035 int retval = 1;
5036
5037 if (!it)
5038 {
5039 /* Callers need to know whether the display spec is any kind
5040 of `(space ...)' spec that is about to affect text-area
5041 display. */
5042 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5043 retval = 2;
5044 return retval;
5045 }
5046
5047 /* Save current settings of IT so that we can restore them
5048 when we are finished with the glyph property value. */
5049 push_it (it, position);
5050 it->from_overlay = overlay;
5051 it->from_disp_prop_p = true;
5052
5053 if (NILP (location))
5054 it->area = TEXT_AREA;
5055 else if (EQ (location, Qleft_margin))
5056 it->area = LEFT_MARGIN_AREA;
5057 else
5058 it->area = RIGHT_MARGIN_AREA;
5059
5060 if (STRINGP (value))
5061 {
5062 it->string = value;
5063 it->multibyte_p = STRING_MULTIBYTE (it->string);
5064 it->current.overlay_string_index = -1;
5065 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5066 it->end_charpos = it->string_nchars = SCHARS (it->string);
5067 it->method = GET_FROM_STRING;
5068 it->stop_charpos = 0;
5069 it->prev_stop = 0;
5070 it->base_level_stop = 0;
5071 it->string_from_display_prop_p = true;
5072 /* Say that we haven't consumed the characters with
5073 `display' property yet. The call to pop_it in
5074 set_iterator_to_next will clean this up. */
5075 if (BUFFERP (object))
5076 *position = start_pos;
5077
5078 /* Force paragraph direction to be that of the parent
5079 object. If the parent object's paragraph direction is
5080 not yet determined, default to L2R. */
5081 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5082 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5083 else
5084 it->paragraph_embedding = L2R;
5085
5086 /* Set up the bidi iterator for this display string. */
5087 if (it->bidi_p)
5088 {
5089 it->bidi_it.string.lstring = it->string;
5090 it->bidi_it.string.s = NULL;
5091 it->bidi_it.string.schars = it->end_charpos;
5092 it->bidi_it.string.bufpos = bufpos;
5093 it->bidi_it.string.from_disp_str = 1;
5094 it->bidi_it.string.unibyte = !it->multibyte_p;
5095 it->bidi_it.w = it->w;
5096 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5097 }
5098 }
5099 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5100 {
5101 it->method = GET_FROM_STRETCH;
5102 it->object = value;
5103 *position = it->position = start_pos;
5104 retval = 1 + (it->area == TEXT_AREA);
5105 }
5106 #ifdef HAVE_WINDOW_SYSTEM
5107 else
5108 {
5109 it->what = IT_IMAGE;
5110 it->image_id = lookup_image (it->f, value);
5111 it->position = start_pos;
5112 it->object = NILP (object) ? it->w->contents : object;
5113 it->method = GET_FROM_IMAGE;
5114
5115 /* Say that we haven't consumed the characters with
5116 `display' property yet. The call to pop_it in
5117 set_iterator_to_next will clean this up. */
5118 *position = start_pos;
5119 }
5120 #endif /* HAVE_WINDOW_SYSTEM */
5121
5122 return retval;
5123 }
5124
5125 /* Invalid property or property not supported. Restore
5126 POSITION to what it was before. */
5127 *position = start_pos;
5128 return 0;
5129 }
5130
5131 /* Check if PROP is a display property value whose text should be
5132 treated as intangible. OVERLAY is the overlay from which PROP
5133 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5134 specify the buffer position covered by PROP. */
5135
5136 int
5137 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5138 ptrdiff_t charpos, ptrdiff_t bytepos)
5139 {
5140 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5141 struct text_pos position;
5142
5143 SET_TEXT_POS (position, charpos, bytepos);
5144 return handle_display_spec (NULL, prop, Qnil, overlay,
5145 &position, charpos, frame_window_p);
5146 }
5147
5148
5149 /* Return 1 if PROP is a display sub-property value containing STRING.
5150
5151 Implementation note: this and the following function are really
5152 special cases of handle_display_spec and
5153 handle_single_display_spec, and should ideally use the same code.
5154 Until they do, these two pairs must be consistent and must be
5155 modified in sync. */
5156
5157 static int
5158 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5159 {
5160 if (EQ (string, prop))
5161 return 1;
5162
5163 /* Skip over `when FORM'. */
5164 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5165 {
5166 prop = XCDR (prop);
5167 if (!CONSP (prop))
5168 return 0;
5169 /* Actually, the condition following `when' should be eval'ed,
5170 like handle_single_display_spec does, and we should return
5171 zero if it evaluates to nil. However, this function is
5172 called only when the buffer was already displayed and some
5173 glyph in the glyph matrix was found to come from a display
5174 string. Therefore, the condition was already evaluated, and
5175 the result was non-nil, otherwise the display string wouldn't
5176 have been displayed and we would have never been called for
5177 this property. Thus, we can skip the evaluation and assume
5178 its result is non-nil. */
5179 prop = XCDR (prop);
5180 }
5181
5182 if (CONSP (prop))
5183 /* Skip over `margin LOCATION'. */
5184 if (EQ (XCAR (prop), Qmargin))
5185 {
5186 prop = XCDR (prop);
5187 if (!CONSP (prop))
5188 return 0;
5189
5190 prop = XCDR (prop);
5191 if (!CONSP (prop))
5192 return 0;
5193 }
5194
5195 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5196 }
5197
5198
5199 /* Return 1 if STRING appears in the `display' property PROP. */
5200
5201 static int
5202 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5203 {
5204 if (CONSP (prop)
5205 && !EQ (XCAR (prop), Qwhen)
5206 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5207 {
5208 /* A list of sub-properties. */
5209 while (CONSP (prop))
5210 {
5211 if (single_display_spec_string_p (XCAR (prop), string))
5212 return 1;
5213 prop = XCDR (prop);
5214 }
5215 }
5216 else if (VECTORP (prop))
5217 {
5218 /* A vector of sub-properties. */
5219 ptrdiff_t i;
5220 for (i = 0; i < ASIZE (prop); ++i)
5221 if (single_display_spec_string_p (AREF (prop, i), string))
5222 return 1;
5223 }
5224 else
5225 return single_display_spec_string_p (prop, string);
5226
5227 return 0;
5228 }
5229
5230 /* Look for STRING in overlays and text properties in the current
5231 buffer, between character positions FROM and TO (excluding TO).
5232 BACK_P non-zero means look back (in this case, TO is supposed to be
5233 less than FROM).
5234 Value is the first character position where STRING was found, or
5235 zero if it wasn't found before hitting TO.
5236
5237 This function may only use code that doesn't eval because it is
5238 called asynchronously from note_mouse_highlight. */
5239
5240 static ptrdiff_t
5241 string_buffer_position_lim (Lisp_Object string,
5242 ptrdiff_t from, ptrdiff_t to, int back_p)
5243 {
5244 Lisp_Object limit, prop, pos;
5245 int found = 0;
5246
5247 pos = make_number (max (from, BEGV));
5248
5249 if (!back_p) /* looking forward */
5250 {
5251 limit = make_number (min (to, ZV));
5252 while (!found && !EQ (pos, limit))
5253 {
5254 prop = Fget_char_property (pos, Qdisplay, Qnil);
5255 if (!NILP (prop) && display_prop_string_p (prop, string))
5256 found = 1;
5257 else
5258 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5259 limit);
5260 }
5261 }
5262 else /* looking back */
5263 {
5264 limit = make_number (max (to, BEGV));
5265 while (!found && !EQ (pos, limit))
5266 {
5267 prop = Fget_char_property (pos, Qdisplay, Qnil);
5268 if (!NILP (prop) && display_prop_string_p (prop, string))
5269 found = 1;
5270 else
5271 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5272 limit);
5273 }
5274 }
5275
5276 return found ? XINT (pos) : 0;
5277 }
5278
5279 /* Determine which buffer position in current buffer STRING comes from.
5280 AROUND_CHARPOS is an approximate position where it could come from.
5281 Value is the buffer position or 0 if it couldn't be determined.
5282
5283 This function is necessary because we don't record buffer positions
5284 in glyphs generated from strings (to keep struct glyph small).
5285 This function may only use code that doesn't eval because it is
5286 called asynchronously from note_mouse_highlight. */
5287
5288 static ptrdiff_t
5289 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5290 {
5291 const int MAX_DISTANCE = 1000;
5292 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5293 around_charpos + MAX_DISTANCE,
5294 0);
5295
5296 if (!found)
5297 found = string_buffer_position_lim (string, around_charpos,
5298 around_charpos - MAX_DISTANCE, 1);
5299 return found;
5300 }
5301
5302
5303 \f
5304 /***********************************************************************
5305 `composition' property
5306 ***********************************************************************/
5307
5308 /* Set up iterator IT from `composition' property at its current
5309 position. Called from handle_stop. */
5310
5311 static enum prop_handled
5312 handle_composition_prop (struct it *it)
5313 {
5314 Lisp_Object prop, string;
5315 ptrdiff_t pos, pos_byte, start, end;
5316
5317 if (STRINGP (it->string))
5318 {
5319 unsigned char *s;
5320
5321 pos = IT_STRING_CHARPOS (*it);
5322 pos_byte = IT_STRING_BYTEPOS (*it);
5323 string = it->string;
5324 s = SDATA (string) + pos_byte;
5325 it->c = STRING_CHAR (s);
5326 }
5327 else
5328 {
5329 pos = IT_CHARPOS (*it);
5330 pos_byte = IT_BYTEPOS (*it);
5331 string = Qnil;
5332 it->c = FETCH_CHAR (pos_byte);
5333 }
5334
5335 /* If there's a valid composition and point is not inside of the
5336 composition (in the case that the composition is from the current
5337 buffer), draw a glyph composed from the composition components. */
5338 if (find_composition (pos, -1, &start, &end, &prop, string)
5339 && composition_valid_p (start, end, prop)
5340 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5341 {
5342 if (start < pos)
5343 /* As we can't handle this situation (perhaps font-lock added
5344 a new composition), we just return here hoping that next
5345 redisplay will detect this composition much earlier. */
5346 return HANDLED_NORMALLY;
5347 if (start != pos)
5348 {
5349 if (STRINGP (it->string))
5350 pos_byte = string_char_to_byte (it->string, start);
5351 else
5352 pos_byte = CHAR_TO_BYTE (start);
5353 }
5354 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5355 prop, string);
5356
5357 if (it->cmp_it.id >= 0)
5358 {
5359 it->cmp_it.ch = -1;
5360 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5361 it->cmp_it.nglyphs = -1;
5362 }
5363 }
5364
5365 return HANDLED_NORMALLY;
5366 }
5367
5368
5369 \f
5370 /***********************************************************************
5371 Overlay strings
5372 ***********************************************************************/
5373
5374 /* The following structure is used to record overlay strings for
5375 later sorting in load_overlay_strings. */
5376
5377 struct overlay_entry
5378 {
5379 Lisp_Object overlay;
5380 Lisp_Object string;
5381 EMACS_INT priority;
5382 int after_string_p;
5383 };
5384
5385
5386 /* Set up iterator IT from overlay strings at its current position.
5387 Called from handle_stop. */
5388
5389 static enum prop_handled
5390 handle_overlay_change (struct it *it)
5391 {
5392 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5393 return HANDLED_RECOMPUTE_PROPS;
5394 else
5395 return HANDLED_NORMALLY;
5396 }
5397
5398
5399 /* Set up the next overlay string for delivery by IT, if there is an
5400 overlay string to deliver. Called by set_iterator_to_next when the
5401 end of the current overlay string is reached. If there are more
5402 overlay strings to display, IT->string and
5403 IT->current.overlay_string_index are set appropriately here.
5404 Otherwise IT->string is set to nil. */
5405
5406 static void
5407 next_overlay_string (struct it *it)
5408 {
5409 ++it->current.overlay_string_index;
5410 if (it->current.overlay_string_index == it->n_overlay_strings)
5411 {
5412 /* No more overlay strings. Restore IT's settings to what
5413 they were before overlay strings were processed, and
5414 continue to deliver from current_buffer. */
5415
5416 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5417 pop_it (it);
5418 eassert (it->sp > 0
5419 || (NILP (it->string)
5420 && it->method == GET_FROM_BUFFER
5421 && it->stop_charpos >= BEGV
5422 && it->stop_charpos <= it->end_charpos));
5423 it->current.overlay_string_index = -1;
5424 it->n_overlay_strings = 0;
5425 it->overlay_strings_charpos = -1;
5426 /* If there's an empty display string on the stack, pop the
5427 stack, to resync the bidi iterator with IT's position. Such
5428 empty strings are pushed onto the stack in
5429 get_overlay_strings_1. */
5430 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5431 pop_it (it);
5432
5433 /* If we're at the end of the buffer, record that we have
5434 processed the overlay strings there already, so that
5435 next_element_from_buffer doesn't try it again. */
5436 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5437 it->overlay_strings_at_end_processed_p = true;
5438 }
5439 else
5440 {
5441 /* There are more overlay strings to process. If
5442 IT->current.overlay_string_index has advanced to a position
5443 where we must load IT->overlay_strings with more strings, do
5444 it. We must load at the IT->overlay_strings_charpos where
5445 IT->n_overlay_strings was originally computed; when invisible
5446 text is present, this might not be IT_CHARPOS (Bug#7016). */
5447 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5448
5449 if (it->current.overlay_string_index && i == 0)
5450 load_overlay_strings (it, it->overlay_strings_charpos);
5451
5452 /* Initialize IT to deliver display elements from the overlay
5453 string. */
5454 it->string = it->overlay_strings[i];
5455 it->multibyte_p = STRING_MULTIBYTE (it->string);
5456 SET_TEXT_POS (it->current.string_pos, 0, 0);
5457 it->method = GET_FROM_STRING;
5458 it->stop_charpos = 0;
5459 it->end_charpos = SCHARS (it->string);
5460 if (it->cmp_it.stop_pos >= 0)
5461 it->cmp_it.stop_pos = 0;
5462 it->prev_stop = 0;
5463 it->base_level_stop = 0;
5464
5465 /* Set up the bidi iterator for this overlay string. */
5466 if (it->bidi_p)
5467 {
5468 it->bidi_it.string.lstring = it->string;
5469 it->bidi_it.string.s = NULL;
5470 it->bidi_it.string.schars = SCHARS (it->string);
5471 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5472 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5473 it->bidi_it.string.unibyte = !it->multibyte_p;
5474 it->bidi_it.w = it->w;
5475 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5476 }
5477 }
5478
5479 CHECK_IT (it);
5480 }
5481
5482
5483 /* Compare two overlay_entry structures E1 and E2. Used as a
5484 comparison function for qsort in load_overlay_strings. Overlay
5485 strings for the same position are sorted so that
5486
5487 1. All after-strings come in front of before-strings, except
5488 when they come from the same overlay.
5489
5490 2. Within after-strings, strings are sorted so that overlay strings
5491 from overlays with higher priorities come first.
5492
5493 2. Within before-strings, strings are sorted so that overlay
5494 strings from overlays with higher priorities come last.
5495
5496 Value is analogous to strcmp. */
5497
5498
5499 static int
5500 compare_overlay_entries (const void *e1, const void *e2)
5501 {
5502 struct overlay_entry const *entry1 = e1;
5503 struct overlay_entry const *entry2 = e2;
5504 int result;
5505
5506 if (entry1->after_string_p != entry2->after_string_p)
5507 {
5508 /* Let after-strings appear in front of before-strings if
5509 they come from different overlays. */
5510 if (EQ (entry1->overlay, entry2->overlay))
5511 result = entry1->after_string_p ? 1 : -1;
5512 else
5513 result = entry1->after_string_p ? -1 : 1;
5514 }
5515 else if (entry1->priority != entry2->priority)
5516 {
5517 if (entry1->after_string_p)
5518 /* After-strings sorted in order of decreasing priority. */
5519 result = entry2->priority < entry1->priority ? -1 : 1;
5520 else
5521 /* Before-strings sorted in order of increasing priority. */
5522 result = entry1->priority < entry2->priority ? -1 : 1;
5523 }
5524 else
5525 result = 0;
5526
5527 return result;
5528 }
5529
5530
5531 /* Load the vector IT->overlay_strings with overlay strings from IT's
5532 current buffer position, or from CHARPOS if that is > 0. Set
5533 IT->n_overlays to the total number of overlay strings found.
5534
5535 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5536 a time. On entry into load_overlay_strings,
5537 IT->current.overlay_string_index gives the number of overlay
5538 strings that have already been loaded by previous calls to this
5539 function.
5540
5541 IT->add_overlay_start contains an additional overlay start
5542 position to consider for taking overlay strings from, if non-zero.
5543 This position comes into play when the overlay has an `invisible'
5544 property, and both before and after-strings. When we've skipped to
5545 the end of the overlay, because of its `invisible' property, we
5546 nevertheless want its before-string to appear.
5547 IT->add_overlay_start will contain the overlay start position
5548 in this case.
5549
5550 Overlay strings are sorted so that after-string strings come in
5551 front of before-string strings. Within before and after-strings,
5552 strings are sorted by overlay priority. See also function
5553 compare_overlay_entries. */
5554
5555 static void
5556 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5557 {
5558 Lisp_Object overlay, window, str, invisible;
5559 struct Lisp_Overlay *ov;
5560 ptrdiff_t start, end;
5561 ptrdiff_t size = 20;
5562 ptrdiff_t n = 0, i, j;
5563 int invis_p;
5564 struct overlay_entry *entries = alloca (size * sizeof *entries);
5565 USE_SAFE_ALLOCA;
5566
5567 if (charpos <= 0)
5568 charpos = IT_CHARPOS (*it);
5569
5570 /* Append the overlay string STRING of overlay OVERLAY to vector
5571 `entries' which has size `size' and currently contains `n'
5572 elements. AFTER_P non-zero means STRING is an after-string of
5573 OVERLAY. */
5574 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5575 do \
5576 { \
5577 Lisp_Object priority; \
5578 \
5579 if (n == size) \
5580 { \
5581 struct overlay_entry *old = entries; \
5582 SAFE_NALLOCA (entries, 2, size); \
5583 memcpy (entries, old, size * sizeof *entries); \
5584 size *= 2; \
5585 } \
5586 \
5587 entries[n].string = (STRING); \
5588 entries[n].overlay = (OVERLAY); \
5589 priority = Foverlay_get ((OVERLAY), Qpriority); \
5590 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5591 entries[n].after_string_p = (AFTER_P); \
5592 ++n; \
5593 } \
5594 while (0)
5595
5596 /* Process overlay before the overlay center. */
5597 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5598 {
5599 XSETMISC (overlay, ov);
5600 eassert (OVERLAYP (overlay));
5601 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5602 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5603
5604 if (end < charpos)
5605 break;
5606
5607 /* Skip this overlay if it doesn't start or end at IT's current
5608 position. */
5609 if (end != charpos && start != charpos)
5610 continue;
5611
5612 /* Skip this overlay if it doesn't apply to IT->w. */
5613 window = Foverlay_get (overlay, Qwindow);
5614 if (WINDOWP (window) && XWINDOW (window) != it->w)
5615 continue;
5616
5617 /* If the text ``under'' the overlay is invisible, both before-
5618 and after-strings from this overlay are visible; start and
5619 end position are indistinguishable. */
5620 invisible = Foverlay_get (overlay, Qinvisible);
5621 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5622
5623 /* If overlay has a non-empty before-string, record it. */
5624 if ((start == charpos || (end == charpos && invis_p))
5625 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5626 && SCHARS (str))
5627 RECORD_OVERLAY_STRING (overlay, str, 0);
5628
5629 /* If overlay has a non-empty after-string, record it. */
5630 if ((end == charpos || (start == charpos && invis_p))
5631 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5632 && SCHARS (str))
5633 RECORD_OVERLAY_STRING (overlay, str, 1);
5634 }
5635
5636 /* Process overlays after the overlay center. */
5637 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5638 {
5639 XSETMISC (overlay, ov);
5640 eassert (OVERLAYP (overlay));
5641 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5642 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5643
5644 if (start > charpos)
5645 break;
5646
5647 /* Skip this overlay if it doesn't start or end at IT's current
5648 position. */
5649 if (end != charpos && start != charpos)
5650 continue;
5651
5652 /* Skip this overlay if it doesn't apply to IT->w. */
5653 window = Foverlay_get (overlay, Qwindow);
5654 if (WINDOWP (window) && XWINDOW (window) != it->w)
5655 continue;
5656
5657 /* If the text ``under'' the overlay is invisible, it has a zero
5658 dimension, and both before- and after-strings apply. */
5659 invisible = Foverlay_get (overlay, Qinvisible);
5660 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5661
5662 /* If overlay has a non-empty before-string, record it. */
5663 if ((start == charpos || (end == charpos && invis_p))
5664 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5665 && SCHARS (str))
5666 RECORD_OVERLAY_STRING (overlay, str, 0);
5667
5668 /* If overlay has a non-empty after-string, record it. */
5669 if ((end == charpos || (start == charpos && invis_p))
5670 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5671 && SCHARS (str))
5672 RECORD_OVERLAY_STRING (overlay, str, 1);
5673 }
5674
5675 #undef RECORD_OVERLAY_STRING
5676
5677 /* Sort entries. */
5678 if (n > 1)
5679 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5680
5681 /* Record number of overlay strings, and where we computed it. */
5682 it->n_overlay_strings = n;
5683 it->overlay_strings_charpos = charpos;
5684
5685 /* IT->current.overlay_string_index is the number of overlay strings
5686 that have already been consumed by IT. Copy some of the
5687 remaining overlay strings to IT->overlay_strings. */
5688 i = 0;
5689 j = it->current.overlay_string_index;
5690 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5691 {
5692 it->overlay_strings[i] = entries[j].string;
5693 it->string_overlays[i++] = entries[j++].overlay;
5694 }
5695
5696 CHECK_IT (it);
5697 SAFE_FREE ();
5698 }
5699
5700
5701 /* Get the first chunk of overlay strings at IT's current buffer
5702 position, or at CHARPOS if that is > 0. Value is non-zero if at
5703 least one overlay string was found. */
5704
5705 static int
5706 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5707 {
5708 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5709 process. This fills IT->overlay_strings with strings, and sets
5710 IT->n_overlay_strings to the total number of strings to process.
5711 IT->pos.overlay_string_index has to be set temporarily to zero
5712 because load_overlay_strings needs this; it must be set to -1
5713 when no overlay strings are found because a zero value would
5714 indicate a position in the first overlay string. */
5715 it->current.overlay_string_index = 0;
5716 load_overlay_strings (it, charpos);
5717
5718 /* If we found overlay strings, set up IT to deliver display
5719 elements from the first one. Otherwise set up IT to deliver
5720 from current_buffer. */
5721 if (it->n_overlay_strings)
5722 {
5723 /* Make sure we know settings in current_buffer, so that we can
5724 restore meaningful values when we're done with the overlay
5725 strings. */
5726 if (compute_stop_p)
5727 compute_stop_pos (it);
5728 eassert (it->face_id >= 0);
5729
5730 /* Save IT's settings. They are restored after all overlay
5731 strings have been processed. */
5732 eassert (!compute_stop_p || it->sp == 0);
5733
5734 /* When called from handle_stop, there might be an empty display
5735 string loaded. In that case, don't bother saving it. But
5736 don't use this optimization with the bidi iterator, since we
5737 need the corresponding pop_it call to resync the bidi
5738 iterator's position with IT's position, after we are done
5739 with the overlay strings. (The corresponding call to pop_it
5740 in case of an empty display string is in
5741 next_overlay_string.) */
5742 if (!(!it->bidi_p
5743 && STRINGP (it->string) && !SCHARS (it->string)))
5744 push_it (it, NULL);
5745
5746 /* Set up IT to deliver display elements from the first overlay
5747 string. */
5748 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5749 it->string = it->overlay_strings[0];
5750 it->from_overlay = Qnil;
5751 it->stop_charpos = 0;
5752 eassert (STRINGP (it->string));
5753 it->end_charpos = SCHARS (it->string);
5754 it->prev_stop = 0;
5755 it->base_level_stop = 0;
5756 it->multibyte_p = STRING_MULTIBYTE (it->string);
5757 it->method = GET_FROM_STRING;
5758 it->from_disp_prop_p = 0;
5759
5760 /* Force paragraph direction to be that of the parent
5761 buffer. */
5762 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5763 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5764 else
5765 it->paragraph_embedding = L2R;
5766
5767 /* Set up the bidi iterator for this overlay string. */
5768 if (it->bidi_p)
5769 {
5770 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5771
5772 it->bidi_it.string.lstring = it->string;
5773 it->bidi_it.string.s = NULL;
5774 it->bidi_it.string.schars = SCHARS (it->string);
5775 it->bidi_it.string.bufpos = pos;
5776 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5777 it->bidi_it.string.unibyte = !it->multibyte_p;
5778 it->bidi_it.w = it->w;
5779 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5780 }
5781 return 1;
5782 }
5783
5784 it->current.overlay_string_index = -1;
5785 return 0;
5786 }
5787
5788 static int
5789 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5790 {
5791 it->string = Qnil;
5792 it->method = GET_FROM_BUFFER;
5793
5794 (void) get_overlay_strings_1 (it, charpos, 1);
5795
5796 CHECK_IT (it);
5797
5798 /* Value is non-zero if we found at least one overlay string. */
5799 return STRINGP (it->string);
5800 }
5801
5802
5803 \f
5804 /***********************************************************************
5805 Saving and restoring state
5806 ***********************************************************************/
5807
5808 /* Save current settings of IT on IT->stack. Called, for example,
5809 before setting up IT for an overlay string, to be able to restore
5810 IT's settings to what they were after the overlay string has been
5811 processed. If POSITION is non-NULL, it is the position to save on
5812 the stack instead of IT->position. */
5813
5814 static void
5815 push_it (struct it *it, struct text_pos *position)
5816 {
5817 struct iterator_stack_entry *p;
5818
5819 eassert (it->sp < IT_STACK_SIZE);
5820 p = it->stack + it->sp;
5821
5822 p->stop_charpos = it->stop_charpos;
5823 p->prev_stop = it->prev_stop;
5824 p->base_level_stop = it->base_level_stop;
5825 p->cmp_it = it->cmp_it;
5826 eassert (it->face_id >= 0);
5827 p->face_id = it->face_id;
5828 p->string = it->string;
5829 p->method = it->method;
5830 p->from_overlay = it->from_overlay;
5831 switch (p->method)
5832 {
5833 case GET_FROM_IMAGE:
5834 p->u.image.object = it->object;
5835 p->u.image.image_id = it->image_id;
5836 p->u.image.slice = it->slice;
5837 break;
5838 case GET_FROM_STRETCH:
5839 p->u.stretch.object = it->object;
5840 break;
5841 }
5842 p->position = position ? *position : it->position;
5843 p->current = it->current;
5844 p->end_charpos = it->end_charpos;
5845 p->string_nchars = it->string_nchars;
5846 p->area = it->area;
5847 p->multibyte_p = it->multibyte_p;
5848 p->avoid_cursor_p = it->avoid_cursor_p;
5849 p->space_width = it->space_width;
5850 p->font_height = it->font_height;
5851 p->voffset = it->voffset;
5852 p->string_from_display_prop_p = it->string_from_display_prop_p;
5853 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5854 p->display_ellipsis_p = 0;
5855 p->line_wrap = it->line_wrap;
5856 p->bidi_p = it->bidi_p;
5857 p->paragraph_embedding = it->paragraph_embedding;
5858 p->from_disp_prop_p = it->from_disp_prop_p;
5859 ++it->sp;
5860
5861 /* Save the state of the bidi iterator as well. */
5862 if (it->bidi_p)
5863 bidi_push_it (&it->bidi_it);
5864 }
5865
5866 static void
5867 iterate_out_of_display_property (struct it *it)
5868 {
5869 int buffer_p = !STRINGP (it->string);
5870 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5871 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5872
5873 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5874
5875 /* Maybe initialize paragraph direction. If we are at the beginning
5876 of a new paragraph, next_element_from_buffer may not have a
5877 chance to do that. */
5878 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5879 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5880 /* prev_stop can be zero, so check against BEGV as well. */
5881 while (it->bidi_it.charpos >= bob
5882 && it->prev_stop <= it->bidi_it.charpos
5883 && it->bidi_it.charpos < CHARPOS (it->position)
5884 && it->bidi_it.charpos < eob)
5885 bidi_move_to_visually_next (&it->bidi_it);
5886 /* Record the stop_pos we just crossed, for when we cross it
5887 back, maybe. */
5888 if (it->bidi_it.charpos > CHARPOS (it->position))
5889 it->prev_stop = CHARPOS (it->position);
5890 /* If we ended up not where pop_it put us, resync IT's
5891 positional members with the bidi iterator. */
5892 if (it->bidi_it.charpos != CHARPOS (it->position))
5893 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5894 if (buffer_p)
5895 it->current.pos = it->position;
5896 else
5897 it->current.string_pos = it->position;
5898 }
5899
5900 /* Restore IT's settings from IT->stack. Called, for example, when no
5901 more overlay strings must be processed, and we return to delivering
5902 display elements from a buffer, or when the end of a string from a
5903 `display' property is reached and we return to delivering display
5904 elements from an overlay string, or from a buffer. */
5905
5906 static void
5907 pop_it (struct it *it)
5908 {
5909 struct iterator_stack_entry *p;
5910 int from_display_prop = it->from_disp_prop_p;
5911
5912 eassert (it->sp > 0);
5913 --it->sp;
5914 p = it->stack + it->sp;
5915 it->stop_charpos = p->stop_charpos;
5916 it->prev_stop = p->prev_stop;
5917 it->base_level_stop = p->base_level_stop;
5918 it->cmp_it = p->cmp_it;
5919 it->face_id = p->face_id;
5920 it->current = p->current;
5921 it->position = p->position;
5922 it->string = p->string;
5923 it->from_overlay = p->from_overlay;
5924 if (NILP (it->string))
5925 SET_TEXT_POS (it->current.string_pos, -1, -1);
5926 it->method = p->method;
5927 switch (it->method)
5928 {
5929 case GET_FROM_IMAGE:
5930 it->image_id = p->u.image.image_id;
5931 it->object = p->u.image.object;
5932 it->slice = p->u.image.slice;
5933 break;
5934 case GET_FROM_STRETCH:
5935 it->object = p->u.stretch.object;
5936 break;
5937 case GET_FROM_BUFFER:
5938 it->object = it->w->contents;
5939 break;
5940 case GET_FROM_STRING:
5941 it->object = it->string;
5942 break;
5943 case GET_FROM_DISPLAY_VECTOR:
5944 if (it->s)
5945 it->method = GET_FROM_C_STRING;
5946 else if (STRINGP (it->string))
5947 it->method = GET_FROM_STRING;
5948 else
5949 {
5950 it->method = GET_FROM_BUFFER;
5951 it->object = it->w->contents;
5952 }
5953 }
5954 it->end_charpos = p->end_charpos;
5955 it->string_nchars = p->string_nchars;
5956 it->area = p->area;
5957 it->multibyte_p = p->multibyte_p;
5958 it->avoid_cursor_p = p->avoid_cursor_p;
5959 it->space_width = p->space_width;
5960 it->font_height = p->font_height;
5961 it->voffset = p->voffset;
5962 it->string_from_display_prop_p = p->string_from_display_prop_p;
5963 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5964 it->line_wrap = p->line_wrap;
5965 it->bidi_p = p->bidi_p;
5966 it->paragraph_embedding = p->paragraph_embedding;
5967 it->from_disp_prop_p = p->from_disp_prop_p;
5968 if (it->bidi_p)
5969 {
5970 bidi_pop_it (&it->bidi_it);
5971 /* Bidi-iterate until we get out of the portion of text, if any,
5972 covered by a `display' text property or by an overlay with
5973 `display' property. (We cannot just jump there, because the
5974 internal coherency of the bidi iterator state can not be
5975 preserved across such jumps.) We also must determine the
5976 paragraph base direction if the overlay we just processed is
5977 at the beginning of a new paragraph. */
5978 if (from_display_prop
5979 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5980 iterate_out_of_display_property (it);
5981
5982 eassert ((BUFFERP (it->object)
5983 && IT_CHARPOS (*it) == it->bidi_it.charpos
5984 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5985 || (STRINGP (it->object)
5986 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5987 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5988 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5989 }
5990 }
5991
5992
5993 \f
5994 /***********************************************************************
5995 Moving over lines
5996 ***********************************************************************/
5997
5998 /* Set IT's current position to the previous line start. */
5999
6000 static void
6001 back_to_previous_line_start (struct it *it)
6002 {
6003 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6004
6005 DEC_BOTH (cp, bp);
6006 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6007 }
6008
6009
6010 /* Move IT to the next line start.
6011
6012 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6013 we skipped over part of the text (as opposed to moving the iterator
6014 continuously over the text). Otherwise, don't change the value
6015 of *SKIPPED_P.
6016
6017 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6018 iterator on the newline, if it was found.
6019
6020 Newlines may come from buffer text, overlay strings, or strings
6021 displayed via the `display' property. That's the reason we can't
6022 simply use find_newline_no_quit.
6023
6024 Note that this function may not skip over invisible text that is so
6025 because of text properties and immediately follows a newline. If
6026 it would, function reseat_at_next_visible_line_start, when called
6027 from set_iterator_to_next, would effectively make invisible
6028 characters following a newline part of the wrong glyph row, which
6029 leads to wrong cursor motion. */
6030
6031 static int
6032 forward_to_next_line_start (struct it *it, int *skipped_p,
6033 struct bidi_it *bidi_it_prev)
6034 {
6035 ptrdiff_t old_selective;
6036 int newline_found_p, n;
6037 const int MAX_NEWLINE_DISTANCE = 500;
6038
6039 /* If already on a newline, just consume it to avoid unintended
6040 skipping over invisible text below. */
6041 if (it->what == IT_CHARACTER
6042 && it->c == '\n'
6043 && CHARPOS (it->position) == IT_CHARPOS (*it))
6044 {
6045 if (it->bidi_p && bidi_it_prev)
6046 *bidi_it_prev = it->bidi_it;
6047 set_iterator_to_next (it, 0);
6048 it->c = 0;
6049 return 1;
6050 }
6051
6052 /* Don't handle selective display in the following. It's (a)
6053 unnecessary because it's done by the caller, and (b) leads to an
6054 infinite recursion because next_element_from_ellipsis indirectly
6055 calls this function. */
6056 old_selective = it->selective;
6057 it->selective = 0;
6058
6059 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6060 from buffer text. */
6061 for (n = newline_found_p = 0;
6062 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6063 n += STRINGP (it->string) ? 0 : 1)
6064 {
6065 if (!get_next_display_element (it))
6066 return 0;
6067 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6068 if (newline_found_p && it->bidi_p && bidi_it_prev)
6069 *bidi_it_prev = it->bidi_it;
6070 set_iterator_to_next (it, 0);
6071 }
6072
6073 /* If we didn't find a newline near enough, see if we can use a
6074 short-cut. */
6075 if (!newline_found_p)
6076 {
6077 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6078 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6079 1, &bytepos);
6080 Lisp_Object pos;
6081
6082 eassert (!STRINGP (it->string));
6083
6084 /* If there isn't any `display' property in sight, and no
6085 overlays, we can just use the position of the newline in
6086 buffer text. */
6087 if (it->stop_charpos >= limit
6088 || ((pos = Fnext_single_property_change (make_number (start),
6089 Qdisplay, Qnil,
6090 make_number (limit)),
6091 NILP (pos))
6092 && next_overlay_change (start) == ZV))
6093 {
6094 if (!it->bidi_p)
6095 {
6096 IT_CHARPOS (*it) = limit;
6097 IT_BYTEPOS (*it) = bytepos;
6098 }
6099 else
6100 {
6101 struct bidi_it bprev;
6102
6103 /* Help bidi.c avoid expensive searches for display
6104 properties and overlays, by telling it that there are
6105 none up to `limit'. */
6106 if (it->bidi_it.disp_pos < limit)
6107 {
6108 it->bidi_it.disp_pos = limit;
6109 it->bidi_it.disp_prop = 0;
6110 }
6111 do {
6112 bprev = it->bidi_it;
6113 bidi_move_to_visually_next (&it->bidi_it);
6114 } while (it->bidi_it.charpos != limit);
6115 IT_CHARPOS (*it) = limit;
6116 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6117 if (bidi_it_prev)
6118 *bidi_it_prev = bprev;
6119 }
6120 *skipped_p = newline_found_p = true;
6121 }
6122 else
6123 {
6124 while (get_next_display_element (it)
6125 && !newline_found_p)
6126 {
6127 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6128 if (newline_found_p && it->bidi_p && bidi_it_prev)
6129 *bidi_it_prev = it->bidi_it;
6130 set_iterator_to_next (it, 0);
6131 }
6132 }
6133 }
6134
6135 it->selective = old_selective;
6136 return newline_found_p;
6137 }
6138
6139
6140 /* Set IT's current position to the previous visible line start. Skip
6141 invisible text that is so either due to text properties or due to
6142 selective display. Caution: this does not change IT->current_x and
6143 IT->hpos. */
6144
6145 static void
6146 back_to_previous_visible_line_start (struct it *it)
6147 {
6148 while (IT_CHARPOS (*it) > BEGV)
6149 {
6150 back_to_previous_line_start (it);
6151
6152 if (IT_CHARPOS (*it) <= BEGV)
6153 break;
6154
6155 /* If selective > 0, then lines indented more than its value are
6156 invisible. */
6157 if (it->selective > 0
6158 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6159 it->selective))
6160 continue;
6161
6162 /* Check the newline before point for invisibility. */
6163 {
6164 Lisp_Object prop;
6165 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6166 Qinvisible, it->window);
6167 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6168 continue;
6169 }
6170
6171 if (IT_CHARPOS (*it) <= BEGV)
6172 break;
6173
6174 {
6175 struct it it2;
6176 void *it2data = NULL;
6177 ptrdiff_t pos;
6178 ptrdiff_t beg, end;
6179 Lisp_Object val, overlay;
6180
6181 SAVE_IT (it2, *it, it2data);
6182
6183 /* If newline is part of a composition, continue from start of composition */
6184 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6185 && beg < IT_CHARPOS (*it))
6186 goto replaced;
6187
6188 /* If newline is replaced by a display property, find start of overlay
6189 or interval and continue search from that point. */
6190 pos = --IT_CHARPOS (it2);
6191 --IT_BYTEPOS (it2);
6192 it2.sp = 0;
6193 bidi_unshelve_cache (NULL, 0);
6194 it2.string_from_display_prop_p = 0;
6195 it2.from_disp_prop_p = 0;
6196 if (handle_display_prop (&it2) == HANDLED_RETURN
6197 && !NILP (val = get_char_property_and_overlay
6198 (make_number (pos), Qdisplay, Qnil, &overlay))
6199 && (OVERLAYP (overlay)
6200 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6201 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6202 {
6203 RESTORE_IT (it, it, it2data);
6204 goto replaced;
6205 }
6206
6207 /* Newline is not replaced by anything -- so we are done. */
6208 RESTORE_IT (it, it, it2data);
6209 break;
6210
6211 replaced:
6212 if (beg < BEGV)
6213 beg = BEGV;
6214 IT_CHARPOS (*it) = beg;
6215 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6216 }
6217 }
6218
6219 it->continuation_lines_width = 0;
6220
6221 eassert (IT_CHARPOS (*it) >= BEGV);
6222 eassert (IT_CHARPOS (*it) == BEGV
6223 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6224 CHECK_IT (it);
6225 }
6226
6227
6228 /* Reseat iterator IT at the previous visible line start. Skip
6229 invisible text that is so either due to text properties or due to
6230 selective display. At the end, update IT's overlay information,
6231 face information etc. */
6232
6233 void
6234 reseat_at_previous_visible_line_start (struct it *it)
6235 {
6236 back_to_previous_visible_line_start (it);
6237 reseat (it, it->current.pos, 1);
6238 CHECK_IT (it);
6239 }
6240
6241
6242 /* Reseat iterator IT on the next visible line start in the current
6243 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6244 preceding the line start. Skip over invisible text that is so
6245 because of selective display. Compute faces, overlays etc at the
6246 new position. Note that this function does not skip over text that
6247 is invisible because of text properties. */
6248
6249 static void
6250 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6251 {
6252 int newline_found_p, skipped_p = 0;
6253 struct bidi_it bidi_it_prev;
6254
6255 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6256
6257 /* Skip over lines that are invisible because they are indented
6258 more than the value of IT->selective. */
6259 if (it->selective > 0)
6260 while (IT_CHARPOS (*it) < ZV
6261 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6262 it->selective))
6263 {
6264 eassert (IT_BYTEPOS (*it) == BEGV
6265 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6266 newline_found_p =
6267 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6268 }
6269
6270 /* Position on the newline if that's what's requested. */
6271 if (on_newline_p && newline_found_p)
6272 {
6273 if (STRINGP (it->string))
6274 {
6275 if (IT_STRING_CHARPOS (*it) > 0)
6276 {
6277 if (!it->bidi_p)
6278 {
6279 --IT_STRING_CHARPOS (*it);
6280 --IT_STRING_BYTEPOS (*it);
6281 }
6282 else
6283 {
6284 /* We need to restore the bidi iterator to the state
6285 it had on the newline, and resync the IT's
6286 position with that. */
6287 it->bidi_it = bidi_it_prev;
6288 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6289 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6290 }
6291 }
6292 }
6293 else if (IT_CHARPOS (*it) > BEGV)
6294 {
6295 if (!it->bidi_p)
6296 {
6297 --IT_CHARPOS (*it);
6298 --IT_BYTEPOS (*it);
6299 }
6300 else
6301 {
6302 /* We need to restore the bidi iterator to the state it
6303 had on the newline and resync IT with that. */
6304 it->bidi_it = bidi_it_prev;
6305 IT_CHARPOS (*it) = it->bidi_it.charpos;
6306 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6307 }
6308 reseat (it, it->current.pos, 0);
6309 }
6310 }
6311 else if (skipped_p)
6312 reseat (it, it->current.pos, 0);
6313
6314 CHECK_IT (it);
6315 }
6316
6317
6318 \f
6319 /***********************************************************************
6320 Changing an iterator's position
6321 ***********************************************************************/
6322
6323 /* Change IT's current position to POS in current_buffer. If FORCE_P
6324 is non-zero, always check for text properties at the new position.
6325 Otherwise, text properties are only looked up if POS >=
6326 IT->check_charpos of a property. */
6327
6328 static void
6329 reseat (struct it *it, struct text_pos pos, int force_p)
6330 {
6331 ptrdiff_t original_pos = IT_CHARPOS (*it);
6332
6333 reseat_1 (it, pos, 0);
6334
6335 /* Determine where to check text properties. Avoid doing it
6336 where possible because text property lookup is very expensive. */
6337 if (force_p
6338 || CHARPOS (pos) > it->stop_charpos
6339 || CHARPOS (pos) < original_pos)
6340 {
6341 if (it->bidi_p)
6342 {
6343 /* For bidi iteration, we need to prime prev_stop and
6344 base_level_stop with our best estimations. */
6345 /* Implementation note: Of course, POS is not necessarily a
6346 stop position, so assigning prev_pos to it is a lie; we
6347 should have called compute_stop_backwards. However, if
6348 the current buffer does not include any R2L characters,
6349 that call would be a waste of cycles, because the
6350 iterator will never move back, and thus never cross this
6351 "fake" stop position. So we delay that backward search
6352 until the time we really need it, in next_element_from_buffer. */
6353 if (CHARPOS (pos) != it->prev_stop)
6354 it->prev_stop = CHARPOS (pos);
6355 if (CHARPOS (pos) < it->base_level_stop)
6356 it->base_level_stop = 0; /* meaning it's unknown */
6357 handle_stop (it);
6358 }
6359 else
6360 {
6361 handle_stop (it);
6362 it->prev_stop = it->base_level_stop = 0;
6363 }
6364
6365 }
6366
6367 CHECK_IT (it);
6368 }
6369
6370
6371 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6372 IT->stop_pos to POS, also. */
6373
6374 static void
6375 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6376 {
6377 /* Don't call this function when scanning a C string. */
6378 eassert (it->s == NULL);
6379
6380 /* POS must be a reasonable value. */
6381 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6382
6383 it->current.pos = it->position = pos;
6384 it->end_charpos = ZV;
6385 it->dpvec = NULL;
6386 it->current.dpvec_index = -1;
6387 it->current.overlay_string_index = -1;
6388 IT_STRING_CHARPOS (*it) = -1;
6389 IT_STRING_BYTEPOS (*it) = -1;
6390 it->string = Qnil;
6391 it->method = GET_FROM_BUFFER;
6392 it->object = it->w->contents;
6393 it->area = TEXT_AREA;
6394 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6395 it->sp = 0;
6396 it->string_from_display_prop_p = 0;
6397 it->string_from_prefix_prop_p = 0;
6398
6399 it->from_disp_prop_p = 0;
6400 it->face_before_selective_p = 0;
6401 if (it->bidi_p)
6402 {
6403 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6404 &it->bidi_it);
6405 bidi_unshelve_cache (NULL, 0);
6406 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6407 it->bidi_it.string.s = NULL;
6408 it->bidi_it.string.lstring = Qnil;
6409 it->bidi_it.string.bufpos = 0;
6410 it->bidi_it.string.unibyte = 0;
6411 it->bidi_it.w = it->w;
6412 }
6413
6414 if (set_stop_p)
6415 {
6416 it->stop_charpos = CHARPOS (pos);
6417 it->base_level_stop = CHARPOS (pos);
6418 }
6419 /* This make the information stored in it->cmp_it invalidate. */
6420 it->cmp_it.id = -1;
6421 }
6422
6423
6424 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6425 If S is non-null, it is a C string to iterate over. Otherwise,
6426 STRING gives a Lisp string to iterate over.
6427
6428 If PRECISION > 0, don't return more then PRECISION number of
6429 characters from the string.
6430
6431 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6432 characters have been returned. FIELD_WIDTH < 0 means an infinite
6433 field width.
6434
6435 MULTIBYTE = 0 means disable processing of multibyte characters,
6436 MULTIBYTE > 0 means enable it,
6437 MULTIBYTE < 0 means use IT->multibyte_p.
6438
6439 IT must be initialized via a prior call to init_iterator before
6440 calling this function. */
6441
6442 static void
6443 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6444 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6445 int multibyte)
6446 {
6447 /* No text property checks performed by default, but see below. */
6448 it->stop_charpos = -1;
6449
6450 /* Set iterator position and end position. */
6451 memset (&it->current, 0, sizeof it->current);
6452 it->current.overlay_string_index = -1;
6453 it->current.dpvec_index = -1;
6454 eassert (charpos >= 0);
6455
6456 /* If STRING is specified, use its multibyteness, otherwise use the
6457 setting of MULTIBYTE, if specified. */
6458 if (multibyte >= 0)
6459 it->multibyte_p = multibyte > 0;
6460
6461 /* Bidirectional reordering of strings is controlled by the default
6462 value of bidi-display-reordering. Don't try to reorder while
6463 loading loadup.el, as the necessary character property tables are
6464 not yet available. */
6465 it->bidi_p =
6466 NILP (Vpurify_flag)
6467 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6468
6469 if (s == NULL)
6470 {
6471 eassert (STRINGP (string));
6472 it->string = string;
6473 it->s = NULL;
6474 it->end_charpos = it->string_nchars = SCHARS (string);
6475 it->method = GET_FROM_STRING;
6476 it->current.string_pos = string_pos (charpos, string);
6477
6478 if (it->bidi_p)
6479 {
6480 it->bidi_it.string.lstring = string;
6481 it->bidi_it.string.s = NULL;
6482 it->bidi_it.string.schars = it->end_charpos;
6483 it->bidi_it.string.bufpos = 0;
6484 it->bidi_it.string.from_disp_str = 0;
6485 it->bidi_it.string.unibyte = !it->multibyte_p;
6486 it->bidi_it.w = it->w;
6487 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6488 FRAME_WINDOW_P (it->f), &it->bidi_it);
6489 }
6490 }
6491 else
6492 {
6493 it->s = (const unsigned char *) s;
6494 it->string = Qnil;
6495
6496 /* Note that we use IT->current.pos, not it->current.string_pos,
6497 for displaying C strings. */
6498 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6499 if (it->multibyte_p)
6500 {
6501 it->current.pos = c_string_pos (charpos, s, 1);
6502 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6503 }
6504 else
6505 {
6506 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6507 it->end_charpos = it->string_nchars = strlen (s);
6508 }
6509
6510 if (it->bidi_p)
6511 {
6512 it->bidi_it.string.lstring = Qnil;
6513 it->bidi_it.string.s = (const unsigned char *) s;
6514 it->bidi_it.string.schars = it->end_charpos;
6515 it->bidi_it.string.bufpos = 0;
6516 it->bidi_it.string.from_disp_str = 0;
6517 it->bidi_it.string.unibyte = !it->multibyte_p;
6518 it->bidi_it.w = it->w;
6519 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6520 &it->bidi_it);
6521 }
6522 it->method = GET_FROM_C_STRING;
6523 }
6524
6525 /* PRECISION > 0 means don't return more than PRECISION characters
6526 from the string. */
6527 if (precision > 0 && it->end_charpos - charpos > precision)
6528 {
6529 it->end_charpos = it->string_nchars = charpos + precision;
6530 if (it->bidi_p)
6531 it->bidi_it.string.schars = it->end_charpos;
6532 }
6533
6534 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6535 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6536 FIELD_WIDTH < 0 means infinite field width. This is useful for
6537 padding with `-' at the end of a mode line. */
6538 if (field_width < 0)
6539 field_width = INFINITY;
6540 /* Implementation note: We deliberately don't enlarge
6541 it->bidi_it.string.schars here to fit it->end_charpos, because
6542 the bidi iterator cannot produce characters out of thin air. */
6543 if (field_width > it->end_charpos - charpos)
6544 it->end_charpos = charpos + field_width;
6545
6546 /* Use the standard display table for displaying strings. */
6547 if (DISP_TABLE_P (Vstandard_display_table))
6548 it->dp = XCHAR_TABLE (Vstandard_display_table);
6549
6550 it->stop_charpos = charpos;
6551 it->prev_stop = charpos;
6552 it->base_level_stop = 0;
6553 if (it->bidi_p)
6554 {
6555 it->bidi_it.first_elt = 1;
6556 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6557 it->bidi_it.disp_pos = -1;
6558 }
6559 if (s == NULL && it->multibyte_p)
6560 {
6561 ptrdiff_t endpos = SCHARS (it->string);
6562 if (endpos > it->end_charpos)
6563 endpos = it->end_charpos;
6564 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6565 it->string);
6566 }
6567 CHECK_IT (it);
6568 }
6569
6570
6571 \f
6572 /***********************************************************************
6573 Iteration
6574 ***********************************************************************/
6575
6576 /* Map enum it_method value to corresponding next_element_from_* function. */
6577
6578 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6579 {
6580 next_element_from_buffer,
6581 next_element_from_display_vector,
6582 next_element_from_string,
6583 next_element_from_c_string,
6584 next_element_from_image,
6585 next_element_from_stretch
6586 };
6587
6588 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6589
6590
6591 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6592 (possibly with the following characters). */
6593
6594 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6595 ((IT)->cmp_it.id >= 0 \
6596 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6597 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6598 END_CHARPOS, (IT)->w, \
6599 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6600 (IT)->string)))
6601
6602
6603 /* Lookup the char-table Vglyphless_char_display for character C (-1
6604 if we want information for no-font case), and return the display
6605 method symbol. By side-effect, update it->what and
6606 it->glyphless_method. This function is called from
6607 get_next_display_element for each character element, and from
6608 x_produce_glyphs when no suitable font was found. */
6609
6610 Lisp_Object
6611 lookup_glyphless_char_display (int c, struct it *it)
6612 {
6613 Lisp_Object glyphless_method = Qnil;
6614
6615 if (CHAR_TABLE_P (Vglyphless_char_display)
6616 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6617 {
6618 if (c >= 0)
6619 {
6620 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6621 if (CONSP (glyphless_method))
6622 glyphless_method = FRAME_WINDOW_P (it->f)
6623 ? XCAR (glyphless_method)
6624 : XCDR (glyphless_method);
6625 }
6626 else
6627 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6628 }
6629
6630 retry:
6631 if (NILP (glyphless_method))
6632 {
6633 if (c >= 0)
6634 /* The default is to display the character by a proper font. */
6635 return Qnil;
6636 /* The default for the no-font case is to display an empty box. */
6637 glyphless_method = Qempty_box;
6638 }
6639 if (EQ (glyphless_method, Qzero_width))
6640 {
6641 if (c >= 0)
6642 return glyphless_method;
6643 /* This method can't be used for the no-font case. */
6644 glyphless_method = Qempty_box;
6645 }
6646 if (EQ (glyphless_method, Qthin_space))
6647 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6648 else if (EQ (glyphless_method, Qempty_box))
6649 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6650 else if (EQ (glyphless_method, Qhex_code))
6651 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6652 else if (STRINGP (glyphless_method))
6653 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6654 else
6655 {
6656 /* Invalid value. We use the default method. */
6657 glyphless_method = Qnil;
6658 goto retry;
6659 }
6660 it->what = IT_GLYPHLESS;
6661 return glyphless_method;
6662 }
6663
6664 /* Merge escape glyph face and cache the result. */
6665
6666 static struct frame *last_escape_glyph_frame = NULL;
6667 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6668 static int last_escape_glyph_merged_face_id = 0;
6669
6670 static int
6671 merge_escape_glyph_face (struct it *it)
6672 {
6673 int face_id;
6674
6675 if (it->f == last_escape_glyph_frame
6676 && it->face_id == last_escape_glyph_face_id)
6677 face_id = last_escape_glyph_merged_face_id;
6678 else
6679 {
6680 /* Merge the `escape-glyph' face into the current face. */
6681 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6682 last_escape_glyph_frame = it->f;
6683 last_escape_glyph_face_id = it->face_id;
6684 last_escape_glyph_merged_face_id = face_id;
6685 }
6686 return face_id;
6687 }
6688
6689 /* Likewise for glyphless glyph face. */
6690
6691 static struct frame *last_glyphless_glyph_frame = NULL;
6692 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6693 static int last_glyphless_glyph_merged_face_id = 0;
6694
6695 int
6696 merge_glyphless_glyph_face (struct it *it)
6697 {
6698 int face_id;
6699
6700 if (it->f == last_glyphless_glyph_frame
6701 && it->face_id == last_glyphless_glyph_face_id)
6702 face_id = last_glyphless_glyph_merged_face_id;
6703 else
6704 {
6705 /* Merge the `glyphless-char' face into the current face. */
6706 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6707 last_glyphless_glyph_frame = it->f;
6708 last_glyphless_glyph_face_id = it->face_id;
6709 last_glyphless_glyph_merged_face_id = face_id;
6710 }
6711 return face_id;
6712 }
6713
6714 /* Load IT's display element fields with information about the next
6715 display element from the current position of IT. Value is zero if
6716 end of buffer (or C string) is reached. */
6717
6718 static int
6719 get_next_display_element (struct it *it)
6720 {
6721 /* Non-zero means that we found a display element. Zero means that
6722 we hit the end of what we iterate over. Performance note: the
6723 function pointer `method' used here turns out to be faster than
6724 using a sequence of if-statements. */
6725 int success_p;
6726
6727 get_next:
6728 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6729
6730 if (it->what == IT_CHARACTER)
6731 {
6732 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6733 and only if (a) the resolved directionality of that character
6734 is R..." */
6735 /* FIXME: Do we need an exception for characters from display
6736 tables? */
6737 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6738 it->c = bidi_mirror_char (it->c);
6739 /* Map via display table or translate control characters.
6740 IT->c, IT->len etc. have been set to the next character by
6741 the function call above. If we have a display table, and it
6742 contains an entry for IT->c, translate it. Don't do this if
6743 IT->c itself comes from a display table, otherwise we could
6744 end up in an infinite recursion. (An alternative could be to
6745 count the recursion depth of this function and signal an
6746 error when a certain maximum depth is reached.) Is it worth
6747 it? */
6748 if (success_p && it->dpvec == NULL)
6749 {
6750 Lisp_Object dv;
6751 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6752 int nonascii_space_p = 0;
6753 int nonascii_hyphen_p = 0;
6754 int c = it->c; /* This is the character to display. */
6755
6756 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6757 {
6758 eassert (SINGLE_BYTE_CHAR_P (c));
6759 if (unibyte_display_via_language_environment)
6760 {
6761 c = DECODE_CHAR (unibyte, c);
6762 if (c < 0)
6763 c = BYTE8_TO_CHAR (it->c);
6764 }
6765 else
6766 c = BYTE8_TO_CHAR (it->c);
6767 }
6768
6769 if (it->dp
6770 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6771 VECTORP (dv)))
6772 {
6773 struct Lisp_Vector *v = XVECTOR (dv);
6774
6775 /* Return the first character from the display table
6776 entry, if not empty. If empty, don't display the
6777 current character. */
6778 if (v->header.size)
6779 {
6780 it->dpvec_char_len = it->len;
6781 it->dpvec = v->contents;
6782 it->dpend = v->contents + v->header.size;
6783 it->current.dpvec_index = 0;
6784 it->dpvec_face_id = -1;
6785 it->saved_face_id = it->face_id;
6786 it->method = GET_FROM_DISPLAY_VECTOR;
6787 it->ellipsis_p = 0;
6788 }
6789 else
6790 {
6791 set_iterator_to_next (it, 0);
6792 }
6793 goto get_next;
6794 }
6795
6796 if (! NILP (lookup_glyphless_char_display (c, it)))
6797 {
6798 if (it->what == IT_GLYPHLESS)
6799 goto done;
6800 /* Don't display this character. */
6801 set_iterator_to_next (it, 0);
6802 goto get_next;
6803 }
6804
6805 /* If `nobreak-char-display' is non-nil, we display
6806 non-ASCII spaces and hyphens specially. */
6807 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6808 {
6809 if (c == 0xA0)
6810 nonascii_space_p = true;
6811 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6812 nonascii_hyphen_p = true;
6813 }
6814
6815 /* Translate control characters into `\003' or `^C' form.
6816 Control characters coming from a display table entry are
6817 currently not translated because we use IT->dpvec to hold
6818 the translation. This could easily be changed but I
6819 don't believe that it is worth doing.
6820
6821 The characters handled by `nobreak-char-display' must be
6822 translated too.
6823
6824 Non-printable characters and raw-byte characters are also
6825 translated to octal form. */
6826 if (((c < ' ' || c == 127) /* ASCII control chars. */
6827 ? (it->area != TEXT_AREA
6828 /* In mode line, treat \n, \t like other crl chars. */
6829 || (c != '\t'
6830 && it->glyph_row
6831 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6832 || (c != '\n' && c != '\t'))
6833 : (nonascii_space_p
6834 || nonascii_hyphen_p
6835 || CHAR_BYTE8_P (c)
6836 || ! CHAR_PRINTABLE_P (c))))
6837 {
6838 /* C is a control character, non-ASCII space/hyphen,
6839 raw-byte, or a non-printable character which must be
6840 displayed either as '\003' or as `^C' where the '\\'
6841 and '^' can be defined in the display table. Fill
6842 IT->ctl_chars with glyphs for what we have to
6843 display. Then, set IT->dpvec to these glyphs. */
6844 Lisp_Object gc;
6845 int ctl_len;
6846 int face_id;
6847 int lface_id = 0;
6848 int escape_glyph;
6849
6850 /* Handle control characters with ^. */
6851
6852 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6853 {
6854 int g;
6855
6856 g = '^'; /* default glyph for Control */
6857 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6858 if (it->dp
6859 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6860 {
6861 g = GLYPH_CODE_CHAR (gc);
6862 lface_id = GLYPH_CODE_FACE (gc);
6863 }
6864
6865 face_id = (lface_id
6866 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6867 : merge_escape_glyph_face (it));
6868
6869 XSETINT (it->ctl_chars[0], g);
6870 XSETINT (it->ctl_chars[1], c ^ 0100);
6871 ctl_len = 2;
6872 goto display_control;
6873 }
6874
6875 /* Handle non-ascii space in the mode where it only gets
6876 highlighting. */
6877
6878 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6879 {
6880 /* Merge `nobreak-space' into the current face. */
6881 face_id = merge_faces (it->f, Qnobreak_space, 0,
6882 it->face_id);
6883 XSETINT (it->ctl_chars[0], ' ');
6884 ctl_len = 1;
6885 goto display_control;
6886 }
6887
6888 /* Handle sequences that start with the "escape glyph". */
6889
6890 /* the default escape glyph is \. */
6891 escape_glyph = '\\';
6892
6893 if (it->dp
6894 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6895 {
6896 escape_glyph = GLYPH_CODE_CHAR (gc);
6897 lface_id = GLYPH_CODE_FACE (gc);
6898 }
6899
6900 face_id = (lface_id
6901 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6902 : merge_escape_glyph_face (it));
6903
6904 /* Draw non-ASCII hyphen with just highlighting: */
6905
6906 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6907 {
6908 XSETINT (it->ctl_chars[0], '-');
6909 ctl_len = 1;
6910 goto display_control;
6911 }
6912
6913 /* Draw non-ASCII space/hyphen with escape glyph: */
6914
6915 if (nonascii_space_p || nonascii_hyphen_p)
6916 {
6917 XSETINT (it->ctl_chars[0], escape_glyph);
6918 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6919 ctl_len = 2;
6920 goto display_control;
6921 }
6922
6923 {
6924 char str[10];
6925 int len, i;
6926
6927 if (CHAR_BYTE8_P (c))
6928 /* Display \200 instead of \17777600. */
6929 c = CHAR_TO_BYTE8 (c);
6930 len = sprintf (str, "%03o", c);
6931
6932 XSETINT (it->ctl_chars[0], escape_glyph);
6933 for (i = 0; i < len; i++)
6934 XSETINT (it->ctl_chars[i + 1], str[i]);
6935 ctl_len = len + 1;
6936 }
6937
6938 display_control:
6939 /* Set up IT->dpvec and return first character from it. */
6940 it->dpvec_char_len = it->len;
6941 it->dpvec = it->ctl_chars;
6942 it->dpend = it->dpvec + ctl_len;
6943 it->current.dpvec_index = 0;
6944 it->dpvec_face_id = face_id;
6945 it->saved_face_id = it->face_id;
6946 it->method = GET_FROM_DISPLAY_VECTOR;
6947 it->ellipsis_p = 0;
6948 goto get_next;
6949 }
6950 it->char_to_display = c;
6951 }
6952 else if (success_p)
6953 {
6954 it->char_to_display = it->c;
6955 }
6956 }
6957
6958 #ifdef HAVE_WINDOW_SYSTEM
6959 /* Adjust face id for a multibyte character. There are no multibyte
6960 character in unibyte text. */
6961 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6962 && it->multibyte_p
6963 && success_p
6964 && FRAME_WINDOW_P (it->f))
6965 {
6966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6967
6968 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6969 {
6970 /* Automatic composition with glyph-string. */
6971 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6972
6973 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6974 }
6975 else
6976 {
6977 ptrdiff_t pos = (it->s ? -1
6978 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6979 : IT_CHARPOS (*it));
6980 int c;
6981
6982 if (it->what == IT_CHARACTER)
6983 c = it->char_to_display;
6984 else
6985 {
6986 struct composition *cmp = composition_table[it->cmp_it.id];
6987 int i;
6988
6989 c = ' ';
6990 for (i = 0; i < cmp->glyph_len; i++)
6991 /* TAB in a composition means display glyphs with
6992 padding space on the left or right. */
6993 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6994 break;
6995 }
6996 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6997 }
6998 }
6999 #endif /* HAVE_WINDOW_SYSTEM */
7000
7001 done:
7002 /* Is this character the last one of a run of characters with
7003 box? If yes, set IT->end_of_box_run_p to 1. */
7004 if (it->face_box_p
7005 && it->s == NULL)
7006 {
7007 if (it->method == GET_FROM_STRING && it->sp)
7008 {
7009 int face_id = underlying_face_id (it);
7010 struct face *face = FACE_FROM_ID (it->f, face_id);
7011
7012 if (face)
7013 {
7014 if (face->box == FACE_NO_BOX)
7015 {
7016 /* If the box comes from face properties in a
7017 display string, check faces in that string. */
7018 int string_face_id = face_after_it_pos (it);
7019 it->end_of_box_run_p
7020 = (FACE_FROM_ID (it->f, string_face_id)->box
7021 == FACE_NO_BOX);
7022 }
7023 /* Otherwise, the box comes from the underlying face.
7024 If this is the last string character displayed, check
7025 the next buffer location. */
7026 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7027 && (it->current.overlay_string_index
7028 == it->n_overlay_strings - 1))
7029 {
7030 ptrdiff_t ignore;
7031 int next_face_id;
7032 struct text_pos pos = it->current.pos;
7033 INC_TEXT_POS (pos, it->multibyte_p);
7034
7035 next_face_id = face_at_buffer_position
7036 (it->w, CHARPOS (pos), &ignore,
7037 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7038 -1);
7039 it->end_of_box_run_p
7040 = (FACE_FROM_ID (it->f, next_face_id)->box
7041 == FACE_NO_BOX);
7042 }
7043 }
7044 }
7045 /* next_element_from_display_vector sets this flag according to
7046 faces of the display vector glyphs, see there. */
7047 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7048 {
7049 int face_id = face_after_it_pos (it);
7050 it->end_of_box_run_p
7051 = (face_id != it->face_id
7052 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7053 }
7054 }
7055 /* If we reached the end of the object we've been iterating (e.g., a
7056 display string or an overlay string), and there's something on
7057 IT->stack, proceed with what's on the stack. It doesn't make
7058 sense to return zero if there's unprocessed stuff on the stack,
7059 because otherwise that stuff will never be displayed. */
7060 if (!success_p && it->sp > 0)
7061 {
7062 set_iterator_to_next (it, 0);
7063 success_p = get_next_display_element (it);
7064 }
7065
7066 /* Value is 0 if end of buffer or string reached. */
7067 return success_p;
7068 }
7069
7070
7071 /* Move IT to the next display element.
7072
7073 RESEAT_P non-zero means if called on a newline in buffer text,
7074 skip to the next visible line start.
7075
7076 Functions get_next_display_element and set_iterator_to_next are
7077 separate because I find this arrangement easier to handle than a
7078 get_next_display_element function that also increments IT's
7079 position. The way it is we can first look at an iterator's current
7080 display element, decide whether it fits on a line, and if it does,
7081 increment the iterator position. The other way around we probably
7082 would either need a flag indicating whether the iterator has to be
7083 incremented the next time, or we would have to implement a
7084 decrement position function which would not be easy to write. */
7085
7086 void
7087 set_iterator_to_next (struct it *it, int reseat_p)
7088 {
7089 /* Reset flags indicating start and end of a sequence of characters
7090 with box. Reset them at the start of this function because
7091 moving the iterator to a new position might set them. */
7092 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7093
7094 switch (it->method)
7095 {
7096 case GET_FROM_BUFFER:
7097 /* The current display element of IT is a character from
7098 current_buffer. Advance in the buffer, and maybe skip over
7099 invisible lines that are so because of selective display. */
7100 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7101 reseat_at_next_visible_line_start (it, 0);
7102 else if (it->cmp_it.id >= 0)
7103 {
7104 /* We are currently getting glyphs from a composition. */
7105 int i;
7106
7107 if (! it->bidi_p)
7108 {
7109 IT_CHARPOS (*it) += it->cmp_it.nchars;
7110 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7111 if (it->cmp_it.to < it->cmp_it.nglyphs)
7112 {
7113 it->cmp_it.from = it->cmp_it.to;
7114 }
7115 else
7116 {
7117 it->cmp_it.id = -1;
7118 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7119 IT_BYTEPOS (*it),
7120 it->end_charpos, Qnil);
7121 }
7122 }
7123 else if (! it->cmp_it.reversed_p)
7124 {
7125 /* Composition created while scanning forward. */
7126 /* Update IT's char/byte positions to point to the first
7127 character of the next grapheme cluster, or to the
7128 character visually after the current composition. */
7129 for (i = 0; i < it->cmp_it.nchars; i++)
7130 bidi_move_to_visually_next (&it->bidi_it);
7131 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7132 IT_CHARPOS (*it) = it->bidi_it.charpos;
7133
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7135 {
7136 /* Proceed to the next grapheme cluster. */
7137 it->cmp_it.from = it->cmp_it.to;
7138 }
7139 else
7140 {
7141 /* No more grapheme clusters in this composition.
7142 Find the next stop position. */
7143 ptrdiff_t stop = it->end_charpos;
7144 if (it->bidi_it.scan_dir < 0)
7145 /* Now we are scanning backward and don't know
7146 where to stop. */
7147 stop = -1;
7148 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7149 IT_BYTEPOS (*it), stop, Qnil);
7150 }
7151 }
7152 else
7153 {
7154 /* Composition created while scanning backward. */
7155 /* Update IT's char/byte positions to point to the last
7156 character of the previous grapheme cluster, or the
7157 character visually after the current composition. */
7158 for (i = 0; i < it->cmp_it.nchars; i++)
7159 bidi_move_to_visually_next (&it->bidi_it);
7160 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7161 IT_CHARPOS (*it) = it->bidi_it.charpos;
7162 if (it->cmp_it.from > 0)
7163 {
7164 /* Proceed to the previous grapheme cluster. */
7165 it->cmp_it.to = it->cmp_it.from;
7166 }
7167 else
7168 {
7169 /* No more grapheme clusters in this composition.
7170 Find the next stop position. */
7171 ptrdiff_t stop = it->end_charpos;
7172 if (it->bidi_it.scan_dir < 0)
7173 /* Now we are scanning backward and don't know
7174 where to stop. */
7175 stop = -1;
7176 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7177 IT_BYTEPOS (*it), stop, Qnil);
7178 }
7179 }
7180 }
7181 else
7182 {
7183 eassert (it->len != 0);
7184
7185 if (!it->bidi_p)
7186 {
7187 IT_BYTEPOS (*it) += it->len;
7188 IT_CHARPOS (*it) += 1;
7189 }
7190 else
7191 {
7192 int prev_scan_dir = it->bidi_it.scan_dir;
7193 /* If this is a new paragraph, determine its base
7194 direction (a.k.a. its base embedding level). */
7195 if (it->bidi_it.new_paragraph)
7196 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7197 bidi_move_to_visually_next (&it->bidi_it);
7198 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7199 IT_CHARPOS (*it) = it->bidi_it.charpos;
7200 if (prev_scan_dir != it->bidi_it.scan_dir)
7201 {
7202 /* As the scan direction was changed, we must
7203 re-compute the stop position for composition. */
7204 ptrdiff_t stop = it->end_charpos;
7205 if (it->bidi_it.scan_dir < 0)
7206 stop = -1;
7207 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7208 IT_BYTEPOS (*it), stop, Qnil);
7209 }
7210 }
7211 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7212 }
7213 break;
7214
7215 case GET_FROM_C_STRING:
7216 /* Current display element of IT is from a C string. */
7217 if (!it->bidi_p
7218 /* If the string position is beyond string's end, it means
7219 next_element_from_c_string is padding the string with
7220 blanks, in which case we bypass the bidi iterator,
7221 because it cannot deal with such virtual characters. */
7222 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7223 {
7224 IT_BYTEPOS (*it) += it->len;
7225 IT_CHARPOS (*it) += 1;
7226 }
7227 else
7228 {
7229 bidi_move_to_visually_next (&it->bidi_it);
7230 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7231 IT_CHARPOS (*it) = it->bidi_it.charpos;
7232 }
7233 break;
7234
7235 case GET_FROM_DISPLAY_VECTOR:
7236 /* Current display element of IT is from a display table entry.
7237 Advance in the display table definition. Reset it to null if
7238 end reached, and continue with characters from buffers/
7239 strings. */
7240 ++it->current.dpvec_index;
7241
7242 /* Restore face of the iterator to what they were before the
7243 display vector entry (these entries may contain faces). */
7244 it->face_id = it->saved_face_id;
7245
7246 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7247 {
7248 int recheck_faces = it->ellipsis_p;
7249
7250 if (it->s)
7251 it->method = GET_FROM_C_STRING;
7252 else if (STRINGP (it->string))
7253 it->method = GET_FROM_STRING;
7254 else
7255 {
7256 it->method = GET_FROM_BUFFER;
7257 it->object = it->w->contents;
7258 }
7259
7260 it->dpvec = NULL;
7261 it->current.dpvec_index = -1;
7262
7263 /* Skip over characters which were displayed via IT->dpvec. */
7264 if (it->dpvec_char_len < 0)
7265 reseat_at_next_visible_line_start (it, 1);
7266 else if (it->dpvec_char_len > 0)
7267 {
7268 if (it->method == GET_FROM_STRING
7269 && it->current.overlay_string_index >= 0
7270 && it->n_overlay_strings > 0)
7271 it->ignore_overlay_strings_at_pos_p = true;
7272 it->len = it->dpvec_char_len;
7273 set_iterator_to_next (it, reseat_p);
7274 }
7275
7276 /* Maybe recheck faces after display vector. */
7277 if (recheck_faces)
7278 it->stop_charpos = IT_CHARPOS (*it);
7279 }
7280 break;
7281
7282 case GET_FROM_STRING:
7283 /* Current display element is a character from a Lisp string. */
7284 eassert (it->s == NULL && STRINGP (it->string));
7285 /* Don't advance past string end. These conditions are true
7286 when set_iterator_to_next is called at the end of
7287 get_next_display_element, in which case the Lisp string is
7288 already exhausted, and all we want is pop the iterator
7289 stack. */
7290 if (it->current.overlay_string_index >= 0)
7291 {
7292 /* This is an overlay string, so there's no padding with
7293 spaces, and the number of characters in the string is
7294 where the string ends. */
7295 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7296 goto consider_string_end;
7297 }
7298 else
7299 {
7300 /* Not an overlay string. There could be padding, so test
7301 against it->end_charpos. */
7302 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7303 goto consider_string_end;
7304 }
7305 if (it->cmp_it.id >= 0)
7306 {
7307 int i;
7308
7309 if (! it->bidi_p)
7310 {
7311 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7312 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7313 if (it->cmp_it.to < it->cmp_it.nglyphs)
7314 it->cmp_it.from = it->cmp_it.to;
7315 else
7316 {
7317 it->cmp_it.id = -1;
7318 composition_compute_stop_pos (&it->cmp_it,
7319 IT_STRING_CHARPOS (*it),
7320 IT_STRING_BYTEPOS (*it),
7321 it->end_charpos, it->string);
7322 }
7323 }
7324 else if (! it->cmp_it.reversed_p)
7325 {
7326 for (i = 0; i < it->cmp_it.nchars; i++)
7327 bidi_move_to_visually_next (&it->bidi_it);
7328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7330
7331 if (it->cmp_it.to < it->cmp_it.nglyphs)
7332 it->cmp_it.from = it->cmp_it.to;
7333 else
7334 {
7335 ptrdiff_t stop = it->end_charpos;
7336 if (it->bidi_it.scan_dir < 0)
7337 stop = -1;
7338 composition_compute_stop_pos (&it->cmp_it,
7339 IT_STRING_CHARPOS (*it),
7340 IT_STRING_BYTEPOS (*it), stop,
7341 it->string);
7342 }
7343 }
7344 else
7345 {
7346 for (i = 0; i < it->cmp_it.nchars; i++)
7347 bidi_move_to_visually_next (&it->bidi_it);
7348 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7349 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (it->cmp_it.from > 0)
7351 it->cmp_it.to = it->cmp_it.from;
7352 else
7353 {
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it,
7358 IT_STRING_CHARPOS (*it),
7359 IT_STRING_BYTEPOS (*it), stop,
7360 it->string);
7361 }
7362 }
7363 }
7364 else
7365 {
7366 if (!it->bidi_p
7367 /* If the string position is beyond string's end, it
7368 means next_element_from_string is padding the string
7369 with blanks, in which case we bypass the bidi
7370 iterator, because it cannot deal with such virtual
7371 characters. */
7372 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7373 {
7374 IT_STRING_BYTEPOS (*it) += it->len;
7375 IT_STRING_CHARPOS (*it) += 1;
7376 }
7377 else
7378 {
7379 int prev_scan_dir = it->bidi_it.scan_dir;
7380
7381 bidi_move_to_visually_next (&it->bidi_it);
7382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7383 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7384 if (prev_scan_dir != it->bidi_it.scan_dir)
7385 {
7386 ptrdiff_t stop = it->end_charpos;
7387
7388 if (it->bidi_it.scan_dir < 0)
7389 stop = -1;
7390 composition_compute_stop_pos (&it->cmp_it,
7391 IT_STRING_CHARPOS (*it),
7392 IT_STRING_BYTEPOS (*it), stop,
7393 it->string);
7394 }
7395 }
7396 }
7397
7398 consider_string_end:
7399
7400 if (it->current.overlay_string_index >= 0)
7401 {
7402 /* IT->string is an overlay string. Advance to the
7403 next, if there is one. */
7404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7405 {
7406 it->ellipsis_p = 0;
7407 next_overlay_string (it);
7408 if (it->ellipsis_p)
7409 setup_for_ellipsis (it, 0);
7410 }
7411 }
7412 else
7413 {
7414 /* IT->string is not an overlay string. If we reached
7415 its end, and there is something on IT->stack, proceed
7416 with what is on the stack. This can be either another
7417 string, this time an overlay string, or a buffer. */
7418 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7419 && it->sp > 0)
7420 {
7421 pop_it (it);
7422 if (it->method == GET_FROM_STRING)
7423 goto consider_string_end;
7424 }
7425 }
7426 break;
7427
7428 case GET_FROM_IMAGE:
7429 case GET_FROM_STRETCH:
7430 /* The position etc with which we have to proceed are on
7431 the stack. The position may be at the end of a string,
7432 if the `display' property takes up the whole string. */
7433 eassert (it->sp > 0);
7434 pop_it (it);
7435 if (it->method == GET_FROM_STRING)
7436 goto consider_string_end;
7437 break;
7438
7439 default:
7440 /* There are no other methods defined, so this should be a bug. */
7441 emacs_abort ();
7442 }
7443
7444 eassert (it->method != GET_FROM_STRING
7445 || (STRINGP (it->string)
7446 && IT_STRING_CHARPOS (*it) >= 0));
7447 }
7448
7449 /* Load IT's display element fields with information about the next
7450 display element which comes from a display table entry or from the
7451 result of translating a control character to one of the forms `^C'
7452 or `\003'.
7453
7454 IT->dpvec holds the glyphs to return as characters.
7455 IT->saved_face_id holds the face id before the display vector--it
7456 is restored into IT->face_id in set_iterator_to_next. */
7457
7458 static int
7459 next_element_from_display_vector (struct it *it)
7460 {
7461 Lisp_Object gc;
7462 int prev_face_id = it->face_id;
7463 int next_face_id;
7464
7465 /* Precondition. */
7466 eassert (it->dpvec && it->current.dpvec_index >= 0);
7467
7468 it->face_id = it->saved_face_id;
7469
7470 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7471 That seemed totally bogus - so I changed it... */
7472 gc = it->dpvec[it->current.dpvec_index];
7473
7474 if (GLYPH_CODE_P (gc))
7475 {
7476 struct face *this_face, *prev_face, *next_face;
7477
7478 it->c = GLYPH_CODE_CHAR (gc);
7479 it->len = CHAR_BYTES (it->c);
7480
7481 /* The entry may contain a face id to use. Such a face id is
7482 the id of a Lisp face, not a realized face. A face id of
7483 zero means no face is specified. */
7484 if (it->dpvec_face_id >= 0)
7485 it->face_id = it->dpvec_face_id;
7486 else
7487 {
7488 int lface_id = GLYPH_CODE_FACE (gc);
7489 if (lface_id > 0)
7490 it->face_id = merge_faces (it->f, Qt, lface_id,
7491 it->saved_face_id);
7492 }
7493
7494 /* Glyphs in the display vector could have the box face, so we
7495 need to set the related flags in the iterator, as
7496 appropriate. */
7497 this_face = FACE_FROM_ID (it->f, it->face_id);
7498 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7499
7500 /* Is this character the first character of a box-face run? */
7501 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7502 && (!prev_face
7503 || prev_face->box == FACE_NO_BOX));
7504
7505 /* For the last character of the box-face run, we need to look
7506 either at the next glyph from the display vector, or at the
7507 face we saw before the display vector. */
7508 next_face_id = it->saved_face_id;
7509 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7510 {
7511 if (it->dpvec_face_id >= 0)
7512 next_face_id = it->dpvec_face_id;
7513 else
7514 {
7515 int lface_id =
7516 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7517
7518 if (lface_id > 0)
7519 next_face_id = merge_faces (it->f, Qt, lface_id,
7520 it->saved_face_id);
7521 }
7522 }
7523 next_face = FACE_FROM_ID (it->f, next_face_id);
7524 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7525 && (!next_face
7526 || next_face->box == FACE_NO_BOX));
7527 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7528 }
7529 else
7530 /* Display table entry is invalid. Return a space. */
7531 it->c = ' ', it->len = 1;
7532
7533 /* Don't change position and object of the iterator here. They are
7534 still the values of the character that had this display table
7535 entry or was translated, and that's what we want. */
7536 it->what = IT_CHARACTER;
7537 return 1;
7538 }
7539
7540 /* Get the first element of string/buffer in the visual order, after
7541 being reseated to a new position in a string or a buffer. */
7542 static void
7543 get_visually_first_element (struct it *it)
7544 {
7545 int string_p = STRINGP (it->string) || it->s;
7546 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7547 ptrdiff_t bob = (string_p ? 0 : BEGV);
7548
7549 if (STRINGP (it->string))
7550 {
7551 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7552 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7553 }
7554 else
7555 {
7556 it->bidi_it.charpos = IT_CHARPOS (*it);
7557 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7558 }
7559
7560 if (it->bidi_it.charpos == eob)
7561 {
7562 /* Nothing to do, but reset the FIRST_ELT flag, like
7563 bidi_paragraph_init does, because we are not going to
7564 call it. */
7565 it->bidi_it.first_elt = 0;
7566 }
7567 else if (it->bidi_it.charpos == bob
7568 || (!string_p
7569 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7570 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7571 {
7572 /* If we are at the beginning of a line/string, we can produce
7573 the next element right away. */
7574 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7575 bidi_move_to_visually_next (&it->bidi_it);
7576 }
7577 else
7578 {
7579 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7580
7581 /* We need to prime the bidi iterator starting at the line's or
7582 string's beginning, before we will be able to produce the
7583 next element. */
7584 if (string_p)
7585 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7586 else
7587 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7588 IT_BYTEPOS (*it), -1,
7589 &it->bidi_it.bytepos);
7590 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7591 do
7592 {
7593 /* Now return to buffer/string position where we were asked
7594 to get the next display element, and produce that. */
7595 bidi_move_to_visually_next (&it->bidi_it);
7596 }
7597 while (it->bidi_it.bytepos != orig_bytepos
7598 && it->bidi_it.charpos < eob);
7599 }
7600
7601 /* Adjust IT's position information to where we ended up. */
7602 if (STRINGP (it->string))
7603 {
7604 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7605 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7606 }
7607 else
7608 {
7609 IT_CHARPOS (*it) = it->bidi_it.charpos;
7610 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7611 }
7612
7613 if (STRINGP (it->string) || !it->s)
7614 {
7615 ptrdiff_t stop, charpos, bytepos;
7616
7617 if (STRINGP (it->string))
7618 {
7619 eassert (!it->s);
7620 stop = SCHARS (it->string);
7621 if (stop > it->end_charpos)
7622 stop = it->end_charpos;
7623 charpos = IT_STRING_CHARPOS (*it);
7624 bytepos = IT_STRING_BYTEPOS (*it);
7625 }
7626 else
7627 {
7628 stop = it->end_charpos;
7629 charpos = IT_CHARPOS (*it);
7630 bytepos = IT_BYTEPOS (*it);
7631 }
7632 if (it->bidi_it.scan_dir < 0)
7633 stop = -1;
7634 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7635 it->string);
7636 }
7637 }
7638
7639 /* Load IT with the next display element from Lisp string IT->string.
7640 IT->current.string_pos is the current position within the string.
7641 If IT->current.overlay_string_index >= 0, the Lisp string is an
7642 overlay string. */
7643
7644 static int
7645 next_element_from_string (struct it *it)
7646 {
7647 struct text_pos position;
7648
7649 eassert (STRINGP (it->string));
7650 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7651 eassert (IT_STRING_CHARPOS (*it) >= 0);
7652 position = it->current.string_pos;
7653
7654 /* With bidi reordering, the character to display might not be the
7655 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7656 that we were reseat()ed to a new string, whose paragraph
7657 direction is not known. */
7658 if (it->bidi_p && it->bidi_it.first_elt)
7659 {
7660 get_visually_first_element (it);
7661 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7662 }
7663
7664 /* Time to check for invisible text? */
7665 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7666 {
7667 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7668 {
7669 if (!(!it->bidi_p
7670 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7671 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7672 {
7673 /* With bidi non-linear iteration, we could find
7674 ourselves far beyond the last computed stop_charpos,
7675 with several other stop positions in between that we
7676 missed. Scan them all now, in buffer's logical
7677 order, until we find and handle the last stop_charpos
7678 that precedes our current position. */
7679 handle_stop_backwards (it, it->stop_charpos);
7680 return GET_NEXT_DISPLAY_ELEMENT (it);
7681 }
7682 else
7683 {
7684 if (it->bidi_p)
7685 {
7686 /* Take note of the stop position we just moved
7687 across, for when we will move back across it. */
7688 it->prev_stop = it->stop_charpos;
7689 /* If we are at base paragraph embedding level, take
7690 note of the last stop position seen at this
7691 level. */
7692 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7693 it->base_level_stop = it->stop_charpos;
7694 }
7695 handle_stop (it);
7696
7697 /* Since a handler may have changed IT->method, we must
7698 recurse here. */
7699 return GET_NEXT_DISPLAY_ELEMENT (it);
7700 }
7701 }
7702 else if (it->bidi_p
7703 /* If we are before prev_stop, we may have overstepped
7704 on our way backwards a stop_pos, and if so, we need
7705 to handle that stop_pos. */
7706 && IT_STRING_CHARPOS (*it) < it->prev_stop
7707 /* We can sometimes back up for reasons that have nothing
7708 to do with bidi reordering. E.g., compositions. The
7709 code below is only needed when we are above the base
7710 embedding level, so test for that explicitly. */
7711 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7712 {
7713 /* If we lost track of base_level_stop, we have no better
7714 place for handle_stop_backwards to start from than string
7715 beginning. This happens, e.g., when we were reseated to
7716 the previous screenful of text by vertical-motion. */
7717 if (it->base_level_stop <= 0
7718 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7719 it->base_level_stop = 0;
7720 handle_stop_backwards (it, it->base_level_stop);
7721 return GET_NEXT_DISPLAY_ELEMENT (it);
7722 }
7723 }
7724
7725 if (it->current.overlay_string_index >= 0)
7726 {
7727 /* Get the next character from an overlay string. In overlay
7728 strings, there is no field width or padding with spaces to
7729 do. */
7730 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7731 {
7732 it->what = IT_EOB;
7733 return 0;
7734 }
7735 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7736 IT_STRING_BYTEPOS (*it),
7737 it->bidi_it.scan_dir < 0
7738 ? -1
7739 : SCHARS (it->string))
7740 && next_element_from_composition (it))
7741 {
7742 return 1;
7743 }
7744 else if (STRING_MULTIBYTE (it->string))
7745 {
7746 const unsigned char *s = (SDATA (it->string)
7747 + IT_STRING_BYTEPOS (*it));
7748 it->c = string_char_and_length (s, &it->len);
7749 }
7750 else
7751 {
7752 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7753 it->len = 1;
7754 }
7755 }
7756 else
7757 {
7758 /* Get the next character from a Lisp string that is not an
7759 overlay string. Such strings come from the mode line, for
7760 example. We may have to pad with spaces, or truncate the
7761 string. See also next_element_from_c_string. */
7762 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7763 {
7764 it->what = IT_EOB;
7765 return 0;
7766 }
7767 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7768 {
7769 /* Pad with spaces. */
7770 it->c = ' ', it->len = 1;
7771 CHARPOS (position) = BYTEPOS (position) = -1;
7772 }
7773 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7774 IT_STRING_BYTEPOS (*it),
7775 it->bidi_it.scan_dir < 0
7776 ? -1
7777 : it->string_nchars)
7778 && next_element_from_composition (it))
7779 {
7780 return 1;
7781 }
7782 else if (STRING_MULTIBYTE (it->string))
7783 {
7784 const unsigned char *s = (SDATA (it->string)
7785 + IT_STRING_BYTEPOS (*it));
7786 it->c = string_char_and_length (s, &it->len);
7787 }
7788 else
7789 {
7790 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7791 it->len = 1;
7792 }
7793 }
7794
7795 /* Record what we have and where it came from. */
7796 it->what = IT_CHARACTER;
7797 it->object = it->string;
7798 it->position = position;
7799 return 1;
7800 }
7801
7802
7803 /* Load IT with next display element from C string IT->s.
7804 IT->string_nchars is the maximum number of characters to return
7805 from the string. IT->end_charpos may be greater than
7806 IT->string_nchars when this function is called, in which case we
7807 may have to return padding spaces. Value is zero if end of string
7808 reached, including padding spaces. */
7809
7810 static int
7811 next_element_from_c_string (struct it *it)
7812 {
7813 bool success_p = true;
7814
7815 eassert (it->s);
7816 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7817 it->what = IT_CHARACTER;
7818 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7819 it->object = Qnil;
7820
7821 /* With bidi reordering, the character to display might not be the
7822 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7823 we were reseated to a new string, whose paragraph direction is
7824 not known. */
7825 if (it->bidi_p && it->bidi_it.first_elt)
7826 get_visually_first_element (it);
7827
7828 /* IT's position can be greater than IT->string_nchars in case a
7829 field width or precision has been specified when the iterator was
7830 initialized. */
7831 if (IT_CHARPOS (*it) >= it->end_charpos)
7832 {
7833 /* End of the game. */
7834 it->what = IT_EOB;
7835 success_p = 0;
7836 }
7837 else if (IT_CHARPOS (*it) >= it->string_nchars)
7838 {
7839 /* Pad with spaces. */
7840 it->c = ' ', it->len = 1;
7841 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7842 }
7843 else if (it->multibyte_p)
7844 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7845 else
7846 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7847
7848 return success_p;
7849 }
7850
7851
7852 /* Set up IT to return characters from an ellipsis, if appropriate.
7853 The definition of the ellipsis glyphs may come from a display table
7854 entry. This function fills IT with the first glyph from the
7855 ellipsis if an ellipsis is to be displayed. */
7856
7857 static int
7858 next_element_from_ellipsis (struct it *it)
7859 {
7860 if (it->selective_display_ellipsis_p)
7861 setup_for_ellipsis (it, it->len);
7862 else
7863 {
7864 /* The face at the current position may be different from the
7865 face we find after the invisible text. Remember what it
7866 was in IT->saved_face_id, and signal that it's there by
7867 setting face_before_selective_p. */
7868 it->saved_face_id = it->face_id;
7869 it->method = GET_FROM_BUFFER;
7870 it->object = it->w->contents;
7871 reseat_at_next_visible_line_start (it, 1);
7872 it->face_before_selective_p = true;
7873 }
7874
7875 return GET_NEXT_DISPLAY_ELEMENT (it);
7876 }
7877
7878
7879 /* Deliver an image display element. The iterator IT is already
7880 filled with image information (done in handle_display_prop). Value
7881 is always 1. */
7882
7883
7884 static int
7885 next_element_from_image (struct it *it)
7886 {
7887 it->what = IT_IMAGE;
7888 it->ignore_overlay_strings_at_pos_p = 0;
7889 return 1;
7890 }
7891
7892
7893 /* Fill iterator IT with next display element from a stretch glyph
7894 property. IT->object is the value of the text property. Value is
7895 always 1. */
7896
7897 static int
7898 next_element_from_stretch (struct it *it)
7899 {
7900 it->what = IT_STRETCH;
7901 return 1;
7902 }
7903
7904 /* Scan backwards from IT's current position until we find a stop
7905 position, or until BEGV. This is called when we find ourself
7906 before both the last known prev_stop and base_level_stop while
7907 reordering bidirectional text. */
7908
7909 static void
7910 compute_stop_pos_backwards (struct it *it)
7911 {
7912 const int SCAN_BACK_LIMIT = 1000;
7913 struct text_pos pos;
7914 struct display_pos save_current = it->current;
7915 struct text_pos save_position = it->position;
7916 ptrdiff_t charpos = IT_CHARPOS (*it);
7917 ptrdiff_t where_we_are = charpos;
7918 ptrdiff_t save_stop_pos = it->stop_charpos;
7919 ptrdiff_t save_end_pos = it->end_charpos;
7920
7921 eassert (NILP (it->string) && !it->s);
7922 eassert (it->bidi_p);
7923 it->bidi_p = 0;
7924 do
7925 {
7926 it->end_charpos = min (charpos + 1, ZV);
7927 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7928 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7929 reseat_1 (it, pos, 0);
7930 compute_stop_pos (it);
7931 /* We must advance forward, right? */
7932 if (it->stop_charpos <= charpos)
7933 emacs_abort ();
7934 }
7935 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7936
7937 if (it->stop_charpos <= where_we_are)
7938 it->prev_stop = it->stop_charpos;
7939 else
7940 it->prev_stop = BEGV;
7941 it->bidi_p = true;
7942 it->current = save_current;
7943 it->position = save_position;
7944 it->stop_charpos = save_stop_pos;
7945 it->end_charpos = save_end_pos;
7946 }
7947
7948 /* Scan forward from CHARPOS in the current buffer/string, until we
7949 find a stop position > current IT's position. Then handle the stop
7950 position before that. This is called when we bump into a stop
7951 position while reordering bidirectional text. CHARPOS should be
7952 the last previously processed stop_pos (or BEGV/0, if none were
7953 processed yet) whose position is less that IT's current
7954 position. */
7955
7956 static void
7957 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7958 {
7959 int bufp = !STRINGP (it->string);
7960 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7961 struct display_pos save_current = it->current;
7962 struct text_pos save_position = it->position;
7963 struct text_pos pos1;
7964 ptrdiff_t next_stop;
7965
7966 /* Scan in strict logical order. */
7967 eassert (it->bidi_p);
7968 it->bidi_p = 0;
7969 do
7970 {
7971 it->prev_stop = charpos;
7972 if (bufp)
7973 {
7974 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7975 reseat_1 (it, pos1, 0);
7976 }
7977 else
7978 it->current.string_pos = string_pos (charpos, it->string);
7979 compute_stop_pos (it);
7980 /* We must advance forward, right? */
7981 if (it->stop_charpos <= it->prev_stop)
7982 emacs_abort ();
7983 charpos = it->stop_charpos;
7984 }
7985 while (charpos <= where_we_are);
7986
7987 it->bidi_p = true;
7988 it->current = save_current;
7989 it->position = save_position;
7990 next_stop = it->stop_charpos;
7991 it->stop_charpos = it->prev_stop;
7992 handle_stop (it);
7993 it->stop_charpos = next_stop;
7994 }
7995
7996 /* Load IT with the next display element from current_buffer. Value
7997 is zero if end of buffer reached. IT->stop_charpos is the next
7998 position at which to stop and check for text properties or buffer
7999 end. */
8000
8001 static int
8002 next_element_from_buffer (struct it *it)
8003 {
8004 bool success_p = true;
8005
8006 eassert (IT_CHARPOS (*it) >= BEGV);
8007 eassert (NILP (it->string) && !it->s);
8008 eassert (!it->bidi_p
8009 || (EQ (it->bidi_it.string.lstring, Qnil)
8010 && it->bidi_it.string.s == NULL));
8011
8012 /* With bidi reordering, the character to display might not be the
8013 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8014 we were reseat()ed to a new buffer position, which is potentially
8015 a different paragraph. */
8016 if (it->bidi_p && it->bidi_it.first_elt)
8017 {
8018 get_visually_first_element (it);
8019 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8020 }
8021
8022 if (IT_CHARPOS (*it) >= it->stop_charpos)
8023 {
8024 if (IT_CHARPOS (*it) >= it->end_charpos)
8025 {
8026 int overlay_strings_follow_p;
8027
8028 /* End of the game, except when overlay strings follow that
8029 haven't been returned yet. */
8030 if (it->overlay_strings_at_end_processed_p)
8031 overlay_strings_follow_p = 0;
8032 else
8033 {
8034 it->overlay_strings_at_end_processed_p = true;
8035 overlay_strings_follow_p = get_overlay_strings (it, 0);
8036 }
8037
8038 if (overlay_strings_follow_p)
8039 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8040 else
8041 {
8042 it->what = IT_EOB;
8043 it->position = it->current.pos;
8044 success_p = 0;
8045 }
8046 }
8047 else if (!(!it->bidi_p
8048 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8049 || IT_CHARPOS (*it) == it->stop_charpos))
8050 {
8051 /* With bidi non-linear iteration, we could find ourselves
8052 far beyond the last computed stop_charpos, with several
8053 other stop positions in between that we missed. Scan
8054 them all now, in buffer's logical order, until we find
8055 and handle the last stop_charpos that precedes our
8056 current position. */
8057 handle_stop_backwards (it, it->stop_charpos);
8058 return GET_NEXT_DISPLAY_ELEMENT (it);
8059 }
8060 else
8061 {
8062 if (it->bidi_p)
8063 {
8064 /* Take note of the stop position we just moved across,
8065 for when we will move back across it. */
8066 it->prev_stop = it->stop_charpos;
8067 /* If we are at base paragraph embedding level, take
8068 note of the last stop position seen at this
8069 level. */
8070 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8071 it->base_level_stop = it->stop_charpos;
8072 }
8073 handle_stop (it);
8074 return GET_NEXT_DISPLAY_ELEMENT (it);
8075 }
8076 }
8077 else if (it->bidi_p
8078 /* If we are before prev_stop, we may have overstepped on
8079 our way backwards a stop_pos, and if so, we need to
8080 handle that stop_pos. */
8081 && IT_CHARPOS (*it) < it->prev_stop
8082 /* We can sometimes back up for reasons that have nothing
8083 to do with bidi reordering. E.g., compositions. The
8084 code below is only needed when we are above the base
8085 embedding level, so test for that explicitly. */
8086 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8087 {
8088 if (it->base_level_stop <= 0
8089 || IT_CHARPOS (*it) < it->base_level_stop)
8090 {
8091 /* If we lost track of base_level_stop, we need to find
8092 prev_stop by looking backwards. This happens, e.g., when
8093 we were reseated to the previous screenful of text by
8094 vertical-motion. */
8095 it->base_level_stop = BEGV;
8096 compute_stop_pos_backwards (it);
8097 handle_stop_backwards (it, it->prev_stop);
8098 }
8099 else
8100 handle_stop_backwards (it, it->base_level_stop);
8101 return GET_NEXT_DISPLAY_ELEMENT (it);
8102 }
8103 else
8104 {
8105 /* No face changes, overlays etc. in sight, so just return a
8106 character from current_buffer. */
8107 unsigned char *p;
8108 ptrdiff_t stop;
8109
8110 /* Maybe run the redisplay end trigger hook. Performance note:
8111 This doesn't seem to cost measurable time. */
8112 if (it->redisplay_end_trigger_charpos
8113 && it->glyph_row
8114 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8115 run_redisplay_end_trigger_hook (it);
8116
8117 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8118 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8119 stop)
8120 && next_element_from_composition (it))
8121 {
8122 return 1;
8123 }
8124
8125 /* Get the next character, maybe multibyte. */
8126 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8127 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8128 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8129 else
8130 it->c = *p, it->len = 1;
8131
8132 /* Record what we have and where it came from. */
8133 it->what = IT_CHARACTER;
8134 it->object = it->w->contents;
8135 it->position = it->current.pos;
8136
8137 /* Normally we return the character found above, except when we
8138 really want to return an ellipsis for selective display. */
8139 if (it->selective)
8140 {
8141 if (it->c == '\n')
8142 {
8143 /* A value of selective > 0 means hide lines indented more
8144 than that number of columns. */
8145 if (it->selective > 0
8146 && IT_CHARPOS (*it) + 1 < ZV
8147 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8148 IT_BYTEPOS (*it) + 1,
8149 it->selective))
8150 {
8151 success_p = next_element_from_ellipsis (it);
8152 it->dpvec_char_len = -1;
8153 }
8154 }
8155 else if (it->c == '\r' && it->selective == -1)
8156 {
8157 /* A value of selective == -1 means that everything from the
8158 CR to the end of the line is invisible, with maybe an
8159 ellipsis displayed for it. */
8160 success_p = next_element_from_ellipsis (it);
8161 it->dpvec_char_len = -1;
8162 }
8163 }
8164 }
8165
8166 /* Value is zero if end of buffer reached. */
8167 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8168 return success_p;
8169 }
8170
8171
8172 /* Run the redisplay end trigger hook for IT. */
8173
8174 static void
8175 run_redisplay_end_trigger_hook (struct it *it)
8176 {
8177 Lisp_Object args[3];
8178
8179 /* IT->glyph_row should be non-null, i.e. we should be actually
8180 displaying something, or otherwise we should not run the hook. */
8181 eassert (it->glyph_row);
8182
8183 /* Set up hook arguments. */
8184 args[0] = Qredisplay_end_trigger_functions;
8185 args[1] = it->window;
8186 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8187 it->redisplay_end_trigger_charpos = 0;
8188
8189 /* Since we are *trying* to run these functions, don't try to run
8190 them again, even if they get an error. */
8191 wset_redisplay_end_trigger (it->w, Qnil);
8192 Frun_hook_with_args (3, args);
8193
8194 /* Notice if it changed the face of the character we are on. */
8195 handle_face_prop (it);
8196 }
8197
8198
8199 /* Deliver a composition display element. Unlike the other
8200 next_element_from_XXX, this function is not registered in the array
8201 get_next_element[]. It is called from next_element_from_buffer and
8202 next_element_from_string when necessary. */
8203
8204 static int
8205 next_element_from_composition (struct it *it)
8206 {
8207 it->what = IT_COMPOSITION;
8208 it->len = it->cmp_it.nbytes;
8209 if (STRINGP (it->string))
8210 {
8211 if (it->c < 0)
8212 {
8213 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8214 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8215 return 0;
8216 }
8217 it->position = it->current.string_pos;
8218 it->object = it->string;
8219 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8220 IT_STRING_BYTEPOS (*it), it->string);
8221 }
8222 else
8223 {
8224 if (it->c < 0)
8225 {
8226 IT_CHARPOS (*it) += it->cmp_it.nchars;
8227 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8228 if (it->bidi_p)
8229 {
8230 if (it->bidi_it.new_paragraph)
8231 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8232 /* Resync the bidi iterator with IT's new position.
8233 FIXME: this doesn't support bidirectional text. */
8234 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8235 bidi_move_to_visually_next (&it->bidi_it);
8236 }
8237 return 0;
8238 }
8239 it->position = it->current.pos;
8240 it->object = it->w->contents;
8241 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8242 IT_BYTEPOS (*it), Qnil);
8243 }
8244 return 1;
8245 }
8246
8247
8248 \f
8249 /***********************************************************************
8250 Moving an iterator without producing glyphs
8251 ***********************************************************************/
8252
8253 /* Check if iterator is at a position corresponding to a valid buffer
8254 position after some move_it_ call. */
8255
8256 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8257 ((it)->method == GET_FROM_STRING \
8258 ? IT_STRING_CHARPOS (*it) == 0 \
8259 : 1)
8260
8261
8262 /* Move iterator IT to a specified buffer or X position within one
8263 line on the display without producing glyphs.
8264
8265 OP should be a bit mask including some or all of these bits:
8266 MOVE_TO_X: Stop upon reaching x-position TO_X.
8267 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8268 Regardless of OP's value, stop upon reaching the end of the display line.
8269
8270 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8271 This means, in particular, that TO_X includes window's horizontal
8272 scroll amount.
8273
8274 The return value has several possible values that
8275 say what condition caused the scan to stop:
8276
8277 MOVE_POS_MATCH_OR_ZV
8278 - when TO_POS or ZV was reached.
8279
8280 MOVE_X_REACHED
8281 -when TO_X was reached before TO_POS or ZV were reached.
8282
8283 MOVE_LINE_CONTINUED
8284 - when we reached the end of the display area and the line must
8285 be continued.
8286
8287 MOVE_LINE_TRUNCATED
8288 - when we reached the end of the display area and the line is
8289 truncated.
8290
8291 MOVE_NEWLINE_OR_CR
8292 - when we stopped at a line end, i.e. a newline or a CR and selective
8293 display is on. */
8294
8295 static enum move_it_result
8296 move_it_in_display_line_to (struct it *it,
8297 ptrdiff_t to_charpos, int to_x,
8298 enum move_operation_enum op)
8299 {
8300 enum move_it_result result = MOVE_UNDEFINED;
8301 struct glyph_row *saved_glyph_row;
8302 struct it wrap_it, atpos_it, atx_it, ppos_it;
8303 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8304 void *ppos_data = NULL;
8305 int may_wrap = 0;
8306 enum it_method prev_method = it->method;
8307 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8308 int saw_smaller_pos = prev_pos < to_charpos;
8309
8310 /* Don't produce glyphs in produce_glyphs. */
8311 saved_glyph_row = it->glyph_row;
8312 it->glyph_row = NULL;
8313
8314 /* Use wrap_it to save a copy of IT wherever a word wrap could
8315 occur. Use atpos_it to save a copy of IT at the desired buffer
8316 position, if found, so that we can scan ahead and check if the
8317 word later overshoots the window edge. Use atx_it similarly, for
8318 pixel positions. */
8319 wrap_it.sp = -1;
8320 atpos_it.sp = -1;
8321 atx_it.sp = -1;
8322
8323 /* Use ppos_it under bidi reordering to save a copy of IT for the
8324 position > CHARPOS that is the closest to CHARPOS. We restore
8325 that position in IT when we have scanned the entire display line
8326 without finding a match for CHARPOS and all the character
8327 positions are greater than CHARPOS. */
8328 if (it->bidi_p)
8329 {
8330 SAVE_IT (ppos_it, *it, ppos_data);
8331 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8332 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8333 SAVE_IT (ppos_it, *it, ppos_data);
8334 }
8335
8336 #define BUFFER_POS_REACHED_P() \
8337 ((op & MOVE_TO_POS) != 0 \
8338 && BUFFERP (it->object) \
8339 && (IT_CHARPOS (*it) == to_charpos \
8340 || ((!it->bidi_p \
8341 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8342 && IT_CHARPOS (*it) > to_charpos) \
8343 || (it->what == IT_COMPOSITION \
8344 && ((IT_CHARPOS (*it) > to_charpos \
8345 && to_charpos >= it->cmp_it.charpos) \
8346 || (IT_CHARPOS (*it) < to_charpos \
8347 && to_charpos <= it->cmp_it.charpos)))) \
8348 && (it->method == GET_FROM_BUFFER \
8349 || (it->method == GET_FROM_DISPLAY_VECTOR \
8350 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8351
8352 /* If there's a line-/wrap-prefix, handle it. */
8353 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8354 && it->current_y < it->last_visible_y)
8355 handle_line_prefix (it);
8356
8357 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8358 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8359
8360 while (1)
8361 {
8362 int x, i, ascent = 0, descent = 0;
8363
8364 /* Utility macro to reset an iterator with x, ascent, and descent. */
8365 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8366 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8367 (IT)->max_descent = descent)
8368
8369 /* Stop if we move beyond TO_CHARPOS (after an image or a
8370 display string or stretch glyph). */
8371 if ((op & MOVE_TO_POS) != 0
8372 && BUFFERP (it->object)
8373 && it->method == GET_FROM_BUFFER
8374 && (((!it->bidi_p
8375 /* When the iterator is at base embedding level, we
8376 are guaranteed that characters are delivered for
8377 display in strictly increasing order of their
8378 buffer positions. */
8379 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8380 && IT_CHARPOS (*it) > to_charpos)
8381 || (it->bidi_p
8382 && (prev_method == GET_FROM_IMAGE
8383 || prev_method == GET_FROM_STRETCH
8384 || prev_method == GET_FROM_STRING)
8385 /* Passed TO_CHARPOS from left to right. */
8386 && ((prev_pos < to_charpos
8387 && IT_CHARPOS (*it) > to_charpos)
8388 /* Passed TO_CHARPOS from right to left. */
8389 || (prev_pos > to_charpos
8390 && IT_CHARPOS (*it) < to_charpos)))))
8391 {
8392 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8393 {
8394 result = MOVE_POS_MATCH_OR_ZV;
8395 break;
8396 }
8397 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8398 /* If wrap_it is valid, the current position might be in a
8399 word that is wrapped. So, save the iterator in
8400 atpos_it and continue to see if wrapping happens. */
8401 SAVE_IT (atpos_it, *it, atpos_data);
8402 }
8403
8404 /* Stop when ZV reached.
8405 We used to stop here when TO_CHARPOS reached as well, but that is
8406 too soon if this glyph does not fit on this line. So we handle it
8407 explicitly below. */
8408 if (!get_next_display_element (it))
8409 {
8410 result = MOVE_POS_MATCH_OR_ZV;
8411 break;
8412 }
8413
8414 if (it->line_wrap == TRUNCATE)
8415 {
8416 if (BUFFER_POS_REACHED_P ())
8417 {
8418 result = MOVE_POS_MATCH_OR_ZV;
8419 break;
8420 }
8421 }
8422 else
8423 {
8424 if (it->line_wrap == WORD_WRAP)
8425 {
8426 if (IT_DISPLAYING_WHITESPACE (it))
8427 may_wrap = 1;
8428 else if (may_wrap)
8429 {
8430 /* We have reached a glyph that follows one or more
8431 whitespace characters. If the position is
8432 already found, we are done. */
8433 if (atpos_it.sp >= 0)
8434 {
8435 RESTORE_IT (it, &atpos_it, atpos_data);
8436 result = MOVE_POS_MATCH_OR_ZV;
8437 goto done;
8438 }
8439 if (atx_it.sp >= 0)
8440 {
8441 RESTORE_IT (it, &atx_it, atx_data);
8442 result = MOVE_X_REACHED;
8443 goto done;
8444 }
8445 /* Otherwise, we can wrap here. */
8446 SAVE_IT (wrap_it, *it, wrap_data);
8447 may_wrap = 0;
8448 }
8449 }
8450 }
8451
8452 /* Remember the line height for the current line, in case
8453 the next element doesn't fit on the line. */
8454 ascent = it->max_ascent;
8455 descent = it->max_descent;
8456
8457 /* The call to produce_glyphs will get the metrics of the
8458 display element IT is loaded with. Record the x-position
8459 before this display element, in case it doesn't fit on the
8460 line. */
8461 x = it->current_x;
8462
8463 PRODUCE_GLYPHS (it);
8464
8465 if (it->area != TEXT_AREA)
8466 {
8467 prev_method = it->method;
8468 if (it->method == GET_FROM_BUFFER)
8469 prev_pos = IT_CHARPOS (*it);
8470 set_iterator_to_next (it, 1);
8471 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8472 SET_TEXT_POS (this_line_min_pos,
8473 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8474 if (it->bidi_p
8475 && (op & MOVE_TO_POS)
8476 && IT_CHARPOS (*it) > to_charpos
8477 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8478 SAVE_IT (ppos_it, *it, ppos_data);
8479 continue;
8480 }
8481
8482 /* The number of glyphs we get back in IT->nglyphs will normally
8483 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8484 character on a terminal frame, or (iii) a line end. For the
8485 second case, IT->nglyphs - 1 padding glyphs will be present.
8486 (On X frames, there is only one glyph produced for a
8487 composite character.)
8488
8489 The behavior implemented below means, for continuation lines,
8490 that as many spaces of a TAB as fit on the current line are
8491 displayed there. For terminal frames, as many glyphs of a
8492 multi-glyph character are displayed in the current line, too.
8493 This is what the old redisplay code did, and we keep it that
8494 way. Under X, the whole shape of a complex character must
8495 fit on the line or it will be completely displayed in the
8496 next line.
8497
8498 Note that both for tabs and padding glyphs, all glyphs have
8499 the same width. */
8500 if (it->nglyphs)
8501 {
8502 /* More than one glyph or glyph doesn't fit on line. All
8503 glyphs have the same width. */
8504 int single_glyph_width = it->pixel_width / it->nglyphs;
8505 int new_x;
8506 int x_before_this_char = x;
8507 int hpos_before_this_char = it->hpos;
8508
8509 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8510 {
8511 new_x = x + single_glyph_width;
8512
8513 /* We want to leave anything reaching TO_X to the caller. */
8514 if ((op & MOVE_TO_X) && new_x > to_x)
8515 {
8516 if (BUFFER_POS_REACHED_P ())
8517 {
8518 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8519 goto buffer_pos_reached;
8520 if (atpos_it.sp < 0)
8521 {
8522 SAVE_IT (atpos_it, *it, atpos_data);
8523 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8524 }
8525 }
8526 else
8527 {
8528 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8529 {
8530 it->current_x = x;
8531 result = MOVE_X_REACHED;
8532 break;
8533 }
8534 if (atx_it.sp < 0)
8535 {
8536 SAVE_IT (atx_it, *it, atx_data);
8537 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8538 }
8539 }
8540 }
8541
8542 if (/* Lines are continued. */
8543 it->line_wrap != TRUNCATE
8544 && (/* And glyph doesn't fit on the line. */
8545 new_x > it->last_visible_x
8546 /* Or it fits exactly and we're on a window
8547 system frame. */
8548 || (new_x == it->last_visible_x
8549 && FRAME_WINDOW_P (it->f)
8550 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8551 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8552 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8553 {
8554 if (/* IT->hpos == 0 means the very first glyph
8555 doesn't fit on the line, e.g. a wide image. */
8556 it->hpos == 0
8557 || (new_x == it->last_visible_x
8558 && FRAME_WINDOW_P (it->f)))
8559 {
8560 ++it->hpos;
8561 it->current_x = new_x;
8562
8563 /* The character's last glyph just barely fits
8564 in this row. */
8565 if (i == it->nglyphs - 1)
8566 {
8567 /* If this is the destination position,
8568 return a position *before* it in this row,
8569 now that we know it fits in this row. */
8570 if (BUFFER_POS_REACHED_P ())
8571 {
8572 if (it->line_wrap != WORD_WRAP
8573 || wrap_it.sp < 0)
8574 {
8575 it->hpos = hpos_before_this_char;
8576 it->current_x = x_before_this_char;
8577 result = MOVE_POS_MATCH_OR_ZV;
8578 break;
8579 }
8580 if (it->line_wrap == WORD_WRAP
8581 && atpos_it.sp < 0)
8582 {
8583 SAVE_IT (atpos_it, *it, atpos_data);
8584 atpos_it.current_x = x_before_this_char;
8585 atpos_it.hpos = hpos_before_this_char;
8586 }
8587 }
8588
8589 prev_method = it->method;
8590 if (it->method == GET_FROM_BUFFER)
8591 prev_pos = IT_CHARPOS (*it);
8592 set_iterator_to_next (it, 1);
8593 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8594 SET_TEXT_POS (this_line_min_pos,
8595 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8596 /* On graphical terminals, newlines may
8597 "overflow" into the fringe if
8598 overflow-newline-into-fringe is non-nil.
8599 On text terminals, and on graphical
8600 terminals with no right margin, newlines
8601 may overflow into the last glyph on the
8602 display line.*/
8603 if (!FRAME_WINDOW_P (it->f)
8604 || ((it->bidi_p
8605 && it->bidi_it.paragraph_dir == R2L)
8606 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8607 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8608 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8609 {
8610 if (!get_next_display_element (it))
8611 {
8612 result = MOVE_POS_MATCH_OR_ZV;
8613 break;
8614 }
8615 if (BUFFER_POS_REACHED_P ())
8616 {
8617 if (ITERATOR_AT_END_OF_LINE_P (it))
8618 result = MOVE_POS_MATCH_OR_ZV;
8619 else
8620 result = MOVE_LINE_CONTINUED;
8621 break;
8622 }
8623 if (ITERATOR_AT_END_OF_LINE_P (it)
8624 && (it->line_wrap != WORD_WRAP
8625 || wrap_it.sp < 0))
8626 {
8627 result = MOVE_NEWLINE_OR_CR;
8628 break;
8629 }
8630 }
8631 }
8632 }
8633 else
8634 IT_RESET_X_ASCENT_DESCENT (it);
8635
8636 if (wrap_it.sp >= 0)
8637 {
8638 RESTORE_IT (it, &wrap_it, wrap_data);
8639 atpos_it.sp = -1;
8640 atx_it.sp = -1;
8641 }
8642
8643 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8644 IT_CHARPOS (*it)));
8645 result = MOVE_LINE_CONTINUED;
8646 break;
8647 }
8648
8649 if (BUFFER_POS_REACHED_P ())
8650 {
8651 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8652 goto buffer_pos_reached;
8653 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8654 {
8655 SAVE_IT (atpos_it, *it, atpos_data);
8656 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8657 }
8658 }
8659
8660 if (new_x > it->first_visible_x)
8661 {
8662 /* Glyph is visible. Increment number of glyphs that
8663 would be displayed. */
8664 ++it->hpos;
8665 }
8666 }
8667
8668 if (result != MOVE_UNDEFINED)
8669 break;
8670 }
8671 else if (BUFFER_POS_REACHED_P ())
8672 {
8673 buffer_pos_reached:
8674 IT_RESET_X_ASCENT_DESCENT (it);
8675 result = MOVE_POS_MATCH_OR_ZV;
8676 break;
8677 }
8678 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8679 {
8680 /* Stop when TO_X specified and reached. This check is
8681 necessary here because of lines consisting of a line end,
8682 only. The line end will not produce any glyphs and we
8683 would never get MOVE_X_REACHED. */
8684 eassert (it->nglyphs == 0);
8685 result = MOVE_X_REACHED;
8686 break;
8687 }
8688
8689 /* Is this a line end? If yes, we're done. */
8690 if (ITERATOR_AT_END_OF_LINE_P (it))
8691 {
8692 /* If we are past TO_CHARPOS, but never saw any character
8693 positions smaller than TO_CHARPOS, return
8694 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8695 did. */
8696 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8697 {
8698 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8699 {
8700 if (IT_CHARPOS (ppos_it) < ZV)
8701 {
8702 RESTORE_IT (it, &ppos_it, ppos_data);
8703 result = MOVE_POS_MATCH_OR_ZV;
8704 }
8705 else
8706 goto buffer_pos_reached;
8707 }
8708 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8709 && IT_CHARPOS (*it) > to_charpos)
8710 goto buffer_pos_reached;
8711 else
8712 result = MOVE_NEWLINE_OR_CR;
8713 }
8714 else
8715 result = MOVE_NEWLINE_OR_CR;
8716 break;
8717 }
8718
8719 prev_method = it->method;
8720 if (it->method == GET_FROM_BUFFER)
8721 prev_pos = IT_CHARPOS (*it);
8722 /* The current display element has been consumed. Advance
8723 to the next. */
8724 set_iterator_to_next (it, 1);
8725 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8726 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8727 if (IT_CHARPOS (*it) < to_charpos)
8728 saw_smaller_pos = 1;
8729 if (it->bidi_p
8730 && (op & MOVE_TO_POS)
8731 && IT_CHARPOS (*it) >= to_charpos
8732 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8733 SAVE_IT (ppos_it, *it, ppos_data);
8734
8735 /* Stop if lines are truncated and IT's current x-position is
8736 past the right edge of the window now. */
8737 if (it->line_wrap == TRUNCATE
8738 && it->current_x >= it->last_visible_x)
8739 {
8740 if (!FRAME_WINDOW_P (it->f)
8741 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8742 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8743 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8744 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8745 {
8746 int at_eob_p = 0;
8747
8748 if ((at_eob_p = !get_next_display_element (it))
8749 || BUFFER_POS_REACHED_P ()
8750 /* If we are past TO_CHARPOS, but never saw any
8751 character positions smaller than TO_CHARPOS,
8752 return MOVE_POS_MATCH_OR_ZV, like the
8753 unidirectional display did. */
8754 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8755 && !saw_smaller_pos
8756 && IT_CHARPOS (*it) > to_charpos))
8757 {
8758 if (it->bidi_p
8759 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8760 RESTORE_IT (it, &ppos_it, ppos_data);
8761 result = MOVE_POS_MATCH_OR_ZV;
8762 break;
8763 }
8764 if (ITERATOR_AT_END_OF_LINE_P (it))
8765 {
8766 result = MOVE_NEWLINE_OR_CR;
8767 break;
8768 }
8769 }
8770 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8771 && !saw_smaller_pos
8772 && IT_CHARPOS (*it) > to_charpos)
8773 {
8774 if (IT_CHARPOS (ppos_it) < ZV)
8775 RESTORE_IT (it, &ppos_it, ppos_data);
8776 result = MOVE_POS_MATCH_OR_ZV;
8777 break;
8778 }
8779 result = MOVE_LINE_TRUNCATED;
8780 break;
8781 }
8782 #undef IT_RESET_X_ASCENT_DESCENT
8783 }
8784
8785 #undef BUFFER_POS_REACHED_P
8786
8787 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8788 restore the saved iterator. */
8789 if (atpos_it.sp >= 0)
8790 RESTORE_IT (it, &atpos_it, atpos_data);
8791 else if (atx_it.sp >= 0)
8792 RESTORE_IT (it, &atx_it, atx_data);
8793
8794 done:
8795
8796 if (atpos_data)
8797 bidi_unshelve_cache (atpos_data, 1);
8798 if (atx_data)
8799 bidi_unshelve_cache (atx_data, 1);
8800 if (wrap_data)
8801 bidi_unshelve_cache (wrap_data, 1);
8802 if (ppos_data)
8803 bidi_unshelve_cache (ppos_data, 1);
8804
8805 /* Restore the iterator settings altered at the beginning of this
8806 function. */
8807 it->glyph_row = saved_glyph_row;
8808 return result;
8809 }
8810
8811 /* For external use. */
8812 void
8813 move_it_in_display_line (struct it *it,
8814 ptrdiff_t to_charpos, int to_x,
8815 enum move_operation_enum op)
8816 {
8817 if (it->line_wrap == WORD_WRAP
8818 && (op & MOVE_TO_X))
8819 {
8820 struct it save_it;
8821 void *save_data = NULL;
8822 int skip;
8823
8824 SAVE_IT (save_it, *it, save_data);
8825 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8826 /* When word-wrap is on, TO_X may lie past the end
8827 of a wrapped line. Then it->current is the
8828 character on the next line, so backtrack to the
8829 space before the wrap point. */
8830 if (skip == MOVE_LINE_CONTINUED)
8831 {
8832 int prev_x = max (it->current_x - 1, 0);
8833 RESTORE_IT (it, &save_it, save_data);
8834 move_it_in_display_line_to
8835 (it, -1, prev_x, MOVE_TO_X);
8836 }
8837 else
8838 bidi_unshelve_cache (save_data, 1);
8839 }
8840 else
8841 move_it_in_display_line_to (it, to_charpos, to_x, op);
8842 }
8843
8844
8845 /* Move IT forward until it satisfies one or more of the criteria in
8846 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8847
8848 OP is a bit-mask that specifies where to stop, and in particular,
8849 which of those four position arguments makes a difference. See the
8850 description of enum move_operation_enum.
8851
8852 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8853 screen line, this function will set IT to the next position that is
8854 displayed to the right of TO_CHARPOS on the screen.
8855
8856 Return the maximum pixel length of any line scanned but never more
8857 than it.last_visible_x. */
8858
8859 int
8860 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8861 {
8862 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8863 int line_height, line_start_x = 0, reached = 0;
8864 int max_current_x = 0;
8865 void *backup_data = NULL;
8866
8867 for (;;)
8868 {
8869 if (op & MOVE_TO_VPOS)
8870 {
8871 /* If no TO_CHARPOS and no TO_X specified, stop at the
8872 start of the line TO_VPOS. */
8873 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8874 {
8875 if (it->vpos == to_vpos)
8876 {
8877 reached = 1;
8878 break;
8879 }
8880 else
8881 skip = move_it_in_display_line_to (it, -1, -1, 0);
8882 }
8883 else
8884 {
8885 /* TO_VPOS >= 0 means stop at TO_X in the line at
8886 TO_VPOS, or at TO_POS, whichever comes first. */
8887 if (it->vpos == to_vpos)
8888 {
8889 reached = 2;
8890 break;
8891 }
8892
8893 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8894
8895 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8896 {
8897 reached = 3;
8898 break;
8899 }
8900 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8901 {
8902 /* We have reached TO_X but not in the line we want. */
8903 skip = move_it_in_display_line_to (it, to_charpos,
8904 -1, MOVE_TO_POS);
8905 if (skip == MOVE_POS_MATCH_OR_ZV)
8906 {
8907 reached = 4;
8908 break;
8909 }
8910 }
8911 }
8912 }
8913 else if (op & MOVE_TO_Y)
8914 {
8915 struct it it_backup;
8916
8917 if (it->line_wrap == WORD_WRAP)
8918 SAVE_IT (it_backup, *it, backup_data);
8919
8920 /* TO_Y specified means stop at TO_X in the line containing
8921 TO_Y---or at TO_CHARPOS if this is reached first. The
8922 problem is that we can't really tell whether the line
8923 contains TO_Y before we have completely scanned it, and
8924 this may skip past TO_X. What we do is to first scan to
8925 TO_X.
8926
8927 If TO_X is not specified, use a TO_X of zero. The reason
8928 is to make the outcome of this function more predictable.
8929 If we didn't use TO_X == 0, we would stop at the end of
8930 the line which is probably not what a caller would expect
8931 to happen. */
8932 skip = move_it_in_display_line_to
8933 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8934 (MOVE_TO_X | (op & MOVE_TO_POS)));
8935
8936 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8937 if (skip == MOVE_POS_MATCH_OR_ZV)
8938 reached = 5;
8939 else if (skip == MOVE_X_REACHED)
8940 {
8941 /* If TO_X was reached, we want to know whether TO_Y is
8942 in the line. We know this is the case if the already
8943 scanned glyphs make the line tall enough. Otherwise,
8944 we must check by scanning the rest of the line. */
8945 line_height = it->max_ascent + it->max_descent;
8946 if (to_y >= it->current_y
8947 && to_y < it->current_y + line_height)
8948 {
8949 reached = 6;
8950 break;
8951 }
8952 SAVE_IT (it_backup, *it, backup_data);
8953 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8954 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8955 op & MOVE_TO_POS);
8956 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8957 line_height = it->max_ascent + it->max_descent;
8958 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8959
8960 if (to_y >= it->current_y
8961 && to_y < it->current_y + line_height)
8962 {
8963 /* If TO_Y is in this line and TO_X was reached
8964 above, we scanned too far. We have to restore
8965 IT's settings to the ones before skipping. But
8966 keep the more accurate values of max_ascent and
8967 max_descent we've found while skipping the rest
8968 of the line, for the sake of callers, such as
8969 pos_visible_p, that need to know the line
8970 height. */
8971 int max_ascent = it->max_ascent;
8972 int max_descent = it->max_descent;
8973
8974 RESTORE_IT (it, &it_backup, backup_data);
8975 it->max_ascent = max_ascent;
8976 it->max_descent = max_descent;
8977 reached = 6;
8978 }
8979 else
8980 {
8981 skip = skip2;
8982 if (skip == MOVE_POS_MATCH_OR_ZV)
8983 reached = 7;
8984 }
8985 }
8986 else
8987 {
8988 /* Check whether TO_Y is in this line. */
8989 line_height = it->max_ascent + it->max_descent;
8990 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8991
8992 if (to_y >= it->current_y
8993 && to_y < it->current_y + line_height)
8994 {
8995 if (to_y > it->current_y)
8996 max_current_x = max (it->current_x, max_current_x);
8997
8998 /* When word-wrap is on, TO_X may lie past the end
8999 of a wrapped line. Then it->current is the
9000 character on the next line, so backtrack to the
9001 space before the wrap point. */
9002 if (skip == MOVE_LINE_CONTINUED
9003 && it->line_wrap == WORD_WRAP)
9004 {
9005 int prev_x = max (it->current_x - 1, 0);
9006 RESTORE_IT (it, &it_backup, backup_data);
9007 skip = move_it_in_display_line_to
9008 (it, -1, prev_x, MOVE_TO_X);
9009 }
9010
9011 reached = 6;
9012 }
9013 }
9014
9015 if (reached)
9016 {
9017 max_current_x = max (it->current_x, max_current_x);
9018 break;
9019 }
9020 }
9021 else if (BUFFERP (it->object)
9022 && (it->method == GET_FROM_BUFFER
9023 || it->method == GET_FROM_STRETCH)
9024 && IT_CHARPOS (*it) >= to_charpos
9025 /* Under bidi iteration, a call to set_iterator_to_next
9026 can scan far beyond to_charpos if the initial
9027 portion of the next line needs to be reordered. In
9028 that case, give move_it_in_display_line_to another
9029 chance below. */
9030 && !(it->bidi_p
9031 && it->bidi_it.scan_dir == -1))
9032 skip = MOVE_POS_MATCH_OR_ZV;
9033 else
9034 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9035
9036 switch (skip)
9037 {
9038 case MOVE_POS_MATCH_OR_ZV:
9039 max_current_x = max (it->current_x, max_current_x);
9040 reached = 8;
9041 goto out;
9042
9043 case MOVE_NEWLINE_OR_CR:
9044 max_current_x = max (it->current_x, max_current_x);
9045 set_iterator_to_next (it, 1);
9046 it->continuation_lines_width = 0;
9047 break;
9048
9049 case MOVE_LINE_TRUNCATED:
9050 max_current_x = it->last_visible_x;
9051 it->continuation_lines_width = 0;
9052 reseat_at_next_visible_line_start (it, 0);
9053 if ((op & MOVE_TO_POS) != 0
9054 && IT_CHARPOS (*it) > to_charpos)
9055 {
9056 reached = 9;
9057 goto out;
9058 }
9059 break;
9060
9061 case MOVE_LINE_CONTINUED:
9062 max_current_x = it->last_visible_x;
9063 /* For continued lines ending in a tab, some of the glyphs
9064 associated with the tab are displayed on the current
9065 line. Since it->current_x does not include these glyphs,
9066 we use it->last_visible_x instead. */
9067 if (it->c == '\t')
9068 {
9069 it->continuation_lines_width += it->last_visible_x;
9070 /* When moving by vpos, ensure that the iterator really
9071 advances to the next line (bug#847, bug#969). Fixme:
9072 do we need to do this in other circumstances? */
9073 if (it->current_x != it->last_visible_x
9074 && (op & MOVE_TO_VPOS)
9075 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9076 {
9077 line_start_x = it->current_x + it->pixel_width
9078 - it->last_visible_x;
9079 set_iterator_to_next (it, 0);
9080 }
9081 }
9082 else
9083 it->continuation_lines_width += it->current_x;
9084 break;
9085
9086 default:
9087 emacs_abort ();
9088 }
9089
9090 /* Reset/increment for the next run. */
9091 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9092 it->current_x = line_start_x;
9093 line_start_x = 0;
9094 it->hpos = 0;
9095 it->current_y += it->max_ascent + it->max_descent;
9096 ++it->vpos;
9097 last_height = it->max_ascent + it->max_descent;
9098 last_max_ascent = it->max_ascent;
9099 it->max_ascent = it->max_descent = 0;
9100 }
9101
9102 out:
9103
9104 /* On text terminals, we may stop at the end of a line in the middle
9105 of a multi-character glyph. If the glyph itself is continued,
9106 i.e. it is actually displayed on the next line, don't treat this
9107 stopping point as valid; move to the next line instead (unless
9108 that brings us offscreen). */
9109 if (!FRAME_WINDOW_P (it->f)
9110 && op & MOVE_TO_POS
9111 && IT_CHARPOS (*it) == to_charpos
9112 && it->what == IT_CHARACTER
9113 && it->nglyphs > 1
9114 && it->line_wrap == WINDOW_WRAP
9115 && it->current_x == it->last_visible_x - 1
9116 && it->c != '\n'
9117 && it->c != '\t'
9118 && it->vpos < it->w->window_end_vpos)
9119 {
9120 it->continuation_lines_width += it->current_x;
9121 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9122 it->current_y += it->max_ascent + it->max_descent;
9123 ++it->vpos;
9124 last_height = it->max_ascent + it->max_descent;
9125 last_max_ascent = it->max_ascent;
9126 }
9127
9128 if (backup_data)
9129 bidi_unshelve_cache (backup_data, 1);
9130
9131 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9132
9133 return max_current_x;
9134 }
9135
9136
9137 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9138
9139 If DY > 0, move IT backward at least that many pixels. DY = 0
9140 means move IT backward to the preceding line start or BEGV. This
9141 function may move over more than DY pixels if IT->current_y - DY
9142 ends up in the middle of a line; in this case IT->current_y will be
9143 set to the top of the line moved to. */
9144
9145 void
9146 move_it_vertically_backward (struct it *it, int dy)
9147 {
9148 int nlines, h;
9149 struct it it2, it3;
9150 void *it2data = NULL, *it3data = NULL;
9151 ptrdiff_t start_pos;
9152 int nchars_per_row
9153 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9154 ptrdiff_t pos_limit;
9155
9156 move_further_back:
9157 eassert (dy >= 0);
9158
9159 start_pos = IT_CHARPOS (*it);
9160
9161 /* Estimate how many newlines we must move back. */
9162 nlines = max (1, dy / default_line_pixel_height (it->w));
9163 if (it->line_wrap == TRUNCATE)
9164 pos_limit = BEGV;
9165 else
9166 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9167
9168 /* Set the iterator's position that many lines back. But don't go
9169 back more than NLINES full screen lines -- this wins a day with
9170 buffers which have very long lines. */
9171 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9172 back_to_previous_visible_line_start (it);
9173
9174 /* Reseat the iterator here. When moving backward, we don't want
9175 reseat to skip forward over invisible text, set up the iterator
9176 to deliver from overlay strings at the new position etc. So,
9177 use reseat_1 here. */
9178 reseat_1 (it, it->current.pos, 1);
9179
9180 /* We are now surely at a line start. */
9181 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9182 reordering is in effect. */
9183 it->continuation_lines_width = 0;
9184
9185 /* Move forward and see what y-distance we moved. First move to the
9186 start of the next line so that we get its height. We need this
9187 height to be able to tell whether we reached the specified
9188 y-distance. */
9189 SAVE_IT (it2, *it, it2data);
9190 it2.max_ascent = it2.max_descent = 0;
9191 do
9192 {
9193 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9194 MOVE_TO_POS | MOVE_TO_VPOS);
9195 }
9196 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9197 /* If we are in a display string which starts at START_POS,
9198 and that display string includes a newline, and we are
9199 right after that newline (i.e. at the beginning of a
9200 display line), exit the loop, because otherwise we will
9201 infloop, since move_it_to will see that it is already at
9202 START_POS and will not move. */
9203 || (it2.method == GET_FROM_STRING
9204 && IT_CHARPOS (it2) == start_pos
9205 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9206 eassert (IT_CHARPOS (*it) >= BEGV);
9207 SAVE_IT (it3, it2, it3data);
9208
9209 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9210 eassert (IT_CHARPOS (*it) >= BEGV);
9211 /* H is the actual vertical distance from the position in *IT
9212 and the starting position. */
9213 h = it2.current_y - it->current_y;
9214 /* NLINES is the distance in number of lines. */
9215 nlines = it2.vpos - it->vpos;
9216
9217 /* Correct IT's y and vpos position
9218 so that they are relative to the starting point. */
9219 it->vpos -= nlines;
9220 it->current_y -= h;
9221
9222 if (dy == 0)
9223 {
9224 /* DY == 0 means move to the start of the screen line. The
9225 value of nlines is > 0 if continuation lines were involved,
9226 or if the original IT position was at start of a line. */
9227 RESTORE_IT (it, it, it2data);
9228 if (nlines > 0)
9229 move_it_by_lines (it, nlines);
9230 /* The above code moves us to some position NLINES down,
9231 usually to its first glyph (leftmost in an L2R line), but
9232 that's not necessarily the start of the line, under bidi
9233 reordering. We want to get to the character position
9234 that is immediately after the newline of the previous
9235 line. */
9236 if (it->bidi_p
9237 && !it->continuation_lines_width
9238 && !STRINGP (it->string)
9239 && IT_CHARPOS (*it) > BEGV
9240 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9241 {
9242 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9243
9244 DEC_BOTH (cp, bp);
9245 cp = find_newline_no_quit (cp, bp, -1, NULL);
9246 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9247 }
9248 bidi_unshelve_cache (it3data, 1);
9249 }
9250 else
9251 {
9252 /* The y-position we try to reach, relative to *IT.
9253 Note that H has been subtracted in front of the if-statement. */
9254 int target_y = it->current_y + h - dy;
9255 int y0 = it3.current_y;
9256 int y1;
9257 int line_height;
9258
9259 RESTORE_IT (&it3, &it3, it3data);
9260 y1 = line_bottom_y (&it3);
9261 line_height = y1 - y0;
9262 RESTORE_IT (it, it, it2data);
9263 /* If we did not reach target_y, try to move further backward if
9264 we can. If we moved too far backward, try to move forward. */
9265 if (target_y < it->current_y
9266 /* This is heuristic. In a window that's 3 lines high, with
9267 a line height of 13 pixels each, recentering with point
9268 on the bottom line will try to move -39/2 = 19 pixels
9269 backward. Try to avoid moving into the first line. */
9270 && (it->current_y - target_y
9271 > min (window_box_height (it->w), line_height * 2 / 3))
9272 && IT_CHARPOS (*it) > BEGV)
9273 {
9274 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9275 target_y - it->current_y));
9276 dy = it->current_y - target_y;
9277 goto move_further_back;
9278 }
9279 else if (target_y >= it->current_y + line_height
9280 && IT_CHARPOS (*it) < ZV)
9281 {
9282 /* Should move forward by at least one line, maybe more.
9283
9284 Note: Calling move_it_by_lines can be expensive on
9285 terminal frames, where compute_motion is used (via
9286 vmotion) to do the job, when there are very long lines
9287 and truncate-lines is nil. That's the reason for
9288 treating terminal frames specially here. */
9289
9290 if (!FRAME_WINDOW_P (it->f))
9291 move_it_vertically (it, target_y - (it->current_y + line_height));
9292 else
9293 {
9294 do
9295 {
9296 move_it_by_lines (it, 1);
9297 }
9298 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9299 }
9300 }
9301 }
9302 }
9303
9304
9305 /* Move IT by a specified amount of pixel lines DY. DY negative means
9306 move backwards. DY = 0 means move to start of screen line. At the
9307 end, IT will be on the start of a screen line. */
9308
9309 void
9310 move_it_vertically (struct it *it, int dy)
9311 {
9312 if (dy <= 0)
9313 move_it_vertically_backward (it, -dy);
9314 else
9315 {
9316 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9317 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9318 MOVE_TO_POS | MOVE_TO_Y);
9319 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9320
9321 /* If buffer ends in ZV without a newline, move to the start of
9322 the line to satisfy the post-condition. */
9323 if (IT_CHARPOS (*it) == ZV
9324 && ZV > BEGV
9325 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9326 move_it_by_lines (it, 0);
9327 }
9328 }
9329
9330
9331 /* Move iterator IT past the end of the text line it is in. */
9332
9333 void
9334 move_it_past_eol (struct it *it)
9335 {
9336 enum move_it_result rc;
9337
9338 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9339 if (rc == MOVE_NEWLINE_OR_CR)
9340 set_iterator_to_next (it, 0);
9341 }
9342
9343
9344 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9345 negative means move up. DVPOS == 0 means move to the start of the
9346 screen line.
9347
9348 Optimization idea: If we would know that IT->f doesn't use
9349 a face with proportional font, we could be faster for
9350 truncate-lines nil. */
9351
9352 void
9353 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9354 {
9355
9356 /* The commented-out optimization uses vmotion on terminals. This
9357 gives bad results, because elements like it->what, on which
9358 callers such as pos_visible_p rely, aren't updated. */
9359 /* struct position pos;
9360 if (!FRAME_WINDOW_P (it->f))
9361 {
9362 struct text_pos textpos;
9363
9364 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9365 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9366 reseat (it, textpos, 1);
9367 it->vpos += pos.vpos;
9368 it->current_y += pos.vpos;
9369 }
9370 else */
9371
9372 if (dvpos == 0)
9373 {
9374 /* DVPOS == 0 means move to the start of the screen line. */
9375 move_it_vertically_backward (it, 0);
9376 /* Let next call to line_bottom_y calculate real line height. */
9377 last_height = 0;
9378 }
9379 else if (dvpos > 0)
9380 {
9381 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9382 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9383 {
9384 /* Only move to the next buffer position if we ended up in a
9385 string from display property, not in an overlay string
9386 (before-string or after-string). That is because the
9387 latter don't conceal the underlying buffer position, so
9388 we can ask to move the iterator to the exact position we
9389 are interested in. Note that, even if we are already at
9390 IT_CHARPOS (*it), the call below is not a no-op, as it
9391 will detect that we are at the end of the string, pop the
9392 iterator, and compute it->current_x and it->hpos
9393 correctly. */
9394 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9395 -1, -1, -1, MOVE_TO_POS);
9396 }
9397 }
9398 else
9399 {
9400 struct it it2;
9401 void *it2data = NULL;
9402 ptrdiff_t start_charpos, i;
9403 int nchars_per_row
9404 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9405 ptrdiff_t pos_limit;
9406
9407 /* Start at the beginning of the screen line containing IT's
9408 position. This may actually move vertically backwards,
9409 in case of overlays, so adjust dvpos accordingly. */
9410 dvpos += it->vpos;
9411 move_it_vertically_backward (it, 0);
9412 dvpos -= it->vpos;
9413
9414 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9415 screen lines, and reseat the iterator there. */
9416 start_charpos = IT_CHARPOS (*it);
9417 if (it->line_wrap == TRUNCATE)
9418 pos_limit = BEGV;
9419 else
9420 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9421 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9422 back_to_previous_visible_line_start (it);
9423 reseat (it, it->current.pos, 1);
9424
9425 /* Move further back if we end up in a string or an image. */
9426 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9427 {
9428 /* First try to move to start of display line. */
9429 dvpos += it->vpos;
9430 move_it_vertically_backward (it, 0);
9431 dvpos -= it->vpos;
9432 if (IT_POS_VALID_AFTER_MOVE_P (it))
9433 break;
9434 /* If start of line is still in string or image,
9435 move further back. */
9436 back_to_previous_visible_line_start (it);
9437 reseat (it, it->current.pos, 1);
9438 dvpos--;
9439 }
9440
9441 it->current_x = it->hpos = 0;
9442
9443 /* Above call may have moved too far if continuation lines
9444 are involved. Scan forward and see if it did. */
9445 SAVE_IT (it2, *it, it2data);
9446 it2.vpos = it2.current_y = 0;
9447 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9448 it->vpos -= it2.vpos;
9449 it->current_y -= it2.current_y;
9450 it->current_x = it->hpos = 0;
9451
9452 /* If we moved too far back, move IT some lines forward. */
9453 if (it2.vpos > -dvpos)
9454 {
9455 int delta = it2.vpos + dvpos;
9456
9457 RESTORE_IT (&it2, &it2, it2data);
9458 SAVE_IT (it2, *it, it2data);
9459 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9460 /* Move back again if we got too far ahead. */
9461 if (IT_CHARPOS (*it) >= start_charpos)
9462 RESTORE_IT (it, &it2, it2data);
9463 else
9464 bidi_unshelve_cache (it2data, 1);
9465 }
9466 else
9467 RESTORE_IT (it, it, it2data);
9468 }
9469 }
9470
9471 /* Return 1 if IT points into the middle of a display vector. */
9472
9473 int
9474 in_display_vector_p (struct it *it)
9475 {
9476 return (it->method == GET_FROM_DISPLAY_VECTOR
9477 && it->current.dpvec_index > 0
9478 && it->dpvec + it->current.dpvec_index != it->dpend);
9479 }
9480
9481 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9482 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9483 WINDOW must be a live window and defaults to the selected one. The
9484 return value is a cons of the maximum pixel-width of any text line and
9485 the maximum pixel-height of all text lines.
9486
9487 The optional argument FROM, if non-nil, specifies the first text
9488 position and defaults to the minimum accessible position of the buffer.
9489 If FROM is t, use the minimum accessible position that is not a newline
9490 character. TO, if non-nil, specifies the last text position and
9491 defaults to the maximum accessible position of the buffer. If TO is t,
9492 use the maximum accessible position that is not a newline character.
9493
9494 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9495 width that can be returned. X_LIMIT nil or omitted, means to use the
9496 pixel-width of WINDOW's body; use this if you do not intend to change
9497 the width of WINDOW. Use the maximum width WINDOW may assume if you
9498 intend to change WINDOW's width.
9499
9500 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9501 height that can be returned. Text lines whose y-coordinate is beyond
9502 Y_LIMIT are ignored. Since calculating the text height of a large
9503 buffer can take some time, it makes sense to specify this argument if
9504 the size of the buffer is unknown.
9505
9506 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9507 include the height of the mode- or header-line of WINDOW in the return
9508 value. If it is either the symbol `mode-line' or `header-line', include
9509 only the height of that line, if present, in the return value. If t,
9510 include the height of any of these lines in the return value. */)
9511 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9512 Lisp_Object mode_and_header_line)
9513 {
9514 struct window *w = decode_live_window (window);
9515 Lisp_Object buf, value;
9516 struct buffer *b;
9517 struct it it;
9518 struct buffer *old_buffer = NULL;
9519 ptrdiff_t start, end, pos;
9520 struct text_pos startp, endp;
9521 void *itdata = NULL;
9522 int c, max_y = -1, x = 0, y = 0;
9523
9524 buf = w->contents;
9525 CHECK_BUFFER (buf);
9526 b = XBUFFER (buf);
9527
9528 if (b != current_buffer)
9529 {
9530 old_buffer = current_buffer;
9531 set_buffer_internal (b);
9532 }
9533
9534 if (NILP (from))
9535 start = BEGV;
9536 else if (EQ (from, Qt))
9537 {
9538 start = pos = BEGV;
9539 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9540 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9541 start = pos;
9542 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9543 start = pos;
9544 }
9545 else
9546 {
9547 CHECK_NUMBER_COERCE_MARKER (from);
9548 start = min (max (XINT (from), BEGV), ZV);
9549 }
9550
9551 if (NILP (to))
9552 end = ZV;
9553 else if (EQ (to, Qt))
9554 {
9555 end = pos = ZV;
9556 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9557 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9558 end = pos;
9559 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9560 end = pos;
9561 }
9562 else
9563 {
9564 CHECK_NUMBER_COERCE_MARKER (to);
9565 end = max (start, min (XINT (to), ZV));
9566 }
9567
9568 if (!NILP (y_limit))
9569 {
9570 CHECK_NUMBER (y_limit);
9571 max_y = XINT (y_limit);
9572 }
9573
9574 itdata = bidi_shelve_cache ();
9575 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9576 start_display (&it, w, startp);
9577
9578 /** move_it_vertically_backward (&it, 0); **/
9579 if (NILP (x_limit))
9580 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9581 else
9582 {
9583 CHECK_NUMBER (x_limit);
9584 it.last_visible_x = XINT (x_limit);
9585 /* Actually, we never want move_it_to stop at to_x. But to make
9586 sure that move_it_in_display_line_to always moves far enough,
9587 we set it to INT_MAX and specify MOVE_TO_X. */
9588 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9589 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9590 }
9591
9592 if (start == end)
9593 y = it.current_y;
9594 else
9595 {
9596 /* Count last line. */
9597 last_height = 0;
9598 y = line_bottom_y (&it); /* - y; */
9599 }
9600
9601 if (!EQ (mode_and_header_line, Qheader_line)
9602 && !EQ (mode_and_header_line, Qt))
9603 /* Do not count the header-line which was counted automatically by
9604 start_display. */
9605 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9606
9607 if (EQ (mode_and_header_line, Qmode_line)
9608 || EQ (mode_and_header_line, Qt))
9609 /* Do count the mode-line which is not included automatically by
9610 start_display. */
9611 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9612
9613 bidi_unshelve_cache (itdata, 0);
9614
9615 if (old_buffer)
9616 set_buffer_internal (old_buffer);
9617
9618 return Fcons (make_number (x), make_number (y));
9619 }
9620 \f
9621 /***********************************************************************
9622 Messages
9623 ***********************************************************************/
9624
9625
9626 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9627 to *Messages*. */
9628
9629 void
9630 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9631 {
9632 Lisp_Object args[3];
9633 Lisp_Object msg, fmt;
9634 char *buffer;
9635 ptrdiff_t len;
9636 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9637 USE_SAFE_ALLOCA;
9638
9639 fmt = msg = Qnil;
9640 GCPRO4 (fmt, msg, arg1, arg2);
9641
9642 args[0] = fmt = build_string (format);
9643 args[1] = arg1;
9644 args[2] = arg2;
9645 msg = Fformat (3, args);
9646
9647 len = SBYTES (msg) + 1;
9648 buffer = SAFE_ALLOCA (len);
9649 memcpy (buffer, SDATA (msg), len);
9650
9651 message_dolog (buffer, len - 1, 1, 0);
9652 SAFE_FREE ();
9653
9654 UNGCPRO;
9655 }
9656
9657
9658 /* Output a newline in the *Messages* buffer if "needs" one. */
9659
9660 void
9661 message_log_maybe_newline (void)
9662 {
9663 if (message_log_need_newline)
9664 message_dolog ("", 0, 1, 0);
9665 }
9666
9667
9668 /* Add a string M of length NBYTES to the message log, optionally
9669 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9670 true, means interpret the contents of M as multibyte. This
9671 function calls low-level routines in order to bypass text property
9672 hooks, etc. which might not be safe to run.
9673
9674 This may GC (insert may run before/after change hooks),
9675 so the buffer M must NOT point to a Lisp string. */
9676
9677 void
9678 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9679 {
9680 const unsigned char *msg = (const unsigned char *) m;
9681
9682 if (!NILP (Vmemory_full))
9683 return;
9684
9685 if (!NILP (Vmessage_log_max))
9686 {
9687 struct buffer *oldbuf;
9688 Lisp_Object oldpoint, oldbegv, oldzv;
9689 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9690 ptrdiff_t point_at_end = 0;
9691 ptrdiff_t zv_at_end = 0;
9692 Lisp_Object old_deactivate_mark;
9693 struct gcpro gcpro1;
9694
9695 old_deactivate_mark = Vdeactivate_mark;
9696 oldbuf = current_buffer;
9697
9698 /* Ensure the Messages buffer exists, and switch to it.
9699 If we created it, set the major-mode. */
9700 {
9701 int newbuffer = 0;
9702 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9703
9704 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9705
9706 if (newbuffer
9707 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9708 call0 (intern ("messages-buffer-mode"));
9709 }
9710
9711 bset_undo_list (current_buffer, Qt);
9712 bset_cache_long_scans (current_buffer, Qnil);
9713
9714 oldpoint = message_dolog_marker1;
9715 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9716 oldbegv = message_dolog_marker2;
9717 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9718 oldzv = message_dolog_marker3;
9719 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9720 GCPRO1 (old_deactivate_mark);
9721
9722 if (PT == Z)
9723 point_at_end = 1;
9724 if (ZV == Z)
9725 zv_at_end = 1;
9726
9727 BEGV = BEG;
9728 BEGV_BYTE = BEG_BYTE;
9729 ZV = Z;
9730 ZV_BYTE = Z_BYTE;
9731 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9732
9733 /* Insert the string--maybe converting multibyte to single byte
9734 or vice versa, so that all the text fits the buffer. */
9735 if (multibyte
9736 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9737 {
9738 ptrdiff_t i;
9739 int c, char_bytes;
9740 char work[1];
9741
9742 /* Convert a multibyte string to single-byte
9743 for the *Message* buffer. */
9744 for (i = 0; i < nbytes; i += char_bytes)
9745 {
9746 c = string_char_and_length (msg + i, &char_bytes);
9747 work[0] = (ASCII_CHAR_P (c)
9748 ? c
9749 : multibyte_char_to_unibyte (c));
9750 insert_1_both (work, 1, 1, 1, 0, 0);
9751 }
9752 }
9753 else if (! multibyte
9754 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9755 {
9756 ptrdiff_t i;
9757 int c, char_bytes;
9758 unsigned char str[MAX_MULTIBYTE_LENGTH];
9759 /* Convert a single-byte string to multibyte
9760 for the *Message* buffer. */
9761 for (i = 0; i < nbytes; i++)
9762 {
9763 c = msg[i];
9764 MAKE_CHAR_MULTIBYTE (c);
9765 char_bytes = CHAR_STRING (c, str);
9766 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9767 }
9768 }
9769 else if (nbytes)
9770 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9771
9772 if (nlflag)
9773 {
9774 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9775 printmax_t dups;
9776
9777 insert_1_both ("\n", 1, 1, 1, 0, 0);
9778
9779 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9780 this_bol = PT;
9781 this_bol_byte = PT_BYTE;
9782
9783 /* See if this line duplicates the previous one.
9784 If so, combine duplicates. */
9785 if (this_bol > BEG)
9786 {
9787 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9788 prev_bol = PT;
9789 prev_bol_byte = PT_BYTE;
9790
9791 dups = message_log_check_duplicate (prev_bol_byte,
9792 this_bol_byte);
9793 if (dups)
9794 {
9795 del_range_both (prev_bol, prev_bol_byte,
9796 this_bol, this_bol_byte, 0);
9797 if (dups > 1)
9798 {
9799 char dupstr[sizeof " [ times]"
9800 + INT_STRLEN_BOUND (printmax_t)];
9801
9802 /* If you change this format, don't forget to also
9803 change message_log_check_duplicate. */
9804 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9805 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9806 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9807 }
9808 }
9809 }
9810
9811 /* If we have more than the desired maximum number of lines
9812 in the *Messages* buffer now, delete the oldest ones.
9813 This is safe because we don't have undo in this buffer. */
9814
9815 if (NATNUMP (Vmessage_log_max))
9816 {
9817 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9818 -XFASTINT (Vmessage_log_max) - 1, 0);
9819 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9820 }
9821 }
9822 BEGV = marker_position (oldbegv);
9823 BEGV_BYTE = marker_byte_position (oldbegv);
9824
9825 if (zv_at_end)
9826 {
9827 ZV = Z;
9828 ZV_BYTE = Z_BYTE;
9829 }
9830 else
9831 {
9832 ZV = marker_position (oldzv);
9833 ZV_BYTE = marker_byte_position (oldzv);
9834 }
9835
9836 if (point_at_end)
9837 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9838 else
9839 /* We can't do Fgoto_char (oldpoint) because it will run some
9840 Lisp code. */
9841 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9842 marker_byte_position (oldpoint));
9843
9844 UNGCPRO;
9845 unchain_marker (XMARKER (oldpoint));
9846 unchain_marker (XMARKER (oldbegv));
9847 unchain_marker (XMARKER (oldzv));
9848
9849 /* We called insert_1_both above with its 5th argument (PREPARE)
9850 zero, which prevents insert_1_both from calling
9851 prepare_to_modify_buffer, which in turns prevents us from
9852 incrementing windows_or_buffers_changed even if *Messages* is
9853 shown in some window. So we must manually set
9854 windows_or_buffers_changed here to make up for that. */
9855 windows_or_buffers_changed = old_windows_or_buffers_changed;
9856 bset_redisplay (current_buffer);
9857
9858 set_buffer_internal (oldbuf);
9859
9860 message_log_need_newline = !nlflag;
9861 Vdeactivate_mark = old_deactivate_mark;
9862 }
9863 }
9864
9865
9866 /* We are at the end of the buffer after just having inserted a newline.
9867 (Note: We depend on the fact we won't be crossing the gap.)
9868 Check to see if the most recent message looks a lot like the previous one.
9869 Return 0 if different, 1 if the new one should just replace it, or a
9870 value N > 1 if we should also append " [N times]". */
9871
9872 static intmax_t
9873 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9874 {
9875 ptrdiff_t i;
9876 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9877 int seen_dots = 0;
9878 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9879 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9880
9881 for (i = 0; i < len; i++)
9882 {
9883 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9884 seen_dots = 1;
9885 if (p1[i] != p2[i])
9886 return seen_dots;
9887 }
9888 p1 += len;
9889 if (*p1 == '\n')
9890 return 2;
9891 if (*p1++ == ' ' && *p1++ == '[')
9892 {
9893 char *pend;
9894 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9895 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9896 return n + 1;
9897 }
9898 return 0;
9899 }
9900 \f
9901
9902 /* Display an echo area message M with a specified length of NBYTES
9903 bytes. The string may include null characters. If M is not a
9904 string, clear out any existing message, and let the mini-buffer
9905 text show through.
9906
9907 This function cancels echoing. */
9908
9909 void
9910 message3 (Lisp_Object m)
9911 {
9912 struct gcpro gcpro1;
9913
9914 GCPRO1 (m);
9915 clear_message (true, true);
9916 cancel_echoing ();
9917
9918 /* First flush out any partial line written with print. */
9919 message_log_maybe_newline ();
9920 if (STRINGP (m))
9921 {
9922 ptrdiff_t nbytes = SBYTES (m);
9923 bool multibyte = STRING_MULTIBYTE (m);
9924 USE_SAFE_ALLOCA;
9925 char *buffer = SAFE_ALLOCA (nbytes);
9926 memcpy (buffer, SDATA (m), nbytes);
9927 message_dolog (buffer, nbytes, 1, multibyte);
9928 SAFE_FREE ();
9929 }
9930 message3_nolog (m);
9931
9932 UNGCPRO;
9933 }
9934
9935
9936 /* The non-logging version of message3.
9937 This does not cancel echoing, because it is used for echoing.
9938 Perhaps we need to make a separate function for echoing
9939 and make this cancel echoing. */
9940
9941 void
9942 message3_nolog (Lisp_Object m)
9943 {
9944 struct frame *sf = SELECTED_FRAME ();
9945
9946 if (FRAME_INITIAL_P (sf))
9947 {
9948 if (noninteractive_need_newline)
9949 putc ('\n', stderr);
9950 noninteractive_need_newline = 0;
9951 if (STRINGP (m))
9952 {
9953 Lisp_Object s = ENCODE_SYSTEM (m);
9954
9955 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9956 }
9957 if (cursor_in_echo_area == 0)
9958 fprintf (stderr, "\n");
9959 fflush (stderr);
9960 }
9961 /* Error messages get reported properly by cmd_error, so this must be just an
9962 informative message; if the frame hasn't really been initialized yet, just
9963 toss it. */
9964 else if (INTERACTIVE && sf->glyphs_initialized_p)
9965 {
9966 /* Get the frame containing the mini-buffer
9967 that the selected frame is using. */
9968 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9969 Lisp_Object frame = XWINDOW (mini_window)->frame;
9970 struct frame *f = XFRAME (frame);
9971
9972 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9973 Fmake_frame_visible (frame);
9974
9975 if (STRINGP (m) && SCHARS (m) > 0)
9976 {
9977 set_message (m);
9978 if (minibuffer_auto_raise)
9979 Fraise_frame (frame);
9980 /* Assume we are not echoing.
9981 (If we are, echo_now will override this.) */
9982 echo_message_buffer = Qnil;
9983 }
9984 else
9985 clear_message (true, true);
9986
9987 do_pending_window_change (0);
9988 echo_area_display (1);
9989 do_pending_window_change (0);
9990 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9991 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9992 }
9993 }
9994
9995
9996 /* Display a null-terminated echo area message M. If M is 0, clear
9997 out any existing message, and let the mini-buffer text show through.
9998
9999 The buffer M must continue to exist until after the echo area gets
10000 cleared or some other message gets displayed there. Do not pass
10001 text that is stored in a Lisp string. Do not pass text in a buffer
10002 that was alloca'd. */
10003
10004 void
10005 message1 (const char *m)
10006 {
10007 message3 (m ? build_unibyte_string (m) : Qnil);
10008 }
10009
10010
10011 /* The non-logging counterpart of message1. */
10012
10013 void
10014 message1_nolog (const char *m)
10015 {
10016 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10017 }
10018
10019 /* Display a message M which contains a single %s
10020 which gets replaced with STRING. */
10021
10022 void
10023 message_with_string (const char *m, Lisp_Object string, int log)
10024 {
10025 CHECK_STRING (string);
10026
10027 if (noninteractive)
10028 {
10029 if (m)
10030 {
10031 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10032 String whose data pointer might be passed to us in M. So
10033 we use a local copy. */
10034 char *fmt = xstrdup (m);
10035
10036 if (noninteractive_need_newline)
10037 putc ('\n', stderr);
10038 noninteractive_need_newline = 0;
10039 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10040 if (!cursor_in_echo_area)
10041 fprintf (stderr, "\n");
10042 fflush (stderr);
10043 xfree (fmt);
10044 }
10045 }
10046 else if (INTERACTIVE)
10047 {
10048 /* The frame whose minibuffer we're going to display the message on.
10049 It may be larger than the selected frame, so we need
10050 to use its buffer, not the selected frame's buffer. */
10051 Lisp_Object mini_window;
10052 struct frame *f, *sf = SELECTED_FRAME ();
10053
10054 /* Get the frame containing the minibuffer
10055 that the selected frame is using. */
10056 mini_window = FRAME_MINIBUF_WINDOW (sf);
10057 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10058
10059 /* Error messages get reported properly by cmd_error, so this must be
10060 just an informative message; if the frame hasn't really been
10061 initialized yet, just toss it. */
10062 if (f->glyphs_initialized_p)
10063 {
10064 Lisp_Object args[2], msg;
10065 struct gcpro gcpro1, gcpro2;
10066
10067 args[0] = build_string (m);
10068 args[1] = msg = string;
10069 GCPRO2 (args[0], msg);
10070 gcpro1.nvars = 2;
10071
10072 msg = Fformat (2, args);
10073
10074 if (log)
10075 message3 (msg);
10076 else
10077 message3_nolog (msg);
10078
10079 UNGCPRO;
10080
10081 /* Print should start at the beginning of the message
10082 buffer next time. */
10083 message_buf_print = 0;
10084 }
10085 }
10086 }
10087
10088
10089 /* Dump an informative message to the minibuf. If M is 0, clear out
10090 any existing message, and let the mini-buffer text show through. */
10091
10092 static void
10093 vmessage (const char *m, va_list ap)
10094 {
10095 if (noninteractive)
10096 {
10097 if (m)
10098 {
10099 if (noninteractive_need_newline)
10100 putc ('\n', stderr);
10101 noninteractive_need_newline = 0;
10102 vfprintf (stderr, m, ap);
10103 if (cursor_in_echo_area == 0)
10104 fprintf (stderr, "\n");
10105 fflush (stderr);
10106 }
10107 }
10108 else if (INTERACTIVE)
10109 {
10110 /* The frame whose mini-buffer we're going to display the message
10111 on. It may be larger than the selected frame, so we need to
10112 use its buffer, not the selected frame's buffer. */
10113 Lisp_Object mini_window;
10114 struct frame *f, *sf = SELECTED_FRAME ();
10115
10116 /* Get the frame containing the mini-buffer
10117 that the selected frame is using. */
10118 mini_window = FRAME_MINIBUF_WINDOW (sf);
10119 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10120
10121 /* Error messages get reported properly by cmd_error, so this must be
10122 just an informative message; if the frame hasn't really been
10123 initialized yet, just toss it. */
10124 if (f->glyphs_initialized_p)
10125 {
10126 if (m)
10127 {
10128 ptrdiff_t len;
10129 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10130 char *message_buf = alloca (maxsize + 1);
10131
10132 len = doprnt (message_buf, maxsize, m, 0, ap);
10133
10134 message3 (make_string (message_buf, len));
10135 }
10136 else
10137 message1 (0);
10138
10139 /* Print should start at the beginning of the message
10140 buffer next time. */
10141 message_buf_print = 0;
10142 }
10143 }
10144 }
10145
10146 void
10147 message (const char *m, ...)
10148 {
10149 va_list ap;
10150 va_start (ap, m);
10151 vmessage (m, ap);
10152 va_end (ap);
10153 }
10154
10155
10156 #if 0
10157 /* The non-logging version of message. */
10158
10159 void
10160 message_nolog (const char *m, ...)
10161 {
10162 Lisp_Object old_log_max;
10163 va_list ap;
10164 va_start (ap, m);
10165 old_log_max = Vmessage_log_max;
10166 Vmessage_log_max = Qnil;
10167 vmessage (m, ap);
10168 Vmessage_log_max = old_log_max;
10169 va_end (ap);
10170 }
10171 #endif
10172
10173
10174 /* Display the current message in the current mini-buffer. This is
10175 only called from error handlers in process.c, and is not time
10176 critical. */
10177
10178 void
10179 update_echo_area (void)
10180 {
10181 if (!NILP (echo_area_buffer[0]))
10182 {
10183 Lisp_Object string;
10184 string = Fcurrent_message ();
10185 message3 (string);
10186 }
10187 }
10188
10189
10190 /* Make sure echo area buffers in `echo_buffers' are live.
10191 If they aren't, make new ones. */
10192
10193 static void
10194 ensure_echo_area_buffers (void)
10195 {
10196 int i;
10197
10198 for (i = 0; i < 2; ++i)
10199 if (!BUFFERP (echo_buffer[i])
10200 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10201 {
10202 char name[30];
10203 Lisp_Object old_buffer;
10204 int j;
10205
10206 old_buffer = echo_buffer[i];
10207 echo_buffer[i] = Fget_buffer_create
10208 (make_formatted_string (name, " *Echo Area %d*", i));
10209 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10210 /* to force word wrap in echo area -
10211 it was decided to postpone this*/
10212 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10213
10214 for (j = 0; j < 2; ++j)
10215 if (EQ (old_buffer, echo_area_buffer[j]))
10216 echo_area_buffer[j] = echo_buffer[i];
10217 }
10218 }
10219
10220
10221 /* Call FN with args A1..A2 with either the current or last displayed
10222 echo_area_buffer as current buffer.
10223
10224 WHICH zero means use the current message buffer
10225 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10226 from echo_buffer[] and clear it.
10227
10228 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10229 suitable buffer from echo_buffer[] and clear it.
10230
10231 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10232 that the current message becomes the last displayed one, make
10233 choose a suitable buffer for echo_area_buffer[0], and clear it.
10234
10235 Value is what FN returns. */
10236
10237 static int
10238 with_echo_area_buffer (struct window *w, int which,
10239 int (*fn) (ptrdiff_t, Lisp_Object),
10240 ptrdiff_t a1, Lisp_Object a2)
10241 {
10242 Lisp_Object buffer;
10243 int this_one, the_other, clear_buffer_p, rc;
10244 ptrdiff_t count = SPECPDL_INDEX ();
10245
10246 /* If buffers aren't live, make new ones. */
10247 ensure_echo_area_buffers ();
10248
10249 clear_buffer_p = 0;
10250
10251 if (which == 0)
10252 this_one = 0, the_other = 1;
10253 else if (which > 0)
10254 this_one = 1, the_other = 0;
10255 else
10256 {
10257 this_one = 0, the_other = 1;
10258 clear_buffer_p = true;
10259
10260 /* We need a fresh one in case the current echo buffer equals
10261 the one containing the last displayed echo area message. */
10262 if (!NILP (echo_area_buffer[this_one])
10263 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10264 echo_area_buffer[this_one] = Qnil;
10265 }
10266
10267 /* Choose a suitable buffer from echo_buffer[] is we don't
10268 have one. */
10269 if (NILP (echo_area_buffer[this_one]))
10270 {
10271 echo_area_buffer[this_one]
10272 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10273 ? echo_buffer[the_other]
10274 : echo_buffer[this_one]);
10275 clear_buffer_p = true;
10276 }
10277
10278 buffer = echo_area_buffer[this_one];
10279
10280 /* Don't get confused by reusing the buffer used for echoing
10281 for a different purpose. */
10282 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10283 cancel_echoing ();
10284
10285 record_unwind_protect (unwind_with_echo_area_buffer,
10286 with_echo_area_buffer_unwind_data (w));
10287
10288 /* Make the echo area buffer current. Note that for display
10289 purposes, it is not necessary that the displayed window's buffer
10290 == current_buffer, except for text property lookup. So, let's
10291 only set that buffer temporarily here without doing a full
10292 Fset_window_buffer. We must also change w->pointm, though,
10293 because otherwise an assertions in unshow_buffer fails, and Emacs
10294 aborts. */
10295 set_buffer_internal_1 (XBUFFER (buffer));
10296 if (w)
10297 {
10298 wset_buffer (w, buffer);
10299 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10300 }
10301
10302 bset_undo_list (current_buffer, Qt);
10303 bset_read_only (current_buffer, Qnil);
10304 specbind (Qinhibit_read_only, Qt);
10305 specbind (Qinhibit_modification_hooks, Qt);
10306
10307 if (clear_buffer_p && Z > BEG)
10308 del_range (BEG, Z);
10309
10310 eassert (BEGV >= BEG);
10311 eassert (ZV <= Z && ZV >= BEGV);
10312
10313 rc = fn (a1, a2);
10314
10315 eassert (BEGV >= BEG);
10316 eassert (ZV <= Z && ZV >= BEGV);
10317
10318 unbind_to (count, Qnil);
10319 return rc;
10320 }
10321
10322
10323 /* Save state that should be preserved around the call to the function
10324 FN called in with_echo_area_buffer. */
10325
10326 static Lisp_Object
10327 with_echo_area_buffer_unwind_data (struct window *w)
10328 {
10329 int i = 0;
10330 Lisp_Object vector, tmp;
10331
10332 /* Reduce consing by keeping one vector in
10333 Vwith_echo_area_save_vector. */
10334 vector = Vwith_echo_area_save_vector;
10335 Vwith_echo_area_save_vector = Qnil;
10336
10337 if (NILP (vector))
10338 vector = Fmake_vector (make_number (9), Qnil);
10339
10340 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10341 ASET (vector, i, Vdeactivate_mark); ++i;
10342 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10343
10344 if (w)
10345 {
10346 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10347 ASET (vector, i, w->contents); ++i;
10348 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10349 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10350 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10351 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10352 }
10353 else
10354 {
10355 int end = i + 6;
10356 for (; i < end; ++i)
10357 ASET (vector, i, Qnil);
10358 }
10359
10360 eassert (i == ASIZE (vector));
10361 return vector;
10362 }
10363
10364
10365 /* Restore global state from VECTOR which was created by
10366 with_echo_area_buffer_unwind_data. */
10367
10368 static void
10369 unwind_with_echo_area_buffer (Lisp_Object vector)
10370 {
10371 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10372 Vdeactivate_mark = AREF (vector, 1);
10373 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10374
10375 if (WINDOWP (AREF (vector, 3)))
10376 {
10377 struct window *w;
10378 Lisp_Object buffer;
10379
10380 w = XWINDOW (AREF (vector, 3));
10381 buffer = AREF (vector, 4);
10382
10383 wset_buffer (w, buffer);
10384 set_marker_both (w->pointm, buffer,
10385 XFASTINT (AREF (vector, 5)),
10386 XFASTINT (AREF (vector, 6)));
10387 set_marker_both (w->start, buffer,
10388 XFASTINT (AREF (vector, 7)),
10389 XFASTINT (AREF (vector, 8)));
10390 }
10391
10392 Vwith_echo_area_save_vector = vector;
10393 }
10394
10395
10396 /* Set up the echo area for use by print functions. MULTIBYTE_P
10397 non-zero means we will print multibyte. */
10398
10399 void
10400 setup_echo_area_for_printing (int multibyte_p)
10401 {
10402 /* If we can't find an echo area any more, exit. */
10403 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10404 Fkill_emacs (Qnil);
10405
10406 ensure_echo_area_buffers ();
10407
10408 if (!message_buf_print)
10409 {
10410 /* A message has been output since the last time we printed.
10411 Choose a fresh echo area buffer. */
10412 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10413 echo_area_buffer[0] = echo_buffer[1];
10414 else
10415 echo_area_buffer[0] = echo_buffer[0];
10416
10417 /* Switch to that buffer and clear it. */
10418 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10419 bset_truncate_lines (current_buffer, Qnil);
10420
10421 if (Z > BEG)
10422 {
10423 ptrdiff_t count = SPECPDL_INDEX ();
10424 specbind (Qinhibit_read_only, Qt);
10425 /* Note that undo recording is always disabled. */
10426 del_range (BEG, Z);
10427 unbind_to (count, Qnil);
10428 }
10429 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10430
10431 /* Set up the buffer for the multibyteness we need. */
10432 if (multibyte_p
10433 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10434 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10435
10436 /* Raise the frame containing the echo area. */
10437 if (minibuffer_auto_raise)
10438 {
10439 struct frame *sf = SELECTED_FRAME ();
10440 Lisp_Object mini_window;
10441 mini_window = FRAME_MINIBUF_WINDOW (sf);
10442 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10443 }
10444
10445 message_log_maybe_newline ();
10446 message_buf_print = 1;
10447 }
10448 else
10449 {
10450 if (NILP (echo_area_buffer[0]))
10451 {
10452 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10453 echo_area_buffer[0] = echo_buffer[1];
10454 else
10455 echo_area_buffer[0] = echo_buffer[0];
10456 }
10457
10458 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10459 {
10460 /* Someone switched buffers between print requests. */
10461 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10462 bset_truncate_lines (current_buffer, Qnil);
10463 }
10464 }
10465 }
10466
10467
10468 /* Display an echo area message in window W. Value is non-zero if W's
10469 height is changed. If display_last_displayed_message_p is
10470 non-zero, display the message that was last displayed, otherwise
10471 display the current message. */
10472
10473 static int
10474 display_echo_area (struct window *w)
10475 {
10476 int i, no_message_p, window_height_changed_p;
10477
10478 /* Temporarily disable garbage collections while displaying the echo
10479 area. This is done because a GC can print a message itself.
10480 That message would modify the echo area buffer's contents while a
10481 redisplay of the buffer is going on, and seriously confuse
10482 redisplay. */
10483 ptrdiff_t count = inhibit_garbage_collection ();
10484
10485 /* If there is no message, we must call display_echo_area_1
10486 nevertheless because it resizes the window. But we will have to
10487 reset the echo_area_buffer in question to nil at the end because
10488 with_echo_area_buffer will sets it to an empty buffer. */
10489 i = display_last_displayed_message_p ? 1 : 0;
10490 no_message_p = NILP (echo_area_buffer[i]);
10491
10492 window_height_changed_p
10493 = with_echo_area_buffer (w, display_last_displayed_message_p,
10494 display_echo_area_1,
10495 (intptr_t) w, Qnil);
10496
10497 if (no_message_p)
10498 echo_area_buffer[i] = Qnil;
10499
10500 unbind_to (count, Qnil);
10501 return window_height_changed_p;
10502 }
10503
10504
10505 /* Helper for display_echo_area. Display the current buffer which
10506 contains the current echo area message in window W, a mini-window,
10507 a pointer to which is passed in A1. A2..A4 are currently not used.
10508 Change the height of W so that all of the message is displayed.
10509 Value is non-zero if height of W was changed. */
10510
10511 static int
10512 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10513 {
10514 intptr_t i1 = a1;
10515 struct window *w = (struct window *) i1;
10516 Lisp_Object window;
10517 struct text_pos start;
10518 int window_height_changed_p = 0;
10519
10520 /* Do this before displaying, so that we have a large enough glyph
10521 matrix for the display. If we can't get enough space for the
10522 whole text, display the last N lines. That works by setting w->start. */
10523 window_height_changed_p = resize_mini_window (w, 0);
10524
10525 /* Use the starting position chosen by resize_mini_window. */
10526 SET_TEXT_POS_FROM_MARKER (start, w->start);
10527
10528 /* Display. */
10529 clear_glyph_matrix (w->desired_matrix);
10530 XSETWINDOW (window, w);
10531 try_window (window, start, 0);
10532
10533 return window_height_changed_p;
10534 }
10535
10536
10537 /* Resize the echo area window to exactly the size needed for the
10538 currently displayed message, if there is one. If a mini-buffer
10539 is active, don't shrink it. */
10540
10541 void
10542 resize_echo_area_exactly (void)
10543 {
10544 if (BUFFERP (echo_area_buffer[0])
10545 && WINDOWP (echo_area_window))
10546 {
10547 struct window *w = XWINDOW (echo_area_window);
10548 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10549 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10550 (intptr_t) w, resize_exactly);
10551 if (resized_p)
10552 {
10553 windows_or_buffers_changed = 42;
10554 update_mode_lines = 30;
10555 redisplay_internal ();
10556 }
10557 }
10558 }
10559
10560
10561 /* Callback function for with_echo_area_buffer, when used from
10562 resize_echo_area_exactly. A1 contains a pointer to the window to
10563 resize, EXACTLY non-nil means resize the mini-window exactly to the
10564 size of the text displayed. A3 and A4 are not used. Value is what
10565 resize_mini_window returns. */
10566
10567 static int
10568 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10569 {
10570 intptr_t i1 = a1;
10571 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10572 }
10573
10574
10575 /* Resize mini-window W to fit the size of its contents. EXACT_P
10576 means size the window exactly to the size needed. Otherwise, it's
10577 only enlarged until W's buffer is empty.
10578
10579 Set W->start to the right place to begin display. If the whole
10580 contents fit, start at the beginning. Otherwise, start so as
10581 to make the end of the contents appear. This is particularly
10582 important for y-or-n-p, but seems desirable generally.
10583
10584 Value is non-zero if the window height has been changed. */
10585
10586 int
10587 resize_mini_window (struct window *w, int exact_p)
10588 {
10589 struct frame *f = XFRAME (w->frame);
10590 int window_height_changed_p = 0;
10591
10592 eassert (MINI_WINDOW_P (w));
10593
10594 /* By default, start display at the beginning. */
10595 set_marker_both (w->start, w->contents,
10596 BUF_BEGV (XBUFFER (w->contents)),
10597 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10598
10599 /* Don't resize windows while redisplaying a window; it would
10600 confuse redisplay functions when the size of the window they are
10601 displaying changes from under them. Such a resizing can happen,
10602 for instance, when which-func prints a long message while
10603 we are running fontification-functions. We're running these
10604 functions with safe_call which binds inhibit-redisplay to t. */
10605 if (!NILP (Vinhibit_redisplay))
10606 return 0;
10607
10608 /* Nil means don't try to resize. */
10609 if (NILP (Vresize_mini_windows)
10610 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10611 return 0;
10612
10613 if (!FRAME_MINIBUF_ONLY_P (f))
10614 {
10615 struct it it;
10616 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10617 + WINDOW_PIXEL_HEIGHT (w));
10618 int unit = FRAME_LINE_HEIGHT (f);
10619 int height, max_height;
10620 struct text_pos start;
10621 struct buffer *old_current_buffer = NULL;
10622
10623 if (current_buffer != XBUFFER (w->contents))
10624 {
10625 old_current_buffer = current_buffer;
10626 set_buffer_internal (XBUFFER (w->contents));
10627 }
10628
10629 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10630
10631 /* Compute the max. number of lines specified by the user. */
10632 if (FLOATP (Vmax_mini_window_height))
10633 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10634 else if (INTEGERP (Vmax_mini_window_height))
10635 max_height = XINT (Vmax_mini_window_height) * unit;
10636 else
10637 max_height = total_height / 4;
10638
10639 /* Correct that max. height if it's bogus. */
10640 max_height = clip_to_bounds (unit, max_height, total_height);
10641
10642 /* Find out the height of the text in the window. */
10643 if (it.line_wrap == TRUNCATE)
10644 height = unit;
10645 else
10646 {
10647 last_height = 0;
10648 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10649 if (it.max_ascent == 0 && it.max_descent == 0)
10650 height = it.current_y + last_height;
10651 else
10652 height = it.current_y + it.max_ascent + it.max_descent;
10653 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10654 }
10655
10656 /* Compute a suitable window start. */
10657 if (height > max_height)
10658 {
10659 height = max_height;
10660 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10661 move_it_vertically_backward (&it, height);
10662 start = it.current.pos;
10663 }
10664 else
10665 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10666 SET_MARKER_FROM_TEXT_POS (w->start, start);
10667
10668 if (EQ (Vresize_mini_windows, Qgrow_only))
10669 {
10670 /* Let it grow only, until we display an empty message, in which
10671 case the window shrinks again. */
10672 if (height > WINDOW_PIXEL_HEIGHT (w))
10673 {
10674 int old_height = WINDOW_PIXEL_HEIGHT (w);
10675
10676 FRAME_WINDOWS_FROZEN (f) = 1;
10677 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10678 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10679 }
10680 else if (height < WINDOW_PIXEL_HEIGHT (w)
10681 && (exact_p || BEGV == ZV))
10682 {
10683 int old_height = WINDOW_PIXEL_HEIGHT (w);
10684
10685 FRAME_WINDOWS_FROZEN (f) = 0;
10686 shrink_mini_window (w, 1);
10687 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10688 }
10689 }
10690 else
10691 {
10692 /* Always resize to exact size needed. */
10693 if (height > WINDOW_PIXEL_HEIGHT (w))
10694 {
10695 int old_height = WINDOW_PIXEL_HEIGHT (w);
10696
10697 FRAME_WINDOWS_FROZEN (f) = 1;
10698 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10699 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10700 }
10701 else if (height < WINDOW_PIXEL_HEIGHT (w))
10702 {
10703 int old_height = WINDOW_PIXEL_HEIGHT (w);
10704
10705 FRAME_WINDOWS_FROZEN (f) = 0;
10706 shrink_mini_window (w, 1);
10707
10708 if (height)
10709 {
10710 FRAME_WINDOWS_FROZEN (f) = 1;
10711 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10712 }
10713
10714 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10715 }
10716 }
10717
10718 if (old_current_buffer)
10719 set_buffer_internal (old_current_buffer);
10720 }
10721
10722 return window_height_changed_p;
10723 }
10724
10725
10726 /* Value is the current message, a string, or nil if there is no
10727 current message. */
10728
10729 Lisp_Object
10730 current_message (void)
10731 {
10732 Lisp_Object msg;
10733
10734 if (!BUFFERP (echo_area_buffer[0]))
10735 msg = Qnil;
10736 else
10737 {
10738 with_echo_area_buffer (0, 0, current_message_1,
10739 (intptr_t) &msg, Qnil);
10740 if (NILP (msg))
10741 echo_area_buffer[0] = Qnil;
10742 }
10743
10744 return msg;
10745 }
10746
10747
10748 static int
10749 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10750 {
10751 intptr_t i1 = a1;
10752 Lisp_Object *msg = (Lisp_Object *) i1;
10753
10754 if (Z > BEG)
10755 *msg = make_buffer_string (BEG, Z, 1);
10756 else
10757 *msg = Qnil;
10758 return 0;
10759 }
10760
10761
10762 /* Push the current message on Vmessage_stack for later restoration
10763 by restore_message. Value is non-zero if the current message isn't
10764 empty. This is a relatively infrequent operation, so it's not
10765 worth optimizing. */
10766
10767 bool
10768 push_message (void)
10769 {
10770 Lisp_Object msg = current_message ();
10771 Vmessage_stack = Fcons (msg, Vmessage_stack);
10772 return STRINGP (msg);
10773 }
10774
10775
10776 /* Restore message display from the top of Vmessage_stack. */
10777
10778 void
10779 restore_message (void)
10780 {
10781 eassert (CONSP (Vmessage_stack));
10782 message3_nolog (XCAR (Vmessage_stack));
10783 }
10784
10785
10786 /* Handler for unwind-protect calling pop_message. */
10787
10788 void
10789 pop_message_unwind (void)
10790 {
10791 /* Pop the top-most entry off Vmessage_stack. */
10792 eassert (CONSP (Vmessage_stack));
10793 Vmessage_stack = XCDR (Vmessage_stack);
10794 }
10795
10796
10797 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10798 exits. If the stack is not empty, we have a missing pop_message
10799 somewhere. */
10800
10801 void
10802 check_message_stack (void)
10803 {
10804 if (!NILP (Vmessage_stack))
10805 emacs_abort ();
10806 }
10807
10808
10809 /* Truncate to NCHARS what will be displayed in the echo area the next
10810 time we display it---but don't redisplay it now. */
10811
10812 void
10813 truncate_echo_area (ptrdiff_t nchars)
10814 {
10815 if (nchars == 0)
10816 echo_area_buffer[0] = Qnil;
10817 else if (!noninteractive
10818 && INTERACTIVE
10819 && !NILP (echo_area_buffer[0]))
10820 {
10821 struct frame *sf = SELECTED_FRAME ();
10822 /* Error messages get reported properly by cmd_error, so this must be
10823 just an informative message; if the frame hasn't really been
10824 initialized yet, just toss it. */
10825 if (sf->glyphs_initialized_p)
10826 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10827 }
10828 }
10829
10830
10831 /* Helper function for truncate_echo_area. Truncate the current
10832 message to at most NCHARS characters. */
10833
10834 static int
10835 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10836 {
10837 if (BEG + nchars < Z)
10838 del_range (BEG + nchars, Z);
10839 if (Z == BEG)
10840 echo_area_buffer[0] = Qnil;
10841 return 0;
10842 }
10843
10844 /* Set the current message to STRING. */
10845
10846 static void
10847 set_message (Lisp_Object string)
10848 {
10849 eassert (STRINGP (string));
10850
10851 message_enable_multibyte = STRING_MULTIBYTE (string);
10852
10853 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10854 message_buf_print = 0;
10855 help_echo_showing_p = 0;
10856
10857 if (STRINGP (Vdebug_on_message)
10858 && STRINGP (string)
10859 && fast_string_match (Vdebug_on_message, string) >= 0)
10860 call_debugger (list2 (Qerror, string));
10861 }
10862
10863
10864 /* Helper function for set_message. First argument is ignored and second
10865 argument has the same meaning as for set_message.
10866 This function is called with the echo area buffer being current. */
10867
10868 static int
10869 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10870 {
10871 eassert (STRINGP (string));
10872
10873 /* Change multibyteness of the echo buffer appropriately. */
10874 if (message_enable_multibyte
10875 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10876 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10877
10878 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10879 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10880 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10881
10882 /* Insert new message at BEG. */
10883 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10884
10885 /* This function takes care of single/multibyte conversion.
10886 We just have to ensure that the echo area buffer has the right
10887 setting of enable_multibyte_characters. */
10888 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10889
10890 return 0;
10891 }
10892
10893
10894 /* Clear messages. CURRENT_P non-zero means clear the current
10895 message. LAST_DISPLAYED_P non-zero means clear the message
10896 last displayed. */
10897
10898 void
10899 clear_message (bool current_p, bool last_displayed_p)
10900 {
10901 if (current_p)
10902 {
10903 echo_area_buffer[0] = Qnil;
10904 message_cleared_p = true;
10905 }
10906
10907 if (last_displayed_p)
10908 echo_area_buffer[1] = Qnil;
10909
10910 message_buf_print = 0;
10911 }
10912
10913 /* Clear garbaged frames.
10914
10915 This function is used where the old redisplay called
10916 redraw_garbaged_frames which in turn called redraw_frame which in
10917 turn called clear_frame. The call to clear_frame was a source of
10918 flickering. I believe a clear_frame is not necessary. It should
10919 suffice in the new redisplay to invalidate all current matrices,
10920 and ensure a complete redisplay of all windows. */
10921
10922 static void
10923 clear_garbaged_frames (void)
10924 {
10925 if (frame_garbaged)
10926 {
10927 Lisp_Object tail, frame;
10928
10929 FOR_EACH_FRAME (tail, frame)
10930 {
10931 struct frame *f = XFRAME (frame);
10932
10933 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10934 {
10935 if (f->resized_p)
10936 redraw_frame (f);
10937 else
10938 clear_current_matrices (f);
10939 fset_redisplay (f);
10940 f->garbaged = false;
10941 f->resized_p = false;
10942 }
10943 }
10944
10945 frame_garbaged = false;
10946 }
10947 }
10948
10949
10950 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10951 is non-zero update selected_frame. Value is non-zero if the
10952 mini-windows height has been changed. */
10953
10954 static int
10955 echo_area_display (int update_frame_p)
10956 {
10957 Lisp_Object mini_window;
10958 struct window *w;
10959 struct frame *f;
10960 int window_height_changed_p = 0;
10961 struct frame *sf = SELECTED_FRAME ();
10962
10963 mini_window = FRAME_MINIBUF_WINDOW (sf);
10964 w = XWINDOW (mini_window);
10965 f = XFRAME (WINDOW_FRAME (w));
10966
10967 /* Don't display if frame is invisible or not yet initialized. */
10968 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10969 return 0;
10970
10971 #ifdef HAVE_WINDOW_SYSTEM
10972 /* When Emacs starts, selected_frame may be the initial terminal
10973 frame. If we let this through, a message would be displayed on
10974 the terminal. */
10975 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10976 return 0;
10977 #endif /* HAVE_WINDOW_SYSTEM */
10978
10979 /* Redraw garbaged frames. */
10980 clear_garbaged_frames ();
10981
10982 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10983 {
10984 echo_area_window = mini_window;
10985 window_height_changed_p = display_echo_area (w);
10986 w->must_be_updated_p = true;
10987
10988 /* Update the display, unless called from redisplay_internal.
10989 Also don't update the screen during redisplay itself. The
10990 update will happen at the end of redisplay, and an update
10991 here could cause confusion. */
10992 if (update_frame_p && !redisplaying_p)
10993 {
10994 int n = 0;
10995
10996 /* If the display update has been interrupted by pending
10997 input, update mode lines in the frame. Due to the
10998 pending input, it might have been that redisplay hasn't
10999 been called, so that mode lines above the echo area are
11000 garbaged. This looks odd, so we prevent it here. */
11001 if (!display_completed)
11002 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11003
11004 if (window_height_changed_p
11005 /* Don't do this if Emacs is shutting down. Redisplay
11006 needs to run hooks. */
11007 && !NILP (Vrun_hooks))
11008 {
11009 /* Must update other windows. Likewise as in other
11010 cases, don't let this update be interrupted by
11011 pending input. */
11012 ptrdiff_t count = SPECPDL_INDEX ();
11013 specbind (Qredisplay_dont_pause, Qt);
11014 windows_or_buffers_changed = 44;
11015 redisplay_internal ();
11016 unbind_to (count, Qnil);
11017 }
11018 else if (FRAME_WINDOW_P (f) && n == 0)
11019 {
11020 /* Window configuration is the same as before.
11021 Can do with a display update of the echo area,
11022 unless we displayed some mode lines. */
11023 update_single_window (w, 1);
11024 flush_frame (f);
11025 }
11026 else
11027 update_frame (f, 1, 1);
11028
11029 /* If cursor is in the echo area, make sure that the next
11030 redisplay displays the minibuffer, so that the cursor will
11031 be replaced with what the minibuffer wants. */
11032 if (cursor_in_echo_area)
11033 wset_redisplay (XWINDOW (mini_window));
11034 }
11035 }
11036 else if (!EQ (mini_window, selected_window))
11037 wset_redisplay (XWINDOW (mini_window));
11038
11039 /* Last displayed message is now the current message. */
11040 echo_area_buffer[1] = echo_area_buffer[0];
11041 /* Inform read_char that we're not echoing. */
11042 echo_message_buffer = Qnil;
11043
11044 /* Prevent redisplay optimization in redisplay_internal by resetting
11045 this_line_start_pos. This is done because the mini-buffer now
11046 displays the message instead of its buffer text. */
11047 if (EQ (mini_window, selected_window))
11048 CHARPOS (this_line_start_pos) = 0;
11049
11050 return window_height_changed_p;
11051 }
11052
11053 /* Nonzero if W's buffer was changed but not saved. */
11054
11055 static int
11056 window_buffer_changed (struct window *w)
11057 {
11058 struct buffer *b = XBUFFER (w->contents);
11059
11060 eassert (BUFFER_LIVE_P (b));
11061
11062 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11063 }
11064
11065 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11066
11067 static int
11068 mode_line_update_needed (struct window *w)
11069 {
11070 return (w->column_number_displayed != -1
11071 && !(PT == w->last_point && !window_outdated (w))
11072 && (w->column_number_displayed != current_column ()));
11073 }
11074
11075 /* Nonzero if window start of W is frozen and may not be changed during
11076 redisplay. */
11077
11078 static bool
11079 window_frozen_p (struct window *w)
11080 {
11081 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11082 {
11083 Lisp_Object window;
11084
11085 XSETWINDOW (window, w);
11086 if (MINI_WINDOW_P (w))
11087 return 0;
11088 else if (EQ (window, selected_window))
11089 return 0;
11090 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11091 && EQ (window, Vminibuf_scroll_window))
11092 /* This special window can't be frozen too. */
11093 return 0;
11094 else
11095 return 1;
11096 }
11097 return 0;
11098 }
11099
11100 /***********************************************************************
11101 Mode Lines and Frame Titles
11102 ***********************************************************************/
11103
11104 /* A buffer for constructing non-propertized mode-line strings and
11105 frame titles in it; allocated from the heap in init_xdisp and
11106 resized as needed in store_mode_line_noprop_char. */
11107
11108 static char *mode_line_noprop_buf;
11109
11110 /* The buffer's end, and a current output position in it. */
11111
11112 static char *mode_line_noprop_buf_end;
11113 static char *mode_line_noprop_ptr;
11114
11115 #define MODE_LINE_NOPROP_LEN(start) \
11116 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11117
11118 static enum {
11119 MODE_LINE_DISPLAY = 0,
11120 MODE_LINE_TITLE,
11121 MODE_LINE_NOPROP,
11122 MODE_LINE_STRING
11123 } mode_line_target;
11124
11125 /* Alist that caches the results of :propertize.
11126 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11127 static Lisp_Object mode_line_proptrans_alist;
11128
11129 /* List of strings making up the mode-line. */
11130 static Lisp_Object mode_line_string_list;
11131
11132 /* Base face property when building propertized mode line string. */
11133 static Lisp_Object mode_line_string_face;
11134 static Lisp_Object mode_line_string_face_prop;
11135
11136
11137 /* Unwind data for mode line strings */
11138
11139 static Lisp_Object Vmode_line_unwind_vector;
11140
11141 static Lisp_Object
11142 format_mode_line_unwind_data (struct frame *target_frame,
11143 struct buffer *obuf,
11144 Lisp_Object owin,
11145 int save_proptrans)
11146 {
11147 Lisp_Object vector, tmp;
11148
11149 /* Reduce consing by keeping one vector in
11150 Vwith_echo_area_save_vector. */
11151 vector = Vmode_line_unwind_vector;
11152 Vmode_line_unwind_vector = Qnil;
11153
11154 if (NILP (vector))
11155 vector = Fmake_vector (make_number (10), Qnil);
11156
11157 ASET (vector, 0, make_number (mode_line_target));
11158 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11159 ASET (vector, 2, mode_line_string_list);
11160 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11161 ASET (vector, 4, mode_line_string_face);
11162 ASET (vector, 5, mode_line_string_face_prop);
11163
11164 if (obuf)
11165 XSETBUFFER (tmp, obuf);
11166 else
11167 tmp = Qnil;
11168 ASET (vector, 6, tmp);
11169 ASET (vector, 7, owin);
11170 if (target_frame)
11171 {
11172 /* Similarly to `with-selected-window', if the operation selects
11173 a window on another frame, we must restore that frame's
11174 selected window, and (for a tty) the top-frame. */
11175 ASET (vector, 8, target_frame->selected_window);
11176 if (FRAME_TERMCAP_P (target_frame))
11177 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11178 }
11179
11180 return vector;
11181 }
11182
11183 static void
11184 unwind_format_mode_line (Lisp_Object vector)
11185 {
11186 Lisp_Object old_window = AREF (vector, 7);
11187 Lisp_Object target_frame_window = AREF (vector, 8);
11188 Lisp_Object old_top_frame = AREF (vector, 9);
11189
11190 mode_line_target = XINT (AREF (vector, 0));
11191 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11192 mode_line_string_list = AREF (vector, 2);
11193 if (! EQ (AREF (vector, 3), Qt))
11194 mode_line_proptrans_alist = AREF (vector, 3);
11195 mode_line_string_face = AREF (vector, 4);
11196 mode_line_string_face_prop = AREF (vector, 5);
11197
11198 /* Select window before buffer, since it may change the buffer. */
11199 if (!NILP (old_window))
11200 {
11201 /* If the operation that we are unwinding had selected a window
11202 on a different frame, reset its frame-selected-window. For a
11203 text terminal, reset its top-frame if necessary. */
11204 if (!NILP (target_frame_window))
11205 {
11206 Lisp_Object frame
11207 = WINDOW_FRAME (XWINDOW (target_frame_window));
11208
11209 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11210 Fselect_window (target_frame_window, Qt);
11211
11212 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11213 Fselect_frame (old_top_frame, Qt);
11214 }
11215
11216 Fselect_window (old_window, Qt);
11217 }
11218
11219 if (!NILP (AREF (vector, 6)))
11220 {
11221 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11222 ASET (vector, 6, Qnil);
11223 }
11224
11225 Vmode_line_unwind_vector = vector;
11226 }
11227
11228
11229 /* Store a single character C for the frame title in mode_line_noprop_buf.
11230 Re-allocate mode_line_noprop_buf if necessary. */
11231
11232 static void
11233 store_mode_line_noprop_char (char c)
11234 {
11235 /* If output position has reached the end of the allocated buffer,
11236 increase the buffer's size. */
11237 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11238 {
11239 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11240 ptrdiff_t size = len;
11241 mode_line_noprop_buf =
11242 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11243 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11244 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11245 }
11246
11247 *mode_line_noprop_ptr++ = c;
11248 }
11249
11250
11251 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11252 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11253 characters that yield more columns than PRECISION; PRECISION <= 0
11254 means copy the whole string. Pad with spaces until FIELD_WIDTH
11255 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11256 pad. Called from display_mode_element when it is used to build a
11257 frame title. */
11258
11259 static int
11260 store_mode_line_noprop (const char *string, int field_width, int precision)
11261 {
11262 const unsigned char *str = (const unsigned char *) string;
11263 int n = 0;
11264 ptrdiff_t dummy, nbytes;
11265
11266 /* Copy at most PRECISION chars from STR. */
11267 nbytes = strlen (string);
11268 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11269 while (nbytes--)
11270 store_mode_line_noprop_char (*str++);
11271
11272 /* Fill up with spaces until FIELD_WIDTH reached. */
11273 while (field_width > 0
11274 && n < field_width)
11275 {
11276 store_mode_line_noprop_char (' ');
11277 ++n;
11278 }
11279
11280 return n;
11281 }
11282
11283 /***********************************************************************
11284 Frame Titles
11285 ***********************************************************************/
11286
11287 #ifdef HAVE_WINDOW_SYSTEM
11288
11289 /* Set the title of FRAME, if it has changed. The title format is
11290 Vicon_title_format if FRAME is iconified, otherwise it is
11291 frame_title_format. */
11292
11293 static void
11294 x_consider_frame_title (Lisp_Object frame)
11295 {
11296 struct frame *f = XFRAME (frame);
11297
11298 if (FRAME_WINDOW_P (f)
11299 || FRAME_MINIBUF_ONLY_P (f)
11300 || f->explicit_name)
11301 {
11302 /* Do we have more than one visible frame on this X display? */
11303 Lisp_Object tail, other_frame, fmt;
11304 ptrdiff_t title_start;
11305 char *title;
11306 ptrdiff_t len;
11307 struct it it;
11308 ptrdiff_t count = SPECPDL_INDEX ();
11309
11310 FOR_EACH_FRAME (tail, other_frame)
11311 {
11312 struct frame *tf = XFRAME (other_frame);
11313
11314 if (tf != f
11315 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11316 && !FRAME_MINIBUF_ONLY_P (tf)
11317 && !EQ (other_frame, tip_frame)
11318 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11319 break;
11320 }
11321
11322 /* Set global variable indicating that multiple frames exist. */
11323 multiple_frames = CONSP (tail);
11324
11325 /* Switch to the buffer of selected window of the frame. Set up
11326 mode_line_target so that display_mode_element will output into
11327 mode_line_noprop_buf; then display the title. */
11328 record_unwind_protect (unwind_format_mode_line,
11329 format_mode_line_unwind_data
11330 (f, current_buffer, selected_window, 0));
11331
11332 Fselect_window (f->selected_window, Qt);
11333 set_buffer_internal_1
11334 (XBUFFER (XWINDOW (f->selected_window)->contents));
11335 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11336
11337 mode_line_target = MODE_LINE_TITLE;
11338 title_start = MODE_LINE_NOPROP_LEN (0);
11339 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11340 NULL, DEFAULT_FACE_ID);
11341 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11342 len = MODE_LINE_NOPROP_LEN (title_start);
11343 title = mode_line_noprop_buf + title_start;
11344 unbind_to (count, Qnil);
11345
11346 /* Set the title only if it's changed. This avoids consing in
11347 the common case where it hasn't. (If it turns out that we've
11348 already wasted too much time by walking through the list with
11349 display_mode_element, then we might need to optimize at a
11350 higher level than this.) */
11351 if (! STRINGP (f->name)
11352 || SBYTES (f->name) != len
11353 || memcmp (title, SDATA (f->name), len) != 0)
11354 x_implicitly_set_name (f, make_string (title, len), Qnil);
11355 }
11356 }
11357
11358 #endif /* not HAVE_WINDOW_SYSTEM */
11359
11360 \f
11361 /***********************************************************************
11362 Menu Bars
11363 ***********************************************************************/
11364
11365 /* Non-zero if we will not redisplay all visible windows. */
11366 #define REDISPLAY_SOME_P() \
11367 ((windows_or_buffers_changed == 0 \
11368 || windows_or_buffers_changed == REDISPLAY_SOME) \
11369 && (update_mode_lines == 0 \
11370 || update_mode_lines == REDISPLAY_SOME))
11371
11372 /* Prepare for redisplay by updating menu-bar item lists when
11373 appropriate. This can call eval. */
11374
11375 static void
11376 prepare_menu_bars (void)
11377 {
11378 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11379 bool some_windows = REDISPLAY_SOME_P ();
11380 struct gcpro gcpro1, gcpro2;
11381 Lisp_Object tooltip_frame;
11382
11383 #ifdef HAVE_WINDOW_SYSTEM
11384 tooltip_frame = tip_frame;
11385 #else
11386 tooltip_frame = Qnil;
11387 #endif
11388
11389 if (FUNCTIONP (Vpre_redisplay_function))
11390 {
11391 Lisp_Object windows = all_windows ? Qt : Qnil;
11392 if (all_windows && some_windows)
11393 {
11394 Lisp_Object ws = window_list ();
11395 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11396 {
11397 Lisp_Object this = XCAR (ws);
11398 struct window *w = XWINDOW (this);
11399 if (w->redisplay
11400 || XFRAME (w->frame)->redisplay
11401 || XBUFFER (w->contents)->text->redisplay)
11402 {
11403 windows = Fcons (this, windows);
11404 }
11405 }
11406 }
11407 safe_call1 (Vpre_redisplay_function, windows);
11408 }
11409
11410 /* Update all frame titles based on their buffer names, etc. We do
11411 this before the menu bars so that the buffer-menu will show the
11412 up-to-date frame titles. */
11413 #ifdef HAVE_WINDOW_SYSTEM
11414 if (all_windows)
11415 {
11416 Lisp_Object tail, frame;
11417
11418 FOR_EACH_FRAME (tail, frame)
11419 {
11420 struct frame *f = XFRAME (frame);
11421 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11422 if (some_windows
11423 && !f->redisplay
11424 && !w->redisplay
11425 && !XBUFFER (w->contents)->text->redisplay)
11426 continue;
11427
11428 if (!EQ (frame, tooltip_frame)
11429 && (FRAME_ICONIFIED_P (f)
11430 || FRAME_VISIBLE_P (f) == 1
11431 /* Exclude TTY frames that are obscured because they
11432 are not the top frame on their console. This is
11433 because x_consider_frame_title actually switches
11434 to the frame, which for TTY frames means it is
11435 marked as garbaged, and will be completely
11436 redrawn on the next redisplay cycle. This causes
11437 TTY frames to be completely redrawn, when there
11438 are more than one of them, even though nothing
11439 should be changed on display. */
11440 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11441 x_consider_frame_title (frame);
11442 }
11443 }
11444 #endif /* HAVE_WINDOW_SYSTEM */
11445
11446 /* Update the menu bar item lists, if appropriate. This has to be
11447 done before any actual redisplay or generation of display lines. */
11448
11449 if (all_windows)
11450 {
11451 Lisp_Object tail, frame;
11452 ptrdiff_t count = SPECPDL_INDEX ();
11453 /* 1 means that update_menu_bar has run its hooks
11454 so any further calls to update_menu_bar shouldn't do so again. */
11455 int menu_bar_hooks_run = 0;
11456
11457 record_unwind_save_match_data ();
11458
11459 FOR_EACH_FRAME (tail, frame)
11460 {
11461 struct frame *f = XFRAME (frame);
11462 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11463
11464 /* Ignore tooltip frame. */
11465 if (EQ (frame, tooltip_frame))
11466 continue;
11467
11468 if (some_windows
11469 && !f->redisplay
11470 && !w->redisplay
11471 && !XBUFFER (w->contents)->text->redisplay)
11472 continue;
11473
11474 /* If a window on this frame changed size, report that to
11475 the user and clear the size-change flag. */
11476 if (FRAME_WINDOW_SIZES_CHANGED (f))
11477 {
11478 Lisp_Object functions;
11479
11480 /* Clear flag first in case we get an error below. */
11481 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11482 functions = Vwindow_size_change_functions;
11483 GCPRO2 (tail, functions);
11484
11485 while (CONSP (functions))
11486 {
11487 if (!EQ (XCAR (functions), Qt))
11488 call1 (XCAR (functions), frame);
11489 functions = XCDR (functions);
11490 }
11491 UNGCPRO;
11492 }
11493
11494 GCPRO1 (tail);
11495 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11496 #ifdef HAVE_WINDOW_SYSTEM
11497 update_tool_bar (f, 0);
11498 #endif
11499 #ifdef HAVE_NS
11500 if (windows_or_buffers_changed
11501 && FRAME_NS_P (f))
11502 ns_set_doc_edited
11503 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11504 #endif
11505 UNGCPRO;
11506 }
11507
11508 unbind_to (count, Qnil);
11509 }
11510 else
11511 {
11512 struct frame *sf = SELECTED_FRAME ();
11513 update_menu_bar (sf, 1, 0);
11514 #ifdef HAVE_WINDOW_SYSTEM
11515 update_tool_bar (sf, 1);
11516 #endif
11517 }
11518 }
11519
11520
11521 /* Update the menu bar item list for frame F. This has to be done
11522 before we start to fill in any display lines, because it can call
11523 eval.
11524
11525 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11526
11527 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11528 already ran the menu bar hooks for this redisplay, so there
11529 is no need to run them again. The return value is the
11530 updated value of this flag, to pass to the next call. */
11531
11532 static int
11533 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11534 {
11535 Lisp_Object window;
11536 register struct window *w;
11537
11538 /* If called recursively during a menu update, do nothing. This can
11539 happen when, for instance, an activate-menubar-hook causes a
11540 redisplay. */
11541 if (inhibit_menubar_update)
11542 return hooks_run;
11543
11544 window = FRAME_SELECTED_WINDOW (f);
11545 w = XWINDOW (window);
11546
11547 if (FRAME_WINDOW_P (f)
11548 ?
11549 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11550 || defined (HAVE_NS) || defined (USE_GTK)
11551 FRAME_EXTERNAL_MENU_BAR (f)
11552 #else
11553 FRAME_MENU_BAR_LINES (f) > 0
11554 #endif
11555 : FRAME_MENU_BAR_LINES (f) > 0)
11556 {
11557 /* If the user has switched buffers or windows, we need to
11558 recompute to reflect the new bindings. But we'll
11559 recompute when update_mode_lines is set too; that means
11560 that people can use force-mode-line-update to request
11561 that the menu bar be recomputed. The adverse effect on
11562 the rest of the redisplay algorithm is about the same as
11563 windows_or_buffers_changed anyway. */
11564 if (windows_or_buffers_changed
11565 /* This used to test w->update_mode_line, but we believe
11566 there is no need to recompute the menu in that case. */
11567 || update_mode_lines
11568 || window_buffer_changed (w))
11569 {
11570 struct buffer *prev = current_buffer;
11571 ptrdiff_t count = SPECPDL_INDEX ();
11572
11573 specbind (Qinhibit_menubar_update, Qt);
11574
11575 set_buffer_internal_1 (XBUFFER (w->contents));
11576 if (save_match_data)
11577 record_unwind_save_match_data ();
11578 if (NILP (Voverriding_local_map_menu_flag))
11579 {
11580 specbind (Qoverriding_terminal_local_map, Qnil);
11581 specbind (Qoverriding_local_map, Qnil);
11582 }
11583
11584 if (!hooks_run)
11585 {
11586 /* Run the Lucid hook. */
11587 safe_run_hooks (Qactivate_menubar_hook);
11588
11589 /* If it has changed current-menubar from previous value,
11590 really recompute the menu-bar from the value. */
11591 if (! NILP (Vlucid_menu_bar_dirty_flag))
11592 call0 (Qrecompute_lucid_menubar);
11593
11594 safe_run_hooks (Qmenu_bar_update_hook);
11595
11596 hooks_run = 1;
11597 }
11598
11599 XSETFRAME (Vmenu_updating_frame, f);
11600 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11601
11602 /* Redisplay the menu bar in case we changed it. */
11603 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11604 || defined (HAVE_NS) || defined (USE_GTK)
11605 if (FRAME_WINDOW_P (f))
11606 {
11607 #if defined (HAVE_NS)
11608 /* All frames on Mac OS share the same menubar. So only
11609 the selected frame should be allowed to set it. */
11610 if (f == SELECTED_FRAME ())
11611 #endif
11612 set_frame_menubar (f, 0, 0);
11613 }
11614 else
11615 /* On a terminal screen, the menu bar is an ordinary screen
11616 line, and this makes it get updated. */
11617 w->update_mode_line = 1;
11618 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11619 /* In the non-toolkit version, the menu bar is an ordinary screen
11620 line, and this makes it get updated. */
11621 w->update_mode_line = 1;
11622 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11623
11624 unbind_to (count, Qnil);
11625 set_buffer_internal_1 (prev);
11626 }
11627 }
11628
11629 return hooks_run;
11630 }
11631
11632 /***********************************************************************
11633 Tool-bars
11634 ***********************************************************************/
11635
11636 #ifdef HAVE_WINDOW_SYSTEM
11637
11638 /* Tool-bar item index of the item on which a mouse button was pressed
11639 or -1. */
11640
11641 int last_tool_bar_item;
11642
11643 /* Select `frame' temporarily without running all the code in
11644 do_switch_frame.
11645 FIXME: Maybe do_switch_frame should be trimmed down similarly
11646 when `norecord' is set. */
11647 static void
11648 fast_set_selected_frame (Lisp_Object frame)
11649 {
11650 if (!EQ (selected_frame, frame))
11651 {
11652 selected_frame = frame;
11653 selected_window = XFRAME (frame)->selected_window;
11654 }
11655 }
11656
11657 /* Update the tool-bar item list for frame F. This has to be done
11658 before we start to fill in any display lines. Called from
11659 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11660 and restore it here. */
11661
11662 static void
11663 update_tool_bar (struct frame *f, int save_match_data)
11664 {
11665 #if defined (USE_GTK) || defined (HAVE_NS)
11666 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11667 #else
11668 int do_update = (WINDOWP (f->tool_bar_window)
11669 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11670 #endif
11671
11672 if (do_update)
11673 {
11674 Lisp_Object window;
11675 struct window *w;
11676
11677 window = FRAME_SELECTED_WINDOW (f);
11678 w = XWINDOW (window);
11679
11680 /* If the user has switched buffers or windows, we need to
11681 recompute to reflect the new bindings. But we'll
11682 recompute when update_mode_lines is set too; that means
11683 that people can use force-mode-line-update to request
11684 that the menu bar be recomputed. The adverse effect on
11685 the rest of the redisplay algorithm is about the same as
11686 windows_or_buffers_changed anyway. */
11687 if (windows_or_buffers_changed
11688 || w->update_mode_line
11689 || update_mode_lines
11690 || window_buffer_changed (w))
11691 {
11692 struct buffer *prev = current_buffer;
11693 ptrdiff_t count = SPECPDL_INDEX ();
11694 Lisp_Object frame, new_tool_bar;
11695 int new_n_tool_bar;
11696 struct gcpro gcpro1;
11697
11698 /* Set current_buffer to the buffer of the selected
11699 window of the frame, so that we get the right local
11700 keymaps. */
11701 set_buffer_internal_1 (XBUFFER (w->contents));
11702
11703 /* Save match data, if we must. */
11704 if (save_match_data)
11705 record_unwind_save_match_data ();
11706
11707 /* Make sure that we don't accidentally use bogus keymaps. */
11708 if (NILP (Voverriding_local_map_menu_flag))
11709 {
11710 specbind (Qoverriding_terminal_local_map, Qnil);
11711 specbind (Qoverriding_local_map, Qnil);
11712 }
11713
11714 GCPRO1 (new_tool_bar);
11715
11716 /* We must temporarily set the selected frame to this frame
11717 before calling tool_bar_items, because the calculation of
11718 the tool-bar keymap uses the selected frame (see
11719 `tool-bar-make-keymap' in tool-bar.el). */
11720 eassert (EQ (selected_window,
11721 /* Since we only explicitly preserve selected_frame,
11722 check that selected_window would be redundant. */
11723 XFRAME (selected_frame)->selected_window));
11724 record_unwind_protect (fast_set_selected_frame, selected_frame);
11725 XSETFRAME (frame, f);
11726 fast_set_selected_frame (frame);
11727
11728 /* Build desired tool-bar items from keymaps. */
11729 new_tool_bar
11730 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11731 &new_n_tool_bar);
11732
11733 /* Redisplay the tool-bar if we changed it. */
11734 if (new_n_tool_bar != f->n_tool_bar_items
11735 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11736 {
11737 /* Redisplay that happens asynchronously due to an expose event
11738 may access f->tool_bar_items. Make sure we update both
11739 variables within BLOCK_INPUT so no such event interrupts. */
11740 block_input ();
11741 fset_tool_bar_items (f, new_tool_bar);
11742 f->n_tool_bar_items = new_n_tool_bar;
11743 w->update_mode_line = 1;
11744 unblock_input ();
11745 }
11746
11747 UNGCPRO;
11748
11749 unbind_to (count, Qnil);
11750 set_buffer_internal_1 (prev);
11751 }
11752 }
11753 }
11754
11755 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11756
11757 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11758 F's desired tool-bar contents. F->tool_bar_items must have
11759 been set up previously by calling prepare_menu_bars. */
11760
11761 static void
11762 build_desired_tool_bar_string (struct frame *f)
11763 {
11764 int i, size, size_needed;
11765 struct gcpro gcpro1, gcpro2, gcpro3;
11766 Lisp_Object image, plist, props;
11767
11768 image = plist = props = Qnil;
11769 GCPRO3 (image, plist, props);
11770
11771 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11772 Otherwise, make a new string. */
11773
11774 /* The size of the string we might be able to reuse. */
11775 size = (STRINGP (f->desired_tool_bar_string)
11776 ? SCHARS (f->desired_tool_bar_string)
11777 : 0);
11778
11779 /* We need one space in the string for each image. */
11780 size_needed = f->n_tool_bar_items;
11781
11782 /* Reuse f->desired_tool_bar_string, if possible. */
11783 if (size < size_needed || NILP (f->desired_tool_bar_string))
11784 fset_desired_tool_bar_string
11785 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11786 else
11787 {
11788 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11789 Fremove_text_properties (make_number (0), make_number (size),
11790 props, f->desired_tool_bar_string);
11791 }
11792
11793 /* Put a `display' property on the string for the images to display,
11794 put a `menu_item' property on tool-bar items with a value that
11795 is the index of the item in F's tool-bar item vector. */
11796 for (i = 0; i < f->n_tool_bar_items; ++i)
11797 {
11798 #define PROP(IDX) \
11799 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11800
11801 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11802 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11803 int hmargin, vmargin, relief, idx, end;
11804
11805 /* If image is a vector, choose the image according to the
11806 button state. */
11807 image = PROP (TOOL_BAR_ITEM_IMAGES);
11808 if (VECTORP (image))
11809 {
11810 if (enabled_p)
11811 idx = (selected_p
11812 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11813 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11814 else
11815 idx = (selected_p
11816 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11817 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11818
11819 eassert (ASIZE (image) >= idx);
11820 image = AREF (image, idx);
11821 }
11822 else
11823 idx = -1;
11824
11825 /* Ignore invalid image specifications. */
11826 if (!valid_image_p (image))
11827 continue;
11828
11829 /* Display the tool-bar button pressed, or depressed. */
11830 plist = Fcopy_sequence (XCDR (image));
11831
11832 /* Compute margin and relief to draw. */
11833 relief = (tool_bar_button_relief >= 0
11834 ? tool_bar_button_relief
11835 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11836 hmargin = vmargin = relief;
11837
11838 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11839 INT_MAX - max (hmargin, vmargin)))
11840 {
11841 hmargin += XFASTINT (Vtool_bar_button_margin);
11842 vmargin += XFASTINT (Vtool_bar_button_margin);
11843 }
11844 else if (CONSP (Vtool_bar_button_margin))
11845 {
11846 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11847 INT_MAX - hmargin))
11848 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11849
11850 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11851 INT_MAX - vmargin))
11852 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11853 }
11854
11855 if (auto_raise_tool_bar_buttons_p)
11856 {
11857 /* Add a `:relief' property to the image spec if the item is
11858 selected. */
11859 if (selected_p)
11860 {
11861 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11862 hmargin -= relief;
11863 vmargin -= relief;
11864 }
11865 }
11866 else
11867 {
11868 /* If image is selected, display it pressed, i.e. with a
11869 negative relief. If it's not selected, display it with a
11870 raised relief. */
11871 plist = Fplist_put (plist, QCrelief,
11872 (selected_p
11873 ? make_number (-relief)
11874 : make_number (relief)));
11875 hmargin -= relief;
11876 vmargin -= relief;
11877 }
11878
11879 /* Put a margin around the image. */
11880 if (hmargin || vmargin)
11881 {
11882 if (hmargin == vmargin)
11883 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11884 else
11885 plist = Fplist_put (plist, QCmargin,
11886 Fcons (make_number (hmargin),
11887 make_number (vmargin)));
11888 }
11889
11890 /* If button is not enabled, and we don't have special images
11891 for the disabled state, make the image appear disabled by
11892 applying an appropriate algorithm to it. */
11893 if (!enabled_p && idx < 0)
11894 plist = Fplist_put (plist, QCconversion, Qdisabled);
11895
11896 /* Put a `display' text property on the string for the image to
11897 display. Put a `menu-item' property on the string that gives
11898 the start of this item's properties in the tool-bar items
11899 vector. */
11900 image = Fcons (Qimage, plist);
11901 props = list4 (Qdisplay, image,
11902 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11903
11904 /* Let the last image hide all remaining spaces in the tool bar
11905 string. The string can be longer than needed when we reuse a
11906 previous string. */
11907 if (i + 1 == f->n_tool_bar_items)
11908 end = SCHARS (f->desired_tool_bar_string);
11909 else
11910 end = i + 1;
11911 Fadd_text_properties (make_number (i), make_number (end),
11912 props, f->desired_tool_bar_string);
11913 #undef PROP
11914 }
11915
11916 UNGCPRO;
11917 }
11918
11919
11920 /* Display one line of the tool-bar of frame IT->f.
11921
11922 HEIGHT specifies the desired height of the tool-bar line.
11923 If the actual height of the glyph row is less than HEIGHT, the
11924 row's height is increased to HEIGHT, and the icons are centered
11925 vertically in the new height.
11926
11927 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11928 count a final empty row in case the tool-bar width exactly matches
11929 the window width.
11930 */
11931
11932 static void
11933 display_tool_bar_line (struct it *it, int height)
11934 {
11935 struct glyph_row *row = it->glyph_row;
11936 int max_x = it->last_visible_x;
11937 struct glyph *last;
11938
11939 prepare_desired_row (row);
11940 row->y = it->current_y;
11941
11942 /* Note that this isn't made use of if the face hasn't a box,
11943 so there's no need to check the face here. */
11944 it->start_of_box_run_p = 1;
11945
11946 while (it->current_x < max_x)
11947 {
11948 int x, n_glyphs_before, i, nglyphs;
11949 struct it it_before;
11950
11951 /* Get the next display element. */
11952 if (!get_next_display_element (it))
11953 {
11954 /* Don't count empty row if we are counting needed tool-bar lines. */
11955 if (height < 0 && !it->hpos)
11956 return;
11957 break;
11958 }
11959
11960 /* Produce glyphs. */
11961 n_glyphs_before = row->used[TEXT_AREA];
11962 it_before = *it;
11963
11964 PRODUCE_GLYPHS (it);
11965
11966 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11967 i = 0;
11968 x = it_before.current_x;
11969 while (i < nglyphs)
11970 {
11971 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11972
11973 if (x + glyph->pixel_width > max_x)
11974 {
11975 /* Glyph doesn't fit on line. Backtrack. */
11976 row->used[TEXT_AREA] = n_glyphs_before;
11977 *it = it_before;
11978 /* If this is the only glyph on this line, it will never fit on the
11979 tool-bar, so skip it. But ensure there is at least one glyph,
11980 so we don't accidentally disable the tool-bar. */
11981 if (n_glyphs_before == 0
11982 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11983 break;
11984 goto out;
11985 }
11986
11987 ++it->hpos;
11988 x += glyph->pixel_width;
11989 ++i;
11990 }
11991
11992 /* Stop at line end. */
11993 if (ITERATOR_AT_END_OF_LINE_P (it))
11994 break;
11995
11996 set_iterator_to_next (it, 1);
11997 }
11998
11999 out:;
12000
12001 row->displays_text_p = row->used[TEXT_AREA] != 0;
12002
12003 /* Use default face for the border below the tool bar.
12004
12005 FIXME: When auto-resize-tool-bars is grow-only, there is
12006 no additional border below the possibly empty tool-bar lines.
12007 So to make the extra empty lines look "normal", we have to
12008 use the tool-bar face for the border too. */
12009 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12010 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12011 it->face_id = DEFAULT_FACE_ID;
12012
12013 extend_face_to_end_of_line (it);
12014 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12015 last->right_box_line_p = 1;
12016 if (last == row->glyphs[TEXT_AREA])
12017 last->left_box_line_p = 1;
12018
12019 /* Make line the desired height and center it vertically. */
12020 if ((height -= it->max_ascent + it->max_descent) > 0)
12021 {
12022 /* Don't add more than one line height. */
12023 height %= FRAME_LINE_HEIGHT (it->f);
12024 it->max_ascent += height / 2;
12025 it->max_descent += (height + 1) / 2;
12026 }
12027
12028 compute_line_metrics (it);
12029
12030 /* If line is empty, make it occupy the rest of the tool-bar. */
12031 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12032 {
12033 row->height = row->phys_height = it->last_visible_y - row->y;
12034 row->visible_height = row->height;
12035 row->ascent = row->phys_ascent = 0;
12036 row->extra_line_spacing = 0;
12037 }
12038
12039 row->full_width_p = 1;
12040 row->continued_p = 0;
12041 row->truncated_on_left_p = 0;
12042 row->truncated_on_right_p = 0;
12043
12044 it->current_x = it->hpos = 0;
12045 it->current_y += row->height;
12046 ++it->vpos;
12047 ++it->glyph_row;
12048 }
12049
12050
12051 /* Max tool-bar height. Basically, this is what makes all other windows
12052 disappear when the frame gets too small. Rethink this! */
12053
12054 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12055 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12056
12057 /* Value is the number of screen lines needed to make all tool-bar
12058 items of frame F visible. The number of actual rows needed is
12059 returned in *N_ROWS if non-NULL. */
12060
12061 static int
12062 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12063 {
12064 struct window *w = XWINDOW (f->tool_bar_window);
12065 struct it it;
12066 /* tool_bar_height is called from redisplay_tool_bar after building
12067 the desired matrix, so use (unused) mode-line row as temporary row to
12068 avoid destroying the first tool-bar row. */
12069 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12070
12071 /* Initialize an iterator for iteration over
12072 F->desired_tool_bar_string in the tool-bar window of frame F. */
12073 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12074 it.first_visible_x = 0;
12075 /* PXW: Use FRAME_PIXEL_WIDTH (f) here ? */
12076 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12077 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12078 it.paragraph_embedding = L2R;
12079
12080 while (!ITERATOR_AT_END_P (&it))
12081 {
12082 clear_glyph_row (temp_row);
12083 it.glyph_row = temp_row;
12084 display_tool_bar_line (&it, -1);
12085 }
12086 clear_glyph_row (temp_row);
12087
12088 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12089 if (n_rows)
12090 *n_rows = it.vpos > 0 ? it.vpos : -1;
12091
12092 if (pixelwise)
12093 return it.current_y;
12094 else
12095 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12096 }
12097
12098 #endif /* !USE_GTK && !HAVE_NS */
12099
12100 #if defined USE_GTK || defined HAVE_NS
12101 EXFUN (Ftool_bar_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 struct frame *f = decode_any_frame (frame);
12112 int height = 0;
12113
12114 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12115 if (WINDOWP (f->tool_bar_window)
12116 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12117 {
12118 update_tool_bar (f, 1);
12119 if (f->n_tool_bar_items)
12120 {
12121 build_desired_tool_bar_string (f);
12122 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12123 }
12124 }
12125 #endif
12126
12127 return make_number (height);
12128 }
12129
12130
12131 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12132 height should be changed. */
12133
12134 static int
12135 redisplay_tool_bar (struct frame *f)
12136 {
12137 #if defined (USE_GTK) || defined (HAVE_NS)
12138
12139 if (FRAME_EXTERNAL_TOOL_BAR (f))
12140 update_frame_tool_bar (f);
12141 return 0;
12142
12143 #else /* !USE_GTK && !HAVE_NS */
12144
12145 struct window *w;
12146 struct it it;
12147 struct glyph_row *row;
12148
12149 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12150 do anything. This means you must start with tool-bar-lines
12151 non-zero to get the auto-sizing effect. Or in other words, you
12152 can turn off tool-bars by specifying tool-bar-lines zero. */
12153 if (!WINDOWP (f->tool_bar_window)
12154 || (w = XWINDOW (f->tool_bar_window),
12155 WINDOW_PIXEL_HEIGHT (w) == 0))
12156 return 0;
12157
12158 /* Set up an iterator for the tool-bar window. */
12159 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12160 it.first_visible_x = 0;
12161 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12162 row = it.glyph_row;
12163
12164 /* Build a string that represents the contents of the tool-bar. */
12165 build_desired_tool_bar_string (f);
12166 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12167 /* FIXME: This should be controlled by a user option. But it
12168 doesn't make sense to have an R2L tool bar if the menu bar cannot
12169 be drawn also R2L, and making the menu bar R2L is tricky due
12170 toolkit-specific code that implements it. If an R2L tool bar is
12171 ever supported, display_tool_bar_line should also be augmented to
12172 call unproduce_glyphs like display_line and display_string
12173 do. */
12174 it.paragraph_embedding = L2R;
12175
12176 if (f->n_tool_bar_rows == 0)
12177 {
12178 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12179
12180 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12181 {
12182 Lisp_Object frame;
12183 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12184 / FRAME_LINE_HEIGHT (f));
12185
12186 XSETFRAME (frame, f);
12187 Fmodify_frame_parameters (frame,
12188 list1 (Fcons (Qtool_bar_lines,
12189 make_number (new_lines))));
12190 /* Always do that now. */
12191 clear_glyph_matrix (w->desired_matrix);
12192 f->fonts_changed = 1;
12193 return 1;
12194 }
12195 }
12196
12197 /* Display as many lines as needed to display all tool-bar items. */
12198
12199 if (f->n_tool_bar_rows > 0)
12200 {
12201 int border, rows, height, extra;
12202
12203 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12204 border = XINT (Vtool_bar_border);
12205 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12206 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12207 else if (EQ (Vtool_bar_border, Qborder_width))
12208 border = f->border_width;
12209 else
12210 border = 0;
12211 if (border < 0)
12212 border = 0;
12213
12214 rows = f->n_tool_bar_rows;
12215 height = max (1, (it.last_visible_y - border) / rows);
12216 extra = it.last_visible_y - border - height * rows;
12217
12218 while (it.current_y < it.last_visible_y)
12219 {
12220 int h = 0;
12221 if (extra > 0 && rows-- > 0)
12222 {
12223 h = (extra + rows - 1) / rows;
12224 extra -= h;
12225 }
12226 display_tool_bar_line (&it, height + h);
12227 }
12228 }
12229 else
12230 {
12231 while (it.current_y < it.last_visible_y)
12232 display_tool_bar_line (&it, 0);
12233 }
12234
12235 /* It doesn't make much sense to try scrolling in the tool-bar
12236 window, so don't do it. */
12237 w->desired_matrix->no_scrolling_p = 1;
12238 w->must_be_updated_p = 1;
12239
12240 if (!NILP (Vauto_resize_tool_bars))
12241 {
12242 /* Do we really allow the toolbar to occupy the whole frame? */
12243 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12244 int change_height_p = 0;
12245
12246 /* If we couldn't display everything, change the tool-bar's
12247 height if there is room for more. */
12248 if (IT_STRING_CHARPOS (it) < it.end_charpos
12249 && it.current_y < max_tool_bar_height)
12250 change_height_p = 1;
12251
12252 row = it.glyph_row - 1;
12253
12254 /* If there are blank lines at the end, except for a partially
12255 visible blank line at the end that is smaller than
12256 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12257 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12258 && row->height >= FRAME_LINE_HEIGHT (f))
12259 change_height_p = 1;
12260
12261 /* If row displays tool-bar items, but is partially visible,
12262 change the tool-bar's height. */
12263 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12264 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12265 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12266 change_height_p = 1;
12267
12268 /* Resize windows as needed by changing the `tool-bar-lines'
12269 frame parameter. */
12270 if (change_height_p)
12271 {
12272 Lisp_Object frame;
12273 int nrows;
12274 int new_height = tool_bar_height (f, &nrows, 1);
12275
12276 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12277 && !f->minimize_tool_bar_window_p)
12278 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12279 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12280 f->minimize_tool_bar_window_p = 0;
12281
12282 if (change_height_p)
12283 {
12284 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12285 / FRAME_LINE_HEIGHT (f));
12286
12287 XSETFRAME (frame, f);
12288 Fmodify_frame_parameters (frame,
12289 list1 (Fcons (Qtool_bar_lines,
12290 make_number (new_lines))));
12291 /* Always do that now. */
12292 clear_glyph_matrix (w->desired_matrix);
12293 f->n_tool_bar_rows = nrows;
12294 f->fonts_changed = 1;
12295 return 1;
12296 }
12297 }
12298 }
12299
12300 f->minimize_tool_bar_window_p = 0;
12301 return 0;
12302
12303 #endif /* USE_GTK || HAVE_NS */
12304 }
12305
12306 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12307
12308 /* Get information about the tool-bar item which is displayed in GLYPH
12309 on frame F. Return in *PROP_IDX the index where tool-bar item
12310 properties start in F->tool_bar_items. Value is zero if
12311 GLYPH doesn't display a tool-bar item. */
12312
12313 static int
12314 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12315 {
12316 Lisp_Object prop;
12317 int success_p;
12318 int charpos;
12319
12320 /* This function can be called asynchronously, which means we must
12321 exclude any possibility that Fget_text_property signals an
12322 error. */
12323 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12324 charpos = max (0, charpos);
12325
12326 /* Get the text property `menu-item' at pos. The value of that
12327 property is the start index of this item's properties in
12328 F->tool_bar_items. */
12329 prop = Fget_text_property (make_number (charpos),
12330 Qmenu_item, f->current_tool_bar_string);
12331 if (INTEGERP (prop))
12332 {
12333 *prop_idx = XINT (prop);
12334 success_p = 1;
12335 }
12336 else
12337 success_p = 0;
12338
12339 return success_p;
12340 }
12341
12342 \f
12343 /* Get information about the tool-bar item at position X/Y on frame F.
12344 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12345 the current matrix of the tool-bar window of F, or NULL if not
12346 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12347 item in F->tool_bar_items. Value is
12348
12349 -1 if X/Y is not on a tool-bar item
12350 0 if X/Y is on the same item that was highlighted before.
12351 1 otherwise. */
12352
12353 static int
12354 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12355 int *hpos, int *vpos, int *prop_idx)
12356 {
12357 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12358 struct window *w = XWINDOW (f->tool_bar_window);
12359 int area;
12360
12361 /* Find the glyph under X/Y. */
12362 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12363 if (*glyph == NULL)
12364 return -1;
12365
12366 /* Get the start of this tool-bar item's properties in
12367 f->tool_bar_items. */
12368 if (!tool_bar_item_info (f, *glyph, prop_idx))
12369 return -1;
12370
12371 /* Is mouse on the highlighted item? */
12372 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12373 && *vpos >= hlinfo->mouse_face_beg_row
12374 && *vpos <= hlinfo->mouse_face_end_row
12375 && (*vpos > hlinfo->mouse_face_beg_row
12376 || *hpos >= hlinfo->mouse_face_beg_col)
12377 && (*vpos < hlinfo->mouse_face_end_row
12378 || *hpos < hlinfo->mouse_face_end_col
12379 || hlinfo->mouse_face_past_end))
12380 return 0;
12381
12382 return 1;
12383 }
12384
12385
12386 /* EXPORT:
12387 Handle mouse button event on the tool-bar of frame F, at
12388 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12389 0 for button release. MODIFIERS is event modifiers for button
12390 release. */
12391
12392 void
12393 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12394 int modifiers)
12395 {
12396 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12397 struct window *w = XWINDOW (f->tool_bar_window);
12398 int hpos, vpos, prop_idx;
12399 struct glyph *glyph;
12400 Lisp_Object enabled_p;
12401 int ts;
12402
12403 /* If not on the highlighted tool-bar item, and mouse-highlight is
12404 non-nil, return. This is so we generate the tool-bar button
12405 click only when the mouse button is released on the same item as
12406 where it was pressed. However, when mouse-highlight is disabled,
12407 generate the click when the button is released regardless of the
12408 highlight, since tool-bar items are not highlighted in that
12409 case. */
12410 frame_to_window_pixel_xy (w, &x, &y);
12411 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12412 if (ts == -1
12413 || (ts != 0 && !NILP (Vmouse_highlight)))
12414 return;
12415
12416 /* When mouse-highlight is off, generate the click for the item
12417 where the button was pressed, disregarding where it was
12418 released. */
12419 if (NILP (Vmouse_highlight) && !down_p)
12420 prop_idx = last_tool_bar_item;
12421
12422 /* If item is disabled, do nothing. */
12423 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12424 if (NILP (enabled_p))
12425 return;
12426
12427 if (down_p)
12428 {
12429 /* Show item in pressed state. */
12430 if (!NILP (Vmouse_highlight))
12431 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12432 last_tool_bar_item = prop_idx;
12433 }
12434 else
12435 {
12436 Lisp_Object key, frame;
12437 struct input_event event;
12438 EVENT_INIT (event);
12439
12440 /* Show item in released state. */
12441 if (!NILP (Vmouse_highlight))
12442 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12443
12444 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12445
12446 XSETFRAME (frame, f);
12447 event.kind = TOOL_BAR_EVENT;
12448 event.frame_or_window = frame;
12449 event.arg = frame;
12450 kbd_buffer_store_event (&event);
12451
12452 event.kind = TOOL_BAR_EVENT;
12453 event.frame_or_window = frame;
12454 event.arg = key;
12455 event.modifiers = modifiers;
12456 kbd_buffer_store_event (&event);
12457 last_tool_bar_item = -1;
12458 }
12459 }
12460
12461
12462 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12463 tool-bar window-relative coordinates X/Y. Called from
12464 note_mouse_highlight. */
12465
12466 static void
12467 note_tool_bar_highlight (struct frame *f, int x, int y)
12468 {
12469 Lisp_Object window = f->tool_bar_window;
12470 struct window *w = XWINDOW (window);
12471 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12472 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12473 int hpos, vpos;
12474 struct glyph *glyph;
12475 struct glyph_row *row;
12476 int i;
12477 Lisp_Object enabled_p;
12478 int prop_idx;
12479 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12480 int mouse_down_p, rc;
12481
12482 /* Function note_mouse_highlight is called with negative X/Y
12483 values when mouse moves outside of the frame. */
12484 if (x <= 0 || y <= 0)
12485 {
12486 clear_mouse_face (hlinfo);
12487 return;
12488 }
12489
12490 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12491 if (rc < 0)
12492 {
12493 /* Not on tool-bar item. */
12494 clear_mouse_face (hlinfo);
12495 return;
12496 }
12497 else if (rc == 0)
12498 /* On same tool-bar item as before. */
12499 goto set_help_echo;
12500
12501 clear_mouse_face (hlinfo);
12502
12503 /* Mouse is down, but on different tool-bar item? */
12504 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12505 && f == dpyinfo->last_mouse_frame);
12506
12507 if (mouse_down_p
12508 && last_tool_bar_item != prop_idx)
12509 return;
12510
12511 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12512
12513 /* If tool-bar item is not enabled, don't highlight it. */
12514 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12515 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12516 {
12517 /* Compute the x-position of the glyph. In front and past the
12518 image is a space. We include this in the highlighted area. */
12519 row = MATRIX_ROW (w->current_matrix, vpos);
12520 for (i = x = 0; i < hpos; ++i)
12521 x += row->glyphs[TEXT_AREA][i].pixel_width;
12522
12523 /* Record this as the current active region. */
12524 hlinfo->mouse_face_beg_col = hpos;
12525 hlinfo->mouse_face_beg_row = vpos;
12526 hlinfo->mouse_face_beg_x = x;
12527 hlinfo->mouse_face_past_end = 0;
12528
12529 hlinfo->mouse_face_end_col = hpos + 1;
12530 hlinfo->mouse_face_end_row = vpos;
12531 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12532 hlinfo->mouse_face_window = window;
12533 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12534
12535 /* Display it as active. */
12536 show_mouse_face (hlinfo, draw);
12537 }
12538
12539 set_help_echo:
12540
12541 /* Set help_echo_string to a help string to display for this tool-bar item.
12542 XTread_socket does the rest. */
12543 help_echo_object = help_echo_window = Qnil;
12544 help_echo_pos = -1;
12545 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12546 if (NILP (help_echo_string))
12547 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12548 }
12549
12550 #endif /* !USE_GTK && !HAVE_NS */
12551
12552 #endif /* HAVE_WINDOW_SYSTEM */
12553
12554
12555 \f
12556 /************************************************************************
12557 Horizontal scrolling
12558 ************************************************************************/
12559
12560 static int hscroll_window_tree (Lisp_Object);
12561 static int hscroll_windows (Lisp_Object);
12562
12563 /* For all leaf windows in the window tree rooted at WINDOW, set their
12564 hscroll value so that PT is (i) visible in the window, and (ii) so
12565 that it is not within a certain margin at the window's left and
12566 right border. Value is non-zero if any window's hscroll has been
12567 changed. */
12568
12569 static int
12570 hscroll_window_tree (Lisp_Object window)
12571 {
12572 int hscrolled_p = 0;
12573 int hscroll_relative_p = FLOATP (Vhscroll_step);
12574 int hscroll_step_abs = 0;
12575 double hscroll_step_rel = 0;
12576
12577 if (hscroll_relative_p)
12578 {
12579 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12580 if (hscroll_step_rel < 0)
12581 {
12582 hscroll_relative_p = 0;
12583 hscroll_step_abs = 0;
12584 }
12585 }
12586 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12587 {
12588 hscroll_step_abs = XINT (Vhscroll_step);
12589 if (hscroll_step_abs < 0)
12590 hscroll_step_abs = 0;
12591 }
12592 else
12593 hscroll_step_abs = 0;
12594
12595 while (WINDOWP (window))
12596 {
12597 struct window *w = XWINDOW (window);
12598
12599 if (WINDOWP (w->contents))
12600 hscrolled_p |= hscroll_window_tree (w->contents);
12601 else if (w->cursor.vpos >= 0)
12602 {
12603 int h_margin;
12604 int text_area_width;
12605 struct glyph_row *current_cursor_row
12606 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12607 struct glyph_row *desired_cursor_row
12608 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12609 struct glyph_row *cursor_row
12610 = (desired_cursor_row->enabled_p
12611 ? desired_cursor_row
12612 : current_cursor_row);
12613 int row_r2l_p = cursor_row->reversed_p;
12614
12615 text_area_width = window_box_width (w, TEXT_AREA);
12616
12617 /* Scroll when cursor is inside this scroll margin. */
12618 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12619
12620 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12621 /* For left-to-right rows, hscroll when cursor is either
12622 (i) inside the right hscroll margin, or (ii) if it is
12623 inside the left margin and the window is already
12624 hscrolled. */
12625 && ((!row_r2l_p
12626 && ((w->hscroll
12627 && w->cursor.x <= h_margin)
12628 || (cursor_row->enabled_p
12629 && cursor_row->truncated_on_right_p
12630 && (w->cursor.x >= text_area_width - h_margin))))
12631 /* For right-to-left rows, the logic is similar,
12632 except that rules for scrolling to left and right
12633 are reversed. E.g., if cursor.x <= h_margin, we
12634 need to hscroll "to the right" unconditionally,
12635 and that will scroll the screen to the left so as
12636 to reveal the next portion of the row. */
12637 || (row_r2l_p
12638 && ((cursor_row->enabled_p
12639 /* FIXME: It is confusing to set the
12640 truncated_on_right_p flag when R2L rows
12641 are actually truncated on the left. */
12642 && cursor_row->truncated_on_right_p
12643 && w->cursor.x <= h_margin)
12644 || (w->hscroll
12645 && (w->cursor.x >= text_area_width - h_margin))))))
12646 {
12647 struct it it;
12648 ptrdiff_t hscroll;
12649 struct buffer *saved_current_buffer;
12650 ptrdiff_t pt;
12651 int wanted_x;
12652
12653 /* Find point in a display of infinite width. */
12654 saved_current_buffer = current_buffer;
12655 current_buffer = XBUFFER (w->contents);
12656
12657 if (w == XWINDOW (selected_window))
12658 pt = PT;
12659 else
12660 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12661
12662 /* Move iterator to pt starting at cursor_row->start in
12663 a line with infinite width. */
12664 init_to_row_start (&it, w, cursor_row);
12665 it.last_visible_x = INFINITY;
12666 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12667 current_buffer = saved_current_buffer;
12668
12669 /* Position cursor in window. */
12670 if (!hscroll_relative_p && hscroll_step_abs == 0)
12671 hscroll = max (0, (it.current_x
12672 - (ITERATOR_AT_END_OF_LINE_P (&it)
12673 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12674 : (text_area_width / 2))))
12675 / FRAME_COLUMN_WIDTH (it.f);
12676 else if ((!row_r2l_p
12677 && w->cursor.x >= text_area_width - h_margin)
12678 || (row_r2l_p && w->cursor.x <= h_margin))
12679 {
12680 if (hscroll_relative_p)
12681 wanted_x = text_area_width * (1 - hscroll_step_rel)
12682 - h_margin;
12683 else
12684 wanted_x = text_area_width
12685 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12686 - h_margin;
12687 hscroll
12688 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12689 }
12690 else
12691 {
12692 if (hscroll_relative_p)
12693 wanted_x = text_area_width * hscroll_step_rel
12694 + h_margin;
12695 else
12696 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12697 + h_margin;
12698 hscroll
12699 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12700 }
12701 hscroll = max (hscroll, w->min_hscroll);
12702
12703 /* Don't prevent redisplay optimizations if hscroll
12704 hasn't changed, as it will unnecessarily slow down
12705 redisplay. */
12706 if (w->hscroll != hscroll)
12707 {
12708 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12709 w->hscroll = hscroll;
12710 hscrolled_p = 1;
12711 }
12712 }
12713 }
12714
12715 window = w->next;
12716 }
12717
12718 /* Value is non-zero if hscroll of any leaf window has been changed. */
12719 return hscrolled_p;
12720 }
12721
12722
12723 /* Set hscroll so that cursor is visible and not inside horizontal
12724 scroll margins for all windows in the tree rooted at WINDOW. See
12725 also hscroll_window_tree above. Value is non-zero if any window's
12726 hscroll has been changed. If it has, desired matrices on the frame
12727 of WINDOW are cleared. */
12728
12729 static int
12730 hscroll_windows (Lisp_Object window)
12731 {
12732 int hscrolled_p = hscroll_window_tree (window);
12733 if (hscrolled_p)
12734 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12735 return hscrolled_p;
12736 }
12737
12738
12739 \f
12740 /************************************************************************
12741 Redisplay
12742 ************************************************************************/
12743
12744 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12745 to a non-zero value. This is sometimes handy to have in a debugger
12746 session. */
12747
12748 #ifdef GLYPH_DEBUG
12749
12750 /* First and last unchanged row for try_window_id. */
12751
12752 static int debug_first_unchanged_at_end_vpos;
12753 static int debug_last_unchanged_at_beg_vpos;
12754
12755 /* Delta vpos and y. */
12756
12757 static int debug_dvpos, debug_dy;
12758
12759 /* Delta in characters and bytes for try_window_id. */
12760
12761 static ptrdiff_t debug_delta, debug_delta_bytes;
12762
12763 /* Values of window_end_pos and window_end_vpos at the end of
12764 try_window_id. */
12765
12766 static ptrdiff_t debug_end_vpos;
12767
12768 /* Append a string to W->desired_matrix->method. FMT is a printf
12769 format string. If trace_redisplay_p is non-zero also printf the
12770 resulting string to stderr. */
12771
12772 static void debug_method_add (struct window *, char const *, ...)
12773 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12774
12775 static void
12776 debug_method_add (struct window *w, char const *fmt, ...)
12777 {
12778 void *ptr = w;
12779 char *method = w->desired_matrix->method;
12780 int len = strlen (method);
12781 int size = sizeof w->desired_matrix->method;
12782 int remaining = size - len - 1;
12783 va_list ap;
12784
12785 if (len && remaining)
12786 {
12787 method[len] = '|';
12788 --remaining, ++len;
12789 }
12790
12791 va_start (ap, fmt);
12792 vsnprintf (method + len, remaining + 1, fmt, ap);
12793 va_end (ap);
12794
12795 if (trace_redisplay_p)
12796 fprintf (stderr, "%p (%s): %s\n",
12797 ptr,
12798 ((BUFFERP (w->contents)
12799 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12800 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12801 : "no buffer"),
12802 method + len);
12803 }
12804
12805 #endif /* GLYPH_DEBUG */
12806
12807
12808 /* Value is non-zero if all changes in window W, which displays
12809 current_buffer, are in the text between START and END. START is a
12810 buffer position, END is given as a distance from Z. Used in
12811 redisplay_internal for display optimization. */
12812
12813 static int
12814 text_outside_line_unchanged_p (struct window *w,
12815 ptrdiff_t start, ptrdiff_t end)
12816 {
12817 int unchanged_p = 1;
12818
12819 /* If text or overlays have changed, see where. */
12820 if (window_outdated (w))
12821 {
12822 /* Gap in the line? */
12823 if (GPT < start || Z - GPT < end)
12824 unchanged_p = 0;
12825
12826 /* Changes start in front of the line, or end after it? */
12827 if (unchanged_p
12828 && (BEG_UNCHANGED < start - 1
12829 || END_UNCHANGED < end))
12830 unchanged_p = 0;
12831
12832 /* If selective display, can't optimize if changes start at the
12833 beginning of the line. */
12834 if (unchanged_p
12835 && INTEGERP (BVAR (current_buffer, selective_display))
12836 && XINT (BVAR (current_buffer, selective_display)) > 0
12837 && (BEG_UNCHANGED < start || GPT <= start))
12838 unchanged_p = 0;
12839
12840 /* If there are overlays at the start or end of the line, these
12841 may have overlay strings with newlines in them. A change at
12842 START, for instance, may actually concern the display of such
12843 overlay strings as well, and they are displayed on different
12844 lines. So, quickly rule out this case. (For the future, it
12845 might be desirable to implement something more telling than
12846 just BEG/END_UNCHANGED.) */
12847 if (unchanged_p)
12848 {
12849 if (BEG + BEG_UNCHANGED == start
12850 && overlay_touches_p (start))
12851 unchanged_p = 0;
12852 if (END_UNCHANGED == end
12853 && overlay_touches_p (Z - end))
12854 unchanged_p = 0;
12855 }
12856
12857 /* Under bidi reordering, adding or deleting a character in the
12858 beginning of a paragraph, before the first strong directional
12859 character, can change the base direction of the paragraph (unless
12860 the buffer specifies a fixed paragraph direction), which will
12861 require to redisplay the whole paragraph. It might be worthwhile
12862 to find the paragraph limits and widen the range of redisplayed
12863 lines to that, but for now just give up this optimization. */
12864 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12865 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12866 unchanged_p = 0;
12867 }
12868
12869 return unchanged_p;
12870 }
12871
12872
12873 /* Do a frame update, taking possible shortcuts into account. This is
12874 the main external entry point for redisplay.
12875
12876 If the last redisplay displayed an echo area message and that message
12877 is no longer requested, we clear the echo area or bring back the
12878 mini-buffer if that is in use. */
12879
12880 void
12881 redisplay (void)
12882 {
12883 redisplay_internal ();
12884 }
12885
12886
12887 static Lisp_Object
12888 overlay_arrow_string_or_property (Lisp_Object var)
12889 {
12890 Lisp_Object val;
12891
12892 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12893 return val;
12894
12895 return Voverlay_arrow_string;
12896 }
12897
12898 /* Return 1 if there are any overlay-arrows in current_buffer. */
12899 static int
12900 overlay_arrow_in_current_buffer_p (void)
12901 {
12902 Lisp_Object vlist;
12903
12904 for (vlist = Voverlay_arrow_variable_list;
12905 CONSP (vlist);
12906 vlist = XCDR (vlist))
12907 {
12908 Lisp_Object var = XCAR (vlist);
12909 Lisp_Object val;
12910
12911 if (!SYMBOLP (var))
12912 continue;
12913 val = find_symbol_value (var);
12914 if (MARKERP (val)
12915 && current_buffer == XMARKER (val)->buffer)
12916 return 1;
12917 }
12918 return 0;
12919 }
12920
12921
12922 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12923 has changed. */
12924
12925 static int
12926 overlay_arrows_changed_p (void)
12927 {
12928 Lisp_Object vlist;
12929
12930 for (vlist = Voverlay_arrow_variable_list;
12931 CONSP (vlist);
12932 vlist = XCDR (vlist))
12933 {
12934 Lisp_Object var = XCAR (vlist);
12935 Lisp_Object val, pstr;
12936
12937 if (!SYMBOLP (var))
12938 continue;
12939 val = find_symbol_value (var);
12940 if (!MARKERP (val))
12941 continue;
12942 if (! EQ (COERCE_MARKER (val),
12943 Fget (var, Qlast_arrow_position))
12944 || ! (pstr = overlay_arrow_string_or_property (var),
12945 EQ (pstr, Fget (var, Qlast_arrow_string))))
12946 return 1;
12947 }
12948 return 0;
12949 }
12950
12951 /* Mark overlay arrows to be updated on next redisplay. */
12952
12953 static void
12954 update_overlay_arrows (int up_to_date)
12955 {
12956 Lisp_Object vlist;
12957
12958 for (vlist = Voverlay_arrow_variable_list;
12959 CONSP (vlist);
12960 vlist = XCDR (vlist))
12961 {
12962 Lisp_Object var = XCAR (vlist);
12963
12964 if (!SYMBOLP (var))
12965 continue;
12966
12967 if (up_to_date > 0)
12968 {
12969 Lisp_Object val = find_symbol_value (var);
12970 Fput (var, Qlast_arrow_position,
12971 COERCE_MARKER (val));
12972 Fput (var, Qlast_arrow_string,
12973 overlay_arrow_string_or_property (var));
12974 }
12975 else if (up_to_date < 0
12976 || !NILP (Fget (var, Qlast_arrow_position)))
12977 {
12978 Fput (var, Qlast_arrow_position, Qt);
12979 Fput (var, Qlast_arrow_string, Qt);
12980 }
12981 }
12982 }
12983
12984
12985 /* Return overlay arrow string to display at row.
12986 Return integer (bitmap number) for arrow bitmap in left fringe.
12987 Return nil if no overlay arrow. */
12988
12989 static Lisp_Object
12990 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12991 {
12992 Lisp_Object vlist;
12993
12994 for (vlist = Voverlay_arrow_variable_list;
12995 CONSP (vlist);
12996 vlist = XCDR (vlist))
12997 {
12998 Lisp_Object var = XCAR (vlist);
12999 Lisp_Object val;
13000
13001 if (!SYMBOLP (var))
13002 continue;
13003
13004 val = find_symbol_value (var);
13005
13006 if (MARKERP (val)
13007 && current_buffer == XMARKER (val)->buffer
13008 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13009 {
13010 if (FRAME_WINDOW_P (it->f)
13011 /* FIXME: if ROW->reversed_p is set, this should test
13012 the right fringe, not the left one. */
13013 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13014 {
13015 #ifdef HAVE_WINDOW_SYSTEM
13016 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13017 {
13018 int fringe_bitmap;
13019 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13020 return make_number (fringe_bitmap);
13021 }
13022 #endif
13023 return make_number (-1); /* Use default arrow bitmap. */
13024 }
13025 return overlay_arrow_string_or_property (var);
13026 }
13027 }
13028
13029 return Qnil;
13030 }
13031
13032 /* Return 1 if point moved out of or into a composition. Otherwise
13033 return 0. PREV_BUF and PREV_PT are the last point buffer and
13034 position. BUF and PT are the current point buffer and position. */
13035
13036 static int
13037 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13038 struct buffer *buf, ptrdiff_t pt)
13039 {
13040 ptrdiff_t start, end;
13041 Lisp_Object prop;
13042 Lisp_Object buffer;
13043
13044 XSETBUFFER (buffer, buf);
13045 /* Check a composition at the last point if point moved within the
13046 same buffer. */
13047 if (prev_buf == buf)
13048 {
13049 if (prev_pt == pt)
13050 /* Point didn't move. */
13051 return 0;
13052
13053 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13054 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13055 && composition_valid_p (start, end, prop)
13056 && start < prev_pt && end > prev_pt)
13057 /* The last point was within the composition. Return 1 iff
13058 point moved out of the composition. */
13059 return (pt <= start || pt >= end);
13060 }
13061
13062 /* Check a composition at the current point. */
13063 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13064 && find_composition (pt, -1, &start, &end, &prop, buffer)
13065 && composition_valid_p (start, end, prop)
13066 && start < pt && end > pt);
13067 }
13068
13069 /* Reconsider the clip changes of buffer which is displayed in W. */
13070
13071 static void
13072 reconsider_clip_changes (struct window *w)
13073 {
13074 struct buffer *b = XBUFFER (w->contents);
13075
13076 if (b->clip_changed
13077 && w->window_end_valid
13078 && w->current_matrix->buffer == b
13079 && w->current_matrix->zv == BUF_ZV (b)
13080 && w->current_matrix->begv == BUF_BEGV (b))
13081 b->clip_changed = 0;
13082
13083 /* If display wasn't paused, and W is not a tool bar window, see if
13084 point has been moved into or out of a composition. In that case,
13085 we set b->clip_changed to 1 to force updating the screen. If
13086 b->clip_changed has already been set to 1, we can skip this
13087 check. */
13088 if (!b->clip_changed && w->window_end_valid)
13089 {
13090 ptrdiff_t pt = (w == XWINDOW (selected_window)
13091 ? PT : marker_position (w->pointm));
13092
13093 if ((w->current_matrix->buffer != b || pt != w->last_point)
13094 && check_point_in_composition (w->current_matrix->buffer,
13095 w->last_point, b, pt))
13096 b->clip_changed = 1;
13097 }
13098 }
13099
13100 static void
13101 propagate_buffer_redisplay (void)
13102 { /* Resetting b->text->redisplay is problematic!
13103 We can't just reset it in the case that some window that displays
13104 it has not been redisplayed; and such a window can stay
13105 unredisplayed for a long time if it's currently invisible.
13106 But we do want to reset it at the end of redisplay otherwise
13107 its displayed windows will keep being redisplayed over and over
13108 again.
13109 So we copy all b->text->redisplay flags up to their windows here,
13110 such that mark_window_display_accurate can safely reset
13111 b->text->redisplay. */
13112 Lisp_Object ws = window_list ();
13113 for (; CONSP (ws); ws = XCDR (ws))
13114 {
13115 struct window *thisw = XWINDOW (XCAR (ws));
13116 struct buffer *thisb = XBUFFER (thisw->contents);
13117 if (thisb->text->redisplay)
13118 thisw->redisplay = true;
13119 }
13120 }
13121
13122 #define STOP_POLLING \
13123 do { if (! polling_stopped_here) stop_polling (); \
13124 polling_stopped_here = 1; } while (0)
13125
13126 #define RESUME_POLLING \
13127 do { if (polling_stopped_here) start_polling (); \
13128 polling_stopped_here = 0; } while (0)
13129
13130
13131 /* Perhaps in the future avoid recentering windows if it
13132 is not necessary; currently that causes some problems. */
13133
13134 static void
13135 redisplay_internal (void)
13136 {
13137 struct window *w = XWINDOW (selected_window);
13138 struct window *sw;
13139 struct frame *fr;
13140 int pending;
13141 bool must_finish = 0, match_p;
13142 struct text_pos tlbufpos, tlendpos;
13143 int number_of_visible_frames;
13144 ptrdiff_t count;
13145 struct frame *sf;
13146 int polling_stopped_here = 0;
13147 Lisp_Object tail, frame;
13148
13149 /* True means redisplay has to consider all windows on all
13150 frames. False, only selected_window is considered. */
13151 bool consider_all_windows_p;
13152
13153 /* True means redisplay has to redisplay the miniwindow. */
13154 bool update_miniwindow_p = false;
13155
13156 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13157
13158 /* No redisplay if running in batch mode or frame is not yet fully
13159 initialized, or redisplay is explicitly turned off by setting
13160 Vinhibit_redisplay. */
13161 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13162 || !NILP (Vinhibit_redisplay))
13163 return;
13164
13165 /* Don't examine these until after testing Vinhibit_redisplay.
13166 When Emacs is shutting down, perhaps because its connection to
13167 X has dropped, we should not look at them at all. */
13168 fr = XFRAME (w->frame);
13169 sf = SELECTED_FRAME ();
13170
13171 if (!fr->glyphs_initialized_p)
13172 return;
13173
13174 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13175 if (popup_activated ())
13176 return;
13177 #endif
13178
13179 /* I don't think this happens but let's be paranoid. */
13180 if (redisplaying_p)
13181 return;
13182
13183 /* Record a function that clears redisplaying_p
13184 when we leave this function. */
13185 count = SPECPDL_INDEX ();
13186 record_unwind_protect_void (unwind_redisplay);
13187 redisplaying_p = 1;
13188 specbind (Qinhibit_free_realized_faces, Qnil);
13189
13190 /* Record this function, so it appears on the profiler's backtraces. */
13191 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13192
13193 FOR_EACH_FRAME (tail, frame)
13194 XFRAME (frame)->already_hscrolled_p = 0;
13195
13196 retry:
13197 /* Remember the currently selected window. */
13198 sw = w;
13199
13200 pending = 0;
13201 last_escape_glyph_frame = NULL;
13202 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13203 last_glyphless_glyph_frame = NULL;
13204 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13205
13206 /* If face_change_count is non-zero, init_iterator will free all
13207 realized faces, which includes the faces referenced from current
13208 matrices. So, we can't reuse current matrices in this case. */
13209 if (face_change_count)
13210 windows_or_buffers_changed = 47;
13211
13212 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13213 && FRAME_TTY (sf)->previous_frame != sf)
13214 {
13215 /* Since frames on a single ASCII terminal share the same
13216 display area, displaying a different frame means redisplay
13217 the whole thing. */
13218 SET_FRAME_GARBAGED (sf);
13219 #ifndef DOS_NT
13220 set_tty_color_mode (FRAME_TTY (sf), sf);
13221 #endif
13222 FRAME_TTY (sf)->previous_frame = sf;
13223 }
13224
13225 /* Set the visible flags for all frames. Do this before checking for
13226 resized or garbaged frames; they want to know if their frames are
13227 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13228 number_of_visible_frames = 0;
13229
13230 FOR_EACH_FRAME (tail, frame)
13231 {
13232 struct frame *f = XFRAME (frame);
13233
13234 if (FRAME_VISIBLE_P (f))
13235 {
13236 ++number_of_visible_frames;
13237 /* Adjust matrices for visible frames only. */
13238 if (f->fonts_changed)
13239 {
13240 adjust_frame_glyphs (f);
13241 f->fonts_changed = 0;
13242 }
13243 /* If cursor type has been changed on the frame
13244 other than selected, consider all frames. */
13245 if (f != sf && f->cursor_type_changed)
13246 update_mode_lines = 31;
13247 }
13248 clear_desired_matrices (f);
13249 }
13250
13251 /* Notice any pending interrupt request to change frame size. */
13252 do_pending_window_change (1);
13253
13254 /* do_pending_window_change could change the selected_window due to
13255 frame resizing which makes the selected window too small. */
13256 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13257 sw = w;
13258
13259 /* Clear frames marked as garbaged. */
13260 clear_garbaged_frames ();
13261
13262 /* Build menubar and tool-bar items. */
13263 if (NILP (Vmemory_full))
13264 prepare_menu_bars ();
13265
13266 reconsider_clip_changes (w);
13267
13268 /* In most cases selected window displays current buffer. */
13269 match_p = XBUFFER (w->contents) == current_buffer;
13270 if (match_p)
13271 {
13272 /* Detect case that we need to write or remove a star in the mode line. */
13273 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13274 w->update_mode_line = 1;
13275
13276 if (mode_line_update_needed (w))
13277 w->update_mode_line = 1;
13278 }
13279
13280 /* Normally the message* functions will have already displayed and
13281 updated the echo area, but the frame may have been trashed, or
13282 the update may have been preempted, so display the echo area
13283 again here. Checking message_cleared_p captures the case that
13284 the echo area should be cleared. */
13285 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13286 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13287 || (message_cleared_p
13288 && minibuf_level == 0
13289 /* If the mini-window is currently selected, this means the
13290 echo-area doesn't show through. */
13291 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13292 {
13293 int window_height_changed_p = echo_area_display (0);
13294
13295 if (message_cleared_p)
13296 update_miniwindow_p = true;
13297
13298 must_finish = 1;
13299
13300 /* If we don't display the current message, don't clear the
13301 message_cleared_p flag, because, if we did, we wouldn't clear
13302 the echo area in the next redisplay which doesn't preserve
13303 the echo area. */
13304 if (!display_last_displayed_message_p)
13305 message_cleared_p = 0;
13306
13307 if (window_height_changed_p)
13308 {
13309 windows_or_buffers_changed = 50;
13310
13311 /* If window configuration was changed, frames may have been
13312 marked garbaged. Clear them or we will experience
13313 surprises wrt scrolling. */
13314 clear_garbaged_frames ();
13315 }
13316 }
13317 else if (EQ (selected_window, minibuf_window)
13318 && (current_buffer->clip_changed || window_outdated (w))
13319 && resize_mini_window (w, 0))
13320 {
13321 /* Resized active mini-window to fit the size of what it is
13322 showing if its contents might have changed. */
13323 must_finish = 1;
13324
13325 /* If window configuration was changed, frames may have been
13326 marked garbaged. Clear them or we will experience
13327 surprises wrt scrolling. */
13328 clear_garbaged_frames ();
13329 }
13330
13331 if (windows_or_buffers_changed && !update_mode_lines)
13332 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13333 only the windows's contents needs to be refreshed, or whether the
13334 mode-lines also need a refresh. */
13335 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13336 ? REDISPLAY_SOME : 32);
13337
13338 /* If specs for an arrow have changed, do thorough redisplay
13339 to ensure we remove any arrow that should no longer exist. */
13340 if (overlay_arrows_changed_p ())
13341 /* Apparently, this is the only case where we update other windows,
13342 without updating other mode-lines. */
13343 windows_or_buffers_changed = 49;
13344
13345 consider_all_windows_p = (update_mode_lines
13346 || windows_or_buffers_changed);
13347
13348 #define AINC(a,i) \
13349 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13350 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13351
13352 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13353 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13354
13355 /* Optimize the case that only the line containing the cursor in the
13356 selected window has changed. Variables starting with this_ are
13357 set in display_line and record information about the line
13358 containing the cursor. */
13359 tlbufpos = this_line_start_pos;
13360 tlendpos = this_line_end_pos;
13361 if (!consider_all_windows_p
13362 && CHARPOS (tlbufpos) > 0
13363 && !w->update_mode_line
13364 && !current_buffer->clip_changed
13365 && !current_buffer->prevent_redisplay_optimizations_p
13366 && FRAME_VISIBLE_P (XFRAME (w->frame))
13367 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13368 && !XFRAME (w->frame)->cursor_type_changed
13369 /* Make sure recorded data applies to current buffer, etc. */
13370 && this_line_buffer == current_buffer
13371 && match_p
13372 && !w->force_start
13373 && !w->optional_new_start
13374 /* Point must be on the line that we have info recorded about. */
13375 && PT >= CHARPOS (tlbufpos)
13376 && PT <= Z - CHARPOS (tlendpos)
13377 /* All text outside that line, including its final newline,
13378 must be unchanged. */
13379 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13380 CHARPOS (tlendpos)))
13381 {
13382 if (CHARPOS (tlbufpos) > BEGV
13383 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13384 && (CHARPOS (tlbufpos) == ZV
13385 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13386 /* Former continuation line has disappeared by becoming empty. */
13387 goto cancel;
13388 else if (window_outdated (w) || MINI_WINDOW_P (w))
13389 {
13390 /* We have to handle the case of continuation around a
13391 wide-column character (see the comment in indent.c around
13392 line 1340).
13393
13394 For instance, in the following case:
13395
13396 -------- Insert --------
13397 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13398 J_I_ ==> J_I_ `^^' are cursors.
13399 ^^ ^^
13400 -------- --------
13401
13402 As we have to redraw the line above, we cannot use this
13403 optimization. */
13404
13405 struct it it;
13406 int line_height_before = this_line_pixel_height;
13407
13408 /* Note that start_display will handle the case that the
13409 line starting at tlbufpos is a continuation line. */
13410 start_display (&it, w, tlbufpos);
13411
13412 /* Implementation note: It this still necessary? */
13413 if (it.current_x != this_line_start_x)
13414 goto cancel;
13415
13416 TRACE ((stderr, "trying display optimization 1\n"));
13417 w->cursor.vpos = -1;
13418 overlay_arrow_seen = 0;
13419 it.vpos = this_line_vpos;
13420 it.current_y = this_line_y;
13421 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13422 display_line (&it);
13423
13424 /* If line contains point, is not continued,
13425 and ends at same distance from eob as before, we win. */
13426 if (w->cursor.vpos >= 0
13427 /* Line is not continued, otherwise this_line_start_pos
13428 would have been set to 0 in display_line. */
13429 && CHARPOS (this_line_start_pos)
13430 /* Line ends as before. */
13431 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13432 /* Line has same height as before. Otherwise other lines
13433 would have to be shifted up or down. */
13434 && this_line_pixel_height == line_height_before)
13435 {
13436 /* If this is not the window's last line, we must adjust
13437 the charstarts of the lines below. */
13438 if (it.current_y < it.last_visible_y)
13439 {
13440 struct glyph_row *row
13441 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13442 ptrdiff_t delta, delta_bytes;
13443
13444 /* We used to distinguish between two cases here,
13445 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13446 when the line ends in a newline or the end of the
13447 buffer's accessible portion. But both cases did
13448 the same, so they were collapsed. */
13449 delta = (Z
13450 - CHARPOS (tlendpos)
13451 - MATRIX_ROW_START_CHARPOS (row));
13452 delta_bytes = (Z_BYTE
13453 - BYTEPOS (tlendpos)
13454 - MATRIX_ROW_START_BYTEPOS (row));
13455
13456 increment_matrix_positions (w->current_matrix,
13457 this_line_vpos + 1,
13458 w->current_matrix->nrows,
13459 delta, delta_bytes);
13460 }
13461
13462 /* If this row displays text now but previously didn't,
13463 or vice versa, w->window_end_vpos may have to be
13464 adjusted. */
13465 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13466 {
13467 if (w->window_end_vpos < this_line_vpos)
13468 w->window_end_vpos = this_line_vpos;
13469 }
13470 else if (w->window_end_vpos == this_line_vpos
13471 && this_line_vpos > 0)
13472 w->window_end_vpos = this_line_vpos - 1;
13473 w->window_end_valid = 0;
13474
13475 /* Update hint: No need to try to scroll in update_window. */
13476 w->desired_matrix->no_scrolling_p = 1;
13477
13478 #ifdef GLYPH_DEBUG
13479 *w->desired_matrix->method = 0;
13480 debug_method_add (w, "optimization 1");
13481 #endif
13482 #ifdef HAVE_WINDOW_SYSTEM
13483 update_window_fringes (w, 0);
13484 #endif
13485 goto update;
13486 }
13487 else
13488 goto cancel;
13489 }
13490 else if (/* Cursor position hasn't changed. */
13491 PT == w->last_point
13492 /* Make sure the cursor was last displayed
13493 in this window. Otherwise we have to reposition it. */
13494
13495 /* PXW: Must be pixelized, probably. */
13496 && 0 <= w->cursor.vpos
13497 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13498 {
13499 if (!must_finish)
13500 {
13501 do_pending_window_change (1);
13502 /* If selected_window changed, redisplay again. */
13503 if (WINDOWP (selected_window)
13504 && (w = XWINDOW (selected_window)) != sw)
13505 goto retry;
13506
13507 /* We used to always goto end_of_redisplay here, but this
13508 isn't enough if we have a blinking cursor. */
13509 if (w->cursor_off_p == w->last_cursor_off_p)
13510 goto end_of_redisplay;
13511 }
13512 goto update;
13513 }
13514 /* If highlighting the region, or if the cursor is in the echo area,
13515 then we can't just move the cursor. */
13516 else if (NILP (Vshow_trailing_whitespace)
13517 && !cursor_in_echo_area)
13518 {
13519 struct it it;
13520 struct glyph_row *row;
13521
13522 /* Skip from tlbufpos to PT and see where it is. Note that
13523 PT may be in invisible text. If so, we will end at the
13524 next visible position. */
13525 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13526 NULL, DEFAULT_FACE_ID);
13527 it.current_x = this_line_start_x;
13528 it.current_y = this_line_y;
13529 it.vpos = this_line_vpos;
13530
13531 /* The call to move_it_to stops in front of PT, but
13532 moves over before-strings. */
13533 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13534
13535 if (it.vpos == this_line_vpos
13536 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13537 row->enabled_p))
13538 {
13539 eassert (this_line_vpos == it.vpos);
13540 eassert (this_line_y == it.current_y);
13541 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13542 #ifdef GLYPH_DEBUG
13543 *w->desired_matrix->method = 0;
13544 debug_method_add (w, "optimization 3");
13545 #endif
13546 goto update;
13547 }
13548 else
13549 goto cancel;
13550 }
13551
13552 cancel:
13553 /* Text changed drastically or point moved off of line. */
13554 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13555 }
13556
13557 CHARPOS (this_line_start_pos) = 0;
13558 ++clear_face_cache_count;
13559 #ifdef HAVE_WINDOW_SYSTEM
13560 ++clear_image_cache_count;
13561 #endif
13562
13563 /* Build desired matrices, and update the display. If
13564 consider_all_windows_p is non-zero, do it for all windows on all
13565 frames. Otherwise do it for selected_window, only. */
13566
13567 if (consider_all_windows_p)
13568 {
13569 FOR_EACH_FRAME (tail, frame)
13570 XFRAME (frame)->updated_p = 0;
13571
13572 propagate_buffer_redisplay ();
13573
13574 FOR_EACH_FRAME (tail, frame)
13575 {
13576 struct frame *f = XFRAME (frame);
13577
13578 /* We don't have to do anything for unselected terminal
13579 frames. */
13580 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13581 && !EQ (FRAME_TTY (f)->top_frame, frame))
13582 continue;
13583
13584 retry_frame:
13585
13586 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13587 {
13588 bool gcscrollbars
13589 /* Only GC scollbars when we redisplay the whole frame. */
13590 = f->redisplay || !REDISPLAY_SOME_P ();
13591 /* Mark all the scroll bars to be removed; we'll redeem
13592 the ones we want when we redisplay their windows. */
13593 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13594 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13595
13596 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13597 redisplay_windows (FRAME_ROOT_WINDOW (f));
13598 /* Remember that the invisible frames need to be redisplayed next
13599 time they're visible. */
13600 else if (!REDISPLAY_SOME_P ())
13601 f->redisplay = true;
13602
13603 /* The X error handler may have deleted that frame. */
13604 if (!FRAME_LIVE_P (f))
13605 continue;
13606
13607 /* Any scroll bars which redisplay_windows should have
13608 nuked should now go away. */
13609 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13610 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13611
13612 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13613 {
13614 /* If fonts changed on visible frame, display again. */
13615 if (f->fonts_changed)
13616 {
13617 adjust_frame_glyphs (f);
13618 f->fonts_changed = 0;
13619 goto retry_frame;
13620 }
13621
13622 /* See if we have to hscroll. */
13623 if (!f->already_hscrolled_p)
13624 {
13625 f->already_hscrolled_p = 1;
13626 if (hscroll_windows (f->root_window))
13627 goto retry_frame;
13628 }
13629
13630 /* Prevent various kinds of signals during display
13631 update. stdio is not robust about handling
13632 signals, which can cause an apparent I/O error. */
13633 if (interrupt_input)
13634 unrequest_sigio ();
13635 STOP_POLLING;
13636
13637 pending |= update_frame (f, 0, 0);
13638 f->cursor_type_changed = 0;
13639 f->updated_p = 1;
13640 }
13641 }
13642 }
13643
13644 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13645
13646 if (!pending)
13647 {
13648 /* Do the mark_window_display_accurate after all windows have
13649 been redisplayed because this call resets flags in buffers
13650 which are needed for proper redisplay. */
13651 FOR_EACH_FRAME (tail, frame)
13652 {
13653 struct frame *f = XFRAME (frame);
13654 if (f->updated_p)
13655 {
13656 f->redisplay = false;
13657 mark_window_display_accurate (f->root_window, 1);
13658 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13659 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13660 }
13661 }
13662 }
13663 }
13664 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13665 {
13666 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13667 struct frame *mini_frame;
13668
13669 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13670 /* Use list_of_error, not Qerror, so that
13671 we catch only errors and don't run the debugger. */
13672 internal_condition_case_1 (redisplay_window_1, selected_window,
13673 list_of_error,
13674 redisplay_window_error);
13675 if (update_miniwindow_p)
13676 internal_condition_case_1 (redisplay_window_1, mini_window,
13677 list_of_error,
13678 redisplay_window_error);
13679
13680 /* Compare desired and current matrices, perform output. */
13681
13682 update:
13683 /* If fonts changed, display again. */
13684 if (sf->fonts_changed)
13685 goto retry;
13686
13687 /* Prevent various kinds of signals during display update.
13688 stdio is not robust about handling signals,
13689 which can cause an apparent I/O error. */
13690 if (interrupt_input)
13691 unrequest_sigio ();
13692 STOP_POLLING;
13693
13694 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13695 {
13696 if (hscroll_windows (selected_window))
13697 goto retry;
13698
13699 XWINDOW (selected_window)->must_be_updated_p = true;
13700 pending = update_frame (sf, 0, 0);
13701 sf->cursor_type_changed = 0;
13702 }
13703
13704 /* We may have called echo_area_display at the top of this
13705 function. If the echo area is on another frame, that may
13706 have put text on a frame other than the selected one, so the
13707 above call to update_frame would not have caught it. Catch
13708 it here. */
13709 mini_window = FRAME_MINIBUF_WINDOW (sf);
13710 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13711
13712 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13713 {
13714 XWINDOW (mini_window)->must_be_updated_p = true;
13715 pending |= update_frame (mini_frame, 0, 0);
13716 mini_frame->cursor_type_changed = 0;
13717 if (!pending && hscroll_windows (mini_window))
13718 goto retry;
13719 }
13720 }
13721
13722 /* If display was paused because of pending input, make sure we do a
13723 thorough update the next time. */
13724 if (pending)
13725 {
13726 /* Prevent the optimization at the beginning of
13727 redisplay_internal that tries a single-line update of the
13728 line containing the cursor in the selected window. */
13729 CHARPOS (this_line_start_pos) = 0;
13730
13731 /* Let the overlay arrow be updated the next time. */
13732 update_overlay_arrows (0);
13733
13734 /* If we pause after scrolling, some rows in the current
13735 matrices of some windows are not valid. */
13736 if (!WINDOW_FULL_WIDTH_P (w)
13737 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13738 update_mode_lines = 36;
13739 }
13740 else
13741 {
13742 if (!consider_all_windows_p)
13743 {
13744 /* This has already been done above if
13745 consider_all_windows_p is set. */
13746 if (XBUFFER (w->contents)->text->redisplay
13747 && buffer_window_count (XBUFFER (w->contents)) > 1)
13748 /* This can happen if b->text->redisplay was set during
13749 jit-lock. */
13750 propagate_buffer_redisplay ();
13751 mark_window_display_accurate_1 (w, 1);
13752
13753 /* Say overlay arrows are up to date. */
13754 update_overlay_arrows (1);
13755
13756 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13757 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13758 }
13759
13760 update_mode_lines = 0;
13761 windows_or_buffers_changed = 0;
13762 }
13763
13764 /* Start SIGIO interrupts coming again. Having them off during the
13765 code above makes it less likely one will discard output, but not
13766 impossible, since there might be stuff in the system buffer here.
13767 But it is much hairier to try to do anything about that. */
13768 if (interrupt_input)
13769 request_sigio ();
13770 RESUME_POLLING;
13771
13772 /* If a frame has become visible which was not before, redisplay
13773 again, so that we display it. Expose events for such a frame
13774 (which it gets when becoming visible) don't call the parts of
13775 redisplay constructing glyphs, so simply exposing a frame won't
13776 display anything in this case. So, we have to display these
13777 frames here explicitly. */
13778 if (!pending)
13779 {
13780 int new_count = 0;
13781
13782 FOR_EACH_FRAME (tail, frame)
13783 {
13784 if (XFRAME (frame)->visible)
13785 new_count++;
13786 }
13787
13788 if (new_count != number_of_visible_frames)
13789 windows_or_buffers_changed = 52;
13790 }
13791
13792 /* Change frame size now if a change is pending. */
13793 do_pending_window_change (1);
13794
13795 /* If we just did a pending size change, or have additional
13796 visible frames, or selected_window changed, redisplay again. */
13797 if ((windows_or_buffers_changed && !pending)
13798 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13799 goto retry;
13800
13801 /* Clear the face and image caches.
13802
13803 We used to do this only if consider_all_windows_p. But the cache
13804 needs to be cleared if a timer creates images in the current
13805 buffer (e.g. the test case in Bug#6230). */
13806
13807 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13808 {
13809 clear_face_cache (0);
13810 clear_face_cache_count = 0;
13811 }
13812
13813 #ifdef HAVE_WINDOW_SYSTEM
13814 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13815 {
13816 clear_image_caches (Qnil);
13817 clear_image_cache_count = 0;
13818 }
13819 #endif /* HAVE_WINDOW_SYSTEM */
13820
13821 end_of_redisplay:
13822 unbind_to (count, Qnil);
13823 RESUME_POLLING;
13824 }
13825
13826
13827 /* Redisplay, but leave alone any recent echo area message unless
13828 another message has been requested in its place.
13829
13830 This is useful in situations where you need to redisplay but no
13831 user action has occurred, making it inappropriate for the message
13832 area to be cleared. See tracking_off and
13833 wait_reading_process_output for examples of these situations.
13834
13835 FROM_WHERE is an integer saying from where this function was
13836 called. This is useful for debugging. */
13837
13838 void
13839 redisplay_preserve_echo_area (int from_where)
13840 {
13841 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13842
13843 if (!NILP (echo_area_buffer[1]))
13844 {
13845 /* We have a previously displayed message, but no current
13846 message. Redisplay the previous message. */
13847 display_last_displayed_message_p = 1;
13848 redisplay_internal ();
13849 display_last_displayed_message_p = 0;
13850 }
13851 else
13852 redisplay_internal ();
13853
13854 flush_frame (SELECTED_FRAME ());
13855 }
13856
13857
13858 /* Function registered with record_unwind_protect in redisplay_internal. */
13859
13860 static void
13861 unwind_redisplay (void)
13862 {
13863 redisplaying_p = 0;
13864 }
13865
13866
13867 /* Mark the display of leaf window W as accurate or inaccurate.
13868 If ACCURATE_P is non-zero mark display of W as accurate. If
13869 ACCURATE_P is zero, arrange for W to be redisplayed the next
13870 time redisplay_internal is called. */
13871
13872 static void
13873 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13874 {
13875 struct buffer *b = XBUFFER (w->contents);
13876
13877 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13878 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13879 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13880
13881 if (accurate_p)
13882 {
13883 b->clip_changed = false;
13884 b->prevent_redisplay_optimizations_p = false;
13885 eassert (buffer_window_count (b) > 0);
13886 /* Resetting b->text->redisplay is problematic!
13887 In order to make it safer to do it here, redisplay_internal must
13888 have copied all b->text->redisplay to their respective windows. */
13889 b->text->redisplay = false;
13890
13891 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13892 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13893 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13894 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13895
13896 w->current_matrix->buffer = b;
13897 w->current_matrix->begv = BUF_BEGV (b);
13898 w->current_matrix->zv = BUF_ZV (b);
13899
13900 w->last_cursor_vpos = w->cursor.vpos;
13901 w->last_cursor_off_p = w->cursor_off_p;
13902
13903 if (w == XWINDOW (selected_window))
13904 w->last_point = BUF_PT (b);
13905 else
13906 w->last_point = marker_position (w->pointm);
13907
13908 w->window_end_valid = true;
13909 w->update_mode_line = false;
13910 }
13911
13912 w->redisplay = !accurate_p;
13913 }
13914
13915
13916 /* Mark the display of windows in the window tree rooted at WINDOW as
13917 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13918 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13919 be redisplayed the next time redisplay_internal is called. */
13920
13921 void
13922 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13923 {
13924 struct window *w;
13925
13926 for (; !NILP (window); window = w->next)
13927 {
13928 w = XWINDOW (window);
13929 if (WINDOWP (w->contents))
13930 mark_window_display_accurate (w->contents, accurate_p);
13931 else
13932 mark_window_display_accurate_1 (w, accurate_p);
13933 }
13934
13935 if (accurate_p)
13936 update_overlay_arrows (1);
13937 else
13938 /* Force a thorough redisplay the next time by setting
13939 last_arrow_position and last_arrow_string to t, which is
13940 unequal to any useful value of Voverlay_arrow_... */
13941 update_overlay_arrows (-1);
13942 }
13943
13944
13945 /* Return value in display table DP (Lisp_Char_Table *) for character
13946 C. Since a display table doesn't have any parent, we don't have to
13947 follow parent. Do not call this function directly but use the
13948 macro DISP_CHAR_VECTOR. */
13949
13950 Lisp_Object
13951 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13952 {
13953 Lisp_Object val;
13954
13955 if (ASCII_CHAR_P (c))
13956 {
13957 val = dp->ascii;
13958 if (SUB_CHAR_TABLE_P (val))
13959 val = XSUB_CHAR_TABLE (val)->contents[c];
13960 }
13961 else
13962 {
13963 Lisp_Object table;
13964
13965 XSETCHAR_TABLE (table, dp);
13966 val = char_table_ref (table, c);
13967 }
13968 if (NILP (val))
13969 val = dp->defalt;
13970 return val;
13971 }
13972
13973
13974 \f
13975 /***********************************************************************
13976 Window Redisplay
13977 ***********************************************************************/
13978
13979 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13980
13981 static void
13982 redisplay_windows (Lisp_Object window)
13983 {
13984 while (!NILP (window))
13985 {
13986 struct window *w = XWINDOW (window);
13987
13988 if (WINDOWP (w->contents))
13989 redisplay_windows (w->contents);
13990 else if (BUFFERP (w->contents))
13991 {
13992 displayed_buffer = XBUFFER (w->contents);
13993 /* Use list_of_error, not Qerror, so that
13994 we catch only errors and don't run the debugger. */
13995 internal_condition_case_1 (redisplay_window_0, window,
13996 list_of_error,
13997 redisplay_window_error);
13998 }
13999
14000 window = w->next;
14001 }
14002 }
14003
14004 static Lisp_Object
14005 redisplay_window_error (Lisp_Object ignore)
14006 {
14007 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14008 return Qnil;
14009 }
14010
14011 static Lisp_Object
14012 redisplay_window_0 (Lisp_Object window)
14013 {
14014 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14015 redisplay_window (window, false);
14016 return Qnil;
14017 }
14018
14019 static Lisp_Object
14020 redisplay_window_1 (Lisp_Object window)
14021 {
14022 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14023 redisplay_window (window, true);
14024 return Qnil;
14025 }
14026 \f
14027
14028 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14029 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14030 which positions recorded in ROW differ from current buffer
14031 positions.
14032
14033 Return 0 if cursor is not on this row, 1 otherwise. */
14034
14035 static int
14036 set_cursor_from_row (struct window *w, struct glyph_row *row,
14037 struct glyph_matrix *matrix,
14038 ptrdiff_t delta, ptrdiff_t delta_bytes,
14039 int dy, int dvpos)
14040 {
14041 struct glyph *glyph = row->glyphs[TEXT_AREA];
14042 struct glyph *end = glyph + row->used[TEXT_AREA];
14043 struct glyph *cursor = NULL;
14044 /* The last known character position in row. */
14045 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14046 int x = row->x;
14047 ptrdiff_t pt_old = PT - delta;
14048 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14049 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14050 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14051 /* A glyph beyond the edge of TEXT_AREA which we should never
14052 touch. */
14053 struct glyph *glyphs_end = end;
14054 /* Non-zero means we've found a match for cursor position, but that
14055 glyph has the avoid_cursor_p flag set. */
14056 int match_with_avoid_cursor = 0;
14057 /* Non-zero means we've seen at least one glyph that came from a
14058 display string. */
14059 int string_seen = 0;
14060 /* Largest and smallest buffer positions seen so far during scan of
14061 glyph row. */
14062 ptrdiff_t bpos_max = pos_before;
14063 ptrdiff_t bpos_min = pos_after;
14064 /* Last buffer position covered by an overlay string with an integer
14065 `cursor' property. */
14066 ptrdiff_t bpos_covered = 0;
14067 /* Non-zero means the display string on which to display the cursor
14068 comes from a text property, not from an overlay. */
14069 int string_from_text_prop = 0;
14070
14071 /* Don't even try doing anything if called for a mode-line or
14072 header-line row, since the rest of the code isn't prepared to
14073 deal with such calamities. */
14074 eassert (!row->mode_line_p);
14075 if (row->mode_line_p)
14076 return 0;
14077
14078 /* Skip over glyphs not having an object at the start and the end of
14079 the row. These are special glyphs like truncation marks on
14080 terminal frames. */
14081 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14082 {
14083 if (!row->reversed_p)
14084 {
14085 while (glyph < end
14086 && INTEGERP (glyph->object)
14087 && glyph->charpos < 0)
14088 {
14089 x += glyph->pixel_width;
14090 ++glyph;
14091 }
14092 while (end > glyph
14093 && INTEGERP ((end - 1)->object)
14094 /* CHARPOS is zero for blanks and stretch glyphs
14095 inserted by extend_face_to_end_of_line. */
14096 && (end - 1)->charpos <= 0)
14097 --end;
14098 glyph_before = glyph - 1;
14099 glyph_after = end;
14100 }
14101 else
14102 {
14103 struct glyph *g;
14104
14105 /* If the glyph row is reversed, we need to process it from back
14106 to front, so swap the edge pointers. */
14107 glyphs_end = end = glyph - 1;
14108 glyph += row->used[TEXT_AREA] - 1;
14109
14110 while (glyph > end + 1
14111 && INTEGERP (glyph->object)
14112 && glyph->charpos < 0)
14113 {
14114 --glyph;
14115 x -= glyph->pixel_width;
14116 }
14117 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14118 --glyph;
14119 /* By default, in reversed rows we put the cursor on the
14120 rightmost (first in the reading order) glyph. */
14121 for (g = end + 1; g < glyph; g++)
14122 x += g->pixel_width;
14123 while (end < glyph
14124 && INTEGERP ((end + 1)->object)
14125 && (end + 1)->charpos <= 0)
14126 ++end;
14127 glyph_before = glyph + 1;
14128 glyph_after = end;
14129 }
14130 }
14131 else if (row->reversed_p)
14132 {
14133 /* In R2L rows that don't display text, put the cursor on the
14134 rightmost glyph. Case in point: an empty last line that is
14135 part of an R2L paragraph. */
14136 cursor = end - 1;
14137 /* Avoid placing the cursor on the last glyph of the row, where
14138 on terminal frames we hold the vertical border between
14139 adjacent windows. */
14140 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14141 && !WINDOW_RIGHTMOST_P (w)
14142 && cursor == row->glyphs[LAST_AREA] - 1)
14143 cursor--;
14144 x = -1; /* will be computed below, at label compute_x */
14145 }
14146
14147 /* Step 1: Try to find the glyph whose character position
14148 corresponds to point. If that's not possible, find 2 glyphs
14149 whose character positions are the closest to point, one before
14150 point, the other after it. */
14151 if (!row->reversed_p)
14152 while (/* not marched to end of glyph row */
14153 glyph < end
14154 /* glyph was not inserted by redisplay for internal purposes */
14155 && !INTEGERP (glyph->object))
14156 {
14157 if (BUFFERP (glyph->object))
14158 {
14159 ptrdiff_t dpos = glyph->charpos - pt_old;
14160
14161 if (glyph->charpos > bpos_max)
14162 bpos_max = glyph->charpos;
14163 if (glyph->charpos < bpos_min)
14164 bpos_min = glyph->charpos;
14165 if (!glyph->avoid_cursor_p)
14166 {
14167 /* If we hit point, we've found the glyph on which to
14168 display the cursor. */
14169 if (dpos == 0)
14170 {
14171 match_with_avoid_cursor = 0;
14172 break;
14173 }
14174 /* See if we've found a better approximation to
14175 POS_BEFORE or to POS_AFTER. */
14176 if (0 > dpos && dpos > pos_before - pt_old)
14177 {
14178 pos_before = glyph->charpos;
14179 glyph_before = glyph;
14180 }
14181 else if (0 < dpos && dpos < pos_after - pt_old)
14182 {
14183 pos_after = glyph->charpos;
14184 glyph_after = glyph;
14185 }
14186 }
14187 else if (dpos == 0)
14188 match_with_avoid_cursor = 1;
14189 }
14190 else if (STRINGP (glyph->object))
14191 {
14192 Lisp_Object chprop;
14193 ptrdiff_t glyph_pos = glyph->charpos;
14194
14195 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14196 glyph->object);
14197 if (!NILP (chprop))
14198 {
14199 /* If the string came from a `display' text property,
14200 look up the buffer position of that property and
14201 use that position to update bpos_max, as if we
14202 actually saw such a position in one of the row's
14203 glyphs. This helps with supporting integer values
14204 of `cursor' property on the display string in
14205 situations where most or all of the row's buffer
14206 text is completely covered by display properties,
14207 so that no glyph with valid buffer positions is
14208 ever seen in the row. */
14209 ptrdiff_t prop_pos =
14210 string_buffer_position_lim (glyph->object, pos_before,
14211 pos_after, 0);
14212
14213 if (prop_pos >= pos_before)
14214 bpos_max = prop_pos - 1;
14215 }
14216 if (INTEGERP (chprop))
14217 {
14218 bpos_covered = bpos_max + XINT (chprop);
14219 /* If the `cursor' property covers buffer positions up
14220 to and including point, we should display cursor on
14221 this glyph. Note that, if a `cursor' property on one
14222 of the string's characters has an integer value, we
14223 will break out of the loop below _before_ we get to
14224 the position match above. IOW, integer values of
14225 the `cursor' property override the "exact match for
14226 point" strategy of positioning the cursor. */
14227 /* Implementation note: bpos_max == pt_old when, e.g.,
14228 we are in an empty line, where bpos_max is set to
14229 MATRIX_ROW_START_CHARPOS, see above. */
14230 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14231 {
14232 cursor = glyph;
14233 break;
14234 }
14235 }
14236
14237 string_seen = 1;
14238 }
14239 x += glyph->pixel_width;
14240 ++glyph;
14241 }
14242 else if (glyph > end) /* row is reversed */
14243 while (!INTEGERP (glyph->object))
14244 {
14245 if (BUFFERP (glyph->object))
14246 {
14247 ptrdiff_t dpos = glyph->charpos - pt_old;
14248
14249 if (glyph->charpos > bpos_max)
14250 bpos_max = glyph->charpos;
14251 if (glyph->charpos < bpos_min)
14252 bpos_min = glyph->charpos;
14253 if (!glyph->avoid_cursor_p)
14254 {
14255 if (dpos == 0)
14256 {
14257 match_with_avoid_cursor = 0;
14258 break;
14259 }
14260 if (0 > dpos && dpos > pos_before - pt_old)
14261 {
14262 pos_before = glyph->charpos;
14263 glyph_before = glyph;
14264 }
14265 else if (0 < dpos && dpos < pos_after - pt_old)
14266 {
14267 pos_after = glyph->charpos;
14268 glyph_after = glyph;
14269 }
14270 }
14271 else if (dpos == 0)
14272 match_with_avoid_cursor = 1;
14273 }
14274 else if (STRINGP (glyph->object))
14275 {
14276 Lisp_Object chprop;
14277 ptrdiff_t glyph_pos = glyph->charpos;
14278
14279 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14280 glyph->object);
14281 if (!NILP (chprop))
14282 {
14283 ptrdiff_t prop_pos =
14284 string_buffer_position_lim (glyph->object, pos_before,
14285 pos_after, 0);
14286
14287 if (prop_pos >= pos_before)
14288 bpos_max = prop_pos - 1;
14289 }
14290 if (INTEGERP (chprop))
14291 {
14292 bpos_covered = bpos_max + XINT (chprop);
14293 /* If the `cursor' property covers buffer positions up
14294 to and including point, we should display cursor on
14295 this glyph. */
14296 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14297 {
14298 cursor = glyph;
14299 break;
14300 }
14301 }
14302 string_seen = 1;
14303 }
14304 --glyph;
14305 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14306 {
14307 x--; /* can't use any pixel_width */
14308 break;
14309 }
14310 x -= glyph->pixel_width;
14311 }
14312
14313 /* Step 2: If we didn't find an exact match for point, we need to
14314 look for a proper place to put the cursor among glyphs between
14315 GLYPH_BEFORE and GLYPH_AFTER. */
14316 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14317 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14318 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14319 {
14320 /* An empty line has a single glyph whose OBJECT is zero and
14321 whose CHARPOS is the position of a newline on that line.
14322 Note that on a TTY, there are more glyphs after that, which
14323 were produced by extend_face_to_end_of_line, but their
14324 CHARPOS is zero or negative. */
14325 int empty_line_p =
14326 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14327 && INTEGERP (glyph->object) && glyph->charpos > 0
14328 /* On a TTY, continued and truncated rows also have a glyph at
14329 their end whose OBJECT is zero and whose CHARPOS is
14330 positive (the continuation and truncation glyphs), but such
14331 rows are obviously not "empty". */
14332 && !(row->continued_p || row->truncated_on_right_p);
14333
14334 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14335 {
14336 ptrdiff_t ellipsis_pos;
14337
14338 /* Scan back over the ellipsis glyphs. */
14339 if (!row->reversed_p)
14340 {
14341 ellipsis_pos = (glyph - 1)->charpos;
14342 while (glyph > row->glyphs[TEXT_AREA]
14343 && (glyph - 1)->charpos == ellipsis_pos)
14344 glyph--, x -= glyph->pixel_width;
14345 /* That loop always goes one position too far, including
14346 the glyph before the ellipsis. So scan forward over
14347 that one. */
14348 x += glyph->pixel_width;
14349 glyph++;
14350 }
14351 else /* row is reversed */
14352 {
14353 ellipsis_pos = (glyph + 1)->charpos;
14354 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14355 && (glyph + 1)->charpos == ellipsis_pos)
14356 glyph++, x += glyph->pixel_width;
14357 x -= glyph->pixel_width;
14358 glyph--;
14359 }
14360 }
14361 else if (match_with_avoid_cursor)
14362 {
14363 cursor = glyph_after;
14364 x = -1;
14365 }
14366 else if (string_seen)
14367 {
14368 int incr = row->reversed_p ? -1 : +1;
14369
14370 /* Need to find the glyph that came out of a string which is
14371 present at point. That glyph is somewhere between
14372 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14373 positioned between POS_BEFORE and POS_AFTER in the
14374 buffer. */
14375 struct glyph *start, *stop;
14376 ptrdiff_t pos = pos_before;
14377
14378 x = -1;
14379
14380 /* If the row ends in a newline from a display string,
14381 reordering could have moved the glyphs belonging to the
14382 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14383 in this case we extend the search to the last glyph in
14384 the row that was not inserted by redisplay. */
14385 if (row->ends_in_newline_from_string_p)
14386 {
14387 glyph_after = end;
14388 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14389 }
14390
14391 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14392 correspond to POS_BEFORE and POS_AFTER, respectively. We
14393 need START and STOP in the order that corresponds to the
14394 row's direction as given by its reversed_p flag. If the
14395 directionality of characters between POS_BEFORE and
14396 POS_AFTER is the opposite of the row's base direction,
14397 these characters will have been reordered for display,
14398 and we need to reverse START and STOP. */
14399 if (!row->reversed_p)
14400 {
14401 start = min (glyph_before, glyph_after);
14402 stop = max (glyph_before, glyph_after);
14403 }
14404 else
14405 {
14406 start = max (glyph_before, glyph_after);
14407 stop = min (glyph_before, glyph_after);
14408 }
14409 for (glyph = start + incr;
14410 row->reversed_p ? glyph > stop : glyph < stop; )
14411 {
14412
14413 /* Any glyphs that come from the buffer are here because
14414 of bidi reordering. Skip them, and only pay
14415 attention to glyphs that came from some string. */
14416 if (STRINGP (glyph->object))
14417 {
14418 Lisp_Object str;
14419 ptrdiff_t tem;
14420 /* If the display property covers the newline, we
14421 need to search for it one position farther. */
14422 ptrdiff_t lim = pos_after
14423 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14424
14425 string_from_text_prop = 0;
14426 str = glyph->object;
14427 tem = string_buffer_position_lim (str, pos, lim, 0);
14428 if (tem == 0 /* from overlay */
14429 || pos <= tem)
14430 {
14431 /* If the string from which this glyph came is
14432 found in the buffer at point, or at position
14433 that is closer to point than pos_after, then
14434 we've found the glyph we've been looking for.
14435 If it comes from an overlay (tem == 0), and
14436 it has the `cursor' property on one of its
14437 glyphs, record that glyph as a candidate for
14438 displaying the cursor. (As in the
14439 unidirectional version, we will display the
14440 cursor on the last candidate we find.) */
14441 if (tem == 0
14442 || tem == pt_old
14443 || (tem - pt_old > 0 && tem < pos_after))
14444 {
14445 /* The glyphs from this string could have
14446 been reordered. Find the one with the
14447 smallest string position. Or there could
14448 be a character in the string with the
14449 `cursor' property, which means display
14450 cursor on that character's glyph. */
14451 ptrdiff_t strpos = glyph->charpos;
14452
14453 if (tem)
14454 {
14455 cursor = glyph;
14456 string_from_text_prop = 1;
14457 }
14458 for ( ;
14459 (row->reversed_p ? glyph > stop : glyph < stop)
14460 && EQ (glyph->object, str);
14461 glyph += incr)
14462 {
14463 Lisp_Object cprop;
14464 ptrdiff_t gpos = glyph->charpos;
14465
14466 cprop = Fget_char_property (make_number (gpos),
14467 Qcursor,
14468 glyph->object);
14469 if (!NILP (cprop))
14470 {
14471 cursor = glyph;
14472 break;
14473 }
14474 if (tem && glyph->charpos < strpos)
14475 {
14476 strpos = glyph->charpos;
14477 cursor = glyph;
14478 }
14479 }
14480
14481 if (tem == pt_old
14482 || (tem - pt_old > 0 && tem < pos_after))
14483 goto compute_x;
14484 }
14485 if (tem)
14486 pos = tem + 1; /* don't find previous instances */
14487 }
14488 /* This string is not what we want; skip all of the
14489 glyphs that came from it. */
14490 while ((row->reversed_p ? glyph > stop : glyph < stop)
14491 && EQ (glyph->object, str))
14492 glyph += incr;
14493 }
14494 else
14495 glyph += incr;
14496 }
14497
14498 /* If we reached the end of the line, and END was from a string,
14499 the cursor is not on this line. */
14500 if (cursor == NULL
14501 && (row->reversed_p ? glyph <= end : glyph >= end)
14502 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14503 && STRINGP (end->object)
14504 && row->continued_p)
14505 return 0;
14506 }
14507 /* A truncated row may not include PT among its character positions.
14508 Setting the cursor inside the scroll margin will trigger
14509 recalculation of hscroll in hscroll_window_tree. But if a
14510 display string covers point, defer to the string-handling
14511 code below to figure this out. */
14512 else if (row->truncated_on_left_p && pt_old < bpos_min)
14513 {
14514 cursor = glyph_before;
14515 x = -1;
14516 }
14517 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14518 /* Zero-width characters produce no glyphs. */
14519 || (!empty_line_p
14520 && (row->reversed_p
14521 ? glyph_after > glyphs_end
14522 : glyph_after < glyphs_end)))
14523 {
14524 cursor = glyph_after;
14525 x = -1;
14526 }
14527 }
14528
14529 compute_x:
14530 if (cursor != NULL)
14531 glyph = cursor;
14532 else if (glyph == glyphs_end
14533 && pos_before == pos_after
14534 && STRINGP ((row->reversed_p
14535 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14536 : row->glyphs[TEXT_AREA])->object))
14537 {
14538 /* If all the glyphs of this row came from strings, put the
14539 cursor on the first glyph of the row. This avoids having the
14540 cursor outside of the text area in this very rare and hard
14541 use case. */
14542 glyph =
14543 row->reversed_p
14544 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14545 : row->glyphs[TEXT_AREA];
14546 }
14547 if (x < 0)
14548 {
14549 struct glyph *g;
14550
14551 /* Need to compute x that corresponds to GLYPH. */
14552 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14553 {
14554 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14555 emacs_abort ();
14556 x += g->pixel_width;
14557 }
14558 }
14559
14560 /* ROW could be part of a continued line, which, under bidi
14561 reordering, might have other rows whose start and end charpos
14562 occlude point. Only set w->cursor if we found a better
14563 approximation to the cursor position than we have from previously
14564 examined candidate rows belonging to the same continued line. */
14565 if (/* We already have a candidate row. */
14566 w->cursor.vpos >= 0
14567 /* That candidate is not the row we are processing. */
14568 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14569 /* Make sure cursor.vpos specifies a row whose start and end
14570 charpos occlude point, and it is valid candidate for being a
14571 cursor-row. This is because some callers of this function
14572 leave cursor.vpos at the row where the cursor was displayed
14573 during the last redisplay cycle. */
14574 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14575 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14576 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14577 {
14578 struct glyph *g1
14579 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14580
14581 /* Don't consider glyphs that are outside TEXT_AREA. */
14582 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14583 return 0;
14584 /* Keep the candidate whose buffer position is the closest to
14585 point or has the `cursor' property. */
14586 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14587 w->cursor.hpos >= 0
14588 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14589 && ((BUFFERP (g1->object)
14590 && (g1->charpos == pt_old /* An exact match always wins. */
14591 || (BUFFERP (glyph->object)
14592 && eabs (g1->charpos - pt_old)
14593 < eabs (glyph->charpos - pt_old))))
14594 /* Previous candidate is a glyph from a string that has
14595 a non-nil `cursor' property. */
14596 || (STRINGP (g1->object)
14597 && (!NILP (Fget_char_property (make_number (g1->charpos),
14598 Qcursor, g1->object))
14599 /* Previous candidate is from the same display
14600 string as this one, and the display string
14601 came from a text property. */
14602 || (EQ (g1->object, glyph->object)
14603 && string_from_text_prop)
14604 /* this candidate is from newline and its
14605 position is not an exact match */
14606 || (INTEGERP (glyph->object)
14607 && glyph->charpos != pt_old)))))
14608 return 0;
14609 /* If this candidate gives an exact match, use that. */
14610 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14611 /* If this candidate is a glyph created for the
14612 terminating newline of a line, and point is on that
14613 newline, it wins because it's an exact match. */
14614 || (!row->continued_p
14615 && INTEGERP (glyph->object)
14616 && glyph->charpos == 0
14617 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14618 /* Otherwise, keep the candidate that comes from a row
14619 spanning less buffer positions. This may win when one or
14620 both candidate positions are on glyphs that came from
14621 display strings, for which we cannot compare buffer
14622 positions. */
14623 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14624 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14625 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14626 return 0;
14627 }
14628 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14629 w->cursor.x = x;
14630 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14631 w->cursor.y = row->y + dy;
14632
14633 if (w == XWINDOW (selected_window))
14634 {
14635 if (!row->continued_p
14636 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14637 && row->x == 0)
14638 {
14639 this_line_buffer = XBUFFER (w->contents);
14640
14641 CHARPOS (this_line_start_pos)
14642 = MATRIX_ROW_START_CHARPOS (row) + delta;
14643 BYTEPOS (this_line_start_pos)
14644 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14645
14646 CHARPOS (this_line_end_pos)
14647 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14648 BYTEPOS (this_line_end_pos)
14649 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14650
14651 this_line_y = w->cursor.y;
14652 this_line_pixel_height = row->height;
14653 this_line_vpos = w->cursor.vpos;
14654 this_line_start_x = row->x;
14655 }
14656 else
14657 CHARPOS (this_line_start_pos) = 0;
14658 }
14659
14660 return 1;
14661 }
14662
14663
14664 /* Run window scroll functions, if any, for WINDOW with new window
14665 start STARTP. Sets the window start of WINDOW to that position.
14666
14667 We assume that the window's buffer is really current. */
14668
14669 static struct text_pos
14670 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14671 {
14672 struct window *w = XWINDOW (window);
14673 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14674
14675 eassert (current_buffer == XBUFFER (w->contents));
14676
14677 if (!NILP (Vwindow_scroll_functions))
14678 {
14679 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14680 make_number (CHARPOS (startp)));
14681 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14682 /* In case the hook functions switch buffers. */
14683 set_buffer_internal (XBUFFER (w->contents));
14684 }
14685
14686 return startp;
14687 }
14688
14689
14690 /* Make sure the line containing the cursor is fully visible.
14691 A value of 1 means there is nothing to be done.
14692 (Either the line is fully visible, or it cannot be made so,
14693 or we cannot tell.)
14694
14695 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14696 is higher than window.
14697
14698 A value of 0 means the caller should do scrolling
14699 as if point had gone off the screen. */
14700
14701 static int
14702 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14703 {
14704 struct glyph_matrix *matrix;
14705 struct glyph_row *row;
14706 int window_height;
14707
14708 if (!make_cursor_line_fully_visible_p)
14709 return 1;
14710
14711 /* It's not always possible to find the cursor, e.g, when a window
14712 is full of overlay strings. Don't do anything in that case. */
14713 if (w->cursor.vpos < 0)
14714 return 1;
14715
14716 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14717 row = MATRIX_ROW (matrix, w->cursor.vpos);
14718
14719 /* If the cursor row is not partially visible, there's nothing to do. */
14720 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14721 return 1;
14722
14723 /* If the row the cursor is in is taller than the window's height,
14724 it's not clear what to do, so do nothing. */
14725 window_height = window_box_height (w);
14726 if (row->height >= window_height)
14727 {
14728 if (!force_p || MINI_WINDOW_P (w)
14729 || w->vscroll || w->cursor.vpos == 0)
14730 return 1;
14731 }
14732 return 0;
14733 }
14734
14735
14736 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14737 non-zero means only WINDOW is redisplayed in redisplay_internal.
14738 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14739 in redisplay_window to bring a partially visible line into view in
14740 the case that only the cursor has moved.
14741
14742 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14743 last screen line's vertical height extends past the end of the screen.
14744
14745 Value is
14746
14747 1 if scrolling succeeded
14748
14749 0 if scrolling didn't find point.
14750
14751 -1 if new fonts have been loaded so that we must interrupt
14752 redisplay, adjust glyph matrices, and try again. */
14753
14754 enum
14755 {
14756 SCROLLING_SUCCESS,
14757 SCROLLING_FAILED,
14758 SCROLLING_NEED_LARGER_MATRICES
14759 };
14760
14761 /* If scroll-conservatively is more than this, never recenter.
14762
14763 If you change this, don't forget to update the doc string of
14764 `scroll-conservatively' and the Emacs manual. */
14765 #define SCROLL_LIMIT 100
14766
14767 static int
14768 try_scrolling (Lisp_Object window, int just_this_one_p,
14769 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14770 int temp_scroll_step, int last_line_misfit)
14771 {
14772 struct window *w = XWINDOW (window);
14773 struct frame *f = XFRAME (w->frame);
14774 struct text_pos pos, startp;
14775 struct it it;
14776 int this_scroll_margin, scroll_max, rc, height;
14777 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14778 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14779 Lisp_Object aggressive;
14780 /* We will never try scrolling more than this number of lines. */
14781 int scroll_limit = SCROLL_LIMIT;
14782 int frame_line_height = default_line_pixel_height (w);
14783 int window_total_lines
14784 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14785
14786 #ifdef GLYPH_DEBUG
14787 debug_method_add (w, "try_scrolling");
14788 #endif
14789
14790 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14791
14792 /* Compute scroll margin height in pixels. We scroll when point is
14793 within this distance from the top or bottom of the window. */
14794 if (scroll_margin > 0)
14795 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14796 * frame_line_height;
14797 else
14798 this_scroll_margin = 0;
14799
14800 /* Force arg_scroll_conservatively to have a reasonable value, to
14801 avoid scrolling too far away with slow move_it_* functions. Note
14802 that the user can supply scroll-conservatively equal to
14803 `most-positive-fixnum', which can be larger than INT_MAX. */
14804 if (arg_scroll_conservatively > scroll_limit)
14805 {
14806 arg_scroll_conservatively = scroll_limit + 1;
14807 scroll_max = scroll_limit * frame_line_height;
14808 }
14809 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14810 /* Compute how much we should try to scroll maximally to bring
14811 point into view. */
14812 scroll_max = (max (scroll_step,
14813 max (arg_scroll_conservatively, temp_scroll_step))
14814 * frame_line_height);
14815 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14816 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14817 /* We're trying to scroll because of aggressive scrolling but no
14818 scroll_step is set. Choose an arbitrary one. */
14819 scroll_max = 10 * frame_line_height;
14820 else
14821 scroll_max = 0;
14822
14823 too_near_end:
14824
14825 /* Decide whether to scroll down. */
14826 if (PT > CHARPOS (startp))
14827 {
14828 int scroll_margin_y;
14829
14830 /* Compute the pixel ypos of the scroll margin, then move IT to
14831 either that ypos or PT, whichever comes first. */
14832 start_display (&it, w, startp);
14833 scroll_margin_y = it.last_visible_y - this_scroll_margin
14834 - frame_line_height * extra_scroll_margin_lines;
14835 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14836 (MOVE_TO_POS | MOVE_TO_Y));
14837
14838 if (PT > CHARPOS (it.current.pos))
14839 {
14840 int y0 = line_bottom_y (&it);
14841 /* Compute how many pixels below window bottom to stop searching
14842 for PT. This avoids costly search for PT that is far away if
14843 the user limited scrolling by a small number of lines, but
14844 always finds PT if scroll_conservatively is set to a large
14845 number, such as most-positive-fixnum. */
14846 int slack = max (scroll_max, 10 * frame_line_height);
14847 int y_to_move = it.last_visible_y + slack;
14848
14849 /* Compute the distance from the scroll margin to PT or to
14850 the scroll limit, whichever comes first. This should
14851 include the height of the cursor line, to make that line
14852 fully visible. */
14853 move_it_to (&it, PT, -1, y_to_move,
14854 -1, MOVE_TO_POS | MOVE_TO_Y);
14855 dy = line_bottom_y (&it) - y0;
14856
14857 if (dy > scroll_max)
14858 return SCROLLING_FAILED;
14859
14860 if (dy > 0)
14861 scroll_down_p = 1;
14862 }
14863 }
14864
14865 if (scroll_down_p)
14866 {
14867 /* Point is in or below the bottom scroll margin, so move the
14868 window start down. If scrolling conservatively, move it just
14869 enough down to make point visible. If scroll_step is set,
14870 move it down by scroll_step. */
14871 if (arg_scroll_conservatively)
14872 amount_to_scroll
14873 = min (max (dy, frame_line_height),
14874 frame_line_height * arg_scroll_conservatively);
14875 else if (scroll_step || temp_scroll_step)
14876 amount_to_scroll = scroll_max;
14877 else
14878 {
14879 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14880 height = WINDOW_BOX_TEXT_HEIGHT (w);
14881 if (NUMBERP (aggressive))
14882 {
14883 double float_amount = XFLOATINT (aggressive) * height;
14884 int aggressive_scroll = float_amount;
14885 if (aggressive_scroll == 0 && float_amount > 0)
14886 aggressive_scroll = 1;
14887 /* Don't let point enter the scroll margin near top of
14888 the window. This could happen if the value of
14889 scroll_up_aggressively is too large and there are
14890 non-zero margins, because scroll_up_aggressively
14891 means put point that fraction of window height
14892 _from_the_bottom_margin_. */
14893 if (aggressive_scroll + 2*this_scroll_margin > height)
14894 aggressive_scroll = height - 2*this_scroll_margin;
14895 amount_to_scroll = dy + aggressive_scroll;
14896 }
14897 }
14898
14899 if (amount_to_scroll <= 0)
14900 return SCROLLING_FAILED;
14901
14902 start_display (&it, w, startp);
14903 if (arg_scroll_conservatively <= scroll_limit)
14904 move_it_vertically (&it, amount_to_scroll);
14905 else
14906 {
14907 /* Extra precision for users who set scroll-conservatively
14908 to a large number: make sure the amount we scroll
14909 the window start is never less than amount_to_scroll,
14910 which was computed as distance from window bottom to
14911 point. This matters when lines at window top and lines
14912 below window bottom have different height. */
14913 struct it it1;
14914 void *it1data = NULL;
14915 /* We use a temporary it1 because line_bottom_y can modify
14916 its argument, if it moves one line down; see there. */
14917 int start_y;
14918
14919 SAVE_IT (it1, it, it1data);
14920 start_y = line_bottom_y (&it1);
14921 do {
14922 RESTORE_IT (&it, &it, it1data);
14923 move_it_by_lines (&it, 1);
14924 SAVE_IT (it1, it, it1data);
14925 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14926 }
14927
14928 /* If STARTP is unchanged, move it down another screen line. */
14929 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14930 move_it_by_lines (&it, 1);
14931 startp = it.current.pos;
14932 }
14933 else
14934 {
14935 struct text_pos scroll_margin_pos = startp;
14936 int y_offset = 0;
14937
14938 /* See if point is inside the scroll margin at the top of the
14939 window. */
14940 if (this_scroll_margin)
14941 {
14942 int y_start;
14943
14944 start_display (&it, w, startp);
14945 y_start = it.current_y;
14946 move_it_vertically (&it, this_scroll_margin);
14947 scroll_margin_pos = it.current.pos;
14948 /* If we didn't move enough before hitting ZV, request
14949 additional amount of scroll, to move point out of the
14950 scroll margin. */
14951 if (IT_CHARPOS (it) == ZV
14952 && it.current_y - y_start < this_scroll_margin)
14953 y_offset = this_scroll_margin - (it.current_y - y_start);
14954 }
14955
14956 if (PT < CHARPOS (scroll_margin_pos))
14957 {
14958 /* Point is in the scroll margin at the top of the window or
14959 above what is displayed in the window. */
14960 int y0, y_to_move;
14961
14962 /* Compute the vertical distance from PT to the scroll
14963 margin position. Move as far as scroll_max allows, or
14964 one screenful, or 10 screen lines, whichever is largest.
14965 Give up if distance is greater than scroll_max or if we
14966 didn't reach the scroll margin position. */
14967 SET_TEXT_POS (pos, PT, PT_BYTE);
14968 start_display (&it, w, pos);
14969 y0 = it.current_y;
14970 y_to_move = max (it.last_visible_y,
14971 max (scroll_max, 10 * frame_line_height));
14972 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14973 y_to_move, -1,
14974 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14975 dy = it.current_y - y0;
14976 if (dy > scroll_max
14977 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14978 return SCROLLING_FAILED;
14979
14980 /* Additional scroll for when ZV was too close to point. */
14981 dy += y_offset;
14982
14983 /* Compute new window start. */
14984 start_display (&it, w, startp);
14985
14986 if (arg_scroll_conservatively)
14987 amount_to_scroll = max (dy, frame_line_height *
14988 max (scroll_step, temp_scroll_step));
14989 else if (scroll_step || temp_scroll_step)
14990 amount_to_scroll = scroll_max;
14991 else
14992 {
14993 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14994 height = WINDOW_BOX_TEXT_HEIGHT (w);
14995 if (NUMBERP (aggressive))
14996 {
14997 double float_amount = XFLOATINT (aggressive) * height;
14998 int aggressive_scroll = float_amount;
14999 if (aggressive_scroll == 0 && float_amount > 0)
15000 aggressive_scroll = 1;
15001 /* Don't let point enter the scroll margin near
15002 bottom of the window, if the value of
15003 scroll_down_aggressively happens to be too
15004 large. */
15005 if (aggressive_scroll + 2*this_scroll_margin > height)
15006 aggressive_scroll = height - 2*this_scroll_margin;
15007 amount_to_scroll = dy + aggressive_scroll;
15008 }
15009 }
15010
15011 if (amount_to_scroll <= 0)
15012 return SCROLLING_FAILED;
15013
15014 move_it_vertically_backward (&it, amount_to_scroll);
15015 startp = it.current.pos;
15016 }
15017 }
15018
15019 /* Run window scroll functions. */
15020 startp = run_window_scroll_functions (window, startp);
15021
15022 /* Display the window. Give up if new fonts are loaded, or if point
15023 doesn't appear. */
15024 if (!try_window (window, startp, 0))
15025 rc = SCROLLING_NEED_LARGER_MATRICES;
15026 else if (w->cursor.vpos < 0)
15027 {
15028 clear_glyph_matrix (w->desired_matrix);
15029 rc = SCROLLING_FAILED;
15030 }
15031 else
15032 {
15033 /* Maybe forget recorded base line for line number display. */
15034 if (!just_this_one_p
15035 || current_buffer->clip_changed
15036 || BEG_UNCHANGED < CHARPOS (startp))
15037 w->base_line_number = 0;
15038
15039 /* If cursor ends up on a partially visible line,
15040 treat that as being off the bottom of the screen. */
15041 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15042 /* It's possible that the cursor is on the first line of the
15043 buffer, which is partially obscured due to a vscroll
15044 (Bug#7537). In that case, avoid looping forever . */
15045 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15046 {
15047 clear_glyph_matrix (w->desired_matrix);
15048 ++extra_scroll_margin_lines;
15049 goto too_near_end;
15050 }
15051 rc = SCROLLING_SUCCESS;
15052 }
15053
15054 return rc;
15055 }
15056
15057
15058 /* Compute a suitable window start for window W if display of W starts
15059 on a continuation line. Value is non-zero if a new window start
15060 was computed.
15061
15062 The new window start will be computed, based on W's width, starting
15063 from the start of the continued line. It is the start of the
15064 screen line with the minimum distance from the old start W->start. */
15065
15066 static int
15067 compute_window_start_on_continuation_line (struct window *w)
15068 {
15069 struct text_pos pos, start_pos;
15070 int window_start_changed_p = 0;
15071
15072 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15073
15074 /* If window start is on a continuation line... Window start may be
15075 < BEGV in case there's invisible text at the start of the
15076 buffer (M-x rmail, for example). */
15077 if (CHARPOS (start_pos) > BEGV
15078 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15079 {
15080 struct it it;
15081 struct glyph_row *row;
15082
15083 /* Handle the case that the window start is out of range. */
15084 if (CHARPOS (start_pos) < BEGV)
15085 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15086 else if (CHARPOS (start_pos) > ZV)
15087 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15088
15089 /* Find the start of the continued line. This should be fast
15090 because find_newline is fast (newline cache). */
15091 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15092 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15093 row, DEFAULT_FACE_ID);
15094 reseat_at_previous_visible_line_start (&it);
15095
15096 /* If the line start is "too far" away from the window start,
15097 say it takes too much time to compute a new window start. */
15098 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15099 /* PXW: Do we need upper bounds here ? */
15100 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15101 {
15102 int min_distance, distance;
15103
15104 /* Move forward by display lines to find the new window
15105 start. If window width was enlarged, the new start can
15106 be expected to be > the old start. If window width was
15107 decreased, the new window start will be < the old start.
15108 So, we're looking for the display line start with the
15109 minimum distance from the old window start. */
15110 pos = it.current.pos;
15111 min_distance = INFINITY;
15112 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15113 distance < min_distance)
15114 {
15115 min_distance = distance;
15116 pos = it.current.pos;
15117 if (it.line_wrap == WORD_WRAP)
15118 {
15119 /* Under WORD_WRAP, move_it_by_lines is likely to
15120 overshoot and stop not at the first, but the
15121 second character from the left margin. So in
15122 that case, we need a more tight control on the X
15123 coordinate of the iterator than move_it_by_lines
15124 promises in its contract. The method is to first
15125 go to the last (rightmost) visible character of a
15126 line, then move to the leftmost character on the
15127 next line in a separate call. */
15128 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15129 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15130 move_it_to (&it, ZV, 0,
15131 it.current_y + it.max_ascent + it.max_descent, -1,
15132 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15133 }
15134 else
15135 move_it_by_lines (&it, 1);
15136 }
15137
15138 /* Set the window start there. */
15139 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15140 window_start_changed_p = 1;
15141 }
15142 }
15143
15144 return window_start_changed_p;
15145 }
15146
15147
15148 /* Try cursor movement in case text has not changed in window WINDOW,
15149 with window start STARTP. Value is
15150
15151 CURSOR_MOVEMENT_SUCCESS if successful
15152
15153 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15154
15155 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15156 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15157 we want to scroll as if scroll-step were set to 1. See the code.
15158
15159 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15160 which case we have to abort this redisplay, and adjust matrices
15161 first. */
15162
15163 enum
15164 {
15165 CURSOR_MOVEMENT_SUCCESS,
15166 CURSOR_MOVEMENT_CANNOT_BE_USED,
15167 CURSOR_MOVEMENT_MUST_SCROLL,
15168 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15169 };
15170
15171 static int
15172 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15173 {
15174 struct window *w = XWINDOW (window);
15175 struct frame *f = XFRAME (w->frame);
15176 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15177
15178 #ifdef GLYPH_DEBUG
15179 if (inhibit_try_cursor_movement)
15180 return rc;
15181 #endif
15182
15183 /* Previously, there was a check for Lisp integer in the
15184 if-statement below. Now, this field is converted to
15185 ptrdiff_t, thus zero means invalid position in a buffer. */
15186 eassert (w->last_point > 0);
15187 /* Likewise there was a check whether window_end_vpos is nil or larger
15188 than the window. Now window_end_vpos is int and so never nil, but
15189 let's leave eassert to check whether it fits in the window. */
15190 eassert (w->window_end_vpos < w->current_matrix->nrows);
15191
15192 /* Handle case where text has not changed, only point, and it has
15193 not moved off the frame. */
15194 if (/* Point may be in this window. */
15195 PT >= CHARPOS (startp)
15196 /* Selective display hasn't changed. */
15197 && !current_buffer->clip_changed
15198 /* Function force-mode-line-update is used to force a thorough
15199 redisplay. It sets either windows_or_buffers_changed or
15200 update_mode_lines. So don't take a shortcut here for these
15201 cases. */
15202 && !update_mode_lines
15203 && !windows_or_buffers_changed
15204 && !f->cursor_type_changed
15205 && NILP (Vshow_trailing_whitespace)
15206 /* This code is not used for mini-buffer for the sake of the case
15207 of redisplaying to replace an echo area message; since in
15208 that case the mini-buffer contents per se are usually
15209 unchanged. This code is of no real use in the mini-buffer
15210 since the handling of this_line_start_pos, etc., in redisplay
15211 handles the same cases. */
15212 && !EQ (window, minibuf_window)
15213 && (FRAME_WINDOW_P (f)
15214 || !overlay_arrow_in_current_buffer_p ()))
15215 {
15216 int this_scroll_margin, top_scroll_margin;
15217 struct glyph_row *row = NULL;
15218 int frame_line_height = default_line_pixel_height (w);
15219 int window_total_lines
15220 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15221
15222 #ifdef GLYPH_DEBUG
15223 debug_method_add (w, "cursor movement");
15224 #endif
15225
15226 /* Scroll if point within this distance from the top or bottom
15227 of the window. This is a pixel value. */
15228 if (scroll_margin > 0)
15229 {
15230 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15231 this_scroll_margin *= frame_line_height;
15232 }
15233 else
15234 this_scroll_margin = 0;
15235
15236 top_scroll_margin = this_scroll_margin;
15237 if (WINDOW_WANTS_HEADER_LINE_P (w))
15238 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15239
15240 /* Start with the row the cursor was displayed during the last
15241 not paused redisplay. Give up if that row is not valid. */
15242 if (w->last_cursor_vpos < 0
15243 || w->last_cursor_vpos >= w->current_matrix->nrows)
15244 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15245 else
15246 {
15247 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15248 if (row->mode_line_p)
15249 ++row;
15250 if (!row->enabled_p)
15251 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15252 }
15253
15254 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15255 {
15256 int scroll_p = 0, must_scroll = 0;
15257 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15258
15259 if (PT > w->last_point)
15260 {
15261 /* Point has moved forward. */
15262 while (MATRIX_ROW_END_CHARPOS (row) < PT
15263 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15264 {
15265 eassert (row->enabled_p);
15266 ++row;
15267 }
15268
15269 /* If the end position of a row equals the start
15270 position of the next row, and PT is at that position,
15271 we would rather display cursor in the next line. */
15272 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15273 && MATRIX_ROW_END_CHARPOS (row) == PT
15274 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15275 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15276 && !cursor_row_p (row))
15277 ++row;
15278
15279 /* If within the scroll margin, scroll. Note that
15280 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15281 the next line would be drawn, and that
15282 this_scroll_margin can be zero. */
15283 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15284 || PT > MATRIX_ROW_END_CHARPOS (row)
15285 /* Line is completely visible last line in window
15286 and PT is to be set in the next line. */
15287 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15288 && PT == MATRIX_ROW_END_CHARPOS (row)
15289 && !row->ends_at_zv_p
15290 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15291 scroll_p = 1;
15292 }
15293 else if (PT < w->last_point)
15294 {
15295 /* Cursor has to be moved backward. Note that PT >=
15296 CHARPOS (startp) because of the outer if-statement. */
15297 while (!row->mode_line_p
15298 && (MATRIX_ROW_START_CHARPOS (row) > PT
15299 || (MATRIX_ROW_START_CHARPOS (row) == PT
15300 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15301 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15302 row > w->current_matrix->rows
15303 && (row-1)->ends_in_newline_from_string_p))))
15304 && (row->y > top_scroll_margin
15305 || CHARPOS (startp) == BEGV))
15306 {
15307 eassert (row->enabled_p);
15308 --row;
15309 }
15310
15311 /* Consider the following case: Window starts at BEGV,
15312 there is invisible, intangible text at BEGV, so that
15313 display starts at some point START > BEGV. It can
15314 happen that we are called with PT somewhere between
15315 BEGV and START. Try to handle that case. */
15316 if (row < w->current_matrix->rows
15317 || row->mode_line_p)
15318 {
15319 row = w->current_matrix->rows;
15320 if (row->mode_line_p)
15321 ++row;
15322 }
15323
15324 /* Due to newlines in overlay strings, we may have to
15325 skip forward over overlay strings. */
15326 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15327 && MATRIX_ROW_END_CHARPOS (row) == PT
15328 && !cursor_row_p (row))
15329 ++row;
15330
15331 /* If within the scroll margin, scroll. */
15332 if (row->y < top_scroll_margin
15333 && CHARPOS (startp) != BEGV)
15334 scroll_p = 1;
15335 }
15336 else
15337 {
15338 /* Cursor did not move. So don't scroll even if cursor line
15339 is partially visible, as it was so before. */
15340 rc = CURSOR_MOVEMENT_SUCCESS;
15341 }
15342
15343 if (PT < MATRIX_ROW_START_CHARPOS (row)
15344 || PT > MATRIX_ROW_END_CHARPOS (row))
15345 {
15346 /* if PT is not in the glyph row, give up. */
15347 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15348 must_scroll = 1;
15349 }
15350 else if (rc != CURSOR_MOVEMENT_SUCCESS
15351 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15352 {
15353 struct glyph_row *row1;
15354
15355 /* If rows are bidi-reordered and point moved, back up
15356 until we find a row that does not belong to a
15357 continuation line. This is because we must consider
15358 all rows of a continued line as candidates for the
15359 new cursor positioning, since row start and end
15360 positions change non-linearly with vertical position
15361 in such rows. */
15362 /* FIXME: Revisit this when glyph ``spilling'' in
15363 continuation lines' rows is implemented for
15364 bidi-reordered rows. */
15365 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15366 MATRIX_ROW_CONTINUATION_LINE_P (row);
15367 --row)
15368 {
15369 /* If we hit the beginning of the displayed portion
15370 without finding the first row of a continued
15371 line, give up. */
15372 if (row <= row1)
15373 {
15374 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15375 break;
15376 }
15377 eassert (row->enabled_p);
15378 }
15379 }
15380 if (must_scroll)
15381 ;
15382 else if (rc != CURSOR_MOVEMENT_SUCCESS
15383 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15384 /* Make sure this isn't a header line by any chance, since
15385 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15386 && !row->mode_line_p
15387 && make_cursor_line_fully_visible_p)
15388 {
15389 if (PT == MATRIX_ROW_END_CHARPOS (row)
15390 && !row->ends_at_zv_p
15391 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15392 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15393 else if (row->height > window_box_height (w))
15394 {
15395 /* If we end up in a partially visible line, let's
15396 make it fully visible, except when it's taller
15397 than the window, in which case we can't do much
15398 about it. */
15399 *scroll_step = 1;
15400 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15401 }
15402 else
15403 {
15404 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15405 if (!cursor_row_fully_visible_p (w, 0, 1))
15406 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15407 else
15408 rc = CURSOR_MOVEMENT_SUCCESS;
15409 }
15410 }
15411 else if (scroll_p)
15412 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15413 else if (rc != CURSOR_MOVEMENT_SUCCESS
15414 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15415 {
15416 /* With bidi-reordered rows, there could be more than
15417 one candidate row whose start and end positions
15418 occlude point. We need to let set_cursor_from_row
15419 find the best candidate. */
15420 /* FIXME: Revisit this when glyph ``spilling'' in
15421 continuation lines' rows is implemented for
15422 bidi-reordered rows. */
15423 int rv = 0;
15424
15425 do
15426 {
15427 int at_zv_p = 0, exact_match_p = 0;
15428
15429 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15430 && PT <= MATRIX_ROW_END_CHARPOS (row)
15431 && cursor_row_p (row))
15432 rv |= set_cursor_from_row (w, row, w->current_matrix,
15433 0, 0, 0, 0);
15434 /* As soon as we've found the exact match for point,
15435 or the first suitable row whose ends_at_zv_p flag
15436 is set, we are done. */
15437 at_zv_p =
15438 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15439 if (rv && !at_zv_p
15440 && w->cursor.hpos >= 0
15441 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15442 w->cursor.vpos))
15443 {
15444 struct glyph_row *candidate =
15445 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15446 struct glyph *g =
15447 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15448 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15449
15450 exact_match_p =
15451 (BUFFERP (g->object) && g->charpos == PT)
15452 || (INTEGERP (g->object)
15453 && (g->charpos == PT
15454 || (g->charpos == 0 && endpos - 1 == PT)));
15455 }
15456 if (rv && (at_zv_p || exact_match_p))
15457 {
15458 rc = CURSOR_MOVEMENT_SUCCESS;
15459 break;
15460 }
15461 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15462 break;
15463 ++row;
15464 }
15465 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15466 || row->continued_p)
15467 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15468 || (MATRIX_ROW_START_CHARPOS (row) == PT
15469 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15470 /* If we didn't find any candidate rows, or exited the
15471 loop before all the candidates were examined, signal
15472 to the caller that this method failed. */
15473 if (rc != CURSOR_MOVEMENT_SUCCESS
15474 && !(rv
15475 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15476 && !row->continued_p))
15477 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15478 else if (rv)
15479 rc = CURSOR_MOVEMENT_SUCCESS;
15480 }
15481 else
15482 {
15483 do
15484 {
15485 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15486 {
15487 rc = CURSOR_MOVEMENT_SUCCESS;
15488 break;
15489 }
15490 ++row;
15491 }
15492 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15493 && MATRIX_ROW_START_CHARPOS (row) == PT
15494 && cursor_row_p (row));
15495 }
15496 }
15497 }
15498
15499 return rc;
15500 }
15501
15502 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15503 static
15504 #endif
15505 void
15506 set_vertical_scroll_bar (struct window *w)
15507 {
15508 ptrdiff_t start, end, whole;
15509
15510 /* Calculate the start and end positions for the current window.
15511 At some point, it would be nice to choose between scrollbars
15512 which reflect the whole buffer size, with special markers
15513 indicating narrowing, and scrollbars which reflect only the
15514 visible region.
15515
15516 Note that mini-buffers sometimes aren't displaying any text. */
15517 if (!MINI_WINDOW_P (w)
15518 || (w == XWINDOW (minibuf_window)
15519 && NILP (echo_area_buffer[0])))
15520 {
15521 struct buffer *buf = XBUFFER (w->contents);
15522 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15523 start = marker_position (w->start) - BUF_BEGV (buf);
15524 /* I don't think this is guaranteed to be right. For the
15525 moment, we'll pretend it is. */
15526 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15527
15528 if (end < start)
15529 end = start;
15530 if (whole < (end - start))
15531 whole = end - start;
15532 }
15533 else
15534 start = end = whole = 0;
15535
15536 /* Indicate what this scroll bar ought to be displaying now. */
15537 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15538 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15539 (w, end - start, whole, start);
15540 }
15541
15542
15543 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15544 selected_window is redisplayed.
15545
15546 We can return without actually redisplaying the window if fonts has been
15547 changed on window's frame. In that case, redisplay_internal will retry. */
15548
15549 static void
15550 redisplay_window (Lisp_Object window, bool just_this_one_p)
15551 {
15552 struct window *w = XWINDOW (window);
15553 struct frame *f = XFRAME (w->frame);
15554 struct buffer *buffer = XBUFFER (w->contents);
15555 struct buffer *old = current_buffer;
15556 struct text_pos lpoint, opoint, startp;
15557 int update_mode_line;
15558 int tem;
15559 struct it it;
15560 /* Record it now because it's overwritten. */
15561 bool current_matrix_up_to_date_p = false;
15562 bool used_current_matrix_p = false;
15563 /* This is less strict than current_matrix_up_to_date_p.
15564 It indicates that the buffer contents and narrowing are unchanged. */
15565 bool buffer_unchanged_p = false;
15566 int temp_scroll_step = 0;
15567 ptrdiff_t count = SPECPDL_INDEX ();
15568 int rc;
15569 int centering_position = -1;
15570 int last_line_misfit = 0;
15571 ptrdiff_t beg_unchanged, end_unchanged;
15572 int frame_line_height;
15573
15574 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15575 opoint = lpoint;
15576
15577 #ifdef GLYPH_DEBUG
15578 *w->desired_matrix->method = 0;
15579 #endif
15580
15581 if (!just_this_one_p
15582 && REDISPLAY_SOME_P ()
15583 && !w->redisplay
15584 && !f->redisplay
15585 && !buffer->text->redisplay)
15586 return;
15587
15588 /* Make sure that both W's markers are valid. */
15589 eassert (XMARKER (w->start)->buffer == buffer);
15590 eassert (XMARKER (w->pointm)->buffer == buffer);
15591
15592 restart:
15593 reconsider_clip_changes (w);
15594 frame_line_height = default_line_pixel_height (w);
15595
15596 /* Has the mode line to be updated? */
15597 update_mode_line = (w->update_mode_line
15598 || update_mode_lines
15599 || buffer->clip_changed
15600 || buffer->prevent_redisplay_optimizations_p);
15601
15602 if (!just_this_one_p)
15603 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15604 cleverly elsewhere. */
15605 w->must_be_updated_p = true;
15606
15607 if (MINI_WINDOW_P (w))
15608 {
15609 if (w == XWINDOW (echo_area_window)
15610 && !NILP (echo_area_buffer[0]))
15611 {
15612 if (update_mode_line)
15613 /* We may have to update a tty frame's menu bar or a
15614 tool-bar. Example `M-x C-h C-h C-g'. */
15615 goto finish_menu_bars;
15616 else
15617 /* We've already displayed the echo area glyphs in this window. */
15618 goto finish_scroll_bars;
15619 }
15620 else if ((w != XWINDOW (minibuf_window)
15621 || minibuf_level == 0)
15622 /* When buffer is nonempty, redisplay window normally. */
15623 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15624 /* Quail displays non-mini buffers in minibuffer window.
15625 In that case, redisplay the window normally. */
15626 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15627 {
15628 /* W is a mini-buffer window, but it's not active, so clear
15629 it. */
15630 int yb = window_text_bottom_y (w);
15631 struct glyph_row *row;
15632 int y;
15633
15634 for (y = 0, row = w->desired_matrix->rows;
15635 y < yb;
15636 y += row->height, ++row)
15637 blank_row (w, row, y);
15638 goto finish_scroll_bars;
15639 }
15640
15641 clear_glyph_matrix (w->desired_matrix);
15642 }
15643
15644 /* Otherwise set up data on this window; select its buffer and point
15645 value. */
15646 /* Really select the buffer, for the sake of buffer-local
15647 variables. */
15648 set_buffer_internal_1 (XBUFFER (w->contents));
15649
15650 current_matrix_up_to_date_p
15651 = (w->window_end_valid
15652 && !current_buffer->clip_changed
15653 && !current_buffer->prevent_redisplay_optimizations_p
15654 && !window_outdated (w));
15655
15656 /* Run the window-bottom-change-functions
15657 if it is possible that the text on the screen has changed
15658 (either due to modification of the text, or any other reason). */
15659 if (!current_matrix_up_to_date_p
15660 && !NILP (Vwindow_text_change_functions))
15661 {
15662 safe_run_hooks (Qwindow_text_change_functions);
15663 goto restart;
15664 }
15665
15666 beg_unchanged = BEG_UNCHANGED;
15667 end_unchanged = END_UNCHANGED;
15668
15669 SET_TEXT_POS (opoint, PT, PT_BYTE);
15670
15671 specbind (Qinhibit_point_motion_hooks, Qt);
15672
15673 buffer_unchanged_p
15674 = (w->window_end_valid
15675 && !current_buffer->clip_changed
15676 && !window_outdated (w));
15677
15678 /* When windows_or_buffers_changed is non-zero, we can't rely
15679 on the window end being valid, so set it to zero there. */
15680 if (windows_or_buffers_changed)
15681 {
15682 /* If window starts on a continuation line, maybe adjust the
15683 window start in case the window's width changed. */
15684 if (XMARKER (w->start)->buffer == current_buffer)
15685 compute_window_start_on_continuation_line (w);
15686
15687 w->window_end_valid = false;
15688 /* If so, we also can't rely on current matrix
15689 and should not fool try_cursor_movement below. */
15690 current_matrix_up_to_date_p = false;
15691 }
15692
15693 /* Some sanity checks. */
15694 CHECK_WINDOW_END (w);
15695 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15696 emacs_abort ();
15697 if (BYTEPOS (opoint) < CHARPOS (opoint))
15698 emacs_abort ();
15699
15700 if (mode_line_update_needed (w))
15701 update_mode_line = 1;
15702
15703 /* Point refers normally to the selected window. For any other
15704 window, set up appropriate value. */
15705 if (!EQ (window, selected_window))
15706 {
15707 ptrdiff_t new_pt = marker_position (w->pointm);
15708 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15709 if (new_pt < BEGV)
15710 {
15711 new_pt = BEGV;
15712 new_pt_byte = BEGV_BYTE;
15713 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15714 }
15715 else if (new_pt > (ZV - 1))
15716 {
15717 new_pt = ZV;
15718 new_pt_byte = ZV_BYTE;
15719 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15720 }
15721
15722 /* We don't use SET_PT so that the point-motion hooks don't run. */
15723 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15724 }
15725
15726 /* If any of the character widths specified in the display table
15727 have changed, invalidate the width run cache. It's true that
15728 this may be a bit late to catch such changes, but the rest of
15729 redisplay goes (non-fatally) haywire when the display table is
15730 changed, so why should we worry about doing any better? */
15731 if (current_buffer->width_run_cache)
15732 {
15733 struct Lisp_Char_Table *disptab = buffer_display_table ();
15734
15735 if (! disptab_matches_widthtab
15736 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15737 {
15738 invalidate_region_cache (current_buffer,
15739 current_buffer->width_run_cache,
15740 BEG, Z);
15741 recompute_width_table (current_buffer, disptab);
15742 }
15743 }
15744
15745 /* If window-start is screwed up, choose a new one. */
15746 if (XMARKER (w->start)->buffer != current_buffer)
15747 goto recenter;
15748
15749 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15750
15751 /* If someone specified a new starting point but did not insist,
15752 check whether it can be used. */
15753 if (w->optional_new_start
15754 && CHARPOS (startp) >= BEGV
15755 && CHARPOS (startp) <= ZV)
15756 {
15757 w->optional_new_start = 0;
15758 start_display (&it, w, startp);
15759 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15760 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15761 if (IT_CHARPOS (it) == PT)
15762 w->force_start = 1;
15763 /* IT may overshoot PT if text at PT is invisible. */
15764 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15765 w->force_start = 1;
15766 }
15767
15768 force_start:
15769
15770 /* Handle case where place to start displaying has been specified,
15771 unless the specified location is outside the accessible range. */
15772 if (w->force_start || window_frozen_p (w))
15773 {
15774 /* We set this later on if we have to adjust point. */
15775 int new_vpos = -1;
15776
15777 w->force_start = 0;
15778 w->vscroll = 0;
15779 w->window_end_valid = 0;
15780
15781 /* Forget any recorded base line for line number display. */
15782 if (!buffer_unchanged_p)
15783 w->base_line_number = 0;
15784
15785 /* Redisplay the mode line. Select the buffer properly for that.
15786 Also, run the hook window-scroll-functions
15787 because we have scrolled. */
15788 /* Note, we do this after clearing force_start because
15789 if there's an error, it is better to forget about force_start
15790 than to get into an infinite loop calling the hook functions
15791 and having them get more errors. */
15792 if (!update_mode_line
15793 || ! NILP (Vwindow_scroll_functions))
15794 {
15795 update_mode_line = 1;
15796 w->update_mode_line = 1;
15797 startp = run_window_scroll_functions (window, startp);
15798 }
15799
15800 if (CHARPOS (startp) < BEGV)
15801 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15802 else if (CHARPOS (startp) > ZV)
15803 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15804
15805 /* Redisplay, then check if cursor has been set during the
15806 redisplay. Give up if new fonts were loaded. */
15807 /* We used to issue a CHECK_MARGINS argument to try_window here,
15808 but this causes scrolling to fail when point begins inside
15809 the scroll margin (bug#148) -- cyd */
15810 if (!try_window (window, startp, 0))
15811 {
15812 w->force_start = 1;
15813 clear_glyph_matrix (w->desired_matrix);
15814 goto need_larger_matrices;
15815 }
15816
15817 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15818 {
15819 /* If point does not appear, try to move point so it does
15820 appear. The desired matrix has been built above, so we
15821 can use it here. */
15822 new_vpos = window_box_height (w) / 2;
15823 }
15824
15825 if (!cursor_row_fully_visible_p (w, 0, 0))
15826 {
15827 /* Point does appear, but on a line partly visible at end of window.
15828 Move it back to a fully-visible line. */
15829 new_vpos = window_box_height (w);
15830 }
15831 else if (w->cursor.vpos >= 0)
15832 {
15833 /* Some people insist on not letting point enter the scroll
15834 margin, even though this part handles windows that didn't
15835 scroll at all. */
15836 int window_total_lines
15837 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15838 int margin = min (scroll_margin, window_total_lines / 4);
15839 int pixel_margin = margin * frame_line_height;
15840 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15841
15842 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15843 below, which finds the row to move point to, advances by
15844 the Y coordinate of the _next_ row, see the definition of
15845 MATRIX_ROW_BOTTOM_Y. */
15846 if (w->cursor.vpos < margin + header_line)
15847 {
15848 w->cursor.vpos = -1;
15849 clear_glyph_matrix (w->desired_matrix);
15850 goto try_to_scroll;
15851 }
15852 else
15853 {
15854 int window_height = window_box_height (w);
15855
15856 if (header_line)
15857 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15858 if (w->cursor.y >= window_height - pixel_margin)
15859 {
15860 w->cursor.vpos = -1;
15861 clear_glyph_matrix (w->desired_matrix);
15862 goto try_to_scroll;
15863 }
15864 }
15865 }
15866
15867 /* If we need to move point for either of the above reasons,
15868 now actually do it. */
15869 if (new_vpos >= 0)
15870 {
15871 struct glyph_row *row;
15872
15873 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15874 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15875 ++row;
15876
15877 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15878 MATRIX_ROW_START_BYTEPOS (row));
15879
15880 if (w != XWINDOW (selected_window))
15881 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15882 else if (current_buffer == old)
15883 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15884
15885 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15886
15887 /* If we are highlighting the region, then we just changed
15888 the region, so redisplay to show it. */
15889 /* FIXME: We need to (re)run pre-redisplay-function! */
15890 /* if (markpos_of_region () >= 0)
15891 {
15892 clear_glyph_matrix (w->desired_matrix);
15893 if (!try_window (window, startp, 0))
15894 goto need_larger_matrices;
15895 }
15896 */
15897 }
15898
15899 #ifdef GLYPH_DEBUG
15900 debug_method_add (w, "forced window start");
15901 #endif
15902 goto done;
15903 }
15904
15905 /* Handle case where text has not changed, only point, and it has
15906 not moved off the frame, and we are not retrying after hscroll.
15907 (current_matrix_up_to_date_p is nonzero when retrying.) */
15908 if (current_matrix_up_to_date_p
15909 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15910 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15911 {
15912 switch (rc)
15913 {
15914 case CURSOR_MOVEMENT_SUCCESS:
15915 used_current_matrix_p = 1;
15916 goto done;
15917
15918 case CURSOR_MOVEMENT_MUST_SCROLL:
15919 goto try_to_scroll;
15920
15921 default:
15922 emacs_abort ();
15923 }
15924 }
15925 /* If current starting point was originally the beginning of a line
15926 but no longer is, find a new starting point. */
15927 else if (w->start_at_line_beg
15928 && !(CHARPOS (startp) <= BEGV
15929 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15930 {
15931 #ifdef GLYPH_DEBUG
15932 debug_method_add (w, "recenter 1");
15933 #endif
15934 goto recenter;
15935 }
15936
15937 /* Try scrolling with try_window_id. Value is > 0 if update has
15938 been done, it is -1 if we know that the same window start will
15939 not work. It is 0 if unsuccessful for some other reason. */
15940 else if ((tem = try_window_id (w)) != 0)
15941 {
15942 #ifdef GLYPH_DEBUG
15943 debug_method_add (w, "try_window_id %d", tem);
15944 #endif
15945
15946 if (f->fonts_changed)
15947 goto need_larger_matrices;
15948 if (tem > 0)
15949 goto done;
15950
15951 /* Otherwise try_window_id has returned -1 which means that we
15952 don't want the alternative below this comment to execute. */
15953 }
15954 else if (CHARPOS (startp) >= BEGV
15955 && CHARPOS (startp) <= ZV
15956 && PT >= CHARPOS (startp)
15957 && (CHARPOS (startp) < ZV
15958 /* Avoid starting at end of buffer. */
15959 || CHARPOS (startp) == BEGV
15960 || !window_outdated (w)))
15961 {
15962 int d1, d2, d3, d4, d5, d6;
15963
15964 /* If first window line is a continuation line, and window start
15965 is inside the modified region, but the first change is before
15966 current window start, we must select a new window start.
15967
15968 However, if this is the result of a down-mouse event (e.g. by
15969 extending the mouse-drag-overlay), we don't want to select a
15970 new window start, since that would change the position under
15971 the mouse, resulting in an unwanted mouse-movement rather
15972 than a simple mouse-click. */
15973 if (!w->start_at_line_beg
15974 && NILP (do_mouse_tracking)
15975 && CHARPOS (startp) > BEGV
15976 && CHARPOS (startp) > BEG + beg_unchanged
15977 && CHARPOS (startp) <= Z - end_unchanged
15978 /* Even if w->start_at_line_beg is nil, a new window may
15979 start at a line_beg, since that's how set_buffer_window
15980 sets it. So, we need to check the return value of
15981 compute_window_start_on_continuation_line. (See also
15982 bug#197). */
15983 && XMARKER (w->start)->buffer == current_buffer
15984 && compute_window_start_on_continuation_line (w)
15985 /* It doesn't make sense to force the window start like we
15986 do at label force_start if it is already known that point
15987 will not be visible in the resulting window, because
15988 doing so will move point from its correct position
15989 instead of scrolling the window to bring point into view.
15990 See bug#9324. */
15991 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15992 {
15993 w->force_start = 1;
15994 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15995 goto force_start;
15996 }
15997
15998 #ifdef GLYPH_DEBUG
15999 debug_method_add (w, "same window start");
16000 #endif
16001
16002 /* Try to redisplay starting at same place as before.
16003 If point has not moved off frame, accept the results. */
16004 if (!current_matrix_up_to_date_p
16005 /* Don't use try_window_reusing_current_matrix in this case
16006 because a window scroll function can have changed the
16007 buffer. */
16008 || !NILP (Vwindow_scroll_functions)
16009 || MINI_WINDOW_P (w)
16010 || !(used_current_matrix_p
16011 = try_window_reusing_current_matrix (w)))
16012 {
16013 IF_DEBUG (debug_method_add (w, "1"));
16014 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16015 /* -1 means we need to scroll.
16016 0 means we need new matrices, but fonts_changed
16017 is set in that case, so we will detect it below. */
16018 goto try_to_scroll;
16019 }
16020
16021 if (f->fonts_changed)
16022 goto need_larger_matrices;
16023
16024 if (w->cursor.vpos >= 0)
16025 {
16026 if (!just_this_one_p
16027 || current_buffer->clip_changed
16028 || BEG_UNCHANGED < CHARPOS (startp))
16029 /* Forget any recorded base line for line number display. */
16030 w->base_line_number = 0;
16031
16032 if (!cursor_row_fully_visible_p (w, 1, 0))
16033 {
16034 clear_glyph_matrix (w->desired_matrix);
16035 last_line_misfit = 1;
16036 }
16037 /* Drop through and scroll. */
16038 else
16039 goto done;
16040 }
16041 else
16042 clear_glyph_matrix (w->desired_matrix);
16043 }
16044
16045 try_to_scroll:
16046
16047 /* Redisplay the mode line. Select the buffer properly for that. */
16048 if (!update_mode_line)
16049 {
16050 update_mode_line = 1;
16051 w->update_mode_line = 1;
16052 }
16053
16054 /* Try to scroll by specified few lines. */
16055 if ((scroll_conservatively
16056 || emacs_scroll_step
16057 || temp_scroll_step
16058 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16059 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16060 && CHARPOS (startp) >= BEGV
16061 && CHARPOS (startp) <= ZV)
16062 {
16063 /* The function returns -1 if new fonts were loaded, 1 if
16064 successful, 0 if not successful. */
16065 int ss = try_scrolling (window, just_this_one_p,
16066 scroll_conservatively,
16067 emacs_scroll_step,
16068 temp_scroll_step, last_line_misfit);
16069 switch (ss)
16070 {
16071 case SCROLLING_SUCCESS:
16072 goto done;
16073
16074 case SCROLLING_NEED_LARGER_MATRICES:
16075 goto need_larger_matrices;
16076
16077 case SCROLLING_FAILED:
16078 break;
16079
16080 default:
16081 emacs_abort ();
16082 }
16083 }
16084
16085 /* Finally, just choose a place to start which positions point
16086 according to user preferences. */
16087
16088 recenter:
16089
16090 #ifdef GLYPH_DEBUG
16091 debug_method_add (w, "recenter");
16092 #endif
16093
16094 /* Forget any previously recorded base line for line number display. */
16095 if (!buffer_unchanged_p)
16096 w->base_line_number = 0;
16097
16098 /* Determine the window start relative to point. */
16099 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16100 it.current_y = it.last_visible_y;
16101 if (centering_position < 0)
16102 {
16103 int window_total_lines
16104 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16105 int margin =
16106 scroll_margin > 0
16107 ? min (scroll_margin, window_total_lines / 4)
16108 : 0;
16109 ptrdiff_t margin_pos = CHARPOS (startp);
16110 Lisp_Object aggressive;
16111 int scrolling_up;
16112
16113 /* If there is a scroll margin at the top of the window, find
16114 its character position. */
16115 if (margin
16116 /* Cannot call start_display if startp is not in the
16117 accessible region of the buffer. This can happen when we
16118 have just switched to a different buffer and/or changed
16119 its restriction. In that case, startp is initialized to
16120 the character position 1 (BEGV) because we did not yet
16121 have chance to display the buffer even once. */
16122 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16123 {
16124 struct it it1;
16125 void *it1data = NULL;
16126
16127 SAVE_IT (it1, it, it1data);
16128 start_display (&it1, w, startp);
16129 move_it_vertically (&it1, margin * frame_line_height);
16130 margin_pos = IT_CHARPOS (it1);
16131 RESTORE_IT (&it, &it, it1data);
16132 }
16133 scrolling_up = PT > margin_pos;
16134 aggressive =
16135 scrolling_up
16136 ? BVAR (current_buffer, scroll_up_aggressively)
16137 : BVAR (current_buffer, scroll_down_aggressively);
16138
16139 if (!MINI_WINDOW_P (w)
16140 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16141 {
16142 int pt_offset = 0;
16143
16144 /* Setting scroll-conservatively overrides
16145 scroll-*-aggressively. */
16146 if (!scroll_conservatively && NUMBERP (aggressive))
16147 {
16148 double float_amount = XFLOATINT (aggressive);
16149
16150 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16151 if (pt_offset == 0 && float_amount > 0)
16152 pt_offset = 1;
16153 if (pt_offset && margin > 0)
16154 margin -= 1;
16155 }
16156 /* Compute how much to move the window start backward from
16157 point so that point will be displayed where the user
16158 wants it. */
16159 if (scrolling_up)
16160 {
16161 centering_position = it.last_visible_y;
16162 if (pt_offset)
16163 centering_position -= pt_offset;
16164 centering_position -=
16165 frame_line_height * (1 + margin + (last_line_misfit != 0))
16166 + WINDOW_HEADER_LINE_HEIGHT (w);
16167 /* Don't let point enter the scroll margin near top of
16168 the window. */
16169 if (centering_position < margin * frame_line_height)
16170 centering_position = margin * frame_line_height;
16171 }
16172 else
16173 centering_position = margin * frame_line_height + pt_offset;
16174 }
16175 else
16176 /* Set the window start half the height of the window backward
16177 from point. */
16178 centering_position = window_box_height (w) / 2;
16179 }
16180 move_it_vertically_backward (&it, centering_position);
16181
16182 eassert (IT_CHARPOS (it) >= BEGV);
16183
16184 /* The function move_it_vertically_backward may move over more
16185 than the specified y-distance. If it->w is small, e.g. a
16186 mini-buffer window, we may end up in front of the window's
16187 display area. Start displaying at the start of the line
16188 containing PT in this case. */
16189 if (it.current_y <= 0)
16190 {
16191 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16192 move_it_vertically_backward (&it, 0);
16193 it.current_y = 0;
16194 }
16195
16196 it.current_x = it.hpos = 0;
16197
16198 /* Set the window start position here explicitly, to avoid an
16199 infinite loop in case the functions in window-scroll-functions
16200 get errors. */
16201 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16202
16203 /* Run scroll hooks. */
16204 startp = run_window_scroll_functions (window, it.current.pos);
16205
16206 /* Redisplay the window. */
16207 if (!current_matrix_up_to_date_p
16208 || windows_or_buffers_changed
16209 || f->cursor_type_changed
16210 /* Don't use try_window_reusing_current_matrix in this case
16211 because it can have changed the buffer. */
16212 || !NILP (Vwindow_scroll_functions)
16213 || !just_this_one_p
16214 || MINI_WINDOW_P (w)
16215 || !(used_current_matrix_p
16216 = try_window_reusing_current_matrix (w)))
16217 try_window (window, startp, 0);
16218
16219 /* If new fonts have been loaded (due to fontsets), give up. We
16220 have to start a new redisplay since we need to re-adjust glyph
16221 matrices. */
16222 if (f->fonts_changed)
16223 goto need_larger_matrices;
16224
16225 /* If cursor did not appear assume that the middle of the window is
16226 in the first line of the window. Do it again with the next line.
16227 (Imagine a window of height 100, displaying two lines of height
16228 60. Moving back 50 from it->last_visible_y will end in the first
16229 line.) */
16230 if (w->cursor.vpos < 0)
16231 {
16232 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16233 {
16234 clear_glyph_matrix (w->desired_matrix);
16235 move_it_by_lines (&it, 1);
16236 try_window (window, it.current.pos, 0);
16237 }
16238 else if (PT < IT_CHARPOS (it))
16239 {
16240 clear_glyph_matrix (w->desired_matrix);
16241 move_it_by_lines (&it, -1);
16242 try_window (window, it.current.pos, 0);
16243 }
16244 else
16245 {
16246 /* Not much we can do about it. */
16247 }
16248 }
16249
16250 /* Consider the following case: Window starts at BEGV, there is
16251 invisible, intangible text at BEGV, so that display starts at
16252 some point START > BEGV. It can happen that we are called with
16253 PT somewhere between BEGV and START. Try to handle that case. */
16254 if (w->cursor.vpos < 0)
16255 {
16256 struct glyph_row *row = w->current_matrix->rows;
16257 if (row->mode_line_p)
16258 ++row;
16259 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16260 }
16261
16262 if (!cursor_row_fully_visible_p (w, 0, 0))
16263 {
16264 /* If vscroll is enabled, disable it and try again. */
16265 if (w->vscroll)
16266 {
16267 w->vscroll = 0;
16268 clear_glyph_matrix (w->desired_matrix);
16269 goto recenter;
16270 }
16271
16272 /* Users who set scroll-conservatively to a large number want
16273 point just above/below the scroll margin. If we ended up
16274 with point's row partially visible, move the window start to
16275 make that row fully visible and out of the margin. */
16276 if (scroll_conservatively > SCROLL_LIMIT)
16277 {
16278 int window_total_lines
16279 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16280 int margin =
16281 scroll_margin > 0
16282 ? min (scroll_margin, window_total_lines / 4)
16283 : 0;
16284 int move_down = w->cursor.vpos >= window_total_lines / 2;
16285
16286 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16287 clear_glyph_matrix (w->desired_matrix);
16288 if (1 == try_window (window, it.current.pos,
16289 TRY_WINDOW_CHECK_MARGINS))
16290 goto done;
16291 }
16292
16293 /* If centering point failed to make the whole line visible,
16294 put point at the top instead. That has to make the whole line
16295 visible, if it can be done. */
16296 if (centering_position == 0)
16297 goto done;
16298
16299 clear_glyph_matrix (w->desired_matrix);
16300 centering_position = 0;
16301 goto recenter;
16302 }
16303
16304 done:
16305
16306 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16307 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16308 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16309
16310 /* Display the mode line, if we must. */
16311 if ((update_mode_line
16312 /* If window not full width, must redo its mode line
16313 if (a) the window to its side is being redone and
16314 (b) we do a frame-based redisplay. This is a consequence
16315 of how inverted lines are drawn in frame-based redisplay. */
16316 || (!just_this_one_p
16317 && !FRAME_WINDOW_P (f)
16318 && !WINDOW_FULL_WIDTH_P (w))
16319 /* Line number to display. */
16320 || w->base_line_pos > 0
16321 /* Column number is displayed and different from the one displayed. */
16322 || (w->column_number_displayed != -1
16323 && (w->column_number_displayed != current_column ())))
16324 /* This means that the window has a mode line. */
16325 && (WINDOW_WANTS_MODELINE_P (w)
16326 || WINDOW_WANTS_HEADER_LINE_P (w)))
16327 {
16328
16329 display_mode_lines (w);
16330
16331 /* If mode line height has changed, arrange for a thorough
16332 immediate redisplay using the correct mode line height. */
16333 if (WINDOW_WANTS_MODELINE_P (w)
16334 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16335 {
16336 f->fonts_changed = 1;
16337 w->mode_line_height = -1;
16338 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16339 = DESIRED_MODE_LINE_HEIGHT (w);
16340 }
16341
16342 /* If header line height has changed, arrange for a thorough
16343 immediate redisplay using the correct header line height. */
16344 if (WINDOW_WANTS_HEADER_LINE_P (w)
16345 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16346 {
16347 f->fonts_changed = 1;
16348 w->header_line_height = -1;
16349 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16350 = DESIRED_HEADER_LINE_HEIGHT (w);
16351 }
16352
16353 if (f->fonts_changed)
16354 goto need_larger_matrices;
16355 }
16356
16357 if (!line_number_displayed && w->base_line_pos != -1)
16358 {
16359 w->base_line_pos = 0;
16360 w->base_line_number = 0;
16361 }
16362
16363 finish_menu_bars:
16364
16365 /* When we reach a frame's selected window, redo the frame's menu bar. */
16366 if (update_mode_line
16367 && EQ (FRAME_SELECTED_WINDOW (f), window))
16368 {
16369 int redisplay_menu_p = 0;
16370
16371 if (FRAME_WINDOW_P (f))
16372 {
16373 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16374 || defined (HAVE_NS) || defined (USE_GTK)
16375 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16376 #else
16377 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16378 #endif
16379 }
16380 else
16381 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16382
16383 if (redisplay_menu_p)
16384 display_menu_bar (w);
16385
16386 #ifdef HAVE_WINDOW_SYSTEM
16387 if (FRAME_WINDOW_P (f))
16388 {
16389 #if defined (USE_GTK) || defined (HAVE_NS)
16390 if (FRAME_EXTERNAL_TOOL_BAR (f))
16391 redisplay_tool_bar (f);
16392 #else
16393 if (WINDOWP (f->tool_bar_window)
16394 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16395 || !NILP (Vauto_resize_tool_bars))
16396 && redisplay_tool_bar (f))
16397 ignore_mouse_drag_p = 1;
16398 #endif
16399 }
16400 #endif
16401 }
16402
16403 #ifdef HAVE_WINDOW_SYSTEM
16404 if (FRAME_WINDOW_P (f)
16405 && update_window_fringes (w, (just_this_one_p
16406 || (!used_current_matrix_p && !overlay_arrow_seen)
16407 || w->pseudo_window_p)))
16408 {
16409 update_begin (f);
16410 block_input ();
16411 if (draw_window_fringes (w, 1))
16412 {
16413 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16414 x_draw_right_divider (w);
16415 else
16416 x_draw_vertical_border (w);
16417 }
16418 unblock_input ();
16419 update_end (f);
16420 }
16421
16422 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16423 x_draw_bottom_divider (w);
16424 #endif /* HAVE_WINDOW_SYSTEM */
16425
16426 /* We go to this label, with fonts_changed set, if it is
16427 necessary to try again using larger glyph matrices.
16428 We have to redeem the scroll bar even in this case,
16429 because the loop in redisplay_internal expects that. */
16430 need_larger_matrices:
16431 ;
16432 finish_scroll_bars:
16433
16434 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16435 {
16436 /* Set the thumb's position and size. */
16437 set_vertical_scroll_bar (w);
16438
16439 /* Note that we actually used the scroll bar attached to this
16440 window, so it shouldn't be deleted at the end of redisplay. */
16441 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16442 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16443 }
16444
16445 /* Restore current_buffer and value of point in it. The window
16446 update may have changed the buffer, so first make sure `opoint'
16447 is still valid (Bug#6177). */
16448 if (CHARPOS (opoint) < BEGV)
16449 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16450 else if (CHARPOS (opoint) > ZV)
16451 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16452 else
16453 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16454
16455 set_buffer_internal_1 (old);
16456 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16457 shorter. This can be caused by log truncation in *Messages*. */
16458 if (CHARPOS (lpoint) <= ZV)
16459 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16460
16461 unbind_to (count, Qnil);
16462 }
16463
16464
16465 /* Build the complete desired matrix of WINDOW with a window start
16466 buffer position POS.
16467
16468 Value is 1 if successful. It is zero if fonts were loaded during
16469 redisplay which makes re-adjusting glyph matrices necessary, and -1
16470 if point would appear in the scroll margins.
16471 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16472 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16473 set in FLAGS.) */
16474
16475 int
16476 try_window (Lisp_Object window, struct text_pos pos, int flags)
16477 {
16478 struct window *w = XWINDOW (window);
16479 struct it it;
16480 struct glyph_row *last_text_row = NULL;
16481 struct frame *f = XFRAME (w->frame);
16482 int frame_line_height = default_line_pixel_height (w);
16483
16484 /* Make POS the new window start. */
16485 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16486
16487 /* Mark cursor position as unknown. No overlay arrow seen. */
16488 w->cursor.vpos = -1;
16489 overlay_arrow_seen = 0;
16490
16491 /* Initialize iterator and info to start at POS. */
16492 start_display (&it, w, pos);
16493
16494 /* Display all lines of W. */
16495 while (it.current_y < it.last_visible_y)
16496 {
16497 if (display_line (&it))
16498 last_text_row = it.glyph_row - 1;
16499 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16500 return 0;
16501 }
16502
16503 /* Don't let the cursor end in the scroll margins. */
16504 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16505 && !MINI_WINDOW_P (w))
16506 {
16507 int this_scroll_margin;
16508 int window_total_lines
16509 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16510
16511 if (scroll_margin > 0)
16512 {
16513 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16514 this_scroll_margin *= frame_line_height;
16515 }
16516 else
16517 this_scroll_margin = 0;
16518
16519 if ((w->cursor.y >= 0 /* not vscrolled */
16520 && w->cursor.y < this_scroll_margin
16521 && CHARPOS (pos) > BEGV
16522 && IT_CHARPOS (it) < ZV)
16523 /* rms: considering make_cursor_line_fully_visible_p here
16524 seems to give wrong results. We don't want to recenter
16525 when the last line is partly visible, we want to allow
16526 that case to be handled in the usual way. */
16527 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16528 {
16529 w->cursor.vpos = -1;
16530 clear_glyph_matrix (w->desired_matrix);
16531 return -1;
16532 }
16533 }
16534
16535 /* If bottom moved off end of frame, change mode line percentage. */
16536 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16537 w->update_mode_line = 1;
16538
16539 /* Set window_end_pos to the offset of the last character displayed
16540 on the window from the end of current_buffer. Set
16541 window_end_vpos to its row number. */
16542 if (last_text_row)
16543 {
16544 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16545 adjust_window_ends (w, last_text_row, 0);
16546 eassert
16547 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16548 w->window_end_vpos)));
16549 }
16550 else
16551 {
16552 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16553 w->window_end_pos = Z - ZV;
16554 w->window_end_vpos = 0;
16555 }
16556
16557 /* But that is not valid info until redisplay finishes. */
16558 w->window_end_valid = 0;
16559 return 1;
16560 }
16561
16562
16563 \f
16564 /************************************************************************
16565 Window redisplay reusing current matrix when buffer has not changed
16566 ************************************************************************/
16567
16568 /* Try redisplay of window W showing an unchanged buffer with a
16569 different window start than the last time it was displayed by
16570 reusing its current matrix. Value is non-zero if successful.
16571 W->start is the new window start. */
16572
16573 static int
16574 try_window_reusing_current_matrix (struct window *w)
16575 {
16576 struct frame *f = XFRAME (w->frame);
16577 struct glyph_row *bottom_row;
16578 struct it it;
16579 struct run run;
16580 struct text_pos start, new_start;
16581 int nrows_scrolled, i;
16582 struct glyph_row *last_text_row;
16583 struct glyph_row *last_reused_text_row;
16584 struct glyph_row *start_row;
16585 int start_vpos, min_y, max_y;
16586
16587 #ifdef GLYPH_DEBUG
16588 if (inhibit_try_window_reusing)
16589 return 0;
16590 #endif
16591
16592 if (/* This function doesn't handle terminal frames. */
16593 !FRAME_WINDOW_P (f)
16594 /* Don't try to reuse the display if windows have been split
16595 or such. */
16596 || windows_or_buffers_changed
16597 || f->cursor_type_changed)
16598 return 0;
16599
16600 /* Can't do this if showing trailing whitespace. */
16601 if (!NILP (Vshow_trailing_whitespace))
16602 return 0;
16603
16604 /* If top-line visibility has changed, give up. */
16605 if (WINDOW_WANTS_HEADER_LINE_P (w)
16606 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16607 return 0;
16608
16609 /* Give up if old or new display is scrolled vertically. We could
16610 make this function handle this, but right now it doesn't. */
16611 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16612 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16613 return 0;
16614
16615 /* The variable new_start now holds the new window start. The old
16616 start `start' can be determined from the current matrix. */
16617 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16618 start = start_row->minpos;
16619 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16620
16621 /* Clear the desired matrix for the display below. */
16622 clear_glyph_matrix (w->desired_matrix);
16623
16624 if (CHARPOS (new_start) <= CHARPOS (start))
16625 {
16626 /* Don't use this method if the display starts with an ellipsis
16627 displayed for invisible text. It's not easy to handle that case
16628 below, and it's certainly not worth the effort since this is
16629 not a frequent case. */
16630 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16631 return 0;
16632
16633 IF_DEBUG (debug_method_add (w, "twu1"));
16634
16635 /* Display up to a row that can be reused. The variable
16636 last_text_row is set to the last row displayed that displays
16637 text. Note that it.vpos == 0 if or if not there is a
16638 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16639 start_display (&it, w, new_start);
16640 w->cursor.vpos = -1;
16641 last_text_row = last_reused_text_row = NULL;
16642
16643 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16644 {
16645 /* If we have reached into the characters in the START row,
16646 that means the line boundaries have changed. So we
16647 can't start copying with the row START. Maybe it will
16648 work to start copying with the following row. */
16649 while (IT_CHARPOS (it) > CHARPOS (start))
16650 {
16651 /* Advance to the next row as the "start". */
16652 start_row++;
16653 start = start_row->minpos;
16654 /* If there are no more rows to try, or just one, give up. */
16655 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16656 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16657 || CHARPOS (start) == ZV)
16658 {
16659 clear_glyph_matrix (w->desired_matrix);
16660 return 0;
16661 }
16662
16663 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16664 }
16665 /* If we have reached alignment, we can copy the rest of the
16666 rows. */
16667 if (IT_CHARPOS (it) == CHARPOS (start)
16668 /* Don't accept "alignment" inside a display vector,
16669 since start_row could have started in the middle of
16670 that same display vector (thus their character
16671 positions match), and we have no way of telling if
16672 that is the case. */
16673 && it.current.dpvec_index < 0)
16674 break;
16675
16676 if (display_line (&it))
16677 last_text_row = it.glyph_row - 1;
16678
16679 }
16680
16681 /* A value of current_y < last_visible_y means that we stopped
16682 at the previous window start, which in turn means that we
16683 have at least one reusable row. */
16684 if (it.current_y < it.last_visible_y)
16685 {
16686 struct glyph_row *row;
16687
16688 /* IT.vpos always starts from 0; it counts text lines. */
16689 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16690
16691 /* Find PT if not already found in the lines displayed. */
16692 if (w->cursor.vpos < 0)
16693 {
16694 int dy = it.current_y - start_row->y;
16695
16696 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16697 row = row_containing_pos (w, PT, row, NULL, dy);
16698 if (row)
16699 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16700 dy, nrows_scrolled);
16701 else
16702 {
16703 clear_glyph_matrix (w->desired_matrix);
16704 return 0;
16705 }
16706 }
16707
16708 /* Scroll the display. Do it before the current matrix is
16709 changed. The problem here is that update has not yet
16710 run, i.e. part of the current matrix is not up to date.
16711 scroll_run_hook will clear the cursor, and use the
16712 current matrix to get the height of the row the cursor is
16713 in. */
16714 run.current_y = start_row->y;
16715 run.desired_y = it.current_y;
16716 run.height = it.last_visible_y - it.current_y;
16717
16718 if (run.height > 0 && run.current_y != run.desired_y)
16719 {
16720 update_begin (f);
16721 FRAME_RIF (f)->update_window_begin_hook (w);
16722 FRAME_RIF (f)->clear_window_mouse_face (w);
16723 FRAME_RIF (f)->scroll_run_hook (w, &run);
16724 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16725 update_end (f);
16726 }
16727
16728 /* Shift current matrix down by nrows_scrolled lines. */
16729 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16730 rotate_matrix (w->current_matrix,
16731 start_vpos,
16732 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16733 nrows_scrolled);
16734
16735 /* Disable lines that must be updated. */
16736 for (i = 0; i < nrows_scrolled; ++i)
16737 (start_row + i)->enabled_p = 0;
16738
16739 /* Re-compute Y positions. */
16740 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16741 max_y = it.last_visible_y;
16742 for (row = start_row + nrows_scrolled;
16743 row < bottom_row;
16744 ++row)
16745 {
16746 row->y = it.current_y;
16747 row->visible_height = row->height;
16748
16749 if (row->y < min_y)
16750 row->visible_height -= min_y - row->y;
16751 if (row->y + row->height > max_y)
16752 row->visible_height -= row->y + row->height - max_y;
16753 if (row->fringe_bitmap_periodic_p)
16754 row->redraw_fringe_bitmaps_p = 1;
16755
16756 it.current_y += row->height;
16757
16758 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16759 last_reused_text_row = row;
16760 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16761 break;
16762 }
16763
16764 /* Disable lines in the current matrix which are now
16765 below the window. */
16766 for (++row; row < bottom_row; ++row)
16767 row->enabled_p = row->mode_line_p = 0;
16768 }
16769
16770 /* Update window_end_pos etc.; last_reused_text_row is the last
16771 reused row from the current matrix containing text, if any.
16772 The value of last_text_row is the last displayed line
16773 containing text. */
16774 if (last_reused_text_row)
16775 adjust_window_ends (w, last_reused_text_row, 1);
16776 else if (last_text_row)
16777 adjust_window_ends (w, last_text_row, 0);
16778 else
16779 {
16780 /* This window must be completely empty. */
16781 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16782 w->window_end_pos = Z - ZV;
16783 w->window_end_vpos = 0;
16784 }
16785 w->window_end_valid = 0;
16786
16787 /* Update hint: don't try scrolling again in update_window. */
16788 w->desired_matrix->no_scrolling_p = 1;
16789
16790 #ifdef GLYPH_DEBUG
16791 debug_method_add (w, "try_window_reusing_current_matrix 1");
16792 #endif
16793 return 1;
16794 }
16795 else if (CHARPOS (new_start) > CHARPOS (start))
16796 {
16797 struct glyph_row *pt_row, *row;
16798 struct glyph_row *first_reusable_row;
16799 struct glyph_row *first_row_to_display;
16800 int dy;
16801 int yb = window_text_bottom_y (w);
16802
16803 /* Find the row starting at new_start, if there is one. Don't
16804 reuse a partially visible line at the end. */
16805 first_reusable_row = start_row;
16806 while (first_reusable_row->enabled_p
16807 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16808 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16809 < CHARPOS (new_start)))
16810 ++first_reusable_row;
16811
16812 /* Give up if there is no row to reuse. */
16813 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16814 || !first_reusable_row->enabled_p
16815 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16816 != CHARPOS (new_start)))
16817 return 0;
16818
16819 /* We can reuse fully visible rows beginning with
16820 first_reusable_row to the end of the window. Set
16821 first_row_to_display to the first row that cannot be reused.
16822 Set pt_row to the row containing point, if there is any. */
16823 pt_row = NULL;
16824 for (first_row_to_display = first_reusable_row;
16825 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16826 ++first_row_to_display)
16827 {
16828 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16829 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16830 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16831 && first_row_to_display->ends_at_zv_p
16832 && pt_row == NULL)))
16833 pt_row = first_row_to_display;
16834 }
16835
16836 /* Start displaying at the start of first_row_to_display. */
16837 eassert (first_row_to_display->y < yb);
16838 init_to_row_start (&it, w, first_row_to_display);
16839
16840 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16841 - start_vpos);
16842 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16843 - nrows_scrolled);
16844 it.current_y = (first_row_to_display->y - first_reusable_row->y
16845 + WINDOW_HEADER_LINE_HEIGHT (w));
16846
16847 /* Display lines beginning with first_row_to_display in the
16848 desired matrix. Set last_text_row to the last row displayed
16849 that displays text. */
16850 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16851 if (pt_row == NULL)
16852 w->cursor.vpos = -1;
16853 last_text_row = NULL;
16854 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16855 if (display_line (&it))
16856 last_text_row = it.glyph_row - 1;
16857
16858 /* If point is in a reused row, adjust y and vpos of the cursor
16859 position. */
16860 if (pt_row)
16861 {
16862 w->cursor.vpos -= nrows_scrolled;
16863 w->cursor.y -= first_reusable_row->y - start_row->y;
16864 }
16865
16866 /* Give up if point isn't in a row displayed or reused. (This
16867 also handles the case where w->cursor.vpos < nrows_scrolled
16868 after the calls to display_line, which can happen with scroll
16869 margins. See bug#1295.) */
16870 if (w->cursor.vpos < 0)
16871 {
16872 clear_glyph_matrix (w->desired_matrix);
16873 return 0;
16874 }
16875
16876 /* Scroll the display. */
16877 run.current_y = first_reusable_row->y;
16878 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16879 run.height = it.last_visible_y - run.current_y;
16880 dy = run.current_y - run.desired_y;
16881
16882 if (run.height)
16883 {
16884 update_begin (f);
16885 FRAME_RIF (f)->update_window_begin_hook (w);
16886 FRAME_RIF (f)->clear_window_mouse_face (w);
16887 FRAME_RIF (f)->scroll_run_hook (w, &run);
16888 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16889 update_end (f);
16890 }
16891
16892 /* Adjust Y positions of reused rows. */
16893 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16894 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16895 max_y = it.last_visible_y;
16896 for (row = first_reusable_row; row < first_row_to_display; ++row)
16897 {
16898 row->y -= dy;
16899 row->visible_height = row->height;
16900 if (row->y < min_y)
16901 row->visible_height -= min_y - row->y;
16902 if (row->y + row->height > max_y)
16903 row->visible_height -= row->y + row->height - max_y;
16904 if (row->fringe_bitmap_periodic_p)
16905 row->redraw_fringe_bitmaps_p = 1;
16906 }
16907
16908 /* Scroll the current matrix. */
16909 eassert (nrows_scrolled > 0);
16910 rotate_matrix (w->current_matrix,
16911 start_vpos,
16912 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16913 -nrows_scrolled);
16914
16915 /* Disable rows not reused. */
16916 for (row -= nrows_scrolled; row < bottom_row; ++row)
16917 row->enabled_p = 0;
16918
16919 /* Point may have moved to a different line, so we cannot assume that
16920 the previous cursor position is valid; locate the correct row. */
16921 if (pt_row)
16922 {
16923 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16924 row < bottom_row
16925 && PT >= MATRIX_ROW_END_CHARPOS (row)
16926 && !row->ends_at_zv_p;
16927 row++)
16928 {
16929 w->cursor.vpos++;
16930 w->cursor.y = row->y;
16931 }
16932 if (row < bottom_row)
16933 {
16934 /* Can't simply scan the row for point with
16935 bidi-reordered glyph rows. Let set_cursor_from_row
16936 figure out where to put the cursor, and if it fails,
16937 give up. */
16938 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16939 {
16940 if (!set_cursor_from_row (w, row, w->current_matrix,
16941 0, 0, 0, 0))
16942 {
16943 clear_glyph_matrix (w->desired_matrix);
16944 return 0;
16945 }
16946 }
16947 else
16948 {
16949 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16950 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16951
16952 for (; glyph < end
16953 && (!BUFFERP (glyph->object)
16954 || glyph->charpos < PT);
16955 glyph++)
16956 {
16957 w->cursor.hpos++;
16958 w->cursor.x += glyph->pixel_width;
16959 }
16960 }
16961 }
16962 }
16963
16964 /* Adjust window end. A null value of last_text_row means that
16965 the window end is in reused rows which in turn means that
16966 only its vpos can have changed. */
16967 if (last_text_row)
16968 adjust_window_ends (w, last_text_row, 0);
16969 else
16970 w->window_end_vpos -= nrows_scrolled;
16971
16972 w->window_end_valid = 0;
16973 w->desired_matrix->no_scrolling_p = 1;
16974
16975 #ifdef GLYPH_DEBUG
16976 debug_method_add (w, "try_window_reusing_current_matrix 2");
16977 #endif
16978 return 1;
16979 }
16980
16981 return 0;
16982 }
16983
16984
16985 \f
16986 /************************************************************************
16987 Window redisplay reusing current matrix when buffer has changed
16988 ************************************************************************/
16989
16990 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16991 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16992 ptrdiff_t *, ptrdiff_t *);
16993 static struct glyph_row *
16994 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16995 struct glyph_row *);
16996
16997
16998 /* Return the last row in MATRIX displaying text. If row START is
16999 non-null, start searching with that row. IT gives the dimensions
17000 of the display. Value is null if matrix is empty; otherwise it is
17001 a pointer to the row found. */
17002
17003 static struct glyph_row *
17004 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17005 struct glyph_row *start)
17006 {
17007 struct glyph_row *row, *row_found;
17008
17009 /* Set row_found to the last row in IT->w's current matrix
17010 displaying text. The loop looks funny but think of partially
17011 visible lines. */
17012 row_found = NULL;
17013 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17014 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17015 {
17016 eassert (row->enabled_p);
17017 row_found = row;
17018 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17019 break;
17020 ++row;
17021 }
17022
17023 return row_found;
17024 }
17025
17026
17027 /* Return the last row in the current matrix of W that is not affected
17028 by changes at the start of current_buffer that occurred since W's
17029 current matrix was built. Value is null if no such row exists.
17030
17031 BEG_UNCHANGED us the number of characters unchanged at the start of
17032 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17033 first changed character in current_buffer. Characters at positions <
17034 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17035 when the current matrix was built. */
17036
17037 static struct glyph_row *
17038 find_last_unchanged_at_beg_row (struct window *w)
17039 {
17040 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17041 struct glyph_row *row;
17042 struct glyph_row *row_found = NULL;
17043 int yb = window_text_bottom_y (w);
17044
17045 /* Find the last row displaying unchanged text. */
17046 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17047 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17048 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17049 ++row)
17050 {
17051 if (/* If row ends before first_changed_pos, it is unchanged,
17052 except in some case. */
17053 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17054 /* When row ends in ZV and we write at ZV it is not
17055 unchanged. */
17056 && !row->ends_at_zv_p
17057 /* When first_changed_pos is the end of a continued line,
17058 row is not unchanged because it may be no longer
17059 continued. */
17060 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17061 && (row->continued_p
17062 || row->exact_window_width_line_p))
17063 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17064 needs to be recomputed, so don't consider this row as
17065 unchanged. This happens when the last line was
17066 bidi-reordered and was killed immediately before this
17067 redisplay cycle. In that case, ROW->end stores the
17068 buffer position of the first visual-order character of
17069 the killed text, which is now beyond ZV. */
17070 && CHARPOS (row->end.pos) <= ZV)
17071 row_found = row;
17072
17073 /* Stop if last visible row. */
17074 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17075 break;
17076 }
17077
17078 return row_found;
17079 }
17080
17081
17082 /* Find the first glyph row in the current matrix of W that is not
17083 affected by changes at the end of current_buffer since the
17084 time W's current matrix was built.
17085
17086 Return in *DELTA the number of chars by which buffer positions in
17087 unchanged text at the end of current_buffer must be adjusted.
17088
17089 Return in *DELTA_BYTES the corresponding number of bytes.
17090
17091 Value is null if no such row exists, i.e. all rows are affected by
17092 changes. */
17093
17094 static struct glyph_row *
17095 find_first_unchanged_at_end_row (struct window *w,
17096 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17097 {
17098 struct glyph_row *row;
17099 struct glyph_row *row_found = NULL;
17100
17101 *delta = *delta_bytes = 0;
17102
17103 /* Display must not have been paused, otherwise the current matrix
17104 is not up to date. */
17105 eassert (w->window_end_valid);
17106
17107 /* A value of window_end_pos >= END_UNCHANGED means that the window
17108 end is in the range of changed text. If so, there is no
17109 unchanged row at the end of W's current matrix. */
17110 if (w->window_end_pos >= END_UNCHANGED)
17111 return NULL;
17112
17113 /* Set row to the last row in W's current matrix displaying text. */
17114 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17115
17116 /* If matrix is entirely empty, no unchanged row exists. */
17117 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17118 {
17119 /* The value of row is the last glyph row in the matrix having a
17120 meaningful buffer position in it. The end position of row
17121 corresponds to window_end_pos. This allows us to translate
17122 buffer positions in the current matrix to current buffer
17123 positions for characters not in changed text. */
17124 ptrdiff_t Z_old =
17125 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17126 ptrdiff_t Z_BYTE_old =
17127 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17128 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17129 struct glyph_row *first_text_row
17130 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17131
17132 *delta = Z - Z_old;
17133 *delta_bytes = Z_BYTE - Z_BYTE_old;
17134
17135 /* Set last_unchanged_pos to the buffer position of the last
17136 character in the buffer that has not been changed. Z is the
17137 index + 1 of the last character in current_buffer, i.e. by
17138 subtracting END_UNCHANGED we get the index of the last
17139 unchanged character, and we have to add BEG to get its buffer
17140 position. */
17141 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17142 last_unchanged_pos_old = last_unchanged_pos - *delta;
17143
17144 /* Search backward from ROW for a row displaying a line that
17145 starts at a minimum position >= last_unchanged_pos_old. */
17146 for (; row > first_text_row; --row)
17147 {
17148 /* This used to abort, but it can happen.
17149 It is ok to just stop the search instead here. KFS. */
17150 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17151 break;
17152
17153 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17154 row_found = row;
17155 }
17156 }
17157
17158 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17159
17160 return row_found;
17161 }
17162
17163
17164 /* Make sure that glyph rows in the current matrix of window W
17165 reference the same glyph memory as corresponding rows in the
17166 frame's frame matrix. This function is called after scrolling W's
17167 current matrix on a terminal frame in try_window_id and
17168 try_window_reusing_current_matrix. */
17169
17170 static void
17171 sync_frame_with_window_matrix_rows (struct window *w)
17172 {
17173 struct frame *f = XFRAME (w->frame);
17174 struct glyph_row *window_row, *window_row_end, *frame_row;
17175
17176 /* Preconditions: W must be a leaf window and full-width. Its frame
17177 must have a frame matrix. */
17178 eassert (BUFFERP (w->contents));
17179 eassert (WINDOW_FULL_WIDTH_P (w));
17180 eassert (!FRAME_WINDOW_P (f));
17181
17182 /* If W is a full-width window, glyph pointers in W's current matrix
17183 have, by definition, to be the same as glyph pointers in the
17184 corresponding frame matrix. Note that frame matrices have no
17185 marginal areas (see build_frame_matrix). */
17186 window_row = w->current_matrix->rows;
17187 window_row_end = window_row + w->current_matrix->nrows;
17188 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17189 while (window_row < window_row_end)
17190 {
17191 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17192 struct glyph *end = window_row->glyphs[LAST_AREA];
17193
17194 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17195 frame_row->glyphs[TEXT_AREA] = start;
17196 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17197 frame_row->glyphs[LAST_AREA] = end;
17198
17199 /* Disable frame rows whose corresponding window rows have
17200 been disabled in try_window_id. */
17201 if (!window_row->enabled_p)
17202 frame_row->enabled_p = 0;
17203
17204 ++window_row, ++frame_row;
17205 }
17206 }
17207
17208
17209 /* Find the glyph row in window W containing CHARPOS. Consider all
17210 rows between START and END (not inclusive). END null means search
17211 all rows to the end of the display area of W. Value is the row
17212 containing CHARPOS or null. */
17213
17214 struct glyph_row *
17215 row_containing_pos (struct window *w, ptrdiff_t charpos,
17216 struct glyph_row *start, struct glyph_row *end, int dy)
17217 {
17218 struct glyph_row *row = start;
17219 struct glyph_row *best_row = NULL;
17220 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17221 int last_y;
17222
17223 /* If we happen to start on a header-line, skip that. */
17224 if (row->mode_line_p)
17225 ++row;
17226
17227 if ((end && row >= end) || !row->enabled_p)
17228 return NULL;
17229
17230 last_y = window_text_bottom_y (w) - dy;
17231
17232 while (1)
17233 {
17234 /* Give up if we have gone too far. */
17235 if (end && row >= end)
17236 return NULL;
17237 /* This formerly returned if they were equal.
17238 I think that both quantities are of a "last plus one" type;
17239 if so, when they are equal, the row is within the screen. -- rms. */
17240 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17241 return NULL;
17242
17243 /* If it is in this row, return this row. */
17244 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17245 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17246 /* The end position of a row equals the start
17247 position of the next row. If CHARPOS is there, we
17248 would rather consider it displayed in the next
17249 line, except when this line ends in ZV. */
17250 && !row_for_charpos_p (row, charpos)))
17251 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17252 {
17253 struct glyph *g;
17254
17255 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17256 || (!best_row && !row->continued_p))
17257 return row;
17258 /* In bidi-reordered rows, there could be several rows whose
17259 edges surround CHARPOS, all of these rows belonging to
17260 the same continued line. We need to find the row which
17261 fits CHARPOS the best. */
17262 for (g = row->glyphs[TEXT_AREA];
17263 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17264 g++)
17265 {
17266 if (!STRINGP (g->object))
17267 {
17268 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17269 {
17270 mindif = eabs (g->charpos - charpos);
17271 best_row = row;
17272 /* Exact match always wins. */
17273 if (mindif == 0)
17274 return best_row;
17275 }
17276 }
17277 }
17278 }
17279 else if (best_row && !row->continued_p)
17280 return best_row;
17281 ++row;
17282 }
17283 }
17284
17285
17286 /* Try to redisplay window W by reusing its existing display. W's
17287 current matrix must be up to date when this function is called,
17288 i.e. window_end_valid must be nonzero.
17289
17290 Value is
17291
17292 1 if display has been updated
17293 0 if otherwise unsuccessful
17294 -1 if redisplay with same window start is known not to succeed
17295
17296 The following steps are performed:
17297
17298 1. Find the last row in the current matrix of W that is not
17299 affected by changes at the start of current_buffer. If no such row
17300 is found, give up.
17301
17302 2. Find the first row in W's current matrix that is not affected by
17303 changes at the end of current_buffer. Maybe there is no such row.
17304
17305 3. Display lines beginning with the row + 1 found in step 1 to the
17306 row found in step 2 or, if step 2 didn't find a row, to the end of
17307 the window.
17308
17309 4. If cursor is not known to appear on the window, give up.
17310
17311 5. If display stopped at the row found in step 2, scroll the
17312 display and current matrix as needed.
17313
17314 6. Maybe display some lines at the end of W, if we must. This can
17315 happen under various circumstances, like a partially visible line
17316 becoming fully visible, or because newly displayed lines are displayed
17317 in smaller font sizes.
17318
17319 7. Update W's window end information. */
17320
17321 static int
17322 try_window_id (struct window *w)
17323 {
17324 struct frame *f = XFRAME (w->frame);
17325 struct glyph_matrix *current_matrix = w->current_matrix;
17326 struct glyph_matrix *desired_matrix = w->desired_matrix;
17327 struct glyph_row *last_unchanged_at_beg_row;
17328 struct glyph_row *first_unchanged_at_end_row;
17329 struct glyph_row *row;
17330 struct glyph_row *bottom_row;
17331 int bottom_vpos;
17332 struct it it;
17333 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17334 int dvpos, dy;
17335 struct text_pos start_pos;
17336 struct run run;
17337 int first_unchanged_at_end_vpos = 0;
17338 struct glyph_row *last_text_row, *last_text_row_at_end;
17339 struct text_pos start;
17340 ptrdiff_t first_changed_charpos, last_changed_charpos;
17341
17342 #ifdef GLYPH_DEBUG
17343 if (inhibit_try_window_id)
17344 return 0;
17345 #endif
17346
17347 /* This is handy for debugging. */
17348 #if 0
17349 #define GIVE_UP(X) \
17350 do { \
17351 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17352 return 0; \
17353 } while (0)
17354 #else
17355 #define GIVE_UP(X) return 0
17356 #endif
17357
17358 SET_TEXT_POS_FROM_MARKER (start, w->start);
17359
17360 /* Don't use this for mini-windows because these can show
17361 messages and mini-buffers, and we don't handle that here. */
17362 if (MINI_WINDOW_P (w))
17363 GIVE_UP (1);
17364
17365 /* This flag is used to prevent redisplay optimizations. */
17366 if (windows_or_buffers_changed || f->cursor_type_changed)
17367 GIVE_UP (2);
17368
17369 /* Verify that narrowing has not changed.
17370 Also verify that we were not told to prevent redisplay optimizations.
17371 It would be nice to further
17372 reduce the number of cases where this prevents try_window_id. */
17373 if (current_buffer->clip_changed
17374 || current_buffer->prevent_redisplay_optimizations_p)
17375 GIVE_UP (3);
17376
17377 /* Window must either use window-based redisplay or be full width. */
17378 if (!FRAME_WINDOW_P (f)
17379 && (!FRAME_LINE_INS_DEL_OK (f)
17380 || !WINDOW_FULL_WIDTH_P (w)))
17381 GIVE_UP (4);
17382
17383 /* Give up if point is known NOT to appear in W. */
17384 if (PT < CHARPOS (start))
17385 GIVE_UP (5);
17386
17387 /* Another way to prevent redisplay optimizations. */
17388 if (w->last_modified == 0)
17389 GIVE_UP (6);
17390
17391 /* Verify that window is not hscrolled. */
17392 if (w->hscroll != 0)
17393 GIVE_UP (7);
17394
17395 /* Verify that display wasn't paused. */
17396 if (!w->window_end_valid)
17397 GIVE_UP (8);
17398
17399 /* Likewise if highlighting trailing whitespace. */
17400 if (!NILP (Vshow_trailing_whitespace))
17401 GIVE_UP (11);
17402
17403 /* Can't use this if overlay arrow position and/or string have
17404 changed. */
17405 if (overlay_arrows_changed_p ())
17406 GIVE_UP (12);
17407
17408 /* When word-wrap is on, adding a space to the first word of a
17409 wrapped line can change the wrap position, altering the line
17410 above it. It might be worthwhile to handle this more
17411 intelligently, but for now just redisplay from scratch. */
17412 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17413 GIVE_UP (21);
17414
17415 /* Under bidi reordering, adding or deleting a character in the
17416 beginning of a paragraph, before the first strong directional
17417 character, can change the base direction of the paragraph (unless
17418 the buffer specifies a fixed paragraph direction), which will
17419 require to redisplay the whole paragraph. It might be worthwhile
17420 to find the paragraph limits and widen the range of redisplayed
17421 lines to that, but for now just give up this optimization and
17422 redisplay from scratch. */
17423 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17424 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17425 GIVE_UP (22);
17426
17427 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17428 only if buffer has really changed. The reason is that the gap is
17429 initially at Z for freshly visited files. The code below would
17430 set end_unchanged to 0 in that case. */
17431 if (MODIFF > SAVE_MODIFF
17432 /* This seems to happen sometimes after saving a buffer. */
17433 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17434 {
17435 if (GPT - BEG < BEG_UNCHANGED)
17436 BEG_UNCHANGED = GPT - BEG;
17437 if (Z - GPT < END_UNCHANGED)
17438 END_UNCHANGED = Z - GPT;
17439 }
17440
17441 /* The position of the first and last character that has been changed. */
17442 first_changed_charpos = BEG + BEG_UNCHANGED;
17443 last_changed_charpos = Z - END_UNCHANGED;
17444
17445 /* If window starts after a line end, and the last change is in
17446 front of that newline, then changes don't affect the display.
17447 This case happens with stealth-fontification. Note that although
17448 the display is unchanged, glyph positions in the matrix have to
17449 be adjusted, of course. */
17450 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17451 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17452 && ((last_changed_charpos < CHARPOS (start)
17453 && CHARPOS (start) == BEGV)
17454 || (last_changed_charpos < CHARPOS (start) - 1
17455 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17456 {
17457 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17458 struct glyph_row *r0;
17459
17460 /* Compute how many chars/bytes have been added to or removed
17461 from the buffer. */
17462 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17463 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17464 Z_delta = Z - Z_old;
17465 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17466
17467 /* Give up if PT is not in the window. Note that it already has
17468 been checked at the start of try_window_id that PT is not in
17469 front of the window start. */
17470 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17471 GIVE_UP (13);
17472
17473 /* If window start is unchanged, we can reuse the whole matrix
17474 as is, after adjusting glyph positions. No need to compute
17475 the window end again, since its offset from Z hasn't changed. */
17476 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17477 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17478 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17479 /* PT must not be in a partially visible line. */
17480 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17481 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17482 {
17483 /* Adjust positions in the glyph matrix. */
17484 if (Z_delta || Z_delta_bytes)
17485 {
17486 struct glyph_row *r1
17487 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17488 increment_matrix_positions (w->current_matrix,
17489 MATRIX_ROW_VPOS (r0, current_matrix),
17490 MATRIX_ROW_VPOS (r1, current_matrix),
17491 Z_delta, Z_delta_bytes);
17492 }
17493
17494 /* Set the cursor. */
17495 row = row_containing_pos (w, PT, r0, NULL, 0);
17496 if (row)
17497 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17498 return 1;
17499 }
17500 }
17501
17502 /* Handle the case that changes are all below what is displayed in
17503 the window, and that PT is in the window. This shortcut cannot
17504 be taken if ZV is visible in the window, and text has been added
17505 there that is visible in the window. */
17506 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17507 /* ZV is not visible in the window, or there are no
17508 changes at ZV, actually. */
17509 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17510 || first_changed_charpos == last_changed_charpos))
17511 {
17512 struct glyph_row *r0;
17513
17514 /* Give up if PT is not in the window. Note that it already has
17515 been checked at the start of try_window_id that PT is not in
17516 front of the window start. */
17517 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17518 GIVE_UP (14);
17519
17520 /* If window start is unchanged, we can reuse the whole matrix
17521 as is, without changing glyph positions since no text has
17522 been added/removed in front of the window end. */
17523 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17524 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17525 /* PT must not be in a partially visible line. */
17526 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17527 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17528 {
17529 /* We have to compute the window end anew since text
17530 could have been added/removed after it. */
17531 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17532 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17533
17534 /* Set the cursor. */
17535 row = row_containing_pos (w, PT, r0, NULL, 0);
17536 if (row)
17537 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17538 return 2;
17539 }
17540 }
17541
17542 /* Give up if window start is in the changed area.
17543
17544 The condition used to read
17545
17546 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17547
17548 but why that was tested escapes me at the moment. */
17549 if (CHARPOS (start) >= first_changed_charpos
17550 && CHARPOS (start) <= last_changed_charpos)
17551 GIVE_UP (15);
17552
17553 /* Check that window start agrees with the start of the first glyph
17554 row in its current matrix. Check this after we know the window
17555 start is not in changed text, otherwise positions would not be
17556 comparable. */
17557 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17558 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17559 GIVE_UP (16);
17560
17561 /* Give up if the window ends in strings. Overlay strings
17562 at the end are difficult to handle, so don't try. */
17563 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17564 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17565 GIVE_UP (20);
17566
17567 /* Compute the position at which we have to start displaying new
17568 lines. Some of the lines at the top of the window might be
17569 reusable because they are not displaying changed text. Find the
17570 last row in W's current matrix not affected by changes at the
17571 start of current_buffer. Value is null if changes start in the
17572 first line of window. */
17573 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17574 if (last_unchanged_at_beg_row)
17575 {
17576 /* Avoid starting to display in the middle of a character, a TAB
17577 for instance. This is easier than to set up the iterator
17578 exactly, and it's not a frequent case, so the additional
17579 effort wouldn't really pay off. */
17580 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17581 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17582 && last_unchanged_at_beg_row > w->current_matrix->rows)
17583 --last_unchanged_at_beg_row;
17584
17585 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17586 GIVE_UP (17);
17587
17588 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17589 GIVE_UP (18);
17590 start_pos = it.current.pos;
17591
17592 /* Start displaying new lines in the desired matrix at the same
17593 vpos we would use in the current matrix, i.e. below
17594 last_unchanged_at_beg_row. */
17595 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17596 current_matrix);
17597 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17598 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17599
17600 eassert (it.hpos == 0 && it.current_x == 0);
17601 }
17602 else
17603 {
17604 /* There are no reusable lines at the start of the window.
17605 Start displaying in the first text line. */
17606 start_display (&it, w, start);
17607 it.vpos = it.first_vpos;
17608 start_pos = it.current.pos;
17609 }
17610
17611 /* Find the first row that is not affected by changes at the end of
17612 the buffer. Value will be null if there is no unchanged row, in
17613 which case we must redisplay to the end of the window. delta
17614 will be set to the value by which buffer positions beginning with
17615 first_unchanged_at_end_row have to be adjusted due to text
17616 changes. */
17617 first_unchanged_at_end_row
17618 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17619 IF_DEBUG (debug_delta = delta);
17620 IF_DEBUG (debug_delta_bytes = delta_bytes);
17621
17622 /* Set stop_pos to the buffer position up to which we will have to
17623 display new lines. If first_unchanged_at_end_row != NULL, this
17624 is the buffer position of the start of the line displayed in that
17625 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17626 that we don't stop at a buffer position. */
17627 stop_pos = 0;
17628 if (first_unchanged_at_end_row)
17629 {
17630 eassert (last_unchanged_at_beg_row == NULL
17631 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17632
17633 /* If this is a continuation line, move forward to the next one
17634 that isn't. Changes in lines above affect this line.
17635 Caution: this may move first_unchanged_at_end_row to a row
17636 not displaying text. */
17637 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17638 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17639 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17640 < it.last_visible_y))
17641 ++first_unchanged_at_end_row;
17642
17643 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17644 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17645 >= it.last_visible_y))
17646 first_unchanged_at_end_row = NULL;
17647 else
17648 {
17649 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17650 + delta);
17651 first_unchanged_at_end_vpos
17652 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17653 eassert (stop_pos >= Z - END_UNCHANGED);
17654 }
17655 }
17656 else if (last_unchanged_at_beg_row == NULL)
17657 GIVE_UP (19);
17658
17659
17660 #ifdef GLYPH_DEBUG
17661
17662 /* Either there is no unchanged row at the end, or the one we have
17663 now displays text. This is a necessary condition for the window
17664 end pos calculation at the end of this function. */
17665 eassert (first_unchanged_at_end_row == NULL
17666 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17667
17668 debug_last_unchanged_at_beg_vpos
17669 = (last_unchanged_at_beg_row
17670 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17671 : -1);
17672 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17673
17674 #endif /* GLYPH_DEBUG */
17675
17676
17677 /* Display new lines. Set last_text_row to the last new line
17678 displayed which has text on it, i.e. might end up as being the
17679 line where the window_end_vpos is. */
17680 w->cursor.vpos = -1;
17681 last_text_row = NULL;
17682 overlay_arrow_seen = 0;
17683 while (it.current_y < it.last_visible_y
17684 && !f->fonts_changed
17685 && (first_unchanged_at_end_row == NULL
17686 || IT_CHARPOS (it) < stop_pos))
17687 {
17688 if (display_line (&it))
17689 last_text_row = it.glyph_row - 1;
17690 }
17691
17692 if (f->fonts_changed)
17693 return -1;
17694
17695
17696 /* Compute differences in buffer positions, y-positions etc. for
17697 lines reused at the bottom of the window. Compute what we can
17698 scroll. */
17699 if (first_unchanged_at_end_row
17700 /* No lines reused because we displayed everything up to the
17701 bottom of the window. */
17702 && it.current_y < it.last_visible_y)
17703 {
17704 dvpos = (it.vpos
17705 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17706 current_matrix));
17707 dy = it.current_y - first_unchanged_at_end_row->y;
17708 run.current_y = first_unchanged_at_end_row->y;
17709 run.desired_y = run.current_y + dy;
17710 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17711 }
17712 else
17713 {
17714 delta = delta_bytes = dvpos = dy
17715 = run.current_y = run.desired_y = run.height = 0;
17716 first_unchanged_at_end_row = NULL;
17717 }
17718 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17719
17720
17721 /* Find the cursor if not already found. We have to decide whether
17722 PT will appear on this window (it sometimes doesn't, but this is
17723 not a very frequent case.) This decision has to be made before
17724 the current matrix is altered. A value of cursor.vpos < 0 means
17725 that PT is either in one of the lines beginning at
17726 first_unchanged_at_end_row or below the window. Don't care for
17727 lines that might be displayed later at the window end; as
17728 mentioned, this is not a frequent case. */
17729 if (w->cursor.vpos < 0)
17730 {
17731 /* Cursor in unchanged rows at the top? */
17732 if (PT < CHARPOS (start_pos)
17733 && last_unchanged_at_beg_row)
17734 {
17735 row = row_containing_pos (w, PT,
17736 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17737 last_unchanged_at_beg_row + 1, 0);
17738 if (row)
17739 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17740 }
17741
17742 /* Start from first_unchanged_at_end_row looking for PT. */
17743 else if (first_unchanged_at_end_row)
17744 {
17745 row = row_containing_pos (w, PT - delta,
17746 first_unchanged_at_end_row, NULL, 0);
17747 if (row)
17748 set_cursor_from_row (w, row, w->current_matrix, delta,
17749 delta_bytes, dy, dvpos);
17750 }
17751
17752 /* Give up if cursor was not found. */
17753 if (w->cursor.vpos < 0)
17754 {
17755 clear_glyph_matrix (w->desired_matrix);
17756 return -1;
17757 }
17758 }
17759
17760 /* Don't let the cursor end in the scroll margins. */
17761 {
17762 int this_scroll_margin, cursor_height;
17763 int frame_line_height = default_line_pixel_height (w);
17764 int window_total_lines
17765 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17766
17767 this_scroll_margin =
17768 max (0, min (scroll_margin, window_total_lines / 4));
17769 this_scroll_margin *= frame_line_height;
17770 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17771
17772 if ((w->cursor.y < this_scroll_margin
17773 && CHARPOS (start) > BEGV)
17774 /* Old redisplay didn't take scroll margin into account at the bottom,
17775 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17776 || (w->cursor.y + (make_cursor_line_fully_visible_p
17777 ? cursor_height + this_scroll_margin
17778 : 1)) > it.last_visible_y)
17779 {
17780 w->cursor.vpos = -1;
17781 clear_glyph_matrix (w->desired_matrix);
17782 return -1;
17783 }
17784 }
17785
17786 /* Scroll the display. Do it before changing the current matrix so
17787 that xterm.c doesn't get confused about where the cursor glyph is
17788 found. */
17789 if (dy && run.height)
17790 {
17791 update_begin (f);
17792
17793 if (FRAME_WINDOW_P (f))
17794 {
17795 FRAME_RIF (f)->update_window_begin_hook (w);
17796 FRAME_RIF (f)->clear_window_mouse_face (w);
17797 FRAME_RIF (f)->scroll_run_hook (w, &run);
17798 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17799 }
17800 else
17801 {
17802 /* Terminal frame. In this case, dvpos gives the number of
17803 lines to scroll by; dvpos < 0 means scroll up. */
17804 int from_vpos
17805 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17806 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17807 int end = (WINDOW_TOP_EDGE_LINE (w)
17808 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17809 + window_internal_height (w));
17810
17811 #if defined (HAVE_GPM) || defined (MSDOS)
17812 x_clear_window_mouse_face (w);
17813 #endif
17814 /* Perform the operation on the screen. */
17815 if (dvpos > 0)
17816 {
17817 /* Scroll last_unchanged_at_beg_row to the end of the
17818 window down dvpos lines. */
17819 set_terminal_window (f, end);
17820
17821 /* On dumb terminals delete dvpos lines at the end
17822 before inserting dvpos empty lines. */
17823 if (!FRAME_SCROLL_REGION_OK (f))
17824 ins_del_lines (f, end - dvpos, -dvpos);
17825
17826 /* Insert dvpos empty lines in front of
17827 last_unchanged_at_beg_row. */
17828 ins_del_lines (f, from, dvpos);
17829 }
17830 else if (dvpos < 0)
17831 {
17832 /* Scroll up last_unchanged_at_beg_vpos to the end of
17833 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17834 set_terminal_window (f, end);
17835
17836 /* Delete dvpos lines in front of
17837 last_unchanged_at_beg_vpos. ins_del_lines will set
17838 the cursor to the given vpos and emit |dvpos| delete
17839 line sequences. */
17840 ins_del_lines (f, from + dvpos, dvpos);
17841
17842 /* On a dumb terminal insert dvpos empty lines at the
17843 end. */
17844 if (!FRAME_SCROLL_REGION_OK (f))
17845 ins_del_lines (f, end + dvpos, -dvpos);
17846 }
17847
17848 set_terminal_window (f, 0);
17849 }
17850
17851 update_end (f);
17852 }
17853
17854 /* Shift reused rows of the current matrix to the right position.
17855 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17856 text. */
17857 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17858 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17859 if (dvpos < 0)
17860 {
17861 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17862 bottom_vpos, dvpos);
17863 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17864 bottom_vpos);
17865 }
17866 else if (dvpos > 0)
17867 {
17868 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17869 bottom_vpos, dvpos);
17870 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17871 first_unchanged_at_end_vpos + dvpos);
17872 }
17873
17874 /* For frame-based redisplay, make sure that current frame and window
17875 matrix are in sync with respect to glyph memory. */
17876 if (!FRAME_WINDOW_P (f))
17877 sync_frame_with_window_matrix_rows (w);
17878
17879 /* Adjust buffer positions in reused rows. */
17880 if (delta || delta_bytes)
17881 increment_matrix_positions (current_matrix,
17882 first_unchanged_at_end_vpos + dvpos,
17883 bottom_vpos, delta, delta_bytes);
17884
17885 /* Adjust Y positions. */
17886 if (dy)
17887 shift_glyph_matrix (w, current_matrix,
17888 first_unchanged_at_end_vpos + dvpos,
17889 bottom_vpos, dy);
17890
17891 if (first_unchanged_at_end_row)
17892 {
17893 first_unchanged_at_end_row += dvpos;
17894 if (first_unchanged_at_end_row->y >= it.last_visible_y
17895 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17896 first_unchanged_at_end_row = NULL;
17897 }
17898
17899 /* If scrolling up, there may be some lines to display at the end of
17900 the window. */
17901 last_text_row_at_end = NULL;
17902 if (dy < 0)
17903 {
17904 /* Scrolling up can leave for example a partially visible line
17905 at the end of the window to be redisplayed. */
17906 /* Set last_row to the glyph row in the current matrix where the
17907 window end line is found. It has been moved up or down in
17908 the matrix by dvpos. */
17909 int last_vpos = w->window_end_vpos + dvpos;
17910 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17911
17912 /* If last_row is the window end line, it should display text. */
17913 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17914
17915 /* If window end line was partially visible before, begin
17916 displaying at that line. Otherwise begin displaying with the
17917 line following it. */
17918 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17919 {
17920 init_to_row_start (&it, w, last_row);
17921 it.vpos = last_vpos;
17922 it.current_y = last_row->y;
17923 }
17924 else
17925 {
17926 init_to_row_end (&it, w, last_row);
17927 it.vpos = 1 + last_vpos;
17928 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17929 ++last_row;
17930 }
17931
17932 /* We may start in a continuation line. If so, we have to
17933 get the right continuation_lines_width and current_x. */
17934 it.continuation_lines_width = last_row->continuation_lines_width;
17935 it.hpos = it.current_x = 0;
17936
17937 /* Display the rest of the lines at the window end. */
17938 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17939 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17940 {
17941 /* Is it always sure that the display agrees with lines in
17942 the current matrix? I don't think so, so we mark rows
17943 displayed invalid in the current matrix by setting their
17944 enabled_p flag to zero. */
17945 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17946 if (display_line (&it))
17947 last_text_row_at_end = it.glyph_row - 1;
17948 }
17949 }
17950
17951 /* Update window_end_pos and window_end_vpos. */
17952 if (first_unchanged_at_end_row && !last_text_row_at_end)
17953 {
17954 /* Window end line if one of the preserved rows from the current
17955 matrix. Set row to the last row displaying text in current
17956 matrix starting at first_unchanged_at_end_row, after
17957 scrolling. */
17958 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17959 row = find_last_row_displaying_text (w->current_matrix, &it,
17960 first_unchanged_at_end_row);
17961 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17962 adjust_window_ends (w, row, 1);
17963 eassert (w->window_end_bytepos >= 0);
17964 IF_DEBUG (debug_method_add (w, "A"));
17965 }
17966 else if (last_text_row_at_end)
17967 {
17968 adjust_window_ends (w, last_text_row_at_end, 0);
17969 eassert (w->window_end_bytepos >= 0);
17970 IF_DEBUG (debug_method_add (w, "B"));
17971 }
17972 else if (last_text_row)
17973 {
17974 /* We have displayed either to the end of the window or at the
17975 end of the window, i.e. the last row with text is to be found
17976 in the desired matrix. */
17977 adjust_window_ends (w, last_text_row, 0);
17978 eassert (w->window_end_bytepos >= 0);
17979 }
17980 else if (first_unchanged_at_end_row == NULL
17981 && last_text_row == NULL
17982 && last_text_row_at_end == NULL)
17983 {
17984 /* Displayed to end of window, but no line containing text was
17985 displayed. Lines were deleted at the end of the window. */
17986 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17987 int vpos = w->window_end_vpos;
17988 struct glyph_row *current_row = current_matrix->rows + vpos;
17989 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17990
17991 for (row = NULL;
17992 row == NULL && vpos >= first_vpos;
17993 --vpos, --current_row, --desired_row)
17994 {
17995 if (desired_row->enabled_p)
17996 {
17997 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17998 row = desired_row;
17999 }
18000 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18001 row = current_row;
18002 }
18003
18004 eassert (row != NULL);
18005 w->window_end_vpos = vpos + 1;
18006 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18007 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18008 eassert (w->window_end_bytepos >= 0);
18009 IF_DEBUG (debug_method_add (w, "C"));
18010 }
18011 else
18012 emacs_abort ();
18013
18014 IF_DEBUG (debug_end_pos = w->window_end_pos;
18015 debug_end_vpos = w->window_end_vpos);
18016
18017 /* Record that display has not been completed. */
18018 w->window_end_valid = 0;
18019 w->desired_matrix->no_scrolling_p = 1;
18020 return 3;
18021
18022 #undef GIVE_UP
18023 }
18024
18025
18026 \f
18027 /***********************************************************************
18028 More debugging support
18029 ***********************************************************************/
18030
18031 #ifdef GLYPH_DEBUG
18032
18033 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18034 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18035 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18036
18037
18038 /* Dump the contents of glyph matrix MATRIX on stderr.
18039
18040 GLYPHS 0 means don't show glyph contents.
18041 GLYPHS 1 means show glyphs in short form
18042 GLYPHS > 1 means show glyphs in long form. */
18043
18044 void
18045 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18046 {
18047 int i;
18048 for (i = 0; i < matrix->nrows; ++i)
18049 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18050 }
18051
18052
18053 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18054 the glyph row and area where the glyph comes from. */
18055
18056 void
18057 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18058 {
18059 if (glyph->type == CHAR_GLYPH
18060 || glyph->type == GLYPHLESS_GLYPH)
18061 {
18062 fprintf (stderr,
18063 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18064 glyph - row->glyphs[TEXT_AREA],
18065 (glyph->type == CHAR_GLYPH
18066 ? 'C'
18067 : 'G'),
18068 glyph->charpos,
18069 (BUFFERP (glyph->object)
18070 ? 'B'
18071 : (STRINGP (glyph->object)
18072 ? 'S'
18073 : (INTEGERP (glyph->object)
18074 ? '0'
18075 : '-'))),
18076 glyph->pixel_width,
18077 glyph->u.ch,
18078 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18079 ? glyph->u.ch
18080 : '.'),
18081 glyph->face_id,
18082 glyph->left_box_line_p,
18083 glyph->right_box_line_p);
18084 }
18085 else if (glyph->type == STRETCH_GLYPH)
18086 {
18087 fprintf (stderr,
18088 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18089 glyph - row->glyphs[TEXT_AREA],
18090 'S',
18091 glyph->charpos,
18092 (BUFFERP (glyph->object)
18093 ? 'B'
18094 : (STRINGP (glyph->object)
18095 ? 'S'
18096 : (INTEGERP (glyph->object)
18097 ? '0'
18098 : '-'))),
18099 glyph->pixel_width,
18100 0,
18101 ' ',
18102 glyph->face_id,
18103 glyph->left_box_line_p,
18104 glyph->right_box_line_p);
18105 }
18106 else if (glyph->type == IMAGE_GLYPH)
18107 {
18108 fprintf (stderr,
18109 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18110 glyph - row->glyphs[TEXT_AREA],
18111 'I',
18112 glyph->charpos,
18113 (BUFFERP (glyph->object)
18114 ? 'B'
18115 : (STRINGP (glyph->object)
18116 ? 'S'
18117 : (INTEGERP (glyph->object)
18118 ? '0'
18119 : '-'))),
18120 glyph->pixel_width,
18121 glyph->u.img_id,
18122 '.',
18123 glyph->face_id,
18124 glyph->left_box_line_p,
18125 glyph->right_box_line_p);
18126 }
18127 else if (glyph->type == COMPOSITE_GLYPH)
18128 {
18129 fprintf (stderr,
18130 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18131 glyph - row->glyphs[TEXT_AREA],
18132 '+',
18133 glyph->charpos,
18134 (BUFFERP (glyph->object)
18135 ? 'B'
18136 : (STRINGP (glyph->object)
18137 ? 'S'
18138 : (INTEGERP (glyph->object)
18139 ? '0'
18140 : '-'))),
18141 glyph->pixel_width,
18142 glyph->u.cmp.id);
18143 if (glyph->u.cmp.automatic)
18144 fprintf (stderr,
18145 "[%d-%d]",
18146 glyph->slice.cmp.from, glyph->slice.cmp.to);
18147 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18148 glyph->face_id,
18149 glyph->left_box_line_p,
18150 glyph->right_box_line_p);
18151 }
18152 }
18153
18154
18155 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18156 GLYPHS 0 means don't show glyph contents.
18157 GLYPHS 1 means show glyphs in short form
18158 GLYPHS > 1 means show glyphs in long form. */
18159
18160 void
18161 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18162 {
18163 if (glyphs != 1)
18164 {
18165 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18166 fprintf (stderr, "==============================================================================\n");
18167
18168 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18169 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18170 vpos,
18171 MATRIX_ROW_START_CHARPOS (row),
18172 MATRIX_ROW_END_CHARPOS (row),
18173 row->used[TEXT_AREA],
18174 row->contains_overlapping_glyphs_p,
18175 row->enabled_p,
18176 row->truncated_on_left_p,
18177 row->truncated_on_right_p,
18178 row->continued_p,
18179 MATRIX_ROW_CONTINUATION_LINE_P (row),
18180 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18181 row->ends_at_zv_p,
18182 row->fill_line_p,
18183 row->ends_in_middle_of_char_p,
18184 row->starts_in_middle_of_char_p,
18185 row->mouse_face_p,
18186 row->x,
18187 row->y,
18188 row->pixel_width,
18189 row->height,
18190 row->visible_height,
18191 row->ascent,
18192 row->phys_ascent);
18193 /* The next 3 lines should align to "Start" in the header. */
18194 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18195 row->end.overlay_string_index,
18196 row->continuation_lines_width);
18197 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18198 CHARPOS (row->start.string_pos),
18199 CHARPOS (row->end.string_pos));
18200 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18201 row->end.dpvec_index);
18202 }
18203
18204 if (glyphs > 1)
18205 {
18206 int area;
18207
18208 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18209 {
18210 struct glyph *glyph = row->glyphs[area];
18211 struct glyph *glyph_end = glyph + row->used[area];
18212
18213 /* Glyph for a line end in text. */
18214 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18215 ++glyph_end;
18216
18217 if (glyph < glyph_end)
18218 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18219
18220 for (; glyph < glyph_end; ++glyph)
18221 dump_glyph (row, glyph, area);
18222 }
18223 }
18224 else if (glyphs == 1)
18225 {
18226 int area;
18227
18228 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18229 {
18230 char *s = alloca (row->used[area] + 4);
18231 int i;
18232
18233 for (i = 0; i < row->used[area]; ++i)
18234 {
18235 struct glyph *glyph = row->glyphs[area] + i;
18236 if (i == row->used[area] - 1
18237 && area == TEXT_AREA
18238 && INTEGERP (glyph->object)
18239 && glyph->type == CHAR_GLYPH
18240 && glyph->u.ch == ' ')
18241 {
18242 strcpy (&s[i], "[\\n]");
18243 i += 4;
18244 }
18245 else if (glyph->type == CHAR_GLYPH
18246 && glyph->u.ch < 0x80
18247 && glyph->u.ch >= ' ')
18248 s[i] = glyph->u.ch;
18249 else
18250 s[i] = '.';
18251 }
18252
18253 s[i] = '\0';
18254 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18255 }
18256 }
18257 }
18258
18259
18260 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18261 Sdump_glyph_matrix, 0, 1, "p",
18262 doc: /* Dump the current matrix of the selected window to stderr.
18263 Shows contents of glyph row structures. With non-nil
18264 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18265 glyphs in short form, otherwise show glyphs in long form. */)
18266 (Lisp_Object glyphs)
18267 {
18268 struct window *w = XWINDOW (selected_window);
18269 struct buffer *buffer = XBUFFER (w->contents);
18270
18271 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18272 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18273 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18274 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18275 fprintf (stderr, "=============================================\n");
18276 dump_glyph_matrix (w->current_matrix,
18277 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18278 return Qnil;
18279 }
18280
18281
18282 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18283 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18284 (void)
18285 {
18286 struct frame *f = XFRAME (selected_frame);
18287 dump_glyph_matrix (f->current_matrix, 1);
18288 return Qnil;
18289 }
18290
18291
18292 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18293 doc: /* Dump glyph row ROW to stderr.
18294 GLYPH 0 means don't dump glyphs.
18295 GLYPH 1 means dump glyphs in short form.
18296 GLYPH > 1 or omitted means dump glyphs in long form. */)
18297 (Lisp_Object row, Lisp_Object glyphs)
18298 {
18299 struct glyph_matrix *matrix;
18300 EMACS_INT vpos;
18301
18302 CHECK_NUMBER (row);
18303 matrix = XWINDOW (selected_window)->current_matrix;
18304 vpos = XINT (row);
18305 if (vpos >= 0 && vpos < matrix->nrows)
18306 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18307 vpos,
18308 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18309 return Qnil;
18310 }
18311
18312
18313 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18314 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18315 GLYPH 0 means don't dump glyphs.
18316 GLYPH 1 means dump glyphs in short form.
18317 GLYPH > 1 or omitted means dump glyphs in long form.
18318
18319 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18320 do nothing. */)
18321 (Lisp_Object row, Lisp_Object glyphs)
18322 {
18323 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18324 struct frame *sf = SELECTED_FRAME ();
18325 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18326 EMACS_INT vpos;
18327
18328 CHECK_NUMBER (row);
18329 vpos = XINT (row);
18330 if (vpos >= 0 && vpos < m->nrows)
18331 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18332 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18333 #endif
18334 return Qnil;
18335 }
18336
18337
18338 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18339 doc: /* Toggle tracing of redisplay.
18340 With ARG, turn tracing on if and only if ARG is positive. */)
18341 (Lisp_Object arg)
18342 {
18343 if (NILP (arg))
18344 trace_redisplay_p = !trace_redisplay_p;
18345 else
18346 {
18347 arg = Fprefix_numeric_value (arg);
18348 trace_redisplay_p = XINT (arg) > 0;
18349 }
18350
18351 return Qnil;
18352 }
18353
18354
18355 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18356 doc: /* Like `format', but print result to stderr.
18357 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18358 (ptrdiff_t nargs, Lisp_Object *args)
18359 {
18360 Lisp_Object s = Fformat (nargs, args);
18361 fprintf (stderr, "%s", SDATA (s));
18362 return Qnil;
18363 }
18364
18365 #endif /* GLYPH_DEBUG */
18366
18367
18368 \f
18369 /***********************************************************************
18370 Building Desired Matrix Rows
18371 ***********************************************************************/
18372
18373 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18374 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18375
18376 static struct glyph_row *
18377 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18378 {
18379 struct frame *f = XFRAME (WINDOW_FRAME (w));
18380 struct buffer *buffer = XBUFFER (w->contents);
18381 struct buffer *old = current_buffer;
18382 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18383 int arrow_len = SCHARS (overlay_arrow_string);
18384 const unsigned char *arrow_end = arrow_string + arrow_len;
18385 const unsigned char *p;
18386 struct it it;
18387 bool multibyte_p;
18388 int n_glyphs_before;
18389
18390 set_buffer_temp (buffer);
18391 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18392 it.glyph_row->used[TEXT_AREA] = 0;
18393 SET_TEXT_POS (it.position, 0, 0);
18394
18395 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18396 p = arrow_string;
18397 while (p < arrow_end)
18398 {
18399 Lisp_Object face, ilisp;
18400
18401 /* Get the next character. */
18402 if (multibyte_p)
18403 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18404 else
18405 {
18406 it.c = it.char_to_display = *p, it.len = 1;
18407 if (! ASCII_CHAR_P (it.c))
18408 it.char_to_display = BYTE8_TO_CHAR (it.c);
18409 }
18410 p += it.len;
18411
18412 /* Get its face. */
18413 ilisp = make_number (p - arrow_string);
18414 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18415 it.face_id = compute_char_face (f, it.char_to_display, face);
18416
18417 /* Compute its width, get its glyphs. */
18418 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18419 SET_TEXT_POS (it.position, -1, -1);
18420 PRODUCE_GLYPHS (&it);
18421
18422 /* If this character doesn't fit any more in the line, we have
18423 to remove some glyphs. */
18424 if (it.current_x > it.last_visible_x)
18425 {
18426 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18427 break;
18428 }
18429 }
18430
18431 set_buffer_temp (old);
18432 return it.glyph_row;
18433 }
18434
18435
18436 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18437 glyphs to insert is determined by produce_special_glyphs. */
18438
18439 static void
18440 insert_left_trunc_glyphs (struct it *it)
18441 {
18442 struct it truncate_it;
18443 struct glyph *from, *end, *to, *toend;
18444
18445 eassert (!FRAME_WINDOW_P (it->f)
18446 || (!it->glyph_row->reversed_p
18447 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18448 || (it->glyph_row->reversed_p
18449 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18450
18451 /* Get the truncation glyphs. */
18452 truncate_it = *it;
18453 truncate_it.current_x = 0;
18454 truncate_it.face_id = DEFAULT_FACE_ID;
18455 truncate_it.glyph_row = &scratch_glyph_row;
18456 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18457 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18458 truncate_it.object = make_number (0);
18459 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18460
18461 /* Overwrite glyphs from IT with truncation glyphs. */
18462 if (!it->glyph_row->reversed_p)
18463 {
18464 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18465
18466 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18467 end = from + tused;
18468 to = it->glyph_row->glyphs[TEXT_AREA];
18469 toend = to + it->glyph_row->used[TEXT_AREA];
18470 if (FRAME_WINDOW_P (it->f))
18471 {
18472 /* On GUI frames, when variable-size fonts are displayed,
18473 the truncation glyphs may need more pixels than the row's
18474 glyphs they overwrite. We overwrite more glyphs to free
18475 enough screen real estate, and enlarge the stretch glyph
18476 on the right (see display_line), if there is one, to
18477 preserve the screen position of the truncation glyphs on
18478 the right. */
18479 int w = 0;
18480 struct glyph *g = to;
18481 short used;
18482
18483 /* The first glyph could be partially visible, in which case
18484 it->glyph_row->x will be negative. But we want the left
18485 truncation glyphs to be aligned at the left margin of the
18486 window, so we override the x coordinate at which the row
18487 will begin. */
18488 it->glyph_row->x = 0;
18489 while (g < toend && w < it->truncation_pixel_width)
18490 {
18491 w += g->pixel_width;
18492 ++g;
18493 }
18494 if (g - to - tused > 0)
18495 {
18496 memmove (to + tused, g, (toend - g) * sizeof(*g));
18497 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18498 }
18499 used = it->glyph_row->used[TEXT_AREA];
18500 if (it->glyph_row->truncated_on_right_p
18501 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18502 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18503 == STRETCH_GLYPH)
18504 {
18505 int extra = w - it->truncation_pixel_width;
18506
18507 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18508 }
18509 }
18510
18511 while (from < end)
18512 *to++ = *from++;
18513
18514 /* There may be padding glyphs left over. Overwrite them too. */
18515 if (!FRAME_WINDOW_P (it->f))
18516 {
18517 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18518 {
18519 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18520 while (from < end)
18521 *to++ = *from++;
18522 }
18523 }
18524
18525 if (to > toend)
18526 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18527 }
18528 else
18529 {
18530 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18531
18532 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18533 that back to front. */
18534 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18535 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18536 toend = it->glyph_row->glyphs[TEXT_AREA];
18537 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18538 if (FRAME_WINDOW_P (it->f))
18539 {
18540 int w = 0;
18541 struct glyph *g = to;
18542
18543 while (g >= toend && w < it->truncation_pixel_width)
18544 {
18545 w += g->pixel_width;
18546 --g;
18547 }
18548 if (to - g - tused > 0)
18549 to = g + tused;
18550 if (it->glyph_row->truncated_on_right_p
18551 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18552 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18553 {
18554 int extra = w - it->truncation_pixel_width;
18555
18556 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18557 }
18558 }
18559
18560 while (from >= end && to >= toend)
18561 *to-- = *from--;
18562 if (!FRAME_WINDOW_P (it->f))
18563 {
18564 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18565 {
18566 from =
18567 truncate_it.glyph_row->glyphs[TEXT_AREA]
18568 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18569 while (from >= end && to >= toend)
18570 *to-- = *from--;
18571 }
18572 }
18573 if (from >= end)
18574 {
18575 /* Need to free some room before prepending additional
18576 glyphs. */
18577 int move_by = from - end + 1;
18578 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18579 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18580
18581 for ( ; g >= g0; g--)
18582 g[move_by] = *g;
18583 while (from >= end)
18584 *to-- = *from--;
18585 it->glyph_row->used[TEXT_AREA] += move_by;
18586 }
18587 }
18588 }
18589
18590 /* Compute the hash code for ROW. */
18591 unsigned
18592 row_hash (struct glyph_row *row)
18593 {
18594 int area, k;
18595 unsigned hashval = 0;
18596
18597 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18598 for (k = 0; k < row->used[area]; ++k)
18599 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18600 + row->glyphs[area][k].u.val
18601 + row->glyphs[area][k].face_id
18602 + row->glyphs[area][k].padding_p
18603 + (row->glyphs[area][k].type << 2));
18604
18605 return hashval;
18606 }
18607
18608 /* Compute the pixel height and width of IT->glyph_row.
18609
18610 Most of the time, ascent and height of a display line will be equal
18611 to the max_ascent and max_height values of the display iterator
18612 structure. This is not the case if
18613
18614 1. We hit ZV without displaying anything. In this case, max_ascent
18615 and max_height will be zero.
18616
18617 2. We have some glyphs that don't contribute to the line height.
18618 (The glyph row flag contributes_to_line_height_p is for future
18619 pixmap extensions).
18620
18621 The first case is easily covered by using default values because in
18622 these cases, the line height does not really matter, except that it
18623 must not be zero. */
18624
18625 static void
18626 compute_line_metrics (struct it *it)
18627 {
18628 struct glyph_row *row = it->glyph_row;
18629
18630 if (FRAME_WINDOW_P (it->f))
18631 {
18632 int i, min_y, max_y;
18633
18634 /* The line may consist of one space only, that was added to
18635 place the cursor on it. If so, the row's height hasn't been
18636 computed yet. */
18637 if (row->height == 0)
18638 {
18639 if (it->max_ascent + it->max_descent == 0)
18640 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18641 row->ascent = it->max_ascent;
18642 row->height = it->max_ascent + it->max_descent;
18643 row->phys_ascent = it->max_phys_ascent;
18644 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18645 row->extra_line_spacing = it->max_extra_line_spacing;
18646 }
18647
18648 /* Compute the width of this line. */
18649 row->pixel_width = row->x;
18650 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18651 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18652
18653 eassert (row->pixel_width >= 0);
18654 eassert (row->ascent >= 0 && row->height > 0);
18655
18656 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18657 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18658
18659 /* If first line's physical ascent is larger than its logical
18660 ascent, use the physical ascent, and make the row taller.
18661 This makes accented characters fully visible. */
18662 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18663 && row->phys_ascent > row->ascent)
18664 {
18665 row->height += row->phys_ascent - row->ascent;
18666 row->ascent = row->phys_ascent;
18667 }
18668
18669 /* Compute how much of the line is visible. */
18670 row->visible_height = row->height;
18671
18672 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18673 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18674
18675 if (row->y < min_y)
18676 row->visible_height -= min_y - row->y;
18677 if (row->y + row->height > max_y)
18678 row->visible_height -= row->y + row->height - max_y;
18679 }
18680 else
18681 {
18682 row->pixel_width = row->used[TEXT_AREA];
18683 if (row->continued_p)
18684 row->pixel_width -= it->continuation_pixel_width;
18685 else if (row->truncated_on_right_p)
18686 row->pixel_width -= it->truncation_pixel_width;
18687 row->ascent = row->phys_ascent = 0;
18688 row->height = row->phys_height = row->visible_height = 1;
18689 row->extra_line_spacing = 0;
18690 }
18691
18692 /* Compute a hash code for this row. */
18693 row->hash = row_hash (row);
18694
18695 it->max_ascent = it->max_descent = 0;
18696 it->max_phys_ascent = it->max_phys_descent = 0;
18697 }
18698
18699
18700 /* Append one space to the glyph row of iterator IT if doing a
18701 window-based redisplay. The space has the same face as
18702 IT->face_id. Value is non-zero if a space was added.
18703
18704 This function is called to make sure that there is always one glyph
18705 at the end of a glyph row that the cursor can be set on under
18706 window-systems. (If there weren't such a glyph we would not know
18707 how wide and tall a box cursor should be displayed).
18708
18709 At the same time this space let's a nicely handle clearing to the
18710 end of the line if the row ends in italic text. */
18711
18712 static int
18713 append_space_for_newline (struct it *it, int default_face_p)
18714 {
18715 if (FRAME_WINDOW_P (it->f))
18716 {
18717 int n = it->glyph_row->used[TEXT_AREA];
18718
18719 if (it->glyph_row->glyphs[TEXT_AREA] + n
18720 < it->glyph_row->glyphs[1 + TEXT_AREA])
18721 {
18722 /* Save some values that must not be changed.
18723 Must save IT->c and IT->len because otherwise
18724 ITERATOR_AT_END_P wouldn't work anymore after
18725 append_space_for_newline has been called. */
18726 enum display_element_type saved_what = it->what;
18727 int saved_c = it->c, saved_len = it->len;
18728 int saved_char_to_display = it->char_to_display;
18729 int saved_x = it->current_x;
18730 int saved_face_id = it->face_id;
18731 int saved_box_end = it->end_of_box_run_p;
18732 struct text_pos saved_pos;
18733 Lisp_Object saved_object;
18734 struct face *face;
18735
18736 saved_object = it->object;
18737 saved_pos = it->position;
18738
18739 it->what = IT_CHARACTER;
18740 memset (&it->position, 0, sizeof it->position);
18741 it->object = make_number (0);
18742 it->c = it->char_to_display = ' ';
18743 it->len = 1;
18744
18745 /* If the default face was remapped, be sure to use the
18746 remapped face for the appended newline. */
18747 if (default_face_p)
18748 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18749 else if (it->face_before_selective_p)
18750 it->face_id = it->saved_face_id;
18751 face = FACE_FROM_ID (it->f, it->face_id);
18752 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18753 /* In R2L rows, we will prepend a stretch glyph that will
18754 have the end_of_box_run_p flag set for it, so there's no
18755 need for the appended newline glyph to have that flag
18756 set. */
18757 if (it->glyph_row->reversed_p
18758 /* But if the appended newline glyph goes all the way to
18759 the end of the row, there will be no stretch glyph,
18760 so leave the box flag set. */
18761 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18762 it->end_of_box_run_p = 0;
18763
18764 PRODUCE_GLYPHS (it);
18765
18766 it->override_ascent = -1;
18767 it->constrain_row_ascent_descent_p = 0;
18768 it->current_x = saved_x;
18769 it->object = saved_object;
18770 it->position = saved_pos;
18771 it->what = saved_what;
18772 it->face_id = saved_face_id;
18773 it->len = saved_len;
18774 it->c = saved_c;
18775 it->char_to_display = saved_char_to_display;
18776 it->end_of_box_run_p = saved_box_end;
18777 return 1;
18778 }
18779 }
18780
18781 return 0;
18782 }
18783
18784
18785 /* Extend the face of the last glyph in the text area of IT->glyph_row
18786 to the end of the display line. Called from display_line. If the
18787 glyph row is empty, add a space glyph to it so that we know the
18788 face to draw. Set the glyph row flag fill_line_p. If the glyph
18789 row is R2L, prepend a stretch glyph to cover the empty space to the
18790 left of the leftmost glyph. */
18791
18792 static void
18793 extend_face_to_end_of_line (struct it *it)
18794 {
18795 struct face *face, *default_face;
18796 struct frame *f = it->f;
18797
18798 /* If line is already filled, do nothing. Non window-system frames
18799 get a grace of one more ``pixel'' because their characters are
18800 1-``pixel'' wide, so they hit the equality too early. This grace
18801 is needed only for R2L rows that are not continued, to produce
18802 one extra blank where we could display the cursor. */
18803 if (it->current_x >= it->last_visible_x
18804 + (!FRAME_WINDOW_P (f)
18805 && it->glyph_row->reversed_p
18806 && !it->glyph_row->continued_p))
18807 return;
18808
18809 /* The default face, possibly remapped. */
18810 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18811
18812 /* Face extension extends the background and box of IT->face_id
18813 to the end of the line. If the background equals the background
18814 of the frame, we don't have to do anything. */
18815 if (it->face_before_selective_p)
18816 face = FACE_FROM_ID (f, it->saved_face_id);
18817 else
18818 face = FACE_FROM_ID (f, it->face_id);
18819
18820 if (FRAME_WINDOW_P (f)
18821 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18822 && face->box == FACE_NO_BOX
18823 && face->background == FRAME_BACKGROUND_PIXEL (f)
18824 #ifdef HAVE_WINDOW_SYSTEM
18825 && !face->stipple
18826 #endif
18827 && !it->glyph_row->reversed_p)
18828 return;
18829
18830 /* Set the glyph row flag indicating that the face of the last glyph
18831 in the text area has to be drawn to the end of the text area. */
18832 it->glyph_row->fill_line_p = 1;
18833
18834 /* If current character of IT is not ASCII, make sure we have the
18835 ASCII face. This will be automatically undone the next time
18836 get_next_display_element returns a multibyte character. Note
18837 that the character will always be single byte in unibyte
18838 text. */
18839 if (!ASCII_CHAR_P (it->c))
18840 {
18841 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18842 }
18843
18844 if (FRAME_WINDOW_P (f))
18845 {
18846 /* If the row is empty, add a space with the current face of IT,
18847 so that we know which face to draw. */
18848 if (it->glyph_row->used[TEXT_AREA] == 0)
18849 {
18850 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18851 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18852 it->glyph_row->used[TEXT_AREA] = 1;
18853 }
18854 #ifdef HAVE_WINDOW_SYSTEM
18855 if (it->glyph_row->reversed_p)
18856 {
18857 /* Prepend a stretch glyph to the row, such that the
18858 rightmost glyph will be drawn flushed all the way to the
18859 right margin of the window. The stretch glyph that will
18860 occupy the empty space, if any, to the left of the
18861 glyphs. */
18862 struct font *font = face->font ? face->font : FRAME_FONT (f);
18863 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18864 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18865 struct glyph *g;
18866 int row_width, stretch_ascent, stretch_width;
18867 struct text_pos saved_pos;
18868 int saved_face_id, saved_avoid_cursor, saved_box_start;
18869
18870 for (row_width = 0, g = row_start; g < row_end; g++)
18871 row_width += g->pixel_width;
18872 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18873 if (stretch_width > 0)
18874 {
18875 stretch_ascent =
18876 (((it->ascent + it->descent)
18877 * FONT_BASE (font)) / FONT_HEIGHT (font));
18878 saved_pos = it->position;
18879 memset (&it->position, 0, sizeof it->position);
18880 saved_avoid_cursor = it->avoid_cursor_p;
18881 it->avoid_cursor_p = 1;
18882 saved_face_id = it->face_id;
18883 saved_box_start = it->start_of_box_run_p;
18884 /* The last row's stretch glyph should get the default
18885 face, to avoid painting the rest of the window with
18886 the region face, if the region ends at ZV. */
18887 if (it->glyph_row->ends_at_zv_p)
18888 it->face_id = default_face->id;
18889 else
18890 it->face_id = face->id;
18891 it->start_of_box_run_p = 0;
18892 append_stretch_glyph (it, make_number (0), stretch_width,
18893 it->ascent + it->descent, stretch_ascent);
18894 it->position = saved_pos;
18895 it->avoid_cursor_p = saved_avoid_cursor;
18896 it->face_id = saved_face_id;
18897 it->start_of_box_run_p = saved_box_start;
18898 }
18899 }
18900 #endif /* HAVE_WINDOW_SYSTEM */
18901 }
18902 else
18903 {
18904 /* Save some values that must not be changed. */
18905 int saved_x = it->current_x;
18906 struct text_pos saved_pos;
18907 Lisp_Object saved_object;
18908 enum display_element_type saved_what = it->what;
18909 int saved_face_id = it->face_id;
18910
18911 saved_object = it->object;
18912 saved_pos = it->position;
18913
18914 it->what = IT_CHARACTER;
18915 memset (&it->position, 0, sizeof it->position);
18916 it->object = make_number (0);
18917 it->c = it->char_to_display = ' ';
18918 it->len = 1;
18919 /* The last row's blank glyphs should get the default face, to
18920 avoid painting the rest of the window with the region face,
18921 if the region ends at ZV. */
18922 if (it->glyph_row->ends_at_zv_p)
18923 it->face_id = default_face->id;
18924 else
18925 it->face_id = face->id;
18926
18927 PRODUCE_GLYPHS (it);
18928
18929 while (it->current_x <= it->last_visible_x)
18930 PRODUCE_GLYPHS (it);
18931
18932 /* Don't count these blanks really. It would let us insert a left
18933 truncation glyph below and make us set the cursor on them, maybe. */
18934 it->current_x = saved_x;
18935 it->object = saved_object;
18936 it->position = saved_pos;
18937 it->what = saved_what;
18938 it->face_id = saved_face_id;
18939 }
18940 }
18941
18942
18943 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18944 trailing whitespace. */
18945
18946 static int
18947 trailing_whitespace_p (ptrdiff_t charpos)
18948 {
18949 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18950 int c = 0;
18951
18952 while (bytepos < ZV_BYTE
18953 && (c = FETCH_CHAR (bytepos),
18954 c == ' ' || c == '\t'))
18955 ++bytepos;
18956
18957 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18958 {
18959 if (bytepos != PT_BYTE)
18960 return 1;
18961 }
18962 return 0;
18963 }
18964
18965
18966 /* Highlight trailing whitespace, if any, in ROW. */
18967
18968 static void
18969 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18970 {
18971 int used = row->used[TEXT_AREA];
18972
18973 if (used)
18974 {
18975 struct glyph *start = row->glyphs[TEXT_AREA];
18976 struct glyph *glyph = start + used - 1;
18977
18978 if (row->reversed_p)
18979 {
18980 /* Right-to-left rows need to be processed in the opposite
18981 direction, so swap the edge pointers. */
18982 glyph = start;
18983 start = row->glyphs[TEXT_AREA] + used - 1;
18984 }
18985
18986 /* Skip over glyphs inserted to display the cursor at the
18987 end of a line, for extending the face of the last glyph
18988 to the end of the line on terminals, and for truncation
18989 and continuation glyphs. */
18990 if (!row->reversed_p)
18991 {
18992 while (glyph >= start
18993 && glyph->type == CHAR_GLYPH
18994 && INTEGERP (glyph->object))
18995 --glyph;
18996 }
18997 else
18998 {
18999 while (glyph <= start
19000 && glyph->type == CHAR_GLYPH
19001 && INTEGERP (glyph->object))
19002 ++glyph;
19003 }
19004
19005 /* If last glyph is a space or stretch, and it's trailing
19006 whitespace, set the face of all trailing whitespace glyphs in
19007 IT->glyph_row to `trailing-whitespace'. */
19008 if ((row->reversed_p ? glyph <= start : glyph >= start)
19009 && BUFFERP (glyph->object)
19010 && (glyph->type == STRETCH_GLYPH
19011 || (glyph->type == CHAR_GLYPH
19012 && glyph->u.ch == ' '))
19013 && trailing_whitespace_p (glyph->charpos))
19014 {
19015 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19016 if (face_id < 0)
19017 return;
19018
19019 if (!row->reversed_p)
19020 {
19021 while (glyph >= start
19022 && BUFFERP (glyph->object)
19023 && (glyph->type == STRETCH_GLYPH
19024 || (glyph->type == CHAR_GLYPH
19025 && glyph->u.ch == ' ')))
19026 (glyph--)->face_id = face_id;
19027 }
19028 else
19029 {
19030 while (glyph <= start
19031 && BUFFERP (glyph->object)
19032 && (glyph->type == STRETCH_GLYPH
19033 || (glyph->type == CHAR_GLYPH
19034 && glyph->u.ch == ' ')))
19035 (glyph++)->face_id = face_id;
19036 }
19037 }
19038 }
19039 }
19040
19041
19042 /* Value is non-zero if glyph row ROW should be
19043 considered to hold the buffer position CHARPOS. */
19044
19045 static int
19046 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19047 {
19048 int result = 1;
19049
19050 if (charpos == CHARPOS (row->end.pos)
19051 || charpos == MATRIX_ROW_END_CHARPOS (row))
19052 {
19053 /* Suppose the row ends on a string.
19054 Unless the row is continued, that means it ends on a newline
19055 in the string. If it's anything other than a display string
19056 (e.g., a before-string from an overlay), we don't want the
19057 cursor there. (This heuristic seems to give the optimal
19058 behavior for the various types of multi-line strings.)
19059 One exception: if the string has `cursor' property on one of
19060 its characters, we _do_ want the cursor there. */
19061 if (CHARPOS (row->end.string_pos) >= 0)
19062 {
19063 if (row->continued_p)
19064 result = 1;
19065 else
19066 {
19067 /* Check for `display' property. */
19068 struct glyph *beg = row->glyphs[TEXT_AREA];
19069 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19070 struct glyph *glyph;
19071
19072 result = 0;
19073 for (glyph = end; glyph >= beg; --glyph)
19074 if (STRINGP (glyph->object))
19075 {
19076 Lisp_Object prop
19077 = Fget_char_property (make_number (charpos),
19078 Qdisplay, Qnil);
19079 result =
19080 (!NILP (prop)
19081 && display_prop_string_p (prop, glyph->object));
19082 /* If there's a `cursor' property on one of the
19083 string's characters, this row is a cursor row,
19084 even though this is not a display string. */
19085 if (!result)
19086 {
19087 Lisp_Object s = glyph->object;
19088
19089 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19090 {
19091 ptrdiff_t gpos = glyph->charpos;
19092
19093 if (!NILP (Fget_char_property (make_number (gpos),
19094 Qcursor, s)))
19095 {
19096 result = 1;
19097 break;
19098 }
19099 }
19100 }
19101 break;
19102 }
19103 }
19104 }
19105 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19106 {
19107 /* If the row ends in middle of a real character,
19108 and the line is continued, we want the cursor here.
19109 That's because CHARPOS (ROW->end.pos) would equal
19110 PT if PT is before the character. */
19111 if (!row->ends_in_ellipsis_p)
19112 result = row->continued_p;
19113 else
19114 /* If the row ends in an ellipsis, then
19115 CHARPOS (ROW->end.pos) will equal point after the
19116 invisible text. We want that position to be displayed
19117 after the ellipsis. */
19118 result = 0;
19119 }
19120 /* If the row ends at ZV, display the cursor at the end of that
19121 row instead of at the start of the row below. */
19122 else if (row->ends_at_zv_p)
19123 result = 1;
19124 else
19125 result = 0;
19126 }
19127
19128 return result;
19129 }
19130
19131 /* Value is non-zero if glyph row ROW should be
19132 used to hold the cursor. */
19133
19134 static int
19135 cursor_row_p (struct glyph_row *row)
19136 {
19137 return row_for_charpos_p (row, PT);
19138 }
19139
19140 \f
19141
19142 /* Push the property PROP so that it will be rendered at the current
19143 position in IT. Return 1 if PROP was successfully pushed, 0
19144 otherwise. Called from handle_line_prefix to handle the
19145 `line-prefix' and `wrap-prefix' properties. */
19146
19147 static int
19148 push_prefix_prop (struct it *it, Lisp_Object prop)
19149 {
19150 struct text_pos pos =
19151 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19152
19153 eassert (it->method == GET_FROM_BUFFER
19154 || it->method == GET_FROM_DISPLAY_VECTOR
19155 || it->method == GET_FROM_STRING);
19156
19157 /* We need to save the current buffer/string position, so it will be
19158 restored by pop_it, because iterate_out_of_display_property
19159 depends on that being set correctly, but some situations leave
19160 it->position not yet set when this function is called. */
19161 push_it (it, &pos);
19162
19163 if (STRINGP (prop))
19164 {
19165 if (SCHARS (prop) == 0)
19166 {
19167 pop_it (it);
19168 return 0;
19169 }
19170
19171 it->string = prop;
19172 it->string_from_prefix_prop_p = 1;
19173 it->multibyte_p = STRING_MULTIBYTE (it->string);
19174 it->current.overlay_string_index = -1;
19175 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19176 it->end_charpos = it->string_nchars = SCHARS (it->string);
19177 it->method = GET_FROM_STRING;
19178 it->stop_charpos = 0;
19179 it->prev_stop = 0;
19180 it->base_level_stop = 0;
19181
19182 /* Force paragraph direction to be that of the parent
19183 buffer/string. */
19184 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19185 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19186 else
19187 it->paragraph_embedding = L2R;
19188
19189 /* Set up the bidi iterator for this display string. */
19190 if (it->bidi_p)
19191 {
19192 it->bidi_it.string.lstring = it->string;
19193 it->bidi_it.string.s = NULL;
19194 it->bidi_it.string.schars = it->end_charpos;
19195 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19196 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19197 it->bidi_it.string.unibyte = !it->multibyte_p;
19198 it->bidi_it.w = it->w;
19199 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19200 }
19201 }
19202 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19203 {
19204 it->method = GET_FROM_STRETCH;
19205 it->object = prop;
19206 }
19207 #ifdef HAVE_WINDOW_SYSTEM
19208 else if (IMAGEP (prop))
19209 {
19210 it->what = IT_IMAGE;
19211 it->image_id = lookup_image (it->f, prop);
19212 it->method = GET_FROM_IMAGE;
19213 }
19214 #endif /* HAVE_WINDOW_SYSTEM */
19215 else
19216 {
19217 pop_it (it); /* bogus display property, give up */
19218 return 0;
19219 }
19220
19221 return 1;
19222 }
19223
19224 /* Return the character-property PROP at the current position in IT. */
19225
19226 static Lisp_Object
19227 get_it_property (struct it *it, Lisp_Object prop)
19228 {
19229 Lisp_Object position, object = it->object;
19230
19231 if (STRINGP (object))
19232 position = make_number (IT_STRING_CHARPOS (*it));
19233 else if (BUFFERP (object))
19234 {
19235 position = make_number (IT_CHARPOS (*it));
19236 object = it->window;
19237 }
19238 else
19239 return Qnil;
19240
19241 return Fget_char_property (position, prop, object);
19242 }
19243
19244 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19245
19246 static void
19247 handle_line_prefix (struct it *it)
19248 {
19249 Lisp_Object prefix;
19250
19251 if (it->continuation_lines_width > 0)
19252 {
19253 prefix = get_it_property (it, Qwrap_prefix);
19254 if (NILP (prefix))
19255 prefix = Vwrap_prefix;
19256 }
19257 else
19258 {
19259 prefix = get_it_property (it, Qline_prefix);
19260 if (NILP (prefix))
19261 prefix = Vline_prefix;
19262 }
19263 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19264 {
19265 /* If the prefix is wider than the window, and we try to wrap
19266 it, it would acquire its own wrap prefix, and so on till the
19267 iterator stack overflows. So, don't wrap the prefix. */
19268 it->line_wrap = TRUNCATE;
19269 it->avoid_cursor_p = 1;
19270 }
19271 }
19272
19273 \f
19274
19275 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19276 only for R2L lines from display_line and display_string, when they
19277 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19278 the line/string needs to be continued on the next glyph row. */
19279 static void
19280 unproduce_glyphs (struct it *it, int n)
19281 {
19282 struct glyph *glyph, *end;
19283
19284 eassert (it->glyph_row);
19285 eassert (it->glyph_row->reversed_p);
19286 eassert (it->area == TEXT_AREA);
19287 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19288
19289 if (n > it->glyph_row->used[TEXT_AREA])
19290 n = it->glyph_row->used[TEXT_AREA];
19291 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19292 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19293 for ( ; glyph < end; glyph++)
19294 glyph[-n] = *glyph;
19295 }
19296
19297 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19298 and ROW->maxpos. */
19299 static void
19300 find_row_edges (struct it *it, struct glyph_row *row,
19301 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19302 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19303 {
19304 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19305 lines' rows is implemented for bidi-reordered rows. */
19306
19307 /* ROW->minpos is the value of min_pos, the minimal buffer position
19308 we have in ROW, or ROW->start.pos if that is smaller. */
19309 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19310 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19311 else
19312 /* We didn't find buffer positions smaller than ROW->start, or
19313 didn't find _any_ valid buffer positions in any of the glyphs,
19314 so we must trust the iterator's computed positions. */
19315 row->minpos = row->start.pos;
19316 if (max_pos <= 0)
19317 {
19318 max_pos = CHARPOS (it->current.pos);
19319 max_bpos = BYTEPOS (it->current.pos);
19320 }
19321
19322 /* Here are the various use-cases for ending the row, and the
19323 corresponding values for ROW->maxpos:
19324
19325 Line ends in a newline from buffer eol_pos + 1
19326 Line is continued from buffer max_pos + 1
19327 Line is truncated on right it->current.pos
19328 Line ends in a newline from string max_pos + 1(*)
19329 (*) + 1 only when line ends in a forward scan
19330 Line is continued from string max_pos
19331 Line is continued from display vector max_pos
19332 Line is entirely from a string min_pos == max_pos
19333 Line is entirely from a display vector min_pos == max_pos
19334 Line that ends at ZV ZV
19335
19336 If you discover other use-cases, please add them here as
19337 appropriate. */
19338 if (row->ends_at_zv_p)
19339 row->maxpos = it->current.pos;
19340 else if (row->used[TEXT_AREA])
19341 {
19342 int seen_this_string = 0;
19343 struct glyph_row *r1 = row - 1;
19344
19345 /* Did we see the same display string on the previous row? */
19346 if (STRINGP (it->object)
19347 /* this is not the first row */
19348 && row > it->w->desired_matrix->rows
19349 /* previous row is not the header line */
19350 && !r1->mode_line_p
19351 /* previous row also ends in a newline from a string */
19352 && r1->ends_in_newline_from_string_p)
19353 {
19354 struct glyph *start, *end;
19355
19356 /* Search for the last glyph of the previous row that came
19357 from buffer or string. Depending on whether the row is
19358 L2R or R2L, we need to process it front to back or the
19359 other way round. */
19360 if (!r1->reversed_p)
19361 {
19362 start = r1->glyphs[TEXT_AREA];
19363 end = start + r1->used[TEXT_AREA];
19364 /* Glyphs inserted by redisplay have an integer (zero)
19365 as their object. */
19366 while (end > start
19367 && INTEGERP ((end - 1)->object)
19368 && (end - 1)->charpos <= 0)
19369 --end;
19370 if (end > start)
19371 {
19372 if (EQ ((end - 1)->object, it->object))
19373 seen_this_string = 1;
19374 }
19375 else
19376 /* If all the glyphs of the previous row were inserted
19377 by redisplay, it means the previous row was
19378 produced from a single newline, which is only
19379 possible if that newline came from the same string
19380 as the one which produced this ROW. */
19381 seen_this_string = 1;
19382 }
19383 else
19384 {
19385 end = r1->glyphs[TEXT_AREA] - 1;
19386 start = end + r1->used[TEXT_AREA];
19387 while (end < start
19388 && INTEGERP ((end + 1)->object)
19389 && (end + 1)->charpos <= 0)
19390 ++end;
19391 if (end < start)
19392 {
19393 if (EQ ((end + 1)->object, it->object))
19394 seen_this_string = 1;
19395 }
19396 else
19397 seen_this_string = 1;
19398 }
19399 }
19400 /* Take note of each display string that covers a newline only
19401 once, the first time we see it. This is for when a display
19402 string includes more than one newline in it. */
19403 if (row->ends_in_newline_from_string_p && !seen_this_string)
19404 {
19405 /* If we were scanning the buffer forward when we displayed
19406 the string, we want to account for at least one buffer
19407 position that belongs to this row (position covered by
19408 the display string), so that cursor positioning will
19409 consider this row as a candidate when point is at the end
19410 of the visual line represented by this row. This is not
19411 required when scanning back, because max_pos will already
19412 have a much larger value. */
19413 if (CHARPOS (row->end.pos) > max_pos)
19414 INC_BOTH (max_pos, max_bpos);
19415 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19416 }
19417 else if (CHARPOS (it->eol_pos) > 0)
19418 SET_TEXT_POS (row->maxpos,
19419 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19420 else if (row->continued_p)
19421 {
19422 /* If max_pos is different from IT's current position, it
19423 means IT->method does not belong to the display element
19424 at max_pos. However, it also means that the display
19425 element at max_pos was displayed in its entirety on this
19426 line, which is equivalent to saying that the next line
19427 starts at the next buffer position. */
19428 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19429 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19430 else
19431 {
19432 INC_BOTH (max_pos, max_bpos);
19433 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19434 }
19435 }
19436 else if (row->truncated_on_right_p)
19437 /* display_line already called reseat_at_next_visible_line_start,
19438 which puts the iterator at the beginning of the next line, in
19439 the logical order. */
19440 row->maxpos = it->current.pos;
19441 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19442 /* A line that is entirely from a string/image/stretch... */
19443 row->maxpos = row->minpos;
19444 else
19445 emacs_abort ();
19446 }
19447 else
19448 row->maxpos = it->current.pos;
19449 }
19450
19451 /* Construct the glyph row IT->glyph_row in the desired matrix of
19452 IT->w from text at the current position of IT. See dispextern.h
19453 for an overview of struct it. Value is non-zero if
19454 IT->glyph_row displays text, as opposed to a line displaying ZV
19455 only. */
19456
19457 static int
19458 display_line (struct it *it)
19459 {
19460 struct glyph_row *row = it->glyph_row;
19461 Lisp_Object overlay_arrow_string;
19462 struct it wrap_it;
19463 void *wrap_data = NULL;
19464 int may_wrap = 0, wrap_x IF_LINT (= 0);
19465 int wrap_row_used = -1;
19466 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19467 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19468 int wrap_row_extra_line_spacing IF_LINT (= 0);
19469 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19470 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19471 int cvpos;
19472 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19473 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19474
19475 /* We always start displaying at hpos zero even if hscrolled. */
19476 eassert (it->hpos == 0 && it->current_x == 0);
19477
19478 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19479 >= it->w->desired_matrix->nrows)
19480 {
19481 it->w->nrows_scale_factor++;
19482 it->f->fonts_changed = 1;
19483 return 0;
19484 }
19485
19486 /* Clear the result glyph row and enable it. */
19487 prepare_desired_row (row);
19488
19489 row->y = it->current_y;
19490 row->start = it->start;
19491 row->continuation_lines_width = it->continuation_lines_width;
19492 row->displays_text_p = 1;
19493 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19494 it->starts_in_middle_of_char_p = 0;
19495
19496 /* Arrange the overlays nicely for our purposes. Usually, we call
19497 display_line on only one line at a time, in which case this
19498 can't really hurt too much, or we call it on lines which appear
19499 one after another in the buffer, in which case all calls to
19500 recenter_overlay_lists but the first will be pretty cheap. */
19501 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19502
19503 /* Move over display elements that are not visible because we are
19504 hscrolled. This may stop at an x-position < IT->first_visible_x
19505 if the first glyph is partially visible or if we hit a line end. */
19506 if (it->current_x < it->first_visible_x)
19507 {
19508 enum move_it_result move_result;
19509
19510 this_line_min_pos = row->start.pos;
19511 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19512 MOVE_TO_POS | MOVE_TO_X);
19513 /* If we are under a large hscroll, move_it_in_display_line_to
19514 could hit the end of the line without reaching
19515 it->first_visible_x. Pretend that we did reach it. This is
19516 especially important on a TTY, where we will call
19517 extend_face_to_end_of_line, which needs to know how many
19518 blank glyphs to produce. */
19519 if (it->current_x < it->first_visible_x
19520 && (move_result == MOVE_NEWLINE_OR_CR
19521 || move_result == MOVE_POS_MATCH_OR_ZV))
19522 it->current_x = it->first_visible_x;
19523
19524 /* Record the smallest positions seen while we moved over
19525 display elements that are not visible. This is needed by
19526 redisplay_internal for optimizing the case where the cursor
19527 stays inside the same line. The rest of this function only
19528 considers positions that are actually displayed, so
19529 RECORD_MAX_MIN_POS will not otherwise record positions that
19530 are hscrolled to the left of the left edge of the window. */
19531 min_pos = CHARPOS (this_line_min_pos);
19532 min_bpos = BYTEPOS (this_line_min_pos);
19533 }
19534 else
19535 {
19536 /* We only do this when not calling `move_it_in_display_line_to'
19537 above, because move_it_in_display_line_to calls
19538 handle_line_prefix itself. */
19539 handle_line_prefix (it);
19540 }
19541
19542 /* Get the initial row height. This is either the height of the
19543 text hscrolled, if there is any, or zero. */
19544 row->ascent = it->max_ascent;
19545 row->height = it->max_ascent + it->max_descent;
19546 row->phys_ascent = it->max_phys_ascent;
19547 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19548 row->extra_line_spacing = it->max_extra_line_spacing;
19549
19550 /* Utility macro to record max and min buffer positions seen until now. */
19551 #define RECORD_MAX_MIN_POS(IT) \
19552 do \
19553 { \
19554 int composition_p = !STRINGP ((IT)->string) \
19555 && ((IT)->what == IT_COMPOSITION); \
19556 ptrdiff_t current_pos = \
19557 composition_p ? (IT)->cmp_it.charpos \
19558 : IT_CHARPOS (*(IT)); \
19559 ptrdiff_t current_bpos = \
19560 composition_p ? CHAR_TO_BYTE (current_pos) \
19561 : IT_BYTEPOS (*(IT)); \
19562 if (current_pos < min_pos) \
19563 { \
19564 min_pos = current_pos; \
19565 min_bpos = current_bpos; \
19566 } \
19567 if (IT_CHARPOS (*it) > max_pos) \
19568 { \
19569 max_pos = IT_CHARPOS (*it); \
19570 max_bpos = IT_BYTEPOS (*it); \
19571 } \
19572 } \
19573 while (0)
19574
19575 /* Loop generating characters. The loop is left with IT on the next
19576 character to display. */
19577 while (1)
19578 {
19579 int n_glyphs_before, hpos_before, x_before;
19580 int x, nglyphs;
19581 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19582
19583 /* Retrieve the next thing to display. Value is zero if end of
19584 buffer reached. */
19585 if (!get_next_display_element (it))
19586 {
19587 /* Maybe add a space at the end of this line that is used to
19588 display the cursor there under X. Set the charpos of the
19589 first glyph of blank lines not corresponding to any text
19590 to -1. */
19591 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19592 row->exact_window_width_line_p = 1;
19593 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19594 || row->used[TEXT_AREA] == 0)
19595 {
19596 row->glyphs[TEXT_AREA]->charpos = -1;
19597 row->displays_text_p = 0;
19598
19599 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19600 && (!MINI_WINDOW_P (it->w)
19601 || (minibuf_level && EQ (it->window, minibuf_window))))
19602 row->indicate_empty_line_p = 1;
19603 }
19604
19605 it->continuation_lines_width = 0;
19606 row->ends_at_zv_p = 1;
19607 /* A row that displays right-to-left text must always have
19608 its last face extended all the way to the end of line,
19609 even if this row ends in ZV, because we still write to
19610 the screen left to right. We also need to extend the
19611 last face if the default face is remapped to some
19612 different face, otherwise the functions that clear
19613 portions of the screen will clear with the default face's
19614 background color. */
19615 if (row->reversed_p
19616 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19617 extend_face_to_end_of_line (it);
19618 break;
19619 }
19620
19621 /* Now, get the metrics of what we want to display. This also
19622 generates glyphs in `row' (which is IT->glyph_row). */
19623 n_glyphs_before = row->used[TEXT_AREA];
19624 x = it->current_x;
19625
19626 /* Remember the line height so far in case the next element doesn't
19627 fit on the line. */
19628 if (it->line_wrap != TRUNCATE)
19629 {
19630 ascent = it->max_ascent;
19631 descent = it->max_descent;
19632 phys_ascent = it->max_phys_ascent;
19633 phys_descent = it->max_phys_descent;
19634
19635 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19636 {
19637 if (IT_DISPLAYING_WHITESPACE (it))
19638 may_wrap = 1;
19639 else if (may_wrap)
19640 {
19641 SAVE_IT (wrap_it, *it, wrap_data);
19642 wrap_x = x;
19643 wrap_row_used = row->used[TEXT_AREA];
19644 wrap_row_ascent = row->ascent;
19645 wrap_row_height = row->height;
19646 wrap_row_phys_ascent = row->phys_ascent;
19647 wrap_row_phys_height = row->phys_height;
19648 wrap_row_extra_line_spacing = row->extra_line_spacing;
19649 wrap_row_min_pos = min_pos;
19650 wrap_row_min_bpos = min_bpos;
19651 wrap_row_max_pos = max_pos;
19652 wrap_row_max_bpos = max_bpos;
19653 may_wrap = 0;
19654 }
19655 }
19656 }
19657
19658 PRODUCE_GLYPHS (it);
19659
19660 /* If this display element was in marginal areas, continue with
19661 the next one. */
19662 if (it->area != TEXT_AREA)
19663 {
19664 row->ascent = max (row->ascent, it->max_ascent);
19665 row->height = max (row->height, it->max_ascent + it->max_descent);
19666 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19667 row->phys_height = max (row->phys_height,
19668 it->max_phys_ascent + it->max_phys_descent);
19669 row->extra_line_spacing = max (row->extra_line_spacing,
19670 it->max_extra_line_spacing);
19671 set_iterator_to_next (it, 1);
19672 continue;
19673 }
19674
19675 /* Does the display element fit on the line? If we truncate
19676 lines, we should draw past the right edge of the window. If
19677 we don't truncate, we want to stop so that we can display the
19678 continuation glyph before the right margin. If lines are
19679 continued, there are two possible strategies for characters
19680 resulting in more than 1 glyph (e.g. tabs): Display as many
19681 glyphs as possible in this line and leave the rest for the
19682 continuation line, or display the whole element in the next
19683 line. Original redisplay did the former, so we do it also. */
19684 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19685 hpos_before = it->hpos;
19686 x_before = x;
19687
19688 if (/* Not a newline. */
19689 nglyphs > 0
19690 /* Glyphs produced fit entirely in the line. */
19691 && it->current_x < it->last_visible_x)
19692 {
19693 it->hpos += nglyphs;
19694 row->ascent = max (row->ascent, it->max_ascent);
19695 row->height = max (row->height, it->max_ascent + it->max_descent);
19696 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19697 row->phys_height = max (row->phys_height,
19698 it->max_phys_ascent + it->max_phys_descent);
19699 row->extra_line_spacing = max (row->extra_line_spacing,
19700 it->max_extra_line_spacing);
19701 if (it->current_x - it->pixel_width < it->first_visible_x)
19702 row->x = x - it->first_visible_x;
19703 /* Record the maximum and minimum buffer positions seen so
19704 far in glyphs that will be displayed by this row. */
19705 if (it->bidi_p)
19706 RECORD_MAX_MIN_POS (it);
19707 }
19708 else
19709 {
19710 int i, new_x;
19711 struct glyph *glyph;
19712
19713 for (i = 0; i < nglyphs; ++i, x = new_x)
19714 {
19715 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19716 new_x = x + glyph->pixel_width;
19717
19718 if (/* Lines are continued. */
19719 it->line_wrap != TRUNCATE
19720 && (/* Glyph doesn't fit on the line. */
19721 new_x > it->last_visible_x
19722 /* Or it fits exactly on a window system frame. */
19723 || (new_x == it->last_visible_x
19724 && FRAME_WINDOW_P (it->f)
19725 && (row->reversed_p
19726 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19727 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19728 {
19729 /* End of a continued line. */
19730
19731 if (it->hpos == 0
19732 || (new_x == it->last_visible_x
19733 && FRAME_WINDOW_P (it->f)
19734 && (row->reversed_p
19735 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19736 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19737 {
19738 /* Current glyph is the only one on the line or
19739 fits exactly on the line. We must continue
19740 the line because we can't draw the cursor
19741 after the glyph. */
19742 row->continued_p = 1;
19743 it->current_x = new_x;
19744 it->continuation_lines_width += new_x;
19745 ++it->hpos;
19746 if (i == nglyphs - 1)
19747 {
19748 /* If line-wrap is on, check if a previous
19749 wrap point was found. */
19750 if (wrap_row_used > 0
19751 /* Even if there is a previous wrap
19752 point, continue the line here as
19753 usual, if (i) the previous character
19754 was a space or tab AND (ii) the
19755 current character is not. */
19756 && (!may_wrap
19757 || IT_DISPLAYING_WHITESPACE (it)))
19758 goto back_to_wrap;
19759
19760 /* Record the maximum and minimum buffer
19761 positions seen so far in glyphs that will be
19762 displayed by this row. */
19763 if (it->bidi_p)
19764 RECORD_MAX_MIN_POS (it);
19765 set_iterator_to_next (it, 1);
19766 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19767 {
19768 if (!get_next_display_element (it))
19769 {
19770 row->exact_window_width_line_p = 1;
19771 it->continuation_lines_width = 0;
19772 row->continued_p = 0;
19773 row->ends_at_zv_p = 1;
19774 }
19775 else if (ITERATOR_AT_END_OF_LINE_P (it))
19776 {
19777 row->continued_p = 0;
19778 row->exact_window_width_line_p = 1;
19779 }
19780 }
19781 }
19782 else if (it->bidi_p)
19783 RECORD_MAX_MIN_POS (it);
19784 }
19785 else if (CHAR_GLYPH_PADDING_P (*glyph)
19786 && !FRAME_WINDOW_P (it->f))
19787 {
19788 /* A padding glyph that doesn't fit on this line.
19789 This means the whole character doesn't fit
19790 on the line. */
19791 if (row->reversed_p)
19792 unproduce_glyphs (it, row->used[TEXT_AREA]
19793 - n_glyphs_before);
19794 row->used[TEXT_AREA] = n_glyphs_before;
19795
19796 /* Fill the rest of the row with continuation
19797 glyphs like in 20.x. */
19798 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19799 < row->glyphs[1 + TEXT_AREA])
19800 produce_special_glyphs (it, IT_CONTINUATION);
19801
19802 row->continued_p = 1;
19803 it->current_x = x_before;
19804 it->continuation_lines_width += x_before;
19805
19806 /* Restore the height to what it was before the
19807 element not fitting on the line. */
19808 it->max_ascent = ascent;
19809 it->max_descent = descent;
19810 it->max_phys_ascent = phys_ascent;
19811 it->max_phys_descent = phys_descent;
19812 }
19813 else if (wrap_row_used > 0)
19814 {
19815 back_to_wrap:
19816 if (row->reversed_p)
19817 unproduce_glyphs (it,
19818 row->used[TEXT_AREA] - wrap_row_used);
19819 RESTORE_IT (it, &wrap_it, wrap_data);
19820 it->continuation_lines_width += wrap_x;
19821 row->used[TEXT_AREA] = wrap_row_used;
19822 row->ascent = wrap_row_ascent;
19823 row->height = wrap_row_height;
19824 row->phys_ascent = wrap_row_phys_ascent;
19825 row->phys_height = wrap_row_phys_height;
19826 row->extra_line_spacing = wrap_row_extra_line_spacing;
19827 min_pos = wrap_row_min_pos;
19828 min_bpos = wrap_row_min_bpos;
19829 max_pos = wrap_row_max_pos;
19830 max_bpos = wrap_row_max_bpos;
19831 row->continued_p = 1;
19832 row->ends_at_zv_p = 0;
19833 row->exact_window_width_line_p = 0;
19834 it->continuation_lines_width += x;
19835
19836 /* Make sure that a non-default face is extended
19837 up to the right margin of the window. */
19838 extend_face_to_end_of_line (it);
19839 }
19840 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19841 {
19842 /* A TAB that extends past the right edge of the
19843 window. This produces a single glyph on
19844 window system frames. We leave the glyph in
19845 this row and let it fill the row, but don't
19846 consume the TAB. */
19847 if ((row->reversed_p
19848 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19849 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19850 produce_special_glyphs (it, IT_CONTINUATION);
19851 it->continuation_lines_width += it->last_visible_x;
19852 row->ends_in_middle_of_char_p = 1;
19853 row->continued_p = 1;
19854 glyph->pixel_width = it->last_visible_x - x;
19855 it->starts_in_middle_of_char_p = 1;
19856 }
19857 else
19858 {
19859 /* Something other than a TAB that draws past
19860 the right edge of the window. Restore
19861 positions to values before the element. */
19862 if (row->reversed_p)
19863 unproduce_glyphs (it, row->used[TEXT_AREA]
19864 - (n_glyphs_before + i));
19865 row->used[TEXT_AREA] = n_glyphs_before + i;
19866
19867 /* Display continuation glyphs. */
19868 it->current_x = x_before;
19869 it->continuation_lines_width += x;
19870 if (!FRAME_WINDOW_P (it->f)
19871 || (row->reversed_p
19872 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19873 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19874 produce_special_glyphs (it, IT_CONTINUATION);
19875 row->continued_p = 1;
19876
19877 extend_face_to_end_of_line (it);
19878
19879 if (nglyphs > 1 && i > 0)
19880 {
19881 row->ends_in_middle_of_char_p = 1;
19882 it->starts_in_middle_of_char_p = 1;
19883 }
19884
19885 /* Restore the height to what it was before the
19886 element not fitting on the line. */
19887 it->max_ascent = ascent;
19888 it->max_descent = descent;
19889 it->max_phys_ascent = phys_ascent;
19890 it->max_phys_descent = phys_descent;
19891 }
19892
19893 break;
19894 }
19895 else if (new_x > it->first_visible_x)
19896 {
19897 /* Increment number of glyphs actually displayed. */
19898 ++it->hpos;
19899
19900 /* Record the maximum and minimum buffer positions
19901 seen so far in glyphs that will be displayed by
19902 this row. */
19903 if (it->bidi_p)
19904 RECORD_MAX_MIN_POS (it);
19905
19906 if (x < it->first_visible_x)
19907 /* Glyph is partially visible, i.e. row starts at
19908 negative X position. */
19909 row->x = x - it->first_visible_x;
19910 }
19911 else
19912 {
19913 /* Glyph is completely off the left margin of the
19914 window. This should not happen because of the
19915 move_it_in_display_line at the start of this
19916 function, unless the text display area of the
19917 window is empty. */
19918 eassert (it->first_visible_x <= it->last_visible_x);
19919 }
19920 }
19921 /* Even if this display element produced no glyphs at all,
19922 we want to record its position. */
19923 if (it->bidi_p && nglyphs == 0)
19924 RECORD_MAX_MIN_POS (it);
19925
19926 row->ascent = max (row->ascent, it->max_ascent);
19927 row->height = max (row->height, it->max_ascent + it->max_descent);
19928 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19929 row->phys_height = max (row->phys_height,
19930 it->max_phys_ascent + it->max_phys_descent);
19931 row->extra_line_spacing = max (row->extra_line_spacing,
19932 it->max_extra_line_spacing);
19933
19934 /* End of this display line if row is continued. */
19935 if (row->continued_p || row->ends_at_zv_p)
19936 break;
19937 }
19938
19939 at_end_of_line:
19940 /* Is this a line end? If yes, we're also done, after making
19941 sure that a non-default face is extended up to the right
19942 margin of the window. */
19943 if (ITERATOR_AT_END_OF_LINE_P (it))
19944 {
19945 int used_before = row->used[TEXT_AREA];
19946
19947 row->ends_in_newline_from_string_p = STRINGP (it->object);
19948
19949 /* Add a space at the end of the line that is used to
19950 display the cursor there. */
19951 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19952 append_space_for_newline (it, 0);
19953
19954 /* Extend the face to the end of the line. */
19955 extend_face_to_end_of_line (it);
19956
19957 /* Make sure we have the position. */
19958 if (used_before == 0)
19959 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19960
19961 /* Record the position of the newline, for use in
19962 find_row_edges. */
19963 it->eol_pos = it->current.pos;
19964
19965 /* Consume the line end. This skips over invisible lines. */
19966 set_iterator_to_next (it, 1);
19967 it->continuation_lines_width = 0;
19968 break;
19969 }
19970
19971 /* Proceed with next display element. Note that this skips
19972 over lines invisible because of selective display. */
19973 set_iterator_to_next (it, 1);
19974
19975 /* If we truncate lines, we are done when the last displayed
19976 glyphs reach past the right margin of the window. */
19977 if (it->line_wrap == TRUNCATE
19978 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19979 ? (it->current_x >= it->last_visible_x)
19980 : (it->current_x > it->last_visible_x)))
19981 {
19982 /* Maybe add truncation glyphs. */
19983 if (!FRAME_WINDOW_P (it->f)
19984 || (row->reversed_p
19985 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19986 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19987 {
19988 int i, n;
19989
19990 if (!row->reversed_p)
19991 {
19992 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19993 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19994 break;
19995 }
19996 else
19997 {
19998 for (i = 0; i < row->used[TEXT_AREA]; i++)
19999 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20000 break;
20001 /* Remove any padding glyphs at the front of ROW, to
20002 make room for the truncation glyphs we will be
20003 adding below. The loop below always inserts at
20004 least one truncation glyph, so also remove the
20005 last glyph added to ROW. */
20006 unproduce_glyphs (it, i + 1);
20007 /* Adjust i for the loop below. */
20008 i = row->used[TEXT_AREA] - (i + 1);
20009 }
20010
20011 it->current_x = x_before;
20012 if (!FRAME_WINDOW_P (it->f))
20013 {
20014 for (n = row->used[TEXT_AREA]; i < n; ++i)
20015 {
20016 row->used[TEXT_AREA] = i;
20017 produce_special_glyphs (it, IT_TRUNCATION);
20018 }
20019 }
20020 else
20021 {
20022 row->used[TEXT_AREA] = i;
20023 produce_special_glyphs (it, IT_TRUNCATION);
20024 }
20025 }
20026 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20027 {
20028 /* Don't truncate if we can overflow newline into fringe. */
20029 if (!get_next_display_element (it))
20030 {
20031 it->continuation_lines_width = 0;
20032 row->ends_at_zv_p = 1;
20033 row->exact_window_width_line_p = 1;
20034 break;
20035 }
20036 if (ITERATOR_AT_END_OF_LINE_P (it))
20037 {
20038 row->exact_window_width_line_p = 1;
20039 goto at_end_of_line;
20040 }
20041 it->current_x = x_before;
20042 }
20043
20044 row->truncated_on_right_p = 1;
20045 it->continuation_lines_width = 0;
20046 reseat_at_next_visible_line_start (it, 0);
20047 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20048 it->hpos = hpos_before;
20049 break;
20050 }
20051 }
20052
20053 if (wrap_data)
20054 bidi_unshelve_cache (wrap_data, 1);
20055
20056 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20057 at the left window margin. */
20058 if (it->first_visible_x
20059 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20060 {
20061 if (!FRAME_WINDOW_P (it->f)
20062 || (row->reversed_p
20063 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20064 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20065 insert_left_trunc_glyphs (it);
20066 row->truncated_on_left_p = 1;
20067 }
20068
20069 /* Remember the position at which this line ends.
20070
20071 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20072 cannot be before the call to find_row_edges below, since that is
20073 where these positions are determined. */
20074 row->end = it->current;
20075 if (!it->bidi_p)
20076 {
20077 row->minpos = row->start.pos;
20078 row->maxpos = row->end.pos;
20079 }
20080 else
20081 {
20082 /* ROW->minpos and ROW->maxpos must be the smallest and
20083 `1 + the largest' buffer positions in ROW. But if ROW was
20084 bidi-reordered, these two positions can be anywhere in the
20085 row, so we must determine them now. */
20086 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20087 }
20088
20089 /* If the start of this line is the overlay arrow-position, then
20090 mark this glyph row as the one containing the overlay arrow.
20091 This is clearly a mess with variable size fonts. It would be
20092 better to let it be displayed like cursors under X. */
20093 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20094 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20095 !NILP (overlay_arrow_string)))
20096 {
20097 /* Overlay arrow in window redisplay is a fringe bitmap. */
20098 if (STRINGP (overlay_arrow_string))
20099 {
20100 struct glyph_row *arrow_row
20101 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20102 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20103 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20104 struct glyph *p = row->glyphs[TEXT_AREA];
20105 struct glyph *p2, *end;
20106
20107 /* Copy the arrow glyphs. */
20108 while (glyph < arrow_end)
20109 *p++ = *glyph++;
20110
20111 /* Throw away padding glyphs. */
20112 p2 = p;
20113 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20114 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20115 ++p2;
20116 if (p2 > p)
20117 {
20118 while (p2 < end)
20119 *p++ = *p2++;
20120 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20121 }
20122 }
20123 else
20124 {
20125 eassert (INTEGERP (overlay_arrow_string));
20126 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20127 }
20128 overlay_arrow_seen = 1;
20129 }
20130
20131 /* Highlight trailing whitespace. */
20132 if (!NILP (Vshow_trailing_whitespace))
20133 highlight_trailing_whitespace (it->f, it->glyph_row);
20134
20135 /* Compute pixel dimensions of this line. */
20136 compute_line_metrics (it);
20137
20138 /* Implementation note: No changes in the glyphs of ROW or in their
20139 faces can be done past this point, because compute_line_metrics
20140 computes ROW's hash value and stores it within the glyph_row
20141 structure. */
20142
20143 /* Record whether this row ends inside an ellipsis. */
20144 row->ends_in_ellipsis_p
20145 = (it->method == GET_FROM_DISPLAY_VECTOR
20146 && it->ellipsis_p);
20147
20148 /* Save fringe bitmaps in this row. */
20149 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20150 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20151 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20152 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20153
20154 it->left_user_fringe_bitmap = 0;
20155 it->left_user_fringe_face_id = 0;
20156 it->right_user_fringe_bitmap = 0;
20157 it->right_user_fringe_face_id = 0;
20158
20159 /* Maybe set the cursor. */
20160 cvpos = it->w->cursor.vpos;
20161 if ((cvpos < 0
20162 /* In bidi-reordered rows, keep checking for proper cursor
20163 position even if one has been found already, because buffer
20164 positions in such rows change non-linearly with ROW->VPOS,
20165 when a line is continued. One exception: when we are at ZV,
20166 display cursor on the first suitable glyph row, since all
20167 the empty rows after that also have their position set to ZV. */
20168 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20169 lines' rows is implemented for bidi-reordered rows. */
20170 || (it->bidi_p
20171 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20172 && PT >= MATRIX_ROW_START_CHARPOS (row)
20173 && PT <= MATRIX_ROW_END_CHARPOS (row)
20174 && cursor_row_p (row))
20175 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20176
20177 /* Prepare for the next line. This line starts horizontally at (X
20178 HPOS) = (0 0). Vertical positions are incremented. As a
20179 convenience for the caller, IT->glyph_row is set to the next
20180 row to be used. */
20181 it->current_x = it->hpos = 0;
20182 it->current_y += row->height;
20183 SET_TEXT_POS (it->eol_pos, 0, 0);
20184 ++it->vpos;
20185 ++it->glyph_row;
20186 /* The next row should by default use the same value of the
20187 reversed_p flag as this one. set_iterator_to_next decides when
20188 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20189 the flag accordingly. */
20190 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20191 it->glyph_row->reversed_p = row->reversed_p;
20192 it->start = row->end;
20193 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20194
20195 #undef RECORD_MAX_MIN_POS
20196 }
20197
20198 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20199 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20200 doc: /* Return paragraph direction at point in BUFFER.
20201 Value is either `left-to-right' or `right-to-left'.
20202 If BUFFER is omitted or nil, it defaults to the current buffer.
20203
20204 Paragraph direction determines how the text in the paragraph is displayed.
20205 In left-to-right paragraphs, text begins at the left margin of the window
20206 and the reading direction is generally left to right. In right-to-left
20207 paragraphs, text begins at the right margin and is read from right to left.
20208
20209 See also `bidi-paragraph-direction'. */)
20210 (Lisp_Object buffer)
20211 {
20212 struct buffer *buf = current_buffer;
20213 struct buffer *old = buf;
20214
20215 if (! NILP (buffer))
20216 {
20217 CHECK_BUFFER (buffer);
20218 buf = XBUFFER (buffer);
20219 }
20220
20221 if (NILP (BVAR (buf, bidi_display_reordering))
20222 || NILP (BVAR (buf, enable_multibyte_characters))
20223 /* When we are loading loadup.el, the character property tables
20224 needed for bidi iteration are not yet available. */
20225 || !NILP (Vpurify_flag))
20226 return Qleft_to_right;
20227 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20228 return BVAR (buf, bidi_paragraph_direction);
20229 else
20230 {
20231 /* Determine the direction from buffer text. We could try to
20232 use current_matrix if it is up to date, but this seems fast
20233 enough as it is. */
20234 struct bidi_it itb;
20235 ptrdiff_t pos = BUF_PT (buf);
20236 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20237 int c;
20238 void *itb_data = bidi_shelve_cache ();
20239
20240 set_buffer_temp (buf);
20241 /* bidi_paragraph_init finds the base direction of the paragraph
20242 by searching forward from paragraph start. We need the base
20243 direction of the current or _previous_ paragraph, so we need
20244 to make sure we are within that paragraph. To that end, find
20245 the previous non-empty line. */
20246 if (pos >= ZV && pos > BEGV)
20247 DEC_BOTH (pos, bytepos);
20248 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20249 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20250 {
20251 while ((c = FETCH_BYTE (bytepos)) == '\n'
20252 || c == ' ' || c == '\t' || c == '\f')
20253 {
20254 if (bytepos <= BEGV_BYTE)
20255 break;
20256 bytepos--;
20257 pos--;
20258 }
20259 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20260 bytepos--;
20261 }
20262 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20263 itb.paragraph_dir = NEUTRAL_DIR;
20264 itb.string.s = NULL;
20265 itb.string.lstring = Qnil;
20266 itb.string.bufpos = 0;
20267 itb.string.unibyte = 0;
20268 /* We have no window to use here for ignoring window-specific
20269 overlays. Using NULL for window pointer will cause
20270 compute_display_string_pos to use the current buffer. */
20271 itb.w = NULL;
20272 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20273 bidi_unshelve_cache (itb_data, 0);
20274 set_buffer_temp (old);
20275 switch (itb.paragraph_dir)
20276 {
20277 case L2R:
20278 return Qleft_to_right;
20279 break;
20280 case R2L:
20281 return Qright_to_left;
20282 break;
20283 default:
20284 emacs_abort ();
20285 }
20286 }
20287 }
20288
20289 DEFUN ("move-point-visually", Fmove_point_visually,
20290 Smove_point_visually, 1, 1, 0,
20291 doc: /* Move point in the visual order in the specified DIRECTION.
20292 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20293 left.
20294
20295 Value is the new character position of point. */)
20296 (Lisp_Object direction)
20297 {
20298 struct window *w = XWINDOW (selected_window);
20299 struct buffer *b = XBUFFER (w->contents);
20300 struct glyph_row *row;
20301 int dir;
20302 Lisp_Object paragraph_dir;
20303
20304 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20305 (!(ROW)->continued_p \
20306 && INTEGERP ((GLYPH)->object) \
20307 && (GLYPH)->type == CHAR_GLYPH \
20308 && (GLYPH)->u.ch == ' ' \
20309 && (GLYPH)->charpos >= 0 \
20310 && !(GLYPH)->avoid_cursor_p)
20311
20312 CHECK_NUMBER (direction);
20313 dir = XINT (direction);
20314 if (dir > 0)
20315 dir = 1;
20316 else
20317 dir = -1;
20318
20319 /* If current matrix is up-to-date, we can use the information
20320 recorded in the glyphs, at least as long as the goal is on the
20321 screen. */
20322 if (w->window_end_valid
20323 && !windows_or_buffers_changed
20324 && b
20325 && !b->clip_changed
20326 && !b->prevent_redisplay_optimizations_p
20327 && !window_outdated (w)
20328 && w->cursor.vpos >= 0
20329 && w->cursor.vpos < w->current_matrix->nrows
20330 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20331 {
20332 struct glyph *g = row->glyphs[TEXT_AREA];
20333 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20334 struct glyph *gpt = g + w->cursor.hpos;
20335
20336 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20337 {
20338 if (BUFFERP (g->object) && g->charpos != PT)
20339 {
20340 SET_PT (g->charpos);
20341 w->cursor.vpos = -1;
20342 return make_number (PT);
20343 }
20344 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20345 {
20346 ptrdiff_t new_pos;
20347
20348 if (BUFFERP (gpt->object))
20349 {
20350 new_pos = PT;
20351 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20352 new_pos += (row->reversed_p ? -dir : dir);
20353 else
20354 new_pos -= (row->reversed_p ? -dir : dir);;
20355 }
20356 else if (BUFFERP (g->object))
20357 new_pos = g->charpos;
20358 else
20359 break;
20360 SET_PT (new_pos);
20361 w->cursor.vpos = -1;
20362 return make_number (PT);
20363 }
20364 else if (ROW_GLYPH_NEWLINE_P (row, g))
20365 {
20366 /* Glyphs inserted at the end of a non-empty line for
20367 positioning the cursor have zero charpos, so we must
20368 deduce the value of point by other means. */
20369 if (g->charpos > 0)
20370 SET_PT (g->charpos);
20371 else if (row->ends_at_zv_p && PT != ZV)
20372 SET_PT (ZV);
20373 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20374 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20375 else
20376 break;
20377 w->cursor.vpos = -1;
20378 return make_number (PT);
20379 }
20380 }
20381 if (g == e || INTEGERP (g->object))
20382 {
20383 if (row->truncated_on_left_p || row->truncated_on_right_p)
20384 goto simulate_display;
20385 if (!row->reversed_p)
20386 row += dir;
20387 else
20388 row -= dir;
20389 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20390 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20391 goto simulate_display;
20392
20393 if (dir > 0)
20394 {
20395 if (row->reversed_p && !row->continued_p)
20396 {
20397 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20398 w->cursor.vpos = -1;
20399 return make_number (PT);
20400 }
20401 g = row->glyphs[TEXT_AREA];
20402 e = g + row->used[TEXT_AREA];
20403 for ( ; g < e; g++)
20404 {
20405 if (BUFFERP (g->object)
20406 /* Empty lines have only one glyph, which stands
20407 for the newline, and whose charpos is the
20408 buffer position of the newline. */
20409 || ROW_GLYPH_NEWLINE_P (row, g)
20410 /* When the buffer ends in a newline, the line at
20411 EOB also has one glyph, but its charpos is -1. */
20412 || (row->ends_at_zv_p
20413 && !row->reversed_p
20414 && INTEGERP (g->object)
20415 && g->type == CHAR_GLYPH
20416 && g->u.ch == ' '))
20417 {
20418 if (g->charpos > 0)
20419 SET_PT (g->charpos);
20420 else if (!row->reversed_p
20421 && row->ends_at_zv_p
20422 && PT != ZV)
20423 SET_PT (ZV);
20424 else
20425 continue;
20426 w->cursor.vpos = -1;
20427 return make_number (PT);
20428 }
20429 }
20430 }
20431 else
20432 {
20433 if (!row->reversed_p && !row->continued_p)
20434 {
20435 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20436 w->cursor.vpos = -1;
20437 return make_number (PT);
20438 }
20439 e = row->glyphs[TEXT_AREA];
20440 g = e + row->used[TEXT_AREA] - 1;
20441 for ( ; g >= e; g--)
20442 {
20443 if (BUFFERP (g->object)
20444 || (ROW_GLYPH_NEWLINE_P (row, g)
20445 && g->charpos > 0)
20446 /* Empty R2L lines on GUI frames have the buffer
20447 position of the newline stored in the stretch
20448 glyph. */
20449 || g->type == STRETCH_GLYPH
20450 || (row->ends_at_zv_p
20451 && row->reversed_p
20452 && INTEGERP (g->object)
20453 && g->type == CHAR_GLYPH
20454 && g->u.ch == ' '))
20455 {
20456 if (g->charpos > 0)
20457 SET_PT (g->charpos);
20458 else if (row->reversed_p
20459 && row->ends_at_zv_p
20460 && PT != ZV)
20461 SET_PT (ZV);
20462 else
20463 continue;
20464 w->cursor.vpos = -1;
20465 return make_number (PT);
20466 }
20467 }
20468 }
20469 }
20470 }
20471
20472 simulate_display:
20473
20474 /* If we wind up here, we failed to move by using the glyphs, so we
20475 need to simulate display instead. */
20476
20477 if (b)
20478 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20479 else
20480 paragraph_dir = Qleft_to_right;
20481 if (EQ (paragraph_dir, Qright_to_left))
20482 dir = -dir;
20483 if (PT <= BEGV && dir < 0)
20484 xsignal0 (Qbeginning_of_buffer);
20485 else if (PT >= ZV && dir > 0)
20486 xsignal0 (Qend_of_buffer);
20487 else
20488 {
20489 struct text_pos pt;
20490 struct it it;
20491 int pt_x, target_x, pixel_width, pt_vpos;
20492 bool at_eol_p;
20493 bool overshoot_expected = false;
20494 bool target_is_eol_p = false;
20495
20496 /* Setup the arena. */
20497 SET_TEXT_POS (pt, PT, PT_BYTE);
20498 start_display (&it, w, pt);
20499
20500 if (it.cmp_it.id < 0
20501 && it.method == GET_FROM_STRING
20502 && it.area == TEXT_AREA
20503 && it.string_from_display_prop_p
20504 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20505 overshoot_expected = true;
20506
20507 /* Find the X coordinate of point. We start from the beginning
20508 of this or previous line to make sure we are before point in
20509 the logical order (since the move_it_* functions can only
20510 move forward). */
20511 reseat_at_previous_visible_line_start (&it);
20512 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20513 if (IT_CHARPOS (it) != PT)
20514 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20515 -1, -1, -1, MOVE_TO_POS);
20516 pt_x = it.current_x;
20517 pt_vpos = it.vpos;
20518 if (dir > 0 || overshoot_expected)
20519 {
20520 struct glyph_row *row = it.glyph_row;
20521
20522 /* When point is at beginning of line, we don't have
20523 information about the glyph there loaded into struct
20524 it. Calling get_next_display_element fixes that. */
20525 if (pt_x == 0)
20526 get_next_display_element (&it);
20527 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20528 it.glyph_row = NULL;
20529 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20530 it.glyph_row = row;
20531 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20532 it, lest it will become out of sync with it's buffer
20533 position. */
20534 it.current_x = pt_x;
20535 }
20536 else
20537 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20538 pixel_width = it.pixel_width;
20539 if (overshoot_expected && at_eol_p)
20540 pixel_width = 0;
20541 else if (pixel_width <= 0)
20542 pixel_width = 1;
20543
20544 /* If there's a display string at point, we are actually at the
20545 glyph to the left of point, so we need to correct the X
20546 coordinate. */
20547 if (overshoot_expected)
20548 pt_x += pixel_width;
20549
20550 /* Compute target X coordinate, either to the left or to the
20551 right of point. On TTY frames, all characters have the same
20552 pixel width of 1, so we can use that. On GUI frames we don't
20553 have an easy way of getting at the pixel width of the
20554 character to the left of point, so we use a different method
20555 of getting to that place. */
20556 if (dir > 0)
20557 target_x = pt_x + pixel_width;
20558 else
20559 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20560
20561 /* Target X coordinate could be one line above or below the line
20562 of point, in which case we need to adjust the target X
20563 coordinate. Also, if moving to the left, we need to begin at
20564 the left edge of the point's screen line. */
20565 if (dir < 0)
20566 {
20567 if (pt_x > 0)
20568 {
20569 start_display (&it, w, pt);
20570 reseat_at_previous_visible_line_start (&it);
20571 it.current_x = it.current_y = it.hpos = 0;
20572 if (pt_vpos != 0)
20573 move_it_by_lines (&it, pt_vpos);
20574 }
20575 else
20576 {
20577 move_it_by_lines (&it, -1);
20578 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20579 target_is_eol_p = true;
20580 }
20581 }
20582 else
20583 {
20584 if (at_eol_p
20585 || (target_x >= it.last_visible_x
20586 && it.line_wrap != TRUNCATE))
20587 {
20588 if (pt_x > 0)
20589 move_it_by_lines (&it, 0);
20590 move_it_by_lines (&it, 1);
20591 target_x = 0;
20592 }
20593 }
20594
20595 /* Move to the target X coordinate. */
20596 #ifdef HAVE_WINDOW_SYSTEM
20597 /* On GUI frames, as we don't know the X coordinate of the
20598 character to the left of point, moving point to the left
20599 requires walking, one grapheme cluster at a time, until we
20600 find ourself at a place immediately to the left of the
20601 character at point. */
20602 if (FRAME_WINDOW_P (it.f) && dir < 0)
20603 {
20604 struct text_pos new_pos = it.current.pos;
20605 enum move_it_result rc = MOVE_X_REACHED;
20606
20607 while (it.current_x + it.pixel_width <= target_x
20608 && rc == MOVE_X_REACHED)
20609 {
20610 int new_x = it.current_x + it.pixel_width;
20611
20612 new_pos = it.current.pos;
20613 if (new_x == it.current_x)
20614 new_x++;
20615 rc = move_it_in_display_line_to (&it, ZV, new_x,
20616 MOVE_TO_POS | MOVE_TO_X);
20617 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20618 break;
20619 }
20620 /* If we ended up on a composed character inside
20621 bidi-reordered text (e.g., Hebrew text with diacritics),
20622 the iterator gives us the buffer position of the last (in
20623 logical order) character of the composed grapheme cluster,
20624 which is not what we want. So we cheat: we compute the
20625 character position of the character that follows (in the
20626 logical order) the one where the above loop stopped. That
20627 character will appear on display to the left of point. */
20628 if (it.bidi_p
20629 && it.bidi_it.scan_dir == -1
20630 && new_pos.charpos - IT_CHARPOS (it) > 1)
20631 {
20632 new_pos.charpos = IT_CHARPOS (it) + 1;
20633 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20634 }
20635 it.current.pos = new_pos;
20636 }
20637 else
20638 #endif
20639 if (it.current_x != target_x)
20640 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20641
20642 /* When lines are truncated, the above loop will stop at the
20643 window edge. But we want to get to the end of line, even if
20644 it is beyond the window edge; automatic hscroll will then
20645 scroll the window to show point as appropriate. */
20646 if (target_is_eol_p && it.line_wrap == TRUNCATE
20647 && get_next_display_element (&it))
20648 {
20649 struct text_pos new_pos = it.current.pos;
20650
20651 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20652 {
20653 set_iterator_to_next (&it, 0);
20654 if (it.method == GET_FROM_BUFFER)
20655 new_pos = it.current.pos;
20656 if (!get_next_display_element (&it))
20657 break;
20658 }
20659
20660 it.current.pos = new_pos;
20661 }
20662
20663 /* If we ended up in a display string that covers point, move to
20664 buffer position to the right in the visual order. */
20665 if (dir > 0)
20666 {
20667 while (IT_CHARPOS (it) == PT)
20668 {
20669 set_iterator_to_next (&it, 0);
20670 if (!get_next_display_element (&it))
20671 break;
20672 }
20673 }
20674
20675 /* Move point to that position. */
20676 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20677 }
20678
20679 return make_number (PT);
20680
20681 #undef ROW_GLYPH_NEWLINE_P
20682 }
20683
20684 \f
20685 /***********************************************************************
20686 Menu Bar
20687 ***********************************************************************/
20688
20689 /* Redisplay the menu bar in the frame for window W.
20690
20691 The menu bar of X frames that don't have X toolkit support is
20692 displayed in a special window W->frame->menu_bar_window.
20693
20694 The menu bar of terminal frames is treated specially as far as
20695 glyph matrices are concerned. Menu bar lines are not part of
20696 windows, so the update is done directly on the frame matrix rows
20697 for the menu bar. */
20698
20699 static void
20700 display_menu_bar (struct window *w)
20701 {
20702 struct frame *f = XFRAME (WINDOW_FRAME (w));
20703 struct it it;
20704 Lisp_Object items;
20705 int i;
20706
20707 /* Don't do all this for graphical frames. */
20708 #ifdef HAVE_NTGUI
20709 if (FRAME_W32_P (f))
20710 return;
20711 #endif
20712 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20713 if (FRAME_X_P (f))
20714 return;
20715 #endif
20716
20717 #ifdef HAVE_NS
20718 if (FRAME_NS_P (f))
20719 return;
20720 #endif /* HAVE_NS */
20721
20722 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20723 eassert (!FRAME_WINDOW_P (f));
20724 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20725 it.first_visible_x = 0;
20726 /* PXW: Use FRAME_PIXEL_WIDTH (f) here ? */
20727 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20728 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20729 if (FRAME_WINDOW_P (f))
20730 {
20731 /* Menu bar lines are displayed in the desired matrix of the
20732 dummy window menu_bar_window. */
20733 struct window *menu_w;
20734 menu_w = XWINDOW (f->menu_bar_window);
20735 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20736 MENU_FACE_ID);
20737 it.first_visible_x = 0;
20738 /* PXW: Use FRAME_PIXEL_WIDTH (f) here ? */
20739 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20740 }
20741 else
20742 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20743 {
20744 /* This is a TTY frame, i.e. character hpos/vpos are used as
20745 pixel x/y. */
20746 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20747 MENU_FACE_ID);
20748 it.first_visible_x = 0;
20749 it.last_visible_x = FRAME_COLS (f);
20750 }
20751
20752 /* FIXME: This should be controlled by a user option. See the
20753 comments in redisplay_tool_bar and display_mode_line about
20754 this. */
20755 it.paragraph_embedding = L2R;
20756
20757 /* Clear all rows of the menu bar. */
20758 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20759 {
20760 struct glyph_row *row = it.glyph_row + i;
20761 clear_glyph_row (row);
20762 row->enabled_p = 1;
20763 row->full_width_p = 1;
20764 }
20765
20766 /* Display all items of the menu bar. */
20767 items = FRAME_MENU_BAR_ITEMS (it.f);
20768 for (i = 0; i < ASIZE (items); i += 4)
20769 {
20770 Lisp_Object string;
20771
20772 /* Stop at nil string. */
20773 string = AREF (items, i + 1);
20774 if (NILP (string))
20775 break;
20776
20777 /* Remember where item was displayed. */
20778 ASET (items, i + 3, make_number (it.hpos));
20779
20780 /* Display the item, pad with one space. */
20781 if (it.current_x < it.last_visible_x)
20782 display_string (NULL, string, Qnil, 0, 0, &it,
20783 SCHARS (string) + 1, 0, 0, -1);
20784 }
20785
20786 /* Fill out the line with spaces. */
20787 if (it.current_x < it.last_visible_x)
20788 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20789
20790 /* Compute the total height of the lines. */
20791 compute_line_metrics (&it);
20792 }
20793
20794 /* Deep copy of a glyph row, including the glyphs. */
20795 static void
20796 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20797 {
20798 struct glyph *pointers[1 + LAST_AREA];
20799 int to_used = to->used[TEXT_AREA];
20800
20801 /* Save glyph pointers of TO. */
20802 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20803
20804 /* Do a structure assignment. */
20805 *to = *from;
20806
20807 /* Restore original glyph pointers of TO. */
20808 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20809
20810 /* Copy the glyphs. */
20811 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20812 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20813
20814 /* If we filled only part of the TO row, fill the rest with
20815 space_glyph (which will display as empty space). */
20816 if (to_used > from->used[TEXT_AREA])
20817 fill_up_frame_row_with_spaces (to, to_used);
20818 }
20819
20820 /* Display one menu item on a TTY, by overwriting the glyphs in the
20821 frame F's desired glyph matrix with glyphs produced from the menu
20822 item text. Called from term.c to display TTY drop-down menus one
20823 item at a time.
20824
20825 ITEM_TEXT is the menu item text as a C string.
20826
20827 FACE_ID is the face ID to be used for this menu item. FACE_ID
20828 could specify one of 3 faces: a face for an enabled item, a face
20829 for a disabled item, or a face for a selected item.
20830
20831 X and Y are coordinates of the first glyph in the frame's desired
20832 matrix to be overwritten by the menu item. Since this is a TTY, Y
20833 is the zero-based number of the glyph row and X is the zero-based
20834 glyph number in the row, starting from left, where to start
20835 displaying the item.
20836
20837 SUBMENU non-zero means this menu item drops down a submenu, which
20838 should be indicated by displaying a proper visual cue after the
20839 item text. */
20840
20841 void
20842 display_tty_menu_item (const char *item_text, int width, int face_id,
20843 int x, int y, int submenu)
20844 {
20845 struct it it;
20846 struct frame *f = SELECTED_FRAME ();
20847 struct window *w = XWINDOW (f->selected_window);
20848 int saved_used, saved_truncated, saved_width, saved_reversed;
20849 struct glyph_row *row;
20850 size_t item_len = strlen (item_text);
20851
20852 eassert (FRAME_TERMCAP_P (f));
20853
20854 /* Don't write beyond the matrix's last row. This can happen for
20855 TTY screens that are not high enough to show the entire menu.
20856 (This is actually a bit of defensive programming, as
20857 tty_menu_display already limits the number of menu items to one
20858 less than the number of screen lines.) */
20859 if (y >= f->desired_matrix->nrows)
20860 return;
20861
20862 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20863 it.first_visible_x = 0;
20864 it.last_visible_x = FRAME_COLS (f) - 1;
20865 row = it.glyph_row;
20866 /* Start with the row contents from the current matrix. */
20867 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20868 saved_width = row->full_width_p;
20869 row->full_width_p = 1;
20870 saved_reversed = row->reversed_p;
20871 row->reversed_p = 0;
20872 row->enabled_p = 1;
20873
20874 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20875 desired face. */
20876 eassert (x < f->desired_matrix->matrix_w);
20877 it.current_x = it.hpos = x;
20878 it.current_y = it.vpos = y;
20879 saved_used = row->used[TEXT_AREA];
20880 saved_truncated = row->truncated_on_right_p;
20881 row->used[TEXT_AREA] = x;
20882 it.face_id = face_id;
20883 it.line_wrap = TRUNCATE;
20884
20885 /* FIXME: This should be controlled by a user option. See the
20886 comments in redisplay_tool_bar and display_mode_line about this.
20887 Also, if paragraph_embedding could ever be R2L, changes will be
20888 needed to avoid shifting to the right the row characters in
20889 term.c:append_glyph. */
20890 it.paragraph_embedding = L2R;
20891
20892 /* Pad with a space on the left. */
20893 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20894 width--;
20895 /* Display the menu item, pad with spaces to WIDTH. */
20896 if (submenu)
20897 {
20898 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20899 item_len, 0, FRAME_COLS (f) - 1, -1);
20900 width -= item_len;
20901 /* Indicate with " >" that there's a submenu. */
20902 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20903 FRAME_COLS (f) - 1, -1);
20904 }
20905 else
20906 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20907 width, 0, FRAME_COLS (f) - 1, -1);
20908
20909 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20910 row->truncated_on_right_p = saved_truncated;
20911 row->hash = row_hash (row);
20912 row->full_width_p = saved_width;
20913 row->reversed_p = saved_reversed;
20914 }
20915 \f
20916 /***********************************************************************
20917 Mode Line
20918 ***********************************************************************/
20919
20920 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20921 FORCE is non-zero, redisplay mode lines unconditionally.
20922 Otherwise, redisplay only mode lines that are garbaged. Value is
20923 the number of windows whose mode lines were redisplayed. */
20924
20925 static int
20926 redisplay_mode_lines (Lisp_Object window, bool force)
20927 {
20928 int nwindows = 0;
20929
20930 while (!NILP (window))
20931 {
20932 struct window *w = XWINDOW (window);
20933
20934 if (WINDOWP (w->contents))
20935 nwindows += redisplay_mode_lines (w->contents, force);
20936 else if (force
20937 || FRAME_GARBAGED_P (XFRAME (w->frame))
20938 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20939 {
20940 struct text_pos lpoint;
20941 struct buffer *old = current_buffer;
20942
20943 /* Set the window's buffer for the mode line display. */
20944 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20945 set_buffer_internal_1 (XBUFFER (w->contents));
20946
20947 /* Point refers normally to the selected window. For any
20948 other window, set up appropriate value. */
20949 if (!EQ (window, selected_window))
20950 {
20951 struct text_pos pt;
20952
20953 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20954 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20955 }
20956
20957 /* Display mode lines. */
20958 clear_glyph_matrix (w->desired_matrix);
20959 if (display_mode_lines (w))
20960 ++nwindows;
20961
20962 /* Restore old settings. */
20963 set_buffer_internal_1 (old);
20964 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20965 }
20966
20967 window = w->next;
20968 }
20969
20970 return nwindows;
20971 }
20972
20973
20974 /* Display the mode and/or header line of window W. Value is the
20975 sum number of mode lines and header lines displayed. */
20976
20977 static int
20978 display_mode_lines (struct window *w)
20979 {
20980 Lisp_Object old_selected_window = selected_window;
20981 Lisp_Object old_selected_frame = selected_frame;
20982 Lisp_Object new_frame = w->frame;
20983 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20984 int n = 0;
20985
20986 selected_frame = new_frame;
20987 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20988 or window's point, then we'd need select_window_1 here as well. */
20989 XSETWINDOW (selected_window, w);
20990 XFRAME (new_frame)->selected_window = selected_window;
20991
20992 /* These will be set while the mode line specs are processed. */
20993 line_number_displayed = 0;
20994 w->column_number_displayed = -1;
20995
20996 if (WINDOW_WANTS_MODELINE_P (w))
20997 {
20998 struct window *sel_w = XWINDOW (old_selected_window);
20999
21000 /* Select mode line face based on the real selected window. */
21001 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21002 BVAR (current_buffer, mode_line_format));
21003 ++n;
21004 }
21005
21006 if (WINDOW_WANTS_HEADER_LINE_P (w))
21007 {
21008 display_mode_line (w, HEADER_LINE_FACE_ID,
21009 BVAR (current_buffer, header_line_format));
21010 ++n;
21011 }
21012
21013 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21014 selected_frame = old_selected_frame;
21015 selected_window = old_selected_window;
21016 if (n > 0)
21017 w->must_be_updated_p = true;
21018 return n;
21019 }
21020
21021
21022 /* Display mode or header line of window W. FACE_ID specifies which
21023 line to display; it is either MODE_LINE_FACE_ID or
21024 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21025 display. Value is the pixel height of the mode/header line
21026 displayed. */
21027
21028 static int
21029 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21030 {
21031 struct it it;
21032 struct face *face;
21033 ptrdiff_t count = SPECPDL_INDEX ();
21034
21035 init_iterator (&it, w, -1, -1, NULL, face_id);
21036 /* Don't extend on a previously drawn mode-line.
21037 This may happen if called from pos_visible_p. */
21038 it.glyph_row->enabled_p = 0;
21039 prepare_desired_row (it.glyph_row);
21040
21041 it.glyph_row->mode_line_p = 1;
21042
21043 /* FIXME: This should be controlled by a user option. But
21044 supporting such an option is not trivial, since the mode line is
21045 made up of many separate strings. */
21046 it.paragraph_embedding = L2R;
21047
21048 record_unwind_protect (unwind_format_mode_line,
21049 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21050
21051 mode_line_target = MODE_LINE_DISPLAY;
21052
21053 /* Temporarily make frame's keyboard the current kboard so that
21054 kboard-local variables in the mode_line_format will get the right
21055 values. */
21056 push_kboard (FRAME_KBOARD (it.f));
21057 record_unwind_save_match_data ();
21058 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21059 pop_kboard ();
21060
21061 unbind_to (count, Qnil);
21062
21063 /* Fill up with spaces. */
21064 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21065
21066 compute_line_metrics (&it);
21067 it.glyph_row->full_width_p = 1;
21068 it.glyph_row->continued_p = 0;
21069 it.glyph_row->truncated_on_left_p = 0;
21070 it.glyph_row->truncated_on_right_p = 0;
21071
21072 /* Make a 3D mode-line have a shadow at its right end. */
21073 face = FACE_FROM_ID (it.f, face_id);
21074 extend_face_to_end_of_line (&it);
21075 if (face->box != FACE_NO_BOX)
21076 {
21077 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21078 + it.glyph_row->used[TEXT_AREA] - 1);
21079 last->right_box_line_p = 1;
21080 }
21081
21082 return it.glyph_row->height;
21083 }
21084
21085 /* Move element ELT in LIST to the front of LIST.
21086 Return the updated list. */
21087
21088 static Lisp_Object
21089 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21090 {
21091 register Lisp_Object tail, prev;
21092 register Lisp_Object tem;
21093
21094 tail = list;
21095 prev = Qnil;
21096 while (CONSP (tail))
21097 {
21098 tem = XCAR (tail);
21099
21100 if (EQ (elt, tem))
21101 {
21102 /* Splice out the link TAIL. */
21103 if (NILP (prev))
21104 list = XCDR (tail);
21105 else
21106 Fsetcdr (prev, XCDR (tail));
21107
21108 /* Now make it the first. */
21109 Fsetcdr (tail, list);
21110 return tail;
21111 }
21112 else
21113 prev = tail;
21114 tail = XCDR (tail);
21115 QUIT;
21116 }
21117
21118 /* Not found--return unchanged LIST. */
21119 return list;
21120 }
21121
21122 /* Contribute ELT to the mode line for window IT->w. How it
21123 translates into text depends on its data type.
21124
21125 IT describes the display environment in which we display, as usual.
21126
21127 DEPTH is the depth in recursion. It is used to prevent
21128 infinite recursion here.
21129
21130 FIELD_WIDTH is the number of characters the display of ELT should
21131 occupy in the mode line, and PRECISION is the maximum number of
21132 characters to display from ELT's representation. See
21133 display_string for details.
21134
21135 Returns the hpos of the end of the text generated by ELT.
21136
21137 PROPS is a property list to add to any string we encounter.
21138
21139 If RISKY is nonzero, remove (disregard) any properties in any string
21140 we encounter, and ignore :eval and :propertize.
21141
21142 The global variable `mode_line_target' determines whether the
21143 output is passed to `store_mode_line_noprop',
21144 `store_mode_line_string', or `display_string'. */
21145
21146 static int
21147 display_mode_element (struct it *it, int depth, int field_width, int precision,
21148 Lisp_Object elt, Lisp_Object props, int risky)
21149 {
21150 int n = 0, field, prec;
21151 int literal = 0;
21152
21153 tail_recurse:
21154 if (depth > 100)
21155 elt = build_string ("*too-deep*");
21156
21157 depth++;
21158
21159 switch (XTYPE (elt))
21160 {
21161 case Lisp_String:
21162 {
21163 /* A string: output it and check for %-constructs within it. */
21164 unsigned char c;
21165 ptrdiff_t offset = 0;
21166
21167 if (SCHARS (elt) > 0
21168 && (!NILP (props) || risky))
21169 {
21170 Lisp_Object oprops, aelt;
21171 oprops = Ftext_properties_at (make_number (0), elt);
21172
21173 /* If the starting string's properties are not what
21174 we want, translate the string. Also, if the string
21175 is risky, do that anyway. */
21176
21177 if (NILP (Fequal (props, oprops)) || risky)
21178 {
21179 /* If the starting string has properties,
21180 merge the specified ones onto the existing ones. */
21181 if (! NILP (oprops) && !risky)
21182 {
21183 Lisp_Object tem;
21184
21185 oprops = Fcopy_sequence (oprops);
21186 tem = props;
21187 while (CONSP (tem))
21188 {
21189 oprops = Fplist_put (oprops, XCAR (tem),
21190 XCAR (XCDR (tem)));
21191 tem = XCDR (XCDR (tem));
21192 }
21193 props = oprops;
21194 }
21195
21196 aelt = Fassoc (elt, mode_line_proptrans_alist);
21197 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21198 {
21199 /* AELT is what we want. Move it to the front
21200 without consing. */
21201 elt = XCAR (aelt);
21202 mode_line_proptrans_alist
21203 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21204 }
21205 else
21206 {
21207 Lisp_Object tem;
21208
21209 /* If AELT has the wrong props, it is useless.
21210 so get rid of it. */
21211 if (! NILP (aelt))
21212 mode_line_proptrans_alist
21213 = Fdelq (aelt, mode_line_proptrans_alist);
21214
21215 elt = Fcopy_sequence (elt);
21216 Fset_text_properties (make_number (0), Flength (elt),
21217 props, elt);
21218 /* Add this item to mode_line_proptrans_alist. */
21219 mode_line_proptrans_alist
21220 = Fcons (Fcons (elt, props),
21221 mode_line_proptrans_alist);
21222 /* Truncate mode_line_proptrans_alist
21223 to at most 50 elements. */
21224 tem = Fnthcdr (make_number (50),
21225 mode_line_proptrans_alist);
21226 if (! NILP (tem))
21227 XSETCDR (tem, Qnil);
21228 }
21229 }
21230 }
21231
21232 offset = 0;
21233
21234 if (literal)
21235 {
21236 prec = precision - n;
21237 switch (mode_line_target)
21238 {
21239 case MODE_LINE_NOPROP:
21240 case MODE_LINE_TITLE:
21241 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21242 break;
21243 case MODE_LINE_STRING:
21244 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21245 break;
21246 case MODE_LINE_DISPLAY:
21247 n += display_string (NULL, elt, Qnil, 0, 0, it,
21248 0, prec, 0, STRING_MULTIBYTE (elt));
21249 break;
21250 }
21251
21252 break;
21253 }
21254
21255 /* Handle the non-literal case. */
21256
21257 while ((precision <= 0 || n < precision)
21258 && SREF (elt, offset) != 0
21259 && (mode_line_target != MODE_LINE_DISPLAY
21260 || it->current_x < it->last_visible_x))
21261 {
21262 ptrdiff_t last_offset = offset;
21263
21264 /* Advance to end of string or next format specifier. */
21265 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21266 ;
21267
21268 if (offset - 1 != last_offset)
21269 {
21270 ptrdiff_t nchars, nbytes;
21271
21272 /* Output to end of string or up to '%'. Field width
21273 is length of string. Don't output more than
21274 PRECISION allows us. */
21275 offset--;
21276
21277 prec = c_string_width (SDATA (elt) + last_offset,
21278 offset - last_offset, precision - n,
21279 &nchars, &nbytes);
21280
21281 switch (mode_line_target)
21282 {
21283 case MODE_LINE_NOPROP:
21284 case MODE_LINE_TITLE:
21285 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21286 break;
21287 case MODE_LINE_STRING:
21288 {
21289 ptrdiff_t bytepos = last_offset;
21290 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21291 ptrdiff_t endpos = (precision <= 0
21292 ? string_byte_to_char (elt, offset)
21293 : charpos + nchars);
21294
21295 n += store_mode_line_string (NULL,
21296 Fsubstring (elt, make_number (charpos),
21297 make_number (endpos)),
21298 0, 0, 0, Qnil);
21299 }
21300 break;
21301 case MODE_LINE_DISPLAY:
21302 {
21303 ptrdiff_t bytepos = last_offset;
21304 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21305
21306 if (precision <= 0)
21307 nchars = string_byte_to_char (elt, offset) - charpos;
21308 n += display_string (NULL, elt, Qnil, 0, charpos,
21309 it, 0, nchars, 0,
21310 STRING_MULTIBYTE (elt));
21311 }
21312 break;
21313 }
21314 }
21315 else /* c == '%' */
21316 {
21317 ptrdiff_t percent_position = offset;
21318
21319 /* Get the specified minimum width. Zero means
21320 don't pad. */
21321 field = 0;
21322 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21323 field = field * 10 + c - '0';
21324
21325 /* Don't pad beyond the total padding allowed. */
21326 if (field_width - n > 0 && field > field_width - n)
21327 field = field_width - n;
21328
21329 /* Note that either PRECISION <= 0 or N < PRECISION. */
21330 prec = precision - n;
21331
21332 if (c == 'M')
21333 n += display_mode_element (it, depth, field, prec,
21334 Vglobal_mode_string, props,
21335 risky);
21336 else if (c != 0)
21337 {
21338 bool multibyte;
21339 ptrdiff_t bytepos, charpos;
21340 const char *spec;
21341 Lisp_Object string;
21342
21343 bytepos = percent_position;
21344 charpos = (STRING_MULTIBYTE (elt)
21345 ? string_byte_to_char (elt, bytepos)
21346 : bytepos);
21347 spec = decode_mode_spec (it->w, c, field, &string);
21348 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21349
21350 switch (mode_line_target)
21351 {
21352 case MODE_LINE_NOPROP:
21353 case MODE_LINE_TITLE:
21354 n += store_mode_line_noprop (spec, field, prec);
21355 break;
21356 case MODE_LINE_STRING:
21357 {
21358 Lisp_Object tem = build_string (spec);
21359 props = Ftext_properties_at (make_number (charpos), elt);
21360 /* Should only keep face property in props */
21361 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21362 }
21363 break;
21364 case MODE_LINE_DISPLAY:
21365 {
21366 int nglyphs_before, nwritten;
21367
21368 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21369 nwritten = display_string (spec, string, elt,
21370 charpos, 0, it,
21371 field, prec, 0,
21372 multibyte);
21373
21374 /* Assign to the glyphs written above the
21375 string where the `%x' came from, position
21376 of the `%'. */
21377 if (nwritten > 0)
21378 {
21379 struct glyph *glyph
21380 = (it->glyph_row->glyphs[TEXT_AREA]
21381 + nglyphs_before);
21382 int i;
21383
21384 for (i = 0; i < nwritten; ++i)
21385 {
21386 glyph[i].object = elt;
21387 glyph[i].charpos = charpos;
21388 }
21389
21390 n += nwritten;
21391 }
21392 }
21393 break;
21394 }
21395 }
21396 else /* c == 0 */
21397 break;
21398 }
21399 }
21400 }
21401 break;
21402
21403 case Lisp_Symbol:
21404 /* A symbol: process the value of the symbol recursively
21405 as if it appeared here directly. Avoid error if symbol void.
21406 Special case: if value of symbol is a string, output the string
21407 literally. */
21408 {
21409 register Lisp_Object tem;
21410
21411 /* If the variable is not marked as risky to set
21412 then its contents are risky to use. */
21413 if (NILP (Fget (elt, Qrisky_local_variable)))
21414 risky = 1;
21415
21416 tem = Fboundp (elt);
21417 if (!NILP (tem))
21418 {
21419 tem = Fsymbol_value (elt);
21420 /* If value is a string, output that string literally:
21421 don't check for % within it. */
21422 if (STRINGP (tem))
21423 literal = 1;
21424
21425 if (!EQ (tem, elt))
21426 {
21427 /* Give up right away for nil or t. */
21428 elt = tem;
21429 goto tail_recurse;
21430 }
21431 }
21432 }
21433 break;
21434
21435 case Lisp_Cons:
21436 {
21437 register Lisp_Object car, tem;
21438
21439 /* A cons cell: five distinct cases.
21440 If first element is :eval or :propertize, do something special.
21441 If first element is a string or a cons, process all the elements
21442 and effectively concatenate them.
21443 If first element is a negative number, truncate displaying cdr to
21444 at most that many characters. If positive, pad (with spaces)
21445 to at least that many characters.
21446 If first element is a symbol, process the cadr or caddr recursively
21447 according to whether the symbol's value is non-nil or nil. */
21448 car = XCAR (elt);
21449 if (EQ (car, QCeval))
21450 {
21451 /* An element of the form (:eval FORM) means evaluate FORM
21452 and use the result as mode line elements. */
21453
21454 if (risky)
21455 break;
21456
21457 if (CONSP (XCDR (elt)))
21458 {
21459 Lisp_Object spec;
21460 spec = safe_eval (XCAR (XCDR (elt)));
21461 n += display_mode_element (it, depth, field_width - n,
21462 precision - n, spec, props,
21463 risky);
21464 }
21465 }
21466 else if (EQ (car, QCpropertize))
21467 {
21468 /* An element of the form (:propertize ELT PROPS...)
21469 means display ELT but applying properties PROPS. */
21470
21471 if (risky)
21472 break;
21473
21474 if (CONSP (XCDR (elt)))
21475 n += display_mode_element (it, depth, field_width - n,
21476 precision - n, XCAR (XCDR (elt)),
21477 XCDR (XCDR (elt)), risky);
21478 }
21479 else if (SYMBOLP (car))
21480 {
21481 tem = Fboundp (car);
21482 elt = XCDR (elt);
21483 if (!CONSP (elt))
21484 goto invalid;
21485 /* elt is now the cdr, and we know it is a cons cell.
21486 Use its car if CAR has a non-nil value. */
21487 if (!NILP (tem))
21488 {
21489 tem = Fsymbol_value (car);
21490 if (!NILP (tem))
21491 {
21492 elt = XCAR (elt);
21493 goto tail_recurse;
21494 }
21495 }
21496 /* Symbol's value is nil (or symbol is unbound)
21497 Get the cddr of the original list
21498 and if possible find the caddr and use that. */
21499 elt = XCDR (elt);
21500 if (NILP (elt))
21501 break;
21502 else if (!CONSP (elt))
21503 goto invalid;
21504 elt = XCAR (elt);
21505 goto tail_recurse;
21506 }
21507 else if (INTEGERP (car))
21508 {
21509 register int lim = XINT (car);
21510 elt = XCDR (elt);
21511 if (lim < 0)
21512 {
21513 /* Negative int means reduce maximum width. */
21514 if (precision <= 0)
21515 precision = -lim;
21516 else
21517 precision = min (precision, -lim);
21518 }
21519 else if (lim > 0)
21520 {
21521 /* Padding specified. Don't let it be more than
21522 current maximum. */
21523 if (precision > 0)
21524 lim = min (precision, lim);
21525
21526 /* If that's more padding than already wanted, queue it.
21527 But don't reduce padding already specified even if
21528 that is beyond the current truncation point. */
21529 field_width = max (lim, field_width);
21530 }
21531 goto tail_recurse;
21532 }
21533 else if (STRINGP (car) || CONSP (car))
21534 {
21535 Lisp_Object halftail = elt;
21536 int len = 0;
21537
21538 while (CONSP (elt)
21539 && (precision <= 0 || n < precision))
21540 {
21541 n += display_mode_element (it, depth,
21542 /* Do padding only after the last
21543 element in the list. */
21544 (! CONSP (XCDR (elt))
21545 ? field_width - n
21546 : 0),
21547 precision - n, XCAR (elt),
21548 props, risky);
21549 elt = XCDR (elt);
21550 len++;
21551 if ((len & 1) == 0)
21552 halftail = XCDR (halftail);
21553 /* Check for cycle. */
21554 if (EQ (halftail, elt))
21555 break;
21556 }
21557 }
21558 }
21559 break;
21560
21561 default:
21562 invalid:
21563 elt = build_string ("*invalid*");
21564 goto tail_recurse;
21565 }
21566
21567 /* Pad to FIELD_WIDTH. */
21568 if (field_width > 0 && n < field_width)
21569 {
21570 switch (mode_line_target)
21571 {
21572 case MODE_LINE_NOPROP:
21573 case MODE_LINE_TITLE:
21574 n += store_mode_line_noprop ("", field_width - n, 0);
21575 break;
21576 case MODE_LINE_STRING:
21577 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21578 break;
21579 case MODE_LINE_DISPLAY:
21580 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21581 0, 0, 0);
21582 break;
21583 }
21584 }
21585
21586 return n;
21587 }
21588
21589 /* Store a mode-line string element in mode_line_string_list.
21590
21591 If STRING is non-null, display that C string. Otherwise, the Lisp
21592 string LISP_STRING is displayed.
21593
21594 FIELD_WIDTH is the minimum number of output glyphs to produce.
21595 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21596 with spaces. FIELD_WIDTH <= 0 means don't pad.
21597
21598 PRECISION is the maximum number of characters to output from
21599 STRING. PRECISION <= 0 means don't truncate the string.
21600
21601 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21602 properties to the string.
21603
21604 PROPS are the properties to add to the string.
21605 The mode_line_string_face face property is always added to the string.
21606 */
21607
21608 static int
21609 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21610 int field_width, int precision, Lisp_Object props)
21611 {
21612 ptrdiff_t len;
21613 int n = 0;
21614
21615 if (string != NULL)
21616 {
21617 len = strlen (string);
21618 if (precision > 0 && len > precision)
21619 len = precision;
21620 lisp_string = make_string (string, len);
21621 if (NILP (props))
21622 props = mode_line_string_face_prop;
21623 else if (!NILP (mode_line_string_face))
21624 {
21625 Lisp_Object face = Fplist_get (props, Qface);
21626 props = Fcopy_sequence (props);
21627 if (NILP (face))
21628 face = mode_line_string_face;
21629 else
21630 face = list2 (face, mode_line_string_face);
21631 props = Fplist_put (props, Qface, face);
21632 }
21633 Fadd_text_properties (make_number (0), make_number (len),
21634 props, lisp_string);
21635 }
21636 else
21637 {
21638 len = XFASTINT (Flength (lisp_string));
21639 if (precision > 0 && len > precision)
21640 {
21641 len = precision;
21642 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21643 precision = -1;
21644 }
21645 if (!NILP (mode_line_string_face))
21646 {
21647 Lisp_Object face;
21648 if (NILP (props))
21649 props = Ftext_properties_at (make_number (0), lisp_string);
21650 face = Fplist_get (props, Qface);
21651 if (NILP (face))
21652 face = mode_line_string_face;
21653 else
21654 face = list2 (face, mode_line_string_face);
21655 props = list2 (Qface, face);
21656 if (copy_string)
21657 lisp_string = Fcopy_sequence (lisp_string);
21658 }
21659 if (!NILP (props))
21660 Fadd_text_properties (make_number (0), make_number (len),
21661 props, lisp_string);
21662 }
21663
21664 if (len > 0)
21665 {
21666 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21667 n += len;
21668 }
21669
21670 if (field_width > len)
21671 {
21672 field_width -= len;
21673 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21674 if (!NILP (props))
21675 Fadd_text_properties (make_number (0), make_number (field_width),
21676 props, lisp_string);
21677 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21678 n += field_width;
21679 }
21680
21681 return n;
21682 }
21683
21684
21685 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21686 1, 4, 0,
21687 doc: /* Format a string out of a mode line format specification.
21688 First arg FORMAT specifies the mode line format (see `mode-line-format'
21689 for details) to use.
21690
21691 By default, the format is evaluated for the currently selected window.
21692
21693 Optional second arg FACE specifies the face property to put on all
21694 characters for which no face is specified. The value nil means the
21695 default face. The value t means whatever face the window's mode line
21696 currently uses (either `mode-line' or `mode-line-inactive',
21697 depending on whether the window is the selected window or not).
21698 An integer value means the value string has no text
21699 properties.
21700
21701 Optional third and fourth args WINDOW and BUFFER specify the window
21702 and buffer to use as the context for the formatting (defaults
21703 are the selected window and the WINDOW's buffer). */)
21704 (Lisp_Object format, Lisp_Object face,
21705 Lisp_Object window, Lisp_Object buffer)
21706 {
21707 struct it it;
21708 int len;
21709 struct window *w;
21710 struct buffer *old_buffer = NULL;
21711 int face_id;
21712 int no_props = INTEGERP (face);
21713 ptrdiff_t count = SPECPDL_INDEX ();
21714 Lisp_Object str;
21715 int string_start = 0;
21716
21717 w = decode_any_window (window);
21718 XSETWINDOW (window, w);
21719
21720 if (NILP (buffer))
21721 buffer = w->contents;
21722 CHECK_BUFFER (buffer);
21723
21724 /* Make formatting the modeline a non-op when noninteractive, otherwise
21725 there will be problems later caused by a partially initialized frame. */
21726 if (NILP (format) || noninteractive)
21727 return empty_unibyte_string;
21728
21729 if (no_props)
21730 face = Qnil;
21731
21732 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21733 : EQ (face, Qt) ? (EQ (window, selected_window)
21734 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21735 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21736 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21737 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21738 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21739 : DEFAULT_FACE_ID;
21740
21741 old_buffer = current_buffer;
21742
21743 /* Save things including mode_line_proptrans_alist,
21744 and set that to nil so that we don't alter the outer value. */
21745 record_unwind_protect (unwind_format_mode_line,
21746 format_mode_line_unwind_data
21747 (XFRAME (WINDOW_FRAME (w)),
21748 old_buffer, selected_window, 1));
21749 mode_line_proptrans_alist = Qnil;
21750
21751 Fselect_window (window, Qt);
21752 set_buffer_internal_1 (XBUFFER (buffer));
21753
21754 init_iterator (&it, w, -1, -1, NULL, face_id);
21755
21756 if (no_props)
21757 {
21758 mode_line_target = MODE_LINE_NOPROP;
21759 mode_line_string_face_prop = Qnil;
21760 mode_line_string_list = Qnil;
21761 string_start = MODE_LINE_NOPROP_LEN (0);
21762 }
21763 else
21764 {
21765 mode_line_target = MODE_LINE_STRING;
21766 mode_line_string_list = Qnil;
21767 mode_line_string_face = face;
21768 mode_line_string_face_prop
21769 = NILP (face) ? Qnil : list2 (Qface, face);
21770 }
21771
21772 push_kboard (FRAME_KBOARD (it.f));
21773 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21774 pop_kboard ();
21775
21776 if (no_props)
21777 {
21778 len = MODE_LINE_NOPROP_LEN (string_start);
21779 str = make_string (mode_line_noprop_buf + string_start, len);
21780 }
21781 else
21782 {
21783 mode_line_string_list = Fnreverse (mode_line_string_list);
21784 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21785 empty_unibyte_string);
21786 }
21787
21788 unbind_to (count, Qnil);
21789 return str;
21790 }
21791
21792 /* Write a null-terminated, right justified decimal representation of
21793 the positive integer D to BUF using a minimal field width WIDTH. */
21794
21795 static void
21796 pint2str (register char *buf, register int width, register ptrdiff_t d)
21797 {
21798 register char *p = buf;
21799
21800 if (d <= 0)
21801 *p++ = '0';
21802 else
21803 {
21804 while (d > 0)
21805 {
21806 *p++ = d % 10 + '0';
21807 d /= 10;
21808 }
21809 }
21810
21811 for (width -= (int) (p - buf); width > 0; --width)
21812 *p++ = ' ';
21813 *p-- = '\0';
21814 while (p > buf)
21815 {
21816 d = *buf;
21817 *buf++ = *p;
21818 *p-- = d;
21819 }
21820 }
21821
21822 /* Write a null-terminated, right justified decimal and "human
21823 readable" representation of the nonnegative integer D to BUF using
21824 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21825
21826 static const char power_letter[] =
21827 {
21828 0, /* no letter */
21829 'k', /* kilo */
21830 'M', /* mega */
21831 'G', /* giga */
21832 'T', /* tera */
21833 'P', /* peta */
21834 'E', /* exa */
21835 'Z', /* zetta */
21836 'Y' /* yotta */
21837 };
21838
21839 static void
21840 pint2hrstr (char *buf, int width, ptrdiff_t d)
21841 {
21842 /* We aim to represent the nonnegative integer D as
21843 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21844 ptrdiff_t quotient = d;
21845 int remainder = 0;
21846 /* -1 means: do not use TENTHS. */
21847 int tenths = -1;
21848 int exponent = 0;
21849
21850 /* Length of QUOTIENT.TENTHS as a string. */
21851 int length;
21852
21853 char * psuffix;
21854 char * p;
21855
21856 if (quotient >= 1000)
21857 {
21858 /* Scale to the appropriate EXPONENT. */
21859 do
21860 {
21861 remainder = quotient % 1000;
21862 quotient /= 1000;
21863 exponent++;
21864 }
21865 while (quotient >= 1000);
21866
21867 /* Round to nearest and decide whether to use TENTHS or not. */
21868 if (quotient <= 9)
21869 {
21870 tenths = remainder / 100;
21871 if (remainder % 100 >= 50)
21872 {
21873 if (tenths < 9)
21874 tenths++;
21875 else
21876 {
21877 quotient++;
21878 if (quotient == 10)
21879 tenths = -1;
21880 else
21881 tenths = 0;
21882 }
21883 }
21884 }
21885 else
21886 if (remainder >= 500)
21887 {
21888 if (quotient < 999)
21889 quotient++;
21890 else
21891 {
21892 quotient = 1;
21893 exponent++;
21894 tenths = 0;
21895 }
21896 }
21897 }
21898
21899 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21900 if (tenths == -1 && quotient <= 99)
21901 if (quotient <= 9)
21902 length = 1;
21903 else
21904 length = 2;
21905 else
21906 length = 3;
21907 p = psuffix = buf + max (width, length);
21908
21909 /* Print EXPONENT. */
21910 *psuffix++ = power_letter[exponent];
21911 *psuffix = '\0';
21912
21913 /* Print TENTHS. */
21914 if (tenths >= 0)
21915 {
21916 *--p = '0' + tenths;
21917 *--p = '.';
21918 }
21919
21920 /* Print QUOTIENT. */
21921 do
21922 {
21923 int digit = quotient % 10;
21924 *--p = '0' + digit;
21925 }
21926 while ((quotient /= 10) != 0);
21927
21928 /* Print leading spaces. */
21929 while (buf < p)
21930 *--p = ' ';
21931 }
21932
21933 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21934 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21935 type of CODING_SYSTEM. Return updated pointer into BUF. */
21936
21937 static unsigned char invalid_eol_type[] = "(*invalid*)";
21938
21939 static char *
21940 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21941 {
21942 Lisp_Object val;
21943 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21944 const unsigned char *eol_str;
21945 int eol_str_len;
21946 /* The EOL conversion we are using. */
21947 Lisp_Object eoltype;
21948
21949 val = CODING_SYSTEM_SPEC (coding_system);
21950 eoltype = Qnil;
21951
21952 if (!VECTORP (val)) /* Not yet decided. */
21953 {
21954 *buf++ = multibyte ? '-' : ' ';
21955 if (eol_flag)
21956 eoltype = eol_mnemonic_undecided;
21957 /* Don't mention EOL conversion if it isn't decided. */
21958 }
21959 else
21960 {
21961 Lisp_Object attrs;
21962 Lisp_Object eolvalue;
21963
21964 attrs = AREF (val, 0);
21965 eolvalue = AREF (val, 2);
21966
21967 *buf++ = multibyte
21968 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21969 : ' ';
21970
21971 if (eol_flag)
21972 {
21973 /* The EOL conversion that is normal on this system. */
21974
21975 if (NILP (eolvalue)) /* Not yet decided. */
21976 eoltype = eol_mnemonic_undecided;
21977 else if (VECTORP (eolvalue)) /* Not yet decided. */
21978 eoltype = eol_mnemonic_undecided;
21979 else /* eolvalue is Qunix, Qdos, or Qmac. */
21980 eoltype = (EQ (eolvalue, Qunix)
21981 ? eol_mnemonic_unix
21982 : (EQ (eolvalue, Qdos) == 1
21983 ? eol_mnemonic_dos : eol_mnemonic_mac));
21984 }
21985 }
21986
21987 if (eol_flag)
21988 {
21989 /* Mention the EOL conversion if it is not the usual one. */
21990 if (STRINGP (eoltype))
21991 {
21992 eol_str = SDATA (eoltype);
21993 eol_str_len = SBYTES (eoltype);
21994 }
21995 else if (CHARACTERP (eoltype))
21996 {
21997 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21998 int c = XFASTINT (eoltype);
21999 eol_str_len = CHAR_STRING (c, tmp);
22000 eol_str = tmp;
22001 }
22002 else
22003 {
22004 eol_str = invalid_eol_type;
22005 eol_str_len = sizeof (invalid_eol_type) - 1;
22006 }
22007 memcpy (buf, eol_str, eol_str_len);
22008 buf += eol_str_len;
22009 }
22010
22011 return buf;
22012 }
22013
22014 /* Return a string for the output of a mode line %-spec for window W,
22015 generated by character C. FIELD_WIDTH > 0 means pad the string
22016 returned with spaces to that value. Return a Lisp string in
22017 *STRING if the resulting string is taken from that Lisp string.
22018
22019 Note we operate on the current buffer for most purposes. */
22020
22021 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22022
22023 static const char *
22024 decode_mode_spec (struct window *w, register int c, int field_width,
22025 Lisp_Object *string)
22026 {
22027 Lisp_Object obj;
22028 struct frame *f = XFRAME (WINDOW_FRAME (w));
22029 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22030 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22031 produce strings from numerical values, so limit preposterously
22032 large values of FIELD_WIDTH to avoid overrunning the buffer's
22033 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22034 bytes plus the terminating null. */
22035 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22036 struct buffer *b = current_buffer;
22037
22038 obj = Qnil;
22039 *string = Qnil;
22040
22041 switch (c)
22042 {
22043 case '*':
22044 if (!NILP (BVAR (b, read_only)))
22045 return "%";
22046 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22047 return "*";
22048 return "-";
22049
22050 case '+':
22051 /* This differs from %* only for a modified read-only buffer. */
22052 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22053 return "*";
22054 if (!NILP (BVAR (b, read_only)))
22055 return "%";
22056 return "-";
22057
22058 case '&':
22059 /* This differs from %* in ignoring read-only-ness. */
22060 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22061 return "*";
22062 return "-";
22063
22064 case '%':
22065 return "%";
22066
22067 case '[':
22068 {
22069 int i;
22070 char *p;
22071
22072 if (command_loop_level > 5)
22073 return "[[[... ";
22074 p = decode_mode_spec_buf;
22075 for (i = 0; i < command_loop_level; i++)
22076 *p++ = '[';
22077 *p = 0;
22078 return decode_mode_spec_buf;
22079 }
22080
22081 case ']':
22082 {
22083 int i;
22084 char *p;
22085
22086 if (command_loop_level > 5)
22087 return " ...]]]";
22088 p = decode_mode_spec_buf;
22089 for (i = 0; i < command_loop_level; i++)
22090 *p++ = ']';
22091 *p = 0;
22092 return decode_mode_spec_buf;
22093 }
22094
22095 case '-':
22096 {
22097 register int i;
22098
22099 /* Let lots_of_dashes be a string of infinite length. */
22100 if (mode_line_target == MODE_LINE_NOPROP
22101 || mode_line_target == MODE_LINE_STRING)
22102 return "--";
22103 if (field_width <= 0
22104 || field_width > sizeof (lots_of_dashes))
22105 {
22106 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22107 decode_mode_spec_buf[i] = '-';
22108 decode_mode_spec_buf[i] = '\0';
22109 return decode_mode_spec_buf;
22110 }
22111 else
22112 return lots_of_dashes;
22113 }
22114
22115 case 'b':
22116 obj = BVAR (b, name);
22117 break;
22118
22119 case 'c':
22120 /* %c and %l are ignored in `frame-title-format'.
22121 (In redisplay_internal, the frame title is drawn _before_ the
22122 windows are updated, so the stuff which depends on actual
22123 window contents (such as %l) may fail to render properly, or
22124 even crash emacs.) */
22125 if (mode_line_target == MODE_LINE_TITLE)
22126 return "";
22127 else
22128 {
22129 ptrdiff_t col = current_column ();
22130 w->column_number_displayed = col;
22131 pint2str (decode_mode_spec_buf, width, col);
22132 return decode_mode_spec_buf;
22133 }
22134
22135 case 'e':
22136 #ifndef SYSTEM_MALLOC
22137 {
22138 if (NILP (Vmemory_full))
22139 return "";
22140 else
22141 return "!MEM FULL! ";
22142 }
22143 #else
22144 return "";
22145 #endif
22146
22147 case 'F':
22148 /* %F displays the frame name. */
22149 if (!NILP (f->title))
22150 return SSDATA (f->title);
22151 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22152 return SSDATA (f->name);
22153 return "Emacs";
22154
22155 case 'f':
22156 obj = BVAR (b, filename);
22157 break;
22158
22159 case 'i':
22160 {
22161 ptrdiff_t size = ZV - BEGV;
22162 pint2str (decode_mode_spec_buf, width, size);
22163 return decode_mode_spec_buf;
22164 }
22165
22166 case 'I':
22167 {
22168 ptrdiff_t size = ZV - BEGV;
22169 pint2hrstr (decode_mode_spec_buf, width, size);
22170 return decode_mode_spec_buf;
22171 }
22172
22173 case 'l':
22174 {
22175 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22176 ptrdiff_t topline, nlines, height;
22177 ptrdiff_t junk;
22178
22179 /* %c and %l are ignored in `frame-title-format'. */
22180 if (mode_line_target == MODE_LINE_TITLE)
22181 return "";
22182
22183 startpos = marker_position (w->start);
22184 startpos_byte = marker_byte_position (w->start);
22185 height = WINDOW_TOTAL_LINES (w);
22186
22187 /* If we decided that this buffer isn't suitable for line numbers,
22188 don't forget that too fast. */
22189 if (w->base_line_pos == -1)
22190 goto no_value;
22191
22192 /* If the buffer is very big, don't waste time. */
22193 if (INTEGERP (Vline_number_display_limit)
22194 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22195 {
22196 w->base_line_pos = 0;
22197 w->base_line_number = 0;
22198 goto no_value;
22199 }
22200
22201 if (w->base_line_number > 0
22202 && w->base_line_pos > 0
22203 && w->base_line_pos <= startpos)
22204 {
22205 line = w->base_line_number;
22206 linepos = w->base_line_pos;
22207 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22208 }
22209 else
22210 {
22211 line = 1;
22212 linepos = BUF_BEGV (b);
22213 linepos_byte = BUF_BEGV_BYTE (b);
22214 }
22215
22216 /* Count lines from base line to window start position. */
22217 nlines = display_count_lines (linepos_byte,
22218 startpos_byte,
22219 startpos, &junk);
22220
22221 topline = nlines + line;
22222
22223 /* Determine a new base line, if the old one is too close
22224 or too far away, or if we did not have one.
22225 "Too close" means it's plausible a scroll-down would
22226 go back past it. */
22227 if (startpos == BUF_BEGV (b))
22228 {
22229 w->base_line_number = topline;
22230 w->base_line_pos = BUF_BEGV (b);
22231 }
22232 else if (nlines < height + 25 || nlines > height * 3 + 50
22233 || linepos == BUF_BEGV (b))
22234 {
22235 ptrdiff_t limit = BUF_BEGV (b);
22236 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22237 ptrdiff_t position;
22238 ptrdiff_t distance =
22239 (height * 2 + 30) * line_number_display_limit_width;
22240
22241 if (startpos - distance > limit)
22242 {
22243 limit = startpos - distance;
22244 limit_byte = CHAR_TO_BYTE (limit);
22245 }
22246
22247 nlines = display_count_lines (startpos_byte,
22248 limit_byte,
22249 - (height * 2 + 30),
22250 &position);
22251 /* If we couldn't find the lines we wanted within
22252 line_number_display_limit_width chars per line,
22253 give up on line numbers for this window. */
22254 if (position == limit_byte && limit == startpos - distance)
22255 {
22256 w->base_line_pos = -1;
22257 w->base_line_number = 0;
22258 goto no_value;
22259 }
22260
22261 w->base_line_number = topline - nlines;
22262 w->base_line_pos = BYTE_TO_CHAR (position);
22263 }
22264
22265 /* Now count lines from the start pos to point. */
22266 nlines = display_count_lines (startpos_byte,
22267 PT_BYTE, PT, &junk);
22268
22269 /* Record that we did display the line number. */
22270 line_number_displayed = 1;
22271
22272 /* Make the string to show. */
22273 pint2str (decode_mode_spec_buf, width, topline + nlines);
22274 return decode_mode_spec_buf;
22275 no_value:
22276 {
22277 char* p = decode_mode_spec_buf;
22278 int pad = width - 2;
22279 while (pad-- > 0)
22280 *p++ = ' ';
22281 *p++ = '?';
22282 *p++ = '?';
22283 *p = '\0';
22284 return decode_mode_spec_buf;
22285 }
22286 }
22287 break;
22288
22289 case 'm':
22290 obj = BVAR (b, mode_name);
22291 break;
22292
22293 case 'n':
22294 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22295 return " Narrow";
22296 break;
22297
22298 case 'p':
22299 {
22300 ptrdiff_t pos = marker_position (w->start);
22301 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22302
22303 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22304 {
22305 if (pos <= BUF_BEGV (b))
22306 return "All";
22307 else
22308 return "Bottom";
22309 }
22310 else if (pos <= BUF_BEGV (b))
22311 return "Top";
22312 else
22313 {
22314 if (total > 1000000)
22315 /* Do it differently for a large value, to avoid overflow. */
22316 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22317 else
22318 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22319 /* We can't normally display a 3-digit number,
22320 so get us a 2-digit number that is close. */
22321 if (total == 100)
22322 total = 99;
22323 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22324 return decode_mode_spec_buf;
22325 }
22326 }
22327
22328 /* Display percentage of size above the bottom of the screen. */
22329 case 'P':
22330 {
22331 ptrdiff_t toppos = marker_position (w->start);
22332 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22333 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22334
22335 if (botpos >= BUF_ZV (b))
22336 {
22337 if (toppos <= BUF_BEGV (b))
22338 return "All";
22339 else
22340 return "Bottom";
22341 }
22342 else
22343 {
22344 if (total > 1000000)
22345 /* Do it differently for a large value, to avoid overflow. */
22346 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22347 else
22348 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22349 /* We can't normally display a 3-digit number,
22350 so get us a 2-digit number that is close. */
22351 if (total == 100)
22352 total = 99;
22353 if (toppos <= BUF_BEGV (b))
22354 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22355 else
22356 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22357 return decode_mode_spec_buf;
22358 }
22359 }
22360
22361 case 's':
22362 /* status of process */
22363 obj = Fget_buffer_process (Fcurrent_buffer ());
22364 if (NILP (obj))
22365 return "no process";
22366 #ifndef MSDOS
22367 obj = Fsymbol_name (Fprocess_status (obj));
22368 #endif
22369 break;
22370
22371 case '@':
22372 {
22373 ptrdiff_t count = inhibit_garbage_collection ();
22374 Lisp_Object val = call1 (intern ("file-remote-p"),
22375 BVAR (current_buffer, directory));
22376 unbind_to (count, Qnil);
22377
22378 if (NILP (val))
22379 return "-";
22380 else
22381 return "@";
22382 }
22383
22384 case 'z':
22385 /* coding-system (not including end-of-line format) */
22386 case 'Z':
22387 /* coding-system (including end-of-line type) */
22388 {
22389 int eol_flag = (c == 'Z');
22390 char *p = decode_mode_spec_buf;
22391
22392 if (! FRAME_WINDOW_P (f))
22393 {
22394 /* No need to mention EOL here--the terminal never needs
22395 to do EOL conversion. */
22396 p = decode_mode_spec_coding (CODING_ID_NAME
22397 (FRAME_KEYBOARD_CODING (f)->id),
22398 p, 0);
22399 p = decode_mode_spec_coding (CODING_ID_NAME
22400 (FRAME_TERMINAL_CODING (f)->id),
22401 p, 0);
22402 }
22403 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22404 p, eol_flag);
22405
22406 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22407 #ifdef subprocesses
22408 obj = Fget_buffer_process (Fcurrent_buffer ());
22409 if (PROCESSP (obj))
22410 {
22411 p = decode_mode_spec_coding
22412 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22413 p = decode_mode_spec_coding
22414 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22415 }
22416 #endif /* subprocesses */
22417 #endif /* 0 */
22418 *p = 0;
22419 return decode_mode_spec_buf;
22420 }
22421 }
22422
22423 if (STRINGP (obj))
22424 {
22425 *string = obj;
22426 return SSDATA (obj);
22427 }
22428 else
22429 return "";
22430 }
22431
22432
22433 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22434 means count lines back from START_BYTE. But don't go beyond
22435 LIMIT_BYTE. Return the number of lines thus found (always
22436 nonnegative).
22437
22438 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22439 either the position COUNT lines after/before START_BYTE, if we
22440 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22441 COUNT lines. */
22442
22443 static ptrdiff_t
22444 display_count_lines (ptrdiff_t start_byte,
22445 ptrdiff_t limit_byte, ptrdiff_t count,
22446 ptrdiff_t *byte_pos_ptr)
22447 {
22448 register unsigned char *cursor;
22449 unsigned char *base;
22450
22451 register ptrdiff_t ceiling;
22452 register unsigned char *ceiling_addr;
22453 ptrdiff_t orig_count = count;
22454
22455 /* If we are not in selective display mode,
22456 check only for newlines. */
22457 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22458 && !INTEGERP (BVAR (current_buffer, selective_display)));
22459
22460 if (count > 0)
22461 {
22462 while (start_byte < limit_byte)
22463 {
22464 ceiling = BUFFER_CEILING_OF (start_byte);
22465 ceiling = min (limit_byte - 1, ceiling);
22466 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22467 base = (cursor = BYTE_POS_ADDR (start_byte));
22468
22469 do
22470 {
22471 if (selective_display)
22472 {
22473 while (*cursor != '\n' && *cursor != 015
22474 && ++cursor != ceiling_addr)
22475 continue;
22476 if (cursor == ceiling_addr)
22477 break;
22478 }
22479 else
22480 {
22481 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22482 if (! cursor)
22483 break;
22484 }
22485
22486 cursor++;
22487
22488 if (--count == 0)
22489 {
22490 start_byte += cursor - base;
22491 *byte_pos_ptr = start_byte;
22492 return orig_count;
22493 }
22494 }
22495 while (cursor < ceiling_addr);
22496
22497 start_byte += ceiling_addr - base;
22498 }
22499 }
22500 else
22501 {
22502 while (start_byte > limit_byte)
22503 {
22504 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22505 ceiling = max (limit_byte, ceiling);
22506 ceiling_addr = BYTE_POS_ADDR (ceiling);
22507 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22508 while (1)
22509 {
22510 if (selective_display)
22511 {
22512 while (--cursor >= ceiling_addr
22513 && *cursor != '\n' && *cursor != 015)
22514 continue;
22515 if (cursor < ceiling_addr)
22516 break;
22517 }
22518 else
22519 {
22520 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22521 if (! cursor)
22522 break;
22523 }
22524
22525 if (++count == 0)
22526 {
22527 start_byte += cursor - base + 1;
22528 *byte_pos_ptr = start_byte;
22529 /* When scanning backwards, we should
22530 not count the newline posterior to which we stop. */
22531 return - orig_count - 1;
22532 }
22533 }
22534 start_byte += ceiling_addr - base;
22535 }
22536 }
22537
22538 *byte_pos_ptr = limit_byte;
22539
22540 if (count < 0)
22541 return - orig_count + count;
22542 return orig_count - count;
22543
22544 }
22545
22546
22547 \f
22548 /***********************************************************************
22549 Displaying strings
22550 ***********************************************************************/
22551
22552 /* Display a NUL-terminated string, starting with index START.
22553
22554 If STRING is non-null, display that C string. Otherwise, the Lisp
22555 string LISP_STRING is displayed. There's a case that STRING is
22556 non-null and LISP_STRING is not nil. It means STRING is a string
22557 data of LISP_STRING. In that case, we display LISP_STRING while
22558 ignoring its text properties.
22559
22560 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22561 FACE_STRING. Display STRING or LISP_STRING with the face at
22562 FACE_STRING_POS in FACE_STRING:
22563
22564 Display the string in the environment given by IT, but use the
22565 standard display table, temporarily.
22566
22567 FIELD_WIDTH is the minimum number of output glyphs to produce.
22568 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22569 with spaces. If STRING has more characters, more than FIELD_WIDTH
22570 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22571
22572 PRECISION is the maximum number of characters to output from
22573 STRING. PRECISION < 0 means don't truncate the string.
22574
22575 This is roughly equivalent to printf format specifiers:
22576
22577 FIELD_WIDTH PRECISION PRINTF
22578 ----------------------------------------
22579 -1 -1 %s
22580 -1 10 %.10s
22581 10 -1 %10s
22582 20 10 %20.10s
22583
22584 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22585 display them, and < 0 means obey the current buffer's value of
22586 enable_multibyte_characters.
22587
22588 Value is the number of columns displayed. */
22589
22590 static int
22591 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22592 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22593 int field_width, int precision, int max_x, int multibyte)
22594 {
22595 int hpos_at_start = it->hpos;
22596 int saved_face_id = it->face_id;
22597 struct glyph_row *row = it->glyph_row;
22598 ptrdiff_t it_charpos;
22599
22600 /* Initialize the iterator IT for iteration over STRING beginning
22601 with index START. */
22602 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22603 precision, field_width, multibyte);
22604 if (string && STRINGP (lisp_string))
22605 /* LISP_STRING is the one returned by decode_mode_spec. We should
22606 ignore its text properties. */
22607 it->stop_charpos = it->end_charpos;
22608
22609 /* If displaying STRING, set up the face of the iterator from
22610 FACE_STRING, if that's given. */
22611 if (STRINGP (face_string))
22612 {
22613 ptrdiff_t endptr;
22614 struct face *face;
22615
22616 it->face_id
22617 = face_at_string_position (it->w, face_string, face_string_pos,
22618 0, &endptr, it->base_face_id, 0);
22619 face = FACE_FROM_ID (it->f, it->face_id);
22620 it->face_box_p = face->box != FACE_NO_BOX;
22621 }
22622
22623 /* Set max_x to the maximum allowed X position. Don't let it go
22624 beyond the right edge of the window. */
22625 if (max_x <= 0)
22626 max_x = it->last_visible_x;
22627 else
22628 max_x = min (max_x, it->last_visible_x);
22629
22630 /* Skip over display elements that are not visible. because IT->w is
22631 hscrolled. */
22632 if (it->current_x < it->first_visible_x)
22633 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22634 MOVE_TO_POS | MOVE_TO_X);
22635
22636 row->ascent = it->max_ascent;
22637 row->height = it->max_ascent + it->max_descent;
22638 row->phys_ascent = it->max_phys_ascent;
22639 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22640 row->extra_line_spacing = it->max_extra_line_spacing;
22641
22642 if (STRINGP (it->string))
22643 it_charpos = IT_STRING_CHARPOS (*it);
22644 else
22645 it_charpos = IT_CHARPOS (*it);
22646
22647 /* This condition is for the case that we are called with current_x
22648 past last_visible_x. */
22649 while (it->current_x < max_x)
22650 {
22651 int x_before, x, n_glyphs_before, i, nglyphs;
22652
22653 /* Get the next display element. */
22654 if (!get_next_display_element (it))
22655 break;
22656
22657 /* Produce glyphs. */
22658 x_before = it->current_x;
22659 n_glyphs_before = row->used[TEXT_AREA];
22660 PRODUCE_GLYPHS (it);
22661
22662 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22663 i = 0;
22664 x = x_before;
22665 while (i < nglyphs)
22666 {
22667 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22668
22669 if (it->line_wrap != TRUNCATE
22670 && x + glyph->pixel_width > max_x)
22671 {
22672 /* End of continued line or max_x reached. */
22673 if (CHAR_GLYPH_PADDING_P (*glyph))
22674 {
22675 /* A wide character is unbreakable. */
22676 if (row->reversed_p)
22677 unproduce_glyphs (it, row->used[TEXT_AREA]
22678 - n_glyphs_before);
22679 row->used[TEXT_AREA] = n_glyphs_before;
22680 it->current_x = x_before;
22681 }
22682 else
22683 {
22684 if (row->reversed_p)
22685 unproduce_glyphs (it, row->used[TEXT_AREA]
22686 - (n_glyphs_before + i));
22687 row->used[TEXT_AREA] = n_glyphs_before + i;
22688 it->current_x = x;
22689 }
22690 break;
22691 }
22692 else if (x + glyph->pixel_width >= it->first_visible_x)
22693 {
22694 /* Glyph is at least partially visible. */
22695 ++it->hpos;
22696 if (x < it->first_visible_x)
22697 row->x = x - it->first_visible_x;
22698 }
22699 else
22700 {
22701 /* Glyph is off the left margin of the display area.
22702 Should not happen. */
22703 emacs_abort ();
22704 }
22705
22706 row->ascent = max (row->ascent, it->max_ascent);
22707 row->height = max (row->height, it->max_ascent + it->max_descent);
22708 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22709 row->phys_height = max (row->phys_height,
22710 it->max_phys_ascent + it->max_phys_descent);
22711 row->extra_line_spacing = max (row->extra_line_spacing,
22712 it->max_extra_line_spacing);
22713 x += glyph->pixel_width;
22714 ++i;
22715 }
22716
22717 /* Stop if max_x reached. */
22718 if (i < nglyphs)
22719 break;
22720
22721 /* Stop at line ends. */
22722 if (ITERATOR_AT_END_OF_LINE_P (it))
22723 {
22724 it->continuation_lines_width = 0;
22725 break;
22726 }
22727
22728 set_iterator_to_next (it, 1);
22729 if (STRINGP (it->string))
22730 it_charpos = IT_STRING_CHARPOS (*it);
22731 else
22732 it_charpos = IT_CHARPOS (*it);
22733
22734 /* Stop if truncating at the right edge. */
22735 if (it->line_wrap == TRUNCATE
22736 && it->current_x >= it->last_visible_x)
22737 {
22738 /* Add truncation mark, but don't do it if the line is
22739 truncated at a padding space. */
22740 if (it_charpos < it->string_nchars)
22741 {
22742 if (!FRAME_WINDOW_P (it->f))
22743 {
22744 int ii, n;
22745
22746 if (it->current_x > it->last_visible_x)
22747 {
22748 if (!row->reversed_p)
22749 {
22750 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22751 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22752 break;
22753 }
22754 else
22755 {
22756 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22757 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22758 break;
22759 unproduce_glyphs (it, ii + 1);
22760 ii = row->used[TEXT_AREA] - (ii + 1);
22761 }
22762 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22763 {
22764 row->used[TEXT_AREA] = ii;
22765 produce_special_glyphs (it, IT_TRUNCATION);
22766 }
22767 }
22768 produce_special_glyphs (it, IT_TRUNCATION);
22769 }
22770 row->truncated_on_right_p = 1;
22771 }
22772 break;
22773 }
22774 }
22775
22776 /* Maybe insert a truncation at the left. */
22777 if (it->first_visible_x
22778 && it_charpos > 0)
22779 {
22780 if (!FRAME_WINDOW_P (it->f)
22781 || (row->reversed_p
22782 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22783 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22784 insert_left_trunc_glyphs (it);
22785 row->truncated_on_left_p = 1;
22786 }
22787
22788 it->face_id = saved_face_id;
22789
22790 /* Value is number of columns displayed. */
22791 return it->hpos - hpos_at_start;
22792 }
22793
22794
22795 \f
22796 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22797 appears as an element of LIST or as the car of an element of LIST.
22798 If PROPVAL is a list, compare each element against LIST in that
22799 way, and return 1/2 if any element of PROPVAL is found in LIST.
22800 Otherwise return 0. This function cannot quit.
22801 The return value is 2 if the text is invisible but with an ellipsis
22802 and 1 if it's invisible and without an ellipsis. */
22803
22804 int
22805 invisible_p (register Lisp_Object propval, Lisp_Object list)
22806 {
22807 register Lisp_Object tail, proptail;
22808
22809 for (tail = list; CONSP (tail); tail = XCDR (tail))
22810 {
22811 register Lisp_Object tem;
22812 tem = XCAR (tail);
22813 if (EQ (propval, tem))
22814 return 1;
22815 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22816 return NILP (XCDR (tem)) ? 1 : 2;
22817 }
22818
22819 if (CONSP (propval))
22820 {
22821 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22822 {
22823 Lisp_Object propelt;
22824 propelt = XCAR (proptail);
22825 for (tail = list; CONSP (tail); tail = XCDR (tail))
22826 {
22827 register Lisp_Object tem;
22828 tem = XCAR (tail);
22829 if (EQ (propelt, tem))
22830 return 1;
22831 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22832 return NILP (XCDR (tem)) ? 1 : 2;
22833 }
22834 }
22835 }
22836
22837 return 0;
22838 }
22839
22840 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22841 doc: /* Non-nil if the property makes the text invisible.
22842 POS-OR-PROP can be a marker or number, in which case it is taken to be
22843 a position in the current buffer and the value of the `invisible' property
22844 is checked; or it can be some other value, which is then presumed to be the
22845 value of the `invisible' property of the text of interest.
22846 The non-nil value returned can be t for truly invisible text or something
22847 else if the text is replaced by an ellipsis. */)
22848 (Lisp_Object pos_or_prop)
22849 {
22850 Lisp_Object prop
22851 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22852 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22853 : pos_or_prop);
22854 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22855 return (invis == 0 ? Qnil
22856 : invis == 1 ? Qt
22857 : make_number (invis));
22858 }
22859
22860 /* Calculate a width or height in pixels from a specification using
22861 the following elements:
22862
22863 SPEC ::=
22864 NUM - a (fractional) multiple of the default font width/height
22865 (NUM) - specifies exactly NUM pixels
22866 UNIT - a fixed number of pixels, see below.
22867 ELEMENT - size of a display element in pixels, see below.
22868 (NUM . SPEC) - equals NUM * SPEC
22869 (+ SPEC SPEC ...) - add pixel values
22870 (- SPEC SPEC ...) - subtract pixel values
22871 (- SPEC) - negate pixel value
22872
22873 NUM ::=
22874 INT or FLOAT - a number constant
22875 SYMBOL - use symbol's (buffer local) variable binding.
22876
22877 UNIT ::=
22878 in - pixels per inch *)
22879 mm - pixels per 1/1000 meter *)
22880 cm - pixels per 1/100 meter *)
22881 width - width of current font in pixels.
22882 height - height of current font in pixels.
22883
22884 *) using the ratio(s) defined in display-pixels-per-inch.
22885
22886 ELEMENT ::=
22887
22888 left-fringe - left fringe width in pixels
22889 right-fringe - right fringe width in pixels
22890
22891 left-margin - left margin width in pixels
22892 right-margin - right margin width in pixels
22893
22894 scroll-bar - scroll-bar area width in pixels
22895
22896 Examples:
22897
22898 Pixels corresponding to 5 inches:
22899 (5 . in)
22900
22901 Total width of non-text areas on left side of window (if scroll-bar is on left):
22902 '(space :width (+ left-fringe left-margin scroll-bar))
22903
22904 Align to first text column (in header line):
22905 '(space :align-to 0)
22906
22907 Align to middle of text area minus half the width of variable `my-image'
22908 containing a loaded image:
22909 '(space :align-to (0.5 . (- text my-image)))
22910
22911 Width of left margin minus width of 1 character in the default font:
22912 '(space :width (- left-margin 1))
22913
22914 Width of left margin minus width of 2 characters in the current font:
22915 '(space :width (- left-margin (2 . width)))
22916
22917 Center 1 character over left-margin (in header line):
22918 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22919
22920 Different ways to express width of left fringe plus left margin minus one pixel:
22921 '(space :width (- (+ left-fringe left-margin) (1)))
22922 '(space :width (+ left-fringe left-margin (- (1))))
22923 '(space :width (+ left-fringe left-margin (-1)))
22924
22925 */
22926
22927 static int
22928 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22929 struct font *font, int width_p, int *align_to)
22930 {
22931 double pixels;
22932
22933 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22934 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22935
22936 if (NILP (prop))
22937 return OK_PIXELS (0);
22938
22939 eassert (FRAME_LIVE_P (it->f));
22940
22941 if (SYMBOLP (prop))
22942 {
22943 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22944 {
22945 char *unit = SSDATA (SYMBOL_NAME (prop));
22946
22947 if (unit[0] == 'i' && unit[1] == 'n')
22948 pixels = 1.0;
22949 else if (unit[0] == 'm' && unit[1] == 'm')
22950 pixels = 25.4;
22951 else if (unit[0] == 'c' && unit[1] == 'm')
22952 pixels = 2.54;
22953 else
22954 pixels = 0;
22955 if (pixels > 0)
22956 {
22957 double ppi = (width_p ? FRAME_RES_X (it->f)
22958 : FRAME_RES_Y (it->f));
22959
22960 if (ppi > 0)
22961 return OK_PIXELS (ppi / pixels);
22962 return 0;
22963 }
22964 }
22965
22966 #ifdef HAVE_WINDOW_SYSTEM
22967 if (EQ (prop, Qheight))
22968 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22969 if (EQ (prop, Qwidth))
22970 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22971 #else
22972 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22973 return OK_PIXELS (1);
22974 #endif
22975
22976 if (EQ (prop, Qtext))
22977 return OK_PIXELS (width_p
22978 ? window_box_width (it->w, TEXT_AREA)
22979 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22980
22981 if (align_to && *align_to < 0)
22982 {
22983 *res = 0;
22984 if (EQ (prop, Qleft))
22985 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22986 if (EQ (prop, Qright))
22987 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22988 if (EQ (prop, Qcenter))
22989 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22990 + window_box_width (it->w, TEXT_AREA) / 2);
22991 if (EQ (prop, Qleft_fringe))
22992 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22993 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22994 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22995 if (EQ (prop, Qright_fringe))
22996 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22997 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22998 : window_box_right_offset (it->w, TEXT_AREA));
22999 if (EQ (prop, Qleft_margin))
23000 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23001 if (EQ (prop, Qright_margin))
23002 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23003 if (EQ (prop, Qscroll_bar))
23004 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23005 ? 0
23006 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23007 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23008 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23009 : 0)));
23010 }
23011 else
23012 {
23013 if (EQ (prop, Qleft_fringe))
23014 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23015 if (EQ (prop, Qright_fringe))
23016 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23017 if (EQ (prop, Qleft_margin))
23018 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23019 if (EQ (prop, Qright_margin))
23020 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23021 if (EQ (prop, Qscroll_bar))
23022 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23023 }
23024
23025 prop = buffer_local_value_1 (prop, it->w->contents);
23026 if (EQ (prop, Qunbound))
23027 prop = Qnil;
23028 }
23029
23030 if (INTEGERP (prop) || FLOATP (prop))
23031 {
23032 int base_unit = (width_p
23033 ? FRAME_COLUMN_WIDTH (it->f)
23034 : FRAME_LINE_HEIGHT (it->f));
23035 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23036 }
23037
23038 if (CONSP (prop))
23039 {
23040 Lisp_Object car = XCAR (prop);
23041 Lisp_Object cdr = XCDR (prop);
23042
23043 if (SYMBOLP (car))
23044 {
23045 #ifdef HAVE_WINDOW_SYSTEM
23046 if (FRAME_WINDOW_P (it->f)
23047 && valid_image_p (prop))
23048 {
23049 ptrdiff_t id = lookup_image (it->f, prop);
23050 struct image *img = IMAGE_FROM_ID (it->f, id);
23051
23052 return OK_PIXELS (width_p ? img->width : img->height);
23053 }
23054 #endif
23055 if (EQ (car, Qplus) || EQ (car, Qminus))
23056 {
23057 int first = 1;
23058 double px;
23059
23060 pixels = 0;
23061 while (CONSP (cdr))
23062 {
23063 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23064 font, width_p, align_to))
23065 return 0;
23066 if (first)
23067 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23068 else
23069 pixels += px;
23070 cdr = XCDR (cdr);
23071 }
23072 if (EQ (car, Qminus))
23073 pixels = -pixels;
23074 return OK_PIXELS (pixels);
23075 }
23076
23077 car = buffer_local_value_1 (car, it->w->contents);
23078 if (EQ (car, Qunbound))
23079 car = Qnil;
23080 }
23081
23082 if (INTEGERP (car) || FLOATP (car))
23083 {
23084 double fact;
23085 pixels = XFLOATINT (car);
23086 if (NILP (cdr))
23087 return OK_PIXELS (pixels);
23088 if (calc_pixel_width_or_height (&fact, it, cdr,
23089 font, width_p, align_to))
23090 return OK_PIXELS (pixels * fact);
23091 return 0;
23092 }
23093
23094 return 0;
23095 }
23096
23097 return 0;
23098 }
23099
23100 \f
23101 /***********************************************************************
23102 Glyph Display
23103 ***********************************************************************/
23104
23105 #ifdef HAVE_WINDOW_SYSTEM
23106
23107 #ifdef GLYPH_DEBUG
23108
23109 void
23110 dump_glyph_string (struct glyph_string *s)
23111 {
23112 fprintf (stderr, "glyph string\n");
23113 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23114 s->x, s->y, s->width, s->height);
23115 fprintf (stderr, " ybase = %d\n", s->ybase);
23116 fprintf (stderr, " hl = %d\n", s->hl);
23117 fprintf (stderr, " left overhang = %d, right = %d\n",
23118 s->left_overhang, s->right_overhang);
23119 fprintf (stderr, " nchars = %d\n", s->nchars);
23120 fprintf (stderr, " extends to end of line = %d\n",
23121 s->extends_to_end_of_line_p);
23122 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23123 fprintf (stderr, " bg width = %d\n", s->background_width);
23124 }
23125
23126 #endif /* GLYPH_DEBUG */
23127
23128 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23129 of XChar2b structures for S; it can't be allocated in
23130 init_glyph_string because it must be allocated via `alloca'. W
23131 is the window on which S is drawn. ROW and AREA are the glyph row
23132 and area within the row from which S is constructed. START is the
23133 index of the first glyph structure covered by S. HL is a
23134 face-override for drawing S. */
23135
23136 #ifdef HAVE_NTGUI
23137 #define OPTIONAL_HDC(hdc) HDC hdc,
23138 #define DECLARE_HDC(hdc) HDC hdc;
23139 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23140 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23141 #endif
23142
23143 #ifndef OPTIONAL_HDC
23144 #define OPTIONAL_HDC(hdc)
23145 #define DECLARE_HDC(hdc)
23146 #define ALLOCATE_HDC(hdc, f)
23147 #define RELEASE_HDC(hdc, f)
23148 #endif
23149
23150 static void
23151 init_glyph_string (struct glyph_string *s,
23152 OPTIONAL_HDC (hdc)
23153 XChar2b *char2b, struct window *w, struct glyph_row *row,
23154 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23155 {
23156 memset (s, 0, sizeof *s);
23157 s->w = w;
23158 s->f = XFRAME (w->frame);
23159 #ifdef HAVE_NTGUI
23160 s->hdc = hdc;
23161 #endif
23162 s->display = FRAME_X_DISPLAY (s->f);
23163 s->window = FRAME_X_WINDOW (s->f);
23164 s->char2b = char2b;
23165 s->hl = hl;
23166 s->row = row;
23167 s->area = area;
23168 s->first_glyph = row->glyphs[area] + start;
23169 s->height = row->height;
23170 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23171 s->ybase = s->y + row->ascent;
23172 }
23173
23174
23175 /* Append the list of glyph strings with head H and tail T to the list
23176 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23177
23178 static void
23179 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23180 struct glyph_string *h, struct glyph_string *t)
23181 {
23182 if (h)
23183 {
23184 if (*head)
23185 (*tail)->next = h;
23186 else
23187 *head = h;
23188 h->prev = *tail;
23189 *tail = t;
23190 }
23191 }
23192
23193
23194 /* Prepend the list of glyph strings with head H and tail T to the
23195 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23196 result. */
23197
23198 static void
23199 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23200 struct glyph_string *h, struct glyph_string *t)
23201 {
23202 if (h)
23203 {
23204 if (*head)
23205 (*head)->prev = t;
23206 else
23207 *tail = t;
23208 t->next = *head;
23209 *head = h;
23210 }
23211 }
23212
23213
23214 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23215 Set *HEAD and *TAIL to the resulting list. */
23216
23217 static void
23218 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23219 struct glyph_string *s)
23220 {
23221 s->next = s->prev = NULL;
23222 append_glyph_string_lists (head, tail, s, s);
23223 }
23224
23225
23226 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23227 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23228 make sure that X resources for the face returned are allocated.
23229 Value is a pointer to a realized face that is ready for display if
23230 DISPLAY_P is non-zero. */
23231
23232 static struct face *
23233 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23234 XChar2b *char2b, int display_p)
23235 {
23236 struct face *face = FACE_FROM_ID (f, face_id);
23237 unsigned code = 0;
23238
23239 if (face->font)
23240 {
23241 code = face->font->driver->encode_char (face->font, c);
23242
23243 if (code == FONT_INVALID_CODE)
23244 code = 0;
23245 }
23246 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23247
23248 /* Make sure X resources of the face are allocated. */
23249 #ifdef HAVE_X_WINDOWS
23250 if (display_p)
23251 #endif
23252 {
23253 eassert (face != NULL);
23254 PREPARE_FACE_FOR_DISPLAY (f, face);
23255 }
23256
23257 return face;
23258 }
23259
23260
23261 /* Get face and two-byte form of character glyph GLYPH on frame F.
23262 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23263 a pointer to a realized face that is ready for display. */
23264
23265 static struct face *
23266 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23267 XChar2b *char2b, int *two_byte_p)
23268 {
23269 struct face *face;
23270 unsigned code = 0;
23271
23272 eassert (glyph->type == CHAR_GLYPH);
23273 face = FACE_FROM_ID (f, glyph->face_id);
23274
23275 /* Make sure X resources of the face are allocated. */
23276 eassert (face != NULL);
23277 PREPARE_FACE_FOR_DISPLAY (f, face);
23278
23279 if (two_byte_p)
23280 *two_byte_p = 0;
23281
23282 if (face->font)
23283 {
23284 if (CHAR_BYTE8_P (glyph->u.ch))
23285 code = CHAR_TO_BYTE8 (glyph->u.ch);
23286 else
23287 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23288
23289 if (code == FONT_INVALID_CODE)
23290 code = 0;
23291 }
23292
23293 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23294 return face;
23295 }
23296
23297
23298 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23299 Return 1 if FONT has a glyph for C, otherwise return 0. */
23300
23301 static int
23302 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23303 {
23304 unsigned code;
23305
23306 if (CHAR_BYTE8_P (c))
23307 code = CHAR_TO_BYTE8 (c);
23308 else
23309 code = font->driver->encode_char (font, c);
23310
23311 if (code == FONT_INVALID_CODE)
23312 return 0;
23313 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23314 return 1;
23315 }
23316
23317
23318 /* Fill glyph string S with composition components specified by S->cmp.
23319
23320 BASE_FACE is the base face of the composition.
23321 S->cmp_from is the index of the first component for S.
23322
23323 OVERLAPS non-zero means S should draw the foreground only, and use
23324 its physical height for clipping. See also draw_glyphs.
23325
23326 Value is the index of a component not in S. */
23327
23328 static int
23329 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23330 int overlaps)
23331 {
23332 int i;
23333 /* For all glyphs of this composition, starting at the offset
23334 S->cmp_from, until we reach the end of the definition or encounter a
23335 glyph that requires the different face, add it to S. */
23336 struct face *face;
23337
23338 eassert (s);
23339
23340 s->for_overlaps = overlaps;
23341 s->face = NULL;
23342 s->font = NULL;
23343 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23344 {
23345 int c = COMPOSITION_GLYPH (s->cmp, i);
23346
23347 /* TAB in a composition means display glyphs with padding space
23348 on the left or right. */
23349 if (c != '\t')
23350 {
23351 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23352 -1, Qnil);
23353
23354 face = get_char_face_and_encoding (s->f, c, face_id,
23355 s->char2b + i, 1);
23356 if (face)
23357 {
23358 if (! s->face)
23359 {
23360 s->face = face;
23361 s->font = s->face->font;
23362 }
23363 else if (s->face != face)
23364 break;
23365 }
23366 }
23367 ++s->nchars;
23368 }
23369 s->cmp_to = i;
23370
23371 if (s->face == NULL)
23372 {
23373 s->face = base_face->ascii_face;
23374 s->font = s->face->font;
23375 }
23376
23377 /* All glyph strings for the same composition has the same width,
23378 i.e. the width set for the first component of the composition. */
23379 s->width = s->first_glyph->pixel_width;
23380
23381 /* If the specified font could not be loaded, use the frame's
23382 default font, but record the fact that we couldn't load it in
23383 the glyph string so that we can draw rectangles for the
23384 characters of the glyph string. */
23385 if (s->font == NULL)
23386 {
23387 s->font_not_found_p = 1;
23388 s->font = FRAME_FONT (s->f);
23389 }
23390
23391 /* Adjust base line for subscript/superscript text. */
23392 s->ybase += s->first_glyph->voffset;
23393
23394 /* This glyph string must always be drawn with 16-bit functions. */
23395 s->two_byte_p = 1;
23396
23397 return s->cmp_to;
23398 }
23399
23400 static int
23401 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23402 int start, int end, int overlaps)
23403 {
23404 struct glyph *glyph, *last;
23405 Lisp_Object lgstring;
23406 int i;
23407
23408 s->for_overlaps = overlaps;
23409 glyph = s->row->glyphs[s->area] + start;
23410 last = s->row->glyphs[s->area] + end;
23411 s->cmp_id = glyph->u.cmp.id;
23412 s->cmp_from = glyph->slice.cmp.from;
23413 s->cmp_to = glyph->slice.cmp.to + 1;
23414 s->face = FACE_FROM_ID (s->f, face_id);
23415 lgstring = composition_gstring_from_id (s->cmp_id);
23416 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23417 glyph++;
23418 while (glyph < last
23419 && glyph->u.cmp.automatic
23420 && glyph->u.cmp.id == s->cmp_id
23421 && s->cmp_to == glyph->slice.cmp.from)
23422 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23423
23424 for (i = s->cmp_from; i < s->cmp_to; i++)
23425 {
23426 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23427 unsigned code = LGLYPH_CODE (lglyph);
23428
23429 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23430 }
23431 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23432 return glyph - s->row->glyphs[s->area];
23433 }
23434
23435
23436 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23437 See the comment of fill_glyph_string for arguments.
23438 Value is the index of the first glyph not in S. */
23439
23440
23441 static int
23442 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23443 int start, int end, int overlaps)
23444 {
23445 struct glyph *glyph, *last;
23446 int voffset;
23447
23448 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23449 s->for_overlaps = overlaps;
23450 glyph = s->row->glyphs[s->area] + start;
23451 last = s->row->glyphs[s->area] + end;
23452 voffset = glyph->voffset;
23453 s->face = FACE_FROM_ID (s->f, face_id);
23454 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23455 s->nchars = 1;
23456 s->width = glyph->pixel_width;
23457 glyph++;
23458 while (glyph < last
23459 && glyph->type == GLYPHLESS_GLYPH
23460 && glyph->voffset == voffset
23461 && glyph->face_id == face_id)
23462 {
23463 s->nchars++;
23464 s->width += glyph->pixel_width;
23465 glyph++;
23466 }
23467 s->ybase += voffset;
23468 return glyph - s->row->glyphs[s->area];
23469 }
23470
23471
23472 /* Fill glyph string S from a sequence of character glyphs.
23473
23474 FACE_ID is the face id of the string. START is the index of the
23475 first glyph to consider, END is the index of the last + 1.
23476 OVERLAPS non-zero means S should draw the foreground only, and use
23477 its physical height for clipping. See also draw_glyphs.
23478
23479 Value is the index of the first glyph not in S. */
23480
23481 static int
23482 fill_glyph_string (struct glyph_string *s, int face_id,
23483 int start, int end, int overlaps)
23484 {
23485 struct glyph *glyph, *last;
23486 int voffset;
23487 int glyph_not_available_p;
23488
23489 eassert (s->f == XFRAME (s->w->frame));
23490 eassert (s->nchars == 0);
23491 eassert (start >= 0 && end > start);
23492
23493 s->for_overlaps = overlaps;
23494 glyph = s->row->glyphs[s->area] + start;
23495 last = s->row->glyphs[s->area] + end;
23496 voffset = glyph->voffset;
23497 s->padding_p = glyph->padding_p;
23498 glyph_not_available_p = glyph->glyph_not_available_p;
23499
23500 while (glyph < last
23501 && glyph->type == CHAR_GLYPH
23502 && glyph->voffset == voffset
23503 /* Same face id implies same font, nowadays. */
23504 && glyph->face_id == face_id
23505 && glyph->glyph_not_available_p == glyph_not_available_p)
23506 {
23507 int two_byte_p;
23508
23509 s->face = get_glyph_face_and_encoding (s->f, glyph,
23510 s->char2b + s->nchars,
23511 &two_byte_p);
23512 s->two_byte_p = two_byte_p;
23513 ++s->nchars;
23514 eassert (s->nchars <= end - start);
23515 s->width += glyph->pixel_width;
23516 if (glyph++->padding_p != s->padding_p)
23517 break;
23518 }
23519
23520 s->font = s->face->font;
23521
23522 /* If the specified font could not be loaded, use the frame's font,
23523 but record the fact that we couldn't load it in
23524 S->font_not_found_p so that we can draw rectangles for the
23525 characters of the glyph string. */
23526 if (s->font == NULL || glyph_not_available_p)
23527 {
23528 s->font_not_found_p = 1;
23529 s->font = FRAME_FONT (s->f);
23530 }
23531
23532 /* Adjust base line for subscript/superscript text. */
23533 s->ybase += voffset;
23534
23535 eassert (s->face && s->face->gc);
23536 return glyph - s->row->glyphs[s->area];
23537 }
23538
23539
23540 /* Fill glyph string S from image glyph S->first_glyph. */
23541
23542 static void
23543 fill_image_glyph_string (struct glyph_string *s)
23544 {
23545 eassert (s->first_glyph->type == IMAGE_GLYPH);
23546 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23547 eassert (s->img);
23548 s->slice = s->first_glyph->slice.img;
23549 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23550 s->font = s->face->font;
23551 s->width = s->first_glyph->pixel_width;
23552
23553 /* Adjust base line for subscript/superscript text. */
23554 s->ybase += s->first_glyph->voffset;
23555 }
23556
23557
23558 /* Fill glyph string S from a sequence of stretch glyphs.
23559
23560 START is the index of the first glyph to consider,
23561 END is the index of the last + 1.
23562
23563 Value is the index of the first glyph not in S. */
23564
23565 static int
23566 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23567 {
23568 struct glyph *glyph, *last;
23569 int voffset, face_id;
23570
23571 eassert (s->first_glyph->type == STRETCH_GLYPH);
23572
23573 glyph = s->row->glyphs[s->area] + start;
23574 last = s->row->glyphs[s->area] + end;
23575 face_id = glyph->face_id;
23576 s->face = FACE_FROM_ID (s->f, face_id);
23577 s->font = s->face->font;
23578 s->width = glyph->pixel_width;
23579 s->nchars = 1;
23580 voffset = glyph->voffset;
23581
23582 for (++glyph;
23583 (glyph < last
23584 && glyph->type == STRETCH_GLYPH
23585 && glyph->voffset == voffset
23586 && glyph->face_id == face_id);
23587 ++glyph)
23588 s->width += glyph->pixel_width;
23589
23590 /* Adjust base line for subscript/superscript text. */
23591 s->ybase += voffset;
23592
23593 /* The case that face->gc == 0 is handled when drawing the glyph
23594 string by calling PREPARE_FACE_FOR_DISPLAY. */
23595 eassert (s->face);
23596 return glyph - s->row->glyphs[s->area];
23597 }
23598
23599 static struct font_metrics *
23600 get_per_char_metric (struct font *font, XChar2b *char2b)
23601 {
23602 static struct font_metrics metrics;
23603 unsigned code;
23604
23605 if (! font)
23606 return NULL;
23607 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23608 if (code == FONT_INVALID_CODE)
23609 return NULL;
23610 font->driver->text_extents (font, &code, 1, &metrics);
23611 return &metrics;
23612 }
23613
23614 /* EXPORT for RIF:
23615 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23616 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23617 assumed to be zero. */
23618
23619 void
23620 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23621 {
23622 *left = *right = 0;
23623
23624 if (glyph->type == CHAR_GLYPH)
23625 {
23626 struct face *face;
23627 XChar2b char2b;
23628 struct font_metrics *pcm;
23629
23630 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23631 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23632 {
23633 if (pcm->rbearing > pcm->width)
23634 *right = pcm->rbearing - pcm->width;
23635 if (pcm->lbearing < 0)
23636 *left = -pcm->lbearing;
23637 }
23638 }
23639 else if (glyph->type == COMPOSITE_GLYPH)
23640 {
23641 if (! glyph->u.cmp.automatic)
23642 {
23643 struct composition *cmp = composition_table[glyph->u.cmp.id];
23644
23645 if (cmp->rbearing > cmp->pixel_width)
23646 *right = cmp->rbearing - cmp->pixel_width;
23647 if (cmp->lbearing < 0)
23648 *left = - cmp->lbearing;
23649 }
23650 else
23651 {
23652 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23653 struct font_metrics metrics;
23654
23655 composition_gstring_width (gstring, glyph->slice.cmp.from,
23656 glyph->slice.cmp.to + 1, &metrics);
23657 if (metrics.rbearing > metrics.width)
23658 *right = metrics.rbearing - metrics.width;
23659 if (metrics.lbearing < 0)
23660 *left = - metrics.lbearing;
23661 }
23662 }
23663 }
23664
23665
23666 /* Return the index of the first glyph preceding glyph string S that
23667 is overwritten by S because of S's left overhang. Value is -1
23668 if no glyphs are overwritten. */
23669
23670 static int
23671 left_overwritten (struct glyph_string *s)
23672 {
23673 int k;
23674
23675 if (s->left_overhang)
23676 {
23677 int x = 0, i;
23678 struct glyph *glyphs = s->row->glyphs[s->area];
23679 int first = s->first_glyph - glyphs;
23680
23681 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23682 x -= glyphs[i].pixel_width;
23683
23684 k = i + 1;
23685 }
23686 else
23687 k = -1;
23688
23689 return k;
23690 }
23691
23692
23693 /* Return the index of the first glyph preceding glyph string S that
23694 is overwriting S because of its right overhang. Value is -1 if no
23695 glyph in front of S overwrites S. */
23696
23697 static int
23698 left_overwriting (struct glyph_string *s)
23699 {
23700 int i, k, x;
23701 struct glyph *glyphs = s->row->glyphs[s->area];
23702 int first = s->first_glyph - glyphs;
23703
23704 k = -1;
23705 x = 0;
23706 for (i = first - 1; i >= 0; --i)
23707 {
23708 int left, right;
23709 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23710 if (x + right > 0)
23711 k = i;
23712 x -= glyphs[i].pixel_width;
23713 }
23714
23715 return k;
23716 }
23717
23718
23719 /* Return the index of the last glyph following glyph string S that is
23720 overwritten by S because of S's right overhang. Value is -1 if
23721 no such glyph is found. */
23722
23723 static int
23724 right_overwritten (struct glyph_string *s)
23725 {
23726 int k = -1;
23727
23728 if (s->right_overhang)
23729 {
23730 int x = 0, i;
23731 struct glyph *glyphs = s->row->glyphs[s->area];
23732 int first = (s->first_glyph - glyphs
23733 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23734 int end = s->row->used[s->area];
23735
23736 for (i = first; i < end && s->right_overhang > x; ++i)
23737 x += glyphs[i].pixel_width;
23738
23739 k = i;
23740 }
23741
23742 return k;
23743 }
23744
23745
23746 /* Return the index of the last glyph following glyph string S that
23747 overwrites S because of its left overhang. Value is negative
23748 if no such glyph is found. */
23749
23750 static int
23751 right_overwriting (struct glyph_string *s)
23752 {
23753 int i, k, x;
23754 int end = s->row->used[s->area];
23755 struct glyph *glyphs = s->row->glyphs[s->area];
23756 int first = (s->first_glyph - glyphs
23757 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23758
23759 k = -1;
23760 x = 0;
23761 for (i = first; i < end; ++i)
23762 {
23763 int left, right;
23764 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23765 if (x - left < 0)
23766 k = i;
23767 x += glyphs[i].pixel_width;
23768 }
23769
23770 return k;
23771 }
23772
23773
23774 /* Set background width of glyph string S. START is the index of the
23775 first glyph following S. LAST_X is the right-most x-position + 1
23776 in the drawing area. */
23777
23778 static void
23779 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23780 {
23781 /* If the face of this glyph string has to be drawn to the end of
23782 the drawing area, set S->extends_to_end_of_line_p. */
23783
23784 if (start == s->row->used[s->area]
23785 && s->area == TEXT_AREA
23786 && ((s->row->fill_line_p
23787 && (s->hl == DRAW_NORMAL_TEXT
23788 || s->hl == DRAW_IMAGE_RAISED
23789 || s->hl == DRAW_IMAGE_SUNKEN))
23790 || s->hl == DRAW_MOUSE_FACE))
23791 s->extends_to_end_of_line_p = 1;
23792
23793 /* If S extends its face to the end of the line, set its
23794 background_width to the distance to the right edge of the drawing
23795 area. */
23796 if (s->extends_to_end_of_line_p)
23797 s->background_width = last_x - s->x + 1;
23798 else
23799 s->background_width = s->width;
23800 }
23801
23802
23803 /* Compute overhangs and x-positions for glyph string S and its
23804 predecessors, or successors. X is the starting x-position for S.
23805 BACKWARD_P non-zero means process predecessors. */
23806
23807 static void
23808 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23809 {
23810 if (backward_p)
23811 {
23812 while (s)
23813 {
23814 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23815 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23816 x -= s->width;
23817 s->x = x;
23818 s = s->prev;
23819 }
23820 }
23821 else
23822 {
23823 while (s)
23824 {
23825 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23826 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23827 s->x = x;
23828 x += s->width;
23829 s = s->next;
23830 }
23831 }
23832 }
23833
23834
23835
23836 /* The following macros are only called from draw_glyphs below.
23837 They reference the following parameters of that function directly:
23838 `w', `row', `area', and `overlap_p'
23839 as well as the following local variables:
23840 `s', `f', and `hdc' (in W32) */
23841
23842 #ifdef HAVE_NTGUI
23843 /* On W32, silently add local `hdc' variable to argument list of
23844 init_glyph_string. */
23845 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23846 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23847 #else
23848 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23849 init_glyph_string (s, char2b, w, row, area, start, hl)
23850 #endif
23851
23852 /* Add a glyph string for a stretch glyph to the list of strings
23853 between HEAD and TAIL. START is the index of the stretch glyph in
23854 row area AREA of glyph row ROW. END is the index of the last glyph
23855 in that glyph row area. X is the current output position assigned
23856 to the new glyph string constructed. HL overrides that face of the
23857 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23858 is the right-most x-position of the drawing area. */
23859
23860 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23861 and below -- keep them on one line. */
23862 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23863 do \
23864 { \
23865 s = alloca (sizeof *s); \
23866 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23867 START = fill_stretch_glyph_string (s, START, END); \
23868 append_glyph_string (&HEAD, &TAIL, s); \
23869 s->x = (X); \
23870 } \
23871 while (0)
23872
23873
23874 /* Add a glyph string for an image glyph to the list of strings
23875 between HEAD and TAIL. START is the index of the image glyph in
23876 row area AREA of glyph row ROW. END is the index of the last glyph
23877 in that glyph row area. X is the current output position assigned
23878 to the new glyph string constructed. HL overrides that face of the
23879 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23880 is the right-most x-position of the drawing area. */
23881
23882 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23883 do \
23884 { \
23885 s = alloca (sizeof *s); \
23886 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23887 fill_image_glyph_string (s); \
23888 append_glyph_string (&HEAD, &TAIL, s); \
23889 ++START; \
23890 s->x = (X); \
23891 } \
23892 while (0)
23893
23894
23895 /* Add a glyph string for a sequence of character glyphs to the list
23896 of strings between HEAD and TAIL. START is the index of the first
23897 glyph in row area AREA of glyph row ROW that is part of the new
23898 glyph string. END is the index of the last glyph in that glyph row
23899 area. X is the current output position assigned to the new glyph
23900 string constructed. HL overrides that face of the glyph; e.g. it
23901 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23902 right-most x-position of the drawing area. */
23903
23904 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23905 do \
23906 { \
23907 int face_id; \
23908 XChar2b *char2b; \
23909 \
23910 face_id = (row)->glyphs[area][START].face_id; \
23911 \
23912 s = alloca (sizeof *s); \
23913 char2b = alloca ((END - START) * sizeof *char2b); \
23914 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23915 append_glyph_string (&HEAD, &TAIL, s); \
23916 s->x = (X); \
23917 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23918 } \
23919 while (0)
23920
23921
23922 /* Add a glyph string for a composite sequence to the list of strings
23923 between HEAD and TAIL. START is the index of the first glyph in
23924 row area AREA of glyph row ROW that is part of the new glyph
23925 string. END is the index of the last glyph in that glyph row area.
23926 X is the current output position assigned to the new glyph string
23927 constructed. HL overrides that face of the glyph; e.g. it is
23928 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23929 x-position of the drawing area. */
23930
23931 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23932 do { \
23933 int face_id = (row)->glyphs[area][START].face_id; \
23934 struct face *base_face = FACE_FROM_ID (f, face_id); \
23935 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23936 struct composition *cmp = composition_table[cmp_id]; \
23937 XChar2b *char2b; \
23938 struct glyph_string *first_s = NULL; \
23939 int n; \
23940 \
23941 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23942 \
23943 /* Make glyph_strings for each glyph sequence that is drawable by \
23944 the same face, and append them to HEAD/TAIL. */ \
23945 for (n = 0; n < cmp->glyph_len;) \
23946 { \
23947 s = alloca (sizeof *s); \
23948 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23949 append_glyph_string (&(HEAD), &(TAIL), s); \
23950 s->cmp = cmp; \
23951 s->cmp_from = n; \
23952 s->x = (X); \
23953 if (n == 0) \
23954 first_s = s; \
23955 n = fill_composite_glyph_string (s, base_face, overlaps); \
23956 } \
23957 \
23958 ++START; \
23959 s = first_s; \
23960 } while (0)
23961
23962
23963 /* Add a glyph string for a glyph-string sequence to the list of strings
23964 between HEAD and TAIL. */
23965
23966 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23967 do { \
23968 int face_id; \
23969 XChar2b *char2b; \
23970 Lisp_Object gstring; \
23971 \
23972 face_id = (row)->glyphs[area][START].face_id; \
23973 gstring = (composition_gstring_from_id \
23974 ((row)->glyphs[area][START].u.cmp.id)); \
23975 s = alloca (sizeof *s); \
23976 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23977 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23978 append_glyph_string (&(HEAD), &(TAIL), s); \
23979 s->x = (X); \
23980 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23981 } while (0)
23982
23983
23984 /* Add a glyph string for a sequence of glyphless character's glyphs
23985 to the list of strings between HEAD and TAIL. The meanings of
23986 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23987
23988 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23989 do \
23990 { \
23991 int face_id; \
23992 \
23993 face_id = (row)->glyphs[area][START].face_id; \
23994 \
23995 s = alloca (sizeof *s); \
23996 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23997 append_glyph_string (&HEAD, &TAIL, s); \
23998 s->x = (X); \
23999 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24000 overlaps); \
24001 } \
24002 while (0)
24003
24004
24005 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24006 of AREA of glyph row ROW on window W between indices START and END.
24007 HL overrides the face for drawing glyph strings, e.g. it is
24008 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24009 x-positions of the drawing area.
24010
24011 This is an ugly monster macro construct because we must use alloca
24012 to allocate glyph strings (because draw_glyphs can be called
24013 asynchronously). */
24014
24015 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24016 do \
24017 { \
24018 HEAD = TAIL = NULL; \
24019 while (START < END) \
24020 { \
24021 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24022 switch (first_glyph->type) \
24023 { \
24024 case CHAR_GLYPH: \
24025 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24026 HL, X, LAST_X); \
24027 break; \
24028 \
24029 case COMPOSITE_GLYPH: \
24030 if (first_glyph->u.cmp.automatic) \
24031 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24032 HL, X, LAST_X); \
24033 else \
24034 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24035 HL, X, LAST_X); \
24036 break; \
24037 \
24038 case STRETCH_GLYPH: \
24039 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24040 HL, X, LAST_X); \
24041 break; \
24042 \
24043 case IMAGE_GLYPH: \
24044 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24045 HL, X, LAST_X); \
24046 break; \
24047 \
24048 case GLYPHLESS_GLYPH: \
24049 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24050 HL, X, LAST_X); \
24051 break; \
24052 \
24053 default: \
24054 emacs_abort (); \
24055 } \
24056 \
24057 if (s) \
24058 { \
24059 set_glyph_string_background_width (s, START, LAST_X); \
24060 (X) += s->width; \
24061 } \
24062 } \
24063 } while (0)
24064
24065
24066 /* Draw glyphs between START and END in AREA of ROW on window W,
24067 starting at x-position X. X is relative to AREA in W. HL is a
24068 face-override with the following meaning:
24069
24070 DRAW_NORMAL_TEXT draw normally
24071 DRAW_CURSOR draw in cursor face
24072 DRAW_MOUSE_FACE draw in mouse face.
24073 DRAW_INVERSE_VIDEO draw in mode line face
24074 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24075 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24076
24077 If OVERLAPS is non-zero, draw only the foreground of characters and
24078 clip to the physical height of ROW. Non-zero value also defines
24079 the overlapping part to be drawn:
24080
24081 OVERLAPS_PRED overlap with preceding rows
24082 OVERLAPS_SUCC overlap with succeeding rows
24083 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24084 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24085
24086 Value is the x-position reached, relative to AREA of W. */
24087
24088 static int
24089 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24090 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24091 enum draw_glyphs_face hl, int overlaps)
24092 {
24093 struct glyph_string *head, *tail;
24094 struct glyph_string *s;
24095 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24096 int i, j, x_reached, last_x, area_left = 0;
24097 struct frame *f = XFRAME (WINDOW_FRAME (w));
24098 DECLARE_HDC (hdc);
24099
24100 ALLOCATE_HDC (hdc, f);
24101
24102 /* Let's rather be paranoid than getting a SEGV. */
24103 end = min (end, row->used[area]);
24104 start = clip_to_bounds (0, start, end);
24105
24106 /* Translate X to frame coordinates. Set last_x to the right
24107 end of the drawing area. */
24108 if (row->full_width_p)
24109 {
24110 /* X is relative to the left edge of W, without scroll bars
24111 or fringes. */
24112 area_left = WINDOW_LEFT_EDGE_X (w);
24113 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24114 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24115 }
24116 else
24117 {
24118 area_left = window_box_left (w, area);
24119 last_x = area_left + window_box_width (w, area);
24120 }
24121 x += area_left;
24122
24123 /* Build a doubly-linked list of glyph_string structures between
24124 head and tail from what we have to draw. Note that the macro
24125 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24126 the reason we use a separate variable `i'. */
24127 i = start;
24128 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24129 if (tail)
24130 x_reached = tail->x + tail->background_width;
24131 else
24132 x_reached = x;
24133
24134 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24135 the row, redraw some glyphs in front or following the glyph
24136 strings built above. */
24137 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24138 {
24139 struct glyph_string *h, *t;
24140 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24141 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24142 int check_mouse_face = 0;
24143 int dummy_x = 0;
24144
24145 /* If mouse highlighting is on, we may need to draw adjacent
24146 glyphs using mouse-face highlighting. */
24147 if (area == TEXT_AREA && row->mouse_face_p
24148 && hlinfo->mouse_face_beg_row >= 0
24149 && hlinfo->mouse_face_end_row >= 0)
24150 {
24151 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24152
24153 if (row_vpos >= hlinfo->mouse_face_beg_row
24154 && row_vpos <= hlinfo->mouse_face_end_row)
24155 {
24156 check_mouse_face = 1;
24157 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24158 ? hlinfo->mouse_face_beg_col : 0;
24159 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24160 ? hlinfo->mouse_face_end_col
24161 : row->used[TEXT_AREA];
24162 }
24163 }
24164
24165 /* Compute overhangs for all glyph strings. */
24166 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24167 for (s = head; s; s = s->next)
24168 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24169
24170 /* Prepend glyph strings for glyphs in front of the first glyph
24171 string that are overwritten because of the first glyph
24172 string's left overhang. The background of all strings
24173 prepended must be drawn because the first glyph string
24174 draws over it. */
24175 i = left_overwritten (head);
24176 if (i >= 0)
24177 {
24178 enum draw_glyphs_face overlap_hl;
24179
24180 /* If this row contains mouse highlighting, attempt to draw
24181 the overlapped glyphs with the correct highlight. This
24182 code fails if the overlap encompasses more than one glyph
24183 and mouse-highlight spans only some of these glyphs.
24184 However, making it work perfectly involves a lot more
24185 code, and I don't know if the pathological case occurs in
24186 practice, so we'll stick to this for now. --- cyd */
24187 if (check_mouse_face
24188 && mouse_beg_col < start && mouse_end_col > i)
24189 overlap_hl = DRAW_MOUSE_FACE;
24190 else
24191 overlap_hl = DRAW_NORMAL_TEXT;
24192
24193 j = i;
24194 BUILD_GLYPH_STRINGS (j, start, h, t,
24195 overlap_hl, dummy_x, last_x);
24196 start = i;
24197 compute_overhangs_and_x (t, head->x, 1);
24198 prepend_glyph_string_lists (&head, &tail, h, t);
24199 clip_head = head;
24200 }
24201
24202 /* Prepend glyph strings for glyphs in front of the first glyph
24203 string that overwrite that glyph string because of their
24204 right overhang. For these strings, only the foreground must
24205 be drawn, because it draws over the glyph string at `head'.
24206 The background must not be drawn because this would overwrite
24207 right overhangs of preceding glyphs for which no glyph
24208 strings exist. */
24209 i = left_overwriting (head);
24210 if (i >= 0)
24211 {
24212 enum draw_glyphs_face overlap_hl;
24213
24214 if (check_mouse_face
24215 && mouse_beg_col < start && mouse_end_col > i)
24216 overlap_hl = DRAW_MOUSE_FACE;
24217 else
24218 overlap_hl = DRAW_NORMAL_TEXT;
24219
24220 clip_head = head;
24221 BUILD_GLYPH_STRINGS (i, start, h, t,
24222 overlap_hl, dummy_x, last_x);
24223 for (s = h; s; s = s->next)
24224 s->background_filled_p = 1;
24225 compute_overhangs_and_x (t, head->x, 1);
24226 prepend_glyph_string_lists (&head, &tail, h, t);
24227 }
24228
24229 /* Append glyphs strings for glyphs following the last glyph
24230 string tail that are overwritten by tail. The background of
24231 these strings has to be drawn because tail's foreground draws
24232 over it. */
24233 i = right_overwritten (tail);
24234 if (i >= 0)
24235 {
24236 enum draw_glyphs_face overlap_hl;
24237
24238 if (check_mouse_face
24239 && mouse_beg_col < i && mouse_end_col > end)
24240 overlap_hl = DRAW_MOUSE_FACE;
24241 else
24242 overlap_hl = DRAW_NORMAL_TEXT;
24243
24244 BUILD_GLYPH_STRINGS (end, i, h, t,
24245 overlap_hl, x, last_x);
24246 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24247 we don't have `end = i;' here. */
24248 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24249 append_glyph_string_lists (&head, &tail, h, t);
24250 clip_tail = tail;
24251 }
24252
24253 /* Append glyph strings for glyphs following the last glyph
24254 string tail that overwrite tail. The foreground of such
24255 glyphs has to be drawn because it writes into the background
24256 of tail. The background must not be drawn because it could
24257 paint over the foreground of following glyphs. */
24258 i = right_overwriting (tail);
24259 if (i >= 0)
24260 {
24261 enum draw_glyphs_face overlap_hl;
24262 if (check_mouse_face
24263 && mouse_beg_col < i && mouse_end_col > end)
24264 overlap_hl = DRAW_MOUSE_FACE;
24265 else
24266 overlap_hl = DRAW_NORMAL_TEXT;
24267
24268 clip_tail = tail;
24269 i++; /* We must include the Ith glyph. */
24270 BUILD_GLYPH_STRINGS (end, i, h, t,
24271 overlap_hl, x, last_x);
24272 for (s = h; s; s = s->next)
24273 s->background_filled_p = 1;
24274 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24275 append_glyph_string_lists (&head, &tail, h, t);
24276 }
24277 if (clip_head || clip_tail)
24278 for (s = head; s; s = s->next)
24279 {
24280 s->clip_head = clip_head;
24281 s->clip_tail = clip_tail;
24282 }
24283 }
24284
24285 /* Draw all strings. */
24286 for (s = head; s; s = s->next)
24287 FRAME_RIF (f)->draw_glyph_string (s);
24288
24289 #ifndef HAVE_NS
24290 /* When focus a sole frame and move horizontally, this sets on_p to 0
24291 causing a failure to erase prev cursor position. */
24292 if (area == TEXT_AREA
24293 && !row->full_width_p
24294 /* When drawing overlapping rows, only the glyph strings'
24295 foreground is drawn, which doesn't erase a cursor
24296 completely. */
24297 && !overlaps)
24298 {
24299 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24300 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24301 : (tail ? tail->x + tail->background_width : x));
24302 x0 -= area_left;
24303 x1 -= area_left;
24304
24305 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24306 row->y, MATRIX_ROW_BOTTOM_Y (row));
24307 }
24308 #endif
24309
24310 /* Value is the x-position up to which drawn, relative to AREA of W.
24311 This doesn't include parts drawn because of overhangs. */
24312 if (row->full_width_p)
24313 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24314 else
24315 x_reached -= area_left;
24316
24317 RELEASE_HDC (hdc, f);
24318
24319 return x_reached;
24320 }
24321
24322 /* Expand row matrix if too narrow. Don't expand if area
24323 is not present. */
24324
24325 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24326 { \
24327 if (!it->f->fonts_changed \
24328 && (it->glyph_row->glyphs[area] \
24329 < it->glyph_row->glyphs[area + 1])) \
24330 { \
24331 it->w->ncols_scale_factor++; \
24332 it->f->fonts_changed = 1; \
24333 } \
24334 }
24335
24336 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24337 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24338
24339 static void
24340 append_glyph (struct it *it)
24341 {
24342 struct glyph *glyph;
24343 enum glyph_row_area area = it->area;
24344
24345 eassert (it->glyph_row);
24346 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24347
24348 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24349 if (glyph < it->glyph_row->glyphs[area + 1])
24350 {
24351 /* If the glyph row is reversed, we need to prepend the glyph
24352 rather than append it. */
24353 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24354 {
24355 struct glyph *g;
24356
24357 /* Make room for the additional glyph. */
24358 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24359 g[1] = *g;
24360 glyph = it->glyph_row->glyphs[area];
24361 }
24362 glyph->charpos = CHARPOS (it->position);
24363 glyph->object = it->object;
24364 if (it->pixel_width > 0)
24365 {
24366 glyph->pixel_width = it->pixel_width;
24367 glyph->padding_p = 0;
24368 }
24369 else
24370 {
24371 /* Assure at least 1-pixel width. Otherwise, cursor can't
24372 be displayed correctly. */
24373 glyph->pixel_width = 1;
24374 glyph->padding_p = 1;
24375 }
24376 glyph->ascent = it->ascent;
24377 glyph->descent = it->descent;
24378 glyph->voffset = it->voffset;
24379 glyph->type = CHAR_GLYPH;
24380 glyph->avoid_cursor_p = it->avoid_cursor_p;
24381 glyph->multibyte_p = it->multibyte_p;
24382 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24383 {
24384 /* In R2L rows, the left and the right box edges need to be
24385 drawn in reverse direction. */
24386 glyph->right_box_line_p = it->start_of_box_run_p;
24387 glyph->left_box_line_p = it->end_of_box_run_p;
24388 }
24389 else
24390 {
24391 glyph->left_box_line_p = it->start_of_box_run_p;
24392 glyph->right_box_line_p = it->end_of_box_run_p;
24393 }
24394 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24395 || it->phys_descent > it->descent);
24396 glyph->glyph_not_available_p = it->glyph_not_available_p;
24397 glyph->face_id = it->face_id;
24398 glyph->u.ch = it->char_to_display;
24399 glyph->slice.img = null_glyph_slice;
24400 glyph->font_type = FONT_TYPE_UNKNOWN;
24401 if (it->bidi_p)
24402 {
24403 glyph->resolved_level = it->bidi_it.resolved_level;
24404 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24405 emacs_abort ();
24406 glyph->bidi_type = it->bidi_it.type;
24407 }
24408 else
24409 {
24410 glyph->resolved_level = 0;
24411 glyph->bidi_type = UNKNOWN_BT;
24412 }
24413 ++it->glyph_row->used[area];
24414 }
24415 else
24416 IT_EXPAND_MATRIX_WIDTH (it, area);
24417 }
24418
24419 /* Store one glyph for the composition IT->cmp_it.id in
24420 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24421 non-null. */
24422
24423 static void
24424 append_composite_glyph (struct it *it)
24425 {
24426 struct glyph *glyph;
24427 enum glyph_row_area area = it->area;
24428
24429 eassert (it->glyph_row);
24430
24431 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24432 if (glyph < it->glyph_row->glyphs[area + 1])
24433 {
24434 /* If the glyph row is reversed, we need to prepend the glyph
24435 rather than append it. */
24436 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24437 {
24438 struct glyph *g;
24439
24440 /* Make room for the new glyph. */
24441 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24442 g[1] = *g;
24443 glyph = it->glyph_row->glyphs[it->area];
24444 }
24445 glyph->charpos = it->cmp_it.charpos;
24446 glyph->object = it->object;
24447 glyph->pixel_width = it->pixel_width;
24448 glyph->ascent = it->ascent;
24449 glyph->descent = it->descent;
24450 glyph->voffset = it->voffset;
24451 glyph->type = COMPOSITE_GLYPH;
24452 if (it->cmp_it.ch < 0)
24453 {
24454 glyph->u.cmp.automatic = 0;
24455 glyph->u.cmp.id = it->cmp_it.id;
24456 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24457 }
24458 else
24459 {
24460 glyph->u.cmp.automatic = 1;
24461 glyph->u.cmp.id = it->cmp_it.id;
24462 glyph->slice.cmp.from = it->cmp_it.from;
24463 glyph->slice.cmp.to = it->cmp_it.to - 1;
24464 }
24465 glyph->avoid_cursor_p = it->avoid_cursor_p;
24466 glyph->multibyte_p = it->multibyte_p;
24467 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24468 {
24469 /* In R2L rows, the left and the right box edges need to be
24470 drawn in reverse direction. */
24471 glyph->right_box_line_p = it->start_of_box_run_p;
24472 glyph->left_box_line_p = it->end_of_box_run_p;
24473 }
24474 else
24475 {
24476 glyph->left_box_line_p = it->start_of_box_run_p;
24477 glyph->right_box_line_p = it->end_of_box_run_p;
24478 }
24479 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24480 || it->phys_descent > it->descent);
24481 glyph->padding_p = 0;
24482 glyph->glyph_not_available_p = 0;
24483 glyph->face_id = it->face_id;
24484 glyph->font_type = FONT_TYPE_UNKNOWN;
24485 if (it->bidi_p)
24486 {
24487 glyph->resolved_level = it->bidi_it.resolved_level;
24488 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24489 emacs_abort ();
24490 glyph->bidi_type = it->bidi_it.type;
24491 }
24492 ++it->glyph_row->used[area];
24493 }
24494 else
24495 IT_EXPAND_MATRIX_WIDTH (it, area);
24496 }
24497
24498
24499 /* Change IT->ascent and IT->height according to the setting of
24500 IT->voffset. */
24501
24502 static void
24503 take_vertical_position_into_account (struct it *it)
24504 {
24505 if (it->voffset)
24506 {
24507 if (it->voffset < 0)
24508 /* Increase the ascent so that we can display the text higher
24509 in the line. */
24510 it->ascent -= it->voffset;
24511 else
24512 /* Increase the descent so that we can display the text lower
24513 in the line. */
24514 it->descent += it->voffset;
24515 }
24516 }
24517
24518
24519 /* Produce glyphs/get display metrics for the image IT is loaded with.
24520 See the description of struct display_iterator in dispextern.h for
24521 an overview of struct display_iterator. */
24522
24523 static void
24524 produce_image_glyph (struct it *it)
24525 {
24526 struct image *img;
24527 struct face *face;
24528 int glyph_ascent, crop;
24529 struct glyph_slice slice;
24530
24531 eassert (it->what == IT_IMAGE);
24532
24533 face = FACE_FROM_ID (it->f, it->face_id);
24534 eassert (face);
24535 /* Make sure X resources of the face is loaded. */
24536 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24537
24538 if (it->image_id < 0)
24539 {
24540 /* Fringe bitmap. */
24541 it->ascent = it->phys_ascent = 0;
24542 it->descent = it->phys_descent = 0;
24543 it->pixel_width = 0;
24544 it->nglyphs = 0;
24545 return;
24546 }
24547
24548 img = IMAGE_FROM_ID (it->f, it->image_id);
24549 eassert (img);
24550 /* Make sure X resources of the image is loaded. */
24551 prepare_image_for_display (it->f, img);
24552
24553 slice.x = slice.y = 0;
24554 slice.width = img->width;
24555 slice.height = img->height;
24556
24557 if (INTEGERP (it->slice.x))
24558 slice.x = XINT (it->slice.x);
24559 else if (FLOATP (it->slice.x))
24560 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24561
24562 if (INTEGERP (it->slice.y))
24563 slice.y = XINT (it->slice.y);
24564 else if (FLOATP (it->slice.y))
24565 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24566
24567 if (INTEGERP (it->slice.width))
24568 slice.width = XINT (it->slice.width);
24569 else if (FLOATP (it->slice.width))
24570 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24571
24572 if (INTEGERP (it->slice.height))
24573 slice.height = XINT (it->slice.height);
24574 else if (FLOATP (it->slice.height))
24575 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24576
24577 if (slice.x >= img->width)
24578 slice.x = img->width;
24579 if (slice.y >= img->height)
24580 slice.y = img->height;
24581 if (slice.x + slice.width >= img->width)
24582 slice.width = img->width - slice.x;
24583 if (slice.y + slice.height > img->height)
24584 slice.height = img->height - slice.y;
24585
24586 if (slice.width == 0 || slice.height == 0)
24587 return;
24588
24589 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24590
24591 it->descent = slice.height - glyph_ascent;
24592 if (slice.y == 0)
24593 it->descent += img->vmargin;
24594 if (slice.y + slice.height == img->height)
24595 it->descent += img->vmargin;
24596 it->phys_descent = it->descent;
24597
24598 it->pixel_width = slice.width;
24599 if (slice.x == 0)
24600 it->pixel_width += img->hmargin;
24601 if (slice.x + slice.width == img->width)
24602 it->pixel_width += img->hmargin;
24603
24604 /* It's quite possible for images to have an ascent greater than
24605 their height, so don't get confused in that case. */
24606 if (it->descent < 0)
24607 it->descent = 0;
24608
24609 it->nglyphs = 1;
24610
24611 if (face->box != FACE_NO_BOX)
24612 {
24613 if (face->box_line_width > 0)
24614 {
24615 if (slice.y == 0)
24616 it->ascent += face->box_line_width;
24617 if (slice.y + slice.height == img->height)
24618 it->descent += face->box_line_width;
24619 }
24620
24621 if (it->start_of_box_run_p && slice.x == 0)
24622 it->pixel_width += eabs (face->box_line_width);
24623 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24624 it->pixel_width += eabs (face->box_line_width);
24625 }
24626
24627 take_vertical_position_into_account (it);
24628
24629 /* Automatically crop wide image glyphs at right edge so we can
24630 draw the cursor on same display row. */
24631 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24632 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24633 {
24634 it->pixel_width -= crop;
24635 slice.width -= crop;
24636 }
24637
24638 if (it->glyph_row)
24639 {
24640 struct glyph *glyph;
24641 enum glyph_row_area area = it->area;
24642
24643 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24644 if (glyph < it->glyph_row->glyphs[area + 1])
24645 {
24646 glyph->charpos = CHARPOS (it->position);
24647 glyph->object = it->object;
24648 glyph->pixel_width = it->pixel_width;
24649 glyph->ascent = glyph_ascent;
24650 glyph->descent = it->descent;
24651 glyph->voffset = it->voffset;
24652 glyph->type = IMAGE_GLYPH;
24653 glyph->avoid_cursor_p = it->avoid_cursor_p;
24654 glyph->multibyte_p = it->multibyte_p;
24655 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24656 {
24657 /* In R2L rows, the left and the right box edges need to be
24658 drawn in reverse direction. */
24659 glyph->right_box_line_p = it->start_of_box_run_p;
24660 glyph->left_box_line_p = it->end_of_box_run_p;
24661 }
24662 else
24663 {
24664 glyph->left_box_line_p = it->start_of_box_run_p;
24665 glyph->right_box_line_p = it->end_of_box_run_p;
24666 }
24667 glyph->overlaps_vertically_p = 0;
24668 glyph->padding_p = 0;
24669 glyph->glyph_not_available_p = 0;
24670 glyph->face_id = it->face_id;
24671 glyph->u.img_id = img->id;
24672 glyph->slice.img = slice;
24673 glyph->font_type = FONT_TYPE_UNKNOWN;
24674 if (it->bidi_p)
24675 {
24676 glyph->resolved_level = it->bidi_it.resolved_level;
24677 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24678 emacs_abort ();
24679 glyph->bidi_type = it->bidi_it.type;
24680 }
24681 ++it->glyph_row->used[area];
24682 }
24683 else
24684 IT_EXPAND_MATRIX_WIDTH (it, area);
24685 }
24686 }
24687
24688
24689 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24690 of the glyph, WIDTH and HEIGHT are the width and height of the
24691 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24692
24693 static void
24694 append_stretch_glyph (struct it *it, Lisp_Object object,
24695 int width, int height, int ascent)
24696 {
24697 struct glyph *glyph;
24698 enum glyph_row_area area = it->area;
24699
24700 eassert (ascent >= 0 && ascent <= height);
24701
24702 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24703 if (glyph < it->glyph_row->glyphs[area + 1])
24704 {
24705 /* If the glyph row is reversed, we need to prepend the glyph
24706 rather than append it. */
24707 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24708 {
24709 struct glyph *g;
24710
24711 /* Make room for the additional glyph. */
24712 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24713 g[1] = *g;
24714 glyph = it->glyph_row->glyphs[area];
24715 }
24716 glyph->charpos = CHARPOS (it->position);
24717 glyph->object = object;
24718 glyph->pixel_width = width;
24719 glyph->ascent = ascent;
24720 glyph->descent = height - ascent;
24721 glyph->voffset = it->voffset;
24722 glyph->type = STRETCH_GLYPH;
24723 glyph->avoid_cursor_p = it->avoid_cursor_p;
24724 glyph->multibyte_p = it->multibyte_p;
24725 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24726 {
24727 /* In R2L rows, the left and the right box edges need to be
24728 drawn in reverse direction. */
24729 glyph->right_box_line_p = it->start_of_box_run_p;
24730 glyph->left_box_line_p = it->end_of_box_run_p;
24731 }
24732 else
24733 {
24734 glyph->left_box_line_p = it->start_of_box_run_p;
24735 glyph->right_box_line_p = it->end_of_box_run_p;
24736 }
24737 glyph->overlaps_vertically_p = 0;
24738 glyph->padding_p = 0;
24739 glyph->glyph_not_available_p = 0;
24740 glyph->face_id = it->face_id;
24741 glyph->u.stretch.ascent = ascent;
24742 glyph->u.stretch.height = height;
24743 glyph->slice.img = null_glyph_slice;
24744 glyph->font_type = FONT_TYPE_UNKNOWN;
24745 if (it->bidi_p)
24746 {
24747 glyph->resolved_level = it->bidi_it.resolved_level;
24748 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24749 emacs_abort ();
24750 glyph->bidi_type = it->bidi_it.type;
24751 }
24752 else
24753 {
24754 glyph->resolved_level = 0;
24755 glyph->bidi_type = UNKNOWN_BT;
24756 }
24757 ++it->glyph_row->used[area];
24758 }
24759 else
24760 IT_EXPAND_MATRIX_WIDTH (it, area);
24761 }
24762
24763 #endif /* HAVE_WINDOW_SYSTEM */
24764
24765 /* Produce a stretch glyph for iterator IT. IT->object is the value
24766 of the glyph property displayed. The value must be a list
24767 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24768 being recognized:
24769
24770 1. `:width WIDTH' specifies that the space should be WIDTH *
24771 canonical char width wide. WIDTH may be an integer or floating
24772 point number.
24773
24774 2. `:relative-width FACTOR' specifies that the width of the stretch
24775 should be computed from the width of the first character having the
24776 `glyph' property, and should be FACTOR times that width.
24777
24778 3. `:align-to HPOS' specifies that the space should be wide enough
24779 to reach HPOS, a value in canonical character units.
24780
24781 Exactly one of the above pairs must be present.
24782
24783 4. `:height HEIGHT' specifies that the height of the stretch produced
24784 should be HEIGHT, measured in canonical character units.
24785
24786 5. `:relative-height FACTOR' specifies that the height of the
24787 stretch should be FACTOR times the height of the characters having
24788 the glyph property.
24789
24790 Either none or exactly one of 4 or 5 must be present.
24791
24792 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24793 of the stretch should be used for the ascent of the stretch.
24794 ASCENT must be in the range 0 <= ASCENT <= 100. */
24795
24796 void
24797 produce_stretch_glyph (struct it *it)
24798 {
24799 /* (space :width WIDTH :height HEIGHT ...) */
24800 Lisp_Object prop, plist;
24801 int width = 0, height = 0, align_to = -1;
24802 int zero_width_ok_p = 0;
24803 double tem;
24804 struct font *font = NULL;
24805
24806 #ifdef HAVE_WINDOW_SYSTEM
24807 int ascent = 0;
24808 int zero_height_ok_p = 0;
24809
24810 if (FRAME_WINDOW_P (it->f))
24811 {
24812 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24813 font = face->font ? face->font : FRAME_FONT (it->f);
24814 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24815 }
24816 #endif
24817
24818 /* List should start with `space'. */
24819 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24820 plist = XCDR (it->object);
24821
24822 /* Compute the width of the stretch. */
24823 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24824 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24825 {
24826 /* Absolute width `:width WIDTH' specified and valid. */
24827 zero_width_ok_p = 1;
24828 width = (int)tem;
24829 }
24830 #ifdef HAVE_WINDOW_SYSTEM
24831 else if (FRAME_WINDOW_P (it->f)
24832 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24833 {
24834 /* Relative width `:relative-width FACTOR' specified and valid.
24835 Compute the width of the characters having the `glyph'
24836 property. */
24837 struct it it2;
24838 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24839
24840 it2 = *it;
24841 if (it->multibyte_p)
24842 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24843 else
24844 {
24845 it2.c = it2.char_to_display = *p, it2.len = 1;
24846 if (! ASCII_CHAR_P (it2.c))
24847 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24848 }
24849
24850 it2.glyph_row = NULL;
24851 it2.what = IT_CHARACTER;
24852 x_produce_glyphs (&it2);
24853 width = NUMVAL (prop) * it2.pixel_width;
24854 }
24855 #endif /* HAVE_WINDOW_SYSTEM */
24856 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24857 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24858 {
24859 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24860 align_to = (align_to < 0
24861 ? 0
24862 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24863 else if (align_to < 0)
24864 align_to = window_box_left_offset (it->w, TEXT_AREA);
24865 width = max (0, (int)tem + align_to - it->current_x);
24866 zero_width_ok_p = 1;
24867 }
24868 else
24869 /* Nothing specified -> width defaults to canonical char width. */
24870 width = FRAME_COLUMN_WIDTH (it->f);
24871
24872 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24873 width = 1;
24874
24875 #ifdef HAVE_WINDOW_SYSTEM
24876 /* Compute height. */
24877 if (FRAME_WINDOW_P (it->f))
24878 {
24879 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24880 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24881 {
24882 height = (int)tem;
24883 zero_height_ok_p = 1;
24884 }
24885 else if (prop = Fplist_get (plist, QCrelative_height),
24886 NUMVAL (prop) > 0)
24887 height = FONT_HEIGHT (font) * NUMVAL (prop);
24888 else
24889 height = FONT_HEIGHT (font);
24890
24891 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24892 height = 1;
24893
24894 /* Compute percentage of height used for ascent. If
24895 `:ascent ASCENT' is present and valid, use that. Otherwise,
24896 derive the ascent from the font in use. */
24897 if (prop = Fplist_get (plist, QCascent),
24898 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24899 ascent = height * NUMVAL (prop) / 100.0;
24900 else if (!NILP (prop)
24901 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24902 ascent = min (max (0, (int)tem), height);
24903 else
24904 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24905 }
24906 else
24907 #endif /* HAVE_WINDOW_SYSTEM */
24908 height = 1;
24909
24910 if (width > 0 && it->line_wrap != TRUNCATE
24911 && it->current_x + width > it->last_visible_x)
24912 {
24913 width = it->last_visible_x - it->current_x;
24914 #ifdef HAVE_WINDOW_SYSTEM
24915 /* Subtract one more pixel from the stretch width, but only on
24916 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24917 width -= FRAME_WINDOW_P (it->f);
24918 #endif
24919 }
24920
24921 if (width > 0 && height > 0 && it->glyph_row)
24922 {
24923 Lisp_Object o_object = it->object;
24924 Lisp_Object object = it->stack[it->sp - 1].string;
24925 int n = width;
24926
24927 if (!STRINGP (object))
24928 object = it->w->contents;
24929 #ifdef HAVE_WINDOW_SYSTEM
24930 if (FRAME_WINDOW_P (it->f))
24931 append_stretch_glyph (it, object, width, height, ascent);
24932 else
24933 #endif
24934 {
24935 it->object = object;
24936 it->char_to_display = ' ';
24937 it->pixel_width = it->len = 1;
24938 while (n--)
24939 tty_append_glyph (it);
24940 it->object = o_object;
24941 }
24942 }
24943
24944 it->pixel_width = width;
24945 #ifdef HAVE_WINDOW_SYSTEM
24946 if (FRAME_WINDOW_P (it->f))
24947 {
24948 it->ascent = it->phys_ascent = ascent;
24949 it->descent = it->phys_descent = height - it->ascent;
24950 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24951 take_vertical_position_into_account (it);
24952 }
24953 else
24954 #endif
24955 it->nglyphs = width;
24956 }
24957
24958 /* Get information about special display element WHAT in an
24959 environment described by IT. WHAT is one of IT_TRUNCATION or
24960 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24961 non-null glyph_row member. This function ensures that fields like
24962 face_id, c, len of IT are left untouched. */
24963
24964 static void
24965 produce_special_glyphs (struct it *it, enum display_element_type what)
24966 {
24967 struct it temp_it;
24968 Lisp_Object gc;
24969 GLYPH glyph;
24970
24971 temp_it = *it;
24972 temp_it.object = make_number (0);
24973 memset (&temp_it.current, 0, sizeof temp_it.current);
24974
24975 if (what == IT_CONTINUATION)
24976 {
24977 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24978 if (it->bidi_it.paragraph_dir == R2L)
24979 SET_GLYPH_FROM_CHAR (glyph, '/');
24980 else
24981 SET_GLYPH_FROM_CHAR (glyph, '\\');
24982 if (it->dp
24983 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24984 {
24985 /* FIXME: Should we mirror GC for R2L lines? */
24986 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24987 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24988 }
24989 }
24990 else if (what == IT_TRUNCATION)
24991 {
24992 /* Truncation glyph. */
24993 SET_GLYPH_FROM_CHAR (glyph, '$');
24994 if (it->dp
24995 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24996 {
24997 /* FIXME: Should we mirror GC for R2L lines? */
24998 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24999 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25000 }
25001 }
25002 else
25003 emacs_abort ();
25004
25005 #ifdef HAVE_WINDOW_SYSTEM
25006 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25007 is turned off, we precede the truncation/continuation glyphs by a
25008 stretch glyph whose width is computed such that these special
25009 glyphs are aligned at the window margin, even when very different
25010 fonts are used in different glyph rows. */
25011 if (FRAME_WINDOW_P (temp_it.f)
25012 /* init_iterator calls this with it->glyph_row == NULL, and it
25013 wants only the pixel width of the truncation/continuation
25014 glyphs. */
25015 && temp_it.glyph_row
25016 /* insert_left_trunc_glyphs calls us at the beginning of the
25017 row, and it has its own calculation of the stretch glyph
25018 width. */
25019 && temp_it.glyph_row->used[TEXT_AREA] > 0
25020 && (temp_it.glyph_row->reversed_p
25021 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25022 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25023 {
25024 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25025
25026 if (stretch_width > 0)
25027 {
25028 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25029 struct font *font =
25030 face->font ? face->font : FRAME_FONT (temp_it.f);
25031 int stretch_ascent =
25032 (((temp_it.ascent + temp_it.descent)
25033 * FONT_BASE (font)) / FONT_HEIGHT (font));
25034
25035 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25036 temp_it.ascent + temp_it.descent,
25037 stretch_ascent);
25038 }
25039 }
25040 #endif
25041
25042 temp_it.dp = NULL;
25043 temp_it.what = IT_CHARACTER;
25044 temp_it.len = 1;
25045 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25046 temp_it.face_id = GLYPH_FACE (glyph);
25047 temp_it.len = CHAR_BYTES (temp_it.c);
25048
25049 PRODUCE_GLYPHS (&temp_it);
25050 it->pixel_width = temp_it.pixel_width;
25051 it->nglyphs = temp_it.pixel_width;
25052 }
25053
25054 #ifdef HAVE_WINDOW_SYSTEM
25055
25056 /* Calculate line-height and line-spacing properties.
25057 An integer value specifies explicit pixel value.
25058 A float value specifies relative value to current face height.
25059 A cons (float . face-name) specifies relative value to
25060 height of specified face font.
25061
25062 Returns height in pixels, or nil. */
25063
25064
25065 static Lisp_Object
25066 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25067 int boff, int override)
25068 {
25069 Lisp_Object face_name = Qnil;
25070 int ascent, descent, height;
25071
25072 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25073 return val;
25074
25075 if (CONSP (val))
25076 {
25077 face_name = XCAR (val);
25078 val = XCDR (val);
25079 if (!NUMBERP (val))
25080 val = make_number (1);
25081 if (NILP (face_name))
25082 {
25083 height = it->ascent + it->descent;
25084 goto scale;
25085 }
25086 }
25087
25088 if (NILP (face_name))
25089 {
25090 font = FRAME_FONT (it->f);
25091 boff = FRAME_BASELINE_OFFSET (it->f);
25092 }
25093 else if (EQ (face_name, Qt))
25094 {
25095 override = 0;
25096 }
25097 else
25098 {
25099 int face_id;
25100 struct face *face;
25101
25102 face_id = lookup_named_face (it->f, face_name, 0);
25103 if (face_id < 0)
25104 return make_number (-1);
25105
25106 face = FACE_FROM_ID (it->f, face_id);
25107 font = face->font;
25108 if (font == NULL)
25109 return make_number (-1);
25110 boff = font->baseline_offset;
25111 if (font->vertical_centering)
25112 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25113 }
25114
25115 ascent = FONT_BASE (font) + boff;
25116 descent = FONT_DESCENT (font) - boff;
25117
25118 if (override)
25119 {
25120 it->override_ascent = ascent;
25121 it->override_descent = descent;
25122 it->override_boff = boff;
25123 }
25124
25125 height = ascent + descent;
25126
25127 scale:
25128 if (FLOATP (val))
25129 height = (int)(XFLOAT_DATA (val) * height);
25130 else if (INTEGERP (val))
25131 height *= XINT (val);
25132
25133 return make_number (height);
25134 }
25135
25136
25137 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25138 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25139 and only if this is for a character for which no font was found.
25140
25141 If the display method (it->glyphless_method) is
25142 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25143 length of the acronym or the hexadecimal string, UPPER_XOFF and
25144 UPPER_YOFF are pixel offsets for the upper part of the string,
25145 LOWER_XOFF and LOWER_YOFF are for the lower part.
25146
25147 For the other display methods, LEN through LOWER_YOFF are zero. */
25148
25149 static void
25150 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25151 short upper_xoff, short upper_yoff,
25152 short lower_xoff, short lower_yoff)
25153 {
25154 struct glyph *glyph;
25155 enum glyph_row_area area = it->area;
25156
25157 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25158 if (glyph < it->glyph_row->glyphs[area + 1])
25159 {
25160 /* If the glyph row is reversed, we need to prepend the glyph
25161 rather than append it. */
25162 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25163 {
25164 struct glyph *g;
25165
25166 /* Make room for the additional glyph. */
25167 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25168 g[1] = *g;
25169 glyph = it->glyph_row->glyphs[area];
25170 }
25171 glyph->charpos = CHARPOS (it->position);
25172 glyph->object = it->object;
25173 glyph->pixel_width = it->pixel_width;
25174 glyph->ascent = it->ascent;
25175 glyph->descent = it->descent;
25176 glyph->voffset = it->voffset;
25177 glyph->type = GLYPHLESS_GLYPH;
25178 glyph->u.glyphless.method = it->glyphless_method;
25179 glyph->u.glyphless.for_no_font = for_no_font;
25180 glyph->u.glyphless.len = len;
25181 glyph->u.glyphless.ch = it->c;
25182 glyph->slice.glyphless.upper_xoff = upper_xoff;
25183 glyph->slice.glyphless.upper_yoff = upper_yoff;
25184 glyph->slice.glyphless.lower_xoff = lower_xoff;
25185 glyph->slice.glyphless.lower_yoff = lower_yoff;
25186 glyph->avoid_cursor_p = it->avoid_cursor_p;
25187 glyph->multibyte_p = it->multibyte_p;
25188 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25189 {
25190 /* In R2L rows, the left and the right box edges need to be
25191 drawn in reverse direction. */
25192 glyph->right_box_line_p = it->start_of_box_run_p;
25193 glyph->left_box_line_p = it->end_of_box_run_p;
25194 }
25195 else
25196 {
25197 glyph->left_box_line_p = it->start_of_box_run_p;
25198 glyph->right_box_line_p = it->end_of_box_run_p;
25199 }
25200 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25201 || it->phys_descent > it->descent);
25202 glyph->padding_p = 0;
25203 glyph->glyph_not_available_p = 0;
25204 glyph->face_id = face_id;
25205 glyph->font_type = FONT_TYPE_UNKNOWN;
25206 if (it->bidi_p)
25207 {
25208 glyph->resolved_level = it->bidi_it.resolved_level;
25209 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25210 emacs_abort ();
25211 glyph->bidi_type = it->bidi_it.type;
25212 }
25213 ++it->glyph_row->used[area];
25214 }
25215 else
25216 IT_EXPAND_MATRIX_WIDTH (it, area);
25217 }
25218
25219
25220 /* Produce a glyph for a glyphless character for iterator IT.
25221 IT->glyphless_method specifies which method to use for displaying
25222 the character. See the description of enum
25223 glyphless_display_method in dispextern.h for the detail.
25224
25225 FOR_NO_FONT is nonzero if and only if this is for a character for
25226 which no font was found. ACRONYM, if non-nil, is an acronym string
25227 for the character. */
25228
25229 static void
25230 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25231 {
25232 int face_id;
25233 struct face *face;
25234 struct font *font;
25235 int base_width, base_height, width, height;
25236 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25237 int len;
25238
25239 /* Get the metrics of the base font. We always refer to the current
25240 ASCII face. */
25241 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25242 font = face->font ? face->font : FRAME_FONT (it->f);
25243 it->ascent = FONT_BASE (font) + font->baseline_offset;
25244 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25245 base_height = it->ascent + it->descent;
25246 base_width = font->average_width;
25247
25248 face_id = merge_glyphless_glyph_face (it);
25249
25250 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25251 {
25252 it->pixel_width = THIN_SPACE_WIDTH;
25253 len = 0;
25254 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25255 }
25256 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25257 {
25258 width = CHAR_WIDTH (it->c);
25259 if (width == 0)
25260 width = 1;
25261 else if (width > 4)
25262 width = 4;
25263 it->pixel_width = base_width * width;
25264 len = 0;
25265 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25266 }
25267 else
25268 {
25269 char buf[7];
25270 const char *str;
25271 unsigned int code[6];
25272 int upper_len;
25273 int ascent, descent;
25274 struct font_metrics metrics_upper, metrics_lower;
25275
25276 face = FACE_FROM_ID (it->f, face_id);
25277 font = face->font ? face->font : FRAME_FONT (it->f);
25278 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25279
25280 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25281 {
25282 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25283 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25284 if (CONSP (acronym))
25285 acronym = XCAR (acronym);
25286 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25287 }
25288 else
25289 {
25290 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25291 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25292 str = buf;
25293 }
25294 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25295 code[len] = font->driver->encode_char (font, str[len]);
25296 upper_len = (len + 1) / 2;
25297 font->driver->text_extents (font, code, upper_len,
25298 &metrics_upper);
25299 font->driver->text_extents (font, code + upper_len, len - upper_len,
25300 &metrics_lower);
25301
25302
25303
25304 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25305 width = max (metrics_upper.width, metrics_lower.width) + 4;
25306 upper_xoff = upper_yoff = 2; /* the typical case */
25307 if (base_width >= width)
25308 {
25309 /* Align the upper to the left, the lower to the right. */
25310 it->pixel_width = base_width;
25311 lower_xoff = base_width - 2 - metrics_lower.width;
25312 }
25313 else
25314 {
25315 /* Center the shorter one. */
25316 it->pixel_width = width;
25317 if (metrics_upper.width >= metrics_lower.width)
25318 lower_xoff = (width - metrics_lower.width) / 2;
25319 else
25320 {
25321 /* FIXME: This code doesn't look right. It formerly was
25322 missing the "lower_xoff = 0;", which couldn't have
25323 been right since it left lower_xoff uninitialized. */
25324 lower_xoff = 0;
25325 upper_xoff = (width - metrics_upper.width) / 2;
25326 }
25327 }
25328
25329 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25330 top, bottom, and between upper and lower strings. */
25331 height = (metrics_upper.ascent + metrics_upper.descent
25332 + metrics_lower.ascent + metrics_lower.descent) + 5;
25333 /* Center vertically.
25334 H:base_height, D:base_descent
25335 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25336
25337 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25338 descent = D - H/2 + h/2;
25339 lower_yoff = descent - 2 - ld;
25340 upper_yoff = lower_yoff - la - 1 - ud; */
25341 ascent = - (it->descent - (base_height + height + 1) / 2);
25342 descent = it->descent - (base_height - height) / 2;
25343 lower_yoff = descent - 2 - metrics_lower.descent;
25344 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25345 - metrics_upper.descent);
25346 /* Don't make the height shorter than the base height. */
25347 if (height > base_height)
25348 {
25349 it->ascent = ascent;
25350 it->descent = descent;
25351 }
25352 }
25353
25354 it->phys_ascent = it->ascent;
25355 it->phys_descent = it->descent;
25356 if (it->glyph_row)
25357 append_glyphless_glyph (it, face_id, for_no_font, len,
25358 upper_xoff, upper_yoff,
25359 lower_xoff, lower_yoff);
25360 it->nglyphs = 1;
25361 take_vertical_position_into_account (it);
25362 }
25363
25364
25365 /* RIF:
25366 Produce glyphs/get display metrics for the display element IT is
25367 loaded with. See the description of struct it in dispextern.h
25368 for an overview of struct it. */
25369
25370 void
25371 x_produce_glyphs (struct it *it)
25372 {
25373 int extra_line_spacing = it->extra_line_spacing;
25374
25375 it->glyph_not_available_p = 0;
25376
25377 if (it->what == IT_CHARACTER)
25378 {
25379 XChar2b char2b;
25380 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25381 struct font *font = face->font;
25382 struct font_metrics *pcm = NULL;
25383 int boff; /* Baseline offset. */
25384
25385 if (font == NULL)
25386 {
25387 /* When no suitable font is found, display this character by
25388 the method specified in the first extra slot of
25389 Vglyphless_char_display. */
25390 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25391
25392 eassert (it->what == IT_GLYPHLESS);
25393 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25394 goto done;
25395 }
25396
25397 boff = font->baseline_offset;
25398 if (font->vertical_centering)
25399 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25400
25401 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25402 {
25403 int stretched_p;
25404
25405 it->nglyphs = 1;
25406
25407 if (it->override_ascent >= 0)
25408 {
25409 it->ascent = it->override_ascent;
25410 it->descent = it->override_descent;
25411 boff = it->override_boff;
25412 }
25413 else
25414 {
25415 it->ascent = FONT_BASE (font) + boff;
25416 it->descent = FONT_DESCENT (font) - boff;
25417 }
25418
25419 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25420 {
25421 pcm = get_per_char_metric (font, &char2b);
25422 if (pcm->width == 0
25423 && pcm->rbearing == 0 && pcm->lbearing == 0)
25424 pcm = NULL;
25425 }
25426
25427 if (pcm)
25428 {
25429 it->phys_ascent = pcm->ascent + boff;
25430 it->phys_descent = pcm->descent - boff;
25431 it->pixel_width = pcm->width;
25432 }
25433 else
25434 {
25435 it->glyph_not_available_p = 1;
25436 it->phys_ascent = it->ascent;
25437 it->phys_descent = it->descent;
25438 it->pixel_width = font->space_width;
25439 }
25440
25441 if (it->constrain_row_ascent_descent_p)
25442 {
25443 if (it->descent > it->max_descent)
25444 {
25445 it->ascent += it->descent - it->max_descent;
25446 it->descent = it->max_descent;
25447 }
25448 if (it->ascent > it->max_ascent)
25449 {
25450 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25451 it->ascent = it->max_ascent;
25452 }
25453 it->phys_ascent = min (it->phys_ascent, it->ascent);
25454 it->phys_descent = min (it->phys_descent, it->descent);
25455 extra_line_spacing = 0;
25456 }
25457
25458 /* If this is a space inside a region of text with
25459 `space-width' property, change its width. */
25460 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25461 if (stretched_p)
25462 it->pixel_width *= XFLOATINT (it->space_width);
25463
25464 /* If face has a box, add the box thickness to the character
25465 height. If character has a box line to the left and/or
25466 right, add the box line width to the character's width. */
25467 if (face->box != FACE_NO_BOX)
25468 {
25469 int thick = face->box_line_width;
25470
25471 if (thick > 0)
25472 {
25473 it->ascent += thick;
25474 it->descent += thick;
25475 }
25476 else
25477 thick = -thick;
25478
25479 if (it->start_of_box_run_p)
25480 it->pixel_width += thick;
25481 if (it->end_of_box_run_p)
25482 it->pixel_width += thick;
25483 }
25484
25485 /* If face has an overline, add the height of the overline
25486 (1 pixel) and a 1 pixel margin to the character height. */
25487 if (face->overline_p)
25488 it->ascent += overline_margin;
25489
25490 if (it->constrain_row_ascent_descent_p)
25491 {
25492 if (it->ascent > it->max_ascent)
25493 it->ascent = it->max_ascent;
25494 if (it->descent > it->max_descent)
25495 it->descent = it->max_descent;
25496 }
25497
25498 take_vertical_position_into_account (it);
25499
25500 /* If we have to actually produce glyphs, do it. */
25501 if (it->glyph_row)
25502 {
25503 if (stretched_p)
25504 {
25505 /* Translate a space with a `space-width' property
25506 into a stretch glyph. */
25507 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25508 / FONT_HEIGHT (font));
25509 append_stretch_glyph (it, it->object, it->pixel_width,
25510 it->ascent + it->descent, ascent);
25511 }
25512 else
25513 append_glyph (it);
25514
25515 /* If characters with lbearing or rbearing are displayed
25516 in this line, record that fact in a flag of the
25517 glyph row. This is used to optimize X output code. */
25518 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25519 it->glyph_row->contains_overlapping_glyphs_p = 1;
25520 }
25521 if (! stretched_p && it->pixel_width == 0)
25522 /* We assure that all visible glyphs have at least 1-pixel
25523 width. */
25524 it->pixel_width = 1;
25525 }
25526 else if (it->char_to_display == '\n')
25527 {
25528 /* A newline has no width, but we need the height of the
25529 line. But if previous part of the line sets a height,
25530 don't increase that height. */
25531
25532 Lisp_Object height;
25533 Lisp_Object total_height = Qnil;
25534
25535 it->override_ascent = -1;
25536 it->pixel_width = 0;
25537 it->nglyphs = 0;
25538
25539 height = get_it_property (it, Qline_height);
25540 /* Split (line-height total-height) list. */
25541 if (CONSP (height)
25542 && CONSP (XCDR (height))
25543 && NILP (XCDR (XCDR (height))))
25544 {
25545 total_height = XCAR (XCDR (height));
25546 height = XCAR (height);
25547 }
25548 height = calc_line_height_property (it, height, font, boff, 1);
25549
25550 if (it->override_ascent >= 0)
25551 {
25552 it->ascent = it->override_ascent;
25553 it->descent = it->override_descent;
25554 boff = it->override_boff;
25555 }
25556 else
25557 {
25558 it->ascent = FONT_BASE (font) + boff;
25559 it->descent = FONT_DESCENT (font) - boff;
25560 }
25561
25562 if (EQ (height, Qt))
25563 {
25564 if (it->descent > it->max_descent)
25565 {
25566 it->ascent += it->descent - it->max_descent;
25567 it->descent = it->max_descent;
25568 }
25569 if (it->ascent > it->max_ascent)
25570 {
25571 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25572 it->ascent = it->max_ascent;
25573 }
25574 it->phys_ascent = min (it->phys_ascent, it->ascent);
25575 it->phys_descent = min (it->phys_descent, it->descent);
25576 it->constrain_row_ascent_descent_p = 1;
25577 extra_line_spacing = 0;
25578 }
25579 else
25580 {
25581 Lisp_Object spacing;
25582
25583 it->phys_ascent = it->ascent;
25584 it->phys_descent = it->descent;
25585
25586 if ((it->max_ascent > 0 || it->max_descent > 0)
25587 && face->box != FACE_NO_BOX
25588 && face->box_line_width > 0)
25589 {
25590 it->ascent += face->box_line_width;
25591 it->descent += face->box_line_width;
25592 }
25593 if (!NILP (height)
25594 && XINT (height) > it->ascent + it->descent)
25595 it->ascent = XINT (height) - it->descent;
25596
25597 if (!NILP (total_height))
25598 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25599 else
25600 {
25601 spacing = get_it_property (it, Qline_spacing);
25602 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25603 }
25604 if (INTEGERP (spacing))
25605 {
25606 extra_line_spacing = XINT (spacing);
25607 if (!NILP (total_height))
25608 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25609 }
25610 }
25611 }
25612 else /* i.e. (it->char_to_display == '\t') */
25613 {
25614 if (font->space_width > 0)
25615 {
25616 int tab_width = it->tab_width * font->space_width;
25617 int x = it->current_x + it->continuation_lines_width;
25618 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25619
25620 /* If the distance from the current position to the next tab
25621 stop is less than a space character width, use the
25622 tab stop after that. */
25623 if (next_tab_x - x < font->space_width)
25624 next_tab_x += tab_width;
25625
25626 it->pixel_width = next_tab_x - x;
25627 it->nglyphs = 1;
25628 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25629 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25630
25631 if (it->glyph_row)
25632 {
25633 append_stretch_glyph (it, it->object, it->pixel_width,
25634 it->ascent + it->descent, it->ascent);
25635 }
25636 }
25637 else
25638 {
25639 it->pixel_width = 0;
25640 it->nglyphs = 1;
25641 }
25642 }
25643 }
25644 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25645 {
25646 /* A static composition.
25647
25648 Note: A composition is represented as one glyph in the
25649 glyph matrix. There are no padding glyphs.
25650
25651 Important note: pixel_width, ascent, and descent are the
25652 values of what is drawn by draw_glyphs (i.e. the values of
25653 the overall glyphs composed). */
25654 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25655 int boff; /* baseline offset */
25656 struct composition *cmp = composition_table[it->cmp_it.id];
25657 int glyph_len = cmp->glyph_len;
25658 struct font *font = face->font;
25659
25660 it->nglyphs = 1;
25661
25662 /* If we have not yet calculated pixel size data of glyphs of
25663 the composition for the current face font, calculate them
25664 now. Theoretically, we have to check all fonts for the
25665 glyphs, but that requires much time and memory space. So,
25666 here we check only the font of the first glyph. This may
25667 lead to incorrect display, but it's very rare, and C-l
25668 (recenter-top-bottom) can correct the display anyway. */
25669 if (! cmp->font || cmp->font != font)
25670 {
25671 /* Ascent and descent of the font of the first character
25672 of this composition (adjusted by baseline offset).
25673 Ascent and descent of overall glyphs should not be less
25674 than these, respectively. */
25675 int font_ascent, font_descent, font_height;
25676 /* Bounding box of the overall glyphs. */
25677 int leftmost, rightmost, lowest, highest;
25678 int lbearing, rbearing;
25679 int i, width, ascent, descent;
25680 int left_padded = 0, right_padded = 0;
25681 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25682 XChar2b char2b;
25683 struct font_metrics *pcm;
25684 int font_not_found_p;
25685 ptrdiff_t pos;
25686
25687 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25688 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25689 break;
25690 if (glyph_len < cmp->glyph_len)
25691 right_padded = 1;
25692 for (i = 0; i < glyph_len; i++)
25693 {
25694 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25695 break;
25696 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25697 }
25698 if (i > 0)
25699 left_padded = 1;
25700
25701 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25702 : IT_CHARPOS (*it));
25703 /* If no suitable font is found, use the default font. */
25704 font_not_found_p = font == NULL;
25705 if (font_not_found_p)
25706 {
25707 face = face->ascii_face;
25708 font = face->font;
25709 }
25710 boff = font->baseline_offset;
25711 if (font->vertical_centering)
25712 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25713 font_ascent = FONT_BASE (font) + boff;
25714 font_descent = FONT_DESCENT (font) - boff;
25715 font_height = FONT_HEIGHT (font);
25716
25717 cmp->font = font;
25718
25719 pcm = NULL;
25720 if (! font_not_found_p)
25721 {
25722 get_char_face_and_encoding (it->f, c, it->face_id,
25723 &char2b, 0);
25724 pcm = get_per_char_metric (font, &char2b);
25725 }
25726
25727 /* Initialize the bounding box. */
25728 if (pcm)
25729 {
25730 width = cmp->glyph_len > 0 ? pcm->width : 0;
25731 ascent = pcm->ascent;
25732 descent = pcm->descent;
25733 lbearing = pcm->lbearing;
25734 rbearing = pcm->rbearing;
25735 }
25736 else
25737 {
25738 width = cmp->glyph_len > 0 ? font->space_width : 0;
25739 ascent = FONT_BASE (font);
25740 descent = FONT_DESCENT (font);
25741 lbearing = 0;
25742 rbearing = width;
25743 }
25744
25745 rightmost = width;
25746 leftmost = 0;
25747 lowest = - descent + boff;
25748 highest = ascent + boff;
25749
25750 if (! font_not_found_p
25751 && font->default_ascent
25752 && CHAR_TABLE_P (Vuse_default_ascent)
25753 && !NILP (Faref (Vuse_default_ascent,
25754 make_number (it->char_to_display))))
25755 highest = font->default_ascent + boff;
25756
25757 /* Draw the first glyph at the normal position. It may be
25758 shifted to right later if some other glyphs are drawn
25759 at the left. */
25760 cmp->offsets[i * 2] = 0;
25761 cmp->offsets[i * 2 + 1] = boff;
25762 cmp->lbearing = lbearing;
25763 cmp->rbearing = rbearing;
25764
25765 /* Set cmp->offsets for the remaining glyphs. */
25766 for (i++; i < glyph_len; i++)
25767 {
25768 int left, right, btm, top;
25769 int ch = COMPOSITION_GLYPH (cmp, i);
25770 int face_id;
25771 struct face *this_face;
25772
25773 if (ch == '\t')
25774 ch = ' ';
25775 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25776 this_face = FACE_FROM_ID (it->f, face_id);
25777 font = this_face->font;
25778
25779 if (font == NULL)
25780 pcm = NULL;
25781 else
25782 {
25783 get_char_face_and_encoding (it->f, ch, face_id,
25784 &char2b, 0);
25785 pcm = get_per_char_metric (font, &char2b);
25786 }
25787 if (! pcm)
25788 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25789 else
25790 {
25791 width = pcm->width;
25792 ascent = pcm->ascent;
25793 descent = pcm->descent;
25794 lbearing = pcm->lbearing;
25795 rbearing = pcm->rbearing;
25796 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25797 {
25798 /* Relative composition with or without
25799 alternate chars. */
25800 left = (leftmost + rightmost - width) / 2;
25801 btm = - descent + boff;
25802 if (font->relative_compose
25803 && (! CHAR_TABLE_P (Vignore_relative_composition)
25804 || NILP (Faref (Vignore_relative_composition,
25805 make_number (ch)))))
25806 {
25807
25808 if (- descent >= font->relative_compose)
25809 /* One extra pixel between two glyphs. */
25810 btm = highest + 1;
25811 else if (ascent <= 0)
25812 /* One extra pixel between two glyphs. */
25813 btm = lowest - 1 - ascent - descent;
25814 }
25815 }
25816 else
25817 {
25818 /* A composition rule is specified by an integer
25819 value that encodes global and new reference
25820 points (GREF and NREF). GREF and NREF are
25821 specified by numbers as below:
25822
25823 0---1---2 -- ascent
25824 | |
25825 | |
25826 | |
25827 9--10--11 -- center
25828 | |
25829 ---3---4---5--- baseline
25830 | |
25831 6---7---8 -- descent
25832 */
25833 int rule = COMPOSITION_RULE (cmp, i);
25834 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25835
25836 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25837 grefx = gref % 3, nrefx = nref % 3;
25838 grefy = gref / 3, nrefy = nref / 3;
25839 if (xoff)
25840 xoff = font_height * (xoff - 128) / 256;
25841 if (yoff)
25842 yoff = font_height * (yoff - 128) / 256;
25843
25844 left = (leftmost
25845 + grefx * (rightmost - leftmost) / 2
25846 - nrefx * width / 2
25847 + xoff);
25848
25849 btm = ((grefy == 0 ? highest
25850 : grefy == 1 ? 0
25851 : grefy == 2 ? lowest
25852 : (highest + lowest) / 2)
25853 - (nrefy == 0 ? ascent + descent
25854 : nrefy == 1 ? descent - boff
25855 : nrefy == 2 ? 0
25856 : (ascent + descent) / 2)
25857 + yoff);
25858 }
25859
25860 cmp->offsets[i * 2] = left;
25861 cmp->offsets[i * 2 + 1] = btm + descent;
25862
25863 /* Update the bounding box of the overall glyphs. */
25864 if (width > 0)
25865 {
25866 right = left + width;
25867 if (left < leftmost)
25868 leftmost = left;
25869 if (right > rightmost)
25870 rightmost = right;
25871 }
25872 top = btm + descent + ascent;
25873 if (top > highest)
25874 highest = top;
25875 if (btm < lowest)
25876 lowest = btm;
25877
25878 if (cmp->lbearing > left + lbearing)
25879 cmp->lbearing = left + lbearing;
25880 if (cmp->rbearing < left + rbearing)
25881 cmp->rbearing = left + rbearing;
25882 }
25883 }
25884
25885 /* If there are glyphs whose x-offsets are negative,
25886 shift all glyphs to the right and make all x-offsets
25887 non-negative. */
25888 if (leftmost < 0)
25889 {
25890 for (i = 0; i < cmp->glyph_len; i++)
25891 cmp->offsets[i * 2] -= leftmost;
25892 rightmost -= leftmost;
25893 cmp->lbearing -= leftmost;
25894 cmp->rbearing -= leftmost;
25895 }
25896
25897 if (left_padded && cmp->lbearing < 0)
25898 {
25899 for (i = 0; i < cmp->glyph_len; i++)
25900 cmp->offsets[i * 2] -= cmp->lbearing;
25901 rightmost -= cmp->lbearing;
25902 cmp->rbearing -= cmp->lbearing;
25903 cmp->lbearing = 0;
25904 }
25905 if (right_padded && rightmost < cmp->rbearing)
25906 {
25907 rightmost = cmp->rbearing;
25908 }
25909
25910 cmp->pixel_width = rightmost;
25911 cmp->ascent = highest;
25912 cmp->descent = - lowest;
25913 if (cmp->ascent < font_ascent)
25914 cmp->ascent = font_ascent;
25915 if (cmp->descent < font_descent)
25916 cmp->descent = font_descent;
25917 }
25918
25919 if (it->glyph_row
25920 && (cmp->lbearing < 0
25921 || cmp->rbearing > cmp->pixel_width))
25922 it->glyph_row->contains_overlapping_glyphs_p = 1;
25923
25924 it->pixel_width = cmp->pixel_width;
25925 it->ascent = it->phys_ascent = cmp->ascent;
25926 it->descent = it->phys_descent = cmp->descent;
25927 if (face->box != FACE_NO_BOX)
25928 {
25929 int thick = face->box_line_width;
25930
25931 if (thick > 0)
25932 {
25933 it->ascent += thick;
25934 it->descent += thick;
25935 }
25936 else
25937 thick = - thick;
25938
25939 if (it->start_of_box_run_p)
25940 it->pixel_width += thick;
25941 if (it->end_of_box_run_p)
25942 it->pixel_width += thick;
25943 }
25944
25945 /* If face has an overline, add the height of the overline
25946 (1 pixel) and a 1 pixel margin to the character height. */
25947 if (face->overline_p)
25948 it->ascent += overline_margin;
25949
25950 take_vertical_position_into_account (it);
25951 if (it->ascent < 0)
25952 it->ascent = 0;
25953 if (it->descent < 0)
25954 it->descent = 0;
25955
25956 if (it->glyph_row && cmp->glyph_len > 0)
25957 append_composite_glyph (it);
25958 }
25959 else if (it->what == IT_COMPOSITION)
25960 {
25961 /* A dynamic (automatic) composition. */
25962 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25963 Lisp_Object gstring;
25964 struct font_metrics metrics;
25965
25966 it->nglyphs = 1;
25967
25968 gstring = composition_gstring_from_id (it->cmp_it.id);
25969 it->pixel_width
25970 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25971 &metrics);
25972 if (it->glyph_row
25973 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25974 it->glyph_row->contains_overlapping_glyphs_p = 1;
25975 it->ascent = it->phys_ascent = metrics.ascent;
25976 it->descent = it->phys_descent = metrics.descent;
25977 if (face->box != FACE_NO_BOX)
25978 {
25979 int thick = face->box_line_width;
25980
25981 if (thick > 0)
25982 {
25983 it->ascent += thick;
25984 it->descent += thick;
25985 }
25986 else
25987 thick = - thick;
25988
25989 if (it->start_of_box_run_p)
25990 it->pixel_width += thick;
25991 if (it->end_of_box_run_p)
25992 it->pixel_width += thick;
25993 }
25994 /* If face has an overline, add the height of the overline
25995 (1 pixel) and a 1 pixel margin to the character height. */
25996 if (face->overline_p)
25997 it->ascent += overline_margin;
25998 take_vertical_position_into_account (it);
25999 if (it->ascent < 0)
26000 it->ascent = 0;
26001 if (it->descent < 0)
26002 it->descent = 0;
26003
26004 if (it->glyph_row)
26005 append_composite_glyph (it);
26006 }
26007 else if (it->what == IT_GLYPHLESS)
26008 produce_glyphless_glyph (it, 0, Qnil);
26009 else if (it->what == IT_IMAGE)
26010 produce_image_glyph (it);
26011 else if (it->what == IT_STRETCH)
26012 produce_stretch_glyph (it);
26013
26014 done:
26015 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26016 because this isn't true for images with `:ascent 100'. */
26017 eassert (it->ascent >= 0 && it->descent >= 0);
26018 if (it->area == TEXT_AREA)
26019 it->current_x += it->pixel_width;
26020
26021 if (extra_line_spacing > 0)
26022 {
26023 it->descent += extra_line_spacing;
26024 if (extra_line_spacing > it->max_extra_line_spacing)
26025 it->max_extra_line_spacing = extra_line_spacing;
26026 }
26027
26028 it->max_ascent = max (it->max_ascent, it->ascent);
26029 it->max_descent = max (it->max_descent, it->descent);
26030 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26031 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26032 }
26033
26034 /* EXPORT for RIF:
26035 Output LEN glyphs starting at START at the nominal cursor position.
26036 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26037 being updated, and UPDATED_AREA is the area of that row being updated. */
26038
26039 void
26040 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26041 struct glyph *start, enum glyph_row_area updated_area, int len)
26042 {
26043 int x, hpos, chpos = w->phys_cursor.hpos;
26044
26045 eassert (updated_row);
26046 /* When the window is hscrolled, cursor hpos can legitimately be out
26047 of bounds, but we draw the cursor at the corresponding window
26048 margin in that case. */
26049 if (!updated_row->reversed_p && chpos < 0)
26050 chpos = 0;
26051 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26052 chpos = updated_row->used[TEXT_AREA] - 1;
26053
26054 block_input ();
26055
26056 /* Write glyphs. */
26057
26058 hpos = start - updated_row->glyphs[updated_area];
26059 x = draw_glyphs (w, w->output_cursor.x,
26060 updated_row, updated_area,
26061 hpos, hpos + len,
26062 DRAW_NORMAL_TEXT, 0);
26063
26064 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26065 if (updated_area == TEXT_AREA
26066 && w->phys_cursor_on_p
26067 && w->phys_cursor.vpos == w->output_cursor.vpos
26068 && chpos >= hpos
26069 && chpos < hpos + len)
26070 w->phys_cursor_on_p = 0;
26071
26072 unblock_input ();
26073
26074 /* Advance the output cursor. */
26075 w->output_cursor.hpos += len;
26076 w->output_cursor.x = x;
26077 }
26078
26079
26080 /* EXPORT for RIF:
26081 Insert LEN glyphs from START at the nominal cursor position. */
26082
26083 void
26084 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26085 struct glyph *start, enum glyph_row_area updated_area, int len)
26086 {
26087 struct frame *f;
26088 int line_height, shift_by_width, shifted_region_width;
26089 struct glyph_row *row;
26090 struct glyph *glyph;
26091 int frame_x, frame_y;
26092 ptrdiff_t hpos;
26093
26094 eassert (updated_row);
26095 block_input ();
26096 f = XFRAME (WINDOW_FRAME (w));
26097
26098 /* Get the height of the line we are in. */
26099 row = updated_row;
26100 line_height = row->height;
26101
26102 /* Get the width of the glyphs to insert. */
26103 shift_by_width = 0;
26104 for (glyph = start; glyph < start + len; ++glyph)
26105 shift_by_width += glyph->pixel_width;
26106
26107 /* Get the width of the region to shift right. */
26108 shifted_region_width = (window_box_width (w, updated_area)
26109 - w->output_cursor.x
26110 - shift_by_width);
26111
26112 /* Shift right. */
26113 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26114 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26115
26116 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26117 line_height, shift_by_width);
26118
26119 /* Write the glyphs. */
26120 hpos = start - row->glyphs[updated_area];
26121 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26122 hpos, hpos + len,
26123 DRAW_NORMAL_TEXT, 0);
26124
26125 /* Advance the output cursor. */
26126 w->output_cursor.hpos += len;
26127 w->output_cursor.x += shift_by_width;
26128 unblock_input ();
26129 }
26130
26131
26132 /* EXPORT for RIF:
26133 Erase the current text line from the nominal cursor position
26134 (inclusive) to pixel column TO_X (exclusive). The idea is that
26135 everything from TO_X onward is already erased.
26136
26137 TO_X is a pixel position relative to UPDATED_AREA of currently
26138 updated window W. TO_X == -1 means clear to the end of this area. */
26139
26140 void
26141 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26142 enum glyph_row_area updated_area, int to_x)
26143 {
26144 struct frame *f;
26145 int max_x, min_y, max_y;
26146 int from_x, from_y, to_y;
26147
26148 eassert (updated_row);
26149 f = XFRAME (w->frame);
26150
26151 if (updated_row->full_width_p)
26152 max_x = (WINDOW_PIXEL_WIDTH (w)
26153 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26154 else
26155 max_x = window_box_width (w, updated_area);
26156 max_y = window_text_bottom_y (w);
26157
26158 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26159 of window. For TO_X > 0, truncate to end of drawing area. */
26160 if (to_x == 0)
26161 return;
26162 else if (to_x < 0)
26163 to_x = max_x;
26164 else
26165 to_x = min (to_x, max_x);
26166
26167 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26168
26169 /* Notice if the cursor will be cleared by this operation. */
26170 if (!updated_row->full_width_p)
26171 notice_overwritten_cursor (w, updated_area,
26172 w->output_cursor.x, -1,
26173 updated_row->y,
26174 MATRIX_ROW_BOTTOM_Y (updated_row));
26175
26176 from_x = w->output_cursor.x;
26177
26178 /* Translate to frame coordinates. */
26179 if (updated_row->full_width_p)
26180 {
26181 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26182 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26183 }
26184 else
26185 {
26186 int area_left = window_box_left (w, updated_area);
26187 from_x += area_left;
26188 to_x += area_left;
26189 }
26190
26191 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26192 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26193 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26194
26195 /* Prevent inadvertently clearing to end of the X window. */
26196 if (to_x > from_x && to_y > from_y)
26197 {
26198 block_input ();
26199 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26200 to_x - from_x, to_y - from_y);
26201 unblock_input ();
26202 }
26203 }
26204
26205 #endif /* HAVE_WINDOW_SYSTEM */
26206
26207
26208 \f
26209 /***********************************************************************
26210 Cursor types
26211 ***********************************************************************/
26212
26213 /* Value is the internal representation of the specified cursor type
26214 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26215 of the bar cursor. */
26216
26217 static enum text_cursor_kinds
26218 get_specified_cursor_type (Lisp_Object arg, int *width)
26219 {
26220 enum text_cursor_kinds type;
26221
26222 if (NILP (arg))
26223 return NO_CURSOR;
26224
26225 if (EQ (arg, Qbox))
26226 return FILLED_BOX_CURSOR;
26227
26228 if (EQ (arg, Qhollow))
26229 return HOLLOW_BOX_CURSOR;
26230
26231 if (EQ (arg, Qbar))
26232 {
26233 *width = 2;
26234 return BAR_CURSOR;
26235 }
26236
26237 if (CONSP (arg)
26238 && EQ (XCAR (arg), Qbar)
26239 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26240 {
26241 *width = XINT (XCDR (arg));
26242 return BAR_CURSOR;
26243 }
26244
26245 if (EQ (arg, Qhbar))
26246 {
26247 *width = 2;
26248 return HBAR_CURSOR;
26249 }
26250
26251 if (CONSP (arg)
26252 && EQ (XCAR (arg), Qhbar)
26253 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26254 {
26255 *width = XINT (XCDR (arg));
26256 return HBAR_CURSOR;
26257 }
26258
26259 /* Treat anything unknown as "hollow box cursor".
26260 It was bad to signal an error; people have trouble fixing
26261 .Xdefaults with Emacs, when it has something bad in it. */
26262 type = HOLLOW_BOX_CURSOR;
26263
26264 return type;
26265 }
26266
26267 /* Set the default cursor types for specified frame. */
26268 void
26269 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26270 {
26271 int width = 1;
26272 Lisp_Object tem;
26273
26274 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26275 FRAME_CURSOR_WIDTH (f) = width;
26276
26277 /* By default, set up the blink-off state depending on the on-state. */
26278
26279 tem = Fassoc (arg, Vblink_cursor_alist);
26280 if (!NILP (tem))
26281 {
26282 FRAME_BLINK_OFF_CURSOR (f)
26283 = get_specified_cursor_type (XCDR (tem), &width);
26284 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26285 }
26286 else
26287 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26288
26289 /* Make sure the cursor gets redrawn. */
26290 f->cursor_type_changed = 1;
26291 }
26292
26293
26294 #ifdef HAVE_WINDOW_SYSTEM
26295
26296 /* Return the cursor we want to be displayed in window W. Return
26297 width of bar/hbar cursor through WIDTH arg. Return with
26298 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26299 (i.e. if the `system caret' should track this cursor).
26300
26301 In a mini-buffer window, we want the cursor only to appear if we
26302 are reading input from this window. For the selected window, we
26303 want the cursor type given by the frame parameter or buffer local
26304 setting of cursor-type. If explicitly marked off, draw no cursor.
26305 In all other cases, we want a hollow box cursor. */
26306
26307 static enum text_cursor_kinds
26308 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26309 int *active_cursor)
26310 {
26311 struct frame *f = XFRAME (w->frame);
26312 struct buffer *b = XBUFFER (w->contents);
26313 int cursor_type = DEFAULT_CURSOR;
26314 Lisp_Object alt_cursor;
26315 int non_selected = 0;
26316
26317 *active_cursor = 1;
26318
26319 /* Echo area */
26320 if (cursor_in_echo_area
26321 && FRAME_HAS_MINIBUF_P (f)
26322 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26323 {
26324 if (w == XWINDOW (echo_area_window))
26325 {
26326 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26327 {
26328 *width = FRAME_CURSOR_WIDTH (f);
26329 return FRAME_DESIRED_CURSOR (f);
26330 }
26331 else
26332 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26333 }
26334
26335 *active_cursor = 0;
26336 non_selected = 1;
26337 }
26338
26339 /* Detect a nonselected window or nonselected frame. */
26340 else if (w != XWINDOW (f->selected_window)
26341 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26342 {
26343 *active_cursor = 0;
26344
26345 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26346 return NO_CURSOR;
26347
26348 non_selected = 1;
26349 }
26350
26351 /* Never display a cursor in a window in which cursor-type is nil. */
26352 if (NILP (BVAR (b, cursor_type)))
26353 return NO_CURSOR;
26354
26355 /* Get the normal cursor type for this window. */
26356 if (EQ (BVAR (b, cursor_type), Qt))
26357 {
26358 cursor_type = FRAME_DESIRED_CURSOR (f);
26359 *width = FRAME_CURSOR_WIDTH (f);
26360 }
26361 else
26362 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26363
26364 /* Use cursor-in-non-selected-windows instead
26365 for non-selected window or frame. */
26366 if (non_selected)
26367 {
26368 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26369 if (!EQ (Qt, alt_cursor))
26370 return get_specified_cursor_type (alt_cursor, width);
26371 /* t means modify the normal cursor type. */
26372 if (cursor_type == FILLED_BOX_CURSOR)
26373 cursor_type = HOLLOW_BOX_CURSOR;
26374 else if (cursor_type == BAR_CURSOR && *width > 1)
26375 --*width;
26376 return cursor_type;
26377 }
26378
26379 /* Use normal cursor if not blinked off. */
26380 if (!w->cursor_off_p)
26381 {
26382 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26383 {
26384 if (cursor_type == FILLED_BOX_CURSOR)
26385 {
26386 /* Using a block cursor on large images can be very annoying.
26387 So use a hollow cursor for "large" images.
26388 If image is not transparent (no mask), also use hollow cursor. */
26389 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26390 if (img != NULL && IMAGEP (img->spec))
26391 {
26392 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26393 where N = size of default frame font size.
26394 This should cover most of the "tiny" icons people may use. */
26395 if (!img->mask
26396 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26397 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26398 cursor_type = HOLLOW_BOX_CURSOR;
26399 }
26400 }
26401 else if (cursor_type != NO_CURSOR)
26402 {
26403 /* Display current only supports BOX and HOLLOW cursors for images.
26404 So for now, unconditionally use a HOLLOW cursor when cursor is
26405 not a solid box cursor. */
26406 cursor_type = HOLLOW_BOX_CURSOR;
26407 }
26408 }
26409 return cursor_type;
26410 }
26411
26412 /* Cursor is blinked off, so determine how to "toggle" it. */
26413
26414 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26415 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26416 return get_specified_cursor_type (XCDR (alt_cursor), width);
26417
26418 /* Then see if frame has specified a specific blink off cursor type. */
26419 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26420 {
26421 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26422 return FRAME_BLINK_OFF_CURSOR (f);
26423 }
26424
26425 #if 0
26426 /* Some people liked having a permanently visible blinking cursor,
26427 while others had very strong opinions against it. So it was
26428 decided to remove it. KFS 2003-09-03 */
26429
26430 /* Finally perform built-in cursor blinking:
26431 filled box <-> hollow box
26432 wide [h]bar <-> narrow [h]bar
26433 narrow [h]bar <-> no cursor
26434 other type <-> no cursor */
26435
26436 if (cursor_type == FILLED_BOX_CURSOR)
26437 return HOLLOW_BOX_CURSOR;
26438
26439 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26440 {
26441 *width = 1;
26442 return cursor_type;
26443 }
26444 #endif
26445
26446 return NO_CURSOR;
26447 }
26448
26449
26450 /* Notice when the text cursor of window W has been completely
26451 overwritten by a drawing operation that outputs glyphs in AREA
26452 starting at X0 and ending at X1 in the line starting at Y0 and
26453 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26454 the rest of the line after X0 has been written. Y coordinates
26455 are window-relative. */
26456
26457 static void
26458 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26459 int x0, int x1, int y0, int y1)
26460 {
26461 int cx0, cx1, cy0, cy1;
26462 struct glyph_row *row;
26463
26464 if (!w->phys_cursor_on_p)
26465 return;
26466 if (area != TEXT_AREA)
26467 return;
26468
26469 if (w->phys_cursor.vpos < 0
26470 || w->phys_cursor.vpos >= w->current_matrix->nrows
26471 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26472 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26473 return;
26474
26475 if (row->cursor_in_fringe_p)
26476 {
26477 row->cursor_in_fringe_p = 0;
26478 draw_fringe_bitmap (w, row, row->reversed_p);
26479 w->phys_cursor_on_p = 0;
26480 return;
26481 }
26482
26483 cx0 = w->phys_cursor.x;
26484 cx1 = cx0 + w->phys_cursor_width;
26485 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26486 return;
26487
26488 /* The cursor image will be completely removed from the
26489 screen if the output area intersects the cursor area in
26490 y-direction. When we draw in [y0 y1[, and some part of
26491 the cursor is at y < y0, that part must have been drawn
26492 before. When scrolling, the cursor is erased before
26493 actually scrolling, so we don't come here. When not
26494 scrolling, the rows above the old cursor row must have
26495 changed, and in this case these rows must have written
26496 over the cursor image.
26497
26498 Likewise if part of the cursor is below y1, with the
26499 exception of the cursor being in the first blank row at
26500 the buffer and window end because update_text_area
26501 doesn't draw that row. (Except when it does, but
26502 that's handled in update_text_area.) */
26503
26504 cy0 = w->phys_cursor.y;
26505 cy1 = cy0 + w->phys_cursor_height;
26506 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26507 return;
26508
26509 w->phys_cursor_on_p = 0;
26510 }
26511
26512 #endif /* HAVE_WINDOW_SYSTEM */
26513
26514 \f
26515 /************************************************************************
26516 Mouse Face
26517 ************************************************************************/
26518
26519 #ifdef HAVE_WINDOW_SYSTEM
26520
26521 /* EXPORT for RIF:
26522 Fix the display of area AREA of overlapping row ROW in window W
26523 with respect to the overlapping part OVERLAPS. */
26524
26525 void
26526 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26527 enum glyph_row_area area, int overlaps)
26528 {
26529 int i, x;
26530
26531 block_input ();
26532
26533 x = 0;
26534 for (i = 0; i < row->used[area];)
26535 {
26536 if (row->glyphs[area][i].overlaps_vertically_p)
26537 {
26538 int start = i, start_x = x;
26539
26540 do
26541 {
26542 x += row->glyphs[area][i].pixel_width;
26543 ++i;
26544 }
26545 while (i < row->used[area]
26546 && row->glyphs[area][i].overlaps_vertically_p);
26547
26548 draw_glyphs (w, start_x, row, area,
26549 start, i,
26550 DRAW_NORMAL_TEXT, overlaps);
26551 }
26552 else
26553 {
26554 x += row->glyphs[area][i].pixel_width;
26555 ++i;
26556 }
26557 }
26558
26559 unblock_input ();
26560 }
26561
26562
26563 /* EXPORT:
26564 Draw the cursor glyph of window W in glyph row ROW. See the
26565 comment of draw_glyphs for the meaning of HL. */
26566
26567 void
26568 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26569 enum draw_glyphs_face hl)
26570 {
26571 /* If cursor hpos is out of bounds, don't draw garbage. This can
26572 happen in mini-buffer windows when switching between echo area
26573 glyphs and mini-buffer. */
26574 if ((row->reversed_p
26575 ? (w->phys_cursor.hpos >= 0)
26576 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26577 {
26578 int on_p = w->phys_cursor_on_p;
26579 int x1;
26580 int hpos = w->phys_cursor.hpos;
26581
26582 /* When the window is hscrolled, cursor hpos can legitimately be
26583 out of bounds, but we draw the cursor at the corresponding
26584 window margin in that case. */
26585 if (!row->reversed_p && hpos < 0)
26586 hpos = 0;
26587 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26588 hpos = row->used[TEXT_AREA] - 1;
26589
26590 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26591 hl, 0);
26592 w->phys_cursor_on_p = on_p;
26593
26594 if (hl == DRAW_CURSOR)
26595 w->phys_cursor_width = x1 - w->phys_cursor.x;
26596 /* When we erase the cursor, and ROW is overlapped by other
26597 rows, make sure that these overlapping parts of other rows
26598 are redrawn. */
26599 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26600 {
26601 w->phys_cursor_width = x1 - w->phys_cursor.x;
26602
26603 if (row > w->current_matrix->rows
26604 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26605 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26606 OVERLAPS_ERASED_CURSOR);
26607
26608 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26609 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26610 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26611 OVERLAPS_ERASED_CURSOR);
26612 }
26613 }
26614 }
26615
26616
26617 /* Erase the image of a cursor of window W from the screen. */
26618
26619 #ifndef HAVE_NTGUI
26620 static
26621 #endif
26622 void
26623 erase_phys_cursor (struct window *w)
26624 {
26625 struct frame *f = XFRAME (w->frame);
26626 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26627 int hpos = w->phys_cursor.hpos;
26628 int vpos = w->phys_cursor.vpos;
26629 int mouse_face_here_p = 0;
26630 struct glyph_matrix *active_glyphs = w->current_matrix;
26631 struct glyph_row *cursor_row;
26632 struct glyph *cursor_glyph;
26633 enum draw_glyphs_face hl;
26634
26635 /* No cursor displayed or row invalidated => nothing to do on the
26636 screen. */
26637 if (w->phys_cursor_type == NO_CURSOR)
26638 goto mark_cursor_off;
26639
26640 /* VPOS >= active_glyphs->nrows means that window has been resized.
26641 Don't bother to erase the cursor. */
26642 if (vpos >= active_glyphs->nrows)
26643 goto mark_cursor_off;
26644
26645 /* If row containing cursor is marked invalid, there is nothing we
26646 can do. */
26647 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26648 if (!cursor_row->enabled_p)
26649 goto mark_cursor_off;
26650
26651 /* If line spacing is > 0, old cursor may only be partially visible in
26652 window after split-window. So adjust visible height. */
26653 cursor_row->visible_height = min (cursor_row->visible_height,
26654 window_text_bottom_y (w) - cursor_row->y);
26655
26656 /* If row is completely invisible, don't attempt to delete a cursor which
26657 isn't there. This can happen if cursor is at top of a window, and
26658 we switch to a buffer with a header line in that window. */
26659 if (cursor_row->visible_height <= 0)
26660 goto mark_cursor_off;
26661
26662 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26663 if (cursor_row->cursor_in_fringe_p)
26664 {
26665 cursor_row->cursor_in_fringe_p = 0;
26666 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26667 goto mark_cursor_off;
26668 }
26669
26670 /* This can happen when the new row is shorter than the old one.
26671 In this case, either draw_glyphs or clear_end_of_line
26672 should have cleared the cursor. Note that we wouldn't be
26673 able to erase the cursor in this case because we don't have a
26674 cursor glyph at hand. */
26675 if ((cursor_row->reversed_p
26676 ? (w->phys_cursor.hpos < 0)
26677 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26678 goto mark_cursor_off;
26679
26680 /* When the window is hscrolled, cursor hpos can legitimately be out
26681 of bounds, but we draw the cursor at the corresponding window
26682 margin in that case. */
26683 if (!cursor_row->reversed_p && hpos < 0)
26684 hpos = 0;
26685 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26686 hpos = cursor_row->used[TEXT_AREA] - 1;
26687
26688 /* If the cursor is in the mouse face area, redisplay that when
26689 we clear the cursor. */
26690 if (! NILP (hlinfo->mouse_face_window)
26691 && coords_in_mouse_face_p (w, hpos, vpos)
26692 /* Don't redraw the cursor's spot in mouse face if it is at the
26693 end of a line (on a newline). The cursor appears there, but
26694 mouse highlighting does not. */
26695 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26696 mouse_face_here_p = 1;
26697
26698 /* Maybe clear the display under the cursor. */
26699 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26700 {
26701 int x, y, left_x;
26702 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26703 int width;
26704
26705 cursor_glyph = get_phys_cursor_glyph (w);
26706 if (cursor_glyph == NULL)
26707 goto mark_cursor_off;
26708
26709 width = cursor_glyph->pixel_width;
26710 left_x = window_box_left_offset (w, TEXT_AREA);
26711 x = w->phys_cursor.x;
26712 if (x < left_x)
26713 width -= left_x - x;
26714 width = min (width, window_box_width (w, TEXT_AREA) - x);
26715 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26716 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26717
26718 if (width > 0)
26719 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26720 }
26721
26722 /* Erase the cursor by redrawing the character underneath it. */
26723 if (mouse_face_here_p)
26724 hl = DRAW_MOUSE_FACE;
26725 else
26726 hl = DRAW_NORMAL_TEXT;
26727 draw_phys_cursor_glyph (w, cursor_row, hl);
26728
26729 mark_cursor_off:
26730 w->phys_cursor_on_p = 0;
26731 w->phys_cursor_type = NO_CURSOR;
26732 }
26733
26734
26735 /* EXPORT:
26736 Display or clear cursor of window W. If ON is zero, clear the
26737 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26738 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26739
26740 void
26741 display_and_set_cursor (struct window *w, bool on,
26742 int hpos, int vpos, int x, int y)
26743 {
26744 struct frame *f = XFRAME (w->frame);
26745 int new_cursor_type;
26746 int new_cursor_width;
26747 int active_cursor;
26748 struct glyph_row *glyph_row;
26749 struct glyph *glyph;
26750
26751 /* This is pointless on invisible frames, and dangerous on garbaged
26752 windows and frames; in the latter case, the frame or window may
26753 be in the midst of changing its size, and x and y may be off the
26754 window. */
26755 if (! FRAME_VISIBLE_P (f)
26756 || FRAME_GARBAGED_P (f)
26757 || vpos >= w->current_matrix->nrows
26758 || hpos >= w->current_matrix->matrix_w)
26759 return;
26760
26761 /* If cursor is off and we want it off, return quickly. */
26762 if (!on && !w->phys_cursor_on_p)
26763 return;
26764
26765 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26766 /* If cursor row is not enabled, we don't really know where to
26767 display the cursor. */
26768 if (!glyph_row->enabled_p)
26769 {
26770 w->phys_cursor_on_p = 0;
26771 return;
26772 }
26773
26774 glyph = NULL;
26775 if (!glyph_row->exact_window_width_line_p
26776 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26777 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26778
26779 eassert (input_blocked_p ());
26780
26781 /* Set new_cursor_type to the cursor we want to be displayed. */
26782 new_cursor_type = get_window_cursor_type (w, glyph,
26783 &new_cursor_width, &active_cursor);
26784
26785 /* If cursor is currently being shown and we don't want it to be or
26786 it is in the wrong place, or the cursor type is not what we want,
26787 erase it. */
26788 if (w->phys_cursor_on_p
26789 && (!on
26790 || w->phys_cursor.x != x
26791 || w->phys_cursor.y != y
26792 || new_cursor_type != w->phys_cursor_type
26793 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26794 && new_cursor_width != w->phys_cursor_width)))
26795 erase_phys_cursor (w);
26796
26797 /* Don't check phys_cursor_on_p here because that flag is only set
26798 to zero in some cases where we know that the cursor has been
26799 completely erased, to avoid the extra work of erasing the cursor
26800 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26801 still not be visible, or it has only been partly erased. */
26802 if (on)
26803 {
26804 w->phys_cursor_ascent = glyph_row->ascent;
26805 w->phys_cursor_height = glyph_row->height;
26806
26807 /* Set phys_cursor_.* before x_draw_.* is called because some
26808 of them may need the information. */
26809 w->phys_cursor.x = x;
26810 w->phys_cursor.y = glyph_row->y;
26811 w->phys_cursor.hpos = hpos;
26812 w->phys_cursor.vpos = vpos;
26813 }
26814
26815 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26816 new_cursor_type, new_cursor_width,
26817 on, active_cursor);
26818 }
26819
26820
26821 /* Switch the display of W's cursor on or off, according to the value
26822 of ON. */
26823
26824 static void
26825 update_window_cursor (struct window *w, bool on)
26826 {
26827 /* Don't update cursor in windows whose frame is in the process
26828 of being deleted. */
26829 if (w->current_matrix)
26830 {
26831 int hpos = w->phys_cursor.hpos;
26832 int vpos = w->phys_cursor.vpos;
26833 struct glyph_row *row;
26834
26835 if (vpos >= w->current_matrix->nrows
26836 || hpos >= w->current_matrix->matrix_w)
26837 return;
26838
26839 row = MATRIX_ROW (w->current_matrix, vpos);
26840
26841 /* When the window is hscrolled, cursor hpos can legitimately be
26842 out of bounds, but we draw the cursor at the corresponding
26843 window margin in that case. */
26844 if (!row->reversed_p && hpos < 0)
26845 hpos = 0;
26846 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26847 hpos = row->used[TEXT_AREA] - 1;
26848
26849 block_input ();
26850 display_and_set_cursor (w, on, hpos, vpos,
26851 w->phys_cursor.x, w->phys_cursor.y);
26852 unblock_input ();
26853 }
26854 }
26855
26856
26857 /* Call update_window_cursor with parameter ON_P on all leaf windows
26858 in the window tree rooted at W. */
26859
26860 static void
26861 update_cursor_in_window_tree (struct window *w, bool on_p)
26862 {
26863 while (w)
26864 {
26865 if (WINDOWP (w->contents))
26866 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26867 else
26868 update_window_cursor (w, on_p);
26869
26870 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26871 }
26872 }
26873
26874
26875 /* EXPORT:
26876 Display the cursor on window W, or clear it, according to ON_P.
26877 Don't change the cursor's position. */
26878
26879 void
26880 x_update_cursor (struct frame *f, bool on_p)
26881 {
26882 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26883 }
26884
26885
26886 /* EXPORT:
26887 Clear the cursor of window W to background color, and mark the
26888 cursor as not shown. This is used when the text where the cursor
26889 is about to be rewritten. */
26890
26891 void
26892 x_clear_cursor (struct window *w)
26893 {
26894 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26895 update_window_cursor (w, 0);
26896 }
26897
26898 #endif /* HAVE_WINDOW_SYSTEM */
26899
26900 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26901 and MSDOS. */
26902 static void
26903 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26904 int start_hpos, int end_hpos,
26905 enum draw_glyphs_face draw)
26906 {
26907 #ifdef HAVE_WINDOW_SYSTEM
26908 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26909 {
26910 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26911 return;
26912 }
26913 #endif
26914 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26915 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26916 #endif
26917 }
26918
26919 /* Display the active region described by mouse_face_* according to DRAW. */
26920
26921 static void
26922 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26923 {
26924 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26925 struct frame *f = XFRAME (WINDOW_FRAME (w));
26926
26927 if (/* If window is in the process of being destroyed, don't bother
26928 to do anything. */
26929 w->current_matrix != NULL
26930 /* Don't update mouse highlight if hidden */
26931 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26932 /* Recognize when we are called to operate on rows that don't exist
26933 anymore. This can happen when a window is split. */
26934 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26935 {
26936 int phys_cursor_on_p = w->phys_cursor_on_p;
26937 struct glyph_row *row, *first, *last;
26938
26939 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26940 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26941
26942 for (row = first; row <= last && row->enabled_p; ++row)
26943 {
26944 int start_hpos, end_hpos, start_x;
26945
26946 /* For all but the first row, the highlight starts at column 0. */
26947 if (row == first)
26948 {
26949 /* R2L rows have BEG and END in reversed order, but the
26950 screen drawing geometry is always left to right. So
26951 we need to mirror the beginning and end of the
26952 highlighted area in R2L rows. */
26953 if (!row->reversed_p)
26954 {
26955 start_hpos = hlinfo->mouse_face_beg_col;
26956 start_x = hlinfo->mouse_face_beg_x;
26957 }
26958 else if (row == last)
26959 {
26960 start_hpos = hlinfo->mouse_face_end_col;
26961 start_x = hlinfo->mouse_face_end_x;
26962 }
26963 else
26964 {
26965 start_hpos = 0;
26966 start_x = 0;
26967 }
26968 }
26969 else if (row->reversed_p && row == last)
26970 {
26971 start_hpos = hlinfo->mouse_face_end_col;
26972 start_x = hlinfo->mouse_face_end_x;
26973 }
26974 else
26975 {
26976 start_hpos = 0;
26977 start_x = 0;
26978 }
26979
26980 if (row == last)
26981 {
26982 if (!row->reversed_p)
26983 end_hpos = hlinfo->mouse_face_end_col;
26984 else if (row == first)
26985 end_hpos = hlinfo->mouse_face_beg_col;
26986 else
26987 {
26988 end_hpos = row->used[TEXT_AREA];
26989 if (draw == DRAW_NORMAL_TEXT)
26990 row->fill_line_p = 1; /* Clear to end of line */
26991 }
26992 }
26993 else if (row->reversed_p && row == first)
26994 end_hpos = hlinfo->mouse_face_beg_col;
26995 else
26996 {
26997 end_hpos = row->used[TEXT_AREA];
26998 if (draw == DRAW_NORMAL_TEXT)
26999 row->fill_line_p = 1; /* Clear to end of line */
27000 }
27001
27002 if (end_hpos > start_hpos)
27003 {
27004 draw_row_with_mouse_face (w, start_x, row,
27005 start_hpos, end_hpos, draw);
27006
27007 row->mouse_face_p
27008 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27009 }
27010 }
27011
27012 #ifdef HAVE_WINDOW_SYSTEM
27013 /* When we've written over the cursor, arrange for it to
27014 be displayed again. */
27015 if (FRAME_WINDOW_P (f)
27016 && phys_cursor_on_p && !w->phys_cursor_on_p)
27017 {
27018 int hpos = w->phys_cursor.hpos;
27019
27020 /* When the window is hscrolled, cursor hpos can legitimately be
27021 out of bounds, but we draw the cursor at the corresponding
27022 window margin in that case. */
27023 if (!row->reversed_p && hpos < 0)
27024 hpos = 0;
27025 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27026 hpos = row->used[TEXT_AREA] - 1;
27027
27028 block_input ();
27029 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27030 w->phys_cursor.x, w->phys_cursor.y);
27031 unblock_input ();
27032 }
27033 #endif /* HAVE_WINDOW_SYSTEM */
27034 }
27035
27036 #ifdef HAVE_WINDOW_SYSTEM
27037 /* Change the mouse cursor. */
27038 if (FRAME_WINDOW_P (f))
27039 {
27040 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27041 if (draw == DRAW_NORMAL_TEXT
27042 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27043 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27044 else
27045 #endif
27046 if (draw == DRAW_MOUSE_FACE)
27047 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27048 else
27049 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27050 }
27051 #endif /* HAVE_WINDOW_SYSTEM */
27052 }
27053
27054 /* EXPORT:
27055 Clear out the mouse-highlighted active region.
27056 Redraw it un-highlighted first. Value is non-zero if mouse
27057 face was actually drawn unhighlighted. */
27058
27059 int
27060 clear_mouse_face (Mouse_HLInfo *hlinfo)
27061 {
27062 int cleared = 0;
27063
27064 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27065 {
27066 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27067 cleared = 1;
27068 }
27069
27070 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27071 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27072 hlinfo->mouse_face_window = Qnil;
27073 hlinfo->mouse_face_overlay = Qnil;
27074 return cleared;
27075 }
27076
27077 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
27078 within the mouse face on that window. */
27079 static int
27080 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27081 {
27082 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27083
27084 /* Quickly resolve the easy cases. */
27085 if (!(WINDOWP (hlinfo->mouse_face_window)
27086 && XWINDOW (hlinfo->mouse_face_window) == w))
27087 return 0;
27088 if (vpos < hlinfo->mouse_face_beg_row
27089 || vpos > hlinfo->mouse_face_end_row)
27090 return 0;
27091 if (vpos > hlinfo->mouse_face_beg_row
27092 && vpos < hlinfo->mouse_face_end_row)
27093 return 1;
27094
27095 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27096 {
27097 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27098 {
27099 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27100 return 1;
27101 }
27102 else if ((vpos == hlinfo->mouse_face_beg_row
27103 && hpos >= hlinfo->mouse_face_beg_col)
27104 || (vpos == hlinfo->mouse_face_end_row
27105 && hpos < hlinfo->mouse_face_end_col))
27106 return 1;
27107 }
27108 else
27109 {
27110 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27111 {
27112 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27113 return 1;
27114 }
27115 else if ((vpos == hlinfo->mouse_face_beg_row
27116 && hpos <= hlinfo->mouse_face_beg_col)
27117 || (vpos == hlinfo->mouse_face_end_row
27118 && hpos > hlinfo->mouse_face_end_col))
27119 return 1;
27120 }
27121 return 0;
27122 }
27123
27124
27125 /* EXPORT:
27126 Non-zero if physical cursor of window W is within mouse face. */
27127
27128 int
27129 cursor_in_mouse_face_p (struct window *w)
27130 {
27131 int hpos = w->phys_cursor.hpos;
27132 int vpos = w->phys_cursor.vpos;
27133 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27134
27135 /* When the window is hscrolled, cursor hpos can legitimately be out
27136 of bounds, but we draw the cursor at the corresponding window
27137 margin in that case. */
27138 if (!row->reversed_p && hpos < 0)
27139 hpos = 0;
27140 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27141 hpos = row->used[TEXT_AREA] - 1;
27142
27143 return coords_in_mouse_face_p (w, hpos, vpos);
27144 }
27145
27146
27147 \f
27148 /* Find the glyph rows START_ROW and END_ROW of window W that display
27149 characters between buffer positions START_CHARPOS and END_CHARPOS
27150 (excluding END_CHARPOS). DISP_STRING is a display string that
27151 covers these buffer positions. This is similar to
27152 row_containing_pos, but is more accurate when bidi reordering makes
27153 buffer positions change non-linearly with glyph rows. */
27154 static void
27155 rows_from_pos_range (struct window *w,
27156 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27157 Lisp_Object disp_string,
27158 struct glyph_row **start, struct glyph_row **end)
27159 {
27160 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27161 int last_y = window_text_bottom_y (w);
27162 struct glyph_row *row;
27163
27164 *start = NULL;
27165 *end = NULL;
27166
27167 while (!first->enabled_p
27168 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27169 first++;
27170
27171 /* Find the START row. */
27172 for (row = first;
27173 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27174 row++)
27175 {
27176 /* A row can potentially be the START row if the range of the
27177 characters it displays intersects the range
27178 [START_CHARPOS..END_CHARPOS). */
27179 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27180 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27181 /* See the commentary in row_containing_pos, for the
27182 explanation of the complicated way to check whether
27183 some position is beyond the end of the characters
27184 displayed by a row. */
27185 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27186 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27187 && !row->ends_at_zv_p
27188 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27189 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27190 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27191 && !row->ends_at_zv_p
27192 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27193 {
27194 /* Found a candidate row. Now make sure at least one of the
27195 glyphs it displays has a charpos from the range
27196 [START_CHARPOS..END_CHARPOS).
27197
27198 This is not obvious because bidi reordering could make
27199 buffer positions of a row be 1,2,3,102,101,100, and if we
27200 want to highlight characters in [50..60), we don't want
27201 this row, even though [50..60) does intersect [1..103),
27202 the range of character positions given by the row's start
27203 and end positions. */
27204 struct glyph *g = row->glyphs[TEXT_AREA];
27205 struct glyph *e = g + row->used[TEXT_AREA];
27206
27207 while (g < e)
27208 {
27209 if (((BUFFERP (g->object) || INTEGERP (g->object))
27210 && start_charpos <= g->charpos && g->charpos < end_charpos)
27211 /* A glyph that comes from DISP_STRING is by
27212 definition to be highlighted. */
27213 || EQ (g->object, disp_string))
27214 *start = row;
27215 g++;
27216 }
27217 if (*start)
27218 break;
27219 }
27220 }
27221
27222 /* Find the END row. */
27223 if (!*start
27224 /* If the last row is partially visible, start looking for END
27225 from that row, instead of starting from FIRST. */
27226 && !(row->enabled_p
27227 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27228 row = first;
27229 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27230 {
27231 struct glyph_row *next = row + 1;
27232 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27233
27234 if (!next->enabled_p
27235 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27236 /* The first row >= START whose range of displayed characters
27237 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27238 is the row END + 1. */
27239 || (start_charpos < next_start
27240 && end_charpos < next_start)
27241 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27242 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27243 && !next->ends_at_zv_p
27244 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27245 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27246 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27247 && !next->ends_at_zv_p
27248 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27249 {
27250 *end = row;
27251 break;
27252 }
27253 else
27254 {
27255 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27256 but none of the characters it displays are in the range, it is
27257 also END + 1. */
27258 struct glyph *g = next->glyphs[TEXT_AREA];
27259 struct glyph *s = g;
27260 struct glyph *e = g + next->used[TEXT_AREA];
27261
27262 while (g < e)
27263 {
27264 if (((BUFFERP (g->object) || INTEGERP (g->object))
27265 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27266 /* If the buffer position of the first glyph in
27267 the row is equal to END_CHARPOS, it means
27268 the last character to be highlighted is the
27269 newline of ROW, and we must consider NEXT as
27270 END, not END+1. */
27271 || (((!next->reversed_p && g == s)
27272 || (next->reversed_p && g == e - 1))
27273 && (g->charpos == end_charpos
27274 /* Special case for when NEXT is an
27275 empty line at ZV. */
27276 || (g->charpos == -1
27277 && !row->ends_at_zv_p
27278 && next_start == end_charpos)))))
27279 /* A glyph that comes from DISP_STRING is by
27280 definition to be highlighted. */
27281 || EQ (g->object, disp_string))
27282 break;
27283 g++;
27284 }
27285 if (g == e)
27286 {
27287 *end = row;
27288 break;
27289 }
27290 /* The first row that ends at ZV must be the last to be
27291 highlighted. */
27292 else if (next->ends_at_zv_p)
27293 {
27294 *end = next;
27295 break;
27296 }
27297 }
27298 }
27299 }
27300
27301 /* This function sets the mouse_face_* elements of HLINFO, assuming
27302 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27303 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27304 for the overlay or run of text properties specifying the mouse
27305 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27306 before-string and after-string that must also be highlighted.
27307 DISP_STRING, if non-nil, is a display string that may cover some
27308 or all of the highlighted text. */
27309
27310 static void
27311 mouse_face_from_buffer_pos (Lisp_Object window,
27312 Mouse_HLInfo *hlinfo,
27313 ptrdiff_t mouse_charpos,
27314 ptrdiff_t start_charpos,
27315 ptrdiff_t end_charpos,
27316 Lisp_Object before_string,
27317 Lisp_Object after_string,
27318 Lisp_Object disp_string)
27319 {
27320 struct window *w = XWINDOW (window);
27321 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27322 struct glyph_row *r1, *r2;
27323 struct glyph *glyph, *end;
27324 ptrdiff_t ignore, pos;
27325 int x;
27326
27327 eassert (NILP (disp_string) || STRINGP (disp_string));
27328 eassert (NILP (before_string) || STRINGP (before_string));
27329 eassert (NILP (after_string) || STRINGP (after_string));
27330
27331 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27332 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27333 if (r1 == NULL)
27334 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27335 /* If the before-string or display-string contains newlines,
27336 rows_from_pos_range skips to its last row. Move back. */
27337 if (!NILP (before_string) || !NILP (disp_string))
27338 {
27339 struct glyph_row *prev;
27340 while ((prev = r1 - 1, prev >= first)
27341 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27342 && prev->used[TEXT_AREA] > 0)
27343 {
27344 struct glyph *beg = prev->glyphs[TEXT_AREA];
27345 glyph = beg + prev->used[TEXT_AREA];
27346 while (--glyph >= beg && INTEGERP (glyph->object));
27347 if (glyph < beg
27348 || !(EQ (glyph->object, before_string)
27349 || EQ (glyph->object, disp_string)))
27350 break;
27351 r1 = prev;
27352 }
27353 }
27354 if (r2 == NULL)
27355 {
27356 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27357 hlinfo->mouse_face_past_end = 1;
27358 }
27359 else if (!NILP (after_string))
27360 {
27361 /* If the after-string has newlines, advance to its last row. */
27362 struct glyph_row *next;
27363 struct glyph_row *last
27364 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27365
27366 for (next = r2 + 1;
27367 next <= last
27368 && next->used[TEXT_AREA] > 0
27369 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27370 ++next)
27371 r2 = next;
27372 }
27373 /* The rest of the display engine assumes that mouse_face_beg_row is
27374 either above mouse_face_end_row or identical to it. But with
27375 bidi-reordered continued lines, the row for START_CHARPOS could
27376 be below the row for END_CHARPOS. If so, swap the rows and store
27377 them in correct order. */
27378 if (r1->y > r2->y)
27379 {
27380 struct glyph_row *tem = r2;
27381
27382 r2 = r1;
27383 r1 = tem;
27384 }
27385
27386 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27387 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27388
27389 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27390 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27391 could be anywhere in the row and in any order. The strategy
27392 below is to find the leftmost and the rightmost glyph that
27393 belongs to either of these 3 strings, or whose position is
27394 between START_CHARPOS and END_CHARPOS, and highlight all the
27395 glyphs between those two. This may cover more than just the text
27396 between START_CHARPOS and END_CHARPOS if the range of characters
27397 strides the bidi level boundary, e.g. if the beginning is in R2L
27398 text while the end is in L2R text or vice versa. */
27399 if (!r1->reversed_p)
27400 {
27401 /* This row is in a left to right paragraph. Scan it left to
27402 right. */
27403 glyph = r1->glyphs[TEXT_AREA];
27404 end = glyph + r1->used[TEXT_AREA];
27405 x = r1->x;
27406
27407 /* Skip truncation glyphs at the start of the glyph row. */
27408 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27409 for (; glyph < end
27410 && INTEGERP (glyph->object)
27411 && glyph->charpos < 0;
27412 ++glyph)
27413 x += glyph->pixel_width;
27414
27415 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27416 or DISP_STRING, and the first glyph from buffer whose
27417 position is between START_CHARPOS and END_CHARPOS. */
27418 for (; glyph < end
27419 && !INTEGERP (glyph->object)
27420 && !EQ (glyph->object, disp_string)
27421 && !(BUFFERP (glyph->object)
27422 && (glyph->charpos >= start_charpos
27423 && glyph->charpos < end_charpos));
27424 ++glyph)
27425 {
27426 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27427 are present at buffer positions between START_CHARPOS and
27428 END_CHARPOS, or if they come from an overlay. */
27429 if (EQ (glyph->object, before_string))
27430 {
27431 pos = string_buffer_position (before_string,
27432 start_charpos);
27433 /* If pos == 0, it means before_string came from an
27434 overlay, not from a buffer position. */
27435 if (!pos || (pos >= start_charpos && pos < end_charpos))
27436 break;
27437 }
27438 else if (EQ (glyph->object, after_string))
27439 {
27440 pos = string_buffer_position (after_string, end_charpos);
27441 if (!pos || (pos >= start_charpos && pos < end_charpos))
27442 break;
27443 }
27444 x += glyph->pixel_width;
27445 }
27446 hlinfo->mouse_face_beg_x = x;
27447 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27448 }
27449 else
27450 {
27451 /* This row is in a right to left paragraph. Scan it right to
27452 left. */
27453 struct glyph *g;
27454
27455 end = r1->glyphs[TEXT_AREA] - 1;
27456 glyph = end + r1->used[TEXT_AREA];
27457
27458 /* Skip truncation glyphs at the start of the glyph row. */
27459 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27460 for (; glyph > end
27461 && INTEGERP (glyph->object)
27462 && glyph->charpos < 0;
27463 --glyph)
27464 ;
27465
27466 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27467 or DISP_STRING, and the first glyph from buffer whose
27468 position is between START_CHARPOS and END_CHARPOS. */
27469 for (; glyph > end
27470 && !INTEGERP (glyph->object)
27471 && !EQ (glyph->object, disp_string)
27472 && !(BUFFERP (glyph->object)
27473 && (glyph->charpos >= start_charpos
27474 && glyph->charpos < end_charpos));
27475 --glyph)
27476 {
27477 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27478 are present at buffer positions between START_CHARPOS and
27479 END_CHARPOS, or if they come from an overlay. */
27480 if (EQ (glyph->object, before_string))
27481 {
27482 pos = string_buffer_position (before_string, start_charpos);
27483 /* If pos == 0, it means before_string came from an
27484 overlay, not from a buffer position. */
27485 if (!pos || (pos >= start_charpos && pos < end_charpos))
27486 break;
27487 }
27488 else if (EQ (glyph->object, after_string))
27489 {
27490 pos = string_buffer_position (after_string, end_charpos);
27491 if (!pos || (pos >= start_charpos && pos < end_charpos))
27492 break;
27493 }
27494 }
27495
27496 glyph++; /* first glyph to the right of the highlighted area */
27497 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27498 x += g->pixel_width;
27499 hlinfo->mouse_face_beg_x = x;
27500 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27501 }
27502
27503 /* If the highlight ends in a different row, compute GLYPH and END
27504 for the end row. Otherwise, reuse the values computed above for
27505 the row where the highlight begins. */
27506 if (r2 != r1)
27507 {
27508 if (!r2->reversed_p)
27509 {
27510 glyph = r2->glyphs[TEXT_AREA];
27511 end = glyph + r2->used[TEXT_AREA];
27512 x = r2->x;
27513 }
27514 else
27515 {
27516 end = r2->glyphs[TEXT_AREA] - 1;
27517 glyph = end + r2->used[TEXT_AREA];
27518 }
27519 }
27520
27521 if (!r2->reversed_p)
27522 {
27523 /* Skip truncation and continuation glyphs near the end of the
27524 row, and also blanks and stretch glyphs inserted by
27525 extend_face_to_end_of_line. */
27526 while (end > glyph
27527 && INTEGERP ((end - 1)->object))
27528 --end;
27529 /* Scan the rest of the glyph row from the end, looking for the
27530 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27531 DISP_STRING, or whose position is between START_CHARPOS
27532 and END_CHARPOS */
27533 for (--end;
27534 end > glyph
27535 && !INTEGERP (end->object)
27536 && !EQ (end->object, disp_string)
27537 && !(BUFFERP (end->object)
27538 && (end->charpos >= start_charpos
27539 && end->charpos < end_charpos));
27540 --end)
27541 {
27542 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27543 are present at buffer positions between START_CHARPOS and
27544 END_CHARPOS, or if they come from an overlay. */
27545 if (EQ (end->object, before_string))
27546 {
27547 pos = string_buffer_position (before_string, start_charpos);
27548 if (!pos || (pos >= start_charpos && pos < end_charpos))
27549 break;
27550 }
27551 else if (EQ (end->object, after_string))
27552 {
27553 pos = string_buffer_position (after_string, end_charpos);
27554 if (!pos || (pos >= start_charpos && pos < end_charpos))
27555 break;
27556 }
27557 }
27558 /* Find the X coordinate of the last glyph to be highlighted. */
27559 for (; glyph <= end; ++glyph)
27560 x += glyph->pixel_width;
27561
27562 hlinfo->mouse_face_end_x = x;
27563 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27564 }
27565 else
27566 {
27567 /* Skip truncation and continuation glyphs near the end of the
27568 row, and also blanks and stretch glyphs inserted by
27569 extend_face_to_end_of_line. */
27570 x = r2->x;
27571 end++;
27572 while (end < glyph
27573 && INTEGERP (end->object))
27574 {
27575 x += end->pixel_width;
27576 ++end;
27577 }
27578 /* Scan the rest of the glyph row from the end, looking for the
27579 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27580 DISP_STRING, or whose position is between START_CHARPOS
27581 and END_CHARPOS */
27582 for ( ;
27583 end < glyph
27584 && !INTEGERP (end->object)
27585 && !EQ (end->object, disp_string)
27586 && !(BUFFERP (end->object)
27587 && (end->charpos >= start_charpos
27588 && end->charpos < end_charpos));
27589 ++end)
27590 {
27591 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27592 are present at buffer positions between START_CHARPOS and
27593 END_CHARPOS, or if they come from an overlay. */
27594 if (EQ (end->object, before_string))
27595 {
27596 pos = string_buffer_position (before_string, start_charpos);
27597 if (!pos || (pos >= start_charpos && pos < end_charpos))
27598 break;
27599 }
27600 else if (EQ (end->object, after_string))
27601 {
27602 pos = string_buffer_position (after_string, end_charpos);
27603 if (!pos || (pos >= start_charpos && pos < end_charpos))
27604 break;
27605 }
27606 x += end->pixel_width;
27607 }
27608 /* If we exited the above loop because we arrived at the last
27609 glyph of the row, and its buffer position is still not in
27610 range, it means the last character in range is the preceding
27611 newline. Bump the end column and x values to get past the
27612 last glyph. */
27613 if (end == glyph
27614 && BUFFERP (end->object)
27615 && (end->charpos < start_charpos
27616 || end->charpos >= end_charpos))
27617 {
27618 x += end->pixel_width;
27619 ++end;
27620 }
27621 hlinfo->mouse_face_end_x = x;
27622 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27623 }
27624
27625 hlinfo->mouse_face_window = window;
27626 hlinfo->mouse_face_face_id
27627 = face_at_buffer_position (w, mouse_charpos, &ignore,
27628 mouse_charpos + 1,
27629 !hlinfo->mouse_face_hidden, -1);
27630 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27631 }
27632
27633 /* The following function is not used anymore (replaced with
27634 mouse_face_from_string_pos), but I leave it here for the time
27635 being, in case someone would. */
27636
27637 #if 0 /* not used */
27638
27639 /* Find the position of the glyph for position POS in OBJECT in
27640 window W's current matrix, and return in *X, *Y the pixel
27641 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27642
27643 RIGHT_P non-zero means return the position of the right edge of the
27644 glyph, RIGHT_P zero means return the left edge position.
27645
27646 If no glyph for POS exists in the matrix, return the position of
27647 the glyph with the next smaller position that is in the matrix, if
27648 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27649 exists in the matrix, return the position of the glyph with the
27650 next larger position in OBJECT.
27651
27652 Value is non-zero if a glyph was found. */
27653
27654 static int
27655 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27656 int *hpos, int *vpos, int *x, int *y, int right_p)
27657 {
27658 int yb = window_text_bottom_y (w);
27659 struct glyph_row *r;
27660 struct glyph *best_glyph = NULL;
27661 struct glyph_row *best_row = NULL;
27662 int best_x = 0;
27663
27664 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27665 r->enabled_p && r->y < yb;
27666 ++r)
27667 {
27668 struct glyph *g = r->glyphs[TEXT_AREA];
27669 struct glyph *e = g + r->used[TEXT_AREA];
27670 int gx;
27671
27672 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27673 if (EQ (g->object, object))
27674 {
27675 if (g->charpos == pos)
27676 {
27677 best_glyph = g;
27678 best_x = gx;
27679 best_row = r;
27680 goto found;
27681 }
27682 else if (best_glyph == NULL
27683 || ((eabs (g->charpos - pos)
27684 < eabs (best_glyph->charpos - pos))
27685 && (right_p
27686 ? g->charpos < pos
27687 : g->charpos > pos)))
27688 {
27689 best_glyph = g;
27690 best_x = gx;
27691 best_row = r;
27692 }
27693 }
27694 }
27695
27696 found:
27697
27698 if (best_glyph)
27699 {
27700 *x = best_x;
27701 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27702
27703 if (right_p)
27704 {
27705 *x += best_glyph->pixel_width;
27706 ++*hpos;
27707 }
27708
27709 *y = best_row->y;
27710 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27711 }
27712
27713 return best_glyph != NULL;
27714 }
27715 #endif /* not used */
27716
27717 /* Find the positions of the first and the last glyphs in window W's
27718 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27719 (assumed to be a string), and return in HLINFO's mouse_face_*
27720 members the pixel and column/row coordinates of those glyphs. */
27721
27722 static void
27723 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27724 Lisp_Object object,
27725 ptrdiff_t startpos, ptrdiff_t endpos)
27726 {
27727 int yb = window_text_bottom_y (w);
27728 struct glyph_row *r;
27729 struct glyph *g, *e;
27730 int gx;
27731 int found = 0;
27732
27733 /* Find the glyph row with at least one position in the range
27734 [STARTPOS..ENDPOS), and the first glyph in that row whose
27735 position belongs to that range. */
27736 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27737 r->enabled_p && r->y < yb;
27738 ++r)
27739 {
27740 if (!r->reversed_p)
27741 {
27742 g = r->glyphs[TEXT_AREA];
27743 e = g + r->used[TEXT_AREA];
27744 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27745 if (EQ (g->object, object)
27746 && startpos <= g->charpos && g->charpos < endpos)
27747 {
27748 hlinfo->mouse_face_beg_row
27749 = MATRIX_ROW_VPOS (r, w->current_matrix);
27750 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27751 hlinfo->mouse_face_beg_x = gx;
27752 found = 1;
27753 break;
27754 }
27755 }
27756 else
27757 {
27758 struct glyph *g1;
27759
27760 e = r->glyphs[TEXT_AREA];
27761 g = e + r->used[TEXT_AREA];
27762 for ( ; g > e; --g)
27763 if (EQ ((g-1)->object, object)
27764 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27765 {
27766 hlinfo->mouse_face_beg_row
27767 = MATRIX_ROW_VPOS (r, w->current_matrix);
27768 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27769 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27770 gx += g1->pixel_width;
27771 hlinfo->mouse_face_beg_x = gx;
27772 found = 1;
27773 break;
27774 }
27775 }
27776 if (found)
27777 break;
27778 }
27779
27780 if (!found)
27781 return;
27782
27783 /* Starting with the next row, look for the first row which does NOT
27784 include any glyphs whose positions are in the range. */
27785 for (++r; r->enabled_p && r->y < yb; ++r)
27786 {
27787 g = r->glyphs[TEXT_AREA];
27788 e = g + r->used[TEXT_AREA];
27789 found = 0;
27790 for ( ; g < e; ++g)
27791 if (EQ (g->object, object)
27792 && startpos <= g->charpos && g->charpos < endpos)
27793 {
27794 found = 1;
27795 break;
27796 }
27797 if (!found)
27798 break;
27799 }
27800
27801 /* The highlighted region ends on the previous row. */
27802 r--;
27803
27804 /* Set the end row. */
27805 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27806
27807 /* Compute and set the end column and the end column's horizontal
27808 pixel coordinate. */
27809 if (!r->reversed_p)
27810 {
27811 g = r->glyphs[TEXT_AREA];
27812 e = g + r->used[TEXT_AREA];
27813 for ( ; e > g; --e)
27814 if (EQ ((e-1)->object, object)
27815 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27816 break;
27817 hlinfo->mouse_face_end_col = e - g;
27818
27819 for (gx = r->x; g < e; ++g)
27820 gx += g->pixel_width;
27821 hlinfo->mouse_face_end_x = gx;
27822 }
27823 else
27824 {
27825 e = r->glyphs[TEXT_AREA];
27826 g = e + r->used[TEXT_AREA];
27827 for (gx = r->x ; e < g; ++e)
27828 {
27829 if (EQ (e->object, object)
27830 && startpos <= e->charpos && e->charpos < endpos)
27831 break;
27832 gx += e->pixel_width;
27833 }
27834 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27835 hlinfo->mouse_face_end_x = gx;
27836 }
27837 }
27838
27839 #ifdef HAVE_WINDOW_SYSTEM
27840
27841 /* See if position X, Y is within a hot-spot of an image. */
27842
27843 static int
27844 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27845 {
27846 if (!CONSP (hot_spot))
27847 return 0;
27848
27849 if (EQ (XCAR (hot_spot), Qrect))
27850 {
27851 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27852 Lisp_Object rect = XCDR (hot_spot);
27853 Lisp_Object tem;
27854 if (!CONSP (rect))
27855 return 0;
27856 if (!CONSP (XCAR (rect)))
27857 return 0;
27858 if (!CONSP (XCDR (rect)))
27859 return 0;
27860 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27861 return 0;
27862 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27863 return 0;
27864 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27865 return 0;
27866 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27867 return 0;
27868 return 1;
27869 }
27870 else if (EQ (XCAR (hot_spot), Qcircle))
27871 {
27872 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27873 Lisp_Object circ = XCDR (hot_spot);
27874 Lisp_Object lr, lx0, ly0;
27875 if (CONSP (circ)
27876 && CONSP (XCAR (circ))
27877 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27878 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27879 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27880 {
27881 double r = XFLOATINT (lr);
27882 double dx = XINT (lx0) - x;
27883 double dy = XINT (ly0) - y;
27884 return (dx * dx + dy * dy <= r * r);
27885 }
27886 }
27887 else if (EQ (XCAR (hot_spot), Qpoly))
27888 {
27889 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27890 if (VECTORP (XCDR (hot_spot)))
27891 {
27892 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27893 Lisp_Object *poly = v->contents;
27894 ptrdiff_t n = v->header.size;
27895 ptrdiff_t i;
27896 int inside = 0;
27897 Lisp_Object lx, ly;
27898 int x0, y0;
27899
27900 /* Need an even number of coordinates, and at least 3 edges. */
27901 if (n < 6 || n & 1)
27902 return 0;
27903
27904 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27905 If count is odd, we are inside polygon. Pixels on edges
27906 may or may not be included depending on actual geometry of the
27907 polygon. */
27908 if ((lx = poly[n-2], !INTEGERP (lx))
27909 || (ly = poly[n-1], !INTEGERP (lx)))
27910 return 0;
27911 x0 = XINT (lx), y0 = XINT (ly);
27912 for (i = 0; i < n; i += 2)
27913 {
27914 int x1 = x0, y1 = y0;
27915 if ((lx = poly[i], !INTEGERP (lx))
27916 || (ly = poly[i+1], !INTEGERP (ly)))
27917 return 0;
27918 x0 = XINT (lx), y0 = XINT (ly);
27919
27920 /* Does this segment cross the X line? */
27921 if (x0 >= x)
27922 {
27923 if (x1 >= x)
27924 continue;
27925 }
27926 else if (x1 < x)
27927 continue;
27928 if (y > y0 && y > y1)
27929 continue;
27930 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27931 inside = !inside;
27932 }
27933 return inside;
27934 }
27935 }
27936 return 0;
27937 }
27938
27939 Lisp_Object
27940 find_hot_spot (Lisp_Object map, int x, int y)
27941 {
27942 while (CONSP (map))
27943 {
27944 if (CONSP (XCAR (map))
27945 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27946 return XCAR (map);
27947 map = XCDR (map);
27948 }
27949
27950 return Qnil;
27951 }
27952
27953 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27954 3, 3, 0,
27955 doc: /* Lookup in image map MAP coordinates X and Y.
27956 An image map is an alist where each element has the format (AREA ID PLIST).
27957 An AREA is specified as either a rectangle, a circle, or a polygon:
27958 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27959 pixel coordinates of the upper left and bottom right corners.
27960 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27961 and the radius of the circle; r may be a float or integer.
27962 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27963 vector describes one corner in the polygon.
27964 Returns the alist element for the first matching AREA in MAP. */)
27965 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27966 {
27967 if (NILP (map))
27968 return Qnil;
27969
27970 CHECK_NUMBER (x);
27971 CHECK_NUMBER (y);
27972
27973 return find_hot_spot (map,
27974 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27975 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27976 }
27977
27978
27979 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27980 static void
27981 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27982 {
27983 /* Do not change cursor shape while dragging mouse. */
27984 if (!NILP (do_mouse_tracking))
27985 return;
27986
27987 if (!NILP (pointer))
27988 {
27989 if (EQ (pointer, Qarrow))
27990 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27991 else if (EQ (pointer, Qhand))
27992 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27993 else if (EQ (pointer, Qtext))
27994 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27995 else if (EQ (pointer, intern ("hdrag")))
27996 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27997 else if (EQ (pointer, intern ("nhdrag")))
27998 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
27999 #ifdef HAVE_X_WINDOWS
28000 else if (EQ (pointer, intern ("vdrag")))
28001 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28002 #endif
28003 else if (EQ (pointer, intern ("hourglass")))
28004 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28005 else if (EQ (pointer, Qmodeline))
28006 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28007 else
28008 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28009 }
28010
28011 if (cursor != No_Cursor)
28012 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28013 }
28014
28015 #endif /* HAVE_WINDOW_SYSTEM */
28016
28017 /* Take proper action when mouse has moved to the mode or header line
28018 or marginal area AREA of window W, x-position X and y-position Y.
28019 X is relative to the start of the text display area of W, so the
28020 width of bitmap areas and scroll bars must be subtracted to get a
28021 position relative to the start of the mode line. */
28022
28023 static void
28024 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28025 enum window_part area)
28026 {
28027 struct window *w = XWINDOW (window);
28028 struct frame *f = XFRAME (w->frame);
28029 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28030 #ifdef HAVE_WINDOW_SYSTEM
28031 Display_Info *dpyinfo;
28032 #endif
28033 Cursor cursor = No_Cursor;
28034 Lisp_Object pointer = Qnil;
28035 int dx, dy, width, height;
28036 ptrdiff_t charpos;
28037 Lisp_Object string, object = Qnil;
28038 Lisp_Object pos IF_LINT (= Qnil), help;
28039
28040 Lisp_Object mouse_face;
28041 int original_x_pixel = x;
28042 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28043 struct glyph_row *row IF_LINT (= 0);
28044
28045 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28046 {
28047 int x0;
28048 struct glyph *end;
28049
28050 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28051 returns them in row/column units! */
28052 string = mode_line_string (w, area, &x, &y, &charpos,
28053 &object, &dx, &dy, &width, &height);
28054
28055 row = (area == ON_MODE_LINE
28056 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28057 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28058
28059 /* Find the glyph under the mouse pointer. */
28060 if (row->mode_line_p && row->enabled_p)
28061 {
28062 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28063 end = glyph + row->used[TEXT_AREA];
28064
28065 for (x0 = original_x_pixel;
28066 glyph < end && x0 >= glyph->pixel_width;
28067 ++glyph)
28068 x0 -= glyph->pixel_width;
28069
28070 if (glyph >= end)
28071 glyph = NULL;
28072 }
28073 }
28074 else
28075 {
28076 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28077 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28078 returns them in row/column units! */
28079 string = marginal_area_string (w, area, &x, &y, &charpos,
28080 &object, &dx, &dy, &width, &height);
28081 }
28082
28083 help = Qnil;
28084
28085 #ifdef HAVE_WINDOW_SYSTEM
28086 if (IMAGEP (object))
28087 {
28088 Lisp_Object image_map, hotspot;
28089 if ((image_map = Fplist_get (XCDR (object), QCmap),
28090 !NILP (image_map))
28091 && (hotspot = find_hot_spot (image_map, dx, dy),
28092 CONSP (hotspot))
28093 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28094 {
28095 Lisp_Object plist;
28096
28097 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28098 If so, we could look for mouse-enter, mouse-leave
28099 properties in PLIST (and do something...). */
28100 hotspot = XCDR (hotspot);
28101 if (CONSP (hotspot)
28102 && (plist = XCAR (hotspot), CONSP (plist)))
28103 {
28104 pointer = Fplist_get (plist, Qpointer);
28105 if (NILP (pointer))
28106 pointer = Qhand;
28107 help = Fplist_get (plist, Qhelp_echo);
28108 if (!NILP (help))
28109 {
28110 help_echo_string = help;
28111 XSETWINDOW (help_echo_window, w);
28112 help_echo_object = w->contents;
28113 help_echo_pos = charpos;
28114 }
28115 }
28116 }
28117 if (NILP (pointer))
28118 pointer = Fplist_get (XCDR (object), QCpointer);
28119 }
28120 #endif /* HAVE_WINDOW_SYSTEM */
28121
28122 if (STRINGP (string))
28123 pos = make_number (charpos);
28124
28125 /* Set the help text and mouse pointer. If the mouse is on a part
28126 of the mode line without any text (e.g. past the right edge of
28127 the mode line text), use the default help text and pointer. */
28128 if (STRINGP (string) || area == ON_MODE_LINE)
28129 {
28130 /* Arrange to display the help by setting the global variables
28131 help_echo_string, help_echo_object, and help_echo_pos. */
28132 if (NILP (help))
28133 {
28134 if (STRINGP (string))
28135 help = Fget_text_property (pos, Qhelp_echo, string);
28136
28137 if (!NILP (help))
28138 {
28139 help_echo_string = help;
28140 XSETWINDOW (help_echo_window, w);
28141 help_echo_object = string;
28142 help_echo_pos = charpos;
28143 }
28144 else if (area == ON_MODE_LINE)
28145 {
28146 Lisp_Object default_help
28147 = buffer_local_value_1 (Qmode_line_default_help_echo,
28148 w->contents);
28149
28150 if (STRINGP (default_help))
28151 {
28152 help_echo_string = default_help;
28153 XSETWINDOW (help_echo_window, w);
28154 help_echo_object = Qnil;
28155 help_echo_pos = -1;
28156 }
28157 }
28158 }
28159
28160 #ifdef HAVE_WINDOW_SYSTEM
28161 /* Change the mouse pointer according to what is under it. */
28162 if (FRAME_WINDOW_P (f))
28163 {
28164 dpyinfo = FRAME_DISPLAY_INFO (f);
28165 if (STRINGP (string))
28166 {
28167 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28168
28169 if (NILP (pointer))
28170 pointer = Fget_text_property (pos, Qpointer, string);
28171
28172 /* Change the mouse pointer according to what is under X/Y. */
28173 if (NILP (pointer)
28174 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28175 {
28176 Lisp_Object map;
28177 map = Fget_text_property (pos, Qlocal_map, string);
28178 if (!KEYMAPP (map))
28179 map = Fget_text_property (pos, Qkeymap, string);
28180 if (!KEYMAPP (map))
28181 cursor = dpyinfo->vertical_scroll_bar_cursor;
28182 }
28183 }
28184 else
28185 /* Default mode-line pointer. */
28186 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28187 }
28188 #endif
28189 }
28190
28191 /* Change the mouse face according to what is under X/Y. */
28192 if (STRINGP (string))
28193 {
28194 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28195 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28196 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28197 && glyph)
28198 {
28199 Lisp_Object b, e;
28200
28201 struct glyph * tmp_glyph;
28202
28203 int gpos;
28204 int gseq_length;
28205 int total_pixel_width;
28206 ptrdiff_t begpos, endpos, ignore;
28207
28208 int vpos, hpos;
28209
28210 b = Fprevious_single_property_change (make_number (charpos + 1),
28211 Qmouse_face, string, Qnil);
28212 if (NILP (b))
28213 begpos = 0;
28214 else
28215 begpos = XINT (b);
28216
28217 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28218 if (NILP (e))
28219 endpos = SCHARS (string);
28220 else
28221 endpos = XINT (e);
28222
28223 /* Calculate the glyph position GPOS of GLYPH in the
28224 displayed string, relative to the beginning of the
28225 highlighted part of the string.
28226
28227 Note: GPOS is different from CHARPOS. CHARPOS is the
28228 position of GLYPH in the internal string object. A mode
28229 line string format has structures which are converted to
28230 a flattened string by the Emacs Lisp interpreter. The
28231 internal string is an element of those structures. The
28232 displayed string is the flattened string. */
28233 tmp_glyph = row_start_glyph;
28234 while (tmp_glyph < glyph
28235 && (!(EQ (tmp_glyph->object, glyph->object)
28236 && begpos <= tmp_glyph->charpos
28237 && tmp_glyph->charpos < endpos)))
28238 tmp_glyph++;
28239 gpos = glyph - tmp_glyph;
28240
28241 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28242 the highlighted part of the displayed string to which
28243 GLYPH belongs. Note: GSEQ_LENGTH is different from
28244 SCHARS (STRING), because the latter returns the length of
28245 the internal string. */
28246 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28247 tmp_glyph > glyph
28248 && (!(EQ (tmp_glyph->object, glyph->object)
28249 && begpos <= tmp_glyph->charpos
28250 && tmp_glyph->charpos < endpos));
28251 tmp_glyph--)
28252 ;
28253 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28254
28255 /* Calculate the total pixel width of all the glyphs between
28256 the beginning of the highlighted area and GLYPH. */
28257 total_pixel_width = 0;
28258 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28259 total_pixel_width += tmp_glyph->pixel_width;
28260
28261 /* Pre calculation of re-rendering position. Note: X is in
28262 column units here, after the call to mode_line_string or
28263 marginal_area_string. */
28264 hpos = x - gpos;
28265 vpos = (area == ON_MODE_LINE
28266 ? (w->current_matrix)->nrows - 1
28267 : 0);
28268
28269 /* If GLYPH's position is included in the region that is
28270 already drawn in mouse face, we have nothing to do. */
28271 if ( EQ (window, hlinfo->mouse_face_window)
28272 && (!row->reversed_p
28273 ? (hlinfo->mouse_face_beg_col <= hpos
28274 && hpos < hlinfo->mouse_face_end_col)
28275 /* In R2L rows we swap BEG and END, see below. */
28276 : (hlinfo->mouse_face_end_col <= hpos
28277 && hpos < hlinfo->mouse_face_beg_col))
28278 && hlinfo->mouse_face_beg_row == vpos )
28279 return;
28280
28281 if (clear_mouse_face (hlinfo))
28282 cursor = No_Cursor;
28283
28284 if (!row->reversed_p)
28285 {
28286 hlinfo->mouse_face_beg_col = hpos;
28287 hlinfo->mouse_face_beg_x = original_x_pixel
28288 - (total_pixel_width + dx);
28289 hlinfo->mouse_face_end_col = hpos + gseq_length;
28290 hlinfo->mouse_face_end_x = 0;
28291 }
28292 else
28293 {
28294 /* In R2L rows, show_mouse_face expects BEG and END
28295 coordinates to be swapped. */
28296 hlinfo->mouse_face_end_col = hpos;
28297 hlinfo->mouse_face_end_x = original_x_pixel
28298 - (total_pixel_width + dx);
28299 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28300 hlinfo->mouse_face_beg_x = 0;
28301 }
28302
28303 hlinfo->mouse_face_beg_row = vpos;
28304 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28305 hlinfo->mouse_face_past_end = 0;
28306 hlinfo->mouse_face_window = window;
28307
28308 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28309 charpos,
28310 0, &ignore,
28311 glyph->face_id,
28312 1);
28313 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28314
28315 if (NILP (pointer))
28316 pointer = Qhand;
28317 }
28318 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28319 clear_mouse_face (hlinfo);
28320 }
28321 #ifdef HAVE_WINDOW_SYSTEM
28322 if (FRAME_WINDOW_P (f))
28323 define_frame_cursor1 (f, cursor, pointer);
28324 #endif
28325 }
28326
28327
28328 /* EXPORT:
28329 Take proper action when the mouse has moved to position X, Y on
28330 frame F with regards to highlighting portions of display that have
28331 mouse-face properties. Also de-highlight portions of display where
28332 the mouse was before, set the mouse pointer shape as appropriate
28333 for the mouse coordinates, and activate help echo (tooltips).
28334 X and Y can be negative or out of range. */
28335
28336 void
28337 note_mouse_highlight (struct frame *f, int x, int y)
28338 {
28339 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28340 enum window_part part = ON_NOTHING;
28341 Lisp_Object window;
28342 struct window *w;
28343 Cursor cursor = No_Cursor;
28344 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28345 struct buffer *b;
28346
28347 /* When a menu is active, don't highlight because this looks odd. */
28348 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28349 if (popup_activated ())
28350 return;
28351 #endif
28352
28353 if (!f->glyphs_initialized_p
28354 || f->pointer_invisible)
28355 return;
28356
28357 hlinfo->mouse_face_mouse_x = x;
28358 hlinfo->mouse_face_mouse_y = y;
28359 hlinfo->mouse_face_mouse_frame = f;
28360
28361 if (hlinfo->mouse_face_defer)
28362 return;
28363
28364 /* Which window is that in? */
28365 window = window_from_coordinates (f, x, y, &part, 1);
28366
28367 /* If displaying active text in another window, clear that. */
28368 if (! EQ (window, hlinfo->mouse_face_window)
28369 /* Also clear if we move out of text area in same window. */
28370 || (!NILP (hlinfo->mouse_face_window)
28371 && !NILP (window)
28372 && part != ON_TEXT
28373 && part != ON_MODE_LINE
28374 && part != ON_HEADER_LINE))
28375 clear_mouse_face (hlinfo);
28376
28377 /* Not on a window -> return. */
28378 if (!WINDOWP (window))
28379 return;
28380
28381 /* Reset help_echo_string. It will get recomputed below. */
28382 help_echo_string = Qnil;
28383
28384 /* Convert to window-relative pixel coordinates. */
28385 w = XWINDOW (window);
28386 frame_to_window_pixel_xy (w, &x, &y);
28387
28388 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28389 /* Handle tool-bar window differently since it doesn't display a
28390 buffer. */
28391 if (EQ (window, f->tool_bar_window))
28392 {
28393 note_tool_bar_highlight (f, x, y);
28394 return;
28395 }
28396 #endif
28397
28398 /* Mouse is on the mode, header line or margin? */
28399 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28400 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28401 {
28402 note_mode_line_or_margin_highlight (window, x, y, part);
28403 return;
28404 }
28405
28406 #ifdef HAVE_WINDOW_SYSTEM
28407 if (part == ON_VERTICAL_BORDER)
28408 {
28409 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28410 help_echo_string = build_string ("drag-mouse-1: resize");
28411 }
28412 else if (part == ON_RIGHT_DIVIDER)
28413 {
28414 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28415 help_echo_string = build_string ("drag-mouse-1: resize");
28416 }
28417 else if (part == ON_BOTTOM_DIVIDER)
28418 {
28419 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28420 help_echo_string = build_string ("drag-mouse-1: resize");
28421 }
28422 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28423 || part == ON_SCROLL_BAR)
28424 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28425 else
28426 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28427 #endif
28428
28429 /* Are we in a window whose display is up to date?
28430 And verify the buffer's text has not changed. */
28431 b = XBUFFER (w->contents);
28432 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28433 {
28434 int hpos, vpos, dx, dy, area = LAST_AREA;
28435 ptrdiff_t pos;
28436 struct glyph *glyph;
28437 Lisp_Object object;
28438 Lisp_Object mouse_face = Qnil, position;
28439 Lisp_Object *overlay_vec = NULL;
28440 ptrdiff_t i, noverlays;
28441 struct buffer *obuf;
28442 ptrdiff_t obegv, ozv;
28443 int same_region;
28444
28445 /* Find the glyph under X/Y. */
28446 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28447
28448 #ifdef HAVE_WINDOW_SYSTEM
28449 /* Look for :pointer property on image. */
28450 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28451 {
28452 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28453 if (img != NULL && IMAGEP (img->spec))
28454 {
28455 Lisp_Object image_map, hotspot;
28456 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28457 !NILP (image_map))
28458 && (hotspot = find_hot_spot (image_map,
28459 glyph->slice.img.x + dx,
28460 glyph->slice.img.y + dy),
28461 CONSP (hotspot))
28462 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28463 {
28464 Lisp_Object plist;
28465
28466 /* Could check XCAR (hotspot) to see if we enter/leave
28467 this hot-spot.
28468 If so, we could look for mouse-enter, mouse-leave
28469 properties in PLIST (and do something...). */
28470 hotspot = XCDR (hotspot);
28471 if (CONSP (hotspot)
28472 && (plist = XCAR (hotspot), CONSP (plist)))
28473 {
28474 pointer = Fplist_get (plist, Qpointer);
28475 if (NILP (pointer))
28476 pointer = Qhand;
28477 help_echo_string = Fplist_get (plist, Qhelp_echo);
28478 if (!NILP (help_echo_string))
28479 {
28480 help_echo_window = window;
28481 help_echo_object = glyph->object;
28482 help_echo_pos = glyph->charpos;
28483 }
28484 }
28485 }
28486 if (NILP (pointer))
28487 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28488 }
28489 }
28490 #endif /* HAVE_WINDOW_SYSTEM */
28491
28492 /* Clear mouse face if X/Y not over text. */
28493 if (glyph == NULL
28494 || area != TEXT_AREA
28495 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28496 /* Glyph's OBJECT is an integer for glyphs inserted by the
28497 display engine for its internal purposes, like truncation
28498 and continuation glyphs and blanks beyond the end of
28499 line's text on text terminals. If we are over such a
28500 glyph, we are not over any text. */
28501 || INTEGERP (glyph->object)
28502 /* R2L rows have a stretch glyph at their front, which
28503 stands for no text, whereas L2R rows have no glyphs at
28504 all beyond the end of text. Treat such stretch glyphs
28505 like we do with NULL glyphs in L2R rows. */
28506 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28507 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28508 && glyph->type == STRETCH_GLYPH
28509 && glyph->avoid_cursor_p))
28510 {
28511 if (clear_mouse_face (hlinfo))
28512 cursor = No_Cursor;
28513 #ifdef HAVE_WINDOW_SYSTEM
28514 if (FRAME_WINDOW_P (f) && NILP (pointer))
28515 {
28516 if (area != TEXT_AREA)
28517 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28518 else
28519 pointer = Vvoid_text_area_pointer;
28520 }
28521 #endif
28522 goto set_cursor;
28523 }
28524
28525 pos = glyph->charpos;
28526 object = glyph->object;
28527 if (!STRINGP (object) && !BUFFERP (object))
28528 goto set_cursor;
28529
28530 /* If we get an out-of-range value, return now; avoid an error. */
28531 if (BUFFERP (object) && pos > BUF_Z (b))
28532 goto set_cursor;
28533
28534 /* Make the window's buffer temporarily current for
28535 overlays_at and compute_char_face. */
28536 obuf = current_buffer;
28537 current_buffer = b;
28538 obegv = BEGV;
28539 ozv = ZV;
28540 BEGV = BEG;
28541 ZV = Z;
28542
28543 /* Is this char mouse-active or does it have help-echo? */
28544 position = make_number (pos);
28545
28546 if (BUFFERP (object))
28547 {
28548 /* Put all the overlays we want in a vector in overlay_vec. */
28549 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28550 /* Sort overlays into increasing priority order. */
28551 noverlays = sort_overlays (overlay_vec, noverlays, w);
28552 }
28553 else
28554 noverlays = 0;
28555
28556 if (NILP (Vmouse_highlight))
28557 {
28558 clear_mouse_face (hlinfo);
28559 goto check_help_echo;
28560 }
28561
28562 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28563
28564 if (same_region)
28565 cursor = No_Cursor;
28566
28567 /* Check mouse-face highlighting. */
28568 if (! same_region
28569 /* If there exists an overlay with mouse-face overlapping
28570 the one we are currently highlighting, we have to
28571 check if we enter the overlapping overlay, and then
28572 highlight only that. */
28573 || (OVERLAYP (hlinfo->mouse_face_overlay)
28574 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28575 {
28576 /* Find the highest priority overlay with a mouse-face. */
28577 Lisp_Object overlay = Qnil;
28578 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28579 {
28580 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28581 if (!NILP (mouse_face))
28582 overlay = overlay_vec[i];
28583 }
28584
28585 /* If we're highlighting the same overlay as before, there's
28586 no need to do that again. */
28587 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28588 goto check_help_echo;
28589 hlinfo->mouse_face_overlay = overlay;
28590
28591 /* Clear the display of the old active region, if any. */
28592 if (clear_mouse_face (hlinfo))
28593 cursor = No_Cursor;
28594
28595 /* If no overlay applies, get a text property. */
28596 if (NILP (overlay))
28597 mouse_face = Fget_text_property (position, Qmouse_face, object);
28598
28599 /* Next, compute the bounds of the mouse highlighting and
28600 display it. */
28601 if (!NILP (mouse_face) && STRINGP (object))
28602 {
28603 /* The mouse-highlighting comes from a display string
28604 with a mouse-face. */
28605 Lisp_Object s, e;
28606 ptrdiff_t ignore;
28607
28608 s = Fprevious_single_property_change
28609 (make_number (pos + 1), Qmouse_face, object, Qnil);
28610 e = Fnext_single_property_change
28611 (position, Qmouse_face, object, Qnil);
28612 if (NILP (s))
28613 s = make_number (0);
28614 if (NILP (e))
28615 e = make_number (SCHARS (object));
28616 mouse_face_from_string_pos (w, hlinfo, object,
28617 XINT (s), XINT (e));
28618 hlinfo->mouse_face_past_end = 0;
28619 hlinfo->mouse_face_window = window;
28620 hlinfo->mouse_face_face_id
28621 = face_at_string_position (w, object, pos, 0, &ignore,
28622 glyph->face_id, 1);
28623 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28624 cursor = No_Cursor;
28625 }
28626 else
28627 {
28628 /* The mouse-highlighting, if any, comes from an overlay
28629 or text property in the buffer. */
28630 Lisp_Object buffer IF_LINT (= Qnil);
28631 Lisp_Object disp_string IF_LINT (= Qnil);
28632
28633 if (STRINGP (object))
28634 {
28635 /* If we are on a display string with no mouse-face,
28636 check if the text under it has one. */
28637 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28638 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28639 pos = string_buffer_position (object, start);
28640 if (pos > 0)
28641 {
28642 mouse_face = get_char_property_and_overlay
28643 (make_number (pos), Qmouse_face, w->contents, &overlay);
28644 buffer = w->contents;
28645 disp_string = object;
28646 }
28647 }
28648 else
28649 {
28650 buffer = object;
28651 disp_string = Qnil;
28652 }
28653
28654 if (!NILP (mouse_face))
28655 {
28656 Lisp_Object before, after;
28657 Lisp_Object before_string, after_string;
28658 /* To correctly find the limits of mouse highlight
28659 in a bidi-reordered buffer, we must not use the
28660 optimization of limiting the search in
28661 previous-single-property-change and
28662 next-single-property-change, because
28663 rows_from_pos_range needs the real start and end
28664 positions to DTRT in this case. That's because
28665 the first row visible in a window does not
28666 necessarily display the character whose position
28667 is the smallest. */
28668 Lisp_Object lim1
28669 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28670 ? Fmarker_position (w->start)
28671 : Qnil;
28672 Lisp_Object lim2
28673 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28674 ? make_number (BUF_Z (XBUFFER (buffer))
28675 - w->window_end_pos)
28676 : Qnil;
28677
28678 if (NILP (overlay))
28679 {
28680 /* Handle the text property case. */
28681 before = Fprevious_single_property_change
28682 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28683 after = Fnext_single_property_change
28684 (make_number (pos), Qmouse_face, buffer, lim2);
28685 before_string = after_string = Qnil;
28686 }
28687 else
28688 {
28689 /* Handle the overlay case. */
28690 before = Foverlay_start (overlay);
28691 after = Foverlay_end (overlay);
28692 before_string = Foverlay_get (overlay, Qbefore_string);
28693 after_string = Foverlay_get (overlay, Qafter_string);
28694
28695 if (!STRINGP (before_string)) before_string = Qnil;
28696 if (!STRINGP (after_string)) after_string = Qnil;
28697 }
28698
28699 mouse_face_from_buffer_pos (window, hlinfo, pos,
28700 NILP (before)
28701 ? 1
28702 : XFASTINT (before),
28703 NILP (after)
28704 ? BUF_Z (XBUFFER (buffer))
28705 : XFASTINT (after),
28706 before_string, after_string,
28707 disp_string);
28708 cursor = No_Cursor;
28709 }
28710 }
28711 }
28712
28713 check_help_echo:
28714
28715 /* Look for a `help-echo' property. */
28716 if (NILP (help_echo_string)) {
28717 Lisp_Object help, overlay;
28718
28719 /* Check overlays first. */
28720 help = overlay = Qnil;
28721 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28722 {
28723 overlay = overlay_vec[i];
28724 help = Foverlay_get (overlay, Qhelp_echo);
28725 }
28726
28727 if (!NILP (help))
28728 {
28729 help_echo_string = help;
28730 help_echo_window = window;
28731 help_echo_object = overlay;
28732 help_echo_pos = pos;
28733 }
28734 else
28735 {
28736 Lisp_Object obj = glyph->object;
28737 ptrdiff_t charpos = glyph->charpos;
28738
28739 /* Try text properties. */
28740 if (STRINGP (obj)
28741 && charpos >= 0
28742 && charpos < SCHARS (obj))
28743 {
28744 help = Fget_text_property (make_number (charpos),
28745 Qhelp_echo, obj);
28746 if (NILP (help))
28747 {
28748 /* If the string itself doesn't specify a help-echo,
28749 see if the buffer text ``under'' it does. */
28750 struct glyph_row *r
28751 = MATRIX_ROW (w->current_matrix, vpos);
28752 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28753 ptrdiff_t p = string_buffer_position (obj, start);
28754 if (p > 0)
28755 {
28756 help = Fget_char_property (make_number (p),
28757 Qhelp_echo, w->contents);
28758 if (!NILP (help))
28759 {
28760 charpos = p;
28761 obj = w->contents;
28762 }
28763 }
28764 }
28765 }
28766 else if (BUFFERP (obj)
28767 && charpos >= BEGV
28768 && charpos < ZV)
28769 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28770 obj);
28771
28772 if (!NILP (help))
28773 {
28774 help_echo_string = help;
28775 help_echo_window = window;
28776 help_echo_object = obj;
28777 help_echo_pos = charpos;
28778 }
28779 }
28780 }
28781
28782 #ifdef HAVE_WINDOW_SYSTEM
28783 /* Look for a `pointer' property. */
28784 if (FRAME_WINDOW_P (f) && NILP (pointer))
28785 {
28786 /* Check overlays first. */
28787 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28788 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28789
28790 if (NILP (pointer))
28791 {
28792 Lisp_Object obj = glyph->object;
28793 ptrdiff_t charpos = glyph->charpos;
28794
28795 /* Try text properties. */
28796 if (STRINGP (obj)
28797 && charpos >= 0
28798 && charpos < SCHARS (obj))
28799 {
28800 pointer = Fget_text_property (make_number (charpos),
28801 Qpointer, obj);
28802 if (NILP (pointer))
28803 {
28804 /* If the string itself doesn't specify a pointer,
28805 see if the buffer text ``under'' it does. */
28806 struct glyph_row *r
28807 = MATRIX_ROW (w->current_matrix, vpos);
28808 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28809 ptrdiff_t p = string_buffer_position (obj, start);
28810 if (p > 0)
28811 pointer = Fget_char_property (make_number (p),
28812 Qpointer, w->contents);
28813 }
28814 }
28815 else if (BUFFERP (obj)
28816 && charpos >= BEGV
28817 && charpos < ZV)
28818 pointer = Fget_text_property (make_number (charpos),
28819 Qpointer, obj);
28820 }
28821 }
28822 #endif /* HAVE_WINDOW_SYSTEM */
28823
28824 BEGV = obegv;
28825 ZV = ozv;
28826 current_buffer = obuf;
28827 }
28828
28829 set_cursor:
28830
28831 #ifdef HAVE_WINDOW_SYSTEM
28832 if (FRAME_WINDOW_P (f))
28833 define_frame_cursor1 (f, cursor, pointer);
28834 #else
28835 /* This is here to prevent a compiler error, about "label at end of
28836 compound statement". */
28837 return;
28838 #endif
28839 }
28840
28841
28842 /* EXPORT for RIF:
28843 Clear any mouse-face on window W. This function is part of the
28844 redisplay interface, and is called from try_window_id and similar
28845 functions to ensure the mouse-highlight is off. */
28846
28847 void
28848 x_clear_window_mouse_face (struct window *w)
28849 {
28850 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28851 Lisp_Object window;
28852
28853 block_input ();
28854 XSETWINDOW (window, w);
28855 if (EQ (window, hlinfo->mouse_face_window))
28856 clear_mouse_face (hlinfo);
28857 unblock_input ();
28858 }
28859
28860
28861 /* EXPORT:
28862 Just discard the mouse face information for frame F, if any.
28863 This is used when the size of F is changed. */
28864
28865 void
28866 cancel_mouse_face (struct frame *f)
28867 {
28868 Lisp_Object window;
28869 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28870
28871 window = hlinfo->mouse_face_window;
28872 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28873 reset_mouse_highlight (hlinfo);
28874 }
28875
28876
28877 \f
28878 /***********************************************************************
28879 Exposure Events
28880 ***********************************************************************/
28881
28882 #ifdef HAVE_WINDOW_SYSTEM
28883
28884 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28885 which intersects rectangle R. R is in window-relative coordinates. */
28886
28887 static void
28888 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28889 enum glyph_row_area area)
28890 {
28891 struct glyph *first = row->glyphs[area];
28892 struct glyph *end = row->glyphs[area] + row->used[area];
28893 struct glyph *last;
28894 int first_x, start_x, x;
28895
28896 if (area == TEXT_AREA && row->fill_line_p)
28897 /* If row extends face to end of line write the whole line. */
28898 draw_glyphs (w, 0, row, area,
28899 0, row->used[area],
28900 DRAW_NORMAL_TEXT, 0);
28901 else
28902 {
28903 /* Set START_X to the window-relative start position for drawing glyphs of
28904 AREA. The first glyph of the text area can be partially visible.
28905 The first glyphs of other areas cannot. */
28906 start_x = window_box_left_offset (w, area);
28907 x = start_x;
28908 if (area == TEXT_AREA)
28909 x += row->x;
28910
28911 /* Find the first glyph that must be redrawn. */
28912 while (first < end
28913 && x + first->pixel_width < r->x)
28914 {
28915 x += first->pixel_width;
28916 ++first;
28917 }
28918
28919 /* Find the last one. */
28920 last = first;
28921 first_x = x;
28922 while (last < end
28923 && x < r->x + r->width)
28924 {
28925 x += last->pixel_width;
28926 ++last;
28927 }
28928
28929 /* Repaint. */
28930 if (last > first)
28931 draw_glyphs (w, first_x - start_x, row, area,
28932 first - row->glyphs[area], last - row->glyphs[area],
28933 DRAW_NORMAL_TEXT, 0);
28934 }
28935 }
28936
28937
28938 /* Redraw the parts of the glyph row ROW on window W intersecting
28939 rectangle R. R is in window-relative coordinates. Value is
28940 non-zero if mouse-face was overwritten. */
28941
28942 static int
28943 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28944 {
28945 eassert (row->enabled_p);
28946
28947 if (row->mode_line_p || w->pseudo_window_p)
28948 draw_glyphs (w, 0, row, TEXT_AREA,
28949 0, row->used[TEXT_AREA],
28950 DRAW_NORMAL_TEXT, 0);
28951 else
28952 {
28953 if (row->used[LEFT_MARGIN_AREA])
28954 expose_area (w, row, r, LEFT_MARGIN_AREA);
28955 if (row->used[TEXT_AREA])
28956 expose_area (w, row, r, TEXT_AREA);
28957 if (row->used[RIGHT_MARGIN_AREA])
28958 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28959 draw_row_fringe_bitmaps (w, row);
28960 }
28961
28962 return row->mouse_face_p;
28963 }
28964
28965
28966 /* Redraw those parts of glyphs rows during expose event handling that
28967 overlap other rows. Redrawing of an exposed line writes over parts
28968 of lines overlapping that exposed line; this function fixes that.
28969
28970 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28971 row in W's current matrix that is exposed and overlaps other rows.
28972 LAST_OVERLAPPING_ROW is the last such row. */
28973
28974 static void
28975 expose_overlaps (struct window *w,
28976 struct glyph_row *first_overlapping_row,
28977 struct glyph_row *last_overlapping_row,
28978 XRectangle *r)
28979 {
28980 struct glyph_row *row;
28981
28982 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28983 if (row->overlapping_p)
28984 {
28985 eassert (row->enabled_p && !row->mode_line_p);
28986
28987 row->clip = r;
28988 if (row->used[LEFT_MARGIN_AREA])
28989 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28990
28991 if (row->used[TEXT_AREA])
28992 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28993
28994 if (row->used[RIGHT_MARGIN_AREA])
28995 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28996 row->clip = NULL;
28997 }
28998 }
28999
29000
29001 /* Return non-zero if W's cursor intersects rectangle R. */
29002
29003 static int
29004 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29005 {
29006 XRectangle cr, result;
29007 struct glyph *cursor_glyph;
29008 struct glyph_row *row;
29009
29010 if (w->phys_cursor.vpos >= 0
29011 && w->phys_cursor.vpos < w->current_matrix->nrows
29012 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29013 row->enabled_p)
29014 && row->cursor_in_fringe_p)
29015 {
29016 /* Cursor is in the fringe. */
29017 cr.x = window_box_right_offset (w,
29018 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29019 ? RIGHT_MARGIN_AREA
29020 : TEXT_AREA));
29021 cr.y = row->y;
29022 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29023 cr.height = row->height;
29024 return x_intersect_rectangles (&cr, r, &result);
29025 }
29026
29027 cursor_glyph = get_phys_cursor_glyph (w);
29028 if (cursor_glyph)
29029 {
29030 /* r is relative to W's box, but w->phys_cursor.x is relative
29031 to left edge of W's TEXT area. Adjust it. */
29032 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29033 cr.y = w->phys_cursor.y;
29034 cr.width = cursor_glyph->pixel_width;
29035 cr.height = w->phys_cursor_height;
29036 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29037 I assume the effect is the same -- and this is portable. */
29038 return x_intersect_rectangles (&cr, r, &result);
29039 }
29040 /* If we don't understand the format, pretend we're not in the hot-spot. */
29041 return 0;
29042 }
29043
29044
29045 /* EXPORT:
29046 Draw a vertical window border to the right of window W if W doesn't
29047 have vertical scroll bars. */
29048
29049 void
29050 x_draw_vertical_border (struct window *w)
29051 {
29052 struct frame *f = XFRAME (WINDOW_FRAME (w));
29053
29054 /* We could do better, if we knew what type of scroll-bar the adjacent
29055 windows (on either side) have... But we don't :-(
29056 However, I think this works ok. ++KFS 2003-04-25 */
29057
29058 /* Redraw borders between horizontally adjacent windows. Don't
29059 do it for frames with vertical scroll bars because either the
29060 right scroll bar of a window, or the left scroll bar of its
29061 neighbor will suffice as a border. */
29062 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29063 return;
29064
29065 /* Note: It is necessary to redraw both the left and the right
29066 borders, for when only this single window W is being
29067 redisplayed. */
29068 if (!WINDOW_RIGHTMOST_P (w)
29069 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29070 {
29071 int x0, x1, y0, y1;
29072
29073 window_box_edges (w, &x0, &y0, &x1, &y1);
29074 y1 -= 1;
29075
29076 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29077 x1 -= 1;
29078
29079 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29080 }
29081
29082 if (!WINDOW_LEFTMOST_P (w)
29083 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29084 {
29085 int x0, x1, y0, y1;
29086
29087 window_box_edges (w, &x0, &y0, &x1, &y1);
29088 y1 -= 1;
29089
29090 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29091 x0 -= 1;
29092
29093 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29094 }
29095 }
29096
29097
29098 /* Draw window dividers for window W. */
29099
29100 void
29101 x_draw_right_divider (struct window *w)
29102 {
29103 struct frame *f = WINDOW_XFRAME (w);
29104
29105 if (w->mini || w->pseudo_window_p)
29106 return;
29107 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29108 {
29109 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29110 int x1 = WINDOW_RIGHT_EDGE_X (w);
29111 int y0 = WINDOW_TOP_EDGE_Y (w);
29112 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29113
29114 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29115 }
29116 }
29117
29118 void
29119 x_draw_bottom_divider (struct window *w)
29120 {
29121 struct frame *f = XFRAME (WINDOW_FRAME (w));
29122
29123 if (w->mini || w->pseudo_window_p)
29124 return;
29125 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29126 {
29127 int x0 = WINDOW_LEFT_EDGE_X (w);
29128 int x1 = WINDOW_RIGHT_EDGE_X (w);
29129 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29130 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29131
29132 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29133 }
29134 }
29135
29136 /* Redraw the part of window W intersection rectangle FR. Pixel
29137 coordinates in FR are frame-relative. Call this function with
29138 input blocked. Value is non-zero if the exposure overwrites
29139 mouse-face. */
29140
29141 static int
29142 expose_window (struct window *w, XRectangle *fr)
29143 {
29144 struct frame *f = XFRAME (w->frame);
29145 XRectangle wr, r;
29146 int mouse_face_overwritten_p = 0;
29147
29148 /* If window is not yet fully initialized, do nothing. This can
29149 happen when toolkit scroll bars are used and a window is split.
29150 Reconfiguring the scroll bar will generate an expose for a newly
29151 created window. */
29152 if (w->current_matrix == NULL)
29153 return 0;
29154
29155 /* When we're currently updating the window, display and current
29156 matrix usually don't agree. Arrange for a thorough display
29157 later. */
29158 if (w->must_be_updated_p)
29159 {
29160 SET_FRAME_GARBAGED (f);
29161 return 0;
29162 }
29163
29164 /* Frame-relative pixel rectangle of W. */
29165 wr.x = WINDOW_LEFT_EDGE_X (w);
29166 wr.y = WINDOW_TOP_EDGE_Y (w);
29167 wr.width = WINDOW_PIXEL_WIDTH (w);
29168 wr.height = WINDOW_PIXEL_HEIGHT (w);
29169
29170 if (x_intersect_rectangles (fr, &wr, &r))
29171 {
29172 int yb = window_text_bottom_y (w);
29173 struct glyph_row *row;
29174 int cursor_cleared_p, phys_cursor_on_p;
29175 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29176
29177 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29178 r.x, r.y, r.width, r.height));
29179
29180 /* Convert to window coordinates. */
29181 r.x -= WINDOW_LEFT_EDGE_X (w);
29182 r.y -= WINDOW_TOP_EDGE_Y (w);
29183
29184 /* Turn off the cursor. */
29185 if (!w->pseudo_window_p
29186 && phys_cursor_in_rect_p (w, &r))
29187 {
29188 x_clear_cursor (w);
29189 cursor_cleared_p = 1;
29190 }
29191 else
29192 cursor_cleared_p = 0;
29193
29194 /* If the row containing the cursor extends face to end of line,
29195 then expose_area might overwrite the cursor outside the
29196 rectangle and thus notice_overwritten_cursor might clear
29197 w->phys_cursor_on_p. We remember the original value and
29198 check later if it is changed. */
29199 phys_cursor_on_p = w->phys_cursor_on_p;
29200
29201 /* Update lines intersecting rectangle R. */
29202 first_overlapping_row = last_overlapping_row = NULL;
29203 for (row = w->current_matrix->rows;
29204 row->enabled_p;
29205 ++row)
29206 {
29207 int y0 = row->y;
29208 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29209
29210 if ((y0 >= r.y && y0 < r.y + r.height)
29211 || (y1 > r.y && y1 < r.y + r.height)
29212 || (r.y >= y0 && r.y < y1)
29213 || (r.y + r.height > y0 && r.y + r.height < y1))
29214 {
29215 /* A header line may be overlapping, but there is no need
29216 to fix overlapping areas for them. KFS 2005-02-12 */
29217 if (row->overlapping_p && !row->mode_line_p)
29218 {
29219 if (first_overlapping_row == NULL)
29220 first_overlapping_row = row;
29221 last_overlapping_row = row;
29222 }
29223
29224 row->clip = fr;
29225 if (expose_line (w, row, &r))
29226 mouse_face_overwritten_p = 1;
29227 row->clip = NULL;
29228 }
29229 else if (row->overlapping_p)
29230 {
29231 /* We must redraw a row overlapping the exposed area. */
29232 if (y0 < r.y
29233 ? y0 + row->phys_height > r.y
29234 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29235 {
29236 if (first_overlapping_row == NULL)
29237 first_overlapping_row = row;
29238 last_overlapping_row = row;
29239 }
29240 }
29241
29242 if (y1 >= yb)
29243 break;
29244 }
29245
29246 /* Display the mode line if there is one. */
29247 if (WINDOW_WANTS_MODELINE_P (w)
29248 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29249 row->enabled_p)
29250 && row->y < r.y + r.height)
29251 {
29252 if (expose_line (w, row, &r))
29253 mouse_face_overwritten_p = 1;
29254 }
29255
29256 if (!w->pseudo_window_p)
29257 {
29258 /* Fix the display of overlapping rows. */
29259 if (first_overlapping_row)
29260 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29261 fr);
29262
29263 /* Draw border between windows. */
29264 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29265 x_draw_right_divider (w);
29266 else
29267 x_draw_vertical_border (w);
29268
29269 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29270 x_draw_bottom_divider (w);
29271
29272 /* Turn the cursor on again. */
29273 if (cursor_cleared_p
29274 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29275 update_window_cursor (w, 1);
29276 }
29277 }
29278
29279 return mouse_face_overwritten_p;
29280 }
29281
29282
29283
29284 /* Redraw (parts) of all windows in the window tree rooted at W that
29285 intersect R. R contains frame pixel coordinates. Value is
29286 non-zero if the exposure overwrites mouse-face. */
29287
29288 static int
29289 expose_window_tree (struct window *w, XRectangle *r)
29290 {
29291 struct frame *f = XFRAME (w->frame);
29292 int mouse_face_overwritten_p = 0;
29293
29294 while (w && !FRAME_GARBAGED_P (f))
29295 {
29296 if (WINDOWP (w->contents))
29297 mouse_face_overwritten_p
29298 |= expose_window_tree (XWINDOW (w->contents), r);
29299 else
29300 mouse_face_overwritten_p |= expose_window (w, r);
29301
29302 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29303 }
29304
29305 return mouse_face_overwritten_p;
29306 }
29307
29308
29309 /* EXPORT:
29310 Redisplay an exposed area of frame F. X and Y are the upper-left
29311 corner of the exposed rectangle. W and H are width and height of
29312 the exposed area. All are pixel values. W or H zero means redraw
29313 the entire frame. */
29314
29315 void
29316 expose_frame (struct frame *f, int x, int y, int w, int h)
29317 {
29318 XRectangle r;
29319 int mouse_face_overwritten_p = 0;
29320
29321 TRACE ((stderr, "expose_frame "));
29322
29323 /* No need to redraw if frame will be redrawn soon. */
29324 if (FRAME_GARBAGED_P (f))
29325 {
29326 TRACE ((stderr, " garbaged\n"));
29327 return;
29328 }
29329
29330 /* If basic faces haven't been realized yet, there is no point in
29331 trying to redraw anything. This can happen when we get an expose
29332 event while Emacs is starting, e.g. by moving another window. */
29333 if (FRAME_FACE_CACHE (f) == NULL
29334 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29335 {
29336 TRACE ((stderr, " no faces\n"));
29337 return;
29338 }
29339
29340 if (w == 0 || h == 0)
29341 {
29342 r.x = r.y = 0;
29343 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29344 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29345 }
29346 else
29347 {
29348 r.x = x;
29349 r.y = y;
29350 r.width = w;
29351 r.height = h;
29352 }
29353
29354 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29355 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29356
29357 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29358 if (WINDOWP (f->tool_bar_window))
29359 mouse_face_overwritten_p
29360 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29361 #endif
29362
29363 #ifdef HAVE_X_WINDOWS
29364 #ifndef MSDOS
29365 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29366 if (WINDOWP (f->menu_bar_window))
29367 mouse_face_overwritten_p
29368 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29369 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29370 #endif
29371 #endif
29372
29373 /* Some window managers support a focus-follows-mouse style with
29374 delayed raising of frames. Imagine a partially obscured frame,
29375 and moving the mouse into partially obscured mouse-face on that
29376 frame. The visible part of the mouse-face will be highlighted,
29377 then the WM raises the obscured frame. With at least one WM, KDE
29378 2.1, Emacs is not getting any event for the raising of the frame
29379 (even tried with SubstructureRedirectMask), only Expose events.
29380 These expose events will draw text normally, i.e. not
29381 highlighted. Which means we must redo the highlight here.
29382 Subsume it under ``we love X''. --gerd 2001-08-15 */
29383 /* Included in Windows version because Windows most likely does not
29384 do the right thing if any third party tool offers
29385 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29386 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29387 {
29388 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29389 if (f == hlinfo->mouse_face_mouse_frame)
29390 {
29391 int mouse_x = hlinfo->mouse_face_mouse_x;
29392 int mouse_y = hlinfo->mouse_face_mouse_y;
29393 clear_mouse_face (hlinfo);
29394 note_mouse_highlight (f, mouse_x, mouse_y);
29395 }
29396 }
29397 }
29398
29399
29400 /* EXPORT:
29401 Determine the intersection of two rectangles R1 and R2. Return
29402 the intersection in *RESULT. Value is non-zero if RESULT is not
29403 empty. */
29404
29405 int
29406 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29407 {
29408 XRectangle *left, *right;
29409 XRectangle *upper, *lower;
29410 int intersection_p = 0;
29411
29412 /* Rearrange so that R1 is the left-most rectangle. */
29413 if (r1->x < r2->x)
29414 left = r1, right = r2;
29415 else
29416 left = r2, right = r1;
29417
29418 /* X0 of the intersection is right.x0, if this is inside R1,
29419 otherwise there is no intersection. */
29420 if (right->x <= left->x + left->width)
29421 {
29422 result->x = right->x;
29423
29424 /* The right end of the intersection is the minimum of
29425 the right ends of left and right. */
29426 result->width = (min (left->x + left->width, right->x + right->width)
29427 - result->x);
29428
29429 /* Same game for Y. */
29430 if (r1->y < r2->y)
29431 upper = r1, lower = r2;
29432 else
29433 upper = r2, lower = r1;
29434
29435 /* The upper end of the intersection is lower.y0, if this is inside
29436 of upper. Otherwise, there is no intersection. */
29437 if (lower->y <= upper->y + upper->height)
29438 {
29439 result->y = lower->y;
29440
29441 /* The lower end of the intersection is the minimum of the lower
29442 ends of upper and lower. */
29443 result->height = (min (lower->y + lower->height,
29444 upper->y + upper->height)
29445 - result->y);
29446 intersection_p = 1;
29447 }
29448 }
29449
29450 return intersection_p;
29451 }
29452
29453 #endif /* HAVE_WINDOW_SYSTEM */
29454
29455 \f
29456 /***********************************************************************
29457 Initialization
29458 ***********************************************************************/
29459
29460 void
29461 syms_of_xdisp (void)
29462 {
29463 Vwith_echo_area_save_vector = Qnil;
29464 staticpro (&Vwith_echo_area_save_vector);
29465
29466 Vmessage_stack = Qnil;
29467 staticpro (&Vmessage_stack);
29468
29469 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29470 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29471
29472 message_dolog_marker1 = Fmake_marker ();
29473 staticpro (&message_dolog_marker1);
29474 message_dolog_marker2 = Fmake_marker ();
29475 staticpro (&message_dolog_marker2);
29476 message_dolog_marker3 = Fmake_marker ();
29477 staticpro (&message_dolog_marker3);
29478
29479 #ifdef GLYPH_DEBUG
29480 defsubr (&Sdump_frame_glyph_matrix);
29481 defsubr (&Sdump_glyph_matrix);
29482 defsubr (&Sdump_glyph_row);
29483 defsubr (&Sdump_tool_bar_row);
29484 defsubr (&Strace_redisplay);
29485 defsubr (&Strace_to_stderr);
29486 #endif
29487 #ifdef HAVE_WINDOW_SYSTEM
29488 defsubr (&Stool_bar_height);
29489 defsubr (&Slookup_image_map);
29490 #endif
29491 defsubr (&Sline_pixel_height);
29492 defsubr (&Sformat_mode_line);
29493 defsubr (&Sinvisible_p);
29494 defsubr (&Scurrent_bidi_paragraph_direction);
29495 defsubr (&Swindow_text_pixel_size);
29496 defsubr (&Smove_point_visually);
29497
29498 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29499 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29500 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29501 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29502 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29503 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29504 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29505 DEFSYM (Qeval, "eval");
29506 DEFSYM (QCdata, ":data");
29507 DEFSYM (Qdisplay, "display");
29508 DEFSYM (Qspace_width, "space-width");
29509 DEFSYM (Qraise, "raise");
29510 DEFSYM (Qslice, "slice");
29511 DEFSYM (Qspace, "space");
29512 DEFSYM (Qmargin, "margin");
29513 DEFSYM (Qpointer, "pointer");
29514 DEFSYM (Qleft_margin, "left-margin");
29515 DEFSYM (Qright_margin, "right-margin");
29516 DEFSYM (Qcenter, "center");
29517 DEFSYM (Qline_height, "line-height");
29518 DEFSYM (QCalign_to, ":align-to");
29519 DEFSYM (QCrelative_width, ":relative-width");
29520 DEFSYM (QCrelative_height, ":relative-height");
29521 DEFSYM (QCeval, ":eval");
29522 DEFSYM (QCpropertize, ":propertize");
29523 DEFSYM (QCfile, ":file");
29524 DEFSYM (Qfontified, "fontified");
29525 DEFSYM (Qfontification_functions, "fontification-functions");
29526 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29527 DEFSYM (Qescape_glyph, "escape-glyph");
29528 DEFSYM (Qnobreak_space, "nobreak-space");
29529 DEFSYM (Qimage, "image");
29530 DEFSYM (Qtext, "text");
29531 DEFSYM (Qboth, "both");
29532 DEFSYM (Qboth_horiz, "both-horiz");
29533 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29534 DEFSYM (QCmap, ":map");
29535 DEFSYM (QCpointer, ":pointer");
29536 DEFSYM (Qrect, "rect");
29537 DEFSYM (Qcircle, "circle");
29538 DEFSYM (Qpoly, "poly");
29539 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29540 DEFSYM (Qgrow_only, "grow-only");
29541 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29542 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29543 DEFSYM (Qposition, "position");
29544 DEFSYM (Qbuffer_position, "buffer-position");
29545 DEFSYM (Qobject, "object");
29546 DEFSYM (Qbar, "bar");
29547 DEFSYM (Qhbar, "hbar");
29548 DEFSYM (Qbox, "box");
29549 DEFSYM (Qhollow, "hollow");
29550 DEFSYM (Qhand, "hand");
29551 DEFSYM (Qarrow, "arrow");
29552 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29553
29554 list_of_error = list1 (list2 (intern_c_string ("error"),
29555 intern_c_string ("void-variable")));
29556 staticpro (&list_of_error);
29557
29558 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29559 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29560 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29561 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29562
29563 echo_buffer[0] = echo_buffer[1] = Qnil;
29564 staticpro (&echo_buffer[0]);
29565 staticpro (&echo_buffer[1]);
29566
29567 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29568 staticpro (&echo_area_buffer[0]);
29569 staticpro (&echo_area_buffer[1]);
29570
29571 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29572 staticpro (&Vmessages_buffer_name);
29573
29574 mode_line_proptrans_alist = Qnil;
29575 staticpro (&mode_line_proptrans_alist);
29576 mode_line_string_list = Qnil;
29577 staticpro (&mode_line_string_list);
29578 mode_line_string_face = Qnil;
29579 staticpro (&mode_line_string_face);
29580 mode_line_string_face_prop = Qnil;
29581 staticpro (&mode_line_string_face_prop);
29582 Vmode_line_unwind_vector = Qnil;
29583 staticpro (&Vmode_line_unwind_vector);
29584
29585 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29586
29587 help_echo_string = Qnil;
29588 staticpro (&help_echo_string);
29589 help_echo_object = Qnil;
29590 staticpro (&help_echo_object);
29591 help_echo_window = Qnil;
29592 staticpro (&help_echo_window);
29593 previous_help_echo_string = Qnil;
29594 staticpro (&previous_help_echo_string);
29595 help_echo_pos = -1;
29596
29597 DEFSYM (Qright_to_left, "right-to-left");
29598 DEFSYM (Qleft_to_right, "left-to-right");
29599
29600 #ifdef HAVE_WINDOW_SYSTEM
29601 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29602 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29603 For example, if a block cursor is over a tab, it will be drawn as
29604 wide as that tab on the display. */);
29605 x_stretch_cursor_p = 0;
29606 #endif
29607
29608 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29609 doc: /* Non-nil means highlight trailing whitespace.
29610 The face used for trailing whitespace is `trailing-whitespace'. */);
29611 Vshow_trailing_whitespace = Qnil;
29612
29613 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29614 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29615 If the value is t, Emacs highlights non-ASCII chars which have the
29616 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29617 or `escape-glyph' face respectively.
29618
29619 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29620 U+2011 (non-breaking hyphen) are affected.
29621
29622 Any other non-nil value means to display these characters as a escape
29623 glyph followed by an ordinary space or hyphen.
29624
29625 A value of nil means no special handling of these characters. */);
29626 Vnobreak_char_display = Qt;
29627
29628 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29629 doc: /* The pointer shape to show in void text areas.
29630 A value of nil means to show the text pointer. Other options are `arrow',
29631 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29632 Vvoid_text_area_pointer = Qarrow;
29633
29634 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29635 doc: /* Non-nil means don't actually do any redisplay.
29636 This is used for internal purposes. */);
29637 Vinhibit_redisplay = Qnil;
29638
29639 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29640 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29641 Vglobal_mode_string = Qnil;
29642
29643 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29644 doc: /* Marker for where to display an arrow on top of the buffer text.
29645 This must be the beginning of a line in order to work.
29646 See also `overlay-arrow-string'. */);
29647 Voverlay_arrow_position = Qnil;
29648
29649 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29650 doc: /* String to display as an arrow in non-window frames.
29651 See also `overlay-arrow-position'. */);
29652 Voverlay_arrow_string = build_pure_c_string ("=>");
29653
29654 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29655 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29656 The symbols on this list are examined during redisplay to determine
29657 where to display overlay arrows. */);
29658 Voverlay_arrow_variable_list
29659 = list1 (intern_c_string ("overlay-arrow-position"));
29660
29661 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29662 doc: /* The number of lines to try scrolling a window by when point moves out.
29663 If that fails to bring point back on frame, point is centered instead.
29664 If this is zero, point is always centered after it moves off frame.
29665 If you want scrolling to always be a line at a time, you should set
29666 `scroll-conservatively' to a large value rather than set this to 1. */);
29667
29668 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29669 doc: /* Scroll up to this many lines, to bring point back on screen.
29670 If point moves off-screen, redisplay will scroll by up to
29671 `scroll-conservatively' lines in order to bring point just barely
29672 onto the screen again. If that cannot be done, then redisplay
29673 recenters point as usual.
29674
29675 If the value is greater than 100, redisplay will never recenter point,
29676 but will always scroll just enough text to bring point into view, even
29677 if you move far away.
29678
29679 A value of zero means always recenter point if it moves off screen. */);
29680 scroll_conservatively = 0;
29681
29682 DEFVAR_INT ("scroll-margin", scroll_margin,
29683 doc: /* Number of lines of margin at the top and bottom of a window.
29684 Recenter the window whenever point gets within this many lines
29685 of the top or bottom of the window. */);
29686 scroll_margin = 0;
29687
29688 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29689 doc: /* Pixels per inch value for non-window system displays.
29690 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29691 Vdisplay_pixels_per_inch = make_float (72.0);
29692
29693 #ifdef GLYPH_DEBUG
29694 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29695 #endif
29696
29697 DEFVAR_LISP ("truncate-partial-width-windows",
29698 Vtruncate_partial_width_windows,
29699 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29700 For an integer value, truncate lines in each window narrower than the
29701 full frame width, provided the window width is less than that integer;
29702 otherwise, respect the value of `truncate-lines'.
29703
29704 For any other non-nil value, truncate lines in all windows that do
29705 not span the full frame width.
29706
29707 A value of nil means to respect the value of `truncate-lines'.
29708
29709 If `word-wrap' is enabled, you might want to reduce this. */);
29710 Vtruncate_partial_width_windows = make_number (50);
29711
29712 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29713 doc: /* Maximum buffer size for which line number should be displayed.
29714 If the buffer is bigger than this, the line number does not appear
29715 in the mode line. A value of nil means no limit. */);
29716 Vline_number_display_limit = Qnil;
29717
29718 DEFVAR_INT ("line-number-display-limit-width",
29719 line_number_display_limit_width,
29720 doc: /* Maximum line width (in characters) for line number display.
29721 If the average length of the lines near point is bigger than this, then the
29722 line number may be omitted from the mode line. */);
29723 line_number_display_limit_width = 200;
29724
29725 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29726 doc: /* Non-nil means highlight region even in nonselected windows. */);
29727 highlight_nonselected_windows = 0;
29728
29729 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29730 doc: /* Non-nil if more than one frame is visible on this display.
29731 Minibuffer-only frames don't count, but iconified frames do.
29732 This variable is not guaranteed to be accurate except while processing
29733 `frame-title-format' and `icon-title-format'. */);
29734
29735 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29736 doc: /* Template for displaying the title bar of visible frames.
29737 \(Assuming the window manager supports this feature.)
29738
29739 This variable has the same structure as `mode-line-format', except that
29740 the %c and %l constructs are ignored. It is used only on frames for
29741 which no explicit name has been set \(see `modify-frame-parameters'). */);
29742
29743 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29744 doc: /* Template for displaying the title bar of an iconified frame.
29745 \(Assuming the window manager supports this feature.)
29746 This variable has the same structure as `mode-line-format' (which see),
29747 and is used only on frames for which no explicit name has been set
29748 \(see `modify-frame-parameters'). */);
29749 Vicon_title_format
29750 = Vframe_title_format
29751 = listn (CONSTYPE_PURE, 3,
29752 intern_c_string ("multiple-frames"),
29753 build_pure_c_string ("%b"),
29754 listn (CONSTYPE_PURE, 4,
29755 empty_unibyte_string,
29756 intern_c_string ("invocation-name"),
29757 build_pure_c_string ("@"),
29758 intern_c_string ("system-name")));
29759
29760 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29761 doc: /* Maximum number of lines to keep in the message log buffer.
29762 If nil, disable message logging. If t, log messages but don't truncate
29763 the buffer when it becomes large. */);
29764 Vmessage_log_max = make_number (1000);
29765
29766 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29767 doc: /* Functions called before redisplay, if window sizes have changed.
29768 The value should be a list of functions that take one argument.
29769 Just before redisplay, for each frame, if any of its windows have changed
29770 size since the last redisplay, or have been split or deleted,
29771 all the functions in the list are called, with the frame as argument. */);
29772 Vwindow_size_change_functions = Qnil;
29773
29774 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29775 doc: /* List of functions to call before redisplaying a window with scrolling.
29776 Each function is called with two arguments, the window and its new
29777 display-start position. Note that these functions are also called by
29778 `set-window-buffer'. Also note that the value of `window-end' is not
29779 valid when these functions are called.
29780
29781 Warning: Do not use this feature to alter the way the window
29782 is scrolled. It is not designed for that, and such use probably won't
29783 work. */);
29784 Vwindow_scroll_functions = Qnil;
29785
29786 DEFVAR_LISP ("window-text-change-functions",
29787 Vwindow_text_change_functions,
29788 doc: /* Functions to call in redisplay when text in the window might change. */);
29789 Vwindow_text_change_functions = Qnil;
29790
29791 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29792 doc: /* Functions called when redisplay of a window reaches the end trigger.
29793 Each function is called with two arguments, the window and the end trigger value.
29794 See `set-window-redisplay-end-trigger'. */);
29795 Vredisplay_end_trigger_functions = Qnil;
29796
29797 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29798 doc: /* Non-nil means autoselect window with mouse pointer.
29799 If nil, do not autoselect windows.
29800 A positive number means delay autoselection by that many seconds: a
29801 window is autoselected only after the mouse has remained in that
29802 window for the duration of the delay.
29803 A negative number has a similar effect, but causes windows to be
29804 autoselected only after the mouse has stopped moving. \(Because of
29805 the way Emacs compares mouse events, you will occasionally wait twice
29806 that time before the window gets selected.\)
29807 Any other value means to autoselect window instantaneously when the
29808 mouse pointer enters it.
29809
29810 Autoselection selects the minibuffer only if it is active, and never
29811 unselects the minibuffer if it is active.
29812
29813 When customizing this variable make sure that the actual value of
29814 `focus-follows-mouse' matches the behavior of your window manager. */);
29815 Vmouse_autoselect_window = Qnil;
29816
29817 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29818 doc: /* Non-nil means automatically resize tool-bars.
29819 This dynamically changes the tool-bar's height to the minimum height
29820 that is needed to make all tool-bar items visible.
29821 If value is `grow-only', the tool-bar's height is only increased
29822 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29823 Vauto_resize_tool_bars = Qt;
29824
29825 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29826 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29827 auto_raise_tool_bar_buttons_p = 1;
29828
29829 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29830 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29831 make_cursor_line_fully_visible_p = 1;
29832
29833 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29834 doc: /* Border below tool-bar in pixels.
29835 If an integer, use it as the height of the border.
29836 If it is one of `internal-border-width' or `border-width', use the
29837 value of the corresponding frame parameter.
29838 Otherwise, no border is added below the tool-bar. */);
29839 Vtool_bar_border = Qinternal_border_width;
29840
29841 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29842 doc: /* Margin around tool-bar buttons in pixels.
29843 If an integer, use that for both horizontal and vertical margins.
29844 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29845 HORZ specifying the horizontal margin, and VERT specifying the
29846 vertical margin. */);
29847 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29848
29849 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29850 doc: /* Relief thickness of tool-bar buttons. */);
29851 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29852
29853 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29854 doc: /* Tool bar style to use.
29855 It can be one of
29856 image - show images only
29857 text - show text only
29858 both - show both, text below image
29859 both-horiz - show text to the right of the image
29860 text-image-horiz - show text to the left of the image
29861 any other - use system default or image if no system default.
29862
29863 This variable only affects the GTK+ toolkit version of Emacs. */);
29864 Vtool_bar_style = Qnil;
29865
29866 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29867 doc: /* Maximum number of characters a label can have to be shown.
29868 The tool bar style must also show labels for this to have any effect, see
29869 `tool-bar-style'. */);
29870 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29871
29872 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29873 doc: /* List of functions to call to fontify regions of text.
29874 Each function is called with one argument POS. Functions must
29875 fontify a region starting at POS in the current buffer, and give
29876 fontified regions the property `fontified'. */);
29877 Vfontification_functions = Qnil;
29878 Fmake_variable_buffer_local (Qfontification_functions);
29879
29880 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29881 unibyte_display_via_language_environment,
29882 doc: /* Non-nil means display unibyte text according to language environment.
29883 Specifically, this means that raw bytes in the range 160-255 decimal
29884 are displayed by converting them to the equivalent multibyte characters
29885 according to the current language environment. As a result, they are
29886 displayed according to the current fontset.
29887
29888 Note that this variable affects only how these bytes are displayed,
29889 but does not change the fact they are interpreted as raw bytes. */);
29890 unibyte_display_via_language_environment = 0;
29891
29892 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29893 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29894 If a float, it specifies a fraction of the mini-window frame's height.
29895 If an integer, it specifies a number of lines. */);
29896 Vmax_mini_window_height = make_float (0.25);
29897
29898 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29899 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29900 A value of nil means don't automatically resize mini-windows.
29901 A value of t means resize them to fit the text displayed in them.
29902 A value of `grow-only', the default, means let mini-windows grow only;
29903 they return to their normal size when the minibuffer is closed, or the
29904 echo area becomes empty. */);
29905 Vresize_mini_windows = Qgrow_only;
29906
29907 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29908 doc: /* Alist specifying how to blink the cursor off.
29909 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29910 `cursor-type' frame-parameter or variable equals ON-STATE,
29911 comparing using `equal', Emacs uses OFF-STATE to specify
29912 how to blink it off. ON-STATE and OFF-STATE are values for
29913 the `cursor-type' frame parameter.
29914
29915 If a frame's ON-STATE has no entry in this list,
29916 the frame's other specifications determine how to blink the cursor off. */);
29917 Vblink_cursor_alist = Qnil;
29918
29919 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29920 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29921 If non-nil, windows are automatically scrolled horizontally to make
29922 point visible. */);
29923 automatic_hscrolling_p = 1;
29924 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29925
29926 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29927 doc: /* How many columns away from the window edge point is allowed to get
29928 before automatic hscrolling will horizontally scroll the window. */);
29929 hscroll_margin = 5;
29930
29931 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29932 doc: /* How many columns to scroll the window when point gets too close to the edge.
29933 When point is less than `hscroll-margin' columns from the window
29934 edge, automatic hscrolling will scroll the window by the amount of columns
29935 determined by this variable. If its value is a positive integer, scroll that
29936 many columns. If it's a positive floating-point number, it specifies the
29937 fraction of the window's width to scroll. If it's nil or zero, point will be
29938 centered horizontally after the scroll. Any other value, including negative
29939 numbers, are treated as if the value were zero.
29940
29941 Automatic hscrolling always moves point outside the scroll margin, so if
29942 point was more than scroll step columns inside the margin, the window will
29943 scroll more than the value given by the scroll step.
29944
29945 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29946 and `scroll-right' overrides this variable's effect. */);
29947 Vhscroll_step = make_number (0);
29948
29949 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29950 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29951 Bind this around calls to `message' to let it take effect. */);
29952 message_truncate_lines = 0;
29953
29954 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29955 doc: /* Normal hook run to update the menu bar definitions.
29956 Redisplay runs this hook before it redisplays the menu bar.
29957 This is used to update submenus such as Buffers,
29958 whose contents depend on various data. */);
29959 Vmenu_bar_update_hook = Qnil;
29960
29961 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29962 doc: /* Frame for which we are updating a menu.
29963 The enable predicate for a menu binding should check this variable. */);
29964 Vmenu_updating_frame = Qnil;
29965
29966 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29967 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29968 inhibit_menubar_update = 0;
29969
29970 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29971 doc: /* Prefix prepended to all continuation lines at display time.
29972 The value may be a string, an image, or a stretch-glyph; it is
29973 interpreted in the same way as the value of a `display' text property.
29974
29975 This variable is overridden by any `wrap-prefix' text or overlay
29976 property.
29977
29978 To add a prefix to non-continuation lines, use `line-prefix'. */);
29979 Vwrap_prefix = Qnil;
29980 DEFSYM (Qwrap_prefix, "wrap-prefix");
29981 Fmake_variable_buffer_local (Qwrap_prefix);
29982
29983 DEFVAR_LISP ("line-prefix", Vline_prefix,
29984 doc: /* Prefix prepended to all non-continuation lines at display time.
29985 The value may be a string, an image, or a stretch-glyph; it is
29986 interpreted in the same way as the value of a `display' text property.
29987
29988 This variable is overridden by any `line-prefix' text or overlay
29989 property.
29990
29991 To add a prefix to continuation lines, use `wrap-prefix'. */);
29992 Vline_prefix = Qnil;
29993 DEFSYM (Qline_prefix, "line-prefix");
29994 Fmake_variable_buffer_local (Qline_prefix);
29995
29996 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29997 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29998 inhibit_eval_during_redisplay = 0;
29999
30000 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30001 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30002 inhibit_free_realized_faces = 0;
30003
30004 #ifdef GLYPH_DEBUG
30005 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30006 doc: /* Inhibit try_window_id display optimization. */);
30007 inhibit_try_window_id = 0;
30008
30009 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30010 doc: /* Inhibit try_window_reusing display optimization. */);
30011 inhibit_try_window_reusing = 0;
30012
30013 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30014 doc: /* Inhibit try_cursor_movement display optimization. */);
30015 inhibit_try_cursor_movement = 0;
30016 #endif /* GLYPH_DEBUG */
30017
30018 DEFVAR_INT ("overline-margin", overline_margin,
30019 doc: /* Space between overline and text, in pixels.
30020 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30021 margin to the character height. */);
30022 overline_margin = 2;
30023
30024 DEFVAR_INT ("underline-minimum-offset",
30025 underline_minimum_offset,
30026 doc: /* Minimum distance between baseline and underline.
30027 This can improve legibility of underlined text at small font sizes,
30028 particularly when using variable `x-use-underline-position-properties'
30029 with fonts that specify an UNDERLINE_POSITION relatively close to the
30030 baseline. The default value is 1. */);
30031 underline_minimum_offset = 1;
30032
30033 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30034 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30035 This feature only works when on a window system that can change
30036 cursor shapes. */);
30037 display_hourglass_p = 1;
30038
30039 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30040 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30041 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30042
30043 #ifdef HAVE_WINDOW_SYSTEM
30044 hourglass_atimer = NULL;
30045 hourglass_shown_p = 0;
30046 #endif /* HAVE_WINDOW_SYSTEM */
30047
30048 DEFSYM (Qglyphless_char, "glyphless-char");
30049 DEFSYM (Qhex_code, "hex-code");
30050 DEFSYM (Qempty_box, "empty-box");
30051 DEFSYM (Qthin_space, "thin-space");
30052 DEFSYM (Qzero_width, "zero-width");
30053
30054 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30055 doc: /* Function run just before redisplay.
30056 It is called with one argument, which is the set of windows that are to
30057 be redisplayed. This set can be nil (meaning, only the selected window),
30058 or t (meaning all windows). */);
30059 Vpre_redisplay_function = intern ("ignore");
30060
30061 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30062 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30063
30064 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30065 doc: /* Char-table defining glyphless characters.
30066 Each element, if non-nil, should be one of the following:
30067 an ASCII acronym string: display this string in a box
30068 `hex-code': display the hexadecimal code of a character in a box
30069 `empty-box': display as an empty box
30070 `thin-space': display as 1-pixel width space
30071 `zero-width': don't display
30072 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30073 display method for graphical terminals and text terminals respectively.
30074 GRAPHICAL and TEXT should each have one of the values listed above.
30075
30076 The char-table has one extra slot to control the display of a character for
30077 which no font is found. This slot only takes effect on graphical terminals.
30078 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30079 `thin-space'. The default is `empty-box'.
30080
30081 If a character has a non-nil entry in an active display table, the
30082 display table takes effect; in this case, Emacs does not consult
30083 `glyphless-char-display' at all. */);
30084 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30085 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30086 Qempty_box);
30087
30088 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30089 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30090 Vdebug_on_message = Qnil;
30091
30092 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30093 doc: /* */);
30094 Vredisplay__all_windows_cause
30095 = Fmake_vector (make_number (100), make_number (0));
30096
30097 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30098 doc: /* */);
30099 Vredisplay__mode_lines_cause
30100 = Fmake_vector (make_number (100), make_number (0));
30101 }
30102
30103
30104 /* Initialize this module when Emacs starts. */
30105
30106 void
30107 init_xdisp (void)
30108 {
30109 CHARPOS (this_line_start_pos) = 0;
30110
30111 if (!noninteractive)
30112 {
30113 struct window *m = XWINDOW (minibuf_window);
30114 Lisp_Object frame = m->frame;
30115 struct frame *f = XFRAME (frame);
30116 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30117 struct window *r = XWINDOW (root);
30118 int i;
30119
30120 echo_area_window = minibuf_window;
30121
30122 r->top_line = FRAME_TOP_MARGIN (f);
30123 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30124 r->total_cols = FRAME_COLS (f);
30125 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30126 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30127 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30128
30129 m->top_line = FRAME_LINES (f) - 1;
30130 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30131 m->total_cols = FRAME_COLS (f);
30132 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30133 m->total_lines = 1;
30134 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30135
30136 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30137 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30138 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30139
30140 /* The default ellipsis glyphs `...'. */
30141 for (i = 0; i < 3; ++i)
30142 default_invis_vector[i] = make_number ('.');
30143 }
30144
30145 {
30146 /* Allocate the buffer for frame titles.
30147 Also used for `format-mode-line'. */
30148 int size = 100;
30149 mode_line_noprop_buf = xmalloc (size);
30150 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30151 mode_line_noprop_ptr = mode_line_noprop_buf;
30152 mode_line_target = MODE_LINE_DISPLAY;
30153 }
30154
30155 help_echo_showing_p = 0;
30156 }
30157
30158 #ifdef HAVE_WINDOW_SYSTEM
30159
30160 /* Platform-independent portion of hourglass implementation. */
30161
30162 /* Cancel a currently active hourglass timer, and start a new one. */
30163 void
30164 start_hourglass (void)
30165 {
30166 struct timespec delay;
30167
30168 cancel_hourglass ();
30169
30170 if (INTEGERP (Vhourglass_delay)
30171 && XINT (Vhourglass_delay) > 0)
30172 delay = make_timespec (min (XINT (Vhourglass_delay),
30173 TYPE_MAXIMUM (time_t)),
30174 0);
30175 else if (FLOATP (Vhourglass_delay)
30176 && XFLOAT_DATA (Vhourglass_delay) > 0)
30177 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30178 else
30179 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30180
30181 #ifdef HAVE_NTGUI
30182 {
30183 extern void w32_note_current_window (void);
30184 w32_note_current_window ();
30185 }
30186 #endif /* HAVE_NTGUI */
30187
30188 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30189 show_hourglass, NULL);
30190 }
30191
30192
30193 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30194 shown. */
30195 void
30196 cancel_hourglass (void)
30197 {
30198 if (hourglass_atimer)
30199 {
30200 cancel_atimer (hourglass_atimer);
30201 hourglass_atimer = NULL;
30202 }
30203
30204 if (hourglass_shown_p)
30205 hide_hourglass ();
30206 }
30207
30208 #endif /* HAVE_WINDOW_SYSTEM */