* src/xdisp.c (REDISPLAY_SOME_P): New macro.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
304
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
308
309 #define INFINITY 10000000
310
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
323
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
326
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
330
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
333
334 static Lisp_Object Qfontification_functions;
335
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
339
340 /* Non-nil means don't actually do any redisplay. */
341
342 Lisp_Object Qinhibit_redisplay;
343
344 /* Names of text properties relevant for redisplay. */
345
346 Lisp_Object Qdisplay;
347
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
356
357 #ifdef HAVE_WINDOW_SYSTEM
358
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
361
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
369
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
373
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
377
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
389
390 /* Name of the face used to highlight trailing whitespace. */
391
392 static Lisp_Object Qtrailing_whitespace;
393
394 /* Name and number of the face used to highlight escape glyphs. */
395
396 static Lisp_Object Qescape_glyph;
397
398 /* Name and number of the face used to highlight non-breaking spaces. */
399
400 static Lisp_Object Qnobreak_space;
401
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
404
405 Lisp_Object Qimage;
406
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
411
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
414
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
417
418 bool noninteractive_need_newline;
419
420 /* Non-zero means print newline to message log before next message. */
421
422 static bool message_log_need_newline;
423
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
430 \f
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
435
436 static struct text_pos this_line_start_pos;
437
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
440
441 static struct text_pos this_line_end_pos;
442
443 /* The vertical positions and the height of this line. */
444
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
448
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
451
452 static int this_line_start_x;
453
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
457
458 static struct text_pos this_line_min_pos;
459
460 /* Buffer that this_line_.* variables are referring to. */
461
462 static struct buffer *this_line_buffer;
463
464
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
469
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
471
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
474
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
476
477 Lisp_Object Qmenu_bar_update_hook;
478
479 /* Nonzero if an overlay arrow has been displayed in this window. */
480
481 static bool overlay_arrow_seen;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
490
491 Lisp_Object echo_area_window;
492
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
497
498 static Lisp_Object Vmessage_stack;
499
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
502
503 static bool message_enable_multibyte;
504
505 /* Nonzero if we should redraw the mode lines on the next redisplay.
506 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
507 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
508 (the number used is then only used to track down the cause for this
509 full-redisplay). */
510
511 int update_mode_lines;
512
513 /* Nonzero if window sizes or contents other than selected-window have changed
514 since last redisplay that finished.
515 If it has value REDISPLAY_SOME, then only redisplay the windows where
516 the `redisplay' bit has been set. Otherwise, redisplay all windows
517 (the number used is then only used to track down the cause for this
518 full-redisplay). */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero after display_mode_line if %l was used and it displayed a
523 line number. */
524
525 static bool line_number_displayed;
526
527 /* The name of the *Messages* buffer, a string. */
528
529 static Lisp_Object Vmessages_buffer_name;
530
531 /* Current, index 0, and last displayed echo area message. Either
532 buffers from echo_buffers, or nil to indicate no message. */
533
534 Lisp_Object echo_area_buffer[2];
535
536 /* The buffers referenced from echo_area_buffer. */
537
538 static Lisp_Object echo_buffer[2];
539
540 /* A vector saved used in with_area_buffer to reduce consing. */
541
542 static Lisp_Object Vwith_echo_area_save_vector;
543
544 /* Non-zero means display_echo_area should display the last echo area
545 message again. Set by redisplay_preserve_echo_area. */
546
547 static bool display_last_displayed_message_p;
548
549 /* Nonzero if echo area is being used by print; zero if being used by
550 message. */
551
552 static bool message_buf_print;
553
554 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
555
556 static Lisp_Object Qinhibit_menubar_update;
557 static Lisp_Object Qmessage_truncate_lines;
558
559 /* Set to 1 in clear_message to make redisplay_internal aware
560 of an emptied echo area. */
561
562 static bool message_cleared_p;
563
564 /* A scratch glyph row with contents used for generating truncation
565 glyphs. Also used in direct_output_for_insert. */
566
567 #define MAX_SCRATCH_GLYPHS 100
568 static struct glyph_row scratch_glyph_row;
569 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
570
571 /* Ascent and height of the last line processed by move_it_to. */
572
573 static int last_height;
574
575 /* Non-zero if there's a help-echo in the echo area. */
576
577 bool help_echo_showing_p;
578
579 /* The maximum distance to look ahead for text properties. Values
580 that are too small let us call compute_char_face and similar
581 functions too often which is expensive. Values that are too large
582 let us call compute_char_face and alike too often because we
583 might not be interested in text properties that far away. */
584
585 #define TEXT_PROP_DISTANCE_LIMIT 100
586
587 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
588 iterator state and later restore it. This is needed because the
589 bidi iterator on bidi.c keeps a stacked cache of its states, which
590 is really a singleton. When we use scratch iterator objects to
591 move around the buffer, we can cause the bidi cache to be pushed or
592 popped, and therefore we need to restore the cache state when we
593 return to the original iterator. */
594 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
595 do { \
596 if (CACHE) \
597 bidi_unshelve_cache (CACHE, 1); \
598 ITCOPY = ITORIG; \
599 CACHE = bidi_shelve_cache (); \
600 } while (0)
601
602 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
603 do { \
604 if (pITORIG != pITCOPY) \
605 *(pITORIG) = *(pITCOPY); \
606 bidi_unshelve_cache (CACHE, 0); \
607 CACHE = NULL; \
608 } while (0)
609
610 /* Functions to mark elements as needing redisplay. */
611 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
612
613 void
614 redisplay_other_windows (void)
615 {
616 if (!windows_or_buffers_changed)
617 windows_or_buffers_changed = REDISPLAY_SOME;
618 }
619
620 void
621 wset_redisplay (struct window *w)
622 {
623 redisplay_other_windows ();
624 w->redisplay = true;
625 }
626
627 void
628 fset_redisplay (struct frame *f)
629 {
630 redisplay_other_windows ();
631 f->redisplay = true;
632 }
633
634 void
635 bset_redisplay (struct buffer *b)
636 {
637 int count = buffer_window_count (b);
638 if (count > 0)
639 {
640 /* ... it's visible in other window than selected, */
641 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
642 redisplay_other_windows ();
643 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
644 so that if we later set windows_or_buffers_changed, this buffer will
645 not be omitted. */
646 b->text->redisplay = true;
647 }
648 }
649
650 void
651 bset_update_mode_line (struct buffer *b)
652 {
653 if (!update_mode_lines)
654 update_mode_lines = REDISPLAY_SOME;
655 b->text->redisplay = true;
656 }
657
658 #ifdef GLYPH_DEBUG
659
660 /* Non-zero means print traces of redisplay if compiled with
661 GLYPH_DEBUG defined. */
662
663 int trace_redisplay_p;
664
665 #endif /* GLYPH_DEBUG */
666
667 #ifdef DEBUG_TRACE_MOVE
668 /* Non-zero means trace with TRACE_MOVE to stderr. */
669 int trace_move;
670
671 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
672 #else
673 #define TRACE_MOVE(x) (void) 0
674 #endif
675
676 static Lisp_Object Qauto_hscroll_mode;
677
678 /* Buffer being redisplayed -- for redisplay_window_error. */
679
680 static struct buffer *displayed_buffer;
681
682 /* Value returned from text property handlers (see below). */
683
684 enum prop_handled
685 {
686 HANDLED_NORMALLY,
687 HANDLED_RECOMPUTE_PROPS,
688 HANDLED_OVERLAY_STRING_CONSUMED,
689 HANDLED_RETURN
690 };
691
692 /* A description of text properties that redisplay is interested
693 in. */
694
695 struct props
696 {
697 /* The name of the property. */
698 Lisp_Object *name;
699
700 /* A unique index for the property. */
701 enum prop_idx idx;
702
703 /* A handler function called to set up iterator IT from the property
704 at IT's current position. Value is used to steer handle_stop. */
705 enum prop_handled (*handler) (struct it *it);
706 };
707
708 static enum prop_handled handle_face_prop (struct it *);
709 static enum prop_handled handle_invisible_prop (struct it *);
710 static enum prop_handled handle_display_prop (struct it *);
711 static enum prop_handled handle_composition_prop (struct it *);
712 static enum prop_handled handle_overlay_change (struct it *);
713 static enum prop_handled handle_fontified_prop (struct it *);
714
715 /* Properties handled by iterators. */
716
717 static struct props it_props[] =
718 {
719 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
720 /* Handle `face' before `display' because some sub-properties of
721 `display' need to know the face. */
722 {&Qface, FACE_PROP_IDX, handle_face_prop},
723 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
724 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
725 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
726 {NULL, 0, NULL}
727 };
728
729 /* Value is the position described by X. If X is a marker, value is
730 the marker_position of X. Otherwise, value is X. */
731
732 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
733
734 /* Enumeration returned by some move_it_.* functions internally. */
735
736 enum move_it_result
737 {
738 /* Not used. Undefined value. */
739 MOVE_UNDEFINED,
740
741 /* Move ended at the requested buffer position or ZV. */
742 MOVE_POS_MATCH_OR_ZV,
743
744 /* Move ended at the requested X pixel position. */
745 MOVE_X_REACHED,
746
747 /* Move within a line ended at the end of a line that must be
748 continued. */
749 MOVE_LINE_CONTINUED,
750
751 /* Move within a line ended at the end of a line that would
752 be displayed truncated. */
753 MOVE_LINE_TRUNCATED,
754
755 /* Move within a line ended at a line end. */
756 MOVE_NEWLINE_OR_CR
757 };
758
759 /* This counter is used to clear the face cache every once in a while
760 in redisplay_internal. It is incremented for each redisplay.
761 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
762 cleared. */
763
764 #define CLEAR_FACE_CACHE_COUNT 500
765 static int clear_face_cache_count;
766
767 /* Similarly for the image cache. */
768
769 #ifdef HAVE_WINDOW_SYSTEM
770 #define CLEAR_IMAGE_CACHE_COUNT 101
771 static int clear_image_cache_count;
772
773 /* Null glyph slice */
774 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
775 #endif
776
777 /* True while redisplay_internal is in progress. */
778
779 bool redisplaying_p;
780
781 static Lisp_Object Qinhibit_free_realized_faces;
782 static Lisp_Object Qmode_line_default_help_echo;
783
784 /* If a string, XTread_socket generates an event to display that string.
785 (The display is done in read_char.) */
786
787 Lisp_Object help_echo_string;
788 Lisp_Object help_echo_window;
789 Lisp_Object help_echo_object;
790 ptrdiff_t help_echo_pos;
791
792 /* Temporary variable for XTread_socket. */
793
794 Lisp_Object previous_help_echo_string;
795
796 /* Platform-independent portion of hourglass implementation. */
797
798 #ifdef HAVE_WINDOW_SYSTEM
799
800 /* Non-zero means an hourglass cursor is currently shown. */
801 bool hourglass_shown_p;
802
803 /* If non-null, an asynchronous timer that, when it expires, displays
804 an hourglass cursor on all frames. */
805 struct atimer *hourglass_atimer;
806
807 #endif /* HAVE_WINDOW_SYSTEM */
808
809 /* Name of the face used to display glyphless characters. */
810 static Lisp_Object Qglyphless_char;
811
812 /* Symbol for the purpose of Vglyphless_char_display. */
813 static Lisp_Object Qglyphless_char_display;
814
815 /* Method symbols for Vglyphless_char_display. */
816 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
817
818 /* Default number of seconds to wait before displaying an hourglass
819 cursor. */
820 #define DEFAULT_HOURGLASS_DELAY 1
821
822 #ifdef HAVE_WINDOW_SYSTEM
823
824 /* Default pixel width of `thin-space' display method. */
825 #define THIN_SPACE_WIDTH 1
826
827 #endif /* HAVE_WINDOW_SYSTEM */
828
829 /* Function prototypes. */
830
831 static void setup_for_ellipsis (struct it *, int);
832 static void set_iterator_to_next (struct it *, int);
833 static void mark_window_display_accurate_1 (struct window *, int);
834 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
835 static int display_prop_string_p (Lisp_Object, Lisp_Object);
836 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
837 static int cursor_row_p (struct glyph_row *);
838 static int redisplay_mode_lines (Lisp_Object, int);
839 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
840
841 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
842
843 static void handle_line_prefix (struct it *);
844
845 static void pint2str (char *, int, ptrdiff_t);
846 static void pint2hrstr (char *, int, ptrdiff_t);
847 static struct text_pos run_window_scroll_functions (Lisp_Object,
848 struct text_pos);
849 static int text_outside_line_unchanged_p (struct window *,
850 ptrdiff_t, ptrdiff_t);
851 static void store_mode_line_noprop_char (char);
852 static int store_mode_line_noprop (const char *, int, int);
853 static void handle_stop (struct it *);
854 static void handle_stop_backwards (struct it *, ptrdiff_t);
855 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
856 static void ensure_echo_area_buffers (void);
857 static void unwind_with_echo_area_buffer (Lisp_Object);
858 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
859 static int with_echo_area_buffer (struct window *, int,
860 int (*) (ptrdiff_t, Lisp_Object),
861 ptrdiff_t, Lisp_Object);
862 static void clear_garbaged_frames (void);
863 static int current_message_1 (ptrdiff_t, Lisp_Object);
864 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
865 static void set_message (Lisp_Object);
866 static int set_message_1 (ptrdiff_t, Lisp_Object);
867 static int display_echo_area (struct window *);
868 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
869 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
870 static void unwind_redisplay (void);
871 static int string_char_and_length (const unsigned char *, int *);
872 static struct text_pos display_prop_end (struct it *, Lisp_Object,
873 struct text_pos);
874 static int compute_window_start_on_continuation_line (struct window *);
875 static void insert_left_trunc_glyphs (struct it *);
876 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
877 Lisp_Object);
878 static void extend_face_to_end_of_line (struct it *);
879 static int append_space_for_newline (struct it *, int);
880 static int cursor_row_fully_visible_p (struct window *, int, int);
881 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
882 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
883 static int trailing_whitespace_p (ptrdiff_t);
884 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
885 static void push_it (struct it *, struct text_pos *);
886 static void iterate_out_of_display_property (struct it *);
887 static void pop_it (struct it *);
888 static void sync_frame_with_window_matrix_rows (struct window *);
889 static void redisplay_internal (void);
890 static int echo_area_display (int);
891 static void redisplay_windows (Lisp_Object);
892 static void redisplay_window (Lisp_Object, int);
893 static Lisp_Object redisplay_window_error (Lisp_Object);
894 static Lisp_Object redisplay_window_0 (Lisp_Object);
895 static Lisp_Object redisplay_window_1 (Lisp_Object);
896 static int set_cursor_from_row (struct window *, struct glyph_row *,
897 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
898 int, int);
899 static int update_menu_bar (struct frame *, int, int);
900 static int try_window_reusing_current_matrix (struct window *);
901 static int try_window_id (struct window *);
902 static int display_line (struct it *);
903 static int display_mode_lines (struct window *);
904 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
905 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
906 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
907 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
908 static void display_menu_bar (struct window *);
909 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
910 ptrdiff_t *);
911 static int display_string (const char *, Lisp_Object, Lisp_Object,
912 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
913 static void compute_line_metrics (struct it *);
914 static void run_redisplay_end_trigger_hook (struct it *);
915 static int get_overlay_strings (struct it *, ptrdiff_t);
916 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
917 static void next_overlay_string (struct it *);
918 static void reseat (struct it *, struct text_pos, int);
919 static void reseat_1 (struct it *, struct text_pos, int);
920 static void back_to_previous_visible_line_start (struct it *);
921 static void reseat_at_next_visible_line_start (struct it *, int);
922 static int next_element_from_ellipsis (struct it *);
923 static int next_element_from_display_vector (struct it *);
924 static int next_element_from_string (struct it *);
925 static int next_element_from_c_string (struct it *);
926 static int next_element_from_buffer (struct it *);
927 static int next_element_from_composition (struct it *);
928 static int next_element_from_image (struct it *);
929 static int next_element_from_stretch (struct it *);
930 static void load_overlay_strings (struct it *, ptrdiff_t);
931 static int init_from_display_pos (struct it *, struct window *,
932 struct display_pos *);
933 static void reseat_to_string (struct it *, const char *,
934 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
935 static int get_next_display_element (struct it *);
936 static enum move_it_result
937 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
938 enum move_operation_enum);
939 static void get_visually_first_element (struct it *);
940 static void init_to_row_start (struct it *, struct window *,
941 struct glyph_row *);
942 static int init_to_row_end (struct it *, struct window *,
943 struct glyph_row *);
944 static void back_to_previous_line_start (struct it *);
945 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
946 static struct text_pos string_pos_nchars_ahead (struct text_pos,
947 Lisp_Object, ptrdiff_t);
948 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
949 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
950 static ptrdiff_t number_of_chars (const char *, bool);
951 static void compute_stop_pos (struct it *);
952 static void compute_string_pos (struct text_pos *, struct text_pos,
953 Lisp_Object);
954 static int face_before_or_after_it_pos (struct it *, int);
955 static ptrdiff_t next_overlay_change (ptrdiff_t);
956 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
957 Lisp_Object, struct text_pos *, ptrdiff_t, int);
958 static int handle_single_display_spec (struct it *, Lisp_Object,
959 Lisp_Object, Lisp_Object,
960 struct text_pos *, ptrdiff_t, int, int);
961 static int underlying_face_id (struct it *);
962 static int in_ellipses_for_invisible_text_p (struct display_pos *,
963 struct window *);
964
965 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
966 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
967
968 #ifdef HAVE_WINDOW_SYSTEM
969
970 static void x_consider_frame_title (Lisp_Object);
971 static void update_tool_bar (struct frame *, int);
972 static int redisplay_tool_bar (struct frame *);
973 static void notice_overwritten_cursor (struct window *,
974 enum glyph_row_area,
975 int, int, int, int);
976 static void append_stretch_glyph (struct it *, Lisp_Object,
977 int, int, int);
978
979
980 #endif /* HAVE_WINDOW_SYSTEM */
981
982 static void produce_special_glyphs (struct it *, enum display_element_type);
983 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
984 static int coords_in_mouse_face_p (struct window *, int, int);
985
986
987 \f
988 /***********************************************************************
989 Window display dimensions
990 ***********************************************************************/
991
992 /* Return the bottom boundary y-position for text lines in window W.
993 This is the first y position at which a line cannot start.
994 It is relative to the top of the window.
995
996 This is the height of W minus the height of a mode line, if any. */
997
998 int
999 window_text_bottom_y (struct window *w)
1000 {
1001 int height = WINDOW_TOTAL_HEIGHT (w);
1002
1003 if (WINDOW_WANTS_MODELINE_P (w))
1004 height -= CURRENT_MODE_LINE_HEIGHT (w);
1005 return height;
1006 }
1007
1008 /* Return the pixel width of display area AREA of window W.
1009 ANY_AREA means return the total width of W, not including
1010 fringes to the left and right of the window. */
1011
1012 int
1013 window_box_width (struct window *w, enum glyph_row_area area)
1014 {
1015 int cols = w->total_cols;
1016 int pixels = 0;
1017
1018 if (!w->pseudo_window_p)
1019 {
1020 cols -= WINDOW_SCROLL_BAR_COLS (w);
1021
1022 if (area == TEXT_AREA)
1023 {
1024 cols -= max (0, w->left_margin_cols);
1025 cols -= max (0, w->right_margin_cols);
1026 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1027 }
1028 else if (area == LEFT_MARGIN_AREA)
1029 {
1030 cols = max (0, w->left_margin_cols);
1031 pixels = 0;
1032 }
1033 else if (area == RIGHT_MARGIN_AREA)
1034 {
1035 cols = max (0, w->right_margin_cols);
1036 pixels = 0;
1037 }
1038 }
1039
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1041 }
1042
1043
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1046
1047 int
1048 window_box_height (struct window *w)
1049 {
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_TOTAL_HEIGHT (w);
1052
1053 eassert (height >= 0);
1054
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1060
1061 if (WINDOW_WANTS_MODELINE_P (w))
1062 {
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1071 }
1072
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1074 {
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1083 }
1084
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1088 }
1089
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. ANY_AREA means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1093
1094 int
1095 window_box_left_offset (struct window *w, enum glyph_row_area area)
1096 {
1097 int x;
1098
1099 if (w->pseudo_window_p)
1100 return 0;
1101
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1103
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1112 ? 0
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. ANY_AREA means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 int
1127 window_box_right_offset (struct window *w, enum glyph_row_area area)
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. ANY_AREA means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 int
1137 window_box_left (struct window *w, enum glyph_row_area area)
1138 {
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1141
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1144
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1147
1148 return x;
1149 }
1150
1151
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. ANY_AREA means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1155
1156 int
1157 window_box_right (struct window *w, enum glyph_row_area area)
1158 {
1159 return window_box_left (w, area) + window_box_width (w, area);
1160 }
1161
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. ANY_AREA means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168
1169 void
1170 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1171 int *box_y, int *box_width, int *box_height)
1172 {
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1180 {
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1184 }
1185 }
1186
1187 #ifdef HAVE_WINDOW_SYSTEM
1188
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1195
1196 static void
1197 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1199 {
1200 window_box (w, ANY_AREA, top_left_x, top_left_y,
1201 bottom_right_x, bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1204 }
1205
1206 #endif /* HAVE_WINDOW_SYSTEM */
1207
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1211
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1214
1215 int
1216 line_bottom_y (struct it *it)
1217 {
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1220
1221 if (line_height == 0)
1222 {
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1226 {
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1231 }
1232 else
1233 {
1234 struct glyph_row *row = it->glyph_row;
1235
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1244 }
1245 }
1246
1247 return line_top_y + line_height;
1248 }
1249
1250 DEFUN ("line-pixel-height", Fline_pixel_height,
1251 Sline_pixel_height, 0, 0, 0,
1252 doc: /* Return height in pixels of text line in the selected window.
1253
1254 Value is the height in pixels of the line at point. */)
1255 (void)
1256 {
1257 struct it it;
1258 struct text_pos pt;
1259 struct window *w = XWINDOW (selected_window);
1260
1261 SET_TEXT_POS (pt, PT, PT_BYTE);
1262 start_display (&it, w, pt);
1263 it.vpos = it.current_y = 0;
1264 last_height = 0;
1265 return make_number (line_bottom_y (&it));
1266 }
1267
1268 /* Return the default pixel height of text lines in window W. The
1269 value is the canonical height of the W frame's default font, plus
1270 any extra space required by the line-spacing variable or frame
1271 parameter.
1272
1273 Implementation note: this ignores any line-spacing text properties
1274 put on the newline characters. This is because those properties
1275 only affect the _screen_ line ending in the newline (i.e., in a
1276 continued line, only the last screen line will be affected), which
1277 means only a small number of lines in a buffer can ever use this
1278 feature. Since this function is used to compute the default pixel
1279 equivalent of text lines in a window, we can safely ignore those
1280 few lines. For the same reasons, we ignore the line-height
1281 properties. */
1282 int
1283 default_line_pixel_height (struct window *w)
1284 {
1285 struct frame *f = WINDOW_XFRAME (w);
1286 int height = FRAME_LINE_HEIGHT (f);
1287
1288 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1289 {
1290 struct buffer *b = XBUFFER (w->contents);
1291 Lisp_Object val = BVAR (b, extra_line_spacing);
1292
1293 if (NILP (val))
1294 val = BVAR (&buffer_defaults, extra_line_spacing);
1295 if (!NILP (val))
1296 {
1297 if (RANGED_INTEGERP (0, val, INT_MAX))
1298 height += XFASTINT (val);
1299 else if (FLOATP (val))
1300 {
1301 int addon = XFLOAT_DATA (val) * height + 0.5;
1302
1303 if (addon >= 0)
1304 height += addon;
1305 }
1306 }
1307 else
1308 height += f->extra_line_spacing;
1309 }
1310
1311 return height;
1312 }
1313
1314 /* Subroutine of pos_visible_p below. Extracts a display string, if
1315 any, from the display spec given as its argument. */
1316 static Lisp_Object
1317 string_from_display_spec (Lisp_Object spec)
1318 {
1319 if (CONSP (spec))
1320 {
1321 while (CONSP (spec))
1322 {
1323 if (STRINGP (XCAR (spec)))
1324 return XCAR (spec);
1325 spec = XCDR (spec);
1326 }
1327 }
1328 else if (VECTORP (spec))
1329 {
1330 ptrdiff_t i;
1331
1332 for (i = 0; i < ASIZE (spec); i++)
1333 {
1334 if (STRINGP (AREF (spec, i)))
1335 return AREF (spec, i);
1336 }
1337 return Qnil;
1338 }
1339
1340 return spec;
1341 }
1342
1343
1344 /* Limit insanely large values of W->hscroll on frame F to the largest
1345 value that will still prevent first_visible_x and last_visible_x of
1346 'struct it' from overflowing an int. */
1347 static int
1348 window_hscroll_limited (struct window *w, struct frame *f)
1349 {
1350 ptrdiff_t window_hscroll = w->hscroll;
1351 int window_text_width = window_box_width (w, TEXT_AREA);
1352 int colwidth = FRAME_COLUMN_WIDTH (f);
1353
1354 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1355 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1356
1357 return window_hscroll;
1358 }
1359
1360 /* Return 1 if position CHARPOS is visible in window W.
1361 CHARPOS < 0 means return info about WINDOW_END position.
1362 If visible, set *X and *Y to pixel coordinates of top left corner.
1363 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1364 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1365
1366 int
1367 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1368 int *rtop, int *rbot, int *rowh, int *vpos)
1369 {
1370 struct it it;
1371 void *itdata = bidi_shelve_cache ();
1372 struct text_pos top;
1373 int visible_p = 0;
1374 struct buffer *old_buffer = NULL;
1375
1376 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1377 return visible_p;
1378
1379 if (XBUFFER (w->contents) != current_buffer)
1380 {
1381 old_buffer = current_buffer;
1382 set_buffer_internal_1 (XBUFFER (w->contents));
1383 }
1384
1385 SET_TEXT_POS_FROM_MARKER (top, w->start);
1386 /* Scrolling a minibuffer window via scroll bar when the echo area
1387 shows long text sometimes resets the minibuffer contents behind
1388 our backs. */
1389 if (CHARPOS (top) > ZV)
1390 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1391
1392 /* Compute exact mode line heights. */
1393 if (WINDOW_WANTS_MODELINE_P (w))
1394 w->mode_line_height
1395 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1396 BVAR (current_buffer, mode_line_format));
1397
1398 if (WINDOW_WANTS_HEADER_LINE_P (w))
1399 w->header_line_height
1400 = display_mode_line (w, HEADER_LINE_FACE_ID,
1401 BVAR (current_buffer, header_line_format));
1402
1403 start_display (&it, w, top);
1404 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1405 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1406
1407 if (charpos >= 0
1408 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1409 && IT_CHARPOS (it) >= charpos)
1410 /* When scanning backwards under bidi iteration, move_it_to
1411 stops at or _before_ CHARPOS, because it stops at or to
1412 the _right_ of the character at CHARPOS. */
1413 || (it.bidi_p && it.bidi_it.scan_dir == -1
1414 && IT_CHARPOS (it) <= charpos)))
1415 {
1416 /* We have reached CHARPOS, or passed it. How the call to
1417 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1418 or covered by a display property, move_it_to stops at the end
1419 of the invisible text, to the right of CHARPOS. (ii) If
1420 CHARPOS is in a display vector, move_it_to stops on its last
1421 glyph. */
1422 int top_x = it.current_x;
1423 int top_y = it.current_y;
1424 /* Calling line_bottom_y may change it.method, it.position, etc. */
1425 enum it_method it_method = it.method;
1426 int bottom_y = (last_height = 0, line_bottom_y (&it));
1427 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1428
1429 if (top_y < window_top_y)
1430 visible_p = bottom_y > window_top_y;
1431 else if (top_y < it.last_visible_y)
1432 visible_p = 1;
1433 if (bottom_y >= it.last_visible_y
1434 && it.bidi_p && it.bidi_it.scan_dir == -1
1435 && IT_CHARPOS (it) < charpos)
1436 {
1437 /* When the last line of the window is scanned backwards
1438 under bidi iteration, we could be duped into thinking
1439 that we have passed CHARPOS, when in fact move_it_to
1440 simply stopped short of CHARPOS because it reached
1441 last_visible_y. To see if that's what happened, we call
1442 move_it_to again with a slightly larger vertical limit,
1443 and see if it actually moved vertically; if it did, we
1444 didn't really reach CHARPOS, which is beyond window end. */
1445 struct it save_it = it;
1446 /* Why 10? because we don't know how many canonical lines
1447 will the height of the next line(s) be. So we guess. */
1448 int ten_more_lines = 10 * default_line_pixel_height (w);
1449
1450 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1451 MOVE_TO_POS | MOVE_TO_Y);
1452 if (it.current_y > top_y)
1453 visible_p = 0;
1454
1455 it = save_it;
1456 }
1457 if (visible_p)
1458 {
1459 if (it_method == GET_FROM_DISPLAY_VECTOR)
1460 {
1461 /* We stopped on the last glyph of a display vector.
1462 Try and recompute. Hack alert! */
1463 if (charpos < 2 || top.charpos >= charpos)
1464 top_x = it.glyph_row->x;
1465 else
1466 {
1467 struct it it2, it2_prev;
1468 /* The idea is to get to the previous buffer
1469 position, consume the character there, and use
1470 the pixel coordinates we get after that. But if
1471 the previous buffer position is also displayed
1472 from a display vector, we need to consume all of
1473 the glyphs from that display vector. */
1474 start_display (&it2, w, top);
1475 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1476 /* If we didn't get to CHARPOS - 1, there's some
1477 replacing display property at that position, and
1478 we stopped after it. That is exactly the place
1479 whose coordinates we want. */
1480 if (IT_CHARPOS (it2) != charpos - 1)
1481 it2_prev = it2;
1482 else
1483 {
1484 /* Iterate until we get out of the display
1485 vector that displays the character at
1486 CHARPOS - 1. */
1487 do {
1488 get_next_display_element (&it2);
1489 PRODUCE_GLYPHS (&it2);
1490 it2_prev = it2;
1491 set_iterator_to_next (&it2, 1);
1492 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1493 && IT_CHARPOS (it2) < charpos);
1494 }
1495 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1496 || it2_prev.current_x > it2_prev.last_visible_x)
1497 top_x = it.glyph_row->x;
1498 else
1499 {
1500 top_x = it2_prev.current_x;
1501 top_y = it2_prev.current_y;
1502 }
1503 }
1504 }
1505 else if (IT_CHARPOS (it) != charpos)
1506 {
1507 Lisp_Object cpos = make_number (charpos);
1508 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1509 Lisp_Object string = string_from_display_spec (spec);
1510 struct text_pos tpos;
1511 int replacing_spec_p;
1512 bool newline_in_string
1513 = (STRINGP (string)
1514 && memchr (SDATA (string), '\n', SBYTES (string)));
1515
1516 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1517 replacing_spec_p
1518 = (!NILP (spec)
1519 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1520 charpos, FRAME_WINDOW_P (it.f)));
1521 /* The tricky code below is needed because there's a
1522 discrepancy between move_it_to and how we set cursor
1523 when PT is at the beginning of a portion of text
1524 covered by a display property or an overlay with a
1525 display property, or the display line ends in a
1526 newline from a display string. move_it_to will stop
1527 _after_ such display strings, whereas
1528 set_cursor_from_row conspires with cursor_row_p to
1529 place the cursor on the first glyph produced from the
1530 display string. */
1531
1532 /* We have overshoot PT because it is covered by a
1533 display property that replaces the text it covers.
1534 If the string includes embedded newlines, we are also
1535 in the wrong display line. Backtrack to the correct
1536 line, where the display property begins. */
1537 if (replacing_spec_p)
1538 {
1539 Lisp_Object startpos, endpos;
1540 EMACS_INT start, end;
1541 struct it it3;
1542 int it3_moved;
1543
1544 /* Find the first and the last buffer positions
1545 covered by the display string. */
1546 endpos =
1547 Fnext_single_char_property_change (cpos, Qdisplay,
1548 Qnil, Qnil);
1549 startpos =
1550 Fprevious_single_char_property_change (endpos, Qdisplay,
1551 Qnil, Qnil);
1552 start = XFASTINT (startpos);
1553 end = XFASTINT (endpos);
1554 /* Move to the last buffer position before the
1555 display property. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1558 /* Move forward one more line if the position before
1559 the display string is a newline or if it is the
1560 rightmost character on a line that is
1561 continued or word-wrapped. */
1562 if (it3.method == GET_FROM_BUFFER
1563 && (it3.c == '\n'
1564 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1565 move_it_by_lines (&it3, 1);
1566 else if (move_it_in_display_line_to (&it3, -1,
1567 it3.current_x
1568 + it3.pixel_width,
1569 MOVE_TO_X)
1570 == MOVE_LINE_CONTINUED)
1571 {
1572 move_it_by_lines (&it3, 1);
1573 /* When we are under word-wrap, the #$@%!
1574 move_it_by_lines moves 2 lines, so we need to
1575 fix that up. */
1576 if (it3.line_wrap == WORD_WRAP)
1577 move_it_by_lines (&it3, -1);
1578 }
1579
1580 /* Record the vertical coordinate of the display
1581 line where we wound up. */
1582 top_y = it3.current_y;
1583 if (it3.bidi_p)
1584 {
1585 /* When characters are reordered for display,
1586 the character displayed to the left of the
1587 display string could be _after_ the display
1588 property in the logical order. Use the
1589 smallest vertical position of these two. */
1590 start_display (&it3, w, top);
1591 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1592 if (it3.current_y < top_y)
1593 top_y = it3.current_y;
1594 }
1595 /* Move from the top of the window to the beginning
1596 of the display line where the display string
1597 begins. */
1598 start_display (&it3, w, top);
1599 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1600 /* If it3_moved stays zero after the 'while' loop
1601 below, that means we already were at a newline
1602 before the loop (e.g., the display string begins
1603 with a newline), so we don't need to (and cannot)
1604 inspect the glyphs of it3.glyph_row, because
1605 PRODUCE_GLYPHS will not produce anything for a
1606 newline, and thus it3.glyph_row stays at its
1607 stale content it got at top of the window. */
1608 it3_moved = 0;
1609 /* Finally, advance the iterator until we hit the
1610 first display element whose character position is
1611 CHARPOS, or until the first newline from the
1612 display string, which signals the end of the
1613 display line. */
1614 while (get_next_display_element (&it3))
1615 {
1616 PRODUCE_GLYPHS (&it3);
1617 if (IT_CHARPOS (it3) == charpos
1618 || ITERATOR_AT_END_OF_LINE_P (&it3))
1619 break;
1620 it3_moved = 1;
1621 set_iterator_to_next (&it3, 0);
1622 }
1623 top_x = it3.current_x - it3.pixel_width;
1624 /* Normally, we would exit the above loop because we
1625 found the display element whose character
1626 position is CHARPOS. For the contingency that we
1627 didn't, and stopped at the first newline from the
1628 display string, move back over the glyphs
1629 produced from the string, until we find the
1630 rightmost glyph not from the string. */
1631 if (it3_moved
1632 && newline_in_string
1633 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1634 {
1635 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1636 + it3.glyph_row->used[TEXT_AREA];
1637
1638 while (EQ ((g - 1)->object, string))
1639 {
1640 --g;
1641 top_x -= g->pixel_width;
1642 }
1643 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1644 + it3.glyph_row->used[TEXT_AREA]);
1645 }
1646 }
1647 }
1648
1649 *x = top_x;
1650 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1651 *rtop = max (0, window_top_y - top_y);
1652 *rbot = max (0, bottom_y - it.last_visible_y);
1653 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1654 - max (top_y, window_top_y)));
1655 *vpos = it.vpos;
1656 }
1657 }
1658 else
1659 {
1660 /* We were asked to provide info about WINDOW_END. */
1661 struct it it2;
1662 void *it2data = NULL;
1663
1664 SAVE_IT (it2, it, it2data);
1665 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1666 move_it_by_lines (&it, 1);
1667 if (charpos < IT_CHARPOS (it)
1668 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1669 {
1670 visible_p = 1;
1671 RESTORE_IT (&it2, &it2, it2data);
1672 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1673 *x = it2.current_x;
1674 *y = it2.current_y + it2.max_ascent - it2.ascent;
1675 *rtop = max (0, -it2.current_y);
1676 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1677 - it.last_visible_y));
1678 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1679 it.last_visible_y)
1680 - max (it2.current_y,
1681 WINDOW_HEADER_LINE_HEIGHT (w))));
1682 *vpos = it2.vpos;
1683 }
1684 else
1685 bidi_unshelve_cache (it2data, 1);
1686 }
1687 bidi_unshelve_cache (itdata, 0);
1688
1689 if (old_buffer)
1690 set_buffer_internal_1 (old_buffer);
1691
1692 if (visible_p && w->hscroll > 0)
1693 *x -=
1694 window_hscroll_limited (w, WINDOW_XFRAME (w))
1695 * WINDOW_FRAME_COLUMN_WIDTH (w);
1696
1697 #if 0
1698 /* Debugging code. */
1699 if (visible_p)
1700 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1701 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1702 else
1703 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1704 #endif
1705
1706 return visible_p;
1707 }
1708
1709
1710 /* Return the next character from STR. Return in *LEN the length of
1711 the character. This is like STRING_CHAR_AND_LENGTH but never
1712 returns an invalid character. If we find one, we return a `?', but
1713 with the length of the invalid character. */
1714
1715 static int
1716 string_char_and_length (const unsigned char *str, int *len)
1717 {
1718 int c;
1719
1720 c = STRING_CHAR_AND_LENGTH (str, *len);
1721 if (!CHAR_VALID_P (c))
1722 /* We may not change the length here because other places in Emacs
1723 don't use this function, i.e. they silently accept invalid
1724 characters. */
1725 c = '?';
1726
1727 return c;
1728 }
1729
1730
1731
1732 /* Given a position POS containing a valid character and byte position
1733 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1734
1735 static struct text_pos
1736 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1737 {
1738 eassert (STRINGP (string) && nchars >= 0);
1739
1740 if (STRING_MULTIBYTE (string))
1741 {
1742 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1743 int len;
1744
1745 while (nchars--)
1746 {
1747 string_char_and_length (p, &len);
1748 p += len;
1749 CHARPOS (pos) += 1;
1750 BYTEPOS (pos) += len;
1751 }
1752 }
1753 else
1754 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1755
1756 return pos;
1757 }
1758
1759
1760 /* Value is the text position, i.e. character and byte position,
1761 for character position CHARPOS in STRING. */
1762
1763 static struct text_pos
1764 string_pos (ptrdiff_t charpos, Lisp_Object string)
1765 {
1766 struct text_pos pos;
1767 eassert (STRINGP (string));
1768 eassert (charpos >= 0);
1769 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1770 return pos;
1771 }
1772
1773
1774 /* Value is a text position, i.e. character and byte position, for
1775 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1776 means recognize multibyte characters. */
1777
1778 static struct text_pos
1779 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1780 {
1781 struct text_pos pos;
1782
1783 eassert (s != NULL);
1784 eassert (charpos >= 0);
1785
1786 if (multibyte_p)
1787 {
1788 int len;
1789
1790 SET_TEXT_POS (pos, 0, 0);
1791 while (charpos--)
1792 {
1793 string_char_and_length ((const unsigned char *) s, &len);
1794 s += len;
1795 CHARPOS (pos) += 1;
1796 BYTEPOS (pos) += len;
1797 }
1798 }
1799 else
1800 SET_TEXT_POS (pos, charpos, charpos);
1801
1802 return pos;
1803 }
1804
1805
1806 /* Value is the number of characters in C string S. MULTIBYTE_P
1807 non-zero means recognize multibyte characters. */
1808
1809 static ptrdiff_t
1810 number_of_chars (const char *s, bool multibyte_p)
1811 {
1812 ptrdiff_t nchars;
1813
1814 if (multibyte_p)
1815 {
1816 ptrdiff_t rest = strlen (s);
1817 int len;
1818 const unsigned char *p = (const unsigned char *) s;
1819
1820 for (nchars = 0; rest > 0; ++nchars)
1821 {
1822 string_char_and_length (p, &len);
1823 rest -= len, p += len;
1824 }
1825 }
1826 else
1827 nchars = strlen (s);
1828
1829 return nchars;
1830 }
1831
1832
1833 /* Compute byte position NEWPOS->bytepos corresponding to
1834 NEWPOS->charpos. POS is a known position in string STRING.
1835 NEWPOS->charpos must be >= POS.charpos. */
1836
1837 static void
1838 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1839 {
1840 eassert (STRINGP (string));
1841 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1842
1843 if (STRING_MULTIBYTE (string))
1844 *newpos = string_pos_nchars_ahead (pos, string,
1845 CHARPOS (*newpos) - CHARPOS (pos));
1846 else
1847 BYTEPOS (*newpos) = CHARPOS (*newpos);
1848 }
1849
1850 /* EXPORT:
1851 Return an estimation of the pixel height of mode or header lines on
1852 frame F. FACE_ID specifies what line's height to estimate. */
1853
1854 int
1855 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1856 {
1857 #ifdef HAVE_WINDOW_SYSTEM
1858 if (FRAME_WINDOW_P (f))
1859 {
1860 int height = FONT_HEIGHT (FRAME_FONT (f));
1861
1862 /* This function is called so early when Emacs starts that the face
1863 cache and mode line face are not yet initialized. */
1864 if (FRAME_FACE_CACHE (f))
1865 {
1866 struct face *face = FACE_FROM_ID (f, face_id);
1867 if (face)
1868 {
1869 if (face->font)
1870 height = FONT_HEIGHT (face->font);
1871 if (face->box_line_width > 0)
1872 height += 2 * face->box_line_width;
1873 }
1874 }
1875
1876 return height;
1877 }
1878 #endif
1879
1880 return 1;
1881 }
1882
1883 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1884 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1885 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1886 not force the value into range. */
1887
1888 void
1889 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1890 int *x, int *y, NativeRectangle *bounds, int noclip)
1891 {
1892
1893 #ifdef HAVE_WINDOW_SYSTEM
1894 if (FRAME_WINDOW_P (f))
1895 {
1896 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1897 even for negative values. */
1898 if (pix_x < 0)
1899 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1900 if (pix_y < 0)
1901 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1902
1903 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1904 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1905
1906 if (bounds)
1907 STORE_NATIVE_RECT (*bounds,
1908 FRAME_COL_TO_PIXEL_X (f, pix_x),
1909 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1910 FRAME_COLUMN_WIDTH (f) - 1,
1911 FRAME_LINE_HEIGHT (f) - 1);
1912
1913 if (!noclip)
1914 {
1915 if (pix_x < 0)
1916 pix_x = 0;
1917 else if (pix_x > FRAME_TOTAL_COLS (f))
1918 pix_x = FRAME_TOTAL_COLS (f);
1919
1920 if (pix_y < 0)
1921 pix_y = 0;
1922 else if (pix_y > FRAME_LINES (f))
1923 pix_y = FRAME_LINES (f);
1924 }
1925 }
1926 #endif
1927
1928 *x = pix_x;
1929 *y = pix_y;
1930 }
1931
1932
1933 /* Find the glyph under window-relative coordinates X/Y in window W.
1934 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1935 strings. Return in *HPOS and *VPOS the row and column number of
1936 the glyph found. Return in *AREA the glyph area containing X.
1937 Value is a pointer to the glyph found or null if X/Y is not on
1938 text, or we can't tell because W's current matrix is not up to
1939 date. */
1940
1941 static struct glyph *
1942 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1943 int *dx, int *dy, int *area)
1944 {
1945 struct glyph *glyph, *end;
1946 struct glyph_row *row = NULL;
1947 int x0, i;
1948
1949 /* Find row containing Y. Give up if some row is not enabled. */
1950 for (i = 0; i < w->current_matrix->nrows; ++i)
1951 {
1952 row = MATRIX_ROW (w->current_matrix, i);
1953 if (!row->enabled_p)
1954 return NULL;
1955 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1956 break;
1957 }
1958
1959 *vpos = i;
1960 *hpos = 0;
1961
1962 /* Give up if Y is not in the window. */
1963 if (i == w->current_matrix->nrows)
1964 return NULL;
1965
1966 /* Get the glyph area containing X. */
1967 if (w->pseudo_window_p)
1968 {
1969 *area = TEXT_AREA;
1970 x0 = 0;
1971 }
1972 else
1973 {
1974 if (x < window_box_left_offset (w, TEXT_AREA))
1975 {
1976 *area = LEFT_MARGIN_AREA;
1977 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1978 }
1979 else if (x < window_box_right_offset (w, TEXT_AREA))
1980 {
1981 *area = TEXT_AREA;
1982 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1983 }
1984 else
1985 {
1986 *area = RIGHT_MARGIN_AREA;
1987 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1988 }
1989 }
1990
1991 /* Find glyph containing X. */
1992 glyph = row->glyphs[*area];
1993 end = glyph + row->used[*area];
1994 x -= x0;
1995 while (glyph < end && x >= glyph->pixel_width)
1996 {
1997 x -= glyph->pixel_width;
1998 ++glyph;
1999 }
2000
2001 if (glyph == end)
2002 return NULL;
2003
2004 if (dx)
2005 {
2006 *dx = x;
2007 *dy = y - (row->y + row->ascent - glyph->ascent);
2008 }
2009
2010 *hpos = glyph - row->glyphs[*area];
2011 return glyph;
2012 }
2013
2014 /* Convert frame-relative x/y to coordinates relative to window W.
2015 Takes pseudo-windows into account. */
2016
2017 static void
2018 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2019 {
2020 if (w->pseudo_window_p)
2021 {
2022 /* A pseudo-window is always full-width, and starts at the
2023 left edge of the frame, plus a frame border. */
2024 struct frame *f = XFRAME (w->frame);
2025 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2026 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2027 }
2028 else
2029 {
2030 *x -= WINDOW_LEFT_EDGE_X (w);
2031 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2032 }
2033 }
2034
2035 #ifdef HAVE_WINDOW_SYSTEM
2036
2037 /* EXPORT:
2038 Return in RECTS[] at most N clipping rectangles for glyph string S.
2039 Return the number of stored rectangles. */
2040
2041 int
2042 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2043 {
2044 XRectangle r;
2045
2046 if (n <= 0)
2047 return 0;
2048
2049 if (s->row->full_width_p)
2050 {
2051 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2052 r.x = WINDOW_LEFT_EDGE_X (s->w);
2053 r.width = WINDOW_TOTAL_WIDTH (s->w);
2054
2055 /* Unless displaying a mode or menu bar line, which are always
2056 fully visible, clip to the visible part of the row. */
2057 if (s->w->pseudo_window_p)
2058 r.height = s->row->visible_height;
2059 else
2060 r.height = s->height;
2061 }
2062 else
2063 {
2064 /* This is a text line that may be partially visible. */
2065 r.x = window_box_left (s->w, s->area);
2066 r.width = window_box_width (s->w, s->area);
2067 r.height = s->row->visible_height;
2068 }
2069
2070 if (s->clip_head)
2071 if (r.x < s->clip_head->x)
2072 {
2073 if (r.width >= s->clip_head->x - r.x)
2074 r.width -= s->clip_head->x - r.x;
2075 else
2076 r.width = 0;
2077 r.x = s->clip_head->x;
2078 }
2079 if (s->clip_tail)
2080 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2081 {
2082 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2083 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2084 else
2085 r.width = 0;
2086 }
2087
2088 /* If S draws overlapping rows, it's sufficient to use the top and
2089 bottom of the window for clipping because this glyph string
2090 intentionally draws over other lines. */
2091 if (s->for_overlaps)
2092 {
2093 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2094 r.height = window_text_bottom_y (s->w) - r.y;
2095
2096 /* Alas, the above simple strategy does not work for the
2097 environments with anti-aliased text: if the same text is
2098 drawn onto the same place multiple times, it gets thicker.
2099 If the overlap we are processing is for the erased cursor, we
2100 take the intersection with the rectangle of the cursor. */
2101 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2102 {
2103 XRectangle rc, r_save = r;
2104
2105 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2106 rc.y = s->w->phys_cursor.y;
2107 rc.width = s->w->phys_cursor_width;
2108 rc.height = s->w->phys_cursor_height;
2109
2110 x_intersect_rectangles (&r_save, &rc, &r);
2111 }
2112 }
2113 else
2114 {
2115 /* Don't use S->y for clipping because it doesn't take partially
2116 visible lines into account. For example, it can be negative for
2117 partially visible lines at the top of a window. */
2118 if (!s->row->full_width_p
2119 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2120 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2121 else
2122 r.y = max (0, s->row->y);
2123 }
2124
2125 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2126
2127 /* If drawing the cursor, don't let glyph draw outside its
2128 advertised boundaries. Cleartype does this under some circumstances. */
2129 if (s->hl == DRAW_CURSOR)
2130 {
2131 struct glyph *glyph = s->first_glyph;
2132 int height, max_y;
2133
2134 if (s->x > r.x)
2135 {
2136 r.width -= s->x - r.x;
2137 r.x = s->x;
2138 }
2139 r.width = min (r.width, glyph->pixel_width);
2140
2141 /* If r.y is below window bottom, ensure that we still see a cursor. */
2142 height = min (glyph->ascent + glyph->descent,
2143 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2144 max_y = window_text_bottom_y (s->w) - height;
2145 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2146 if (s->ybase - glyph->ascent > max_y)
2147 {
2148 r.y = max_y;
2149 r.height = height;
2150 }
2151 else
2152 {
2153 /* Don't draw cursor glyph taller than our actual glyph. */
2154 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2155 if (height < r.height)
2156 {
2157 max_y = r.y + r.height;
2158 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2159 r.height = min (max_y - r.y, height);
2160 }
2161 }
2162 }
2163
2164 if (s->row->clip)
2165 {
2166 XRectangle r_save = r;
2167
2168 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2169 r.width = 0;
2170 }
2171
2172 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2173 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2174 {
2175 #ifdef CONVERT_FROM_XRECT
2176 CONVERT_FROM_XRECT (r, *rects);
2177 #else
2178 *rects = r;
2179 #endif
2180 return 1;
2181 }
2182 else
2183 {
2184 /* If we are processing overlapping and allowed to return
2185 multiple clipping rectangles, we exclude the row of the glyph
2186 string from the clipping rectangle. This is to avoid drawing
2187 the same text on the environment with anti-aliasing. */
2188 #ifdef CONVERT_FROM_XRECT
2189 XRectangle rs[2];
2190 #else
2191 XRectangle *rs = rects;
2192 #endif
2193 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2194
2195 if (s->for_overlaps & OVERLAPS_PRED)
2196 {
2197 rs[i] = r;
2198 if (r.y + r.height > row_y)
2199 {
2200 if (r.y < row_y)
2201 rs[i].height = row_y - r.y;
2202 else
2203 rs[i].height = 0;
2204 }
2205 i++;
2206 }
2207 if (s->for_overlaps & OVERLAPS_SUCC)
2208 {
2209 rs[i] = r;
2210 if (r.y < row_y + s->row->visible_height)
2211 {
2212 if (r.y + r.height > row_y + s->row->visible_height)
2213 {
2214 rs[i].y = row_y + s->row->visible_height;
2215 rs[i].height = r.y + r.height - rs[i].y;
2216 }
2217 else
2218 rs[i].height = 0;
2219 }
2220 i++;
2221 }
2222
2223 n = i;
2224 #ifdef CONVERT_FROM_XRECT
2225 for (i = 0; i < n; i++)
2226 CONVERT_FROM_XRECT (rs[i], rects[i]);
2227 #endif
2228 return n;
2229 }
2230 }
2231
2232 /* EXPORT:
2233 Return in *NR the clipping rectangle for glyph string S. */
2234
2235 void
2236 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2237 {
2238 get_glyph_string_clip_rects (s, nr, 1);
2239 }
2240
2241
2242 /* EXPORT:
2243 Return the position and height of the phys cursor in window W.
2244 Set w->phys_cursor_width to width of phys cursor.
2245 */
2246
2247 void
2248 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2249 struct glyph *glyph, int *xp, int *yp, int *heightp)
2250 {
2251 struct frame *f = XFRAME (WINDOW_FRAME (w));
2252 int x, y, wd, h, h0, y0;
2253
2254 /* Compute the width of the rectangle to draw. If on a stretch
2255 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2256 rectangle as wide as the glyph, but use a canonical character
2257 width instead. */
2258 wd = glyph->pixel_width - 1;
2259 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2260 wd++; /* Why? */
2261 #endif
2262
2263 x = w->phys_cursor.x;
2264 if (x < 0)
2265 {
2266 wd += x;
2267 x = 0;
2268 }
2269
2270 if (glyph->type == STRETCH_GLYPH
2271 && !x_stretch_cursor_p)
2272 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2273 w->phys_cursor_width = wd;
2274
2275 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2276
2277 /* If y is below window bottom, ensure that we still see a cursor. */
2278 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2279
2280 h = max (h0, glyph->ascent + glyph->descent);
2281 h0 = min (h0, glyph->ascent + glyph->descent);
2282
2283 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2284 if (y < y0)
2285 {
2286 h = max (h - (y0 - y) + 1, h0);
2287 y = y0 - 1;
2288 }
2289 else
2290 {
2291 y0 = window_text_bottom_y (w) - h0;
2292 if (y > y0)
2293 {
2294 h += y - y0;
2295 y = y0;
2296 }
2297 }
2298
2299 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2300 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2301 *heightp = h;
2302 }
2303
2304 /*
2305 * Remember which glyph the mouse is over.
2306 */
2307
2308 void
2309 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2310 {
2311 Lisp_Object window;
2312 struct window *w;
2313 struct glyph_row *r, *gr, *end_row;
2314 enum window_part part;
2315 enum glyph_row_area area;
2316 int x, y, width, height;
2317
2318 /* Try to determine frame pixel position and size of the glyph under
2319 frame pixel coordinates X/Y on frame F. */
2320
2321 if (!f->glyphs_initialized_p
2322 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2323 NILP (window)))
2324 {
2325 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2326 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2327 goto virtual_glyph;
2328 }
2329
2330 w = XWINDOW (window);
2331 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2332 height = WINDOW_FRAME_LINE_HEIGHT (w);
2333
2334 x = window_relative_x_coord (w, part, gx);
2335 y = gy - WINDOW_TOP_EDGE_Y (w);
2336
2337 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2338 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2339
2340 if (w->pseudo_window_p)
2341 {
2342 area = TEXT_AREA;
2343 part = ON_MODE_LINE; /* Don't adjust margin. */
2344 goto text_glyph;
2345 }
2346
2347 switch (part)
2348 {
2349 case ON_LEFT_MARGIN:
2350 area = LEFT_MARGIN_AREA;
2351 goto text_glyph;
2352
2353 case ON_RIGHT_MARGIN:
2354 area = RIGHT_MARGIN_AREA;
2355 goto text_glyph;
2356
2357 case ON_HEADER_LINE:
2358 case ON_MODE_LINE:
2359 gr = (part == ON_HEADER_LINE
2360 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2361 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2362 gy = gr->y;
2363 area = TEXT_AREA;
2364 goto text_glyph_row_found;
2365
2366 case ON_TEXT:
2367 area = TEXT_AREA;
2368
2369 text_glyph:
2370 gr = 0; gy = 0;
2371 for (; r <= end_row && r->enabled_p; ++r)
2372 if (r->y + r->height > y)
2373 {
2374 gr = r; gy = r->y;
2375 break;
2376 }
2377
2378 text_glyph_row_found:
2379 if (gr && gy <= y)
2380 {
2381 struct glyph *g = gr->glyphs[area];
2382 struct glyph *end = g + gr->used[area];
2383
2384 height = gr->height;
2385 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2386 if (gx + g->pixel_width > x)
2387 break;
2388
2389 if (g < end)
2390 {
2391 if (g->type == IMAGE_GLYPH)
2392 {
2393 /* Don't remember when mouse is over image, as
2394 image may have hot-spots. */
2395 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2396 return;
2397 }
2398 width = g->pixel_width;
2399 }
2400 else
2401 {
2402 /* Use nominal char spacing at end of line. */
2403 x -= gx;
2404 gx += (x / width) * width;
2405 }
2406
2407 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2408 gx += window_box_left_offset (w, area);
2409 }
2410 else
2411 {
2412 /* Use nominal line height at end of window. */
2413 gx = (x / width) * width;
2414 y -= gy;
2415 gy += (y / height) * height;
2416 }
2417 break;
2418
2419 case ON_LEFT_FRINGE:
2420 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2421 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2422 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2423 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2424 goto row_glyph;
2425
2426 case ON_RIGHT_FRINGE:
2427 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2428 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2429 : window_box_right_offset (w, TEXT_AREA));
2430 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2431 goto row_glyph;
2432
2433 case ON_SCROLL_BAR:
2434 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2435 ? 0
2436 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2437 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2438 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2439 : 0)));
2440 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2441
2442 row_glyph:
2443 gr = 0, gy = 0;
2444 for (; r <= end_row && r->enabled_p; ++r)
2445 if (r->y + r->height > y)
2446 {
2447 gr = r; gy = r->y;
2448 break;
2449 }
2450
2451 if (gr && gy <= y)
2452 height = gr->height;
2453 else
2454 {
2455 /* Use nominal line height at end of window. */
2456 y -= gy;
2457 gy += (y / height) * height;
2458 }
2459 break;
2460
2461 default:
2462 ;
2463 virtual_glyph:
2464 /* If there is no glyph under the mouse, then we divide the screen
2465 into a grid of the smallest glyph in the frame, and use that
2466 as our "glyph". */
2467
2468 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2469 round down even for negative values. */
2470 if (gx < 0)
2471 gx -= width - 1;
2472 if (gy < 0)
2473 gy -= height - 1;
2474
2475 gx = (gx / width) * width;
2476 gy = (gy / height) * height;
2477
2478 goto store_rect;
2479 }
2480
2481 gx += WINDOW_LEFT_EDGE_X (w);
2482 gy += WINDOW_TOP_EDGE_Y (w);
2483
2484 store_rect:
2485 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2486
2487 /* Visible feedback for debugging. */
2488 #if 0
2489 #if HAVE_X_WINDOWS
2490 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2491 f->output_data.x->normal_gc,
2492 gx, gy, width, height);
2493 #endif
2494 #endif
2495 }
2496
2497
2498 #endif /* HAVE_WINDOW_SYSTEM */
2499
2500 static void
2501 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2502 {
2503 eassert (w);
2504 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2505 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2506 w->window_end_vpos
2507 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2508 }
2509
2510 /***********************************************************************
2511 Lisp form evaluation
2512 ***********************************************************************/
2513
2514 /* Error handler for safe_eval and safe_call. */
2515
2516 static Lisp_Object
2517 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2518 {
2519 add_to_log ("Error during redisplay: %S signaled %S",
2520 Flist (nargs, args), arg);
2521 return Qnil;
2522 }
2523
2524 /* Call function FUNC with the rest of NARGS - 1 arguments
2525 following. Return the result, or nil if something went
2526 wrong. Prevent redisplay during the evaluation. */
2527
2528 Lisp_Object
2529 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2530 {
2531 Lisp_Object val;
2532
2533 if (inhibit_eval_during_redisplay)
2534 val = Qnil;
2535 else
2536 {
2537 va_list ap;
2538 ptrdiff_t i;
2539 ptrdiff_t count = SPECPDL_INDEX ();
2540 struct gcpro gcpro1;
2541 Lisp_Object *args = alloca (nargs * word_size);
2542
2543 args[0] = func;
2544 va_start (ap, func);
2545 for (i = 1; i < nargs; i++)
2546 args[i] = va_arg (ap, Lisp_Object);
2547 va_end (ap);
2548
2549 GCPRO1 (args[0]);
2550 gcpro1.nvars = nargs;
2551 specbind (Qinhibit_redisplay, Qt);
2552 /* Use Qt to ensure debugger does not run,
2553 so there is no possibility of wanting to redisplay. */
2554 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2555 safe_eval_handler);
2556 UNGCPRO;
2557 val = unbind_to (count, val);
2558 }
2559
2560 return val;
2561 }
2562
2563
2564 /* Call function FN with one argument ARG.
2565 Return the result, or nil if something went wrong. */
2566
2567 Lisp_Object
2568 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2569 {
2570 return safe_call (2, fn, arg);
2571 }
2572
2573 static Lisp_Object Qeval;
2574
2575 Lisp_Object
2576 safe_eval (Lisp_Object sexpr)
2577 {
2578 return safe_call1 (Qeval, sexpr);
2579 }
2580
2581 /* Call function FN with two arguments ARG1 and ARG2.
2582 Return the result, or nil if something went wrong. */
2583
2584 Lisp_Object
2585 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2586 {
2587 return safe_call (3, fn, arg1, arg2);
2588 }
2589
2590
2591 \f
2592 /***********************************************************************
2593 Debugging
2594 ***********************************************************************/
2595
2596 #if 0
2597
2598 /* Define CHECK_IT to perform sanity checks on iterators.
2599 This is for debugging. It is too slow to do unconditionally. */
2600
2601 static void
2602 check_it (struct it *it)
2603 {
2604 if (it->method == GET_FROM_STRING)
2605 {
2606 eassert (STRINGP (it->string));
2607 eassert (IT_STRING_CHARPOS (*it) >= 0);
2608 }
2609 else
2610 {
2611 eassert (IT_STRING_CHARPOS (*it) < 0);
2612 if (it->method == GET_FROM_BUFFER)
2613 {
2614 /* Check that character and byte positions agree. */
2615 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2616 }
2617 }
2618
2619 if (it->dpvec)
2620 eassert (it->current.dpvec_index >= 0);
2621 else
2622 eassert (it->current.dpvec_index < 0);
2623 }
2624
2625 #define CHECK_IT(IT) check_it ((IT))
2626
2627 #else /* not 0 */
2628
2629 #define CHECK_IT(IT) (void) 0
2630
2631 #endif /* not 0 */
2632
2633
2634 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2635
2636 /* Check that the window end of window W is what we expect it
2637 to be---the last row in the current matrix displaying text. */
2638
2639 static void
2640 check_window_end (struct window *w)
2641 {
2642 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2643 {
2644 struct glyph_row *row;
2645 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2646 !row->enabled_p
2647 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2648 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2649 }
2650 }
2651
2652 #define CHECK_WINDOW_END(W) check_window_end ((W))
2653
2654 #else
2655
2656 #define CHECK_WINDOW_END(W) (void) 0
2657
2658 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2659
2660 /***********************************************************************
2661 Iterator initialization
2662 ***********************************************************************/
2663
2664 /* Initialize IT for displaying current_buffer in window W, starting
2665 at character position CHARPOS. CHARPOS < 0 means that no buffer
2666 position is specified which is useful when the iterator is assigned
2667 a position later. BYTEPOS is the byte position corresponding to
2668 CHARPOS.
2669
2670 If ROW is not null, calls to produce_glyphs with IT as parameter
2671 will produce glyphs in that row.
2672
2673 BASE_FACE_ID is the id of a base face to use. It must be one of
2674 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2675 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2676 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2677
2678 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2679 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2680 will be initialized to use the corresponding mode line glyph row of
2681 the desired matrix of W. */
2682
2683 void
2684 init_iterator (struct it *it, struct window *w,
2685 ptrdiff_t charpos, ptrdiff_t bytepos,
2686 struct glyph_row *row, enum face_id base_face_id)
2687 {
2688 enum face_id remapped_base_face_id = base_face_id;
2689
2690 /* Some precondition checks. */
2691 eassert (w != NULL && it != NULL);
2692 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2693 && charpos <= ZV));
2694
2695 /* If face attributes have been changed since the last redisplay,
2696 free realized faces now because they depend on face definitions
2697 that might have changed. Don't free faces while there might be
2698 desired matrices pending which reference these faces. */
2699 if (face_change_count && !inhibit_free_realized_faces)
2700 {
2701 face_change_count = 0;
2702 free_all_realized_faces (Qnil);
2703 }
2704
2705 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2706 if (! NILP (Vface_remapping_alist))
2707 remapped_base_face_id
2708 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2709
2710 /* Use one of the mode line rows of W's desired matrix if
2711 appropriate. */
2712 if (row == NULL)
2713 {
2714 if (base_face_id == MODE_LINE_FACE_ID
2715 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2716 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2717 else if (base_face_id == HEADER_LINE_FACE_ID)
2718 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2719 }
2720
2721 /* Clear IT. */
2722 memset (it, 0, sizeof *it);
2723 it->current.overlay_string_index = -1;
2724 it->current.dpvec_index = -1;
2725 it->base_face_id = remapped_base_face_id;
2726 it->string = Qnil;
2727 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2728 it->paragraph_embedding = L2R;
2729 it->bidi_it.string.lstring = Qnil;
2730 it->bidi_it.string.s = NULL;
2731 it->bidi_it.string.bufpos = 0;
2732 it->bidi_it.w = w;
2733
2734 /* The window in which we iterate over current_buffer: */
2735 XSETWINDOW (it->window, w);
2736 it->w = w;
2737 it->f = XFRAME (w->frame);
2738
2739 it->cmp_it.id = -1;
2740
2741 /* Extra space between lines (on window systems only). */
2742 if (base_face_id == DEFAULT_FACE_ID
2743 && FRAME_WINDOW_P (it->f))
2744 {
2745 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2746 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2747 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2748 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2749 * FRAME_LINE_HEIGHT (it->f));
2750 else if (it->f->extra_line_spacing > 0)
2751 it->extra_line_spacing = it->f->extra_line_spacing;
2752 it->max_extra_line_spacing = 0;
2753 }
2754
2755 /* If realized faces have been removed, e.g. because of face
2756 attribute changes of named faces, recompute them. When running
2757 in batch mode, the face cache of the initial frame is null. If
2758 we happen to get called, make a dummy face cache. */
2759 if (FRAME_FACE_CACHE (it->f) == NULL)
2760 init_frame_faces (it->f);
2761 if (FRAME_FACE_CACHE (it->f)->used == 0)
2762 recompute_basic_faces (it->f);
2763
2764 /* Current value of the `slice', `space-width', and 'height' properties. */
2765 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2766 it->space_width = Qnil;
2767 it->font_height = Qnil;
2768 it->override_ascent = -1;
2769
2770 /* Are control characters displayed as `^C'? */
2771 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2772
2773 /* -1 means everything between a CR and the following line end
2774 is invisible. >0 means lines indented more than this value are
2775 invisible. */
2776 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2777 ? (clip_to_bounds
2778 (-1, XINT (BVAR (current_buffer, selective_display)),
2779 PTRDIFF_MAX))
2780 : (!NILP (BVAR (current_buffer, selective_display))
2781 ? -1 : 0));
2782 it->selective_display_ellipsis_p
2783 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2784
2785 /* Display table to use. */
2786 it->dp = window_display_table (w);
2787
2788 /* Are multibyte characters enabled in current_buffer? */
2789 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2790
2791 /* Get the position at which the redisplay_end_trigger hook should
2792 be run, if it is to be run at all. */
2793 if (MARKERP (w->redisplay_end_trigger)
2794 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2795 it->redisplay_end_trigger_charpos
2796 = marker_position (w->redisplay_end_trigger);
2797 else if (INTEGERP (w->redisplay_end_trigger))
2798 it->redisplay_end_trigger_charpos =
2799 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2800
2801 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2802
2803 /* Are lines in the display truncated? */
2804 if (base_face_id != DEFAULT_FACE_ID
2805 || it->w->hscroll
2806 || (! WINDOW_FULL_WIDTH_P (it->w)
2807 && ((!NILP (Vtruncate_partial_width_windows)
2808 && !INTEGERP (Vtruncate_partial_width_windows))
2809 || (INTEGERP (Vtruncate_partial_width_windows)
2810 && (WINDOW_TOTAL_COLS (it->w)
2811 < XINT (Vtruncate_partial_width_windows))))))
2812 it->line_wrap = TRUNCATE;
2813 else if (NILP (BVAR (current_buffer, truncate_lines)))
2814 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2815 ? WINDOW_WRAP : WORD_WRAP;
2816 else
2817 it->line_wrap = TRUNCATE;
2818
2819 /* Get dimensions of truncation and continuation glyphs. These are
2820 displayed as fringe bitmaps under X, but we need them for such
2821 frames when the fringes are turned off. But leave the dimensions
2822 zero for tooltip frames, as these glyphs look ugly there and also
2823 sabotage calculations of tooltip dimensions in x-show-tip. */
2824 #ifdef HAVE_WINDOW_SYSTEM
2825 if (!(FRAME_WINDOW_P (it->f)
2826 && FRAMEP (tip_frame)
2827 && it->f == XFRAME (tip_frame)))
2828 #endif
2829 {
2830 if (it->line_wrap == TRUNCATE)
2831 {
2832 /* We will need the truncation glyph. */
2833 eassert (it->glyph_row == NULL);
2834 produce_special_glyphs (it, IT_TRUNCATION);
2835 it->truncation_pixel_width = it->pixel_width;
2836 }
2837 else
2838 {
2839 /* We will need the continuation glyph. */
2840 eassert (it->glyph_row == NULL);
2841 produce_special_glyphs (it, IT_CONTINUATION);
2842 it->continuation_pixel_width = it->pixel_width;
2843 }
2844 }
2845
2846 /* Reset these values to zero because the produce_special_glyphs
2847 above has changed them. */
2848 it->pixel_width = it->ascent = it->descent = 0;
2849 it->phys_ascent = it->phys_descent = 0;
2850
2851 /* Set this after getting the dimensions of truncation and
2852 continuation glyphs, so that we don't produce glyphs when calling
2853 produce_special_glyphs, above. */
2854 it->glyph_row = row;
2855 it->area = TEXT_AREA;
2856
2857 /* Forget any previous info about this row being reversed. */
2858 if (it->glyph_row)
2859 it->glyph_row->reversed_p = 0;
2860
2861 /* Get the dimensions of the display area. The display area
2862 consists of the visible window area plus a horizontally scrolled
2863 part to the left of the window. All x-values are relative to the
2864 start of this total display area. */
2865 if (base_face_id != DEFAULT_FACE_ID)
2866 {
2867 /* Mode lines, menu bar in terminal frames. */
2868 it->first_visible_x = 0;
2869 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2870 }
2871 else
2872 {
2873 it->first_visible_x
2874 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2875 it->last_visible_x = (it->first_visible_x
2876 + window_box_width (w, TEXT_AREA));
2877
2878 /* If we truncate lines, leave room for the truncation glyph(s) at
2879 the right margin. Otherwise, leave room for the continuation
2880 glyph(s). Done only if the window has no fringes. Since we
2881 don't know at this point whether there will be any R2L lines in
2882 the window, we reserve space for truncation/continuation glyphs
2883 even if only one of the fringes is absent. */
2884 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2885 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2886 {
2887 if (it->line_wrap == TRUNCATE)
2888 it->last_visible_x -= it->truncation_pixel_width;
2889 else
2890 it->last_visible_x -= it->continuation_pixel_width;
2891 }
2892
2893 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2894 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2895 }
2896
2897 /* Leave room for a border glyph. */
2898 if (!FRAME_WINDOW_P (it->f)
2899 && !WINDOW_RIGHTMOST_P (it->w))
2900 it->last_visible_x -= 1;
2901
2902 it->last_visible_y = window_text_bottom_y (w);
2903
2904 /* For mode lines and alike, arrange for the first glyph having a
2905 left box line if the face specifies a box. */
2906 if (base_face_id != DEFAULT_FACE_ID)
2907 {
2908 struct face *face;
2909
2910 it->face_id = remapped_base_face_id;
2911
2912 /* If we have a boxed mode line, make the first character appear
2913 with a left box line. */
2914 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2915 if (face->box != FACE_NO_BOX)
2916 it->start_of_box_run_p = 1;
2917 }
2918
2919 /* If a buffer position was specified, set the iterator there,
2920 getting overlays and face properties from that position. */
2921 if (charpos >= BUF_BEG (current_buffer))
2922 {
2923 it->end_charpos = ZV;
2924 eassert (charpos == BYTE_TO_CHAR (bytepos));
2925 IT_CHARPOS (*it) = charpos;
2926 IT_BYTEPOS (*it) = bytepos;
2927
2928 /* We will rely on `reseat' to set this up properly, via
2929 handle_face_prop. */
2930 it->face_id = it->base_face_id;
2931
2932 it->start = it->current;
2933 /* Do we need to reorder bidirectional text? Not if this is a
2934 unibyte buffer: by definition, none of the single-byte
2935 characters are strong R2L, so no reordering is needed. And
2936 bidi.c doesn't support unibyte buffers anyway. Also, don't
2937 reorder while we are loading loadup.el, since the tables of
2938 character properties needed for reordering are not yet
2939 available. */
2940 it->bidi_p =
2941 NILP (Vpurify_flag)
2942 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2943 && it->multibyte_p;
2944
2945 /* If we are to reorder bidirectional text, init the bidi
2946 iterator. */
2947 if (it->bidi_p)
2948 {
2949 /* Note the paragraph direction that this buffer wants to
2950 use. */
2951 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2952 Qleft_to_right))
2953 it->paragraph_embedding = L2R;
2954 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2955 Qright_to_left))
2956 it->paragraph_embedding = R2L;
2957 else
2958 it->paragraph_embedding = NEUTRAL_DIR;
2959 bidi_unshelve_cache (NULL, 0);
2960 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2961 &it->bidi_it);
2962 }
2963
2964 /* Compute faces etc. */
2965 reseat (it, it->current.pos, 1);
2966 }
2967
2968 CHECK_IT (it);
2969 }
2970
2971
2972 /* Initialize IT for the display of window W with window start POS. */
2973
2974 void
2975 start_display (struct it *it, struct window *w, struct text_pos pos)
2976 {
2977 struct glyph_row *row;
2978 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2979
2980 row = w->desired_matrix->rows + first_vpos;
2981 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2982 it->first_vpos = first_vpos;
2983
2984 /* Don't reseat to previous visible line start if current start
2985 position is in a string or image. */
2986 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2987 {
2988 int start_at_line_beg_p;
2989 int first_y = it->current_y;
2990
2991 /* If window start is not at a line start, skip forward to POS to
2992 get the correct continuation lines width. */
2993 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2994 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2995 if (!start_at_line_beg_p)
2996 {
2997 int new_x;
2998
2999 reseat_at_previous_visible_line_start (it);
3000 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3001
3002 new_x = it->current_x + it->pixel_width;
3003
3004 /* If lines are continued, this line may end in the middle
3005 of a multi-glyph character (e.g. a control character
3006 displayed as \003, or in the middle of an overlay
3007 string). In this case move_it_to above will not have
3008 taken us to the start of the continuation line but to the
3009 end of the continued line. */
3010 if (it->current_x > 0
3011 && it->line_wrap != TRUNCATE /* Lines are continued. */
3012 && (/* And glyph doesn't fit on the line. */
3013 new_x > it->last_visible_x
3014 /* Or it fits exactly and we're on a window
3015 system frame. */
3016 || (new_x == it->last_visible_x
3017 && FRAME_WINDOW_P (it->f)
3018 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3019 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3020 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3021 {
3022 if ((it->current.dpvec_index >= 0
3023 || it->current.overlay_string_index >= 0)
3024 /* If we are on a newline from a display vector or
3025 overlay string, then we are already at the end of
3026 a screen line; no need to go to the next line in
3027 that case, as this line is not really continued.
3028 (If we do go to the next line, C-e will not DTRT.) */
3029 && it->c != '\n')
3030 {
3031 set_iterator_to_next (it, 1);
3032 move_it_in_display_line_to (it, -1, -1, 0);
3033 }
3034
3035 it->continuation_lines_width += it->current_x;
3036 }
3037 /* If the character at POS is displayed via a display
3038 vector, move_it_to above stops at the final glyph of
3039 IT->dpvec. To make the caller redisplay that character
3040 again (a.k.a. start at POS), we need to reset the
3041 dpvec_index to the beginning of IT->dpvec. */
3042 else if (it->current.dpvec_index >= 0)
3043 it->current.dpvec_index = 0;
3044
3045 /* We're starting a new display line, not affected by the
3046 height of the continued line, so clear the appropriate
3047 fields in the iterator structure. */
3048 it->max_ascent = it->max_descent = 0;
3049 it->max_phys_ascent = it->max_phys_descent = 0;
3050
3051 it->current_y = first_y;
3052 it->vpos = 0;
3053 it->current_x = it->hpos = 0;
3054 }
3055 }
3056 }
3057
3058
3059 /* Return 1 if POS is a position in ellipses displayed for invisible
3060 text. W is the window we display, for text property lookup. */
3061
3062 static int
3063 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3064 {
3065 Lisp_Object prop, window;
3066 int ellipses_p = 0;
3067 ptrdiff_t charpos = CHARPOS (pos->pos);
3068
3069 /* If POS specifies a position in a display vector, this might
3070 be for an ellipsis displayed for invisible text. We won't
3071 get the iterator set up for delivering that ellipsis unless
3072 we make sure that it gets aware of the invisible text. */
3073 if (pos->dpvec_index >= 0
3074 && pos->overlay_string_index < 0
3075 && CHARPOS (pos->string_pos) < 0
3076 && charpos > BEGV
3077 && (XSETWINDOW (window, w),
3078 prop = Fget_char_property (make_number (charpos),
3079 Qinvisible, window),
3080 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3081 {
3082 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3083 window);
3084 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3085 }
3086
3087 return ellipses_p;
3088 }
3089
3090
3091 /* Initialize IT for stepping through current_buffer in window W,
3092 starting at position POS that includes overlay string and display
3093 vector/ control character translation position information. Value
3094 is zero if there are overlay strings with newlines at POS. */
3095
3096 static int
3097 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3098 {
3099 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3100 int i, overlay_strings_with_newlines = 0;
3101
3102 /* If POS specifies a position in a display vector, this might
3103 be for an ellipsis displayed for invisible text. We won't
3104 get the iterator set up for delivering that ellipsis unless
3105 we make sure that it gets aware of the invisible text. */
3106 if (in_ellipses_for_invisible_text_p (pos, w))
3107 {
3108 --charpos;
3109 bytepos = 0;
3110 }
3111
3112 /* Keep in mind: the call to reseat in init_iterator skips invisible
3113 text, so we might end up at a position different from POS. This
3114 is only a problem when POS is a row start after a newline and an
3115 overlay starts there with an after-string, and the overlay has an
3116 invisible property. Since we don't skip invisible text in
3117 display_line and elsewhere immediately after consuming the
3118 newline before the row start, such a POS will not be in a string,
3119 but the call to init_iterator below will move us to the
3120 after-string. */
3121 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3122
3123 /* This only scans the current chunk -- it should scan all chunks.
3124 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3125 to 16 in 22.1 to make this a lesser problem. */
3126 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3127 {
3128 const char *s = SSDATA (it->overlay_strings[i]);
3129 const char *e = s + SBYTES (it->overlay_strings[i]);
3130
3131 while (s < e && *s != '\n')
3132 ++s;
3133
3134 if (s < e)
3135 {
3136 overlay_strings_with_newlines = 1;
3137 break;
3138 }
3139 }
3140
3141 /* If position is within an overlay string, set up IT to the right
3142 overlay string. */
3143 if (pos->overlay_string_index >= 0)
3144 {
3145 int relative_index;
3146
3147 /* If the first overlay string happens to have a `display'
3148 property for an image, the iterator will be set up for that
3149 image, and we have to undo that setup first before we can
3150 correct the overlay string index. */
3151 if (it->method == GET_FROM_IMAGE)
3152 pop_it (it);
3153
3154 /* We already have the first chunk of overlay strings in
3155 IT->overlay_strings. Load more until the one for
3156 pos->overlay_string_index is in IT->overlay_strings. */
3157 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3158 {
3159 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3160 it->current.overlay_string_index = 0;
3161 while (n--)
3162 {
3163 load_overlay_strings (it, 0);
3164 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3165 }
3166 }
3167
3168 it->current.overlay_string_index = pos->overlay_string_index;
3169 relative_index = (it->current.overlay_string_index
3170 % OVERLAY_STRING_CHUNK_SIZE);
3171 it->string = it->overlay_strings[relative_index];
3172 eassert (STRINGP (it->string));
3173 it->current.string_pos = pos->string_pos;
3174 it->method = GET_FROM_STRING;
3175 it->end_charpos = SCHARS (it->string);
3176 /* Set up the bidi iterator for this overlay string. */
3177 if (it->bidi_p)
3178 {
3179 it->bidi_it.string.lstring = it->string;
3180 it->bidi_it.string.s = NULL;
3181 it->bidi_it.string.schars = SCHARS (it->string);
3182 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3183 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3184 it->bidi_it.string.unibyte = !it->multibyte_p;
3185 it->bidi_it.w = it->w;
3186 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3187 FRAME_WINDOW_P (it->f), &it->bidi_it);
3188
3189 /* Synchronize the state of the bidi iterator with
3190 pos->string_pos. For any string position other than
3191 zero, this will be done automagically when we resume
3192 iteration over the string and get_visually_first_element
3193 is called. But if string_pos is zero, and the string is
3194 to be reordered for display, we need to resync manually,
3195 since it could be that the iteration state recorded in
3196 pos ended at string_pos of 0 moving backwards in string. */
3197 if (CHARPOS (pos->string_pos) == 0)
3198 {
3199 get_visually_first_element (it);
3200 if (IT_STRING_CHARPOS (*it) != 0)
3201 do {
3202 /* Paranoia. */
3203 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3204 bidi_move_to_visually_next (&it->bidi_it);
3205 } while (it->bidi_it.charpos != 0);
3206 }
3207 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3208 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3209 }
3210 }
3211
3212 if (CHARPOS (pos->string_pos) >= 0)
3213 {
3214 /* Recorded position is not in an overlay string, but in another
3215 string. This can only be a string from a `display' property.
3216 IT should already be filled with that string. */
3217 it->current.string_pos = pos->string_pos;
3218 eassert (STRINGP (it->string));
3219 if (it->bidi_p)
3220 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3221 FRAME_WINDOW_P (it->f), &it->bidi_it);
3222 }
3223
3224 /* Restore position in display vector translations, control
3225 character translations or ellipses. */
3226 if (pos->dpvec_index >= 0)
3227 {
3228 if (it->dpvec == NULL)
3229 get_next_display_element (it);
3230 eassert (it->dpvec && it->current.dpvec_index == 0);
3231 it->current.dpvec_index = pos->dpvec_index;
3232 }
3233
3234 CHECK_IT (it);
3235 return !overlay_strings_with_newlines;
3236 }
3237
3238
3239 /* Initialize IT for stepping through current_buffer in window W
3240 starting at ROW->start. */
3241
3242 static void
3243 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3244 {
3245 init_from_display_pos (it, w, &row->start);
3246 it->start = row->start;
3247 it->continuation_lines_width = row->continuation_lines_width;
3248 CHECK_IT (it);
3249 }
3250
3251
3252 /* Initialize IT for stepping through current_buffer in window W
3253 starting in the line following ROW, i.e. starting at ROW->end.
3254 Value is zero if there are overlay strings with newlines at ROW's
3255 end position. */
3256
3257 static int
3258 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3259 {
3260 int success = 0;
3261
3262 if (init_from_display_pos (it, w, &row->end))
3263 {
3264 if (row->continued_p)
3265 it->continuation_lines_width
3266 = row->continuation_lines_width + row->pixel_width;
3267 CHECK_IT (it);
3268 success = 1;
3269 }
3270
3271 return success;
3272 }
3273
3274
3275
3276 \f
3277 /***********************************************************************
3278 Text properties
3279 ***********************************************************************/
3280
3281 /* Called when IT reaches IT->stop_charpos. Handle text property and
3282 overlay changes. Set IT->stop_charpos to the next position where
3283 to stop. */
3284
3285 static void
3286 handle_stop (struct it *it)
3287 {
3288 enum prop_handled handled;
3289 int handle_overlay_change_p;
3290 struct props *p;
3291
3292 it->dpvec = NULL;
3293 it->current.dpvec_index = -1;
3294 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3295 it->ignore_overlay_strings_at_pos_p = 0;
3296 it->ellipsis_p = 0;
3297
3298 /* Use face of preceding text for ellipsis (if invisible) */
3299 if (it->selective_display_ellipsis_p)
3300 it->saved_face_id = it->face_id;
3301
3302 do
3303 {
3304 handled = HANDLED_NORMALLY;
3305
3306 /* Call text property handlers. */
3307 for (p = it_props; p->handler; ++p)
3308 {
3309 handled = p->handler (it);
3310
3311 if (handled == HANDLED_RECOMPUTE_PROPS)
3312 break;
3313 else if (handled == HANDLED_RETURN)
3314 {
3315 /* We still want to show before and after strings from
3316 overlays even if the actual buffer text is replaced. */
3317 if (!handle_overlay_change_p
3318 || it->sp > 1
3319 /* Don't call get_overlay_strings_1 if we already
3320 have overlay strings loaded, because doing so
3321 will load them again and push the iterator state
3322 onto the stack one more time, which is not
3323 expected by the rest of the code that processes
3324 overlay strings. */
3325 || (it->current.overlay_string_index < 0
3326 ? !get_overlay_strings_1 (it, 0, 0)
3327 : 0))
3328 {
3329 if (it->ellipsis_p)
3330 setup_for_ellipsis (it, 0);
3331 /* When handling a display spec, we might load an
3332 empty string. In that case, discard it here. We
3333 used to discard it in handle_single_display_spec,
3334 but that causes get_overlay_strings_1, above, to
3335 ignore overlay strings that we must check. */
3336 if (STRINGP (it->string) && !SCHARS (it->string))
3337 pop_it (it);
3338 return;
3339 }
3340 else if (STRINGP (it->string) && !SCHARS (it->string))
3341 pop_it (it);
3342 else
3343 {
3344 it->ignore_overlay_strings_at_pos_p = 1;
3345 it->string_from_display_prop_p = 0;
3346 it->from_disp_prop_p = 0;
3347 handle_overlay_change_p = 0;
3348 }
3349 handled = HANDLED_RECOMPUTE_PROPS;
3350 break;
3351 }
3352 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3353 handle_overlay_change_p = 0;
3354 }
3355
3356 if (handled != HANDLED_RECOMPUTE_PROPS)
3357 {
3358 /* Don't check for overlay strings below when set to deliver
3359 characters from a display vector. */
3360 if (it->method == GET_FROM_DISPLAY_VECTOR)
3361 handle_overlay_change_p = 0;
3362
3363 /* Handle overlay changes.
3364 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3365 if it finds overlays. */
3366 if (handle_overlay_change_p)
3367 handled = handle_overlay_change (it);
3368 }
3369
3370 if (it->ellipsis_p)
3371 {
3372 setup_for_ellipsis (it, 0);
3373 break;
3374 }
3375 }
3376 while (handled == HANDLED_RECOMPUTE_PROPS);
3377
3378 /* Determine where to stop next. */
3379 if (handled == HANDLED_NORMALLY)
3380 compute_stop_pos (it);
3381 }
3382
3383
3384 /* Compute IT->stop_charpos from text property and overlay change
3385 information for IT's current position. */
3386
3387 static void
3388 compute_stop_pos (struct it *it)
3389 {
3390 register INTERVAL iv, next_iv;
3391 Lisp_Object object, limit, position;
3392 ptrdiff_t charpos, bytepos;
3393
3394 if (STRINGP (it->string))
3395 {
3396 /* Strings are usually short, so don't limit the search for
3397 properties. */
3398 it->stop_charpos = it->end_charpos;
3399 object = it->string;
3400 limit = Qnil;
3401 charpos = IT_STRING_CHARPOS (*it);
3402 bytepos = IT_STRING_BYTEPOS (*it);
3403 }
3404 else
3405 {
3406 ptrdiff_t pos;
3407
3408 /* If end_charpos is out of range for some reason, such as a
3409 misbehaving display function, rationalize it (Bug#5984). */
3410 if (it->end_charpos > ZV)
3411 it->end_charpos = ZV;
3412 it->stop_charpos = it->end_charpos;
3413
3414 /* If next overlay change is in front of the current stop pos
3415 (which is IT->end_charpos), stop there. Note: value of
3416 next_overlay_change is point-max if no overlay change
3417 follows. */
3418 charpos = IT_CHARPOS (*it);
3419 bytepos = IT_BYTEPOS (*it);
3420 pos = next_overlay_change (charpos);
3421 if (pos < it->stop_charpos)
3422 it->stop_charpos = pos;
3423
3424 /* Set up variables for computing the stop position from text
3425 property changes. */
3426 XSETBUFFER (object, current_buffer);
3427 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3428 }
3429
3430 /* Get the interval containing IT's position. Value is a null
3431 interval if there isn't such an interval. */
3432 position = make_number (charpos);
3433 iv = validate_interval_range (object, &position, &position, 0);
3434 if (iv)
3435 {
3436 Lisp_Object values_here[LAST_PROP_IDX];
3437 struct props *p;
3438
3439 /* Get properties here. */
3440 for (p = it_props; p->handler; ++p)
3441 values_here[p->idx] = textget (iv->plist, *p->name);
3442
3443 /* Look for an interval following iv that has different
3444 properties. */
3445 for (next_iv = next_interval (iv);
3446 (next_iv
3447 && (NILP (limit)
3448 || XFASTINT (limit) > next_iv->position));
3449 next_iv = next_interval (next_iv))
3450 {
3451 for (p = it_props; p->handler; ++p)
3452 {
3453 Lisp_Object new_value;
3454
3455 new_value = textget (next_iv->plist, *p->name);
3456 if (!EQ (values_here[p->idx], new_value))
3457 break;
3458 }
3459
3460 if (p->handler)
3461 break;
3462 }
3463
3464 if (next_iv)
3465 {
3466 if (INTEGERP (limit)
3467 && next_iv->position >= XFASTINT (limit))
3468 /* No text property change up to limit. */
3469 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3470 else
3471 /* Text properties change in next_iv. */
3472 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3473 }
3474 }
3475
3476 if (it->cmp_it.id < 0)
3477 {
3478 ptrdiff_t stoppos = it->end_charpos;
3479
3480 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3481 stoppos = -1;
3482 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3483 stoppos, it->string);
3484 }
3485
3486 eassert (STRINGP (it->string)
3487 || (it->stop_charpos >= BEGV
3488 && it->stop_charpos >= IT_CHARPOS (*it)));
3489 }
3490
3491
3492 /* Return the position of the next overlay change after POS in
3493 current_buffer. Value is point-max if no overlay change
3494 follows. This is like `next-overlay-change' but doesn't use
3495 xmalloc. */
3496
3497 static ptrdiff_t
3498 next_overlay_change (ptrdiff_t pos)
3499 {
3500 ptrdiff_t i, noverlays;
3501 ptrdiff_t endpos;
3502 Lisp_Object *overlays;
3503
3504 /* Get all overlays at the given position. */
3505 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3506
3507 /* If any of these overlays ends before endpos,
3508 use its ending point instead. */
3509 for (i = 0; i < noverlays; ++i)
3510 {
3511 Lisp_Object oend;
3512 ptrdiff_t oendpos;
3513
3514 oend = OVERLAY_END (overlays[i]);
3515 oendpos = OVERLAY_POSITION (oend);
3516 endpos = min (endpos, oendpos);
3517 }
3518
3519 return endpos;
3520 }
3521
3522 /* How many characters forward to search for a display property or
3523 display string. Searching too far forward makes the bidi display
3524 sluggish, especially in small windows. */
3525 #define MAX_DISP_SCAN 250
3526
3527 /* Return the character position of a display string at or after
3528 position specified by POSITION. If no display string exists at or
3529 after POSITION, return ZV. A display string is either an overlay
3530 with `display' property whose value is a string, or a `display'
3531 text property whose value is a string. STRING is data about the
3532 string to iterate; if STRING->lstring is nil, we are iterating a
3533 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3534 on a GUI frame. DISP_PROP is set to zero if we searched
3535 MAX_DISP_SCAN characters forward without finding any display
3536 strings, non-zero otherwise. It is set to 2 if the display string
3537 uses any kind of `(space ...)' spec that will produce a stretch of
3538 white space in the text area. */
3539 ptrdiff_t
3540 compute_display_string_pos (struct text_pos *position,
3541 struct bidi_string_data *string,
3542 struct window *w,
3543 int frame_window_p, int *disp_prop)
3544 {
3545 /* OBJECT = nil means current buffer. */
3546 Lisp_Object object, object1;
3547 Lisp_Object pos, spec, limpos;
3548 int string_p = (string && (STRINGP (string->lstring) || string->s));
3549 ptrdiff_t eob = string_p ? string->schars : ZV;
3550 ptrdiff_t begb = string_p ? 0 : BEGV;
3551 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3552 ptrdiff_t lim =
3553 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3554 struct text_pos tpos;
3555 int rv = 0;
3556
3557 if (string && STRINGP (string->lstring))
3558 object1 = object = string->lstring;
3559 else if (w && !string_p)
3560 {
3561 XSETWINDOW (object, w);
3562 object1 = Qnil;
3563 }
3564 else
3565 object1 = object = Qnil;
3566
3567 *disp_prop = 1;
3568
3569 if (charpos >= eob
3570 /* We don't support display properties whose values are strings
3571 that have display string properties. */
3572 || string->from_disp_str
3573 /* C strings cannot have display properties. */
3574 || (string->s && !STRINGP (object)))
3575 {
3576 *disp_prop = 0;
3577 return eob;
3578 }
3579
3580 /* If the character at CHARPOS is where the display string begins,
3581 return CHARPOS. */
3582 pos = make_number (charpos);
3583 if (STRINGP (object))
3584 bufpos = string->bufpos;
3585 else
3586 bufpos = charpos;
3587 tpos = *position;
3588 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3589 && (charpos <= begb
3590 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3591 object),
3592 spec))
3593 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3594 frame_window_p)))
3595 {
3596 if (rv == 2)
3597 *disp_prop = 2;
3598 return charpos;
3599 }
3600
3601 /* Look forward for the first character with a `display' property
3602 that will replace the underlying text when displayed. */
3603 limpos = make_number (lim);
3604 do {
3605 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3606 CHARPOS (tpos) = XFASTINT (pos);
3607 if (CHARPOS (tpos) >= lim)
3608 {
3609 *disp_prop = 0;
3610 break;
3611 }
3612 if (STRINGP (object))
3613 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3614 else
3615 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3616 spec = Fget_char_property (pos, Qdisplay, object);
3617 if (!STRINGP (object))
3618 bufpos = CHARPOS (tpos);
3619 } while (NILP (spec)
3620 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3621 bufpos, frame_window_p)));
3622 if (rv == 2)
3623 *disp_prop = 2;
3624
3625 return CHARPOS (tpos);
3626 }
3627
3628 /* Return the character position of the end of the display string that
3629 started at CHARPOS. If there's no display string at CHARPOS,
3630 return -1. A display string is either an overlay with `display'
3631 property whose value is a string or a `display' text property whose
3632 value is a string. */
3633 ptrdiff_t
3634 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3635 {
3636 /* OBJECT = nil means current buffer. */
3637 Lisp_Object object =
3638 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3639 Lisp_Object pos = make_number (charpos);
3640 ptrdiff_t eob =
3641 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3642
3643 if (charpos >= eob || (string->s && !STRINGP (object)))
3644 return eob;
3645
3646 /* It could happen that the display property or overlay was removed
3647 since we found it in compute_display_string_pos above. One way
3648 this can happen is if JIT font-lock was called (through
3649 handle_fontified_prop), and jit-lock-functions remove text
3650 properties or overlays from the portion of buffer that includes
3651 CHARPOS. Muse mode is known to do that, for example. In this
3652 case, we return -1 to the caller, to signal that no display
3653 string is actually present at CHARPOS. See bidi_fetch_char for
3654 how this is handled.
3655
3656 An alternative would be to never look for display properties past
3657 it->stop_charpos. But neither compute_display_string_pos nor
3658 bidi_fetch_char that calls it know or care where the next
3659 stop_charpos is. */
3660 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3661 return -1;
3662
3663 /* Look forward for the first character where the `display' property
3664 changes. */
3665 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3666
3667 return XFASTINT (pos);
3668 }
3669
3670
3671 \f
3672 /***********************************************************************
3673 Fontification
3674 ***********************************************************************/
3675
3676 /* Handle changes in the `fontified' property of the current buffer by
3677 calling hook functions from Qfontification_functions to fontify
3678 regions of text. */
3679
3680 static enum prop_handled
3681 handle_fontified_prop (struct it *it)
3682 {
3683 Lisp_Object prop, pos;
3684 enum prop_handled handled = HANDLED_NORMALLY;
3685
3686 if (!NILP (Vmemory_full))
3687 return handled;
3688
3689 /* Get the value of the `fontified' property at IT's current buffer
3690 position. (The `fontified' property doesn't have a special
3691 meaning in strings.) If the value is nil, call functions from
3692 Qfontification_functions. */
3693 if (!STRINGP (it->string)
3694 && it->s == NULL
3695 && !NILP (Vfontification_functions)
3696 && !NILP (Vrun_hooks)
3697 && (pos = make_number (IT_CHARPOS (*it)),
3698 prop = Fget_char_property (pos, Qfontified, Qnil),
3699 /* Ignore the special cased nil value always present at EOB since
3700 no amount of fontifying will be able to change it. */
3701 NILP (prop) && IT_CHARPOS (*it) < Z))
3702 {
3703 ptrdiff_t count = SPECPDL_INDEX ();
3704 Lisp_Object val;
3705 struct buffer *obuf = current_buffer;
3706 ptrdiff_t begv = BEGV, zv = ZV;
3707 bool old_clip_changed = current_buffer->clip_changed;
3708
3709 val = Vfontification_functions;
3710 specbind (Qfontification_functions, Qnil);
3711
3712 eassert (it->end_charpos == ZV);
3713
3714 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3715 safe_call1 (val, pos);
3716 else
3717 {
3718 Lisp_Object fns, fn;
3719 struct gcpro gcpro1, gcpro2;
3720
3721 fns = Qnil;
3722 GCPRO2 (val, fns);
3723
3724 for (; CONSP (val); val = XCDR (val))
3725 {
3726 fn = XCAR (val);
3727
3728 if (EQ (fn, Qt))
3729 {
3730 /* A value of t indicates this hook has a local
3731 binding; it means to run the global binding too.
3732 In a global value, t should not occur. If it
3733 does, we must ignore it to avoid an endless
3734 loop. */
3735 for (fns = Fdefault_value (Qfontification_functions);
3736 CONSP (fns);
3737 fns = XCDR (fns))
3738 {
3739 fn = XCAR (fns);
3740 if (!EQ (fn, Qt))
3741 safe_call1 (fn, pos);
3742 }
3743 }
3744 else
3745 safe_call1 (fn, pos);
3746 }
3747
3748 UNGCPRO;
3749 }
3750
3751 unbind_to (count, Qnil);
3752
3753 /* Fontification functions routinely call `save-restriction'.
3754 Normally, this tags clip_changed, which can confuse redisplay
3755 (see discussion in Bug#6671). Since we don't perform any
3756 special handling of fontification changes in the case where
3757 `save-restriction' isn't called, there's no point doing so in
3758 this case either. So, if the buffer's restrictions are
3759 actually left unchanged, reset clip_changed. */
3760 if (obuf == current_buffer)
3761 {
3762 if (begv == BEGV && zv == ZV)
3763 current_buffer->clip_changed = old_clip_changed;
3764 }
3765 /* There isn't much we can reasonably do to protect against
3766 misbehaving fontification, but here's a fig leaf. */
3767 else if (BUFFER_LIVE_P (obuf))
3768 set_buffer_internal_1 (obuf);
3769
3770 /* The fontification code may have added/removed text.
3771 It could do even a lot worse, but let's at least protect against
3772 the most obvious case where only the text past `pos' gets changed',
3773 as is/was done in grep.el where some escapes sequences are turned
3774 into face properties (bug#7876). */
3775 it->end_charpos = ZV;
3776
3777 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3778 something. This avoids an endless loop if they failed to
3779 fontify the text for which reason ever. */
3780 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3781 handled = HANDLED_RECOMPUTE_PROPS;
3782 }
3783
3784 return handled;
3785 }
3786
3787
3788 \f
3789 /***********************************************************************
3790 Faces
3791 ***********************************************************************/
3792
3793 /* Set up iterator IT from face properties at its current position.
3794 Called from handle_stop. */
3795
3796 static enum prop_handled
3797 handle_face_prop (struct it *it)
3798 {
3799 int new_face_id;
3800 ptrdiff_t next_stop;
3801
3802 if (!STRINGP (it->string))
3803 {
3804 new_face_id
3805 = face_at_buffer_position (it->w,
3806 IT_CHARPOS (*it),
3807 &next_stop,
3808 (IT_CHARPOS (*it)
3809 + TEXT_PROP_DISTANCE_LIMIT),
3810 0, it->base_face_id);
3811
3812 /* Is this a start of a run of characters with box face?
3813 Caveat: this can be called for a freshly initialized
3814 iterator; face_id is -1 in this case. We know that the new
3815 face will not change until limit, i.e. if the new face has a
3816 box, all characters up to limit will have one. But, as
3817 usual, we don't know whether limit is really the end. */
3818 if (new_face_id != it->face_id)
3819 {
3820 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3821 /* If it->face_id is -1, old_face below will be NULL, see
3822 the definition of FACE_FROM_ID. This will happen if this
3823 is the initial call that gets the face. */
3824 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3825
3826 /* If the value of face_id of the iterator is -1, we have to
3827 look in front of IT's position and see whether there is a
3828 face there that's different from new_face_id. */
3829 if (!old_face && IT_CHARPOS (*it) > BEG)
3830 {
3831 int prev_face_id = face_before_it_pos (it);
3832
3833 old_face = FACE_FROM_ID (it->f, prev_face_id);
3834 }
3835
3836 /* If the new face has a box, but the old face does not,
3837 this is the start of a run of characters with box face,
3838 i.e. this character has a shadow on the left side. */
3839 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3840 && (old_face == NULL || !old_face->box));
3841 it->face_box_p = new_face->box != FACE_NO_BOX;
3842 }
3843 }
3844 else
3845 {
3846 int base_face_id;
3847 ptrdiff_t bufpos;
3848 int i;
3849 Lisp_Object from_overlay
3850 = (it->current.overlay_string_index >= 0
3851 ? it->string_overlays[it->current.overlay_string_index
3852 % OVERLAY_STRING_CHUNK_SIZE]
3853 : Qnil);
3854
3855 /* See if we got to this string directly or indirectly from
3856 an overlay property. That includes the before-string or
3857 after-string of an overlay, strings in display properties
3858 provided by an overlay, their text properties, etc.
3859
3860 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3861 if (! NILP (from_overlay))
3862 for (i = it->sp - 1; i >= 0; i--)
3863 {
3864 if (it->stack[i].current.overlay_string_index >= 0)
3865 from_overlay
3866 = it->string_overlays[it->stack[i].current.overlay_string_index
3867 % OVERLAY_STRING_CHUNK_SIZE];
3868 else if (! NILP (it->stack[i].from_overlay))
3869 from_overlay = it->stack[i].from_overlay;
3870
3871 if (!NILP (from_overlay))
3872 break;
3873 }
3874
3875 if (! NILP (from_overlay))
3876 {
3877 bufpos = IT_CHARPOS (*it);
3878 /* For a string from an overlay, the base face depends
3879 only on text properties and ignores overlays. */
3880 base_face_id
3881 = face_for_overlay_string (it->w,
3882 IT_CHARPOS (*it),
3883 &next_stop,
3884 (IT_CHARPOS (*it)
3885 + TEXT_PROP_DISTANCE_LIMIT),
3886 0,
3887 from_overlay);
3888 }
3889 else
3890 {
3891 bufpos = 0;
3892
3893 /* For strings from a `display' property, use the face at
3894 IT's current buffer position as the base face to merge
3895 with, so that overlay strings appear in the same face as
3896 surrounding text, unless they specify their own faces.
3897 For strings from wrap-prefix and line-prefix properties,
3898 use the default face, possibly remapped via
3899 Vface_remapping_alist. */
3900 base_face_id = it->string_from_prefix_prop_p
3901 ? (!NILP (Vface_remapping_alist)
3902 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3903 : DEFAULT_FACE_ID)
3904 : underlying_face_id (it);
3905 }
3906
3907 new_face_id = face_at_string_position (it->w,
3908 it->string,
3909 IT_STRING_CHARPOS (*it),
3910 bufpos,
3911 &next_stop,
3912 base_face_id, 0);
3913
3914 /* Is this a start of a run of characters with box? Caveat:
3915 this can be called for a freshly allocated iterator; face_id
3916 is -1 is this case. We know that the new face will not
3917 change until the next check pos, i.e. if the new face has a
3918 box, all characters up to that position will have a
3919 box. But, as usual, we don't know whether that position
3920 is really the end. */
3921 if (new_face_id != it->face_id)
3922 {
3923 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3924 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3925
3926 /* If new face has a box but old face hasn't, this is the
3927 start of a run of characters with box, i.e. it has a
3928 shadow on the left side. */
3929 it->start_of_box_run_p
3930 = new_face->box && (old_face == NULL || !old_face->box);
3931 it->face_box_p = new_face->box != FACE_NO_BOX;
3932 }
3933 }
3934
3935 it->face_id = new_face_id;
3936 return HANDLED_NORMALLY;
3937 }
3938
3939
3940 /* Return the ID of the face ``underlying'' IT's current position,
3941 which is in a string. If the iterator is associated with a
3942 buffer, return the face at IT's current buffer position.
3943 Otherwise, use the iterator's base_face_id. */
3944
3945 static int
3946 underlying_face_id (struct it *it)
3947 {
3948 int face_id = it->base_face_id, i;
3949
3950 eassert (STRINGP (it->string));
3951
3952 for (i = it->sp - 1; i >= 0; --i)
3953 if (NILP (it->stack[i].string))
3954 face_id = it->stack[i].face_id;
3955
3956 return face_id;
3957 }
3958
3959
3960 /* Compute the face one character before or after the current position
3961 of IT, in the visual order. BEFORE_P non-zero means get the face
3962 in front (to the left in L2R paragraphs, to the right in R2L
3963 paragraphs) of IT's screen position. Value is the ID of the face. */
3964
3965 static int
3966 face_before_or_after_it_pos (struct it *it, int before_p)
3967 {
3968 int face_id, limit;
3969 ptrdiff_t next_check_charpos;
3970 struct it it_copy;
3971 void *it_copy_data = NULL;
3972
3973 eassert (it->s == NULL);
3974
3975 if (STRINGP (it->string))
3976 {
3977 ptrdiff_t bufpos, charpos;
3978 int base_face_id;
3979
3980 /* No face change past the end of the string (for the case
3981 we are padding with spaces). No face change before the
3982 string start. */
3983 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3984 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3985 return it->face_id;
3986
3987 if (!it->bidi_p)
3988 {
3989 /* Set charpos to the position before or after IT's current
3990 position, in the logical order, which in the non-bidi
3991 case is the same as the visual order. */
3992 if (before_p)
3993 charpos = IT_STRING_CHARPOS (*it) - 1;
3994 else if (it->what == IT_COMPOSITION)
3995 /* For composition, we must check the character after the
3996 composition. */
3997 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3998 else
3999 charpos = IT_STRING_CHARPOS (*it) + 1;
4000 }
4001 else
4002 {
4003 if (before_p)
4004 {
4005 /* With bidi iteration, the character before the current
4006 in the visual order cannot be found by simple
4007 iteration, because "reverse" reordering is not
4008 supported. Instead, we need to use the move_it_*
4009 family of functions. */
4010 /* Ignore face changes before the first visible
4011 character on this display line. */
4012 if (it->current_x <= it->first_visible_x)
4013 return it->face_id;
4014 SAVE_IT (it_copy, *it, it_copy_data);
4015 /* Implementation note: Since move_it_in_display_line
4016 works in the iterator geometry, and thinks the first
4017 character is always the leftmost, even in R2L lines,
4018 we don't need to distinguish between the R2L and L2R
4019 cases here. */
4020 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4021 it_copy.current_x - 1, MOVE_TO_X);
4022 charpos = IT_STRING_CHARPOS (it_copy);
4023 RESTORE_IT (it, it, it_copy_data);
4024 }
4025 else
4026 {
4027 /* Set charpos to the string position of the character
4028 that comes after IT's current position in the visual
4029 order. */
4030 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4031
4032 it_copy = *it;
4033 while (n--)
4034 bidi_move_to_visually_next (&it_copy.bidi_it);
4035
4036 charpos = it_copy.bidi_it.charpos;
4037 }
4038 }
4039 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4040
4041 if (it->current.overlay_string_index >= 0)
4042 bufpos = IT_CHARPOS (*it);
4043 else
4044 bufpos = 0;
4045
4046 base_face_id = underlying_face_id (it);
4047
4048 /* Get the face for ASCII, or unibyte. */
4049 face_id = face_at_string_position (it->w,
4050 it->string,
4051 charpos,
4052 bufpos,
4053 &next_check_charpos,
4054 base_face_id, 0);
4055
4056 /* Correct the face for charsets different from ASCII. Do it
4057 for the multibyte case only. The face returned above is
4058 suitable for unibyte text if IT->string is unibyte. */
4059 if (STRING_MULTIBYTE (it->string))
4060 {
4061 struct text_pos pos1 = string_pos (charpos, it->string);
4062 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4063 int c, len;
4064 struct face *face = FACE_FROM_ID (it->f, face_id);
4065
4066 c = string_char_and_length (p, &len);
4067 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4068 }
4069 }
4070 else
4071 {
4072 struct text_pos pos;
4073
4074 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4075 || (IT_CHARPOS (*it) <= BEGV && before_p))
4076 return it->face_id;
4077
4078 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4079 pos = it->current.pos;
4080
4081 if (!it->bidi_p)
4082 {
4083 if (before_p)
4084 DEC_TEXT_POS (pos, it->multibyte_p);
4085 else
4086 {
4087 if (it->what == IT_COMPOSITION)
4088 {
4089 /* For composition, we must check the position after
4090 the composition. */
4091 pos.charpos += it->cmp_it.nchars;
4092 pos.bytepos += it->len;
4093 }
4094 else
4095 INC_TEXT_POS (pos, it->multibyte_p);
4096 }
4097 }
4098 else
4099 {
4100 if (before_p)
4101 {
4102 /* With bidi iteration, the character before the current
4103 in the visual order cannot be found by simple
4104 iteration, because "reverse" reordering is not
4105 supported. Instead, we need to use the move_it_*
4106 family of functions. */
4107 /* Ignore face changes before the first visible
4108 character on this display line. */
4109 if (it->current_x <= it->first_visible_x)
4110 return it->face_id;
4111 SAVE_IT (it_copy, *it, it_copy_data);
4112 /* Implementation note: Since move_it_in_display_line
4113 works in the iterator geometry, and thinks the first
4114 character is always the leftmost, even in R2L lines,
4115 we don't need to distinguish between the R2L and L2R
4116 cases here. */
4117 move_it_in_display_line (&it_copy, ZV,
4118 it_copy.current_x - 1, MOVE_TO_X);
4119 pos = it_copy.current.pos;
4120 RESTORE_IT (it, it, it_copy_data);
4121 }
4122 else
4123 {
4124 /* Set charpos to the buffer position of the character
4125 that comes after IT's current position in the visual
4126 order. */
4127 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4128
4129 it_copy = *it;
4130 while (n--)
4131 bidi_move_to_visually_next (&it_copy.bidi_it);
4132
4133 SET_TEXT_POS (pos,
4134 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4135 }
4136 }
4137 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4138
4139 /* Determine face for CHARSET_ASCII, or unibyte. */
4140 face_id = face_at_buffer_position (it->w,
4141 CHARPOS (pos),
4142 &next_check_charpos,
4143 limit, 0, -1);
4144
4145 /* Correct the face for charsets different from ASCII. Do it
4146 for the multibyte case only. The face returned above is
4147 suitable for unibyte text if current_buffer is unibyte. */
4148 if (it->multibyte_p)
4149 {
4150 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4151 struct face *face = FACE_FROM_ID (it->f, face_id);
4152 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4153 }
4154 }
4155
4156 return face_id;
4157 }
4158
4159
4160 \f
4161 /***********************************************************************
4162 Invisible text
4163 ***********************************************************************/
4164
4165 /* Set up iterator IT from invisible properties at its current
4166 position. Called from handle_stop. */
4167
4168 static enum prop_handled
4169 handle_invisible_prop (struct it *it)
4170 {
4171 enum prop_handled handled = HANDLED_NORMALLY;
4172 int invis_p;
4173 Lisp_Object prop;
4174
4175 if (STRINGP (it->string))
4176 {
4177 Lisp_Object end_charpos, limit, charpos;
4178
4179 /* Get the value of the invisible text property at the
4180 current position. Value will be nil if there is no such
4181 property. */
4182 charpos = make_number (IT_STRING_CHARPOS (*it));
4183 prop = Fget_text_property (charpos, Qinvisible, it->string);
4184 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4185
4186 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4187 {
4188 /* Record whether we have to display an ellipsis for the
4189 invisible text. */
4190 int display_ellipsis_p = (invis_p == 2);
4191 ptrdiff_t len, endpos;
4192
4193 handled = HANDLED_RECOMPUTE_PROPS;
4194
4195 /* Get the position at which the next visible text can be
4196 found in IT->string, if any. */
4197 endpos = len = SCHARS (it->string);
4198 XSETINT (limit, len);
4199 do
4200 {
4201 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4202 it->string, limit);
4203 if (INTEGERP (end_charpos))
4204 {
4205 endpos = XFASTINT (end_charpos);
4206 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4207 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4208 if (invis_p == 2)
4209 display_ellipsis_p = 1;
4210 }
4211 }
4212 while (invis_p && endpos < len);
4213
4214 if (display_ellipsis_p)
4215 it->ellipsis_p = 1;
4216
4217 if (endpos < len)
4218 {
4219 /* Text at END_CHARPOS is visible. Move IT there. */
4220 struct text_pos old;
4221 ptrdiff_t oldpos;
4222
4223 old = it->current.string_pos;
4224 oldpos = CHARPOS (old);
4225 if (it->bidi_p)
4226 {
4227 if (it->bidi_it.first_elt
4228 && it->bidi_it.charpos < SCHARS (it->string))
4229 bidi_paragraph_init (it->paragraph_embedding,
4230 &it->bidi_it, 1);
4231 /* Bidi-iterate out of the invisible text. */
4232 do
4233 {
4234 bidi_move_to_visually_next (&it->bidi_it);
4235 }
4236 while (oldpos <= it->bidi_it.charpos
4237 && it->bidi_it.charpos < endpos);
4238
4239 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4240 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4241 if (IT_CHARPOS (*it) >= endpos)
4242 it->prev_stop = endpos;
4243 }
4244 else
4245 {
4246 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4247 compute_string_pos (&it->current.string_pos, old, it->string);
4248 }
4249 }
4250 else
4251 {
4252 /* The rest of the string is invisible. If this is an
4253 overlay string, proceed with the next overlay string
4254 or whatever comes and return a character from there. */
4255 if (it->current.overlay_string_index >= 0
4256 && !display_ellipsis_p)
4257 {
4258 next_overlay_string (it);
4259 /* Don't check for overlay strings when we just
4260 finished processing them. */
4261 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4262 }
4263 else
4264 {
4265 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4266 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4267 }
4268 }
4269 }
4270 }
4271 else
4272 {
4273 ptrdiff_t newpos, next_stop, start_charpos, tem;
4274 Lisp_Object pos, overlay;
4275
4276 /* First of all, is there invisible text at this position? */
4277 tem = start_charpos = IT_CHARPOS (*it);
4278 pos = make_number (tem);
4279 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4280 &overlay);
4281 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4282
4283 /* If we are on invisible text, skip over it. */
4284 if (invis_p && start_charpos < it->end_charpos)
4285 {
4286 /* Record whether we have to display an ellipsis for the
4287 invisible text. */
4288 int display_ellipsis_p = invis_p == 2;
4289
4290 handled = HANDLED_RECOMPUTE_PROPS;
4291
4292 /* Loop skipping over invisible text. The loop is left at
4293 ZV or with IT on the first char being visible again. */
4294 do
4295 {
4296 /* Try to skip some invisible text. Return value is the
4297 position reached which can be equal to where we start
4298 if there is nothing invisible there. This skips both
4299 over invisible text properties and overlays with
4300 invisible property. */
4301 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4302
4303 /* If we skipped nothing at all we weren't at invisible
4304 text in the first place. If everything to the end of
4305 the buffer was skipped, end the loop. */
4306 if (newpos == tem || newpos >= ZV)
4307 invis_p = 0;
4308 else
4309 {
4310 /* We skipped some characters but not necessarily
4311 all there are. Check if we ended up on visible
4312 text. Fget_char_property returns the property of
4313 the char before the given position, i.e. if we
4314 get invis_p = 0, this means that the char at
4315 newpos is visible. */
4316 pos = make_number (newpos);
4317 prop = Fget_char_property (pos, Qinvisible, it->window);
4318 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4319 }
4320
4321 /* If we ended up on invisible text, proceed to
4322 skip starting with next_stop. */
4323 if (invis_p)
4324 tem = next_stop;
4325
4326 /* If there are adjacent invisible texts, don't lose the
4327 second one's ellipsis. */
4328 if (invis_p == 2)
4329 display_ellipsis_p = 1;
4330 }
4331 while (invis_p);
4332
4333 /* The position newpos is now either ZV or on visible text. */
4334 if (it->bidi_p)
4335 {
4336 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4337 int on_newline =
4338 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4339 int after_newline =
4340 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4341
4342 /* If the invisible text ends on a newline or on a
4343 character after a newline, we can avoid the costly,
4344 character by character, bidi iteration to NEWPOS, and
4345 instead simply reseat the iterator there. That's
4346 because all bidi reordering information is tossed at
4347 the newline. This is a big win for modes that hide
4348 complete lines, like Outline, Org, etc. */
4349 if (on_newline || after_newline)
4350 {
4351 struct text_pos tpos;
4352 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4353
4354 SET_TEXT_POS (tpos, newpos, bpos);
4355 reseat_1 (it, tpos, 0);
4356 /* If we reseat on a newline/ZV, we need to prep the
4357 bidi iterator for advancing to the next character
4358 after the newline/EOB, keeping the current paragraph
4359 direction (so that PRODUCE_GLYPHS does TRT wrt
4360 prepending/appending glyphs to a glyph row). */
4361 if (on_newline)
4362 {
4363 it->bidi_it.first_elt = 0;
4364 it->bidi_it.paragraph_dir = pdir;
4365 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4366 it->bidi_it.nchars = 1;
4367 it->bidi_it.ch_len = 1;
4368 }
4369 }
4370 else /* Must use the slow method. */
4371 {
4372 /* With bidi iteration, the region of invisible text
4373 could start and/or end in the middle of a
4374 non-base embedding level. Therefore, we need to
4375 skip invisible text using the bidi iterator,
4376 starting at IT's current position, until we find
4377 ourselves outside of the invisible text.
4378 Skipping invisible text _after_ bidi iteration
4379 avoids affecting the visual order of the
4380 displayed text when invisible properties are
4381 added or removed. */
4382 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4383 {
4384 /* If we were `reseat'ed to a new paragraph,
4385 determine the paragraph base direction. We
4386 need to do it now because
4387 next_element_from_buffer may not have a
4388 chance to do it, if we are going to skip any
4389 text at the beginning, which resets the
4390 FIRST_ELT flag. */
4391 bidi_paragraph_init (it->paragraph_embedding,
4392 &it->bidi_it, 1);
4393 }
4394 do
4395 {
4396 bidi_move_to_visually_next (&it->bidi_it);
4397 }
4398 while (it->stop_charpos <= it->bidi_it.charpos
4399 && it->bidi_it.charpos < newpos);
4400 IT_CHARPOS (*it) = it->bidi_it.charpos;
4401 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4402 /* If we overstepped NEWPOS, record its position in
4403 the iterator, so that we skip invisible text if
4404 later the bidi iteration lands us in the
4405 invisible region again. */
4406 if (IT_CHARPOS (*it) >= newpos)
4407 it->prev_stop = newpos;
4408 }
4409 }
4410 else
4411 {
4412 IT_CHARPOS (*it) = newpos;
4413 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4414 }
4415
4416 /* If there are before-strings at the start of invisible
4417 text, and the text is invisible because of a text
4418 property, arrange to show before-strings because 20.x did
4419 it that way. (If the text is invisible because of an
4420 overlay property instead of a text property, this is
4421 already handled in the overlay code.) */
4422 if (NILP (overlay)
4423 && get_overlay_strings (it, it->stop_charpos))
4424 {
4425 handled = HANDLED_RECOMPUTE_PROPS;
4426 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4427 }
4428 else if (display_ellipsis_p)
4429 {
4430 /* Make sure that the glyphs of the ellipsis will get
4431 correct `charpos' values. If we would not update
4432 it->position here, the glyphs would belong to the
4433 last visible character _before_ the invisible
4434 text, which confuses `set_cursor_from_row'.
4435
4436 We use the last invisible position instead of the
4437 first because this way the cursor is always drawn on
4438 the first "." of the ellipsis, whenever PT is inside
4439 the invisible text. Otherwise the cursor would be
4440 placed _after_ the ellipsis when the point is after the
4441 first invisible character. */
4442 if (!STRINGP (it->object))
4443 {
4444 it->position.charpos = newpos - 1;
4445 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4446 }
4447 it->ellipsis_p = 1;
4448 /* Let the ellipsis display before
4449 considering any properties of the following char.
4450 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4451 handled = HANDLED_RETURN;
4452 }
4453 }
4454 }
4455
4456 return handled;
4457 }
4458
4459
4460 /* Make iterator IT return `...' next.
4461 Replaces LEN characters from buffer. */
4462
4463 static void
4464 setup_for_ellipsis (struct it *it, int len)
4465 {
4466 /* Use the display table definition for `...'. Invalid glyphs
4467 will be handled by the method returning elements from dpvec. */
4468 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4469 {
4470 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4471 it->dpvec = v->contents;
4472 it->dpend = v->contents + v->header.size;
4473 }
4474 else
4475 {
4476 /* Default `...'. */
4477 it->dpvec = default_invis_vector;
4478 it->dpend = default_invis_vector + 3;
4479 }
4480
4481 it->dpvec_char_len = len;
4482 it->current.dpvec_index = 0;
4483 it->dpvec_face_id = -1;
4484
4485 /* Remember the current face id in case glyphs specify faces.
4486 IT's face is restored in set_iterator_to_next.
4487 saved_face_id was set to preceding char's face in handle_stop. */
4488 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4489 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4490
4491 it->method = GET_FROM_DISPLAY_VECTOR;
4492 it->ellipsis_p = 1;
4493 }
4494
4495
4496 \f
4497 /***********************************************************************
4498 'display' property
4499 ***********************************************************************/
4500
4501 /* Set up iterator IT from `display' property at its current position.
4502 Called from handle_stop.
4503 We return HANDLED_RETURN if some part of the display property
4504 overrides the display of the buffer text itself.
4505 Otherwise we return HANDLED_NORMALLY. */
4506
4507 static enum prop_handled
4508 handle_display_prop (struct it *it)
4509 {
4510 Lisp_Object propval, object, overlay;
4511 struct text_pos *position;
4512 ptrdiff_t bufpos;
4513 /* Nonzero if some property replaces the display of the text itself. */
4514 int display_replaced_p = 0;
4515
4516 if (STRINGP (it->string))
4517 {
4518 object = it->string;
4519 position = &it->current.string_pos;
4520 bufpos = CHARPOS (it->current.pos);
4521 }
4522 else
4523 {
4524 XSETWINDOW (object, it->w);
4525 position = &it->current.pos;
4526 bufpos = CHARPOS (*position);
4527 }
4528
4529 /* Reset those iterator values set from display property values. */
4530 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4531 it->space_width = Qnil;
4532 it->font_height = Qnil;
4533 it->voffset = 0;
4534
4535 /* We don't support recursive `display' properties, i.e. string
4536 values that have a string `display' property, that have a string
4537 `display' property etc. */
4538 if (!it->string_from_display_prop_p)
4539 it->area = TEXT_AREA;
4540
4541 propval = get_char_property_and_overlay (make_number (position->charpos),
4542 Qdisplay, object, &overlay);
4543 if (NILP (propval))
4544 return HANDLED_NORMALLY;
4545 /* Now OVERLAY is the overlay that gave us this property, or nil
4546 if it was a text property. */
4547
4548 if (!STRINGP (it->string))
4549 object = it->w->contents;
4550
4551 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4552 position, bufpos,
4553 FRAME_WINDOW_P (it->f));
4554
4555 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4556 }
4557
4558 /* Subroutine of handle_display_prop. Returns non-zero if the display
4559 specification in SPEC is a replacing specification, i.e. it would
4560 replace the text covered by `display' property with something else,
4561 such as an image or a display string. If SPEC includes any kind or
4562 `(space ...) specification, the value is 2; this is used by
4563 compute_display_string_pos, which see.
4564
4565 See handle_single_display_spec for documentation of arguments.
4566 frame_window_p is non-zero if the window being redisplayed is on a
4567 GUI frame; this argument is used only if IT is NULL, see below.
4568
4569 IT can be NULL, if this is called by the bidi reordering code
4570 through compute_display_string_pos, which see. In that case, this
4571 function only examines SPEC, but does not otherwise "handle" it, in
4572 the sense that it doesn't set up members of IT from the display
4573 spec. */
4574 static int
4575 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4576 Lisp_Object overlay, struct text_pos *position,
4577 ptrdiff_t bufpos, int frame_window_p)
4578 {
4579 int replacing_p = 0;
4580 int rv;
4581
4582 if (CONSP (spec)
4583 /* Simple specifications. */
4584 && !EQ (XCAR (spec), Qimage)
4585 && !EQ (XCAR (spec), Qspace)
4586 && !EQ (XCAR (spec), Qwhen)
4587 && !EQ (XCAR (spec), Qslice)
4588 && !EQ (XCAR (spec), Qspace_width)
4589 && !EQ (XCAR (spec), Qheight)
4590 && !EQ (XCAR (spec), Qraise)
4591 /* Marginal area specifications. */
4592 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4593 && !EQ (XCAR (spec), Qleft_fringe)
4594 && !EQ (XCAR (spec), Qright_fringe)
4595 && !NILP (XCAR (spec)))
4596 {
4597 for (; CONSP (spec); spec = XCDR (spec))
4598 {
4599 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4600 overlay, position, bufpos,
4601 replacing_p, frame_window_p)))
4602 {
4603 replacing_p = rv;
4604 /* If some text in a string is replaced, `position' no
4605 longer points to the position of `object'. */
4606 if (!it || STRINGP (object))
4607 break;
4608 }
4609 }
4610 }
4611 else if (VECTORP (spec))
4612 {
4613 ptrdiff_t i;
4614 for (i = 0; i < ASIZE (spec); ++i)
4615 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4616 overlay, position, bufpos,
4617 replacing_p, frame_window_p)))
4618 {
4619 replacing_p = rv;
4620 /* If some text in a string is replaced, `position' no
4621 longer points to the position of `object'. */
4622 if (!it || STRINGP (object))
4623 break;
4624 }
4625 }
4626 else
4627 {
4628 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4629 position, bufpos, 0,
4630 frame_window_p)))
4631 replacing_p = rv;
4632 }
4633
4634 return replacing_p;
4635 }
4636
4637 /* Value is the position of the end of the `display' property starting
4638 at START_POS in OBJECT. */
4639
4640 static struct text_pos
4641 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4642 {
4643 Lisp_Object end;
4644 struct text_pos end_pos;
4645
4646 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4647 Qdisplay, object, Qnil);
4648 CHARPOS (end_pos) = XFASTINT (end);
4649 if (STRINGP (object))
4650 compute_string_pos (&end_pos, start_pos, it->string);
4651 else
4652 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4653
4654 return end_pos;
4655 }
4656
4657
4658 /* Set up IT from a single `display' property specification SPEC. OBJECT
4659 is the object in which the `display' property was found. *POSITION
4660 is the position in OBJECT at which the `display' property was found.
4661 BUFPOS is the buffer position of OBJECT (different from POSITION if
4662 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4663 previously saw a display specification which already replaced text
4664 display with something else, for example an image; we ignore such
4665 properties after the first one has been processed.
4666
4667 OVERLAY is the overlay this `display' property came from,
4668 or nil if it was a text property.
4669
4670 If SPEC is a `space' or `image' specification, and in some other
4671 cases too, set *POSITION to the position where the `display'
4672 property ends.
4673
4674 If IT is NULL, only examine the property specification in SPEC, but
4675 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4676 is intended to be displayed in a window on a GUI frame.
4677
4678 Value is non-zero if something was found which replaces the display
4679 of buffer or string text. */
4680
4681 static int
4682 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4683 Lisp_Object overlay, struct text_pos *position,
4684 ptrdiff_t bufpos, int display_replaced_p,
4685 int frame_window_p)
4686 {
4687 Lisp_Object form;
4688 Lisp_Object location, value;
4689 struct text_pos start_pos = *position;
4690 int valid_p;
4691
4692 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4693 If the result is non-nil, use VALUE instead of SPEC. */
4694 form = Qt;
4695 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4696 {
4697 spec = XCDR (spec);
4698 if (!CONSP (spec))
4699 return 0;
4700 form = XCAR (spec);
4701 spec = XCDR (spec);
4702 }
4703
4704 if (!NILP (form) && !EQ (form, Qt))
4705 {
4706 ptrdiff_t count = SPECPDL_INDEX ();
4707 struct gcpro gcpro1;
4708
4709 /* Bind `object' to the object having the `display' property, a
4710 buffer or string. Bind `position' to the position in the
4711 object where the property was found, and `buffer-position'
4712 to the current position in the buffer. */
4713
4714 if (NILP (object))
4715 XSETBUFFER (object, current_buffer);
4716 specbind (Qobject, object);
4717 specbind (Qposition, make_number (CHARPOS (*position)));
4718 specbind (Qbuffer_position, make_number (bufpos));
4719 GCPRO1 (form);
4720 form = safe_eval (form);
4721 UNGCPRO;
4722 unbind_to (count, Qnil);
4723 }
4724
4725 if (NILP (form))
4726 return 0;
4727
4728 /* Handle `(height HEIGHT)' specifications. */
4729 if (CONSP (spec)
4730 && EQ (XCAR (spec), Qheight)
4731 && CONSP (XCDR (spec)))
4732 {
4733 if (it)
4734 {
4735 if (!FRAME_WINDOW_P (it->f))
4736 return 0;
4737
4738 it->font_height = XCAR (XCDR (spec));
4739 if (!NILP (it->font_height))
4740 {
4741 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4742 int new_height = -1;
4743
4744 if (CONSP (it->font_height)
4745 && (EQ (XCAR (it->font_height), Qplus)
4746 || EQ (XCAR (it->font_height), Qminus))
4747 && CONSP (XCDR (it->font_height))
4748 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4749 {
4750 /* `(+ N)' or `(- N)' where N is an integer. */
4751 int steps = XINT (XCAR (XCDR (it->font_height)));
4752 if (EQ (XCAR (it->font_height), Qplus))
4753 steps = - steps;
4754 it->face_id = smaller_face (it->f, it->face_id, steps);
4755 }
4756 else if (FUNCTIONP (it->font_height))
4757 {
4758 /* Call function with current height as argument.
4759 Value is the new height. */
4760 Lisp_Object height;
4761 height = safe_call1 (it->font_height,
4762 face->lface[LFACE_HEIGHT_INDEX]);
4763 if (NUMBERP (height))
4764 new_height = XFLOATINT (height);
4765 }
4766 else if (NUMBERP (it->font_height))
4767 {
4768 /* Value is a multiple of the canonical char height. */
4769 struct face *f;
4770
4771 f = FACE_FROM_ID (it->f,
4772 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4773 new_height = (XFLOATINT (it->font_height)
4774 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4775 }
4776 else
4777 {
4778 /* Evaluate IT->font_height with `height' bound to the
4779 current specified height to get the new height. */
4780 ptrdiff_t count = SPECPDL_INDEX ();
4781
4782 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4783 value = safe_eval (it->font_height);
4784 unbind_to (count, Qnil);
4785
4786 if (NUMBERP (value))
4787 new_height = XFLOATINT (value);
4788 }
4789
4790 if (new_height > 0)
4791 it->face_id = face_with_height (it->f, it->face_id, new_height);
4792 }
4793 }
4794
4795 return 0;
4796 }
4797
4798 /* Handle `(space-width WIDTH)'. */
4799 if (CONSP (spec)
4800 && EQ (XCAR (spec), Qspace_width)
4801 && CONSP (XCDR (spec)))
4802 {
4803 if (it)
4804 {
4805 if (!FRAME_WINDOW_P (it->f))
4806 return 0;
4807
4808 value = XCAR (XCDR (spec));
4809 if (NUMBERP (value) && XFLOATINT (value) > 0)
4810 it->space_width = value;
4811 }
4812
4813 return 0;
4814 }
4815
4816 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4817 if (CONSP (spec)
4818 && EQ (XCAR (spec), Qslice))
4819 {
4820 Lisp_Object tem;
4821
4822 if (it)
4823 {
4824 if (!FRAME_WINDOW_P (it->f))
4825 return 0;
4826
4827 if (tem = XCDR (spec), CONSP (tem))
4828 {
4829 it->slice.x = XCAR (tem);
4830 if (tem = XCDR (tem), CONSP (tem))
4831 {
4832 it->slice.y = XCAR (tem);
4833 if (tem = XCDR (tem), CONSP (tem))
4834 {
4835 it->slice.width = XCAR (tem);
4836 if (tem = XCDR (tem), CONSP (tem))
4837 it->slice.height = XCAR (tem);
4838 }
4839 }
4840 }
4841 }
4842
4843 return 0;
4844 }
4845
4846 /* Handle `(raise FACTOR)'. */
4847 if (CONSP (spec)
4848 && EQ (XCAR (spec), Qraise)
4849 && CONSP (XCDR (spec)))
4850 {
4851 if (it)
4852 {
4853 if (!FRAME_WINDOW_P (it->f))
4854 return 0;
4855
4856 #ifdef HAVE_WINDOW_SYSTEM
4857 value = XCAR (XCDR (spec));
4858 if (NUMBERP (value))
4859 {
4860 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4861 it->voffset = - (XFLOATINT (value)
4862 * (FONT_HEIGHT (face->font)));
4863 }
4864 #endif /* HAVE_WINDOW_SYSTEM */
4865 }
4866
4867 return 0;
4868 }
4869
4870 /* Don't handle the other kinds of display specifications
4871 inside a string that we got from a `display' property. */
4872 if (it && it->string_from_display_prop_p)
4873 return 0;
4874
4875 /* Characters having this form of property are not displayed, so
4876 we have to find the end of the property. */
4877 if (it)
4878 {
4879 start_pos = *position;
4880 *position = display_prop_end (it, object, start_pos);
4881 }
4882 value = Qnil;
4883
4884 /* Stop the scan at that end position--we assume that all
4885 text properties change there. */
4886 if (it)
4887 it->stop_charpos = position->charpos;
4888
4889 /* Handle `(left-fringe BITMAP [FACE])'
4890 and `(right-fringe BITMAP [FACE])'. */
4891 if (CONSP (spec)
4892 && (EQ (XCAR (spec), Qleft_fringe)
4893 || EQ (XCAR (spec), Qright_fringe))
4894 && CONSP (XCDR (spec)))
4895 {
4896 int fringe_bitmap;
4897
4898 if (it)
4899 {
4900 if (!FRAME_WINDOW_P (it->f))
4901 /* If we return here, POSITION has been advanced
4902 across the text with this property. */
4903 {
4904 /* Synchronize the bidi iterator with POSITION. This is
4905 needed because we are not going to push the iterator
4906 on behalf of this display property, so there will be
4907 no pop_it call to do this synchronization for us. */
4908 if (it->bidi_p)
4909 {
4910 it->position = *position;
4911 iterate_out_of_display_property (it);
4912 *position = it->position;
4913 }
4914 return 1;
4915 }
4916 }
4917 else if (!frame_window_p)
4918 return 1;
4919
4920 #ifdef HAVE_WINDOW_SYSTEM
4921 value = XCAR (XCDR (spec));
4922 if (!SYMBOLP (value)
4923 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4924 /* If we return here, POSITION has been advanced
4925 across the text with this property. */
4926 {
4927 if (it && it->bidi_p)
4928 {
4929 it->position = *position;
4930 iterate_out_of_display_property (it);
4931 *position = it->position;
4932 }
4933 return 1;
4934 }
4935
4936 if (it)
4937 {
4938 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4939
4940 if (CONSP (XCDR (XCDR (spec))))
4941 {
4942 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4943 int face_id2 = lookup_derived_face (it->f, face_name,
4944 FRINGE_FACE_ID, 0);
4945 if (face_id2 >= 0)
4946 face_id = face_id2;
4947 }
4948
4949 /* Save current settings of IT so that we can restore them
4950 when we are finished with the glyph property value. */
4951 push_it (it, position);
4952
4953 it->area = TEXT_AREA;
4954 it->what = IT_IMAGE;
4955 it->image_id = -1; /* no image */
4956 it->position = start_pos;
4957 it->object = NILP (object) ? it->w->contents : object;
4958 it->method = GET_FROM_IMAGE;
4959 it->from_overlay = Qnil;
4960 it->face_id = face_id;
4961 it->from_disp_prop_p = 1;
4962
4963 /* Say that we haven't consumed the characters with
4964 `display' property yet. The call to pop_it in
4965 set_iterator_to_next will clean this up. */
4966 *position = start_pos;
4967
4968 if (EQ (XCAR (spec), Qleft_fringe))
4969 {
4970 it->left_user_fringe_bitmap = fringe_bitmap;
4971 it->left_user_fringe_face_id = face_id;
4972 }
4973 else
4974 {
4975 it->right_user_fringe_bitmap = fringe_bitmap;
4976 it->right_user_fringe_face_id = face_id;
4977 }
4978 }
4979 #endif /* HAVE_WINDOW_SYSTEM */
4980 return 1;
4981 }
4982
4983 /* Prepare to handle `((margin left-margin) ...)',
4984 `((margin right-margin) ...)' and `((margin nil) ...)'
4985 prefixes for display specifications. */
4986 location = Qunbound;
4987 if (CONSP (spec) && CONSP (XCAR (spec)))
4988 {
4989 Lisp_Object tem;
4990
4991 value = XCDR (spec);
4992 if (CONSP (value))
4993 value = XCAR (value);
4994
4995 tem = XCAR (spec);
4996 if (EQ (XCAR (tem), Qmargin)
4997 && (tem = XCDR (tem),
4998 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4999 (NILP (tem)
5000 || EQ (tem, Qleft_margin)
5001 || EQ (tem, Qright_margin))))
5002 location = tem;
5003 }
5004
5005 if (EQ (location, Qunbound))
5006 {
5007 location = Qnil;
5008 value = spec;
5009 }
5010
5011 /* After this point, VALUE is the property after any
5012 margin prefix has been stripped. It must be a string,
5013 an image specification, or `(space ...)'.
5014
5015 LOCATION specifies where to display: `left-margin',
5016 `right-margin' or nil. */
5017
5018 valid_p = (STRINGP (value)
5019 #ifdef HAVE_WINDOW_SYSTEM
5020 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5021 && valid_image_p (value))
5022 #endif /* not HAVE_WINDOW_SYSTEM */
5023 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5024
5025 if (valid_p && !display_replaced_p)
5026 {
5027 int retval = 1;
5028
5029 if (!it)
5030 {
5031 /* Callers need to know whether the display spec is any kind
5032 of `(space ...)' spec that is about to affect text-area
5033 display. */
5034 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5035 retval = 2;
5036 return retval;
5037 }
5038
5039 /* Save current settings of IT so that we can restore them
5040 when we are finished with the glyph property value. */
5041 push_it (it, position);
5042 it->from_overlay = overlay;
5043 it->from_disp_prop_p = 1;
5044
5045 if (NILP (location))
5046 it->area = TEXT_AREA;
5047 else if (EQ (location, Qleft_margin))
5048 it->area = LEFT_MARGIN_AREA;
5049 else
5050 it->area = RIGHT_MARGIN_AREA;
5051
5052 if (STRINGP (value))
5053 {
5054 it->string = value;
5055 it->multibyte_p = STRING_MULTIBYTE (it->string);
5056 it->current.overlay_string_index = -1;
5057 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5058 it->end_charpos = it->string_nchars = SCHARS (it->string);
5059 it->method = GET_FROM_STRING;
5060 it->stop_charpos = 0;
5061 it->prev_stop = 0;
5062 it->base_level_stop = 0;
5063 it->string_from_display_prop_p = 1;
5064 /* Say that we haven't consumed the characters with
5065 `display' property yet. The call to pop_it in
5066 set_iterator_to_next will clean this up. */
5067 if (BUFFERP (object))
5068 *position = start_pos;
5069
5070 /* Force paragraph direction to be that of the parent
5071 object. If the parent object's paragraph direction is
5072 not yet determined, default to L2R. */
5073 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5074 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5075 else
5076 it->paragraph_embedding = L2R;
5077
5078 /* Set up the bidi iterator for this display string. */
5079 if (it->bidi_p)
5080 {
5081 it->bidi_it.string.lstring = it->string;
5082 it->bidi_it.string.s = NULL;
5083 it->bidi_it.string.schars = it->end_charpos;
5084 it->bidi_it.string.bufpos = bufpos;
5085 it->bidi_it.string.from_disp_str = 1;
5086 it->bidi_it.string.unibyte = !it->multibyte_p;
5087 it->bidi_it.w = it->w;
5088 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5089 }
5090 }
5091 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5092 {
5093 it->method = GET_FROM_STRETCH;
5094 it->object = value;
5095 *position = it->position = start_pos;
5096 retval = 1 + (it->area == TEXT_AREA);
5097 }
5098 #ifdef HAVE_WINDOW_SYSTEM
5099 else
5100 {
5101 it->what = IT_IMAGE;
5102 it->image_id = lookup_image (it->f, value);
5103 it->position = start_pos;
5104 it->object = NILP (object) ? it->w->contents : object;
5105 it->method = GET_FROM_IMAGE;
5106
5107 /* Say that we haven't consumed the characters with
5108 `display' property yet. The call to pop_it in
5109 set_iterator_to_next will clean this up. */
5110 *position = start_pos;
5111 }
5112 #endif /* HAVE_WINDOW_SYSTEM */
5113
5114 return retval;
5115 }
5116
5117 /* Invalid property or property not supported. Restore
5118 POSITION to what it was before. */
5119 *position = start_pos;
5120 return 0;
5121 }
5122
5123 /* Check if PROP is a display property value whose text should be
5124 treated as intangible. OVERLAY is the overlay from which PROP
5125 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5126 specify the buffer position covered by PROP. */
5127
5128 int
5129 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5130 ptrdiff_t charpos, ptrdiff_t bytepos)
5131 {
5132 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5133 struct text_pos position;
5134
5135 SET_TEXT_POS (position, charpos, bytepos);
5136 return handle_display_spec (NULL, prop, Qnil, overlay,
5137 &position, charpos, frame_window_p);
5138 }
5139
5140
5141 /* Return 1 if PROP is a display sub-property value containing STRING.
5142
5143 Implementation note: this and the following function are really
5144 special cases of handle_display_spec and
5145 handle_single_display_spec, and should ideally use the same code.
5146 Until they do, these two pairs must be consistent and must be
5147 modified in sync. */
5148
5149 static int
5150 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5151 {
5152 if (EQ (string, prop))
5153 return 1;
5154
5155 /* Skip over `when FORM'. */
5156 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5157 {
5158 prop = XCDR (prop);
5159 if (!CONSP (prop))
5160 return 0;
5161 /* Actually, the condition following `when' should be eval'ed,
5162 like handle_single_display_spec does, and we should return
5163 zero if it evaluates to nil. However, this function is
5164 called only when the buffer was already displayed and some
5165 glyph in the glyph matrix was found to come from a display
5166 string. Therefore, the condition was already evaluated, and
5167 the result was non-nil, otherwise the display string wouldn't
5168 have been displayed and we would have never been called for
5169 this property. Thus, we can skip the evaluation and assume
5170 its result is non-nil. */
5171 prop = XCDR (prop);
5172 }
5173
5174 if (CONSP (prop))
5175 /* Skip over `margin LOCATION'. */
5176 if (EQ (XCAR (prop), Qmargin))
5177 {
5178 prop = XCDR (prop);
5179 if (!CONSP (prop))
5180 return 0;
5181
5182 prop = XCDR (prop);
5183 if (!CONSP (prop))
5184 return 0;
5185 }
5186
5187 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5188 }
5189
5190
5191 /* Return 1 if STRING appears in the `display' property PROP. */
5192
5193 static int
5194 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5195 {
5196 if (CONSP (prop)
5197 && !EQ (XCAR (prop), Qwhen)
5198 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5199 {
5200 /* A list of sub-properties. */
5201 while (CONSP (prop))
5202 {
5203 if (single_display_spec_string_p (XCAR (prop), string))
5204 return 1;
5205 prop = XCDR (prop);
5206 }
5207 }
5208 else if (VECTORP (prop))
5209 {
5210 /* A vector of sub-properties. */
5211 ptrdiff_t i;
5212 for (i = 0; i < ASIZE (prop); ++i)
5213 if (single_display_spec_string_p (AREF (prop, i), string))
5214 return 1;
5215 }
5216 else
5217 return single_display_spec_string_p (prop, string);
5218
5219 return 0;
5220 }
5221
5222 /* Look for STRING in overlays and text properties in the current
5223 buffer, between character positions FROM and TO (excluding TO).
5224 BACK_P non-zero means look back (in this case, TO is supposed to be
5225 less than FROM).
5226 Value is the first character position where STRING was found, or
5227 zero if it wasn't found before hitting TO.
5228
5229 This function may only use code that doesn't eval because it is
5230 called asynchronously from note_mouse_highlight. */
5231
5232 static ptrdiff_t
5233 string_buffer_position_lim (Lisp_Object string,
5234 ptrdiff_t from, ptrdiff_t to, int back_p)
5235 {
5236 Lisp_Object limit, prop, pos;
5237 int found = 0;
5238
5239 pos = make_number (max (from, BEGV));
5240
5241 if (!back_p) /* looking forward */
5242 {
5243 limit = make_number (min (to, ZV));
5244 while (!found && !EQ (pos, limit))
5245 {
5246 prop = Fget_char_property (pos, Qdisplay, Qnil);
5247 if (!NILP (prop) && display_prop_string_p (prop, string))
5248 found = 1;
5249 else
5250 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5251 limit);
5252 }
5253 }
5254 else /* looking back */
5255 {
5256 limit = make_number (max (to, BEGV));
5257 while (!found && !EQ (pos, limit))
5258 {
5259 prop = Fget_char_property (pos, Qdisplay, Qnil);
5260 if (!NILP (prop) && display_prop_string_p (prop, string))
5261 found = 1;
5262 else
5263 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5264 limit);
5265 }
5266 }
5267
5268 return found ? XINT (pos) : 0;
5269 }
5270
5271 /* Determine which buffer position in current buffer STRING comes from.
5272 AROUND_CHARPOS is an approximate position where it could come from.
5273 Value is the buffer position or 0 if it couldn't be determined.
5274
5275 This function is necessary because we don't record buffer positions
5276 in glyphs generated from strings (to keep struct glyph small).
5277 This function may only use code that doesn't eval because it is
5278 called asynchronously from note_mouse_highlight. */
5279
5280 static ptrdiff_t
5281 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5282 {
5283 const int MAX_DISTANCE = 1000;
5284 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5285 around_charpos + MAX_DISTANCE,
5286 0);
5287
5288 if (!found)
5289 found = string_buffer_position_lim (string, around_charpos,
5290 around_charpos - MAX_DISTANCE, 1);
5291 return found;
5292 }
5293
5294
5295 \f
5296 /***********************************************************************
5297 `composition' property
5298 ***********************************************************************/
5299
5300 /* Set up iterator IT from `composition' property at its current
5301 position. Called from handle_stop. */
5302
5303 static enum prop_handled
5304 handle_composition_prop (struct it *it)
5305 {
5306 Lisp_Object prop, string;
5307 ptrdiff_t pos, pos_byte, start, end;
5308
5309 if (STRINGP (it->string))
5310 {
5311 unsigned char *s;
5312
5313 pos = IT_STRING_CHARPOS (*it);
5314 pos_byte = IT_STRING_BYTEPOS (*it);
5315 string = it->string;
5316 s = SDATA (string) + pos_byte;
5317 it->c = STRING_CHAR (s);
5318 }
5319 else
5320 {
5321 pos = IT_CHARPOS (*it);
5322 pos_byte = IT_BYTEPOS (*it);
5323 string = Qnil;
5324 it->c = FETCH_CHAR (pos_byte);
5325 }
5326
5327 /* If there's a valid composition and point is not inside of the
5328 composition (in the case that the composition is from the current
5329 buffer), draw a glyph composed from the composition components. */
5330 if (find_composition (pos, -1, &start, &end, &prop, string)
5331 && composition_valid_p (start, end, prop)
5332 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5333 {
5334 if (start < pos)
5335 /* As we can't handle this situation (perhaps font-lock added
5336 a new composition), we just return here hoping that next
5337 redisplay will detect this composition much earlier. */
5338 return HANDLED_NORMALLY;
5339 if (start != pos)
5340 {
5341 if (STRINGP (it->string))
5342 pos_byte = string_char_to_byte (it->string, start);
5343 else
5344 pos_byte = CHAR_TO_BYTE (start);
5345 }
5346 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5347 prop, string);
5348
5349 if (it->cmp_it.id >= 0)
5350 {
5351 it->cmp_it.ch = -1;
5352 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5353 it->cmp_it.nglyphs = -1;
5354 }
5355 }
5356
5357 return HANDLED_NORMALLY;
5358 }
5359
5360
5361 \f
5362 /***********************************************************************
5363 Overlay strings
5364 ***********************************************************************/
5365
5366 /* The following structure is used to record overlay strings for
5367 later sorting in load_overlay_strings. */
5368
5369 struct overlay_entry
5370 {
5371 Lisp_Object overlay;
5372 Lisp_Object string;
5373 EMACS_INT priority;
5374 int after_string_p;
5375 };
5376
5377
5378 /* Set up iterator IT from overlay strings at its current position.
5379 Called from handle_stop. */
5380
5381 static enum prop_handled
5382 handle_overlay_change (struct it *it)
5383 {
5384 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5385 return HANDLED_RECOMPUTE_PROPS;
5386 else
5387 return HANDLED_NORMALLY;
5388 }
5389
5390
5391 /* Set up the next overlay string for delivery by IT, if there is an
5392 overlay string to deliver. Called by set_iterator_to_next when the
5393 end of the current overlay string is reached. If there are more
5394 overlay strings to display, IT->string and
5395 IT->current.overlay_string_index are set appropriately here.
5396 Otherwise IT->string is set to nil. */
5397
5398 static void
5399 next_overlay_string (struct it *it)
5400 {
5401 ++it->current.overlay_string_index;
5402 if (it->current.overlay_string_index == it->n_overlay_strings)
5403 {
5404 /* No more overlay strings. Restore IT's settings to what
5405 they were before overlay strings were processed, and
5406 continue to deliver from current_buffer. */
5407
5408 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5409 pop_it (it);
5410 eassert (it->sp > 0
5411 || (NILP (it->string)
5412 && it->method == GET_FROM_BUFFER
5413 && it->stop_charpos >= BEGV
5414 && it->stop_charpos <= it->end_charpos));
5415 it->current.overlay_string_index = -1;
5416 it->n_overlay_strings = 0;
5417 it->overlay_strings_charpos = -1;
5418 /* If there's an empty display string on the stack, pop the
5419 stack, to resync the bidi iterator with IT's position. Such
5420 empty strings are pushed onto the stack in
5421 get_overlay_strings_1. */
5422 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5423 pop_it (it);
5424
5425 /* If we're at the end of the buffer, record that we have
5426 processed the overlay strings there already, so that
5427 next_element_from_buffer doesn't try it again. */
5428 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5429 it->overlay_strings_at_end_processed_p = 1;
5430 }
5431 else
5432 {
5433 /* There are more overlay strings to process. If
5434 IT->current.overlay_string_index has advanced to a position
5435 where we must load IT->overlay_strings with more strings, do
5436 it. We must load at the IT->overlay_strings_charpos where
5437 IT->n_overlay_strings was originally computed; when invisible
5438 text is present, this might not be IT_CHARPOS (Bug#7016). */
5439 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5440
5441 if (it->current.overlay_string_index && i == 0)
5442 load_overlay_strings (it, it->overlay_strings_charpos);
5443
5444 /* Initialize IT to deliver display elements from the overlay
5445 string. */
5446 it->string = it->overlay_strings[i];
5447 it->multibyte_p = STRING_MULTIBYTE (it->string);
5448 SET_TEXT_POS (it->current.string_pos, 0, 0);
5449 it->method = GET_FROM_STRING;
5450 it->stop_charpos = 0;
5451 it->end_charpos = SCHARS (it->string);
5452 if (it->cmp_it.stop_pos >= 0)
5453 it->cmp_it.stop_pos = 0;
5454 it->prev_stop = 0;
5455 it->base_level_stop = 0;
5456
5457 /* Set up the bidi iterator for this overlay string. */
5458 if (it->bidi_p)
5459 {
5460 it->bidi_it.string.lstring = it->string;
5461 it->bidi_it.string.s = NULL;
5462 it->bidi_it.string.schars = SCHARS (it->string);
5463 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5464 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5465 it->bidi_it.string.unibyte = !it->multibyte_p;
5466 it->bidi_it.w = it->w;
5467 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5468 }
5469 }
5470
5471 CHECK_IT (it);
5472 }
5473
5474
5475 /* Compare two overlay_entry structures E1 and E2. Used as a
5476 comparison function for qsort in load_overlay_strings. Overlay
5477 strings for the same position are sorted so that
5478
5479 1. All after-strings come in front of before-strings, except
5480 when they come from the same overlay.
5481
5482 2. Within after-strings, strings are sorted so that overlay strings
5483 from overlays with higher priorities come first.
5484
5485 2. Within before-strings, strings are sorted so that overlay
5486 strings from overlays with higher priorities come last.
5487
5488 Value is analogous to strcmp. */
5489
5490
5491 static int
5492 compare_overlay_entries (const void *e1, const void *e2)
5493 {
5494 struct overlay_entry const *entry1 = e1;
5495 struct overlay_entry const *entry2 = e2;
5496 int result;
5497
5498 if (entry1->after_string_p != entry2->after_string_p)
5499 {
5500 /* Let after-strings appear in front of before-strings if
5501 they come from different overlays. */
5502 if (EQ (entry1->overlay, entry2->overlay))
5503 result = entry1->after_string_p ? 1 : -1;
5504 else
5505 result = entry1->after_string_p ? -1 : 1;
5506 }
5507 else if (entry1->priority != entry2->priority)
5508 {
5509 if (entry1->after_string_p)
5510 /* After-strings sorted in order of decreasing priority. */
5511 result = entry2->priority < entry1->priority ? -1 : 1;
5512 else
5513 /* Before-strings sorted in order of increasing priority. */
5514 result = entry1->priority < entry2->priority ? -1 : 1;
5515 }
5516 else
5517 result = 0;
5518
5519 return result;
5520 }
5521
5522
5523 /* Load the vector IT->overlay_strings with overlay strings from IT's
5524 current buffer position, or from CHARPOS if that is > 0. Set
5525 IT->n_overlays to the total number of overlay strings found.
5526
5527 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5528 a time. On entry into load_overlay_strings,
5529 IT->current.overlay_string_index gives the number of overlay
5530 strings that have already been loaded by previous calls to this
5531 function.
5532
5533 IT->add_overlay_start contains an additional overlay start
5534 position to consider for taking overlay strings from, if non-zero.
5535 This position comes into play when the overlay has an `invisible'
5536 property, and both before and after-strings. When we've skipped to
5537 the end of the overlay, because of its `invisible' property, we
5538 nevertheless want its before-string to appear.
5539 IT->add_overlay_start will contain the overlay start position
5540 in this case.
5541
5542 Overlay strings are sorted so that after-string strings come in
5543 front of before-string strings. Within before and after-strings,
5544 strings are sorted by overlay priority. See also function
5545 compare_overlay_entries. */
5546
5547 static void
5548 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5549 {
5550 Lisp_Object overlay, window, str, invisible;
5551 struct Lisp_Overlay *ov;
5552 ptrdiff_t start, end;
5553 ptrdiff_t size = 20;
5554 ptrdiff_t n = 0, i, j;
5555 int invis_p;
5556 struct overlay_entry *entries = alloca (size * sizeof *entries);
5557 USE_SAFE_ALLOCA;
5558
5559 if (charpos <= 0)
5560 charpos = IT_CHARPOS (*it);
5561
5562 /* Append the overlay string STRING of overlay OVERLAY to vector
5563 `entries' which has size `size' and currently contains `n'
5564 elements. AFTER_P non-zero means STRING is an after-string of
5565 OVERLAY. */
5566 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5567 do \
5568 { \
5569 Lisp_Object priority; \
5570 \
5571 if (n == size) \
5572 { \
5573 struct overlay_entry *old = entries; \
5574 SAFE_NALLOCA (entries, 2, size); \
5575 memcpy (entries, old, size * sizeof *entries); \
5576 size *= 2; \
5577 } \
5578 \
5579 entries[n].string = (STRING); \
5580 entries[n].overlay = (OVERLAY); \
5581 priority = Foverlay_get ((OVERLAY), Qpriority); \
5582 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5583 entries[n].after_string_p = (AFTER_P); \
5584 ++n; \
5585 } \
5586 while (0)
5587
5588 /* Process overlay before the overlay center. */
5589 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5590 {
5591 XSETMISC (overlay, ov);
5592 eassert (OVERLAYP (overlay));
5593 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5594 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5595
5596 if (end < charpos)
5597 break;
5598
5599 /* Skip this overlay if it doesn't start or end at IT's current
5600 position. */
5601 if (end != charpos && start != charpos)
5602 continue;
5603
5604 /* Skip this overlay if it doesn't apply to IT->w. */
5605 window = Foverlay_get (overlay, Qwindow);
5606 if (WINDOWP (window) && XWINDOW (window) != it->w)
5607 continue;
5608
5609 /* If the text ``under'' the overlay is invisible, both before-
5610 and after-strings from this overlay are visible; start and
5611 end position are indistinguishable. */
5612 invisible = Foverlay_get (overlay, Qinvisible);
5613 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5614
5615 /* If overlay has a non-empty before-string, record it. */
5616 if ((start == charpos || (end == charpos && invis_p))
5617 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5618 && SCHARS (str))
5619 RECORD_OVERLAY_STRING (overlay, str, 0);
5620
5621 /* If overlay has a non-empty after-string, record it. */
5622 if ((end == charpos || (start == charpos && invis_p))
5623 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5624 && SCHARS (str))
5625 RECORD_OVERLAY_STRING (overlay, str, 1);
5626 }
5627
5628 /* Process overlays after the overlay center. */
5629 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5630 {
5631 XSETMISC (overlay, ov);
5632 eassert (OVERLAYP (overlay));
5633 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5634 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5635
5636 if (start > charpos)
5637 break;
5638
5639 /* Skip this overlay if it doesn't start or end at IT's current
5640 position. */
5641 if (end != charpos && start != charpos)
5642 continue;
5643
5644 /* Skip this overlay if it doesn't apply to IT->w. */
5645 window = Foverlay_get (overlay, Qwindow);
5646 if (WINDOWP (window) && XWINDOW (window) != it->w)
5647 continue;
5648
5649 /* If the text ``under'' the overlay is invisible, it has a zero
5650 dimension, and both before- and after-strings apply. */
5651 invisible = Foverlay_get (overlay, Qinvisible);
5652 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5653
5654 /* If overlay has a non-empty before-string, record it. */
5655 if ((start == charpos || (end == charpos && invis_p))
5656 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5657 && SCHARS (str))
5658 RECORD_OVERLAY_STRING (overlay, str, 0);
5659
5660 /* If overlay has a non-empty after-string, record it. */
5661 if ((end == charpos || (start == charpos && invis_p))
5662 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5663 && SCHARS (str))
5664 RECORD_OVERLAY_STRING (overlay, str, 1);
5665 }
5666
5667 #undef RECORD_OVERLAY_STRING
5668
5669 /* Sort entries. */
5670 if (n > 1)
5671 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5672
5673 /* Record number of overlay strings, and where we computed it. */
5674 it->n_overlay_strings = n;
5675 it->overlay_strings_charpos = charpos;
5676
5677 /* IT->current.overlay_string_index is the number of overlay strings
5678 that have already been consumed by IT. Copy some of the
5679 remaining overlay strings to IT->overlay_strings. */
5680 i = 0;
5681 j = it->current.overlay_string_index;
5682 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5683 {
5684 it->overlay_strings[i] = entries[j].string;
5685 it->string_overlays[i++] = entries[j++].overlay;
5686 }
5687
5688 CHECK_IT (it);
5689 SAFE_FREE ();
5690 }
5691
5692
5693 /* Get the first chunk of overlay strings at IT's current buffer
5694 position, or at CHARPOS if that is > 0. Value is non-zero if at
5695 least one overlay string was found. */
5696
5697 static int
5698 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5699 {
5700 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5701 process. This fills IT->overlay_strings with strings, and sets
5702 IT->n_overlay_strings to the total number of strings to process.
5703 IT->pos.overlay_string_index has to be set temporarily to zero
5704 because load_overlay_strings needs this; it must be set to -1
5705 when no overlay strings are found because a zero value would
5706 indicate a position in the first overlay string. */
5707 it->current.overlay_string_index = 0;
5708 load_overlay_strings (it, charpos);
5709
5710 /* If we found overlay strings, set up IT to deliver display
5711 elements from the first one. Otherwise set up IT to deliver
5712 from current_buffer. */
5713 if (it->n_overlay_strings)
5714 {
5715 /* Make sure we know settings in current_buffer, so that we can
5716 restore meaningful values when we're done with the overlay
5717 strings. */
5718 if (compute_stop_p)
5719 compute_stop_pos (it);
5720 eassert (it->face_id >= 0);
5721
5722 /* Save IT's settings. They are restored after all overlay
5723 strings have been processed. */
5724 eassert (!compute_stop_p || it->sp == 0);
5725
5726 /* When called from handle_stop, there might be an empty display
5727 string loaded. In that case, don't bother saving it. But
5728 don't use this optimization with the bidi iterator, since we
5729 need the corresponding pop_it call to resync the bidi
5730 iterator's position with IT's position, after we are done
5731 with the overlay strings. (The corresponding call to pop_it
5732 in case of an empty display string is in
5733 next_overlay_string.) */
5734 if (!(!it->bidi_p
5735 && STRINGP (it->string) && !SCHARS (it->string)))
5736 push_it (it, NULL);
5737
5738 /* Set up IT to deliver display elements from the first overlay
5739 string. */
5740 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5741 it->string = it->overlay_strings[0];
5742 it->from_overlay = Qnil;
5743 it->stop_charpos = 0;
5744 eassert (STRINGP (it->string));
5745 it->end_charpos = SCHARS (it->string);
5746 it->prev_stop = 0;
5747 it->base_level_stop = 0;
5748 it->multibyte_p = STRING_MULTIBYTE (it->string);
5749 it->method = GET_FROM_STRING;
5750 it->from_disp_prop_p = 0;
5751
5752 /* Force paragraph direction to be that of the parent
5753 buffer. */
5754 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5755 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5756 else
5757 it->paragraph_embedding = L2R;
5758
5759 /* Set up the bidi iterator for this overlay string. */
5760 if (it->bidi_p)
5761 {
5762 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5763
5764 it->bidi_it.string.lstring = it->string;
5765 it->bidi_it.string.s = NULL;
5766 it->bidi_it.string.schars = SCHARS (it->string);
5767 it->bidi_it.string.bufpos = pos;
5768 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5769 it->bidi_it.string.unibyte = !it->multibyte_p;
5770 it->bidi_it.w = it->w;
5771 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5772 }
5773 return 1;
5774 }
5775
5776 it->current.overlay_string_index = -1;
5777 return 0;
5778 }
5779
5780 static int
5781 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5782 {
5783 it->string = Qnil;
5784 it->method = GET_FROM_BUFFER;
5785
5786 (void) get_overlay_strings_1 (it, charpos, 1);
5787
5788 CHECK_IT (it);
5789
5790 /* Value is non-zero if we found at least one overlay string. */
5791 return STRINGP (it->string);
5792 }
5793
5794
5795 \f
5796 /***********************************************************************
5797 Saving and restoring state
5798 ***********************************************************************/
5799
5800 /* Save current settings of IT on IT->stack. Called, for example,
5801 before setting up IT for an overlay string, to be able to restore
5802 IT's settings to what they were after the overlay string has been
5803 processed. If POSITION is non-NULL, it is the position to save on
5804 the stack instead of IT->position. */
5805
5806 static void
5807 push_it (struct it *it, struct text_pos *position)
5808 {
5809 struct iterator_stack_entry *p;
5810
5811 eassert (it->sp < IT_STACK_SIZE);
5812 p = it->stack + it->sp;
5813
5814 p->stop_charpos = it->stop_charpos;
5815 p->prev_stop = it->prev_stop;
5816 p->base_level_stop = it->base_level_stop;
5817 p->cmp_it = it->cmp_it;
5818 eassert (it->face_id >= 0);
5819 p->face_id = it->face_id;
5820 p->string = it->string;
5821 p->method = it->method;
5822 p->from_overlay = it->from_overlay;
5823 switch (p->method)
5824 {
5825 case GET_FROM_IMAGE:
5826 p->u.image.object = it->object;
5827 p->u.image.image_id = it->image_id;
5828 p->u.image.slice = it->slice;
5829 break;
5830 case GET_FROM_STRETCH:
5831 p->u.stretch.object = it->object;
5832 break;
5833 }
5834 p->position = position ? *position : it->position;
5835 p->current = it->current;
5836 p->end_charpos = it->end_charpos;
5837 p->string_nchars = it->string_nchars;
5838 p->area = it->area;
5839 p->multibyte_p = it->multibyte_p;
5840 p->avoid_cursor_p = it->avoid_cursor_p;
5841 p->space_width = it->space_width;
5842 p->font_height = it->font_height;
5843 p->voffset = it->voffset;
5844 p->string_from_display_prop_p = it->string_from_display_prop_p;
5845 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5846 p->display_ellipsis_p = 0;
5847 p->line_wrap = it->line_wrap;
5848 p->bidi_p = it->bidi_p;
5849 p->paragraph_embedding = it->paragraph_embedding;
5850 p->from_disp_prop_p = it->from_disp_prop_p;
5851 ++it->sp;
5852
5853 /* Save the state of the bidi iterator as well. */
5854 if (it->bidi_p)
5855 bidi_push_it (&it->bidi_it);
5856 }
5857
5858 static void
5859 iterate_out_of_display_property (struct it *it)
5860 {
5861 int buffer_p = !STRINGP (it->string);
5862 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5863 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5864
5865 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5866
5867 /* Maybe initialize paragraph direction. If we are at the beginning
5868 of a new paragraph, next_element_from_buffer may not have a
5869 chance to do that. */
5870 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5871 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5872 /* prev_stop can be zero, so check against BEGV as well. */
5873 while (it->bidi_it.charpos >= bob
5874 && it->prev_stop <= it->bidi_it.charpos
5875 && it->bidi_it.charpos < CHARPOS (it->position)
5876 && it->bidi_it.charpos < eob)
5877 bidi_move_to_visually_next (&it->bidi_it);
5878 /* Record the stop_pos we just crossed, for when we cross it
5879 back, maybe. */
5880 if (it->bidi_it.charpos > CHARPOS (it->position))
5881 it->prev_stop = CHARPOS (it->position);
5882 /* If we ended up not where pop_it put us, resync IT's
5883 positional members with the bidi iterator. */
5884 if (it->bidi_it.charpos != CHARPOS (it->position))
5885 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5886 if (buffer_p)
5887 it->current.pos = it->position;
5888 else
5889 it->current.string_pos = it->position;
5890 }
5891
5892 /* Restore IT's settings from IT->stack. Called, for example, when no
5893 more overlay strings must be processed, and we return to delivering
5894 display elements from a buffer, or when the end of a string from a
5895 `display' property is reached and we return to delivering display
5896 elements from an overlay string, or from a buffer. */
5897
5898 static void
5899 pop_it (struct it *it)
5900 {
5901 struct iterator_stack_entry *p;
5902 int from_display_prop = it->from_disp_prop_p;
5903
5904 eassert (it->sp > 0);
5905 --it->sp;
5906 p = it->stack + it->sp;
5907 it->stop_charpos = p->stop_charpos;
5908 it->prev_stop = p->prev_stop;
5909 it->base_level_stop = p->base_level_stop;
5910 it->cmp_it = p->cmp_it;
5911 it->face_id = p->face_id;
5912 it->current = p->current;
5913 it->position = p->position;
5914 it->string = p->string;
5915 it->from_overlay = p->from_overlay;
5916 if (NILP (it->string))
5917 SET_TEXT_POS (it->current.string_pos, -1, -1);
5918 it->method = p->method;
5919 switch (it->method)
5920 {
5921 case GET_FROM_IMAGE:
5922 it->image_id = p->u.image.image_id;
5923 it->object = p->u.image.object;
5924 it->slice = p->u.image.slice;
5925 break;
5926 case GET_FROM_STRETCH:
5927 it->object = p->u.stretch.object;
5928 break;
5929 case GET_FROM_BUFFER:
5930 it->object = it->w->contents;
5931 break;
5932 case GET_FROM_STRING:
5933 it->object = it->string;
5934 break;
5935 case GET_FROM_DISPLAY_VECTOR:
5936 if (it->s)
5937 it->method = GET_FROM_C_STRING;
5938 else if (STRINGP (it->string))
5939 it->method = GET_FROM_STRING;
5940 else
5941 {
5942 it->method = GET_FROM_BUFFER;
5943 it->object = it->w->contents;
5944 }
5945 }
5946 it->end_charpos = p->end_charpos;
5947 it->string_nchars = p->string_nchars;
5948 it->area = p->area;
5949 it->multibyte_p = p->multibyte_p;
5950 it->avoid_cursor_p = p->avoid_cursor_p;
5951 it->space_width = p->space_width;
5952 it->font_height = p->font_height;
5953 it->voffset = p->voffset;
5954 it->string_from_display_prop_p = p->string_from_display_prop_p;
5955 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5956 it->line_wrap = p->line_wrap;
5957 it->bidi_p = p->bidi_p;
5958 it->paragraph_embedding = p->paragraph_embedding;
5959 it->from_disp_prop_p = p->from_disp_prop_p;
5960 if (it->bidi_p)
5961 {
5962 bidi_pop_it (&it->bidi_it);
5963 /* Bidi-iterate until we get out of the portion of text, if any,
5964 covered by a `display' text property or by an overlay with
5965 `display' property. (We cannot just jump there, because the
5966 internal coherency of the bidi iterator state can not be
5967 preserved across such jumps.) We also must determine the
5968 paragraph base direction if the overlay we just processed is
5969 at the beginning of a new paragraph. */
5970 if (from_display_prop
5971 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5972 iterate_out_of_display_property (it);
5973
5974 eassert ((BUFFERP (it->object)
5975 && IT_CHARPOS (*it) == it->bidi_it.charpos
5976 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5977 || (STRINGP (it->object)
5978 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5979 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5980 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5981 }
5982 }
5983
5984
5985 \f
5986 /***********************************************************************
5987 Moving over lines
5988 ***********************************************************************/
5989
5990 /* Set IT's current position to the previous line start. */
5991
5992 static void
5993 back_to_previous_line_start (struct it *it)
5994 {
5995 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5996
5997 DEC_BOTH (cp, bp);
5998 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5999 }
6000
6001
6002 /* Move IT to the next line start.
6003
6004 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6005 we skipped over part of the text (as opposed to moving the iterator
6006 continuously over the text). Otherwise, don't change the value
6007 of *SKIPPED_P.
6008
6009 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6010 iterator on the newline, if it was found.
6011
6012 Newlines may come from buffer text, overlay strings, or strings
6013 displayed via the `display' property. That's the reason we can't
6014 simply use find_newline_no_quit.
6015
6016 Note that this function may not skip over invisible text that is so
6017 because of text properties and immediately follows a newline. If
6018 it would, function reseat_at_next_visible_line_start, when called
6019 from set_iterator_to_next, would effectively make invisible
6020 characters following a newline part of the wrong glyph row, which
6021 leads to wrong cursor motion. */
6022
6023 static int
6024 forward_to_next_line_start (struct it *it, int *skipped_p,
6025 struct bidi_it *bidi_it_prev)
6026 {
6027 ptrdiff_t old_selective;
6028 int newline_found_p, n;
6029 const int MAX_NEWLINE_DISTANCE = 500;
6030
6031 /* If already on a newline, just consume it to avoid unintended
6032 skipping over invisible text below. */
6033 if (it->what == IT_CHARACTER
6034 && it->c == '\n'
6035 && CHARPOS (it->position) == IT_CHARPOS (*it))
6036 {
6037 if (it->bidi_p && bidi_it_prev)
6038 *bidi_it_prev = it->bidi_it;
6039 set_iterator_to_next (it, 0);
6040 it->c = 0;
6041 return 1;
6042 }
6043
6044 /* Don't handle selective display in the following. It's (a)
6045 unnecessary because it's done by the caller, and (b) leads to an
6046 infinite recursion because next_element_from_ellipsis indirectly
6047 calls this function. */
6048 old_selective = it->selective;
6049 it->selective = 0;
6050
6051 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6052 from buffer text. */
6053 for (n = newline_found_p = 0;
6054 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6055 n += STRINGP (it->string) ? 0 : 1)
6056 {
6057 if (!get_next_display_element (it))
6058 return 0;
6059 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6060 if (newline_found_p && it->bidi_p && bidi_it_prev)
6061 *bidi_it_prev = it->bidi_it;
6062 set_iterator_to_next (it, 0);
6063 }
6064
6065 /* If we didn't find a newline near enough, see if we can use a
6066 short-cut. */
6067 if (!newline_found_p)
6068 {
6069 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6070 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6071 1, &bytepos);
6072 Lisp_Object pos;
6073
6074 eassert (!STRINGP (it->string));
6075
6076 /* If there isn't any `display' property in sight, and no
6077 overlays, we can just use the position of the newline in
6078 buffer text. */
6079 if (it->stop_charpos >= limit
6080 || ((pos = Fnext_single_property_change (make_number (start),
6081 Qdisplay, Qnil,
6082 make_number (limit)),
6083 NILP (pos))
6084 && next_overlay_change (start) == ZV))
6085 {
6086 if (!it->bidi_p)
6087 {
6088 IT_CHARPOS (*it) = limit;
6089 IT_BYTEPOS (*it) = bytepos;
6090 }
6091 else
6092 {
6093 struct bidi_it bprev;
6094
6095 /* Help bidi.c avoid expensive searches for display
6096 properties and overlays, by telling it that there are
6097 none up to `limit'. */
6098 if (it->bidi_it.disp_pos < limit)
6099 {
6100 it->bidi_it.disp_pos = limit;
6101 it->bidi_it.disp_prop = 0;
6102 }
6103 do {
6104 bprev = it->bidi_it;
6105 bidi_move_to_visually_next (&it->bidi_it);
6106 } while (it->bidi_it.charpos != limit);
6107 IT_CHARPOS (*it) = limit;
6108 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6109 if (bidi_it_prev)
6110 *bidi_it_prev = bprev;
6111 }
6112 *skipped_p = newline_found_p = 1;
6113 }
6114 else
6115 {
6116 while (get_next_display_element (it)
6117 && !newline_found_p)
6118 {
6119 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6120 if (newline_found_p && it->bidi_p && bidi_it_prev)
6121 *bidi_it_prev = it->bidi_it;
6122 set_iterator_to_next (it, 0);
6123 }
6124 }
6125 }
6126
6127 it->selective = old_selective;
6128 return newline_found_p;
6129 }
6130
6131
6132 /* Set IT's current position to the previous visible line start. Skip
6133 invisible text that is so either due to text properties or due to
6134 selective display. Caution: this does not change IT->current_x and
6135 IT->hpos. */
6136
6137 static void
6138 back_to_previous_visible_line_start (struct it *it)
6139 {
6140 while (IT_CHARPOS (*it) > BEGV)
6141 {
6142 back_to_previous_line_start (it);
6143
6144 if (IT_CHARPOS (*it) <= BEGV)
6145 break;
6146
6147 /* If selective > 0, then lines indented more than its value are
6148 invisible. */
6149 if (it->selective > 0
6150 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6151 it->selective))
6152 continue;
6153
6154 /* Check the newline before point for invisibility. */
6155 {
6156 Lisp_Object prop;
6157 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6158 Qinvisible, it->window);
6159 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6160 continue;
6161 }
6162
6163 if (IT_CHARPOS (*it) <= BEGV)
6164 break;
6165
6166 {
6167 struct it it2;
6168 void *it2data = NULL;
6169 ptrdiff_t pos;
6170 ptrdiff_t beg, end;
6171 Lisp_Object val, overlay;
6172
6173 SAVE_IT (it2, *it, it2data);
6174
6175 /* If newline is part of a composition, continue from start of composition */
6176 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6177 && beg < IT_CHARPOS (*it))
6178 goto replaced;
6179
6180 /* If newline is replaced by a display property, find start of overlay
6181 or interval and continue search from that point. */
6182 pos = --IT_CHARPOS (it2);
6183 --IT_BYTEPOS (it2);
6184 it2.sp = 0;
6185 bidi_unshelve_cache (NULL, 0);
6186 it2.string_from_display_prop_p = 0;
6187 it2.from_disp_prop_p = 0;
6188 if (handle_display_prop (&it2) == HANDLED_RETURN
6189 && !NILP (val = get_char_property_and_overlay
6190 (make_number (pos), Qdisplay, Qnil, &overlay))
6191 && (OVERLAYP (overlay)
6192 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6193 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6194 {
6195 RESTORE_IT (it, it, it2data);
6196 goto replaced;
6197 }
6198
6199 /* Newline is not replaced by anything -- so we are done. */
6200 RESTORE_IT (it, it, it2data);
6201 break;
6202
6203 replaced:
6204 if (beg < BEGV)
6205 beg = BEGV;
6206 IT_CHARPOS (*it) = beg;
6207 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6208 }
6209 }
6210
6211 it->continuation_lines_width = 0;
6212
6213 eassert (IT_CHARPOS (*it) >= BEGV);
6214 eassert (IT_CHARPOS (*it) == BEGV
6215 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6216 CHECK_IT (it);
6217 }
6218
6219
6220 /* Reseat iterator IT at the previous visible line start. Skip
6221 invisible text that is so either due to text properties or due to
6222 selective display. At the end, update IT's overlay information,
6223 face information etc. */
6224
6225 void
6226 reseat_at_previous_visible_line_start (struct it *it)
6227 {
6228 back_to_previous_visible_line_start (it);
6229 reseat (it, it->current.pos, 1);
6230 CHECK_IT (it);
6231 }
6232
6233
6234 /* Reseat iterator IT on the next visible line start in the current
6235 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6236 preceding the line start. Skip over invisible text that is so
6237 because of selective display. Compute faces, overlays etc at the
6238 new position. Note that this function does not skip over text that
6239 is invisible because of text properties. */
6240
6241 static void
6242 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6243 {
6244 int newline_found_p, skipped_p = 0;
6245 struct bidi_it bidi_it_prev;
6246
6247 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6248
6249 /* Skip over lines that are invisible because they are indented
6250 more than the value of IT->selective. */
6251 if (it->selective > 0)
6252 while (IT_CHARPOS (*it) < ZV
6253 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6254 it->selective))
6255 {
6256 eassert (IT_BYTEPOS (*it) == BEGV
6257 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6258 newline_found_p =
6259 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6260 }
6261
6262 /* Position on the newline if that's what's requested. */
6263 if (on_newline_p && newline_found_p)
6264 {
6265 if (STRINGP (it->string))
6266 {
6267 if (IT_STRING_CHARPOS (*it) > 0)
6268 {
6269 if (!it->bidi_p)
6270 {
6271 --IT_STRING_CHARPOS (*it);
6272 --IT_STRING_BYTEPOS (*it);
6273 }
6274 else
6275 {
6276 /* We need to restore the bidi iterator to the state
6277 it had on the newline, and resync the IT's
6278 position with that. */
6279 it->bidi_it = bidi_it_prev;
6280 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6281 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6282 }
6283 }
6284 }
6285 else if (IT_CHARPOS (*it) > BEGV)
6286 {
6287 if (!it->bidi_p)
6288 {
6289 --IT_CHARPOS (*it);
6290 --IT_BYTEPOS (*it);
6291 }
6292 else
6293 {
6294 /* We need to restore the bidi iterator to the state it
6295 had on the newline and resync IT with that. */
6296 it->bidi_it = bidi_it_prev;
6297 IT_CHARPOS (*it) = it->bidi_it.charpos;
6298 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6299 }
6300 reseat (it, it->current.pos, 0);
6301 }
6302 }
6303 else if (skipped_p)
6304 reseat (it, it->current.pos, 0);
6305
6306 CHECK_IT (it);
6307 }
6308
6309
6310 \f
6311 /***********************************************************************
6312 Changing an iterator's position
6313 ***********************************************************************/
6314
6315 /* Change IT's current position to POS in current_buffer. If FORCE_P
6316 is non-zero, always check for text properties at the new position.
6317 Otherwise, text properties are only looked up if POS >=
6318 IT->check_charpos of a property. */
6319
6320 static void
6321 reseat (struct it *it, struct text_pos pos, int force_p)
6322 {
6323 ptrdiff_t original_pos = IT_CHARPOS (*it);
6324
6325 reseat_1 (it, pos, 0);
6326
6327 /* Determine where to check text properties. Avoid doing it
6328 where possible because text property lookup is very expensive. */
6329 if (force_p
6330 || CHARPOS (pos) > it->stop_charpos
6331 || CHARPOS (pos) < original_pos)
6332 {
6333 if (it->bidi_p)
6334 {
6335 /* For bidi iteration, we need to prime prev_stop and
6336 base_level_stop with our best estimations. */
6337 /* Implementation note: Of course, POS is not necessarily a
6338 stop position, so assigning prev_pos to it is a lie; we
6339 should have called compute_stop_backwards. However, if
6340 the current buffer does not include any R2L characters,
6341 that call would be a waste of cycles, because the
6342 iterator will never move back, and thus never cross this
6343 "fake" stop position. So we delay that backward search
6344 until the time we really need it, in next_element_from_buffer. */
6345 if (CHARPOS (pos) != it->prev_stop)
6346 it->prev_stop = CHARPOS (pos);
6347 if (CHARPOS (pos) < it->base_level_stop)
6348 it->base_level_stop = 0; /* meaning it's unknown */
6349 handle_stop (it);
6350 }
6351 else
6352 {
6353 handle_stop (it);
6354 it->prev_stop = it->base_level_stop = 0;
6355 }
6356
6357 }
6358
6359 CHECK_IT (it);
6360 }
6361
6362
6363 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6364 IT->stop_pos to POS, also. */
6365
6366 static void
6367 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6368 {
6369 /* Don't call this function when scanning a C string. */
6370 eassert (it->s == NULL);
6371
6372 /* POS must be a reasonable value. */
6373 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6374
6375 it->current.pos = it->position = pos;
6376 it->end_charpos = ZV;
6377 it->dpvec = NULL;
6378 it->current.dpvec_index = -1;
6379 it->current.overlay_string_index = -1;
6380 IT_STRING_CHARPOS (*it) = -1;
6381 IT_STRING_BYTEPOS (*it) = -1;
6382 it->string = Qnil;
6383 it->method = GET_FROM_BUFFER;
6384 it->object = it->w->contents;
6385 it->area = TEXT_AREA;
6386 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6387 it->sp = 0;
6388 it->string_from_display_prop_p = 0;
6389 it->string_from_prefix_prop_p = 0;
6390
6391 it->from_disp_prop_p = 0;
6392 it->face_before_selective_p = 0;
6393 if (it->bidi_p)
6394 {
6395 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6396 &it->bidi_it);
6397 bidi_unshelve_cache (NULL, 0);
6398 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6399 it->bidi_it.string.s = NULL;
6400 it->bidi_it.string.lstring = Qnil;
6401 it->bidi_it.string.bufpos = 0;
6402 it->bidi_it.string.unibyte = 0;
6403 it->bidi_it.w = it->w;
6404 }
6405
6406 if (set_stop_p)
6407 {
6408 it->stop_charpos = CHARPOS (pos);
6409 it->base_level_stop = CHARPOS (pos);
6410 }
6411 /* This make the information stored in it->cmp_it invalidate. */
6412 it->cmp_it.id = -1;
6413 }
6414
6415
6416 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6417 If S is non-null, it is a C string to iterate over. Otherwise,
6418 STRING gives a Lisp string to iterate over.
6419
6420 If PRECISION > 0, don't return more then PRECISION number of
6421 characters from the string.
6422
6423 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6424 characters have been returned. FIELD_WIDTH < 0 means an infinite
6425 field width.
6426
6427 MULTIBYTE = 0 means disable processing of multibyte characters,
6428 MULTIBYTE > 0 means enable it,
6429 MULTIBYTE < 0 means use IT->multibyte_p.
6430
6431 IT must be initialized via a prior call to init_iterator before
6432 calling this function. */
6433
6434 static void
6435 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6436 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6437 int multibyte)
6438 {
6439 /* No text property checks performed by default, but see below. */
6440 it->stop_charpos = -1;
6441
6442 /* Set iterator position and end position. */
6443 memset (&it->current, 0, sizeof it->current);
6444 it->current.overlay_string_index = -1;
6445 it->current.dpvec_index = -1;
6446 eassert (charpos >= 0);
6447
6448 /* If STRING is specified, use its multibyteness, otherwise use the
6449 setting of MULTIBYTE, if specified. */
6450 if (multibyte >= 0)
6451 it->multibyte_p = multibyte > 0;
6452
6453 /* Bidirectional reordering of strings is controlled by the default
6454 value of bidi-display-reordering. Don't try to reorder while
6455 loading loadup.el, as the necessary character property tables are
6456 not yet available. */
6457 it->bidi_p =
6458 NILP (Vpurify_flag)
6459 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6460
6461 if (s == NULL)
6462 {
6463 eassert (STRINGP (string));
6464 it->string = string;
6465 it->s = NULL;
6466 it->end_charpos = it->string_nchars = SCHARS (string);
6467 it->method = GET_FROM_STRING;
6468 it->current.string_pos = string_pos (charpos, string);
6469
6470 if (it->bidi_p)
6471 {
6472 it->bidi_it.string.lstring = string;
6473 it->bidi_it.string.s = NULL;
6474 it->bidi_it.string.schars = it->end_charpos;
6475 it->bidi_it.string.bufpos = 0;
6476 it->bidi_it.string.from_disp_str = 0;
6477 it->bidi_it.string.unibyte = !it->multibyte_p;
6478 it->bidi_it.w = it->w;
6479 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6480 FRAME_WINDOW_P (it->f), &it->bidi_it);
6481 }
6482 }
6483 else
6484 {
6485 it->s = (const unsigned char *) s;
6486 it->string = Qnil;
6487
6488 /* Note that we use IT->current.pos, not it->current.string_pos,
6489 for displaying C strings. */
6490 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6491 if (it->multibyte_p)
6492 {
6493 it->current.pos = c_string_pos (charpos, s, 1);
6494 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6495 }
6496 else
6497 {
6498 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6499 it->end_charpos = it->string_nchars = strlen (s);
6500 }
6501
6502 if (it->bidi_p)
6503 {
6504 it->bidi_it.string.lstring = Qnil;
6505 it->bidi_it.string.s = (const unsigned char *) s;
6506 it->bidi_it.string.schars = it->end_charpos;
6507 it->bidi_it.string.bufpos = 0;
6508 it->bidi_it.string.from_disp_str = 0;
6509 it->bidi_it.string.unibyte = !it->multibyte_p;
6510 it->bidi_it.w = it->w;
6511 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6512 &it->bidi_it);
6513 }
6514 it->method = GET_FROM_C_STRING;
6515 }
6516
6517 /* PRECISION > 0 means don't return more than PRECISION characters
6518 from the string. */
6519 if (precision > 0 && it->end_charpos - charpos > precision)
6520 {
6521 it->end_charpos = it->string_nchars = charpos + precision;
6522 if (it->bidi_p)
6523 it->bidi_it.string.schars = it->end_charpos;
6524 }
6525
6526 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6527 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6528 FIELD_WIDTH < 0 means infinite field width. This is useful for
6529 padding with `-' at the end of a mode line. */
6530 if (field_width < 0)
6531 field_width = INFINITY;
6532 /* Implementation note: We deliberately don't enlarge
6533 it->bidi_it.string.schars here to fit it->end_charpos, because
6534 the bidi iterator cannot produce characters out of thin air. */
6535 if (field_width > it->end_charpos - charpos)
6536 it->end_charpos = charpos + field_width;
6537
6538 /* Use the standard display table for displaying strings. */
6539 if (DISP_TABLE_P (Vstandard_display_table))
6540 it->dp = XCHAR_TABLE (Vstandard_display_table);
6541
6542 it->stop_charpos = charpos;
6543 it->prev_stop = charpos;
6544 it->base_level_stop = 0;
6545 if (it->bidi_p)
6546 {
6547 it->bidi_it.first_elt = 1;
6548 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6549 it->bidi_it.disp_pos = -1;
6550 }
6551 if (s == NULL && it->multibyte_p)
6552 {
6553 ptrdiff_t endpos = SCHARS (it->string);
6554 if (endpos > it->end_charpos)
6555 endpos = it->end_charpos;
6556 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6557 it->string);
6558 }
6559 CHECK_IT (it);
6560 }
6561
6562
6563 \f
6564 /***********************************************************************
6565 Iteration
6566 ***********************************************************************/
6567
6568 /* Map enum it_method value to corresponding next_element_from_* function. */
6569
6570 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6571 {
6572 next_element_from_buffer,
6573 next_element_from_display_vector,
6574 next_element_from_string,
6575 next_element_from_c_string,
6576 next_element_from_image,
6577 next_element_from_stretch
6578 };
6579
6580 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6581
6582
6583 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6584 (possibly with the following characters). */
6585
6586 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6587 ((IT)->cmp_it.id >= 0 \
6588 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6589 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6590 END_CHARPOS, (IT)->w, \
6591 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6592 (IT)->string)))
6593
6594
6595 /* Lookup the char-table Vglyphless_char_display for character C (-1
6596 if we want information for no-font case), and return the display
6597 method symbol. By side-effect, update it->what and
6598 it->glyphless_method. This function is called from
6599 get_next_display_element for each character element, and from
6600 x_produce_glyphs when no suitable font was found. */
6601
6602 Lisp_Object
6603 lookup_glyphless_char_display (int c, struct it *it)
6604 {
6605 Lisp_Object glyphless_method = Qnil;
6606
6607 if (CHAR_TABLE_P (Vglyphless_char_display)
6608 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6609 {
6610 if (c >= 0)
6611 {
6612 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6613 if (CONSP (glyphless_method))
6614 glyphless_method = FRAME_WINDOW_P (it->f)
6615 ? XCAR (glyphless_method)
6616 : XCDR (glyphless_method);
6617 }
6618 else
6619 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6620 }
6621
6622 retry:
6623 if (NILP (glyphless_method))
6624 {
6625 if (c >= 0)
6626 /* The default is to display the character by a proper font. */
6627 return Qnil;
6628 /* The default for the no-font case is to display an empty box. */
6629 glyphless_method = Qempty_box;
6630 }
6631 if (EQ (glyphless_method, Qzero_width))
6632 {
6633 if (c >= 0)
6634 return glyphless_method;
6635 /* This method can't be used for the no-font case. */
6636 glyphless_method = Qempty_box;
6637 }
6638 if (EQ (glyphless_method, Qthin_space))
6639 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6640 else if (EQ (glyphless_method, Qempty_box))
6641 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6642 else if (EQ (glyphless_method, Qhex_code))
6643 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6644 else if (STRINGP (glyphless_method))
6645 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6646 else
6647 {
6648 /* Invalid value. We use the default method. */
6649 glyphless_method = Qnil;
6650 goto retry;
6651 }
6652 it->what = IT_GLYPHLESS;
6653 return glyphless_method;
6654 }
6655
6656 /* Merge escape glyph face and cache the result. */
6657
6658 static struct frame *last_escape_glyph_frame = NULL;
6659 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6660 static int last_escape_glyph_merged_face_id = 0;
6661
6662 static int
6663 merge_escape_glyph_face (struct it *it)
6664 {
6665 int face_id;
6666
6667 if (it->f == last_escape_glyph_frame
6668 && it->face_id == last_escape_glyph_face_id)
6669 face_id = last_escape_glyph_merged_face_id;
6670 else
6671 {
6672 /* Merge the `escape-glyph' face into the current face. */
6673 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6674 last_escape_glyph_frame = it->f;
6675 last_escape_glyph_face_id = it->face_id;
6676 last_escape_glyph_merged_face_id = face_id;
6677 }
6678 return face_id;
6679 }
6680
6681 /* Likewise for glyphless glyph face. */
6682
6683 static struct frame *last_glyphless_glyph_frame = NULL;
6684 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6685 static int last_glyphless_glyph_merged_face_id = 0;
6686
6687 int
6688 merge_glyphless_glyph_face (struct it *it)
6689 {
6690 int face_id;
6691
6692 if (it->f == last_glyphless_glyph_frame
6693 && it->face_id == last_glyphless_glyph_face_id)
6694 face_id = last_glyphless_glyph_merged_face_id;
6695 else
6696 {
6697 /* Merge the `glyphless-char' face into the current face. */
6698 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6699 last_glyphless_glyph_frame = it->f;
6700 last_glyphless_glyph_face_id = it->face_id;
6701 last_glyphless_glyph_merged_face_id = face_id;
6702 }
6703 return face_id;
6704 }
6705
6706 /* Load IT's display element fields with information about the next
6707 display element from the current position of IT. Value is zero if
6708 end of buffer (or C string) is reached. */
6709
6710 static int
6711 get_next_display_element (struct it *it)
6712 {
6713 /* Non-zero means that we found a display element. Zero means that
6714 we hit the end of what we iterate over. Performance note: the
6715 function pointer `method' used here turns out to be faster than
6716 using a sequence of if-statements. */
6717 int success_p;
6718
6719 get_next:
6720 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6721
6722 if (it->what == IT_CHARACTER)
6723 {
6724 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6725 and only if (a) the resolved directionality of that character
6726 is R..." */
6727 /* FIXME: Do we need an exception for characters from display
6728 tables? */
6729 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6730 it->c = bidi_mirror_char (it->c);
6731 /* Map via display table or translate control characters.
6732 IT->c, IT->len etc. have been set to the next character by
6733 the function call above. If we have a display table, and it
6734 contains an entry for IT->c, translate it. Don't do this if
6735 IT->c itself comes from a display table, otherwise we could
6736 end up in an infinite recursion. (An alternative could be to
6737 count the recursion depth of this function and signal an
6738 error when a certain maximum depth is reached.) Is it worth
6739 it? */
6740 if (success_p && it->dpvec == NULL)
6741 {
6742 Lisp_Object dv;
6743 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6744 int nonascii_space_p = 0;
6745 int nonascii_hyphen_p = 0;
6746 int c = it->c; /* This is the character to display. */
6747
6748 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6749 {
6750 eassert (SINGLE_BYTE_CHAR_P (c));
6751 if (unibyte_display_via_language_environment)
6752 {
6753 c = DECODE_CHAR (unibyte, c);
6754 if (c < 0)
6755 c = BYTE8_TO_CHAR (it->c);
6756 }
6757 else
6758 c = BYTE8_TO_CHAR (it->c);
6759 }
6760
6761 if (it->dp
6762 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6763 VECTORP (dv)))
6764 {
6765 struct Lisp_Vector *v = XVECTOR (dv);
6766
6767 /* Return the first character from the display table
6768 entry, if not empty. If empty, don't display the
6769 current character. */
6770 if (v->header.size)
6771 {
6772 it->dpvec_char_len = it->len;
6773 it->dpvec = v->contents;
6774 it->dpend = v->contents + v->header.size;
6775 it->current.dpvec_index = 0;
6776 it->dpvec_face_id = -1;
6777 it->saved_face_id = it->face_id;
6778 it->method = GET_FROM_DISPLAY_VECTOR;
6779 it->ellipsis_p = 0;
6780 }
6781 else
6782 {
6783 set_iterator_to_next (it, 0);
6784 }
6785 goto get_next;
6786 }
6787
6788 if (! NILP (lookup_glyphless_char_display (c, it)))
6789 {
6790 if (it->what == IT_GLYPHLESS)
6791 goto done;
6792 /* Don't display this character. */
6793 set_iterator_to_next (it, 0);
6794 goto get_next;
6795 }
6796
6797 /* If `nobreak-char-display' is non-nil, we display
6798 non-ASCII spaces and hyphens specially. */
6799 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6800 {
6801 if (c == 0xA0)
6802 nonascii_space_p = 1;
6803 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6804 nonascii_hyphen_p = 1;
6805 }
6806
6807 /* Translate control characters into `\003' or `^C' form.
6808 Control characters coming from a display table entry are
6809 currently not translated because we use IT->dpvec to hold
6810 the translation. This could easily be changed but I
6811 don't believe that it is worth doing.
6812
6813 The characters handled by `nobreak-char-display' must be
6814 translated too.
6815
6816 Non-printable characters and raw-byte characters are also
6817 translated to octal form. */
6818 if (((c < ' ' || c == 127) /* ASCII control chars. */
6819 ? (it->area != TEXT_AREA
6820 /* In mode line, treat \n, \t like other crl chars. */
6821 || (c != '\t'
6822 && it->glyph_row
6823 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6824 || (c != '\n' && c != '\t'))
6825 : (nonascii_space_p
6826 || nonascii_hyphen_p
6827 || CHAR_BYTE8_P (c)
6828 || ! CHAR_PRINTABLE_P (c))))
6829 {
6830 /* C is a control character, non-ASCII space/hyphen,
6831 raw-byte, or a non-printable character which must be
6832 displayed either as '\003' or as `^C' where the '\\'
6833 and '^' can be defined in the display table. Fill
6834 IT->ctl_chars with glyphs for what we have to
6835 display. Then, set IT->dpvec to these glyphs. */
6836 Lisp_Object gc;
6837 int ctl_len;
6838 int face_id;
6839 int lface_id = 0;
6840 int escape_glyph;
6841
6842 /* Handle control characters with ^. */
6843
6844 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6845 {
6846 int g;
6847
6848 g = '^'; /* default glyph for Control */
6849 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6850 if (it->dp
6851 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6852 {
6853 g = GLYPH_CODE_CHAR (gc);
6854 lface_id = GLYPH_CODE_FACE (gc);
6855 }
6856
6857 face_id = (lface_id
6858 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6859 : merge_escape_glyph_face (it));
6860
6861 XSETINT (it->ctl_chars[0], g);
6862 XSETINT (it->ctl_chars[1], c ^ 0100);
6863 ctl_len = 2;
6864 goto display_control;
6865 }
6866
6867 /* Handle non-ascii space in the mode where it only gets
6868 highlighting. */
6869
6870 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6871 {
6872 /* Merge `nobreak-space' into the current face. */
6873 face_id = merge_faces (it->f, Qnobreak_space, 0,
6874 it->face_id);
6875 XSETINT (it->ctl_chars[0], ' ');
6876 ctl_len = 1;
6877 goto display_control;
6878 }
6879
6880 /* Handle sequences that start with the "escape glyph". */
6881
6882 /* the default escape glyph is \. */
6883 escape_glyph = '\\';
6884
6885 if (it->dp
6886 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6887 {
6888 escape_glyph = GLYPH_CODE_CHAR (gc);
6889 lface_id = GLYPH_CODE_FACE (gc);
6890 }
6891
6892 face_id = (lface_id
6893 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6894 : merge_escape_glyph_face (it));
6895
6896 /* Draw non-ASCII hyphen with just highlighting: */
6897
6898 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6899 {
6900 XSETINT (it->ctl_chars[0], '-');
6901 ctl_len = 1;
6902 goto display_control;
6903 }
6904
6905 /* Draw non-ASCII space/hyphen with escape glyph: */
6906
6907 if (nonascii_space_p || nonascii_hyphen_p)
6908 {
6909 XSETINT (it->ctl_chars[0], escape_glyph);
6910 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6911 ctl_len = 2;
6912 goto display_control;
6913 }
6914
6915 {
6916 char str[10];
6917 int len, i;
6918
6919 if (CHAR_BYTE8_P (c))
6920 /* Display \200 instead of \17777600. */
6921 c = CHAR_TO_BYTE8 (c);
6922 len = sprintf (str, "%03o", c);
6923
6924 XSETINT (it->ctl_chars[0], escape_glyph);
6925 for (i = 0; i < len; i++)
6926 XSETINT (it->ctl_chars[i + 1], str[i]);
6927 ctl_len = len + 1;
6928 }
6929
6930 display_control:
6931 /* Set up IT->dpvec and return first character from it. */
6932 it->dpvec_char_len = it->len;
6933 it->dpvec = it->ctl_chars;
6934 it->dpend = it->dpvec + ctl_len;
6935 it->current.dpvec_index = 0;
6936 it->dpvec_face_id = face_id;
6937 it->saved_face_id = it->face_id;
6938 it->method = GET_FROM_DISPLAY_VECTOR;
6939 it->ellipsis_p = 0;
6940 goto get_next;
6941 }
6942 it->char_to_display = c;
6943 }
6944 else if (success_p)
6945 {
6946 it->char_to_display = it->c;
6947 }
6948 }
6949
6950 #ifdef HAVE_WINDOW_SYSTEM
6951 /* Adjust face id for a multibyte character. There are no multibyte
6952 character in unibyte text. */
6953 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6954 && it->multibyte_p
6955 && success_p
6956 && FRAME_WINDOW_P (it->f))
6957 {
6958 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6959
6960 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6961 {
6962 /* Automatic composition with glyph-string. */
6963 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6964
6965 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6966 }
6967 else
6968 {
6969 ptrdiff_t pos = (it->s ? -1
6970 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6971 : IT_CHARPOS (*it));
6972 int c;
6973
6974 if (it->what == IT_CHARACTER)
6975 c = it->char_to_display;
6976 else
6977 {
6978 struct composition *cmp = composition_table[it->cmp_it.id];
6979 int i;
6980
6981 c = ' ';
6982 for (i = 0; i < cmp->glyph_len; i++)
6983 /* TAB in a composition means display glyphs with
6984 padding space on the left or right. */
6985 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6986 break;
6987 }
6988 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6989 }
6990 }
6991 #endif /* HAVE_WINDOW_SYSTEM */
6992
6993 done:
6994 /* Is this character the last one of a run of characters with
6995 box? If yes, set IT->end_of_box_run_p to 1. */
6996 if (it->face_box_p
6997 && it->s == NULL)
6998 {
6999 if (it->method == GET_FROM_STRING && it->sp)
7000 {
7001 int face_id = underlying_face_id (it);
7002 struct face *face = FACE_FROM_ID (it->f, face_id);
7003
7004 if (face)
7005 {
7006 if (face->box == FACE_NO_BOX)
7007 {
7008 /* If the box comes from face properties in a
7009 display string, check faces in that string. */
7010 int string_face_id = face_after_it_pos (it);
7011 it->end_of_box_run_p
7012 = (FACE_FROM_ID (it->f, string_face_id)->box
7013 == FACE_NO_BOX);
7014 }
7015 /* Otherwise, the box comes from the underlying face.
7016 If this is the last string character displayed, check
7017 the next buffer location. */
7018 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7019 && (it->current.overlay_string_index
7020 == it->n_overlay_strings - 1))
7021 {
7022 ptrdiff_t ignore;
7023 int next_face_id;
7024 struct text_pos pos = it->current.pos;
7025 INC_TEXT_POS (pos, it->multibyte_p);
7026
7027 next_face_id = face_at_buffer_position
7028 (it->w, CHARPOS (pos), &ignore,
7029 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7030 -1);
7031 it->end_of_box_run_p
7032 = (FACE_FROM_ID (it->f, next_face_id)->box
7033 == FACE_NO_BOX);
7034 }
7035 }
7036 }
7037 /* next_element_from_display_vector sets this flag according to
7038 faces of the display vector glyphs, see there. */
7039 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7040 {
7041 int face_id = face_after_it_pos (it);
7042 it->end_of_box_run_p
7043 = (face_id != it->face_id
7044 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7045 }
7046 }
7047 /* If we reached the end of the object we've been iterating (e.g., a
7048 display string or an overlay string), and there's something on
7049 IT->stack, proceed with what's on the stack. It doesn't make
7050 sense to return zero if there's unprocessed stuff on the stack,
7051 because otherwise that stuff will never be displayed. */
7052 if (!success_p && it->sp > 0)
7053 {
7054 set_iterator_to_next (it, 0);
7055 success_p = get_next_display_element (it);
7056 }
7057
7058 /* Value is 0 if end of buffer or string reached. */
7059 return success_p;
7060 }
7061
7062
7063 /* Move IT to the next display element.
7064
7065 RESEAT_P non-zero means if called on a newline in buffer text,
7066 skip to the next visible line start.
7067
7068 Functions get_next_display_element and set_iterator_to_next are
7069 separate because I find this arrangement easier to handle than a
7070 get_next_display_element function that also increments IT's
7071 position. The way it is we can first look at an iterator's current
7072 display element, decide whether it fits on a line, and if it does,
7073 increment the iterator position. The other way around we probably
7074 would either need a flag indicating whether the iterator has to be
7075 incremented the next time, or we would have to implement a
7076 decrement position function which would not be easy to write. */
7077
7078 void
7079 set_iterator_to_next (struct it *it, int reseat_p)
7080 {
7081 /* Reset flags indicating start and end of a sequence of characters
7082 with box. Reset them at the start of this function because
7083 moving the iterator to a new position might set them. */
7084 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7085
7086 switch (it->method)
7087 {
7088 case GET_FROM_BUFFER:
7089 /* The current display element of IT is a character from
7090 current_buffer. Advance in the buffer, and maybe skip over
7091 invisible lines that are so because of selective display. */
7092 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7093 reseat_at_next_visible_line_start (it, 0);
7094 else if (it->cmp_it.id >= 0)
7095 {
7096 /* We are currently getting glyphs from a composition. */
7097 int i;
7098
7099 if (! it->bidi_p)
7100 {
7101 IT_CHARPOS (*it) += it->cmp_it.nchars;
7102 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7103 if (it->cmp_it.to < it->cmp_it.nglyphs)
7104 {
7105 it->cmp_it.from = it->cmp_it.to;
7106 }
7107 else
7108 {
7109 it->cmp_it.id = -1;
7110 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7111 IT_BYTEPOS (*it),
7112 it->end_charpos, Qnil);
7113 }
7114 }
7115 else if (! it->cmp_it.reversed_p)
7116 {
7117 /* Composition created while scanning forward. */
7118 /* Update IT's char/byte positions to point to the first
7119 character of the next grapheme cluster, or to the
7120 character visually after the current composition. */
7121 for (i = 0; i < it->cmp_it.nchars; i++)
7122 bidi_move_to_visually_next (&it->bidi_it);
7123 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7124 IT_CHARPOS (*it) = it->bidi_it.charpos;
7125
7126 if (it->cmp_it.to < it->cmp_it.nglyphs)
7127 {
7128 /* Proceed to the next grapheme cluster. */
7129 it->cmp_it.from = it->cmp_it.to;
7130 }
7131 else
7132 {
7133 /* No more grapheme clusters in this composition.
7134 Find the next stop position. */
7135 ptrdiff_t stop = it->end_charpos;
7136 if (it->bidi_it.scan_dir < 0)
7137 /* Now we are scanning backward and don't know
7138 where to stop. */
7139 stop = -1;
7140 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7141 IT_BYTEPOS (*it), stop, Qnil);
7142 }
7143 }
7144 else
7145 {
7146 /* Composition created while scanning backward. */
7147 /* Update IT's char/byte positions to point to the last
7148 character of the previous grapheme cluster, or the
7149 character visually after the current composition. */
7150 for (i = 0; i < it->cmp_it.nchars; i++)
7151 bidi_move_to_visually_next (&it->bidi_it);
7152 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7153 IT_CHARPOS (*it) = it->bidi_it.charpos;
7154 if (it->cmp_it.from > 0)
7155 {
7156 /* Proceed to the previous grapheme cluster. */
7157 it->cmp_it.to = it->cmp_it.from;
7158 }
7159 else
7160 {
7161 /* No more grapheme clusters in this composition.
7162 Find the next stop position. */
7163 ptrdiff_t stop = it->end_charpos;
7164 if (it->bidi_it.scan_dir < 0)
7165 /* Now we are scanning backward and don't know
7166 where to stop. */
7167 stop = -1;
7168 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7169 IT_BYTEPOS (*it), stop, Qnil);
7170 }
7171 }
7172 }
7173 else
7174 {
7175 eassert (it->len != 0);
7176
7177 if (!it->bidi_p)
7178 {
7179 IT_BYTEPOS (*it) += it->len;
7180 IT_CHARPOS (*it) += 1;
7181 }
7182 else
7183 {
7184 int prev_scan_dir = it->bidi_it.scan_dir;
7185 /* If this is a new paragraph, determine its base
7186 direction (a.k.a. its base embedding level). */
7187 if (it->bidi_it.new_paragraph)
7188 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7189 bidi_move_to_visually_next (&it->bidi_it);
7190 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7191 IT_CHARPOS (*it) = it->bidi_it.charpos;
7192 if (prev_scan_dir != it->bidi_it.scan_dir)
7193 {
7194 /* As the scan direction was changed, we must
7195 re-compute the stop position for composition. */
7196 ptrdiff_t stop = it->end_charpos;
7197 if (it->bidi_it.scan_dir < 0)
7198 stop = -1;
7199 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7200 IT_BYTEPOS (*it), stop, Qnil);
7201 }
7202 }
7203 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7204 }
7205 break;
7206
7207 case GET_FROM_C_STRING:
7208 /* Current display element of IT is from a C string. */
7209 if (!it->bidi_p
7210 /* If the string position is beyond string's end, it means
7211 next_element_from_c_string is padding the string with
7212 blanks, in which case we bypass the bidi iterator,
7213 because it cannot deal with such virtual characters. */
7214 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7215 {
7216 IT_BYTEPOS (*it) += it->len;
7217 IT_CHARPOS (*it) += 1;
7218 }
7219 else
7220 {
7221 bidi_move_to_visually_next (&it->bidi_it);
7222 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7223 IT_CHARPOS (*it) = it->bidi_it.charpos;
7224 }
7225 break;
7226
7227 case GET_FROM_DISPLAY_VECTOR:
7228 /* Current display element of IT is from a display table entry.
7229 Advance in the display table definition. Reset it to null if
7230 end reached, and continue with characters from buffers/
7231 strings. */
7232 ++it->current.dpvec_index;
7233
7234 /* Restore face of the iterator to what they were before the
7235 display vector entry (these entries may contain faces). */
7236 it->face_id = it->saved_face_id;
7237
7238 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7239 {
7240 int recheck_faces = it->ellipsis_p;
7241
7242 if (it->s)
7243 it->method = GET_FROM_C_STRING;
7244 else if (STRINGP (it->string))
7245 it->method = GET_FROM_STRING;
7246 else
7247 {
7248 it->method = GET_FROM_BUFFER;
7249 it->object = it->w->contents;
7250 }
7251
7252 it->dpvec = NULL;
7253 it->current.dpvec_index = -1;
7254
7255 /* Skip over characters which were displayed via IT->dpvec. */
7256 if (it->dpvec_char_len < 0)
7257 reseat_at_next_visible_line_start (it, 1);
7258 else if (it->dpvec_char_len > 0)
7259 {
7260 if (it->method == GET_FROM_STRING
7261 && it->current.overlay_string_index >= 0
7262 && it->n_overlay_strings > 0)
7263 it->ignore_overlay_strings_at_pos_p = 1;
7264 it->len = it->dpvec_char_len;
7265 set_iterator_to_next (it, reseat_p);
7266 }
7267
7268 /* Maybe recheck faces after display vector */
7269 if (recheck_faces)
7270 it->stop_charpos = IT_CHARPOS (*it);
7271 }
7272 break;
7273
7274 case GET_FROM_STRING:
7275 /* Current display element is a character from a Lisp string. */
7276 eassert (it->s == NULL && STRINGP (it->string));
7277 /* Don't advance past string end. These conditions are true
7278 when set_iterator_to_next is called at the end of
7279 get_next_display_element, in which case the Lisp string is
7280 already exhausted, and all we want is pop the iterator
7281 stack. */
7282 if (it->current.overlay_string_index >= 0)
7283 {
7284 /* This is an overlay string, so there's no padding with
7285 spaces, and the number of characters in the string is
7286 where the string ends. */
7287 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7288 goto consider_string_end;
7289 }
7290 else
7291 {
7292 /* Not an overlay string. There could be padding, so test
7293 against it->end_charpos . */
7294 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7295 goto consider_string_end;
7296 }
7297 if (it->cmp_it.id >= 0)
7298 {
7299 int i;
7300
7301 if (! it->bidi_p)
7302 {
7303 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7304 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7305 if (it->cmp_it.to < it->cmp_it.nglyphs)
7306 it->cmp_it.from = it->cmp_it.to;
7307 else
7308 {
7309 it->cmp_it.id = -1;
7310 composition_compute_stop_pos (&it->cmp_it,
7311 IT_STRING_CHARPOS (*it),
7312 IT_STRING_BYTEPOS (*it),
7313 it->end_charpos, it->string);
7314 }
7315 }
7316 else if (! it->cmp_it.reversed_p)
7317 {
7318 for (i = 0; i < it->cmp_it.nchars; i++)
7319 bidi_move_to_visually_next (&it->bidi_it);
7320 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7321 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7322
7323 if (it->cmp_it.to < it->cmp_it.nglyphs)
7324 it->cmp_it.from = it->cmp_it.to;
7325 else
7326 {
7327 ptrdiff_t stop = it->end_charpos;
7328 if (it->bidi_it.scan_dir < 0)
7329 stop = -1;
7330 composition_compute_stop_pos (&it->cmp_it,
7331 IT_STRING_CHARPOS (*it),
7332 IT_STRING_BYTEPOS (*it), stop,
7333 it->string);
7334 }
7335 }
7336 else
7337 {
7338 for (i = 0; i < it->cmp_it.nchars; i++)
7339 bidi_move_to_visually_next (&it->bidi_it);
7340 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7341 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7342 if (it->cmp_it.from > 0)
7343 it->cmp_it.to = it->cmp_it.from;
7344 else
7345 {
7346 ptrdiff_t stop = it->end_charpos;
7347 if (it->bidi_it.scan_dir < 0)
7348 stop = -1;
7349 composition_compute_stop_pos (&it->cmp_it,
7350 IT_STRING_CHARPOS (*it),
7351 IT_STRING_BYTEPOS (*it), stop,
7352 it->string);
7353 }
7354 }
7355 }
7356 else
7357 {
7358 if (!it->bidi_p
7359 /* If the string position is beyond string's end, it
7360 means next_element_from_string is padding the string
7361 with blanks, in which case we bypass the bidi
7362 iterator, because it cannot deal with such virtual
7363 characters. */
7364 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7365 {
7366 IT_STRING_BYTEPOS (*it) += it->len;
7367 IT_STRING_CHARPOS (*it) += 1;
7368 }
7369 else
7370 {
7371 int prev_scan_dir = it->bidi_it.scan_dir;
7372
7373 bidi_move_to_visually_next (&it->bidi_it);
7374 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7375 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7376 if (prev_scan_dir != it->bidi_it.scan_dir)
7377 {
7378 ptrdiff_t stop = it->end_charpos;
7379
7380 if (it->bidi_it.scan_dir < 0)
7381 stop = -1;
7382 composition_compute_stop_pos (&it->cmp_it,
7383 IT_STRING_CHARPOS (*it),
7384 IT_STRING_BYTEPOS (*it), stop,
7385 it->string);
7386 }
7387 }
7388 }
7389
7390 consider_string_end:
7391
7392 if (it->current.overlay_string_index >= 0)
7393 {
7394 /* IT->string is an overlay string. Advance to the
7395 next, if there is one. */
7396 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7397 {
7398 it->ellipsis_p = 0;
7399 next_overlay_string (it);
7400 if (it->ellipsis_p)
7401 setup_for_ellipsis (it, 0);
7402 }
7403 }
7404 else
7405 {
7406 /* IT->string is not an overlay string. If we reached
7407 its end, and there is something on IT->stack, proceed
7408 with what is on the stack. This can be either another
7409 string, this time an overlay string, or a buffer. */
7410 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7411 && it->sp > 0)
7412 {
7413 pop_it (it);
7414 if (it->method == GET_FROM_STRING)
7415 goto consider_string_end;
7416 }
7417 }
7418 break;
7419
7420 case GET_FROM_IMAGE:
7421 case GET_FROM_STRETCH:
7422 /* The position etc with which we have to proceed are on
7423 the stack. The position may be at the end of a string,
7424 if the `display' property takes up the whole string. */
7425 eassert (it->sp > 0);
7426 pop_it (it);
7427 if (it->method == GET_FROM_STRING)
7428 goto consider_string_end;
7429 break;
7430
7431 default:
7432 /* There are no other methods defined, so this should be a bug. */
7433 emacs_abort ();
7434 }
7435
7436 eassert (it->method != GET_FROM_STRING
7437 || (STRINGP (it->string)
7438 && IT_STRING_CHARPOS (*it) >= 0));
7439 }
7440
7441 /* Load IT's display element fields with information about the next
7442 display element which comes from a display table entry or from the
7443 result of translating a control character to one of the forms `^C'
7444 or `\003'.
7445
7446 IT->dpvec holds the glyphs to return as characters.
7447 IT->saved_face_id holds the face id before the display vector--it
7448 is restored into IT->face_id in set_iterator_to_next. */
7449
7450 static int
7451 next_element_from_display_vector (struct it *it)
7452 {
7453 Lisp_Object gc;
7454 int prev_face_id = it->face_id;
7455 int next_face_id;
7456
7457 /* Precondition. */
7458 eassert (it->dpvec && it->current.dpvec_index >= 0);
7459
7460 it->face_id = it->saved_face_id;
7461
7462 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7463 That seemed totally bogus - so I changed it... */
7464 gc = it->dpvec[it->current.dpvec_index];
7465
7466 if (GLYPH_CODE_P (gc))
7467 {
7468 struct face *this_face, *prev_face, *next_face;
7469
7470 it->c = GLYPH_CODE_CHAR (gc);
7471 it->len = CHAR_BYTES (it->c);
7472
7473 /* The entry may contain a face id to use. Such a face id is
7474 the id of a Lisp face, not a realized face. A face id of
7475 zero means no face is specified. */
7476 if (it->dpvec_face_id >= 0)
7477 it->face_id = it->dpvec_face_id;
7478 else
7479 {
7480 int lface_id = GLYPH_CODE_FACE (gc);
7481 if (lface_id > 0)
7482 it->face_id = merge_faces (it->f, Qt, lface_id,
7483 it->saved_face_id);
7484 }
7485
7486 /* Glyphs in the display vector could have the box face, so we
7487 need to set the related flags in the iterator, as
7488 appropriate. */
7489 this_face = FACE_FROM_ID (it->f, it->face_id);
7490 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7491
7492 /* Is this character the first character of a box-face run? */
7493 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7494 && (!prev_face
7495 || prev_face->box == FACE_NO_BOX));
7496
7497 /* For the last character of the box-face run, we need to look
7498 either at the next glyph from the display vector, or at the
7499 face we saw before the display vector. */
7500 next_face_id = it->saved_face_id;
7501 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7502 {
7503 if (it->dpvec_face_id >= 0)
7504 next_face_id = it->dpvec_face_id;
7505 else
7506 {
7507 int lface_id =
7508 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7509
7510 if (lface_id > 0)
7511 next_face_id = merge_faces (it->f, Qt, lface_id,
7512 it->saved_face_id);
7513 }
7514 }
7515 next_face = FACE_FROM_ID (it->f, next_face_id);
7516 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7517 && (!next_face
7518 || next_face->box == FACE_NO_BOX));
7519 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7520 }
7521 else
7522 /* Display table entry is invalid. Return a space. */
7523 it->c = ' ', it->len = 1;
7524
7525 /* Don't change position and object of the iterator here. They are
7526 still the values of the character that had this display table
7527 entry or was translated, and that's what we want. */
7528 it->what = IT_CHARACTER;
7529 return 1;
7530 }
7531
7532 /* Get the first element of string/buffer in the visual order, after
7533 being reseated to a new position in a string or a buffer. */
7534 static void
7535 get_visually_first_element (struct it *it)
7536 {
7537 int string_p = STRINGP (it->string) || it->s;
7538 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7539 ptrdiff_t bob = (string_p ? 0 : BEGV);
7540
7541 if (STRINGP (it->string))
7542 {
7543 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7544 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7545 }
7546 else
7547 {
7548 it->bidi_it.charpos = IT_CHARPOS (*it);
7549 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7550 }
7551
7552 if (it->bidi_it.charpos == eob)
7553 {
7554 /* Nothing to do, but reset the FIRST_ELT flag, like
7555 bidi_paragraph_init does, because we are not going to
7556 call it. */
7557 it->bidi_it.first_elt = 0;
7558 }
7559 else if (it->bidi_it.charpos == bob
7560 || (!string_p
7561 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7562 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7563 {
7564 /* If we are at the beginning of a line/string, we can produce
7565 the next element right away. */
7566 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7567 bidi_move_to_visually_next (&it->bidi_it);
7568 }
7569 else
7570 {
7571 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7572
7573 /* We need to prime the bidi iterator starting at the line's or
7574 string's beginning, before we will be able to produce the
7575 next element. */
7576 if (string_p)
7577 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7578 else
7579 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7580 IT_BYTEPOS (*it), -1,
7581 &it->bidi_it.bytepos);
7582 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7583 do
7584 {
7585 /* Now return to buffer/string position where we were asked
7586 to get the next display element, and produce that. */
7587 bidi_move_to_visually_next (&it->bidi_it);
7588 }
7589 while (it->bidi_it.bytepos != orig_bytepos
7590 && it->bidi_it.charpos < eob);
7591 }
7592
7593 /* Adjust IT's position information to where we ended up. */
7594 if (STRINGP (it->string))
7595 {
7596 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7597 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7598 }
7599 else
7600 {
7601 IT_CHARPOS (*it) = it->bidi_it.charpos;
7602 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7603 }
7604
7605 if (STRINGP (it->string) || !it->s)
7606 {
7607 ptrdiff_t stop, charpos, bytepos;
7608
7609 if (STRINGP (it->string))
7610 {
7611 eassert (!it->s);
7612 stop = SCHARS (it->string);
7613 if (stop > it->end_charpos)
7614 stop = it->end_charpos;
7615 charpos = IT_STRING_CHARPOS (*it);
7616 bytepos = IT_STRING_BYTEPOS (*it);
7617 }
7618 else
7619 {
7620 stop = it->end_charpos;
7621 charpos = IT_CHARPOS (*it);
7622 bytepos = IT_BYTEPOS (*it);
7623 }
7624 if (it->bidi_it.scan_dir < 0)
7625 stop = -1;
7626 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7627 it->string);
7628 }
7629 }
7630
7631 /* Load IT with the next display element from Lisp string IT->string.
7632 IT->current.string_pos is the current position within the string.
7633 If IT->current.overlay_string_index >= 0, the Lisp string is an
7634 overlay string. */
7635
7636 static int
7637 next_element_from_string (struct it *it)
7638 {
7639 struct text_pos position;
7640
7641 eassert (STRINGP (it->string));
7642 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7643 eassert (IT_STRING_CHARPOS (*it) >= 0);
7644 position = it->current.string_pos;
7645
7646 /* With bidi reordering, the character to display might not be the
7647 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7648 that we were reseat()ed to a new string, whose paragraph
7649 direction is not known. */
7650 if (it->bidi_p && it->bidi_it.first_elt)
7651 {
7652 get_visually_first_element (it);
7653 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7654 }
7655
7656 /* Time to check for invisible text? */
7657 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7658 {
7659 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7660 {
7661 if (!(!it->bidi_p
7662 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7663 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7664 {
7665 /* With bidi non-linear iteration, we could find
7666 ourselves far beyond the last computed stop_charpos,
7667 with several other stop positions in between that we
7668 missed. Scan them all now, in buffer's logical
7669 order, until we find and handle the last stop_charpos
7670 that precedes our current position. */
7671 handle_stop_backwards (it, it->stop_charpos);
7672 return GET_NEXT_DISPLAY_ELEMENT (it);
7673 }
7674 else
7675 {
7676 if (it->bidi_p)
7677 {
7678 /* Take note of the stop position we just moved
7679 across, for when we will move back across it. */
7680 it->prev_stop = it->stop_charpos;
7681 /* If we are at base paragraph embedding level, take
7682 note of the last stop position seen at this
7683 level. */
7684 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7685 it->base_level_stop = it->stop_charpos;
7686 }
7687 handle_stop (it);
7688
7689 /* Since a handler may have changed IT->method, we must
7690 recurse here. */
7691 return GET_NEXT_DISPLAY_ELEMENT (it);
7692 }
7693 }
7694 else if (it->bidi_p
7695 /* If we are before prev_stop, we may have overstepped
7696 on our way backwards a stop_pos, and if so, we need
7697 to handle that stop_pos. */
7698 && IT_STRING_CHARPOS (*it) < it->prev_stop
7699 /* We can sometimes back up for reasons that have nothing
7700 to do with bidi reordering. E.g., compositions. The
7701 code below is only needed when we are above the base
7702 embedding level, so test for that explicitly. */
7703 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7704 {
7705 /* If we lost track of base_level_stop, we have no better
7706 place for handle_stop_backwards to start from than string
7707 beginning. This happens, e.g., when we were reseated to
7708 the previous screenful of text by vertical-motion. */
7709 if (it->base_level_stop <= 0
7710 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7711 it->base_level_stop = 0;
7712 handle_stop_backwards (it, it->base_level_stop);
7713 return GET_NEXT_DISPLAY_ELEMENT (it);
7714 }
7715 }
7716
7717 if (it->current.overlay_string_index >= 0)
7718 {
7719 /* Get the next character from an overlay string. In overlay
7720 strings, there is no field width or padding with spaces to
7721 do. */
7722 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7723 {
7724 it->what = IT_EOB;
7725 return 0;
7726 }
7727 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7728 IT_STRING_BYTEPOS (*it),
7729 it->bidi_it.scan_dir < 0
7730 ? -1
7731 : SCHARS (it->string))
7732 && next_element_from_composition (it))
7733 {
7734 return 1;
7735 }
7736 else if (STRING_MULTIBYTE (it->string))
7737 {
7738 const unsigned char *s = (SDATA (it->string)
7739 + IT_STRING_BYTEPOS (*it));
7740 it->c = string_char_and_length (s, &it->len);
7741 }
7742 else
7743 {
7744 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7745 it->len = 1;
7746 }
7747 }
7748 else
7749 {
7750 /* Get the next character from a Lisp string that is not an
7751 overlay string. Such strings come from the mode line, for
7752 example. We may have to pad with spaces, or truncate the
7753 string. See also next_element_from_c_string. */
7754 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7755 {
7756 it->what = IT_EOB;
7757 return 0;
7758 }
7759 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7760 {
7761 /* Pad with spaces. */
7762 it->c = ' ', it->len = 1;
7763 CHARPOS (position) = BYTEPOS (position) = -1;
7764 }
7765 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7766 IT_STRING_BYTEPOS (*it),
7767 it->bidi_it.scan_dir < 0
7768 ? -1
7769 : it->string_nchars)
7770 && next_element_from_composition (it))
7771 {
7772 return 1;
7773 }
7774 else if (STRING_MULTIBYTE (it->string))
7775 {
7776 const unsigned char *s = (SDATA (it->string)
7777 + IT_STRING_BYTEPOS (*it));
7778 it->c = string_char_and_length (s, &it->len);
7779 }
7780 else
7781 {
7782 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7783 it->len = 1;
7784 }
7785 }
7786
7787 /* Record what we have and where it came from. */
7788 it->what = IT_CHARACTER;
7789 it->object = it->string;
7790 it->position = position;
7791 return 1;
7792 }
7793
7794
7795 /* Load IT with next display element from C string IT->s.
7796 IT->string_nchars is the maximum number of characters to return
7797 from the string. IT->end_charpos may be greater than
7798 IT->string_nchars when this function is called, in which case we
7799 may have to return padding spaces. Value is zero if end of string
7800 reached, including padding spaces. */
7801
7802 static int
7803 next_element_from_c_string (struct it *it)
7804 {
7805 int success_p = 1;
7806
7807 eassert (it->s);
7808 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7809 it->what = IT_CHARACTER;
7810 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7811 it->object = Qnil;
7812
7813 /* With bidi reordering, the character to display might not be the
7814 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7815 we were reseated to a new string, whose paragraph direction is
7816 not known. */
7817 if (it->bidi_p && it->bidi_it.first_elt)
7818 get_visually_first_element (it);
7819
7820 /* IT's position can be greater than IT->string_nchars in case a
7821 field width or precision has been specified when the iterator was
7822 initialized. */
7823 if (IT_CHARPOS (*it) >= it->end_charpos)
7824 {
7825 /* End of the game. */
7826 it->what = IT_EOB;
7827 success_p = 0;
7828 }
7829 else if (IT_CHARPOS (*it) >= it->string_nchars)
7830 {
7831 /* Pad with spaces. */
7832 it->c = ' ', it->len = 1;
7833 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7834 }
7835 else if (it->multibyte_p)
7836 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7837 else
7838 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7839
7840 return success_p;
7841 }
7842
7843
7844 /* Set up IT to return characters from an ellipsis, if appropriate.
7845 The definition of the ellipsis glyphs may come from a display table
7846 entry. This function fills IT with the first glyph from the
7847 ellipsis if an ellipsis is to be displayed. */
7848
7849 static int
7850 next_element_from_ellipsis (struct it *it)
7851 {
7852 if (it->selective_display_ellipsis_p)
7853 setup_for_ellipsis (it, it->len);
7854 else
7855 {
7856 /* The face at the current position may be different from the
7857 face we find after the invisible text. Remember what it
7858 was in IT->saved_face_id, and signal that it's there by
7859 setting face_before_selective_p. */
7860 it->saved_face_id = it->face_id;
7861 it->method = GET_FROM_BUFFER;
7862 it->object = it->w->contents;
7863 reseat_at_next_visible_line_start (it, 1);
7864 it->face_before_selective_p = 1;
7865 }
7866
7867 return GET_NEXT_DISPLAY_ELEMENT (it);
7868 }
7869
7870
7871 /* Deliver an image display element. The iterator IT is already
7872 filled with image information (done in handle_display_prop). Value
7873 is always 1. */
7874
7875
7876 static int
7877 next_element_from_image (struct it *it)
7878 {
7879 it->what = IT_IMAGE;
7880 it->ignore_overlay_strings_at_pos_p = 0;
7881 return 1;
7882 }
7883
7884
7885 /* Fill iterator IT with next display element from a stretch glyph
7886 property. IT->object is the value of the text property. Value is
7887 always 1. */
7888
7889 static int
7890 next_element_from_stretch (struct it *it)
7891 {
7892 it->what = IT_STRETCH;
7893 return 1;
7894 }
7895
7896 /* Scan backwards from IT's current position until we find a stop
7897 position, or until BEGV. This is called when we find ourself
7898 before both the last known prev_stop and base_level_stop while
7899 reordering bidirectional text. */
7900
7901 static void
7902 compute_stop_pos_backwards (struct it *it)
7903 {
7904 const int SCAN_BACK_LIMIT = 1000;
7905 struct text_pos pos;
7906 struct display_pos save_current = it->current;
7907 struct text_pos save_position = it->position;
7908 ptrdiff_t charpos = IT_CHARPOS (*it);
7909 ptrdiff_t where_we_are = charpos;
7910 ptrdiff_t save_stop_pos = it->stop_charpos;
7911 ptrdiff_t save_end_pos = it->end_charpos;
7912
7913 eassert (NILP (it->string) && !it->s);
7914 eassert (it->bidi_p);
7915 it->bidi_p = 0;
7916 do
7917 {
7918 it->end_charpos = min (charpos + 1, ZV);
7919 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7920 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7921 reseat_1 (it, pos, 0);
7922 compute_stop_pos (it);
7923 /* We must advance forward, right? */
7924 if (it->stop_charpos <= charpos)
7925 emacs_abort ();
7926 }
7927 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7928
7929 if (it->stop_charpos <= where_we_are)
7930 it->prev_stop = it->stop_charpos;
7931 else
7932 it->prev_stop = BEGV;
7933 it->bidi_p = 1;
7934 it->current = save_current;
7935 it->position = save_position;
7936 it->stop_charpos = save_stop_pos;
7937 it->end_charpos = save_end_pos;
7938 }
7939
7940 /* Scan forward from CHARPOS in the current buffer/string, until we
7941 find a stop position > current IT's position. Then handle the stop
7942 position before that. This is called when we bump into a stop
7943 position while reordering bidirectional text. CHARPOS should be
7944 the last previously processed stop_pos (or BEGV/0, if none were
7945 processed yet) whose position is less that IT's current
7946 position. */
7947
7948 static void
7949 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7950 {
7951 int bufp = !STRINGP (it->string);
7952 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7953 struct display_pos save_current = it->current;
7954 struct text_pos save_position = it->position;
7955 struct text_pos pos1;
7956 ptrdiff_t next_stop;
7957
7958 /* Scan in strict logical order. */
7959 eassert (it->bidi_p);
7960 it->bidi_p = 0;
7961 do
7962 {
7963 it->prev_stop = charpos;
7964 if (bufp)
7965 {
7966 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7967 reseat_1 (it, pos1, 0);
7968 }
7969 else
7970 it->current.string_pos = string_pos (charpos, it->string);
7971 compute_stop_pos (it);
7972 /* We must advance forward, right? */
7973 if (it->stop_charpos <= it->prev_stop)
7974 emacs_abort ();
7975 charpos = it->stop_charpos;
7976 }
7977 while (charpos <= where_we_are);
7978
7979 it->bidi_p = 1;
7980 it->current = save_current;
7981 it->position = save_position;
7982 next_stop = it->stop_charpos;
7983 it->stop_charpos = it->prev_stop;
7984 handle_stop (it);
7985 it->stop_charpos = next_stop;
7986 }
7987
7988 /* Load IT with the next display element from current_buffer. Value
7989 is zero if end of buffer reached. IT->stop_charpos is the next
7990 position at which to stop and check for text properties or buffer
7991 end. */
7992
7993 static int
7994 next_element_from_buffer (struct it *it)
7995 {
7996 int success_p = 1;
7997
7998 eassert (IT_CHARPOS (*it) >= BEGV);
7999 eassert (NILP (it->string) && !it->s);
8000 eassert (!it->bidi_p
8001 || (EQ (it->bidi_it.string.lstring, Qnil)
8002 && it->bidi_it.string.s == NULL));
8003
8004 /* With bidi reordering, the character to display might not be the
8005 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8006 we were reseat()ed to a new buffer position, which is potentially
8007 a different paragraph. */
8008 if (it->bidi_p && it->bidi_it.first_elt)
8009 {
8010 get_visually_first_element (it);
8011 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8012 }
8013
8014 if (IT_CHARPOS (*it) >= it->stop_charpos)
8015 {
8016 if (IT_CHARPOS (*it) >= it->end_charpos)
8017 {
8018 int overlay_strings_follow_p;
8019
8020 /* End of the game, except when overlay strings follow that
8021 haven't been returned yet. */
8022 if (it->overlay_strings_at_end_processed_p)
8023 overlay_strings_follow_p = 0;
8024 else
8025 {
8026 it->overlay_strings_at_end_processed_p = 1;
8027 overlay_strings_follow_p = get_overlay_strings (it, 0);
8028 }
8029
8030 if (overlay_strings_follow_p)
8031 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8032 else
8033 {
8034 it->what = IT_EOB;
8035 it->position = it->current.pos;
8036 success_p = 0;
8037 }
8038 }
8039 else if (!(!it->bidi_p
8040 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8041 || IT_CHARPOS (*it) == it->stop_charpos))
8042 {
8043 /* With bidi non-linear iteration, we could find ourselves
8044 far beyond the last computed stop_charpos, with several
8045 other stop positions in between that we missed. Scan
8046 them all now, in buffer's logical order, until we find
8047 and handle the last stop_charpos that precedes our
8048 current position. */
8049 handle_stop_backwards (it, it->stop_charpos);
8050 return GET_NEXT_DISPLAY_ELEMENT (it);
8051 }
8052 else
8053 {
8054 if (it->bidi_p)
8055 {
8056 /* Take note of the stop position we just moved across,
8057 for when we will move back across it. */
8058 it->prev_stop = it->stop_charpos;
8059 /* If we are at base paragraph embedding level, take
8060 note of the last stop position seen at this
8061 level. */
8062 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8063 it->base_level_stop = it->stop_charpos;
8064 }
8065 handle_stop (it);
8066 return GET_NEXT_DISPLAY_ELEMENT (it);
8067 }
8068 }
8069 else if (it->bidi_p
8070 /* If we are before prev_stop, we may have overstepped on
8071 our way backwards a stop_pos, and if so, we need to
8072 handle that stop_pos. */
8073 && IT_CHARPOS (*it) < it->prev_stop
8074 /* We can sometimes back up for reasons that have nothing
8075 to do with bidi reordering. E.g., compositions. The
8076 code below is only needed when we are above the base
8077 embedding level, so test for that explicitly. */
8078 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8079 {
8080 if (it->base_level_stop <= 0
8081 || IT_CHARPOS (*it) < it->base_level_stop)
8082 {
8083 /* If we lost track of base_level_stop, we need to find
8084 prev_stop by looking backwards. This happens, e.g., when
8085 we were reseated to the previous screenful of text by
8086 vertical-motion. */
8087 it->base_level_stop = BEGV;
8088 compute_stop_pos_backwards (it);
8089 handle_stop_backwards (it, it->prev_stop);
8090 }
8091 else
8092 handle_stop_backwards (it, it->base_level_stop);
8093 return GET_NEXT_DISPLAY_ELEMENT (it);
8094 }
8095 else
8096 {
8097 /* No face changes, overlays etc. in sight, so just return a
8098 character from current_buffer. */
8099 unsigned char *p;
8100 ptrdiff_t stop;
8101
8102 /* Maybe run the redisplay end trigger hook. Performance note:
8103 This doesn't seem to cost measurable time. */
8104 if (it->redisplay_end_trigger_charpos
8105 && it->glyph_row
8106 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8107 run_redisplay_end_trigger_hook (it);
8108
8109 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8110 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8111 stop)
8112 && next_element_from_composition (it))
8113 {
8114 return 1;
8115 }
8116
8117 /* Get the next character, maybe multibyte. */
8118 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8119 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8120 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8121 else
8122 it->c = *p, it->len = 1;
8123
8124 /* Record what we have and where it came from. */
8125 it->what = IT_CHARACTER;
8126 it->object = it->w->contents;
8127 it->position = it->current.pos;
8128
8129 /* Normally we return the character found above, except when we
8130 really want to return an ellipsis for selective display. */
8131 if (it->selective)
8132 {
8133 if (it->c == '\n')
8134 {
8135 /* A value of selective > 0 means hide lines indented more
8136 than that number of columns. */
8137 if (it->selective > 0
8138 && IT_CHARPOS (*it) + 1 < ZV
8139 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8140 IT_BYTEPOS (*it) + 1,
8141 it->selective))
8142 {
8143 success_p = next_element_from_ellipsis (it);
8144 it->dpvec_char_len = -1;
8145 }
8146 }
8147 else if (it->c == '\r' && it->selective == -1)
8148 {
8149 /* A value of selective == -1 means that everything from the
8150 CR to the end of the line is invisible, with maybe an
8151 ellipsis displayed for it. */
8152 success_p = next_element_from_ellipsis (it);
8153 it->dpvec_char_len = -1;
8154 }
8155 }
8156 }
8157
8158 /* Value is zero if end of buffer reached. */
8159 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8160 return success_p;
8161 }
8162
8163
8164 /* Run the redisplay end trigger hook for IT. */
8165
8166 static void
8167 run_redisplay_end_trigger_hook (struct it *it)
8168 {
8169 Lisp_Object args[3];
8170
8171 /* IT->glyph_row should be non-null, i.e. we should be actually
8172 displaying something, or otherwise we should not run the hook. */
8173 eassert (it->glyph_row);
8174
8175 /* Set up hook arguments. */
8176 args[0] = Qredisplay_end_trigger_functions;
8177 args[1] = it->window;
8178 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8179 it->redisplay_end_trigger_charpos = 0;
8180
8181 /* Since we are *trying* to run these functions, don't try to run
8182 them again, even if they get an error. */
8183 wset_redisplay_end_trigger (it->w, Qnil);
8184 Frun_hook_with_args (3, args);
8185
8186 /* Notice if it changed the face of the character we are on. */
8187 handle_face_prop (it);
8188 }
8189
8190
8191 /* Deliver a composition display element. Unlike the other
8192 next_element_from_XXX, this function is not registered in the array
8193 get_next_element[]. It is called from next_element_from_buffer and
8194 next_element_from_string when necessary. */
8195
8196 static int
8197 next_element_from_composition (struct it *it)
8198 {
8199 it->what = IT_COMPOSITION;
8200 it->len = it->cmp_it.nbytes;
8201 if (STRINGP (it->string))
8202 {
8203 if (it->c < 0)
8204 {
8205 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8206 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8207 return 0;
8208 }
8209 it->position = it->current.string_pos;
8210 it->object = it->string;
8211 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8212 IT_STRING_BYTEPOS (*it), it->string);
8213 }
8214 else
8215 {
8216 if (it->c < 0)
8217 {
8218 IT_CHARPOS (*it) += it->cmp_it.nchars;
8219 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8220 if (it->bidi_p)
8221 {
8222 if (it->bidi_it.new_paragraph)
8223 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8224 /* Resync the bidi iterator with IT's new position.
8225 FIXME: this doesn't support bidirectional text. */
8226 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8227 bidi_move_to_visually_next (&it->bidi_it);
8228 }
8229 return 0;
8230 }
8231 it->position = it->current.pos;
8232 it->object = it->w->contents;
8233 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8234 IT_BYTEPOS (*it), Qnil);
8235 }
8236 return 1;
8237 }
8238
8239
8240 \f
8241 /***********************************************************************
8242 Moving an iterator without producing glyphs
8243 ***********************************************************************/
8244
8245 /* Check if iterator is at a position corresponding to a valid buffer
8246 position after some move_it_ call. */
8247
8248 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8249 ((it)->method == GET_FROM_STRING \
8250 ? IT_STRING_CHARPOS (*it) == 0 \
8251 : 1)
8252
8253
8254 /* Move iterator IT to a specified buffer or X position within one
8255 line on the display without producing glyphs.
8256
8257 OP should be a bit mask including some or all of these bits:
8258 MOVE_TO_X: Stop upon reaching x-position TO_X.
8259 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8260 Regardless of OP's value, stop upon reaching the end of the display line.
8261
8262 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8263 This means, in particular, that TO_X includes window's horizontal
8264 scroll amount.
8265
8266 The return value has several possible values that
8267 say what condition caused the scan to stop:
8268
8269 MOVE_POS_MATCH_OR_ZV
8270 - when TO_POS or ZV was reached.
8271
8272 MOVE_X_REACHED
8273 -when TO_X was reached before TO_POS or ZV were reached.
8274
8275 MOVE_LINE_CONTINUED
8276 - when we reached the end of the display area and the line must
8277 be continued.
8278
8279 MOVE_LINE_TRUNCATED
8280 - when we reached the end of the display area and the line is
8281 truncated.
8282
8283 MOVE_NEWLINE_OR_CR
8284 - when we stopped at a line end, i.e. a newline or a CR and selective
8285 display is on. */
8286
8287 static enum move_it_result
8288 move_it_in_display_line_to (struct it *it,
8289 ptrdiff_t to_charpos, int to_x,
8290 enum move_operation_enum op)
8291 {
8292 enum move_it_result result = MOVE_UNDEFINED;
8293 struct glyph_row *saved_glyph_row;
8294 struct it wrap_it, atpos_it, atx_it, ppos_it;
8295 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8296 void *ppos_data = NULL;
8297 int may_wrap = 0;
8298 enum it_method prev_method = it->method;
8299 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8300 int saw_smaller_pos = prev_pos < to_charpos;
8301
8302 /* Don't produce glyphs in produce_glyphs. */
8303 saved_glyph_row = it->glyph_row;
8304 it->glyph_row = NULL;
8305
8306 /* Use wrap_it to save a copy of IT wherever a word wrap could
8307 occur. Use atpos_it to save a copy of IT at the desired buffer
8308 position, if found, so that we can scan ahead and check if the
8309 word later overshoots the window edge. Use atx_it similarly, for
8310 pixel positions. */
8311 wrap_it.sp = -1;
8312 atpos_it.sp = -1;
8313 atx_it.sp = -1;
8314
8315 /* Use ppos_it under bidi reordering to save a copy of IT for the
8316 position > CHARPOS that is the closest to CHARPOS. We restore
8317 that position in IT when we have scanned the entire display line
8318 without finding a match for CHARPOS and all the character
8319 positions are greater than CHARPOS. */
8320 if (it->bidi_p)
8321 {
8322 SAVE_IT (ppos_it, *it, ppos_data);
8323 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8324 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8325 SAVE_IT (ppos_it, *it, ppos_data);
8326 }
8327
8328 #define BUFFER_POS_REACHED_P() \
8329 ((op & MOVE_TO_POS) != 0 \
8330 && BUFFERP (it->object) \
8331 && (IT_CHARPOS (*it) == to_charpos \
8332 || ((!it->bidi_p \
8333 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8334 && IT_CHARPOS (*it) > to_charpos) \
8335 || (it->what == IT_COMPOSITION \
8336 && ((IT_CHARPOS (*it) > to_charpos \
8337 && to_charpos >= it->cmp_it.charpos) \
8338 || (IT_CHARPOS (*it) < to_charpos \
8339 && to_charpos <= it->cmp_it.charpos)))) \
8340 && (it->method == GET_FROM_BUFFER \
8341 || (it->method == GET_FROM_DISPLAY_VECTOR \
8342 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8343
8344 /* If there's a line-/wrap-prefix, handle it. */
8345 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8346 && it->current_y < it->last_visible_y)
8347 handle_line_prefix (it);
8348
8349 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8350 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8351
8352 while (1)
8353 {
8354 int x, i, ascent = 0, descent = 0;
8355
8356 /* Utility macro to reset an iterator with x, ascent, and descent. */
8357 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8358 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8359 (IT)->max_descent = descent)
8360
8361 /* Stop if we move beyond TO_CHARPOS (after an image or a
8362 display string or stretch glyph). */
8363 if ((op & MOVE_TO_POS) != 0
8364 && BUFFERP (it->object)
8365 && it->method == GET_FROM_BUFFER
8366 && (((!it->bidi_p
8367 /* When the iterator is at base embedding level, we
8368 are guaranteed that characters are delivered for
8369 display in strictly increasing order of their
8370 buffer positions. */
8371 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8372 && IT_CHARPOS (*it) > to_charpos)
8373 || (it->bidi_p
8374 && (prev_method == GET_FROM_IMAGE
8375 || prev_method == GET_FROM_STRETCH
8376 || prev_method == GET_FROM_STRING)
8377 /* Passed TO_CHARPOS from left to right. */
8378 && ((prev_pos < to_charpos
8379 && IT_CHARPOS (*it) > to_charpos)
8380 /* Passed TO_CHARPOS from right to left. */
8381 || (prev_pos > to_charpos
8382 && IT_CHARPOS (*it) < to_charpos)))))
8383 {
8384 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8385 {
8386 result = MOVE_POS_MATCH_OR_ZV;
8387 break;
8388 }
8389 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8390 /* If wrap_it is valid, the current position might be in a
8391 word that is wrapped. So, save the iterator in
8392 atpos_it and continue to see if wrapping happens. */
8393 SAVE_IT (atpos_it, *it, atpos_data);
8394 }
8395
8396 /* Stop when ZV reached.
8397 We used to stop here when TO_CHARPOS reached as well, but that is
8398 too soon if this glyph does not fit on this line. So we handle it
8399 explicitly below. */
8400 if (!get_next_display_element (it))
8401 {
8402 result = MOVE_POS_MATCH_OR_ZV;
8403 break;
8404 }
8405
8406 if (it->line_wrap == TRUNCATE)
8407 {
8408 if (BUFFER_POS_REACHED_P ())
8409 {
8410 result = MOVE_POS_MATCH_OR_ZV;
8411 break;
8412 }
8413 }
8414 else
8415 {
8416 if (it->line_wrap == WORD_WRAP)
8417 {
8418 if (IT_DISPLAYING_WHITESPACE (it))
8419 may_wrap = 1;
8420 else if (may_wrap)
8421 {
8422 /* We have reached a glyph that follows one or more
8423 whitespace characters. If the position is
8424 already found, we are done. */
8425 if (atpos_it.sp >= 0)
8426 {
8427 RESTORE_IT (it, &atpos_it, atpos_data);
8428 result = MOVE_POS_MATCH_OR_ZV;
8429 goto done;
8430 }
8431 if (atx_it.sp >= 0)
8432 {
8433 RESTORE_IT (it, &atx_it, atx_data);
8434 result = MOVE_X_REACHED;
8435 goto done;
8436 }
8437 /* Otherwise, we can wrap here. */
8438 SAVE_IT (wrap_it, *it, wrap_data);
8439 may_wrap = 0;
8440 }
8441 }
8442 }
8443
8444 /* Remember the line height for the current line, in case
8445 the next element doesn't fit on the line. */
8446 ascent = it->max_ascent;
8447 descent = it->max_descent;
8448
8449 /* The call to produce_glyphs will get the metrics of the
8450 display element IT is loaded with. Record the x-position
8451 before this display element, in case it doesn't fit on the
8452 line. */
8453 x = it->current_x;
8454
8455 PRODUCE_GLYPHS (it);
8456
8457 if (it->area != TEXT_AREA)
8458 {
8459 prev_method = it->method;
8460 if (it->method == GET_FROM_BUFFER)
8461 prev_pos = IT_CHARPOS (*it);
8462 set_iterator_to_next (it, 1);
8463 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8464 SET_TEXT_POS (this_line_min_pos,
8465 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8466 if (it->bidi_p
8467 && (op & MOVE_TO_POS)
8468 && IT_CHARPOS (*it) > to_charpos
8469 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8470 SAVE_IT (ppos_it, *it, ppos_data);
8471 continue;
8472 }
8473
8474 /* The number of glyphs we get back in IT->nglyphs will normally
8475 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8476 character on a terminal frame, or (iii) a line end. For the
8477 second case, IT->nglyphs - 1 padding glyphs will be present.
8478 (On X frames, there is only one glyph produced for a
8479 composite character.)
8480
8481 The behavior implemented below means, for continuation lines,
8482 that as many spaces of a TAB as fit on the current line are
8483 displayed there. For terminal frames, as many glyphs of a
8484 multi-glyph character are displayed in the current line, too.
8485 This is what the old redisplay code did, and we keep it that
8486 way. Under X, the whole shape of a complex character must
8487 fit on the line or it will be completely displayed in the
8488 next line.
8489
8490 Note that both for tabs and padding glyphs, all glyphs have
8491 the same width. */
8492 if (it->nglyphs)
8493 {
8494 /* More than one glyph or glyph doesn't fit on line. All
8495 glyphs have the same width. */
8496 int single_glyph_width = it->pixel_width / it->nglyphs;
8497 int new_x;
8498 int x_before_this_char = x;
8499 int hpos_before_this_char = it->hpos;
8500
8501 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8502 {
8503 new_x = x + single_glyph_width;
8504
8505 /* We want to leave anything reaching TO_X to the caller. */
8506 if ((op & MOVE_TO_X) && new_x > to_x)
8507 {
8508 if (BUFFER_POS_REACHED_P ())
8509 {
8510 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8511 goto buffer_pos_reached;
8512 if (atpos_it.sp < 0)
8513 {
8514 SAVE_IT (atpos_it, *it, atpos_data);
8515 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8516 }
8517 }
8518 else
8519 {
8520 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8521 {
8522 it->current_x = x;
8523 result = MOVE_X_REACHED;
8524 break;
8525 }
8526 if (atx_it.sp < 0)
8527 {
8528 SAVE_IT (atx_it, *it, atx_data);
8529 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8530 }
8531 }
8532 }
8533
8534 if (/* Lines are continued. */
8535 it->line_wrap != TRUNCATE
8536 && (/* And glyph doesn't fit on the line. */
8537 new_x > it->last_visible_x
8538 /* Or it fits exactly and we're on a window
8539 system frame. */
8540 || (new_x == it->last_visible_x
8541 && FRAME_WINDOW_P (it->f)
8542 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8543 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8544 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8545 {
8546 if (/* IT->hpos == 0 means the very first glyph
8547 doesn't fit on the line, e.g. a wide image. */
8548 it->hpos == 0
8549 || (new_x == it->last_visible_x
8550 && FRAME_WINDOW_P (it->f)))
8551 {
8552 ++it->hpos;
8553 it->current_x = new_x;
8554
8555 /* The character's last glyph just barely fits
8556 in this row. */
8557 if (i == it->nglyphs - 1)
8558 {
8559 /* If this is the destination position,
8560 return a position *before* it in this row,
8561 now that we know it fits in this row. */
8562 if (BUFFER_POS_REACHED_P ())
8563 {
8564 if (it->line_wrap != WORD_WRAP
8565 || wrap_it.sp < 0)
8566 {
8567 it->hpos = hpos_before_this_char;
8568 it->current_x = x_before_this_char;
8569 result = MOVE_POS_MATCH_OR_ZV;
8570 break;
8571 }
8572 if (it->line_wrap == WORD_WRAP
8573 && atpos_it.sp < 0)
8574 {
8575 SAVE_IT (atpos_it, *it, atpos_data);
8576 atpos_it.current_x = x_before_this_char;
8577 atpos_it.hpos = hpos_before_this_char;
8578 }
8579 }
8580
8581 prev_method = it->method;
8582 if (it->method == GET_FROM_BUFFER)
8583 prev_pos = IT_CHARPOS (*it);
8584 set_iterator_to_next (it, 1);
8585 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8586 SET_TEXT_POS (this_line_min_pos,
8587 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8588 /* On graphical terminals, newlines may
8589 "overflow" into the fringe if
8590 overflow-newline-into-fringe is non-nil.
8591 On text terminals, and on graphical
8592 terminals with no right margin, newlines
8593 may overflow into the last glyph on the
8594 display line.*/
8595 if (!FRAME_WINDOW_P (it->f)
8596 || ((it->bidi_p
8597 && it->bidi_it.paragraph_dir == R2L)
8598 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8599 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8600 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8601 {
8602 if (!get_next_display_element (it))
8603 {
8604 result = MOVE_POS_MATCH_OR_ZV;
8605 break;
8606 }
8607 if (BUFFER_POS_REACHED_P ())
8608 {
8609 if (ITERATOR_AT_END_OF_LINE_P (it))
8610 result = MOVE_POS_MATCH_OR_ZV;
8611 else
8612 result = MOVE_LINE_CONTINUED;
8613 break;
8614 }
8615 if (ITERATOR_AT_END_OF_LINE_P (it)
8616 && (it->line_wrap != WORD_WRAP
8617 || wrap_it.sp < 0))
8618 {
8619 result = MOVE_NEWLINE_OR_CR;
8620 break;
8621 }
8622 }
8623 }
8624 }
8625 else
8626 IT_RESET_X_ASCENT_DESCENT (it);
8627
8628 if (wrap_it.sp >= 0)
8629 {
8630 RESTORE_IT (it, &wrap_it, wrap_data);
8631 atpos_it.sp = -1;
8632 atx_it.sp = -1;
8633 }
8634
8635 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8636 IT_CHARPOS (*it)));
8637 result = MOVE_LINE_CONTINUED;
8638 break;
8639 }
8640
8641 if (BUFFER_POS_REACHED_P ())
8642 {
8643 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8644 goto buffer_pos_reached;
8645 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8646 {
8647 SAVE_IT (atpos_it, *it, atpos_data);
8648 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8649 }
8650 }
8651
8652 if (new_x > it->first_visible_x)
8653 {
8654 /* Glyph is visible. Increment number of glyphs that
8655 would be displayed. */
8656 ++it->hpos;
8657 }
8658 }
8659
8660 if (result != MOVE_UNDEFINED)
8661 break;
8662 }
8663 else if (BUFFER_POS_REACHED_P ())
8664 {
8665 buffer_pos_reached:
8666 IT_RESET_X_ASCENT_DESCENT (it);
8667 result = MOVE_POS_MATCH_OR_ZV;
8668 break;
8669 }
8670 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8671 {
8672 /* Stop when TO_X specified and reached. This check is
8673 necessary here because of lines consisting of a line end,
8674 only. The line end will not produce any glyphs and we
8675 would never get MOVE_X_REACHED. */
8676 eassert (it->nglyphs == 0);
8677 result = MOVE_X_REACHED;
8678 break;
8679 }
8680
8681 /* Is this a line end? If yes, we're done. */
8682 if (ITERATOR_AT_END_OF_LINE_P (it))
8683 {
8684 /* If we are past TO_CHARPOS, but never saw any character
8685 positions smaller than TO_CHARPOS, return
8686 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8687 did. */
8688 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8689 {
8690 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8691 {
8692 if (IT_CHARPOS (ppos_it) < ZV)
8693 {
8694 RESTORE_IT (it, &ppos_it, ppos_data);
8695 result = MOVE_POS_MATCH_OR_ZV;
8696 }
8697 else
8698 goto buffer_pos_reached;
8699 }
8700 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8701 && IT_CHARPOS (*it) > to_charpos)
8702 goto buffer_pos_reached;
8703 else
8704 result = MOVE_NEWLINE_OR_CR;
8705 }
8706 else
8707 result = MOVE_NEWLINE_OR_CR;
8708 break;
8709 }
8710
8711 prev_method = it->method;
8712 if (it->method == GET_FROM_BUFFER)
8713 prev_pos = IT_CHARPOS (*it);
8714 /* The current display element has been consumed. Advance
8715 to the next. */
8716 set_iterator_to_next (it, 1);
8717 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8718 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8719 if (IT_CHARPOS (*it) < to_charpos)
8720 saw_smaller_pos = 1;
8721 if (it->bidi_p
8722 && (op & MOVE_TO_POS)
8723 && IT_CHARPOS (*it) >= to_charpos
8724 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8725 SAVE_IT (ppos_it, *it, ppos_data);
8726
8727 /* Stop if lines are truncated and IT's current x-position is
8728 past the right edge of the window now. */
8729 if (it->line_wrap == TRUNCATE
8730 && it->current_x >= it->last_visible_x)
8731 {
8732 if (!FRAME_WINDOW_P (it->f)
8733 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8734 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8735 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8736 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8737 {
8738 int at_eob_p = 0;
8739
8740 if ((at_eob_p = !get_next_display_element (it))
8741 || BUFFER_POS_REACHED_P ()
8742 /* If we are past TO_CHARPOS, but never saw any
8743 character positions smaller than TO_CHARPOS,
8744 return MOVE_POS_MATCH_OR_ZV, like the
8745 unidirectional display did. */
8746 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8747 && !saw_smaller_pos
8748 && IT_CHARPOS (*it) > to_charpos))
8749 {
8750 if (it->bidi_p
8751 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8752 RESTORE_IT (it, &ppos_it, ppos_data);
8753 result = MOVE_POS_MATCH_OR_ZV;
8754 break;
8755 }
8756 if (ITERATOR_AT_END_OF_LINE_P (it))
8757 {
8758 result = MOVE_NEWLINE_OR_CR;
8759 break;
8760 }
8761 }
8762 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8763 && !saw_smaller_pos
8764 && IT_CHARPOS (*it) > to_charpos)
8765 {
8766 if (IT_CHARPOS (ppos_it) < ZV)
8767 RESTORE_IT (it, &ppos_it, ppos_data);
8768 result = MOVE_POS_MATCH_OR_ZV;
8769 break;
8770 }
8771 result = MOVE_LINE_TRUNCATED;
8772 break;
8773 }
8774 #undef IT_RESET_X_ASCENT_DESCENT
8775 }
8776
8777 #undef BUFFER_POS_REACHED_P
8778
8779 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8780 restore the saved iterator. */
8781 if (atpos_it.sp >= 0)
8782 RESTORE_IT (it, &atpos_it, atpos_data);
8783 else if (atx_it.sp >= 0)
8784 RESTORE_IT (it, &atx_it, atx_data);
8785
8786 done:
8787
8788 if (atpos_data)
8789 bidi_unshelve_cache (atpos_data, 1);
8790 if (atx_data)
8791 bidi_unshelve_cache (atx_data, 1);
8792 if (wrap_data)
8793 bidi_unshelve_cache (wrap_data, 1);
8794 if (ppos_data)
8795 bidi_unshelve_cache (ppos_data, 1);
8796
8797 /* Restore the iterator settings altered at the beginning of this
8798 function. */
8799 it->glyph_row = saved_glyph_row;
8800 return result;
8801 }
8802
8803 /* For external use. */
8804 void
8805 move_it_in_display_line (struct it *it,
8806 ptrdiff_t to_charpos, int to_x,
8807 enum move_operation_enum op)
8808 {
8809 if (it->line_wrap == WORD_WRAP
8810 && (op & MOVE_TO_X))
8811 {
8812 struct it save_it;
8813 void *save_data = NULL;
8814 int skip;
8815
8816 SAVE_IT (save_it, *it, save_data);
8817 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8818 /* When word-wrap is on, TO_X may lie past the end
8819 of a wrapped line. Then it->current is the
8820 character on the next line, so backtrack to the
8821 space before the wrap point. */
8822 if (skip == MOVE_LINE_CONTINUED)
8823 {
8824 int prev_x = max (it->current_x - 1, 0);
8825 RESTORE_IT (it, &save_it, save_data);
8826 move_it_in_display_line_to
8827 (it, -1, prev_x, MOVE_TO_X);
8828 }
8829 else
8830 bidi_unshelve_cache (save_data, 1);
8831 }
8832 else
8833 move_it_in_display_line_to (it, to_charpos, to_x, op);
8834 }
8835
8836
8837 /* Move IT forward until it satisfies one or more of the criteria in
8838 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8839
8840 OP is a bit-mask that specifies where to stop, and in particular,
8841 which of those four position arguments makes a difference. See the
8842 description of enum move_operation_enum.
8843
8844 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8845 screen line, this function will set IT to the next position that is
8846 displayed to the right of TO_CHARPOS on the screen. */
8847
8848 void
8849 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8850 {
8851 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8852 int line_height, line_start_x = 0, reached = 0;
8853 void *backup_data = NULL;
8854
8855 for (;;)
8856 {
8857 if (op & MOVE_TO_VPOS)
8858 {
8859 /* If no TO_CHARPOS and no TO_X specified, stop at the
8860 start of the line TO_VPOS. */
8861 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8862 {
8863 if (it->vpos == to_vpos)
8864 {
8865 reached = 1;
8866 break;
8867 }
8868 else
8869 skip = move_it_in_display_line_to (it, -1, -1, 0);
8870 }
8871 else
8872 {
8873 /* TO_VPOS >= 0 means stop at TO_X in the line at
8874 TO_VPOS, or at TO_POS, whichever comes first. */
8875 if (it->vpos == to_vpos)
8876 {
8877 reached = 2;
8878 break;
8879 }
8880
8881 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8882
8883 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8884 {
8885 reached = 3;
8886 break;
8887 }
8888 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8889 {
8890 /* We have reached TO_X but not in the line we want. */
8891 skip = move_it_in_display_line_to (it, to_charpos,
8892 -1, MOVE_TO_POS);
8893 if (skip == MOVE_POS_MATCH_OR_ZV)
8894 {
8895 reached = 4;
8896 break;
8897 }
8898 }
8899 }
8900 }
8901 else if (op & MOVE_TO_Y)
8902 {
8903 struct it it_backup;
8904
8905 if (it->line_wrap == WORD_WRAP)
8906 SAVE_IT (it_backup, *it, backup_data);
8907
8908 /* TO_Y specified means stop at TO_X in the line containing
8909 TO_Y---or at TO_CHARPOS if this is reached first. The
8910 problem is that we can't really tell whether the line
8911 contains TO_Y before we have completely scanned it, and
8912 this may skip past TO_X. What we do is to first scan to
8913 TO_X.
8914
8915 If TO_X is not specified, use a TO_X of zero. The reason
8916 is to make the outcome of this function more predictable.
8917 If we didn't use TO_X == 0, we would stop at the end of
8918 the line which is probably not what a caller would expect
8919 to happen. */
8920 skip = move_it_in_display_line_to
8921 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8922 (MOVE_TO_X | (op & MOVE_TO_POS)));
8923
8924 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8925 if (skip == MOVE_POS_MATCH_OR_ZV)
8926 reached = 5;
8927 else if (skip == MOVE_X_REACHED)
8928 {
8929 /* If TO_X was reached, we want to know whether TO_Y is
8930 in the line. We know this is the case if the already
8931 scanned glyphs make the line tall enough. Otherwise,
8932 we must check by scanning the rest of the line. */
8933 line_height = it->max_ascent + it->max_descent;
8934 if (to_y >= it->current_y
8935 && to_y < it->current_y + line_height)
8936 {
8937 reached = 6;
8938 break;
8939 }
8940 SAVE_IT (it_backup, *it, backup_data);
8941 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8942 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8943 op & MOVE_TO_POS);
8944 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8945 line_height = it->max_ascent + it->max_descent;
8946 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8947
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8950 {
8951 /* If TO_Y is in this line and TO_X was reached
8952 above, we scanned too far. We have to restore
8953 IT's settings to the ones before skipping. But
8954 keep the more accurate values of max_ascent and
8955 max_descent we've found while skipping the rest
8956 of the line, for the sake of callers, such as
8957 pos_visible_p, that need to know the line
8958 height. */
8959 int max_ascent = it->max_ascent;
8960 int max_descent = it->max_descent;
8961
8962 RESTORE_IT (it, &it_backup, backup_data);
8963 it->max_ascent = max_ascent;
8964 it->max_descent = max_descent;
8965 reached = 6;
8966 }
8967 else
8968 {
8969 skip = skip2;
8970 if (skip == MOVE_POS_MATCH_OR_ZV)
8971 reached = 7;
8972 }
8973 }
8974 else
8975 {
8976 /* Check whether TO_Y is in this line. */
8977 line_height = it->max_ascent + it->max_descent;
8978 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8979
8980 if (to_y >= it->current_y
8981 && to_y < it->current_y + line_height)
8982 {
8983 /* When word-wrap is on, TO_X may lie past the end
8984 of a wrapped line. Then it->current is the
8985 character on the next line, so backtrack to the
8986 space before the wrap point. */
8987 if (skip == MOVE_LINE_CONTINUED
8988 && it->line_wrap == WORD_WRAP)
8989 {
8990 int prev_x = max (it->current_x - 1, 0);
8991 RESTORE_IT (it, &it_backup, backup_data);
8992 skip = move_it_in_display_line_to
8993 (it, -1, prev_x, MOVE_TO_X);
8994 }
8995 reached = 6;
8996 }
8997 }
8998
8999 if (reached)
9000 break;
9001 }
9002 else if (BUFFERP (it->object)
9003 && (it->method == GET_FROM_BUFFER
9004 || it->method == GET_FROM_STRETCH)
9005 && IT_CHARPOS (*it) >= to_charpos
9006 /* Under bidi iteration, a call to set_iterator_to_next
9007 can scan far beyond to_charpos if the initial
9008 portion of the next line needs to be reordered. In
9009 that case, give move_it_in_display_line_to another
9010 chance below. */
9011 && !(it->bidi_p
9012 && it->bidi_it.scan_dir == -1))
9013 skip = MOVE_POS_MATCH_OR_ZV;
9014 else
9015 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9016
9017 switch (skip)
9018 {
9019 case MOVE_POS_MATCH_OR_ZV:
9020 reached = 8;
9021 goto out;
9022
9023 case MOVE_NEWLINE_OR_CR:
9024 set_iterator_to_next (it, 1);
9025 it->continuation_lines_width = 0;
9026 break;
9027
9028 case MOVE_LINE_TRUNCATED:
9029 it->continuation_lines_width = 0;
9030 reseat_at_next_visible_line_start (it, 0);
9031 if ((op & MOVE_TO_POS) != 0
9032 && IT_CHARPOS (*it) > to_charpos)
9033 {
9034 reached = 9;
9035 goto out;
9036 }
9037 break;
9038
9039 case MOVE_LINE_CONTINUED:
9040 /* For continued lines ending in a tab, some of the glyphs
9041 associated with the tab are displayed on the current
9042 line. Since it->current_x does not include these glyphs,
9043 we use it->last_visible_x instead. */
9044 if (it->c == '\t')
9045 {
9046 it->continuation_lines_width += it->last_visible_x;
9047 /* When moving by vpos, ensure that the iterator really
9048 advances to the next line (bug#847, bug#969). Fixme:
9049 do we need to do this in other circumstances? */
9050 if (it->current_x != it->last_visible_x
9051 && (op & MOVE_TO_VPOS)
9052 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9053 {
9054 line_start_x = it->current_x + it->pixel_width
9055 - it->last_visible_x;
9056 set_iterator_to_next (it, 0);
9057 }
9058 }
9059 else
9060 it->continuation_lines_width += it->current_x;
9061 break;
9062
9063 default:
9064 emacs_abort ();
9065 }
9066
9067 /* Reset/increment for the next run. */
9068 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9069 it->current_x = line_start_x;
9070 line_start_x = 0;
9071 it->hpos = 0;
9072 it->current_y += it->max_ascent + it->max_descent;
9073 ++it->vpos;
9074 last_height = it->max_ascent + it->max_descent;
9075 it->max_ascent = it->max_descent = 0;
9076 }
9077
9078 out:
9079
9080 /* On text terminals, we may stop at the end of a line in the middle
9081 of a multi-character glyph. If the glyph itself is continued,
9082 i.e. it is actually displayed on the next line, don't treat this
9083 stopping point as valid; move to the next line instead (unless
9084 that brings us offscreen). */
9085 if (!FRAME_WINDOW_P (it->f)
9086 && op & MOVE_TO_POS
9087 && IT_CHARPOS (*it) == to_charpos
9088 && it->what == IT_CHARACTER
9089 && it->nglyphs > 1
9090 && it->line_wrap == WINDOW_WRAP
9091 && it->current_x == it->last_visible_x - 1
9092 && it->c != '\n'
9093 && it->c != '\t'
9094 && it->vpos < it->w->window_end_vpos)
9095 {
9096 it->continuation_lines_width += it->current_x;
9097 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9098 it->current_y += it->max_ascent + it->max_descent;
9099 ++it->vpos;
9100 last_height = it->max_ascent + it->max_descent;
9101 }
9102
9103 if (backup_data)
9104 bidi_unshelve_cache (backup_data, 1);
9105
9106 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9107 }
9108
9109
9110 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9111
9112 If DY > 0, move IT backward at least that many pixels. DY = 0
9113 means move IT backward to the preceding line start or BEGV. This
9114 function may move over more than DY pixels if IT->current_y - DY
9115 ends up in the middle of a line; in this case IT->current_y will be
9116 set to the top of the line moved to. */
9117
9118 void
9119 move_it_vertically_backward (struct it *it, int dy)
9120 {
9121 int nlines, h;
9122 struct it it2, it3;
9123 void *it2data = NULL, *it3data = NULL;
9124 ptrdiff_t start_pos;
9125 int nchars_per_row
9126 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9127 ptrdiff_t pos_limit;
9128
9129 move_further_back:
9130 eassert (dy >= 0);
9131
9132 start_pos = IT_CHARPOS (*it);
9133
9134 /* Estimate how many newlines we must move back. */
9135 nlines = max (1, dy / default_line_pixel_height (it->w));
9136 if (it->line_wrap == TRUNCATE)
9137 pos_limit = BEGV;
9138 else
9139 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9140
9141 /* Set the iterator's position that many lines back. But don't go
9142 back more than NLINES full screen lines -- this wins a day with
9143 buffers which have very long lines. */
9144 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9145 back_to_previous_visible_line_start (it);
9146
9147 /* Reseat the iterator here. When moving backward, we don't want
9148 reseat to skip forward over invisible text, set up the iterator
9149 to deliver from overlay strings at the new position etc. So,
9150 use reseat_1 here. */
9151 reseat_1 (it, it->current.pos, 1);
9152
9153 /* We are now surely at a line start. */
9154 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9155 reordering is in effect. */
9156 it->continuation_lines_width = 0;
9157
9158 /* Move forward and see what y-distance we moved. First move to the
9159 start of the next line so that we get its height. We need this
9160 height to be able to tell whether we reached the specified
9161 y-distance. */
9162 SAVE_IT (it2, *it, it2data);
9163 it2.max_ascent = it2.max_descent = 0;
9164 do
9165 {
9166 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9167 MOVE_TO_POS | MOVE_TO_VPOS);
9168 }
9169 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9170 /* If we are in a display string which starts at START_POS,
9171 and that display string includes a newline, and we are
9172 right after that newline (i.e. at the beginning of a
9173 display line), exit the loop, because otherwise we will
9174 infloop, since move_it_to will see that it is already at
9175 START_POS and will not move. */
9176 || (it2.method == GET_FROM_STRING
9177 && IT_CHARPOS (it2) == start_pos
9178 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9179 eassert (IT_CHARPOS (*it) >= BEGV);
9180 SAVE_IT (it3, it2, it3data);
9181
9182 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9183 eassert (IT_CHARPOS (*it) >= BEGV);
9184 /* H is the actual vertical distance from the position in *IT
9185 and the starting position. */
9186 h = it2.current_y - it->current_y;
9187 /* NLINES is the distance in number of lines. */
9188 nlines = it2.vpos - it->vpos;
9189
9190 /* Correct IT's y and vpos position
9191 so that they are relative to the starting point. */
9192 it->vpos -= nlines;
9193 it->current_y -= h;
9194
9195 if (dy == 0)
9196 {
9197 /* DY == 0 means move to the start of the screen line. The
9198 value of nlines is > 0 if continuation lines were involved,
9199 or if the original IT position was at start of a line. */
9200 RESTORE_IT (it, it, it2data);
9201 if (nlines > 0)
9202 move_it_by_lines (it, nlines);
9203 /* The above code moves us to some position NLINES down,
9204 usually to its first glyph (leftmost in an L2R line), but
9205 that's not necessarily the start of the line, under bidi
9206 reordering. We want to get to the character position
9207 that is immediately after the newline of the previous
9208 line. */
9209 if (it->bidi_p
9210 && !it->continuation_lines_width
9211 && !STRINGP (it->string)
9212 && IT_CHARPOS (*it) > BEGV
9213 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9214 {
9215 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9216
9217 DEC_BOTH (cp, bp);
9218 cp = find_newline_no_quit (cp, bp, -1, NULL);
9219 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9220 }
9221 bidi_unshelve_cache (it3data, 1);
9222 }
9223 else
9224 {
9225 /* The y-position we try to reach, relative to *IT.
9226 Note that H has been subtracted in front of the if-statement. */
9227 int target_y = it->current_y + h - dy;
9228 int y0 = it3.current_y;
9229 int y1;
9230 int line_height;
9231
9232 RESTORE_IT (&it3, &it3, it3data);
9233 y1 = line_bottom_y (&it3);
9234 line_height = y1 - y0;
9235 RESTORE_IT (it, it, it2data);
9236 /* If we did not reach target_y, try to move further backward if
9237 we can. If we moved too far backward, try to move forward. */
9238 if (target_y < it->current_y
9239 /* This is heuristic. In a window that's 3 lines high, with
9240 a line height of 13 pixels each, recentering with point
9241 on the bottom line will try to move -39/2 = 19 pixels
9242 backward. Try to avoid moving into the first line. */
9243 && (it->current_y - target_y
9244 > min (window_box_height (it->w), line_height * 2 / 3))
9245 && IT_CHARPOS (*it) > BEGV)
9246 {
9247 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9248 target_y - it->current_y));
9249 dy = it->current_y - target_y;
9250 goto move_further_back;
9251 }
9252 else if (target_y >= it->current_y + line_height
9253 && IT_CHARPOS (*it) < ZV)
9254 {
9255 /* Should move forward by at least one line, maybe more.
9256
9257 Note: Calling move_it_by_lines can be expensive on
9258 terminal frames, where compute_motion is used (via
9259 vmotion) to do the job, when there are very long lines
9260 and truncate-lines is nil. That's the reason for
9261 treating terminal frames specially here. */
9262
9263 if (!FRAME_WINDOW_P (it->f))
9264 move_it_vertically (it, target_y - (it->current_y + line_height));
9265 else
9266 {
9267 do
9268 {
9269 move_it_by_lines (it, 1);
9270 }
9271 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9272 }
9273 }
9274 }
9275 }
9276
9277
9278 /* Move IT by a specified amount of pixel lines DY. DY negative means
9279 move backwards. DY = 0 means move to start of screen line. At the
9280 end, IT will be on the start of a screen line. */
9281
9282 void
9283 move_it_vertically (struct it *it, int dy)
9284 {
9285 if (dy <= 0)
9286 move_it_vertically_backward (it, -dy);
9287 else
9288 {
9289 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9290 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9291 MOVE_TO_POS | MOVE_TO_Y);
9292 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9293
9294 /* If buffer ends in ZV without a newline, move to the start of
9295 the line to satisfy the post-condition. */
9296 if (IT_CHARPOS (*it) == ZV
9297 && ZV > BEGV
9298 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9299 move_it_by_lines (it, 0);
9300 }
9301 }
9302
9303
9304 /* Move iterator IT past the end of the text line it is in. */
9305
9306 void
9307 move_it_past_eol (struct it *it)
9308 {
9309 enum move_it_result rc;
9310
9311 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9312 if (rc == MOVE_NEWLINE_OR_CR)
9313 set_iterator_to_next (it, 0);
9314 }
9315
9316
9317 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9318 negative means move up. DVPOS == 0 means move to the start of the
9319 screen line.
9320
9321 Optimization idea: If we would know that IT->f doesn't use
9322 a face with proportional font, we could be faster for
9323 truncate-lines nil. */
9324
9325 void
9326 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9327 {
9328
9329 /* The commented-out optimization uses vmotion on terminals. This
9330 gives bad results, because elements like it->what, on which
9331 callers such as pos_visible_p rely, aren't updated. */
9332 /* struct position pos;
9333 if (!FRAME_WINDOW_P (it->f))
9334 {
9335 struct text_pos textpos;
9336
9337 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9338 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9339 reseat (it, textpos, 1);
9340 it->vpos += pos.vpos;
9341 it->current_y += pos.vpos;
9342 }
9343 else */
9344
9345 if (dvpos == 0)
9346 {
9347 /* DVPOS == 0 means move to the start of the screen line. */
9348 move_it_vertically_backward (it, 0);
9349 /* Let next call to line_bottom_y calculate real line height. */
9350 last_height = 0;
9351 }
9352 else if (dvpos > 0)
9353 {
9354 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9355 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9356 {
9357 /* Only move to the next buffer position if we ended up in a
9358 string from display property, not in an overlay string
9359 (before-string or after-string). That is because the
9360 latter don't conceal the underlying buffer position, so
9361 we can ask to move the iterator to the exact position we
9362 are interested in. Note that, even if we are already at
9363 IT_CHARPOS (*it), the call below is not a no-op, as it
9364 will detect that we are at the end of the string, pop the
9365 iterator, and compute it->current_x and it->hpos
9366 correctly. */
9367 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9368 -1, -1, -1, MOVE_TO_POS);
9369 }
9370 }
9371 else
9372 {
9373 struct it it2;
9374 void *it2data = NULL;
9375 ptrdiff_t start_charpos, i;
9376 int nchars_per_row
9377 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9378 ptrdiff_t pos_limit;
9379
9380 /* Start at the beginning of the screen line containing IT's
9381 position. This may actually move vertically backwards,
9382 in case of overlays, so adjust dvpos accordingly. */
9383 dvpos += it->vpos;
9384 move_it_vertically_backward (it, 0);
9385 dvpos -= it->vpos;
9386
9387 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9388 screen lines, and reseat the iterator there. */
9389 start_charpos = IT_CHARPOS (*it);
9390 if (it->line_wrap == TRUNCATE)
9391 pos_limit = BEGV;
9392 else
9393 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9394 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9395 back_to_previous_visible_line_start (it);
9396 reseat (it, it->current.pos, 1);
9397
9398 /* Move further back if we end up in a string or an image. */
9399 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9400 {
9401 /* First try to move to start of display line. */
9402 dvpos += it->vpos;
9403 move_it_vertically_backward (it, 0);
9404 dvpos -= it->vpos;
9405 if (IT_POS_VALID_AFTER_MOVE_P (it))
9406 break;
9407 /* If start of line is still in string or image,
9408 move further back. */
9409 back_to_previous_visible_line_start (it);
9410 reseat (it, it->current.pos, 1);
9411 dvpos--;
9412 }
9413
9414 it->current_x = it->hpos = 0;
9415
9416 /* Above call may have moved too far if continuation lines
9417 are involved. Scan forward and see if it did. */
9418 SAVE_IT (it2, *it, it2data);
9419 it2.vpos = it2.current_y = 0;
9420 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9421 it->vpos -= it2.vpos;
9422 it->current_y -= it2.current_y;
9423 it->current_x = it->hpos = 0;
9424
9425 /* If we moved too far back, move IT some lines forward. */
9426 if (it2.vpos > -dvpos)
9427 {
9428 int delta = it2.vpos + dvpos;
9429
9430 RESTORE_IT (&it2, &it2, it2data);
9431 SAVE_IT (it2, *it, it2data);
9432 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9433 /* Move back again if we got too far ahead. */
9434 if (IT_CHARPOS (*it) >= start_charpos)
9435 RESTORE_IT (it, &it2, it2data);
9436 else
9437 bidi_unshelve_cache (it2data, 1);
9438 }
9439 else
9440 RESTORE_IT (it, it, it2data);
9441 }
9442 }
9443
9444 /* Return 1 if IT points into the middle of a display vector. */
9445
9446 int
9447 in_display_vector_p (struct it *it)
9448 {
9449 return (it->method == GET_FROM_DISPLAY_VECTOR
9450 && it->current.dpvec_index > 0
9451 && it->dpvec + it->current.dpvec_index != it->dpend);
9452 }
9453
9454 \f
9455 /***********************************************************************
9456 Messages
9457 ***********************************************************************/
9458
9459
9460 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9461 to *Messages*. */
9462
9463 void
9464 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9465 {
9466 Lisp_Object args[3];
9467 Lisp_Object msg, fmt;
9468 char *buffer;
9469 ptrdiff_t len;
9470 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9471 USE_SAFE_ALLOCA;
9472
9473 fmt = msg = Qnil;
9474 GCPRO4 (fmt, msg, arg1, arg2);
9475
9476 args[0] = fmt = build_string (format);
9477 args[1] = arg1;
9478 args[2] = arg2;
9479 msg = Fformat (3, args);
9480
9481 len = SBYTES (msg) + 1;
9482 buffer = SAFE_ALLOCA (len);
9483 memcpy (buffer, SDATA (msg), len);
9484
9485 message_dolog (buffer, len - 1, 1, 0);
9486 SAFE_FREE ();
9487
9488 UNGCPRO;
9489 }
9490
9491
9492 /* Output a newline in the *Messages* buffer if "needs" one. */
9493
9494 void
9495 message_log_maybe_newline (void)
9496 {
9497 if (message_log_need_newline)
9498 message_dolog ("", 0, 1, 0);
9499 }
9500
9501
9502 /* Add a string M of length NBYTES to the message log, optionally
9503 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9504 true, means interpret the contents of M as multibyte. This
9505 function calls low-level routines in order to bypass text property
9506 hooks, etc. which might not be safe to run.
9507
9508 This may GC (insert may run before/after change hooks),
9509 so the buffer M must NOT point to a Lisp string. */
9510
9511 void
9512 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9513 {
9514 const unsigned char *msg = (const unsigned char *) m;
9515
9516 if (!NILP (Vmemory_full))
9517 return;
9518
9519 if (!NILP (Vmessage_log_max))
9520 {
9521 struct buffer *oldbuf;
9522 Lisp_Object oldpoint, oldbegv, oldzv;
9523 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9524 ptrdiff_t point_at_end = 0;
9525 ptrdiff_t zv_at_end = 0;
9526 Lisp_Object old_deactivate_mark;
9527 struct gcpro gcpro1;
9528
9529 old_deactivate_mark = Vdeactivate_mark;
9530 oldbuf = current_buffer;
9531
9532 /* Ensure the Messages buffer exists, and switch to it.
9533 If we created it, set the major-mode. */
9534 {
9535 int newbuffer = 0;
9536 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9537
9538 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9539
9540 if (newbuffer
9541 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9542 call0 (intern ("messages-buffer-mode"));
9543 }
9544
9545 bset_undo_list (current_buffer, Qt);
9546 bset_cache_long_scans (current_buffer, Qnil);
9547
9548 oldpoint = message_dolog_marker1;
9549 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9550 oldbegv = message_dolog_marker2;
9551 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9552 oldzv = message_dolog_marker3;
9553 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9554 GCPRO1 (old_deactivate_mark);
9555
9556 if (PT == Z)
9557 point_at_end = 1;
9558 if (ZV == Z)
9559 zv_at_end = 1;
9560
9561 BEGV = BEG;
9562 BEGV_BYTE = BEG_BYTE;
9563 ZV = Z;
9564 ZV_BYTE = Z_BYTE;
9565 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9566
9567 /* Insert the string--maybe converting multibyte to single byte
9568 or vice versa, so that all the text fits the buffer. */
9569 if (multibyte
9570 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9571 {
9572 ptrdiff_t i;
9573 int c, char_bytes;
9574 char work[1];
9575
9576 /* Convert a multibyte string to single-byte
9577 for the *Message* buffer. */
9578 for (i = 0; i < nbytes; i += char_bytes)
9579 {
9580 c = string_char_and_length (msg + i, &char_bytes);
9581 work[0] = (ASCII_CHAR_P (c)
9582 ? c
9583 : multibyte_char_to_unibyte (c));
9584 insert_1_both (work, 1, 1, 1, 0, 0);
9585 }
9586 }
9587 else if (! multibyte
9588 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9589 {
9590 ptrdiff_t i;
9591 int c, char_bytes;
9592 unsigned char str[MAX_MULTIBYTE_LENGTH];
9593 /* Convert a single-byte string to multibyte
9594 for the *Message* buffer. */
9595 for (i = 0; i < nbytes; i++)
9596 {
9597 c = msg[i];
9598 MAKE_CHAR_MULTIBYTE (c);
9599 char_bytes = CHAR_STRING (c, str);
9600 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9601 }
9602 }
9603 else if (nbytes)
9604 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9605
9606 if (nlflag)
9607 {
9608 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9609 printmax_t dups;
9610
9611 insert_1_both ("\n", 1, 1, 1, 0, 0);
9612
9613 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9614 this_bol = PT;
9615 this_bol_byte = PT_BYTE;
9616
9617 /* See if this line duplicates the previous one.
9618 If so, combine duplicates. */
9619 if (this_bol > BEG)
9620 {
9621 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9622 prev_bol = PT;
9623 prev_bol_byte = PT_BYTE;
9624
9625 dups = message_log_check_duplicate (prev_bol_byte,
9626 this_bol_byte);
9627 if (dups)
9628 {
9629 del_range_both (prev_bol, prev_bol_byte,
9630 this_bol, this_bol_byte, 0);
9631 if (dups > 1)
9632 {
9633 char dupstr[sizeof " [ times]"
9634 + INT_STRLEN_BOUND (printmax_t)];
9635
9636 /* If you change this format, don't forget to also
9637 change message_log_check_duplicate. */
9638 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9639 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9640 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9641 }
9642 }
9643 }
9644
9645 /* If we have more than the desired maximum number of lines
9646 in the *Messages* buffer now, delete the oldest ones.
9647 This is safe because we don't have undo in this buffer. */
9648
9649 if (NATNUMP (Vmessage_log_max))
9650 {
9651 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9652 -XFASTINT (Vmessage_log_max) - 1, 0);
9653 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9654 }
9655 }
9656 BEGV = marker_position (oldbegv);
9657 BEGV_BYTE = marker_byte_position (oldbegv);
9658
9659 if (zv_at_end)
9660 {
9661 ZV = Z;
9662 ZV_BYTE = Z_BYTE;
9663 }
9664 else
9665 {
9666 ZV = marker_position (oldzv);
9667 ZV_BYTE = marker_byte_position (oldzv);
9668 }
9669
9670 if (point_at_end)
9671 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9672 else
9673 /* We can't do Fgoto_char (oldpoint) because it will run some
9674 Lisp code. */
9675 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9676 marker_byte_position (oldpoint));
9677
9678 UNGCPRO;
9679 unchain_marker (XMARKER (oldpoint));
9680 unchain_marker (XMARKER (oldbegv));
9681 unchain_marker (XMARKER (oldzv));
9682
9683 /* We called insert_1_both above with its 5th argument (PREPARE)
9684 zero, which prevents insert_1_both from calling
9685 prepare_to_modify_buffer, which in turns prevents us from
9686 incrementing windows_or_buffers_changed even if *Messages* is
9687 shown in some window. So we must manually set
9688 windows_or_buffers_changed here to make up for that. */
9689 windows_or_buffers_changed = old_windows_or_buffers_changed;
9690 bset_redisplay (current_buffer);
9691
9692 set_buffer_internal (oldbuf);
9693
9694 message_log_need_newline = !nlflag;
9695 Vdeactivate_mark = old_deactivate_mark;
9696 }
9697 }
9698
9699
9700 /* We are at the end of the buffer after just having inserted a newline.
9701 (Note: We depend on the fact we won't be crossing the gap.)
9702 Check to see if the most recent message looks a lot like the previous one.
9703 Return 0 if different, 1 if the new one should just replace it, or a
9704 value N > 1 if we should also append " [N times]". */
9705
9706 static intmax_t
9707 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9708 {
9709 ptrdiff_t i;
9710 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9711 int seen_dots = 0;
9712 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9713 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9714
9715 for (i = 0; i < len; i++)
9716 {
9717 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9718 seen_dots = 1;
9719 if (p1[i] != p2[i])
9720 return seen_dots;
9721 }
9722 p1 += len;
9723 if (*p1 == '\n')
9724 return 2;
9725 if (*p1++ == ' ' && *p1++ == '[')
9726 {
9727 char *pend;
9728 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9729 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9730 return n + 1;
9731 }
9732 return 0;
9733 }
9734 \f
9735
9736 /* Display an echo area message M with a specified length of NBYTES
9737 bytes. The string may include null characters. If M is not a
9738 string, clear out any existing message, and let the mini-buffer
9739 text show through.
9740
9741 This function cancels echoing. */
9742
9743 void
9744 message3 (Lisp_Object m)
9745 {
9746 struct gcpro gcpro1;
9747
9748 GCPRO1 (m);
9749 clear_message (1,1);
9750 cancel_echoing ();
9751
9752 /* First flush out any partial line written with print. */
9753 message_log_maybe_newline ();
9754 if (STRINGP (m))
9755 {
9756 ptrdiff_t nbytes = SBYTES (m);
9757 bool multibyte = STRING_MULTIBYTE (m);
9758 USE_SAFE_ALLOCA;
9759 char *buffer = SAFE_ALLOCA (nbytes);
9760 memcpy (buffer, SDATA (m), nbytes);
9761 message_dolog (buffer, nbytes, 1, multibyte);
9762 SAFE_FREE ();
9763 }
9764 message3_nolog (m);
9765
9766 UNGCPRO;
9767 }
9768
9769
9770 /* The non-logging version of message3.
9771 This does not cancel echoing, because it is used for echoing.
9772 Perhaps we need to make a separate function for echoing
9773 and make this cancel echoing. */
9774
9775 void
9776 message3_nolog (Lisp_Object m)
9777 {
9778 struct frame *sf = SELECTED_FRAME ();
9779
9780 if (FRAME_INITIAL_P (sf))
9781 {
9782 if (noninteractive_need_newline)
9783 putc ('\n', stderr);
9784 noninteractive_need_newline = 0;
9785 if (STRINGP (m))
9786 {
9787 Lisp_Object s = ENCODE_SYSTEM (m);
9788
9789 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9790 }
9791 if (cursor_in_echo_area == 0)
9792 fprintf (stderr, "\n");
9793 fflush (stderr);
9794 }
9795 /* Error messages get reported properly by cmd_error, so this must be just an
9796 informative message; if the frame hasn't really been initialized yet, just
9797 toss it. */
9798 else if (INTERACTIVE && sf->glyphs_initialized_p)
9799 {
9800 /* Get the frame containing the mini-buffer
9801 that the selected frame is using. */
9802 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9803 Lisp_Object frame = XWINDOW (mini_window)->frame;
9804 struct frame *f = XFRAME (frame);
9805
9806 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9807 Fmake_frame_visible (frame);
9808
9809 if (STRINGP (m) && SCHARS (m) > 0)
9810 {
9811 set_message (m);
9812 if (minibuffer_auto_raise)
9813 Fraise_frame (frame);
9814 /* Assume we are not echoing.
9815 (If we are, echo_now will override this.) */
9816 echo_message_buffer = Qnil;
9817 }
9818 else
9819 clear_message (1, 1);
9820
9821 do_pending_window_change (0);
9822 echo_area_display (1);
9823 do_pending_window_change (0);
9824 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9825 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9826 }
9827 }
9828
9829
9830 /* Display a null-terminated echo area message M. If M is 0, clear
9831 out any existing message, and let the mini-buffer text show through.
9832
9833 The buffer M must continue to exist until after the echo area gets
9834 cleared or some other message gets displayed there. Do not pass
9835 text that is stored in a Lisp string. Do not pass text in a buffer
9836 that was alloca'd. */
9837
9838 void
9839 message1 (const char *m)
9840 {
9841 message3 (m ? build_unibyte_string (m) : Qnil);
9842 }
9843
9844
9845 /* The non-logging counterpart of message1. */
9846
9847 void
9848 message1_nolog (const char *m)
9849 {
9850 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9851 }
9852
9853 /* Display a message M which contains a single %s
9854 which gets replaced with STRING. */
9855
9856 void
9857 message_with_string (const char *m, Lisp_Object string, int log)
9858 {
9859 CHECK_STRING (string);
9860
9861 if (noninteractive)
9862 {
9863 if (m)
9864 {
9865 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
9866 String whose data pointer might be passed to us in M. So
9867 we use a local copy. */
9868 char *fmt = xstrdup (m);
9869
9870 if (noninteractive_need_newline)
9871 putc ('\n', stderr);
9872 noninteractive_need_newline = 0;
9873 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
9874 if (!cursor_in_echo_area)
9875 fprintf (stderr, "\n");
9876 fflush (stderr);
9877 xfree (fmt);
9878 }
9879 }
9880 else if (INTERACTIVE)
9881 {
9882 /* The frame whose minibuffer we're going to display the message on.
9883 It may be larger than the selected frame, so we need
9884 to use its buffer, not the selected frame's buffer. */
9885 Lisp_Object mini_window;
9886 struct frame *f, *sf = SELECTED_FRAME ();
9887
9888 /* Get the frame containing the minibuffer
9889 that the selected frame is using. */
9890 mini_window = FRAME_MINIBUF_WINDOW (sf);
9891 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9892
9893 /* Error messages get reported properly by cmd_error, so this must be
9894 just an informative message; if the frame hasn't really been
9895 initialized yet, just toss it. */
9896 if (f->glyphs_initialized_p)
9897 {
9898 Lisp_Object args[2], msg;
9899 struct gcpro gcpro1, gcpro2;
9900
9901 args[0] = build_string (m);
9902 args[1] = msg = string;
9903 GCPRO2 (args[0], msg);
9904 gcpro1.nvars = 2;
9905
9906 msg = Fformat (2, args);
9907
9908 if (log)
9909 message3 (msg);
9910 else
9911 message3_nolog (msg);
9912
9913 UNGCPRO;
9914
9915 /* Print should start at the beginning of the message
9916 buffer next time. */
9917 message_buf_print = 0;
9918 }
9919 }
9920 }
9921
9922
9923 /* Dump an informative message to the minibuf. If M is 0, clear out
9924 any existing message, and let the mini-buffer text show through. */
9925
9926 static void
9927 vmessage (const char *m, va_list ap)
9928 {
9929 if (noninteractive)
9930 {
9931 if (m)
9932 {
9933 if (noninteractive_need_newline)
9934 putc ('\n', stderr);
9935 noninteractive_need_newline = 0;
9936 vfprintf (stderr, m, ap);
9937 if (cursor_in_echo_area == 0)
9938 fprintf (stderr, "\n");
9939 fflush (stderr);
9940 }
9941 }
9942 else if (INTERACTIVE)
9943 {
9944 /* The frame whose mini-buffer we're going to display the message
9945 on. It may be larger than the selected frame, so we need to
9946 use its buffer, not the selected frame's buffer. */
9947 Lisp_Object mini_window;
9948 struct frame *f, *sf = SELECTED_FRAME ();
9949
9950 /* Get the frame containing the mini-buffer
9951 that the selected frame is using. */
9952 mini_window = FRAME_MINIBUF_WINDOW (sf);
9953 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9954
9955 /* Error messages get reported properly by cmd_error, so this must be
9956 just an informative message; if the frame hasn't really been
9957 initialized yet, just toss it. */
9958 if (f->glyphs_initialized_p)
9959 {
9960 if (m)
9961 {
9962 ptrdiff_t len;
9963 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9964 char *message_buf = alloca (maxsize + 1);
9965
9966 len = doprnt (message_buf, maxsize, m, 0, ap);
9967
9968 message3 (make_string (message_buf, len));
9969 }
9970 else
9971 message1 (0);
9972
9973 /* Print should start at the beginning of the message
9974 buffer next time. */
9975 message_buf_print = 0;
9976 }
9977 }
9978 }
9979
9980 void
9981 message (const char *m, ...)
9982 {
9983 va_list ap;
9984 va_start (ap, m);
9985 vmessage (m, ap);
9986 va_end (ap);
9987 }
9988
9989
9990 #if 0
9991 /* The non-logging version of message. */
9992
9993 void
9994 message_nolog (const char *m, ...)
9995 {
9996 Lisp_Object old_log_max;
9997 va_list ap;
9998 va_start (ap, m);
9999 old_log_max = Vmessage_log_max;
10000 Vmessage_log_max = Qnil;
10001 vmessage (m, ap);
10002 Vmessage_log_max = old_log_max;
10003 va_end (ap);
10004 }
10005 #endif
10006
10007
10008 /* Display the current message in the current mini-buffer. This is
10009 only called from error handlers in process.c, and is not time
10010 critical. */
10011
10012 void
10013 update_echo_area (void)
10014 {
10015 if (!NILP (echo_area_buffer[0]))
10016 {
10017 Lisp_Object string;
10018 string = Fcurrent_message ();
10019 message3 (string);
10020 }
10021 }
10022
10023
10024 /* Make sure echo area buffers in `echo_buffers' are live.
10025 If they aren't, make new ones. */
10026
10027 static void
10028 ensure_echo_area_buffers (void)
10029 {
10030 int i;
10031
10032 for (i = 0; i < 2; ++i)
10033 if (!BUFFERP (echo_buffer[i])
10034 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10035 {
10036 char name[30];
10037 Lisp_Object old_buffer;
10038 int j;
10039
10040 old_buffer = echo_buffer[i];
10041 echo_buffer[i] = Fget_buffer_create
10042 (make_formatted_string (name, " *Echo Area %d*", i));
10043 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10044 /* to force word wrap in echo area -
10045 it was decided to postpone this*/
10046 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10047
10048 for (j = 0; j < 2; ++j)
10049 if (EQ (old_buffer, echo_area_buffer[j]))
10050 echo_area_buffer[j] = echo_buffer[i];
10051 }
10052 }
10053
10054
10055 /* Call FN with args A1..A2 with either the current or last displayed
10056 echo_area_buffer as current buffer.
10057
10058 WHICH zero means use the current message buffer
10059 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10060 from echo_buffer[] and clear it.
10061
10062 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10063 suitable buffer from echo_buffer[] and clear it.
10064
10065 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10066 that the current message becomes the last displayed one, make
10067 choose a suitable buffer for echo_area_buffer[0], and clear it.
10068
10069 Value is what FN returns. */
10070
10071 static int
10072 with_echo_area_buffer (struct window *w, int which,
10073 int (*fn) (ptrdiff_t, Lisp_Object),
10074 ptrdiff_t a1, Lisp_Object a2)
10075 {
10076 Lisp_Object buffer;
10077 int this_one, the_other, clear_buffer_p, rc;
10078 ptrdiff_t count = SPECPDL_INDEX ();
10079
10080 /* If buffers aren't live, make new ones. */
10081 ensure_echo_area_buffers ();
10082
10083 clear_buffer_p = 0;
10084
10085 if (which == 0)
10086 this_one = 0, the_other = 1;
10087 else if (which > 0)
10088 this_one = 1, the_other = 0;
10089 else
10090 {
10091 this_one = 0, the_other = 1;
10092 clear_buffer_p = 1;
10093
10094 /* We need a fresh one in case the current echo buffer equals
10095 the one containing the last displayed echo area message. */
10096 if (!NILP (echo_area_buffer[this_one])
10097 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10098 echo_area_buffer[this_one] = Qnil;
10099 }
10100
10101 /* Choose a suitable buffer from echo_buffer[] is we don't
10102 have one. */
10103 if (NILP (echo_area_buffer[this_one]))
10104 {
10105 echo_area_buffer[this_one]
10106 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10107 ? echo_buffer[the_other]
10108 : echo_buffer[this_one]);
10109 clear_buffer_p = 1;
10110 }
10111
10112 buffer = echo_area_buffer[this_one];
10113
10114 /* Don't get confused by reusing the buffer used for echoing
10115 for a different purpose. */
10116 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10117 cancel_echoing ();
10118
10119 record_unwind_protect (unwind_with_echo_area_buffer,
10120 with_echo_area_buffer_unwind_data (w));
10121
10122 /* Make the echo area buffer current. Note that for display
10123 purposes, it is not necessary that the displayed window's buffer
10124 == current_buffer, except for text property lookup. So, let's
10125 only set that buffer temporarily here without doing a full
10126 Fset_window_buffer. We must also change w->pointm, though,
10127 because otherwise an assertions in unshow_buffer fails, and Emacs
10128 aborts. */
10129 set_buffer_internal_1 (XBUFFER (buffer));
10130 if (w)
10131 {
10132 wset_buffer (w, buffer);
10133 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10134 }
10135
10136 bset_undo_list (current_buffer, Qt);
10137 bset_read_only (current_buffer, Qnil);
10138 specbind (Qinhibit_read_only, Qt);
10139 specbind (Qinhibit_modification_hooks, Qt);
10140
10141 if (clear_buffer_p && Z > BEG)
10142 del_range (BEG, Z);
10143
10144 eassert (BEGV >= BEG);
10145 eassert (ZV <= Z && ZV >= BEGV);
10146
10147 rc = fn (a1, a2);
10148
10149 eassert (BEGV >= BEG);
10150 eassert (ZV <= Z && ZV >= BEGV);
10151
10152 unbind_to (count, Qnil);
10153 return rc;
10154 }
10155
10156
10157 /* Save state that should be preserved around the call to the function
10158 FN called in with_echo_area_buffer. */
10159
10160 static Lisp_Object
10161 with_echo_area_buffer_unwind_data (struct window *w)
10162 {
10163 int i = 0;
10164 Lisp_Object vector, tmp;
10165
10166 /* Reduce consing by keeping one vector in
10167 Vwith_echo_area_save_vector. */
10168 vector = Vwith_echo_area_save_vector;
10169 Vwith_echo_area_save_vector = Qnil;
10170
10171 if (NILP (vector))
10172 vector = Fmake_vector (make_number (9), Qnil);
10173
10174 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10175 ASET (vector, i, Vdeactivate_mark); ++i;
10176 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10177
10178 if (w)
10179 {
10180 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10181 ASET (vector, i, w->contents); ++i;
10182 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10183 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10184 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10185 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10186 }
10187 else
10188 {
10189 int end = i + 6;
10190 for (; i < end; ++i)
10191 ASET (vector, i, Qnil);
10192 }
10193
10194 eassert (i == ASIZE (vector));
10195 return vector;
10196 }
10197
10198
10199 /* Restore global state from VECTOR which was created by
10200 with_echo_area_buffer_unwind_data. */
10201
10202 static void
10203 unwind_with_echo_area_buffer (Lisp_Object vector)
10204 {
10205 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10206 Vdeactivate_mark = AREF (vector, 1);
10207 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10208
10209 if (WINDOWP (AREF (vector, 3)))
10210 {
10211 struct window *w;
10212 Lisp_Object buffer;
10213
10214 w = XWINDOW (AREF (vector, 3));
10215 buffer = AREF (vector, 4);
10216
10217 wset_buffer (w, buffer);
10218 set_marker_both (w->pointm, buffer,
10219 XFASTINT (AREF (vector, 5)),
10220 XFASTINT (AREF (vector, 6)));
10221 set_marker_both (w->start, buffer,
10222 XFASTINT (AREF (vector, 7)),
10223 XFASTINT (AREF (vector, 8)));
10224 }
10225
10226 Vwith_echo_area_save_vector = vector;
10227 }
10228
10229
10230 /* Set up the echo area for use by print functions. MULTIBYTE_P
10231 non-zero means we will print multibyte. */
10232
10233 void
10234 setup_echo_area_for_printing (int multibyte_p)
10235 {
10236 /* If we can't find an echo area any more, exit. */
10237 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10238 Fkill_emacs (Qnil);
10239
10240 ensure_echo_area_buffers ();
10241
10242 if (!message_buf_print)
10243 {
10244 /* A message has been output since the last time we printed.
10245 Choose a fresh echo area buffer. */
10246 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10247 echo_area_buffer[0] = echo_buffer[1];
10248 else
10249 echo_area_buffer[0] = echo_buffer[0];
10250
10251 /* Switch to that buffer and clear it. */
10252 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10253 bset_truncate_lines (current_buffer, Qnil);
10254
10255 if (Z > BEG)
10256 {
10257 ptrdiff_t count = SPECPDL_INDEX ();
10258 specbind (Qinhibit_read_only, Qt);
10259 /* Note that undo recording is always disabled. */
10260 del_range (BEG, Z);
10261 unbind_to (count, Qnil);
10262 }
10263 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10264
10265 /* Set up the buffer for the multibyteness we need. */
10266 if (multibyte_p
10267 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10268 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10269
10270 /* Raise the frame containing the echo area. */
10271 if (minibuffer_auto_raise)
10272 {
10273 struct frame *sf = SELECTED_FRAME ();
10274 Lisp_Object mini_window;
10275 mini_window = FRAME_MINIBUF_WINDOW (sf);
10276 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10277 }
10278
10279 message_log_maybe_newline ();
10280 message_buf_print = 1;
10281 }
10282 else
10283 {
10284 if (NILP (echo_area_buffer[0]))
10285 {
10286 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10287 echo_area_buffer[0] = echo_buffer[1];
10288 else
10289 echo_area_buffer[0] = echo_buffer[0];
10290 }
10291
10292 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10293 {
10294 /* Someone switched buffers between print requests. */
10295 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10296 bset_truncate_lines (current_buffer, Qnil);
10297 }
10298 }
10299 }
10300
10301
10302 /* Display an echo area message in window W. Value is non-zero if W's
10303 height is changed. If display_last_displayed_message_p is
10304 non-zero, display the message that was last displayed, otherwise
10305 display the current message. */
10306
10307 static int
10308 display_echo_area (struct window *w)
10309 {
10310 int i, no_message_p, window_height_changed_p;
10311
10312 /* Temporarily disable garbage collections while displaying the echo
10313 area. This is done because a GC can print a message itself.
10314 That message would modify the echo area buffer's contents while a
10315 redisplay of the buffer is going on, and seriously confuse
10316 redisplay. */
10317 ptrdiff_t count = inhibit_garbage_collection ();
10318
10319 /* If there is no message, we must call display_echo_area_1
10320 nevertheless because it resizes the window. But we will have to
10321 reset the echo_area_buffer in question to nil at the end because
10322 with_echo_area_buffer will sets it to an empty buffer. */
10323 i = display_last_displayed_message_p ? 1 : 0;
10324 no_message_p = NILP (echo_area_buffer[i]);
10325
10326 window_height_changed_p
10327 = with_echo_area_buffer (w, display_last_displayed_message_p,
10328 display_echo_area_1,
10329 (intptr_t) w, Qnil);
10330
10331 if (no_message_p)
10332 echo_area_buffer[i] = Qnil;
10333
10334 unbind_to (count, Qnil);
10335 return window_height_changed_p;
10336 }
10337
10338
10339 /* Helper for display_echo_area. Display the current buffer which
10340 contains the current echo area message in window W, a mini-window,
10341 a pointer to which is passed in A1. A2..A4 are currently not used.
10342 Change the height of W so that all of the message is displayed.
10343 Value is non-zero if height of W was changed. */
10344
10345 static int
10346 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10347 {
10348 intptr_t i1 = a1;
10349 struct window *w = (struct window *) i1;
10350 Lisp_Object window;
10351 struct text_pos start;
10352 int window_height_changed_p = 0;
10353
10354 /* Do this before displaying, so that we have a large enough glyph
10355 matrix for the display. If we can't get enough space for the
10356 whole text, display the last N lines. That works by setting w->start. */
10357 window_height_changed_p = resize_mini_window (w, 0);
10358
10359 /* Use the starting position chosen by resize_mini_window. */
10360 SET_TEXT_POS_FROM_MARKER (start, w->start);
10361
10362 /* Display. */
10363 clear_glyph_matrix (w->desired_matrix);
10364 XSETWINDOW (window, w);
10365 try_window (window, start, 0);
10366
10367 return window_height_changed_p;
10368 }
10369
10370
10371 /* Resize the echo area window to exactly the size needed for the
10372 currently displayed message, if there is one. If a mini-buffer
10373 is active, don't shrink it. */
10374
10375 void
10376 resize_echo_area_exactly (void)
10377 {
10378 if (BUFFERP (echo_area_buffer[0])
10379 && WINDOWP (echo_area_window))
10380 {
10381 struct window *w = XWINDOW (echo_area_window);
10382 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10383 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10384 (intptr_t) w, resize_exactly);
10385 if (resized_p)
10386 {
10387 windows_or_buffers_changed = 42;
10388 update_mode_lines = 30;
10389 redisplay_internal ();
10390 }
10391 }
10392 }
10393
10394
10395 /* Callback function for with_echo_area_buffer, when used from
10396 resize_echo_area_exactly. A1 contains a pointer to the window to
10397 resize, EXACTLY non-nil means resize the mini-window exactly to the
10398 size of the text displayed. A3 and A4 are not used. Value is what
10399 resize_mini_window returns. */
10400
10401 static int
10402 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10403 {
10404 intptr_t i1 = a1;
10405 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10406 }
10407
10408
10409 /* Resize mini-window W to fit the size of its contents. EXACT_P
10410 means size the window exactly to the size needed. Otherwise, it's
10411 only enlarged until W's buffer is empty.
10412
10413 Set W->start to the right place to begin display. If the whole
10414 contents fit, start at the beginning. Otherwise, start so as
10415 to make the end of the contents appear. This is particularly
10416 important for y-or-n-p, but seems desirable generally.
10417
10418 Value is non-zero if the window height has been changed. */
10419
10420 int
10421 resize_mini_window (struct window *w, int exact_p)
10422 {
10423 struct frame *f = XFRAME (w->frame);
10424 int window_height_changed_p = 0;
10425
10426 eassert (MINI_WINDOW_P (w));
10427
10428 /* By default, start display at the beginning. */
10429 set_marker_both (w->start, w->contents,
10430 BUF_BEGV (XBUFFER (w->contents)),
10431 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10432
10433 /* Don't resize windows while redisplaying a window; it would
10434 confuse redisplay functions when the size of the window they are
10435 displaying changes from under them. Such a resizing can happen,
10436 for instance, when which-func prints a long message while
10437 we are running fontification-functions. We're running these
10438 functions with safe_call which binds inhibit-redisplay to t. */
10439 if (!NILP (Vinhibit_redisplay))
10440 return 0;
10441
10442 /* Nil means don't try to resize. */
10443 if (NILP (Vresize_mini_windows)
10444 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10445 return 0;
10446
10447 if (!FRAME_MINIBUF_ONLY_P (f))
10448 {
10449 struct it it;
10450 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10451 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10452 int height;
10453 EMACS_INT max_height;
10454 int unit = FRAME_LINE_HEIGHT (f);
10455 struct text_pos start;
10456 struct buffer *old_current_buffer = NULL;
10457
10458 if (current_buffer != XBUFFER (w->contents))
10459 {
10460 old_current_buffer = current_buffer;
10461 set_buffer_internal (XBUFFER (w->contents));
10462 }
10463
10464 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10465
10466 /* Compute the max. number of lines specified by the user. */
10467 if (FLOATP (Vmax_mini_window_height))
10468 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10469 else if (INTEGERP (Vmax_mini_window_height))
10470 max_height = XINT (Vmax_mini_window_height);
10471 else
10472 max_height = total_height / 4;
10473
10474 /* Correct that max. height if it's bogus. */
10475 max_height = clip_to_bounds (1, max_height, total_height);
10476
10477 /* Find out the height of the text in the window. */
10478 if (it.line_wrap == TRUNCATE)
10479 height = 1;
10480 else
10481 {
10482 last_height = 0;
10483 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10484 if (it.max_ascent == 0 && it.max_descent == 0)
10485 height = it.current_y + last_height;
10486 else
10487 height = it.current_y + it.max_ascent + it.max_descent;
10488 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10489 height = (height + unit - 1) / unit;
10490 }
10491
10492 /* Compute a suitable window start. */
10493 if (height > max_height)
10494 {
10495 height = max_height;
10496 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10497 move_it_vertically_backward (&it, (height - 1) * unit);
10498 start = it.current.pos;
10499 }
10500 else
10501 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10502 SET_MARKER_FROM_TEXT_POS (w->start, start);
10503
10504 if (EQ (Vresize_mini_windows, Qgrow_only))
10505 {
10506 /* Let it grow only, until we display an empty message, in which
10507 case the window shrinks again. */
10508 if (height > WINDOW_TOTAL_LINES (w))
10509 {
10510 int old_height = WINDOW_TOTAL_LINES (w);
10511
10512 FRAME_WINDOWS_FROZEN (f) = 1;
10513 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10514 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10515 }
10516 else if (height < WINDOW_TOTAL_LINES (w)
10517 && (exact_p || BEGV == ZV))
10518 {
10519 int old_height = WINDOW_TOTAL_LINES (w);
10520
10521 FRAME_WINDOWS_FROZEN (f) = 0;
10522 shrink_mini_window (w);
10523 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10524 }
10525 }
10526 else
10527 {
10528 /* Always resize to exact size needed. */
10529 if (height > WINDOW_TOTAL_LINES (w))
10530 {
10531 int old_height = WINDOW_TOTAL_LINES (w);
10532
10533 FRAME_WINDOWS_FROZEN (f) = 1;
10534 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10535 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10536 }
10537 else if (height < WINDOW_TOTAL_LINES (w))
10538 {
10539 int old_height = WINDOW_TOTAL_LINES (w);
10540
10541 FRAME_WINDOWS_FROZEN (f) = 0;
10542 shrink_mini_window (w);
10543
10544 if (height)
10545 {
10546 FRAME_WINDOWS_FROZEN (f) = 1;
10547 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10548 }
10549
10550 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10551 }
10552 }
10553
10554 if (old_current_buffer)
10555 set_buffer_internal (old_current_buffer);
10556 }
10557
10558 return window_height_changed_p;
10559 }
10560
10561
10562 /* Value is the current message, a string, or nil if there is no
10563 current message. */
10564
10565 Lisp_Object
10566 current_message (void)
10567 {
10568 Lisp_Object msg;
10569
10570 if (!BUFFERP (echo_area_buffer[0]))
10571 msg = Qnil;
10572 else
10573 {
10574 with_echo_area_buffer (0, 0, current_message_1,
10575 (intptr_t) &msg, Qnil);
10576 if (NILP (msg))
10577 echo_area_buffer[0] = Qnil;
10578 }
10579
10580 return msg;
10581 }
10582
10583
10584 static int
10585 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10586 {
10587 intptr_t i1 = a1;
10588 Lisp_Object *msg = (Lisp_Object *) i1;
10589
10590 if (Z > BEG)
10591 *msg = make_buffer_string (BEG, Z, 1);
10592 else
10593 *msg = Qnil;
10594 return 0;
10595 }
10596
10597
10598 /* Push the current message on Vmessage_stack for later restoration
10599 by restore_message. Value is non-zero if the current message isn't
10600 empty. This is a relatively infrequent operation, so it's not
10601 worth optimizing. */
10602
10603 bool
10604 push_message (void)
10605 {
10606 Lisp_Object msg = current_message ();
10607 Vmessage_stack = Fcons (msg, Vmessage_stack);
10608 return STRINGP (msg);
10609 }
10610
10611
10612 /* Restore message display from the top of Vmessage_stack. */
10613
10614 void
10615 restore_message (void)
10616 {
10617 eassert (CONSP (Vmessage_stack));
10618 message3_nolog (XCAR (Vmessage_stack));
10619 }
10620
10621
10622 /* Handler for unwind-protect calling pop_message. */
10623
10624 void
10625 pop_message_unwind (void)
10626 {
10627 /* Pop the top-most entry off Vmessage_stack. */
10628 eassert (CONSP (Vmessage_stack));
10629 Vmessage_stack = XCDR (Vmessage_stack);
10630 }
10631
10632
10633 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10634 exits. If the stack is not empty, we have a missing pop_message
10635 somewhere. */
10636
10637 void
10638 check_message_stack (void)
10639 {
10640 if (!NILP (Vmessage_stack))
10641 emacs_abort ();
10642 }
10643
10644
10645 /* Truncate to NCHARS what will be displayed in the echo area the next
10646 time we display it---but don't redisplay it now. */
10647
10648 void
10649 truncate_echo_area (ptrdiff_t nchars)
10650 {
10651 if (nchars == 0)
10652 echo_area_buffer[0] = Qnil;
10653 else if (!noninteractive
10654 && INTERACTIVE
10655 && !NILP (echo_area_buffer[0]))
10656 {
10657 struct frame *sf = SELECTED_FRAME ();
10658 /* Error messages get reported properly by cmd_error, so this must be
10659 just an informative message; if the frame hasn't really been
10660 initialized yet, just toss it. */
10661 if (sf->glyphs_initialized_p)
10662 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10663 }
10664 }
10665
10666
10667 /* Helper function for truncate_echo_area. Truncate the current
10668 message to at most NCHARS characters. */
10669
10670 static int
10671 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10672 {
10673 if (BEG + nchars < Z)
10674 del_range (BEG + nchars, Z);
10675 if (Z == BEG)
10676 echo_area_buffer[0] = Qnil;
10677 return 0;
10678 }
10679
10680 /* Set the current message to STRING. */
10681
10682 static void
10683 set_message (Lisp_Object string)
10684 {
10685 eassert (STRINGP (string));
10686
10687 message_enable_multibyte = STRING_MULTIBYTE (string);
10688
10689 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10690 message_buf_print = 0;
10691 help_echo_showing_p = 0;
10692
10693 if (STRINGP (Vdebug_on_message)
10694 && STRINGP (string)
10695 && fast_string_match (Vdebug_on_message, string) >= 0)
10696 call_debugger (list2 (Qerror, string));
10697 }
10698
10699
10700 /* Helper function for set_message. First argument is ignored and second
10701 argument has the same meaning as for set_message.
10702 This function is called with the echo area buffer being current. */
10703
10704 static int
10705 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10706 {
10707 eassert (STRINGP (string));
10708
10709 /* Change multibyteness of the echo buffer appropriately. */
10710 if (message_enable_multibyte
10711 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10712 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10713
10714 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10715 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10716 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10717
10718 /* Insert new message at BEG. */
10719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10720
10721 /* This function takes care of single/multibyte conversion.
10722 We just have to ensure that the echo area buffer has the right
10723 setting of enable_multibyte_characters. */
10724 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10725
10726 return 0;
10727 }
10728
10729
10730 /* Clear messages. CURRENT_P non-zero means clear the current
10731 message. LAST_DISPLAYED_P non-zero means clear the message
10732 last displayed. */
10733
10734 void
10735 clear_message (int current_p, int last_displayed_p)
10736 {
10737 if (current_p)
10738 {
10739 echo_area_buffer[0] = Qnil;
10740 message_cleared_p = 1;
10741 }
10742
10743 if (last_displayed_p)
10744 echo_area_buffer[1] = Qnil;
10745
10746 message_buf_print = 0;
10747 }
10748
10749 /* Clear garbaged frames.
10750
10751 This function is used where the old redisplay called
10752 redraw_garbaged_frames which in turn called redraw_frame which in
10753 turn called clear_frame. The call to clear_frame was a source of
10754 flickering. I believe a clear_frame is not necessary. It should
10755 suffice in the new redisplay to invalidate all current matrices,
10756 and ensure a complete redisplay of all windows. */
10757
10758 static void
10759 clear_garbaged_frames (void)
10760 {
10761 if (frame_garbaged)
10762 {
10763 Lisp_Object tail, frame;
10764
10765 FOR_EACH_FRAME (tail, frame)
10766 {
10767 struct frame *f = XFRAME (frame);
10768
10769 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10770 {
10771 if (f->resized_p)
10772 redraw_frame (f);
10773 else
10774 clear_current_matrices (f);
10775 fset_redisplay (f);
10776 f->garbaged = false;
10777 f->resized_p = false;
10778 }
10779 }
10780
10781 frame_garbaged = false;
10782 }
10783 }
10784
10785
10786 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10787 is non-zero update selected_frame. Value is non-zero if the
10788 mini-windows height has been changed. */
10789
10790 static int
10791 echo_area_display (int update_frame_p)
10792 {
10793 Lisp_Object mini_window;
10794 struct window *w;
10795 struct frame *f;
10796 int window_height_changed_p = 0;
10797 struct frame *sf = SELECTED_FRAME ();
10798
10799 mini_window = FRAME_MINIBUF_WINDOW (sf);
10800 w = XWINDOW (mini_window);
10801 f = XFRAME (WINDOW_FRAME (w));
10802
10803 /* Don't display if frame is invisible or not yet initialized. */
10804 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10805 return 0;
10806
10807 #ifdef HAVE_WINDOW_SYSTEM
10808 /* When Emacs starts, selected_frame may be the initial terminal
10809 frame. If we let this through, a message would be displayed on
10810 the terminal. */
10811 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10812 return 0;
10813 #endif /* HAVE_WINDOW_SYSTEM */
10814
10815 /* Redraw garbaged frames. */
10816 clear_garbaged_frames ();
10817
10818 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10819 {
10820 echo_area_window = mini_window;
10821 window_height_changed_p = display_echo_area (w);
10822 w->must_be_updated_p = 1;
10823
10824 /* Update the display, unless called from redisplay_internal.
10825 Also don't update the screen during redisplay itself. The
10826 update will happen at the end of redisplay, and an update
10827 here could cause confusion. */
10828 if (update_frame_p && !redisplaying_p)
10829 {
10830 int n = 0;
10831
10832 /* If the display update has been interrupted by pending
10833 input, update mode lines in the frame. Due to the
10834 pending input, it might have been that redisplay hasn't
10835 been called, so that mode lines above the echo area are
10836 garbaged. This looks odd, so we prevent it here. */
10837 if (!display_completed)
10838 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10839
10840 if (window_height_changed_p
10841 /* Don't do this if Emacs is shutting down. Redisplay
10842 needs to run hooks. */
10843 && !NILP (Vrun_hooks))
10844 {
10845 /* Must update other windows. Likewise as in other
10846 cases, don't let this update be interrupted by
10847 pending input. */
10848 ptrdiff_t count = SPECPDL_INDEX ();
10849 specbind (Qredisplay_dont_pause, Qt);
10850 windows_or_buffers_changed = 44;
10851 redisplay_internal ();
10852 unbind_to (count, Qnil);
10853 }
10854 else if (FRAME_WINDOW_P (f) && n == 0)
10855 {
10856 /* Window configuration is the same as before.
10857 Can do with a display update of the echo area,
10858 unless we displayed some mode lines. */
10859 update_single_window (w, 1);
10860 flush_frame (f);
10861 }
10862 else
10863 update_frame (f, 1, 1);
10864
10865 /* If cursor is in the echo area, make sure that the next
10866 redisplay displays the minibuffer, so that the cursor will
10867 be replaced with what the minibuffer wants. */
10868 if (cursor_in_echo_area)
10869 wset_redisplay (XWINDOW (mini_window));
10870 }
10871 }
10872 else if (!EQ (mini_window, selected_window))
10873 wset_redisplay (XWINDOW (mini_window));
10874
10875 /* Last displayed message is now the current message. */
10876 echo_area_buffer[1] = echo_area_buffer[0];
10877 /* Inform read_char that we're not echoing. */
10878 echo_message_buffer = Qnil;
10879
10880 /* Prevent redisplay optimization in redisplay_internal by resetting
10881 this_line_start_pos. This is done because the mini-buffer now
10882 displays the message instead of its buffer text. */
10883 if (EQ (mini_window, selected_window))
10884 CHARPOS (this_line_start_pos) = 0;
10885
10886 return window_height_changed_p;
10887 }
10888
10889 /* Nonzero if W's buffer was changed but not saved. */
10890
10891 static int
10892 window_buffer_changed (struct window *w)
10893 {
10894 struct buffer *b = XBUFFER (w->contents);
10895
10896 eassert (BUFFER_LIVE_P (b));
10897
10898 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
10899 }
10900
10901 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10902
10903 static int
10904 mode_line_update_needed (struct window *w)
10905 {
10906 return (w->column_number_displayed != -1
10907 && !(PT == w->last_point && !window_outdated (w))
10908 && (w->column_number_displayed != current_column ()));
10909 }
10910
10911 /* Nonzero if window start of W is frozen and may not be changed during
10912 redisplay. */
10913
10914 static bool
10915 window_frozen_p (struct window *w)
10916 {
10917 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10918 {
10919 Lisp_Object window;
10920
10921 XSETWINDOW (window, w);
10922 if (MINI_WINDOW_P (w))
10923 return 0;
10924 else if (EQ (window, selected_window))
10925 return 0;
10926 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10927 && EQ (window, Vminibuf_scroll_window))
10928 /* This special window can't be frozen too. */
10929 return 0;
10930 else
10931 return 1;
10932 }
10933 return 0;
10934 }
10935
10936 /***********************************************************************
10937 Mode Lines and Frame Titles
10938 ***********************************************************************/
10939
10940 /* A buffer for constructing non-propertized mode-line strings and
10941 frame titles in it; allocated from the heap in init_xdisp and
10942 resized as needed in store_mode_line_noprop_char. */
10943
10944 static char *mode_line_noprop_buf;
10945
10946 /* The buffer's end, and a current output position in it. */
10947
10948 static char *mode_line_noprop_buf_end;
10949 static char *mode_line_noprop_ptr;
10950
10951 #define MODE_LINE_NOPROP_LEN(start) \
10952 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10953
10954 static enum {
10955 MODE_LINE_DISPLAY = 0,
10956 MODE_LINE_TITLE,
10957 MODE_LINE_NOPROP,
10958 MODE_LINE_STRING
10959 } mode_line_target;
10960
10961 /* Alist that caches the results of :propertize.
10962 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10963 static Lisp_Object mode_line_proptrans_alist;
10964
10965 /* List of strings making up the mode-line. */
10966 static Lisp_Object mode_line_string_list;
10967
10968 /* Base face property when building propertized mode line string. */
10969 static Lisp_Object mode_line_string_face;
10970 static Lisp_Object mode_line_string_face_prop;
10971
10972
10973 /* Unwind data for mode line strings */
10974
10975 static Lisp_Object Vmode_line_unwind_vector;
10976
10977 static Lisp_Object
10978 format_mode_line_unwind_data (struct frame *target_frame,
10979 struct buffer *obuf,
10980 Lisp_Object owin,
10981 int save_proptrans)
10982 {
10983 Lisp_Object vector, tmp;
10984
10985 /* Reduce consing by keeping one vector in
10986 Vwith_echo_area_save_vector. */
10987 vector = Vmode_line_unwind_vector;
10988 Vmode_line_unwind_vector = Qnil;
10989
10990 if (NILP (vector))
10991 vector = Fmake_vector (make_number (10), Qnil);
10992
10993 ASET (vector, 0, make_number (mode_line_target));
10994 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10995 ASET (vector, 2, mode_line_string_list);
10996 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10997 ASET (vector, 4, mode_line_string_face);
10998 ASET (vector, 5, mode_line_string_face_prop);
10999
11000 if (obuf)
11001 XSETBUFFER (tmp, obuf);
11002 else
11003 tmp = Qnil;
11004 ASET (vector, 6, tmp);
11005 ASET (vector, 7, owin);
11006 if (target_frame)
11007 {
11008 /* Similarly to `with-selected-window', if the operation selects
11009 a window on another frame, we must restore that frame's
11010 selected window, and (for a tty) the top-frame. */
11011 ASET (vector, 8, target_frame->selected_window);
11012 if (FRAME_TERMCAP_P (target_frame))
11013 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11014 }
11015
11016 return vector;
11017 }
11018
11019 static void
11020 unwind_format_mode_line (Lisp_Object vector)
11021 {
11022 Lisp_Object old_window = AREF (vector, 7);
11023 Lisp_Object target_frame_window = AREF (vector, 8);
11024 Lisp_Object old_top_frame = AREF (vector, 9);
11025
11026 mode_line_target = XINT (AREF (vector, 0));
11027 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11028 mode_line_string_list = AREF (vector, 2);
11029 if (! EQ (AREF (vector, 3), Qt))
11030 mode_line_proptrans_alist = AREF (vector, 3);
11031 mode_line_string_face = AREF (vector, 4);
11032 mode_line_string_face_prop = AREF (vector, 5);
11033
11034 /* Select window before buffer, since it may change the buffer. */
11035 if (!NILP (old_window))
11036 {
11037 /* If the operation that we are unwinding had selected a window
11038 on a different frame, reset its frame-selected-window. For a
11039 text terminal, reset its top-frame if necessary. */
11040 if (!NILP (target_frame_window))
11041 {
11042 Lisp_Object frame
11043 = WINDOW_FRAME (XWINDOW (target_frame_window));
11044
11045 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11046 Fselect_window (target_frame_window, Qt);
11047
11048 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11049 Fselect_frame (old_top_frame, Qt);
11050 }
11051
11052 Fselect_window (old_window, Qt);
11053 }
11054
11055 if (!NILP (AREF (vector, 6)))
11056 {
11057 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11058 ASET (vector, 6, Qnil);
11059 }
11060
11061 Vmode_line_unwind_vector = vector;
11062 }
11063
11064
11065 /* Store a single character C for the frame title in mode_line_noprop_buf.
11066 Re-allocate mode_line_noprop_buf if necessary. */
11067
11068 static void
11069 store_mode_line_noprop_char (char c)
11070 {
11071 /* If output position has reached the end of the allocated buffer,
11072 increase the buffer's size. */
11073 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11074 {
11075 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11076 ptrdiff_t size = len;
11077 mode_line_noprop_buf =
11078 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11079 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11080 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11081 }
11082
11083 *mode_line_noprop_ptr++ = c;
11084 }
11085
11086
11087 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11088 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11089 characters that yield more columns than PRECISION; PRECISION <= 0
11090 means copy the whole string. Pad with spaces until FIELD_WIDTH
11091 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11092 pad. Called from display_mode_element when it is used to build a
11093 frame title. */
11094
11095 static int
11096 store_mode_line_noprop (const char *string, int field_width, int precision)
11097 {
11098 const unsigned char *str = (const unsigned char *) string;
11099 int n = 0;
11100 ptrdiff_t dummy, nbytes;
11101
11102 /* Copy at most PRECISION chars from STR. */
11103 nbytes = strlen (string);
11104 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11105 while (nbytes--)
11106 store_mode_line_noprop_char (*str++);
11107
11108 /* Fill up with spaces until FIELD_WIDTH reached. */
11109 while (field_width > 0
11110 && n < field_width)
11111 {
11112 store_mode_line_noprop_char (' ');
11113 ++n;
11114 }
11115
11116 return n;
11117 }
11118
11119 /***********************************************************************
11120 Frame Titles
11121 ***********************************************************************/
11122
11123 #ifdef HAVE_WINDOW_SYSTEM
11124
11125 /* Set the title of FRAME, if it has changed. The title format is
11126 Vicon_title_format if FRAME is iconified, otherwise it is
11127 frame_title_format. */
11128
11129 static void
11130 x_consider_frame_title (Lisp_Object frame)
11131 {
11132 struct frame *f = XFRAME (frame);
11133
11134 if (FRAME_WINDOW_P (f)
11135 || FRAME_MINIBUF_ONLY_P (f)
11136 || f->explicit_name)
11137 {
11138 /* Do we have more than one visible frame on this X display? */
11139 Lisp_Object tail, other_frame, fmt;
11140 ptrdiff_t title_start;
11141 char *title;
11142 ptrdiff_t len;
11143 struct it it;
11144 ptrdiff_t count = SPECPDL_INDEX ();
11145
11146 FOR_EACH_FRAME (tail, other_frame)
11147 {
11148 struct frame *tf = XFRAME (other_frame);
11149
11150 if (tf != f
11151 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11152 && !FRAME_MINIBUF_ONLY_P (tf)
11153 && !EQ (other_frame, tip_frame)
11154 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11155 break;
11156 }
11157
11158 /* Set global variable indicating that multiple frames exist. */
11159 multiple_frames = CONSP (tail);
11160
11161 /* Switch to the buffer of selected window of the frame. Set up
11162 mode_line_target so that display_mode_element will output into
11163 mode_line_noprop_buf; then display the title. */
11164 record_unwind_protect (unwind_format_mode_line,
11165 format_mode_line_unwind_data
11166 (f, current_buffer, selected_window, 0));
11167
11168 Fselect_window (f->selected_window, Qt);
11169 set_buffer_internal_1
11170 (XBUFFER (XWINDOW (f->selected_window)->contents));
11171 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11172
11173 mode_line_target = MODE_LINE_TITLE;
11174 title_start = MODE_LINE_NOPROP_LEN (0);
11175 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11176 NULL, DEFAULT_FACE_ID);
11177 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11178 len = MODE_LINE_NOPROP_LEN (title_start);
11179 title = mode_line_noprop_buf + title_start;
11180 unbind_to (count, Qnil);
11181
11182 /* Set the title only if it's changed. This avoids consing in
11183 the common case where it hasn't. (If it turns out that we've
11184 already wasted too much time by walking through the list with
11185 display_mode_element, then we might need to optimize at a
11186 higher level than this.) */
11187 if (! STRINGP (f->name)
11188 || SBYTES (f->name) != len
11189 || memcmp (title, SDATA (f->name), len) != 0)
11190 x_implicitly_set_name (f, make_string (title, len), Qnil);
11191 }
11192 }
11193
11194 #endif /* not HAVE_WINDOW_SYSTEM */
11195
11196 \f
11197 /***********************************************************************
11198 Menu Bars
11199 ***********************************************************************/
11200
11201 /* Non-zero if we will not redisplay all visible windows. */
11202 #define REDISPLAY_SOME_P() \
11203 ((windows_or_buffers_changed == 0 \
11204 || windows_or_buffers_changed == REDISPLAY_SOME) \
11205 && (update_mode_lines == 0 \
11206 || update_mode_lines == REDISPLAY_SOME))
11207
11208 /* Prepare for redisplay by updating menu-bar item lists when
11209 appropriate. This can call eval. */
11210
11211 static void
11212 prepare_menu_bars (void)
11213 {
11214 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11215 bool some_windows = REDISPLAY_SOME_P ();
11216 struct gcpro gcpro1, gcpro2;
11217 Lisp_Object tooltip_frame;
11218
11219 #ifdef HAVE_WINDOW_SYSTEM
11220 tooltip_frame = tip_frame;
11221 #else
11222 tooltip_frame = Qnil;
11223 #endif
11224
11225 if (FUNCTIONP (Vpre_redisplay_function))
11226 {
11227 Lisp_Object windows = all_windows ? Qt : Qnil;
11228 if (all_windows && some_windows)
11229 {
11230 Lisp_Object ws = window_list ();
11231 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11232 {
11233 Lisp_Object this = XCAR (ws);
11234 struct window *w = XWINDOW (this);
11235 if (w->redisplay
11236 || XFRAME (w->frame)->redisplay
11237 || XBUFFER (w->contents)->text->redisplay)
11238 {
11239 windows = Fcons (this, windows);
11240 }
11241 }
11242 }
11243 safe_call1 (Vpre_redisplay_function, windows);
11244 }
11245
11246 /* Update all frame titles based on their buffer names, etc. We do
11247 this before the menu bars so that the buffer-menu will show the
11248 up-to-date frame titles. */
11249 #ifdef HAVE_WINDOW_SYSTEM
11250 if (all_windows)
11251 {
11252 Lisp_Object tail, frame;
11253
11254 FOR_EACH_FRAME (tail, frame)
11255 {
11256 struct frame *f = XFRAME (frame);
11257 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11258 if (some_windows
11259 && !f->redisplay
11260 && !w->redisplay
11261 && !XBUFFER (w->contents)->text->redisplay)
11262 continue;
11263
11264 if (!EQ (frame, tooltip_frame)
11265 && (FRAME_ICONIFIED_P (f)
11266 || FRAME_VISIBLE_P (f) == 1
11267 /* Exclude TTY frames that are obscured because they
11268 are not the top frame on their console. This is
11269 because x_consider_frame_title actually switches
11270 to the frame, which for TTY frames means it is
11271 marked as garbaged, and will be completely
11272 redrawn on the next redisplay cycle. This causes
11273 TTY frames to be completely redrawn, when there
11274 are more than one of them, even though nothing
11275 should be changed on display. */
11276 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11277 x_consider_frame_title (frame);
11278 }
11279 }
11280 #endif /* HAVE_WINDOW_SYSTEM */
11281
11282 /* Update the menu bar item lists, if appropriate. This has to be
11283 done before any actual redisplay or generation of display lines. */
11284
11285 if (all_windows)
11286 {
11287 Lisp_Object tail, frame;
11288 ptrdiff_t count = SPECPDL_INDEX ();
11289 /* 1 means that update_menu_bar has run its hooks
11290 so any further calls to update_menu_bar shouldn't do so again. */
11291 int menu_bar_hooks_run = 0;
11292
11293 record_unwind_save_match_data ();
11294
11295 FOR_EACH_FRAME (tail, frame)
11296 {
11297 struct frame *f = XFRAME (frame);
11298 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11299
11300 /* Ignore tooltip frame. */
11301 if (EQ (frame, tooltip_frame))
11302 continue;
11303
11304 if (some_windows
11305 && !f->redisplay
11306 && !w->redisplay
11307 && !XBUFFER (w->contents)->text->redisplay)
11308 continue;
11309
11310 /* If a window on this frame changed size, report that to
11311 the user and clear the size-change flag. */
11312 if (FRAME_WINDOW_SIZES_CHANGED (f))
11313 {
11314 Lisp_Object functions;
11315
11316 /* Clear flag first in case we get an error below. */
11317 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11318 functions = Vwindow_size_change_functions;
11319 GCPRO2 (tail, functions);
11320
11321 while (CONSP (functions))
11322 {
11323 if (!EQ (XCAR (functions), Qt))
11324 call1 (XCAR (functions), frame);
11325 functions = XCDR (functions);
11326 }
11327 UNGCPRO;
11328 }
11329
11330 GCPRO1 (tail);
11331 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11332 #ifdef HAVE_WINDOW_SYSTEM
11333 update_tool_bar (f, 0);
11334 #endif
11335 #ifdef HAVE_NS
11336 if (windows_or_buffers_changed
11337 && FRAME_NS_P (f))
11338 ns_set_doc_edited
11339 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11340 #endif
11341 UNGCPRO;
11342 }
11343
11344 unbind_to (count, Qnil);
11345 }
11346 else
11347 {
11348 struct frame *sf = SELECTED_FRAME ();
11349 update_menu_bar (sf, 1, 0);
11350 #ifdef HAVE_WINDOW_SYSTEM
11351 update_tool_bar (sf, 1);
11352 #endif
11353 }
11354 }
11355
11356
11357 /* Update the menu bar item list for frame F. This has to be done
11358 before we start to fill in any display lines, because it can call
11359 eval.
11360
11361 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11362
11363 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11364 already ran the menu bar hooks for this redisplay, so there
11365 is no need to run them again. The return value is the
11366 updated value of this flag, to pass to the next call. */
11367
11368 static int
11369 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11370 {
11371 Lisp_Object window;
11372 register struct window *w;
11373
11374 /* If called recursively during a menu update, do nothing. This can
11375 happen when, for instance, an activate-menubar-hook causes a
11376 redisplay. */
11377 if (inhibit_menubar_update)
11378 return hooks_run;
11379
11380 window = FRAME_SELECTED_WINDOW (f);
11381 w = XWINDOW (window);
11382
11383 if (FRAME_WINDOW_P (f)
11384 ?
11385 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11386 || defined (HAVE_NS) || defined (USE_GTK)
11387 FRAME_EXTERNAL_MENU_BAR (f)
11388 #else
11389 FRAME_MENU_BAR_LINES (f) > 0
11390 #endif
11391 : FRAME_MENU_BAR_LINES (f) > 0)
11392 {
11393 /* If the user has switched buffers or windows, we need to
11394 recompute to reflect the new bindings. But we'll
11395 recompute when update_mode_lines is set too; that means
11396 that people can use force-mode-line-update to request
11397 that the menu bar be recomputed. The adverse effect on
11398 the rest of the redisplay algorithm is about the same as
11399 windows_or_buffers_changed anyway. */
11400 if (windows_or_buffers_changed
11401 /* This used to test w->update_mode_line, but we believe
11402 there is no need to recompute the menu in that case. */
11403 || update_mode_lines
11404 || window_buffer_changed (w))
11405 {
11406 struct buffer *prev = current_buffer;
11407 ptrdiff_t count = SPECPDL_INDEX ();
11408
11409 specbind (Qinhibit_menubar_update, Qt);
11410
11411 set_buffer_internal_1 (XBUFFER (w->contents));
11412 if (save_match_data)
11413 record_unwind_save_match_data ();
11414 if (NILP (Voverriding_local_map_menu_flag))
11415 {
11416 specbind (Qoverriding_terminal_local_map, Qnil);
11417 specbind (Qoverriding_local_map, Qnil);
11418 }
11419
11420 if (!hooks_run)
11421 {
11422 /* Run the Lucid hook. */
11423 safe_run_hooks (Qactivate_menubar_hook);
11424
11425 /* If it has changed current-menubar from previous value,
11426 really recompute the menu-bar from the value. */
11427 if (! NILP (Vlucid_menu_bar_dirty_flag))
11428 call0 (Qrecompute_lucid_menubar);
11429
11430 safe_run_hooks (Qmenu_bar_update_hook);
11431
11432 hooks_run = 1;
11433 }
11434
11435 XSETFRAME (Vmenu_updating_frame, f);
11436 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11437
11438 /* Redisplay the menu bar in case we changed it. */
11439 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11440 || defined (HAVE_NS) || defined (USE_GTK)
11441 if (FRAME_WINDOW_P (f))
11442 {
11443 #if defined (HAVE_NS)
11444 /* All frames on Mac OS share the same menubar. So only
11445 the selected frame should be allowed to set it. */
11446 if (f == SELECTED_FRAME ())
11447 #endif
11448 set_frame_menubar (f, 0, 0);
11449 }
11450 else
11451 /* On a terminal screen, the menu bar is an ordinary screen
11452 line, and this makes it get updated. */
11453 w->update_mode_line = 1;
11454 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11455 /* In the non-toolkit version, the menu bar is an ordinary screen
11456 line, and this makes it get updated. */
11457 w->update_mode_line = 1;
11458 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11459
11460 unbind_to (count, Qnil);
11461 set_buffer_internal_1 (prev);
11462 }
11463 }
11464
11465 return hooks_run;
11466 }
11467
11468 /***********************************************************************
11469 Tool-bars
11470 ***********************************************************************/
11471
11472 #ifdef HAVE_WINDOW_SYSTEM
11473
11474 /* Tool-bar item index of the item on which a mouse button was pressed
11475 or -1. */
11476
11477 int last_tool_bar_item;
11478
11479 /* Select `frame' temporarily without running all the code in
11480 do_switch_frame.
11481 FIXME: Maybe do_switch_frame should be trimmed down similarly
11482 when `norecord' is set. */
11483 static void
11484 fast_set_selected_frame (Lisp_Object frame)
11485 {
11486 if (!EQ (selected_frame, frame))
11487 {
11488 selected_frame = frame;
11489 selected_window = XFRAME (frame)->selected_window;
11490 }
11491 }
11492
11493 /* Update the tool-bar item list for frame F. This has to be done
11494 before we start to fill in any display lines. Called from
11495 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11496 and restore it here. */
11497
11498 static void
11499 update_tool_bar (struct frame *f, int save_match_data)
11500 {
11501 #if defined (USE_GTK) || defined (HAVE_NS)
11502 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11503 #else
11504 int do_update = WINDOWP (f->tool_bar_window)
11505 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11506 #endif
11507
11508 if (do_update)
11509 {
11510 Lisp_Object window;
11511 struct window *w;
11512
11513 window = FRAME_SELECTED_WINDOW (f);
11514 w = XWINDOW (window);
11515
11516 /* If the user has switched buffers or windows, we need to
11517 recompute to reflect the new bindings. But we'll
11518 recompute when update_mode_lines is set too; that means
11519 that people can use force-mode-line-update to request
11520 that the menu bar be recomputed. The adverse effect on
11521 the rest of the redisplay algorithm is about the same as
11522 windows_or_buffers_changed anyway. */
11523 if (windows_or_buffers_changed
11524 || w->update_mode_line
11525 || update_mode_lines
11526 || window_buffer_changed (w))
11527 {
11528 struct buffer *prev = current_buffer;
11529 ptrdiff_t count = SPECPDL_INDEX ();
11530 Lisp_Object frame, new_tool_bar;
11531 int new_n_tool_bar;
11532 struct gcpro gcpro1;
11533
11534 /* Set current_buffer to the buffer of the selected
11535 window of the frame, so that we get the right local
11536 keymaps. */
11537 set_buffer_internal_1 (XBUFFER (w->contents));
11538
11539 /* Save match data, if we must. */
11540 if (save_match_data)
11541 record_unwind_save_match_data ();
11542
11543 /* Make sure that we don't accidentally use bogus keymaps. */
11544 if (NILP (Voverriding_local_map_menu_flag))
11545 {
11546 specbind (Qoverriding_terminal_local_map, Qnil);
11547 specbind (Qoverriding_local_map, Qnil);
11548 }
11549
11550 GCPRO1 (new_tool_bar);
11551
11552 /* We must temporarily set the selected frame to this frame
11553 before calling tool_bar_items, because the calculation of
11554 the tool-bar keymap uses the selected frame (see
11555 `tool-bar-make-keymap' in tool-bar.el). */
11556 eassert (EQ (selected_window,
11557 /* Since we only explicitly preserve selected_frame,
11558 check that selected_window would be redundant. */
11559 XFRAME (selected_frame)->selected_window));
11560 record_unwind_protect (fast_set_selected_frame, selected_frame);
11561 XSETFRAME (frame, f);
11562 fast_set_selected_frame (frame);
11563
11564 /* Build desired tool-bar items from keymaps. */
11565 new_tool_bar
11566 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11567 &new_n_tool_bar);
11568
11569 /* Redisplay the tool-bar if we changed it. */
11570 if (new_n_tool_bar != f->n_tool_bar_items
11571 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11572 {
11573 /* Redisplay that happens asynchronously due to an expose event
11574 may access f->tool_bar_items. Make sure we update both
11575 variables within BLOCK_INPUT so no such event interrupts. */
11576 block_input ();
11577 fset_tool_bar_items (f, new_tool_bar);
11578 f->n_tool_bar_items = new_n_tool_bar;
11579 w->update_mode_line = 1;
11580 unblock_input ();
11581 }
11582
11583 UNGCPRO;
11584
11585 unbind_to (count, Qnil);
11586 set_buffer_internal_1 (prev);
11587 }
11588 }
11589 }
11590
11591 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11592
11593 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11594 F's desired tool-bar contents. F->tool_bar_items must have
11595 been set up previously by calling prepare_menu_bars. */
11596
11597 static void
11598 build_desired_tool_bar_string (struct frame *f)
11599 {
11600 int i, size, size_needed;
11601 struct gcpro gcpro1, gcpro2, gcpro3;
11602 Lisp_Object image, plist, props;
11603
11604 image = plist = props = Qnil;
11605 GCPRO3 (image, plist, props);
11606
11607 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11608 Otherwise, make a new string. */
11609
11610 /* The size of the string we might be able to reuse. */
11611 size = (STRINGP (f->desired_tool_bar_string)
11612 ? SCHARS (f->desired_tool_bar_string)
11613 : 0);
11614
11615 /* We need one space in the string for each image. */
11616 size_needed = f->n_tool_bar_items;
11617
11618 /* Reuse f->desired_tool_bar_string, if possible. */
11619 if (size < size_needed || NILP (f->desired_tool_bar_string))
11620 fset_desired_tool_bar_string
11621 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11622 else
11623 {
11624 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11625 Fremove_text_properties (make_number (0), make_number (size),
11626 props, f->desired_tool_bar_string);
11627 }
11628
11629 /* Put a `display' property on the string for the images to display,
11630 put a `menu_item' property on tool-bar items with a value that
11631 is the index of the item in F's tool-bar item vector. */
11632 for (i = 0; i < f->n_tool_bar_items; ++i)
11633 {
11634 #define PROP(IDX) \
11635 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11636
11637 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11638 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11639 int hmargin, vmargin, relief, idx, end;
11640
11641 /* If image is a vector, choose the image according to the
11642 button state. */
11643 image = PROP (TOOL_BAR_ITEM_IMAGES);
11644 if (VECTORP (image))
11645 {
11646 if (enabled_p)
11647 idx = (selected_p
11648 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11649 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11650 else
11651 idx = (selected_p
11652 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11653 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11654
11655 eassert (ASIZE (image) >= idx);
11656 image = AREF (image, idx);
11657 }
11658 else
11659 idx = -1;
11660
11661 /* Ignore invalid image specifications. */
11662 if (!valid_image_p (image))
11663 continue;
11664
11665 /* Display the tool-bar button pressed, or depressed. */
11666 plist = Fcopy_sequence (XCDR (image));
11667
11668 /* Compute margin and relief to draw. */
11669 relief = (tool_bar_button_relief >= 0
11670 ? tool_bar_button_relief
11671 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11672 hmargin = vmargin = relief;
11673
11674 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11675 INT_MAX - max (hmargin, vmargin)))
11676 {
11677 hmargin += XFASTINT (Vtool_bar_button_margin);
11678 vmargin += XFASTINT (Vtool_bar_button_margin);
11679 }
11680 else if (CONSP (Vtool_bar_button_margin))
11681 {
11682 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11683 INT_MAX - hmargin))
11684 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11685
11686 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11687 INT_MAX - vmargin))
11688 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11689 }
11690
11691 if (auto_raise_tool_bar_buttons_p)
11692 {
11693 /* Add a `:relief' property to the image spec if the item is
11694 selected. */
11695 if (selected_p)
11696 {
11697 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11698 hmargin -= relief;
11699 vmargin -= relief;
11700 }
11701 }
11702 else
11703 {
11704 /* If image is selected, display it pressed, i.e. with a
11705 negative relief. If it's not selected, display it with a
11706 raised relief. */
11707 plist = Fplist_put (plist, QCrelief,
11708 (selected_p
11709 ? make_number (-relief)
11710 : make_number (relief)));
11711 hmargin -= relief;
11712 vmargin -= relief;
11713 }
11714
11715 /* Put a margin around the image. */
11716 if (hmargin || vmargin)
11717 {
11718 if (hmargin == vmargin)
11719 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11720 else
11721 plist = Fplist_put (plist, QCmargin,
11722 Fcons (make_number (hmargin),
11723 make_number (vmargin)));
11724 }
11725
11726 /* If button is not enabled, and we don't have special images
11727 for the disabled state, make the image appear disabled by
11728 applying an appropriate algorithm to it. */
11729 if (!enabled_p && idx < 0)
11730 plist = Fplist_put (plist, QCconversion, Qdisabled);
11731
11732 /* Put a `display' text property on the string for the image to
11733 display. Put a `menu-item' property on the string that gives
11734 the start of this item's properties in the tool-bar items
11735 vector. */
11736 image = Fcons (Qimage, plist);
11737 props = list4 (Qdisplay, image,
11738 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11739
11740 /* Let the last image hide all remaining spaces in the tool bar
11741 string. The string can be longer than needed when we reuse a
11742 previous string. */
11743 if (i + 1 == f->n_tool_bar_items)
11744 end = SCHARS (f->desired_tool_bar_string);
11745 else
11746 end = i + 1;
11747 Fadd_text_properties (make_number (i), make_number (end),
11748 props, f->desired_tool_bar_string);
11749 #undef PROP
11750 }
11751
11752 UNGCPRO;
11753 }
11754
11755
11756 /* Display one line of the tool-bar of frame IT->f.
11757
11758 HEIGHT specifies the desired height of the tool-bar line.
11759 If the actual height of the glyph row is less than HEIGHT, the
11760 row's height is increased to HEIGHT, and the icons are centered
11761 vertically in the new height.
11762
11763 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11764 count a final empty row in case the tool-bar width exactly matches
11765 the window width.
11766 */
11767
11768 static void
11769 display_tool_bar_line (struct it *it, int height)
11770 {
11771 struct glyph_row *row = it->glyph_row;
11772 int max_x = it->last_visible_x;
11773 struct glyph *last;
11774
11775 prepare_desired_row (row);
11776 row->y = it->current_y;
11777
11778 /* Note that this isn't made use of if the face hasn't a box,
11779 so there's no need to check the face here. */
11780 it->start_of_box_run_p = 1;
11781
11782 while (it->current_x < max_x)
11783 {
11784 int x, n_glyphs_before, i, nglyphs;
11785 struct it it_before;
11786
11787 /* Get the next display element. */
11788 if (!get_next_display_element (it))
11789 {
11790 /* Don't count empty row if we are counting needed tool-bar lines. */
11791 if (height < 0 && !it->hpos)
11792 return;
11793 break;
11794 }
11795
11796 /* Produce glyphs. */
11797 n_glyphs_before = row->used[TEXT_AREA];
11798 it_before = *it;
11799
11800 PRODUCE_GLYPHS (it);
11801
11802 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11803 i = 0;
11804 x = it_before.current_x;
11805 while (i < nglyphs)
11806 {
11807 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11808
11809 if (x + glyph->pixel_width > max_x)
11810 {
11811 /* Glyph doesn't fit on line. Backtrack. */
11812 row->used[TEXT_AREA] = n_glyphs_before;
11813 *it = it_before;
11814 /* If this is the only glyph on this line, it will never fit on the
11815 tool-bar, so skip it. But ensure there is at least one glyph,
11816 so we don't accidentally disable the tool-bar. */
11817 if (n_glyphs_before == 0
11818 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11819 break;
11820 goto out;
11821 }
11822
11823 ++it->hpos;
11824 x += glyph->pixel_width;
11825 ++i;
11826 }
11827
11828 /* Stop at line end. */
11829 if (ITERATOR_AT_END_OF_LINE_P (it))
11830 break;
11831
11832 set_iterator_to_next (it, 1);
11833 }
11834
11835 out:;
11836
11837 row->displays_text_p = row->used[TEXT_AREA] != 0;
11838
11839 /* Use default face for the border below the tool bar.
11840
11841 FIXME: When auto-resize-tool-bars is grow-only, there is
11842 no additional border below the possibly empty tool-bar lines.
11843 So to make the extra empty lines look "normal", we have to
11844 use the tool-bar face for the border too. */
11845 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11846 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11847 it->face_id = DEFAULT_FACE_ID;
11848
11849 extend_face_to_end_of_line (it);
11850 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11851 last->right_box_line_p = 1;
11852 if (last == row->glyphs[TEXT_AREA])
11853 last->left_box_line_p = 1;
11854
11855 /* Make line the desired height and center it vertically. */
11856 if ((height -= it->max_ascent + it->max_descent) > 0)
11857 {
11858 /* Don't add more than one line height. */
11859 height %= FRAME_LINE_HEIGHT (it->f);
11860 it->max_ascent += height / 2;
11861 it->max_descent += (height + 1) / 2;
11862 }
11863
11864 compute_line_metrics (it);
11865
11866 /* If line is empty, make it occupy the rest of the tool-bar. */
11867 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11868 {
11869 row->height = row->phys_height = it->last_visible_y - row->y;
11870 row->visible_height = row->height;
11871 row->ascent = row->phys_ascent = 0;
11872 row->extra_line_spacing = 0;
11873 }
11874
11875 row->full_width_p = 1;
11876 row->continued_p = 0;
11877 row->truncated_on_left_p = 0;
11878 row->truncated_on_right_p = 0;
11879
11880 it->current_x = it->hpos = 0;
11881 it->current_y += row->height;
11882 ++it->vpos;
11883 ++it->glyph_row;
11884 }
11885
11886
11887 /* Max tool-bar height. */
11888
11889 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11890 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11891
11892 /* Value is the number of screen lines needed to make all tool-bar
11893 items of frame F visible. The number of actual rows needed is
11894 returned in *N_ROWS if non-NULL. */
11895
11896 static int
11897 tool_bar_lines_needed (struct frame *f, int *n_rows)
11898 {
11899 struct window *w = XWINDOW (f->tool_bar_window);
11900 struct it it;
11901 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11902 the desired matrix, so use (unused) mode-line row as temporary row to
11903 avoid destroying the first tool-bar row. */
11904 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11905
11906 /* Initialize an iterator for iteration over
11907 F->desired_tool_bar_string in the tool-bar window of frame F. */
11908 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11909 it.first_visible_x = 0;
11910 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11911 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11912 it.paragraph_embedding = L2R;
11913
11914 while (!ITERATOR_AT_END_P (&it))
11915 {
11916 clear_glyph_row (temp_row);
11917 it.glyph_row = temp_row;
11918 display_tool_bar_line (&it, -1);
11919 }
11920 clear_glyph_row (temp_row);
11921
11922 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11923 if (n_rows)
11924 *n_rows = it.vpos > 0 ? it.vpos : -1;
11925
11926 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11927 }
11928
11929 #endif /* !USE_GTK && !HAVE_NS */
11930
11931 #if defined USE_GTK || defined HAVE_NS
11932 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11933 #endif
11934
11935 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11936 0, 1, 0,
11937 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11938 If FRAME is nil or omitted, use the selected frame. */)
11939 (Lisp_Object frame)
11940 {
11941 int nlines = 0;
11942 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11943 struct frame *f = decode_any_frame (frame);
11944 struct window *w;
11945
11946 if (WINDOWP (f->tool_bar_window)
11947 && (w = XWINDOW (f->tool_bar_window),
11948 WINDOW_TOTAL_LINES (w) > 0))
11949 {
11950 update_tool_bar (f, 1);
11951 if (f->n_tool_bar_items)
11952 {
11953 build_desired_tool_bar_string (f);
11954 nlines = tool_bar_lines_needed (f, NULL);
11955 }
11956 }
11957 #endif
11958 return make_number (nlines);
11959 }
11960
11961
11962 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11963 height should be changed. */
11964
11965 static int
11966 redisplay_tool_bar (struct frame *f)
11967 {
11968 #if defined (USE_GTK) || defined (HAVE_NS)
11969
11970 if (FRAME_EXTERNAL_TOOL_BAR (f))
11971 update_frame_tool_bar (f);
11972 return 0;
11973
11974 #else /* !USE_GTK && !HAVE_NS */
11975
11976 struct window *w;
11977 struct it it;
11978 struct glyph_row *row;
11979
11980 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11981 do anything. This means you must start with tool-bar-lines
11982 non-zero to get the auto-sizing effect. Or in other words, you
11983 can turn off tool-bars by specifying tool-bar-lines zero. */
11984 if (!WINDOWP (f->tool_bar_window)
11985 || (w = XWINDOW (f->tool_bar_window),
11986 WINDOW_TOTAL_LINES (w) == 0))
11987 return 0;
11988
11989 /* Set up an iterator for the tool-bar window. */
11990 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11991 it.first_visible_x = 0;
11992 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11993 row = it.glyph_row;
11994
11995 /* Build a string that represents the contents of the tool-bar. */
11996 build_desired_tool_bar_string (f);
11997 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11998 /* FIXME: This should be controlled by a user option. But it
11999 doesn't make sense to have an R2L tool bar if the menu bar cannot
12000 be drawn also R2L, and making the menu bar R2L is tricky due
12001 toolkit-specific code that implements it. If an R2L tool bar is
12002 ever supported, display_tool_bar_line should also be augmented to
12003 call unproduce_glyphs like display_line and display_string
12004 do. */
12005 it.paragraph_embedding = L2R;
12006
12007 if (f->n_tool_bar_rows == 0)
12008 {
12009 int nlines;
12010
12011 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12012 nlines != WINDOW_TOTAL_LINES (w)))
12013 {
12014 Lisp_Object frame;
12015 int old_height = WINDOW_TOTAL_LINES (w);
12016
12017 XSETFRAME (frame, f);
12018 Fmodify_frame_parameters (frame,
12019 list1 (Fcons (Qtool_bar_lines,
12020 make_number (nlines))));
12021 if (WINDOW_TOTAL_LINES (w) != old_height)
12022 {
12023 clear_glyph_matrix (w->desired_matrix);
12024 f->fonts_changed = 1;
12025 return 1;
12026 }
12027 }
12028 }
12029
12030 /* Display as many lines as needed to display all tool-bar items. */
12031
12032 if (f->n_tool_bar_rows > 0)
12033 {
12034 int border, rows, height, extra;
12035
12036 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12037 border = XINT (Vtool_bar_border);
12038 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12039 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12040 else if (EQ (Vtool_bar_border, Qborder_width))
12041 border = f->border_width;
12042 else
12043 border = 0;
12044 if (border < 0)
12045 border = 0;
12046
12047 rows = f->n_tool_bar_rows;
12048 height = max (1, (it.last_visible_y - border) / rows);
12049 extra = it.last_visible_y - border - height * rows;
12050
12051 while (it.current_y < it.last_visible_y)
12052 {
12053 int h = 0;
12054 if (extra > 0 && rows-- > 0)
12055 {
12056 h = (extra + rows - 1) / rows;
12057 extra -= h;
12058 }
12059 display_tool_bar_line (&it, height + h);
12060 }
12061 }
12062 else
12063 {
12064 while (it.current_y < it.last_visible_y)
12065 display_tool_bar_line (&it, 0);
12066 }
12067
12068 /* It doesn't make much sense to try scrolling in the tool-bar
12069 window, so don't do it. */
12070 w->desired_matrix->no_scrolling_p = 1;
12071 w->must_be_updated_p = 1;
12072
12073 if (!NILP (Vauto_resize_tool_bars))
12074 {
12075 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12076 int change_height_p = 0;
12077
12078 /* If we couldn't display everything, change the tool-bar's
12079 height if there is room for more. */
12080 if (IT_STRING_CHARPOS (it) < it.end_charpos
12081 && it.current_y < max_tool_bar_height)
12082 change_height_p = 1;
12083
12084 row = it.glyph_row - 1;
12085
12086 /* If there are blank lines at the end, except for a partially
12087 visible blank line at the end that is smaller than
12088 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12089 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12090 && row->height >= FRAME_LINE_HEIGHT (f))
12091 change_height_p = 1;
12092
12093 /* If row displays tool-bar items, but is partially visible,
12094 change the tool-bar's height. */
12095 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12096 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12097 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12098 change_height_p = 1;
12099
12100 /* Resize windows as needed by changing the `tool-bar-lines'
12101 frame parameter. */
12102 if (change_height_p)
12103 {
12104 Lisp_Object frame;
12105 int old_height = WINDOW_TOTAL_LINES (w);
12106 int nrows;
12107 int nlines = tool_bar_lines_needed (f, &nrows);
12108
12109 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12110 && !f->minimize_tool_bar_window_p)
12111 ? (nlines > old_height)
12112 : (nlines != old_height));
12113 f->minimize_tool_bar_window_p = 0;
12114
12115 if (change_height_p)
12116 {
12117 XSETFRAME (frame, f);
12118 Fmodify_frame_parameters (frame,
12119 list1 (Fcons (Qtool_bar_lines,
12120 make_number (nlines))));
12121 if (WINDOW_TOTAL_LINES (w) != old_height)
12122 {
12123 clear_glyph_matrix (w->desired_matrix);
12124 f->n_tool_bar_rows = nrows;
12125 f->fonts_changed = 1;
12126 return 1;
12127 }
12128 }
12129 }
12130 }
12131
12132 f->minimize_tool_bar_window_p = 0;
12133 return 0;
12134
12135 #endif /* USE_GTK || HAVE_NS */
12136 }
12137
12138 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12139
12140 /* Get information about the tool-bar item which is displayed in GLYPH
12141 on frame F. Return in *PROP_IDX the index where tool-bar item
12142 properties start in F->tool_bar_items. Value is zero if
12143 GLYPH doesn't display a tool-bar item. */
12144
12145 static int
12146 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12147 {
12148 Lisp_Object prop;
12149 int success_p;
12150 int charpos;
12151
12152 /* This function can be called asynchronously, which means we must
12153 exclude any possibility that Fget_text_property signals an
12154 error. */
12155 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12156 charpos = max (0, charpos);
12157
12158 /* Get the text property `menu-item' at pos. The value of that
12159 property is the start index of this item's properties in
12160 F->tool_bar_items. */
12161 prop = Fget_text_property (make_number (charpos),
12162 Qmenu_item, f->current_tool_bar_string);
12163 if (INTEGERP (prop))
12164 {
12165 *prop_idx = XINT (prop);
12166 success_p = 1;
12167 }
12168 else
12169 success_p = 0;
12170
12171 return success_p;
12172 }
12173
12174 \f
12175 /* Get information about the tool-bar item at position X/Y on frame F.
12176 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12177 the current matrix of the tool-bar window of F, or NULL if not
12178 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12179 item in F->tool_bar_items. Value is
12180
12181 -1 if X/Y is not on a tool-bar item
12182 0 if X/Y is on the same item that was highlighted before.
12183 1 otherwise. */
12184
12185 static int
12186 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12187 int *hpos, int *vpos, int *prop_idx)
12188 {
12189 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12190 struct window *w = XWINDOW (f->tool_bar_window);
12191 int area;
12192
12193 /* Find the glyph under X/Y. */
12194 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12195 if (*glyph == NULL)
12196 return -1;
12197
12198 /* Get the start of this tool-bar item's properties in
12199 f->tool_bar_items. */
12200 if (!tool_bar_item_info (f, *glyph, prop_idx))
12201 return -1;
12202
12203 /* Is mouse on the highlighted item? */
12204 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12205 && *vpos >= hlinfo->mouse_face_beg_row
12206 && *vpos <= hlinfo->mouse_face_end_row
12207 && (*vpos > hlinfo->mouse_face_beg_row
12208 || *hpos >= hlinfo->mouse_face_beg_col)
12209 && (*vpos < hlinfo->mouse_face_end_row
12210 || *hpos < hlinfo->mouse_face_end_col
12211 || hlinfo->mouse_face_past_end))
12212 return 0;
12213
12214 return 1;
12215 }
12216
12217
12218 /* EXPORT:
12219 Handle mouse button event on the tool-bar of frame F, at
12220 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12221 0 for button release. MODIFIERS is event modifiers for button
12222 release. */
12223
12224 void
12225 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12226 int modifiers)
12227 {
12228 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12229 struct window *w = XWINDOW (f->tool_bar_window);
12230 int hpos, vpos, prop_idx;
12231 struct glyph *glyph;
12232 Lisp_Object enabled_p;
12233 int ts;
12234
12235 /* If not on the highlighted tool-bar item, and mouse-highlight is
12236 non-nil, return. This is so we generate the tool-bar button
12237 click only when the mouse button is released on the same item as
12238 where it was pressed. However, when mouse-highlight is disabled,
12239 generate the click when the button is released regardless of the
12240 highlight, since tool-bar items are not highlighted in that
12241 case. */
12242 frame_to_window_pixel_xy (w, &x, &y);
12243 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12244 if (ts == -1
12245 || (ts != 0 && !NILP (Vmouse_highlight)))
12246 return;
12247
12248 /* When mouse-highlight is off, generate the click for the item
12249 where the button was pressed, disregarding where it was
12250 released. */
12251 if (NILP (Vmouse_highlight) && !down_p)
12252 prop_idx = last_tool_bar_item;
12253
12254 /* If item is disabled, do nothing. */
12255 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12256 if (NILP (enabled_p))
12257 return;
12258
12259 if (down_p)
12260 {
12261 /* Show item in pressed state. */
12262 if (!NILP (Vmouse_highlight))
12263 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12264 last_tool_bar_item = prop_idx;
12265 }
12266 else
12267 {
12268 Lisp_Object key, frame;
12269 struct input_event event;
12270 EVENT_INIT (event);
12271
12272 /* Show item in released state. */
12273 if (!NILP (Vmouse_highlight))
12274 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12275
12276 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12277
12278 XSETFRAME (frame, f);
12279 event.kind = TOOL_BAR_EVENT;
12280 event.frame_or_window = frame;
12281 event.arg = frame;
12282 kbd_buffer_store_event (&event);
12283
12284 event.kind = TOOL_BAR_EVENT;
12285 event.frame_or_window = frame;
12286 event.arg = key;
12287 event.modifiers = modifiers;
12288 kbd_buffer_store_event (&event);
12289 last_tool_bar_item = -1;
12290 }
12291 }
12292
12293
12294 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12295 tool-bar window-relative coordinates X/Y. Called from
12296 note_mouse_highlight. */
12297
12298 static void
12299 note_tool_bar_highlight (struct frame *f, int x, int y)
12300 {
12301 Lisp_Object window = f->tool_bar_window;
12302 struct window *w = XWINDOW (window);
12303 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12304 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12305 int hpos, vpos;
12306 struct glyph *glyph;
12307 struct glyph_row *row;
12308 int i;
12309 Lisp_Object enabled_p;
12310 int prop_idx;
12311 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12312 int mouse_down_p, rc;
12313
12314 /* Function note_mouse_highlight is called with negative X/Y
12315 values when mouse moves outside of the frame. */
12316 if (x <= 0 || y <= 0)
12317 {
12318 clear_mouse_face (hlinfo);
12319 return;
12320 }
12321
12322 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12323 if (rc < 0)
12324 {
12325 /* Not on tool-bar item. */
12326 clear_mouse_face (hlinfo);
12327 return;
12328 }
12329 else if (rc == 0)
12330 /* On same tool-bar item as before. */
12331 goto set_help_echo;
12332
12333 clear_mouse_face (hlinfo);
12334
12335 /* Mouse is down, but on different tool-bar item? */
12336 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12337 && f == dpyinfo->last_mouse_frame);
12338
12339 if (mouse_down_p
12340 && last_tool_bar_item != prop_idx)
12341 return;
12342
12343 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12344
12345 /* If tool-bar item is not enabled, don't highlight it. */
12346 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12347 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12348 {
12349 /* Compute the x-position of the glyph. In front and past the
12350 image is a space. We include this in the highlighted area. */
12351 row = MATRIX_ROW (w->current_matrix, vpos);
12352 for (i = x = 0; i < hpos; ++i)
12353 x += row->glyphs[TEXT_AREA][i].pixel_width;
12354
12355 /* Record this as the current active region. */
12356 hlinfo->mouse_face_beg_col = hpos;
12357 hlinfo->mouse_face_beg_row = vpos;
12358 hlinfo->mouse_face_beg_x = x;
12359 hlinfo->mouse_face_past_end = 0;
12360
12361 hlinfo->mouse_face_end_col = hpos + 1;
12362 hlinfo->mouse_face_end_row = vpos;
12363 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12364 hlinfo->mouse_face_window = window;
12365 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12366
12367 /* Display it as active. */
12368 show_mouse_face (hlinfo, draw);
12369 }
12370
12371 set_help_echo:
12372
12373 /* Set help_echo_string to a help string to display for this tool-bar item.
12374 XTread_socket does the rest. */
12375 help_echo_object = help_echo_window = Qnil;
12376 help_echo_pos = -1;
12377 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12378 if (NILP (help_echo_string))
12379 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12380 }
12381
12382 #endif /* !USE_GTK && !HAVE_NS */
12383
12384 #endif /* HAVE_WINDOW_SYSTEM */
12385
12386
12387 \f
12388 /************************************************************************
12389 Horizontal scrolling
12390 ************************************************************************/
12391
12392 static int hscroll_window_tree (Lisp_Object);
12393 static int hscroll_windows (Lisp_Object);
12394
12395 /* For all leaf windows in the window tree rooted at WINDOW, set their
12396 hscroll value so that PT is (i) visible in the window, and (ii) so
12397 that it is not within a certain margin at the window's left and
12398 right border. Value is non-zero if any window's hscroll has been
12399 changed. */
12400
12401 static int
12402 hscroll_window_tree (Lisp_Object window)
12403 {
12404 int hscrolled_p = 0;
12405 int hscroll_relative_p = FLOATP (Vhscroll_step);
12406 int hscroll_step_abs = 0;
12407 double hscroll_step_rel = 0;
12408
12409 if (hscroll_relative_p)
12410 {
12411 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12412 if (hscroll_step_rel < 0)
12413 {
12414 hscroll_relative_p = 0;
12415 hscroll_step_abs = 0;
12416 }
12417 }
12418 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12419 {
12420 hscroll_step_abs = XINT (Vhscroll_step);
12421 if (hscroll_step_abs < 0)
12422 hscroll_step_abs = 0;
12423 }
12424 else
12425 hscroll_step_abs = 0;
12426
12427 while (WINDOWP (window))
12428 {
12429 struct window *w = XWINDOW (window);
12430
12431 if (WINDOWP (w->contents))
12432 hscrolled_p |= hscroll_window_tree (w->contents);
12433 else if (w->cursor.vpos >= 0)
12434 {
12435 int h_margin;
12436 int text_area_width;
12437 struct glyph_row *current_cursor_row
12438 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12439 struct glyph_row *desired_cursor_row
12440 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12441 struct glyph_row *cursor_row
12442 = (desired_cursor_row->enabled_p
12443 ? desired_cursor_row
12444 : current_cursor_row);
12445 int row_r2l_p = cursor_row->reversed_p;
12446
12447 text_area_width = window_box_width (w, TEXT_AREA);
12448
12449 /* Scroll when cursor is inside this scroll margin. */
12450 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12451
12452 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12453 /* For left-to-right rows, hscroll when cursor is either
12454 (i) inside the right hscroll margin, or (ii) if it is
12455 inside the left margin and the window is already
12456 hscrolled. */
12457 && ((!row_r2l_p
12458 && ((w->hscroll
12459 && w->cursor.x <= h_margin)
12460 || (cursor_row->enabled_p
12461 && cursor_row->truncated_on_right_p
12462 && (w->cursor.x >= text_area_width - h_margin))))
12463 /* For right-to-left rows, the logic is similar,
12464 except that rules for scrolling to left and right
12465 are reversed. E.g., if cursor.x <= h_margin, we
12466 need to hscroll "to the right" unconditionally,
12467 and that will scroll the screen to the left so as
12468 to reveal the next portion of the row. */
12469 || (row_r2l_p
12470 && ((cursor_row->enabled_p
12471 /* FIXME: It is confusing to set the
12472 truncated_on_right_p flag when R2L rows
12473 are actually truncated on the left. */
12474 && cursor_row->truncated_on_right_p
12475 && w->cursor.x <= h_margin)
12476 || (w->hscroll
12477 && (w->cursor.x >= text_area_width - h_margin))))))
12478 {
12479 struct it it;
12480 ptrdiff_t hscroll;
12481 struct buffer *saved_current_buffer;
12482 ptrdiff_t pt;
12483 int wanted_x;
12484
12485 /* Find point in a display of infinite width. */
12486 saved_current_buffer = current_buffer;
12487 current_buffer = XBUFFER (w->contents);
12488
12489 if (w == XWINDOW (selected_window))
12490 pt = PT;
12491 else
12492 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12493
12494 /* Move iterator to pt starting at cursor_row->start in
12495 a line with infinite width. */
12496 init_to_row_start (&it, w, cursor_row);
12497 it.last_visible_x = INFINITY;
12498 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12499 current_buffer = saved_current_buffer;
12500
12501 /* Position cursor in window. */
12502 if (!hscroll_relative_p && hscroll_step_abs == 0)
12503 hscroll = max (0, (it.current_x
12504 - (ITERATOR_AT_END_OF_LINE_P (&it)
12505 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12506 : (text_area_width / 2))))
12507 / FRAME_COLUMN_WIDTH (it.f);
12508 else if ((!row_r2l_p
12509 && w->cursor.x >= text_area_width - h_margin)
12510 || (row_r2l_p && w->cursor.x <= h_margin))
12511 {
12512 if (hscroll_relative_p)
12513 wanted_x = text_area_width * (1 - hscroll_step_rel)
12514 - h_margin;
12515 else
12516 wanted_x = text_area_width
12517 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12518 - h_margin;
12519 hscroll
12520 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12521 }
12522 else
12523 {
12524 if (hscroll_relative_p)
12525 wanted_x = text_area_width * hscroll_step_rel
12526 + h_margin;
12527 else
12528 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12529 + h_margin;
12530 hscroll
12531 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12532 }
12533 hscroll = max (hscroll, w->min_hscroll);
12534
12535 /* Don't prevent redisplay optimizations if hscroll
12536 hasn't changed, as it will unnecessarily slow down
12537 redisplay. */
12538 if (w->hscroll != hscroll)
12539 {
12540 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12541 w->hscroll = hscroll;
12542 hscrolled_p = 1;
12543 }
12544 }
12545 }
12546
12547 window = w->next;
12548 }
12549
12550 /* Value is non-zero if hscroll of any leaf window has been changed. */
12551 return hscrolled_p;
12552 }
12553
12554
12555 /* Set hscroll so that cursor is visible and not inside horizontal
12556 scroll margins for all windows in the tree rooted at WINDOW. See
12557 also hscroll_window_tree above. Value is non-zero if any window's
12558 hscroll has been changed. If it has, desired matrices on the frame
12559 of WINDOW are cleared. */
12560
12561 static int
12562 hscroll_windows (Lisp_Object window)
12563 {
12564 int hscrolled_p = hscroll_window_tree (window);
12565 if (hscrolled_p)
12566 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12567 return hscrolled_p;
12568 }
12569
12570
12571 \f
12572 /************************************************************************
12573 Redisplay
12574 ************************************************************************/
12575
12576 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12577 to a non-zero value. This is sometimes handy to have in a debugger
12578 session. */
12579
12580 #ifdef GLYPH_DEBUG
12581
12582 /* First and last unchanged row for try_window_id. */
12583
12584 static int debug_first_unchanged_at_end_vpos;
12585 static int debug_last_unchanged_at_beg_vpos;
12586
12587 /* Delta vpos and y. */
12588
12589 static int debug_dvpos, debug_dy;
12590
12591 /* Delta in characters and bytes for try_window_id. */
12592
12593 static ptrdiff_t debug_delta, debug_delta_bytes;
12594
12595 /* Values of window_end_pos and window_end_vpos at the end of
12596 try_window_id. */
12597
12598 static ptrdiff_t debug_end_vpos;
12599
12600 /* Append a string to W->desired_matrix->method. FMT is a printf
12601 format string. If trace_redisplay_p is non-zero also printf the
12602 resulting string to stderr. */
12603
12604 static void debug_method_add (struct window *, char const *, ...)
12605 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12606
12607 static void
12608 debug_method_add (struct window *w, char const *fmt, ...)
12609 {
12610 void *ptr = w;
12611 char *method = w->desired_matrix->method;
12612 int len = strlen (method);
12613 int size = sizeof w->desired_matrix->method;
12614 int remaining = size - len - 1;
12615 va_list ap;
12616
12617 if (len && remaining)
12618 {
12619 method[len] = '|';
12620 --remaining, ++len;
12621 }
12622
12623 va_start (ap, fmt);
12624 vsnprintf (method + len, remaining + 1, fmt, ap);
12625 va_end (ap);
12626
12627 if (trace_redisplay_p)
12628 fprintf (stderr, "%p (%s): %s\n",
12629 ptr,
12630 ((BUFFERP (w->contents)
12631 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12632 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12633 : "no buffer"),
12634 method + len);
12635 }
12636
12637 #endif /* GLYPH_DEBUG */
12638
12639
12640 /* Value is non-zero if all changes in window W, which displays
12641 current_buffer, are in the text between START and END. START is a
12642 buffer position, END is given as a distance from Z. Used in
12643 redisplay_internal for display optimization. */
12644
12645 static int
12646 text_outside_line_unchanged_p (struct window *w,
12647 ptrdiff_t start, ptrdiff_t end)
12648 {
12649 int unchanged_p = 1;
12650
12651 /* If text or overlays have changed, see where. */
12652 if (window_outdated (w))
12653 {
12654 /* Gap in the line? */
12655 if (GPT < start || Z - GPT < end)
12656 unchanged_p = 0;
12657
12658 /* Changes start in front of the line, or end after it? */
12659 if (unchanged_p
12660 && (BEG_UNCHANGED < start - 1
12661 || END_UNCHANGED < end))
12662 unchanged_p = 0;
12663
12664 /* If selective display, can't optimize if changes start at the
12665 beginning of the line. */
12666 if (unchanged_p
12667 && INTEGERP (BVAR (current_buffer, selective_display))
12668 && XINT (BVAR (current_buffer, selective_display)) > 0
12669 && (BEG_UNCHANGED < start || GPT <= start))
12670 unchanged_p = 0;
12671
12672 /* If there are overlays at the start or end of the line, these
12673 may have overlay strings with newlines in them. A change at
12674 START, for instance, may actually concern the display of such
12675 overlay strings as well, and they are displayed on different
12676 lines. So, quickly rule out this case. (For the future, it
12677 might be desirable to implement something more telling than
12678 just BEG/END_UNCHANGED.) */
12679 if (unchanged_p)
12680 {
12681 if (BEG + BEG_UNCHANGED == start
12682 && overlay_touches_p (start))
12683 unchanged_p = 0;
12684 if (END_UNCHANGED == end
12685 && overlay_touches_p (Z - end))
12686 unchanged_p = 0;
12687 }
12688
12689 /* Under bidi reordering, adding or deleting a character in the
12690 beginning of a paragraph, before the first strong directional
12691 character, can change the base direction of the paragraph (unless
12692 the buffer specifies a fixed paragraph direction), which will
12693 require to redisplay the whole paragraph. It might be worthwhile
12694 to find the paragraph limits and widen the range of redisplayed
12695 lines to that, but for now just give up this optimization. */
12696 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12697 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12698 unchanged_p = 0;
12699 }
12700
12701 return unchanged_p;
12702 }
12703
12704
12705 /* Do a frame update, taking possible shortcuts into account. This is
12706 the main external entry point for redisplay.
12707
12708 If the last redisplay displayed an echo area message and that message
12709 is no longer requested, we clear the echo area or bring back the
12710 mini-buffer if that is in use. */
12711
12712 void
12713 redisplay (void)
12714 {
12715 redisplay_internal ();
12716 }
12717
12718
12719 static Lisp_Object
12720 overlay_arrow_string_or_property (Lisp_Object var)
12721 {
12722 Lisp_Object val;
12723
12724 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12725 return val;
12726
12727 return Voverlay_arrow_string;
12728 }
12729
12730 /* Return 1 if there are any overlay-arrows in current_buffer. */
12731 static int
12732 overlay_arrow_in_current_buffer_p (void)
12733 {
12734 Lisp_Object vlist;
12735
12736 for (vlist = Voverlay_arrow_variable_list;
12737 CONSP (vlist);
12738 vlist = XCDR (vlist))
12739 {
12740 Lisp_Object var = XCAR (vlist);
12741 Lisp_Object val;
12742
12743 if (!SYMBOLP (var))
12744 continue;
12745 val = find_symbol_value (var);
12746 if (MARKERP (val)
12747 && current_buffer == XMARKER (val)->buffer)
12748 return 1;
12749 }
12750 return 0;
12751 }
12752
12753
12754 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12755 has changed. */
12756
12757 static int
12758 overlay_arrows_changed_p (void)
12759 {
12760 Lisp_Object vlist;
12761
12762 for (vlist = Voverlay_arrow_variable_list;
12763 CONSP (vlist);
12764 vlist = XCDR (vlist))
12765 {
12766 Lisp_Object var = XCAR (vlist);
12767 Lisp_Object val, pstr;
12768
12769 if (!SYMBOLP (var))
12770 continue;
12771 val = find_symbol_value (var);
12772 if (!MARKERP (val))
12773 continue;
12774 if (! EQ (COERCE_MARKER (val),
12775 Fget (var, Qlast_arrow_position))
12776 || ! (pstr = overlay_arrow_string_or_property (var),
12777 EQ (pstr, Fget (var, Qlast_arrow_string))))
12778 return 1;
12779 }
12780 return 0;
12781 }
12782
12783 /* Mark overlay arrows to be updated on next redisplay. */
12784
12785 static void
12786 update_overlay_arrows (int up_to_date)
12787 {
12788 Lisp_Object vlist;
12789
12790 for (vlist = Voverlay_arrow_variable_list;
12791 CONSP (vlist);
12792 vlist = XCDR (vlist))
12793 {
12794 Lisp_Object var = XCAR (vlist);
12795
12796 if (!SYMBOLP (var))
12797 continue;
12798
12799 if (up_to_date > 0)
12800 {
12801 Lisp_Object val = find_symbol_value (var);
12802 Fput (var, Qlast_arrow_position,
12803 COERCE_MARKER (val));
12804 Fput (var, Qlast_arrow_string,
12805 overlay_arrow_string_or_property (var));
12806 }
12807 else if (up_to_date < 0
12808 || !NILP (Fget (var, Qlast_arrow_position)))
12809 {
12810 Fput (var, Qlast_arrow_position, Qt);
12811 Fput (var, Qlast_arrow_string, Qt);
12812 }
12813 }
12814 }
12815
12816
12817 /* Return overlay arrow string to display at row.
12818 Return integer (bitmap number) for arrow bitmap in left fringe.
12819 Return nil if no overlay arrow. */
12820
12821 static Lisp_Object
12822 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12823 {
12824 Lisp_Object vlist;
12825
12826 for (vlist = Voverlay_arrow_variable_list;
12827 CONSP (vlist);
12828 vlist = XCDR (vlist))
12829 {
12830 Lisp_Object var = XCAR (vlist);
12831 Lisp_Object val;
12832
12833 if (!SYMBOLP (var))
12834 continue;
12835
12836 val = find_symbol_value (var);
12837
12838 if (MARKERP (val)
12839 && current_buffer == XMARKER (val)->buffer
12840 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12841 {
12842 if (FRAME_WINDOW_P (it->f)
12843 /* FIXME: if ROW->reversed_p is set, this should test
12844 the right fringe, not the left one. */
12845 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12846 {
12847 #ifdef HAVE_WINDOW_SYSTEM
12848 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12849 {
12850 int fringe_bitmap;
12851 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12852 return make_number (fringe_bitmap);
12853 }
12854 #endif
12855 return make_number (-1); /* Use default arrow bitmap. */
12856 }
12857 return overlay_arrow_string_or_property (var);
12858 }
12859 }
12860
12861 return Qnil;
12862 }
12863
12864 /* Return 1 if point moved out of or into a composition. Otherwise
12865 return 0. PREV_BUF and PREV_PT are the last point buffer and
12866 position. BUF and PT are the current point buffer and position. */
12867
12868 static int
12869 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12870 struct buffer *buf, ptrdiff_t pt)
12871 {
12872 ptrdiff_t start, end;
12873 Lisp_Object prop;
12874 Lisp_Object buffer;
12875
12876 XSETBUFFER (buffer, buf);
12877 /* Check a composition at the last point if point moved within the
12878 same buffer. */
12879 if (prev_buf == buf)
12880 {
12881 if (prev_pt == pt)
12882 /* Point didn't move. */
12883 return 0;
12884
12885 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12886 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12887 && composition_valid_p (start, end, prop)
12888 && start < prev_pt && end > prev_pt)
12889 /* The last point was within the composition. Return 1 iff
12890 point moved out of the composition. */
12891 return (pt <= start || pt >= end);
12892 }
12893
12894 /* Check a composition at the current point. */
12895 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12896 && find_composition (pt, -1, &start, &end, &prop, buffer)
12897 && composition_valid_p (start, end, prop)
12898 && start < pt && end > pt);
12899 }
12900
12901 /* Reconsider the clip changes of buffer which is displayed in W. */
12902
12903 static void
12904 reconsider_clip_changes (struct window *w)
12905 {
12906 struct buffer *b = XBUFFER (w->contents);
12907
12908 if (b->clip_changed
12909 && w->window_end_valid
12910 && w->current_matrix->buffer == b
12911 && w->current_matrix->zv == BUF_ZV (b)
12912 && w->current_matrix->begv == BUF_BEGV (b))
12913 b->clip_changed = 0;
12914
12915 /* If display wasn't paused, and W is not a tool bar window, see if
12916 point has been moved into or out of a composition. In that case,
12917 we set b->clip_changed to 1 to force updating the screen. If
12918 b->clip_changed has already been set to 1, we can skip this
12919 check. */
12920 if (!b->clip_changed && w->window_end_valid)
12921 {
12922 ptrdiff_t pt = (w == XWINDOW (selected_window)
12923 ? PT : marker_position (w->pointm));
12924
12925 if ((w->current_matrix->buffer != b || pt != w->last_point)
12926 && check_point_in_composition (w->current_matrix->buffer,
12927 w->last_point, b, pt))
12928 b->clip_changed = 1;
12929 }
12930 }
12931
12932 static void
12933 propagate_buffer_redisplay (void)
12934 { /* Resetting b->text->redisplay is problematic!
12935 We can't just reset it in the case that some window that displays
12936 it has not been redisplayed; and such a window can stay
12937 unredisplayed for a long time if it's currently invisible.
12938 But we do want to reset it at the end of redisplay otherwise
12939 its displayed windows will keep being redisplayed over and over
12940 again.
12941 So we copy all b->text->redisplay flags up to their windows here,
12942 such that mark_window_display_accurate can safely reset
12943 b->text->redisplay. */
12944 Lisp_Object ws = window_list ();
12945 for (; CONSP (ws); ws = XCDR (ws))
12946 {
12947 struct window *thisw = XWINDOW (XCAR (ws));
12948 struct buffer *thisb = XBUFFER (thisw->contents);
12949 if (thisb->text->redisplay)
12950 thisw->redisplay = true;
12951 }
12952 }
12953
12954 #define STOP_POLLING \
12955 do { if (! polling_stopped_here) stop_polling (); \
12956 polling_stopped_here = 1; } while (0)
12957
12958 #define RESUME_POLLING \
12959 do { if (polling_stopped_here) start_polling (); \
12960 polling_stopped_here = 0; } while (0)
12961
12962
12963 /* Perhaps in the future avoid recentering windows if it
12964 is not necessary; currently that causes some problems. */
12965
12966 static void
12967 redisplay_internal (void)
12968 {
12969 struct window *w = XWINDOW (selected_window);
12970 struct window *sw;
12971 struct frame *fr;
12972 int pending;
12973 bool must_finish = 0, match_p;
12974 struct text_pos tlbufpos, tlendpos;
12975 int number_of_visible_frames;
12976 ptrdiff_t count;
12977 struct frame *sf;
12978 int polling_stopped_here = 0;
12979 Lisp_Object tail, frame;
12980
12981 /* True means redisplay has to consider all windows on all
12982 frames. False, only selected_window is considered. */
12983 bool consider_all_windows_p;
12984
12985 /* True means redisplay has to redisplay the miniwindow. */
12986 bool update_miniwindow_p = false;
12987
12988 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12989
12990 /* No redisplay if running in batch mode or frame is not yet fully
12991 initialized, or redisplay is explicitly turned off by setting
12992 Vinhibit_redisplay. */
12993 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12994 || !NILP (Vinhibit_redisplay))
12995 return;
12996
12997 /* Don't examine these until after testing Vinhibit_redisplay.
12998 When Emacs is shutting down, perhaps because its connection to
12999 X has dropped, we should not look at them at all. */
13000 fr = XFRAME (w->frame);
13001 sf = SELECTED_FRAME ();
13002
13003 if (!fr->glyphs_initialized_p)
13004 return;
13005
13006 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13007 if (popup_activated ())
13008 return;
13009 #endif
13010
13011 /* I don't think this happens but let's be paranoid. */
13012 if (redisplaying_p)
13013 return;
13014
13015 /* Record a function that clears redisplaying_p
13016 when we leave this function. */
13017 count = SPECPDL_INDEX ();
13018 record_unwind_protect_void (unwind_redisplay);
13019 redisplaying_p = 1;
13020 specbind (Qinhibit_free_realized_faces, Qnil);
13021
13022 /* Record this function, so it appears on the profiler's backtraces. */
13023 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13024
13025 FOR_EACH_FRAME (tail, frame)
13026 XFRAME (frame)->already_hscrolled_p = 0;
13027
13028 retry:
13029 /* Remember the currently selected window. */
13030 sw = w;
13031
13032 pending = 0;
13033 last_escape_glyph_frame = NULL;
13034 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13035 last_glyphless_glyph_frame = NULL;
13036 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13037
13038 /* If face_change_count is non-zero, init_iterator will free all
13039 realized faces, which includes the faces referenced from current
13040 matrices. So, we can't reuse current matrices in this case. */
13041 if (face_change_count)
13042 windows_or_buffers_changed = 47;
13043
13044 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13045 && FRAME_TTY (sf)->previous_frame != sf)
13046 {
13047 /* Since frames on a single ASCII terminal share the same
13048 display area, displaying a different frame means redisplay
13049 the whole thing. */
13050 SET_FRAME_GARBAGED (sf);
13051 #ifndef DOS_NT
13052 set_tty_color_mode (FRAME_TTY (sf), sf);
13053 #endif
13054 FRAME_TTY (sf)->previous_frame = sf;
13055 }
13056
13057 /* Set the visible flags for all frames. Do this before checking for
13058 resized or garbaged frames; they want to know if their frames are
13059 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13060 number_of_visible_frames = 0;
13061
13062 FOR_EACH_FRAME (tail, frame)
13063 {
13064 struct frame *f = XFRAME (frame);
13065
13066 if (FRAME_VISIBLE_P (f))
13067 {
13068 ++number_of_visible_frames;
13069 /* Adjust matrices for visible frames only. */
13070 if (f->fonts_changed)
13071 {
13072 adjust_frame_glyphs (f);
13073 f->fonts_changed = 0;
13074 }
13075 /* If cursor type has been changed on the frame
13076 other than selected, consider all frames. */
13077 if (f != sf && f->cursor_type_changed)
13078 update_mode_lines = 31;
13079 }
13080 clear_desired_matrices (f);
13081 }
13082
13083 /* Notice any pending interrupt request to change frame size. */
13084 do_pending_window_change (1);
13085
13086 /* do_pending_window_change could change the selected_window due to
13087 frame resizing which makes the selected window too small. */
13088 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13089 sw = w;
13090
13091 /* Clear frames marked as garbaged. */
13092 clear_garbaged_frames ();
13093
13094 /* Build menubar and tool-bar items. */
13095 if (NILP (Vmemory_full))
13096 prepare_menu_bars ();
13097
13098 reconsider_clip_changes (w);
13099
13100 /* In most cases selected window displays current buffer. */
13101 match_p = XBUFFER (w->contents) == current_buffer;
13102 if (match_p)
13103 {
13104 /* Detect case that we need to write or remove a star in the mode line. */
13105 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13106 w->update_mode_line = 1;
13107
13108 if (mode_line_update_needed (w))
13109 w->update_mode_line = 1;
13110 }
13111
13112 /* Normally the message* functions will have already displayed and
13113 updated the echo area, but the frame may have been trashed, or
13114 the update may have been preempted, so display the echo area
13115 again here. Checking message_cleared_p captures the case that
13116 the echo area should be cleared. */
13117 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13118 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13119 || (message_cleared_p
13120 && minibuf_level == 0
13121 /* If the mini-window is currently selected, this means the
13122 echo-area doesn't show through. */
13123 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13124 {
13125 int window_height_changed_p = echo_area_display (0);
13126
13127 if (message_cleared_p)
13128 update_miniwindow_p = true;
13129
13130 must_finish = 1;
13131
13132 /* If we don't display the current message, don't clear the
13133 message_cleared_p flag, because, if we did, we wouldn't clear
13134 the echo area in the next redisplay which doesn't preserve
13135 the echo area. */
13136 if (!display_last_displayed_message_p)
13137 message_cleared_p = 0;
13138
13139 if (window_height_changed_p)
13140 {
13141 windows_or_buffers_changed = 50;
13142
13143 /* If window configuration was changed, frames may have been
13144 marked garbaged. Clear them or we will experience
13145 surprises wrt scrolling. */
13146 clear_garbaged_frames ();
13147 }
13148 }
13149 else if (EQ (selected_window, minibuf_window)
13150 && (current_buffer->clip_changed || window_outdated (w))
13151 && resize_mini_window (w, 0))
13152 {
13153 /* Resized active mini-window to fit the size of what it is
13154 showing if its contents might have changed. */
13155 must_finish = 1;
13156
13157 /* If window configuration was changed, frames may have been
13158 marked garbaged. Clear them or we will experience
13159 surprises wrt scrolling. */
13160 clear_garbaged_frames ();
13161 }
13162
13163 if (windows_or_buffers_changed && !update_mode_lines)
13164 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13165 only the windows's contents needs to be refreshed, or whether the
13166 mode-lines also need a refresh. */
13167 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13168 ? REDISPLAY_SOME : 32);
13169
13170 /* If specs for an arrow have changed, do thorough redisplay
13171 to ensure we remove any arrow that should no longer exist. */
13172 if (overlay_arrows_changed_p ())
13173 /* Apparently, this is the only case where we update other windows,
13174 without updating other mode-lines. */
13175 windows_or_buffers_changed = 49;
13176
13177 consider_all_windows_p = (update_mode_lines
13178 || windows_or_buffers_changed);
13179
13180 #define AINC(a,i) \
13181 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13182 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13183
13184 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13185 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13186
13187 /* Optimize the case that only the line containing the cursor in the
13188 selected window has changed. Variables starting with this_ are
13189 set in display_line and record information about the line
13190 containing the cursor. */
13191 tlbufpos = this_line_start_pos;
13192 tlendpos = this_line_end_pos;
13193 if (!consider_all_windows_p
13194 && CHARPOS (tlbufpos) > 0
13195 && !w->update_mode_line
13196 && !current_buffer->clip_changed
13197 && !current_buffer->prevent_redisplay_optimizations_p
13198 && FRAME_VISIBLE_P (XFRAME (w->frame))
13199 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13200 && !XFRAME (w->frame)->cursor_type_changed
13201 /* Make sure recorded data applies to current buffer, etc. */
13202 && this_line_buffer == current_buffer
13203 && match_p
13204 && !w->force_start
13205 && !w->optional_new_start
13206 /* Point must be on the line that we have info recorded about. */
13207 && PT >= CHARPOS (tlbufpos)
13208 && PT <= Z - CHARPOS (tlendpos)
13209 /* All text outside that line, including its final newline,
13210 must be unchanged. */
13211 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13212 CHARPOS (tlendpos)))
13213 {
13214 if (CHARPOS (tlbufpos) > BEGV
13215 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13216 && (CHARPOS (tlbufpos) == ZV
13217 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13218 /* Former continuation line has disappeared by becoming empty. */
13219 goto cancel;
13220 else if (window_outdated (w) || MINI_WINDOW_P (w))
13221 {
13222 /* We have to handle the case of continuation around a
13223 wide-column character (see the comment in indent.c around
13224 line 1340).
13225
13226 For instance, in the following case:
13227
13228 -------- Insert --------
13229 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13230 J_I_ ==> J_I_ `^^' are cursors.
13231 ^^ ^^
13232 -------- --------
13233
13234 As we have to redraw the line above, we cannot use this
13235 optimization. */
13236
13237 struct it it;
13238 int line_height_before = this_line_pixel_height;
13239
13240 /* Note that start_display will handle the case that the
13241 line starting at tlbufpos is a continuation line. */
13242 start_display (&it, w, tlbufpos);
13243
13244 /* Implementation note: It this still necessary? */
13245 if (it.current_x != this_line_start_x)
13246 goto cancel;
13247
13248 TRACE ((stderr, "trying display optimization 1\n"));
13249 w->cursor.vpos = -1;
13250 overlay_arrow_seen = 0;
13251 it.vpos = this_line_vpos;
13252 it.current_y = this_line_y;
13253 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13254 display_line (&it);
13255
13256 /* If line contains point, is not continued,
13257 and ends at same distance from eob as before, we win. */
13258 if (w->cursor.vpos >= 0
13259 /* Line is not continued, otherwise this_line_start_pos
13260 would have been set to 0 in display_line. */
13261 && CHARPOS (this_line_start_pos)
13262 /* Line ends as before. */
13263 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13264 /* Line has same height as before. Otherwise other lines
13265 would have to be shifted up or down. */
13266 && this_line_pixel_height == line_height_before)
13267 {
13268 /* If this is not the window's last line, we must adjust
13269 the charstarts of the lines below. */
13270 if (it.current_y < it.last_visible_y)
13271 {
13272 struct glyph_row *row
13273 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13274 ptrdiff_t delta, delta_bytes;
13275
13276 /* We used to distinguish between two cases here,
13277 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13278 when the line ends in a newline or the end of the
13279 buffer's accessible portion. But both cases did
13280 the same, so they were collapsed. */
13281 delta = (Z
13282 - CHARPOS (tlendpos)
13283 - MATRIX_ROW_START_CHARPOS (row));
13284 delta_bytes = (Z_BYTE
13285 - BYTEPOS (tlendpos)
13286 - MATRIX_ROW_START_BYTEPOS (row));
13287
13288 increment_matrix_positions (w->current_matrix,
13289 this_line_vpos + 1,
13290 w->current_matrix->nrows,
13291 delta, delta_bytes);
13292 }
13293
13294 /* If this row displays text now but previously didn't,
13295 or vice versa, w->window_end_vpos may have to be
13296 adjusted. */
13297 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13298 {
13299 if (w->window_end_vpos < this_line_vpos)
13300 w->window_end_vpos = this_line_vpos;
13301 }
13302 else if (w->window_end_vpos == this_line_vpos
13303 && this_line_vpos > 0)
13304 w->window_end_vpos = this_line_vpos - 1;
13305 w->window_end_valid = 0;
13306
13307 /* Update hint: No need to try to scroll in update_window. */
13308 w->desired_matrix->no_scrolling_p = 1;
13309
13310 #ifdef GLYPH_DEBUG
13311 *w->desired_matrix->method = 0;
13312 debug_method_add (w, "optimization 1");
13313 #endif
13314 #ifdef HAVE_WINDOW_SYSTEM
13315 update_window_fringes (w, 0);
13316 #endif
13317 goto update;
13318 }
13319 else
13320 goto cancel;
13321 }
13322 else if (/* Cursor position hasn't changed. */
13323 PT == w->last_point
13324 /* Make sure the cursor was last displayed
13325 in this window. Otherwise we have to reposition it. */
13326 && 0 <= w->cursor.vpos
13327 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13328 {
13329 if (!must_finish)
13330 {
13331 do_pending_window_change (1);
13332 /* If selected_window changed, redisplay again. */
13333 if (WINDOWP (selected_window)
13334 && (w = XWINDOW (selected_window)) != sw)
13335 goto retry;
13336
13337 /* We used to always goto end_of_redisplay here, but this
13338 isn't enough if we have a blinking cursor. */
13339 if (w->cursor_off_p == w->last_cursor_off_p)
13340 goto end_of_redisplay;
13341 }
13342 goto update;
13343 }
13344 /* If highlighting the region, or if the cursor is in the echo area,
13345 then we can't just move the cursor. */
13346 else if (NILP (Vshow_trailing_whitespace)
13347 && !cursor_in_echo_area)
13348 {
13349 struct it it;
13350 struct glyph_row *row;
13351
13352 /* Skip from tlbufpos to PT and see where it is. Note that
13353 PT may be in invisible text. If so, we will end at the
13354 next visible position. */
13355 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13356 NULL, DEFAULT_FACE_ID);
13357 it.current_x = this_line_start_x;
13358 it.current_y = this_line_y;
13359 it.vpos = this_line_vpos;
13360
13361 /* The call to move_it_to stops in front of PT, but
13362 moves over before-strings. */
13363 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13364
13365 if (it.vpos == this_line_vpos
13366 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13367 row->enabled_p))
13368 {
13369 eassert (this_line_vpos == it.vpos);
13370 eassert (this_line_y == it.current_y);
13371 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13372 #ifdef GLYPH_DEBUG
13373 *w->desired_matrix->method = 0;
13374 debug_method_add (w, "optimization 3");
13375 #endif
13376 goto update;
13377 }
13378 else
13379 goto cancel;
13380 }
13381
13382 cancel:
13383 /* Text changed drastically or point moved off of line. */
13384 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13385 }
13386
13387 CHARPOS (this_line_start_pos) = 0;
13388 ++clear_face_cache_count;
13389 #ifdef HAVE_WINDOW_SYSTEM
13390 ++clear_image_cache_count;
13391 #endif
13392
13393 /* Build desired matrices, and update the display. If
13394 consider_all_windows_p is non-zero, do it for all windows on all
13395 frames. Otherwise do it for selected_window, only. */
13396
13397 if (consider_all_windows_p)
13398 {
13399 FOR_EACH_FRAME (tail, frame)
13400 XFRAME (frame)->updated_p = 0;
13401
13402 propagate_buffer_redisplay ();
13403
13404 FOR_EACH_FRAME (tail, frame)
13405 {
13406 struct frame *f = XFRAME (frame);
13407
13408 /* We don't have to do anything for unselected terminal
13409 frames. */
13410 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13411 && !EQ (FRAME_TTY (f)->top_frame, frame))
13412 continue;
13413
13414 retry_frame:
13415
13416 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13417 {
13418 bool gcscrollbars
13419 /* Only GC scollbars when we redisplay the whole frame. */
13420 = f->redisplay || !REDISPLAY_SOME_P ();
13421 /* Mark all the scroll bars to be removed; we'll redeem
13422 the ones we want when we redisplay their windows. */
13423 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13424 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13425
13426 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13427 redisplay_windows (FRAME_ROOT_WINDOW (f));
13428
13429 /* The X error handler may have deleted that frame. */
13430 if (!FRAME_LIVE_P (f))
13431 continue;
13432
13433 /* Any scroll bars which redisplay_windows should have
13434 nuked should now go away. */
13435 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13436 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13437
13438 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13439 {
13440 /* If fonts changed on visible frame, display again. */
13441 if (f->fonts_changed)
13442 {
13443 adjust_frame_glyphs (f);
13444 f->fonts_changed = 0;
13445 goto retry_frame;
13446 }
13447
13448 /* See if we have to hscroll. */
13449 if (!f->already_hscrolled_p)
13450 {
13451 f->already_hscrolled_p = 1;
13452 if (hscroll_windows (f->root_window))
13453 goto retry_frame;
13454 }
13455
13456 /* Prevent various kinds of signals during display
13457 update. stdio is not robust about handling
13458 signals, which can cause an apparent I/O
13459 error. */
13460 if (interrupt_input)
13461 unrequest_sigio ();
13462 STOP_POLLING;
13463
13464 /* Mark windows on frame F to update. If we decide to
13465 update all frames but windows_or_buffers_changed is
13466 zero, we assume that only the windows that shows
13467 current buffer should be really updated. */
13468 set_window_update_flags
13469 (XWINDOW (f->root_window),
13470 (windows_or_buffers_changed ? NULL : current_buffer), 1);
13471 pending |= update_frame (f, 0, 0);
13472 f->cursor_type_changed = 0;
13473 f->updated_p = 1;
13474 }
13475 }
13476 }
13477
13478 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13479
13480 if (!pending)
13481 {
13482 /* Do the mark_window_display_accurate after all windows have
13483 been redisplayed because this call resets flags in buffers
13484 which are needed for proper redisplay. */
13485 FOR_EACH_FRAME (tail, frame)
13486 {
13487 struct frame *f = XFRAME (frame);
13488 if (f->updated_p)
13489 {
13490 f->redisplay = false;
13491 mark_window_display_accurate (f->root_window, 1);
13492 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13493 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13494 }
13495 }
13496 }
13497 }
13498 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13499 {
13500 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13501 struct frame *mini_frame;
13502
13503 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13504 /* Use list_of_error, not Qerror, so that
13505 we catch only errors and don't run the debugger. */
13506 internal_condition_case_1 (redisplay_window_1, selected_window,
13507 list_of_error,
13508 redisplay_window_error);
13509 if (update_miniwindow_p)
13510 internal_condition_case_1 (redisplay_window_1, mini_window,
13511 list_of_error,
13512 redisplay_window_error);
13513
13514 /* Compare desired and current matrices, perform output. */
13515
13516 update:
13517 /* If fonts changed, display again. */
13518 if (sf->fonts_changed)
13519 goto retry;
13520
13521 /* Prevent various kinds of signals during display update.
13522 stdio is not robust about handling signals,
13523 which can cause an apparent I/O error. */
13524 if (interrupt_input)
13525 unrequest_sigio ();
13526 STOP_POLLING;
13527
13528 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13529 {
13530 if (hscroll_windows (selected_window))
13531 goto retry;
13532
13533 XWINDOW (selected_window)->must_be_updated_p = 1;
13534 pending = update_frame (sf, 0, 0);
13535 sf->cursor_type_changed = 0;
13536 }
13537
13538 /* We may have called echo_area_display at the top of this
13539 function. If the echo area is on another frame, that may
13540 have put text on a frame other than the selected one, so the
13541 above call to update_frame would not have caught it. Catch
13542 it here. */
13543 mini_window = FRAME_MINIBUF_WINDOW (sf);
13544 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13545
13546 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13547 {
13548 XWINDOW (mini_window)->must_be_updated_p = 1;
13549 pending |= update_frame (mini_frame, 0, 0);
13550 mini_frame->cursor_type_changed = 0;
13551 if (!pending && hscroll_windows (mini_window))
13552 goto retry;
13553 }
13554 }
13555
13556 /* If display was paused because of pending input, make sure we do a
13557 thorough update the next time. */
13558 if (pending)
13559 {
13560 /* Prevent the optimization at the beginning of
13561 redisplay_internal that tries a single-line update of the
13562 line containing the cursor in the selected window. */
13563 CHARPOS (this_line_start_pos) = 0;
13564
13565 /* Let the overlay arrow be updated the next time. */
13566 update_overlay_arrows (0);
13567
13568 /* If we pause after scrolling, some rows in the current
13569 matrices of some windows are not valid. */
13570 if (!WINDOW_FULL_WIDTH_P (w)
13571 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13572 update_mode_lines = 36;
13573 }
13574 else
13575 {
13576 if (!consider_all_windows_p)
13577 {
13578 /* This has already been done above if
13579 consider_all_windows_p is set. */
13580 if (XBUFFER (w->contents)->text->redisplay
13581 && buffer_window_count (XBUFFER (w->contents)) > 1)
13582 /* This can happen if b->text->redisplay was set during
13583 jit-lock. */
13584 propagate_buffer_redisplay ();
13585 mark_window_display_accurate_1 (w, 1);
13586
13587 /* Say overlay arrows are up to date. */
13588 update_overlay_arrows (1);
13589
13590 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13591 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13592 }
13593
13594 update_mode_lines = 0;
13595 windows_or_buffers_changed = 0;
13596 }
13597
13598 /* Start SIGIO interrupts coming again. Having them off during the
13599 code above makes it less likely one will discard output, but not
13600 impossible, since there might be stuff in the system buffer here.
13601 But it is much hairier to try to do anything about that. */
13602 if (interrupt_input)
13603 request_sigio ();
13604 RESUME_POLLING;
13605
13606 /* If a frame has become visible which was not before, redisplay
13607 again, so that we display it. Expose events for such a frame
13608 (which it gets when becoming visible) don't call the parts of
13609 redisplay constructing glyphs, so simply exposing a frame won't
13610 display anything in this case. So, we have to display these
13611 frames here explicitly. */
13612 if (!pending)
13613 {
13614 int new_count = 0;
13615
13616 FOR_EACH_FRAME (tail, frame)
13617 {
13618 if (XFRAME (frame)->visible)
13619 new_count++;
13620 }
13621
13622 if (new_count != number_of_visible_frames)
13623 windows_or_buffers_changed = 52;
13624 }
13625
13626 /* Change frame size now if a change is pending. */
13627 do_pending_window_change (1);
13628
13629 /* If we just did a pending size change, or have additional
13630 visible frames, or selected_window changed, redisplay again. */
13631 if ((windows_or_buffers_changed && !pending)
13632 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13633 goto retry;
13634
13635 /* Clear the face and image caches.
13636
13637 We used to do this only if consider_all_windows_p. But the cache
13638 needs to be cleared if a timer creates images in the current
13639 buffer (e.g. the test case in Bug#6230). */
13640
13641 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13642 {
13643 clear_face_cache (0);
13644 clear_face_cache_count = 0;
13645 }
13646
13647 #ifdef HAVE_WINDOW_SYSTEM
13648 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13649 {
13650 clear_image_caches (Qnil);
13651 clear_image_cache_count = 0;
13652 }
13653 #endif /* HAVE_WINDOW_SYSTEM */
13654
13655 end_of_redisplay:
13656 unbind_to (count, Qnil);
13657 RESUME_POLLING;
13658 }
13659
13660
13661 /* Redisplay, but leave alone any recent echo area message unless
13662 another message has been requested in its place.
13663
13664 This is useful in situations where you need to redisplay but no
13665 user action has occurred, making it inappropriate for the message
13666 area to be cleared. See tracking_off and
13667 wait_reading_process_output for examples of these situations.
13668
13669 FROM_WHERE is an integer saying from where this function was
13670 called. This is useful for debugging. */
13671
13672 void
13673 redisplay_preserve_echo_area (int from_where)
13674 {
13675 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13676
13677 if (!NILP (echo_area_buffer[1]))
13678 {
13679 /* We have a previously displayed message, but no current
13680 message. Redisplay the previous message. */
13681 display_last_displayed_message_p = 1;
13682 redisplay_internal ();
13683 display_last_displayed_message_p = 0;
13684 }
13685 else
13686 redisplay_internal ();
13687
13688 flush_frame (SELECTED_FRAME ());
13689 }
13690
13691
13692 /* Function registered with record_unwind_protect in redisplay_internal. */
13693
13694 static void
13695 unwind_redisplay (void)
13696 {
13697 redisplaying_p = 0;
13698 }
13699
13700
13701 /* Mark the display of leaf window W as accurate or inaccurate.
13702 If ACCURATE_P is non-zero mark display of W as accurate. If
13703 ACCURATE_P is zero, arrange for W to be redisplayed the next
13704 time redisplay_internal is called. */
13705
13706 static void
13707 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13708 {
13709 struct buffer *b = XBUFFER (w->contents);
13710
13711 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13712 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13713 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13714
13715 if (accurate_p)
13716 {
13717 b->clip_changed = false;
13718 b->prevent_redisplay_optimizations_p = false;
13719 eassert (buffer_window_count (b) > 0);
13720 /* Resetting b->text->redisplay is problematic!
13721 In order to make it safer to do it here, redisplay_internal must
13722 have copied all b->text->redisplay to their respective windows. */
13723 b->text->redisplay = false;
13724
13725 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13726 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13727 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13728 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13729
13730 w->current_matrix->buffer = b;
13731 w->current_matrix->begv = BUF_BEGV (b);
13732 w->current_matrix->zv = BUF_ZV (b);
13733
13734 w->last_cursor_vpos = w->cursor.vpos;
13735 w->last_cursor_off_p = w->cursor_off_p;
13736
13737 if (w == XWINDOW (selected_window))
13738 w->last_point = BUF_PT (b);
13739 else
13740 w->last_point = marker_position (w->pointm);
13741
13742 w->window_end_valid = true;
13743 w->update_mode_line = false;
13744 }
13745
13746 w->redisplay = !accurate_p;
13747 }
13748
13749
13750 /* Mark the display of windows in the window tree rooted at WINDOW as
13751 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13752 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13753 be redisplayed the next time redisplay_internal is called. */
13754
13755 void
13756 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13757 {
13758 struct window *w;
13759
13760 for (; !NILP (window); window = w->next)
13761 {
13762 w = XWINDOW (window);
13763 if (WINDOWP (w->contents))
13764 mark_window_display_accurate (w->contents, accurate_p);
13765 else
13766 mark_window_display_accurate_1 (w, accurate_p);
13767 }
13768
13769 if (accurate_p)
13770 update_overlay_arrows (1);
13771 else
13772 /* Force a thorough redisplay the next time by setting
13773 last_arrow_position and last_arrow_string to t, which is
13774 unequal to any useful value of Voverlay_arrow_... */
13775 update_overlay_arrows (-1);
13776 }
13777
13778
13779 /* Return value in display table DP (Lisp_Char_Table *) for character
13780 C. Since a display table doesn't have any parent, we don't have to
13781 follow parent. Do not call this function directly but use the
13782 macro DISP_CHAR_VECTOR. */
13783
13784 Lisp_Object
13785 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13786 {
13787 Lisp_Object val;
13788
13789 if (ASCII_CHAR_P (c))
13790 {
13791 val = dp->ascii;
13792 if (SUB_CHAR_TABLE_P (val))
13793 val = XSUB_CHAR_TABLE (val)->contents[c];
13794 }
13795 else
13796 {
13797 Lisp_Object table;
13798
13799 XSETCHAR_TABLE (table, dp);
13800 val = char_table_ref (table, c);
13801 }
13802 if (NILP (val))
13803 val = dp->defalt;
13804 return val;
13805 }
13806
13807
13808 \f
13809 /***********************************************************************
13810 Window Redisplay
13811 ***********************************************************************/
13812
13813 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13814
13815 static void
13816 redisplay_windows (Lisp_Object window)
13817 {
13818 while (!NILP (window))
13819 {
13820 struct window *w = XWINDOW (window);
13821
13822 if (WINDOWP (w->contents))
13823 redisplay_windows (w->contents);
13824 else if (BUFFERP (w->contents))
13825 {
13826 displayed_buffer = XBUFFER (w->contents);
13827 /* Use list_of_error, not Qerror, so that
13828 we catch only errors and don't run the debugger. */
13829 internal_condition_case_1 (redisplay_window_0, window,
13830 list_of_error,
13831 redisplay_window_error);
13832 }
13833
13834 window = w->next;
13835 }
13836 }
13837
13838 static Lisp_Object
13839 redisplay_window_error (Lisp_Object ignore)
13840 {
13841 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13842 return Qnil;
13843 }
13844
13845 static Lisp_Object
13846 redisplay_window_0 (Lisp_Object window)
13847 {
13848 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13849 redisplay_window (window, 0);
13850 return Qnil;
13851 }
13852
13853 static Lisp_Object
13854 redisplay_window_1 (Lisp_Object window)
13855 {
13856 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13857 redisplay_window (window, 1);
13858 return Qnil;
13859 }
13860 \f
13861
13862 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13863 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13864 which positions recorded in ROW differ from current buffer
13865 positions.
13866
13867 Return 0 if cursor is not on this row, 1 otherwise. */
13868
13869 static int
13870 set_cursor_from_row (struct window *w, struct glyph_row *row,
13871 struct glyph_matrix *matrix,
13872 ptrdiff_t delta, ptrdiff_t delta_bytes,
13873 int dy, int dvpos)
13874 {
13875 struct glyph *glyph = row->glyphs[TEXT_AREA];
13876 struct glyph *end = glyph + row->used[TEXT_AREA];
13877 struct glyph *cursor = NULL;
13878 /* The last known character position in row. */
13879 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13880 int x = row->x;
13881 ptrdiff_t pt_old = PT - delta;
13882 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13883 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13884 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13885 /* A glyph beyond the edge of TEXT_AREA which we should never
13886 touch. */
13887 struct glyph *glyphs_end = end;
13888 /* Non-zero means we've found a match for cursor position, but that
13889 glyph has the avoid_cursor_p flag set. */
13890 int match_with_avoid_cursor = 0;
13891 /* Non-zero means we've seen at least one glyph that came from a
13892 display string. */
13893 int string_seen = 0;
13894 /* Largest and smallest buffer positions seen so far during scan of
13895 glyph row. */
13896 ptrdiff_t bpos_max = pos_before;
13897 ptrdiff_t bpos_min = pos_after;
13898 /* Last buffer position covered by an overlay string with an integer
13899 `cursor' property. */
13900 ptrdiff_t bpos_covered = 0;
13901 /* Non-zero means the display string on which to display the cursor
13902 comes from a text property, not from an overlay. */
13903 int string_from_text_prop = 0;
13904
13905 /* Don't even try doing anything if called for a mode-line or
13906 header-line row, since the rest of the code isn't prepared to
13907 deal with such calamities. */
13908 eassert (!row->mode_line_p);
13909 if (row->mode_line_p)
13910 return 0;
13911
13912 /* Skip over glyphs not having an object at the start and the end of
13913 the row. These are special glyphs like truncation marks on
13914 terminal frames. */
13915 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13916 {
13917 if (!row->reversed_p)
13918 {
13919 while (glyph < end
13920 && INTEGERP (glyph->object)
13921 && glyph->charpos < 0)
13922 {
13923 x += glyph->pixel_width;
13924 ++glyph;
13925 }
13926 while (end > glyph
13927 && INTEGERP ((end - 1)->object)
13928 /* CHARPOS is zero for blanks and stretch glyphs
13929 inserted by extend_face_to_end_of_line. */
13930 && (end - 1)->charpos <= 0)
13931 --end;
13932 glyph_before = glyph - 1;
13933 glyph_after = end;
13934 }
13935 else
13936 {
13937 struct glyph *g;
13938
13939 /* If the glyph row is reversed, we need to process it from back
13940 to front, so swap the edge pointers. */
13941 glyphs_end = end = glyph - 1;
13942 glyph += row->used[TEXT_AREA] - 1;
13943
13944 while (glyph > end + 1
13945 && INTEGERP (glyph->object)
13946 && glyph->charpos < 0)
13947 {
13948 --glyph;
13949 x -= glyph->pixel_width;
13950 }
13951 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13952 --glyph;
13953 /* By default, in reversed rows we put the cursor on the
13954 rightmost (first in the reading order) glyph. */
13955 for (g = end + 1; g < glyph; g++)
13956 x += g->pixel_width;
13957 while (end < glyph
13958 && INTEGERP ((end + 1)->object)
13959 && (end + 1)->charpos <= 0)
13960 ++end;
13961 glyph_before = glyph + 1;
13962 glyph_after = end;
13963 }
13964 }
13965 else if (row->reversed_p)
13966 {
13967 /* In R2L rows that don't display text, put the cursor on the
13968 rightmost glyph. Case in point: an empty last line that is
13969 part of an R2L paragraph. */
13970 cursor = end - 1;
13971 /* Avoid placing the cursor on the last glyph of the row, where
13972 on terminal frames we hold the vertical border between
13973 adjacent windows. */
13974 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13975 && !WINDOW_RIGHTMOST_P (w)
13976 && cursor == row->glyphs[LAST_AREA] - 1)
13977 cursor--;
13978 x = -1; /* will be computed below, at label compute_x */
13979 }
13980
13981 /* Step 1: Try to find the glyph whose character position
13982 corresponds to point. If that's not possible, find 2 glyphs
13983 whose character positions are the closest to point, one before
13984 point, the other after it. */
13985 if (!row->reversed_p)
13986 while (/* not marched to end of glyph row */
13987 glyph < end
13988 /* glyph was not inserted by redisplay for internal purposes */
13989 && !INTEGERP (glyph->object))
13990 {
13991 if (BUFFERP (glyph->object))
13992 {
13993 ptrdiff_t dpos = glyph->charpos - pt_old;
13994
13995 if (glyph->charpos > bpos_max)
13996 bpos_max = glyph->charpos;
13997 if (glyph->charpos < bpos_min)
13998 bpos_min = glyph->charpos;
13999 if (!glyph->avoid_cursor_p)
14000 {
14001 /* If we hit point, we've found the glyph on which to
14002 display the cursor. */
14003 if (dpos == 0)
14004 {
14005 match_with_avoid_cursor = 0;
14006 break;
14007 }
14008 /* See if we've found a better approximation to
14009 POS_BEFORE or to POS_AFTER. */
14010 if (0 > dpos && dpos > pos_before - pt_old)
14011 {
14012 pos_before = glyph->charpos;
14013 glyph_before = glyph;
14014 }
14015 else if (0 < dpos && dpos < pos_after - pt_old)
14016 {
14017 pos_after = glyph->charpos;
14018 glyph_after = glyph;
14019 }
14020 }
14021 else if (dpos == 0)
14022 match_with_avoid_cursor = 1;
14023 }
14024 else if (STRINGP (glyph->object))
14025 {
14026 Lisp_Object chprop;
14027 ptrdiff_t glyph_pos = glyph->charpos;
14028
14029 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14030 glyph->object);
14031 if (!NILP (chprop))
14032 {
14033 /* If the string came from a `display' text property,
14034 look up the buffer position of that property and
14035 use that position to update bpos_max, as if we
14036 actually saw such a position in one of the row's
14037 glyphs. This helps with supporting integer values
14038 of `cursor' property on the display string in
14039 situations where most or all of the row's buffer
14040 text is completely covered by display properties,
14041 so that no glyph with valid buffer positions is
14042 ever seen in the row. */
14043 ptrdiff_t prop_pos =
14044 string_buffer_position_lim (glyph->object, pos_before,
14045 pos_after, 0);
14046
14047 if (prop_pos >= pos_before)
14048 bpos_max = prop_pos - 1;
14049 }
14050 if (INTEGERP (chprop))
14051 {
14052 bpos_covered = bpos_max + XINT (chprop);
14053 /* If the `cursor' property covers buffer positions up
14054 to and including point, we should display cursor on
14055 this glyph. Note that, if a `cursor' property on one
14056 of the string's characters has an integer value, we
14057 will break out of the loop below _before_ we get to
14058 the position match above. IOW, integer values of
14059 the `cursor' property override the "exact match for
14060 point" strategy of positioning the cursor. */
14061 /* Implementation note: bpos_max == pt_old when, e.g.,
14062 we are in an empty line, where bpos_max is set to
14063 MATRIX_ROW_START_CHARPOS, see above. */
14064 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14065 {
14066 cursor = glyph;
14067 break;
14068 }
14069 }
14070
14071 string_seen = 1;
14072 }
14073 x += glyph->pixel_width;
14074 ++glyph;
14075 }
14076 else if (glyph > end) /* row is reversed */
14077 while (!INTEGERP (glyph->object))
14078 {
14079 if (BUFFERP (glyph->object))
14080 {
14081 ptrdiff_t dpos = glyph->charpos - pt_old;
14082
14083 if (glyph->charpos > bpos_max)
14084 bpos_max = glyph->charpos;
14085 if (glyph->charpos < bpos_min)
14086 bpos_min = glyph->charpos;
14087 if (!glyph->avoid_cursor_p)
14088 {
14089 if (dpos == 0)
14090 {
14091 match_with_avoid_cursor = 0;
14092 break;
14093 }
14094 if (0 > dpos && dpos > pos_before - pt_old)
14095 {
14096 pos_before = glyph->charpos;
14097 glyph_before = glyph;
14098 }
14099 else if (0 < dpos && dpos < pos_after - pt_old)
14100 {
14101 pos_after = glyph->charpos;
14102 glyph_after = glyph;
14103 }
14104 }
14105 else if (dpos == 0)
14106 match_with_avoid_cursor = 1;
14107 }
14108 else if (STRINGP (glyph->object))
14109 {
14110 Lisp_Object chprop;
14111 ptrdiff_t glyph_pos = glyph->charpos;
14112
14113 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14114 glyph->object);
14115 if (!NILP (chprop))
14116 {
14117 ptrdiff_t prop_pos =
14118 string_buffer_position_lim (glyph->object, pos_before,
14119 pos_after, 0);
14120
14121 if (prop_pos >= pos_before)
14122 bpos_max = prop_pos - 1;
14123 }
14124 if (INTEGERP (chprop))
14125 {
14126 bpos_covered = bpos_max + XINT (chprop);
14127 /* If the `cursor' property covers buffer positions up
14128 to and including point, we should display cursor on
14129 this glyph. */
14130 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14131 {
14132 cursor = glyph;
14133 break;
14134 }
14135 }
14136 string_seen = 1;
14137 }
14138 --glyph;
14139 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14140 {
14141 x--; /* can't use any pixel_width */
14142 break;
14143 }
14144 x -= glyph->pixel_width;
14145 }
14146
14147 /* Step 2: If we didn't find an exact match for point, we need to
14148 look for a proper place to put the cursor among glyphs between
14149 GLYPH_BEFORE and GLYPH_AFTER. */
14150 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14151 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14152 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14153 {
14154 /* An empty line has a single glyph whose OBJECT is zero and
14155 whose CHARPOS is the position of a newline on that line.
14156 Note that on a TTY, there are more glyphs after that, which
14157 were produced by extend_face_to_end_of_line, but their
14158 CHARPOS is zero or negative. */
14159 int empty_line_p =
14160 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14161 && INTEGERP (glyph->object) && glyph->charpos > 0
14162 /* On a TTY, continued and truncated rows also have a glyph at
14163 their end whose OBJECT is zero and whose CHARPOS is
14164 positive (the continuation and truncation glyphs), but such
14165 rows are obviously not "empty". */
14166 && !(row->continued_p || row->truncated_on_right_p);
14167
14168 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14169 {
14170 ptrdiff_t ellipsis_pos;
14171
14172 /* Scan back over the ellipsis glyphs. */
14173 if (!row->reversed_p)
14174 {
14175 ellipsis_pos = (glyph - 1)->charpos;
14176 while (glyph > row->glyphs[TEXT_AREA]
14177 && (glyph - 1)->charpos == ellipsis_pos)
14178 glyph--, x -= glyph->pixel_width;
14179 /* That loop always goes one position too far, including
14180 the glyph before the ellipsis. So scan forward over
14181 that one. */
14182 x += glyph->pixel_width;
14183 glyph++;
14184 }
14185 else /* row is reversed */
14186 {
14187 ellipsis_pos = (glyph + 1)->charpos;
14188 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14189 && (glyph + 1)->charpos == ellipsis_pos)
14190 glyph++, x += glyph->pixel_width;
14191 x -= glyph->pixel_width;
14192 glyph--;
14193 }
14194 }
14195 else if (match_with_avoid_cursor)
14196 {
14197 cursor = glyph_after;
14198 x = -1;
14199 }
14200 else if (string_seen)
14201 {
14202 int incr = row->reversed_p ? -1 : +1;
14203
14204 /* Need to find the glyph that came out of a string which is
14205 present at point. That glyph is somewhere between
14206 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14207 positioned between POS_BEFORE and POS_AFTER in the
14208 buffer. */
14209 struct glyph *start, *stop;
14210 ptrdiff_t pos = pos_before;
14211
14212 x = -1;
14213
14214 /* If the row ends in a newline from a display string,
14215 reordering could have moved the glyphs belonging to the
14216 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14217 in this case we extend the search to the last glyph in
14218 the row that was not inserted by redisplay. */
14219 if (row->ends_in_newline_from_string_p)
14220 {
14221 glyph_after = end;
14222 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14223 }
14224
14225 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14226 correspond to POS_BEFORE and POS_AFTER, respectively. We
14227 need START and STOP in the order that corresponds to the
14228 row's direction as given by its reversed_p flag. If the
14229 directionality of characters between POS_BEFORE and
14230 POS_AFTER is the opposite of the row's base direction,
14231 these characters will have been reordered for display,
14232 and we need to reverse START and STOP. */
14233 if (!row->reversed_p)
14234 {
14235 start = min (glyph_before, glyph_after);
14236 stop = max (glyph_before, glyph_after);
14237 }
14238 else
14239 {
14240 start = max (glyph_before, glyph_after);
14241 stop = min (glyph_before, glyph_after);
14242 }
14243 for (glyph = start + incr;
14244 row->reversed_p ? glyph > stop : glyph < stop; )
14245 {
14246
14247 /* Any glyphs that come from the buffer are here because
14248 of bidi reordering. Skip them, and only pay
14249 attention to glyphs that came from some string. */
14250 if (STRINGP (glyph->object))
14251 {
14252 Lisp_Object str;
14253 ptrdiff_t tem;
14254 /* If the display property covers the newline, we
14255 need to search for it one position farther. */
14256 ptrdiff_t lim = pos_after
14257 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14258
14259 string_from_text_prop = 0;
14260 str = glyph->object;
14261 tem = string_buffer_position_lim (str, pos, lim, 0);
14262 if (tem == 0 /* from overlay */
14263 || pos <= tem)
14264 {
14265 /* If the string from which this glyph came is
14266 found in the buffer at point, or at position
14267 that is closer to point than pos_after, then
14268 we've found the glyph we've been looking for.
14269 If it comes from an overlay (tem == 0), and
14270 it has the `cursor' property on one of its
14271 glyphs, record that glyph as a candidate for
14272 displaying the cursor. (As in the
14273 unidirectional version, we will display the
14274 cursor on the last candidate we find.) */
14275 if (tem == 0
14276 || tem == pt_old
14277 || (tem - pt_old > 0 && tem < pos_after))
14278 {
14279 /* The glyphs from this string could have
14280 been reordered. Find the one with the
14281 smallest string position. Or there could
14282 be a character in the string with the
14283 `cursor' property, which means display
14284 cursor on that character's glyph. */
14285 ptrdiff_t strpos = glyph->charpos;
14286
14287 if (tem)
14288 {
14289 cursor = glyph;
14290 string_from_text_prop = 1;
14291 }
14292 for ( ;
14293 (row->reversed_p ? glyph > stop : glyph < stop)
14294 && EQ (glyph->object, str);
14295 glyph += incr)
14296 {
14297 Lisp_Object cprop;
14298 ptrdiff_t gpos = glyph->charpos;
14299
14300 cprop = Fget_char_property (make_number (gpos),
14301 Qcursor,
14302 glyph->object);
14303 if (!NILP (cprop))
14304 {
14305 cursor = glyph;
14306 break;
14307 }
14308 if (tem && glyph->charpos < strpos)
14309 {
14310 strpos = glyph->charpos;
14311 cursor = glyph;
14312 }
14313 }
14314
14315 if (tem == pt_old
14316 || (tem - pt_old > 0 && tem < pos_after))
14317 goto compute_x;
14318 }
14319 if (tem)
14320 pos = tem + 1; /* don't find previous instances */
14321 }
14322 /* This string is not what we want; skip all of the
14323 glyphs that came from it. */
14324 while ((row->reversed_p ? glyph > stop : glyph < stop)
14325 && EQ (glyph->object, str))
14326 glyph += incr;
14327 }
14328 else
14329 glyph += incr;
14330 }
14331
14332 /* If we reached the end of the line, and END was from a string,
14333 the cursor is not on this line. */
14334 if (cursor == NULL
14335 && (row->reversed_p ? glyph <= end : glyph >= end)
14336 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14337 && STRINGP (end->object)
14338 && row->continued_p)
14339 return 0;
14340 }
14341 /* A truncated row may not include PT among its character positions.
14342 Setting the cursor inside the scroll margin will trigger
14343 recalculation of hscroll in hscroll_window_tree. But if a
14344 display string covers point, defer to the string-handling
14345 code below to figure this out. */
14346 else if (row->truncated_on_left_p && pt_old < bpos_min)
14347 {
14348 cursor = glyph_before;
14349 x = -1;
14350 }
14351 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14352 /* Zero-width characters produce no glyphs. */
14353 || (!empty_line_p
14354 && (row->reversed_p
14355 ? glyph_after > glyphs_end
14356 : glyph_after < glyphs_end)))
14357 {
14358 cursor = glyph_after;
14359 x = -1;
14360 }
14361 }
14362
14363 compute_x:
14364 if (cursor != NULL)
14365 glyph = cursor;
14366 else if (glyph == glyphs_end
14367 && pos_before == pos_after
14368 && STRINGP ((row->reversed_p
14369 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14370 : row->glyphs[TEXT_AREA])->object))
14371 {
14372 /* If all the glyphs of this row came from strings, put the
14373 cursor on the first glyph of the row. This avoids having the
14374 cursor outside of the text area in this very rare and hard
14375 use case. */
14376 glyph =
14377 row->reversed_p
14378 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14379 : row->glyphs[TEXT_AREA];
14380 }
14381 if (x < 0)
14382 {
14383 struct glyph *g;
14384
14385 /* Need to compute x that corresponds to GLYPH. */
14386 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14387 {
14388 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14389 emacs_abort ();
14390 x += g->pixel_width;
14391 }
14392 }
14393
14394 /* ROW could be part of a continued line, which, under bidi
14395 reordering, might have other rows whose start and end charpos
14396 occlude point. Only set w->cursor if we found a better
14397 approximation to the cursor position than we have from previously
14398 examined candidate rows belonging to the same continued line. */
14399 if (/* We already have a candidate row. */
14400 w->cursor.vpos >= 0
14401 /* That candidate is not the row we are processing. */
14402 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14403 /* Make sure cursor.vpos specifies a row whose start and end
14404 charpos occlude point, and it is valid candidate for being a
14405 cursor-row. This is because some callers of this function
14406 leave cursor.vpos at the row where the cursor was displayed
14407 during the last redisplay cycle. */
14408 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14409 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14410 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14411 {
14412 struct glyph *g1
14413 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14414
14415 /* Don't consider glyphs that are outside TEXT_AREA. */
14416 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14417 return 0;
14418 /* Keep the candidate whose buffer position is the closest to
14419 point or has the `cursor' property. */
14420 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14421 w->cursor.hpos >= 0
14422 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14423 && ((BUFFERP (g1->object)
14424 && (g1->charpos == pt_old /* An exact match always wins. */
14425 || (BUFFERP (glyph->object)
14426 && eabs (g1->charpos - pt_old)
14427 < eabs (glyph->charpos - pt_old))))
14428 /* Previous candidate is a glyph from a string that has
14429 a non-nil `cursor' property. */
14430 || (STRINGP (g1->object)
14431 && (!NILP (Fget_char_property (make_number (g1->charpos),
14432 Qcursor, g1->object))
14433 /* Previous candidate is from the same display
14434 string as this one, and the display string
14435 came from a text property. */
14436 || (EQ (g1->object, glyph->object)
14437 && string_from_text_prop)
14438 /* this candidate is from newline and its
14439 position is not an exact match */
14440 || (INTEGERP (glyph->object)
14441 && glyph->charpos != pt_old)))))
14442 return 0;
14443 /* If this candidate gives an exact match, use that. */
14444 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14445 /* If this candidate is a glyph created for the
14446 terminating newline of a line, and point is on that
14447 newline, it wins because it's an exact match. */
14448 || (!row->continued_p
14449 && INTEGERP (glyph->object)
14450 && glyph->charpos == 0
14451 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14452 /* Otherwise, keep the candidate that comes from a row
14453 spanning less buffer positions. This may win when one or
14454 both candidate positions are on glyphs that came from
14455 display strings, for which we cannot compare buffer
14456 positions. */
14457 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14458 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14459 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14460 return 0;
14461 }
14462 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14463 w->cursor.x = x;
14464 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14465 w->cursor.y = row->y + dy;
14466
14467 if (w == XWINDOW (selected_window))
14468 {
14469 if (!row->continued_p
14470 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14471 && row->x == 0)
14472 {
14473 this_line_buffer = XBUFFER (w->contents);
14474
14475 CHARPOS (this_line_start_pos)
14476 = MATRIX_ROW_START_CHARPOS (row) + delta;
14477 BYTEPOS (this_line_start_pos)
14478 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14479
14480 CHARPOS (this_line_end_pos)
14481 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14482 BYTEPOS (this_line_end_pos)
14483 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14484
14485 this_line_y = w->cursor.y;
14486 this_line_pixel_height = row->height;
14487 this_line_vpos = w->cursor.vpos;
14488 this_line_start_x = row->x;
14489 }
14490 else
14491 CHARPOS (this_line_start_pos) = 0;
14492 }
14493
14494 return 1;
14495 }
14496
14497
14498 /* Run window scroll functions, if any, for WINDOW with new window
14499 start STARTP. Sets the window start of WINDOW to that position.
14500
14501 We assume that the window's buffer is really current. */
14502
14503 static struct text_pos
14504 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14505 {
14506 struct window *w = XWINDOW (window);
14507 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14508
14509 eassert (current_buffer == XBUFFER (w->contents));
14510
14511 if (!NILP (Vwindow_scroll_functions))
14512 {
14513 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14514 make_number (CHARPOS (startp)));
14515 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14516 /* In case the hook functions switch buffers. */
14517 set_buffer_internal (XBUFFER (w->contents));
14518 }
14519
14520 return startp;
14521 }
14522
14523
14524 /* Make sure the line containing the cursor is fully visible.
14525 A value of 1 means there is nothing to be done.
14526 (Either the line is fully visible, or it cannot be made so,
14527 or we cannot tell.)
14528
14529 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14530 is higher than window.
14531
14532 A value of 0 means the caller should do scrolling
14533 as if point had gone off the screen. */
14534
14535 static int
14536 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14537 {
14538 struct glyph_matrix *matrix;
14539 struct glyph_row *row;
14540 int window_height;
14541
14542 if (!make_cursor_line_fully_visible_p)
14543 return 1;
14544
14545 /* It's not always possible to find the cursor, e.g, when a window
14546 is full of overlay strings. Don't do anything in that case. */
14547 if (w->cursor.vpos < 0)
14548 return 1;
14549
14550 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14551 row = MATRIX_ROW (matrix, w->cursor.vpos);
14552
14553 /* If the cursor row is not partially visible, there's nothing to do. */
14554 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14555 return 1;
14556
14557 /* If the row the cursor is in is taller than the window's height,
14558 it's not clear what to do, so do nothing. */
14559 window_height = window_box_height (w);
14560 if (row->height >= window_height)
14561 {
14562 if (!force_p || MINI_WINDOW_P (w)
14563 || w->vscroll || w->cursor.vpos == 0)
14564 return 1;
14565 }
14566 return 0;
14567 }
14568
14569
14570 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14571 non-zero means only WINDOW is redisplayed in redisplay_internal.
14572 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14573 in redisplay_window to bring a partially visible line into view in
14574 the case that only the cursor has moved.
14575
14576 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14577 last screen line's vertical height extends past the end of the screen.
14578
14579 Value is
14580
14581 1 if scrolling succeeded
14582
14583 0 if scrolling didn't find point.
14584
14585 -1 if new fonts have been loaded so that we must interrupt
14586 redisplay, adjust glyph matrices, and try again. */
14587
14588 enum
14589 {
14590 SCROLLING_SUCCESS,
14591 SCROLLING_FAILED,
14592 SCROLLING_NEED_LARGER_MATRICES
14593 };
14594
14595 /* If scroll-conservatively is more than this, never recenter.
14596
14597 If you change this, don't forget to update the doc string of
14598 `scroll-conservatively' and the Emacs manual. */
14599 #define SCROLL_LIMIT 100
14600
14601 static int
14602 try_scrolling (Lisp_Object window, int just_this_one_p,
14603 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14604 int temp_scroll_step, int last_line_misfit)
14605 {
14606 struct window *w = XWINDOW (window);
14607 struct frame *f = XFRAME (w->frame);
14608 struct text_pos pos, startp;
14609 struct it it;
14610 int this_scroll_margin, scroll_max, rc, height;
14611 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14612 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14613 Lisp_Object aggressive;
14614 /* We will never try scrolling more than this number of lines. */
14615 int scroll_limit = SCROLL_LIMIT;
14616 int frame_line_height = default_line_pixel_height (w);
14617 int window_total_lines
14618 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14619
14620 #ifdef GLYPH_DEBUG
14621 debug_method_add (w, "try_scrolling");
14622 #endif
14623
14624 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14625
14626 /* Compute scroll margin height in pixels. We scroll when point is
14627 within this distance from the top or bottom of the window. */
14628 if (scroll_margin > 0)
14629 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14630 * frame_line_height;
14631 else
14632 this_scroll_margin = 0;
14633
14634 /* Force arg_scroll_conservatively to have a reasonable value, to
14635 avoid scrolling too far away with slow move_it_* functions. Note
14636 that the user can supply scroll-conservatively equal to
14637 `most-positive-fixnum', which can be larger than INT_MAX. */
14638 if (arg_scroll_conservatively > scroll_limit)
14639 {
14640 arg_scroll_conservatively = scroll_limit + 1;
14641 scroll_max = scroll_limit * frame_line_height;
14642 }
14643 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14644 /* Compute how much we should try to scroll maximally to bring
14645 point into view. */
14646 scroll_max = (max (scroll_step,
14647 max (arg_scroll_conservatively, temp_scroll_step))
14648 * frame_line_height);
14649 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14650 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14651 /* We're trying to scroll because of aggressive scrolling but no
14652 scroll_step is set. Choose an arbitrary one. */
14653 scroll_max = 10 * frame_line_height;
14654 else
14655 scroll_max = 0;
14656
14657 too_near_end:
14658
14659 /* Decide whether to scroll down. */
14660 if (PT > CHARPOS (startp))
14661 {
14662 int scroll_margin_y;
14663
14664 /* Compute the pixel ypos of the scroll margin, then move IT to
14665 either that ypos or PT, whichever comes first. */
14666 start_display (&it, w, startp);
14667 scroll_margin_y = it.last_visible_y - this_scroll_margin
14668 - frame_line_height * extra_scroll_margin_lines;
14669 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14670 (MOVE_TO_POS | MOVE_TO_Y));
14671
14672 if (PT > CHARPOS (it.current.pos))
14673 {
14674 int y0 = line_bottom_y (&it);
14675 /* Compute how many pixels below window bottom to stop searching
14676 for PT. This avoids costly search for PT that is far away if
14677 the user limited scrolling by a small number of lines, but
14678 always finds PT if scroll_conservatively is set to a large
14679 number, such as most-positive-fixnum. */
14680 int slack = max (scroll_max, 10 * frame_line_height);
14681 int y_to_move = it.last_visible_y + slack;
14682
14683 /* Compute the distance from the scroll margin to PT or to
14684 the scroll limit, whichever comes first. This should
14685 include the height of the cursor line, to make that line
14686 fully visible. */
14687 move_it_to (&it, PT, -1, y_to_move,
14688 -1, MOVE_TO_POS | MOVE_TO_Y);
14689 dy = line_bottom_y (&it) - y0;
14690
14691 if (dy > scroll_max)
14692 return SCROLLING_FAILED;
14693
14694 if (dy > 0)
14695 scroll_down_p = 1;
14696 }
14697 }
14698
14699 if (scroll_down_p)
14700 {
14701 /* Point is in or below the bottom scroll margin, so move the
14702 window start down. If scrolling conservatively, move it just
14703 enough down to make point visible. If scroll_step is set,
14704 move it down by scroll_step. */
14705 if (arg_scroll_conservatively)
14706 amount_to_scroll
14707 = min (max (dy, frame_line_height),
14708 frame_line_height * arg_scroll_conservatively);
14709 else if (scroll_step || temp_scroll_step)
14710 amount_to_scroll = scroll_max;
14711 else
14712 {
14713 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14714 height = WINDOW_BOX_TEXT_HEIGHT (w);
14715 if (NUMBERP (aggressive))
14716 {
14717 double float_amount = XFLOATINT (aggressive) * height;
14718 int aggressive_scroll = float_amount;
14719 if (aggressive_scroll == 0 && float_amount > 0)
14720 aggressive_scroll = 1;
14721 /* Don't let point enter the scroll margin near top of
14722 the window. This could happen if the value of
14723 scroll_up_aggressively is too large and there are
14724 non-zero margins, because scroll_up_aggressively
14725 means put point that fraction of window height
14726 _from_the_bottom_margin_. */
14727 if (aggressive_scroll + 2*this_scroll_margin > height)
14728 aggressive_scroll = height - 2*this_scroll_margin;
14729 amount_to_scroll = dy + aggressive_scroll;
14730 }
14731 }
14732
14733 if (amount_to_scroll <= 0)
14734 return SCROLLING_FAILED;
14735
14736 start_display (&it, w, startp);
14737 if (arg_scroll_conservatively <= scroll_limit)
14738 move_it_vertically (&it, amount_to_scroll);
14739 else
14740 {
14741 /* Extra precision for users who set scroll-conservatively
14742 to a large number: make sure the amount we scroll
14743 the window start is never less than amount_to_scroll,
14744 which was computed as distance from window bottom to
14745 point. This matters when lines at window top and lines
14746 below window bottom have different height. */
14747 struct it it1;
14748 void *it1data = NULL;
14749 /* We use a temporary it1 because line_bottom_y can modify
14750 its argument, if it moves one line down; see there. */
14751 int start_y;
14752
14753 SAVE_IT (it1, it, it1data);
14754 start_y = line_bottom_y (&it1);
14755 do {
14756 RESTORE_IT (&it, &it, it1data);
14757 move_it_by_lines (&it, 1);
14758 SAVE_IT (it1, it, it1data);
14759 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14760 }
14761
14762 /* If STARTP is unchanged, move it down another screen line. */
14763 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14764 move_it_by_lines (&it, 1);
14765 startp = it.current.pos;
14766 }
14767 else
14768 {
14769 struct text_pos scroll_margin_pos = startp;
14770 int y_offset = 0;
14771
14772 /* See if point is inside the scroll margin at the top of the
14773 window. */
14774 if (this_scroll_margin)
14775 {
14776 int y_start;
14777
14778 start_display (&it, w, startp);
14779 y_start = it.current_y;
14780 move_it_vertically (&it, this_scroll_margin);
14781 scroll_margin_pos = it.current.pos;
14782 /* If we didn't move enough before hitting ZV, request
14783 additional amount of scroll, to move point out of the
14784 scroll margin. */
14785 if (IT_CHARPOS (it) == ZV
14786 && it.current_y - y_start < this_scroll_margin)
14787 y_offset = this_scroll_margin - (it.current_y - y_start);
14788 }
14789
14790 if (PT < CHARPOS (scroll_margin_pos))
14791 {
14792 /* Point is in the scroll margin at the top of the window or
14793 above what is displayed in the window. */
14794 int y0, y_to_move;
14795
14796 /* Compute the vertical distance from PT to the scroll
14797 margin position. Move as far as scroll_max allows, or
14798 one screenful, or 10 screen lines, whichever is largest.
14799 Give up if distance is greater than scroll_max or if we
14800 didn't reach the scroll margin position. */
14801 SET_TEXT_POS (pos, PT, PT_BYTE);
14802 start_display (&it, w, pos);
14803 y0 = it.current_y;
14804 y_to_move = max (it.last_visible_y,
14805 max (scroll_max, 10 * frame_line_height));
14806 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14807 y_to_move, -1,
14808 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14809 dy = it.current_y - y0;
14810 if (dy > scroll_max
14811 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14812 return SCROLLING_FAILED;
14813
14814 /* Additional scroll for when ZV was too close to point. */
14815 dy += y_offset;
14816
14817 /* Compute new window start. */
14818 start_display (&it, w, startp);
14819
14820 if (arg_scroll_conservatively)
14821 amount_to_scroll = max (dy, frame_line_height *
14822 max (scroll_step, temp_scroll_step));
14823 else if (scroll_step || temp_scroll_step)
14824 amount_to_scroll = scroll_max;
14825 else
14826 {
14827 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14828 height = WINDOW_BOX_TEXT_HEIGHT (w);
14829 if (NUMBERP (aggressive))
14830 {
14831 double float_amount = XFLOATINT (aggressive) * height;
14832 int aggressive_scroll = float_amount;
14833 if (aggressive_scroll == 0 && float_amount > 0)
14834 aggressive_scroll = 1;
14835 /* Don't let point enter the scroll margin near
14836 bottom of the window, if the value of
14837 scroll_down_aggressively happens to be too
14838 large. */
14839 if (aggressive_scroll + 2*this_scroll_margin > height)
14840 aggressive_scroll = height - 2*this_scroll_margin;
14841 amount_to_scroll = dy + aggressive_scroll;
14842 }
14843 }
14844
14845 if (amount_to_scroll <= 0)
14846 return SCROLLING_FAILED;
14847
14848 move_it_vertically_backward (&it, amount_to_scroll);
14849 startp = it.current.pos;
14850 }
14851 }
14852
14853 /* Run window scroll functions. */
14854 startp = run_window_scroll_functions (window, startp);
14855
14856 /* Display the window. Give up if new fonts are loaded, or if point
14857 doesn't appear. */
14858 if (!try_window (window, startp, 0))
14859 rc = SCROLLING_NEED_LARGER_MATRICES;
14860 else if (w->cursor.vpos < 0)
14861 {
14862 clear_glyph_matrix (w->desired_matrix);
14863 rc = SCROLLING_FAILED;
14864 }
14865 else
14866 {
14867 /* Maybe forget recorded base line for line number display. */
14868 if (!just_this_one_p
14869 || current_buffer->clip_changed
14870 || BEG_UNCHANGED < CHARPOS (startp))
14871 w->base_line_number = 0;
14872
14873 /* If cursor ends up on a partially visible line,
14874 treat that as being off the bottom of the screen. */
14875 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14876 /* It's possible that the cursor is on the first line of the
14877 buffer, which is partially obscured due to a vscroll
14878 (Bug#7537). In that case, avoid looping forever . */
14879 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14880 {
14881 clear_glyph_matrix (w->desired_matrix);
14882 ++extra_scroll_margin_lines;
14883 goto too_near_end;
14884 }
14885 rc = SCROLLING_SUCCESS;
14886 }
14887
14888 return rc;
14889 }
14890
14891
14892 /* Compute a suitable window start for window W if display of W starts
14893 on a continuation line. Value is non-zero if a new window start
14894 was computed.
14895
14896 The new window start will be computed, based on W's width, starting
14897 from the start of the continued line. It is the start of the
14898 screen line with the minimum distance from the old start W->start. */
14899
14900 static int
14901 compute_window_start_on_continuation_line (struct window *w)
14902 {
14903 struct text_pos pos, start_pos;
14904 int window_start_changed_p = 0;
14905
14906 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14907
14908 /* If window start is on a continuation line... Window start may be
14909 < BEGV in case there's invisible text at the start of the
14910 buffer (M-x rmail, for example). */
14911 if (CHARPOS (start_pos) > BEGV
14912 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14913 {
14914 struct it it;
14915 struct glyph_row *row;
14916
14917 /* Handle the case that the window start is out of range. */
14918 if (CHARPOS (start_pos) < BEGV)
14919 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14920 else if (CHARPOS (start_pos) > ZV)
14921 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14922
14923 /* Find the start of the continued line. This should be fast
14924 because find_newline is fast (newline cache). */
14925 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14926 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14927 row, DEFAULT_FACE_ID);
14928 reseat_at_previous_visible_line_start (&it);
14929
14930 /* If the line start is "too far" away from the window start,
14931 say it takes too much time to compute a new window start. */
14932 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14933 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14934 {
14935 int min_distance, distance;
14936
14937 /* Move forward by display lines to find the new window
14938 start. If window width was enlarged, the new start can
14939 be expected to be > the old start. If window width was
14940 decreased, the new window start will be < the old start.
14941 So, we're looking for the display line start with the
14942 minimum distance from the old window start. */
14943 pos = it.current.pos;
14944 min_distance = INFINITY;
14945 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14946 distance < min_distance)
14947 {
14948 min_distance = distance;
14949 pos = it.current.pos;
14950 if (it.line_wrap == WORD_WRAP)
14951 {
14952 /* Under WORD_WRAP, move_it_by_lines is likely to
14953 overshoot and stop not at the first, but the
14954 second character from the left margin. So in
14955 that case, we need a more tight control on the X
14956 coordinate of the iterator than move_it_by_lines
14957 promises in its contract. The method is to first
14958 go to the last (rightmost) visible character of a
14959 line, then move to the leftmost character on the
14960 next line in a separate call. */
14961 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14962 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14963 move_it_to (&it, ZV, 0,
14964 it.current_y + it.max_ascent + it.max_descent, -1,
14965 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14966 }
14967 else
14968 move_it_by_lines (&it, 1);
14969 }
14970
14971 /* Set the window start there. */
14972 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14973 window_start_changed_p = 1;
14974 }
14975 }
14976
14977 return window_start_changed_p;
14978 }
14979
14980
14981 /* Try cursor movement in case text has not changed in window WINDOW,
14982 with window start STARTP. Value is
14983
14984 CURSOR_MOVEMENT_SUCCESS if successful
14985
14986 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14987
14988 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14989 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14990 we want to scroll as if scroll-step were set to 1. See the code.
14991
14992 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14993 which case we have to abort this redisplay, and adjust matrices
14994 first. */
14995
14996 enum
14997 {
14998 CURSOR_MOVEMENT_SUCCESS,
14999 CURSOR_MOVEMENT_CANNOT_BE_USED,
15000 CURSOR_MOVEMENT_MUST_SCROLL,
15001 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15002 };
15003
15004 static int
15005 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15006 {
15007 struct window *w = XWINDOW (window);
15008 struct frame *f = XFRAME (w->frame);
15009 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15010
15011 #ifdef GLYPH_DEBUG
15012 if (inhibit_try_cursor_movement)
15013 return rc;
15014 #endif
15015
15016 /* Previously, there was a check for Lisp integer in the
15017 if-statement below. Now, this field is converted to
15018 ptrdiff_t, thus zero means invalid position in a buffer. */
15019 eassert (w->last_point > 0);
15020 /* Likewise there was a check whether window_end_vpos is nil or larger
15021 than the window. Now window_end_vpos is int and so never nil, but
15022 let's leave eassert to check whether it fits in the window. */
15023 eassert (w->window_end_vpos < w->current_matrix->nrows);
15024
15025 /* Handle case where text has not changed, only point, and it has
15026 not moved off the frame. */
15027 if (/* Point may be in this window. */
15028 PT >= CHARPOS (startp)
15029 /* Selective display hasn't changed. */
15030 && !current_buffer->clip_changed
15031 /* Function force-mode-line-update is used to force a thorough
15032 redisplay. It sets either windows_or_buffers_changed or
15033 update_mode_lines. So don't take a shortcut here for these
15034 cases. */
15035 && !update_mode_lines
15036 && !windows_or_buffers_changed
15037 && !f->cursor_type_changed
15038 && NILP (Vshow_trailing_whitespace)
15039 /* This code is not used for mini-buffer for the sake of the case
15040 of redisplaying to replace an echo area message; since in
15041 that case the mini-buffer contents per se are usually
15042 unchanged. This code is of no real use in the mini-buffer
15043 since the handling of this_line_start_pos, etc., in redisplay
15044 handles the same cases. */
15045 && !EQ (window, minibuf_window)
15046 && (FRAME_WINDOW_P (f)
15047 || !overlay_arrow_in_current_buffer_p ()))
15048 {
15049 int this_scroll_margin, top_scroll_margin;
15050 struct glyph_row *row = NULL;
15051 int frame_line_height = default_line_pixel_height (w);
15052 int window_total_lines
15053 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15054
15055 #ifdef GLYPH_DEBUG
15056 debug_method_add (w, "cursor movement");
15057 #endif
15058
15059 /* Scroll if point within this distance from the top or bottom
15060 of the window. This is a pixel value. */
15061 if (scroll_margin > 0)
15062 {
15063 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15064 this_scroll_margin *= frame_line_height;
15065 }
15066 else
15067 this_scroll_margin = 0;
15068
15069 top_scroll_margin = this_scroll_margin;
15070 if (WINDOW_WANTS_HEADER_LINE_P (w))
15071 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15072
15073 /* Start with the row the cursor was displayed during the last
15074 not paused redisplay. Give up if that row is not valid. */
15075 if (w->last_cursor_vpos < 0
15076 || w->last_cursor_vpos >= w->current_matrix->nrows)
15077 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15078 else
15079 {
15080 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15081 if (row->mode_line_p)
15082 ++row;
15083 if (!row->enabled_p)
15084 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15085 }
15086
15087 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15088 {
15089 int scroll_p = 0, must_scroll = 0;
15090 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15091
15092 if (PT > w->last_point)
15093 {
15094 /* Point has moved forward. */
15095 while (MATRIX_ROW_END_CHARPOS (row) < PT
15096 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15097 {
15098 eassert (row->enabled_p);
15099 ++row;
15100 }
15101
15102 /* If the end position of a row equals the start
15103 position of the next row, and PT is at that position,
15104 we would rather display cursor in the next line. */
15105 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15106 && MATRIX_ROW_END_CHARPOS (row) == PT
15107 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15108 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15109 && !cursor_row_p (row))
15110 ++row;
15111
15112 /* If within the scroll margin, scroll. Note that
15113 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15114 the next line would be drawn, and that
15115 this_scroll_margin can be zero. */
15116 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15117 || PT > MATRIX_ROW_END_CHARPOS (row)
15118 /* Line is completely visible last line in window
15119 and PT is to be set in the next line. */
15120 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15121 && PT == MATRIX_ROW_END_CHARPOS (row)
15122 && !row->ends_at_zv_p
15123 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15124 scroll_p = 1;
15125 }
15126 else if (PT < w->last_point)
15127 {
15128 /* Cursor has to be moved backward. Note that PT >=
15129 CHARPOS (startp) because of the outer if-statement. */
15130 while (!row->mode_line_p
15131 && (MATRIX_ROW_START_CHARPOS (row) > PT
15132 || (MATRIX_ROW_START_CHARPOS (row) == PT
15133 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15134 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15135 row > w->current_matrix->rows
15136 && (row-1)->ends_in_newline_from_string_p))))
15137 && (row->y > top_scroll_margin
15138 || CHARPOS (startp) == BEGV))
15139 {
15140 eassert (row->enabled_p);
15141 --row;
15142 }
15143
15144 /* Consider the following case: Window starts at BEGV,
15145 there is invisible, intangible text at BEGV, so that
15146 display starts at some point START > BEGV. It can
15147 happen that we are called with PT somewhere between
15148 BEGV and START. Try to handle that case. */
15149 if (row < w->current_matrix->rows
15150 || row->mode_line_p)
15151 {
15152 row = w->current_matrix->rows;
15153 if (row->mode_line_p)
15154 ++row;
15155 }
15156
15157 /* Due to newlines in overlay strings, we may have to
15158 skip forward over overlay strings. */
15159 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15160 && MATRIX_ROW_END_CHARPOS (row) == PT
15161 && !cursor_row_p (row))
15162 ++row;
15163
15164 /* If within the scroll margin, scroll. */
15165 if (row->y < top_scroll_margin
15166 && CHARPOS (startp) != BEGV)
15167 scroll_p = 1;
15168 }
15169 else
15170 {
15171 /* Cursor did not move. So don't scroll even if cursor line
15172 is partially visible, as it was so before. */
15173 rc = CURSOR_MOVEMENT_SUCCESS;
15174 }
15175
15176 if (PT < MATRIX_ROW_START_CHARPOS (row)
15177 || PT > MATRIX_ROW_END_CHARPOS (row))
15178 {
15179 /* if PT is not in the glyph row, give up. */
15180 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15181 must_scroll = 1;
15182 }
15183 else if (rc != CURSOR_MOVEMENT_SUCCESS
15184 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15185 {
15186 struct glyph_row *row1;
15187
15188 /* If rows are bidi-reordered and point moved, back up
15189 until we find a row that does not belong to a
15190 continuation line. This is because we must consider
15191 all rows of a continued line as candidates for the
15192 new cursor positioning, since row start and end
15193 positions change non-linearly with vertical position
15194 in such rows. */
15195 /* FIXME: Revisit this when glyph ``spilling'' in
15196 continuation lines' rows is implemented for
15197 bidi-reordered rows. */
15198 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15199 MATRIX_ROW_CONTINUATION_LINE_P (row);
15200 --row)
15201 {
15202 /* If we hit the beginning of the displayed portion
15203 without finding the first row of a continued
15204 line, give up. */
15205 if (row <= row1)
15206 {
15207 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15208 break;
15209 }
15210 eassert (row->enabled_p);
15211 }
15212 }
15213 if (must_scroll)
15214 ;
15215 else if (rc != CURSOR_MOVEMENT_SUCCESS
15216 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15217 /* Make sure this isn't a header line by any chance, since
15218 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15219 && !row->mode_line_p
15220 && make_cursor_line_fully_visible_p)
15221 {
15222 if (PT == MATRIX_ROW_END_CHARPOS (row)
15223 && !row->ends_at_zv_p
15224 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15225 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15226 else if (row->height > window_box_height (w))
15227 {
15228 /* If we end up in a partially visible line, let's
15229 make it fully visible, except when it's taller
15230 than the window, in which case we can't do much
15231 about it. */
15232 *scroll_step = 1;
15233 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15234 }
15235 else
15236 {
15237 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15238 if (!cursor_row_fully_visible_p (w, 0, 1))
15239 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15240 else
15241 rc = CURSOR_MOVEMENT_SUCCESS;
15242 }
15243 }
15244 else if (scroll_p)
15245 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15246 else if (rc != CURSOR_MOVEMENT_SUCCESS
15247 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15248 {
15249 /* With bidi-reordered rows, there could be more than
15250 one candidate row whose start and end positions
15251 occlude point. We need to let set_cursor_from_row
15252 find the best candidate. */
15253 /* FIXME: Revisit this when glyph ``spilling'' in
15254 continuation lines' rows is implemented for
15255 bidi-reordered rows. */
15256 int rv = 0;
15257
15258 do
15259 {
15260 int at_zv_p = 0, exact_match_p = 0;
15261
15262 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15263 && PT <= MATRIX_ROW_END_CHARPOS (row)
15264 && cursor_row_p (row))
15265 rv |= set_cursor_from_row (w, row, w->current_matrix,
15266 0, 0, 0, 0);
15267 /* As soon as we've found the exact match for point,
15268 or the first suitable row whose ends_at_zv_p flag
15269 is set, we are done. */
15270 at_zv_p =
15271 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15272 if (rv && !at_zv_p
15273 && w->cursor.hpos >= 0
15274 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15275 w->cursor.vpos))
15276 {
15277 struct glyph_row *candidate =
15278 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15279 struct glyph *g =
15280 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15281 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15282
15283 exact_match_p =
15284 (BUFFERP (g->object) && g->charpos == PT)
15285 || (INTEGERP (g->object)
15286 && (g->charpos == PT
15287 || (g->charpos == 0 && endpos - 1 == PT)));
15288 }
15289 if (rv && (at_zv_p || exact_match_p))
15290 {
15291 rc = CURSOR_MOVEMENT_SUCCESS;
15292 break;
15293 }
15294 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15295 break;
15296 ++row;
15297 }
15298 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15299 || row->continued_p)
15300 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15301 || (MATRIX_ROW_START_CHARPOS (row) == PT
15302 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15303 /* If we didn't find any candidate rows, or exited the
15304 loop before all the candidates were examined, signal
15305 to the caller that this method failed. */
15306 if (rc != CURSOR_MOVEMENT_SUCCESS
15307 && !(rv
15308 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15309 && !row->continued_p))
15310 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15311 else if (rv)
15312 rc = CURSOR_MOVEMENT_SUCCESS;
15313 }
15314 else
15315 {
15316 do
15317 {
15318 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15319 {
15320 rc = CURSOR_MOVEMENT_SUCCESS;
15321 break;
15322 }
15323 ++row;
15324 }
15325 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15326 && MATRIX_ROW_START_CHARPOS (row) == PT
15327 && cursor_row_p (row));
15328 }
15329 }
15330 }
15331
15332 return rc;
15333 }
15334
15335 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15336 static
15337 #endif
15338 void
15339 set_vertical_scroll_bar (struct window *w)
15340 {
15341 ptrdiff_t start, end, whole;
15342
15343 /* Calculate the start and end positions for the current window.
15344 At some point, it would be nice to choose between scrollbars
15345 which reflect the whole buffer size, with special markers
15346 indicating narrowing, and scrollbars which reflect only the
15347 visible region.
15348
15349 Note that mini-buffers sometimes aren't displaying any text. */
15350 if (!MINI_WINDOW_P (w)
15351 || (w == XWINDOW (minibuf_window)
15352 && NILP (echo_area_buffer[0])))
15353 {
15354 struct buffer *buf = XBUFFER (w->contents);
15355 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15356 start = marker_position (w->start) - BUF_BEGV (buf);
15357 /* I don't think this is guaranteed to be right. For the
15358 moment, we'll pretend it is. */
15359 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15360
15361 if (end < start)
15362 end = start;
15363 if (whole < (end - start))
15364 whole = end - start;
15365 }
15366 else
15367 start = end = whole = 0;
15368
15369 /* Indicate what this scroll bar ought to be displaying now. */
15370 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15371 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15372 (w, end - start, whole, start);
15373 }
15374
15375
15376 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15377 selected_window is redisplayed.
15378
15379 We can return without actually redisplaying the window if fonts has been
15380 changed on window's frame. In that case, redisplay_internal will retry. */
15381
15382 static void
15383 redisplay_window (Lisp_Object window, int just_this_one_p)
15384 {
15385 struct window *w = XWINDOW (window);
15386 struct frame *f = XFRAME (w->frame);
15387 struct buffer *buffer = XBUFFER (w->contents);
15388 struct buffer *old = current_buffer;
15389 struct text_pos lpoint, opoint, startp;
15390 int update_mode_line;
15391 int tem;
15392 struct it it;
15393 /* Record it now because it's overwritten. */
15394 int current_matrix_up_to_date_p = 0;
15395 int used_current_matrix_p = 0;
15396 /* This is less strict than current_matrix_up_to_date_p.
15397 It indicates that the buffer contents and narrowing are unchanged. */
15398 int buffer_unchanged_p = 0;
15399 int temp_scroll_step = 0;
15400 ptrdiff_t count = SPECPDL_INDEX ();
15401 int rc;
15402 int centering_position = -1;
15403 int last_line_misfit = 0;
15404 ptrdiff_t beg_unchanged, end_unchanged;
15405 int frame_line_height;
15406
15407 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15408 opoint = lpoint;
15409
15410 #ifdef GLYPH_DEBUG
15411 *w->desired_matrix->method = 0;
15412 #endif
15413
15414 if (!just_this_one_p
15415 && REDISPLAY_SOME_P ()
15416 && !w->redisplay
15417 && !f->redisplay
15418 && !buffer->text->redisplay)
15419 return;
15420
15421 /* Make sure that both W's markers are valid. */
15422 eassert (XMARKER (w->start)->buffer == buffer);
15423 eassert (XMARKER (w->pointm)->buffer == buffer);
15424
15425 restart:
15426 reconsider_clip_changes (w);
15427 frame_line_height = default_line_pixel_height (w);
15428
15429 /* Has the mode line to be updated? */
15430 update_mode_line = (w->update_mode_line
15431 || update_mode_lines
15432 || buffer->clip_changed
15433 || buffer->prevent_redisplay_optimizations_p);
15434
15435 if (MINI_WINDOW_P (w))
15436 {
15437 if (w == XWINDOW (echo_area_window)
15438 && !NILP (echo_area_buffer[0]))
15439 {
15440 if (update_mode_line)
15441 /* We may have to update a tty frame's menu bar or a
15442 tool-bar. Example `M-x C-h C-h C-g'. */
15443 goto finish_menu_bars;
15444 else
15445 /* We've already displayed the echo area glyphs in this window. */
15446 goto finish_scroll_bars;
15447 }
15448 else if ((w != XWINDOW (minibuf_window)
15449 || minibuf_level == 0)
15450 /* When buffer is nonempty, redisplay window normally. */
15451 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15452 /* Quail displays non-mini buffers in minibuffer window.
15453 In that case, redisplay the window normally. */
15454 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15455 {
15456 /* W is a mini-buffer window, but it's not active, so clear
15457 it. */
15458 int yb = window_text_bottom_y (w);
15459 struct glyph_row *row;
15460 int y;
15461
15462 for (y = 0, row = w->desired_matrix->rows;
15463 y < yb;
15464 y += row->height, ++row)
15465 blank_row (w, row, y);
15466 goto finish_scroll_bars;
15467 }
15468
15469 clear_glyph_matrix (w->desired_matrix);
15470 }
15471
15472 /* Otherwise set up data on this window; select its buffer and point
15473 value. */
15474 /* Really select the buffer, for the sake of buffer-local
15475 variables. */
15476 set_buffer_internal_1 (XBUFFER (w->contents));
15477
15478 current_matrix_up_to_date_p
15479 = (w->window_end_valid
15480 && !current_buffer->clip_changed
15481 && !current_buffer->prevent_redisplay_optimizations_p
15482 && !window_outdated (w));
15483
15484 /* Run the window-bottom-change-functions
15485 if it is possible that the text on the screen has changed
15486 (either due to modification of the text, or any other reason). */
15487 if (!current_matrix_up_to_date_p
15488 && !NILP (Vwindow_text_change_functions))
15489 {
15490 safe_run_hooks (Qwindow_text_change_functions);
15491 goto restart;
15492 }
15493
15494 beg_unchanged = BEG_UNCHANGED;
15495 end_unchanged = END_UNCHANGED;
15496
15497 SET_TEXT_POS (opoint, PT, PT_BYTE);
15498
15499 specbind (Qinhibit_point_motion_hooks, Qt);
15500
15501 buffer_unchanged_p
15502 = (w->window_end_valid
15503 && !current_buffer->clip_changed
15504 && !window_outdated (w));
15505
15506 /* When windows_or_buffers_changed is non-zero, we can't rely
15507 on the window end being valid, so set it to zero there. */
15508 if (windows_or_buffers_changed)
15509 {
15510 /* If window starts on a continuation line, maybe adjust the
15511 window start in case the window's width changed. */
15512 if (XMARKER (w->start)->buffer == current_buffer)
15513 compute_window_start_on_continuation_line (w);
15514
15515 w->window_end_valid = 0;
15516 /* If so, we also can't rely on current matrix
15517 and should not fool try_cursor_movement below. */
15518 current_matrix_up_to_date_p = 0;
15519 }
15520
15521 /* Some sanity checks. */
15522 CHECK_WINDOW_END (w);
15523 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15524 emacs_abort ();
15525 if (BYTEPOS (opoint) < CHARPOS (opoint))
15526 emacs_abort ();
15527
15528 if (mode_line_update_needed (w))
15529 update_mode_line = 1;
15530
15531 /* Point refers normally to the selected window. For any other
15532 window, set up appropriate value. */
15533 if (!EQ (window, selected_window))
15534 {
15535 ptrdiff_t new_pt = marker_position (w->pointm);
15536 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15537 if (new_pt < BEGV)
15538 {
15539 new_pt = BEGV;
15540 new_pt_byte = BEGV_BYTE;
15541 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15542 }
15543 else if (new_pt > (ZV - 1))
15544 {
15545 new_pt = ZV;
15546 new_pt_byte = ZV_BYTE;
15547 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15548 }
15549
15550 /* We don't use SET_PT so that the point-motion hooks don't run. */
15551 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15552 }
15553
15554 /* If any of the character widths specified in the display table
15555 have changed, invalidate the width run cache. It's true that
15556 this may be a bit late to catch such changes, but the rest of
15557 redisplay goes (non-fatally) haywire when the display table is
15558 changed, so why should we worry about doing any better? */
15559 if (current_buffer->width_run_cache)
15560 {
15561 struct Lisp_Char_Table *disptab = buffer_display_table ();
15562
15563 if (! disptab_matches_widthtab
15564 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15565 {
15566 invalidate_region_cache (current_buffer,
15567 current_buffer->width_run_cache,
15568 BEG, Z);
15569 recompute_width_table (current_buffer, disptab);
15570 }
15571 }
15572
15573 /* If window-start is screwed up, choose a new one. */
15574 if (XMARKER (w->start)->buffer != current_buffer)
15575 goto recenter;
15576
15577 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15578
15579 /* If someone specified a new starting point but did not insist,
15580 check whether it can be used. */
15581 if (w->optional_new_start
15582 && CHARPOS (startp) >= BEGV
15583 && CHARPOS (startp) <= ZV)
15584 {
15585 w->optional_new_start = 0;
15586 start_display (&it, w, startp);
15587 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15588 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15589 if (IT_CHARPOS (it) == PT)
15590 w->force_start = 1;
15591 /* IT may overshoot PT if text at PT is invisible. */
15592 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15593 w->force_start = 1;
15594 }
15595
15596 force_start:
15597
15598 /* Handle case where place to start displaying has been specified,
15599 unless the specified location is outside the accessible range. */
15600 if (w->force_start || window_frozen_p (w))
15601 {
15602 /* We set this later on if we have to adjust point. */
15603 int new_vpos = -1;
15604
15605 w->force_start = 0;
15606 w->vscroll = 0;
15607 w->window_end_valid = 0;
15608
15609 /* Forget any recorded base line for line number display. */
15610 if (!buffer_unchanged_p)
15611 w->base_line_number = 0;
15612
15613 /* Redisplay the mode line. Select the buffer properly for that.
15614 Also, run the hook window-scroll-functions
15615 because we have scrolled. */
15616 /* Note, we do this after clearing force_start because
15617 if there's an error, it is better to forget about force_start
15618 than to get into an infinite loop calling the hook functions
15619 and having them get more errors. */
15620 if (!update_mode_line
15621 || ! NILP (Vwindow_scroll_functions))
15622 {
15623 update_mode_line = 1;
15624 w->update_mode_line = 1;
15625 startp = run_window_scroll_functions (window, startp);
15626 }
15627
15628 if (CHARPOS (startp) < BEGV)
15629 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15630 else if (CHARPOS (startp) > ZV)
15631 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15632
15633 /* Redisplay, then check if cursor has been set during the
15634 redisplay. Give up if new fonts were loaded. */
15635 /* We used to issue a CHECK_MARGINS argument to try_window here,
15636 but this causes scrolling to fail when point begins inside
15637 the scroll margin (bug#148) -- cyd */
15638 if (!try_window (window, startp, 0))
15639 {
15640 w->force_start = 1;
15641 clear_glyph_matrix (w->desired_matrix);
15642 goto need_larger_matrices;
15643 }
15644
15645 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15646 {
15647 /* If point does not appear, try to move point so it does
15648 appear. The desired matrix has been built above, so we
15649 can use it here. */
15650 new_vpos = window_box_height (w) / 2;
15651 }
15652
15653 if (!cursor_row_fully_visible_p (w, 0, 0))
15654 {
15655 /* Point does appear, but on a line partly visible at end of window.
15656 Move it back to a fully-visible line. */
15657 new_vpos = window_box_height (w);
15658 }
15659 else if (w->cursor.vpos >= 0)
15660 {
15661 /* Some people insist on not letting point enter the scroll
15662 margin, even though this part handles windows that didn't
15663 scroll at all. */
15664 int window_total_lines
15665 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15666 int margin = min (scroll_margin, window_total_lines / 4);
15667 int pixel_margin = margin * frame_line_height;
15668 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15669
15670 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15671 below, which finds the row to move point to, advances by
15672 the Y coordinate of the _next_ row, see the definition of
15673 MATRIX_ROW_BOTTOM_Y. */
15674 if (w->cursor.vpos < margin + header_line)
15675 {
15676 w->cursor.vpos = -1;
15677 clear_glyph_matrix (w->desired_matrix);
15678 goto try_to_scroll;
15679 }
15680 else
15681 {
15682 int window_height = window_box_height (w);
15683
15684 if (header_line)
15685 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15686 if (w->cursor.y >= window_height - pixel_margin)
15687 {
15688 w->cursor.vpos = -1;
15689 clear_glyph_matrix (w->desired_matrix);
15690 goto try_to_scroll;
15691 }
15692 }
15693 }
15694
15695 /* If we need to move point for either of the above reasons,
15696 now actually do it. */
15697 if (new_vpos >= 0)
15698 {
15699 struct glyph_row *row;
15700
15701 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15702 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15703 ++row;
15704
15705 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15706 MATRIX_ROW_START_BYTEPOS (row));
15707
15708 if (w != XWINDOW (selected_window))
15709 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15710 else if (current_buffer == old)
15711 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15712
15713 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15714
15715 /* If we are highlighting the region, then we just changed
15716 the region, so redisplay to show it. */
15717 /* FIXME: We need to (re)run pre-redisplay-function! */
15718 /* if (markpos_of_region () >= 0)
15719 {
15720 clear_glyph_matrix (w->desired_matrix);
15721 if (!try_window (window, startp, 0))
15722 goto need_larger_matrices;
15723 }
15724 */
15725 }
15726
15727 #ifdef GLYPH_DEBUG
15728 debug_method_add (w, "forced window start");
15729 #endif
15730 goto done;
15731 }
15732
15733 /* Handle case where text has not changed, only point, and it has
15734 not moved off the frame, and we are not retrying after hscroll.
15735 (current_matrix_up_to_date_p is nonzero when retrying.) */
15736 if (current_matrix_up_to_date_p
15737 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15738 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15739 {
15740 switch (rc)
15741 {
15742 case CURSOR_MOVEMENT_SUCCESS:
15743 used_current_matrix_p = 1;
15744 goto done;
15745
15746 case CURSOR_MOVEMENT_MUST_SCROLL:
15747 goto try_to_scroll;
15748
15749 default:
15750 emacs_abort ();
15751 }
15752 }
15753 /* If current starting point was originally the beginning of a line
15754 but no longer is, find a new starting point. */
15755 else if (w->start_at_line_beg
15756 && !(CHARPOS (startp) <= BEGV
15757 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15758 {
15759 #ifdef GLYPH_DEBUG
15760 debug_method_add (w, "recenter 1");
15761 #endif
15762 goto recenter;
15763 }
15764
15765 /* Try scrolling with try_window_id. Value is > 0 if update has
15766 been done, it is -1 if we know that the same window start will
15767 not work. It is 0 if unsuccessful for some other reason. */
15768 else if ((tem = try_window_id (w)) != 0)
15769 {
15770 #ifdef GLYPH_DEBUG
15771 debug_method_add (w, "try_window_id %d", tem);
15772 #endif
15773
15774 if (f->fonts_changed)
15775 goto need_larger_matrices;
15776 if (tem > 0)
15777 goto done;
15778
15779 /* Otherwise try_window_id has returned -1 which means that we
15780 don't want the alternative below this comment to execute. */
15781 }
15782 else if (CHARPOS (startp) >= BEGV
15783 && CHARPOS (startp) <= ZV
15784 && PT >= CHARPOS (startp)
15785 && (CHARPOS (startp) < ZV
15786 /* Avoid starting at end of buffer. */
15787 || CHARPOS (startp) == BEGV
15788 || !window_outdated (w)))
15789 {
15790 int d1, d2, d3, d4, d5, d6;
15791
15792 /* If first window line is a continuation line, and window start
15793 is inside the modified region, but the first change is before
15794 current window start, we must select a new window start.
15795
15796 However, if this is the result of a down-mouse event (e.g. by
15797 extending the mouse-drag-overlay), we don't want to select a
15798 new window start, since that would change the position under
15799 the mouse, resulting in an unwanted mouse-movement rather
15800 than a simple mouse-click. */
15801 if (!w->start_at_line_beg
15802 && NILP (do_mouse_tracking)
15803 && CHARPOS (startp) > BEGV
15804 && CHARPOS (startp) > BEG + beg_unchanged
15805 && CHARPOS (startp) <= Z - end_unchanged
15806 /* Even if w->start_at_line_beg is nil, a new window may
15807 start at a line_beg, since that's how set_buffer_window
15808 sets it. So, we need to check the return value of
15809 compute_window_start_on_continuation_line. (See also
15810 bug#197). */
15811 && XMARKER (w->start)->buffer == current_buffer
15812 && compute_window_start_on_continuation_line (w)
15813 /* It doesn't make sense to force the window start like we
15814 do at label force_start if it is already known that point
15815 will not be visible in the resulting window, because
15816 doing so will move point from its correct position
15817 instead of scrolling the window to bring point into view.
15818 See bug#9324. */
15819 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15820 {
15821 w->force_start = 1;
15822 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15823 goto force_start;
15824 }
15825
15826 #ifdef GLYPH_DEBUG
15827 debug_method_add (w, "same window start");
15828 #endif
15829
15830 /* Try to redisplay starting at same place as before.
15831 If point has not moved off frame, accept the results. */
15832 if (!current_matrix_up_to_date_p
15833 /* Don't use try_window_reusing_current_matrix in this case
15834 because a window scroll function can have changed the
15835 buffer. */
15836 || !NILP (Vwindow_scroll_functions)
15837 || MINI_WINDOW_P (w)
15838 || !(used_current_matrix_p
15839 = try_window_reusing_current_matrix (w)))
15840 {
15841 IF_DEBUG (debug_method_add (w, "1"));
15842 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15843 /* -1 means we need to scroll.
15844 0 means we need new matrices, but fonts_changed
15845 is set in that case, so we will detect it below. */
15846 goto try_to_scroll;
15847 }
15848
15849 if (f->fonts_changed)
15850 goto need_larger_matrices;
15851
15852 if (w->cursor.vpos >= 0)
15853 {
15854 if (!just_this_one_p
15855 || current_buffer->clip_changed
15856 || BEG_UNCHANGED < CHARPOS (startp))
15857 /* Forget any recorded base line for line number display. */
15858 w->base_line_number = 0;
15859
15860 if (!cursor_row_fully_visible_p (w, 1, 0))
15861 {
15862 clear_glyph_matrix (w->desired_matrix);
15863 last_line_misfit = 1;
15864 }
15865 /* Drop through and scroll. */
15866 else
15867 goto done;
15868 }
15869 else
15870 clear_glyph_matrix (w->desired_matrix);
15871 }
15872
15873 try_to_scroll:
15874
15875 /* Redisplay the mode line. Select the buffer properly for that. */
15876 if (!update_mode_line)
15877 {
15878 update_mode_line = 1;
15879 w->update_mode_line = 1;
15880 }
15881
15882 /* Try to scroll by specified few lines. */
15883 if ((scroll_conservatively
15884 || emacs_scroll_step
15885 || temp_scroll_step
15886 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15887 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15888 && CHARPOS (startp) >= BEGV
15889 && CHARPOS (startp) <= ZV)
15890 {
15891 /* The function returns -1 if new fonts were loaded, 1 if
15892 successful, 0 if not successful. */
15893 int ss = try_scrolling (window, just_this_one_p,
15894 scroll_conservatively,
15895 emacs_scroll_step,
15896 temp_scroll_step, last_line_misfit);
15897 switch (ss)
15898 {
15899 case SCROLLING_SUCCESS:
15900 goto done;
15901
15902 case SCROLLING_NEED_LARGER_MATRICES:
15903 goto need_larger_matrices;
15904
15905 case SCROLLING_FAILED:
15906 break;
15907
15908 default:
15909 emacs_abort ();
15910 }
15911 }
15912
15913 /* Finally, just choose a place to start which positions point
15914 according to user preferences. */
15915
15916 recenter:
15917
15918 #ifdef GLYPH_DEBUG
15919 debug_method_add (w, "recenter");
15920 #endif
15921
15922 /* Forget any previously recorded base line for line number display. */
15923 if (!buffer_unchanged_p)
15924 w->base_line_number = 0;
15925
15926 /* Determine the window start relative to point. */
15927 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15928 it.current_y = it.last_visible_y;
15929 if (centering_position < 0)
15930 {
15931 int window_total_lines
15932 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15933 int margin =
15934 scroll_margin > 0
15935 ? min (scroll_margin, window_total_lines / 4)
15936 : 0;
15937 ptrdiff_t margin_pos = CHARPOS (startp);
15938 Lisp_Object aggressive;
15939 int scrolling_up;
15940
15941 /* If there is a scroll margin at the top of the window, find
15942 its character position. */
15943 if (margin
15944 /* Cannot call start_display if startp is not in the
15945 accessible region of the buffer. This can happen when we
15946 have just switched to a different buffer and/or changed
15947 its restriction. In that case, startp is initialized to
15948 the character position 1 (BEGV) because we did not yet
15949 have chance to display the buffer even once. */
15950 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15951 {
15952 struct it it1;
15953 void *it1data = NULL;
15954
15955 SAVE_IT (it1, it, it1data);
15956 start_display (&it1, w, startp);
15957 move_it_vertically (&it1, margin * frame_line_height);
15958 margin_pos = IT_CHARPOS (it1);
15959 RESTORE_IT (&it, &it, it1data);
15960 }
15961 scrolling_up = PT > margin_pos;
15962 aggressive =
15963 scrolling_up
15964 ? BVAR (current_buffer, scroll_up_aggressively)
15965 : BVAR (current_buffer, scroll_down_aggressively);
15966
15967 if (!MINI_WINDOW_P (w)
15968 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15969 {
15970 int pt_offset = 0;
15971
15972 /* Setting scroll-conservatively overrides
15973 scroll-*-aggressively. */
15974 if (!scroll_conservatively && NUMBERP (aggressive))
15975 {
15976 double float_amount = XFLOATINT (aggressive);
15977
15978 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15979 if (pt_offset == 0 && float_amount > 0)
15980 pt_offset = 1;
15981 if (pt_offset && margin > 0)
15982 margin -= 1;
15983 }
15984 /* Compute how much to move the window start backward from
15985 point so that point will be displayed where the user
15986 wants it. */
15987 if (scrolling_up)
15988 {
15989 centering_position = it.last_visible_y;
15990 if (pt_offset)
15991 centering_position -= pt_offset;
15992 centering_position -=
15993 frame_line_height * (1 + margin + (last_line_misfit != 0))
15994 + WINDOW_HEADER_LINE_HEIGHT (w);
15995 /* Don't let point enter the scroll margin near top of
15996 the window. */
15997 if (centering_position < margin * frame_line_height)
15998 centering_position = margin * frame_line_height;
15999 }
16000 else
16001 centering_position = margin * frame_line_height + pt_offset;
16002 }
16003 else
16004 /* Set the window start half the height of the window backward
16005 from point. */
16006 centering_position = window_box_height (w) / 2;
16007 }
16008 move_it_vertically_backward (&it, centering_position);
16009
16010 eassert (IT_CHARPOS (it) >= BEGV);
16011
16012 /* The function move_it_vertically_backward may move over more
16013 than the specified y-distance. If it->w is small, e.g. a
16014 mini-buffer window, we may end up in front of the window's
16015 display area. Start displaying at the start of the line
16016 containing PT in this case. */
16017 if (it.current_y <= 0)
16018 {
16019 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16020 move_it_vertically_backward (&it, 0);
16021 it.current_y = 0;
16022 }
16023
16024 it.current_x = it.hpos = 0;
16025
16026 /* Set the window start position here explicitly, to avoid an
16027 infinite loop in case the functions in window-scroll-functions
16028 get errors. */
16029 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16030
16031 /* Run scroll hooks. */
16032 startp = run_window_scroll_functions (window, it.current.pos);
16033
16034 /* Redisplay the window. */
16035 if (!current_matrix_up_to_date_p
16036 || windows_or_buffers_changed
16037 || f->cursor_type_changed
16038 /* Don't use try_window_reusing_current_matrix in this case
16039 because it can have changed the buffer. */
16040 || !NILP (Vwindow_scroll_functions)
16041 || !just_this_one_p
16042 || MINI_WINDOW_P (w)
16043 || !(used_current_matrix_p
16044 = try_window_reusing_current_matrix (w)))
16045 try_window (window, startp, 0);
16046
16047 /* If new fonts have been loaded (due to fontsets), give up. We
16048 have to start a new redisplay since we need to re-adjust glyph
16049 matrices. */
16050 if (f->fonts_changed)
16051 goto need_larger_matrices;
16052
16053 /* If cursor did not appear assume that the middle of the window is
16054 in the first line of the window. Do it again with the next line.
16055 (Imagine a window of height 100, displaying two lines of height
16056 60. Moving back 50 from it->last_visible_y will end in the first
16057 line.) */
16058 if (w->cursor.vpos < 0)
16059 {
16060 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16061 {
16062 clear_glyph_matrix (w->desired_matrix);
16063 move_it_by_lines (&it, 1);
16064 try_window (window, it.current.pos, 0);
16065 }
16066 else if (PT < IT_CHARPOS (it))
16067 {
16068 clear_glyph_matrix (w->desired_matrix);
16069 move_it_by_lines (&it, -1);
16070 try_window (window, it.current.pos, 0);
16071 }
16072 else
16073 {
16074 /* Not much we can do about it. */
16075 }
16076 }
16077
16078 /* Consider the following case: Window starts at BEGV, there is
16079 invisible, intangible text at BEGV, so that display starts at
16080 some point START > BEGV. It can happen that we are called with
16081 PT somewhere between BEGV and START. Try to handle that case. */
16082 if (w->cursor.vpos < 0)
16083 {
16084 struct glyph_row *row = w->current_matrix->rows;
16085 if (row->mode_line_p)
16086 ++row;
16087 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16088 }
16089
16090 if (!cursor_row_fully_visible_p (w, 0, 0))
16091 {
16092 /* If vscroll is enabled, disable it and try again. */
16093 if (w->vscroll)
16094 {
16095 w->vscroll = 0;
16096 clear_glyph_matrix (w->desired_matrix);
16097 goto recenter;
16098 }
16099
16100 /* Users who set scroll-conservatively to a large number want
16101 point just above/below the scroll margin. If we ended up
16102 with point's row partially visible, move the window start to
16103 make that row fully visible and out of the margin. */
16104 if (scroll_conservatively > SCROLL_LIMIT)
16105 {
16106 int window_total_lines
16107 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16108 int margin =
16109 scroll_margin > 0
16110 ? min (scroll_margin, window_total_lines / 4)
16111 : 0;
16112 int move_down = w->cursor.vpos >= window_total_lines / 2;
16113
16114 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16115 clear_glyph_matrix (w->desired_matrix);
16116 if (1 == try_window (window, it.current.pos,
16117 TRY_WINDOW_CHECK_MARGINS))
16118 goto done;
16119 }
16120
16121 /* If centering point failed to make the whole line visible,
16122 put point at the top instead. That has to make the whole line
16123 visible, if it can be done. */
16124 if (centering_position == 0)
16125 goto done;
16126
16127 clear_glyph_matrix (w->desired_matrix);
16128 centering_position = 0;
16129 goto recenter;
16130 }
16131
16132 done:
16133
16134 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16135 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16136 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16137
16138 /* Display the mode line, if we must. */
16139 if ((update_mode_line
16140 /* If window not full width, must redo its mode line
16141 if (a) the window to its side is being redone and
16142 (b) we do a frame-based redisplay. This is a consequence
16143 of how inverted lines are drawn in frame-based redisplay. */
16144 || (!just_this_one_p
16145 && !FRAME_WINDOW_P (f)
16146 && !WINDOW_FULL_WIDTH_P (w))
16147 /* Line number to display. */
16148 || w->base_line_pos > 0
16149 /* Column number is displayed and different from the one displayed. */
16150 || (w->column_number_displayed != -1
16151 && (w->column_number_displayed != current_column ())))
16152 /* This means that the window has a mode line. */
16153 && (WINDOW_WANTS_MODELINE_P (w)
16154 || WINDOW_WANTS_HEADER_LINE_P (w)))
16155 {
16156 display_mode_lines (w);
16157
16158 /* If mode line height has changed, arrange for a thorough
16159 immediate redisplay using the correct mode line height. */
16160 if (WINDOW_WANTS_MODELINE_P (w)
16161 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16162 {
16163 f->fonts_changed = 1;
16164 w->mode_line_height = -1;
16165 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16166 = DESIRED_MODE_LINE_HEIGHT (w);
16167 }
16168
16169 /* If header line height has changed, arrange for a thorough
16170 immediate redisplay using the correct header line height. */
16171 if (WINDOW_WANTS_HEADER_LINE_P (w)
16172 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16173 {
16174 f->fonts_changed = 1;
16175 w->header_line_height = -1;
16176 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16177 = DESIRED_HEADER_LINE_HEIGHT (w);
16178 }
16179
16180 if (f->fonts_changed)
16181 goto need_larger_matrices;
16182 }
16183
16184 if (!line_number_displayed && w->base_line_pos != -1)
16185 {
16186 w->base_line_pos = 0;
16187 w->base_line_number = 0;
16188 }
16189
16190 finish_menu_bars:
16191
16192 /* When we reach a frame's selected window, redo the frame's menu bar. */
16193 if (update_mode_line
16194 && EQ (FRAME_SELECTED_WINDOW (f), window))
16195 {
16196 int redisplay_menu_p = 0;
16197
16198 if (FRAME_WINDOW_P (f))
16199 {
16200 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16201 || defined (HAVE_NS) || defined (USE_GTK)
16202 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16203 #else
16204 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16205 #endif
16206 }
16207 else
16208 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16209
16210 if (redisplay_menu_p)
16211 display_menu_bar (w);
16212
16213 #ifdef HAVE_WINDOW_SYSTEM
16214 if (FRAME_WINDOW_P (f))
16215 {
16216 #if defined (USE_GTK) || defined (HAVE_NS)
16217 if (FRAME_EXTERNAL_TOOL_BAR (f))
16218 redisplay_tool_bar (f);
16219 #else
16220 if (WINDOWP (f->tool_bar_window)
16221 && (FRAME_TOOL_BAR_LINES (f) > 0
16222 || !NILP (Vauto_resize_tool_bars))
16223 && redisplay_tool_bar (f))
16224 ignore_mouse_drag_p = 1;
16225 #endif
16226 }
16227 #endif
16228 }
16229
16230 #ifdef HAVE_WINDOW_SYSTEM
16231 if (FRAME_WINDOW_P (f)
16232 && update_window_fringes (w, (just_this_one_p
16233 || (!used_current_matrix_p && !overlay_arrow_seen)
16234 || w->pseudo_window_p)))
16235 {
16236 update_begin (f);
16237 block_input ();
16238 if (draw_window_fringes (w, 1))
16239 x_draw_vertical_border (w);
16240 unblock_input ();
16241 update_end (f);
16242 }
16243 #endif /* HAVE_WINDOW_SYSTEM */
16244
16245 /* We go to this label, with fonts_changed set, if it is
16246 necessary to try again using larger glyph matrices.
16247 We have to redeem the scroll bar even in this case,
16248 because the loop in redisplay_internal expects that. */
16249 need_larger_matrices:
16250 ;
16251 finish_scroll_bars:
16252
16253 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16254 {
16255 /* Set the thumb's position and size. */
16256 set_vertical_scroll_bar (w);
16257
16258 /* Note that we actually used the scroll bar attached to this
16259 window, so it shouldn't be deleted at the end of redisplay. */
16260 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16261 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16262 }
16263
16264 /* Restore current_buffer and value of point in it. The window
16265 update may have changed the buffer, so first make sure `opoint'
16266 is still valid (Bug#6177). */
16267 if (CHARPOS (opoint) < BEGV)
16268 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16269 else if (CHARPOS (opoint) > ZV)
16270 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16271 else
16272 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16273
16274 set_buffer_internal_1 (old);
16275 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16276 shorter. This can be caused by log truncation in *Messages*. */
16277 if (CHARPOS (lpoint) <= ZV)
16278 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16279
16280 unbind_to (count, Qnil);
16281 }
16282
16283
16284 /* Build the complete desired matrix of WINDOW with a window start
16285 buffer position POS.
16286
16287 Value is 1 if successful. It is zero if fonts were loaded during
16288 redisplay which makes re-adjusting glyph matrices necessary, and -1
16289 if point would appear in the scroll margins.
16290 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16291 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16292 set in FLAGS.) */
16293
16294 int
16295 try_window (Lisp_Object window, struct text_pos pos, int flags)
16296 {
16297 struct window *w = XWINDOW (window);
16298 struct it it;
16299 struct glyph_row *last_text_row = NULL;
16300 struct frame *f = XFRAME (w->frame);
16301 int frame_line_height = default_line_pixel_height (w);
16302
16303 /* Make POS the new window start. */
16304 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16305
16306 /* Mark cursor position as unknown. No overlay arrow seen. */
16307 w->cursor.vpos = -1;
16308 overlay_arrow_seen = 0;
16309
16310 /* Initialize iterator and info to start at POS. */
16311 start_display (&it, w, pos);
16312
16313 /* Display all lines of W. */
16314 while (it.current_y < it.last_visible_y)
16315 {
16316 if (display_line (&it))
16317 last_text_row = it.glyph_row - 1;
16318 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16319 return 0;
16320 }
16321
16322 /* Don't let the cursor end in the scroll margins. */
16323 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16324 && !MINI_WINDOW_P (w))
16325 {
16326 int this_scroll_margin;
16327 int window_total_lines
16328 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16329
16330 if (scroll_margin > 0)
16331 {
16332 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16333 this_scroll_margin *= frame_line_height;
16334 }
16335 else
16336 this_scroll_margin = 0;
16337
16338 if ((w->cursor.y >= 0 /* not vscrolled */
16339 && w->cursor.y < this_scroll_margin
16340 && CHARPOS (pos) > BEGV
16341 && IT_CHARPOS (it) < ZV)
16342 /* rms: considering make_cursor_line_fully_visible_p here
16343 seems to give wrong results. We don't want to recenter
16344 when the last line is partly visible, we want to allow
16345 that case to be handled in the usual way. */
16346 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16347 {
16348 w->cursor.vpos = -1;
16349 clear_glyph_matrix (w->desired_matrix);
16350 return -1;
16351 }
16352 }
16353
16354 /* If bottom moved off end of frame, change mode line percentage. */
16355 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16356 w->update_mode_line = 1;
16357
16358 /* Set window_end_pos to the offset of the last character displayed
16359 on the window from the end of current_buffer. Set
16360 window_end_vpos to its row number. */
16361 if (last_text_row)
16362 {
16363 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16364 adjust_window_ends (w, last_text_row, 0);
16365 eassert
16366 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16367 w->window_end_vpos)));
16368 }
16369 else
16370 {
16371 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16372 w->window_end_pos = Z - ZV;
16373 w->window_end_vpos = 0;
16374 }
16375
16376 /* But that is not valid info until redisplay finishes. */
16377 w->window_end_valid = 0;
16378 return 1;
16379 }
16380
16381
16382 \f
16383 /************************************************************************
16384 Window redisplay reusing current matrix when buffer has not changed
16385 ************************************************************************/
16386
16387 /* Try redisplay of window W showing an unchanged buffer with a
16388 different window start than the last time it was displayed by
16389 reusing its current matrix. Value is non-zero if successful.
16390 W->start is the new window start. */
16391
16392 static int
16393 try_window_reusing_current_matrix (struct window *w)
16394 {
16395 struct frame *f = XFRAME (w->frame);
16396 struct glyph_row *bottom_row;
16397 struct it it;
16398 struct run run;
16399 struct text_pos start, new_start;
16400 int nrows_scrolled, i;
16401 struct glyph_row *last_text_row;
16402 struct glyph_row *last_reused_text_row;
16403 struct glyph_row *start_row;
16404 int start_vpos, min_y, max_y;
16405
16406 #ifdef GLYPH_DEBUG
16407 if (inhibit_try_window_reusing)
16408 return 0;
16409 #endif
16410
16411 if (/* This function doesn't handle terminal frames. */
16412 !FRAME_WINDOW_P (f)
16413 /* Don't try to reuse the display if windows have been split
16414 or such. */
16415 || windows_or_buffers_changed
16416 || f->cursor_type_changed)
16417 return 0;
16418
16419 /* Can't do this if showing trailing whitespace. */
16420 if (!NILP (Vshow_trailing_whitespace))
16421 return 0;
16422
16423 /* If top-line visibility has changed, give up. */
16424 if (WINDOW_WANTS_HEADER_LINE_P (w)
16425 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16426 return 0;
16427
16428 /* Give up if old or new display is scrolled vertically. We could
16429 make this function handle this, but right now it doesn't. */
16430 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16431 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16432 return 0;
16433
16434 /* The variable new_start now holds the new window start. The old
16435 start `start' can be determined from the current matrix. */
16436 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16437 start = start_row->minpos;
16438 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16439
16440 /* Clear the desired matrix for the display below. */
16441 clear_glyph_matrix (w->desired_matrix);
16442
16443 if (CHARPOS (new_start) <= CHARPOS (start))
16444 {
16445 /* Don't use this method if the display starts with an ellipsis
16446 displayed for invisible text. It's not easy to handle that case
16447 below, and it's certainly not worth the effort since this is
16448 not a frequent case. */
16449 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16450 return 0;
16451
16452 IF_DEBUG (debug_method_add (w, "twu1"));
16453
16454 /* Display up to a row that can be reused. The variable
16455 last_text_row is set to the last row displayed that displays
16456 text. Note that it.vpos == 0 if or if not there is a
16457 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16458 start_display (&it, w, new_start);
16459 w->cursor.vpos = -1;
16460 last_text_row = last_reused_text_row = NULL;
16461
16462 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16463 {
16464 /* If we have reached into the characters in the START row,
16465 that means the line boundaries have changed. So we
16466 can't start copying with the row START. Maybe it will
16467 work to start copying with the following row. */
16468 while (IT_CHARPOS (it) > CHARPOS (start))
16469 {
16470 /* Advance to the next row as the "start". */
16471 start_row++;
16472 start = start_row->minpos;
16473 /* If there are no more rows to try, or just one, give up. */
16474 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16475 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16476 || CHARPOS (start) == ZV)
16477 {
16478 clear_glyph_matrix (w->desired_matrix);
16479 return 0;
16480 }
16481
16482 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16483 }
16484 /* If we have reached alignment, we can copy the rest of the
16485 rows. */
16486 if (IT_CHARPOS (it) == CHARPOS (start)
16487 /* Don't accept "alignment" inside a display vector,
16488 since start_row could have started in the middle of
16489 that same display vector (thus their character
16490 positions match), and we have no way of telling if
16491 that is the case. */
16492 && it.current.dpvec_index < 0)
16493 break;
16494
16495 if (display_line (&it))
16496 last_text_row = it.glyph_row - 1;
16497
16498 }
16499
16500 /* A value of current_y < last_visible_y means that we stopped
16501 at the previous window start, which in turn means that we
16502 have at least one reusable row. */
16503 if (it.current_y < it.last_visible_y)
16504 {
16505 struct glyph_row *row;
16506
16507 /* IT.vpos always starts from 0; it counts text lines. */
16508 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16509
16510 /* Find PT if not already found in the lines displayed. */
16511 if (w->cursor.vpos < 0)
16512 {
16513 int dy = it.current_y - start_row->y;
16514
16515 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16516 row = row_containing_pos (w, PT, row, NULL, dy);
16517 if (row)
16518 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16519 dy, nrows_scrolled);
16520 else
16521 {
16522 clear_glyph_matrix (w->desired_matrix);
16523 return 0;
16524 }
16525 }
16526
16527 /* Scroll the display. Do it before the current matrix is
16528 changed. The problem here is that update has not yet
16529 run, i.e. part of the current matrix is not up to date.
16530 scroll_run_hook will clear the cursor, and use the
16531 current matrix to get the height of the row the cursor is
16532 in. */
16533 run.current_y = start_row->y;
16534 run.desired_y = it.current_y;
16535 run.height = it.last_visible_y - it.current_y;
16536
16537 if (run.height > 0 && run.current_y != run.desired_y)
16538 {
16539 update_begin (f);
16540 FRAME_RIF (f)->update_window_begin_hook (w);
16541 FRAME_RIF (f)->clear_window_mouse_face (w);
16542 FRAME_RIF (f)->scroll_run_hook (w, &run);
16543 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16544 update_end (f);
16545 }
16546
16547 /* Shift current matrix down by nrows_scrolled lines. */
16548 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16549 rotate_matrix (w->current_matrix,
16550 start_vpos,
16551 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16552 nrows_scrolled);
16553
16554 /* Disable lines that must be updated. */
16555 for (i = 0; i < nrows_scrolled; ++i)
16556 (start_row + i)->enabled_p = 0;
16557
16558 /* Re-compute Y positions. */
16559 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16560 max_y = it.last_visible_y;
16561 for (row = start_row + nrows_scrolled;
16562 row < bottom_row;
16563 ++row)
16564 {
16565 row->y = it.current_y;
16566 row->visible_height = row->height;
16567
16568 if (row->y < min_y)
16569 row->visible_height -= min_y - row->y;
16570 if (row->y + row->height > max_y)
16571 row->visible_height -= row->y + row->height - max_y;
16572 if (row->fringe_bitmap_periodic_p)
16573 row->redraw_fringe_bitmaps_p = 1;
16574
16575 it.current_y += row->height;
16576
16577 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16578 last_reused_text_row = row;
16579 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16580 break;
16581 }
16582
16583 /* Disable lines in the current matrix which are now
16584 below the window. */
16585 for (++row; row < bottom_row; ++row)
16586 row->enabled_p = row->mode_line_p = 0;
16587 }
16588
16589 /* Update window_end_pos etc.; last_reused_text_row is the last
16590 reused row from the current matrix containing text, if any.
16591 The value of last_text_row is the last displayed line
16592 containing text. */
16593 if (last_reused_text_row)
16594 adjust_window_ends (w, last_reused_text_row, 1);
16595 else if (last_text_row)
16596 adjust_window_ends (w, last_text_row, 0);
16597 else
16598 {
16599 /* This window must be completely empty. */
16600 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16601 w->window_end_pos = Z - ZV;
16602 w->window_end_vpos = 0;
16603 }
16604 w->window_end_valid = 0;
16605
16606 /* Update hint: don't try scrolling again in update_window. */
16607 w->desired_matrix->no_scrolling_p = 1;
16608
16609 #ifdef GLYPH_DEBUG
16610 debug_method_add (w, "try_window_reusing_current_matrix 1");
16611 #endif
16612 return 1;
16613 }
16614 else if (CHARPOS (new_start) > CHARPOS (start))
16615 {
16616 struct glyph_row *pt_row, *row;
16617 struct glyph_row *first_reusable_row;
16618 struct glyph_row *first_row_to_display;
16619 int dy;
16620 int yb = window_text_bottom_y (w);
16621
16622 /* Find the row starting at new_start, if there is one. Don't
16623 reuse a partially visible line at the end. */
16624 first_reusable_row = start_row;
16625 while (first_reusable_row->enabled_p
16626 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16627 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16628 < CHARPOS (new_start)))
16629 ++first_reusable_row;
16630
16631 /* Give up if there is no row to reuse. */
16632 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16633 || !first_reusable_row->enabled_p
16634 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16635 != CHARPOS (new_start)))
16636 return 0;
16637
16638 /* We can reuse fully visible rows beginning with
16639 first_reusable_row to the end of the window. Set
16640 first_row_to_display to the first row that cannot be reused.
16641 Set pt_row to the row containing point, if there is any. */
16642 pt_row = NULL;
16643 for (first_row_to_display = first_reusable_row;
16644 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16645 ++first_row_to_display)
16646 {
16647 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16648 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16649 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16650 && first_row_to_display->ends_at_zv_p
16651 && pt_row == NULL)))
16652 pt_row = first_row_to_display;
16653 }
16654
16655 /* Start displaying at the start of first_row_to_display. */
16656 eassert (first_row_to_display->y < yb);
16657 init_to_row_start (&it, w, first_row_to_display);
16658
16659 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16660 - start_vpos);
16661 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16662 - nrows_scrolled);
16663 it.current_y = (first_row_to_display->y - first_reusable_row->y
16664 + WINDOW_HEADER_LINE_HEIGHT (w));
16665
16666 /* Display lines beginning with first_row_to_display in the
16667 desired matrix. Set last_text_row to the last row displayed
16668 that displays text. */
16669 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16670 if (pt_row == NULL)
16671 w->cursor.vpos = -1;
16672 last_text_row = NULL;
16673 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16674 if (display_line (&it))
16675 last_text_row = it.glyph_row - 1;
16676
16677 /* If point is in a reused row, adjust y and vpos of the cursor
16678 position. */
16679 if (pt_row)
16680 {
16681 w->cursor.vpos -= nrows_scrolled;
16682 w->cursor.y -= first_reusable_row->y - start_row->y;
16683 }
16684
16685 /* Give up if point isn't in a row displayed or reused. (This
16686 also handles the case where w->cursor.vpos < nrows_scrolled
16687 after the calls to display_line, which can happen with scroll
16688 margins. See bug#1295.) */
16689 if (w->cursor.vpos < 0)
16690 {
16691 clear_glyph_matrix (w->desired_matrix);
16692 return 0;
16693 }
16694
16695 /* Scroll the display. */
16696 run.current_y = first_reusable_row->y;
16697 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16698 run.height = it.last_visible_y - run.current_y;
16699 dy = run.current_y - run.desired_y;
16700
16701 if (run.height)
16702 {
16703 update_begin (f);
16704 FRAME_RIF (f)->update_window_begin_hook (w);
16705 FRAME_RIF (f)->clear_window_mouse_face (w);
16706 FRAME_RIF (f)->scroll_run_hook (w, &run);
16707 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16708 update_end (f);
16709 }
16710
16711 /* Adjust Y positions of reused rows. */
16712 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16713 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16714 max_y = it.last_visible_y;
16715 for (row = first_reusable_row; row < first_row_to_display; ++row)
16716 {
16717 row->y -= dy;
16718 row->visible_height = row->height;
16719 if (row->y < min_y)
16720 row->visible_height -= min_y - row->y;
16721 if (row->y + row->height > max_y)
16722 row->visible_height -= row->y + row->height - max_y;
16723 if (row->fringe_bitmap_periodic_p)
16724 row->redraw_fringe_bitmaps_p = 1;
16725 }
16726
16727 /* Scroll the current matrix. */
16728 eassert (nrows_scrolled > 0);
16729 rotate_matrix (w->current_matrix,
16730 start_vpos,
16731 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16732 -nrows_scrolled);
16733
16734 /* Disable rows not reused. */
16735 for (row -= nrows_scrolled; row < bottom_row; ++row)
16736 row->enabled_p = 0;
16737
16738 /* Point may have moved to a different line, so we cannot assume that
16739 the previous cursor position is valid; locate the correct row. */
16740 if (pt_row)
16741 {
16742 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16743 row < bottom_row
16744 && PT >= MATRIX_ROW_END_CHARPOS (row)
16745 && !row->ends_at_zv_p;
16746 row++)
16747 {
16748 w->cursor.vpos++;
16749 w->cursor.y = row->y;
16750 }
16751 if (row < bottom_row)
16752 {
16753 /* Can't simply scan the row for point with
16754 bidi-reordered glyph rows. Let set_cursor_from_row
16755 figure out where to put the cursor, and if it fails,
16756 give up. */
16757 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16758 {
16759 if (!set_cursor_from_row (w, row, w->current_matrix,
16760 0, 0, 0, 0))
16761 {
16762 clear_glyph_matrix (w->desired_matrix);
16763 return 0;
16764 }
16765 }
16766 else
16767 {
16768 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16769 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16770
16771 for (; glyph < end
16772 && (!BUFFERP (glyph->object)
16773 || glyph->charpos < PT);
16774 glyph++)
16775 {
16776 w->cursor.hpos++;
16777 w->cursor.x += glyph->pixel_width;
16778 }
16779 }
16780 }
16781 }
16782
16783 /* Adjust window end. A null value of last_text_row means that
16784 the window end is in reused rows which in turn means that
16785 only its vpos can have changed. */
16786 if (last_text_row)
16787 adjust_window_ends (w, last_text_row, 0);
16788 else
16789 w->window_end_vpos -= nrows_scrolled;
16790
16791 w->window_end_valid = 0;
16792 w->desired_matrix->no_scrolling_p = 1;
16793
16794 #ifdef GLYPH_DEBUG
16795 debug_method_add (w, "try_window_reusing_current_matrix 2");
16796 #endif
16797 return 1;
16798 }
16799
16800 return 0;
16801 }
16802
16803
16804 \f
16805 /************************************************************************
16806 Window redisplay reusing current matrix when buffer has changed
16807 ************************************************************************/
16808
16809 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16810 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16811 ptrdiff_t *, ptrdiff_t *);
16812 static struct glyph_row *
16813 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16814 struct glyph_row *);
16815
16816
16817 /* Return the last row in MATRIX displaying text. If row START is
16818 non-null, start searching with that row. IT gives the dimensions
16819 of the display. Value is null if matrix is empty; otherwise it is
16820 a pointer to the row found. */
16821
16822 static struct glyph_row *
16823 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16824 struct glyph_row *start)
16825 {
16826 struct glyph_row *row, *row_found;
16827
16828 /* Set row_found to the last row in IT->w's current matrix
16829 displaying text. The loop looks funny but think of partially
16830 visible lines. */
16831 row_found = NULL;
16832 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16833 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16834 {
16835 eassert (row->enabled_p);
16836 row_found = row;
16837 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16838 break;
16839 ++row;
16840 }
16841
16842 return row_found;
16843 }
16844
16845
16846 /* Return the last row in the current matrix of W that is not affected
16847 by changes at the start of current_buffer that occurred since W's
16848 current matrix was built. Value is null if no such row exists.
16849
16850 BEG_UNCHANGED us the number of characters unchanged at the start of
16851 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16852 first changed character in current_buffer. Characters at positions <
16853 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16854 when the current matrix was built. */
16855
16856 static struct glyph_row *
16857 find_last_unchanged_at_beg_row (struct window *w)
16858 {
16859 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16860 struct glyph_row *row;
16861 struct glyph_row *row_found = NULL;
16862 int yb = window_text_bottom_y (w);
16863
16864 /* Find the last row displaying unchanged text. */
16865 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16866 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16867 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16868 ++row)
16869 {
16870 if (/* If row ends before first_changed_pos, it is unchanged,
16871 except in some case. */
16872 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16873 /* When row ends in ZV and we write at ZV it is not
16874 unchanged. */
16875 && !row->ends_at_zv_p
16876 /* When first_changed_pos is the end of a continued line,
16877 row is not unchanged because it may be no longer
16878 continued. */
16879 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16880 && (row->continued_p
16881 || row->exact_window_width_line_p))
16882 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16883 needs to be recomputed, so don't consider this row as
16884 unchanged. This happens when the last line was
16885 bidi-reordered and was killed immediately before this
16886 redisplay cycle. In that case, ROW->end stores the
16887 buffer position of the first visual-order character of
16888 the killed text, which is now beyond ZV. */
16889 && CHARPOS (row->end.pos) <= ZV)
16890 row_found = row;
16891
16892 /* Stop if last visible row. */
16893 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16894 break;
16895 }
16896
16897 return row_found;
16898 }
16899
16900
16901 /* Find the first glyph row in the current matrix of W that is not
16902 affected by changes at the end of current_buffer since the
16903 time W's current matrix was built.
16904
16905 Return in *DELTA the number of chars by which buffer positions in
16906 unchanged text at the end of current_buffer must be adjusted.
16907
16908 Return in *DELTA_BYTES the corresponding number of bytes.
16909
16910 Value is null if no such row exists, i.e. all rows are affected by
16911 changes. */
16912
16913 static struct glyph_row *
16914 find_first_unchanged_at_end_row (struct window *w,
16915 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16916 {
16917 struct glyph_row *row;
16918 struct glyph_row *row_found = NULL;
16919
16920 *delta = *delta_bytes = 0;
16921
16922 /* Display must not have been paused, otherwise the current matrix
16923 is not up to date. */
16924 eassert (w->window_end_valid);
16925
16926 /* A value of window_end_pos >= END_UNCHANGED means that the window
16927 end is in the range of changed text. If so, there is no
16928 unchanged row at the end of W's current matrix. */
16929 if (w->window_end_pos >= END_UNCHANGED)
16930 return NULL;
16931
16932 /* Set row to the last row in W's current matrix displaying text. */
16933 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16934
16935 /* If matrix is entirely empty, no unchanged row exists. */
16936 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16937 {
16938 /* The value of row is the last glyph row in the matrix having a
16939 meaningful buffer position in it. The end position of row
16940 corresponds to window_end_pos. This allows us to translate
16941 buffer positions in the current matrix to current buffer
16942 positions for characters not in changed text. */
16943 ptrdiff_t Z_old =
16944 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16945 ptrdiff_t Z_BYTE_old =
16946 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16947 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16948 struct glyph_row *first_text_row
16949 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16950
16951 *delta = Z - Z_old;
16952 *delta_bytes = Z_BYTE - Z_BYTE_old;
16953
16954 /* Set last_unchanged_pos to the buffer position of the last
16955 character in the buffer that has not been changed. Z is the
16956 index + 1 of the last character in current_buffer, i.e. by
16957 subtracting END_UNCHANGED we get the index of the last
16958 unchanged character, and we have to add BEG to get its buffer
16959 position. */
16960 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16961 last_unchanged_pos_old = last_unchanged_pos - *delta;
16962
16963 /* Search backward from ROW for a row displaying a line that
16964 starts at a minimum position >= last_unchanged_pos_old. */
16965 for (; row > first_text_row; --row)
16966 {
16967 /* This used to abort, but it can happen.
16968 It is ok to just stop the search instead here. KFS. */
16969 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16970 break;
16971
16972 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16973 row_found = row;
16974 }
16975 }
16976
16977 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16978
16979 return row_found;
16980 }
16981
16982
16983 /* Make sure that glyph rows in the current matrix of window W
16984 reference the same glyph memory as corresponding rows in the
16985 frame's frame matrix. This function is called after scrolling W's
16986 current matrix on a terminal frame in try_window_id and
16987 try_window_reusing_current_matrix. */
16988
16989 static void
16990 sync_frame_with_window_matrix_rows (struct window *w)
16991 {
16992 struct frame *f = XFRAME (w->frame);
16993 struct glyph_row *window_row, *window_row_end, *frame_row;
16994
16995 /* Preconditions: W must be a leaf window and full-width. Its frame
16996 must have a frame matrix. */
16997 eassert (BUFFERP (w->contents));
16998 eassert (WINDOW_FULL_WIDTH_P (w));
16999 eassert (!FRAME_WINDOW_P (f));
17000
17001 /* If W is a full-width window, glyph pointers in W's current matrix
17002 have, by definition, to be the same as glyph pointers in the
17003 corresponding frame matrix. Note that frame matrices have no
17004 marginal areas (see build_frame_matrix). */
17005 window_row = w->current_matrix->rows;
17006 window_row_end = window_row + w->current_matrix->nrows;
17007 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17008 while (window_row < window_row_end)
17009 {
17010 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17011 struct glyph *end = window_row->glyphs[LAST_AREA];
17012
17013 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17014 frame_row->glyphs[TEXT_AREA] = start;
17015 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17016 frame_row->glyphs[LAST_AREA] = end;
17017
17018 /* Disable frame rows whose corresponding window rows have
17019 been disabled in try_window_id. */
17020 if (!window_row->enabled_p)
17021 frame_row->enabled_p = 0;
17022
17023 ++window_row, ++frame_row;
17024 }
17025 }
17026
17027
17028 /* Find the glyph row in window W containing CHARPOS. Consider all
17029 rows between START and END (not inclusive). END null means search
17030 all rows to the end of the display area of W. Value is the row
17031 containing CHARPOS or null. */
17032
17033 struct glyph_row *
17034 row_containing_pos (struct window *w, ptrdiff_t charpos,
17035 struct glyph_row *start, struct glyph_row *end, int dy)
17036 {
17037 struct glyph_row *row = start;
17038 struct glyph_row *best_row = NULL;
17039 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17040 int last_y;
17041
17042 /* If we happen to start on a header-line, skip that. */
17043 if (row->mode_line_p)
17044 ++row;
17045
17046 if ((end && row >= end) || !row->enabled_p)
17047 return NULL;
17048
17049 last_y = window_text_bottom_y (w) - dy;
17050
17051 while (1)
17052 {
17053 /* Give up if we have gone too far. */
17054 if (end && row >= end)
17055 return NULL;
17056 /* This formerly returned if they were equal.
17057 I think that both quantities are of a "last plus one" type;
17058 if so, when they are equal, the row is within the screen. -- rms. */
17059 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17060 return NULL;
17061
17062 /* If it is in this row, return this row. */
17063 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17064 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17065 /* The end position of a row equals the start
17066 position of the next row. If CHARPOS is there, we
17067 would rather consider it displayed in the next
17068 line, except when this line ends in ZV. */
17069 && !row_for_charpos_p (row, charpos)))
17070 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17071 {
17072 struct glyph *g;
17073
17074 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17075 || (!best_row && !row->continued_p))
17076 return row;
17077 /* In bidi-reordered rows, there could be several rows whose
17078 edges surround CHARPOS, all of these rows belonging to
17079 the same continued line. We need to find the row which
17080 fits CHARPOS the best. */
17081 for (g = row->glyphs[TEXT_AREA];
17082 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17083 g++)
17084 {
17085 if (!STRINGP (g->object))
17086 {
17087 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17088 {
17089 mindif = eabs (g->charpos - charpos);
17090 best_row = row;
17091 /* Exact match always wins. */
17092 if (mindif == 0)
17093 return best_row;
17094 }
17095 }
17096 }
17097 }
17098 else if (best_row && !row->continued_p)
17099 return best_row;
17100 ++row;
17101 }
17102 }
17103
17104
17105 /* Try to redisplay window W by reusing its existing display. W's
17106 current matrix must be up to date when this function is called,
17107 i.e. window_end_valid must be nonzero.
17108
17109 Value is
17110
17111 1 if display has been updated
17112 0 if otherwise unsuccessful
17113 -1 if redisplay with same window start is known not to succeed
17114
17115 The following steps are performed:
17116
17117 1. Find the last row in the current matrix of W that is not
17118 affected by changes at the start of current_buffer. If no such row
17119 is found, give up.
17120
17121 2. Find the first row in W's current matrix that is not affected by
17122 changes at the end of current_buffer. Maybe there is no such row.
17123
17124 3. Display lines beginning with the row + 1 found in step 1 to the
17125 row found in step 2 or, if step 2 didn't find a row, to the end of
17126 the window.
17127
17128 4. If cursor is not known to appear on the window, give up.
17129
17130 5. If display stopped at the row found in step 2, scroll the
17131 display and current matrix as needed.
17132
17133 6. Maybe display some lines at the end of W, if we must. This can
17134 happen under various circumstances, like a partially visible line
17135 becoming fully visible, or because newly displayed lines are displayed
17136 in smaller font sizes.
17137
17138 7. Update W's window end information. */
17139
17140 static int
17141 try_window_id (struct window *w)
17142 {
17143 struct frame *f = XFRAME (w->frame);
17144 struct glyph_matrix *current_matrix = w->current_matrix;
17145 struct glyph_matrix *desired_matrix = w->desired_matrix;
17146 struct glyph_row *last_unchanged_at_beg_row;
17147 struct glyph_row *first_unchanged_at_end_row;
17148 struct glyph_row *row;
17149 struct glyph_row *bottom_row;
17150 int bottom_vpos;
17151 struct it it;
17152 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17153 int dvpos, dy;
17154 struct text_pos start_pos;
17155 struct run run;
17156 int first_unchanged_at_end_vpos = 0;
17157 struct glyph_row *last_text_row, *last_text_row_at_end;
17158 struct text_pos start;
17159 ptrdiff_t first_changed_charpos, last_changed_charpos;
17160
17161 #ifdef GLYPH_DEBUG
17162 if (inhibit_try_window_id)
17163 return 0;
17164 #endif
17165
17166 /* This is handy for debugging. */
17167 #if 0
17168 #define GIVE_UP(X) \
17169 do { \
17170 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17171 return 0; \
17172 } while (0)
17173 #else
17174 #define GIVE_UP(X) return 0
17175 #endif
17176
17177 SET_TEXT_POS_FROM_MARKER (start, w->start);
17178
17179 /* Don't use this for mini-windows because these can show
17180 messages and mini-buffers, and we don't handle that here. */
17181 if (MINI_WINDOW_P (w))
17182 GIVE_UP (1);
17183
17184 /* This flag is used to prevent redisplay optimizations. */
17185 if (windows_or_buffers_changed || f->cursor_type_changed)
17186 GIVE_UP (2);
17187
17188 /* Verify that narrowing has not changed.
17189 Also verify that we were not told to prevent redisplay optimizations.
17190 It would be nice to further
17191 reduce the number of cases where this prevents try_window_id. */
17192 if (current_buffer->clip_changed
17193 || current_buffer->prevent_redisplay_optimizations_p)
17194 GIVE_UP (3);
17195
17196 /* Window must either use window-based redisplay or be full width. */
17197 if (!FRAME_WINDOW_P (f)
17198 && (!FRAME_LINE_INS_DEL_OK (f)
17199 || !WINDOW_FULL_WIDTH_P (w)))
17200 GIVE_UP (4);
17201
17202 /* Give up if point is known NOT to appear in W. */
17203 if (PT < CHARPOS (start))
17204 GIVE_UP (5);
17205
17206 /* Another way to prevent redisplay optimizations. */
17207 if (w->last_modified == 0)
17208 GIVE_UP (6);
17209
17210 /* Verify that window is not hscrolled. */
17211 if (w->hscroll != 0)
17212 GIVE_UP (7);
17213
17214 /* Verify that display wasn't paused. */
17215 if (!w->window_end_valid)
17216 GIVE_UP (8);
17217
17218 /* Likewise if highlighting trailing whitespace. */
17219 if (!NILP (Vshow_trailing_whitespace))
17220 GIVE_UP (11);
17221
17222 /* Can't use this if overlay arrow position and/or string have
17223 changed. */
17224 if (overlay_arrows_changed_p ())
17225 GIVE_UP (12);
17226
17227 /* When word-wrap is on, adding a space to the first word of a
17228 wrapped line can change the wrap position, altering the line
17229 above it. It might be worthwhile to handle this more
17230 intelligently, but for now just redisplay from scratch. */
17231 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17232 GIVE_UP (21);
17233
17234 /* Under bidi reordering, adding or deleting a character in the
17235 beginning of a paragraph, before the first strong directional
17236 character, can change the base direction of the paragraph (unless
17237 the buffer specifies a fixed paragraph direction), which will
17238 require to redisplay the whole paragraph. It might be worthwhile
17239 to find the paragraph limits and widen the range of redisplayed
17240 lines to that, but for now just give up this optimization and
17241 redisplay from scratch. */
17242 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17243 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17244 GIVE_UP (22);
17245
17246 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17247 only if buffer has really changed. The reason is that the gap is
17248 initially at Z for freshly visited files. The code below would
17249 set end_unchanged to 0 in that case. */
17250 if (MODIFF > SAVE_MODIFF
17251 /* This seems to happen sometimes after saving a buffer. */
17252 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17253 {
17254 if (GPT - BEG < BEG_UNCHANGED)
17255 BEG_UNCHANGED = GPT - BEG;
17256 if (Z - GPT < END_UNCHANGED)
17257 END_UNCHANGED = Z - GPT;
17258 }
17259
17260 /* The position of the first and last character that has been changed. */
17261 first_changed_charpos = BEG + BEG_UNCHANGED;
17262 last_changed_charpos = Z - END_UNCHANGED;
17263
17264 /* If window starts after a line end, and the last change is in
17265 front of that newline, then changes don't affect the display.
17266 This case happens with stealth-fontification. Note that although
17267 the display is unchanged, glyph positions in the matrix have to
17268 be adjusted, of course. */
17269 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17270 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17271 && ((last_changed_charpos < CHARPOS (start)
17272 && CHARPOS (start) == BEGV)
17273 || (last_changed_charpos < CHARPOS (start) - 1
17274 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17275 {
17276 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17277 struct glyph_row *r0;
17278
17279 /* Compute how many chars/bytes have been added to or removed
17280 from the buffer. */
17281 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17282 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17283 Z_delta = Z - Z_old;
17284 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17285
17286 /* Give up if PT is not in the window. Note that it already has
17287 been checked at the start of try_window_id that PT is not in
17288 front of the window start. */
17289 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17290 GIVE_UP (13);
17291
17292 /* If window start is unchanged, we can reuse the whole matrix
17293 as is, after adjusting glyph positions. No need to compute
17294 the window end again, since its offset from Z hasn't changed. */
17295 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17296 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17297 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17298 /* PT must not be in a partially visible line. */
17299 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17300 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17301 {
17302 /* Adjust positions in the glyph matrix. */
17303 if (Z_delta || Z_delta_bytes)
17304 {
17305 struct glyph_row *r1
17306 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17307 increment_matrix_positions (w->current_matrix,
17308 MATRIX_ROW_VPOS (r0, current_matrix),
17309 MATRIX_ROW_VPOS (r1, current_matrix),
17310 Z_delta, Z_delta_bytes);
17311 }
17312
17313 /* Set the cursor. */
17314 row = row_containing_pos (w, PT, r0, NULL, 0);
17315 if (row)
17316 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17317 return 1;
17318 }
17319 }
17320
17321 /* Handle the case that changes are all below what is displayed in
17322 the window, and that PT is in the window. This shortcut cannot
17323 be taken if ZV is visible in the window, and text has been added
17324 there that is visible in the window. */
17325 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17326 /* ZV is not visible in the window, or there are no
17327 changes at ZV, actually. */
17328 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17329 || first_changed_charpos == last_changed_charpos))
17330 {
17331 struct glyph_row *r0;
17332
17333 /* Give up if PT is not in the window. Note that it already has
17334 been checked at the start of try_window_id that PT is not in
17335 front of the window start. */
17336 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17337 GIVE_UP (14);
17338
17339 /* If window start is unchanged, we can reuse the whole matrix
17340 as is, without changing glyph positions since no text has
17341 been added/removed in front of the window end. */
17342 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17343 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17344 /* PT must not be in a partially visible line. */
17345 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17346 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17347 {
17348 /* We have to compute the window end anew since text
17349 could have been added/removed after it. */
17350 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17351 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17352
17353 /* Set the cursor. */
17354 row = row_containing_pos (w, PT, r0, NULL, 0);
17355 if (row)
17356 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17357 return 2;
17358 }
17359 }
17360
17361 /* Give up if window start is in the changed area.
17362
17363 The condition used to read
17364
17365 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17366
17367 but why that was tested escapes me at the moment. */
17368 if (CHARPOS (start) >= first_changed_charpos
17369 && CHARPOS (start) <= last_changed_charpos)
17370 GIVE_UP (15);
17371
17372 /* Check that window start agrees with the start of the first glyph
17373 row in its current matrix. Check this after we know the window
17374 start is not in changed text, otherwise positions would not be
17375 comparable. */
17376 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17377 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17378 GIVE_UP (16);
17379
17380 /* Give up if the window ends in strings. Overlay strings
17381 at the end are difficult to handle, so don't try. */
17382 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17383 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17384 GIVE_UP (20);
17385
17386 /* Compute the position at which we have to start displaying new
17387 lines. Some of the lines at the top of the window might be
17388 reusable because they are not displaying changed text. Find the
17389 last row in W's current matrix not affected by changes at the
17390 start of current_buffer. Value is null if changes start in the
17391 first line of window. */
17392 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17393 if (last_unchanged_at_beg_row)
17394 {
17395 /* Avoid starting to display in the middle of a character, a TAB
17396 for instance. This is easier than to set up the iterator
17397 exactly, and it's not a frequent case, so the additional
17398 effort wouldn't really pay off. */
17399 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17400 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17401 && last_unchanged_at_beg_row > w->current_matrix->rows)
17402 --last_unchanged_at_beg_row;
17403
17404 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17405 GIVE_UP (17);
17406
17407 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17408 GIVE_UP (18);
17409 start_pos = it.current.pos;
17410
17411 /* Start displaying new lines in the desired matrix at the same
17412 vpos we would use in the current matrix, i.e. below
17413 last_unchanged_at_beg_row. */
17414 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17415 current_matrix);
17416 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17417 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17418
17419 eassert (it.hpos == 0 && it.current_x == 0);
17420 }
17421 else
17422 {
17423 /* There are no reusable lines at the start of the window.
17424 Start displaying in the first text line. */
17425 start_display (&it, w, start);
17426 it.vpos = it.first_vpos;
17427 start_pos = it.current.pos;
17428 }
17429
17430 /* Find the first row that is not affected by changes at the end of
17431 the buffer. Value will be null if there is no unchanged row, in
17432 which case we must redisplay to the end of the window. delta
17433 will be set to the value by which buffer positions beginning with
17434 first_unchanged_at_end_row have to be adjusted due to text
17435 changes. */
17436 first_unchanged_at_end_row
17437 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17438 IF_DEBUG (debug_delta = delta);
17439 IF_DEBUG (debug_delta_bytes = delta_bytes);
17440
17441 /* Set stop_pos to the buffer position up to which we will have to
17442 display new lines. If first_unchanged_at_end_row != NULL, this
17443 is the buffer position of the start of the line displayed in that
17444 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17445 that we don't stop at a buffer position. */
17446 stop_pos = 0;
17447 if (first_unchanged_at_end_row)
17448 {
17449 eassert (last_unchanged_at_beg_row == NULL
17450 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17451
17452 /* If this is a continuation line, move forward to the next one
17453 that isn't. Changes in lines above affect this line.
17454 Caution: this may move first_unchanged_at_end_row to a row
17455 not displaying text. */
17456 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17457 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17458 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17459 < it.last_visible_y))
17460 ++first_unchanged_at_end_row;
17461
17462 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17463 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17464 >= it.last_visible_y))
17465 first_unchanged_at_end_row = NULL;
17466 else
17467 {
17468 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17469 + delta);
17470 first_unchanged_at_end_vpos
17471 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17472 eassert (stop_pos >= Z - END_UNCHANGED);
17473 }
17474 }
17475 else if (last_unchanged_at_beg_row == NULL)
17476 GIVE_UP (19);
17477
17478
17479 #ifdef GLYPH_DEBUG
17480
17481 /* Either there is no unchanged row at the end, or the one we have
17482 now displays text. This is a necessary condition for the window
17483 end pos calculation at the end of this function. */
17484 eassert (first_unchanged_at_end_row == NULL
17485 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17486
17487 debug_last_unchanged_at_beg_vpos
17488 = (last_unchanged_at_beg_row
17489 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17490 : -1);
17491 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17492
17493 #endif /* GLYPH_DEBUG */
17494
17495
17496 /* Display new lines. Set last_text_row to the last new line
17497 displayed which has text on it, i.e. might end up as being the
17498 line where the window_end_vpos is. */
17499 w->cursor.vpos = -1;
17500 last_text_row = NULL;
17501 overlay_arrow_seen = 0;
17502 while (it.current_y < it.last_visible_y
17503 && !f->fonts_changed
17504 && (first_unchanged_at_end_row == NULL
17505 || IT_CHARPOS (it) < stop_pos))
17506 {
17507 if (display_line (&it))
17508 last_text_row = it.glyph_row - 1;
17509 }
17510
17511 if (f->fonts_changed)
17512 return -1;
17513
17514
17515 /* Compute differences in buffer positions, y-positions etc. for
17516 lines reused at the bottom of the window. Compute what we can
17517 scroll. */
17518 if (first_unchanged_at_end_row
17519 /* No lines reused because we displayed everything up to the
17520 bottom of the window. */
17521 && it.current_y < it.last_visible_y)
17522 {
17523 dvpos = (it.vpos
17524 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17525 current_matrix));
17526 dy = it.current_y - first_unchanged_at_end_row->y;
17527 run.current_y = first_unchanged_at_end_row->y;
17528 run.desired_y = run.current_y + dy;
17529 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17530 }
17531 else
17532 {
17533 delta = delta_bytes = dvpos = dy
17534 = run.current_y = run.desired_y = run.height = 0;
17535 first_unchanged_at_end_row = NULL;
17536 }
17537 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17538
17539
17540 /* Find the cursor if not already found. We have to decide whether
17541 PT will appear on this window (it sometimes doesn't, but this is
17542 not a very frequent case.) This decision has to be made before
17543 the current matrix is altered. A value of cursor.vpos < 0 means
17544 that PT is either in one of the lines beginning at
17545 first_unchanged_at_end_row or below the window. Don't care for
17546 lines that might be displayed later at the window end; as
17547 mentioned, this is not a frequent case. */
17548 if (w->cursor.vpos < 0)
17549 {
17550 /* Cursor in unchanged rows at the top? */
17551 if (PT < CHARPOS (start_pos)
17552 && last_unchanged_at_beg_row)
17553 {
17554 row = row_containing_pos (w, PT,
17555 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17556 last_unchanged_at_beg_row + 1, 0);
17557 if (row)
17558 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17559 }
17560
17561 /* Start from first_unchanged_at_end_row looking for PT. */
17562 else if (first_unchanged_at_end_row)
17563 {
17564 row = row_containing_pos (w, PT - delta,
17565 first_unchanged_at_end_row, NULL, 0);
17566 if (row)
17567 set_cursor_from_row (w, row, w->current_matrix, delta,
17568 delta_bytes, dy, dvpos);
17569 }
17570
17571 /* Give up if cursor was not found. */
17572 if (w->cursor.vpos < 0)
17573 {
17574 clear_glyph_matrix (w->desired_matrix);
17575 return -1;
17576 }
17577 }
17578
17579 /* Don't let the cursor end in the scroll margins. */
17580 {
17581 int this_scroll_margin, cursor_height;
17582 int frame_line_height = default_line_pixel_height (w);
17583 int window_total_lines
17584 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17585
17586 this_scroll_margin =
17587 max (0, min (scroll_margin, window_total_lines / 4));
17588 this_scroll_margin *= frame_line_height;
17589 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17590
17591 if ((w->cursor.y < this_scroll_margin
17592 && CHARPOS (start) > BEGV)
17593 /* Old redisplay didn't take scroll margin into account at the bottom,
17594 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17595 || (w->cursor.y + (make_cursor_line_fully_visible_p
17596 ? cursor_height + this_scroll_margin
17597 : 1)) > it.last_visible_y)
17598 {
17599 w->cursor.vpos = -1;
17600 clear_glyph_matrix (w->desired_matrix);
17601 return -1;
17602 }
17603 }
17604
17605 /* Scroll the display. Do it before changing the current matrix so
17606 that xterm.c doesn't get confused about where the cursor glyph is
17607 found. */
17608 if (dy && run.height)
17609 {
17610 update_begin (f);
17611
17612 if (FRAME_WINDOW_P (f))
17613 {
17614 FRAME_RIF (f)->update_window_begin_hook (w);
17615 FRAME_RIF (f)->clear_window_mouse_face (w);
17616 FRAME_RIF (f)->scroll_run_hook (w, &run);
17617 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17618 }
17619 else
17620 {
17621 /* Terminal frame. In this case, dvpos gives the number of
17622 lines to scroll by; dvpos < 0 means scroll up. */
17623 int from_vpos
17624 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17625 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17626 int end = (WINDOW_TOP_EDGE_LINE (w)
17627 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17628 + window_internal_height (w));
17629
17630 #if defined (HAVE_GPM) || defined (MSDOS)
17631 x_clear_window_mouse_face (w);
17632 #endif
17633 /* Perform the operation on the screen. */
17634 if (dvpos > 0)
17635 {
17636 /* Scroll last_unchanged_at_beg_row to the end of the
17637 window down dvpos lines. */
17638 set_terminal_window (f, end);
17639
17640 /* On dumb terminals delete dvpos lines at the end
17641 before inserting dvpos empty lines. */
17642 if (!FRAME_SCROLL_REGION_OK (f))
17643 ins_del_lines (f, end - dvpos, -dvpos);
17644
17645 /* Insert dvpos empty lines in front of
17646 last_unchanged_at_beg_row. */
17647 ins_del_lines (f, from, dvpos);
17648 }
17649 else if (dvpos < 0)
17650 {
17651 /* Scroll up last_unchanged_at_beg_vpos to the end of
17652 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17653 set_terminal_window (f, end);
17654
17655 /* Delete dvpos lines in front of
17656 last_unchanged_at_beg_vpos. ins_del_lines will set
17657 the cursor to the given vpos and emit |dvpos| delete
17658 line sequences. */
17659 ins_del_lines (f, from + dvpos, dvpos);
17660
17661 /* On a dumb terminal insert dvpos empty lines at the
17662 end. */
17663 if (!FRAME_SCROLL_REGION_OK (f))
17664 ins_del_lines (f, end + dvpos, -dvpos);
17665 }
17666
17667 set_terminal_window (f, 0);
17668 }
17669
17670 update_end (f);
17671 }
17672
17673 /* Shift reused rows of the current matrix to the right position.
17674 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17675 text. */
17676 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17677 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17678 if (dvpos < 0)
17679 {
17680 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17681 bottom_vpos, dvpos);
17682 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17683 bottom_vpos);
17684 }
17685 else if (dvpos > 0)
17686 {
17687 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17688 bottom_vpos, dvpos);
17689 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17690 first_unchanged_at_end_vpos + dvpos);
17691 }
17692
17693 /* For frame-based redisplay, make sure that current frame and window
17694 matrix are in sync with respect to glyph memory. */
17695 if (!FRAME_WINDOW_P (f))
17696 sync_frame_with_window_matrix_rows (w);
17697
17698 /* Adjust buffer positions in reused rows. */
17699 if (delta || delta_bytes)
17700 increment_matrix_positions (current_matrix,
17701 first_unchanged_at_end_vpos + dvpos,
17702 bottom_vpos, delta, delta_bytes);
17703
17704 /* Adjust Y positions. */
17705 if (dy)
17706 shift_glyph_matrix (w, current_matrix,
17707 first_unchanged_at_end_vpos + dvpos,
17708 bottom_vpos, dy);
17709
17710 if (first_unchanged_at_end_row)
17711 {
17712 first_unchanged_at_end_row += dvpos;
17713 if (first_unchanged_at_end_row->y >= it.last_visible_y
17714 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17715 first_unchanged_at_end_row = NULL;
17716 }
17717
17718 /* If scrolling up, there may be some lines to display at the end of
17719 the window. */
17720 last_text_row_at_end = NULL;
17721 if (dy < 0)
17722 {
17723 /* Scrolling up can leave for example a partially visible line
17724 at the end of the window to be redisplayed. */
17725 /* Set last_row to the glyph row in the current matrix where the
17726 window end line is found. It has been moved up or down in
17727 the matrix by dvpos. */
17728 int last_vpos = w->window_end_vpos + dvpos;
17729 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17730
17731 /* If last_row is the window end line, it should display text. */
17732 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17733
17734 /* If window end line was partially visible before, begin
17735 displaying at that line. Otherwise begin displaying with the
17736 line following it. */
17737 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17738 {
17739 init_to_row_start (&it, w, last_row);
17740 it.vpos = last_vpos;
17741 it.current_y = last_row->y;
17742 }
17743 else
17744 {
17745 init_to_row_end (&it, w, last_row);
17746 it.vpos = 1 + last_vpos;
17747 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17748 ++last_row;
17749 }
17750
17751 /* We may start in a continuation line. If so, we have to
17752 get the right continuation_lines_width and current_x. */
17753 it.continuation_lines_width = last_row->continuation_lines_width;
17754 it.hpos = it.current_x = 0;
17755
17756 /* Display the rest of the lines at the window end. */
17757 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17758 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17759 {
17760 /* Is it always sure that the display agrees with lines in
17761 the current matrix? I don't think so, so we mark rows
17762 displayed invalid in the current matrix by setting their
17763 enabled_p flag to zero. */
17764 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17765 if (display_line (&it))
17766 last_text_row_at_end = it.glyph_row - 1;
17767 }
17768 }
17769
17770 /* Update window_end_pos and window_end_vpos. */
17771 if (first_unchanged_at_end_row && !last_text_row_at_end)
17772 {
17773 /* Window end line if one of the preserved rows from the current
17774 matrix. Set row to the last row displaying text in current
17775 matrix starting at first_unchanged_at_end_row, after
17776 scrolling. */
17777 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17778 row = find_last_row_displaying_text (w->current_matrix, &it,
17779 first_unchanged_at_end_row);
17780 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17781 adjust_window_ends (w, row, 1);
17782 eassert (w->window_end_bytepos >= 0);
17783 IF_DEBUG (debug_method_add (w, "A"));
17784 }
17785 else if (last_text_row_at_end)
17786 {
17787 adjust_window_ends (w, last_text_row_at_end, 0);
17788 eassert (w->window_end_bytepos >= 0);
17789 IF_DEBUG (debug_method_add (w, "B"));
17790 }
17791 else if (last_text_row)
17792 {
17793 /* We have displayed either to the end of the window or at the
17794 end of the window, i.e. the last row with text is to be found
17795 in the desired matrix. */
17796 adjust_window_ends (w, last_text_row, 0);
17797 eassert (w->window_end_bytepos >= 0);
17798 }
17799 else if (first_unchanged_at_end_row == NULL
17800 && last_text_row == NULL
17801 && last_text_row_at_end == NULL)
17802 {
17803 /* Displayed to end of window, but no line containing text was
17804 displayed. Lines were deleted at the end of the window. */
17805 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17806 int vpos = w->window_end_vpos;
17807 struct glyph_row *current_row = current_matrix->rows + vpos;
17808 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17809
17810 for (row = NULL;
17811 row == NULL && vpos >= first_vpos;
17812 --vpos, --current_row, --desired_row)
17813 {
17814 if (desired_row->enabled_p)
17815 {
17816 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17817 row = desired_row;
17818 }
17819 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17820 row = current_row;
17821 }
17822
17823 eassert (row != NULL);
17824 w->window_end_vpos = vpos + 1;
17825 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17826 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17827 eassert (w->window_end_bytepos >= 0);
17828 IF_DEBUG (debug_method_add (w, "C"));
17829 }
17830 else
17831 emacs_abort ();
17832
17833 IF_DEBUG (debug_end_pos = w->window_end_pos;
17834 debug_end_vpos = w->window_end_vpos);
17835
17836 /* Record that display has not been completed. */
17837 w->window_end_valid = 0;
17838 w->desired_matrix->no_scrolling_p = 1;
17839 return 3;
17840
17841 #undef GIVE_UP
17842 }
17843
17844
17845 \f
17846 /***********************************************************************
17847 More debugging support
17848 ***********************************************************************/
17849
17850 #ifdef GLYPH_DEBUG
17851
17852 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17853 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17854 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17855
17856
17857 /* Dump the contents of glyph matrix MATRIX on stderr.
17858
17859 GLYPHS 0 means don't show glyph contents.
17860 GLYPHS 1 means show glyphs in short form
17861 GLYPHS > 1 means show glyphs in long form. */
17862
17863 void
17864 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17865 {
17866 int i;
17867 for (i = 0; i < matrix->nrows; ++i)
17868 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17869 }
17870
17871
17872 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17873 the glyph row and area where the glyph comes from. */
17874
17875 void
17876 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17877 {
17878 if (glyph->type == CHAR_GLYPH
17879 || glyph->type == GLYPHLESS_GLYPH)
17880 {
17881 fprintf (stderr,
17882 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17883 glyph - row->glyphs[TEXT_AREA],
17884 (glyph->type == CHAR_GLYPH
17885 ? 'C'
17886 : 'G'),
17887 glyph->charpos,
17888 (BUFFERP (glyph->object)
17889 ? 'B'
17890 : (STRINGP (glyph->object)
17891 ? 'S'
17892 : (INTEGERP (glyph->object)
17893 ? '0'
17894 : '-'))),
17895 glyph->pixel_width,
17896 glyph->u.ch,
17897 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17898 ? glyph->u.ch
17899 : '.'),
17900 glyph->face_id,
17901 glyph->left_box_line_p,
17902 glyph->right_box_line_p);
17903 }
17904 else if (glyph->type == STRETCH_GLYPH)
17905 {
17906 fprintf (stderr,
17907 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17908 glyph - row->glyphs[TEXT_AREA],
17909 'S',
17910 glyph->charpos,
17911 (BUFFERP (glyph->object)
17912 ? 'B'
17913 : (STRINGP (glyph->object)
17914 ? 'S'
17915 : (INTEGERP (glyph->object)
17916 ? '0'
17917 : '-'))),
17918 glyph->pixel_width,
17919 0,
17920 ' ',
17921 glyph->face_id,
17922 glyph->left_box_line_p,
17923 glyph->right_box_line_p);
17924 }
17925 else if (glyph->type == IMAGE_GLYPH)
17926 {
17927 fprintf (stderr,
17928 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17929 glyph - row->glyphs[TEXT_AREA],
17930 'I',
17931 glyph->charpos,
17932 (BUFFERP (glyph->object)
17933 ? 'B'
17934 : (STRINGP (glyph->object)
17935 ? 'S'
17936 : (INTEGERP (glyph->object)
17937 ? '0'
17938 : '-'))),
17939 glyph->pixel_width,
17940 glyph->u.img_id,
17941 '.',
17942 glyph->face_id,
17943 glyph->left_box_line_p,
17944 glyph->right_box_line_p);
17945 }
17946 else if (glyph->type == COMPOSITE_GLYPH)
17947 {
17948 fprintf (stderr,
17949 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17950 glyph - row->glyphs[TEXT_AREA],
17951 '+',
17952 glyph->charpos,
17953 (BUFFERP (glyph->object)
17954 ? 'B'
17955 : (STRINGP (glyph->object)
17956 ? 'S'
17957 : (INTEGERP (glyph->object)
17958 ? '0'
17959 : '-'))),
17960 glyph->pixel_width,
17961 glyph->u.cmp.id);
17962 if (glyph->u.cmp.automatic)
17963 fprintf (stderr,
17964 "[%d-%d]",
17965 glyph->slice.cmp.from, glyph->slice.cmp.to);
17966 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17967 glyph->face_id,
17968 glyph->left_box_line_p,
17969 glyph->right_box_line_p);
17970 }
17971 }
17972
17973
17974 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17975 GLYPHS 0 means don't show glyph contents.
17976 GLYPHS 1 means show glyphs in short form
17977 GLYPHS > 1 means show glyphs in long form. */
17978
17979 void
17980 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17981 {
17982 if (glyphs != 1)
17983 {
17984 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17985 fprintf (stderr, "==============================================================================\n");
17986
17987 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17988 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17989 vpos,
17990 MATRIX_ROW_START_CHARPOS (row),
17991 MATRIX_ROW_END_CHARPOS (row),
17992 row->used[TEXT_AREA],
17993 row->contains_overlapping_glyphs_p,
17994 row->enabled_p,
17995 row->truncated_on_left_p,
17996 row->truncated_on_right_p,
17997 row->continued_p,
17998 MATRIX_ROW_CONTINUATION_LINE_P (row),
17999 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18000 row->ends_at_zv_p,
18001 row->fill_line_p,
18002 row->ends_in_middle_of_char_p,
18003 row->starts_in_middle_of_char_p,
18004 row->mouse_face_p,
18005 row->x,
18006 row->y,
18007 row->pixel_width,
18008 row->height,
18009 row->visible_height,
18010 row->ascent,
18011 row->phys_ascent);
18012 /* The next 3 lines should align to "Start" in the header. */
18013 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18014 row->end.overlay_string_index,
18015 row->continuation_lines_width);
18016 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18017 CHARPOS (row->start.string_pos),
18018 CHARPOS (row->end.string_pos));
18019 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18020 row->end.dpvec_index);
18021 }
18022
18023 if (glyphs > 1)
18024 {
18025 int area;
18026
18027 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18028 {
18029 struct glyph *glyph = row->glyphs[area];
18030 struct glyph *glyph_end = glyph + row->used[area];
18031
18032 /* Glyph for a line end in text. */
18033 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18034 ++glyph_end;
18035
18036 if (glyph < glyph_end)
18037 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18038
18039 for (; glyph < glyph_end; ++glyph)
18040 dump_glyph (row, glyph, area);
18041 }
18042 }
18043 else if (glyphs == 1)
18044 {
18045 int area;
18046
18047 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18048 {
18049 char *s = alloca (row->used[area] + 4);
18050 int i;
18051
18052 for (i = 0; i < row->used[area]; ++i)
18053 {
18054 struct glyph *glyph = row->glyphs[area] + i;
18055 if (i == row->used[area] - 1
18056 && area == TEXT_AREA
18057 && INTEGERP (glyph->object)
18058 && glyph->type == CHAR_GLYPH
18059 && glyph->u.ch == ' ')
18060 {
18061 strcpy (&s[i], "[\\n]");
18062 i += 4;
18063 }
18064 else if (glyph->type == CHAR_GLYPH
18065 && glyph->u.ch < 0x80
18066 && glyph->u.ch >= ' ')
18067 s[i] = glyph->u.ch;
18068 else
18069 s[i] = '.';
18070 }
18071
18072 s[i] = '\0';
18073 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18074 }
18075 }
18076 }
18077
18078
18079 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18080 Sdump_glyph_matrix, 0, 1, "p",
18081 doc: /* Dump the current matrix of the selected window to stderr.
18082 Shows contents of glyph row structures. With non-nil
18083 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18084 glyphs in short form, otherwise show glyphs in long form. */)
18085 (Lisp_Object glyphs)
18086 {
18087 struct window *w = XWINDOW (selected_window);
18088 struct buffer *buffer = XBUFFER (w->contents);
18089
18090 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18091 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18092 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18093 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18094 fprintf (stderr, "=============================================\n");
18095 dump_glyph_matrix (w->current_matrix,
18096 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18097 return Qnil;
18098 }
18099
18100
18101 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18102 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18103 (void)
18104 {
18105 struct frame *f = XFRAME (selected_frame);
18106 dump_glyph_matrix (f->current_matrix, 1);
18107 return Qnil;
18108 }
18109
18110
18111 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18112 doc: /* Dump glyph row ROW to stderr.
18113 GLYPH 0 means don't dump glyphs.
18114 GLYPH 1 means dump glyphs in short form.
18115 GLYPH > 1 or omitted means dump glyphs in long form. */)
18116 (Lisp_Object row, Lisp_Object glyphs)
18117 {
18118 struct glyph_matrix *matrix;
18119 EMACS_INT vpos;
18120
18121 CHECK_NUMBER (row);
18122 matrix = XWINDOW (selected_window)->current_matrix;
18123 vpos = XINT (row);
18124 if (vpos >= 0 && vpos < matrix->nrows)
18125 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18126 vpos,
18127 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18128 return Qnil;
18129 }
18130
18131
18132 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18133 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18134 GLYPH 0 means don't dump glyphs.
18135 GLYPH 1 means dump glyphs in short form.
18136 GLYPH > 1 or omitted means dump glyphs in long form.
18137
18138 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18139 do nothing. */)
18140 (Lisp_Object row, Lisp_Object glyphs)
18141 {
18142 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18143 struct frame *sf = SELECTED_FRAME ();
18144 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18145 EMACS_INT vpos;
18146
18147 CHECK_NUMBER (row);
18148 vpos = XINT (row);
18149 if (vpos >= 0 && vpos < m->nrows)
18150 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18151 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18152 #endif
18153 return Qnil;
18154 }
18155
18156
18157 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18158 doc: /* Toggle tracing of redisplay.
18159 With ARG, turn tracing on if and only if ARG is positive. */)
18160 (Lisp_Object arg)
18161 {
18162 if (NILP (arg))
18163 trace_redisplay_p = !trace_redisplay_p;
18164 else
18165 {
18166 arg = Fprefix_numeric_value (arg);
18167 trace_redisplay_p = XINT (arg) > 0;
18168 }
18169
18170 return Qnil;
18171 }
18172
18173
18174 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18175 doc: /* Like `format', but print result to stderr.
18176 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18177 (ptrdiff_t nargs, Lisp_Object *args)
18178 {
18179 Lisp_Object s = Fformat (nargs, args);
18180 fprintf (stderr, "%s", SDATA (s));
18181 return Qnil;
18182 }
18183
18184 #endif /* GLYPH_DEBUG */
18185
18186
18187 \f
18188 /***********************************************************************
18189 Building Desired Matrix Rows
18190 ***********************************************************************/
18191
18192 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18193 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18194
18195 static struct glyph_row *
18196 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18197 {
18198 struct frame *f = XFRAME (WINDOW_FRAME (w));
18199 struct buffer *buffer = XBUFFER (w->contents);
18200 struct buffer *old = current_buffer;
18201 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18202 int arrow_len = SCHARS (overlay_arrow_string);
18203 const unsigned char *arrow_end = arrow_string + arrow_len;
18204 const unsigned char *p;
18205 struct it it;
18206 bool multibyte_p;
18207 int n_glyphs_before;
18208
18209 set_buffer_temp (buffer);
18210 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18211 it.glyph_row->used[TEXT_AREA] = 0;
18212 SET_TEXT_POS (it.position, 0, 0);
18213
18214 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18215 p = arrow_string;
18216 while (p < arrow_end)
18217 {
18218 Lisp_Object face, ilisp;
18219
18220 /* Get the next character. */
18221 if (multibyte_p)
18222 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18223 else
18224 {
18225 it.c = it.char_to_display = *p, it.len = 1;
18226 if (! ASCII_CHAR_P (it.c))
18227 it.char_to_display = BYTE8_TO_CHAR (it.c);
18228 }
18229 p += it.len;
18230
18231 /* Get its face. */
18232 ilisp = make_number (p - arrow_string);
18233 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18234 it.face_id = compute_char_face (f, it.char_to_display, face);
18235
18236 /* Compute its width, get its glyphs. */
18237 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18238 SET_TEXT_POS (it.position, -1, -1);
18239 PRODUCE_GLYPHS (&it);
18240
18241 /* If this character doesn't fit any more in the line, we have
18242 to remove some glyphs. */
18243 if (it.current_x > it.last_visible_x)
18244 {
18245 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18246 break;
18247 }
18248 }
18249
18250 set_buffer_temp (old);
18251 return it.glyph_row;
18252 }
18253
18254
18255 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18256 glyphs to insert is determined by produce_special_glyphs. */
18257
18258 static void
18259 insert_left_trunc_glyphs (struct it *it)
18260 {
18261 struct it truncate_it;
18262 struct glyph *from, *end, *to, *toend;
18263
18264 eassert (!FRAME_WINDOW_P (it->f)
18265 || (!it->glyph_row->reversed_p
18266 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18267 || (it->glyph_row->reversed_p
18268 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18269
18270 /* Get the truncation glyphs. */
18271 truncate_it = *it;
18272 truncate_it.current_x = 0;
18273 truncate_it.face_id = DEFAULT_FACE_ID;
18274 truncate_it.glyph_row = &scratch_glyph_row;
18275 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18276 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18277 truncate_it.object = make_number (0);
18278 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18279
18280 /* Overwrite glyphs from IT with truncation glyphs. */
18281 if (!it->glyph_row->reversed_p)
18282 {
18283 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18284
18285 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18286 end = from + tused;
18287 to = it->glyph_row->glyphs[TEXT_AREA];
18288 toend = to + it->glyph_row->used[TEXT_AREA];
18289 if (FRAME_WINDOW_P (it->f))
18290 {
18291 /* On GUI frames, when variable-size fonts are displayed,
18292 the truncation glyphs may need more pixels than the row's
18293 glyphs they overwrite. We overwrite more glyphs to free
18294 enough screen real estate, and enlarge the stretch glyph
18295 on the right (see display_line), if there is one, to
18296 preserve the screen position of the truncation glyphs on
18297 the right. */
18298 int w = 0;
18299 struct glyph *g = to;
18300 short used;
18301
18302 /* The first glyph could be partially visible, in which case
18303 it->glyph_row->x will be negative. But we want the left
18304 truncation glyphs to be aligned at the left margin of the
18305 window, so we override the x coordinate at which the row
18306 will begin. */
18307 it->glyph_row->x = 0;
18308 while (g < toend && w < it->truncation_pixel_width)
18309 {
18310 w += g->pixel_width;
18311 ++g;
18312 }
18313 if (g - to - tused > 0)
18314 {
18315 memmove (to + tused, g, (toend - g) * sizeof(*g));
18316 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18317 }
18318 used = it->glyph_row->used[TEXT_AREA];
18319 if (it->glyph_row->truncated_on_right_p
18320 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18321 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18322 == STRETCH_GLYPH)
18323 {
18324 int extra = w - it->truncation_pixel_width;
18325
18326 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18327 }
18328 }
18329
18330 while (from < end)
18331 *to++ = *from++;
18332
18333 /* There may be padding glyphs left over. Overwrite them too. */
18334 if (!FRAME_WINDOW_P (it->f))
18335 {
18336 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18337 {
18338 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18339 while (from < end)
18340 *to++ = *from++;
18341 }
18342 }
18343
18344 if (to > toend)
18345 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18346 }
18347 else
18348 {
18349 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18350
18351 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18352 that back to front. */
18353 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18354 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18355 toend = it->glyph_row->glyphs[TEXT_AREA];
18356 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18357 if (FRAME_WINDOW_P (it->f))
18358 {
18359 int w = 0;
18360 struct glyph *g = to;
18361
18362 while (g >= toend && w < it->truncation_pixel_width)
18363 {
18364 w += g->pixel_width;
18365 --g;
18366 }
18367 if (to - g - tused > 0)
18368 to = g + tused;
18369 if (it->glyph_row->truncated_on_right_p
18370 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18371 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18372 {
18373 int extra = w - it->truncation_pixel_width;
18374
18375 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18376 }
18377 }
18378
18379 while (from >= end && to >= toend)
18380 *to-- = *from--;
18381 if (!FRAME_WINDOW_P (it->f))
18382 {
18383 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18384 {
18385 from =
18386 truncate_it.glyph_row->glyphs[TEXT_AREA]
18387 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18388 while (from >= end && to >= toend)
18389 *to-- = *from--;
18390 }
18391 }
18392 if (from >= end)
18393 {
18394 /* Need to free some room before prepending additional
18395 glyphs. */
18396 int move_by = from - end + 1;
18397 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18398 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18399
18400 for ( ; g >= g0; g--)
18401 g[move_by] = *g;
18402 while (from >= end)
18403 *to-- = *from--;
18404 it->glyph_row->used[TEXT_AREA] += move_by;
18405 }
18406 }
18407 }
18408
18409 /* Compute the hash code for ROW. */
18410 unsigned
18411 row_hash (struct glyph_row *row)
18412 {
18413 int area, k;
18414 unsigned hashval = 0;
18415
18416 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18417 for (k = 0; k < row->used[area]; ++k)
18418 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18419 + row->glyphs[area][k].u.val
18420 + row->glyphs[area][k].face_id
18421 + row->glyphs[area][k].padding_p
18422 + (row->glyphs[area][k].type << 2));
18423
18424 return hashval;
18425 }
18426
18427 /* Compute the pixel height and width of IT->glyph_row.
18428
18429 Most of the time, ascent and height of a display line will be equal
18430 to the max_ascent and max_height values of the display iterator
18431 structure. This is not the case if
18432
18433 1. We hit ZV without displaying anything. In this case, max_ascent
18434 and max_height will be zero.
18435
18436 2. We have some glyphs that don't contribute to the line height.
18437 (The glyph row flag contributes_to_line_height_p is for future
18438 pixmap extensions).
18439
18440 The first case is easily covered by using default values because in
18441 these cases, the line height does not really matter, except that it
18442 must not be zero. */
18443
18444 static void
18445 compute_line_metrics (struct it *it)
18446 {
18447 struct glyph_row *row = it->glyph_row;
18448
18449 if (FRAME_WINDOW_P (it->f))
18450 {
18451 int i, min_y, max_y;
18452
18453 /* The line may consist of one space only, that was added to
18454 place the cursor on it. If so, the row's height hasn't been
18455 computed yet. */
18456 if (row->height == 0)
18457 {
18458 if (it->max_ascent + it->max_descent == 0)
18459 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18460 row->ascent = it->max_ascent;
18461 row->height = it->max_ascent + it->max_descent;
18462 row->phys_ascent = it->max_phys_ascent;
18463 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18464 row->extra_line_spacing = it->max_extra_line_spacing;
18465 }
18466
18467 /* Compute the width of this line. */
18468 row->pixel_width = row->x;
18469 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18470 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18471
18472 eassert (row->pixel_width >= 0);
18473 eassert (row->ascent >= 0 && row->height > 0);
18474
18475 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18476 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18477
18478 /* If first line's physical ascent is larger than its logical
18479 ascent, use the physical ascent, and make the row taller.
18480 This makes accented characters fully visible. */
18481 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18482 && row->phys_ascent > row->ascent)
18483 {
18484 row->height += row->phys_ascent - row->ascent;
18485 row->ascent = row->phys_ascent;
18486 }
18487
18488 /* Compute how much of the line is visible. */
18489 row->visible_height = row->height;
18490
18491 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18492 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18493
18494 if (row->y < min_y)
18495 row->visible_height -= min_y - row->y;
18496 if (row->y + row->height > max_y)
18497 row->visible_height -= row->y + row->height - max_y;
18498 }
18499 else
18500 {
18501 row->pixel_width = row->used[TEXT_AREA];
18502 if (row->continued_p)
18503 row->pixel_width -= it->continuation_pixel_width;
18504 else if (row->truncated_on_right_p)
18505 row->pixel_width -= it->truncation_pixel_width;
18506 row->ascent = row->phys_ascent = 0;
18507 row->height = row->phys_height = row->visible_height = 1;
18508 row->extra_line_spacing = 0;
18509 }
18510
18511 /* Compute a hash code for this row. */
18512 row->hash = row_hash (row);
18513
18514 it->max_ascent = it->max_descent = 0;
18515 it->max_phys_ascent = it->max_phys_descent = 0;
18516 }
18517
18518
18519 /* Append one space to the glyph row of iterator IT if doing a
18520 window-based redisplay. The space has the same face as
18521 IT->face_id. Value is non-zero if a space was added.
18522
18523 This function is called to make sure that there is always one glyph
18524 at the end of a glyph row that the cursor can be set on under
18525 window-systems. (If there weren't such a glyph we would not know
18526 how wide and tall a box cursor should be displayed).
18527
18528 At the same time this space let's a nicely handle clearing to the
18529 end of the line if the row ends in italic text. */
18530
18531 static int
18532 append_space_for_newline (struct it *it, int default_face_p)
18533 {
18534 if (FRAME_WINDOW_P (it->f))
18535 {
18536 int n = it->glyph_row->used[TEXT_AREA];
18537
18538 if (it->glyph_row->glyphs[TEXT_AREA] + n
18539 < it->glyph_row->glyphs[1 + TEXT_AREA])
18540 {
18541 /* Save some values that must not be changed.
18542 Must save IT->c and IT->len because otherwise
18543 ITERATOR_AT_END_P wouldn't work anymore after
18544 append_space_for_newline has been called. */
18545 enum display_element_type saved_what = it->what;
18546 int saved_c = it->c, saved_len = it->len;
18547 int saved_char_to_display = it->char_to_display;
18548 int saved_x = it->current_x;
18549 int saved_face_id = it->face_id;
18550 int saved_box_end = it->end_of_box_run_p;
18551 struct text_pos saved_pos;
18552 Lisp_Object saved_object;
18553 struct face *face;
18554
18555 saved_object = it->object;
18556 saved_pos = it->position;
18557
18558 it->what = IT_CHARACTER;
18559 memset (&it->position, 0, sizeof it->position);
18560 it->object = make_number (0);
18561 it->c = it->char_to_display = ' ';
18562 it->len = 1;
18563
18564 /* If the default face was remapped, be sure to use the
18565 remapped face for the appended newline. */
18566 if (default_face_p)
18567 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18568 else if (it->face_before_selective_p)
18569 it->face_id = it->saved_face_id;
18570 face = FACE_FROM_ID (it->f, it->face_id);
18571 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18572 /* In R2L rows, we will prepend a stretch glyph that will
18573 have the end_of_box_run_p flag set for it, so there's no
18574 need for the appended newline glyph to have that flag
18575 set. */
18576 if (it->glyph_row->reversed_p
18577 /* But if the appended newline glyph goes all the way to
18578 the end of the row, there will be no stretch glyph,
18579 so leave the box flag set. */
18580 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18581 it->end_of_box_run_p = 0;
18582
18583 PRODUCE_GLYPHS (it);
18584
18585 it->override_ascent = -1;
18586 it->constrain_row_ascent_descent_p = 0;
18587 it->current_x = saved_x;
18588 it->object = saved_object;
18589 it->position = saved_pos;
18590 it->what = saved_what;
18591 it->face_id = saved_face_id;
18592 it->len = saved_len;
18593 it->c = saved_c;
18594 it->char_to_display = saved_char_to_display;
18595 it->end_of_box_run_p = saved_box_end;
18596 return 1;
18597 }
18598 }
18599
18600 return 0;
18601 }
18602
18603
18604 /* Extend the face of the last glyph in the text area of IT->glyph_row
18605 to the end of the display line. Called from display_line. If the
18606 glyph row is empty, add a space glyph to it so that we know the
18607 face to draw. Set the glyph row flag fill_line_p. If the glyph
18608 row is R2L, prepend a stretch glyph to cover the empty space to the
18609 left of the leftmost glyph. */
18610
18611 static void
18612 extend_face_to_end_of_line (struct it *it)
18613 {
18614 struct face *face, *default_face;
18615 struct frame *f = it->f;
18616
18617 /* If line is already filled, do nothing. Non window-system frames
18618 get a grace of one more ``pixel'' because their characters are
18619 1-``pixel'' wide, so they hit the equality too early. This grace
18620 is needed only for R2L rows that are not continued, to produce
18621 one extra blank where we could display the cursor. */
18622 if (it->current_x >= it->last_visible_x
18623 + (!FRAME_WINDOW_P (f)
18624 && it->glyph_row->reversed_p
18625 && !it->glyph_row->continued_p))
18626 return;
18627
18628 /* The default face, possibly remapped. */
18629 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18630
18631 /* Face extension extends the background and box of IT->face_id
18632 to the end of the line. If the background equals the background
18633 of the frame, we don't have to do anything. */
18634 if (it->face_before_selective_p)
18635 face = FACE_FROM_ID (f, it->saved_face_id);
18636 else
18637 face = FACE_FROM_ID (f, it->face_id);
18638
18639 if (FRAME_WINDOW_P (f)
18640 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18641 && face->box == FACE_NO_BOX
18642 && face->background == FRAME_BACKGROUND_PIXEL (f)
18643 #ifdef HAVE_WINDOW_SYSTEM
18644 && !face->stipple
18645 #endif
18646 && !it->glyph_row->reversed_p)
18647 return;
18648
18649 /* Set the glyph row flag indicating that the face of the last glyph
18650 in the text area has to be drawn to the end of the text area. */
18651 it->glyph_row->fill_line_p = 1;
18652
18653 /* If current character of IT is not ASCII, make sure we have the
18654 ASCII face. This will be automatically undone the next time
18655 get_next_display_element returns a multibyte character. Note
18656 that the character will always be single byte in unibyte
18657 text. */
18658 if (!ASCII_CHAR_P (it->c))
18659 {
18660 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18661 }
18662
18663 if (FRAME_WINDOW_P (f))
18664 {
18665 /* If the row is empty, add a space with the current face of IT,
18666 so that we know which face to draw. */
18667 if (it->glyph_row->used[TEXT_AREA] == 0)
18668 {
18669 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18670 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18671 it->glyph_row->used[TEXT_AREA] = 1;
18672 }
18673 #ifdef HAVE_WINDOW_SYSTEM
18674 if (it->glyph_row->reversed_p)
18675 {
18676 /* Prepend a stretch glyph to the row, such that the
18677 rightmost glyph will be drawn flushed all the way to the
18678 right margin of the window. The stretch glyph that will
18679 occupy the empty space, if any, to the left of the
18680 glyphs. */
18681 struct font *font = face->font ? face->font : FRAME_FONT (f);
18682 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18683 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18684 struct glyph *g;
18685 int row_width, stretch_ascent, stretch_width;
18686 struct text_pos saved_pos;
18687 int saved_face_id, saved_avoid_cursor, saved_box_start;
18688
18689 for (row_width = 0, g = row_start; g < row_end; g++)
18690 row_width += g->pixel_width;
18691 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18692 if (stretch_width > 0)
18693 {
18694 stretch_ascent =
18695 (((it->ascent + it->descent)
18696 * FONT_BASE (font)) / FONT_HEIGHT (font));
18697 saved_pos = it->position;
18698 memset (&it->position, 0, sizeof it->position);
18699 saved_avoid_cursor = it->avoid_cursor_p;
18700 it->avoid_cursor_p = 1;
18701 saved_face_id = it->face_id;
18702 saved_box_start = it->start_of_box_run_p;
18703 /* The last row's stretch glyph should get the default
18704 face, to avoid painting the rest of the window with
18705 the region face, if the region ends at ZV. */
18706 if (it->glyph_row->ends_at_zv_p)
18707 it->face_id = default_face->id;
18708 else
18709 it->face_id = face->id;
18710 it->start_of_box_run_p = 0;
18711 append_stretch_glyph (it, make_number (0), stretch_width,
18712 it->ascent + it->descent, stretch_ascent);
18713 it->position = saved_pos;
18714 it->avoid_cursor_p = saved_avoid_cursor;
18715 it->face_id = saved_face_id;
18716 it->start_of_box_run_p = saved_box_start;
18717 }
18718 }
18719 #endif /* HAVE_WINDOW_SYSTEM */
18720 }
18721 else
18722 {
18723 /* Save some values that must not be changed. */
18724 int saved_x = it->current_x;
18725 struct text_pos saved_pos;
18726 Lisp_Object saved_object;
18727 enum display_element_type saved_what = it->what;
18728 int saved_face_id = it->face_id;
18729
18730 saved_object = it->object;
18731 saved_pos = it->position;
18732
18733 it->what = IT_CHARACTER;
18734 memset (&it->position, 0, sizeof it->position);
18735 it->object = make_number (0);
18736 it->c = it->char_to_display = ' ';
18737 it->len = 1;
18738 /* The last row's blank glyphs should get the default face, to
18739 avoid painting the rest of the window with the region face,
18740 if the region ends at ZV. */
18741 if (it->glyph_row->ends_at_zv_p)
18742 it->face_id = default_face->id;
18743 else
18744 it->face_id = face->id;
18745
18746 PRODUCE_GLYPHS (it);
18747
18748 while (it->current_x <= it->last_visible_x)
18749 PRODUCE_GLYPHS (it);
18750
18751 /* Don't count these blanks really. It would let us insert a left
18752 truncation glyph below and make us set the cursor on them, maybe. */
18753 it->current_x = saved_x;
18754 it->object = saved_object;
18755 it->position = saved_pos;
18756 it->what = saved_what;
18757 it->face_id = saved_face_id;
18758 }
18759 }
18760
18761
18762 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18763 trailing whitespace. */
18764
18765 static int
18766 trailing_whitespace_p (ptrdiff_t charpos)
18767 {
18768 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18769 int c = 0;
18770
18771 while (bytepos < ZV_BYTE
18772 && (c = FETCH_CHAR (bytepos),
18773 c == ' ' || c == '\t'))
18774 ++bytepos;
18775
18776 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18777 {
18778 if (bytepos != PT_BYTE)
18779 return 1;
18780 }
18781 return 0;
18782 }
18783
18784
18785 /* Highlight trailing whitespace, if any, in ROW. */
18786
18787 static void
18788 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18789 {
18790 int used = row->used[TEXT_AREA];
18791
18792 if (used)
18793 {
18794 struct glyph *start = row->glyphs[TEXT_AREA];
18795 struct glyph *glyph = start + used - 1;
18796
18797 if (row->reversed_p)
18798 {
18799 /* Right-to-left rows need to be processed in the opposite
18800 direction, so swap the edge pointers. */
18801 glyph = start;
18802 start = row->glyphs[TEXT_AREA] + used - 1;
18803 }
18804
18805 /* Skip over glyphs inserted to display the cursor at the
18806 end of a line, for extending the face of the last glyph
18807 to the end of the line on terminals, and for truncation
18808 and continuation glyphs. */
18809 if (!row->reversed_p)
18810 {
18811 while (glyph >= start
18812 && glyph->type == CHAR_GLYPH
18813 && INTEGERP (glyph->object))
18814 --glyph;
18815 }
18816 else
18817 {
18818 while (glyph <= start
18819 && glyph->type == CHAR_GLYPH
18820 && INTEGERP (glyph->object))
18821 ++glyph;
18822 }
18823
18824 /* If last glyph is a space or stretch, and it's trailing
18825 whitespace, set the face of all trailing whitespace glyphs in
18826 IT->glyph_row to `trailing-whitespace'. */
18827 if ((row->reversed_p ? glyph <= start : glyph >= start)
18828 && BUFFERP (glyph->object)
18829 && (glyph->type == STRETCH_GLYPH
18830 || (glyph->type == CHAR_GLYPH
18831 && glyph->u.ch == ' '))
18832 && trailing_whitespace_p (glyph->charpos))
18833 {
18834 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18835 if (face_id < 0)
18836 return;
18837
18838 if (!row->reversed_p)
18839 {
18840 while (glyph >= start
18841 && BUFFERP (glyph->object)
18842 && (glyph->type == STRETCH_GLYPH
18843 || (glyph->type == CHAR_GLYPH
18844 && glyph->u.ch == ' ')))
18845 (glyph--)->face_id = face_id;
18846 }
18847 else
18848 {
18849 while (glyph <= start
18850 && BUFFERP (glyph->object)
18851 && (glyph->type == STRETCH_GLYPH
18852 || (glyph->type == CHAR_GLYPH
18853 && glyph->u.ch == ' ')))
18854 (glyph++)->face_id = face_id;
18855 }
18856 }
18857 }
18858 }
18859
18860
18861 /* Value is non-zero if glyph row ROW should be
18862 considered to hold the buffer position CHARPOS. */
18863
18864 static int
18865 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18866 {
18867 int result = 1;
18868
18869 if (charpos == CHARPOS (row->end.pos)
18870 || charpos == MATRIX_ROW_END_CHARPOS (row))
18871 {
18872 /* Suppose the row ends on a string.
18873 Unless the row is continued, that means it ends on a newline
18874 in the string. If it's anything other than a display string
18875 (e.g., a before-string from an overlay), we don't want the
18876 cursor there. (This heuristic seems to give the optimal
18877 behavior for the various types of multi-line strings.)
18878 One exception: if the string has `cursor' property on one of
18879 its characters, we _do_ want the cursor there. */
18880 if (CHARPOS (row->end.string_pos) >= 0)
18881 {
18882 if (row->continued_p)
18883 result = 1;
18884 else
18885 {
18886 /* Check for `display' property. */
18887 struct glyph *beg = row->glyphs[TEXT_AREA];
18888 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18889 struct glyph *glyph;
18890
18891 result = 0;
18892 for (glyph = end; glyph >= beg; --glyph)
18893 if (STRINGP (glyph->object))
18894 {
18895 Lisp_Object prop
18896 = Fget_char_property (make_number (charpos),
18897 Qdisplay, Qnil);
18898 result =
18899 (!NILP (prop)
18900 && display_prop_string_p (prop, glyph->object));
18901 /* If there's a `cursor' property on one of the
18902 string's characters, this row is a cursor row,
18903 even though this is not a display string. */
18904 if (!result)
18905 {
18906 Lisp_Object s = glyph->object;
18907
18908 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18909 {
18910 ptrdiff_t gpos = glyph->charpos;
18911
18912 if (!NILP (Fget_char_property (make_number (gpos),
18913 Qcursor, s)))
18914 {
18915 result = 1;
18916 break;
18917 }
18918 }
18919 }
18920 break;
18921 }
18922 }
18923 }
18924 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18925 {
18926 /* If the row ends in middle of a real character,
18927 and the line is continued, we want the cursor here.
18928 That's because CHARPOS (ROW->end.pos) would equal
18929 PT if PT is before the character. */
18930 if (!row->ends_in_ellipsis_p)
18931 result = row->continued_p;
18932 else
18933 /* If the row ends in an ellipsis, then
18934 CHARPOS (ROW->end.pos) will equal point after the
18935 invisible text. We want that position to be displayed
18936 after the ellipsis. */
18937 result = 0;
18938 }
18939 /* If the row ends at ZV, display the cursor at the end of that
18940 row instead of at the start of the row below. */
18941 else if (row->ends_at_zv_p)
18942 result = 1;
18943 else
18944 result = 0;
18945 }
18946
18947 return result;
18948 }
18949
18950 /* Value is non-zero if glyph row ROW should be
18951 used to hold the cursor. */
18952
18953 static int
18954 cursor_row_p (struct glyph_row *row)
18955 {
18956 return row_for_charpos_p (row, PT);
18957 }
18958
18959 \f
18960
18961 /* Push the property PROP so that it will be rendered at the current
18962 position in IT. Return 1 if PROP was successfully pushed, 0
18963 otherwise. Called from handle_line_prefix to handle the
18964 `line-prefix' and `wrap-prefix' properties. */
18965
18966 static int
18967 push_prefix_prop (struct it *it, Lisp_Object prop)
18968 {
18969 struct text_pos pos =
18970 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18971
18972 eassert (it->method == GET_FROM_BUFFER
18973 || it->method == GET_FROM_DISPLAY_VECTOR
18974 || it->method == GET_FROM_STRING);
18975
18976 /* We need to save the current buffer/string position, so it will be
18977 restored by pop_it, because iterate_out_of_display_property
18978 depends on that being set correctly, but some situations leave
18979 it->position not yet set when this function is called. */
18980 push_it (it, &pos);
18981
18982 if (STRINGP (prop))
18983 {
18984 if (SCHARS (prop) == 0)
18985 {
18986 pop_it (it);
18987 return 0;
18988 }
18989
18990 it->string = prop;
18991 it->string_from_prefix_prop_p = 1;
18992 it->multibyte_p = STRING_MULTIBYTE (it->string);
18993 it->current.overlay_string_index = -1;
18994 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18995 it->end_charpos = it->string_nchars = SCHARS (it->string);
18996 it->method = GET_FROM_STRING;
18997 it->stop_charpos = 0;
18998 it->prev_stop = 0;
18999 it->base_level_stop = 0;
19000
19001 /* Force paragraph direction to be that of the parent
19002 buffer/string. */
19003 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19004 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19005 else
19006 it->paragraph_embedding = L2R;
19007
19008 /* Set up the bidi iterator for this display string. */
19009 if (it->bidi_p)
19010 {
19011 it->bidi_it.string.lstring = it->string;
19012 it->bidi_it.string.s = NULL;
19013 it->bidi_it.string.schars = it->end_charpos;
19014 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19015 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19016 it->bidi_it.string.unibyte = !it->multibyte_p;
19017 it->bidi_it.w = it->w;
19018 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19019 }
19020 }
19021 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19022 {
19023 it->method = GET_FROM_STRETCH;
19024 it->object = prop;
19025 }
19026 #ifdef HAVE_WINDOW_SYSTEM
19027 else if (IMAGEP (prop))
19028 {
19029 it->what = IT_IMAGE;
19030 it->image_id = lookup_image (it->f, prop);
19031 it->method = GET_FROM_IMAGE;
19032 }
19033 #endif /* HAVE_WINDOW_SYSTEM */
19034 else
19035 {
19036 pop_it (it); /* bogus display property, give up */
19037 return 0;
19038 }
19039
19040 return 1;
19041 }
19042
19043 /* Return the character-property PROP at the current position in IT. */
19044
19045 static Lisp_Object
19046 get_it_property (struct it *it, Lisp_Object prop)
19047 {
19048 Lisp_Object position, object = it->object;
19049
19050 if (STRINGP (object))
19051 position = make_number (IT_STRING_CHARPOS (*it));
19052 else if (BUFFERP (object))
19053 {
19054 position = make_number (IT_CHARPOS (*it));
19055 object = it->window;
19056 }
19057 else
19058 return Qnil;
19059
19060 return Fget_char_property (position, prop, object);
19061 }
19062
19063 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19064
19065 static void
19066 handle_line_prefix (struct it *it)
19067 {
19068 Lisp_Object prefix;
19069
19070 if (it->continuation_lines_width > 0)
19071 {
19072 prefix = get_it_property (it, Qwrap_prefix);
19073 if (NILP (prefix))
19074 prefix = Vwrap_prefix;
19075 }
19076 else
19077 {
19078 prefix = get_it_property (it, Qline_prefix);
19079 if (NILP (prefix))
19080 prefix = Vline_prefix;
19081 }
19082 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19083 {
19084 /* If the prefix is wider than the window, and we try to wrap
19085 it, it would acquire its own wrap prefix, and so on till the
19086 iterator stack overflows. So, don't wrap the prefix. */
19087 it->line_wrap = TRUNCATE;
19088 it->avoid_cursor_p = 1;
19089 }
19090 }
19091
19092 \f
19093
19094 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19095 only for R2L lines from display_line and display_string, when they
19096 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19097 the line/string needs to be continued on the next glyph row. */
19098 static void
19099 unproduce_glyphs (struct it *it, int n)
19100 {
19101 struct glyph *glyph, *end;
19102
19103 eassert (it->glyph_row);
19104 eassert (it->glyph_row->reversed_p);
19105 eassert (it->area == TEXT_AREA);
19106 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19107
19108 if (n > it->glyph_row->used[TEXT_AREA])
19109 n = it->glyph_row->used[TEXT_AREA];
19110 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19111 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19112 for ( ; glyph < end; glyph++)
19113 glyph[-n] = *glyph;
19114 }
19115
19116 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19117 and ROW->maxpos. */
19118 static void
19119 find_row_edges (struct it *it, struct glyph_row *row,
19120 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19121 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19122 {
19123 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19124 lines' rows is implemented for bidi-reordered rows. */
19125
19126 /* ROW->minpos is the value of min_pos, the minimal buffer position
19127 we have in ROW, or ROW->start.pos if that is smaller. */
19128 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19129 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19130 else
19131 /* We didn't find buffer positions smaller than ROW->start, or
19132 didn't find _any_ valid buffer positions in any of the glyphs,
19133 so we must trust the iterator's computed positions. */
19134 row->minpos = row->start.pos;
19135 if (max_pos <= 0)
19136 {
19137 max_pos = CHARPOS (it->current.pos);
19138 max_bpos = BYTEPOS (it->current.pos);
19139 }
19140
19141 /* Here are the various use-cases for ending the row, and the
19142 corresponding values for ROW->maxpos:
19143
19144 Line ends in a newline from buffer eol_pos + 1
19145 Line is continued from buffer max_pos + 1
19146 Line is truncated on right it->current.pos
19147 Line ends in a newline from string max_pos + 1(*)
19148 (*) + 1 only when line ends in a forward scan
19149 Line is continued from string max_pos
19150 Line is continued from display vector max_pos
19151 Line is entirely from a string min_pos == max_pos
19152 Line is entirely from a display vector min_pos == max_pos
19153 Line that ends at ZV ZV
19154
19155 If you discover other use-cases, please add them here as
19156 appropriate. */
19157 if (row->ends_at_zv_p)
19158 row->maxpos = it->current.pos;
19159 else if (row->used[TEXT_AREA])
19160 {
19161 int seen_this_string = 0;
19162 struct glyph_row *r1 = row - 1;
19163
19164 /* Did we see the same display string on the previous row? */
19165 if (STRINGP (it->object)
19166 /* this is not the first row */
19167 && row > it->w->desired_matrix->rows
19168 /* previous row is not the header line */
19169 && !r1->mode_line_p
19170 /* previous row also ends in a newline from a string */
19171 && r1->ends_in_newline_from_string_p)
19172 {
19173 struct glyph *start, *end;
19174
19175 /* Search for the last glyph of the previous row that came
19176 from buffer or string. Depending on whether the row is
19177 L2R or R2L, we need to process it front to back or the
19178 other way round. */
19179 if (!r1->reversed_p)
19180 {
19181 start = r1->glyphs[TEXT_AREA];
19182 end = start + r1->used[TEXT_AREA];
19183 /* Glyphs inserted by redisplay have an integer (zero)
19184 as their object. */
19185 while (end > start
19186 && INTEGERP ((end - 1)->object)
19187 && (end - 1)->charpos <= 0)
19188 --end;
19189 if (end > start)
19190 {
19191 if (EQ ((end - 1)->object, it->object))
19192 seen_this_string = 1;
19193 }
19194 else
19195 /* If all the glyphs of the previous row were inserted
19196 by redisplay, it means the previous row was
19197 produced from a single newline, which is only
19198 possible if that newline came from the same string
19199 as the one which produced this ROW. */
19200 seen_this_string = 1;
19201 }
19202 else
19203 {
19204 end = r1->glyphs[TEXT_AREA] - 1;
19205 start = end + r1->used[TEXT_AREA];
19206 while (end < start
19207 && INTEGERP ((end + 1)->object)
19208 && (end + 1)->charpos <= 0)
19209 ++end;
19210 if (end < start)
19211 {
19212 if (EQ ((end + 1)->object, it->object))
19213 seen_this_string = 1;
19214 }
19215 else
19216 seen_this_string = 1;
19217 }
19218 }
19219 /* Take note of each display string that covers a newline only
19220 once, the first time we see it. This is for when a display
19221 string includes more than one newline in it. */
19222 if (row->ends_in_newline_from_string_p && !seen_this_string)
19223 {
19224 /* If we were scanning the buffer forward when we displayed
19225 the string, we want to account for at least one buffer
19226 position that belongs to this row (position covered by
19227 the display string), so that cursor positioning will
19228 consider this row as a candidate when point is at the end
19229 of the visual line represented by this row. This is not
19230 required when scanning back, because max_pos will already
19231 have a much larger value. */
19232 if (CHARPOS (row->end.pos) > max_pos)
19233 INC_BOTH (max_pos, max_bpos);
19234 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19235 }
19236 else if (CHARPOS (it->eol_pos) > 0)
19237 SET_TEXT_POS (row->maxpos,
19238 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19239 else if (row->continued_p)
19240 {
19241 /* If max_pos is different from IT's current position, it
19242 means IT->method does not belong to the display element
19243 at max_pos. However, it also means that the display
19244 element at max_pos was displayed in its entirety on this
19245 line, which is equivalent to saying that the next line
19246 starts at the next buffer position. */
19247 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19248 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19249 else
19250 {
19251 INC_BOTH (max_pos, max_bpos);
19252 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19253 }
19254 }
19255 else if (row->truncated_on_right_p)
19256 /* display_line already called reseat_at_next_visible_line_start,
19257 which puts the iterator at the beginning of the next line, in
19258 the logical order. */
19259 row->maxpos = it->current.pos;
19260 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19261 /* A line that is entirely from a string/image/stretch... */
19262 row->maxpos = row->minpos;
19263 else
19264 emacs_abort ();
19265 }
19266 else
19267 row->maxpos = it->current.pos;
19268 }
19269
19270 /* Construct the glyph row IT->glyph_row in the desired matrix of
19271 IT->w from text at the current position of IT. See dispextern.h
19272 for an overview of struct it. Value is non-zero if
19273 IT->glyph_row displays text, as opposed to a line displaying ZV
19274 only. */
19275
19276 static int
19277 display_line (struct it *it)
19278 {
19279 struct glyph_row *row = it->glyph_row;
19280 Lisp_Object overlay_arrow_string;
19281 struct it wrap_it;
19282 void *wrap_data = NULL;
19283 int may_wrap = 0, wrap_x IF_LINT (= 0);
19284 int wrap_row_used = -1;
19285 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19286 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19287 int wrap_row_extra_line_spacing IF_LINT (= 0);
19288 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19289 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19290 int cvpos;
19291 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19292 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19293
19294 /* We always start displaying at hpos zero even if hscrolled. */
19295 eassert (it->hpos == 0 && it->current_x == 0);
19296
19297 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19298 >= it->w->desired_matrix->nrows)
19299 {
19300 it->w->nrows_scale_factor++;
19301 it->f->fonts_changed = 1;
19302 return 0;
19303 }
19304
19305 /* Clear the result glyph row and enable it. */
19306 prepare_desired_row (row);
19307
19308 row->y = it->current_y;
19309 row->start = it->start;
19310 row->continuation_lines_width = it->continuation_lines_width;
19311 row->displays_text_p = 1;
19312 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19313 it->starts_in_middle_of_char_p = 0;
19314
19315 /* Arrange the overlays nicely for our purposes. Usually, we call
19316 display_line on only one line at a time, in which case this
19317 can't really hurt too much, or we call it on lines which appear
19318 one after another in the buffer, in which case all calls to
19319 recenter_overlay_lists but the first will be pretty cheap. */
19320 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19321
19322 /* Move over display elements that are not visible because we are
19323 hscrolled. This may stop at an x-position < IT->first_visible_x
19324 if the first glyph is partially visible or if we hit a line end. */
19325 if (it->current_x < it->first_visible_x)
19326 {
19327 enum move_it_result move_result;
19328
19329 this_line_min_pos = row->start.pos;
19330 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19331 MOVE_TO_POS | MOVE_TO_X);
19332 /* If we are under a large hscroll, move_it_in_display_line_to
19333 could hit the end of the line without reaching
19334 it->first_visible_x. Pretend that we did reach it. This is
19335 especially important on a TTY, where we will call
19336 extend_face_to_end_of_line, which needs to know how many
19337 blank glyphs to produce. */
19338 if (it->current_x < it->first_visible_x
19339 && (move_result == MOVE_NEWLINE_OR_CR
19340 || move_result == MOVE_POS_MATCH_OR_ZV))
19341 it->current_x = it->first_visible_x;
19342
19343 /* Record the smallest positions seen while we moved over
19344 display elements that are not visible. This is needed by
19345 redisplay_internal for optimizing the case where the cursor
19346 stays inside the same line. The rest of this function only
19347 considers positions that are actually displayed, so
19348 RECORD_MAX_MIN_POS will not otherwise record positions that
19349 are hscrolled to the left of the left edge of the window. */
19350 min_pos = CHARPOS (this_line_min_pos);
19351 min_bpos = BYTEPOS (this_line_min_pos);
19352 }
19353 else
19354 {
19355 /* We only do this when not calling `move_it_in_display_line_to'
19356 above, because move_it_in_display_line_to calls
19357 handle_line_prefix itself. */
19358 handle_line_prefix (it);
19359 }
19360
19361 /* Get the initial row height. This is either the height of the
19362 text hscrolled, if there is any, or zero. */
19363 row->ascent = it->max_ascent;
19364 row->height = it->max_ascent + it->max_descent;
19365 row->phys_ascent = it->max_phys_ascent;
19366 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19367 row->extra_line_spacing = it->max_extra_line_spacing;
19368
19369 /* Utility macro to record max and min buffer positions seen until now. */
19370 #define RECORD_MAX_MIN_POS(IT) \
19371 do \
19372 { \
19373 int composition_p = !STRINGP ((IT)->string) \
19374 && ((IT)->what == IT_COMPOSITION); \
19375 ptrdiff_t current_pos = \
19376 composition_p ? (IT)->cmp_it.charpos \
19377 : IT_CHARPOS (*(IT)); \
19378 ptrdiff_t current_bpos = \
19379 composition_p ? CHAR_TO_BYTE (current_pos) \
19380 : IT_BYTEPOS (*(IT)); \
19381 if (current_pos < min_pos) \
19382 { \
19383 min_pos = current_pos; \
19384 min_bpos = current_bpos; \
19385 } \
19386 if (IT_CHARPOS (*it) > max_pos) \
19387 { \
19388 max_pos = IT_CHARPOS (*it); \
19389 max_bpos = IT_BYTEPOS (*it); \
19390 } \
19391 } \
19392 while (0)
19393
19394 /* Loop generating characters. The loop is left with IT on the next
19395 character to display. */
19396 while (1)
19397 {
19398 int n_glyphs_before, hpos_before, x_before;
19399 int x, nglyphs;
19400 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19401
19402 /* Retrieve the next thing to display. Value is zero if end of
19403 buffer reached. */
19404 if (!get_next_display_element (it))
19405 {
19406 /* Maybe add a space at the end of this line that is used to
19407 display the cursor there under X. Set the charpos of the
19408 first glyph of blank lines not corresponding to any text
19409 to -1. */
19410 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19411 row->exact_window_width_line_p = 1;
19412 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19413 || row->used[TEXT_AREA] == 0)
19414 {
19415 row->glyphs[TEXT_AREA]->charpos = -1;
19416 row->displays_text_p = 0;
19417
19418 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19419 && (!MINI_WINDOW_P (it->w)
19420 || (minibuf_level && EQ (it->window, minibuf_window))))
19421 row->indicate_empty_line_p = 1;
19422 }
19423
19424 it->continuation_lines_width = 0;
19425 row->ends_at_zv_p = 1;
19426 /* A row that displays right-to-left text must always have
19427 its last face extended all the way to the end of line,
19428 even if this row ends in ZV, because we still write to
19429 the screen left to right. We also need to extend the
19430 last face if the default face is remapped to some
19431 different face, otherwise the functions that clear
19432 portions of the screen will clear with the default face's
19433 background color. */
19434 if (row->reversed_p
19435 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19436 extend_face_to_end_of_line (it);
19437 break;
19438 }
19439
19440 /* Now, get the metrics of what we want to display. This also
19441 generates glyphs in `row' (which is IT->glyph_row). */
19442 n_glyphs_before = row->used[TEXT_AREA];
19443 x = it->current_x;
19444
19445 /* Remember the line height so far in case the next element doesn't
19446 fit on the line. */
19447 if (it->line_wrap != TRUNCATE)
19448 {
19449 ascent = it->max_ascent;
19450 descent = it->max_descent;
19451 phys_ascent = it->max_phys_ascent;
19452 phys_descent = it->max_phys_descent;
19453
19454 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19455 {
19456 if (IT_DISPLAYING_WHITESPACE (it))
19457 may_wrap = 1;
19458 else if (may_wrap)
19459 {
19460 SAVE_IT (wrap_it, *it, wrap_data);
19461 wrap_x = x;
19462 wrap_row_used = row->used[TEXT_AREA];
19463 wrap_row_ascent = row->ascent;
19464 wrap_row_height = row->height;
19465 wrap_row_phys_ascent = row->phys_ascent;
19466 wrap_row_phys_height = row->phys_height;
19467 wrap_row_extra_line_spacing = row->extra_line_spacing;
19468 wrap_row_min_pos = min_pos;
19469 wrap_row_min_bpos = min_bpos;
19470 wrap_row_max_pos = max_pos;
19471 wrap_row_max_bpos = max_bpos;
19472 may_wrap = 0;
19473 }
19474 }
19475 }
19476
19477 PRODUCE_GLYPHS (it);
19478
19479 /* If this display element was in marginal areas, continue with
19480 the next one. */
19481 if (it->area != TEXT_AREA)
19482 {
19483 row->ascent = max (row->ascent, it->max_ascent);
19484 row->height = max (row->height, it->max_ascent + it->max_descent);
19485 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19486 row->phys_height = max (row->phys_height,
19487 it->max_phys_ascent + it->max_phys_descent);
19488 row->extra_line_spacing = max (row->extra_line_spacing,
19489 it->max_extra_line_spacing);
19490 set_iterator_to_next (it, 1);
19491 continue;
19492 }
19493
19494 /* Does the display element fit on the line? If we truncate
19495 lines, we should draw past the right edge of the window. If
19496 we don't truncate, we want to stop so that we can display the
19497 continuation glyph before the right margin. If lines are
19498 continued, there are two possible strategies for characters
19499 resulting in more than 1 glyph (e.g. tabs): Display as many
19500 glyphs as possible in this line and leave the rest for the
19501 continuation line, or display the whole element in the next
19502 line. Original redisplay did the former, so we do it also. */
19503 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19504 hpos_before = it->hpos;
19505 x_before = x;
19506
19507 if (/* Not a newline. */
19508 nglyphs > 0
19509 /* Glyphs produced fit entirely in the line. */
19510 && it->current_x < it->last_visible_x)
19511 {
19512 it->hpos += nglyphs;
19513 row->ascent = max (row->ascent, it->max_ascent);
19514 row->height = max (row->height, it->max_ascent + it->max_descent);
19515 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19516 row->phys_height = max (row->phys_height,
19517 it->max_phys_ascent + it->max_phys_descent);
19518 row->extra_line_spacing = max (row->extra_line_spacing,
19519 it->max_extra_line_spacing);
19520 if (it->current_x - it->pixel_width < it->first_visible_x)
19521 row->x = x - it->first_visible_x;
19522 /* Record the maximum and minimum buffer positions seen so
19523 far in glyphs that will be displayed by this row. */
19524 if (it->bidi_p)
19525 RECORD_MAX_MIN_POS (it);
19526 }
19527 else
19528 {
19529 int i, new_x;
19530 struct glyph *glyph;
19531
19532 for (i = 0; i < nglyphs; ++i, x = new_x)
19533 {
19534 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19535 new_x = x + glyph->pixel_width;
19536
19537 if (/* Lines are continued. */
19538 it->line_wrap != TRUNCATE
19539 && (/* Glyph doesn't fit on the line. */
19540 new_x > it->last_visible_x
19541 /* Or it fits exactly on a window system frame. */
19542 || (new_x == it->last_visible_x
19543 && FRAME_WINDOW_P (it->f)
19544 && (row->reversed_p
19545 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19546 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19547 {
19548 /* End of a continued line. */
19549
19550 if (it->hpos == 0
19551 || (new_x == it->last_visible_x
19552 && FRAME_WINDOW_P (it->f)
19553 && (row->reversed_p
19554 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19555 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19556 {
19557 /* Current glyph is the only one on the line or
19558 fits exactly on the line. We must continue
19559 the line because we can't draw the cursor
19560 after the glyph. */
19561 row->continued_p = 1;
19562 it->current_x = new_x;
19563 it->continuation_lines_width += new_x;
19564 ++it->hpos;
19565 if (i == nglyphs - 1)
19566 {
19567 /* If line-wrap is on, check if a previous
19568 wrap point was found. */
19569 if (wrap_row_used > 0
19570 /* Even if there is a previous wrap
19571 point, continue the line here as
19572 usual, if (i) the previous character
19573 was a space or tab AND (ii) the
19574 current character is not. */
19575 && (!may_wrap
19576 || IT_DISPLAYING_WHITESPACE (it)))
19577 goto back_to_wrap;
19578
19579 /* Record the maximum and minimum buffer
19580 positions seen so far in glyphs that will be
19581 displayed by this row. */
19582 if (it->bidi_p)
19583 RECORD_MAX_MIN_POS (it);
19584 set_iterator_to_next (it, 1);
19585 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19586 {
19587 if (!get_next_display_element (it))
19588 {
19589 row->exact_window_width_line_p = 1;
19590 it->continuation_lines_width = 0;
19591 row->continued_p = 0;
19592 row->ends_at_zv_p = 1;
19593 }
19594 else if (ITERATOR_AT_END_OF_LINE_P (it))
19595 {
19596 row->continued_p = 0;
19597 row->exact_window_width_line_p = 1;
19598 }
19599 }
19600 }
19601 else if (it->bidi_p)
19602 RECORD_MAX_MIN_POS (it);
19603 }
19604 else if (CHAR_GLYPH_PADDING_P (*glyph)
19605 && !FRAME_WINDOW_P (it->f))
19606 {
19607 /* A padding glyph that doesn't fit on this line.
19608 This means the whole character doesn't fit
19609 on the line. */
19610 if (row->reversed_p)
19611 unproduce_glyphs (it, row->used[TEXT_AREA]
19612 - n_glyphs_before);
19613 row->used[TEXT_AREA] = n_glyphs_before;
19614
19615 /* Fill the rest of the row with continuation
19616 glyphs like in 20.x. */
19617 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19618 < row->glyphs[1 + TEXT_AREA])
19619 produce_special_glyphs (it, IT_CONTINUATION);
19620
19621 row->continued_p = 1;
19622 it->current_x = x_before;
19623 it->continuation_lines_width += x_before;
19624
19625 /* Restore the height to what it was before the
19626 element not fitting on the line. */
19627 it->max_ascent = ascent;
19628 it->max_descent = descent;
19629 it->max_phys_ascent = phys_ascent;
19630 it->max_phys_descent = phys_descent;
19631 }
19632 else if (wrap_row_used > 0)
19633 {
19634 back_to_wrap:
19635 if (row->reversed_p)
19636 unproduce_glyphs (it,
19637 row->used[TEXT_AREA] - wrap_row_used);
19638 RESTORE_IT (it, &wrap_it, wrap_data);
19639 it->continuation_lines_width += wrap_x;
19640 row->used[TEXT_AREA] = wrap_row_used;
19641 row->ascent = wrap_row_ascent;
19642 row->height = wrap_row_height;
19643 row->phys_ascent = wrap_row_phys_ascent;
19644 row->phys_height = wrap_row_phys_height;
19645 row->extra_line_spacing = wrap_row_extra_line_spacing;
19646 min_pos = wrap_row_min_pos;
19647 min_bpos = wrap_row_min_bpos;
19648 max_pos = wrap_row_max_pos;
19649 max_bpos = wrap_row_max_bpos;
19650 row->continued_p = 1;
19651 row->ends_at_zv_p = 0;
19652 row->exact_window_width_line_p = 0;
19653 it->continuation_lines_width += x;
19654
19655 /* Make sure that a non-default face is extended
19656 up to the right margin of the window. */
19657 extend_face_to_end_of_line (it);
19658 }
19659 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19660 {
19661 /* A TAB that extends past the right edge of the
19662 window. This produces a single glyph on
19663 window system frames. We leave the glyph in
19664 this row and let it fill the row, but don't
19665 consume the TAB. */
19666 if ((row->reversed_p
19667 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19668 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19669 produce_special_glyphs (it, IT_CONTINUATION);
19670 it->continuation_lines_width += it->last_visible_x;
19671 row->ends_in_middle_of_char_p = 1;
19672 row->continued_p = 1;
19673 glyph->pixel_width = it->last_visible_x - x;
19674 it->starts_in_middle_of_char_p = 1;
19675 }
19676 else
19677 {
19678 /* Something other than a TAB that draws past
19679 the right edge of the window. Restore
19680 positions to values before the element. */
19681 if (row->reversed_p)
19682 unproduce_glyphs (it, row->used[TEXT_AREA]
19683 - (n_glyphs_before + i));
19684 row->used[TEXT_AREA] = n_glyphs_before + i;
19685
19686 /* Display continuation glyphs. */
19687 it->current_x = x_before;
19688 it->continuation_lines_width += x;
19689 if (!FRAME_WINDOW_P (it->f)
19690 || (row->reversed_p
19691 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19692 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19693 produce_special_glyphs (it, IT_CONTINUATION);
19694 row->continued_p = 1;
19695
19696 extend_face_to_end_of_line (it);
19697
19698 if (nglyphs > 1 && i > 0)
19699 {
19700 row->ends_in_middle_of_char_p = 1;
19701 it->starts_in_middle_of_char_p = 1;
19702 }
19703
19704 /* Restore the height to what it was before the
19705 element not fitting on the line. */
19706 it->max_ascent = ascent;
19707 it->max_descent = descent;
19708 it->max_phys_ascent = phys_ascent;
19709 it->max_phys_descent = phys_descent;
19710 }
19711
19712 break;
19713 }
19714 else if (new_x > it->first_visible_x)
19715 {
19716 /* Increment number of glyphs actually displayed. */
19717 ++it->hpos;
19718
19719 /* Record the maximum and minimum buffer positions
19720 seen so far in glyphs that will be displayed by
19721 this row. */
19722 if (it->bidi_p)
19723 RECORD_MAX_MIN_POS (it);
19724
19725 if (x < it->first_visible_x)
19726 /* Glyph is partially visible, i.e. row starts at
19727 negative X position. */
19728 row->x = x - it->first_visible_x;
19729 }
19730 else
19731 {
19732 /* Glyph is completely off the left margin of the
19733 window. This should not happen because of the
19734 move_it_in_display_line at the start of this
19735 function, unless the text display area of the
19736 window is empty. */
19737 eassert (it->first_visible_x <= it->last_visible_x);
19738 }
19739 }
19740 /* Even if this display element produced no glyphs at all,
19741 we want to record its position. */
19742 if (it->bidi_p && nglyphs == 0)
19743 RECORD_MAX_MIN_POS (it);
19744
19745 row->ascent = max (row->ascent, it->max_ascent);
19746 row->height = max (row->height, it->max_ascent + it->max_descent);
19747 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19748 row->phys_height = max (row->phys_height,
19749 it->max_phys_ascent + it->max_phys_descent);
19750 row->extra_line_spacing = max (row->extra_line_spacing,
19751 it->max_extra_line_spacing);
19752
19753 /* End of this display line if row is continued. */
19754 if (row->continued_p || row->ends_at_zv_p)
19755 break;
19756 }
19757
19758 at_end_of_line:
19759 /* Is this a line end? If yes, we're also done, after making
19760 sure that a non-default face is extended up to the right
19761 margin of the window. */
19762 if (ITERATOR_AT_END_OF_LINE_P (it))
19763 {
19764 int used_before = row->used[TEXT_AREA];
19765
19766 row->ends_in_newline_from_string_p = STRINGP (it->object);
19767
19768 /* Add a space at the end of the line that is used to
19769 display the cursor there. */
19770 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19771 append_space_for_newline (it, 0);
19772
19773 /* Extend the face to the end of the line. */
19774 extend_face_to_end_of_line (it);
19775
19776 /* Make sure we have the position. */
19777 if (used_before == 0)
19778 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19779
19780 /* Record the position of the newline, for use in
19781 find_row_edges. */
19782 it->eol_pos = it->current.pos;
19783
19784 /* Consume the line end. This skips over invisible lines. */
19785 set_iterator_to_next (it, 1);
19786 it->continuation_lines_width = 0;
19787 break;
19788 }
19789
19790 /* Proceed with next display element. Note that this skips
19791 over lines invisible because of selective display. */
19792 set_iterator_to_next (it, 1);
19793
19794 /* If we truncate lines, we are done when the last displayed
19795 glyphs reach past the right margin of the window. */
19796 if (it->line_wrap == TRUNCATE
19797 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19798 ? (it->current_x >= it->last_visible_x)
19799 : (it->current_x > it->last_visible_x)))
19800 {
19801 /* Maybe add truncation glyphs. */
19802 if (!FRAME_WINDOW_P (it->f)
19803 || (row->reversed_p
19804 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19805 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19806 {
19807 int i, n;
19808
19809 if (!row->reversed_p)
19810 {
19811 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19812 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19813 break;
19814 }
19815 else
19816 {
19817 for (i = 0; i < row->used[TEXT_AREA]; i++)
19818 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19819 break;
19820 /* Remove any padding glyphs at the front of ROW, to
19821 make room for the truncation glyphs we will be
19822 adding below. The loop below always inserts at
19823 least one truncation glyph, so also remove the
19824 last glyph added to ROW. */
19825 unproduce_glyphs (it, i + 1);
19826 /* Adjust i for the loop below. */
19827 i = row->used[TEXT_AREA] - (i + 1);
19828 }
19829
19830 it->current_x = x_before;
19831 if (!FRAME_WINDOW_P (it->f))
19832 {
19833 for (n = row->used[TEXT_AREA]; i < n; ++i)
19834 {
19835 row->used[TEXT_AREA] = i;
19836 produce_special_glyphs (it, IT_TRUNCATION);
19837 }
19838 }
19839 else
19840 {
19841 row->used[TEXT_AREA] = i;
19842 produce_special_glyphs (it, IT_TRUNCATION);
19843 }
19844 }
19845 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19846 {
19847 /* Don't truncate if we can overflow newline into fringe. */
19848 if (!get_next_display_element (it))
19849 {
19850 it->continuation_lines_width = 0;
19851 row->ends_at_zv_p = 1;
19852 row->exact_window_width_line_p = 1;
19853 break;
19854 }
19855 if (ITERATOR_AT_END_OF_LINE_P (it))
19856 {
19857 row->exact_window_width_line_p = 1;
19858 goto at_end_of_line;
19859 }
19860 it->current_x = x_before;
19861 }
19862
19863 row->truncated_on_right_p = 1;
19864 it->continuation_lines_width = 0;
19865 reseat_at_next_visible_line_start (it, 0);
19866 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19867 it->hpos = hpos_before;
19868 break;
19869 }
19870 }
19871
19872 if (wrap_data)
19873 bidi_unshelve_cache (wrap_data, 1);
19874
19875 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19876 at the left window margin. */
19877 if (it->first_visible_x
19878 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19879 {
19880 if (!FRAME_WINDOW_P (it->f)
19881 || (row->reversed_p
19882 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19883 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19884 insert_left_trunc_glyphs (it);
19885 row->truncated_on_left_p = 1;
19886 }
19887
19888 /* Remember the position at which this line ends.
19889
19890 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19891 cannot be before the call to find_row_edges below, since that is
19892 where these positions are determined. */
19893 row->end = it->current;
19894 if (!it->bidi_p)
19895 {
19896 row->minpos = row->start.pos;
19897 row->maxpos = row->end.pos;
19898 }
19899 else
19900 {
19901 /* ROW->minpos and ROW->maxpos must be the smallest and
19902 `1 + the largest' buffer positions in ROW. But if ROW was
19903 bidi-reordered, these two positions can be anywhere in the
19904 row, so we must determine them now. */
19905 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19906 }
19907
19908 /* If the start of this line is the overlay arrow-position, then
19909 mark this glyph row as the one containing the overlay arrow.
19910 This is clearly a mess with variable size fonts. It would be
19911 better to let it be displayed like cursors under X. */
19912 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19913 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19914 !NILP (overlay_arrow_string)))
19915 {
19916 /* Overlay arrow in window redisplay is a fringe bitmap. */
19917 if (STRINGP (overlay_arrow_string))
19918 {
19919 struct glyph_row *arrow_row
19920 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19921 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19922 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19923 struct glyph *p = row->glyphs[TEXT_AREA];
19924 struct glyph *p2, *end;
19925
19926 /* Copy the arrow glyphs. */
19927 while (glyph < arrow_end)
19928 *p++ = *glyph++;
19929
19930 /* Throw away padding glyphs. */
19931 p2 = p;
19932 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19933 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19934 ++p2;
19935 if (p2 > p)
19936 {
19937 while (p2 < end)
19938 *p++ = *p2++;
19939 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19940 }
19941 }
19942 else
19943 {
19944 eassert (INTEGERP (overlay_arrow_string));
19945 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19946 }
19947 overlay_arrow_seen = 1;
19948 }
19949
19950 /* Highlight trailing whitespace. */
19951 if (!NILP (Vshow_trailing_whitespace))
19952 highlight_trailing_whitespace (it->f, it->glyph_row);
19953
19954 /* Compute pixel dimensions of this line. */
19955 compute_line_metrics (it);
19956
19957 /* Implementation note: No changes in the glyphs of ROW or in their
19958 faces can be done past this point, because compute_line_metrics
19959 computes ROW's hash value and stores it within the glyph_row
19960 structure. */
19961
19962 /* Record whether this row ends inside an ellipsis. */
19963 row->ends_in_ellipsis_p
19964 = (it->method == GET_FROM_DISPLAY_VECTOR
19965 && it->ellipsis_p);
19966
19967 /* Save fringe bitmaps in this row. */
19968 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19969 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19970 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19971 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19972
19973 it->left_user_fringe_bitmap = 0;
19974 it->left_user_fringe_face_id = 0;
19975 it->right_user_fringe_bitmap = 0;
19976 it->right_user_fringe_face_id = 0;
19977
19978 /* Maybe set the cursor. */
19979 cvpos = it->w->cursor.vpos;
19980 if ((cvpos < 0
19981 /* In bidi-reordered rows, keep checking for proper cursor
19982 position even if one has been found already, because buffer
19983 positions in such rows change non-linearly with ROW->VPOS,
19984 when a line is continued. One exception: when we are at ZV,
19985 display cursor on the first suitable glyph row, since all
19986 the empty rows after that also have their position set to ZV. */
19987 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19988 lines' rows is implemented for bidi-reordered rows. */
19989 || (it->bidi_p
19990 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19991 && PT >= MATRIX_ROW_START_CHARPOS (row)
19992 && PT <= MATRIX_ROW_END_CHARPOS (row)
19993 && cursor_row_p (row))
19994 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19995
19996 /* Prepare for the next line. This line starts horizontally at (X
19997 HPOS) = (0 0). Vertical positions are incremented. As a
19998 convenience for the caller, IT->glyph_row is set to the next
19999 row to be used. */
20000 it->current_x = it->hpos = 0;
20001 it->current_y += row->height;
20002 SET_TEXT_POS (it->eol_pos, 0, 0);
20003 ++it->vpos;
20004 ++it->glyph_row;
20005 /* The next row should by default use the same value of the
20006 reversed_p flag as this one. set_iterator_to_next decides when
20007 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20008 the flag accordingly. */
20009 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20010 it->glyph_row->reversed_p = row->reversed_p;
20011 it->start = row->end;
20012 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20013
20014 #undef RECORD_MAX_MIN_POS
20015 }
20016
20017 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20018 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20019 doc: /* Return paragraph direction at point in BUFFER.
20020 Value is either `left-to-right' or `right-to-left'.
20021 If BUFFER is omitted or nil, it defaults to the current buffer.
20022
20023 Paragraph direction determines how the text in the paragraph is displayed.
20024 In left-to-right paragraphs, text begins at the left margin of the window
20025 and the reading direction is generally left to right. In right-to-left
20026 paragraphs, text begins at the right margin and is read from right to left.
20027
20028 See also `bidi-paragraph-direction'. */)
20029 (Lisp_Object buffer)
20030 {
20031 struct buffer *buf = current_buffer;
20032 struct buffer *old = buf;
20033
20034 if (! NILP (buffer))
20035 {
20036 CHECK_BUFFER (buffer);
20037 buf = XBUFFER (buffer);
20038 }
20039
20040 if (NILP (BVAR (buf, bidi_display_reordering))
20041 || NILP (BVAR (buf, enable_multibyte_characters))
20042 /* When we are loading loadup.el, the character property tables
20043 needed for bidi iteration are not yet available. */
20044 || !NILP (Vpurify_flag))
20045 return Qleft_to_right;
20046 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20047 return BVAR (buf, bidi_paragraph_direction);
20048 else
20049 {
20050 /* Determine the direction from buffer text. We could try to
20051 use current_matrix if it is up to date, but this seems fast
20052 enough as it is. */
20053 struct bidi_it itb;
20054 ptrdiff_t pos = BUF_PT (buf);
20055 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20056 int c;
20057 void *itb_data = bidi_shelve_cache ();
20058
20059 set_buffer_temp (buf);
20060 /* bidi_paragraph_init finds the base direction of the paragraph
20061 by searching forward from paragraph start. We need the base
20062 direction of the current or _previous_ paragraph, so we need
20063 to make sure we are within that paragraph. To that end, find
20064 the previous non-empty line. */
20065 if (pos >= ZV && pos > BEGV)
20066 DEC_BOTH (pos, bytepos);
20067 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20068 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20069 {
20070 while ((c = FETCH_BYTE (bytepos)) == '\n'
20071 || c == ' ' || c == '\t' || c == '\f')
20072 {
20073 if (bytepos <= BEGV_BYTE)
20074 break;
20075 bytepos--;
20076 pos--;
20077 }
20078 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20079 bytepos--;
20080 }
20081 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20082 itb.paragraph_dir = NEUTRAL_DIR;
20083 itb.string.s = NULL;
20084 itb.string.lstring = Qnil;
20085 itb.string.bufpos = 0;
20086 itb.string.unibyte = 0;
20087 /* We have no window to use here for ignoring window-specific
20088 overlays. Using NULL for window pointer will cause
20089 compute_display_string_pos to use the current buffer. */
20090 itb.w = NULL;
20091 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20092 bidi_unshelve_cache (itb_data, 0);
20093 set_buffer_temp (old);
20094 switch (itb.paragraph_dir)
20095 {
20096 case L2R:
20097 return Qleft_to_right;
20098 break;
20099 case R2L:
20100 return Qright_to_left;
20101 break;
20102 default:
20103 emacs_abort ();
20104 }
20105 }
20106 }
20107
20108 DEFUN ("move-point-visually", Fmove_point_visually,
20109 Smove_point_visually, 1, 1, 0,
20110 doc: /* Move point in the visual order in the specified DIRECTION.
20111 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20112 left.
20113
20114 Value is the new character position of point. */)
20115 (Lisp_Object direction)
20116 {
20117 struct window *w = XWINDOW (selected_window);
20118 struct buffer *b = XBUFFER (w->contents);
20119 struct glyph_row *row;
20120 int dir;
20121 Lisp_Object paragraph_dir;
20122
20123 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20124 (!(ROW)->continued_p \
20125 && INTEGERP ((GLYPH)->object) \
20126 && (GLYPH)->type == CHAR_GLYPH \
20127 && (GLYPH)->u.ch == ' ' \
20128 && (GLYPH)->charpos >= 0 \
20129 && !(GLYPH)->avoid_cursor_p)
20130
20131 CHECK_NUMBER (direction);
20132 dir = XINT (direction);
20133 if (dir > 0)
20134 dir = 1;
20135 else
20136 dir = -1;
20137
20138 /* If current matrix is up-to-date, we can use the information
20139 recorded in the glyphs, at least as long as the goal is on the
20140 screen. */
20141 if (w->window_end_valid
20142 && !windows_or_buffers_changed
20143 && b
20144 && !b->clip_changed
20145 && !b->prevent_redisplay_optimizations_p
20146 && !window_outdated (w)
20147 && w->cursor.vpos >= 0
20148 && w->cursor.vpos < w->current_matrix->nrows
20149 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20150 {
20151 struct glyph *g = row->glyphs[TEXT_AREA];
20152 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20153 struct glyph *gpt = g + w->cursor.hpos;
20154
20155 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20156 {
20157 if (BUFFERP (g->object) && g->charpos != PT)
20158 {
20159 SET_PT (g->charpos);
20160 w->cursor.vpos = -1;
20161 return make_number (PT);
20162 }
20163 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20164 {
20165 ptrdiff_t new_pos;
20166
20167 if (BUFFERP (gpt->object))
20168 {
20169 new_pos = PT;
20170 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20171 new_pos += (row->reversed_p ? -dir : dir);
20172 else
20173 new_pos -= (row->reversed_p ? -dir : dir);;
20174 }
20175 else if (BUFFERP (g->object))
20176 new_pos = g->charpos;
20177 else
20178 break;
20179 SET_PT (new_pos);
20180 w->cursor.vpos = -1;
20181 return make_number (PT);
20182 }
20183 else if (ROW_GLYPH_NEWLINE_P (row, g))
20184 {
20185 /* Glyphs inserted at the end of a non-empty line for
20186 positioning the cursor have zero charpos, so we must
20187 deduce the value of point by other means. */
20188 if (g->charpos > 0)
20189 SET_PT (g->charpos);
20190 else if (row->ends_at_zv_p && PT != ZV)
20191 SET_PT (ZV);
20192 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20193 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20194 else
20195 break;
20196 w->cursor.vpos = -1;
20197 return make_number (PT);
20198 }
20199 }
20200 if (g == e || INTEGERP (g->object))
20201 {
20202 if (row->truncated_on_left_p || row->truncated_on_right_p)
20203 goto simulate_display;
20204 if (!row->reversed_p)
20205 row += dir;
20206 else
20207 row -= dir;
20208 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20209 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20210 goto simulate_display;
20211
20212 if (dir > 0)
20213 {
20214 if (row->reversed_p && !row->continued_p)
20215 {
20216 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20217 w->cursor.vpos = -1;
20218 return make_number (PT);
20219 }
20220 g = row->glyphs[TEXT_AREA];
20221 e = g + row->used[TEXT_AREA];
20222 for ( ; g < e; g++)
20223 {
20224 if (BUFFERP (g->object)
20225 /* Empty lines have only one glyph, which stands
20226 for the newline, and whose charpos is the
20227 buffer position of the newline. */
20228 || ROW_GLYPH_NEWLINE_P (row, g)
20229 /* When the buffer ends in a newline, the line at
20230 EOB also has one glyph, but its charpos is -1. */
20231 || (row->ends_at_zv_p
20232 && !row->reversed_p
20233 && INTEGERP (g->object)
20234 && g->type == CHAR_GLYPH
20235 && g->u.ch == ' '))
20236 {
20237 if (g->charpos > 0)
20238 SET_PT (g->charpos);
20239 else if (!row->reversed_p
20240 && row->ends_at_zv_p
20241 && PT != ZV)
20242 SET_PT (ZV);
20243 else
20244 continue;
20245 w->cursor.vpos = -1;
20246 return make_number (PT);
20247 }
20248 }
20249 }
20250 else
20251 {
20252 if (!row->reversed_p && !row->continued_p)
20253 {
20254 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20255 w->cursor.vpos = -1;
20256 return make_number (PT);
20257 }
20258 e = row->glyphs[TEXT_AREA];
20259 g = e + row->used[TEXT_AREA] - 1;
20260 for ( ; g >= e; g--)
20261 {
20262 if (BUFFERP (g->object)
20263 || (ROW_GLYPH_NEWLINE_P (row, g)
20264 && g->charpos > 0)
20265 /* Empty R2L lines on GUI frames have the buffer
20266 position of the newline stored in the stretch
20267 glyph. */
20268 || g->type == STRETCH_GLYPH
20269 || (row->ends_at_zv_p
20270 && row->reversed_p
20271 && INTEGERP (g->object)
20272 && g->type == CHAR_GLYPH
20273 && g->u.ch == ' '))
20274 {
20275 if (g->charpos > 0)
20276 SET_PT (g->charpos);
20277 else if (row->reversed_p
20278 && row->ends_at_zv_p
20279 && PT != ZV)
20280 SET_PT (ZV);
20281 else
20282 continue;
20283 w->cursor.vpos = -1;
20284 return make_number (PT);
20285 }
20286 }
20287 }
20288 }
20289 }
20290
20291 simulate_display:
20292
20293 /* If we wind up here, we failed to move by using the glyphs, so we
20294 need to simulate display instead. */
20295
20296 if (b)
20297 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20298 else
20299 paragraph_dir = Qleft_to_right;
20300 if (EQ (paragraph_dir, Qright_to_left))
20301 dir = -dir;
20302 if (PT <= BEGV && dir < 0)
20303 xsignal0 (Qbeginning_of_buffer);
20304 else if (PT >= ZV && dir > 0)
20305 xsignal0 (Qend_of_buffer);
20306 else
20307 {
20308 struct text_pos pt;
20309 struct it it;
20310 int pt_x, target_x, pixel_width, pt_vpos;
20311 bool at_eol_p;
20312 bool overshoot_expected = false;
20313 bool target_is_eol_p = false;
20314
20315 /* Setup the arena. */
20316 SET_TEXT_POS (pt, PT, PT_BYTE);
20317 start_display (&it, w, pt);
20318
20319 if (it.cmp_it.id < 0
20320 && it.method == GET_FROM_STRING
20321 && it.area == TEXT_AREA
20322 && it.string_from_display_prop_p
20323 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20324 overshoot_expected = true;
20325
20326 /* Find the X coordinate of point. We start from the beginning
20327 of this or previous line to make sure we are before point in
20328 the logical order (since the move_it_* functions can only
20329 move forward). */
20330 reseat_at_previous_visible_line_start (&it);
20331 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20332 if (IT_CHARPOS (it) != PT)
20333 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20334 -1, -1, -1, MOVE_TO_POS);
20335 pt_x = it.current_x;
20336 pt_vpos = it.vpos;
20337 if (dir > 0 || overshoot_expected)
20338 {
20339 struct glyph_row *row = it.glyph_row;
20340
20341 /* When point is at beginning of line, we don't have
20342 information about the glyph there loaded into struct
20343 it. Calling get_next_display_element fixes that. */
20344 if (pt_x == 0)
20345 get_next_display_element (&it);
20346 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20347 it.glyph_row = NULL;
20348 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20349 it.glyph_row = row;
20350 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20351 it, lest it will become out of sync with it's buffer
20352 position. */
20353 it.current_x = pt_x;
20354 }
20355 else
20356 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20357 pixel_width = it.pixel_width;
20358 if (overshoot_expected && at_eol_p)
20359 pixel_width = 0;
20360 else if (pixel_width <= 0)
20361 pixel_width = 1;
20362
20363 /* If there's a display string at point, we are actually at the
20364 glyph to the left of point, so we need to correct the X
20365 coordinate. */
20366 if (overshoot_expected)
20367 pt_x += pixel_width;
20368
20369 /* Compute target X coordinate, either to the left or to the
20370 right of point. On TTY frames, all characters have the same
20371 pixel width of 1, so we can use that. On GUI frames we don't
20372 have an easy way of getting at the pixel width of the
20373 character to the left of point, so we use a different method
20374 of getting to that place. */
20375 if (dir > 0)
20376 target_x = pt_x + pixel_width;
20377 else
20378 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20379
20380 /* Target X coordinate could be one line above or below the line
20381 of point, in which case we need to adjust the target X
20382 coordinate. Also, if moving to the left, we need to begin at
20383 the left edge of the point's screen line. */
20384 if (dir < 0)
20385 {
20386 if (pt_x > 0)
20387 {
20388 start_display (&it, w, pt);
20389 reseat_at_previous_visible_line_start (&it);
20390 it.current_x = it.current_y = it.hpos = 0;
20391 if (pt_vpos != 0)
20392 move_it_by_lines (&it, pt_vpos);
20393 }
20394 else
20395 {
20396 move_it_by_lines (&it, -1);
20397 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20398 target_is_eol_p = true;
20399 }
20400 }
20401 else
20402 {
20403 if (at_eol_p
20404 || (target_x >= it.last_visible_x
20405 && it.line_wrap != TRUNCATE))
20406 {
20407 if (pt_x > 0)
20408 move_it_by_lines (&it, 0);
20409 move_it_by_lines (&it, 1);
20410 target_x = 0;
20411 }
20412 }
20413
20414 /* Move to the target X coordinate. */
20415 #ifdef HAVE_WINDOW_SYSTEM
20416 /* On GUI frames, as we don't know the X coordinate of the
20417 character to the left of point, moving point to the left
20418 requires walking, one grapheme cluster at a time, until we
20419 find ourself at a place immediately to the left of the
20420 character at point. */
20421 if (FRAME_WINDOW_P (it.f) && dir < 0)
20422 {
20423 struct text_pos new_pos = it.current.pos;
20424 enum move_it_result rc = MOVE_X_REACHED;
20425
20426 while (it.current_x + it.pixel_width <= target_x
20427 && rc == MOVE_X_REACHED)
20428 {
20429 int new_x = it.current_x + it.pixel_width;
20430
20431 new_pos = it.current.pos;
20432 if (new_x == it.current_x)
20433 new_x++;
20434 rc = move_it_in_display_line_to (&it, ZV, new_x,
20435 MOVE_TO_POS | MOVE_TO_X);
20436 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20437 break;
20438 }
20439 /* If we ended up on a composed character inside
20440 bidi-reordered text (e.g., Hebrew text with diacritics),
20441 the iterator gives us the buffer position of the last (in
20442 logical order) character of the composed grapheme cluster,
20443 which is not what we want. So we cheat: we compute the
20444 character position of the character that follows (in the
20445 logical order) the one where the above loop stopped. That
20446 character will appear on display to the left of point. */
20447 if (it.bidi_p
20448 && it.bidi_it.scan_dir == -1
20449 && new_pos.charpos - IT_CHARPOS (it) > 1)
20450 {
20451 new_pos.charpos = IT_CHARPOS (it) + 1;
20452 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20453 }
20454 it.current.pos = new_pos;
20455 }
20456 else
20457 #endif
20458 if (it.current_x != target_x)
20459 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20460
20461 /* When lines are truncated, the above loop will stop at the
20462 window edge. But we want to get to the end of line, even if
20463 it is beyond the window edge; automatic hscroll will then
20464 scroll the window to show point as appropriate. */
20465 if (target_is_eol_p && it.line_wrap == TRUNCATE
20466 && get_next_display_element (&it))
20467 {
20468 struct text_pos new_pos = it.current.pos;
20469
20470 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20471 {
20472 set_iterator_to_next (&it, 0);
20473 if (it.method == GET_FROM_BUFFER)
20474 new_pos = it.current.pos;
20475 if (!get_next_display_element (&it))
20476 break;
20477 }
20478
20479 it.current.pos = new_pos;
20480 }
20481
20482 /* If we ended up in a display string that covers point, move to
20483 buffer position to the right in the visual order. */
20484 if (dir > 0)
20485 {
20486 while (IT_CHARPOS (it) == PT)
20487 {
20488 set_iterator_to_next (&it, 0);
20489 if (!get_next_display_element (&it))
20490 break;
20491 }
20492 }
20493
20494 /* Move point to that position. */
20495 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20496 }
20497
20498 return make_number (PT);
20499
20500 #undef ROW_GLYPH_NEWLINE_P
20501 }
20502
20503 \f
20504 /***********************************************************************
20505 Menu Bar
20506 ***********************************************************************/
20507
20508 /* Redisplay the menu bar in the frame for window W.
20509
20510 The menu bar of X frames that don't have X toolkit support is
20511 displayed in a special window W->frame->menu_bar_window.
20512
20513 The menu bar of terminal frames is treated specially as far as
20514 glyph matrices are concerned. Menu bar lines are not part of
20515 windows, so the update is done directly on the frame matrix rows
20516 for the menu bar. */
20517
20518 static void
20519 display_menu_bar (struct window *w)
20520 {
20521 struct frame *f = XFRAME (WINDOW_FRAME (w));
20522 struct it it;
20523 Lisp_Object items;
20524 int i;
20525
20526 /* Don't do all this for graphical frames. */
20527 #ifdef HAVE_NTGUI
20528 if (FRAME_W32_P (f))
20529 return;
20530 #endif
20531 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20532 if (FRAME_X_P (f))
20533 return;
20534 #endif
20535
20536 #ifdef HAVE_NS
20537 if (FRAME_NS_P (f))
20538 return;
20539 #endif /* HAVE_NS */
20540
20541 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20542 eassert (!FRAME_WINDOW_P (f));
20543 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20544 it.first_visible_x = 0;
20545 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20546 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20547 if (FRAME_WINDOW_P (f))
20548 {
20549 /* Menu bar lines are displayed in the desired matrix of the
20550 dummy window menu_bar_window. */
20551 struct window *menu_w;
20552 menu_w = XWINDOW (f->menu_bar_window);
20553 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20554 MENU_FACE_ID);
20555 it.first_visible_x = 0;
20556 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20557 }
20558 else
20559 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20560 {
20561 /* This is a TTY frame, i.e. character hpos/vpos are used as
20562 pixel x/y. */
20563 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20564 MENU_FACE_ID);
20565 it.first_visible_x = 0;
20566 it.last_visible_x = FRAME_COLS (f);
20567 }
20568
20569 /* FIXME: This should be controlled by a user option. See the
20570 comments in redisplay_tool_bar and display_mode_line about
20571 this. */
20572 it.paragraph_embedding = L2R;
20573
20574 /* Clear all rows of the menu bar. */
20575 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20576 {
20577 struct glyph_row *row = it.glyph_row + i;
20578 clear_glyph_row (row);
20579 row->enabled_p = 1;
20580 row->full_width_p = 1;
20581 }
20582
20583 /* Display all items of the menu bar. */
20584 items = FRAME_MENU_BAR_ITEMS (it.f);
20585 for (i = 0; i < ASIZE (items); i += 4)
20586 {
20587 Lisp_Object string;
20588
20589 /* Stop at nil string. */
20590 string = AREF (items, i + 1);
20591 if (NILP (string))
20592 break;
20593
20594 /* Remember where item was displayed. */
20595 ASET (items, i + 3, make_number (it.hpos));
20596
20597 /* Display the item, pad with one space. */
20598 if (it.current_x < it.last_visible_x)
20599 display_string (NULL, string, Qnil, 0, 0, &it,
20600 SCHARS (string) + 1, 0, 0, -1);
20601 }
20602
20603 /* Fill out the line with spaces. */
20604 if (it.current_x < it.last_visible_x)
20605 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20606
20607 /* Compute the total height of the lines. */
20608 compute_line_metrics (&it);
20609 }
20610
20611 /* Deep copy of a glyph row, including the glyphs. */
20612 static void
20613 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20614 {
20615 struct glyph *pointers[1 + LAST_AREA];
20616 int to_used = to->used[TEXT_AREA];
20617
20618 /* Save glyph pointers of TO. */
20619 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20620
20621 /* Do a structure assignment. */
20622 *to = *from;
20623
20624 /* Restore original glyph pointers of TO. */
20625 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20626
20627 /* Copy the glyphs. */
20628 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20629 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20630
20631 /* If we filled only part of the TO row, fill the rest with
20632 space_glyph (which will display as empty space). */
20633 if (to_used > from->used[TEXT_AREA])
20634 fill_up_frame_row_with_spaces (to, to_used);
20635 }
20636
20637 /* Display one menu item on a TTY, by overwriting the glyphs in the
20638 frame F's desired glyph matrix with glyphs produced from the menu
20639 item text. Called from term.c to display TTY drop-down menus one
20640 item at a time.
20641
20642 ITEM_TEXT is the menu item text as a C string.
20643
20644 FACE_ID is the face ID to be used for this menu item. FACE_ID
20645 could specify one of 3 faces: a face for an enabled item, a face
20646 for a disabled item, or a face for a selected item.
20647
20648 X and Y are coordinates of the first glyph in the frame's desired
20649 matrix to be overwritten by the menu item. Since this is a TTY, Y
20650 is the zero-based number of the glyph row and X is the zero-based
20651 glyph number in the row, starting from left, where to start
20652 displaying the item.
20653
20654 SUBMENU non-zero means this menu item drops down a submenu, which
20655 should be indicated by displaying a proper visual cue after the
20656 item text. */
20657
20658 void
20659 display_tty_menu_item (const char *item_text, int width, int face_id,
20660 int x, int y, int submenu)
20661 {
20662 struct it it;
20663 struct frame *f = SELECTED_FRAME ();
20664 struct window *w = XWINDOW (f->selected_window);
20665 int saved_used, saved_truncated, saved_width, saved_reversed;
20666 struct glyph_row *row;
20667 size_t item_len = strlen (item_text);
20668
20669 eassert (FRAME_TERMCAP_P (f));
20670
20671 /* Don't write beyond the matrix's last row. This can happen for
20672 TTY screens that are not high enough to show the entire menu.
20673 (This is actually a bit of defensive programming, as
20674 tty_menu_display already limits the number of menu items to one
20675 less than the number of screen lines.) */
20676 if (y >= f->desired_matrix->nrows)
20677 return;
20678
20679 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20680 it.first_visible_x = 0;
20681 it.last_visible_x = FRAME_COLS (f) - 1;
20682 row = it.glyph_row;
20683 /* Start with the row contents from the current matrix. */
20684 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20685 saved_width = row->full_width_p;
20686 row->full_width_p = 1;
20687 saved_reversed = row->reversed_p;
20688 row->reversed_p = 0;
20689 row->enabled_p = 1;
20690
20691 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20692 desired face. */
20693 eassert (x < f->desired_matrix->matrix_w);
20694 it.current_x = it.hpos = x;
20695 it.current_y = it.vpos = y;
20696 saved_used = row->used[TEXT_AREA];
20697 saved_truncated = row->truncated_on_right_p;
20698 row->used[TEXT_AREA] = x;
20699 it.face_id = face_id;
20700 it.line_wrap = TRUNCATE;
20701
20702 /* FIXME: This should be controlled by a user option. See the
20703 comments in redisplay_tool_bar and display_mode_line about this.
20704 Also, if paragraph_embedding could ever be R2L, changes will be
20705 needed to avoid shifting to the right the row characters in
20706 term.c:append_glyph. */
20707 it.paragraph_embedding = L2R;
20708
20709 /* Pad with a space on the left. */
20710 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20711 width--;
20712 /* Display the menu item, pad with spaces to WIDTH. */
20713 if (submenu)
20714 {
20715 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20716 item_len, 0, FRAME_COLS (f) - 1, -1);
20717 width -= item_len;
20718 /* Indicate with " >" that there's a submenu. */
20719 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20720 FRAME_COLS (f) - 1, -1);
20721 }
20722 else
20723 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20724 width, 0, FRAME_COLS (f) - 1, -1);
20725
20726 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20727 row->truncated_on_right_p = saved_truncated;
20728 row->hash = row_hash (row);
20729 row->full_width_p = saved_width;
20730 row->reversed_p = saved_reversed;
20731 }
20732 \f
20733 /***********************************************************************
20734 Mode Line
20735 ***********************************************************************/
20736
20737 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20738 FORCE is non-zero, redisplay mode lines unconditionally.
20739 Otherwise, redisplay only mode lines that are garbaged. Value is
20740 the number of windows whose mode lines were redisplayed. */
20741
20742 static int
20743 redisplay_mode_lines (Lisp_Object window, int force)
20744 {
20745 int nwindows = 0;
20746
20747 while (!NILP (window))
20748 {
20749 struct window *w = XWINDOW (window);
20750
20751 if (WINDOWP (w->contents))
20752 nwindows += redisplay_mode_lines (w->contents, force);
20753 else if (force
20754 || FRAME_GARBAGED_P (XFRAME (w->frame))
20755 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20756 {
20757 struct text_pos lpoint;
20758 struct buffer *old = current_buffer;
20759
20760 /* Set the window's buffer for the mode line display. */
20761 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20762 set_buffer_internal_1 (XBUFFER (w->contents));
20763
20764 /* Point refers normally to the selected window. For any
20765 other window, set up appropriate value. */
20766 if (!EQ (window, selected_window))
20767 {
20768 struct text_pos pt;
20769
20770 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20771 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20772 }
20773
20774 /* Display mode lines. */
20775 clear_glyph_matrix (w->desired_matrix);
20776 if (display_mode_lines (w))
20777 {
20778 ++nwindows;
20779 w->must_be_updated_p = 1;
20780 }
20781
20782 /* Restore old settings. */
20783 set_buffer_internal_1 (old);
20784 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20785 }
20786
20787 window = w->next;
20788 }
20789
20790 return nwindows;
20791 }
20792
20793
20794 /* Display the mode and/or header line of window W. Value is the
20795 sum number of mode lines and header lines displayed. */
20796
20797 static int
20798 display_mode_lines (struct window *w)
20799 {
20800 Lisp_Object old_selected_window = selected_window;
20801 Lisp_Object old_selected_frame = selected_frame;
20802 Lisp_Object new_frame = w->frame;
20803 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20804 int n = 0;
20805
20806 selected_frame = new_frame;
20807 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20808 or window's point, then we'd need select_window_1 here as well. */
20809 XSETWINDOW (selected_window, w);
20810 XFRAME (new_frame)->selected_window = selected_window;
20811
20812 /* These will be set while the mode line specs are processed. */
20813 line_number_displayed = 0;
20814 w->column_number_displayed = -1;
20815
20816 if (WINDOW_WANTS_MODELINE_P (w))
20817 {
20818 struct window *sel_w = XWINDOW (old_selected_window);
20819
20820 /* Select mode line face based on the real selected window. */
20821 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20822 BVAR (current_buffer, mode_line_format));
20823 ++n;
20824 }
20825
20826 if (WINDOW_WANTS_HEADER_LINE_P (w))
20827 {
20828 display_mode_line (w, HEADER_LINE_FACE_ID,
20829 BVAR (current_buffer, header_line_format));
20830 ++n;
20831 }
20832
20833 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20834 selected_frame = old_selected_frame;
20835 selected_window = old_selected_window;
20836 return n;
20837 }
20838
20839
20840 /* Display mode or header line of window W. FACE_ID specifies which
20841 line to display; it is either MODE_LINE_FACE_ID or
20842 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20843 display. Value is the pixel height of the mode/header line
20844 displayed. */
20845
20846 static int
20847 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20848 {
20849 struct it it;
20850 struct face *face;
20851 ptrdiff_t count = SPECPDL_INDEX ();
20852
20853 init_iterator (&it, w, -1, -1, NULL, face_id);
20854 /* Don't extend on a previously drawn mode-line.
20855 This may happen if called from pos_visible_p. */
20856 it.glyph_row->enabled_p = 0;
20857 prepare_desired_row (it.glyph_row);
20858
20859 it.glyph_row->mode_line_p = 1;
20860
20861 /* FIXME: This should be controlled by a user option. But
20862 supporting such an option is not trivial, since the mode line is
20863 made up of many separate strings. */
20864 it.paragraph_embedding = L2R;
20865
20866 record_unwind_protect (unwind_format_mode_line,
20867 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20868
20869 mode_line_target = MODE_LINE_DISPLAY;
20870
20871 /* Temporarily make frame's keyboard the current kboard so that
20872 kboard-local variables in the mode_line_format will get the right
20873 values. */
20874 push_kboard (FRAME_KBOARD (it.f));
20875 record_unwind_save_match_data ();
20876 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20877 pop_kboard ();
20878
20879 unbind_to (count, Qnil);
20880
20881 /* Fill up with spaces. */
20882 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20883
20884 compute_line_metrics (&it);
20885 it.glyph_row->full_width_p = 1;
20886 it.glyph_row->continued_p = 0;
20887 it.glyph_row->truncated_on_left_p = 0;
20888 it.glyph_row->truncated_on_right_p = 0;
20889
20890 /* Make a 3D mode-line have a shadow at its right end. */
20891 face = FACE_FROM_ID (it.f, face_id);
20892 extend_face_to_end_of_line (&it);
20893 if (face->box != FACE_NO_BOX)
20894 {
20895 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20896 + it.glyph_row->used[TEXT_AREA] - 1);
20897 last->right_box_line_p = 1;
20898 }
20899
20900 return it.glyph_row->height;
20901 }
20902
20903 /* Move element ELT in LIST to the front of LIST.
20904 Return the updated list. */
20905
20906 static Lisp_Object
20907 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20908 {
20909 register Lisp_Object tail, prev;
20910 register Lisp_Object tem;
20911
20912 tail = list;
20913 prev = Qnil;
20914 while (CONSP (tail))
20915 {
20916 tem = XCAR (tail);
20917
20918 if (EQ (elt, tem))
20919 {
20920 /* Splice out the link TAIL. */
20921 if (NILP (prev))
20922 list = XCDR (tail);
20923 else
20924 Fsetcdr (prev, XCDR (tail));
20925
20926 /* Now make it the first. */
20927 Fsetcdr (tail, list);
20928 return tail;
20929 }
20930 else
20931 prev = tail;
20932 tail = XCDR (tail);
20933 QUIT;
20934 }
20935
20936 /* Not found--return unchanged LIST. */
20937 return list;
20938 }
20939
20940 /* Contribute ELT to the mode line for window IT->w. How it
20941 translates into text depends on its data type.
20942
20943 IT describes the display environment in which we display, as usual.
20944
20945 DEPTH is the depth in recursion. It is used to prevent
20946 infinite recursion here.
20947
20948 FIELD_WIDTH is the number of characters the display of ELT should
20949 occupy in the mode line, and PRECISION is the maximum number of
20950 characters to display from ELT's representation. See
20951 display_string for details.
20952
20953 Returns the hpos of the end of the text generated by ELT.
20954
20955 PROPS is a property list to add to any string we encounter.
20956
20957 If RISKY is nonzero, remove (disregard) any properties in any string
20958 we encounter, and ignore :eval and :propertize.
20959
20960 The global variable `mode_line_target' determines whether the
20961 output is passed to `store_mode_line_noprop',
20962 `store_mode_line_string', or `display_string'. */
20963
20964 static int
20965 display_mode_element (struct it *it, int depth, int field_width, int precision,
20966 Lisp_Object elt, Lisp_Object props, int risky)
20967 {
20968 int n = 0, field, prec;
20969 int literal = 0;
20970
20971 tail_recurse:
20972 if (depth > 100)
20973 elt = build_string ("*too-deep*");
20974
20975 depth++;
20976
20977 switch (XTYPE (elt))
20978 {
20979 case Lisp_String:
20980 {
20981 /* A string: output it and check for %-constructs within it. */
20982 unsigned char c;
20983 ptrdiff_t offset = 0;
20984
20985 if (SCHARS (elt) > 0
20986 && (!NILP (props) || risky))
20987 {
20988 Lisp_Object oprops, aelt;
20989 oprops = Ftext_properties_at (make_number (0), elt);
20990
20991 /* If the starting string's properties are not what
20992 we want, translate the string. Also, if the string
20993 is risky, do that anyway. */
20994
20995 if (NILP (Fequal (props, oprops)) || risky)
20996 {
20997 /* If the starting string has properties,
20998 merge the specified ones onto the existing ones. */
20999 if (! NILP (oprops) && !risky)
21000 {
21001 Lisp_Object tem;
21002
21003 oprops = Fcopy_sequence (oprops);
21004 tem = props;
21005 while (CONSP (tem))
21006 {
21007 oprops = Fplist_put (oprops, XCAR (tem),
21008 XCAR (XCDR (tem)));
21009 tem = XCDR (XCDR (tem));
21010 }
21011 props = oprops;
21012 }
21013
21014 aelt = Fassoc (elt, mode_line_proptrans_alist);
21015 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21016 {
21017 /* AELT is what we want. Move it to the front
21018 without consing. */
21019 elt = XCAR (aelt);
21020 mode_line_proptrans_alist
21021 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21022 }
21023 else
21024 {
21025 Lisp_Object tem;
21026
21027 /* If AELT has the wrong props, it is useless.
21028 so get rid of it. */
21029 if (! NILP (aelt))
21030 mode_line_proptrans_alist
21031 = Fdelq (aelt, mode_line_proptrans_alist);
21032
21033 elt = Fcopy_sequence (elt);
21034 Fset_text_properties (make_number (0), Flength (elt),
21035 props, elt);
21036 /* Add this item to mode_line_proptrans_alist. */
21037 mode_line_proptrans_alist
21038 = Fcons (Fcons (elt, props),
21039 mode_line_proptrans_alist);
21040 /* Truncate mode_line_proptrans_alist
21041 to at most 50 elements. */
21042 tem = Fnthcdr (make_number (50),
21043 mode_line_proptrans_alist);
21044 if (! NILP (tem))
21045 XSETCDR (tem, Qnil);
21046 }
21047 }
21048 }
21049
21050 offset = 0;
21051
21052 if (literal)
21053 {
21054 prec = precision - n;
21055 switch (mode_line_target)
21056 {
21057 case MODE_LINE_NOPROP:
21058 case MODE_LINE_TITLE:
21059 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21060 break;
21061 case MODE_LINE_STRING:
21062 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21063 break;
21064 case MODE_LINE_DISPLAY:
21065 n += display_string (NULL, elt, Qnil, 0, 0, it,
21066 0, prec, 0, STRING_MULTIBYTE (elt));
21067 break;
21068 }
21069
21070 break;
21071 }
21072
21073 /* Handle the non-literal case. */
21074
21075 while ((precision <= 0 || n < precision)
21076 && SREF (elt, offset) != 0
21077 && (mode_line_target != MODE_LINE_DISPLAY
21078 || it->current_x < it->last_visible_x))
21079 {
21080 ptrdiff_t last_offset = offset;
21081
21082 /* Advance to end of string or next format specifier. */
21083 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21084 ;
21085
21086 if (offset - 1 != last_offset)
21087 {
21088 ptrdiff_t nchars, nbytes;
21089
21090 /* Output to end of string or up to '%'. Field width
21091 is length of string. Don't output more than
21092 PRECISION allows us. */
21093 offset--;
21094
21095 prec = c_string_width (SDATA (elt) + last_offset,
21096 offset - last_offset, precision - n,
21097 &nchars, &nbytes);
21098
21099 switch (mode_line_target)
21100 {
21101 case MODE_LINE_NOPROP:
21102 case MODE_LINE_TITLE:
21103 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21104 break;
21105 case MODE_LINE_STRING:
21106 {
21107 ptrdiff_t bytepos = last_offset;
21108 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21109 ptrdiff_t endpos = (precision <= 0
21110 ? string_byte_to_char (elt, offset)
21111 : charpos + nchars);
21112
21113 n += store_mode_line_string (NULL,
21114 Fsubstring (elt, make_number (charpos),
21115 make_number (endpos)),
21116 0, 0, 0, Qnil);
21117 }
21118 break;
21119 case MODE_LINE_DISPLAY:
21120 {
21121 ptrdiff_t bytepos = last_offset;
21122 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21123
21124 if (precision <= 0)
21125 nchars = string_byte_to_char (elt, offset) - charpos;
21126 n += display_string (NULL, elt, Qnil, 0, charpos,
21127 it, 0, nchars, 0,
21128 STRING_MULTIBYTE (elt));
21129 }
21130 break;
21131 }
21132 }
21133 else /* c == '%' */
21134 {
21135 ptrdiff_t percent_position = offset;
21136
21137 /* Get the specified minimum width. Zero means
21138 don't pad. */
21139 field = 0;
21140 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21141 field = field * 10 + c - '0';
21142
21143 /* Don't pad beyond the total padding allowed. */
21144 if (field_width - n > 0 && field > field_width - n)
21145 field = field_width - n;
21146
21147 /* Note that either PRECISION <= 0 or N < PRECISION. */
21148 prec = precision - n;
21149
21150 if (c == 'M')
21151 n += display_mode_element (it, depth, field, prec,
21152 Vglobal_mode_string, props,
21153 risky);
21154 else if (c != 0)
21155 {
21156 bool multibyte;
21157 ptrdiff_t bytepos, charpos;
21158 const char *spec;
21159 Lisp_Object string;
21160
21161 bytepos = percent_position;
21162 charpos = (STRING_MULTIBYTE (elt)
21163 ? string_byte_to_char (elt, bytepos)
21164 : bytepos);
21165 spec = decode_mode_spec (it->w, c, field, &string);
21166 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21167
21168 switch (mode_line_target)
21169 {
21170 case MODE_LINE_NOPROP:
21171 case MODE_LINE_TITLE:
21172 n += store_mode_line_noprop (spec, field, prec);
21173 break;
21174 case MODE_LINE_STRING:
21175 {
21176 Lisp_Object tem = build_string (spec);
21177 props = Ftext_properties_at (make_number (charpos), elt);
21178 /* Should only keep face property in props */
21179 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21180 }
21181 break;
21182 case MODE_LINE_DISPLAY:
21183 {
21184 int nglyphs_before, nwritten;
21185
21186 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21187 nwritten = display_string (spec, string, elt,
21188 charpos, 0, it,
21189 field, prec, 0,
21190 multibyte);
21191
21192 /* Assign to the glyphs written above the
21193 string where the `%x' came from, position
21194 of the `%'. */
21195 if (nwritten > 0)
21196 {
21197 struct glyph *glyph
21198 = (it->glyph_row->glyphs[TEXT_AREA]
21199 + nglyphs_before);
21200 int i;
21201
21202 for (i = 0; i < nwritten; ++i)
21203 {
21204 glyph[i].object = elt;
21205 glyph[i].charpos = charpos;
21206 }
21207
21208 n += nwritten;
21209 }
21210 }
21211 break;
21212 }
21213 }
21214 else /* c == 0 */
21215 break;
21216 }
21217 }
21218 }
21219 break;
21220
21221 case Lisp_Symbol:
21222 /* A symbol: process the value of the symbol recursively
21223 as if it appeared here directly. Avoid error if symbol void.
21224 Special case: if value of symbol is a string, output the string
21225 literally. */
21226 {
21227 register Lisp_Object tem;
21228
21229 /* If the variable is not marked as risky to set
21230 then its contents are risky to use. */
21231 if (NILP (Fget (elt, Qrisky_local_variable)))
21232 risky = 1;
21233
21234 tem = Fboundp (elt);
21235 if (!NILP (tem))
21236 {
21237 tem = Fsymbol_value (elt);
21238 /* If value is a string, output that string literally:
21239 don't check for % within it. */
21240 if (STRINGP (tem))
21241 literal = 1;
21242
21243 if (!EQ (tem, elt))
21244 {
21245 /* Give up right away for nil or t. */
21246 elt = tem;
21247 goto tail_recurse;
21248 }
21249 }
21250 }
21251 break;
21252
21253 case Lisp_Cons:
21254 {
21255 register Lisp_Object car, tem;
21256
21257 /* A cons cell: five distinct cases.
21258 If first element is :eval or :propertize, do something special.
21259 If first element is a string or a cons, process all the elements
21260 and effectively concatenate them.
21261 If first element is a negative number, truncate displaying cdr to
21262 at most that many characters. If positive, pad (with spaces)
21263 to at least that many characters.
21264 If first element is a symbol, process the cadr or caddr recursively
21265 according to whether the symbol's value is non-nil or nil. */
21266 car = XCAR (elt);
21267 if (EQ (car, QCeval))
21268 {
21269 /* An element of the form (:eval FORM) means evaluate FORM
21270 and use the result as mode line elements. */
21271
21272 if (risky)
21273 break;
21274
21275 if (CONSP (XCDR (elt)))
21276 {
21277 Lisp_Object spec;
21278 spec = safe_eval (XCAR (XCDR (elt)));
21279 n += display_mode_element (it, depth, field_width - n,
21280 precision - n, spec, props,
21281 risky);
21282 }
21283 }
21284 else if (EQ (car, QCpropertize))
21285 {
21286 /* An element of the form (:propertize ELT PROPS...)
21287 means display ELT but applying properties PROPS. */
21288
21289 if (risky)
21290 break;
21291
21292 if (CONSP (XCDR (elt)))
21293 n += display_mode_element (it, depth, field_width - n,
21294 precision - n, XCAR (XCDR (elt)),
21295 XCDR (XCDR (elt)), risky);
21296 }
21297 else if (SYMBOLP (car))
21298 {
21299 tem = Fboundp (car);
21300 elt = XCDR (elt);
21301 if (!CONSP (elt))
21302 goto invalid;
21303 /* elt is now the cdr, and we know it is a cons cell.
21304 Use its car if CAR has a non-nil value. */
21305 if (!NILP (tem))
21306 {
21307 tem = Fsymbol_value (car);
21308 if (!NILP (tem))
21309 {
21310 elt = XCAR (elt);
21311 goto tail_recurse;
21312 }
21313 }
21314 /* Symbol's value is nil (or symbol is unbound)
21315 Get the cddr of the original list
21316 and if possible find the caddr and use that. */
21317 elt = XCDR (elt);
21318 if (NILP (elt))
21319 break;
21320 else if (!CONSP (elt))
21321 goto invalid;
21322 elt = XCAR (elt);
21323 goto tail_recurse;
21324 }
21325 else if (INTEGERP (car))
21326 {
21327 register int lim = XINT (car);
21328 elt = XCDR (elt);
21329 if (lim < 0)
21330 {
21331 /* Negative int means reduce maximum width. */
21332 if (precision <= 0)
21333 precision = -lim;
21334 else
21335 precision = min (precision, -lim);
21336 }
21337 else if (lim > 0)
21338 {
21339 /* Padding specified. Don't let it be more than
21340 current maximum. */
21341 if (precision > 0)
21342 lim = min (precision, lim);
21343
21344 /* If that's more padding than already wanted, queue it.
21345 But don't reduce padding already specified even if
21346 that is beyond the current truncation point. */
21347 field_width = max (lim, field_width);
21348 }
21349 goto tail_recurse;
21350 }
21351 else if (STRINGP (car) || CONSP (car))
21352 {
21353 Lisp_Object halftail = elt;
21354 int len = 0;
21355
21356 while (CONSP (elt)
21357 && (precision <= 0 || n < precision))
21358 {
21359 n += display_mode_element (it, depth,
21360 /* Do padding only after the last
21361 element in the list. */
21362 (! CONSP (XCDR (elt))
21363 ? field_width - n
21364 : 0),
21365 precision - n, XCAR (elt),
21366 props, risky);
21367 elt = XCDR (elt);
21368 len++;
21369 if ((len & 1) == 0)
21370 halftail = XCDR (halftail);
21371 /* Check for cycle. */
21372 if (EQ (halftail, elt))
21373 break;
21374 }
21375 }
21376 }
21377 break;
21378
21379 default:
21380 invalid:
21381 elt = build_string ("*invalid*");
21382 goto tail_recurse;
21383 }
21384
21385 /* Pad to FIELD_WIDTH. */
21386 if (field_width > 0 && n < field_width)
21387 {
21388 switch (mode_line_target)
21389 {
21390 case MODE_LINE_NOPROP:
21391 case MODE_LINE_TITLE:
21392 n += store_mode_line_noprop ("", field_width - n, 0);
21393 break;
21394 case MODE_LINE_STRING:
21395 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21396 break;
21397 case MODE_LINE_DISPLAY:
21398 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21399 0, 0, 0);
21400 break;
21401 }
21402 }
21403
21404 return n;
21405 }
21406
21407 /* Store a mode-line string element in mode_line_string_list.
21408
21409 If STRING is non-null, display that C string. Otherwise, the Lisp
21410 string LISP_STRING is displayed.
21411
21412 FIELD_WIDTH is the minimum number of output glyphs to produce.
21413 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21414 with spaces. FIELD_WIDTH <= 0 means don't pad.
21415
21416 PRECISION is the maximum number of characters to output from
21417 STRING. PRECISION <= 0 means don't truncate the string.
21418
21419 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21420 properties to the string.
21421
21422 PROPS are the properties to add to the string.
21423 The mode_line_string_face face property is always added to the string.
21424 */
21425
21426 static int
21427 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21428 int field_width, int precision, Lisp_Object props)
21429 {
21430 ptrdiff_t len;
21431 int n = 0;
21432
21433 if (string != NULL)
21434 {
21435 len = strlen (string);
21436 if (precision > 0 && len > precision)
21437 len = precision;
21438 lisp_string = make_string (string, len);
21439 if (NILP (props))
21440 props = mode_line_string_face_prop;
21441 else if (!NILP (mode_line_string_face))
21442 {
21443 Lisp_Object face = Fplist_get (props, Qface);
21444 props = Fcopy_sequence (props);
21445 if (NILP (face))
21446 face = mode_line_string_face;
21447 else
21448 face = list2 (face, mode_line_string_face);
21449 props = Fplist_put (props, Qface, face);
21450 }
21451 Fadd_text_properties (make_number (0), make_number (len),
21452 props, lisp_string);
21453 }
21454 else
21455 {
21456 len = XFASTINT (Flength (lisp_string));
21457 if (precision > 0 && len > precision)
21458 {
21459 len = precision;
21460 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21461 precision = -1;
21462 }
21463 if (!NILP (mode_line_string_face))
21464 {
21465 Lisp_Object face;
21466 if (NILP (props))
21467 props = Ftext_properties_at (make_number (0), lisp_string);
21468 face = Fplist_get (props, Qface);
21469 if (NILP (face))
21470 face = mode_line_string_face;
21471 else
21472 face = list2 (face, mode_line_string_face);
21473 props = list2 (Qface, face);
21474 if (copy_string)
21475 lisp_string = Fcopy_sequence (lisp_string);
21476 }
21477 if (!NILP (props))
21478 Fadd_text_properties (make_number (0), make_number (len),
21479 props, lisp_string);
21480 }
21481
21482 if (len > 0)
21483 {
21484 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21485 n += len;
21486 }
21487
21488 if (field_width > len)
21489 {
21490 field_width -= len;
21491 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21492 if (!NILP (props))
21493 Fadd_text_properties (make_number (0), make_number (field_width),
21494 props, lisp_string);
21495 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21496 n += field_width;
21497 }
21498
21499 return n;
21500 }
21501
21502
21503 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21504 1, 4, 0,
21505 doc: /* Format a string out of a mode line format specification.
21506 First arg FORMAT specifies the mode line format (see `mode-line-format'
21507 for details) to use.
21508
21509 By default, the format is evaluated for the currently selected window.
21510
21511 Optional second arg FACE specifies the face property to put on all
21512 characters for which no face is specified. The value nil means the
21513 default face. The value t means whatever face the window's mode line
21514 currently uses (either `mode-line' or `mode-line-inactive',
21515 depending on whether the window is the selected window or not).
21516 An integer value means the value string has no text
21517 properties.
21518
21519 Optional third and fourth args WINDOW and BUFFER specify the window
21520 and buffer to use as the context for the formatting (defaults
21521 are the selected window and the WINDOW's buffer). */)
21522 (Lisp_Object format, Lisp_Object face,
21523 Lisp_Object window, Lisp_Object buffer)
21524 {
21525 struct it it;
21526 int len;
21527 struct window *w;
21528 struct buffer *old_buffer = NULL;
21529 int face_id;
21530 int no_props = INTEGERP (face);
21531 ptrdiff_t count = SPECPDL_INDEX ();
21532 Lisp_Object str;
21533 int string_start = 0;
21534
21535 w = decode_any_window (window);
21536 XSETWINDOW (window, w);
21537
21538 if (NILP (buffer))
21539 buffer = w->contents;
21540 CHECK_BUFFER (buffer);
21541
21542 /* Make formatting the modeline a non-op when noninteractive, otherwise
21543 there will be problems later caused by a partially initialized frame. */
21544 if (NILP (format) || noninteractive)
21545 return empty_unibyte_string;
21546
21547 if (no_props)
21548 face = Qnil;
21549
21550 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21551 : EQ (face, Qt) ? (EQ (window, selected_window)
21552 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21553 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21554 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21555 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21556 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21557 : DEFAULT_FACE_ID;
21558
21559 old_buffer = current_buffer;
21560
21561 /* Save things including mode_line_proptrans_alist,
21562 and set that to nil so that we don't alter the outer value. */
21563 record_unwind_protect (unwind_format_mode_line,
21564 format_mode_line_unwind_data
21565 (XFRAME (WINDOW_FRAME (w)),
21566 old_buffer, selected_window, 1));
21567 mode_line_proptrans_alist = Qnil;
21568
21569 Fselect_window (window, Qt);
21570 set_buffer_internal_1 (XBUFFER (buffer));
21571
21572 init_iterator (&it, w, -1, -1, NULL, face_id);
21573
21574 if (no_props)
21575 {
21576 mode_line_target = MODE_LINE_NOPROP;
21577 mode_line_string_face_prop = Qnil;
21578 mode_line_string_list = Qnil;
21579 string_start = MODE_LINE_NOPROP_LEN (0);
21580 }
21581 else
21582 {
21583 mode_line_target = MODE_LINE_STRING;
21584 mode_line_string_list = Qnil;
21585 mode_line_string_face = face;
21586 mode_line_string_face_prop
21587 = NILP (face) ? Qnil : list2 (Qface, face);
21588 }
21589
21590 push_kboard (FRAME_KBOARD (it.f));
21591 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21592 pop_kboard ();
21593
21594 if (no_props)
21595 {
21596 len = MODE_LINE_NOPROP_LEN (string_start);
21597 str = make_string (mode_line_noprop_buf + string_start, len);
21598 }
21599 else
21600 {
21601 mode_line_string_list = Fnreverse (mode_line_string_list);
21602 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21603 empty_unibyte_string);
21604 }
21605
21606 unbind_to (count, Qnil);
21607 return str;
21608 }
21609
21610 /* Write a null-terminated, right justified decimal representation of
21611 the positive integer D to BUF using a minimal field width WIDTH. */
21612
21613 static void
21614 pint2str (register char *buf, register int width, register ptrdiff_t d)
21615 {
21616 register char *p = buf;
21617
21618 if (d <= 0)
21619 *p++ = '0';
21620 else
21621 {
21622 while (d > 0)
21623 {
21624 *p++ = d % 10 + '0';
21625 d /= 10;
21626 }
21627 }
21628
21629 for (width -= (int) (p - buf); width > 0; --width)
21630 *p++ = ' ';
21631 *p-- = '\0';
21632 while (p > buf)
21633 {
21634 d = *buf;
21635 *buf++ = *p;
21636 *p-- = d;
21637 }
21638 }
21639
21640 /* Write a null-terminated, right justified decimal and "human
21641 readable" representation of the nonnegative integer D to BUF using
21642 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21643
21644 static const char power_letter[] =
21645 {
21646 0, /* no letter */
21647 'k', /* kilo */
21648 'M', /* mega */
21649 'G', /* giga */
21650 'T', /* tera */
21651 'P', /* peta */
21652 'E', /* exa */
21653 'Z', /* zetta */
21654 'Y' /* yotta */
21655 };
21656
21657 static void
21658 pint2hrstr (char *buf, int width, ptrdiff_t d)
21659 {
21660 /* We aim to represent the nonnegative integer D as
21661 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21662 ptrdiff_t quotient = d;
21663 int remainder = 0;
21664 /* -1 means: do not use TENTHS. */
21665 int tenths = -1;
21666 int exponent = 0;
21667
21668 /* Length of QUOTIENT.TENTHS as a string. */
21669 int length;
21670
21671 char * psuffix;
21672 char * p;
21673
21674 if (quotient >= 1000)
21675 {
21676 /* Scale to the appropriate EXPONENT. */
21677 do
21678 {
21679 remainder = quotient % 1000;
21680 quotient /= 1000;
21681 exponent++;
21682 }
21683 while (quotient >= 1000);
21684
21685 /* Round to nearest and decide whether to use TENTHS or not. */
21686 if (quotient <= 9)
21687 {
21688 tenths = remainder / 100;
21689 if (remainder % 100 >= 50)
21690 {
21691 if (tenths < 9)
21692 tenths++;
21693 else
21694 {
21695 quotient++;
21696 if (quotient == 10)
21697 tenths = -1;
21698 else
21699 tenths = 0;
21700 }
21701 }
21702 }
21703 else
21704 if (remainder >= 500)
21705 {
21706 if (quotient < 999)
21707 quotient++;
21708 else
21709 {
21710 quotient = 1;
21711 exponent++;
21712 tenths = 0;
21713 }
21714 }
21715 }
21716
21717 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21718 if (tenths == -1 && quotient <= 99)
21719 if (quotient <= 9)
21720 length = 1;
21721 else
21722 length = 2;
21723 else
21724 length = 3;
21725 p = psuffix = buf + max (width, length);
21726
21727 /* Print EXPONENT. */
21728 *psuffix++ = power_letter[exponent];
21729 *psuffix = '\0';
21730
21731 /* Print TENTHS. */
21732 if (tenths >= 0)
21733 {
21734 *--p = '0' + tenths;
21735 *--p = '.';
21736 }
21737
21738 /* Print QUOTIENT. */
21739 do
21740 {
21741 int digit = quotient % 10;
21742 *--p = '0' + digit;
21743 }
21744 while ((quotient /= 10) != 0);
21745
21746 /* Print leading spaces. */
21747 while (buf < p)
21748 *--p = ' ';
21749 }
21750
21751 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21752 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21753 type of CODING_SYSTEM. Return updated pointer into BUF. */
21754
21755 static unsigned char invalid_eol_type[] = "(*invalid*)";
21756
21757 static char *
21758 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21759 {
21760 Lisp_Object val;
21761 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21762 const unsigned char *eol_str;
21763 int eol_str_len;
21764 /* The EOL conversion we are using. */
21765 Lisp_Object eoltype;
21766
21767 val = CODING_SYSTEM_SPEC (coding_system);
21768 eoltype = Qnil;
21769
21770 if (!VECTORP (val)) /* Not yet decided. */
21771 {
21772 *buf++ = multibyte ? '-' : ' ';
21773 if (eol_flag)
21774 eoltype = eol_mnemonic_undecided;
21775 /* Don't mention EOL conversion if it isn't decided. */
21776 }
21777 else
21778 {
21779 Lisp_Object attrs;
21780 Lisp_Object eolvalue;
21781
21782 attrs = AREF (val, 0);
21783 eolvalue = AREF (val, 2);
21784
21785 *buf++ = multibyte
21786 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21787 : ' ';
21788
21789 if (eol_flag)
21790 {
21791 /* The EOL conversion that is normal on this system. */
21792
21793 if (NILP (eolvalue)) /* Not yet decided. */
21794 eoltype = eol_mnemonic_undecided;
21795 else if (VECTORP (eolvalue)) /* Not yet decided. */
21796 eoltype = eol_mnemonic_undecided;
21797 else /* eolvalue is Qunix, Qdos, or Qmac. */
21798 eoltype = (EQ (eolvalue, Qunix)
21799 ? eol_mnemonic_unix
21800 : (EQ (eolvalue, Qdos) == 1
21801 ? eol_mnemonic_dos : eol_mnemonic_mac));
21802 }
21803 }
21804
21805 if (eol_flag)
21806 {
21807 /* Mention the EOL conversion if it is not the usual one. */
21808 if (STRINGP (eoltype))
21809 {
21810 eol_str = SDATA (eoltype);
21811 eol_str_len = SBYTES (eoltype);
21812 }
21813 else if (CHARACTERP (eoltype))
21814 {
21815 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21816 int c = XFASTINT (eoltype);
21817 eol_str_len = CHAR_STRING (c, tmp);
21818 eol_str = tmp;
21819 }
21820 else
21821 {
21822 eol_str = invalid_eol_type;
21823 eol_str_len = sizeof (invalid_eol_type) - 1;
21824 }
21825 memcpy (buf, eol_str, eol_str_len);
21826 buf += eol_str_len;
21827 }
21828
21829 return buf;
21830 }
21831
21832 /* Return a string for the output of a mode line %-spec for window W,
21833 generated by character C. FIELD_WIDTH > 0 means pad the string
21834 returned with spaces to that value. Return a Lisp string in
21835 *STRING if the resulting string is taken from that Lisp string.
21836
21837 Note we operate on the current buffer for most purposes. */
21838
21839 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21840
21841 static const char *
21842 decode_mode_spec (struct window *w, register int c, int field_width,
21843 Lisp_Object *string)
21844 {
21845 Lisp_Object obj;
21846 struct frame *f = XFRAME (WINDOW_FRAME (w));
21847 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21848 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21849 produce strings from numerical values, so limit preposterously
21850 large values of FIELD_WIDTH to avoid overrunning the buffer's
21851 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21852 bytes plus the terminating null. */
21853 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21854 struct buffer *b = current_buffer;
21855
21856 obj = Qnil;
21857 *string = Qnil;
21858
21859 switch (c)
21860 {
21861 case '*':
21862 if (!NILP (BVAR (b, read_only)))
21863 return "%";
21864 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21865 return "*";
21866 return "-";
21867
21868 case '+':
21869 /* This differs from %* only for a modified read-only buffer. */
21870 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21871 return "*";
21872 if (!NILP (BVAR (b, read_only)))
21873 return "%";
21874 return "-";
21875
21876 case '&':
21877 /* This differs from %* in ignoring read-only-ness. */
21878 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21879 return "*";
21880 return "-";
21881
21882 case '%':
21883 return "%";
21884
21885 case '[':
21886 {
21887 int i;
21888 char *p;
21889
21890 if (command_loop_level > 5)
21891 return "[[[... ";
21892 p = decode_mode_spec_buf;
21893 for (i = 0; i < command_loop_level; i++)
21894 *p++ = '[';
21895 *p = 0;
21896 return decode_mode_spec_buf;
21897 }
21898
21899 case ']':
21900 {
21901 int i;
21902 char *p;
21903
21904 if (command_loop_level > 5)
21905 return " ...]]]";
21906 p = decode_mode_spec_buf;
21907 for (i = 0; i < command_loop_level; i++)
21908 *p++ = ']';
21909 *p = 0;
21910 return decode_mode_spec_buf;
21911 }
21912
21913 case '-':
21914 {
21915 register int i;
21916
21917 /* Let lots_of_dashes be a string of infinite length. */
21918 if (mode_line_target == MODE_LINE_NOPROP
21919 || mode_line_target == MODE_LINE_STRING)
21920 return "--";
21921 if (field_width <= 0
21922 || field_width > sizeof (lots_of_dashes))
21923 {
21924 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21925 decode_mode_spec_buf[i] = '-';
21926 decode_mode_spec_buf[i] = '\0';
21927 return decode_mode_spec_buf;
21928 }
21929 else
21930 return lots_of_dashes;
21931 }
21932
21933 case 'b':
21934 obj = BVAR (b, name);
21935 break;
21936
21937 case 'c':
21938 /* %c and %l are ignored in `frame-title-format'.
21939 (In redisplay_internal, the frame title is drawn _before_ the
21940 windows are updated, so the stuff which depends on actual
21941 window contents (such as %l) may fail to render properly, or
21942 even crash emacs.) */
21943 if (mode_line_target == MODE_LINE_TITLE)
21944 return "";
21945 else
21946 {
21947 ptrdiff_t col = current_column ();
21948 w->column_number_displayed = col;
21949 pint2str (decode_mode_spec_buf, width, col);
21950 return decode_mode_spec_buf;
21951 }
21952
21953 case 'e':
21954 #ifndef SYSTEM_MALLOC
21955 {
21956 if (NILP (Vmemory_full))
21957 return "";
21958 else
21959 return "!MEM FULL! ";
21960 }
21961 #else
21962 return "";
21963 #endif
21964
21965 case 'F':
21966 /* %F displays the frame name. */
21967 if (!NILP (f->title))
21968 return SSDATA (f->title);
21969 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21970 return SSDATA (f->name);
21971 return "Emacs";
21972
21973 case 'f':
21974 obj = BVAR (b, filename);
21975 break;
21976
21977 case 'i':
21978 {
21979 ptrdiff_t size = ZV - BEGV;
21980 pint2str (decode_mode_spec_buf, width, size);
21981 return decode_mode_spec_buf;
21982 }
21983
21984 case 'I':
21985 {
21986 ptrdiff_t size = ZV - BEGV;
21987 pint2hrstr (decode_mode_spec_buf, width, size);
21988 return decode_mode_spec_buf;
21989 }
21990
21991 case 'l':
21992 {
21993 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21994 ptrdiff_t topline, nlines, height;
21995 ptrdiff_t junk;
21996
21997 /* %c and %l are ignored in `frame-title-format'. */
21998 if (mode_line_target == MODE_LINE_TITLE)
21999 return "";
22000
22001 startpos = marker_position (w->start);
22002 startpos_byte = marker_byte_position (w->start);
22003 height = WINDOW_TOTAL_LINES (w);
22004
22005 /* If we decided that this buffer isn't suitable for line numbers,
22006 don't forget that too fast. */
22007 if (w->base_line_pos == -1)
22008 goto no_value;
22009
22010 /* If the buffer is very big, don't waste time. */
22011 if (INTEGERP (Vline_number_display_limit)
22012 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22013 {
22014 w->base_line_pos = 0;
22015 w->base_line_number = 0;
22016 goto no_value;
22017 }
22018
22019 if (w->base_line_number > 0
22020 && w->base_line_pos > 0
22021 && w->base_line_pos <= startpos)
22022 {
22023 line = w->base_line_number;
22024 linepos = w->base_line_pos;
22025 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22026 }
22027 else
22028 {
22029 line = 1;
22030 linepos = BUF_BEGV (b);
22031 linepos_byte = BUF_BEGV_BYTE (b);
22032 }
22033
22034 /* Count lines from base line to window start position. */
22035 nlines = display_count_lines (linepos_byte,
22036 startpos_byte,
22037 startpos, &junk);
22038
22039 topline = nlines + line;
22040
22041 /* Determine a new base line, if the old one is too close
22042 or too far away, or if we did not have one.
22043 "Too close" means it's plausible a scroll-down would
22044 go back past it. */
22045 if (startpos == BUF_BEGV (b))
22046 {
22047 w->base_line_number = topline;
22048 w->base_line_pos = BUF_BEGV (b);
22049 }
22050 else if (nlines < height + 25 || nlines > height * 3 + 50
22051 || linepos == BUF_BEGV (b))
22052 {
22053 ptrdiff_t limit = BUF_BEGV (b);
22054 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22055 ptrdiff_t position;
22056 ptrdiff_t distance =
22057 (height * 2 + 30) * line_number_display_limit_width;
22058
22059 if (startpos - distance > limit)
22060 {
22061 limit = startpos - distance;
22062 limit_byte = CHAR_TO_BYTE (limit);
22063 }
22064
22065 nlines = display_count_lines (startpos_byte,
22066 limit_byte,
22067 - (height * 2 + 30),
22068 &position);
22069 /* If we couldn't find the lines we wanted within
22070 line_number_display_limit_width chars per line,
22071 give up on line numbers for this window. */
22072 if (position == limit_byte && limit == startpos - distance)
22073 {
22074 w->base_line_pos = -1;
22075 w->base_line_number = 0;
22076 goto no_value;
22077 }
22078
22079 w->base_line_number = topline - nlines;
22080 w->base_line_pos = BYTE_TO_CHAR (position);
22081 }
22082
22083 /* Now count lines from the start pos to point. */
22084 nlines = display_count_lines (startpos_byte,
22085 PT_BYTE, PT, &junk);
22086
22087 /* Record that we did display the line number. */
22088 line_number_displayed = 1;
22089
22090 /* Make the string to show. */
22091 pint2str (decode_mode_spec_buf, width, topline + nlines);
22092 return decode_mode_spec_buf;
22093 no_value:
22094 {
22095 char* p = decode_mode_spec_buf;
22096 int pad = width - 2;
22097 while (pad-- > 0)
22098 *p++ = ' ';
22099 *p++ = '?';
22100 *p++ = '?';
22101 *p = '\0';
22102 return decode_mode_spec_buf;
22103 }
22104 }
22105 break;
22106
22107 case 'm':
22108 obj = BVAR (b, mode_name);
22109 break;
22110
22111 case 'n':
22112 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22113 return " Narrow";
22114 break;
22115
22116 case 'p':
22117 {
22118 ptrdiff_t pos = marker_position (w->start);
22119 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22120
22121 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22122 {
22123 if (pos <= BUF_BEGV (b))
22124 return "All";
22125 else
22126 return "Bottom";
22127 }
22128 else if (pos <= BUF_BEGV (b))
22129 return "Top";
22130 else
22131 {
22132 if (total > 1000000)
22133 /* Do it differently for a large value, to avoid overflow. */
22134 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22135 else
22136 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22137 /* We can't normally display a 3-digit number,
22138 so get us a 2-digit number that is close. */
22139 if (total == 100)
22140 total = 99;
22141 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22142 return decode_mode_spec_buf;
22143 }
22144 }
22145
22146 /* Display percentage of size above the bottom of the screen. */
22147 case 'P':
22148 {
22149 ptrdiff_t toppos = marker_position (w->start);
22150 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22151 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22152
22153 if (botpos >= BUF_ZV (b))
22154 {
22155 if (toppos <= BUF_BEGV (b))
22156 return "All";
22157 else
22158 return "Bottom";
22159 }
22160 else
22161 {
22162 if (total > 1000000)
22163 /* Do it differently for a large value, to avoid overflow. */
22164 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22165 else
22166 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22167 /* We can't normally display a 3-digit number,
22168 so get us a 2-digit number that is close. */
22169 if (total == 100)
22170 total = 99;
22171 if (toppos <= BUF_BEGV (b))
22172 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22173 else
22174 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22175 return decode_mode_spec_buf;
22176 }
22177 }
22178
22179 case 's':
22180 /* status of process */
22181 obj = Fget_buffer_process (Fcurrent_buffer ());
22182 if (NILP (obj))
22183 return "no process";
22184 #ifndef MSDOS
22185 obj = Fsymbol_name (Fprocess_status (obj));
22186 #endif
22187 break;
22188
22189 case '@':
22190 {
22191 ptrdiff_t count = inhibit_garbage_collection ();
22192 Lisp_Object val = call1 (intern ("file-remote-p"),
22193 BVAR (current_buffer, directory));
22194 unbind_to (count, Qnil);
22195
22196 if (NILP (val))
22197 return "-";
22198 else
22199 return "@";
22200 }
22201
22202 case 'z':
22203 /* coding-system (not including end-of-line format) */
22204 case 'Z':
22205 /* coding-system (including end-of-line type) */
22206 {
22207 int eol_flag = (c == 'Z');
22208 char *p = decode_mode_spec_buf;
22209
22210 if (! FRAME_WINDOW_P (f))
22211 {
22212 /* No need to mention EOL here--the terminal never needs
22213 to do EOL conversion. */
22214 p = decode_mode_spec_coding (CODING_ID_NAME
22215 (FRAME_KEYBOARD_CODING (f)->id),
22216 p, 0);
22217 p = decode_mode_spec_coding (CODING_ID_NAME
22218 (FRAME_TERMINAL_CODING (f)->id),
22219 p, 0);
22220 }
22221 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22222 p, eol_flag);
22223
22224 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22225 #ifdef subprocesses
22226 obj = Fget_buffer_process (Fcurrent_buffer ());
22227 if (PROCESSP (obj))
22228 {
22229 p = decode_mode_spec_coding
22230 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22231 p = decode_mode_spec_coding
22232 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22233 }
22234 #endif /* subprocesses */
22235 #endif /* 0 */
22236 *p = 0;
22237 return decode_mode_spec_buf;
22238 }
22239 }
22240
22241 if (STRINGP (obj))
22242 {
22243 *string = obj;
22244 return SSDATA (obj);
22245 }
22246 else
22247 return "";
22248 }
22249
22250
22251 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22252 means count lines back from START_BYTE. But don't go beyond
22253 LIMIT_BYTE. Return the number of lines thus found (always
22254 nonnegative).
22255
22256 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22257 either the position COUNT lines after/before START_BYTE, if we
22258 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22259 COUNT lines. */
22260
22261 static ptrdiff_t
22262 display_count_lines (ptrdiff_t start_byte,
22263 ptrdiff_t limit_byte, ptrdiff_t count,
22264 ptrdiff_t *byte_pos_ptr)
22265 {
22266 register unsigned char *cursor;
22267 unsigned char *base;
22268
22269 register ptrdiff_t ceiling;
22270 register unsigned char *ceiling_addr;
22271 ptrdiff_t orig_count = count;
22272
22273 /* If we are not in selective display mode,
22274 check only for newlines. */
22275 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22276 && !INTEGERP (BVAR (current_buffer, selective_display)));
22277
22278 if (count > 0)
22279 {
22280 while (start_byte < limit_byte)
22281 {
22282 ceiling = BUFFER_CEILING_OF (start_byte);
22283 ceiling = min (limit_byte - 1, ceiling);
22284 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22285 base = (cursor = BYTE_POS_ADDR (start_byte));
22286
22287 do
22288 {
22289 if (selective_display)
22290 {
22291 while (*cursor != '\n' && *cursor != 015
22292 && ++cursor != ceiling_addr)
22293 continue;
22294 if (cursor == ceiling_addr)
22295 break;
22296 }
22297 else
22298 {
22299 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22300 if (! cursor)
22301 break;
22302 }
22303
22304 cursor++;
22305
22306 if (--count == 0)
22307 {
22308 start_byte += cursor - base;
22309 *byte_pos_ptr = start_byte;
22310 return orig_count;
22311 }
22312 }
22313 while (cursor < ceiling_addr);
22314
22315 start_byte += ceiling_addr - base;
22316 }
22317 }
22318 else
22319 {
22320 while (start_byte > limit_byte)
22321 {
22322 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22323 ceiling = max (limit_byte, ceiling);
22324 ceiling_addr = BYTE_POS_ADDR (ceiling);
22325 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22326 while (1)
22327 {
22328 if (selective_display)
22329 {
22330 while (--cursor >= ceiling_addr
22331 && *cursor != '\n' && *cursor != 015)
22332 continue;
22333 if (cursor < ceiling_addr)
22334 break;
22335 }
22336 else
22337 {
22338 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22339 if (! cursor)
22340 break;
22341 }
22342
22343 if (++count == 0)
22344 {
22345 start_byte += cursor - base + 1;
22346 *byte_pos_ptr = start_byte;
22347 /* When scanning backwards, we should
22348 not count the newline posterior to which we stop. */
22349 return - orig_count - 1;
22350 }
22351 }
22352 start_byte += ceiling_addr - base;
22353 }
22354 }
22355
22356 *byte_pos_ptr = limit_byte;
22357
22358 if (count < 0)
22359 return - orig_count + count;
22360 return orig_count - count;
22361
22362 }
22363
22364
22365 \f
22366 /***********************************************************************
22367 Displaying strings
22368 ***********************************************************************/
22369
22370 /* Display a NUL-terminated string, starting with index START.
22371
22372 If STRING is non-null, display that C string. Otherwise, the Lisp
22373 string LISP_STRING is displayed. There's a case that STRING is
22374 non-null and LISP_STRING is not nil. It means STRING is a string
22375 data of LISP_STRING. In that case, we display LISP_STRING while
22376 ignoring its text properties.
22377
22378 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22379 FACE_STRING. Display STRING or LISP_STRING with the face at
22380 FACE_STRING_POS in FACE_STRING:
22381
22382 Display the string in the environment given by IT, but use the
22383 standard display table, temporarily.
22384
22385 FIELD_WIDTH is the minimum number of output glyphs to produce.
22386 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22387 with spaces. If STRING has more characters, more than FIELD_WIDTH
22388 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22389
22390 PRECISION is the maximum number of characters to output from
22391 STRING. PRECISION < 0 means don't truncate the string.
22392
22393 This is roughly equivalent to printf format specifiers:
22394
22395 FIELD_WIDTH PRECISION PRINTF
22396 ----------------------------------------
22397 -1 -1 %s
22398 -1 10 %.10s
22399 10 -1 %10s
22400 20 10 %20.10s
22401
22402 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22403 display them, and < 0 means obey the current buffer's value of
22404 enable_multibyte_characters.
22405
22406 Value is the number of columns displayed. */
22407
22408 static int
22409 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22410 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22411 int field_width, int precision, int max_x, int multibyte)
22412 {
22413 int hpos_at_start = it->hpos;
22414 int saved_face_id = it->face_id;
22415 struct glyph_row *row = it->glyph_row;
22416 ptrdiff_t it_charpos;
22417
22418 /* Initialize the iterator IT for iteration over STRING beginning
22419 with index START. */
22420 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22421 precision, field_width, multibyte);
22422 if (string && STRINGP (lisp_string))
22423 /* LISP_STRING is the one returned by decode_mode_spec. We should
22424 ignore its text properties. */
22425 it->stop_charpos = it->end_charpos;
22426
22427 /* If displaying STRING, set up the face of the iterator from
22428 FACE_STRING, if that's given. */
22429 if (STRINGP (face_string))
22430 {
22431 ptrdiff_t endptr;
22432 struct face *face;
22433
22434 it->face_id
22435 = face_at_string_position (it->w, face_string, face_string_pos,
22436 0, &endptr, it->base_face_id, 0);
22437 face = FACE_FROM_ID (it->f, it->face_id);
22438 it->face_box_p = face->box != FACE_NO_BOX;
22439 }
22440
22441 /* Set max_x to the maximum allowed X position. Don't let it go
22442 beyond the right edge of the window. */
22443 if (max_x <= 0)
22444 max_x = it->last_visible_x;
22445 else
22446 max_x = min (max_x, it->last_visible_x);
22447
22448 /* Skip over display elements that are not visible. because IT->w is
22449 hscrolled. */
22450 if (it->current_x < it->first_visible_x)
22451 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22452 MOVE_TO_POS | MOVE_TO_X);
22453
22454 row->ascent = it->max_ascent;
22455 row->height = it->max_ascent + it->max_descent;
22456 row->phys_ascent = it->max_phys_ascent;
22457 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22458 row->extra_line_spacing = it->max_extra_line_spacing;
22459
22460 if (STRINGP (it->string))
22461 it_charpos = IT_STRING_CHARPOS (*it);
22462 else
22463 it_charpos = IT_CHARPOS (*it);
22464
22465 /* This condition is for the case that we are called with current_x
22466 past last_visible_x. */
22467 while (it->current_x < max_x)
22468 {
22469 int x_before, x, n_glyphs_before, i, nglyphs;
22470
22471 /* Get the next display element. */
22472 if (!get_next_display_element (it))
22473 break;
22474
22475 /* Produce glyphs. */
22476 x_before = it->current_x;
22477 n_glyphs_before = row->used[TEXT_AREA];
22478 PRODUCE_GLYPHS (it);
22479
22480 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22481 i = 0;
22482 x = x_before;
22483 while (i < nglyphs)
22484 {
22485 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22486
22487 if (it->line_wrap != TRUNCATE
22488 && x + glyph->pixel_width > max_x)
22489 {
22490 /* End of continued line or max_x reached. */
22491 if (CHAR_GLYPH_PADDING_P (*glyph))
22492 {
22493 /* A wide character is unbreakable. */
22494 if (row->reversed_p)
22495 unproduce_glyphs (it, row->used[TEXT_AREA]
22496 - n_glyphs_before);
22497 row->used[TEXT_AREA] = n_glyphs_before;
22498 it->current_x = x_before;
22499 }
22500 else
22501 {
22502 if (row->reversed_p)
22503 unproduce_glyphs (it, row->used[TEXT_AREA]
22504 - (n_glyphs_before + i));
22505 row->used[TEXT_AREA] = n_glyphs_before + i;
22506 it->current_x = x;
22507 }
22508 break;
22509 }
22510 else if (x + glyph->pixel_width >= it->first_visible_x)
22511 {
22512 /* Glyph is at least partially visible. */
22513 ++it->hpos;
22514 if (x < it->first_visible_x)
22515 row->x = x - it->first_visible_x;
22516 }
22517 else
22518 {
22519 /* Glyph is off the left margin of the display area.
22520 Should not happen. */
22521 emacs_abort ();
22522 }
22523
22524 row->ascent = max (row->ascent, it->max_ascent);
22525 row->height = max (row->height, it->max_ascent + it->max_descent);
22526 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22527 row->phys_height = max (row->phys_height,
22528 it->max_phys_ascent + it->max_phys_descent);
22529 row->extra_line_spacing = max (row->extra_line_spacing,
22530 it->max_extra_line_spacing);
22531 x += glyph->pixel_width;
22532 ++i;
22533 }
22534
22535 /* Stop if max_x reached. */
22536 if (i < nglyphs)
22537 break;
22538
22539 /* Stop at line ends. */
22540 if (ITERATOR_AT_END_OF_LINE_P (it))
22541 {
22542 it->continuation_lines_width = 0;
22543 break;
22544 }
22545
22546 set_iterator_to_next (it, 1);
22547 if (STRINGP (it->string))
22548 it_charpos = IT_STRING_CHARPOS (*it);
22549 else
22550 it_charpos = IT_CHARPOS (*it);
22551
22552 /* Stop if truncating at the right edge. */
22553 if (it->line_wrap == TRUNCATE
22554 && it->current_x >= it->last_visible_x)
22555 {
22556 /* Add truncation mark, but don't do it if the line is
22557 truncated at a padding space. */
22558 if (it_charpos < it->string_nchars)
22559 {
22560 if (!FRAME_WINDOW_P (it->f))
22561 {
22562 int ii, n;
22563
22564 if (it->current_x > it->last_visible_x)
22565 {
22566 if (!row->reversed_p)
22567 {
22568 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22569 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22570 break;
22571 }
22572 else
22573 {
22574 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22575 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22576 break;
22577 unproduce_glyphs (it, ii + 1);
22578 ii = row->used[TEXT_AREA] - (ii + 1);
22579 }
22580 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22581 {
22582 row->used[TEXT_AREA] = ii;
22583 produce_special_glyphs (it, IT_TRUNCATION);
22584 }
22585 }
22586 produce_special_glyphs (it, IT_TRUNCATION);
22587 }
22588 row->truncated_on_right_p = 1;
22589 }
22590 break;
22591 }
22592 }
22593
22594 /* Maybe insert a truncation at the left. */
22595 if (it->first_visible_x
22596 && it_charpos > 0)
22597 {
22598 if (!FRAME_WINDOW_P (it->f)
22599 || (row->reversed_p
22600 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22601 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22602 insert_left_trunc_glyphs (it);
22603 row->truncated_on_left_p = 1;
22604 }
22605
22606 it->face_id = saved_face_id;
22607
22608 /* Value is number of columns displayed. */
22609 return it->hpos - hpos_at_start;
22610 }
22611
22612
22613 \f
22614 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22615 appears as an element of LIST or as the car of an element of LIST.
22616 If PROPVAL is a list, compare each element against LIST in that
22617 way, and return 1/2 if any element of PROPVAL is found in LIST.
22618 Otherwise return 0. This function cannot quit.
22619 The return value is 2 if the text is invisible but with an ellipsis
22620 and 1 if it's invisible and without an ellipsis. */
22621
22622 int
22623 invisible_p (register Lisp_Object propval, Lisp_Object list)
22624 {
22625 register Lisp_Object tail, proptail;
22626
22627 for (tail = list; CONSP (tail); tail = XCDR (tail))
22628 {
22629 register Lisp_Object tem;
22630 tem = XCAR (tail);
22631 if (EQ (propval, tem))
22632 return 1;
22633 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22634 return NILP (XCDR (tem)) ? 1 : 2;
22635 }
22636
22637 if (CONSP (propval))
22638 {
22639 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22640 {
22641 Lisp_Object propelt;
22642 propelt = XCAR (proptail);
22643 for (tail = list; CONSP (tail); tail = XCDR (tail))
22644 {
22645 register Lisp_Object tem;
22646 tem = XCAR (tail);
22647 if (EQ (propelt, tem))
22648 return 1;
22649 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22650 return NILP (XCDR (tem)) ? 1 : 2;
22651 }
22652 }
22653 }
22654
22655 return 0;
22656 }
22657
22658 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22659 doc: /* Non-nil if the property makes the text invisible.
22660 POS-OR-PROP can be a marker or number, in which case it is taken to be
22661 a position in the current buffer and the value of the `invisible' property
22662 is checked; or it can be some other value, which is then presumed to be the
22663 value of the `invisible' property of the text of interest.
22664 The non-nil value returned can be t for truly invisible text or something
22665 else if the text is replaced by an ellipsis. */)
22666 (Lisp_Object pos_or_prop)
22667 {
22668 Lisp_Object prop
22669 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22670 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22671 : pos_or_prop);
22672 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22673 return (invis == 0 ? Qnil
22674 : invis == 1 ? Qt
22675 : make_number (invis));
22676 }
22677
22678 /* Calculate a width or height in pixels from a specification using
22679 the following elements:
22680
22681 SPEC ::=
22682 NUM - a (fractional) multiple of the default font width/height
22683 (NUM) - specifies exactly NUM pixels
22684 UNIT - a fixed number of pixels, see below.
22685 ELEMENT - size of a display element in pixels, see below.
22686 (NUM . SPEC) - equals NUM * SPEC
22687 (+ SPEC SPEC ...) - add pixel values
22688 (- SPEC SPEC ...) - subtract pixel values
22689 (- SPEC) - negate pixel value
22690
22691 NUM ::=
22692 INT or FLOAT - a number constant
22693 SYMBOL - use symbol's (buffer local) variable binding.
22694
22695 UNIT ::=
22696 in - pixels per inch *)
22697 mm - pixels per 1/1000 meter *)
22698 cm - pixels per 1/100 meter *)
22699 width - width of current font in pixels.
22700 height - height of current font in pixels.
22701
22702 *) using the ratio(s) defined in display-pixels-per-inch.
22703
22704 ELEMENT ::=
22705
22706 left-fringe - left fringe width in pixels
22707 right-fringe - right fringe width in pixels
22708
22709 left-margin - left margin width in pixels
22710 right-margin - right margin width in pixels
22711
22712 scroll-bar - scroll-bar area width in pixels
22713
22714 Examples:
22715
22716 Pixels corresponding to 5 inches:
22717 (5 . in)
22718
22719 Total width of non-text areas on left side of window (if scroll-bar is on left):
22720 '(space :width (+ left-fringe left-margin scroll-bar))
22721
22722 Align to first text column (in header line):
22723 '(space :align-to 0)
22724
22725 Align to middle of text area minus half the width of variable `my-image'
22726 containing a loaded image:
22727 '(space :align-to (0.5 . (- text my-image)))
22728
22729 Width of left margin minus width of 1 character in the default font:
22730 '(space :width (- left-margin 1))
22731
22732 Width of left margin minus width of 2 characters in the current font:
22733 '(space :width (- left-margin (2 . width)))
22734
22735 Center 1 character over left-margin (in header line):
22736 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22737
22738 Different ways to express width of left fringe plus left margin minus one pixel:
22739 '(space :width (- (+ left-fringe left-margin) (1)))
22740 '(space :width (+ left-fringe left-margin (- (1))))
22741 '(space :width (+ left-fringe left-margin (-1)))
22742
22743 */
22744
22745 static int
22746 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22747 struct font *font, int width_p, int *align_to)
22748 {
22749 double pixels;
22750
22751 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22752 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22753
22754 if (NILP (prop))
22755 return OK_PIXELS (0);
22756
22757 eassert (FRAME_LIVE_P (it->f));
22758
22759 if (SYMBOLP (prop))
22760 {
22761 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22762 {
22763 char *unit = SSDATA (SYMBOL_NAME (prop));
22764
22765 if (unit[0] == 'i' && unit[1] == 'n')
22766 pixels = 1.0;
22767 else if (unit[0] == 'm' && unit[1] == 'm')
22768 pixels = 25.4;
22769 else if (unit[0] == 'c' && unit[1] == 'm')
22770 pixels = 2.54;
22771 else
22772 pixels = 0;
22773 if (pixels > 0)
22774 {
22775 double ppi = (width_p ? FRAME_RES_X (it->f)
22776 : FRAME_RES_Y (it->f));
22777
22778 if (ppi > 0)
22779 return OK_PIXELS (ppi / pixels);
22780 return 0;
22781 }
22782 }
22783
22784 #ifdef HAVE_WINDOW_SYSTEM
22785 if (EQ (prop, Qheight))
22786 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22787 if (EQ (prop, Qwidth))
22788 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22789 #else
22790 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22791 return OK_PIXELS (1);
22792 #endif
22793
22794 if (EQ (prop, Qtext))
22795 return OK_PIXELS (width_p
22796 ? window_box_width (it->w, TEXT_AREA)
22797 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22798
22799 if (align_to && *align_to < 0)
22800 {
22801 *res = 0;
22802 if (EQ (prop, Qleft))
22803 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22804 if (EQ (prop, Qright))
22805 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22806 if (EQ (prop, Qcenter))
22807 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22808 + window_box_width (it->w, TEXT_AREA) / 2);
22809 if (EQ (prop, Qleft_fringe))
22810 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22811 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22812 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22813 if (EQ (prop, Qright_fringe))
22814 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22815 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22816 : window_box_right_offset (it->w, TEXT_AREA));
22817 if (EQ (prop, Qleft_margin))
22818 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22819 if (EQ (prop, Qright_margin))
22820 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22821 if (EQ (prop, Qscroll_bar))
22822 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22823 ? 0
22824 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22825 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22826 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22827 : 0)));
22828 }
22829 else
22830 {
22831 if (EQ (prop, Qleft_fringe))
22832 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22833 if (EQ (prop, Qright_fringe))
22834 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22835 if (EQ (prop, Qleft_margin))
22836 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22837 if (EQ (prop, Qright_margin))
22838 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22839 if (EQ (prop, Qscroll_bar))
22840 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22841 }
22842
22843 prop = buffer_local_value_1 (prop, it->w->contents);
22844 if (EQ (prop, Qunbound))
22845 prop = Qnil;
22846 }
22847
22848 if (INTEGERP (prop) || FLOATP (prop))
22849 {
22850 int base_unit = (width_p
22851 ? FRAME_COLUMN_WIDTH (it->f)
22852 : FRAME_LINE_HEIGHT (it->f));
22853 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22854 }
22855
22856 if (CONSP (prop))
22857 {
22858 Lisp_Object car = XCAR (prop);
22859 Lisp_Object cdr = XCDR (prop);
22860
22861 if (SYMBOLP (car))
22862 {
22863 #ifdef HAVE_WINDOW_SYSTEM
22864 if (FRAME_WINDOW_P (it->f)
22865 && valid_image_p (prop))
22866 {
22867 ptrdiff_t id = lookup_image (it->f, prop);
22868 struct image *img = IMAGE_FROM_ID (it->f, id);
22869
22870 return OK_PIXELS (width_p ? img->width : img->height);
22871 }
22872 #endif
22873 if (EQ (car, Qplus) || EQ (car, Qminus))
22874 {
22875 int first = 1;
22876 double px;
22877
22878 pixels = 0;
22879 while (CONSP (cdr))
22880 {
22881 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22882 font, width_p, align_to))
22883 return 0;
22884 if (first)
22885 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22886 else
22887 pixels += px;
22888 cdr = XCDR (cdr);
22889 }
22890 if (EQ (car, Qminus))
22891 pixels = -pixels;
22892 return OK_PIXELS (pixels);
22893 }
22894
22895 car = buffer_local_value_1 (car, it->w->contents);
22896 if (EQ (car, Qunbound))
22897 car = Qnil;
22898 }
22899
22900 if (INTEGERP (car) || FLOATP (car))
22901 {
22902 double fact;
22903 pixels = XFLOATINT (car);
22904 if (NILP (cdr))
22905 return OK_PIXELS (pixels);
22906 if (calc_pixel_width_or_height (&fact, it, cdr,
22907 font, width_p, align_to))
22908 return OK_PIXELS (pixels * fact);
22909 return 0;
22910 }
22911
22912 return 0;
22913 }
22914
22915 return 0;
22916 }
22917
22918 \f
22919 /***********************************************************************
22920 Glyph Display
22921 ***********************************************************************/
22922
22923 #ifdef HAVE_WINDOW_SYSTEM
22924
22925 #ifdef GLYPH_DEBUG
22926
22927 void
22928 dump_glyph_string (struct glyph_string *s)
22929 {
22930 fprintf (stderr, "glyph string\n");
22931 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22932 s->x, s->y, s->width, s->height);
22933 fprintf (stderr, " ybase = %d\n", s->ybase);
22934 fprintf (stderr, " hl = %d\n", s->hl);
22935 fprintf (stderr, " left overhang = %d, right = %d\n",
22936 s->left_overhang, s->right_overhang);
22937 fprintf (stderr, " nchars = %d\n", s->nchars);
22938 fprintf (stderr, " extends to end of line = %d\n",
22939 s->extends_to_end_of_line_p);
22940 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22941 fprintf (stderr, " bg width = %d\n", s->background_width);
22942 }
22943
22944 #endif /* GLYPH_DEBUG */
22945
22946 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22947 of XChar2b structures for S; it can't be allocated in
22948 init_glyph_string because it must be allocated via `alloca'. W
22949 is the window on which S is drawn. ROW and AREA are the glyph row
22950 and area within the row from which S is constructed. START is the
22951 index of the first glyph structure covered by S. HL is a
22952 face-override for drawing S. */
22953
22954 #ifdef HAVE_NTGUI
22955 #define OPTIONAL_HDC(hdc) HDC hdc,
22956 #define DECLARE_HDC(hdc) HDC hdc;
22957 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22958 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22959 #endif
22960
22961 #ifndef OPTIONAL_HDC
22962 #define OPTIONAL_HDC(hdc)
22963 #define DECLARE_HDC(hdc)
22964 #define ALLOCATE_HDC(hdc, f)
22965 #define RELEASE_HDC(hdc, f)
22966 #endif
22967
22968 static void
22969 init_glyph_string (struct glyph_string *s,
22970 OPTIONAL_HDC (hdc)
22971 XChar2b *char2b, struct window *w, struct glyph_row *row,
22972 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22973 {
22974 memset (s, 0, sizeof *s);
22975 s->w = w;
22976 s->f = XFRAME (w->frame);
22977 #ifdef HAVE_NTGUI
22978 s->hdc = hdc;
22979 #endif
22980 s->display = FRAME_X_DISPLAY (s->f);
22981 s->window = FRAME_X_WINDOW (s->f);
22982 s->char2b = char2b;
22983 s->hl = hl;
22984 s->row = row;
22985 s->area = area;
22986 s->first_glyph = row->glyphs[area] + start;
22987 s->height = row->height;
22988 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22989 s->ybase = s->y + row->ascent;
22990 }
22991
22992
22993 /* Append the list of glyph strings with head H and tail T to the list
22994 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22995
22996 static void
22997 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22998 struct glyph_string *h, struct glyph_string *t)
22999 {
23000 if (h)
23001 {
23002 if (*head)
23003 (*tail)->next = h;
23004 else
23005 *head = h;
23006 h->prev = *tail;
23007 *tail = t;
23008 }
23009 }
23010
23011
23012 /* Prepend the list of glyph strings with head H and tail T to the
23013 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23014 result. */
23015
23016 static void
23017 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23018 struct glyph_string *h, struct glyph_string *t)
23019 {
23020 if (h)
23021 {
23022 if (*head)
23023 (*head)->prev = t;
23024 else
23025 *tail = t;
23026 t->next = *head;
23027 *head = h;
23028 }
23029 }
23030
23031
23032 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23033 Set *HEAD and *TAIL to the resulting list. */
23034
23035 static void
23036 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23037 struct glyph_string *s)
23038 {
23039 s->next = s->prev = NULL;
23040 append_glyph_string_lists (head, tail, s, s);
23041 }
23042
23043
23044 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23045 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23046 make sure that X resources for the face returned are allocated.
23047 Value is a pointer to a realized face that is ready for display if
23048 DISPLAY_P is non-zero. */
23049
23050 static struct face *
23051 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23052 XChar2b *char2b, int display_p)
23053 {
23054 struct face *face = FACE_FROM_ID (f, face_id);
23055 unsigned code = 0;
23056
23057 if (face->font)
23058 {
23059 code = face->font->driver->encode_char (face->font, c);
23060
23061 if (code == FONT_INVALID_CODE)
23062 code = 0;
23063 }
23064 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23065
23066 /* Make sure X resources of the face are allocated. */
23067 #ifdef HAVE_X_WINDOWS
23068 if (display_p)
23069 #endif
23070 {
23071 eassert (face != NULL);
23072 PREPARE_FACE_FOR_DISPLAY (f, face);
23073 }
23074
23075 return face;
23076 }
23077
23078
23079 /* Get face and two-byte form of character glyph GLYPH on frame F.
23080 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23081 a pointer to a realized face that is ready for display. */
23082
23083 static struct face *
23084 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23085 XChar2b *char2b, int *two_byte_p)
23086 {
23087 struct face *face;
23088 unsigned code = 0;
23089
23090 eassert (glyph->type == CHAR_GLYPH);
23091 face = FACE_FROM_ID (f, glyph->face_id);
23092
23093 /* Make sure X resources of the face are allocated. */
23094 eassert (face != NULL);
23095 PREPARE_FACE_FOR_DISPLAY (f, face);
23096
23097 if (two_byte_p)
23098 *two_byte_p = 0;
23099
23100 if (face->font)
23101 {
23102 if (CHAR_BYTE8_P (glyph->u.ch))
23103 code = CHAR_TO_BYTE8 (glyph->u.ch);
23104 else
23105 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23106
23107 if (code == FONT_INVALID_CODE)
23108 code = 0;
23109 }
23110
23111 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23112 return face;
23113 }
23114
23115
23116 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23117 Return 1 if FONT has a glyph for C, otherwise return 0. */
23118
23119 static int
23120 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23121 {
23122 unsigned code;
23123
23124 if (CHAR_BYTE8_P (c))
23125 code = CHAR_TO_BYTE8 (c);
23126 else
23127 code = font->driver->encode_char (font, c);
23128
23129 if (code == FONT_INVALID_CODE)
23130 return 0;
23131 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23132 return 1;
23133 }
23134
23135
23136 /* Fill glyph string S with composition components specified by S->cmp.
23137
23138 BASE_FACE is the base face of the composition.
23139 S->cmp_from is the index of the first component for S.
23140
23141 OVERLAPS non-zero means S should draw the foreground only, and use
23142 its physical height for clipping. See also draw_glyphs.
23143
23144 Value is the index of a component not in S. */
23145
23146 static int
23147 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23148 int overlaps)
23149 {
23150 int i;
23151 /* For all glyphs of this composition, starting at the offset
23152 S->cmp_from, until we reach the end of the definition or encounter a
23153 glyph that requires the different face, add it to S. */
23154 struct face *face;
23155
23156 eassert (s);
23157
23158 s->for_overlaps = overlaps;
23159 s->face = NULL;
23160 s->font = NULL;
23161 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23162 {
23163 int c = COMPOSITION_GLYPH (s->cmp, i);
23164
23165 /* TAB in a composition means display glyphs with padding space
23166 on the left or right. */
23167 if (c != '\t')
23168 {
23169 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23170 -1, Qnil);
23171
23172 face = get_char_face_and_encoding (s->f, c, face_id,
23173 s->char2b + i, 1);
23174 if (face)
23175 {
23176 if (! s->face)
23177 {
23178 s->face = face;
23179 s->font = s->face->font;
23180 }
23181 else if (s->face != face)
23182 break;
23183 }
23184 }
23185 ++s->nchars;
23186 }
23187 s->cmp_to = i;
23188
23189 if (s->face == NULL)
23190 {
23191 s->face = base_face->ascii_face;
23192 s->font = s->face->font;
23193 }
23194
23195 /* All glyph strings for the same composition has the same width,
23196 i.e. the width set for the first component of the composition. */
23197 s->width = s->first_glyph->pixel_width;
23198
23199 /* If the specified font could not be loaded, use the frame's
23200 default font, but record the fact that we couldn't load it in
23201 the glyph string so that we can draw rectangles for the
23202 characters of the glyph string. */
23203 if (s->font == NULL)
23204 {
23205 s->font_not_found_p = 1;
23206 s->font = FRAME_FONT (s->f);
23207 }
23208
23209 /* Adjust base line for subscript/superscript text. */
23210 s->ybase += s->first_glyph->voffset;
23211
23212 /* This glyph string must always be drawn with 16-bit functions. */
23213 s->two_byte_p = 1;
23214
23215 return s->cmp_to;
23216 }
23217
23218 static int
23219 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23220 int start, int end, int overlaps)
23221 {
23222 struct glyph *glyph, *last;
23223 Lisp_Object lgstring;
23224 int i;
23225
23226 s->for_overlaps = overlaps;
23227 glyph = s->row->glyphs[s->area] + start;
23228 last = s->row->glyphs[s->area] + end;
23229 s->cmp_id = glyph->u.cmp.id;
23230 s->cmp_from = glyph->slice.cmp.from;
23231 s->cmp_to = glyph->slice.cmp.to + 1;
23232 s->face = FACE_FROM_ID (s->f, face_id);
23233 lgstring = composition_gstring_from_id (s->cmp_id);
23234 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23235 glyph++;
23236 while (glyph < last
23237 && glyph->u.cmp.automatic
23238 && glyph->u.cmp.id == s->cmp_id
23239 && s->cmp_to == glyph->slice.cmp.from)
23240 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23241
23242 for (i = s->cmp_from; i < s->cmp_to; i++)
23243 {
23244 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23245 unsigned code = LGLYPH_CODE (lglyph);
23246
23247 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23248 }
23249 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23250 return glyph - s->row->glyphs[s->area];
23251 }
23252
23253
23254 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23255 See the comment of fill_glyph_string for arguments.
23256 Value is the index of the first glyph not in S. */
23257
23258
23259 static int
23260 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23261 int start, int end, int overlaps)
23262 {
23263 struct glyph *glyph, *last;
23264 int voffset;
23265
23266 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23267 s->for_overlaps = overlaps;
23268 glyph = s->row->glyphs[s->area] + start;
23269 last = s->row->glyphs[s->area] + end;
23270 voffset = glyph->voffset;
23271 s->face = FACE_FROM_ID (s->f, face_id);
23272 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23273 s->nchars = 1;
23274 s->width = glyph->pixel_width;
23275 glyph++;
23276 while (glyph < last
23277 && glyph->type == GLYPHLESS_GLYPH
23278 && glyph->voffset == voffset
23279 && glyph->face_id == face_id)
23280 {
23281 s->nchars++;
23282 s->width += glyph->pixel_width;
23283 glyph++;
23284 }
23285 s->ybase += voffset;
23286 return glyph - s->row->glyphs[s->area];
23287 }
23288
23289
23290 /* Fill glyph string S from a sequence of character glyphs.
23291
23292 FACE_ID is the face id of the string. START is the index of the
23293 first glyph to consider, END is the index of the last + 1.
23294 OVERLAPS non-zero means S should draw the foreground only, and use
23295 its physical height for clipping. See also draw_glyphs.
23296
23297 Value is the index of the first glyph not in S. */
23298
23299 static int
23300 fill_glyph_string (struct glyph_string *s, int face_id,
23301 int start, int end, int overlaps)
23302 {
23303 struct glyph *glyph, *last;
23304 int voffset;
23305 int glyph_not_available_p;
23306
23307 eassert (s->f == XFRAME (s->w->frame));
23308 eassert (s->nchars == 0);
23309 eassert (start >= 0 && end > start);
23310
23311 s->for_overlaps = overlaps;
23312 glyph = s->row->glyphs[s->area] + start;
23313 last = s->row->glyphs[s->area] + end;
23314 voffset = glyph->voffset;
23315 s->padding_p = glyph->padding_p;
23316 glyph_not_available_p = glyph->glyph_not_available_p;
23317
23318 while (glyph < last
23319 && glyph->type == CHAR_GLYPH
23320 && glyph->voffset == voffset
23321 /* Same face id implies same font, nowadays. */
23322 && glyph->face_id == face_id
23323 && glyph->glyph_not_available_p == glyph_not_available_p)
23324 {
23325 int two_byte_p;
23326
23327 s->face = get_glyph_face_and_encoding (s->f, glyph,
23328 s->char2b + s->nchars,
23329 &two_byte_p);
23330 s->two_byte_p = two_byte_p;
23331 ++s->nchars;
23332 eassert (s->nchars <= end - start);
23333 s->width += glyph->pixel_width;
23334 if (glyph++->padding_p != s->padding_p)
23335 break;
23336 }
23337
23338 s->font = s->face->font;
23339
23340 /* If the specified font could not be loaded, use the frame's font,
23341 but record the fact that we couldn't load it in
23342 S->font_not_found_p so that we can draw rectangles for the
23343 characters of the glyph string. */
23344 if (s->font == NULL || glyph_not_available_p)
23345 {
23346 s->font_not_found_p = 1;
23347 s->font = FRAME_FONT (s->f);
23348 }
23349
23350 /* Adjust base line for subscript/superscript text. */
23351 s->ybase += voffset;
23352
23353 eassert (s->face && s->face->gc);
23354 return glyph - s->row->glyphs[s->area];
23355 }
23356
23357
23358 /* Fill glyph string S from image glyph S->first_glyph. */
23359
23360 static void
23361 fill_image_glyph_string (struct glyph_string *s)
23362 {
23363 eassert (s->first_glyph->type == IMAGE_GLYPH);
23364 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23365 eassert (s->img);
23366 s->slice = s->first_glyph->slice.img;
23367 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23368 s->font = s->face->font;
23369 s->width = s->first_glyph->pixel_width;
23370
23371 /* Adjust base line for subscript/superscript text. */
23372 s->ybase += s->first_glyph->voffset;
23373 }
23374
23375
23376 /* Fill glyph string S from a sequence of stretch glyphs.
23377
23378 START is the index of the first glyph to consider,
23379 END is the index of the last + 1.
23380
23381 Value is the index of the first glyph not in S. */
23382
23383 static int
23384 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23385 {
23386 struct glyph *glyph, *last;
23387 int voffset, face_id;
23388
23389 eassert (s->first_glyph->type == STRETCH_GLYPH);
23390
23391 glyph = s->row->glyphs[s->area] + start;
23392 last = s->row->glyphs[s->area] + end;
23393 face_id = glyph->face_id;
23394 s->face = FACE_FROM_ID (s->f, face_id);
23395 s->font = s->face->font;
23396 s->width = glyph->pixel_width;
23397 s->nchars = 1;
23398 voffset = glyph->voffset;
23399
23400 for (++glyph;
23401 (glyph < last
23402 && glyph->type == STRETCH_GLYPH
23403 && glyph->voffset == voffset
23404 && glyph->face_id == face_id);
23405 ++glyph)
23406 s->width += glyph->pixel_width;
23407
23408 /* Adjust base line for subscript/superscript text. */
23409 s->ybase += voffset;
23410
23411 /* The case that face->gc == 0 is handled when drawing the glyph
23412 string by calling PREPARE_FACE_FOR_DISPLAY. */
23413 eassert (s->face);
23414 return glyph - s->row->glyphs[s->area];
23415 }
23416
23417 static struct font_metrics *
23418 get_per_char_metric (struct font *font, XChar2b *char2b)
23419 {
23420 static struct font_metrics metrics;
23421 unsigned code;
23422
23423 if (! font)
23424 return NULL;
23425 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23426 if (code == FONT_INVALID_CODE)
23427 return NULL;
23428 font->driver->text_extents (font, &code, 1, &metrics);
23429 return &metrics;
23430 }
23431
23432 /* EXPORT for RIF:
23433 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23434 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23435 assumed to be zero. */
23436
23437 void
23438 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23439 {
23440 *left = *right = 0;
23441
23442 if (glyph->type == CHAR_GLYPH)
23443 {
23444 struct face *face;
23445 XChar2b char2b;
23446 struct font_metrics *pcm;
23447
23448 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23449 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23450 {
23451 if (pcm->rbearing > pcm->width)
23452 *right = pcm->rbearing - pcm->width;
23453 if (pcm->lbearing < 0)
23454 *left = -pcm->lbearing;
23455 }
23456 }
23457 else if (glyph->type == COMPOSITE_GLYPH)
23458 {
23459 if (! glyph->u.cmp.automatic)
23460 {
23461 struct composition *cmp = composition_table[glyph->u.cmp.id];
23462
23463 if (cmp->rbearing > cmp->pixel_width)
23464 *right = cmp->rbearing - cmp->pixel_width;
23465 if (cmp->lbearing < 0)
23466 *left = - cmp->lbearing;
23467 }
23468 else
23469 {
23470 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23471 struct font_metrics metrics;
23472
23473 composition_gstring_width (gstring, glyph->slice.cmp.from,
23474 glyph->slice.cmp.to + 1, &metrics);
23475 if (metrics.rbearing > metrics.width)
23476 *right = metrics.rbearing - metrics.width;
23477 if (metrics.lbearing < 0)
23478 *left = - metrics.lbearing;
23479 }
23480 }
23481 }
23482
23483
23484 /* Return the index of the first glyph preceding glyph string S that
23485 is overwritten by S because of S's left overhang. Value is -1
23486 if no glyphs are overwritten. */
23487
23488 static int
23489 left_overwritten (struct glyph_string *s)
23490 {
23491 int k;
23492
23493 if (s->left_overhang)
23494 {
23495 int x = 0, i;
23496 struct glyph *glyphs = s->row->glyphs[s->area];
23497 int first = s->first_glyph - glyphs;
23498
23499 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23500 x -= glyphs[i].pixel_width;
23501
23502 k = i + 1;
23503 }
23504 else
23505 k = -1;
23506
23507 return k;
23508 }
23509
23510
23511 /* Return the index of the first glyph preceding glyph string S that
23512 is overwriting S because of its right overhang. Value is -1 if no
23513 glyph in front of S overwrites S. */
23514
23515 static int
23516 left_overwriting (struct glyph_string *s)
23517 {
23518 int i, k, x;
23519 struct glyph *glyphs = s->row->glyphs[s->area];
23520 int first = s->first_glyph - glyphs;
23521
23522 k = -1;
23523 x = 0;
23524 for (i = first - 1; i >= 0; --i)
23525 {
23526 int left, right;
23527 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23528 if (x + right > 0)
23529 k = i;
23530 x -= glyphs[i].pixel_width;
23531 }
23532
23533 return k;
23534 }
23535
23536
23537 /* Return the index of the last glyph following glyph string S that is
23538 overwritten by S because of S's right overhang. Value is -1 if
23539 no such glyph is found. */
23540
23541 static int
23542 right_overwritten (struct glyph_string *s)
23543 {
23544 int k = -1;
23545
23546 if (s->right_overhang)
23547 {
23548 int x = 0, i;
23549 struct glyph *glyphs = s->row->glyphs[s->area];
23550 int first = (s->first_glyph - glyphs
23551 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23552 int end = s->row->used[s->area];
23553
23554 for (i = first; i < end && s->right_overhang > x; ++i)
23555 x += glyphs[i].pixel_width;
23556
23557 k = i;
23558 }
23559
23560 return k;
23561 }
23562
23563
23564 /* Return the index of the last glyph following glyph string S that
23565 overwrites S because of its left overhang. Value is negative
23566 if no such glyph is found. */
23567
23568 static int
23569 right_overwriting (struct glyph_string *s)
23570 {
23571 int i, k, x;
23572 int end = s->row->used[s->area];
23573 struct glyph *glyphs = s->row->glyphs[s->area];
23574 int first = (s->first_glyph - glyphs
23575 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23576
23577 k = -1;
23578 x = 0;
23579 for (i = first; i < end; ++i)
23580 {
23581 int left, right;
23582 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23583 if (x - left < 0)
23584 k = i;
23585 x += glyphs[i].pixel_width;
23586 }
23587
23588 return k;
23589 }
23590
23591
23592 /* Set background width of glyph string S. START is the index of the
23593 first glyph following S. LAST_X is the right-most x-position + 1
23594 in the drawing area. */
23595
23596 static void
23597 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23598 {
23599 /* If the face of this glyph string has to be drawn to the end of
23600 the drawing area, set S->extends_to_end_of_line_p. */
23601
23602 if (start == s->row->used[s->area]
23603 && s->area == TEXT_AREA
23604 && ((s->row->fill_line_p
23605 && (s->hl == DRAW_NORMAL_TEXT
23606 || s->hl == DRAW_IMAGE_RAISED
23607 || s->hl == DRAW_IMAGE_SUNKEN))
23608 || s->hl == DRAW_MOUSE_FACE))
23609 s->extends_to_end_of_line_p = 1;
23610
23611 /* If S extends its face to the end of the line, set its
23612 background_width to the distance to the right edge of the drawing
23613 area. */
23614 if (s->extends_to_end_of_line_p)
23615 s->background_width = last_x - s->x + 1;
23616 else
23617 s->background_width = s->width;
23618 }
23619
23620
23621 /* Compute overhangs and x-positions for glyph string S and its
23622 predecessors, or successors. X is the starting x-position for S.
23623 BACKWARD_P non-zero means process predecessors. */
23624
23625 static void
23626 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23627 {
23628 if (backward_p)
23629 {
23630 while (s)
23631 {
23632 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23633 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23634 x -= s->width;
23635 s->x = x;
23636 s = s->prev;
23637 }
23638 }
23639 else
23640 {
23641 while (s)
23642 {
23643 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23644 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23645 s->x = x;
23646 x += s->width;
23647 s = s->next;
23648 }
23649 }
23650 }
23651
23652
23653
23654 /* The following macros are only called from draw_glyphs below.
23655 They reference the following parameters of that function directly:
23656 `w', `row', `area', and `overlap_p'
23657 as well as the following local variables:
23658 `s', `f', and `hdc' (in W32) */
23659
23660 #ifdef HAVE_NTGUI
23661 /* On W32, silently add local `hdc' variable to argument list of
23662 init_glyph_string. */
23663 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23664 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23665 #else
23666 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23667 init_glyph_string (s, char2b, w, row, area, start, hl)
23668 #endif
23669
23670 /* Add a glyph string for a stretch glyph to the list of strings
23671 between HEAD and TAIL. START is the index of the stretch glyph in
23672 row area AREA of glyph row ROW. END is the index of the last glyph
23673 in that glyph row area. X is the current output position assigned
23674 to the new glyph string constructed. HL overrides that face of the
23675 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23676 is the right-most x-position of the drawing area. */
23677
23678 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23679 and below -- keep them on one line. */
23680 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23681 do \
23682 { \
23683 s = alloca (sizeof *s); \
23684 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23685 START = fill_stretch_glyph_string (s, START, END); \
23686 append_glyph_string (&HEAD, &TAIL, s); \
23687 s->x = (X); \
23688 } \
23689 while (0)
23690
23691
23692 /* Add a glyph string for an image glyph to the list of strings
23693 between HEAD and TAIL. START is the index of the image glyph in
23694 row area AREA of glyph row ROW. END is the index of the last glyph
23695 in that glyph row area. X is the current output position assigned
23696 to the new glyph string constructed. HL overrides that face of the
23697 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23698 is the right-most x-position of the drawing area. */
23699
23700 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23701 do \
23702 { \
23703 s = alloca (sizeof *s); \
23704 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23705 fill_image_glyph_string (s); \
23706 append_glyph_string (&HEAD, &TAIL, s); \
23707 ++START; \
23708 s->x = (X); \
23709 } \
23710 while (0)
23711
23712
23713 /* Add a glyph string for a sequence of character glyphs to the list
23714 of strings between HEAD and TAIL. START is the index of the first
23715 glyph in row area AREA of glyph row ROW that is part of the new
23716 glyph string. END is the index of the last glyph in that glyph row
23717 area. X is the current output position assigned to the new glyph
23718 string constructed. HL overrides that face of the glyph; e.g. it
23719 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23720 right-most x-position of the drawing area. */
23721
23722 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23723 do \
23724 { \
23725 int face_id; \
23726 XChar2b *char2b; \
23727 \
23728 face_id = (row)->glyphs[area][START].face_id; \
23729 \
23730 s = alloca (sizeof *s); \
23731 char2b = alloca ((END - START) * sizeof *char2b); \
23732 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23733 append_glyph_string (&HEAD, &TAIL, s); \
23734 s->x = (X); \
23735 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23736 } \
23737 while (0)
23738
23739
23740 /* Add a glyph string for a composite sequence to the list of strings
23741 between HEAD and TAIL. START is the index of the first glyph in
23742 row area AREA of glyph row ROW that is part of the new glyph
23743 string. END is the index of the last glyph in that glyph row area.
23744 X is the current output position assigned to the new glyph string
23745 constructed. HL overrides that face of the glyph; e.g. it is
23746 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23747 x-position of the drawing area. */
23748
23749 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23750 do { \
23751 int face_id = (row)->glyphs[area][START].face_id; \
23752 struct face *base_face = FACE_FROM_ID (f, face_id); \
23753 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23754 struct composition *cmp = composition_table[cmp_id]; \
23755 XChar2b *char2b; \
23756 struct glyph_string *first_s = NULL; \
23757 int n; \
23758 \
23759 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23760 \
23761 /* Make glyph_strings for each glyph sequence that is drawable by \
23762 the same face, and append them to HEAD/TAIL. */ \
23763 for (n = 0; n < cmp->glyph_len;) \
23764 { \
23765 s = alloca (sizeof *s); \
23766 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23767 append_glyph_string (&(HEAD), &(TAIL), s); \
23768 s->cmp = cmp; \
23769 s->cmp_from = n; \
23770 s->x = (X); \
23771 if (n == 0) \
23772 first_s = s; \
23773 n = fill_composite_glyph_string (s, base_face, overlaps); \
23774 } \
23775 \
23776 ++START; \
23777 s = first_s; \
23778 } while (0)
23779
23780
23781 /* Add a glyph string for a glyph-string sequence to the list of strings
23782 between HEAD and TAIL. */
23783
23784 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23785 do { \
23786 int face_id; \
23787 XChar2b *char2b; \
23788 Lisp_Object gstring; \
23789 \
23790 face_id = (row)->glyphs[area][START].face_id; \
23791 gstring = (composition_gstring_from_id \
23792 ((row)->glyphs[area][START].u.cmp.id)); \
23793 s = alloca (sizeof *s); \
23794 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23795 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23796 append_glyph_string (&(HEAD), &(TAIL), s); \
23797 s->x = (X); \
23798 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23799 } while (0)
23800
23801
23802 /* Add a glyph string for a sequence of glyphless character's glyphs
23803 to the list of strings between HEAD and TAIL. The meanings of
23804 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23805
23806 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23807 do \
23808 { \
23809 int face_id; \
23810 \
23811 face_id = (row)->glyphs[area][START].face_id; \
23812 \
23813 s = alloca (sizeof *s); \
23814 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23815 append_glyph_string (&HEAD, &TAIL, s); \
23816 s->x = (X); \
23817 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23818 overlaps); \
23819 } \
23820 while (0)
23821
23822
23823 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23824 of AREA of glyph row ROW on window W between indices START and END.
23825 HL overrides the face for drawing glyph strings, e.g. it is
23826 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23827 x-positions of the drawing area.
23828
23829 This is an ugly monster macro construct because we must use alloca
23830 to allocate glyph strings (because draw_glyphs can be called
23831 asynchronously). */
23832
23833 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23834 do \
23835 { \
23836 HEAD = TAIL = NULL; \
23837 while (START < END) \
23838 { \
23839 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23840 switch (first_glyph->type) \
23841 { \
23842 case CHAR_GLYPH: \
23843 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23844 HL, X, LAST_X); \
23845 break; \
23846 \
23847 case COMPOSITE_GLYPH: \
23848 if (first_glyph->u.cmp.automatic) \
23849 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23850 HL, X, LAST_X); \
23851 else \
23852 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23853 HL, X, LAST_X); \
23854 break; \
23855 \
23856 case STRETCH_GLYPH: \
23857 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23858 HL, X, LAST_X); \
23859 break; \
23860 \
23861 case IMAGE_GLYPH: \
23862 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23863 HL, X, LAST_X); \
23864 break; \
23865 \
23866 case GLYPHLESS_GLYPH: \
23867 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23868 HL, X, LAST_X); \
23869 break; \
23870 \
23871 default: \
23872 emacs_abort (); \
23873 } \
23874 \
23875 if (s) \
23876 { \
23877 set_glyph_string_background_width (s, START, LAST_X); \
23878 (X) += s->width; \
23879 } \
23880 } \
23881 } while (0)
23882
23883
23884 /* Draw glyphs between START and END in AREA of ROW on window W,
23885 starting at x-position X. X is relative to AREA in W. HL is a
23886 face-override with the following meaning:
23887
23888 DRAW_NORMAL_TEXT draw normally
23889 DRAW_CURSOR draw in cursor face
23890 DRAW_MOUSE_FACE draw in mouse face.
23891 DRAW_INVERSE_VIDEO draw in mode line face
23892 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23893 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23894
23895 If OVERLAPS is non-zero, draw only the foreground of characters and
23896 clip to the physical height of ROW. Non-zero value also defines
23897 the overlapping part to be drawn:
23898
23899 OVERLAPS_PRED overlap with preceding rows
23900 OVERLAPS_SUCC overlap with succeeding rows
23901 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23902 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23903
23904 Value is the x-position reached, relative to AREA of W. */
23905
23906 static int
23907 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23908 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23909 enum draw_glyphs_face hl, int overlaps)
23910 {
23911 struct glyph_string *head, *tail;
23912 struct glyph_string *s;
23913 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23914 int i, j, x_reached, last_x, area_left = 0;
23915 struct frame *f = XFRAME (WINDOW_FRAME (w));
23916 DECLARE_HDC (hdc);
23917
23918 ALLOCATE_HDC (hdc, f);
23919
23920 /* Let's rather be paranoid than getting a SEGV. */
23921 end = min (end, row->used[area]);
23922 start = clip_to_bounds (0, start, end);
23923
23924 /* Translate X to frame coordinates. Set last_x to the right
23925 end of the drawing area. */
23926 if (row->full_width_p)
23927 {
23928 /* X is relative to the left edge of W, without scroll bars
23929 or fringes. */
23930 area_left = WINDOW_LEFT_EDGE_X (w);
23931 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23932 }
23933 else
23934 {
23935 area_left = window_box_left (w, area);
23936 last_x = area_left + window_box_width (w, area);
23937 }
23938 x += area_left;
23939
23940 /* Build a doubly-linked list of glyph_string structures between
23941 head and tail from what we have to draw. Note that the macro
23942 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23943 the reason we use a separate variable `i'. */
23944 i = start;
23945 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23946 if (tail)
23947 x_reached = tail->x + tail->background_width;
23948 else
23949 x_reached = x;
23950
23951 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23952 the row, redraw some glyphs in front or following the glyph
23953 strings built above. */
23954 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23955 {
23956 struct glyph_string *h, *t;
23957 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23958 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23959 int check_mouse_face = 0;
23960 int dummy_x = 0;
23961
23962 /* If mouse highlighting is on, we may need to draw adjacent
23963 glyphs using mouse-face highlighting. */
23964 if (area == TEXT_AREA && row->mouse_face_p
23965 && hlinfo->mouse_face_beg_row >= 0
23966 && hlinfo->mouse_face_end_row >= 0)
23967 {
23968 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23969
23970 if (row_vpos >= hlinfo->mouse_face_beg_row
23971 && row_vpos <= hlinfo->mouse_face_end_row)
23972 {
23973 check_mouse_face = 1;
23974 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23975 ? hlinfo->mouse_face_beg_col : 0;
23976 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23977 ? hlinfo->mouse_face_end_col
23978 : row->used[TEXT_AREA];
23979 }
23980 }
23981
23982 /* Compute overhangs for all glyph strings. */
23983 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23984 for (s = head; s; s = s->next)
23985 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23986
23987 /* Prepend glyph strings for glyphs in front of the first glyph
23988 string that are overwritten because of the first glyph
23989 string's left overhang. The background of all strings
23990 prepended must be drawn because the first glyph string
23991 draws over it. */
23992 i = left_overwritten (head);
23993 if (i >= 0)
23994 {
23995 enum draw_glyphs_face overlap_hl;
23996
23997 /* If this row contains mouse highlighting, attempt to draw
23998 the overlapped glyphs with the correct highlight. This
23999 code fails if the overlap encompasses more than one glyph
24000 and mouse-highlight spans only some of these glyphs.
24001 However, making it work perfectly involves a lot more
24002 code, and I don't know if the pathological case occurs in
24003 practice, so we'll stick to this for now. --- cyd */
24004 if (check_mouse_face
24005 && mouse_beg_col < start && mouse_end_col > i)
24006 overlap_hl = DRAW_MOUSE_FACE;
24007 else
24008 overlap_hl = DRAW_NORMAL_TEXT;
24009
24010 j = i;
24011 BUILD_GLYPH_STRINGS (j, start, h, t,
24012 overlap_hl, dummy_x, last_x);
24013 start = i;
24014 compute_overhangs_and_x (t, head->x, 1);
24015 prepend_glyph_string_lists (&head, &tail, h, t);
24016 clip_head = head;
24017 }
24018
24019 /* Prepend glyph strings for glyphs in front of the first glyph
24020 string that overwrite that glyph string because of their
24021 right overhang. For these strings, only the foreground must
24022 be drawn, because it draws over the glyph string at `head'.
24023 The background must not be drawn because this would overwrite
24024 right overhangs of preceding glyphs for which no glyph
24025 strings exist. */
24026 i = left_overwriting (head);
24027 if (i >= 0)
24028 {
24029 enum draw_glyphs_face overlap_hl;
24030
24031 if (check_mouse_face
24032 && mouse_beg_col < start && mouse_end_col > i)
24033 overlap_hl = DRAW_MOUSE_FACE;
24034 else
24035 overlap_hl = DRAW_NORMAL_TEXT;
24036
24037 clip_head = head;
24038 BUILD_GLYPH_STRINGS (i, start, h, t,
24039 overlap_hl, dummy_x, last_x);
24040 for (s = h; s; s = s->next)
24041 s->background_filled_p = 1;
24042 compute_overhangs_and_x (t, head->x, 1);
24043 prepend_glyph_string_lists (&head, &tail, h, t);
24044 }
24045
24046 /* Append glyphs strings for glyphs following the last glyph
24047 string tail that are overwritten by tail. The background of
24048 these strings has to be drawn because tail's foreground draws
24049 over it. */
24050 i = right_overwritten (tail);
24051 if (i >= 0)
24052 {
24053 enum draw_glyphs_face overlap_hl;
24054
24055 if (check_mouse_face
24056 && mouse_beg_col < i && mouse_end_col > end)
24057 overlap_hl = DRAW_MOUSE_FACE;
24058 else
24059 overlap_hl = DRAW_NORMAL_TEXT;
24060
24061 BUILD_GLYPH_STRINGS (end, i, h, t,
24062 overlap_hl, x, last_x);
24063 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24064 we don't have `end = i;' here. */
24065 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24066 append_glyph_string_lists (&head, &tail, h, t);
24067 clip_tail = tail;
24068 }
24069
24070 /* Append glyph strings for glyphs following the last glyph
24071 string tail that overwrite tail. The foreground of such
24072 glyphs has to be drawn because it writes into the background
24073 of tail. The background must not be drawn because it could
24074 paint over the foreground of following glyphs. */
24075 i = right_overwriting (tail);
24076 if (i >= 0)
24077 {
24078 enum draw_glyphs_face overlap_hl;
24079 if (check_mouse_face
24080 && mouse_beg_col < i && mouse_end_col > end)
24081 overlap_hl = DRAW_MOUSE_FACE;
24082 else
24083 overlap_hl = DRAW_NORMAL_TEXT;
24084
24085 clip_tail = tail;
24086 i++; /* We must include the Ith glyph. */
24087 BUILD_GLYPH_STRINGS (end, i, h, t,
24088 overlap_hl, x, last_x);
24089 for (s = h; s; s = s->next)
24090 s->background_filled_p = 1;
24091 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24092 append_glyph_string_lists (&head, &tail, h, t);
24093 }
24094 if (clip_head || clip_tail)
24095 for (s = head; s; s = s->next)
24096 {
24097 s->clip_head = clip_head;
24098 s->clip_tail = clip_tail;
24099 }
24100 }
24101
24102 /* Draw all strings. */
24103 for (s = head; s; s = s->next)
24104 FRAME_RIF (f)->draw_glyph_string (s);
24105
24106 #ifndef HAVE_NS
24107 /* When focus a sole frame and move horizontally, this sets on_p to 0
24108 causing a failure to erase prev cursor position. */
24109 if (area == TEXT_AREA
24110 && !row->full_width_p
24111 /* When drawing overlapping rows, only the glyph strings'
24112 foreground is drawn, which doesn't erase a cursor
24113 completely. */
24114 && !overlaps)
24115 {
24116 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24117 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24118 : (tail ? tail->x + tail->background_width : x));
24119 x0 -= area_left;
24120 x1 -= area_left;
24121
24122 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24123 row->y, MATRIX_ROW_BOTTOM_Y (row));
24124 }
24125 #endif
24126
24127 /* Value is the x-position up to which drawn, relative to AREA of W.
24128 This doesn't include parts drawn because of overhangs. */
24129 if (row->full_width_p)
24130 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24131 else
24132 x_reached -= area_left;
24133
24134 RELEASE_HDC (hdc, f);
24135
24136 return x_reached;
24137 }
24138
24139 /* Expand row matrix if too narrow. Don't expand if area
24140 is not present. */
24141
24142 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24143 { \
24144 if (!it->f->fonts_changed \
24145 && (it->glyph_row->glyphs[area] \
24146 < it->glyph_row->glyphs[area + 1])) \
24147 { \
24148 it->w->ncols_scale_factor++; \
24149 it->f->fonts_changed = 1; \
24150 } \
24151 }
24152
24153 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24154 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24155
24156 static void
24157 append_glyph (struct it *it)
24158 {
24159 struct glyph *glyph;
24160 enum glyph_row_area area = it->area;
24161
24162 eassert (it->glyph_row);
24163 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24164
24165 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24166 if (glyph < it->glyph_row->glyphs[area + 1])
24167 {
24168 /* If the glyph row is reversed, we need to prepend the glyph
24169 rather than append it. */
24170 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24171 {
24172 struct glyph *g;
24173
24174 /* Make room for the additional glyph. */
24175 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24176 g[1] = *g;
24177 glyph = it->glyph_row->glyphs[area];
24178 }
24179 glyph->charpos = CHARPOS (it->position);
24180 glyph->object = it->object;
24181 if (it->pixel_width > 0)
24182 {
24183 glyph->pixel_width = it->pixel_width;
24184 glyph->padding_p = 0;
24185 }
24186 else
24187 {
24188 /* Assure at least 1-pixel width. Otherwise, cursor can't
24189 be displayed correctly. */
24190 glyph->pixel_width = 1;
24191 glyph->padding_p = 1;
24192 }
24193 glyph->ascent = it->ascent;
24194 glyph->descent = it->descent;
24195 glyph->voffset = it->voffset;
24196 glyph->type = CHAR_GLYPH;
24197 glyph->avoid_cursor_p = it->avoid_cursor_p;
24198 glyph->multibyte_p = it->multibyte_p;
24199 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24200 {
24201 /* In R2L rows, the left and the right box edges need to be
24202 drawn in reverse direction. */
24203 glyph->right_box_line_p = it->start_of_box_run_p;
24204 glyph->left_box_line_p = it->end_of_box_run_p;
24205 }
24206 else
24207 {
24208 glyph->left_box_line_p = it->start_of_box_run_p;
24209 glyph->right_box_line_p = it->end_of_box_run_p;
24210 }
24211 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24212 || it->phys_descent > it->descent);
24213 glyph->glyph_not_available_p = it->glyph_not_available_p;
24214 glyph->face_id = it->face_id;
24215 glyph->u.ch = it->char_to_display;
24216 glyph->slice.img = null_glyph_slice;
24217 glyph->font_type = FONT_TYPE_UNKNOWN;
24218 if (it->bidi_p)
24219 {
24220 glyph->resolved_level = it->bidi_it.resolved_level;
24221 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24222 emacs_abort ();
24223 glyph->bidi_type = it->bidi_it.type;
24224 }
24225 else
24226 {
24227 glyph->resolved_level = 0;
24228 glyph->bidi_type = UNKNOWN_BT;
24229 }
24230 ++it->glyph_row->used[area];
24231 }
24232 else
24233 IT_EXPAND_MATRIX_WIDTH (it, area);
24234 }
24235
24236 /* Store one glyph for the composition IT->cmp_it.id in
24237 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24238 non-null. */
24239
24240 static void
24241 append_composite_glyph (struct it *it)
24242 {
24243 struct glyph *glyph;
24244 enum glyph_row_area area = it->area;
24245
24246 eassert (it->glyph_row);
24247
24248 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24249 if (glyph < it->glyph_row->glyphs[area + 1])
24250 {
24251 /* If the glyph row is reversed, we need to prepend the glyph
24252 rather than append it. */
24253 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24254 {
24255 struct glyph *g;
24256
24257 /* Make room for the new glyph. */
24258 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24259 g[1] = *g;
24260 glyph = it->glyph_row->glyphs[it->area];
24261 }
24262 glyph->charpos = it->cmp_it.charpos;
24263 glyph->object = it->object;
24264 glyph->pixel_width = it->pixel_width;
24265 glyph->ascent = it->ascent;
24266 glyph->descent = it->descent;
24267 glyph->voffset = it->voffset;
24268 glyph->type = COMPOSITE_GLYPH;
24269 if (it->cmp_it.ch < 0)
24270 {
24271 glyph->u.cmp.automatic = 0;
24272 glyph->u.cmp.id = it->cmp_it.id;
24273 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24274 }
24275 else
24276 {
24277 glyph->u.cmp.automatic = 1;
24278 glyph->u.cmp.id = it->cmp_it.id;
24279 glyph->slice.cmp.from = it->cmp_it.from;
24280 glyph->slice.cmp.to = it->cmp_it.to - 1;
24281 }
24282 glyph->avoid_cursor_p = it->avoid_cursor_p;
24283 glyph->multibyte_p = it->multibyte_p;
24284 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24285 {
24286 /* In R2L rows, the left and the right box edges need to be
24287 drawn in reverse direction. */
24288 glyph->right_box_line_p = it->start_of_box_run_p;
24289 glyph->left_box_line_p = it->end_of_box_run_p;
24290 }
24291 else
24292 {
24293 glyph->left_box_line_p = it->start_of_box_run_p;
24294 glyph->right_box_line_p = it->end_of_box_run_p;
24295 }
24296 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24297 || it->phys_descent > it->descent);
24298 glyph->padding_p = 0;
24299 glyph->glyph_not_available_p = 0;
24300 glyph->face_id = it->face_id;
24301 glyph->font_type = FONT_TYPE_UNKNOWN;
24302 if (it->bidi_p)
24303 {
24304 glyph->resolved_level = it->bidi_it.resolved_level;
24305 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24306 emacs_abort ();
24307 glyph->bidi_type = it->bidi_it.type;
24308 }
24309 ++it->glyph_row->used[area];
24310 }
24311 else
24312 IT_EXPAND_MATRIX_WIDTH (it, area);
24313 }
24314
24315
24316 /* Change IT->ascent and IT->height according to the setting of
24317 IT->voffset. */
24318
24319 static void
24320 take_vertical_position_into_account (struct it *it)
24321 {
24322 if (it->voffset)
24323 {
24324 if (it->voffset < 0)
24325 /* Increase the ascent so that we can display the text higher
24326 in the line. */
24327 it->ascent -= it->voffset;
24328 else
24329 /* Increase the descent so that we can display the text lower
24330 in the line. */
24331 it->descent += it->voffset;
24332 }
24333 }
24334
24335
24336 /* Produce glyphs/get display metrics for the image IT is loaded with.
24337 See the description of struct display_iterator in dispextern.h for
24338 an overview of struct display_iterator. */
24339
24340 static void
24341 produce_image_glyph (struct it *it)
24342 {
24343 struct image *img;
24344 struct face *face;
24345 int glyph_ascent, crop;
24346 struct glyph_slice slice;
24347
24348 eassert (it->what == IT_IMAGE);
24349
24350 face = FACE_FROM_ID (it->f, it->face_id);
24351 eassert (face);
24352 /* Make sure X resources of the face is loaded. */
24353 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24354
24355 if (it->image_id < 0)
24356 {
24357 /* Fringe bitmap. */
24358 it->ascent = it->phys_ascent = 0;
24359 it->descent = it->phys_descent = 0;
24360 it->pixel_width = 0;
24361 it->nglyphs = 0;
24362 return;
24363 }
24364
24365 img = IMAGE_FROM_ID (it->f, it->image_id);
24366 eassert (img);
24367 /* Make sure X resources of the image is loaded. */
24368 prepare_image_for_display (it->f, img);
24369
24370 slice.x = slice.y = 0;
24371 slice.width = img->width;
24372 slice.height = img->height;
24373
24374 if (INTEGERP (it->slice.x))
24375 slice.x = XINT (it->slice.x);
24376 else if (FLOATP (it->slice.x))
24377 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24378
24379 if (INTEGERP (it->slice.y))
24380 slice.y = XINT (it->slice.y);
24381 else if (FLOATP (it->slice.y))
24382 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24383
24384 if (INTEGERP (it->slice.width))
24385 slice.width = XINT (it->slice.width);
24386 else if (FLOATP (it->slice.width))
24387 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24388
24389 if (INTEGERP (it->slice.height))
24390 slice.height = XINT (it->slice.height);
24391 else if (FLOATP (it->slice.height))
24392 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24393
24394 if (slice.x >= img->width)
24395 slice.x = img->width;
24396 if (slice.y >= img->height)
24397 slice.y = img->height;
24398 if (slice.x + slice.width >= img->width)
24399 slice.width = img->width - slice.x;
24400 if (slice.y + slice.height > img->height)
24401 slice.height = img->height - slice.y;
24402
24403 if (slice.width == 0 || slice.height == 0)
24404 return;
24405
24406 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24407
24408 it->descent = slice.height - glyph_ascent;
24409 if (slice.y == 0)
24410 it->descent += img->vmargin;
24411 if (slice.y + slice.height == img->height)
24412 it->descent += img->vmargin;
24413 it->phys_descent = it->descent;
24414
24415 it->pixel_width = slice.width;
24416 if (slice.x == 0)
24417 it->pixel_width += img->hmargin;
24418 if (slice.x + slice.width == img->width)
24419 it->pixel_width += img->hmargin;
24420
24421 /* It's quite possible for images to have an ascent greater than
24422 their height, so don't get confused in that case. */
24423 if (it->descent < 0)
24424 it->descent = 0;
24425
24426 it->nglyphs = 1;
24427
24428 if (face->box != FACE_NO_BOX)
24429 {
24430 if (face->box_line_width > 0)
24431 {
24432 if (slice.y == 0)
24433 it->ascent += face->box_line_width;
24434 if (slice.y + slice.height == img->height)
24435 it->descent += face->box_line_width;
24436 }
24437
24438 if (it->start_of_box_run_p && slice.x == 0)
24439 it->pixel_width += eabs (face->box_line_width);
24440 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24441 it->pixel_width += eabs (face->box_line_width);
24442 }
24443
24444 take_vertical_position_into_account (it);
24445
24446 /* Automatically crop wide image glyphs at right edge so we can
24447 draw the cursor on same display row. */
24448 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24449 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24450 {
24451 it->pixel_width -= crop;
24452 slice.width -= crop;
24453 }
24454
24455 if (it->glyph_row)
24456 {
24457 struct glyph *glyph;
24458 enum glyph_row_area area = it->area;
24459
24460 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24461 if (glyph < it->glyph_row->glyphs[area + 1])
24462 {
24463 glyph->charpos = CHARPOS (it->position);
24464 glyph->object = it->object;
24465 glyph->pixel_width = it->pixel_width;
24466 glyph->ascent = glyph_ascent;
24467 glyph->descent = it->descent;
24468 glyph->voffset = it->voffset;
24469 glyph->type = IMAGE_GLYPH;
24470 glyph->avoid_cursor_p = it->avoid_cursor_p;
24471 glyph->multibyte_p = it->multibyte_p;
24472 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24473 {
24474 /* In R2L rows, the left and the right box edges need to be
24475 drawn in reverse direction. */
24476 glyph->right_box_line_p = it->start_of_box_run_p;
24477 glyph->left_box_line_p = it->end_of_box_run_p;
24478 }
24479 else
24480 {
24481 glyph->left_box_line_p = it->start_of_box_run_p;
24482 glyph->right_box_line_p = it->end_of_box_run_p;
24483 }
24484 glyph->overlaps_vertically_p = 0;
24485 glyph->padding_p = 0;
24486 glyph->glyph_not_available_p = 0;
24487 glyph->face_id = it->face_id;
24488 glyph->u.img_id = img->id;
24489 glyph->slice.img = slice;
24490 glyph->font_type = FONT_TYPE_UNKNOWN;
24491 if (it->bidi_p)
24492 {
24493 glyph->resolved_level = it->bidi_it.resolved_level;
24494 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24495 emacs_abort ();
24496 glyph->bidi_type = it->bidi_it.type;
24497 }
24498 ++it->glyph_row->used[area];
24499 }
24500 else
24501 IT_EXPAND_MATRIX_WIDTH (it, area);
24502 }
24503 }
24504
24505
24506 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24507 of the glyph, WIDTH and HEIGHT are the width and height of the
24508 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24509
24510 static void
24511 append_stretch_glyph (struct it *it, Lisp_Object object,
24512 int width, int height, int ascent)
24513 {
24514 struct glyph *glyph;
24515 enum glyph_row_area area = it->area;
24516
24517 eassert (ascent >= 0 && ascent <= height);
24518
24519 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24520 if (glyph < it->glyph_row->glyphs[area + 1])
24521 {
24522 /* If the glyph row is reversed, we need to prepend the glyph
24523 rather than append it. */
24524 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24525 {
24526 struct glyph *g;
24527
24528 /* Make room for the additional glyph. */
24529 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24530 g[1] = *g;
24531 glyph = it->glyph_row->glyphs[area];
24532 }
24533 glyph->charpos = CHARPOS (it->position);
24534 glyph->object = object;
24535 glyph->pixel_width = width;
24536 glyph->ascent = ascent;
24537 glyph->descent = height - ascent;
24538 glyph->voffset = it->voffset;
24539 glyph->type = STRETCH_GLYPH;
24540 glyph->avoid_cursor_p = it->avoid_cursor_p;
24541 glyph->multibyte_p = it->multibyte_p;
24542 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24543 {
24544 /* In R2L rows, the left and the right box edges need to be
24545 drawn in reverse direction. */
24546 glyph->right_box_line_p = it->start_of_box_run_p;
24547 glyph->left_box_line_p = it->end_of_box_run_p;
24548 }
24549 else
24550 {
24551 glyph->left_box_line_p = it->start_of_box_run_p;
24552 glyph->right_box_line_p = it->end_of_box_run_p;
24553 }
24554 glyph->overlaps_vertically_p = 0;
24555 glyph->padding_p = 0;
24556 glyph->glyph_not_available_p = 0;
24557 glyph->face_id = it->face_id;
24558 glyph->u.stretch.ascent = ascent;
24559 glyph->u.stretch.height = height;
24560 glyph->slice.img = null_glyph_slice;
24561 glyph->font_type = FONT_TYPE_UNKNOWN;
24562 if (it->bidi_p)
24563 {
24564 glyph->resolved_level = it->bidi_it.resolved_level;
24565 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24566 emacs_abort ();
24567 glyph->bidi_type = it->bidi_it.type;
24568 }
24569 else
24570 {
24571 glyph->resolved_level = 0;
24572 glyph->bidi_type = UNKNOWN_BT;
24573 }
24574 ++it->glyph_row->used[area];
24575 }
24576 else
24577 IT_EXPAND_MATRIX_WIDTH (it, area);
24578 }
24579
24580 #endif /* HAVE_WINDOW_SYSTEM */
24581
24582 /* Produce a stretch glyph for iterator IT. IT->object is the value
24583 of the glyph property displayed. The value must be a list
24584 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24585 being recognized:
24586
24587 1. `:width WIDTH' specifies that the space should be WIDTH *
24588 canonical char width wide. WIDTH may be an integer or floating
24589 point number.
24590
24591 2. `:relative-width FACTOR' specifies that the width of the stretch
24592 should be computed from the width of the first character having the
24593 `glyph' property, and should be FACTOR times that width.
24594
24595 3. `:align-to HPOS' specifies that the space should be wide enough
24596 to reach HPOS, a value in canonical character units.
24597
24598 Exactly one of the above pairs must be present.
24599
24600 4. `:height HEIGHT' specifies that the height of the stretch produced
24601 should be HEIGHT, measured in canonical character units.
24602
24603 5. `:relative-height FACTOR' specifies that the height of the
24604 stretch should be FACTOR times the height of the characters having
24605 the glyph property.
24606
24607 Either none or exactly one of 4 or 5 must be present.
24608
24609 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24610 of the stretch should be used for the ascent of the stretch.
24611 ASCENT must be in the range 0 <= ASCENT <= 100. */
24612
24613 void
24614 produce_stretch_glyph (struct it *it)
24615 {
24616 /* (space :width WIDTH :height HEIGHT ...) */
24617 Lisp_Object prop, plist;
24618 int width = 0, height = 0, align_to = -1;
24619 int zero_width_ok_p = 0;
24620 double tem;
24621 struct font *font = NULL;
24622
24623 #ifdef HAVE_WINDOW_SYSTEM
24624 int ascent = 0;
24625 int zero_height_ok_p = 0;
24626
24627 if (FRAME_WINDOW_P (it->f))
24628 {
24629 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24630 font = face->font ? face->font : FRAME_FONT (it->f);
24631 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24632 }
24633 #endif
24634
24635 /* List should start with `space'. */
24636 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24637 plist = XCDR (it->object);
24638
24639 /* Compute the width of the stretch. */
24640 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24641 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24642 {
24643 /* Absolute width `:width WIDTH' specified and valid. */
24644 zero_width_ok_p = 1;
24645 width = (int)tem;
24646 }
24647 #ifdef HAVE_WINDOW_SYSTEM
24648 else if (FRAME_WINDOW_P (it->f)
24649 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24650 {
24651 /* Relative width `:relative-width FACTOR' specified and valid.
24652 Compute the width of the characters having the `glyph'
24653 property. */
24654 struct it it2;
24655 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24656
24657 it2 = *it;
24658 if (it->multibyte_p)
24659 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24660 else
24661 {
24662 it2.c = it2.char_to_display = *p, it2.len = 1;
24663 if (! ASCII_CHAR_P (it2.c))
24664 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24665 }
24666
24667 it2.glyph_row = NULL;
24668 it2.what = IT_CHARACTER;
24669 x_produce_glyphs (&it2);
24670 width = NUMVAL (prop) * it2.pixel_width;
24671 }
24672 #endif /* HAVE_WINDOW_SYSTEM */
24673 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24674 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24675 {
24676 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24677 align_to = (align_to < 0
24678 ? 0
24679 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24680 else if (align_to < 0)
24681 align_to = window_box_left_offset (it->w, TEXT_AREA);
24682 width = max (0, (int)tem + align_to - it->current_x);
24683 zero_width_ok_p = 1;
24684 }
24685 else
24686 /* Nothing specified -> width defaults to canonical char width. */
24687 width = FRAME_COLUMN_WIDTH (it->f);
24688
24689 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24690 width = 1;
24691
24692 #ifdef HAVE_WINDOW_SYSTEM
24693 /* Compute height. */
24694 if (FRAME_WINDOW_P (it->f))
24695 {
24696 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24697 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24698 {
24699 height = (int)tem;
24700 zero_height_ok_p = 1;
24701 }
24702 else if (prop = Fplist_get (plist, QCrelative_height),
24703 NUMVAL (prop) > 0)
24704 height = FONT_HEIGHT (font) * NUMVAL (prop);
24705 else
24706 height = FONT_HEIGHT (font);
24707
24708 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24709 height = 1;
24710
24711 /* Compute percentage of height used for ascent. If
24712 `:ascent ASCENT' is present and valid, use that. Otherwise,
24713 derive the ascent from the font in use. */
24714 if (prop = Fplist_get (plist, QCascent),
24715 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24716 ascent = height * NUMVAL (prop) / 100.0;
24717 else if (!NILP (prop)
24718 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24719 ascent = min (max (0, (int)tem), height);
24720 else
24721 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24722 }
24723 else
24724 #endif /* HAVE_WINDOW_SYSTEM */
24725 height = 1;
24726
24727 if (width > 0 && it->line_wrap != TRUNCATE
24728 && it->current_x + width > it->last_visible_x)
24729 {
24730 width = it->last_visible_x - it->current_x;
24731 #ifdef HAVE_WINDOW_SYSTEM
24732 /* Subtract one more pixel from the stretch width, but only on
24733 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24734 width -= FRAME_WINDOW_P (it->f);
24735 #endif
24736 }
24737
24738 if (width > 0 && height > 0 && it->glyph_row)
24739 {
24740 Lisp_Object o_object = it->object;
24741 Lisp_Object object = it->stack[it->sp - 1].string;
24742 int n = width;
24743
24744 if (!STRINGP (object))
24745 object = it->w->contents;
24746 #ifdef HAVE_WINDOW_SYSTEM
24747 if (FRAME_WINDOW_P (it->f))
24748 append_stretch_glyph (it, object, width, height, ascent);
24749 else
24750 #endif
24751 {
24752 it->object = object;
24753 it->char_to_display = ' ';
24754 it->pixel_width = it->len = 1;
24755 while (n--)
24756 tty_append_glyph (it);
24757 it->object = o_object;
24758 }
24759 }
24760
24761 it->pixel_width = width;
24762 #ifdef HAVE_WINDOW_SYSTEM
24763 if (FRAME_WINDOW_P (it->f))
24764 {
24765 it->ascent = it->phys_ascent = ascent;
24766 it->descent = it->phys_descent = height - it->ascent;
24767 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24768 take_vertical_position_into_account (it);
24769 }
24770 else
24771 #endif
24772 it->nglyphs = width;
24773 }
24774
24775 /* Get information about special display element WHAT in an
24776 environment described by IT. WHAT is one of IT_TRUNCATION or
24777 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24778 non-null glyph_row member. This function ensures that fields like
24779 face_id, c, len of IT are left untouched. */
24780
24781 static void
24782 produce_special_glyphs (struct it *it, enum display_element_type what)
24783 {
24784 struct it temp_it;
24785 Lisp_Object gc;
24786 GLYPH glyph;
24787
24788 temp_it = *it;
24789 temp_it.object = make_number (0);
24790 memset (&temp_it.current, 0, sizeof temp_it.current);
24791
24792 if (what == IT_CONTINUATION)
24793 {
24794 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24795 if (it->bidi_it.paragraph_dir == R2L)
24796 SET_GLYPH_FROM_CHAR (glyph, '/');
24797 else
24798 SET_GLYPH_FROM_CHAR (glyph, '\\');
24799 if (it->dp
24800 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24801 {
24802 /* FIXME: Should we mirror GC for R2L lines? */
24803 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24804 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24805 }
24806 }
24807 else if (what == IT_TRUNCATION)
24808 {
24809 /* Truncation glyph. */
24810 SET_GLYPH_FROM_CHAR (glyph, '$');
24811 if (it->dp
24812 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24813 {
24814 /* FIXME: Should we mirror GC for R2L lines? */
24815 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24816 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24817 }
24818 }
24819 else
24820 emacs_abort ();
24821
24822 #ifdef HAVE_WINDOW_SYSTEM
24823 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24824 is turned off, we precede the truncation/continuation glyphs by a
24825 stretch glyph whose width is computed such that these special
24826 glyphs are aligned at the window margin, even when very different
24827 fonts are used in different glyph rows. */
24828 if (FRAME_WINDOW_P (temp_it.f)
24829 /* init_iterator calls this with it->glyph_row == NULL, and it
24830 wants only the pixel width of the truncation/continuation
24831 glyphs. */
24832 && temp_it.glyph_row
24833 /* insert_left_trunc_glyphs calls us at the beginning of the
24834 row, and it has its own calculation of the stretch glyph
24835 width. */
24836 && temp_it.glyph_row->used[TEXT_AREA] > 0
24837 && (temp_it.glyph_row->reversed_p
24838 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24839 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24840 {
24841 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24842
24843 if (stretch_width > 0)
24844 {
24845 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24846 struct font *font =
24847 face->font ? face->font : FRAME_FONT (temp_it.f);
24848 int stretch_ascent =
24849 (((temp_it.ascent + temp_it.descent)
24850 * FONT_BASE (font)) / FONT_HEIGHT (font));
24851
24852 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24853 temp_it.ascent + temp_it.descent,
24854 stretch_ascent);
24855 }
24856 }
24857 #endif
24858
24859 temp_it.dp = NULL;
24860 temp_it.what = IT_CHARACTER;
24861 temp_it.len = 1;
24862 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24863 temp_it.face_id = GLYPH_FACE (glyph);
24864 temp_it.len = CHAR_BYTES (temp_it.c);
24865
24866 PRODUCE_GLYPHS (&temp_it);
24867 it->pixel_width = temp_it.pixel_width;
24868 it->nglyphs = temp_it.pixel_width;
24869 }
24870
24871 #ifdef HAVE_WINDOW_SYSTEM
24872
24873 /* Calculate line-height and line-spacing properties.
24874 An integer value specifies explicit pixel value.
24875 A float value specifies relative value to current face height.
24876 A cons (float . face-name) specifies relative value to
24877 height of specified face font.
24878
24879 Returns height in pixels, or nil. */
24880
24881
24882 static Lisp_Object
24883 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24884 int boff, int override)
24885 {
24886 Lisp_Object face_name = Qnil;
24887 int ascent, descent, height;
24888
24889 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24890 return val;
24891
24892 if (CONSP (val))
24893 {
24894 face_name = XCAR (val);
24895 val = XCDR (val);
24896 if (!NUMBERP (val))
24897 val = make_number (1);
24898 if (NILP (face_name))
24899 {
24900 height = it->ascent + it->descent;
24901 goto scale;
24902 }
24903 }
24904
24905 if (NILP (face_name))
24906 {
24907 font = FRAME_FONT (it->f);
24908 boff = FRAME_BASELINE_OFFSET (it->f);
24909 }
24910 else if (EQ (face_name, Qt))
24911 {
24912 override = 0;
24913 }
24914 else
24915 {
24916 int face_id;
24917 struct face *face;
24918
24919 face_id = lookup_named_face (it->f, face_name, 0);
24920 if (face_id < 0)
24921 return make_number (-1);
24922
24923 face = FACE_FROM_ID (it->f, face_id);
24924 font = face->font;
24925 if (font == NULL)
24926 return make_number (-1);
24927 boff = font->baseline_offset;
24928 if (font->vertical_centering)
24929 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24930 }
24931
24932 ascent = FONT_BASE (font) + boff;
24933 descent = FONT_DESCENT (font) - boff;
24934
24935 if (override)
24936 {
24937 it->override_ascent = ascent;
24938 it->override_descent = descent;
24939 it->override_boff = boff;
24940 }
24941
24942 height = ascent + descent;
24943
24944 scale:
24945 if (FLOATP (val))
24946 height = (int)(XFLOAT_DATA (val) * height);
24947 else if (INTEGERP (val))
24948 height *= XINT (val);
24949
24950 return make_number (height);
24951 }
24952
24953
24954 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24955 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24956 and only if this is for a character for which no font was found.
24957
24958 If the display method (it->glyphless_method) is
24959 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24960 length of the acronym or the hexadecimal string, UPPER_XOFF and
24961 UPPER_YOFF are pixel offsets for the upper part of the string,
24962 LOWER_XOFF and LOWER_YOFF are for the lower part.
24963
24964 For the other display methods, LEN through LOWER_YOFF are zero. */
24965
24966 static void
24967 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24968 short upper_xoff, short upper_yoff,
24969 short lower_xoff, short lower_yoff)
24970 {
24971 struct glyph *glyph;
24972 enum glyph_row_area area = it->area;
24973
24974 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24975 if (glyph < it->glyph_row->glyphs[area + 1])
24976 {
24977 /* If the glyph row is reversed, we need to prepend the glyph
24978 rather than append it. */
24979 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24980 {
24981 struct glyph *g;
24982
24983 /* Make room for the additional glyph. */
24984 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24985 g[1] = *g;
24986 glyph = it->glyph_row->glyphs[area];
24987 }
24988 glyph->charpos = CHARPOS (it->position);
24989 glyph->object = it->object;
24990 glyph->pixel_width = it->pixel_width;
24991 glyph->ascent = it->ascent;
24992 glyph->descent = it->descent;
24993 glyph->voffset = it->voffset;
24994 glyph->type = GLYPHLESS_GLYPH;
24995 glyph->u.glyphless.method = it->glyphless_method;
24996 glyph->u.glyphless.for_no_font = for_no_font;
24997 glyph->u.glyphless.len = len;
24998 glyph->u.glyphless.ch = it->c;
24999 glyph->slice.glyphless.upper_xoff = upper_xoff;
25000 glyph->slice.glyphless.upper_yoff = upper_yoff;
25001 glyph->slice.glyphless.lower_xoff = lower_xoff;
25002 glyph->slice.glyphless.lower_yoff = lower_yoff;
25003 glyph->avoid_cursor_p = it->avoid_cursor_p;
25004 glyph->multibyte_p = it->multibyte_p;
25005 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25006 {
25007 /* In R2L rows, the left and the right box edges need to be
25008 drawn in reverse direction. */
25009 glyph->right_box_line_p = it->start_of_box_run_p;
25010 glyph->left_box_line_p = it->end_of_box_run_p;
25011 }
25012 else
25013 {
25014 glyph->left_box_line_p = it->start_of_box_run_p;
25015 glyph->right_box_line_p = it->end_of_box_run_p;
25016 }
25017 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25018 || it->phys_descent > it->descent);
25019 glyph->padding_p = 0;
25020 glyph->glyph_not_available_p = 0;
25021 glyph->face_id = face_id;
25022 glyph->font_type = FONT_TYPE_UNKNOWN;
25023 if (it->bidi_p)
25024 {
25025 glyph->resolved_level = it->bidi_it.resolved_level;
25026 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25027 emacs_abort ();
25028 glyph->bidi_type = it->bidi_it.type;
25029 }
25030 ++it->glyph_row->used[area];
25031 }
25032 else
25033 IT_EXPAND_MATRIX_WIDTH (it, area);
25034 }
25035
25036
25037 /* Produce a glyph for a glyphless character for iterator IT.
25038 IT->glyphless_method specifies which method to use for displaying
25039 the character. See the description of enum
25040 glyphless_display_method in dispextern.h for the detail.
25041
25042 FOR_NO_FONT is nonzero if and only if this is for a character for
25043 which no font was found. ACRONYM, if non-nil, is an acronym string
25044 for the character. */
25045
25046 static void
25047 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25048 {
25049 int face_id;
25050 struct face *face;
25051 struct font *font;
25052 int base_width, base_height, width, height;
25053 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25054 int len;
25055
25056 /* Get the metrics of the base font. We always refer to the current
25057 ASCII face. */
25058 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25059 font = face->font ? face->font : FRAME_FONT (it->f);
25060 it->ascent = FONT_BASE (font) + font->baseline_offset;
25061 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25062 base_height = it->ascent + it->descent;
25063 base_width = font->average_width;
25064
25065 face_id = merge_glyphless_glyph_face (it);
25066
25067 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25068 {
25069 it->pixel_width = THIN_SPACE_WIDTH;
25070 len = 0;
25071 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25072 }
25073 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25074 {
25075 width = CHAR_WIDTH (it->c);
25076 if (width == 0)
25077 width = 1;
25078 else if (width > 4)
25079 width = 4;
25080 it->pixel_width = base_width * width;
25081 len = 0;
25082 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25083 }
25084 else
25085 {
25086 char buf[7];
25087 const char *str;
25088 unsigned int code[6];
25089 int upper_len;
25090 int ascent, descent;
25091 struct font_metrics metrics_upper, metrics_lower;
25092
25093 face = FACE_FROM_ID (it->f, face_id);
25094 font = face->font ? face->font : FRAME_FONT (it->f);
25095 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25096
25097 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25098 {
25099 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25100 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25101 if (CONSP (acronym))
25102 acronym = XCAR (acronym);
25103 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25104 }
25105 else
25106 {
25107 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25108 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25109 str = buf;
25110 }
25111 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25112 code[len] = font->driver->encode_char (font, str[len]);
25113 upper_len = (len + 1) / 2;
25114 font->driver->text_extents (font, code, upper_len,
25115 &metrics_upper);
25116 font->driver->text_extents (font, code + upper_len, len - upper_len,
25117 &metrics_lower);
25118
25119
25120
25121 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25122 width = max (metrics_upper.width, metrics_lower.width) + 4;
25123 upper_xoff = upper_yoff = 2; /* the typical case */
25124 if (base_width >= width)
25125 {
25126 /* Align the upper to the left, the lower to the right. */
25127 it->pixel_width = base_width;
25128 lower_xoff = base_width - 2 - metrics_lower.width;
25129 }
25130 else
25131 {
25132 /* Center the shorter one. */
25133 it->pixel_width = width;
25134 if (metrics_upper.width >= metrics_lower.width)
25135 lower_xoff = (width - metrics_lower.width) / 2;
25136 else
25137 {
25138 /* FIXME: This code doesn't look right. It formerly was
25139 missing the "lower_xoff = 0;", which couldn't have
25140 been right since it left lower_xoff uninitialized. */
25141 lower_xoff = 0;
25142 upper_xoff = (width - metrics_upper.width) / 2;
25143 }
25144 }
25145
25146 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25147 top, bottom, and between upper and lower strings. */
25148 height = (metrics_upper.ascent + metrics_upper.descent
25149 + metrics_lower.ascent + metrics_lower.descent) + 5;
25150 /* Center vertically.
25151 H:base_height, D:base_descent
25152 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25153
25154 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25155 descent = D - H/2 + h/2;
25156 lower_yoff = descent - 2 - ld;
25157 upper_yoff = lower_yoff - la - 1 - ud; */
25158 ascent = - (it->descent - (base_height + height + 1) / 2);
25159 descent = it->descent - (base_height - height) / 2;
25160 lower_yoff = descent - 2 - metrics_lower.descent;
25161 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25162 - metrics_upper.descent);
25163 /* Don't make the height shorter than the base height. */
25164 if (height > base_height)
25165 {
25166 it->ascent = ascent;
25167 it->descent = descent;
25168 }
25169 }
25170
25171 it->phys_ascent = it->ascent;
25172 it->phys_descent = it->descent;
25173 if (it->glyph_row)
25174 append_glyphless_glyph (it, face_id, for_no_font, len,
25175 upper_xoff, upper_yoff,
25176 lower_xoff, lower_yoff);
25177 it->nglyphs = 1;
25178 take_vertical_position_into_account (it);
25179 }
25180
25181
25182 /* RIF:
25183 Produce glyphs/get display metrics for the display element IT is
25184 loaded with. See the description of struct it in dispextern.h
25185 for an overview of struct it. */
25186
25187 void
25188 x_produce_glyphs (struct it *it)
25189 {
25190 int extra_line_spacing = it->extra_line_spacing;
25191
25192 it->glyph_not_available_p = 0;
25193
25194 if (it->what == IT_CHARACTER)
25195 {
25196 XChar2b char2b;
25197 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25198 struct font *font = face->font;
25199 struct font_metrics *pcm = NULL;
25200 int boff; /* Baseline offset. */
25201
25202 if (font == NULL)
25203 {
25204 /* When no suitable font is found, display this character by
25205 the method specified in the first extra slot of
25206 Vglyphless_char_display. */
25207 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25208
25209 eassert (it->what == IT_GLYPHLESS);
25210 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25211 goto done;
25212 }
25213
25214 boff = font->baseline_offset;
25215 if (font->vertical_centering)
25216 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25217
25218 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25219 {
25220 int stretched_p;
25221
25222 it->nglyphs = 1;
25223
25224 if (it->override_ascent >= 0)
25225 {
25226 it->ascent = it->override_ascent;
25227 it->descent = it->override_descent;
25228 boff = it->override_boff;
25229 }
25230 else
25231 {
25232 it->ascent = FONT_BASE (font) + boff;
25233 it->descent = FONT_DESCENT (font) - boff;
25234 }
25235
25236 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25237 {
25238 pcm = get_per_char_metric (font, &char2b);
25239 if (pcm->width == 0
25240 && pcm->rbearing == 0 && pcm->lbearing == 0)
25241 pcm = NULL;
25242 }
25243
25244 if (pcm)
25245 {
25246 it->phys_ascent = pcm->ascent + boff;
25247 it->phys_descent = pcm->descent - boff;
25248 it->pixel_width = pcm->width;
25249 }
25250 else
25251 {
25252 it->glyph_not_available_p = 1;
25253 it->phys_ascent = it->ascent;
25254 it->phys_descent = it->descent;
25255 it->pixel_width = font->space_width;
25256 }
25257
25258 if (it->constrain_row_ascent_descent_p)
25259 {
25260 if (it->descent > it->max_descent)
25261 {
25262 it->ascent += it->descent - it->max_descent;
25263 it->descent = it->max_descent;
25264 }
25265 if (it->ascent > it->max_ascent)
25266 {
25267 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25268 it->ascent = it->max_ascent;
25269 }
25270 it->phys_ascent = min (it->phys_ascent, it->ascent);
25271 it->phys_descent = min (it->phys_descent, it->descent);
25272 extra_line_spacing = 0;
25273 }
25274
25275 /* If this is a space inside a region of text with
25276 `space-width' property, change its width. */
25277 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25278 if (stretched_p)
25279 it->pixel_width *= XFLOATINT (it->space_width);
25280
25281 /* If face has a box, add the box thickness to the character
25282 height. If character has a box line to the left and/or
25283 right, add the box line width to the character's width. */
25284 if (face->box != FACE_NO_BOX)
25285 {
25286 int thick = face->box_line_width;
25287
25288 if (thick > 0)
25289 {
25290 it->ascent += thick;
25291 it->descent += thick;
25292 }
25293 else
25294 thick = -thick;
25295
25296 if (it->start_of_box_run_p)
25297 it->pixel_width += thick;
25298 if (it->end_of_box_run_p)
25299 it->pixel_width += thick;
25300 }
25301
25302 /* If face has an overline, add the height of the overline
25303 (1 pixel) and a 1 pixel margin to the character height. */
25304 if (face->overline_p)
25305 it->ascent += overline_margin;
25306
25307 if (it->constrain_row_ascent_descent_p)
25308 {
25309 if (it->ascent > it->max_ascent)
25310 it->ascent = it->max_ascent;
25311 if (it->descent > it->max_descent)
25312 it->descent = it->max_descent;
25313 }
25314
25315 take_vertical_position_into_account (it);
25316
25317 /* If we have to actually produce glyphs, do it. */
25318 if (it->glyph_row)
25319 {
25320 if (stretched_p)
25321 {
25322 /* Translate a space with a `space-width' property
25323 into a stretch glyph. */
25324 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25325 / FONT_HEIGHT (font));
25326 append_stretch_glyph (it, it->object, it->pixel_width,
25327 it->ascent + it->descent, ascent);
25328 }
25329 else
25330 append_glyph (it);
25331
25332 /* If characters with lbearing or rbearing are displayed
25333 in this line, record that fact in a flag of the
25334 glyph row. This is used to optimize X output code. */
25335 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25336 it->glyph_row->contains_overlapping_glyphs_p = 1;
25337 }
25338 if (! stretched_p && it->pixel_width == 0)
25339 /* We assure that all visible glyphs have at least 1-pixel
25340 width. */
25341 it->pixel_width = 1;
25342 }
25343 else if (it->char_to_display == '\n')
25344 {
25345 /* A newline has no width, but we need the height of the
25346 line. But if previous part of the line sets a height,
25347 don't increase that height. */
25348
25349 Lisp_Object height;
25350 Lisp_Object total_height = Qnil;
25351
25352 it->override_ascent = -1;
25353 it->pixel_width = 0;
25354 it->nglyphs = 0;
25355
25356 height = get_it_property (it, Qline_height);
25357 /* Split (line-height total-height) list. */
25358 if (CONSP (height)
25359 && CONSP (XCDR (height))
25360 && NILP (XCDR (XCDR (height))))
25361 {
25362 total_height = XCAR (XCDR (height));
25363 height = XCAR (height);
25364 }
25365 height = calc_line_height_property (it, height, font, boff, 1);
25366
25367 if (it->override_ascent >= 0)
25368 {
25369 it->ascent = it->override_ascent;
25370 it->descent = it->override_descent;
25371 boff = it->override_boff;
25372 }
25373 else
25374 {
25375 it->ascent = FONT_BASE (font) + boff;
25376 it->descent = FONT_DESCENT (font) - boff;
25377 }
25378
25379 if (EQ (height, Qt))
25380 {
25381 if (it->descent > it->max_descent)
25382 {
25383 it->ascent += it->descent - it->max_descent;
25384 it->descent = it->max_descent;
25385 }
25386 if (it->ascent > it->max_ascent)
25387 {
25388 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25389 it->ascent = it->max_ascent;
25390 }
25391 it->phys_ascent = min (it->phys_ascent, it->ascent);
25392 it->phys_descent = min (it->phys_descent, it->descent);
25393 it->constrain_row_ascent_descent_p = 1;
25394 extra_line_spacing = 0;
25395 }
25396 else
25397 {
25398 Lisp_Object spacing;
25399
25400 it->phys_ascent = it->ascent;
25401 it->phys_descent = it->descent;
25402
25403 if ((it->max_ascent > 0 || it->max_descent > 0)
25404 && face->box != FACE_NO_BOX
25405 && face->box_line_width > 0)
25406 {
25407 it->ascent += face->box_line_width;
25408 it->descent += face->box_line_width;
25409 }
25410 if (!NILP (height)
25411 && XINT (height) > it->ascent + it->descent)
25412 it->ascent = XINT (height) - it->descent;
25413
25414 if (!NILP (total_height))
25415 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25416 else
25417 {
25418 spacing = get_it_property (it, Qline_spacing);
25419 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25420 }
25421 if (INTEGERP (spacing))
25422 {
25423 extra_line_spacing = XINT (spacing);
25424 if (!NILP (total_height))
25425 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25426 }
25427 }
25428 }
25429 else /* i.e. (it->char_to_display == '\t') */
25430 {
25431 if (font->space_width > 0)
25432 {
25433 int tab_width = it->tab_width * font->space_width;
25434 int x = it->current_x + it->continuation_lines_width;
25435 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25436
25437 /* If the distance from the current position to the next tab
25438 stop is less than a space character width, use the
25439 tab stop after that. */
25440 if (next_tab_x - x < font->space_width)
25441 next_tab_x += tab_width;
25442
25443 it->pixel_width = next_tab_x - x;
25444 it->nglyphs = 1;
25445 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25446 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25447
25448 if (it->glyph_row)
25449 {
25450 append_stretch_glyph (it, it->object, it->pixel_width,
25451 it->ascent + it->descent, it->ascent);
25452 }
25453 }
25454 else
25455 {
25456 it->pixel_width = 0;
25457 it->nglyphs = 1;
25458 }
25459 }
25460 }
25461 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25462 {
25463 /* A static composition.
25464
25465 Note: A composition is represented as one glyph in the
25466 glyph matrix. There are no padding glyphs.
25467
25468 Important note: pixel_width, ascent, and descent are the
25469 values of what is drawn by draw_glyphs (i.e. the values of
25470 the overall glyphs composed). */
25471 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25472 int boff; /* baseline offset */
25473 struct composition *cmp = composition_table[it->cmp_it.id];
25474 int glyph_len = cmp->glyph_len;
25475 struct font *font = face->font;
25476
25477 it->nglyphs = 1;
25478
25479 /* If we have not yet calculated pixel size data of glyphs of
25480 the composition for the current face font, calculate them
25481 now. Theoretically, we have to check all fonts for the
25482 glyphs, but that requires much time and memory space. So,
25483 here we check only the font of the first glyph. This may
25484 lead to incorrect display, but it's very rare, and C-l
25485 (recenter-top-bottom) can correct the display anyway. */
25486 if (! cmp->font || cmp->font != font)
25487 {
25488 /* Ascent and descent of the font of the first character
25489 of this composition (adjusted by baseline offset).
25490 Ascent and descent of overall glyphs should not be less
25491 than these, respectively. */
25492 int font_ascent, font_descent, font_height;
25493 /* Bounding box of the overall glyphs. */
25494 int leftmost, rightmost, lowest, highest;
25495 int lbearing, rbearing;
25496 int i, width, ascent, descent;
25497 int left_padded = 0, right_padded = 0;
25498 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25499 XChar2b char2b;
25500 struct font_metrics *pcm;
25501 int font_not_found_p;
25502 ptrdiff_t pos;
25503
25504 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25505 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25506 break;
25507 if (glyph_len < cmp->glyph_len)
25508 right_padded = 1;
25509 for (i = 0; i < glyph_len; i++)
25510 {
25511 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25512 break;
25513 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25514 }
25515 if (i > 0)
25516 left_padded = 1;
25517
25518 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25519 : IT_CHARPOS (*it));
25520 /* If no suitable font is found, use the default font. */
25521 font_not_found_p = font == NULL;
25522 if (font_not_found_p)
25523 {
25524 face = face->ascii_face;
25525 font = face->font;
25526 }
25527 boff = font->baseline_offset;
25528 if (font->vertical_centering)
25529 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25530 font_ascent = FONT_BASE (font) + boff;
25531 font_descent = FONT_DESCENT (font) - boff;
25532 font_height = FONT_HEIGHT (font);
25533
25534 cmp->font = font;
25535
25536 pcm = NULL;
25537 if (! font_not_found_p)
25538 {
25539 get_char_face_and_encoding (it->f, c, it->face_id,
25540 &char2b, 0);
25541 pcm = get_per_char_metric (font, &char2b);
25542 }
25543
25544 /* Initialize the bounding box. */
25545 if (pcm)
25546 {
25547 width = cmp->glyph_len > 0 ? pcm->width : 0;
25548 ascent = pcm->ascent;
25549 descent = pcm->descent;
25550 lbearing = pcm->lbearing;
25551 rbearing = pcm->rbearing;
25552 }
25553 else
25554 {
25555 width = cmp->glyph_len > 0 ? font->space_width : 0;
25556 ascent = FONT_BASE (font);
25557 descent = FONT_DESCENT (font);
25558 lbearing = 0;
25559 rbearing = width;
25560 }
25561
25562 rightmost = width;
25563 leftmost = 0;
25564 lowest = - descent + boff;
25565 highest = ascent + boff;
25566
25567 if (! font_not_found_p
25568 && font->default_ascent
25569 && CHAR_TABLE_P (Vuse_default_ascent)
25570 && !NILP (Faref (Vuse_default_ascent,
25571 make_number (it->char_to_display))))
25572 highest = font->default_ascent + boff;
25573
25574 /* Draw the first glyph at the normal position. It may be
25575 shifted to right later if some other glyphs are drawn
25576 at the left. */
25577 cmp->offsets[i * 2] = 0;
25578 cmp->offsets[i * 2 + 1] = boff;
25579 cmp->lbearing = lbearing;
25580 cmp->rbearing = rbearing;
25581
25582 /* Set cmp->offsets for the remaining glyphs. */
25583 for (i++; i < glyph_len; i++)
25584 {
25585 int left, right, btm, top;
25586 int ch = COMPOSITION_GLYPH (cmp, i);
25587 int face_id;
25588 struct face *this_face;
25589
25590 if (ch == '\t')
25591 ch = ' ';
25592 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25593 this_face = FACE_FROM_ID (it->f, face_id);
25594 font = this_face->font;
25595
25596 if (font == NULL)
25597 pcm = NULL;
25598 else
25599 {
25600 get_char_face_and_encoding (it->f, ch, face_id,
25601 &char2b, 0);
25602 pcm = get_per_char_metric (font, &char2b);
25603 }
25604 if (! pcm)
25605 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25606 else
25607 {
25608 width = pcm->width;
25609 ascent = pcm->ascent;
25610 descent = pcm->descent;
25611 lbearing = pcm->lbearing;
25612 rbearing = pcm->rbearing;
25613 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25614 {
25615 /* Relative composition with or without
25616 alternate chars. */
25617 left = (leftmost + rightmost - width) / 2;
25618 btm = - descent + boff;
25619 if (font->relative_compose
25620 && (! CHAR_TABLE_P (Vignore_relative_composition)
25621 || NILP (Faref (Vignore_relative_composition,
25622 make_number (ch)))))
25623 {
25624
25625 if (- descent >= font->relative_compose)
25626 /* One extra pixel between two glyphs. */
25627 btm = highest + 1;
25628 else if (ascent <= 0)
25629 /* One extra pixel between two glyphs. */
25630 btm = lowest - 1 - ascent - descent;
25631 }
25632 }
25633 else
25634 {
25635 /* A composition rule is specified by an integer
25636 value that encodes global and new reference
25637 points (GREF and NREF). GREF and NREF are
25638 specified by numbers as below:
25639
25640 0---1---2 -- ascent
25641 | |
25642 | |
25643 | |
25644 9--10--11 -- center
25645 | |
25646 ---3---4---5--- baseline
25647 | |
25648 6---7---8 -- descent
25649 */
25650 int rule = COMPOSITION_RULE (cmp, i);
25651 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25652
25653 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25654 grefx = gref % 3, nrefx = nref % 3;
25655 grefy = gref / 3, nrefy = nref / 3;
25656 if (xoff)
25657 xoff = font_height * (xoff - 128) / 256;
25658 if (yoff)
25659 yoff = font_height * (yoff - 128) / 256;
25660
25661 left = (leftmost
25662 + grefx * (rightmost - leftmost) / 2
25663 - nrefx * width / 2
25664 + xoff);
25665
25666 btm = ((grefy == 0 ? highest
25667 : grefy == 1 ? 0
25668 : grefy == 2 ? lowest
25669 : (highest + lowest) / 2)
25670 - (nrefy == 0 ? ascent + descent
25671 : nrefy == 1 ? descent - boff
25672 : nrefy == 2 ? 0
25673 : (ascent + descent) / 2)
25674 + yoff);
25675 }
25676
25677 cmp->offsets[i * 2] = left;
25678 cmp->offsets[i * 2 + 1] = btm + descent;
25679
25680 /* Update the bounding box of the overall glyphs. */
25681 if (width > 0)
25682 {
25683 right = left + width;
25684 if (left < leftmost)
25685 leftmost = left;
25686 if (right > rightmost)
25687 rightmost = right;
25688 }
25689 top = btm + descent + ascent;
25690 if (top > highest)
25691 highest = top;
25692 if (btm < lowest)
25693 lowest = btm;
25694
25695 if (cmp->lbearing > left + lbearing)
25696 cmp->lbearing = left + lbearing;
25697 if (cmp->rbearing < left + rbearing)
25698 cmp->rbearing = left + rbearing;
25699 }
25700 }
25701
25702 /* If there are glyphs whose x-offsets are negative,
25703 shift all glyphs to the right and make all x-offsets
25704 non-negative. */
25705 if (leftmost < 0)
25706 {
25707 for (i = 0; i < cmp->glyph_len; i++)
25708 cmp->offsets[i * 2] -= leftmost;
25709 rightmost -= leftmost;
25710 cmp->lbearing -= leftmost;
25711 cmp->rbearing -= leftmost;
25712 }
25713
25714 if (left_padded && cmp->lbearing < 0)
25715 {
25716 for (i = 0; i < cmp->glyph_len; i++)
25717 cmp->offsets[i * 2] -= cmp->lbearing;
25718 rightmost -= cmp->lbearing;
25719 cmp->rbearing -= cmp->lbearing;
25720 cmp->lbearing = 0;
25721 }
25722 if (right_padded && rightmost < cmp->rbearing)
25723 {
25724 rightmost = cmp->rbearing;
25725 }
25726
25727 cmp->pixel_width = rightmost;
25728 cmp->ascent = highest;
25729 cmp->descent = - lowest;
25730 if (cmp->ascent < font_ascent)
25731 cmp->ascent = font_ascent;
25732 if (cmp->descent < font_descent)
25733 cmp->descent = font_descent;
25734 }
25735
25736 if (it->glyph_row
25737 && (cmp->lbearing < 0
25738 || cmp->rbearing > cmp->pixel_width))
25739 it->glyph_row->contains_overlapping_glyphs_p = 1;
25740
25741 it->pixel_width = cmp->pixel_width;
25742 it->ascent = it->phys_ascent = cmp->ascent;
25743 it->descent = it->phys_descent = cmp->descent;
25744 if (face->box != FACE_NO_BOX)
25745 {
25746 int thick = face->box_line_width;
25747
25748 if (thick > 0)
25749 {
25750 it->ascent += thick;
25751 it->descent += thick;
25752 }
25753 else
25754 thick = - thick;
25755
25756 if (it->start_of_box_run_p)
25757 it->pixel_width += thick;
25758 if (it->end_of_box_run_p)
25759 it->pixel_width += thick;
25760 }
25761
25762 /* If face has an overline, add the height of the overline
25763 (1 pixel) and a 1 pixel margin to the character height. */
25764 if (face->overline_p)
25765 it->ascent += overline_margin;
25766
25767 take_vertical_position_into_account (it);
25768 if (it->ascent < 0)
25769 it->ascent = 0;
25770 if (it->descent < 0)
25771 it->descent = 0;
25772
25773 if (it->glyph_row && cmp->glyph_len > 0)
25774 append_composite_glyph (it);
25775 }
25776 else if (it->what == IT_COMPOSITION)
25777 {
25778 /* A dynamic (automatic) composition. */
25779 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25780 Lisp_Object gstring;
25781 struct font_metrics metrics;
25782
25783 it->nglyphs = 1;
25784
25785 gstring = composition_gstring_from_id (it->cmp_it.id);
25786 it->pixel_width
25787 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25788 &metrics);
25789 if (it->glyph_row
25790 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25791 it->glyph_row->contains_overlapping_glyphs_p = 1;
25792 it->ascent = it->phys_ascent = metrics.ascent;
25793 it->descent = it->phys_descent = metrics.descent;
25794 if (face->box != FACE_NO_BOX)
25795 {
25796 int thick = face->box_line_width;
25797
25798 if (thick > 0)
25799 {
25800 it->ascent += thick;
25801 it->descent += thick;
25802 }
25803 else
25804 thick = - thick;
25805
25806 if (it->start_of_box_run_p)
25807 it->pixel_width += thick;
25808 if (it->end_of_box_run_p)
25809 it->pixel_width += thick;
25810 }
25811 /* If face has an overline, add the height of the overline
25812 (1 pixel) and a 1 pixel margin to the character height. */
25813 if (face->overline_p)
25814 it->ascent += overline_margin;
25815 take_vertical_position_into_account (it);
25816 if (it->ascent < 0)
25817 it->ascent = 0;
25818 if (it->descent < 0)
25819 it->descent = 0;
25820
25821 if (it->glyph_row)
25822 append_composite_glyph (it);
25823 }
25824 else if (it->what == IT_GLYPHLESS)
25825 produce_glyphless_glyph (it, 0, Qnil);
25826 else if (it->what == IT_IMAGE)
25827 produce_image_glyph (it);
25828 else if (it->what == IT_STRETCH)
25829 produce_stretch_glyph (it);
25830
25831 done:
25832 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25833 because this isn't true for images with `:ascent 100'. */
25834 eassert (it->ascent >= 0 && it->descent >= 0);
25835 if (it->area == TEXT_AREA)
25836 it->current_x += it->pixel_width;
25837
25838 if (extra_line_spacing > 0)
25839 {
25840 it->descent += extra_line_spacing;
25841 if (extra_line_spacing > it->max_extra_line_spacing)
25842 it->max_extra_line_spacing = extra_line_spacing;
25843 }
25844
25845 it->max_ascent = max (it->max_ascent, it->ascent);
25846 it->max_descent = max (it->max_descent, it->descent);
25847 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25848 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25849 }
25850
25851 /* EXPORT for RIF:
25852 Output LEN glyphs starting at START at the nominal cursor position.
25853 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25854 being updated, and UPDATED_AREA is the area of that row being updated. */
25855
25856 void
25857 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25858 struct glyph *start, enum glyph_row_area updated_area, int len)
25859 {
25860 int x, hpos, chpos = w->phys_cursor.hpos;
25861
25862 eassert (updated_row);
25863 /* When the window is hscrolled, cursor hpos can legitimately be out
25864 of bounds, but we draw the cursor at the corresponding window
25865 margin in that case. */
25866 if (!updated_row->reversed_p && chpos < 0)
25867 chpos = 0;
25868 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25869 chpos = updated_row->used[TEXT_AREA] - 1;
25870
25871 block_input ();
25872
25873 /* Write glyphs. */
25874
25875 hpos = start - updated_row->glyphs[updated_area];
25876 x = draw_glyphs (w, w->output_cursor.x,
25877 updated_row, updated_area,
25878 hpos, hpos + len,
25879 DRAW_NORMAL_TEXT, 0);
25880
25881 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25882 if (updated_area == TEXT_AREA
25883 && w->phys_cursor_on_p
25884 && w->phys_cursor.vpos == w->output_cursor.vpos
25885 && chpos >= hpos
25886 && chpos < hpos + len)
25887 w->phys_cursor_on_p = 0;
25888
25889 unblock_input ();
25890
25891 /* Advance the output cursor. */
25892 w->output_cursor.hpos += len;
25893 w->output_cursor.x = x;
25894 }
25895
25896
25897 /* EXPORT for RIF:
25898 Insert LEN glyphs from START at the nominal cursor position. */
25899
25900 void
25901 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25902 struct glyph *start, enum glyph_row_area updated_area, int len)
25903 {
25904 struct frame *f;
25905 int line_height, shift_by_width, shifted_region_width;
25906 struct glyph_row *row;
25907 struct glyph *glyph;
25908 int frame_x, frame_y;
25909 ptrdiff_t hpos;
25910
25911 eassert (updated_row);
25912 block_input ();
25913 f = XFRAME (WINDOW_FRAME (w));
25914
25915 /* Get the height of the line we are in. */
25916 row = updated_row;
25917 line_height = row->height;
25918
25919 /* Get the width of the glyphs to insert. */
25920 shift_by_width = 0;
25921 for (glyph = start; glyph < start + len; ++glyph)
25922 shift_by_width += glyph->pixel_width;
25923
25924 /* Get the width of the region to shift right. */
25925 shifted_region_width = (window_box_width (w, updated_area)
25926 - w->output_cursor.x
25927 - shift_by_width);
25928
25929 /* Shift right. */
25930 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25931 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25932
25933 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25934 line_height, shift_by_width);
25935
25936 /* Write the glyphs. */
25937 hpos = start - row->glyphs[updated_area];
25938 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25939 hpos, hpos + len,
25940 DRAW_NORMAL_TEXT, 0);
25941
25942 /* Advance the output cursor. */
25943 w->output_cursor.hpos += len;
25944 w->output_cursor.x += shift_by_width;
25945 unblock_input ();
25946 }
25947
25948
25949 /* EXPORT for RIF:
25950 Erase the current text line from the nominal cursor position
25951 (inclusive) to pixel column TO_X (exclusive). The idea is that
25952 everything from TO_X onward is already erased.
25953
25954 TO_X is a pixel position relative to UPDATED_AREA of currently
25955 updated window W. TO_X == -1 means clear to the end of this area. */
25956
25957 void
25958 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25959 enum glyph_row_area updated_area, int to_x)
25960 {
25961 struct frame *f;
25962 int max_x, min_y, max_y;
25963 int from_x, from_y, to_y;
25964
25965 eassert (updated_row);
25966 f = XFRAME (w->frame);
25967
25968 if (updated_row->full_width_p)
25969 max_x = WINDOW_TOTAL_WIDTH (w);
25970 else
25971 max_x = window_box_width (w, updated_area);
25972 max_y = window_text_bottom_y (w);
25973
25974 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25975 of window. For TO_X > 0, truncate to end of drawing area. */
25976 if (to_x == 0)
25977 return;
25978 else if (to_x < 0)
25979 to_x = max_x;
25980 else
25981 to_x = min (to_x, max_x);
25982
25983 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25984
25985 /* Notice if the cursor will be cleared by this operation. */
25986 if (!updated_row->full_width_p)
25987 notice_overwritten_cursor (w, updated_area,
25988 w->output_cursor.x, -1,
25989 updated_row->y,
25990 MATRIX_ROW_BOTTOM_Y (updated_row));
25991
25992 from_x = w->output_cursor.x;
25993
25994 /* Translate to frame coordinates. */
25995 if (updated_row->full_width_p)
25996 {
25997 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25998 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25999 }
26000 else
26001 {
26002 int area_left = window_box_left (w, updated_area);
26003 from_x += area_left;
26004 to_x += area_left;
26005 }
26006
26007 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26008 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26009 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26010
26011 /* Prevent inadvertently clearing to end of the X window. */
26012 if (to_x > from_x && to_y > from_y)
26013 {
26014 block_input ();
26015 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26016 to_x - from_x, to_y - from_y);
26017 unblock_input ();
26018 }
26019 }
26020
26021 #endif /* HAVE_WINDOW_SYSTEM */
26022
26023
26024 \f
26025 /***********************************************************************
26026 Cursor types
26027 ***********************************************************************/
26028
26029 /* Value is the internal representation of the specified cursor type
26030 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26031 of the bar cursor. */
26032
26033 static enum text_cursor_kinds
26034 get_specified_cursor_type (Lisp_Object arg, int *width)
26035 {
26036 enum text_cursor_kinds type;
26037
26038 if (NILP (arg))
26039 return NO_CURSOR;
26040
26041 if (EQ (arg, Qbox))
26042 return FILLED_BOX_CURSOR;
26043
26044 if (EQ (arg, Qhollow))
26045 return HOLLOW_BOX_CURSOR;
26046
26047 if (EQ (arg, Qbar))
26048 {
26049 *width = 2;
26050 return BAR_CURSOR;
26051 }
26052
26053 if (CONSP (arg)
26054 && EQ (XCAR (arg), Qbar)
26055 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26056 {
26057 *width = XINT (XCDR (arg));
26058 return BAR_CURSOR;
26059 }
26060
26061 if (EQ (arg, Qhbar))
26062 {
26063 *width = 2;
26064 return HBAR_CURSOR;
26065 }
26066
26067 if (CONSP (arg)
26068 && EQ (XCAR (arg), Qhbar)
26069 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26070 {
26071 *width = XINT (XCDR (arg));
26072 return HBAR_CURSOR;
26073 }
26074
26075 /* Treat anything unknown as "hollow box cursor".
26076 It was bad to signal an error; people have trouble fixing
26077 .Xdefaults with Emacs, when it has something bad in it. */
26078 type = HOLLOW_BOX_CURSOR;
26079
26080 return type;
26081 }
26082
26083 /* Set the default cursor types for specified frame. */
26084 void
26085 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26086 {
26087 int width = 1;
26088 Lisp_Object tem;
26089
26090 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26091 FRAME_CURSOR_WIDTH (f) = width;
26092
26093 /* By default, set up the blink-off state depending on the on-state. */
26094
26095 tem = Fassoc (arg, Vblink_cursor_alist);
26096 if (!NILP (tem))
26097 {
26098 FRAME_BLINK_OFF_CURSOR (f)
26099 = get_specified_cursor_type (XCDR (tem), &width);
26100 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26101 }
26102 else
26103 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26104
26105 /* Make sure the cursor gets redrawn. */
26106 f->cursor_type_changed = 1;
26107 }
26108
26109
26110 #ifdef HAVE_WINDOW_SYSTEM
26111
26112 /* Return the cursor we want to be displayed in window W. Return
26113 width of bar/hbar cursor through WIDTH arg. Return with
26114 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26115 (i.e. if the `system caret' should track this cursor).
26116
26117 In a mini-buffer window, we want the cursor only to appear if we
26118 are reading input from this window. For the selected window, we
26119 want the cursor type given by the frame parameter or buffer local
26120 setting of cursor-type. If explicitly marked off, draw no cursor.
26121 In all other cases, we want a hollow box cursor. */
26122
26123 static enum text_cursor_kinds
26124 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26125 int *active_cursor)
26126 {
26127 struct frame *f = XFRAME (w->frame);
26128 struct buffer *b = XBUFFER (w->contents);
26129 int cursor_type = DEFAULT_CURSOR;
26130 Lisp_Object alt_cursor;
26131 int non_selected = 0;
26132
26133 *active_cursor = 1;
26134
26135 /* Echo area */
26136 if (cursor_in_echo_area
26137 && FRAME_HAS_MINIBUF_P (f)
26138 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26139 {
26140 if (w == XWINDOW (echo_area_window))
26141 {
26142 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26143 {
26144 *width = FRAME_CURSOR_WIDTH (f);
26145 return FRAME_DESIRED_CURSOR (f);
26146 }
26147 else
26148 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26149 }
26150
26151 *active_cursor = 0;
26152 non_selected = 1;
26153 }
26154
26155 /* Detect a nonselected window or nonselected frame. */
26156 else if (w != XWINDOW (f->selected_window)
26157 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26158 {
26159 *active_cursor = 0;
26160
26161 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26162 return NO_CURSOR;
26163
26164 non_selected = 1;
26165 }
26166
26167 /* Never display a cursor in a window in which cursor-type is nil. */
26168 if (NILP (BVAR (b, cursor_type)))
26169 return NO_CURSOR;
26170
26171 /* Get the normal cursor type for this window. */
26172 if (EQ (BVAR (b, cursor_type), Qt))
26173 {
26174 cursor_type = FRAME_DESIRED_CURSOR (f);
26175 *width = FRAME_CURSOR_WIDTH (f);
26176 }
26177 else
26178 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26179
26180 /* Use cursor-in-non-selected-windows instead
26181 for non-selected window or frame. */
26182 if (non_selected)
26183 {
26184 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26185 if (!EQ (Qt, alt_cursor))
26186 return get_specified_cursor_type (alt_cursor, width);
26187 /* t means modify the normal cursor type. */
26188 if (cursor_type == FILLED_BOX_CURSOR)
26189 cursor_type = HOLLOW_BOX_CURSOR;
26190 else if (cursor_type == BAR_CURSOR && *width > 1)
26191 --*width;
26192 return cursor_type;
26193 }
26194
26195 /* Use normal cursor if not blinked off. */
26196 if (!w->cursor_off_p)
26197 {
26198 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26199 {
26200 if (cursor_type == FILLED_BOX_CURSOR)
26201 {
26202 /* Using a block cursor on large images can be very annoying.
26203 So use a hollow cursor for "large" images.
26204 If image is not transparent (no mask), also use hollow cursor. */
26205 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26206 if (img != NULL && IMAGEP (img->spec))
26207 {
26208 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26209 where N = size of default frame font size.
26210 This should cover most of the "tiny" icons people may use. */
26211 if (!img->mask
26212 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26213 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26214 cursor_type = HOLLOW_BOX_CURSOR;
26215 }
26216 }
26217 else if (cursor_type != NO_CURSOR)
26218 {
26219 /* Display current only supports BOX and HOLLOW cursors for images.
26220 So for now, unconditionally use a HOLLOW cursor when cursor is
26221 not a solid box cursor. */
26222 cursor_type = HOLLOW_BOX_CURSOR;
26223 }
26224 }
26225 return cursor_type;
26226 }
26227
26228 /* Cursor is blinked off, so determine how to "toggle" it. */
26229
26230 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26231 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26232 return get_specified_cursor_type (XCDR (alt_cursor), width);
26233
26234 /* Then see if frame has specified a specific blink off cursor type. */
26235 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26236 {
26237 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26238 return FRAME_BLINK_OFF_CURSOR (f);
26239 }
26240
26241 #if 0
26242 /* Some people liked having a permanently visible blinking cursor,
26243 while others had very strong opinions against it. So it was
26244 decided to remove it. KFS 2003-09-03 */
26245
26246 /* Finally perform built-in cursor blinking:
26247 filled box <-> hollow box
26248 wide [h]bar <-> narrow [h]bar
26249 narrow [h]bar <-> no cursor
26250 other type <-> no cursor */
26251
26252 if (cursor_type == FILLED_BOX_CURSOR)
26253 return HOLLOW_BOX_CURSOR;
26254
26255 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26256 {
26257 *width = 1;
26258 return cursor_type;
26259 }
26260 #endif
26261
26262 return NO_CURSOR;
26263 }
26264
26265
26266 /* Notice when the text cursor of window W has been completely
26267 overwritten by a drawing operation that outputs glyphs in AREA
26268 starting at X0 and ending at X1 in the line starting at Y0 and
26269 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26270 the rest of the line after X0 has been written. Y coordinates
26271 are window-relative. */
26272
26273 static void
26274 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26275 int x0, int x1, int y0, int y1)
26276 {
26277 int cx0, cx1, cy0, cy1;
26278 struct glyph_row *row;
26279
26280 if (!w->phys_cursor_on_p)
26281 return;
26282 if (area != TEXT_AREA)
26283 return;
26284
26285 if (w->phys_cursor.vpos < 0
26286 || w->phys_cursor.vpos >= w->current_matrix->nrows
26287 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26288 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26289 return;
26290
26291 if (row->cursor_in_fringe_p)
26292 {
26293 row->cursor_in_fringe_p = 0;
26294 draw_fringe_bitmap (w, row, row->reversed_p);
26295 w->phys_cursor_on_p = 0;
26296 return;
26297 }
26298
26299 cx0 = w->phys_cursor.x;
26300 cx1 = cx0 + w->phys_cursor_width;
26301 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26302 return;
26303
26304 /* The cursor image will be completely removed from the
26305 screen if the output area intersects the cursor area in
26306 y-direction. When we draw in [y0 y1[, and some part of
26307 the cursor is at y < y0, that part must have been drawn
26308 before. When scrolling, the cursor is erased before
26309 actually scrolling, so we don't come here. When not
26310 scrolling, the rows above the old cursor row must have
26311 changed, and in this case these rows must have written
26312 over the cursor image.
26313
26314 Likewise if part of the cursor is below y1, with the
26315 exception of the cursor being in the first blank row at
26316 the buffer and window end because update_text_area
26317 doesn't draw that row. (Except when it does, but
26318 that's handled in update_text_area.) */
26319
26320 cy0 = w->phys_cursor.y;
26321 cy1 = cy0 + w->phys_cursor_height;
26322 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26323 return;
26324
26325 w->phys_cursor_on_p = 0;
26326 }
26327
26328 #endif /* HAVE_WINDOW_SYSTEM */
26329
26330 \f
26331 /************************************************************************
26332 Mouse Face
26333 ************************************************************************/
26334
26335 #ifdef HAVE_WINDOW_SYSTEM
26336
26337 /* EXPORT for RIF:
26338 Fix the display of area AREA of overlapping row ROW in window W
26339 with respect to the overlapping part OVERLAPS. */
26340
26341 void
26342 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26343 enum glyph_row_area area, int overlaps)
26344 {
26345 int i, x;
26346
26347 block_input ();
26348
26349 x = 0;
26350 for (i = 0; i < row->used[area];)
26351 {
26352 if (row->glyphs[area][i].overlaps_vertically_p)
26353 {
26354 int start = i, start_x = x;
26355
26356 do
26357 {
26358 x += row->glyphs[area][i].pixel_width;
26359 ++i;
26360 }
26361 while (i < row->used[area]
26362 && row->glyphs[area][i].overlaps_vertically_p);
26363
26364 draw_glyphs (w, start_x, row, area,
26365 start, i,
26366 DRAW_NORMAL_TEXT, overlaps);
26367 }
26368 else
26369 {
26370 x += row->glyphs[area][i].pixel_width;
26371 ++i;
26372 }
26373 }
26374
26375 unblock_input ();
26376 }
26377
26378
26379 /* EXPORT:
26380 Draw the cursor glyph of window W in glyph row ROW. See the
26381 comment of draw_glyphs for the meaning of HL. */
26382
26383 void
26384 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26385 enum draw_glyphs_face hl)
26386 {
26387 /* If cursor hpos is out of bounds, don't draw garbage. This can
26388 happen in mini-buffer windows when switching between echo area
26389 glyphs and mini-buffer. */
26390 if ((row->reversed_p
26391 ? (w->phys_cursor.hpos >= 0)
26392 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26393 {
26394 int on_p = w->phys_cursor_on_p;
26395 int x1;
26396 int hpos = w->phys_cursor.hpos;
26397
26398 /* When the window is hscrolled, cursor hpos can legitimately be
26399 out of bounds, but we draw the cursor at the corresponding
26400 window margin in that case. */
26401 if (!row->reversed_p && hpos < 0)
26402 hpos = 0;
26403 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26404 hpos = row->used[TEXT_AREA] - 1;
26405
26406 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26407 hl, 0);
26408 w->phys_cursor_on_p = on_p;
26409
26410 if (hl == DRAW_CURSOR)
26411 w->phys_cursor_width = x1 - w->phys_cursor.x;
26412 /* When we erase the cursor, and ROW is overlapped by other
26413 rows, make sure that these overlapping parts of other rows
26414 are redrawn. */
26415 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26416 {
26417 w->phys_cursor_width = x1 - w->phys_cursor.x;
26418
26419 if (row > w->current_matrix->rows
26420 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26421 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26422 OVERLAPS_ERASED_CURSOR);
26423
26424 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26425 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26426 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26427 OVERLAPS_ERASED_CURSOR);
26428 }
26429 }
26430 }
26431
26432
26433 /* Erase the image of a cursor of window W from the screen. */
26434
26435 #ifndef HAVE_NTGUI
26436 static
26437 #endif
26438 void
26439 erase_phys_cursor (struct window *w)
26440 {
26441 struct frame *f = XFRAME (w->frame);
26442 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26443 int hpos = w->phys_cursor.hpos;
26444 int vpos = w->phys_cursor.vpos;
26445 int mouse_face_here_p = 0;
26446 struct glyph_matrix *active_glyphs = w->current_matrix;
26447 struct glyph_row *cursor_row;
26448 struct glyph *cursor_glyph;
26449 enum draw_glyphs_face hl;
26450
26451 /* No cursor displayed or row invalidated => nothing to do on the
26452 screen. */
26453 if (w->phys_cursor_type == NO_CURSOR)
26454 goto mark_cursor_off;
26455
26456 /* VPOS >= active_glyphs->nrows means that window has been resized.
26457 Don't bother to erase the cursor. */
26458 if (vpos >= active_glyphs->nrows)
26459 goto mark_cursor_off;
26460
26461 /* If row containing cursor is marked invalid, there is nothing we
26462 can do. */
26463 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26464 if (!cursor_row->enabled_p)
26465 goto mark_cursor_off;
26466
26467 /* If line spacing is > 0, old cursor may only be partially visible in
26468 window after split-window. So adjust visible height. */
26469 cursor_row->visible_height = min (cursor_row->visible_height,
26470 window_text_bottom_y (w) - cursor_row->y);
26471
26472 /* If row is completely invisible, don't attempt to delete a cursor which
26473 isn't there. This can happen if cursor is at top of a window, and
26474 we switch to a buffer with a header line in that window. */
26475 if (cursor_row->visible_height <= 0)
26476 goto mark_cursor_off;
26477
26478 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26479 if (cursor_row->cursor_in_fringe_p)
26480 {
26481 cursor_row->cursor_in_fringe_p = 0;
26482 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26483 goto mark_cursor_off;
26484 }
26485
26486 /* This can happen when the new row is shorter than the old one.
26487 In this case, either draw_glyphs or clear_end_of_line
26488 should have cleared the cursor. Note that we wouldn't be
26489 able to erase the cursor in this case because we don't have a
26490 cursor glyph at hand. */
26491 if ((cursor_row->reversed_p
26492 ? (w->phys_cursor.hpos < 0)
26493 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26494 goto mark_cursor_off;
26495
26496 /* When the window is hscrolled, cursor hpos can legitimately be out
26497 of bounds, but we draw the cursor at the corresponding window
26498 margin in that case. */
26499 if (!cursor_row->reversed_p && hpos < 0)
26500 hpos = 0;
26501 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26502 hpos = cursor_row->used[TEXT_AREA] - 1;
26503
26504 /* If the cursor is in the mouse face area, redisplay that when
26505 we clear the cursor. */
26506 if (! NILP (hlinfo->mouse_face_window)
26507 && coords_in_mouse_face_p (w, hpos, vpos)
26508 /* Don't redraw the cursor's spot in mouse face if it is at the
26509 end of a line (on a newline). The cursor appears there, but
26510 mouse highlighting does not. */
26511 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26512 mouse_face_here_p = 1;
26513
26514 /* Maybe clear the display under the cursor. */
26515 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26516 {
26517 int x, y, left_x;
26518 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26519 int width;
26520
26521 cursor_glyph = get_phys_cursor_glyph (w);
26522 if (cursor_glyph == NULL)
26523 goto mark_cursor_off;
26524
26525 width = cursor_glyph->pixel_width;
26526 left_x = window_box_left_offset (w, TEXT_AREA);
26527 x = w->phys_cursor.x;
26528 if (x < left_x)
26529 width -= left_x - x;
26530 width = min (width, window_box_width (w, TEXT_AREA) - x);
26531 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26532 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26533
26534 if (width > 0)
26535 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26536 }
26537
26538 /* Erase the cursor by redrawing the character underneath it. */
26539 if (mouse_face_here_p)
26540 hl = DRAW_MOUSE_FACE;
26541 else
26542 hl = DRAW_NORMAL_TEXT;
26543 draw_phys_cursor_glyph (w, cursor_row, hl);
26544
26545 mark_cursor_off:
26546 w->phys_cursor_on_p = 0;
26547 w->phys_cursor_type = NO_CURSOR;
26548 }
26549
26550
26551 /* EXPORT:
26552 Display or clear cursor of window W. If ON is zero, clear the
26553 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26554 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26555
26556 void
26557 display_and_set_cursor (struct window *w, bool on,
26558 int hpos, int vpos, int x, int y)
26559 {
26560 struct frame *f = XFRAME (w->frame);
26561 int new_cursor_type;
26562 int new_cursor_width;
26563 int active_cursor;
26564 struct glyph_row *glyph_row;
26565 struct glyph *glyph;
26566
26567 /* This is pointless on invisible frames, and dangerous on garbaged
26568 windows and frames; in the latter case, the frame or window may
26569 be in the midst of changing its size, and x and y may be off the
26570 window. */
26571 if (! FRAME_VISIBLE_P (f)
26572 || FRAME_GARBAGED_P (f)
26573 || vpos >= w->current_matrix->nrows
26574 || hpos >= w->current_matrix->matrix_w)
26575 return;
26576
26577 /* If cursor is off and we want it off, return quickly. */
26578 if (!on && !w->phys_cursor_on_p)
26579 return;
26580
26581 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26582 /* If cursor row is not enabled, we don't really know where to
26583 display the cursor. */
26584 if (!glyph_row->enabled_p)
26585 {
26586 w->phys_cursor_on_p = 0;
26587 return;
26588 }
26589
26590 glyph = NULL;
26591 if (!glyph_row->exact_window_width_line_p
26592 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26593 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26594
26595 eassert (input_blocked_p ());
26596
26597 /* Set new_cursor_type to the cursor we want to be displayed. */
26598 new_cursor_type = get_window_cursor_type (w, glyph,
26599 &new_cursor_width, &active_cursor);
26600
26601 /* If cursor is currently being shown and we don't want it to be or
26602 it is in the wrong place, or the cursor type is not what we want,
26603 erase it. */
26604 if (w->phys_cursor_on_p
26605 && (!on
26606 || w->phys_cursor.x != x
26607 || w->phys_cursor.y != y
26608 || new_cursor_type != w->phys_cursor_type
26609 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26610 && new_cursor_width != w->phys_cursor_width)))
26611 erase_phys_cursor (w);
26612
26613 /* Don't check phys_cursor_on_p here because that flag is only set
26614 to zero in some cases where we know that the cursor has been
26615 completely erased, to avoid the extra work of erasing the cursor
26616 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26617 still not be visible, or it has only been partly erased. */
26618 if (on)
26619 {
26620 w->phys_cursor_ascent = glyph_row->ascent;
26621 w->phys_cursor_height = glyph_row->height;
26622
26623 /* Set phys_cursor_.* before x_draw_.* is called because some
26624 of them may need the information. */
26625 w->phys_cursor.x = x;
26626 w->phys_cursor.y = glyph_row->y;
26627 w->phys_cursor.hpos = hpos;
26628 w->phys_cursor.vpos = vpos;
26629 }
26630
26631 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26632 new_cursor_type, new_cursor_width,
26633 on, active_cursor);
26634 }
26635
26636
26637 /* Switch the display of W's cursor on or off, according to the value
26638 of ON. */
26639
26640 static void
26641 update_window_cursor (struct window *w, bool on)
26642 {
26643 /* Don't update cursor in windows whose frame is in the process
26644 of being deleted. */
26645 if (w->current_matrix)
26646 {
26647 int hpos = w->phys_cursor.hpos;
26648 int vpos = w->phys_cursor.vpos;
26649 struct glyph_row *row;
26650
26651 if (vpos >= w->current_matrix->nrows
26652 || hpos >= w->current_matrix->matrix_w)
26653 return;
26654
26655 row = MATRIX_ROW (w->current_matrix, vpos);
26656
26657 /* When the window is hscrolled, cursor hpos can legitimately be
26658 out of bounds, but we draw the cursor at the corresponding
26659 window margin in that case. */
26660 if (!row->reversed_p && hpos < 0)
26661 hpos = 0;
26662 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26663 hpos = row->used[TEXT_AREA] - 1;
26664
26665 block_input ();
26666 display_and_set_cursor (w, on, hpos, vpos,
26667 w->phys_cursor.x, w->phys_cursor.y);
26668 unblock_input ();
26669 }
26670 }
26671
26672
26673 /* Call update_window_cursor with parameter ON_P on all leaf windows
26674 in the window tree rooted at W. */
26675
26676 static void
26677 update_cursor_in_window_tree (struct window *w, bool on_p)
26678 {
26679 while (w)
26680 {
26681 if (WINDOWP (w->contents))
26682 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26683 else
26684 update_window_cursor (w, on_p);
26685
26686 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26687 }
26688 }
26689
26690
26691 /* EXPORT:
26692 Display the cursor on window W, or clear it, according to ON_P.
26693 Don't change the cursor's position. */
26694
26695 void
26696 x_update_cursor (struct frame *f, bool on_p)
26697 {
26698 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26699 }
26700
26701
26702 /* EXPORT:
26703 Clear the cursor of window W to background color, and mark the
26704 cursor as not shown. This is used when the text where the cursor
26705 is about to be rewritten. */
26706
26707 void
26708 x_clear_cursor (struct window *w)
26709 {
26710 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26711 update_window_cursor (w, 0);
26712 }
26713
26714 #endif /* HAVE_WINDOW_SYSTEM */
26715
26716 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26717 and MSDOS. */
26718 static void
26719 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26720 int start_hpos, int end_hpos,
26721 enum draw_glyphs_face draw)
26722 {
26723 #ifdef HAVE_WINDOW_SYSTEM
26724 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26725 {
26726 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26727 return;
26728 }
26729 #endif
26730 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26731 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26732 #endif
26733 }
26734
26735 /* Display the active region described by mouse_face_* according to DRAW. */
26736
26737 static void
26738 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26739 {
26740 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26741 struct frame *f = XFRAME (WINDOW_FRAME (w));
26742
26743 if (/* If window is in the process of being destroyed, don't bother
26744 to do anything. */
26745 w->current_matrix != NULL
26746 /* Don't update mouse highlight if hidden */
26747 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26748 /* Recognize when we are called to operate on rows that don't exist
26749 anymore. This can happen when a window is split. */
26750 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26751 {
26752 int phys_cursor_on_p = w->phys_cursor_on_p;
26753 struct glyph_row *row, *first, *last;
26754
26755 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26756 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26757
26758 for (row = first; row <= last && row->enabled_p; ++row)
26759 {
26760 int start_hpos, end_hpos, start_x;
26761
26762 /* For all but the first row, the highlight starts at column 0. */
26763 if (row == first)
26764 {
26765 /* R2L rows have BEG and END in reversed order, but the
26766 screen drawing geometry is always left to right. So
26767 we need to mirror the beginning and end of the
26768 highlighted area in R2L rows. */
26769 if (!row->reversed_p)
26770 {
26771 start_hpos = hlinfo->mouse_face_beg_col;
26772 start_x = hlinfo->mouse_face_beg_x;
26773 }
26774 else if (row == last)
26775 {
26776 start_hpos = hlinfo->mouse_face_end_col;
26777 start_x = hlinfo->mouse_face_end_x;
26778 }
26779 else
26780 {
26781 start_hpos = 0;
26782 start_x = 0;
26783 }
26784 }
26785 else if (row->reversed_p && row == last)
26786 {
26787 start_hpos = hlinfo->mouse_face_end_col;
26788 start_x = hlinfo->mouse_face_end_x;
26789 }
26790 else
26791 {
26792 start_hpos = 0;
26793 start_x = 0;
26794 }
26795
26796 if (row == last)
26797 {
26798 if (!row->reversed_p)
26799 end_hpos = hlinfo->mouse_face_end_col;
26800 else if (row == first)
26801 end_hpos = hlinfo->mouse_face_beg_col;
26802 else
26803 {
26804 end_hpos = row->used[TEXT_AREA];
26805 if (draw == DRAW_NORMAL_TEXT)
26806 row->fill_line_p = 1; /* Clear to end of line */
26807 }
26808 }
26809 else if (row->reversed_p && row == first)
26810 end_hpos = hlinfo->mouse_face_beg_col;
26811 else
26812 {
26813 end_hpos = row->used[TEXT_AREA];
26814 if (draw == DRAW_NORMAL_TEXT)
26815 row->fill_line_p = 1; /* Clear to end of line */
26816 }
26817
26818 if (end_hpos > start_hpos)
26819 {
26820 draw_row_with_mouse_face (w, start_x, row,
26821 start_hpos, end_hpos, draw);
26822
26823 row->mouse_face_p
26824 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26825 }
26826 }
26827
26828 #ifdef HAVE_WINDOW_SYSTEM
26829 /* When we've written over the cursor, arrange for it to
26830 be displayed again. */
26831 if (FRAME_WINDOW_P (f)
26832 && phys_cursor_on_p && !w->phys_cursor_on_p)
26833 {
26834 int hpos = w->phys_cursor.hpos;
26835
26836 /* When the window is hscrolled, cursor hpos can legitimately be
26837 out of bounds, but we draw the cursor at the corresponding
26838 window margin in that case. */
26839 if (!row->reversed_p && hpos < 0)
26840 hpos = 0;
26841 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26842 hpos = row->used[TEXT_AREA] - 1;
26843
26844 block_input ();
26845 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26846 w->phys_cursor.x, w->phys_cursor.y);
26847 unblock_input ();
26848 }
26849 #endif /* HAVE_WINDOW_SYSTEM */
26850 }
26851
26852 #ifdef HAVE_WINDOW_SYSTEM
26853 /* Change the mouse cursor. */
26854 if (FRAME_WINDOW_P (f))
26855 {
26856 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26857 if (draw == DRAW_NORMAL_TEXT
26858 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26859 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26860 else
26861 #endif
26862 if (draw == DRAW_MOUSE_FACE)
26863 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26864 else
26865 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26866 }
26867 #endif /* HAVE_WINDOW_SYSTEM */
26868 }
26869
26870 /* EXPORT:
26871 Clear out the mouse-highlighted active region.
26872 Redraw it un-highlighted first. Value is non-zero if mouse
26873 face was actually drawn unhighlighted. */
26874
26875 int
26876 clear_mouse_face (Mouse_HLInfo *hlinfo)
26877 {
26878 int cleared = 0;
26879
26880 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26881 {
26882 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26883 cleared = 1;
26884 }
26885
26886 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26887 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26888 hlinfo->mouse_face_window = Qnil;
26889 hlinfo->mouse_face_overlay = Qnil;
26890 return cleared;
26891 }
26892
26893 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26894 within the mouse face on that window. */
26895 static int
26896 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26897 {
26898 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26899
26900 /* Quickly resolve the easy cases. */
26901 if (!(WINDOWP (hlinfo->mouse_face_window)
26902 && XWINDOW (hlinfo->mouse_face_window) == w))
26903 return 0;
26904 if (vpos < hlinfo->mouse_face_beg_row
26905 || vpos > hlinfo->mouse_face_end_row)
26906 return 0;
26907 if (vpos > hlinfo->mouse_face_beg_row
26908 && vpos < hlinfo->mouse_face_end_row)
26909 return 1;
26910
26911 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26912 {
26913 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26914 {
26915 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26916 return 1;
26917 }
26918 else if ((vpos == hlinfo->mouse_face_beg_row
26919 && hpos >= hlinfo->mouse_face_beg_col)
26920 || (vpos == hlinfo->mouse_face_end_row
26921 && hpos < hlinfo->mouse_face_end_col))
26922 return 1;
26923 }
26924 else
26925 {
26926 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26927 {
26928 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26929 return 1;
26930 }
26931 else if ((vpos == hlinfo->mouse_face_beg_row
26932 && hpos <= hlinfo->mouse_face_beg_col)
26933 || (vpos == hlinfo->mouse_face_end_row
26934 && hpos > hlinfo->mouse_face_end_col))
26935 return 1;
26936 }
26937 return 0;
26938 }
26939
26940
26941 /* EXPORT:
26942 Non-zero if physical cursor of window W is within mouse face. */
26943
26944 int
26945 cursor_in_mouse_face_p (struct window *w)
26946 {
26947 int hpos = w->phys_cursor.hpos;
26948 int vpos = w->phys_cursor.vpos;
26949 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26950
26951 /* When the window is hscrolled, cursor hpos can legitimately be out
26952 of bounds, but we draw the cursor at the corresponding window
26953 margin in that case. */
26954 if (!row->reversed_p && hpos < 0)
26955 hpos = 0;
26956 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26957 hpos = row->used[TEXT_AREA] - 1;
26958
26959 return coords_in_mouse_face_p (w, hpos, vpos);
26960 }
26961
26962
26963 \f
26964 /* Find the glyph rows START_ROW and END_ROW of window W that display
26965 characters between buffer positions START_CHARPOS and END_CHARPOS
26966 (excluding END_CHARPOS). DISP_STRING is a display string that
26967 covers these buffer positions. This is similar to
26968 row_containing_pos, but is more accurate when bidi reordering makes
26969 buffer positions change non-linearly with glyph rows. */
26970 static void
26971 rows_from_pos_range (struct window *w,
26972 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26973 Lisp_Object disp_string,
26974 struct glyph_row **start, struct glyph_row **end)
26975 {
26976 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26977 int last_y = window_text_bottom_y (w);
26978 struct glyph_row *row;
26979
26980 *start = NULL;
26981 *end = NULL;
26982
26983 while (!first->enabled_p
26984 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26985 first++;
26986
26987 /* Find the START row. */
26988 for (row = first;
26989 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26990 row++)
26991 {
26992 /* A row can potentially be the START row if the range of the
26993 characters it displays intersects the range
26994 [START_CHARPOS..END_CHARPOS). */
26995 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26996 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26997 /* See the commentary in row_containing_pos, for the
26998 explanation of the complicated way to check whether
26999 some position is beyond the end of the characters
27000 displayed by a row. */
27001 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27002 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27003 && !row->ends_at_zv_p
27004 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27005 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27006 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27007 && !row->ends_at_zv_p
27008 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27009 {
27010 /* Found a candidate row. Now make sure at least one of the
27011 glyphs it displays has a charpos from the range
27012 [START_CHARPOS..END_CHARPOS).
27013
27014 This is not obvious because bidi reordering could make
27015 buffer positions of a row be 1,2,3,102,101,100, and if we
27016 want to highlight characters in [50..60), we don't want
27017 this row, even though [50..60) does intersect [1..103),
27018 the range of character positions given by the row's start
27019 and end positions. */
27020 struct glyph *g = row->glyphs[TEXT_AREA];
27021 struct glyph *e = g + row->used[TEXT_AREA];
27022
27023 while (g < e)
27024 {
27025 if (((BUFFERP (g->object) || INTEGERP (g->object))
27026 && start_charpos <= g->charpos && g->charpos < end_charpos)
27027 /* A glyph that comes from DISP_STRING is by
27028 definition to be highlighted. */
27029 || EQ (g->object, disp_string))
27030 *start = row;
27031 g++;
27032 }
27033 if (*start)
27034 break;
27035 }
27036 }
27037
27038 /* Find the END row. */
27039 if (!*start
27040 /* If the last row is partially visible, start looking for END
27041 from that row, instead of starting from FIRST. */
27042 && !(row->enabled_p
27043 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27044 row = first;
27045 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27046 {
27047 struct glyph_row *next = row + 1;
27048 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27049
27050 if (!next->enabled_p
27051 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27052 /* The first row >= START whose range of displayed characters
27053 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27054 is the row END + 1. */
27055 || (start_charpos < next_start
27056 && end_charpos < next_start)
27057 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27058 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27059 && !next->ends_at_zv_p
27060 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27061 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27062 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27063 && !next->ends_at_zv_p
27064 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27065 {
27066 *end = row;
27067 break;
27068 }
27069 else
27070 {
27071 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27072 but none of the characters it displays are in the range, it is
27073 also END + 1. */
27074 struct glyph *g = next->glyphs[TEXT_AREA];
27075 struct glyph *s = g;
27076 struct glyph *e = g + next->used[TEXT_AREA];
27077
27078 while (g < e)
27079 {
27080 if (((BUFFERP (g->object) || INTEGERP (g->object))
27081 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27082 /* If the buffer position of the first glyph in
27083 the row is equal to END_CHARPOS, it means
27084 the last character to be highlighted is the
27085 newline of ROW, and we must consider NEXT as
27086 END, not END+1. */
27087 || (((!next->reversed_p && g == s)
27088 || (next->reversed_p && g == e - 1))
27089 && (g->charpos == end_charpos
27090 /* Special case for when NEXT is an
27091 empty line at ZV. */
27092 || (g->charpos == -1
27093 && !row->ends_at_zv_p
27094 && next_start == end_charpos)))))
27095 /* A glyph that comes from DISP_STRING is by
27096 definition to be highlighted. */
27097 || EQ (g->object, disp_string))
27098 break;
27099 g++;
27100 }
27101 if (g == e)
27102 {
27103 *end = row;
27104 break;
27105 }
27106 /* The first row that ends at ZV must be the last to be
27107 highlighted. */
27108 else if (next->ends_at_zv_p)
27109 {
27110 *end = next;
27111 break;
27112 }
27113 }
27114 }
27115 }
27116
27117 /* This function sets the mouse_face_* elements of HLINFO, assuming
27118 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27119 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27120 for the overlay or run of text properties specifying the mouse
27121 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27122 before-string and after-string that must also be highlighted.
27123 DISP_STRING, if non-nil, is a display string that may cover some
27124 or all of the highlighted text. */
27125
27126 static void
27127 mouse_face_from_buffer_pos (Lisp_Object window,
27128 Mouse_HLInfo *hlinfo,
27129 ptrdiff_t mouse_charpos,
27130 ptrdiff_t start_charpos,
27131 ptrdiff_t end_charpos,
27132 Lisp_Object before_string,
27133 Lisp_Object after_string,
27134 Lisp_Object disp_string)
27135 {
27136 struct window *w = XWINDOW (window);
27137 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27138 struct glyph_row *r1, *r2;
27139 struct glyph *glyph, *end;
27140 ptrdiff_t ignore, pos;
27141 int x;
27142
27143 eassert (NILP (disp_string) || STRINGP (disp_string));
27144 eassert (NILP (before_string) || STRINGP (before_string));
27145 eassert (NILP (after_string) || STRINGP (after_string));
27146
27147 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27148 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27149 if (r1 == NULL)
27150 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27151 /* If the before-string or display-string contains newlines,
27152 rows_from_pos_range skips to its last row. Move back. */
27153 if (!NILP (before_string) || !NILP (disp_string))
27154 {
27155 struct glyph_row *prev;
27156 while ((prev = r1 - 1, prev >= first)
27157 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27158 && prev->used[TEXT_AREA] > 0)
27159 {
27160 struct glyph *beg = prev->glyphs[TEXT_AREA];
27161 glyph = beg + prev->used[TEXT_AREA];
27162 while (--glyph >= beg && INTEGERP (glyph->object));
27163 if (glyph < beg
27164 || !(EQ (glyph->object, before_string)
27165 || EQ (glyph->object, disp_string)))
27166 break;
27167 r1 = prev;
27168 }
27169 }
27170 if (r2 == NULL)
27171 {
27172 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27173 hlinfo->mouse_face_past_end = 1;
27174 }
27175 else if (!NILP (after_string))
27176 {
27177 /* If the after-string has newlines, advance to its last row. */
27178 struct glyph_row *next;
27179 struct glyph_row *last
27180 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27181
27182 for (next = r2 + 1;
27183 next <= last
27184 && next->used[TEXT_AREA] > 0
27185 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27186 ++next)
27187 r2 = next;
27188 }
27189 /* The rest of the display engine assumes that mouse_face_beg_row is
27190 either above mouse_face_end_row or identical to it. But with
27191 bidi-reordered continued lines, the row for START_CHARPOS could
27192 be below the row for END_CHARPOS. If so, swap the rows and store
27193 them in correct order. */
27194 if (r1->y > r2->y)
27195 {
27196 struct glyph_row *tem = r2;
27197
27198 r2 = r1;
27199 r1 = tem;
27200 }
27201
27202 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27203 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27204
27205 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27206 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27207 could be anywhere in the row and in any order. The strategy
27208 below is to find the leftmost and the rightmost glyph that
27209 belongs to either of these 3 strings, or whose position is
27210 between START_CHARPOS and END_CHARPOS, and highlight all the
27211 glyphs between those two. This may cover more than just the text
27212 between START_CHARPOS and END_CHARPOS if the range of characters
27213 strides the bidi level boundary, e.g. if the beginning is in R2L
27214 text while the end is in L2R text or vice versa. */
27215 if (!r1->reversed_p)
27216 {
27217 /* This row is in a left to right paragraph. Scan it left to
27218 right. */
27219 glyph = r1->glyphs[TEXT_AREA];
27220 end = glyph + r1->used[TEXT_AREA];
27221 x = r1->x;
27222
27223 /* Skip truncation glyphs at the start of the glyph row. */
27224 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27225 for (; glyph < end
27226 && INTEGERP (glyph->object)
27227 && glyph->charpos < 0;
27228 ++glyph)
27229 x += glyph->pixel_width;
27230
27231 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27232 or DISP_STRING, and the first glyph from buffer whose
27233 position is between START_CHARPOS and END_CHARPOS. */
27234 for (; glyph < end
27235 && !INTEGERP (glyph->object)
27236 && !EQ (glyph->object, disp_string)
27237 && !(BUFFERP (glyph->object)
27238 && (glyph->charpos >= start_charpos
27239 && glyph->charpos < end_charpos));
27240 ++glyph)
27241 {
27242 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27243 are present at buffer positions between START_CHARPOS and
27244 END_CHARPOS, or if they come from an overlay. */
27245 if (EQ (glyph->object, before_string))
27246 {
27247 pos = string_buffer_position (before_string,
27248 start_charpos);
27249 /* If pos == 0, it means before_string came from an
27250 overlay, not from a buffer position. */
27251 if (!pos || (pos >= start_charpos && pos < end_charpos))
27252 break;
27253 }
27254 else if (EQ (glyph->object, after_string))
27255 {
27256 pos = string_buffer_position (after_string, end_charpos);
27257 if (!pos || (pos >= start_charpos && pos < end_charpos))
27258 break;
27259 }
27260 x += glyph->pixel_width;
27261 }
27262 hlinfo->mouse_face_beg_x = x;
27263 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27264 }
27265 else
27266 {
27267 /* This row is in a right to left paragraph. Scan it right to
27268 left. */
27269 struct glyph *g;
27270
27271 end = r1->glyphs[TEXT_AREA] - 1;
27272 glyph = end + r1->used[TEXT_AREA];
27273
27274 /* Skip truncation glyphs at the start of the glyph row. */
27275 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27276 for (; glyph > end
27277 && INTEGERP (glyph->object)
27278 && glyph->charpos < 0;
27279 --glyph)
27280 ;
27281
27282 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27283 or DISP_STRING, and the first glyph from buffer whose
27284 position is between START_CHARPOS and END_CHARPOS. */
27285 for (; glyph > end
27286 && !INTEGERP (glyph->object)
27287 && !EQ (glyph->object, disp_string)
27288 && !(BUFFERP (glyph->object)
27289 && (glyph->charpos >= start_charpos
27290 && glyph->charpos < end_charpos));
27291 --glyph)
27292 {
27293 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27294 are present at buffer positions between START_CHARPOS and
27295 END_CHARPOS, or if they come from an overlay. */
27296 if (EQ (glyph->object, before_string))
27297 {
27298 pos = string_buffer_position (before_string, start_charpos);
27299 /* If pos == 0, it means before_string came from an
27300 overlay, not from a buffer position. */
27301 if (!pos || (pos >= start_charpos && pos < end_charpos))
27302 break;
27303 }
27304 else if (EQ (glyph->object, after_string))
27305 {
27306 pos = string_buffer_position (after_string, end_charpos);
27307 if (!pos || (pos >= start_charpos && pos < end_charpos))
27308 break;
27309 }
27310 }
27311
27312 glyph++; /* first glyph to the right of the highlighted area */
27313 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27314 x += g->pixel_width;
27315 hlinfo->mouse_face_beg_x = x;
27316 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27317 }
27318
27319 /* If the highlight ends in a different row, compute GLYPH and END
27320 for the end row. Otherwise, reuse the values computed above for
27321 the row where the highlight begins. */
27322 if (r2 != r1)
27323 {
27324 if (!r2->reversed_p)
27325 {
27326 glyph = r2->glyphs[TEXT_AREA];
27327 end = glyph + r2->used[TEXT_AREA];
27328 x = r2->x;
27329 }
27330 else
27331 {
27332 end = r2->glyphs[TEXT_AREA] - 1;
27333 glyph = end + r2->used[TEXT_AREA];
27334 }
27335 }
27336
27337 if (!r2->reversed_p)
27338 {
27339 /* Skip truncation and continuation glyphs near the end of the
27340 row, and also blanks and stretch glyphs inserted by
27341 extend_face_to_end_of_line. */
27342 while (end > glyph
27343 && INTEGERP ((end - 1)->object))
27344 --end;
27345 /* Scan the rest of the glyph row from the end, looking for the
27346 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27347 DISP_STRING, or whose position is between START_CHARPOS
27348 and END_CHARPOS */
27349 for (--end;
27350 end > glyph
27351 && !INTEGERP (end->object)
27352 && !EQ (end->object, disp_string)
27353 && !(BUFFERP (end->object)
27354 && (end->charpos >= start_charpos
27355 && end->charpos < end_charpos));
27356 --end)
27357 {
27358 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27359 are present at buffer positions between START_CHARPOS and
27360 END_CHARPOS, or if they come from an overlay. */
27361 if (EQ (end->object, before_string))
27362 {
27363 pos = string_buffer_position (before_string, start_charpos);
27364 if (!pos || (pos >= start_charpos && pos < end_charpos))
27365 break;
27366 }
27367 else if (EQ (end->object, after_string))
27368 {
27369 pos = string_buffer_position (after_string, end_charpos);
27370 if (!pos || (pos >= start_charpos && pos < end_charpos))
27371 break;
27372 }
27373 }
27374 /* Find the X coordinate of the last glyph to be highlighted. */
27375 for (; glyph <= end; ++glyph)
27376 x += glyph->pixel_width;
27377
27378 hlinfo->mouse_face_end_x = x;
27379 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27380 }
27381 else
27382 {
27383 /* Skip truncation and continuation glyphs near the end of the
27384 row, and also blanks and stretch glyphs inserted by
27385 extend_face_to_end_of_line. */
27386 x = r2->x;
27387 end++;
27388 while (end < glyph
27389 && INTEGERP (end->object))
27390 {
27391 x += end->pixel_width;
27392 ++end;
27393 }
27394 /* Scan the rest of the glyph row from the end, looking for the
27395 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27396 DISP_STRING, or whose position is between START_CHARPOS
27397 and END_CHARPOS */
27398 for ( ;
27399 end < glyph
27400 && !INTEGERP (end->object)
27401 && !EQ (end->object, disp_string)
27402 && !(BUFFERP (end->object)
27403 && (end->charpos >= start_charpos
27404 && end->charpos < end_charpos));
27405 ++end)
27406 {
27407 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27408 are present at buffer positions between START_CHARPOS and
27409 END_CHARPOS, or if they come from an overlay. */
27410 if (EQ (end->object, before_string))
27411 {
27412 pos = string_buffer_position (before_string, start_charpos);
27413 if (!pos || (pos >= start_charpos && pos < end_charpos))
27414 break;
27415 }
27416 else if (EQ (end->object, after_string))
27417 {
27418 pos = string_buffer_position (after_string, end_charpos);
27419 if (!pos || (pos >= start_charpos && pos < end_charpos))
27420 break;
27421 }
27422 x += end->pixel_width;
27423 }
27424 /* If we exited the above loop because we arrived at the last
27425 glyph of the row, and its buffer position is still not in
27426 range, it means the last character in range is the preceding
27427 newline. Bump the end column and x values to get past the
27428 last glyph. */
27429 if (end == glyph
27430 && BUFFERP (end->object)
27431 && (end->charpos < start_charpos
27432 || end->charpos >= end_charpos))
27433 {
27434 x += end->pixel_width;
27435 ++end;
27436 }
27437 hlinfo->mouse_face_end_x = x;
27438 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27439 }
27440
27441 hlinfo->mouse_face_window = window;
27442 hlinfo->mouse_face_face_id
27443 = face_at_buffer_position (w, mouse_charpos, &ignore,
27444 mouse_charpos + 1,
27445 !hlinfo->mouse_face_hidden, -1);
27446 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27447 }
27448
27449 /* The following function is not used anymore (replaced with
27450 mouse_face_from_string_pos), but I leave it here for the time
27451 being, in case someone would. */
27452
27453 #if 0 /* not used */
27454
27455 /* Find the position of the glyph for position POS in OBJECT in
27456 window W's current matrix, and return in *X, *Y the pixel
27457 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27458
27459 RIGHT_P non-zero means return the position of the right edge of the
27460 glyph, RIGHT_P zero means return the left edge position.
27461
27462 If no glyph for POS exists in the matrix, return the position of
27463 the glyph with the next smaller position that is in the matrix, if
27464 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27465 exists in the matrix, return the position of the glyph with the
27466 next larger position in OBJECT.
27467
27468 Value is non-zero if a glyph was found. */
27469
27470 static int
27471 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27472 int *hpos, int *vpos, int *x, int *y, int right_p)
27473 {
27474 int yb = window_text_bottom_y (w);
27475 struct glyph_row *r;
27476 struct glyph *best_glyph = NULL;
27477 struct glyph_row *best_row = NULL;
27478 int best_x = 0;
27479
27480 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27481 r->enabled_p && r->y < yb;
27482 ++r)
27483 {
27484 struct glyph *g = r->glyphs[TEXT_AREA];
27485 struct glyph *e = g + r->used[TEXT_AREA];
27486 int gx;
27487
27488 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27489 if (EQ (g->object, object))
27490 {
27491 if (g->charpos == pos)
27492 {
27493 best_glyph = g;
27494 best_x = gx;
27495 best_row = r;
27496 goto found;
27497 }
27498 else if (best_glyph == NULL
27499 || ((eabs (g->charpos - pos)
27500 < eabs (best_glyph->charpos - pos))
27501 && (right_p
27502 ? g->charpos < pos
27503 : g->charpos > pos)))
27504 {
27505 best_glyph = g;
27506 best_x = gx;
27507 best_row = r;
27508 }
27509 }
27510 }
27511
27512 found:
27513
27514 if (best_glyph)
27515 {
27516 *x = best_x;
27517 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27518
27519 if (right_p)
27520 {
27521 *x += best_glyph->pixel_width;
27522 ++*hpos;
27523 }
27524
27525 *y = best_row->y;
27526 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27527 }
27528
27529 return best_glyph != NULL;
27530 }
27531 #endif /* not used */
27532
27533 /* Find the positions of the first and the last glyphs in window W's
27534 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27535 (assumed to be a string), and return in HLINFO's mouse_face_*
27536 members the pixel and column/row coordinates of those glyphs. */
27537
27538 static void
27539 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27540 Lisp_Object object,
27541 ptrdiff_t startpos, ptrdiff_t endpos)
27542 {
27543 int yb = window_text_bottom_y (w);
27544 struct glyph_row *r;
27545 struct glyph *g, *e;
27546 int gx;
27547 int found = 0;
27548
27549 /* Find the glyph row with at least one position in the range
27550 [STARTPOS..ENDPOS), and the first glyph in that row whose
27551 position belongs to that range. */
27552 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27553 r->enabled_p && r->y < yb;
27554 ++r)
27555 {
27556 if (!r->reversed_p)
27557 {
27558 g = r->glyphs[TEXT_AREA];
27559 e = g + r->used[TEXT_AREA];
27560 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27561 if (EQ (g->object, object)
27562 && startpos <= g->charpos && g->charpos < endpos)
27563 {
27564 hlinfo->mouse_face_beg_row
27565 = MATRIX_ROW_VPOS (r, w->current_matrix);
27566 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27567 hlinfo->mouse_face_beg_x = gx;
27568 found = 1;
27569 break;
27570 }
27571 }
27572 else
27573 {
27574 struct glyph *g1;
27575
27576 e = r->glyphs[TEXT_AREA];
27577 g = e + r->used[TEXT_AREA];
27578 for ( ; g > e; --g)
27579 if (EQ ((g-1)->object, object)
27580 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27581 {
27582 hlinfo->mouse_face_beg_row
27583 = MATRIX_ROW_VPOS (r, w->current_matrix);
27584 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27585 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27586 gx += g1->pixel_width;
27587 hlinfo->mouse_face_beg_x = gx;
27588 found = 1;
27589 break;
27590 }
27591 }
27592 if (found)
27593 break;
27594 }
27595
27596 if (!found)
27597 return;
27598
27599 /* Starting with the next row, look for the first row which does NOT
27600 include any glyphs whose positions are in the range. */
27601 for (++r; r->enabled_p && r->y < yb; ++r)
27602 {
27603 g = r->glyphs[TEXT_AREA];
27604 e = g + r->used[TEXT_AREA];
27605 found = 0;
27606 for ( ; g < e; ++g)
27607 if (EQ (g->object, object)
27608 && startpos <= g->charpos && g->charpos < endpos)
27609 {
27610 found = 1;
27611 break;
27612 }
27613 if (!found)
27614 break;
27615 }
27616
27617 /* The highlighted region ends on the previous row. */
27618 r--;
27619
27620 /* Set the end row. */
27621 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27622
27623 /* Compute and set the end column and the end column's horizontal
27624 pixel coordinate. */
27625 if (!r->reversed_p)
27626 {
27627 g = r->glyphs[TEXT_AREA];
27628 e = g + r->used[TEXT_AREA];
27629 for ( ; e > g; --e)
27630 if (EQ ((e-1)->object, object)
27631 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27632 break;
27633 hlinfo->mouse_face_end_col = e - g;
27634
27635 for (gx = r->x; g < e; ++g)
27636 gx += g->pixel_width;
27637 hlinfo->mouse_face_end_x = gx;
27638 }
27639 else
27640 {
27641 e = r->glyphs[TEXT_AREA];
27642 g = e + r->used[TEXT_AREA];
27643 for (gx = r->x ; e < g; ++e)
27644 {
27645 if (EQ (e->object, object)
27646 && startpos <= e->charpos && e->charpos < endpos)
27647 break;
27648 gx += e->pixel_width;
27649 }
27650 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27651 hlinfo->mouse_face_end_x = gx;
27652 }
27653 }
27654
27655 #ifdef HAVE_WINDOW_SYSTEM
27656
27657 /* See if position X, Y is within a hot-spot of an image. */
27658
27659 static int
27660 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27661 {
27662 if (!CONSP (hot_spot))
27663 return 0;
27664
27665 if (EQ (XCAR (hot_spot), Qrect))
27666 {
27667 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27668 Lisp_Object rect = XCDR (hot_spot);
27669 Lisp_Object tem;
27670 if (!CONSP (rect))
27671 return 0;
27672 if (!CONSP (XCAR (rect)))
27673 return 0;
27674 if (!CONSP (XCDR (rect)))
27675 return 0;
27676 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27677 return 0;
27678 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27679 return 0;
27680 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27681 return 0;
27682 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27683 return 0;
27684 return 1;
27685 }
27686 else if (EQ (XCAR (hot_spot), Qcircle))
27687 {
27688 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27689 Lisp_Object circ = XCDR (hot_spot);
27690 Lisp_Object lr, lx0, ly0;
27691 if (CONSP (circ)
27692 && CONSP (XCAR (circ))
27693 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27694 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27695 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27696 {
27697 double r = XFLOATINT (lr);
27698 double dx = XINT (lx0) - x;
27699 double dy = XINT (ly0) - y;
27700 return (dx * dx + dy * dy <= r * r);
27701 }
27702 }
27703 else if (EQ (XCAR (hot_spot), Qpoly))
27704 {
27705 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27706 if (VECTORP (XCDR (hot_spot)))
27707 {
27708 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27709 Lisp_Object *poly = v->contents;
27710 ptrdiff_t n = v->header.size;
27711 ptrdiff_t i;
27712 int inside = 0;
27713 Lisp_Object lx, ly;
27714 int x0, y0;
27715
27716 /* Need an even number of coordinates, and at least 3 edges. */
27717 if (n < 6 || n & 1)
27718 return 0;
27719
27720 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27721 If count is odd, we are inside polygon. Pixels on edges
27722 may or may not be included depending on actual geometry of the
27723 polygon. */
27724 if ((lx = poly[n-2], !INTEGERP (lx))
27725 || (ly = poly[n-1], !INTEGERP (lx)))
27726 return 0;
27727 x0 = XINT (lx), y0 = XINT (ly);
27728 for (i = 0; i < n; i += 2)
27729 {
27730 int x1 = x0, y1 = y0;
27731 if ((lx = poly[i], !INTEGERP (lx))
27732 || (ly = poly[i+1], !INTEGERP (ly)))
27733 return 0;
27734 x0 = XINT (lx), y0 = XINT (ly);
27735
27736 /* Does this segment cross the X line? */
27737 if (x0 >= x)
27738 {
27739 if (x1 >= x)
27740 continue;
27741 }
27742 else if (x1 < x)
27743 continue;
27744 if (y > y0 && y > y1)
27745 continue;
27746 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27747 inside = !inside;
27748 }
27749 return inside;
27750 }
27751 }
27752 return 0;
27753 }
27754
27755 Lisp_Object
27756 find_hot_spot (Lisp_Object map, int x, int y)
27757 {
27758 while (CONSP (map))
27759 {
27760 if (CONSP (XCAR (map))
27761 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27762 return XCAR (map);
27763 map = XCDR (map);
27764 }
27765
27766 return Qnil;
27767 }
27768
27769 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27770 3, 3, 0,
27771 doc: /* Lookup in image map MAP coordinates X and Y.
27772 An image map is an alist where each element has the format (AREA ID PLIST).
27773 An AREA is specified as either a rectangle, a circle, or a polygon:
27774 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27775 pixel coordinates of the upper left and bottom right corners.
27776 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27777 and the radius of the circle; r may be a float or integer.
27778 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27779 vector describes one corner in the polygon.
27780 Returns the alist element for the first matching AREA in MAP. */)
27781 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27782 {
27783 if (NILP (map))
27784 return Qnil;
27785
27786 CHECK_NUMBER (x);
27787 CHECK_NUMBER (y);
27788
27789 return find_hot_spot (map,
27790 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27791 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27792 }
27793
27794
27795 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27796 static void
27797 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27798 {
27799 /* Do not change cursor shape while dragging mouse. */
27800 if (!NILP (do_mouse_tracking))
27801 return;
27802
27803 if (!NILP (pointer))
27804 {
27805 if (EQ (pointer, Qarrow))
27806 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27807 else if (EQ (pointer, Qhand))
27808 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27809 else if (EQ (pointer, Qtext))
27810 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27811 else if (EQ (pointer, intern ("hdrag")))
27812 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27813 #ifdef HAVE_X_WINDOWS
27814 else if (EQ (pointer, intern ("vdrag")))
27815 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27816 #endif
27817 else if (EQ (pointer, intern ("hourglass")))
27818 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27819 else if (EQ (pointer, Qmodeline))
27820 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27821 else
27822 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27823 }
27824
27825 if (cursor != No_Cursor)
27826 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27827 }
27828
27829 #endif /* HAVE_WINDOW_SYSTEM */
27830
27831 /* Take proper action when mouse has moved to the mode or header line
27832 or marginal area AREA of window W, x-position X and y-position Y.
27833 X is relative to the start of the text display area of W, so the
27834 width of bitmap areas and scroll bars must be subtracted to get a
27835 position relative to the start of the mode line. */
27836
27837 static void
27838 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27839 enum window_part area)
27840 {
27841 struct window *w = XWINDOW (window);
27842 struct frame *f = XFRAME (w->frame);
27843 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27844 #ifdef HAVE_WINDOW_SYSTEM
27845 Display_Info *dpyinfo;
27846 #endif
27847 Cursor cursor = No_Cursor;
27848 Lisp_Object pointer = Qnil;
27849 int dx, dy, width, height;
27850 ptrdiff_t charpos;
27851 Lisp_Object string, object = Qnil;
27852 Lisp_Object pos IF_LINT (= Qnil), help;
27853
27854 Lisp_Object mouse_face;
27855 int original_x_pixel = x;
27856 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27857 struct glyph_row *row IF_LINT (= 0);
27858
27859 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27860 {
27861 int x0;
27862 struct glyph *end;
27863
27864 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27865 returns them in row/column units! */
27866 string = mode_line_string (w, area, &x, &y, &charpos,
27867 &object, &dx, &dy, &width, &height);
27868
27869 row = (area == ON_MODE_LINE
27870 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27871 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27872
27873 /* Find the glyph under the mouse pointer. */
27874 if (row->mode_line_p && row->enabled_p)
27875 {
27876 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27877 end = glyph + row->used[TEXT_AREA];
27878
27879 for (x0 = original_x_pixel;
27880 glyph < end && x0 >= glyph->pixel_width;
27881 ++glyph)
27882 x0 -= glyph->pixel_width;
27883
27884 if (glyph >= end)
27885 glyph = NULL;
27886 }
27887 }
27888 else
27889 {
27890 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27891 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27892 returns them in row/column units! */
27893 string = marginal_area_string (w, area, &x, &y, &charpos,
27894 &object, &dx, &dy, &width, &height);
27895 }
27896
27897 help = Qnil;
27898
27899 #ifdef HAVE_WINDOW_SYSTEM
27900 if (IMAGEP (object))
27901 {
27902 Lisp_Object image_map, hotspot;
27903 if ((image_map = Fplist_get (XCDR (object), QCmap),
27904 !NILP (image_map))
27905 && (hotspot = find_hot_spot (image_map, dx, dy),
27906 CONSP (hotspot))
27907 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27908 {
27909 Lisp_Object plist;
27910
27911 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27912 If so, we could look for mouse-enter, mouse-leave
27913 properties in PLIST (and do something...). */
27914 hotspot = XCDR (hotspot);
27915 if (CONSP (hotspot)
27916 && (plist = XCAR (hotspot), CONSP (plist)))
27917 {
27918 pointer = Fplist_get (plist, Qpointer);
27919 if (NILP (pointer))
27920 pointer = Qhand;
27921 help = Fplist_get (plist, Qhelp_echo);
27922 if (!NILP (help))
27923 {
27924 help_echo_string = help;
27925 XSETWINDOW (help_echo_window, w);
27926 help_echo_object = w->contents;
27927 help_echo_pos = charpos;
27928 }
27929 }
27930 }
27931 if (NILP (pointer))
27932 pointer = Fplist_get (XCDR (object), QCpointer);
27933 }
27934 #endif /* HAVE_WINDOW_SYSTEM */
27935
27936 if (STRINGP (string))
27937 pos = make_number (charpos);
27938
27939 /* Set the help text and mouse pointer. If the mouse is on a part
27940 of the mode line without any text (e.g. past the right edge of
27941 the mode line text), use the default help text and pointer. */
27942 if (STRINGP (string) || area == ON_MODE_LINE)
27943 {
27944 /* Arrange to display the help by setting the global variables
27945 help_echo_string, help_echo_object, and help_echo_pos. */
27946 if (NILP (help))
27947 {
27948 if (STRINGP (string))
27949 help = Fget_text_property (pos, Qhelp_echo, string);
27950
27951 if (!NILP (help))
27952 {
27953 help_echo_string = help;
27954 XSETWINDOW (help_echo_window, w);
27955 help_echo_object = string;
27956 help_echo_pos = charpos;
27957 }
27958 else if (area == ON_MODE_LINE)
27959 {
27960 Lisp_Object default_help
27961 = buffer_local_value_1 (Qmode_line_default_help_echo,
27962 w->contents);
27963
27964 if (STRINGP (default_help))
27965 {
27966 help_echo_string = default_help;
27967 XSETWINDOW (help_echo_window, w);
27968 help_echo_object = Qnil;
27969 help_echo_pos = -1;
27970 }
27971 }
27972 }
27973
27974 #ifdef HAVE_WINDOW_SYSTEM
27975 /* Change the mouse pointer according to what is under it. */
27976 if (FRAME_WINDOW_P (f))
27977 {
27978 dpyinfo = FRAME_DISPLAY_INFO (f);
27979 if (STRINGP (string))
27980 {
27981 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27982
27983 if (NILP (pointer))
27984 pointer = Fget_text_property (pos, Qpointer, string);
27985
27986 /* Change the mouse pointer according to what is under X/Y. */
27987 if (NILP (pointer)
27988 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27989 {
27990 Lisp_Object map;
27991 map = Fget_text_property (pos, Qlocal_map, string);
27992 if (!KEYMAPP (map))
27993 map = Fget_text_property (pos, Qkeymap, string);
27994 if (!KEYMAPP (map))
27995 cursor = dpyinfo->vertical_scroll_bar_cursor;
27996 }
27997 }
27998 else
27999 /* Default mode-line pointer. */
28000 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28001 }
28002 #endif
28003 }
28004
28005 /* Change the mouse face according to what is under X/Y. */
28006 if (STRINGP (string))
28007 {
28008 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28009 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28010 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28011 && glyph)
28012 {
28013 Lisp_Object b, e;
28014
28015 struct glyph * tmp_glyph;
28016
28017 int gpos;
28018 int gseq_length;
28019 int total_pixel_width;
28020 ptrdiff_t begpos, endpos, ignore;
28021
28022 int vpos, hpos;
28023
28024 b = Fprevious_single_property_change (make_number (charpos + 1),
28025 Qmouse_face, string, Qnil);
28026 if (NILP (b))
28027 begpos = 0;
28028 else
28029 begpos = XINT (b);
28030
28031 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28032 if (NILP (e))
28033 endpos = SCHARS (string);
28034 else
28035 endpos = XINT (e);
28036
28037 /* Calculate the glyph position GPOS of GLYPH in the
28038 displayed string, relative to the beginning of the
28039 highlighted part of the string.
28040
28041 Note: GPOS is different from CHARPOS. CHARPOS is the
28042 position of GLYPH in the internal string object. A mode
28043 line string format has structures which are converted to
28044 a flattened string by the Emacs Lisp interpreter. The
28045 internal string is an element of those structures. The
28046 displayed string is the flattened string. */
28047 tmp_glyph = row_start_glyph;
28048 while (tmp_glyph < glyph
28049 && (!(EQ (tmp_glyph->object, glyph->object)
28050 && begpos <= tmp_glyph->charpos
28051 && tmp_glyph->charpos < endpos)))
28052 tmp_glyph++;
28053 gpos = glyph - tmp_glyph;
28054
28055 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28056 the highlighted part of the displayed string to which
28057 GLYPH belongs. Note: GSEQ_LENGTH is different from
28058 SCHARS (STRING), because the latter returns the length of
28059 the internal string. */
28060 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28061 tmp_glyph > glyph
28062 && (!(EQ (tmp_glyph->object, glyph->object)
28063 && begpos <= tmp_glyph->charpos
28064 && tmp_glyph->charpos < endpos));
28065 tmp_glyph--)
28066 ;
28067 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28068
28069 /* Calculate the total pixel width of all the glyphs between
28070 the beginning of the highlighted area and GLYPH. */
28071 total_pixel_width = 0;
28072 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28073 total_pixel_width += tmp_glyph->pixel_width;
28074
28075 /* Pre calculation of re-rendering position. Note: X is in
28076 column units here, after the call to mode_line_string or
28077 marginal_area_string. */
28078 hpos = x - gpos;
28079 vpos = (area == ON_MODE_LINE
28080 ? (w->current_matrix)->nrows - 1
28081 : 0);
28082
28083 /* If GLYPH's position is included in the region that is
28084 already drawn in mouse face, we have nothing to do. */
28085 if ( EQ (window, hlinfo->mouse_face_window)
28086 && (!row->reversed_p
28087 ? (hlinfo->mouse_face_beg_col <= hpos
28088 && hpos < hlinfo->mouse_face_end_col)
28089 /* In R2L rows we swap BEG and END, see below. */
28090 : (hlinfo->mouse_face_end_col <= hpos
28091 && hpos < hlinfo->mouse_face_beg_col))
28092 && hlinfo->mouse_face_beg_row == vpos )
28093 return;
28094
28095 if (clear_mouse_face (hlinfo))
28096 cursor = No_Cursor;
28097
28098 if (!row->reversed_p)
28099 {
28100 hlinfo->mouse_face_beg_col = hpos;
28101 hlinfo->mouse_face_beg_x = original_x_pixel
28102 - (total_pixel_width + dx);
28103 hlinfo->mouse_face_end_col = hpos + gseq_length;
28104 hlinfo->mouse_face_end_x = 0;
28105 }
28106 else
28107 {
28108 /* In R2L rows, show_mouse_face expects BEG and END
28109 coordinates to be swapped. */
28110 hlinfo->mouse_face_end_col = hpos;
28111 hlinfo->mouse_face_end_x = original_x_pixel
28112 - (total_pixel_width + dx);
28113 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28114 hlinfo->mouse_face_beg_x = 0;
28115 }
28116
28117 hlinfo->mouse_face_beg_row = vpos;
28118 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28119 hlinfo->mouse_face_past_end = 0;
28120 hlinfo->mouse_face_window = window;
28121
28122 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28123 charpos,
28124 0, &ignore,
28125 glyph->face_id,
28126 1);
28127 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28128
28129 if (NILP (pointer))
28130 pointer = Qhand;
28131 }
28132 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28133 clear_mouse_face (hlinfo);
28134 }
28135 #ifdef HAVE_WINDOW_SYSTEM
28136 if (FRAME_WINDOW_P (f))
28137 define_frame_cursor1 (f, cursor, pointer);
28138 #endif
28139 }
28140
28141
28142 /* EXPORT:
28143 Take proper action when the mouse has moved to position X, Y on
28144 frame F with regards to highlighting portions of display that have
28145 mouse-face properties. Also de-highlight portions of display where
28146 the mouse was before, set the mouse pointer shape as appropriate
28147 for the mouse coordinates, and activate help echo (tooltips).
28148 X and Y can be negative or out of range. */
28149
28150 void
28151 note_mouse_highlight (struct frame *f, int x, int y)
28152 {
28153 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28154 enum window_part part = ON_NOTHING;
28155 Lisp_Object window;
28156 struct window *w;
28157 Cursor cursor = No_Cursor;
28158 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28159 struct buffer *b;
28160
28161 /* When a menu is active, don't highlight because this looks odd. */
28162 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28163 if (popup_activated ())
28164 return;
28165 #endif
28166
28167 if (!f->glyphs_initialized_p
28168 || f->pointer_invisible)
28169 return;
28170
28171 hlinfo->mouse_face_mouse_x = x;
28172 hlinfo->mouse_face_mouse_y = y;
28173 hlinfo->mouse_face_mouse_frame = f;
28174
28175 if (hlinfo->mouse_face_defer)
28176 return;
28177
28178 /* Which window is that in? */
28179 window = window_from_coordinates (f, x, y, &part, 1);
28180
28181 /* If displaying active text in another window, clear that. */
28182 if (! EQ (window, hlinfo->mouse_face_window)
28183 /* Also clear if we move out of text area in same window. */
28184 || (!NILP (hlinfo->mouse_face_window)
28185 && !NILP (window)
28186 && part != ON_TEXT
28187 && part != ON_MODE_LINE
28188 && part != ON_HEADER_LINE))
28189 clear_mouse_face (hlinfo);
28190
28191 /* Not on a window -> return. */
28192 if (!WINDOWP (window))
28193 return;
28194
28195 /* Reset help_echo_string. It will get recomputed below. */
28196 help_echo_string = Qnil;
28197
28198 /* Convert to window-relative pixel coordinates. */
28199 w = XWINDOW (window);
28200 frame_to_window_pixel_xy (w, &x, &y);
28201
28202 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28203 /* Handle tool-bar window differently since it doesn't display a
28204 buffer. */
28205 if (EQ (window, f->tool_bar_window))
28206 {
28207 note_tool_bar_highlight (f, x, y);
28208 return;
28209 }
28210 #endif
28211
28212 /* Mouse is on the mode, header line or margin? */
28213 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28214 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28215 {
28216 note_mode_line_or_margin_highlight (window, x, y, part);
28217 return;
28218 }
28219
28220 #ifdef HAVE_WINDOW_SYSTEM
28221 if (part == ON_VERTICAL_BORDER)
28222 {
28223 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28224 help_echo_string = build_string ("drag-mouse-1: resize");
28225 }
28226 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28227 || part == ON_SCROLL_BAR)
28228 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28229 else
28230 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28231 #endif
28232
28233 /* Are we in a window whose display is up to date?
28234 And verify the buffer's text has not changed. */
28235 b = XBUFFER (w->contents);
28236 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28237 {
28238 int hpos, vpos, dx, dy, area = LAST_AREA;
28239 ptrdiff_t pos;
28240 struct glyph *glyph;
28241 Lisp_Object object;
28242 Lisp_Object mouse_face = Qnil, position;
28243 Lisp_Object *overlay_vec = NULL;
28244 ptrdiff_t i, noverlays;
28245 struct buffer *obuf;
28246 ptrdiff_t obegv, ozv;
28247 int same_region;
28248
28249 /* Find the glyph under X/Y. */
28250 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28251
28252 #ifdef HAVE_WINDOW_SYSTEM
28253 /* Look for :pointer property on image. */
28254 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28255 {
28256 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28257 if (img != NULL && IMAGEP (img->spec))
28258 {
28259 Lisp_Object image_map, hotspot;
28260 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28261 !NILP (image_map))
28262 && (hotspot = find_hot_spot (image_map,
28263 glyph->slice.img.x + dx,
28264 glyph->slice.img.y + dy),
28265 CONSP (hotspot))
28266 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28267 {
28268 Lisp_Object plist;
28269
28270 /* Could check XCAR (hotspot) to see if we enter/leave
28271 this hot-spot.
28272 If so, we could look for mouse-enter, mouse-leave
28273 properties in PLIST (and do something...). */
28274 hotspot = XCDR (hotspot);
28275 if (CONSP (hotspot)
28276 && (plist = XCAR (hotspot), CONSP (plist)))
28277 {
28278 pointer = Fplist_get (plist, Qpointer);
28279 if (NILP (pointer))
28280 pointer = Qhand;
28281 help_echo_string = Fplist_get (plist, Qhelp_echo);
28282 if (!NILP (help_echo_string))
28283 {
28284 help_echo_window = window;
28285 help_echo_object = glyph->object;
28286 help_echo_pos = glyph->charpos;
28287 }
28288 }
28289 }
28290 if (NILP (pointer))
28291 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28292 }
28293 }
28294 #endif /* HAVE_WINDOW_SYSTEM */
28295
28296 /* Clear mouse face if X/Y not over text. */
28297 if (glyph == NULL
28298 || area != TEXT_AREA
28299 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28300 /* Glyph's OBJECT is an integer for glyphs inserted by the
28301 display engine for its internal purposes, like truncation
28302 and continuation glyphs and blanks beyond the end of
28303 line's text on text terminals. If we are over such a
28304 glyph, we are not over any text. */
28305 || INTEGERP (glyph->object)
28306 /* R2L rows have a stretch glyph at their front, which
28307 stands for no text, whereas L2R rows have no glyphs at
28308 all beyond the end of text. Treat such stretch glyphs
28309 like we do with NULL glyphs in L2R rows. */
28310 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28311 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28312 && glyph->type == STRETCH_GLYPH
28313 && glyph->avoid_cursor_p))
28314 {
28315 if (clear_mouse_face (hlinfo))
28316 cursor = No_Cursor;
28317 #ifdef HAVE_WINDOW_SYSTEM
28318 if (FRAME_WINDOW_P (f) && NILP (pointer))
28319 {
28320 if (area != TEXT_AREA)
28321 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28322 else
28323 pointer = Vvoid_text_area_pointer;
28324 }
28325 #endif
28326 goto set_cursor;
28327 }
28328
28329 pos = glyph->charpos;
28330 object = glyph->object;
28331 if (!STRINGP (object) && !BUFFERP (object))
28332 goto set_cursor;
28333
28334 /* If we get an out-of-range value, return now; avoid an error. */
28335 if (BUFFERP (object) && pos > BUF_Z (b))
28336 goto set_cursor;
28337
28338 /* Make the window's buffer temporarily current for
28339 overlays_at and compute_char_face. */
28340 obuf = current_buffer;
28341 current_buffer = b;
28342 obegv = BEGV;
28343 ozv = ZV;
28344 BEGV = BEG;
28345 ZV = Z;
28346
28347 /* Is this char mouse-active or does it have help-echo? */
28348 position = make_number (pos);
28349
28350 if (BUFFERP (object))
28351 {
28352 /* Put all the overlays we want in a vector in overlay_vec. */
28353 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28354 /* Sort overlays into increasing priority order. */
28355 noverlays = sort_overlays (overlay_vec, noverlays, w);
28356 }
28357 else
28358 noverlays = 0;
28359
28360 if (NILP (Vmouse_highlight))
28361 {
28362 clear_mouse_face (hlinfo);
28363 goto check_help_echo;
28364 }
28365
28366 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28367
28368 if (same_region)
28369 cursor = No_Cursor;
28370
28371 /* Check mouse-face highlighting. */
28372 if (! same_region
28373 /* If there exists an overlay with mouse-face overlapping
28374 the one we are currently highlighting, we have to
28375 check if we enter the overlapping overlay, and then
28376 highlight only that. */
28377 || (OVERLAYP (hlinfo->mouse_face_overlay)
28378 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28379 {
28380 /* Find the highest priority overlay with a mouse-face. */
28381 Lisp_Object overlay = Qnil;
28382 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28383 {
28384 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28385 if (!NILP (mouse_face))
28386 overlay = overlay_vec[i];
28387 }
28388
28389 /* If we're highlighting the same overlay as before, there's
28390 no need to do that again. */
28391 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28392 goto check_help_echo;
28393 hlinfo->mouse_face_overlay = overlay;
28394
28395 /* Clear the display of the old active region, if any. */
28396 if (clear_mouse_face (hlinfo))
28397 cursor = No_Cursor;
28398
28399 /* If no overlay applies, get a text property. */
28400 if (NILP (overlay))
28401 mouse_face = Fget_text_property (position, Qmouse_face, object);
28402
28403 /* Next, compute the bounds of the mouse highlighting and
28404 display it. */
28405 if (!NILP (mouse_face) && STRINGP (object))
28406 {
28407 /* The mouse-highlighting comes from a display string
28408 with a mouse-face. */
28409 Lisp_Object s, e;
28410 ptrdiff_t ignore;
28411
28412 s = Fprevious_single_property_change
28413 (make_number (pos + 1), Qmouse_face, object, Qnil);
28414 e = Fnext_single_property_change
28415 (position, Qmouse_face, object, Qnil);
28416 if (NILP (s))
28417 s = make_number (0);
28418 if (NILP (e))
28419 e = make_number (SCHARS (object));
28420 mouse_face_from_string_pos (w, hlinfo, object,
28421 XINT (s), XINT (e));
28422 hlinfo->mouse_face_past_end = 0;
28423 hlinfo->mouse_face_window = window;
28424 hlinfo->mouse_face_face_id
28425 = face_at_string_position (w, object, pos, 0, &ignore,
28426 glyph->face_id, 1);
28427 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28428 cursor = No_Cursor;
28429 }
28430 else
28431 {
28432 /* The mouse-highlighting, if any, comes from an overlay
28433 or text property in the buffer. */
28434 Lisp_Object buffer IF_LINT (= Qnil);
28435 Lisp_Object disp_string IF_LINT (= Qnil);
28436
28437 if (STRINGP (object))
28438 {
28439 /* If we are on a display string with no mouse-face,
28440 check if the text under it has one. */
28441 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28442 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28443 pos = string_buffer_position (object, start);
28444 if (pos > 0)
28445 {
28446 mouse_face = get_char_property_and_overlay
28447 (make_number (pos), Qmouse_face, w->contents, &overlay);
28448 buffer = w->contents;
28449 disp_string = object;
28450 }
28451 }
28452 else
28453 {
28454 buffer = object;
28455 disp_string = Qnil;
28456 }
28457
28458 if (!NILP (mouse_face))
28459 {
28460 Lisp_Object before, after;
28461 Lisp_Object before_string, after_string;
28462 /* To correctly find the limits of mouse highlight
28463 in a bidi-reordered buffer, we must not use the
28464 optimization of limiting the search in
28465 previous-single-property-change and
28466 next-single-property-change, because
28467 rows_from_pos_range needs the real start and end
28468 positions to DTRT in this case. That's because
28469 the first row visible in a window does not
28470 necessarily display the character whose position
28471 is the smallest. */
28472 Lisp_Object lim1
28473 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28474 ? Fmarker_position (w->start)
28475 : Qnil;
28476 Lisp_Object lim2
28477 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28478 ? make_number (BUF_Z (XBUFFER (buffer))
28479 - w->window_end_pos)
28480 : Qnil;
28481
28482 if (NILP (overlay))
28483 {
28484 /* Handle the text property case. */
28485 before = Fprevious_single_property_change
28486 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28487 after = Fnext_single_property_change
28488 (make_number (pos), Qmouse_face, buffer, lim2);
28489 before_string = after_string = Qnil;
28490 }
28491 else
28492 {
28493 /* Handle the overlay case. */
28494 before = Foverlay_start (overlay);
28495 after = Foverlay_end (overlay);
28496 before_string = Foverlay_get (overlay, Qbefore_string);
28497 after_string = Foverlay_get (overlay, Qafter_string);
28498
28499 if (!STRINGP (before_string)) before_string = Qnil;
28500 if (!STRINGP (after_string)) after_string = Qnil;
28501 }
28502
28503 mouse_face_from_buffer_pos (window, hlinfo, pos,
28504 NILP (before)
28505 ? 1
28506 : XFASTINT (before),
28507 NILP (after)
28508 ? BUF_Z (XBUFFER (buffer))
28509 : XFASTINT (after),
28510 before_string, after_string,
28511 disp_string);
28512 cursor = No_Cursor;
28513 }
28514 }
28515 }
28516
28517 check_help_echo:
28518
28519 /* Look for a `help-echo' property. */
28520 if (NILP (help_echo_string)) {
28521 Lisp_Object help, overlay;
28522
28523 /* Check overlays first. */
28524 help = overlay = Qnil;
28525 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28526 {
28527 overlay = overlay_vec[i];
28528 help = Foverlay_get (overlay, Qhelp_echo);
28529 }
28530
28531 if (!NILP (help))
28532 {
28533 help_echo_string = help;
28534 help_echo_window = window;
28535 help_echo_object = overlay;
28536 help_echo_pos = pos;
28537 }
28538 else
28539 {
28540 Lisp_Object obj = glyph->object;
28541 ptrdiff_t charpos = glyph->charpos;
28542
28543 /* Try text properties. */
28544 if (STRINGP (obj)
28545 && charpos >= 0
28546 && charpos < SCHARS (obj))
28547 {
28548 help = Fget_text_property (make_number (charpos),
28549 Qhelp_echo, obj);
28550 if (NILP (help))
28551 {
28552 /* If the string itself doesn't specify a help-echo,
28553 see if the buffer text ``under'' it does. */
28554 struct glyph_row *r
28555 = MATRIX_ROW (w->current_matrix, vpos);
28556 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28557 ptrdiff_t p = string_buffer_position (obj, start);
28558 if (p > 0)
28559 {
28560 help = Fget_char_property (make_number (p),
28561 Qhelp_echo, w->contents);
28562 if (!NILP (help))
28563 {
28564 charpos = p;
28565 obj = w->contents;
28566 }
28567 }
28568 }
28569 }
28570 else if (BUFFERP (obj)
28571 && charpos >= BEGV
28572 && charpos < ZV)
28573 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28574 obj);
28575
28576 if (!NILP (help))
28577 {
28578 help_echo_string = help;
28579 help_echo_window = window;
28580 help_echo_object = obj;
28581 help_echo_pos = charpos;
28582 }
28583 }
28584 }
28585
28586 #ifdef HAVE_WINDOW_SYSTEM
28587 /* Look for a `pointer' property. */
28588 if (FRAME_WINDOW_P (f) && NILP (pointer))
28589 {
28590 /* Check overlays first. */
28591 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28592 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28593
28594 if (NILP (pointer))
28595 {
28596 Lisp_Object obj = glyph->object;
28597 ptrdiff_t charpos = glyph->charpos;
28598
28599 /* Try text properties. */
28600 if (STRINGP (obj)
28601 && charpos >= 0
28602 && charpos < SCHARS (obj))
28603 {
28604 pointer = Fget_text_property (make_number (charpos),
28605 Qpointer, obj);
28606 if (NILP (pointer))
28607 {
28608 /* If the string itself doesn't specify a pointer,
28609 see if the buffer text ``under'' it does. */
28610 struct glyph_row *r
28611 = MATRIX_ROW (w->current_matrix, vpos);
28612 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28613 ptrdiff_t p = string_buffer_position (obj, start);
28614 if (p > 0)
28615 pointer = Fget_char_property (make_number (p),
28616 Qpointer, w->contents);
28617 }
28618 }
28619 else if (BUFFERP (obj)
28620 && charpos >= BEGV
28621 && charpos < ZV)
28622 pointer = Fget_text_property (make_number (charpos),
28623 Qpointer, obj);
28624 }
28625 }
28626 #endif /* HAVE_WINDOW_SYSTEM */
28627
28628 BEGV = obegv;
28629 ZV = ozv;
28630 current_buffer = obuf;
28631 }
28632
28633 set_cursor:
28634
28635 #ifdef HAVE_WINDOW_SYSTEM
28636 if (FRAME_WINDOW_P (f))
28637 define_frame_cursor1 (f, cursor, pointer);
28638 #else
28639 /* This is here to prevent a compiler error, about "label at end of
28640 compound statement". */
28641 return;
28642 #endif
28643 }
28644
28645
28646 /* EXPORT for RIF:
28647 Clear any mouse-face on window W. This function is part of the
28648 redisplay interface, and is called from try_window_id and similar
28649 functions to ensure the mouse-highlight is off. */
28650
28651 void
28652 x_clear_window_mouse_face (struct window *w)
28653 {
28654 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28655 Lisp_Object window;
28656
28657 block_input ();
28658 XSETWINDOW (window, w);
28659 if (EQ (window, hlinfo->mouse_face_window))
28660 clear_mouse_face (hlinfo);
28661 unblock_input ();
28662 }
28663
28664
28665 /* EXPORT:
28666 Just discard the mouse face information for frame F, if any.
28667 This is used when the size of F is changed. */
28668
28669 void
28670 cancel_mouse_face (struct frame *f)
28671 {
28672 Lisp_Object window;
28673 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28674
28675 window = hlinfo->mouse_face_window;
28676 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28677 reset_mouse_highlight (hlinfo);
28678 }
28679
28680
28681 \f
28682 /***********************************************************************
28683 Exposure Events
28684 ***********************************************************************/
28685
28686 #ifdef HAVE_WINDOW_SYSTEM
28687
28688 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28689 which intersects rectangle R. R is in window-relative coordinates. */
28690
28691 static void
28692 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28693 enum glyph_row_area area)
28694 {
28695 struct glyph *first = row->glyphs[area];
28696 struct glyph *end = row->glyphs[area] + row->used[area];
28697 struct glyph *last;
28698 int first_x, start_x, x;
28699
28700 if (area == TEXT_AREA && row->fill_line_p)
28701 /* If row extends face to end of line write the whole line. */
28702 draw_glyphs (w, 0, row, area,
28703 0, row->used[area],
28704 DRAW_NORMAL_TEXT, 0);
28705 else
28706 {
28707 /* Set START_X to the window-relative start position for drawing glyphs of
28708 AREA. The first glyph of the text area can be partially visible.
28709 The first glyphs of other areas cannot. */
28710 start_x = window_box_left_offset (w, area);
28711 x = start_x;
28712 if (area == TEXT_AREA)
28713 x += row->x;
28714
28715 /* Find the first glyph that must be redrawn. */
28716 while (first < end
28717 && x + first->pixel_width < r->x)
28718 {
28719 x += first->pixel_width;
28720 ++first;
28721 }
28722
28723 /* Find the last one. */
28724 last = first;
28725 first_x = x;
28726 while (last < end
28727 && x < r->x + r->width)
28728 {
28729 x += last->pixel_width;
28730 ++last;
28731 }
28732
28733 /* Repaint. */
28734 if (last > first)
28735 draw_glyphs (w, first_x - start_x, row, area,
28736 first - row->glyphs[area], last - row->glyphs[area],
28737 DRAW_NORMAL_TEXT, 0);
28738 }
28739 }
28740
28741
28742 /* Redraw the parts of the glyph row ROW on window W intersecting
28743 rectangle R. R is in window-relative coordinates. Value is
28744 non-zero if mouse-face was overwritten. */
28745
28746 static int
28747 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28748 {
28749 eassert (row->enabled_p);
28750
28751 if (row->mode_line_p || w->pseudo_window_p)
28752 draw_glyphs (w, 0, row, TEXT_AREA,
28753 0, row->used[TEXT_AREA],
28754 DRAW_NORMAL_TEXT, 0);
28755 else
28756 {
28757 if (row->used[LEFT_MARGIN_AREA])
28758 expose_area (w, row, r, LEFT_MARGIN_AREA);
28759 if (row->used[TEXT_AREA])
28760 expose_area (w, row, r, TEXT_AREA);
28761 if (row->used[RIGHT_MARGIN_AREA])
28762 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28763 draw_row_fringe_bitmaps (w, row);
28764 }
28765
28766 return row->mouse_face_p;
28767 }
28768
28769
28770 /* Redraw those parts of glyphs rows during expose event handling that
28771 overlap other rows. Redrawing of an exposed line writes over parts
28772 of lines overlapping that exposed line; this function fixes that.
28773
28774 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28775 row in W's current matrix that is exposed and overlaps other rows.
28776 LAST_OVERLAPPING_ROW is the last such row. */
28777
28778 static void
28779 expose_overlaps (struct window *w,
28780 struct glyph_row *first_overlapping_row,
28781 struct glyph_row *last_overlapping_row,
28782 XRectangle *r)
28783 {
28784 struct glyph_row *row;
28785
28786 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28787 if (row->overlapping_p)
28788 {
28789 eassert (row->enabled_p && !row->mode_line_p);
28790
28791 row->clip = r;
28792 if (row->used[LEFT_MARGIN_AREA])
28793 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28794
28795 if (row->used[TEXT_AREA])
28796 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28797
28798 if (row->used[RIGHT_MARGIN_AREA])
28799 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28800 row->clip = NULL;
28801 }
28802 }
28803
28804
28805 /* Return non-zero if W's cursor intersects rectangle R. */
28806
28807 static int
28808 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28809 {
28810 XRectangle cr, result;
28811 struct glyph *cursor_glyph;
28812 struct glyph_row *row;
28813
28814 if (w->phys_cursor.vpos >= 0
28815 && w->phys_cursor.vpos < w->current_matrix->nrows
28816 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28817 row->enabled_p)
28818 && row->cursor_in_fringe_p)
28819 {
28820 /* Cursor is in the fringe. */
28821 cr.x = window_box_right_offset (w,
28822 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28823 ? RIGHT_MARGIN_AREA
28824 : TEXT_AREA));
28825 cr.y = row->y;
28826 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28827 cr.height = row->height;
28828 return x_intersect_rectangles (&cr, r, &result);
28829 }
28830
28831 cursor_glyph = get_phys_cursor_glyph (w);
28832 if (cursor_glyph)
28833 {
28834 /* r is relative to W's box, but w->phys_cursor.x is relative
28835 to left edge of W's TEXT area. Adjust it. */
28836 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28837 cr.y = w->phys_cursor.y;
28838 cr.width = cursor_glyph->pixel_width;
28839 cr.height = w->phys_cursor_height;
28840 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28841 I assume the effect is the same -- and this is portable. */
28842 return x_intersect_rectangles (&cr, r, &result);
28843 }
28844 /* If we don't understand the format, pretend we're not in the hot-spot. */
28845 return 0;
28846 }
28847
28848
28849 /* EXPORT:
28850 Draw a vertical window border to the right of window W if W doesn't
28851 have vertical scroll bars. */
28852
28853 void
28854 x_draw_vertical_border (struct window *w)
28855 {
28856 struct frame *f = XFRAME (WINDOW_FRAME (w));
28857
28858 /* We could do better, if we knew what type of scroll-bar the adjacent
28859 windows (on either side) have... But we don't :-(
28860 However, I think this works ok. ++KFS 2003-04-25 */
28861
28862 /* Redraw borders between horizontally adjacent windows. Don't
28863 do it for frames with vertical scroll bars because either the
28864 right scroll bar of a window, or the left scroll bar of its
28865 neighbor will suffice as a border. */
28866 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28867 return;
28868
28869 /* Note: It is necessary to redraw both the left and the right
28870 borders, for when only this single window W is being
28871 redisplayed. */
28872 if (!WINDOW_RIGHTMOST_P (w)
28873 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28874 {
28875 int x0, x1, y0, y1;
28876
28877 window_box_edges (w, &x0, &y0, &x1, &y1);
28878 y1 -= 1;
28879
28880 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28881 x1 -= 1;
28882
28883 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28884 }
28885 if (!WINDOW_LEFTMOST_P (w)
28886 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28887 {
28888 int x0, x1, y0, y1;
28889
28890 window_box_edges (w, &x0, &y0, &x1, &y1);
28891 y1 -= 1;
28892
28893 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28894 x0 -= 1;
28895
28896 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28897 }
28898 }
28899
28900
28901 /* Redraw the part of window W intersection rectangle FR. Pixel
28902 coordinates in FR are frame-relative. Call this function with
28903 input blocked. Value is non-zero if the exposure overwrites
28904 mouse-face. */
28905
28906 static int
28907 expose_window (struct window *w, XRectangle *fr)
28908 {
28909 struct frame *f = XFRAME (w->frame);
28910 XRectangle wr, r;
28911 int mouse_face_overwritten_p = 0;
28912
28913 /* If window is not yet fully initialized, do nothing. This can
28914 happen when toolkit scroll bars are used and a window is split.
28915 Reconfiguring the scroll bar will generate an expose for a newly
28916 created window. */
28917 if (w->current_matrix == NULL)
28918 return 0;
28919
28920 /* When we're currently updating the window, display and current
28921 matrix usually don't agree. Arrange for a thorough display
28922 later. */
28923 if (w->must_be_updated_p)
28924 {
28925 SET_FRAME_GARBAGED (f);
28926 return 0;
28927 }
28928
28929 /* Frame-relative pixel rectangle of W. */
28930 wr.x = WINDOW_LEFT_EDGE_X (w);
28931 wr.y = WINDOW_TOP_EDGE_Y (w);
28932 wr.width = WINDOW_TOTAL_WIDTH (w);
28933 wr.height = WINDOW_TOTAL_HEIGHT (w);
28934
28935 if (x_intersect_rectangles (fr, &wr, &r))
28936 {
28937 int yb = window_text_bottom_y (w);
28938 struct glyph_row *row;
28939 int cursor_cleared_p, phys_cursor_on_p;
28940 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28941
28942 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28943 r.x, r.y, r.width, r.height));
28944
28945 /* Convert to window coordinates. */
28946 r.x -= WINDOW_LEFT_EDGE_X (w);
28947 r.y -= WINDOW_TOP_EDGE_Y (w);
28948
28949 /* Turn off the cursor. */
28950 if (!w->pseudo_window_p
28951 && phys_cursor_in_rect_p (w, &r))
28952 {
28953 x_clear_cursor (w);
28954 cursor_cleared_p = 1;
28955 }
28956 else
28957 cursor_cleared_p = 0;
28958
28959 /* If the row containing the cursor extends face to end of line,
28960 then expose_area might overwrite the cursor outside the
28961 rectangle and thus notice_overwritten_cursor might clear
28962 w->phys_cursor_on_p. We remember the original value and
28963 check later if it is changed. */
28964 phys_cursor_on_p = w->phys_cursor_on_p;
28965
28966 /* Update lines intersecting rectangle R. */
28967 first_overlapping_row = last_overlapping_row = NULL;
28968 for (row = w->current_matrix->rows;
28969 row->enabled_p;
28970 ++row)
28971 {
28972 int y0 = row->y;
28973 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28974
28975 if ((y0 >= r.y && y0 < r.y + r.height)
28976 || (y1 > r.y && y1 < r.y + r.height)
28977 || (r.y >= y0 && r.y < y1)
28978 || (r.y + r.height > y0 && r.y + r.height < y1))
28979 {
28980 /* A header line may be overlapping, but there is no need
28981 to fix overlapping areas for them. KFS 2005-02-12 */
28982 if (row->overlapping_p && !row->mode_line_p)
28983 {
28984 if (first_overlapping_row == NULL)
28985 first_overlapping_row = row;
28986 last_overlapping_row = row;
28987 }
28988
28989 row->clip = fr;
28990 if (expose_line (w, row, &r))
28991 mouse_face_overwritten_p = 1;
28992 row->clip = NULL;
28993 }
28994 else if (row->overlapping_p)
28995 {
28996 /* We must redraw a row overlapping the exposed area. */
28997 if (y0 < r.y
28998 ? y0 + row->phys_height > r.y
28999 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29000 {
29001 if (first_overlapping_row == NULL)
29002 first_overlapping_row = row;
29003 last_overlapping_row = row;
29004 }
29005 }
29006
29007 if (y1 >= yb)
29008 break;
29009 }
29010
29011 /* Display the mode line if there is one. */
29012 if (WINDOW_WANTS_MODELINE_P (w)
29013 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29014 row->enabled_p)
29015 && row->y < r.y + r.height)
29016 {
29017 if (expose_line (w, row, &r))
29018 mouse_face_overwritten_p = 1;
29019 }
29020
29021 if (!w->pseudo_window_p)
29022 {
29023 /* Fix the display of overlapping rows. */
29024 if (first_overlapping_row)
29025 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29026 fr);
29027
29028 /* Draw border between windows. */
29029 x_draw_vertical_border (w);
29030
29031 /* Turn the cursor on again. */
29032 if (cursor_cleared_p
29033 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29034 update_window_cursor (w, 1);
29035 }
29036 }
29037
29038 return mouse_face_overwritten_p;
29039 }
29040
29041
29042
29043 /* Redraw (parts) of all windows in the window tree rooted at W that
29044 intersect R. R contains frame pixel coordinates. Value is
29045 non-zero if the exposure overwrites mouse-face. */
29046
29047 static int
29048 expose_window_tree (struct window *w, XRectangle *r)
29049 {
29050 struct frame *f = XFRAME (w->frame);
29051 int mouse_face_overwritten_p = 0;
29052
29053 while (w && !FRAME_GARBAGED_P (f))
29054 {
29055 if (WINDOWP (w->contents))
29056 mouse_face_overwritten_p
29057 |= expose_window_tree (XWINDOW (w->contents), r);
29058 else
29059 mouse_face_overwritten_p |= expose_window (w, r);
29060
29061 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29062 }
29063
29064 return mouse_face_overwritten_p;
29065 }
29066
29067
29068 /* EXPORT:
29069 Redisplay an exposed area of frame F. X and Y are the upper-left
29070 corner of the exposed rectangle. W and H are width and height of
29071 the exposed area. All are pixel values. W or H zero means redraw
29072 the entire frame. */
29073
29074 void
29075 expose_frame (struct frame *f, int x, int y, int w, int h)
29076 {
29077 XRectangle r;
29078 int mouse_face_overwritten_p = 0;
29079
29080 TRACE ((stderr, "expose_frame "));
29081
29082 /* No need to redraw if frame will be redrawn soon. */
29083 if (FRAME_GARBAGED_P (f))
29084 {
29085 TRACE ((stderr, " garbaged\n"));
29086 return;
29087 }
29088
29089 /* If basic faces haven't been realized yet, there is no point in
29090 trying to redraw anything. This can happen when we get an expose
29091 event while Emacs is starting, e.g. by moving another window. */
29092 if (FRAME_FACE_CACHE (f) == NULL
29093 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29094 {
29095 TRACE ((stderr, " no faces\n"));
29096 return;
29097 }
29098
29099 if (w == 0 || h == 0)
29100 {
29101 r.x = r.y = 0;
29102 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29103 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29104 }
29105 else
29106 {
29107 r.x = x;
29108 r.y = y;
29109 r.width = w;
29110 r.height = h;
29111 }
29112
29113 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29114 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29115
29116 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29117 if (WINDOWP (f->tool_bar_window))
29118 mouse_face_overwritten_p
29119 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29120 #endif
29121
29122 #ifdef HAVE_X_WINDOWS
29123 #ifndef MSDOS
29124 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29125 if (WINDOWP (f->menu_bar_window))
29126 mouse_face_overwritten_p
29127 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29128 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29129 #endif
29130 #endif
29131
29132 /* Some window managers support a focus-follows-mouse style with
29133 delayed raising of frames. Imagine a partially obscured frame,
29134 and moving the mouse into partially obscured mouse-face on that
29135 frame. The visible part of the mouse-face will be highlighted,
29136 then the WM raises the obscured frame. With at least one WM, KDE
29137 2.1, Emacs is not getting any event for the raising of the frame
29138 (even tried with SubstructureRedirectMask), only Expose events.
29139 These expose events will draw text normally, i.e. not
29140 highlighted. Which means we must redo the highlight here.
29141 Subsume it under ``we love X''. --gerd 2001-08-15 */
29142 /* Included in Windows version because Windows most likely does not
29143 do the right thing if any third party tool offers
29144 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29145 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29146 {
29147 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29148 if (f == hlinfo->mouse_face_mouse_frame)
29149 {
29150 int mouse_x = hlinfo->mouse_face_mouse_x;
29151 int mouse_y = hlinfo->mouse_face_mouse_y;
29152 clear_mouse_face (hlinfo);
29153 note_mouse_highlight (f, mouse_x, mouse_y);
29154 }
29155 }
29156 }
29157
29158
29159 /* EXPORT:
29160 Determine the intersection of two rectangles R1 and R2. Return
29161 the intersection in *RESULT. Value is non-zero if RESULT is not
29162 empty. */
29163
29164 int
29165 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29166 {
29167 XRectangle *left, *right;
29168 XRectangle *upper, *lower;
29169 int intersection_p = 0;
29170
29171 /* Rearrange so that R1 is the left-most rectangle. */
29172 if (r1->x < r2->x)
29173 left = r1, right = r2;
29174 else
29175 left = r2, right = r1;
29176
29177 /* X0 of the intersection is right.x0, if this is inside R1,
29178 otherwise there is no intersection. */
29179 if (right->x <= left->x + left->width)
29180 {
29181 result->x = right->x;
29182
29183 /* The right end of the intersection is the minimum of
29184 the right ends of left and right. */
29185 result->width = (min (left->x + left->width, right->x + right->width)
29186 - result->x);
29187
29188 /* Same game for Y. */
29189 if (r1->y < r2->y)
29190 upper = r1, lower = r2;
29191 else
29192 upper = r2, lower = r1;
29193
29194 /* The upper end of the intersection is lower.y0, if this is inside
29195 of upper. Otherwise, there is no intersection. */
29196 if (lower->y <= upper->y + upper->height)
29197 {
29198 result->y = lower->y;
29199
29200 /* The lower end of the intersection is the minimum of the lower
29201 ends of upper and lower. */
29202 result->height = (min (lower->y + lower->height,
29203 upper->y + upper->height)
29204 - result->y);
29205 intersection_p = 1;
29206 }
29207 }
29208
29209 return intersection_p;
29210 }
29211
29212 #endif /* HAVE_WINDOW_SYSTEM */
29213
29214 \f
29215 /***********************************************************************
29216 Initialization
29217 ***********************************************************************/
29218
29219 void
29220 syms_of_xdisp (void)
29221 {
29222 Vwith_echo_area_save_vector = Qnil;
29223 staticpro (&Vwith_echo_area_save_vector);
29224
29225 Vmessage_stack = Qnil;
29226 staticpro (&Vmessage_stack);
29227
29228 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29229 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29230
29231 message_dolog_marker1 = Fmake_marker ();
29232 staticpro (&message_dolog_marker1);
29233 message_dolog_marker2 = Fmake_marker ();
29234 staticpro (&message_dolog_marker2);
29235 message_dolog_marker3 = Fmake_marker ();
29236 staticpro (&message_dolog_marker3);
29237
29238 #ifdef GLYPH_DEBUG
29239 defsubr (&Sdump_frame_glyph_matrix);
29240 defsubr (&Sdump_glyph_matrix);
29241 defsubr (&Sdump_glyph_row);
29242 defsubr (&Sdump_tool_bar_row);
29243 defsubr (&Strace_redisplay);
29244 defsubr (&Strace_to_stderr);
29245 #endif
29246 #ifdef HAVE_WINDOW_SYSTEM
29247 defsubr (&Stool_bar_lines_needed);
29248 defsubr (&Slookup_image_map);
29249 #endif
29250 defsubr (&Sline_pixel_height);
29251 defsubr (&Sformat_mode_line);
29252 defsubr (&Sinvisible_p);
29253 defsubr (&Scurrent_bidi_paragraph_direction);
29254 defsubr (&Smove_point_visually);
29255
29256 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29257 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29258 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29259 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29260 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29261 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29262 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29263 DEFSYM (Qeval, "eval");
29264 DEFSYM (QCdata, ":data");
29265 DEFSYM (Qdisplay, "display");
29266 DEFSYM (Qspace_width, "space-width");
29267 DEFSYM (Qraise, "raise");
29268 DEFSYM (Qslice, "slice");
29269 DEFSYM (Qspace, "space");
29270 DEFSYM (Qmargin, "margin");
29271 DEFSYM (Qpointer, "pointer");
29272 DEFSYM (Qleft_margin, "left-margin");
29273 DEFSYM (Qright_margin, "right-margin");
29274 DEFSYM (Qcenter, "center");
29275 DEFSYM (Qline_height, "line-height");
29276 DEFSYM (QCalign_to, ":align-to");
29277 DEFSYM (QCrelative_width, ":relative-width");
29278 DEFSYM (QCrelative_height, ":relative-height");
29279 DEFSYM (QCeval, ":eval");
29280 DEFSYM (QCpropertize, ":propertize");
29281 DEFSYM (QCfile, ":file");
29282 DEFSYM (Qfontified, "fontified");
29283 DEFSYM (Qfontification_functions, "fontification-functions");
29284 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29285 DEFSYM (Qescape_glyph, "escape-glyph");
29286 DEFSYM (Qnobreak_space, "nobreak-space");
29287 DEFSYM (Qimage, "image");
29288 DEFSYM (Qtext, "text");
29289 DEFSYM (Qboth, "both");
29290 DEFSYM (Qboth_horiz, "both-horiz");
29291 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29292 DEFSYM (QCmap, ":map");
29293 DEFSYM (QCpointer, ":pointer");
29294 DEFSYM (Qrect, "rect");
29295 DEFSYM (Qcircle, "circle");
29296 DEFSYM (Qpoly, "poly");
29297 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29298 DEFSYM (Qgrow_only, "grow-only");
29299 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29300 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29301 DEFSYM (Qposition, "position");
29302 DEFSYM (Qbuffer_position, "buffer-position");
29303 DEFSYM (Qobject, "object");
29304 DEFSYM (Qbar, "bar");
29305 DEFSYM (Qhbar, "hbar");
29306 DEFSYM (Qbox, "box");
29307 DEFSYM (Qhollow, "hollow");
29308 DEFSYM (Qhand, "hand");
29309 DEFSYM (Qarrow, "arrow");
29310 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29311
29312 list_of_error = list1 (list2 (intern_c_string ("error"),
29313 intern_c_string ("void-variable")));
29314 staticpro (&list_of_error);
29315
29316 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29317 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29318 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29319 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29320
29321 echo_buffer[0] = echo_buffer[1] = Qnil;
29322 staticpro (&echo_buffer[0]);
29323 staticpro (&echo_buffer[1]);
29324
29325 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29326 staticpro (&echo_area_buffer[0]);
29327 staticpro (&echo_area_buffer[1]);
29328
29329 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29330 staticpro (&Vmessages_buffer_name);
29331
29332 mode_line_proptrans_alist = Qnil;
29333 staticpro (&mode_line_proptrans_alist);
29334 mode_line_string_list = Qnil;
29335 staticpro (&mode_line_string_list);
29336 mode_line_string_face = Qnil;
29337 staticpro (&mode_line_string_face);
29338 mode_line_string_face_prop = Qnil;
29339 staticpro (&mode_line_string_face_prop);
29340 Vmode_line_unwind_vector = Qnil;
29341 staticpro (&Vmode_line_unwind_vector);
29342
29343 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29344
29345 help_echo_string = Qnil;
29346 staticpro (&help_echo_string);
29347 help_echo_object = Qnil;
29348 staticpro (&help_echo_object);
29349 help_echo_window = Qnil;
29350 staticpro (&help_echo_window);
29351 previous_help_echo_string = Qnil;
29352 staticpro (&previous_help_echo_string);
29353 help_echo_pos = -1;
29354
29355 DEFSYM (Qright_to_left, "right-to-left");
29356 DEFSYM (Qleft_to_right, "left-to-right");
29357
29358 #ifdef HAVE_WINDOW_SYSTEM
29359 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29360 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29361 For example, if a block cursor is over a tab, it will be drawn as
29362 wide as that tab on the display. */);
29363 x_stretch_cursor_p = 0;
29364 #endif
29365
29366 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29367 doc: /* Non-nil means highlight trailing whitespace.
29368 The face used for trailing whitespace is `trailing-whitespace'. */);
29369 Vshow_trailing_whitespace = Qnil;
29370
29371 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29372 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29373 If the value is t, Emacs highlights non-ASCII chars which have the
29374 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29375 or `escape-glyph' face respectively.
29376
29377 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29378 U+2011 (non-breaking hyphen) are affected.
29379
29380 Any other non-nil value means to display these characters as a escape
29381 glyph followed by an ordinary space or hyphen.
29382
29383 A value of nil means no special handling of these characters. */);
29384 Vnobreak_char_display = Qt;
29385
29386 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29387 doc: /* The pointer shape to show in void text areas.
29388 A value of nil means to show the text pointer. Other options are `arrow',
29389 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29390 Vvoid_text_area_pointer = Qarrow;
29391
29392 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29393 doc: /* Non-nil means don't actually do any redisplay.
29394 This is used for internal purposes. */);
29395 Vinhibit_redisplay = Qnil;
29396
29397 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29398 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29399 Vglobal_mode_string = Qnil;
29400
29401 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29402 doc: /* Marker for where to display an arrow on top of the buffer text.
29403 This must be the beginning of a line in order to work.
29404 See also `overlay-arrow-string'. */);
29405 Voverlay_arrow_position = Qnil;
29406
29407 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29408 doc: /* String to display as an arrow in non-window frames.
29409 See also `overlay-arrow-position'. */);
29410 Voverlay_arrow_string = build_pure_c_string ("=>");
29411
29412 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29413 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29414 The symbols on this list are examined during redisplay to determine
29415 where to display overlay arrows. */);
29416 Voverlay_arrow_variable_list
29417 = list1 (intern_c_string ("overlay-arrow-position"));
29418
29419 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29420 doc: /* The number of lines to try scrolling a window by when point moves out.
29421 If that fails to bring point back on frame, point is centered instead.
29422 If this is zero, point is always centered after it moves off frame.
29423 If you want scrolling to always be a line at a time, you should set
29424 `scroll-conservatively' to a large value rather than set this to 1. */);
29425
29426 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29427 doc: /* Scroll up to this many lines, to bring point back on screen.
29428 If point moves off-screen, redisplay will scroll by up to
29429 `scroll-conservatively' lines in order to bring point just barely
29430 onto the screen again. If that cannot be done, then redisplay
29431 recenters point as usual.
29432
29433 If the value is greater than 100, redisplay will never recenter point,
29434 but will always scroll just enough text to bring point into view, even
29435 if you move far away.
29436
29437 A value of zero means always recenter point if it moves off screen. */);
29438 scroll_conservatively = 0;
29439
29440 DEFVAR_INT ("scroll-margin", scroll_margin,
29441 doc: /* Number of lines of margin at the top and bottom of a window.
29442 Recenter the window whenever point gets within this many lines
29443 of the top or bottom of the window. */);
29444 scroll_margin = 0;
29445
29446 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29447 doc: /* Pixels per inch value for non-window system displays.
29448 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29449 Vdisplay_pixels_per_inch = make_float (72.0);
29450
29451 #ifdef GLYPH_DEBUG
29452 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29453 #endif
29454
29455 DEFVAR_LISP ("truncate-partial-width-windows",
29456 Vtruncate_partial_width_windows,
29457 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29458 For an integer value, truncate lines in each window narrower than the
29459 full frame width, provided the window width is less than that integer;
29460 otherwise, respect the value of `truncate-lines'.
29461
29462 For any other non-nil value, truncate lines in all windows that do
29463 not span the full frame width.
29464
29465 A value of nil means to respect the value of `truncate-lines'.
29466
29467 If `word-wrap' is enabled, you might want to reduce this. */);
29468 Vtruncate_partial_width_windows = make_number (50);
29469
29470 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29471 doc: /* Maximum buffer size for which line number should be displayed.
29472 If the buffer is bigger than this, the line number does not appear
29473 in the mode line. A value of nil means no limit. */);
29474 Vline_number_display_limit = Qnil;
29475
29476 DEFVAR_INT ("line-number-display-limit-width",
29477 line_number_display_limit_width,
29478 doc: /* Maximum line width (in characters) for line number display.
29479 If the average length of the lines near point is bigger than this, then the
29480 line number may be omitted from the mode line. */);
29481 line_number_display_limit_width = 200;
29482
29483 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29484 doc: /* Non-nil means highlight region even in nonselected windows. */);
29485 highlight_nonselected_windows = 0;
29486
29487 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29488 doc: /* Non-nil if more than one frame is visible on this display.
29489 Minibuffer-only frames don't count, but iconified frames do.
29490 This variable is not guaranteed to be accurate except while processing
29491 `frame-title-format' and `icon-title-format'. */);
29492
29493 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29494 doc: /* Template for displaying the title bar of visible frames.
29495 \(Assuming the window manager supports this feature.)
29496
29497 This variable has the same structure as `mode-line-format', except that
29498 the %c and %l constructs are ignored. It is used only on frames for
29499 which no explicit name has been set \(see `modify-frame-parameters'). */);
29500
29501 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29502 doc: /* Template for displaying the title bar of an iconified frame.
29503 \(Assuming the window manager supports this feature.)
29504 This variable has the same structure as `mode-line-format' (which see),
29505 and is used only on frames for which no explicit name has been set
29506 \(see `modify-frame-parameters'). */);
29507 Vicon_title_format
29508 = Vframe_title_format
29509 = listn (CONSTYPE_PURE, 3,
29510 intern_c_string ("multiple-frames"),
29511 build_pure_c_string ("%b"),
29512 listn (CONSTYPE_PURE, 4,
29513 empty_unibyte_string,
29514 intern_c_string ("invocation-name"),
29515 build_pure_c_string ("@"),
29516 intern_c_string ("system-name")));
29517
29518 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29519 doc: /* Maximum number of lines to keep in the message log buffer.
29520 If nil, disable message logging. If t, log messages but don't truncate
29521 the buffer when it becomes large. */);
29522 Vmessage_log_max = make_number (1000);
29523
29524 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29525 doc: /* Functions called before redisplay, if window sizes have changed.
29526 The value should be a list of functions that take one argument.
29527 Just before redisplay, for each frame, if any of its windows have changed
29528 size since the last redisplay, or have been split or deleted,
29529 all the functions in the list are called, with the frame as argument. */);
29530 Vwindow_size_change_functions = Qnil;
29531
29532 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29533 doc: /* List of functions to call before redisplaying a window with scrolling.
29534 Each function is called with two arguments, the window and its new
29535 display-start position. Note that these functions are also called by
29536 `set-window-buffer'. Also note that the value of `window-end' is not
29537 valid when these functions are called.
29538
29539 Warning: Do not use this feature to alter the way the window
29540 is scrolled. It is not designed for that, and such use probably won't
29541 work. */);
29542 Vwindow_scroll_functions = Qnil;
29543
29544 DEFVAR_LISP ("window-text-change-functions",
29545 Vwindow_text_change_functions,
29546 doc: /* Functions to call in redisplay when text in the window might change. */);
29547 Vwindow_text_change_functions = Qnil;
29548
29549 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29550 doc: /* Functions called when redisplay of a window reaches the end trigger.
29551 Each function is called with two arguments, the window and the end trigger value.
29552 See `set-window-redisplay-end-trigger'. */);
29553 Vredisplay_end_trigger_functions = Qnil;
29554
29555 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29556 doc: /* Non-nil means autoselect window with mouse pointer.
29557 If nil, do not autoselect windows.
29558 A positive number means delay autoselection by that many seconds: a
29559 window is autoselected only after the mouse has remained in that
29560 window for the duration of the delay.
29561 A negative number has a similar effect, but causes windows to be
29562 autoselected only after the mouse has stopped moving. \(Because of
29563 the way Emacs compares mouse events, you will occasionally wait twice
29564 that time before the window gets selected.\)
29565 Any other value means to autoselect window instantaneously when the
29566 mouse pointer enters it.
29567
29568 Autoselection selects the minibuffer only if it is active, and never
29569 unselects the minibuffer if it is active.
29570
29571 When customizing this variable make sure that the actual value of
29572 `focus-follows-mouse' matches the behavior of your window manager. */);
29573 Vmouse_autoselect_window = Qnil;
29574
29575 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29576 doc: /* Non-nil means automatically resize tool-bars.
29577 This dynamically changes the tool-bar's height to the minimum height
29578 that is needed to make all tool-bar items visible.
29579 If value is `grow-only', the tool-bar's height is only increased
29580 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29581 Vauto_resize_tool_bars = Qt;
29582
29583 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29584 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29585 auto_raise_tool_bar_buttons_p = 1;
29586
29587 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29588 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29589 make_cursor_line_fully_visible_p = 1;
29590
29591 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29592 doc: /* Border below tool-bar in pixels.
29593 If an integer, use it as the height of the border.
29594 If it is one of `internal-border-width' or `border-width', use the
29595 value of the corresponding frame parameter.
29596 Otherwise, no border is added below the tool-bar. */);
29597 Vtool_bar_border = Qinternal_border_width;
29598
29599 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29600 doc: /* Margin around tool-bar buttons in pixels.
29601 If an integer, use that for both horizontal and vertical margins.
29602 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29603 HORZ specifying the horizontal margin, and VERT specifying the
29604 vertical margin. */);
29605 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29606
29607 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29608 doc: /* Relief thickness of tool-bar buttons. */);
29609 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29610
29611 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29612 doc: /* Tool bar style to use.
29613 It can be one of
29614 image - show images only
29615 text - show text only
29616 both - show both, text below image
29617 both-horiz - show text to the right of the image
29618 text-image-horiz - show text to the left of the image
29619 any other - use system default or image if no system default.
29620
29621 This variable only affects the GTK+ toolkit version of Emacs. */);
29622 Vtool_bar_style = Qnil;
29623
29624 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29625 doc: /* Maximum number of characters a label can have to be shown.
29626 The tool bar style must also show labels for this to have any effect, see
29627 `tool-bar-style'. */);
29628 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29629
29630 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29631 doc: /* List of functions to call to fontify regions of text.
29632 Each function is called with one argument POS. Functions must
29633 fontify a region starting at POS in the current buffer, and give
29634 fontified regions the property `fontified'. */);
29635 Vfontification_functions = Qnil;
29636 Fmake_variable_buffer_local (Qfontification_functions);
29637
29638 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29639 unibyte_display_via_language_environment,
29640 doc: /* Non-nil means display unibyte text according to language environment.
29641 Specifically, this means that raw bytes in the range 160-255 decimal
29642 are displayed by converting them to the equivalent multibyte characters
29643 according to the current language environment. As a result, they are
29644 displayed according to the current fontset.
29645
29646 Note that this variable affects only how these bytes are displayed,
29647 but does not change the fact they are interpreted as raw bytes. */);
29648 unibyte_display_via_language_environment = 0;
29649
29650 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29651 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29652 If a float, it specifies a fraction of the mini-window frame's height.
29653 If an integer, it specifies a number of lines. */);
29654 Vmax_mini_window_height = make_float (0.25);
29655
29656 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29657 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29658 A value of nil means don't automatically resize mini-windows.
29659 A value of t means resize them to fit the text displayed in them.
29660 A value of `grow-only', the default, means let mini-windows grow only;
29661 they return to their normal size when the minibuffer is closed, or the
29662 echo area becomes empty. */);
29663 Vresize_mini_windows = Qgrow_only;
29664
29665 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29666 doc: /* Alist specifying how to blink the cursor off.
29667 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29668 `cursor-type' frame-parameter or variable equals ON-STATE,
29669 comparing using `equal', Emacs uses OFF-STATE to specify
29670 how to blink it off. ON-STATE and OFF-STATE are values for
29671 the `cursor-type' frame parameter.
29672
29673 If a frame's ON-STATE has no entry in this list,
29674 the frame's other specifications determine how to blink the cursor off. */);
29675 Vblink_cursor_alist = Qnil;
29676
29677 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29678 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29679 If non-nil, windows are automatically scrolled horizontally to make
29680 point visible. */);
29681 automatic_hscrolling_p = 1;
29682 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29683
29684 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29685 doc: /* How many columns away from the window edge point is allowed to get
29686 before automatic hscrolling will horizontally scroll the window. */);
29687 hscroll_margin = 5;
29688
29689 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29690 doc: /* How many columns to scroll the window when point gets too close to the edge.
29691 When point is less than `hscroll-margin' columns from the window
29692 edge, automatic hscrolling will scroll the window by the amount of columns
29693 determined by this variable. If its value is a positive integer, scroll that
29694 many columns. If it's a positive floating-point number, it specifies the
29695 fraction of the window's width to scroll. If it's nil or zero, point will be
29696 centered horizontally after the scroll. Any other value, including negative
29697 numbers, are treated as if the value were zero.
29698
29699 Automatic hscrolling always moves point outside the scroll margin, so if
29700 point was more than scroll step columns inside the margin, the window will
29701 scroll more than the value given by the scroll step.
29702
29703 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29704 and `scroll-right' overrides this variable's effect. */);
29705 Vhscroll_step = make_number (0);
29706
29707 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29708 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29709 Bind this around calls to `message' to let it take effect. */);
29710 message_truncate_lines = 0;
29711
29712 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29713 doc: /* Normal hook run to update the menu bar definitions.
29714 Redisplay runs this hook before it redisplays the menu bar.
29715 This is used to update submenus such as Buffers,
29716 whose contents depend on various data. */);
29717 Vmenu_bar_update_hook = Qnil;
29718
29719 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29720 doc: /* Frame for which we are updating a menu.
29721 The enable predicate for a menu binding should check this variable. */);
29722 Vmenu_updating_frame = Qnil;
29723
29724 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29725 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29726 inhibit_menubar_update = 0;
29727
29728 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29729 doc: /* Prefix prepended to all continuation lines at display time.
29730 The value may be a string, an image, or a stretch-glyph; it is
29731 interpreted in the same way as the value of a `display' text property.
29732
29733 This variable is overridden by any `wrap-prefix' text or overlay
29734 property.
29735
29736 To add a prefix to non-continuation lines, use `line-prefix'. */);
29737 Vwrap_prefix = Qnil;
29738 DEFSYM (Qwrap_prefix, "wrap-prefix");
29739 Fmake_variable_buffer_local (Qwrap_prefix);
29740
29741 DEFVAR_LISP ("line-prefix", Vline_prefix,
29742 doc: /* Prefix prepended to all non-continuation lines at display time.
29743 The value may be a string, an image, or a stretch-glyph; it is
29744 interpreted in the same way as the value of a `display' text property.
29745
29746 This variable is overridden by any `line-prefix' text or overlay
29747 property.
29748
29749 To add a prefix to continuation lines, use `wrap-prefix'. */);
29750 Vline_prefix = Qnil;
29751 DEFSYM (Qline_prefix, "line-prefix");
29752 Fmake_variable_buffer_local (Qline_prefix);
29753
29754 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29755 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29756 inhibit_eval_during_redisplay = 0;
29757
29758 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29759 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29760 inhibit_free_realized_faces = 0;
29761
29762 #ifdef GLYPH_DEBUG
29763 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29764 doc: /* Inhibit try_window_id display optimization. */);
29765 inhibit_try_window_id = 0;
29766
29767 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29768 doc: /* Inhibit try_window_reusing display optimization. */);
29769 inhibit_try_window_reusing = 0;
29770
29771 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29772 doc: /* Inhibit try_cursor_movement display optimization. */);
29773 inhibit_try_cursor_movement = 0;
29774 #endif /* GLYPH_DEBUG */
29775
29776 DEFVAR_INT ("overline-margin", overline_margin,
29777 doc: /* Space between overline and text, in pixels.
29778 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29779 margin to the character height. */);
29780 overline_margin = 2;
29781
29782 DEFVAR_INT ("underline-minimum-offset",
29783 underline_minimum_offset,
29784 doc: /* Minimum distance between baseline and underline.
29785 This can improve legibility of underlined text at small font sizes,
29786 particularly when using variable `x-use-underline-position-properties'
29787 with fonts that specify an UNDERLINE_POSITION relatively close to the
29788 baseline. The default value is 1. */);
29789 underline_minimum_offset = 1;
29790
29791 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29792 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29793 This feature only works when on a window system that can change
29794 cursor shapes. */);
29795 display_hourglass_p = 1;
29796
29797 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29798 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29799 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29800
29801 #ifdef HAVE_WINDOW_SYSTEM
29802 hourglass_atimer = NULL;
29803 hourglass_shown_p = 0;
29804 #endif /* HAVE_WINDOW_SYSTEM */
29805
29806 DEFSYM (Qglyphless_char, "glyphless-char");
29807 DEFSYM (Qhex_code, "hex-code");
29808 DEFSYM (Qempty_box, "empty-box");
29809 DEFSYM (Qthin_space, "thin-space");
29810 DEFSYM (Qzero_width, "zero-width");
29811
29812 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
29813 doc: /* Function run just before redisplay.
29814 It is called with one argument, which is the set of windows that are to
29815 be redisplayed. This set can be nil (meaning, only the selected window),
29816 or t (meaning all windows). */);
29817 Vpre_redisplay_function = intern ("ignore");
29818
29819 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29820 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29821
29822 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29823 doc: /* Char-table defining glyphless characters.
29824 Each element, if non-nil, should be one of the following:
29825 an ASCII acronym string: display this string in a box
29826 `hex-code': display the hexadecimal code of a character in a box
29827 `empty-box': display as an empty box
29828 `thin-space': display as 1-pixel width space
29829 `zero-width': don't display
29830 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29831 display method for graphical terminals and text terminals respectively.
29832 GRAPHICAL and TEXT should each have one of the values listed above.
29833
29834 The char-table has one extra slot to control the display of a character for
29835 which no font is found. This slot only takes effect on graphical terminals.
29836 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29837 `thin-space'. The default is `empty-box'.
29838
29839 If a character has a non-nil entry in an active display table, the
29840 display table takes effect; in this case, Emacs does not consult
29841 `glyphless-char-display' at all. */);
29842 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29843 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29844 Qempty_box);
29845
29846 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29847 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29848 Vdebug_on_message = Qnil;
29849
29850 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
29851 doc: /* */);
29852 Vredisplay__all_windows_cause
29853 = Fmake_vector (make_number (100), make_number (0));
29854
29855 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
29856 doc: /* */);
29857 Vredisplay__mode_lines_cause
29858 = Fmake_vector (make_number (100), make_number (0));
29859 }
29860
29861
29862 /* Initialize this module when Emacs starts. */
29863
29864 void
29865 init_xdisp (void)
29866 {
29867 CHARPOS (this_line_start_pos) = 0;
29868
29869 if (!noninteractive)
29870 {
29871 struct window *m = XWINDOW (minibuf_window);
29872 Lisp_Object frame = m->frame;
29873 struct frame *f = XFRAME (frame);
29874 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29875 struct window *r = XWINDOW (root);
29876 int i;
29877
29878 echo_area_window = minibuf_window;
29879
29880 r->top_line = FRAME_TOP_MARGIN (f);
29881 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29882 r->total_cols = FRAME_COLS (f);
29883
29884 m->top_line = FRAME_LINES (f) - 1;
29885 m->total_lines = 1;
29886 m->total_cols = FRAME_COLS (f);
29887
29888 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29889 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29890 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29891
29892 /* The default ellipsis glyphs `...'. */
29893 for (i = 0; i < 3; ++i)
29894 default_invis_vector[i] = make_number ('.');
29895 }
29896
29897 {
29898 /* Allocate the buffer for frame titles.
29899 Also used for `format-mode-line'. */
29900 int size = 100;
29901 mode_line_noprop_buf = xmalloc (size);
29902 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29903 mode_line_noprop_ptr = mode_line_noprop_buf;
29904 mode_line_target = MODE_LINE_DISPLAY;
29905 }
29906
29907 help_echo_showing_p = 0;
29908 }
29909
29910 #ifdef HAVE_WINDOW_SYSTEM
29911
29912 /* Platform-independent portion of hourglass implementation. */
29913
29914 /* Cancel a currently active hourglass timer, and start a new one. */
29915 void
29916 start_hourglass (void)
29917 {
29918 struct timespec delay;
29919
29920 cancel_hourglass ();
29921
29922 if (INTEGERP (Vhourglass_delay)
29923 && XINT (Vhourglass_delay) > 0)
29924 delay = make_timespec (min (XINT (Vhourglass_delay),
29925 TYPE_MAXIMUM (time_t)),
29926 0);
29927 else if (FLOATP (Vhourglass_delay)
29928 && XFLOAT_DATA (Vhourglass_delay) > 0)
29929 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29930 else
29931 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29932
29933 #ifdef HAVE_NTGUI
29934 {
29935 extern void w32_note_current_window (void);
29936 w32_note_current_window ();
29937 }
29938 #endif /* HAVE_NTGUI */
29939
29940 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29941 show_hourglass, NULL);
29942 }
29943
29944
29945 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29946 shown. */
29947 void
29948 cancel_hourglass (void)
29949 {
29950 if (hourglass_atimer)
29951 {
29952 cancel_atimer (hourglass_atimer);
29953 hourglass_atimer = NULL;
29954 }
29955
29956 if (hourglass_shown_p)
29957 hide_hourglass ();
29958 }
29959
29960 #endif /* HAVE_WINDOW_SYSTEM */