]> code.delx.au - gnu-emacs/blob - src/xdisp.c
src/xdisp.c (pos_visible_p): Don't assign a boolean value to an int var.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2014 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
104
105 . try_window
106
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
112
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
117
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
130
131 Desired matrices.
132
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
139
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
146
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
156
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
163
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
175
176 Frame matrices.
177
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
184
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
196
197 Bidirectional display.
198
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
211
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
220
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
239
240 Bidirectional display and character compositions
241
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
246
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
266
267 Moving an iterator in bidirectional text
268 without producing glyphs
269
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
288
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
292
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "keyboard.h"
296 #include "frame.h"
297 #include "window.h"
298 #include "termchar.h"
299 #include "dispextern.h"
300 #include "character.h"
301 #include "buffer.h"
302 #include "charset.h"
303 #include "indent.h"
304 #include "commands.h"
305 #include "keymap.h"
306 #include "macros.h"
307 #include "disptab.h"
308 #include "termhooks.h"
309 #include "termopts.h"
310 #include "intervals.h"
311 #include "coding.h"
312 #include "process.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
320
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
324
325 #define INFINITY 10000000
326
327 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
328 Lisp_Object Qwindow_scroll_functions;
329 static Lisp_Object Qwindow_text_change_functions;
330 static Lisp_Object Qredisplay_end_trigger_functions;
331 Lisp_Object Qinhibit_point_motion_hooks;
332 static Lisp_Object QCeval, QCpropertize;
333 Lisp_Object QCfile, QCdata;
334 static Lisp_Object Qfontified;
335 static Lisp_Object Qgrow_only;
336 static Lisp_Object Qinhibit_eval_during_redisplay;
337 static Lisp_Object Qbuffer_position, Qposition, Qobject;
338 static Lisp_Object Qright_to_left, Qleft_to_right;
339
340 /* Cursor shapes. */
341 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
342
343 /* Pointer shapes. */
344 static Lisp_Object Qarrow, Qhand;
345 Lisp_Object Qtext;
346
347 /* Holds the list (error). */
348 static Lisp_Object list_of_error;
349
350 static Lisp_Object Qfontification_functions;
351
352 static Lisp_Object Qwrap_prefix;
353 static Lisp_Object Qline_prefix;
354 static Lisp_Object Qredisplay_internal;
355
356 /* Non-nil means don't actually do any redisplay. */
357
358 Lisp_Object Qinhibit_redisplay;
359
360 /* Names of text properties relevant for redisplay. */
361
362 Lisp_Object Qdisplay;
363
364 Lisp_Object Qspace, QCalign_to;
365 static Lisp_Object QCrelative_width, QCrelative_height;
366 Lisp_Object Qleft_margin, Qright_margin;
367 static Lisp_Object Qspace_width, Qraise;
368 static Lisp_Object Qslice;
369 Lisp_Object Qcenter;
370 static Lisp_Object Qmargin, Qpointer;
371 static Lisp_Object Qline_height;
372
373 #ifdef HAVE_WINDOW_SYSTEM
374
375 /* Test if overflow newline into fringe. Called with iterator IT
376 at or past right window margin, and with IT->current_x set. */
377
378 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
379 (!NILP (Voverflow_newline_into_fringe) \
380 && FRAME_WINDOW_P ((IT)->f) \
381 && ((IT)->bidi_it.paragraph_dir == R2L \
382 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
383 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
384 && (IT)->current_x == (IT)->last_visible_x)
385
386 #else /* !HAVE_WINDOW_SYSTEM */
387 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
388 #endif /* HAVE_WINDOW_SYSTEM */
389
390 /* Test if the display element loaded in IT, or the underlying buffer
391 or string character, is a space or a TAB character. This is used
392 to determine where word wrapping can occur. */
393
394 #define IT_DISPLAYING_WHITESPACE(it) \
395 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
396 || ((STRINGP (it->string) \
397 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
398 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
399 || (it->s \
400 && (it->s[IT_BYTEPOS (*it)] == ' ' \
401 || it->s[IT_BYTEPOS (*it)] == '\t')) \
402 || (IT_BYTEPOS (*it) < ZV_BYTE \
403 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
404 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
405
406 /* Name of the face used to highlight trailing whitespace. */
407
408 static Lisp_Object Qtrailing_whitespace;
409
410 /* Name and number of the face used to highlight escape glyphs. */
411
412 static Lisp_Object Qescape_glyph;
413
414 /* Name and number of the face used to highlight non-breaking spaces. */
415
416 static Lisp_Object Qnobreak_space;
417
418 /* The symbol `image' which is the car of the lists used to represent
419 images in Lisp. Also a tool bar style. */
420
421 Lisp_Object Qimage;
422
423 /* The image map types. */
424 Lisp_Object QCmap;
425 static Lisp_Object QCpointer;
426 static Lisp_Object Qrect, Qcircle, Qpoly;
427
428 /* Tool bar styles */
429 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
430
431 /* Non-zero means print newline to stdout before next mini-buffer
432 message. */
433
434 bool noninteractive_need_newline;
435
436 /* Non-zero means print newline to message log before next message. */
437
438 static bool message_log_need_newline;
439
440 /* Three markers that message_dolog uses.
441 It could allocate them itself, but that causes trouble
442 in handling memory-full errors. */
443 static Lisp_Object message_dolog_marker1;
444 static Lisp_Object message_dolog_marker2;
445 static Lisp_Object message_dolog_marker3;
446 \f
447 /* The buffer position of the first character appearing entirely or
448 partially on the line of the selected window which contains the
449 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
450 redisplay optimization in redisplay_internal. */
451
452 static struct text_pos this_line_start_pos;
453
454 /* Number of characters past the end of the line above, including the
455 terminating newline. */
456
457 static struct text_pos this_line_end_pos;
458
459 /* The vertical positions and the height of this line. */
460
461 static int this_line_vpos;
462 static int this_line_y;
463 static int this_line_pixel_height;
464
465 /* X position at which this display line starts. Usually zero;
466 negative if first character is partially visible. */
467
468 static int this_line_start_x;
469
470 /* The smallest character position seen by move_it_* functions as they
471 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
472 hscrolled lines, see display_line. */
473
474 static struct text_pos this_line_min_pos;
475
476 /* Buffer that this_line_.* variables are referring to. */
477
478 static struct buffer *this_line_buffer;
479
480
481 /* Values of those variables at last redisplay are stored as
482 properties on `overlay-arrow-position' symbol. However, if
483 Voverlay_arrow_position is a marker, last-arrow-position is its
484 numerical position. */
485
486 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
487
488 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
489 properties on a symbol in overlay-arrow-variable-list. */
490
491 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
492
493 Lisp_Object Qmenu_bar_update_hook;
494
495 /* Nonzero if an overlay arrow has been displayed in this window. */
496
497 static bool overlay_arrow_seen;
498
499 /* Vector containing glyphs for an ellipsis `...'. */
500
501 static Lisp_Object default_invis_vector[3];
502
503 /* This is the window where the echo area message was displayed. It
504 is always a mini-buffer window, but it may not be the same window
505 currently active as a mini-buffer. */
506
507 Lisp_Object echo_area_window;
508
509 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
510 pushes the current message and the value of
511 message_enable_multibyte on the stack, the function restore_message
512 pops the stack and displays MESSAGE again. */
513
514 static Lisp_Object Vmessage_stack;
515
516 /* Nonzero means multibyte characters were enabled when the echo area
517 message was specified. */
518
519 static bool message_enable_multibyte;
520
521 /* Nonzero if we should redraw the mode lines on the next redisplay.
522 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
523 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
524 (the number used is then only used to track down the cause for this
525 full-redisplay). */
526
527 int update_mode_lines;
528
529 /* Nonzero if window sizes or contents other than selected-window have changed
530 since last redisplay that finished.
531 If it has value REDISPLAY_SOME, then only redisplay the windows where
532 the `redisplay' bit has been set. Otherwise, redisplay all windows
533 (the number used is then only used to track down the cause for this
534 full-redisplay). */
535
536 int windows_or_buffers_changed;
537
538 /* Nonzero after display_mode_line if %l was used and it displayed a
539 line number. */
540
541 static bool line_number_displayed;
542
543 /* The name of the *Messages* buffer, a string. */
544
545 static Lisp_Object Vmessages_buffer_name;
546
547 /* Current, index 0, and last displayed echo area message. Either
548 buffers from echo_buffers, or nil to indicate no message. */
549
550 Lisp_Object echo_area_buffer[2];
551
552 /* The buffers referenced from echo_area_buffer. */
553
554 static Lisp_Object echo_buffer[2];
555
556 /* A vector saved used in with_area_buffer to reduce consing. */
557
558 static Lisp_Object Vwith_echo_area_save_vector;
559
560 /* Non-zero means display_echo_area should display the last echo area
561 message again. Set by redisplay_preserve_echo_area. */
562
563 static bool display_last_displayed_message_p;
564
565 /* Nonzero if echo area is being used by print; zero if being used by
566 message. */
567
568 static bool message_buf_print;
569
570 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
571
572 static Lisp_Object Qinhibit_menubar_update;
573 static Lisp_Object Qmessage_truncate_lines;
574
575 /* Set to 1 in clear_message to make redisplay_internal aware
576 of an emptied echo area. */
577
578 static bool message_cleared_p;
579
580 /* A scratch glyph row with contents used for generating truncation
581 glyphs. Also used in direct_output_for_insert. */
582
583 #define MAX_SCRATCH_GLYPHS 100
584 static struct glyph_row scratch_glyph_row;
585 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
586
587 /* Ascent and height of the last line processed by move_it_to. */
588
589 static int last_height;
590
591 /* Non-zero if there's a help-echo in the echo area. */
592
593 bool help_echo_showing_p;
594
595 /* The maximum distance to look ahead for text properties. Values
596 that are too small let us call compute_char_face and similar
597 functions too often which is expensive. Values that are too large
598 let us call compute_char_face and alike too often because we
599 might not be interested in text properties that far away. */
600
601 #define TEXT_PROP_DISTANCE_LIMIT 100
602
603 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
604 iterator state and later restore it. This is needed because the
605 bidi iterator on bidi.c keeps a stacked cache of its states, which
606 is really a singleton. When we use scratch iterator objects to
607 move around the buffer, we can cause the bidi cache to be pushed or
608 popped, and therefore we need to restore the cache state when we
609 return to the original iterator. */
610 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
611 do { \
612 if (CACHE) \
613 bidi_unshelve_cache (CACHE, 1); \
614 ITCOPY = ITORIG; \
615 CACHE = bidi_shelve_cache (); \
616 } while (0)
617
618 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
619 do { \
620 if (pITORIG != pITCOPY) \
621 *(pITORIG) = *(pITCOPY); \
622 bidi_unshelve_cache (CACHE, 0); \
623 CACHE = NULL; \
624 } while (0)
625
626 /* Functions to mark elements as needing redisplay. */
627 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
628
629 void
630 redisplay_other_windows (void)
631 {
632 if (!windows_or_buffers_changed)
633 windows_or_buffers_changed = REDISPLAY_SOME;
634 }
635
636 void
637 wset_redisplay (struct window *w)
638 {
639 /* Beware: selected_window can be nil during early stages. */
640 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
641 redisplay_other_windows ();
642 w->redisplay = true;
643 }
644
645 void
646 fset_redisplay (struct frame *f)
647 {
648 redisplay_other_windows ();
649 f->redisplay = true;
650 }
651
652 void
653 bset_redisplay (struct buffer *b)
654 {
655 int count = buffer_window_count (b);
656 if (count > 0)
657 {
658 /* ... it's visible in other window than selected, */
659 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
660 redisplay_other_windows ();
661 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
662 so that if we later set windows_or_buffers_changed, this buffer will
663 not be omitted. */
664 b->text->redisplay = true;
665 }
666 }
667
668 void
669 bset_update_mode_line (struct buffer *b)
670 {
671 if (!update_mode_lines)
672 update_mode_lines = REDISPLAY_SOME;
673 b->text->redisplay = true;
674 }
675
676 #ifdef GLYPH_DEBUG
677
678 /* Non-zero means print traces of redisplay if compiled with
679 GLYPH_DEBUG defined. */
680
681 bool trace_redisplay_p;
682
683 #endif /* GLYPH_DEBUG */
684
685 #ifdef DEBUG_TRACE_MOVE
686 /* Non-zero means trace with TRACE_MOVE to stderr. */
687 int trace_move;
688
689 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
690 #else
691 #define TRACE_MOVE(x) (void) 0
692 #endif
693
694 static Lisp_Object Qauto_hscroll_mode;
695
696 /* Buffer being redisplayed -- for redisplay_window_error. */
697
698 static struct buffer *displayed_buffer;
699
700 /* Value returned from text property handlers (see below). */
701
702 enum prop_handled
703 {
704 HANDLED_NORMALLY,
705 HANDLED_RECOMPUTE_PROPS,
706 HANDLED_OVERLAY_STRING_CONSUMED,
707 HANDLED_RETURN
708 };
709
710 /* A description of text properties that redisplay is interested
711 in. */
712
713 struct props
714 {
715 /* The name of the property. */
716 Lisp_Object *name;
717
718 /* A unique index for the property. */
719 enum prop_idx idx;
720
721 /* A handler function called to set up iterator IT from the property
722 at IT's current position. Value is used to steer handle_stop. */
723 enum prop_handled (*handler) (struct it *it);
724 };
725
726 static enum prop_handled handle_face_prop (struct it *);
727 static enum prop_handled handle_invisible_prop (struct it *);
728 static enum prop_handled handle_display_prop (struct it *);
729 static enum prop_handled handle_composition_prop (struct it *);
730 static enum prop_handled handle_overlay_change (struct it *);
731 static enum prop_handled handle_fontified_prop (struct it *);
732
733 /* Properties handled by iterators. */
734
735 static struct props it_props[] =
736 {
737 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
738 /* Handle `face' before `display' because some sub-properties of
739 `display' need to know the face. */
740 {&Qface, FACE_PROP_IDX, handle_face_prop},
741 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
742 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
743 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
744 {NULL, 0, NULL}
745 };
746
747 /* Value is the position described by X. If X is a marker, value is
748 the marker_position of X. Otherwise, value is X. */
749
750 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
751
752 /* Enumeration returned by some move_it_.* functions internally. */
753
754 enum move_it_result
755 {
756 /* Not used. Undefined value. */
757 MOVE_UNDEFINED,
758
759 /* Move ended at the requested buffer position or ZV. */
760 MOVE_POS_MATCH_OR_ZV,
761
762 /* Move ended at the requested X pixel position. */
763 MOVE_X_REACHED,
764
765 /* Move within a line ended at the end of a line that must be
766 continued. */
767 MOVE_LINE_CONTINUED,
768
769 /* Move within a line ended at the end of a line that would
770 be displayed truncated. */
771 MOVE_LINE_TRUNCATED,
772
773 /* Move within a line ended at a line end. */
774 MOVE_NEWLINE_OR_CR
775 };
776
777 /* This counter is used to clear the face cache every once in a while
778 in redisplay_internal. It is incremented for each redisplay.
779 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
780 cleared. */
781
782 #define CLEAR_FACE_CACHE_COUNT 500
783 static int clear_face_cache_count;
784
785 /* Similarly for the image cache. */
786
787 #ifdef HAVE_WINDOW_SYSTEM
788 #define CLEAR_IMAGE_CACHE_COUNT 101
789 static int clear_image_cache_count;
790
791 /* Null glyph slice */
792 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
793 #endif
794
795 /* True while redisplay_internal is in progress. */
796
797 bool redisplaying_p;
798
799 static Lisp_Object Qinhibit_free_realized_faces;
800 static Lisp_Object Qmode_line_default_help_echo;
801
802 /* If a string, XTread_socket generates an event to display that string.
803 (The display is done in read_char.) */
804
805 Lisp_Object help_echo_string;
806 Lisp_Object help_echo_window;
807 Lisp_Object help_echo_object;
808 ptrdiff_t help_echo_pos;
809
810 /* Temporary variable for XTread_socket. */
811
812 Lisp_Object previous_help_echo_string;
813
814 /* Platform-independent portion of hourglass implementation. */
815
816 #ifdef HAVE_WINDOW_SYSTEM
817
818 /* Non-zero means an hourglass cursor is currently shown. */
819 bool hourglass_shown_p;
820
821 /* If non-null, an asynchronous timer that, when it expires, displays
822 an hourglass cursor on all frames. */
823 struct atimer *hourglass_atimer;
824
825 #endif /* HAVE_WINDOW_SYSTEM */
826
827 /* Name of the face used to display glyphless characters. */
828 static Lisp_Object Qglyphless_char;
829
830 /* Symbol for the purpose of Vglyphless_char_display. */
831 static Lisp_Object Qglyphless_char_display;
832
833 /* Method symbols for Vglyphless_char_display. */
834 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
835
836 /* Default number of seconds to wait before displaying an hourglass
837 cursor. */
838 #define DEFAULT_HOURGLASS_DELAY 1
839
840 #ifdef HAVE_WINDOW_SYSTEM
841
842 /* Default pixel width of `thin-space' display method. */
843 #define THIN_SPACE_WIDTH 1
844
845 #endif /* HAVE_WINDOW_SYSTEM */
846
847 /* Function prototypes. */
848
849 static void setup_for_ellipsis (struct it *, int);
850 static void set_iterator_to_next (struct it *, int);
851 static void mark_window_display_accurate_1 (struct window *, int);
852 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
853 static int display_prop_string_p (Lisp_Object, Lisp_Object);
854 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
855 static int cursor_row_p (struct glyph_row *);
856 static int redisplay_mode_lines (Lisp_Object, bool);
857 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
858
859 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
860
861 static void handle_line_prefix (struct it *);
862
863 static void pint2str (char *, int, ptrdiff_t);
864 static void pint2hrstr (char *, int, ptrdiff_t);
865 static struct text_pos run_window_scroll_functions (Lisp_Object,
866 struct text_pos);
867 static int text_outside_line_unchanged_p (struct window *,
868 ptrdiff_t, ptrdiff_t);
869 static void store_mode_line_noprop_char (char);
870 static int store_mode_line_noprop (const char *, int, int);
871 static void handle_stop (struct it *);
872 static void handle_stop_backwards (struct it *, ptrdiff_t);
873 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
874 static void ensure_echo_area_buffers (void);
875 static void unwind_with_echo_area_buffer (Lisp_Object);
876 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
877 static int with_echo_area_buffer (struct window *, int,
878 int (*) (ptrdiff_t, Lisp_Object),
879 ptrdiff_t, Lisp_Object);
880 static void clear_garbaged_frames (void);
881 static int current_message_1 (ptrdiff_t, Lisp_Object);
882 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
883 static void set_message (Lisp_Object);
884 static int set_message_1 (ptrdiff_t, Lisp_Object);
885 static int display_echo_area (struct window *);
886 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
887 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
888 static void unwind_redisplay (void);
889 static int string_char_and_length (const unsigned char *, int *);
890 static struct text_pos display_prop_end (struct it *, Lisp_Object,
891 struct text_pos);
892 static int compute_window_start_on_continuation_line (struct window *);
893 static void insert_left_trunc_glyphs (struct it *);
894 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
895 Lisp_Object);
896 static void extend_face_to_end_of_line (struct it *);
897 static int append_space_for_newline (struct it *, int);
898 static int cursor_row_fully_visible_p (struct window *, int, int);
899 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
900 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
901 static int trailing_whitespace_p (ptrdiff_t);
902 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
903 static void push_it (struct it *, struct text_pos *);
904 static void iterate_out_of_display_property (struct it *);
905 static void pop_it (struct it *);
906 static void sync_frame_with_window_matrix_rows (struct window *);
907 static void redisplay_internal (void);
908 static int echo_area_display (int);
909 static void redisplay_windows (Lisp_Object);
910 static void redisplay_window (Lisp_Object, bool);
911 static Lisp_Object redisplay_window_error (Lisp_Object);
912 static Lisp_Object redisplay_window_0 (Lisp_Object);
913 static Lisp_Object redisplay_window_1 (Lisp_Object);
914 static int set_cursor_from_row (struct window *, struct glyph_row *,
915 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
916 int, int);
917 static int update_menu_bar (struct frame *, int, int);
918 static int try_window_reusing_current_matrix (struct window *);
919 static int try_window_id (struct window *);
920 static int display_line (struct it *);
921 static int display_mode_lines (struct window *);
922 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
923 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
924 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
925 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
926 static void display_menu_bar (struct window *);
927 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
928 ptrdiff_t *);
929 static int display_string (const char *, Lisp_Object, Lisp_Object,
930 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
931 static void compute_line_metrics (struct it *);
932 static void run_redisplay_end_trigger_hook (struct it *);
933 static int get_overlay_strings (struct it *, ptrdiff_t);
934 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
935 static void next_overlay_string (struct it *);
936 static void reseat (struct it *, struct text_pos, int);
937 static void reseat_1 (struct it *, struct text_pos, int);
938 static void back_to_previous_visible_line_start (struct it *);
939 static void reseat_at_next_visible_line_start (struct it *, int);
940 static int next_element_from_ellipsis (struct it *);
941 static int next_element_from_display_vector (struct it *);
942 static int next_element_from_string (struct it *);
943 static int next_element_from_c_string (struct it *);
944 static int next_element_from_buffer (struct it *);
945 static int next_element_from_composition (struct it *);
946 static int next_element_from_image (struct it *);
947 static int next_element_from_stretch (struct it *);
948 static void load_overlay_strings (struct it *, ptrdiff_t);
949 static int init_from_display_pos (struct it *, struct window *,
950 struct display_pos *);
951 static void reseat_to_string (struct it *, const char *,
952 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
953 static int get_next_display_element (struct it *);
954 static enum move_it_result
955 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
956 enum move_operation_enum);
957 static void get_visually_first_element (struct it *);
958 static void init_to_row_start (struct it *, struct window *,
959 struct glyph_row *);
960 static int init_to_row_end (struct it *, struct window *,
961 struct glyph_row *);
962 static void back_to_previous_line_start (struct it *);
963 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
964 static struct text_pos string_pos_nchars_ahead (struct text_pos,
965 Lisp_Object, ptrdiff_t);
966 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
967 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
968 static ptrdiff_t number_of_chars (const char *, bool);
969 static void compute_stop_pos (struct it *);
970 static void compute_string_pos (struct text_pos *, struct text_pos,
971 Lisp_Object);
972 static int face_before_or_after_it_pos (struct it *, int);
973 static ptrdiff_t next_overlay_change (ptrdiff_t);
974 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
975 Lisp_Object, struct text_pos *, ptrdiff_t, int);
976 static int handle_single_display_spec (struct it *, Lisp_Object,
977 Lisp_Object, Lisp_Object,
978 struct text_pos *, ptrdiff_t, int, int);
979 static int underlying_face_id (struct it *);
980 static int in_ellipses_for_invisible_text_p (struct display_pos *,
981 struct window *);
982
983 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
984 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
985
986 #ifdef HAVE_WINDOW_SYSTEM
987
988 static void x_consider_frame_title (Lisp_Object);
989 static void update_tool_bar (struct frame *, int);
990 static int redisplay_tool_bar (struct frame *);
991 static void x_draw_bottom_divider (struct window *w);
992 static void notice_overwritten_cursor (struct window *,
993 enum glyph_row_area,
994 int, int, int, int);
995 static void append_stretch_glyph (struct it *, Lisp_Object,
996 int, int, int);
997
998
999 #endif /* HAVE_WINDOW_SYSTEM */
1000
1001 static void produce_special_glyphs (struct it *, enum display_element_type);
1002 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
1003 static bool coords_in_mouse_face_p (struct window *, int, int);
1004
1005
1006 \f
1007 /***********************************************************************
1008 Window display dimensions
1009 ***********************************************************************/
1010
1011 /* Return the bottom boundary y-position for text lines in window W.
1012 This is the first y position at which a line cannot start.
1013 It is relative to the top of the window.
1014
1015 This is the height of W minus the height of a mode line, if any. */
1016
1017 int
1018 window_text_bottom_y (struct window *w)
1019 {
1020 int height = WINDOW_PIXEL_HEIGHT (w);
1021
1022 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 height -= CURRENT_MODE_LINE_HEIGHT (w);
1026
1027 return height;
1028 }
1029
1030 /* Return the pixel width of display area AREA of window W.
1031 ANY_AREA means return the total width of W, not including
1032 fringes to the left and right of the window. */
1033
1034 int
1035 window_box_width (struct window *w, enum glyph_row_area area)
1036 {
1037 int width = w->pixel_width;
1038
1039 if (!w->pseudo_window_p)
1040 {
1041 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1042 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1043
1044 if (area == TEXT_AREA)
1045 width -= (WINDOW_MARGINS_WIDTH (w)
1046 + WINDOW_FRINGES_WIDTH (w));
1047 else if (area == LEFT_MARGIN_AREA)
1048 width = WINDOW_LEFT_MARGIN_WIDTH (w);
1049 else if (area == RIGHT_MARGIN_AREA)
1050 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
1051 }
1052
1053 /* With wide margins, fringes, etc. we might end up with a negative
1054 width, correct that here. */
1055 return max (0, width);
1056 }
1057
1058
1059 /* Return the pixel height of the display area of window W, not
1060 including mode lines of W, if any. */
1061
1062 int
1063 window_box_height (struct window *w)
1064 {
1065 struct frame *f = XFRAME (w->frame);
1066 int height = WINDOW_PIXEL_HEIGHT (w);
1067
1068 eassert (height >= 0);
1069
1070 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1071
1072 /* Note: the code below that determines the mode-line/header-line
1073 height is essentially the same as that contained in the macro
1074 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1075 the appropriate glyph row has its `mode_line_p' flag set,
1076 and if it doesn't, uses estimate_mode_line_height instead. */
1077
1078 if (WINDOW_WANTS_MODELINE_P (w))
1079 {
1080 struct glyph_row *ml_row
1081 = (w->current_matrix && w->current_matrix->rows
1082 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1083 : 0);
1084 if (ml_row && ml_row->mode_line_p)
1085 height -= ml_row->height;
1086 else
1087 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1088 }
1089
1090 if (WINDOW_WANTS_HEADER_LINE_P (w))
1091 {
1092 struct glyph_row *hl_row
1093 = (w->current_matrix && w->current_matrix->rows
1094 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1095 : 0);
1096 if (hl_row && hl_row->mode_line_p)
1097 height -= hl_row->height;
1098 else
1099 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1100 }
1101
1102 /* With a very small font and a mode-line that's taller than
1103 default, we might end up with a negative height. */
1104 return max (0, height);
1105 }
1106
1107 /* Return the window-relative coordinate of the left edge of display
1108 area AREA of window W. ANY_AREA means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1110
1111 int
1112 window_box_left_offset (struct window *w, enum glyph_row_area area)
1113 {
1114 int x;
1115
1116 if (w->pseudo_window_p)
1117 return 0;
1118
1119 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1120
1121 if (area == TEXT_AREA)
1122 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1123 + window_box_width (w, LEFT_MARGIN_AREA));
1124 else if (area == RIGHT_MARGIN_AREA)
1125 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1126 + window_box_width (w, LEFT_MARGIN_AREA)
1127 + window_box_width (w, TEXT_AREA)
1128 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1129 ? 0
1130 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1131 else if (area == LEFT_MARGIN_AREA
1132 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1133 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1134
1135 /* Don't return more than the window's pixel width. */
1136 return min (x, w->pixel_width);
1137 }
1138
1139
1140 /* Return the window-relative coordinate of the right edge of display
1141 area AREA of window W. ANY_AREA means return the right edge of the
1142 whole window, to the left of the right fringe of W. */
1143
1144 int
1145 window_box_right_offset (struct window *w, enum glyph_row_area area)
1146 {
1147 /* Don't return more than the window's pixel width. */
1148 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1149 w->pixel_width);
1150 }
1151
1152 /* Return the frame-relative coordinate of the left edge of display
1153 area AREA of window W. ANY_AREA means return the left edge of the
1154 whole window, to the right of the left fringe of W. */
1155
1156 int
1157 window_box_left (struct window *w, enum glyph_row_area area)
1158 {
1159 struct frame *f = XFRAME (w->frame);
1160 int x;
1161
1162 if (w->pseudo_window_p)
1163 return FRAME_INTERNAL_BORDER_WIDTH (f);
1164
1165 x = (WINDOW_LEFT_EDGE_X (w)
1166 + window_box_left_offset (w, area));
1167
1168 return x;
1169 }
1170
1171
1172 /* Return the frame-relative coordinate of the right edge of display
1173 area AREA of window W. ANY_AREA means return the right edge of the
1174 whole window, to the left of the right fringe of W. */
1175
1176 int
1177 window_box_right (struct window *w, enum glyph_row_area area)
1178 {
1179 return window_box_left (w, area) + window_box_width (w, area);
1180 }
1181
1182 /* Get the bounding box of the display area AREA of window W, without
1183 mode lines, in frame-relative coordinates. ANY_AREA means the
1184 whole window, not including the left and right fringes of
1185 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1186 coordinates of the upper-left corner of the box. Return in
1187 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1188
1189 void
1190 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1191 int *box_y, int *box_width, int *box_height)
1192 {
1193 if (box_width)
1194 *box_width = window_box_width (w, area);
1195 if (box_height)
1196 *box_height = window_box_height (w);
1197 if (box_x)
1198 *box_x = window_box_left (w, area);
1199 if (box_y)
1200 {
1201 *box_y = WINDOW_TOP_EDGE_Y (w);
1202 if (WINDOW_WANTS_HEADER_LINE_P (w))
1203 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1204 }
1205 }
1206
1207 #ifdef HAVE_WINDOW_SYSTEM
1208
1209 /* Get the bounding box of the display area AREA of window W, without
1210 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1211 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1212 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1213 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1214 box. */
1215
1216 static void
1217 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1218 int *bottom_right_x, int *bottom_right_y)
1219 {
1220 window_box (w, ANY_AREA, top_left_x, top_left_y,
1221 bottom_right_x, bottom_right_y);
1222 *bottom_right_x += *top_left_x;
1223 *bottom_right_y += *top_left_y;
1224 }
1225
1226 #endif /* HAVE_WINDOW_SYSTEM */
1227
1228 /***********************************************************************
1229 Utilities
1230 ***********************************************************************/
1231
1232 /* Return the bottom y-position of the line the iterator IT is in.
1233 This can modify IT's settings. */
1234
1235 int
1236 line_bottom_y (struct it *it)
1237 {
1238 int line_height = it->max_ascent + it->max_descent;
1239 int line_top_y = it->current_y;
1240
1241 if (line_height == 0)
1242 {
1243 if (last_height)
1244 line_height = last_height;
1245 else if (IT_CHARPOS (*it) < ZV)
1246 {
1247 move_it_by_lines (it, 1);
1248 line_height = (it->max_ascent || it->max_descent
1249 ? it->max_ascent + it->max_descent
1250 : last_height);
1251 }
1252 else
1253 {
1254 struct glyph_row *row = it->glyph_row;
1255
1256 /* Use the default character height. */
1257 it->glyph_row = NULL;
1258 it->what = IT_CHARACTER;
1259 it->c = ' ';
1260 it->len = 1;
1261 PRODUCE_GLYPHS (it);
1262 line_height = it->ascent + it->descent;
1263 it->glyph_row = row;
1264 }
1265 }
1266
1267 return line_top_y + line_height;
1268 }
1269
1270 DEFUN ("line-pixel-height", Fline_pixel_height,
1271 Sline_pixel_height, 0, 0, 0,
1272 doc: /* Return height in pixels of text line in the selected window.
1273
1274 Value is the height in pixels of the line at point. */)
1275 (void)
1276 {
1277 struct it it;
1278 struct text_pos pt;
1279 struct window *w = XWINDOW (selected_window);
1280 struct buffer *old_buffer = NULL;
1281 Lisp_Object result;
1282
1283 if (XBUFFER (w->contents) != current_buffer)
1284 {
1285 old_buffer = current_buffer;
1286 set_buffer_internal_1 (XBUFFER (w->contents));
1287 }
1288 SET_TEXT_POS (pt, PT, PT_BYTE);
1289 start_display (&it, w, pt);
1290 it.vpos = it.current_y = 0;
1291 last_height = 0;
1292 result = make_number (line_bottom_y (&it));
1293 if (old_buffer)
1294 set_buffer_internal_1 (old_buffer);
1295
1296 return result;
1297 }
1298
1299 /* Return the default pixel height of text lines in window W. The
1300 value is the canonical height of the W frame's default font, plus
1301 any extra space required by the line-spacing variable or frame
1302 parameter.
1303
1304 Implementation note: this ignores any line-spacing text properties
1305 put on the newline characters. This is because those properties
1306 only affect the _screen_ line ending in the newline (i.e., in a
1307 continued line, only the last screen line will be affected), which
1308 means only a small number of lines in a buffer can ever use this
1309 feature. Since this function is used to compute the default pixel
1310 equivalent of text lines in a window, we can safely ignore those
1311 few lines. For the same reasons, we ignore the line-height
1312 properties. */
1313 int
1314 default_line_pixel_height (struct window *w)
1315 {
1316 struct frame *f = WINDOW_XFRAME (w);
1317 int height = FRAME_LINE_HEIGHT (f);
1318
1319 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1320 {
1321 struct buffer *b = XBUFFER (w->contents);
1322 Lisp_Object val = BVAR (b, extra_line_spacing);
1323
1324 if (NILP (val))
1325 val = BVAR (&buffer_defaults, extra_line_spacing);
1326 if (!NILP (val))
1327 {
1328 if (RANGED_INTEGERP (0, val, INT_MAX))
1329 height += XFASTINT (val);
1330 else if (FLOATP (val))
1331 {
1332 int addon = XFLOAT_DATA (val) * height + 0.5;
1333
1334 if (addon >= 0)
1335 height += addon;
1336 }
1337 }
1338 else
1339 height += f->extra_line_spacing;
1340 }
1341
1342 return height;
1343 }
1344
1345 /* Subroutine of pos_visible_p below. Extracts a display string, if
1346 any, from the display spec given as its argument. */
1347 static Lisp_Object
1348 string_from_display_spec (Lisp_Object spec)
1349 {
1350 if (CONSP (spec))
1351 {
1352 while (CONSP (spec))
1353 {
1354 if (STRINGP (XCAR (spec)))
1355 return XCAR (spec);
1356 spec = XCDR (spec);
1357 }
1358 }
1359 else if (VECTORP (spec))
1360 {
1361 ptrdiff_t i;
1362
1363 for (i = 0; i < ASIZE (spec); i++)
1364 {
1365 if (STRINGP (AREF (spec, i)))
1366 return AREF (spec, i);
1367 }
1368 return Qnil;
1369 }
1370
1371 return spec;
1372 }
1373
1374
1375 /* Limit insanely large values of W->hscroll on frame F to the largest
1376 value that will still prevent first_visible_x and last_visible_x of
1377 'struct it' from overflowing an int. */
1378 static int
1379 window_hscroll_limited (struct window *w, struct frame *f)
1380 {
1381 ptrdiff_t window_hscroll = w->hscroll;
1382 int window_text_width = window_box_width (w, TEXT_AREA);
1383 int colwidth = FRAME_COLUMN_WIDTH (f);
1384
1385 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1386 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1387
1388 return window_hscroll;
1389 }
1390
1391 /* Return 1 if position CHARPOS is visible in window W.
1392 CHARPOS < 0 means return info about WINDOW_END position.
1393 If visible, set *X and *Y to pixel coordinates of top left corner.
1394 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1395 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1396
1397 int
1398 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1399 int *rtop, int *rbot, int *rowh, int *vpos)
1400 {
1401 struct it it;
1402 void *itdata = bidi_shelve_cache ();
1403 struct text_pos top;
1404 int visible_p = 0;
1405 struct buffer *old_buffer = NULL;
1406
1407 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1408 return visible_p;
1409
1410 if (XBUFFER (w->contents) != current_buffer)
1411 {
1412 old_buffer = current_buffer;
1413 set_buffer_internal_1 (XBUFFER (w->contents));
1414 }
1415
1416 SET_TEXT_POS_FROM_MARKER (top, w->start);
1417 /* Scrolling a minibuffer window via scroll bar when the echo area
1418 shows long text sometimes resets the minibuffer contents behind
1419 our backs. */
1420 if (CHARPOS (top) > ZV)
1421 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1422
1423 /* Compute exact mode line heights. */
1424 if (WINDOW_WANTS_MODELINE_P (w))
1425 w->mode_line_height
1426 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1427 BVAR (current_buffer, mode_line_format));
1428
1429 if (WINDOW_WANTS_HEADER_LINE_P (w))
1430 w->header_line_height
1431 = display_mode_line (w, HEADER_LINE_FACE_ID,
1432 BVAR (current_buffer, header_line_format));
1433
1434 start_display (&it, w, top);
1435 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1436 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1437
1438 if (charpos >= 0
1439 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1440 && IT_CHARPOS (it) >= charpos)
1441 /* When scanning backwards under bidi iteration, move_it_to
1442 stops at or _before_ CHARPOS, because it stops at or to
1443 the _right_ of the character at CHARPOS. */
1444 || (it.bidi_p && it.bidi_it.scan_dir == -1
1445 && IT_CHARPOS (it) <= charpos)))
1446 {
1447 /* We have reached CHARPOS, or passed it. How the call to
1448 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1449 or covered by a display property, move_it_to stops at the end
1450 of the invisible text, to the right of CHARPOS. (ii) If
1451 CHARPOS is in a display vector, move_it_to stops on its last
1452 glyph. */
1453 int top_x = it.current_x;
1454 int top_y = it.current_y;
1455 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1456 int bottom_y;
1457 struct it save_it;
1458 void *save_it_data = NULL;
1459
1460 /* Calling line_bottom_y may change it.method, it.position, etc. */
1461 SAVE_IT (save_it, it, save_it_data);
1462 last_height = 0;
1463 bottom_y = line_bottom_y (&it);
1464 if (top_y < window_top_y)
1465 visible_p = bottom_y > window_top_y;
1466 else if (top_y < it.last_visible_y)
1467 visible_p = 1;
1468 if (bottom_y >= it.last_visible_y
1469 && it.bidi_p && it.bidi_it.scan_dir == -1
1470 && IT_CHARPOS (it) < charpos)
1471 {
1472 /* When the last line of the window is scanned backwards
1473 under bidi iteration, we could be duped into thinking
1474 that we have passed CHARPOS, when in fact move_it_to
1475 simply stopped short of CHARPOS because it reached
1476 last_visible_y. To see if that's what happened, we call
1477 move_it_to again with a slightly larger vertical limit,
1478 and see if it actually moved vertically; if it did, we
1479 didn't really reach CHARPOS, which is beyond window end. */
1480 /* Why 10? because we don't know how many canonical lines
1481 will the height of the next line(s) be. So we guess. */
1482 int ten_more_lines = 10 * default_line_pixel_height (w);
1483
1484 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1485 MOVE_TO_POS | MOVE_TO_Y);
1486 if (it.current_y > top_y)
1487 visible_p = 0;
1488
1489 }
1490 RESTORE_IT (&it, &save_it, save_it_data);
1491 if (visible_p)
1492 {
1493 if (it.method == GET_FROM_DISPLAY_VECTOR)
1494 {
1495 /* We stopped on the last glyph of a display vector.
1496 Try and recompute. Hack alert! */
1497 if (charpos < 2 || top.charpos >= charpos)
1498 top_x = it.glyph_row->x;
1499 else
1500 {
1501 struct it it2, it2_prev;
1502 /* The idea is to get to the previous buffer
1503 position, consume the character there, and use
1504 the pixel coordinates we get after that. But if
1505 the previous buffer position is also displayed
1506 from a display vector, we need to consume all of
1507 the glyphs from that display vector. */
1508 start_display (&it2, w, top);
1509 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1510 /* If we didn't get to CHARPOS - 1, there's some
1511 replacing display property at that position, and
1512 we stopped after it. That is exactly the place
1513 whose coordinates we want. */
1514 if (IT_CHARPOS (it2) != charpos - 1)
1515 it2_prev = it2;
1516 else
1517 {
1518 /* Iterate until we get out of the display
1519 vector that displays the character at
1520 CHARPOS - 1. */
1521 do {
1522 get_next_display_element (&it2);
1523 PRODUCE_GLYPHS (&it2);
1524 it2_prev = it2;
1525 set_iterator_to_next (&it2, 1);
1526 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1527 && IT_CHARPOS (it2) < charpos);
1528 }
1529 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1530 || it2_prev.current_x > it2_prev.last_visible_x)
1531 top_x = it.glyph_row->x;
1532 else
1533 {
1534 top_x = it2_prev.current_x;
1535 top_y = it2_prev.current_y;
1536 }
1537 }
1538 }
1539 else if (IT_CHARPOS (it) != charpos)
1540 {
1541 Lisp_Object cpos = make_number (charpos);
1542 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1543 Lisp_Object string = string_from_display_spec (spec);
1544 struct text_pos tpos;
1545 int replacing_spec_p;
1546 bool newline_in_string
1547 = (STRINGP (string)
1548 && memchr (SDATA (string), '\n', SBYTES (string)));
1549
1550 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1551 replacing_spec_p
1552 = (!NILP (spec)
1553 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1554 charpos, FRAME_WINDOW_P (it.f)));
1555 /* The tricky code below is needed because there's a
1556 discrepancy between move_it_to and how we set cursor
1557 when PT is at the beginning of a portion of text
1558 covered by a display property or an overlay with a
1559 display property, or the display line ends in a
1560 newline from a display string. move_it_to will stop
1561 _after_ such display strings, whereas
1562 set_cursor_from_row conspires with cursor_row_p to
1563 place the cursor on the first glyph produced from the
1564 display string. */
1565
1566 /* We have overshoot PT because it is covered by a
1567 display property that replaces the text it covers.
1568 If the string includes embedded newlines, we are also
1569 in the wrong display line. Backtrack to the correct
1570 line, where the display property begins. */
1571 if (replacing_spec_p)
1572 {
1573 Lisp_Object startpos, endpos;
1574 EMACS_INT start, end;
1575 struct it it3;
1576 int it3_moved;
1577
1578 /* Find the first and the last buffer positions
1579 covered by the display string. */
1580 endpos =
1581 Fnext_single_char_property_change (cpos, Qdisplay,
1582 Qnil, Qnil);
1583 startpos =
1584 Fprevious_single_char_property_change (endpos, Qdisplay,
1585 Qnil, Qnil);
1586 start = XFASTINT (startpos);
1587 end = XFASTINT (endpos);
1588 /* Move to the last buffer position before the
1589 display property. */
1590 start_display (&it3, w, top);
1591 if (start > CHARPOS (top))
1592 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1593 /* Move forward one more line if the position before
1594 the display string is a newline or if it is the
1595 rightmost character on a line that is
1596 continued or word-wrapped. */
1597 if (it3.method == GET_FROM_BUFFER
1598 && (it3.c == '\n'
1599 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1600 move_it_by_lines (&it3, 1);
1601 else if (move_it_in_display_line_to (&it3, -1,
1602 it3.current_x
1603 + it3.pixel_width,
1604 MOVE_TO_X)
1605 == MOVE_LINE_CONTINUED)
1606 {
1607 move_it_by_lines (&it3, 1);
1608 /* When we are under word-wrap, the #$@%!
1609 move_it_by_lines moves 2 lines, so we need to
1610 fix that up. */
1611 if (it3.line_wrap == WORD_WRAP)
1612 move_it_by_lines (&it3, -1);
1613 }
1614
1615 /* Record the vertical coordinate of the display
1616 line where we wound up. */
1617 top_y = it3.current_y;
1618 if (it3.bidi_p)
1619 {
1620 /* When characters are reordered for display,
1621 the character displayed to the left of the
1622 display string could be _after_ the display
1623 property in the logical order. Use the
1624 smallest vertical position of these two. */
1625 start_display (&it3, w, top);
1626 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1627 if (it3.current_y < top_y)
1628 top_y = it3.current_y;
1629 }
1630 /* Move from the top of the window to the beginning
1631 of the display line where the display string
1632 begins. */
1633 start_display (&it3, w, top);
1634 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1635 /* If it3_moved stays zero after the 'while' loop
1636 below, that means we already were at a newline
1637 before the loop (e.g., the display string begins
1638 with a newline), so we don't need to (and cannot)
1639 inspect the glyphs of it3.glyph_row, because
1640 PRODUCE_GLYPHS will not produce anything for a
1641 newline, and thus it3.glyph_row stays at its
1642 stale content it got at top of the window. */
1643 it3_moved = 0;
1644 /* Finally, advance the iterator until we hit the
1645 first display element whose character position is
1646 CHARPOS, or until the first newline from the
1647 display string, which signals the end of the
1648 display line. */
1649 while (get_next_display_element (&it3))
1650 {
1651 PRODUCE_GLYPHS (&it3);
1652 if (IT_CHARPOS (it3) == charpos
1653 || ITERATOR_AT_END_OF_LINE_P (&it3))
1654 break;
1655 it3_moved = 1;
1656 set_iterator_to_next (&it3, 0);
1657 }
1658 top_x = it3.current_x - it3.pixel_width;
1659 /* Normally, we would exit the above loop because we
1660 found the display element whose character
1661 position is CHARPOS. For the contingency that we
1662 didn't, and stopped at the first newline from the
1663 display string, move back over the glyphs
1664 produced from the string, until we find the
1665 rightmost glyph not from the string. */
1666 if (it3_moved
1667 && newline_in_string
1668 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1669 {
1670 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1671 + it3.glyph_row->used[TEXT_AREA];
1672
1673 while (EQ ((g - 1)->object, string))
1674 {
1675 --g;
1676 top_x -= g->pixel_width;
1677 }
1678 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1679 + it3.glyph_row->used[TEXT_AREA]);
1680 }
1681 }
1682 }
1683
1684 *x = top_x;
1685 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1686 *rtop = max (0, window_top_y - top_y);
1687 *rbot = max (0, bottom_y - it.last_visible_y);
1688 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1689 - max (top_y, window_top_y)));
1690 *vpos = it.vpos;
1691 }
1692 }
1693 else
1694 {
1695 /* Either we were asked to provide info about WINDOW_END, or
1696 CHARPOS is in the partially visible glyph row at end of
1697 window. */
1698 struct it it2;
1699 void *it2data = NULL;
1700
1701 SAVE_IT (it2, it, it2data);
1702 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1703 move_it_by_lines (&it, 1);
1704 if (charpos < IT_CHARPOS (it)
1705 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1706 {
1707 visible_p = true;
1708 RESTORE_IT (&it2, &it2, it2data);
1709 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1710 *x = it2.current_x;
1711 *y = it2.current_y + it2.max_ascent - it2.ascent;
1712 *rtop = max (0, -it2.current_y);
1713 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1714 - it.last_visible_y));
1715 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1716 it.last_visible_y)
1717 - max (it2.current_y,
1718 WINDOW_HEADER_LINE_HEIGHT (w))));
1719 *vpos = it2.vpos;
1720 }
1721 else
1722 bidi_unshelve_cache (it2data, 1);
1723 }
1724 bidi_unshelve_cache (itdata, 0);
1725
1726 if (old_buffer)
1727 set_buffer_internal_1 (old_buffer);
1728
1729 if (visible_p && w->hscroll > 0)
1730 *x -=
1731 window_hscroll_limited (w, WINDOW_XFRAME (w))
1732 * WINDOW_FRAME_COLUMN_WIDTH (w);
1733
1734 #if 0
1735 /* Debugging code. */
1736 if (visible_p)
1737 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1738 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1739 else
1740 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1741 #endif
1742
1743 return visible_p;
1744 }
1745
1746
1747 /* Return the next character from STR. Return in *LEN the length of
1748 the character. This is like STRING_CHAR_AND_LENGTH but never
1749 returns an invalid character. If we find one, we return a `?', but
1750 with the length of the invalid character. */
1751
1752 static int
1753 string_char_and_length (const unsigned char *str, int *len)
1754 {
1755 int c;
1756
1757 c = STRING_CHAR_AND_LENGTH (str, *len);
1758 if (!CHAR_VALID_P (c))
1759 /* We may not change the length here because other places in Emacs
1760 don't use this function, i.e. they silently accept invalid
1761 characters. */
1762 c = '?';
1763
1764 return c;
1765 }
1766
1767
1768
1769 /* Given a position POS containing a valid character and byte position
1770 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1771
1772 static struct text_pos
1773 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1774 {
1775 eassert (STRINGP (string) && nchars >= 0);
1776
1777 if (STRING_MULTIBYTE (string))
1778 {
1779 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1780 int len;
1781
1782 while (nchars--)
1783 {
1784 string_char_and_length (p, &len);
1785 p += len;
1786 CHARPOS (pos) += 1;
1787 BYTEPOS (pos) += len;
1788 }
1789 }
1790 else
1791 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1792
1793 return pos;
1794 }
1795
1796
1797 /* Value is the text position, i.e. character and byte position,
1798 for character position CHARPOS in STRING. */
1799
1800 static struct text_pos
1801 string_pos (ptrdiff_t charpos, Lisp_Object string)
1802 {
1803 struct text_pos pos;
1804 eassert (STRINGP (string));
1805 eassert (charpos >= 0);
1806 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1807 return pos;
1808 }
1809
1810
1811 /* Value is a text position, i.e. character and byte position, for
1812 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1813 means recognize multibyte characters. */
1814
1815 static struct text_pos
1816 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1817 {
1818 struct text_pos pos;
1819
1820 eassert (s != NULL);
1821 eassert (charpos >= 0);
1822
1823 if (multibyte_p)
1824 {
1825 int len;
1826
1827 SET_TEXT_POS (pos, 0, 0);
1828 while (charpos--)
1829 {
1830 string_char_and_length ((const unsigned char *) s, &len);
1831 s += len;
1832 CHARPOS (pos) += 1;
1833 BYTEPOS (pos) += len;
1834 }
1835 }
1836 else
1837 SET_TEXT_POS (pos, charpos, charpos);
1838
1839 return pos;
1840 }
1841
1842
1843 /* Value is the number of characters in C string S. MULTIBYTE_P
1844 non-zero means recognize multibyte characters. */
1845
1846 static ptrdiff_t
1847 number_of_chars (const char *s, bool multibyte_p)
1848 {
1849 ptrdiff_t nchars;
1850
1851 if (multibyte_p)
1852 {
1853 ptrdiff_t rest = strlen (s);
1854 int len;
1855 const unsigned char *p = (const unsigned char *) s;
1856
1857 for (nchars = 0; rest > 0; ++nchars)
1858 {
1859 string_char_and_length (p, &len);
1860 rest -= len, p += len;
1861 }
1862 }
1863 else
1864 nchars = strlen (s);
1865
1866 return nchars;
1867 }
1868
1869
1870 /* Compute byte position NEWPOS->bytepos corresponding to
1871 NEWPOS->charpos. POS is a known position in string STRING.
1872 NEWPOS->charpos must be >= POS.charpos. */
1873
1874 static void
1875 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1876 {
1877 eassert (STRINGP (string));
1878 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1879
1880 if (STRING_MULTIBYTE (string))
1881 *newpos = string_pos_nchars_ahead (pos, string,
1882 CHARPOS (*newpos) - CHARPOS (pos));
1883 else
1884 BYTEPOS (*newpos) = CHARPOS (*newpos);
1885 }
1886
1887 /* EXPORT:
1888 Return an estimation of the pixel height of mode or header lines on
1889 frame F. FACE_ID specifies what line's height to estimate. */
1890
1891 int
1892 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1893 {
1894 #ifdef HAVE_WINDOW_SYSTEM
1895 if (FRAME_WINDOW_P (f))
1896 {
1897 int height = FONT_HEIGHT (FRAME_FONT (f));
1898
1899 /* This function is called so early when Emacs starts that the face
1900 cache and mode line face are not yet initialized. */
1901 if (FRAME_FACE_CACHE (f))
1902 {
1903 struct face *face = FACE_FROM_ID (f, face_id);
1904 if (face)
1905 {
1906 if (face->font)
1907 height = FONT_HEIGHT (face->font);
1908 if (face->box_line_width > 0)
1909 height += 2 * face->box_line_width;
1910 }
1911 }
1912
1913 return height;
1914 }
1915 #endif
1916
1917 return 1;
1918 }
1919
1920 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1921 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1922 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1923 not force the value into range. */
1924
1925 void
1926 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1927 int *x, int *y, NativeRectangle *bounds, int noclip)
1928 {
1929
1930 #ifdef HAVE_WINDOW_SYSTEM
1931 if (FRAME_WINDOW_P (f))
1932 {
1933 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1934 even for negative values. */
1935 if (pix_x < 0)
1936 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1937 if (pix_y < 0)
1938 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1939
1940 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1941 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1942
1943 if (bounds)
1944 STORE_NATIVE_RECT (*bounds,
1945 FRAME_COL_TO_PIXEL_X (f, pix_x),
1946 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1947 FRAME_COLUMN_WIDTH (f) - 1,
1948 FRAME_LINE_HEIGHT (f) - 1);
1949
1950 /* PXW: Should we clip pixels before converting to columns/lines? */
1951 if (!noclip)
1952 {
1953 if (pix_x < 0)
1954 pix_x = 0;
1955 else if (pix_x > FRAME_TOTAL_COLS (f))
1956 pix_x = FRAME_TOTAL_COLS (f);
1957
1958 if (pix_y < 0)
1959 pix_y = 0;
1960 else if (pix_y > FRAME_LINES (f))
1961 pix_y = FRAME_LINES (f);
1962 }
1963 }
1964 #endif
1965
1966 *x = pix_x;
1967 *y = pix_y;
1968 }
1969
1970
1971 /* Find the glyph under window-relative coordinates X/Y in window W.
1972 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1973 strings. Return in *HPOS and *VPOS the row and column number of
1974 the glyph found. Return in *AREA the glyph area containing X.
1975 Value is a pointer to the glyph found or null if X/Y is not on
1976 text, or we can't tell because W's current matrix is not up to
1977 date. */
1978
1979 static struct glyph *
1980 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1981 int *dx, int *dy, int *area)
1982 {
1983 struct glyph *glyph, *end;
1984 struct glyph_row *row = NULL;
1985 int x0, i;
1986
1987 /* Find row containing Y. Give up if some row is not enabled. */
1988 for (i = 0; i < w->current_matrix->nrows; ++i)
1989 {
1990 row = MATRIX_ROW (w->current_matrix, i);
1991 if (!row->enabled_p)
1992 return NULL;
1993 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1994 break;
1995 }
1996
1997 *vpos = i;
1998 *hpos = 0;
1999
2000 /* Give up if Y is not in the window. */
2001 if (i == w->current_matrix->nrows)
2002 return NULL;
2003
2004 /* Get the glyph area containing X. */
2005 if (w->pseudo_window_p)
2006 {
2007 *area = TEXT_AREA;
2008 x0 = 0;
2009 }
2010 else
2011 {
2012 if (x < window_box_left_offset (w, TEXT_AREA))
2013 {
2014 *area = LEFT_MARGIN_AREA;
2015 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
2016 }
2017 else if (x < window_box_right_offset (w, TEXT_AREA))
2018 {
2019 *area = TEXT_AREA;
2020 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
2021 }
2022 else
2023 {
2024 *area = RIGHT_MARGIN_AREA;
2025 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
2026 }
2027 }
2028
2029 /* Find glyph containing X. */
2030 glyph = row->glyphs[*area];
2031 end = glyph + row->used[*area];
2032 x -= x0;
2033 while (glyph < end && x >= glyph->pixel_width)
2034 {
2035 x -= glyph->pixel_width;
2036 ++glyph;
2037 }
2038
2039 if (glyph == end)
2040 return NULL;
2041
2042 if (dx)
2043 {
2044 *dx = x;
2045 *dy = y - (row->y + row->ascent - glyph->ascent);
2046 }
2047
2048 *hpos = glyph - row->glyphs[*area];
2049 return glyph;
2050 }
2051
2052 /* Convert frame-relative x/y to coordinates relative to window W.
2053 Takes pseudo-windows into account. */
2054
2055 static void
2056 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2057 {
2058 if (w->pseudo_window_p)
2059 {
2060 /* A pseudo-window is always full-width, and starts at the
2061 left edge of the frame, plus a frame border. */
2062 struct frame *f = XFRAME (w->frame);
2063 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2064 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2065 }
2066 else
2067 {
2068 *x -= WINDOW_LEFT_EDGE_X (w);
2069 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2070 }
2071 }
2072
2073 #ifdef HAVE_WINDOW_SYSTEM
2074
2075 /* EXPORT:
2076 Return in RECTS[] at most N clipping rectangles for glyph string S.
2077 Return the number of stored rectangles. */
2078
2079 int
2080 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2081 {
2082 XRectangle r;
2083
2084 if (n <= 0)
2085 return 0;
2086
2087 if (s->row->full_width_p)
2088 {
2089 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2090 r.x = WINDOW_LEFT_EDGE_X (s->w);
2091 if (s->row->mode_line_p)
2092 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2093 else
2094 r.width = WINDOW_PIXEL_WIDTH (s->w);
2095
2096 /* Unless displaying a mode or menu bar line, which are always
2097 fully visible, clip to the visible part of the row. */
2098 if (s->w->pseudo_window_p)
2099 r.height = s->row->visible_height;
2100 else
2101 r.height = s->height;
2102 }
2103 else
2104 {
2105 /* This is a text line that may be partially visible. */
2106 r.x = window_box_left (s->w, s->area);
2107 r.width = window_box_width (s->w, s->area);
2108 r.height = s->row->visible_height;
2109 }
2110
2111 if (s->clip_head)
2112 if (r.x < s->clip_head->x)
2113 {
2114 if (r.width >= s->clip_head->x - r.x)
2115 r.width -= s->clip_head->x - r.x;
2116 else
2117 r.width = 0;
2118 r.x = s->clip_head->x;
2119 }
2120 if (s->clip_tail)
2121 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2122 {
2123 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2124 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2125 else
2126 r.width = 0;
2127 }
2128
2129 /* If S draws overlapping rows, it's sufficient to use the top and
2130 bottom of the window for clipping because this glyph string
2131 intentionally draws over other lines. */
2132 if (s->for_overlaps)
2133 {
2134 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2135 r.height = window_text_bottom_y (s->w) - r.y;
2136
2137 /* Alas, the above simple strategy does not work for the
2138 environments with anti-aliased text: if the same text is
2139 drawn onto the same place multiple times, it gets thicker.
2140 If the overlap we are processing is for the erased cursor, we
2141 take the intersection with the rectangle of the cursor. */
2142 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2143 {
2144 XRectangle rc, r_save = r;
2145
2146 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2147 rc.y = s->w->phys_cursor.y;
2148 rc.width = s->w->phys_cursor_width;
2149 rc.height = s->w->phys_cursor_height;
2150
2151 x_intersect_rectangles (&r_save, &rc, &r);
2152 }
2153 }
2154 else
2155 {
2156 /* Don't use S->y for clipping because it doesn't take partially
2157 visible lines into account. For example, it can be negative for
2158 partially visible lines at the top of a window. */
2159 if (!s->row->full_width_p
2160 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2161 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2162 else
2163 r.y = max (0, s->row->y);
2164 }
2165
2166 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2167
2168 /* If drawing the cursor, don't let glyph draw outside its
2169 advertised boundaries. Cleartype does this under some circumstances. */
2170 if (s->hl == DRAW_CURSOR)
2171 {
2172 struct glyph *glyph = s->first_glyph;
2173 int height, max_y;
2174
2175 if (s->x > r.x)
2176 {
2177 r.width -= s->x - r.x;
2178 r.x = s->x;
2179 }
2180 r.width = min (r.width, glyph->pixel_width);
2181
2182 /* If r.y is below window bottom, ensure that we still see a cursor. */
2183 height = min (glyph->ascent + glyph->descent,
2184 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2185 max_y = window_text_bottom_y (s->w) - height;
2186 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2187 if (s->ybase - glyph->ascent > max_y)
2188 {
2189 r.y = max_y;
2190 r.height = height;
2191 }
2192 else
2193 {
2194 /* Don't draw cursor glyph taller than our actual glyph. */
2195 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2196 if (height < r.height)
2197 {
2198 max_y = r.y + r.height;
2199 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2200 r.height = min (max_y - r.y, height);
2201 }
2202 }
2203 }
2204
2205 if (s->row->clip)
2206 {
2207 XRectangle r_save = r;
2208
2209 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2210 r.width = 0;
2211 }
2212
2213 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2214 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2215 {
2216 #ifdef CONVERT_FROM_XRECT
2217 CONVERT_FROM_XRECT (r, *rects);
2218 #else
2219 *rects = r;
2220 #endif
2221 return 1;
2222 }
2223 else
2224 {
2225 /* If we are processing overlapping and allowed to return
2226 multiple clipping rectangles, we exclude the row of the glyph
2227 string from the clipping rectangle. This is to avoid drawing
2228 the same text on the environment with anti-aliasing. */
2229 #ifdef CONVERT_FROM_XRECT
2230 XRectangle rs[2];
2231 #else
2232 XRectangle *rs = rects;
2233 #endif
2234 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2235
2236 if (s->for_overlaps & OVERLAPS_PRED)
2237 {
2238 rs[i] = r;
2239 if (r.y + r.height > row_y)
2240 {
2241 if (r.y < row_y)
2242 rs[i].height = row_y - r.y;
2243 else
2244 rs[i].height = 0;
2245 }
2246 i++;
2247 }
2248 if (s->for_overlaps & OVERLAPS_SUCC)
2249 {
2250 rs[i] = r;
2251 if (r.y < row_y + s->row->visible_height)
2252 {
2253 if (r.y + r.height > row_y + s->row->visible_height)
2254 {
2255 rs[i].y = row_y + s->row->visible_height;
2256 rs[i].height = r.y + r.height - rs[i].y;
2257 }
2258 else
2259 rs[i].height = 0;
2260 }
2261 i++;
2262 }
2263
2264 n = i;
2265 #ifdef CONVERT_FROM_XRECT
2266 for (i = 0; i < n; i++)
2267 CONVERT_FROM_XRECT (rs[i], rects[i]);
2268 #endif
2269 return n;
2270 }
2271 }
2272
2273 /* EXPORT:
2274 Return in *NR the clipping rectangle for glyph string S. */
2275
2276 void
2277 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2278 {
2279 get_glyph_string_clip_rects (s, nr, 1);
2280 }
2281
2282
2283 /* EXPORT:
2284 Return the position and height of the phys cursor in window W.
2285 Set w->phys_cursor_width to width of phys cursor.
2286 */
2287
2288 void
2289 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2290 struct glyph *glyph, int *xp, int *yp, int *heightp)
2291 {
2292 struct frame *f = XFRAME (WINDOW_FRAME (w));
2293 int x, y, wd, h, h0, y0;
2294
2295 /* Compute the width of the rectangle to draw. If on a stretch
2296 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2297 rectangle as wide as the glyph, but use a canonical character
2298 width instead. */
2299 wd = glyph->pixel_width - 1;
2300 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2301 wd++; /* Why? */
2302 #endif
2303
2304 x = w->phys_cursor.x;
2305 if (x < 0)
2306 {
2307 wd += x;
2308 x = 0;
2309 }
2310
2311 if (glyph->type == STRETCH_GLYPH
2312 && !x_stretch_cursor_p)
2313 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2314 w->phys_cursor_width = wd;
2315
2316 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2317
2318 /* If y is below window bottom, ensure that we still see a cursor. */
2319 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2320
2321 h = max (h0, glyph->ascent + glyph->descent);
2322 h0 = min (h0, glyph->ascent + glyph->descent);
2323
2324 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2325 if (y < y0)
2326 {
2327 h = max (h - (y0 - y) + 1, h0);
2328 y = y0 - 1;
2329 }
2330 else
2331 {
2332 y0 = window_text_bottom_y (w) - h0;
2333 if (y > y0)
2334 {
2335 h += y - y0;
2336 y = y0;
2337 }
2338 }
2339
2340 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2341 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2342 *heightp = h;
2343 }
2344
2345 /*
2346 * Remember which glyph the mouse is over.
2347 */
2348
2349 void
2350 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2351 {
2352 Lisp_Object window;
2353 struct window *w;
2354 struct glyph_row *r, *gr, *end_row;
2355 enum window_part part;
2356 enum glyph_row_area area;
2357 int x, y, width, height;
2358
2359 /* Try to determine frame pixel position and size of the glyph under
2360 frame pixel coordinates X/Y on frame F. */
2361
2362 if (window_resize_pixelwise)
2363 {
2364 width = height = 1;
2365 goto virtual_glyph;
2366 }
2367 else if (!f->glyphs_initialized_p
2368 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2369 NILP (window)))
2370 {
2371 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2372 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2373 goto virtual_glyph;
2374 }
2375
2376 w = XWINDOW (window);
2377 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2378 height = WINDOW_FRAME_LINE_HEIGHT (w);
2379
2380 x = window_relative_x_coord (w, part, gx);
2381 y = gy - WINDOW_TOP_EDGE_Y (w);
2382
2383 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2384 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2385
2386 if (w->pseudo_window_p)
2387 {
2388 area = TEXT_AREA;
2389 part = ON_MODE_LINE; /* Don't adjust margin. */
2390 goto text_glyph;
2391 }
2392
2393 switch (part)
2394 {
2395 case ON_LEFT_MARGIN:
2396 area = LEFT_MARGIN_AREA;
2397 goto text_glyph;
2398
2399 case ON_RIGHT_MARGIN:
2400 area = RIGHT_MARGIN_AREA;
2401 goto text_glyph;
2402
2403 case ON_HEADER_LINE:
2404 case ON_MODE_LINE:
2405 gr = (part == ON_HEADER_LINE
2406 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2407 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2408 gy = gr->y;
2409 area = TEXT_AREA;
2410 goto text_glyph_row_found;
2411
2412 case ON_TEXT:
2413 area = TEXT_AREA;
2414
2415 text_glyph:
2416 gr = 0; gy = 0;
2417 for (; r <= end_row && r->enabled_p; ++r)
2418 if (r->y + r->height > y)
2419 {
2420 gr = r; gy = r->y;
2421 break;
2422 }
2423
2424 text_glyph_row_found:
2425 if (gr && gy <= y)
2426 {
2427 struct glyph *g = gr->glyphs[area];
2428 struct glyph *end = g + gr->used[area];
2429
2430 height = gr->height;
2431 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2432 if (gx + g->pixel_width > x)
2433 break;
2434
2435 if (g < end)
2436 {
2437 if (g->type == IMAGE_GLYPH)
2438 {
2439 /* Don't remember when mouse is over image, as
2440 image may have hot-spots. */
2441 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2442 return;
2443 }
2444 width = g->pixel_width;
2445 }
2446 else
2447 {
2448 /* Use nominal char spacing at end of line. */
2449 x -= gx;
2450 gx += (x / width) * width;
2451 }
2452
2453 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2454 {
2455 gx += window_box_left_offset (w, area);
2456 /* Don't expand over the modeline to make sure the vertical
2457 drag cursor is shown early enough. */
2458 height = min (height,
2459 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2460 }
2461 }
2462 else
2463 {
2464 /* Use nominal line height at end of window. */
2465 gx = (x / width) * width;
2466 y -= gy;
2467 gy += (y / height) * height;
2468 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2469 /* See comment above. */
2470 height = min (height,
2471 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2472 }
2473 break;
2474
2475 case ON_LEFT_FRINGE:
2476 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2477 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2478 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2479 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2480 goto row_glyph;
2481
2482 case ON_RIGHT_FRINGE:
2483 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2484 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2485 : window_box_right_offset (w, TEXT_AREA));
2486 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2487 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2488 && !WINDOW_RIGHTMOST_P (w))
2489 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2490 /* Make sure the vertical border can get her own glyph to the
2491 right of the one we build here. */
2492 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2493 else
2494 width = WINDOW_PIXEL_WIDTH (w) - gx;
2495 else
2496 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2497
2498 goto row_glyph;
2499
2500 case ON_VERTICAL_BORDER:
2501 gx = WINDOW_PIXEL_WIDTH (w) - width;
2502 goto row_glyph;
2503
2504 case ON_SCROLL_BAR:
2505 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2506 ? 0
2507 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2508 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2509 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2510 : 0)));
2511 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2512
2513 row_glyph:
2514 gr = 0, gy = 0;
2515 for (; r <= end_row && r->enabled_p; ++r)
2516 if (r->y + r->height > y)
2517 {
2518 gr = r; gy = r->y;
2519 break;
2520 }
2521
2522 if (gr && gy <= y)
2523 height = gr->height;
2524 else
2525 {
2526 /* Use nominal line height at end of window. */
2527 y -= gy;
2528 gy += (y / height) * height;
2529 }
2530 break;
2531
2532 case ON_RIGHT_DIVIDER:
2533 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2534 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2535 gy = 0;
2536 /* The bottom divider prevails. */
2537 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2538 goto add_edge;;
2539
2540 case ON_BOTTOM_DIVIDER:
2541 gx = 0;
2542 width = WINDOW_PIXEL_WIDTH (w);
2543 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2544 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2545 goto add_edge;
2546
2547 default:
2548 ;
2549 virtual_glyph:
2550 /* If there is no glyph under the mouse, then we divide the screen
2551 into a grid of the smallest glyph in the frame, and use that
2552 as our "glyph". */
2553
2554 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2555 round down even for negative values. */
2556 if (gx < 0)
2557 gx -= width - 1;
2558 if (gy < 0)
2559 gy -= height - 1;
2560
2561 gx = (gx / width) * width;
2562 gy = (gy / height) * height;
2563
2564 goto store_rect;
2565 }
2566
2567 add_edge:
2568 gx += WINDOW_LEFT_EDGE_X (w);
2569 gy += WINDOW_TOP_EDGE_Y (w);
2570
2571 store_rect:
2572 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2573
2574 /* Visible feedback for debugging. */
2575 #if 0
2576 #if HAVE_X_WINDOWS
2577 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2578 f->output_data.x->normal_gc,
2579 gx, gy, width, height);
2580 #endif
2581 #endif
2582 }
2583
2584
2585 #endif /* HAVE_WINDOW_SYSTEM */
2586
2587 static void
2588 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2589 {
2590 eassert (w);
2591 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2592 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2593 w->window_end_vpos
2594 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2595 }
2596
2597 /***********************************************************************
2598 Lisp form evaluation
2599 ***********************************************************************/
2600
2601 /* Error handler for safe_eval and safe_call. */
2602
2603 static Lisp_Object
2604 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2605 {
2606 add_to_log ("Error during redisplay: %S signaled %S",
2607 Flist (nargs, args), arg);
2608 return Qnil;
2609 }
2610
2611 /* Call function FUNC with the rest of NARGS - 1 arguments
2612 following. Return the result, or nil if something went
2613 wrong. Prevent redisplay during the evaluation. */
2614
2615 static Lisp_Object
2616 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2617 {
2618 Lisp_Object val;
2619
2620 if (inhibit_eval_during_redisplay)
2621 val = Qnil;
2622 else
2623 {
2624 ptrdiff_t i;
2625 ptrdiff_t count = SPECPDL_INDEX ();
2626 struct gcpro gcpro1;
2627 Lisp_Object *args = alloca (nargs * word_size);
2628
2629 args[0] = func;
2630 for (i = 1; i < nargs; i++)
2631 args[i] = va_arg (ap, Lisp_Object);
2632
2633 GCPRO1 (args[0]);
2634 gcpro1.nvars = nargs;
2635 specbind (Qinhibit_redisplay, Qt);
2636 if (inhibit_quit)
2637 specbind (Qinhibit_quit, Qt);
2638 /* Use Qt to ensure debugger does not run,
2639 so there is no possibility of wanting to redisplay. */
2640 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2641 safe_eval_handler);
2642 UNGCPRO;
2643 val = unbind_to (count, val);
2644 }
2645
2646 return val;
2647 }
2648
2649 Lisp_Object
2650 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2651 {
2652 Lisp_Object retval;
2653 va_list ap;
2654
2655 va_start (ap, func);
2656 retval = safe__call (false, nargs, func, ap);
2657 va_end (ap);
2658 return retval;
2659 }
2660
2661 /* Call function FN with one argument ARG.
2662 Return the result, or nil if something went wrong. */
2663
2664 Lisp_Object
2665 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2666 {
2667 return safe_call (2, fn, arg);
2668 }
2669
2670 static Lisp_Object
2671 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2672 {
2673 Lisp_Object retval;
2674 va_list ap;
2675
2676 va_start (ap, fn);
2677 retval = safe__call (inhibit_quit, 2, fn, ap);
2678 va_end (ap);
2679 return retval;
2680 }
2681
2682 static Lisp_Object Qeval;
2683
2684 Lisp_Object
2685 safe_eval (Lisp_Object sexpr)
2686 {
2687 return safe__call1 (false, Qeval, sexpr);
2688 }
2689
2690 static Lisp_Object
2691 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2692 {
2693 return safe__call1 (inhibit_quit, Qeval, sexpr);
2694 }
2695
2696 /* Call function FN with two arguments ARG1 and ARG2.
2697 Return the result, or nil if something went wrong. */
2698
2699 Lisp_Object
2700 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2701 {
2702 return safe_call (3, fn, arg1, arg2);
2703 }
2704
2705
2706 \f
2707 /***********************************************************************
2708 Debugging
2709 ***********************************************************************/
2710
2711 #if 0
2712
2713 /* Define CHECK_IT to perform sanity checks on iterators.
2714 This is for debugging. It is too slow to do unconditionally. */
2715
2716 static void
2717 check_it (struct it *it)
2718 {
2719 if (it->method == GET_FROM_STRING)
2720 {
2721 eassert (STRINGP (it->string));
2722 eassert (IT_STRING_CHARPOS (*it) >= 0);
2723 }
2724 else
2725 {
2726 eassert (IT_STRING_CHARPOS (*it) < 0);
2727 if (it->method == GET_FROM_BUFFER)
2728 {
2729 /* Check that character and byte positions agree. */
2730 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2731 }
2732 }
2733
2734 if (it->dpvec)
2735 eassert (it->current.dpvec_index >= 0);
2736 else
2737 eassert (it->current.dpvec_index < 0);
2738 }
2739
2740 #define CHECK_IT(IT) check_it ((IT))
2741
2742 #else /* not 0 */
2743
2744 #define CHECK_IT(IT) (void) 0
2745
2746 #endif /* not 0 */
2747
2748
2749 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2750
2751 /* Check that the window end of window W is what we expect it
2752 to be---the last row in the current matrix displaying text. */
2753
2754 static void
2755 check_window_end (struct window *w)
2756 {
2757 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2758 {
2759 struct glyph_row *row;
2760 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2761 !row->enabled_p
2762 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2763 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2764 }
2765 }
2766
2767 #define CHECK_WINDOW_END(W) check_window_end ((W))
2768
2769 #else
2770
2771 #define CHECK_WINDOW_END(W) (void) 0
2772
2773 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2774
2775 /***********************************************************************
2776 Iterator initialization
2777 ***********************************************************************/
2778
2779 /* Initialize IT for displaying current_buffer in window W, starting
2780 at character position CHARPOS. CHARPOS < 0 means that no buffer
2781 position is specified which is useful when the iterator is assigned
2782 a position later. BYTEPOS is the byte position corresponding to
2783 CHARPOS.
2784
2785 If ROW is not null, calls to produce_glyphs with IT as parameter
2786 will produce glyphs in that row.
2787
2788 BASE_FACE_ID is the id of a base face to use. It must be one of
2789 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2790 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2791 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2792
2793 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2794 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2795 will be initialized to use the corresponding mode line glyph row of
2796 the desired matrix of W. */
2797
2798 void
2799 init_iterator (struct it *it, struct window *w,
2800 ptrdiff_t charpos, ptrdiff_t bytepos,
2801 struct glyph_row *row, enum face_id base_face_id)
2802 {
2803 enum face_id remapped_base_face_id = base_face_id;
2804
2805 /* Some precondition checks. */
2806 eassert (w != NULL && it != NULL);
2807 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2808 && charpos <= ZV));
2809
2810 /* If face attributes have been changed since the last redisplay,
2811 free realized faces now because they depend on face definitions
2812 that might have changed. Don't free faces while there might be
2813 desired matrices pending which reference these faces. */
2814 if (face_change_count && !inhibit_free_realized_faces)
2815 {
2816 face_change_count = 0;
2817 free_all_realized_faces (Qnil);
2818 }
2819
2820 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2821 if (! NILP (Vface_remapping_alist))
2822 remapped_base_face_id
2823 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2824
2825 /* Use one of the mode line rows of W's desired matrix if
2826 appropriate. */
2827 if (row == NULL)
2828 {
2829 if (base_face_id == MODE_LINE_FACE_ID
2830 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2831 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2832 else if (base_face_id == HEADER_LINE_FACE_ID)
2833 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2834 }
2835
2836 /* Clear IT. */
2837 memset (it, 0, sizeof *it);
2838 it->current.overlay_string_index = -1;
2839 it->current.dpvec_index = -1;
2840 it->base_face_id = remapped_base_face_id;
2841 it->string = Qnil;
2842 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2843 it->paragraph_embedding = L2R;
2844 it->bidi_it.string.lstring = Qnil;
2845 it->bidi_it.string.s = NULL;
2846 it->bidi_it.string.bufpos = 0;
2847 it->bidi_it.w = w;
2848
2849 /* The window in which we iterate over current_buffer: */
2850 XSETWINDOW (it->window, w);
2851 it->w = w;
2852 it->f = XFRAME (w->frame);
2853
2854 it->cmp_it.id = -1;
2855
2856 /* Extra space between lines (on window systems only). */
2857 if (base_face_id == DEFAULT_FACE_ID
2858 && FRAME_WINDOW_P (it->f))
2859 {
2860 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2861 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2862 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2863 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2864 * FRAME_LINE_HEIGHT (it->f));
2865 else if (it->f->extra_line_spacing > 0)
2866 it->extra_line_spacing = it->f->extra_line_spacing;
2867 it->max_extra_line_spacing = 0;
2868 }
2869
2870 /* If realized faces have been removed, e.g. because of face
2871 attribute changes of named faces, recompute them. When running
2872 in batch mode, the face cache of the initial frame is null. If
2873 we happen to get called, make a dummy face cache. */
2874 if (FRAME_FACE_CACHE (it->f) == NULL)
2875 init_frame_faces (it->f);
2876 if (FRAME_FACE_CACHE (it->f)->used == 0)
2877 recompute_basic_faces (it->f);
2878
2879 /* Current value of the `slice', `space-width', and 'height' properties. */
2880 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2881 it->space_width = Qnil;
2882 it->font_height = Qnil;
2883 it->override_ascent = -1;
2884
2885 /* Are control characters displayed as `^C'? */
2886 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2887
2888 /* -1 means everything between a CR and the following line end
2889 is invisible. >0 means lines indented more than this value are
2890 invisible. */
2891 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2892 ? (clip_to_bounds
2893 (-1, XINT (BVAR (current_buffer, selective_display)),
2894 PTRDIFF_MAX))
2895 : (!NILP (BVAR (current_buffer, selective_display))
2896 ? -1 : 0));
2897 it->selective_display_ellipsis_p
2898 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2899
2900 /* Display table to use. */
2901 it->dp = window_display_table (w);
2902
2903 /* Are multibyte characters enabled in current_buffer? */
2904 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2905
2906 /* Get the position at which the redisplay_end_trigger hook should
2907 be run, if it is to be run at all. */
2908 if (MARKERP (w->redisplay_end_trigger)
2909 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2910 it->redisplay_end_trigger_charpos
2911 = marker_position (w->redisplay_end_trigger);
2912 else if (INTEGERP (w->redisplay_end_trigger))
2913 it->redisplay_end_trigger_charpos
2914 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2915 PTRDIFF_MAX);
2916
2917 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2918
2919 /* Are lines in the display truncated? */
2920 if (base_face_id != DEFAULT_FACE_ID
2921 || it->w->hscroll
2922 || (! WINDOW_FULL_WIDTH_P (it->w)
2923 && ((!NILP (Vtruncate_partial_width_windows)
2924 && !INTEGERP (Vtruncate_partial_width_windows))
2925 || (INTEGERP (Vtruncate_partial_width_windows)
2926 /* PXW: Shall we do something about this? */
2927 && (WINDOW_TOTAL_COLS (it->w)
2928 < XINT (Vtruncate_partial_width_windows))))))
2929 it->line_wrap = TRUNCATE;
2930 else if (NILP (BVAR (current_buffer, truncate_lines)))
2931 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2932 ? WINDOW_WRAP : WORD_WRAP;
2933 else
2934 it->line_wrap = TRUNCATE;
2935
2936 /* Get dimensions of truncation and continuation glyphs. These are
2937 displayed as fringe bitmaps under X, but we need them for such
2938 frames when the fringes are turned off. But leave the dimensions
2939 zero for tooltip frames, as these glyphs look ugly there and also
2940 sabotage calculations of tooltip dimensions in x-show-tip. */
2941 #ifdef HAVE_WINDOW_SYSTEM
2942 if (!(FRAME_WINDOW_P (it->f)
2943 && FRAMEP (tip_frame)
2944 && it->f == XFRAME (tip_frame)))
2945 #endif
2946 {
2947 if (it->line_wrap == TRUNCATE)
2948 {
2949 /* We will need the truncation glyph. */
2950 eassert (it->glyph_row == NULL);
2951 produce_special_glyphs (it, IT_TRUNCATION);
2952 it->truncation_pixel_width = it->pixel_width;
2953 }
2954 else
2955 {
2956 /* We will need the continuation glyph. */
2957 eassert (it->glyph_row == NULL);
2958 produce_special_glyphs (it, IT_CONTINUATION);
2959 it->continuation_pixel_width = it->pixel_width;
2960 }
2961 }
2962
2963 /* Reset these values to zero because the produce_special_glyphs
2964 above has changed them. */
2965 it->pixel_width = it->ascent = it->descent = 0;
2966 it->phys_ascent = it->phys_descent = 0;
2967
2968 /* Set this after getting the dimensions of truncation and
2969 continuation glyphs, so that we don't produce glyphs when calling
2970 produce_special_glyphs, above. */
2971 it->glyph_row = row;
2972 it->area = TEXT_AREA;
2973
2974 /* Forget any previous info about this row being reversed. */
2975 if (it->glyph_row)
2976 it->glyph_row->reversed_p = 0;
2977
2978 /* Get the dimensions of the display area. The display area
2979 consists of the visible window area plus a horizontally scrolled
2980 part to the left of the window. All x-values are relative to the
2981 start of this total display area. */
2982 if (base_face_id != DEFAULT_FACE_ID)
2983 {
2984 /* Mode lines, menu bar in terminal frames. */
2985 it->first_visible_x = 0;
2986 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2987 }
2988 else
2989 {
2990 it->first_visible_x
2991 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2992 it->last_visible_x = (it->first_visible_x
2993 + window_box_width (w, TEXT_AREA));
2994
2995 /* If we truncate lines, leave room for the truncation glyph(s) at
2996 the right margin. Otherwise, leave room for the continuation
2997 glyph(s). Done only if the window has no fringes. Since we
2998 don't know at this point whether there will be any R2L lines in
2999 the window, we reserve space for truncation/continuation glyphs
3000 even if only one of the fringes is absent. */
3001 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
3002 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
3003 {
3004 if (it->line_wrap == TRUNCATE)
3005 it->last_visible_x -= it->truncation_pixel_width;
3006 else
3007 it->last_visible_x -= it->continuation_pixel_width;
3008 }
3009
3010 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
3011 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
3012 }
3013
3014 /* Leave room for a border glyph. */
3015 if (!FRAME_WINDOW_P (it->f)
3016 && !WINDOW_RIGHTMOST_P (it->w))
3017 it->last_visible_x -= 1;
3018
3019 it->last_visible_y = window_text_bottom_y (w);
3020
3021 /* For mode lines and alike, arrange for the first glyph having a
3022 left box line if the face specifies a box. */
3023 if (base_face_id != DEFAULT_FACE_ID)
3024 {
3025 struct face *face;
3026
3027 it->face_id = remapped_base_face_id;
3028
3029 /* If we have a boxed mode line, make the first character appear
3030 with a left box line. */
3031 face = FACE_FROM_ID (it->f, remapped_base_face_id);
3032 if (face && face->box != FACE_NO_BOX)
3033 it->start_of_box_run_p = true;
3034 }
3035
3036 /* If a buffer position was specified, set the iterator there,
3037 getting overlays and face properties from that position. */
3038 if (charpos >= BUF_BEG (current_buffer))
3039 {
3040 it->stop_charpos = charpos;
3041 it->end_charpos = ZV;
3042 eassert (charpos == BYTE_TO_CHAR (bytepos));
3043 IT_CHARPOS (*it) = charpos;
3044 IT_BYTEPOS (*it) = bytepos;
3045
3046 /* We will rely on `reseat' to set this up properly, via
3047 handle_face_prop. */
3048 it->face_id = it->base_face_id;
3049
3050 it->start = it->current;
3051 /* Do we need to reorder bidirectional text? Not if this is a
3052 unibyte buffer: by definition, none of the single-byte
3053 characters are strong R2L, so no reordering is needed. And
3054 bidi.c doesn't support unibyte buffers anyway. Also, don't
3055 reorder while we are loading loadup.el, since the tables of
3056 character properties needed for reordering are not yet
3057 available. */
3058 it->bidi_p =
3059 NILP (Vpurify_flag)
3060 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3061 && it->multibyte_p;
3062
3063 /* If we are to reorder bidirectional text, init the bidi
3064 iterator. */
3065 if (it->bidi_p)
3066 {
3067 /* Note the paragraph direction that this buffer wants to
3068 use. */
3069 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3070 Qleft_to_right))
3071 it->paragraph_embedding = L2R;
3072 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3073 Qright_to_left))
3074 it->paragraph_embedding = R2L;
3075 else
3076 it->paragraph_embedding = NEUTRAL_DIR;
3077 bidi_unshelve_cache (NULL, 0);
3078 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3079 &it->bidi_it);
3080 }
3081
3082 /* Compute faces etc. */
3083 reseat (it, it->current.pos, 1);
3084 }
3085
3086 CHECK_IT (it);
3087 }
3088
3089
3090 /* Initialize IT for the display of window W with window start POS. */
3091
3092 void
3093 start_display (struct it *it, struct window *w, struct text_pos pos)
3094 {
3095 struct glyph_row *row;
3096 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3097
3098 row = w->desired_matrix->rows + first_vpos;
3099 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3100 it->first_vpos = first_vpos;
3101
3102 /* Don't reseat to previous visible line start if current start
3103 position is in a string or image. */
3104 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3105 {
3106 int start_at_line_beg_p;
3107 int first_y = it->current_y;
3108
3109 /* If window start is not at a line start, skip forward to POS to
3110 get the correct continuation lines width. */
3111 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3112 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3113 if (!start_at_line_beg_p)
3114 {
3115 int new_x;
3116
3117 reseat_at_previous_visible_line_start (it);
3118 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3119
3120 new_x = it->current_x + it->pixel_width;
3121
3122 /* If lines are continued, this line may end in the middle
3123 of a multi-glyph character (e.g. a control character
3124 displayed as \003, or in the middle of an overlay
3125 string). In this case move_it_to above will not have
3126 taken us to the start of the continuation line but to the
3127 end of the continued line. */
3128 if (it->current_x > 0
3129 && it->line_wrap != TRUNCATE /* Lines are continued. */
3130 && (/* And glyph doesn't fit on the line. */
3131 new_x > it->last_visible_x
3132 /* Or it fits exactly and we're on a window
3133 system frame. */
3134 || (new_x == it->last_visible_x
3135 && FRAME_WINDOW_P (it->f)
3136 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3137 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3138 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3139 {
3140 if ((it->current.dpvec_index >= 0
3141 || it->current.overlay_string_index >= 0)
3142 /* If we are on a newline from a display vector or
3143 overlay string, then we are already at the end of
3144 a screen line; no need to go to the next line in
3145 that case, as this line is not really continued.
3146 (If we do go to the next line, C-e will not DTRT.) */
3147 && it->c != '\n')
3148 {
3149 set_iterator_to_next (it, 1);
3150 move_it_in_display_line_to (it, -1, -1, 0);
3151 }
3152
3153 it->continuation_lines_width += it->current_x;
3154 }
3155 /* If the character at POS is displayed via a display
3156 vector, move_it_to above stops at the final glyph of
3157 IT->dpvec. To make the caller redisplay that character
3158 again (a.k.a. start at POS), we need to reset the
3159 dpvec_index to the beginning of IT->dpvec. */
3160 else if (it->current.dpvec_index >= 0)
3161 it->current.dpvec_index = 0;
3162
3163 /* We're starting a new display line, not affected by the
3164 height of the continued line, so clear the appropriate
3165 fields in the iterator structure. */
3166 it->max_ascent = it->max_descent = 0;
3167 it->max_phys_ascent = it->max_phys_descent = 0;
3168
3169 it->current_y = first_y;
3170 it->vpos = 0;
3171 it->current_x = it->hpos = 0;
3172 }
3173 }
3174 }
3175
3176
3177 /* Return 1 if POS is a position in ellipses displayed for invisible
3178 text. W is the window we display, for text property lookup. */
3179
3180 static int
3181 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3182 {
3183 Lisp_Object prop, window;
3184 int ellipses_p = 0;
3185 ptrdiff_t charpos = CHARPOS (pos->pos);
3186
3187 /* If POS specifies a position in a display vector, this might
3188 be for an ellipsis displayed for invisible text. We won't
3189 get the iterator set up for delivering that ellipsis unless
3190 we make sure that it gets aware of the invisible text. */
3191 if (pos->dpvec_index >= 0
3192 && pos->overlay_string_index < 0
3193 && CHARPOS (pos->string_pos) < 0
3194 && charpos > BEGV
3195 && (XSETWINDOW (window, w),
3196 prop = Fget_char_property (make_number (charpos),
3197 Qinvisible, window),
3198 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3199 {
3200 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3201 window);
3202 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3203 }
3204
3205 return ellipses_p;
3206 }
3207
3208
3209 /* Initialize IT for stepping through current_buffer in window W,
3210 starting at position POS that includes overlay string and display
3211 vector/ control character translation position information. Value
3212 is zero if there are overlay strings with newlines at POS. */
3213
3214 static int
3215 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3216 {
3217 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3218 int i, overlay_strings_with_newlines = 0;
3219
3220 /* If POS specifies a position in a display vector, this might
3221 be for an ellipsis displayed for invisible text. We won't
3222 get the iterator set up for delivering that ellipsis unless
3223 we make sure that it gets aware of the invisible text. */
3224 if (in_ellipses_for_invisible_text_p (pos, w))
3225 {
3226 --charpos;
3227 bytepos = 0;
3228 }
3229
3230 /* Keep in mind: the call to reseat in init_iterator skips invisible
3231 text, so we might end up at a position different from POS. This
3232 is only a problem when POS is a row start after a newline and an
3233 overlay starts there with an after-string, and the overlay has an
3234 invisible property. Since we don't skip invisible text in
3235 display_line and elsewhere immediately after consuming the
3236 newline before the row start, such a POS will not be in a string,
3237 but the call to init_iterator below will move us to the
3238 after-string. */
3239 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3240
3241 /* This only scans the current chunk -- it should scan all chunks.
3242 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3243 to 16 in 22.1 to make this a lesser problem. */
3244 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3245 {
3246 const char *s = SSDATA (it->overlay_strings[i]);
3247 const char *e = s + SBYTES (it->overlay_strings[i]);
3248
3249 while (s < e && *s != '\n')
3250 ++s;
3251
3252 if (s < e)
3253 {
3254 overlay_strings_with_newlines = 1;
3255 break;
3256 }
3257 }
3258
3259 /* If position is within an overlay string, set up IT to the right
3260 overlay string. */
3261 if (pos->overlay_string_index >= 0)
3262 {
3263 int relative_index;
3264
3265 /* If the first overlay string happens to have a `display'
3266 property for an image, the iterator will be set up for that
3267 image, and we have to undo that setup first before we can
3268 correct the overlay string index. */
3269 if (it->method == GET_FROM_IMAGE)
3270 pop_it (it);
3271
3272 /* We already have the first chunk of overlay strings in
3273 IT->overlay_strings. Load more until the one for
3274 pos->overlay_string_index is in IT->overlay_strings. */
3275 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3276 {
3277 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3278 it->current.overlay_string_index = 0;
3279 while (n--)
3280 {
3281 load_overlay_strings (it, 0);
3282 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3283 }
3284 }
3285
3286 it->current.overlay_string_index = pos->overlay_string_index;
3287 relative_index = (it->current.overlay_string_index
3288 % OVERLAY_STRING_CHUNK_SIZE);
3289 it->string = it->overlay_strings[relative_index];
3290 eassert (STRINGP (it->string));
3291 it->current.string_pos = pos->string_pos;
3292 it->method = GET_FROM_STRING;
3293 it->end_charpos = SCHARS (it->string);
3294 /* Set up the bidi iterator for this overlay string. */
3295 if (it->bidi_p)
3296 {
3297 it->bidi_it.string.lstring = it->string;
3298 it->bidi_it.string.s = NULL;
3299 it->bidi_it.string.schars = SCHARS (it->string);
3300 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3301 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3302 it->bidi_it.string.unibyte = !it->multibyte_p;
3303 it->bidi_it.w = it->w;
3304 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3305 FRAME_WINDOW_P (it->f), &it->bidi_it);
3306
3307 /* Synchronize the state of the bidi iterator with
3308 pos->string_pos. For any string position other than
3309 zero, this will be done automagically when we resume
3310 iteration over the string and get_visually_first_element
3311 is called. But if string_pos is zero, and the string is
3312 to be reordered for display, we need to resync manually,
3313 since it could be that the iteration state recorded in
3314 pos ended at string_pos of 0 moving backwards in string. */
3315 if (CHARPOS (pos->string_pos) == 0)
3316 {
3317 get_visually_first_element (it);
3318 if (IT_STRING_CHARPOS (*it) != 0)
3319 do {
3320 /* Paranoia. */
3321 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3322 bidi_move_to_visually_next (&it->bidi_it);
3323 } while (it->bidi_it.charpos != 0);
3324 }
3325 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3326 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3327 }
3328 }
3329
3330 if (CHARPOS (pos->string_pos) >= 0)
3331 {
3332 /* Recorded position is not in an overlay string, but in another
3333 string. This can only be a string from a `display' property.
3334 IT should already be filled with that string. */
3335 it->current.string_pos = pos->string_pos;
3336 eassert (STRINGP (it->string));
3337 if (it->bidi_p)
3338 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3339 FRAME_WINDOW_P (it->f), &it->bidi_it);
3340 }
3341
3342 /* Restore position in display vector translations, control
3343 character translations or ellipses. */
3344 if (pos->dpvec_index >= 0)
3345 {
3346 if (it->dpvec == NULL)
3347 get_next_display_element (it);
3348 eassert (it->dpvec && it->current.dpvec_index == 0);
3349 it->current.dpvec_index = pos->dpvec_index;
3350 }
3351
3352 CHECK_IT (it);
3353 return !overlay_strings_with_newlines;
3354 }
3355
3356
3357 /* Initialize IT for stepping through current_buffer in window W
3358 starting at ROW->start. */
3359
3360 static void
3361 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3362 {
3363 init_from_display_pos (it, w, &row->start);
3364 it->start = row->start;
3365 it->continuation_lines_width = row->continuation_lines_width;
3366 CHECK_IT (it);
3367 }
3368
3369
3370 /* Initialize IT for stepping through current_buffer in window W
3371 starting in the line following ROW, i.e. starting at ROW->end.
3372 Value is zero if there are overlay strings with newlines at ROW's
3373 end position. */
3374
3375 static int
3376 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3377 {
3378 int success = 0;
3379
3380 if (init_from_display_pos (it, w, &row->end))
3381 {
3382 if (row->continued_p)
3383 it->continuation_lines_width
3384 = row->continuation_lines_width + row->pixel_width;
3385 CHECK_IT (it);
3386 success = 1;
3387 }
3388
3389 return success;
3390 }
3391
3392
3393
3394 \f
3395 /***********************************************************************
3396 Text properties
3397 ***********************************************************************/
3398
3399 /* Called when IT reaches IT->stop_charpos. Handle text property and
3400 overlay changes. Set IT->stop_charpos to the next position where
3401 to stop. */
3402
3403 static void
3404 handle_stop (struct it *it)
3405 {
3406 enum prop_handled handled;
3407 int handle_overlay_change_p;
3408 struct props *p;
3409
3410 it->dpvec = NULL;
3411 it->current.dpvec_index = -1;
3412 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3413 it->ignore_overlay_strings_at_pos_p = 0;
3414 it->ellipsis_p = 0;
3415
3416 /* Use face of preceding text for ellipsis (if invisible) */
3417 if (it->selective_display_ellipsis_p)
3418 it->saved_face_id = it->face_id;
3419
3420 /* Here's the description of the semantics of, and the logic behind,
3421 the various HANDLED_* statuses:
3422
3423 HANDLED_NORMALLY means the handler did its job, and the loop
3424 should proceed to calling the next handler in order.
3425
3426 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3427 change in the properties and overlays at current position, so the
3428 loop should be restarted, to re-invoke the handlers that were
3429 already called. This happens when fontification-functions were
3430 called by handle_fontified_prop, and actually fontified
3431 something. Another case where HANDLED_RECOMPUTE_PROPS is
3432 returned is when we discover overlay strings that need to be
3433 displayed right away. The loop below will continue for as long
3434 as the status is HANDLED_RECOMPUTE_PROPS.
3435
3436 HANDLED_RETURN means return immediately to the caller, to
3437 continue iteration without calling any further handlers. This is
3438 used when we need to act on some property right away, for example
3439 when we need to display the ellipsis or a replacing display
3440 property, such as display string or image.
3441
3442 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3443 consumed, and the handler switched to the next overlay string.
3444 This signals the loop below to refrain from looking for more
3445 overlays before all the overlay strings of the current overlay
3446 are processed.
3447
3448 Some of the handlers called by the loop push the iterator state
3449 onto the stack (see 'push_it'), and arrange for the iteration to
3450 continue with another object, such as an image, a display string,
3451 or an overlay string. In most such cases, it->stop_charpos is
3452 set to the first character of the string, so that when the
3453 iteration resumes, this function will immediately be called
3454 again, to examine the properties at the beginning of the string.
3455
3456 When a display or overlay string is exhausted, the iterator state
3457 is popped (see 'pop_it'), and iteration continues with the
3458 previous object. Again, in many such cases this function is
3459 called again to find the next position where properties might
3460 change. */
3461
3462 do
3463 {
3464 handled = HANDLED_NORMALLY;
3465
3466 /* Call text property handlers. */
3467 for (p = it_props; p->handler; ++p)
3468 {
3469 handled = p->handler (it);
3470
3471 if (handled == HANDLED_RECOMPUTE_PROPS)
3472 break;
3473 else if (handled == HANDLED_RETURN)
3474 {
3475 /* We still want to show before and after strings from
3476 overlays even if the actual buffer text is replaced. */
3477 if (!handle_overlay_change_p
3478 || it->sp > 1
3479 /* Don't call get_overlay_strings_1 if we already
3480 have overlay strings loaded, because doing so
3481 will load them again and push the iterator state
3482 onto the stack one more time, which is not
3483 expected by the rest of the code that processes
3484 overlay strings. */
3485 || (it->current.overlay_string_index < 0
3486 ? !get_overlay_strings_1 (it, 0, 0)
3487 : 0))
3488 {
3489 if (it->ellipsis_p)
3490 setup_for_ellipsis (it, 0);
3491 /* When handling a display spec, we might load an
3492 empty string. In that case, discard it here. We
3493 used to discard it in handle_single_display_spec,
3494 but that causes get_overlay_strings_1, above, to
3495 ignore overlay strings that we must check. */
3496 if (STRINGP (it->string) && !SCHARS (it->string))
3497 pop_it (it);
3498 return;
3499 }
3500 else if (STRINGP (it->string) && !SCHARS (it->string))
3501 pop_it (it);
3502 else
3503 {
3504 it->ignore_overlay_strings_at_pos_p = true;
3505 it->string_from_display_prop_p = 0;
3506 it->from_disp_prop_p = 0;
3507 handle_overlay_change_p = 0;
3508 }
3509 handled = HANDLED_RECOMPUTE_PROPS;
3510 break;
3511 }
3512 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3513 handle_overlay_change_p = 0;
3514 }
3515
3516 if (handled != HANDLED_RECOMPUTE_PROPS)
3517 {
3518 /* Don't check for overlay strings below when set to deliver
3519 characters from a display vector. */
3520 if (it->method == GET_FROM_DISPLAY_VECTOR)
3521 handle_overlay_change_p = 0;
3522
3523 /* Handle overlay changes.
3524 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3525 if it finds overlays. */
3526 if (handle_overlay_change_p)
3527 handled = handle_overlay_change (it);
3528 }
3529
3530 if (it->ellipsis_p)
3531 {
3532 setup_for_ellipsis (it, 0);
3533 break;
3534 }
3535 }
3536 while (handled == HANDLED_RECOMPUTE_PROPS);
3537
3538 /* Determine where to stop next. */
3539 if (handled == HANDLED_NORMALLY)
3540 compute_stop_pos (it);
3541 }
3542
3543
3544 /* Compute IT->stop_charpos from text property and overlay change
3545 information for IT's current position. */
3546
3547 static void
3548 compute_stop_pos (struct it *it)
3549 {
3550 register INTERVAL iv, next_iv;
3551 Lisp_Object object, limit, position;
3552 ptrdiff_t charpos, bytepos;
3553
3554 if (STRINGP (it->string))
3555 {
3556 /* Strings are usually short, so don't limit the search for
3557 properties. */
3558 it->stop_charpos = it->end_charpos;
3559 object = it->string;
3560 limit = Qnil;
3561 charpos = IT_STRING_CHARPOS (*it);
3562 bytepos = IT_STRING_BYTEPOS (*it);
3563 }
3564 else
3565 {
3566 ptrdiff_t pos;
3567
3568 /* If end_charpos is out of range for some reason, such as a
3569 misbehaving display function, rationalize it (Bug#5984). */
3570 if (it->end_charpos > ZV)
3571 it->end_charpos = ZV;
3572 it->stop_charpos = it->end_charpos;
3573
3574 /* If next overlay change is in front of the current stop pos
3575 (which is IT->end_charpos), stop there. Note: value of
3576 next_overlay_change is point-max if no overlay change
3577 follows. */
3578 charpos = IT_CHARPOS (*it);
3579 bytepos = IT_BYTEPOS (*it);
3580 pos = next_overlay_change (charpos);
3581 if (pos < it->stop_charpos)
3582 it->stop_charpos = pos;
3583
3584 /* Set up variables for computing the stop position from text
3585 property changes. */
3586 XSETBUFFER (object, current_buffer);
3587 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3588 }
3589
3590 /* Get the interval containing IT's position. Value is a null
3591 interval if there isn't such an interval. */
3592 position = make_number (charpos);
3593 iv = validate_interval_range (object, &position, &position, 0);
3594 if (iv)
3595 {
3596 Lisp_Object values_here[LAST_PROP_IDX];
3597 struct props *p;
3598
3599 /* Get properties here. */
3600 for (p = it_props; p->handler; ++p)
3601 values_here[p->idx] = textget (iv->plist, *p->name);
3602
3603 /* Look for an interval following iv that has different
3604 properties. */
3605 for (next_iv = next_interval (iv);
3606 (next_iv
3607 && (NILP (limit)
3608 || XFASTINT (limit) > next_iv->position));
3609 next_iv = next_interval (next_iv))
3610 {
3611 for (p = it_props; p->handler; ++p)
3612 {
3613 Lisp_Object new_value;
3614
3615 new_value = textget (next_iv->plist, *p->name);
3616 if (!EQ (values_here[p->idx], new_value))
3617 break;
3618 }
3619
3620 if (p->handler)
3621 break;
3622 }
3623
3624 if (next_iv)
3625 {
3626 if (INTEGERP (limit)
3627 && next_iv->position >= XFASTINT (limit))
3628 /* No text property change up to limit. */
3629 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3630 else
3631 /* Text properties change in next_iv. */
3632 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3633 }
3634 }
3635
3636 if (it->cmp_it.id < 0)
3637 {
3638 ptrdiff_t stoppos = it->end_charpos;
3639
3640 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3641 stoppos = -1;
3642 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3643 stoppos, it->string);
3644 }
3645
3646 eassert (STRINGP (it->string)
3647 || (it->stop_charpos >= BEGV
3648 && it->stop_charpos >= IT_CHARPOS (*it)));
3649 }
3650
3651
3652 /* Return the position of the next overlay change after POS in
3653 current_buffer. Value is point-max if no overlay change
3654 follows. This is like `next-overlay-change' but doesn't use
3655 xmalloc. */
3656
3657 static ptrdiff_t
3658 next_overlay_change (ptrdiff_t pos)
3659 {
3660 ptrdiff_t i, noverlays;
3661 ptrdiff_t endpos;
3662 Lisp_Object *overlays;
3663
3664 /* Get all overlays at the given position. */
3665 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3666
3667 /* If any of these overlays ends before endpos,
3668 use its ending point instead. */
3669 for (i = 0; i < noverlays; ++i)
3670 {
3671 Lisp_Object oend;
3672 ptrdiff_t oendpos;
3673
3674 oend = OVERLAY_END (overlays[i]);
3675 oendpos = OVERLAY_POSITION (oend);
3676 endpos = min (endpos, oendpos);
3677 }
3678
3679 return endpos;
3680 }
3681
3682 /* How many characters forward to search for a display property or
3683 display string. Searching too far forward makes the bidi display
3684 sluggish, especially in small windows. */
3685 #define MAX_DISP_SCAN 250
3686
3687 /* Return the character position of a display string at or after
3688 position specified by POSITION. If no display string exists at or
3689 after POSITION, return ZV. A display string is either an overlay
3690 with `display' property whose value is a string, or a `display'
3691 text property whose value is a string. STRING is data about the
3692 string to iterate; if STRING->lstring is nil, we are iterating a
3693 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3694 on a GUI frame. DISP_PROP is set to zero if we searched
3695 MAX_DISP_SCAN characters forward without finding any display
3696 strings, non-zero otherwise. It is set to 2 if the display string
3697 uses any kind of `(space ...)' spec that will produce a stretch of
3698 white space in the text area. */
3699 ptrdiff_t
3700 compute_display_string_pos (struct text_pos *position,
3701 struct bidi_string_data *string,
3702 struct window *w,
3703 int frame_window_p, int *disp_prop)
3704 {
3705 /* OBJECT = nil means current buffer. */
3706 Lisp_Object object, object1;
3707 Lisp_Object pos, spec, limpos;
3708 int string_p = (string && (STRINGP (string->lstring) || string->s));
3709 ptrdiff_t eob = string_p ? string->schars : ZV;
3710 ptrdiff_t begb = string_p ? 0 : BEGV;
3711 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3712 ptrdiff_t lim =
3713 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3714 struct text_pos tpos;
3715 int rv = 0;
3716
3717 if (string && STRINGP (string->lstring))
3718 object1 = object = string->lstring;
3719 else if (w && !string_p)
3720 {
3721 XSETWINDOW (object, w);
3722 object1 = Qnil;
3723 }
3724 else
3725 object1 = object = Qnil;
3726
3727 *disp_prop = 1;
3728
3729 if (charpos >= eob
3730 /* We don't support display properties whose values are strings
3731 that have display string properties. */
3732 || string->from_disp_str
3733 /* C strings cannot have display properties. */
3734 || (string->s && !STRINGP (object)))
3735 {
3736 *disp_prop = 0;
3737 return eob;
3738 }
3739
3740 /* If the character at CHARPOS is where the display string begins,
3741 return CHARPOS. */
3742 pos = make_number (charpos);
3743 if (STRINGP (object))
3744 bufpos = string->bufpos;
3745 else
3746 bufpos = charpos;
3747 tpos = *position;
3748 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3749 && (charpos <= begb
3750 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3751 object),
3752 spec))
3753 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3754 frame_window_p)))
3755 {
3756 if (rv == 2)
3757 *disp_prop = 2;
3758 return charpos;
3759 }
3760
3761 /* Look forward for the first character with a `display' property
3762 that will replace the underlying text when displayed. */
3763 limpos = make_number (lim);
3764 do {
3765 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3766 CHARPOS (tpos) = XFASTINT (pos);
3767 if (CHARPOS (tpos) >= lim)
3768 {
3769 *disp_prop = 0;
3770 break;
3771 }
3772 if (STRINGP (object))
3773 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3774 else
3775 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3776 spec = Fget_char_property (pos, Qdisplay, object);
3777 if (!STRINGP (object))
3778 bufpos = CHARPOS (tpos);
3779 } while (NILP (spec)
3780 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3781 bufpos, frame_window_p)));
3782 if (rv == 2)
3783 *disp_prop = 2;
3784
3785 return CHARPOS (tpos);
3786 }
3787
3788 /* Return the character position of the end of the display string that
3789 started at CHARPOS. If there's no display string at CHARPOS,
3790 return -1. A display string is either an overlay with `display'
3791 property whose value is a string or a `display' text property whose
3792 value is a string. */
3793 ptrdiff_t
3794 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3795 {
3796 /* OBJECT = nil means current buffer. */
3797 Lisp_Object object =
3798 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3799 Lisp_Object pos = make_number (charpos);
3800 ptrdiff_t eob =
3801 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3802
3803 if (charpos >= eob || (string->s && !STRINGP (object)))
3804 return eob;
3805
3806 /* It could happen that the display property or overlay was removed
3807 since we found it in compute_display_string_pos above. One way
3808 this can happen is if JIT font-lock was called (through
3809 handle_fontified_prop), and jit-lock-functions remove text
3810 properties or overlays from the portion of buffer that includes
3811 CHARPOS. Muse mode is known to do that, for example. In this
3812 case, we return -1 to the caller, to signal that no display
3813 string is actually present at CHARPOS. See bidi_fetch_char for
3814 how this is handled.
3815
3816 An alternative would be to never look for display properties past
3817 it->stop_charpos. But neither compute_display_string_pos nor
3818 bidi_fetch_char that calls it know or care where the next
3819 stop_charpos is. */
3820 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3821 return -1;
3822
3823 /* Look forward for the first character where the `display' property
3824 changes. */
3825 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3826
3827 return XFASTINT (pos);
3828 }
3829
3830
3831 \f
3832 /***********************************************************************
3833 Fontification
3834 ***********************************************************************/
3835
3836 /* Handle changes in the `fontified' property of the current buffer by
3837 calling hook functions from Qfontification_functions to fontify
3838 regions of text. */
3839
3840 static enum prop_handled
3841 handle_fontified_prop (struct it *it)
3842 {
3843 Lisp_Object prop, pos;
3844 enum prop_handled handled = HANDLED_NORMALLY;
3845
3846 if (!NILP (Vmemory_full))
3847 return handled;
3848
3849 /* Get the value of the `fontified' property at IT's current buffer
3850 position. (The `fontified' property doesn't have a special
3851 meaning in strings.) If the value is nil, call functions from
3852 Qfontification_functions. */
3853 if (!STRINGP (it->string)
3854 && it->s == NULL
3855 && !NILP (Vfontification_functions)
3856 && !NILP (Vrun_hooks)
3857 && (pos = make_number (IT_CHARPOS (*it)),
3858 prop = Fget_char_property (pos, Qfontified, Qnil),
3859 /* Ignore the special cased nil value always present at EOB since
3860 no amount of fontifying will be able to change it. */
3861 NILP (prop) && IT_CHARPOS (*it) < Z))
3862 {
3863 ptrdiff_t count = SPECPDL_INDEX ();
3864 Lisp_Object val;
3865 struct buffer *obuf = current_buffer;
3866 ptrdiff_t begv = BEGV, zv = ZV;
3867 bool old_clip_changed = current_buffer->clip_changed;
3868
3869 val = Vfontification_functions;
3870 specbind (Qfontification_functions, Qnil);
3871
3872 eassert (it->end_charpos == ZV);
3873
3874 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3875 safe_call1 (val, pos);
3876 else
3877 {
3878 Lisp_Object fns, fn;
3879 struct gcpro gcpro1, gcpro2;
3880
3881 fns = Qnil;
3882 GCPRO2 (val, fns);
3883
3884 for (; CONSP (val); val = XCDR (val))
3885 {
3886 fn = XCAR (val);
3887
3888 if (EQ (fn, Qt))
3889 {
3890 /* A value of t indicates this hook has a local
3891 binding; it means to run the global binding too.
3892 In a global value, t should not occur. If it
3893 does, we must ignore it to avoid an endless
3894 loop. */
3895 for (fns = Fdefault_value (Qfontification_functions);
3896 CONSP (fns);
3897 fns = XCDR (fns))
3898 {
3899 fn = XCAR (fns);
3900 if (!EQ (fn, Qt))
3901 safe_call1 (fn, pos);
3902 }
3903 }
3904 else
3905 safe_call1 (fn, pos);
3906 }
3907
3908 UNGCPRO;
3909 }
3910
3911 unbind_to (count, Qnil);
3912
3913 /* Fontification functions routinely call `save-restriction'.
3914 Normally, this tags clip_changed, which can confuse redisplay
3915 (see discussion in Bug#6671). Since we don't perform any
3916 special handling of fontification changes in the case where
3917 `save-restriction' isn't called, there's no point doing so in
3918 this case either. So, if the buffer's restrictions are
3919 actually left unchanged, reset clip_changed. */
3920 if (obuf == current_buffer)
3921 {
3922 if (begv == BEGV && zv == ZV)
3923 current_buffer->clip_changed = old_clip_changed;
3924 }
3925 /* There isn't much we can reasonably do to protect against
3926 misbehaving fontification, but here's a fig leaf. */
3927 else if (BUFFER_LIVE_P (obuf))
3928 set_buffer_internal_1 (obuf);
3929
3930 /* The fontification code may have added/removed text.
3931 It could do even a lot worse, but let's at least protect against
3932 the most obvious case where only the text past `pos' gets changed',
3933 as is/was done in grep.el where some escapes sequences are turned
3934 into face properties (bug#7876). */
3935 it->end_charpos = ZV;
3936
3937 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3938 something. This avoids an endless loop if they failed to
3939 fontify the text for which reason ever. */
3940 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3941 handled = HANDLED_RECOMPUTE_PROPS;
3942 }
3943
3944 return handled;
3945 }
3946
3947
3948 \f
3949 /***********************************************************************
3950 Faces
3951 ***********************************************************************/
3952
3953 /* Set up iterator IT from face properties at its current position.
3954 Called from handle_stop. */
3955
3956 static enum prop_handled
3957 handle_face_prop (struct it *it)
3958 {
3959 int new_face_id;
3960 ptrdiff_t next_stop;
3961
3962 if (!STRINGP (it->string))
3963 {
3964 new_face_id
3965 = face_at_buffer_position (it->w,
3966 IT_CHARPOS (*it),
3967 &next_stop,
3968 (IT_CHARPOS (*it)
3969 + TEXT_PROP_DISTANCE_LIMIT),
3970 0, it->base_face_id);
3971
3972 /* Is this a start of a run of characters with box face?
3973 Caveat: this can be called for a freshly initialized
3974 iterator; face_id is -1 in this case. We know that the new
3975 face will not change until limit, i.e. if the new face has a
3976 box, all characters up to limit will have one. But, as
3977 usual, we don't know whether limit is really the end. */
3978 if (new_face_id != it->face_id)
3979 {
3980 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3981 /* If it->face_id is -1, old_face below will be NULL, see
3982 the definition of FACE_FROM_ID. This will happen if this
3983 is the initial call that gets the face. */
3984 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3985
3986 /* If the value of face_id of the iterator is -1, we have to
3987 look in front of IT's position and see whether there is a
3988 face there that's different from new_face_id. */
3989 if (!old_face && IT_CHARPOS (*it) > BEG)
3990 {
3991 int prev_face_id = face_before_it_pos (it);
3992
3993 old_face = FACE_FROM_ID (it->f, prev_face_id);
3994 }
3995
3996 /* If the new face has a box, but the old face does not,
3997 this is the start of a run of characters with box face,
3998 i.e. this character has a shadow on the left side. */
3999 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
4000 && (old_face == NULL || !old_face->box));
4001 it->face_box_p = new_face->box != FACE_NO_BOX;
4002 }
4003 }
4004 else
4005 {
4006 int base_face_id;
4007 ptrdiff_t bufpos;
4008 int i;
4009 Lisp_Object from_overlay
4010 = (it->current.overlay_string_index >= 0
4011 ? it->string_overlays[it->current.overlay_string_index
4012 % OVERLAY_STRING_CHUNK_SIZE]
4013 : Qnil);
4014
4015 /* See if we got to this string directly or indirectly from
4016 an overlay property. That includes the before-string or
4017 after-string of an overlay, strings in display properties
4018 provided by an overlay, their text properties, etc.
4019
4020 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
4021 if (! NILP (from_overlay))
4022 for (i = it->sp - 1; i >= 0; i--)
4023 {
4024 if (it->stack[i].current.overlay_string_index >= 0)
4025 from_overlay
4026 = it->string_overlays[it->stack[i].current.overlay_string_index
4027 % OVERLAY_STRING_CHUNK_SIZE];
4028 else if (! NILP (it->stack[i].from_overlay))
4029 from_overlay = it->stack[i].from_overlay;
4030
4031 if (!NILP (from_overlay))
4032 break;
4033 }
4034
4035 if (! NILP (from_overlay))
4036 {
4037 bufpos = IT_CHARPOS (*it);
4038 /* For a string from an overlay, the base face depends
4039 only on text properties and ignores overlays. */
4040 base_face_id
4041 = face_for_overlay_string (it->w,
4042 IT_CHARPOS (*it),
4043 &next_stop,
4044 (IT_CHARPOS (*it)
4045 + TEXT_PROP_DISTANCE_LIMIT),
4046 0,
4047 from_overlay);
4048 }
4049 else
4050 {
4051 bufpos = 0;
4052
4053 /* For strings from a `display' property, use the face at
4054 IT's current buffer position as the base face to merge
4055 with, so that overlay strings appear in the same face as
4056 surrounding text, unless they specify their own faces.
4057 For strings from wrap-prefix and line-prefix properties,
4058 use the default face, possibly remapped via
4059 Vface_remapping_alist. */
4060 /* Note that the fact that we use the face at _buffer_
4061 position means that a 'display' property on an overlay
4062 string will not inherit the face of that overlay string,
4063 but will instead revert to the face of buffer text
4064 covered by the overlay. This is visible, e.g., when the
4065 overlay specifies a box face, but neither the buffer nor
4066 the display string do. This sounds like a design bug,
4067 but Emacs always did that since v21.1, so changing that
4068 might be a big deal. */
4069 base_face_id = it->string_from_prefix_prop_p
4070 ? (!NILP (Vface_remapping_alist)
4071 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4072 : DEFAULT_FACE_ID)
4073 : underlying_face_id (it);
4074 }
4075
4076 new_face_id = face_at_string_position (it->w,
4077 it->string,
4078 IT_STRING_CHARPOS (*it),
4079 bufpos,
4080 &next_stop,
4081 base_face_id, 0);
4082
4083 /* Is this a start of a run of characters with box? Caveat:
4084 this can be called for a freshly allocated iterator; face_id
4085 is -1 is this case. We know that the new face will not
4086 change until the next check pos, i.e. if the new face has a
4087 box, all characters up to that position will have a
4088 box. But, as usual, we don't know whether that position
4089 is really the end. */
4090 if (new_face_id != it->face_id)
4091 {
4092 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4093 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4094
4095 /* If new face has a box but old face hasn't, this is the
4096 start of a run of characters with box, i.e. it has a
4097 shadow on the left side. */
4098 it->start_of_box_run_p
4099 = new_face->box && (old_face == NULL || !old_face->box);
4100 it->face_box_p = new_face->box != FACE_NO_BOX;
4101 }
4102 }
4103
4104 it->face_id = new_face_id;
4105 return HANDLED_NORMALLY;
4106 }
4107
4108
4109 /* Return the ID of the face ``underlying'' IT's current position,
4110 which is in a string. If the iterator is associated with a
4111 buffer, return the face at IT's current buffer position.
4112 Otherwise, use the iterator's base_face_id. */
4113
4114 static int
4115 underlying_face_id (struct it *it)
4116 {
4117 int face_id = it->base_face_id, i;
4118
4119 eassert (STRINGP (it->string));
4120
4121 for (i = it->sp - 1; i >= 0; --i)
4122 if (NILP (it->stack[i].string))
4123 face_id = it->stack[i].face_id;
4124
4125 return face_id;
4126 }
4127
4128
4129 /* Compute the face one character before or after the current position
4130 of IT, in the visual order. BEFORE_P non-zero means get the face
4131 in front (to the left in L2R paragraphs, to the right in R2L
4132 paragraphs) of IT's screen position. Value is the ID of the face. */
4133
4134 static int
4135 face_before_or_after_it_pos (struct it *it, int before_p)
4136 {
4137 int face_id, limit;
4138 ptrdiff_t next_check_charpos;
4139 struct it it_copy;
4140 void *it_copy_data = NULL;
4141
4142 eassert (it->s == NULL);
4143
4144 if (STRINGP (it->string))
4145 {
4146 ptrdiff_t bufpos, charpos;
4147 int base_face_id;
4148
4149 /* No face change past the end of the string (for the case
4150 we are padding with spaces). No face change before the
4151 string start. */
4152 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4153 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4154 return it->face_id;
4155
4156 if (!it->bidi_p)
4157 {
4158 /* Set charpos to the position before or after IT's current
4159 position, in the logical order, which in the non-bidi
4160 case is the same as the visual order. */
4161 if (before_p)
4162 charpos = IT_STRING_CHARPOS (*it) - 1;
4163 else if (it->what == IT_COMPOSITION)
4164 /* For composition, we must check the character after the
4165 composition. */
4166 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4167 else
4168 charpos = IT_STRING_CHARPOS (*it) + 1;
4169 }
4170 else
4171 {
4172 if (before_p)
4173 {
4174 /* With bidi iteration, the character before the current
4175 in the visual order cannot be found by simple
4176 iteration, because "reverse" reordering is not
4177 supported. Instead, we need to use the move_it_*
4178 family of functions. */
4179 /* Ignore face changes before the first visible
4180 character on this display line. */
4181 if (it->current_x <= it->first_visible_x)
4182 return it->face_id;
4183 SAVE_IT (it_copy, *it, it_copy_data);
4184 /* Implementation note: Since move_it_in_display_line
4185 works in the iterator geometry, and thinks the first
4186 character is always the leftmost, even in R2L lines,
4187 we don't need to distinguish between the R2L and L2R
4188 cases here. */
4189 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4190 it_copy.current_x - 1, MOVE_TO_X);
4191 charpos = IT_STRING_CHARPOS (it_copy);
4192 RESTORE_IT (it, it, it_copy_data);
4193 }
4194 else
4195 {
4196 /* Set charpos to the string position of the character
4197 that comes after IT's current position in the visual
4198 order. */
4199 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4200
4201 it_copy = *it;
4202 while (n--)
4203 bidi_move_to_visually_next (&it_copy.bidi_it);
4204
4205 charpos = it_copy.bidi_it.charpos;
4206 }
4207 }
4208 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4209
4210 if (it->current.overlay_string_index >= 0)
4211 bufpos = IT_CHARPOS (*it);
4212 else
4213 bufpos = 0;
4214
4215 base_face_id = underlying_face_id (it);
4216
4217 /* Get the face for ASCII, or unibyte. */
4218 face_id = face_at_string_position (it->w,
4219 it->string,
4220 charpos,
4221 bufpos,
4222 &next_check_charpos,
4223 base_face_id, 0);
4224
4225 /* Correct the face for charsets different from ASCII. Do it
4226 for the multibyte case only. The face returned above is
4227 suitable for unibyte text if IT->string is unibyte. */
4228 if (STRING_MULTIBYTE (it->string))
4229 {
4230 struct text_pos pos1 = string_pos (charpos, it->string);
4231 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4232 int c, len;
4233 struct face *face = FACE_FROM_ID (it->f, face_id);
4234
4235 c = string_char_and_length (p, &len);
4236 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4237 }
4238 }
4239 else
4240 {
4241 struct text_pos pos;
4242
4243 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4244 || (IT_CHARPOS (*it) <= BEGV && before_p))
4245 return it->face_id;
4246
4247 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4248 pos = it->current.pos;
4249
4250 if (!it->bidi_p)
4251 {
4252 if (before_p)
4253 DEC_TEXT_POS (pos, it->multibyte_p);
4254 else
4255 {
4256 if (it->what == IT_COMPOSITION)
4257 {
4258 /* For composition, we must check the position after
4259 the composition. */
4260 pos.charpos += it->cmp_it.nchars;
4261 pos.bytepos += it->len;
4262 }
4263 else
4264 INC_TEXT_POS (pos, it->multibyte_p);
4265 }
4266 }
4267 else
4268 {
4269 if (before_p)
4270 {
4271 /* With bidi iteration, the character before the current
4272 in the visual order cannot be found by simple
4273 iteration, because "reverse" reordering is not
4274 supported. Instead, we need to use the move_it_*
4275 family of functions. */
4276 /* Ignore face changes before the first visible
4277 character on this display line. */
4278 if (it->current_x <= it->first_visible_x)
4279 return it->face_id;
4280 SAVE_IT (it_copy, *it, it_copy_data);
4281 /* Implementation note: Since move_it_in_display_line
4282 works in the iterator geometry, and thinks the first
4283 character is always the leftmost, even in R2L lines,
4284 we don't need to distinguish between the R2L and L2R
4285 cases here. */
4286 move_it_in_display_line (&it_copy, ZV,
4287 it_copy.current_x - 1, MOVE_TO_X);
4288 pos = it_copy.current.pos;
4289 RESTORE_IT (it, it, it_copy_data);
4290 }
4291 else
4292 {
4293 /* Set charpos to the buffer position of the character
4294 that comes after IT's current position in the visual
4295 order. */
4296 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4297
4298 it_copy = *it;
4299 while (n--)
4300 bidi_move_to_visually_next (&it_copy.bidi_it);
4301
4302 SET_TEXT_POS (pos,
4303 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4304 }
4305 }
4306 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4307
4308 /* Determine face for CHARSET_ASCII, or unibyte. */
4309 face_id = face_at_buffer_position (it->w,
4310 CHARPOS (pos),
4311 &next_check_charpos,
4312 limit, 0, -1);
4313
4314 /* Correct the face for charsets different from ASCII. Do it
4315 for the multibyte case only. The face returned above is
4316 suitable for unibyte text if current_buffer is unibyte. */
4317 if (it->multibyte_p)
4318 {
4319 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4320 struct face *face = FACE_FROM_ID (it->f, face_id);
4321 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4322 }
4323 }
4324
4325 return face_id;
4326 }
4327
4328
4329 \f
4330 /***********************************************************************
4331 Invisible text
4332 ***********************************************************************/
4333
4334 /* Set up iterator IT from invisible properties at its current
4335 position. Called from handle_stop. */
4336
4337 static enum prop_handled
4338 handle_invisible_prop (struct it *it)
4339 {
4340 enum prop_handled handled = HANDLED_NORMALLY;
4341 int invis_p;
4342 Lisp_Object prop;
4343
4344 if (STRINGP (it->string))
4345 {
4346 Lisp_Object end_charpos, limit, charpos;
4347
4348 /* Get the value of the invisible text property at the
4349 current position. Value will be nil if there is no such
4350 property. */
4351 charpos = make_number (IT_STRING_CHARPOS (*it));
4352 prop = Fget_text_property (charpos, Qinvisible, it->string);
4353 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4354
4355 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4356 {
4357 /* Record whether we have to display an ellipsis for the
4358 invisible text. */
4359 int display_ellipsis_p = (invis_p == 2);
4360 ptrdiff_t len, endpos;
4361
4362 handled = HANDLED_RECOMPUTE_PROPS;
4363
4364 /* Get the position at which the next visible text can be
4365 found in IT->string, if any. */
4366 endpos = len = SCHARS (it->string);
4367 XSETINT (limit, len);
4368 do
4369 {
4370 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4371 it->string, limit);
4372 if (INTEGERP (end_charpos))
4373 {
4374 endpos = XFASTINT (end_charpos);
4375 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4376 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4377 if (invis_p == 2)
4378 display_ellipsis_p = true;
4379 }
4380 }
4381 while (invis_p && endpos < len);
4382
4383 if (display_ellipsis_p)
4384 it->ellipsis_p = true;
4385
4386 if (endpos < len)
4387 {
4388 /* Text at END_CHARPOS is visible. Move IT there. */
4389 struct text_pos old;
4390 ptrdiff_t oldpos;
4391
4392 old = it->current.string_pos;
4393 oldpos = CHARPOS (old);
4394 if (it->bidi_p)
4395 {
4396 if (it->bidi_it.first_elt
4397 && it->bidi_it.charpos < SCHARS (it->string))
4398 bidi_paragraph_init (it->paragraph_embedding,
4399 &it->bidi_it, 1);
4400 /* Bidi-iterate out of the invisible text. */
4401 do
4402 {
4403 bidi_move_to_visually_next (&it->bidi_it);
4404 }
4405 while (oldpos <= it->bidi_it.charpos
4406 && it->bidi_it.charpos < endpos);
4407
4408 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4409 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4410 if (IT_CHARPOS (*it) >= endpos)
4411 it->prev_stop = endpos;
4412 }
4413 else
4414 {
4415 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4416 compute_string_pos (&it->current.string_pos, old, it->string);
4417 }
4418 }
4419 else
4420 {
4421 /* The rest of the string is invisible. If this is an
4422 overlay string, proceed with the next overlay string
4423 or whatever comes and return a character from there. */
4424 if (it->current.overlay_string_index >= 0
4425 && !display_ellipsis_p)
4426 {
4427 next_overlay_string (it);
4428 /* Don't check for overlay strings when we just
4429 finished processing them. */
4430 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4431 }
4432 else
4433 {
4434 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4435 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4436 }
4437 }
4438 }
4439 }
4440 else
4441 {
4442 ptrdiff_t newpos, next_stop, start_charpos, tem;
4443 Lisp_Object pos, overlay;
4444
4445 /* First of all, is there invisible text at this position? */
4446 tem = start_charpos = IT_CHARPOS (*it);
4447 pos = make_number (tem);
4448 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4449 &overlay);
4450 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4451
4452 /* If we are on invisible text, skip over it. */
4453 if (invis_p && start_charpos < it->end_charpos)
4454 {
4455 /* Record whether we have to display an ellipsis for the
4456 invisible text. */
4457 int display_ellipsis_p = invis_p == 2;
4458
4459 handled = HANDLED_RECOMPUTE_PROPS;
4460
4461 /* Loop skipping over invisible text. The loop is left at
4462 ZV or with IT on the first char being visible again. */
4463 do
4464 {
4465 /* Try to skip some invisible text. Return value is the
4466 position reached which can be equal to where we start
4467 if there is nothing invisible there. This skips both
4468 over invisible text properties and overlays with
4469 invisible property. */
4470 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4471
4472 /* If we skipped nothing at all we weren't at invisible
4473 text in the first place. If everything to the end of
4474 the buffer was skipped, end the loop. */
4475 if (newpos == tem || newpos >= ZV)
4476 invis_p = 0;
4477 else
4478 {
4479 /* We skipped some characters but not necessarily
4480 all there are. Check if we ended up on visible
4481 text. Fget_char_property returns the property of
4482 the char before the given position, i.e. if we
4483 get invis_p = 0, this means that the char at
4484 newpos is visible. */
4485 pos = make_number (newpos);
4486 prop = Fget_char_property (pos, Qinvisible, it->window);
4487 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4488 }
4489
4490 /* If we ended up on invisible text, proceed to
4491 skip starting with next_stop. */
4492 if (invis_p)
4493 tem = next_stop;
4494
4495 /* If there are adjacent invisible texts, don't lose the
4496 second one's ellipsis. */
4497 if (invis_p == 2)
4498 display_ellipsis_p = true;
4499 }
4500 while (invis_p);
4501
4502 /* The position newpos is now either ZV or on visible text. */
4503 if (it->bidi_p)
4504 {
4505 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4506 int on_newline
4507 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4508 int after_newline
4509 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4510
4511 /* If the invisible text ends on a newline or on a
4512 character after a newline, we can avoid the costly,
4513 character by character, bidi iteration to NEWPOS, and
4514 instead simply reseat the iterator there. That's
4515 because all bidi reordering information is tossed at
4516 the newline. This is a big win for modes that hide
4517 complete lines, like Outline, Org, etc. */
4518 if (on_newline || after_newline)
4519 {
4520 struct text_pos tpos;
4521 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4522
4523 SET_TEXT_POS (tpos, newpos, bpos);
4524 reseat_1 (it, tpos, 0);
4525 /* If we reseat on a newline/ZV, we need to prep the
4526 bidi iterator for advancing to the next character
4527 after the newline/EOB, keeping the current paragraph
4528 direction (so that PRODUCE_GLYPHS does TRT wrt
4529 prepending/appending glyphs to a glyph row). */
4530 if (on_newline)
4531 {
4532 it->bidi_it.first_elt = 0;
4533 it->bidi_it.paragraph_dir = pdir;
4534 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4535 it->bidi_it.nchars = 1;
4536 it->bidi_it.ch_len = 1;
4537 }
4538 }
4539 else /* Must use the slow method. */
4540 {
4541 /* With bidi iteration, the region of invisible text
4542 could start and/or end in the middle of a
4543 non-base embedding level. Therefore, we need to
4544 skip invisible text using the bidi iterator,
4545 starting at IT's current position, until we find
4546 ourselves outside of the invisible text.
4547 Skipping invisible text _after_ bidi iteration
4548 avoids affecting the visual order of the
4549 displayed text when invisible properties are
4550 added or removed. */
4551 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4552 {
4553 /* If we were `reseat'ed to a new paragraph,
4554 determine the paragraph base direction. We
4555 need to do it now because
4556 next_element_from_buffer may not have a
4557 chance to do it, if we are going to skip any
4558 text at the beginning, which resets the
4559 FIRST_ELT flag. */
4560 bidi_paragraph_init (it->paragraph_embedding,
4561 &it->bidi_it, 1);
4562 }
4563 do
4564 {
4565 bidi_move_to_visually_next (&it->bidi_it);
4566 }
4567 while (it->stop_charpos <= it->bidi_it.charpos
4568 && it->bidi_it.charpos < newpos);
4569 IT_CHARPOS (*it) = it->bidi_it.charpos;
4570 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4571 /* If we overstepped NEWPOS, record its position in
4572 the iterator, so that we skip invisible text if
4573 later the bidi iteration lands us in the
4574 invisible region again. */
4575 if (IT_CHARPOS (*it) >= newpos)
4576 it->prev_stop = newpos;
4577 }
4578 }
4579 else
4580 {
4581 IT_CHARPOS (*it) = newpos;
4582 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4583 }
4584
4585 /* If there are before-strings at the start of invisible
4586 text, and the text is invisible because of a text
4587 property, arrange to show before-strings because 20.x did
4588 it that way. (If the text is invisible because of an
4589 overlay property instead of a text property, this is
4590 already handled in the overlay code.) */
4591 if (NILP (overlay)
4592 && get_overlay_strings (it, it->stop_charpos))
4593 {
4594 handled = HANDLED_RECOMPUTE_PROPS;
4595 if (it->sp > 0)
4596 {
4597 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4598 /* The call to get_overlay_strings above recomputes
4599 it->stop_charpos, but it only considers changes
4600 in properties and overlays beyond iterator's
4601 current position. This causes us to miss changes
4602 that happen exactly where the invisible property
4603 ended. So we play it safe here and force the
4604 iterator to check for potential stop positions
4605 immediately after the invisible text. Note that
4606 if get_overlay_strings returns non-zero, it
4607 normally also pushed the iterator stack, so we
4608 need to update the stop position in the slot
4609 below the current one. */
4610 it->stack[it->sp - 1].stop_charpos
4611 = CHARPOS (it->stack[it->sp - 1].current.pos);
4612 }
4613 }
4614 else if (display_ellipsis_p)
4615 {
4616 /* Make sure that the glyphs of the ellipsis will get
4617 correct `charpos' values. If we would not update
4618 it->position here, the glyphs would belong to the
4619 last visible character _before_ the invisible
4620 text, which confuses `set_cursor_from_row'.
4621
4622 We use the last invisible position instead of the
4623 first because this way the cursor is always drawn on
4624 the first "." of the ellipsis, whenever PT is inside
4625 the invisible text. Otherwise the cursor would be
4626 placed _after_ the ellipsis when the point is after the
4627 first invisible character. */
4628 if (!STRINGP (it->object))
4629 {
4630 it->position.charpos = newpos - 1;
4631 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4632 }
4633 it->ellipsis_p = true;
4634 /* Let the ellipsis display before
4635 considering any properties of the following char.
4636 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4637 handled = HANDLED_RETURN;
4638 }
4639 }
4640 }
4641
4642 return handled;
4643 }
4644
4645
4646 /* Make iterator IT return `...' next.
4647 Replaces LEN characters from buffer. */
4648
4649 static void
4650 setup_for_ellipsis (struct it *it, int len)
4651 {
4652 /* Use the display table definition for `...'. Invalid glyphs
4653 will be handled by the method returning elements from dpvec. */
4654 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4655 {
4656 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4657 it->dpvec = v->contents;
4658 it->dpend = v->contents + v->header.size;
4659 }
4660 else
4661 {
4662 /* Default `...'. */
4663 it->dpvec = default_invis_vector;
4664 it->dpend = default_invis_vector + 3;
4665 }
4666
4667 it->dpvec_char_len = len;
4668 it->current.dpvec_index = 0;
4669 it->dpvec_face_id = -1;
4670
4671 /* Remember the current face id in case glyphs specify faces.
4672 IT's face is restored in set_iterator_to_next.
4673 saved_face_id was set to preceding char's face in handle_stop. */
4674 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4675 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4676
4677 it->method = GET_FROM_DISPLAY_VECTOR;
4678 it->ellipsis_p = true;
4679 }
4680
4681
4682 \f
4683 /***********************************************************************
4684 'display' property
4685 ***********************************************************************/
4686
4687 /* Set up iterator IT from `display' property at its current position.
4688 Called from handle_stop.
4689 We return HANDLED_RETURN if some part of the display property
4690 overrides the display of the buffer text itself.
4691 Otherwise we return HANDLED_NORMALLY. */
4692
4693 static enum prop_handled
4694 handle_display_prop (struct it *it)
4695 {
4696 Lisp_Object propval, object, overlay;
4697 struct text_pos *position;
4698 ptrdiff_t bufpos;
4699 /* Nonzero if some property replaces the display of the text itself. */
4700 int display_replaced_p = 0;
4701
4702 if (STRINGP (it->string))
4703 {
4704 object = it->string;
4705 position = &it->current.string_pos;
4706 bufpos = CHARPOS (it->current.pos);
4707 }
4708 else
4709 {
4710 XSETWINDOW (object, it->w);
4711 position = &it->current.pos;
4712 bufpos = CHARPOS (*position);
4713 }
4714
4715 /* Reset those iterator values set from display property values. */
4716 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4717 it->space_width = Qnil;
4718 it->font_height = Qnil;
4719 it->voffset = 0;
4720
4721 /* We don't support recursive `display' properties, i.e. string
4722 values that have a string `display' property, that have a string
4723 `display' property etc. */
4724 if (!it->string_from_display_prop_p)
4725 it->area = TEXT_AREA;
4726
4727 propval = get_char_property_and_overlay (make_number (position->charpos),
4728 Qdisplay, object, &overlay);
4729 if (NILP (propval))
4730 return HANDLED_NORMALLY;
4731 /* Now OVERLAY is the overlay that gave us this property, or nil
4732 if it was a text property. */
4733
4734 if (!STRINGP (it->string))
4735 object = it->w->contents;
4736
4737 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4738 position, bufpos,
4739 FRAME_WINDOW_P (it->f));
4740
4741 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4742 }
4743
4744 /* Subroutine of handle_display_prop. Returns non-zero if the display
4745 specification in SPEC is a replacing specification, i.e. it would
4746 replace the text covered by `display' property with something else,
4747 such as an image or a display string. If SPEC includes any kind or
4748 `(space ...) specification, the value is 2; this is used by
4749 compute_display_string_pos, which see.
4750
4751 See handle_single_display_spec for documentation of arguments.
4752 frame_window_p is non-zero if the window being redisplayed is on a
4753 GUI frame; this argument is used only if IT is NULL, see below.
4754
4755 IT can be NULL, if this is called by the bidi reordering code
4756 through compute_display_string_pos, which see. In that case, this
4757 function only examines SPEC, but does not otherwise "handle" it, in
4758 the sense that it doesn't set up members of IT from the display
4759 spec. */
4760 static int
4761 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4762 Lisp_Object overlay, struct text_pos *position,
4763 ptrdiff_t bufpos, int frame_window_p)
4764 {
4765 int replacing_p = 0;
4766 int rv;
4767
4768 if (CONSP (spec)
4769 /* Simple specifications. */
4770 && !EQ (XCAR (spec), Qimage)
4771 && !EQ (XCAR (spec), Qspace)
4772 && !EQ (XCAR (spec), Qwhen)
4773 && !EQ (XCAR (spec), Qslice)
4774 && !EQ (XCAR (spec), Qspace_width)
4775 && !EQ (XCAR (spec), Qheight)
4776 && !EQ (XCAR (spec), Qraise)
4777 /* Marginal area specifications. */
4778 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4779 && !EQ (XCAR (spec), Qleft_fringe)
4780 && !EQ (XCAR (spec), Qright_fringe)
4781 && !NILP (XCAR (spec)))
4782 {
4783 for (; CONSP (spec); spec = XCDR (spec))
4784 {
4785 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4786 overlay, position, bufpos,
4787 replacing_p, frame_window_p)))
4788 {
4789 replacing_p = rv;
4790 /* If some text in a string is replaced, `position' no
4791 longer points to the position of `object'. */
4792 if (!it || STRINGP (object))
4793 break;
4794 }
4795 }
4796 }
4797 else if (VECTORP (spec))
4798 {
4799 ptrdiff_t i;
4800 for (i = 0; i < ASIZE (spec); ++i)
4801 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4802 overlay, position, bufpos,
4803 replacing_p, frame_window_p)))
4804 {
4805 replacing_p = rv;
4806 /* If some text in a string is replaced, `position' no
4807 longer points to the position of `object'. */
4808 if (!it || STRINGP (object))
4809 break;
4810 }
4811 }
4812 else
4813 {
4814 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4815 position, bufpos, 0,
4816 frame_window_p)))
4817 replacing_p = rv;
4818 }
4819
4820 return replacing_p;
4821 }
4822
4823 /* Value is the position of the end of the `display' property starting
4824 at START_POS in OBJECT. */
4825
4826 static struct text_pos
4827 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4828 {
4829 Lisp_Object end;
4830 struct text_pos end_pos;
4831
4832 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4833 Qdisplay, object, Qnil);
4834 CHARPOS (end_pos) = XFASTINT (end);
4835 if (STRINGP (object))
4836 compute_string_pos (&end_pos, start_pos, it->string);
4837 else
4838 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4839
4840 return end_pos;
4841 }
4842
4843
4844 /* Set up IT from a single `display' property specification SPEC. OBJECT
4845 is the object in which the `display' property was found. *POSITION
4846 is the position in OBJECT at which the `display' property was found.
4847 BUFPOS is the buffer position of OBJECT (different from POSITION if
4848 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4849 previously saw a display specification which already replaced text
4850 display with something else, for example an image; we ignore such
4851 properties after the first one has been processed.
4852
4853 OVERLAY is the overlay this `display' property came from,
4854 or nil if it was a text property.
4855
4856 If SPEC is a `space' or `image' specification, and in some other
4857 cases too, set *POSITION to the position where the `display'
4858 property ends.
4859
4860 If IT is NULL, only examine the property specification in SPEC, but
4861 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4862 is intended to be displayed in a window on a GUI frame.
4863
4864 Value is non-zero if something was found which replaces the display
4865 of buffer or string text. */
4866
4867 static int
4868 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4869 Lisp_Object overlay, struct text_pos *position,
4870 ptrdiff_t bufpos, int display_replaced_p,
4871 int frame_window_p)
4872 {
4873 Lisp_Object form;
4874 Lisp_Object location, value;
4875 struct text_pos start_pos = *position;
4876 int valid_p;
4877
4878 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4879 If the result is non-nil, use VALUE instead of SPEC. */
4880 form = Qt;
4881 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4882 {
4883 spec = XCDR (spec);
4884 if (!CONSP (spec))
4885 return 0;
4886 form = XCAR (spec);
4887 spec = XCDR (spec);
4888 }
4889
4890 if (!NILP (form) && !EQ (form, Qt))
4891 {
4892 ptrdiff_t count = SPECPDL_INDEX ();
4893 struct gcpro gcpro1;
4894
4895 /* Bind `object' to the object having the `display' property, a
4896 buffer or string. Bind `position' to the position in the
4897 object where the property was found, and `buffer-position'
4898 to the current position in the buffer. */
4899
4900 if (NILP (object))
4901 XSETBUFFER (object, current_buffer);
4902 specbind (Qobject, object);
4903 specbind (Qposition, make_number (CHARPOS (*position)));
4904 specbind (Qbuffer_position, make_number (bufpos));
4905 GCPRO1 (form);
4906 form = safe_eval (form);
4907 UNGCPRO;
4908 unbind_to (count, Qnil);
4909 }
4910
4911 if (NILP (form))
4912 return 0;
4913
4914 /* Handle `(height HEIGHT)' specifications. */
4915 if (CONSP (spec)
4916 && EQ (XCAR (spec), Qheight)
4917 && CONSP (XCDR (spec)))
4918 {
4919 if (it)
4920 {
4921 if (!FRAME_WINDOW_P (it->f))
4922 return 0;
4923
4924 it->font_height = XCAR (XCDR (spec));
4925 if (!NILP (it->font_height))
4926 {
4927 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4928 int new_height = -1;
4929
4930 if (CONSP (it->font_height)
4931 && (EQ (XCAR (it->font_height), Qplus)
4932 || EQ (XCAR (it->font_height), Qminus))
4933 && CONSP (XCDR (it->font_height))
4934 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4935 {
4936 /* `(+ N)' or `(- N)' where N is an integer. */
4937 int steps = XINT (XCAR (XCDR (it->font_height)));
4938 if (EQ (XCAR (it->font_height), Qplus))
4939 steps = - steps;
4940 it->face_id = smaller_face (it->f, it->face_id, steps);
4941 }
4942 else if (FUNCTIONP (it->font_height))
4943 {
4944 /* Call function with current height as argument.
4945 Value is the new height. */
4946 Lisp_Object height;
4947 height = safe_call1 (it->font_height,
4948 face->lface[LFACE_HEIGHT_INDEX]);
4949 if (NUMBERP (height))
4950 new_height = XFLOATINT (height);
4951 }
4952 else if (NUMBERP (it->font_height))
4953 {
4954 /* Value is a multiple of the canonical char height. */
4955 struct face *f;
4956
4957 f = FACE_FROM_ID (it->f,
4958 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4959 new_height = (XFLOATINT (it->font_height)
4960 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4961 }
4962 else
4963 {
4964 /* Evaluate IT->font_height with `height' bound to the
4965 current specified height to get the new height. */
4966 ptrdiff_t count = SPECPDL_INDEX ();
4967
4968 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4969 value = safe_eval (it->font_height);
4970 unbind_to (count, Qnil);
4971
4972 if (NUMBERP (value))
4973 new_height = XFLOATINT (value);
4974 }
4975
4976 if (new_height > 0)
4977 it->face_id = face_with_height (it->f, it->face_id, new_height);
4978 }
4979 }
4980
4981 return 0;
4982 }
4983
4984 /* Handle `(space-width WIDTH)'. */
4985 if (CONSP (spec)
4986 && EQ (XCAR (spec), Qspace_width)
4987 && CONSP (XCDR (spec)))
4988 {
4989 if (it)
4990 {
4991 if (!FRAME_WINDOW_P (it->f))
4992 return 0;
4993
4994 value = XCAR (XCDR (spec));
4995 if (NUMBERP (value) && XFLOATINT (value) > 0)
4996 it->space_width = value;
4997 }
4998
4999 return 0;
5000 }
5001
5002 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5003 if (CONSP (spec)
5004 && EQ (XCAR (spec), Qslice))
5005 {
5006 Lisp_Object tem;
5007
5008 if (it)
5009 {
5010 if (!FRAME_WINDOW_P (it->f))
5011 return 0;
5012
5013 if (tem = XCDR (spec), CONSP (tem))
5014 {
5015 it->slice.x = XCAR (tem);
5016 if (tem = XCDR (tem), CONSP (tem))
5017 {
5018 it->slice.y = XCAR (tem);
5019 if (tem = XCDR (tem), CONSP (tem))
5020 {
5021 it->slice.width = XCAR (tem);
5022 if (tem = XCDR (tem), CONSP (tem))
5023 it->slice.height = XCAR (tem);
5024 }
5025 }
5026 }
5027 }
5028
5029 return 0;
5030 }
5031
5032 /* Handle `(raise FACTOR)'. */
5033 if (CONSP (spec)
5034 && EQ (XCAR (spec), Qraise)
5035 && CONSP (XCDR (spec)))
5036 {
5037 if (it)
5038 {
5039 if (!FRAME_WINDOW_P (it->f))
5040 return 0;
5041
5042 #ifdef HAVE_WINDOW_SYSTEM
5043 value = XCAR (XCDR (spec));
5044 if (NUMBERP (value))
5045 {
5046 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5047 it->voffset = - (XFLOATINT (value)
5048 * (FONT_HEIGHT (face->font)));
5049 }
5050 #endif /* HAVE_WINDOW_SYSTEM */
5051 }
5052
5053 return 0;
5054 }
5055
5056 /* Don't handle the other kinds of display specifications
5057 inside a string that we got from a `display' property. */
5058 if (it && it->string_from_display_prop_p)
5059 return 0;
5060
5061 /* Characters having this form of property are not displayed, so
5062 we have to find the end of the property. */
5063 if (it)
5064 {
5065 start_pos = *position;
5066 *position = display_prop_end (it, object, start_pos);
5067 }
5068 value = Qnil;
5069
5070 /* Stop the scan at that end position--we assume that all
5071 text properties change there. */
5072 if (it)
5073 it->stop_charpos = position->charpos;
5074
5075 /* Handle `(left-fringe BITMAP [FACE])'
5076 and `(right-fringe BITMAP [FACE])'. */
5077 if (CONSP (spec)
5078 && (EQ (XCAR (spec), Qleft_fringe)
5079 || EQ (XCAR (spec), Qright_fringe))
5080 && CONSP (XCDR (spec)))
5081 {
5082 int fringe_bitmap;
5083
5084 if (it)
5085 {
5086 if (!FRAME_WINDOW_P (it->f))
5087 /* If we return here, POSITION has been advanced
5088 across the text with this property. */
5089 {
5090 /* Synchronize the bidi iterator with POSITION. This is
5091 needed because we are not going to push the iterator
5092 on behalf of this display property, so there will be
5093 no pop_it call to do this synchronization for us. */
5094 if (it->bidi_p)
5095 {
5096 it->position = *position;
5097 iterate_out_of_display_property (it);
5098 *position = it->position;
5099 }
5100 return 1;
5101 }
5102 }
5103 else if (!frame_window_p)
5104 return 1;
5105
5106 #ifdef HAVE_WINDOW_SYSTEM
5107 value = XCAR (XCDR (spec));
5108 if (!SYMBOLP (value)
5109 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5110 /* If we return here, POSITION has been advanced
5111 across the text with this property. */
5112 {
5113 if (it && it->bidi_p)
5114 {
5115 it->position = *position;
5116 iterate_out_of_display_property (it);
5117 *position = it->position;
5118 }
5119 return 1;
5120 }
5121
5122 if (it)
5123 {
5124 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
5125
5126 if (CONSP (XCDR (XCDR (spec))))
5127 {
5128 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5129 int face_id2 = lookup_derived_face (it->f, face_name,
5130 FRINGE_FACE_ID, 0);
5131 if (face_id2 >= 0)
5132 face_id = face_id2;
5133 }
5134
5135 /* Save current settings of IT so that we can restore them
5136 when we are finished with the glyph property value. */
5137 push_it (it, position);
5138
5139 it->area = TEXT_AREA;
5140 it->what = IT_IMAGE;
5141 it->image_id = -1; /* no image */
5142 it->position = start_pos;
5143 it->object = NILP (object) ? it->w->contents : object;
5144 it->method = GET_FROM_IMAGE;
5145 it->from_overlay = Qnil;
5146 it->face_id = face_id;
5147 it->from_disp_prop_p = true;
5148
5149 /* Say that we haven't consumed the characters with
5150 `display' property yet. The call to pop_it in
5151 set_iterator_to_next will clean this up. */
5152 *position = start_pos;
5153
5154 if (EQ (XCAR (spec), Qleft_fringe))
5155 {
5156 it->left_user_fringe_bitmap = fringe_bitmap;
5157 it->left_user_fringe_face_id = face_id;
5158 }
5159 else
5160 {
5161 it->right_user_fringe_bitmap = fringe_bitmap;
5162 it->right_user_fringe_face_id = face_id;
5163 }
5164 }
5165 #endif /* HAVE_WINDOW_SYSTEM */
5166 return 1;
5167 }
5168
5169 /* Prepare to handle `((margin left-margin) ...)',
5170 `((margin right-margin) ...)' and `((margin nil) ...)'
5171 prefixes for display specifications. */
5172 location = Qunbound;
5173 if (CONSP (spec) && CONSP (XCAR (spec)))
5174 {
5175 Lisp_Object tem;
5176
5177 value = XCDR (spec);
5178 if (CONSP (value))
5179 value = XCAR (value);
5180
5181 tem = XCAR (spec);
5182 if (EQ (XCAR (tem), Qmargin)
5183 && (tem = XCDR (tem),
5184 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5185 (NILP (tem)
5186 || EQ (tem, Qleft_margin)
5187 || EQ (tem, Qright_margin))))
5188 location = tem;
5189 }
5190
5191 if (EQ (location, Qunbound))
5192 {
5193 location = Qnil;
5194 value = spec;
5195 }
5196
5197 /* After this point, VALUE is the property after any
5198 margin prefix has been stripped. It must be a string,
5199 an image specification, or `(space ...)'.
5200
5201 LOCATION specifies where to display: `left-margin',
5202 `right-margin' or nil. */
5203
5204 valid_p = (STRINGP (value)
5205 #ifdef HAVE_WINDOW_SYSTEM
5206 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5207 && valid_image_p (value))
5208 #endif /* not HAVE_WINDOW_SYSTEM */
5209 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5210
5211 if (valid_p && !display_replaced_p)
5212 {
5213 int retval = 1;
5214
5215 if (!it)
5216 {
5217 /* Callers need to know whether the display spec is any kind
5218 of `(space ...)' spec that is about to affect text-area
5219 display. */
5220 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5221 retval = 2;
5222 return retval;
5223 }
5224
5225 /* Save current settings of IT so that we can restore them
5226 when we are finished with the glyph property value. */
5227 push_it (it, position);
5228 it->from_overlay = overlay;
5229 it->from_disp_prop_p = true;
5230
5231 if (NILP (location))
5232 it->area = TEXT_AREA;
5233 else if (EQ (location, Qleft_margin))
5234 it->area = LEFT_MARGIN_AREA;
5235 else
5236 it->area = RIGHT_MARGIN_AREA;
5237
5238 if (STRINGP (value))
5239 {
5240 it->string = value;
5241 it->multibyte_p = STRING_MULTIBYTE (it->string);
5242 it->current.overlay_string_index = -1;
5243 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5244 it->end_charpos = it->string_nchars = SCHARS (it->string);
5245 it->method = GET_FROM_STRING;
5246 it->stop_charpos = 0;
5247 it->prev_stop = 0;
5248 it->base_level_stop = 0;
5249 it->string_from_display_prop_p = true;
5250 /* Say that we haven't consumed the characters with
5251 `display' property yet. The call to pop_it in
5252 set_iterator_to_next will clean this up. */
5253 if (BUFFERP (object))
5254 *position = start_pos;
5255
5256 /* Force paragraph direction to be that of the parent
5257 object. If the parent object's paragraph direction is
5258 not yet determined, default to L2R. */
5259 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5260 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5261 else
5262 it->paragraph_embedding = L2R;
5263
5264 /* Set up the bidi iterator for this display string. */
5265 if (it->bidi_p)
5266 {
5267 it->bidi_it.string.lstring = it->string;
5268 it->bidi_it.string.s = NULL;
5269 it->bidi_it.string.schars = it->end_charpos;
5270 it->bidi_it.string.bufpos = bufpos;
5271 it->bidi_it.string.from_disp_str = 1;
5272 it->bidi_it.string.unibyte = !it->multibyte_p;
5273 it->bidi_it.w = it->w;
5274 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5275 }
5276 }
5277 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5278 {
5279 it->method = GET_FROM_STRETCH;
5280 it->object = value;
5281 *position = it->position = start_pos;
5282 retval = 1 + (it->area == TEXT_AREA);
5283 }
5284 #ifdef HAVE_WINDOW_SYSTEM
5285 else
5286 {
5287 it->what = IT_IMAGE;
5288 it->image_id = lookup_image (it->f, value);
5289 it->position = start_pos;
5290 it->object = NILP (object) ? it->w->contents : object;
5291 it->method = GET_FROM_IMAGE;
5292
5293 /* Say that we haven't consumed the characters with
5294 `display' property yet. The call to pop_it in
5295 set_iterator_to_next will clean this up. */
5296 *position = start_pos;
5297 }
5298 #endif /* HAVE_WINDOW_SYSTEM */
5299
5300 return retval;
5301 }
5302
5303 /* Invalid property or property not supported. Restore
5304 POSITION to what it was before. */
5305 *position = start_pos;
5306 return 0;
5307 }
5308
5309 /* Check if PROP is a display property value whose text should be
5310 treated as intangible. OVERLAY is the overlay from which PROP
5311 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5312 specify the buffer position covered by PROP. */
5313
5314 int
5315 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5316 ptrdiff_t charpos, ptrdiff_t bytepos)
5317 {
5318 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5319 struct text_pos position;
5320
5321 SET_TEXT_POS (position, charpos, bytepos);
5322 return handle_display_spec (NULL, prop, Qnil, overlay,
5323 &position, charpos, frame_window_p);
5324 }
5325
5326
5327 /* Return 1 if PROP is a display sub-property value containing STRING.
5328
5329 Implementation note: this and the following function are really
5330 special cases of handle_display_spec and
5331 handle_single_display_spec, and should ideally use the same code.
5332 Until they do, these two pairs must be consistent and must be
5333 modified in sync. */
5334
5335 static int
5336 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5337 {
5338 if (EQ (string, prop))
5339 return 1;
5340
5341 /* Skip over `when FORM'. */
5342 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5343 {
5344 prop = XCDR (prop);
5345 if (!CONSP (prop))
5346 return 0;
5347 /* Actually, the condition following `when' should be eval'ed,
5348 like handle_single_display_spec does, and we should return
5349 zero if it evaluates to nil. However, this function is
5350 called only when the buffer was already displayed and some
5351 glyph in the glyph matrix was found to come from a display
5352 string. Therefore, the condition was already evaluated, and
5353 the result was non-nil, otherwise the display string wouldn't
5354 have been displayed and we would have never been called for
5355 this property. Thus, we can skip the evaluation and assume
5356 its result is non-nil. */
5357 prop = XCDR (prop);
5358 }
5359
5360 if (CONSP (prop))
5361 /* Skip over `margin LOCATION'. */
5362 if (EQ (XCAR (prop), Qmargin))
5363 {
5364 prop = XCDR (prop);
5365 if (!CONSP (prop))
5366 return 0;
5367
5368 prop = XCDR (prop);
5369 if (!CONSP (prop))
5370 return 0;
5371 }
5372
5373 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5374 }
5375
5376
5377 /* Return 1 if STRING appears in the `display' property PROP. */
5378
5379 static int
5380 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5381 {
5382 if (CONSP (prop)
5383 && !EQ (XCAR (prop), Qwhen)
5384 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5385 {
5386 /* A list of sub-properties. */
5387 while (CONSP (prop))
5388 {
5389 if (single_display_spec_string_p (XCAR (prop), string))
5390 return 1;
5391 prop = XCDR (prop);
5392 }
5393 }
5394 else if (VECTORP (prop))
5395 {
5396 /* A vector of sub-properties. */
5397 ptrdiff_t i;
5398 for (i = 0; i < ASIZE (prop); ++i)
5399 if (single_display_spec_string_p (AREF (prop, i), string))
5400 return 1;
5401 }
5402 else
5403 return single_display_spec_string_p (prop, string);
5404
5405 return 0;
5406 }
5407
5408 /* Look for STRING in overlays and text properties in the current
5409 buffer, between character positions FROM and TO (excluding TO).
5410 BACK_P non-zero means look back (in this case, TO is supposed to be
5411 less than FROM).
5412 Value is the first character position where STRING was found, or
5413 zero if it wasn't found before hitting TO.
5414
5415 This function may only use code that doesn't eval because it is
5416 called asynchronously from note_mouse_highlight. */
5417
5418 static ptrdiff_t
5419 string_buffer_position_lim (Lisp_Object string,
5420 ptrdiff_t from, ptrdiff_t to, int back_p)
5421 {
5422 Lisp_Object limit, prop, pos;
5423 int found = 0;
5424
5425 pos = make_number (max (from, BEGV));
5426
5427 if (!back_p) /* looking forward */
5428 {
5429 limit = make_number (min (to, ZV));
5430 while (!found && !EQ (pos, limit))
5431 {
5432 prop = Fget_char_property (pos, Qdisplay, Qnil);
5433 if (!NILP (prop) && display_prop_string_p (prop, string))
5434 found = 1;
5435 else
5436 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5437 limit);
5438 }
5439 }
5440 else /* looking back */
5441 {
5442 limit = make_number (max (to, BEGV));
5443 while (!found && !EQ (pos, limit))
5444 {
5445 prop = Fget_char_property (pos, Qdisplay, Qnil);
5446 if (!NILP (prop) && display_prop_string_p (prop, string))
5447 found = 1;
5448 else
5449 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5450 limit);
5451 }
5452 }
5453
5454 return found ? XINT (pos) : 0;
5455 }
5456
5457 /* Determine which buffer position in current buffer STRING comes from.
5458 AROUND_CHARPOS is an approximate position where it could come from.
5459 Value is the buffer position or 0 if it couldn't be determined.
5460
5461 This function is necessary because we don't record buffer positions
5462 in glyphs generated from strings (to keep struct glyph small).
5463 This function may only use code that doesn't eval because it is
5464 called asynchronously from note_mouse_highlight. */
5465
5466 static ptrdiff_t
5467 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5468 {
5469 const int MAX_DISTANCE = 1000;
5470 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5471 around_charpos + MAX_DISTANCE,
5472 0);
5473
5474 if (!found)
5475 found = string_buffer_position_lim (string, around_charpos,
5476 around_charpos - MAX_DISTANCE, 1);
5477 return found;
5478 }
5479
5480
5481 \f
5482 /***********************************************************************
5483 `composition' property
5484 ***********************************************************************/
5485
5486 /* Set up iterator IT from `composition' property at its current
5487 position. Called from handle_stop. */
5488
5489 static enum prop_handled
5490 handle_composition_prop (struct it *it)
5491 {
5492 Lisp_Object prop, string;
5493 ptrdiff_t pos, pos_byte, start, end;
5494
5495 if (STRINGP (it->string))
5496 {
5497 unsigned char *s;
5498
5499 pos = IT_STRING_CHARPOS (*it);
5500 pos_byte = IT_STRING_BYTEPOS (*it);
5501 string = it->string;
5502 s = SDATA (string) + pos_byte;
5503 it->c = STRING_CHAR (s);
5504 }
5505 else
5506 {
5507 pos = IT_CHARPOS (*it);
5508 pos_byte = IT_BYTEPOS (*it);
5509 string = Qnil;
5510 it->c = FETCH_CHAR (pos_byte);
5511 }
5512
5513 /* If there's a valid composition and point is not inside of the
5514 composition (in the case that the composition is from the current
5515 buffer), draw a glyph composed from the composition components. */
5516 if (find_composition (pos, -1, &start, &end, &prop, string)
5517 && composition_valid_p (start, end, prop)
5518 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5519 {
5520 if (start < pos)
5521 /* As we can't handle this situation (perhaps font-lock added
5522 a new composition), we just return here hoping that next
5523 redisplay will detect this composition much earlier. */
5524 return HANDLED_NORMALLY;
5525 if (start != pos)
5526 {
5527 if (STRINGP (it->string))
5528 pos_byte = string_char_to_byte (it->string, start);
5529 else
5530 pos_byte = CHAR_TO_BYTE (start);
5531 }
5532 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5533 prop, string);
5534
5535 if (it->cmp_it.id >= 0)
5536 {
5537 it->cmp_it.ch = -1;
5538 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5539 it->cmp_it.nglyphs = -1;
5540 }
5541 }
5542
5543 return HANDLED_NORMALLY;
5544 }
5545
5546
5547 \f
5548 /***********************************************************************
5549 Overlay strings
5550 ***********************************************************************/
5551
5552 /* The following structure is used to record overlay strings for
5553 later sorting in load_overlay_strings. */
5554
5555 struct overlay_entry
5556 {
5557 Lisp_Object overlay;
5558 Lisp_Object string;
5559 EMACS_INT priority;
5560 int after_string_p;
5561 };
5562
5563
5564 /* Set up iterator IT from overlay strings at its current position.
5565 Called from handle_stop. */
5566
5567 static enum prop_handled
5568 handle_overlay_change (struct it *it)
5569 {
5570 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5571 return HANDLED_RECOMPUTE_PROPS;
5572 else
5573 return HANDLED_NORMALLY;
5574 }
5575
5576
5577 /* Set up the next overlay string for delivery by IT, if there is an
5578 overlay string to deliver. Called by set_iterator_to_next when the
5579 end of the current overlay string is reached. If there are more
5580 overlay strings to display, IT->string and
5581 IT->current.overlay_string_index are set appropriately here.
5582 Otherwise IT->string is set to nil. */
5583
5584 static void
5585 next_overlay_string (struct it *it)
5586 {
5587 ++it->current.overlay_string_index;
5588 if (it->current.overlay_string_index == it->n_overlay_strings)
5589 {
5590 /* No more overlay strings. Restore IT's settings to what
5591 they were before overlay strings were processed, and
5592 continue to deliver from current_buffer. */
5593
5594 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5595 pop_it (it);
5596 eassert (it->sp > 0
5597 || (NILP (it->string)
5598 && it->method == GET_FROM_BUFFER
5599 && it->stop_charpos >= BEGV
5600 && it->stop_charpos <= it->end_charpos));
5601 it->current.overlay_string_index = -1;
5602 it->n_overlay_strings = 0;
5603 it->overlay_strings_charpos = -1;
5604 /* If there's an empty display string on the stack, pop the
5605 stack, to resync the bidi iterator with IT's position. Such
5606 empty strings are pushed onto the stack in
5607 get_overlay_strings_1. */
5608 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5609 pop_it (it);
5610
5611 /* If we're at the end of the buffer, record that we have
5612 processed the overlay strings there already, so that
5613 next_element_from_buffer doesn't try it again. */
5614 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5615 it->overlay_strings_at_end_processed_p = true;
5616 }
5617 else
5618 {
5619 /* There are more overlay strings to process. If
5620 IT->current.overlay_string_index has advanced to a position
5621 where we must load IT->overlay_strings with more strings, do
5622 it. We must load at the IT->overlay_strings_charpos where
5623 IT->n_overlay_strings was originally computed; when invisible
5624 text is present, this might not be IT_CHARPOS (Bug#7016). */
5625 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5626
5627 if (it->current.overlay_string_index && i == 0)
5628 load_overlay_strings (it, it->overlay_strings_charpos);
5629
5630 /* Initialize IT to deliver display elements from the overlay
5631 string. */
5632 it->string = it->overlay_strings[i];
5633 it->multibyte_p = STRING_MULTIBYTE (it->string);
5634 SET_TEXT_POS (it->current.string_pos, 0, 0);
5635 it->method = GET_FROM_STRING;
5636 it->stop_charpos = 0;
5637 it->end_charpos = SCHARS (it->string);
5638 if (it->cmp_it.stop_pos >= 0)
5639 it->cmp_it.stop_pos = 0;
5640 it->prev_stop = 0;
5641 it->base_level_stop = 0;
5642
5643 /* Set up the bidi iterator for this overlay string. */
5644 if (it->bidi_p)
5645 {
5646 it->bidi_it.string.lstring = it->string;
5647 it->bidi_it.string.s = NULL;
5648 it->bidi_it.string.schars = SCHARS (it->string);
5649 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5650 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5651 it->bidi_it.string.unibyte = !it->multibyte_p;
5652 it->bidi_it.w = it->w;
5653 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5654 }
5655 }
5656
5657 CHECK_IT (it);
5658 }
5659
5660
5661 /* Compare two overlay_entry structures E1 and E2. Used as a
5662 comparison function for qsort in load_overlay_strings. Overlay
5663 strings for the same position are sorted so that
5664
5665 1. All after-strings come in front of before-strings, except
5666 when they come from the same overlay.
5667
5668 2. Within after-strings, strings are sorted so that overlay strings
5669 from overlays with higher priorities come first.
5670
5671 2. Within before-strings, strings are sorted so that overlay
5672 strings from overlays with higher priorities come last.
5673
5674 Value is analogous to strcmp. */
5675
5676
5677 static int
5678 compare_overlay_entries (const void *e1, const void *e2)
5679 {
5680 struct overlay_entry const *entry1 = e1;
5681 struct overlay_entry const *entry2 = e2;
5682 int result;
5683
5684 if (entry1->after_string_p != entry2->after_string_p)
5685 {
5686 /* Let after-strings appear in front of before-strings if
5687 they come from different overlays. */
5688 if (EQ (entry1->overlay, entry2->overlay))
5689 result = entry1->after_string_p ? 1 : -1;
5690 else
5691 result = entry1->after_string_p ? -1 : 1;
5692 }
5693 else if (entry1->priority != entry2->priority)
5694 {
5695 if (entry1->after_string_p)
5696 /* After-strings sorted in order of decreasing priority. */
5697 result = entry2->priority < entry1->priority ? -1 : 1;
5698 else
5699 /* Before-strings sorted in order of increasing priority. */
5700 result = entry1->priority < entry2->priority ? -1 : 1;
5701 }
5702 else
5703 result = 0;
5704
5705 return result;
5706 }
5707
5708
5709 /* Load the vector IT->overlay_strings with overlay strings from IT's
5710 current buffer position, or from CHARPOS if that is > 0. Set
5711 IT->n_overlays to the total number of overlay strings found.
5712
5713 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5714 a time. On entry into load_overlay_strings,
5715 IT->current.overlay_string_index gives the number of overlay
5716 strings that have already been loaded by previous calls to this
5717 function.
5718
5719 IT->add_overlay_start contains an additional overlay start
5720 position to consider for taking overlay strings from, if non-zero.
5721 This position comes into play when the overlay has an `invisible'
5722 property, and both before and after-strings. When we've skipped to
5723 the end of the overlay, because of its `invisible' property, we
5724 nevertheless want its before-string to appear.
5725 IT->add_overlay_start will contain the overlay start position
5726 in this case.
5727
5728 Overlay strings are sorted so that after-string strings come in
5729 front of before-string strings. Within before and after-strings,
5730 strings are sorted by overlay priority. See also function
5731 compare_overlay_entries. */
5732
5733 static void
5734 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5735 {
5736 Lisp_Object overlay, window, str, invisible;
5737 struct Lisp_Overlay *ov;
5738 ptrdiff_t start, end;
5739 ptrdiff_t size = 20;
5740 ptrdiff_t n = 0, i, j;
5741 int invis_p;
5742 struct overlay_entry *entries = alloca (size * sizeof *entries);
5743 USE_SAFE_ALLOCA;
5744
5745 if (charpos <= 0)
5746 charpos = IT_CHARPOS (*it);
5747
5748 /* Append the overlay string STRING of overlay OVERLAY to vector
5749 `entries' which has size `size' and currently contains `n'
5750 elements. AFTER_P non-zero means STRING is an after-string of
5751 OVERLAY. */
5752 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5753 do \
5754 { \
5755 Lisp_Object priority; \
5756 \
5757 if (n == size) \
5758 { \
5759 struct overlay_entry *old = entries; \
5760 SAFE_NALLOCA (entries, 2, size); \
5761 memcpy (entries, old, size * sizeof *entries); \
5762 size *= 2; \
5763 } \
5764 \
5765 entries[n].string = (STRING); \
5766 entries[n].overlay = (OVERLAY); \
5767 priority = Foverlay_get ((OVERLAY), Qpriority); \
5768 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5769 entries[n].after_string_p = (AFTER_P); \
5770 ++n; \
5771 } \
5772 while (0)
5773
5774 /* Process overlay before the overlay center. */
5775 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5776 {
5777 XSETMISC (overlay, ov);
5778 eassert (OVERLAYP (overlay));
5779 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5780 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5781
5782 if (end < charpos)
5783 break;
5784
5785 /* Skip this overlay if it doesn't start or end at IT's current
5786 position. */
5787 if (end != charpos && start != charpos)
5788 continue;
5789
5790 /* Skip this overlay if it doesn't apply to IT->w. */
5791 window = Foverlay_get (overlay, Qwindow);
5792 if (WINDOWP (window) && XWINDOW (window) != it->w)
5793 continue;
5794
5795 /* If the text ``under'' the overlay is invisible, both before-
5796 and after-strings from this overlay are visible; start and
5797 end position are indistinguishable. */
5798 invisible = Foverlay_get (overlay, Qinvisible);
5799 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5800
5801 /* If overlay has a non-empty before-string, record it. */
5802 if ((start == charpos || (end == charpos && invis_p))
5803 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5804 && SCHARS (str))
5805 RECORD_OVERLAY_STRING (overlay, str, 0);
5806
5807 /* If overlay has a non-empty after-string, record it. */
5808 if ((end == charpos || (start == charpos && invis_p))
5809 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5810 && SCHARS (str))
5811 RECORD_OVERLAY_STRING (overlay, str, 1);
5812 }
5813
5814 /* Process overlays after the overlay center. */
5815 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5816 {
5817 XSETMISC (overlay, ov);
5818 eassert (OVERLAYP (overlay));
5819 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5820 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5821
5822 if (start > charpos)
5823 break;
5824
5825 /* Skip this overlay if it doesn't start or end at IT's current
5826 position. */
5827 if (end != charpos && start != charpos)
5828 continue;
5829
5830 /* Skip this overlay if it doesn't apply to IT->w. */
5831 window = Foverlay_get (overlay, Qwindow);
5832 if (WINDOWP (window) && XWINDOW (window) != it->w)
5833 continue;
5834
5835 /* If the text ``under'' the overlay is invisible, it has a zero
5836 dimension, and both before- and after-strings apply. */
5837 invisible = Foverlay_get (overlay, Qinvisible);
5838 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5839
5840 /* If overlay has a non-empty before-string, record it. */
5841 if ((start == charpos || (end == charpos && invis_p))
5842 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5843 && SCHARS (str))
5844 RECORD_OVERLAY_STRING (overlay, str, 0);
5845
5846 /* If overlay has a non-empty after-string, record it. */
5847 if ((end == charpos || (start == charpos && invis_p))
5848 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5849 && SCHARS (str))
5850 RECORD_OVERLAY_STRING (overlay, str, 1);
5851 }
5852
5853 #undef RECORD_OVERLAY_STRING
5854
5855 /* Sort entries. */
5856 if (n > 1)
5857 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5858
5859 /* Record number of overlay strings, and where we computed it. */
5860 it->n_overlay_strings = n;
5861 it->overlay_strings_charpos = charpos;
5862
5863 /* IT->current.overlay_string_index is the number of overlay strings
5864 that have already been consumed by IT. Copy some of the
5865 remaining overlay strings to IT->overlay_strings. */
5866 i = 0;
5867 j = it->current.overlay_string_index;
5868 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5869 {
5870 it->overlay_strings[i] = entries[j].string;
5871 it->string_overlays[i++] = entries[j++].overlay;
5872 }
5873
5874 CHECK_IT (it);
5875 SAFE_FREE ();
5876 }
5877
5878
5879 /* Get the first chunk of overlay strings at IT's current buffer
5880 position, or at CHARPOS if that is > 0. Value is non-zero if at
5881 least one overlay string was found. */
5882
5883 static int
5884 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5885 {
5886 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5887 process. This fills IT->overlay_strings with strings, and sets
5888 IT->n_overlay_strings to the total number of strings to process.
5889 IT->pos.overlay_string_index has to be set temporarily to zero
5890 because load_overlay_strings needs this; it must be set to -1
5891 when no overlay strings are found because a zero value would
5892 indicate a position in the first overlay string. */
5893 it->current.overlay_string_index = 0;
5894 load_overlay_strings (it, charpos);
5895
5896 /* If we found overlay strings, set up IT to deliver display
5897 elements from the first one. Otherwise set up IT to deliver
5898 from current_buffer. */
5899 if (it->n_overlay_strings)
5900 {
5901 /* Make sure we know settings in current_buffer, so that we can
5902 restore meaningful values when we're done with the overlay
5903 strings. */
5904 if (compute_stop_p)
5905 compute_stop_pos (it);
5906 eassert (it->face_id >= 0);
5907
5908 /* Save IT's settings. They are restored after all overlay
5909 strings have been processed. */
5910 eassert (!compute_stop_p || it->sp == 0);
5911
5912 /* When called from handle_stop, there might be an empty display
5913 string loaded. In that case, don't bother saving it. But
5914 don't use this optimization with the bidi iterator, since we
5915 need the corresponding pop_it call to resync the bidi
5916 iterator's position with IT's position, after we are done
5917 with the overlay strings. (The corresponding call to pop_it
5918 in case of an empty display string is in
5919 next_overlay_string.) */
5920 if (!(!it->bidi_p
5921 && STRINGP (it->string) && !SCHARS (it->string)))
5922 push_it (it, NULL);
5923
5924 /* Set up IT to deliver display elements from the first overlay
5925 string. */
5926 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5927 it->string = it->overlay_strings[0];
5928 it->from_overlay = Qnil;
5929 it->stop_charpos = 0;
5930 eassert (STRINGP (it->string));
5931 it->end_charpos = SCHARS (it->string);
5932 it->prev_stop = 0;
5933 it->base_level_stop = 0;
5934 it->multibyte_p = STRING_MULTIBYTE (it->string);
5935 it->method = GET_FROM_STRING;
5936 it->from_disp_prop_p = 0;
5937
5938 /* Force paragraph direction to be that of the parent
5939 buffer. */
5940 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5941 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5942 else
5943 it->paragraph_embedding = L2R;
5944
5945 /* Set up the bidi iterator for this overlay string. */
5946 if (it->bidi_p)
5947 {
5948 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5949
5950 it->bidi_it.string.lstring = it->string;
5951 it->bidi_it.string.s = NULL;
5952 it->bidi_it.string.schars = SCHARS (it->string);
5953 it->bidi_it.string.bufpos = pos;
5954 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5955 it->bidi_it.string.unibyte = !it->multibyte_p;
5956 it->bidi_it.w = it->w;
5957 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5958 }
5959 return 1;
5960 }
5961
5962 it->current.overlay_string_index = -1;
5963 return 0;
5964 }
5965
5966 static int
5967 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5968 {
5969 it->string = Qnil;
5970 it->method = GET_FROM_BUFFER;
5971
5972 (void) get_overlay_strings_1 (it, charpos, 1);
5973
5974 CHECK_IT (it);
5975
5976 /* Value is non-zero if we found at least one overlay string. */
5977 return STRINGP (it->string);
5978 }
5979
5980
5981 \f
5982 /***********************************************************************
5983 Saving and restoring state
5984 ***********************************************************************/
5985
5986 /* Save current settings of IT on IT->stack. Called, for example,
5987 before setting up IT for an overlay string, to be able to restore
5988 IT's settings to what they were after the overlay string has been
5989 processed. If POSITION is non-NULL, it is the position to save on
5990 the stack instead of IT->position. */
5991
5992 static void
5993 push_it (struct it *it, struct text_pos *position)
5994 {
5995 struct iterator_stack_entry *p;
5996
5997 eassert (it->sp < IT_STACK_SIZE);
5998 p = it->stack + it->sp;
5999
6000 p->stop_charpos = it->stop_charpos;
6001 p->prev_stop = it->prev_stop;
6002 p->base_level_stop = it->base_level_stop;
6003 p->cmp_it = it->cmp_it;
6004 eassert (it->face_id >= 0);
6005 p->face_id = it->face_id;
6006 p->string = it->string;
6007 p->method = it->method;
6008 p->from_overlay = it->from_overlay;
6009 switch (p->method)
6010 {
6011 case GET_FROM_IMAGE:
6012 p->u.image.object = it->object;
6013 p->u.image.image_id = it->image_id;
6014 p->u.image.slice = it->slice;
6015 break;
6016 case GET_FROM_STRETCH:
6017 p->u.stretch.object = it->object;
6018 break;
6019 }
6020 p->position = position ? *position : it->position;
6021 p->current = it->current;
6022 p->end_charpos = it->end_charpos;
6023 p->string_nchars = it->string_nchars;
6024 p->area = it->area;
6025 p->multibyte_p = it->multibyte_p;
6026 p->avoid_cursor_p = it->avoid_cursor_p;
6027 p->space_width = it->space_width;
6028 p->font_height = it->font_height;
6029 p->voffset = it->voffset;
6030 p->string_from_display_prop_p = it->string_from_display_prop_p;
6031 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6032 p->display_ellipsis_p = 0;
6033 p->line_wrap = it->line_wrap;
6034 p->bidi_p = it->bidi_p;
6035 p->paragraph_embedding = it->paragraph_embedding;
6036 p->from_disp_prop_p = it->from_disp_prop_p;
6037 ++it->sp;
6038
6039 /* Save the state of the bidi iterator as well. */
6040 if (it->bidi_p)
6041 bidi_push_it (&it->bidi_it);
6042 }
6043
6044 static void
6045 iterate_out_of_display_property (struct it *it)
6046 {
6047 int buffer_p = !STRINGP (it->string);
6048 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6049 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6050
6051 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6052
6053 /* Maybe initialize paragraph direction. If we are at the beginning
6054 of a new paragraph, next_element_from_buffer may not have a
6055 chance to do that. */
6056 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6057 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6058 /* prev_stop can be zero, so check against BEGV as well. */
6059 while (it->bidi_it.charpos >= bob
6060 && it->prev_stop <= it->bidi_it.charpos
6061 && it->bidi_it.charpos < CHARPOS (it->position)
6062 && it->bidi_it.charpos < eob)
6063 bidi_move_to_visually_next (&it->bidi_it);
6064 /* Record the stop_pos we just crossed, for when we cross it
6065 back, maybe. */
6066 if (it->bidi_it.charpos > CHARPOS (it->position))
6067 it->prev_stop = CHARPOS (it->position);
6068 /* If we ended up not where pop_it put us, resync IT's
6069 positional members with the bidi iterator. */
6070 if (it->bidi_it.charpos != CHARPOS (it->position))
6071 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6072 if (buffer_p)
6073 it->current.pos = it->position;
6074 else
6075 it->current.string_pos = it->position;
6076 }
6077
6078 /* Restore IT's settings from IT->stack. Called, for example, when no
6079 more overlay strings must be processed, and we return to delivering
6080 display elements from a buffer, or when the end of a string from a
6081 `display' property is reached and we return to delivering display
6082 elements from an overlay string, or from a buffer. */
6083
6084 static void
6085 pop_it (struct it *it)
6086 {
6087 struct iterator_stack_entry *p;
6088 int from_display_prop = it->from_disp_prop_p;
6089
6090 eassert (it->sp > 0);
6091 --it->sp;
6092 p = it->stack + it->sp;
6093 it->stop_charpos = p->stop_charpos;
6094 it->prev_stop = p->prev_stop;
6095 it->base_level_stop = p->base_level_stop;
6096 it->cmp_it = p->cmp_it;
6097 it->face_id = p->face_id;
6098 it->current = p->current;
6099 it->position = p->position;
6100 it->string = p->string;
6101 it->from_overlay = p->from_overlay;
6102 if (NILP (it->string))
6103 SET_TEXT_POS (it->current.string_pos, -1, -1);
6104 it->method = p->method;
6105 switch (it->method)
6106 {
6107 case GET_FROM_IMAGE:
6108 it->image_id = p->u.image.image_id;
6109 it->object = p->u.image.object;
6110 it->slice = p->u.image.slice;
6111 break;
6112 case GET_FROM_STRETCH:
6113 it->object = p->u.stretch.object;
6114 break;
6115 case GET_FROM_BUFFER:
6116 it->object = it->w->contents;
6117 break;
6118 case GET_FROM_STRING:
6119 {
6120 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6121
6122 /* Restore the face_box_p flag, since it could have been
6123 overwritten by the face of the object that we just finished
6124 displaying. */
6125 if (face)
6126 it->face_box_p = face->box != FACE_NO_BOX;
6127 it->object = it->string;
6128 }
6129 break;
6130 case GET_FROM_DISPLAY_VECTOR:
6131 if (it->s)
6132 it->method = GET_FROM_C_STRING;
6133 else if (STRINGP (it->string))
6134 it->method = GET_FROM_STRING;
6135 else
6136 {
6137 it->method = GET_FROM_BUFFER;
6138 it->object = it->w->contents;
6139 }
6140 }
6141 it->end_charpos = p->end_charpos;
6142 it->string_nchars = p->string_nchars;
6143 it->area = p->area;
6144 it->multibyte_p = p->multibyte_p;
6145 it->avoid_cursor_p = p->avoid_cursor_p;
6146 it->space_width = p->space_width;
6147 it->font_height = p->font_height;
6148 it->voffset = p->voffset;
6149 it->string_from_display_prop_p = p->string_from_display_prop_p;
6150 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6151 it->line_wrap = p->line_wrap;
6152 it->bidi_p = p->bidi_p;
6153 it->paragraph_embedding = p->paragraph_embedding;
6154 it->from_disp_prop_p = p->from_disp_prop_p;
6155 if (it->bidi_p)
6156 {
6157 bidi_pop_it (&it->bidi_it);
6158 /* Bidi-iterate until we get out of the portion of text, if any,
6159 covered by a `display' text property or by an overlay with
6160 `display' property. (We cannot just jump there, because the
6161 internal coherency of the bidi iterator state can not be
6162 preserved across such jumps.) We also must determine the
6163 paragraph base direction if the overlay we just processed is
6164 at the beginning of a new paragraph. */
6165 if (from_display_prop
6166 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6167 iterate_out_of_display_property (it);
6168
6169 eassert ((BUFFERP (it->object)
6170 && IT_CHARPOS (*it) == it->bidi_it.charpos
6171 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6172 || (STRINGP (it->object)
6173 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6174 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6175 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6176 }
6177 }
6178
6179
6180 \f
6181 /***********************************************************************
6182 Moving over lines
6183 ***********************************************************************/
6184
6185 /* Set IT's current position to the previous line start. */
6186
6187 static void
6188 back_to_previous_line_start (struct it *it)
6189 {
6190 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6191
6192 DEC_BOTH (cp, bp);
6193 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6194 }
6195
6196
6197 /* Move IT to the next line start.
6198
6199 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6200 we skipped over part of the text (as opposed to moving the iterator
6201 continuously over the text). Otherwise, don't change the value
6202 of *SKIPPED_P.
6203
6204 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6205 iterator on the newline, if it was found.
6206
6207 Newlines may come from buffer text, overlay strings, or strings
6208 displayed via the `display' property. That's the reason we can't
6209 simply use find_newline_no_quit.
6210
6211 Note that this function may not skip over invisible text that is so
6212 because of text properties and immediately follows a newline. If
6213 it would, function reseat_at_next_visible_line_start, when called
6214 from set_iterator_to_next, would effectively make invisible
6215 characters following a newline part of the wrong glyph row, which
6216 leads to wrong cursor motion. */
6217
6218 static int
6219 forward_to_next_line_start (struct it *it, int *skipped_p,
6220 struct bidi_it *bidi_it_prev)
6221 {
6222 ptrdiff_t old_selective;
6223 int newline_found_p, n;
6224 const int MAX_NEWLINE_DISTANCE = 500;
6225
6226 /* If already on a newline, just consume it to avoid unintended
6227 skipping over invisible text below. */
6228 if (it->what == IT_CHARACTER
6229 && it->c == '\n'
6230 && CHARPOS (it->position) == IT_CHARPOS (*it))
6231 {
6232 if (it->bidi_p && bidi_it_prev)
6233 *bidi_it_prev = it->bidi_it;
6234 set_iterator_to_next (it, 0);
6235 it->c = 0;
6236 return 1;
6237 }
6238
6239 /* Don't handle selective display in the following. It's (a)
6240 unnecessary because it's done by the caller, and (b) leads to an
6241 infinite recursion because next_element_from_ellipsis indirectly
6242 calls this function. */
6243 old_selective = it->selective;
6244 it->selective = 0;
6245
6246 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6247 from buffer text. */
6248 for (n = newline_found_p = 0;
6249 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6250 n += STRINGP (it->string) ? 0 : 1)
6251 {
6252 if (!get_next_display_element (it))
6253 return 0;
6254 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6255 if (newline_found_p && it->bidi_p && bidi_it_prev)
6256 *bidi_it_prev = it->bidi_it;
6257 set_iterator_to_next (it, 0);
6258 }
6259
6260 /* If we didn't find a newline near enough, see if we can use a
6261 short-cut. */
6262 if (!newline_found_p)
6263 {
6264 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6265 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6266 1, &bytepos);
6267 Lisp_Object pos;
6268
6269 eassert (!STRINGP (it->string));
6270
6271 /* If there isn't any `display' property in sight, and no
6272 overlays, we can just use the position of the newline in
6273 buffer text. */
6274 if (it->stop_charpos >= limit
6275 || ((pos = Fnext_single_property_change (make_number (start),
6276 Qdisplay, Qnil,
6277 make_number (limit)),
6278 NILP (pos))
6279 && next_overlay_change (start) == ZV))
6280 {
6281 if (!it->bidi_p)
6282 {
6283 IT_CHARPOS (*it) = limit;
6284 IT_BYTEPOS (*it) = bytepos;
6285 }
6286 else
6287 {
6288 struct bidi_it bprev;
6289
6290 /* Help bidi.c avoid expensive searches for display
6291 properties and overlays, by telling it that there are
6292 none up to `limit'. */
6293 if (it->bidi_it.disp_pos < limit)
6294 {
6295 it->bidi_it.disp_pos = limit;
6296 it->bidi_it.disp_prop = 0;
6297 }
6298 do {
6299 bprev = it->bidi_it;
6300 bidi_move_to_visually_next (&it->bidi_it);
6301 } while (it->bidi_it.charpos != limit);
6302 IT_CHARPOS (*it) = limit;
6303 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6304 if (bidi_it_prev)
6305 *bidi_it_prev = bprev;
6306 }
6307 *skipped_p = newline_found_p = true;
6308 }
6309 else
6310 {
6311 while (get_next_display_element (it)
6312 && !newline_found_p)
6313 {
6314 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6315 if (newline_found_p && it->bidi_p && bidi_it_prev)
6316 *bidi_it_prev = it->bidi_it;
6317 set_iterator_to_next (it, 0);
6318 }
6319 }
6320 }
6321
6322 it->selective = old_selective;
6323 return newline_found_p;
6324 }
6325
6326
6327 /* Set IT's current position to the previous visible line start. Skip
6328 invisible text that is so either due to text properties or due to
6329 selective display. Caution: this does not change IT->current_x and
6330 IT->hpos. */
6331
6332 static void
6333 back_to_previous_visible_line_start (struct it *it)
6334 {
6335 while (IT_CHARPOS (*it) > BEGV)
6336 {
6337 back_to_previous_line_start (it);
6338
6339 if (IT_CHARPOS (*it) <= BEGV)
6340 break;
6341
6342 /* If selective > 0, then lines indented more than its value are
6343 invisible. */
6344 if (it->selective > 0
6345 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6346 it->selective))
6347 continue;
6348
6349 /* Check the newline before point for invisibility. */
6350 {
6351 Lisp_Object prop;
6352 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6353 Qinvisible, it->window);
6354 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6355 continue;
6356 }
6357
6358 if (IT_CHARPOS (*it) <= BEGV)
6359 break;
6360
6361 {
6362 struct it it2;
6363 void *it2data = NULL;
6364 ptrdiff_t pos;
6365 ptrdiff_t beg, end;
6366 Lisp_Object val, overlay;
6367
6368 SAVE_IT (it2, *it, it2data);
6369
6370 /* If newline is part of a composition, continue from start of composition */
6371 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6372 && beg < IT_CHARPOS (*it))
6373 goto replaced;
6374
6375 /* If newline is replaced by a display property, find start of overlay
6376 or interval and continue search from that point. */
6377 pos = --IT_CHARPOS (it2);
6378 --IT_BYTEPOS (it2);
6379 it2.sp = 0;
6380 bidi_unshelve_cache (NULL, 0);
6381 it2.string_from_display_prop_p = 0;
6382 it2.from_disp_prop_p = 0;
6383 if (handle_display_prop (&it2) == HANDLED_RETURN
6384 && !NILP (val = get_char_property_and_overlay
6385 (make_number (pos), Qdisplay, Qnil, &overlay))
6386 && (OVERLAYP (overlay)
6387 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6388 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6389 {
6390 RESTORE_IT (it, it, it2data);
6391 goto replaced;
6392 }
6393
6394 /* Newline is not replaced by anything -- so we are done. */
6395 RESTORE_IT (it, it, it2data);
6396 break;
6397
6398 replaced:
6399 if (beg < BEGV)
6400 beg = BEGV;
6401 IT_CHARPOS (*it) = beg;
6402 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6403 }
6404 }
6405
6406 it->continuation_lines_width = 0;
6407
6408 eassert (IT_CHARPOS (*it) >= BEGV);
6409 eassert (IT_CHARPOS (*it) == BEGV
6410 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6411 CHECK_IT (it);
6412 }
6413
6414
6415 /* Reseat iterator IT at the previous visible line start. Skip
6416 invisible text that is so either due to text properties or due to
6417 selective display. At the end, update IT's overlay information,
6418 face information etc. */
6419
6420 void
6421 reseat_at_previous_visible_line_start (struct it *it)
6422 {
6423 back_to_previous_visible_line_start (it);
6424 reseat (it, it->current.pos, 1);
6425 CHECK_IT (it);
6426 }
6427
6428
6429 /* Reseat iterator IT on the next visible line start in the current
6430 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6431 preceding the line start. Skip over invisible text that is so
6432 because of selective display. Compute faces, overlays etc at the
6433 new position. Note that this function does not skip over text that
6434 is invisible because of text properties. */
6435
6436 static void
6437 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6438 {
6439 int newline_found_p, skipped_p = 0;
6440 struct bidi_it bidi_it_prev;
6441
6442 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6443
6444 /* Skip over lines that are invisible because they are indented
6445 more than the value of IT->selective. */
6446 if (it->selective > 0)
6447 while (IT_CHARPOS (*it) < ZV
6448 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6449 it->selective))
6450 {
6451 eassert (IT_BYTEPOS (*it) == BEGV
6452 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6453 newline_found_p =
6454 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6455 }
6456
6457 /* Position on the newline if that's what's requested. */
6458 if (on_newline_p && newline_found_p)
6459 {
6460 if (STRINGP (it->string))
6461 {
6462 if (IT_STRING_CHARPOS (*it) > 0)
6463 {
6464 if (!it->bidi_p)
6465 {
6466 --IT_STRING_CHARPOS (*it);
6467 --IT_STRING_BYTEPOS (*it);
6468 }
6469 else
6470 {
6471 /* We need to restore the bidi iterator to the state
6472 it had on the newline, and resync the IT's
6473 position with that. */
6474 it->bidi_it = bidi_it_prev;
6475 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6476 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6477 }
6478 }
6479 }
6480 else if (IT_CHARPOS (*it) > BEGV)
6481 {
6482 if (!it->bidi_p)
6483 {
6484 --IT_CHARPOS (*it);
6485 --IT_BYTEPOS (*it);
6486 }
6487 else
6488 {
6489 /* We need to restore the bidi iterator to the state it
6490 had on the newline and resync IT with that. */
6491 it->bidi_it = bidi_it_prev;
6492 IT_CHARPOS (*it) = it->bidi_it.charpos;
6493 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6494 }
6495 reseat (it, it->current.pos, 0);
6496 }
6497 }
6498 else if (skipped_p)
6499 reseat (it, it->current.pos, 0);
6500
6501 CHECK_IT (it);
6502 }
6503
6504
6505 \f
6506 /***********************************************************************
6507 Changing an iterator's position
6508 ***********************************************************************/
6509
6510 /* Change IT's current position to POS in current_buffer. If FORCE_P
6511 is non-zero, always check for text properties at the new position.
6512 Otherwise, text properties are only looked up if POS >=
6513 IT->check_charpos of a property. */
6514
6515 static void
6516 reseat (struct it *it, struct text_pos pos, int force_p)
6517 {
6518 ptrdiff_t original_pos = IT_CHARPOS (*it);
6519
6520 reseat_1 (it, pos, 0);
6521
6522 /* Determine where to check text properties. Avoid doing it
6523 where possible because text property lookup is very expensive. */
6524 if (force_p
6525 || CHARPOS (pos) > it->stop_charpos
6526 || CHARPOS (pos) < original_pos)
6527 {
6528 if (it->bidi_p)
6529 {
6530 /* For bidi iteration, we need to prime prev_stop and
6531 base_level_stop with our best estimations. */
6532 /* Implementation note: Of course, POS is not necessarily a
6533 stop position, so assigning prev_pos to it is a lie; we
6534 should have called compute_stop_backwards. However, if
6535 the current buffer does not include any R2L characters,
6536 that call would be a waste of cycles, because the
6537 iterator will never move back, and thus never cross this
6538 "fake" stop position. So we delay that backward search
6539 until the time we really need it, in next_element_from_buffer. */
6540 if (CHARPOS (pos) != it->prev_stop)
6541 it->prev_stop = CHARPOS (pos);
6542 if (CHARPOS (pos) < it->base_level_stop)
6543 it->base_level_stop = 0; /* meaning it's unknown */
6544 handle_stop (it);
6545 }
6546 else
6547 {
6548 handle_stop (it);
6549 it->prev_stop = it->base_level_stop = 0;
6550 }
6551
6552 }
6553
6554 CHECK_IT (it);
6555 }
6556
6557
6558 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6559 IT->stop_pos to POS, also. */
6560
6561 static void
6562 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6563 {
6564 /* Don't call this function when scanning a C string. */
6565 eassert (it->s == NULL);
6566
6567 /* POS must be a reasonable value. */
6568 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6569
6570 it->current.pos = it->position = pos;
6571 it->end_charpos = ZV;
6572 it->dpvec = NULL;
6573 it->current.dpvec_index = -1;
6574 it->current.overlay_string_index = -1;
6575 IT_STRING_CHARPOS (*it) = -1;
6576 IT_STRING_BYTEPOS (*it) = -1;
6577 it->string = Qnil;
6578 it->method = GET_FROM_BUFFER;
6579 it->object = it->w->contents;
6580 it->area = TEXT_AREA;
6581 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6582 it->sp = 0;
6583 it->string_from_display_prop_p = 0;
6584 it->string_from_prefix_prop_p = 0;
6585
6586 it->from_disp_prop_p = 0;
6587 it->face_before_selective_p = 0;
6588 if (it->bidi_p)
6589 {
6590 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6591 &it->bidi_it);
6592 bidi_unshelve_cache (NULL, 0);
6593 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6594 it->bidi_it.string.s = NULL;
6595 it->bidi_it.string.lstring = Qnil;
6596 it->bidi_it.string.bufpos = 0;
6597 it->bidi_it.string.from_disp_str = 0;
6598 it->bidi_it.string.unibyte = 0;
6599 it->bidi_it.w = it->w;
6600 }
6601
6602 if (set_stop_p)
6603 {
6604 it->stop_charpos = CHARPOS (pos);
6605 it->base_level_stop = CHARPOS (pos);
6606 }
6607 /* This make the information stored in it->cmp_it invalidate. */
6608 it->cmp_it.id = -1;
6609 }
6610
6611
6612 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6613 If S is non-null, it is a C string to iterate over. Otherwise,
6614 STRING gives a Lisp string to iterate over.
6615
6616 If PRECISION > 0, don't return more then PRECISION number of
6617 characters from the string.
6618
6619 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6620 characters have been returned. FIELD_WIDTH < 0 means an infinite
6621 field width.
6622
6623 MULTIBYTE = 0 means disable processing of multibyte characters,
6624 MULTIBYTE > 0 means enable it,
6625 MULTIBYTE < 0 means use IT->multibyte_p.
6626
6627 IT must be initialized via a prior call to init_iterator before
6628 calling this function. */
6629
6630 static void
6631 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6632 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6633 int multibyte)
6634 {
6635 /* No text property checks performed by default, but see below. */
6636 it->stop_charpos = -1;
6637
6638 /* Set iterator position and end position. */
6639 memset (&it->current, 0, sizeof it->current);
6640 it->current.overlay_string_index = -1;
6641 it->current.dpvec_index = -1;
6642 eassert (charpos >= 0);
6643
6644 /* If STRING is specified, use its multibyteness, otherwise use the
6645 setting of MULTIBYTE, if specified. */
6646 if (multibyte >= 0)
6647 it->multibyte_p = multibyte > 0;
6648
6649 /* Bidirectional reordering of strings is controlled by the default
6650 value of bidi-display-reordering. Don't try to reorder while
6651 loading loadup.el, as the necessary character property tables are
6652 not yet available. */
6653 it->bidi_p =
6654 NILP (Vpurify_flag)
6655 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6656
6657 if (s == NULL)
6658 {
6659 eassert (STRINGP (string));
6660 it->string = string;
6661 it->s = NULL;
6662 it->end_charpos = it->string_nchars = SCHARS (string);
6663 it->method = GET_FROM_STRING;
6664 it->current.string_pos = string_pos (charpos, string);
6665
6666 if (it->bidi_p)
6667 {
6668 it->bidi_it.string.lstring = string;
6669 it->bidi_it.string.s = NULL;
6670 it->bidi_it.string.schars = it->end_charpos;
6671 it->bidi_it.string.bufpos = 0;
6672 it->bidi_it.string.from_disp_str = 0;
6673 it->bidi_it.string.unibyte = !it->multibyte_p;
6674 it->bidi_it.w = it->w;
6675 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6676 FRAME_WINDOW_P (it->f), &it->bidi_it);
6677 }
6678 }
6679 else
6680 {
6681 it->s = (const unsigned char *) s;
6682 it->string = Qnil;
6683
6684 /* Note that we use IT->current.pos, not it->current.string_pos,
6685 for displaying C strings. */
6686 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6687 if (it->multibyte_p)
6688 {
6689 it->current.pos = c_string_pos (charpos, s, 1);
6690 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6691 }
6692 else
6693 {
6694 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6695 it->end_charpos = it->string_nchars = strlen (s);
6696 }
6697
6698 if (it->bidi_p)
6699 {
6700 it->bidi_it.string.lstring = Qnil;
6701 it->bidi_it.string.s = (const unsigned char *) s;
6702 it->bidi_it.string.schars = it->end_charpos;
6703 it->bidi_it.string.bufpos = 0;
6704 it->bidi_it.string.from_disp_str = 0;
6705 it->bidi_it.string.unibyte = !it->multibyte_p;
6706 it->bidi_it.w = it->w;
6707 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6708 &it->bidi_it);
6709 }
6710 it->method = GET_FROM_C_STRING;
6711 }
6712
6713 /* PRECISION > 0 means don't return more than PRECISION characters
6714 from the string. */
6715 if (precision > 0 && it->end_charpos - charpos > precision)
6716 {
6717 it->end_charpos = it->string_nchars = charpos + precision;
6718 if (it->bidi_p)
6719 it->bidi_it.string.schars = it->end_charpos;
6720 }
6721
6722 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6723 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6724 FIELD_WIDTH < 0 means infinite field width. This is useful for
6725 padding with `-' at the end of a mode line. */
6726 if (field_width < 0)
6727 field_width = INFINITY;
6728 /* Implementation note: We deliberately don't enlarge
6729 it->bidi_it.string.schars here to fit it->end_charpos, because
6730 the bidi iterator cannot produce characters out of thin air. */
6731 if (field_width > it->end_charpos - charpos)
6732 it->end_charpos = charpos + field_width;
6733
6734 /* Use the standard display table for displaying strings. */
6735 if (DISP_TABLE_P (Vstandard_display_table))
6736 it->dp = XCHAR_TABLE (Vstandard_display_table);
6737
6738 it->stop_charpos = charpos;
6739 it->prev_stop = charpos;
6740 it->base_level_stop = 0;
6741 if (it->bidi_p)
6742 {
6743 it->bidi_it.first_elt = 1;
6744 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6745 it->bidi_it.disp_pos = -1;
6746 }
6747 if (s == NULL && it->multibyte_p)
6748 {
6749 ptrdiff_t endpos = SCHARS (it->string);
6750 if (endpos > it->end_charpos)
6751 endpos = it->end_charpos;
6752 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6753 it->string);
6754 }
6755 CHECK_IT (it);
6756 }
6757
6758
6759 \f
6760 /***********************************************************************
6761 Iteration
6762 ***********************************************************************/
6763
6764 /* Map enum it_method value to corresponding next_element_from_* function. */
6765
6766 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6767 {
6768 next_element_from_buffer,
6769 next_element_from_display_vector,
6770 next_element_from_string,
6771 next_element_from_c_string,
6772 next_element_from_image,
6773 next_element_from_stretch
6774 };
6775
6776 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6777
6778
6779 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6780 (possibly with the following characters). */
6781
6782 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6783 ((IT)->cmp_it.id >= 0 \
6784 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6785 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6786 END_CHARPOS, (IT)->w, \
6787 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6788 (IT)->string)))
6789
6790
6791 /* Lookup the char-table Vglyphless_char_display for character C (-1
6792 if we want information for no-font case), and return the display
6793 method symbol. By side-effect, update it->what and
6794 it->glyphless_method. This function is called from
6795 get_next_display_element for each character element, and from
6796 x_produce_glyphs when no suitable font was found. */
6797
6798 Lisp_Object
6799 lookup_glyphless_char_display (int c, struct it *it)
6800 {
6801 Lisp_Object glyphless_method = Qnil;
6802
6803 if (CHAR_TABLE_P (Vglyphless_char_display)
6804 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6805 {
6806 if (c >= 0)
6807 {
6808 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6809 if (CONSP (glyphless_method))
6810 glyphless_method = FRAME_WINDOW_P (it->f)
6811 ? XCAR (glyphless_method)
6812 : XCDR (glyphless_method);
6813 }
6814 else
6815 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6816 }
6817
6818 retry:
6819 if (NILP (glyphless_method))
6820 {
6821 if (c >= 0)
6822 /* The default is to display the character by a proper font. */
6823 return Qnil;
6824 /* The default for the no-font case is to display an empty box. */
6825 glyphless_method = Qempty_box;
6826 }
6827 if (EQ (glyphless_method, Qzero_width))
6828 {
6829 if (c >= 0)
6830 return glyphless_method;
6831 /* This method can't be used for the no-font case. */
6832 glyphless_method = Qempty_box;
6833 }
6834 if (EQ (glyphless_method, Qthin_space))
6835 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6836 else if (EQ (glyphless_method, Qempty_box))
6837 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6838 else if (EQ (glyphless_method, Qhex_code))
6839 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6840 else if (STRINGP (glyphless_method))
6841 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6842 else
6843 {
6844 /* Invalid value. We use the default method. */
6845 glyphless_method = Qnil;
6846 goto retry;
6847 }
6848 it->what = IT_GLYPHLESS;
6849 return glyphless_method;
6850 }
6851
6852 /* Merge escape glyph face and cache the result. */
6853
6854 static struct frame *last_escape_glyph_frame = NULL;
6855 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6856 static int last_escape_glyph_merged_face_id = 0;
6857
6858 static int
6859 merge_escape_glyph_face (struct it *it)
6860 {
6861 int face_id;
6862
6863 if (it->f == last_escape_glyph_frame
6864 && it->face_id == last_escape_glyph_face_id)
6865 face_id = last_escape_glyph_merged_face_id;
6866 else
6867 {
6868 /* Merge the `escape-glyph' face into the current face. */
6869 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6870 last_escape_glyph_frame = it->f;
6871 last_escape_glyph_face_id = it->face_id;
6872 last_escape_glyph_merged_face_id = face_id;
6873 }
6874 return face_id;
6875 }
6876
6877 /* Likewise for glyphless glyph face. */
6878
6879 static struct frame *last_glyphless_glyph_frame = NULL;
6880 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6881 static int last_glyphless_glyph_merged_face_id = 0;
6882
6883 int
6884 merge_glyphless_glyph_face (struct it *it)
6885 {
6886 int face_id;
6887
6888 if (it->f == last_glyphless_glyph_frame
6889 && it->face_id == last_glyphless_glyph_face_id)
6890 face_id = last_glyphless_glyph_merged_face_id;
6891 else
6892 {
6893 /* Merge the `glyphless-char' face into the current face. */
6894 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6895 last_glyphless_glyph_frame = it->f;
6896 last_glyphless_glyph_face_id = it->face_id;
6897 last_glyphless_glyph_merged_face_id = face_id;
6898 }
6899 return face_id;
6900 }
6901
6902 /* Load IT's display element fields with information about the next
6903 display element from the current position of IT. Value is zero if
6904 end of buffer (or C string) is reached. */
6905
6906 static int
6907 get_next_display_element (struct it *it)
6908 {
6909 /* Non-zero means that we found a display element. Zero means that
6910 we hit the end of what we iterate over. Performance note: the
6911 function pointer `method' used here turns out to be faster than
6912 using a sequence of if-statements. */
6913 int success_p;
6914
6915 get_next:
6916 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6917
6918 if (it->what == IT_CHARACTER)
6919 {
6920 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6921 and only if (a) the resolved directionality of that character
6922 is R..." */
6923 /* FIXME: Do we need an exception for characters from display
6924 tables? */
6925 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6926 it->c = bidi_mirror_char (it->c);
6927 /* Map via display table or translate control characters.
6928 IT->c, IT->len etc. have been set to the next character by
6929 the function call above. If we have a display table, and it
6930 contains an entry for IT->c, translate it. Don't do this if
6931 IT->c itself comes from a display table, otherwise we could
6932 end up in an infinite recursion. (An alternative could be to
6933 count the recursion depth of this function and signal an
6934 error when a certain maximum depth is reached.) Is it worth
6935 it? */
6936 if (success_p && it->dpvec == NULL)
6937 {
6938 Lisp_Object dv;
6939 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6940 int nonascii_space_p = 0;
6941 int nonascii_hyphen_p = 0;
6942 int c = it->c; /* This is the character to display. */
6943
6944 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6945 {
6946 eassert (SINGLE_BYTE_CHAR_P (c));
6947 if (unibyte_display_via_language_environment)
6948 {
6949 c = DECODE_CHAR (unibyte, c);
6950 if (c < 0)
6951 c = BYTE8_TO_CHAR (it->c);
6952 }
6953 else
6954 c = BYTE8_TO_CHAR (it->c);
6955 }
6956
6957 if (it->dp
6958 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6959 VECTORP (dv)))
6960 {
6961 struct Lisp_Vector *v = XVECTOR (dv);
6962
6963 /* Return the first character from the display table
6964 entry, if not empty. If empty, don't display the
6965 current character. */
6966 if (v->header.size)
6967 {
6968 it->dpvec_char_len = it->len;
6969 it->dpvec = v->contents;
6970 it->dpend = v->contents + v->header.size;
6971 it->current.dpvec_index = 0;
6972 it->dpvec_face_id = -1;
6973 it->saved_face_id = it->face_id;
6974 it->method = GET_FROM_DISPLAY_VECTOR;
6975 it->ellipsis_p = 0;
6976 }
6977 else
6978 {
6979 set_iterator_to_next (it, 0);
6980 }
6981 goto get_next;
6982 }
6983
6984 if (! NILP (lookup_glyphless_char_display (c, it)))
6985 {
6986 if (it->what == IT_GLYPHLESS)
6987 goto done;
6988 /* Don't display this character. */
6989 set_iterator_to_next (it, 0);
6990 goto get_next;
6991 }
6992
6993 /* If `nobreak-char-display' is non-nil, we display
6994 non-ASCII spaces and hyphens specially. */
6995 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6996 {
6997 if (c == 0xA0)
6998 nonascii_space_p = true;
6999 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
7000 nonascii_hyphen_p = true;
7001 }
7002
7003 /* Translate control characters into `\003' or `^C' form.
7004 Control characters coming from a display table entry are
7005 currently not translated because we use IT->dpvec to hold
7006 the translation. This could easily be changed but I
7007 don't believe that it is worth doing.
7008
7009 The characters handled by `nobreak-char-display' must be
7010 translated too.
7011
7012 Non-printable characters and raw-byte characters are also
7013 translated to octal form. */
7014 if (((c < ' ' || c == 127) /* ASCII control chars. */
7015 ? (it->area != TEXT_AREA
7016 /* In mode line, treat \n, \t like other crl chars. */
7017 || (c != '\t'
7018 && it->glyph_row
7019 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7020 || (c != '\n' && c != '\t'))
7021 : (nonascii_space_p
7022 || nonascii_hyphen_p
7023 || CHAR_BYTE8_P (c)
7024 || ! CHAR_PRINTABLE_P (c))))
7025 {
7026 /* C is a control character, non-ASCII space/hyphen,
7027 raw-byte, or a non-printable character which must be
7028 displayed either as '\003' or as `^C' where the '\\'
7029 and '^' can be defined in the display table. Fill
7030 IT->ctl_chars with glyphs for what we have to
7031 display. Then, set IT->dpvec to these glyphs. */
7032 Lisp_Object gc;
7033 int ctl_len;
7034 int face_id;
7035 int lface_id = 0;
7036 int escape_glyph;
7037
7038 /* Handle control characters with ^. */
7039
7040 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7041 {
7042 int g;
7043
7044 g = '^'; /* default glyph for Control */
7045 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7046 if (it->dp
7047 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7048 {
7049 g = GLYPH_CODE_CHAR (gc);
7050 lface_id = GLYPH_CODE_FACE (gc);
7051 }
7052
7053 face_id = (lface_id
7054 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7055 : merge_escape_glyph_face (it));
7056
7057 XSETINT (it->ctl_chars[0], g);
7058 XSETINT (it->ctl_chars[1], c ^ 0100);
7059 ctl_len = 2;
7060 goto display_control;
7061 }
7062
7063 /* Handle non-ascii space in the mode where it only gets
7064 highlighting. */
7065
7066 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7067 {
7068 /* Merge `nobreak-space' into the current face. */
7069 face_id = merge_faces (it->f, Qnobreak_space, 0,
7070 it->face_id);
7071 XSETINT (it->ctl_chars[0], ' ');
7072 ctl_len = 1;
7073 goto display_control;
7074 }
7075
7076 /* Handle sequences that start with the "escape glyph". */
7077
7078 /* the default escape glyph is \. */
7079 escape_glyph = '\\';
7080
7081 if (it->dp
7082 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7083 {
7084 escape_glyph = GLYPH_CODE_CHAR (gc);
7085 lface_id = GLYPH_CODE_FACE (gc);
7086 }
7087
7088 face_id = (lface_id
7089 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7090 : merge_escape_glyph_face (it));
7091
7092 /* Draw non-ASCII hyphen with just highlighting: */
7093
7094 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7095 {
7096 XSETINT (it->ctl_chars[0], '-');
7097 ctl_len = 1;
7098 goto display_control;
7099 }
7100
7101 /* Draw non-ASCII space/hyphen with escape glyph: */
7102
7103 if (nonascii_space_p || nonascii_hyphen_p)
7104 {
7105 XSETINT (it->ctl_chars[0], escape_glyph);
7106 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7107 ctl_len = 2;
7108 goto display_control;
7109 }
7110
7111 {
7112 char str[10];
7113 int len, i;
7114
7115 if (CHAR_BYTE8_P (c))
7116 /* Display \200 instead of \17777600. */
7117 c = CHAR_TO_BYTE8 (c);
7118 len = sprintf (str, "%03o", c);
7119
7120 XSETINT (it->ctl_chars[0], escape_glyph);
7121 for (i = 0; i < len; i++)
7122 XSETINT (it->ctl_chars[i + 1], str[i]);
7123 ctl_len = len + 1;
7124 }
7125
7126 display_control:
7127 /* Set up IT->dpvec and return first character from it. */
7128 it->dpvec_char_len = it->len;
7129 it->dpvec = it->ctl_chars;
7130 it->dpend = it->dpvec + ctl_len;
7131 it->current.dpvec_index = 0;
7132 it->dpvec_face_id = face_id;
7133 it->saved_face_id = it->face_id;
7134 it->method = GET_FROM_DISPLAY_VECTOR;
7135 it->ellipsis_p = 0;
7136 goto get_next;
7137 }
7138 it->char_to_display = c;
7139 }
7140 else if (success_p)
7141 {
7142 it->char_to_display = it->c;
7143 }
7144 }
7145
7146 #ifdef HAVE_WINDOW_SYSTEM
7147 /* Adjust face id for a multibyte character. There are no multibyte
7148 character in unibyte text. */
7149 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7150 && it->multibyte_p
7151 && success_p
7152 && FRAME_WINDOW_P (it->f))
7153 {
7154 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7155
7156 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7157 {
7158 /* Automatic composition with glyph-string. */
7159 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7160
7161 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7162 }
7163 else
7164 {
7165 ptrdiff_t pos = (it->s ? -1
7166 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7167 : IT_CHARPOS (*it));
7168 int c;
7169
7170 if (it->what == IT_CHARACTER)
7171 c = it->char_to_display;
7172 else
7173 {
7174 struct composition *cmp = composition_table[it->cmp_it.id];
7175 int i;
7176
7177 c = ' ';
7178 for (i = 0; i < cmp->glyph_len; i++)
7179 /* TAB in a composition means display glyphs with
7180 padding space on the left or right. */
7181 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7182 break;
7183 }
7184 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7185 }
7186 }
7187 #endif /* HAVE_WINDOW_SYSTEM */
7188
7189 done:
7190 /* Is this character the last one of a run of characters with
7191 box? If yes, set IT->end_of_box_run_p to 1. */
7192 if (it->face_box_p
7193 && it->s == NULL)
7194 {
7195 if (it->method == GET_FROM_STRING && it->sp)
7196 {
7197 int face_id = underlying_face_id (it);
7198 struct face *face = FACE_FROM_ID (it->f, face_id);
7199
7200 if (face)
7201 {
7202 if (face->box == FACE_NO_BOX)
7203 {
7204 /* If the box comes from face properties in a
7205 display string, check faces in that string. */
7206 int string_face_id = face_after_it_pos (it);
7207 it->end_of_box_run_p
7208 = (FACE_FROM_ID (it->f, string_face_id)->box
7209 == FACE_NO_BOX);
7210 }
7211 /* Otherwise, the box comes from the underlying face.
7212 If this is the last string character displayed, check
7213 the next buffer location. */
7214 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7215 /* n_overlay_strings is unreliable unless
7216 overlay_string_index is non-negative. */
7217 && ((it->current.overlay_string_index >= 0
7218 && (it->current.overlay_string_index
7219 == it->n_overlay_strings - 1))
7220 /* A string from display property. */
7221 || it->from_disp_prop_p))
7222 {
7223 ptrdiff_t ignore;
7224 int next_face_id;
7225 struct text_pos pos = it->current.pos;
7226
7227 /* For a string from a display property, the next
7228 buffer position is stored in the 'position'
7229 member of the iteration stack slot below the
7230 current one, see handle_single_display_spec. By
7231 contrast, it->current.pos was is not yet updated
7232 to point to that buffer position; that will
7233 happen in pop_it, after we finish displaying the
7234 current string. Note that we already checked
7235 above that it->sp is positive, so subtracting one
7236 from it is safe. */
7237 if (it->from_disp_prop_p)
7238 pos = (it->stack + it->sp - 1)->position;
7239 else
7240 INC_TEXT_POS (pos, it->multibyte_p);
7241
7242 if (CHARPOS (pos) >= ZV)
7243 it->end_of_box_run_p = true;
7244 else
7245 {
7246 next_face_id = face_at_buffer_position
7247 (it->w, CHARPOS (pos), &ignore,
7248 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7249 it->end_of_box_run_p
7250 = (FACE_FROM_ID (it->f, next_face_id)->box
7251 == FACE_NO_BOX);
7252 }
7253 }
7254 }
7255 }
7256 /* next_element_from_display_vector sets this flag according to
7257 faces of the display vector glyphs, see there. */
7258 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7259 {
7260 int face_id = face_after_it_pos (it);
7261 it->end_of_box_run_p
7262 = (face_id != it->face_id
7263 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7264 }
7265 }
7266 /* If we reached the end of the object we've been iterating (e.g., a
7267 display string or an overlay string), and there's something on
7268 IT->stack, proceed with what's on the stack. It doesn't make
7269 sense to return zero if there's unprocessed stuff on the stack,
7270 because otherwise that stuff will never be displayed. */
7271 if (!success_p && it->sp > 0)
7272 {
7273 set_iterator_to_next (it, 0);
7274 success_p = get_next_display_element (it);
7275 }
7276
7277 /* Value is 0 if end of buffer or string reached. */
7278 return success_p;
7279 }
7280
7281
7282 /* Move IT to the next display element.
7283
7284 RESEAT_P non-zero means if called on a newline in buffer text,
7285 skip to the next visible line start.
7286
7287 Functions get_next_display_element and set_iterator_to_next are
7288 separate because I find this arrangement easier to handle than a
7289 get_next_display_element function that also increments IT's
7290 position. The way it is we can first look at an iterator's current
7291 display element, decide whether it fits on a line, and if it does,
7292 increment the iterator position. The other way around we probably
7293 would either need a flag indicating whether the iterator has to be
7294 incremented the next time, or we would have to implement a
7295 decrement position function which would not be easy to write. */
7296
7297 void
7298 set_iterator_to_next (struct it *it, int reseat_p)
7299 {
7300 /* Reset flags indicating start and end of a sequence of characters
7301 with box. Reset them at the start of this function because
7302 moving the iterator to a new position might set them. */
7303 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7304
7305 switch (it->method)
7306 {
7307 case GET_FROM_BUFFER:
7308 /* The current display element of IT is a character from
7309 current_buffer. Advance in the buffer, and maybe skip over
7310 invisible lines that are so because of selective display. */
7311 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7312 reseat_at_next_visible_line_start (it, 0);
7313 else if (it->cmp_it.id >= 0)
7314 {
7315 /* We are currently getting glyphs from a composition. */
7316 int i;
7317
7318 if (! it->bidi_p)
7319 {
7320 IT_CHARPOS (*it) += it->cmp_it.nchars;
7321 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7322 if (it->cmp_it.to < it->cmp_it.nglyphs)
7323 {
7324 it->cmp_it.from = it->cmp_it.to;
7325 }
7326 else
7327 {
7328 it->cmp_it.id = -1;
7329 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7330 IT_BYTEPOS (*it),
7331 it->end_charpos, Qnil);
7332 }
7333 }
7334 else if (! it->cmp_it.reversed_p)
7335 {
7336 /* Composition created while scanning forward. */
7337 /* Update IT's char/byte positions to point to the first
7338 character of the next grapheme cluster, or to the
7339 character visually after the current composition. */
7340 for (i = 0; i < it->cmp_it.nchars; i++)
7341 bidi_move_to_visually_next (&it->bidi_it);
7342 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7343 IT_CHARPOS (*it) = it->bidi_it.charpos;
7344
7345 if (it->cmp_it.to < it->cmp_it.nglyphs)
7346 {
7347 /* Proceed to the next grapheme cluster. */
7348 it->cmp_it.from = it->cmp_it.to;
7349 }
7350 else
7351 {
7352 /* No more grapheme clusters in this composition.
7353 Find the next stop position. */
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 /* Now we are scanning backward and don't know
7357 where to stop. */
7358 stop = -1;
7359 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7360 IT_BYTEPOS (*it), stop, Qnil);
7361 }
7362 }
7363 else
7364 {
7365 /* Composition created while scanning backward. */
7366 /* Update IT's char/byte positions to point to the last
7367 character of the previous grapheme cluster, or the
7368 character visually after the current composition. */
7369 for (i = 0; i < it->cmp_it.nchars; i++)
7370 bidi_move_to_visually_next (&it->bidi_it);
7371 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7372 IT_CHARPOS (*it) = it->bidi_it.charpos;
7373 if (it->cmp_it.from > 0)
7374 {
7375 /* Proceed to the previous grapheme cluster. */
7376 it->cmp_it.to = it->cmp_it.from;
7377 }
7378 else
7379 {
7380 /* No more grapheme clusters in this composition.
7381 Find the next stop position. */
7382 ptrdiff_t stop = it->end_charpos;
7383 if (it->bidi_it.scan_dir < 0)
7384 /* Now we are scanning backward and don't know
7385 where to stop. */
7386 stop = -1;
7387 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7388 IT_BYTEPOS (*it), stop, Qnil);
7389 }
7390 }
7391 }
7392 else
7393 {
7394 eassert (it->len != 0);
7395
7396 if (!it->bidi_p)
7397 {
7398 IT_BYTEPOS (*it) += it->len;
7399 IT_CHARPOS (*it) += 1;
7400 }
7401 else
7402 {
7403 int prev_scan_dir = it->bidi_it.scan_dir;
7404 /* If this is a new paragraph, determine its base
7405 direction (a.k.a. its base embedding level). */
7406 if (it->bidi_it.new_paragraph)
7407 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7408 bidi_move_to_visually_next (&it->bidi_it);
7409 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7410 IT_CHARPOS (*it) = it->bidi_it.charpos;
7411 if (prev_scan_dir != it->bidi_it.scan_dir)
7412 {
7413 /* As the scan direction was changed, we must
7414 re-compute the stop position for composition. */
7415 ptrdiff_t stop = it->end_charpos;
7416 if (it->bidi_it.scan_dir < 0)
7417 stop = -1;
7418 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7419 IT_BYTEPOS (*it), stop, Qnil);
7420 }
7421 }
7422 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7423 }
7424 break;
7425
7426 case GET_FROM_C_STRING:
7427 /* Current display element of IT is from a C string. */
7428 if (!it->bidi_p
7429 /* If the string position is beyond string's end, it means
7430 next_element_from_c_string is padding the string with
7431 blanks, in which case we bypass the bidi iterator,
7432 because it cannot deal with such virtual characters. */
7433 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7434 {
7435 IT_BYTEPOS (*it) += it->len;
7436 IT_CHARPOS (*it) += 1;
7437 }
7438 else
7439 {
7440 bidi_move_to_visually_next (&it->bidi_it);
7441 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7442 IT_CHARPOS (*it) = it->bidi_it.charpos;
7443 }
7444 break;
7445
7446 case GET_FROM_DISPLAY_VECTOR:
7447 /* Current display element of IT is from a display table entry.
7448 Advance in the display table definition. Reset it to null if
7449 end reached, and continue with characters from buffers/
7450 strings. */
7451 ++it->current.dpvec_index;
7452
7453 /* Restore face of the iterator to what they were before the
7454 display vector entry (these entries may contain faces). */
7455 it->face_id = it->saved_face_id;
7456
7457 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7458 {
7459 int recheck_faces = it->ellipsis_p;
7460
7461 if (it->s)
7462 it->method = GET_FROM_C_STRING;
7463 else if (STRINGP (it->string))
7464 it->method = GET_FROM_STRING;
7465 else
7466 {
7467 it->method = GET_FROM_BUFFER;
7468 it->object = it->w->contents;
7469 }
7470
7471 it->dpvec = NULL;
7472 it->current.dpvec_index = -1;
7473
7474 /* Skip over characters which were displayed via IT->dpvec. */
7475 if (it->dpvec_char_len < 0)
7476 reseat_at_next_visible_line_start (it, 1);
7477 else if (it->dpvec_char_len > 0)
7478 {
7479 if (it->method == GET_FROM_STRING
7480 && it->current.overlay_string_index >= 0
7481 && it->n_overlay_strings > 0)
7482 it->ignore_overlay_strings_at_pos_p = true;
7483 it->len = it->dpvec_char_len;
7484 set_iterator_to_next (it, reseat_p);
7485 }
7486
7487 /* Maybe recheck faces after display vector. */
7488 if (recheck_faces)
7489 it->stop_charpos = IT_CHARPOS (*it);
7490 }
7491 break;
7492
7493 case GET_FROM_STRING:
7494 /* Current display element is a character from a Lisp string. */
7495 eassert (it->s == NULL && STRINGP (it->string));
7496 /* Don't advance past string end. These conditions are true
7497 when set_iterator_to_next is called at the end of
7498 get_next_display_element, in which case the Lisp string is
7499 already exhausted, and all we want is pop the iterator
7500 stack. */
7501 if (it->current.overlay_string_index >= 0)
7502 {
7503 /* This is an overlay string, so there's no padding with
7504 spaces, and the number of characters in the string is
7505 where the string ends. */
7506 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7507 goto consider_string_end;
7508 }
7509 else
7510 {
7511 /* Not an overlay string. There could be padding, so test
7512 against it->end_charpos. */
7513 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7514 goto consider_string_end;
7515 }
7516 if (it->cmp_it.id >= 0)
7517 {
7518 int i;
7519
7520 if (! it->bidi_p)
7521 {
7522 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7523 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7524 if (it->cmp_it.to < it->cmp_it.nglyphs)
7525 it->cmp_it.from = it->cmp_it.to;
7526 else
7527 {
7528 it->cmp_it.id = -1;
7529 composition_compute_stop_pos (&it->cmp_it,
7530 IT_STRING_CHARPOS (*it),
7531 IT_STRING_BYTEPOS (*it),
7532 it->end_charpos, it->string);
7533 }
7534 }
7535 else if (! it->cmp_it.reversed_p)
7536 {
7537 for (i = 0; i < it->cmp_it.nchars; i++)
7538 bidi_move_to_visually_next (&it->bidi_it);
7539 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7540 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7541
7542 if (it->cmp_it.to < it->cmp_it.nglyphs)
7543 it->cmp_it.from = it->cmp_it.to;
7544 else
7545 {
7546 ptrdiff_t stop = it->end_charpos;
7547 if (it->bidi_it.scan_dir < 0)
7548 stop = -1;
7549 composition_compute_stop_pos (&it->cmp_it,
7550 IT_STRING_CHARPOS (*it),
7551 IT_STRING_BYTEPOS (*it), stop,
7552 it->string);
7553 }
7554 }
7555 else
7556 {
7557 for (i = 0; i < it->cmp_it.nchars; i++)
7558 bidi_move_to_visually_next (&it->bidi_it);
7559 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7560 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7561 if (it->cmp_it.from > 0)
7562 it->cmp_it.to = it->cmp_it.from;
7563 else
7564 {
7565 ptrdiff_t stop = it->end_charpos;
7566 if (it->bidi_it.scan_dir < 0)
7567 stop = -1;
7568 composition_compute_stop_pos (&it->cmp_it,
7569 IT_STRING_CHARPOS (*it),
7570 IT_STRING_BYTEPOS (*it), stop,
7571 it->string);
7572 }
7573 }
7574 }
7575 else
7576 {
7577 if (!it->bidi_p
7578 /* If the string position is beyond string's end, it
7579 means next_element_from_string is padding the string
7580 with blanks, in which case we bypass the bidi
7581 iterator, because it cannot deal with such virtual
7582 characters. */
7583 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7584 {
7585 IT_STRING_BYTEPOS (*it) += it->len;
7586 IT_STRING_CHARPOS (*it) += 1;
7587 }
7588 else
7589 {
7590 int prev_scan_dir = it->bidi_it.scan_dir;
7591
7592 bidi_move_to_visually_next (&it->bidi_it);
7593 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7594 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7595 if (prev_scan_dir != it->bidi_it.scan_dir)
7596 {
7597 ptrdiff_t stop = it->end_charpos;
7598
7599 if (it->bidi_it.scan_dir < 0)
7600 stop = -1;
7601 composition_compute_stop_pos (&it->cmp_it,
7602 IT_STRING_CHARPOS (*it),
7603 IT_STRING_BYTEPOS (*it), stop,
7604 it->string);
7605 }
7606 }
7607 }
7608
7609 consider_string_end:
7610
7611 if (it->current.overlay_string_index >= 0)
7612 {
7613 /* IT->string is an overlay string. Advance to the
7614 next, if there is one. */
7615 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7616 {
7617 it->ellipsis_p = 0;
7618 next_overlay_string (it);
7619 if (it->ellipsis_p)
7620 setup_for_ellipsis (it, 0);
7621 }
7622 }
7623 else
7624 {
7625 /* IT->string is not an overlay string. If we reached
7626 its end, and there is something on IT->stack, proceed
7627 with what is on the stack. This can be either another
7628 string, this time an overlay string, or a buffer. */
7629 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7630 && it->sp > 0)
7631 {
7632 pop_it (it);
7633 if (it->method == GET_FROM_STRING)
7634 goto consider_string_end;
7635 }
7636 }
7637 break;
7638
7639 case GET_FROM_IMAGE:
7640 case GET_FROM_STRETCH:
7641 /* The position etc with which we have to proceed are on
7642 the stack. The position may be at the end of a string,
7643 if the `display' property takes up the whole string. */
7644 eassert (it->sp > 0);
7645 pop_it (it);
7646 if (it->method == GET_FROM_STRING)
7647 goto consider_string_end;
7648 break;
7649
7650 default:
7651 /* There are no other methods defined, so this should be a bug. */
7652 emacs_abort ();
7653 }
7654
7655 eassert (it->method != GET_FROM_STRING
7656 || (STRINGP (it->string)
7657 && IT_STRING_CHARPOS (*it) >= 0));
7658 }
7659
7660 /* Load IT's display element fields with information about the next
7661 display element which comes from a display table entry or from the
7662 result of translating a control character to one of the forms `^C'
7663 or `\003'.
7664
7665 IT->dpvec holds the glyphs to return as characters.
7666 IT->saved_face_id holds the face id before the display vector--it
7667 is restored into IT->face_id in set_iterator_to_next. */
7668
7669 static int
7670 next_element_from_display_vector (struct it *it)
7671 {
7672 Lisp_Object gc;
7673 int prev_face_id = it->face_id;
7674 int next_face_id;
7675
7676 /* Precondition. */
7677 eassert (it->dpvec && it->current.dpvec_index >= 0);
7678
7679 it->face_id = it->saved_face_id;
7680
7681 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7682 That seemed totally bogus - so I changed it... */
7683 gc = it->dpvec[it->current.dpvec_index];
7684
7685 if (GLYPH_CODE_P (gc))
7686 {
7687 struct face *this_face, *prev_face, *next_face;
7688
7689 it->c = GLYPH_CODE_CHAR (gc);
7690 it->len = CHAR_BYTES (it->c);
7691
7692 /* The entry may contain a face id to use. Such a face id is
7693 the id of a Lisp face, not a realized face. A face id of
7694 zero means no face is specified. */
7695 if (it->dpvec_face_id >= 0)
7696 it->face_id = it->dpvec_face_id;
7697 else
7698 {
7699 int lface_id = GLYPH_CODE_FACE (gc);
7700 if (lface_id > 0)
7701 it->face_id = merge_faces (it->f, Qt, lface_id,
7702 it->saved_face_id);
7703 }
7704
7705 /* Glyphs in the display vector could have the box face, so we
7706 need to set the related flags in the iterator, as
7707 appropriate. */
7708 this_face = FACE_FROM_ID (it->f, it->face_id);
7709 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7710
7711 /* Is this character the first character of a box-face run? */
7712 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7713 && (!prev_face
7714 || prev_face->box == FACE_NO_BOX));
7715
7716 /* For the last character of the box-face run, we need to look
7717 either at the next glyph from the display vector, or at the
7718 face we saw before the display vector. */
7719 next_face_id = it->saved_face_id;
7720 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7721 {
7722 if (it->dpvec_face_id >= 0)
7723 next_face_id = it->dpvec_face_id;
7724 else
7725 {
7726 int lface_id =
7727 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7728
7729 if (lface_id > 0)
7730 next_face_id = merge_faces (it->f, Qt, lface_id,
7731 it->saved_face_id);
7732 }
7733 }
7734 next_face = FACE_FROM_ID (it->f, next_face_id);
7735 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7736 && (!next_face
7737 || next_face->box == FACE_NO_BOX));
7738 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7739 }
7740 else
7741 /* Display table entry is invalid. Return a space. */
7742 it->c = ' ', it->len = 1;
7743
7744 /* Don't change position and object of the iterator here. They are
7745 still the values of the character that had this display table
7746 entry or was translated, and that's what we want. */
7747 it->what = IT_CHARACTER;
7748 return 1;
7749 }
7750
7751 /* Get the first element of string/buffer in the visual order, after
7752 being reseated to a new position in a string or a buffer. */
7753 static void
7754 get_visually_first_element (struct it *it)
7755 {
7756 int string_p = STRINGP (it->string) || it->s;
7757 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7758 ptrdiff_t bob = (string_p ? 0 : BEGV);
7759
7760 if (STRINGP (it->string))
7761 {
7762 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7763 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7764 }
7765 else
7766 {
7767 it->bidi_it.charpos = IT_CHARPOS (*it);
7768 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7769 }
7770
7771 if (it->bidi_it.charpos == eob)
7772 {
7773 /* Nothing to do, but reset the FIRST_ELT flag, like
7774 bidi_paragraph_init does, because we are not going to
7775 call it. */
7776 it->bidi_it.first_elt = 0;
7777 }
7778 else if (it->bidi_it.charpos == bob
7779 || (!string_p
7780 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7781 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7782 {
7783 /* If we are at the beginning of a line/string, we can produce
7784 the next element right away. */
7785 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7786 bidi_move_to_visually_next (&it->bidi_it);
7787 }
7788 else
7789 {
7790 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7791
7792 /* We need to prime the bidi iterator starting at the line's or
7793 string's beginning, before we will be able to produce the
7794 next element. */
7795 if (string_p)
7796 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7797 else
7798 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7799 IT_BYTEPOS (*it), -1,
7800 &it->bidi_it.bytepos);
7801 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7802 do
7803 {
7804 /* Now return to buffer/string position where we were asked
7805 to get the next display element, and produce that. */
7806 bidi_move_to_visually_next (&it->bidi_it);
7807 }
7808 while (it->bidi_it.bytepos != orig_bytepos
7809 && it->bidi_it.charpos < eob);
7810 }
7811
7812 /* Adjust IT's position information to where we ended up. */
7813 if (STRINGP (it->string))
7814 {
7815 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7816 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7817 }
7818 else
7819 {
7820 IT_CHARPOS (*it) = it->bidi_it.charpos;
7821 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7822 }
7823
7824 if (STRINGP (it->string) || !it->s)
7825 {
7826 ptrdiff_t stop, charpos, bytepos;
7827
7828 if (STRINGP (it->string))
7829 {
7830 eassert (!it->s);
7831 stop = SCHARS (it->string);
7832 if (stop > it->end_charpos)
7833 stop = it->end_charpos;
7834 charpos = IT_STRING_CHARPOS (*it);
7835 bytepos = IT_STRING_BYTEPOS (*it);
7836 }
7837 else
7838 {
7839 stop = it->end_charpos;
7840 charpos = IT_CHARPOS (*it);
7841 bytepos = IT_BYTEPOS (*it);
7842 }
7843 if (it->bidi_it.scan_dir < 0)
7844 stop = -1;
7845 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7846 it->string);
7847 }
7848 }
7849
7850 /* Load IT with the next display element from Lisp string IT->string.
7851 IT->current.string_pos is the current position within the string.
7852 If IT->current.overlay_string_index >= 0, the Lisp string is an
7853 overlay string. */
7854
7855 static int
7856 next_element_from_string (struct it *it)
7857 {
7858 struct text_pos position;
7859
7860 eassert (STRINGP (it->string));
7861 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7862 eassert (IT_STRING_CHARPOS (*it) >= 0);
7863 position = it->current.string_pos;
7864
7865 /* With bidi reordering, the character to display might not be the
7866 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7867 that we were reseat()ed to a new string, whose paragraph
7868 direction is not known. */
7869 if (it->bidi_p && it->bidi_it.first_elt)
7870 {
7871 get_visually_first_element (it);
7872 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7873 }
7874
7875 /* Time to check for invisible text? */
7876 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7877 {
7878 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7879 {
7880 if (!(!it->bidi_p
7881 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7882 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7883 {
7884 /* With bidi non-linear iteration, we could find
7885 ourselves far beyond the last computed stop_charpos,
7886 with several other stop positions in between that we
7887 missed. Scan them all now, in buffer's logical
7888 order, until we find and handle the last stop_charpos
7889 that precedes our current position. */
7890 handle_stop_backwards (it, it->stop_charpos);
7891 return GET_NEXT_DISPLAY_ELEMENT (it);
7892 }
7893 else
7894 {
7895 if (it->bidi_p)
7896 {
7897 /* Take note of the stop position we just moved
7898 across, for when we will move back across it. */
7899 it->prev_stop = it->stop_charpos;
7900 /* If we are at base paragraph embedding level, take
7901 note of the last stop position seen at this
7902 level. */
7903 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7904 it->base_level_stop = it->stop_charpos;
7905 }
7906 handle_stop (it);
7907
7908 /* Since a handler may have changed IT->method, we must
7909 recurse here. */
7910 return GET_NEXT_DISPLAY_ELEMENT (it);
7911 }
7912 }
7913 else if (it->bidi_p
7914 /* If we are before prev_stop, we may have overstepped
7915 on our way backwards a stop_pos, and if so, we need
7916 to handle that stop_pos. */
7917 && IT_STRING_CHARPOS (*it) < it->prev_stop
7918 /* We can sometimes back up for reasons that have nothing
7919 to do with bidi reordering. E.g., compositions. The
7920 code below is only needed when we are above the base
7921 embedding level, so test for that explicitly. */
7922 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7923 {
7924 /* If we lost track of base_level_stop, we have no better
7925 place for handle_stop_backwards to start from than string
7926 beginning. This happens, e.g., when we were reseated to
7927 the previous screenful of text by vertical-motion. */
7928 if (it->base_level_stop <= 0
7929 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7930 it->base_level_stop = 0;
7931 handle_stop_backwards (it, it->base_level_stop);
7932 return GET_NEXT_DISPLAY_ELEMENT (it);
7933 }
7934 }
7935
7936 if (it->current.overlay_string_index >= 0)
7937 {
7938 /* Get the next character from an overlay string. In overlay
7939 strings, there is no field width or padding with spaces to
7940 do. */
7941 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7942 {
7943 it->what = IT_EOB;
7944 return 0;
7945 }
7946 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7947 IT_STRING_BYTEPOS (*it),
7948 it->bidi_it.scan_dir < 0
7949 ? -1
7950 : SCHARS (it->string))
7951 && next_element_from_composition (it))
7952 {
7953 return 1;
7954 }
7955 else if (STRING_MULTIBYTE (it->string))
7956 {
7957 const unsigned char *s = (SDATA (it->string)
7958 + IT_STRING_BYTEPOS (*it));
7959 it->c = string_char_and_length (s, &it->len);
7960 }
7961 else
7962 {
7963 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7964 it->len = 1;
7965 }
7966 }
7967 else
7968 {
7969 /* Get the next character from a Lisp string that is not an
7970 overlay string. Such strings come from the mode line, for
7971 example. We may have to pad with spaces, or truncate the
7972 string. See also next_element_from_c_string. */
7973 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7974 {
7975 it->what = IT_EOB;
7976 return 0;
7977 }
7978 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7979 {
7980 /* Pad with spaces. */
7981 it->c = ' ', it->len = 1;
7982 CHARPOS (position) = BYTEPOS (position) = -1;
7983 }
7984 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7985 IT_STRING_BYTEPOS (*it),
7986 it->bidi_it.scan_dir < 0
7987 ? -1
7988 : it->string_nchars)
7989 && next_element_from_composition (it))
7990 {
7991 return 1;
7992 }
7993 else if (STRING_MULTIBYTE (it->string))
7994 {
7995 const unsigned char *s = (SDATA (it->string)
7996 + IT_STRING_BYTEPOS (*it));
7997 it->c = string_char_and_length (s, &it->len);
7998 }
7999 else
8000 {
8001 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8002 it->len = 1;
8003 }
8004 }
8005
8006 /* Record what we have and where it came from. */
8007 it->what = IT_CHARACTER;
8008 it->object = it->string;
8009 it->position = position;
8010 return 1;
8011 }
8012
8013
8014 /* Load IT with next display element from C string IT->s.
8015 IT->string_nchars is the maximum number of characters to return
8016 from the string. IT->end_charpos may be greater than
8017 IT->string_nchars when this function is called, in which case we
8018 may have to return padding spaces. Value is zero if end of string
8019 reached, including padding spaces. */
8020
8021 static int
8022 next_element_from_c_string (struct it *it)
8023 {
8024 bool success_p = true;
8025
8026 eassert (it->s);
8027 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8028 it->what = IT_CHARACTER;
8029 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8030 it->object = Qnil;
8031
8032 /* With bidi reordering, the character to display might not be the
8033 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8034 we were reseated to a new string, whose paragraph direction is
8035 not known. */
8036 if (it->bidi_p && it->bidi_it.first_elt)
8037 get_visually_first_element (it);
8038
8039 /* IT's position can be greater than IT->string_nchars in case a
8040 field width or precision has been specified when the iterator was
8041 initialized. */
8042 if (IT_CHARPOS (*it) >= it->end_charpos)
8043 {
8044 /* End of the game. */
8045 it->what = IT_EOB;
8046 success_p = 0;
8047 }
8048 else if (IT_CHARPOS (*it) >= it->string_nchars)
8049 {
8050 /* Pad with spaces. */
8051 it->c = ' ', it->len = 1;
8052 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8053 }
8054 else if (it->multibyte_p)
8055 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8056 else
8057 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8058
8059 return success_p;
8060 }
8061
8062
8063 /* Set up IT to return characters from an ellipsis, if appropriate.
8064 The definition of the ellipsis glyphs may come from a display table
8065 entry. This function fills IT with the first glyph from the
8066 ellipsis if an ellipsis is to be displayed. */
8067
8068 static int
8069 next_element_from_ellipsis (struct it *it)
8070 {
8071 if (it->selective_display_ellipsis_p)
8072 setup_for_ellipsis (it, it->len);
8073 else
8074 {
8075 /* The face at the current position may be different from the
8076 face we find after the invisible text. Remember what it
8077 was in IT->saved_face_id, and signal that it's there by
8078 setting face_before_selective_p. */
8079 it->saved_face_id = it->face_id;
8080 it->method = GET_FROM_BUFFER;
8081 it->object = it->w->contents;
8082 reseat_at_next_visible_line_start (it, 1);
8083 it->face_before_selective_p = true;
8084 }
8085
8086 return GET_NEXT_DISPLAY_ELEMENT (it);
8087 }
8088
8089
8090 /* Deliver an image display element. The iterator IT is already
8091 filled with image information (done in handle_display_prop). Value
8092 is always 1. */
8093
8094
8095 static int
8096 next_element_from_image (struct it *it)
8097 {
8098 it->what = IT_IMAGE;
8099 it->ignore_overlay_strings_at_pos_p = 0;
8100 return 1;
8101 }
8102
8103
8104 /* Fill iterator IT with next display element from a stretch glyph
8105 property. IT->object is the value of the text property. Value is
8106 always 1. */
8107
8108 static int
8109 next_element_from_stretch (struct it *it)
8110 {
8111 it->what = IT_STRETCH;
8112 return 1;
8113 }
8114
8115 /* Scan backwards from IT's current position until we find a stop
8116 position, or until BEGV. This is called when we find ourself
8117 before both the last known prev_stop and base_level_stop while
8118 reordering bidirectional text. */
8119
8120 static void
8121 compute_stop_pos_backwards (struct it *it)
8122 {
8123 const int SCAN_BACK_LIMIT = 1000;
8124 struct text_pos pos;
8125 struct display_pos save_current = it->current;
8126 struct text_pos save_position = it->position;
8127 ptrdiff_t charpos = IT_CHARPOS (*it);
8128 ptrdiff_t where_we_are = charpos;
8129 ptrdiff_t save_stop_pos = it->stop_charpos;
8130 ptrdiff_t save_end_pos = it->end_charpos;
8131
8132 eassert (NILP (it->string) && !it->s);
8133 eassert (it->bidi_p);
8134 it->bidi_p = 0;
8135 do
8136 {
8137 it->end_charpos = min (charpos + 1, ZV);
8138 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8139 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8140 reseat_1 (it, pos, 0);
8141 compute_stop_pos (it);
8142 /* We must advance forward, right? */
8143 if (it->stop_charpos <= charpos)
8144 emacs_abort ();
8145 }
8146 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8147
8148 if (it->stop_charpos <= where_we_are)
8149 it->prev_stop = it->stop_charpos;
8150 else
8151 it->prev_stop = BEGV;
8152 it->bidi_p = true;
8153 it->current = save_current;
8154 it->position = save_position;
8155 it->stop_charpos = save_stop_pos;
8156 it->end_charpos = save_end_pos;
8157 }
8158
8159 /* Scan forward from CHARPOS in the current buffer/string, until we
8160 find a stop position > current IT's position. Then handle the stop
8161 position before that. This is called when we bump into a stop
8162 position while reordering bidirectional text. CHARPOS should be
8163 the last previously processed stop_pos (or BEGV/0, if none were
8164 processed yet) whose position is less that IT's current
8165 position. */
8166
8167 static void
8168 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8169 {
8170 int bufp = !STRINGP (it->string);
8171 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8172 struct display_pos save_current = it->current;
8173 struct text_pos save_position = it->position;
8174 struct text_pos pos1;
8175 ptrdiff_t next_stop;
8176
8177 /* Scan in strict logical order. */
8178 eassert (it->bidi_p);
8179 it->bidi_p = 0;
8180 do
8181 {
8182 it->prev_stop = charpos;
8183 if (bufp)
8184 {
8185 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8186 reseat_1 (it, pos1, 0);
8187 }
8188 else
8189 it->current.string_pos = string_pos (charpos, it->string);
8190 compute_stop_pos (it);
8191 /* We must advance forward, right? */
8192 if (it->stop_charpos <= it->prev_stop)
8193 emacs_abort ();
8194 charpos = it->stop_charpos;
8195 }
8196 while (charpos <= where_we_are);
8197
8198 it->bidi_p = true;
8199 it->current = save_current;
8200 it->position = save_position;
8201 next_stop = it->stop_charpos;
8202 it->stop_charpos = it->prev_stop;
8203 handle_stop (it);
8204 it->stop_charpos = next_stop;
8205 }
8206
8207 /* Load IT with the next display element from current_buffer. Value
8208 is zero if end of buffer reached. IT->stop_charpos is the next
8209 position at which to stop and check for text properties or buffer
8210 end. */
8211
8212 static int
8213 next_element_from_buffer (struct it *it)
8214 {
8215 bool success_p = true;
8216
8217 eassert (IT_CHARPOS (*it) >= BEGV);
8218 eassert (NILP (it->string) && !it->s);
8219 eassert (!it->bidi_p
8220 || (EQ (it->bidi_it.string.lstring, Qnil)
8221 && it->bidi_it.string.s == NULL));
8222
8223 /* With bidi reordering, the character to display might not be the
8224 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8225 we were reseat()ed to a new buffer position, which is potentially
8226 a different paragraph. */
8227 if (it->bidi_p && it->bidi_it.first_elt)
8228 {
8229 get_visually_first_element (it);
8230 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8231 }
8232
8233 if (IT_CHARPOS (*it) >= it->stop_charpos)
8234 {
8235 if (IT_CHARPOS (*it) >= it->end_charpos)
8236 {
8237 int overlay_strings_follow_p;
8238
8239 /* End of the game, except when overlay strings follow that
8240 haven't been returned yet. */
8241 if (it->overlay_strings_at_end_processed_p)
8242 overlay_strings_follow_p = 0;
8243 else
8244 {
8245 it->overlay_strings_at_end_processed_p = true;
8246 overlay_strings_follow_p = get_overlay_strings (it, 0);
8247 }
8248
8249 if (overlay_strings_follow_p)
8250 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8251 else
8252 {
8253 it->what = IT_EOB;
8254 it->position = it->current.pos;
8255 success_p = 0;
8256 }
8257 }
8258 else if (!(!it->bidi_p
8259 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8260 || IT_CHARPOS (*it) == it->stop_charpos))
8261 {
8262 /* With bidi non-linear iteration, we could find ourselves
8263 far beyond the last computed stop_charpos, with several
8264 other stop positions in between that we missed. Scan
8265 them all now, in buffer's logical order, until we find
8266 and handle the last stop_charpos that precedes our
8267 current position. */
8268 handle_stop_backwards (it, it->stop_charpos);
8269 return GET_NEXT_DISPLAY_ELEMENT (it);
8270 }
8271 else
8272 {
8273 if (it->bidi_p)
8274 {
8275 /* Take note of the stop position we just moved across,
8276 for when we will move back across it. */
8277 it->prev_stop = it->stop_charpos;
8278 /* If we are at base paragraph embedding level, take
8279 note of the last stop position seen at this
8280 level. */
8281 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8282 it->base_level_stop = it->stop_charpos;
8283 }
8284 handle_stop (it);
8285 return GET_NEXT_DISPLAY_ELEMENT (it);
8286 }
8287 }
8288 else if (it->bidi_p
8289 /* If we are before prev_stop, we may have overstepped on
8290 our way backwards a stop_pos, and if so, we need to
8291 handle that stop_pos. */
8292 && IT_CHARPOS (*it) < it->prev_stop
8293 /* We can sometimes back up for reasons that have nothing
8294 to do with bidi reordering. E.g., compositions. The
8295 code below is only needed when we are above the base
8296 embedding level, so test for that explicitly. */
8297 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8298 {
8299 if (it->base_level_stop <= 0
8300 || IT_CHARPOS (*it) < it->base_level_stop)
8301 {
8302 /* If we lost track of base_level_stop, we need to find
8303 prev_stop by looking backwards. This happens, e.g., when
8304 we were reseated to the previous screenful of text by
8305 vertical-motion. */
8306 it->base_level_stop = BEGV;
8307 compute_stop_pos_backwards (it);
8308 handle_stop_backwards (it, it->prev_stop);
8309 }
8310 else
8311 handle_stop_backwards (it, it->base_level_stop);
8312 return GET_NEXT_DISPLAY_ELEMENT (it);
8313 }
8314 else
8315 {
8316 /* No face changes, overlays etc. in sight, so just return a
8317 character from current_buffer. */
8318 unsigned char *p;
8319 ptrdiff_t stop;
8320
8321 /* Maybe run the redisplay end trigger hook. Performance note:
8322 This doesn't seem to cost measurable time. */
8323 if (it->redisplay_end_trigger_charpos
8324 && it->glyph_row
8325 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8326 run_redisplay_end_trigger_hook (it);
8327
8328 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8329 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8330 stop)
8331 && next_element_from_composition (it))
8332 {
8333 return 1;
8334 }
8335
8336 /* Get the next character, maybe multibyte. */
8337 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8338 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8339 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8340 else
8341 it->c = *p, it->len = 1;
8342
8343 /* Record what we have and where it came from. */
8344 it->what = IT_CHARACTER;
8345 it->object = it->w->contents;
8346 it->position = it->current.pos;
8347
8348 /* Normally we return the character found above, except when we
8349 really want to return an ellipsis for selective display. */
8350 if (it->selective)
8351 {
8352 if (it->c == '\n')
8353 {
8354 /* A value of selective > 0 means hide lines indented more
8355 than that number of columns. */
8356 if (it->selective > 0
8357 && IT_CHARPOS (*it) + 1 < ZV
8358 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8359 IT_BYTEPOS (*it) + 1,
8360 it->selective))
8361 {
8362 success_p = next_element_from_ellipsis (it);
8363 it->dpvec_char_len = -1;
8364 }
8365 }
8366 else if (it->c == '\r' && it->selective == -1)
8367 {
8368 /* A value of selective == -1 means that everything from the
8369 CR to the end of the line is invisible, with maybe an
8370 ellipsis displayed for it. */
8371 success_p = next_element_from_ellipsis (it);
8372 it->dpvec_char_len = -1;
8373 }
8374 }
8375 }
8376
8377 /* Value is zero if end of buffer reached. */
8378 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8379 return success_p;
8380 }
8381
8382
8383 /* Run the redisplay end trigger hook for IT. */
8384
8385 static void
8386 run_redisplay_end_trigger_hook (struct it *it)
8387 {
8388 Lisp_Object args[3];
8389
8390 /* IT->glyph_row should be non-null, i.e. we should be actually
8391 displaying something, or otherwise we should not run the hook. */
8392 eassert (it->glyph_row);
8393
8394 /* Set up hook arguments. */
8395 args[0] = Qredisplay_end_trigger_functions;
8396 args[1] = it->window;
8397 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8398 it->redisplay_end_trigger_charpos = 0;
8399
8400 /* Since we are *trying* to run these functions, don't try to run
8401 them again, even if they get an error. */
8402 wset_redisplay_end_trigger (it->w, Qnil);
8403 Frun_hook_with_args (3, args);
8404
8405 /* Notice if it changed the face of the character we are on. */
8406 handle_face_prop (it);
8407 }
8408
8409
8410 /* Deliver a composition display element. Unlike the other
8411 next_element_from_XXX, this function is not registered in the array
8412 get_next_element[]. It is called from next_element_from_buffer and
8413 next_element_from_string when necessary. */
8414
8415 static int
8416 next_element_from_composition (struct it *it)
8417 {
8418 it->what = IT_COMPOSITION;
8419 it->len = it->cmp_it.nbytes;
8420 if (STRINGP (it->string))
8421 {
8422 if (it->c < 0)
8423 {
8424 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8425 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8426 return 0;
8427 }
8428 it->position = it->current.string_pos;
8429 it->object = it->string;
8430 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8431 IT_STRING_BYTEPOS (*it), it->string);
8432 }
8433 else
8434 {
8435 if (it->c < 0)
8436 {
8437 IT_CHARPOS (*it) += it->cmp_it.nchars;
8438 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8439 if (it->bidi_p)
8440 {
8441 if (it->bidi_it.new_paragraph)
8442 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8443 /* Resync the bidi iterator with IT's new position.
8444 FIXME: this doesn't support bidirectional text. */
8445 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8446 bidi_move_to_visually_next (&it->bidi_it);
8447 }
8448 return 0;
8449 }
8450 it->position = it->current.pos;
8451 it->object = it->w->contents;
8452 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8453 IT_BYTEPOS (*it), Qnil);
8454 }
8455 return 1;
8456 }
8457
8458
8459 \f
8460 /***********************************************************************
8461 Moving an iterator without producing glyphs
8462 ***********************************************************************/
8463
8464 /* Check if iterator is at a position corresponding to a valid buffer
8465 position after some move_it_ call. */
8466
8467 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8468 ((it)->method == GET_FROM_STRING \
8469 ? IT_STRING_CHARPOS (*it) == 0 \
8470 : 1)
8471
8472
8473 /* Move iterator IT to a specified buffer or X position within one
8474 line on the display without producing glyphs.
8475
8476 OP should be a bit mask including some or all of these bits:
8477 MOVE_TO_X: Stop upon reaching x-position TO_X.
8478 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8479 Regardless of OP's value, stop upon reaching the end of the display line.
8480
8481 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8482 This means, in particular, that TO_X includes window's horizontal
8483 scroll amount.
8484
8485 The return value has several possible values that
8486 say what condition caused the scan to stop:
8487
8488 MOVE_POS_MATCH_OR_ZV
8489 - when TO_POS or ZV was reached.
8490
8491 MOVE_X_REACHED
8492 -when TO_X was reached before TO_POS or ZV were reached.
8493
8494 MOVE_LINE_CONTINUED
8495 - when we reached the end of the display area and the line must
8496 be continued.
8497
8498 MOVE_LINE_TRUNCATED
8499 - when we reached the end of the display area and the line is
8500 truncated.
8501
8502 MOVE_NEWLINE_OR_CR
8503 - when we stopped at a line end, i.e. a newline or a CR and selective
8504 display is on. */
8505
8506 static enum move_it_result
8507 move_it_in_display_line_to (struct it *it,
8508 ptrdiff_t to_charpos, int to_x,
8509 enum move_operation_enum op)
8510 {
8511 enum move_it_result result = MOVE_UNDEFINED;
8512 struct glyph_row *saved_glyph_row;
8513 struct it wrap_it, atpos_it, atx_it, ppos_it;
8514 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8515 void *ppos_data = NULL;
8516 int may_wrap = 0;
8517 enum it_method prev_method = it->method;
8518 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8519 int saw_smaller_pos = prev_pos < to_charpos;
8520
8521 /* Don't produce glyphs in produce_glyphs. */
8522 saved_glyph_row = it->glyph_row;
8523 it->glyph_row = NULL;
8524
8525 /* Use wrap_it to save a copy of IT wherever a word wrap could
8526 occur. Use atpos_it to save a copy of IT at the desired buffer
8527 position, if found, so that we can scan ahead and check if the
8528 word later overshoots the window edge. Use atx_it similarly, for
8529 pixel positions. */
8530 wrap_it.sp = -1;
8531 atpos_it.sp = -1;
8532 atx_it.sp = -1;
8533
8534 /* Use ppos_it under bidi reordering to save a copy of IT for the
8535 initial position. We restore that position in IT when we have
8536 scanned the entire display line without finding a match for
8537 TO_CHARPOS and all the character positions are greater than
8538 TO_CHARPOS. We then restart the scan from the initial position,
8539 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8540 the closest to TO_CHARPOS. */
8541 if (it->bidi_p)
8542 {
8543 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8544 {
8545 SAVE_IT (ppos_it, *it, ppos_data);
8546 closest_pos = IT_CHARPOS (*it);
8547 }
8548 else
8549 closest_pos = ZV;
8550 }
8551
8552 #define BUFFER_POS_REACHED_P() \
8553 ((op & MOVE_TO_POS) != 0 \
8554 && BUFFERP (it->object) \
8555 && (IT_CHARPOS (*it) == to_charpos \
8556 || ((!it->bidi_p \
8557 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8558 && IT_CHARPOS (*it) > to_charpos) \
8559 || (it->what == IT_COMPOSITION \
8560 && ((IT_CHARPOS (*it) > to_charpos \
8561 && to_charpos >= it->cmp_it.charpos) \
8562 || (IT_CHARPOS (*it) < to_charpos \
8563 && to_charpos <= it->cmp_it.charpos)))) \
8564 && (it->method == GET_FROM_BUFFER \
8565 || (it->method == GET_FROM_DISPLAY_VECTOR \
8566 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8567
8568 /* If there's a line-/wrap-prefix, handle it. */
8569 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8570 && it->current_y < it->last_visible_y)
8571 handle_line_prefix (it);
8572
8573 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8574 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8575
8576 while (1)
8577 {
8578 int x, i, ascent = 0, descent = 0;
8579
8580 /* Utility macro to reset an iterator with x, ascent, and descent. */
8581 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8582 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8583 (IT)->max_descent = descent)
8584
8585 /* Stop if we move beyond TO_CHARPOS (after an image or a
8586 display string or stretch glyph). */
8587 if ((op & MOVE_TO_POS) != 0
8588 && BUFFERP (it->object)
8589 && it->method == GET_FROM_BUFFER
8590 && (((!it->bidi_p
8591 /* When the iterator is at base embedding level, we
8592 are guaranteed that characters are delivered for
8593 display in strictly increasing order of their
8594 buffer positions. */
8595 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8596 && IT_CHARPOS (*it) > to_charpos)
8597 || (it->bidi_p
8598 && (prev_method == GET_FROM_IMAGE
8599 || prev_method == GET_FROM_STRETCH
8600 || prev_method == GET_FROM_STRING)
8601 /* Passed TO_CHARPOS from left to right. */
8602 && ((prev_pos < to_charpos
8603 && IT_CHARPOS (*it) > to_charpos)
8604 /* Passed TO_CHARPOS from right to left. */
8605 || (prev_pos > to_charpos
8606 && IT_CHARPOS (*it) < to_charpos)))))
8607 {
8608 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8609 {
8610 result = MOVE_POS_MATCH_OR_ZV;
8611 break;
8612 }
8613 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8614 /* If wrap_it is valid, the current position might be in a
8615 word that is wrapped. So, save the iterator in
8616 atpos_it and continue to see if wrapping happens. */
8617 SAVE_IT (atpos_it, *it, atpos_data);
8618 }
8619
8620 /* Stop when ZV reached.
8621 We used to stop here when TO_CHARPOS reached as well, but that is
8622 too soon if this glyph does not fit on this line. So we handle it
8623 explicitly below. */
8624 if (!get_next_display_element (it))
8625 {
8626 result = MOVE_POS_MATCH_OR_ZV;
8627 break;
8628 }
8629
8630 if (it->line_wrap == TRUNCATE)
8631 {
8632 if (BUFFER_POS_REACHED_P ())
8633 {
8634 result = MOVE_POS_MATCH_OR_ZV;
8635 break;
8636 }
8637 }
8638 else
8639 {
8640 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8641 {
8642 if (IT_DISPLAYING_WHITESPACE (it))
8643 may_wrap = 1;
8644 else if (may_wrap)
8645 {
8646 /* We have reached a glyph that follows one or more
8647 whitespace characters. If the position is
8648 already found, we are done. */
8649 if (atpos_it.sp >= 0)
8650 {
8651 RESTORE_IT (it, &atpos_it, atpos_data);
8652 result = MOVE_POS_MATCH_OR_ZV;
8653 goto done;
8654 }
8655 if (atx_it.sp >= 0)
8656 {
8657 RESTORE_IT (it, &atx_it, atx_data);
8658 result = MOVE_X_REACHED;
8659 goto done;
8660 }
8661 /* Otherwise, we can wrap here. */
8662 SAVE_IT (wrap_it, *it, wrap_data);
8663 may_wrap = 0;
8664 }
8665 }
8666 }
8667
8668 /* Remember the line height for the current line, in case
8669 the next element doesn't fit on the line. */
8670 ascent = it->max_ascent;
8671 descent = it->max_descent;
8672
8673 /* The call to produce_glyphs will get the metrics of the
8674 display element IT is loaded with. Record the x-position
8675 before this display element, in case it doesn't fit on the
8676 line. */
8677 x = it->current_x;
8678
8679 PRODUCE_GLYPHS (it);
8680
8681 if (it->area != TEXT_AREA)
8682 {
8683 prev_method = it->method;
8684 if (it->method == GET_FROM_BUFFER)
8685 prev_pos = IT_CHARPOS (*it);
8686 set_iterator_to_next (it, 1);
8687 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8688 SET_TEXT_POS (this_line_min_pos,
8689 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8690 if (it->bidi_p
8691 && (op & MOVE_TO_POS)
8692 && IT_CHARPOS (*it) > to_charpos
8693 && IT_CHARPOS (*it) < closest_pos)
8694 closest_pos = IT_CHARPOS (*it);
8695 continue;
8696 }
8697
8698 /* The number of glyphs we get back in IT->nglyphs will normally
8699 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8700 character on a terminal frame, or (iii) a line end. For the
8701 second case, IT->nglyphs - 1 padding glyphs will be present.
8702 (On X frames, there is only one glyph produced for a
8703 composite character.)
8704
8705 The behavior implemented below means, for continuation lines,
8706 that as many spaces of a TAB as fit on the current line are
8707 displayed there. For terminal frames, as many glyphs of a
8708 multi-glyph character are displayed in the current line, too.
8709 This is what the old redisplay code did, and we keep it that
8710 way. Under X, the whole shape of a complex character must
8711 fit on the line or it will be completely displayed in the
8712 next line.
8713
8714 Note that both for tabs and padding glyphs, all glyphs have
8715 the same width. */
8716 if (it->nglyphs)
8717 {
8718 /* More than one glyph or glyph doesn't fit on line. All
8719 glyphs have the same width. */
8720 int single_glyph_width = it->pixel_width / it->nglyphs;
8721 int new_x;
8722 int x_before_this_char = x;
8723 int hpos_before_this_char = it->hpos;
8724
8725 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8726 {
8727 new_x = x + single_glyph_width;
8728
8729 /* We want to leave anything reaching TO_X to the caller. */
8730 if ((op & MOVE_TO_X) && new_x > to_x)
8731 {
8732 if (BUFFER_POS_REACHED_P ())
8733 {
8734 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8735 goto buffer_pos_reached;
8736 if (atpos_it.sp < 0)
8737 {
8738 SAVE_IT (atpos_it, *it, atpos_data);
8739 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8740 }
8741 }
8742 else
8743 {
8744 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8745 {
8746 it->current_x = x;
8747 result = MOVE_X_REACHED;
8748 break;
8749 }
8750 if (atx_it.sp < 0)
8751 {
8752 SAVE_IT (atx_it, *it, atx_data);
8753 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8754 }
8755 }
8756 }
8757
8758 if (/* Lines are continued. */
8759 it->line_wrap != TRUNCATE
8760 && (/* And glyph doesn't fit on the line. */
8761 new_x > it->last_visible_x
8762 /* Or it fits exactly and we're on a window
8763 system frame. */
8764 || (new_x == it->last_visible_x
8765 && FRAME_WINDOW_P (it->f)
8766 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8767 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8768 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8769 {
8770 if (/* IT->hpos == 0 means the very first glyph
8771 doesn't fit on the line, e.g. a wide image. */
8772 it->hpos == 0
8773 || (new_x == it->last_visible_x
8774 && FRAME_WINDOW_P (it->f)
8775 /* When word-wrap is ON and we have a valid
8776 wrap point, we don't allow the last glyph
8777 to "just barely fit" on the line. */
8778 && (it->line_wrap != WORD_WRAP
8779 || wrap_it.sp < 0)))
8780 {
8781 ++it->hpos;
8782 it->current_x = new_x;
8783
8784 /* The character's last glyph just barely fits
8785 in this row. */
8786 if (i == it->nglyphs - 1)
8787 {
8788 /* If this is the destination position,
8789 return a position *before* it in this row,
8790 now that we know it fits in this row. */
8791 if (BUFFER_POS_REACHED_P ())
8792 {
8793 if (it->line_wrap != WORD_WRAP
8794 || wrap_it.sp < 0)
8795 {
8796 it->hpos = hpos_before_this_char;
8797 it->current_x = x_before_this_char;
8798 result = MOVE_POS_MATCH_OR_ZV;
8799 break;
8800 }
8801 if (it->line_wrap == WORD_WRAP
8802 && atpos_it.sp < 0)
8803 {
8804 SAVE_IT (atpos_it, *it, atpos_data);
8805 atpos_it.current_x = x_before_this_char;
8806 atpos_it.hpos = hpos_before_this_char;
8807 }
8808 }
8809
8810 prev_method = it->method;
8811 if (it->method == GET_FROM_BUFFER)
8812 prev_pos = IT_CHARPOS (*it);
8813 set_iterator_to_next (it, 1);
8814 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8815 SET_TEXT_POS (this_line_min_pos,
8816 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8817 /* On graphical terminals, newlines may
8818 "overflow" into the fringe if
8819 overflow-newline-into-fringe is non-nil.
8820 On text terminals, and on graphical
8821 terminals with no right margin, newlines
8822 may overflow into the last glyph on the
8823 display line.*/
8824 if (!FRAME_WINDOW_P (it->f)
8825 || ((it->bidi_p
8826 && it->bidi_it.paragraph_dir == R2L)
8827 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8828 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8829 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8830 {
8831 if (!get_next_display_element (it))
8832 {
8833 result = MOVE_POS_MATCH_OR_ZV;
8834 break;
8835 }
8836 if (BUFFER_POS_REACHED_P ())
8837 {
8838 if (ITERATOR_AT_END_OF_LINE_P (it))
8839 result = MOVE_POS_MATCH_OR_ZV;
8840 else
8841 result = MOVE_LINE_CONTINUED;
8842 break;
8843 }
8844 if (ITERATOR_AT_END_OF_LINE_P (it)
8845 && (it->line_wrap != WORD_WRAP
8846 || wrap_it.sp < 0))
8847 {
8848 result = MOVE_NEWLINE_OR_CR;
8849 break;
8850 }
8851 }
8852 }
8853 }
8854 else
8855 IT_RESET_X_ASCENT_DESCENT (it);
8856
8857 if (wrap_it.sp >= 0)
8858 {
8859 RESTORE_IT (it, &wrap_it, wrap_data);
8860 atpos_it.sp = -1;
8861 atx_it.sp = -1;
8862 }
8863
8864 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8865 IT_CHARPOS (*it)));
8866 result = MOVE_LINE_CONTINUED;
8867 break;
8868 }
8869
8870 if (BUFFER_POS_REACHED_P ())
8871 {
8872 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8873 goto buffer_pos_reached;
8874 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8875 {
8876 SAVE_IT (atpos_it, *it, atpos_data);
8877 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8878 }
8879 }
8880
8881 if (new_x > it->first_visible_x)
8882 {
8883 /* Glyph is visible. Increment number of glyphs that
8884 would be displayed. */
8885 ++it->hpos;
8886 }
8887 }
8888
8889 if (result != MOVE_UNDEFINED)
8890 break;
8891 }
8892 else if (BUFFER_POS_REACHED_P ())
8893 {
8894 buffer_pos_reached:
8895 IT_RESET_X_ASCENT_DESCENT (it);
8896 result = MOVE_POS_MATCH_OR_ZV;
8897 break;
8898 }
8899 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8900 {
8901 /* Stop when TO_X specified and reached. This check is
8902 necessary here because of lines consisting of a line end,
8903 only. The line end will not produce any glyphs and we
8904 would never get MOVE_X_REACHED. */
8905 eassert (it->nglyphs == 0);
8906 result = MOVE_X_REACHED;
8907 break;
8908 }
8909
8910 /* Is this a line end? If yes, we're done. */
8911 if (ITERATOR_AT_END_OF_LINE_P (it))
8912 {
8913 /* If we are past TO_CHARPOS, but never saw any character
8914 positions smaller than TO_CHARPOS, return
8915 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8916 did. */
8917 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8918 {
8919 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8920 {
8921 if (closest_pos < ZV)
8922 {
8923 RESTORE_IT (it, &ppos_it, ppos_data);
8924 /* Don't recurse if closest_pos is equal to
8925 to_charpos, since we have just tried that. */
8926 if (closest_pos != to_charpos)
8927 move_it_in_display_line_to (it, closest_pos, -1,
8928 MOVE_TO_POS);
8929 result = MOVE_POS_MATCH_OR_ZV;
8930 }
8931 else
8932 goto buffer_pos_reached;
8933 }
8934 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8935 && IT_CHARPOS (*it) > to_charpos)
8936 goto buffer_pos_reached;
8937 else
8938 result = MOVE_NEWLINE_OR_CR;
8939 }
8940 else
8941 result = MOVE_NEWLINE_OR_CR;
8942 break;
8943 }
8944
8945 prev_method = it->method;
8946 if (it->method == GET_FROM_BUFFER)
8947 prev_pos = IT_CHARPOS (*it);
8948 /* The current display element has been consumed. Advance
8949 to the next. */
8950 set_iterator_to_next (it, 1);
8951 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8952 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8953 if (IT_CHARPOS (*it) < to_charpos)
8954 saw_smaller_pos = 1;
8955 if (it->bidi_p
8956 && (op & MOVE_TO_POS)
8957 && IT_CHARPOS (*it) >= to_charpos
8958 && IT_CHARPOS (*it) < closest_pos)
8959 closest_pos = IT_CHARPOS (*it);
8960
8961 /* Stop if lines are truncated and IT's current x-position is
8962 past the right edge of the window now. */
8963 if (it->line_wrap == TRUNCATE
8964 && it->current_x >= it->last_visible_x)
8965 {
8966 if (!FRAME_WINDOW_P (it->f)
8967 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8968 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8969 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8970 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8971 {
8972 int at_eob_p = 0;
8973
8974 if ((at_eob_p = !get_next_display_element (it))
8975 || BUFFER_POS_REACHED_P ()
8976 /* If we are past TO_CHARPOS, but never saw any
8977 character positions smaller than TO_CHARPOS,
8978 return MOVE_POS_MATCH_OR_ZV, like the
8979 unidirectional display did. */
8980 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8981 && !saw_smaller_pos
8982 && IT_CHARPOS (*it) > to_charpos))
8983 {
8984 if (it->bidi_p
8985 && !BUFFER_POS_REACHED_P ()
8986 && !at_eob_p && closest_pos < ZV)
8987 {
8988 RESTORE_IT (it, &ppos_it, ppos_data);
8989 if (closest_pos != to_charpos)
8990 move_it_in_display_line_to (it, closest_pos, -1,
8991 MOVE_TO_POS);
8992 }
8993 result = MOVE_POS_MATCH_OR_ZV;
8994 break;
8995 }
8996 if (ITERATOR_AT_END_OF_LINE_P (it))
8997 {
8998 result = MOVE_NEWLINE_OR_CR;
8999 break;
9000 }
9001 }
9002 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9003 && !saw_smaller_pos
9004 && IT_CHARPOS (*it) > to_charpos)
9005 {
9006 if (closest_pos < ZV)
9007 {
9008 RESTORE_IT (it, &ppos_it, ppos_data);
9009 if (closest_pos != to_charpos)
9010 move_it_in_display_line_to (it, closest_pos, -1,
9011 MOVE_TO_POS);
9012 }
9013 result = MOVE_POS_MATCH_OR_ZV;
9014 break;
9015 }
9016 result = MOVE_LINE_TRUNCATED;
9017 break;
9018 }
9019 #undef IT_RESET_X_ASCENT_DESCENT
9020 }
9021
9022 #undef BUFFER_POS_REACHED_P
9023
9024 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9025 restore the saved iterator. */
9026 if (atpos_it.sp >= 0)
9027 RESTORE_IT (it, &atpos_it, atpos_data);
9028 else if (atx_it.sp >= 0)
9029 RESTORE_IT (it, &atx_it, atx_data);
9030
9031 done:
9032
9033 if (atpos_data)
9034 bidi_unshelve_cache (atpos_data, 1);
9035 if (atx_data)
9036 bidi_unshelve_cache (atx_data, 1);
9037 if (wrap_data)
9038 bidi_unshelve_cache (wrap_data, 1);
9039 if (ppos_data)
9040 bidi_unshelve_cache (ppos_data, 1);
9041
9042 /* Restore the iterator settings altered at the beginning of this
9043 function. */
9044 it->glyph_row = saved_glyph_row;
9045 return result;
9046 }
9047
9048 /* For external use. */
9049 void
9050 move_it_in_display_line (struct it *it,
9051 ptrdiff_t to_charpos, int to_x,
9052 enum move_operation_enum op)
9053 {
9054 if (it->line_wrap == WORD_WRAP
9055 && (op & MOVE_TO_X))
9056 {
9057 struct it save_it;
9058 void *save_data = NULL;
9059 int skip;
9060
9061 SAVE_IT (save_it, *it, save_data);
9062 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9063 /* When word-wrap is on, TO_X may lie past the end
9064 of a wrapped line. Then it->current is the
9065 character on the next line, so backtrack to the
9066 space before the wrap point. */
9067 if (skip == MOVE_LINE_CONTINUED)
9068 {
9069 int prev_x = max (it->current_x - 1, 0);
9070 RESTORE_IT (it, &save_it, save_data);
9071 move_it_in_display_line_to
9072 (it, -1, prev_x, MOVE_TO_X);
9073 }
9074 else
9075 bidi_unshelve_cache (save_data, 1);
9076 }
9077 else
9078 move_it_in_display_line_to (it, to_charpos, to_x, op);
9079 }
9080
9081
9082 /* Move IT forward until it satisfies one or more of the criteria in
9083 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9084
9085 OP is a bit-mask that specifies where to stop, and in particular,
9086 which of those four position arguments makes a difference. See the
9087 description of enum move_operation_enum.
9088
9089 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9090 screen line, this function will set IT to the next position that is
9091 displayed to the right of TO_CHARPOS on the screen.
9092
9093 Return the maximum pixel length of any line scanned but never more
9094 than it.last_visible_x. */
9095
9096 int
9097 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9098 {
9099 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9100 int line_height, line_start_x = 0, reached = 0;
9101 int max_current_x = 0;
9102 void *backup_data = NULL;
9103
9104 for (;;)
9105 {
9106 if (op & MOVE_TO_VPOS)
9107 {
9108 /* If no TO_CHARPOS and no TO_X specified, stop at the
9109 start of the line TO_VPOS. */
9110 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9111 {
9112 if (it->vpos == to_vpos)
9113 {
9114 reached = 1;
9115 break;
9116 }
9117 else
9118 skip = move_it_in_display_line_to (it, -1, -1, 0);
9119 }
9120 else
9121 {
9122 /* TO_VPOS >= 0 means stop at TO_X in the line at
9123 TO_VPOS, or at TO_POS, whichever comes first. */
9124 if (it->vpos == to_vpos)
9125 {
9126 reached = 2;
9127 break;
9128 }
9129
9130 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9131
9132 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9133 {
9134 reached = 3;
9135 break;
9136 }
9137 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9138 {
9139 /* We have reached TO_X but not in the line we want. */
9140 skip = move_it_in_display_line_to (it, to_charpos,
9141 -1, MOVE_TO_POS);
9142 if (skip == MOVE_POS_MATCH_OR_ZV)
9143 {
9144 reached = 4;
9145 break;
9146 }
9147 }
9148 }
9149 }
9150 else if (op & MOVE_TO_Y)
9151 {
9152 struct it it_backup;
9153
9154 if (it->line_wrap == WORD_WRAP)
9155 SAVE_IT (it_backup, *it, backup_data);
9156
9157 /* TO_Y specified means stop at TO_X in the line containing
9158 TO_Y---or at TO_CHARPOS if this is reached first. The
9159 problem is that we can't really tell whether the line
9160 contains TO_Y before we have completely scanned it, and
9161 this may skip past TO_X. What we do is to first scan to
9162 TO_X.
9163
9164 If TO_X is not specified, use a TO_X of zero. The reason
9165 is to make the outcome of this function more predictable.
9166 If we didn't use TO_X == 0, we would stop at the end of
9167 the line which is probably not what a caller would expect
9168 to happen. */
9169 skip = move_it_in_display_line_to
9170 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9171 (MOVE_TO_X | (op & MOVE_TO_POS)));
9172
9173 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9174 if (skip == MOVE_POS_MATCH_OR_ZV)
9175 reached = 5;
9176 else if (skip == MOVE_X_REACHED)
9177 {
9178 /* If TO_X was reached, we want to know whether TO_Y is
9179 in the line. We know this is the case if the already
9180 scanned glyphs make the line tall enough. Otherwise,
9181 we must check by scanning the rest of the line. */
9182 line_height = it->max_ascent + it->max_descent;
9183 if (to_y >= it->current_y
9184 && to_y < it->current_y + line_height)
9185 {
9186 reached = 6;
9187 break;
9188 }
9189 SAVE_IT (it_backup, *it, backup_data);
9190 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9191 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9192 op & MOVE_TO_POS);
9193 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9194 line_height = it->max_ascent + it->max_descent;
9195 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9196
9197 if (to_y >= it->current_y
9198 && to_y < it->current_y + line_height)
9199 {
9200 /* If TO_Y is in this line and TO_X was reached
9201 above, we scanned too far. We have to restore
9202 IT's settings to the ones before skipping. But
9203 keep the more accurate values of max_ascent and
9204 max_descent we've found while skipping the rest
9205 of the line, for the sake of callers, such as
9206 pos_visible_p, that need to know the line
9207 height. */
9208 int max_ascent = it->max_ascent;
9209 int max_descent = it->max_descent;
9210
9211 RESTORE_IT (it, &it_backup, backup_data);
9212 it->max_ascent = max_ascent;
9213 it->max_descent = max_descent;
9214 reached = 6;
9215 }
9216 else
9217 {
9218 skip = skip2;
9219 if (skip == MOVE_POS_MATCH_OR_ZV)
9220 reached = 7;
9221 }
9222 }
9223 else
9224 {
9225 /* Check whether TO_Y is in this line. */
9226 line_height = it->max_ascent + it->max_descent;
9227 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9228
9229 if (to_y >= it->current_y
9230 && to_y < it->current_y + line_height)
9231 {
9232 if (to_y > it->current_y)
9233 max_current_x = max (it->current_x, max_current_x);
9234
9235 /* When word-wrap is on, TO_X may lie past the end
9236 of a wrapped line. Then it->current is the
9237 character on the next line, so backtrack to the
9238 space before the wrap point. */
9239 if (skip == MOVE_LINE_CONTINUED
9240 && it->line_wrap == WORD_WRAP)
9241 {
9242 int prev_x = max (it->current_x - 1, 0);
9243 RESTORE_IT (it, &it_backup, backup_data);
9244 skip = move_it_in_display_line_to
9245 (it, -1, prev_x, MOVE_TO_X);
9246 }
9247
9248 reached = 6;
9249 }
9250 }
9251
9252 if (reached)
9253 {
9254 max_current_x = max (it->current_x, max_current_x);
9255 break;
9256 }
9257 }
9258 else if (BUFFERP (it->object)
9259 && (it->method == GET_FROM_BUFFER
9260 || it->method == GET_FROM_STRETCH)
9261 && IT_CHARPOS (*it) >= to_charpos
9262 /* Under bidi iteration, a call to set_iterator_to_next
9263 can scan far beyond to_charpos if the initial
9264 portion of the next line needs to be reordered. In
9265 that case, give move_it_in_display_line_to another
9266 chance below. */
9267 && !(it->bidi_p
9268 && it->bidi_it.scan_dir == -1))
9269 skip = MOVE_POS_MATCH_OR_ZV;
9270 else
9271 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9272
9273 switch (skip)
9274 {
9275 case MOVE_POS_MATCH_OR_ZV:
9276 max_current_x = max (it->current_x, max_current_x);
9277 reached = 8;
9278 goto out;
9279
9280 case MOVE_NEWLINE_OR_CR:
9281 max_current_x = max (it->current_x, max_current_x);
9282 set_iterator_to_next (it, 1);
9283 it->continuation_lines_width = 0;
9284 break;
9285
9286 case MOVE_LINE_TRUNCATED:
9287 max_current_x = it->last_visible_x;
9288 it->continuation_lines_width = 0;
9289 reseat_at_next_visible_line_start (it, 0);
9290 if ((op & MOVE_TO_POS) != 0
9291 && IT_CHARPOS (*it) > to_charpos)
9292 {
9293 reached = 9;
9294 goto out;
9295 }
9296 break;
9297
9298 case MOVE_LINE_CONTINUED:
9299 max_current_x = it->last_visible_x;
9300 /* For continued lines ending in a tab, some of the glyphs
9301 associated with the tab are displayed on the current
9302 line. Since it->current_x does not include these glyphs,
9303 we use it->last_visible_x instead. */
9304 if (it->c == '\t')
9305 {
9306 it->continuation_lines_width += it->last_visible_x;
9307 /* When moving by vpos, ensure that the iterator really
9308 advances to the next line (bug#847, bug#969). Fixme:
9309 do we need to do this in other circumstances? */
9310 if (it->current_x != it->last_visible_x
9311 && (op & MOVE_TO_VPOS)
9312 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9313 {
9314 line_start_x = it->current_x + it->pixel_width
9315 - it->last_visible_x;
9316 if (FRAME_WINDOW_P (it->f))
9317 {
9318 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9319 struct font *face_font = face->font;
9320
9321 /* When display_line produces a continued line
9322 that ends in a TAB, it skips a tab stop that
9323 is closer than the font's space character
9324 width (see x_produce_glyphs where it produces
9325 the stretch glyph which represents a TAB).
9326 We need to reproduce the same logic here. */
9327 eassert (face_font);
9328 if (face_font)
9329 {
9330 if (line_start_x < face_font->space_width)
9331 line_start_x
9332 += it->tab_width * face_font->space_width;
9333 }
9334 }
9335 set_iterator_to_next (it, 0);
9336 }
9337 }
9338 else
9339 it->continuation_lines_width += it->current_x;
9340 break;
9341
9342 default:
9343 emacs_abort ();
9344 }
9345
9346 /* Reset/increment for the next run. */
9347 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9348 it->current_x = line_start_x;
9349 line_start_x = 0;
9350 it->hpos = 0;
9351 it->current_y += it->max_ascent + it->max_descent;
9352 ++it->vpos;
9353 last_height = it->max_ascent + it->max_descent;
9354 it->max_ascent = it->max_descent = 0;
9355 }
9356
9357 out:
9358
9359 /* On text terminals, we may stop at the end of a line in the middle
9360 of a multi-character glyph. If the glyph itself is continued,
9361 i.e. it is actually displayed on the next line, don't treat this
9362 stopping point as valid; move to the next line instead (unless
9363 that brings us offscreen). */
9364 if (!FRAME_WINDOW_P (it->f)
9365 && op & MOVE_TO_POS
9366 && IT_CHARPOS (*it) == to_charpos
9367 && it->what == IT_CHARACTER
9368 && it->nglyphs > 1
9369 && it->line_wrap == WINDOW_WRAP
9370 && it->current_x == it->last_visible_x - 1
9371 && it->c != '\n'
9372 && it->c != '\t'
9373 && it->vpos < it->w->window_end_vpos)
9374 {
9375 it->continuation_lines_width += it->current_x;
9376 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9377 it->current_y += it->max_ascent + it->max_descent;
9378 ++it->vpos;
9379 last_height = it->max_ascent + it->max_descent;
9380 }
9381
9382 if (backup_data)
9383 bidi_unshelve_cache (backup_data, 1);
9384
9385 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9386
9387 return max_current_x;
9388 }
9389
9390
9391 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9392
9393 If DY > 0, move IT backward at least that many pixels. DY = 0
9394 means move IT backward to the preceding line start or BEGV. This
9395 function may move over more than DY pixels if IT->current_y - DY
9396 ends up in the middle of a line; in this case IT->current_y will be
9397 set to the top of the line moved to. */
9398
9399 void
9400 move_it_vertically_backward (struct it *it, int dy)
9401 {
9402 int nlines, h;
9403 struct it it2, it3;
9404 void *it2data = NULL, *it3data = NULL;
9405 ptrdiff_t start_pos;
9406 int nchars_per_row
9407 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9408 ptrdiff_t pos_limit;
9409
9410 move_further_back:
9411 eassert (dy >= 0);
9412
9413 start_pos = IT_CHARPOS (*it);
9414
9415 /* Estimate how many newlines we must move back. */
9416 nlines = max (1, dy / default_line_pixel_height (it->w));
9417 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9418 pos_limit = BEGV;
9419 else
9420 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9421
9422 /* Set the iterator's position that many lines back. But don't go
9423 back more than NLINES full screen lines -- this wins a day with
9424 buffers which have very long lines. */
9425 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9426 back_to_previous_visible_line_start (it);
9427
9428 /* Reseat the iterator here. When moving backward, we don't want
9429 reseat to skip forward over invisible text, set up the iterator
9430 to deliver from overlay strings at the new position etc. So,
9431 use reseat_1 here. */
9432 reseat_1 (it, it->current.pos, 1);
9433
9434 /* We are now surely at a line start. */
9435 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9436 reordering is in effect. */
9437 it->continuation_lines_width = 0;
9438
9439 /* Move forward and see what y-distance we moved. First move to the
9440 start of the next line so that we get its height. We need this
9441 height to be able to tell whether we reached the specified
9442 y-distance. */
9443 SAVE_IT (it2, *it, it2data);
9444 it2.max_ascent = it2.max_descent = 0;
9445 do
9446 {
9447 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9448 MOVE_TO_POS | MOVE_TO_VPOS);
9449 }
9450 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9451 /* If we are in a display string which starts at START_POS,
9452 and that display string includes a newline, and we are
9453 right after that newline (i.e. at the beginning of a
9454 display line), exit the loop, because otherwise we will
9455 infloop, since move_it_to will see that it is already at
9456 START_POS and will not move. */
9457 || (it2.method == GET_FROM_STRING
9458 && IT_CHARPOS (it2) == start_pos
9459 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9460 eassert (IT_CHARPOS (*it) >= BEGV);
9461 SAVE_IT (it3, it2, it3data);
9462
9463 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9464 eassert (IT_CHARPOS (*it) >= BEGV);
9465 /* H is the actual vertical distance from the position in *IT
9466 and the starting position. */
9467 h = it2.current_y - it->current_y;
9468 /* NLINES is the distance in number of lines. */
9469 nlines = it2.vpos - it->vpos;
9470
9471 /* Correct IT's y and vpos position
9472 so that they are relative to the starting point. */
9473 it->vpos -= nlines;
9474 it->current_y -= h;
9475
9476 if (dy == 0)
9477 {
9478 /* DY == 0 means move to the start of the screen line. The
9479 value of nlines is > 0 if continuation lines were involved,
9480 or if the original IT position was at start of a line. */
9481 RESTORE_IT (it, it, it2data);
9482 if (nlines > 0)
9483 move_it_by_lines (it, nlines);
9484 /* The above code moves us to some position NLINES down,
9485 usually to its first glyph (leftmost in an L2R line), but
9486 that's not necessarily the start of the line, under bidi
9487 reordering. We want to get to the character position
9488 that is immediately after the newline of the previous
9489 line. */
9490 if (it->bidi_p
9491 && !it->continuation_lines_width
9492 && !STRINGP (it->string)
9493 && IT_CHARPOS (*it) > BEGV
9494 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9495 {
9496 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9497
9498 DEC_BOTH (cp, bp);
9499 cp = find_newline_no_quit (cp, bp, -1, NULL);
9500 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9501 }
9502 bidi_unshelve_cache (it3data, 1);
9503 }
9504 else
9505 {
9506 /* The y-position we try to reach, relative to *IT.
9507 Note that H has been subtracted in front of the if-statement. */
9508 int target_y = it->current_y + h - dy;
9509 int y0 = it3.current_y;
9510 int y1;
9511 int line_height;
9512
9513 RESTORE_IT (&it3, &it3, it3data);
9514 y1 = line_bottom_y (&it3);
9515 line_height = y1 - y0;
9516 RESTORE_IT (it, it, it2data);
9517 /* If we did not reach target_y, try to move further backward if
9518 we can. If we moved too far backward, try to move forward. */
9519 if (target_y < it->current_y
9520 /* This is heuristic. In a window that's 3 lines high, with
9521 a line height of 13 pixels each, recentering with point
9522 on the bottom line will try to move -39/2 = 19 pixels
9523 backward. Try to avoid moving into the first line. */
9524 && (it->current_y - target_y
9525 > min (window_box_height (it->w), line_height * 2 / 3))
9526 && IT_CHARPOS (*it) > BEGV)
9527 {
9528 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9529 target_y - it->current_y));
9530 dy = it->current_y - target_y;
9531 goto move_further_back;
9532 }
9533 else if (target_y >= it->current_y + line_height
9534 && IT_CHARPOS (*it) < ZV)
9535 {
9536 /* Should move forward by at least one line, maybe more.
9537
9538 Note: Calling move_it_by_lines can be expensive on
9539 terminal frames, where compute_motion is used (via
9540 vmotion) to do the job, when there are very long lines
9541 and truncate-lines is nil. That's the reason for
9542 treating terminal frames specially here. */
9543
9544 if (!FRAME_WINDOW_P (it->f))
9545 move_it_vertically (it, target_y - (it->current_y + line_height));
9546 else
9547 {
9548 do
9549 {
9550 move_it_by_lines (it, 1);
9551 }
9552 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9553 }
9554 }
9555 }
9556 }
9557
9558
9559 /* Move IT by a specified amount of pixel lines DY. DY negative means
9560 move backwards. DY = 0 means move to start of screen line. At the
9561 end, IT will be on the start of a screen line. */
9562
9563 void
9564 move_it_vertically (struct it *it, int dy)
9565 {
9566 if (dy <= 0)
9567 move_it_vertically_backward (it, -dy);
9568 else
9569 {
9570 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9571 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9572 MOVE_TO_POS | MOVE_TO_Y);
9573 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9574
9575 /* If buffer ends in ZV without a newline, move to the start of
9576 the line to satisfy the post-condition. */
9577 if (IT_CHARPOS (*it) == ZV
9578 && ZV > BEGV
9579 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9580 move_it_by_lines (it, 0);
9581 }
9582 }
9583
9584
9585 /* Move iterator IT past the end of the text line it is in. */
9586
9587 void
9588 move_it_past_eol (struct it *it)
9589 {
9590 enum move_it_result rc;
9591
9592 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9593 if (rc == MOVE_NEWLINE_OR_CR)
9594 set_iterator_to_next (it, 0);
9595 }
9596
9597
9598 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9599 negative means move up. DVPOS == 0 means move to the start of the
9600 screen line.
9601
9602 Optimization idea: If we would know that IT->f doesn't use
9603 a face with proportional font, we could be faster for
9604 truncate-lines nil. */
9605
9606 void
9607 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9608 {
9609
9610 /* The commented-out optimization uses vmotion on terminals. This
9611 gives bad results, because elements like it->what, on which
9612 callers such as pos_visible_p rely, aren't updated. */
9613 /* struct position pos;
9614 if (!FRAME_WINDOW_P (it->f))
9615 {
9616 struct text_pos textpos;
9617
9618 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9619 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9620 reseat (it, textpos, 1);
9621 it->vpos += pos.vpos;
9622 it->current_y += pos.vpos;
9623 }
9624 else */
9625
9626 if (dvpos == 0)
9627 {
9628 /* DVPOS == 0 means move to the start of the screen line. */
9629 move_it_vertically_backward (it, 0);
9630 /* Let next call to line_bottom_y calculate real line height. */
9631 last_height = 0;
9632 }
9633 else if (dvpos > 0)
9634 {
9635 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9636 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9637 {
9638 /* Only move to the next buffer position if we ended up in a
9639 string from display property, not in an overlay string
9640 (before-string or after-string). That is because the
9641 latter don't conceal the underlying buffer position, so
9642 we can ask to move the iterator to the exact position we
9643 are interested in. Note that, even if we are already at
9644 IT_CHARPOS (*it), the call below is not a no-op, as it
9645 will detect that we are at the end of the string, pop the
9646 iterator, and compute it->current_x and it->hpos
9647 correctly. */
9648 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9649 -1, -1, -1, MOVE_TO_POS);
9650 }
9651 }
9652 else
9653 {
9654 struct it it2;
9655 void *it2data = NULL;
9656 ptrdiff_t start_charpos, i;
9657 int nchars_per_row
9658 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9659 bool hit_pos_limit = false;
9660 ptrdiff_t pos_limit;
9661
9662 /* Start at the beginning of the screen line containing IT's
9663 position. This may actually move vertically backwards,
9664 in case of overlays, so adjust dvpos accordingly. */
9665 dvpos += it->vpos;
9666 move_it_vertically_backward (it, 0);
9667 dvpos -= it->vpos;
9668
9669 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9670 screen lines, and reseat the iterator there. */
9671 start_charpos = IT_CHARPOS (*it);
9672 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9673 pos_limit = BEGV;
9674 else
9675 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9676
9677 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9678 back_to_previous_visible_line_start (it);
9679 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9680 hit_pos_limit = true;
9681 reseat (it, it->current.pos, 1);
9682
9683 /* Move further back if we end up in a string or an image. */
9684 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9685 {
9686 /* First try to move to start of display line. */
9687 dvpos += it->vpos;
9688 move_it_vertically_backward (it, 0);
9689 dvpos -= it->vpos;
9690 if (IT_POS_VALID_AFTER_MOVE_P (it))
9691 break;
9692 /* If start of line is still in string or image,
9693 move further back. */
9694 back_to_previous_visible_line_start (it);
9695 reseat (it, it->current.pos, 1);
9696 dvpos--;
9697 }
9698
9699 it->current_x = it->hpos = 0;
9700
9701 /* Above call may have moved too far if continuation lines
9702 are involved. Scan forward and see if it did. */
9703 SAVE_IT (it2, *it, it2data);
9704 it2.vpos = it2.current_y = 0;
9705 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9706 it->vpos -= it2.vpos;
9707 it->current_y -= it2.current_y;
9708 it->current_x = it->hpos = 0;
9709
9710 /* If we moved too far back, move IT some lines forward. */
9711 if (it2.vpos > -dvpos)
9712 {
9713 int delta = it2.vpos + dvpos;
9714
9715 RESTORE_IT (&it2, &it2, it2data);
9716 SAVE_IT (it2, *it, it2data);
9717 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9718 /* Move back again if we got too far ahead. */
9719 if (IT_CHARPOS (*it) >= start_charpos)
9720 RESTORE_IT (it, &it2, it2data);
9721 else
9722 bidi_unshelve_cache (it2data, 1);
9723 }
9724 else if (hit_pos_limit && pos_limit > BEGV
9725 && dvpos < 0 && it2.vpos < -dvpos)
9726 {
9727 /* If we hit the limit, but still didn't make it far enough
9728 back, that means there's a display string with a newline
9729 covering a large chunk of text, and that caused
9730 back_to_previous_visible_line_start try to go too far.
9731 Punish those who commit such atrocities by going back
9732 until we've reached DVPOS, after lifting the limit, which
9733 could make it slow for very long lines. "If it hurts,
9734 don't do that!" */
9735 dvpos += it2.vpos;
9736 RESTORE_IT (it, it, it2data);
9737 for (i = -dvpos; i > 0; --i)
9738 {
9739 back_to_previous_visible_line_start (it);
9740 it->vpos--;
9741 }
9742 }
9743 else
9744 RESTORE_IT (it, it, it2data);
9745 }
9746 }
9747
9748 /* Return true if IT points into the middle of a display vector. */
9749
9750 bool
9751 in_display_vector_p (struct it *it)
9752 {
9753 return (it->method == GET_FROM_DISPLAY_VECTOR
9754 && it->current.dpvec_index > 0
9755 && it->dpvec + it->current.dpvec_index != it->dpend);
9756 }
9757
9758 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9759 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9760 WINDOW must be a live window and defaults to the selected one. The
9761 return value is a cons of the maximum pixel-width of any text line and
9762 the maximum pixel-height of all text lines.
9763
9764 The optional argument FROM, if non-nil, specifies the first text
9765 position and defaults to the minimum accessible position of the buffer.
9766 If FROM is t, use the minimum accessible position that is not a newline
9767 character. TO, if non-nil, specifies the last text position and
9768 defaults to the maximum accessible position of the buffer. If TO is t,
9769 use the maximum accessible position that is not a newline character.
9770
9771 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9772 width that can be returned. X-LIMIT nil or omitted, means to use the
9773 pixel-width of WINDOW's body; use this if you do not intend to change
9774 the width of WINDOW. Use the maximum width WINDOW may assume if you
9775 intend to change WINDOW's width. In any case, text whose x-coordinate
9776 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9777 can take some time, it's always a good idea to make this argument as
9778 small as possible; in particular, if the buffer contains long lines that
9779 shall be truncated anyway.
9780
9781 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9782 height that can be returned. Text lines whose y-coordinate is beyond
9783 Y-LIMIT are ignored. Since calculating the text height of a large
9784 buffer can take some time, it makes sense to specify this argument if
9785 the size of the buffer is unknown.
9786
9787 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9788 include the height of the mode- or header-line of WINDOW in the return
9789 value. If it is either the symbol `mode-line' or `header-line', include
9790 only the height of that line, if present, in the return value. If t,
9791 include the height of both, if present, in the return value. */)
9792 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9793 Lisp_Object mode_and_header_line)
9794 {
9795 struct window *w = decode_live_window (window);
9796 Lisp_Object buf;
9797 struct buffer *b;
9798 struct it it;
9799 struct buffer *old_buffer = NULL;
9800 ptrdiff_t start, end, pos;
9801 struct text_pos startp;
9802 void *itdata = NULL;
9803 int c, max_y = -1, x = 0, y = 0;
9804
9805 buf = w->contents;
9806 CHECK_BUFFER (buf);
9807 b = XBUFFER (buf);
9808
9809 if (b != current_buffer)
9810 {
9811 old_buffer = current_buffer;
9812 set_buffer_internal (b);
9813 }
9814
9815 if (NILP (from))
9816 start = BEGV;
9817 else if (EQ (from, Qt))
9818 {
9819 start = pos = BEGV;
9820 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9821 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9822 start = pos;
9823 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9824 start = pos;
9825 }
9826 else
9827 {
9828 CHECK_NUMBER_COERCE_MARKER (from);
9829 start = min (max (XINT (from), BEGV), ZV);
9830 }
9831
9832 if (NILP (to))
9833 end = ZV;
9834 else if (EQ (to, Qt))
9835 {
9836 end = pos = ZV;
9837 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9838 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9839 end = pos;
9840 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9841 end = pos;
9842 }
9843 else
9844 {
9845 CHECK_NUMBER_COERCE_MARKER (to);
9846 end = max (start, min (XINT (to), ZV));
9847 }
9848
9849 if (!NILP (y_limit))
9850 {
9851 CHECK_NUMBER (y_limit);
9852 max_y = min (XINT (y_limit), INT_MAX);
9853 }
9854
9855 itdata = bidi_shelve_cache ();
9856 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9857 start_display (&it, w, startp);
9858
9859 if (NILP (x_limit))
9860 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9861 else
9862 {
9863 CHECK_NUMBER (x_limit);
9864 it.last_visible_x = min (XINT (x_limit), INFINITY);
9865 /* Actually, we never want move_it_to stop at to_x. But to make
9866 sure that move_it_in_display_line_to always moves far enough,
9867 we set it to INT_MAX and specify MOVE_TO_X. */
9868 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9869 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9870 }
9871
9872 y = it.current_y + it.max_ascent + it.max_descent;
9873
9874 if (!EQ (mode_and_header_line, Qheader_line)
9875 && !EQ (mode_and_header_line, Qt))
9876 /* Do not count the header-line which was counted automatically by
9877 start_display. */
9878 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9879
9880 if (EQ (mode_and_header_line, Qmode_line)
9881 || EQ (mode_and_header_line, Qt))
9882 /* Do count the mode-line which is not included automatically by
9883 start_display. */
9884 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9885
9886 bidi_unshelve_cache (itdata, 0);
9887
9888 if (old_buffer)
9889 set_buffer_internal (old_buffer);
9890
9891 return Fcons (make_number (x), make_number (y));
9892 }
9893 \f
9894 /***********************************************************************
9895 Messages
9896 ***********************************************************************/
9897
9898
9899 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9900 to *Messages*. */
9901
9902 void
9903 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9904 {
9905 Lisp_Object args[3];
9906 Lisp_Object msg, fmt;
9907 char *buffer;
9908 ptrdiff_t len;
9909 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9910 USE_SAFE_ALLOCA;
9911
9912 fmt = msg = Qnil;
9913 GCPRO4 (fmt, msg, arg1, arg2);
9914
9915 args[0] = fmt = build_string (format);
9916 args[1] = arg1;
9917 args[2] = arg2;
9918 msg = Fformat (3, args);
9919
9920 len = SBYTES (msg) + 1;
9921 buffer = SAFE_ALLOCA (len);
9922 memcpy (buffer, SDATA (msg), len);
9923
9924 message_dolog (buffer, len - 1, 1, 0);
9925 SAFE_FREE ();
9926
9927 UNGCPRO;
9928 }
9929
9930
9931 /* Output a newline in the *Messages* buffer if "needs" one. */
9932
9933 void
9934 message_log_maybe_newline (void)
9935 {
9936 if (message_log_need_newline)
9937 message_dolog ("", 0, 1, 0);
9938 }
9939
9940
9941 /* Add a string M of length NBYTES to the message log, optionally
9942 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9943 true, means interpret the contents of M as multibyte. This
9944 function calls low-level routines in order to bypass text property
9945 hooks, etc. which might not be safe to run.
9946
9947 This may GC (insert may run before/after change hooks),
9948 so the buffer M must NOT point to a Lisp string. */
9949
9950 void
9951 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9952 {
9953 const unsigned char *msg = (const unsigned char *) m;
9954
9955 if (!NILP (Vmemory_full))
9956 return;
9957
9958 if (!NILP (Vmessage_log_max))
9959 {
9960 struct buffer *oldbuf;
9961 Lisp_Object oldpoint, oldbegv, oldzv;
9962 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9963 ptrdiff_t point_at_end = 0;
9964 ptrdiff_t zv_at_end = 0;
9965 Lisp_Object old_deactivate_mark;
9966 struct gcpro gcpro1;
9967
9968 old_deactivate_mark = Vdeactivate_mark;
9969 oldbuf = current_buffer;
9970
9971 /* Ensure the Messages buffer exists, and switch to it.
9972 If we created it, set the major-mode. */
9973 {
9974 int newbuffer = 0;
9975 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9976
9977 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9978
9979 if (newbuffer
9980 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9981 call0 (intern ("messages-buffer-mode"));
9982 }
9983
9984 bset_undo_list (current_buffer, Qt);
9985 bset_cache_long_scans (current_buffer, Qnil);
9986
9987 oldpoint = message_dolog_marker1;
9988 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9989 oldbegv = message_dolog_marker2;
9990 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9991 oldzv = message_dolog_marker3;
9992 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9993 GCPRO1 (old_deactivate_mark);
9994
9995 if (PT == Z)
9996 point_at_end = 1;
9997 if (ZV == Z)
9998 zv_at_end = 1;
9999
10000 BEGV = BEG;
10001 BEGV_BYTE = BEG_BYTE;
10002 ZV = Z;
10003 ZV_BYTE = Z_BYTE;
10004 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10005
10006 /* Insert the string--maybe converting multibyte to single byte
10007 or vice versa, so that all the text fits the buffer. */
10008 if (multibyte
10009 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10010 {
10011 ptrdiff_t i;
10012 int c, char_bytes;
10013 char work[1];
10014
10015 /* Convert a multibyte string to single-byte
10016 for the *Message* buffer. */
10017 for (i = 0; i < nbytes; i += char_bytes)
10018 {
10019 c = string_char_and_length (msg + i, &char_bytes);
10020 work[0] = (ASCII_CHAR_P (c)
10021 ? c
10022 : multibyte_char_to_unibyte (c));
10023 insert_1_both (work, 1, 1, 1, 0, 0);
10024 }
10025 }
10026 else if (! multibyte
10027 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10028 {
10029 ptrdiff_t i;
10030 int c, char_bytes;
10031 unsigned char str[MAX_MULTIBYTE_LENGTH];
10032 /* Convert a single-byte string to multibyte
10033 for the *Message* buffer. */
10034 for (i = 0; i < nbytes; i++)
10035 {
10036 c = msg[i];
10037 MAKE_CHAR_MULTIBYTE (c);
10038 char_bytes = CHAR_STRING (c, str);
10039 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
10040 }
10041 }
10042 else if (nbytes)
10043 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
10044
10045 if (nlflag)
10046 {
10047 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10048 printmax_t dups;
10049
10050 insert_1_both ("\n", 1, 1, 1, 0, 0);
10051
10052 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
10053 this_bol = PT;
10054 this_bol_byte = PT_BYTE;
10055
10056 /* See if this line duplicates the previous one.
10057 If so, combine duplicates. */
10058 if (this_bol > BEG)
10059 {
10060 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
10061 prev_bol = PT;
10062 prev_bol_byte = PT_BYTE;
10063
10064 dups = message_log_check_duplicate (prev_bol_byte,
10065 this_bol_byte);
10066 if (dups)
10067 {
10068 del_range_both (prev_bol, prev_bol_byte,
10069 this_bol, this_bol_byte, 0);
10070 if (dups > 1)
10071 {
10072 char dupstr[sizeof " [ times]"
10073 + INT_STRLEN_BOUND (printmax_t)];
10074
10075 /* If you change this format, don't forget to also
10076 change message_log_check_duplicate. */
10077 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10078 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10079 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
10080 }
10081 }
10082 }
10083
10084 /* If we have more than the desired maximum number of lines
10085 in the *Messages* buffer now, delete the oldest ones.
10086 This is safe because we don't have undo in this buffer. */
10087
10088 if (NATNUMP (Vmessage_log_max))
10089 {
10090 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10091 -XFASTINT (Vmessage_log_max) - 1, 0);
10092 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
10093 }
10094 }
10095 BEGV = marker_position (oldbegv);
10096 BEGV_BYTE = marker_byte_position (oldbegv);
10097
10098 if (zv_at_end)
10099 {
10100 ZV = Z;
10101 ZV_BYTE = Z_BYTE;
10102 }
10103 else
10104 {
10105 ZV = marker_position (oldzv);
10106 ZV_BYTE = marker_byte_position (oldzv);
10107 }
10108
10109 if (point_at_end)
10110 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10111 else
10112 /* We can't do Fgoto_char (oldpoint) because it will run some
10113 Lisp code. */
10114 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10115 marker_byte_position (oldpoint));
10116
10117 UNGCPRO;
10118 unchain_marker (XMARKER (oldpoint));
10119 unchain_marker (XMARKER (oldbegv));
10120 unchain_marker (XMARKER (oldzv));
10121
10122 /* We called insert_1_both above with its 5th argument (PREPARE)
10123 zero, which prevents insert_1_both from calling
10124 prepare_to_modify_buffer, which in turns prevents us from
10125 incrementing windows_or_buffers_changed even if *Messages* is
10126 shown in some window. So we must manually set
10127 windows_or_buffers_changed here to make up for that. */
10128 windows_or_buffers_changed = old_windows_or_buffers_changed;
10129 bset_redisplay (current_buffer);
10130
10131 set_buffer_internal (oldbuf);
10132
10133 message_log_need_newline = !nlflag;
10134 Vdeactivate_mark = old_deactivate_mark;
10135 }
10136 }
10137
10138
10139 /* We are at the end of the buffer after just having inserted a newline.
10140 (Note: We depend on the fact we won't be crossing the gap.)
10141 Check to see if the most recent message looks a lot like the previous one.
10142 Return 0 if different, 1 if the new one should just replace it, or a
10143 value N > 1 if we should also append " [N times]". */
10144
10145 static intmax_t
10146 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10147 {
10148 ptrdiff_t i;
10149 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10150 int seen_dots = 0;
10151 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10152 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10153
10154 for (i = 0; i < len; i++)
10155 {
10156 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10157 seen_dots = 1;
10158 if (p1[i] != p2[i])
10159 return seen_dots;
10160 }
10161 p1 += len;
10162 if (*p1 == '\n')
10163 return 2;
10164 if (*p1++ == ' ' && *p1++ == '[')
10165 {
10166 char *pend;
10167 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10168 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10169 return n + 1;
10170 }
10171 return 0;
10172 }
10173 \f
10174
10175 /* Display an echo area message M with a specified length of NBYTES
10176 bytes. The string may include null characters. If M is not a
10177 string, clear out any existing message, and let the mini-buffer
10178 text show through.
10179
10180 This function cancels echoing. */
10181
10182 void
10183 message3 (Lisp_Object m)
10184 {
10185 struct gcpro gcpro1;
10186
10187 GCPRO1 (m);
10188 clear_message (true, true);
10189 cancel_echoing ();
10190
10191 /* First flush out any partial line written with print. */
10192 message_log_maybe_newline ();
10193 if (STRINGP (m))
10194 {
10195 ptrdiff_t nbytes = SBYTES (m);
10196 bool multibyte = STRING_MULTIBYTE (m);
10197 USE_SAFE_ALLOCA;
10198 char *buffer = SAFE_ALLOCA (nbytes);
10199 memcpy (buffer, SDATA (m), nbytes);
10200 message_dolog (buffer, nbytes, 1, multibyte);
10201 SAFE_FREE ();
10202 }
10203 message3_nolog (m);
10204
10205 UNGCPRO;
10206 }
10207
10208
10209 /* The non-logging version of message3.
10210 This does not cancel echoing, because it is used for echoing.
10211 Perhaps we need to make a separate function for echoing
10212 and make this cancel echoing. */
10213
10214 void
10215 message3_nolog (Lisp_Object m)
10216 {
10217 struct frame *sf = SELECTED_FRAME ();
10218
10219 if (FRAME_INITIAL_P (sf))
10220 {
10221 if (noninteractive_need_newline)
10222 putc ('\n', stderr);
10223 noninteractive_need_newline = 0;
10224 if (STRINGP (m))
10225 {
10226 Lisp_Object s = ENCODE_SYSTEM (m);
10227
10228 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10229 }
10230 if (cursor_in_echo_area == 0)
10231 fprintf (stderr, "\n");
10232 fflush (stderr);
10233 }
10234 /* Error messages get reported properly by cmd_error, so this must be just an
10235 informative message; if the frame hasn't really been initialized yet, just
10236 toss it. */
10237 else if (INTERACTIVE && sf->glyphs_initialized_p)
10238 {
10239 /* Get the frame containing the mini-buffer
10240 that the selected frame is using. */
10241 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10242 Lisp_Object frame = XWINDOW (mini_window)->frame;
10243 struct frame *f = XFRAME (frame);
10244
10245 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10246 Fmake_frame_visible (frame);
10247
10248 if (STRINGP (m) && SCHARS (m) > 0)
10249 {
10250 set_message (m);
10251 if (minibuffer_auto_raise)
10252 Fraise_frame (frame);
10253 /* Assume we are not echoing.
10254 (If we are, echo_now will override this.) */
10255 echo_message_buffer = Qnil;
10256 }
10257 else
10258 clear_message (true, true);
10259
10260 do_pending_window_change (0);
10261 echo_area_display (1);
10262 do_pending_window_change (0);
10263 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10264 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10265 }
10266 }
10267
10268
10269 /* Display a null-terminated echo area message M. If M is 0, clear
10270 out any existing message, and let the mini-buffer text show through.
10271
10272 The buffer M must continue to exist until after the echo area gets
10273 cleared or some other message gets displayed there. Do not pass
10274 text that is stored in a Lisp string. Do not pass text in a buffer
10275 that was alloca'd. */
10276
10277 void
10278 message1 (const char *m)
10279 {
10280 message3 (m ? build_unibyte_string (m) : Qnil);
10281 }
10282
10283
10284 /* The non-logging counterpart of message1. */
10285
10286 void
10287 message1_nolog (const char *m)
10288 {
10289 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10290 }
10291
10292 /* Display a message M which contains a single %s
10293 which gets replaced with STRING. */
10294
10295 void
10296 message_with_string (const char *m, Lisp_Object string, int log)
10297 {
10298 CHECK_STRING (string);
10299
10300 if (noninteractive)
10301 {
10302 if (m)
10303 {
10304 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10305 String whose data pointer might be passed to us in M. So
10306 we use a local copy. */
10307 char *fmt = xstrdup (m);
10308
10309 if (noninteractive_need_newline)
10310 putc ('\n', stderr);
10311 noninteractive_need_newline = 0;
10312 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10313 if (!cursor_in_echo_area)
10314 fprintf (stderr, "\n");
10315 fflush (stderr);
10316 xfree (fmt);
10317 }
10318 }
10319 else if (INTERACTIVE)
10320 {
10321 /* The frame whose minibuffer we're going to display the message on.
10322 It may be larger than the selected frame, so we need
10323 to use its buffer, not the selected frame's buffer. */
10324 Lisp_Object mini_window;
10325 struct frame *f, *sf = SELECTED_FRAME ();
10326
10327 /* Get the frame containing the minibuffer
10328 that the selected frame is using. */
10329 mini_window = FRAME_MINIBUF_WINDOW (sf);
10330 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10331
10332 /* Error messages get reported properly by cmd_error, so this must be
10333 just an informative message; if the frame hasn't really been
10334 initialized yet, just toss it. */
10335 if (f->glyphs_initialized_p)
10336 {
10337 Lisp_Object args[2], msg;
10338 struct gcpro gcpro1, gcpro2;
10339
10340 args[0] = build_string (m);
10341 args[1] = msg = string;
10342 GCPRO2 (args[0], msg);
10343 gcpro1.nvars = 2;
10344
10345 msg = Fformat (2, args);
10346
10347 if (log)
10348 message3 (msg);
10349 else
10350 message3_nolog (msg);
10351
10352 UNGCPRO;
10353
10354 /* Print should start at the beginning of the message
10355 buffer next time. */
10356 message_buf_print = 0;
10357 }
10358 }
10359 }
10360
10361
10362 /* Dump an informative message to the minibuf. If M is 0, clear out
10363 any existing message, and let the mini-buffer text show through. */
10364
10365 static void
10366 vmessage (const char *m, va_list ap)
10367 {
10368 if (noninteractive)
10369 {
10370 if (m)
10371 {
10372 if (noninteractive_need_newline)
10373 putc ('\n', stderr);
10374 noninteractive_need_newline = 0;
10375 vfprintf (stderr, m, ap);
10376 if (cursor_in_echo_area == 0)
10377 fprintf (stderr, "\n");
10378 fflush (stderr);
10379 }
10380 }
10381 else if (INTERACTIVE)
10382 {
10383 /* The frame whose mini-buffer we're going to display the message
10384 on. It may be larger than the selected frame, so we need to
10385 use its buffer, not the selected frame's buffer. */
10386 Lisp_Object mini_window;
10387 struct frame *f, *sf = SELECTED_FRAME ();
10388
10389 /* Get the frame containing the mini-buffer
10390 that the selected frame is using. */
10391 mini_window = FRAME_MINIBUF_WINDOW (sf);
10392 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10393
10394 /* Error messages get reported properly by cmd_error, so this must be
10395 just an informative message; if the frame hasn't really been
10396 initialized yet, just toss it. */
10397 if (f->glyphs_initialized_p)
10398 {
10399 if (m)
10400 {
10401 ptrdiff_t len;
10402 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10403 char *message_buf = alloca (maxsize + 1);
10404
10405 len = doprnt (message_buf, maxsize, m, 0, ap);
10406
10407 message3 (make_string (message_buf, len));
10408 }
10409 else
10410 message1 (0);
10411
10412 /* Print should start at the beginning of the message
10413 buffer next time. */
10414 message_buf_print = 0;
10415 }
10416 }
10417 }
10418
10419 void
10420 message (const char *m, ...)
10421 {
10422 va_list ap;
10423 va_start (ap, m);
10424 vmessage (m, ap);
10425 va_end (ap);
10426 }
10427
10428
10429 #if 0
10430 /* The non-logging version of message. */
10431
10432 void
10433 message_nolog (const char *m, ...)
10434 {
10435 Lisp_Object old_log_max;
10436 va_list ap;
10437 va_start (ap, m);
10438 old_log_max = Vmessage_log_max;
10439 Vmessage_log_max = Qnil;
10440 vmessage (m, ap);
10441 Vmessage_log_max = old_log_max;
10442 va_end (ap);
10443 }
10444 #endif
10445
10446
10447 /* Display the current message in the current mini-buffer. This is
10448 only called from error handlers in process.c, and is not time
10449 critical. */
10450
10451 void
10452 update_echo_area (void)
10453 {
10454 if (!NILP (echo_area_buffer[0]))
10455 {
10456 Lisp_Object string;
10457 string = Fcurrent_message ();
10458 message3 (string);
10459 }
10460 }
10461
10462
10463 /* Make sure echo area buffers in `echo_buffers' are live.
10464 If they aren't, make new ones. */
10465
10466 static void
10467 ensure_echo_area_buffers (void)
10468 {
10469 int i;
10470
10471 for (i = 0; i < 2; ++i)
10472 if (!BUFFERP (echo_buffer[i])
10473 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10474 {
10475 char name[30];
10476 Lisp_Object old_buffer;
10477 int j;
10478
10479 old_buffer = echo_buffer[i];
10480 echo_buffer[i] = Fget_buffer_create
10481 (make_formatted_string (name, " *Echo Area %d*", i));
10482 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10483 /* to force word wrap in echo area -
10484 it was decided to postpone this*/
10485 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10486
10487 for (j = 0; j < 2; ++j)
10488 if (EQ (old_buffer, echo_area_buffer[j]))
10489 echo_area_buffer[j] = echo_buffer[i];
10490 }
10491 }
10492
10493
10494 /* Call FN with args A1..A2 with either the current or last displayed
10495 echo_area_buffer as current buffer.
10496
10497 WHICH zero means use the current message buffer
10498 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10499 from echo_buffer[] and clear it.
10500
10501 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10502 suitable buffer from echo_buffer[] and clear it.
10503
10504 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10505 that the current message becomes the last displayed one, make
10506 choose a suitable buffer for echo_area_buffer[0], and clear it.
10507
10508 Value is what FN returns. */
10509
10510 static int
10511 with_echo_area_buffer (struct window *w, int which,
10512 int (*fn) (ptrdiff_t, Lisp_Object),
10513 ptrdiff_t a1, Lisp_Object a2)
10514 {
10515 Lisp_Object buffer;
10516 int this_one, the_other, clear_buffer_p, rc;
10517 ptrdiff_t count = SPECPDL_INDEX ();
10518
10519 /* If buffers aren't live, make new ones. */
10520 ensure_echo_area_buffers ();
10521
10522 clear_buffer_p = 0;
10523
10524 if (which == 0)
10525 this_one = 0, the_other = 1;
10526 else if (which > 0)
10527 this_one = 1, the_other = 0;
10528 else
10529 {
10530 this_one = 0, the_other = 1;
10531 clear_buffer_p = true;
10532
10533 /* We need a fresh one in case the current echo buffer equals
10534 the one containing the last displayed echo area message. */
10535 if (!NILP (echo_area_buffer[this_one])
10536 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10537 echo_area_buffer[this_one] = Qnil;
10538 }
10539
10540 /* Choose a suitable buffer from echo_buffer[] is we don't
10541 have one. */
10542 if (NILP (echo_area_buffer[this_one]))
10543 {
10544 echo_area_buffer[this_one]
10545 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10546 ? echo_buffer[the_other]
10547 : echo_buffer[this_one]);
10548 clear_buffer_p = true;
10549 }
10550
10551 buffer = echo_area_buffer[this_one];
10552
10553 /* Don't get confused by reusing the buffer used for echoing
10554 for a different purpose. */
10555 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10556 cancel_echoing ();
10557
10558 record_unwind_protect (unwind_with_echo_area_buffer,
10559 with_echo_area_buffer_unwind_data (w));
10560
10561 /* Make the echo area buffer current. Note that for display
10562 purposes, it is not necessary that the displayed window's buffer
10563 == current_buffer, except for text property lookup. So, let's
10564 only set that buffer temporarily here without doing a full
10565 Fset_window_buffer. We must also change w->pointm, though,
10566 because otherwise an assertions in unshow_buffer fails, and Emacs
10567 aborts. */
10568 set_buffer_internal_1 (XBUFFER (buffer));
10569 if (w)
10570 {
10571 wset_buffer (w, buffer);
10572 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10573 }
10574
10575 bset_undo_list (current_buffer, Qt);
10576 bset_read_only (current_buffer, Qnil);
10577 specbind (Qinhibit_read_only, Qt);
10578 specbind (Qinhibit_modification_hooks, Qt);
10579
10580 if (clear_buffer_p && Z > BEG)
10581 del_range (BEG, Z);
10582
10583 eassert (BEGV >= BEG);
10584 eassert (ZV <= Z && ZV >= BEGV);
10585
10586 rc = fn (a1, a2);
10587
10588 eassert (BEGV >= BEG);
10589 eassert (ZV <= Z && ZV >= BEGV);
10590
10591 unbind_to (count, Qnil);
10592 return rc;
10593 }
10594
10595
10596 /* Save state that should be preserved around the call to the function
10597 FN called in with_echo_area_buffer. */
10598
10599 static Lisp_Object
10600 with_echo_area_buffer_unwind_data (struct window *w)
10601 {
10602 int i = 0;
10603 Lisp_Object vector, tmp;
10604
10605 /* Reduce consing by keeping one vector in
10606 Vwith_echo_area_save_vector. */
10607 vector = Vwith_echo_area_save_vector;
10608 Vwith_echo_area_save_vector = Qnil;
10609
10610 if (NILP (vector))
10611 vector = Fmake_vector (make_number (9), Qnil);
10612
10613 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10614 ASET (vector, i, Vdeactivate_mark); ++i;
10615 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10616
10617 if (w)
10618 {
10619 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10620 ASET (vector, i, w->contents); ++i;
10621 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10622 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10623 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10624 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10625 }
10626 else
10627 {
10628 int end = i + 6;
10629 for (; i < end; ++i)
10630 ASET (vector, i, Qnil);
10631 }
10632
10633 eassert (i == ASIZE (vector));
10634 return vector;
10635 }
10636
10637
10638 /* Restore global state from VECTOR which was created by
10639 with_echo_area_buffer_unwind_data. */
10640
10641 static void
10642 unwind_with_echo_area_buffer (Lisp_Object vector)
10643 {
10644 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10645 Vdeactivate_mark = AREF (vector, 1);
10646 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10647
10648 if (WINDOWP (AREF (vector, 3)))
10649 {
10650 struct window *w;
10651 Lisp_Object buffer;
10652
10653 w = XWINDOW (AREF (vector, 3));
10654 buffer = AREF (vector, 4);
10655
10656 wset_buffer (w, buffer);
10657 set_marker_both (w->pointm, buffer,
10658 XFASTINT (AREF (vector, 5)),
10659 XFASTINT (AREF (vector, 6)));
10660 set_marker_both (w->start, buffer,
10661 XFASTINT (AREF (vector, 7)),
10662 XFASTINT (AREF (vector, 8)));
10663 }
10664
10665 Vwith_echo_area_save_vector = vector;
10666 }
10667
10668
10669 /* Set up the echo area for use by print functions. MULTIBYTE_P
10670 non-zero means we will print multibyte. */
10671
10672 void
10673 setup_echo_area_for_printing (int multibyte_p)
10674 {
10675 /* If we can't find an echo area any more, exit. */
10676 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10677 Fkill_emacs (Qnil);
10678
10679 ensure_echo_area_buffers ();
10680
10681 if (!message_buf_print)
10682 {
10683 /* A message has been output since the last time we printed.
10684 Choose a fresh echo area buffer. */
10685 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10686 echo_area_buffer[0] = echo_buffer[1];
10687 else
10688 echo_area_buffer[0] = echo_buffer[0];
10689
10690 /* Switch to that buffer and clear it. */
10691 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10692 bset_truncate_lines (current_buffer, Qnil);
10693
10694 if (Z > BEG)
10695 {
10696 ptrdiff_t count = SPECPDL_INDEX ();
10697 specbind (Qinhibit_read_only, Qt);
10698 /* Note that undo recording is always disabled. */
10699 del_range (BEG, Z);
10700 unbind_to (count, Qnil);
10701 }
10702 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10703
10704 /* Set up the buffer for the multibyteness we need. */
10705 if (multibyte_p
10706 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10707 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10708
10709 /* Raise the frame containing the echo area. */
10710 if (minibuffer_auto_raise)
10711 {
10712 struct frame *sf = SELECTED_FRAME ();
10713 Lisp_Object mini_window;
10714 mini_window = FRAME_MINIBUF_WINDOW (sf);
10715 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10716 }
10717
10718 message_log_maybe_newline ();
10719 message_buf_print = 1;
10720 }
10721 else
10722 {
10723 if (NILP (echo_area_buffer[0]))
10724 {
10725 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10726 echo_area_buffer[0] = echo_buffer[1];
10727 else
10728 echo_area_buffer[0] = echo_buffer[0];
10729 }
10730
10731 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10732 {
10733 /* Someone switched buffers between print requests. */
10734 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10735 bset_truncate_lines (current_buffer, Qnil);
10736 }
10737 }
10738 }
10739
10740
10741 /* Display an echo area message in window W. Value is non-zero if W's
10742 height is changed. If display_last_displayed_message_p is
10743 non-zero, display the message that was last displayed, otherwise
10744 display the current message. */
10745
10746 static int
10747 display_echo_area (struct window *w)
10748 {
10749 int i, no_message_p, window_height_changed_p;
10750
10751 /* Temporarily disable garbage collections while displaying the echo
10752 area. This is done because a GC can print a message itself.
10753 That message would modify the echo area buffer's contents while a
10754 redisplay of the buffer is going on, and seriously confuse
10755 redisplay. */
10756 ptrdiff_t count = inhibit_garbage_collection ();
10757
10758 /* If there is no message, we must call display_echo_area_1
10759 nevertheless because it resizes the window. But we will have to
10760 reset the echo_area_buffer in question to nil at the end because
10761 with_echo_area_buffer will sets it to an empty buffer. */
10762 i = display_last_displayed_message_p ? 1 : 0;
10763 no_message_p = NILP (echo_area_buffer[i]);
10764
10765 window_height_changed_p
10766 = with_echo_area_buffer (w, display_last_displayed_message_p,
10767 display_echo_area_1,
10768 (intptr_t) w, Qnil);
10769
10770 if (no_message_p)
10771 echo_area_buffer[i] = Qnil;
10772
10773 unbind_to (count, Qnil);
10774 return window_height_changed_p;
10775 }
10776
10777
10778 /* Helper for display_echo_area. Display the current buffer which
10779 contains the current echo area message in window W, a mini-window,
10780 a pointer to which is passed in A1. A2..A4 are currently not used.
10781 Change the height of W so that all of the message is displayed.
10782 Value is non-zero if height of W was changed. */
10783
10784 static int
10785 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10786 {
10787 intptr_t i1 = a1;
10788 struct window *w = (struct window *) i1;
10789 Lisp_Object window;
10790 struct text_pos start;
10791 int window_height_changed_p = 0;
10792
10793 /* Do this before displaying, so that we have a large enough glyph
10794 matrix for the display. If we can't get enough space for the
10795 whole text, display the last N lines. That works by setting w->start. */
10796 window_height_changed_p = resize_mini_window (w, 0);
10797
10798 /* Use the starting position chosen by resize_mini_window. */
10799 SET_TEXT_POS_FROM_MARKER (start, w->start);
10800
10801 /* Display. */
10802 clear_glyph_matrix (w->desired_matrix);
10803 XSETWINDOW (window, w);
10804 try_window (window, start, 0);
10805
10806 return window_height_changed_p;
10807 }
10808
10809
10810 /* Resize the echo area window to exactly the size needed for the
10811 currently displayed message, if there is one. If a mini-buffer
10812 is active, don't shrink it. */
10813
10814 void
10815 resize_echo_area_exactly (void)
10816 {
10817 if (BUFFERP (echo_area_buffer[0])
10818 && WINDOWP (echo_area_window))
10819 {
10820 struct window *w = XWINDOW (echo_area_window);
10821 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10822 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10823 (intptr_t) w, resize_exactly);
10824 if (resized_p)
10825 {
10826 windows_or_buffers_changed = 42;
10827 update_mode_lines = 30;
10828 redisplay_internal ();
10829 }
10830 }
10831 }
10832
10833
10834 /* Callback function for with_echo_area_buffer, when used from
10835 resize_echo_area_exactly. A1 contains a pointer to the window to
10836 resize, EXACTLY non-nil means resize the mini-window exactly to the
10837 size of the text displayed. A3 and A4 are not used. Value is what
10838 resize_mini_window returns. */
10839
10840 static int
10841 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10842 {
10843 intptr_t i1 = a1;
10844 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10845 }
10846
10847
10848 /* Resize mini-window W to fit the size of its contents. EXACT_P
10849 means size the window exactly to the size needed. Otherwise, it's
10850 only enlarged until W's buffer is empty.
10851
10852 Set W->start to the right place to begin display. If the whole
10853 contents fit, start at the beginning. Otherwise, start so as
10854 to make the end of the contents appear. This is particularly
10855 important for y-or-n-p, but seems desirable generally.
10856
10857 Value is non-zero if the window height has been changed. */
10858
10859 int
10860 resize_mini_window (struct window *w, int exact_p)
10861 {
10862 struct frame *f = XFRAME (w->frame);
10863 int window_height_changed_p = 0;
10864
10865 eassert (MINI_WINDOW_P (w));
10866
10867 /* By default, start display at the beginning. */
10868 set_marker_both (w->start, w->contents,
10869 BUF_BEGV (XBUFFER (w->contents)),
10870 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10871
10872 /* Don't resize windows while redisplaying a window; it would
10873 confuse redisplay functions when the size of the window they are
10874 displaying changes from under them. Such a resizing can happen,
10875 for instance, when which-func prints a long message while
10876 we are running fontification-functions. We're running these
10877 functions with safe_call which binds inhibit-redisplay to t. */
10878 if (!NILP (Vinhibit_redisplay))
10879 return 0;
10880
10881 /* Nil means don't try to resize. */
10882 if (NILP (Vresize_mini_windows)
10883 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10884 return 0;
10885
10886 if (!FRAME_MINIBUF_ONLY_P (f))
10887 {
10888 struct it it;
10889 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10890 + WINDOW_PIXEL_HEIGHT (w));
10891 int unit = FRAME_LINE_HEIGHT (f);
10892 int height, max_height;
10893 struct text_pos start;
10894 struct buffer *old_current_buffer = NULL;
10895
10896 if (current_buffer != XBUFFER (w->contents))
10897 {
10898 old_current_buffer = current_buffer;
10899 set_buffer_internal (XBUFFER (w->contents));
10900 }
10901
10902 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10903
10904 /* Compute the max. number of lines specified by the user. */
10905 if (FLOATP (Vmax_mini_window_height))
10906 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10907 else if (INTEGERP (Vmax_mini_window_height))
10908 max_height = XINT (Vmax_mini_window_height) * unit;
10909 else
10910 max_height = total_height / 4;
10911
10912 /* Correct that max. height if it's bogus. */
10913 max_height = clip_to_bounds (unit, max_height, total_height);
10914
10915 /* Find out the height of the text in the window. */
10916 if (it.line_wrap == TRUNCATE)
10917 height = unit;
10918 else
10919 {
10920 last_height = 0;
10921 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10922 if (it.max_ascent == 0 && it.max_descent == 0)
10923 height = it.current_y + last_height;
10924 else
10925 height = it.current_y + it.max_ascent + it.max_descent;
10926 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10927 }
10928
10929 /* Compute a suitable window start. */
10930 if (height > max_height)
10931 {
10932 height = (max_height / unit) * unit;
10933 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10934 move_it_vertically_backward (&it, height - unit);
10935 start = it.current.pos;
10936 }
10937 else
10938 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10939 SET_MARKER_FROM_TEXT_POS (w->start, start);
10940
10941 if (EQ (Vresize_mini_windows, Qgrow_only))
10942 {
10943 /* Let it grow only, until we display an empty message, in which
10944 case the window shrinks again. */
10945 if (height > WINDOW_PIXEL_HEIGHT (w))
10946 {
10947 int old_height = WINDOW_PIXEL_HEIGHT (w);
10948
10949 FRAME_WINDOWS_FROZEN (f) = 1;
10950 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10951 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10952 }
10953 else if (height < WINDOW_PIXEL_HEIGHT (w)
10954 && (exact_p || BEGV == ZV))
10955 {
10956 int old_height = WINDOW_PIXEL_HEIGHT (w);
10957
10958 FRAME_WINDOWS_FROZEN (f) = 0;
10959 shrink_mini_window (w, 1);
10960 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10961 }
10962 }
10963 else
10964 {
10965 /* Always resize to exact size needed. */
10966 if (height > WINDOW_PIXEL_HEIGHT (w))
10967 {
10968 int old_height = WINDOW_PIXEL_HEIGHT (w);
10969
10970 FRAME_WINDOWS_FROZEN (f) = 1;
10971 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10972 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10973 }
10974 else if (height < WINDOW_PIXEL_HEIGHT (w))
10975 {
10976 int old_height = WINDOW_PIXEL_HEIGHT (w);
10977
10978 FRAME_WINDOWS_FROZEN (f) = 0;
10979 shrink_mini_window (w, 1);
10980
10981 if (height)
10982 {
10983 FRAME_WINDOWS_FROZEN (f) = 1;
10984 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10985 }
10986
10987 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10988 }
10989 }
10990
10991 if (old_current_buffer)
10992 set_buffer_internal (old_current_buffer);
10993 }
10994
10995 return window_height_changed_p;
10996 }
10997
10998
10999 /* Value is the current message, a string, or nil if there is no
11000 current message. */
11001
11002 Lisp_Object
11003 current_message (void)
11004 {
11005 Lisp_Object msg;
11006
11007 if (!BUFFERP (echo_area_buffer[0]))
11008 msg = Qnil;
11009 else
11010 {
11011 with_echo_area_buffer (0, 0, current_message_1,
11012 (intptr_t) &msg, Qnil);
11013 if (NILP (msg))
11014 echo_area_buffer[0] = Qnil;
11015 }
11016
11017 return msg;
11018 }
11019
11020
11021 static int
11022 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11023 {
11024 intptr_t i1 = a1;
11025 Lisp_Object *msg = (Lisp_Object *) i1;
11026
11027 if (Z > BEG)
11028 *msg = make_buffer_string (BEG, Z, 1);
11029 else
11030 *msg = Qnil;
11031 return 0;
11032 }
11033
11034
11035 /* Push the current message on Vmessage_stack for later restoration
11036 by restore_message. Value is non-zero if the current message isn't
11037 empty. This is a relatively infrequent operation, so it's not
11038 worth optimizing. */
11039
11040 bool
11041 push_message (void)
11042 {
11043 Lisp_Object msg = current_message ();
11044 Vmessage_stack = Fcons (msg, Vmessage_stack);
11045 return STRINGP (msg);
11046 }
11047
11048
11049 /* Restore message display from the top of Vmessage_stack. */
11050
11051 void
11052 restore_message (void)
11053 {
11054 eassert (CONSP (Vmessage_stack));
11055 message3_nolog (XCAR (Vmessage_stack));
11056 }
11057
11058
11059 /* Handler for unwind-protect calling pop_message. */
11060
11061 void
11062 pop_message_unwind (void)
11063 {
11064 /* Pop the top-most entry off Vmessage_stack. */
11065 eassert (CONSP (Vmessage_stack));
11066 Vmessage_stack = XCDR (Vmessage_stack);
11067 }
11068
11069
11070 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11071 exits. If the stack is not empty, we have a missing pop_message
11072 somewhere. */
11073
11074 void
11075 check_message_stack (void)
11076 {
11077 if (!NILP (Vmessage_stack))
11078 emacs_abort ();
11079 }
11080
11081
11082 /* Truncate to NCHARS what will be displayed in the echo area the next
11083 time we display it---but don't redisplay it now. */
11084
11085 void
11086 truncate_echo_area (ptrdiff_t nchars)
11087 {
11088 if (nchars == 0)
11089 echo_area_buffer[0] = Qnil;
11090 else if (!noninteractive
11091 && INTERACTIVE
11092 && !NILP (echo_area_buffer[0]))
11093 {
11094 struct frame *sf = SELECTED_FRAME ();
11095 /* Error messages get reported properly by cmd_error, so this must be
11096 just an informative message; if the frame hasn't really been
11097 initialized yet, just toss it. */
11098 if (sf->glyphs_initialized_p)
11099 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11100 }
11101 }
11102
11103
11104 /* Helper function for truncate_echo_area. Truncate the current
11105 message to at most NCHARS characters. */
11106
11107 static int
11108 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11109 {
11110 if (BEG + nchars < Z)
11111 del_range (BEG + nchars, Z);
11112 if (Z == BEG)
11113 echo_area_buffer[0] = Qnil;
11114 return 0;
11115 }
11116
11117 /* Set the current message to STRING. */
11118
11119 static void
11120 set_message (Lisp_Object string)
11121 {
11122 eassert (STRINGP (string));
11123
11124 message_enable_multibyte = STRING_MULTIBYTE (string);
11125
11126 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11127 message_buf_print = 0;
11128 help_echo_showing_p = 0;
11129
11130 if (STRINGP (Vdebug_on_message)
11131 && STRINGP (string)
11132 && fast_string_match (Vdebug_on_message, string) >= 0)
11133 call_debugger (list2 (Qerror, string));
11134 }
11135
11136
11137 /* Helper function for set_message. First argument is ignored and second
11138 argument has the same meaning as for set_message.
11139 This function is called with the echo area buffer being current. */
11140
11141 static int
11142 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11143 {
11144 eassert (STRINGP (string));
11145
11146 /* Change multibyteness of the echo buffer appropriately. */
11147 if (message_enable_multibyte
11148 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11149 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11150
11151 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11152 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11153 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11154
11155 /* Insert new message at BEG. */
11156 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11157
11158 /* This function takes care of single/multibyte conversion.
11159 We just have to ensure that the echo area buffer has the right
11160 setting of enable_multibyte_characters. */
11161 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11162
11163 return 0;
11164 }
11165
11166
11167 /* Clear messages. CURRENT_P non-zero means clear the current
11168 message. LAST_DISPLAYED_P non-zero means clear the message
11169 last displayed. */
11170
11171 void
11172 clear_message (bool current_p, bool last_displayed_p)
11173 {
11174 if (current_p)
11175 {
11176 echo_area_buffer[0] = Qnil;
11177 message_cleared_p = true;
11178 }
11179
11180 if (last_displayed_p)
11181 echo_area_buffer[1] = Qnil;
11182
11183 message_buf_print = 0;
11184 }
11185
11186 /* Clear garbaged frames.
11187
11188 This function is used where the old redisplay called
11189 redraw_garbaged_frames which in turn called redraw_frame which in
11190 turn called clear_frame. The call to clear_frame was a source of
11191 flickering. I believe a clear_frame is not necessary. It should
11192 suffice in the new redisplay to invalidate all current matrices,
11193 and ensure a complete redisplay of all windows. */
11194
11195 static void
11196 clear_garbaged_frames (void)
11197 {
11198 if (frame_garbaged)
11199 {
11200 Lisp_Object tail, frame;
11201
11202 FOR_EACH_FRAME (tail, frame)
11203 {
11204 struct frame *f = XFRAME (frame);
11205
11206 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11207 {
11208 if (f->resized_p)
11209 redraw_frame (f);
11210 else
11211 clear_current_matrices (f);
11212 fset_redisplay (f);
11213 f->garbaged = false;
11214 f->resized_p = false;
11215 }
11216 }
11217
11218 frame_garbaged = false;
11219 }
11220 }
11221
11222
11223 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11224 is non-zero update selected_frame. Value is non-zero if the
11225 mini-windows height has been changed. */
11226
11227 static int
11228 echo_area_display (int update_frame_p)
11229 {
11230 Lisp_Object mini_window;
11231 struct window *w;
11232 struct frame *f;
11233 int window_height_changed_p = 0;
11234 struct frame *sf = SELECTED_FRAME ();
11235
11236 mini_window = FRAME_MINIBUF_WINDOW (sf);
11237 w = XWINDOW (mini_window);
11238 f = XFRAME (WINDOW_FRAME (w));
11239
11240 /* Don't display if frame is invisible or not yet initialized. */
11241 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11242 return 0;
11243
11244 #ifdef HAVE_WINDOW_SYSTEM
11245 /* When Emacs starts, selected_frame may be the initial terminal
11246 frame. If we let this through, a message would be displayed on
11247 the terminal. */
11248 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11249 return 0;
11250 #endif /* HAVE_WINDOW_SYSTEM */
11251
11252 /* Redraw garbaged frames. */
11253 clear_garbaged_frames ();
11254
11255 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11256 {
11257 echo_area_window = mini_window;
11258 window_height_changed_p = display_echo_area (w);
11259 w->must_be_updated_p = true;
11260
11261 /* Update the display, unless called from redisplay_internal.
11262 Also don't update the screen during redisplay itself. The
11263 update will happen at the end of redisplay, and an update
11264 here could cause confusion. */
11265 if (update_frame_p && !redisplaying_p)
11266 {
11267 int n = 0;
11268
11269 /* If the display update has been interrupted by pending
11270 input, update mode lines in the frame. Due to the
11271 pending input, it might have been that redisplay hasn't
11272 been called, so that mode lines above the echo area are
11273 garbaged. This looks odd, so we prevent it here. */
11274 if (!display_completed)
11275 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11276
11277 if (window_height_changed_p
11278 /* Don't do this if Emacs is shutting down. Redisplay
11279 needs to run hooks. */
11280 && !NILP (Vrun_hooks))
11281 {
11282 /* Must update other windows. Likewise as in other
11283 cases, don't let this update be interrupted by
11284 pending input. */
11285 ptrdiff_t count = SPECPDL_INDEX ();
11286 specbind (Qredisplay_dont_pause, Qt);
11287 windows_or_buffers_changed = 44;
11288 redisplay_internal ();
11289 unbind_to (count, Qnil);
11290 }
11291 else if (FRAME_WINDOW_P (f) && n == 0)
11292 {
11293 /* Window configuration is the same as before.
11294 Can do with a display update of the echo area,
11295 unless we displayed some mode lines. */
11296 update_single_window (w, 1);
11297 flush_frame (f);
11298 }
11299 else
11300 update_frame (f, 1, 1);
11301
11302 /* If cursor is in the echo area, make sure that the next
11303 redisplay displays the minibuffer, so that the cursor will
11304 be replaced with what the minibuffer wants. */
11305 if (cursor_in_echo_area)
11306 wset_redisplay (XWINDOW (mini_window));
11307 }
11308 }
11309 else if (!EQ (mini_window, selected_window))
11310 wset_redisplay (XWINDOW (mini_window));
11311
11312 /* Last displayed message is now the current message. */
11313 echo_area_buffer[1] = echo_area_buffer[0];
11314 /* Inform read_char that we're not echoing. */
11315 echo_message_buffer = Qnil;
11316
11317 /* Prevent redisplay optimization in redisplay_internal by resetting
11318 this_line_start_pos. This is done because the mini-buffer now
11319 displays the message instead of its buffer text. */
11320 if (EQ (mini_window, selected_window))
11321 CHARPOS (this_line_start_pos) = 0;
11322
11323 return window_height_changed_p;
11324 }
11325
11326 /* Nonzero if W's buffer was changed but not saved. */
11327
11328 static int
11329 window_buffer_changed (struct window *w)
11330 {
11331 struct buffer *b = XBUFFER (w->contents);
11332
11333 eassert (BUFFER_LIVE_P (b));
11334
11335 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11336 }
11337
11338 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11339
11340 static int
11341 mode_line_update_needed (struct window *w)
11342 {
11343 return (w->column_number_displayed != -1
11344 && !(PT == w->last_point && !window_outdated (w))
11345 && (w->column_number_displayed != current_column ()));
11346 }
11347
11348 /* Nonzero if window start of W is frozen and may not be changed during
11349 redisplay. */
11350
11351 static bool
11352 window_frozen_p (struct window *w)
11353 {
11354 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11355 {
11356 Lisp_Object window;
11357
11358 XSETWINDOW (window, w);
11359 if (MINI_WINDOW_P (w))
11360 return 0;
11361 else if (EQ (window, selected_window))
11362 return 0;
11363 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11364 && EQ (window, Vminibuf_scroll_window))
11365 /* This special window can't be frozen too. */
11366 return 0;
11367 else
11368 return 1;
11369 }
11370 return 0;
11371 }
11372
11373 /***********************************************************************
11374 Mode Lines and Frame Titles
11375 ***********************************************************************/
11376
11377 /* A buffer for constructing non-propertized mode-line strings and
11378 frame titles in it; allocated from the heap in init_xdisp and
11379 resized as needed in store_mode_line_noprop_char. */
11380
11381 static char *mode_line_noprop_buf;
11382
11383 /* The buffer's end, and a current output position in it. */
11384
11385 static char *mode_line_noprop_buf_end;
11386 static char *mode_line_noprop_ptr;
11387
11388 #define MODE_LINE_NOPROP_LEN(start) \
11389 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11390
11391 static enum {
11392 MODE_LINE_DISPLAY = 0,
11393 MODE_LINE_TITLE,
11394 MODE_LINE_NOPROP,
11395 MODE_LINE_STRING
11396 } mode_line_target;
11397
11398 /* Alist that caches the results of :propertize.
11399 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11400 static Lisp_Object mode_line_proptrans_alist;
11401
11402 /* List of strings making up the mode-line. */
11403 static Lisp_Object mode_line_string_list;
11404
11405 /* Base face property when building propertized mode line string. */
11406 static Lisp_Object mode_line_string_face;
11407 static Lisp_Object mode_line_string_face_prop;
11408
11409
11410 /* Unwind data for mode line strings */
11411
11412 static Lisp_Object Vmode_line_unwind_vector;
11413
11414 static Lisp_Object
11415 format_mode_line_unwind_data (struct frame *target_frame,
11416 struct buffer *obuf,
11417 Lisp_Object owin,
11418 int save_proptrans)
11419 {
11420 Lisp_Object vector, tmp;
11421
11422 /* Reduce consing by keeping one vector in
11423 Vwith_echo_area_save_vector. */
11424 vector = Vmode_line_unwind_vector;
11425 Vmode_line_unwind_vector = Qnil;
11426
11427 if (NILP (vector))
11428 vector = Fmake_vector (make_number (10), Qnil);
11429
11430 ASET (vector, 0, make_number (mode_line_target));
11431 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11432 ASET (vector, 2, mode_line_string_list);
11433 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11434 ASET (vector, 4, mode_line_string_face);
11435 ASET (vector, 5, mode_line_string_face_prop);
11436
11437 if (obuf)
11438 XSETBUFFER (tmp, obuf);
11439 else
11440 tmp = Qnil;
11441 ASET (vector, 6, tmp);
11442 ASET (vector, 7, owin);
11443 if (target_frame)
11444 {
11445 /* Similarly to `with-selected-window', if the operation selects
11446 a window on another frame, we must restore that frame's
11447 selected window, and (for a tty) the top-frame. */
11448 ASET (vector, 8, target_frame->selected_window);
11449 if (FRAME_TERMCAP_P (target_frame))
11450 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11451 }
11452
11453 return vector;
11454 }
11455
11456 static void
11457 unwind_format_mode_line (Lisp_Object vector)
11458 {
11459 Lisp_Object old_window = AREF (vector, 7);
11460 Lisp_Object target_frame_window = AREF (vector, 8);
11461 Lisp_Object old_top_frame = AREF (vector, 9);
11462
11463 mode_line_target = XINT (AREF (vector, 0));
11464 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11465 mode_line_string_list = AREF (vector, 2);
11466 if (! EQ (AREF (vector, 3), Qt))
11467 mode_line_proptrans_alist = AREF (vector, 3);
11468 mode_line_string_face = AREF (vector, 4);
11469 mode_line_string_face_prop = AREF (vector, 5);
11470
11471 /* Select window before buffer, since it may change the buffer. */
11472 if (!NILP (old_window))
11473 {
11474 /* If the operation that we are unwinding had selected a window
11475 on a different frame, reset its frame-selected-window. For a
11476 text terminal, reset its top-frame if necessary. */
11477 if (!NILP (target_frame_window))
11478 {
11479 Lisp_Object frame
11480 = WINDOW_FRAME (XWINDOW (target_frame_window));
11481
11482 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11483 Fselect_window (target_frame_window, Qt);
11484
11485 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11486 Fselect_frame (old_top_frame, Qt);
11487 }
11488
11489 Fselect_window (old_window, Qt);
11490 }
11491
11492 if (!NILP (AREF (vector, 6)))
11493 {
11494 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11495 ASET (vector, 6, Qnil);
11496 }
11497
11498 Vmode_line_unwind_vector = vector;
11499 }
11500
11501
11502 /* Store a single character C for the frame title in mode_line_noprop_buf.
11503 Re-allocate mode_line_noprop_buf if necessary. */
11504
11505 static void
11506 store_mode_line_noprop_char (char c)
11507 {
11508 /* If output position has reached the end of the allocated buffer,
11509 increase the buffer's size. */
11510 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11511 {
11512 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11513 ptrdiff_t size = len;
11514 mode_line_noprop_buf =
11515 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11516 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11517 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11518 }
11519
11520 *mode_line_noprop_ptr++ = c;
11521 }
11522
11523
11524 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11525 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11526 characters that yield more columns than PRECISION; PRECISION <= 0
11527 means copy the whole string. Pad with spaces until FIELD_WIDTH
11528 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11529 pad. Called from display_mode_element when it is used to build a
11530 frame title. */
11531
11532 static int
11533 store_mode_line_noprop (const char *string, int field_width, int precision)
11534 {
11535 const unsigned char *str = (const unsigned char *) string;
11536 int n = 0;
11537 ptrdiff_t dummy, nbytes;
11538
11539 /* Copy at most PRECISION chars from STR. */
11540 nbytes = strlen (string);
11541 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11542 while (nbytes--)
11543 store_mode_line_noprop_char (*str++);
11544
11545 /* Fill up with spaces until FIELD_WIDTH reached. */
11546 while (field_width > 0
11547 && n < field_width)
11548 {
11549 store_mode_line_noprop_char (' ');
11550 ++n;
11551 }
11552
11553 return n;
11554 }
11555
11556 /***********************************************************************
11557 Frame Titles
11558 ***********************************************************************/
11559
11560 #ifdef HAVE_WINDOW_SYSTEM
11561
11562 /* Set the title of FRAME, if it has changed. The title format is
11563 Vicon_title_format if FRAME is iconified, otherwise it is
11564 frame_title_format. */
11565
11566 static void
11567 x_consider_frame_title (Lisp_Object frame)
11568 {
11569 struct frame *f = XFRAME (frame);
11570
11571 if (FRAME_WINDOW_P (f)
11572 || FRAME_MINIBUF_ONLY_P (f)
11573 || f->explicit_name)
11574 {
11575 /* Do we have more than one visible frame on this X display? */
11576 Lisp_Object tail, other_frame, fmt;
11577 ptrdiff_t title_start;
11578 char *title;
11579 ptrdiff_t len;
11580 struct it it;
11581 ptrdiff_t count = SPECPDL_INDEX ();
11582
11583 FOR_EACH_FRAME (tail, other_frame)
11584 {
11585 struct frame *tf = XFRAME (other_frame);
11586
11587 if (tf != f
11588 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11589 && !FRAME_MINIBUF_ONLY_P (tf)
11590 && !EQ (other_frame, tip_frame)
11591 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11592 break;
11593 }
11594
11595 /* Set global variable indicating that multiple frames exist. */
11596 multiple_frames = CONSP (tail);
11597
11598 /* Switch to the buffer of selected window of the frame. Set up
11599 mode_line_target so that display_mode_element will output into
11600 mode_line_noprop_buf; then display the title. */
11601 record_unwind_protect (unwind_format_mode_line,
11602 format_mode_line_unwind_data
11603 (f, current_buffer, selected_window, 0));
11604
11605 Fselect_window (f->selected_window, Qt);
11606 set_buffer_internal_1
11607 (XBUFFER (XWINDOW (f->selected_window)->contents));
11608 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11609
11610 mode_line_target = MODE_LINE_TITLE;
11611 title_start = MODE_LINE_NOPROP_LEN (0);
11612 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11613 NULL, DEFAULT_FACE_ID);
11614 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11615 len = MODE_LINE_NOPROP_LEN (title_start);
11616 title = mode_line_noprop_buf + title_start;
11617 unbind_to (count, Qnil);
11618
11619 /* Set the title only if it's changed. This avoids consing in
11620 the common case where it hasn't. (If it turns out that we've
11621 already wasted too much time by walking through the list with
11622 display_mode_element, then we might need to optimize at a
11623 higher level than this.) */
11624 if (! STRINGP (f->name)
11625 || SBYTES (f->name) != len
11626 || memcmp (title, SDATA (f->name), len) != 0)
11627 x_implicitly_set_name (f, make_string (title, len), Qnil);
11628 }
11629 }
11630
11631 #endif /* not HAVE_WINDOW_SYSTEM */
11632
11633 \f
11634 /***********************************************************************
11635 Menu Bars
11636 ***********************************************************************/
11637
11638 /* Non-zero if we will not redisplay all visible windows. */
11639 #define REDISPLAY_SOME_P() \
11640 ((windows_or_buffers_changed == 0 \
11641 || windows_or_buffers_changed == REDISPLAY_SOME) \
11642 && (update_mode_lines == 0 \
11643 || update_mode_lines == REDISPLAY_SOME))
11644
11645 /* Prepare for redisplay by updating menu-bar item lists when
11646 appropriate. This can call eval. */
11647
11648 static void
11649 prepare_menu_bars (void)
11650 {
11651 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11652 bool some_windows = REDISPLAY_SOME_P ();
11653 struct gcpro gcpro1, gcpro2;
11654 Lisp_Object tooltip_frame;
11655
11656 #ifdef HAVE_WINDOW_SYSTEM
11657 tooltip_frame = tip_frame;
11658 #else
11659 tooltip_frame = Qnil;
11660 #endif
11661
11662 if (FUNCTIONP (Vpre_redisplay_function))
11663 {
11664 Lisp_Object windows = all_windows ? Qt : Qnil;
11665 if (all_windows && some_windows)
11666 {
11667 Lisp_Object ws = window_list ();
11668 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11669 {
11670 Lisp_Object this = XCAR (ws);
11671 struct window *w = XWINDOW (this);
11672 if (w->redisplay
11673 || XFRAME (w->frame)->redisplay
11674 || XBUFFER (w->contents)->text->redisplay)
11675 {
11676 windows = Fcons (this, windows);
11677 }
11678 }
11679 }
11680 safe__call1 (true, Vpre_redisplay_function, windows);
11681 }
11682
11683 /* Update all frame titles based on their buffer names, etc. We do
11684 this before the menu bars so that the buffer-menu will show the
11685 up-to-date frame titles. */
11686 #ifdef HAVE_WINDOW_SYSTEM
11687 if (all_windows)
11688 {
11689 Lisp_Object tail, frame;
11690
11691 FOR_EACH_FRAME (tail, frame)
11692 {
11693 struct frame *f = XFRAME (frame);
11694 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11695 if (some_windows
11696 && !f->redisplay
11697 && !w->redisplay
11698 && !XBUFFER (w->contents)->text->redisplay)
11699 continue;
11700
11701 if (!EQ (frame, tooltip_frame)
11702 && (FRAME_ICONIFIED_P (f)
11703 || FRAME_VISIBLE_P (f) == 1
11704 /* Exclude TTY frames that are obscured because they
11705 are not the top frame on their console. This is
11706 because x_consider_frame_title actually switches
11707 to the frame, which for TTY frames means it is
11708 marked as garbaged, and will be completely
11709 redrawn on the next redisplay cycle. This causes
11710 TTY frames to be completely redrawn, when there
11711 are more than one of them, even though nothing
11712 should be changed on display. */
11713 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11714 x_consider_frame_title (frame);
11715 }
11716 }
11717 #endif /* HAVE_WINDOW_SYSTEM */
11718
11719 /* Update the menu bar item lists, if appropriate. This has to be
11720 done before any actual redisplay or generation of display lines. */
11721
11722 if (all_windows)
11723 {
11724 Lisp_Object tail, frame;
11725 ptrdiff_t count = SPECPDL_INDEX ();
11726 /* 1 means that update_menu_bar has run its hooks
11727 so any further calls to update_menu_bar shouldn't do so again. */
11728 int menu_bar_hooks_run = 0;
11729
11730 record_unwind_save_match_data ();
11731
11732 FOR_EACH_FRAME (tail, frame)
11733 {
11734 struct frame *f = XFRAME (frame);
11735 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11736
11737 /* Ignore tooltip frame. */
11738 if (EQ (frame, tooltip_frame))
11739 continue;
11740
11741 if (some_windows
11742 && !f->redisplay
11743 && !w->redisplay
11744 && !XBUFFER (w->contents)->text->redisplay)
11745 continue;
11746
11747 /* If a window on this frame changed size, report that to
11748 the user and clear the size-change flag. */
11749 if (FRAME_WINDOW_SIZES_CHANGED (f))
11750 {
11751 Lisp_Object functions;
11752
11753 /* Clear flag first in case we get an error below. */
11754 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11755 functions = Vwindow_size_change_functions;
11756 GCPRO2 (tail, functions);
11757
11758 while (CONSP (functions))
11759 {
11760 if (!EQ (XCAR (functions), Qt))
11761 call1 (XCAR (functions), frame);
11762 functions = XCDR (functions);
11763 }
11764 UNGCPRO;
11765 }
11766
11767 GCPRO1 (tail);
11768 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11769 #ifdef HAVE_WINDOW_SYSTEM
11770 update_tool_bar (f, 0);
11771 #endif
11772 #ifdef HAVE_NS
11773 if (windows_or_buffers_changed
11774 && FRAME_NS_P (f))
11775 ns_set_doc_edited
11776 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11777 #endif
11778 UNGCPRO;
11779 }
11780
11781 unbind_to (count, Qnil);
11782 }
11783 else
11784 {
11785 struct frame *sf = SELECTED_FRAME ();
11786 update_menu_bar (sf, 1, 0);
11787 #ifdef HAVE_WINDOW_SYSTEM
11788 update_tool_bar (sf, 1);
11789 #endif
11790 }
11791 }
11792
11793
11794 /* Update the menu bar item list for frame F. This has to be done
11795 before we start to fill in any display lines, because it can call
11796 eval.
11797
11798 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11799
11800 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11801 already ran the menu bar hooks for this redisplay, so there
11802 is no need to run them again. The return value is the
11803 updated value of this flag, to pass to the next call. */
11804
11805 static int
11806 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11807 {
11808 Lisp_Object window;
11809 register struct window *w;
11810
11811 /* If called recursively during a menu update, do nothing. This can
11812 happen when, for instance, an activate-menubar-hook causes a
11813 redisplay. */
11814 if (inhibit_menubar_update)
11815 return hooks_run;
11816
11817 window = FRAME_SELECTED_WINDOW (f);
11818 w = XWINDOW (window);
11819
11820 if (FRAME_WINDOW_P (f)
11821 ?
11822 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11823 || defined (HAVE_NS) || defined (USE_GTK)
11824 FRAME_EXTERNAL_MENU_BAR (f)
11825 #else
11826 FRAME_MENU_BAR_LINES (f) > 0
11827 #endif
11828 : FRAME_MENU_BAR_LINES (f) > 0)
11829 {
11830 /* If the user has switched buffers or windows, we need to
11831 recompute to reflect the new bindings. But we'll
11832 recompute when update_mode_lines is set too; that means
11833 that people can use force-mode-line-update to request
11834 that the menu bar be recomputed. The adverse effect on
11835 the rest of the redisplay algorithm is about the same as
11836 windows_or_buffers_changed anyway. */
11837 if (windows_or_buffers_changed
11838 /* This used to test w->update_mode_line, but we believe
11839 there is no need to recompute the menu in that case. */
11840 || update_mode_lines
11841 || window_buffer_changed (w))
11842 {
11843 struct buffer *prev = current_buffer;
11844 ptrdiff_t count = SPECPDL_INDEX ();
11845
11846 specbind (Qinhibit_menubar_update, Qt);
11847
11848 set_buffer_internal_1 (XBUFFER (w->contents));
11849 if (save_match_data)
11850 record_unwind_save_match_data ();
11851 if (NILP (Voverriding_local_map_menu_flag))
11852 {
11853 specbind (Qoverriding_terminal_local_map, Qnil);
11854 specbind (Qoverriding_local_map, Qnil);
11855 }
11856
11857 if (!hooks_run)
11858 {
11859 /* Run the Lucid hook. */
11860 safe_run_hooks (Qactivate_menubar_hook);
11861
11862 /* If it has changed current-menubar from previous value,
11863 really recompute the menu-bar from the value. */
11864 if (! NILP (Vlucid_menu_bar_dirty_flag))
11865 call0 (Qrecompute_lucid_menubar);
11866
11867 safe_run_hooks (Qmenu_bar_update_hook);
11868
11869 hooks_run = 1;
11870 }
11871
11872 XSETFRAME (Vmenu_updating_frame, f);
11873 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11874
11875 /* Redisplay the menu bar in case we changed it. */
11876 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11877 || defined (HAVE_NS) || defined (USE_GTK)
11878 if (FRAME_WINDOW_P (f))
11879 {
11880 #if defined (HAVE_NS)
11881 /* All frames on Mac OS share the same menubar. So only
11882 the selected frame should be allowed to set it. */
11883 if (f == SELECTED_FRAME ())
11884 #endif
11885 set_frame_menubar (f, 0, 0);
11886 }
11887 else
11888 /* On a terminal screen, the menu bar is an ordinary screen
11889 line, and this makes it get updated. */
11890 w->update_mode_line = 1;
11891 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11892 /* In the non-toolkit version, the menu bar is an ordinary screen
11893 line, and this makes it get updated. */
11894 w->update_mode_line = 1;
11895 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11896
11897 unbind_to (count, Qnil);
11898 set_buffer_internal_1 (prev);
11899 }
11900 }
11901
11902 return hooks_run;
11903 }
11904
11905 /***********************************************************************
11906 Tool-bars
11907 ***********************************************************************/
11908
11909 #ifdef HAVE_WINDOW_SYSTEM
11910
11911 /* Tool-bar item index of the item on which a mouse button was pressed
11912 or -1. */
11913
11914 int last_tool_bar_item;
11915
11916 /* Select `frame' temporarily without running all the code in
11917 do_switch_frame.
11918 FIXME: Maybe do_switch_frame should be trimmed down similarly
11919 when `norecord' is set. */
11920 static void
11921 fast_set_selected_frame (Lisp_Object frame)
11922 {
11923 if (!EQ (selected_frame, frame))
11924 {
11925 selected_frame = frame;
11926 selected_window = XFRAME (frame)->selected_window;
11927 }
11928 }
11929
11930 /* Update the tool-bar item list for frame F. This has to be done
11931 before we start to fill in any display lines. Called from
11932 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11933 and restore it here. */
11934
11935 static void
11936 update_tool_bar (struct frame *f, int save_match_data)
11937 {
11938 #if defined (USE_GTK) || defined (HAVE_NS)
11939 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11940 #else
11941 int do_update = (WINDOWP (f->tool_bar_window)
11942 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11943 #endif
11944
11945 if (do_update)
11946 {
11947 Lisp_Object window;
11948 struct window *w;
11949
11950 window = FRAME_SELECTED_WINDOW (f);
11951 w = XWINDOW (window);
11952
11953 /* If the user has switched buffers or windows, we need to
11954 recompute to reflect the new bindings. But we'll
11955 recompute when update_mode_lines is set too; that means
11956 that people can use force-mode-line-update to request
11957 that the menu bar be recomputed. The adverse effect on
11958 the rest of the redisplay algorithm is about the same as
11959 windows_or_buffers_changed anyway. */
11960 if (windows_or_buffers_changed
11961 || w->update_mode_line
11962 || update_mode_lines
11963 || window_buffer_changed (w))
11964 {
11965 struct buffer *prev = current_buffer;
11966 ptrdiff_t count = SPECPDL_INDEX ();
11967 Lisp_Object frame, new_tool_bar;
11968 int new_n_tool_bar;
11969 struct gcpro gcpro1;
11970
11971 /* Set current_buffer to the buffer of the selected
11972 window of the frame, so that we get the right local
11973 keymaps. */
11974 set_buffer_internal_1 (XBUFFER (w->contents));
11975
11976 /* Save match data, if we must. */
11977 if (save_match_data)
11978 record_unwind_save_match_data ();
11979
11980 /* Make sure that we don't accidentally use bogus keymaps. */
11981 if (NILP (Voverriding_local_map_menu_flag))
11982 {
11983 specbind (Qoverriding_terminal_local_map, Qnil);
11984 specbind (Qoverriding_local_map, Qnil);
11985 }
11986
11987 GCPRO1 (new_tool_bar);
11988
11989 /* We must temporarily set the selected frame to this frame
11990 before calling tool_bar_items, because the calculation of
11991 the tool-bar keymap uses the selected frame (see
11992 `tool-bar-make-keymap' in tool-bar.el). */
11993 eassert (EQ (selected_window,
11994 /* Since we only explicitly preserve selected_frame,
11995 check that selected_window would be redundant. */
11996 XFRAME (selected_frame)->selected_window));
11997 record_unwind_protect (fast_set_selected_frame, selected_frame);
11998 XSETFRAME (frame, f);
11999 fast_set_selected_frame (frame);
12000
12001 /* Build desired tool-bar items from keymaps. */
12002 new_tool_bar
12003 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12004 &new_n_tool_bar);
12005
12006 /* Redisplay the tool-bar if we changed it. */
12007 if (new_n_tool_bar != f->n_tool_bar_items
12008 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12009 {
12010 /* Redisplay that happens asynchronously due to an expose event
12011 may access f->tool_bar_items. Make sure we update both
12012 variables within BLOCK_INPUT so no such event interrupts. */
12013 block_input ();
12014 fset_tool_bar_items (f, new_tool_bar);
12015 f->n_tool_bar_items = new_n_tool_bar;
12016 w->update_mode_line = 1;
12017 unblock_input ();
12018 }
12019
12020 UNGCPRO;
12021
12022 unbind_to (count, Qnil);
12023 set_buffer_internal_1 (prev);
12024 }
12025 }
12026 }
12027
12028 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12029
12030 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12031 F's desired tool-bar contents. F->tool_bar_items must have
12032 been set up previously by calling prepare_menu_bars. */
12033
12034 static void
12035 build_desired_tool_bar_string (struct frame *f)
12036 {
12037 int i, size, size_needed;
12038 struct gcpro gcpro1, gcpro2, gcpro3;
12039 Lisp_Object image, plist, props;
12040
12041 image = plist = props = Qnil;
12042 GCPRO3 (image, plist, props);
12043
12044 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12045 Otherwise, make a new string. */
12046
12047 /* The size of the string we might be able to reuse. */
12048 size = (STRINGP (f->desired_tool_bar_string)
12049 ? SCHARS (f->desired_tool_bar_string)
12050 : 0);
12051
12052 /* We need one space in the string for each image. */
12053 size_needed = f->n_tool_bar_items;
12054
12055 /* Reuse f->desired_tool_bar_string, if possible. */
12056 if (size < size_needed || NILP (f->desired_tool_bar_string))
12057 fset_desired_tool_bar_string
12058 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12059 else
12060 {
12061 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
12062 Fremove_text_properties (make_number (0), make_number (size),
12063 props, f->desired_tool_bar_string);
12064 }
12065
12066 /* Put a `display' property on the string for the images to display,
12067 put a `menu_item' property on tool-bar items with a value that
12068 is the index of the item in F's tool-bar item vector. */
12069 for (i = 0; i < f->n_tool_bar_items; ++i)
12070 {
12071 #define PROP(IDX) \
12072 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12073
12074 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12075 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12076 int hmargin, vmargin, relief, idx, end;
12077
12078 /* If image is a vector, choose the image according to the
12079 button state. */
12080 image = PROP (TOOL_BAR_ITEM_IMAGES);
12081 if (VECTORP (image))
12082 {
12083 if (enabled_p)
12084 idx = (selected_p
12085 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12086 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12087 else
12088 idx = (selected_p
12089 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12090 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12091
12092 eassert (ASIZE (image) >= idx);
12093 image = AREF (image, idx);
12094 }
12095 else
12096 idx = -1;
12097
12098 /* Ignore invalid image specifications. */
12099 if (!valid_image_p (image))
12100 continue;
12101
12102 /* Display the tool-bar button pressed, or depressed. */
12103 plist = Fcopy_sequence (XCDR (image));
12104
12105 /* Compute margin and relief to draw. */
12106 relief = (tool_bar_button_relief >= 0
12107 ? tool_bar_button_relief
12108 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12109 hmargin = vmargin = relief;
12110
12111 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12112 INT_MAX - max (hmargin, vmargin)))
12113 {
12114 hmargin += XFASTINT (Vtool_bar_button_margin);
12115 vmargin += XFASTINT (Vtool_bar_button_margin);
12116 }
12117 else if (CONSP (Vtool_bar_button_margin))
12118 {
12119 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12120 INT_MAX - hmargin))
12121 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12122
12123 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12124 INT_MAX - vmargin))
12125 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12126 }
12127
12128 if (auto_raise_tool_bar_buttons_p)
12129 {
12130 /* Add a `:relief' property to the image spec if the item is
12131 selected. */
12132 if (selected_p)
12133 {
12134 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12135 hmargin -= relief;
12136 vmargin -= relief;
12137 }
12138 }
12139 else
12140 {
12141 /* If image is selected, display it pressed, i.e. with a
12142 negative relief. If it's not selected, display it with a
12143 raised relief. */
12144 plist = Fplist_put (plist, QCrelief,
12145 (selected_p
12146 ? make_number (-relief)
12147 : make_number (relief)));
12148 hmargin -= relief;
12149 vmargin -= relief;
12150 }
12151
12152 /* Put a margin around the image. */
12153 if (hmargin || vmargin)
12154 {
12155 if (hmargin == vmargin)
12156 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12157 else
12158 plist = Fplist_put (plist, QCmargin,
12159 Fcons (make_number (hmargin),
12160 make_number (vmargin)));
12161 }
12162
12163 /* If button is not enabled, and we don't have special images
12164 for the disabled state, make the image appear disabled by
12165 applying an appropriate algorithm to it. */
12166 if (!enabled_p && idx < 0)
12167 plist = Fplist_put (plist, QCconversion, Qdisabled);
12168
12169 /* Put a `display' text property on the string for the image to
12170 display. Put a `menu-item' property on the string that gives
12171 the start of this item's properties in the tool-bar items
12172 vector. */
12173 image = Fcons (Qimage, plist);
12174 props = list4 (Qdisplay, image,
12175 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
12176
12177 /* Let the last image hide all remaining spaces in the tool bar
12178 string. The string can be longer than needed when we reuse a
12179 previous string. */
12180 if (i + 1 == f->n_tool_bar_items)
12181 end = SCHARS (f->desired_tool_bar_string);
12182 else
12183 end = i + 1;
12184 Fadd_text_properties (make_number (i), make_number (end),
12185 props, f->desired_tool_bar_string);
12186 #undef PROP
12187 }
12188
12189 UNGCPRO;
12190 }
12191
12192
12193 /* Display one line of the tool-bar of frame IT->f.
12194
12195 HEIGHT specifies the desired height of the tool-bar line.
12196 If the actual height of the glyph row is less than HEIGHT, the
12197 row's height is increased to HEIGHT, and the icons are centered
12198 vertically in the new height.
12199
12200 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12201 count a final empty row in case the tool-bar width exactly matches
12202 the window width.
12203 */
12204
12205 static void
12206 display_tool_bar_line (struct it *it, int height)
12207 {
12208 struct glyph_row *row = it->glyph_row;
12209 int max_x = it->last_visible_x;
12210 struct glyph *last;
12211
12212 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12213 clear_glyph_row (row);
12214 row->enabled_p = true;
12215 row->y = it->current_y;
12216
12217 /* Note that this isn't made use of if the face hasn't a box,
12218 so there's no need to check the face here. */
12219 it->start_of_box_run_p = 1;
12220
12221 while (it->current_x < max_x)
12222 {
12223 int x, n_glyphs_before, i, nglyphs;
12224 struct it it_before;
12225
12226 /* Get the next display element. */
12227 if (!get_next_display_element (it))
12228 {
12229 /* Don't count empty row if we are counting needed tool-bar lines. */
12230 if (height < 0 && !it->hpos)
12231 return;
12232 break;
12233 }
12234
12235 /* Produce glyphs. */
12236 n_glyphs_before = row->used[TEXT_AREA];
12237 it_before = *it;
12238
12239 PRODUCE_GLYPHS (it);
12240
12241 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12242 i = 0;
12243 x = it_before.current_x;
12244 while (i < nglyphs)
12245 {
12246 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12247
12248 if (x + glyph->pixel_width > max_x)
12249 {
12250 /* Glyph doesn't fit on line. Backtrack. */
12251 row->used[TEXT_AREA] = n_glyphs_before;
12252 *it = it_before;
12253 /* If this is the only glyph on this line, it will never fit on the
12254 tool-bar, so skip it. But ensure there is at least one glyph,
12255 so we don't accidentally disable the tool-bar. */
12256 if (n_glyphs_before == 0
12257 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12258 break;
12259 goto out;
12260 }
12261
12262 ++it->hpos;
12263 x += glyph->pixel_width;
12264 ++i;
12265 }
12266
12267 /* Stop at line end. */
12268 if (ITERATOR_AT_END_OF_LINE_P (it))
12269 break;
12270
12271 set_iterator_to_next (it, 1);
12272 }
12273
12274 out:;
12275
12276 row->displays_text_p = row->used[TEXT_AREA] != 0;
12277
12278 /* Use default face for the border below the tool bar.
12279
12280 FIXME: When auto-resize-tool-bars is grow-only, there is
12281 no additional border below the possibly empty tool-bar lines.
12282 So to make the extra empty lines look "normal", we have to
12283 use the tool-bar face for the border too. */
12284 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12285 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12286 it->face_id = DEFAULT_FACE_ID;
12287
12288 extend_face_to_end_of_line (it);
12289 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12290 last->right_box_line_p = 1;
12291 if (last == row->glyphs[TEXT_AREA])
12292 last->left_box_line_p = 1;
12293
12294 /* Make line the desired height and center it vertically. */
12295 if ((height -= it->max_ascent + it->max_descent) > 0)
12296 {
12297 /* Don't add more than one line height. */
12298 height %= FRAME_LINE_HEIGHT (it->f);
12299 it->max_ascent += height / 2;
12300 it->max_descent += (height + 1) / 2;
12301 }
12302
12303 compute_line_metrics (it);
12304
12305 /* If line is empty, make it occupy the rest of the tool-bar. */
12306 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12307 {
12308 row->height = row->phys_height = it->last_visible_y - row->y;
12309 row->visible_height = row->height;
12310 row->ascent = row->phys_ascent = 0;
12311 row->extra_line_spacing = 0;
12312 }
12313
12314 row->full_width_p = 1;
12315 row->continued_p = 0;
12316 row->truncated_on_left_p = 0;
12317 row->truncated_on_right_p = 0;
12318
12319 it->current_x = it->hpos = 0;
12320 it->current_y += row->height;
12321 ++it->vpos;
12322 ++it->glyph_row;
12323 }
12324
12325
12326 /* Max tool-bar height. Basically, this is what makes all other windows
12327 disappear when the frame gets too small. Rethink this! */
12328
12329 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12330 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12331
12332 /* Value is the number of pixels needed to make all tool-bar items of
12333 frame F visible. The actual number of glyph rows needed is
12334 returned in *N_ROWS if non-NULL. */
12335
12336 static int
12337 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12338 {
12339 struct window *w = XWINDOW (f->tool_bar_window);
12340 struct it it;
12341 /* tool_bar_height is called from redisplay_tool_bar after building
12342 the desired matrix, so use (unused) mode-line row as temporary row to
12343 avoid destroying the first tool-bar row. */
12344 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12345
12346 /* Initialize an iterator for iteration over
12347 F->desired_tool_bar_string in the tool-bar window of frame F. */
12348 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12349 it.first_visible_x = 0;
12350 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12351 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12352 it.paragraph_embedding = L2R;
12353
12354 while (!ITERATOR_AT_END_P (&it))
12355 {
12356 clear_glyph_row (temp_row);
12357 it.glyph_row = temp_row;
12358 display_tool_bar_line (&it, -1);
12359 }
12360 clear_glyph_row (temp_row);
12361
12362 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12363 if (n_rows)
12364 *n_rows = it.vpos > 0 ? it.vpos : -1;
12365
12366 if (pixelwise)
12367 return it.current_y;
12368 else
12369 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12370 }
12371
12372 #endif /* !USE_GTK && !HAVE_NS */
12373
12374 #if defined USE_GTK || defined HAVE_NS
12375 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12376 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12377 #endif
12378
12379 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12380 0, 2, 0,
12381 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12382 If FRAME is nil or omitted, use the selected frame. Optional argument
12383 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12384 (Lisp_Object frame, Lisp_Object pixelwise)
12385 {
12386 int height = 0;
12387
12388 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12389 struct frame *f = decode_any_frame (frame);
12390
12391 if (WINDOWP (f->tool_bar_window)
12392 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12393 {
12394 update_tool_bar (f, 1);
12395 if (f->n_tool_bar_items)
12396 {
12397 build_desired_tool_bar_string (f);
12398 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12399 }
12400 }
12401 #endif
12402
12403 return make_number (height);
12404 }
12405
12406
12407 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12408 height should be changed. */
12409
12410 static int
12411 redisplay_tool_bar (struct frame *f)
12412 {
12413 #if defined (USE_GTK) || defined (HAVE_NS)
12414
12415 if (FRAME_EXTERNAL_TOOL_BAR (f))
12416 update_frame_tool_bar (f);
12417 return 0;
12418
12419 #else /* !USE_GTK && !HAVE_NS */
12420
12421 struct window *w;
12422 struct it it;
12423 struct glyph_row *row;
12424
12425 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12426 do anything. This means you must start with tool-bar-lines
12427 non-zero to get the auto-sizing effect. Or in other words, you
12428 can turn off tool-bars by specifying tool-bar-lines zero. */
12429 if (!WINDOWP (f->tool_bar_window)
12430 || (w = XWINDOW (f->tool_bar_window),
12431 WINDOW_PIXEL_HEIGHT (w) == 0))
12432 return 0;
12433
12434 /* Set up an iterator for the tool-bar window. */
12435 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12436 it.first_visible_x = 0;
12437 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12438 row = it.glyph_row;
12439
12440 /* Build a string that represents the contents of the tool-bar. */
12441 build_desired_tool_bar_string (f);
12442 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12443 /* FIXME: This should be controlled by a user option. But it
12444 doesn't make sense to have an R2L tool bar if the menu bar cannot
12445 be drawn also R2L, and making the menu bar R2L is tricky due
12446 toolkit-specific code that implements it. If an R2L tool bar is
12447 ever supported, display_tool_bar_line should also be augmented to
12448 call unproduce_glyphs like display_line and display_string
12449 do. */
12450 it.paragraph_embedding = L2R;
12451
12452 if (f->n_tool_bar_rows == 0)
12453 {
12454 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12455
12456 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12457 {
12458 Lisp_Object frame;
12459 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12460 / FRAME_LINE_HEIGHT (f));
12461
12462 XSETFRAME (frame, f);
12463 Fmodify_frame_parameters (frame,
12464 list1 (Fcons (Qtool_bar_lines,
12465 make_number (new_lines))));
12466 /* Always do that now. */
12467 clear_glyph_matrix (w->desired_matrix);
12468 f->fonts_changed = 1;
12469 return 1;
12470 }
12471 }
12472
12473 /* Display as many lines as needed to display all tool-bar items. */
12474
12475 if (f->n_tool_bar_rows > 0)
12476 {
12477 int border, rows, height, extra;
12478
12479 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12480 border = XINT (Vtool_bar_border);
12481 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12482 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12483 else if (EQ (Vtool_bar_border, Qborder_width))
12484 border = f->border_width;
12485 else
12486 border = 0;
12487 if (border < 0)
12488 border = 0;
12489
12490 rows = f->n_tool_bar_rows;
12491 height = max (1, (it.last_visible_y - border) / rows);
12492 extra = it.last_visible_y - border - height * rows;
12493
12494 while (it.current_y < it.last_visible_y)
12495 {
12496 int h = 0;
12497 if (extra > 0 && rows-- > 0)
12498 {
12499 h = (extra + rows - 1) / rows;
12500 extra -= h;
12501 }
12502 display_tool_bar_line (&it, height + h);
12503 }
12504 }
12505 else
12506 {
12507 while (it.current_y < it.last_visible_y)
12508 display_tool_bar_line (&it, 0);
12509 }
12510
12511 /* It doesn't make much sense to try scrolling in the tool-bar
12512 window, so don't do it. */
12513 w->desired_matrix->no_scrolling_p = 1;
12514 w->must_be_updated_p = 1;
12515
12516 if (!NILP (Vauto_resize_tool_bars))
12517 {
12518 /* Do we really allow the toolbar to occupy the whole frame? */
12519 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12520 int change_height_p = 0;
12521
12522 /* If we couldn't display everything, change the tool-bar's
12523 height if there is room for more. */
12524 if (IT_STRING_CHARPOS (it) < it.end_charpos
12525 && it.current_y < max_tool_bar_height)
12526 change_height_p = 1;
12527
12528 /* We subtract 1 because display_tool_bar_line advances the
12529 glyph_row pointer before returning to its caller. We want to
12530 examine the last glyph row produced by
12531 display_tool_bar_line. */
12532 row = it.glyph_row - 1;
12533
12534 /* If there are blank lines at the end, except for a partially
12535 visible blank line at the end that is smaller than
12536 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12537 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12538 && row->height >= FRAME_LINE_HEIGHT (f))
12539 change_height_p = 1;
12540
12541 /* If row displays tool-bar items, but is partially visible,
12542 change the tool-bar's height. */
12543 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12544 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12545 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12546 change_height_p = 1;
12547
12548 /* Resize windows as needed by changing the `tool-bar-lines'
12549 frame parameter. */
12550 if (change_height_p)
12551 {
12552 Lisp_Object frame;
12553 int nrows;
12554 int new_height = tool_bar_height (f, &nrows, 1);
12555
12556 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12557 && !f->minimize_tool_bar_window_p)
12558 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12559 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12560 f->minimize_tool_bar_window_p = 0;
12561
12562 if (change_height_p)
12563 {
12564 /* Current size of the tool-bar window in canonical line
12565 units. */
12566 int old_lines = WINDOW_TOTAL_LINES (w);
12567 /* Required size of the tool-bar window in canonical
12568 line units. */
12569 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12570 / FRAME_LINE_HEIGHT (f));
12571 /* Maximum size of the tool-bar window in canonical line
12572 units that this frame can allow. */
12573 int max_lines =
12574 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12575
12576 /* Don't try to change the tool-bar window size and set
12577 the fonts_changed flag unless really necessary. That
12578 flag causes redisplay to give up and retry
12579 redisplaying the frame from scratch, so setting it
12580 unnecessarily can lead to nasty redisplay loops. */
12581 if (new_lines <= max_lines
12582 && eabs (new_lines - old_lines) >= 1)
12583 {
12584 XSETFRAME (frame, f);
12585 Fmodify_frame_parameters (frame,
12586 list1 (Fcons (Qtool_bar_lines,
12587 make_number (new_lines))));
12588 clear_glyph_matrix (w->desired_matrix);
12589 f->n_tool_bar_rows = nrows;
12590 f->fonts_changed = 1;
12591 return 1;
12592 }
12593 }
12594 }
12595 }
12596
12597 f->minimize_tool_bar_window_p = 0;
12598 return 0;
12599
12600 #endif /* USE_GTK || HAVE_NS */
12601 }
12602
12603 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12604
12605 /* Get information about the tool-bar item which is displayed in GLYPH
12606 on frame F. Return in *PROP_IDX the index where tool-bar item
12607 properties start in F->tool_bar_items. Value is zero if
12608 GLYPH doesn't display a tool-bar item. */
12609
12610 static int
12611 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12612 {
12613 Lisp_Object prop;
12614 int success_p;
12615 int charpos;
12616
12617 /* This function can be called asynchronously, which means we must
12618 exclude any possibility that Fget_text_property signals an
12619 error. */
12620 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12621 charpos = max (0, charpos);
12622
12623 /* Get the text property `menu-item' at pos. The value of that
12624 property is the start index of this item's properties in
12625 F->tool_bar_items. */
12626 prop = Fget_text_property (make_number (charpos),
12627 Qmenu_item, f->current_tool_bar_string);
12628 if (INTEGERP (prop))
12629 {
12630 *prop_idx = XINT (prop);
12631 success_p = 1;
12632 }
12633 else
12634 success_p = 0;
12635
12636 return success_p;
12637 }
12638
12639 \f
12640 /* Get information about the tool-bar item at position X/Y on frame F.
12641 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12642 the current matrix of the tool-bar window of F, or NULL if not
12643 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12644 item in F->tool_bar_items. Value is
12645
12646 -1 if X/Y is not on a tool-bar item
12647 0 if X/Y is on the same item that was highlighted before.
12648 1 otherwise. */
12649
12650 static int
12651 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12652 int *hpos, int *vpos, int *prop_idx)
12653 {
12654 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12655 struct window *w = XWINDOW (f->tool_bar_window);
12656 int area;
12657
12658 /* Find the glyph under X/Y. */
12659 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12660 if (*glyph == NULL)
12661 return -1;
12662
12663 /* Get the start of this tool-bar item's properties in
12664 f->tool_bar_items. */
12665 if (!tool_bar_item_info (f, *glyph, prop_idx))
12666 return -1;
12667
12668 /* Is mouse on the highlighted item? */
12669 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12670 && *vpos >= hlinfo->mouse_face_beg_row
12671 && *vpos <= hlinfo->mouse_face_end_row
12672 && (*vpos > hlinfo->mouse_face_beg_row
12673 || *hpos >= hlinfo->mouse_face_beg_col)
12674 && (*vpos < hlinfo->mouse_face_end_row
12675 || *hpos < hlinfo->mouse_face_end_col
12676 || hlinfo->mouse_face_past_end))
12677 return 0;
12678
12679 return 1;
12680 }
12681
12682
12683 /* EXPORT:
12684 Handle mouse button event on the tool-bar of frame F, at
12685 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12686 0 for button release. MODIFIERS is event modifiers for button
12687 release. */
12688
12689 void
12690 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12691 int modifiers)
12692 {
12693 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12694 struct window *w = XWINDOW (f->tool_bar_window);
12695 int hpos, vpos, prop_idx;
12696 struct glyph *glyph;
12697 Lisp_Object enabled_p;
12698 int ts;
12699
12700 /* If not on the highlighted tool-bar item, and mouse-highlight is
12701 non-nil, return. This is so we generate the tool-bar button
12702 click only when the mouse button is released on the same item as
12703 where it was pressed. However, when mouse-highlight is disabled,
12704 generate the click when the button is released regardless of the
12705 highlight, since tool-bar items are not highlighted in that
12706 case. */
12707 frame_to_window_pixel_xy (w, &x, &y);
12708 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12709 if (ts == -1
12710 || (ts != 0 && !NILP (Vmouse_highlight)))
12711 return;
12712
12713 /* When mouse-highlight is off, generate the click for the item
12714 where the button was pressed, disregarding where it was
12715 released. */
12716 if (NILP (Vmouse_highlight) && !down_p)
12717 prop_idx = last_tool_bar_item;
12718
12719 /* If item is disabled, do nothing. */
12720 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12721 if (NILP (enabled_p))
12722 return;
12723
12724 if (down_p)
12725 {
12726 /* Show item in pressed state. */
12727 if (!NILP (Vmouse_highlight))
12728 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12729 last_tool_bar_item = prop_idx;
12730 }
12731 else
12732 {
12733 Lisp_Object key, frame;
12734 struct input_event event;
12735 EVENT_INIT (event);
12736
12737 /* Show item in released state. */
12738 if (!NILP (Vmouse_highlight))
12739 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12740
12741 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12742
12743 XSETFRAME (frame, f);
12744 event.kind = TOOL_BAR_EVENT;
12745 event.frame_or_window = frame;
12746 event.arg = frame;
12747 kbd_buffer_store_event (&event);
12748
12749 event.kind = TOOL_BAR_EVENT;
12750 event.frame_or_window = frame;
12751 event.arg = key;
12752 event.modifiers = modifiers;
12753 kbd_buffer_store_event (&event);
12754 last_tool_bar_item = -1;
12755 }
12756 }
12757
12758
12759 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12760 tool-bar window-relative coordinates X/Y. Called from
12761 note_mouse_highlight. */
12762
12763 static void
12764 note_tool_bar_highlight (struct frame *f, int x, int y)
12765 {
12766 Lisp_Object window = f->tool_bar_window;
12767 struct window *w = XWINDOW (window);
12768 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12769 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12770 int hpos, vpos;
12771 struct glyph *glyph;
12772 struct glyph_row *row;
12773 int i;
12774 Lisp_Object enabled_p;
12775 int prop_idx;
12776 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12777 int mouse_down_p, rc;
12778
12779 /* Function note_mouse_highlight is called with negative X/Y
12780 values when mouse moves outside of the frame. */
12781 if (x <= 0 || y <= 0)
12782 {
12783 clear_mouse_face (hlinfo);
12784 return;
12785 }
12786
12787 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12788 if (rc < 0)
12789 {
12790 /* Not on tool-bar item. */
12791 clear_mouse_face (hlinfo);
12792 return;
12793 }
12794 else if (rc == 0)
12795 /* On same tool-bar item as before. */
12796 goto set_help_echo;
12797
12798 clear_mouse_face (hlinfo);
12799
12800 /* Mouse is down, but on different tool-bar item? */
12801 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12802 && f == dpyinfo->last_mouse_frame);
12803
12804 if (mouse_down_p
12805 && last_tool_bar_item != prop_idx)
12806 return;
12807
12808 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12809
12810 /* If tool-bar item is not enabled, don't highlight it. */
12811 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12812 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12813 {
12814 /* Compute the x-position of the glyph. In front and past the
12815 image is a space. We include this in the highlighted area. */
12816 row = MATRIX_ROW (w->current_matrix, vpos);
12817 for (i = x = 0; i < hpos; ++i)
12818 x += row->glyphs[TEXT_AREA][i].pixel_width;
12819
12820 /* Record this as the current active region. */
12821 hlinfo->mouse_face_beg_col = hpos;
12822 hlinfo->mouse_face_beg_row = vpos;
12823 hlinfo->mouse_face_beg_x = x;
12824 hlinfo->mouse_face_past_end = 0;
12825
12826 hlinfo->mouse_face_end_col = hpos + 1;
12827 hlinfo->mouse_face_end_row = vpos;
12828 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12829 hlinfo->mouse_face_window = window;
12830 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12831
12832 /* Display it as active. */
12833 show_mouse_face (hlinfo, draw);
12834 }
12835
12836 set_help_echo:
12837
12838 /* Set help_echo_string to a help string to display for this tool-bar item.
12839 XTread_socket does the rest. */
12840 help_echo_object = help_echo_window = Qnil;
12841 help_echo_pos = -1;
12842 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12843 if (NILP (help_echo_string))
12844 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12845 }
12846
12847 #endif /* !USE_GTK && !HAVE_NS */
12848
12849 #endif /* HAVE_WINDOW_SYSTEM */
12850
12851
12852 \f
12853 /************************************************************************
12854 Horizontal scrolling
12855 ************************************************************************/
12856
12857 static int hscroll_window_tree (Lisp_Object);
12858 static int hscroll_windows (Lisp_Object);
12859
12860 /* For all leaf windows in the window tree rooted at WINDOW, set their
12861 hscroll value so that PT is (i) visible in the window, and (ii) so
12862 that it is not within a certain margin at the window's left and
12863 right border. Value is non-zero if any window's hscroll has been
12864 changed. */
12865
12866 static int
12867 hscroll_window_tree (Lisp_Object window)
12868 {
12869 int hscrolled_p = 0;
12870 int hscroll_relative_p = FLOATP (Vhscroll_step);
12871 int hscroll_step_abs = 0;
12872 double hscroll_step_rel = 0;
12873
12874 if (hscroll_relative_p)
12875 {
12876 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12877 if (hscroll_step_rel < 0)
12878 {
12879 hscroll_relative_p = 0;
12880 hscroll_step_abs = 0;
12881 }
12882 }
12883 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12884 {
12885 hscroll_step_abs = XINT (Vhscroll_step);
12886 if (hscroll_step_abs < 0)
12887 hscroll_step_abs = 0;
12888 }
12889 else
12890 hscroll_step_abs = 0;
12891
12892 while (WINDOWP (window))
12893 {
12894 struct window *w = XWINDOW (window);
12895
12896 if (WINDOWP (w->contents))
12897 hscrolled_p |= hscroll_window_tree (w->contents);
12898 else if (w->cursor.vpos >= 0)
12899 {
12900 int h_margin;
12901 int text_area_width;
12902 struct glyph_row *cursor_row;
12903 struct glyph_row *bottom_row;
12904 int row_r2l_p;
12905
12906 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12907 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12908 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12909 else
12910 cursor_row = bottom_row - 1;
12911
12912 if (!cursor_row->enabled_p)
12913 {
12914 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12915 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12916 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12917 else
12918 cursor_row = bottom_row - 1;
12919 }
12920 row_r2l_p = cursor_row->reversed_p;
12921
12922 text_area_width = window_box_width (w, TEXT_AREA);
12923
12924 /* Scroll when cursor is inside this scroll margin. */
12925 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12926
12927 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12928 /* In some pathological cases, like restoring a window
12929 configuration into a frame that is much smaller than
12930 the one from which the configuration was saved, we
12931 get glyph rows whose start and end have zero buffer
12932 positions, which we cannot handle below. Just skip
12933 such windows. */
12934 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12935 /* For left-to-right rows, hscroll when cursor is either
12936 (i) inside the right hscroll margin, or (ii) if it is
12937 inside the left margin and the window is already
12938 hscrolled. */
12939 && ((!row_r2l_p
12940 && ((w->hscroll
12941 && w->cursor.x <= h_margin)
12942 || (cursor_row->enabled_p
12943 && cursor_row->truncated_on_right_p
12944 && (w->cursor.x >= text_area_width - h_margin))))
12945 /* For right-to-left rows, the logic is similar,
12946 except that rules for scrolling to left and right
12947 are reversed. E.g., if cursor.x <= h_margin, we
12948 need to hscroll "to the right" unconditionally,
12949 and that will scroll the screen to the left so as
12950 to reveal the next portion of the row. */
12951 || (row_r2l_p
12952 && ((cursor_row->enabled_p
12953 /* FIXME: It is confusing to set the
12954 truncated_on_right_p flag when R2L rows
12955 are actually truncated on the left. */
12956 && cursor_row->truncated_on_right_p
12957 && w->cursor.x <= h_margin)
12958 || (w->hscroll
12959 && (w->cursor.x >= text_area_width - h_margin))))))
12960 {
12961 struct it it;
12962 ptrdiff_t hscroll;
12963 struct buffer *saved_current_buffer;
12964 ptrdiff_t pt;
12965 int wanted_x;
12966
12967 /* Find point in a display of infinite width. */
12968 saved_current_buffer = current_buffer;
12969 current_buffer = XBUFFER (w->contents);
12970
12971 if (w == XWINDOW (selected_window))
12972 pt = PT;
12973 else
12974 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12975
12976 /* Move iterator to pt starting at cursor_row->start in
12977 a line with infinite width. */
12978 init_to_row_start (&it, w, cursor_row);
12979 it.last_visible_x = INFINITY;
12980 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12981 current_buffer = saved_current_buffer;
12982
12983 /* Position cursor in window. */
12984 if (!hscroll_relative_p && hscroll_step_abs == 0)
12985 hscroll = max (0, (it.current_x
12986 - (ITERATOR_AT_END_OF_LINE_P (&it)
12987 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12988 : (text_area_width / 2))))
12989 / FRAME_COLUMN_WIDTH (it.f);
12990 else if ((!row_r2l_p
12991 && w->cursor.x >= text_area_width - h_margin)
12992 || (row_r2l_p && w->cursor.x <= h_margin))
12993 {
12994 if (hscroll_relative_p)
12995 wanted_x = text_area_width * (1 - hscroll_step_rel)
12996 - h_margin;
12997 else
12998 wanted_x = text_area_width
12999 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13000 - h_margin;
13001 hscroll
13002 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13003 }
13004 else
13005 {
13006 if (hscroll_relative_p)
13007 wanted_x = text_area_width * hscroll_step_rel
13008 + h_margin;
13009 else
13010 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13011 + h_margin;
13012 hscroll
13013 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13014 }
13015 hscroll = max (hscroll, w->min_hscroll);
13016
13017 /* Don't prevent redisplay optimizations if hscroll
13018 hasn't changed, as it will unnecessarily slow down
13019 redisplay. */
13020 if (w->hscroll != hscroll)
13021 {
13022 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
13023 w->hscroll = hscroll;
13024 hscrolled_p = 1;
13025 }
13026 }
13027 }
13028
13029 window = w->next;
13030 }
13031
13032 /* Value is non-zero if hscroll of any leaf window has been changed. */
13033 return hscrolled_p;
13034 }
13035
13036
13037 /* Set hscroll so that cursor is visible and not inside horizontal
13038 scroll margins for all windows in the tree rooted at WINDOW. See
13039 also hscroll_window_tree above. Value is non-zero if any window's
13040 hscroll has been changed. If it has, desired matrices on the frame
13041 of WINDOW are cleared. */
13042
13043 static int
13044 hscroll_windows (Lisp_Object window)
13045 {
13046 int hscrolled_p = hscroll_window_tree (window);
13047 if (hscrolled_p)
13048 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13049 return hscrolled_p;
13050 }
13051
13052
13053 \f
13054 /************************************************************************
13055 Redisplay
13056 ************************************************************************/
13057
13058 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
13059 to a non-zero value. This is sometimes handy to have in a debugger
13060 session. */
13061
13062 #ifdef GLYPH_DEBUG
13063
13064 /* First and last unchanged row for try_window_id. */
13065
13066 static int debug_first_unchanged_at_end_vpos;
13067 static int debug_last_unchanged_at_beg_vpos;
13068
13069 /* Delta vpos and y. */
13070
13071 static int debug_dvpos, debug_dy;
13072
13073 /* Delta in characters and bytes for try_window_id. */
13074
13075 static ptrdiff_t debug_delta, debug_delta_bytes;
13076
13077 /* Values of window_end_pos and window_end_vpos at the end of
13078 try_window_id. */
13079
13080 static ptrdiff_t debug_end_vpos;
13081
13082 /* Append a string to W->desired_matrix->method. FMT is a printf
13083 format string. If trace_redisplay_p is true also printf the
13084 resulting string to stderr. */
13085
13086 static void debug_method_add (struct window *, char const *, ...)
13087 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13088
13089 static void
13090 debug_method_add (struct window *w, char const *fmt, ...)
13091 {
13092 void *ptr = w;
13093 char *method = w->desired_matrix->method;
13094 int len = strlen (method);
13095 int size = sizeof w->desired_matrix->method;
13096 int remaining = size - len - 1;
13097 va_list ap;
13098
13099 if (len && remaining)
13100 {
13101 method[len] = '|';
13102 --remaining, ++len;
13103 }
13104
13105 va_start (ap, fmt);
13106 vsnprintf (method + len, remaining + 1, fmt, ap);
13107 va_end (ap);
13108
13109 if (trace_redisplay_p)
13110 fprintf (stderr, "%p (%s): %s\n",
13111 ptr,
13112 ((BUFFERP (w->contents)
13113 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13114 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13115 : "no buffer"),
13116 method + len);
13117 }
13118
13119 #endif /* GLYPH_DEBUG */
13120
13121
13122 /* Value is non-zero if all changes in window W, which displays
13123 current_buffer, are in the text between START and END. START is a
13124 buffer position, END is given as a distance from Z. Used in
13125 redisplay_internal for display optimization. */
13126
13127 static int
13128 text_outside_line_unchanged_p (struct window *w,
13129 ptrdiff_t start, ptrdiff_t end)
13130 {
13131 int unchanged_p = 1;
13132
13133 /* If text or overlays have changed, see where. */
13134 if (window_outdated (w))
13135 {
13136 /* Gap in the line? */
13137 if (GPT < start || Z - GPT < end)
13138 unchanged_p = 0;
13139
13140 /* Changes start in front of the line, or end after it? */
13141 if (unchanged_p
13142 && (BEG_UNCHANGED < start - 1
13143 || END_UNCHANGED < end))
13144 unchanged_p = 0;
13145
13146 /* If selective display, can't optimize if changes start at the
13147 beginning of the line. */
13148 if (unchanged_p
13149 && INTEGERP (BVAR (current_buffer, selective_display))
13150 && XINT (BVAR (current_buffer, selective_display)) > 0
13151 && (BEG_UNCHANGED < start || GPT <= start))
13152 unchanged_p = 0;
13153
13154 /* If there are overlays at the start or end of the line, these
13155 may have overlay strings with newlines in them. A change at
13156 START, for instance, may actually concern the display of such
13157 overlay strings as well, and they are displayed on different
13158 lines. So, quickly rule out this case. (For the future, it
13159 might be desirable to implement something more telling than
13160 just BEG/END_UNCHANGED.) */
13161 if (unchanged_p)
13162 {
13163 if (BEG + BEG_UNCHANGED == start
13164 && overlay_touches_p (start))
13165 unchanged_p = 0;
13166 if (END_UNCHANGED == end
13167 && overlay_touches_p (Z - end))
13168 unchanged_p = 0;
13169 }
13170
13171 /* Under bidi reordering, adding or deleting a character in the
13172 beginning of a paragraph, before the first strong directional
13173 character, can change the base direction of the paragraph (unless
13174 the buffer specifies a fixed paragraph direction), which will
13175 require to redisplay the whole paragraph. It might be worthwhile
13176 to find the paragraph limits and widen the range of redisplayed
13177 lines to that, but for now just give up this optimization. */
13178 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13179 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13180 unchanged_p = 0;
13181 }
13182
13183 return unchanged_p;
13184 }
13185
13186
13187 /* Do a frame update, taking possible shortcuts into account. This is
13188 the main external entry point for redisplay.
13189
13190 If the last redisplay displayed an echo area message and that message
13191 is no longer requested, we clear the echo area or bring back the
13192 mini-buffer if that is in use. */
13193
13194 void
13195 redisplay (void)
13196 {
13197 redisplay_internal ();
13198 }
13199
13200
13201 static Lisp_Object
13202 overlay_arrow_string_or_property (Lisp_Object var)
13203 {
13204 Lisp_Object val;
13205
13206 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13207 return val;
13208
13209 return Voverlay_arrow_string;
13210 }
13211
13212 /* Return 1 if there are any overlay-arrows in current_buffer. */
13213 static int
13214 overlay_arrow_in_current_buffer_p (void)
13215 {
13216 Lisp_Object vlist;
13217
13218 for (vlist = Voverlay_arrow_variable_list;
13219 CONSP (vlist);
13220 vlist = XCDR (vlist))
13221 {
13222 Lisp_Object var = XCAR (vlist);
13223 Lisp_Object val;
13224
13225 if (!SYMBOLP (var))
13226 continue;
13227 val = find_symbol_value (var);
13228 if (MARKERP (val)
13229 && current_buffer == XMARKER (val)->buffer)
13230 return 1;
13231 }
13232 return 0;
13233 }
13234
13235
13236 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13237 has changed. */
13238
13239 static int
13240 overlay_arrows_changed_p (void)
13241 {
13242 Lisp_Object vlist;
13243
13244 for (vlist = Voverlay_arrow_variable_list;
13245 CONSP (vlist);
13246 vlist = XCDR (vlist))
13247 {
13248 Lisp_Object var = XCAR (vlist);
13249 Lisp_Object val, pstr;
13250
13251 if (!SYMBOLP (var))
13252 continue;
13253 val = find_symbol_value (var);
13254 if (!MARKERP (val))
13255 continue;
13256 if (! EQ (COERCE_MARKER (val),
13257 Fget (var, Qlast_arrow_position))
13258 || ! (pstr = overlay_arrow_string_or_property (var),
13259 EQ (pstr, Fget (var, Qlast_arrow_string))))
13260 return 1;
13261 }
13262 return 0;
13263 }
13264
13265 /* Mark overlay arrows to be updated on next redisplay. */
13266
13267 static void
13268 update_overlay_arrows (int up_to_date)
13269 {
13270 Lisp_Object vlist;
13271
13272 for (vlist = Voverlay_arrow_variable_list;
13273 CONSP (vlist);
13274 vlist = XCDR (vlist))
13275 {
13276 Lisp_Object var = XCAR (vlist);
13277
13278 if (!SYMBOLP (var))
13279 continue;
13280
13281 if (up_to_date > 0)
13282 {
13283 Lisp_Object val = find_symbol_value (var);
13284 Fput (var, Qlast_arrow_position,
13285 COERCE_MARKER (val));
13286 Fput (var, Qlast_arrow_string,
13287 overlay_arrow_string_or_property (var));
13288 }
13289 else if (up_to_date < 0
13290 || !NILP (Fget (var, Qlast_arrow_position)))
13291 {
13292 Fput (var, Qlast_arrow_position, Qt);
13293 Fput (var, Qlast_arrow_string, Qt);
13294 }
13295 }
13296 }
13297
13298
13299 /* Return overlay arrow string to display at row.
13300 Return integer (bitmap number) for arrow bitmap in left fringe.
13301 Return nil if no overlay arrow. */
13302
13303 static Lisp_Object
13304 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13305 {
13306 Lisp_Object vlist;
13307
13308 for (vlist = Voverlay_arrow_variable_list;
13309 CONSP (vlist);
13310 vlist = XCDR (vlist))
13311 {
13312 Lisp_Object var = XCAR (vlist);
13313 Lisp_Object val;
13314
13315 if (!SYMBOLP (var))
13316 continue;
13317
13318 val = find_symbol_value (var);
13319
13320 if (MARKERP (val)
13321 && current_buffer == XMARKER (val)->buffer
13322 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13323 {
13324 if (FRAME_WINDOW_P (it->f)
13325 /* FIXME: if ROW->reversed_p is set, this should test
13326 the right fringe, not the left one. */
13327 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13328 {
13329 #ifdef HAVE_WINDOW_SYSTEM
13330 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13331 {
13332 int fringe_bitmap;
13333 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13334 return make_number (fringe_bitmap);
13335 }
13336 #endif
13337 return make_number (-1); /* Use default arrow bitmap. */
13338 }
13339 return overlay_arrow_string_or_property (var);
13340 }
13341 }
13342
13343 return Qnil;
13344 }
13345
13346 /* Return 1 if point moved out of or into a composition. Otherwise
13347 return 0. PREV_BUF and PREV_PT are the last point buffer and
13348 position. BUF and PT are the current point buffer and position. */
13349
13350 static int
13351 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13352 struct buffer *buf, ptrdiff_t pt)
13353 {
13354 ptrdiff_t start, end;
13355 Lisp_Object prop;
13356 Lisp_Object buffer;
13357
13358 XSETBUFFER (buffer, buf);
13359 /* Check a composition at the last point if point moved within the
13360 same buffer. */
13361 if (prev_buf == buf)
13362 {
13363 if (prev_pt == pt)
13364 /* Point didn't move. */
13365 return 0;
13366
13367 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13368 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13369 && composition_valid_p (start, end, prop)
13370 && start < prev_pt && end > prev_pt)
13371 /* The last point was within the composition. Return 1 iff
13372 point moved out of the composition. */
13373 return (pt <= start || pt >= end);
13374 }
13375
13376 /* Check a composition at the current point. */
13377 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13378 && find_composition (pt, -1, &start, &end, &prop, buffer)
13379 && composition_valid_p (start, end, prop)
13380 && start < pt && end > pt);
13381 }
13382
13383 /* Reconsider the clip changes of buffer which is displayed in W. */
13384
13385 static void
13386 reconsider_clip_changes (struct window *w)
13387 {
13388 struct buffer *b = XBUFFER (w->contents);
13389
13390 if (b->clip_changed
13391 && w->window_end_valid
13392 && w->current_matrix->buffer == b
13393 && w->current_matrix->zv == BUF_ZV (b)
13394 && w->current_matrix->begv == BUF_BEGV (b))
13395 b->clip_changed = 0;
13396
13397 /* If display wasn't paused, and W is not a tool bar window, see if
13398 point has been moved into or out of a composition. In that case,
13399 we set b->clip_changed to 1 to force updating the screen. If
13400 b->clip_changed has already been set to 1, we can skip this
13401 check. */
13402 if (!b->clip_changed && w->window_end_valid)
13403 {
13404 ptrdiff_t pt = (w == XWINDOW (selected_window)
13405 ? PT : marker_position (w->pointm));
13406
13407 if ((w->current_matrix->buffer != b || pt != w->last_point)
13408 && check_point_in_composition (w->current_matrix->buffer,
13409 w->last_point, b, pt))
13410 b->clip_changed = 1;
13411 }
13412 }
13413
13414 static void
13415 propagate_buffer_redisplay (void)
13416 { /* Resetting b->text->redisplay is problematic!
13417 We can't just reset it in the case that some window that displays
13418 it has not been redisplayed; and such a window can stay
13419 unredisplayed for a long time if it's currently invisible.
13420 But we do want to reset it at the end of redisplay otherwise
13421 its displayed windows will keep being redisplayed over and over
13422 again.
13423 So we copy all b->text->redisplay flags up to their windows here,
13424 such that mark_window_display_accurate can safely reset
13425 b->text->redisplay. */
13426 Lisp_Object ws = window_list ();
13427 for (; CONSP (ws); ws = XCDR (ws))
13428 {
13429 struct window *thisw = XWINDOW (XCAR (ws));
13430 struct buffer *thisb = XBUFFER (thisw->contents);
13431 if (thisb->text->redisplay)
13432 thisw->redisplay = true;
13433 }
13434 }
13435
13436 #define STOP_POLLING \
13437 do { if (! polling_stopped_here) stop_polling (); \
13438 polling_stopped_here = 1; } while (0)
13439
13440 #define RESUME_POLLING \
13441 do { if (polling_stopped_here) start_polling (); \
13442 polling_stopped_here = 0; } while (0)
13443
13444
13445 /* Perhaps in the future avoid recentering windows if it
13446 is not necessary; currently that causes some problems. */
13447
13448 static void
13449 redisplay_internal (void)
13450 {
13451 struct window *w = XWINDOW (selected_window);
13452 struct window *sw;
13453 struct frame *fr;
13454 int pending;
13455 bool must_finish = 0, match_p;
13456 struct text_pos tlbufpos, tlendpos;
13457 int number_of_visible_frames;
13458 ptrdiff_t count;
13459 struct frame *sf;
13460 int polling_stopped_here = 0;
13461 Lisp_Object tail, frame;
13462
13463 /* True means redisplay has to consider all windows on all
13464 frames. False, only selected_window is considered. */
13465 bool consider_all_windows_p;
13466
13467 /* True means redisplay has to redisplay the miniwindow. */
13468 bool update_miniwindow_p = false;
13469
13470 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13471
13472 /* No redisplay if running in batch mode or frame is not yet fully
13473 initialized, or redisplay is explicitly turned off by setting
13474 Vinhibit_redisplay. */
13475 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13476 || !NILP (Vinhibit_redisplay))
13477 return;
13478
13479 /* Don't examine these until after testing Vinhibit_redisplay.
13480 When Emacs is shutting down, perhaps because its connection to
13481 X has dropped, we should not look at them at all. */
13482 fr = XFRAME (w->frame);
13483 sf = SELECTED_FRAME ();
13484
13485 if (!fr->glyphs_initialized_p)
13486 return;
13487
13488 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13489 if (popup_activated ())
13490 return;
13491 #endif
13492
13493 /* I don't think this happens but let's be paranoid. */
13494 if (redisplaying_p)
13495 return;
13496
13497 /* Record a function that clears redisplaying_p
13498 when we leave this function. */
13499 count = SPECPDL_INDEX ();
13500 record_unwind_protect_void (unwind_redisplay);
13501 redisplaying_p = 1;
13502 specbind (Qinhibit_free_realized_faces, Qnil);
13503
13504 /* Record this function, so it appears on the profiler's backtraces. */
13505 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13506
13507 FOR_EACH_FRAME (tail, frame)
13508 XFRAME (frame)->already_hscrolled_p = 0;
13509
13510 retry:
13511 /* Remember the currently selected window. */
13512 sw = w;
13513
13514 pending = 0;
13515 last_escape_glyph_frame = NULL;
13516 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13517 last_glyphless_glyph_frame = NULL;
13518 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13519
13520 /* If face_change_count is non-zero, init_iterator will free all
13521 realized faces, which includes the faces referenced from current
13522 matrices. So, we can't reuse current matrices in this case. */
13523 if (face_change_count)
13524 windows_or_buffers_changed = 47;
13525
13526 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13527 && FRAME_TTY (sf)->previous_frame != sf)
13528 {
13529 /* Since frames on a single ASCII terminal share the same
13530 display area, displaying a different frame means redisplay
13531 the whole thing. */
13532 SET_FRAME_GARBAGED (sf);
13533 #ifndef DOS_NT
13534 set_tty_color_mode (FRAME_TTY (sf), sf);
13535 #endif
13536 FRAME_TTY (sf)->previous_frame = sf;
13537 }
13538
13539 /* Set the visible flags for all frames. Do this before checking for
13540 resized or garbaged frames; they want to know if their frames are
13541 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13542 number_of_visible_frames = 0;
13543
13544 FOR_EACH_FRAME (tail, frame)
13545 {
13546 struct frame *f = XFRAME (frame);
13547
13548 if (FRAME_VISIBLE_P (f))
13549 {
13550 ++number_of_visible_frames;
13551 /* Adjust matrices for visible frames only. */
13552 if (f->fonts_changed)
13553 {
13554 adjust_frame_glyphs (f);
13555 f->fonts_changed = 0;
13556 }
13557 /* If cursor type has been changed on the frame
13558 other than selected, consider all frames. */
13559 if (f != sf && f->cursor_type_changed)
13560 update_mode_lines = 31;
13561 }
13562 clear_desired_matrices (f);
13563 }
13564
13565 /* Notice any pending interrupt request to change frame size. */
13566 do_pending_window_change (1);
13567
13568 /* do_pending_window_change could change the selected_window due to
13569 frame resizing which makes the selected window too small. */
13570 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13571 sw = w;
13572
13573 /* Clear frames marked as garbaged. */
13574 clear_garbaged_frames ();
13575
13576 /* Build menubar and tool-bar items. */
13577 if (NILP (Vmemory_full))
13578 prepare_menu_bars ();
13579
13580 reconsider_clip_changes (w);
13581
13582 /* In most cases selected window displays current buffer. */
13583 match_p = XBUFFER (w->contents) == current_buffer;
13584 if (match_p)
13585 {
13586 /* Detect case that we need to write or remove a star in the mode line. */
13587 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13588 w->update_mode_line = 1;
13589
13590 if (mode_line_update_needed (w))
13591 w->update_mode_line = 1;
13592 }
13593
13594 /* Normally the message* functions will have already displayed and
13595 updated the echo area, but the frame may have been trashed, or
13596 the update may have been preempted, so display the echo area
13597 again here. Checking message_cleared_p captures the case that
13598 the echo area should be cleared. */
13599 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13600 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13601 || (message_cleared_p
13602 && minibuf_level == 0
13603 /* If the mini-window is currently selected, this means the
13604 echo-area doesn't show through. */
13605 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13606 {
13607 int window_height_changed_p = echo_area_display (0);
13608
13609 if (message_cleared_p)
13610 update_miniwindow_p = true;
13611
13612 must_finish = 1;
13613
13614 /* If we don't display the current message, don't clear the
13615 message_cleared_p flag, because, if we did, we wouldn't clear
13616 the echo area in the next redisplay which doesn't preserve
13617 the echo area. */
13618 if (!display_last_displayed_message_p)
13619 message_cleared_p = 0;
13620
13621 if (window_height_changed_p)
13622 {
13623 windows_or_buffers_changed = 50;
13624
13625 /* If window configuration was changed, frames may have been
13626 marked garbaged. Clear them or we will experience
13627 surprises wrt scrolling. */
13628 clear_garbaged_frames ();
13629 }
13630 }
13631 else if (EQ (selected_window, minibuf_window)
13632 && (current_buffer->clip_changed || window_outdated (w))
13633 && resize_mini_window (w, 0))
13634 {
13635 /* Resized active mini-window to fit the size of what it is
13636 showing if its contents might have changed. */
13637 must_finish = 1;
13638
13639 /* If window configuration was changed, frames may have been
13640 marked garbaged. Clear them or we will experience
13641 surprises wrt scrolling. */
13642 clear_garbaged_frames ();
13643 }
13644
13645 if (windows_or_buffers_changed && !update_mode_lines)
13646 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13647 only the windows's contents needs to be refreshed, or whether the
13648 mode-lines also need a refresh. */
13649 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13650 ? REDISPLAY_SOME : 32);
13651
13652 /* If specs for an arrow have changed, do thorough redisplay
13653 to ensure we remove any arrow that should no longer exist. */
13654 if (overlay_arrows_changed_p ())
13655 /* Apparently, this is the only case where we update other windows,
13656 without updating other mode-lines. */
13657 windows_or_buffers_changed = 49;
13658
13659 consider_all_windows_p = (update_mode_lines
13660 || windows_or_buffers_changed);
13661
13662 #define AINC(a,i) \
13663 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13664 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13665
13666 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13667 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13668
13669 /* Optimize the case that only the line containing the cursor in the
13670 selected window has changed. Variables starting with this_ are
13671 set in display_line and record information about the line
13672 containing the cursor. */
13673 tlbufpos = this_line_start_pos;
13674 tlendpos = this_line_end_pos;
13675 if (!consider_all_windows_p
13676 && CHARPOS (tlbufpos) > 0
13677 && !w->update_mode_line
13678 && !current_buffer->clip_changed
13679 && !current_buffer->prevent_redisplay_optimizations_p
13680 && FRAME_VISIBLE_P (XFRAME (w->frame))
13681 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13682 && !XFRAME (w->frame)->cursor_type_changed
13683 /* Make sure recorded data applies to current buffer, etc. */
13684 && this_line_buffer == current_buffer
13685 && match_p
13686 && !w->force_start
13687 && !w->optional_new_start
13688 /* Point must be on the line that we have info recorded about. */
13689 && PT >= CHARPOS (tlbufpos)
13690 && PT <= Z - CHARPOS (tlendpos)
13691 /* All text outside that line, including its final newline,
13692 must be unchanged. */
13693 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13694 CHARPOS (tlendpos)))
13695 {
13696 if (CHARPOS (tlbufpos) > BEGV
13697 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13698 && (CHARPOS (tlbufpos) == ZV
13699 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13700 /* Former continuation line has disappeared by becoming empty. */
13701 goto cancel;
13702 else if (window_outdated (w) || MINI_WINDOW_P (w))
13703 {
13704 /* We have to handle the case of continuation around a
13705 wide-column character (see the comment in indent.c around
13706 line 1340).
13707
13708 For instance, in the following case:
13709
13710 -------- Insert --------
13711 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13712 J_I_ ==> J_I_ `^^' are cursors.
13713 ^^ ^^
13714 -------- --------
13715
13716 As we have to redraw the line above, we cannot use this
13717 optimization. */
13718
13719 struct it it;
13720 int line_height_before = this_line_pixel_height;
13721
13722 /* Note that start_display will handle the case that the
13723 line starting at tlbufpos is a continuation line. */
13724 start_display (&it, w, tlbufpos);
13725
13726 /* Implementation note: It this still necessary? */
13727 if (it.current_x != this_line_start_x)
13728 goto cancel;
13729
13730 TRACE ((stderr, "trying display optimization 1\n"));
13731 w->cursor.vpos = -1;
13732 overlay_arrow_seen = 0;
13733 it.vpos = this_line_vpos;
13734 it.current_y = this_line_y;
13735 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13736 display_line (&it);
13737
13738 /* If line contains point, is not continued,
13739 and ends at same distance from eob as before, we win. */
13740 if (w->cursor.vpos >= 0
13741 /* Line is not continued, otherwise this_line_start_pos
13742 would have been set to 0 in display_line. */
13743 && CHARPOS (this_line_start_pos)
13744 /* Line ends as before. */
13745 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13746 /* Line has same height as before. Otherwise other lines
13747 would have to be shifted up or down. */
13748 && this_line_pixel_height == line_height_before)
13749 {
13750 /* If this is not the window's last line, we must adjust
13751 the charstarts of the lines below. */
13752 if (it.current_y < it.last_visible_y)
13753 {
13754 struct glyph_row *row
13755 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13756 ptrdiff_t delta, delta_bytes;
13757
13758 /* We used to distinguish between two cases here,
13759 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13760 when the line ends in a newline or the end of the
13761 buffer's accessible portion. But both cases did
13762 the same, so they were collapsed. */
13763 delta = (Z
13764 - CHARPOS (tlendpos)
13765 - MATRIX_ROW_START_CHARPOS (row));
13766 delta_bytes = (Z_BYTE
13767 - BYTEPOS (tlendpos)
13768 - MATRIX_ROW_START_BYTEPOS (row));
13769
13770 increment_matrix_positions (w->current_matrix,
13771 this_line_vpos + 1,
13772 w->current_matrix->nrows,
13773 delta, delta_bytes);
13774 }
13775
13776 /* If this row displays text now but previously didn't,
13777 or vice versa, w->window_end_vpos may have to be
13778 adjusted. */
13779 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13780 {
13781 if (w->window_end_vpos < this_line_vpos)
13782 w->window_end_vpos = this_line_vpos;
13783 }
13784 else if (w->window_end_vpos == this_line_vpos
13785 && this_line_vpos > 0)
13786 w->window_end_vpos = this_line_vpos - 1;
13787 w->window_end_valid = 0;
13788
13789 /* Update hint: No need to try to scroll in update_window. */
13790 w->desired_matrix->no_scrolling_p = 1;
13791
13792 #ifdef GLYPH_DEBUG
13793 *w->desired_matrix->method = 0;
13794 debug_method_add (w, "optimization 1");
13795 #endif
13796 #ifdef HAVE_WINDOW_SYSTEM
13797 update_window_fringes (w, 0);
13798 #endif
13799 goto update;
13800 }
13801 else
13802 goto cancel;
13803 }
13804 else if (/* Cursor position hasn't changed. */
13805 PT == w->last_point
13806 /* Make sure the cursor was last displayed
13807 in this window. Otherwise we have to reposition it. */
13808
13809 /* PXW: Must be converted to pixels, probably. */
13810 && 0 <= w->cursor.vpos
13811 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13812 {
13813 if (!must_finish)
13814 {
13815 do_pending_window_change (1);
13816 /* If selected_window changed, redisplay again. */
13817 if (WINDOWP (selected_window)
13818 && (w = XWINDOW (selected_window)) != sw)
13819 goto retry;
13820
13821 /* We used to always goto end_of_redisplay here, but this
13822 isn't enough if we have a blinking cursor. */
13823 if (w->cursor_off_p == w->last_cursor_off_p)
13824 goto end_of_redisplay;
13825 }
13826 goto update;
13827 }
13828 /* If highlighting the region, or if the cursor is in the echo area,
13829 then we can't just move the cursor. */
13830 else if (NILP (Vshow_trailing_whitespace)
13831 && !cursor_in_echo_area)
13832 {
13833 struct it it;
13834 struct glyph_row *row;
13835
13836 /* Skip from tlbufpos to PT and see where it is. Note that
13837 PT may be in invisible text. If so, we will end at the
13838 next visible position. */
13839 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13840 NULL, DEFAULT_FACE_ID);
13841 it.current_x = this_line_start_x;
13842 it.current_y = this_line_y;
13843 it.vpos = this_line_vpos;
13844
13845 /* The call to move_it_to stops in front of PT, but
13846 moves over before-strings. */
13847 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13848
13849 if (it.vpos == this_line_vpos
13850 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13851 row->enabled_p))
13852 {
13853 eassert (this_line_vpos == it.vpos);
13854 eassert (this_line_y == it.current_y);
13855 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13856 #ifdef GLYPH_DEBUG
13857 *w->desired_matrix->method = 0;
13858 debug_method_add (w, "optimization 3");
13859 #endif
13860 goto update;
13861 }
13862 else
13863 goto cancel;
13864 }
13865
13866 cancel:
13867 /* Text changed drastically or point moved off of line. */
13868 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13869 }
13870
13871 CHARPOS (this_line_start_pos) = 0;
13872 ++clear_face_cache_count;
13873 #ifdef HAVE_WINDOW_SYSTEM
13874 ++clear_image_cache_count;
13875 #endif
13876
13877 /* Build desired matrices, and update the display. If
13878 consider_all_windows_p is non-zero, do it for all windows on all
13879 frames. Otherwise do it for selected_window, only. */
13880
13881 if (consider_all_windows_p)
13882 {
13883 FOR_EACH_FRAME (tail, frame)
13884 XFRAME (frame)->updated_p = 0;
13885
13886 propagate_buffer_redisplay ();
13887
13888 FOR_EACH_FRAME (tail, frame)
13889 {
13890 struct frame *f = XFRAME (frame);
13891
13892 /* We don't have to do anything for unselected terminal
13893 frames. */
13894 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13895 && !EQ (FRAME_TTY (f)->top_frame, frame))
13896 continue;
13897
13898 retry_frame:
13899
13900 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13901 {
13902 bool gcscrollbars
13903 /* Only GC scrollbars when we redisplay the whole frame. */
13904 = f->redisplay || !REDISPLAY_SOME_P ();
13905 /* Mark all the scroll bars to be removed; we'll redeem
13906 the ones we want when we redisplay their windows. */
13907 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13908 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13909
13910 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13911 redisplay_windows (FRAME_ROOT_WINDOW (f));
13912 /* Remember that the invisible frames need to be redisplayed next
13913 time they're visible. */
13914 else if (!REDISPLAY_SOME_P ())
13915 f->redisplay = true;
13916
13917 /* The X error handler may have deleted that frame. */
13918 if (!FRAME_LIVE_P (f))
13919 continue;
13920
13921 /* Any scroll bars which redisplay_windows should have
13922 nuked should now go away. */
13923 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13924 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13925
13926 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13927 {
13928 /* If fonts changed on visible frame, display again. */
13929 if (f->fonts_changed)
13930 {
13931 adjust_frame_glyphs (f);
13932 f->fonts_changed = 0;
13933 goto retry_frame;
13934 }
13935
13936 /* See if we have to hscroll. */
13937 if (!f->already_hscrolled_p)
13938 {
13939 f->already_hscrolled_p = 1;
13940 if (hscroll_windows (f->root_window))
13941 goto retry_frame;
13942 }
13943
13944 /* Prevent various kinds of signals during display
13945 update. stdio is not robust about handling
13946 signals, which can cause an apparent I/O error. */
13947 if (interrupt_input)
13948 unrequest_sigio ();
13949 STOP_POLLING;
13950
13951 pending |= update_frame (f, 0, 0);
13952 f->cursor_type_changed = 0;
13953 f->updated_p = 1;
13954 }
13955 }
13956 }
13957
13958 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13959
13960 if (!pending)
13961 {
13962 /* Do the mark_window_display_accurate after all windows have
13963 been redisplayed because this call resets flags in buffers
13964 which are needed for proper redisplay. */
13965 FOR_EACH_FRAME (tail, frame)
13966 {
13967 struct frame *f = XFRAME (frame);
13968 if (f->updated_p)
13969 {
13970 f->redisplay = false;
13971 mark_window_display_accurate (f->root_window, 1);
13972 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13973 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13974 }
13975 }
13976 }
13977 }
13978 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13979 {
13980 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13981 struct frame *mini_frame;
13982
13983 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13984 /* Use list_of_error, not Qerror, so that
13985 we catch only errors and don't run the debugger. */
13986 internal_condition_case_1 (redisplay_window_1, selected_window,
13987 list_of_error,
13988 redisplay_window_error);
13989 if (update_miniwindow_p)
13990 internal_condition_case_1 (redisplay_window_1, mini_window,
13991 list_of_error,
13992 redisplay_window_error);
13993
13994 /* Compare desired and current matrices, perform output. */
13995
13996 update:
13997 /* If fonts changed, display again. */
13998 if (sf->fonts_changed)
13999 goto retry;
14000
14001 /* Prevent various kinds of signals during display update.
14002 stdio is not robust about handling signals,
14003 which can cause an apparent I/O error. */
14004 if (interrupt_input)
14005 unrequest_sigio ();
14006 STOP_POLLING;
14007
14008 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14009 {
14010 if (hscroll_windows (selected_window))
14011 goto retry;
14012
14013 XWINDOW (selected_window)->must_be_updated_p = true;
14014 pending = update_frame (sf, 0, 0);
14015 sf->cursor_type_changed = 0;
14016 }
14017
14018 /* We may have called echo_area_display at the top of this
14019 function. If the echo area is on another frame, that may
14020 have put text on a frame other than the selected one, so the
14021 above call to update_frame would not have caught it. Catch
14022 it here. */
14023 mini_window = FRAME_MINIBUF_WINDOW (sf);
14024 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14025
14026 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14027 {
14028 XWINDOW (mini_window)->must_be_updated_p = true;
14029 pending |= update_frame (mini_frame, 0, 0);
14030 mini_frame->cursor_type_changed = 0;
14031 if (!pending && hscroll_windows (mini_window))
14032 goto retry;
14033 }
14034 }
14035
14036 /* If display was paused because of pending input, make sure we do a
14037 thorough update the next time. */
14038 if (pending)
14039 {
14040 /* Prevent the optimization at the beginning of
14041 redisplay_internal that tries a single-line update of the
14042 line containing the cursor in the selected window. */
14043 CHARPOS (this_line_start_pos) = 0;
14044
14045 /* Let the overlay arrow be updated the next time. */
14046 update_overlay_arrows (0);
14047
14048 /* If we pause after scrolling, some rows in the current
14049 matrices of some windows are not valid. */
14050 if (!WINDOW_FULL_WIDTH_P (w)
14051 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14052 update_mode_lines = 36;
14053 }
14054 else
14055 {
14056 if (!consider_all_windows_p)
14057 {
14058 /* This has already been done above if
14059 consider_all_windows_p is set. */
14060 if (XBUFFER (w->contents)->text->redisplay
14061 && buffer_window_count (XBUFFER (w->contents)) > 1)
14062 /* This can happen if b->text->redisplay was set during
14063 jit-lock. */
14064 propagate_buffer_redisplay ();
14065 mark_window_display_accurate_1 (w, 1);
14066
14067 /* Say overlay arrows are up to date. */
14068 update_overlay_arrows (1);
14069
14070 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14071 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14072 }
14073
14074 update_mode_lines = 0;
14075 windows_or_buffers_changed = 0;
14076 }
14077
14078 /* Start SIGIO interrupts coming again. Having them off during the
14079 code above makes it less likely one will discard output, but not
14080 impossible, since there might be stuff in the system buffer here.
14081 But it is much hairier to try to do anything about that. */
14082 if (interrupt_input)
14083 request_sigio ();
14084 RESUME_POLLING;
14085
14086 /* If a frame has become visible which was not before, redisplay
14087 again, so that we display it. Expose events for such a frame
14088 (which it gets when becoming visible) don't call the parts of
14089 redisplay constructing glyphs, so simply exposing a frame won't
14090 display anything in this case. So, we have to display these
14091 frames here explicitly. */
14092 if (!pending)
14093 {
14094 int new_count = 0;
14095
14096 FOR_EACH_FRAME (tail, frame)
14097 {
14098 if (XFRAME (frame)->visible)
14099 new_count++;
14100 }
14101
14102 if (new_count != number_of_visible_frames)
14103 windows_or_buffers_changed = 52;
14104 }
14105
14106 /* Change frame size now if a change is pending. */
14107 do_pending_window_change (1);
14108
14109 /* If we just did a pending size change, or have additional
14110 visible frames, or selected_window changed, redisplay again. */
14111 if ((windows_or_buffers_changed && !pending)
14112 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14113 goto retry;
14114
14115 /* Clear the face and image caches.
14116
14117 We used to do this only if consider_all_windows_p. But the cache
14118 needs to be cleared if a timer creates images in the current
14119 buffer (e.g. the test case in Bug#6230). */
14120
14121 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14122 {
14123 clear_face_cache (0);
14124 clear_face_cache_count = 0;
14125 }
14126
14127 #ifdef HAVE_WINDOW_SYSTEM
14128 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14129 {
14130 clear_image_caches (Qnil);
14131 clear_image_cache_count = 0;
14132 }
14133 #endif /* HAVE_WINDOW_SYSTEM */
14134
14135 end_of_redisplay:
14136 if (interrupt_input && interrupts_deferred)
14137 request_sigio ();
14138
14139 unbind_to (count, Qnil);
14140 RESUME_POLLING;
14141 }
14142
14143
14144 /* Redisplay, but leave alone any recent echo area message unless
14145 another message has been requested in its place.
14146
14147 This is useful in situations where you need to redisplay but no
14148 user action has occurred, making it inappropriate for the message
14149 area to be cleared. See tracking_off and
14150 wait_reading_process_output for examples of these situations.
14151
14152 FROM_WHERE is an integer saying from where this function was
14153 called. This is useful for debugging. */
14154
14155 void
14156 redisplay_preserve_echo_area (int from_where)
14157 {
14158 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14159
14160 if (!NILP (echo_area_buffer[1]))
14161 {
14162 /* We have a previously displayed message, but no current
14163 message. Redisplay the previous message. */
14164 display_last_displayed_message_p = 1;
14165 redisplay_internal ();
14166 display_last_displayed_message_p = 0;
14167 }
14168 else
14169 redisplay_internal ();
14170
14171 flush_frame (SELECTED_FRAME ());
14172 }
14173
14174
14175 /* Function registered with record_unwind_protect in redisplay_internal. */
14176
14177 static void
14178 unwind_redisplay (void)
14179 {
14180 redisplaying_p = 0;
14181 }
14182
14183
14184 /* Mark the display of leaf window W as accurate or inaccurate.
14185 If ACCURATE_P is non-zero mark display of W as accurate. If
14186 ACCURATE_P is zero, arrange for W to be redisplayed the next
14187 time redisplay_internal is called. */
14188
14189 static void
14190 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14191 {
14192 struct buffer *b = XBUFFER (w->contents);
14193
14194 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14195 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14196 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14197
14198 if (accurate_p)
14199 {
14200 b->clip_changed = false;
14201 b->prevent_redisplay_optimizations_p = false;
14202 eassert (buffer_window_count (b) > 0);
14203 /* Resetting b->text->redisplay is problematic!
14204 In order to make it safer to do it here, redisplay_internal must
14205 have copied all b->text->redisplay to their respective windows. */
14206 b->text->redisplay = false;
14207
14208 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14209 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14210 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14211 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14212
14213 w->current_matrix->buffer = b;
14214 w->current_matrix->begv = BUF_BEGV (b);
14215 w->current_matrix->zv = BUF_ZV (b);
14216
14217 w->last_cursor_vpos = w->cursor.vpos;
14218 w->last_cursor_off_p = w->cursor_off_p;
14219
14220 if (w == XWINDOW (selected_window))
14221 w->last_point = BUF_PT (b);
14222 else
14223 w->last_point = marker_position (w->pointm);
14224
14225 w->window_end_valid = true;
14226 w->update_mode_line = false;
14227 }
14228
14229 w->redisplay = !accurate_p;
14230 }
14231
14232
14233 /* Mark the display of windows in the window tree rooted at WINDOW as
14234 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14235 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14236 be redisplayed the next time redisplay_internal is called. */
14237
14238 void
14239 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14240 {
14241 struct window *w;
14242
14243 for (; !NILP (window); window = w->next)
14244 {
14245 w = XWINDOW (window);
14246 if (WINDOWP (w->contents))
14247 mark_window_display_accurate (w->contents, accurate_p);
14248 else
14249 mark_window_display_accurate_1 (w, accurate_p);
14250 }
14251
14252 if (accurate_p)
14253 update_overlay_arrows (1);
14254 else
14255 /* Force a thorough redisplay the next time by setting
14256 last_arrow_position and last_arrow_string to t, which is
14257 unequal to any useful value of Voverlay_arrow_... */
14258 update_overlay_arrows (-1);
14259 }
14260
14261
14262 /* Return value in display table DP (Lisp_Char_Table *) for character
14263 C. Since a display table doesn't have any parent, we don't have to
14264 follow parent. Do not call this function directly but use the
14265 macro DISP_CHAR_VECTOR. */
14266
14267 Lisp_Object
14268 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14269 {
14270 Lisp_Object val;
14271
14272 if (ASCII_CHAR_P (c))
14273 {
14274 val = dp->ascii;
14275 if (SUB_CHAR_TABLE_P (val))
14276 val = XSUB_CHAR_TABLE (val)->contents[c];
14277 }
14278 else
14279 {
14280 Lisp_Object table;
14281
14282 XSETCHAR_TABLE (table, dp);
14283 val = char_table_ref (table, c);
14284 }
14285 if (NILP (val))
14286 val = dp->defalt;
14287 return val;
14288 }
14289
14290
14291 \f
14292 /***********************************************************************
14293 Window Redisplay
14294 ***********************************************************************/
14295
14296 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14297
14298 static void
14299 redisplay_windows (Lisp_Object window)
14300 {
14301 while (!NILP (window))
14302 {
14303 struct window *w = XWINDOW (window);
14304
14305 if (WINDOWP (w->contents))
14306 redisplay_windows (w->contents);
14307 else if (BUFFERP (w->contents))
14308 {
14309 displayed_buffer = XBUFFER (w->contents);
14310 /* Use list_of_error, not Qerror, so that
14311 we catch only errors and don't run the debugger. */
14312 internal_condition_case_1 (redisplay_window_0, window,
14313 list_of_error,
14314 redisplay_window_error);
14315 }
14316
14317 window = w->next;
14318 }
14319 }
14320
14321 static Lisp_Object
14322 redisplay_window_error (Lisp_Object ignore)
14323 {
14324 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14325 return Qnil;
14326 }
14327
14328 static Lisp_Object
14329 redisplay_window_0 (Lisp_Object window)
14330 {
14331 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14332 redisplay_window (window, false);
14333 return Qnil;
14334 }
14335
14336 static Lisp_Object
14337 redisplay_window_1 (Lisp_Object window)
14338 {
14339 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14340 redisplay_window (window, true);
14341 return Qnil;
14342 }
14343 \f
14344
14345 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14346 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14347 which positions recorded in ROW differ from current buffer
14348 positions.
14349
14350 Return 0 if cursor is not on this row, 1 otherwise. */
14351
14352 static int
14353 set_cursor_from_row (struct window *w, struct glyph_row *row,
14354 struct glyph_matrix *matrix,
14355 ptrdiff_t delta, ptrdiff_t delta_bytes,
14356 int dy, int dvpos)
14357 {
14358 struct glyph *glyph = row->glyphs[TEXT_AREA];
14359 struct glyph *end = glyph + row->used[TEXT_AREA];
14360 struct glyph *cursor = NULL;
14361 /* The last known character position in row. */
14362 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14363 int x = row->x;
14364 ptrdiff_t pt_old = PT - delta;
14365 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14366 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14367 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14368 /* A glyph beyond the edge of TEXT_AREA which we should never
14369 touch. */
14370 struct glyph *glyphs_end = end;
14371 /* Non-zero means we've found a match for cursor position, but that
14372 glyph has the avoid_cursor_p flag set. */
14373 int match_with_avoid_cursor = 0;
14374 /* Non-zero means we've seen at least one glyph that came from a
14375 display string. */
14376 int string_seen = 0;
14377 /* Largest and smallest buffer positions seen so far during scan of
14378 glyph row. */
14379 ptrdiff_t bpos_max = pos_before;
14380 ptrdiff_t bpos_min = pos_after;
14381 /* Last buffer position covered by an overlay string with an integer
14382 `cursor' property. */
14383 ptrdiff_t bpos_covered = 0;
14384 /* Non-zero means the display string on which to display the cursor
14385 comes from a text property, not from an overlay. */
14386 int string_from_text_prop = 0;
14387
14388 /* Don't even try doing anything if called for a mode-line or
14389 header-line row, since the rest of the code isn't prepared to
14390 deal with such calamities. */
14391 eassert (!row->mode_line_p);
14392 if (row->mode_line_p)
14393 return 0;
14394
14395 /* Skip over glyphs not having an object at the start and the end of
14396 the row. These are special glyphs like truncation marks on
14397 terminal frames. */
14398 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14399 {
14400 if (!row->reversed_p)
14401 {
14402 while (glyph < end
14403 && INTEGERP (glyph->object)
14404 && glyph->charpos < 0)
14405 {
14406 x += glyph->pixel_width;
14407 ++glyph;
14408 }
14409 while (end > glyph
14410 && INTEGERP ((end - 1)->object)
14411 /* CHARPOS is zero for blanks and stretch glyphs
14412 inserted by extend_face_to_end_of_line. */
14413 && (end - 1)->charpos <= 0)
14414 --end;
14415 glyph_before = glyph - 1;
14416 glyph_after = end;
14417 }
14418 else
14419 {
14420 struct glyph *g;
14421
14422 /* If the glyph row is reversed, we need to process it from back
14423 to front, so swap the edge pointers. */
14424 glyphs_end = end = glyph - 1;
14425 glyph += row->used[TEXT_AREA] - 1;
14426
14427 while (glyph > end + 1
14428 && INTEGERP (glyph->object)
14429 && glyph->charpos < 0)
14430 {
14431 --glyph;
14432 x -= glyph->pixel_width;
14433 }
14434 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14435 --glyph;
14436 /* By default, in reversed rows we put the cursor on the
14437 rightmost (first in the reading order) glyph. */
14438 for (g = end + 1; g < glyph; g++)
14439 x += g->pixel_width;
14440 while (end < glyph
14441 && INTEGERP ((end + 1)->object)
14442 && (end + 1)->charpos <= 0)
14443 ++end;
14444 glyph_before = glyph + 1;
14445 glyph_after = end;
14446 }
14447 }
14448 else if (row->reversed_p)
14449 {
14450 /* In R2L rows that don't display text, put the cursor on the
14451 rightmost glyph. Case in point: an empty last line that is
14452 part of an R2L paragraph. */
14453 cursor = end - 1;
14454 /* Avoid placing the cursor on the last glyph of the row, where
14455 on terminal frames we hold the vertical border between
14456 adjacent windows. */
14457 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14458 && !WINDOW_RIGHTMOST_P (w)
14459 && cursor == row->glyphs[LAST_AREA] - 1)
14460 cursor--;
14461 x = -1; /* will be computed below, at label compute_x */
14462 }
14463
14464 /* Step 1: Try to find the glyph whose character position
14465 corresponds to point. If that's not possible, find 2 glyphs
14466 whose character positions are the closest to point, one before
14467 point, the other after it. */
14468 if (!row->reversed_p)
14469 while (/* not marched to end of glyph row */
14470 glyph < end
14471 /* glyph was not inserted by redisplay for internal purposes */
14472 && !INTEGERP (glyph->object))
14473 {
14474 if (BUFFERP (glyph->object))
14475 {
14476 ptrdiff_t dpos = glyph->charpos - pt_old;
14477
14478 if (glyph->charpos > bpos_max)
14479 bpos_max = glyph->charpos;
14480 if (glyph->charpos < bpos_min)
14481 bpos_min = glyph->charpos;
14482 if (!glyph->avoid_cursor_p)
14483 {
14484 /* If we hit point, we've found the glyph on which to
14485 display the cursor. */
14486 if (dpos == 0)
14487 {
14488 match_with_avoid_cursor = 0;
14489 break;
14490 }
14491 /* See if we've found a better approximation to
14492 POS_BEFORE or to POS_AFTER. */
14493 if (0 > dpos && dpos > pos_before - pt_old)
14494 {
14495 pos_before = glyph->charpos;
14496 glyph_before = glyph;
14497 }
14498 else if (0 < dpos && dpos < pos_after - pt_old)
14499 {
14500 pos_after = glyph->charpos;
14501 glyph_after = glyph;
14502 }
14503 }
14504 else if (dpos == 0)
14505 match_with_avoid_cursor = 1;
14506 }
14507 else if (STRINGP (glyph->object))
14508 {
14509 Lisp_Object chprop;
14510 ptrdiff_t glyph_pos = glyph->charpos;
14511
14512 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14513 glyph->object);
14514 if (!NILP (chprop))
14515 {
14516 /* If the string came from a `display' text property,
14517 look up the buffer position of that property and
14518 use that position to update bpos_max, as if we
14519 actually saw such a position in one of the row's
14520 glyphs. This helps with supporting integer values
14521 of `cursor' property on the display string in
14522 situations where most or all of the row's buffer
14523 text is completely covered by display properties,
14524 so that no glyph with valid buffer positions is
14525 ever seen in the row. */
14526 ptrdiff_t prop_pos =
14527 string_buffer_position_lim (glyph->object, pos_before,
14528 pos_after, 0);
14529
14530 if (prop_pos >= pos_before)
14531 bpos_max = prop_pos;
14532 }
14533 if (INTEGERP (chprop))
14534 {
14535 bpos_covered = bpos_max + XINT (chprop);
14536 /* If the `cursor' property covers buffer positions up
14537 to and including point, we should display cursor on
14538 this glyph. Note that, if a `cursor' property on one
14539 of the string's characters has an integer value, we
14540 will break out of the loop below _before_ we get to
14541 the position match above. IOW, integer values of
14542 the `cursor' property override the "exact match for
14543 point" strategy of positioning the cursor. */
14544 /* Implementation note: bpos_max == pt_old when, e.g.,
14545 we are in an empty line, where bpos_max is set to
14546 MATRIX_ROW_START_CHARPOS, see above. */
14547 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14548 {
14549 cursor = glyph;
14550 break;
14551 }
14552 }
14553
14554 string_seen = 1;
14555 }
14556 x += glyph->pixel_width;
14557 ++glyph;
14558 }
14559 else if (glyph > end) /* row is reversed */
14560 while (!INTEGERP (glyph->object))
14561 {
14562 if (BUFFERP (glyph->object))
14563 {
14564 ptrdiff_t dpos = glyph->charpos - pt_old;
14565
14566 if (glyph->charpos > bpos_max)
14567 bpos_max = glyph->charpos;
14568 if (glyph->charpos < bpos_min)
14569 bpos_min = glyph->charpos;
14570 if (!glyph->avoid_cursor_p)
14571 {
14572 if (dpos == 0)
14573 {
14574 match_with_avoid_cursor = 0;
14575 break;
14576 }
14577 if (0 > dpos && dpos > pos_before - pt_old)
14578 {
14579 pos_before = glyph->charpos;
14580 glyph_before = glyph;
14581 }
14582 else if (0 < dpos && dpos < pos_after - pt_old)
14583 {
14584 pos_after = glyph->charpos;
14585 glyph_after = glyph;
14586 }
14587 }
14588 else if (dpos == 0)
14589 match_with_avoid_cursor = 1;
14590 }
14591 else if (STRINGP (glyph->object))
14592 {
14593 Lisp_Object chprop;
14594 ptrdiff_t glyph_pos = glyph->charpos;
14595
14596 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14597 glyph->object);
14598 if (!NILP (chprop))
14599 {
14600 ptrdiff_t prop_pos =
14601 string_buffer_position_lim (glyph->object, pos_before,
14602 pos_after, 0);
14603
14604 if (prop_pos >= pos_before)
14605 bpos_max = prop_pos;
14606 }
14607 if (INTEGERP (chprop))
14608 {
14609 bpos_covered = bpos_max + XINT (chprop);
14610 /* If the `cursor' property covers buffer positions up
14611 to and including point, we should display cursor on
14612 this glyph. */
14613 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14614 {
14615 cursor = glyph;
14616 break;
14617 }
14618 }
14619 string_seen = 1;
14620 }
14621 --glyph;
14622 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14623 {
14624 x--; /* can't use any pixel_width */
14625 break;
14626 }
14627 x -= glyph->pixel_width;
14628 }
14629
14630 /* Step 2: If we didn't find an exact match for point, we need to
14631 look for a proper place to put the cursor among glyphs between
14632 GLYPH_BEFORE and GLYPH_AFTER. */
14633 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14634 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14635 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14636 {
14637 /* An empty line has a single glyph whose OBJECT is zero and
14638 whose CHARPOS is the position of a newline on that line.
14639 Note that on a TTY, there are more glyphs after that, which
14640 were produced by extend_face_to_end_of_line, but their
14641 CHARPOS is zero or negative. */
14642 int empty_line_p =
14643 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14644 && INTEGERP (glyph->object) && glyph->charpos > 0
14645 /* On a TTY, continued and truncated rows also have a glyph at
14646 their end whose OBJECT is zero and whose CHARPOS is
14647 positive (the continuation and truncation glyphs), but such
14648 rows are obviously not "empty". */
14649 && !(row->continued_p || row->truncated_on_right_p);
14650
14651 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14652 {
14653 ptrdiff_t ellipsis_pos;
14654
14655 /* Scan back over the ellipsis glyphs. */
14656 if (!row->reversed_p)
14657 {
14658 ellipsis_pos = (glyph - 1)->charpos;
14659 while (glyph > row->glyphs[TEXT_AREA]
14660 && (glyph - 1)->charpos == ellipsis_pos)
14661 glyph--, x -= glyph->pixel_width;
14662 /* That loop always goes one position too far, including
14663 the glyph before the ellipsis. So scan forward over
14664 that one. */
14665 x += glyph->pixel_width;
14666 glyph++;
14667 }
14668 else /* row is reversed */
14669 {
14670 ellipsis_pos = (glyph + 1)->charpos;
14671 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14672 && (glyph + 1)->charpos == ellipsis_pos)
14673 glyph++, x += glyph->pixel_width;
14674 x -= glyph->pixel_width;
14675 glyph--;
14676 }
14677 }
14678 else if (match_with_avoid_cursor)
14679 {
14680 cursor = glyph_after;
14681 x = -1;
14682 }
14683 else if (string_seen)
14684 {
14685 int incr = row->reversed_p ? -1 : +1;
14686
14687 /* Need to find the glyph that came out of a string which is
14688 present at point. That glyph is somewhere between
14689 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14690 positioned between POS_BEFORE and POS_AFTER in the
14691 buffer. */
14692 struct glyph *start, *stop;
14693 ptrdiff_t pos = pos_before;
14694
14695 x = -1;
14696
14697 /* If the row ends in a newline from a display string,
14698 reordering could have moved the glyphs belonging to the
14699 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14700 in this case we extend the search to the last glyph in
14701 the row that was not inserted by redisplay. */
14702 if (row->ends_in_newline_from_string_p)
14703 {
14704 glyph_after = end;
14705 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14706 }
14707
14708 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14709 correspond to POS_BEFORE and POS_AFTER, respectively. We
14710 need START and STOP in the order that corresponds to the
14711 row's direction as given by its reversed_p flag. If the
14712 directionality of characters between POS_BEFORE and
14713 POS_AFTER is the opposite of the row's base direction,
14714 these characters will have been reordered for display,
14715 and we need to reverse START and STOP. */
14716 if (!row->reversed_p)
14717 {
14718 start = min (glyph_before, glyph_after);
14719 stop = max (glyph_before, glyph_after);
14720 }
14721 else
14722 {
14723 start = max (glyph_before, glyph_after);
14724 stop = min (glyph_before, glyph_after);
14725 }
14726 for (glyph = start + incr;
14727 row->reversed_p ? glyph > stop : glyph < stop; )
14728 {
14729
14730 /* Any glyphs that come from the buffer are here because
14731 of bidi reordering. Skip them, and only pay
14732 attention to glyphs that came from some string. */
14733 if (STRINGP (glyph->object))
14734 {
14735 Lisp_Object str;
14736 ptrdiff_t tem;
14737 /* If the display property covers the newline, we
14738 need to search for it one position farther. */
14739 ptrdiff_t lim = pos_after
14740 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14741
14742 string_from_text_prop = 0;
14743 str = glyph->object;
14744 tem = string_buffer_position_lim (str, pos, lim, 0);
14745 if (tem == 0 /* from overlay */
14746 || pos <= tem)
14747 {
14748 /* If the string from which this glyph came is
14749 found in the buffer at point, or at position
14750 that is closer to point than pos_after, then
14751 we've found the glyph we've been looking for.
14752 If it comes from an overlay (tem == 0), and
14753 it has the `cursor' property on one of its
14754 glyphs, record that glyph as a candidate for
14755 displaying the cursor. (As in the
14756 unidirectional version, we will display the
14757 cursor on the last candidate we find.) */
14758 if (tem == 0
14759 || tem == pt_old
14760 || (tem - pt_old > 0 && tem < pos_after))
14761 {
14762 /* The glyphs from this string could have
14763 been reordered. Find the one with the
14764 smallest string position. Or there could
14765 be a character in the string with the
14766 `cursor' property, which means display
14767 cursor on that character's glyph. */
14768 ptrdiff_t strpos = glyph->charpos;
14769
14770 if (tem)
14771 {
14772 cursor = glyph;
14773 string_from_text_prop = 1;
14774 }
14775 for ( ;
14776 (row->reversed_p ? glyph > stop : glyph < stop)
14777 && EQ (glyph->object, str);
14778 glyph += incr)
14779 {
14780 Lisp_Object cprop;
14781 ptrdiff_t gpos = glyph->charpos;
14782
14783 cprop = Fget_char_property (make_number (gpos),
14784 Qcursor,
14785 glyph->object);
14786 if (!NILP (cprop))
14787 {
14788 cursor = glyph;
14789 break;
14790 }
14791 if (tem && glyph->charpos < strpos)
14792 {
14793 strpos = glyph->charpos;
14794 cursor = glyph;
14795 }
14796 }
14797
14798 if (tem == pt_old
14799 || (tem - pt_old > 0 && tem < pos_after))
14800 goto compute_x;
14801 }
14802 if (tem)
14803 pos = tem + 1; /* don't find previous instances */
14804 }
14805 /* This string is not what we want; skip all of the
14806 glyphs that came from it. */
14807 while ((row->reversed_p ? glyph > stop : glyph < stop)
14808 && EQ (glyph->object, str))
14809 glyph += incr;
14810 }
14811 else
14812 glyph += incr;
14813 }
14814
14815 /* If we reached the end of the line, and END was from a string,
14816 the cursor is not on this line. */
14817 if (cursor == NULL
14818 && (row->reversed_p ? glyph <= end : glyph >= end)
14819 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14820 && STRINGP (end->object)
14821 && row->continued_p)
14822 return 0;
14823 }
14824 /* A truncated row may not include PT among its character positions.
14825 Setting the cursor inside the scroll margin will trigger
14826 recalculation of hscroll in hscroll_window_tree. But if a
14827 display string covers point, defer to the string-handling
14828 code below to figure this out. */
14829 else if (row->truncated_on_left_p && pt_old < bpos_min)
14830 {
14831 cursor = glyph_before;
14832 x = -1;
14833 }
14834 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14835 /* Zero-width characters produce no glyphs. */
14836 || (!empty_line_p
14837 && (row->reversed_p
14838 ? glyph_after > glyphs_end
14839 : glyph_after < glyphs_end)))
14840 {
14841 cursor = glyph_after;
14842 x = -1;
14843 }
14844 }
14845
14846 compute_x:
14847 if (cursor != NULL)
14848 glyph = cursor;
14849 else if (glyph == glyphs_end
14850 && pos_before == pos_after
14851 && STRINGP ((row->reversed_p
14852 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14853 : row->glyphs[TEXT_AREA])->object))
14854 {
14855 /* If all the glyphs of this row came from strings, put the
14856 cursor on the first glyph of the row. This avoids having the
14857 cursor outside of the text area in this very rare and hard
14858 use case. */
14859 glyph =
14860 row->reversed_p
14861 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14862 : row->glyphs[TEXT_AREA];
14863 }
14864 if (x < 0)
14865 {
14866 struct glyph *g;
14867
14868 /* Need to compute x that corresponds to GLYPH. */
14869 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14870 {
14871 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14872 emacs_abort ();
14873 x += g->pixel_width;
14874 }
14875 }
14876
14877 /* ROW could be part of a continued line, which, under bidi
14878 reordering, might have other rows whose start and end charpos
14879 occlude point. Only set w->cursor if we found a better
14880 approximation to the cursor position than we have from previously
14881 examined candidate rows belonging to the same continued line. */
14882 if (/* We already have a candidate row. */
14883 w->cursor.vpos >= 0
14884 /* That candidate is not the row we are processing. */
14885 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14886 /* Make sure cursor.vpos specifies a row whose start and end
14887 charpos occlude point, and it is valid candidate for being a
14888 cursor-row. This is because some callers of this function
14889 leave cursor.vpos at the row where the cursor was displayed
14890 during the last redisplay cycle. */
14891 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14892 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14893 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14894 {
14895 struct glyph *g1
14896 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14897
14898 /* Don't consider glyphs that are outside TEXT_AREA. */
14899 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14900 return 0;
14901 /* Keep the candidate whose buffer position is the closest to
14902 point or has the `cursor' property. */
14903 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14904 w->cursor.hpos >= 0
14905 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14906 && ((BUFFERP (g1->object)
14907 && (g1->charpos == pt_old /* An exact match always wins. */
14908 || (BUFFERP (glyph->object)
14909 && eabs (g1->charpos - pt_old)
14910 < eabs (glyph->charpos - pt_old))))
14911 /* Previous candidate is a glyph from a string that has
14912 a non-nil `cursor' property. */
14913 || (STRINGP (g1->object)
14914 && (!NILP (Fget_char_property (make_number (g1->charpos),
14915 Qcursor, g1->object))
14916 /* Previous candidate is from the same display
14917 string as this one, and the display string
14918 came from a text property. */
14919 || (EQ (g1->object, glyph->object)
14920 && string_from_text_prop)
14921 /* this candidate is from newline and its
14922 position is not an exact match */
14923 || (INTEGERP (glyph->object)
14924 && glyph->charpos != pt_old)))))
14925 return 0;
14926 /* If this candidate gives an exact match, use that. */
14927 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14928 /* If this candidate is a glyph created for the
14929 terminating newline of a line, and point is on that
14930 newline, it wins because it's an exact match. */
14931 || (!row->continued_p
14932 && INTEGERP (glyph->object)
14933 && glyph->charpos == 0
14934 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14935 /* Otherwise, keep the candidate that comes from a row
14936 spanning less buffer positions. This may win when one or
14937 both candidate positions are on glyphs that came from
14938 display strings, for which we cannot compare buffer
14939 positions. */
14940 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14941 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14942 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14943 return 0;
14944 }
14945 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14946 w->cursor.x = x;
14947 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14948 w->cursor.y = row->y + dy;
14949
14950 if (w == XWINDOW (selected_window))
14951 {
14952 if (!row->continued_p
14953 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14954 && row->x == 0)
14955 {
14956 this_line_buffer = XBUFFER (w->contents);
14957
14958 CHARPOS (this_line_start_pos)
14959 = MATRIX_ROW_START_CHARPOS (row) + delta;
14960 BYTEPOS (this_line_start_pos)
14961 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14962
14963 CHARPOS (this_line_end_pos)
14964 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14965 BYTEPOS (this_line_end_pos)
14966 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14967
14968 this_line_y = w->cursor.y;
14969 this_line_pixel_height = row->height;
14970 this_line_vpos = w->cursor.vpos;
14971 this_line_start_x = row->x;
14972 }
14973 else
14974 CHARPOS (this_line_start_pos) = 0;
14975 }
14976
14977 return 1;
14978 }
14979
14980
14981 /* Run window scroll functions, if any, for WINDOW with new window
14982 start STARTP. Sets the window start of WINDOW to that position.
14983
14984 We assume that the window's buffer is really current. */
14985
14986 static struct text_pos
14987 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14988 {
14989 struct window *w = XWINDOW (window);
14990 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14991
14992 eassert (current_buffer == XBUFFER (w->contents));
14993
14994 if (!NILP (Vwindow_scroll_functions))
14995 {
14996 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14997 make_number (CHARPOS (startp)));
14998 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14999 /* In case the hook functions switch buffers. */
15000 set_buffer_internal (XBUFFER (w->contents));
15001 }
15002
15003 return startp;
15004 }
15005
15006
15007 /* Make sure the line containing the cursor is fully visible.
15008 A value of 1 means there is nothing to be done.
15009 (Either the line is fully visible, or it cannot be made so,
15010 or we cannot tell.)
15011
15012 If FORCE_P is non-zero, return 0 even if partial visible cursor row
15013 is higher than window.
15014
15015 A value of 0 means the caller should do scrolling
15016 as if point had gone off the screen. */
15017
15018 static int
15019 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
15020 {
15021 struct glyph_matrix *matrix;
15022 struct glyph_row *row;
15023 int window_height;
15024
15025 if (!make_cursor_line_fully_visible_p)
15026 return 1;
15027
15028 /* It's not always possible to find the cursor, e.g, when a window
15029 is full of overlay strings. Don't do anything in that case. */
15030 if (w->cursor.vpos < 0)
15031 return 1;
15032
15033 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15034 row = MATRIX_ROW (matrix, w->cursor.vpos);
15035
15036 /* If the cursor row is not partially visible, there's nothing to do. */
15037 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15038 return 1;
15039
15040 /* If the row the cursor is in is taller than the window's height,
15041 it's not clear what to do, so do nothing. */
15042 window_height = window_box_height (w);
15043 if (row->height >= window_height)
15044 {
15045 if (!force_p || MINI_WINDOW_P (w)
15046 || w->vscroll || w->cursor.vpos == 0)
15047 return 1;
15048 }
15049 return 0;
15050 }
15051
15052
15053 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15054 non-zero means only WINDOW is redisplayed in redisplay_internal.
15055 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15056 in redisplay_window to bring a partially visible line into view in
15057 the case that only the cursor has moved.
15058
15059 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
15060 last screen line's vertical height extends past the end of the screen.
15061
15062 Value is
15063
15064 1 if scrolling succeeded
15065
15066 0 if scrolling didn't find point.
15067
15068 -1 if new fonts have been loaded so that we must interrupt
15069 redisplay, adjust glyph matrices, and try again. */
15070
15071 enum
15072 {
15073 SCROLLING_SUCCESS,
15074 SCROLLING_FAILED,
15075 SCROLLING_NEED_LARGER_MATRICES
15076 };
15077
15078 /* If scroll-conservatively is more than this, never recenter.
15079
15080 If you change this, don't forget to update the doc string of
15081 `scroll-conservatively' and the Emacs manual. */
15082 #define SCROLL_LIMIT 100
15083
15084 static int
15085 try_scrolling (Lisp_Object window, int just_this_one_p,
15086 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15087 int temp_scroll_step, int last_line_misfit)
15088 {
15089 struct window *w = XWINDOW (window);
15090 struct frame *f = XFRAME (w->frame);
15091 struct text_pos pos, startp;
15092 struct it it;
15093 int this_scroll_margin, scroll_max, rc, height;
15094 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
15095 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
15096 Lisp_Object aggressive;
15097 /* We will never try scrolling more than this number of lines. */
15098 int scroll_limit = SCROLL_LIMIT;
15099 int frame_line_height = default_line_pixel_height (w);
15100 int window_total_lines
15101 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15102
15103 #ifdef GLYPH_DEBUG
15104 debug_method_add (w, "try_scrolling");
15105 #endif
15106
15107 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15108
15109 /* Compute scroll margin height in pixels. We scroll when point is
15110 within this distance from the top or bottom of the window. */
15111 if (scroll_margin > 0)
15112 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15113 * frame_line_height;
15114 else
15115 this_scroll_margin = 0;
15116
15117 /* Force arg_scroll_conservatively to have a reasonable value, to
15118 avoid scrolling too far away with slow move_it_* functions. Note
15119 that the user can supply scroll-conservatively equal to
15120 `most-positive-fixnum', which can be larger than INT_MAX. */
15121 if (arg_scroll_conservatively > scroll_limit)
15122 {
15123 arg_scroll_conservatively = scroll_limit + 1;
15124 scroll_max = scroll_limit * frame_line_height;
15125 }
15126 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15127 /* Compute how much we should try to scroll maximally to bring
15128 point into view. */
15129 scroll_max = (max (scroll_step,
15130 max (arg_scroll_conservatively, temp_scroll_step))
15131 * frame_line_height);
15132 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15133 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15134 /* We're trying to scroll because of aggressive scrolling but no
15135 scroll_step is set. Choose an arbitrary one. */
15136 scroll_max = 10 * frame_line_height;
15137 else
15138 scroll_max = 0;
15139
15140 too_near_end:
15141
15142 /* Decide whether to scroll down. */
15143 if (PT > CHARPOS (startp))
15144 {
15145 int scroll_margin_y;
15146
15147 /* Compute the pixel ypos of the scroll margin, then move IT to
15148 either that ypos or PT, whichever comes first. */
15149 start_display (&it, w, startp);
15150 scroll_margin_y = it.last_visible_y - this_scroll_margin
15151 - frame_line_height * extra_scroll_margin_lines;
15152 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15153 (MOVE_TO_POS | MOVE_TO_Y));
15154
15155 if (PT > CHARPOS (it.current.pos))
15156 {
15157 int y0 = line_bottom_y (&it);
15158 /* Compute how many pixels below window bottom to stop searching
15159 for PT. This avoids costly search for PT that is far away if
15160 the user limited scrolling by a small number of lines, but
15161 always finds PT if scroll_conservatively is set to a large
15162 number, such as most-positive-fixnum. */
15163 int slack = max (scroll_max, 10 * frame_line_height);
15164 int y_to_move = it.last_visible_y + slack;
15165
15166 /* Compute the distance from the scroll margin to PT or to
15167 the scroll limit, whichever comes first. This should
15168 include the height of the cursor line, to make that line
15169 fully visible. */
15170 move_it_to (&it, PT, -1, y_to_move,
15171 -1, MOVE_TO_POS | MOVE_TO_Y);
15172 dy = line_bottom_y (&it) - y0;
15173
15174 if (dy > scroll_max)
15175 return SCROLLING_FAILED;
15176
15177 if (dy > 0)
15178 scroll_down_p = 1;
15179 }
15180 }
15181
15182 if (scroll_down_p)
15183 {
15184 /* Point is in or below the bottom scroll margin, so move the
15185 window start down. If scrolling conservatively, move it just
15186 enough down to make point visible. If scroll_step is set,
15187 move it down by scroll_step. */
15188 if (arg_scroll_conservatively)
15189 amount_to_scroll
15190 = min (max (dy, frame_line_height),
15191 frame_line_height * arg_scroll_conservatively);
15192 else if (scroll_step || temp_scroll_step)
15193 amount_to_scroll = scroll_max;
15194 else
15195 {
15196 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15197 height = WINDOW_BOX_TEXT_HEIGHT (w);
15198 if (NUMBERP (aggressive))
15199 {
15200 double float_amount = XFLOATINT (aggressive) * height;
15201 int aggressive_scroll = float_amount;
15202 if (aggressive_scroll == 0 && float_amount > 0)
15203 aggressive_scroll = 1;
15204 /* Don't let point enter the scroll margin near top of
15205 the window. This could happen if the value of
15206 scroll_up_aggressively is too large and there are
15207 non-zero margins, because scroll_up_aggressively
15208 means put point that fraction of window height
15209 _from_the_bottom_margin_. */
15210 if (aggressive_scroll + 2*this_scroll_margin > height)
15211 aggressive_scroll = height - 2*this_scroll_margin;
15212 amount_to_scroll = dy + aggressive_scroll;
15213 }
15214 }
15215
15216 if (amount_to_scroll <= 0)
15217 return SCROLLING_FAILED;
15218
15219 start_display (&it, w, startp);
15220 if (arg_scroll_conservatively <= scroll_limit)
15221 move_it_vertically (&it, amount_to_scroll);
15222 else
15223 {
15224 /* Extra precision for users who set scroll-conservatively
15225 to a large number: make sure the amount we scroll
15226 the window start is never less than amount_to_scroll,
15227 which was computed as distance from window bottom to
15228 point. This matters when lines at window top and lines
15229 below window bottom have different height. */
15230 struct it it1;
15231 void *it1data = NULL;
15232 /* We use a temporary it1 because line_bottom_y can modify
15233 its argument, if it moves one line down; see there. */
15234 int start_y;
15235
15236 SAVE_IT (it1, it, it1data);
15237 start_y = line_bottom_y (&it1);
15238 do {
15239 RESTORE_IT (&it, &it, it1data);
15240 move_it_by_lines (&it, 1);
15241 SAVE_IT (it1, it, it1data);
15242 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15243 }
15244
15245 /* If STARTP is unchanged, move it down another screen line. */
15246 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15247 move_it_by_lines (&it, 1);
15248 startp = it.current.pos;
15249 }
15250 else
15251 {
15252 struct text_pos scroll_margin_pos = startp;
15253 int y_offset = 0;
15254
15255 /* See if point is inside the scroll margin at the top of the
15256 window. */
15257 if (this_scroll_margin)
15258 {
15259 int y_start;
15260
15261 start_display (&it, w, startp);
15262 y_start = it.current_y;
15263 move_it_vertically (&it, this_scroll_margin);
15264 scroll_margin_pos = it.current.pos;
15265 /* If we didn't move enough before hitting ZV, request
15266 additional amount of scroll, to move point out of the
15267 scroll margin. */
15268 if (IT_CHARPOS (it) == ZV
15269 && it.current_y - y_start < this_scroll_margin)
15270 y_offset = this_scroll_margin - (it.current_y - y_start);
15271 }
15272
15273 if (PT < CHARPOS (scroll_margin_pos))
15274 {
15275 /* Point is in the scroll margin at the top of the window or
15276 above what is displayed in the window. */
15277 int y0, y_to_move;
15278
15279 /* Compute the vertical distance from PT to the scroll
15280 margin position. Move as far as scroll_max allows, or
15281 one screenful, or 10 screen lines, whichever is largest.
15282 Give up if distance is greater than scroll_max or if we
15283 didn't reach the scroll margin position. */
15284 SET_TEXT_POS (pos, PT, PT_BYTE);
15285 start_display (&it, w, pos);
15286 y0 = it.current_y;
15287 y_to_move = max (it.last_visible_y,
15288 max (scroll_max, 10 * frame_line_height));
15289 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15290 y_to_move, -1,
15291 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15292 dy = it.current_y - y0;
15293 if (dy > scroll_max
15294 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15295 return SCROLLING_FAILED;
15296
15297 /* Additional scroll for when ZV was too close to point. */
15298 dy += y_offset;
15299
15300 /* Compute new window start. */
15301 start_display (&it, w, startp);
15302
15303 if (arg_scroll_conservatively)
15304 amount_to_scroll = max (dy, frame_line_height *
15305 max (scroll_step, temp_scroll_step));
15306 else if (scroll_step || temp_scroll_step)
15307 amount_to_scroll = scroll_max;
15308 else
15309 {
15310 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15311 height = WINDOW_BOX_TEXT_HEIGHT (w);
15312 if (NUMBERP (aggressive))
15313 {
15314 double float_amount = XFLOATINT (aggressive) * height;
15315 int aggressive_scroll = float_amount;
15316 if (aggressive_scroll == 0 && float_amount > 0)
15317 aggressive_scroll = 1;
15318 /* Don't let point enter the scroll margin near
15319 bottom of the window, if the value of
15320 scroll_down_aggressively happens to be too
15321 large. */
15322 if (aggressive_scroll + 2*this_scroll_margin > height)
15323 aggressive_scroll = height - 2*this_scroll_margin;
15324 amount_to_scroll = dy + aggressive_scroll;
15325 }
15326 }
15327
15328 if (amount_to_scroll <= 0)
15329 return SCROLLING_FAILED;
15330
15331 move_it_vertically_backward (&it, amount_to_scroll);
15332 startp = it.current.pos;
15333 }
15334 }
15335
15336 /* Run window scroll functions. */
15337 startp = run_window_scroll_functions (window, startp);
15338
15339 /* Display the window. Give up if new fonts are loaded, or if point
15340 doesn't appear. */
15341 if (!try_window (window, startp, 0))
15342 rc = SCROLLING_NEED_LARGER_MATRICES;
15343 else if (w->cursor.vpos < 0)
15344 {
15345 clear_glyph_matrix (w->desired_matrix);
15346 rc = SCROLLING_FAILED;
15347 }
15348 else
15349 {
15350 /* Maybe forget recorded base line for line number display. */
15351 if (!just_this_one_p
15352 || current_buffer->clip_changed
15353 || BEG_UNCHANGED < CHARPOS (startp))
15354 w->base_line_number = 0;
15355
15356 /* If cursor ends up on a partially visible line,
15357 treat that as being off the bottom of the screen. */
15358 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15359 /* It's possible that the cursor is on the first line of the
15360 buffer, which is partially obscured due to a vscroll
15361 (Bug#7537). In that case, avoid looping forever. */
15362 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15363 {
15364 clear_glyph_matrix (w->desired_matrix);
15365 ++extra_scroll_margin_lines;
15366 goto too_near_end;
15367 }
15368 rc = SCROLLING_SUCCESS;
15369 }
15370
15371 return rc;
15372 }
15373
15374
15375 /* Compute a suitable window start for window W if display of W starts
15376 on a continuation line. Value is non-zero if a new window start
15377 was computed.
15378
15379 The new window start will be computed, based on W's width, starting
15380 from the start of the continued line. It is the start of the
15381 screen line with the minimum distance from the old start W->start. */
15382
15383 static int
15384 compute_window_start_on_continuation_line (struct window *w)
15385 {
15386 struct text_pos pos, start_pos;
15387 int window_start_changed_p = 0;
15388
15389 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15390
15391 /* If window start is on a continuation line... Window start may be
15392 < BEGV in case there's invisible text at the start of the
15393 buffer (M-x rmail, for example). */
15394 if (CHARPOS (start_pos) > BEGV
15395 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15396 {
15397 struct it it;
15398 struct glyph_row *row;
15399
15400 /* Handle the case that the window start is out of range. */
15401 if (CHARPOS (start_pos) < BEGV)
15402 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15403 else if (CHARPOS (start_pos) > ZV)
15404 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15405
15406 /* Find the start of the continued line. This should be fast
15407 because find_newline is fast (newline cache). */
15408 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15409 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15410 row, DEFAULT_FACE_ID);
15411 reseat_at_previous_visible_line_start (&it);
15412
15413 /* If the line start is "too far" away from the window start,
15414 say it takes too much time to compute a new window start. */
15415 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15416 /* PXW: Do we need upper bounds here? */
15417 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15418 {
15419 int min_distance, distance;
15420
15421 /* Move forward by display lines to find the new window
15422 start. If window width was enlarged, the new start can
15423 be expected to be > the old start. If window width was
15424 decreased, the new window start will be < the old start.
15425 So, we're looking for the display line start with the
15426 minimum distance from the old window start. */
15427 pos = it.current.pos;
15428 min_distance = INFINITY;
15429 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15430 distance < min_distance)
15431 {
15432 min_distance = distance;
15433 pos = it.current.pos;
15434 if (it.line_wrap == WORD_WRAP)
15435 {
15436 /* Under WORD_WRAP, move_it_by_lines is likely to
15437 overshoot and stop not at the first, but the
15438 second character from the left margin. So in
15439 that case, we need a more tight control on the X
15440 coordinate of the iterator than move_it_by_lines
15441 promises in its contract. The method is to first
15442 go to the last (rightmost) visible character of a
15443 line, then move to the leftmost character on the
15444 next line in a separate call. */
15445 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15446 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15447 move_it_to (&it, ZV, 0,
15448 it.current_y + it.max_ascent + it.max_descent, -1,
15449 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15450 }
15451 else
15452 move_it_by_lines (&it, 1);
15453 }
15454
15455 /* Set the window start there. */
15456 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15457 window_start_changed_p = 1;
15458 }
15459 }
15460
15461 return window_start_changed_p;
15462 }
15463
15464
15465 /* Try cursor movement in case text has not changed in window WINDOW,
15466 with window start STARTP. Value is
15467
15468 CURSOR_MOVEMENT_SUCCESS if successful
15469
15470 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15471
15472 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15473 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15474 we want to scroll as if scroll-step were set to 1. See the code.
15475
15476 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15477 which case we have to abort this redisplay, and adjust matrices
15478 first. */
15479
15480 enum
15481 {
15482 CURSOR_MOVEMENT_SUCCESS,
15483 CURSOR_MOVEMENT_CANNOT_BE_USED,
15484 CURSOR_MOVEMENT_MUST_SCROLL,
15485 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15486 };
15487
15488 static int
15489 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15490 {
15491 struct window *w = XWINDOW (window);
15492 struct frame *f = XFRAME (w->frame);
15493 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15494
15495 #ifdef GLYPH_DEBUG
15496 if (inhibit_try_cursor_movement)
15497 return rc;
15498 #endif
15499
15500 /* Previously, there was a check for Lisp integer in the
15501 if-statement below. Now, this field is converted to
15502 ptrdiff_t, thus zero means invalid position in a buffer. */
15503 eassert (w->last_point > 0);
15504 /* Likewise there was a check whether window_end_vpos is nil or larger
15505 than the window. Now window_end_vpos is int and so never nil, but
15506 let's leave eassert to check whether it fits in the window. */
15507 eassert (w->window_end_vpos < w->current_matrix->nrows);
15508
15509 /* Handle case where text has not changed, only point, and it has
15510 not moved off the frame. */
15511 if (/* Point may be in this window. */
15512 PT >= CHARPOS (startp)
15513 /* Selective display hasn't changed. */
15514 && !current_buffer->clip_changed
15515 /* Function force-mode-line-update is used to force a thorough
15516 redisplay. It sets either windows_or_buffers_changed or
15517 update_mode_lines. So don't take a shortcut here for these
15518 cases. */
15519 && !update_mode_lines
15520 && !windows_or_buffers_changed
15521 && !f->cursor_type_changed
15522 && NILP (Vshow_trailing_whitespace)
15523 /* This code is not used for mini-buffer for the sake of the case
15524 of redisplaying to replace an echo area message; since in
15525 that case the mini-buffer contents per se are usually
15526 unchanged. This code is of no real use in the mini-buffer
15527 since the handling of this_line_start_pos, etc., in redisplay
15528 handles the same cases. */
15529 && !EQ (window, minibuf_window)
15530 && (FRAME_WINDOW_P (f)
15531 || !overlay_arrow_in_current_buffer_p ()))
15532 {
15533 int this_scroll_margin, top_scroll_margin;
15534 struct glyph_row *row = NULL;
15535 int frame_line_height = default_line_pixel_height (w);
15536 int window_total_lines
15537 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15538
15539 #ifdef GLYPH_DEBUG
15540 debug_method_add (w, "cursor movement");
15541 #endif
15542
15543 /* Scroll if point within this distance from the top or bottom
15544 of the window. This is a pixel value. */
15545 if (scroll_margin > 0)
15546 {
15547 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15548 this_scroll_margin *= frame_line_height;
15549 }
15550 else
15551 this_scroll_margin = 0;
15552
15553 top_scroll_margin = this_scroll_margin;
15554 if (WINDOW_WANTS_HEADER_LINE_P (w))
15555 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15556
15557 /* Start with the row the cursor was displayed during the last
15558 not paused redisplay. Give up if that row is not valid. */
15559 if (w->last_cursor_vpos < 0
15560 || w->last_cursor_vpos >= w->current_matrix->nrows)
15561 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15562 else
15563 {
15564 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15565 if (row->mode_line_p)
15566 ++row;
15567 if (!row->enabled_p)
15568 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15569 }
15570
15571 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15572 {
15573 int scroll_p = 0, must_scroll = 0;
15574 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15575
15576 if (PT > w->last_point)
15577 {
15578 /* Point has moved forward. */
15579 while (MATRIX_ROW_END_CHARPOS (row) < PT
15580 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15581 {
15582 eassert (row->enabled_p);
15583 ++row;
15584 }
15585
15586 /* If the end position of a row equals the start
15587 position of the next row, and PT is at that position,
15588 we would rather display cursor in the next line. */
15589 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15590 && MATRIX_ROW_END_CHARPOS (row) == PT
15591 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15592 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15593 && !cursor_row_p (row))
15594 ++row;
15595
15596 /* If within the scroll margin, scroll. Note that
15597 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15598 the next line would be drawn, and that
15599 this_scroll_margin can be zero. */
15600 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15601 || PT > MATRIX_ROW_END_CHARPOS (row)
15602 /* Line is completely visible last line in window
15603 and PT is to be set in the next line. */
15604 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15605 && PT == MATRIX_ROW_END_CHARPOS (row)
15606 && !row->ends_at_zv_p
15607 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15608 scroll_p = 1;
15609 }
15610 else if (PT < w->last_point)
15611 {
15612 /* Cursor has to be moved backward. Note that PT >=
15613 CHARPOS (startp) because of the outer if-statement. */
15614 while (!row->mode_line_p
15615 && (MATRIX_ROW_START_CHARPOS (row) > PT
15616 || (MATRIX_ROW_START_CHARPOS (row) == PT
15617 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15618 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15619 row > w->current_matrix->rows
15620 && (row-1)->ends_in_newline_from_string_p))))
15621 && (row->y > top_scroll_margin
15622 || CHARPOS (startp) == BEGV))
15623 {
15624 eassert (row->enabled_p);
15625 --row;
15626 }
15627
15628 /* Consider the following case: Window starts at BEGV,
15629 there is invisible, intangible text at BEGV, so that
15630 display starts at some point START > BEGV. It can
15631 happen that we are called with PT somewhere between
15632 BEGV and START. Try to handle that case. */
15633 if (row < w->current_matrix->rows
15634 || row->mode_line_p)
15635 {
15636 row = w->current_matrix->rows;
15637 if (row->mode_line_p)
15638 ++row;
15639 }
15640
15641 /* Due to newlines in overlay strings, we may have to
15642 skip forward over overlay strings. */
15643 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15644 && MATRIX_ROW_END_CHARPOS (row) == PT
15645 && !cursor_row_p (row))
15646 ++row;
15647
15648 /* If within the scroll margin, scroll. */
15649 if (row->y < top_scroll_margin
15650 && CHARPOS (startp) != BEGV)
15651 scroll_p = 1;
15652 }
15653 else
15654 {
15655 /* Cursor did not move. So don't scroll even if cursor line
15656 is partially visible, as it was so before. */
15657 rc = CURSOR_MOVEMENT_SUCCESS;
15658 }
15659
15660 if (PT < MATRIX_ROW_START_CHARPOS (row)
15661 || PT > MATRIX_ROW_END_CHARPOS (row))
15662 {
15663 /* if PT is not in the glyph row, give up. */
15664 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15665 must_scroll = 1;
15666 }
15667 else if (rc != CURSOR_MOVEMENT_SUCCESS
15668 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15669 {
15670 struct glyph_row *row1;
15671
15672 /* If rows are bidi-reordered and point moved, back up
15673 until we find a row that does not belong to a
15674 continuation line. This is because we must consider
15675 all rows of a continued line as candidates for the
15676 new cursor positioning, since row start and end
15677 positions change non-linearly with vertical position
15678 in such rows. */
15679 /* FIXME: Revisit this when glyph ``spilling'' in
15680 continuation lines' rows is implemented for
15681 bidi-reordered rows. */
15682 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15683 MATRIX_ROW_CONTINUATION_LINE_P (row);
15684 --row)
15685 {
15686 /* If we hit the beginning of the displayed portion
15687 without finding the first row of a continued
15688 line, give up. */
15689 if (row <= row1)
15690 {
15691 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15692 break;
15693 }
15694 eassert (row->enabled_p);
15695 }
15696 }
15697 if (must_scroll)
15698 ;
15699 else if (rc != CURSOR_MOVEMENT_SUCCESS
15700 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15701 /* Make sure this isn't a header line by any chance, since
15702 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15703 && !row->mode_line_p
15704 && make_cursor_line_fully_visible_p)
15705 {
15706 if (PT == MATRIX_ROW_END_CHARPOS (row)
15707 && !row->ends_at_zv_p
15708 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15709 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15710 else if (row->height > window_box_height (w))
15711 {
15712 /* If we end up in a partially visible line, let's
15713 make it fully visible, except when it's taller
15714 than the window, in which case we can't do much
15715 about it. */
15716 *scroll_step = 1;
15717 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15718 }
15719 else
15720 {
15721 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15722 if (!cursor_row_fully_visible_p (w, 0, 1))
15723 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15724 else
15725 rc = CURSOR_MOVEMENT_SUCCESS;
15726 }
15727 }
15728 else if (scroll_p)
15729 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15730 else if (rc != CURSOR_MOVEMENT_SUCCESS
15731 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15732 {
15733 /* With bidi-reordered rows, there could be more than
15734 one candidate row whose start and end positions
15735 occlude point. We need to let set_cursor_from_row
15736 find the best candidate. */
15737 /* FIXME: Revisit this when glyph ``spilling'' in
15738 continuation lines' rows is implemented for
15739 bidi-reordered rows. */
15740 int rv = 0;
15741
15742 do
15743 {
15744 int at_zv_p = 0, exact_match_p = 0;
15745
15746 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15747 && PT <= MATRIX_ROW_END_CHARPOS (row)
15748 && cursor_row_p (row))
15749 rv |= set_cursor_from_row (w, row, w->current_matrix,
15750 0, 0, 0, 0);
15751 /* As soon as we've found the exact match for point,
15752 or the first suitable row whose ends_at_zv_p flag
15753 is set, we are done. */
15754 if (rv)
15755 {
15756 at_zv_p = MATRIX_ROW (w->current_matrix,
15757 w->cursor.vpos)->ends_at_zv_p;
15758 if (!at_zv_p
15759 && w->cursor.hpos >= 0
15760 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15761 w->cursor.vpos))
15762 {
15763 struct glyph_row *candidate =
15764 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15765 struct glyph *g =
15766 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15767 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15768
15769 exact_match_p =
15770 (BUFFERP (g->object) && g->charpos == PT)
15771 || (INTEGERP (g->object)
15772 && (g->charpos == PT
15773 || (g->charpos == 0 && endpos - 1 == PT)));
15774 }
15775 if (at_zv_p || exact_match_p)
15776 {
15777 rc = CURSOR_MOVEMENT_SUCCESS;
15778 break;
15779 }
15780 }
15781 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15782 break;
15783 ++row;
15784 }
15785 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15786 || row->continued_p)
15787 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15788 || (MATRIX_ROW_START_CHARPOS (row) == PT
15789 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15790 /* If we didn't find any candidate rows, or exited the
15791 loop before all the candidates were examined, signal
15792 to the caller that this method failed. */
15793 if (rc != CURSOR_MOVEMENT_SUCCESS
15794 && !(rv
15795 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15796 && !row->continued_p))
15797 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15798 else if (rv)
15799 rc = CURSOR_MOVEMENT_SUCCESS;
15800 }
15801 else
15802 {
15803 do
15804 {
15805 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15806 {
15807 rc = CURSOR_MOVEMENT_SUCCESS;
15808 break;
15809 }
15810 ++row;
15811 }
15812 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15813 && MATRIX_ROW_START_CHARPOS (row) == PT
15814 && cursor_row_p (row));
15815 }
15816 }
15817 }
15818
15819 return rc;
15820 }
15821
15822 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15823 static
15824 #endif
15825 void
15826 set_vertical_scroll_bar (struct window *w)
15827 {
15828 ptrdiff_t start, end, whole;
15829
15830 /* Calculate the start and end positions for the current window.
15831 At some point, it would be nice to choose between scrollbars
15832 which reflect the whole buffer size, with special markers
15833 indicating narrowing, and scrollbars which reflect only the
15834 visible region.
15835
15836 Note that mini-buffers sometimes aren't displaying any text. */
15837 if (!MINI_WINDOW_P (w)
15838 || (w == XWINDOW (minibuf_window)
15839 && NILP (echo_area_buffer[0])))
15840 {
15841 struct buffer *buf = XBUFFER (w->contents);
15842 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15843 start = marker_position (w->start) - BUF_BEGV (buf);
15844 /* I don't think this is guaranteed to be right. For the
15845 moment, we'll pretend it is. */
15846 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15847
15848 if (end < start)
15849 end = start;
15850 if (whole < (end - start))
15851 whole = end - start;
15852 }
15853 else
15854 start = end = whole = 0;
15855
15856 /* Indicate what this scroll bar ought to be displaying now. */
15857 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15858 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15859 (w, end - start, whole, start);
15860 }
15861
15862
15863 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15864 selected_window is redisplayed.
15865
15866 We can return without actually redisplaying the window if fonts has been
15867 changed on window's frame. In that case, redisplay_internal will retry.
15868
15869 As one of the important parts of redisplaying a window, we need to
15870 decide whether the previous window-start position (stored in the
15871 window's w->start marker position) is still valid, and if it isn't,
15872 recompute it. Some details about that:
15873
15874 . The previous window-start could be in a continuation line, in
15875 which case we need to recompute it when the window width
15876 changes. See compute_window_start_on_continuation_line and its
15877 call below.
15878
15879 . The text that changed since last redisplay could include the
15880 previous window-start position. In that case, we try to salvage
15881 what we can from the current glyph matrix by calling
15882 try_scrolling, which see.
15883
15884 . Some Emacs command could force us to use a specific window-start
15885 position by setting the window's force_start flag, or gently
15886 propose doing that by setting the window's optional_new_start
15887 flag. In these cases, we try using the specified start point if
15888 that succeeds (i.e. the window desired matrix is successfully
15889 recomputed, and point location is within the window). In case
15890 of optional_new_start, we first check if the specified start
15891 position is feasible, i.e. if it will allow point to be
15892 displayed in the window. If using the specified start point
15893 fails, e.g., if new fonts are needed to be loaded, we abort the
15894 redisplay cycle and leave it up to the next cycle to figure out
15895 things.
15896
15897 . Note that the window's force_start flag is sometimes set by
15898 redisplay itself, when it decides that the previous window start
15899 point is fine and should be kept. Search for "goto force_start"
15900 below to see the details. Like the values of window-start
15901 specified outside of redisplay, these internally-deduced values
15902 are tested for feasibility, and ignored if found to be
15903 unfeasible.
15904
15905 . Note that the function try_window, used to completely redisplay
15906 a window, accepts the window's start point as its argument.
15907 This is used several times in the redisplay code to control
15908 where the window start will be, according to user options such
15909 as scroll-conservatively, and also to ensure the screen line
15910 showing point will be fully (as opposed to partially) visible on
15911 display. */
15912
15913 static void
15914 redisplay_window (Lisp_Object window, bool just_this_one_p)
15915 {
15916 struct window *w = XWINDOW (window);
15917 struct frame *f = XFRAME (w->frame);
15918 struct buffer *buffer = XBUFFER (w->contents);
15919 struct buffer *old = current_buffer;
15920 struct text_pos lpoint, opoint, startp;
15921 int update_mode_line;
15922 int tem;
15923 struct it it;
15924 /* Record it now because it's overwritten. */
15925 bool current_matrix_up_to_date_p = false;
15926 bool used_current_matrix_p = false;
15927 /* This is less strict than current_matrix_up_to_date_p.
15928 It indicates that the buffer contents and narrowing are unchanged. */
15929 bool buffer_unchanged_p = false;
15930 int temp_scroll_step = 0;
15931 ptrdiff_t count = SPECPDL_INDEX ();
15932 int rc;
15933 int centering_position = -1;
15934 int last_line_misfit = 0;
15935 ptrdiff_t beg_unchanged, end_unchanged;
15936 int frame_line_height;
15937
15938 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15939 opoint = lpoint;
15940
15941 #ifdef GLYPH_DEBUG
15942 *w->desired_matrix->method = 0;
15943 #endif
15944
15945 if (!just_this_one_p
15946 && REDISPLAY_SOME_P ()
15947 && !w->redisplay
15948 && !f->redisplay
15949 && !buffer->text->redisplay
15950 && BUF_PT (buffer) == w->last_point)
15951 return;
15952
15953 /* Make sure that both W's markers are valid. */
15954 eassert (XMARKER (w->start)->buffer == buffer);
15955 eassert (XMARKER (w->pointm)->buffer == buffer);
15956
15957 /* We come here again if we need to run window-text-change-functions
15958 below. */
15959 restart:
15960 reconsider_clip_changes (w);
15961 frame_line_height = default_line_pixel_height (w);
15962
15963 /* Has the mode line to be updated? */
15964 update_mode_line = (w->update_mode_line
15965 || update_mode_lines
15966 || buffer->clip_changed
15967 || buffer->prevent_redisplay_optimizations_p);
15968
15969 if (!just_this_one_p)
15970 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15971 cleverly elsewhere. */
15972 w->must_be_updated_p = true;
15973
15974 if (MINI_WINDOW_P (w))
15975 {
15976 if (w == XWINDOW (echo_area_window)
15977 && !NILP (echo_area_buffer[0]))
15978 {
15979 if (update_mode_line)
15980 /* We may have to update a tty frame's menu bar or a
15981 tool-bar. Example `M-x C-h C-h C-g'. */
15982 goto finish_menu_bars;
15983 else
15984 /* We've already displayed the echo area glyphs in this window. */
15985 goto finish_scroll_bars;
15986 }
15987 else if ((w != XWINDOW (minibuf_window)
15988 || minibuf_level == 0)
15989 /* When buffer is nonempty, redisplay window normally. */
15990 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15991 /* Quail displays non-mini buffers in minibuffer window.
15992 In that case, redisplay the window normally. */
15993 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15994 {
15995 /* W is a mini-buffer window, but it's not active, so clear
15996 it. */
15997 int yb = window_text_bottom_y (w);
15998 struct glyph_row *row;
15999 int y;
16000
16001 for (y = 0, row = w->desired_matrix->rows;
16002 y < yb;
16003 y += row->height, ++row)
16004 blank_row (w, row, y);
16005 goto finish_scroll_bars;
16006 }
16007
16008 clear_glyph_matrix (w->desired_matrix);
16009 }
16010
16011 /* Otherwise set up data on this window; select its buffer and point
16012 value. */
16013 /* Really select the buffer, for the sake of buffer-local
16014 variables. */
16015 set_buffer_internal_1 (XBUFFER (w->contents));
16016
16017 current_matrix_up_to_date_p
16018 = (w->window_end_valid
16019 && !current_buffer->clip_changed
16020 && !current_buffer->prevent_redisplay_optimizations_p
16021 && !window_outdated (w));
16022
16023 /* Run the window-text-change-functions
16024 if it is possible that the text on the screen has changed
16025 (either due to modification of the text, or any other reason). */
16026 if (!current_matrix_up_to_date_p
16027 && !NILP (Vwindow_text_change_functions))
16028 {
16029 safe_run_hooks (Qwindow_text_change_functions);
16030 goto restart;
16031 }
16032
16033 beg_unchanged = BEG_UNCHANGED;
16034 end_unchanged = END_UNCHANGED;
16035
16036 SET_TEXT_POS (opoint, PT, PT_BYTE);
16037
16038 specbind (Qinhibit_point_motion_hooks, Qt);
16039
16040 buffer_unchanged_p
16041 = (w->window_end_valid
16042 && !current_buffer->clip_changed
16043 && !window_outdated (w));
16044
16045 /* When windows_or_buffers_changed is non-zero, we can't rely
16046 on the window end being valid, so set it to zero there. */
16047 if (windows_or_buffers_changed)
16048 {
16049 /* If window starts on a continuation line, maybe adjust the
16050 window start in case the window's width changed. */
16051 if (XMARKER (w->start)->buffer == current_buffer)
16052 compute_window_start_on_continuation_line (w);
16053
16054 w->window_end_valid = false;
16055 /* If so, we also can't rely on current matrix
16056 and should not fool try_cursor_movement below. */
16057 current_matrix_up_to_date_p = false;
16058 }
16059
16060 /* Some sanity checks. */
16061 CHECK_WINDOW_END (w);
16062 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16063 emacs_abort ();
16064 if (BYTEPOS (opoint) < CHARPOS (opoint))
16065 emacs_abort ();
16066
16067 if (mode_line_update_needed (w))
16068 update_mode_line = 1;
16069
16070 /* Point refers normally to the selected window. For any other
16071 window, set up appropriate value. */
16072 if (!EQ (window, selected_window))
16073 {
16074 ptrdiff_t new_pt = marker_position (w->pointm);
16075 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16076 if (new_pt < BEGV)
16077 {
16078 new_pt = BEGV;
16079 new_pt_byte = BEGV_BYTE;
16080 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16081 }
16082 else if (new_pt > (ZV - 1))
16083 {
16084 new_pt = ZV;
16085 new_pt_byte = ZV_BYTE;
16086 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16087 }
16088
16089 /* We don't use SET_PT so that the point-motion hooks don't run. */
16090 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16091 }
16092
16093 /* If any of the character widths specified in the display table
16094 have changed, invalidate the width run cache. It's true that
16095 this may be a bit late to catch such changes, but the rest of
16096 redisplay goes (non-fatally) haywire when the display table is
16097 changed, so why should we worry about doing any better? */
16098 if (current_buffer->width_run_cache
16099 || (current_buffer->base_buffer
16100 && current_buffer->base_buffer->width_run_cache))
16101 {
16102 struct Lisp_Char_Table *disptab = buffer_display_table ();
16103
16104 if (! disptab_matches_widthtab
16105 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16106 {
16107 struct buffer *buf = current_buffer;
16108
16109 if (buf->base_buffer)
16110 buf = buf->base_buffer;
16111 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16112 recompute_width_table (current_buffer, disptab);
16113 }
16114 }
16115
16116 /* If window-start is screwed up, choose a new one. */
16117 if (XMARKER (w->start)->buffer != current_buffer)
16118 goto recenter;
16119
16120 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16121
16122 /* If someone specified a new starting point but did not insist,
16123 check whether it can be used. */
16124 if (w->optional_new_start
16125 && CHARPOS (startp) >= BEGV
16126 && CHARPOS (startp) <= ZV)
16127 {
16128 w->optional_new_start = 0;
16129 start_display (&it, w, startp);
16130 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16131 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16132 if (IT_CHARPOS (it) == PT)
16133 w->force_start = 1;
16134 /* IT may overshoot PT if text at PT is invisible. */
16135 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
16136 w->force_start = 1;
16137 }
16138
16139 force_start:
16140
16141 /* Handle case where place to start displaying has been specified,
16142 unless the specified location is outside the accessible range. */
16143 if (w->force_start || window_frozen_p (w))
16144 {
16145 /* We set this later on if we have to adjust point. */
16146 int new_vpos = -1;
16147
16148 w->force_start = 0;
16149 w->vscroll = 0;
16150 w->window_end_valid = 0;
16151
16152 /* Forget any recorded base line for line number display. */
16153 if (!buffer_unchanged_p)
16154 w->base_line_number = 0;
16155
16156 /* Redisplay the mode line. Select the buffer properly for that.
16157 Also, run the hook window-scroll-functions
16158 because we have scrolled. */
16159 /* Note, we do this after clearing force_start because
16160 if there's an error, it is better to forget about force_start
16161 than to get into an infinite loop calling the hook functions
16162 and having them get more errors. */
16163 if (!update_mode_line
16164 || ! NILP (Vwindow_scroll_functions))
16165 {
16166 update_mode_line = 1;
16167 w->update_mode_line = 1;
16168 startp = run_window_scroll_functions (window, startp);
16169 }
16170
16171 if (CHARPOS (startp) < BEGV)
16172 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16173 else if (CHARPOS (startp) > ZV)
16174 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16175
16176 /* Redisplay, then check if cursor has been set during the
16177 redisplay. Give up if new fonts were loaded. */
16178 /* We used to issue a CHECK_MARGINS argument to try_window here,
16179 but this causes scrolling to fail when point begins inside
16180 the scroll margin (bug#148) -- cyd */
16181 if (!try_window (window, startp, 0))
16182 {
16183 w->force_start = 1;
16184 clear_glyph_matrix (w->desired_matrix);
16185 goto need_larger_matrices;
16186 }
16187
16188 if (w->cursor.vpos < 0 && !window_frozen_p (w))
16189 {
16190 /* If point does not appear, try to move point so it does
16191 appear. The desired matrix has been built above, so we
16192 can use it here. */
16193 new_vpos = window_box_height (w) / 2;
16194 }
16195
16196 if (!cursor_row_fully_visible_p (w, 0, 0))
16197 {
16198 /* Point does appear, but on a line partly visible at end of window.
16199 Move it back to a fully-visible line. */
16200 new_vpos = window_box_height (w);
16201 /* But if window_box_height suggests a Y coordinate that is
16202 not less than we already have, that line will clearly not
16203 be fully visible, so give up and scroll the display.
16204 This can happen when the default face uses a font whose
16205 dimensions are different from the frame's default
16206 font. */
16207 if (new_vpos >= w->cursor.y)
16208 {
16209 w->cursor.vpos = -1;
16210 clear_glyph_matrix (w->desired_matrix);
16211 goto try_to_scroll;
16212 }
16213 }
16214 else if (w->cursor.vpos >= 0)
16215 {
16216 /* Some people insist on not letting point enter the scroll
16217 margin, even though this part handles windows that didn't
16218 scroll at all. */
16219 int window_total_lines
16220 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16221 int margin = min (scroll_margin, window_total_lines / 4);
16222 int pixel_margin = margin * frame_line_height;
16223 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16224
16225 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16226 below, which finds the row to move point to, advances by
16227 the Y coordinate of the _next_ row, see the definition of
16228 MATRIX_ROW_BOTTOM_Y. */
16229 if (w->cursor.vpos < margin + header_line)
16230 {
16231 w->cursor.vpos = -1;
16232 clear_glyph_matrix (w->desired_matrix);
16233 goto try_to_scroll;
16234 }
16235 else
16236 {
16237 int window_height = window_box_height (w);
16238
16239 if (header_line)
16240 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16241 if (w->cursor.y >= window_height - pixel_margin)
16242 {
16243 w->cursor.vpos = -1;
16244 clear_glyph_matrix (w->desired_matrix);
16245 goto try_to_scroll;
16246 }
16247 }
16248 }
16249
16250 /* If we need to move point for either of the above reasons,
16251 now actually do it. */
16252 if (new_vpos >= 0)
16253 {
16254 struct glyph_row *row;
16255
16256 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16257 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16258 ++row;
16259
16260 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16261 MATRIX_ROW_START_BYTEPOS (row));
16262
16263 if (w != XWINDOW (selected_window))
16264 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16265 else if (current_buffer == old)
16266 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16267
16268 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16269
16270 /* If we are highlighting the region, then we just changed
16271 the region, so redisplay to show it. */
16272 /* FIXME: We need to (re)run pre-redisplay-function! */
16273 /* if (markpos_of_region () >= 0)
16274 {
16275 clear_glyph_matrix (w->desired_matrix);
16276 if (!try_window (window, startp, 0))
16277 goto need_larger_matrices;
16278 }
16279 */
16280 }
16281
16282 #ifdef GLYPH_DEBUG
16283 debug_method_add (w, "forced window start");
16284 #endif
16285 goto done;
16286 }
16287
16288 /* Handle case where text has not changed, only point, and it has
16289 not moved off the frame, and we are not retrying after hscroll.
16290 (current_matrix_up_to_date_p is nonzero when retrying.) */
16291 if (current_matrix_up_to_date_p
16292 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16293 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16294 {
16295 switch (rc)
16296 {
16297 case CURSOR_MOVEMENT_SUCCESS:
16298 used_current_matrix_p = 1;
16299 goto done;
16300
16301 case CURSOR_MOVEMENT_MUST_SCROLL:
16302 goto try_to_scroll;
16303
16304 default:
16305 emacs_abort ();
16306 }
16307 }
16308 /* If current starting point was originally the beginning of a line
16309 but no longer is, find a new starting point. */
16310 else if (w->start_at_line_beg
16311 && !(CHARPOS (startp) <= BEGV
16312 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16313 {
16314 #ifdef GLYPH_DEBUG
16315 debug_method_add (w, "recenter 1");
16316 #endif
16317 goto recenter;
16318 }
16319
16320 /* Try scrolling with try_window_id. Value is > 0 if update has
16321 been done, it is -1 if we know that the same window start will
16322 not work. It is 0 if unsuccessful for some other reason. */
16323 else if ((tem = try_window_id (w)) != 0)
16324 {
16325 #ifdef GLYPH_DEBUG
16326 debug_method_add (w, "try_window_id %d", tem);
16327 #endif
16328
16329 if (f->fonts_changed)
16330 goto need_larger_matrices;
16331 if (tem > 0)
16332 goto done;
16333
16334 /* Otherwise try_window_id has returned -1 which means that we
16335 don't want the alternative below this comment to execute. */
16336 }
16337 else if (CHARPOS (startp) >= BEGV
16338 && CHARPOS (startp) <= ZV
16339 && PT >= CHARPOS (startp)
16340 && (CHARPOS (startp) < ZV
16341 /* Avoid starting at end of buffer. */
16342 || CHARPOS (startp) == BEGV
16343 || !window_outdated (w)))
16344 {
16345 int d1, d2, d3, d4, d5, d6;
16346
16347 /* If first window line is a continuation line, and window start
16348 is inside the modified region, but the first change is before
16349 current window start, we must select a new window start.
16350
16351 However, if this is the result of a down-mouse event (e.g. by
16352 extending the mouse-drag-overlay), we don't want to select a
16353 new window start, since that would change the position under
16354 the mouse, resulting in an unwanted mouse-movement rather
16355 than a simple mouse-click. */
16356 if (!w->start_at_line_beg
16357 && NILP (do_mouse_tracking)
16358 && CHARPOS (startp) > BEGV
16359 && CHARPOS (startp) > BEG + beg_unchanged
16360 && CHARPOS (startp) <= Z - end_unchanged
16361 /* Even if w->start_at_line_beg is nil, a new window may
16362 start at a line_beg, since that's how set_buffer_window
16363 sets it. So, we need to check the return value of
16364 compute_window_start_on_continuation_line. (See also
16365 bug#197). */
16366 && XMARKER (w->start)->buffer == current_buffer
16367 && compute_window_start_on_continuation_line (w)
16368 /* It doesn't make sense to force the window start like we
16369 do at label force_start if it is already known that point
16370 will not be visible in the resulting window, because
16371 doing so will move point from its correct position
16372 instead of scrolling the window to bring point into view.
16373 See bug#9324. */
16374 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16375 {
16376 w->force_start = 1;
16377 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16378 goto force_start;
16379 }
16380
16381 #ifdef GLYPH_DEBUG
16382 debug_method_add (w, "same window start");
16383 #endif
16384
16385 /* Try to redisplay starting at same place as before.
16386 If point has not moved off frame, accept the results. */
16387 if (!current_matrix_up_to_date_p
16388 /* Don't use try_window_reusing_current_matrix in this case
16389 because a window scroll function can have changed the
16390 buffer. */
16391 || !NILP (Vwindow_scroll_functions)
16392 || MINI_WINDOW_P (w)
16393 || !(used_current_matrix_p
16394 = try_window_reusing_current_matrix (w)))
16395 {
16396 IF_DEBUG (debug_method_add (w, "1"));
16397 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16398 /* -1 means we need to scroll.
16399 0 means we need new matrices, but fonts_changed
16400 is set in that case, so we will detect it below. */
16401 goto try_to_scroll;
16402 }
16403
16404 if (f->fonts_changed)
16405 goto need_larger_matrices;
16406
16407 if (w->cursor.vpos >= 0)
16408 {
16409 if (!just_this_one_p
16410 || current_buffer->clip_changed
16411 || BEG_UNCHANGED < CHARPOS (startp))
16412 /* Forget any recorded base line for line number display. */
16413 w->base_line_number = 0;
16414
16415 if (!cursor_row_fully_visible_p (w, 1, 0))
16416 {
16417 clear_glyph_matrix (w->desired_matrix);
16418 last_line_misfit = 1;
16419 }
16420 /* Drop through and scroll. */
16421 else
16422 goto done;
16423 }
16424 else
16425 clear_glyph_matrix (w->desired_matrix);
16426 }
16427
16428 try_to_scroll:
16429
16430 /* Redisplay the mode line. Select the buffer properly for that. */
16431 if (!update_mode_line)
16432 {
16433 update_mode_line = 1;
16434 w->update_mode_line = 1;
16435 }
16436
16437 /* Try to scroll by specified few lines. */
16438 if ((scroll_conservatively
16439 || emacs_scroll_step
16440 || temp_scroll_step
16441 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16442 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16443 && CHARPOS (startp) >= BEGV
16444 && CHARPOS (startp) <= ZV)
16445 {
16446 /* The function returns -1 if new fonts were loaded, 1 if
16447 successful, 0 if not successful. */
16448 int ss = try_scrolling (window, just_this_one_p,
16449 scroll_conservatively,
16450 emacs_scroll_step,
16451 temp_scroll_step, last_line_misfit);
16452 switch (ss)
16453 {
16454 case SCROLLING_SUCCESS:
16455 goto done;
16456
16457 case SCROLLING_NEED_LARGER_MATRICES:
16458 goto need_larger_matrices;
16459
16460 case SCROLLING_FAILED:
16461 break;
16462
16463 default:
16464 emacs_abort ();
16465 }
16466 }
16467
16468 /* Finally, just choose a place to start which positions point
16469 according to user preferences. */
16470
16471 recenter:
16472
16473 #ifdef GLYPH_DEBUG
16474 debug_method_add (w, "recenter");
16475 #endif
16476
16477 /* Forget any previously recorded base line for line number display. */
16478 if (!buffer_unchanged_p)
16479 w->base_line_number = 0;
16480
16481 /* Determine the window start relative to point. */
16482 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16483 it.current_y = it.last_visible_y;
16484 if (centering_position < 0)
16485 {
16486 int window_total_lines
16487 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16488 int margin =
16489 scroll_margin > 0
16490 ? min (scroll_margin, window_total_lines / 4)
16491 : 0;
16492 ptrdiff_t margin_pos = CHARPOS (startp);
16493 Lisp_Object aggressive;
16494 int scrolling_up;
16495
16496 /* If there is a scroll margin at the top of the window, find
16497 its character position. */
16498 if (margin
16499 /* Cannot call start_display if startp is not in the
16500 accessible region of the buffer. This can happen when we
16501 have just switched to a different buffer and/or changed
16502 its restriction. In that case, startp is initialized to
16503 the character position 1 (BEGV) because we did not yet
16504 have chance to display the buffer even once. */
16505 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16506 {
16507 struct it it1;
16508 void *it1data = NULL;
16509
16510 SAVE_IT (it1, it, it1data);
16511 start_display (&it1, w, startp);
16512 move_it_vertically (&it1, margin * frame_line_height);
16513 margin_pos = IT_CHARPOS (it1);
16514 RESTORE_IT (&it, &it, it1data);
16515 }
16516 scrolling_up = PT > margin_pos;
16517 aggressive =
16518 scrolling_up
16519 ? BVAR (current_buffer, scroll_up_aggressively)
16520 : BVAR (current_buffer, scroll_down_aggressively);
16521
16522 if (!MINI_WINDOW_P (w)
16523 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16524 {
16525 int pt_offset = 0;
16526
16527 /* Setting scroll-conservatively overrides
16528 scroll-*-aggressively. */
16529 if (!scroll_conservatively && NUMBERP (aggressive))
16530 {
16531 double float_amount = XFLOATINT (aggressive);
16532
16533 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16534 if (pt_offset == 0 && float_amount > 0)
16535 pt_offset = 1;
16536 if (pt_offset && margin > 0)
16537 margin -= 1;
16538 }
16539 /* Compute how much to move the window start backward from
16540 point so that point will be displayed where the user
16541 wants it. */
16542 if (scrolling_up)
16543 {
16544 centering_position = it.last_visible_y;
16545 if (pt_offset)
16546 centering_position -= pt_offset;
16547 centering_position -=
16548 frame_line_height * (1 + margin + (last_line_misfit != 0))
16549 + WINDOW_HEADER_LINE_HEIGHT (w);
16550 /* Don't let point enter the scroll margin near top of
16551 the window. */
16552 if (centering_position < margin * frame_line_height)
16553 centering_position = margin * frame_line_height;
16554 }
16555 else
16556 centering_position = margin * frame_line_height + pt_offset;
16557 }
16558 else
16559 /* Set the window start half the height of the window backward
16560 from point. */
16561 centering_position = window_box_height (w) / 2;
16562 }
16563 move_it_vertically_backward (&it, centering_position);
16564
16565 eassert (IT_CHARPOS (it) >= BEGV);
16566
16567 /* The function move_it_vertically_backward may move over more
16568 than the specified y-distance. If it->w is small, e.g. a
16569 mini-buffer window, we may end up in front of the window's
16570 display area. Start displaying at the start of the line
16571 containing PT in this case. */
16572 if (it.current_y <= 0)
16573 {
16574 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16575 move_it_vertically_backward (&it, 0);
16576 it.current_y = 0;
16577 }
16578
16579 it.current_x = it.hpos = 0;
16580
16581 /* Set the window start position here explicitly, to avoid an
16582 infinite loop in case the functions in window-scroll-functions
16583 get errors. */
16584 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16585
16586 /* Run scroll hooks. */
16587 startp = run_window_scroll_functions (window, it.current.pos);
16588
16589 /* Redisplay the window. */
16590 if (!current_matrix_up_to_date_p
16591 || windows_or_buffers_changed
16592 || f->cursor_type_changed
16593 /* Don't use try_window_reusing_current_matrix in this case
16594 because it can have changed the buffer. */
16595 || !NILP (Vwindow_scroll_functions)
16596 || !just_this_one_p
16597 || MINI_WINDOW_P (w)
16598 || !(used_current_matrix_p
16599 = try_window_reusing_current_matrix (w)))
16600 try_window (window, startp, 0);
16601
16602 /* If new fonts have been loaded (due to fontsets), give up. We
16603 have to start a new redisplay since we need to re-adjust glyph
16604 matrices. */
16605 if (f->fonts_changed)
16606 goto need_larger_matrices;
16607
16608 /* If cursor did not appear assume that the middle of the window is
16609 in the first line of the window. Do it again with the next line.
16610 (Imagine a window of height 100, displaying two lines of height
16611 60. Moving back 50 from it->last_visible_y will end in the first
16612 line.) */
16613 if (w->cursor.vpos < 0)
16614 {
16615 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16616 {
16617 clear_glyph_matrix (w->desired_matrix);
16618 move_it_by_lines (&it, 1);
16619 try_window (window, it.current.pos, 0);
16620 }
16621 else if (PT < IT_CHARPOS (it))
16622 {
16623 clear_glyph_matrix (w->desired_matrix);
16624 move_it_by_lines (&it, -1);
16625 try_window (window, it.current.pos, 0);
16626 }
16627 else
16628 {
16629 /* Not much we can do about it. */
16630 }
16631 }
16632
16633 /* Consider the following case: Window starts at BEGV, there is
16634 invisible, intangible text at BEGV, so that display starts at
16635 some point START > BEGV. It can happen that we are called with
16636 PT somewhere between BEGV and START. Try to handle that case,
16637 and similar ones. */
16638 if (w->cursor.vpos < 0)
16639 {
16640 /* First, try locating the proper glyph row for PT. */
16641 struct glyph_row *row =
16642 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16643
16644 /* Sometimes point is at the beginning of invisible text that is
16645 before the 1st character displayed in the row. In that case,
16646 row_containing_pos fails to find the row, because no glyphs
16647 with appropriate buffer positions are present in the row.
16648 Therefore, we next try to find the row which shows the 1st
16649 position after the invisible text. */
16650 if (!row)
16651 {
16652 Lisp_Object val =
16653 get_char_property_and_overlay (make_number (PT), Qinvisible,
16654 Qnil, NULL);
16655
16656 if (TEXT_PROP_MEANS_INVISIBLE (val))
16657 {
16658 ptrdiff_t alt_pos;
16659 Lisp_Object invis_end =
16660 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16661 Qnil, Qnil);
16662
16663 if (NATNUMP (invis_end))
16664 alt_pos = XFASTINT (invis_end);
16665 else
16666 alt_pos = ZV;
16667 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16668 NULL, 0);
16669 }
16670 }
16671 /* Finally, fall back on the first row of the window after the
16672 header line (if any). This is slightly better than not
16673 displaying the cursor at all. */
16674 if (!row)
16675 {
16676 row = w->current_matrix->rows;
16677 if (row->mode_line_p)
16678 ++row;
16679 }
16680 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16681 }
16682
16683 if (!cursor_row_fully_visible_p (w, 0, 0))
16684 {
16685 /* If vscroll is enabled, disable it and try again. */
16686 if (w->vscroll)
16687 {
16688 w->vscroll = 0;
16689 clear_glyph_matrix (w->desired_matrix);
16690 goto recenter;
16691 }
16692
16693 /* Users who set scroll-conservatively to a large number want
16694 point just above/below the scroll margin. If we ended up
16695 with point's row partially visible, move the window start to
16696 make that row fully visible and out of the margin. */
16697 if (scroll_conservatively > SCROLL_LIMIT)
16698 {
16699 int window_total_lines
16700 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16701 int margin =
16702 scroll_margin > 0
16703 ? min (scroll_margin, window_total_lines / 4)
16704 : 0;
16705 int move_down = w->cursor.vpos >= window_total_lines / 2;
16706
16707 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16708 clear_glyph_matrix (w->desired_matrix);
16709 if (1 == try_window (window, it.current.pos,
16710 TRY_WINDOW_CHECK_MARGINS))
16711 goto done;
16712 }
16713
16714 /* If centering point failed to make the whole line visible,
16715 put point at the top instead. That has to make the whole line
16716 visible, if it can be done. */
16717 if (centering_position == 0)
16718 goto done;
16719
16720 clear_glyph_matrix (w->desired_matrix);
16721 centering_position = 0;
16722 goto recenter;
16723 }
16724
16725 done:
16726
16727 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16728 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16729 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16730
16731 /* Display the mode line, if we must. */
16732 if ((update_mode_line
16733 /* If window not full width, must redo its mode line
16734 if (a) the window to its side is being redone and
16735 (b) we do a frame-based redisplay. This is a consequence
16736 of how inverted lines are drawn in frame-based redisplay. */
16737 || (!just_this_one_p
16738 && !FRAME_WINDOW_P (f)
16739 && !WINDOW_FULL_WIDTH_P (w))
16740 /* Line number to display. */
16741 || w->base_line_pos > 0
16742 /* Column number is displayed and different from the one displayed. */
16743 || (w->column_number_displayed != -1
16744 && (w->column_number_displayed != current_column ())))
16745 /* This means that the window has a mode line. */
16746 && (WINDOW_WANTS_MODELINE_P (w)
16747 || WINDOW_WANTS_HEADER_LINE_P (w)))
16748 {
16749
16750 display_mode_lines (w);
16751
16752 /* If mode line height has changed, arrange for a thorough
16753 immediate redisplay using the correct mode line height. */
16754 if (WINDOW_WANTS_MODELINE_P (w)
16755 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16756 {
16757 f->fonts_changed = 1;
16758 w->mode_line_height = -1;
16759 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16760 = DESIRED_MODE_LINE_HEIGHT (w);
16761 }
16762
16763 /* If header line height has changed, arrange for a thorough
16764 immediate redisplay using the correct header line height. */
16765 if (WINDOW_WANTS_HEADER_LINE_P (w)
16766 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16767 {
16768 f->fonts_changed = 1;
16769 w->header_line_height = -1;
16770 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16771 = DESIRED_HEADER_LINE_HEIGHT (w);
16772 }
16773
16774 if (f->fonts_changed)
16775 goto need_larger_matrices;
16776 }
16777
16778 if (!line_number_displayed && w->base_line_pos != -1)
16779 {
16780 w->base_line_pos = 0;
16781 w->base_line_number = 0;
16782 }
16783
16784 finish_menu_bars:
16785
16786 /* When we reach a frame's selected window, redo the frame's menu bar. */
16787 if (update_mode_line
16788 && EQ (FRAME_SELECTED_WINDOW (f), window))
16789 {
16790 int redisplay_menu_p = 0;
16791
16792 if (FRAME_WINDOW_P (f))
16793 {
16794 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16795 || defined (HAVE_NS) || defined (USE_GTK)
16796 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16797 #else
16798 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16799 #endif
16800 }
16801 else
16802 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16803
16804 if (redisplay_menu_p)
16805 display_menu_bar (w);
16806
16807 #ifdef HAVE_WINDOW_SYSTEM
16808 if (FRAME_WINDOW_P (f))
16809 {
16810 #if defined (USE_GTK) || defined (HAVE_NS)
16811 if (FRAME_EXTERNAL_TOOL_BAR (f))
16812 redisplay_tool_bar (f);
16813 #else
16814 if (WINDOWP (f->tool_bar_window)
16815 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16816 || !NILP (Vauto_resize_tool_bars))
16817 && redisplay_tool_bar (f))
16818 ignore_mouse_drag_p = 1;
16819 #endif
16820 }
16821 #endif
16822 }
16823
16824 #ifdef HAVE_WINDOW_SYSTEM
16825 if (FRAME_WINDOW_P (f)
16826 && update_window_fringes (w, (just_this_one_p
16827 || (!used_current_matrix_p && !overlay_arrow_seen)
16828 || w->pseudo_window_p)))
16829 {
16830 update_begin (f);
16831 block_input ();
16832 if (draw_window_fringes (w, 1))
16833 {
16834 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16835 x_draw_right_divider (w);
16836 else
16837 x_draw_vertical_border (w);
16838 }
16839 unblock_input ();
16840 update_end (f);
16841 }
16842
16843 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16844 x_draw_bottom_divider (w);
16845 #endif /* HAVE_WINDOW_SYSTEM */
16846
16847 /* We go to this label, with fonts_changed set, if it is
16848 necessary to try again using larger glyph matrices.
16849 We have to redeem the scroll bar even in this case,
16850 because the loop in redisplay_internal expects that. */
16851 need_larger_matrices:
16852 ;
16853 finish_scroll_bars:
16854
16855 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16856 {
16857 /* Set the thumb's position and size. */
16858 set_vertical_scroll_bar (w);
16859
16860 /* Note that we actually used the scroll bar attached to this
16861 window, so it shouldn't be deleted at the end of redisplay. */
16862 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16863 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16864 }
16865
16866 /* Restore current_buffer and value of point in it. The window
16867 update may have changed the buffer, so first make sure `opoint'
16868 is still valid (Bug#6177). */
16869 if (CHARPOS (opoint) < BEGV)
16870 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16871 else if (CHARPOS (opoint) > ZV)
16872 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16873 else
16874 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16875
16876 set_buffer_internal_1 (old);
16877 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16878 shorter. This can be caused by log truncation in *Messages*. */
16879 if (CHARPOS (lpoint) <= ZV)
16880 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16881
16882 unbind_to (count, Qnil);
16883 }
16884
16885
16886 /* Build the complete desired matrix of WINDOW with a window start
16887 buffer position POS.
16888
16889 Value is 1 if successful. It is zero if fonts were loaded during
16890 redisplay which makes re-adjusting glyph matrices necessary, and -1
16891 if point would appear in the scroll margins.
16892 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16893 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16894 set in FLAGS.) */
16895
16896 int
16897 try_window (Lisp_Object window, struct text_pos pos, int flags)
16898 {
16899 struct window *w = XWINDOW (window);
16900 struct it it;
16901 struct glyph_row *last_text_row = NULL;
16902 struct frame *f = XFRAME (w->frame);
16903 int frame_line_height = default_line_pixel_height (w);
16904
16905 /* Make POS the new window start. */
16906 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16907
16908 /* Mark cursor position as unknown. No overlay arrow seen. */
16909 w->cursor.vpos = -1;
16910 overlay_arrow_seen = 0;
16911
16912 /* Initialize iterator and info to start at POS. */
16913 start_display (&it, w, pos);
16914
16915 /* Display all lines of W. */
16916 while (it.current_y < it.last_visible_y)
16917 {
16918 if (display_line (&it))
16919 last_text_row = it.glyph_row - 1;
16920 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16921 return 0;
16922 }
16923
16924 /* Don't let the cursor end in the scroll margins. */
16925 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16926 && !MINI_WINDOW_P (w))
16927 {
16928 int this_scroll_margin;
16929 int window_total_lines
16930 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16931
16932 if (scroll_margin > 0)
16933 {
16934 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16935 this_scroll_margin *= frame_line_height;
16936 }
16937 else
16938 this_scroll_margin = 0;
16939
16940 if ((w->cursor.y >= 0 /* not vscrolled */
16941 && w->cursor.y < this_scroll_margin
16942 && CHARPOS (pos) > BEGV
16943 && IT_CHARPOS (it) < ZV)
16944 /* rms: considering make_cursor_line_fully_visible_p here
16945 seems to give wrong results. We don't want to recenter
16946 when the last line is partly visible, we want to allow
16947 that case to be handled in the usual way. */
16948 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16949 {
16950 w->cursor.vpos = -1;
16951 clear_glyph_matrix (w->desired_matrix);
16952 return -1;
16953 }
16954 }
16955
16956 /* If bottom moved off end of frame, change mode line percentage. */
16957 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16958 w->update_mode_line = 1;
16959
16960 /* Set window_end_pos to the offset of the last character displayed
16961 on the window from the end of current_buffer. Set
16962 window_end_vpos to its row number. */
16963 if (last_text_row)
16964 {
16965 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16966 adjust_window_ends (w, last_text_row, 0);
16967 eassert
16968 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16969 w->window_end_vpos)));
16970 }
16971 else
16972 {
16973 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16974 w->window_end_pos = Z - ZV;
16975 w->window_end_vpos = 0;
16976 }
16977
16978 /* But that is not valid info until redisplay finishes. */
16979 w->window_end_valid = 0;
16980 return 1;
16981 }
16982
16983
16984 \f
16985 /************************************************************************
16986 Window redisplay reusing current matrix when buffer has not changed
16987 ************************************************************************/
16988
16989 /* Try redisplay of window W showing an unchanged buffer with a
16990 different window start than the last time it was displayed by
16991 reusing its current matrix. Value is non-zero if successful.
16992 W->start is the new window start. */
16993
16994 static int
16995 try_window_reusing_current_matrix (struct window *w)
16996 {
16997 struct frame *f = XFRAME (w->frame);
16998 struct glyph_row *bottom_row;
16999 struct it it;
17000 struct run run;
17001 struct text_pos start, new_start;
17002 int nrows_scrolled, i;
17003 struct glyph_row *last_text_row;
17004 struct glyph_row *last_reused_text_row;
17005 struct glyph_row *start_row;
17006 int start_vpos, min_y, max_y;
17007
17008 #ifdef GLYPH_DEBUG
17009 if (inhibit_try_window_reusing)
17010 return 0;
17011 #endif
17012
17013 if (/* This function doesn't handle terminal frames. */
17014 !FRAME_WINDOW_P (f)
17015 /* Don't try to reuse the display if windows have been split
17016 or such. */
17017 || windows_or_buffers_changed
17018 || f->cursor_type_changed)
17019 return 0;
17020
17021 /* Can't do this if showing trailing whitespace. */
17022 if (!NILP (Vshow_trailing_whitespace))
17023 return 0;
17024
17025 /* If top-line visibility has changed, give up. */
17026 if (WINDOW_WANTS_HEADER_LINE_P (w)
17027 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17028 return 0;
17029
17030 /* Give up if old or new display is scrolled vertically. We could
17031 make this function handle this, but right now it doesn't. */
17032 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17033 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17034 return 0;
17035
17036 /* The variable new_start now holds the new window start. The old
17037 start `start' can be determined from the current matrix. */
17038 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17039 start = start_row->minpos;
17040 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17041
17042 /* Clear the desired matrix for the display below. */
17043 clear_glyph_matrix (w->desired_matrix);
17044
17045 if (CHARPOS (new_start) <= CHARPOS (start))
17046 {
17047 /* Don't use this method if the display starts with an ellipsis
17048 displayed for invisible text. It's not easy to handle that case
17049 below, and it's certainly not worth the effort since this is
17050 not a frequent case. */
17051 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17052 return 0;
17053
17054 IF_DEBUG (debug_method_add (w, "twu1"));
17055
17056 /* Display up to a row that can be reused. The variable
17057 last_text_row is set to the last row displayed that displays
17058 text. Note that it.vpos == 0 if or if not there is a
17059 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17060 start_display (&it, w, new_start);
17061 w->cursor.vpos = -1;
17062 last_text_row = last_reused_text_row = NULL;
17063
17064 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17065 {
17066 /* If we have reached into the characters in the START row,
17067 that means the line boundaries have changed. So we
17068 can't start copying with the row START. Maybe it will
17069 work to start copying with the following row. */
17070 while (IT_CHARPOS (it) > CHARPOS (start))
17071 {
17072 /* Advance to the next row as the "start". */
17073 start_row++;
17074 start = start_row->minpos;
17075 /* If there are no more rows to try, or just one, give up. */
17076 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17077 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17078 || CHARPOS (start) == ZV)
17079 {
17080 clear_glyph_matrix (w->desired_matrix);
17081 return 0;
17082 }
17083
17084 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17085 }
17086 /* If we have reached alignment, we can copy the rest of the
17087 rows. */
17088 if (IT_CHARPOS (it) == CHARPOS (start)
17089 /* Don't accept "alignment" inside a display vector,
17090 since start_row could have started in the middle of
17091 that same display vector (thus their character
17092 positions match), and we have no way of telling if
17093 that is the case. */
17094 && it.current.dpvec_index < 0)
17095 break;
17096
17097 if (display_line (&it))
17098 last_text_row = it.glyph_row - 1;
17099
17100 }
17101
17102 /* A value of current_y < last_visible_y means that we stopped
17103 at the previous window start, which in turn means that we
17104 have at least one reusable row. */
17105 if (it.current_y < it.last_visible_y)
17106 {
17107 struct glyph_row *row;
17108
17109 /* IT.vpos always starts from 0; it counts text lines. */
17110 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17111
17112 /* Find PT if not already found in the lines displayed. */
17113 if (w->cursor.vpos < 0)
17114 {
17115 int dy = it.current_y - start_row->y;
17116
17117 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17118 row = row_containing_pos (w, PT, row, NULL, dy);
17119 if (row)
17120 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17121 dy, nrows_scrolled);
17122 else
17123 {
17124 clear_glyph_matrix (w->desired_matrix);
17125 return 0;
17126 }
17127 }
17128
17129 /* Scroll the display. Do it before the current matrix is
17130 changed. The problem here is that update has not yet
17131 run, i.e. part of the current matrix is not up to date.
17132 scroll_run_hook will clear the cursor, and use the
17133 current matrix to get the height of the row the cursor is
17134 in. */
17135 run.current_y = start_row->y;
17136 run.desired_y = it.current_y;
17137 run.height = it.last_visible_y - it.current_y;
17138
17139 if (run.height > 0 && run.current_y != run.desired_y)
17140 {
17141 update_begin (f);
17142 FRAME_RIF (f)->update_window_begin_hook (w);
17143 FRAME_RIF (f)->clear_window_mouse_face (w);
17144 FRAME_RIF (f)->scroll_run_hook (w, &run);
17145 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17146 update_end (f);
17147 }
17148
17149 /* Shift current matrix down by nrows_scrolled lines. */
17150 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17151 rotate_matrix (w->current_matrix,
17152 start_vpos,
17153 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17154 nrows_scrolled);
17155
17156 /* Disable lines that must be updated. */
17157 for (i = 0; i < nrows_scrolled; ++i)
17158 (start_row + i)->enabled_p = false;
17159
17160 /* Re-compute Y positions. */
17161 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17162 max_y = it.last_visible_y;
17163 for (row = start_row + nrows_scrolled;
17164 row < bottom_row;
17165 ++row)
17166 {
17167 row->y = it.current_y;
17168 row->visible_height = row->height;
17169
17170 if (row->y < min_y)
17171 row->visible_height -= min_y - row->y;
17172 if (row->y + row->height > max_y)
17173 row->visible_height -= row->y + row->height - max_y;
17174 if (row->fringe_bitmap_periodic_p)
17175 row->redraw_fringe_bitmaps_p = 1;
17176
17177 it.current_y += row->height;
17178
17179 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17180 last_reused_text_row = row;
17181 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17182 break;
17183 }
17184
17185 /* Disable lines in the current matrix which are now
17186 below the window. */
17187 for (++row; row < bottom_row; ++row)
17188 row->enabled_p = row->mode_line_p = 0;
17189 }
17190
17191 /* Update window_end_pos etc.; last_reused_text_row is the last
17192 reused row from the current matrix containing text, if any.
17193 The value of last_text_row is the last displayed line
17194 containing text. */
17195 if (last_reused_text_row)
17196 adjust_window_ends (w, last_reused_text_row, 1);
17197 else if (last_text_row)
17198 adjust_window_ends (w, last_text_row, 0);
17199 else
17200 {
17201 /* This window must be completely empty. */
17202 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17203 w->window_end_pos = Z - ZV;
17204 w->window_end_vpos = 0;
17205 }
17206 w->window_end_valid = 0;
17207
17208 /* Update hint: don't try scrolling again in update_window. */
17209 w->desired_matrix->no_scrolling_p = 1;
17210
17211 #ifdef GLYPH_DEBUG
17212 debug_method_add (w, "try_window_reusing_current_matrix 1");
17213 #endif
17214 return 1;
17215 }
17216 else if (CHARPOS (new_start) > CHARPOS (start))
17217 {
17218 struct glyph_row *pt_row, *row;
17219 struct glyph_row *first_reusable_row;
17220 struct glyph_row *first_row_to_display;
17221 int dy;
17222 int yb = window_text_bottom_y (w);
17223
17224 /* Find the row starting at new_start, if there is one. Don't
17225 reuse a partially visible line at the end. */
17226 first_reusable_row = start_row;
17227 while (first_reusable_row->enabled_p
17228 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17229 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17230 < CHARPOS (new_start)))
17231 ++first_reusable_row;
17232
17233 /* Give up if there is no row to reuse. */
17234 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17235 || !first_reusable_row->enabled_p
17236 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17237 != CHARPOS (new_start)))
17238 return 0;
17239
17240 /* We can reuse fully visible rows beginning with
17241 first_reusable_row to the end of the window. Set
17242 first_row_to_display to the first row that cannot be reused.
17243 Set pt_row to the row containing point, if there is any. */
17244 pt_row = NULL;
17245 for (first_row_to_display = first_reusable_row;
17246 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17247 ++first_row_to_display)
17248 {
17249 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17250 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17251 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17252 && first_row_to_display->ends_at_zv_p
17253 && pt_row == NULL)))
17254 pt_row = first_row_to_display;
17255 }
17256
17257 /* Start displaying at the start of first_row_to_display. */
17258 eassert (first_row_to_display->y < yb);
17259 init_to_row_start (&it, w, first_row_to_display);
17260
17261 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17262 - start_vpos);
17263 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17264 - nrows_scrolled);
17265 it.current_y = (first_row_to_display->y - first_reusable_row->y
17266 + WINDOW_HEADER_LINE_HEIGHT (w));
17267
17268 /* Display lines beginning with first_row_to_display in the
17269 desired matrix. Set last_text_row to the last row displayed
17270 that displays text. */
17271 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17272 if (pt_row == NULL)
17273 w->cursor.vpos = -1;
17274 last_text_row = NULL;
17275 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17276 if (display_line (&it))
17277 last_text_row = it.glyph_row - 1;
17278
17279 /* If point is in a reused row, adjust y and vpos of the cursor
17280 position. */
17281 if (pt_row)
17282 {
17283 w->cursor.vpos -= nrows_scrolled;
17284 w->cursor.y -= first_reusable_row->y - start_row->y;
17285 }
17286
17287 /* Give up if point isn't in a row displayed or reused. (This
17288 also handles the case where w->cursor.vpos < nrows_scrolled
17289 after the calls to display_line, which can happen with scroll
17290 margins. See bug#1295.) */
17291 if (w->cursor.vpos < 0)
17292 {
17293 clear_glyph_matrix (w->desired_matrix);
17294 return 0;
17295 }
17296
17297 /* Scroll the display. */
17298 run.current_y = first_reusable_row->y;
17299 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17300 run.height = it.last_visible_y - run.current_y;
17301 dy = run.current_y - run.desired_y;
17302
17303 if (run.height)
17304 {
17305 update_begin (f);
17306 FRAME_RIF (f)->update_window_begin_hook (w);
17307 FRAME_RIF (f)->clear_window_mouse_face (w);
17308 FRAME_RIF (f)->scroll_run_hook (w, &run);
17309 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17310 update_end (f);
17311 }
17312
17313 /* Adjust Y positions of reused rows. */
17314 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17315 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17316 max_y = it.last_visible_y;
17317 for (row = first_reusable_row; row < first_row_to_display; ++row)
17318 {
17319 row->y -= dy;
17320 row->visible_height = row->height;
17321 if (row->y < min_y)
17322 row->visible_height -= min_y - row->y;
17323 if (row->y + row->height > max_y)
17324 row->visible_height -= row->y + row->height - max_y;
17325 if (row->fringe_bitmap_periodic_p)
17326 row->redraw_fringe_bitmaps_p = 1;
17327 }
17328
17329 /* Scroll the current matrix. */
17330 eassert (nrows_scrolled > 0);
17331 rotate_matrix (w->current_matrix,
17332 start_vpos,
17333 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17334 -nrows_scrolled);
17335
17336 /* Disable rows not reused. */
17337 for (row -= nrows_scrolled; row < bottom_row; ++row)
17338 row->enabled_p = false;
17339
17340 /* Point may have moved to a different line, so we cannot assume that
17341 the previous cursor position is valid; locate the correct row. */
17342 if (pt_row)
17343 {
17344 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17345 row < bottom_row
17346 && PT >= MATRIX_ROW_END_CHARPOS (row)
17347 && !row->ends_at_zv_p;
17348 row++)
17349 {
17350 w->cursor.vpos++;
17351 w->cursor.y = row->y;
17352 }
17353 if (row < bottom_row)
17354 {
17355 /* Can't simply scan the row for point with
17356 bidi-reordered glyph rows. Let set_cursor_from_row
17357 figure out where to put the cursor, and if it fails,
17358 give up. */
17359 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17360 {
17361 if (!set_cursor_from_row (w, row, w->current_matrix,
17362 0, 0, 0, 0))
17363 {
17364 clear_glyph_matrix (w->desired_matrix);
17365 return 0;
17366 }
17367 }
17368 else
17369 {
17370 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17371 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17372
17373 for (; glyph < end
17374 && (!BUFFERP (glyph->object)
17375 || glyph->charpos < PT);
17376 glyph++)
17377 {
17378 w->cursor.hpos++;
17379 w->cursor.x += glyph->pixel_width;
17380 }
17381 }
17382 }
17383 }
17384
17385 /* Adjust window end. A null value of last_text_row means that
17386 the window end is in reused rows which in turn means that
17387 only its vpos can have changed. */
17388 if (last_text_row)
17389 adjust_window_ends (w, last_text_row, 0);
17390 else
17391 w->window_end_vpos -= nrows_scrolled;
17392
17393 w->window_end_valid = 0;
17394 w->desired_matrix->no_scrolling_p = 1;
17395
17396 #ifdef GLYPH_DEBUG
17397 debug_method_add (w, "try_window_reusing_current_matrix 2");
17398 #endif
17399 return 1;
17400 }
17401
17402 return 0;
17403 }
17404
17405
17406 \f
17407 /************************************************************************
17408 Window redisplay reusing current matrix when buffer has changed
17409 ************************************************************************/
17410
17411 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17412 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17413 ptrdiff_t *, ptrdiff_t *);
17414 static struct glyph_row *
17415 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17416 struct glyph_row *);
17417
17418
17419 /* Return the last row in MATRIX displaying text. If row START is
17420 non-null, start searching with that row. IT gives the dimensions
17421 of the display. Value is null if matrix is empty; otherwise it is
17422 a pointer to the row found. */
17423
17424 static struct glyph_row *
17425 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17426 struct glyph_row *start)
17427 {
17428 struct glyph_row *row, *row_found;
17429
17430 /* Set row_found to the last row in IT->w's current matrix
17431 displaying text. The loop looks funny but think of partially
17432 visible lines. */
17433 row_found = NULL;
17434 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17435 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17436 {
17437 eassert (row->enabled_p);
17438 row_found = row;
17439 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17440 break;
17441 ++row;
17442 }
17443
17444 return row_found;
17445 }
17446
17447
17448 /* Return the last row in the current matrix of W that is not affected
17449 by changes at the start of current_buffer that occurred since W's
17450 current matrix was built. Value is null if no such row exists.
17451
17452 BEG_UNCHANGED us the number of characters unchanged at the start of
17453 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17454 first changed character in current_buffer. Characters at positions <
17455 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17456 when the current matrix was built. */
17457
17458 static struct glyph_row *
17459 find_last_unchanged_at_beg_row (struct window *w)
17460 {
17461 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17462 struct glyph_row *row;
17463 struct glyph_row *row_found = NULL;
17464 int yb = window_text_bottom_y (w);
17465
17466 /* Find the last row displaying unchanged text. */
17467 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17468 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17469 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17470 ++row)
17471 {
17472 if (/* If row ends before first_changed_pos, it is unchanged,
17473 except in some case. */
17474 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17475 /* When row ends in ZV and we write at ZV it is not
17476 unchanged. */
17477 && !row->ends_at_zv_p
17478 /* When first_changed_pos is the end of a continued line,
17479 row is not unchanged because it may be no longer
17480 continued. */
17481 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17482 && (row->continued_p
17483 || row->exact_window_width_line_p))
17484 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17485 needs to be recomputed, so don't consider this row as
17486 unchanged. This happens when the last line was
17487 bidi-reordered and was killed immediately before this
17488 redisplay cycle. In that case, ROW->end stores the
17489 buffer position of the first visual-order character of
17490 the killed text, which is now beyond ZV. */
17491 && CHARPOS (row->end.pos) <= ZV)
17492 row_found = row;
17493
17494 /* Stop if last visible row. */
17495 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17496 break;
17497 }
17498
17499 return row_found;
17500 }
17501
17502
17503 /* Find the first glyph row in the current matrix of W that is not
17504 affected by changes at the end of current_buffer since the
17505 time W's current matrix was built.
17506
17507 Return in *DELTA the number of chars by which buffer positions in
17508 unchanged text at the end of current_buffer must be adjusted.
17509
17510 Return in *DELTA_BYTES the corresponding number of bytes.
17511
17512 Value is null if no such row exists, i.e. all rows are affected by
17513 changes. */
17514
17515 static struct glyph_row *
17516 find_first_unchanged_at_end_row (struct window *w,
17517 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17518 {
17519 struct glyph_row *row;
17520 struct glyph_row *row_found = NULL;
17521
17522 *delta = *delta_bytes = 0;
17523
17524 /* Display must not have been paused, otherwise the current matrix
17525 is not up to date. */
17526 eassert (w->window_end_valid);
17527
17528 /* A value of window_end_pos >= END_UNCHANGED means that the window
17529 end is in the range of changed text. If so, there is no
17530 unchanged row at the end of W's current matrix. */
17531 if (w->window_end_pos >= END_UNCHANGED)
17532 return NULL;
17533
17534 /* Set row to the last row in W's current matrix displaying text. */
17535 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17536
17537 /* If matrix is entirely empty, no unchanged row exists. */
17538 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17539 {
17540 /* The value of row is the last glyph row in the matrix having a
17541 meaningful buffer position in it. The end position of row
17542 corresponds to window_end_pos. This allows us to translate
17543 buffer positions in the current matrix to current buffer
17544 positions for characters not in changed text. */
17545 ptrdiff_t Z_old =
17546 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17547 ptrdiff_t Z_BYTE_old =
17548 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17549 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17550 struct glyph_row *first_text_row
17551 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17552
17553 *delta = Z - Z_old;
17554 *delta_bytes = Z_BYTE - Z_BYTE_old;
17555
17556 /* Set last_unchanged_pos to the buffer position of the last
17557 character in the buffer that has not been changed. Z is the
17558 index + 1 of the last character in current_buffer, i.e. by
17559 subtracting END_UNCHANGED we get the index of the last
17560 unchanged character, and we have to add BEG to get its buffer
17561 position. */
17562 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17563 last_unchanged_pos_old = last_unchanged_pos - *delta;
17564
17565 /* Search backward from ROW for a row displaying a line that
17566 starts at a minimum position >= last_unchanged_pos_old. */
17567 for (; row > first_text_row; --row)
17568 {
17569 /* This used to abort, but it can happen.
17570 It is ok to just stop the search instead here. KFS. */
17571 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17572 break;
17573
17574 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17575 row_found = row;
17576 }
17577 }
17578
17579 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17580
17581 return row_found;
17582 }
17583
17584
17585 /* Make sure that glyph rows in the current matrix of window W
17586 reference the same glyph memory as corresponding rows in the
17587 frame's frame matrix. This function is called after scrolling W's
17588 current matrix on a terminal frame in try_window_id and
17589 try_window_reusing_current_matrix. */
17590
17591 static void
17592 sync_frame_with_window_matrix_rows (struct window *w)
17593 {
17594 struct frame *f = XFRAME (w->frame);
17595 struct glyph_row *window_row, *window_row_end, *frame_row;
17596
17597 /* Preconditions: W must be a leaf window and full-width. Its frame
17598 must have a frame matrix. */
17599 eassert (BUFFERP (w->contents));
17600 eassert (WINDOW_FULL_WIDTH_P (w));
17601 eassert (!FRAME_WINDOW_P (f));
17602
17603 /* If W is a full-width window, glyph pointers in W's current matrix
17604 have, by definition, to be the same as glyph pointers in the
17605 corresponding frame matrix. Note that frame matrices have no
17606 marginal areas (see build_frame_matrix). */
17607 window_row = w->current_matrix->rows;
17608 window_row_end = window_row + w->current_matrix->nrows;
17609 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17610 while (window_row < window_row_end)
17611 {
17612 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17613 struct glyph *end = window_row->glyphs[LAST_AREA];
17614
17615 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17616 frame_row->glyphs[TEXT_AREA] = start;
17617 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17618 frame_row->glyphs[LAST_AREA] = end;
17619
17620 /* Disable frame rows whose corresponding window rows have
17621 been disabled in try_window_id. */
17622 if (!window_row->enabled_p)
17623 frame_row->enabled_p = false;
17624
17625 ++window_row, ++frame_row;
17626 }
17627 }
17628
17629
17630 /* Find the glyph row in window W containing CHARPOS. Consider all
17631 rows between START and END (not inclusive). END null means search
17632 all rows to the end of the display area of W. Value is the row
17633 containing CHARPOS or null. */
17634
17635 struct glyph_row *
17636 row_containing_pos (struct window *w, ptrdiff_t charpos,
17637 struct glyph_row *start, struct glyph_row *end, int dy)
17638 {
17639 struct glyph_row *row = start;
17640 struct glyph_row *best_row = NULL;
17641 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17642 int last_y;
17643
17644 /* If we happen to start on a header-line, skip that. */
17645 if (row->mode_line_p)
17646 ++row;
17647
17648 if ((end && row >= end) || !row->enabled_p)
17649 return NULL;
17650
17651 last_y = window_text_bottom_y (w) - dy;
17652
17653 while (1)
17654 {
17655 /* Give up if we have gone too far. */
17656 if (end && row >= end)
17657 return NULL;
17658 /* This formerly returned if they were equal.
17659 I think that both quantities are of a "last plus one" type;
17660 if so, when they are equal, the row is within the screen. -- rms. */
17661 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17662 return NULL;
17663
17664 /* If it is in this row, return this row. */
17665 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17666 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17667 /* The end position of a row equals the start
17668 position of the next row. If CHARPOS is there, we
17669 would rather consider it displayed in the next
17670 line, except when this line ends in ZV. */
17671 && !row_for_charpos_p (row, charpos)))
17672 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17673 {
17674 struct glyph *g;
17675
17676 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17677 || (!best_row && !row->continued_p))
17678 return row;
17679 /* In bidi-reordered rows, there could be several rows whose
17680 edges surround CHARPOS, all of these rows belonging to
17681 the same continued line. We need to find the row which
17682 fits CHARPOS the best. */
17683 for (g = row->glyphs[TEXT_AREA];
17684 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17685 g++)
17686 {
17687 if (!STRINGP (g->object))
17688 {
17689 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17690 {
17691 mindif = eabs (g->charpos - charpos);
17692 best_row = row;
17693 /* Exact match always wins. */
17694 if (mindif == 0)
17695 return best_row;
17696 }
17697 }
17698 }
17699 }
17700 else if (best_row && !row->continued_p)
17701 return best_row;
17702 ++row;
17703 }
17704 }
17705
17706
17707 /* Try to redisplay window W by reusing its existing display. W's
17708 current matrix must be up to date when this function is called,
17709 i.e. window_end_valid must be nonzero.
17710
17711 Value is
17712
17713 >= 1 if successful, i.e. display has been updated
17714 specifically:
17715 1 means the changes were in front of a newline that precedes
17716 the window start, and the whole current matrix was reused
17717 2 means the changes were after the last position displayed
17718 in the window, and the whole current matrix was reused
17719 3 means portions of the current matrix were reused, while
17720 some of the screen lines were redrawn
17721 -1 if redisplay with same window start is known not to succeed
17722 0 if otherwise unsuccessful
17723
17724 The following steps are performed:
17725
17726 1. Find the last row in the current matrix of W that is not
17727 affected by changes at the start of current_buffer. If no such row
17728 is found, give up.
17729
17730 2. Find the first row in W's current matrix that is not affected by
17731 changes at the end of current_buffer. Maybe there is no such row.
17732
17733 3. Display lines beginning with the row + 1 found in step 1 to the
17734 row found in step 2 or, if step 2 didn't find a row, to the end of
17735 the window.
17736
17737 4. If cursor is not known to appear on the window, give up.
17738
17739 5. If display stopped at the row found in step 2, scroll the
17740 display and current matrix as needed.
17741
17742 6. Maybe display some lines at the end of W, if we must. This can
17743 happen under various circumstances, like a partially visible line
17744 becoming fully visible, or because newly displayed lines are displayed
17745 in smaller font sizes.
17746
17747 7. Update W's window end information. */
17748
17749 static int
17750 try_window_id (struct window *w)
17751 {
17752 struct frame *f = XFRAME (w->frame);
17753 struct glyph_matrix *current_matrix = w->current_matrix;
17754 struct glyph_matrix *desired_matrix = w->desired_matrix;
17755 struct glyph_row *last_unchanged_at_beg_row;
17756 struct glyph_row *first_unchanged_at_end_row;
17757 struct glyph_row *row;
17758 struct glyph_row *bottom_row;
17759 int bottom_vpos;
17760 struct it it;
17761 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17762 int dvpos, dy;
17763 struct text_pos start_pos;
17764 struct run run;
17765 int first_unchanged_at_end_vpos = 0;
17766 struct glyph_row *last_text_row, *last_text_row_at_end;
17767 struct text_pos start;
17768 ptrdiff_t first_changed_charpos, last_changed_charpos;
17769
17770 #ifdef GLYPH_DEBUG
17771 if (inhibit_try_window_id)
17772 return 0;
17773 #endif
17774
17775 /* This is handy for debugging. */
17776 #if 0
17777 #define GIVE_UP(X) \
17778 do { \
17779 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17780 return 0; \
17781 } while (0)
17782 #else
17783 #define GIVE_UP(X) return 0
17784 #endif
17785
17786 SET_TEXT_POS_FROM_MARKER (start, w->start);
17787
17788 /* Don't use this for mini-windows because these can show
17789 messages and mini-buffers, and we don't handle that here. */
17790 if (MINI_WINDOW_P (w))
17791 GIVE_UP (1);
17792
17793 /* This flag is used to prevent redisplay optimizations. */
17794 if (windows_or_buffers_changed || f->cursor_type_changed)
17795 GIVE_UP (2);
17796
17797 /* This function's optimizations cannot be used if overlays have
17798 changed in the buffer displayed by the window, so give up if they
17799 have. */
17800 if (w->last_overlay_modified != OVERLAY_MODIFF)
17801 GIVE_UP (21);
17802
17803 /* Verify that narrowing has not changed.
17804 Also verify that we were not told to prevent redisplay optimizations.
17805 It would be nice to further
17806 reduce the number of cases where this prevents try_window_id. */
17807 if (current_buffer->clip_changed
17808 || current_buffer->prevent_redisplay_optimizations_p)
17809 GIVE_UP (3);
17810
17811 /* Window must either use window-based redisplay or be full width. */
17812 if (!FRAME_WINDOW_P (f)
17813 && (!FRAME_LINE_INS_DEL_OK (f)
17814 || !WINDOW_FULL_WIDTH_P (w)))
17815 GIVE_UP (4);
17816
17817 /* Give up if point is known NOT to appear in W. */
17818 if (PT < CHARPOS (start))
17819 GIVE_UP (5);
17820
17821 /* Another way to prevent redisplay optimizations. */
17822 if (w->last_modified == 0)
17823 GIVE_UP (6);
17824
17825 /* Verify that window is not hscrolled. */
17826 if (w->hscroll != 0)
17827 GIVE_UP (7);
17828
17829 /* Verify that display wasn't paused. */
17830 if (!w->window_end_valid)
17831 GIVE_UP (8);
17832
17833 /* Likewise if highlighting trailing whitespace. */
17834 if (!NILP (Vshow_trailing_whitespace))
17835 GIVE_UP (11);
17836
17837 /* Can't use this if overlay arrow position and/or string have
17838 changed. */
17839 if (overlay_arrows_changed_p ())
17840 GIVE_UP (12);
17841
17842 /* When word-wrap is on, adding a space to the first word of a
17843 wrapped line can change the wrap position, altering the line
17844 above it. It might be worthwhile to handle this more
17845 intelligently, but for now just redisplay from scratch. */
17846 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17847 GIVE_UP (21);
17848
17849 /* Under bidi reordering, adding or deleting a character in the
17850 beginning of a paragraph, before the first strong directional
17851 character, can change the base direction of the paragraph (unless
17852 the buffer specifies a fixed paragraph direction), which will
17853 require to redisplay the whole paragraph. It might be worthwhile
17854 to find the paragraph limits and widen the range of redisplayed
17855 lines to that, but for now just give up this optimization and
17856 redisplay from scratch. */
17857 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17858 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17859 GIVE_UP (22);
17860
17861 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17862 only if buffer has really changed. The reason is that the gap is
17863 initially at Z for freshly visited files. The code below would
17864 set end_unchanged to 0 in that case. */
17865 if (MODIFF > SAVE_MODIFF
17866 /* This seems to happen sometimes after saving a buffer. */
17867 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17868 {
17869 if (GPT - BEG < BEG_UNCHANGED)
17870 BEG_UNCHANGED = GPT - BEG;
17871 if (Z - GPT < END_UNCHANGED)
17872 END_UNCHANGED = Z - GPT;
17873 }
17874
17875 /* The position of the first and last character that has been changed. */
17876 first_changed_charpos = BEG + BEG_UNCHANGED;
17877 last_changed_charpos = Z - END_UNCHANGED;
17878
17879 /* If window starts after a line end, and the last change is in
17880 front of that newline, then changes don't affect the display.
17881 This case happens with stealth-fontification. Note that although
17882 the display is unchanged, glyph positions in the matrix have to
17883 be adjusted, of course. */
17884 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17885 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17886 && ((last_changed_charpos < CHARPOS (start)
17887 && CHARPOS (start) == BEGV)
17888 || (last_changed_charpos < CHARPOS (start) - 1
17889 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17890 {
17891 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17892 struct glyph_row *r0;
17893
17894 /* Compute how many chars/bytes have been added to or removed
17895 from the buffer. */
17896 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17897 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17898 Z_delta = Z - Z_old;
17899 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17900
17901 /* Give up if PT is not in the window. Note that it already has
17902 been checked at the start of try_window_id that PT is not in
17903 front of the window start. */
17904 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17905 GIVE_UP (13);
17906
17907 /* If window start is unchanged, we can reuse the whole matrix
17908 as is, after adjusting glyph positions. No need to compute
17909 the window end again, since its offset from Z hasn't changed. */
17910 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17911 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17912 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17913 /* PT must not be in a partially visible line. */
17914 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17915 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17916 {
17917 /* Adjust positions in the glyph matrix. */
17918 if (Z_delta || Z_delta_bytes)
17919 {
17920 struct glyph_row *r1
17921 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17922 increment_matrix_positions (w->current_matrix,
17923 MATRIX_ROW_VPOS (r0, current_matrix),
17924 MATRIX_ROW_VPOS (r1, current_matrix),
17925 Z_delta, Z_delta_bytes);
17926 }
17927
17928 /* Set the cursor. */
17929 row = row_containing_pos (w, PT, r0, NULL, 0);
17930 if (row)
17931 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17932 return 1;
17933 }
17934 }
17935
17936 /* Handle the case that changes are all below what is displayed in
17937 the window, and that PT is in the window. This shortcut cannot
17938 be taken if ZV is visible in the window, and text has been added
17939 there that is visible in the window. */
17940 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17941 /* ZV is not visible in the window, or there are no
17942 changes at ZV, actually. */
17943 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17944 || first_changed_charpos == last_changed_charpos))
17945 {
17946 struct glyph_row *r0;
17947
17948 /* Give up if PT is not in the window. Note that it already has
17949 been checked at the start of try_window_id that PT is not in
17950 front of the window start. */
17951 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17952 GIVE_UP (14);
17953
17954 /* If window start is unchanged, we can reuse the whole matrix
17955 as is, without changing glyph positions since no text has
17956 been added/removed in front of the window end. */
17957 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17958 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17959 /* PT must not be in a partially visible line. */
17960 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17961 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17962 {
17963 /* We have to compute the window end anew since text
17964 could have been added/removed after it. */
17965 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17966 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17967
17968 /* Set the cursor. */
17969 row = row_containing_pos (w, PT, r0, NULL, 0);
17970 if (row)
17971 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17972 return 2;
17973 }
17974 }
17975
17976 /* Give up if window start is in the changed area.
17977
17978 The condition used to read
17979
17980 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17981
17982 but why that was tested escapes me at the moment. */
17983 if (CHARPOS (start) >= first_changed_charpos
17984 && CHARPOS (start) <= last_changed_charpos)
17985 GIVE_UP (15);
17986
17987 /* Check that window start agrees with the start of the first glyph
17988 row in its current matrix. Check this after we know the window
17989 start is not in changed text, otherwise positions would not be
17990 comparable. */
17991 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17992 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17993 GIVE_UP (16);
17994
17995 /* Give up if the window ends in strings. Overlay strings
17996 at the end are difficult to handle, so don't try. */
17997 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17998 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17999 GIVE_UP (20);
18000
18001 /* Compute the position at which we have to start displaying new
18002 lines. Some of the lines at the top of the window might be
18003 reusable because they are not displaying changed text. Find the
18004 last row in W's current matrix not affected by changes at the
18005 start of current_buffer. Value is null if changes start in the
18006 first line of window. */
18007 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18008 if (last_unchanged_at_beg_row)
18009 {
18010 /* Avoid starting to display in the middle of a character, a TAB
18011 for instance. This is easier than to set up the iterator
18012 exactly, and it's not a frequent case, so the additional
18013 effort wouldn't really pay off. */
18014 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18015 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18016 && last_unchanged_at_beg_row > w->current_matrix->rows)
18017 --last_unchanged_at_beg_row;
18018
18019 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18020 GIVE_UP (17);
18021
18022 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18023 GIVE_UP (18);
18024 start_pos = it.current.pos;
18025
18026 /* Start displaying new lines in the desired matrix at the same
18027 vpos we would use in the current matrix, i.e. below
18028 last_unchanged_at_beg_row. */
18029 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18030 current_matrix);
18031 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18032 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18033
18034 eassert (it.hpos == 0 && it.current_x == 0);
18035 }
18036 else
18037 {
18038 /* There are no reusable lines at the start of the window.
18039 Start displaying in the first text line. */
18040 start_display (&it, w, start);
18041 it.vpos = it.first_vpos;
18042 start_pos = it.current.pos;
18043 }
18044
18045 /* Find the first row that is not affected by changes at the end of
18046 the buffer. Value will be null if there is no unchanged row, in
18047 which case we must redisplay to the end of the window. delta
18048 will be set to the value by which buffer positions beginning with
18049 first_unchanged_at_end_row have to be adjusted due to text
18050 changes. */
18051 first_unchanged_at_end_row
18052 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18053 IF_DEBUG (debug_delta = delta);
18054 IF_DEBUG (debug_delta_bytes = delta_bytes);
18055
18056 /* Set stop_pos to the buffer position up to which we will have to
18057 display new lines. If first_unchanged_at_end_row != NULL, this
18058 is the buffer position of the start of the line displayed in that
18059 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18060 that we don't stop at a buffer position. */
18061 stop_pos = 0;
18062 if (first_unchanged_at_end_row)
18063 {
18064 eassert (last_unchanged_at_beg_row == NULL
18065 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18066
18067 /* If this is a continuation line, move forward to the next one
18068 that isn't. Changes in lines above affect this line.
18069 Caution: this may move first_unchanged_at_end_row to a row
18070 not displaying text. */
18071 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18072 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18073 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18074 < it.last_visible_y))
18075 ++first_unchanged_at_end_row;
18076
18077 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18078 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18079 >= it.last_visible_y))
18080 first_unchanged_at_end_row = NULL;
18081 else
18082 {
18083 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18084 + delta);
18085 first_unchanged_at_end_vpos
18086 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18087 eassert (stop_pos >= Z - END_UNCHANGED);
18088 }
18089 }
18090 else if (last_unchanged_at_beg_row == NULL)
18091 GIVE_UP (19);
18092
18093
18094 #ifdef GLYPH_DEBUG
18095
18096 /* Either there is no unchanged row at the end, or the one we have
18097 now displays text. This is a necessary condition for the window
18098 end pos calculation at the end of this function. */
18099 eassert (first_unchanged_at_end_row == NULL
18100 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18101
18102 debug_last_unchanged_at_beg_vpos
18103 = (last_unchanged_at_beg_row
18104 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18105 : -1);
18106 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18107
18108 #endif /* GLYPH_DEBUG */
18109
18110
18111 /* Display new lines. Set last_text_row to the last new line
18112 displayed which has text on it, i.e. might end up as being the
18113 line where the window_end_vpos is. */
18114 w->cursor.vpos = -1;
18115 last_text_row = NULL;
18116 overlay_arrow_seen = 0;
18117 while (it.current_y < it.last_visible_y
18118 && !f->fonts_changed
18119 && (first_unchanged_at_end_row == NULL
18120 || IT_CHARPOS (it) < stop_pos))
18121 {
18122 if (display_line (&it))
18123 last_text_row = it.glyph_row - 1;
18124 }
18125
18126 if (f->fonts_changed)
18127 return -1;
18128
18129
18130 /* Compute differences in buffer positions, y-positions etc. for
18131 lines reused at the bottom of the window. Compute what we can
18132 scroll. */
18133 if (first_unchanged_at_end_row
18134 /* No lines reused because we displayed everything up to the
18135 bottom of the window. */
18136 && it.current_y < it.last_visible_y)
18137 {
18138 dvpos = (it.vpos
18139 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18140 current_matrix));
18141 dy = it.current_y - first_unchanged_at_end_row->y;
18142 run.current_y = first_unchanged_at_end_row->y;
18143 run.desired_y = run.current_y + dy;
18144 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18145 }
18146 else
18147 {
18148 delta = delta_bytes = dvpos = dy
18149 = run.current_y = run.desired_y = run.height = 0;
18150 first_unchanged_at_end_row = NULL;
18151 }
18152 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18153
18154
18155 /* Find the cursor if not already found. We have to decide whether
18156 PT will appear on this window (it sometimes doesn't, but this is
18157 not a very frequent case.) This decision has to be made before
18158 the current matrix is altered. A value of cursor.vpos < 0 means
18159 that PT is either in one of the lines beginning at
18160 first_unchanged_at_end_row or below the window. Don't care for
18161 lines that might be displayed later at the window end; as
18162 mentioned, this is not a frequent case. */
18163 if (w->cursor.vpos < 0)
18164 {
18165 /* Cursor in unchanged rows at the top? */
18166 if (PT < CHARPOS (start_pos)
18167 && last_unchanged_at_beg_row)
18168 {
18169 row = row_containing_pos (w, PT,
18170 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18171 last_unchanged_at_beg_row + 1, 0);
18172 if (row)
18173 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18174 }
18175
18176 /* Start from first_unchanged_at_end_row looking for PT. */
18177 else if (first_unchanged_at_end_row)
18178 {
18179 row = row_containing_pos (w, PT - delta,
18180 first_unchanged_at_end_row, NULL, 0);
18181 if (row)
18182 set_cursor_from_row (w, row, w->current_matrix, delta,
18183 delta_bytes, dy, dvpos);
18184 }
18185
18186 /* Give up if cursor was not found. */
18187 if (w->cursor.vpos < 0)
18188 {
18189 clear_glyph_matrix (w->desired_matrix);
18190 return -1;
18191 }
18192 }
18193
18194 /* Don't let the cursor end in the scroll margins. */
18195 {
18196 int this_scroll_margin, cursor_height;
18197 int frame_line_height = default_line_pixel_height (w);
18198 int window_total_lines
18199 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18200
18201 this_scroll_margin =
18202 max (0, min (scroll_margin, window_total_lines / 4));
18203 this_scroll_margin *= frame_line_height;
18204 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18205
18206 if ((w->cursor.y < this_scroll_margin
18207 && CHARPOS (start) > BEGV)
18208 /* Old redisplay didn't take scroll margin into account at the bottom,
18209 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18210 || (w->cursor.y + (make_cursor_line_fully_visible_p
18211 ? cursor_height + this_scroll_margin
18212 : 1)) > it.last_visible_y)
18213 {
18214 w->cursor.vpos = -1;
18215 clear_glyph_matrix (w->desired_matrix);
18216 return -1;
18217 }
18218 }
18219
18220 /* Scroll the display. Do it before changing the current matrix so
18221 that xterm.c doesn't get confused about where the cursor glyph is
18222 found. */
18223 if (dy && run.height)
18224 {
18225 update_begin (f);
18226
18227 if (FRAME_WINDOW_P (f))
18228 {
18229 FRAME_RIF (f)->update_window_begin_hook (w);
18230 FRAME_RIF (f)->clear_window_mouse_face (w);
18231 FRAME_RIF (f)->scroll_run_hook (w, &run);
18232 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18233 }
18234 else
18235 {
18236 /* Terminal frame. In this case, dvpos gives the number of
18237 lines to scroll by; dvpos < 0 means scroll up. */
18238 int from_vpos
18239 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18240 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18241 int end = (WINDOW_TOP_EDGE_LINE (w)
18242 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18243 + window_internal_height (w));
18244
18245 #if defined (HAVE_GPM) || defined (MSDOS)
18246 x_clear_window_mouse_face (w);
18247 #endif
18248 /* Perform the operation on the screen. */
18249 if (dvpos > 0)
18250 {
18251 /* Scroll last_unchanged_at_beg_row to the end of the
18252 window down dvpos lines. */
18253 set_terminal_window (f, end);
18254
18255 /* On dumb terminals delete dvpos lines at the end
18256 before inserting dvpos empty lines. */
18257 if (!FRAME_SCROLL_REGION_OK (f))
18258 ins_del_lines (f, end - dvpos, -dvpos);
18259
18260 /* Insert dvpos empty lines in front of
18261 last_unchanged_at_beg_row. */
18262 ins_del_lines (f, from, dvpos);
18263 }
18264 else if (dvpos < 0)
18265 {
18266 /* Scroll up last_unchanged_at_beg_vpos to the end of
18267 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18268 set_terminal_window (f, end);
18269
18270 /* Delete dvpos lines in front of
18271 last_unchanged_at_beg_vpos. ins_del_lines will set
18272 the cursor to the given vpos and emit |dvpos| delete
18273 line sequences. */
18274 ins_del_lines (f, from + dvpos, dvpos);
18275
18276 /* On a dumb terminal insert dvpos empty lines at the
18277 end. */
18278 if (!FRAME_SCROLL_REGION_OK (f))
18279 ins_del_lines (f, end + dvpos, -dvpos);
18280 }
18281
18282 set_terminal_window (f, 0);
18283 }
18284
18285 update_end (f);
18286 }
18287
18288 /* Shift reused rows of the current matrix to the right position.
18289 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18290 text. */
18291 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18292 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18293 if (dvpos < 0)
18294 {
18295 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18296 bottom_vpos, dvpos);
18297 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18298 bottom_vpos);
18299 }
18300 else if (dvpos > 0)
18301 {
18302 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18303 bottom_vpos, dvpos);
18304 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18305 first_unchanged_at_end_vpos + dvpos);
18306 }
18307
18308 /* For frame-based redisplay, make sure that current frame and window
18309 matrix are in sync with respect to glyph memory. */
18310 if (!FRAME_WINDOW_P (f))
18311 sync_frame_with_window_matrix_rows (w);
18312
18313 /* Adjust buffer positions in reused rows. */
18314 if (delta || delta_bytes)
18315 increment_matrix_positions (current_matrix,
18316 first_unchanged_at_end_vpos + dvpos,
18317 bottom_vpos, delta, delta_bytes);
18318
18319 /* Adjust Y positions. */
18320 if (dy)
18321 shift_glyph_matrix (w, current_matrix,
18322 first_unchanged_at_end_vpos + dvpos,
18323 bottom_vpos, dy);
18324
18325 if (first_unchanged_at_end_row)
18326 {
18327 first_unchanged_at_end_row += dvpos;
18328 if (first_unchanged_at_end_row->y >= it.last_visible_y
18329 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18330 first_unchanged_at_end_row = NULL;
18331 }
18332
18333 /* If scrolling up, there may be some lines to display at the end of
18334 the window. */
18335 last_text_row_at_end = NULL;
18336 if (dy < 0)
18337 {
18338 /* Scrolling up can leave for example a partially visible line
18339 at the end of the window to be redisplayed. */
18340 /* Set last_row to the glyph row in the current matrix where the
18341 window end line is found. It has been moved up or down in
18342 the matrix by dvpos. */
18343 int last_vpos = w->window_end_vpos + dvpos;
18344 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18345
18346 /* If last_row is the window end line, it should display text. */
18347 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18348
18349 /* If window end line was partially visible before, begin
18350 displaying at that line. Otherwise begin displaying with the
18351 line following it. */
18352 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18353 {
18354 init_to_row_start (&it, w, last_row);
18355 it.vpos = last_vpos;
18356 it.current_y = last_row->y;
18357 }
18358 else
18359 {
18360 init_to_row_end (&it, w, last_row);
18361 it.vpos = 1 + last_vpos;
18362 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18363 ++last_row;
18364 }
18365
18366 /* We may start in a continuation line. If so, we have to
18367 get the right continuation_lines_width and current_x. */
18368 it.continuation_lines_width = last_row->continuation_lines_width;
18369 it.hpos = it.current_x = 0;
18370
18371 /* Display the rest of the lines at the window end. */
18372 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18373 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18374 {
18375 /* Is it always sure that the display agrees with lines in
18376 the current matrix? I don't think so, so we mark rows
18377 displayed invalid in the current matrix by setting their
18378 enabled_p flag to zero. */
18379 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18380 if (display_line (&it))
18381 last_text_row_at_end = it.glyph_row - 1;
18382 }
18383 }
18384
18385 /* Update window_end_pos and window_end_vpos. */
18386 if (first_unchanged_at_end_row && !last_text_row_at_end)
18387 {
18388 /* Window end line if one of the preserved rows from the current
18389 matrix. Set row to the last row displaying text in current
18390 matrix starting at first_unchanged_at_end_row, after
18391 scrolling. */
18392 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18393 row = find_last_row_displaying_text (w->current_matrix, &it,
18394 first_unchanged_at_end_row);
18395 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18396 adjust_window_ends (w, row, 1);
18397 eassert (w->window_end_bytepos >= 0);
18398 IF_DEBUG (debug_method_add (w, "A"));
18399 }
18400 else if (last_text_row_at_end)
18401 {
18402 adjust_window_ends (w, last_text_row_at_end, 0);
18403 eassert (w->window_end_bytepos >= 0);
18404 IF_DEBUG (debug_method_add (w, "B"));
18405 }
18406 else if (last_text_row)
18407 {
18408 /* We have displayed either to the end of the window or at the
18409 end of the window, i.e. the last row with text is to be found
18410 in the desired matrix. */
18411 adjust_window_ends (w, last_text_row, 0);
18412 eassert (w->window_end_bytepos >= 0);
18413 }
18414 else if (first_unchanged_at_end_row == NULL
18415 && last_text_row == NULL
18416 && last_text_row_at_end == NULL)
18417 {
18418 /* Displayed to end of window, but no line containing text was
18419 displayed. Lines were deleted at the end of the window. */
18420 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18421 int vpos = w->window_end_vpos;
18422 struct glyph_row *current_row = current_matrix->rows + vpos;
18423 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18424
18425 for (row = NULL;
18426 row == NULL && vpos >= first_vpos;
18427 --vpos, --current_row, --desired_row)
18428 {
18429 if (desired_row->enabled_p)
18430 {
18431 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18432 row = desired_row;
18433 }
18434 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18435 row = current_row;
18436 }
18437
18438 eassert (row != NULL);
18439 w->window_end_vpos = vpos + 1;
18440 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18441 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18442 eassert (w->window_end_bytepos >= 0);
18443 IF_DEBUG (debug_method_add (w, "C"));
18444 }
18445 else
18446 emacs_abort ();
18447
18448 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18449 debug_end_vpos = w->window_end_vpos));
18450
18451 /* Record that display has not been completed. */
18452 w->window_end_valid = 0;
18453 w->desired_matrix->no_scrolling_p = 1;
18454 return 3;
18455
18456 #undef GIVE_UP
18457 }
18458
18459
18460 \f
18461 /***********************************************************************
18462 More debugging support
18463 ***********************************************************************/
18464
18465 #ifdef GLYPH_DEBUG
18466
18467 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18468 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18469 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18470
18471
18472 /* Dump the contents of glyph matrix MATRIX on stderr.
18473
18474 GLYPHS 0 means don't show glyph contents.
18475 GLYPHS 1 means show glyphs in short form
18476 GLYPHS > 1 means show glyphs in long form. */
18477
18478 void
18479 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18480 {
18481 int i;
18482 for (i = 0; i < matrix->nrows; ++i)
18483 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18484 }
18485
18486
18487 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18488 the glyph row and area where the glyph comes from. */
18489
18490 void
18491 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18492 {
18493 if (glyph->type == CHAR_GLYPH
18494 || glyph->type == GLYPHLESS_GLYPH)
18495 {
18496 fprintf (stderr,
18497 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18498 glyph - row->glyphs[TEXT_AREA],
18499 (glyph->type == CHAR_GLYPH
18500 ? 'C'
18501 : 'G'),
18502 glyph->charpos,
18503 (BUFFERP (glyph->object)
18504 ? 'B'
18505 : (STRINGP (glyph->object)
18506 ? 'S'
18507 : (INTEGERP (glyph->object)
18508 ? '0'
18509 : '-'))),
18510 glyph->pixel_width,
18511 glyph->u.ch,
18512 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18513 ? glyph->u.ch
18514 : '.'),
18515 glyph->face_id,
18516 glyph->left_box_line_p,
18517 glyph->right_box_line_p);
18518 }
18519 else if (glyph->type == STRETCH_GLYPH)
18520 {
18521 fprintf (stderr,
18522 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18523 glyph - row->glyphs[TEXT_AREA],
18524 'S',
18525 glyph->charpos,
18526 (BUFFERP (glyph->object)
18527 ? 'B'
18528 : (STRINGP (glyph->object)
18529 ? 'S'
18530 : (INTEGERP (glyph->object)
18531 ? '0'
18532 : '-'))),
18533 glyph->pixel_width,
18534 0,
18535 ' ',
18536 glyph->face_id,
18537 glyph->left_box_line_p,
18538 glyph->right_box_line_p);
18539 }
18540 else if (glyph->type == IMAGE_GLYPH)
18541 {
18542 fprintf (stderr,
18543 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18544 glyph - row->glyphs[TEXT_AREA],
18545 'I',
18546 glyph->charpos,
18547 (BUFFERP (glyph->object)
18548 ? 'B'
18549 : (STRINGP (glyph->object)
18550 ? 'S'
18551 : (INTEGERP (glyph->object)
18552 ? '0'
18553 : '-'))),
18554 glyph->pixel_width,
18555 glyph->u.img_id,
18556 '.',
18557 glyph->face_id,
18558 glyph->left_box_line_p,
18559 glyph->right_box_line_p);
18560 }
18561 else if (glyph->type == COMPOSITE_GLYPH)
18562 {
18563 fprintf (stderr,
18564 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18565 glyph - row->glyphs[TEXT_AREA],
18566 '+',
18567 glyph->charpos,
18568 (BUFFERP (glyph->object)
18569 ? 'B'
18570 : (STRINGP (glyph->object)
18571 ? 'S'
18572 : (INTEGERP (glyph->object)
18573 ? '0'
18574 : '-'))),
18575 glyph->pixel_width,
18576 glyph->u.cmp.id);
18577 if (glyph->u.cmp.automatic)
18578 fprintf (stderr,
18579 "[%d-%d]",
18580 glyph->slice.cmp.from, glyph->slice.cmp.to);
18581 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18582 glyph->face_id,
18583 glyph->left_box_line_p,
18584 glyph->right_box_line_p);
18585 }
18586 }
18587
18588
18589 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18590 GLYPHS 0 means don't show glyph contents.
18591 GLYPHS 1 means show glyphs in short form
18592 GLYPHS > 1 means show glyphs in long form. */
18593
18594 void
18595 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18596 {
18597 if (glyphs != 1)
18598 {
18599 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18600 fprintf (stderr, "==============================================================================\n");
18601
18602 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18603 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18604 vpos,
18605 MATRIX_ROW_START_CHARPOS (row),
18606 MATRIX_ROW_END_CHARPOS (row),
18607 row->used[TEXT_AREA],
18608 row->contains_overlapping_glyphs_p,
18609 row->enabled_p,
18610 row->truncated_on_left_p,
18611 row->truncated_on_right_p,
18612 row->continued_p,
18613 MATRIX_ROW_CONTINUATION_LINE_P (row),
18614 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18615 row->ends_at_zv_p,
18616 row->fill_line_p,
18617 row->ends_in_middle_of_char_p,
18618 row->starts_in_middle_of_char_p,
18619 row->mouse_face_p,
18620 row->x,
18621 row->y,
18622 row->pixel_width,
18623 row->height,
18624 row->visible_height,
18625 row->ascent,
18626 row->phys_ascent);
18627 /* The next 3 lines should align to "Start" in the header. */
18628 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18629 row->end.overlay_string_index,
18630 row->continuation_lines_width);
18631 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18632 CHARPOS (row->start.string_pos),
18633 CHARPOS (row->end.string_pos));
18634 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18635 row->end.dpvec_index);
18636 }
18637
18638 if (glyphs > 1)
18639 {
18640 int area;
18641
18642 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18643 {
18644 struct glyph *glyph = row->glyphs[area];
18645 struct glyph *glyph_end = glyph + row->used[area];
18646
18647 /* Glyph for a line end in text. */
18648 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18649 ++glyph_end;
18650
18651 if (glyph < glyph_end)
18652 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18653
18654 for (; glyph < glyph_end; ++glyph)
18655 dump_glyph (row, glyph, area);
18656 }
18657 }
18658 else if (glyphs == 1)
18659 {
18660 int area;
18661
18662 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18663 {
18664 char *s = alloca (row->used[area] + 4);
18665 int i;
18666
18667 for (i = 0; i < row->used[area]; ++i)
18668 {
18669 struct glyph *glyph = row->glyphs[area] + i;
18670 if (i == row->used[area] - 1
18671 && area == TEXT_AREA
18672 && INTEGERP (glyph->object)
18673 && glyph->type == CHAR_GLYPH
18674 && glyph->u.ch == ' ')
18675 {
18676 strcpy (&s[i], "[\\n]");
18677 i += 4;
18678 }
18679 else if (glyph->type == CHAR_GLYPH
18680 && glyph->u.ch < 0x80
18681 && glyph->u.ch >= ' ')
18682 s[i] = glyph->u.ch;
18683 else
18684 s[i] = '.';
18685 }
18686
18687 s[i] = '\0';
18688 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18689 }
18690 }
18691 }
18692
18693
18694 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18695 Sdump_glyph_matrix, 0, 1, "p",
18696 doc: /* Dump the current matrix of the selected window to stderr.
18697 Shows contents of glyph row structures. With non-nil
18698 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18699 glyphs in short form, otherwise show glyphs in long form. */)
18700 (Lisp_Object glyphs)
18701 {
18702 struct window *w = XWINDOW (selected_window);
18703 struct buffer *buffer = XBUFFER (w->contents);
18704
18705 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18706 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18707 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18708 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18709 fprintf (stderr, "=============================================\n");
18710 dump_glyph_matrix (w->current_matrix,
18711 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18712 return Qnil;
18713 }
18714
18715
18716 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18717 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18718 (void)
18719 {
18720 struct frame *f = XFRAME (selected_frame);
18721 dump_glyph_matrix (f->current_matrix, 1);
18722 return Qnil;
18723 }
18724
18725
18726 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18727 doc: /* Dump glyph row ROW to stderr.
18728 GLYPH 0 means don't dump glyphs.
18729 GLYPH 1 means dump glyphs in short form.
18730 GLYPH > 1 or omitted means dump glyphs in long form. */)
18731 (Lisp_Object row, Lisp_Object glyphs)
18732 {
18733 struct glyph_matrix *matrix;
18734 EMACS_INT vpos;
18735
18736 CHECK_NUMBER (row);
18737 matrix = XWINDOW (selected_window)->current_matrix;
18738 vpos = XINT (row);
18739 if (vpos >= 0 && vpos < matrix->nrows)
18740 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18741 vpos,
18742 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18743 return Qnil;
18744 }
18745
18746
18747 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18748 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18749 GLYPH 0 means don't dump glyphs.
18750 GLYPH 1 means dump glyphs in short form.
18751 GLYPH > 1 or omitted means dump glyphs in long form.
18752
18753 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18754 do nothing. */)
18755 (Lisp_Object row, Lisp_Object glyphs)
18756 {
18757 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18758 struct frame *sf = SELECTED_FRAME ();
18759 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18760 EMACS_INT vpos;
18761
18762 CHECK_NUMBER (row);
18763 vpos = XINT (row);
18764 if (vpos >= 0 && vpos < m->nrows)
18765 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18766 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18767 #endif
18768 return Qnil;
18769 }
18770
18771
18772 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18773 doc: /* Toggle tracing of redisplay.
18774 With ARG, turn tracing on if and only if ARG is positive. */)
18775 (Lisp_Object arg)
18776 {
18777 if (NILP (arg))
18778 trace_redisplay_p = !trace_redisplay_p;
18779 else
18780 {
18781 arg = Fprefix_numeric_value (arg);
18782 trace_redisplay_p = XINT (arg) > 0;
18783 }
18784
18785 return Qnil;
18786 }
18787
18788
18789 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18790 doc: /* Like `format', but print result to stderr.
18791 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18792 (ptrdiff_t nargs, Lisp_Object *args)
18793 {
18794 Lisp_Object s = Fformat (nargs, args);
18795 fprintf (stderr, "%s", SDATA (s));
18796 return Qnil;
18797 }
18798
18799 #endif /* GLYPH_DEBUG */
18800
18801
18802 \f
18803 /***********************************************************************
18804 Building Desired Matrix Rows
18805 ***********************************************************************/
18806
18807 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18808 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18809
18810 static struct glyph_row *
18811 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18812 {
18813 struct frame *f = XFRAME (WINDOW_FRAME (w));
18814 struct buffer *buffer = XBUFFER (w->contents);
18815 struct buffer *old = current_buffer;
18816 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18817 int arrow_len = SCHARS (overlay_arrow_string);
18818 const unsigned char *arrow_end = arrow_string + arrow_len;
18819 const unsigned char *p;
18820 struct it it;
18821 bool multibyte_p;
18822 int n_glyphs_before;
18823
18824 set_buffer_temp (buffer);
18825 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18826 it.glyph_row->used[TEXT_AREA] = 0;
18827 SET_TEXT_POS (it.position, 0, 0);
18828
18829 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18830 p = arrow_string;
18831 while (p < arrow_end)
18832 {
18833 Lisp_Object face, ilisp;
18834
18835 /* Get the next character. */
18836 if (multibyte_p)
18837 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18838 else
18839 {
18840 it.c = it.char_to_display = *p, it.len = 1;
18841 if (! ASCII_CHAR_P (it.c))
18842 it.char_to_display = BYTE8_TO_CHAR (it.c);
18843 }
18844 p += it.len;
18845
18846 /* Get its face. */
18847 ilisp = make_number (p - arrow_string);
18848 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18849 it.face_id = compute_char_face (f, it.char_to_display, face);
18850
18851 /* Compute its width, get its glyphs. */
18852 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18853 SET_TEXT_POS (it.position, -1, -1);
18854 PRODUCE_GLYPHS (&it);
18855
18856 /* If this character doesn't fit any more in the line, we have
18857 to remove some glyphs. */
18858 if (it.current_x > it.last_visible_x)
18859 {
18860 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18861 break;
18862 }
18863 }
18864
18865 set_buffer_temp (old);
18866 return it.glyph_row;
18867 }
18868
18869
18870 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18871 glyphs to insert is determined by produce_special_glyphs. */
18872
18873 static void
18874 insert_left_trunc_glyphs (struct it *it)
18875 {
18876 struct it truncate_it;
18877 struct glyph *from, *end, *to, *toend;
18878
18879 eassert (!FRAME_WINDOW_P (it->f)
18880 || (!it->glyph_row->reversed_p
18881 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18882 || (it->glyph_row->reversed_p
18883 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18884
18885 /* Get the truncation glyphs. */
18886 truncate_it = *it;
18887 truncate_it.current_x = 0;
18888 truncate_it.face_id = DEFAULT_FACE_ID;
18889 truncate_it.glyph_row = &scratch_glyph_row;
18890 truncate_it.area = TEXT_AREA;
18891 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18892 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18893 truncate_it.object = make_number (0);
18894 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18895
18896 /* Overwrite glyphs from IT with truncation glyphs. */
18897 if (!it->glyph_row->reversed_p)
18898 {
18899 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18900
18901 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18902 end = from + tused;
18903 to = it->glyph_row->glyphs[TEXT_AREA];
18904 toend = to + it->glyph_row->used[TEXT_AREA];
18905 if (FRAME_WINDOW_P (it->f))
18906 {
18907 /* On GUI frames, when variable-size fonts are displayed,
18908 the truncation glyphs may need more pixels than the row's
18909 glyphs they overwrite. We overwrite more glyphs to free
18910 enough screen real estate, and enlarge the stretch glyph
18911 on the right (see display_line), if there is one, to
18912 preserve the screen position of the truncation glyphs on
18913 the right. */
18914 int w = 0;
18915 struct glyph *g = to;
18916 short used;
18917
18918 /* The first glyph could be partially visible, in which case
18919 it->glyph_row->x will be negative. But we want the left
18920 truncation glyphs to be aligned at the left margin of the
18921 window, so we override the x coordinate at which the row
18922 will begin. */
18923 it->glyph_row->x = 0;
18924 while (g < toend && w < it->truncation_pixel_width)
18925 {
18926 w += g->pixel_width;
18927 ++g;
18928 }
18929 if (g - to - tused > 0)
18930 {
18931 memmove (to + tused, g, (toend - g) * sizeof(*g));
18932 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18933 }
18934 used = it->glyph_row->used[TEXT_AREA];
18935 if (it->glyph_row->truncated_on_right_p
18936 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18937 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18938 == STRETCH_GLYPH)
18939 {
18940 int extra = w - it->truncation_pixel_width;
18941
18942 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18943 }
18944 }
18945
18946 while (from < end)
18947 *to++ = *from++;
18948
18949 /* There may be padding glyphs left over. Overwrite them too. */
18950 if (!FRAME_WINDOW_P (it->f))
18951 {
18952 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18953 {
18954 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18955 while (from < end)
18956 *to++ = *from++;
18957 }
18958 }
18959
18960 if (to > toend)
18961 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18962 }
18963 else
18964 {
18965 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18966
18967 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18968 that back to front. */
18969 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18970 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18971 toend = it->glyph_row->glyphs[TEXT_AREA];
18972 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18973 if (FRAME_WINDOW_P (it->f))
18974 {
18975 int w = 0;
18976 struct glyph *g = to;
18977
18978 while (g >= toend && w < it->truncation_pixel_width)
18979 {
18980 w += g->pixel_width;
18981 --g;
18982 }
18983 if (to - g - tused > 0)
18984 to = g + tused;
18985 if (it->glyph_row->truncated_on_right_p
18986 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18987 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18988 {
18989 int extra = w - it->truncation_pixel_width;
18990
18991 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18992 }
18993 }
18994
18995 while (from >= end && to >= toend)
18996 *to-- = *from--;
18997 if (!FRAME_WINDOW_P (it->f))
18998 {
18999 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19000 {
19001 from =
19002 truncate_it.glyph_row->glyphs[TEXT_AREA]
19003 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19004 while (from >= end && to >= toend)
19005 *to-- = *from--;
19006 }
19007 }
19008 if (from >= end)
19009 {
19010 /* Need to free some room before prepending additional
19011 glyphs. */
19012 int move_by = from - end + 1;
19013 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19014 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19015
19016 for ( ; g >= g0; g--)
19017 g[move_by] = *g;
19018 while (from >= end)
19019 *to-- = *from--;
19020 it->glyph_row->used[TEXT_AREA] += move_by;
19021 }
19022 }
19023 }
19024
19025 /* Compute the hash code for ROW. */
19026 unsigned
19027 row_hash (struct glyph_row *row)
19028 {
19029 int area, k;
19030 unsigned hashval = 0;
19031
19032 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19033 for (k = 0; k < row->used[area]; ++k)
19034 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19035 + row->glyphs[area][k].u.val
19036 + row->glyphs[area][k].face_id
19037 + row->glyphs[area][k].padding_p
19038 + (row->glyphs[area][k].type << 2));
19039
19040 return hashval;
19041 }
19042
19043 /* Compute the pixel height and width of IT->glyph_row.
19044
19045 Most of the time, ascent and height of a display line will be equal
19046 to the max_ascent and max_height values of the display iterator
19047 structure. This is not the case if
19048
19049 1. We hit ZV without displaying anything. In this case, max_ascent
19050 and max_height will be zero.
19051
19052 2. We have some glyphs that don't contribute to the line height.
19053 (The glyph row flag contributes_to_line_height_p is for future
19054 pixmap extensions).
19055
19056 The first case is easily covered by using default values because in
19057 these cases, the line height does not really matter, except that it
19058 must not be zero. */
19059
19060 static void
19061 compute_line_metrics (struct it *it)
19062 {
19063 struct glyph_row *row = it->glyph_row;
19064
19065 if (FRAME_WINDOW_P (it->f))
19066 {
19067 int i, min_y, max_y;
19068
19069 /* The line may consist of one space only, that was added to
19070 place the cursor on it. If so, the row's height hasn't been
19071 computed yet. */
19072 if (row->height == 0)
19073 {
19074 if (it->max_ascent + it->max_descent == 0)
19075 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19076 row->ascent = it->max_ascent;
19077 row->height = it->max_ascent + it->max_descent;
19078 row->phys_ascent = it->max_phys_ascent;
19079 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19080 row->extra_line_spacing = it->max_extra_line_spacing;
19081 }
19082
19083 /* Compute the width of this line. */
19084 row->pixel_width = row->x;
19085 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19086 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19087
19088 eassert (row->pixel_width >= 0);
19089 eassert (row->ascent >= 0 && row->height > 0);
19090
19091 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19092 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19093
19094 /* If first line's physical ascent is larger than its logical
19095 ascent, use the physical ascent, and make the row taller.
19096 This makes accented characters fully visible. */
19097 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19098 && row->phys_ascent > row->ascent)
19099 {
19100 row->height += row->phys_ascent - row->ascent;
19101 row->ascent = row->phys_ascent;
19102 }
19103
19104 /* Compute how much of the line is visible. */
19105 row->visible_height = row->height;
19106
19107 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19108 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19109
19110 if (row->y < min_y)
19111 row->visible_height -= min_y - row->y;
19112 if (row->y + row->height > max_y)
19113 row->visible_height -= row->y + row->height - max_y;
19114 }
19115 else
19116 {
19117 row->pixel_width = row->used[TEXT_AREA];
19118 if (row->continued_p)
19119 row->pixel_width -= it->continuation_pixel_width;
19120 else if (row->truncated_on_right_p)
19121 row->pixel_width -= it->truncation_pixel_width;
19122 row->ascent = row->phys_ascent = 0;
19123 row->height = row->phys_height = row->visible_height = 1;
19124 row->extra_line_spacing = 0;
19125 }
19126
19127 /* Compute a hash code for this row. */
19128 row->hash = row_hash (row);
19129
19130 it->max_ascent = it->max_descent = 0;
19131 it->max_phys_ascent = it->max_phys_descent = 0;
19132 }
19133
19134
19135 /* Append one space to the glyph row of iterator IT if doing a
19136 window-based redisplay. The space has the same face as
19137 IT->face_id. Value is non-zero if a space was added.
19138
19139 This function is called to make sure that there is always one glyph
19140 at the end of a glyph row that the cursor can be set on under
19141 window-systems. (If there weren't such a glyph we would not know
19142 how wide and tall a box cursor should be displayed).
19143
19144 At the same time this space let's a nicely handle clearing to the
19145 end of the line if the row ends in italic text. */
19146
19147 static int
19148 append_space_for_newline (struct it *it, int default_face_p)
19149 {
19150 if (FRAME_WINDOW_P (it->f))
19151 {
19152 int n = it->glyph_row->used[TEXT_AREA];
19153
19154 if (it->glyph_row->glyphs[TEXT_AREA] + n
19155 < it->glyph_row->glyphs[1 + TEXT_AREA])
19156 {
19157 /* Save some values that must not be changed.
19158 Must save IT->c and IT->len because otherwise
19159 ITERATOR_AT_END_P wouldn't work anymore after
19160 append_space_for_newline has been called. */
19161 enum display_element_type saved_what = it->what;
19162 int saved_c = it->c, saved_len = it->len;
19163 int saved_char_to_display = it->char_to_display;
19164 int saved_x = it->current_x;
19165 int saved_face_id = it->face_id;
19166 int saved_box_end = it->end_of_box_run_p;
19167 struct text_pos saved_pos;
19168 Lisp_Object saved_object;
19169 struct face *face;
19170
19171 saved_object = it->object;
19172 saved_pos = it->position;
19173
19174 it->what = IT_CHARACTER;
19175 memset (&it->position, 0, sizeof it->position);
19176 it->object = make_number (0);
19177 it->c = it->char_to_display = ' ';
19178 it->len = 1;
19179
19180 /* If the default face was remapped, be sure to use the
19181 remapped face for the appended newline. */
19182 if (default_face_p)
19183 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19184 else if (it->face_before_selective_p)
19185 it->face_id = it->saved_face_id;
19186 face = FACE_FROM_ID (it->f, it->face_id);
19187 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19188 /* In R2L rows, we will prepend a stretch glyph that will
19189 have the end_of_box_run_p flag set for it, so there's no
19190 need for the appended newline glyph to have that flag
19191 set. */
19192 if (it->glyph_row->reversed_p
19193 /* But if the appended newline glyph goes all the way to
19194 the end of the row, there will be no stretch glyph,
19195 so leave the box flag set. */
19196 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19197 it->end_of_box_run_p = 0;
19198
19199 PRODUCE_GLYPHS (it);
19200
19201 it->override_ascent = -1;
19202 it->constrain_row_ascent_descent_p = 0;
19203 it->current_x = saved_x;
19204 it->object = saved_object;
19205 it->position = saved_pos;
19206 it->what = saved_what;
19207 it->face_id = saved_face_id;
19208 it->len = saved_len;
19209 it->c = saved_c;
19210 it->char_to_display = saved_char_to_display;
19211 it->end_of_box_run_p = saved_box_end;
19212 return 1;
19213 }
19214 }
19215
19216 return 0;
19217 }
19218
19219
19220 /* Extend the face of the last glyph in the text area of IT->glyph_row
19221 to the end of the display line. Called from display_line. If the
19222 glyph row is empty, add a space glyph to it so that we know the
19223 face to draw. Set the glyph row flag fill_line_p. If the glyph
19224 row is R2L, prepend a stretch glyph to cover the empty space to the
19225 left of the leftmost glyph. */
19226
19227 static void
19228 extend_face_to_end_of_line (struct it *it)
19229 {
19230 struct face *face, *default_face;
19231 struct frame *f = it->f;
19232
19233 /* If line is already filled, do nothing. Non window-system frames
19234 get a grace of one more ``pixel'' because their characters are
19235 1-``pixel'' wide, so they hit the equality too early. This grace
19236 is needed only for R2L rows that are not continued, to produce
19237 one extra blank where we could display the cursor. */
19238 if ((it->current_x >= it->last_visible_x
19239 + (!FRAME_WINDOW_P (f)
19240 && it->glyph_row->reversed_p
19241 && !it->glyph_row->continued_p))
19242 /* If the window has display margins, we will need to extend
19243 their face even if the text area is filled. */
19244 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19245 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19246 return;
19247
19248 /* The default face, possibly remapped. */
19249 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19250
19251 /* Face extension extends the background and box of IT->face_id
19252 to the end of the line. If the background equals the background
19253 of the frame, we don't have to do anything. */
19254 if (it->face_before_selective_p)
19255 face = FACE_FROM_ID (f, it->saved_face_id);
19256 else
19257 face = FACE_FROM_ID (f, it->face_id);
19258
19259 if (FRAME_WINDOW_P (f)
19260 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19261 && face->box == FACE_NO_BOX
19262 && face->background == FRAME_BACKGROUND_PIXEL (f)
19263 #ifdef HAVE_WINDOW_SYSTEM
19264 && !face->stipple
19265 #endif
19266 && !it->glyph_row->reversed_p)
19267 return;
19268
19269 /* Set the glyph row flag indicating that the face of the last glyph
19270 in the text area has to be drawn to the end of the text area. */
19271 it->glyph_row->fill_line_p = 1;
19272
19273 /* If current character of IT is not ASCII, make sure we have the
19274 ASCII face. This will be automatically undone the next time
19275 get_next_display_element returns a multibyte character. Note
19276 that the character will always be single byte in unibyte
19277 text. */
19278 if (!ASCII_CHAR_P (it->c))
19279 {
19280 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19281 }
19282
19283 if (FRAME_WINDOW_P (f))
19284 {
19285 /* If the row is empty, add a space with the current face of IT,
19286 so that we know which face to draw. */
19287 if (it->glyph_row->used[TEXT_AREA] == 0)
19288 {
19289 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19290 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19291 it->glyph_row->used[TEXT_AREA] = 1;
19292 }
19293 /* Mode line and the header line don't have margins, and
19294 likewise the frame's tool-bar window, if there is any. */
19295 if (!(it->glyph_row->mode_line_p
19296 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19297 || (WINDOWP (f->tool_bar_window)
19298 && it->w == XWINDOW (f->tool_bar_window))
19299 #endif
19300 ))
19301 {
19302 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19303 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19304 {
19305 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19306 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19307 default_face->id;
19308 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19309 }
19310 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19311 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19312 {
19313 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19314 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19315 default_face->id;
19316 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19317 }
19318 }
19319 #ifdef HAVE_WINDOW_SYSTEM
19320 if (it->glyph_row->reversed_p)
19321 {
19322 /* Prepend a stretch glyph to the row, such that the
19323 rightmost glyph will be drawn flushed all the way to the
19324 right margin of the window. The stretch glyph that will
19325 occupy the empty space, if any, to the left of the
19326 glyphs. */
19327 struct font *font = face->font ? face->font : FRAME_FONT (f);
19328 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19329 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19330 struct glyph *g;
19331 int row_width, stretch_ascent, stretch_width;
19332 struct text_pos saved_pos;
19333 int saved_face_id, saved_avoid_cursor, saved_box_start;
19334
19335 for (row_width = 0, g = row_start; g < row_end; g++)
19336 row_width += g->pixel_width;
19337 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
19338 if (stretch_width > 0)
19339 {
19340 stretch_ascent =
19341 (((it->ascent + it->descent)
19342 * FONT_BASE (font)) / FONT_HEIGHT (font));
19343 saved_pos = it->position;
19344 memset (&it->position, 0, sizeof it->position);
19345 saved_avoid_cursor = it->avoid_cursor_p;
19346 it->avoid_cursor_p = 1;
19347 saved_face_id = it->face_id;
19348 saved_box_start = it->start_of_box_run_p;
19349 /* The last row's stretch glyph should get the default
19350 face, to avoid painting the rest of the window with
19351 the region face, if the region ends at ZV. */
19352 if (it->glyph_row->ends_at_zv_p)
19353 it->face_id = default_face->id;
19354 else
19355 it->face_id = face->id;
19356 it->start_of_box_run_p = 0;
19357 append_stretch_glyph (it, make_number (0), stretch_width,
19358 it->ascent + it->descent, stretch_ascent);
19359 it->position = saved_pos;
19360 it->avoid_cursor_p = saved_avoid_cursor;
19361 it->face_id = saved_face_id;
19362 it->start_of_box_run_p = saved_box_start;
19363 }
19364 /* If stretch_width comes out negative, it means that the
19365 last glyph is only partially visible. In R2L rows, we
19366 want the leftmost glyph to be partially visible, so we
19367 need to give the row the corresponding left offset. */
19368 if (stretch_width < 0)
19369 it->glyph_row->x = stretch_width;
19370 }
19371 #endif /* HAVE_WINDOW_SYSTEM */
19372 }
19373 else
19374 {
19375 /* Save some values that must not be changed. */
19376 int saved_x = it->current_x;
19377 struct text_pos saved_pos;
19378 Lisp_Object saved_object;
19379 enum display_element_type saved_what = it->what;
19380 int saved_face_id = it->face_id;
19381
19382 saved_object = it->object;
19383 saved_pos = it->position;
19384
19385 it->what = IT_CHARACTER;
19386 memset (&it->position, 0, sizeof it->position);
19387 it->object = make_number (0);
19388 it->c = it->char_to_display = ' ';
19389 it->len = 1;
19390
19391 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19392 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19393 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19394 && !it->glyph_row->mode_line_p
19395 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19396 {
19397 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19398 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19399
19400 for (it->current_x = 0; g < e; g++)
19401 it->current_x += g->pixel_width;
19402
19403 it->area = LEFT_MARGIN_AREA;
19404 it->face_id = default_face->id;
19405 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19406 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19407 {
19408 PRODUCE_GLYPHS (it);
19409 /* term.c:produce_glyphs advances it->current_x only for
19410 TEXT_AREA. */
19411 it->current_x += it->pixel_width;
19412 }
19413
19414 it->current_x = saved_x;
19415 it->area = TEXT_AREA;
19416 }
19417
19418 /* The last row's blank glyphs should get the default face, to
19419 avoid painting the rest of the window with the region face,
19420 if the region ends at ZV. */
19421 if (it->glyph_row->ends_at_zv_p)
19422 it->face_id = default_face->id;
19423 else
19424 it->face_id = face->id;
19425 PRODUCE_GLYPHS (it);
19426
19427 while (it->current_x <= it->last_visible_x)
19428 PRODUCE_GLYPHS (it);
19429
19430 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19431 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19432 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19433 && !it->glyph_row->mode_line_p
19434 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19435 {
19436 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19437 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19438
19439 for ( ; g < e; g++)
19440 it->current_x += g->pixel_width;
19441
19442 it->area = RIGHT_MARGIN_AREA;
19443 it->face_id = default_face->id;
19444 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19445 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19446 {
19447 PRODUCE_GLYPHS (it);
19448 it->current_x += it->pixel_width;
19449 }
19450
19451 it->area = TEXT_AREA;
19452 }
19453
19454 /* Don't count these blanks really. It would let us insert a left
19455 truncation glyph below and make us set the cursor on them, maybe. */
19456 it->current_x = saved_x;
19457 it->object = saved_object;
19458 it->position = saved_pos;
19459 it->what = saved_what;
19460 it->face_id = saved_face_id;
19461 }
19462 }
19463
19464
19465 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19466 trailing whitespace. */
19467
19468 static int
19469 trailing_whitespace_p (ptrdiff_t charpos)
19470 {
19471 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19472 int c = 0;
19473
19474 while (bytepos < ZV_BYTE
19475 && (c = FETCH_CHAR (bytepos),
19476 c == ' ' || c == '\t'))
19477 ++bytepos;
19478
19479 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19480 {
19481 if (bytepos != PT_BYTE)
19482 return 1;
19483 }
19484 return 0;
19485 }
19486
19487
19488 /* Highlight trailing whitespace, if any, in ROW. */
19489
19490 static void
19491 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19492 {
19493 int used = row->used[TEXT_AREA];
19494
19495 if (used)
19496 {
19497 struct glyph *start = row->glyphs[TEXT_AREA];
19498 struct glyph *glyph = start + used - 1;
19499
19500 if (row->reversed_p)
19501 {
19502 /* Right-to-left rows need to be processed in the opposite
19503 direction, so swap the edge pointers. */
19504 glyph = start;
19505 start = row->glyphs[TEXT_AREA] + used - 1;
19506 }
19507
19508 /* Skip over glyphs inserted to display the cursor at the
19509 end of a line, for extending the face of the last glyph
19510 to the end of the line on terminals, and for truncation
19511 and continuation glyphs. */
19512 if (!row->reversed_p)
19513 {
19514 while (glyph >= start
19515 && glyph->type == CHAR_GLYPH
19516 && INTEGERP (glyph->object))
19517 --glyph;
19518 }
19519 else
19520 {
19521 while (glyph <= start
19522 && glyph->type == CHAR_GLYPH
19523 && INTEGERP (glyph->object))
19524 ++glyph;
19525 }
19526
19527 /* If last glyph is a space or stretch, and it's trailing
19528 whitespace, set the face of all trailing whitespace glyphs in
19529 IT->glyph_row to `trailing-whitespace'. */
19530 if ((row->reversed_p ? glyph <= start : glyph >= start)
19531 && BUFFERP (glyph->object)
19532 && (glyph->type == STRETCH_GLYPH
19533 || (glyph->type == CHAR_GLYPH
19534 && glyph->u.ch == ' '))
19535 && trailing_whitespace_p (glyph->charpos))
19536 {
19537 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19538 if (face_id < 0)
19539 return;
19540
19541 if (!row->reversed_p)
19542 {
19543 while (glyph >= start
19544 && BUFFERP (glyph->object)
19545 && (glyph->type == STRETCH_GLYPH
19546 || (glyph->type == CHAR_GLYPH
19547 && glyph->u.ch == ' ')))
19548 (glyph--)->face_id = face_id;
19549 }
19550 else
19551 {
19552 while (glyph <= start
19553 && BUFFERP (glyph->object)
19554 && (glyph->type == STRETCH_GLYPH
19555 || (glyph->type == CHAR_GLYPH
19556 && glyph->u.ch == ' ')))
19557 (glyph++)->face_id = face_id;
19558 }
19559 }
19560 }
19561 }
19562
19563
19564 /* Value is non-zero if glyph row ROW should be
19565 considered to hold the buffer position CHARPOS. */
19566
19567 static int
19568 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19569 {
19570 int result = 1;
19571
19572 if (charpos == CHARPOS (row->end.pos)
19573 || charpos == MATRIX_ROW_END_CHARPOS (row))
19574 {
19575 /* Suppose the row ends on a string.
19576 Unless the row is continued, that means it ends on a newline
19577 in the string. If it's anything other than a display string
19578 (e.g., a before-string from an overlay), we don't want the
19579 cursor there. (This heuristic seems to give the optimal
19580 behavior for the various types of multi-line strings.)
19581 One exception: if the string has `cursor' property on one of
19582 its characters, we _do_ want the cursor there. */
19583 if (CHARPOS (row->end.string_pos) >= 0)
19584 {
19585 if (row->continued_p)
19586 result = 1;
19587 else
19588 {
19589 /* Check for `display' property. */
19590 struct glyph *beg = row->glyphs[TEXT_AREA];
19591 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19592 struct glyph *glyph;
19593
19594 result = 0;
19595 for (glyph = end; glyph >= beg; --glyph)
19596 if (STRINGP (glyph->object))
19597 {
19598 Lisp_Object prop
19599 = Fget_char_property (make_number (charpos),
19600 Qdisplay, Qnil);
19601 result =
19602 (!NILP (prop)
19603 && display_prop_string_p (prop, glyph->object));
19604 /* If there's a `cursor' property on one of the
19605 string's characters, this row is a cursor row,
19606 even though this is not a display string. */
19607 if (!result)
19608 {
19609 Lisp_Object s = glyph->object;
19610
19611 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19612 {
19613 ptrdiff_t gpos = glyph->charpos;
19614
19615 if (!NILP (Fget_char_property (make_number (gpos),
19616 Qcursor, s)))
19617 {
19618 result = 1;
19619 break;
19620 }
19621 }
19622 }
19623 break;
19624 }
19625 }
19626 }
19627 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19628 {
19629 /* If the row ends in middle of a real character,
19630 and the line is continued, we want the cursor here.
19631 That's because CHARPOS (ROW->end.pos) would equal
19632 PT if PT is before the character. */
19633 if (!row->ends_in_ellipsis_p)
19634 result = row->continued_p;
19635 else
19636 /* If the row ends in an ellipsis, then
19637 CHARPOS (ROW->end.pos) will equal point after the
19638 invisible text. We want that position to be displayed
19639 after the ellipsis. */
19640 result = 0;
19641 }
19642 /* If the row ends at ZV, display the cursor at the end of that
19643 row instead of at the start of the row below. */
19644 else if (row->ends_at_zv_p)
19645 result = 1;
19646 else
19647 result = 0;
19648 }
19649
19650 return result;
19651 }
19652
19653 /* Value is non-zero if glyph row ROW should be
19654 used to hold the cursor. */
19655
19656 static int
19657 cursor_row_p (struct glyph_row *row)
19658 {
19659 return row_for_charpos_p (row, PT);
19660 }
19661
19662 \f
19663
19664 /* Push the property PROP so that it will be rendered at the current
19665 position in IT. Return 1 if PROP was successfully pushed, 0
19666 otherwise. Called from handle_line_prefix to handle the
19667 `line-prefix' and `wrap-prefix' properties. */
19668
19669 static int
19670 push_prefix_prop (struct it *it, Lisp_Object prop)
19671 {
19672 struct text_pos pos =
19673 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19674
19675 eassert (it->method == GET_FROM_BUFFER
19676 || it->method == GET_FROM_DISPLAY_VECTOR
19677 || it->method == GET_FROM_STRING);
19678
19679 /* We need to save the current buffer/string position, so it will be
19680 restored by pop_it, because iterate_out_of_display_property
19681 depends on that being set correctly, but some situations leave
19682 it->position not yet set when this function is called. */
19683 push_it (it, &pos);
19684
19685 if (STRINGP (prop))
19686 {
19687 if (SCHARS (prop) == 0)
19688 {
19689 pop_it (it);
19690 return 0;
19691 }
19692
19693 it->string = prop;
19694 it->string_from_prefix_prop_p = 1;
19695 it->multibyte_p = STRING_MULTIBYTE (it->string);
19696 it->current.overlay_string_index = -1;
19697 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19698 it->end_charpos = it->string_nchars = SCHARS (it->string);
19699 it->method = GET_FROM_STRING;
19700 it->stop_charpos = 0;
19701 it->prev_stop = 0;
19702 it->base_level_stop = 0;
19703
19704 /* Force paragraph direction to be that of the parent
19705 buffer/string. */
19706 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19707 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19708 else
19709 it->paragraph_embedding = L2R;
19710
19711 /* Set up the bidi iterator for this display string. */
19712 if (it->bidi_p)
19713 {
19714 it->bidi_it.string.lstring = it->string;
19715 it->bidi_it.string.s = NULL;
19716 it->bidi_it.string.schars = it->end_charpos;
19717 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19718 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19719 it->bidi_it.string.unibyte = !it->multibyte_p;
19720 it->bidi_it.w = it->w;
19721 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19722 }
19723 }
19724 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19725 {
19726 it->method = GET_FROM_STRETCH;
19727 it->object = prop;
19728 }
19729 #ifdef HAVE_WINDOW_SYSTEM
19730 else if (IMAGEP (prop))
19731 {
19732 it->what = IT_IMAGE;
19733 it->image_id = lookup_image (it->f, prop);
19734 it->method = GET_FROM_IMAGE;
19735 }
19736 #endif /* HAVE_WINDOW_SYSTEM */
19737 else
19738 {
19739 pop_it (it); /* bogus display property, give up */
19740 return 0;
19741 }
19742
19743 return 1;
19744 }
19745
19746 /* Return the character-property PROP at the current position in IT. */
19747
19748 static Lisp_Object
19749 get_it_property (struct it *it, Lisp_Object prop)
19750 {
19751 Lisp_Object position, object = it->object;
19752
19753 if (STRINGP (object))
19754 position = make_number (IT_STRING_CHARPOS (*it));
19755 else if (BUFFERP (object))
19756 {
19757 position = make_number (IT_CHARPOS (*it));
19758 object = it->window;
19759 }
19760 else
19761 return Qnil;
19762
19763 return Fget_char_property (position, prop, object);
19764 }
19765
19766 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19767
19768 static void
19769 handle_line_prefix (struct it *it)
19770 {
19771 Lisp_Object prefix;
19772
19773 if (it->continuation_lines_width > 0)
19774 {
19775 prefix = get_it_property (it, Qwrap_prefix);
19776 if (NILP (prefix))
19777 prefix = Vwrap_prefix;
19778 }
19779 else
19780 {
19781 prefix = get_it_property (it, Qline_prefix);
19782 if (NILP (prefix))
19783 prefix = Vline_prefix;
19784 }
19785 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19786 {
19787 /* If the prefix is wider than the window, and we try to wrap
19788 it, it would acquire its own wrap prefix, and so on till the
19789 iterator stack overflows. So, don't wrap the prefix. */
19790 it->line_wrap = TRUNCATE;
19791 it->avoid_cursor_p = 1;
19792 }
19793 }
19794
19795 \f
19796
19797 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19798 only for R2L lines from display_line and display_string, when they
19799 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19800 the line/string needs to be continued on the next glyph row. */
19801 static void
19802 unproduce_glyphs (struct it *it, int n)
19803 {
19804 struct glyph *glyph, *end;
19805
19806 eassert (it->glyph_row);
19807 eassert (it->glyph_row->reversed_p);
19808 eassert (it->area == TEXT_AREA);
19809 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19810
19811 if (n > it->glyph_row->used[TEXT_AREA])
19812 n = it->glyph_row->used[TEXT_AREA];
19813 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19814 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19815 for ( ; glyph < end; glyph++)
19816 glyph[-n] = *glyph;
19817 }
19818
19819 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19820 and ROW->maxpos. */
19821 static void
19822 find_row_edges (struct it *it, struct glyph_row *row,
19823 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19824 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19825 {
19826 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19827 lines' rows is implemented for bidi-reordered rows. */
19828
19829 /* ROW->minpos is the value of min_pos, the minimal buffer position
19830 we have in ROW, or ROW->start.pos if that is smaller. */
19831 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19832 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19833 else
19834 /* We didn't find buffer positions smaller than ROW->start, or
19835 didn't find _any_ valid buffer positions in any of the glyphs,
19836 so we must trust the iterator's computed positions. */
19837 row->minpos = row->start.pos;
19838 if (max_pos <= 0)
19839 {
19840 max_pos = CHARPOS (it->current.pos);
19841 max_bpos = BYTEPOS (it->current.pos);
19842 }
19843
19844 /* Here are the various use-cases for ending the row, and the
19845 corresponding values for ROW->maxpos:
19846
19847 Line ends in a newline from buffer eol_pos + 1
19848 Line is continued from buffer max_pos + 1
19849 Line is truncated on right it->current.pos
19850 Line ends in a newline from string max_pos + 1(*)
19851 (*) + 1 only when line ends in a forward scan
19852 Line is continued from string max_pos
19853 Line is continued from display vector max_pos
19854 Line is entirely from a string min_pos == max_pos
19855 Line is entirely from a display vector min_pos == max_pos
19856 Line that ends at ZV ZV
19857
19858 If you discover other use-cases, please add them here as
19859 appropriate. */
19860 if (row->ends_at_zv_p)
19861 row->maxpos = it->current.pos;
19862 else if (row->used[TEXT_AREA])
19863 {
19864 int seen_this_string = 0;
19865 struct glyph_row *r1 = row - 1;
19866
19867 /* Did we see the same display string on the previous row? */
19868 if (STRINGP (it->object)
19869 /* this is not the first row */
19870 && row > it->w->desired_matrix->rows
19871 /* previous row is not the header line */
19872 && !r1->mode_line_p
19873 /* previous row also ends in a newline from a string */
19874 && r1->ends_in_newline_from_string_p)
19875 {
19876 struct glyph *start, *end;
19877
19878 /* Search for the last glyph of the previous row that came
19879 from buffer or string. Depending on whether the row is
19880 L2R or R2L, we need to process it front to back or the
19881 other way round. */
19882 if (!r1->reversed_p)
19883 {
19884 start = r1->glyphs[TEXT_AREA];
19885 end = start + r1->used[TEXT_AREA];
19886 /* Glyphs inserted by redisplay have an integer (zero)
19887 as their object. */
19888 while (end > start
19889 && INTEGERP ((end - 1)->object)
19890 && (end - 1)->charpos <= 0)
19891 --end;
19892 if (end > start)
19893 {
19894 if (EQ ((end - 1)->object, it->object))
19895 seen_this_string = 1;
19896 }
19897 else
19898 /* If all the glyphs of the previous row were inserted
19899 by redisplay, it means the previous row was
19900 produced from a single newline, which is only
19901 possible if that newline came from the same string
19902 as the one which produced this ROW. */
19903 seen_this_string = 1;
19904 }
19905 else
19906 {
19907 end = r1->glyphs[TEXT_AREA] - 1;
19908 start = end + r1->used[TEXT_AREA];
19909 while (end < start
19910 && INTEGERP ((end + 1)->object)
19911 && (end + 1)->charpos <= 0)
19912 ++end;
19913 if (end < start)
19914 {
19915 if (EQ ((end + 1)->object, it->object))
19916 seen_this_string = 1;
19917 }
19918 else
19919 seen_this_string = 1;
19920 }
19921 }
19922 /* Take note of each display string that covers a newline only
19923 once, the first time we see it. This is for when a display
19924 string includes more than one newline in it. */
19925 if (row->ends_in_newline_from_string_p && !seen_this_string)
19926 {
19927 /* If we were scanning the buffer forward when we displayed
19928 the string, we want to account for at least one buffer
19929 position that belongs to this row (position covered by
19930 the display string), so that cursor positioning will
19931 consider this row as a candidate when point is at the end
19932 of the visual line represented by this row. This is not
19933 required when scanning back, because max_pos will already
19934 have a much larger value. */
19935 if (CHARPOS (row->end.pos) > max_pos)
19936 INC_BOTH (max_pos, max_bpos);
19937 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19938 }
19939 else if (CHARPOS (it->eol_pos) > 0)
19940 SET_TEXT_POS (row->maxpos,
19941 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19942 else if (row->continued_p)
19943 {
19944 /* If max_pos is different from IT's current position, it
19945 means IT->method does not belong to the display element
19946 at max_pos. However, it also means that the display
19947 element at max_pos was displayed in its entirety on this
19948 line, which is equivalent to saying that the next line
19949 starts at the next buffer position. */
19950 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19951 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19952 else
19953 {
19954 INC_BOTH (max_pos, max_bpos);
19955 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19956 }
19957 }
19958 else if (row->truncated_on_right_p)
19959 /* display_line already called reseat_at_next_visible_line_start,
19960 which puts the iterator at the beginning of the next line, in
19961 the logical order. */
19962 row->maxpos = it->current.pos;
19963 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19964 /* A line that is entirely from a string/image/stretch... */
19965 row->maxpos = row->minpos;
19966 else
19967 emacs_abort ();
19968 }
19969 else
19970 row->maxpos = it->current.pos;
19971 }
19972
19973 /* Construct the glyph row IT->glyph_row in the desired matrix of
19974 IT->w from text at the current position of IT. See dispextern.h
19975 for an overview of struct it. Value is non-zero if
19976 IT->glyph_row displays text, as opposed to a line displaying ZV
19977 only. */
19978
19979 static int
19980 display_line (struct it *it)
19981 {
19982 struct glyph_row *row = it->glyph_row;
19983 Lisp_Object overlay_arrow_string;
19984 struct it wrap_it;
19985 void *wrap_data = NULL;
19986 int may_wrap = 0, wrap_x IF_LINT (= 0);
19987 int wrap_row_used = -1;
19988 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19989 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19990 int wrap_row_extra_line_spacing IF_LINT (= 0);
19991 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19992 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19993 int cvpos;
19994 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19995 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19996 bool pending_handle_line_prefix = false;
19997
19998 /* We always start displaying at hpos zero even if hscrolled. */
19999 eassert (it->hpos == 0 && it->current_x == 0);
20000
20001 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20002 >= it->w->desired_matrix->nrows)
20003 {
20004 it->w->nrows_scale_factor++;
20005 it->f->fonts_changed = 1;
20006 return 0;
20007 }
20008
20009 /* Clear the result glyph row and enable it. */
20010 prepare_desired_row (it->w, row, false);
20011
20012 row->y = it->current_y;
20013 row->start = it->start;
20014 row->continuation_lines_width = it->continuation_lines_width;
20015 row->displays_text_p = 1;
20016 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20017 it->starts_in_middle_of_char_p = 0;
20018
20019 /* Arrange the overlays nicely for our purposes. Usually, we call
20020 display_line on only one line at a time, in which case this
20021 can't really hurt too much, or we call it on lines which appear
20022 one after another in the buffer, in which case all calls to
20023 recenter_overlay_lists but the first will be pretty cheap. */
20024 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20025
20026 /* Move over display elements that are not visible because we are
20027 hscrolled. This may stop at an x-position < IT->first_visible_x
20028 if the first glyph is partially visible or if we hit a line end. */
20029 if (it->current_x < it->first_visible_x)
20030 {
20031 enum move_it_result move_result;
20032
20033 this_line_min_pos = row->start.pos;
20034 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20035 MOVE_TO_POS | MOVE_TO_X);
20036 /* If we are under a large hscroll, move_it_in_display_line_to
20037 could hit the end of the line without reaching
20038 it->first_visible_x. Pretend that we did reach it. This is
20039 especially important on a TTY, where we will call
20040 extend_face_to_end_of_line, which needs to know how many
20041 blank glyphs to produce. */
20042 if (it->current_x < it->first_visible_x
20043 && (move_result == MOVE_NEWLINE_OR_CR
20044 || move_result == MOVE_POS_MATCH_OR_ZV))
20045 it->current_x = it->first_visible_x;
20046
20047 /* Record the smallest positions seen while we moved over
20048 display elements that are not visible. This is needed by
20049 redisplay_internal for optimizing the case where the cursor
20050 stays inside the same line. The rest of this function only
20051 considers positions that are actually displayed, so
20052 RECORD_MAX_MIN_POS will not otherwise record positions that
20053 are hscrolled to the left of the left edge of the window. */
20054 min_pos = CHARPOS (this_line_min_pos);
20055 min_bpos = BYTEPOS (this_line_min_pos);
20056 }
20057 else if (it->area == TEXT_AREA)
20058 {
20059 /* We only do this when not calling move_it_in_display_line_to
20060 above, because that function calls itself handle_line_prefix. */
20061 handle_line_prefix (it);
20062 }
20063 else
20064 {
20065 /* Line-prefix and wrap-prefix are always displayed in the text
20066 area. But if this is the first call to display_line after
20067 init_iterator, the iterator might have been set up to write
20068 into a marginal area, e.g. if the line begins with some
20069 display property that writes to the margins. So we need to
20070 wait with the call to handle_line_prefix until whatever
20071 writes to the margin has done its job. */
20072 pending_handle_line_prefix = true;
20073 }
20074
20075 /* Get the initial row height. This is either the height of the
20076 text hscrolled, if there is any, or zero. */
20077 row->ascent = it->max_ascent;
20078 row->height = it->max_ascent + it->max_descent;
20079 row->phys_ascent = it->max_phys_ascent;
20080 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20081 row->extra_line_spacing = it->max_extra_line_spacing;
20082
20083 /* Utility macro to record max and min buffer positions seen until now. */
20084 #define RECORD_MAX_MIN_POS(IT) \
20085 do \
20086 { \
20087 int composition_p = !STRINGP ((IT)->string) \
20088 && ((IT)->what == IT_COMPOSITION); \
20089 ptrdiff_t current_pos = \
20090 composition_p ? (IT)->cmp_it.charpos \
20091 : IT_CHARPOS (*(IT)); \
20092 ptrdiff_t current_bpos = \
20093 composition_p ? CHAR_TO_BYTE (current_pos) \
20094 : IT_BYTEPOS (*(IT)); \
20095 if (current_pos < min_pos) \
20096 { \
20097 min_pos = current_pos; \
20098 min_bpos = current_bpos; \
20099 } \
20100 if (IT_CHARPOS (*it) > max_pos) \
20101 { \
20102 max_pos = IT_CHARPOS (*it); \
20103 max_bpos = IT_BYTEPOS (*it); \
20104 } \
20105 } \
20106 while (0)
20107
20108 /* Loop generating characters. The loop is left with IT on the next
20109 character to display. */
20110 while (1)
20111 {
20112 int n_glyphs_before, hpos_before, x_before;
20113 int x, nglyphs;
20114 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20115
20116 /* Retrieve the next thing to display. Value is zero if end of
20117 buffer reached. */
20118 if (!get_next_display_element (it))
20119 {
20120 /* Maybe add a space at the end of this line that is used to
20121 display the cursor there under X. Set the charpos of the
20122 first glyph of blank lines not corresponding to any text
20123 to -1. */
20124 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20125 row->exact_window_width_line_p = 1;
20126 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20127 || row->used[TEXT_AREA] == 0)
20128 {
20129 row->glyphs[TEXT_AREA]->charpos = -1;
20130 row->displays_text_p = 0;
20131
20132 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20133 && (!MINI_WINDOW_P (it->w)
20134 || (minibuf_level && EQ (it->window, minibuf_window))))
20135 row->indicate_empty_line_p = 1;
20136 }
20137
20138 it->continuation_lines_width = 0;
20139 row->ends_at_zv_p = 1;
20140 /* A row that displays right-to-left text must always have
20141 its last face extended all the way to the end of line,
20142 even if this row ends in ZV, because we still write to
20143 the screen left to right. We also need to extend the
20144 last face if the default face is remapped to some
20145 different face, otherwise the functions that clear
20146 portions of the screen will clear with the default face's
20147 background color. */
20148 if (row->reversed_p
20149 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20150 extend_face_to_end_of_line (it);
20151 break;
20152 }
20153
20154 /* Now, get the metrics of what we want to display. This also
20155 generates glyphs in `row' (which is IT->glyph_row). */
20156 n_glyphs_before = row->used[TEXT_AREA];
20157 x = it->current_x;
20158
20159 /* Remember the line height so far in case the next element doesn't
20160 fit on the line. */
20161 if (it->line_wrap != TRUNCATE)
20162 {
20163 ascent = it->max_ascent;
20164 descent = it->max_descent;
20165 phys_ascent = it->max_phys_ascent;
20166 phys_descent = it->max_phys_descent;
20167
20168 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20169 {
20170 if (IT_DISPLAYING_WHITESPACE (it))
20171 may_wrap = 1;
20172 else if (may_wrap)
20173 {
20174 SAVE_IT (wrap_it, *it, wrap_data);
20175 wrap_x = x;
20176 wrap_row_used = row->used[TEXT_AREA];
20177 wrap_row_ascent = row->ascent;
20178 wrap_row_height = row->height;
20179 wrap_row_phys_ascent = row->phys_ascent;
20180 wrap_row_phys_height = row->phys_height;
20181 wrap_row_extra_line_spacing = row->extra_line_spacing;
20182 wrap_row_min_pos = min_pos;
20183 wrap_row_min_bpos = min_bpos;
20184 wrap_row_max_pos = max_pos;
20185 wrap_row_max_bpos = max_bpos;
20186 may_wrap = 0;
20187 }
20188 }
20189 }
20190
20191 PRODUCE_GLYPHS (it);
20192
20193 /* If this display element was in marginal areas, continue with
20194 the next one. */
20195 if (it->area != TEXT_AREA)
20196 {
20197 row->ascent = max (row->ascent, it->max_ascent);
20198 row->height = max (row->height, it->max_ascent + it->max_descent);
20199 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20200 row->phys_height = max (row->phys_height,
20201 it->max_phys_ascent + it->max_phys_descent);
20202 row->extra_line_spacing = max (row->extra_line_spacing,
20203 it->max_extra_line_spacing);
20204 set_iterator_to_next (it, 1);
20205 /* If we didn't handle the line/wrap prefix above, and the
20206 call to set_iterator_to_next just switched to TEXT_AREA,
20207 process the prefix now. */
20208 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20209 {
20210 pending_handle_line_prefix = false;
20211 handle_line_prefix (it);
20212 }
20213 continue;
20214 }
20215
20216 /* Does the display element fit on the line? If we truncate
20217 lines, we should draw past the right edge of the window. If
20218 we don't truncate, we want to stop so that we can display the
20219 continuation glyph before the right margin. If lines are
20220 continued, there are two possible strategies for characters
20221 resulting in more than 1 glyph (e.g. tabs): Display as many
20222 glyphs as possible in this line and leave the rest for the
20223 continuation line, or display the whole element in the next
20224 line. Original redisplay did the former, so we do it also. */
20225 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20226 hpos_before = it->hpos;
20227 x_before = x;
20228
20229 if (/* Not a newline. */
20230 nglyphs > 0
20231 /* Glyphs produced fit entirely in the line. */
20232 && it->current_x < it->last_visible_x)
20233 {
20234 it->hpos += nglyphs;
20235 row->ascent = max (row->ascent, it->max_ascent);
20236 row->height = max (row->height, it->max_ascent + it->max_descent);
20237 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20238 row->phys_height = max (row->phys_height,
20239 it->max_phys_ascent + it->max_phys_descent);
20240 row->extra_line_spacing = max (row->extra_line_spacing,
20241 it->max_extra_line_spacing);
20242 if (it->current_x - it->pixel_width < it->first_visible_x
20243 /* In R2L rows, we arrange in extend_face_to_end_of_line
20244 to add a right offset to the line, by a suitable
20245 change to the stretch glyph that is the leftmost
20246 glyph of the line. */
20247 && !row->reversed_p)
20248 row->x = x - it->first_visible_x;
20249 /* Record the maximum and minimum buffer positions seen so
20250 far in glyphs that will be displayed by this row. */
20251 if (it->bidi_p)
20252 RECORD_MAX_MIN_POS (it);
20253 }
20254 else
20255 {
20256 int i, new_x;
20257 struct glyph *glyph;
20258
20259 for (i = 0; i < nglyphs; ++i, x = new_x)
20260 {
20261 /* Identify the glyphs added by the last call to
20262 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20263 the previous glyphs. */
20264 if (!row->reversed_p)
20265 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20266 else
20267 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20268 new_x = x + glyph->pixel_width;
20269
20270 if (/* Lines are continued. */
20271 it->line_wrap != TRUNCATE
20272 && (/* Glyph doesn't fit on the line. */
20273 new_x > it->last_visible_x
20274 /* Or it fits exactly on a window system frame. */
20275 || (new_x == it->last_visible_x
20276 && FRAME_WINDOW_P (it->f)
20277 && (row->reversed_p
20278 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20279 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20280 {
20281 /* End of a continued line. */
20282
20283 if (it->hpos == 0
20284 || (new_x == it->last_visible_x
20285 && FRAME_WINDOW_P (it->f)
20286 && (row->reversed_p
20287 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20288 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20289 {
20290 /* Current glyph is the only one on the line or
20291 fits exactly on the line. We must continue
20292 the line because we can't draw the cursor
20293 after the glyph. */
20294 row->continued_p = 1;
20295 it->current_x = new_x;
20296 it->continuation_lines_width += new_x;
20297 ++it->hpos;
20298 if (i == nglyphs - 1)
20299 {
20300 /* If line-wrap is on, check if a previous
20301 wrap point was found. */
20302 if (wrap_row_used > 0
20303 /* Even if there is a previous wrap
20304 point, continue the line here as
20305 usual, if (i) the previous character
20306 was a space or tab AND (ii) the
20307 current character is not. */
20308 && (!may_wrap
20309 || IT_DISPLAYING_WHITESPACE (it)))
20310 goto back_to_wrap;
20311
20312 /* Record the maximum and minimum buffer
20313 positions seen so far in glyphs that will be
20314 displayed by this row. */
20315 if (it->bidi_p)
20316 RECORD_MAX_MIN_POS (it);
20317 set_iterator_to_next (it, 1);
20318 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20319 {
20320 if (!get_next_display_element (it))
20321 {
20322 row->exact_window_width_line_p = 1;
20323 it->continuation_lines_width = 0;
20324 row->continued_p = 0;
20325 row->ends_at_zv_p = 1;
20326 }
20327 else if (ITERATOR_AT_END_OF_LINE_P (it))
20328 {
20329 row->continued_p = 0;
20330 row->exact_window_width_line_p = 1;
20331 }
20332 }
20333 }
20334 else if (it->bidi_p)
20335 RECORD_MAX_MIN_POS (it);
20336 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20337 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20338 extend_face_to_end_of_line (it);
20339 }
20340 else if (CHAR_GLYPH_PADDING_P (*glyph)
20341 && !FRAME_WINDOW_P (it->f))
20342 {
20343 /* A padding glyph that doesn't fit on this line.
20344 This means the whole character doesn't fit
20345 on the line. */
20346 if (row->reversed_p)
20347 unproduce_glyphs (it, row->used[TEXT_AREA]
20348 - n_glyphs_before);
20349 row->used[TEXT_AREA] = n_glyphs_before;
20350
20351 /* Fill the rest of the row with continuation
20352 glyphs like in 20.x. */
20353 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20354 < row->glyphs[1 + TEXT_AREA])
20355 produce_special_glyphs (it, IT_CONTINUATION);
20356
20357 row->continued_p = 1;
20358 it->current_x = x_before;
20359 it->continuation_lines_width += x_before;
20360
20361 /* Restore the height to what it was before the
20362 element not fitting on the line. */
20363 it->max_ascent = ascent;
20364 it->max_descent = descent;
20365 it->max_phys_ascent = phys_ascent;
20366 it->max_phys_descent = phys_descent;
20367 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20368 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20369 extend_face_to_end_of_line (it);
20370 }
20371 else if (wrap_row_used > 0)
20372 {
20373 back_to_wrap:
20374 if (row->reversed_p)
20375 unproduce_glyphs (it,
20376 row->used[TEXT_AREA] - wrap_row_used);
20377 RESTORE_IT (it, &wrap_it, wrap_data);
20378 it->continuation_lines_width += wrap_x;
20379 row->used[TEXT_AREA] = wrap_row_used;
20380 row->ascent = wrap_row_ascent;
20381 row->height = wrap_row_height;
20382 row->phys_ascent = wrap_row_phys_ascent;
20383 row->phys_height = wrap_row_phys_height;
20384 row->extra_line_spacing = wrap_row_extra_line_spacing;
20385 min_pos = wrap_row_min_pos;
20386 min_bpos = wrap_row_min_bpos;
20387 max_pos = wrap_row_max_pos;
20388 max_bpos = wrap_row_max_bpos;
20389 row->continued_p = 1;
20390 row->ends_at_zv_p = 0;
20391 row->exact_window_width_line_p = 0;
20392 it->continuation_lines_width += x;
20393
20394 /* Make sure that a non-default face is extended
20395 up to the right margin of the window. */
20396 extend_face_to_end_of_line (it);
20397 }
20398 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20399 {
20400 /* A TAB that extends past the right edge of the
20401 window. This produces a single glyph on
20402 window system frames. We leave the glyph in
20403 this row and let it fill the row, but don't
20404 consume the TAB. */
20405 if ((row->reversed_p
20406 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20407 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20408 produce_special_glyphs (it, IT_CONTINUATION);
20409 it->continuation_lines_width += it->last_visible_x;
20410 row->ends_in_middle_of_char_p = 1;
20411 row->continued_p = 1;
20412 glyph->pixel_width = it->last_visible_x - x;
20413 it->starts_in_middle_of_char_p = 1;
20414 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20415 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20416 extend_face_to_end_of_line (it);
20417 }
20418 else
20419 {
20420 /* Something other than a TAB that draws past
20421 the right edge of the window. Restore
20422 positions to values before the element. */
20423 if (row->reversed_p)
20424 unproduce_glyphs (it, row->used[TEXT_AREA]
20425 - (n_glyphs_before + i));
20426 row->used[TEXT_AREA] = n_glyphs_before + i;
20427
20428 /* Display continuation glyphs. */
20429 it->current_x = x_before;
20430 it->continuation_lines_width += x;
20431 if (!FRAME_WINDOW_P (it->f)
20432 || (row->reversed_p
20433 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20434 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20435 produce_special_glyphs (it, IT_CONTINUATION);
20436 row->continued_p = 1;
20437
20438 extend_face_to_end_of_line (it);
20439
20440 if (nglyphs > 1 && i > 0)
20441 {
20442 row->ends_in_middle_of_char_p = 1;
20443 it->starts_in_middle_of_char_p = 1;
20444 }
20445
20446 /* Restore the height to what it was before the
20447 element not fitting on the line. */
20448 it->max_ascent = ascent;
20449 it->max_descent = descent;
20450 it->max_phys_ascent = phys_ascent;
20451 it->max_phys_descent = phys_descent;
20452 }
20453
20454 break;
20455 }
20456 else if (new_x > it->first_visible_x)
20457 {
20458 /* Increment number of glyphs actually displayed. */
20459 ++it->hpos;
20460
20461 /* Record the maximum and minimum buffer positions
20462 seen so far in glyphs that will be displayed by
20463 this row. */
20464 if (it->bidi_p)
20465 RECORD_MAX_MIN_POS (it);
20466
20467 if (x < it->first_visible_x && !row->reversed_p)
20468 /* Glyph is partially visible, i.e. row starts at
20469 negative X position. Don't do that in R2L
20470 rows, where we arrange to add a right offset to
20471 the line in extend_face_to_end_of_line, by a
20472 suitable change to the stretch glyph that is
20473 the leftmost glyph of the line. */
20474 row->x = x - it->first_visible_x;
20475 /* When the last glyph of an R2L row only fits
20476 partially on the line, we need to set row->x to a
20477 negative offset, so that the leftmost glyph is
20478 the one that is partially visible. */
20479 if (row->reversed_p && new_x > it->last_visible_x)
20480 row->x = it->last_visible_x - new_x;
20481 }
20482 else
20483 {
20484 /* Glyph is completely off the left margin of the
20485 window. This should not happen because of the
20486 move_it_in_display_line at the start of this
20487 function, unless the text display area of the
20488 window is empty. */
20489 eassert (it->first_visible_x <= it->last_visible_x);
20490 }
20491 }
20492 /* Even if this display element produced no glyphs at all,
20493 we want to record its position. */
20494 if (it->bidi_p && nglyphs == 0)
20495 RECORD_MAX_MIN_POS (it);
20496
20497 row->ascent = max (row->ascent, it->max_ascent);
20498 row->height = max (row->height, it->max_ascent + it->max_descent);
20499 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20500 row->phys_height = max (row->phys_height,
20501 it->max_phys_ascent + it->max_phys_descent);
20502 row->extra_line_spacing = max (row->extra_line_spacing,
20503 it->max_extra_line_spacing);
20504
20505 /* End of this display line if row is continued. */
20506 if (row->continued_p || row->ends_at_zv_p)
20507 break;
20508 }
20509
20510 at_end_of_line:
20511 /* Is this a line end? If yes, we're also done, after making
20512 sure that a non-default face is extended up to the right
20513 margin of the window. */
20514 if (ITERATOR_AT_END_OF_LINE_P (it))
20515 {
20516 int used_before = row->used[TEXT_AREA];
20517
20518 row->ends_in_newline_from_string_p = STRINGP (it->object);
20519
20520 /* Add a space at the end of the line that is used to
20521 display the cursor there. */
20522 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20523 append_space_for_newline (it, 0);
20524
20525 /* Extend the face to the end of the line. */
20526 extend_face_to_end_of_line (it);
20527
20528 /* Make sure we have the position. */
20529 if (used_before == 0)
20530 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20531
20532 /* Record the position of the newline, for use in
20533 find_row_edges. */
20534 it->eol_pos = it->current.pos;
20535
20536 /* Consume the line end. This skips over invisible lines. */
20537 set_iterator_to_next (it, 1);
20538 it->continuation_lines_width = 0;
20539 break;
20540 }
20541
20542 /* Proceed with next display element. Note that this skips
20543 over lines invisible because of selective display. */
20544 set_iterator_to_next (it, 1);
20545
20546 /* If we truncate lines, we are done when the last displayed
20547 glyphs reach past the right margin of the window. */
20548 if (it->line_wrap == TRUNCATE
20549 && ((FRAME_WINDOW_P (it->f)
20550 /* Images are preprocessed in produce_image_glyph such
20551 that they are cropped at the right edge of the
20552 window, so an image glyph will always end exactly at
20553 last_visible_x, even if there's no right fringe. */
20554 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20555 ? (it->current_x >= it->last_visible_x)
20556 : (it->current_x > it->last_visible_x)))
20557 {
20558 /* Maybe add truncation glyphs. */
20559 if (!FRAME_WINDOW_P (it->f)
20560 || (row->reversed_p
20561 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20562 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20563 {
20564 int i, n;
20565
20566 if (!row->reversed_p)
20567 {
20568 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20569 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20570 break;
20571 }
20572 else
20573 {
20574 for (i = 0; i < row->used[TEXT_AREA]; i++)
20575 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20576 break;
20577 /* Remove any padding glyphs at the front of ROW, to
20578 make room for the truncation glyphs we will be
20579 adding below. The loop below always inserts at
20580 least one truncation glyph, so also remove the
20581 last glyph added to ROW. */
20582 unproduce_glyphs (it, i + 1);
20583 /* Adjust i for the loop below. */
20584 i = row->used[TEXT_AREA] - (i + 1);
20585 }
20586
20587 /* produce_special_glyphs overwrites the last glyph, so
20588 we don't want that if we want to keep that last
20589 glyph, which means it's an image. */
20590 if (it->current_x > it->last_visible_x)
20591 {
20592 it->current_x = x_before;
20593 if (!FRAME_WINDOW_P (it->f))
20594 {
20595 for (n = row->used[TEXT_AREA]; i < n; ++i)
20596 {
20597 row->used[TEXT_AREA] = i;
20598 produce_special_glyphs (it, IT_TRUNCATION);
20599 }
20600 }
20601 else
20602 {
20603 row->used[TEXT_AREA] = i;
20604 produce_special_glyphs (it, IT_TRUNCATION);
20605 }
20606 it->hpos = hpos_before;
20607 }
20608 }
20609 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20610 {
20611 /* Don't truncate if we can overflow newline into fringe. */
20612 if (!get_next_display_element (it))
20613 {
20614 it->continuation_lines_width = 0;
20615 row->ends_at_zv_p = 1;
20616 row->exact_window_width_line_p = 1;
20617 break;
20618 }
20619 if (ITERATOR_AT_END_OF_LINE_P (it))
20620 {
20621 row->exact_window_width_line_p = 1;
20622 goto at_end_of_line;
20623 }
20624 it->current_x = x_before;
20625 it->hpos = hpos_before;
20626 }
20627
20628 row->truncated_on_right_p = 1;
20629 it->continuation_lines_width = 0;
20630 reseat_at_next_visible_line_start (it, 0);
20631 /* We insist below that IT's position be at ZV because in
20632 bidi-reordered lines the character at visible line start
20633 might not be the character that follows the newline in
20634 the logical order. */
20635 if (IT_BYTEPOS (*it) > BEG_BYTE)
20636 row->ends_at_zv_p =
20637 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20638 else
20639 row->ends_at_zv_p = false;
20640 break;
20641 }
20642 }
20643
20644 if (wrap_data)
20645 bidi_unshelve_cache (wrap_data, 1);
20646
20647 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20648 at the left window margin. */
20649 if (it->first_visible_x
20650 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20651 {
20652 if (!FRAME_WINDOW_P (it->f)
20653 || (((row->reversed_p
20654 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20655 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20656 /* Don't let insert_left_trunc_glyphs overwrite the
20657 first glyph of the row if it is an image. */
20658 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20659 insert_left_trunc_glyphs (it);
20660 row->truncated_on_left_p = 1;
20661 }
20662
20663 /* Remember the position at which this line ends.
20664
20665 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20666 cannot be before the call to find_row_edges below, since that is
20667 where these positions are determined. */
20668 row->end = it->current;
20669 if (!it->bidi_p)
20670 {
20671 row->minpos = row->start.pos;
20672 row->maxpos = row->end.pos;
20673 }
20674 else
20675 {
20676 /* ROW->minpos and ROW->maxpos must be the smallest and
20677 `1 + the largest' buffer positions in ROW. But if ROW was
20678 bidi-reordered, these two positions can be anywhere in the
20679 row, so we must determine them now. */
20680 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20681 }
20682
20683 /* If the start of this line is the overlay arrow-position, then
20684 mark this glyph row as the one containing the overlay arrow.
20685 This is clearly a mess with variable size fonts. It would be
20686 better to let it be displayed like cursors under X. */
20687 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20688 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20689 !NILP (overlay_arrow_string)))
20690 {
20691 /* Overlay arrow in window redisplay is a fringe bitmap. */
20692 if (STRINGP (overlay_arrow_string))
20693 {
20694 struct glyph_row *arrow_row
20695 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20696 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20697 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20698 struct glyph *p = row->glyphs[TEXT_AREA];
20699 struct glyph *p2, *end;
20700
20701 /* Copy the arrow glyphs. */
20702 while (glyph < arrow_end)
20703 *p++ = *glyph++;
20704
20705 /* Throw away padding glyphs. */
20706 p2 = p;
20707 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20708 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20709 ++p2;
20710 if (p2 > p)
20711 {
20712 while (p2 < end)
20713 *p++ = *p2++;
20714 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20715 }
20716 }
20717 else
20718 {
20719 eassert (INTEGERP (overlay_arrow_string));
20720 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20721 }
20722 overlay_arrow_seen = 1;
20723 }
20724
20725 /* Highlight trailing whitespace. */
20726 if (!NILP (Vshow_trailing_whitespace))
20727 highlight_trailing_whitespace (it->f, it->glyph_row);
20728
20729 /* Compute pixel dimensions of this line. */
20730 compute_line_metrics (it);
20731
20732 /* Implementation note: No changes in the glyphs of ROW or in their
20733 faces can be done past this point, because compute_line_metrics
20734 computes ROW's hash value and stores it within the glyph_row
20735 structure. */
20736
20737 /* Record whether this row ends inside an ellipsis. */
20738 row->ends_in_ellipsis_p
20739 = (it->method == GET_FROM_DISPLAY_VECTOR
20740 && it->ellipsis_p);
20741
20742 /* Save fringe bitmaps in this row. */
20743 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20744 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20745 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20746 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20747
20748 it->left_user_fringe_bitmap = 0;
20749 it->left_user_fringe_face_id = 0;
20750 it->right_user_fringe_bitmap = 0;
20751 it->right_user_fringe_face_id = 0;
20752
20753 /* Maybe set the cursor. */
20754 cvpos = it->w->cursor.vpos;
20755 if ((cvpos < 0
20756 /* In bidi-reordered rows, keep checking for proper cursor
20757 position even if one has been found already, because buffer
20758 positions in such rows change non-linearly with ROW->VPOS,
20759 when a line is continued. One exception: when we are at ZV,
20760 display cursor on the first suitable glyph row, since all
20761 the empty rows after that also have their position set to ZV. */
20762 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20763 lines' rows is implemented for bidi-reordered rows. */
20764 || (it->bidi_p
20765 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20766 && PT >= MATRIX_ROW_START_CHARPOS (row)
20767 && PT <= MATRIX_ROW_END_CHARPOS (row)
20768 && cursor_row_p (row))
20769 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20770
20771 /* Prepare for the next line. This line starts horizontally at (X
20772 HPOS) = (0 0). Vertical positions are incremented. As a
20773 convenience for the caller, IT->glyph_row is set to the next
20774 row to be used. */
20775 it->current_x = it->hpos = 0;
20776 it->current_y += row->height;
20777 SET_TEXT_POS (it->eol_pos, 0, 0);
20778 ++it->vpos;
20779 ++it->glyph_row;
20780 /* The next row should by default use the same value of the
20781 reversed_p flag as this one. set_iterator_to_next decides when
20782 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20783 the flag accordingly. */
20784 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20785 it->glyph_row->reversed_p = row->reversed_p;
20786 it->start = row->end;
20787 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20788
20789 #undef RECORD_MAX_MIN_POS
20790 }
20791
20792 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20793 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20794 doc: /* Return paragraph direction at point in BUFFER.
20795 Value is either `left-to-right' or `right-to-left'.
20796 If BUFFER is omitted or nil, it defaults to the current buffer.
20797
20798 Paragraph direction determines how the text in the paragraph is displayed.
20799 In left-to-right paragraphs, text begins at the left margin of the window
20800 and the reading direction is generally left to right. In right-to-left
20801 paragraphs, text begins at the right margin and is read from right to left.
20802
20803 See also `bidi-paragraph-direction'. */)
20804 (Lisp_Object buffer)
20805 {
20806 struct buffer *buf = current_buffer;
20807 struct buffer *old = buf;
20808
20809 if (! NILP (buffer))
20810 {
20811 CHECK_BUFFER (buffer);
20812 buf = XBUFFER (buffer);
20813 }
20814
20815 if (NILP (BVAR (buf, bidi_display_reordering))
20816 || NILP (BVAR (buf, enable_multibyte_characters))
20817 /* When we are loading loadup.el, the character property tables
20818 needed for bidi iteration are not yet available. */
20819 || !NILP (Vpurify_flag))
20820 return Qleft_to_right;
20821 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20822 return BVAR (buf, bidi_paragraph_direction);
20823 else
20824 {
20825 /* Determine the direction from buffer text. We could try to
20826 use current_matrix if it is up to date, but this seems fast
20827 enough as it is. */
20828 struct bidi_it itb;
20829 ptrdiff_t pos = BUF_PT (buf);
20830 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20831 int c;
20832 void *itb_data = bidi_shelve_cache ();
20833
20834 set_buffer_temp (buf);
20835 /* bidi_paragraph_init finds the base direction of the paragraph
20836 by searching forward from paragraph start. We need the base
20837 direction of the current or _previous_ paragraph, so we need
20838 to make sure we are within that paragraph. To that end, find
20839 the previous non-empty line. */
20840 if (pos >= ZV && pos > BEGV)
20841 DEC_BOTH (pos, bytepos);
20842 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20843 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20844 {
20845 while ((c = FETCH_BYTE (bytepos)) == '\n'
20846 || c == ' ' || c == '\t' || c == '\f')
20847 {
20848 if (bytepos <= BEGV_BYTE)
20849 break;
20850 bytepos--;
20851 pos--;
20852 }
20853 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20854 bytepos--;
20855 }
20856 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20857 itb.paragraph_dir = NEUTRAL_DIR;
20858 itb.string.s = NULL;
20859 itb.string.lstring = Qnil;
20860 itb.string.bufpos = 0;
20861 itb.string.from_disp_str = 0;
20862 itb.string.unibyte = 0;
20863 /* We have no window to use here for ignoring window-specific
20864 overlays. Using NULL for window pointer will cause
20865 compute_display_string_pos to use the current buffer. */
20866 itb.w = NULL;
20867 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20868 bidi_unshelve_cache (itb_data, 0);
20869 set_buffer_temp (old);
20870 switch (itb.paragraph_dir)
20871 {
20872 case L2R:
20873 return Qleft_to_right;
20874 break;
20875 case R2L:
20876 return Qright_to_left;
20877 break;
20878 default:
20879 emacs_abort ();
20880 }
20881 }
20882 }
20883
20884 DEFUN ("move-point-visually", Fmove_point_visually,
20885 Smove_point_visually, 1, 1, 0,
20886 doc: /* Move point in the visual order in the specified DIRECTION.
20887 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20888 left.
20889
20890 Value is the new character position of point. */)
20891 (Lisp_Object direction)
20892 {
20893 struct window *w = XWINDOW (selected_window);
20894 struct buffer *b = XBUFFER (w->contents);
20895 struct glyph_row *row;
20896 int dir;
20897 Lisp_Object paragraph_dir;
20898
20899 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20900 (!(ROW)->continued_p \
20901 && INTEGERP ((GLYPH)->object) \
20902 && (GLYPH)->type == CHAR_GLYPH \
20903 && (GLYPH)->u.ch == ' ' \
20904 && (GLYPH)->charpos >= 0 \
20905 && !(GLYPH)->avoid_cursor_p)
20906
20907 CHECK_NUMBER (direction);
20908 dir = XINT (direction);
20909 if (dir > 0)
20910 dir = 1;
20911 else
20912 dir = -1;
20913
20914 /* If current matrix is up-to-date, we can use the information
20915 recorded in the glyphs, at least as long as the goal is on the
20916 screen. */
20917 if (w->window_end_valid
20918 && !windows_or_buffers_changed
20919 && b
20920 && !b->clip_changed
20921 && !b->prevent_redisplay_optimizations_p
20922 && !window_outdated (w)
20923 /* We rely below on the cursor coordinates to be up to date, but
20924 we cannot trust them if some command moved point since the
20925 last complete redisplay. */
20926 && w->last_point == BUF_PT (b)
20927 && w->cursor.vpos >= 0
20928 && w->cursor.vpos < w->current_matrix->nrows
20929 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20930 {
20931 struct glyph *g = row->glyphs[TEXT_AREA];
20932 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20933 struct glyph *gpt = g + w->cursor.hpos;
20934
20935 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20936 {
20937 if (BUFFERP (g->object) && g->charpos != PT)
20938 {
20939 SET_PT (g->charpos);
20940 w->cursor.vpos = -1;
20941 return make_number (PT);
20942 }
20943 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20944 {
20945 ptrdiff_t new_pos;
20946
20947 if (BUFFERP (gpt->object))
20948 {
20949 new_pos = PT;
20950 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20951 new_pos += (row->reversed_p ? -dir : dir);
20952 else
20953 new_pos -= (row->reversed_p ? -dir : dir);;
20954 }
20955 else if (BUFFERP (g->object))
20956 new_pos = g->charpos;
20957 else
20958 break;
20959 SET_PT (new_pos);
20960 w->cursor.vpos = -1;
20961 return make_number (PT);
20962 }
20963 else if (ROW_GLYPH_NEWLINE_P (row, g))
20964 {
20965 /* Glyphs inserted at the end of a non-empty line for
20966 positioning the cursor have zero charpos, so we must
20967 deduce the value of point by other means. */
20968 if (g->charpos > 0)
20969 SET_PT (g->charpos);
20970 else if (row->ends_at_zv_p && PT != ZV)
20971 SET_PT (ZV);
20972 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20973 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20974 else
20975 break;
20976 w->cursor.vpos = -1;
20977 return make_number (PT);
20978 }
20979 }
20980 if (g == e || INTEGERP (g->object))
20981 {
20982 if (row->truncated_on_left_p || row->truncated_on_right_p)
20983 goto simulate_display;
20984 if (!row->reversed_p)
20985 row += dir;
20986 else
20987 row -= dir;
20988 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20989 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20990 goto simulate_display;
20991
20992 if (dir > 0)
20993 {
20994 if (row->reversed_p && !row->continued_p)
20995 {
20996 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20997 w->cursor.vpos = -1;
20998 return make_number (PT);
20999 }
21000 g = row->glyphs[TEXT_AREA];
21001 e = g + row->used[TEXT_AREA];
21002 for ( ; g < e; g++)
21003 {
21004 if (BUFFERP (g->object)
21005 /* Empty lines have only one glyph, which stands
21006 for the newline, and whose charpos is the
21007 buffer position of the newline. */
21008 || ROW_GLYPH_NEWLINE_P (row, g)
21009 /* When the buffer ends in a newline, the line at
21010 EOB also has one glyph, but its charpos is -1. */
21011 || (row->ends_at_zv_p
21012 && !row->reversed_p
21013 && INTEGERP (g->object)
21014 && g->type == CHAR_GLYPH
21015 && g->u.ch == ' '))
21016 {
21017 if (g->charpos > 0)
21018 SET_PT (g->charpos);
21019 else if (!row->reversed_p
21020 && row->ends_at_zv_p
21021 && PT != ZV)
21022 SET_PT (ZV);
21023 else
21024 continue;
21025 w->cursor.vpos = -1;
21026 return make_number (PT);
21027 }
21028 }
21029 }
21030 else
21031 {
21032 if (!row->reversed_p && !row->continued_p)
21033 {
21034 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21035 w->cursor.vpos = -1;
21036 return make_number (PT);
21037 }
21038 e = row->glyphs[TEXT_AREA];
21039 g = e + row->used[TEXT_AREA] - 1;
21040 for ( ; g >= e; g--)
21041 {
21042 if (BUFFERP (g->object)
21043 || (ROW_GLYPH_NEWLINE_P (row, g)
21044 && g->charpos > 0)
21045 /* Empty R2L lines on GUI frames have the buffer
21046 position of the newline stored in the stretch
21047 glyph. */
21048 || g->type == STRETCH_GLYPH
21049 || (row->ends_at_zv_p
21050 && row->reversed_p
21051 && INTEGERP (g->object)
21052 && g->type == CHAR_GLYPH
21053 && g->u.ch == ' '))
21054 {
21055 if (g->charpos > 0)
21056 SET_PT (g->charpos);
21057 else if (row->reversed_p
21058 && row->ends_at_zv_p
21059 && PT != ZV)
21060 SET_PT (ZV);
21061 else
21062 continue;
21063 w->cursor.vpos = -1;
21064 return make_number (PT);
21065 }
21066 }
21067 }
21068 }
21069 }
21070
21071 simulate_display:
21072
21073 /* If we wind up here, we failed to move by using the glyphs, so we
21074 need to simulate display instead. */
21075
21076 if (b)
21077 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21078 else
21079 paragraph_dir = Qleft_to_right;
21080 if (EQ (paragraph_dir, Qright_to_left))
21081 dir = -dir;
21082 if (PT <= BEGV && dir < 0)
21083 xsignal0 (Qbeginning_of_buffer);
21084 else if (PT >= ZV && dir > 0)
21085 xsignal0 (Qend_of_buffer);
21086 else
21087 {
21088 struct text_pos pt;
21089 struct it it;
21090 int pt_x, target_x, pixel_width, pt_vpos;
21091 bool at_eol_p;
21092 bool overshoot_expected = false;
21093 bool target_is_eol_p = false;
21094
21095 /* Setup the arena. */
21096 SET_TEXT_POS (pt, PT, PT_BYTE);
21097 start_display (&it, w, pt);
21098
21099 if (it.cmp_it.id < 0
21100 && it.method == GET_FROM_STRING
21101 && it.area == TEXT_AREA
21102 && it.string_from_display_prop_p
21103 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21104 overshoot_expected = true;
21105
21106 /* Find the X coordinate of point. We start from the beginning
21107 of this or previous line to make sure we are before point in
21108 the logical order (since the move_it_* functions can only
21109 move forward). */
21110 reseat:
21111 reseat_at_previous_visible_line_start (&it);
21112 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21113 if (IT_CHARPOS (it) != PT)
21114 {
21115 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21116 -1, -1, -1, MOVE_TO_POS);
21117 /* If we missed point because the character there is
21118 displayed out of a display vector that has more than one
21119 glyph, retry expecting overshoot. */
21120 if (it.method == GET_FROM_DISPLAY_VECTOR
21121 && it.current.dpvec_index > 0
21122 && !overshoot_expected)
21123 {
21124 overshoot_expected = true;
21125 goto reseat;
21126 }
21127 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21128 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21129 }
21130 pt_x = it.current_x;
21131 pt_vpos = it.vpos;
21132 if (dir > 0 || overshoot_expected)
21133 {
21134 struct glyph_row *row = it.glyph_row;
21135
21136 /* When point is at beginning of line, we don't have
21137 information about the glyph there loaded into struct
21138 it. Calling get_next_display_element fixes that. */
21139 if (pt_x == 0)
21140 get_next_display_element (&it);
21141 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21142 it.glyph_row = NULL;
21143 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21144 it.glyph_row = row;
21145 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21146 it, lest it will become out of sync with it's buffer
21147 position. */
21148 it.current_x = pt_x;
21149 }
21150 else
21151 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21152 pixel_width = it.pixel_width;
21153 if (overshoot_expected && at_eol_p)
21154 pixel_width = 0;
21155 else if (pixel_width <= 0)
21156 pixel_width = 1;
21157
21158 /* If there's a display string (or something similar) at point,
21159 we are actually at the glyph to the left of point, so we need
21160 to correct the X coordinate. */
21161 if (overshoot_expected)
21162 {
21163 if (it.bidi_p)
21164 pt_x += pixel_width * it.bidi_it.scan_dir;
21165 else
21166 pt_x += pixel_width;
21167 }
21168
21169 /* Compute target X coordinate, either to the left or to the
21170 right of point. On TTY frames, all characters have the same
21171 pixel width of 1, so we can use that. On GUI frames we don't
21172 have an easy way of getting at the pixel width of the
21173 character to the left of point, so we use a different method
21174 of getting to that place. */
21175 if (dir > 0)
21176 target_x = pt_x + pixel_width;
21177 else
21178 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21179
21180 /* Target X coordinate could be one line above or below the line
21181 of point, in which case we need to adjust the target X
21182 coordinate. Also, if moving to the left, we need to begin at
21183 the left edge of the point's screen line. */
21184 if (dir < 0)
21185 {
21186 if (pt_x > 0)
21187 {
21188 start_display (&it, w, pt);
21189 reseat_at_previous_visible_line_start (&it);
21190 it.current_x = it.current_y = it.hpos = 0;
21191 if (pt_vpos != 0)
21192 move_it_by_lines (&it, pt_vpos);
21193 }
21194 else
21195 {
21196 move_it_by_lines (&it, -1);
21197 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21198 target_is_eol_p = true;
21199 /* Under word-wrap, we don't know the x coordinate of
21200 the last character displayed on the previous line,
21201 which immediately precedes the wrap point. To find
21202 out its x coordinate, we try moving to the right
21203 margin of the window, which will stop at the wrap
21204 point, and then reset target_x to point at the
21205 character that precedes the wrap point. This is not
21206 needed on GUI frames, because (see below) there we
21207 move from the left margin one grapheme cluster at a
21208 time, and stop when we hit the wrap point. */
21209 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21210 {
21211 void *it_data = NULL;
21212 struct it it2;
21213
21214 SAVE_IT (it2, it, it_data);
21215 move_it_in_display_line_to (&it, ZV, target_x,
21216 MOVE_TO_POS | MOVE_TO_X);
21217 /* If we arrived at target_x, that _is_ the last
21218 character on the previous line. */
21219 if (it.current_x != target_x)
21220 target_x = it.current_x - 1;
21221 RESTORE_IT (&it, &it2, it_data);
21222 }
21223 }
21224 }
21225 else
21226 {
21227 if (at_eol_p
21228 || (target_x >= it.last_visible_x
21229 && it.line_wrap != TRUNCATE))
21230 {
21231 if (pt_x > 0)
21232 move_it_by_lines (&it, 0);
21233 move_it_by_lines (&it, 1);
21234 target_x = 0;
21235 }
21236 }
21237
21238 /* Move to the target X coordinate. */
21239 #ifdef HAVE_WINDOW_SYSTEM
21240 /* On GUI frames, as we don't know the X coordinate of the
21241 character to the left of point, moving point to the left
21242 requires walking, one grapheme cluster at a time, until we
21243 find ourself at a place immediately to the left of the
21244 character at point. */
21245 if (FRAME_WINDOW_P (it.f) && dir < 0)
21246 {
21247 struct text_pos new_pos;
21248 enum move_it_result rc = MOVE_X_REACHED;
21249
21250 if (it.current_x == 0)
21251 get_next_display_element (&it);
21252 if (it.what == IT_COMPOSITION)
21253 {
21254 new_pos.charpos = it.cmp_it.charpos;
21255 new_pos.bytepos = -1;
21256 }
21257 else
21258 new_pos = it.current.pos;
21259
21260 while (it.current_x + it.pixel_width <= target_x
21261 && (rc == MOVE_X_REACHED
21262 /* Under word-wrap, move_it_in_display_line_to
21263 stops at correct coordinates, but sometimes
21264 returns MOVE_POS_MATCH_OR_ZV. */
21265 || (it.line_wrap == WORD_WRAP
21266 && rc == MOVE_POS_MATCH_OR_ZV)))
21267 {
21268 int new_x = it.current_x + it.pixel_width;
21269
21270 /* For composed characters, we want the position of the
21271 first character in the grapheme cluster (usually, the
21272 composition's base character), whereas it.current
21273 might give us the position of the _last_ one, e.g. if
21274 the composition is rendered in reverse due to bidi
21275 reordering. */
21276 if (it.what == IT_COMPOSITION)
21277 {
21278 new_pos.charpos = it.cmp_it.charpos;
21279 new_pos.bytepos = -1;
21280 }
21281 else
21282 new_pos = it.current.pos;
21283 if (new_x == it.current_x)
21284 new_x++;
21285 rc = move_it_in_display_line_to (&it, ZV, new_x,
21286 MOVE_TO_POS | MOVE_TO_X);
21287 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21288 break;
21289 }
21290 /* The previous position we saw in the loop is the one we
21291 want. */
21292 if (new_pos.bytepos == -1)
21293 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21294 it.current.pos = new_pos;
21295 }
21296 else
21297 #endif
21298 if (it.current_x != target_x)
21299 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21300
21301 /* When lines are truncated, the above loop will stop at the
21302 window edge. But we want to get to the end of line, even if
21303 it is beyond the window edge; automatic hscroll will then
21304 scroll the window to show point as appropriate. */
21305 if (target_is_eol_p && it.line_wrap == TRUNCATE
21306 && get_next_display_element (&it))
21307 {
21308 struct text_pos new_pos = it.current.pos;
21309
21310 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21311 {
21312 set_iterator_to_next (&it, 0);
21313 if (it.method == GET_FROM_BUFFER)
21314 new_pos = it.current.pos;
21315 if (!get_next_display_element (&it))
21316 break;
21317 }
21318
21319 it.current.pos = new_pos;
21320 }
21321
21322 /* If we ended up in a display string that covers point, move to
21323 buffer position to the right in the visual order. */
21324 if (dir > 0)
21325 {
21326 while (IT_CHARPOS (it) == PT)
21327 {
21328 set_iterator_to_next (&it, 0);
21329 if (!get_next_display_element (&it))
21330 break;
21331 }
21332 }
21333
21334 /* Move point to that position. */
21335 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21336 }
21337
21338 return make_number (PT);
21339
21340 #undef ROW_GLYPH_NEWLINE_P
21341 }
21342
21343 \f
21344 /***********************************************************************
21345 Menu Bar
21346 ***********************************************************************/
21347
21348 /* Redisplay the menu bar in the frame for window W.
21349
21350 The menu bar of X frames that don't have X toolkit support is
21351 displayed in a special window W->frame->menu_bar_window.
21352
21353 The menu bar of terminal frames is treated specially as far as
21354 glyph matrices are concerned. Menu bar lines are not part of
21355 windows, so the update is done directly on the frame matrix rows
21356 for the menu bar. */
21357
21358 static void
21359 display_menu_bar (struct window *w)
21360 {
21361 struct frame *f = XFRAME (WINDOW_FRAME (w));
21362 struct it it;
21363 Lisp_Object items;
21364 int i;
21365
21366 /* Don't do all this for graphical frames. */
21367 #ifdef HAVE_NTGUI
21368 if (FRAME_W32_P (f))
21369 return;
21370 #endif
21371 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21372 if (FRAME_X_P (f))
21373 return;
21374 #endif
21375
21376 #ifdef HAVE_NS
21377 if (FRAME_NS_P (f))
21378 return;
21379 #endif /* HAVE_NS */
21380
21381 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21382 eassert (!FRAME_WINDOW_P (f));
21383 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21384 it.first_visible_x = 0;
21385 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21386 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21387 if (FRAME_WINDOW_P (f))
21388 {
21389 /* Menu bar lines are displayed in the desired matrix of the
21390 dummy window menu_bar_window. */
21391 struct window *menu_w;
21392 menu_w = XWINDOW (f->menu_bar_window);
21393 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21394 MENU_FACE_ID);
21395 it.first_visible_x = 0;
21396 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21397 }
21398 else
21399 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21400 {
21401 /* This is a TTY frame, i.e. character hpos/vpos are used as
21402 pixel x/y. */
21403 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21404 MENU_FACE_ID);
21405 it.first_visible_x = 0;
21406 it.last_visible_x = FRAME_COLS (f);
21407 }
21408
21409 /* FIXME: This should be controlled by a user option. See the
21410 comments in redisplay_tool_bar and display_mode_line about
21411 this. */
21412 it.paragraph_embedding = L2R;
21413
21414 /* Clear all rows of the menu bar. */
21415 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21416 {
21417 struct glyph_row *row = it.glyph_row + i;
21418 clear_glyph_row (row);
21419 row->enabled_p = true;
21420 row->full_width_p = 1;
21421 }
21422
21423 /* Display all items of the menu bar. */
21424 items = FRAME_MENU_BAR_ITEMS (it.f);
21425 for (i = 0; i < ASIZE (items); i += 4)
21426 {
21427 Lisp_Object string;
21428
21429 /* Stop at nil string. */
21430 string = AREF (items, i + 1);
21431 if (NILP (string))
21432 break;
21433
21434 /* Remember where item was displayed. */
21435 ASET (items, i + 3, make_number (it.hpos));
21436
21437 /* Display the item, pad with one space. */
21438 if (it.current_x < it.last_visible_x)
21439 display_string (NULL, string, Qnil, 0, 0, &it,
21440 SCHARS (string) + 1, 0, 0, -1);
21441 }
21442
21443 /* Fill out the line with spaces. */
21444 if (it.current_x < it.last_visible_x)
21445 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21446
21447 /* Compute the total height of the lines. */
21448 compute_line_metrics (&it);
21449 }
21450
21451 /* Deep copy of a glyph row, including the glyphs. */
21452 static void
21453 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21454 {
21455 struct glyph *pointers[1 + LAST_AREA];
21456 int to_used = to->used[TEXT_AREA];
21457
21458 /* Save glyph pointers of TO. */
21459 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21460
21461 /* Do a structure assignment. */
21462 *to = *from;
21463
21464 /* Restore original glyph pointers of TO. */
21465 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21466
21467 /* Copy the glyphs. */
21468 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21469 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21470
21471 /* If we filled only part of the TO row, fill the rest with
21472 space_glyph (which will display as empty space). */
21473 if (to_used > from->used[TEXT_AREA])
21474 fill_up_frame_row_with_spaces (to, to_used);
21475 }
21476
21477 /* Display one menu item on a TTY, by overwriting the glyphs in the
21478 frame F's desired glyph matrix with glyphs produced from the menu
21479 item text. Called from term.c to display TTY drop-down menus one
21480 item at a time.
21481
21482 ITEM_TEXT is the menu item text as a C string.
21483
21484 FACE_ID is the face ID to be used for this menu item. FACE_ID
21485 could specify one of 3 faces: a face for an enabled item, a face
21486 for a disabled item, or a face for a selected item.
21487
21488 X and Y are coordinates of the first glyph in the frame's desired
21489 matrix to be overwritten by the menu item. Since this is a TTY, Y
21490 is the zero-based number of the glyph row and X is the zero-based
21491 glyph number in the row, starting from left, where to start
21492 displaying the item.
21493
21494 SUBMENU non-zero means this menu item drops down a submenu, which
21495 should be indicated by displaying a proper visual cue after the
21496 item text. */
21497
21498 void
21499 display_tty_menu_item (const char *item_text, int width, int face_id,
21500 int x, int y, int submenu)
21501 {
21502 struct it it;
21503 struct frame *f = SELECTED_FRAME ();
21504 struct window *w = XWINDOW (f->selected_window);
21505 int saved_used, saved_truncated, saved_width, saved_reversed;
21506 struct glyph_row *row;
21507 size_t item_len = strlen (item_text);
21508
21509 eassert (FRAME_TERMCAP_P (f));
21510
21511 /* Don't write beyond the matrix's last row. This can happen for
21512 TTY screens that are not high enough to show the entire menu.
21513 (This is actually a bit of defensive programming, as
21514 tty_menu_display already limits the number of menu items to one
21515 less than the number of screen lines.) */
21516 if (y >= f->desired_matrix->nrows)
21517 return;
21518
21519 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21520 it.first_visible_x = 0;
21521 it.last_visible_x = FRAME_COLS (f) - 1;
21522 row = it.glyph_row;
21523 /* Start with the row contents from the current matrix. */
21524 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21525 saved_width = row->full_width_p;
21526 row->full_width_p = 1;
21527 saved_reversed = row->reversed_p;
21528 row->reversed_p = 0;
21529 row->enabled_p = true;
21530
21531 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21532 desired face. */
21533 eassert (x < f->desired_matrix->matrix_w);
21534 it.current_x = it.hpos = x;
21535 it.current_y = it.vpos = y;
21536 saved_used = row->used[TEXT_AREA];
21537 saved_truncated = row->truncated_on_right_p;
21538 row->used[TEXT_AREA] = x;
21539 it.face_id = face_id;
21540 it.line_wrap = TRUNCATE;
21541
21542 /* FIXME: This should be controlled by a user option. See the
21543 comments in redisplay_tool_bar and display_mode_line about this.
21544 Also, if paragraph_embedding could ever be R2L, changes will be
21545 needed to avoid shifting to the right the row characters in
21546 term.c:append_glyph. */
21547 it.paragraph_embedding = L2R;
21548
21549 /* Pad with a space on the left. */
21550 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21551 width--;
21552 /* Display the menu item, pad with spaces to WIDTH. */
21553 if (submenu)
21554 {
21555 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21556 item_len, 0, FRAME_COLS (f) - 1, -1);
21557 width -= item_len;
21558 /* Indicate with " >" that there's a submenu. */
21559 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21560 FRAME_COLS (f) - 1, -1);
21561 }
21562 else
21563 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21564 width, 0, FRAME_COLS (f) - 1, -1);
21565
21566 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21567 row->truncated_on_right_p = saved_truncated;
21568 row->hash = row_hash (row);
21569 row->full_width_p = saved_width;
21570 row->reversed_p = saved_reversed;
21571 }
21572 \f
21573 /***********************************************************************
21574 Mode Line
21575 ***********************************************************************/
21576
21577 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21578 FORCE is non-zero, redisplay mode lines unconditionally.
21579 Otherwise, redisplay only mode lines that are garbaged. Value is
21580 the number of windows whose mode lines were redisplayed. */
21581
21582 static int
21583 redisplay_mode_lines (Lisp_Object window, bool force)
21584 {
21585 int nwindows = 0;
21586
21587 while (!NILP (window))
21588 {
21589 struct window *w = XWINDOW (window);
21590
21591 if (WINDOWP (w->contents))
21592 nwindows += redisplay_mode_lines (w->contents, force);
21593 else if (force
21594 || FRAME_GARBAGED_P (XFRAME (w->frame))
21595 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21596 {
21597 struct text_pos lpoint;
21598 struct buffer *old = current_buffer;
21599
21600 /* Set the window's buffer for the mode line display. */
21601 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21602 set_buffer_internal_1 (XBUFFER (w->contents));
21603
21604 /* Point refers normally to the selected window. For any
21605 other window, set up appropriate value. */
21606 if (!EQ (window, selected_window))
21607 {
21608 struct text_pos pt;
21609
21610 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21611 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21612 }
21613
21614 /* Display mode lines. */
21615 clear_glyph_matrix (w->desired_matrix);
21616 if (display_mode_lines (w))
21617 ++nwindows;
21618
21619 /* Restore old settings. */
21620 set_buffer_internal_1 (old);
21621 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21622 }
21623
21624 window = w->next;
21625 }
21626
21627 return nwindows;
21628 }
21629
21630
21631 /* Display the mode and/or header line of window W. Value is the
21632 sum number of mode lines and header lines displayed. */
21633
21634 static int
21635 display_mode_lines (struct window *w)
21636 {
21637 Lisp_Object old_selected_window = selected_window;
21638 Lisp_Object old_selected_frame = selected_frame;
21639 Lisp_Object new_frame = w->frame;
21640 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21641 int n = 0;
21642
21643 selected_frame = new_frame;
21644 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21645 or window's point, then we'd need select_window_1 here as well. */
21646 XSETWINDOW (selected_window, w);
21647 XFRAME (new_frame)->selected_window = selected_window;
21648
21649 /* These will be set while the mode line specs are processed. */
21650 line_number_displayed = 0;
21651 w->column_number_displayed = -1;
21652
21653 if (WINDOW_WANTS_MODELINE_P (w))
21654 {
21655 struct window *sel_w = XWINDOW (old_selected_window);
21656
21657 /* Select mode line face based on the real selected window. */
21658 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21659 BVAR (current_buffer, mode_line_format));
21660 ++n;
21661 }
21662
21663 if (WINDOW_WANTS_HEADER_LINE_P (w))
21664 {
21665 display_mode_line (w, HEADER_LINE_FACE_ID,
21666 BVAR (current_buffer, header_line_format));
21667 ++n;
21668 }
21669
21670 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21671 selected_frame = old_selected_frame;
21672 selected_window = old_selected_window;
21673 if (n > 0)
21674 w->must_be_updated_p = true;
21675 return n;
21676 }
21677
21678
21679 /* Display mode or header line of window W. FACE_ID specifies which
21680 line to display; it is either MODE_LINE_FACE_ID or
21681 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21682 display. Value is the pixel height of the mode/header line
21683 displayed. */
21684
21685 static int
21686 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21687 {
21688 struct it it;
21689 struct face *face;
21690 ptrdiff_t count = SPECPDL_INDEX ();
21691
21692 init_iterator (&it, w, -1, -1, NULL, face_id);
21693 /* Don't extend on a previously drawn mode-line.
21694 This may happen if called from pos_visible_p. */
21695 it.glyph_row->enabled_p = false;
21696 prepare_desired_row (w, it.glyph_row, true);
21697
21698 it.glyph_row->mode_line_p = 1;
21699
21700 /* FIXME: This should be controlled by a user option. But
21701 supporting such an option is not trivial, since the mode line is
21702 made up of many separate strings. */
21703 it.paragraph_embedding = L2R;
21704
21705 record_unwind_protect (unwind_format_mode_line,
21706 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21707
21708 mode_line_target = MODE_LINE_DISPLAY;
21709
21710 /* Temporarily make frame's keyboard the current kboard so that
21711 kboard-local variables in the mode_line_format will get the right
21712 values. */
21713 push_kboard (FRAME_KBOARD (it.f));
21714 record_unwind_save_match_data ();
21715 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21716 pop_kboard ();
21717
21718 unbind_to (count, Qnil);
21719
21720 /* Fill up with spaces. */
21721 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21722
21723 compute_line_metrics (&it);
21724 it.glyph_row->full_width_p = 1;
21725 it.glyph_row->continued_p = 0;
21726 it.glyph_row->truncated_on_left_p = 0;
21727 it.glyph_row->truncated_on_right_p = 0;
21728
21729 /* Make a 3D mode-line have a shadow at its right end. */
21730 face = FACE_FROM_ID (it.f, face_id);
21731 extend_face_to_end_of_line (&it);
21732 if (face->box != FACE_NO_BOX)
21733 {
21734 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21735 + it.glyph_row->used[TEXT_AREA] - 1);
21736 last->right_box_line_p = 1;
21737 }
21738
21739 return it.glyph_row->height;
21740 }
21741
21742 /* Move element ELT in LIST to the front of LIST.
21743 Return the updated list. */
21744
21745 static Lisp_Object
21746 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21747 {
21748 register Lisp_Object tail, prev;
21749 register Lisp_Object tem;
21750
21751 tail = list;
21752 prev = Qnil;
21753 while (CONSP (tail))
21754 {
21755 tem = XCAR (tail);
21756
21757 if (EQ (elt, tem))
21758 {
21759 /* Splice out the link TAIL. */
21760 if (NILP (prev))
21761 list = XCDR (tail);
21762 else
21763 Fsetcdr (prev, XCDR (tail));
21764
21765 /* Now make it the first. */
21766 Fsetcdr (tail, list);
21767 return tail;
21768 }
21769 else
21770 prev = tail;
21771 tail = XCDR (tail);
21772 QUIT;
21773 }
21774
21775 /* Not found--return unchanged LIST. */
21776 return list;
21777 }
21778
21779 /* Contribute ELT to the mode line for window IT->w. How it
21780 translates into text depends on its data type.
21781
21782 IT describes the display environment in which we display, as usual.
21783
21784 DEPTH is the depth in recursion. It is used to prevent
21785 infinite recursion here.
21786
21787 FIELD_WIDTH is the number of characters the display of ELT should
21788 occupy in the mode line, and PRECISION is the maximum number of
21789 characters to display from ELT's representation. See
21790 display_string for details.
21791
21792 Returns the hpos of the end of the text generated by ELT.
21793
21794 PROPS is a property list to add to any string we encounter.
21795
21796 If RISKY is nonzero, remove (disregard) any properties in any string
21797 we encounter, and ignore :eval and :propertize.
21798
21799 The global variable `mode_line_target' determines whether the
21800 output is passed to `store_mode_line_noprop',
21801 `store_mode_line_string', or `display_string'. */
21802
21803 static int
21804 display_mode_element (struct it *it, int depth, int field_width, int precision,
21805 Lisp_Object elt, Lisp_Object props, int risky)
21806 {
21807 int n = 0, field, prec;
21808 int literal = 0;
21809
21810 tail_recurse:
21811 if (depth > 100)
21812 elt = build_string ("*too-deep*");
21813
21814 depth++;
21815
21816 switch (XTYPE (elt))
21817 {
21818 case Lisp_String:
21819 {
21820 /* A string: output it and check for %-constructs within it. */
21821 unsigned char c;
21822 ptrdiff_t offset = 0;
21823
21824 if (SCHARS (elt) > 0
21825 && (!NILP (props) || risky))
21826 {
21827 Lisp_Object oprops, aelt;
21828 oprops = Ftext_properties_at (make_number (0), elt);
21829
21830 /* If the starting string's properties are not what
21831 we want, translate the string. Also, if the string
21832 is risky, do that anyway. */
21833
21834 if (NILP (Fequal (props, oprops)) || risky)
21835 {
21836 /* If the starting string has properties,
21837 merge the specified ones onto the existing ones. */
21838 if (! NILP (oprops) && !risky)
21839 {
21840 Lisp_Object tem;
21841
21842 oprops = Fcopy_sequence (oprops);
21843 tem = props;
21844 while (CONSP (tem))
21845 {
21846 oprops = Fplist_put (oprops, XCAR (tem),
21847 XCAR (XCDR (tem)));
21848 tem = XCDR (XCDR (tem));
21849 }
21850 props = oprops;
21851 }
21852
21853 aelt = Fassoc (elt, mode_line_proptrans_alist);
21854 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21855 {
21856 /* AELT is what we want. Move it to the front
21857 without consing. */
21858 elt = XCAR (aelt);
21859 mode_line_proptrans_alist
21860 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21861 }
21862 else
21863 {
21864 Lisp_Object tem;
21865
21866 /* If AELT has the wrong props, it is useless.
21867 so get rid of it. */
21868 if (! NILP (aelt))
21869 mode_line_proptrans_alist
21870 = Fdelq (aelt, mode_line_proptrans_alist);
21871
21872 elt = Fcopy_sequence (elt);
21873 Fset_text_properties (make_number (0), Flength (elt),
21874 props, elt);
21875 /* Add this item to mode_line_proptrans_alist. */
21876 mode_line_proptrans_alist
21877 = Fcons (Fcons (elt, props),
21878 mode_line_proptrans_alist);
21879 /* Truncate mode_line_proptrans_alist
21880 to at most 50 elements. */
21881 tem = Fnthcdr (make_number (50),
21882 mode_line_proptrans_alist);
21883 if (! NILP (tem))
21884 XSETCDR (tem, Qnil);
21885 }
21886 }
21887 }
21888
21889 offset = 0;
21890
21891 if (literal)
21892 {
21893 prec = precision - n;
21894 switch (mode_line_target)
21895 {
21896 case MODE_LINE_NOPROP:
21897 case MODE_LINE_TITLE:
21898 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21899 break;
21900 case MODE_LINE_STRING:
21901 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21902 break;
21903 case MODE_LINE_DISPLAY:
21904 n += display_string (NULL, elt, Qnil, 0, 0, it,
21905 0, prec, 0, STRING_MULTIBYTE (elt));
21906 break;
21907 }
21908
21909 break;
21910 }
21911
21912 /* Handle the non-literal case. */
21913
21914 while ((precision <= 0 || n < precision)
21915 && SREF (elt, offset) != 0
21916 && (mode_line_target != MODE_LINE_DISPLAY
21917 || it->current_x < it->last_visible_x))
21918 {
21919 ptrdiff_t last_offset = offset;
21920
21921 /* Advance to end of string or next format specifier. */
21922 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21923 ;
21924
21925 if (offset - 1 != last_offset)
21926 {
21927 ptrdiff_t nchars, nbytes;
21928
21929 /* Output to end of string or up to '%'. Field width
21930 is length of string. Don't output more than
21931 PRECISION allows us. */
21932 offset--;
21933
21934 prec = c_string_width (SDATA (elt) + last_offset,
21935 offset - last_offset, precision - n,
21936 &nchars, &nbytes);
21937
21938 switch (mode_line_target)
21939 {
21940 case MODE_LINE_NOPROP:
21941 case MODE_LINE_TITLE:
21942 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21943 break;
21944 case MODE_LINE_STRING:
21945 {
21946 ptrdiff_t bytepos = last_offset;
21947 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21948 ptrdiff_t endpos = (precision <= 0
21949 ? string_byte_to_char (elt, offset)
21950 : charpos + nchars);
21951
21952 n += store_mode_line_string (NULL,
21953 Fsubstring (elt, make_number (charpos),
21954 make_number (endpos)),
21955 0, 0, 0, Qnil);
21956 }
21957 break;
21958 case MODE_LINE_DISPLAY:
21959 {
21960 ptrdiff_t bytepos = last_offset;
21961 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21962
21963 if (precision <= 0)
21964 nchars = string_byte_to_char (elt, offset) - charpos;
21965 n += display_string (NULL, elt, Qnil, 0, charpos,
21966 it, 0, nchars, 0,
21967 STRING_MULTIBYTE (elt));
21968 }
21969 break;
21970 }
21971 }
21972 else /* c == '%' */
21973 {
21974 ptrdiff_t percent_position = offset;
21975
21976 /* Get the specified minimum width. Zero means
21977 don't pad. */
21978 field = 0;
21979 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21980 field = field * 10 + c - '0';
21981
21982 /* Don't pad beyond the total padding allowed. */
21983 if (field_width - n > 0 && field > field_width - n)
21984 field = field_width - n;
21985
21986 /* Note that either PRECISION <= 0 or N < PRECISION. */
21987 prec = precision - n;
21988
21989 if (c == 'M')
21990 n += display_mode_element (it, depth, field, prec,
21991 Vglobal_mode_string, props,
21992 risky);
21993 else if (c != 0)
21994 {
21995 bool multibyte;
21996 ptrdiff_t bytepos, charpos;
21997 const char *spec;
21998 Lisp_Object string;
21999
22000 bytepos = percent_position;
22001 charpos = (STRING_MULTIBYTE (elt)
22002 ? string_byte_to_char (elt, bytepos)
22003 : bytepos);
22004 spec = decode_mode_spec (it->w, c, field, &string);
22005 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22006
22007 switch (mode_line_target)
22008 {
22009 case MODE_LINE_NOPROP:
22010 case MODE_LINE_TITLE:
22011 n += store_mode_line_noprop (spec, field, prec);
22012 break;
22013 case MODE_LINE_STRING:
22014 {
22015 Lisp_Object tem = build_string (spec);
22016 props = Ftext_properties_at (make_number (charpos), elt);
22017 /* Should only keep face property in props */
22018 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22019 }
22020 break;
22021 case MODE_LINE_DISPLAY:
22022 {
22023 int nglyphs_before, nwritten;
22024
22025 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22026 nwritten = display_string (spec, string, elt,
22027 charpos, 0, it,
22028 field, prec, 0,
22029 multibyte);
22030
22031 /* Assign to the glyphs written above the
22032 string where the `%x' came from, position
22033 of the `%'. */
22034 if (nwritten > 0)
22035 {
22036 struct glyph *glyph
22037 = (it->glyph_row->glyphs[TEXT_AREA]
22038 + nglyphs_before);
22039 int i;
22040
22041 for (i = 0; i < nwritten; ++i)
22042 {
22043 glyph[i].object = elt;
22044 glyph[i].charpos = charpos;
22045 }
22046
22047 n += nwritten;
22048 }
22049 }
22050 break;
22051 }
22052 }
22053 else /* c == 0 */
22054 break;
22055 }
22056 }
22057 }
22058 break;
22059
22060 case Lisp_Symbol:
22061 /* A symbol: process the value of the symbol recursively
22062 as if it appeared here directly. Avoid error if symbol void.
22063 Special case: if value of symbol is a string, output the string
22064 literally. */
22065 {
22066 register Lisp_Object tem;
22067
22068 /* If the variable is not marked as risky to set
22069 then its contents are risky to use. */
22070 if (NILP (Fget (elt, Qrisky_local_variable)))
22071 risky = 1;
22072
22073 tem = Fboundp (elt);
22074 if (!NILP (tem))
22075 {
22076 tem = Fsymbol_value (elt);
22077 /* If value is a string, output that string literally:
22078 don't check for % within it. */
22079 if (STRINGP (tem))
22080 literal = 1;
22081
22082 if (!EQ (tem, elt))
22083 {
22084 /* Give up right away for nil or t. */
22085 elt = tem;
22086 goto tail_recurse;
22087 }
22088 }
22089 }
22090 break;
22091
22092 case Lisp_Cons:
22093 {
22094 register Lisp_Object car, tem;
22095
22096 /* A cons cell: five distinct cases.
22097 If first element is :eval or :propertize, do something special.
22098 If first element is a string or a cons, process all the elements
22099 and effectively concatenate them.
22100 If first element is a negative number, truncate displaying cdr to
22101 at most that many characters. If positive, pad (with spaces)
22102 to at least that many characters.
22103 If first element is a symbol, process the cadr or caddr recursively
22104 according to whether the symbol's value is non-nil or nil. */
22105 car = XCAR (elt);
22106 if (EQ (car, QCeval))
22107 {
22108 /* An element of the form (:eval FORM) means evaluate FORM
22109 and use the result as mode line elements. */
22110
22111 if (risky)
22112 break;
22113
22114 if (CONSP (XCDR (elt)))
22115 {
22116 Lisp_Object spec;
22117 spec = safe__eval (true, XCAR (XCDR (elt)));
22118 n += display_mode_element (it, depth, field_width - n,
22119 precision - n, spec, props,
22120 risky);
22121 }
22122 }
22123 else if (EQ (car, QCpropertize))
22124 {
22125 /* An element of the form (:propertize ELT PROPS...)
22126 means display ELT but applying properties PROPS. */
22127
22128 if (risky)
22129 break;
22130
22131 if (CONSP (XCDR (elt)))
22132 n += display_mode_element (it, depth, field_width - n,
22133 precision - n, XCAR (XCDR (elt)),
22134 XCDR (XCDR (elt)), risky);
22135 }
22136 else if (SYMBOLP (car))
22137 {
22138 tem = Fboundp (car);
22139 elt = XCDR (elt);
22140 if (!CONSP (elt))
22141 goto invalid;
22142 /* elt is now the cdr, and we know it is a cons cell.
22143 Use its car if CAR has a non-nil value. */
22144 if (!NILP (tem))
22145 {
22146 tem = Fsymbol_value (car);
22147 if (!NILP (tem))
22148 {
22149 elt = XCAR (elt);
22150 goto tail_recurse;
22151 }
22152 }
22153 /* Symbol's value is nil (or symbol is unbound)
22154 Get the cddr of the original list
22155 and if possible find the caddr and use that. */
22156 elt = XCDR (elt);
22157 if (NILP (elt))
22158 break;
22159 else if (!CONSP (elt))
22160 goto invalid;
22161 elt = XCAR (elt);
22162 goto tail_recurse;
22163 }
22164 else if (INTEGERP (car))
22165 {
22166 register int lim = XINT (car);
22167 elt = XCDR (elt);
22168 if (lim < 0)
22169 {
22170 /* Negative int means reduce maximum width. */
22171 if (precision <= 0)
22172 precision = -lim;
22173 else
22174 precision = min (precision, -lim);
22175 }
22176 else if (lim > 0)
22177 {
22178 /* Padding specified. Don't let it be more than
22179 current maximum. */
22180 if (precision > 0)
22181 lim = min (precision, lim);
22182
22183 /* If that's more padding than already wanted, queue it.
22184 But don't reduce padding already specified even if
22185 that is beyond the current truncation point. */
22186 field_width = max (lim, field_width);
22187 }
22188 goto tail_recurse;
22189 }
22190 else if (STRINGP (car) || CONSP (car))
22191 {
22192 Lisp_Object halftail = elt;
22193 int len = 0;
22194
22195 while (CONSP (elt)
22196 && (precision <= 0 || n < precision))
22197 {
22198 n += display_mode_element (it, depth,
22199 /* Do padding only after the last
22200 element in the list. */
22201 (! CONSP (XCDR (elt))
22202 ? field_width - n
22203 : 0),
22204 precision - n, XCAR (elt),
22205 props, risky);
22206 elt = XCDR (elt);
22207 len++;
22208 if ((len & 1) == 0)
22209 halftail = XCDR (halftail);
22210 /* Check for cycle. */
22211 if (EQ (halftail, elt))
22212 break;
22213 }
22214 }
22215 }
22216 break;
22217
22218 default:
22219 invalid:
22220 elt = build_string ("*invalid*");
22221 goto tail_recurse;
22222 }
22223
22224 /* Pad to FIELD_WIDTH. */
22225 if (field_width > 0 && n < field_width)
22226 {
22227 switch (mode_line_target)
22228 {
22229 case MODE_LINE_NOPROP:
22230 case MODE_LINE_TITLE:
22231 n += store_mode_line_noprop ("", field_width - n, 0);
22232 break;
22233 case MODE_LINE_STRING:
22234 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22235 break;
22236 case MODE_LINE_DISPLAY:
22237 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22238 0, 0, 0);
22239 break;
22240 }
22241 }
22242
22243 return n;
22244 }
22245
22246 /* Store a mode-line string element in mode_line_string_list.
22247
22248 If STRING is non-null, display that C string. Otherwise, the Lisp
22249 string LISP_STRING is displayed.
22250
22251 FIELD_WIDTH is the minimum number of output glyphs to produce.
22252 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22253 with spaces. FIELD_WIDTH <= 0 means don't pad.
22254
22255 PRECISION is the maximum number of characters to output from
22256 STRING. PRECISION <= 0 means don't truncate the string.
22257
22258 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22259 properties to the string.
22260
22261 PROPS are the properties to add to the string.
22262 The mode_line_string_face face property is always added to the string.
22263 */
22264
22265 static int
22266 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22267 int field_width, int precision, Lisp_Object props)
22268 {
22269 ptrdiff_t len;
22270 int n = 0;
22271
22272 if (string != NULL)
22273 {
22274 len = strlen (string);
22275 if (precision > 0 && len > precision)
22276 len = precision;
22277 lisp_string = make_string (string, len);
22278 if (NILP (props))
22279 props = mode_line_string_face_prop;
22280 else if (!NILP (mode_line_string_face))
22281 {
22282 Lisp_Object face = Fplist_get (props, Qface);
22283 props = Fcopy_sequence (props);
22284 if (NILP (face))
22285 face = mode_line_string_face;
22286 else
22287 face = list2 (face, mode_line_string_face);
22288 props = Fplist_put (props, Qface, face);
22289 }
22290 Fadd_text_properties (make_number (0), make_number (len),
22291 props, lisp_string);
22292 }
22293 else
22294 {
22295 len = XFASTINT (Flength (lisp_string));
22296 if (precision > 0 && len > precision)
22297 {
22298 len = precision;
22299 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22300 precision = -1;
22301 }
22302 if (!NILP (mode_line_string_face))
22303 {
22304 Lisp_Object face;
22305 if (NILP (props))
22306 props = Ftext_properties_at (make_number (0), lisp_string);
22307 face = Fplist_get (props, Qface);
22308 if (NILP (face))
22309 face = mode_line_string_face;
22310 else
22311 face = list2 (face, mode_line_string_face);
22312 props = list2 (Qface, face);
22313 if (copy_string)
22314 lisp_string = Fcopy_sequence (lisp_string);
22315 }
22316 if (!NILP (props))
22317 Fadd_text_properties (make_number (0), make_number (len),
22318 props, lisp_string);
22319 }
22320
22321 if (len > 0)
22322 {
22323 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22324 n += len;
22325 }
22326
22327 if (field_width > len)
22328 {
22329 field_width -= len;
22330 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22331 if (!NILP (props))
22332 Fadd_text_properties (make_number (0), make_number (field_width),
22333 props, lisp_string);
22334 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22335 n += field_width;
22336 }
22337
22338 return n;
22339 }
22340
22341
22342 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22343 1, 4, 0,
22344 doc: /* Format a string out of a mode line format specification.
22345 First arg FORMAT specifies the mode line format (see `mode-line-format'
22346 for details) to use.
22347
22348 By default, the format is evaluated for the currently selected window.
22349
22350 Optional second arg FACE specifies the face property to put on all
22351 characters for which no face is specified. The value nil means the
22352 default face. The value t means whatever face the window's mode line
22353 currently uses (either `mode-line' or `mode-line-inactive',
22354 depending on whether the window is the selected window or not).
22355 An integer value means the value string has no text
22356 properties.
22357
22358 Optional third and fourth args WINDOW and BUFFER specify the window
22359 and buffer to use as the context for the formatting (defaults
22360 are the selected window and the WINDOW's buffer). */)
22361 (Lisp_Object format, Lisp_Object face,
22362 Lisp_Object window, Lisp_Object buffer)
22363 {
22364 struct it it;
22365 int len;
22366 struct window *w;
22367 struct buffer *old_buffer = NULL;
22368 int face_id;
22369 int no_props = INTEGERP (face);
22370 ptrdiff_t count = SPECPDL_INDEX ();
22371 Lisp_Object str;
22372 int string_start = 0;
22373
22374 w = decode_any_window (window);
22375 XSETWINDOW (window, w);
22376
22377 if (NILP (buffer))
22378 buffer = w->contents;
22379 CHECK_BUFFER (buffer);
22380
22381 /* Make formatting the modeline a non-op when noninteractive, otherwise
22382 there will be problems later caused by a partially initialized frame. */
22383 if (NILP (format) || noninteractive)
22384 return empty_unibyte_string;
22385
22386 if (no_props)
22387 face = Qnil;
22388
22389 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22390 : EQ (face, Qt) ? (EQ (window, selected_window)
22391 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22392 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22393 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22394 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22395 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22396 : DEFAULT_FACE_ID;
22397
22398 old_buffer = current_buffer;
22399
22400 /* Save things including mode_line_proptrans_alist,
22401 and set that to nil so that we don't alter the outer value. */
22402 record_unwind_protect (unwind_format_mode_line,
22403 format_mode_line_unwind_data
22404 (XFRAME (WINDOW_FRAME (w)),
22405 old_buffer, selected_window, 1));
22406 mode_line_proptrans_alist = Qnil;
22407
22408 Fselect_window (window, Qt);
22409 set_buffer_internal_1 (XBUFFER (buffer));
22410
22411 init_iterator (&it, w, -1, -1, NULL, face_id);
22412
22413 if (no_props)
22414 {
22415 mode_line_target = MODE_LINE_NOPROP;
22416 mode_line_string_face_prop = Qnil;
22417 mode_line_string_list = Qnil;
22418 string_start = MODE_LINE_NOPROP_LEN (0);
22419 }
22420 else
22421 {
22422 mode_line_target = MODE_LINE_STRING;
22423 mode_line_string_list = Qnil;
22424 mode_line_string_face = face;
22425 mode_line_string_face_prop
22426 = NILP (face) ? Qnil : list2 (Qface, face);
22427 }
22428
22429 push_kboard (FRAME_KBOARD (it.f));
22430 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22431 pop_kboard ();
22432
22433 if (no_props)
22434 {
22435 len = MODE_LINE_NOPROP_LEN (string_start);
22436 str = make_string (mode_line_noprop_buf + string_start, len);
22437 }
22438 else
22439 {
22440 mode_line_string_list = Fnreverse (mode_line_string_list);
22441 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22442 empty_unibyte_string);
22443 }
22444
22445 unbind_to (count, Qnil);
22446 return str;
22447 }
22448
22449 /* Write a null-terminated, right justified decimal representation of
22450 the positive integer D to BUF using a minimal field width WIDTH. */
22451
22452 static void
22453 pint2str (register char *buf, register int width, register ptrdiff_t d)
22454 {
22455 register char *p = buf;
22456
22457 if (d <= 0)
22458 *p++ = '0';
22459 else
22460 {
22461 while (d > 0)
22462 {
22463 *p++ = d % 10 + '0';
22464 d /= 10;
22465 }
22466 }
22467
22468 for (width -= (int) (p - buf); width > 0; --width)
22469 *p++ = ' ';
22470 *p-- = '\0';
22471 while (p > buf)
22472 {
22473 d = *buf;
22474 *buf++ = *p;
22475 *p-- = d;
22476 }
22477 }
22478
22479 /* Write a null-terminated, right justified decimal and "human
22480 readable" representation of the nonnegative integer D to BUF using
22481 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22482
22483 static const char power_letter[] =
22484 {
22485 0, /* no letter */
22486 'k', /* kilo */
22487 'M', /* mega */
22488 'G', /* giga */
22489 'T', /* tera */
22490 'P', /* peta */
22491 'E', /* exa */
22492 'Z', /* zetta */
22493 'Y' /* yotta */
22494 };
22495
22496 static void
22497 pint2hrstr (char *buf, int width, ptrdiff_t d)
22498 {
22499 /* We aim to represent the nonnegative integer D as
22500 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22501 ptrdiff_t quotient = d;
22502 int remainder = 0;
22503 /* -1 means: do not use TENTHS. */
22504 int tenths = -1;
22505 int exponent = 0;
22506
22507 /* Length of QUOTIENT.TENTHS as a string. */
22508 int length;
22509
22510 char * psuffix;
22511 char * p;
22512
22513 if (quotient >= 1000)
22514 {
22515 /* Scale to the appropriate EXPONENT. */
22516 do
22517 {
22518 remainder = quotient % 1000;
22519 quotient /= 1000;
22520 exponent++;
22521 }
22522 while (quotient >= 1000);
22523
22524 /* Round to nearest and decide whether to use TENTHS or not. */
22525 if (quotient <= 9)
22526 {
22527 tenths = remainder / 100;
22528 if (remainder % 100 >= 50)
22529 {
22530 if (tenths < 9)
22531 tenths++;
22532 else
22533 {
22534 quotient++;
22535 if (quotient == 10)
22536 tenths = -1;
22537 else
22538 tenths = 0;
22539 }
22540 }
22541 }
22542 else
22543 if (remainder >= 500)
22544 {
22545 if (quotient < 999)
22546 quotient++;
22547 else
22548 {
22549 quotient = 1;
22550 exponent++;
22551 tenths = 0;
22552 }
22553 }
22554 }
22555
22556 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22557 if (tenths == -1 && quotient <= 99)
22558 if (quotient <= 9)
22559 length = 1;
22560 else
22561 length = 2;
22562 else
22563 length = 3;
22564 p = psuffix = buf + max (width, length);
22565
22566 /* Print EXPONENT. */
22567 *psuffix++ = power_letter[exponent];
22568 *psuffix = '\0';
22569
22570 /* Print TENTHS. */
22571 if (tenths >= 0)
22572 {
22573 *--p = '0' + tenths;
22574 *--p = '.';
22575 }
22576
22577 /* Print QUOTIENT. */
22578 do
22579 {
22580 int digit = quotient % 10;
22581 *--p = '0' + digit;
22582 }
22583 while ((quotient /= 10) != 0);
22584
22585 /* Print leading spaces. */
22586 while (buf < p)
22587 *--p = ' ';
22588 }
22589
22590 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22591 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22592 type of CODING_SYSTEM. Return updated pointer into BUF. */
22593
22594 static unsigned char invalid_eol_type[] = "(*invalid*)";
22595
22596 static char *
22597 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22598 {
22599 Lisp_Object val;
22600 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22601 const unsigned char *eol_str;
22602 int eol_str_len;
22603 /* The EOL conversion we are using. */
22604 Lisp_Object eoltype;
22605
22606 val = CODING_SYSTEM_SPEC (coding_system);
22607 eoltype = Qnil;
22608
22609 if (!VECTORP (val)) /* Not yet decided. */
22610 {
22611 *buf++ = multibyte ? '-' : ' ';
22612 if (eol_flag)
22613 eoltype = eol_mnemonic_undecided;
22614 /* Don't mention EOL conversion if it isn't decided. */
22615 }
22616 else
22617 {
22618 Lisp_Object attrs;
22619 Lisp_Object eolvalue;
22620
22621 attrs = AREF (val, 0);
22622 eolvalue = AREF (val, 2);
22623
22624 *buf++ = multibyte
22625 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22626 : ' ';
22627
22628 if (eol_flag)
22629 {
22630 /* The EOL conversion that is normal on this system. */
22631
22632 if (NILP (eolvalue)) /* Not yet decided. */
22633 eoltype = eol_mnemonic_undecided;
22634 else if (VECTORP (eolvalue)) /* Not yet decided. */
22635 eoltype = eol_mnemonic_undecided;
22636 else /* eolvalue is Qunix, Qdos, or Qmac. */
22637 eoltype = (EQ (eolvalue, Qunix)
22638 ? eol_mnemonic_unix
22639 : (EQ (eolvalue, Qdos) == 1
22640 ? eol_mnemonic_dos : eol_mnemonic_mac));
22641 }
22642 }
22643
22644 if (eol_flag)
22645 {
22646 /* Mention the EOL conversion if it is not the usual one. */
22647 if (STRINGP (eoltype))
22648 {
22649 eol_str = SDATA (eoltype);
22650 eol_str_len = SBYTES (eoltype);
22651 }
22652 else if (CHARACTERP (eoltype))
22653 {
22654 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22655 int c = XFASTINT (eoltype);
22656 eol_str_len = CHAR_STRING (c, tmp);
22657 eol_str = tmp;
22658 }
22659 else
22660 {
22661 eol_str = invalid_eol_type;
22662 eol_str_len = sizeof (invalid_eol_type) - 1;
22663 }
22664 memcpy (buf, eol_str, eol_str_len);
22665 buf += eol_str_len;
22666 }
22667
22668 return buf;
22669 }
22670
22671 /* Return a string for the output of a mode line %-spec for window W,
22672 generated by character C. FIELD_WIDTH > 0 means pad the string
22673 returned with spaces to that value. Return a Lisp string in
22674 *STRING if the resulting string is taken from that Lisp string.
22675
22676 Note we operate on the current buffer for most purposes. */
22677
22678 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22679
22680 static const char *
22681 decode_mode_spec (struct window *w, register int c, int field_width,
22682 Lisp_Object *string)
22683 {
22684 Lisp_Object obj;
22685 struct frame *f = XFRAME (WINDOW_FRAME (w));
22686 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22687 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22688 produce strings from numerical values, so limit preposterously
22689 large values of FIELD_WIDTH to avoid overrunning the buffer's
22690 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22691 bytes plus the terminating null. */
22692 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22693 struct buffer *b = current_buffer;
22694
22695 obj = Qnil;
22696 *string = Qnil;
22697
22698 switch (c)
22699 {
22700 case '*':
22701 if (!NILP (BVAR (b, read_only)))
22702 return "%";
22703 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22704 return "*";
22705 return "-";
22706
22707 case '+':
22708 /* This differs from %* only for a modified read-only buffer. */
22709 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22710 return "*";
22711 if (!NILP (BVAR (b, read_only)))
22712 return "%";
22713 return "-";
22714
22715 case '&':
22716 /* This differs from %* in ignoring read-only-ness. */
22717 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22718 return "*";
22719 return "-";
22720
22721 case '%':
22722 return "%";
22723
22724 case '[':
22725 {
22726 int i;
22727 char *p;
22728
22729 if (command_loop_level > 5)
22730 return "[[[... ";
22731 p = decode_mode_spec_buf;
22732 for (i = 0; i < command_loop_level; i++)
22733 *p++ = '[';
22734 *p = 0;
22735 return decode_mode_spec_buf;
22736 }
22737
22738 case ']':
22739 {
22740 int i;
22741 char *p;
22742
22743 if (command_loop_level > 5)
22744 return " ...]]]";
22745 p = decode_mode_spec_buf;
22746 for (i = 0; i < command_loop_level; i++)
22747 *p++ = ']';
22748 *p = 0;
22749 return decode_mode_spec_buf;
22750 }
22751
22752 case '-':
22753 {
22754 register int i;
22755
22756 /* Let lots_of_dashes be a string of infinite length. */
22757 if (mode_line_target == MODE_LINE_NOPROP
22758 || mode_line_target == MODE_LINE_STRING)
22759 return "--";
22760 if (field_width <= 0
22761 || field_width > sizeof (lots_of_dashes))
22762 {
22763 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22764 decode_mode_spec_buf[i] = '-';
22765 decode_mode_spec_buf[i] = '\0';
22766 return decode_mode_spec_buf;
22767 }
22768 else
22769 return lots_of_dashes;
22770 }
22771
22772 case 'b':
22773 obj = BVAR (b, name);
22774 break;
22775
22776 case 'c':
22777 /* %c and %l are ignored in `frame-title-format'.
22778 (In redisplay_internal, the frame title is drawn _before_ the
22779 windows are updated, so the stuff which depends on actual
22780 window contents (such as %l) may fail to render properly, or
22781 even crash emacs.) */
22782 if (mode_line_target == MODE_LINE_TITLE)
22783 return "";
22784 else
22785 {
22786 ptrdiff_t col = current_column ();
22787 w->column_number_displayed = col;
22788 pint2str (decode_mode_spec_buf, width, col);
22789 return decode_mode_spec_buf;
22790 }
22791
22792 case 'e':
22793 #ifndef SYSTEM_MALLOC
22794 {
22795 if (NILP (Vmemory_full))
22796 return "";
22797 else
22798 return "!MEM FULL! ";
22799 }
22800 #else
22801 return "";
22802 #endif
22803
22804 case 'F':
22805 /* %F displays the frame name. */
22806 if (!NILP (f->title))
22807 return SSDATA (f->title);
22808 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22809 return SSDATA (f->name);
22810 return "Emacs";
22811
22812 case 'f':
22813 obj = BVAR (b, filename);
22814 break;
22815
22816 case 'i':
22817 {
22818 ptrdiff_t size = ZV - BEGV;
22819 pint2str (decode_mode_spec_buf, width, size);
22820 return decode_mode_spec_buf;
22821 }
22822
22823 case 'I':
22824 {
22825 ptrdiff_t size = ZV - BEGV;
22826 pint2hrstr (decode_mode_spec_buf, width, size);
22827 return decode_mode_spec_buf;
22828 }
22829
22830 case 'l':
22831 {
22832 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22833 ptrdiff_t topline, nlines, height;
22834 ptrdiff_t junk;
22835
22836 /* %c and %l are ignored in `frame-title-format'. */
22837 if (mode_line_target == MODE_LINE_TITLE)
22838 return "";
22839
22840 startpos = marker_position (w->start);
22841 startpos_byte = marker_byte_position (w->start);
22842 height = WINDOW_TOTAL_LINES (w);
22843
22844 /* If we decided that this buffer isn't suitable for line numbers,
22845 don't forget that too fast. */
22846 if (w->base_line_pos == -1)
22847 goto no_value;
22848
22849 /* If the buffer is very big, don't waste time. */
22850 if (INTEGERP (Vline_number_display_limit)
22851 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22852 {
22853 w->base_line_pos = 0;
22854 w->base_line_number = 0;
22855 goto no_value;
22856 }
22857
22858 if (w->base_line_number > 0
22859 && w->base_line_pos > 0
22860 && w->base_line_pos <= startpos)
22861 {
22862 line = w->base_line_number;
22863 linepos = w->base_line_pos;
22864 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22865 }
22866 else
22867 {
22868 line = 1;
22869 linepos = BUF_BEGV (b);
22870 linepos_byte = BUF_BEGV_BYTE (b);
22871 }
22872
22873 /* Count lines from base line to window start position. */
22874 nlines = display_count_lines (linepos_byte,
22875 startpos_byte,
22876 startpos, &junk);
22877
22878 topline = nlines + line;
22879
22880 /* Determine a new base line, if the old one is too close
22881 or too far away, or if we did not have one.
22882 "Too close" means it's plausible a scroll-down would
22883 go back past it. */
22884 if (startpos == BUF_BEGV (b))
22885 {
22886 w->base_line_number = topline;
22887 w->base_line_pos = BUF_BEGV (b);
22888 }
22889 else if (nlines < height + 25 || nlines > height * 3 + 50
22890 || linepos == BUF_BEGV (b))
22891 {
22892 ptrdiff_t limit = BUF_BEGV (b);
22893 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22894 ptrdiff_t position;
22895 ptrdiff_t distance =
22896 (height * 2 + 30) * line_number_display_limit_width;
22897
22898 if (startpos - distance > limit)
22899 {
22900 limit = startpos - distance;
22901 limit_byte = CHAR_TO_BYTE (limit);
22902 }
22903
22904 nlines = display_count_lines (startpos_byte,
22905 limit_byte,
22906 - (height * 2 + 30),
22907 &position);
22908 /* If we couldn't find the lines we wanted within
22909 line_number_display_limit_width chars per line,
22910 give up on line numbers for this window. */
22911 if (position == limit_byte && limit == startpos - distance)
22912 {
22913 w->base_line_pos = -1;
22914 w->base_line_number = 0;
22915 goto no_value;
22916 }
22917
22918 w->base_line_number = topline - nlines;
22919 w->base_line_pos = BYTE_TO_CHAR (position);
22920 }
22921
22922 /* Now count lines from the start pos to point. */
22923 nlines = display_count_lines (startpos_byte,
22924 PT_BYTE, PT, &junk);
22925
22926 /* Record that we did display the line number. */
22927 line_number_displayed = 1;
22928
22929 /* Make the string to show. */
22930 pint2str (decode_mode_spec_buf, width, topline + nlines);
22931 return decode_mode_spec_buf;
22932 no_value:
22933 {
22934 char* p = decode_mode_spec_buf;
22935 int pad = width - 2;
22936 while (pad-- > 0)
22937 *p++ = ' ';
22938 *p++ = '?';
22939 *p++ = '?';
22940 *p = '\0';
22941 return decode_mode_spec_buf;
22942 }
22943 }
22944 break;
22945
22946 case 'm':
22947 obj = BVAR (b, mode_name);
22948 break;
22949
22950 case 'n':
22951 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22952 return " Narrow";
22953 break;
22954
22955 case 'p':
22956 {
22957 ptrdiff_t pos = marker_position (w->start);
22958 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22959
22960 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22961 {
22962 if (pos <= BUF_BEGV (b))
22963 return "All";
22964 else
22965 return "Bottom";
22966 }
22967 else if (pos <= BUF_BEGV (b))
22968 return "Top";
22969 else
22970 {
22971 if (total > 1000000)
22972 /* Do it differently for a large value, to avoid overflow. */
22973 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22974 else
22975 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22976 /* We can't normally display a 3-digit number,
22977 so get us a 2-digit number that is close. */
22978 if (total == 100)
22979 total = 99;
22980 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22981 return decode_mode_spec_buf;
22982 }
22983 }
22984
22985 /* Display percentage of size above the bottom of the screen. */
22986 case 'P':
22987 {
22988 ptrdiff_t toppos = marker_position (w->start);
22989 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22990 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22991
22992 if (botpos >= BUF_ZV (b))
22993 {
22994 if (toppos <= BUF_BEGV (b))
22995 return "All";
22996 else
22997 return "Bottom";
22998 }
22999 else
23000 {
23001 if (total > 1000000)
23002 /* Do it differently for a large value, to avoid overflow. */
23003 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23004 else
23005 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23006 /* We can't normally display a 3-digit number,
23007 so get us a 2-digit number that is close. */
23008 if (total == 100)
23009 total = 99;
23010 if (toppos <= BUF_BEGV (b))
23011 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23012 else
23013 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23014 return decode_mode_spec_buf;
23015 }
23016 }
23017
23018 case 's':
23019 /* status of process */
23020 obj = Fget_buffer_process (Fcurrent_buffer ());
23021 if (NILP (obj))
23022 return "no process";
23023 #ifndef MSDOS
23024 obj = Fsymbol_name (Fprocess_status (obj));
23025 #endif
23026 break;
23027
23028 case '@':
23029 {
23030 ptrdiff_t count = inhibit_garbage_collection ();
23031 Lisp_Object curdir = BVAR (current_buffer, directory);
23032 Lisp_Object val = Qnil;
23033
23034 if (STRINGP (curdir))
23035 val = call1 (intern ("file-remote-p"), curdir);
23036
23037 unbind_to (count, Qnil);
23038
23039 if (NILP (val))
23040 return "-";
23041 else
23042 return "@";
23043 }
23044
23045 case 'z':
23046 /* coding-system (not including end-of-line format) */
23047 case 'Z':
23048 /* coding-system (including end-of-line type) */
23049 {
23050 int eol_flag = (c == 'Z');
23051 char *p = decode_mode_spec_buf;
23052
23053 if (! FRAME_WINDOW_P (f))
23054 {
23055 /* No need to mention EOL here--the terminal never needs
23056 to do EOL conversion. */
23057 p = decode_mode_spec_coding (CODING_ID_NAME
23058 (FRAME_KEYBOARD_CODING (f)->id),
23059 p, 0);
23060 p = decode_mode_spec_coding (CODING_ID_NAME
23061 (FRAME_TERMINAL_CODING (f)->id),
23062 p, 0);
23063 }
23064 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23065 p, eol_flag);
23066
23067 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23068 #ifdef subprocesses
23069 obj = Fget_buffer_process (Fcurrent_buffer ());
23070 if (PROCESSP (obj))
23071 {
23072 p = decode_mode_spec_coding
23073 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23074 p = decode_mode_spec_coding
23075 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23076 }
23077 #endif /* subprocesses */
23078 #endif /* 0 */
23079 *p = 0;
23080 return decode_mode_spec_buf;
23081 }
23082 }
23083
23084 if (STRINGP (obj))
23085 {
23086 *string = obj;
23087 return SSDATA (obj);
23088 }
23089 else
23090 return "";
23091 }
23092
23093
23094 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23095 means count lines back from START_BYTE. But don't go beyond
23096 LIMIT_BYTE. Return the number of lines thus found (always
23097 nonnegative).
23098
23099 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23100 either the position COUNT lines after/before START_BYTE, if we
23101 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23102 COUNT lines. */
23103
23104 static ptrdiff_t
23105 display_count_lines (ptrdiff_t start_byte,
23106 ptrdiff_t limit_byte, ptrdiff_t count,
23107 ptrdiff_t *byte_pos_ptr)
23108 {
23109 register unsigned char *cursor;
23110 unsigned char *base;
23111
23112 register ptrdiff_t ceiling;
23113 register unsigned char *ceiling_addr;
23114 ptrdiff_t orig_count = count;
23115
23116 /* If we are not in selective display mode,
23117 check only for newlines. */
23118 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23119 && !INTEGERP (BVAR (current_buffer, selective_display)));
23120
23121 if (count > 0)
23122 {
23123 while (start_byte < limit_byte)
23124 {
23125 ceiling = BUFFER_CEILING_OF (start_byte);
23126 ceiling = min (limit_byte - 1, ceiling);
23127 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23128 base = (cursor = BYTE_POS_ADDR (start_byte));
23129
23130 do
23131 {
23132 if (selective_display)
23133 {
23134 while (*cursor != '\n' && *cursor != 015
23135 && ++cursor != ceiling_addr)
23136 continue;
23137 if (cursor == ceiling_addr)
23138 break;
23139 }
23140 else
23141 {
23142 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23143 if (! cursor)
23144 break;
23145 }
23146
23147 cursor++;
23148
23149 if (--count == 0)
23150 {
23151 start_byte += cursor - base;
23152 *byte_pos_ptr = start_byte;
23153 return orig_count;
23154 }
23155 }
23156 while (cursor < ceiling_addr);
23157
23158 start_byte += ceiling_addr - base;
23159 }
23160 }
23161 else
23162 {
23163 while (start_byte > limit_byte)
23164 {
23165 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23166 ceiling = max (limit_byte, ceiling);
23167 ceiling_addr = BYTE_POS_ADDR (ceiling);
23168 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23169 while (1)
23170 {
23171 if (selective_display)
23172 {
23173 while (--cursor >= ceiling_addr
23174 && *cursor != '\n' && *cursor != 015)
23175 continue;
23176 if (cursor < ceiling_addr)
23177 break;
23178 }
23179 else
23180 {
23181 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23182 if (! cursor)
23183 break;
23184 }
23185
23186 if (++count == 0)
23187 {
23188 start_byte += cursor - base + 1;
23189 *byte_pos_ptr = start_byte;
23190 /* When scanning backwards, we should
23191 not count the newline posterior to which we stop. */
23192 return - orig_count - 1;
23193 }
23194 }
23195 start_byte += ceiling_addr - base;
23196 }
23197 }
23198
23199 *byte_pos_ptr = limit_byte;
23200
23201 if (count < 0)
23202 return - orig_count + count;
23203 return orig_count - count;
23204
23205 }
23206
23207
23208 \f
23209 /***********************************************************************
23210 Displaying strings
23211 ***********************************************************************/
23212
23213 /* Display a NUL-terminated string, starting with index START.
23214
23215 If STRING is non-null, display that C string. Otherwise, the Lisp
23216 string LISP_STRING is displayed. There's a case that STRING is
23217 non-null and LISP_STRING is not nil. It means STRING is a string
23218 data of LISP_STRING. In that case, we display LISP_STRING while
23219 ignoring its text properties.
23220
23221 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23222 FACE_STRING. Display STRING or LISP_STRING with the face at
23223 FACE_STRING_POS in FACE_STRING:
23224
23225 Display the string in the environment given by IT, but use the
23226 standard display table, temporarily.
23227
23228 FIELD_WIDTH is the minimum number of output glyphs to produce.
23229 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23230 with spaces. If STRING has more characters, more than FIELD_WIDTH
23231 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23232
23233 PRECISION is the maximum number of characters to output from
23234 STRING. PRECISION < 0 means don't truncate the string.
23235
23236 This is roughly equivalent to printf format specifiers:
23237
23238 FIELD_WIDTH PRECISION PRINTF
23239 ----------------------------------------
23240 -1 -1 %s
23241 -1 10 %.10s
23242 10 -1 %10s
23243 20 10 %20.10s
23244
23245 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23246 display them, and < 0 means obey the current buffer's value of
23247 enable_multibyte_characters.
23248
23249 Value is the number of columns displayed. */
23250
23251 static int
23252 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23253 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23254 int field_width, int precision, int max_x, int multibyte)
23255 {
23256 int hpos_at_start = it->hpos;
23257 int saved_face_id = it->face_id;
23258 struct glyph_row *row = it->glyph_row;
23259 ptrdiff_t it_charpos;
23260
23261 /* Initialize the iterator IT for iteration over STRING beginning
23262 with index START. */
23263 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23264 precision, field_width, multibyte);
23265 if (string && STRINGP (lisp_string))
23266 /* LISP_STRING is the one returned by decode_mode_spec. We should
23267 ignore its text properties. */
23268 it->stop_charpos = it->end_charpos;
23269
23270 /* If displaying STRING, set up the face of the iterator from
23271 FACE_STRING, if that's given. */
23272 if (STRINGP (face_string))
23273 {
23274 ptrdiff_t endptr;
23275 struct face *face;
23276
23277 it->face_id
23278 = face_at_string_position (it->w, face_string, face_string_pos,
23279 0, &endptr, it->base_face_id, 0);
23280 face = FACE_FROM_ID (it->f, it->face_id);
23281 it->face_box_p = face->box != FACE_NO_BOX;
23282 }
23283
23284 /* Set max_x to the maximum allowed X position. Don't let it go
23285 beyond the right edge of the window. */
23286 if (max_x <= 0)
23287 max_x = it->last_visible_x;
23288 else
23289 max_x = min (max_x, it->last_visible_x);
23290
23291 /* Skip over display elements that are not visible. because IT->w is
23292 hscrolled. */
23293 if (it->current_x < it->first_visible_x)
23294 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23295 MOVE_TO_POS | MOVE_TO_X);
23296
23297 row->ascent = it->max_ascent;
23298 row->height = it->max_ascent + it->max_descent;
23299 row->phys_ascent = it->max_phys_ascent;
23300 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23301 row->extra_line_spacing = it->max_extra_line_spacing;
23302
23303 if (STRINGP (it->string))
23304 it_charpos = IT_STRING_CHARPOS (*it);
23305 else
23306 it_charpos = IT_CHARPOS (*it);
23307
23308 /* This condition is for the case that we are called with current_x
23309 past last_visible_x. */
23310 while (it->current_x < max_x)
23311 {
23312 int x_before, x, n_glyphs_before, i, nglyphs;
23313
23314 /* Get the next display element. */
23315 if (!get_next_display_element (it))
23316 break;
23317
23318 /* Produce glyphs. */
23319 x_before = it->current_x;
23320 n_glyphs_before = row->used[TEXT_AREA];
23321 PRODUCE_GLYPHS (it);
23322
23323 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23324 i = 0;
23325 x = x_before;
23326 while (i < nglyphs)
23327 {
23328 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23329
23330 if (it->line_wrap != TRUNCATE
23331 && x + glyph->pixel_width > max_x)
23332 {
23333 /* End of continued line or max_x reached. */
23334 if (CHAR_GLYPH_PADDING_P (*glyph))
23335 {
23336 /* A wide character is unbreakable. */
23337 if (row->reversed_p)
23338 unproduce_glyphs (it, row->used[TEXT_AREA]
23339 - n_glyphs_before);
23340 row->used[TEXT_AREA] = n_glyphs_before;
23341 it->current_x = x_before;
23342 }
23343 else
23344 {
23345 if (row->reversed_p)
23346 unproduce_glyphs (it, row->used[TEXT_AREA]
23347 - (n_glyphs_before + i));
23348 row->used[TEXT_AREA] = n_glyphs_before + i;
23349 it->current_x = x;
23350 }
23351 break;
23352 }
23353 else if (x + glyph->pixel_width >= it->first_visible_x)
23354 {
23355 /* Glyph is at least partially visible. */
23356 ++it->hpos;
23357 if (x < it->first_visible_x)
23358 row->x = x - it->first_visible_x;
23359 }
23360 else
23361 {
23362 /* Glyph is off the left margin of the display area.
23363 Should not happen. */
23364 emacs_abort ();
23365 }
23366
23367 row->ascent = max (row->ascent, it->max_ascent);
23368 row->height = max (row->height, it->max_ascent + it->max_descent);
23369 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23370 row->phys_height = max (row->phys_height,
23371 it->max_phys_ascent + it->max_phys_descent);
23372 row->extra_line_spacing = max (row->extra_line_spacing,
23373 it->max_extra_line_spacing);
23374 x += glyph->pixel_width;
23375 ++i;
23376 }
23377
23378 /* Stop if max_x reached. */
23379 if (i < nglyphs)
23380 break;
23381
23382 /* Stop at line ends. */
23383 if (ITERATOR_AT_END_OF_LINE_P (it))
23384 {
23385 it->continuation_lines_width = 0;
23386 break;
23387 }
23388
23389 set_iterator_to_next (it, 1);
23390 if (STRINGP (it->string))
23391 it_charpos = IT_STRING_CHARPOS (*it);
23392 else
23393 it_charpos = IT_CHARPOS (*it);
23394
23395 /* Stop if truncating at the right edge. */
23396 if (it->line_wrap == TRUNCATE
23397 && it->current_x >= it->last_visible_x)
23398 {
23399 /* Add truncation mark, but don't do it if the line is
23400 truncated at a padding space. */
23401 if (it_charpos < it->string_nchars)
23402 {
23403 if (!FRAME_WINDOW_P (it->f))
23404 {
23405 int ii, n;
23406
23407 if (it->current_x > it->last_visible_x)
23408 {
23409 if (!row->reversed_p)
23410 {
23411 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23412 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23413 break;
23414 }
23415 else
23416 {
23417 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23418 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23419 break;
23420 unproduce_glyphs (it, ii + 1);
23421 ii = row->used[TEXT_AREA] - (ii + 1);
23422 }
23423 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23424 {
23425 row->used[TEXT_AREA] = ii;
23426 produce_special_glyphs (it, IT_TRUNCATION);
23427 }
23428 }
23429 produce_special_glyphs (it, IT_TRUNCATION);
23430 }
23431 row->truncated_on_right_p = 1;
23432 }
23433 break;
23434 }
23435 }
23436
23437 /* Maybe insert a truncation at the left. */
23438 if (it->first_visible_x
23439 && it_charpos > 0)
23440 {
23441 if (!FRAME_WINDOW_P (it->f)
23442 || (row->reversed_p
23443 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23444 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23445 insert_left_trunc_glyphs (it);
23446 row->truncated_on_left_p = 1;
23447 }
23448
23449 it->face_id = saved_face_id;
23450
23451 /* Value is number of columns displayed. */
23452 return it->hpos - hpos_at_start;
23453 }
23454
23455
23456 \f
23457 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23458 appears as an element of LIST or as the car of an element of LIST.
23459 If PROPVAL is a list, compare each element against LIST in that
23460 way, and return 1/2 if any element of PROPVAL is found in LIST.
23461 Otherwise return 0. This function cannot quit.
23462 The return value is 2 if the text is invisible but with an ellipsis
23463 and 1 if it's invisible and without an ellipsis. */
23464
23465 int
23466 invisible_p (register Lisp_Object propval, Lisp_Object list)
23467 {
23468 register Lisp_Object tail, proptail;
23469
23470 for (tail = list; CONSP (tail); tail = XCDR (tail))
23471 {
23472 register Lisp_Object tem;
23473 tem = XCAR (tail);
23474 if (EQ (propval, tem))
23475 return 1;
23476 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23477 return NILP (XCDR (tem)) ? 1 : 2;
23478 }
23479
23480 if (CONSP (propval))
23481 {
23482 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23483 {
23484 Lisp_Object propelt;
23485 propelt = XCAR (proptail);
23486 for (tail = list; CONSP (tail); tail = XCDR (tail))
23487 {
23488 register Lisp_Object tem;
23489 tem = XCAR (tail);
23490 if (EQ (propelt, tem))
23491 return 1;
23492 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23493 return NILP (XCDR (tem)) ? 1 : 2;
23494 }
23495 }
23496 }
23497
23498 return 0;
23499 }
23500
23501 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23502 doc: /* Non-nil if the property makes the text invisible.
23503 POS-OR-PROP can be a marker or number, in which case it is taken to be
23504 a position in the current buffer and the value of the `invisible' property
23505 is checked; or it can be some other value, which is then presumed to be the
23506 value of the `invisible' property of the text of interest.
23507 The non-nil value returned can be t for truly invisible text or something
23508 else if the text is replaced by an ellipsis. */)
23509 (Lisp_Object pos_or_prop)
23510 {
23511 Lisp_Object prop
23512 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23513 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23514 : pos_or_prop);
23515 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23516 return (invis == 0 ? Qnil
23517 : invis == 1 ? Qt
23518 : make_number (invis));
23519 }
23520
23521 /* Calculate a width or height in pixels from a specification using
23522 the following elements:
23523
23524 SPEC ::=
23525 NUM - a (fractional) multiple of the default font width/height
23526 (NUM) - specifies exactly NUM pixels
23527 UNIT - a fixed number of pixels, see below.
23528 ELEMENT - size of a display element in pixels, see below.
23529 (NUM . SPEC) - equals NUM * SPEC
23530 (+ SPEC SPEC ...) - add pixel values
23531 (- SPEC SPEC ...) - subtract pixel values
23532 (- SPEC) - negate pixel value
23533
23534 NUM ::=
23535 INT or FLOAT - a number constant
23536 SYMBOL - use symbol's (buffer local) variable binding.
23537
23538 UNIT ::=
23539 in - pixels per inch *)
23540 mm - pixels per 1/1000 meter *)
23541 cm - pixels per 1/100 meter *)
23542 width - width of current font in pixels.
23543 height - height of current font in pixels.
23544
23545 *) using the ratio(s) defined in display-pixels-per-inch.
23546
23547 ELEMENT ::=
23548
23549 left-fringe - left fringe width in pixels
23550 right-fringe - right fringe width in pixels
23551
23552 left-margin - left margin width in pixels
23553 right-margin - right margin width in pixels
23554
23555 scroll-bar - scroll-bar area width in pixels
23556
23557 Examples:
23558
23559 Pixels corresponding to 5 inches:
23560 (5 . in)
23561
23562 Total width of non-text areas on left side of window (if scroll-bar is on left):
23563 '(space :width (+ left-fringe left-margin scroll-bar))
23564
23565 Align to first text column (in header line):
23566 '(space :align-to 0)
23567
23568 Align to middle of text area minus half the width of variable `my-image'
23569 containing a loaded image:
23570 '(space :align-to (0.5 . (- text my-image)))
23571
23572 Width of left margin minus width of 1 character in the default font:
23573 '(space :width (- left-margin 1))
23574
23575 Width of left margin minus width of 2 characters in the current font:
23576 '(space :width (- left-margin (2 . width)))
23577
23578 Center 1 character over left-margin (in header line):
23579 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23580
23581 Different ways to express width of left fringe plus left margin minus one pixel:
23582 '(space :width (- (+ left-fringe left-margin) (1)))
23583 '(space :width (+ left-fringe left-margin (- (1))))
23584 '(space :width (+ left-fringe left-margin (-1)))
23585
23586 */
23587
23588 static int
23589 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23590 struct font *font, int width_p, int *align_to)
23591 {
23592 double pixels;
23593
23594 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23595 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23596
23597 if (NILP (prop))
23598 return OK_PIXELS (0);
23599
23600 eassert (FRAME_LIVE_P (it->f));
23601
23602 if (SYMBOLP (prop))
23603 {
23604 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23605 {
23606 char *unit = SSDATA (SYMBOL_NAME (prop));
23607
23608 if (unit[0] == 'i' && unit[1] == 'n')
23609 pixels = 1.0;
23610 else if (unit[0] == 'm' && unit[1] == 'm')
23611 pixels = 25.4;
23612 else if (unit[0] == 'c' && unit[1] == 'm')
23613 pixels = 2.54;
23614 else
23615 pixels = 0;
23616 if (pixels > 0)
23617 {
23618 double ppi = (width_p ? FRAME_RES_X (it->f)
23619 : FRAME_RES_Y (it->f));
23620
23621 if (ppi > 0)
23622 return OK_PIXELS (ppi / pixels);
23623 return 0;
23624 }
23625 }
23626
23627 #ifdef HAVE_WINDOW_SYSTEM
23628 if (EQ (prop, Qheight))
23629 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23630 if (EQ (prop, Qwidth))
23631 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23632 #else
23633 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23634 return OK_PIXELS (1);
23635 #endif
23636
23637 if (EQ (prop, Qtext))
23638 return OK_PIXELS (width_p
23639 ? window_box_width (it->w, TEXT_AREA)
23640 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23641
23642 if (align_to && *align_to < 0)
23643 {
23644 *res = 0;
23645 if (EQ (prop, Qleft))
23646 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23647 if (EQ (prop, Qright))
23648 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23649 if (EQ (prop, Qcenter))
23650 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23651 + window_box_width (it->w, TEXT_AREA) / 2);
23652 if (EQ (prop, Qleft_fringe))
23653 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23654 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23655 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23656 if (EQ (prop, Qright_fringe))
23657 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23658 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23659 : window_box_right_offset (it->w, TEXT_AREA));
23660 if (EQ (prop, Qleft_margin))
23661 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23662 if (EQ (prop, Qright_margin))
23663 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23664 if (EQ (prop, Qscroll_bar))
23665 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23666 ? 0
23667 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23668 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23669 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23670 : 0)));
23671 }
23672 else
23673 {
23674 if (EQ (prop, Qleft_fringe))
23675 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23676 if (EQ (prop, Qright_fringe))
23677 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23678 if (EQ (prop, Qleft_margin))
23679 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23680 if (EQ (prop, Qright_margin))
23681 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23682 if (EQ (prop, Qscroll_bar))
23683 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23684 }
23685
23686 prop = buffer_local_value_1 (prop, it->w->contents);
23687 if (EQ (prop, Qunbound))
23688 prop = Qnil;
23689 }
23690
23691 if (INTEGERP (prop) || FLOATP (prop))
23692 {
23693 int base_unit = (width_p
23694 ? FRAME_COLUMN_WIDTH (it->f)
23695 : FRAME_LINE_HEIGHT (it->f));
23696 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23697 }
23698
23699 if (CONSP (prop))
23700 {
23701 Lisp_Object car = XCAR (prop);
23702 Lisp_Object cdr = XCDR (prop);
23703
23704 if (SYMBOLP (car))
23705 {
23706 #ifdef HAVE_WINDOW_SYSTEM
23707 if (FRAME_WINDOW_P (it->f)
23708 && valid_image_p (prop))
23709 {
23710 ptrdiff_t id = lookup_image (it->f, prop);
23711 struct image *img = IMAGE_FROM_ID (it->f, id);
23712
23713 return OK_PIXELS (width_p ? img->width : img->height);
23714 }
23715 #endif
23716 if (EQ (car, Qplus) || EQ (car, Qminus))
23717 {
23718 int first = 1;
23719 double px;
23720
23721 pixels = 0;
23722 while (CONSP (cdr))
23723 {
23724 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23725 font, width_p, align_to))
23726 return 0;
23727 if (first)
23728 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23729 else
23730 pixels += px;
23731 cdr = XCDR (cdr);
23732 }
23733 if (EQ (car, Qminus))
23734 pixels = -pixels;
23735 return OK_PIXELS (pixels);
23736 }
23737
23738 car = buffer_local_value_1 (car, it->w->contents);
23739 if (EQ (car, Qunbound))
23740 car = Qnil;
23741 }
23742
23743 if (INTEGERP (car) || FLOATP (car))
23744 {
23745 double fact;
23746 pixels = XFLOATINT (car);
23747 if (NILP (cdr))
23748 return OK_PIXELS (pixels);
23749 if (calc_pixel_width_or_height (&fact, it, cdr,
23750 font, width_p, align_to))
23751 return OK_PIXELS (pixels * fact);
23752 return 0;
23753 }
23754
23755 return 0;
23756 }
23757
23758 return 0;
23759 }
23760
23761 \f
23762 /***********************************************************************
23763 Glyph Display
23764 ***********************************************************************/
23765
23766 #ifdef HAVE_WINDOW_SYSTEM
23767
23768 #ifdef GLYPH_DEBUG
23769
23770 void
23771 dump_glyph_string (struct glyph_string *s)
23772 {
23773 fprintf (stderr, "glyph string\n");
23774 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23775 s->x, s->y, s->width, s->height);
23776 fprintf (stderr, " ybase = %d\n", s->ybase);
23777 fprintf (stderr, " hl = %d\n", s->hl);
23778 fprintf (stderr, " left overhang = %d, right = %d\n",
23779 s->left_overhang, s->right_overhang);
23780 fprintf (stderr, " nchars = %d\n", s->nchars);
23781 fprintf (stderr, " extends to end of line = %d\n",
23782 s->extends_to_end_of_line_p);
23783 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23784 fprintf (stderr, " bg width = %d\n", s->background_width);
23785 }
23786
23787 #endif /* GLYPH_DEBUG */
23788
23789 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23790 of XChar2b structures for S; it can't be allocated in
23791 init_glyph_string because it must be allocated via `alloca'. W
23792 is the window on which S is drawn. ROW and AREA are the glyph row
23793 and area within the row from which S is constructed. START is the
23794 index of the first glyph structure covered by S. HL is a
23795 face-override for drawing S. */
23796
23797 #ifdef HAVE_NTGUI
23798 #define OPTIONAL_HDC(hdc) HDC hdc,
23799 #define DECLARE_HDC(hdc) HDC hdc;
23800 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23801 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23802 #endif
23803
23804 #ifndef OPTIONAL_HDC
23805 #define OPTIONAL_HDC(hdc)
23806 #define DECLARE_HDC(hdc)
23807 #define ALLOCATE_HDC(hdc, f)
23808 #define RELEASE_HDC(hdc, f)
23809 #endif
23810
23811 static void
23812 init_glyph_string (struct glyph_string *s,
23813 OPTIONAL_HDC (hdc)
23814 XChar2b *char2b, struct window *w, struct glyph_row *row,
23815 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23816 {
23817 memset (s, 0, sizeof *s);
23818 s->w = w;
23819 s->f = XFRAME (w->frame);
23820 #ifdef HAVE_NTGUI
23821 s->hdc = hdc;
23822 #endif
23823 s->display = FRAME_X_DISPLAY (s->f);
23824 s->window = FRAME_X_WINDOW (s->f);
23825 s->char2b = char2b;
23826 s->hl = hl;
23827 s->row = row;
23828 s->area = area;
23829 s->first_glyph = row->glyphs[area] + start;
23830 s->height = row->height;
23831 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23832 s->ybase = s->y + row->ascent;
23833 }
23834
23835
23836 /* Append the list of glyph strings with head H and tail T to the list
23837 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23838
23839 static void
23840 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23841 struct glyph_string *h, struct glyph_string *t)
23842 {
23843 if (h)
23844 {
23845 if (*head)
23846 (*tail)->next = h;
23847 else
23848 *head = h;
23849 h->prev = *tail;
23850 *tail = t;
23851 }
23852 }
23853
23854
23855 /* Prepend the list of glyph strings with head H and tail T to the
23856 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23857 result. */
23858
23859 static void
23860 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23861 struct glyph_string *h, struct glyph_string *t)
23862 {
23863 if (h)
23864 {
23865 if (*head)
23866 (*head)->prev = t;
23867 else
23868 *tail = t;
23869 t->next = *head;
23870 *head = h;
23871 }
23872 }
23873
23874
23875 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23876 Set *HEAD and *TAIL to the resulting list. */
23877
23878 static void
23879 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23880 struct glyph_string *s)
23881 {
23882 s->next = s->prev = NULL;
23883 append_glyph_string_lists (head, tail, s, s);
23884 }
23885
23886
23887 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23888 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23889 make sure that X resources for the face returned are allocated.
23890 Value is a pointer to a realized face that is ready for display if
23891 DISPLAY_P is non-zero. */
23892
23893 static struct face *
23894 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23895 XChar2b *char2b, int display_p)
23896 {
23897 struct face *face = FACE_FROM_ID (f, face_id);
23898 unsigned code = 0;
23899
23900 if (face->font)
23901 {
23902 code = face->font->driver->encode_char (face->font, c);
23903
23904 if (code == FONT_INVALID_CODE)
23905 code = 0;
23906 }
23907 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23908
23909 /* Make sure X resources of the face are allocated. */
23910 #ifdef HAVE_X_WINDOWS
23911 if (display_p)
23912 #endif
23913 {
23914 eassert (face != NULL);
23915 PREPARE_FACE_FOR_DISPLAY (f, face);
23916 }
23917
23918 return face;
23919 }
23920
23921
23922 /* Get face and two-byte form of character glyph GLYPH on frame F.
23923 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23924 a pointer to a realized face that is ready for display. */
23925
23926 static struct face *
23927 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23928 XChar2b *char2b, int *two_byte_p)
23929 {
23930 struct face *face;
23931 unsigned code = 0;
23932
23933 eassert (glyph->type == CHAR_GLYPH);
23934 face = FACE_FROM_ID (f, glyph->face_id);
23935
23936 /* Make sure X resources of the face are allocated. */
23937 eassert (face != NULL);
23938 PREPARE_FACE_FOR_DISPLAY (f, face);
23939
23940 if (two_byte_p)
23941 *two_byte_p = 0;
23942
23943 if (face->font)
23944 {
23945 if (CHAR_BYTE8_P (glyph->u.ch))
23946 code = CHAR_TO_BYTE8 (glyph->u.ch);
23947 else
23948 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23949
23950 if (code == FONT_INVALID_CODE)
23951 code = 0;
23952 }
23953
23954 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23955 return face;
23956 }
23957
23958
23959 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23960 Return 1 if FONT has a glyph for C, otherwise return 0. */
23961
23962 static int
23963 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23964 {
23965 unsigned code;
23966
23967 if (CHAR_BYTE8_P (c))
23968 code = CHAR_TO_BYTE8 (c);
23969 else
23970 code = font->driver->encode_char (font, c);
23971
23972 if (code == FONT_INVALID_CODE)
23973 return 0;
23974 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23975 return 1;
23976 }
23977
23978
23979 /* Fill glyph string S with composition components specified by S->cmp.
23980
23981 BASE_FACE is the base face of the composition.
23982 S->cmp_from is the index of the first component for S.
23983
23984 OVERLAPS non-zero means S should draw the foreground only, and use
23985 its physical height for clipping. See also draw_glyphs.
23986
23987 Value is the index of a component not in S. */
23988
23989 static int
23990 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23991 int overlaps)
23992 {
23993 int i;
23994 /* For all glyphs of this composition, starting at the offset
23995 S->cmp_from, until we reach the end of the definition or encounter a
23996 glyph that requires the different face, add it to S. */
23997 struct face *face;
23998
23999 eassert (s);
24000
24001 s->for_overlaps = overlaps;
24002 s->face = NULL;
24003 s->font = NULL;
24004 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24005 {
24006 int c = COMPOSITION_GLYPH (s->cmp, i);
24007
24008 /* TAB in a composition means display glyphs with padding space
24009 on the left or right. */
24010 if (c != '\t')
24011 {
24012 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24013 -1, Qnil);
24014
24015 face = get_char_face_and_encoding (s->f, c, face_id,
24016 s->char2b + i, 1);
24017 if (face)
24018 {
24019 if (! s->face)
24020 {
24021 s->face = face;
24022 s->font = s->face->font;
24023 }
24024 else if (s->face != face)
24025 break;
24026 }
24027 }
24028 ++s->nchars;
24029 }
24030 s->cmp_to = i;
24031
24032 if (s->face == NULL)
24033 {
24034 s->face = base_face->ascii_face;
24035 s->font = s->face->font;
24036 }
24037
24038 /* All glyph strings for the same composition has the same width,
24039 i.e. the width set for the first component of the composition. */
24040 s->width = s->first_glyph->pixel_width;
24041
24042 /* If the specified font could not be loaded, use the frame's
24043 default font, but record the fact that we couldn't load it in
24044 the glyph string so that we can draw rectangles for the
24045 characters of the glyph string. */
24046 if (s->font == NULL)
24047 {
24048 s->font_not_found_p = 1;
24049 s->font = FRAME_FONT (s->f);
24050 }
24051
24052 /* Adjust base line for subscript/superscript text. */
24053 s->ybase += s->first_glyph->voffset;
24054
24055 /* This glyph string must always be drawn with 16-bit functions. */
24056 s->two_byte_p = 1;
24057
24058 return s->cmp_to;
24059 }
24060
24061 static int
24062 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24063 int start, int end, int overlaps)
24064 {
24065 struct glyph *glyph, *last;
24066 Lisp_Object lgstring;
24067 int i;
24068
24069 s->for_overlaps = overlaps;
24070 glyph = s->row->glyphs[s->area] + start;
24071 last = s->row->glyphs[s->area] + end;
24072 s->cmp_id = glyph->u.cmp.id;
24073 s->cmp_from = glyph->slice.cmp.from;
24074 s->cmp_to = glyph->slice.cmp.to + 1;
24075 s->face = FACE_FROM_ID (s->f, face_id);
24076 lgstring = composition_gstring_from_id (s->cmp_id);
24077 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24078 glyph++;
24079 while (glyph < last
24080 && glyph->u.cmp.automatic
24081 && glyph->u.cmp.id == s->cmp_id
24082 && s->cmp_to == glyph->slice.cmp.from)
24083 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24084
24085 for (i = s->cmp_from; i < s->cmp_to; i++)
24086 {
24087 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24088 unsigned code = LGLYPH_CODE (lglyph);
24089
24090 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24091 }
24092 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24093 return glyph - s->row->glyphs[s->area];
24094 }
24095
24096
24097 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24098 See the comment of fill_glyph_string for arguments.
24099 Value is the index of the first glyph not in S. */
24100
24101
24102 static int
24103 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24104 int start, int end, int overlaps)
24105 {
24106 struct glyph *glyph, *last;
24107 int voffset;
24108
24109 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24110 s->for_overlaps = overlaps;
24111 glyph = s->row->glyphs[s->area] + start;
24112 last = s->row->glyphs[s->area] + end;
24113 voffset = glyph->voffset;
24114 s->face = FACE_FROM_ID (s->f, face_id);
24115 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24116 s->nchars = 1;
24117 s->width = glyph->pixel_width;
24118 glyph++;
24119 while (glyph < last
24120 && glyph->type == GLYPHLESS_GLYPH
24121 && glyph->voffset == voffset
24122 && glyph->face_id == face_id)
24123 {
24124 s->nchars++;
24125 s->width += glyph->pixel_width;
24126 glyph++;
24127 }
24128 s->ybase += voffset;
24129 return glyph - s->row->glyphs[s->area];
24130 }
24131
24132
24133 /* Fill glyph string S from a sequence of character glyphs.
24134
24135 FACE_ID is the face id of the string. START is the index of the
24136 first glyph to consider, END is the index of the last + 1.
24137 OVERLAPS non-zero means S should draw the foreground only, and use
24138 its physical height for clipping. See also draw_glyphs.
24139
24140 Value is the index of the first glyph not in S. */
24141
24142 static int
24143 fill_glyph_string (struct glyph_string *s, int face_id,
24144 int start, int end, int overlaps)
24145 {
24146 struct glyph *glyph, *last;
24147 int voffset;
24148 int glyph_not_available_p;
24149
24150 eassert (s->f == XFRAME (s->w->frame));
24151 eassert (s->nchars == 0);
24152 eassert (start >= 0 && end > start);
24153
24154 s->for_overlaps = overlaps;
24155 glyph = s->row->glyphs[s->area] + start;
24156 last = s->row->glyphs[s->area] + end;
24157 voffset = glyph->voffset;
24158 s->padding_p = glyph->padding_p;
24159 glyph_not_available_p = glyph->glyph_not_available_p;
24160
24161 while (glyph < last
24162 && glyph->type == CHAR_GLYPH
24163 && glyph->voffset == voffset
24164 /* Same face id implies same font, nowadays. */
24165 && glyph->face_id == face_id
24166 && glyph->glyph_not_available_p == glyph_not_available_p)
24167 {
24168 int two_byte_p;
24169
24170 s->face = get_glyph_face_and_encoding (s->f, glyph,
24171 s->char2b + s->nchars,
24172 &two_byte_p);
24173 s->two_byte_p = two_byte_p;
24174 ++s->nchars;
24175 eassert (s->nchars <= end - start);
24176 s->width += glyph->pixel_width;
24177 if (glyph++->padding_p != s->padding_p)
24178 break;
24179 }
24180
24181 s->font = s->face->font;
24182
24183 /* If the specified font could not be loaded, use the frame's font,
24184 but record the fact that we couldn't load it in
24185 S->font_not_found_p so that we can draw rectangles for the
24186 characters of the glyph string. */
24187 if (s->font == NULL || glyph_not_available_p)
24188 {
24189 s->font_not_found_p = 1;
24190 s->font = FRAME_FONT (s->f);
24191 }
24192
24193 /* Adjust base line for subscript/superscript text. */
24194 s->ybase += voffset;
24195
24196 eassert (s->face && s->face->gc);
24197 return glyph - s->row->glyphs[s->area];
24198 }
24199
24200
24201 /* Fill glyph string S from image glyph S->first_glyph. */
24202
24203 static void
24204 fill_image_glyph_string (struct glyph_string *s)
24205 {
24206 eassert (s->first_glyph->type == IMAGE_GLYPH);
24207 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24208 eassert (s->img);
24209 s->slice = s->first_glyph->slice.img;
24210 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24211 s->font = s->face->font;
24212 s->width = s->first_glyph->pixel_width;
24213
24214 /* Adjust base line for subscript/superscript text. */
24215 s->ybase += s->first_glyph->voffset;
24216 }
24217
24218
24219 /* Fill glyph string S from a sequence of stretch glyphs.
24220
24221 START is the index of the first glyph to consider,
24222 END is the index of the last + 1.
24223
24224 Value is the index of the first glyph not in S. */
24225
24226 static int
24227 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24228 {
24229 struct glyph *glyph, *last;
24230 int voffset, face_id;
24231
24232 eassert (s->first_glyph->type == STRETCH_GLYPH);
24233
24234 glyph = s->row->glyphs[s->area] + start;
24235 last = s->row->glyphs[s->area] + end;
24236 face_id = glyph->face_id;
24237 s->face = FACE_FROM_ID (s->f, face_id);
24238 s->font = s->face->font;
24239 s->width = glyph->pixel_width;
24240 s->nchars = 1;
24241 voffset = glyph->voffset;
24242
24243 for (++glyph;
24244 (glyph < last
24245 && glyph->type == STRETCH_GLYPH
24246 && glyph->voffset == voffset
24247 && glyph->face_id == face_id);
24248 ++glyph)
24249 s->width += glyph->pixel_width;
24250
24251 /* Adjust base line for subscript/superscript text. */
24252 s->ybase += voffset;
24253
24254 /* The case that face->gc == 0 is handled when drawing the glyph
24255 string by calling PREPARE_FACE_FOR_DISPLAY. */
24256 eassert (s->face);
24257 return glyph - s->row->glyphs[s->area];
24258 }
24259
24260 static struct font_metrics *
24261 get_per_char_metric (struct font *font, XChar2b *char2b)
24262 {
24263 static struct font_metrics metrics;
24264 unsigned code;
24265
24266 if (! font)
24267 return NULL;
24268 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24269 if (code == FONT_INVALID_CODE)
24270 return NULL;
24271 font->driver->text_extents (font, &code, 1, &metrics);
24272 return &metrics;
24273 }
24274
24275 /* EXPORT for RIF:
24276 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24277 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24278 assumed to be zero. */
24279
24280 void
24281 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24282 {
24283 *left = *right = 0;
24284
24285 if (glyph->type == CHAR_GLYPH)
24286 {
24287 struct face *face;
24288 XChar2b char2b;
24289 struct font_metrics *pcm;
24290
24291 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24292 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24293 {
24294 if (pcm->rbearing > pcm->width)
24295 *right = pcm->rbearing - pcm->width;
24296 if (pcm->lbearing < 0)
24297 *left = -pcm->lbearing;
24298 }
24299 }
24300 else if (glyph->type == COMPOSITE_GLYPH)
24301 {
24302 if (! glyph->u.cmp.automatic)
24303 {
24304 struct composition *cmp = composition_table[glyph->u.cmp.id];
24305
24306 if (cmp->rbearing > cmp->pixel_width)
24307 *right = cmp->rbearing - cmp->pixel_width;
24308 if (cmp->lbearing < 0)
24309 *left = - cmp->lbearing;
24310 }
24311 else
24312 {
24313 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24314 struct font_metrics metrics;
24315
24316 composition_gstring_width (gstring, glyph->slice.cmp.from,
24317 glyph->slice.cmp.to + 1, &metrics);
24318 if (metrics.rbearing > metrics.width)
24319 *right = metrics.rbearing - metrics.width;
24320 if (metrics.lbearing < 0)
24321 *left = - metrics.lbearing;
24322 }
24323 }
24324 }
24325
24326
24327 /* Return the index of the first glyph preceding glyph string S that
24328 is overwritten by S because of S's left overhang. Value is -1
24329 if no glyphs are overwritten. */
24330
24331 static int
24332 left_overwritten (struct glyph_string *s)
24333 {
24334 int k;
24335
24336 if (s->left_overhang)
24337 {
24338 int x = 0, i;
24339 struct glyph *glyphs = s->row->glyphs[s->area];
24340 int first = s->first_glyph - glyphs;
24341
24342 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24343 x -= glyphs[i].pixel_width;
24344
24345 k = i + 1;
24346 }
24347 else
24348 k = -1;
24349
24350 return k;
24351 }
24352
24353
24354 /* Return the index of the first glyph preceding glyph string S that
24355 is overwriting S because of its right overhang. Value is -1 if no
24356 glyph in front of S overwrites S. */
24357
24358 static int
24359 left_overwriting (struct glyph_string *s)
24360 {
24361 int i, k, x;
24362 struct glyph *glyphs = s->row->glyphs[s->area];
24363 int first = s->first_glyph - glyphs;
24364
24365 k = -1;
24366 x = 0;
24367 for (i = first - 1; i >= 0; --i)
24368 {
24369 int left, right;
24370 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24371 if (x + right > 0)
24372 k = i;
24373 x -= glyphs[i].pixel_width;
24374 }
24375
24376 return k;
24377 }
24378
24379
24380 /* Return the index of the last glyph following glyph string S that is
24381 overwritten by S because of S's right overhang. Value is -1 if
24382 no such glyph is found. */
24383
24384 static int
24385 right_overwritten (struct glyph_string *s)
24386 {
24387 int k = -1;
24388
24389 if (s->right_overhang)
24390 {
24391 int x = 0, i;
24392 struct glyph *glyphs = s->row->glyphs[s->area];
24393 int first = (s->first_glyph - glyphs
24394 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24395 int end = s->row->used[s->area];
24396
24397 for (i = first; i < end && s->right_overhang > x; ++i)
24398 x += glyphs[i].pixel_width;
24399
24400 k = i;
24401 }
24402
24403 return k;
24404 }
24405
24406
24407 /* Return the index of the last glyph following glyph string S that
24408 overwrites S because of its left overhang. Value is negative
24409 if no such glyph is found. */
24410
24411 static int
24412 right_overwriting (struct glyph_string *s)
24413 {
24414 int i, k, x;
24415 int end = s->row->used[s->area];
24416 struct glyph *glyphs = s->row->glyphs[s->area];
24417 int first = (s->first_glyph - glyphs
24418 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24419
24420 k = -1;
24421 x = 0;
24422 for (i = first; i < end; ++i)
24423 {
24424 int left, right;
24425 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24426 if (x - left < 0)
24427 k = i;
24428 x += glyphs[i].pixel_width;
24429 }
24430
24431 return k;
24432 }
24433
24434
24435 /* Set background width of glyph string S. START is the index of the
24436 first glyph following S. LAST_X is the right-most x-position + 1
24437 in the drawing area. */
24438
24439 static void
24440 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24441 {
24442 /* If the face of this glyph string has to be drawn to the end of
24443 the drawing area, set S->extends_to_end_of_line_p. */
24444
24445 if (start == s->row->used[s->area]
24446 && ((s->row->fill_line_p
24447 && (s->hl == DRAW_NORMAL_TEXT
24448 || s->hl == DRAW_IMAGE_RAISED
24449 || s->hl == DRAW_IMAGE_SUNKEN))
24450 || s->hl == DRAW_MOUSE_FACE))
24451 s->extends_to_end_of_line_p = 1;
24452
24453 /* If S extends its face to the end of the line, set its
24454 background_width to the distance to the right edge of the drawing
24455 area. */
24456 if (s->extends_to_end_of_line_p)
24457 s->background_width = last_x - s->x + 1;
24458 else
24459 s->background_width = s->width;
24460 }
24461
24462
24463 /* Compute overhangs and x-positions for glyph string S and its
24464 predecessors, or successors. X is the starting x-position for S.
24465 BACKWARD_P non-zero means process predecessors. */
24466
24467 static void
24468 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24469 {
24470 if (backward_p)
24471 {
24472 while (s)
24473 {
24474 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24475 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24476 x -= s->width;
24477 s->x = x;
24478 s = s->prev;
24479 }
24480 }
24481 else
24482 {
24483 while (s)
24484 {
24485 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24486 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24487 s->x = x;
24488 x += s->width;
24489 s = s->next;
24490 }
24491 }
24492 }
24493
24494
24495
24496 /* The following macros are only called from draw_glyphs below.
24497 They reference the following parameters of that function directly:
24498 `w', `row', `area', and `overlap_p'
24499 as well as the following local variables:
24500 `s', `f', and `hdc' (in W32) */
24501
24502 #ifdef HAVE_NTGUI
24503 /* On W32, silently add local `hdc' variable to argument list of
24504 init_glyph_string. */
24505 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24506 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24507 #else
24508 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24509 init_glyph_string (s, char2b, w, row, area, start, hl)
24510 #endif
24511
24512 /* Add a glyph string for a stretch glyph to the list of strings
24513 between HEAD and TAIL. START is the index of the stretch glyph in
24514 row area AREA of glyph row ROW. END is the index of the last glyph
24515 in that glyph row area. X is the current output position assigned
24516 to the new glyph string constructed. HL overrides that face of the
24517 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24518 is the right-most x-position of the drawing area. */
24519
24520 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24521 and below -- keep them on one line. */
24522 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24523 do \
24524 { \
24525 s = alloca (sizeof *s); \
24526 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24527 START = fill_stretch_glyph_string (s, START, END); \
24528 append_glyph_string (&HEAD, &TAIL, s); \
24529 s->x = (X); \
24530 } \
24531 while (0)
24532
24533
24534 /* Add a glyph string for an image glyph to the list of strings
24535 between HEAD and TAIL. START is the index of the image glyph in
24536 row area AREA of glyph row ROW. END is the index of the last glyph
24537 in that glyph row area. X is the current output position assigned
24538 to the new glyph string constructed. HL overrides that face of the
24539 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24540 is the right-most x-position of the drawing area. */
24541
24542 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24543 do \
24544 { \
24545 s = alloca (sizeof *s); \
24546 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24547 fill_image_glyph_string (s); \
24548 append_glyph_string (&HEAD, &TAIL, s); \
24549 ++START; \
24550 s->x = (X); \
24551 } \
24552 while (0)
24553
24554
24555 /* Add a glyph string for a sequence of character glyphs to the list
24556 of strings between HEAD and TAIL. START is the index of the first
24557 glyph in row area AREA of glyph row ROW that is part of the new
24558 glyph string. END is the index of the last glyph in that glyph row
24559 area. X is the current output position assigned to the new glyph
24560 string constructed. HL overrides that face of the glyph; e.g. it
24561 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24562 right-most x-position of the drawing area. */
24563
24564 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24565 do \
24566 { \
24567 int face_id; \
24568 XChar2b *char2b; \
24569 \
24570 face_id = (row)->glyphs[area][START].face_id; \
24571 \
24572 s = alloca (sizeof *s); \
24573 char2b = alloca ((END - START) * sizeof *char2b); \
24574 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24575 append_glyph_string (&HEAD, &TAIL, s); \
24576 s->x = (X); \
24577 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24578 } \
24579 while (0)
24580
24581
24582 /* Add a glyph string for a composite sequence to the list of strings
24583 between HEAD and TAIL. START is the index of the first glyph in
24584 row area AREA of glyph row ROW that is part of the new glyph
24585 string. END is the index of the last glyph in that glyph row area.
24586 X is the current output position assigned to the new glyph string
24587 constructed. HL overrides that face of the glyph; e.g. it is
24588 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24589 x-position of the drawing area. */
24590
24591 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24592 do { \
24593 int face_id = (row)->glyphs[area][START].face_id; \
24594 struct face *base_face = FACE_FROM_ID (f, face_id); \
24595 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24596 struct composition *cmp = composition_table[cmp_id]; \
24597 XChar2b *char2b; \
24598 struct glyph_string *first_s = NULL; \
24599 int n; \
24600 \
24601 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24602 \
24603 /* Make glyph_strings for each glyph sequence that is drawable by \
24604 the same face, and append them to HEAD/TAIL. */ \
24605 for (n = 0; n < cmp->glyph_len;) \
24606 { \
24607 s = alloca (sizeof *s); \
24608 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24609 append_glyph_string (&(HEAD), &(TAIL), s); \
24610 s->cmp = cmp; \
24611 s->cmp_from = n; \
24612 s->x = (X); \
24613 if (n == 0) \
24614 first_s = s; \
24615 n = fill_composite_glyph_string (s, base_face, overlaps); \
24616 } \
24617 \
24618 ++START; \
24619 s = first_s; \
24620 } while (0)
24621
24622
24623 /* Add a glyph string for a glyph-string sequence to the list of strings
24624 between HEAD and TAIL. */
24625
24626 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24627 do { \
24628 int face_id; \
24629 XChar2b *char2b; \
24630 Lisp_Object gstring; \
24631 \
24632 face_id = (row)->glyphs[area][START].face_id; \
24633 gstring = (composition_gstring_from_id \
24634 ((row)->glyphs[area][START].u.cmp.id)); \
24635 s = alloca (sizeof *s); \
24636 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24637 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24638 append_glyph_string (&(HEAD), &(TAIL), s); \
24639 s->x = (X); \
24640 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24641 } while (0)
24642
24643
24644 /* Add a glyph string for a sequence of glyphless character's glyphs
24645 to the list of strings between HEAD and TAIL. The meanings of
24646 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24647
24648 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24649 do \
24650 { \
24651 int face_id; \
24652 \
24653 face_id = (row)->glyphs[area][START].face_id; \
24654 \
24655 s = alloca (sizeof *s); \
24656 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24657 append_glyph_string (&HEAD, &TAIL, s); \
24658 s->x = (X); \
24659 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24660 overlaps); \
24661 } \
24662 while (0)
24663
24664
24665 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24666 of AREA of glyph row ROW on window W between indices START and END.
24667 HL overrides the face for drawing glyph strings, e.g. it is
24668 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24669 x-positions of the drawing area.
24670
24671 This is an ugly monster macro construct because we must use alloca
24672 to allocate glyph strings (because draw_glyphs can be called
24673 asynchronously). */
24674
24675 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24676 do \
24677 { \
24678 HEAD = TAIL = NULL; \
24679 while (START < END) \
24680 { \
24681 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24682 switch (first_glyph->type) \
24683 { \
24684 case CHAR_GLYPH: \
24685 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24686 HL, X, LAST_X); \
24687 break; \
24688 \
24689 case COMPOSITE_GLYPH: \
24690 if (first_glyph->u.cmp.automatic) \
24691 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24692 HL, X, LAST_X); \
24693 else \
24694 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24695 HL, X, LAST_X); \
24696 break; \
24697 \
24698 case STRETCH_GLYPH: \
24699 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24700 HL, X, LAST_X); \
24701 break; \
24702 \
24703 case IMAGE_GLYPH: \
24704 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24705 HL, X, LAST_X); \
24706 break; \
24707 \
24708 case GLYPHLESS_GLYPH: \
24709 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24710 HL, X, LAST_X); \
24711 break; \
24712 \
24713 default: \
24714 emacs_abort (); \
24715 } \
24716 \
24717 if (s) \
24718 { \
24719 set_glyph_string_background_width (s, START, LAST_X); \
24720 (X) += s->width; \
24721 } \
24722 } \
24723 } while (0)
24724
24725
24726 /* Draw glyphs between START and END in AREA of ROW on window W,
24727 starting at x-position X. X is relative to AREA in W. HL is a
24728 face-override with the following meaning:
24729
24730 DRAW_NORMAL_TEXT draw normally
24731 DRAW_CURSOR draw in cursor face
24732 DRAW_MOUSE_FACE draw in mouse face.
24733 DRAW_INVERSE_VIDEO draw in mode line face
24734 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24735 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24736
24737 If OVERLAPS is non-zero, draw only the foreground of characters and
24738 clip to the physical height of ROW. Non-zero value also defines
24739 the overlapping part to be drawn:
24740
24741 OVERLAPS_PRED overlap with preceding rows
24742 OVERLAPS_SUCC overlap with succeeding rows
24743 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24744 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24745
24746 Value is the x-position reached, relative to AREA of W. */
24747
24748 static int
24749 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24750 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24751 enum draw_glyphs_face hl, int overlaps)
24752 {
24753 struct glyph_string *head, *tail;
24754 struct glyph_string *s;
24755 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24756 int i, j, x_reached, last_x, area_left = 0;
24757 struct frame *f = XFRAME (WINDOW_FRAME (w));
24758 DECLARE_HDC (hdc);
24759
24760 ALLOCATE_HDC (hdc, f);
24761
24762 /* Let's rather be paranoid than getting a SEGV. */
24763 end = min (end, row->used[area]);
24764 start = clip_to_bounds (0, start, end);
24765
24766 /* Translate X to frame coordinates. Set last_x to the right
24767 end of the drawing area. */
24768 if (row->full_width_p)
24769 {
24770 /* X is relative to the left edge of W, without scroll bars
24771 or fringes. */
24772 area_left = WINDOW_LEFT_EDGE_X (w);
24773 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24774 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24775 }
24776 else
24777 {
24778 area_left = window_box_left (w, area);
24779 last_x = area_left + window_box_width (w, area);
24780 }
24781 x += area_left;
24782
24783 /* Build a doubly-linked list of glyph_string structures between
24784 head and tail from what we have to draw. Note that the macro
24785 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24786 the reason we use a separate variable `i'. */
24787 i = start;
24788 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24789 if (tail)
24790 x_reached = tail->x + tail->background_width;
24791 else
24792 x_reached = x;
24793
24794 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24795 the row, redraw some glyphs in front or following the glyph
24796 strings built above. */
24797 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24798 {
24799 struct glyph_string *h, *t;
24800 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24801 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24802 int check_mouse_face = 0;
24803 int dummy_x = 0;
24804
24805 /* If mouse highlighting is on, we may need to draw adjacent
24806 glyphs using mouse-face highlighting. */
24807 if (area == TEXT_AREA && row->mouse_face_p
24808 && hlinfo->mouse_face_beg_row >= 0
24809 && hlinfo->mouse_face_end_row >= 0)
24810 {
24811 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24812
24813 if (row_vpos >= hlinfo->mouse_face_beg_row
24814 && row_vpos <= hlinfo->mouse_face_end_row)
24815 {
24816 check_mouse_face = 1;
24817 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24818 ? hlinfo->mouse_face_beg_col : 0;
24819 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24820 ? hlinfo->mouse_face_end_col
24821 : row->used[TEXT_AREA];
24822 }
24823 }
24824
24825 /* Compute overhangs for all glyph strings. */
24826 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24827 for (s = head; s; s = s->next)
24828 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24829
24830 /* Prepend glyph strings for glyphs in front of the first glyph
24831 string that are overwritten because of the first glyph
24832 string's left overhang. The background of all strings
24833 prepended must be drawn because the first glyph string
24834 draws over it. */
24835 i = left_overwritten (head);
24836 if (i >= 0)
24837 {
24838 enum draw_glyphs_face overlap_hl;
24839
24840 /* If this row contains mouse highlighting, attempt to draw
24841 the overlapped glyphs with the correct highlight. This
24842 code fails if the overlap encompasses more than one glyph
24843 and mouse-highlight spans only some of these glyphs.
24844 However, making it work perfectly involves a lot more
24845 code, and I don't know if the pathological case occurs in
24846 practice, so we'll stick to this for now. --- cyd */
24847 if (check_mouse_face
24848 && mouse_beg_col < start && mouse_end_col > i)
24849 overlap_hl = DRAW_MOUSE_FACE;
24850 else
24851 overlap_hl = DRAW_NORMAL_TEXT;
24852
24853 j = i;
24854 BUILD_GLYPH_STRINGS (j, start, h, t,
24855 overlap_hl, dummy_x, last_x);
24856 start = i;
24857 compute_overhangs_and_x (t, head->x, 1);
24858 prepend_glyph_string_lists (&head, &tail, h, t);
24859 clip_head = head;
24860 }
24861
24862 /* Prepend glyph strings for glyphs in front of the first glyph
24863 string that overwrite that glyph string because of their
24864 right overhang. For these strings, only the foreground must
24865 be drawn, because it draws over the glyph string at `head'.
24866 The background must not be drawn because this would overwrite
24867 right overhangs of preceding glyphs for which no glyph
24868 strings exist. */
24869 i = left_overwriting (head);
24870 if (i >= 0)
24871 {
24872 enum draw_glyphs_face overlap_hl;
24873
24874 if (check_mouse_face
24875 && mouse_beg_col < start && mouse_end_col > i)
24876 overlap_hl = DRAW_MOUSE_FACE;
24877 else
24878 overlap_hl = DRAW_NORMAL_TEXT;
24879
24880 clip_head = head;
24881 BUILD_GLYPH_STRINGS (i, start, h, t,
24882 overlap_hl, dummy_x, last_x);
24883 for (s = h; s; s = s->next)
24884 s->background_filled_p = 1;
24885 compute_overhangs_and_x (t, head->x, 1);
24886 prepend_glyph_string_lists (&head, &tail, h, t);
24887 }
24888
24889 /* Append glyphs strings for glyphs following the last glyph
24890 string tail that are overwritten by tail. The background of
24891 these strings has to be drawn because tail's foreground draws
24892 over it. */
24893 i = right_overwritten (tail);
24894 if (i >= 0)
24895 {
24896 enum draw_glyphs_face overlap_hl;
24897
24898 if (check_mouse_face
24899 && mouse_beg_col < i && mouse_end_col > end)
24900 overlap_hl = DRAW_MOUSE_FACE;
24901 else
24902 overlap_hl = DRAW_NORMAL_TEXT;
24903
24904 BUILD_GLYPH_STRINGS (end, i, h, t,
24905 overlap_hl, x, last_x);
24906 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24907 we don't have `end = i;' here. */
24908 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24909 append_glyph_string_lists (&head, &tail, h, t);
24910 clip_tail = tail;
24911 }
24912
24913 /* Append glyph strings for glyphs following the last glyph
24914 string tail that overwrite tail. The foreground of such
24915 glyphs has to be drawn because it writes into the background
24916 of tail. The background must not be drawn because it could
24917 paint over the foreground of following glyphs. */
24918 i = right_overwriting (tail);
24919 if (i >= 0)
24920 {
24921 enum draw_glyphs_face overlap_hl;
24922 if (check_mouse_face
24923 && mouse_beg_col < i && mouse_end_col > end)
24924 overlap_hl = DRAW_MOUSE_FACE;
24925 else
24926 overlap_hl = DRAW_NORMAL_TEXT;
24927
24928 clip_tail = tail;
24929 i++; /* We must include the Ith glyph. */
24930 BUILD_GLYPH_STRINGS (end, i, h, t,
24931 overlap_hl, x, last_x);
24932 for (s = h; s; s = s->next)
24933 s->background_filled_p = 1;
24934 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24935 append_glyph_string_lists (&head, &tail, h, t);
24936 }
24937 if (clip_head || clip_tail)
24938 for (s = head; s; s = s->next)
24939 {
24940 s->clip_head = clip_head;
24941 s->clip_tail = clip_tail;
24942 }
24943 }
24944
24945 /* Draw all strings. */
24946 for (s = head; s; s = s->next)
24947 FRAME_RIF (f)->draw_glyph_string (s);
24948
24949 #ifndef HAVE_NS
24950 /* When focus a sole frame and move horizontally, this sets on_p to 0
24951 causing a failure to erase prev cursor position. */
24952 if (area == TEXT_AREA
24953 && !row->full_width_p
24954 /* When drawing overlapping rows, only the glyph strings'
24955 foreground is drawn, which doesn't erase a cursor
24956 completely. */
24957 && !overlaps)
24958 {
24959 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24960 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24961 : (tail ? tail->x + tail->background_width : x));
24962 x0 -= area_left;
24963 x1 -= area_left;
24964
24965 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24966 row->y, MATRIX_ROW_BOTTOM_Y (row));
24967 }
24968 #endif
24969
24970 /* Value is the x-position up to which drawn, relative to AREA of W.
24971 This doesn't include parts drawn because of overhangs. */
24972 if (row->full_width_p)
24973 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24974 else
24975 x_reached -= area_left;
24976
24977 RELEASE_HDC (hdc, f);
24978
24979 return x_reached;
24980 }
24981
24982 /* Expand row matrix if too narrow. Don't expand if area
24983 is not present. */
24984
24985 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24986 { \
24987 if (!it->f->fonts_changed \
24988 && (it->glyph_row->glyphs[area] \
24989 < it->glyph_row->glyphs[area + 1])) \
24990 { \
24991 it->w->ncols_scale_factor++; \
24992 it->f->fonts_changed = 1; \
24993 } \
24994 }
24995
24996 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24997 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24998
24999 static void
25000 append_glyph (struct it *it)
25001 {
25002 struct glyph *glyph;
25003 enum glyph_row_area area = it->area;
25004
25005 eassert (it->glyph_row);
25006 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25007
25008 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25009 if (glyph < it->glyph_row->glyphs[area + 1])
25010 {
25011 /* If the glyph row is reversed, we need to prepend the glyph
25012 rather than append it. */
25013 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25014 {
25015 struct glyph *g;
25016
25017 /* Make room for the additional glyph. */
25018 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25019 g[1] = *g;
25020 glyph = it->glyph_row->glyphs[area];
25021 }
25022 glyph->charpos = CHARPOS (it->position);
25023 glyph->object = it->object;
25024 if (it->pixel_width > 0)
25025 {
25026 glyph->pixel_width = it->pixel_width;
25027 glyph->padding_p = 0;
25028 }
25029 else
25030 {
25031 /* Assure at least 1-pixel width. Otherwise, cursor can't
25032 be displayed correctly. */
25033 glyph->pixel_width = 1;
25034 glyph->padding_p = 1;
25035 }
25036 glyph->ascent = it->ascent;
25037 glyph->descent = it->descent;
25038 glyph->voffset = it->voffset;
25039 glyph->type = CHAR_GLYPH;
25040 glyph->avoid_cursor_p = it->avoid_cursor_p;
25041 glyph->multibyte_p = it->multibyte_p;
25042 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25043 {
25044 /* In R2L rows, the left and the right box edges need to be
25045 drawn in reverse direction. */
25046 glyph->right_box_line_p = it->start_of_box_run_p;
25047 glyph->left_box_line_p = it->end_of_box_run_p;
25048 }
25049 else
25050 {
25051 glyph->left_box_line_p = it->start_of_box_run_p;
25052 glyph->right_box_line_p = it->end_of_box_run_p;
25053 }
25054 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25055 || it->phys_descent > it->descent);
25056 glyph->glyph_not_available_p = it->glyph_not_available_p;
25057 glyph->face_id = it->face_id;
25058 glyph->u.ch = it->char_to_display;
25059 glyph->slice.img = null_glyph_slice;
25060 glyph->font_type = FONT_TYPE_UNKNOWN;
25061 if (it->bidi_p)
25062 {
25063 glyph->resolved_level = it->bidi_it.resolved_level;
25064 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25065 emacs_abort ();
25066 glyph->bidi_type = it->bidi_it.type;
25067 }
25068 else
25069 {
25070 glyph->resolved_level = 0;
25071 glyph->bidi_type = UNKNOWN_BT;
25072 }
25073 ++it->glyph_row->used[area];
25074 }
25075 else
25076 IT_EXPAND_MATRIX_WIDTH (it, area);
25077 }
25078
25079 /* Store one glyph for the composition IT->cmp_it.id in
25080 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25081 non-null. */
25082
25083 static void
25084 append_composite_glyph (struct it *it)
25085 {
25086 struct glyph *glyph;
25087 enum glyph_row_area area = it->area;
25088
25089 eassert (it->glyph_row);
25090
25091 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25092 if (glyph < it->glyph_row->glyphs[area + 1])
25093 {
25094 /* If the glyph row is reversed, we need to prepend the glyph
25095 rather than append it. */
25096 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25097 {
25098 struct glyph *g;
25099
25100 /* Make room for the new glyph. */
25101 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25102 g[1] = *g;
25103 glyph = it->glyph_row->glyphs[it->area];
25104 }
25105 glyph->charpos = it->cmp_it.charpos;
25106 glyph->object = it->object;
25107 glyph->pixel_width = it->pixel_width;
25108 glyph->ascent = it->ascent;
25109 glyph->descent = it->descent;
25110 glyph->voffset = it->voffset;
25111 glyph->type = COMPOSITE_GLYPH;
25112 if (it->cmp_it.ch < 0)
25113 {
25114 glyph->u.cmp.automatic = 0;
25115 glyph->u.cmp.id = it->cmp_it.id;
25116 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25117 }
25118 else
25119 {
25120 glyph->u.cmp.automatic = 1;
25121 glyph->u.cmp.id = it->cmp_it.id;
25122 glyph->slice.cmp.from = it->cmp_it.from;
25123 glyph->slice.cmp.to = it->cmp_it.to - 1;
25124 }
25125 glyph->avoid_cursor_p = it->avoid_cursor_p;
25126 glyph->multibyte_p = it->multibyte_p;
25127 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25128 {
25129 /* In R2L rows, the left and the right box edges need to be
25130 drawn in reverse direction. */
25131 glyph->right_box_line_p = it->start_of_box_run_p;
25132 glyph->left_box_line_p = it->end_of_box_run_p;
25133 }
25134 else
25135 {
25136 glyph->left_box_line_p = it->start_of_box_run_p;
25137 glyph->right_box_line_p = it->end_of_box_run_p;
25138 }
25139 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25140 || it->phys_descent > it->descent);
25141 glyph->padding_p = 0;
25142 glyph->glyph_not_available_p = 0;
25143 glyph->face_id = it->face_id;
25144 glyph->font_type = FONT_TYPE_UNKNOWN;
25145 if (it->bidi_p)
25146 {
25147 glyph->resolved_level = it->bidi_it.resolved_level;
25148 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25149 emacs_abort ();
25150 glyph->bidi_type = it->bidi_it.type;
25151 }
25152 ++it->glyph_row->used[area];
25153 }
25154 else
25155 IT_EXPAND_MATRIX_WIDTH (it, area);
25156 }
25157
25158
25159 /* Change IT->ascent and IT->height according to the setting of
25160 IT->voffset. */
25161
25162 static void
25163 take_vertical_position_into_account (struct it *it)
25164 {
25165 if (it->voffset)
25166 {
25167 if (it->voffset < 0)
25168 /* Increase the ascent so that we can display the text higher
25169 in the line. */
25170 it->ascent -= it->voffset;
25171 else
25172 /* Increase the descent so that we can display the text lower
25173 in the line. */
25174 it->descent += it->voffset;
25175 }
25176 }
25177
25178
25179 /* Produce glyphs/get display metrics for the image IT is loaded with.
25180 See the description of struct display_iterator in dispextern.h for
25181 an overview of struct display_iterator. */
25182
25183 static void
25184 produce_image_glyph (struct it *it)
25185 {
25186 struct image *img;
25187 struct face *face;
25188 int glyph_ascent, crop;
25189 struct glyph_slice slice;
25190
25191 eassert (it->what == IT_IMAGE);
25192
25193 face = FACE_FROM_ID (it->f, it->face_id);
25194 eassert (face);
25195 /* Make sure X resources of the face is loaded. */
25196 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25197
25198 if (it->image_id < 0)
25199 {
25200 /* Fringe bitmap. */
25201 it->ascent = it->phys_ascent = 0;
25202 it->descent = it->phys_descent = 0;
25203 it->pixel_width = 0;
25204 it->nglyphs = 0;
25205 return;
25206 }
25207
25208 img = IMAGE_FROM_ID (it->f, it->image_id);
25209 eassert (img);
25210 /* Make sure X resources of the image is loaded. */
25211 prepare_image_for_display (it->f, img);
25212
25213 slice.x = slice.y = 0;
25214 slice.width = img->width;
25215 slice.height = img->height;
25216
25217 if (INTEGERP (it->slice.x))
25218 slice.x = XINT (it->slice.x);
25219 else if (FLOATP (it->slice.x))
25220 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25221
25222 if (INTEGERP (it->slice.y))
25223 slice.y = XINT (it->slice.y);
25224 else if (FLOATP (it->slice.y))
25225 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25226
25227 if (INTEGERP (it->slice.width))
25228 slice.width = XINT (it->slice.width);
25229 else if (FLOATP (it->slice.width))
25230 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25231
25232 if (INTEGERP (it->slice.height))
25233 slice.height = XINT (it->slice.height);
25234 else if (FLOATP (it->slice.height))
25235 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25236
25237 if (slice.x >= img->width)
25238 slice.x = img->width;
25239 if (slice.y >= img->height)
25240 slice.y = img->height;
25241 if (slice.x + slice.width >= img->width)
25242 slice.width = img->width - slice.x;
25243 if (slice.y + slice.height > img->height)
25244 slice.height = img->height - slice.y;
25245
25246 if (slice.width == 0 || slice.height == 0)
25247 return;
25248
25249 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25250
25251 it->descent = slice.height - glyph_ascent;
25252 if (slice.y == 0)
25253 it->descent += img->vmargin;
25254 if (slice.y + slice.height == img->height)
25255 it->descent += img->vmargin;
25256 it->phys_descent = it->descent;
25257
25258 it->pixel_width = slice.width;
25259 if (slice.x == 0)
25260 it->pixel_width += img->hmargin;
25261 if (slice.x + slice.width == img->width)
25262 it->pixel_width += img->hmargin;
25263
25264 /* It's quite possible for images to have an ascent greater than
25265 their height, so don't get confused in that case. */
25266 if (it->descent < 0)
25267 it->descent = 0;
25268
25269 it->nglyphs = 1;
25270
25271 if (face->box != FACE_NO_BOX)
25272 {
25273 if (face->box_line_width > 0)
25274 {
25275 if (slice.y == 0)
25276 it->ascent += face->box_line_width;
25277 if (slice.y + slice.height == img->height)
25278 it->descent += face->box_line_width;
25279 }
25280
25281 if (it->start_of_box_run_p && slice.x == 0)
25282 it->pixel_width += eabs (face->box_line_width);
25283 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25284 it->pixel_width += eabs (face->box_line_width);
25285 }
25286
25287 take_vertical_position_into_account (it);
25288
25289 /* Automatically crop wide image glyphs at right edge so we can
25290 draw the cursor on same display row. */
25291 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25292 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25293 {
25294 it->pixel_width -= crop;
25295 slice.width -= crop;
25296 }
25297
25298 if (it->glyph_row)
25299 {
25300 struct glyph *glyph;
25301 enum glyph_row_area area = it->area;
25302
25303 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25304 if (glyph < it->glyph_row->glyphs[area + 1])
25305 {
25306 glyph->charpos = CHARPOS (it->position);
25307 glyph->object = it->object;
25308 glyph->pixel_width = it->pixel_width;
25309 glyph->ascent = glyph_ascent;
25310 glyph->descent = it->descent;
25311 glyph->voffset = it->voffset;
25312 glyph->type = IMAGE_GLYPH;
25313 glyph->avoid_cursor_p = it->avoid_cursor_p;
25314 glyph->multibyte_p = it->multibyte_p;
25315 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25316 {
25317 /* In R2L rows, the left and the right box edges need to be
25318 drawn in reverse direction. */
25319 glyph->right_box_line_p = it->start_of_box_run_p;
25320 glyph->left_box_line_p = it->end_of_box_run_p;
25321 }
25322 else
25323 {
25324 glyph->left_box_line_p = it->start_of_box_run_p;
25325 glyph->right_box_line_p = it->end_of_box_run_p;
25326 }
25327 glyph->overlaps_vertically_p = 0;
25328 glyph->padding_p = 0;
25329 glyph->glyph_not_available_p = 0;
25330 glyph->face_id = it->face_id;
25331 glyph->u.img_id = img->id;
25332 glyph->slice.img = slice;
25333 glyph->font_type = FONT_TYPE_UNKNOWN;
25334 if (it->bidi_p)
25335 {
25336 glyph->resolved_level = it->bidi_it.resolved_level;
25337 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25338 emacs_abort ();
25339 glyph->bidi_type = it->bidi_it.type;
25340 }
25341 ++it->glyph_row->used[area];
25342 }
25343 else
25344 IT_EXPAND_MATRIX_WIDTH (it, area);
25345 }
25346 }
25347
25348
25349 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25350 of the glyph, WIDTH and HEIGHT are the width and height of the
25351 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25352
25353 static void
25354 append_stretch_glyph (struct it *it, Lisp_Object object,
25355 int width, int height, int ascent)
25356 {
25357 struct glyph *glyph;
25358 enum glyph_row_area area = it->area;
25359
25360 eassert (ascent >= 0 && ascent <= height);
25361
25362 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25363 if (glyph < it->glyph_row->glyphs[area + 1])
25364 {
25365 /* If the glyph row is reversed, we need to prepend the glyph
25366 rather than append it. */
25367 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25368 {
25369 struct glyph *g;
25370
25371 /* Make room for the additional glyph. */
25372 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25373 g[1] = *g;
25374 glyph = it->glyph_row->glyphs[area];
25375
25376 /* Decrease the width of the first glyph of the row that
25377 begins before first_visible_x (e.g., due to hscroll).
25378 This is so the overall width of the row becomes smaller
25379 by the scroll amount, and the stretch glyph appended by
25380 extend_face_to_end_of_line will be wider, to shift the
25381 row glyphs to the right. (In L2R rows, the corresponding
25382 left-shift effect is accomplished by setting row->x to a
25383 negative value, which won't work with R2L rows.)
25384
25385 This must leave us with a positive value of WIDTH, since
25386 otherwise the call to move_it_in_display_line_to at the
25387 beginning of display_line would have got past the entire
25388 first glyph, and then it->current_x would have been
25389 greater or equal to it->first_visible_x. */
25390 if (it->current_x < it->first_visible_x)
25391 width -= it->first_visible_x - it->current_x;
25392 eassert (width > 0);
25393 }
25394 glyph->charpos = CHARPOS (it->position);
25395 glyph->object = object;
25396 glyph->pixel_width = width;
25397 glyph->ascent = ascent;
25398 glyph->descent = height - ascent;
25399 glyph->voffset = it->voffset;
25400 glyph->type = STRETCH_GLYPH;
25401 glyph->avoid_cursor_p = it->avoid_cursor_p;
25402 glyph->multibyte_p = it->multibyte_p;
25403 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25404 {
25405 /* In R2L rows, the left and the right box edges need to be
25406 drawn in reverse direction. */
25407 glyph->right_box_line_p = it->start_of_box_run_p;
25408 glyph->left_box_line_p = it->end_of_box_run_p;
25409 }
25410 else
25411 {
25412 glyph->left_box_line_p = it->start_of_box_run_p;
25413 glyph->right_box_line_p = it->end_of_box_run_p;
25414 }
25415 glyph->overlaps_vertically_p = 0;
25416 glyph->padding_p = 0;
25417 glyph->glyph_not_available_p = 0;
25418 glyph->face_id = it->face_id;
25419 glyph->u.stretch.ascent = ascent;
25420 glyph->u.stretch.height = height;
25421 glyph->slice.img = null_glyph_slice;
25422 glyph->font_type = FONT_TYPE_UNKNOWN;
25423 if (it->bidi_p)
25424 {
25425 glyph->resolved_level = it->bidi_it.resolved_level;
25426 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25427 emacs_abort ();
25428 glyph->bidi_type = it->bidi_it.type;
25429 }
25430 else
25431 {
25432 glyph->resolved_level = 0;
25433 glyph->bidi_type = UNKNOWN_BT;
25434 }
25435 ++it->glyph_row->used[area];
25436 }
25437 else
25438 IT_EXPAND_MATRIX_WIDTH (it, area);
25439 }
25440
25441 #endif /* HAVE_WINDOW_SYSTEM */
25442
25443 /* Produce a stretch glyph for iterator IT. IT->object is the value
25444 of the glyph property displayed. The value must be a list
25445 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25446 being recognized:
25447
25448 1. `:width WIDTH' specifies that the space should be WIDTH *
25449 canonical char width wide. WIDTH may be an integer or floating
25450 point number.
25451
25452 2. `:relative-width FACTOR' specifies that the width of the stretch
25453 should be computed from the width of the first character having the
25454 `glyph' property, and should be FACTOR times that width.
25455
25456 3. `:align-to HPOS' specifies that the space should be wide enough
25457 to reach HPOS, a value in canonical character units.
25458
25459 Exactly one of the above pairs must be present.
25460
25461 4. `:height HEIGHT' specifies that the height of the stretch produced
25462 should be HEIGHT, measured in canonical character units.
25463
25464 5. `:relative-height FACTOR' specifies that the height of the
25465 stretch should be FACTOR times the height of the characters having
25466 the glyph property.
25467
25468 Either none or exactly one of 4 or 5 must be present.
25469
25470 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25471 of the stretch should be used for the ascent of the stretch.
25472 ASCENT must be in the range 0 <= ASCENT <= 100. */
25473
25474 void
25475 produce_stretch_glyph (struct it *it)
25476 {
25477 /* (space :width WIDTH :height HEIGHT ...) */
25478 Lisp_Object prop, plist;
25479 int width = 0, height = 0, align_to = -1;
25480 int zero_width_ok_p = 0;
25481 double tem;
25482 struct font *font = NULL;
25483
25484 #ifdef HAVE_WINDOW_SYSTEM
25485 int ascent = 0;
25486 int zero_height_ok_p = 0;
25487
25488 if (FRAME_WINDOW_P (it->f))
25489 {
25490 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25491 font = face->font ? face->font : FRAME_FONT (it->f);
25492 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25493 }
25494 #endif
25495
25496 /* List should start with `space'. */
25497 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25498 plist = XCDR (it->object);
25499
25500 /* Compute the width of the stretch. */
25501 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25502 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25503 {
25504 /* Absolute width `:width WIDTH' specified and valid. */
25505 zero_width_ok_p = 1;
25506 width = (int)tem;
25507 }
25508 #ifdef HAVE_WINDOW_SYSTEM
25509 else if (FRAME_WINDOW_P (it->f)
25510 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25511 {
25512 /* Relative width `:relative-width FACTOR' specified and valid.
25513 Compute the width of the characters having the `glyph'
25514 property. */
25515 struct it it2;
25516 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25517
25518 it2 = *it;
25519 if (it->multibyte_p)
25520 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25521 else
25522 {
25523 it2.c = it2.char_to_display = *p, it2.len = 1;
25524 if (! ASCII_CHAR_P (it2.c))
25525 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25526 }
25527
25528 it2.glyph_row = NULL;
25529 it2.what = IT_CHARACTER;
25530 x_produce_glyphs (&it2);
25531 width = NUMVAL (prop) * it2.pixel_width;
25532 }
25533 #endif /* HAVE_WINDOW_SYSTEM */
25534 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25535 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25536 {
25537 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25538 align_to = (align_to < 0
25539 ? 0
25540 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25541 else if (align_to < 0)
25542 align_to = window_box_left_offset (it->w, TEXT_AREA);
25543 width = max (0, (int)tem + align_to - it->current_x);
25544 zero_width_ok_p = 1;
25545 }
25546 else
25547 /* Nothing specified -> width defaults to canonical char width. */
25548 width = FRAME_COLUMN_WIDTH (it->f);
25549
25550 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25551 width = 1;
25552
25553 #ifdef HAVE_WINDOW_SYSTEM
25554 /* Compute height. */
25555 if (FRAME_WINDOW_P (it->f))
25556 {
25557 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25558 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25559 {
25560 height = (int)tem;
25561 zero_height_ok_p = 1;
25562 }
25563 else if (prop = Fplist_get (plist, QCrelative_height),
25564 NUMVAL (prop) > 0)
25565 height = FONT_HEIGHT (font) * NUMVAL (prop);
25566 else
25567 height = FONT_HEIGHT (font);
25568
25569 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25570 height = 1;
25571
25572 /* Compute percentage of height used for ascent. If
25573 `:ascent ASCENT' is present and valid, use that. Otherwise,
25574 derive the ascent from the font in use. */
25575 if (prop = Fplist_get (plist, QCascent),
25576 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25577 ascent = height * NUMVAL (prop) / 100.0;
25578 else if (!NILP (prop)
25579 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25580 ascent = min (max (0, (int)tem), height);
25581 else
25582 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25583 }
25584 else
25585 #endif /* HAVE_WINDOW_SYSTEM */
25586 height = 1;
25587
25588 if (width > 0 && it->line_wrap != TRUNCATE
25589 && it->current_x + width > it->last_visible_x)
25590 {
25591 width = it->last_visible_x - it->current_x;
25592 #ifdef HAVE_WINDOW_SYSTEM
25593 /* Subtract one more pixel from the stretch width, but only on
25594 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25595 width -= FRAME_WINDOW_P (it->f);
25596 #endif
25597 }
25598
25599 if (width > 0 && height > 0 && it->glyph_row)
25600 {
25601 Lisp_Object o_object = it->object;
25602 Lisp_Object object = it->stack[it->sp - 1].string;
25603 int n = width;
25604
25605 if (!STRINGP (object))
25606 object = it->w->contents;
25607 #ifdef HAVE_WINDOW_SYSTEM
25608 if (FRAME_WINDOW_P (it->f))
25609 append_stretch_glyph (it, object, width, height, ascent);
25610 else
25611 #endif
25612 {
25613 it->object = object;
25614 it->char_to_display = ' ';
25615 it->pixel_width = it->len = 1;
25616 while (n--)
25617 tty_append_glyph (it);
25618 it->object = o_object;
25619 }
25620 }
25621
25622 it->pixel_width = width;
25623 #ifdef HAVE_WINDOW_SYSTEM
25624 if (FRAME_WINDOW_P (it->f))
25625 {
25626 it->ascent = it->phys_ascent = ascent;
25627 it->descent = it->phys_descent = height - it->ascent;
25628 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25629 take_vertical_position_into_account (it);
25630 }
25631 else
25632 #endif
25633 it->nglyphs = width;
25634 }
25635
25636 /* Get information about special display element WHAT in an
25637 environment described by IT. WHAT is one of IT_TRUNCATION or
25638 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25639 non-null glyph_row member. This function ensures that fields like
25640 face_id, c, len of IT are left untouched. */
25641
25642 static void
25643 produce_special_glyphs (struct it *it, enum display_element_type what)
25644 {
25645 struct it temp_it;
25646 Lisp_Object gc;
25647 GLYPH glyph;
25648
25649 temp_it = *it;
25650 temp_it.object = make_number (0);
25651 memset (&temp_it.current, 0, sizeof temp_it.current);
25652
25653 if (what == IT_CONTINUATION)
25654 {
25655 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25656 if (it->bidi_it.paragraph_dir == R2L)
25657 SET_GLYPH_FROM_CHAR (glyph, '/');
25658 else
25659 SET_GLYPH_FROM_CHAR (glyph, '\\');
25660 if (it->dp
25661 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25662 {
25663 /* FIXME: Should we mirror GC for R2L lines? */
25664 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25665 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25666 }
25667 }
25668 else if (what == IT_TRUNCATION)
25669 {
25670 /* Truncation glyph. */
25671 SET_GLYPH_FROM_CHAR (glyph, '$');
25672 if (it->dp
25673 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25674 {
25675 /* FIXME: Should we mirror GC for R2L lines? */
25676 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25677 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25678 }
25679 }
25680 else
25681 emacs_abort ();
25682
25683 #ifdef HAVE_WINDOW_SYSTEM
25684 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25685 is turned off, we precede the truncation/continuation glyphs by a
25686 stretch glyph whose width is computed such that these special
25687 glyphs are aligned at the window margin, even when very different
25688 fonts are used in different glyph rows. */
25689 if (FRAME_WINDOW_P (temp_it.f)
25690 /* init_iterator calls this with it->glyph_row == NULL, and it
25691 wants only the pixel width of the truncation/continuation
25692 glyphs. */
25693 && temp_it.glyph_row
25694 /* insert_left_trunc_glyphs calls us at the beginning of the
25695 row, and it has its own calculation of the stretch glyph
25696 width. */
25697 && temp_it.glyph_row->used[TEXT_AREA] > 0
25698 && (temp_it.glyph_row->reversed_p
25699 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25700 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25701 {
25702 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25703
25704 if (stretch_width > 0)
25705 {
25706 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25707 struct font *font =
25708 face->font ? face->font : FRAME_FONT (temp_it.f);
25709 int stretch_ascent =
25710 (((temp_it.ascent + temp_it.descent)
25711 * FONT_BASE (font)) / FONT_HEIGHT (font));
25712
25713 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25714 temp_it.ascent + temp_it.descent,
25715 stretch_ascent);
25716 }
25717 }
25718 #endif
25719
25720 temp_it.dp = NULL;
25721 temp_it.what = IT_CHARACTER;
25722 temp_it.len = 1;
25723 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25724 temp_it.face_id = GLYPH_FACE (glyph);
25725 temp_it.len = CHAR_BYTES (temp_it.c);
25726
25727 PRODUCE_GLYPHS (&temp_it);
25728 it->pixel_width = temp_it.pixel_width;
25729 it->nglyphs = temp_it.pixel_width;
25730 }
25731
25732 #ifdef HAVE_WINDOW_SYSTEM
25733
25734 /* Calculate line-height and line-spacing properties.
25735 An integer value specifies explicit pixel value.
25736 A float value specifies relative value to current face height.
25737 A cons (float . face-name) specifies relative value to
25738 height of specified face font.
25739
25740 Returns height in pixels, or nil. */
25741
25742
25743 static Lisp_Object
25744 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25745 int boff, int override)
25746 {
25747 Lisp_Object face_name = Qnil;
25748 int ascent, descent, height;
25749
25750 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25751 return val;
25752
25753 if (CONSP (val))
25754 {
25755 face_name = XCAR (val);
25756 val = XCDR (val);
25757 if (!NUMBERP (val))
25758 val = make_number (1);
25759 if (NILP (face_name))
25760 {
25761 height = it->ascent + it->descent;
25762 goto scale;
25763 }
25764 }
25765
25766 if (NILP (face_name))
25767 {
25768 font = FRAME_FONT (it->f);
25769 boff = FRAME_BASELINE_OFFSET (it->f);
25770 }
25771 else if (EQ (face_name, Qt))
25772 {
25773 override = 0;
25774 }
25775 else
25776 {
25777 int face_id;
25778 struct face *face;
25779
25780 face_id = lookup_named_face (it->f, face_name, 0);
25781 if (face_id < 0)
25782 return make_number (-1);
25783
25784 face = FACE_FROM_ID (it->f, face_id);
25785 font = face->font;
25786 if (font == NULL)
25787 return make_number (-1);
25788 boff = font->baseline_offset;
25789 if (font->vertical_centering)
25790 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25791 }
25792
25793 ascent = FONT_BASE (font) + boff;
25794 descent = FONT_DESCENT (font) - boff;
25795
25796 if (override)
25797 {
25798 it->override_ascent = ascent;
25799 it->override_descent = descent;
25800 it->override_boff = boff;
25801 }
25802
25803 height = ascent + descent;
25804
25805 scale:
25806 if (FLOATP (val))
25807 height = (int)(XFLOAT_DATA (val) * height);
25808 else if (INTEGERP (val))
25809 height *= XINT (val);
25810
25811 return make_number (height);
25812 }
25813
25814
25815 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25816 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25817 and only if this is for a character for which no font was found.
25818
25819 If the display method (it->glyphless_method) is
25820 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25821 length of the acronym or the hexadecimal string, UPPER_XOFF and
25822 UPPER_YOFF are pixel offsets for the upper part of the string,
25823 LOWER_XOFF and LOWER_YOFF are for the lower part.
25824
25825 For the other display methods, LEN through LOWER_YOFF are zero. */
25826
25827 static void
25828 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25829 short upper_xoff, short upper_yoff,
25830 short lower_xoff, short lower_yoff)
25831 {
25832 struct glyph *glyph;
25833 enum glyph_row_area area = it->area;
25834
25835 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25836 if (glyph < it->glyph_row->glyphs[area + 1])
25837 {
25838 /* If the glyph row is reversed, we need to prepend the glyph
25839 rather than append it. */
25840 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25841 {
25842 struct glyph *g;
25843
25844 /* Make room for the additional glyph. */
25845 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25846 g[1] = *g;
25847 glyph = it->glyph_row->glyphs[area];
25848 }
25849 glyph->charpos = CHARPOS (it->position);
25850 glyph->object = it->object;
25851 glyph->pixel_width = it->pixel_width;
25852 glyph->ascent = it->ascent;
25853 glyph->descent = it->descent;
25854 glyph->voffset = it->voffset;
25855 glyph->type = GLYPHLESS_GLYPH;
25856 glyph->u.glyphless.method = it->glyphless_method;
25857 glyph->u.glyphless.for_no_font = for_no_font;
25858 glyph->u.glyphless.len = len;
25859 glyph->u.glyphless.ch = it->c;
25860 glyph->slice.glyphless.upper_xoff = upper_xoff;
25861 glyph->slice.glyphless.upper_yoff = upper_yoff;
25862 glyph->slice.glyphless.lower_xoff = lower_xoff;
25863 glyph->slice.glyphless.lower_yoff = lower_yoff;
25864 glyph->avoid_cursor_p = it->avoid_cursor_p;
25865 glyph->multibyte_p = it->multibyte_p;
25866 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25867 {
25868 /* In R2L rows, the left and the right box edges need to be
25869 drawn in reverse direction. */
25870 glyph->right_box_line_p = it->start_of_box_run_p;
25871 glyph->left_box_line_p = it->end_of_box_run_p;
25872 }
25873 else
25874 {
25875 glyph->left_box_line_p = it->start_of_box_run_p;
25876 glyph->right_box_line_p = it->end_of_box_run_p;
25877 }
25878 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25879 || it->phys_descent > it->descent);
25880 glyph->padding_p = 0;
25881 glyph->glyph_not_available_p = 0;
25882 glyph->face_id = face_id;
25883 glyph->font_type = FONT_TYPE_UNKNOWN;
25884 if (it->bidi_p)
25885 {
25886 glyph->resolved_level = it->bidi_it.resolved_level;
25887 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25888 emacs_abort ();
25889 glyph->bidi_type = it->bidi_it.type;
25890 }
25891 ++it->glyph_row->used[area];
25892 }
25893 else
25894 IT_EXPAND_MATRIX_WIDTH (it, area);
25895 }
25896
25897
25898 /* Produce a glyph for a glyphless character for iterator IT.
25899 IT->glyphless_method specifies which method to use for displaying
25900 the character. See the description of enum
25901 glyphless_display_method in dispextern.h for the detail.
25902
25903 FOR_NO_FONT is nonzero if and only if this is for a character for
25904 which no font was found. ACRONYM, if non-nil, is an acronym string
25905 for the character. */
25906
25907 static void
25908 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25909 {
25910 int face_id;
25911 struct face *face;
25912 struct font *font;
25913 int base_width, base_height, width, height;
25914 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25915 int len;
25916
25917 /* Get the metrics of the base font. We always refer to the current
25918 ASCII face. */
25919 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25920 font = face->font ? face->font : FRAME_FONT (it->f);
25921 it->ascent = FONT_BASE (font) + font->baseline_offset;
25922 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25923 base_height = it->ascent + it->descent;
25924 base_width = font->average_width;
25925
25926 face_id = merge_glyphless_glyph_face (it);
25927
25928 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25929 {
25930 it->pixel_width = THIN_SPACE_WIDTH;
25931 len = 0;
25932 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25933 }
25934 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25935 {
25936 width = CHAR_WIDTH (it->c);
25937 if (width == 0)
25938 width = 1;
25939 else if (width > 4)
25940 width = 4;
25941 it->pixel_width = base_width * width;
25942 len = 0;
25943 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25944 }
25945 else
25946 {
25947 char buf[7];
25948 const char *str;
25949 unsigned int code[6];
25950 int upper_len;
25951 int ascent, descent;
25952 struct font_metrics metrics_upper, metrics_lower;
25953
25954 face = FACE_FROM_ID (it->f, face_id);
25955 font = face->font ? face->font : FRAME_FONT (it->f);
25956 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25957
25958 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25959 {
25960 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25961 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25962 if (CONSP (acronym))
25963 acronym = XCAR (acronym);
25964 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25965 }
25966 else
25967 {
25968 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25969 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25970 str = buf;
25971 }
25972 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25973 code[len] = font->driver->encode_char (font, str[len]);
25974 upper_len = (len + 1) / 2;
25975 font->driver->text_extents (font, code, upper_len,
25976 &metrics_upper);
25977 font->driver->text_extents (font, code + upper_len, len - upper_len,
25978 &metrics_lower);
25979
25980
25981
25982 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25983 width = max (metrics_upper.width, metrics_lower.width) + 4;
25984 upper_xoff = upper_yoff = 2; /* the typical case */
25985 if (base_width >= width)
25986 {
25987 /* Align the upper to the left, the lower to the right. */
25988 it->pixel_width = base_width;
25989 lower_xoff = base_width - 2 - metrics_lower.width;
25990 }
25991 else
25992 {
25993 /* Center the shorter one. */
25994 it->pixel_width = width;
25995 if (metrics_upper.width >= metrics_lower.width)
25996 lower_xoff = (width - metrics_lower.width) / 2;
25997 else
25998 {
25999 /* FIXME: This code doesn't look right. It formerly was
26000 missing the "lower_xoff = 0;", which couldn't have
26001 been right since it left lower_xoff uninitialized. */
26002 lower_xoff = 0;
26003 upper_xoff = (width - metrics_upper.width) / 2;
26004 }
26005 }
26006
26007 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26008 top, bottom, and between upper and lower strings. */
26009 height = (metrics_upper.ascent + metrics_upper.descent
26010 + metrics_lower.ascent + metrics_lower.descent) + 5;
26011 /* Center vertically.
26012 H:base_height, D:base_descent
26013 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26014
26015 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26016 descent = D - H/2 + h/2;
26017 lower_yoff = descent - 2 - ld;
26018 upper_yoff = lower_yoff - la - 1 - ud; */
26019 ascent = - (it->descent - (base_height + height + 1) / 2);
26020 descent = it->descent - (base_height - height) / 2;
26021 lower_yoff = descent - 2 - metrics_lower.descent;
26022 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26023 - metrics_upper.descent);
26024 /* Don't make the height shorter than the base height. */
26025 if (height > base_height)
26026 {
26027 it->ascent = ascent;
26028 it->descent = descent;
26029 }
26030 }
26031
26032 it->phys_ascent = it->ascent;
26033 it->phys_descent = it->descent;
26034 if (it->glyph_row)
26035 append_glyphless_glyph (it, face_id, for_no_font, len,
26036 upper_xoff, upper_yoff,
26037 lower_xoff, lower_yoff);
26038 it->nglyphs = 1;
26039 take_vertical_position_into_account (it);
26040 }
26041
26042
26043 /* RIF:
26044 Produce glyphs/get display metrics for the display element IT is
26045 loaded with. See the description of struct it in dispextern.h
26046 for an overview of struct it. */
26047
26048 void
26049 x_produce_glyphs (struct it *it)
26050 {
26051 int extra_line_spacing = it->extra_line_spacing;
26052
26053 it->glyph_not_available_p = 0;
26054
26055 if (it->what == IT_CHARACTER)
26056 {
26057 XChar2b char2b;
26058 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26059 struct font *font = face->font;
26060 struct font_metrics *pcm = NULL;
26061 int boff; /* Baseline offset. */
26062
26063 if (font == NULL)
26064 {
26065 /* When no suitable font is found, display this character by
26066 the method specified in the first extra slot of
26067 Vglyphless_char_display. */
26068 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26069
26070 eassert (it->what == IT_GLYPHLESS);
26071 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26072 goto done;
26073 }
26074
26075 boff = font->baseline_offset;
26076 if (font->vertical_centering)
26077 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26078
26079 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26080 {
26081 int stretched_p;
26082
26083 it->nglyphs = 1;
26084
26085 if (it->override_ascent >= 0)
26086 {
26087 it->ascent = it->override_ascent;
26088 it->descent = it->override_descent;
26089 boff = it->override_boff;
26090 }
26091 else
26092 {
26093 it->ascent = FONT_BASE (font) + boff;
26094 it->descent = FONT_DESCENT (font) - boff;
26095 }
26096
26097 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26098 {
26099 pcm = get_per_char_metric (font, &char2b);
26100 if (pcm->width == 0
26101 && pcm->rbearing == 0 && pcm->lbearing == 0)
26102 pcm = NULL;
26103 }
26104
26105 if (pcm)
26106 {
26107 it->phys_ascent = pcm->ascent + boff;
26108 it->phys_descent = pcm->descent - boff;
26109 it->pixel_width = pcm->width;
26110 }
26111 else
26112 {
26113 it->glyph_not_available_p = 1;
26114 it->phys_ascent = it->ascent;
26115 it->phys_descent = it->descent;
26116 it->pixel_width = font->space_width;
26117 }
26118
26119 if (it->constrain_row_ascent_descent_p)
26120 {
26121 if (it->descent > it->max_descent)
26122 {
26123 it->ascent += it->descent - it->max_descent;
26124 it->descent = it->max_descent;
26125 }
26126 if (it->ascent > it->max_ascent)
26127 {
26128 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26129 it->ascent = it->max_ascent;
26130 }
26131 it->phys_ascent = min (it->phys_ascent, it->ascent);
26132 it->phys_descent = min (it->phys_descent, it->descent);
26133 extra_line_spacing = 0;
26134 }
26135
26136 /* If this is a space inside a region of text with
26137 `space-width' property, change its width. */
26138 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26139 if (stretched_p)
26140 it->pixel_width *= XFLOATINT (it->space_width);
26141
26142 /* If face has a box, add the box thickness to the character
26143 height. If character has a box line to the left and/or
26144 right, add the box line width to the character's width. */
26145 if (face->box != FACE_NO_BOX)
26146 {
26147 int thick = face->box_line_width;
26148
26149 if (thick > 0)
26150 {
26151 it->ascent += thick;
26152 it->descent += thick;
26153 }
26154 else
26155 thick = -thick;
26156
26157 if (it->start_of_box_run_p)
26158 it->pixel_width += thick;
26159 if (it->end_of_box_run_p)
26160 it->pixel_width += thick;
26161 }
26162
26163 /* If face has an overline, add the height of the overline
26164 (1 pixel) and a 1 pixel margin to the character height. */
26165 if (face->overline_p)
26166 it->ascent += overline_margin;
26167
26168 if (it->constrain_row_ascent_descent_p)
26169 {
26170 if (it->ascent > it->max_ascent)
26171 it->ascent = it->max_ascent;
26172 if (it->descent > it->max_descent)
26173 it->descent = it->max_descent;
26174 }
26175
26176 take_vertical_position_into_account (it);
26177
26178 /* If we have to actually produce glyphs, do it. */
26179 if (it->glyph_row)
26180 {
26181 if (stretched_p)
26182 {
26183 /* Translate a space with a `space-width' property
26184 into a stretch glyph. */
26185 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26186 / FONT_HEIGHT (font));
26187 append_stretch_glyph (it, it->object, it->pixel_width,
26188 it->ascent + it->descent, ascent);
26189 }
26190 else
26191 append_glyph (it);
26192
26193 /* If characters with lbearing or rbearing are displayed
26194 in this line, record that fact in a flag of the
26195 glyph row. This is used to optimize X output code. */
26196 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26197 it->glyph_row->contains_overlapping_glyphs_p = 1;
26198 }
26199 if (! stretched_p && it->pixel_width == 0)
26200 /* We assure that all visible glyphs have at least 1-pixel
26201 width. */
26202 it->pixel_width = 1;
26203 }
26204 else if (it->char_to_display == '\n')
26205 {
26206 /* A newline has no width, but we need the height of the
26207 line. But if previous part of the line sets a height,
26208 don't increase that height. */
26209
26210 Lisp_Object height;
26211 Lisp_Object total_height = Qnil;
26212
26213 it->override_ascent = -1;
26214 it->pixel_width = 0;
26215 it->nglyphs = 0;
26216
26217 height = get_it_property (it, Qline_height);
26218 /* Split (line-height total-height) list. */
26219 if (CONSP (height)
26220 && CONSP (XCDR (height))
26221 && NILP (XCDR (XCDR (height))))
26222 {
26223 total_height = XCAR (XCDR (height));
26224 height = XCAR (height);
26225 }
26226 height = calc_line_height_property (it, height, font, boff, 1);
26227
26228 if (it->override_ascent >= 0)
26229 {
26230 it->ascent = it->override_ascent;
26231 it->descent = it->override_descent;
26232 boff = it->override_boff;
26233 }
26234 else
26235 {
26236 it->ascent = FONT_BASE (font) + boff;
26237 it->descent = FONT_DESCENT (font) - boff;
26238 }
26239
26240 if (EQ (height, Qt))
26241 {
26242 if (it->descent > it->max_descent)
26243 {
26244 it->ascent += it->descent - it->max_descent;
26245 it->descent = it->max_descent;
26246 }
26247 if (it->ascent > it->max_ascent)
26248 {
26249 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26250 it->ascent = it->max_ascent;
26251 }
26252 it->phys_ascent = min (it->phys_ascent, it->ascent);
26253 it->phys_descent = min (it->phys_descent, it->descent);
26254 it->constrain_row_ascent_descent_p = 1;
26255 extra_line_spacing = 0;
26256 }
26257 else
26258 {
26259 Lisp_Object spacing;
26260
26261 it->phys_ascent = it->ascent;
26262 it->phys_descent = it->descent;
26263
26264 if ((it->max_ascent > 0 || it->max_descent > 0)
26265 && face->box != FACE_NO_BOX
26266 && face->box_line_width > 0)
26267 {
26268 it->ascent += face->box_line_width;
26269 it->descent += face->box_line_width;
26270 }
26271 if (!NILP (height)
26272 && XINT (height) > it->ascent + it->descent)
26273 it->ascent = XINT (height) - it->descent;
26274
26275 if (!NILP (total_height))
26276 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26277 else
26278 {
26279 spacing = get_it_property (it, Qline_spacing);
26280 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26281 }
26282 if (INTEGERP (spacing))
26283 {
26284 extra_line_spacing = XINT (spacing);
26285 if (!NILP (total_height))
26286 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26287 }
26288 }
26289 }
26290 else /* i.e. (it->char_to_display == '\t') */
26291 {
26292 if (font->space_width > 0)
26293 {
26294 int tab_width = it->tab_width * font->space_width;
26295 int x = it->current_x + it->continuation_lines_width;
26296 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26297
26298 /* If the distance from the current position to the next tab
26299 stop is less than a space character width, use the
26300 tab stop after that. */
26301 if (next_tab_x - x < font->space_width)
26302 next_tab_x += tab_width;
26303
26304 it->pixel_width = next_tab_x - x;
26305 it->nglyphs = 1;
26306 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26307 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26308
26309 if (it->glyph_row)
26310 {
26311 append_stretch_glyph (it, it->object, it->pixel_width,
26312 it->ascent + it->descent, it->ascent);
26313 }
26314 }
26315 else
26316 {
26317 it->pixel_width = 0;
26318 it->nglyphs = 1;
26319 }
26320 }
26321 }
26322 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26323 {
26324 /* A static composition.
26325
26326 Note: A composition is represented as one glyph in the
26327 glyph matrix. There are no padding glyphs.
26328
26329 Important note: pixel_width, ascent, and descent are the
26330 values of what is drawn by draw_glyphs (i.e. the values of
26331 the overall glyphs composed). */
26332 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26333 int boff; /* baseline offset */
26334 struct composition *cmp = composition_table[it->cmp_it.id];
26335 int glyph_len = cmp->glyph_len;
26336 struct font *font = face->font;
26337
26338 it->nglyphs = 1;
26339
26340 /* If we have not yet calculated pixel size data of glyphs of
26341 the composition for the current face font, calculate them
26342 now. Theoretically, we have to check all fonts for the
26343 glyphs, but that requires much time and memory space. So,
26344 here we check only the font of the first glyph. This may
26345 lead to incorrect display, but it's very rare, and C-l
26346 (recenter-top-bottom) can correct the display anyway. */
26347 if (! cmp->font || cmp->font != font)
26348 {
26349 /* Ascent and descent of the font of the first character
26350 of this composition (adjusted by baseline offset).
26351 Ascent and descent of overall glyphs should not be less
26352 than these, respectively. */
26353 int font_ascent, font_descent, font_height;
26354 /* Bounding box of the overall glyphs. */
26355 int leftmost, rightmost, lowest, highest;
26356 int lbearing, rbearing;
26357 int i, width, ascent, descent;
26358 int left_padded = 0, right_padded = 0;
26359 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26360 XChar2b char2b;
26361 struct font_metrics *pcm;
26362 int font_not_found_p;
26363 ptrdiff_t pos;
26364
26365 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26366 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26367 break;
26368 if (glyph_len < cmp->glyph_len)
26369 right_padded = 1;
26370 for (i = 0; i < glyph_len; i++)
26371 {
26372 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26373 break;
26374 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26375 }
26376 if (i > 0)
26377 left_padded = 1;
26378
26379 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26380 : IT_CHARPOS (*it));
26381 /* If no suitable font is found, use the default font. */
26382 font_not_found_p = font == NULL;
26383 if (font_not_found_p)
26384 {
26385 face = face->ascii_face;
26386 font = face->font;
26387 }
26388 boff = font->baseline_offset;
26389 if (font->vertical_centering)
26390 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26391 font_ascent = FONT_BASE (font) + boff;
26392 font_descent = FONT_DESCENT (font) - boff;
26393 font_height = FONT_HEIGHT (font);
26394
26395 cmp->font = font;
26396
26397 pcm = NULL;
26398 if (! font_not_found_p)
26399 {
26400 get_char_face_and_encoding (it->f, c, it->face_id,
26401 &char2b, 0);
26402 pcm = get_per_char_metric (font, &char2b);
26403 }
26404
26405 /* Initialize the bounding box. */
26406 if (pcm)
26407 {
26408 width = cmp->glyph_len > 0 ? pcm->width : 0;
26409 ascent = pcm->ascent;
26410 descent = pcm->descent;
26411 lbearing = pcm->lbearing;
26412 rbearing = pcm->rbearing;
26413 }
26414 else
26415 {
26416 width = cmp->glyph_len > 0 ? font->space_width : 0;
26417 ascent = FONT_BASE (font);
26418 descent = FONT_DESCENT (font);
26419 lbearing = 0;
26420 rbearing = width;
26421 }
26422
26423 rightmost = width;
26424 leftmost = 0;
26425 lowest = - descent + boff;
26426 highest = ascent + boff;
26427
26428 if (! font_not_found_p
26429 && font->default_ascent
26430 && CHAR_TABLE_P (Vuse_default_ascent)
26431 && !NILP (Faref (Vuse_default_ascent,
26432 make_number (it->char_to_display))))
26433 highest = font->default_ascent + boff;
26434
26435 /* Draw the first glyph at the normal position. It may be
26436 shifted to right later if some other glyphs are drawn
26437 at the left. */
26438 cmp->offsets[i * 2] = 0;
26439 cmp->offsets[i * 2 + 1] = boff;
26440 cmp->lbearing = lbearing;
26441 cmp->rbearing = rbearing;
26442
26443 /* Set cmp->offsets for the remaining glyphs. */
26444 for (i++; i < glyph_len; i++)
26445 {
26446 int left, right, btm, top;
26447 int ch = COMPOSITION_GLYPH (cmp, i);
26448 int face_id;
26449 struct face *this_face;
26450
26451 if (ch == '\t')
26452 ch = ' ';
26453 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26454 this_face = FACE_FROM_ID (it->f, face_id);
26455 font = this_face->font;
26456
26457 if (font == NULL)
26458 pcm = NULL;
26459 else
26460 {
26461 get_char_face_and_encoding (it->f, ch, face_id,
26462 &char2b, 0);
26463 pcm = get_per_char_metric (font, &char2b);
26464 }
26465 if (! pcm)
26466 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26467 else
26468 {
26469 width = pcm->width;
26470 ascent = pcm->ascent;
26471 descent = pcm->descent;
26472 lbearing = pcm->lbearing;
26473 rbearing = pcm->rbearing;
26474 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26475 {
26476 /* Relative composition with or without
26477 alternate chars. */
26478 left = (leftmost + rightmost - width) / 2;
26479 btm = - descent + boff;
26480 if (font->relative_compose
26481 && (! CHAR_TABLE_P (Vignore_relative_composition)
26482 || NILP (Faref (Vignore_relative_composition,
26483 make_number (ch)))))
26484 {
26485
26486 if (- descent >= font->relative_compose)
26487 /* One extra pixel between two glyphs. */
26488 btm = highest + 1;
26489 else if (ascent <= 0)
26490 /* One extra pixel between two glyphs. */
26491 btm = lowest - 1 - ascent - descent;
26492 }
26493 }
26494 else
26495 {
26496 /* A composition rule is specified by an integer
26497 value that encodes global and new reference
26498 points (GREF and NREF). GREF and NREF are
26499 specified by numbers as below:
26500
26501 0---1---2 -- ascent
26502 | |
26503 | |
26504 | |
26505 9--10--11 -- center
26506 | |
26507 ---3---4---5--- baseline
26508 | |
26509 6---7---8 -- descent
26510 */
26511 int rule = COMPOSITION_RULE (cmp, i);
26512 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26513
26514 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26515 grefx = gref % 3, nrefx = nref % 3;
26516 grefy = gref / 3, nrefy = nref / 3;
26517 if (xoff)
26518 xoff = font_height * (xoff - 128) / 256;
26519 if (yoff)
26520 yoff = font_height * (yoff - 128) / 256;
26521
26522 left = (leftmost
26523 + grefx * (rightmost - leftmost) / 2
26524 - nrefx * width / 2
26525 + xoff);
26526
26527 btm = ((grefy == 0 ? highest
26528 : grefy == 1 ? 0
26529 : grefy == 2 ? lowest
26530 : (highest + lowest) / 2)
26531 - (nrefy == 0 ? ascent + descent
26532 : nrefy == 1 ? descent - boff
26533 : nrefy == 2 ? 0
26534 : (ascent + descent) / 2)
26535 + yoff);
26536 }
26537
26538 cmp->offsets[i * 2] = left;
26539 cmp->offsets[i * 2 + 1] = btm + descent;
26540
26541 /* Update the bounding box of the overall glyphs. */
26542 if (width > 0)
26543 {
26544 right = left + width;
26545 if (left < leftmost)
26546 leftmost = left;
26547 if (right > rightmost)
26548 rightmost = right;
26549 }
26550 top = btm + descent + ascent;
26551 if (top > highest)
26552 highest = top;
26553 if (btm < lowest)
26554 lowest = btm;
26555
26556 if (cmp->lbearing > left + lbearing)
26557 cmp->lbearing = left + lbearing;
26558 if (cmp->rbearing < left + rbearing)
26559 cmp->rbearing = left + rbearing;
26560 }
26561 }
26562
26563 /* If there are glyphs whose x-offsets are negative,
26564 shift all glyphs to the right and make all x-offsets
26565 non-negative. */
26566 if (leftmost < 0)
26567 {
26568 for (i = 0; i < cmp->glyph_len; i++)
26569 cmp->offsets[i * 2] -= leftmost;
26570 rightmost -= leftmost;
26571 cmp->lbearing -= leftmost;
26572 cmp->rbearing -= leftmost;
26573 }
26574
26575 if (left_padded && cmp->lbearing < 0)
26576 {
26577 for (i = 0; i < cmp->glyph_len; i++)
26578 cmp->offsets[i * 2] -= cmp->lbearing;
26579 rightmost -= cmp->lbearing;
26580 cmp->rbearing -= cmp->lbearing;
26581 cmp->lbearing = 0;
26582 }
26583 if (right_padded && rightmost < cmp->rbearing)
26584 {
26585 rightmost = cmp->rbearing;
26586 }
26587
26588 cmp->pixel_width = rightmost;
26589 cmp->ascent = highest;
26590 cmp->descent = - lowest;
26591 if (cmp->ascent < font_ascent)
26592 cmp->ascent = font_ascent;
26593 if (cmp->descent < font_descent)
26594 cmp->descent = font_descent;
26595 }
26596
26597 if (it->glyph_row
26598 && (cmp->lbearing < 0
26599 || cmp->rbearing > cmp->pixel_width))
26600 it->glyph_row->contains_overlapping_glyphs_p = 1;
26601
26602 it->pixel_width = cmp->pixel_width;
26603 it->ascent = it->phys_ascent = cmp->ascent;
26604 it->descent = it->phys_descent = cmp->descent;
26605 if (face->box != FACE_NO_BOX)
26606 {
26607 int thick = face->box_line_width;
26608
26609 if (thick > 0)
26610 {
26611 it->ascent += thick;
26612 it->descent += thick;
26613 }
26614 else
26615 thick = - thick;
26616
26617 if (it->start_of_box_run_p)
26618 it->pixel_width += thick;
26619 if (it->end_of_box_run_p)
26620 it->pixel_width += thick;
26621 }
26622
26623 /* If face has an overline, add the height of the overline
26624 (1 pixel) and a 1 pixel margin to the character height. */
26625 if (face->overline_p)
26626 it->ascent += overline_margin;
26627
26628 take_vertical_position_into_account (it);
26629 if (it->ascent < 0)
26630 it->ascent = 0;
26631 if (it->descent < 0)
26632 it->descent = 0;
26633
26634 if (it->glyph_row && cmp->glyph_len > 0)
26635 append_composite_glyph (it);
26636 }
26637 else if (it->what == IT_COMPOSITION)
26638 {
26639 /* A dynamic (automatic) composition. */
26640 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26641 Lisp_Object gstring;
26642 struct font_metrics metrics;
26643
26644 it->nglyphs = 1;
26645
26646 gstring = composition_gstring_from_id (it->cmp_it.id);
26647 it->pixel_width
26648 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26649 &metrics);
26650 if (it->glyph_row
26651 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26652 it->glyph_row->contains_overlapping_glyphs_p = 1;
26653 it->ascent = it->phys_ascent = metrics.ascent;
26654 it->descent = it->phys_descent = metrics.descent;
26655 if (face->box != FACE_NO_BOX)
26656 {
26657 int thick = face->box_line_width;
26658
26659 if (thick > 0)
26660 {
26661 it->ascent += thick;
26662 it->descent += thick;
26663 }
26664 else
26665 thick = - thick;
26666
26667 if (it->start_of_box_run_p)
26668 it->pixel_width += thick;
26669 if (it->end_of_box_run_p)
26670 it->pixel_width += thick;
26671 }
26672 /* If face has an overline, add the height of the overline
26673 (1 pixel) and a 1 pixel margin to the character height. */
26674 if (face->overline_p)
26675 it->ascent += overline_margin;
26676 take_vertical_position_into_account (it);
26677 if (it->ascent < 0)
26678 it->ascent = 0;
26679 if (it->descent < 0)
26680 it->descent = 0;
26681
26682 if (it->glyph_row)
26683 append_composite_glyph (it);
26684 }
26685 else if (it->what == IT_GLYPHLESS)
26686 produce_glyphless_glyph (it, 0, Qnil);
26687 else if (it->what == IT_IMAGE)
26688 produce_image_glyph (it);
26689 else if (it->what == IT_STRETCH)
26690 produce_stretch_glyph (it);
26691
26692 done:
26693 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26694 because this isn't true for images with `:ascent 100'. */
26695 eassert (it->ascent >= 0 && it->descent >= 0);
26696 if (it->area == TEXT_AREA)
26697 it->current_x += it->pixel_width;
26698
26699 if (extra_line_spacing > 0)
26700 {
26701 it->descent += extra_line_spacing;
26702 if (extra_line_spacing > it->max_extra_line_spacing)
26703 it->max_extra_line_spacing = extra_line_spacing;
26704 }
26705
26706 it->max_ascent = max (it->max_ascent, it->ascent);
26707 it->max_descent = max (it->max_descent, it->descent);
26708 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26709 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26710 }
26711
26712 /* EXPORT for RIF:
26713 Output LEN glyphs starting at START at the nominal cursor position.
26714 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26715 being updated, and UPDATED_AREA is the area of that row being updated. */
26716
26717 void
26718 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26719 struct glyph *start, enum glyph_row_area updated_area, int len)
26720 {
26721 int x, hpos, chpos = w->phys_cursor.hpos;
26722
26723 eassert (updated_row);
26724 /* When the window is hscrolled, cursor hpos can legitimately be out
26725 of bounds, but we draw the cursor at the corresponding window
26726 margin in that case. */
26727 if (!updated_row->reversed_p && chpos < 0)
26728 chpos = 0;
26729 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26730 chpos = updated_row->used[TEXT_AREA] - 1;
26731
26732 block_input ();
26733
26734 /* Write glyphs. */
26735
26736 hpos = start - updated_row->glyphs[updated_area];
26737 x = draw_glyphs (w, w->output_cursor.x,
26738 updated_row, updated_area,
26739 hpos, hpos + len,
26740 DRAW_NORMAL_TEXT, 0);
26741
26742 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26743 if (updated_area == TEXT_AREA
26744 && w->phys_cursor_on_p
26745 && w->phys_cursor.vpos == w->output_cursor.vpos
26746 && chpos >= hpos
26747 && chpos < hpos + len)
26748 w->phys_cursor_on_p = 0;
26749
26750 unblock_input ();
26751
26752 /* Advance the output cursor. */
26753 w->output_cursor.hpos += len;
26754 w->output_cursor.x = x;
26755 }
26756
26757
26758 /* EXPORT for RIF:
26759 Insert LEN glyphs from START at the nominal cursor position. */
26760
26761 void
26762 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26763 struct glyph *start, enum glyph_row_area updated_area, int len)
26764 {
26765 struct frame *f;
26766 int line_height, shift_by_width, shifted_region_width;
26767 struct glyph_row *row;
26768 struct glyph *glyph;
26769 int frame_x, frame_y;
26770 ptrdiff_t hpos;
26771
26772 eassert (updated_row);
26773 block_input ();
26774 f = XFRAME (WINDOW_FRAME (w));
26775
26776 /* Get the height of the line we are in. */
26777 row = updated_row;
26778 line_height = row->height;
26779
26780 /* Get the width of the glyphs to insert. */
26781 shift_by_width = 0;
26782 for (glyph = start; glyph < start + len; ++glyph)
26783 shift_by_width += glyph->pixel_width;
26784
26785 /* Get the width of the region to shift right. */
26786 shifted_region_width = (window_box_width (w, updated_area)
26787 - w->output_cursor.x
26788 - shift_by_width);
26789
26790 /* Shift right. */
26791 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26792 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26793
26794 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26795 line_height, shift_by_width);
26796
26797 /* Write the glyphs. */
26798 hpos = start - row->glyphs[updated_area];
26799 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26800 hpos, hpos + len,
26801 DRAW_NORMAL_TEXT, 0);
26802
26803 /* Advance the output cursor. */
26804 w->output_cursor.hpos += len;
26805 w->output_cursor.x += shift_by_width;
26806 unblock_input ();
26807 }
26808
26809
26810 /* EXPORT for RIF:
26811 Erase the current text line from the nominal cursor position
26812 (inclusive) to pixel column TO_X (exclusive). The idea is that
26813 everything from TO_X onward is already erased.
26814
26815 TO_X is a pixel position relative to UPDATED_AREA of currently
26816 updated window W. TO_X == -1 means clear to the end of this area. */
26817
26818 void
26819 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26820 enum glyph_row_area updated_area, int to_x)
26821 {
26822 struct frame *f;
26823 int max_x, min_y, max_y;
26824 int from_x, from_y, to_y;
26825
26826 eassert (updated_row);
26827 f = XFRAME (w->frame);
26828
26829 if (updated_row->full_width_p)
26830 max_x = (WINDOW_PIXEL_WIDTH (w)
26831 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26832 else
26833 max_x = window_box_width (w, updated_area);
26834 max_y = window_text_bottom_y (w);
26835
26836 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26837 of window. For TO_X > 0, truncate to end of drawing area. */
26838 if (to_x == 0)
26839 return;
26840 else if (to_x < 0)
26841 to_x = max_x;
26842 else
26843 to_x = min (to_x, max_x);
26844
26845 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26846
26847 /* Notice if the cursor will be cleared by this operation. */
26848 if (!updated_row->full_width_p)
26849 notice_overwritten_cursor (w, updated_area,
26850 w->output_cursor.x, -1,
26851 updated_row->y,
26852 MATRIX_ROW_BOTTOM_Y (updated_row));
26853
26854 from_x = w->output_cursor.x;
26855
26856 /* Translate to frame coordinates. */
26857 if (updated_row->full_width_p)
26858 {
26859 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26860 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26861 }
26862 else
26863 {
26864 int area_left = window_box_left (w, updated_area);
26865 from_x += area_left;
26866 to_x += area_left;
26867 }
26868
26869 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26870 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26871 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26872
26873 /* Prevent inadvertently clearing to end of the X window. */
26874 if (to_x > from_x && to_y > from_y)
26875 {
26876 block_input ();
26877 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26878 to_x - from_x, to_y - from_y);
26879 unblock_input ();
26880 }
26881 }
26882
26883 #endif /* HAVE_WINDOW_SYSTEM */
26884
26885
26886 \f
26887 /***********************************************************************
26888 Cursor types
26889 ***********************************************************************/
26890
26891 /* Value is the internal representation of the specified cursor type
26892 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26893 of the bar cursor. */
26894
26895 static enum text_cursor_kinds
26896 get_specified_cursor_type (Lisp_Object arg, int *width)
26897 {
26898 enum text_cursor_kinds type;
26899
26900 if (NILP (arg))
26901 return NO_CURSOR;
26902
26903 if (EQ (arg, Qbox))
26904 return FILLED_BOX_CURSOR;
26905
26906 if (EQ (arg, Qhollow))
26907 return HOLLOW_BOX_CURSOR;
26908
26909 if (EQ (arg, Qbar))
26910 {
26911 *width = 2;
26912 return BAR_CURSOR;
26913 }
26914
26915 if (CONSP (arg)
26916 && EQ (XCAR (arg), Qbar)
26917 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26918 {
26919 *width = XINT (XCDR (arg));
26920 return BAR_CURSOR;
26921 }
26922
26923 if (EQ (arg, Qhbar))
26924 {
26925 *width = 2;
26926 return HBAR_CURSOR;
26927 }
26928
26929 if (CONSP (arg)
26930 && EQ (XCAR (arg), Qhbar)
26931 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26932 {
26933 *width = XINT (XCDR (arg));
26934 return HBAR_CURSOR;
26935 }
26936
26937 /* Treat anything unknown as "hollow box cursor".
26938 It was bad to signal an error; people have trouble fixing
26939 .Xdefaults with Emacs, when it has something bad in it. */
26940 type = HOLLOW_BOX_CURSOR;
26941
26942 return type;
26943 }
26944
26945 /* Set the default cursor types for specified frame. */
26946 void
26947 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26948 {
26949 int width = 1;
26950 Lisp_Object tem;
26951
26952 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26953 FRAME_CURSOR_WIDTH (f) = width;
26954
26955 /* By default, set up the blink-off state depending on the on-state. */
26956
26957 tem = Fassoc (arg, Vblink_cursor_alist);
26958 if (!NILP (tem))
26959 {
26960 FRAME_BLINK_OFF_CURSOR (f)
26961 = get_specified_cursor_type (XCDR (tem), &width);
26962 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26963 }
26964 else
26965 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26966
26967 /* Make sure the cursor gets redrawn. */
26968 f->cursor_type_changed = 1;
26969 }
26970
26971
26972 #ifdef HAVE_WINDOW_SYSTEM
26973
26974 /* Return the cursor we want to be displayed in window W. Return
26975 width of bar/hbar cursor through WIDTH arg. Return with
26976 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26977 (i.e. if the `system caret' should track this cursor).
26978
26979 In a mini-buffer window, we want the cursor only to appear if we
26980 are reading input from this window. For the selected window, we
26981 want the cursor type given by the frame parameter or buffer local
26982 setting of cursor-type. If explicitly marked off, draw no cursor.
26983 In all other cases, we want a hollow box cursor. */
26984
26985 static enum text_cursor_kinds
26986 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26987 int *active_cursor)
26988 {
26989 struct frame *f = XFRAME (w->frame);
26990 struct buffer *b = XBUFFER (w->contents);
26991 int cursor_type = DEFAULT_CURSOR;
26992 Lisp_Object alt_cursor;
26993 int non_selected = 0;
26994
26995 *active_cursor = 1;
26996
26997 /* Echo area */
26998 if (cursor_in_echo_area
26999 && FRAME_HAS_MINIBUF_P (f)
27000 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27001 {
27002 if (w == XWINDOW (echo_area_window))
27003 {
27004 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27005 {
27006 *width = FRAME_CURSOR_WIDTH (f);
27007 return FRAME_DESIRED_CURSOR (f);
27008 }
27009 else
27010 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27011 }
27012
27013 *active_cursor = 0;
27014 non_selected = 1;
27015 }
27016
27017 /* Detect a nonselected window or nonselected frame. */
27018 else if (w != XWINDOW (f->selected_window)
27019 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27020 {
27021 *active_cursor = 0;
27022
27023 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27024 return NO_CURSOR;
27025
27026 non_selected = 1;
27027 }
27028
27029 /* Never display a cursor in a window in which cursor-type is nil. */
27030 if (NILP (BVAR (b, cursor_type)))
27031 return NO_CURSOR;
27032
27033 /* Get the normal cursor type for this window. */
27034 if (EQ (BVAR (b, cursor_type), Qt))
27035 {
27036 cursor_type = FRAME_DESIRED_CURSOR (f);
27037 *width = FRAME_CURSOR_WIDTH (f);
27038 }
27039 else
27040 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27041
27042 /* Use cursor-in-non-selected-windows instead
27043 for non-selected window or frame. */
27044 if (non_selected)
27045 {
27046 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27047 if (!EQ (Qt, alt_cursor))
27048 return get_specified_cursor_type (alt_cursor, width);
27049 /* t means modify the normal cursor type. */
27050 if (cursor_type == FILLED_BOX_CURSOR)
27051 cursor_type = HOLLOW_BOX_CURSOR;
27052 else if (cursor_type == BAR_CURSOR && *width > 1)
27053 --*width;
27054 return cursor_type;
27055 }
27056
27057 /* Use normal cursor if not blinked off. */
27058 if (!w->cursor_off_p)
27059 {
27060 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27061 {
27062 if (cursor_type == FILLED_BOX_CURSOR)
27063 {
27064 /* Using a block cursor on large images can be very annoying.
27065 So use a hollow cursor for "large" images.
27066 If image is not transparent (no mask), also use hollow cursor. */
27067 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27068 if (img != NULL && IMAGEP (img->spec))
27069 {
27070 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27071 where N = size of default frame font size.
27072 This should cover most of the "tiny" icons people may use. */
27073 if (!img->mask
27074 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27075 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27076 cursor_type = HOLLOW_BOX_CURSOR;
27077 }
27078 }
27079 else if (cursor_type != NO_CURSOR)
27080 {
27081 /* Display current only supports BOX and HOLLOW cursors for images.
27082 So for now, unconditionally use a HOLLOW cursor when cursor is
27083 not a solid box cursor. */
27084 cursor_type = HOLLOW_BOX_CURSOR;
27085 }
27086 }
27087 return cursor_type;
27088 }
27089
27090 /* Cursor is blinked off, so determine how to "toggle" it. */
27091
27092 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27093 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27094 return get_specified_cursor_type (XCDR (alt_cursor), width);
27095
27096 /* Then see if frame has specified a specific blink off cursor type. */
27097 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27098 {
27099 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27100 return FRAME_BLINK_OFF_CURSOR (f);
27101 }
27102
27103 #if 0
27104 /* Some people liked having a permanently visible blinking cursor,
27105 while others had very strong opinions against it. So it was
27106 decided to remove it. KFS 2003-09-03 */
27107
27108 /* Finally perform built-in cursor blinking:
27109 filled box <-> hollow box
27110 wide [h]bar <-> narrow [h]bar
27111 narrow [h]bar <-> no cursor
27112 other type <-> no cursor */
27113
27114 if (cursor_type == FILLED_BOX_CURSOR)
27115 return HOLLOW_BOX_CURSOR;
27116
27117 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27118 {
27119 *width = 1;
27120 return cursor_type;
27121 }
27122 #endif
27123
27124 return NO_CURSOR;
27125 }
27126
27127
27128 /* Notice when the text cursor of window W has been completely
27129 overwritten by a drawing operation that outputs glyphs in AREA
27130 starting at X0 and ending at X1 in the line starting at Y0 and
27131 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27132 the rest of the line after X0 has been written. Y coordinates
27133 are window-relative. */
27134
27135 static void
27136 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27137 int x0, int x1, int y0, int y1)
27138 {
27139 int cx0, cx1, cy0, cy1;
27140 struct glyph_row *row;
27141
27142 if (!w->phys_cursor_on_p)
27143 return;
27144 if (area != TEXT_AREA)
27145 return;
27146
27147 if (w->phys_cursor.vpos < 0
27148 || w->phys_cursor.vpos >= w->current_matrix->nrows
27149 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27150 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27151 return;
27152
27153 if (row->cursor_in_fringe_p)
27154 {
27155 row->cursor_in_fringe_p = 0;
27156 draw_fringe_bitmap (w, row, row->reversed_p);
27157 w->phys_cursor_on_p = 0;
27158 return;
27159 }
27160
27161 cx0 = w->phys_cursor.x;
27162 cx1 = cx0 + w->phys_cursor_width;
27163 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27164 return;
27165
27166 /* The cursor image will be completely removed from the
27167 screen if the output area intersects the cursor area in
27168 y-direction. When we draw in [y0 y1[, and some part of
27169 the cursor is at y < y0, that part must have been drawn
27170 before. When scrolling, the cursor is erased before
27171 actually scrolling, so we don't come here. When not
27172 scrolling, the rows above the old cursor row must have
27173 changed, and in this case these rows must have written
27174 over the cursor image.
27175
27176 Likewise if part of the cursor is below y1, with the
27177 exception of the cursor being in the first blank row at
27178 the buffer and window end because update_text_area
27179 doesn't draw that row. (Except when it does, but
27180 that's handled in update_text_area.) */
27181
27182 cy0 = w->phys_cursor.y;
27183 cy1 = cy0 + w->phys_cursor_height;
27184 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27185 return;
27186
27187 w->phys_cursor_on_p = 0;
27188 }
27189
27190 #endif /* HAVE_WINDOW_SYSTEM */
27191
27192 \f
27193 /************************************************************************
27194 Mouse Face
27195 ************************************************************************/
27196
27197 #ifdef HAVE_WINDOW_SYSTEM
27198
27199 /* EXPORT for RIF:
27200 Fix the display of area AREA of overlapping row ROW in window W
27201 with respect to the overlapping part OVERLAPS. */
27202
27203 void
27204 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27205 enum glyph_row_area area, int overlaps)
27206 {
27207 int i, x;
27208
27209 block_input ();
27210
27211 x = 0;
27212 for (i = 0; i < row->used[area];)
27213 {
27214 if (row->glyphs[area][i].overlaps_vertically_p)
27215 {
27216 int start = i, start_x = x;
27217
27218 do
27219 {
27220 x += row->glyphs[area][i].pixel_width;
27221 ++i;
27222 }
27223 while (i < row->used[area]
27224 && row->glyphs[area][i].overlaps_vertically_p);
27225
27226 draw_glyphs (w, start_x, row, area,
27227 start, i,
27228 DRAW_NORMAL_TEXT, overlaps);
27229 }
27230 else
27231 {
27232 x += row->glyphs[area][i].pixel_width;
27233 ++i;
27234 }
27235 }
27236
27237 unblock_input ();
27238 }
27239
27240
27241 /* EXPORT:
27242 Draw the cursor glyph of window W in glyph row ROW. See the
27243 comment of draw_glyphs for the meaning of HL. */
27244
27245 void
27246 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27247 enum draw_glyphs_face hl)
27248 {
27249 /* If cursor hpos is out of bounds, don't draw garbage. This can
27250 happen in mini-buffer windows when switching between echo area
27251 glyphs and mini-buffer. */
27252 if ((row->reversed_p
27253 ? (w->phys_cursor.hpos >= 0)
27254 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27255 {
27256 int on_p = w->phys_cursor_on_p;
27257 int x1;
27258 int hpos = w->phys_cursor.hpos;
27259
27260 /* When the window is hscrolled, cursor hpos can legitimately be
27261 out of bounds, but we draw the cursor at the corresponding
27262 window margin in that case. */
27263 if (!row->reversed_p && hpos < 0)
27264 hpos = 0;
27265 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27266 hpos = row->used[TEXT_AREA] - 1;
27267
27268 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27269 hl, 0);
27270 w->phys_cursor_on_p = on_p;
27271
27272 if (hl == DRAW_CURSOR)
27273 w->phys_cursor_width = x1 - w->phys_cursor.x;
27274 /* When we erase the cursor, and ROW is overlapped by other
27275 rows, make sure that these overlapping parts of other rows
27276 are redrawn. */
27277 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27278 {
27279 w->phys_cursor_width = x1 - w->phys_cursor.x;
27280
27281 if (row > w->current_matrix->rows
27282 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27283 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27284 OVERLAPS_ERASED_CURSOR);
27285
27286 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27287 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27288 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27289 OVERLAPS_ERASED_CURSOR);
27290 }
27291 }
27292 }
27293
27294
27295 /* Erase the image of a cursor of window W from the screen. */
27296
27297 #ifndef HAVE_NTGUI
27298 static
27299 #endif
27300 void
27301 erase_phys_cursor (struct window *w)
27302 {
27303 struct frame *f = XFRAME (w->frame);
27304 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27305 int hpos = w->phys_cursor.hpos;
27306 int vpos = w->phys_cursor.vpos;
27307 int mouse_face_here_p = 0;
27308 struct glyph_matrix *active_glyphs = w->current_matrix;
27309 struct glyph_row *cursor_row;
27310 struct glyph *cursor_glyph;
27311 enum draw_glyphs_face hl;
27312
27313 /* No cursor displayed or row invalidated => nothing to do on the
27314 screen. */
27315 if (w->phys_cursor_type == NO_CURSOR)
27316 goto mark_cursor_off;
27317
27318 /* VPOS >= active_glyphs->nrows means that window has been resized.
27319 Don't bother to erase the cursor. */
27320 if (vpos >= active_glyphs->nrows)
27321 goto mark_cursor_off;
27322
27323 /* If row containing cursor is marked invalid, there is nothing we
27324 can do. */
27325 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27326 if (!cursor_row->enabled_p)
27327 goto mark_cursor_off;
27328
27329 /* If line spacing is > 0, old cursor may only be partially visible in
27330 window after split-window. So adjust visible height. */
27331 cursor_row->visible_height = min (cursor_row->visible_height,
27332 window_text_bottom_y (w) - cursor_row->y);
27333
27334 /* If row is completely invisible, don't attempt to delete a cursor which
27335 isn't there. This can happen if cursor is at top of a window, and
27336 we switch to a buffer with a header line in that window. */
27337 if (cursor_row->visible_height <= 0)
27338 goto mark_cursor_off;
27339
27340 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27341 if (cursor_row->cursor_in_fringe_p)
27342 {
27343 cursor_row->cursor_in_fringe_p = 0;
27344 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27345 goto mark_cursor_off;
27346 }
27347
27348 /* This can happen when the new row is shorter than the old one.
27349 In this case, either draw_glyphs or clear_end_of_line
27350 should have cleared the cursor. Note that we wouldn't be
27351 able to erase the cursor in this case because we don't have a
27352 cursor glyph at hand. */
27353 if ((cursor_row->reversed_p
27354 ? (w->phys_cursor.hpos < 0)
27355 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27356 goto mark_cursor_off;
27357
27358 /* When the window is hscrolled, cursor hpos can legitimately be out
27359 of bounds, but we draw the cursor at the corresponding window
27360 margin in that case. */
27361 if (!cursor_row->reversed_p && hpos < 0)
27362 hpos = 0;
27363 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27364 hpos = cursor_row->used[TEXT_AREA] - 1;
27365
27366 /* If the cursor is in the mouse face area, redisplay that when
27367 we clear the cursor. */
27368 if (! NILP (hlinfo->mouse_face_window)
27369 && coords_in_mouse_face_p (w, hpos, vpos)
27370 /* Don't redraw the cursor's spot in mouse face if it is at the
27371 end of a line (on a newline). The cursor appears there, but
27372 mouse highlighting does not. */
27373 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27374 mouse_face_here_p = 1;
27375
27376 /* Maybe clear the display under the cursor. */
27377 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27378 {
27379 int x, y, left_x;
27380 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27381 int width;
27382
27383 cursor_glyph = get_phys_cursor_glyph (w);
27384 if (cursor_glyph == NULL)
27385 goto mark_cursor_off;
27386
27387 width = cursor_glyph->pixel_width;
27388 left_x = window_box_left_offset (w, TEXT_AREA);
27389 x = w->phys_cursor.x;
27390 if (x < left_x)
27391 width -= left_x - x;
27392 width = min (width, window_box_width (w, TEXT_AREA) - x);
27393 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27394 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
27395
27396 if (width > 0)
27397 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27398 }
27399
27400 /* Erase the cursor by redrawing the character underneath it. */
27401 if (mouse_face_here_p)
27402 hl = DRAW_MOUSE_FACE;
27403 else
27404 hl = DRAW_NORMAL_TEXT;
27405 draw_phys_cursor_glyph (w, cursor_row, hl);
27406
27407 mark_cursor_off:
27408 w->phys_cursor_on_p = 0;
27409 w->phys_cursor_type = NO_CURSOR;
27410 }
27411
27412
27413 /* EXPORT:
27414 Display or clear cursor of window W. If ON is zero, clear the
27415 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27416 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27417
27418 void
27419 display_and_set_cursor (struct window *w, bool on,
27420 int hpos, int vpos, int x, int y)
27421 {
27422 struct frame *f = XFRAME (w->frame);
27423 int new_cursor_type;
27424 int new_cursor_width;
27425 int active_cursor;
27426 struct glyph_row *glyph_row;
27427 struct glyph *glyph;
27428
27429 /* This is pointless on invisible frames, and dangerous on garbaged
27430 windows and frames; in the latter case, the frame or window may
27431 be in the midst of changing its size, and x and y may be off the
27432 window. */
27433 if (! FRAME_VISIBLE_P (f)
27434 || FRAME_GARBAGED_P (f)
27435 || vpos >= w->current_matrix->nrows
27436 || hpos >= w->current_matrix->matrix_w)
27437 return;
27438
27439 /* If cursor is off and we want it off, return quickly. */
27440 if (!on && !w->phys_cursor_on_p)
27441 return;
27442
27443 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27444 /* If cursor row is not enabled, we don't really know where to
27445 display the cursor. */
27446 if (!glyph_row->enabled_p)
27447 {
27448 w->phys_cursor_on_p = 0;
27449 return;
27450 }
27451
27452 glyph = NULL;
27453 if (!glyph_row->exact_window_width_line_p
27454 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27455 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27456
27457 eassert (input_blocked_p ());
27458
27459 /* Set new_cursor_type to the cursor we want to be displayed. */
27460 new_cursor_type = get_window_cursor_type (w, glyph,
27461 &new_cursor_width, &active_cursor);
27462
27463 /* If cursor is currently being shown and we don't want it to be or
27464 it is in the wrong place, or the cursor type is not what we want,
27465 erase it. */
27466 if (w->phys_cursor_on_p
27467 && (!on
27468 || w->phys_cursor.x != x
27469 || w->phys_cursor.y != y
27470 /* HPOS can be negative in R2L rows whose
27471 exact_window_width_line_p flag is set (i.e. their newline
27472 would "overflow into the fringe"). */
27473 || hpos < 0
27474 || new_cursor_type != w->phys_cursor_type
27475 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27476 && new_cursor_width != w->phys_cursor_width)))
27477 erase_phys_cursor (w);
27478
27479 /* Don't check phys_cursor_on_p here because that flag is only set
27480 to zero in some cases where we know that the cursor has been
27481 completely erased, to avoid the extra work of erasing the cursor
27482 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27483 still not be visible, or it has only been partly erased. */
27484 if (on)
27485 {
27486 w->phys_cursor_ascent = glyph_row->ascent;
27487 w->phys_cursor_height = glyph_row->height;
27488
27489 /* Set phys_cursor_.* before x_draw_.* is called because some
27490 of them may need the information. */
27491 w->phys_cursor.x = x;
27492 w->phys_cursor.y = glyph_row->y;
27493 w->phys_cursor.hpos = hpos;
27494 w->phys_cursor.vpos = vpos;
27495 }
27496
27497 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27498 new_cursor_type, new_cursor_width,
27499 on, active_cursor);
27500 }
27501
27502
27503 /* Switch the display of W's cursor on or off, according to the value
27504 of ON. */
27505
27506 static void
27507 update_window_cursor (struct window *w, bool on)
27508 {
27509 /* Don't update cursor in windows whose frame is in the process
27510 of being deleted. */
27511 if (w->current_matrix)
27512 {
27513 int hpos = w->phys_cursor.hpos;
27514 int vpos = w->phys_cursor.vpos;
27515 struct glyph_row *row;
27516
27517 if (vpos >= w->current_matrix->nrows
27518 || hpos >= w->current_matrix->matrix_w)
27519 return;
27520
27521 row = MATRIX_ROW (w->current_matrix, vpos);
27522
27523 /* When the window is hscrolled, cursor hpos can legitimately be
27524 out of bounds, but we draw the cursor at the corresponding
27525 window margin in that case. */
27526 if (!row->reversed_p && hpos < 0)
27527 hpos = 0;
27528 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27529 hpos = row->used[TEXT_AREA] - 1;
27530
27531 block_input ();
27532 display_and_set_cursor (w, on, hpos, vpos,
27533 w->phys_cursor.x, w->phys_cursor.y);
27534 unblock_input ();
27535 }
27536 }
27537
27538
27539 /* Call update_window_cursor with parameter ON_P on all leaf windows
27540 in the window tree rooted at W. */
27541
27542 static void
27543 update_cursor_in_window_tree (struct window *w, bool on_p)
27544 {
27545 while (w)
27546 {
27547 if (WINDOWP (w->contents))
27548 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27549 else
27550 update_window_cursor (w, on_p);
27551
27552 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27553 }
27554 }
27555
27556
27557 /* EXPORT:
27558 Display the cursor on window W, or clear it, according to ON_P.
27559 Don't change the cursor's position. */
27560
27561 void
27562 x_update_cursor (struct frame *f, bool on_p)
27563 {
27564 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27565 }
27566
27567
27568 /* EXPORT:
27569 Clear the cursor of window W to background color, and mark the
27570 cursor as not shown. This is used when the text where the cursor
27571 is about to be rewritten. */
27572
27573 void
27574 x_clear_cursor (struct window *w)
27575 {
27576 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27577 update_window_cursor (w, 0);
27578 }
27579
27580 #endif /* HAVE_WINDOW_SYSTEM */
27581
27582 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27583 and MSDOS. */
27584 static void
27585 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27586 int start_hpos, int end_hpos,
27587 enum draw_glyphs_face draw)
27588 {
27589 #ifdef HAVE_WINDOW_SYSTEM
27590 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27591 {
27592 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27593 return;
27594 }
27595 #endif
27596 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27597 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27598 #endif
27599 }
27600
27601 /* Display the active region described by mouse_face_* according to DRAW. */
27602
27603 static void
27604 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27605 {
27606 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27607 struct frame *f = XFRAME (WINDOW_FRAME (w));
27608
27609 if (/* If window is in the process of being destroyed, don't bother
27610 to do anything. */
27611 w->current_matrix != NULL
27612 /* Don't update mouse highlight if hidden. */
27613 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27614 /* Recognize when we are called to operate on rows that don't exist
27615 anymore. This can happen when a window is split. */
27616 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27617 {
27618 int phys_cursor_on_p = w->phys_cursor_on_p;
27619 struct glyph_row *row, *first, *last;
27620
27621 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27622 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27623
27624 for (row = first; row <= last && row->enabled_p; ++row)
27625 {
27626 int start_hpos, end_hpos, start_x;
27627
27628 /* For all but the first row, the highlight starts at column 0. */
27629 if (row == first)
27630 {
27631 /* R2L rows have BEG and END in reversed order, but the
27632 screen drawing geometry is always left to right. So
27633 we need to mirror the beginning and end of the
27634 highlighted area in R2L rows. */
27635 if (!row->reversed_p)
27636 {
27637 start_hpos = hlinfo->mouse_face_beg_col;
27638 start_x = hlinfo->mouse_face_beg_x;
27639 }
27640 else if (row == last)
27641 {
27642 start_hpos = hlinfo->mouse_face_end_col;
27643 start_x = hlinfo->mouse_face_end_x;
27644 }
27645 else
27646 {
27647 start_hpos = 0;
27648 start_x = 0;
27649 }
27650 }
27651 else if (row->reversed_p && row == last)
27652 {
27653 start_hpos = hlinfo->mouse_face_end_col;
27654 start_x = hlinfo->mouse_face_end_x;
27655 }
27656 else
27657 {
27658 start_hpos = 0;
27659 start_x = 0;
27660 }
27661
27662 if (row == last)
27663 {
27664 if (!row->reversed_p)
27665 end_hpos = hlinfo->mouse_face_end_col;
27666 else if (row == first)
27667 end_hpos = hlinfo->mouse_face_beg_col;
27668 else
27669 {
27670 end_hpos = row->used[TEXT_AREA];
27671 if (draw == DRAW_NORMAL_TEXT)
27672 row->fill_line_p = 1; /* Clear to end of line */
27673 }
27674 }
27675 else if (row->reversed_p && row == first)
27676 end_hpos = hlinfo->mouse_face_beg_col;
27677 else
27678 {
27679 end_hpos = row->used[TEXT_AREA];
27680 if (draw == DRAW_NORMAL_TEXT)
27681 row->fill_line_p = 1; /* Clear to end of line */
27682 }
27683
27684 if (end_hpos > start_hpos)
27685 {
27686 draw_row_with_mouse_face (w, start_x, row,
27687 start_hpos, end_hpos, draw);
27688
27689 row->mouse_face_p
27690 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27691 }
27692 }
27693
27694 #ifdef HAVE_WINDOW_SYSTEM
27695 /* When we've written over the cursor, arrange for it to
27696 be displayed again. */
27697 if (FRAME_WINDOW_P (f)
27698 && phys_cursor_on_p && !w->phys_cursor_on_p)
27699 {
27700 int hpos = w->phys_cursor.hpos;
27701
27702 /* When the window is hscrolled, cursor hpos can legitimately be
27703 out of bounds, but we draw the cursor at the corresponding
27704 window margin in that case. */
27705 if (!row->reversed_p && hpos < 0)
27706 hpos = 0;
27707 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27708 hpos = row->used[TEXT_AREA] - 1;
27709
27710 block_input ();
27711 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27712 w->phys_cursor.x, w->phys_cursor.y);
27713 unblock_input ();
27714 }
27715 #endif /* HAVE_WINDOW_SYSTEM */
27716 }
27717
27718 #ifdef HAVE_WINDOW_SYSTEM
27719 /* Change the mouse cursor. */
27720 if (FRAME_WINDOW_P (f))
27721 {
27722 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27723 if (draw == DRAW_NORMAL_TEXT
27724 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27725 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27726 else
27727 #endif
27728 if (draw == DRAW_MOUSE_FACE)
27729 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27730 else
27731 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27732 }
27733 #endif /* HAVE_WINDOW_SYSTEM */
27734 }
27735
27736 /* EXPORT:
27737 Clear out the mouse-highlighted active region.
27738 Redraw it un-highlighted first. Value is non-zero if mouse
27739 face was actually drawn unhighlighted. */
27740
27741 int
27742 clear_mouse_face (Mouse_HLInfo *hlinfo)
27743 {
27744 int cleared = 0;
27745
27746 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27747 {
27748 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27749 cleared = 1;
27750 }
27751
27752 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27753 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27754 hlinfo->mouse_face_window = Qnil;
27755 hlinfo->mouse_face_overlay = Qnil;
27756 return cleared;
27757 }
27758
27759 /* Return true if the coordinates HPOS and VPOS on windows W are
27760 within the mouse face on that window. */
27761 static bool
27762 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27763 {
27764 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27765
27766 /* Quickly resolve the easy cases. */
27767 if (!(WINDOWP (hlinfo->mouse_face_window)
27768 && XWINDOW (hlinfo->mouse_face_window) == w))
27769 return false;
27770 if (vpos < hlinfo->mouse_face_beg_row
27771 || vpos > hlinfo->mouse_face_end_row)
27772 return false;
27773 if (vpos > hlinfo->mouse_face_beg_row
27774 && vpos < hlinfo->mouse_face_end_row)
27775 return true;
27776
27777 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27778 {
27779 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27780 {
27781 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27782 return true;
27783 }
27784 else if ((vpos == hlinfo->mouse_face_beg_row
27785 && hpos >= hlinfo->mouse_face_beg_col)
27786 || (vpos == hlinfo->mouse_face_end_row
27787 && hpos < hlinfo->mouse_face_end_col))
27788 return true;
27789 }
27790 else
27791 {
27792 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27793 {
27794 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27795 return true;
27796 }
27797 else if ((vpos == hlinfo->mouse_face_beg_row
27798 && hpos <= hlinfo->mouse_face_beg_col)
27799 || (vpos == hlinfo->mouse_face_end_row
27800 && hpos > hlinfo->mouse_face_end_col))
27801 return true;
27802 }
27803 return false;
27804 }
27805
27806
27807 /* EXPORT:
27808 True if physical cursor of window W is within mouse face. */
27809
27810 bool
27811 cursor_in_mouse_face_p (struct window *w)
27812 {
27813 int hpos = w->phys_cursor.hpos;
27814 int vpos = w->phys_cursor.vpos;
27815 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27816
27817 /* When the window is hscrolled, cursor hpos can legitimately be out
27818 of bounds, but we draw the cursor at the corresponding window
27819 margin in that case. */
27820 if (!row->reversed_p && hpos < 0)
27821 hpos = 0;
27822 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27823 hpos = row->used[TEXT_AREA] - 1;
27824
27825 return coords_in_mouse_face_p (w, hpos, vpos);
27826 }
27827
27828
27829 \f
27830 /* Find the glyph rows START_ROW and END_ROW of window W that display
27831 characters between buffer positions START_CHARPOS and END_CHARPOS
27832 (excluding END_CHARPOS). DISP_STRING is a display string that
27833 covers these buffer positions. This is similar to
27834 row_containing_pos, but is more accurate when bidi reordering makes
27835 buffer positions change non-linearly with glyph rows. */
27836 static void
27837 rows_from_pos_range (struct window *w,
27838 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27839 Lisp_Object disp_string,
27840 struct glyph_row **start, struct glyph_row **end)
27841 {
27842 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27843 int last_y = window_text_bottom_y (w);
27844 struct glyph_row *row;
27845
27846 *start = NULL;
27847 *end = NULL;
27848
27849 while (!first->enabled_p
27850 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27851 first++;
27852
27853 /* Find the START row. */
27854 for (row = first;
27855 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27856 row++)
27857 {
27858 /* A row can potentially be the START row if the range of the
27859 characters it displays intersects the range
27860 [START_CHARPOS..END_CHARPOS). */
27861 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27862 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27863 /* See the commentary in row_containing_pos, for the
27864 explanation of the complicated way to check whether
27865 some position is beyond the end of the characters
27866 displayed by a row. */
27867 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27868 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27869 && !row->ends_at_zv_p
27870 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27871 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27872 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27873 && !row->ends_at_zv_p
27874 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27875 {
27876 /* Found a candidate row. Now make sure at least one of the
27877 glyphs it displays has a charpos from the range
27878 [START_CHARPOS..END_CHARPOS).
27879
27880 This is not obvious because bidi reordering could make
27881 buffer positions of a row be 1,2,3,102,101,100, and if we
27882 want to highlight characters in [50..60), we don't want
27883 this row, even though [50..60) does intersect [1..103),
27884 the range of character positions given by the row's start
27885 and end positions. */
27886 struct glyph *g = row->glyphs[TEXT_AREA];
27887 struct glyph *e = g + row->used[TEXT_AREA];
27888
27889 while (g < e)
27890 {
27891 if (((BUFFERP (g->object) || INTEGERP (g->object))
27892 && start_charpos <= g->charpos && g->charpos < end_charpos)
27893 /* A glyph that comes from DISP_STRING is by
27894 definition to be highlighted. */
27895 || EQ (g->object, disp_string))
27896 *start = row;
27897 g++;
27898 }
27899 if (*start)
27900 break;
27901 }
27902 }
27903
27904 /* Find the END row. */
27905 if (!*start
27906 /* If the last row is partially visible, start looking for END
27907 from that row, instead of starting from FIRST. */
27908 && !(row->enabled_p
27909 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27910 row = first;
27911 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27912 {
27913 struct glyph_row *next = row + 1;
27914 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27915
27916 if (!next->enabled_p
27917 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27918 /* The first row >= START whose range of displayed characters
27919 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27920 is the row END + 1. */
27921 || (start_charpos < next_start
27922 && end_charpos < next_start)
27923 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27924 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27925 && !next->ends_at_zv_p
27926 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27927 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27928 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27929 && !next->ends_at_zv_p
27930 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27931 {
27932 *end = row;
27933 break;
27934 }
27935 else
27936 {
27937 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27938 but none of the characters it displays are in the range, it is
27939 also END + 1. */
27940 struct glyph *g = next->glyphs[TEXT_AREA];
27941 struct glyph *s = g;
27942 struct glyph *e = g + next->used[TEXT_AREA];
27943
27944 while (g < e)
27945 {
27946 if (((BUFFERP (g->object) || INTEGERP (g->object))
27947 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27948 /* If the buffer position of the first glyph in
27949 the row is equal to END_CHARPOS, it means
27950 the last character to be highlighted is the
27951 newline of ROW, and we must consider NEXT as
27952 END, not END+1. */
27953 || (((!next->reversed_p && g == s)
27954 || (next->reversed_p && g == e - 1))
27955 && (g->charpos == end_charpos
27956 /* Special case for when NEXT is an
27957 empty line at ZV. */
27958 || (g->charpos == -1
27959 && !row->ends_at_zv_p
27960 && next_start == end_charpos)))))
27961 /* A glyph that comes from DISP_STRING is by
27962 definition to be highlighted. */
27963 || EQ (g->object, disp_string))
27964 break;
27965 g++;
27966 }
27967 if (g == e)
27968 {
27969 *end = row;
27970 break;
27971 }
27972 /* The first row that ends at ZV must be the last to be
27973 highlighted. */
27974 else if (next->ends_at_zv_p)
27975 {
27976 *end = next;
27977 break;
27978 }
27979 }
27980 }
27981 }
27982
27983 /* This function sets the mouse_face_* elements of HLINFO, assuming
27984 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27985 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27986 for the overlay or run of text properties specifying the mouse
27987 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27988 before-string and after-string that must also be highlighted.
27989 DISP_STRING, if non-nil, is a display string that may cover some
27990 or all of the highlighted text. */
27991
27992 static void
27993 mouse_face_from_buffer_pos (Lisp_Object window,
27994 Mouse_HLInfo *hlinfo,
27995 ptrdiff_t mouse_charpos,
27996 ptrdiff_t start_charpos,
27997 ptrdiff_t end_charpos,
27998 Lisp_Object before_string,
27999 Lisp_Object after_string,
28000 Lisp_Object disp_string)
28001 {
28002 struct window *w = XWINDOW (window);
28003 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28004 struct glyph_row *r1, *r2;
28005 struct glyph *glyph, *end;
28006 ptrdiff_t ignore, pos;
28007 int x;
28008
28009 eassert (NILP (disp_string) || STRINGP (disp_string));
28010 eassert (NILP (before_string) || STRINGP (before_string));
28011 eassert (NILP (after_string) || STRINGP (after_string));
28012
28013 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28014 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28015 if (r1 == NULL)
28016 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28017 /* If the before-string or display-string contains newlines,
28018 rows_from_pos_range skips to its last row. Move back. */
28019 if (!NILP (before_string) || !NILP (disp_string))
28020 {
28021 struct glyph_row *prev;
28022 while ((prev = r1 - 1, prev >= first)
28023 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28024 && prev->used[TEXT_AREA] > 0)
28025 {
28026 struct glyph *beg = prev->glyphs[TEXT_AREA];
28027 glyph = beg + prev->used[TEXT_AREA];
28028 while (--glyph >= beg && INTEGERP (glyph->object));
28029 if (glyph < beg
28030 || !(EQ (glyph->object, before_string)
28031 || EQ (glyph->object, disp_string)))
28032 break;
28033 r1 = prev;
28034 }
28035 }
28036 if (r2 == NULL)
28037 {
28038 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28039 hlinfo->mouse_face_past_end = 1;
28040 }
28041 else if (!NILP (after_string))
28042 {
28043 /* If the after-string has newlines, advance to its last row. */
28044 struct glyph_row *next;
28045 struct glyph_row *last
28046 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28047
28048 for (next = r2 + 1;
28049 next <= last
28050 && next->used[TEXT_AREA] > 0
28051 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28052 ++next)
28053 r2 = next;
28054 }
28055 /* The rest of the display engine assumes that mouse_face_beg_row is
28056 either above mouse_face_end_row or identical to it. But with
28057 bidi-reordered continued lines, the row for START_CHARPOS could
28058 be below the row for END_CHARPOS. If so, swap the rows and store
28059 them in correct order. */
28060 if (r1->y > r2->y)
28061 {
28062 struct glyph_row *tem = r2;
28063
28064 r2 = r1;
28065 r1 = tem;
28066 }
28067
28068 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28069 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28070
28071 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28072 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28073 could be anywhere in the row and in any order. The strategy
28074 below is to find the leftmost and the rightmost glyph that
28075 belongs to either of these 3 strings, or whose position is
28076 between START_CHARPOS and END_CHARPOS, and highlight all the
28077 glyphs between those two. This may cover more than just the text
28078 between START_CHARPOS and END_CHARPOS if the range of characters
28079 strides the bidi level boundary, e.g. if the beginning is in R2L
28080 text while the end is in L2R text or vice versa. */
28081 if (!r1->reversed_p)
28082 {
28083 /* This row is in a left to right paragraph. Scan it left to
28084 right. */
28085 glyph = r1->glyphs[TEXT_AREA];
28086 end = glyph + r1->used[TEXT_AREA];
28087 x = r1->x;
28088
28089 /* Skip truncation glyphs at the start of the glyph row. */
28090 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28091 for (; glyph < end
28092 && INTEGERP (glyph->object)
28093 && glyph->charpos < 0;
28094 ++glyph)
28095 x += glyph->pixel_width;
28096
28097 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28098 or DISP_STRING, and the first glyph from buffer whose
28099 position is between START_CHARPOS and END_CHARPOS. */
28100 for (; glyph < end
28101 && !INTEGERP (glyph->object)
28102 && !EQ (glyph->object, disp_string)
28103 && !(BUFFERP (glyph->object)
28104 && (glyph->charpos >= start_charpos
28105 && glyph->charpos < end_charpos));
28106 ++glyph)
28107 {
28108 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28109 are present at buffer positions between START_CHARPOS and
28110 END_CHARPOS, or if they come from an overlay. */
28111 if (EQ (glyph->object, before_string))
28112 {
28113 pos = string_buffer_position (before_string,
28114 start_charpos);
28115 /* If pos == 0, it means before_string came from an
28116 overlay, not from a buffer position. */
28117 if (!pos || (pos >= start_charpos && pos < end_charpos))
28118 break;
28119 }
28120 else if (EQ (glyph->object, after_string))
28121 {
28122 pos = string_buffer_position (after_string, end_charpos);
28123 if (!pos || (pos >= start_charpos && pos < end_charpos))
28124 break;
28125 }
28126 x += glyph->pixel_width;
28127 }
28128 hlinfo->mouse_face_beg_x = x;
28129 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28130 }
28131 else
28132 {
28133 /* This row is in a right to left paragraph. Scan it right to
28134 left. */
28135 struct glyph *g;
28136
28137 end = r1->glyphs[TEXT_AREA] - 1;
28138 glyph = end + r1->used[TEXT_AREA];
28139
28140 /* Skip truncation glyphs at the start of the glyph row. */
28141 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28142 for (; glyph > end
28143 && INTEGERP (glyph->object)
28144 && glyph->charpos < 0;
28145 --glyph)
28146 ;
28147
28148 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28149 or DISP_STRING, and the first glyph from buffer whose
28150 position is between START_CHARPOS and END_CHARPOS. */
28151 for (; glyph > end
28152 && !INTEGERP (glyph->object)
28153 && !EQ (glyph->object, disp_string)
28154 && !(BUFFERP (glyph->object)
28155 && (glyph->charpos >= start_charpos
28156 && glyph->charpos < end_charpos));
28157 --glyph)
28158 {
28159 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28160 are present at buffer positions between START_CHARPOS and
28161 END_CHARPOS, or if they come from an overlay. */
28162 if (EQ (glyph->object, before_string))
28163 {
28164 pos = string_buffer_position (before_string, start_charpos);
28165 /* If pos == 0, it means before_string came from an
28166 overlay, not from a buffer position. */
28167 if (!pos || (pos >= start_charpos && pos < end_charpos))
28168 break;
28169 }
28170 else if (EQ (glyph->object, after_string))
28171 {
28172 pos = string_buffer_position (after_string, end_charpos);
28173 if (!pos || (pos >= start_charpos && pos < end_charpos))
28174 break;
28175 }
28176 }
28177
28178 glyph++; /* first glyph to the right of the highlighted area */
28179 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28180 x += g->pixel_width;
28181 hlinfo->mouse_face_beg_x = x;
28182 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28183 }
28184
28185 /* If the highlight ends in a different row, compute GLYPH and END
28186 for the end row. Otherwise, reuse the values computed above for
28187 the row where the highlight begins. */
28188 if (r2 != r1)
28189 {
28190 if (!r2->reversed_p)
28191 {
28192 glyph = r2->glyphs[TEXT_AREA];
28193 end = glyph + r2->used[TEXT_AREA];
28194 x = r2->x;
28195 }
28196 else
28197 {
28198 end = r2->glyphs[TEXT_AREA] - 1;
28199 glyph = end + r2->used[TEXT_AREA];
28200 }
28201 }
28202
28203 if (!r2->reversed_p)
28204 {
28205 /* Skip truncation and continuation glyphs near the end of the
28206 row, and also blanks and stretch glyphs inserted by
28207 extend_face_to_end_of_line. */
28208 while (end > glyph
28209 && INTEGERP ((end - 1)->object))
28210 --end;
28211 /* Scan the rest of the glyph row from the end, looking for the
28212 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28213 DISP_STRING, or whose position is between START_CHARPOS
28214 and END_CHARPOS */
28215 for (--end;
28216 end > glyph
28217 && !INTEGERP (end->object)
28218 && !EQ (end->object, disp_string)
28219 && !(BUFFERP (end->object)
28220 && (end->charpos >= start_charpos
28221 && end->charpos < end_charpos));
28222 --end)
28223 {
28224 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28225 are present at buffer positions between START_CHARPOS and
28226 END_CHARPOS, or if they come from an overlay. */
28227 if (EQ (end->object, before_string))
28228 {
28229 pos = string_buffer_position (before_string, start_charpos);
28230 if (!pos || (pos >= start_charpos && pos < end_charpos))
28231 break;
28232 }
28233 else if (EQ (end->object, after_string))
28234 {
28235 pos = string_buffer_position (after_string, end_charpos);
28236 if (!pos || (pos >= start_charpos && pos < end_charpos))
28237 break;
28238 }
28239 }
28240 /* Find the X coordinate of the last glyph to be highlighted. */
28241 for (; glyph <= end; ++glyph)
28242 x += glyph->pixel_width;
28243
28244 hlinfo->mouse_face_end_x = x;
28245 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28246 }
28247 else
28248 {
28249 /* Skip truncation and continuation glyphs near the end of the
28250 row, and also blanks and stretch glyphs inserted by
28251 extend_face_to_end_of_line. */
28252 x = r2->x;
28253 end++;
28254 while (end < glyph
28255 && INTEGERP (end->object))
28256 {
28257 x += end->pixel_width;
28258 ++end;
28259 }
28260 /* Scan the rest of the glyph row from the end, looking for the
28261 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28262 DISP_STRING, or whose position is between START_CHARPOS
28263 and END_CHARPOS */
28264 for ( ;
28265 end < glyph
28266 && !INTEGERP (end->object)
28267 && !EQ (end->object, disp_string)
28268 && !(BUFFERP (end->object)
28269 && (end->charpos >= start_charpos
28270 && end->charpos < end_charpos));
28271 ++end)
28272 {
28273 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28274 are present at buffer positions between START_CHARPOS and
28275 END_CHARPOS, or if they come from an overlay. */
28276 if (EQ (end->object, before_string))
28277 {
28278 pos = string_buffer_position (before_string, start_charpos);
28279 if (!pos || (pos >= start_charpos && pos < end_charpos))
28280 break;
28281 }
28282 else if (EQ (end->object, after_string))
28283 {
28284 pos = string_buffer_position (after_string, end_charpos);
28285 if (!pos || (pos >= start_charpos && pos < end_charpos))
28286 break;
28287 }
28288 x += end->pixel_width;
28289 }
28290 /* If we exited the above loop because we arrived at the last
28291 glyph of the row, and its buffer position is still not in
28292 range, it means the last character in range is the preceding
28293 newline. Bump the end column and x values to get past the
28294 last glyph. */
28295 if (end == glyph
28296 && BUFFERP (end->object)
28297 && (end->charpos < start_charpos
28298 || end->charpos >= end_charpos))
28299 {
28300 x += end->pixel_width;
28301 ++end;
28302 }
28303 hlinfo->mouse_face_end_x = x;
28304 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28305 }
28306
28307 hlinfo->mouse_face_window = window;
28308 hlinfo->mouse_face_face_id
28309 = face_at_buffer_position (w, mouse_charpos, &ignore,
28310 mouse_charpos + 1,
28311 !hlinfo->mouse_face_hidden, -1);
28312 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28313 }
28314
28315 /* The following function is not used anymore (replaced with
28316 mouse_face_from_string_pos), but I leave it here for the time
28317 being, in case someone would. */
28318
28319 #if 0 /* not used */
28320
28321 /* Find the position of the glyph for position POS in OBJECT in
28322 window W's current matrix, and return in *X, *Y the pixel
28323 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28324
28325 RIGHT_P non-zero means return the position of the right edge of the
28326 glyph, RIGHT_P zero means return the left edge position.
28327
28328 If no glyph for POS exists in the matrix, return the position of
28329 the glyph with the next smaller position that is in the matrix, if
28330 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28331 exists in the matrix, return the position of the glyph with the
28332 next larger position in OBJECT.
28333
28334 Value is non-zero if a glyph was found. */
28335
28336 static int
28337 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28338 int *hpos, int *vpos, int *x, int *y, int right_p)
28339 {
28340 int yb = window_text_bottom_y (w);
28341 struct glyph_row *r;
28342 struct glyph *best_glyph = NULL;
28343 struct glyph_row *best_row = NULL;
28344 int best_x = 0;
28345
28346 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28347 r->enabled_p && r->y < yb;
28348 ++r)
28349 {
28350 struct glyph *g = r->glyphs[TEXT_AREA];
28351 struct glyph *e = g + r->used[TEXT_AREA];
28352 int gx;
28353
28354 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28355 if (EQ (g->object, object))
28356 {
28357 if (g->charpos == pos)
28358 {
28359 best_glyph = g;
28360 best_x = gx;
28361 best_row = r;
28362 goto found;
28363 }
28364 else if (best_glyph == NULL
28365 || ((eabs (g->charpos - pos)
28366 < eabs (best_glyph->charpos - pos))
28367 && (right_p
28368 ? g->charpos < pos
28369 : g->charpos > pos)))
28370 {
28371 best_glyph = g;
28372 best_x = gx;
28373 best_row = r;
28374 }
28375 }
28376 }
28377
28378 found:
28379
28380 if (best_glyph)
28381 {
28382 *x = best_x;
28383 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28384
28385 if (right_p)
28386 {
28387 *x += best_glyph->pixel_width;
28388 ++*hpos;
28389 }
28390
28391 *y = best_row->y;
28392 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28393 }
28394
28395 return best_glyph != NULL;
28396 }
28397 #endif /* not used */
28398
28399 /* Find the positions of the first and the last glyphs in window W's
28400 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28401 (assumed to be a string), and return in HLINFO's mouse_face_*
28402 members the pixel and column/row coordinates of those glyphs. */
28403
28404 static void
28405 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28406 Lisp_Object object,
28407 ptrdiff_t startpos, ptrdiff_t endpos)
28408 {
28409 int yb = window_text_bottom_y (w);
28410 struct glyph_row *r;
28411 struct glyph *g, *e;
28412 int gx;
28413 int found = 0;
28414
28415 /* Find the glyph row with at least one position in the range
28416 [STARTPOS..ENDPOS), and the first glyph in that row whose
28417 position belongs to that range. */
28418 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28419 r->enabled_p && r->y < yb;
28420 ++r)
28421 {
28422 if (!r->reversed_p)
28423 {
28424 g = r->glyphs[TEXT_AREA];
28425 e = g + r->used[TEXT_AREA];
28426 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28427 if (EQ (g->object, object)
28428 && startpos <= g->charpos && g->charpos < endpos)
28429 {
28430 hlinfo->mouse_face_beg_row
28431 = MATRIX_ROW_VPOS (r, w->current_matrix);
28432 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28433 hlinfo->mouse_face_beg_x = gx;
28434 found = 1;
28435 break;
28436 }
28437 }
28438 else
28439 {
28440 struct glyph *g1;
28441
28442 e = r->glyphs[TEXT_AREA];
28443 g = e + r->used[TEXT_AREA];
28444 for ( ; g > e; --g)
28445 if (EQ ((g-1)->object, object)
28446 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28447 {
28448 hlinfo->mouse_face_beg_row
28449 = MATRIX_ROW_VPOS (r, w->current_matrix);
28450 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28451 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28452 gx += g1->pixel_width;
28453 hlinfo->mouse_face_beg_x = gx;
28454 found = 1;
28455 break;
28456 }
28457 }
28458 if (found)
28459 break;
28460 }
28461
28462 if (!found)
28463 return;
28464
28465 /* Starting with the next row, look for the first row which does NOT
28466 include any glyphs whose positions are in the range. */
28467 for (++r; r->enabled_p && r->y < yb; ++r)
28468 {
28469 g = r->glyphs[TEXT_AREA];
28470 e = g + r->used[TEXT_AREA];
28471 found = 0;
28472 for ( ; g < e; ++g)
28473 if (EQ (g->object, object)
28474 && startpos <= g->charpos && g->charpos < endpos)
28475 {
28476 found = 1;
28477 break;
28478 }
28479 if (!found)
28480 break;
28481 }
28482
28483 /* The highlighted region ends on the previous row. */
28484 r--;
28485
28486 /* Set the end row. */
28487 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28488
28489 /* Compute and set the end column and the end column's horizontal
28490 pixel coordinate. */
28491 if (!r->reversed_p)
28492 {
28493 g = r->glyphs[TEXT_AREA];
28494 e = g + r->used[TEXT_AREA];
28495 for ( ; e > g; --e)
28496 if (EQ ((e-1)->object, object)
28497 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28498 break;
28499 hlinfo->mouse_face_end_col = e - g;
28500
28501 for (gx = r->x; g < e; ++g)
28502 gx += g->pixel_width;
28503 hlinfo->mouse_face_end_x = gx;
28504 }
28505 else
28506 {
28507 e = r->glyphs[TEXT_AREA];
28508 g = e + r->used[TEXT_AREA];
28509 for (gx = r->x ; e < g; ++e)
28510 {
28511 if (EQ (e->object, object)
28512 && startpos <= e->charpos && e->charpos < endpos)
28513 break;
28514 gx += e->pixel_width;
28515 }
28516 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28517 hlinfo->mouse_face_end_x = gx;
28518 }
28519 }
28520
28521 #ifdef HAVE_WINDOW_SYSTEM
28522
28523 /* See if position X, Y is within a hot-spot of an image. */
28524
28525 static int
28526 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28527 {
28528 if (!CONSP (hot_spot))
28529 return 0;
28530
28531 if (EQ (XCAR (hot_spot), Qrect))
28532 {
28533 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28534 Lisp_Object rect = XCDR (hot_spot);
28535 Lisp_Object tem;
28536 if (!CONSP (rect))
28537 return 0;
28538 if (!CONSP (XCAR (rect)))
28539 return 0;
28540 if (!CONSP (XCDR (rect)))
28541 return 0;
28542 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28543 return 0;
28544 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28545 return 0;
28546 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28547 return 0;
28548 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28549 return 0;
28550 return 1;
28551 }
28552 else if (EQ (XCAR (hot_spot), Qcircle))
28553 {
28554 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28555 Lisp_Object circ = XCDR (hot_spot);
28556 Lisp_Object lr, lx0, ly0;
28557 if (CONSP (circ)
28558 && CONSP (XCAR (circ))
28559 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28560 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28561 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28562 {
28563 double r = XFLOATINT (lr);
28564 double dx = XINT (lx0) - x;
28565 double dy = XINT (ly0) - y;
28566 return (dx * dx + dy * dy <= r * r);
28567 }
28568 }
28569 else if (EQ (XCAR (hot_spot), Qpoly))
28570 {
28571 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28572 if (VECTORP (XCDR (hot_spot)))
28573 {
28574 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28575 Lisp_Object *poly = v->contents;
28576 ptrdiff_t n = v->header.size;
28577 ptrdiff_t i;
28578 int inside = 0;
28579 Lisp_Object lx, ly;
28580 int x0, y0;
28581
28582 /* Need an even number of coordinates, and at least 3 edges. */
28583 if (n < 6 || n & 1)
28584 return 0;
28585
28586 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28587 If count is odd, we are inside polygon. Pixels on edges
28588 may or may not be included depending on actual geometry of the
28589 polygon. */
28590 if ((lx = poly[n-2], !INTEGERP (lx))
28591 || (ly = poly[n-1], !INTEGERP (lx)))
28592 return 0;
28593 x0 = XINT (lx), y0 = XINT (ly);
28594 for (i = 0; i < n; i += 2)
28595 {
28596 int x1 = x0, y1 = y0;
28597 if ((lx = poly[i], !INTEGERP (lx))
28598 || (ly = poly[i+1], !INTEGERP (ly)))
28599 return 0;
28600 x0 = XINT (lx), y0 = XINT (ly);
28601
28602 /* Does this segment cross the X line? */
28603 if (x0 >= x)
28604 {
28605 if (x1 >= x)
28606 continue;
28607 }
28608 else if (x1 < x)
28609 continue;
28610 if (y > y0 && y > y1)
28611 continue;
28612 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28613 inside = !inside;
28614 }
28615 return inside;
28616 }
28617 }
28618 return 0;
28619 }
28620
28621 Lisp_Object
28622 find_hot_spot (Lisp_Object map, int x, int y)
28623 {
28624 while (CONSP (map))
28625 {
28626 if (CONSP (XCAR (map))
28627 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28628 return XCAR (map);
28629 map = XCDR (map);
28630 }
28631
28632 return Qnil;
28633 }
28634
28635 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28636 3, 3, 0,
28637 doc: /* Lookup in image map MAP coordinates X and Y.
28638 An image map is an alist where each element has the format (AREA ID PLIST).
28639 An AREA is specified as either a rectangle, a circle, or a polygon:
28640 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28641 pixel coordinates of the upper left and bottom right corners.
28642 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28643 and the radius of the circle; r may be a float or integer.
28644 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28645 vector describes one corner in the polygon.
28646 Returns the alist element for the first matching AREA in MAP. */)
28647 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28648 {
28649 if (NILP (map))
28650 return Qnil;
28651
28652 CHECK_NUMBER (x);
28653 CHECK_NUMBER (y);
28654
28655 return find_hot_spot (map,
28656 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28657 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28658 }
28659
28660
28661 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28662 static void
28663 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28664 {
28665 /* Do not change cursor shape while dragging mouse. */
28666 if (!NILP (do_mouse_tracking))
28667 return;
28668
28669 if (!NILP (pointer))
28670 {
28671 if (EQ (pointer, Qarrow))
28672 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28673 else if (EQ (pointer, Qhand))
28674 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28675 else if (EQ (pointer, Qtext))
28676 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28677 else if (EQ (pointer, intern ("hdrag")))
28678 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28679 else if (EQ (pointer, intern ("nhdrag")))
28680 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28681 #ifdef HAVE_X_WINDOWS
28682 else if (EQ (pointer, intern ("vdrag")))
28683 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28684 #endif
28685 else if (EQ (pointer, intern ("hourglass")))
28686 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28687 else if (EQ (pointer, Qmodeline))
28688 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28689 else
28690 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28691 }
28692
28693 if (cursor != No_Cursor)
28694 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28695 }
28696
28697 #endif /* HAVE_WINDOW_SYSTEM */
28698
28699 /* Take proper action when mouse has moved to the mode or header line
28700 or marginal area AREA of window W, x-position X and y-position Y.
28701 X is relative to the start of the text display area of W, so the
28702 width of bitmap areas and scroll bars must be subtracted to get a
28703 position relative to the start of the mode line. */
28704
28705 static void
28706 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28707 enum window_part area)
28708 {
28709 struct window *w = XWINDOW (window);
28710 struct frame *f = XFRAME (w->frame);
28711 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28712 #ifdef HAVE_WINDOW_SYSTEM
28713 Display_Info *dpyinfo;
28714 #endif
28715 Cursor cursor = No_Cursor;
28716 Lisp_Object pointer = Qnil;
28717 int dx, dy, width, height;
28718 ptrdiff_t charpos;
28719 Lisp_Object string, object = Qnil;
28720 Lisp_Object pos IF_LINT (= Qnil), help;
28721
28722 Lisp_Object mouse_face;
28723 int original_x_pixel = x;
28724 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28725 struct glyph_row *row IF_LINT (= 0);
28726
28727 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28728 {
28729 int x0;
28730 struct glyph *end;
28731
28732 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28733 returns them in row/column units! */
28734 string = mode_line_string (w, area, &x, &y, &charpos,
28735 &object, &dx, &dy, &width, &height);
28736
28737 row = (area == ON_MODE_LINE
28738 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28739 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28740
28741 /* Find the glyph under the mouse pointer. */
28742 if (row->mode_line_p && row->enabled_p)
28743 {
28744 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28745 end = glyph + row->used[TEXT_AREA];
28746
28747 for (x0 = original_x_pixel;
28748 glyph < end && x0 >= glyph->pixel_width;
28749 ++glyph)
28750 x0 -= glyph->pixel_width;
28751
28752 if (glyph >= end)
28753 glyph = NULL;
28754 }
28755 }
28756 else
28757 {
28758 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28759 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28760 returns them in row/column units! */
28761 string = marginal_area_string (w, area, &x, &y, &charpos,
28762 &object, &dx, &dy, &width, &height);
28763 }
28764
28765 help = Qnil;
28766
28767 #ifdef HAVE_WINDOW_SYSTEM
28768 if (IMAGEP (object))
28769 {
28770 Lisp_Object image_map, hotspot;
28771 if ((image_map = Fplist_get (XCDR (object), QCmap),
28772 !NILP (image_map))
28773 && (hotspot = find_hot_spot (image_map, dx, dy),
28774 CONSP (hotspot))
28775 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28776 {
28777 Lisp_Object plist;
28778
28779 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28780 If so, we could look for mouse-enter, mouse-leave
28781 properties in PLIST (and do something...). */
28782 hotspot = XCDR (hotspot);
28783 if (CONSP (hotspot)
28784 && (plist = XCAR (hotspot), CONSP (plist)))
28785 {
28786 pointer = Fplist_get (plist, Qpointer);
28787 if (NILP (pointer))
28788 pointer = Qhand;
28789 help = Fplist_get (plist, Qhelp_echo);
28790 if (!NILP (help))
28791 {
28792 help_echo_string = help;
28793 XSETWINDOW (help_echo_window, w);
28794 help_echo_object = w->contents;
28795 help_echo_pos = charpos;
28796 }
28797 }
28798 }
28799 if (NILP (pointer))
28800 pointer = Fplist_get (XCDR (object), QCpointer);
28801 }
28802 #endif /* HAVE_WINDOW_SYSTEM */
28803
28804 if (STRINGP (string))
28805 pos = make_number (charpos);
28806
28807 /* Set the help text and mouse pointer. If the mouse is on a part
28808 of the mode line without any text (e.g. past the right edge of
28809 the mode line text), use the default help text and pointer. */
28810 if (STRINGP (string) || area == ON_MODE_LINE)
28811 {
28812 /* Arrange to display the help by setting the global variables
28813 help_echo_string, help_echo_object, and help_echo_pos. */
28814 if (NILP (help))
28815 {
28816 if (STRINGP (string))
28817 help = Fget_text_property (pos, Qhelp_echo, string);
28818
28819 if (!NILP (help))
28820 {
28821 help_echo_string = help;
28822 XSETWINDOW (help_echo_window, w);
28823 help_echo_object = string;
28824 help_echo_pos = charpos;
28825 }
28826 else if (area == ON_MODE_LINE)
28827 {
28828 Lisp_Object default_help
28829 = buffer_local_value_1 (Qmode_line_default_help_echo,
28830 w->contents);
28831
28832 if (STRINGP (default_help))
28833 {
28834 help_echo_string = default_help;
28835 XSETWINDOW (help_echo_window, w);
28836 help_echo_object = Qnil;
28837 help_echo_pos = -1;
28838 }
28839 }
28840 }
28841
28842 #ifdef HAVE_WINDOW_SYSTEM
28843 /* Change the mouse pointer according to what is under it. */
28844 if (FRAME_WINDOW_P (f))
28845 {
28846 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
28847 || minibuf_level
28848 || NILP (Vresize_mini_windows));
28849
28850 dpyinfo = FRAME_DISPLAY_INFO (f);
28851 if (STRINGP (string))
28852 {
28853 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28854
28855 if (NILP (pointer))
28856 pointer = Fget_text_property (pos, Qpointer, string);
28857
28858 /* Change the mouse pointer according to what is under X/Y. */
28859 if (NILP (pointer)
28860 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28861 {
28862 Lisp_Object map;
28863 map = Fget_text_property (pos, Qlocal_map, string);
28864 if (!KEYMAPP (map))
28865 map = Fget_text_property (pos, Qkeymap, string);
28866 if (!KEYMAPP (map) && draggable)
28867 cursor = dpyinfo->vertical_scroll_bar_cursor;
28868 }
28869 }
28870 else if (draggable)
28871 /* Default mode-line pointer. */
28872 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28873 }
28874 #endif
28875 }
28876
28877 /* Change the mouse face according to what is under X/Y. */
28878 if (STRINGP (string))
28879 {
28880 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28881 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28882 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28883 && glyph)
28884 {
28885 Lisp_Object b, e;
28886
28887 struct glyph * tmp_glyph;
28888
28889 int gpos;
28890 int gseq_length;
28891 int total_pixel_width;
28892 ptrdiff_t begpos, endpos, ignore;
28893
28894 int vpos, hpos;
28895
28896 b = Fprevious_single_property_change (make_number (charpos + 1),
28897 Qmouse_face, string, Qnil);
28898 if (NILP (b))
28899 begpos = 0;
28900 else
28901 begpos = XINT (b);
28902
28903 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28904 if (NILP (e))
28905 endpos = SCHARS (string);
28906 else
28907 endpos = XINT (e);
28908
28909 /* Calculate the glyph position GPOS of GLYPH in the
28910 displayed string, relative to the beginning of the
28911 highlighted part of the string.
28912
28913 Note: GPOS is different from CHARPOS. CHARPOS is the
28914 position of GLYPH in the internal string object. A mode
28915 line string format has structures which are converted to
28916 a flattened string by the Emacs Lisp interpreter. The
28917 internal string is an element of those structures. The
28918 displayed string is the flattened string. */
28919 tmp_glyph = row_start_glyph;
28920 while (tmp_glyph < glyph
28921 && (!(EQ (tmp_glyph->object, glyph->object)
28922 && begpos <= tmp_glyph->charpos
28923 && tmp_glyph->charpos < endpos)))
28924 tmp_glyph++;
28925 gpos = glyph - tmp_glyph;
28926
28927 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28928 the highlighted part of the displayed string to which
28929 GLYPH belongs. Note: GSEQ_LENGTH is different from
28930 SCHARS (STRING), because the latter returns the length of
28931 the internal string. */
28932 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28933 tmp_glyph > glyph
28934 && (!(EQ (tmp_glyph->object, glyph->object)
28935 && begpos <= tmp_glyph->charpos
28936 && tmp_glyph->charpos < endpos));
28937 tmp_glyph--)
28938 ;
28939 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28940
28941 /* Calculate the total pixel width of all the glyphs between
28942 the beginning of the highlighted area and GLYPH. */
28943 total_pixel_width = 0;
28944 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28945 total_pixel_width += tmp_glyph->pixel_width;
28946
28947 /* Pre calculation of re-rendering position. Note: X is in
28948 column units here, after the call to mode_line_string or
28949 marginal_area_string. */
28950 hpos = x - gpos;
28951 vpos = (area == ON_MODE_LINE
28952 ? (w->current_matrix)->nrows - 1
28953 : 0);
28954
28955 /* If GLYPH's position is included in the region that is
28956 already drawn in mouse face, we have nothing to do. */
28957 if ( EQ (window, hlinfo->mouse_face_window)
28958 && (!row->reversed_p
28959 ? (hlinfo->mouse_face_beg_col <= hpos
28960 && hpos < hlinfo->mouse_face_end_col)
28961 /* In R2L rows we swap BEG and END, see below. */
28962 : (hlinfo->mouse_face_end_col <= hpos
28963 && hpos < hlinfo->mouse_face_beg_col))
28964 && hlinfo->mouse_face_beg_row == vpos )
28965 return;
28966
28967 if (clear_mouse_face (hlinfo))
28968 cursor = No_Cursor;
28969
28970 if (!row->reversed_p)
28971 {
28972 hlinfo->mouse_face_beg_col = hpos;
28973 hlinfo->mouse_face_beg_x = original_x_pixel
28974 - (total_pixel_width + dx);
28975 hlinfo->mouse_face_end_col = hpos + gseq_length;
28976 hlinfo->mouse_face_end_x = 0;
28977 }
28978 else
28979 {
28980 /* In R2L rows, show_mouse_face expects BEG and END
28981 coordinates to be swapped. */
28982 hlinfo->mouse_face_end_col = hpos;
28983 hlinfo->mouse_face_end_x = original_x_pixel
28984 - (total_pixel_width + dx);
28985 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28986 hlinfo->mouse_face_beg_x = 0;
28987 }
28988
28989 hlinfo->mouse_face_beg_row = vpos;
28990 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28991 hlinfo->mouse_face_past_end = 0;
28992 hlinfo->mouse_face_window = window;
28993
28994 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28995 charpos,
28996 0, &ignore,
28997 glyph->face_id,
28998 1);
28999 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29000
29001 if (NILP (pointer))
29002 pointer = Qhand;
29003 }
29004 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29005 clear_mouse_face (hlinfo);
29006 }
29007 #ifdef HAVE_WINDOW_SYSTEM
29008 if (FRAME_WINDOW_P (f))
29009 define_frame_cursor1 (f, cursor, pointer);
29010 #endif
29011 }
29012
29013
29014 /* EXPORT:
29015 Take proper action when the mouse has moved to position X, Y on
29016 frame F with regards to highlighting portions of display that have
29017 mouse-face properties. Also de-highlight portions of display where
29018 the mouse was before, set the mouse pointer shape as appropriate
29019 for the mouse coordinates, and activate help echo (tooltips).
29020 X and Y can be negative or out of range. */
29021
29022 void
29023 note_mouse_highlight (struct frame *f, int x, int y)
29024 {
29025 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29026 enum window_part part = ON_NOTHING;
29027 Lisp_Object window;
29028 struct window *w;
29029 Cursor cursor = No_Cursor;
29030 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29031 struct buffer *b;
29032
29033 /* When a menu is active, don't highlight because this looks odd. */
29034 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29035 if (popup_activated ())
29036 return;
29037 #endif
29038
29039 if (!f->glyphs_initialized_p
29040 || f->pointer_invisible)
29041 return;
29042
29043 hlinfo->mouse_face_mouse_x = x;
29044 hlinfo->mouse_face_mouse_y = y;
29045 hlinfo->mouse_face_mouse_frame = f;
29046
29047 if (hlinfo->mouse_face_defer)
29048 return;
29049
29050 /* Which window is that in? */
29051 window = window_from_coordinates (f, x, y, &part, 1);
29052
29053 /* If displaying active text in another window, clear that. */
29054 if (! EQ (window, hlinfo->mouse_face_window)
29055 /* Also clear if we move out of text area in same window. */
29056 || (!NILP (hlinfo->mouse_face_window)
29057 && !NILP (window)
29058 && part != ON_TEXT
29059 && part != ON_MODE_LINE
29060 && part != ON_HEADER_LINE))
29061 clear_mouse_face (hlinfo);
29062
29063 /* Not on a window -> return. */
29064 if (!WINDOWP (window))
29065 return;
29066
29067 /* Reset help_echo_string. It will get recomputed below. */
29068 help_echo_string = Qnil;
29069
29070 /* Convert to window-relative pixel coordinates. */
29071 w = XWINDOW (window);
29072 frame_to_window_pixel_xy (w, &x, &y);
29073
29074 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29075 /* Handle tool-bar window differently since it doesn't display a
29076 buffer. */
29077 if (EQ (window, f->tool_bar_window))
29078 {
29079 note_tool_bar_highlight (f, x, y);
29080 return;
29081 }
29082 #endif
29083
29084 /* Mouse is on the mode, header line or margin? */
29085 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29086 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29087 {
29088 note_mode_line_or_margin_highlight (window, x, y, part);
29089
29090 #ifdef HAVE_WINDOW_SYSTEM
29091 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29092 {
29093 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29094 /* Show non-text cursor (Bug#16647). */
29095 goto set_cursor;
29096 }
29097 else
29098 #endif
29099 return;
29100 }
29101
29102 #ifdef HAVE_WINDOW_SYSTEM
29103 if (part == ON_VERTICAL_BORDER)
29104 {
29105 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29106 help_echo_string = build_string ("drag-mouse-1: resize");
29107 }
29108 else if (part == ON_RIGHT_DIVIDER)
29109 {
29110 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29111 help_echo_string = build_string ("drag-mouse-1: resize");
29112 }
29113 else if (part == ON_BOTTOM_DIVIDER)
29114 if (! WINDOW_BOTTOMMOST_P (w)
29115 || minibuf_level
29116 || NILP (Vresize_mini_windows))
29117 {
29118 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29119 help_echo_string = build_string ("drag-mouse-1: resize");
29120 }
29121 else
29122 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29123 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29124 || part == ON_SCROLL_BAR)
29125 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29126 else
29127 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29128 #endif
29129
29130 /* Are we in a window whose display is up to date?
29131 And verify the buffer's text has not changed. */
29132 b = XBUFFER (w->contents);
29133 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29134 {
29135 int hpos, vpos, dx, dy, area = LAST_AREA;
29136 ptrdiff_t pos;
29137 struct glyph *glyph;
29138 Lisp_Object object;
29139 Lisp_Object mouse_face = Qnil, position;
29140 Lisp_Object *overlay_vec = NULL;
29141 ptrdiff_t i, noverlays;
29142 struct buffer *obuf;
29143 ptrdiff_t obegv, ozv;
29144 int same_region;
29145
29146 /* Find the glyph under X/Y. */
29147 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29148
29149 #ifdef HAVE_WINDOW_SYSTEM
29150 /* Look for :pointer property on image. */
29151 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29152 {
29153 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29154 if (img != NULL && IMAGEP (img->spec))
29155 {
29156 Lisp_Object image_map, hotspot;
29157 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29158 !NILP (image_map))
29159 && (hotspot = find_hot_spot (image_map,
29160 glyph->slice.img.x + dx,
29161 glyph->slice.img.y + dy),
29162 CONSP (hotspot))
29163 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29164 {
29165 Lisp_Object plist;
29166
29167 /* Could check XCAR (hotspot) to see if we enter/leave
29168 this hot-spot.
29169 If so, we could look for mouse-enter, mouse-leave
29170 properties in PLIST (and do something...). */
29171 hotspot = XCDR (hotspot);
29172 if (CONSP (hotspot)
29173 && (plist = XCAR (hotspot), CONSP (plist)))
29174 {
29175 pointer = Fplist_get (plist, Qpointer);
29176 if (NILP (pointer))
29177 pointer = Qhand;
29178 help_echo_string = Fplist_get (plist, Qhelp_echo);
29179 if (!NILP (help_echo_string))
29180 {
29181 help_echo_window = window;
29182 help_echo_object = glyph->object;
29183 help_echo_pos = glyph->charpos;
29184 }
29185 }
29186 }
29187 if (NILP (pointer))
29188 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29189 }
29190 }
29191 #endif /* HAVE_WINDOW_SYSTEM */
29192
29193 /* Clear mouse face if X/Y not over text. */
29194 if (glyph == NULL
29195 || area != TEXT_AREA
29196 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29197 /* Glyph's OBJECT is an integer for glyphs inserted by the
29198 display engine for its internal purposes, like truncation
29199 and continuation glyphs and blanks beyond the end of
29200 line's text on text terminals. If we are over such a
29201 glyph, we are not over any text. */
29202 || INTEGERP (glyph->object)
29203 /* R2L rows have a stretch glyph at their front, which
29204 stands for no text, whereas L2R rows have no glyphs at
29205 all beyond the end of text. Treat such stretch glyphs
29206 like we do with NULL glyphs in L2R rows. */
29207 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29208 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29209 && glyph->type == STRETCH_GLYPH
29210 && glyph->avoid_cursor_p))
29211 {
29212 if (clear_mouse_face (hlinfo))
29213 cursor = No_Cursor;
29214 #ifdef HAVE_WINDOW_SYSTEM
29215 if (FRAME_WINDOW_P (f) && NILP (pointer))
29216 {
29217 if (area != TEXT_AREA)
29218 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29219 else
29220 pointer = Vvoid_text_area_pointer;
29221 }
29222 #endif
29223 goto set_cursor;
29224 }
29225
29226 pos = glyph->charpos;
29227 object = glyph->object;
29228 if (!STRINGP (object) && !BUFFERP (object))
29229 goto set_cursor;
29230
29231 /* If we get an out-of-range value, return now; avoid an error. */
29232 if (BUFFERP (object) && pos > BUF_Z (b))
29233 goto set_cursor;
29234
29235 /* Make the window's buffer temporarily current for
29236 overlays_at and compute_char_face. */
29237 obuf = current_buffer;
29238 current_buffer = b;
29239 obegv = BEGV;
29240 ozv = ZV;
29241 BEGV = BEG;
29242 ZV = Z;
29243
29244 /* Is this char mouse-active or does it have help-echo? */
29245 position = make_number (pos);
29246
29247 if (BUFFERP (object))
29248 {
29249 /* Put all the overlays we want in a vector in overlay_vec. */
29250 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29251 /* Sort overlays into increasing priority order. */
29252 noverlays = sort_overlays (overlay_vec, noverlays, w);
29253 }
29254 else
29255 noverlays = 0;
29256
29257 if (NILP (Vmouse_highlight))
29258 {
29259 clear_mouse_face (hlinfo);
29260 goto check_help_echo;
29261 }
29262
29263 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29264
29265 if (same_region)
29266 cursor = No_Cursor;
29267
29268 /* Check mouse-face highlighting. */
29269 if (! same_region
29270 /* If there exists an overlay with mouse-face overlapping
29271 the one we are currently highlighting, we have to
29272 check if we enter the overlapping overlay, and then
29273 highlight only that. */
29274 || (OVERLAYP (hlinfo->mouse_face_overlay)
29275 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29276 {
29277 /* Find the highest priority overlay with a mouse-face. */
29278 Lisp_Object overlay = Qnil;
29279 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29280 {
29281 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29282 if (!NILP (mouse_face))
29283 overlay = overlay_vec[i];
29284 }
29285
29286 /* If we're highlighting the same overlay as before, there's
29287 no need to do that again. */
29288 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29289 goto check_help_echo;
29290 hlinfo->mouse_face_overlay = overlay;
29291
29292 /* Clear the display of the old active region, if any. */
29293 if (clear_mouse_face (hlinfo))
29294 cursor = No_Cursor;
29295
29296 /* If no overlay applies, get a text property. */
29297 if (NILP (overlay))
29298 mouse_face = Fget_text_property (position, Qmouse_face, object);
29299
29300 /* Next, compute the bounds of the mouse highlighting and
29301 display it. */
29302 if (!NILP (mouse_face) && STRINGP (object))
29303 {
29304 /* The mouse-highlighting comes from a display string
29305 with a mouse-face. */
29306 Lisp_Object s, e;
29307 ptrdiff_t ignore;
29308
29309 s = Fprevious_single_property_change
29310 (make_number (pos + 1), Qmouse_face, object, Qnil);
29311 e = Fnext_single_property_change
29312 (position, Qmouse_face, object, Qnil);
29313 if (NILP (s))
29314 s = make_number (0);
29315 if (NILP (e))
29316 e = make_number (SCHARS (object));
29317 mouse_face_from_string_pos (w, hlinfo, object,
29318 XINT (s), XINT (e));
29319 hlinfo->mouse_face_past_end = 0;
29320 hlinfo->mouse_face_window = window;
29321 hlinfo->mouse_face_face_id
29322 = face_at_string_position (w, object, pos, 0, &ignore,
29323 glyph->face_id, 1);
29324 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29325 cursor = No_Cursor;
29326 }
29327 else
29328 {
29329 /* The mouse-highlighting, if any, comes from an overlay
29330 or text property in the buffer. */
29331 Lisp_Object buffer IF_LINT (= Qnil);
29332 Lisp_Object disp_string IF_LINT (= Qnil);
29333
29334 if (STRINGP (object))
29335 {
29336 /* If we are on a display string with no mouse-face,
29337 check if the text under it has one. */
29338 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29339 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29340 pos = string_buffer_position (object, start);
29341 if (pos > 0)
29342 {
29343 mouse_face = get_char_property_and_overlay
29344 (make_number (pos), Qmouse_face, w->contents, &overlay);
29345 buffer = w->contents;
29346 disp_string = object;
29347 }
29348 }
29349 else
29350 {
29351 buffer = object;
29352 disp_string = Qnil;
29353 }
29354
29355 if (!NILP (mouse_face))
29356 {
29357 Lisp_Object before, after;
29358 Lisp_Object before_string, after_string;
29359 /* To correctly find the limits of mouse highlight
29360 in a bidi-reordered buffer, we must not use the
29361 optimization of limiting the search in
29362 previous-single-property-change and
29363 next-single-property-change, because
29364 rows_from_pos_range needs the real start and end
29365 positions to DTRT in this case. That's because
29366 the first row visible in a window does not
29367 necessarily display the character whose position
29368 is the smallest. */
29369 Lisp_Object lim1
29370 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29371 ? Fmarker_position (w->start)
29372 : Qnil;
29373 Lisp_Object lim2
29374 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29375 ? make_number (BUF_Z (XBUFFER (buffer))
29376 - w->window_end_pos)
29377 : Qnil;
29378
29379 if (NILP (overlay))
29380 {
29381 /* Handle the text property case. */
29382 before = Fprevious_single_property_change
29383 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29384 after = Fnext_single_property_change
29385 (make_number (pos), Qmouse_face, buffer, lim2);
29386 before_string = after_string = Qnil;
29387 }
29388 else
29389 {
29390 /* Handle the overlay case. */
29391 before = Foverlay_start (overlay);
29392 after = Foverlay_end (overlay);
29393 before_string = Foverlay_get (overlay, Qbefore_string);
29394 after_string = Foverlay_get (overlay, Qafter_string);
29395
29396 if (!STRINGP (before_string)) before_string = Qnil;
29397 if (!STRINGP (after_string)) after_string = Qnil;
29398 }
29399
29400 mouse_face_from_buffer_pos (window, hlinfo, pos,
29401 NILP (before)
29402 ? 1
29403 : XFASTINT (before),
29404 NILP (after)
29405 ? BUF_Z (XBUFFER (buffer))
29406 : XFASTINT (after),
29407 before_string, after_string,
29408 disp_string);
29409 cursor = No_Cursor;
29410 }
29411 }
29412 }
29413
29414 check_help_echo:
29415
29416 /* Look for a `help-echo' property. */
29417 if (NILP (help_echo_string)) {
29418 Lisp_Object help, overlay;
29419
29420 /* Check overlays first. */
29421 help = overlay = Qnil;
29422 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29423 {
29424 overlay = overlay_vec[i];
29425 help = Foverlay_get (overlay, Qhelp_echo);
29426 }
29427
29428 if (!NILP (help))
29429 {
29430 help_echo_string = help;
29431 help_echo_window = window;
29432 help_echo_object = overlay;
29433 help_echo_pos = pos;
29434 }
29435 else
29436 {
29437 Lisp_Object obj = glyph->object;
29438 ptrdiff_t charpos = glyph->charpos;
29439
29440 /* Try text properties. */
29441 if (STRINGP (obj)
29442 && charpos >= 0
29443 && charpos < SCHARS (obj))
29444 {
29445 help = Fget_text_property (make_number (charpos),
29446 Qhelp_echo, obj);
29447 if (NILP (help))
29448 {
29449 /* If the string itself doesn't specify a help-echo,
29450 see if the buffer text ``under'' it does. */
29451 struct glyph_row *r
29452 = MATRIX_ROW (w->current_matrix, vpos);
29453 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29454 ptrdiff_t p = string_buffer_position (obj, start);
29455 if (p > 0)
29456 {
29457 help = Fget_char_property (make_number (p),
29458 Qhelp_echo, w->contents);
29459 if (!NILP (help))
29460 {
29461 charpos = p;
29462 obj = w->contents;
29463 }
29464 }
29465 }
29466 }
29467 else if (BUFFERP (obj)
29468 && charpos >= BEGV
29469 && charpos < ZV)
29470 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29471 obj);
29472
29473 if (!NILP (help))
29474 {
29475 help_echo_string = help;
29476 help_echo_window = window;
29477 help_echo_object = obj;
29478 help_echo_pos = charpos;
29479 }
29480 }
29481 }
29482
29483 #ifdef HAVE_WINDOW_SYSTEM
29484 /* Look for a `pointer' property. */
29485 if (FRAME_WINDOW_P (f) && NILP (pointer))
29486 {
29487 /* Check overlays first. */
29488 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29489 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29490
29491 if (NILP (pointer))
29492 {
29493 Lisp_Object obj = glyph->object;
29494 ptrdiff_t charpos = glyph->charpos;
29495
29496 /* Try text properties. */
29497 if (STRINGP (obj)
29498 && charpos >= 0
29499 && charpos < SCHARS (obj))
29500 {
29501 pointer = Fget_text_property (make_number (charpos),
29502 Qpointer, obj);
29503 if (NILP (pointer))
29504 {
29505 /* If the string itself doesn't specify a pointer,
29506 see if the buffer text ``under'' it does. */
29507 struct glyph_row *r
29508 = MATRIX_ROW (w->current_matrix, vpos);
29509 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29510 ptrdiff_t p = string_buffer_position (obj, start);
29511 if (p > 0)
29512 pointer = Fget_char_property (make_number (p),
29513 Qpointer, w->contents);
29514 }
29515 }
29516 else if (BUFFERP (obj)
29517 && charpos >= BEGV
29518 && charpos < ZV)
29519 pointer = Fget_text_property (make_number (charpos),
29520 Qpointer, obj);
29521 }
29522 }
29523 #endif /* HAVE_WINDOW_SYSTEM */
29524
29525 BEGV = obegv;
29526 ZV = ozv;
29527 current_buffer = obuf;
29528 }
29529
29530 set_cursor:
29531
29532 #ifdef HAVE_WINDOW_SYSTEM
29533 if (FRAME_WINDOW_P (f))
29534 define_frame_cursor1 (f, cursor, pointer);
29535 #else
29536 /* This is here to prevent a compiler error, about "label at end of
29537 compound statement". */
29538 return;
29539 #endif
29540 }
29541
29542
29543 /* EXPORT for RIF:
29544 Clear any mouse-face on window W. This function is part of the
29545 redisplay interface, and is called from try_window_id and similar
29546 functions to ensure the mouse-highlight is off. */
29547
29548 void
29549 x_clear_window_mouse_face (struct window *w)
29550 {
29551 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29552 Lisp_Object window;
29553
29554 block_input ();
29555 XSETWINDOW (window, w);
29556 if (EQ (window, hlinfo->mouse_face_window))
29557 clear_mouse_face (hlinfo);
29558 unblock_input ();
29559 }
29560
29561
29562 /* EXPORT:
29563 Just discard the mouse face information for frame F, if any.
29564 This is used when the size of F is changed. */
29565
29566 void
29567 cancel_mouse_face (struct frame *f)
29568 {
29569 Lisp_Object window;
29570 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29571
29572 window = hlinfo->mouse_face_window;
29573 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29574 reset_mouse_highlight (hlinfo);
29575 }
29576
29577
29578 \f
29579 /***********************************************************************
29580 Exposure Events
29581 ***********************************************************************/
29582
29583 #ifdef HAVE_WINDOW_SYSTEM
29584
29585 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29586 which intersects rectangle R. R is in window-relative coordinates. */
29587
29588 static void
29589 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29590 enum glyph_row_area area)
29591 {
29592 struct glyph *first = row->glyphs[area];
29593 struct glyph *end = row->glyphs[area] + row->used[area];
29594 struct glyph *last;
29595 int first_x, start_x, x;
29596
29597 if (area == TEXT_AREA && row->fill_line_p)
29598 /* If row extends face to end of line write the whole line. */
29599 draw_glyphs (w, 0, row, area,
29600 0, row->used[area],
29601 DRAW_NORMAL_TEXT, 0);
29602 else
29603 {
29604 /* Set START_X to the window-relative start position for drawing glyphs of
29605 AREA. The first glyph of the text area can be partially visible.
29606 The first glyphs of other areas cannot. */
29607 start_x = window_box_left_offset (w, area);
29608 x = start_x;
29609 if (area == TEXT_AREA)
29610 x += row->x;
29611
29612 /* Find the first glyph that must be redrawn. */
29613 while (first < end
29614 && x + first->pixel_width < r->x)
29615 {
29616 x += first->pixel_width;
29617 ++first;
29618 }
29619
29620 /* Find the last one. */
29621 last = first;
29622 first_x = x;
29623 while (last < end
29624 && x < r->x + r->width)
29625 {
29626 x += last->pixel_width;
29627 ++last;
29628 }
29629
29630 /* Repaint. */
29631 if (last > first)
29632 draw_glyphs (w, first_x - start_x, row, area,
29633 first - row->glyphs[area], last - row->glyphs[area],
29634 DRAW_NORMAL_TEXT, 0);
29635 }
29636 }
29637
29638
29639 /* Redraw the parts of the glyph row ROW on window W intersecting
29640 rectangle R. R is in window-relative coordinates. Value is
29641 non-zero if mouse-face was overwritten. */
29642
29643 static int
29644 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29645 {
29646 eassert (row->enabled_p);
29647
29648 if (row->mode_line_p || w->pseudo_window_p)
29649 draw_glyphs (w, 0, row, TEXT_AREA,
29650 0, row->used[TEXT_AREA],
29651 DRAW_NORMAL_TEXT, 0);
29652 else
29653 {
29654 if (row->used[LEFT_MARGIN_AREA])
29655 expose_area (w, row, r, LEFT_MARGIN_AREA);
29656 if (row->used[TEXT_AREA])
29657 expose_area (w, row, r, TEXT_AREA);
29658 if (row->used[RIGHT_MARGIN_AREA])
29659 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29660 draw_row_fringe_bitmaps (w, row);
29661 }
29662
29663 return row->mouse_face_p;
29664 }
29665
29666
29667 /* Redraw those parts of glyphs rows during expose event handling that
29668 overlap other rows. Redrawing of an exposed line writes over parts
29669 of lines overlapping that exposed line; this function fixes that.
29670
29671 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29672 row in W's current matrix that is exposed and overlaps other rows.
29673 LAST_OVERLAPPING_ROW is the last such row. */
29674
29675 static void
29676 expose_overlaps (struct window *w,
29677 struct glyph_row *first_overlapping_row,
29678 struct glyph_row *last_overlapping_row,
29679 XRectangle *r)
29680 {
29681 struct glyph_row *row;
29682
29683 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29684 if (row->overlapping_p)
29685 {
29686 eassert (row->enabled_p && !row->mode_line_p);
29687
29688 row->clip = r;
29689 if (row->used[LEFT_MARGIN_AREA])
29690 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29691
29692 if (row->used[TEXT_AREA])
29693 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29694
29695 if (row->used[RIGHT_MARGIN_AREA])
29696 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29697 row->clip = NULL;
29698 }
29699 }
29700
29701
29702 /* Return non-zero if W's cursor intersects rectangle R. */
29703
29704 static int
29705 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29706 {
29707 XRectangle cr, result;
29708 struct glyph *cursor_glyph;
29709 struct glyph_row *row;
29710
29711 if (w->phys_cursor.vpos >= 0
29712 && w->phys_cursor.vpos < w->current_matrix->nrows
29713 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29714 row->enabled_p)
29715 && row->cursor_in_fringe_p)
29716 {
29717 /* Cursor is in the fringe. */
29718 cr.x = window_box_right_offset (w,
29719 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29720 ? RIGHT_MARGIN_AREA
29721 : TEXT_AREA));
29722 cr.y = row->y;
29723 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29724 cr.height = row->height;
29725 return x_intersect_rectangles (&cr, r, &result);
29726 }
29727
29728 cursor_glyph = get_phys_cursor_glyph (w);
29729 if (cursor_glyph)
29730 {
29731 /* r is relative to W's box, but w->phys_cursor.x is relative
29732 to left edge of W's TEXT area. Adjust it. */
29733 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29734 cr.y = w->phys_cursor.y;
29735 cr.width = cursor_glyph->pixel_width;
29736 cr.height = w->phys_cursor_height;
29737 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29738 I assume the effect is the same -- and this is portable. */
29739 return x_intersect_rectangles (&cr, r, &result);
29740 }
29741 /* If we don't understand the format, pretend we're not in the hot-spot. */
29742 return 0;
29743 }
29744
29745
29746 /* EXPORT:
29747 Draw a vertical window border to the right of window W if W doesn't
29748 have vertical scroll bars. */
29749
29750 void
29751 x_draw_vertical_border (struct window *w)
29752 {
29753 struct frame *f = XFRAME (WINDOW_FRAME (w));
29754
29755 /* We could do better, if we knew what type of scroll-bar the adjacent
29756 windows (on either side) have... But we don't :-(
29757 However, I think this works ok. ++KFS 2003-04-25 */
29758
29759 /* Redraw borders between horizontally adjacent windows. Don't
29760 do it for frames with vertical scroll bars because either the
29761 right scroll bar of a window, or the left scroll bar of its
29762 neighbor will suffice as a border. */
29763 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29764 return;
29765
29766 /* Note: It is necessary to redraw both the left and the right
29767 borders, for when only this single window W is being
29768 redisplayed. */
29769 if (!WINDOW_RIGHTMOST_P (w)
29770 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29771 {
29772 int x0, x1, y0, y1;
29773
29774 window_box_edges (w, &x0, &y0, &x1, &y1);
29775 y1 -= 1;
29776
29777 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29778 x1 -= 1;
29779
29780 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29781 }
29782
29783 if (!WINDOW_LEFTMOST_P (w)
29784 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29785 {
29786 int x0, x1, y0, y1;
29787
29788 window_box_edges (w, &x0, &y0, &x1, &y1);
29789 y1 -= 1;
29790
29791 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29792 x0 -= 1;
29793
29794 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29795 }
29796 }
29797
29798
29799 /* Draw window dividers for window W. */
29800
29801 void
29802 x_draw_right_divider (struct window *w)
29803 {
29804 struct frame *f = WINDOW_XFRAME (w);
29805
29806 if (w->mini || w->pseudo_window_p)
29807 return;
29808 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29809 {
29810 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29811 int x1 = WINDOW_RIGHT_EDGE_X (w);
29812 int y0 = WINDOW_TOP_EDGE_Y (w);
29813 /* The bottom divider prevails. */
29814 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29815
29816 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29817 }
29818 }
29819
29820 static void
29821 x_draw_bottom_divider (struct window *w)
29822 {
29823 struct frame *f = XFRAME (WINDOW_FRAME (w));
29824
29825 if (w->mini || w->pseudo_window_p)
29826 return;
29827 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29828 {
29829 int x0 = WINDOW_LEFT_EDGE_X (w);
29830 int x1 = WINDOW_RIGHT_EDGE_X (w);
29831 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29832 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29833
29834 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29835 }
29836 }
29837
29838 /* Redraw the part of window W intersection rectangle FR. Pixel
29839 coordinates in FR are frame-relative. Call this function with
29840 input blocked. Value is non-zero if the exposure overwrites
29841 mouse-face. */
29842
29843 static int
29844 expose_window (struct window *w, XRectangle *fr)
29845 {
29846 struct frame *f = XFRAME (w->frame);
29847 XRectangle wr, r;
29848 int mouse_face_overwritten_p = 0;
29849
29850 /* If window is not yet fully initialized, do nothing. This can
29851 happen when toolkit scroll bars are used and a window is split.
29852 Reconfiguring the scroll bar will generate an expose for a newly
29853 created window. */
29854 if (w->current_matrix == NULL)
29855 return 0;
29856
29857 /* When we're currently updating the window, display and current
29858 matrix usually don't agree. Arrange for a thorough display
29859 later. */
29860 if (w->must_be_updated_p)
29861 {
29862 SET_FRAME_GARBAGED (f);
29863 return 0;
29864 }
29865
29866 /* Frame-relative pixel rectangle of W. */
29867 wr.x = WINDOW_LEFT_EDGE_X (w);
29868 wr.y = WINDOW_TOP_EDGE_Y (w);
29869 wr.width = WINDOW_PIXEL_WIDTH (w);
29870 wr.height = WINDOW_PIXEL_HEIGHT (w);
29871
29872 if (x_intersect_rectangles (fr, &wr, &r))
29873 {
29874 int yb = window_text_bottom_y (w);
29875 struct glyph_row *row;
29876 int cursor_cleared_p, phys_cursor_on_p;
29877 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29878
29879 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29880 r.x, r.y, r.width, r.height));
29881
29882 /* Convert to window coordinates. */
29883 r.x -= WINDOW_LEFT_EDGE_X (w);
29884 r.y -= WINDOW_TOP_EDGE_Y (w);
29885
29886 /* Turn off the cursor. */
29887 if (!w->pseudo_window_p
29888 && phys_cursor_in_rect_p (w, &r))
29889 {
29890 x_clear_cursor (w);
29891 cursor_cleared_p = 1;
29892 }
29893 else
29894 cursor_cleared_p = 0;
29895
29896 /* If the row containing the cursor extends face to end of line,
29897 then expose_area might overwrite the cursor outside the
29898 rectangle and thus notice_overwritten_cursor might clear
29899 w->phys_cursor_on_p. We remember the original value and
29900 check later if it is changed. */
29901 phys_cursor_on_p = w->phys_cursor_on_p;
29902
29903 /* Update lines intersecting rectangle R. */
29904 first_overlapping_row = last_overlapping_row = NULL;
29905 for (row = w->current_matrix->rows;
29906 row->enabled_p;
29907 ++row)
29908 {
29909 int y0 = row->y;
29910 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29911
29912 if ((y0 >= r.y && y0 < r.y + r.height)
29913 || (y1 > r.y && y1 < r.y + r.height)
29914 || (r.y >= y0 && r.y < y1)
29915 || (r.y + r.height > y0 && r.y + r.height < y1))
29916 {
29917 /* A header line may be overlapping, but there is no need
29918 to fix overlapping areas for them. KFS 2005-02-12 */
29919 if (row->overlapping_p && !row->mode_line_p)
29920 {
29921 if (first_overlapping_row == NULL)
29922 first_overlapping_row = row;
29923 last_overlapping_row = row;
29924 }
29925
29926 row->clip = fr;
29927 if (expose_line (w, row, &r))
29928 mouse_face_overwritten_p = 1;
29929 row->clip = NULL;
29930 }
29931 else if (row->overlapping_p)
29932 {
29933 /* We must redraw a row overlapping the exposed area. */
29934 if (y0 < r.y
29935 ? y0 + row->phys_height > r.y
29936 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29937 {
29938 if (first_overlapping_row == NULL)
29939 first_overlapping_row = row;
29940 last_overlapping_row = row;
29941 }
29942 }
29943
29944 if (y1 >= yb)
29945 break;
29946 }
29947
29948 /* Display the mode line if there is one. */
29949 if (WINDOW_WANTS_MODELINE_P (w)
29950 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29951 row->enabled_p)
29952 && row->y < r.y + r.height)
29953 {
29954 if (expose_line (w, row, &r))
29955 mouse_face_overwritten_p = 1;
29956 }
29957
29958 if (!w->pseudo_window_p)
29959 {
29960 /* Fix the display of overlapping rows. */
29961 if (first_overlapping_row)
29962 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29963 fr);
29964
29965 /* Draw border between windows. */
29966 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29967 x_draw_right_divider (w);
29968 else
29969 x_draw_vertical_border (w);
29970
29971 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29972 x_draw_bottom_divider (w);
29973
29974 /* Turn the cursor on again. */
29975 if (cursor_cleared_p
29976 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29977 update_window_cursor (w, 1);
29978 }
29979 }
29980
29981 return mouse_face_overwritten_p;
29982 }
29983
29984
29985
29986 /* Redraw (parts) of all windows in the window tree rooted at W that
29987 intersect R. R contains frame pixel coordinates. Value is
29988 non-zero if the exposure overwrites mouse-face. */
29989
29990 static int
29991 expose_window_tree (struct window *w, XRectangle *r)
29992 {
29993 struct frame *f = XFRAME (w->frame);
29994 int mouse_face_overwritten_p = 0;
29995
29996 while (w && !FRAME_GARBAGED_P (f))
29997 {
29998 if (WINDOWP (w->contents))
29999 mouse_face_overwritten_p
30000 |= expose_window_tree (XWINDOW (w->contents), r);
30001 else
30002 mouse_face_overwritten_p |= expose_window (w, r);
30003
30004 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30005 }
30006
30007 return mouse_face_overwritten_p;
30008 }
30009
30010
30011 /* EXPORT:
30012 Redisplay an exposed area of frame F. X and Y are the upper-left
30013 corner of the exposed rectangle. W and H are width and height of
30014 the exposed area. All are pixel values. W or H zero means redraw
30015 the entire frame. */
30016
30017 void
30018 expose_frame (struct frame *f, int x, int y, int w, int h)
30019 {
30020 XRectangle r;
30021 int mouse_face_overwritten_p = 0;
30022
30023 TRACE ((stderr, "expose_frame "));
30024
30025 /* No need to redraw if frame will be redrawn soon. */
30026 if (FRAME_GARBAGED_P (f))
30027 {
30028 TRACE ((stderr, " garbaged\n"));
30029 return;
30030 }
30031
30032 /* If basic faces haven't been realized yet, there is no point in
30033 trying to redraw anything. This can happen when we get an expose
30034 event while Emacs is starting, e.g. by moving another window. */
30035 if (FRAME_FACE_CACHE (f) == NULL
30036 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30037 {
30038 TRACE ((stderr, " no faces\n"));
30039 return;
30040 }
30041
30042 if (w == 0 || h == 0)
30043 {
30044 r.x = r.y = 0;
30045 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
30046 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
30047 }
30048 else
30049 {
30050 r.x = x;
30051 r.y = y;
30052 r.width = w;
30053 r.height = h;
30054 }
30055
30056 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30057 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30058
30059 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30060 if (WINDOWP (f->tool_bar_window))
30061 mouse_face_overwritten_p
30062 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30063 #endif
30064
30065 #ifdef HAVE_X_WINDOWS
30066 #ifndef MSDOS
30067 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30068 if (WINDOWP (f->menu_bar_window))
30069 mouse_face_overwritten_p
30070 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30071 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30072 #endif
30073 #endif
30074
30075 /* Some window managers support a focus-follows-mouse style with
30076 delayed raising of frames. Imagine a partially obscured frame,
30077 and moving the mouse into partially obscured mouse-face on that
30078 frame. The visible part of the mouse-face will be highlighted,
30079 then the WM raises the obscured frame. With at least one WM, KDE
30080 2.1, Emacs is not getting any event for the raising of the frame
30081 (even tried with SubstructureRedirectMask), only Expose events.
30082 These expose events will draw text normally, i.e. not
30083 highlighted. Which means we must redo the highlight here.
30084 Subsume it under ``we love X''. --gerd 2001-08-15 */
30085 /* Included in Windows version because Windows most likely does not
30086 do the right thing if any third party tool offers
30087 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30088 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30089 {
30090 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30091 if (f == hlinfo->mouse_face_mouse_frame)
30092 {
30093 int mouse_x = hlinfo->mouse_face_mouse_x;
30094 int mouse_y = hlinfo->mouse_face_mouse_y;
30095 clear_mouse_face (hlinfo);
30096 note_mouse_highlight (f, mouse_x, mouse_y);
30097 }
30098 }
30099 }
30100
30101
30102 /* EXPORT:
30103 Determine the intersection of two rectangles R1 and R2. Return
30104 the intersection in *RESULT. Value is non-zero if RESULT is not
30105 empty. */
30106
30107 int
30108 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30109 {
30110 XRectangle *left, *right;
30111 XRectangle *upper, *lower;
30112 int intersection_p = 0;
30113
30114 /* Rearrange so that R1 is the left-most rectangle. */
30115 if (r1->x < r2->x)
30116 left = r1, right = r2;
30117 else
30118 left = r2, right = r1;
30119
30120 /* X0 of the intersection is right.x0, if this is inside R1,
30121 otherwise there is no intersection. */
30122 if (right->x <= left->x + left->width)
30123 {
30124 result->x = right->x;
30125
30126 /* The right end of the intersection is the minimum of
30127 the right ends of left and right. */
30128 result->width = (min (left->x + left->width, right->x + right->width)
30129 - result->x);
30130
30131 /* Same game for Y. */
30132 if (r1->y < r2->y)
30133 upper = r1, lower = r2;
30134 else
30135 upper = r2, lower = r1;
30136
30137 /* The upper end of the intersection is lower.y0, if this is inside
30138 of upper. Otherwise, there is no intersection. */
30139 if (lower->y <= upper->y + upper->height)
30140 {
30141 result->y = lower->y;
30142
30143 /* The lower end of the intersection is the minimum of the lower
30144 ends of upper and lower. */
30145 result->height = (min (lower->y + lower->height,
30146 upper->y + upper->height)
30147 - result->y);
30148 intersection_p = 1;
30149 }
30150 }
30151
30152 return intersection_p;
30153 }
30154
30155 #endif /* HAVE_WINDOW_SYSTEM */
30156
30157 \f
30158 /***********************************************************************
30159 Initialization
30160 ***********************************************************************/
30161
30162 void
30163 syms_of_xdisp (void)
30164 {
30165 Vwith_echo_area_save_vector = Qnil;
30166 staticpro (&Vwith_echo_area_save_vector);
30167
30168 Vmessage_stack = Qnil;
30169 staticpro (&Vmessage_stack);
30170
30171 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30172 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30173
30174 message_dolog_marker1 = Fmake_marker ();
30175 staticpro (&message_dolog_marker1);
30176 message_dolog_marker2 = Fmake_marker ();
30177 staticpro (&message_dolog_marker2);
30178 message_dolog_marker3 = Fmake_marker ();
30179 staticpro (&message_dolog_marker3);
30180
30181 #ifdef GLYPH_DEBUG
30182 defsubr (&Sdump_frame_glyph_matrix);
30183 defsubr (&Sdump_glyph_matrix);
30184 defsubr (&Sdump_glyph_row);
30185 defsubr (&Sdump_tool_bar_row);
30186 defsubr (&Strace_redisplay);
30187 defsubr (&Strace_to_stderr);
30188 #endif
30189 #ifdef HAVE_WINDOW_SYSTEM
30190 defsubr (&Stool_bar_height);
30191 defsubr (&Slookup_image_map);
30192 #endif
30193 defsubr (&Sline_pixel_height);
30194 defsubr (&Sformat_mode_line);
30195 defsubr (&Sinvisible_p);
30196 defsubr (&Scurrent_bidi_paragraph_direction);
30197 defsubr (&Swindow_text_pixel_size);
30198 defsubr (&Smove_point_visually);
30199
30200 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30201 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30202 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30203 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30204 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30205 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30206 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30207 DEFSYM (Qeval, "eval");
30208 DEFSYM (QCdata, ":data");
30209 DEFSYM (Qdisplay, "display");
30210 DEFSYM (Qspace_width, "space-width");
30211 DEFSYM (Qraise, "raise");
30212 DEFSYM (Qslice, "slice");
30213 DEFSYM (Qspace, "space");
30214 DEFSYM (Qmargin, "margin");
30215 DEFSYM (Qpointer, "pointer");
30216 DEFSYM (Qleft_margin, "left-margin");
30217 DEFSYM (Qright_margin, "right-margin");
30218 DEFSYM (Qcenter, "center");
30219 DEFSYM (Qline_height, "line-height");
30220 DEFSYM (QCalign_to, ":align-to");
30221 DEFSYM (QCrelative_width, ":relative-width");
30222 DEFSYM (QCrelative_height, ":relative-height");
30223 DEFSYM (QCeval, ":eval");
30224 DEFSYM (QCpropertize, ":propertize");
30225 DEFSYM (QCfile, ":file");
30226 DEFSYM (Qfontified, "fontified");
30227 DEFSYM (Qfontification_functions, "fontification-functions");
30228 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30229 DEFSYM (Qescape_glyph, "escape-glyph");
30230 DEFSYM (Qnobreak_space, "nobreak-space");
30231 DEFSYM (Qimage, "image");
30232 DEFSYM (Qtext, "text");
30233 DEFSYM (Qboth, "both");
30234 DEFSYM (Qboth_horiz, "both-horiz");
30235 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30236 DEFSYM (QCmap, ":map");
30237 DEFSYM (QCpointer, ":pointer");
30238 DEFSYM (Qrect, "rect");
30239 DEFSYM (Qcircle, "circle");
30240 DEFSYM (Qpoly, "poly");
30241 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30242 DEFSYM (Qgrow_only, "grow-only");
30243 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30244 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30245 DEFSYM (Qposition, "position");
30246 DEFSYM (Qbuffer_position, "buffer-position");
30247 DEFSYM (Qobject, "object");
30248 DEFSYM (Qbar, "bar");
30249 DEFSYM (Qhbar, "hbar");
30250 DEFSYM (Qbox, "box");
30251 DEFSYM (Qhollow, "hollow");
30252 DEFSYM (Qhand, "hand");
30253 DEFSYM (Qarrow, "arrow");
30254 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30255
30256 list_of_error = list1 (list2 (intern_c_string ("error"),
30257 intern_c_string ("void-variable")));
30258 staticpro (&list_of_error);
30259
30260 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30261 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30262 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30263 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30264
30265 echo_buffer[0] = echo_buffer[1] = Qnil;
30266 staticpro (&echo_buffer[0]);
30267 staticpro (&echo_buffer[1]);
30268
30269 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30270 staticpro (&echo_area_buffer[0]);
30271 staticpro (&echo_area_buffer[1]);
30272
30273 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30274 staticpro (&Vmessages_buffer_name);
30275
30276 mode_line_proptrans_alist = Qnil;
30277 staticpro (&mode_line_proptrans_alist);
30278 mode_line_string_list = Qnil;
30279 staticpro (&mode_line_string_list);
30280 mode_line_string_face = Qnil;
30281 staticpro (&mode_line_string_face);
30282 mode_line_string_face_prop = Qnil;
30283 staticpro (&mode_line_string_face_prop);
30284 Vmode_line_unwind_vector = Qnil;
30285 staticpro (&Vmode_line_unwind_vector);
30286
30287 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30288
30289 help_echo_string = Qnil;
30290 staticpro (&help_echo_string);
30291 help_echo_object = Qnil;
30292 staticpro (&help_echo_object);
30293 help_echo_window = Qnil;
30294 staticpro (&help_echo_window);
30295 previous_help_echo_string = Qnil;
30296 staticpro (&previous_help_echo_string);
30297 help_echo_pos = -1;
30298
30299 DEFSYM (Qright_to_left, "right-to-left");
30300 DEFSYM (Qleft_to_right, "left-to-right");
30301
30302 #ifdef HAVE_WINDOW_SYSTEM
30303 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30304 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30305 For example, if a block cursor is over a tab, it will be drawn as
30306 wide as that tab on the display. */);
30307 x_stretch_cursor_p = 0;
30308 #endif
30309
30310 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30311 doc: /* Non-nil means highlight trailing whitespace.
30312 The face used for trailing whitespace is `trailing-whitespace'. */);
30313 Vshow_trailing_whitespace = Qnil;
30314
30315 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30316 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30317 If the value is t, Emacs highlights non-ASCII chars which have the
30318 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30319 or `escape-glyph' face respectively.
30320
30321 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30322 U+2011 (non-breaking hyphen) are affected.
30323
30324 Any other non-nil value means to display these characters as a escape
30325 glyph followed by an ordinary space or hyphen.
30326
30327 A value of nil means no special handling of these characters. */);
30328 Vnobreak_char_display = Qt;
30329
30330 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30331 doc: /* The pointer shape to show in void text areas.
30332 A value of nil means to show the text pointer. Other options are `arrow',
30333 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
30334 Vvoid_text_area_pointer = Qarrow;
30335
30336 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30337 doc: /* Non-nil means don't actually do any redisplay.
30338 This is used for internal purposes. */);
30339 Vinhibit_redisplay = Qnil;
30340
30341 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30342 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30343 Vglobal_mode_string = Qnil;
30344
30345 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30346 doc: /* Marker for where to display an arrow on top of the buffer text.
30347 This must be the beginning of a line in order to work.
30348 See also `overlay-arrow-string'. */);
30349 Voverlay_arrow_position = Qnil;
30350
30351 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30352 doc: /* String to display as an arrow in non-window frames.
30353 See also `overlay-arrow-position'. */);
30354 Voverlay_arrow_string = build_pure_c_string ("=>");
30355
30356 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30357 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30358 The symbols on this list are examined during redisplay to determine
30359 where to display overlay arrows. */);
30360 Voverlay_arrow_variable_list
30361 = list1 (intern_c_string ("overlay-arrow-position"));
30362
30363 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30364 doc: /* The number of lines to try scrolling a window by when point moves out.
30365 If that fails to bring point back on frame, point is centered instead.
30366 If this is zero, point is always centered after it moves off frame.
30367 If you want scrolling to always be a line at a time, you should set
30368 `scroll-conservatively' to a large value rather than set this to 1. */);
30369
30370 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30371 doc: /* Scroll up to this many lines, to bring point back on screen.
30372 If point moves off-screen, redisplay will scroll by up to
30373 `scroll-conservatively' lines in order to bring point just barely
30374 onto the screen again. If that cannot be done, then redisplay
30375 recenters point as usual.
30376
30377 If the value is greater than 100, redisplay will never recenter point,
30378 but will always scroll just enough text to bring point into view, even
30379 if you move far away.
30380
30381 A value of zero means always recenter point if it moves off screen. */);
30382 scroll_conservatively = 0;
30383
30384 DEFVAR_INT ("scroll-margin", scroll_margin,
30385 doc: /* Number of lines of margin at the top and bottom of a window.
30386 Recenter the window whenever point gets within this many lines
30387 of the top or bottom of the window. */);
30388 scroll_margin = 0;
30389
30390 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30391 doc: /* Pixels per inch value for non-window system displays.
30392 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30393 Vdisplay_pixels_per_inch = make_float (72.0);
30394
30395 #ifdef GLYPH_DEBUG
30396 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30397 #endif
30398
30399 DEFVAR_LISP ("truncate-partial-width-windows",
30400 Vtruncate_partial_width_windows,
30401 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30402 For an integer value, truncate lines in each window narrower than the
30403 full frame width, provided the window width is less than that integer;
30404 otherwise, respect the value of `truncate-lines'.
30405
30406 For any other non-nil value, truncate lines in all windows that do
30407 not span the full frame width.
30408
30409 A value of nil means to respect the value of `truncate-lines'.
30410
30411 If `word-wrap' is enabled, you might want to reduce this. */);
30412 Vtruncate_partial_width_windows = make_number (50);
30413
30414 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30415 doc: /* Maximum buffer size for which line number should be displayed.
30416 If the buffer is bigger than this, the line number does not appear
30417 in the mode line. A value of nil means no limit. */);
30418 Vline_number_display_limit = Qnil;
30419
30420 DEFVAR_INT ("line-number-display-limit-width",
30421 line_number_display_limit_width,
30422 doc: /* Maximum line width (in characters) for line number display.
30423 If the average length of the lines near point is bigger than this, then the
30424 line number may be omitted from the mode line. */);
30425 line_number_display_limit_width = 200;
30426
30427 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30428 doc: /* Non-nil means highlight region even in nonselected windows. */);
30429 highlight_nonselected_windows = 0;
30430
30431 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30432 doc: /* Non-nil if more than one frame is visible on this display.
30433 Minibuffer-only frames don't count, but iconified frames do.
30434 This variable is not guaranteed to be accurate except while processing
30435 `frame-title-format' and `icon-title-format'. */);
30436
30437 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30438 doc: /* Template for displaying the title bar of visible frames.
30439 \(Assuming the window manager supports this feature.)
30440
30441 This variable has the same structure as `mode-line-format', except that
30442 the %c and %l constructs are ignored. It is used only on frames for
30443 which no explicit name has been set \(see `modify-frame-parameters'). */);
30444
30445 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30446 doc: /* Template for displaying the title bar of an iconified frame.
30447 \(Assuming the window manager supports this feature.)
30448 This variable has the same structure as `mode-line-format' (which see),
30449 and is used only on frames for which no explicit name has been set
30450 \(see `modify-frame-parameters'). */);
30451 Vicon_title_format
30452 = Vframe_title_format
30453 = listn (CONSTYPE_PURE, 3,
30454 intern_c_string ("multiple-frames"),
30455 build_pure_c_string ("%b"),
30456 listn (CONSTYPE_PURE, 4,
30457 empty_unibyte_string,
30458 intern_c_string ("invocation-name"),
30459 build_pure_c_string ("@"),
30460 intern_c_string ("system-name")));
30461
30462 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30463 doc: /* Maximum number of lines to keep in the message log buffer.
30464 If nil, disable message logging. If t, log messages but don't truncate
30465 the buffer when it becomes large. */);
30466 Vmessage_log_max = make_number (1000);
30467
30468 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30469 doc: /* Functions called before redisplay, if window sizes have changed.
30470 The value should be a list of functions that take one argument.
30471 Just before redisplay, for each frame, if any of its windows have changed
30472 size since the last redisplay, or have been split or deleted,
30473 all the functions in the list are called, with the frame as argument. */);
30474 Vwindow_size_change_functions = Qnil;
30475
30476 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30477 doc: /* List of functions to call before redisplaying a window with scrolling.
30478 Each function is called with two arguments, the window and its new
30479 display-start position. Note that these functions are also called by
30480 `set-window-buffer'. Also note that the value of `window-end' is not
30481 valid when these functions are called.
30482
30483 Warning: Do not use this feature to alter the way the window
30484 is scrolled. It is not designed for that, and such use probably won't
30485 work. */);
30486 Vwindow_scroll_functions = Qnil;
30487
30488 DEFVAR_LISP ("window-text-change-functions",
30489 Vwindow_text_change_functions,
30490 doc: /* Functions to call in redisplay when text in the window might change. */);
30491 Vwindow_text_change_functions = Qnil;
30492
30493 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30494 doc: /* Functions called when redisplay of a window reaches the end trigger.
30495 Each function is called with two arguments, the window and the end trigger value.
30496 See `set-window-redisplay-end-trigger'. */);
30497 Vredisplay_end_trigger_functions = Qnil;
30498
30499 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30500 doc: /* Non-nil means autoselect window with mouse pointer.
30501 If nil, do not autoselect windows.
30502 A positive number means delay autoselection by that many seconds: a
30503 window is autoselected only after the mouse has remained in that
30504 window for the duration of the delay.
30505 A negative number has a similar effect, but causes windows to be
30506 autoselected only after the mouse has stopped moving. \(Because of
30507 the way Emacs compares mouse events, you will occasionally wait twice
30508 that time before the window gets selected.\)
30509 Any other value means to autoselect window instantaneously when the
30510 mouse pointer enters it.
30511
30512 Autoselection selects the minibuffer only if it is active, and never
30513 unselects the minibuffer if it is active.
30514
30515 When customizing this variable make sure that the actual value of
30516 `focus-follows-mouse' matches the behavior of your window manager. */);
30517 Vmouse_autoselect_window = Qnil;
30518
30519 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30520 doc: /* Non-nil means automatically resize tool-bars.
30521 This dynamically changes the tool-bar's height to the minimum height
30522 that is needed to make all tool-bar items visible.
30523 If value is `grow-only', the tool-bar's height is only increased
30524 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30525 Vauto_resize_tool_bars = Qt;
30526
30527 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30528 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30529 auto_raise_tool_bar_buttons_p = 1;
30530
30531 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30532 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30533 make_cursor_line_fully_visible_p = 1;
30534
30535 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30536 doc: /* Border below tool-bar in pixels.
30537 If an integer, use it as the height of the border.
30538 If it is one of `internal-border-width' or `border-width', use the
30539 value of the corresponding frame parameter.
30540 Otherwise, no border is added below the tool-bar. */);
30541 Vtool_bar_border = Qinternal_border_width;
30542
30543 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30544 doc: /* Margin around tool-bar buttons in pixels.
30545 If an integer, use that for both horizontal and vertical margins.
30546 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30547 HORZ specifying the horizontal margin, and VERT specifying the
30548 vertical margin. */);
30549 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30550
30551 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30552 doc: /* Relief thickness of tool-bar buttons. */);
30553 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30554
30555 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30556 doc: /* Tool bar style to use.
30557 It can be one of
30558 image - show images only
30559 text - show text only
30560 both - show both, text below image
30561 both-horiz - show text to the right of the image
30562 text-image-horiz - show text to the left of the image
30563 any other - use system default or image if no system default.
30564
30565 This variable only affects the GTK+ toolkit version of Emacs. */);
30566 Vtool_bar_style = Qnil;
30567
30568 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30569 doc: /* Maximum number of characters a label can have to be shown.
30570 The tool bar style must also show labels for this to have any effect, see
30571 `tool-bar-style'. */);
30572 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30573
30574 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30575 doc: /* List of functions to call to fontify regions of text.
30576 Each function is called with one argument POS. Functions must
30577 fontify a region starting at POS in the current buffer, and give
30578 fontified regions the property `fontified'. */);
30579 Vfontification_functions = Qnil;
30580 Fmake_variable_buffer_local (Qfontification_functions);
30581
30582 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30583 unibyte_display_via_language_environment,
30584 doc: /* Non-nil means display unibyte text according to language environment.
30585 Specifically, this means that raw bytes in the range 160-255 decimal
30586 are displayed by converting them to the equivalent multibyte characters
30587 according to the current language environment. As a result, they are
30588 displayed according to the current fontset.
30589
30590 Note that this variable affects only how these bytes are displayed,
30591 but does not change the fact they are interpreted as raw bytes. */);
30592 unibyte_display_via_language_environment = 0;
30593
30594 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30595 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30596 If a float, it specifies a fraction of the mini-window frame's height.
30597 If an integer, it specifies a number of lines. */);
30598 Vmax_mini_window_height = make_float (0.25);
30599
30600 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30601 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30602 A value of nil means don't automatically resize mini-windows.
30603 A value of t means resize them to fit the text displayed in them.
30604 A value of `grow-only', the default, means let mini-windows grow only;
30605 they return to their normal size when the minibuffer is closed, or the
30606 echo area becomes empty. */);
30607 Vresize_mini_windows = Qgrow_only;
30608
30609 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30610 doc: /* Alist specifying how to blink the cursor off.
30611 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30612 `cursor-type' frame-parameter or variable equals ON-STATE,
30613 comparing using `equal', Emacs uses OFF-STATE to specify
30614 how to blink it off. ON-STATE and OFF-STATE are values for
30615 the `cursor-type' frame parameter.
30616
30617 If a frame's ON-STATE has no entry in this list,
30618 the frame's other specifications determine how to blink the cursor off. */);
30619 Vblink_cursor_alist = Qnil;
30620
30621 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30622 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30623 If non-nil, windows are automatically scrolled horizontally to make
30624 point visible. */);
30625 automatic_hscrolling_p = 1;
30626 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30627
30628 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30629 doc: /* How many columns away from the window edge point is allowed to get
30630 before automatic hscrolling will horizontally scroll the window. */);
30631 hscroll_margin = 5;
30632
30633 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30634 doc: /* How many columns to scroll the window when point gets too close to the edge.
30635 When point is less than `hscroll-margin' columns from the window
30636 edge, automatic hscrolling will scroll the window by the amount of columns
30637 determined by this variable. If its value is a positive integer, scroll that
30638 many columns. If it's a positive floating-point number, it specifies the
30639 fraction of the window's width to scroll. If it's nil or zero, point will be
30640 centered horizontally after the scroll. Any other value, including negative
30641 numbers, are treated as if the value were zero.
30642
30643 Automatic hscrolling always moves point outside the scroll margin, so if
30644 point was more than scroll step columns inside the margin, the window will
30645 scroll more than the value given by the scroll step.
30646
30647 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30648 and `scroll-right' overrides this variable's effect. */);
30649 Vhscroll_step = make_number (0);
30650
30651 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30652 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30653 Bind this around calls to `message' to let it take effect. */);
30654 message_truncate_lines = 0;
30655
30656 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30657 doc: /* Normal hook run to update the menu bar definitions.
30658 Redisplay runs this hook before it redisplays the menu bar.
30659 This is used to update menus such as Buffers, whose contents depend on
30660 various data. */);
30661 Vmenu_bar_update_hook = Qnil;
30662
30663 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30664 doc: /* Frame for which we are updating a menu.
30665 The enable predicate for a menu binding should check this variable. */);
30666 Vmenu_updating_frame = Qnil;
30667
30668 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30669 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30670 inhibit_menubar_update = 0;
30671
30672 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30673 doc: /* Prefix prepended to all continuation lines at display time.
30674 The value may be a string, an image, or a stretch-glyph; it is
30675 interpreted in the same way as the value of a `display' text property.
30676
30677 This variable is overridden by any `wrap-prefix' text or overlay
30678 property.
30679
30680 To add a prefix to non-continuation lines, use `line-prefix'. */);
30681 Vwrap_prefix = Qnil;
30682 DEFSYM (Qwrap_prefix, "wrap-prefix");
30683 Fmake_variable_buffer_local (Qwrap_prefix);
30684
30685 DEFVAR_LISP ("line-prefix", Vline_prefix,
30686 doc: /* Prefix prepended to all non-continuation lines at display time.
30687 The value may be a string, an image, or a stretch-glyph; it is
30688 interpreted in the same way as the value of a `display' text property.
30689
30690 This variable is overridden by any `line-prefix' text or overlay
30691 property.
30692
30693 To add a prefix to continuation lines, use `wrap-prefix'. */);
30694 Vline_prefix = Qnil;
30695 DEFSYM (Qline_prefix, "line-prefix");
30696 Fmake_variable_buffer_local (Qline_prefix);
30697
30698 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30699 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30700 inhibit_eval_during_redisplay = 0;
30701
30702 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30703 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30704 inhibit_free_realized_faces = 0;
30705
30706 #ifdef GLYPH_DEBUG
30707 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30708 doc: /* Inhibit try_window_id display optimization. */);
30709 inhibit_try_window_id = 0;
30710
30711 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30712 doc: /* Inhibit try_window_reusing display optimization. */);
30713 inhibit_try_window_reusing = 0;
30714
30715 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30716 doc: /* Inhibit try_cursor_movement display optimization. */);
30717 inhibit_try_cursor_movement = 0;
30718 #endif /* GLYPH_DEBUG */
30719
30720 DEFVAR_INT ("overline-margin", overline_margin,
30721 doc: /* Space between overline and text, in pixels.
30722 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30723 margin to the character height. */);
30724 overline_margin = 2;
30725
30726 DEFVAR_INT ("underline-minimum-offset",
30727 underline_minimum_offset,
30728 doc: /* Minimum distance between baseline and underline.
30729 This can improve legibility of underlined text at small font sizes,
30730 particularly when using variable `x-use-underline-position-properties'
30731 with fonts that specify an UNDERLINE_POSITION relatively close to the
30732 baseline. The default value is 1. */);
30733 underline_minimum_offset = 1;
30734
30735 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30736 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30737 This feature only works when on a window system that can change
30738 cursor shapes. */);
30739 display_hourglass_p = 1;
30740
30741 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30742 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30743 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30744
30745 #ifdef HAVE_WINDOW_SYSTEM
30746 hourglass_atimer = NULL;
30747 hourglass_shown_p = 0;
30748 #endif /* HAVE_WINDOW_SYSTEM */
30749
30750 DEFSYM (Qglyphless_char, "glyphless-char");
30751 DEFSYM (Qhex_code, "hex-code");
30752 DEFSYM (Qempty_box, "empty-box");
30753 DEFSYM (Qthin_space, "thin-space");
30754 DEFSYM (Qzero_width, "zero-width");
30755
30756 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30757 doc: /* Function run just before redisplay.
30758 It is called with one argument, which is the set of windows that are to
30759 be redisplayed. This set can be nil (meaning, only the selected window),
30760 or t (meaning all windows). */);
30761 Vpre_redisplay_function = intern ("ignore");
30762
30763 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30764 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30765
30766 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30767 doc: /* Char-table defining glyphless characters.
30768 Each element, if non-nil, should be one of the following:
30769 an ASCII acronym string: display this string in a box
30770 `hex-code': display the hexadecimal code of a character in a box
30771 `empty-box': display as an empty box
30772 `thin-space': display as 1-pixel width space
30773 `zero-width': don't display
30774 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30775 display method for graphical terminals and text terminals respectively.
30776 GRAPHICAL and TEXT should each have one of the values listed above.
30777
30778 The char-table has one extra slot to control the display of a character for
30779 which no font is found. This slot only takes effect on graphical terminals.
30780 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30781 `thin-space'. The default is `empty-box'.
30782
30783 If a character has a non-nil entry in an active display table, the
30784 display table takes effect; in this case, Emacs does not consult
30785 `glyphless-char-display' at all. */);
30786 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30787 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30788 Qempty_box);
30789
30790 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30791 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30792 Vdebug_on_message = Qnil;
30793
30794 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30795 doc: /* */);
30796 Vredisplay__all_windows_cause
30797 = Fmake_vector (make_number (100), make_number (0));
30798
30799 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30800 doc: /* */);
30801 Vredisplay__mode_lines_cause
30802 = Fmake_vector (make_number (100), make_number (0));
30803 }
30804
30805
30806 /* Initialize this module when Emacs starts. */
30807
30808 void
30809 init_xdisp (void)
30810 {
30811 CHARPOS (this_line_start_pos) = 0;
30812
30813 if (!noninteractive)
30814 {
30815 struct window *m = XWINDOW (minibuf_window);
30816 Lisp_Object frame = m->frame;
30817 struct frame *f = XFRAME (frame);
30818 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30819 struct window *r = XWINDOW (root);
30820 int i;
30821
30822 echo_area_window = minibuf_window;
30823
30824 r->top_line = FRAME_TOP_MARGIN (f);
30825 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30826 r->total_cols = FRAME_COLS (f);
30827 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30828 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30829 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30830
30831 m->top_line = FRAME_LINES (f) - 1;
30832 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30833 m->total_cols = FRAME_COLS (f);
30834 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30835 m->total_lines = 1;
30836 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30837
30838 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30839 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30840 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30841
30842 /* The default ellipsis glyphs `...'. */
30843 for (i = 0; i < 3; ++i)
30844 default_invis_vector[i] = make_number ('.');
30845 }
30846
30847 {
30848 /* Allocate the buffer for frame titles.
30849 Also used for `format-mode-line'. */
30850 int size = 100;
30851 mode_line_noprop_buf = xmalloc (size);
30852 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30853 mode_line_noprop_ptr = mode_line_noprop_buf;
30854 mode_line_target = MODE_LINE_DISPLAY;
30855 }
30856
30857 help_echo_showing_p = 0;
30858 }
30859
30860 #ifdef HAVE_WINDOW_SYSTEM
30861
30862 /* Platform-independent portion of hourglass implementation. */
30863
30864 /* Cancel a currently active hourglass timer, and start a new one. */
30865 void
30866 start_hourglass (void)
30867 {
30868 struct timespec delay;
30869
30870 cancel_hourglass ();
30871
30872 if (INTEGERP (Vhourglass_delay)
30873 && XINT (Vhourglass_delay) > 0)
30874 delay = make_timespec (min (XINT (Vhourglass_delay),
30875 TYPE_MAXIMUM (time_t)),
30876 0);
30877 else if (FLOATP (Vhourglass_delay)
30878 && XFLOAT_DATA (Vhourglass_delay) > 0)
30879 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30880 else
30881 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30882
30883 #ifdef HAVE_NTGUI
30884 {
30885 extern void w32_note_current_window (void);
30886 w32_note_current_window ();
30887 }
30888 #endif /* HAVE_NTGUI */
30889
30890 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30891 show_hourglass, NULL);
30892 }
30893
30894
30895 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30896 shown. */
30897 void
30898 cancel_hourglass (void)
30899 {
30900 if (hourglass_atimer)
30901 {
30902 cancel_atimer (hourglass_atimer);
30903 hourglass_atimer = NULL;
30904 }
30905
30906 if (hourglass_shown_p)
30907 hide_hourglass ();
30908 }
30909
30910 #endif /* HAVE_WINDOW_SYSTEM */